cleanup
[linux-2.4.git] / fs / xfs / linux-2.4 / mrlock.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <linux/time.h>
34 #include <linux/sched.h>
35 #include <asm/system.h>
36 #include <linux/interrupt.h>
37 #include <asm/current.h>
38
39 #include "mrlock.h"
40
41
42 #if USE_RW_WAIT_QUEUE_SPINLOCK
43 # define wq_write_lock  write_lock
44 #else
45 # define wq_write_lock  spin_lock
46 #endif
47
48 /*
49  * We don't seem to need lock_type (only one supported), name, or
50  * sequence. But, XFS will pass it so let's leave them here for now.
51  */
52 /* ARGSUSED */
53 void
54 mrlock_init(mrlock_t *mrp, int lock_type, char *name, long sequence)
55 {
56         mrp->mr_count = 0;
57         mrp->mr_reads_waiting = 0;
58         mrp->mr_writes_waiting = 0;
59         init_waitqueue_head(&mrp->mr_readerq);
60         init_waitqueue_head(&mrp->mr_writerq);
61         mrp->mr_lock = SPIN_LOCK_UNLOCKED;
62 }
63
64 /*
65  * Macros to lock/unlock the mrlock_t.
66  */
67
68 #define MRLOCK(m)               spin_lock(&(m)->mr_lock);
69 #define MRUNLOCK(m)             spin_unlock(&(m)->mr_lock);
70
71
72 /*
73  * lock_wait should never be called in an interrupt thread.
74  *
75  * mrlocks can sleep (i.e. call schedule) and so they can't ever
76  * be called from an interrupt thread.
77  *
78  * threads that wake-up should also never be invoked from interrupt threads.
79  *
80  * But, waitqueue_lock is locked from interrupt threads - and we are
81  * called with interrupts disabled, so it is all OK.
82  */
83
84 /* ARGSUSED */
85 void
86 lock_wait(wait_queue_head_t *q, spinlock_t *lock, int rw)
87 {
88         DECLARE_WAITQUEUE( wait, current );
89
90         __set_current_state(TASK_UNINTERRUPTIBLE);
91
92         wq_write_lock(&q->lock);
93         if (rw) {
94                 __add_wait_queue_tail(q, &wait);
95         } else {
96                 __add_wait_queue(q, &wait);
97         }
98
99         wq_write_unlock(&q->lock);
100         spin_unlock(lock);
101
102         schedule();
103
104         wq_write_lock(&q->lock);
105         __remove_wait_queue(q, &wait);
106         wq_write_unlock(&q->lock);
107
108         spin_lock(lock);
109
110         /* return with lock held */
111 }
112
113 /* ARGSUSED */
114 void
115 mrfree(mrlock_t *mrp)
116 {
117 }
118
119 /* ARGSUSED */
120 void
121 mrlock(mrlock_t *mrp, int type, int flags)
122 {
123         if (type == MR_ACCESS)
124                 mraccess(mrp);
125         else
126                 mrupdate(mrp);
127 }
128
129 /* ARGSUSED */
130 void
131 mraccessf(mrlock_t *mrp, int flags)
132 {
133         MRLOCK(mrp);
134         if(mrp->mr_writes_waiting > 0) {
135                 mrp->mr_reads_waiting++;
136                 lock_wait(&mrp->mr_readerq, &mrp->mr_lock, 0);
137                 mrp->mr_reads_waiting--;
138         }
139         while (mrp->mr_count < 0) {
140                 mrp->mr_reads_waiting++;
141                 lock_wait(&mrp->mr_readerq, &mrp->mr_lock, 0);
142                 mrp->mr_reads_waiting--;
143         }
144         mrp->mr_count++;
145         MRUNLOCK(mrp);
146 }
147
148 /* ARGSUSED */
149 void
150 mrupdatef(mrlock_t *mrp, int flags)
151 {
152         MRLOCK(mrp);
153         while(mrp->mr_count) {
154                 mrp->mr_writes_waiting++;
155                 lock_wait(&mrp->mr_writerq, &mrp->mr_lock, 1);
156                 mrp->mr_writes_waiting--;
157         }
158
159         mrp->mr_count = -1; /* writer on it */
160         MRUNLOCK(mrp);
161 }
162
163 int
164 mrtryaccess(mrlock_t *mrp)
165 {
166         MRLOCK(mrp);
167         /*
168          * If anyone is waiting for update access or the lock is held for update
169          * fail the request.
170          */
171         if(mrp->mr_writes_waiting > 0 || mrp->mr_count < 0) {
172                 MRUNLOCK(mrp);
173                 return 0;
174         }
175         mrp->mr_count++;
176         MRUNLOCK(mrp);
177         return 1;
178 }
179
180 int
181 mrtrypromote(mrlock_t *mrp)
182 {
183         MRLOCK(mrp);
184
185         if(mrp->mr_count == 1) { /* We are the only thread with the lock */
186                 mrp->mr_count = -1; /* writer on it */
187                 MRUNLOCK(mrp);
188                 return 1;
189         }
190
191         MRUNLOCK(mrp);
192         return 0;
193 }
194
195 int
196 mrtryupdate(mrlock_t *mrp)
197 {
198         MRLOCK(mrp);
199
200         if(mrp->mr_count) {
201                 MRUNLOCK(mrp);
202                 return 0;
203         }
204
205         mrp->mr_count = -1; /* writer on it */
206         MRUNLOCK(mrp);
207         return 1;
208 }
209
210 static __inline__ void mrwake(mrlock_t *mrp)
211 {
212         /*
213          * First, if the count is now 0, we need to wake-up anyone waiting.
214          */
215         if (!mrp->mr_count) {
216                 if (mrp->mr_writes_waiting) {   /* Wake-up first writer waiting */
217                         wake_up(&mrp->mr_writerq);
218                 } else if (mrp->mr_reads_waiting) {     /* Wakeup any readers waiting */
219                         wake_up(&mrp->mr_readerq);
220                 }
221         }
222 }
223
224 void
225 mraccunlock(mrlock_t *mrp)
226 {
227         MRLOCK(mrp);
228         mrp->mr_count--;
229         mrwake(mrp);
230         MRUNLOCK(mrp);
231 }
232
233 void
234 mrunlock(mrlock_t *mrp)
235 {
236         MRLOCK(mrp);
237         if (mrp->mr_count < 0) {
238                 mrp->mr_count = 0;
239         } else {
240                 mrp->mr_count--;
241         }
242         mrwake(mrp);
243         MRUNLOCK(mrp);
244 }
245
246 int
247 ismrlocked(mrlock_t *mrp, int type)     /* No need to lock since info can change */
248 {
249         if (type == MR_ACCESS)
250                 return (mrp->mr_count > 0); /* Read lock */
251         else if (type == MR_UPDATE)
252                 return (mrp->mr_count < 0); /* Write lock */
253         else if (type == (MR_UPDATE | MR_ACCESS))
254                 return (mrp->mr_count); /* Any type of lock held */
255         else /* Any waiters */
256                 return (mrp->mr_reads_waiting | mrp->mr_writes_waiting);
257 }
258
259 /*
260  * Demote from update to access. We better be the only thread with the
261  * lock in update mode so it should be easy to set to 1.
262  * Wake-up any readers waiting.
263  */
264
265 void
266 mrdemote(mrlock_t *mrp)
267 {
268         MRLOCK(mrp);
269         mrp->mr_count = 1;
270         if (mrp->mr_reads_waiting) {    /* Wakeup all readers waiting */
271                 wake_up(&mrp->mr_readerq);
272         }
273         MRUNLOCK(mrp);
274 }