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