Home | History | Annotate | Line # | Download | only in dev
ld.c revision 1.14
      1 /*	$NetBSD: ld.c,v 1.14 2002/07/20 11:28:07 hannken 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.14 2002/07/20 11:28:07 hannken 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/endian.h>
     56 #include <sys/disklabel.h>
     57 #include <sys/disk.h>
     58 #include <sys/dkio.h>
     59 #include <sys/stat.h>
     60 #include <sys/lock.h>
     61 #include <sys/conf.h>
     62 #include <sys/fcntl.h>
     63 #include <sys/vnode.h>
     64 #include <sys/syslog.h>
     65 #if NRND > 0
     66 #include <sys/rnd.h>
     67 #endif
     68 
     69 #include <dev/ldvar.h>
     70 
     71 static void	ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
     72 static void	ldgetdisklabel(struct ld_softc *);
     73 static int	ldlock(struct ld_softc *);
     74 static void	ldminphys(struct buf *bp);
     75 static void	ldshutdown(void *);
     76 static int	ldstart(struct ld_softc *, struct buf *);
     77 static void	ldunlock(struct ld_softc *);
     78 
     79 extern struct	cfdriver ld_cd;
     80 
     81 static struct	dkdriver lddkdriver = { ldstrategy };
     82 static void	*ld_sdh;
     83 
     84 void
     85 ldattach(struct ld_softc *sc)
     86 {
     87 	char buf[9];
     88 
     89 	if ((sc->sc_flags & LDF_ENABLED) == 0) {
     90 		printf("%s: disabled\n", sc->sc_dv.dv_xname);
     91 		return;
     92 	}
     93 
     94 	/* Initialise and attach the disk structure. */
     95 	sc->sc_dk.dk_driver = &lddkdriver;
     96 	sc->sc_dk.dk_name = sc->sc_dv.dv_xname;
     97 	disk_attach(&sc->sc_dk);
     98 
     99 	if (sc->sc_maxxfer > MAXPHYS)
    100 		sc->sc_maxxfer = MAXPHYS;
    101 
    102 	/* Build synthetic geometry. */
    103 	if (sc->sc_secperunit <= 528 * 2048)		/* 528MB */
    104 		sc->sc_nheads = 16;
    105 	else if (sc->sc_secperunit <= 1024 * 2048)	/* 1GB */
    106 		sc->sc_nheads = 32;
    107 	else if (sc->sc_secperunit <= 21504 * 2048)	/* 21GB */
    108 		sc->sc_nheads = 64;
    109 	else if (sc->sc_secperunit <= 43008 * 2048)	/* 42GB */
    110 		sc->sc_nheads = 128;
    111 	else
    112 		sc->sc_nheads = 255;
    113 
    114 	sc->sc_nsectors = 63;
    115 	sc->sc_ncylinders = sc->sc_secperunit /
    116 	    (sc->sc_nheads * sc->sc_nsectors);
    117 
    118 	format_bytes(buf, sizeof(buf), (u_int64_t)sc->sc_secperunit *
    119 	    sc->sc_secsize);
    120 	printf("%s: %s, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
    121 	    sc->sc_dv.dv_xname, buf, sc->sc_ncylinders, sc->sc_nheads,
    122 	    sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
    123 
    124 #if NRND > 0
    125 	/* Attach the device into the rnd source list. */
    126 	rnd_attach_source(&sc->sc_rnd_source, sc->sc_dv.dv_xname,
    127 	    RND_TYPE_DISK, 0);
    128 #endif
    129 
    130 	/* Set the `shutdownhook'. */
    131 	if (ld_sdh == NULL)
    132 		ld_sdh = shutdownhook_establish(ldshutdown, NULL);
    133 	bufq_init(&sc->sc_bufq, BUFQ_FCFS);
    134 }
    135 
    136 int
    137 ldadjqparam(struct ld_softc *sc, int max)
    138 {
    139 	int s, rv;
    140 
    141 	s = splbio();
    142 	sc->sc_maxqueuecnt = max;
    143 	if (sc->sc_queuecnt > max) {
    144 		sc->sc_flags |= LDF_DRAIN;
    145 		rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 30 * hz);
    146 		sc->sc_flags &= ~LDF_DRAIN;
    147 	} else
    148 		rv = 0;
    149 	splx(s);
    150 
    151 	return (rv);
    152 }
    153 
    154 int
    155 ldbegindetach(struct ld_softc *sc, int flags)
    156 {
    157 	int s, rv;
    158 
    159 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    160 		return (0);
    161 
    162 	if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0)
    163 		return (EBUSY);
    164 
    165 	s = splbio();
    166 	sc->sc_flags |= LDF_DETACH;
    167 	rv = ldadjqparam(sc, 0);
    168 	splx(s);
    169 
    170 	return (rv);
    171 }
    172 
    173 void
    174 ldenddetach(struct ld_softc *sc)
    175 {
    176 	struct buf *bp;
    177 	int s, bmaj, cmaj, i, mn;
    178 
    179 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    180 		return;
    181 
    182 	/* Wait for commands queued with the hardware to complete. */
    183 	if (sc->sc_queuecnt != 0)
    184 		if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz))
    185 			printf("%s: not drained\n", sc->sc_dv.dv_xname);
    186 
    187 	/* Locate the major numbers. */
    188 	for (bmaj = 0; bmaj <= nblkdev; bmaj++)
    189 		if (bdevsw[bmaj].d_open == ldopen)
    190 			break;
    191 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
    192 		if (cdevsw[cmaj].d_open == ldopen)
    193 			break;
    194 
    195 	/* Kill off any queued buffers. */
    196 	s = splbio();
    197 	while ((bp = BUFQ_GET(&sc->sc_bufq)) != NULL) {
    198 		bp->b_error = EIO;
    199 		bp->b_flags |= B_ERROR;
    200 		bp->b_resid = bp->b_bcount;
    201 		biodone(bp);
    202 	}
    203 	splx(s);
    204 
    205 	/* Nuke the vnodes for any open instances. */
    206 	for (i = 0; i < MAXPARTITIONS; i++) {
    207 		mn = DISKMINOR(sc->sc_dv.dv_unit, i);
    208 		vdevgone(bmaj, mn, mn, VBLK);
    209 		vdevgone(cmaj, mn, mn, VCHR);
    210 	}
    211 
    212 	/* Detach from the disk list. */
    213 	disk_detach(&sc->sc_dk);
    214 
    215 #if NRND > 0
    216 	/* Unhook the entropy source. */
    217 	rnd_detach_source(&sc->sc_rnd_source);
    218 #endif
    219 
    220 	/* Flush the device's cache. */
    221 	if (sc->sc_flush != NULL)
    222 		if ((*sc->sc_flush)(sc) != 0)
    223 			printf("%s: unable to flush cache\n",
    224 			    sc->sc_dv.dv_xname);
    225 }
    226 
    227 /* ARGSUSED */
    228 static void
    229 ldshutdown(void *cookie)
    230 {
    231 	struct ld_softc *sc;
    232 	int i;
    233 
    234 	for (i = 0; i < ld_cd.cd_ndevs; i++) {
    235 		if ((sc = device_lookup(&ld_cd, i)) == NULL)
    236 			continue;
    237 		if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
    238 			printf("%s: unable to flush cache\n",
    239 			    sc->sc_dv.dv_xname);
    240 	}
    241 }
    242 
    243 /* ARGSUSED */
    244 int
    245 ldopen(dev_t dev, int flags, int fmt, struct proc *p)
    246 {
    247 	struct ld_softc *sc;
    248 	int unit, part;
    249 
    250 	unit = DISKUNIT(dev);
    251 	if ((sc = device_lookup(&ld_cd, unit))== NULL)
    252 		return (ENXIO);
    253 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    254 		return (ENODEV);
    255 	part = DISKPART(dev);
    256 	ldlock(sc);
    257 
    258 	if (sc->sc_dk.dk_openmask == 0)
    259 		ldgetdisklabel(sc);
    260 
    261 	/* Check that the partition exists. */
    262 	if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
    263 	    sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    264 	     	ldunlock(sc);
    265 		return (ENXIO);
    266 	}
    267 
    268 	/* Ensure only one open at a time. */
    269 	switch (fmt) {
    270 	case S_IFCHR:
    271 		sc->sc_dk.dk_copenmask |= (1 << part);
    272 		break;
    273 	case S_IFBLK:
    274 		sc->sc_dk.dk_bopenmask |= (1 << part);
    275 		break;
    276 	}
    277 	sc->sc_dk.dk_openmask =
    278 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    279 
    280 	ldunlock(sc);
    281 	return (0);
    282 }
    283 
    284 /* ARGSUSED */
    285 int
    286 ldclose(dev_t dev, int flags, int fmt, struct proc *p)
    287 {
    288 	struct ld_softc *sc;
    289 	int part, unit;
    290 
    291 	unit = DISKUNIT(dev);
    292 	part = DISKPART(dev);
    293 	sc = device_lookup(&ld_cd, unit);
    294 	ldlock(sc);
    295 
    296 	switch (fmt) {
    297 	case S_IFCHR:
    298 		sc->sc_dk.dk_copenmask &= ~(1 << part);
    299 		break;
    300 	case S_IFBLK:
    301 		sc->sc_dk.dk_bopenmask &= ~(1 << part);
    302 		break;
    303 	}
    304 	sc->sc_dk.dk_openmask =
    305 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    306 
    307 	if (sc->sc_dk.dk_openmask == 0 && sc->sc_flush != NULL)
    308 		if ((*sc->sc_flush)(sc) != 0)
    309 			printf("%s: unable to flush cache\n",
    310 			    sc->sc_dv.dv_xname);
    311 
    312 	ldunlock(sc);
    313 	return (0);
    314 }
    315 
    316 /* ARGSUSED */
    317 int
    318 ldread(dev_t dev, struct uio *uio, int ioflag)
    319 {
    320 
    321 	return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
    322 }
    323 
    324 /* ARGSUSED */
    325 int
    326 ldwrite(dev_t dev, struct uio *uio, int ioflag)
    327 {
    328 
    329 	return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
    330 }
    331 
    332 /* ARGSUSED */
    333 int
    334 ldioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
    335 {
    336 	struct ld_softc *sc;
    337 	int part, unit, error;
    338 #ifdef __HAVE_OLD_DISKLABEL
    339 	struct disklabel newlabel;
    340 #endif
    341 	struct disklabel *lp;
    342 
    343 	unit = DISKUNIT(dev);
    344 	part = DISKPART(dev);
    345 	sc = device_lookup(&ld_cd, unit);
    346 	error = 0;
    347 
    348 	switch (cmd) {
    349 	case DIOCGDINFO:
    350 		memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
    351 		return (0);
    352 
    353 #ifdef __HAVE_OLD_DISKLABEL
    354 	case ODIOCGDINFO:
    355 		newlabel = *(sc->sc_dk.dk_label);
    356 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    357 			return ENOTTY;
    358 		memcpy(addr, &newlabel, sizeof(struct olddisklabel));
    359 		return (0);
    360 #endif
    361 
    362 	case DIOCGPART:
    363 		((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
    364 		((struct partinfo *)addr)->part =
    365 		    &sc->sc_dk.dk_label->d_partitions[part];
    366 		break;
    367 
    368 	case DIOCWDINFO:
    369 	case DIOCSDINFO:
    370 #ifdef __HAVE_OLD_DISKLABEL
    371 	case ODIOCWDINFO:
    372 	case ODIOCSDINFO:
    373 
    374 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    375 			memset(&newlabel, 0, sizeof newlabel);
    376 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
    377 			lp = &newlabel;
    378 		} else
    379 #endif
    380 		lp = (struct disklabel *)addr;
    381 
    382 		if ((flag & FWRITE) == 0)
    383 			return (EBADF);
    384 
    385 		if ((error = ldlock(sc)) != 0)
    386 			return (error);
    387 		sc->sc_flags |= LDF_LABELLING;
    388 
    389 		error = setdisklabel(sc->sc_dk.dk_label,
    390 		    lp, /*sc->sc_dk.dk_openmask : */0,
    391 		    sc->sc_dk.dk_cpulabel);
    392 		if (error == 0 && (cmd == DIOCWDINFO
    393 #ifdef __HAVE_OLD_DISKLABEL
    394 		    || cmd == ODIOCWDINFO
    395 #endif
    396 		    ))
    397 			error = writedisklabel(
    398 			    MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
    399 			    ldstrategy, sc->sc_dk.dk_label,
    400 			    sc->sc_dk.dk_cpulabel);
    401 
    402 		sc->sc_flags &= ~LDF_LABELLING;
    403 		ldunlock(sc);
    404 		break;
    405 
    406 	case DIOCWLABEL:
    407 		if ((flag & FWRITE) == 0)
    408 			return (EBADF);
    409 		if (*(int *)addr)
    410 			sc->sc_flags |= LDF_WLABEL;
    411 		else
    412 			sc->sc_flags &= ~LDF_WLABEL;
    413 		break;
    414 
    415 	case DIOCGDEFLABEL:
    416 		ldgetdefaultlabel(sc, (struct disklabel *)addr);
    417 		break;
    418 
    419 #ifdef __HAVE_OLD_DISKLABEL
    420 	case ODIOCGDEFLABEL:
    421 		ldgetdefaultlabel(sc, &newlabel);
    422 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    423 			return ENOTTY;
    424 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
    425 		break;
    426 #endif
    427 
    428 	default:
    429 		error = ENOTTY;
    430 		break;
    431 	}
    432 
    433 	return (error);
    434 }
    435 
    436 void
    437 ldstrategy(struct buf *bp)
    438 {
    439 	struct ld_softc *sc;
    440 	int s;
    441 
    442 	sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
    443 
    444 	s = splbio();
    445 	if (sc->sc_queuecnt >= sc->sc_maxqueuecnt) {
    446 		BUFQ_PUT(&sc->sc_bufq, bp);
    447 		splx(s);
    448 		return;
    449 	}
    450 	splx(s);
    451 	ldstart(sc, bp);
    452 }
    453 
    454 static int
    455 ldstart(struct ld_softc *sc, struct buf *bp)
    456 {
    457 	struct disklabel *lp;
    458 	int part, s, rv;
    459 
    460 	if ((sc->sc_flags & LDF_DETACH) != 0) {
    461 		bp->b_error = EIO;
    462 		bp->b_flags |= B_ERROR;
    463 		bp->b_resid = bp->b_bcount;
    464 		biodone(bp);
    465 		return (-1);
    466 	}
    467 
    468 	part = DISKPART(bp->b_dev);
    469 	lp = sc->sc_dk.dk_label;
    470 
    471 	/*
    472 	 * The transfer must be a whole number of blocks and the offset must
    473 	 * not be negative.
    474 	 */
    475 	if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
    476 		bp->b_flags |= B_ERROR;
    477 		biodone(bp);
    478 		return (-1);
    479 	}
    480 
    481 	/*
    482 	 * If it's a null transfer, return.
    483 	 */
    484 	if (bp->b_bcount == 0) {
    485 		bp->b_resid = bp->b_bcount;
    486 		biodone(bp);
    487 		return (-1);
    488 	}
    489 
    490 	/*
    491 	 * Do bounds checking and adjust the transfer.  If error, process.
    492 	 * If past the end of partition, just return.
    493 	 */
    494 	if (part != RAW_PART &&
    495 	    bounds_check_with_label(bp, lp,
    496 	    (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
    497 		bp->b_resid = bp->b_bcount;
    498 		biodone(bp);
    499 		return (-1);
    500 	}
    501 
    502 	/*
    503 	 * Convert the logical block number to a physical one and put it in
    504 	 * terms of the device's logical block size.
    505 	 */
    506 	if (lp->d_secsize >= DEV_BSIZE)
    507 		bp->b_rawblkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    508 	else
    509 		bp->b_rawblkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    510 
    511 	if (part != RAW_PART)
    512 		bp->b_rawblkno += lp->d_partitions[part].p_offset;
    513 
    514 	s = splbio();
    515 	disk_busy(&sc->sc_dk);
    516 	sc->sc_queuecnt++;
    517 	splx(s);
    518 
    519 	if ((rv = (*sc->sc_start)(sc, bp)) != 0) {
    520 		bp->b_error = rv;
    521 		bp->b_flags |= B_ERROR;
    522 		bp->b_resid = bp->b_bcount;
    523 		s = splbio();
    524 		lddone(sc, bp);
    525 		splx(s);
    526 	}
    527 
    528 	return (0);
    529 }
    530 
    531 void
    532 lddone(struct ld_softc *sc, struct buf *bp)
    533 {
    534 
    535 	if ((bp->b_flags & B_ERROR) != 0) {
    536 		diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
    537 		printf("\n");
    538 	}
    539 
    540 	disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid);
    541 #if NRND > 0
    542 	rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
    543 #endif
    544 	biodone(bp);
    545 
    546 	if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
    547 		if ((sc->sc_flags & LDF_DRAIN) != 0)
    548 			wakeup(&sc->sc_queuecnt);
    549 		while ((bp = BUFQ_GET(&sc->sc_bufq)) != NULL) {
    550 			if (!ldstart(sc, bp))
    551 				break;
    552 		}
    553 	}
    554 }
    555 
    556 int
    557 ldsize(dev_t dev)
    558 {
    559 	struct ld_softc *sc;
    560 	int part, unit, omask, size;
    561 
    562 	unit = DISKUNIT(dev);
    563 	if ((sc = device_lookup(&ld_cd, unit)) == NULL)
    564 		return (ENODEV);
    565 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    566 		return (ENODEV);
    567 	part = DISKPART(dev);
    568 
    569 	omask = sc->sc_dk.dk_openmask & (1 << part);
    570 
    571 	if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
    572 		return (-1);
    573 	else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    574 		size = -1;
    575 	else
    576 		size = sc->sc_dk.dk_label->d_partitions[part].p_size *
    577 		    (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    578 	if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
    579 		return (-1);
    580 
    581 	return (size);
    582 }
    583 
    584 /*
    585  * Load the label information from the specified device.
    586  */
    587 static void
    588 ldgetdisklabel(struct ld_softc *sc)
    589 {
    590 	const char *errstring;
    591 
    592 	ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
    593 
    594 	/* Call the generic disklabel extraction routine. */
    595 	errstring = readdisklabel(MAKEDISKDEV(0, sc->sc_dv.dv_unit, RAW_PART),
    596 	    ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
    597 	if (errstring != NULL)
    598 		printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
    599 }
    600 
    601 /*
    602  * Construct a ficticious label.
    603  */
    604 static void
    605 ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
    606 {
    607 
    608 	memset(lp, 0, sizeof(struct disklabel));
    609 
    610 	lp->d_secsize = sc->sc_secsize;
    611 	lp->d_ntracks = sc->sc_nheads;
    612 	lp->d_nsectors = sc->sc_nsectors;
    613 	lp->d_ncylinders = sc->sc_ncylinders;
    614 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    615 	lp->d_type = DTYPE_LD;
    616 	strcpy(lp->d_typename, "unknown");
    617 	strcpy(lp->d_packname, "fictitious");
    618 	lp->d_secperunit = sc->sc_secperunit;
    619 	lp->d_rpm = 7200;
    620 	lp->d_interleave = 1;
    621 	lp->d_flags = 0;
    622 
    623 	lp->d_partitions[RAW_PART].p_offset = 0;
    624 	lp->d_partitions[RAW_PART].p_size =
    625 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    626 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    627 	lp->d_npartitions = RAW_PART + 1;
    628 
    629 	lp->d_magic = DISKMAGIC;
    630 	lp->d_magic2 = DISKMAGIC;
    631 	lp->d_checksum = dkcksum(lp);
    632 }
    633 
    634 /*
    635  * Wait interruptibly for an exclusive lock.
    636  *
    637  * XXX Several drivers do this; it should be abstracted and made MP-safe.
    638  */
    639 static int
    640 ldlock(struct ld_softc *sc)
    641 {
    642 	int error;
    643 
    644 	while ((sc->sc_flags & LDF_LKHELD) != 0) {
    645 		sc->sc_flags |= LDF_LKWANTED;
    646 		if ((error = tsleep(sc, PRIBIO | PCATCH, "ldlck", 0)) != 0)
    647 			return (error);
    648 	}
    649 	sc->sc_flags |= LDF_LKHELD;
    650 	return (0);
    651 }
    652 
    653 /*
    654  * Unlock and wake up any waiters.
    655  */
    656 static void
    657 ldunlock(struct ld_softc *sc)
    658 {
    659 
    660 	sc->sc_flags &= ~LDF_LKHELD;
    661 	if ((sc->sc_flags & LDF_LKWANTED) != 0) {
    662 		sc->sc_flags &= ~LDF_LKWANTED;
    663 		wakeup(sc);
    664 	}
    665 }
    666 
    667 /*
    668  * Take a dump.
    669  */
    670 int
    671 lddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
    672 {
    673 	struct ld_softc *sc;
    674 	struct disklabel *lp;
    675 	int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
    676 	static int dumping;
    677 
    678 	unit = DISKUNIT(dev);
    679 	if ((sc = device_lookup(&ld_cd, unit)) == NULL)
    680 		return (ENXIO);
    681 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    682 		return (ENODEV);
    683 	if (sc->sc_dump == NULL)
    684 		return (ENXIO);
    685 
    686 	/* Check if recursive dump; if so, punt. */
    687 	if (dumping)
    688 		return (EFAULT);
    689 	dumping = 1;
    690 
    691 	/* Convert to disk sectors.  Request must be a multiple of size. */
    692 	part = DISKPART(dev);
    693 	lp = sc->sc_dk.dk_label;
    694 	if ((size % lp->d_secsize) != 0)
    695 		return (EFAULT);
    696 	towrt = size / lp->d_secsize;
    697 	blkno = dbtob(blkno) / lp->d_secsize;	/* blkno in DEV_BSIZE units */
    698 
    699 	nsects = lp->d_partitions[part].p_size;
    700 	sectoff = lp->d_partitions[part].p_offset;
    701 
    702 	/* Check transfer bounds against partition size. */
    703 	if ((blkno < 0) || ((blkno + towrt) > nsects))
    704 		return (EINVAL);
    705 
    706 	/* Offset block number to start of partition. */
    707 	blkno += sectoff;
    708 
    709 	/* Start dumping and return when done. */
    710 	maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
    711 	while (towrt > 0) {
    712 		nblk = min(maxblkcnt, towrt);
    713 
    714 		if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
    715 			return (rv);
    716 
    717 		towrt -= nblk;
    718 		blkno += nblk;
    719 		va += nblk * sc->sc_secsize;
    720 	}
    721 
    722 	dumping = 0;
    723 	return (0);
    724 }
    725 
    726 /*
    727  * Adjust the size of a transfer.
    728  */
    729 static void
    730 ldminphys(struct buf *bp)
    731 {
    732 	struct ld_softc *sc;
    733 
    734 	sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
    735 
    736 	if (bp->b_bcount > sc->sc_maxxfer)
    737 		bp->b_bcount = sc->sc_maxxfer;
    738 	minphys(bp);
    739 }
    740