Replace crufty use of lockmgr.

This commit is contained in:
ad 2008-01-30 14:08:00 +00:00
parent ff536c35f7
commit 9ef95126d8
4 changed files with 184 additions and 101 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: smbfs_vfsops.c,v 1.77 2008/01/28 14:31:17 dholland Exp $ */
/* $NetBSD: smbfs_vfsops.c,v 1.78 2008/01/30 14:08:00 ad Exp $ */
/*
* Copyright (c) 2000-2001, Boris Popov
@ -35,7 +35,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: smbfs_vfsops.c,v 1.77 2008/01/28 14:31:17 dholland Exp $");
__KERNEL_RCSID(0, "$NetBSD: smbfs_vfsops.c,v 1.78 2008/01/30 14:08:00 ad Exp $");
#ifdef _KERNEL_OPT
#include "opt_quota.h"
@ -180,7 +180,7 @@ smbfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
error = smb_dev2share(args->dev_fd, SMBM_EXEC, &scred, &ssp);
if (error)
return error;
smb_share_unlock(ssp, 0); /* keep ref, but unlock */
smb_share_unlock(ssp); /* keep ref, but unlock */
vcp = SSTOVC(ssp);
mp->mnt_stat.f_iosize = vcp->vc_txmax;
mp->mnt_stat.f_namemax =
@ -245,7 +245,7 @@ smbfs_unmount(struct mount *mp, int mntflags)
} while (error == EBUSY && smp->sm_didrele != 0);
smb_makescred(&scred, l, l->l_cred);
smb_share_lock(smp->sm_share, 0);
smb_share_lock(smp->sm_share);
smb_share_put(smp->sm_share, &scred);
mp->mnt_data = NULL;

View File

