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