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