1 /*-
2 * Copyright (c) 1997, 1998
3 * Nan Yang Computer Services Limited. All rights reserved.
4 *
5 * Parts copyright (c) 1997, 1998 Cybernet Corporation, NetMAX project.
6 *
7 * Written by Greg Lehey
8 *
9 * This software is distributed under the so-called ``Berkeley
10 * License'':
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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
23 * Services Limited.
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.
27 *
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.
39 *
40 * $Id: vinumlock.c,v 1.13 2000/05/02 23:25:02 grog Exp grog $
41 * $FreeBSD: releng/5.0/sys/dev/vinum/vinumlock.c 76960 2001-05-22 02:34:30Z grog $
42 */
43
44 #include <dev/vinum/vinumhdr.h>
45 #include <dev/vinum/request.h>
46
47 /* Lock a drive, wait if it's in use */
48 #if VINUMDEBUG
49 int
50 lockdrive(struct drive *drive, char *file, int line)
51 #else
52 int
53 lockdrive(struct drive *drive)
54 #endif
55 {
56 int error;
57
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! */
61 #ifdef VINUMDEBUG
62 log(LOG_WARNING,
63 "vinum lockdrive: already locking %s from %s:%d, called from %s:%d\n",
64 drive->label.name,
65 drive->lockfilename,
66 drive->lockline,
67 basename(file),
68 line);
69 #else
70 log(LOG_WARNING,
71 "vinum lockdrive: already locking %s\n",
72 drive->label.name);
73 #endif
74 return 0;
75 }
76 while ((drive->flags & VF_LOCKED) != 0) {
77 /*
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.
83 */
84 if ((error = tsleep(&lockdrive,
85 PRIBIO,
86 "vindrv",
87 0)) != 0)
88 return error;
89 }
90 drive->flags |= VF_LOCKED;
91 drive->pid = curproc->p_pid; /* it's a panic error if curproc is null */
92 #ifdef VINUMDEBUG
93 bcopy(basename(file), drive->lockfilename, 15);
94 drive->lockfilename[15] = '\0'; /* truncate if necessary */
95 drive->lockline = line;
96 #endif
97 return 0;
98 }
99
100 /* Unlock a drive and let the next one at it */
101 void
102 unlockdrive(struct drive *drive)
103 {
104 drive->flags &= ~VF_LOCKED;
105 /* we don't reset pid: it's of hysterical interest */
106 wakeup(&lockdrive);
107 }
108
109 /* Lock a stripe of a plex, wait if it's in use */
110 struct rangelock *
111 lockrange(daddr_t stripe, struct buf *bp, struct plex *plex)
112 {
113 struct rangelock *lock;
114 struct rangelock *pos; /* position of first free lock */
115 int foundlocks; /* number of locks found */
116
117 /*
118 * We could get by without counting the number
119 * of locks we find, but we have a linear search
120 * through a table which in most cases will be
121 * empty. It's faster to stop when we've found
122 * all the locks that are there. This is also
123 * the reason why we put pos at the beginning
124 * instead of the end, though it requires an
125 * extra test.
126 */
127 pos = NULL;
128 foundlocks = 0;
129
130 /*
131 * we can't use 0 as a valid address, so
132 * increment all addresses by 1.
133 */
134 stripe++;
135 mtx_lock(&plex->lockmtx);
136
137 /* Wait here if the table is full */
138 while (plex->usedlocks == PLEX_LOCKS) /* all in use */
139 msleep(&plex->usedlocks, &plex->lockmtx, PRIBIO, "vlock", 0);
140
141 #ifdef DIAGNOSTIC
142 if (plex->usedlocks >= PLEX_LOCKS)
143 panic("lockrange: Too many locks in use");
144 #endif
145
146 lock = plex->lock; /* pointer in lock table */
147 if (plex->usedlocks > 0) /* something locked, */
148 /* Search the lock table for our stripe */
149 for (; lock < &plex->lock[PLEX_LOCKS]
150 && foundlocks < plex->usedlocks;
151 lock++) {
152 if (lock->stripe) { /* in use */
153 foundlocks++; /* found another one in use */
154 if ((lock->stripe == stripe) /* it's our stripe */
155 &&(lock->bp != bp)) { /* but not our request */
156 #ifdef VINUMDEBUG
157 if (debug & DEBUG_LOCKREQS) {
158 struct rangelockinfo lockinfo;
159
160 lockinfo.stripe = stripe;
161 lockinfo.bp = bp;
162 lockinfo.plexno = plex->plexno;
163 logrq(loginfo_lockwait, (union rqinfou) &lockinfo, bp);
164 }
165 #endif
166 plex->lockwaits++; /* waited one more time */
167 msleep(lock, &plex->lockmtx, PRIBIO, "vrlock", 0);
168 lock = &plex->lock[-1]; /* start again */
169 foundlocks = 0;
170 pos = NULL;
171 }
172 } else if (pos == NULL) /* still looking for somewhere? */
173 pos = lock; /* a place to put this one */
174 }
175 /*
176 * This untidy looking code ensures that we'll
177 * always end up pointing to the first free lock
178 * entry, thus minimizing the number of
179 * iterations necessary.
180 */
181 if (pos == NULL) /* didn't find one on the way, */
182 pos = lock; /* use the one we're pointing to */
183
184 /*
185 * The address range is free, and we're pointing
186 * to the first unused entry. Make it ours.
187 */
188 pos->stripe = stripe;
189 pos->bp = bp;
190 plex->usedlocks++; /* one more lock */
191 mtx_unlock(&plex->lockmtx);
192 #ifdef VINUMDEBUG
193 if (debug & DEBUG_LOCKREQS) {
194 struct rangelockinfo lockinfo;
195
196 lockinfo.stripe = stripe;
197 lockinfo.bp = bp;
198 lockinfo.plexno = plex->plexno;
199 logrq(loginfo_lock, (union rqinfou) &lockinfo, bp);
200 }
201 #endif
202 return pos;
203 }
204
205 /* Unlock a volume and let the next one at it */
206 void
207 unlockrange(int plexno, struct rangelock *lock)
208 {
209 struct plex *plex;
210
211 plex = &PLEX[plexno];
212 #ifdef DIAGNOSTIC
213 if (lock < &plex->lock[0] || lock >= &plex->lock[PLEX_LOCKS])
214 panic("vinum: rangelock %p on plex %d invalid, not between %p and %p",
215 lock,
216 plexno,
217 &plex->lock[0],
218 &plex->lock[PLEX_LOCKS]);
219 #endif
220 #ifdef VINUMDEBUG
221 if (debug & DEBUG_LOCKREQS) {
222 struct rangelockinfo lockinfo;
223
224 lockinfo.stripe = lock->stripe;
225 lockinfo.bp = lock->bp;
226 lockinfo.plexno = plex->plexno;
227 logrq(loginfo_lockwait, (union rqinfou) &lockinfo, lock->bp);
228 }
229 #endif
230 lock->stripe = 0; /* no longer used */
231 plex->usedlocks--; /* one less lock */
232 if (plex->usedlocks == PLEX_LOCKS - 1) /* we were full, */
233 wakeup(&plex->usedlocks); /* get a waiter if one's there */
234 wakeup((void *) lock);
235 }
236
237 /* Get a lock for the global config, wait if it's not available */
238 int
239 lock_config(void)
240 {
241 int error;
242
243 while ((vinum_conf.flags & VF_LOCKED) != 0) {
244 vinum_conf.flags |= VF_LOCKING;
245 if ((error = tsleep(&vinum_conf, PRIBIO, "vincfg", 0)) != 0)
246 return error;
247 }
248 vinum_conf.flags |= VF_LOCKED;
249 return 0;
250 }
251
252 /* Unlock and wake up any waiters */
253 void
254 unlock_config(void)
255 {
256 vinum_conf.flags &= ~VF_LOCKED;
257 if ((vinum_conf.flags & VF_LOCKING) != 0) {
258 vinum_conf.flags &= ~VF_LOCKING;
259 wakeup(&vinum_conf);
260 }
261 }
262 /* Local Variables: */
263 /* fill-column: 50 */
264 /* End: */
Cache object: c3c0e32fa323fada45a87b7823ff9157
|