2009-05-12 16:10:29 +04:00
|
|
|
/* $NetBSD: dk.c,v 1.44 2009/05/12 12:12:26 cegger Exp $ */
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/*-
|
2007-07-21 23:51:47 +04:00
|
|
|
* Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
|
2004-09-25 07:30:44 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2009-05-12 16:10:29 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.44 2009/05/12 12:12:26 cegger Exp $");
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
#include "opt_dkwedge.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/pool.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/fcntl.h>
|
2004-10-28 11:07:35 +04:00
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/bufq.h>
|
2004-09-25 07:30:44 +04:00
|
|
|
#include <sys/vnode.h>
|
2004-10-23 21:16:45 +04:00
|
|
|
#include <sys/stat.h>
|
2004-09-25 07:30:44 +04:00
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/callout.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
2004-10-15 08:42:09 +04:00
|
|
|
#include <sys/device.h>
|
2006-05-15 01:42:26 +04:00
|
|
|
#include <sys/kauth.h>
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
#include <miscfs/specfs/specdev.h>
|
|
|
|
|
|
|
|
MALLOC_DEFINE(M_DKWEDGE, "dkwedge", "Disk wedge structures");
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
DKW_STATE_LARVAL = 0,
|
|
|
|
DKW_STATE_RUNNING = 1,
|
|
|
|
DKW_STATE_DYING = 2,
|
|
|
|
DKW_STATE_DEAD = 666
|
|
|
|
} dkwedge_state_t;
|
|
|
|
|
|
|
|
struct dkwedge_softc {
|
2004-10-15 08:42:09 +04:00
|
|
|
struct device *sc_dev; /* pointer to our pseudo-device */
|
|
|
|
struct cfdata sc_cfdata; /* our cfdata structure */
|
2004-09-25 07:30:44 +04:00
|
|
|
uint8_t sc_wname[128]; /* wedge name (Unicode, UTF-8) */
|
|
|
|
|
|
|
|
dkwedge_state_t sc_state; /* state this wedge is in */
|
|
|
|
|
|
|
|
struct disk *sc_parent; /* parent disk */
|
|
|
|
daddr_t sc_offset; /* LBA offset of wedge in parent */
|
|
|
|
uint64_t sc_size; /* size of wedge in blocks */
|
|
|
|
char sc_ptype[32]; /* partition type */
|
|
|
|
dev_t sc_pdev; /* cached parent's dev_t */
|
|
|
|
/* link on parent's wedge list */
|
|
|
|
LIST_ENTRY(dkwedge_softc) sc_plink;
|
|
|
|
|
|
|
|
struct disk sc_dk; /* our own disk structure */
|
2005-10-15 21:29:10 +04:00
|
|
|
struct bufq_state *sc_bufq; /* buffer queue */
|
2004-09-25 07:30:44 +04:00
|
|
|
struct callout sc_restart_ch; /* callout to restart I/O */
|
|
|
|
|
|
|
|
u_int sc_iopend; /* I/Os pending */
|
|
|
|
int sc_flags; /* flags (splbio) */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DK_F_WAIT_DRAIN 0x0001 /* waiting for I/O to drain */
|
|
|
|
|
|
|
|
static void dkstart(struct dkwedge_softc *);
|
|
|
|
static void dkiodone(struct buf *);
|
|
|
|
static void dkrestart(void *);
|
|
|
|
|
|
|
|
static dev_type_open(dkopen);
|
|
|
|
static dev_type_close(dkclose);
|
|
|
|
static dev_type_read(dkread);
|
|
|
|
static dev_type_write(dkwrite);
|
|
|
|
static dev_type_ioctl(dkioctl);
|
|
|
|
static dev_type_strategy(dkstrategy);
|
|
|
|
static dev_type_dump(dkdump);
|
|
|
|
static dev_type_size(dksize);
|
|
|
|
|
|
|
|
const struct bdevsw dk_bdevsw = {
|
|
|
|
dkopen, dkclose, dkstrategy, dkioctl, dkdump, dksize, D_DISK
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct cdevsw dk_cdevsw = {
|
|
|
|
dkopen, dkclose, dkread, dkwrite, dkioctl,
|
|
|
|
nostop, notty, nopoll, nommap, nokqfilter, D_DISK
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct dkwedge_softc **dkwedges;
|
|
|
|
static u_int ndkwedges;
|
2007-07-21 23:51:47 +04:00
|
|
|
static krwlock_t dkwedges_lock;
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2004-10-01 09:16:04 +04:00
|
|
|
static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
|
2007-07-21 23:51:47 +04:00
|
|
|
static krwlock_t dkwedge_discovery_methods_lock;
|
2004-10-01 09:16:04 +04:00
|
|
|
|
2004-10-15 08:42:09 +04:00
|
|
|
/*
|
|
|
|
* dkwedge_match:
|
|
|
|
*
|
|
|
|
* Autoconfiguration match function for pseudo-device glue.
|
|
|
|
*/
|
|
|
|
static int
|
2009-05-12 16:10:29 +04:00
|
|
|
dkwedge_match(struct device *parent, cfdata_t match,
|
2006-11-16 04:32:37 +03:00
|
|
|
void *aux)
|
2004-10-15 08:42:09 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Pseudo-device; always present. */
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_attach:
|
|
|
|
*
|
|
|
|
* Autoconfiguration attach function for pseudo-device glue.
|
|
|
|
*/
|
|
|
|
static void
|
2006-11-16 04:32:37 +03:00
|
|
|
dkwedge_attach(struct device *parent, struct device *self,
|
|
|
|
void *aux)
|
2004-10-15 08:42:09 +04:00
|
|
|
{
|
|
|
|
|
2007-12-09 23:27:42 +03:00
|
|
|
if (!pmf_device_register(self, NULL, NULL))
|
|
|
|
aprint_error_dev(self, "couldn't establish power handler\n");
|
2004-10-15 08:42:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_detach:
|
|
|
|
*
|
|
|
|
* Autoconfiguration detach function for pseudo-device glue.
|
|
|
|
*/
|
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
dkwedge_detach(struct device *self, int flags)
|
2004-10-15 08:42:09 +04:00
|
|
|
{
|
|
|
|
|
2007-12-09 23:27:42 +03:00
|
|
|
pmf_device_deregister(self);
|
2004-10-15 08:42:09 +04:00
|
|
|
/* Always succeeds. */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFDRIVER_DECL(dk, DV_DISK, NULL);
|
2008-03-04 16:51:18 +03:00
|
|
|
CFATTACH_DECL_NEW(dk, 0,
|
2004-10-15 08:42:09 +04:00
|
|
|
dkwedge_match, dkwedge_attach, dkwedge_detach, NULL);
|
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
/*
|
|
|
|
* dkwedge_wait_drain:
|
|
|
|
*
|
|
|
|
* Wait for I/O on the wedge to drain.
|
|
|
|
* NOTE: Must be called at splbio()!
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dkwedge_wait_drain(struct dkwedge_softc *sc)
|
|
|
|
{
|
|
|
|
|
|
|
|
while (sc->sc_iopend != 0) {
|
|
|
|
sc->sc_flags |= DK_F_WAIT_DRAIN;
|
|
|
|
(void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_compute_pdev:
|
|
|
|
*
|
|
|
|
* Compute the parent disk's dev_t.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
|
|
|
|
{
|
|
|
|
const char *name, *cp;
|
|
|
|
int punit, pmaj;
|
|
|
|
char devname[16];
|
|
|
|
|
|
|
|
name = pname;
|
|
|
|
if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
|
|
|
|
return (ENODEV);
|
2005-02-27 03:26:58 +03:00
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
name += strlen(devname);
|
|
|
|
for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
|
|
|
|
punit = (punit * 10) + (*cp - '0');
|
|
|
|
if (cp == name) {
|
|
|
|
/* Invalid parent disk name. */
|
|
|
|
return (ENODEV);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_array_expand:
|
|
|
|
*
|
|
|
|
* Expand the dkwedges array.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dkwedge_array_expand(void)
|
|
|
|
{
|
|
|
|
int newcnt = ndkwedges + 16;
|
|
|
|
struct dkwedge_softc **newarray, **oldarray;
|
|
|
|
|
|
|
|
newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
|
|
|
|
M_WAITOK|M_ZERO);
|
|
|
|
if ((oldarray = dkwedges) != NULL)
|
|
|
|
memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
|
|
|
|
dkwedges = newarray;
|
|
|
|
ndkwedges = newcnt;
|
|
|
|
if (oldarray != NULL)
|
|
|
|
free(oldarray, M_DKWEDGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_add: [exported function]
|
|
|
|
*
|
|
|
|
* Add a disk wedge based on the provided information.
|
|
|
|
*
|
|
|
|
* The incoming dkw_devname[] is ignored, instead being
|
|
|
|
* filled in and returned to the caller.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dkwedge_add(struct dkwedge_info *dkw)
|
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc, *lsc;
|
|
|
|
struct disk *pdk;
|
|
|
|
u_int unit;
|
|
|
|
int error;
|
|
|
|
dev_t pdev;
|
|
|
|
|
|
|
|
dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
|
|
|
|
pdk = disk_find(dkw->dkw_parent);
|
|
|
|
if (pdk == NULL)
|
|
|
|
return (ENODEV);
|
|
|
|
|
|
|
|
error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if (dkw->dkw_offset < 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
|
|
|
|
sc->sc_state = DKW_STATE_LARVAL;
|
|
|
|
sc->sc_parent = pdk;
|
|
|
|
sc->sc_pdev = pdev;
|
|
|
|
sc->sc_offset = dkw->dkw_offset;
|
|
|
|
sc->sc_size = dkw->dkw_size;
|
|
|
|
|
|
|
|
memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
|
|
|
|
sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
|
|
|
|
|
|
|
|
memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
|
|
|
|
sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
|
|
|
|
|
2005-10-15 21:29:10 +04:00
|
|
|
bufq_alloc(&sc->sc_bufq, "fcfs", 0);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2007-07-10 00:51:58 +04:00
|
|
|
callout_init(&sc->sc_restart_ch, 0);
|
2004-09-25 07:30:44 +04:00
|
|
|
callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wedge will be added; increment the wedge count for the parent.
|
|
|
|
* Only allow this to happend if RAW_PART is the only thing open.
|
|
|
|
*/
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (pdk->dk_openmask & ~(1 << RAW_PART))
|
|
|
|
error = EBUSY;
|
|
|
|
else {
|
|
|
|
/* Check for wedge overlap. */
|
|
|
|
LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
|
|
|
|
daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
|
|
|
|
daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
|
|
|
|
|
|
|
|
if (sc->sc_offset >= lsc->sc_offset &&
|
|
|
|
sc->sc_offset <= llastblk) {
|
|
|
|
/* Overlaps the tail of the exsiting wedge. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lastblk >= lsc->sc_offset &&
|
|
|
|
lastblk <= llastblk) {
|
|
|
|
/* Overlaps the head of the existing wedge. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lsc != NULL)
|
|
|
|
error = EINVAL;
|
|
|
|
else {
|
|
|
|
pdk->dk_nwedges++;
|
|
|
|
LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
|
|
|
|
}
|
|
|
|
}
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (error) {
|
2005-10-15 21:29:10 +04:00
|
|
|
bufq_free(sc->sc_bufq);
|
2004-09-25 07:30:44 +04:00
|
|
|
free(sc, M_DKWEDGE);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-10-15 08:42:09 +04:00
|
|
|
/* Fill in our cfdata for the pseudo-device glue. */
|
|
|
|
sc->sc_cfdata.cf_name = dk_cd.cd_name;
|
|
|
|
sc->sc_cfdata.cf_atname = dk_ca.ca_name;
|
|
|
|
/* sc->sc_cfdata.cf_unit set below */
|
2005-09-28 22:25:19 +04:00
|
|
|
sc->sc_cfdata.cf_fstate = FSTATE_STAR;
|
2004-10-15 08:42:09 +04:00
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
/* Insert the larval wedge into the array. */
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_enter(&dkwedges_lock, RW_WRITER);
|
2004-09-25 07:30:44 +04:00
|
|
|
for (error = 0;;) {
|
|
|
|
struct dkwedge_softc **scpp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for a duplicate wname while searching for
|
|
|
|
* a slot.
|
|
|
|
*/
|
|
|
|
for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
|
|
|
|
if (dkwedges[unit] == NULL) {
|
|
|
|
if (scpp == NULL) {
|
|
|
|
scpp = &dkwedges[unit];
|
2004-10-15 08:42:09 +04:00
|
|
|
sc->sc_cfdata.cf_unit = unit;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* XXX Unicode. */
|
|
|
|
if (strcmp(dkwedges[unit]->sc_wname,
|
|
|
|
sc->sc_wname) == 0) {
|
|
|
|
error = EEXIST;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
KASSERT(unit == ndkwedges);
|
|
|
|
if (scpp == NULL)
|
|
|
|
dkwedge_array_expand();
|
|
|
|
else {
|
2004-10-15 08:42:09 +04:00
|
|
|
KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
|
2004-09-25 07:30:44 +04:00
|
|
|
*scpp = sc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedges_lock);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (error) {
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
pdk->dk_nwedges--;
|
|
|
|
LIST_REMOVE(sc, sc_plink);
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2005-10-15 21:29:10 +04:00
|
|
|
bufq_free(sc->sc_bufq);
|
2004-09-25 07:30:44 +04:00
|
|
|
free(sc, M_DKWEDGE);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-10-15 08:42:09 +04:00
|
|
|
/*
|
|
|
|
* Now that we know the unit #, attach a pseudo-device for
|
|
|
|
* this wedge instance. This will provide us with the
|
|
|
|
* "struct device" necessary for glue to other parts of the
|
|
|
|
* system.
|
|
|
|
*
|
|
|
|
* This should never fail, unless we're almost totally out of
|
|
|
|
* memory.
|
|
|
|
*/
|
|
|
|
if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
|
|
|
|
aprint_error("%s%u: unable to attach pseudo-device\n",
|
|
|
|
sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_enter(&dkwedges_lock, RW_WRITER);
|
2004-10-15 08:42:09 +04:00
|
|
|
dkwedges[sc->sc_cfdata.cf_unit] = NULL;
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedges_lock);
|
2004-10-15 08:42:09 +04:00
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&pdk->dk_openlock);
|
2004-10-15 08:42:09 +04:00
|
|
|
pdk->dk_nwedges--;
|
|
|
|
LIST_REMOVE(sc, sc_plink);
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&pdk->dk_openlock);
|
2004-10-15 08:42:09 +04:00
|
|
|
|
2005-10-15 21:29:10 +04:00
|
|
|
bufq_free(sc->sc_bufq);
|
2004-10-15 08:42:09 +04:00
|
|
|
free(sc, M_DKWEDGE);
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/* Return the devname to the caller. */
|
2008-04-06 12:33:31 +04:00
|
|
|
strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
|
|
|
|
sizeof(dkw->dkw_devname));
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX Really ought to make the disk_attach() and the changing
|
|
|
|
* of state to RUNNING atomic.
|
|
|
|
*/
|
|
|
|
|
2008-04-06 12:33:31 +04:00
|
|
|
disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
|
2004-09-25 07:30:44 +04:00
|
|
|
disk_attach(&sc->sc_dk);
|
|
|
|
|
|
|
|
/* Disk wedge is ready for use! */
|
|
|
|
sc->sc_state = DKW_STATE_RUNNING;
|
|
|
|
|
|
|
|
/* Announce our arrival. */
|
2008-04-06 12:33:31 +04:00
|
|
|
aprint_normal("%s at %s: %s\n", device_xname(sc->sc_dev), pdk->dk_name,
|
2004-09-25 07:30:44 +04:00
|
|
|
sc->sc_wname); /* XXX Unicode */
|
2004-09-27 11:56:14 +04:00
|
|
|
aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
|
2008-04-06 12:33:31 +04:00
|
|
|
device_xname(sc->sc_dev), sc->sc_size, sc->sc_offset, sc->sc_ptype);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_del: [exported function]
|
|
|
|
*
|
|
|
|
* Delete a disk wedge based on the provided information.
|
|
|
|
* NOTE: We look up the wedge based on the wedge devname,
|
|
|
|
* not wname.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dkwedge_del(struct dkwedge_info *dkw)
|
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc = NULL;
|
|
|
|
u_int unit;
|
2006-04-06 21:55:43 +04:00
|
|
|
int bmaj, cmaj, s;
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/* Find our softc. */
|
|
|
|
dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_enter(&dkwedges_lock, RW_WRITER);
|
2004-09-25 07:30:44 +04:00
|
|
|
for (unit = 0; unit < ndkwedges; unit++) {
|
|
|
|
if ((sc = dkwedges[unit]) != NULL &&
|
2008-04-06 12:33:31 +04:00
|
|
|
strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 &&
|
2004-09-25 07:30:44 +04:00
|
|
|
strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
|
|
|
|
/* Mark the wedge as dying. */
|
|
|
|
sc->sc_state = DKW_STATE_DYING;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedges_lock);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (unit == ndkwedges)
|
|
|
|
return (ESRCH);
|
|
|
|
|
|
|
|
KASSERT(sc != NULL);
|
|
|
|
|
|
|
|
/* Locate the wedge major numbers. */
|
|
|
|
bmaj = bdevsw_lookup_major(&dk_bdevsw);
|
|
|
|
cmaj = cdevsw_lookup_major(&dk_cdevsw);
|
|
|
|
|
|
|
|
/* Kill any pending restart. */
|
|
|
|
callout_stop(&sc->sc_restart_ch);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkstart() will kill any queued buffers now that the
|
|
|
|
* state of the wedge is not RUNNING. Once we've done
|
|
|
|
* that, wait for any other pending I/O to complete.
|
|
|
|
*/
|
|
|
|
s = splbio();
|
|
|
|
dkstart(sc);
|
|
|
|
dkwedge_wait_drain(sc);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/* Nuke the vnodes for any open instances. */
|
2006-04-06 21:55:43 +04:00
|
|
|
vdevgone(bmaj, unit, unit, VBLK);
|
|
|
|
vdevgone(cmaj, unit, unit, VCHR);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/* Clean up the parent. */
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&sc->sc_dk.dk_openlock);
|
|
|
|
mutex_enter(&sc->sc_parent->dk_rawlock);
|
2004-10-23 21:16:45 +04:00
|
|
|
if (sc->sc_dk.dk_openmask) {
|
2004-09-25 07:30:44 +04:00
|
|
|
if (sc->sc_parent->dk_rawopens-- == 1) {
|
|
|
|
KASSERT(sc->sc_parent->dk_rawvp != NULL);
|
|
|
|
(void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
|
2008-03-22 00:54:58 +03:00
|
|
|
NOCRED);
|
2004-09-25 07:30:44 +04:00
|
|
|
sc->sc_parent->dk_rawvp = NULL;
|
|
|
|
}
|
2004-10-23 21:16:45 +04:00
|
|
|
sc->sc_dk.dk_openmask = 0;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&sc->sc_parent->dk_rawlock);
|
|
|
|
mutex_exit(&sc->sc_dk.dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/* Announce our departure. */
|
2008-04-06 12:33:31 +04:00
|
|
|
aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
|
2004-09-25 07:30:44 +04:00
|
|
|
sc->sc_parent->dk_name,
|
|
|
|
sc->sc_wname); /* XXX Unicode */
|
|
|
|
|
2004-10-15 08:42:09 +04:00
|
|
|
/* Delete our pseudo-device. */
|
|
|
|
(void) config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET);
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&sc->sc_parent->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
sc->sc_parent->dk_nwedges--;
|
|
|
|
LIST_REMOVE(sc, sc_plink);
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&sc->sc_parent->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/* Delete our buffer queue. */
|
2005-10-15 21:29:10 +04:00
|
|
|
bufq_free(sc->sc_bufq);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/* Detach from the disk list. */
|
|
|
|
disk_detach(&sc->sc_dk);
|
2008-05-03 12:23:41 +04:00
|
|
|
disk_destroy(&sc->sc_dk);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/* Poof. */
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_enter(&dkwedges_lock, RW_WRITER);
|
2004-09-25 07:30:44 +04:00
|
|
|
dkwedges[unit] = NULL;
|
|
|
|
sc->sc_state = DKW_STATE_DEAD;
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedges_lock);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
free(sc, M_DKWEDGE);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_delall: [exported function]
|
|
|
|
*
|
|
|
|
* Delete all of the wedges on the specified disk. Used when
|
|
|
|
* a disk is being detached.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dkwedge_delall(struct disk *pdk)
|
|
|
|
{
|
|
|
|
struct dkwedge_info dkw;
|
|
|
|
struct dkwedge_softc *sc;
|
|
|
|
|
|
|
|
for (;;) {
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
|
|
|
|
KASSERT(pdk->dk_nwedges == 0);
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
strcpy(dkw.dkw_parent, pdk->dk_name);
|
2008-04-06 12:33:31 +04:00
|
|
|
strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
|
|
|
|
sizeof(dkw.dkw_devname));
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
(void) dkwedge_del(&dkw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_list: [exported function]
|
|
|
|
*
|
|
|
|
* List all of the wedges on a particular disk.
|
|
|
|
* If p == NULL, the buffer is in kernel space. Otherwise, it is
|
|
|
|
* in user space of the specified process.
|
|
|
|
*/
|
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
|
2004-09-25 07:30:44 +04:00
|
|
|
{
|
|
|
|
struct uio uio;
|
|
|
|
struct iovec iov;
|
|
|
|
struct dkwedge_softc *sc;
|
|
|
|
struct dkwedge_info dkw;
|
2006-03-01 15:38:10 +03:00
|
|
|
struct vmspace *vm;
|
2004-09-25 07:30:44 +04:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
iov.iov_base = dkwl->dkwl_buf;
|
|
|
|
iov.iov_len = dkwl->dkwl_bufsize;
|
|
|
|
|
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_offset = 0;
|
|
|
|
uio.uio_resid = dkwl->dkwl_bufsize;
|
|
|
|
uio.uio_rw = UIO_READ;
|
2006-03-01 15:38:10 +03:00
|
|
|
if (l == NULL) {
|
|
|
|
UIO_SETUP_SYSSPACE(&uio);
|
|
|
|
} else {
|
|
|
|
error = proc_vmspace_getref(l->l_proc, &vm);
|
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
uio.uio_vmspace = vm;
|
|
|
|
}
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
dkwl->dkwl_ncopied = 0;
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
|
|
|
|
if (uio.uio_resid < sizeof(dkw))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING)
|
|
|
|
continue;
|
|
|
|
|
2008-04-06 12:33:31 +04:00
|
|
|
strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
|
|
|
|
sizeof(dkw.dkw_devname));
|
2004-09-25 07:30:44 +04:00
|
|
|
memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
|
|
|
|
dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
|
|
|
|
strcpy(dkw.dkw_parent, sc->sc_parent->dk_name);
|
|
|
|
dkw.dkw_offset = sc->sc_offset;
|
|
|
|
dkw.dkw_size = sc->sc_size;
|
|
|
|
strcpy(dkw.dkw_ptype, sc->sc_ptype);
|
|
|
|
|
|
|
|
error = uiomove(&dkw, sizeof(dkw), &uio);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
dkwl->dkwl_ncopied++;
|
|
|
|
}
|
|
|
|
dkwl->dkwl_nwedges = pdk->dk_nwedges;
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&pdk->dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2006-03-01 15:38:10 +03:00
|
|
|
if (l != NULL) {
|
|
|
|
uvmspace_free(vm);
|
|
|
|
}
|
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
Extract common code from i386, xen, and sparc64, creating
config_handle_wedges() and read_disk_sectors(). On x86, handle_wedges()
is a thin wrapper for config_handle_wedges(). Share opendisk()
across architectures.
Add kernel code in support of specifying a root partition by wedge
name. E.g., root specifications "wedge:wd0a", "wedge:David's Root
Volume" are possible. (Patches for config(1) coming soon.)
In support of moving disks between architectures (esp. i386 <->
evbmips), I've written a routine convertdisklabel() that ensures
that the raw partition is at RAW_DISK by following these steps:
0 If we have read a disklabel that has a RAW_PART with
p_offset == 0 and p_size != 0, then use that raw partition.
1 If we have read a disklabel that has both partitions 'c'
and 'd', and RAW_PART has p_offset != 0 or p_size == 0,
but the other partition is suitable for a raw partition
(p_offset == 0, p_size != 0), then swap the two partitions
and use the new raw partition.
2 If the architecture's raw partition is 'd', and if there
is no partition 'd', but there is a partition 'c' that
is suitable for a raw partition, then copy partition 'c'
to partition 'd'.
3 Determine the drive's last sector, using either the
d_secperunit the drive reported, or by guessing (0x1fffffff).
If we cannot read the drive's last sector, then fail.
4 If we have read a disklabel that has no partition slot
RAW_PART, then create a partition RAW_PART. Make it span
the whole drive.
5 If there are fewer than MAXPARTITIONS partitions,
then "slide" the unsuitable raw partition RAW_PART, and
subsequent partitions, into partition slots RAW_PART+1
and subsequent slots. Create a raw partition at RAW_PART.
Make it span the whole drive.
The convertdisklabel() procedure can probably stand to be simplified,
but it ought to deal with all but an extraordinarily broken disklabel,
now.
i386: compiled and tested, sparc64: compiled, evbmips: compiled.
2007-06-24 05:43:34 +04:00
|
|
|
device_t
|
|
|
|
dkwedge_find_by_wname(const char *wname)
|
|
|
|
{
|
|
|
|
device_t dv = NULL;
|
|
|
|
struct dkwedge_softc *sc;
|
|
|
|
int i;
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_enter(&dkwedges_lock, RW_WRITER);
|
Extract common code from i386, xen, and sparc64, creating
config_handle_wedges() and read_disk_sectors(). On x86, handle_wedges()
is a thin wrapper for config_handle_wedges(). Share opendisk()
across architectures.
Add kernel code in support of specifying a root partition by wedge
name. E.g., root specifications "wedge:wd0a", "wedge:David's Root
Volume" are possible. (Patches for config(1) coming soon.)
In support of moving disks between architectures (esp. i386 <->
evbmips), I've written a routine convertdisklabel() that ensures
that the raw partition is at RAW_DISK by following these steps:
0 If we have read a disklabel that has a RAW_PART with
p_offset == 0 and p_size != 0, then use that raw partition.
1 If we have read a disklabel that has both partitions 'c'
and 'd', and RAW_PART has p_offset != 0 or p_size == 0,
but the other partition is suitable for a raw partition
(p_offset == 0, p_size != 0), then swap the two partitions
and use the new raw partition.
2 If the architecture's raw partition is 'd', and if there
is no partition 'd', but there is a partition 'c' that
is suitable for a raw partition, then copy partition 'c'
to partition 'd'.
3 Determine the drive's last sector, using either the
d_secperunit the drive reported, or by guessing (0x1fffffff).
If we cannot read the drive's last sector, then fail.
4 If we have read a disklabel that has no partition slot
RAW_PART, then create a partition RAW_PART. Make it span
the whole drive.
5 If there are fewer than MAXPARTITIONS partitions,
then "slide" the unsuitable raw partition RAW_PART, and
subsequent partitions, into partition slots RAW_PART+1
and subsequent slots. Create a raw partition at RAW_PART.
Make it span the whole drive.
The convertdisklabel() procedure can probably stand to be simplified,
but it ought to deal with all but an extraordinarily broken disklabel,
now.
i386: compiled and tested, sparc64: compiled, evbmips: compiled.
2007-06-24 05:43:34 +04:00
|
|
|
for (i = 0; i < ndkwedges; i++) {
|
|
|
|
if ((sc = dkwedges[i]) == NULL)
|
|
|
|
continue;
|
|
|
|
if (strcmp(sc->sc_wname, wname) == 0) {
|
|
|
|
if (dv != NULL) {
|
|
|
|
printf(
|
|
|
|
"WARNING: double match for wedge name %s "
|
|
|
|
"(%s, %s)\n", wname, device_xname(dv),
|
|
|
|
device_xname(sc->sc_dev));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dv = sc->sc_dev;
|
|
|
|
}
|
|
|
|
}
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedges_lock);
|
Extract common code from i386, xen, and sparc64, creating
config_handle_wedges() and read_disk_sectors(). On x86, handle_wedges()
is a thin wrapper for config_handle_wedges(). Share opendisk()
across architectures.
Add kernel code in support of specifying a root partition by wedge
name. E.g., root specifications "wedge:wd0a", "wedge:David's Root
Volume" are possible. (Patches for config(1) coming soon.)
In support of moving disks between architectures (esp. i386 <->
evbmips), I've written a routine convertdisklabel() that ensures
that the raw partition is at RAW_DISK by following these steps:
0 If we have read a disklabel that has a RAW_PART with
p_offset == 0 and p_size != 0, then use that raw partition.
1 If we have read a disklabel that has both partitions 'c'
and 'd', and RAW_PART has p_offset != 0 or p_size == 0,
but the other partition is suitable for a raw partition
(p_offset == 0, p_size != 0), then swap the two partitions
and use the new raw partition.
2 If the architecture's raw partition is 'd', and if there
is no partition 'd', but there is a partition 'c' that
is suitable for a raw partition, then copy partition 'c'
to partition 'd'.
3 Determine the drive's last sector, using either the
d_secperunit the drive reported, or by guessing (0x1fffffff).
If we cannot read the drive's last sector, then fail.
4 If we have read a disklabel that has no partition slot
RAW_PART, then create a partition RAW_PART. Make it span
the whole drive.
5 If there are fewer than MAXPARTITIONS partitions,
then "slide" the unsuitable raw partition RAW_PART, and
subsequent partitions, into partition slots RAW_PART+1
and subsequent slots. Create a raw partition at RAW_PART.
Make it span the whole drive.
The convertdisklabel() procedure can probably stand to be simplified,
but it ought to deal with all but an extraordinarily broken disklabel,
now.
i386: compiled and tested, sparc64: compiled, evbmips: compiled.
2007-06-24 05:43:34 +04:00
|
|
|
return dv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dkwedge_print_wnames(void)
|
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc;
|
|
|
|
int i;
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_enter(&dkwedges_lock, RW_WRITER);
|
Extract common code from i386, xen, and sparc64, creating
config_handle_wedges() and read_disk_sectors(). On x86, handle_wedges()
is a thin wrapper for config_handle_wedges(). Share opendisk()
across architectures.
Add kernel code in support of specifying a root partition by wedge
name. E.g., root specifications "wedge:wd0a", "wedge:David's Root
Volume" are possible. (Patches for config(1) coming soon.)
In support of moving disks between architectures (esp. i386 <->
evbmips), I've written a routine convertdisklabel() that ensures
that the raw partition is at RAW_DISK by following these steps:
0 If we have read a disklabel that has a RAW_PART with
p_offset == 0 and p_size != 0, then use that raw partition.
1 If we have read a disklabel that has both partitions 'c'
and 'd', and RAW_PART has p_offset != 0 or p_size == 0,
but the other partition is suitable for a raw partition
(p_offset == 0, p_size != 0), then swap the two partitions
and use the new raw partition.
2 If the architecture's raw partition is 'd', and if there
is no partition 'd', but there is a partition 'c' that
is suitable for a raw partition, then copy partition 'c'
to partition 'd'.
3 Determine the drive's last sector, using either the
d_secperunit the drive reported, or by guessing (0x1fffffff).
If we cannot read the drive's last sector, then fail.
4 If we have read a disklabel that has no partition slot
RAW_PART, then create a partition RAW_PART. Make it span
the whole drive.
5 If there are fewer than MAXPARTITIONS partitions,
then "slide" the unsuitable raw partition RAW_PART, and
subsequent partitions, into partition slots RAW_PART+1
and subsequent slots. Create a raw partition at RAW_PART.
Make it span the whole drive.
The convertdisklabel() procedure can probably stand to be simplified,
but it ought to deal with all but an extraordinarily broken disklabel,
now.
i386: compiled and tested, sparc64: compiled, evbmips: compiled.
2007-06-24 05:43:34 +04:00
|
|
|
for (i = 0; i < ndkwedges; i++) {
|
|
|
|
if ((sc = dkwedges[i]) == NULL)
|
|
|
|
continue;
|
|
|
|
printf(" wedge:%s", sc->sc_wname);
|
|
|
|
}
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedges_lock);
|
Extract common code from i386, xen, and sparc64, creating
config_handle_wedges() and read_disk_sectors(). On x86, handle_wedges()
is a thin wrapper for config_handle_wedges(). Share opendisk()
across architectures.
Add kernel code in support of specifying a root partition by wedge
name. E.g., root specifications "wedge:wd0a", "wedge:David's Root
Volume" are possible. (Patches for config(1) coming soon.)
In support of moving disks between architectures (esp. i386 <->
evbmips), I've written a routine convertdisklabel() that ensures
that the raw partition is at RAW_DISK by following these steps:
0 If we have read a disklabel that has a RAW_PART with
p_offset == 0 and p_size != 0, then use that raw partition.
1 If we have read a disklabel that has both partitions 'c'
and 'd', and RAW_PART has p_offset != 0 or p_size == 0,
but the other partition is suitable for a raw partition
(p_offset == 0, p_size != 0), then swap the two partitions
and use the new raw partition.
2 If the architecture's raw partition is 'd', and if there
is no partition 'd', but there is a partition 'c' that
is suitable for a raw partition, then copy partition 'c'
to partition 'd'.
3 Determine the drive's last sector, using either the
d_secperunit the drive reported, or by guessing (0x1fffffff).
If we cannot read the drive's last sector, then fail.
4 If we have read a disklabel that has no partition slot
RAW_PART, then create a partition RAW_PART. Make it span
the whole drive.
5 If there are fewer than MAXPARTITIONS partitions,
then "slide" the unsuitable raw partition RAW_PART, and
subsequent partitions, into partition slots RAW_PART+1
and subsequent slots. Create a raw partition at RAW_PART.
Make it span the whole drive.
The convertdisklabel() procedure can probably stand to be simplified,
but it ought to deal with all but an extraordinarily broken disklabel,
now.
i386: compiled and tested, sparc64: compiled, evbmips: compiled.
2007-06-24 05:43:34 +04:00
|
|
|
}
|
|
|
|
|
2004-10-23 21:16:45 +04:00
|
|
|
/*
|
|
|
|
* dkwedge_set_bootwedge
|
|
|
|
*
|
|
|
|
* Set the booted_wedge global based on the specified parent name
|
|
|
|
* and offset/length.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dkwedge_set_bootwedge(struct device *parent, daddr_t startblk, uint64_t nblks)
|
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc;
|
|
|
|
int i;
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_enter(&dkwedges_lock, RW_WRITER);
|
2004-10-23 21:16:45 +04:00
|
|
|
for (i = 0; i < ndkwedges; i++) {
|
|
|
|
if ((sc = dkwedges[i]) == NULL)
|
|
|
|
continue;
|
2008-04-06 12:33:31 +04:00
|
|
|
if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 &&
|
2004-10-23 21:16:45 +04:00
|
|
|
sc->sc_offset == startblk &&
|
|
|
|
sc->sc_size == nblks) {
|
|
|
|
if (booted_wedge) {
|
|
|
|
printf("WARNING: double match for boot wedge "
|
|
|
|
"(%s, %s)\n",
|
2008-04-06 12:33:31 +04:00
|
|
|
device_xname(booted_wedge),
|
|
|
|
device_xname(sc->sc_dev));
|
2004-10-23 21:16:45 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
booted_device = parent;
|
|
|
|
booted_wedge = sc->sc_dev;
|
|
|
|
booted_partition = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* XXX What if we don't find one? Should we create a special
|
|
|
|
* XXX root wedge?
|
|
|
|
*/
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedges_lock);
|
2004-10-23 21:16:45 +04:00
|
|
|
}
|
|
|
|
|
2004-10-01 09:16:04 +04:00
|
|
|
/*
|
2006-09-18 11:47:13 +04:00
|
|
|
* We need a dummy object to stuff into the dkwedge discovery method link
|
2004-10-01 09:16:04 +04:00
|
|
|
* set to ensure that there is always at least one object in the set.
|
|
|
|
*/
|
|
|
|
static struct dkwedge_discovery_method dummy_discovery_method;
|
|
|
|
__link_set_add_bss(dkwedge_methods, dummy_discovery_method);
|
|
|
|
|
|
|
|
/*
|
2007-07-21 23:51:47 +04:00
|
|
|
* dkwedge_init:
|
2004-10-01 09:16:04 +04:00
|
|
|
*
|
2007-07-21 23:51:47 +04:00
|
|
|
* Initialize the disk wedge subsystem.
|
2004-10-01 09:16:04 +04:00
|
|
|
*/
|
2007-07-21 23:51:47 +04:00
|
|
|
void
|
|
|
|
dkwedge_init(void)
|
2004-10-01 09:16:04 +04:00
|
|
|
{
|
|
|
|
__link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
|
|
|
|
struct dkwedge_discovery_method * const *ddmp;
|
|
|
|
struct dkwedge_discovery_method *lddm, *ddm;
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_init(&dkwedges_lock);
|
|
|
|
rw_init(&dkwedge_discovery_methods_lock);
|
2004-10-01 09:16:04 +04:00
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
if (config_cfdriver_attach(&dk_cd) != 0)
|
|
|
|
panic("dkwedge: unable to attach cfdriver");
|
|
|
|
if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0)
|
|
|
|
panic("dkwedge: unable to attach cfattach");
|
|
|
|
|
|
|
|
rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER);
|
2004-10-01 09:16:04 +04:00
|
|
|
|
|
|
|
LIST_INIT(&dkwedge_discovery_methods);
|
|
|
|
|
|
|
|
__link_set_foreach(ddmp, dkwedge_methods) {
|
|
|
|
ddm = *ddmp;
|
|
|
|
if (ddm == &dummy_discovery_method)
|
|
|
|
continue;
|
|
|
|
if (LIST_EMPTY(&dkwedge_discovery_methods)) {
|
|
|
|
LIST_INSERT_HEAD(&dkwedge_discovery_methods,
|
|
|
|
ddm, ddm_list);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
|
|
|
|
if (ddm->ddm_priority == lddm->ddm_priority) {
|
|
|
|
aprint_error("dk-method-%s: method \"%s\" "
|
|
|
|
"already exists at priority %d\n",
|
|
|
|
ddm->ddm_name, lddm->ddm_name,
|
|
|
|
lddm->ddm_priority);
|
|
|
|
/* Not inserted. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ddm->ddm_priority < lddm->ddm_priority) {
|
|
|
|
/* Higher priority; insert before. */
|
|
|
|
LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (LIST_NEXT(lddm, ddm_list) == NULL) {
|
|
|
|
/* Last one; insert after. */
|
|
|
|
KASSERT(lddm->ddm_priority < ddm->ddm_priority);
|
|
|
|
LIST_INSERT_AFTER(lddm, ddm, ddm_list);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedge_discovery_methods_lock);
|
2004-10-01 09:16:04 +04:00
|
|
|
}
|
|
|
|
|
2004-09-25 20:42:15 +04:00
|
|
|
#ifdef DKWEDGE_AUTODISCOVER
|
2004-10-01 09:16:04 +04:00
|
|
|
int dkwedge_autodiscover = 1;
|
|
|
|
#else
|
|
|
|
int dkwedge_autodiscover = 0;
|
2004-09-25 20:42:15 +04:00
|
|
|
#endif
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_discover: [exported function]
|
|
|
|
*
|
|
|
|
* Discover the wedges on a newly attached disk.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dkwedge_discover(struct disk *pdk)
|
|
|
|
{
|
2004-10-01 09:16:04 +04:00
|
|
|
struct dkwedge_discovery_method *ddm;
|
|
|
|
struct vnode *vp;
|
|
|
|
int error;
|
|
|
|
dev_t pdev;
|
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
/*
|
|
|
|
* Require people playing with wedges to enable this explicitly.
|
|
|
|
*/
|
2004-10-01 09:16:04 +04:00
|
|
|
if (dkwedge_autodiscover == 0)
|
|
|
|
return;
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
|
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to compute pdev, error = %d\n",
|
|
|
|
pdk->dk_name, error);
|
2004-10-01 09:16:04 +04:00
|
|
|
goto out;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
error = bdevvp(pdev, &vp);
|
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to find vnode for pdev, error = %d\n",
|
|
|
|
pdk->dk_name, error);
|
2004-10-01 09:16:04 +04:00
|
|
|
goto out;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
|
|
|
|
pdk->dk_name, error);
|
|
|
|
vrele(vp);
|
2004-10-01 09:16:04 +04:00
|
|
|
goto out;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
|
2007-11-26 22:01:26 +03:00
|
|
|
error = VOP_OPEN(vp, FREAD, NOCRED);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to open device, error = %d\n",
|
|
|
|
pdk->dk_name, error);
|
|
|
|
vput(vp);
|
2004-10-01 09:16:04 +04:00
|
|
|
goto out;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
VOP_UNLOCK(vp, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For each supported partition map type, look to see if
|
|
|
|
* this map type exists. If so, parse it and add the
|
|
|
|
* corresponding wedges.
|
|
|
|
*/
|
2004-10-01 09:16:04 +04:00
|
|
|
LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
|
|
|
|
error = (*ddm->ddm_discover)(pdk, vp);
|
|
|
|
if (error == 0) {
|
|
|
|
/* Successfully created wedges; we're done. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2008-03-22 00:54:58 +03:00
|
|
|
error = vn_close(vp, FREAD, NOCRED);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to close device, error = %d\n",
|
|
|
|
pdk->dk_name, error);
|
|
|
|
/* We'll just assume the vnode has been cleaned up. */
|
|
|
|
}
|
2004-10-01 09:16:04 +04:00
|
|
|
out:
|
2007-07-21 23:51:47 +04:00
|
|
|
rw_exit(&dkwedge_discovery_methods_lock);
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_read:
|
|
|
|
*
|
2008-04-10 13:31:51 +04:00
|
|
|
* Read some data from the specified disk, used for
|
2004-09-25 07:30:44 +04:00
|
|
|
* partition discovery.
|
|
|
|
*/
|
2004-10-01 09:16:04 +04:00
|
|
|
int
|
2006-11-16 04:32:37 +03:00
|
|
|
dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
|
2006-10-12 05:30:41 +04:00
|
|
|
void *tbuf, size_t len)
|
2004-09-25 07:30:44 +04:00
|
|
|
{
|
2008-06-03 16:14:08 +04:00
|
|
|
struct buf *bp;
|
2008-06-01 15:38:26 +04:00
|
|
|
int result;
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2008-06-03 16:14:08 +04:00
|
|
|
bp = getiobuf(vp, true);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2008-06-03 16:14:08 +04:00
|
|
|
bp->b_dev = vp->v_rdev;
|
|
|
|
bp->b_blkno = blkno;
|
|
|
|
bp->b_bcount = len;
|
|
|
|
bp->b_resid = len;
|
|
|
|
bp->b_flags = B_READ;
|
|
|
|
bp->b_data = tbuf;
|
2008-06-17 18:53:10 +04:00
|
|
|
SET(bp->b_cflags, BC_BUSY); /* mark buffer busy */
|
2008-06-03 16:14:08 +04:00
|
|
|
|
|
|
|
VOP_STRATEGY(vp, bp);
|
|
|
|
result = biowait(bp);
|
|
|
|
putiobuf(bp);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2008-06-01 15:38:26 +04:00
|
|
|
return result;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwedge_lookup:
|
|
|
|
*
|
|
|
|
* Look up a dkwedge_softc based on the provided dev_t.
|
|
|
|
*/
|
|
|
|
static struct dkwedge_softc *
|
|
|
|
dkwedge_lookup(dev_t dev)
|
|
|
|
{
|
2004-10-23 21:16:45 +04:00
|
|
|
int unit = minor(dev);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
if (unit >= ndkwedges)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
KASSERT(dkwedges != NULL);
|
|
|
|
|
|
|
|
return (dkwedges[unit]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkopen: [devsw entry point]
|
|
|
|
*
|
|
|
|
* Open a wedge.
|
|
|
|
*/
|
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
|
2004-09-25 07:30:44 +04:00
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(dev);
|
|
|
|
struct vnode *vp;
|
2006-04-06 21:55:43 +04:00
|
|
|
int error = 0;
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return (ENODEV);
|
|
|
|
|
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We go through a complicated little dance to only open the parent
|
|
|
|
* vnode once per wedge, no matter how many times the wedge is
|
|
|
|
* opened. The reason? We see one dkopen() per open call, but
|
|
|
|
* only dkclose() on the last close.
|
|
|
|
*/
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&sc->sc_dk.dk_openlock);
|
|
|
|
mutex_enter(&sc->sc_parent->dk_rawlock);
|
2004-10-23 21:16:45 +04:00
|
|
|
if (sc->sc_dk.dk_openmask == 0) {
|
2007-06-09 06:26:27 +04:00
|
|
|
if (sc->sc_parent->dk_rawopens == 0) {
|
2004-09-25 07:30:44 +04:00
|
|
|
KASSERT(sc->sc_parent->dk_rawvp == NULL);
|
|
|
|
error = bdevvp(sc->sc_pdev, &vp);
|
|
|
|
if (error)
|
|
|
|
goto popen_fail;
|
|
|
|
error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
|
|
|
if (error) {
|
|
|
|
vrele(vp);
|
|
|
|
goto popen_fail;
|
|
|
|
}
|
2007-11-26 22:01:26 +03:00
|
|
|
error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (error) {
|
|
|
|
vput(vp);
|
|
|
|
goto popen_fail;
|
|
|
|
}
|
|
|
|
/* VOP_OPEN() doesn't do this for us. */
|
2008-01-30 18:42:52 +03:00
|
|
|
mutex_enter(&vp->v_interlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
vp->v_writecount++;
|
2008-01-30 18:42:52 +03:00
|
|
|
mutex_exit(&vp->v_interlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
VOP_UNLOCK(vp, 0);
|
|
|
|
sc->sc_parent->dk_rawvp = vp;
|
|
|
|
}
|
2007-06-16 22:11:33 +04:00
|
|
|
sc->sc_parent->dk_rawopens++;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
2006-08-25 01:57:11 +04:00
|
|
|
if (fmt == S_IFCHR)
|
|
|
|
sc->sc_dk.dk_copenmask |= 1;
|
|
|
|
else
|
|
|
|
sc->sc_dk.dk_bopenmask |= 1;
|
|
|
|
sc->sc_dk.dk_openmask =
|
|
|
|
sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
popen_fail:
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&sc->sc_parent->dk_rawlock);
|
|
|
|
mutex_exit(&sc->sc_dk.dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkclose: [devsw entry point]
|
|
|
|
*
|
|
|
|
* Close a wedge.
|
|
|
|
*/
|
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
|
2004-09-25 07:30:44 +04:00
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(dev);
|
|
|
|
int error = 0;
|
|
|
|
|
2004-10-23 21:16:45 +04:00
|
|
|
KASSERT(sc->sc_dk.dk_openmask != 0);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&sc->sc_dk.dk_openlock);
|
|
|
|
mutex_enter(&sc->sc_parent->dk_rawlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2004-10-23 21:16:45 +04:00
|
|
|
if (fmt == S_IFCHR)
|
|
|
|
sc->sc_dk.dk_copenmask &= ~1;
|
|
|
|
else
|
|
|
|
sc->sc_dk.dk_bopenmask &= ~1;
|
|
|
|
sc->sc_dk.dk_openmask =
|
|
|
|
sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
|
|
|
|
|
|
|
|
if (sc->sc_dk.dk_openmask == 0) {
|
|
|
|
if (sc->sc_parent->dk_rawopens-- == 1) {
|
|
|
|
KASSERT(sc->sc_parent->dk_rawvp != NULL);
|
|
|
|
error = vn_close(sc->sc_parent->dk_rawvp,
|
2008-03-22 00:54:58 +03:00
|
|
|
FREAD | FWRITE, NOCRED);
|
2004-10-23 21:16:45 +04:00
|
|
|
sc->sc_parent->dk_rawvp = NULL;
|
|
|
|
}
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&sc->sc_parent->dk_rawlock);
|
|
|
|
mutex_exit(&sc->sc_dk.dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkstragegy: [devsw entry point]
|
|
|
|
*
|
|
|
|
* Perform I/O based on the wedge I/O strategy.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dkstrategy(struct buf *bp)
|
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING) {
|
|
|
|
bp->b_error = ENXIO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it's an empty transfer, wake up the top half now. */
|
|
|
|
if (bp->b_bcount == 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Make sure it's in-range. */
|
|
|
|
if (bounds_check_with_mediasize(bp, DEV_BSIZE, sc->sc_size) <= 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Translate it to the parent's raw LBA. */
|
|
|
|
bp->b_rawblkno = bp->b_blkno + sc->sc_offset;
|
|
|
|
|
|
|
|
/* Place it in the queue and start I/O on the unit. */
|
|
|
|
s = splbio();
|
|
|
|
sc->sc_iopend++;
|
2009-01-13 16:33:58 +03:00
|
|
|
bufq_put(sc->sc_bufq, bp);
|
2004-09-25 07:30:44 +04:00
|
|
|
dkstart(sc);
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
|
|
|
|
done:
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkstart:
|
|
|
|
*
|
|
|
|
* Start I/O that has been enqueued on the wedge.
|
|
|
|
* NOTE: Must be called at splbio()!
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dkstart(struct dkwedge_softc *sc)
|
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
struct vnode *vp;
|
2004-09-25 07:30:44 +04:00
|
|
|
struct buf *bp, *nbp;
|
|
|
|
|
|
|
|
/* Do as much work as has been enqueued. */
|
2009-01-13 16:33:58 +03:00
|
|
|
while ((bp = bufq_peek(sc->sc_bufq)) != NULL) {
|
2004-09-25 07:30:44 +04:00
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING) {
|
2009-01-13 16:33:58 +03:00
|
|
|
(void) bufq_get(sc->sc_bufq);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (sc->sc_iopend-- == 1 &&
|
|
|
|
(sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
|
|
|
|
sc->sc_flags &= ~DK_F_WAIT_DRAIN;
|
|
|
|
wakeup(&sc->sc_iopend);
|
|
|
|
}
|
|
|
|
bp->b_error = ENXIO;
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Instrumentation. */
|
|
|
|
disk_busy(&sc->sc_dk);
|
2005-02-27 03:26:58 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
nbp = getiobuf(sc->sc_parent->dk_rawvp, false);
|
2004-09-25 07:30:44 +04:00
|
|
|
if (nbp == NULL) {
|
|
|
|
/*
|
|
|
|
* No resources to run this request; leave the
|
|
|
|
* buffer queued up, and schedule a timer to
|
|
|
|
* restart the queue in 1/2 a second.
|
|
|
|
*/
|
|
|
|
disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
|
|
|
|
callout_schedule(&sc->sc_restart_ch, hz / 2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-13 16:33:58 +03:00
|
|
|
(void) bufq_get(sc->sc_bufq);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
nbp->b_data = bp->b_data;
|
2008-01-02 14:48:20 +03:00
|
|
|
nbp->b_flags = bp->b_flags;
|
|
|
|
nbp->b_oflags = bp->b_oflags;
|
|
|
|
nbp->b_cflags = bp->b_cflags;
|
2004-09-25 07:30:44 +04:00
|
|
|
nbp->b_iodone = dkiodone;
|
|
|
|
nbp->b_proc = bp->b_proc;
|
|
|
|
nbp->b_blkno = bp->b_rawblkno;
|
|
|
|
nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
|
|
|
|
nbp->b_bcount = bp->b_bcount;
|
|
|
|
nbp->b_private = bp;
|
|
|
|
BIO_COPYPRIO(nbp, bp);
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
vp = nbp->b_vp;
|
|
|
|
if ((nbp->b_flags & B_READ) == 0) {
|
|
|
|
mutex_enter(&vp->v_interlock);
|
|
|
|
vp->v_numoutput++;
|
|
|
|
mutex_exit(&vp->v_interlock);
|
|
|
|
}
|
|
|
|
VOP_STRATEGY(vp, nbp);
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkiodone:
|
|
|
|
*
|
|
|
|
* I/O to a wedge has completed; alert the top half.
|
|
|
|
* NOTE: Must be called at splbio()!
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dkiodone(struct buf *bp)
|
|
|
|
{
|
|
|
|
struct buf *obp = bp->b_private;
|
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
|
|
|
|
|
2007-07-29 16:50:17 +04:00
|
|
|
if (bp->b_error != 0)
|
2004-09-25 07:30:44 +04:00
|
|
|
obp->b_error = bp->b_error;
|
|
|
|
obp->b_resid = bp->b_resid;
|
2006-01-04 13:13:05 +03:00
|
|
|
putiobuf(bp);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
|
|
|
if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
|
|
|
|
sc->sc_flags &= ~DK_F_WAIT_DRAIN;
|
|
|
|
wakeup(&sc->sc_iopend);
|
|
|
|
}
|
|
|
|
|
|
|
|
disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
|
|
|
|
obp->b_flags & B_READ);
|
|
|
|
|
|
|
|
biodone(obp);
|
|
|
|
|
|
|
|
/* Kick the queue in case there is more work we can do. */
|
|
|
|
dkstart(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkrestart:
|
|
|
|
*
|
|
|
|
* Restart the work queue after it was stalled due to
|
|
|
|
* a resource shortage. Invoked via a callout.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dkrestart(void *v)
|
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc = v;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
dkstart(sc);
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkread: [devsw entry point]
|
|
|
|
*
|
|
|
|
* Read from a wedge.
|
|
|
|
*/
|
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
dkread(dev_t dev, struct uio *uio, int flags)
|
2004-09-25 07:30:44 +04:00
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(dev);
|
|
|
|
|
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING)
|
|
|
|
return (ENXIO);
|
2005-02-27 03:26:58 +03:00
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
return (physio(dkstrategy, NULL, dev, B_READ,
|
|
|
|
sc->sc_parent->dk_driver->d_minphys, uio));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkwrite: [devsw entry point]
|
|
|
|
*
|
|
|
|
* Write to a wedge.
|
|
|
|
*/
|
|
|
|
static int
|
2006-11-16 04:32:37 +03:00
|
|
|
dkwrite(dev_t dev, struct uio *uio, int flags)
|
2004-09-25 07:30:44 +04:00
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(dev);
|
|
|
|
|
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING)
|
|
|
|
return (ENXIO);
|
2005-02-27 03:26:58 +03:00
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
return (physio(dkstrategy, NULL, dev, B_WRITE,
|
|
|
|
sc->sc_parent->dk_driver->d_minphys, uio));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkioctl: [devsw entry point]
|
|
|
|
*
|
|
|
|
* Perform an ioctl request on a wedge.
|
|
|
|
*/
|
|
|
|
static int
|
2007-03-04 08:59:00 +03:00
|
|
|
dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
|
2004-09-25 07:30:44 +04:00
|
|
|
{
|
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(dev);
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
switch (cmd) {
|
2004-10-27 02:50:16 +04:00
|
|
|
case DIOCCACHESYNC:
|
|
|
|
/*
|
|
|
|
* XXX Do we really need to care about having a writable
|
|
|
|
* file descriptor here?
|
|
|
|
*/
|
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
error = EBADF;
|
|
|
|
else
|
|
|
|
error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
|
|
|
|
cmd, data, flag,
|
2007-11-26 22:01:26 +03:00
|
|
|
l != NULL ? l->l_cred : NOCRED);
|
2004-10-27 02:50:16 +04:00
|
|
|
break;
|
2004-09-25 07:30:44 +04:00
|
|
|
case DIOCGWEDGEINFO:
|
|
|
|
{
|
|
|
|
struct dkwedge_info *dkw = (void *) data;
|
|
|
|
|
2008-04-06 12:33:31 +04:00
|
|
|
strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
|
|
|
|
sizeof(dkw->dkw_devname));
|
2004-09-25 07:30:44 +04:00
|
|
|
memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
|
|
|
|
dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
|
|
|
|
strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
|
|
|
|
dkw->dkw_offset = sc->sc_offset;
|
|
|
|
dkw->dkw_size = sc->sc_size;
|
|
|
|
strcpy(dkw->dkw_ptype, sc->sc_ptype);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dksize: [devsw entry point]
|
|
|
|
*
|
|
|
|
* Query the size of a wedge for the purpose of performing a dump
|
|
|
|
* or for swapping to.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dksize(dev_t dev)
|
|
|
|
{
|
2006-04-06 21:17:45 +04:00
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(dev);
|
|
|
|
int rv = -1;
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2006-04-06 21:17:45 +04:00
|
|
|
if (sc == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING)
|
|
|
|
return (ENXIO);
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&sc->sc_dk.dk_openlock);
|
|
|
|
mutex_enter(&sc->sc_parent->dk_rawlock);
|
2006-04-06 21:17:45 +04:00
|
|
|
|
|
|
|
/* Our content type is static, no need to open the device. */
|
|
|
|
|
|
|
|
if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
|
|
|
|
/* Saturate if we are larger than INT_MAX. */
|
|
|
|
if (sc->sc_size > INT_MAX)
|
|
|
|
rv = INT_MAX;
|
|
|
|
else
|
|
|
|
rv = (int) sc->sc_size;
|
|
|
|
}
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&sc->sc_parent->dk_rawlock);
|
|
|
|
mutex_exit(&sc->sc_dk.dk_openlock);
|
2006-04-06 21:17:45 +04:00
|
|
|
|
|
|
|
return (rv);
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dkdump: [devsw entry point]
|
|
|
|
*
|
|
|
|
* Perform a crash dump to a wedge.
|
|
|
|
*/
|
|
|
|
static int
|
2007-06-09 06:26:27 +04:00
|
|
|
dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
|
2004-09-25 07:30:44 +04:00
|
|
|
{
|
2007-06-09 06:26:27 +04:00
|
|
|
struct dkwedge_softc *sc = dkwedge_lookup(dev);
|
|
|
|
const struct bdevsw *bdev;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
if (sc == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (sc->sc_state != DKW_STATE_RUNNING)
|
|
|
|
return (ENXIO);
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&sc->sc_dk.dk_openlock);
|
|
|
|
mutex_enter(&sc->sc_parent->dk_rawlock);
|
2007-06-09 06:26:27 +04:00
|
|
|
|
|
|
|
/* Our content type is static, no need to open the device. */
|
|
|
|
|
|
|
|
if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0) {
|
|
|
|
rv = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (size % DEV_BSIZE != 0) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (blkno + size / DEV_BSIZE > sc->sc_size) {
|
|
|
|
printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
|
|
|
|
"sc->sc_size (%" PRIu64 ")\n", __func__, blkno,
|
|
|
|
size / DEV_BSIZE, sc->sc_size);
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bdev = bdevsw_lookup(sc->sc_pdev);
|
|
|
|
rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size);
|
|
|
|
|
|
|
|
out:
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&sc->sc_parent->dk_rawlock);
|
|
|
|
mutex_exit(&sc->sc_dk.dk_openlock);
|
2004-09-25 07:30:44 +04:00
|
|
|
|
2007-06-09 06:26:27 +04:00
|
|
|
return rv;
|
2004-09-25 07:30:44 +04:00
|
|
|
}
|