@ -1,4 +1,37 @@
/* $NetBSD: smb_conn.c,v 1.22 2006/11/16 01:33:51 christos Exp $ */
/* $NetBSD: smb_conn.c,v 1.23 2008/01/30 14:08:00 ad Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* 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.
*/
/*
* Copyright (c) 2000-2001 Boris Popov
@ -35,7 +68,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: smb_conn.c,v 1.22 2006/11/16 01:33:51 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: smb_conn.c,v 1.23 2008/01/30 14:08:00 ad Exp $");
/*
* Connection engine.
@ -70,9 +103,6 @@ MALLOC_DEFINE(M_SMBCONN, "SMB conn", "SMB connection");
static void smb_co_init(struct smb_connobj *cp, int level, const char *objname);
static void smb_co_done(struct smb_connobj *cp);
#ifdef DIAGNOSTIC
static int smb_co_lockstatus(struct smb_connobj *cp);
#endif
static int smb_vc_disconnect(struct smb_vc *vcp);
static void smb_vc_free(struct smb_connobj *cp);
@ -92,7 +122,9 @@ smb_sm_init(void)
{
smb_co_init(&smb_vclist, SMBL_SM, "smbsm");
smb_co_unlock(&smb_vclist, 0);
mutex_enter(&smb_vclist.co_interlock);
smb_co_unlock(&smb_vclist);
mutex_exit(&smb_vclist.co_interlock);
return 0;
}
@ -112,15 +144,22 @@ smb_sm_done(void)
static int
smb_sm_lockvclist(int flags)
{
int error;
return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE);
mutex_enter(&smb_vclist.co_interlock);
error = smb_co_lock(&smb_vclist);
mutex_exit(&smb_vclist.co_interlock);
return error;
}
static void
smb_sm_unlockvclist(void)
{
smb_co_unlock(&smb_vclist, LK_RELEASE);
mutex_enter(&smb_vclist.co_interlock);
smb_co_unlock(&smb_vclist);
mutex_exit(&smb_vclist.co_interlock);
}
static int
@ -135,7 +174,7 @@ smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
SMBCO_FOREACH(ocp, &smb_vclist) {
struct smb_vc *vcp = (struct smb_vc *)ocp;
if (smb_vc_lock(vcp, LK_EXCLUSIVE) != 0)
if (smb_vc_lock(vcp) != 0)
continue;
do {
@ -174,7 +213,7 @@ smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
goto out;
} while(0);
smb_vc_unlock(vcp, 0);
smb_vc_unlock(vcp);
}
out:
@ -235,21 +274,21 @@ smb_co_init(struct smb_connobj *cp, int level, const char *objname)
{
SLIST_INIT(&cp->co_children);
smb_sl_init(&cp->co_interlock, objname);
lockinit(&cp->co_lock, PZERO, objname, 0, 0);
cv_init(&cp->co_lock, "smblock");
cp->co_lockcnt = 0;
cp->co_locker = NULL;
cp->co_level = level;
cp->co_usecount = 1;
KASSERT(smb_co_lock(cp, LK_EXCLUSIVE) == 0);
mutex_enter(&cp->co_interlock);
smb_co_lock(cp);
mutex_exit(&cp->co_interlock);
}
static void
smb_co_done(struct smb_connobj *cp)
{
smb_sl_destroy(&cp->co_interlock);
#ifdef __NetBSD__
lockmgr(&cp->co_lock, LK_DRAIN, NULL);
#else
lockdestroy(&cp->co_lock);
#endif
cv_destroy(&cp->co_lock);
}
static void
@ -261,7 +300,9 @@ smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
cp->co_gone(cp, scred);
parent = cp->co_parent;
if (parent) {
smb_co_lock(parent, LK_EXCLUSIVE|LK_CANRECURSE);
mutex_enter(&parent->co_interlock);
smb_co_lock(parent);
mutex_exit(&parent->co_interlock);
SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
smb_co_put(parent, scred);
}
@ -273,19 +314,19 @@ void
smb_co_ref(struct smb_connobj *cp)
{
SMB_CO_LOCK(cp);
mutex_enter(&cp->co_interlock);
cp->co_usecount++;
SMB_CO_UNLOCK(cp);
mutex_exit(&cp->co_interlock);
}
void
smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
{
SMB_CO_LOCK(cp);
lockmgr(&cp->co_lock, LK_RELEASE, NULL);
mutex_enter(&cp->co_interlock);
smb_co_unlock(cp);
if (cp->co_usecount > 1) {
cp->co_usecount--;
SMB_CO_UNLOCK(cp);
mutex_exit(&cp->co_interlock);
return;
}
#ifdef DIAGNOSTIC
@ -294,34 +335,29 @@ smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
#endif
cp->co_usecount--;
cp->co_flags |= SMBO_GONE;
SMB_CO_UNLOCK(cp);
mutex_exit(&cp->co_interlock);
smb_co_gone(cp, scred);
}
int
smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred)
smb_co_get(struct smb_connobj *cp, struct smb_cred *scred)
{
int error;
if ((flags & LK_INTERLOCK) == 0)
SMB_CO_LOCK(cp);
KASSERT(mutex_owned(&cp->co_interlock));
cp->co_usecount++;
error = smb_co_lock(cp, flags | LK_INTERLOCK);
if (error) {
SMB_CO_LOCK(cp);
error = smb_co_lock(cp);
if (error)
cp->co_usecount--;
SMB_CO_UNLOCK(cp);
return error;
}
return 0;
return error;
}
void
smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
{
SMB_CO_LOCK(cp);
mutex_enter(&cp->co_interlock);
if (cp->co_usecount > 1) {
cp->co_usecount--;
} else if (cp->co_usecount == 1) {
@ -332,42 +368,52 @@ smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
else
panic("smb_co_put: negative usecount");
#endif
lockmgr(&cp->co_lock, LK_RELEASE | LK_INTERLOCK, &cp->co_interlock);
smb_co_unlock(cp);
mutex_exit(&cp->co_interlock);
if ((cp->co_flags & SMBO_GONE) == 0)
return;
smb_co_gone(cp, scred);
}
#ifdef DIAGNOSTIC
int
smb_co_lockstatus(struct smb_connobj *cp)
{
return lockstatus(&cp->co_lock);
}
#endif
int
smb_co_lock(struct smb_connobj *cp, int flags)
smb_co_lock(struct smb_connobj *cp)
{
if (cp->co_flags & SMBO_GONE)
return EINVAL;
if ((flags & LK_TYPE_MASK) == 0)
flags |= LK_EXCLUSIVE;
return lockmgr(&cp->co_lock, flags, &cp->co_interlock);
KASSERT(mutex_owned(&cp->co_interlock));
for (;;) {
if (cp->co_flags & SMBO_GONE)
return EINVAL;
if (cp->co_locker == NULL) {
cp->co_locker = curlwp;
return 0;
}
if (cp->co_locker == curlwp) {
cp->co_lockcnt++;
return 0;
}
cv_wait(&cp->co_lock, &cp->co_interlock);
}
}
void
smb_co_unlock(struct smb_connobj *cp, int flags)
smb_co_unlock(struct smb_connobj *cp)
{
(void)lockmgr(&cp->co_lock, flags | LK_RELEASE, &cp->co_interlock);
KASSERT(mutex_owned(&cp->co_interlock));
KASSERT(cp->co_locker == curlwp);
if (cp->co_lockcnt != 0) {
cp->co_lockcnt--;
return;
}
cp->co_locker = NULL;
cv_signal(&cp->co_lock);
}
static void
smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
{
KASSERT(smb_co_lockstatus(parent) == LK_EXCLUSIVE);
KASSERT(smb_co_lockstatus(child) == LK_EXCLUSIVE);
smb_co_ref(parent);
SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
@ -523,9 +569,16 @@ smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
}
int
smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred)
smb_vc_get(struct smb_vc *vcp, struct smb_cred *scred)
{
return smb_co_get(VCTOCP(vcp), flags, scred);
struct smb_connobj *cp = VCTOCP(vcp);
int error;
mutex_enter(&cp->co_interlock);
error = smb_co_get(cp, scred);
mutex_exit(&cp->co_interlock);
return error;
}
void
@ -535,17 +588,29 @@ smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
}
int
smb_vc_lock(struct smb_vc *vcp, int flags)
smb_vc_lock(struct smb_vc *vcp)
{
return smb_co_lock(VCTOCP(vcp), flags);
struct smb_connobj *cp = VCTOCP(vcp);
int error;
mutex_enter(&cp->co_interlock);
error = smb_co_lock(cp);
mutex_exit(&cp->co_interlock);
return error;
}
void
smb_vc_unlock(struct smb_vc *vcp, int flags)
smb_vc_unlock(struct smb_vc *vcp)
{
smb_co_unlock(VCTOCP(vcp), flags);
struct smb_connobj *cp = VCTOCP(vcp);
mutex_enter(&cp->co_interlock);
smb_co_unlock(cp);
mutex_exit(&cp->co_interlock);
}
int
smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
{
@ -605,12 +670,12 @@ smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
dp->scred = scred;
SMBCO_FOREACH(osp, VCTOCP(vcp)) {
ssp = (struct smb_share *)osp;
error = smb_share_lock(ssp, LK_EXCLUSIVE);
error = smb_share_lock(ssp);
if (error)
continue;
if (smb_vc_cmpshare(ssp, dp) == 0)
break;
smb_share_unlock(ssp, 0);
smb_share_unlock(ssp);
}
if (ssp) {
smb_share_ref(ssp);
@ -675,9 +740,9 @@ smb_vc_nextmid(struct smb_vc *vcp)
{
u_short r;
SMB_CO_LOCK(&vcp->obj);
mutex_enter(&vcp->obj.co_interlock);
r = vcp->vc_mid++;
SMB_CO_UNLOCK(&vcp->obj);
mutex_exit(&vcp->obj.co_interlock);
return r;
}
@ -771,9 +836,16 @@ smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
}
int
smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred)
smb_share_get(struct smb_share *ssp, struct smb_cred *scred)
{
return smb_co_get(SSTOCP(ssp), flags, scred);
struct smb_connobj *cp = SSTOCP(ssp);
int error;
mutex_enter(&cp->co_interlock);
error = smb_co_get(cp, scred);
mutex_exit(&cp->co_interlock);
return error;
}
void
@ -783,15 +855,26 @@ smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
}
int
smb_share_lock(struct smb_share *ssp, int flags)
smb_share_lock(struct smb_share *ssp)
{
return smb_co_lock(SSTOCP(ssp), flags);
struct smb_connobj *cp = SSTOCP(ssp);
int error;
mutex_enter(&cp->co_interlock);
error = smb_co_lock(cp);
mutex_exit(&cp->co_interlock);
return error;
}
void
smb_share_unlock(struct smb_share *ssp, int flags)
smb_share_unlock(struct smb_share *ssp)
{
smb_co_unlock(SSTOCP(ssp), flags);
struct smb_connobj *cp = SSTOCP(ssp);
mutex_enter(&cp->co_interlock);
smb_co_unlock(cp);
mutex_exit(&cp->co_interlock);
}
int

View File

@ -1,4 +1,4 @@
/* $NetBSD: smb_conn.h,v 1.17 2005/12/11 12:25:16 christos Exp $ */
/* $NetBSD: smb_conn.h,v 1.18 2008/01/30 14:08:01 ad Exp $ */
/*
* Copyright (c) 2000-2001 Boris Popov
@ -169,7 +169,8 @@ struct smb_share_info {
#ifdef _KERNEL
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <netsmb/smb_subr.h>
#define CONNADDREQ(a1,a2) ((a1)->sa_len == (a2)->sa_len && \
@ -197,14 +198,13 @@ struct smb_connobj;
typedef void smb_co_gone_t (struct smb_connobj *cp, struct smb_cred *scred);
typedef void smb_co_free_t (struct smb_connobj *cp);
#define SMB_CO_LOCK(cp) smb_sl_lock(&(cp)->co_interlock)
#define SMB_CO_UNLOCK(cp) smb_sl_unlock(&(cp)->co_interlock)
struct smb_connobj {
kcondvar_t co_lock;
struct lwp *co_locker;
kmutex_t co_interlock;
int co_lockcnt;
int co_level; /* SMBL_ */
int co_flags;
struct lock co_lock;
struct smb_slock co_interlock;
int co_usecount;
struct smb_connobj * co_parent;
SLIST_HEAD(,smb_connobj)co_children;
@ -359,10 +359,10 @@ int smb_sm_lookup(struct smb_vcspec *vcspec,
*/
void smb_co_ref(struct smb_connobj *cp);
void smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred);
int smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred);
int smb_co_get(struct smb_connobj *cp, struct smb_cred *scred);
void smb_co_put(struct smb_connobj *cp, struct smb_cred *scred);
int smb_co_lock(struct smb_connobj *cp, int flags);
void smb_co_unlock(struct smb_connobj *cp, int flags);
int smb_co_lock(struct smb_connobj *cp);
void smb_co_unlock(struct smb_connobj *cp);
/*
* session level functions
@ -371,12 +371,12 @@ int smb_vc_create(struct smb_vcspec *vcspec,
struct smb_cred *scred, struct smb_vc **vcpp);
int smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred);
int smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode);
int smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred);
int smb_vc_get(struct smb_vc *vcp, struct smb_cred *scred);
void smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred);
void smb_vc_ref(struct smb_vc *vcp);
void smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred);
int smb_vc_lock(struct smb_vc *vcp, int flags);
void smb_vc_unlock(struct smb_vc *vcp, int flags);
int smb_vc_lock(struct smb_vc *vcp);
void smb_vc_unlock(struct smb_vc *vcp);
int smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *shspec,
struct smb_cred *scred, struct smb_share **sspp);
const char * smb_vc_getpass(struct smb_vc *vcp);
@ -390,10 +390,10 @@ int smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
int smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode);
void smb_share_ref(struct smb_share *ssp);
void smb_share_rele(struct smb_share *ssp, struct smb_cred *scred);
int smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred);
int smb_share_get(struct smb_share *ssp, struct smb_cred *scred);
void smb_share_put(struct smb_share *ssp, struct smb_cred *scred);
int smb_share_lock(struct smb_share *ssp, int flags);
void smb_share_unlock(struct smb_share *ssp, int flags);
int smb_share_lock(struct smb_share *ssp);
void smb_share_unlock(struct smb_share *ssp);
int smb_share_valid(struct smb_share *ssp);
const char * smb_share_getpass(struct smb_share *ssp);

View File

@ -1,4 +1,4 @@
/* $NetBSD: smb_dev.c,v 1.26 2007/03/04 06:03:35 christos Exp $ */
/* $NetBSD: smb_dev.c,v 1.27 2008/01/30 14:08:01 ad Exp $ */
/*
* Copyright (c) 2000-2001 Boris Popov
@ -35,7 +35,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.26 2007/03/04 06:03:35 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: smb_dev.c,v 1.27 2008/01/30 14:08:01 ad Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
@ -238,12 +238,12 @@ nsmb_dev_close(dev_t dev, int flag, int fmt, struct lwp *l)
smb_makescred(&scred, l, NULL);
ssp = sdp->sd_share;
if (ssp != NULL) {
smb_share_lock(ssp, 0);
smb_share_lock(ssp);
smb_share_rele(ssp, &scred);
}
vcp = sdp->sd_vc;
if (vcp != NULL) {
smb_vc_lock(vcp, 0);
smb_vc_lock(vcp);
smb_vc_rele(vcp, &scred);
}
/*
@ -286,7 +286,7 @@ nsmb_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag,
if (error)
break;
sdp->sd_vc = vcp;
smb_vc_unlock(vcp, 0);
smb_vc_unlock(vcp);
sdp->sd_level = SMBL_VC;
break;
case SMBIOC_OPENSHARE:
@ -299,7 +299,7 @@ nsmb_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag,
if (error)
break;
sdp->sd_share = ssp;
smb_share_unlock(ssp, 0);
smb_share_unlock(ssp);
sdp->sd_level = SMBL_SHARE;
break;
case SMBIOC_REQUEST:
@ -323,7 +323,7 @@ nsmb_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag,
on = fl->ioc_flags & SMBV_PERMANENT;
if ((vcp = sdp->sd_vc) == NULL)
return ENOTCONN;
error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
error = smb_vc_get(vcp, &scred);
if (error)
break;
if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
@ -341,7 +341,7 @@ nsmb_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag,
on = fl->ioc_flags & SMBS_PERMANENT;
if ((ssp = sdp->sd_share) == NULL)
return ENOTCONN;
error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
error = smb_share_get(ssp, &scred);
if (error)
break;
if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
@ -369,12 +369,12 @@ nsmb_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag,
break;
if (vcp) {
sdp->sd_vc = vcp;
smb_vc_unlock(vcp, 0);
smb_vc_unlock(vcp);
sdp->sd_level = SMBL_VC;
}
if (ssp) {
sdp->sd_share = ssp;
smb_share_unlock(ssp, 0);
smb_share_unlock(ssp);
sdp->sd_level = SMBL_SHARE;
}
break;
@ -502,7 +502,7 @@ smb_dev2share(int fd, int mode, struct smb_cred *scred,
if (ssp == NULL)
return ENOTCONN;
error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
error = smb_share_get(ssp, scred);
if (error)
return error;