2001-01-04 00:01:28 +03:00
|
|
|
/* $NetBSD: ld.c,v 1.3 2001/01/03 21:01:28 ad Exp $ */
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Andrew Doran and Charles M. Hannum.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2000-11-08 22:20:33 +03:00
|
|
|
* Disk driver for use by RAID controllers.
|
2000-10-19 18:06:02 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rnd.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/dkio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/fcntl.h>
|
2000-12-03 16:03:30 +03:00
|
|
|
#include <sys/vnode.h>
|
2000-10-19 18:06:02 +04:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
#if NRND > 0
|
|
|
|
#include <sys/rnd.h>
|
|
|
|
#endif
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
#include <dev/ldvar.h>
|
2000-10-19 18:06:02 +04:00
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
|
|
|
|
static void ldgetdisklabel(struct ld_softc *);
|
|
|
|
static int ldlock(struct ld_softc *);
|
|
|
|
static void ldminphys(struct buf *bp);
|
|
|
|
static void ldshutdown(void *);
|
|
|
|
static int ldstart(struct ld_softc *, struct buf *);
|
|
|
|
static void ldunlock(struct ld_softc *);
|
2000-10-19 18:06:02 +04:00
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
extern struct cfdriver ld_cd;
|
2000-10-19 18:06:02 +04:00
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
static struct dkdriver lddkdriver = { ldstrategy };
|
|
|
|
static void *ld_sdh;
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
void
|
2000-11-26 20:44:02 +03:00
|
|
|
ldattach(struct ld_softc *sc)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
|
|
|
char buf[9];
|
|
|
|
|
|
|
|
/* Initialise and attach the disk structure. */
|
2000-11-26 20:44:02 +03:00
|
|
|
sc->sc_dk.dk_driver = &lddkdriver;
|
2000-10-19 18:06:02 +04:00
|
|
|
sc->sc_dk.dk_name = sc->sc_dv.dv_xname;
|
|
|
|
disk_attach(&sc->sc_dk);
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
if ((sc->sc_flags & LDF_ENABLED) == 0) {
|
2000-10-19 18:06:02 +04:00
|
|
|
printf("%s: disabled\n", sc->sc_dv.dv_xname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sc->sc_maxxfer > MAXPHYS)
|
|
|
|
sc->sc_maxxfer = MAXPHYS;
|
|
|
|
|
|
|
|
format_bytes(buf, sizeof(buf), (u_int64_t)sc->sc_secperunit *
|
|
|
|
sc->sc_secsize);
|
|
|
|
printf("%s: %s, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
|
|
|
|
sc->sc_dv.dv_xname, buf, sc->sc_ncylinders, sc->sc_nheads,
|
|
|
|
sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
|
|
|
|
|
|
|
|
#if NRND > 0
|
|
|
|
/* Attach the device into the rnd source list. */
|
|
|
|
rnd_attach_source(&sc->sc_rnd_source, sc->sc_dv.dv_xname,
|
|
|
|
RND_TYPE_DISK, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set the `shutdownhook'. */
|
2000-11-26 20:44:02 +03:00
|
|
|
if (ld_sdh == NULL)
|
|
|
|
ld_sdh = shutdownhook_establish(ldshutdown, NULL);
|
2000-11-08 22:20:33 +03:00
|
|
|
BUFQ_INIT(&sc->sc_bufq);
|
2000-10-19 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
2001-01-04 00:01:28 +03:00
|
|
|
int
|
|
|
|
lddrain(struct ld_softc *sc, int flags)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0)
|
|
|
|
return (EBUSY);
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
sc->sc_flags |= LDF_DRAIN;
|
|
|
|
splx(s);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-12-03 16:03:30 +03:00
|
|
|
void
|
|
|
|
lddetach(struct ld_softc *sc)
|
|
|
|
{
|
|
|
|
struct buf *bp;
|
|
|
|
int s, bmaj, cmaj, mn;
|
|
|
|
|
|
|
|
/* Wait for commands queued with the hardware to complete. */
|
|
|
|
if (sc->sc_queuecnt != 0)
|
2001-01-04 00:01:28 +03:00
|
|
|
tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 30 * hz);
|
2000-12-03 16:03:30 +03:00
|
|
|
|
|
|
|
/* Locate the major numbers. */
|
|
|
|
for (bmaj = 0; bmaj <= nblkdev; bmaj++)
|
|
|
|
if (bdevsw[bmaj].d_open == sdopen)
|
|
|
|
break;
|
|
|
|
for (cmaj = 0; cmaj <= nchrdev; cmaj++)
|
|
|
|
if (cdevsw[cmaj].d_open == sdopen)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Kill off any queued buffers. */
|
|
|
|
s = splbio();
|
|
|
|
while ((bp = BUFQ_FIRST(&sc->sc_bufq)) != NULL) {
|
|
|
|
BUFQ_REMOVE(&sc->sc_bufq, bp);
|
|
|
|
bp->b_error = EIO;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/* Nuke the vnodes for any open instances. */
|
|
|
|
mn = DISKUNIT(sc->sc_dv.dv_unit);
|
|
|
|
vdevgone(bmaj, mn, mn + (MAXPARTITIONS - 1), VBLK);
|
|
|
|
vdevgone(cmaj, mn, mn + (MAXPARTITIONS - 1), VCHR);
|
|
|
|
|
|
|
|
/* Detach from the disk list. */
|
|
|
|
disk_detach(&sc->sc_dk);
|
|
|
|
|
|
|
|
#if NRND > 0
|
|
|
|
/* Unhook the entropy source. */
|
|
|
|
rnd_detach_source(&sc->sc_rnd_source);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Flush the device's cache. */
|
|
|
|
if (sc->sc_flush != NULL)
|
|
|
|
if ((*sc->sc_flush)(sc) != 0)
|
|
|
|
printf("%s: unable to flush cache\n",
|
|
|
|
sc->sc_dv.dv_xname);
|
|
|
|
}
|
|
|
|
|
2000-10-19 18:06:02 +04:00
|
|
|
static void
|
2000-11-26 20:44:02 +03:00
|
|
|
ldshutdown(void *cookie)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
2000-11-26 20:44:02 +03:00
|
|
|
struct ld_softc *sc;
|
2000-10-19 18:06:02 +04:00
|
|
|
int i;
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
for (i = 0; i < ld_cd.cd_ndevs; i++) {
|
|
|
|
if ((sc = device_lookup(&ld_cd, i)) == NULL)
|
2000-10-19 18:06:02 +04:00
|
|
|
continue;
|
|
|
|
if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
|
|
|
|
printf("%s: unable to flush cache\n",
|
|
|
|
sc->sc_dv.dv_xname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-11-26 20:44:02 +03:00
|
|
|
ldopen(dev_t dev, int flags, int fmt, struct proc *p)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
2000-11-26 20:44:02 +03:00
|
|
|
struct ld_softc *sc;
|
2000-10-19 18:06:02 +04:00
|
|
|
int unit, part;
|
|
|
|
|
|
|
|
unit = DISKUNIT(dev);
|
2000-11-26 20:44:02 +03:00
|
|
|
if ((sc = device_lookup(&ld_cd, unit))== NULL)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (ENXIO);
|
2000-11-26 20:44:02 +03:00
|
|
|
if ((sc->sc_flags & LDF_ENABLED) == 0)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (ENODEV);
|
|
|
|
part = DISKPART(dev);
|
2000-11-26 20:44:02 +03:00
|
|
|
ldlock(sc);
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
if (sc->sc_dk.dk_openmask == 0)
|
2000-11-26 20:44:02 +03:00
|
|
|
ldgetdisklabel(sc);
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
/* Check that the partition exists. */
|
|
|
|
if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
|
|
|
|
sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
|
2000-11-26 20:44:02 +03:00
|
|
|
ldunlock(sc);
|
2000-10-19 18:06:02 +04:00
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure only one open at a time. */
|
|
|
|
switch (fmt) {
|
|
|
|
case S_IFCHR:
|
|
|
|
sc->sc_dk.dk_copenmask |= (1 << part);
|
|
|
|
break;
|
|
|
|
case S_IFBLK:
|
|
|
|
sc->sc_dk.dk_bopenmask |= (1 << part);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sc->sc_dk.dk_openmask =
|
|
|
|
sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
ldunlock(sc);
|
2000-10-19 18:06:02 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-11-26 20:44:02 +03:00
|
|
|
ldclose(dev_t dev, int flags, int fmt, struct proc *p)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
2000-11-26 20:44:02 +03:00
|
|
|
struct ld_softc *sc;
|
2000-10-19 18:06:02 +04:00
|
|
|
int part, unit;
|
|
|
|
|
|
|
|
unit = DISKUNIT(dev);
|
|
|
|
part = DISKPART(dev);
|
2000-11-26 20:44:02 +03:00
|
|
|
sc = device_lookup(&ld_cd, unit);
|
|
|
|
ldlock(sc);
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
switch (fmt) {
|
|
|
|
case S_IFCHR:
|
|
|
|
sc->sc_dk.dk_copenmask &= ~(1 << part);
|
|
|
|
break;
|
|
|
|
case S_IFBLK:
|
|
|
|
sc->sc_dk.dk_bopenmask &= ~(1 << part);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sc->sc_dk.dk_openmask =
|
|
|
|
sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
|
|
|
|
|
|
|
|
if (sc->sc_dk.dk_openmask == 0 && sc->sc_flush != NULL)
|
|
|
|
if ((*sc->sc_flush)(sc) != 0)
|
|
|
|
printf("%s: unable to flush cache\n",
|
|
|
|
sc->sc_dv.dv_xname);
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
ldunlock(sc);
|
2000-10-19 18:06:02 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-11-26 20:44:02 +03:00
|
|
|
ldread(dev_t dev, struct uio *uio, int ioflag)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
|
2000-10-19 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-11-26 20:44:02 +03:00
|
|
|
ldwrite(dev_t dev, struct uio *uio, int ioflag)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
|
2000-10-19 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-11-26 20:44:02 +03:00
|
|
|
ldioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
2000-11-26 20:44:02 +03:00
|
|
|
struct ld_softc *sc;
|
2000-10-19 18:06:02 +04:00
|
|
|
int part, unit, error;
|
|
|
|
|
|
|
|
unit = DISKUNIT(dev);
|
|
|
|
part = DISKPART(dev);
|
2000-11-26 20:44:02 +03:00
|
|
|
sc = device_lookup(&ld_cd, unit);
|
2000-10-19 18:06:02 +04:00
|
|
|
error = 0;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case DIOCGDINFO:
|
|
|
|
memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
case DIOCGPART:
|
|
|
|
((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
|
|
|
|
((struct partinfo *)addr)->part =
|
|
|
|
&sc->sc_dk.dk_label->d_partitions[part];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIOCWDINFO:
|
|
|
|
case DIOCSDINFO:
|
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
return (EBADF);
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
if ((error = ldlock(sc)) != 0)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (error);
|
2000-11-26 20:44:02 +03:00
|
|
|
sc->sc_flags |= LDF_LABELLING;
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
error = setdisklabel(sc->sc_dk.dk_label,
|
|
|
|
(struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
|
|
|
|
sc->sc_dk.dk_cpulabel);
|
|
|
|
if (error == 0 && cmd == DIOCWDINFO)
|
|
|
|
error = writedisklabel(
|
|
|
|
MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
|
2000-11-26 20:44:02 +03:00
|
|
|
ldstrategy, sc->sc_dk.dk_label,
|
2000-10-19 18:06:02 +04:00
|
|
|
sc->sc_dk.dk_cpulabel);
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
sc->sc_flags &= ~LDF_LABELLING;
|
|
|
|
ldunlock(sc);
|
2000-10-19 18:06:02 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIOCWLABEL:
|
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
return (EBADF);
|
|
|
|
if (*(int *)addr)
|
2000-11-26 20:44:02 +03:00
|
|
|
sc->sc_flags |= LDF_WLABEL;
|
2000-10-19 18:06:02 +04:00
|
|
|
else
|
2000-11-26 20:44:02 +03:00
|
|
|
sc->sc_flags &= ~LDF_WLABEL;
|
2000-10-19 18:06:02 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIOCGDEFLABEL:
|
2000-11-26 20:44:02 +03:00
|
|
|
ldgetdefaultlabel(sc, (struct disklabel *)addr);
|
2000-10-19 18:06:02 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-11-26 20:44:02 +03:00
|
|
|
ldstrategy(struct buf *bp)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
2000-11-26 20:44:02 +03:00
|
|
|
struct ld_softc *sc;
|
2000-11-08 22:20:33 +03:00
|
|
|
int s;
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
|
2000-11-08 22:20:33 +03:00
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
if (sc->sc_queuecnt == sc->sc_maxqueuecnt) {
|
|
|
|
BUFQ_INSERT_TAIL(&sc->sc_bufq, bp);
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
splx(s);
|
2000-11-26 20:44:02 +03:00
|
|
|
ldstart(sc, bp);
|
2000-11-08 22:20:33 +03:00
|
|
|
}
|
|
|
|
|
2000-11-26 19:16:04 +03:00
|
|
|
static int
|
2000-11-26 20:44:02 +03:00
|
|
|
ldstart(struct ld_softc *sc, struct buf *bp)
|
2000-11-08 22:20:33 +03:00
|
|
|
{
|
|
|
|
struct disklabel *lp;
|
|
|
|
int part, s, rv;
|
2000-10-19 18:06:02 +04:00
|
|
|
|
2000-12-03 16:03:30 +03:00
|
|
|
if ((sc->sc_flags & LDF_DRAIN) != 0) {
|
|
|
|
bp->b_error = EIO;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2000-10-19 18:06:02 +04:00
|
|
|
part = DISKPART(bp->b_dev);
|
|
|
|
lp = sc->sc_dk.dk_label;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The transfer must be a whole number of blocks and the offset must
|
|
|
|
* not be negative.
|
|
|
|
*/
|
|
|
|
if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
biodone(bp);
|
2000-11-26 19:16:04 +03:00
|
|
|
return (-1);
|
2000-10-19 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's a null transfer, return.
|
|
|
|
*/
|
|
|
|
if (bp->b_bcount == 0) {
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
2000-11-26 19:16:04 +03:00
|
|
|
return (-1);
|
2000-10-19 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do bounds checking and adjust the transfer. If error, process.
|
|
|
|
* If past the end of partition, just return.
|
|
|
|
*/
|
|
|
|
if (part != RAW_PART &&
|
|
|
|
bounds_check_with_label(bp, lp,
|
2000-11-26 20:44:02 +03:00
|
|
|
(sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
|
2000-10-19 18:06:02 +04:00
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
2000-11-26 19:16:04 +03:00
|
|
|
return (-1);
|
2000-10-19 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert the logical block number to a physical one and put it in
|
|
|
|
* terms of the device's logical block size.
|
|
|
|
*/
|
|
|
|
if (lp->d_secsize >= DEV_BSIZE)
|
|
|
|
bp->b_rawblkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
|
|
|
|
else
|
|
|
|
bp->b_rawblkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
|
|
|
|
|
|
|
|
if (bp->b_dev != RAW_PART)
|
|
|
|
bp->b_rawblkno += lp->d_partitions[part].p_offset;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
disk_busy(&sc->sc_dk);
|
2000-11-08 22:20:33 +03:00
|
|
|
sc->sc_queuecnt++;
|
2000-10-19 18:06:02 +04:00
|
|
|
splx(s);
|
|
|
|
|
|
|
|
if ((rv = (*sc->sc_start)(sc, bp)) != 0) {
|
|
|
|
bp->b_error = rv;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
s = splbio();
|
2000-11-26 20:44:02 +03:00
|
|
|
lddone(sc, bp);
|
2000-10-19 18:06:02 +04:00
|
|
|
splx(s);
|
|
|
|
}
|
2000-11-26 19:16:04 +03:00
|
|
|
|
|
|
|
return (0);
|
2000-10-19 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-11-26 20:44:02 +03:00
|
|
|
lddone(struct ld_softc *sc, struct buf *bp)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
if ((bp->b_flags & B_ERROR) != 0) {
|
2000-11-26 20:44:02 +03:00
|
|
|
diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
|
2000-10-19 18:06:02 +04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid);
|
|
|
|
#if NRND > 0
|
|
|
|
rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
|
|
|
|
#endif
|
|
|
|
biodone(bp);
|
2000-12-03 16:03:30 +03:00
|
|
|
if (--sc->sc_queuecnt == 0 && (sc->sc_flags & LDF_DRAIN) != 0)
|
|
|
|
wakeup(&sc->sc_queuecnt);
|
2000-11-08 22:20:33 +03:00
|
|
|
|
2000-11-26 19:16:04 +03:00
|
|
|
while ((bp = BUFQ_FIRST(&sc->sc_bufq)) != NULL) {
|
2000-11-08 22:20:33 +03:00
|
|
|
BUFQ_REMOVE(&sc->sc_bufq, bp);
|
2000-11-26 20:44:02 +03:00
|
|
|
if (!ldstart(sc, bp))
|
2000-11-26 19:16:04 +03:00
|
|
|
break;
|
2000-11-08 22:20:33 +03:00
|
|
|
}
|
2000-10-19 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-11-26 20:44:02 +03:00
|
|
|
ldsize(dev_t dev)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
2000-11-26 20:44:02 +03:00
|
|
|
struct ld_softc *sc;
|
2000-10-19 18:06:02 +04:00
|
|
|
int part, unit, omask, size;
|
|
|
|
|
|
|
|
unit = DISKUNIT(dev);
|
2000-11-26 20:44:02 +03:00
|
|
|
if ((sc = device_lookup(&ld_cd, unit)) == NULL)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (ENODEV);
|
2000-11-26 20:44:02 +03:00
|
|
|
if ((sc->sc_flags & LDF_ENABLED) == 0)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (ENODEV);
|
|
|
|
part = DISKPART(dev);
|
|
|
|
|
|
|
|
omask = sc->sc_dk.dk_openmask & (1 << part);
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (-1);
|
|
|
|
else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
|
|
|
|
size = -1;
|
|
|
|
else
|
|
|
|
size = sc->sc_dk.dk_label->d_partitions[part].p_size *
|
|
|
|
(sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
|
2000-11-26 20:44:02 +03:00
|
|
|
if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the label information from the specified device.
|
|
|
|
*/
|
|
|
|
static void
|
2000-11-26 20:44:02 +03:00
|
|
|
ldgetdisklabel(struct ld_softc *sc)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
|
|
|
const char *errstring;
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
/* Call the generic disklabel extraction routine. */
|
|
|
|
errstring = readdisklabel(MAKEDISKDEV(0, sc->sc_dv.dv_unit, RAW_PART),
|
2000-11-26 20:44:02 +03:00
|
|
|
ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
|
2000-10-19 18:06:02 +04:00
|
|
|
if (errstring != NULL)
|
|
|
|
printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a ficticious label.
|
|
|
|
*/
|
|
|
|
static void
|
2000-11-26 20:44:02 +03:00
|
|
|
ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
memset(lp, 0, sizeof(struct disklabel));
|
|
|
|
|
|
|
|
lp->d_secsize = sc->sc_secsize;
|
|
|
|
lp->d_ntracks = sc->sc_nheads;
|
|
|
|
lp->d_nsectors = sc->sc_nsectors;
|
|
|
|
lp->d_ncylinders = sc->sc_ncylinders;
|
|
|
|
lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
|
2000-11-26 20:44:02 +03:00
|
|
|
lp->d_type = DTYPE_LD;
|
2000-10-19 18:06:02 +04:00
|
|
|
strcpy(lp->d_typename, "unknown");
|
|
|
|
strcpy(lp->d_packname, "fictitious");
|
|
|
|
lp->d_secperunit = sc->sc_secperunit;
|
|
|
|
lp->d_rpm = 7200;
|
|
|
|
lp->d_interleave = 1;
|
|
|
|
lp->d_flags = 0;
|
|
|
|
|
|
|
|
lp->d_partitions[RAW_PART].p_offset = 0;
|
|
|
|
lp->d_partitions[RAW_PART].p_size =
|
|
|
|
lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
|
|
|
|
lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
|
|
|
|
lp->d_npartitions = RAW_PART + 1;
|
|
|
|
|
|
|
|
lp->d_magic = DISKMAGIC;
|
|
|
|
lp->d_magic2 = DISKMAGIC;
|
|
|
|
lp->d_checksum = dkcksum(lp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait interruptibly for an exclusive lock.
|
|
|
|
*
|
|
|
|
* XXX Several drivers do this; it should be abstracted and made MP-safe.
|
|
|
|
*/
|
|
|
|
static int
|
2000-11-26 20:44:02 +03:00
|
|
|
ldlock(struct ld_softc *sc)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
while ((sc->sc_flags & LDF_LKHELD) != 0) {
|
|
|
|
sc->sc_flags |= LDF_LKWANTED;
|
|
|
|
if ((error = tsleep(sc, PRIBIO | PCATCH, "ldlck", 0)) != 0)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (error);
|
|
|
|
}
|
2000-11-26 20:44:02 +03:00
|
|
|
sc->sc_flags |= LDF_LKHELD;
|
2000-10-19 18:06:02 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlock and wake up any waiters.
|
|
|
|
*/
|
|
|
|
static void
|
2000-11-26 20:44:02 +03:00
|
|
|
ldunlock(struct ld_softc *sc)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
sc->sc_flags &= ~LDF_LKHELD;
|
|
|
|
if ((sc->sc_flags & LDF_LKWANTED) != 0) {
|
|
|
|
sc->sc_flags &= ~LDF_LKWANTED;
|
2000-10-19 18:06:02 +04:00
|
|
|
wakeup(sc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a dump.
|
|
|
|
*/
|
|
|
|
int
|
2000-11-26 20:44:02 +03:00
|
|
|
lddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
2000-11-26 20:44:02 +03:00
|
|
|
struct ld_softc *sc;
|
2000-10-19 18:06:02 +04:00
|
|
|
struct disklabel *lp;
|
|
|
|
int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
|
|
|
|
static int dumping;
|
|
|
|
|
|
|
|
unit = DISKUNIT(dev);
|
2000-11-26 20:44:02 +03:00
|
|
|
if ((sc = device_lookup(&ld_cd, unit)) == NULL)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (ENXIO);
|
2000-11-26 20:44:02 +03:00
|
|
|
if ((sc->sc_flags & LDF_ENABLED) == 0)
|
2000-10-19 18:06:02 +04:00
|
|
|
return (ENODEV);
|
2001-01-04 00:01:28 +03:00
|
|
|
if (sc->sc_dump == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/* Check if recursive dump; if so, punt. */
|
|
|
|
if (dumping)
|
|
|
|
return (EFAULT);
|
|
|
|
dumping = 1;
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
/* Convert to disk sectors. Request must be a multiple of size. */
|
2001-01-04 00:01:28 +03:00
|
|
|
part = DISKPART(dev);
|
2000-10-19 18:06:02 +04:00
|
|
|
lp = sc->sc_dk.dk_label;
|
|
|
|
if ((size % lp->d_secsize) != 0)
|
|
|
|
return (EFAULT);
|
|
|
|
towrt = size / lp->d_secsize;
|
|
|
|
blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */
|
|
|
|
|
|
|
|
nsects = lp->d_partitions[part].p_size;
|
|
|
|
sectoff = lp->d_partitions[part].p_offset;
|
|
|
|
|
|
|
|
/* Check transfer bounds against partition size. */
|
|
|
|
if ((blkno < 0) || ((blkno + towrt) > nsects))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Offset block number to start of partition. */
|
|
|
|
blkno += sectoff;
|
|
|
|
|
|
|
|
/* Start dumping and return when done. */
|
2001-01-04 00:01:28 +03:00
|
|
|
maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
|
2000-10-19 18:06:02 +04:00
|
|
|
while (towrt > 0) {
|
2001-01-04 00:01:28 +03:00
|
|
|
nblk = min(maxblkcnt, towrt);
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
|
|
|
|
return (rv);
|
|
|
|
|
|
|
|
towrt -= nblk;
|
|
|
|
blkno += nblk;
|
|
|
|
va += nblk * sc->sc_secsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
dumping = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adjust the size of a transfer.
|
|
|
|
*/
|
|
|
|
static void
|
2000-11-26 20:44:02 +03:00
|
|
|
ldminphys(struct buf *bp)
|
2000-10-19 18:06:02 +04:00
|
|
|
{
|
2000-11-26 20:44:02 +03:00
|
|
|
struct ld_softc *sc;
|
2000-10-19 18:06:02 +04:00
|
|
|
|
2000-11-26 20:44:02 +03:00
|
|
|
sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
|
2000-10-19 18:06:02 +04:00
|
|
|
|
|
|
|
if (bp->b_bcount > sc->sc_maxxfer)
|
|
|
|
bp->b_bcount = sc->sc_maxxfer;
|
|
|
|
minphys(bp);
|
|
|
|
}
|