sys/kern/kern_device.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | /* * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> All rights reserved. * cdevsw from kern/kern_conf.c Copyright (c) 1995 Terrence R. Lambert * cdevsw from kern/kern_conf.c Copyright (c) 1995 Julian R. Elischer, * All rights reserved. * Copyright (c) 1982, 1986, 1991, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/sysctl.h> #include <sys/module.h> #include <sys/malloc.h> #include <sys/conf.h> #include <sys/bio.h> #include <sys/buf.h> #include <sys/vnode.h> #include <sys/queue.h> #include <sys/device.h> #include <sys/tree.h> #include <sys/syslink_rpc.h> #include <sys/proc.h> #include <sys/dsched.h> #include <sys/devfs.h> #include <sys/file.h> #include <machine/stdarg.h> #include <sys/mplock2.h> /* * system link descriptors identify the command in the * arguments structure. */ #define DDESCNAME(name) __CONCAT(__CONCAT(dev_,name),_desc) #define DEVOP_DESC_INIT(name) \ struct syslink_desc DDESCNAME(name) = { \ __offsetof(struct dev_ops, __CONCAT(d_, name)), \ #name } DEVOP_DESC_INIT(default); DEVOP_DESC_INIT(open); DEVOP_DESC_INIT(close); DEVOP_DESC_INIT(read); DEVOP_DESC_INIT(write); DEVOP_DESC_INIT(ioctl); DEVOP_DESC_INIT(dump); DEVOP_DESC_INIT(psize); DEVOP_DESC_INIT(mmap); DEVOP_DESC_INIT(mmap_single); DEVOP_DESC_INIT(strategy); DEVOP_DESC_INIT(kqfilter); DEVOP_DESC_INIT(revoke); DEVOP_DESC_INIT(clone); /* * Misc default ops */ struct dev_ops dead_dev_ops; static d_open_t noopen; static d_close_t noclose; static d_read_t noread; static d_write_t nowrite; static d_ioctl_t noioctl; static d_mmap_t nommap; static d_mmap_single_t nommap_single; static d_strategy_t nostrategy; static d_dump_t nodump; static d_psize_t nopsize; static d_kqfilter_t nokqfilter; static d_clone_t noclone; static d_revoke_t norevoke; struct dev_ops default_dev_ops = { { "null" }, .d_default = NULL, /* must be NULL */ .d_open = noopen, .d_close = noclose, .d_read = noread, .d_write = nowrite, .d_ioctl = noioctl, .d_mmap = nommap, .d_mmap_single = nommap_single, .d_strategy = nostrategy, .d_dump = nodump, .d_psize = nopsize, .d_kqfilter = nokqfilter, .d_revoke = norevoke, .d_clone = noclone }; static __inline int dev_needmplock(cdev_t dev) { return((dev->si_ops->head.flags & D_MPSAFE) == 0); } static __inline int dev_nokvabio(cdev_t dev) { return((dev->si_ops->head.flags & D_KVABIO) == 0); } /************************************************************************ * GENERAL DEVICE API FUNCTIONS * ************************************************************************ * * The MPSAFEness of these depends on dev->si_ops->head.flags */ int dev_dopen(cdev_t dev, int oflags, int devtype, struct ucred *cred, struct file **fpp, struct vnode *vp) { struct dev_open_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_open_desc; ap.a_head.a_dev = dev; ap.a_oflags = oflags; ap.a_devtype = devtype; ap.a_cred = cred; ap.a_fpp = fpp; if (ap.a_fpp) (*ap.a_fpp)->f_data = vp; /* * vref(vp) is being done in vop_stdopen() * * If a non-null vp is passed-in, the caller must also issue a * vop_stdopen() * * NOTE: d_open() may replace *ap.a_fpp */ if (needmplock) get_mplock(); error = dev->si_ops->d_open(&ap); if (needmplock) rel_mplock(); return (error); } int dev_dclose(cdev_t dev, int fflag, int devtype, struct file *fp) { struct dev_close_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_close_desc; ap.a_head.a_dev = dev; ap.a_fflag = fflag; ap.a_devtype = devtype; ap.a_fp = fp; if (needmplock) get_mplock(); error = dev->si_ops->d_close(&ap); if (needmplock) rel_mplock(); return (error); } int dev_dread(cdev_t dev, struct uio *uio, int ioflag, struct file *fp) { struct dev_read_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_read_desc; ap.a_head.a_dev = dev; ap.a_uio = uio; ap.a_ioflag = ioflag; ap.a_fp = fp; if (needmplock) get_mplock(); error = dev->si_ops->d_read(&ap); if (needmplock) rel_mplock(); if (error == 0) dev->si_lastread = time_uptime; return (error); } int dev_dwrite(cdev_t dev, struct uio *uio, int ioflag, struct file *fp) { struct dev_write_args ap; int needmplock = dev_needmplock(dev); int error; dev->si_lastwrite = time_uptime; ap.a_head.a_desc = &dev_write_desc; ap.a_head.a_dev = dev; ap.a_uio = uio; ap.a_ioflag = ioflag; ap.a_fp = fp; if (needmplock) get_mplock(); error = dev->si_ops->d_write(&ap); if (needmplock) rel_mplock(); return (error); } int dev_dioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag, struct ucred *cred, struct sysmsg *msg, struct file *fp) { struct dev_ioctl_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_ioctl_desc; ap.a_head.a_dev = dev; ap.a_cmd = cmd; ap.a_data = data; ap.a_fflag = fflag; ap.a_cred = cred; ap.a_sysmsg = msg; ap.a_fp = fp; if (needmplock) get_mplock(); error = dev->si_ops->d_ioctl(&ap); if (needmplock) rel_mplock(); return (error); } int64_t dev_dmmap(cdev_t dev, vm_offset_t offset, int nprot, struct file *fp) { struct dev_mmap_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_mmap_desc; ap.a_head.a_dev = dev; ap.a_offset = offset; ap.a_nprot = nprot; ap.a_fp = fp; if (needmplock) get_mplock(); error = dev->si_ops->d_mmap(&ap); if (needmplock) rel_mplock(); if (error == 0) return(ap.a_result); return(-1); } int dev_dmmap_single(cdev_t dev, vm_ooffset_t *offset, vm_size_t size, struct vm_object **object, int nprot, struct file *fp) { struct dev_mmap_single_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_mmap_single_desc; ap.a_head.a_dev = dev; ap.a_offset = offset; ap.a_size = size; ap.a_object = object; ap.a_nprot = nprot; ap.a_fp = fp; if (needmplock) get_mplock(); error = dev->si_ops->d_mmap_single(&ap); if (needmplock) rel_mplock(); return(error); } int dev_dclone(cdev_t dev) { struct dev_clone_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_clone_desc; ap.a_head.a_dev = dev; if (needmplock) get_mplock(); error = dev->si_ops->d_clone(&ap); if (needmplock) rel_mplock(); return (error); } int dev_drevoke(cdev_t dev) { struct dev_revoke_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_revoke_desc; ap.a_head.a_dev = dev; if (needmplock) get_mplock(); error = dev->si_ops->d_revoke(&ap); if (needmplock) rel_mplock(); return (error); } /* * Core device strategy call, used to issue I/O on a device. There are * two versions, a non-chained version and a chained version. The chained * version reuses a BIO set up by vn_strategy(). The only difference is * that, for now, we do not push a new tracking structure when chaining * from vn_strategy. XXX this will ultimately have to change. */ void dev_dstrategy(cdev_t dev, struct bio *bio) { struct dev_strategy_args ap; struct bio_track *track; struct buf *bp = bio->bio_buf; int needmplock = dev_needmplock(dev); /* * If the device doe snot support KVABIO and the buffer is using * KVABIO, we must synchronize b_data to all cpus before dispatching. */ if (dev_nokvabio(dev) && (bp->b_flags & B_KVABIO)) bkvasync_all(bp); ap.a_head.a_desc = &dev_strategy_desc; ap.a_head.a_dev = dev; ap.a_bio = bio; KKASSERT(bio->bio_track == NULL); KKASSERT(bp->b_cmd != BUF_CMD_DONE); if (bp->b_cmd == BUF_CMD_READ) track = &dev->si_track_read; else track = &dev->si_track_write; bio_track_ref(track); bio->bio_track = track; dsched_buf_enter(bp); /* might stack */ KKASSERT((bio->bio_flags & BIO_DONE) == 0); if (needmplock) get_mplock(); (void)dev->si_ops->d_strategy(&ap); if (needmplock) rel_mplock(); } void dev_dstrategy_chain(cdev_t dev, struct bio *bio) { struct dev_strategy_args ap; struct buf *bp = bio->bio_buf; int needmplock = dev_needmplock(dev); /* * If the device doe snot support KVABIO and the buffer is using * KVABIO, we must synchronize b_data to all cpus before dispatching. */ if (dev_nokvabio(dev) && (bp->b_flags & B_KVABIO)) bkvasync_all(bp); ap.a_head.a_desc = &dev_strategy_desc; ap.a_head.a_dev = dev; ap.a_bio = bio; KKASSERT(bio->bio_track != NULL); KKASSERT((bio->bio_flags & BIO_DONE) == 0); if (needmplock) get_mplock(); (void)dev->si_ops->d_strategy(&ap); if (needmplock) rel_mplock(); } /* * note: the disk layer is expected to set count, blkno, and secsize before * forwarding the message. */ int dev_ddump(cdev_t dev, void *virtual, vm_offset_t physical, off_t offset, size_t length) { struct dev_dump_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_dump_desc; ap.a_head.a_dev = dev; ap.a_count = 0; ap.a_blkno = 0; ap.a_secsize = 0; ap.a_virtual = virtual; ap.a_physical = physical; ap.a_offset = offset; ap.a_length = length; if (needmplock) get_mplock(); error = dev->si_ops->d_dump(&ap); if (needmplock) rel_mplock(); return (error); } int64_t dev_dpsize(cdev_t dev) { struct dev_psize_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_psize_desc; ap.a_head.a_dev = dev; if (needmplock) get_mplock(); error = dev->si_ops->d_psize(&ap); if (needmplock) rel_mplock(); if (error == 0) return (ap.a_result); return(-1); } /* * Pass-thru to the device kqfilter. * * NOTE: We explicitly preset a_result to 0 so d_kqfilter() functions * which return 0 do not have to bother setting a_result. */ int dev_dkqfilter(cdev_t dev, struct knote *kn, struct file *fp) { struct dev_kqfilter_args ap; int needmplock = dev_needmplock(dev); int error; ap.a_head.a_desc = &dev_kqfilter_desc; ap.a_head.a_dev = dev; ap.a_kn = kn; ap.a_result = 0; ap.a_fp = fp; if (needmplock) get_mplock(); error = dev->si_ops->d_kqfilter(&ap); if (needmplock) rel_mplock(); if (error == 0) return(ap.a_result); return(ENODEV); } /************************************************************************ * DEVICE HELPER FUNCTIONS * ************************************************************************/ /* * MPSAFE */ int dev_drefs(cdev_t dev) { return(dev->si_sysref.refcnt); } /* * MPSAFE */ const char * dev_dname(cdev_t dev) { return(dev->si_ops->head.name); } /* * MPSAFE */ int dev_dflags(cdev_t dev) { return(dev->si_ops->head.flags); } /* * MPSAFE */ int dev_dmaj(cdev_t dev) { return(dev->si_ops->head.maj); } /* * Used when forwarding a request through layers. The caller adjusts * ap->a_head.a_dev and then calls this function. */ int dev_doperate(struct dev_generic_args *ap) { int (*func)(struct dev_generic_args *); int needmplock = dev_needmplock(ap->a_dev); int error; func = *(void **)((char *)ap->a_dev->si_ops + ap->a_desc->sd_offset); if (needmplock) get_mplock(); error = func(ap); if (needmplock) rel_mplock(); return (error); } /* * Used by the console intercept code only. Issue an operation through * a foreign ops structure allowing the ops structure associated * with the device to remain intact. */ int dev_doperate_ops(struct dev_ops *ops, struct dev_generic_args *ap) { int (*func)(struct dev_generic_args *); int needmplock = ((ops->head.flags & D_MPSAFE) == 0); int error; func = *(void **)((char *)ops + ap->a_desc->sd_offset); if (needmplock) get_mplock(); error = func(ap); if (needmplock) rel_mplock(); return (error); } /* * Convert a template dev_ops into the real thing by filling in * uninitialized fields. */ void compile_dev_ops(struct dev_ops *ops) { int offset; for (offset = offsetof(struct dev_ops, dev_ops_first_field); offset <= offsetof(struct dev_ops, dev_ops_last_field); offset += sizeof(void *) ) { void **func_p = (void **)((char *)ops + offset); void **def_p = (void **)((char *)&default_dev_ops + offset); if (*func_p == NULL) { if (ops->d_default) *func_p = ops->d_default; else *func_p = *def_p; } } } /************************************************************************ * MAJOR/MINOR SPACE FUNCTION * ************************************************************************/ /* * This makes a dev_ops entry visible to userland (e.g /dev/<blah>). * * Disk devices typically register their major, e.g. 'ad0', and then call * into the disk label management code which overloads its own onto e.g. 'ad0' * to support all the various slice and partition combinations. * * The mask/match supplied in this call are a full 32 bits and the same * mask and match must be specified in a later dev_ops_remove() call to * match this add. However, the match value for the minor number should never * have any bits set in the major number's bit range (8-15). The mask value * may be conveniently specified as -1 without creating any major number * interference. */ static int rb_dev_ops_compare(struct dev_ops_maj *a, struct dev_ops_maj *b) { if (a->maj < b->maj) return(-1); else if (a->maj > b->maj) return(1); return(0); } RB_GENERATE2(dev_ops_rb_tree, dev_ops_maj, rbnode, rb_dev_ops_compare, int, maj); struct dev_ops_rb_tree dev_ops_rbhead = RB_INITIALIZER(dev_ops_rbhead); int dev_ops_remove_all(struct dev_ops *ops) { return devfs_destroy_dev_by_ops(ops, -1); } int dev_ops_remove_minor(struct dev_ops *ops, int minor) { return devfs_destroy_dev_by_ops(ops, minor); } struct dev_ops * dev_ops_intercept(cdev_t dev, struct dev_ops *iops) { struct dev_ops *oops = dev->si_ops; compile_dev_ops(iops); iops->head.maj = oops->head.maj; iops->head.data = oops->head.data; iops->head.flags = oops->head.flags; dev->si_ops = iops; dev->si_flags |= SI_INTERCEPTED; return (oops); } void dev_ops_restore(cdev_t dev, struct dev_ops *oops) { struct dev_ops *iops = dev->si_ops; dev->si_ops = oops; dev->si_flags &= ~SI_INTERCEPTED; iops->head.maj = 0; iops->head.data = NULL; iops->head.flags = 0; } /************************************************************************ * DEFAULT DEV OPS FUNCTIONS * ************************************************************************/ /* * Unsupported devswitch functions (e.g. for writing to read-only device). * XXX may belong elsewhere. */ static int norevoke(struct dev_revoke_args *ap) { /* take no action */ return(0); } static int noclone(struct dev_clone_args *ap) { /* take no action */ return (0); /* allow the clone */ } static int noopen(struct dev_open_args *ap) { return (ENODEV); } static int noclose(struct dev_close_args *ap) { return (ENODEV); } static int noread(struct dev_read_args *ap) { return (ENODEV); } static int nowrite(struct dev_write_args *ap) { return (ENODEV); } static int noioctl(struct dev_ioctl_args *ap) { return (ENODEV); } static int nokqfilter(struct dev_kqfilter_args *ap) { return (ENODEV); } static int nommap(struct dev_mmap_args *ap) { return (ENODEV); } static int nommap_single(struct dev_mmap_single_args *ap) { return (ENODEV); } static int nostrategy(struct dev_strategy_args *ap) { struct bio *bio = ap->a_bio; bio->bio_buf->b_flags |= B_ERROR; bio->bio_buf->b_error = EOPNOTSUPP; biodone(bio); return(0); } static int nopsize(struct dev_psize_args *ap) { ap->a_result = 0; return(0); } static int nodump(struct dev_dump_args *ap) { return (ENODEV); } /* * XXX this is probably bogus. Any device that uses it isn't checking the * minor number. */ int nullopen(struct dev_open_args *ap) { return (0); } int nullclose(struct dev_close_args *ap) { return (0); } |