Replace home-grown locks with lockmgr().

This commit is contained in:
mycroft 2003-11-07 04:10:56 +00:00
parent bad71a3818
commit 552d27479a
3 changed files with 27 additions and 108 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: wd.c,v 1.266 2003/10/29 21:27:38 mycroft Exp $ */
/* $NetBSD: wd.c,v 1.267 2003/11/07 04:10:56 mycroft Exp $ */
/*
* Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
@ -66,7 +66,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.266 2003/10/29 21:27:38 mycroft Exp $");
__KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.267 2003/11/07 04:10:56 mycroft Exp $");
#ifndef WDCDEBUG
#define WDCDEBUG
@ -201,8 +201,6 @@ struct dkdriver wddkdriver = { wdstrategy };
#ifdef HAS_BAD144_HANDLING
static void bad144intern(struct wd_softc *);
#endif
int wdlock(struct wd_softc *);
void wdunlock(struct wd_softc *);
#define WD_QUIRK_SPLIT_MOD15_WRITE 0x0001 /* must split certain writes */
@ -278,6 +276,8 @@ wdattach(struct device *parent, struct device *self, void *aux)
const struct wd_quirk *wdq;
WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
lockinit(&wd->sc_lock, PRIBIO | PCATCH, "wdlock", 0, 0);
callout_init(&wd->sc_restart_ch);
#ifdef NEW_BUFQ_STRATEGY
bufq_alloc(&wd->sc_q, BUFQ_READ_PRIO|BUFQ_SORT_RAWBLOCK);
@ -420,6 +420,8 @@ wddetach(struct device *self, int flags)
struct buf *bp;
int s, bmaj, cmaj, i, mn;
lockmgr(&sc->sc_lock, LK_DRAIN, NULL);
/* Clean out the bad sector list */
while (!SLIST_EMPTY(&sc->sc_bslist)) {
void *head = SLIST_FIRST(&sc->sc_bslist);
@ -843,51 +845,6 @@ wdwrite(dev_t dev, struct uio *uio, int flags)
return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
}
/*
* Wait interruptibly for an exclusive lock.
*
* XXX
* Several drivers do this; it should be abstracted and made MP-safe.
*/
int
wdlock(struct wd_softc *wd)
{
int error;
int s;
WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
s = splbio();
while ((wd->sc_flags & WDF_LOCKED) != 0) {
wd->sc_flags |= WDF_WANTED;
if ((error = tsleep(wd, PRIBIO | PCATCH,
"wdlck", 0)) != 0) {
splx(s);
return error;
}
}
wd->sc_flags |= WDF_LOCKED;
splx(s);
return 0;
}
/*
* Unlock and wake up any waiters.
*/
void
wdunlock(struct wd_softc *wd)
{
WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
wd->sc_flags &= ~WDF_LOCKED;
if ((wd->sc_flags & WDF_WANTED) != 0) {
wd->sc_flags &= ~WDF_WANTED;
wakeup(wd);
}
}
int
wdopen(dev_t dev, int flag, int fmt, struct proc *p)
{
@ -907,7 +864,7 @@ wdopen(dev_t dev, int flag, int fmt, struct proc *p)
(error = wd->atabus->ata_addref(wd->drvp)) != 0)
return (error);
if ((error = wdlock(wd)) != 0)
if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
goto bad4;
if (wd->sc_dk.dk_openmask != 0) {
@ -953,7 +910,7 @@ wdopen(dev_t dev, int flag, int fmt, struct proc *p)
wd->sc_dk.dk_openmask =
wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
wdunlock(wd);
lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
return 0;
bad:
@ -961,7 +918,7 @@ bad:
}
bad3:
wdunlock(wd);
lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
bad4:
if (wd->sc_dk.dk_openmask == 0)
wd->atabus->ata_delref(wd->drvp);
@ -976,7 +933,7 @@ wdclose(dev_t dev, int flag, int fmt, struct proc *p)
int error;
WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
if ((error = wdlock(wd)) != 0)
if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
return error;
switch (fmt) {
@ -1000,7 +957,7 @@ wdclose(dev_t dev, int flag, int fmt, struct proc *p)
wd->atabus->ata_delref(wd->drvp);
}
wdunlock(wd);
lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
return 0;
}
@ -1256,7 +1213,7 @@ wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct proc *p)
#endif
lp = (struct disklabel *)addr;
if ((error = wdlock(wd)) != 0)
if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
goto bad;
wd->sc_flags |= WDF_LABELLING;
@ -1277,7 +1234,7 @@ wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct proc *p)
}
wd->sc_flags &= ~WDF_LABELLING;
wdunlock(wd);
lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
bad:
#ifdef __HAVE_OLD_DISKLABEL
if (newlabel != NULL)

View File

