Home | History | Annotate | Line # | Download | only in dev
ld.c revision 1.2
      1  1.2  ad /*	$NetBSD: ld.c,v 1.2 2000/12/03 13:03:30 ad Exp $	*/
      2  1.1  ad 
      3  1.1  ad /*-
      4  1.1  ad  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  1.1  ad  * All rights reserved.
      6  1.1  ad  *
      7  1.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  ad  * by Andrew Doran and Charles M. Hannum.
      9  1.1  ad  *
     10  1.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1  ad  * modification, are permitted provided that the following conditions
     12  1.1  ad  * are met:
     13  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1  ad  *    must display the following acknowledgement:
     20  1.1  ad  *        This product includes software developed by the NetBSD
     21  1.1  ad  *        Foundation, Inc. and its contributors.
     22  1.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  ad  *    contributors may be used to endorse or promote products derived
     24  1.1  ad  *    from this software without specific prior written permission.
     25  1.1  ad  *
     26  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  ad  */
     38  1.1  ad 
     39  1.1  ad /*
     40  1.1  ad  * Disk driver for use by RAID controllers.
     41  1.1  ad  */
     42  1.1  ad 
     43  1.1  ad #include "rnd.h"
     44  1.1  ad 
     45  1.1  ad #include <sys/param.h>
     46  1.1  ad #include <sys/systm.h>
     47  1.1  ad #include <sys/kernel.h>
     48  1.1  ad #include <sys/device.h>
     49  1.1  ad #include <sys/queue.h>
     50  1.1  ad #include <sys/proc.h>
     51  1.1  ad #include <sys/buf.h>
     52  1.1  ad #include <sys/endian.h>
     53  1.1  ad #include <sys/disklabel.h>
     54  1.1  ad #include <sys/disk.h>
     55  1.1  ad #include <sys/dkio.h>
     56  1.1  ad #include <sys/stat.h>
     57  1.1  ad #include <sys/lock.h>
     58  1.1  ad #include <sys/conf.h>
     59  1.1  ad #include <sys/fcntl.h>
     60  1.2  ad #include <sys/vnode.h>
     61  1.1  ad #include <sys/syslog.h>
     62  1.1  ad #if NRND > 0
     63  1.1  ad #include <sys/rnd.h>
     64  1.1  ad #endif
     65  1.1  ad 
     66  1.1  ad #include <dev/ldvar.h>
     67  1.1  ad 
     68  1.1  ad static void	ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
     69  1.1  ad static void	ldgetdisklabel(struct ld_softc *);
     70  1.1  ad static int	ldlock(struct ld_softc *);
     71  1.1  ad static void	ldminphys(struct buf *bp);
     72  1.1  ad static void	ldshutdown(void *);
     73  1.1  ad static int	ldstart(struct ld_softc *, struct buf *);
     74  1.1  ad static void	ldunlock(struct ld_softc *);
     75  1.1  ad 
     76  1.1  ad extern struct	cfdriver ld_cd;
     77  1.1  ad 
     78  1.1  ad static struct	dkdriver lddkdriver = { ldstrategy };
     79  1.1  ad static void	*ld_sdh;
     80  1.1  ad 
     81  1.1  ad void
     82  1.1  ad ldattach(struct ld_softc *sc)
     83  1.1  ad {
     84  1.1  ad 	char buf[9];
     85  1.1  ad 
     86  1.1  ad 	/* Initialise and attach the disk structure. */
     87  1.1  ad 	sc->sc_dk.dk_driver = &lddkdriver;
     88  1.1  ad 	sc->sc_dk.dk_name = sc->sc_dv.dv_xname;
     89  1.1  ad 	disk_attach(&sc->sc_dk);
     90  1.1  ad 
     91  1.1  ad 	if ((sc->sc_flags & LDF_ENABLED) == 0) {
     92  1.1  ad 		printf("%s: disabled\n", sc->sc_dv.dv_xname);
     93  1.1  ad 		return;
     94  1.1  ad 	}
     95  1.1  ad 	if (sc->sc_maxxfer > MAXPHYS)
     96  1.1  ad 		sc->sc_maxxfer = MAXPHYS;
     97  1.1  ad 
     98  1.1  ad 	format_bytes(buf, sizeof(buf), (u_int64_t)sc->sc_secperunit *
     99  1.1  ad 	    sc->sc_secsize);
    100  1.1  ad 	printf("%s: %s, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
    101  1.1  ad 	    sc->sc_dv.dv_xname, buf, sc->sc_ncylinders, sc->sc_nheads,
    102  1.1  ad 	    sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
    103  1.1  ad 
    104  1.1  ad #if NRND > 0
    105  1.1  ad 	/* Attach the device into the rnd source list. */
    106  1.1  ad 	rnd_attach_source(&sc->sc_rnd_source, sc->sc_dv.dv_xname,
    107  1.1  ad 	    RND_TYPE_DISK, 0);
    108  1.1  ad #endif
    109  1.1  ad 
    110  1.1  ad 	/* Set the `shutdownhook'. */
    111  1.1  ad 	if (ld_sdh == NULL)
    112  1.1  ad 		ld_sdh = shutdownhook_establish(ldshutdown, NULL);
    113  1.1  ad 	BUFQ_INIT(&sc->sc_bufq);
    114  1.1  ad }
    115  1.1  ad 
    116  1.2  ad void
    117  1.2  ad lddetach(struct ld_softc *sc)
    118  1.2  ad {
    119  1.2  ad 	struct buf *bp;
    120  1.2  ad 	int s, bmaj, cmaj, mn;
    121  1.2  ad 
    122  1.2  ad 	/* Wait for commands queued with the hardware to complete. */
    123  1.2  ad 	if (sc->sc_queuecnt != 0)
    124  1.2  ad 		tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0);
    125  1.2  ad 
    126  1.2  ad 	/* Locate the major numbers. */
    127  1.2  ad 	for (bmaj = 0; bmaj <= nblkdev; bmaj++)
    128  1.2  ad 		if (bdevsw[bmaj].d_open == sdopen)
    129  1.2  ad 			break;
    130  1.2  ad 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
    131  1.2  ad 		if (cdevsw[cmaj].d_open == sdopen)
    132  1.2  ad 			break;
    133  1.2  ad 
    134  1.2  ad 	/* Kill off any queued buffers. */
    135  1.2  ad 	s = splbio();
    136  1.2  ad 	while ((bp = BUFQ_FIRST(&sc->sc_bufq)) != NULL) {
    137  1.2  ad 		BUFQ_REMOVE(&sc->sc_bufq, bp);
    138  1.2  ad 		bp->b_error = EIO;
    139  1.2  ad 		bp->b_flags |= B_ERROR;
    140  1.2  ad 		bp->b_resid = bp->b_bcount;
    141  1.2  ad 		biodone(bp);
    142  1.2  ad 	}
    143  1.2  ad 	splx(s);
    144  1.2  ad 
    145  1.2  ad 	/* Nuke the vnodes for any open instances. */
    146  1.2  ad 	mn = DISKUNIT(sc->sc_dv.dv_unit);
    147  1.2  ad 	vdevgone(bmaj, mn, mn + (MAXPARTITIONS - 1), VBLK);
    148  1.2  ad 	vdevgone(cmaj, mn, mn + (MAXPARTITIONS - 1), VCHR);
    149  1.2  ad 
    150  1.2  ad 	/* Detach from the disk list. */
    151  1.2  ad 	disk_detach(&sc->sc_dk);
    152  1.2  ad 
    153  1.2  ad #if NRND > 0
    154  1.2  ad 	/* Unhook the entropy source. */
    155  1.2  ad 	rnd_detach_source(&sc->sc_rnd_source);
    156  1.2  ad #endif
    157  1.2  ad 
    158  1.2  ad 	/* Flush the device's cache. */
    159  1.2  ad 	if (sc->sc_flush != NULL)
    160  1.2  ad 		if ((*sc->sc_flush)(sc) != 0)
    161  1.2  ad 			printf("%s: unable to flush cache\n",
    162  1.2  ad 			    sc->sc_dv.dv_xname);
    163  1.2  ad }
    164  1.2  ad 
    165  1.1  ad static void
    166  1.1  ad ldshutdown(void *cookie)
    167  1.1  ad {
    168  1.1  ad 	struct ld_softc *sc;
    169  1.1  ad 	int i;
    170  1.1  ad 
    171  1.1  ad 	for (i = 0; i < ld_cd.cd_ndevs; i++) {
    172  1.1  ad 		if ((sc = device_lookup(&ld_cd, i)) == NULL)
    173  1.1  ad 			continue;
    174  1.1  ad 		if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
    175  1.1  ad 			printf("%s: unable to flush cache\n",
    176  1.1  ad 			    sc->sc_dv.dv_xname);
    177  1.1  ad 	}
    178  1.1  ad }
    179  1.1  ad 
    180  1.1  ad int
    181  1.1  ad ldopen(dev_t dev, int flags, int fmt, struct proc *p)
    182  1.1  ad {
    183  1.1  ad 	struct ld_softc *sc;
    184  1.1  ad 	int unit, part;
    185  1.1  ad 
    186  1.1  ad 	unit = DISKUNIT(dev);
    187  1.1  ad 	if ((sc = device_lookup(&ld_cd, unit))== NULL)
    188  1.1  ad 		return (ENXIO);
    189  1.1  ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    190  1.1  ad 		return (ENODEV);
    191  1.1  ad 	part = DISKPART(dev);
    192  1.1  ad 	ldlock(sc);
    193  1.1  ad 
    194  1.1  ad 	if (sc->sc_dk.dk_openmask == 0)
    195  1.1  ad 		ldgetdisklabel(sc);
    196  1.1  ad 
    197  1.1  ad 	/* Check that the partition exists. */
    198  1.1  ad 	if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
    199  1.1  ad 	    sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    200  1.1  ad 	     	ldunlock(sc);
    201  1.1  ad 		return (ENXIO);
    202  1.1  ad 	}
    203  1.1  ad 
    204  1.1  ad 	/* Ensure only one open at a time. */
    205  1.1  ad 	switch (fmt) {
    206  1.1  ad 	case S_IFCHR:
    207  1.1  ad 		sc->sc_dk.dk_copenmask |= (1 << part);
    208  1.1  ad 		break;
    209  1.1  ad 	case S_IFBLK:
    210  1.1  ad 		sc->sc_dk.dk_bopenmask |= (1 << part);
    211  1.1  ad 		break;
    212  1.1  ad 	}
    213  1.1  ad 	sc->sc_dk.dk_openmask =
    214  1.1  ad 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    215  1.1  ad 
    216  1.1  ad 	ldunlock(sc);
    217  1.1  ad 	return (0);
    218  1.1  ad }
    219  1.1  ad 
    220  1.1  ad int
    221  1.1  ad ldclose(dev_t dev, int flags, int fmt, struct proc *p)
    222  1.1  ad {
    223  1.1  ad 	struct ld_softc *sc;
    224  1.1  ad 	int part, unit;
    225  1.1  ad 
    226  1.1  ad 	unit = DISKUNIT(dev);
    227  1.1  ad 	part = DISKPART(dev);
    228  1.1  ad 	sc = device_lookup(&ld_cd, unit);
    229  1.1  ad 	ldlock(sc);
    230  1.1  ad 
    231  1.1  ad 	switch (fmt) {
    232  1.1  ad 	case S_IFCHR:
    233  1.1  ad 		sc->sc_dk.dk_copenmask &= ~(1 << part);
    234  1.1  ad 		break;
    235  1.1  ad 	case S_IFBLK:
    236  1.1  ad 		sc->sc_dk.dk_bopenmask &= ~(1 << part);
    237  1.1  ad 		break;
    238  1.1  ad 	}
    239  1.1  ad 	sc->sc_dk.dk_openmask =
    240  1.1  ad 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    241  1.1  ad 
    242  1.1  ad 	if (sc->sc_dk.dk_openmask == 0 && sc->sc_flush != NULL)
    243  1.1  ad 		if ((*sc->sc_flush)(sc) != 0)
    244  1.1  ad 			printf("%s: unable to flush cache\n",
    245  1.1  ad 			    sc->sc_dv.dv_xname);
    246  1.1  ad 
    247  1.1  ad 	ldunlock(sc);
    248  1.1  ad 	return (0);
    249  1.1  ad }
    250  1.1  ad 
    251  1.1  ad int
    252  1.1  ad ldread(dev_t dev, struct uio *uio, int ioflag)
    253  1.1  ad {
    254  1.1  ad 
    255  1.1  ad 	return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
    256  1.1  ad }
    257  1.1  ad 
    258  1.1  ad int
    259  1.1  ad ldwrite(dev_t dev, struct uio *uio, int ioflag)
    260  1.1  ad {
    261  1.1  ad 
    262  1.1  ad 	return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
    263  1.1  ad }
    264  1.1  ad 
    265  1.1  ad int
    266  1.1  ad ldioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
    267  1.1  ad {
    268  1.1  ad 	struct ld_softc *sc;
    269  1.1  ad 	int part, unit, error;
    270  1.1  ad 
    271  1.1  ad 	unit = DISKUNIT(dev);
    272  1.1  ad 	part = DISKPART(dev);
    273  1.1  ad 	sc = device_lookup(&ld_cd, unit);
    274  1.1  ad 	error = 0;
    275  1.1  ad 
    276  1.1  ad 	switch (cmd) {
    277  1.1  ad 	case DIOCGDINFO:
    278  1.1  ad 		memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
    279  1.1  ad 		return (0);
    280  1.1  ad 
    281  1.1  ad 	case DIOCGPART:
    282  1.1  ad 		((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
    283  1.1  ad 		((struct partinfo *)addr)->part =
    284  1.1  ad 		    &sc->sc_dk.dk_label->d_partitions[part];
    285  1.1  ad 		break;
    286  1.1  ad 
    287  1.1  ad 	case DIOCWDINFO:
    288  1.1  ad 	case DIOCSDINFO:
    289  1.1  ad 		if ((flag & FWRITE) == 0)
    290  1.1  ad 			return (EBADF);
    291  1.1  ad 
    292  1.1  ad 		if ((error = ldlock(sc)) != 0)
    293  1.1  ad 			return (error);
    294  1.1  ad 		sc->sc_flags |= LDF_LABELLING;
    295  1.1  ad 
    296  1.1  ad 		error = setdisklabel(sc->sc_dk.dk_label,
    297  1.1  ad 		    (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
    298  1.1  ad 		    sc->sc_dk.dk_cpulabel);
    299  1.1  ad 		if (error == 0 && cmd == DIOCWDINFO)
    300  1.1  ad 			error = writedisklabel(
    301  1.1  ad 			    MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
    302  1.1  ad 			    ldstrategy, sc->sc_dk.dk_label,
    303  1.1  ad 			    sc->sc_dk.dk_cpulabel);
    304  1.1  ad 
    305  1.1  ad 		sc->sc_flags &= ~LDF_LABELLING;
    306  1.1  ad 		ldunlock(sc);
    307  1.1  ad 		break;
    308  1.1  ad 
    309  1.1  ad 	case DIOCWLABEL:
    310  1.1  ad 		if ((flag & FWRITE) == 0)
    311  1.1  ad 			return (EBADF);
    312  1.1  ad 		if (*(int *)addr)
    313  1.1  ad 			sc->sc_flags |= LDF_WLABEL;
    314  1.1  ad 		else
    315  1.1  ad 			sc->sc_flags &= ~LDF_WLABEL;
    316  1.1  ad 		break;
    317  1.1  ad 
    318  1.1  ad 	case DIOCGDEFLABEL:
    319  1.1  ad 		ldgetdefaultlabel(sc, (struct disklabel *)addr);
    320  1.1  ad 		break;
    321  1.1  ad 
    322  1.1  ad 	default:
    323  1.1  ad 		error = ENOTTY;
    324  1.1  ad 		break;
    325  1.1  ad 	}
    326  1.1  ad 
    327  1.1  ad 	return (error);
    328  1.1  ad }
    329  1.1  ad 
    330  1.1  ad void
    331  1.1  ad ldstrategy(struct buf *bp)
    332  1.1  ad {
    333  1.1  ad 	struct ld_softc *sc;
    334  1.1  ad 	int s;
    335  1.1  ad 
    336  1.1  ad 	sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
    337  1.1  ad 
    338  1.1  ad 	s = splbio();
    339  1.1  ad 	if (sc->sc_queuecnt == sc->sc_maxqueuecnt) {
    340  1.1  ad 		BUFQ_INSERT_TAIL(&sc->sc_bufq, bp);
    341  1.1  ad 		splx(s);
    342  1.1  ad 		return;
    343  1.1  ad 	}
    344  1.1  ad 	splx(s);
    345  1.1  ad 	ldstart(sc, bp);
    346  1.1  ad }
    347  1.1  ad 
    348  1.1  ad static int
    349  1.1  ad ldstart(struct ld_softc *sc, struct buf *bp)
    350  1.1  ad {
    351  1.1  ad 	struct disklabel *lp;
    352  1.1  ad 	int part, s, rv;
    353  1.1  ad 
    354  1.2  ad 	if ((sc->sc_flags & LDF_DRAIN) != 0) {
    355  1.2  ad 		bp->b_error = EIO;
    356  1.2  ad 		bp->b_flags |= B_ERROR;
    357  1.2  ad 		bp->b_resid = bp->b_bcount;
    358  1.2  ad 		biodone(bp);
    359  1.2  ad 		return (-1);
    360  1.2  ad 	}
    361  1.2  ad 
    362  1.1  ad 	part = DISKPART(bp->b_dev);
    363  1.1  ad 	lp = sc->sc_dk.dk_label;
    364  1.1  ad 
    365  1.1  ad 	/*
    366  1.1  ad 	 * The transfer must be a whole number of blocks and the offset must
    367  1.1  ad 	 * not be negative.
    368  1.1  ad 	 */
    369  1.1  ad 	if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
    370  1.1  ad 		bp->b_flags |= B_ERROR;
    371  1.1  ad 		biodone(bp);
    372  1.1  ad 		return (-1);
    373  1.1  ad 	}
    374  1.1  ad 
    375  1.1  ad 	/*
    376  1.1  ad 	 * If it's a null transfer, return.
    377  1.1  ad 	 */
    378  1.1  ad 	if (bp->b_bcount == 0) {
    379  1.1  ad 		bp->b_resid = bp->b_bcount;
    380  1.1  ad 		biodone(bp);
    381  1.1  ad 		return (-1);
    382  1.1  ad 	}
    383  1.1  ad 
    384  1.1  ad 	/*
    385  1.1  ad 	 * Do bounds checking and adjust the transfer.  If error, process.
    386  1.1  ad 	 * If past the end of partition, just return.
    387  1.1  ad 	 */
    388  1.1  ad 	if (part != RAW_PART &&
    389  1.1  ad 	    bounds_check_with_label(bp, lp,
    390  1.1  ad 	    (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
    391  1.1  ad 		bp->b_resid = bp->b_bcount;
    392  1.1  ad 		biodone(bp);
    393  1.1  ad 		return (-1);
    394  1.1  ad 	}
    395  1.1  ad 
    396  1.1  ad 	/*
    397  1.1  ad 	 * Convert the logical block number to a physical one and put it in
    398  1.1  ad 	 * terms of the device's logical block size.
    399  1.1  ad 	 */
    400  1.1  ad 	if (lp->d_secsize >= DEV_BSIZE)
    401  1.1  ad 		bp->b_rawblkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    402  1.1  ad 	else
    403  1.1  ad 		bp->b_rawblkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    404  1.1  ad 
    405  1.1  ad 	if (bp->b_dev != RAW_PART)
    406  1.1  ad 		bp->b_rawblkno += lp->d_partitions[part].p_offset;
    407  1.1  ad 
    408  1.1  ad 	s = splbio();
    409  1.1  ad 	disk_busy(&sc->sc_dk);
    410  1.1  ad 	sc->sc_queuecnt++;
    411  1.1  ad 	splx(s);
    412  1.1  ad 
    413  1.1  ad 	if ((rv = (*sc->sc_start)(sc, bp)) != 0) {
    414  1.1  ad 		bp->b_error = rv;
    415  1.1  ad 		bp->b_flags |= B_ERROR;
    416  1.1  ad 		bp->b_resid = bp->b_bcount;
    417  1.1  ad 		s = splbio();
    418  1.1  ad 		lddone(sc, bp);
    419  1.1  ad 		splx(s);
    420  1.1  ad 	}
    421  1.1  ad 
    422  1.1  ad 	return (0);
    423  1.1  ad }
    424  1.1  ad 
    425  1.1  ad void
    426  1.1  ad lddone(struct ld_softc *sc, struct buf *bp)
    427  1.1  ad {
    428  1.1  ad 
    429  1.1  ad 	if ((bp->b_flags & B_ERROR) != 0) {
    430  1.1  ad 		diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
    431  1.1  ad 		printf("\n");
    432  1.1  ad 	}
    433  1.1  ad 
    434  1.1  ad 	disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid);
    435  1.1  ad #if NRND > 0
    436  1.1  ad 	rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
    437  1.1  ad #endif
    438  1.1  ad 	biodone(bp);
    439  1.2  ad 	if (--sc->sc_queuecnt == 0 && (sc->sc_flags & LDF_DRAIN) != 0)
    440  1.2  ad 		wakeup(&sc->sc_queuecnt);
    441  1.1  ad 
    442  1.1  ad 	while ((bp = BUFQ_FIRST(&sc->sc_bufq)) != NULL) {
    443  1.1  ad 		BUFQ_REMOVE(&sc->sc_bufq, bp);
    444  1.1  ad 		if (!ldstart(sc, bp))
    445  1.1  ad 			break;
    446  1.1  ad 	}
    447  1.1  ad }
    448  1.1  ad 
    449  1.1  ad int
    450  1.1  ad ldsize(dev_t dev)
    451  1.1  ad {
    452  1.1  ad 	struct ld_softc *sc;
    453  1.1  ad 	int part, unit, omask, size;
    454  1.1  ad 
    455  1.1  ad 	unit = DISKUNIT(dev);
    456  1.1  ad 	if ((sc = device_lookup(&ld_cd, unit)) == NULL)
    457  1.1  ad 		return (ENODEV);
    458  1.1  ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    459  1.1  ad 		return (ENODEV);
    460  1.1  ad 	part = DISKPART(dev);
    461  1.1  ad 
    462  1.1  ad 	omask = sc->sc_dk.dk_openmask & (1 << part);
    463  1.1  ad 
    464  1.1  ad 	if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
    465  1.1  ad 		return (-1);
    466  1.1  ad 	else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    467  1.1  ad 		size = -1;
    468  1.1  ad 	else
    469  1.1  ad 		size = sc->sc_dk.dk_label->d_partitions[part].p_size *
    470  1.1  ad 		    (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    471  1.1  ad 	if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
    472  1.1  ad 		return (-1);
    473  1.1  ad 
    474  1.1  ad 	return (size);
    475  1.1  ad }
    476  1.1  ad 
    477  1.1  ad /*
    478  1.1  ad  * Load the label information from the specified device.
    479  1.1  ad  */
    480  1.1  ad static void
    481  1.1  ad ldgetdisklabel(struct ld_softc *sc)
    482  1.1  ad {
    483  1.1  ad 	const char *errstring;
    484  1.1  ad 
    485  1.1  ad 	ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
    486  1.1  ad 
    487  1.1  ad 	/* Call the generic disklabel extraction routine. */
    488  1.1  ad 	errstring = readdisklabel(MAKEDISKDEV(0, sc->sc_dv.dv_unit, RAW_PART),
    489  1.1  ad 	    ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
    490  1.1  ad 	if (errstring != NULL)
    491  1.1  ad 		printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
    492  1.1  ad }
    493  1.1  ad 
    494  1.1  ad /*
    495  1.1  ad  * Construct a ficticious label.
    496  1.1  ad  */
    497  1.1  ad static void
    498  1.1  ad ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
    499  1.1  ad {
    500  1.1  ad 
    501  1.1  ad 	memset(lp, 0, sizeof(struct disklabel));
    502  1.1  ad 
    503  1.1  ad 	lp->d_secsize = sc->sc_secsize;
    504  1.1  ad 	lp->d_ntracks = sc->sc_nheads;
    505  1.1  ad 	lp->d_nsectors = sc->sc_nsectors;
    506  1.1  ad 	lp->d_ncylinders = sc->sc_ncylinders;
    507  1.1  ad 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    508  1.1  ad 	lp->d_type = DTYPE_LD;
    509  1.1  ad 	strcpy(lp->d_typename, "unknown");
    510  1.1  ad 	strcpy(lp->d_packname, "fictitious");
    511  1.1  ad 	lp->d_secperunit = sc->sc_secperunit;
    512  1.1  ad 	lp->d_rpm = 7200;
    513  1.1  ad 	lp->d_interleave = 1;
    514  1.1  ad 	lp->d_flags = 0;
    515  1.1  ad 
    516  1.1  ad 	lp->d_partitions[RAW_PART].p_offset = 0;
    517  1.1  ad 	lp->d_partitions[RAW_PART].p_size =
    518  1.1  ad 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    519  1.1  ad 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    520  1.1  ad 	lp->d_npartitions = RAW_PART + 1;
    521  1.1  ad 
    522  1.1  ad 	lp->d_magic = DISKMAGIC;
    523  1.1  ad 	lp->d_magic2 = DISKMAGIC;
    524  1.1  ad 	lp->d_checksum = dkcksum(lp);
    525  1.1  ad }
    526  1.1  ad 
    527  1.1  ad /*
    528  1.1  ad  * Wait interruptibly for an exclusive lock.
    529  1.1  ad  *
    530  1.1  ad  * XXX Several drivers do this; it should be abstracted and made MP-safe.
    531  1.1  ad  */
    532  1.1  ad static int
    533  1.1  ad ldlock(struct ld_softc *sc)
    534  1.1  ad {
    535  1.1  ad 	int error;
    536  1.1  ad 
    537  1.1  ad 	while ((sc->sc_flags & LDF_LKHELD) != 0) {
    538  1.1  ad 		sc->sc_flags |= LDF_LKWANTED;
    539  1.1  ad 		if ((error = tsleep(sc, PRIBIO | PCATCH, "ldlck", 0)) != 0)
    540  1.1  ad 			return (error);
    541  1.1  ad 	}
    542  1.1  ad 	sc->sc_flags |= LDF_LKHELD;
    543  1.1  ad 	return (0);
    544  1.1  ad }
    545  1.1  ad 
    546  1.1  ad /*
    547  1.1  ad  * Unlock and wake up any waiters.
    548  1.1  ad  */
    549  1.1  ad static void
    550  1.1  ad ldunlock(struct ld_softc *sc)
    551  1.1  ad {
    552  1.1  ad 
    553  1.1  ad 	sc->sc_flags &= ~LDF_LKHELD;
    554  1.1  ad 	if ((sc->sc_flags & LDF_LKWANTED) != 0) {
    555  1.1  ad 		sc->sc_flags &= ~LDF_LKWANTED;
    556  1.1  ad 		wakeup(sc);
    557  1.1  ad 	}
    558  1.1  ad }
    559  1.1  ad 
    560  1.1  ad /*
    561  1.1  ad  * Take a dump.
    562  1.1  ad  */
    563  1.1  ad int
    564  1.1  ad lddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
    565  1.1  ad {
    566  1.1  ad 	struct ld_softc *sc;
    567  1.1  ad 	struct disklabel *lp;
    568  1.1  ad 	int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
    569  1.1  ad 	static int dumping;
    570  1.1  ad 
    571  1.1  ad 	/* Check if recursive dump; if so, punt. */
    572  1.1  ad 	if (dumping)
    573  1.1  ad 		return (EFAULT);
    574  1.1  ad 	dumping = 1;
    575  1.1  ad 	if (sc->sc_dump == NULL)
    576  1.1  ad 		return (ENXIO);
    577  1.1  ad 
    578  1.1  ad 	unit = DISKUNIT(dev);
    579  1.1  ad 	if ((sc = device_lookup(&ld_cd, unit)) == NULL)
    580  1.1  ad 		return (ENXIO);
    581  1.1  ad 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    582  1.1  ad 		return (ENODEV);
    583  1.1  ad 	part = DISKPART(dev);
    584  1.1  ad 
    585  1.1  ad 	/* Convert to disk sectors.  Request must be a multiple of size. */
    586  1.1  ad 	lp = sc->sc_dk.dk_label;
    587  1.1  ad 	if ((size % lp->d_secsize) != 0)
    588  1.1  ad 		return (EFAULT);
    589  1.1  ad 	towrt = size / lp->d_secsize;
    590  1.1  ad 	blkno = dbtob(blkno) / lp->d_secsize;	/* blkno in DEV_BSIZE units */
    591  1.1  ad 
    592  1.1  ad 	nsects = lp->d_partitions[part].p_size;
    593  1.1  ad 	sectoff = lp->d_partitions[part].p_offset;
    594  1.1  ad 
    595  1.1  ad 	/* Check transfer bounds against partition size. */
    596  1.1  ad 	if ((blkno < 0) || ((blkno + towrt) > nsects))
    597  1.1  ad 		return (EINVAL);
    598  1.1  ad 
    599  1.1  ad 	/* Offset block number to start of partition. */
    600  1.1  ad 	blkno += sectoff;
    601  1.1  ad 
    602  1.1  ad 	/* Start dumping and return when done. */
    603  1.1  ad 	maxblkcnt = sc->sc_maxxfer / sc->sc_secsize;
    604  1.1  ad 	while (towrt > 0) {
    605  1.1  ad 		nblk = max(maxblkcnt, towrt);
    606  1.1  ad 
    607  1.1  ad 		if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
    608  1.1  ad 			return (rv);
    609  1.1  ad 
    610  1.1  ad 		towrt -= nblk;
    611  1.1  ad 		blkno += nblk;
    612  1.1  ad 		va += nblk * sc->sc_secsize;
    613  1.1  ad 	}
    614  1.1  ad 
    615  1.1  ad 	dumping = 0;
    616  1.1  ad 	return (0);
    617  1.1  ad }
    618  1.1  ad 
    619  1.1  ad /*
    620  1.1  ad  * Adjust the size of a transfer.
    621  1.1  ad  */
    622  1.1  ad static void
    623  1.1  ad ldminphys(struct buf *bp)
    624  1.1  ad {
    625  1.1  ad 	struct ld_softc *sc;
    626  1.1  ad 
    627  1.1  ad 	sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
    628  1.1  ad 
    629  1.1  ad 	if (bp->b_bcount > sc->sc_maxxfer)
    630  1.1  ad 		bp->b_bcount = sc->sc_maxxfer;
    631  1.1  ad 	minphys(bp);
    632  1.1  ad }
    633