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