linux-follow-link-light-needs-putlink-20050406
[openafs.git] / src / afs / afs_daemons.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 #ifdef AFS_AIX51_ENV
17 #define __FULL_PROTO
18 #include <sys/sleep.h>
19 #endif
20
21 #include "afs/sysincludes.h"    /* Standard vendor system headers */
22 #include "afsincludes.h"        /* Afs-based standard headers */
23 #include "afs/afs_stats.h"      /* statistics gathering code */
24 #include "afs/afs_cbqueue.h"
25 #ifdef AFS_AIX_ENV
26 #include <sys/adspace.h>        /* for vm_att(), vm_det() */
27 #endif
28
29
30 /* background request queue size */
31 afs_lock_t afs_xbrs;            /* lock for brs */
32 static int brsInit = 0;
33 short afs_brsWaiters = 0;       /* number of users waiting for brs buffers */
34 short afs_brsDaemons = 0;       /* number of daemons waiting for brs requests */
35 struct brequest afs_brs[NBRS];  /* request structures */
36 struct afs_osi_WaitHandle AFS_WaitHandler, AFS_CSWaitHandler;
37 static int afs_brs_count = 0;   /* request counter, to service reqs in order */
38
39 static int rxepoch_checked = 0;
40 #define afs_CheckRXEpoch() {if (rxepoch_checked == 0 && rxkad_EpochWasSet) { \
41         rxepoch_checked = 1; afs_GCUserData(/* force flag */ 1);  } }
42
43 /* PAG garbage collection */
44 /* We induce a compile error if param.h does not define AFS_GCPAGS */
45 afs_int32 afs_gcpags = AFS_GCPAGS;
46 afs_int32 afs_gcpags_procsize = 0;
47
48 afs_int32 afs_CheckServerDaemonStarted = 0;
49 #ifdef DEFAULT_PROBE_INTERVAL
50 afs_int32 PROBE_INTERVAL = DEFAULT_PROBE_INTERVAL;      /* overridding during compile */
51 #else
52 afs_int32 PROBE_INTERVAL = 180; /* default to 3 min */
53 #endif
54
55 #define PROBE_WAIT() (1000 * (PROBE_INTERVAL - ((afs_random() & 0x7fffffff) \
56                       % (PROBE_INTERVAL/2))))
57
58 void
59 afs_CheckServerDaemon(void)
60 {
61     afs_int32 now, delay, lastCheck, last10MinCheck;
62
63     afs_CheckServerDaemonStarted = 1;
64
65     while (afs_initState < 101)
66         afs_osi_Sleep(&afs_initState);
67     afs_osi_Wait(PROBE_WAIT(), &AFS_CSWaitHandler, 0);
68
69     last10MinCheck = lastCheck = osi_Time();
70     while (1) {
71         if (afs_termState == AFSOP_STOP_CS) {
72             afs_termState = AFSOP_STOP_BKG;
73             afs_osi_Wakeup(&afs_termState);
74             break;
75         }
76
77         now = osi_Time();
78         if (PROBE_INTERVAL + lastCheck <= now) {
79             afs_CheckServers(1, NULL);  /* check down servers */
80             lastCheck = now = osi_Time();
81         }
82
83         if (600 + last10MinCheck <= now) {
84             afs_Trace1(afs_iclSetp, CM_TRACE_PROBEUP, ICL_TYPE_INT32, 600);
85             afs_CheckServers(0, NULL);
86             last10MinCheck = now = osi_Time();
87         }
88         /* shutdown check. */
89         if (afs_termState == AFSOP_STOP_CS) {
90             afs_termState = AFSOP_STOP_BKG;
91             afs_osi_Wakeup(&afs_termState);
92             break;
93         }
94
95         /* Compute time to next probe. */
96         delay = PROBE_INTERVAL + lastCheck;
97         if (delay > 600 + last10MinCheck)
98             delay = 600 + last10MinCheck;
99         delay -= now;
100         if (delay < 1)
101             delay = 1;
102         afs_osi_Wait(delay * 1000, &AFS_CSWaitHandler, 0);
103     }
104     afs_CheckServerDaemonStarted = 0;
105 }
106
107 void
108 afs_Daemon(void)
109 {
110     afs_int32 code;
111     struct afs_exporter *exporter;
112     afs_int32 now;
113     afs_int32 last3MinCheck, last10MinCheck, last60MinCheck, lastNMinCheck;
114     afs_int32 last1MinCheck;
115     afs_uint32 lastCBSlotBump;
116     char cs_warned = 0;
117
118     AFS_STATCNT(afs_Daemon);
119     last1MinCheck = last3MinCheck = last60MinCheck = last10MinCheck =
120         lastNMinCheck = 0;
121
122     afs_rootFid.Fid.Volume = 0;
123     while (afs_initState < 101)
124         afs_osi_Sleep(&afs_initState);
125
126     now = osi_Time();
127     lastCBSlotBump = now;
128
129     /* when a lot of clients are booted simultaneously, they develop
130      * annoying synchronous VL server bashing behaviors.  So we stagger them.
131      */
132     last1MinCheck = now + ((afs_random() & 0x7fffffff) % 60);   /* an extra 30 */
133     last3MinCheck = now - 90 + ((afs_random() & 0x7fffffff) % 180);
134     last60MinCheck = now - 1800 + ((afs_random() & 0x7fffffff) % 3600);
135     last10MinCheck = now - 300 + ((afs_random() & 0x7fffffff) % 600);
136     lastNMinCheck = now - 90 + ((afs_random() & 0x7fffffff) % 180);
137
138     /* start off with afs_initState >= 101 (basic init done) */
139     while (1) {
140         afs_CheckCallbacks(20); /* unstat anything which will expire soon */
141
142         /* things to do every 20 seconds or less - required by protocol spec */
143         if (afs_nfsexporter)
144             afs_FlushActiveVcaches(0);  /* flush NFS writes */
145         afs_FlushVCBs(1);       /* flush queued callbacks */
146         afs_MaybeWakeupTruncateDaemon();        /* free cache space if have too */
147         rx_CheckPackets();      /* Does RX need more packets? */
148
149         now = osi_Time();
150         if (lastCBSlotBump + CBHTSLOTLEN < now) {       /* pretty time-dependant */
151             lastCBSlotBump = now;
152             if (afs_BumpBase()) {
153                 afs_CheckCallbacks(20); /* unstat anything which will expire soon */
154             }
155         }
156
157         if (last1MinCheck + 60 < now) {
158             /* things to do every minute */
159             DFlush();           /* write out dir buffers */
160             afs_WriteThroughDSlots();   /* write through cacheinfo entries */
161             afs_FlushActiveVcaches(1);  /* keep flocks held & flush nfs writes */
162 #ifdef AFS_DISCON_ENV
163             afs_StoreDirtyVcaches();
164 #endif
165             afs_CheckRXEpoch();
166             last1MinCheck = now;
167         }
168
169         if (last3MinCheck + 180 < now) {
170             afs_CheckTokenCache();      /* check for access cache resets due to expired
171                                          * tickets */
172             last3MinCheck = now;
173         }
174         if (!afs_CheckServerDaemonStarted) {
175             /* Do the check here if the correct afsd is not installed. */
176             if (!cs_warned) {
177                 cs_warned = 1;
178                 printf("Please install afsd with check server daemon.\n");
179             }
180             if (lastNMinCheck + PROBE_INTERVAL < now) {
181                 /* only check down servers */
182                 afs_CheckServers(1, NULL);
183                 lastNMinCheck = now;
184             }
185         }
186         if (last10MinCheck + 600 < now) {
187 #ifdef AFS_USERSPACE_IP_ADDR
188             extern int rxi_GetcbiInfo(void);
189 #endif
190             afs_Trace1(afs_iclSetp, CM_TRACE_PROBEUP, ICL_TYPE_INT32, 600);
191 #ifdef AFS_USERSPACE_IP_ADDR
192             if (rxi_GetcbiInfo()) {     /* addresses changed from last time */
193                 afs_FlushCBs();
194             }
195 #else /* AFS_USERSPACE_IP_ADDR */
196             if (rxi_GetIFInfo()) {      /* addresses changed from last time */
197                 afs_FlushCBs();
198             }
199 #endif /* else AFS_USERSPACE_IP_ADDR */
200             if (!afs_CheckServerDaemonStarted)
201                 afs_CheckServers(0, NULL);
202             afs_GCUserData(0);  /* gc old conns */
203             /* This is probably the wrong way of doing GC for the various exporters but it will suffice for a while */
204             for (exporter = root_exported; exporter;
205                  exporter = exporter->exp_next) {
206                 (void)EXP_GC(exporter, 0);      /* Generalize params */
207             }
208             {
209                 static int cnt = 0;
210                 if (++cnt < 12) {
211                     afs_CheckVolumeNames(AFS_VOLCHECK_EXPIRED |
212                                          AFS_VOLCHECK_BUSY);
213                 } else {
214                     cnt = 0;
215                     afs_CheckVolumeNames(AFS_VOLCHECK_EXPIRED |
216                                          AFS_VOLCHECK_BUSY |
217                                          AFS_VOLCHECK_MTPTS);
218                 }
219             }
220             last10MinCheck = now;
221         }
222         if (last60MinCheck + 3600 < now) {
223             afs_Trace1(afs_iclSetp, CM_TRACE_PROBEVOLUME, ICL_TYPE_INT32,
224                        3600);
225             afs_CheckRootVolume();
226 #if AFS_GCPAGS
227             if (afs_gcpags == AFS_GCPAGS_OK) {
228                 afs_int32 didany;
229                 afs_GCPAGs(&didany);
230             }
231 #endif
232             last60MinCheck = now;
233         }
234         if (afs_initState < 300) {      /* while things ain't rosy */
235             code = afs_CheckRootVolume();
236             if (code == 0)
237                 afs_initState = 300;    /* succeeded */
238             if (afs_initState < 200)
239                 afs_initState = 200;    /* tried once */
240             afs_osi_Wakeup(&afs_initState);
241         }
242
243         /* 18285 is because we're trying to divide evenly into 128, that is,
244          * CBSlotLen, while staying just under 20 seconds.  If CBSlotLen
245          * changes, should probably change this interval, too. 
246          * Some of the preceding actions may take quite some time, so we
247          * might not want to wait the entire interval */
248         now = 18285 - (osi_Time() - now);
249         if (now > 0) {
250             afs_osi_Wait(now, &AFS_WaitHandler, 0);
251         }
252
253         if (afs_termState == AFSOP_STOP_AFS) {
254             if (afs_CheckServerDaemonStarted)
255                 afs_termState = AFSOP_STOP_CS;
256             else
257                 afs_termState = AFSOP_STOP_BKG;
258             afs_osi_Wakeup(&afs_termState);
259             return;
260         }
261     }
262 }
263
264 int
265 afs_CheckRootVolume(void)
266 {
267     char rootVolName[32];
268     struct volume *tvp = NULL;
269     int usingDynroot = afs_GetDynrootEnable();
270     int localcell;
271
272     AFS_STATCNT(afs_CheckRootVolume);
273     if (*afs_rootVolumeName == 0) {
274         strcpy(rootVolName, "root.afs");
275     } else {
276         strcpy(rootVolName, afs_rootVolumeName);
277     }
278
279     if (usingDynroot) {
280         afs_GetDynrootFid(&afs_rootFid);
281         tvp = afs_GetVolume(&afs_rootFid, NULL, READ_LOCK);
282     } else {
283         struct cell *lc = afs_GetPrimaryCell(READ_LOCK);
284
285         if (!lc)
286             return ENOENT;
287         localcell = lc->cellNum;
288         afs_PutCell(lc, READ_LOCK);
289         tvp = afs_GetVolumeByName(rootVolName, localcell, 1, NULL, READ_LOCK);
290         if (!tvp) {
291             char buf[128];
292             int len = strlen(rootVolName);
293
294             if ((len < 9) || strcmp(&rootVolName[len - 9], ".readonly")) {
295                 strcpy(buf, rootVolName);
296                 afs_strcat(buf, ".readonly");
297                 tvp = afs_GetVolumeByName(buf, localcell, 1, NULL, READ_LOCK);
298             }
299         }
300         if (tvp) {
301             int volid = (tvp->roVol ? tvp->roVol : tvp->volume);
302             afs_rootFid.Cell = localcell;
303             if (afs_rootFid.Fid.Volume && afs_rootFid.Fid.Volume != volid
304                 && afs_globalVp) {
305                 /* If we had a root fid before and it changed location we reset
306                  * the afs_globalVp so that it will be reevaluated.
307                  * Just decrement the reference count. This only occurs during
308                  * initial cell setup and can panic the machine if we set the
309                  * count to zero and fs checkv is executed when the current
310                  * directory is /afs.
311                  */
312                 AFS_FAST_RELE(afs_globalVp);
313                 afs_globalVp = 0;
314             }
315             afs_rootFid.Fid.Volume = volid;
316             afs_rootFid.Fid.Vnode = 1;
317             afs_rootFid.Fid.Unique = 1;
318         }
319     }
320     if (tvp) {
321         afs_initState = 300;    /* won */
322         afs_osi_Wakeup(&afs_initState);
323         afs_PutVolume(tvp, READ_LOCK);
324     }
325     if (afs_rootFid.Fid.Volume)
326         return 0;
327     else
328         return ENOENT;
329 }
330
331 /* ptr_parm 0 is the pathname, size_parm 0 to the fetch is the chunk number */
332 static void
333 BPath(register struct brequest *ab)
334 {
335     register struct dcache *tdc = NULL;
336     struct vcache *tvc = NULL;
337     struct vnode *tvn = NULL;
338 #ifdef AFS_LINUX22_ENV
339     struct dentry *dp = NULL;
340 #endif
341     afs_size_t offset, len;
342     struct vrequest treq;
343     afs_int32 code;
344
345     AFS_STATCNT(BPath);
346     if ((code = afs_InitReq(&treq, ab->cred)))
347         return;
348     AFS_GUNLOCK();
349 #ifdef AFS_LINUX22_ENV
350     code = gop_lookupname((char *)ab->ptr_parm[0], AFS_UIOSYS, 1, &dp);
351     if (dp)
352         tvn = (struct vnode *)dp->d_inode;
353 #else
354     code = gop_lookupname((char *)ab->ptr_parm[0], AFS_UIOSYS, 1, &tvn);
355 #endif
356     AFS_GLOCK();
357     osi_FreeLargeSpace((char *)ab->ptr_parm[0]);        /* free path name buffer here */
358     if (code)
359         return;
360     /* now path may not have been in afs, so check that before calling our cache manager */
361     if (!tvn || !IsAfsVnode(tvn)) {
362         /* release it and give up */
363         if (tvn) {
364 #ifdef AFS_LINUX22_ENV
365             dput(dp);
366 #else
367             AFS_RELE(tvn);
368 #endif
369         }
370         return;
371     }
372     tvc = VTOAFS(tvn);
373     /* here we know its an afs vnode, so we can get the data for the chunk */
374     tdc = afs_GetDCache(tvc, ab->size_parm[0], &treq, &offset, &len, 1);
375     if (tdc) {
376         afs_PutDCache(tdc);
377     }
378 #ifdef AFS_LINUX22_ENV
379     dput(dp);
380 #else
381     AFS_RELE(tvn);
382 #endif
383 }
384
385 /* size_parm 0 to the fetch is the chunk number,
386  * ptr_parm 0 is the dcache entry to wakeup,
387  * size_parm 1 is true iff we should release the dcache entry here.
388  */
389 static void
390 BPrefetch(register struct brequest *ab)
391 {
392     register struct dcache *tdc;
393     register struct vcache *tvc;
394     afs_size_t offset, len;
395     struct vrequest treq;
396
397     AFS_STATCNT(BPrefetch);
398     if ((len = afs_InitReq(&treq, ab->cred)))
399         return;
400     tvc = ab->vc;
401     tdc = afs_GetDCache(tvc, ab->size_parm[0], &treq, &offset, &len, 1);
402     if (tdc) {
403         afs_PutDCache(tdc);
404     }
405     /* now, dude may be waiting for us to clear DFFetchReq bit; do so.  Can't
406      * use tdc from GetDCache since afs_GetDCache may fail, but someone may
407      * be waiting for our wakeup anyway.
408      */
409     tdc = (struct dcache *)(ab->ptr_parm[0]);
410     ObtainSharedLock(&tdc->lock, 640);
411     if (tdc->mflags & DFFetchReq) {
412         UpgradeSToWLock(&tdc->lock, 641);
413         tdc->mflags &= ~DFFetchReq;
414         ReleaseWriteLock(&tdc->lock);
415     } else {
416         ReleaseSharedLock(&tdc->lock);
417     }
418     afs_osi_Wakeup(&tdc->validPos);
419     if (ab->size_parm[1]) {
420         afs_PutDCache(tdc);     /* put this one back, too */
421     }
422 }
423
424
425 static void
426 BStore(register struct brequest *ab)
427 {
428     register struct vcache *tvc;
429     register afs_int32 code;
430     struct vrequest treq;
431 #if defined(AFS_SGI_ENV)
432     struct cred *tmpcred;
433 #endif
434
435     AFS_STATCNT(BStore);
436     if ((code = afs_InitReq(&treq, ab->cred)))
437         return;
438     code = 0;
439     tvc = ab->vc;
440 #if defined(AFS_SGI_ENV)
441     /*
442      * Since StoreOnLastReference can end up calling osi_SyncVM which
443      * calls into VM code that assumes that u.u_cred has the
444      * correct credentials, we set our to theirs for this xaction
445      */
446     tmpcred = OSI_GET_CURRENT_CRED();
447     OSI_SET_CURRENT_CRED(ab->cred);
448
449     /*
450      * To avoid recursion since the WriteLock may be released during VM
451      * operations, we hold the VOP_RWLOCK across this transaction as
452      * do the other callers of StoreOnLastReference
453      */
454     AFS_RWLOCK((vnode_t *) tvc, 1);
455 #endif
456     ObtainWriteLock(&tvc->lock, 209);
457     code = afs_StoreOnLastReference(tvc, &treq);
458     ReleaseWriteLock(&tvc->lock);
459 #if defined(AFS_SGI_ENV)
460     OSI_SET_CURRENT_CRED(tmpcred);
461     AFS_RWUNLOCK((vnode_t *) tvc, 1);
462 #endif
463     /* now set final return code, and wakeup anyone waiting */
464     if ((ab->flags & BUVALID) == 0) {
465         ab->code = afs_CheckCode(code, &treq, 43);      /* set final code, since treq doesn't go across processes */
466         ab->flags |= BUVALID;
467         if (ab->flags & BUWAIT) {
468             ab->flags &= ~BUWAIT;
469             afs_osi_Wakeup(ab);
470         }
471     }
472 }
473
474 /* release a held request buffer */
475 void
476 afs_BRelease(register struct brequest *ab)
477 {
478
479     AFS_STATCNT(afs_BRelease);
480     MObtainWriteLock(&afs_xbrs, 294);
481     if (--ab->refCount <= 0) {
482         ab->flags = 0;
483     }
484     if (afs_brsWaiters)
485         afs_osi_Wakeup(&afs_brsWaiters);
486     MReleaseWriteLock(&afs_xbrs);
487 }
488
489 /* return true if bkg fetch daemons are all busy */
490 int
491 afs_BBusy(void)
492 {
493     AFS_STATCNT(afs_BBusy);
494     if (afs_brsDaemons > 0)
495         return 0;
496     return 1;
497 }
498
499 struct brequest *
500 afs_BQueue(register short aopcode, register struct vcache *avc,
501            afs_int32 dontwait, afs_int32 ause, struct AFS_UCRED *acred,
502            afs_size_t asparm0, afs_size_t asparm1, void *apparm0)
503 {
504     register int i;
505     register struct brequest *tb;
506
507     AFS_STATCNT(afs_BQueue);
508     MObtainWriteLock(&afs_xbrs, 296);
509     while (1) {
510         tb = afs_brs;
511         for (i = 0; i < NBRS; i++, tb++) {
512             if (tb->refCount == 0)
513                 break;
514         }
515         if (i < NBRS) {
516             /* found a buffer */
517             tb->opcode = aopcode;
518             tb->vc = avc;
519             tb->cred = acred;
520             crhold(tb->cred);
521             if (avc) {
522                 VN_HOLD(AFSTOV(avc));
523             }
524             tb->refCount = ause + 1;
525             tb->size_parm[0] = asparm0;
526             tb->size_parm[1] = asparm1;
527             tb->ptr_parm[0] = apparm0;
528             tb->flags = 0;
529             tb->code = 0;
530             tb->ts = afs_brs_count++;
531             /* if daemons are waiting for work, wake them up */
532             if (afs_brsDaemons > 0) {
533                 afs_osi_Wakeup(&afs_brsDaemons);
534             }
535             MReleaseWriteLock(&afs_xbrs);
536             return tb;
537         }
538         if (dontwait) {
539             MReleaseWriteLock(&afs_xbrs);
540             return NULL;
541         }
542         /* no free buffers, sleep a while */
543         afs_brsWaiters++;
544         MReleaseWriteLock(&afs_xbrs);
545         afs_osi_Sleep(&afs_brsWaiters);
546         MObtainWriteLock(&afs_xbrs, 301);
547         afs_brsWaiters--;
548     }
549 }
550
551 #ifdef  AFS_AIX32_ENV
552 #ifdef AFS_AIX41_ENV
553 /* AIX 4.1 has a much different sleep/wakeup mechanism available for use. 
554  * The modifications here will work for either a UP or MP machine.
555  */
556 struct buf *afs_asyncbuf = (struct buf *)0;
557 tid_t afs_asyncbuf_cv = EVENT_NULL;
558 afs_int32 afs_biodcnt = 0;
559
560 /* in implementing this, I assumed that all external linked lists were
561  * null-terminated.  
562  *
563  * Several places in this code traverse a linked list.  The algorithm
564  * used here is probably unfamiliar to most people.  Careful examination
565  * will show that it eliminates an assignment inside the loop, as compared
566  * to the standard algorithm, at the cost of occasionally using an extra
567  * variable.
568  */
569
570 /* get_bioreq()
571  *
572  * This function obtains, and returns, a pointer to a buffer for
573  * processing by a daemon.  It sleeps until such a buffer is available.
574  * The source of buffers for it is the list afs_asyncbuf (see also 
575  * naix_vm_strategy).  This function may be invoked concurrently by
576  * several processes, that is, several instances of the same daemon.
577  * naix_vm_strategy, which adds buffers to the list, runs at interrupt
578  * level, while get_bioreq runs at process level.
579  *
580  * Since AIX 4.1 can wake just one process at a time, the separate sleep
581  * addresses have been removed.
582  * Note that the kernel_lock is held until the e_sleep_thread() occurs. 
583  * The afs_asyncbuf_lock is primarily used to serialize access between
584  * process and interrupts.
585  */
586 Simple_lock afs_asyncbuf_lock;
587 /*static*/ struct buf *
588 afs_get_bioreq()
589 {
590     struct buf *bp = NULL;
591     struct buf *bestbp;
592     struct buf **bestlbpP, **lbpP;
593     long bestage, stop;
594     struct buf *t1P, *t2P;      /* temp pointers for list manipulation */
595     int oldPriority;
596     afs_uint32 wait_ret;
597     struct afs_bioqueue *s;
598
599     /* ??? Does the forward pointer of the returned buffer need to be NULL?
600      */
601
602     /* Disable interrupts from the strategy function, and save the 
603      * prior priority level and lock access to the afs_asyncbuf.
604      */
605     AFS_GUNLOCK();
606     oldPriority = disable_lock(INTMAX, &afs_asyncbuf_lock);
607
608     while (1) {
609         if (afs_asyncbuf) {
610             /* look for oldest buffer */
611             bp = bestbp = afs_asyncbuf;
612             bestage = (long)bestbp->av_back;
613             bestlbpP = &afs_asyncbuf;
614             while (1) {
615                 lbpP = &bp->av_forw;
616                 bp = *lbpP;
617                 if (!bp)
618                     break;
619                 if ((long)bp->av_back - bestage < 0) {
620                     bestbp = bp;
621                     bestlbpP = lbpP;
622                     bestage = (long)bp->av_back;
623                 }
624             }
625             bp = bestbp;
626             *bestlbpP = bp->av_forw;
627             break;
628         } else {
629             /* If afs_asyncbuf is null, it is necessary to go to sleep.
630              * e_wakeup_one() ensures that only one thread wakes.
631              */
632             int interrupted;
633             /* The LOCK_HANDLER indicates to e_sleep_thread to only drop the
634              * lock on an MP machine.
635              */
636             interrupted =
637                 e_sleep_thread(&afs_asyncbuf_cv, &afs_asyncbuf_lock,
638                                LOCK_HANDLER | INTERRUPTIBLE);
639             if (interrupted == THREAD_INTERRUPTED) {
640                 /* re-enable interrupts from strategy */
641                 unlock_enable(oldPriority, &afs_asyncbuf_lock);
642                 AFS_GLOCK();
643                 return (NULL);
644             }
645         }                       /* end of "else asyncbuf is empty" */
646     }                           /* end of "inner loop" */
647
648     /*assert (bp); */
649
650     unlock_enable(oldPriority, &afs_asyncbuf_lock);
651     AFS_GLOCK();
652
653     /* For the convenience of other code, replace the gnodes in
654      * the b_vp field of bp and the other buffers on the b_work
655      * chain with the corresponding vnodes.   
656      *
657      * ??? what happens to the gnodes?  They're not just cut loose,
658      * are they?
659      */
660     for (t1P = bp;;) {
661         t2P = (struct buf *)t1P->b_work;
662         t1P->b_vp = ((struct gnode *)t1P->b_vp)->gn_vnode;
663         if (!t2P)
664             break;
665
666         t1P = (struct buf *)t2P->b_work;
667         t2P->b_vp = ((struct gnode *)t2P->b_vp)->gn_vnode;
668         if (!t1P)
669             break;
670     }
671
672     /* If the buffer does not specify I/O, it may immediately
673      * be returned to the caller.  This condition is detected
674      * by examining the buffer's flags (the b_flags field).  If
675      * the B_PFPROT bit is set, the buffer represents a protection
676      * violation, rather than a request for I/O.  The remainder
677      * of the outer loop handles the case where the B_PFPROT bit is clear.
678      */
679     if (bp->b_flags & B_PFPROT) {
680         return (bp);
681     }
682     return (bp);
683
684 }                               /* end of function get_bioreq() */
685
686
687 /* afs_BioDaemon
688  *
689  * This function is the daemon.  It is called from the syscall
690  * interface.  Ordinarily, a script or an administrator will run a
691  * daemon startup utility, specifying the number of I/O daemons to
692  * run.  The utility will fork off that number of processes,
693  * each making the appropriate syscall, which will cause this
694  * function to be invoked.
695  */
696 static int afs_initbiod = 0;    /* this is self-initializing code */
697 int DOvmlock = 0;
698 int
699 afs_BioDaemon(afs_int32 nbiods)
700 {
701     afs_int32 code, s, pflg = 0;
702     label_t jmpbuf;
703     struct buf *bp, *bp1, *tbp1, *tbp2; /* temp pointers only */
704     caddr_t tmpaddr;
705     struct vnode *vp;
706     struct vcache *vcp;
707     char tmperr;
708     if (!afs_initbiod) {
709         /* XXX ###1 XXX */
710         afs_initbiod = 1;
711         /* pin lock, since we'll be using it in an interrupt. */
712         lock_alloc(&afs_asyncbuf_lock, LOCK_ALLOC_PIN, 2, 1);
713         simple_lock_init(&afs_asyncbuf_lock);
714         pin(&afs_asyncbuf, sizeof(struct buf *));
715         pin(&afs_asyncbuf_cv, sizeof(afs_int32));
716     }
717
718     /* Ignore HUP signals... */
719     {
720         sigset_t sigbits, osigbits;
721         /*
722          * add SIGHUP to the set of already masked signals
723          */
724         SIGFILLSET(sigbits);    /* allow all signals    */
725         SIGDELSET(sigbits, SIGHUP);     /*   except SIGHUP      */
726         limit_sigs(&sigbits, &osigbits);        /*   and already masked */
727     }
728     /* Main body starts here -- this is an intentional infinite loop, and
729      * should NEVER exit 
730      *
731      * Now, the loop will exit if get_bioreq() returns NULL, indicating 
732      * that we've been interrupted.
733      */
734     while (1) {
735         bp = afs_get_bioreq();
736         if (!bp)
737             break;              /* we were interrupted */
738         if (code = setjmpx(&jmpbuf)) {
739             /* This should not have happend, maybe a lack of resources  */
740             AFS_GUNLOCK();
741             s = disable_lock(INTMAX, &afs_asyncbuf_lock);
742             for (bp1 = bp; bp; bp = bp1) {
743                 if (bp1)
744                     bp1 = (struct buf *)bp1->b_work;
745                 bp->b_actf = 0;
746                 bp->b_error = code;
747                 bp->b_flags |= B_ERROR;
748                 iodone(bp);
749             }
750             unlock_enable(s, &afs_asyncbuf_lock);
751             AFS_GLOCK();
752             continue;
753         }
754         vcp = VTOAFS(bp->b_vp);
755         if (bp->b_flags & B_PFSTORE) {  /* XXXX */
756             ObtainWriteLock(&vcp->lock, 404);
757             if (vcp->v.v_gnode->gn_mwrcnt) {
758                 afs_offs_t newlength =
759                     (afs_offs_t) dbtob(bp->b_blkno) + bp->b_bcount;
760                 if (vcp->m.Length < newlength) {
761                     afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH,
762                                ICL_TYPE_STRING, __FILE__, ICL_TYPE_LONG,
763                                __LINE__, ICL_TYPE_OFFSET,
764                                ICL_HANDLE_OFFSET(vcp->m.Length),
765                                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(newlength));
766                     vcp->m.Length = newlength;
767                 }
768             }
769             ReleaseWriteLock(&vcp->lock);
770         }
771         /* If the buffer represents a protection violation, rather than
772          * an actual request for I/O, no special action need be taken.  
773          */
774         if (bp->b_flags & B_PFPROT) {
775             iodone(bp);         /* Notify all users of the buffer that we're done */
776             clrjmpx(&jmpbuf);
777             continue;
778         }
779         if (DOvmlock)
780             ObtainWriteLock(&vcp->pvmlock, 211);
781         /*
782          * First map its data area to a region in the current address space
783          * by calling vm_att with the subspace identifier, and a pointer to
784          * the data area.  vm_att returns  a new data area pointer, but we
785          * also want to hang onto the old one.
786          */
787         tmpaddr = bp->b_baddr;
788         bp->b_baddr = (caddr_t) vm_att(bp->b_xmemd.subspace_id, tmpaddr);
789         tmperr = afs_ustrategy(bp);     /* temp variable saves offset calculation */
790         if (tmperr) {           /* in non-error case */
791             bp->b_flags |= B_ERROR;     /* should other flags remain set ??? */
792             bp->b_error = tmperr;
793         }
794
795         /* Unmap the buffer's data area by calling vm_det.  Reset data area
796          * to the value that we saved above.
797          */
798         vm_det(bp->b_baddr);
799         bp->b_baddr = tmpaddr;
800
801         /*
802          * buffer may be linked with other buffers via the b_work field.
803          * See also naix_vm_strategy.  For each buffer in the chain (including
804          * bp) notify all users of the buffer that the daemon is finished
805          * using it by calling iodone.  
806          * assumes iodone can modify the b_work field.
807          */
808         for (tbp1 = bp;;) {
809             tbp2 = (struct buf *)tbp1->b_work;
810             iodone(tbp1);
811             if (!tbp2)
812                 break;
813
814             tbp1 = (struct buf *)tbp2->b_work;
815             iodone(tbp2);
816             if (!tbp1)
817                 break;
818         }
819         if (DOvmlock)
820             ReleaseWriteLock(&vcp->pvmlock);    /* Unlock the vnode.  */
821         clrjmpx(&jmpbuf);
822     }                           /* infinite loop (unless we're interrupted) */
823 }                               /* end of afs_BioDaemon() */
824
825 #else /* AFS_AIX41_ENV */
826
827
828 #define squeue afs_q
829 struct afs_bioqueue {
830     struct squeue lruq;
831     int sleeper;
832     int cnt;
833 };
834 struct afs_bioqueue afs_bioqueue;
835 struct buf *afs_busyq = NULL;
836 struct buf *afs_asyncbuf;
837 afs_int32 afs_biodcnt = 0;
838
839 /* in implementing this, I assumed that all external linked lists were
840  * null-terminated.  
841  *
842  * Several places in this code traverse a linked list.  The algorithm
843  * used here is probably unfamiliar to most people.  Careful examination
844  * will show that it eliminates an assignment inside the loop, as compared
845  * to the standard algorithm, at the cost of occasionally using an extra
846  * variable.
847  */
848
849 /* get_bioreq()
850  *
851  * This function obtains, and returns, a pointer to a buffer for
852  * processing by a daemon.  It sleeps until such a buffer is available.
853  * The source of buffers for it is the list afs_asyncbuf (see also 
854  * naix_vm_strategy).  This function may be invoked concurrently by
855  * several processes, that is, several instances of the same daemon.
856  * naix_vm_strategy, which adds buffers to the list, runs at interrupt
857  * level, while get_bioreq runs at process level.
858  *
859  * The common kernel paradigm of sleeping and waking up, in which all the
860  * competing processes sleep waiting for wakeups on one address, is not
861  * followed here.  Instead, the following paradigm is used:  when a daemon
862  * goes to sleep, it checks for other sleeping daemons.  If there aren't any,
863  * it sleeps on the address of variable afs_asyncbuf.  But if there is
864  * already a daemon sleeping on that address, it threads its own unique
865  * address onto a list, and sleeps on that address.  This way, every 
866  * sleeper is sleeping on a different address, and every wakeup wakes up
867  * exactly one daemon.  This prevents a whole bunch of daemons from waking
868  * up and then immediately having to go back to sleep.  This provides a
869  * performance gain and makes the I/O scheduling a bit more deterministic.
870  * The list of sleepers is variable afs_bioqueue.  The unique address
871  * on which to sleep is passed to get_bioreq as its parameter.
872  */
873 /*static*/ struct buf *
874 afs_get_bioreq(self)
875      struct afs_bioqueue *self; /* address on which to sleep */
876
877 {
878     struct buf *bp = NULL;
879     struct buf *bestbp;
880     struct buf **bestlbpP, **lbpP;
881     int bestage, stop;
882     struct buf *t1P, *t2P;      /* temp pointers for list manipulation */
883     int oldPriority;
884     afs_uint32 wait_ret;
885     struct afs_bioqueue *s;
886
887     /* ??? Does the forward pointer of the returned buffer need to be NULL?
888      */
889
890     /* Disable interrupts from the strategy function, and save the 
891      * prior priority level
892      */
893     oldPriority = i_disable(INTMAX);
894
895     /* Each iteration of following loop either pulls
896      * a buffer off afs_asyncbuf, or sleeps.  
897      */
898     while (1) {                 /* inner loop */
899         if (afs_asyncbuf) {
900             /* look for oldest buffer */
901             bp = bestbp = afs_asyncbuf;
902             bestage = (int)bestbp->av_back;
903             bestlbpP = &afs_asyncbuf;
904             while (1) {
905                 lbpP = &bp->av_forw;
906                 bp = *lbpP;
907                 if (!bp)
908                     break;
909                 if ((int)bp->av_back - bestage < 0) {
910                     bestbp = bp;
911                     bestlbpP = lbpP;
912                     bestage = (int)bp->av_back;
913                 }
914             }
915             bp = bestbp;
916             *bestlbpP = bp->av_forw;
917             break;
918         } else {
919             int interrupted;
920
921             /* If afs_asyncbuf is null, it is necessary to go to sleep.
922              * There are two possibilities:  either there is already a
923              * daemon that is sleeping on the address of afs_asyncbuf,
924              * or there isn't. 
925              */
926             if (afs_bioqueue.sleeper) {
927                 /* enqueue */
928                 QAdd(&(afs_bioqueue.lruq), &(self->lruq));
929                 interrupted = sleep((caddr_t) self, PCATCH | (PZERO + 1));
930                 if (self->lruq.next != &self->lruq) {   /* XXX ##3 XXX */
931                     QRemove(&(self->lruq));     /* dequeue */
932                 }
933                 self->cnt++;
934                 afs_bioqueue.sleeper = FALSE;
935                 if (interrupted) {
936                     /* re-enable interrupts from strategy */
937                     i_enable(oldPriority);
938                     return (NULL);
939                 }
940                 continue;
941             } else {
942                 afs_bioqueue.sleeper = TRUE;
943                 interrupted =
944                     sleep((caddr_t) & afs_asyncbuf, PCATCH | (PZERO + 1));
945                 afs_bioqueue.sleeper = FALSE;
946                 if (interrupted) {
947                     /*
948                      * We need to wakeup another daemon if present
949                      * since we were waiting on afs_asyncbuf.
950                      */
951 #ifdef  notdef                  /* The following doesn't work as advertised */
952                     if (afs_bioqueue.lruq.next != &afs_bioqueue.lruq) {
953                         struct squeue *bq = afs_bioqueue.lruq.next;
954                         QRemove(bq);
955                         wakeup(bq);
956                     }
957 #endif
958                     /* re-enable interrupts from strategy */
959                     i_enable(oldPriority);
960                     return (NULL);
961                 }
962                 continue;
963             }
964
965         }                       /* end of "else asyncbuf is empty" */
966     }                           /* end of "inner loop" */
967
968     /*assert (bp); */
969
970     i_enable(oldPriority);      /* re-enable interrupts from strategy */
971
972     /* For the convenience of other code, replace the gnodes in
973      * the b_vp field of bp and the other buffers on the b_work
974      * chain with the corresponding vnodes.   
975      *
976      * ??? what happens to the gnodes?  They're not just cut loose,
977      * are they?
978      */
979     for (t1P = bp;;) {
980         t2P = (struct buf *)t1P->b_work;
981         t1P->b_vp = ((struct gnode *)t1P->b_vp)->gn_vnode;
982         if (!t2P)
983             break;
984
985         t1P = (struct buf *)t2P->b_work;
986         t2P->b_vp = ((struct gnode *)t2P->b_vp)->gn_vnode;
987         if (!t1P)
988             break;
989     }
990
991     /* If the buffer does not specify I/O, it may immediately
992      * be returned to the caller.  This condition is detected
993      * by examining the buffer's flags (the b_flags field).  If
994      * the B_PFPROT bit is set, the buffer represents a protection
995      * violation, rather than a request for I/O.  The remainder
996      * of the outer loop handles the case where the B_PFPROT bit is clear.
997      */
998     if (bp->b_flags & B_PFPROT) {
999         return (bp);
1000     }
1001
1002     /* wake up another process to handle the next buffer, and return
1003      * bp to the caller.
1004      */
1005     oldPriority = i_disable(INTMAX);
1006
1007     /* determine where to find the sleeping process. 
1008      * There are two cases: either it is sleeping on
1009      * afs_asyncbuf, or it is sleeping on its own unique
1010      * address.  These cases are distinguished by examining
1011      * the sleeper field of afs_bioqueue.
1012      */
1013     if (afs_bioqueue.sleeper) {
1014         wakeup(&afs_asyncbuf);
1015     } else {
1016         if (afs_bioqueue.lruq.next == &afs_bioqueue.lruq) {
1017             /* queue is empty, what now? ??? */
1018             /* Should this be impossible, or does    */
1019             /* it just mean that nobody is sleeping? */ ;
1020         } else {
1021             struct squeue *bq = afs_bioqueue.lruq.next;
1022             QRemove(bq);
1023             QInit(bq);
1024             wakeup(bq);
1025             afs_bioqueue.sleeper = TRUE;
1026         }
1027     }
1028     i_enable(oldPriority);      /* re-enable interrupts from strategy */
1029     return (bp);
1030
1031 }                               /* end of function get_bioreq() */
1032
1033
1034 /* afs_BioDaemon
1035  *
1036  * This function is the daemon.  It is called from the syscall
1037  * interface.  Ordinarily, a script or an administrator will run a
1038  * daemon startup utility, specifying the number of I/O daemons to
1039  * run.  The utility will fork off that number of processes,
1040  * each making the appropriate syscall, which will cause this
1041  * function to be invoked.
1042  */
1043 static int afs_initbiod = 0;    /* this is self-initializing code */
1044 int DOvmlock = 0;
1045 afs_BioDaemon(nbiods)
1046      afs_int32 nbiods;
1047 {
1048     struct afs_bioqueue *self;
1049     afs_int32 code, s, pflg = 0;
1050     label_t jmpbuf;
1051     struct buf *bp, *bp1, *tbp1, *tbp2; /* temp pointers only */
1052     caddr_t tmpaddr;
1053     struct vnode *vp;
1054     struct vcache *vcp;
1055     char tmperr;
1056     if (!afs_initbiod) {
1057         /* XXX ###1 XXX */
1058         afs_initbiod = 1;
1059         /* Initialize the queue of waiting processes, afs_bioqueue.  */
1060         QInit(&(afs_bioqueue.lruq));
1061     }
1062
1063     /* establish ourself as a kernel process so shutdown won't kill us */
1064 /*    u.u_procp->p_flag |= SKPROC;*/
1065
1066     /* Initialize a token (self) to use in the queue of sleeping processes.   */
1067     self = (struct afs_bioqueue *)afs_osi_Alloc(sizeof(struct afs_bioqueue));
1068     pin(self, sizeof(struct afs_bioqueue));     /* fix in memory */
1069     memset(self, 0, sizeof(*self));
1070     QInit(&(self->lruq));       /* initialize queue entry pointers */
1071
1072
1073     /* Ignore HUP signals... */
1074     SIGDELSET(u.u_procp->p_sig, SIGHUP);
1075     SIGADDSET(u.u_procp->p_sigignore, SIGHUP);
1076     SIGDELSET(u.u_procp->p_sigcatch, SIGHUP);
1077     /* Main body starts here -- this is an intentional infinite loop, and
1078      * should NEVER exit 
1079      *
1080      * Now, the loop will exit if get_bioreq() returns NULL, indicating 
1081      * that we've been interrupted.
1082      */
1083     while (1) {
1084         bp = afs_get_bioreq(self);
1085         if (!bp)
1086             break;              /* we were interrupted */
1087         if (code = setjmpx(&jmpbuf)) {
1088             /* This should not have happend, maybe a lack of resources  */
1089             s = splimp();
1090             for (bp1 = bp; bp; bp = bp1) {
1091                 if (bp1)
1092                     bp1 = bp1->b_work;
1093                 bp->b_actf = 0;
1094                 bp->b_error = code;
1095                 bp->b_flags |= B_ERROR;
1096                 iodone(bp);
1097             }
1098             splx(s);
1099             continue;
1100         }
1101         vcp = VTOAFS(bp->b_vp);
1102         if (bp->b_flags & B_PFSTORE) {
1103             ObtainWriteLock(&vcp->lock, 210);
1104             if (vcp->v.v_gnode->gn_mwrcnt) {
1105                 afs_offs_t newlength =
1106                     (afs_offs_t) dbtob(bp->b_blkno) + bp->b_bcount;
1107                 if (vcp->m.Length < newlength) {
1108                     afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH,
1109                                ICL_TYPE_STRING, __FILE__, ICL_TYPE_LONG,
1110                                __LINE__, ICL_TYPE_OFFSET,
1111                                ICL_HANDLE_OFFSET(vcp->m.Length),
1112                                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(newlength));
1113                     vcp->m.Length = newlength;
1114                 }
1115             }
1116             ReleaseWriteLock(&vcp->lock);
1117         }
1118         /* If the buffer represents a protection violation, rather than
1119          * an actual request for I/O, no special action need be taken.  
1120          */
1121         if (bp->b_flags & B_PFPROT) {
1122             iodone(bp);         /* Notify all users of the buffer that we're done */
1123             continue;
1124         }
1125         if (DOvmlock)
1126             ObtainWriteLock(&vcp->pvmlock, 558);
1127         /*
1128          * First map its data area to a region in the current address space
1129          * by calling vm_att with the subspace identifier, and a pointer to
1130          * the data area.  vm_att returns  a new data area pointer, but we
1131          * also want to hang onto the old one.
1132          */
1133         tmpaddr = bp->b_baddr;
1134         bp->b_baddr = vm_att(bp->b_xmemd.subspace_id, tmpaddr);
1135         tmperr = afs_ustrategy(bp);     /* temp variable saves offset calculation */
1136         if (tmperr) {           /* in non-error case */
1137             bp->b_flags |= B_ERROR;     /* should other flags remain set ??? */
1138             bp->b_error = tmperr;
1139         }
1140
1141         /* Unmap the buffer's data area by calling vm_det.  Reset data area
1142          * to the value that we saved above.
1143          */
1144         vm_det(bp->b_un.b_addr);
1145         bp->b_baddr = tmpaddr;
1146
1147         /*
1148          * buffer may be linked with other buffers via the b_work field.
1149          * See also naix_vm_strategy.  For each buffer in the chain (including
1150          * bp) notify all users of the buffer that the daemon is finished
1151          * using it by calling iodone.  
1152          * assumes iodone can modify the b_work field.
1153          */
1154         for (tbp1 = bp;;) {
1155             tbp2 = (struct buf *)tbp1->b_work;
1156             iodone(tbp1);
1157             if (!tbp2)
1158                 break;
1159
1160             tbp1 = (struct buf *)tbp2->b_work;
1161             iodone(tbp2);
1162             if (!tbp1)
1163                 break;
1164         }
1165         if (DOvmlock)
1166             ReleaseWriteLock(&vcp->pvmlock);    /* Unlock the vnode.  */
1167         clrjmpx(&jmpbuf);
1168     }                           /* infinite loop (unless we're interrupted) */
1169     unpin(self, sizeof(struct afs_bioqueue));
1170     afs_osi_Free(self, sizeof(struct afs_bioqueue));
1171 }                               /* end of afs_BioDaemon() */
1172 #endif /* AFS_AIX41_ENV */
1173 #endif /* AFS_AIX32_ENV */
1174
1175
1176 int afs_nbrs = 0;
1177 void
1178 afs_BackgroundDaemon(void)
1179 {
1180     struct brequest *tb;
1181     int i, foundAny;
1182
1183     AFS_STATCNT(afs_BackgroundDaemon);
1184     /* initialize subsystem */
1185     if (brsInit == 0) {
1186         LOCK_INIT(&afs_xbrs, "afs_xbrs");
1187         memset((char *)afs_brs, 0, sizeof(afs_brs));
1188         brsInit = 1;
1189 #if defined (AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1190         /*
1191          * steal the first daemon for doing delayed DSlot flushing
1192          * (see afs_GetDownDSlot)
1193          */
1194         AFS_GUNLOCK();
1195         afs_sgidaemon();
1196         return;
1197 #endif
1198     }
1199     afs_nbrs++;
1200
1201     MObtainWriteLock(&afs_xbrs, 302);
1202     while (1) {
1203         int min_ts = 0;
1204         struct brequest *min_tb = NULL;
1205
1206         if (afs_termState == AFSOP_STOP_BKG) {
1207             if (--afs_nbrs <= 0)
1208                 afs_termState = AFSOP_STOP_TRUNCDAEMON;
1209             MReleaseWriteLock(&afs_xbrs);
1210             afs_osi_Wakeup(&afs_termState);
1211             return;
1212         }
1213
1214         /* find a request */
1215         tb = afs_brs;
1216         foundAny = 0;
1217         for (i = 0; i < NBRS; i++, tb++) {
1218             /* look for request with smallest ts */
1219             if ((tb->refCount > 0) && !(tb->flags & BSTARTED)) {
1220                 /* new request, not yet picked up */
1221                 if ((min_tb && (min_ts - tb->ts > 0)) || !min_tb) {
1222                     min_tb = tb;
1223                     min_ts = tb->ts;
1224                 }
1225             }
1226         }
1227         if ((tb = min_tb)) {
1228             /* claim and process this request */
1229             tb->flags |= BSTARTED;
1230             MReleaseWriteLock(&afs_xbrs);
1231             foundAny = 1;
1232             afs_Trace1(afs_iclSetp, CM_TRACE_BKG1, ICL_TYPE_INT32,
1233                        tb->opcode);
1234             if (tb->opcode == BOP_FETCH)
1235                 BPrefetch(tb);
1236             else if (tb->opcode == BOP_STORE)
1237                 BStore(tb);
1238             else if (tb->opcode == BOP_PATH)
1239                 BPath(tb);
1240             else
1241                 panic("background bop");
1242             if (tb->vc) {
1243                 AFS_RELE(AFSTOV(tb->vc));       /* MUST call vnode layer or could lose vnodes */
1244                 tb->vc = NULL;
1245             }
1246             if (tb->cred) {
1247                 crfree(tb->cred);
1248                 tb->cred = (struct AFS_UCRED *)0;
1249             }
1250             afs_BRelease(tb);   /* this grabs and releases afs_xbrs lock */
1251             MObtainWriteLock(&afs_xbrs, 305);
1252         }
1253         if (!foundAny) {
1254             /* wait for new request */
1255             afs_brsDaemons++;
1256             MReleaseWriteLock(&afs_xbrs);
1257             afs_osi_Sleep(&afs_brsDaemons);
1258             MObtainWriteLock(&afs_xbrs, 307);
1259             afs_brsDaemons--;
1260         }
1261     }
1262 }
1263
1264
1265 void
1266 shutdown_daemons(void)
1267 {
1268     AFS_STATCNT(shutdown_daemons);
1269     if (afs_cold_shutdown) {
1270         afs_brsDaemons = brsInit = 0;
1271         rxepoch_checked = afs_nbrs = 0;
1272         memset((char *)afs_brs, 0, sizeof(afs_brs));
1273         memset((char *)&afs_xbrs, 0, sizeof(afs_lock_t));
1274         afs_brsWaiters = 0;
1275 #ifdef AFS_AIX32_ENV
1276 #ifdef AFS_AIX41_ENV
1277         lock_free(&afs_asyncbuf_lock);
1278         unpin(&afs_asyncbuf, sizeof(struct buf *));
1279         unpin(&afs_asyncbuf_cv, sizeof(afs_int32));
1280 #else /* AFS_AIX41_ENV */
1281         afs_busyq = NULL;
1282         afs_biodcnt = 0;
1283         memset((char *)&afs_bioqueue, 0, sizeof(struct afs_bioqueue));
1284 #endif
1285         afs_initbiod = 0;
1286 #endif
1287     }
1288 }
1289
1290 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1291 /*
1292  * sgi - daemon - handles certain operations that otherwise
1293  * would use up too much kernel stack space
1294  *
1295  * This all assumes that since the caller must have the xdcache lock
1296  * exclusively that the list will never be more than one long
1297  * and noone else can attempt to add anything until we're done.
1298  */
1299 SV_TYPE afs_sgibksync;
1300 SV_TYPE afs_sgibkwait;
1301 lock_t afs_sgibklock;
1302 struct dcache *afs_sgibklist;
1303
1304 int
1305 afs_sgidaemon(void)
1306 {
1307     int s;
1308     struct dcache *tdc;
1309
1310     if (afs_sgibklock == NULL) {
1311         SV_INIT(&afs_sgibksync, "bksync", 0, 0);
1312         SV_INIT(&afs_sgibkwait, "bkwait", 0, 0);
1313         SPINLOCK_INIT(&afs_sgibklock, "bklock");
1314     }
1315     s = SPLOCK(afs_sgibklock);
1316     for (;;) {
1317         /* wait for something to do */
1318         SP_WAIT(afs_sgibklock, s, &afs_sgibksync, PINOD);
1319         osi_Assert(afs_sgibklist);
1320
1321         /* XX will probably need to generalize to real list someday */
1322         s = SPLOCK(afs_sgibklock);
1323         while (afs_sgibklist) {
1324             tdc = afs_sgibklist;
1325             afs_sgibklist = NULL;
1326             SPUNLOCK(afs_sgibklock, s);
1327             AFS_GLOCK();
1328             tdc->dflags &= ~DFEntryMod;
1329             afs_WriteDCache(tdc, 1);
1330             AFS_GUNLOCK();
1331             s = SPLOCK(afs_sgibklock);
1332         }
1333
1334         /* done all the work - wake everyone up */
1335         while (SV_SIGNAL(&afs_sgibkwait));
1336     }
1337 }
1338 #endif