@ -1,4 +1,4 @@
/* $NetBSD: wdvar.h,v 1.20 2003/11/02 09:52:33 wiz Exp $ */
/* $NetBSD: wdvar.h,v 1.21 2003/11/07 04:10:56 mycroft Exp $ */
/*
* Copyright (c) 1998, 2001 Manuel Bouyer.
@ -102,6 +102,7 @@ struct wd_softc {
/* General disk infos */
struct device sc_dev;
struct disk sc_dk;
struct lock sc_lock;
struct bufq_state sc_q;
struct callout sc_restart_ch;
int sc_quirks; /* any quirks drive might have */
@ -113,8 +114,6 @@ struct wd_softc {
int openings;
struct ataparams sc_params;/* drive characteristics found */
int sc_flags;
#define WDF_LOCKED 0x001
#define WDF_WANTED 0x002
#define WDF_WLABEL 0x004 /* label is writable */
#define WDF_LABELLING 0x008 /* writing label */
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: mcd.c,v 1.84 2003/06/29 22:30:20 fvdl Exp $ */
/* $NetBSD: mcd.c,v 1.85 2003/11/07 04:10:57 mycroft Exp $ */
/*
* Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
@ -56,7 +56,7 @@
/*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mcd.c,v 1.84 2003/06/29 22:30:20 fvdl Exp $");
__KERNEL_RCSID(0, "$NetBSD: mcd.c,v 1.85 2003/11/07 04:10:57 mycroft Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -121,6 +121,7 @@ struct mcd_mbx {
struct mcd_softc {
struct device sc_dev;
struct disk sc_dk;
struct lock sc_lock;
void *sc_ih;
struct callout sc_pintr_ch;
@ -132,8 +133,6 @@ struct mcd_softc {
char *type;
int flags;
#define MCDF_LOCKED 0x01
#define MCDF_WANTED 0x02
#define MCDF_WLABEL 0x04 /* label is writable */
#define MCDF_LABELLING 0x08 /* writing label */
#define MCDF_LOADED 0x10 /* parameters loaded */
@ -218,8 +217,6 @@ void mcdgetdefaultlabel __P((struct mcd_softc *, struct disklabel *));
void mcdgetdisklabel __P((struct mcd_softc *));
int mcd_get_parms __P((struct mcd_softc *));
void mcdstart __P((struct mcd_softc *));
int mcdlock __P((struct mcd_softc *));
void mcdunlock __P((struct mcd_softc *));
void mcd_pseudointr __P((void *));
struct dkdriver mcddkdriver = { mcdstrategy };
@ -251,6 +248,8 @@ mcdattach(parent, self, aux)
return;
}
lockinit(&sc->sc_lock, PRIBIO | PCATCH, "mcdlock", 0, 0);
sc->sc_iot = iot;
sc->sc_ioh = ioh;
@ -289,42 +288,6 @@ mcdattach(parent, self, aux)
IST_EDGE, IPL_BIO, mcdintr, sc);
}
/*
* Wait interruptibly for an exclusive lock.
*
* XXX
* Several drivers do this; it should be abstracted and made MP-safe.
*/
int
mcdlock(sc)
struct mcd_softc *sc;
{
int error;
while ((sc->flags & MCDF_LOCKED) != 0) {
sc->flags |= MCDF_WANTED;
if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
return error;
}
sc->flags |= MCDF_LOCKED;
return 0;
}
/*
* Unlock and wake up any waiters.
*/
void
mcdunlock(sc)
struct mcd_softc *sc;
{
sc->flags &= ~MCDF_LOCKED;
if ((sc->flags & MCDF_WANTED) != 0) {
sc->flags &= ~MCDF_WANTED;
wakeup(sc);
}
}
int
mcdopen(dev, flag, fmt, p)
dev_t dev;
@ -338,7 +301,7 @@ mcdopen(dev, flag, fmt, p)
if (sc == NULL)
return ENXIO;
if ((error = mcdlock(sc)) != 0)
if ((error = lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
return error;
if (sc->sc_dk.dk_openmask != 0) {
@ -408,7 +371,7 @@ mcdopen(dev, flag, fmt, p)
}
sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
mcdunlock(sc);
lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
return 0;
bad2:
@ -423,7 +386,7 @@ bad:
}
bad3:
mcdunlock(sc);
lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
return error;
}
@ -439,7 +402,7 @@ mcdclose(dev, flag, fmt, p)
MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
if ((error = mcdlock(sc)) != 0)
if ((error = lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
return error;
switch (fmt) {
@ -461,7 +424,7 @@ mcdclose(dev, flag, fmt, p)
(void) mcd_setlock(sc, MCD_LK_UNLOCK);
}
mcdunlock(sc);
lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
return 0;
}
@ -662,7 +625,7 @@ mcdioctl(dev, cmd, addr, flag, p)
#endif
lp = (struct disklabel *)addr;
if ((error = mcdlock(sc)) != 0)
if ((error = lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
return error;
sc->flags |= MCDF_LABELLING;
@ -673,7 +636,7 @@ mcdioctl(dev, cmd, addr, flag, p)
}
sc->flags &= ~MCDF_LABELLING;
mcdunlock(sc);
lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
return error;
}