2 * Copyright (c) 1997, 1998
3 * Nan Yang Computer Services Limited. All rights reserved.
5 * Parts copyright (c) 1997, 1998 Cybernet Corporation, NetMAX project.
7 * Written by Greg Lehey
9 * This software is distributed under the so-called ``Berkeley
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Nan Yang Computer
24 * 4. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * This software is provided ``as is'', and any express or implied
29 * warranties, including, but not limited to, the implied warranties of
30 * merchantability and fitness for a particular purpose are disclaimed.
31 * In no event shall the company or contributors be liable for any
32 * direct, indirect, incidental, special, exemplary, or consequential
33 * damages (including, but not limited to, procurement of substitute
34 * goods or services; loss of use, data, or profits; or business
35 * interruption) however caused and on any theory of liability, whether
36 * in contract, strict liability, or tort (including negligence or
37 * otherwise) arising in any way out of the use of this software, even if
38 * advised of the possibility of such damage.
40 * $Id: vinumlock.c,v 1.13 2000/05/02 23:25:02 grog Exp grog $
41 * $FreeBSD: src/sys/dev/vinum/vinumlock.c,v 1.18.2.3 2001/04/04 06:27:11 grog Exp $
44 #include <dev/vinum/vinumhdr.h>
45 #include <dev/vinum/request.h>
47 /* Lock a drive, wait if it's in use */
50 lockdrive(struct drive *drive, char *file, int line)
53 lockdrive(struct drive *drive)
58 /* XXX get rid of drive->flags |= VF_LOCKING; */
59 if ((drive->flags & VF_LOCKED) /* it's locked */
60 &&(drive->pid == curproc->p_pid)) { /* by us! */
63 "vinum lockdrive: already locking %s from %s:%d, called from %s:%d\n",
71 "vinum lockdrive: already locking %s\n",
76 while ((drive->flags & VF_LOCKED) != 0) {
78 * There are problems sleeping on a unique identifier,
79 * since the drive structure can move, and the unlock
80 * function can be called after killing the drive.
81 * Solve this by waiting on this function; the number
82 * of conflicts is negligible.
84 if ((error = tsleep(&lockdrive,
90 drive->flags |= VF_LOCKED;
91 drive->pid = curproc->p_pid; /* it's a panic error if curproc is null */
93 bcopy(basename(file), drive->lockfilename, 15);
94 drive->lockfilename[15] = '\0'; /* truncate if necessary */
95 drive->lockline = line;
100 /* Unlock a drive and let the next one at it */
102 unlockdrive(struct drive *drive)
104 drive->flags &= ~VF_LOCKED;
105 /* we don't reset pid: it's of hysterical interest */
109 /* Lock a stripe of a plex, wait if it's in use */
111 lockrange(daddr_t stripe, struct buf *bp, struct plex *plex)
114 struct rangelock *lock;
115 struct rangelock *pos; /* position of first free lock */
116 int foundlocks; /* number of locks found */
119 * We could get by without counting the number
120 * of locks we find, but we have a linear search
121 * through a table which in most cases will be
122 * empty. It's faster to stop when we've found
123 * all the locks that are there. This is also
124 * the reason why we put pos at the beginning
125 * instead of the end, though it requires an
132 * we can't use 0 as a valid address, so
133 * increment all addresses by 1.
137 * We give the locks back from an interrupt
138 * context, so we need to raise the spl here.
142 /* Wait here if the table is full */
143 while (plex->usedlocks == PLEX_LOCKS) /* all in use */
144 tsleep(&plex->usedlocks, PRIBIO, "vlock", 0);
147 if (plex->usedlocks >= PLEX_LOCKS)
148 panic("lockrange: Too many locks in use");
151 lock = plex->lock; /* pointer in lock table */
152 if (plex->usedlocks > 0) /* something locked, */
153 /* Search the lock table for our stripe */
154 for (; lock < &plex->lock[PLEX_LOCKS]
155 && foundlocks < plex->usedlocks;
157 if (lock->stripe) { /* in use */
158 foundlocks++; /* found another one in use */
159 if ((lock->stripe == stripe) /* it's our stripe */
160 &&(lock->bp != bp)) { /* but not our request */
162 if (debug & DEBUG_LASTREQS) {
163 struct rangelock info;
165 info.stripe = stripe;
167 logrq(loginfo_lockwait, (union rqinfou) &info, bp);
170 plex->lockwaits++; /* waited one more time */
171 tsleep(lock, PRIBIO, "vrlock", 0);
172 lock = &plex->lock[-1]; /* start again */
176 } else if (pos == NULL) /* still looking for somewhere? */
177 pos = lock; /* a place to put this one */
180 * This untidy looking code ensures that we'll
181 * always end up pointing to the first free lock
182 * entry, thus minimizing the number of
183 * iterations necessary.
185 if (pos == NULL) /* didn't find one on the way, */
186 pos = lock; /* use the one we're pointing to */
189 * The address range is free, and we're pointing
190 * to the first unused entry. Make it ours.
192 pos->stripe = stripe;
194 plex->usedlocks++; /* one more lock */
197 if (debug & DEBUG_LASTREQS)
198 logrq(loginfo_lock, (union rqinfou) pos, bp);
203 /* Unlock a volume and let the next one at it */
205 unlockrange(int plexno, struct rangelock *lock)
209 plex = &PLEX[plexno];
211 if (lock < &plex->lock[0] || lock >= &plex->lock[PLEX_LOCKS])
212 panic("vinum: rangelock %p on plex %d invalid, not between %p and %p",
216 &plex->lock[PLEX_LOCKS]);
219 if (debug & DEBUG_LASTREQS)
220 logrq(loginfo_unlock, (union rqinfou) lock, lock->bp);
222 lock->stripe = 0; /* no longer used */
223 plex->usedlocks--; /* one less lock */
224 if (plex->usedlocks == PLEX_LOCKS - 1) /* we were full, */
225 wakeup(&plex->usedlocks); /* get a waiter if one's there */
226 wakeup((void *) lock);
229 /* Get a lock for the global config, wait if it's not available */
235 while ((vinum_conf.flags & VF_LOCKED) != 0) {
236 vinum_conf.flags |= VF_LOCKING;
237 if ((error = tsleep(&vinum_conf, PRIBIO, "vincfg", 0)) != 0)
240 vinum_conf.flags |= VF_LOCKED;
244 /* Unlock and wake up any waiters */
248 vinum_conf.flags &= ~VF_LOCKED;
249 if ((vinum_conf.flags & VF_LOCKING) != 0) {
250 vinum_conf.flags &= ~VF_LOCKING;
254 /* Local Variables: */
255 /* fill-column: 50 */