1 /* $NetBSD: ld.c,v 1.66 2009/07/23 21:38:33 dyoung Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran and Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Disk driver for use by RAID controllers. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.66 2009/07/23 21:38:33 dyoung Exp $"); 38 39 #include "rnd.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/device.h> 45 #include <sys/queue.h> 46 #include <sys/proc.h> 47 #include <sys/buf.h> 48 #include <sys/bufq.h> 49 #include <sys/endian.h> 50 #include <sys/disklabel.h> 51 #include <sys/disk.h> 52 #include <sys/dkio.h> 53 #include <sys/stat.h> 54 #include <sys/conf.h> 55 #include <sys/fcntl.h> 56 #include <sys/vnode.h> 57 #include <sys/syslog.h> 58 #include <sys/mutex.h> 59 #if NRND > 0 60 #include <sys/rnd.h> 61 #endif 62 63 #include <dev/ldvar.h> 64 65 #include <prop/proplib.h> 66 67 static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *); 68 static void ldgetdisklabel(struct ld_softc *); 69 static void ldminphys(struct buf *bp); 70 static bool ld_shutdown(device_t, int); 71 static void ldstart(struct ld_softc *, struct buf *); 72 static void ld_set_properties(struct ld_softc *); 73 static void ld_config_interrupts (device_t); 74 static int ldlastclose(device_t); 75 76 extern struct cfdriver ld_cd; 77 78 static dev_type_open(ldopen); 79 static dev_type_close(ldclose); 80 static dev_type_read(ldread); 81 static dev_type_write(ldwrite); 82 static dev_type_ioctl(ldioctl); 83 static dev_type_strategy(ldstrategy); 84 static dev_type_dump(lddump); 85 static dev_type_size(ldsize); 86 87 const struct bdevsw ld_bdevsw = { 88 ldopen, ldclose, ldstrategy, ldioctl, lddump, ldsize, D_DISK 89 }; 90 91 const struct cdevsw ld_cdevsw = { 92 ldopen, ldclose, ldread, ldwrite, ldioctl, 93 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 94 }; 95 96 static struct dkdriver lddkdriver = { ldstrategy, ldminphys }; 97 98 void 99 ldattach(struct ld_softc *sc) 100 { 101 char tbuf[9]; 102 103 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM); 104 105 if ((sc->sc_flags & LDF_ENABLED) == 0) { 106 aprint_normal_dev(sc->sc_dv, "disabled\n"); 107 return; 108 } 109 110 /* Initialise and attach the disk structure. */ 111 disk_init(&sc->sc_dk, device_xname(sc->sc_dv), &lddkdriver); 112 disk_attach(&sc->sc_dk); 113 114 if (sc->sc_maxxfer > MAXPHYS) 115 sc->sc_maxxfer = MAXPHYS; 116 117 /* Build synthetic geometry if necessary. */ 118 if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 || 119 sc->sc_ncylinders == 0) { 120 uint64_t ncyl; 121 122 if (sc->sc_secperunit <= 528 * 2048) /* 528MB */ 123 sc->sc_nheads = 16; 124 else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */ 125 sc->sc_nheads = 32; 126 else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */ 127 sc->sc_nheads = 64; 128 else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */ 129 sc->sc_nheads = 128; 130 else 131 sc->sc_nheads = 255; 132 133 sc->sc_nsectors = 63; 134 sc->sc_ncylinders = INT_MAX; 135 ncyl = sc->sc_secperunit / 136 (sc->sc_nheads * sc->sc_nsectors); 137 if (ncyl < INT_MAX) 138 sc->sc_ncylinders = (int)ncyl; 139 } 140 141 format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit * 142 sc->sc_secsize); 143 aprint_normal_dev(sc->sc_dv, "%s, %d cyl, %d head, %d sec, " 144 "%d bytes/sect x %"PRIu64" sectors\n", 145 tbuf, sc->sc_ncylinders, sc->sc_nheads, 146 sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit); 147 148 ld_set_properties(sc); 149 150 #if NRND > 0 151 /* Attach the device into the rnd source list. */ 152 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dv), 153 RND_TYPE_DISK, 0); 154 #endif 155 156 /* Register with PMF */ 157 if (!pmf_device_register1(sc->sc_dv, NULL, NULL, ld_shutdown)) 158 aprint_error_dev(sc->sc_dv, 159 "couldn't establish power handler\n"); 160 161 bufq_alloc(&sc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK); 162 163 /* Discover wedges on this disk. */ 164 config_interrupts(sc->sc_dv, ld_config_interrupts); 165 } 166 167 int 168 ldadjqparam(struct ld_softc *sc, int xmax) 169 { 170 int s; 171 172 s = splbio(); 173 sc->sc_maxqueuecnt = xmax; 174 splx(s); 175 176 return (0); 177 } 178 179 int 180 ldbegindetach(struct ld_softc *sc, int flags) 181 { 182 int s, rv = 0; 183 184 if ((sc->sc_flags & LDF_ENABLED) == 0) 185 return (0); 186 187 rv = disk_begindetach(&sc->sc_dk, ldlastclose, sc->sc_dv, flags); 188 189 if (rv != 0) 190 return rv; 191 192 s = splbio(); 193 sc->sc_maxqueuecnt = 0; 194 sc->sc_flags |= LDF_DETACH; 195 while (sc->sc_queuecnt > 0) { 196 sc->sc_flags |= LDF_DRAIN; 197 rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0); 198 if (rv) 199 break; 200 } 201 splx(s); 202 203 return (rv); 204 } 205 206 void 207 ldenddetach(struct ld_softc *sc) 208 { 209 int s, bmaj, cmaj, i, mn; 210 211 if ((sc->sc_flags & LDF_ENABLED) == 0) 212 return; 213 214 /* Wait for commands queued with the hardware to complete. */ 215 if (sc->sc_queuecnt != 0) 216 if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz)) 217 printf("%s: not drained\n", device_xname(sc->sc_dv)); 218 219 /* Locate the major numbers. */ 220 bmaj = bdevsw_lookup_major(&ld_bdevsw); 221 cmaj = cdevsw_lookup_major(&ld_cdevsw); 222 223 /* Kill off any queued buffers. */ 224 s = splbio(); 225 bufq_drain(sc->sc_bufq); 226 splx(s); 227 228 bufq_free(sc->sc_bufq); 229 230 /* Nuke the vnodes for any open instances. */ 231 for (i = 0; i < MAXPARTITIONS; i++) { 232 mn = DISKMINOR(device_unit(sc->sc_dv), i); 233 vdevgone(bmaj, mn, mn, VBLK); 234 vdevgone(cmaj, mn, mn, VCHR); 235 } 236 237 /* Delete all of our wedges. */ 238 dkwedge_delall(&sc->sc_dk); 239 240 /* Detach from the disk list. */ 241 disk_detach(&sc->sc_dk); 242 disk_destroy(&sc->sc_dk); 243 244 #if NRND > 0 245 /* Unhook the entropy source. */ 246 rnd_detach_source(&sc->sc_rnd_source); 247 #endif 248 249 /* Deregister with PMF */ 250 pmf_device_deregister(sc->sc_dv); 251 252 /* 253 * XXX We can't really flush the cache here, beceause the 254 * XXX device may already be non-existent from the controller's 255 * XXX perspective. 256 */ 257 #if 0 258 /* Flush the device's cache. */ 259 if (sc->sc_flush != NULL) 260 if ((*sc->sc_flush)(sc, 0) != 0) 261 aprint_error_dev(&sc->sc_dv, "unable to flush cache\n"); 262 #endif 263 mutex_destroy(&sc->sc_mutex); 264 } 265 266 /* ARGSUSED */ 267 static bool 268 ld_shutdown(device_t dev, int flags) 269 { 270 struct ld_softc *sc = device_private(dev); 271 272 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, LDFL_POLL) != 0) { 273 printf("%s: unable to flush cache\n", device_xname(dev)); 274 return false; 275 } 276 277 return true; 278 } 279 280 /* ARGSUSED */ 281 static int 282 ldopen(dev_t dev, int flags, int fmt, struct lwp *l) 283 { 284 struct ld_softc *sc; 285 int error, unit, part; 286 287 unit = DISKUNIT(dev); 288 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL) 289 return (ENXIO); 290 if ((sc->sc_flags & LDF_ENABLED) == 0) 291 return (ENODEV); 292 part = DISKPART(dev); 293 294 mutex_enter(&sc->sc_dk.dk_openlock); 295 296 if (sc->sc_dk.dk_openmask == 0) { 297 /* Load the partition info if not already loaded. */ 298 if ((sc->sc_flags & LDF_VLABEL) == 0) 299 ldgetdisklabel(sc); 300 } 301 302 /* Check that the partition exists. */ 303 if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions || 304 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { 305 error = ENXIO; 306 goto bad1; 307 } 308 309 /* Ensure only one open at a time. */ 310 switch (fmt) { 311 case S_IFCHR: 312 sc->sc_dk.dk_copenmask |= (1 << part); 313 break; 314 case S_IFBLK: 315 sc->sc_dk.dk_bopenmask |= (1 << part); 316 break; 317 } 318 sc->sc_dk.dk_openmask = 319 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 320 321 error = 0; 322 bad1: 323 mutex_exit(&sc->sc_dk.dk_openlock); 324 return (error); 325 } 326 327 static int 328 ldlastclose(device_t self) 329 { 330 struct ld_softc *sc = device_private(self); 331 332 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, 0) != 0) 333 aprint_error_dev(self, "unable to flush cache\n"); 334 if ((sc->sc_flags & LDF_KLABEL) == 0) 335 sc->sc_flags &= ~LDF_VLABEL; 336 337 return 0; 338 } 339 340 /* ARGSUSED */ 341 static int 342 ldclose(dev_t dev, int flags, int fmt, struct lwp *l) 343 { 344 struct ld_softc *sc; 345 int part, unit; 346 347 unit = DISKUNIT(dev); 348 part = DISKPART(dev); 349 sc = device_lookup_private(&ld_cd, unit); 350 351 mutex_enter(&sc->sc_dk.dk_openlock); 352 353 switch (fmt) { 354 case S_IFCHR: 355 sc->sc_dk.dk_copenmask &= ~(1 << part); 356 break; 357 case S_IFBLK: 358 sc->sc_dk.dk_bopenmask &= ~(1 << part); 359 break; 360 } 361 sc->sc_dk.dk_openmask = 362 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 363 364 if (sc->sc_dk.dk_openmask == 0) 365 ldlastclose(sc->sc_dv); 366 367 mutex_exit(&sc->sc_dk.dk_openlock); 368 return (0); 369 } 370 371 /* ARGSUSED */ 372 static int 373 ldread(dev_t dev, struct uio *uio, int ioflag) 374 { 375 376 return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio)); 377 } 378 379 /* ARGSUSED */ 380 static int 381 ldwrite(dev_t dev, struct uio *uio, int ioflag) 382 { 383 384 return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio)); 385 } 386 387 /* ARGSUSED */ 388 static int 389 ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l) 390 { 391 struct ld_softc *sc; 392 int part, unit, error; 393 #ifdef __HAVE_OLD_DISKLABEL 394 struct disklabel newlabel; 395 #endif 396 struct disklabel *lp; 397 398 unit = DISKUNIT(dev); 399 part = DISKPART(dev); 400 sc = device_lookup_private(&ld_cd, unit); 401 402 error = disk_ioctl(&sc->sc_dk, cmd, addr, flag, l); 403 if (error != EPASSTHROUGH) 404 return (error); 405 406 error = 0; 407 switch (cmd) { 408 case DIOCGDINFO: 409 memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel)); 410 return (0); 411 412 #ifdef __HAVE_OLD_DISKLABEL 413 case ODIOCGDINFO: 414 newlabel = *(sc->sc_dk.dk_label); 415 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 416 return ENOTTY; 417 memcpy(addr, &newlabel, sizeof(struct olddisklabel)); 418 return (0); 419 #endif 420 421 case DIOCGPART: 422 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label; 423 ((struct partinfo *)addr)->part = 424 &sc->sc_dk.dk_label->d_partitions[part]; 425 break; 426 427 case DIOCWDINFO: 428 case DIOCSDINFO: 429 #ifdef __HAVE_OLD_DISKLABEL 430 case ODIOCWDINFO: 431 case ODIOCSDINFO: 432 433 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 434 memset(&newlabel, 0, sizeof newlabel); 435 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 436 lp = &newlabel; 437 } else 438 #endif 439 lp = (struct disklabel *)addr; 440 441 if ((flag & FWRITE) == 0) 442 return (EBADF); 443 444 mutex_enter(&sc->sc_dk.dk_openlock); 445 sc->sc_flags |= LDF_LABELLING; 446 447 error = setdisklabel(sc->sc_dk.dk_label, 448 lp, /*sc->sc_dk.dk_openmask : */0, 449 sc->sc_dk.dk_cpulabel); 450 if (error == 0 && (cmd == DIOCWDINFO 451 #ifdef __HAVE_OLD_DISKLABEL 452 || cmd == ODIOCWDINFO 453 #endif 454 )) 455 error = writedisklabel( 456 MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART), 457 ldstrategy, sc->sc_dk.dk_label, 458 sc->sc_dk.dk_cpulabel); 459 460 sc->sc_flags &= ~LDF_LABELLING; 461 mutex_exit(&sc->sc_dk.dk_openlock); 462 break; 463 464 case DIOCKLABEL: 465 if ((flag & FWRITE) == 0) 466 return (EBADF); 467 if (*(int *)addr) 468 sc->sc_flags |= LDF_KLABEL; 469 else 470 sc->sc_flags &= ~LDF_KLABEL; 471 break; 472 473 case DIOCWLABEL: 474 if ((flag & FWRITE) == 0) 475 return (EBADF); 476 if (*(int *)addr) 477 sc->sc_flags |= LDF_WLABEL; 478 else 479 sc->sc_flags &= ~LDF_WLABEL; 480 break; 481 482 case DIOCGDEFLABEL: 483 ldgetdefaultlabel(sc, (struct disklabel *)addr); 484 break; 485 486 #ifdef __HAVE_OLD_DISKLABEL 487 case ODIOCGDEFLABEL: 488 ldgetdefaultlabel(sc, &newlabel); 489 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 490 return ENOTTY; 491 memcpy(addr, &newlabel, sizeof (struct olddisklabel)); 492 break; 493 #endif 494 495 case DIOCCACHESYNC: 496 /* 497 * XXX Do we really need to care about having a writable 498 * file descriptor here? 499 */ 500 if ((flag & FWRITE) == 0) 501 error = EBADF; 502 else if (sc->sc_flush) 503 error = (*sc->sc_flush)(sc, 0); 504 else 505 error = 0; /* XXX Error out instead? */ 506 break; 507 508 case DIOCAWEDGE: 509 { 510 struct dkwedge_info *dkw = (void *) addr; 511 512 if ((flag & FWRITE) == 0) 513 return (EBADF); 514 515 /* If the ioctl happens here, the parent is us. */ 516 strlcpy(dkw->dkw_parent, device_xname(sc->sc_dv), 517 sizeof(dkw->dkw_parent)); 518 return (dkwedge_add(dkw)); 519 } 520 521 case DIOCDWEDGE: 522 { 523 struct dkwedge_info *dkw = (void *) addr; 524 525 if ((flag & FWRITE) == 0) 526 return (EBADF); 527 528 /* If the ioctl happens here, the parent is us. */ 529 strlcpy(dkw->dkw_parent, device_xname(sc->sc_dv), 530 sizeof(dkw->dkw_parent)); 531 return (dkwedge_del(dkw)); 532 } 533 534 case DIOCLWEDGES: 535 { 536 struct dkwedge_list *dkwl = (void *) addr; 537 538 return (dkwedge_list(&sc->sc_dk, dkwl, l)); 539 } 540 case DIOCGSTRATEGY: 541 { 542 struct disk_strategy *dks = (void *)addr; 543 544 mutex_enter(&sc->sc_mutex); 545 strlcpy(dks->dks_name, bufq_getstrategyname(sc->sc_bufq), 546 sizeof(dks->dks_name)); 547 mutex_exit(&sc->sc_mutex); 548 dks->dks_paramlen = 0; 549 550 return 0; 551 } 552 case DIOCSSTRATEGY: 553 { 554 struct disk_strategy *dks = (void *)addr; 555 struct bufq_state *new, *old; 556 557 if ((flag & FWRITE) == 0) 558 return EPERM; 559 560 if (dks->dks_param != NULL) 561 return EINVAL; 562 563 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */ 564 error = bufq_alloc(&new, dks->dks_name, 565 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK); 566 if (error) 567 return error; 568 569 mutex_enter(&sc->sc_mutex); 570 old = sc->sc_bufq; 571 bufq_move(new, old); 572 sc->sc_bufq = new; 573 mutex_exit(&sc->sc_mutex); 574 bufq_free(old); 575 576 return 0; 577 } 578 default: 579 error = ENOTTY; 580 break; 581 } 582 583 return (error); 584 } 585 586 static void 587 ldstrategy(struct buf *bp) 588 { 589 struct ld_softc *sc; 590 struct disklabel *lp; 591 daddr_t blkno; 592 int s, part; 593 594 sc = device_lookup_private(&ld_cd, DISKUNIT(bp->b_dev)); 595 part = DISKPART(bp->b_dev); 596 597 if ((sc->sc_flags & LDF_DETACH) != 0) { 598 bp->b_error = EIO; 599 goto done; 600 } 601 602 lp = sc->sc_dk.dk_label; 603 604 /* 605 * The transfer must be a whole number of blocks and the offset must 606 * not be negative. 607 */ 608 if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) { 609 bp->b_error = EINVAL; 610 goto done; 611 } 612 613 /* If it's a null transfer, return immediately. */ 614 if (bp->b_bcount == 0) 615 goto done; 616 617 /* 618 * Do bounds checking and adjust the transfer. If error, process. 619 * If past the end of partition, just return. 620 */ 621 if (part != RAW_PART && 622 bounds_check_with_label(&sc->sc_dk, bp, 623 (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) { 624 goto done; 625 } 626 627 /* 628 * Convert the block number to absolute and put it in terms 629 * of the device's logical block size. 630 */ 631 if (lp->d_secsize == DEV_BSIZE) 632 blkno = bp->b_blkno; 633 else if (lp->d_secsize > DEV_BSIZE) 634 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); 635 else 636 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); 637 638 if (part != RAW_PART) 639 blkno += lp->d_partitions[part].p_offset; 640 641 bp->b_rawblkno = blkno; 642 643 s = splbio(); 644 ldstart(sc, bp); 645 splx(s); 646 return; 647 648 done: 649 bp->b_resid = bp->b_bcount; 650 biodone(bp); 651 } 652 653 static void 654 ldstart(struct ld_softc *sc, struct buf *bp) 655 { 656 int error; 657 658 mutex_enter(&sc->sc_mutex); 659 660 if (bp != NULL) 661 bufq_put(sc->sc_bufq, bp); 662 663 while (sc->sc_queuecnt < sc->sc_maxqueuecnt) { 664 /* See if there is work to do. */ 665 if ((bp = bufq_peek(sc->sc_bufq)) == NULL) 666 break; 667 668 disk_busy(&sc->sc_dk); 669 sc->sc_queuecnt++; 670 671 if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) { 672 /* 673 * The back-end is running the job; remove it from 674 * the queue. 675 */ 676 (void) bufq_get(sc->sc_bufq); 677 } else { 678 disk_unbusy(&sc->sc_dk, 0, (bp->b_flags & B_READ)); 679 sc->sc_queuecnt--; 680 if (error == EAGAIN) { 681 /* 682 * Temporary resource shortage in the 683 * back-end; just defer the job until 684 * later. 685 * 686 * XXX We might consider a watchdog timer 687 * XXX to make sure we are kicked into action. 688 */ 689 break; 690 } else { 691 (void) bufq_get(sc->sc_bufq); 692 bp->b_error = error; 693 bp->b_resid = bp->b_bcount; 694 mutex_exit(&sc->sc_mutex); 695 biodone(bp); 696 mutex_enter(&sc->sc_mutex); 697 } 698 } 699 } 700 701 mutex_exit(&sc->sc_mutex); 702 } 703 704 void 705 lddone(struct ld_softc *sc, struct buf *bp) 706 { 707 708 if (bp->b_error != 0) { 709 diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label); 710 printf("\n"); 711 } 712 713 disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid, 714 (bp->b_flags & B_READ)); 715 #if NRND > 0 716 rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno); 717 #endif 718 biodone(bp); 719 720 mutex_enter(&sc->sc_mutex); 721 if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) { 722 if ((sc->sc_flags & LDF_DRAIN) != 0) { 723 sc->sc_flags &= ~LDF_DRAIN; 724 wakeup(&sc->sc_queuecnt); 725 } 726 mutex_exit(&sc->sc_mutex); 727 ldstart(sc, NULL); 728 } else 729 mutex_exit(&sc->sc_mutex); 730 } 731 732 static int 733 ldsize(dev_t dev) 734 { 735 struct ld_softc *sc; 736 int part, unit, omask, size; 737 738 unit = DISKUNIT(dev); 739 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL) 740 return (ENODEV); 741 if ((sc->sc_flags & LDF_ENABLED) == 0) 742 return (ENODEV); 743 part = DISKPART(dev); 744 745 omask = sc->sc_dk.dk_openmask & (1 << part); 746 747 if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0) 748 return (-1); 749 else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) 750 size = -1; 751 else 752 size = sc->sc_dk.dk_label->d_partitions[part].p_size * 753 (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE); 754 if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0) 755 return (-1); 756 757 return (size); 758 } 759 760 /* 761 * Load the label information from the specified device. 762 */ 763 static void 764 ldgetdisklabel(struct ld_softc *sc) 765 { 766 const char *errstring; 767 768 ldgetdefaultlabel(sc, sc->sc_dk.dk_label); 769 770 /* Call the generic disklabel extraction routine. */ 771 errstring = readdisklabel(MAKEDISKDEV(0, device_unit(sc->sc_dv), 772 RAW_PART), ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel); 773 if (errstring != NULL) 774 printf("%s: %s\n", device_xname(sc->sc_dv), errstring); 775 776 /* In-core label now valid. */ 777 sc->sc_flags |= LDF_VLABEL; 778 } 779 780 /* 781 * Construct a ficticious label. 782 */ 783 static void 784 ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp) 785 { 786 787 memset(lp, 0, sizeof(struct disklabel)); 788 789 lp->d_secsize = sc->sc_secsize; 790 lp->d_ntracks = sc->sc_nheads; 791 lp->d_nsectors = sc->sc_nsectors; 792 lp->d_ncylinders = sc->sc_ncylinders; 793 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 794 lp->d_type = DTYPE_LD; 795 strlcpy(lp->d_typename, "unknown", sizeof(lp->d_typename)); 796 strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname)); 797 lp->d_secperunit = sc->sc_secperunit; 798 lp->d_rpm = 7200; 799 lp->d_interleave = 1; 800 lp->d_flags = 0; 801 802 lp->d_partitions[RAW_PART].p_offset = 0; 803 lp->d_partitions[RAW_PART].p_size = 804 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 805 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 806 lp->d_npartitions = RAW_PART + 1; 807 808 lp->d_magic = DISKMAGIC; 809 lp->d_magic2 = DISKMAGIC; 810 lp->d_checksum = dkcksum(lp); 811 } 812 813 /* 814 * Take a dump. 815 */ 816 static int 817 lddump(dev_t dev, daddr_t blkno, void *vav, size_t size) 818 { 819 char *va = vav; 820 struct ld_softc *sc; 821 struct disklabel *lp; 822 int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv; 823 static int dumping; 824 825 unit = DISKUNIT(dev); 826 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL) 827 return (ENXIO); 828 if ((sc->sc_flags & LDF_ENABLED) == 0) 829 return (ENODEV); 830 if (sc->sc_dump == NULL) 831 return (ENXIO); 832 833 /* Check if recursive dump; if so, punt. */ 834 if (dumping) 835 return (EFAULT); 836 dumping = 1; 837 838 /* Convert to disk sectors. Request must be a multiple of size. */ 839 part = DISKPART(dev); 840 lp = sc->sc_dk.dk_label; 841 if ((size % lp->d_secsize) != 0) 842 return (EFAULT); 843 towrt = size / lp->d_secsize; 844 blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */ 845 846 nsects = lp->d_partitions[part].p_size; 847 sectoff = lp->d_partitions[part].p_offset; 848 849 /* Check transfer bounds against partition size. */ 850 if ((blkno < 0) || ((blkno + towrt) > nsects)) 851 return (EINVAL); 852 853 /* Offset block number to start of partition. */ 854 blkno += sectoff; 855 856 /* Start dumping and return when done. */ 857 maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1; 858 while (towrt > 0) { 859 nblk = min(maxblkcnt, towrt); 860 861 if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0) 862 return (rv); 863 864 towrt -= nblk; 865 blkno += nblk; 866 va += nblk * sc->sc_secsize; 867 } 868 869 dumping = 0; 870 return (0); 871 } 872 873 /* 874 * Adjust the size of a transfer. 875 */ 876 static void 877 ldminphys(struct buf *bp) 878 { 879 struct ld_softc *sc; 880 881 sc = device_lookup_private(&ld_cd, DISKUNIT(bp->b_dev)); 882 883 if (bp->b_bcount > sc->sc_maxxfer) 884 bp->b_bcount = sc->sc_maxxfer; 885 minphys(bp); 886 } 887 888 static void 889 ld_set_properties(struct ld_softc *ld) 890 { 891 prop_dictionary_t disk_info, odisk_info, geom; 892 893 disk_info = prop_dictionary_create(); 894 895 geom = prop_dictionary_create(); 896 897 prop_dictionary_set_uint64(geom, "sectors-per-unit", 898 ld->sc_secperunit); 899 900 prop_dictionary_set_uint32(geom, "sector-size", 901 ld->sc_secsize); 902 903 prop_dictionary_set_uint16(geom, "sectors-per-track", 904 ld->sc_nsectors); 905 906 prop_dictionary_set_uint16(geom, "tracks-per-cylinder", 907 ld->sc_nheads); 908 909 prop_dictionary_set_uint64(geom, "cylinders-per-unit", 910 ld->sc_ncylinders); 911 912 prop_dictionary_set(disk_info, "geometry", geom); 913 prop_object_release(geom); 914 915 prop_dictionary_set(device_properties(ld->sc_dv), 916 "disk-info", disk_info); 917 918 /* 919 * Don't release disk_info here; we keep a reference to it. 920 * disk_detach() will release it when we go away. 921 */ 922 923 odisk_info = ld->sc_dk.dk_info; 924 ld->sc_dk.dk_info = disk_info; 925 if (odisk_info) 926 prop_object_release(odisk_info); 927 } 928 929 static void 930 ld_config_interrupts(device_t d) 931 { 932 struct ld_softc *sc = device_private(d); 933 dkwedge_discover(&sc->sc_dk); 934 } 935