1 /* 2 * Copyright (c) 2003,2004,2009 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * and Alex Hornung <ahornung@gmail.com> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * ---------------------------------------------------------------------------- 36 * "THE BEER-WARE LICENSE" (Revision 42): 37 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 38 * can do whatever you want with this stuff. If we meet some day, and you think 39 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 40 * ---------------------------------------------------------------------------- 41 * 42 * Copyright (c) 1982, 1986, 1988, 1993 43 * The Regents of the University of California. All rights reserved. 44 * (c) UNIX System Laboratories, Inc. 45 * All or some portions of this file are derived from material licensed 46 * to the University of California by American Telephone and Telegraph 47 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 48 * the permission of UNIX System Laboratories, Inc. 49 * 50 * Redistribution and use in source and binary forms, with or without 51 * modification, are permitted provided that the following conditions 52 * are met: 53 * 1. Redistributions of source code must retain the above copyright 54 * notice, this list of conditions and the following disclaimer. 55 * 2. Redistributions in binary form must reproduce the above copyright 56 * notice, this list of conditions and the following disclaimer in the 57 * documentation and/or other materials provided with the distribution. 58 * 3. Neither the name of the University nor the names of its contributors 59 * may be used to endorse or promote products derived from this software 60 * without specific prior written permission. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 * 74 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94 75 * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $ 76 * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $ 77 */ 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/kernel.h> 82 #include <sys/proc.h> 83 #include <sys/sysctl.h> 84 #include <sys/buf.h> 85 #include <sys/conf.h> 86 #include <sys/disklabel.h> 87 #include <sys/disklabel32.h> 88 #include <sys/disklabel64.h> 89 #include <sys/diskslice.h> 90 #include <sys/diskmbr.h> 91 #include <sys/disk.h> 92 #include <sys/kerneldump.h> 93 #include <sys/malloc.h> 94 #include <machine/md_var.h> 95 #include <sys/ctype.h> 96 #include <sys/syslog.h> 97 #include <sys/device.h> 98 #include <sys/msgport.h> 99 #include <sys/devfs.h> 100 #include <sys/thread.h> 101 #include <sys/dsched.h> 102 #include <sys/queue.h> 103 #include <sys/lock.h> 104 #include <sys/udev.h> 105 #include <sys/uuid.h> 106 107 #include <sys/buf2.h> 108 #include <sys/msgport2.h> 109 #include <sys/thread2.h> 110 111 static MALLOC_DEFINE(M_DISK, "disk", "disk data"); 112 static int disk_debug_enable = 0; 113 114 static void disk_msg_autofree_reply(lwkt_port_t, lwkt_msg_t); 115 static void disk_msg_core(void *); 116 static int disk_probe_slice(struct disk *dp, cdev_t dev, int slice, int reprobe); 117 static void disk_probe(struct disk *dp, int reprobe); 118 static void _setdiskinfo(struct disk *disk, struct disk_info *info); 119 static void bioqwritereorder(struct bio_queue_head *bioq); 120 static void disk_cleanserial(char *serno); 121 static int disk_debug(int, char *, ...) __printflike(2, 3); 122 static cdev_t _disk_create_named(const char *name, int unit, struct disk *dp, 123 struct dev_ops *raw_ops, int clone); 124 125 static d_open_t diskopen; 126 static d_close_t diskclose; 127 static d_ioctl_t diskioctl; 128 static d_strategy_t diskstrategy; 129 static d_psize_t diskpsize; 130 static d_dump_t diskdump; 131 132 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist); 133 static struct lwkt_token disklist_token; 134 static struct lwkt_token ds_token; 135 136 static struct dev_ops disk1_ops = { 137 { "disk", 0, D_DISK | D_MPSAFE | D_TRACKCLOSE | D_KVABIO }, 138 .d_open = diskopen, 139 .d_close = diskclose, 140 .d_read = physread, 141 .d_write = physwrite, 142 .d_ioctl = diskioctl, 143 .d_strategy = diskstrategy, 144 .d_dump = diskdump, 145 .d_psize = diskpsize, 146 }; 147 148 static struct dev_ops disk2_ops = { 149 { "disk", 0, D_DISK | D_MPSAFE | D_TRACKCLOSE | D_KVABIO | 150 D_NOEMERGPGR }, 151 .d_open = diskopen, 152 .d_close = diskclose, 153 .d_read = physread, 154 .d_write = physwrite, 155 .d_ioctl = diskioctl, 156 .d_strategy = diskstrategy, 157 .d_dump = diskdump, 158 .d_psize = diskpsize, 159 }; 160 161 static struct objcache *disk_msg_cache; 162 163 struct objcache_malloc_args disk_msg_malloc_args = { 164 sizeof(struct disk_msg), M_DISK }; 165 166 static struct lwkt_port disk_dispose_port; 167 static struct lwkt_port disk_msg_port; 168 169 static int 170 disk_debug(int level, char *fmt, ...) 171 { 172 __va_list ap; 173 174 __va_start(ap, fmt); 175 if (level <= disk_debug_enable) 176 kvprintf(fmt, ap); 177 __va_end(ap); 178 179 return 0; 180 } 181 182 static int 183 disk_probe_slice(struct disk *dp, cdev_t dev, int slice, int reprobe) 184 { 185 struct disk_info *info = &dp->d_info; 186 struct diskslice *sp = &dp->d_slice->dss_slices[slice]; 187 disklabel_ops_t ops; 188 struct dev_ops *dops; 189 struct partinfo part; 190 const char *msg; 191 char uuid_buf[128]; 192 cdev_t ndev; 193 int sno; 194 u_int i; 195 196 disk_debug(2, "disk_probe_slice (begin): %s (%s)\n", 197 dev->si_name, dp->d_cdev->si_name); 198 199 sno = slice ? slice - 1 : 0; 200 dops = (dp->d_rawdev->si_ops->head.flags & D_NOEMERGPGR) ? 201 &disk2_ops : &disk1_ops; 202 203 ops = &disklabel32_ops; 204 msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info); 205 if (msg && !strcmp(msg, "no disk label")) { 206 ops = &disklabel64_ops; 207 msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info); 208 } 209 210 if (msg == NULL) { 211 if (slice != WHOLE_DISK_SLICE) 212 ops->op_adjust_label_reserved(dp->d_slice, slice, sp); 213 else 214 sp->ds_reserved = 0; 215 216 sp->ds_ops = ops; 217 for (i = 0; i < ops->op_getnumparts(sp->ds_label); i++) { 218 ops->op_loadpartinfo(sp->ds_label, i, &part); 219 if (part.fstype) { 220 if (reprobe && 221 (ndev = devfs_find_device_by_name("%s%c", 222 dev->si_name, 'a' + i)) 223 ) { 224 /* 225 * Device already exists and 226 * is still valid. 227 */ 228 ndev->si_flags |= SI_REPROBE_TEST; 229 230 /* 231 * Destroy old UUID alias 232 */ 233 destroy_dev_alias(ndev, "part-by-uuid/*"); 234 235 /* Create UUID alias */ 236 if (!kuuid_is_nil(&part.storage_uuid)) { 237 snprintf_uuid(uuid_buf, 238 sizeof(uuid_buf), 239 &part.storage_uuid); 240 make_dev_alias(ndev, 241 "part-by-uuid/%s", 242 uuid_buf); 243 udev_dict_set_cstr(ndev, "uuid", uuid_buf); 244 } 245 } else { 246 ndev = make_dev_covering(dops, 247 dp->d_rawdev->si_ops, 248 dkmakeminor(dkunit(dp->d_cdev), 249 slice, i), 250 UID_ROOT, GID_OPERATOR, 0640, 251 "%s%c", dev->si_name, 'a'+ i); 252 ndev->si_parent = dev; 253 ndev->si_iosize_max = dev->si_iosize_max; 254 ndev->si_disk = dp; 255 udev_dict_set_cstr(ndev, "subsystem", "disk"); 256 /* Inherit parent's disk type */ 257 if (dp->d_disktype) { 258 udev_dict_set_cstr(ndev, "disk-type", 259 __DECONST(char *, dp->d_disktype)); 260 } 261 262 /* Create serno alias */ 263 if (dp->d_info.d_serialno) { 264 make_dev_alias(ndev, 265 "serno/%s.s%d%c", 266 dp->d_info.d_serialno, 267 sno, 'a' + i); 268 } 269 270 /* Create UUID alias */ 271 if (!kuuid_is_nil(&part.storage_uuid)) { 272 snprintf_uuid(uuid_buf, 273 sizeof(uuid_buf), 274 &part.storage_uuid); 275 make_dev_alias(ndev, 276 "part-by-uuid/%s", 277 uuid_buf); 278 udev_dict_set_cstr(ndev, "uuid", uuid_buf); 279 } 280 ndev->si_flags |= SI_REPROBE_TEST; 281 } 282 } 283 } 284 } else if (info->d_dsflags & DSO_COMPATLABEL) { 285 msg = NULL; 286 if (sp->ds_size >= 0x100000000ULL) 287 ops = &disklabel64_ops; 288 else 289 ops = &disklabel32_ops; 290 sp->ds_label = ops->op_clone_label(info, sp); 291 } else { 292 if (sp->ds_type == DOSPTYP_386BSD || /* XXX */ 293 sp->ds_type == DOSPTYP_NETBSD || 294 sp->ds_type == DOSPTYP_OPENBSD || 295 sp->ds_type == DOSPTYP_DFLYBSD) { 296 log(LOG_WARNING, "%s: cannot find label (%s)\n", 297 dev->si_name, msg); 298 } 299 300 if (sp->ds_label.opaque != NULL && sp->ds_ops != NULL) { 301 /* Clear out old label - it's not around anymore */ 302 disk_debug(2, 303 "disk_probe_slice: clear out old diskabel on %s\n", 304 dev->si_name); 305 306 sp->ds_ops->op_freedisklabel(&sp->ds_label); 307 sp->ds_ops = NULL; 308 } 309 } 310 311 if (msg == NULL) { 312 sp->ds_wlabel = FALSE; 313 } 314 315 return (msg ? EINVAL : 0); 316 } 317 318 /* 319 * This routine is only called for newly minted drives or to reprobe 320 * a drive with no open slices. disk_probe_slice() is called directly 321 * when reprobing partition changes within slices. 322 */ 323 static void 324 disk_probe(struct disk *dp, int reprobe) 325 { 326 struct disk_info *info = &dp->d_info; 327 cdev_t dev = dp->d_cdev; 328 cdev_t ndev; 329 int error, i, sno; 330 struct diskslices *osp; 331 struct diskslice *sp; 332 struct dev_ops *dops; 333 char uuid_buf[128]; 334 335 KKASSERT (info->d_media_blksize != 0); 336 337 osp = dp->d_slice; 338 dp->d_slice = dsmakeslicestruct(BASE_SLICE, info); 339 disk_debug(1, "disk_probe (begin): %s\n", dp->d_cdev->si_name); 340 341 error = mbrinit(dev, info, &(dp->d_slice)); 342 if (error) { 343 dsgone(&osp); 344 return; 345 } 346 347 dops = (dp->d_rawdev->si_ops->head.flags & D_NOEMERGPGR) ? 348 &disk2_ops : &disk1_ops; 349 350 for (i = 0; i < dp->d_slice->dss_nslices; i++) { 351 /* 352 * Ignore the whole-disk slice, it has already been created. 353 */ 354 if (i == WHOLE_DISK_SLICE) 355 continue; 356 357 #if 1 358 /* 359 * Ignore the compatibility slice s0 if it's a device mapper 360 * volume. 361 */ 362 if ((i == COMPATIBILITY_SLICE) && 363 (info->d_dsflags & DSO_DEVICEMAPPER)) 364 continue; 365 #endif 366 367 sp = &dp->d_slice->dss_slices[i]; 368 369 /* 370 * Handle s0. s0 is a compatibility slice if there are no 371 * other slices and it has not otherwise been set up, else 372 * we ignore it. 373 */ 374 if (i == COMPATIBILITY_SLICE) { 375 sno = 0; 376 if (sp->ds_type == 0 && 377 dp->d_slice->dss_nslices == BASE_SLICE) { 378 sp->ds_size = info->d_media_blocks; 379 sp->ds_reserved = 0; 380 } 381 } else { 382 sno = i - 1; 383 sp->ds_reserved = 0; 384 } 385 386 /* 387 * Ignore 0-length slices 388 */ 389 if (sp->ds_size == 0) 390 continue; 391 392 if (reprobe && 393 (ndev = devfs_find_device_by_name("%ss%d", 394 dev->si_name, sno))) { 395 /* 396 * Device already exists and is still valid 397 */ 398 ndev->si_flags |= SI_REPROBE_TEST; 399 400 /* 401 * Destroy old UUID alias 402 */ 403 destroy_dev_alias(ndev, "slice-by-uuid/*"); 404 405 /* Create UUID alias */ 406 if (!kuuid_is_nil(&sp->ds_stor_uuid)) { 407 snprintf_uuid(uuid_buf, sizeof(uuid_buf), 408 &sp->ds_stor_uuid); 409 make_dev_alias(ndev, "slice-by-uuid/%s", 410 uuid_buf); 411 } 412 } else { 413 /* 414 * Else create new device 415 */ 416 ndev = make_dev_covering(dops, dp->d_rawdev->si_ops, 417 dkmakewholeslice(dkunit(dev), i), 418 UID_ROOT, GID_OPERATOR, 0640, 419 (info->d_dsflags & DSO_DEVICEMAPPER)? 420 "%s.s%d" : "%ss%d", dev->si_name, sno); 421 ndev->si_parent = dev; 422 ndev->si_iosize_max = dev->si_iosize_max; 423 udev_dict_set_cstr(ndev, "subsystem", "disk"); 424 /* Inherit parent's disk type */ 425 if (dp->d_disktype) { 426 udev_dict_set_cstr(ndev, "disk-type", 427 __DECONST(char *, dp->d_disktype)); 428 } 429 430 /* Create serno alias */ 431 if (dp->d_info.d_serialno) { 432 make_dev_alias(ndev, "serno/%s.s%d", 433 dp->d_info.d_serialno, sno); 434 } 435 436 /* Create UUID alias */ 437 if (!kuuid_is_nil(&sp->ds_stor_uuid)) { 438 snprintf_uuid(uuid_buf, sizeof(uuid_buf), 439 &sp->ds_stor_uuid); 440 make_dev_alias(ndev, "slice-by-uuid/%s", 441 uuid_buf); 442 } 443 444 ndev->si_disk = dp; 445 ndev->si_flags |= SI_REPROBE_TEST; 446 } 447 sp->ds_dev = ndev; 448 449 /* 450 * Probe appropriate slices for a disklabel 451 * 452 * XXX slice type 1 used by our gpt probe code. 453 * XXX slice type 0 used by mbr compat slice. 454 */ 455 if (sp->ds_type == DOSPTYP_386BSD || 456 sp->ds_type == DOSPTYP_NETBSD || 457 sp->ds_type == DOSPTYP_OPENBSD || 458 sp->ds_type == DOSPTYP_DFLYBSD || 459 sp->ds_type == 0 || 460 sp->ds_type == 1) { 461 if (dp->d_slice->dss_first_bsd_slice == 0) 462 dp->d_slice->dss_first_bsd_slice = i; 463 disk_probe_slice(dp, ndev, i, reprobe); 464 } 465 } 466 dsgone(&osp); 467 disk_debug(1, "disk_probe (end): %s\n", dp->d_cdev->si_name); 468 } 469 470 471 static void 472 disk_msg_core(void *arg) 473 { 474 struct disk *dp; 475 struct diskslice *sp; 476 disk_msg_t msg; 477 int run; 478 479 lwkt_gettoken(&disklist_token); 480 lwkt_initport_thread(&disk_msg_port, curthread); 481 wakeup(curthread); /* synchronous startup */ 482 lwkt_reltoken(&disklist_token); 483 484 lwkt_gettoken(&ds_token); 485 run = 1; 486 487 while (run) { 488 msg = (disk_msg_t)lwkt_waitport(&disk_msg_port, 0); 489 490 switch (msg->hdr.u.ms_result) { 491 case DISK_DISK_PROBE: 492 dp = (struct disk *)msg->load; 493 disk_debug(1, 494 "DISK_DISK_PROBE: %s\n", 495 dp->d_cdev->si_name); 496 disk_iocom_update(dp); 497 disk_probe(dp, 0); 498 break; 499 case DISK_DISK_DESTROY: 500 dp = (struct disk *)msg->load; 501 disk_debug(1, 502 "DISK_DISK_DESTROY: %s\n", 503 dp->d_cdev->si_name); 504 disk_iocom_uninit(dp); 505 506 /* 507 * Interlock against struct disk enumerations. 508 * Wait for enumerations to complete then remove 509 * the dp from the list before tearing it down. 510 * This avoids numerous races. 511 */ 512 lwkt_gettoken(&disklist_token); 513 while (dp->d_refs) 514 tsleep(&dp->d_refs, 0, "diskdel", hz / 10); 515 LIST_REMOVE(dp, d_list); 516 517 dsched_disk_destroy(dp); 518 devfs_destroy_related(dp->d_cdev); 519 destroy_dev(dp->d_cdev); 520 destroy_only_dev(dp->d_rawdev); 521 522 lwkt_reltoken(&disklist_token); 523 524 if (dp->d_info.d_serialno) { 525 kfree(dp->d_info.d_serialno, M_TEMP); 526 dp->d_info.d_serialno = NULL; 527 } 528 break; 529 case DISK_UNPROBE: 530 dp = (struct disk *)msg->load; 531 disk_debug(1, 532 "DISK_DISK_UNPROBE: %s\n", 533 dp->d_cdev->si_name); 534 devfs_destroy_related(dp->d_cdev); 535 break; 536 case DISK_SLICE_REPROBE: 537 dp = (struct disk *)msg->load; 538 sp = (struct diskslice *)msg->load2; 539 devfs_clr_related_flag(sp->ds_dev, 540 SI_REPROBE_TEST); 541 disk_debug(1, 542 "DISK_SLICE_REPROBE: %s\n", 543 sp->ds_dev->si_name); 544 disk_probe_slice(dp, sp->ds_dev, 545 dkslice(sp->ds_dev), 1); 546 devfs_destroy_related_without_flag( 547 sp->ds_dev, SI_REPROBE_TEST); 548 break; 549 case DISK_DISK_REPROBE: 550 dp = (struct disk *)msg->load; 551 devfs_clr_related_flag(dp->d_cdev, SI_REPROBE_TEST); 552 disk_debug(1, 553 "DISK_DISK_REPROBE: %s\n", 554 dp->d_cdev->si_name); 555 disk_probe(dp, 1); 556 devfs_destroy_related_without_flag( 557 dp->d_cdev, SI_REPROBE_TEST); 558 break; 559 case DISK_SYNC: 560 disk_debug(1, "DISK_SYNC\n"); 561 break; 562 default: 563 devfs_debug(DEVFS_DEBUG_WARNING, 564 "disk_msg_core: unknown message " 565 "received at core\n"); 566 break; 567 } 568 lwkt_replymsg(&msg->hdr, 0); 569 } 570 lwkt_reltoken(&ds_token); 571 lwkt_exit(); 572 } 573 574 575 /* 576 * Acts as a message drain. Any message that is replied to here gets 577 * destroyed and the memory freed. 578 */ 579 static void 580 disk_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg) 581 { 582 objcache_put(disk_msg_cache, msg); 583 } 584 585 586 void 587 disk_msg_send(uint32_t cmd, void *load, void *load2) 588 { 589 disk_msg_t disk_msg; 590 lwkt_port_t port = &disk_msg_port; 591 592 disk_msg = objcache_get(disk_msg_cache, M_WAITOK); 593 594 lwkt_initmsg(&disk_msg->hdr, &disk_dispose_port, 0); 595 596 disk_msg->hdr.u.ms_result = cmd; 597 disk_msg->load = load; 598 disk_msg->load2 = load2; 599 KKASSERT(port); 600 lwkt_sendmsg(port, &disk_msg->hdr); 601 } 602 603 void 604 disk_msg_send_sync(uint32_t cmd, void *load, void *load2) 605 { 606 struct lwkt_port rep_port; 607 disk_msg_t disk_msg; 608 lwkt_port_t port; 609 610 disk_msg = objcache_get(disk_msg_cache, M_WAITOK); 611 port = &disk_msg_port; 612 613 /* XXX could probably use curthread's built-in msgport */ 614 lwkt_initport_thread(&rep_port, curthread); 615 lwkt_initmsg(&disk_msg->hdr, &rep_port, 0); 616 617 disk_msg->hdr.u.ms_result = cmd; 618 disk_msg->load = load; 619 disk_msg->load2 = load2; 620 621 lwkt_domsg(port, &disk_msg->hdr, 0); 622 objcache_put(disk_msg_cache, disk_msg); 623 } 624 625 /* 626 * Create a raw device for the dev_ops template (which is returned). Also 627 * create a slice and unit managed disk and overload the user visible 628 * device space with it. 629 * 630 * NOTE: The returned raw device is NOT a slice and unit managed device. 631 * It is an actual raw device representing the raw disk as specified by 632 * the passed dev_ops. The disk layer not only returns such a raw device, 633 * it also uses it internally when passing (modified) commands through. 634 */ 635 cdev_t 636 disk_create(int unit, struct disk *dp, struct dev_ops *raw_ops) 637 { 638 return _disk_create_named(NULL, unit, dp, raw_ops, 0); 639 } 640 641 cdev_t 642 disk_create_clone(int unit, struct disk *dp, 643 struct dev_ops *raw_ops) 644 { 645 return _disk_create_named(NULL, unit, dp, raw_ops, 1); 646 } 647 648 cdev_t 649 disk_create_named(const char *name, int unit, struct disk *dp, 650 struct dev_ops *raw_ops) 651 { 652 return _disk_create_named(name, unit, dp, raw_ops, 0); 653 } 654 655 cdev_t 656 disk_create_named_clone(const char *name, int unit, struct disk *dp, 657 struct dev_ops *raw_ops) 658 { 659 return _disk_create_named(name, unit, dp, raw_ops, 1); 660 } 661 662 static cdev_t 663 _disk_create_named(const char *name, int unit, struct disk *dp, 664 struct dev_ops *raw_ops, int clone) 665 { 666 cdev_t rawdev; 667 struct dev_ops *dops; 668 669 disk_debug(1, "disk_create (begin): %s%d\n", name, unit); 670 671 if (name) { 672 rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit), 673 UID_ROOT, GID_OPERATOR, 0640, "%s", name); 674 } else { 675 rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit), 676 UID_ROOT, GID_OPERATOR, 0640, 677 "%s%d", raw_ops->head.name, unit); 678 } 679 680 bzero(dp, sizeof(*dp)); 681 682 dops = (raw_ops->head.flags & D_NOEMERGPGR) ? &disk2_ops : &disk1_ops; 683 684 dp->d_rawdev = rawdev; 685 dp->d_raw_ops = raw_ops; 686 dp->d_dev_ops = dops; 687 688 if (name) { 689 if (clone) { 690 dp->d_cdev = make_only_dev_covering( 691 dops, dp->d_rawdev->si_ops, 692 dkmakewholedisk(unit), 693 UID_ROOT, GID_OPERATOR, 0640, 694 "%s", name); 695 } else { 696 dp->d_cdev = make_dev_covering( 697 dops, dp->d_rawdev->si_ops, 698 dkmakewholedisk(unit), 699 UID_ROOT, GID_OPERATOR, 0640, 700 "%s", name); 701 } 702 } else { 703 if (clone) { 704 dp->d_cdev = make_only_dev_covering( 705 dops, dp->d_rawdev->si_ops, 706 dkmakewholedisk(unit), 707 UID_ROOT, GID_OPERATOR, 0640, 708 "%s%d", raw_ops->head.name, unit); 709 } else { 710 dp->d_cdev = make_dev_covering( 711 dops, dp->d_rawdev->si_ops, 712 dkmakewholedisk(unit), 713 UID_ROOT, GID_OPERATOR, 0640, 714 "%s%d", raw_ops->head.name, unit); 715 } 716 } 717 718 udev_dict_set_cstr(dp->d_cdev, "subsystem", "disk"); 719 dp->d_cdev->si_disk = dp; 720 721 if (name) 722 dsched_disk_create(dp, name, unit); 723 else 724 dsched_disk_create(dp, raw_ops->head.name, unit); 725 726 lwkt_gettoken(&disklist_token); 727 LIST_INSERT_HEAD(&disklist, dp, d_list); 728 lwkt_reltoken(&disklist_token); 729 730 disk_iocom_init(dp); 731 732 disk_debug(1, "disk_create (end): %s%d\n", 733 (name != NULL)?(name):(raw_ops->head.name), unit); 734 735 return (dp->d_rawdev); 736 } 737 738 int 739 disk_setdisktype(struct disk *disk, const char *type) 740 { 741 int error; 742 743 KKASSERT(disk != NULL); 744 745 disk->d_disktype = type; 746 error = udev_dict_set_cstr(disk->d_cdev, "disk-type", 747 __DECONST(char *, type)); 748 return error; 749 } 750 751 int 752 disk_getopencount(struct disk *disk) 753 { 754 return disk->d_opencount; 755 } 756 757 static void 758 _setdiskinfo(struct disk *disk, struct disk_info *info) 759 { 760 char *oldserialno; 761 762 oldserialno = disk->d_info.d_serialno; 763 bcopy(info, &disk->d_info, sizeof(disk->d_info)); 764 info = &disk->d_info; 765 766 disk_debug(1, "_setdiskinfo: %s\n", disk->d_cdev->si_name); 767 768 /* 769 * The serial number is duplicated so the caller can throw 770 * their copy away. 771 */ 772 if (info->d_serialno && info->d_serialno[0] && 773 (info->d_serialno[0] != ' ' || strlen(info->d_serialno) > 1)) { 774 info->d_serialno = kstrdup(info->d_serialno, M_TEMP); 775 disk_cleanserial(info->d_serialno); 776 if (disk->d_cdev) { 777 make_dev_alias(disk->d_cdev, "serno/%s", 778 info->d_serialno); 779 } 780 } else { 781 info->d_serialno = NULL; 782 } 783 if (oldserialno) 784 kfree(oldserialno, M_TEMP); 785 786 dsched_disk_update(disk, info); 787 788 /* 789 * The caller may set d_media_size or d_media_blocks and we 790 * calculate the other. 791 */ 792 KKASSERT(info->d_media_size == 0 || info->d_media_blocks == 0); 793 if (info->d_media_size == 0 && info->d_media_blocks) { 794 info->d_media_size = (u_int64_t)info->d_media_blocks * 795 info->d_media_blksize; 796 } else if (info->d_media_size && info->d_media_blocks == 0 && 797 info->d_media_blksize) { 798 info->d_media_blocks = info->d_media_size / 799 info->d_media_blksize; 800 } 801 802 /* 803 * The si_* fields for rawdev are not set until after the 804 * disk_create() call, so someone using the cooked version 805 * of the raw device (i.e. da0s0) will not get the right 806 * si_iosize_max unless we fix it up here. 807 */ 808 if (disk->d_cdev && disk->d_rawdev && 809 disk->d_cdev->si_iosize_max == 0) { 810 disk->d_cdev->si_iosize_max = disk->d_rawdev->si_iosize_max; 811 disk->d_cdev->si_bsize_phys = disk->d_rawdev->si_bsize_phys; 812 disk->d_cdev->si_bsize_best = disk->d_rawdev->si_bsize_best; 813 } 814 815 /* Add the serial number to the udev_dictionary */ 816 if (info->d_serialno) 817 udev_dict_set_cstr(disk->d_cdev, "serno", info->d_serialno); 818 } 819 820 /* 821 * Disk drivers must call this routine when media parameters are available 822 * or have changed. 823 */ 824 void 825 disk_setdiskinfo(struct disk *disk, struct disk_info *info) 826 { 827 _setdiskinfo(disk, info); 828 disk_msg_send(DISK_DISK_PROBE, disk, NULL); 829 disk_debug(1, "disk_setdiskinfo: sent probe for %s\n", 830 disk->d_cdev->si_name); 831 } 832 833 void 834 disk_setdiskinfo_sync(struct disk *disk, struct disk_info *info) 835 { 836 _setdiskinfo(disk, info); 837 disk_msg_send_sync(DISK_DISK_PROBE, disk, NULL); 838 disk_debug(1, "disk_setdiskinfo_sync: sent probe for %s\n", 839 disk->d_cdev->si_name); 840 } 841 842 /* 843 * This routine is called when an adapter detaches. The higher level 844 * managed disk device is destroyed while the lower level raw device is 845 * released. 846 */ 847 void 848 disk_destroy(struct disk *disk) 849 { 850 disk_msg_send_sync(DISK_DISK_DESTROY, disk, NULL); 851 return; 852 } 853 854 int 855 disk_dumpcheck(cdev_t dev, u_int64_t *size, 856 u_int64_t *blkno, u_int32_t *secsize) 857 { 858 struct partinfo pinfo; 859 int error; 860 861 if (size) 862 *size = 0; /* avoid gcc warnings */ 863 if (secsize) 864 *secsize = 512; /* avoid gcc warnings */ 865 bzero(&pinfo, sizeof(pinfo)); 866 867 error = dev_dioctl(dev, DIOCGPART, (void *)&pinfo, 0, 868 proc0.p_ucred, NULL, NULL); 869 if (error) 870 return (error); 871 872 if (pinfo.media_blksize == 0) 873 return (ENXIO); 874 875 if (blkno) /* XXX: make sure this reserved stuff is right */ 876 *blkno = pinfo.reserved_blocks + 877 pinfo.media_offset / pinfo.media_blksize; 878 if (secsize) 879 *secsize = pinfo.media_blksize; 880 if (size) 881 *size = (pinfo.media_blocks - pinfo.reserved_blocks); 882 883 return (0); 884 } 885 886 int 887 disk_dumpconf(cdev_t dev, u_int onoff) 888 { 889 struct dumperinfo di; 890 u_int64_t size, blkno; 891 u_int32_t secsize; 892 int error; 893 894 if (!onoff) 895 return set_dumper(NULL); 896 897 error = disk_dumpcheck(dev, &size, &blkno, &secsize); 898 899 if (error) 900 return ENXIO; 901 902 bzero(&di, sizeof(struct dumperinfo)); 903 di.dumper = diskdump; 904 di.priv = dev; 905 di.blocksize = secsize; 906 di.maxiosize = dev->si_iosize_max; 907 di.mediaoffset = blkno * DEV_BSIZE; 908 di.mediasize = size * DEV_BSIZE; 909 910 return set_dumper(&di); 911 } 912 913 void 914 disk_unprobe(struct disk *disk) 915 { 916 if (disk == NULL) 917 return; 918 919 disk_msg_send_sync(DISK_UNPROBE, disk, NULL); 920 } 921 922 void 923 disk_invalidate (struct disk *disk) 924 { 925 dsgone(&disk->d_slice); 926 } 927 928 /* 929 * Enumerate disks, pass a marker and an initial NULL dp to initialize, 930 * then loop with the previously returned dp. 931 * 932 * The returned dp will be referenced, preventing its destruction. When 933 * you pass the returned dp back into the loop the ref is dropped. 934 * 935 * WARNING: If terminating your loop early you must call 936 * disk_enumerate_stop(). 937 */ 938 struct disk * 939 disk_enumerate(struct disk *marker, struct disk *dp) 940 { 941 lwkt_gettoken(&disklist_token); 942 if (dp) { 943 --dp->d_refs; 944 dp = LIST_NEXT(marker, d_list); 945 LIST_REMOVE(marker, d_list); 946 } else { 947 bzero(marker, sizeof(*marker)); 948 marker->d_flags = DISKFLAG_MARKER; 949 dp = LIST_FIRST(&disklist); 950 } 951 while (dp) { 952 if ((dp->d_flags & DISKFLAG_MARKER) == 0) 953 break; 954 dp = LIST_NEXT(dp, d_list); 955 } 956 if (dp) { 957 ++dp->d_refs; 958 LIST_INSERT_AFTER(dp, marker, d_list); 959 } 960 lwkt_reltoken(&disklist_token); 961 return (dp); 962 } 963 964 /* 965 * Terminate an enumeration early. Do not call this function if the 966 * enumeration ended normally. dp can be NULL, indicating that you 967 * wish to retain the ref count on dp. 968 * 969 * This function removes the marker. 970 */ 971 void 972 disk_enumerate_stop(struct disk *marker, struct disk *dp) 973 { 974 lwkt_gettoken(&disklist_token); 975 LIST_REMOVE(marker, d_list); 976 if (dp) 977 --dp->d_refs; 978 lwkt_reltoken(&disklist_token); 979 } 980 981 static 982 int 983 sysctl_disks(SYSCTL_HANDLER_ARGS) 984 { 985 struct disk marker; 986 struct disk *dp; 987 int error, first; 988 989 first = 1; 990 error = 0; 991 dp = NULL; 992 993 while ((dp = disk_enumerate(&marker, dp))) { 994 if (!first) { 995 error = SYSCTL_OUT(req, " ", 1); 996 if (error) { 997 disk_enumerate_stop(&marker, dp); 998 break; 999 } 1000 } else { 1001 first = 0; 1002 } 1003 error = SYSCTL_OUT(req, dp->d_rawdev->si_name, 1004 strlen(dp->d_rawdev->si_name)); 1005 if (error) { 1006 disk_enumerate_stop(&marker, dp); 1007 break; 1008 } 1009 } 1010 if (error == 0) 1011 error = SYSCTL_OUT(req, "", 1); 1012 return error; 1013 } 1014 1015 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, 1016 sysctl_disks, "A", "names of available disks"); 1017 1018 /* 1019 * Open a disk device or partition. 1020 */ 1021 static 1022 int 1023 diskopen(struct dev_open_args *ap) 1024 { 1025 cdev_t dev = ap->a_head.a_dev; 1026 struct disk *dp; 1027 int error; 1028 1029 /* 1030 * dp can't be NULL here XXX. 1031 * 1032 * d_slice will be NULL if setdiskinfo() has not been called yet. 1033 * setdiskinfo() is typically called whether the disk is present 1034 * or not (e.g. CD), but the base disk device is created first 1035 * and there may be a race. 1036 */ 1037 dp = dev->si_disk; 1038 if (dp == NULL || dp->d_slice == NULL) 1039 return (ENXIO); 1040 error = 0; 1041 1042 /* 1043 * Deal with open races 1044 */ 1045 lwkt_gettoken(&ds_token); 1046 while (dp->d_flags & DISKFLAG_LOCK) { 1047 dp->d_flags |= DISKFLAG_WANTED; 1048 error = tsleep(dp, PCATCH, "diskopen", hz); 1049 if (error) { 1050 lwkt_reltoken(&ds_token); 1051 return (error); 1052 } 1053 } 1054 dp->d_flags |= DISKFLAG_LOCK; 1055 1056 /* 1057 * Open the underlying raw device. 1058 */ 1059 if (!dsisopen(dp->d_slice)) { 1060 #if 0 1061 if (!pdev->si_iosize_max) 1062 pdev->si_iosize_max = dev->si_iosize_max; 1063 #endif 1064 error = dev_dopen(dp->d_rawdev, ap->a_oflags, 1065 ap->a_devtype, ap->a_cred, NULL); 1066 } 1067 1068 if (error) 1069 goto out; 1070 error = dsopen(dev, ap->a_devtype, dp->d_info.d_dsflags, 1071 &dp->d_slice, &dp->d_info); 1072 if (!dsisopen(dp->d_slice)) { 1073 dev_dclose(dp->d_rawdev, ap->a_oflags, ap->a_devtype, NULL); 1074 } 1075 out: 1076 dp->d_flags &= ~DISKFLAG_LOCK; 1077 if (dp->d_flags & DISKFLAG_WANTED) { 1078 dp->d_flags &= ~DISKFLAG_WANTED; 1079 wakeup(dp); 1080 } 1081 lwkt_reltoken(&ds_token); 1082 1083 KKASSERT(dp->d_opencount >= 0); 1084 /* If the open was successful, bump open count */ 1085 if (error == 0) 1086 atomic_add_int(&dp->d_opencount, 1); 1087 1088 return(error); 1089 } 1090 1091 /* 1092 * Close a disk device or partition 1093 */ 1094 static 1095 int 1096 diskclose(struct dev_close_args *ap) 1097 { 1098 cdev_t dev = ap->a_head.a_dev; 1099 struct disk *dp; 1100 int error; 1101 int lcount; 1102 1103 error = 0; 1104 dp = dev->si_disk; 1105 1106 /* 1107 * The cdev_t represents the disk/slice/part. The shared 1108 * dp structure governs all cdevs associated with the disk. 1109 * 1110 * As a safety only close the underlying raw device on the last 1111 * close the disk device if our tracking of the slices/partitions 1112 * also indicates nothing is open. 1113 */ 1114 KKASSERT(dp->d_opencount >= 1); 1115 lcount = atomic_fetchadd_int(&dp->d_opencount, -1); 1116 1117 lwkt_gettoken(&ds_token); 1118 dsclose(dev, ap->a_devtype, dp->d_slice); 1119 if (lcount <= 1 && !dsisopen(dp->d_slice)) { 1120 error = dev_dclose(dp->d_rawdev, ap->a_fflag, ap->a_devtype, NULL); 1121 } 1122 lwkt_reltoken(&ds_token); 1123 1124 return (error); 1125 } 1126 1127 /* 1128 * First execute the ioctl on the disk device, and if it isn't supported 1129 * try running it on the backing device. 1130 */ 1131 static 1132 int 1133 diskioctl(struct dev_ioctl_args *ap) 1134 { 1135 cdev_t dev = ap->a_head.a_dev; 1136 struct disk *dp; 1137 int error; 1138 u_int u; 1139 1140 dp = dev->si_disk; 1141 if (dp == NULL) 1142 return (ENXIO); 1143 1144 devfs_debug(DEVFS_DEBUG_DEBUG, 1145 "diskioctl: cmd is: %lx (name: %s)\n", 1146 ap->a_cmd, dev->si_name); 1147 devfs_debug(DEVFS_DEBUG_DEBUG, 1148 "diskioctl: &dp->d_slice is: %p, %p\n", 1149 &dp->d_slice, dp->d_slice); 1150 1151 if (ap->a_cmd == DIOCGKERNELDUMP) { 1152 u = *(u_int *)ap->a_data; 1153 return disk_dumpconf(dev, u); 1154 } 1155 1156 if (ap->a_cmd == DIOCRECLUSTER && dev == dp->d_cdev) { 1157 error = disk_iocom_ioctl(dp, ap->a_cmd, ap->a_data); 1158 return error; 1159 } 1160 1161 if (&dp->d_slice == NULL || dp->d_slice == NULL || 1162 ((dp->d_info.d_dsflags & DSO_DEVICEMAPPER) && 1163 dkslice(dev) == WHOLE_DISK_SLICE)) { 1164 error = ENOIOCTL; 1165 } else { 1166 lwkt_gettoken(&ds_token); 1167 error = dsioctl(dev, ap->a_cmd, ap->a_data, ap->a_fflag, 1168 &dp->d_slice, &dp->d_info); 1169 lwkt_reltoken(&ds_token); 1170 } 1171 1172 if (error == ENOIOCTL) { 1173 error = dev_dioctl(dp->d_rawdev, ap->a_cmd, ap->a_data, 1174 ap->a_fflag, ap->a_cred, NULL, NULL); 1175 } 1176 return (error); 1177 } 1178 1179 /* 1180 * Execute strategy routine 1181 * 1182 * WARNING! We are using the KVABIO API and must not access memory 1183 * through bp->b_data without first calling bkvasync(bp). 1184 */ 1185 static 1186 int 1187 diskstrategy(struct dev_strategy_args *ap) 1188 { 1189 cdev_t dev = ap->a_head.a_dev; 1190 struct bio *bio = ap->a_bio; 1191 struct bio *nbio; 1192 struct disk *dp; 1193 1194 dp = dev->si_disk; 1195 1196 if (dp == NULL) { 1197 bio->bio_buf->b_error = ENXIO; 1198 bio->bio_buf->b_flags |= B_ERROR; 1199 biodone(bio); 1200 return(0); 1201 } 1202 KKASSERT(dev->si_disk == dp); 1203 1204 /* 1205 * The dscheck() function will also transform the slice relative 1206 * block number i.e. bio->bio_offset into a block number that can be 1207 * passed directly to the underlying raw device. If dscheck() 1208 * returns NULL it will have handled the bio for us (e.g. EOF 1209 * or error due to being beyond the device size). 1210 */ 1211 if ((nbio = dscheck(dev, bio, dp->d_slice)) != NULL) { 1212 dev_dstrategy(dp->d_rawdev, nbio); 1213 } else { 1214 biodone(bio); 1215 } 1216 return(0); 1217 } 1218 1219 /* 1220 * Return the partition size in ?blocks? 1221 */ 1222 static 1223 int 1224 diskpsize(struct dev_psize_args *ap) 1225 { 1226 cdev_t dev = ap->a_head.a_dev; 1227 struct disk *dp; 1228 1229 dp = dev->si_disk; 1230 if (dp == NULL) 1231 return(ENODEV); 1232 1233 ap->a_result = dssize(dev, &dp->d_slice); 1234 1235 if ((ap->a_result == -1) && 1236 (dp->d_info.d_dsflags & DSO_RAWPSIZE)) { 1237 ap->a_head.a_dev = dp->d_rawdev; 1238 return dev_doperate(&ap->a_head); 1239 } 1240 return(0); 1241 } 1242 1243 static int 1244 diskdump(struct dev_dump_args *ap) 1245 { 1246 cdev_t dev = ap->a_head.a_dev; 1247 struct disk *dp = dev->si_disk; 1248 u_int64_t size, offset; 1249 int error; 1250 1251 error = disk_dumpcheck(dev, &size, &ap->a_blkno, &ap->a_secsize); 1252 /* XXX: this should probably go in disk_dumpcheck somehow */ 1253 if (ap->a_length != 0) { 1254 size *= DEV_BSIZE; 1255 offset = ap->a_blkno * DEV_BSIZE; 1256 if ((ap->a_offset < offset) || 1257 (ap->a_offset + ap->a_length - offset > size)) { 1258 kprintf("Attempt to write outside dump " 1259 "device boundaries.\n"); 1260 error = ENOSPC; 1261 } 1262 } 1263 1264 if (error == 0) { 1265 ap->a_head.a_dev = dp->d_rawdev; 1266 error = dev_doperate(&ap->a_head); 1267 } 1268 1269 return(error); 1270 } 1271 1272 1273 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD, 1274 0, sizeof(struct diskslices), "sizeof(struct diskslices)"); 1275 1276 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD, 1277 0, sizeof(struct disk), "sizeof(struct disk)"); 1278 1279 /* 1280 * Reorder interval for burst write allowance and minor write 1281 * allowance. 1282 * 1283 * We always want to trickle some writes in to make use of the 1284 * disk's zone cache. Bursting occurs on a longer interval and only 1285 * runningbufspace is well over the hirunningspace limit. 1286 */ 1287 int bioq_reorder_burst_interval = 60; /* should be multiple of minor */ 1288 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_burst_interval, 1289 CTLFLAG_RW, &bioq_reorder_burst_interval, 0, ""); 1290 int bioq_reorder_minor_interval = 5; 1291 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_minor_interval, 1292 CTLFLAG_RW, &bioq_reorder_minor_interval, 0, ""); 1293 1294 int bioq_reorder_burst_bytes = 3000000; 1295 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_burst_bytes, 1296 CTLFLAG_RW, &bioq_reorder_burst_bytes, 0, ""); 1297 int bioq_reorder_minor_bytes = 262144; 1298 SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_minor_bytes, 1299 CTLFLAG_RW, &bioq_reorder_minor_bytes, 0, ""); 1300 1301 1302 /* 1303 * Order I/Os. Generally speaking this code is designed to make better 1304 * use of drive zone caches. A drive zone cache can typically track linear 1305 * reads or writes for around 16 zones simultaniously. 1306 * 1307 * Read prioritization issues: It is possible for hundreds of megabytes worth 1308 * of writes to be queued asynchronously. This creates a huge bottleneck 1309 * for reads which reduce read bandwidth to a trickle. 1310 * 1311 * To solve this problem we generally reorder reads before writes. 1312 * 1313 * However, a large number of random reads can also starve writes and 1314 * make poor use of the drive zone cache so we allow writes to trickle 1315 * in every N reads. 1316 */ 1317 void 1318 bioqdisksort(struct bio_queue_head *bioq, struct bio *bio) 1319 { 1320 #if 0 1321 /* 1322 * The BIO wants to be ordered. Adding to the tail also 1323 * causes transition to be set to NULL, forcing the ordering 1324 * of all prior I/O's. 1325 */ 1326 if (bio->bio_buf->b_flags & B_ORDERED) { 1327 bioq_insert_tail(bioq, bio); 1328 return; 1329 } 1330 #endif 1331 1332 switch(bio->bio_buf->b_cmd) { 1333 case BUF_CMD_READ: 1334 if (bioq->transition) { 1335 /* 1336 * Insert before the first write. Bleedover writes 1337 * based on reorder intervals to prevent starvation. 1338 */ 1339 TAILQ_INSERT_BEFORE(bioq->transition, bio, bio_act); 1340 ++bioq->reorder; 1341 if (bioq->reorder % bioq_reorder_minor_interval == 0) { 1342 bioqwritereorder(bioq); 1343 if (bioq->reorder >= 1344 bioq_reorder_burst_interval) { 1345 bioq->reorder = 0; 1346 } 1347 } 1348 } else { 1349 /* 1350 * No writes queued (or ordering was forced), 1351 * insert at tail. 1352 */ 1353 TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act); 1354 } 1355 break; 1356 case BUF_CMD_WRITE: 1357 /* 1358 * Writes are always appended. If no writes were previously 1359 * queued or an ordered tail insertion occured the transition 1360 * field will be NULL. 1361 */ 1362 TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act); 1363 if (bioq->transition == NULL) 1364 bioq->transition = bio; 1365 break; 1366 default: 1367 /* 1368 * All other request types are forced to be ordered. 1369 */ 1370 bioq_insert_tail(bioq, bio); 1371 break; 1372 } 1373 } 1374 1375 /* 1376 * Move the read-write transition point to prevent reads from 1377 * completely starving our writes. This brings a number of writes into 1378 * the fold every N reads. 1379 * 1380 * We bring a few linear writes into the fold on a minor interval 1381 * and we bring a non-linear burst of writes into the fold on a major 1382 * interval. Bursting only occurs if runningbufspace is really high 1383 * (typically from syncs, fsyncs, or HAMMER flushes). 1384 */ 1385 static 1386 void 1387 bioqwritereorder(struct bio_queue_head *bioq) 1388 { 1389 struct bio *bio; 1390 off_t next_offset; 1391 size_t left; 1392 size_t n; 1393 int check_off; 1394 1395 if (bioq->reorder < bioq_reorder_burst_interval || 1396 !buf_runningbufspace_severe()) { 1397 left = (size_t)bioq_reorder_minor_bytes; 1398 check_off = 1; 1399 } else { 1400 left = (size_t)bioq_reorder_burst_bytes; 1401 check_off = 0; 1402 } 1403 1404 next_offset = bioq->transition->bio_offset; 1405 while ((bio = bioq->transition) != NULL && 1406 (check_off == 0 || next_offset == bio->bio_offset) 1407 ) { 1408 n = bio->bio_buf->b_bcount; 1409 next_offset = bio->bio_offset + n; 1410 bioq->transition = TAILQ_NEXT(bio, bio_act); 1411 if (left < n) 1412 break; 1413 left -= n; 1414 } 1415 } 1416 1417 /* 1418 * Bounds checking against the media size, used for the raw partition. 1419 * secsize, mediasize and b_blkno must all be the same units. 1420 * Possibly this has to be DEV_BSIZE (512). 1421 */ 1422 int 1423 bounds_check_with_mediasize(struct bio *bio, int secsize, uint64_t mediasize) 1424 { 1425 struct buf *bp = bio->bio_buf; 1426 int64_t sz; 1427 1428 sz = howmany(bp->b_bcount, secsize); 1429 1430 if (bio->bio_offset/DEV_BSIZE + sz > mediasize) { 1431 sz = mediasize - bio->bio_offset/DEV_BSIZE; 1432 if (sz == 0) { 1433 /* If exactly at end of disk, return EOF. */ 1434 bp->b_resid = bp->b_bcount; 1435 return 0; 1436 } 1437 if (sz < 0) { 1438 /* If past end of disk, return EINVAL. */ 1439 bp->b_error = EINVAL; 1440 return 0; 1441 } 1442 /* Otherwise, truncate request. */ 1443 bp->b_bcount = sz * secsize; 1444 } 1445 1446 return 1; 1447 } 1448 1449 /* 1450 * Disk error is the preface to plaintive error messages 1451 * about failing disk transfers. It prints messages of the form 1452 1453 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d) 1454 1455 * if the offset of the error in the transfer and a disk label 1456 * are both available. blkdone should be -1 if the position of the error 1457 * is unknown; the disklabel pointer may be null from drivers that have not 1458 * been converted to use them. The message is printed with kprintf 1459 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority. 1460 * The message should be completed (with at least a newline) with kprintf 1461 * or log(-1, ...), respectively. There is no trailing space. 1462 */ 1463 void 1464 diskerr(struct bio *bio, cdev_t dev, const char *what, int pri, int donecnt) 1465 { 1466 struct buf *bp = bio->bio_buf; 1467 const char *term; 1468 1469 switch(bp->b_cmd) { 1470 case BUF_CMD_READ: 1471 term = "read"; 1472 break; 1473 case BUF_CMD_WRITE: 1474 term = "write"; 1475 break; 1476 default: 1477 term = "access"; 1478 break; 1479 } 1480 kprintf("%s: %s %sing ", dev->si_name, what, term); 1481 kprintf("offset %012llx for %d", 1482 (long long)bio->bio_offset, 1483 bp->b_bcount); 1484 1485 if (donecnt) 1486 kprintf(" (%d bytes completed)", donecnt); 1487 } 1488 1489 /* 1490 * Locate a disk device 1491 */ 1492 cdev_t 1493 disk_locate(const char *devname) 1494 { 1495 return devfs_find_device_by_name("%s", devname); 1496 } 1497 1498 void 1499 disk_config(void *arg) 1500 { 1501 disk_msg_send_sync(DISK_SYNC, NULL, NULL); 1502 } 1503 1504 static void 1505 disk_init(void) 1506 { 1507 struct thread* td_core; 1508 1509 disk_msg_cache = objcache_create("disk-msg-cache", 0, 0, 1510 NULL, NULL, NULL, 1511 objcache_malloc_alloc, 1512 objcache_malloc_free, 1513 &disk_msg_malloc_args); 1514 1515 lwkt_token_init(&disklist_token, "disks"); 1516 lwkt_token_init(&ds_token, "ds"); 1517 1518 /* 1519 * Initialize the reply-only port which acts as a message drain 1520 */ 1521 lwkt_initport_replyonly(&disk_dispose_port, disk_msg_autofree_reply); 1522 1523 lwkt_gettoken(&disklist_token); 1524 lwkt_create(disk_msg_core, /*args*/NULL, &td_core, NULL, 1525 0, -1, "disk_msg_core"); 1526 tsleep(td_core, 0, "diskcore", 0); 1527 lwkt_reltoken(&disklist_token); 1528 } 1529 1530 static void 1531 disk_uninit(void) 1532 { 1533 objcache_destroy(disk_msg_cache); 1534 } 1535 1536 /* 1537 * Clean out illegal characters in serial numbers. 1538 */ 1539 static void 1540 disk_cleanserial(char *serno) 1541 { 1542 char c; 1543 1544 while ((c = *serno) != 0) { 1545 if (c >= 'a' && c <= 'z') 1546 ; 1547 else if (c >= 'A' && c <= 'Z') 1548 ; 1549 else if (c >= '0' && c <= '9') 1550 ; 1551 else if (c == '-' || c == '@' || c == '+' || c == '.') 1552 ; 1553 else 1554 c = '_'; 1555 *serno++= c; 1556 } 1557 } 1558 1559 TUNABLE_INT("kern.disk_debug", &disk_debug_enable); 1560 SYSCTL_INT(_kern, OID_AUTO, disk_debug, CTLFLAG_RW, &disk_debug_enable, 1561 0, "Enable subr_disk debugging"); 1562 1563 SYSINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, disk_init, NULL); 1564 SYSUNINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, disk_uninit, NULL); 1565