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