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