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