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