Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/dev/dkwedge Implement DIOCMWEDGES ioctl that triggers we...
details: https://anonhg.NetBSD.org/src/rev/f97315f864c5
branches: trunk
changeset: 333436:f97315f864c5
user: mlelstv <mlelstv%NetBSD.org@localhost>
date: Tue Nov 04 07:50:39 2014 +0000
description:
Implement DIOCMWEDGES ioctl that triggers wedge autodiscovery.
Also fix a reference counting bug and clean up some code.
diffstat:
sys/dev/dkwedge/dk.c | 150 +++++++++++++++++++++++++++++++++++---------------
1 files changed, 103 insertions(+), 47 deletions(-)
diffs (truncated from 301 to 300 lines):
diff -r 6c1f5aa02ab0 -r f97315f864c5 sys/dev/dkwedge/dk.c
--- a/sys/dev/dkwedge/dk.c Tue Nov 04 07:46:26 2014 +0000
+++ b/sys/dev/dkwedge/dk.c Tue Nov 04 07:50:39 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: dk.c,v 1.73 2014/08/28 19:37:46 riastradh Exp $ */
+/* $NetBSD: dk.c,v 1.74 2014/11/04 07:50:39 mlelstv Exp $ */
/*-
* Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.73 2014/08/28 19:37:46 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.74 2014/11/04 07:50:39 mlelstv Exp $");
#ifdef _KERNEL_OPT
#include "opt_dkwedge.h"
@@ -98,7 +98,10 @@
static void dkminphys(struct buf *);
static int dklastclose(struct dkwedge_softc *);
+static int dkwedge_cleanup_parent(struct dkwedge_softc *, int);
static int dkwedge_detach(device_t, int);
+static void dkwedge_delall1(struct disk *, bool);
+static int dkwedge_del1(struct dkwedge_info *, int);
static dev_type_open(dkopen);
static dev_type_close(dkclose);
@@ -198,7 +201,7 @@
* Compute the parent disk's dev_t.
*/
static int
-dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
+dkwedge_compute_pdev(const char *pname, dev_t *pdevp, enum vtype type)
{
const char *name, *cp;
devmajor_t pmaj;
@@ -206,7 +209,18 @@
char devname[16];
name = pname;
- if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
+ switch (type) {
+ case VBLK:
+ pmaj = devsw_name2blk(name, devname, sizeof(devname));
+ break;
+ case VCHR:
+ pmaj = devsw_name2chr(name, devname, sizeof(devname));
+ break;
+ default:
+ pmaj = -1;
+ break;
+ }
+ if (pmaj == -1)
return (ENODEV);
name += strlen(devname);
@@ -282,7 +296,7 @@
if (pdk == NULL)
return (ENODEV);
- error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
+ error = dkwedge_compute_pdev(pdk->dk_name, &pdev, VBLK);
if (error)
return (error);
@@ -331,9 +345,14 @@
break;
}
}
- if (lsc != NULL)
- error = EINVAL;
- else {
+ if (lsc != NULL) {
+ if (sc->sc_offset == lsc->sc_offset &&
+ sc->sc_size == lsc->sc_size &&
+ strcmp(sc->sc_wname, lsc->sc_wname) == 0)
+ error = EEXIST;
+ else
+ error = EINVAL;
+ } else {
pdk->dk_nwedges++;
LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
}
@@ -497,17 +516,23 @@
int
dkwedge_del(struct dkwedge_info *dkw)
{
+ return dkwedge_del1(dkw, 0);
+}
+
+int
+dkwedge_del1(struct dkwedge_info *dkw, int flags)
+{
struct dkwedge_softc *sc = NULL;
/* Find our softc. */
if ((sc = dkwedge_find(dkw, NULL)) == NULL)
return (ESRCH);
- return config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET);
+ return config_detach(sc->sc_dev, flags);
}
static int
-dkwedge_begindetach(struct dkwedge_softc *sc, int flags)
+dkwedge_cleanup_parent(struct dkwedge_softc *sc, int flags)
{
struct disk *dk = &sc->sc_dk;
int rc;
@@ -546,7 +571,7 @@
}
if (unit == ndkwedges)
rc = ENXIO;
- else if ((rc = dkwedge_begindetach(sc, flags)) == 0) {
+ else if ((rc = dkwedge_cleanup_parent(sc, flags)) == 0) {
/* Mark the wedge as dying. */
sc->sc_state = DKW_STATE_DYING;
}
@@ -579,20 +604,7 @@
vdevgone(cmaj, unit, unit, VCHR);
/* Clean up the parent. */
- mutex_enter(&sc->sc_dk.dk_openlock);
- if (sc->sc_dk.dk_openmask) {
- mutex_enter(&sc->sc_parent->dk_rawlock);
- if (sc->sc_parent->dk_rawopens-- == 1) {
- KASSERT(sc->sc_parent->dk_rawvp != NULL);
- mutex_exit(&sc->sc_parent->dk_rawlock);
- (void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
- NOCRED);
- sc->sc_parent->dk_rawvp = NULL;
- } else
- mutex_exit(&sc->sc_parent->dk_rawlock);
- sc->sc_dk.dk_openmask = 0;
- }
- mutex_exit(&sc->sc_dk.dk_openlock);
+ dkwedge_cleanup_parent(sc, flags | DETACH_FORCE);
/* Announce our departure. */
aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
@@ -631,13 +643,27 @@
void
dkwedge_delall(struct disk *pdk)
{
+ dkwedge_delall1(pdk, false);
+}
+
+static void
+dkwedge_delall1(struct disk *pdk, bool idleonly)
+{
struct dkwedge_info dkw;
struct dkwedge_softc *sc;
+ int flags;
+
+ flags = DETACH_QUIET;
+ if (!idleonly) flags |= DETACH_FORCE;
for (;;) {
mutex_enter(&pdk->dk_openlock);
- if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
- KASSERT(pdk->dk_nwedges == 0);
+ LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
+ if (!idleonly || sc->sc_dk.dk_openmask == 0)
+ break;
+ }
+ if (sc == NULL) {
+ KASSERT(idleonly || pdk->dk_nwedges == 0);
mutex_exit(&pdk->dk_openlock);
return;
}
@@ -645,7 +671,7 @@
strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
sizeof(dkw.dkw_devname));
mutex_exit(&pdk->dk_openlock);
- (void) dkwedge_del(&dkw);
+ (void) dkwedge_del1(&dkw, flags);
}
}
@@ -823,6 +849,7 @@
* dkwedge_discover: [exported function]
*
* Discover the wedges on a newly attached disk.
+ * Remove all unused wedges on the disk first.
*/
void
dkwedge_discover(struct disk *pdk)
@@ -840,14 +867,18 @@
rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
- error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
+ /*
+ * Use the character device for scanning, the block device
+ * is busy if there are already wedges attached.
+ */
+ error = dkwedge_compute_pdev(pdk->dk_name, &pdev, VCHR);
if (error) {
aprint_error("%s: unable to compute pdev, error = %d\n",
pdk->dk_name, error);
goto out;
}
- error = bdevvp(pdev, &vp);
+ error = cdevvp(pdev, &vp);
if (error) {
aprint_error("%s: unable to find vnode for pdev, error = %d\n",
pdk->dk_name, error);
@@ -873,6 +904,11 @@
VOP_UNLOCK(vp);
/*
+ * Remove unused wedges
+ */
+ dkwedge_delall1(pdk, true);
+
+ /*
* For each supported partition map type, look to see if
* this map type exists. If so, parse it and add the
* corresponding wedges.
@@ -905,24 +941,37 @@
dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
void *tbuf, size_t len)
{
- struct buf *bp;
- int result;
+ buf_t *bp;
+ int error;
+
+ /*
+ * The kernel cannot read from a character device vnode
+ * as physio() only handles user memory.
+ *
+ * Determine the corresponding block device and call into
+ * the driver directly.
+ */
bp = getiobuf(vp, true);
-
- bp->b_dev = vp->v_rdev;
+ bp->b_flags = B_READ;
+ bp->b_cflags = BC_BUSY;
+ bp->b_dev = devsw_chr2blk(vp->v_rdev);
+ bp->b_data = tbuf;
+ bp->b_bufsize = bp->b_resid = bp->b_bcount = len;
+ bp->b_lblkno = 0;
bp->b_blkno = blkno;
- bp->b_bcount = len;
- bp->b_resid = len;
- bp->b_flags = B_READ;
- bp->b_data = tbuf;
- SET(bp->b_cflags, BC_BUSY); /* mark buffer busy */
- VOP_STRATEGY(vp, bp);
- result = biowait(bp);
+ error = bdev_open(bp->b_dev, FREAD, S_IFBLK, curlwp);
+ if (error)
+ return error;
+
+ bdev_strategy(bp);
+ error = biowait(bp);
putiobuf(bp);
- return result;
+ bdev_close(bp->b_dev, FREAD, S_IFBLK, curlwp);
+
+ return error;
}
/*
@@ -1012,16 +1061,23 @@
static int
dklastclose(struct dkwedge_softc *sc)
{
- int error = 0;
+ int error = 0, doclose;
- if (sc->sc_parent->dk_rawopens-- == 1) {
+ doclose = 0;
+ if (sc->sc_parent->dk_rawopens > 0) {
+ if (--sc->sc_parent->dk_rawopens == 0)
+ doclose = 1;
+ }
+
+ mutex_exit(&sc->sc_parent->dk_rawlock);
+
+ if (doclose) {
KASSERT(sc->sc_parent->dk_rawvp != NULL);
- mutex_exit(&sc->sc_parent->dk_rawlock);
error = vn_close(sc->sc_parent->dk_rawvp,
FREAD | FWRITE, NOCRED);
sc->sc_parent->dk_rawvp = NULL;
- } else
- mutex_exit(&sc->sc_parent->dk_rawlock);
+ }
+
return error;
}
Home |
Main Index |
Thread Index |
Old Index