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