1998-12-28 04:56:24 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997, 1998
|
|
|
|
* Nan Yang Computer Services Limited. All rights reserved.
|
|
|
|
*
|
1999-08-07 08:11:22 +00:00
|
|
|
* Parts copyright (c) 1997, 1998 Cybernet Corporation, NetMAX project.
|
|
|
|
*
|
|
|
|
* Written by Greg Lehey
|
|
|
|
*
|
1998-12-28 04:56:24 +00:00
|
|
|
* This software is distributed under the so-called ``Berkeley
|
|
|
|
* License'':
|
|
|
|
*
|
|
|
|
* 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 Nan Yang Computer
|
|
|
|
* Services Limited.
|
|
|
|
* 4. Neither the name of the Company nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
1999-04-10 08:11:21 +00:00
|
|
|
*
|
1998-12-28 04:56:24 +00:00
|
|
|
* This software is provided ``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 company 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-01-05 22:59:36 +00:00
|
|
|
* $Id: vinumlock.c,v 1.11 1999/10/12 04:35:37 grog Exp grog $
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-12-28 04:56:24 +00:00
|
|
|
*/
|
|
|
|
|
1998-12-28 16:28:24 +00:00
|
|
|
#include <dev/vinum/vinumhdr.h>
|
1999-08-14 06:28:21 +00:00
|
|
|
#include <dev/vinum/request.h>
|
1998-12-28 04:56:24 +00:00
|
|
|
|
1999-01-29 01:17:54 +00:00
|
|
|
/*
|
|
|
|
* Lock routines. Currently, we lock either an individual volume
|
1998-12-28 04:56:24 +00:00
|
|
|
* or the global configuration. I don't think tsleep and
|
1999-04-10 08:11:21 +00:00
|
|
|
* wakeup are SMP safe. FIXME XXX
|
1999-01-29 01:17:54 +00:00
|
|
|
*/
|
1998-12-28 04:56:24 +00:00
|
|
|
|
1999-01-21 00:37:38 +00:00
|
|
|
/* Lock a drive, wait if it's in use */
|
1999-04-10 08:11:21 +00:00
|
|
|
#if VINUMDEBUG
|
1999-08-24 02:23:21 +00:00
|
|
|
int
|
1999-04-10 08:11:21 +00:00
|
|
|
lockdrive(struct drive *drive, char *file, int line)
|
|
|
|
#else
|
1999-08-24 02:23:21 +00:00
|
|
|
int
|
1999-01-21 00:37:38 +00:00
|
|
|
lockdrive(struct drive *drive)
|
1999-04-10 08:11:21 +00:00
|
|
|
#endif
|
1999-01-21 00:37:38 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* XXX get rid of drive->flags |= VF_LOCKING; */
|
1999-03-13 07:35:40 +00:00
|
|
|
if ((drive->flags & VF_LOCKED) /* it's locked */
|
|
|
|
&&(drive->pid == curproc->p_pid)) { /* by us! */
|
1999-04-10 08:11:21 +00:00
|
|
|
#ifdef VINUMDEBUG
|
|
|
|
log(LOG_WARNING,
|
|
|
|
"vinum lockdrive: already locking %s from %s:%d, called from %s:%d\n",
|
|
|
|
drive->label.name,
|
|
|
|
drive->lockfilename,
|
|
|
|
drive->lockline,
|
|
|
|
basename(file),
|
|
|
|
line);
|
|
|
|
#else
|
1999-03-13 07:35:40 +00:00
|
|
|
log(LOG_WARNING,
|
|
|
|
"vinum lockdrive: already locking %s\n",
|
|
|
|
drive->label.name);
|
1999-04-10 08:11:21 +00:00
|
|
|
#endif
|
1999-03-13 07:35:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-01-21 00:37:38 +00:00
|
|
|
while ((drive->flags & VF_LOCKED) != 0) {
|
1999-01-29 01:17:54 +00:00
|
|
|
/*
|
|
|
|
* There are problems sleeping on a unique identifier,
|
1999-01-21 00:37:38 +00:00
|
|
|
* since the drive structure can move, and the unlock
|
|
|
|
* function can be called after killing the drive.
|
|
|
|
* Solve this by waiting on this function; the number
|
1999-04-10 08:11:21 +00:00
|
|
|
* of conflicts is negligible.
|
1999-01-29 01:17:54 +00:00
|
|
|
*/
|
1999-01-21 00:37:38 +00:00
|
|
|
if ((error = tsleep(&lockdrive,
|
2000-01-05 22:59:36 +00:00
|
|
|
PRIBIO,
|
1999-01-21 00:37:38 +00:00
|
|
|
"vindrv",
|
|
|
|
0)) != 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
drive->flags |= VF_LOCKED;
|
|
|
|
drive->pid = curproc->p_pid; /* it's a panic error if curproc is null */
|
1999-04-10 08:11:21 +00:00
|
|
|
#ifdef VINUMDEBUG
|
|
|
|
bcopy(basename(file), drive->lockfilename, 15);
|
|
|
|
drive->lockfilename[15] = '\0'; /* truncate if necessary */
|
|
|
|
drive->lockline = line;
|
|
|
|
#endif
|
1999-01-21 00:37:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock a drive and let the next one at it */
|
1999-08-24 02:23:21 +00:00
|
|
|
void
|
1999-01-21 00:37:38 +00:00
|
|
|
unlockdrive(struct drive *drive)
|
|
|
|
{
|
|
|
|
drive->flags &= ~VF_LOCKED;
|
|
|
|
/* we don't reset pid: it's of hysterical interest */
|
|
|
|
wakeup(&lockdrive);
|
|
|
|
}
|
|
|
|
|
1998-12-28 04:56:24 +00:00
|
|
|
/* Lock a volume, wait if it's in use */
|
1999-08-24 02:23:21 +00:00
|
|
|
int
|
1998-12-28 04:56:24 +00:00
|
|
|
lockvol(struct volume *vol)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
while ((vol->flags & VF_LOCKED) != 0) {
|
|
|
|
vol->flags |= VF_LOCKING;
|
1999-01-29 01:17:54 +00:00
|
|
|
/*
|
|
|
|
* It would seem to make more sense to sleep on
|
1998-12-28 04:56:24 +00:00
|
|
|
* the address 'vol'. Unfortuntaly we can't
|
|
|
|
* guarantee that this address won't change due to
|
1999-04-10 08:11:21 +00:00
|
|
|
* table expansion. The address we choose won't change.
|
1999-01-29 01:17:54 +00:00
|
|
|
*/
|
1999-05-15 05:49:21 +00:00
|
|
|
if ((error = tsleep(&vinum_conf.volume + vol->volno,
|
2000-01-05 22:59:36 +00:00
|
|
|
PRIBIO,
|
1998-12-28 04:56:24 +00:00
|
|
|
"volock",
|
|
|
|
0)) != 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
vol->flags |= VF_LOCKED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock a volume and let the next one at it */
|
1999-08-24 02:23:21 +00:00
|
|
|
void
|
1998-12-28 04:56:24 +00:00
|
|
|
unlockvol(struct volume *vol)
|
|
|
|
{
|
|
|
|
vol->flags &= ~VF_LOCKED;
|
|
|
|
if ((vol->flags & VF_LOCKING) != 0) {
|
|
|
|
vol->flags &= ~VF_LOCKING;
|
1999-05-15 05:49:21 +00:00
|
|
|
wakeup(&vinum_conf.volume + vol->volno);
|
1998-12-28 04:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock a plex, wait if it's in use */
|
1999-08-24 02:23:21 +00:00
|
|
|
int
|
1998-12-28 04:56:24 +00:00
|
|
|
lockplex(struct plex *plex)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
while ((plex->flags & VF_LOCKED) != 0) {
|
|
|
|
plex->flags |= VF_LOCKING;
|
1999-01-29 01:17:54 +00:00
|
|
|
/*
|
|
|
|
* It would seem to make more sense to sleep on
|
1999-01-21 00:37:38 +00:00
|
|
|
* the address 'plex'. Unfortunately we can't
|
1998-12-28 04:56:24 +00:00
|
|
|
* guarantee that this address won't change due to
|
1999-04-10 08:11:21 +00:00
|
|
|
* table expansion. The address we choose won't change.
|
1999-01-29 01:17:54 +00:00
|
|
|
*/
|
1998-12-28 04:56:24 +00:00
|
|
|
if ((error = tsleep(&vinum_conf.plex + plex->sdnos[0],
|
2000-01-05 22:59:36 +00:00
|
|
|
PRIBIO,
|
1998-12-28 04:56:24 +00:00
|
|
|
"plexlk",
|
|
|
|
0)) != 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
plex->flags |= VF_LOCKED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock a plex and let the next one at it */
|
1999-08-24 02:23:21 +00:00
|
|
|
void
|
1998-12-28 04:56:24 +00:00
|
|
|
unlockplex(struct plex *plex)
|
|
|
|
{
|
|
|
|
plex->flags &= ~VF_LOCKED;
|
|
|
|
if ((plex->flags & VF_LOCKING) != 0) {
|
|
|
|
plex->flags &= ~VF_LOCKING;
|
|
|
|
wakeup(&vinum_conf.plex + plex->plexno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-14 06:28:21 +00:00
|
|
|
/* Lock a stripe of a plex, wait if it's in use */
|
|
|
|
struct rangelock *
|
|
|
|
lockrange(daddr_t stripe, struct buf *bp, struct plex *plex)
|
1999-08-07 08:11:22 +00:00
|
|
|
{
|
1999-08-14 06:28:21 +00:00
|
|
|
int s;
|
|
|
|
struct rangelock *lock;
|
|
|
|
struct rangelock *pos; /* position of first free lock */
|
|
|
|
int foundlocks; /* number of locks found */
|
|
|
|
int newlock;
|
1999-08-07 08:11:22 +00:00
|
|
|
|
1999-08-14 06:28:21 +00:00
|
|
|
/*
|
|
|
|
* We could get by without counting the number
|
|
|
|
* of locks we find, but we have a linear search
|
|
|
|
* through a table which in most cases will be
|
|
|
|
* empty. It's faster to stop when we've found
|
|
|
|
* all the locks that are there. This is also
|
|
|
|
* the reason why we put pos at the beginning
|
|
|
|
* instead of the end, though it requires an
|
|
|
|
* extra test.
|
|
|
|
*/
|
|
|
|
pos = NULL;
|
|
|
|
foundlocks = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we can't use 0 as a valid address, so
|
|
|
|
* increment all addresses by 1.
|
|
|
|
*/
|
|
|
|
stripe++;
|
|
|
|
/*
|
|
|
|
* We give the locks back from an interrupt
|
|
|
|
* context, so we need to raise the spl here.
|
|
|
|
*/
|
|
|
|
s = splbio();
|
|
|
|
|
|
|
|
/* Search the lock table for our stripe */
|
|
|
|
for (lock = plex->lock;
|
|
|
|
lock < &plex->lock[plex->alloclocks]
|
|
|
|
&& foundlocks < plex->usedlocks;
|
|
|
|
lock++) {
|
|
|
|
if (lock->stripe) { /* in use */
|
|
|
|
foundlocks++; /* found another one in use */
|
|
|
|
if ((lock->stripe == stripe) /* it's our stripe */
|
|
|
|
&&(lock->plexno == plex->plexno) /* and our plex */
|
|
|
|
&&(lock->bp != bp)) { /* but not our request */
|
|
|
|
/*
|
|
|
|
* It would be nice to sleep on the lock
|
|
|
|
* itself, but it could get moved if the
|
|
|
|
* table expands during the wait. Wait on
|
|
|
|
* the lock address + 1 (since waiting on
|
|
|
|
* 0 isn't allowed) instead. It isn't
|
|
|
|
* exactly unique, but we won't have many
|
|
|
|
* conflicts. The worst effect of a
|
|
|
|
* conflict would be an additional
|
|
|
|
* schedule and time through this loop.
|
|
|
|
*/
|
|
|
|
#ifdef VINUMDEBUG
|
1999-09-28 22:56:04 +00:00
|
|
|
if (debug & DEBUG_LASTREQS) {
|
|
|
|
struct rangelock info;
|
1999-08-14 06:28:21 +00:00
|
|
|
|
1999-09-28 22:56:04 +00:00
|
|
|
info.stripe = stripe;
|
|
|
|
info.bp = bp;
|
|
|
|
info.plexno = plex->plexno;
|
|
|
|
logrq(loginfo_lockwait, (union rqinfou) &info, bp);
|
|
|
|
}
|
1999-08-14 06:28:21 +00:00
|
|
|
#endif
|
1999-09-28 22:56:04 +00:00
|
|
|
plex->lockwaits++; /* waited one more time */
|
|
|
|
while (lock->stripe) /* wait for it to become free */
|
2000-01-05 22:59:36 +00:00
|
|
|
tsleep((void *) lock->stripe, PRIBIO, "vrlock", 2 * hz);
|
1999-08-07 08:11:22 +00:00
|
|
|
break; /* out of the inner level loop */
|
|
|
|
}
|
1999-08-14 06:28:21 +00:00
|
|
|
} else {
|
|
|
|
if (pos == NULL) /* still looking for somewhere? */
|
|
|
|
pos = lock; /* a place to put this one */
|
1999-08-07 08:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-08-14 06:28:21 +00:00
|
|
|
* The address range is free. Add our lock
|
|
|
|
* entry.
|
1999-08-07 08:11:22 +00:00
|
|
|
*/
|
1999-08-14 06:28:21 +00:00
|
|
|
if (pos == NULL) { /* Didn't find an entry */
|
|
|
|
if (foundlocks >= plex->alloclocks) { /* searched the lot, */
|
|
|
|
newlock = plex->alloclocks;
|
|
|
|
EXPAND(plex->lock, struct rangelock, plex->alloclocks, INITIAL_LOCKS);
|
1999-08-26 03:25:38 +00:00
|
|
|
pos = &plex->lock[newlock];
|
1999-08-14 06:28:21 +00:00
|
|
|
while (newlock < plex->alloclocks)
|
|
|
|
plex->lock[newlock++].stripe = 0;
|
1999-08-26 03:25:38 +00:00
|
|
|
} else
|
|
|
|
pos = lock; /* put it at the end */
|
1999-08-07 08:11:22 +00:00
|
|
|
}
|
1999-08-14 06:28:21 +00:00
|
|
|
pos->stripe = stripe;
|
|
|
|
pos->bp = bp;
|
|
|
|
pos->plexno = plex->plexno;
|
|
|
|
plex->usedlocks++; /* one more lock */
|
|
|
|
splx(s);
|
|
|
|
#ifdef VINUMDEBUG
|
|
|
|
if (debug & DEBUG_LASTREQS)
|
|
|
|
logrq(loginfo_lock, (union rqinfou) pos, bp);
|
|
|
|
#endif
|
|
|
|
return pos;
|
1999-08-07 08:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock a volume and let the next one at it */
|
1999-08-24 02:23:21 +00:00
|
|
|
void
|
|
|
|
unlockrange(int plexno, struct rangelock *lock)
|
1999-08-07 08:11:22 +00:00
|
|
|
{
|
1999-08-14 06:28:21 +00:00
|
|
|
daddr_t lockaddr;
|
1999-08-07 08:11:22 +00:00
|
|
|
|
1999-08-14 06:28:21 +00:00
|
|
|
#ifdef VINUMDEBUG
|
|
|
|
if (debug & DEBUG_LASTREQS)
|
1999-08-24 02:23:21 +00:00
|
|
|
logrq(loginfo_unlock, (union rqinfou) lock, lock->bp);
|
1999-08-14 06:28:21 +00:00
|
|
|
#endif
|
1999-08-24 02:23:21 +00:00
|
|
|
lockaddr = lock->stripe;
|
|
|
|
lock->stripe = 0; /* no longer used */
|
|
|
|
PLEX[plexno].usedlocks--; /* one less lock */
|
1999-08-14 06:28:21 +00:00
|
|
|
wakeup((void *) lockaddr);
|
1999-08-07 08:11:22 +00:00
|
|
|
}
|
1998-12-28 04:56:24 +00:00
|
|
|
|
|
|
|
/* Get a lock for the global config, wait if it's not available */
|
1999-08-24 02:23:21 +00:00
|
|
|
int
|
1998-12-28 04:56:24 +00:00
|
|
|
lock_config(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
while ((vinum_conf.flags & VF_LOCKED) != 0) {
|
|
|
|
vinum_conf.flags |= VF_LOCKING;
|
2000-01-05 22:59:36 +00:00
|
|
|
if ((error = tsleep(&vinum_conf, PRIBIO, "vincfg", 0)) != 0)
|
1998-12-28 04:56:24 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
vinum_conf.flags |= VF_LOCKED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock and wake up any waiters */
|
1999-08-24 02:23:21 +00:00
|
|
|
void
|
1998-12-28 04:56:24 +00:00
|
|
|
unlock_config(void)
|
|
|
|
{
|
|
|
|
vinum_conf.flags &= ~VF_LOCKED;
|
|
|
|
if ((vinum_conf.flags & VF_LOCKING) != 0) {
|
|
|
|
vinum_conf.flags &= ~VF_LOCKING;
|
|
|
|
wakeup(&vinum_conf);
|
|
|
|
}
|
|
|
|
}
|
1999-08-14 06:28:21 +00:00
|
|
|
/* Local Variables: */
|
|
|
|
/* fill-column: 50 */
|
|
|
|
/* End: */
|