linux-4gb-32bit-file-fix-20050802
[openafs.git] / src / afs / LINUX / osi_sleep.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID
14     ("$Header$");
15
16 #include "afs/sysincludes.h"    /* Standard vendor system headers */
17 #include "afsincludes.h"        /* Afs-based standard headers */
18 #include "afs/afs_stats.h"      /* afs statistics */
19
20 static int osi_TimedSleep(char *event, afs_int32 ams, int aintok);
21
22 static char waitV, dummyV;
23
24 void
25 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
26 {
27     AFS_STATCNT(osi_InitWaitHandle);
28     achandle->proc = (caddr_t) 0;
29 }
30
31 /* cancel osi_Wait */
32 void
33 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
34 {
35     caddr_t proc;
36
37     AFS_STATCNT(osi_CancelWait);
38     proc = achandle->proc;
39     if (proc == 0)
40         return;
41     achandle->proc = (caddr_t) 0;       /* so dude can figure out he was signalled */
42     afs_osi_Wakeup(&waitV);
43 }
44
45 /* afs_osi_Wait
46  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
47  * Returns 0 if timeout and EINTR if signalled.
48  */
49 int
50 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
51 {
52     afs_int32 endTime;
53     int code;
54
55     AFS_STATCNT(osi_Wait);
56     endTime = osi_Time() + (ams / 1000);
57     if (ahandle)
58         ahandle->proc = (caddr_t) current;
59
60     do {
61         AFS_ASSERT_GLOCK();
62         code = osi_TimedSleep(&waitV, ams, 1);
63         if (code)
64             break;
65         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
66             /* we've been signalled */
67             break;
68         }
69     } while (osi_Time() < endTime);
70     return code;
71 }
72
73
74
75
76 typedef struct afs_event {
77     struct afs_event *next;     /* next in hash chain */
78     char *event;                /* lwp event: an address */
79     int refcount;               /* Is it in use? */
80     int seq;                    /* Sequence number: this is incremented
81                                  * by wakeup calls; wait will not return until
82                                  * it changes */
83 #if defined(AFS_LINUX24_ENV)
84     wait_queue_head_t cond;
85 #else
86     struct wait_queue *cond;
87 #endif
88 } afs_event_t;
89
90 #define HASHSIZE 128
91 afs_event_t *afs_evhasht[HASHSIZE];     /* Hash table for events */
92 #define afs_evhash(event)       (afs_uint32) ((((long)event)>>2) & (HASHSIZE-1));
93 int afs_evhashcnt = 0;
94
95 /* Get and initialize event structure corresponding to lwp event (i.e. address)
96  * */
97 static afs_event_t *
98 afs_getevent(char *event)
99 {
100     afs_event_t *evp, *newp = 0;
101     int hashcode;
102
103     AFS_ASSERT_GLOCK();
104     hashcode = afs_evhash(event);
105     evp = afs_evhasht[hashcode];
106     while (evp) {
107         if (evp->event == event) {
108             evp->refcount++;
109             return evp;
110         }
111         if (evp->refcount == 0)
112             newp = evp;
113         evp = evp->next;
114     }
115     if (!newp)
116         return NULL;
117
118     newp->event = event;
119     newp->refcount = 1;
120     return newp;
121 }
122
123 /* afs_addevent -- allocates a new event for the address.  It isn't returned;
124  *     instead, afs_getevent should be called again.  Thus, the real effect of
125  *     this routine is to add another event to the hash bucket for this
126  *     address.
127  *
128  * Locks:
129  *     Called with GLOCK held. However the function might drop
130  *     GLOCK when it calls osi_AllocSmallSpace for allocating
131  *     a new event (In Linux, the allocator drops GLOCK to avoid
132  *     a deadlock).
133  */
134
135 static void
136 afs_addevent(char *event)
137 {
138     int hashcode;
139     afs_event_t *newp;
140
141     AFS_ASSERT_GLOCK();
142     hashcode = afs_evhash(event);
143     newp = osi_linux_alloc(sizeof(afs_event_t), 0);
144     afs_evhashcnt++;
145     newp->next = afs_evhasht[hashcode];
146     afs_evhasht[hashcode] = newp;
147 #if defined(AFS_LINUX24_ENV)
148     init_waitqueue_head(&newp->cond);
149 #else
150     init_waitqueue(&newp->cond);
151 #endif
152     newp->seq = 0;
153     newp->event = &dummyV;      /* Dummy address for new events */
154     newp->refcount = 0;
155 }
156
157 #ifndef set_current_state
158 #define set_current_state(x)            current->state = (x);
159 #endif
160
161 /* Release the specified event */
162 #define relevent(evp) ((evp)->refcount--)
163
164 /* afs_osi_SleepSig
165  *
166  * Waits for an event to be notified, returning early if a signal
167  * is received.  Returns EINTR if signaled, and 0 otherwise.
168  */
169 int
170 afs_osi_SleepSig(void *event)
171 {
172     struct afs_event *evp;
173     int seq, retval;
174 #ifdef DECLARE_WAITQUEUE
175     DECLARE_WAITQUEUE(wait, current);
176 #else
177     struct wait_queue wait = { current, NULL };
178 #endif
179
180     evp = afs_getevent(event);
181     if (!evp) {
182         afs_addevent(event);
183         evp = afs_getevent(event);
184     }
185
186     seq = evp->seq;
187     retval = 0;
188
189     add_wait_queue(&evp->cond, &wait);
190     while (seq == evp->seq) {
191         set_current_state(TASK_INTERRUPTIBLE);
192         AFS_ASSERT_GLOCK();
193         AFS_GUNLOCK();
194         schedule();
195 #ifdef AFS_LINUX26_ENV
196 #ifdef CONFIG_PM
197         if (current->flags & PF_FREEZE)
198             refrigerator(PF_FREEZE);
199 #endif
200 #endif
201         AFS_GLOCK();
202         if (signal_pending(current)) {
203             retval = EINTR;
204             break;
205         }
206     }
207     remove_wait_queue(&evp->cond, &wait);
208     set_current_state(TASK_RUNNING);
209
210     relevent(evp);
211     return retval;
212 }
213
214 /* afs_osi_Sleep -- waits for an event to be notified, ignoring signals.
215  * - NOTE: that on Linux, there are circumstances in which TASK_INTERRUPTIBLE
216  *   can wake up, even if all signals are blocked
217  * - TODO: handle signals correctly by passing an indication back to the
218  *   caller that the wait has been interrupted and the stack should be cleaned
219  *   up preparatory to signal delivery
220  */
221 void
222 afs_osi_Sleep(void *event)
223 {
224     sigset_t saved_set;
225
226     SIG_LOCK(current);
227     saved_set = current->blocked;
228     sigfillset(&current->blocked);
229     RECALC_SIGPENDING(current);
230     SIG_UNLOCK(current);
231
232     afs_osi_SleepSig(event);
233
234     SIG_LOCK(current);
235     current->blocked = saved_set;
236     RECALC_SIGPENDING(current);
237     SIG_UNLOCK(current);
238 }
239
240 /* osi_TimedSleep
241  * 
242  * Arguments:
243  * event - event to sleep on
244  * ams --- max sleep time in milliseconds
245  * aintok - 1 if should sleep interruptibly
246  *
247  * Returns 0 if timeout, EINTR if signalled, and EGAIN if it might
248  * have raced.
249  */
250 static int
251 osi_TimedSleep(char *event, afs_int32 ams, int aintok)
252 {
253     int code = 0;
254     long ticks = (ams * HZ / 1000) + 1;
255     struct afs_event *evp;
256 #ifdef DECLARE_WAITQUEUE
257     DECLARE_WAITQUEUE(wait, current);
258 #else
259     struct wait_queue wait = { current, NULL };
260 #endif
261
262     evp = afs_getevent(event);
263     if (!evp) {
264         afs_addevent(event);
265         evp = afs_getevent(event);
266     }
267
268     add_wait_queue(&evp->cond, &wait);
269     set_current_state(TASK_INTERRUPTIBLE);
270     /* always sleep TASK_INTERRUPTIBLE to keep load average
271      * from artifically increasing. */
272     AFS_GUNLOCK();
273
274     if (aintok) {
275         if (schedule_timeout(ticks))
276             code = EINTR;
277     } else
278         schedule_timeout(ticks);
279 #ifdef AFS_LINUX26_ENV
280 #ifdef CONFIG_PM
281     if (current->flags & PF_FREEZE)
282         refrigerator(PF_FREEZE);
283 #endif
284 #endif
285
286     AFS_GLOCK();
287     remove_wait_queue(&evp->cond, &wait);
288     set_current_state(TASK_RUNNING);
289
290     relevent(evp);
291
292     return code;
293 }
294
295
296 int
297 afs_osi_Wakeup(void *event)
298 {
299     int ret = 2;
300     struct afs_event *evp;
301
302     evp = afs_getevent(event);
303     if (!evp)                   /* No sleepers */
304         return 1;
305
306     if (evp->refcount > 1) {
307         evp->seq++;
308         wake_up(&evp->cond);
309         ret = 0;
310     }
311     relevent(evp);
312     return ret;
313 }