1 /* $NetBSD: spec_vnops.c,v 1.181 2020/12/25 09:28:56 mlelstv 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.181 2020/12/25 09:28:56 mlelstv 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 int i, nra; 699 daddr_t lastbn, *rablks; 700 int *rasizes; 701 int nrablks, ratogo; 702 703 KASSERT(uio->uio_rw == UIO_READ); 704 KASSERTMSG(VMSPACE_IS_KERNEL_P(uio->uio_vmspace) || 705 uio->uio_vmspace == curproc->p_vmspace, 706 "vmspace belongs to neither kernel nor curproc"); 707 708 if (uio->uio_resid == 0) 709 return (0); 710 711 switch (vp->v_type) { 712 713 case VCHR: 714 VOP_UNLOCK(vp); 715 error = cdev_read(vp->v_rdev, uio, ap->a_ioflag); 716 vn_lock(vp, LK_SHARED | LK_RETRY); 717 return (error); 718 719 case VBLK: 720 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 721 if (uio->uio_offset < 0) 722 return (EINVAL); 723 724 if (bdev_ioctl(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, l) == 0) 725 bsize = imin(imax(pi.pi_bsize, DEV_BSIZE), MAXBSIZE); 726 else 727 bsize = BLKDEV_IOSIZE; 728 729 bscale = bsize >> DEV_BSHIFT; 730 731 nra = uimax(16 * MAXPHYS / bsize - 1, 511); 732 rablks = kmem_alloc(nra * sizeof(*rablks), KM_SLEEP); 733 rasizes = kmem_alloc(nra * sizeof(*rasizes), KM_SLEEP); 734 lastbn = ((uio->uio_offset + uio->uio_resid - 1) >> DEV_BSHIFT) 735 &~ (bscale - 1); 736 nrablks = ratogo = 0; 737 do { 738 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1); 739 on = uio->uio_offset % bsize; 740 n = uimin((unsigned)(bsize - on), uio->uio_resid); 741 742 if (ratogo == 0) { 743 nrablks = uimin((lastbn - bn) / bscale, nra); 744 ratogo = nrablks; 745 746 for (i = 0; i < nrablks; ++i) { 747 rablks[i] = bn + (i+1) * bscale; 748 rasizes[i] = bsize; 749 } 750 751 error = breadn(vp, bn, bsize, 752 rablks, rasizes, nrablks, 753 0, &bp); 754 } else { 755 if (ratogo > 0) 756 --ratogo; 757 error = bread(vp, bn, bsize, 0, &bp); 758 } 759 if (error) 760 break; 761 n = uimin(n, bsize - bp->b_resid); 762 error = uiomove((char *)bp->b_data + on, n, uio); 763 brelse(bp, 0); 764 } while (error == 0 && uio->uio_resid > 0 && n != 0); 765 766 kmem_free(rablks, nra * sizeof(*rablks)); 767 kmem_free(rasizes, nra * sizeof(*rasizes)); 768 769 return (error); 770 771 default: 772 panic("spec_read type"); 773 } 774 /* NOTREACHED */ 775 } 776 777 /* 778 * Vnode op for write 779 */ 780 /* ARGSUSED */ 781 int 782 spec_write(void *v) 783 { 784 struct vop_write_args /* { 785 struct vnode *a_vp; 786 struct uio *a_uio; 787 int a_ioflag; 788 kauth_cred_t a_cred; 789 } */ *ap = v; 790 struct vnode *vp = ap->a_vp; 791 struct uio *uio = ap->a_uio; 792 struct lwp *l = curlwp; 793 struct buf *bp; 794 daddr_t bn; 795 int bsize, bscale; 796 struct partinfo pi; 797 int n, on; 798 int error = 0; 799 800 KASSERT(uio->uio_rw == UIO_WRITE); 801 KASSERTMSG(VMSPACE_IS_KERNEL_P(uio->uio_vmspace) || 802 uio->uio_vmspace == curproc->p_vmspace, 803 "vmspace belongs to neither kernel nor curproc"); 804 805 switch (vp->v_type) { 806 807 case VCHR: 808 VOP_UNLOCK(vp); 809 error = cdev_write(vp->v_rdev, uio, ap->a_ioflag); 810 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 811 return (error); 812 813 case VBLK: 814 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 815 if (uio->uio_resid == 0) 816 return (0); 817 if (uio->uio_offset < 0) 818 return (EINVAL); 819 820 if (bdev_ioctl(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, l) == 0) 821 bsize = imin(imax(pi.pi_bsize, DEV_BSIZE), MAXBSIZE); 822 else 823 bsize = BLKDEV_IOSIZE; 824 825 bscale = bsize >> DEV_BSHIFT; 826 do { 827 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1); 828 on = uio->uio_offset % bsize; 829 n = uimin((unsigned)(bsize - on), uio->uio_resid); 830 if (n == bsize) 831 bp = getblk(vp, bn, bsize, 0, 0); 832 else 833 error = bread(vp, bn, bsize, B_MODIFY, &bp); 834 if (error) { 835 return (error); 836 } 837 n = uimin(n, bsize - bp->b_resid); 838 error = uiomove((char *)bp->b_data + on, n, uio); 839 if (error) 840 brelse(bp, 0); 841 else { 842 if (n + on == bsize) 843 bawrite(bp); 844 else 845 bdwrite(bp); 846 error = bp->b_error; 847 } 848 } while (error == 0 && uio->uio_resid > 0 && n != 0); 849 return (error); 850 851 default: 852 panic("spec_write type"); 853 } 854 /* NOTREACHED */ 855 } 856 857 /* 858 * fdiscard, which on disk devices becomes TRIM. 859 */ 860 int 861 spec_fdiscard(void *v) 862 { 863 struct vop_fdiscard_args /* { 864 struct vnode *a_vp; 865 off_t a_pos; 866 off_t a_len; 867 } */ *ap = v; 868 struct vnode *vp; 869 dev_t dev; 870 871 vp = ap->a_vp; 872 dev = NODEV; 873 874 mutex_enter(vp->v_interlock); 875 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode != NULL) { 876 dev = vp->v_rdev; 877 } 878 mutex_exit(vp->v_interlock); 879 880 if (dev == NODEV) { 881 return ENXIO; 882 } 883 884 switch (vp->v_type) { 885 case VCHR: 886 // this is not stored for character devices 887 //KASSERT(vp == vp->v_specnode->sn_dev->sd_cdevvp); 888 return cdev_discard(dev, ap->a_pos, ap->a_len); 889 case VBLK: 890 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 891 return bdev_discard(dev, ap->a_pos, ap->a_len); 892 default: 893 panic("spec_fdiscard: not a device\n"); 894 } 895 } 896 897 /* 898 * Device ioctl operation. 899 */ 900 /* ARGSUSED */ 901 int 902 spec_ioctl(void *v) 903 { 904 struct vop_ioctl_args /* { 905 struct vnode *a_vp; 906 u_long a_command; 907 void *a_data; 908 int a_fflag; 909 kauth_cred_t a_cred; 910 } */ *ap = v; 911 struct vnode *vp; 912 dev_t dev; 913 914 /* 915 * Extract all the info we need from the vnode, taking care to 916 * avoid a race with VOP_REVOKE(). 917 */ 918 919 vp = ap->a_vp; 920 dev = NODEV; 921 mutex_enter(vp->v_interlock); 922 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) { 923 dev = vp->v_rdev; 924 } 925 mutex_exit(vp->v_interlock); 926 if (dev == NODEV) { 927 return ENXIO; 928 } 929 930 switch (vp->v_type) { 931 932 case VCHR: 933 return cdev_ioctl(dev, ap->a_command, ap->a_data, 934 ap->a_fflag, curlwp); 935 936 case VBLK: 937 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 938 return bdev_ioctl(dev, ap->a_command, ap->a_data, 939 ap->a_fflag, curlwp); 940 941 default: 942 panic("spec_ioctl"); 943 /* NOTREACHED */ 944 } 945 } 946 947 /* ARGSUSED */ 948 int 949 spec_poll(void *v) 950 { 951 struct vop_poll_args /* { 952 struct vnode *a_vp; 953 int a_events; 954 } */ *ap = v; 955 struct vnode *vp; 956 dev_t dev; 957 958 /* 959 * Extract all the info we need from the vnode, taking care to 960 * avoid a race with VOP_REVOKE(). 961 */ 962 963 vp = ap->a_vp; 964 dev = NODEV; 965 mutex_enter(vp->v_interlock); 966 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) { 967 dev = vp->v_rdev; 968 } 969 mutex_exit(vp->v_interlock); 970 if (dev == NODEV) { 971 return POLLERR; 972 } 973 974 switch (vp->v_type) { 975 976 case VCHR: 977 return cdev_poll(dev, ap->a_events, curlwp); 978 979 default: 980 return (genfs_poll(v)); 981 } 982 } 983 984 /* ARGSUSED */ 985 int 986 spec_kqfilter(void *v) 987 { 988 struct vop_kqfilter_args /* { 989 struct vnode *a_vp; 990 struct proc *a_kn; 991 } */ *ap = v; 992 dev_t dev; 993 994 switch (ap->a_vp->v_type) { 995 996 case VCHR: 997 dev = ap->a_vp->v_rdev; 998 return cdev_kqfilter(dev, ap->a_kn); 999 default: 1000 /* 1001 * Block devices don't support kqfilter, and refuse it 1002 * for any other files (like those vflush()ed) too. 1003 */ 1004 return (EOPNOTSUPP); 1005 } 1006 } 1007 1008 /* 1009 * Allow mapping of only D_DISK. This is called only for VBLK. 1010 */ 1011 int 1012 spec_mmap(void *v) 1013 { 1014 struct vop_mmap_args /* { 1015 struct vnode *a_vp; 1016 vm_prot_t a_prot; 1017 kauth_cred_t a_cred; 1018 } */ *ap = v; 1019 struct vnode *vp = ap->a_vp; 1020 1021 KASSERT(vp->v_type == VBLK); 1022 if (bdev_type(vp->v_rdev) != D_DISK) 1023 return EINVAL; 1024 1025 return 0; 1026 } 1027 1028 /* 1029 * Synch buffers associated with a block device 1030 */ 1031 /* ARGSUSED */ 1032 int 1033 spec_fsync(void *v) 1034 { 1035 struct vop_fsync_args /* { 1036 struct vnode *a_vp; 1037 kauth_cred_t a_cred; 1038 int a_flags; 1039 off_t offlo; 1040 off_t offhi; 1041 } */ *ap = v; 1042 struct vnode *vp = ap->a_vp; 1043 struct mount *mp; 1044 int error; 1045 1046 if (vp->v_type == VBLK) { 1047 if ((mp = spec_node_getmountedfs(vp)) != NULL) { 1048 error = VFS_FSYNC(mp, vp, ap->a_flags); 1049 if (error != EOPNOTSUPP) 1050 return error; 1051 } 1052 return vflushbuf(vp, ap->a_flags); 1053 } 1054 return (0); 1055 } 1056 1057 /* 1058 * Just call the device strategy routine 1059 */ 1060 int 1061 spec_strategy(void *v) 1062 { 1063 struct vop_strategy_args /* { 1064 struct vnode *a_vp; 1065 struct buf *a_bp; 1066 } */ *ap = v; 1067 struct vnode *vp = ap->a_vp; 1068 struct buf *bp = ap->a_bp; 1069 dev_t dev; 1070 int error; 1071 1072 dev = NODEV; 1073 1074 /* 1075 * Extract all the info we need from the vnode, taking care to 1076 * avoid a race with VOP_REVOKE(). 1077 */ 1078 1079 mutex_enter(vp->v_interlock); 1080 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode != NULL) { 1081 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 1082 dev = vp->v_rdev; 1083 } 1084 mutex_exit(vp->v_interlock); 1085 1086 if (dev == NODEV) { 1087 error = ENXIO; 1088 goto out; 1089 } 1090 bp->b_dev = dev; 1091 1092 if (!(bp->b_flags & B_READ)) { 1093 #ifdef DIAGNOSTIC 1094 if (bp->b_vp && bp->b_vp->v_type == VBLK) { 1095 struct mount *mp = spec_node_getmountedfs(bp->b_vp); 1096 1097 if (mp && (mp->mnt_flag & MNT_RDONLY)) { 1098 printf("%s blk %"PRId64" written while ro!\n", 1099 mp->mnt_stat.f_mntonname, bp->b_blkno); 1100 } 1101 } 1102 #endif /* DIAGNOSTIC */ 1103 error = fscow_run(bp, false); 1104 if (error) 1105 goto out; 1106 } 1107 bdev_strategy(bp); 1108 1109 return 0; 1110 1111 out: 1112 bp->b_error = error; 1113 bp->b_resid = bp->b_bcount; 1114 biodone(bp); 1115 1116 return error; 1117 } 1118 1119 int 1120 spec_inactive(void *v) 1121 { 1122 struct vop_inactive_v2_args /* { 1123 struct vnode *a_vp; 1124 struct bool *a_recycle; 1125 } */ *ap = v; 1126 1127 KASSERT(ap->a_vp->v_mount == dead_rootmount); 1128 *ap->a_recycle = true; 1129 1130 return 0; 1131 } 1132 1133 int 1134 spec_reclaim(void *v) 1135 { 1136 struct vop_reclaim_v2_args /* { 1137 struct vnode *a_vp; 1138 } */ *ap = v; 1139 struct vnode *vp = ap->a_vp; 1140 1141 VOP_UNLOCK(vp); 1142 1143 KASSERT(vp->v_mount == dead_rootmount); 1144 return 0; 1145 } 1146 1147 /* 1148 * This is a noop, simply returning what one has been given. 1149 */ 1150 int 1151 spec_bmap(void *v) 1152 { 1153 struct vop_bmap_args /* { 1154 struct vnode *a_vp; 1155 daddr_t a_bn; 1156 struct vnode **a_vpp; 1157 daddr_t *a_bnp; 1158 int *a_runp; 1159 } */ *ap = v; 1160 1161 if (ap->a_vpp != NULL) 1162 *ap->a_vpp = ap->a_vp; 1163 if (ap->a_bnp != NULL) 1164 *ap->a_bnp = ap->a_bn; 1165 if (ap->a_runp != NULL) 1166 *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1; 1167 return (0); 1168 } 1169 1170 /* 1171 * Device close routine 1172 */ 1173 /* ARGSUSED */ 1174 int 1175 spec_close(void *v) 1176 { 1177 struct vop_close_args /* { 1178 struct vnode *a_vp; 1179 int a_fflag; 1180 kauth_cred_t a_cred; 1181 } */ *ap = v; 1182 struct vnode *vp = ap->a_vp; 1183 struct session *sess; 1184 dev_t dev = vp->v_rdev; 1185 int flags = ap->a_fflag; 1186 int mode, error, count; 1187 specnode_t *sn; 1188 specdev_t *sd; 1189 1190 mutex_enter(vp->v_interlock); 1191 sn = vp->v_specnode; 1192 sd = sn->sn_dev; 1193 /* 1194 * If we're going away soon, make this non-blocking. 1195 * Also ensures that we won't wedge in vn_lock below. 1196 */ 1197 if (vdead_check(vp, VDEAD_NOWAIT) != 0) 1198 flags |= FNONBLOCK; 1199 mutex_exit(vp->v_interlock); 1200 1201 switch (vp->v_type) { 1202 1203 case VCHR: 1204 /* 1205 * Hack: a tty device that is a controlling terminal 1206 * has a reference from the session structure. We 1207 * cannot easily tell that a character device is a 1208 * controlling terminal, unless it is the closing 1209 * process' controlling terminal. In that case, if the 1210 * open count is 1 release the reference from the 1211 * session. Also, remove the link from the tty back to 1212 * the session and pgrp. 1213 * 1214 * XXX V. fishy. 1215 */ 1216 mutex_enter(&proc_lock); 1217 sess = curlwp->l_proc->p_session; 1218 if (sn->sn_opencnt == 1 && vp == sess->s_ttyvp) { 1219 mutex_spin_enter(&tty_lock); 1220 sess->s_ttyvp = NULL; 1221 if (sess->s_ttyp->t_session != NULL) { 1222 sess->s_ttyp->t_pgrp = NULL; 1223 sess->s_ttyp->t_session = NULL; 1224 mutex_spin_exit(&tty_lock); 1225 /* Releases proc_lock. */ 1226 proc_sessrele(sess); 1227 } else { 1228 mutex_spin_exit(&tty_lock); 1229 if (sess->s_ttyp->t_pgrp != NULL) 1230 panic("spec_close: spurious pgrp ref"); 1231 mutex_exit(&proc_lock); 1232 } 1233 vrele(vp); 1234 } else 1235 mutex_exit(&proc_lock); 1236 1237 /* 1238 * If the vnode is locked, then we are in the midst 1239 * of forcably closing the device, otherwise we only 1240 * close on last reference. 1241 */ 1242 mode = S_IFCHR; 1243 break; 1244 1245 case VBLK: 1246 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp); 1247 /* 1248 * On last close of a block device (that isn't mounted) 1249 * we must invalidate any in core blocks, so that 1250 * we can, for instance, change floppy disks. 1251 */ 1252 error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0); 1253 if (error) 1254 return (error); 1255 /* 1256 * We do not want to really close the device if it 1257 * is still in use unless we are trying to close it 1258 * forcibly. Since every use (buffer, vnode, swap, cmap) 1259 * holds a reference to the vnode, and because we mark 1260 * any other vnodes that alias this device, when the 1261 * sum of the reference counts on all the aliased 1262 * vnodes descends to one, we are on last close. 1263 */ 1264 mode = S_IFBLK; 1265 break; 1266 1267 default: 1268 panic("spec_close: not special"); 1269 } 1270 1271 mutex_enter(&device_lock); 1272 sn->sn_opencnt--; 1273 count = --sd->sd_opencnt; 1274 if (vp->v_type == VBLK) 1275 sd->sd_bdevvp = NULL; 1276 mutex_exit(&device_lock); 1277 1278 if (count != 0 && (vp->v_type != VCHR || !(cdev_flags(dev) & D_MCLOSE))) 1279 return 0; 1280 1281 /* 1282 * If we're able to block, release the vnode lock & reacquire. We 1283 * might end up sleeping for someone else who wants our queues. They 1284 * won't get them if we hold the vnode locked. 1285 */ 1286 if (!(flags & FNONBLOCK)) 1287 VOP_UNLOCK(vp); 1288 1289 if (vp->v_type == VBLK) 1290 error = bdev_close(dev, flags, mode, curlwp); 1291 else 1292 error = cdev_close(dev, flags, mode, curlwp); 1293 1294 if (!(flags & FNONBLOCK)) 1295 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1296 1297 return (error); 1298 } 1299 1300 /* 1301 * Print out the contents of a special device vnode. 1302 */ 1303 int 1304 spec_print(void *v) 1305 { 1306 struct vop_print_args /* { 1307 struct vnode *a_vp; 1308 } */ *ap = v; 1309 1310 printf("dev %llu, %llu\n", (unsigned long long)major(ap->a_vp->v_rdev), 1311 (unsigned long long)minor(ap->a_vp->v_rdev)); 1312 return 0; 1313 } 1314 1315 /* 1316 * Return POSIX pathconf information applicable to special devices. 1317 */ 1318 int 1319 spec_pathconf(void *v) 1320 { 1321 struct vop_pathconf_args /* { 1322 struct vnode *a_vp; 1323 int a_name; 1324 register_t *a_retval; 1325 } */ *ap = v; 1326 1327 switch (ap->a_name) { 1328 case _PC_LINK_MAX: 1329 *ap->a_retval = LINK_MAX; 1330 return (0); 1331 case _PC_MAX_CANON: 1332 *ap->a_retval = MAX_CANON; 1333 return (0); 1334 case _PC_MAX_INPUT: 1335 *ap->a_retval = MAX_INPUT; 1336 return (0); 1337 case _PC_PIPE_BUF: 1338 *ap->a_retval = PIPE_BUF; 1339 return (0); 1340 case _PC_CHOWN_RESTRICTED: 1341 *ap->a_retval = 1; 1342 return (0); 1343 case _PC_VDISABLE: 1344 *ap->a_retval = _POSIX_VDISABLE; 1345 return (0); 1346 case _PC_SYNC_IO: 1347 *ap->a_retval = 1; 1348 return (0); 1349 default: 1350 return genfs_pathconf(ap); 1351 } 1352 /* NOTREACHED */ 1353 } 1354 1355 /* 1356 * Advisory record locking support. 1357 */ 1358 int 1359 spec_advlock(void *v) 1360 { 1361 struct vop_advlock_args /* { 1362 struct vnode *a_vp; 1363 void *a_id; 1364 int a_op; 1365 struct flock *a_fl; 1366 int a_flags; 1367 } */ *ap = v; 1368 struct vnode *vp = ap->a_vp; 1369 1370 return lf_advlock(ap, &vp->v_speclockf, (off_t)0); 1371 } 1372