2 * Copyright 2000, International Business Machines Corporation and others.
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
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"
16 #include <sys/adspace.h> /* for vm_att(), vm_det() */
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;
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); } }
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();
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;
48 afs_int32 afs_CheckServerDaemonStarted = 0;
49 afs_int32 PROBE_INTERVAL=180; /* default to 3 min */
51 #define PROBE_WAIT() (1000 * (PROBE_INTERVAL - ((afs_random() & 0x7fffffff) \
52 % (PROBE_INTERVAL/2))))
54 afs_CheckServerDaemon()
56 afs_int32 now, delay, lastCheck, last10MinCheck;
58 afs_CheckServerDaemonStarted = 1;
60 while (afs_initState < 101) afs_osi_Sleep(&afs_initState);
61 afs_osi_Wait(PROBE_WAIT(), &AFS_CSWaitHandler, 0);
63 last10MinCheck = lastCheck = osi_Time();
65 if (afs_termState == AFSOP_STOP_CS) {
66 afs_termState = AFSOP_STOP_BKG;
67 afs_osi_Wakeup(&afs_termState);
72 if (PROBE_INTERVAL + lastCheck <= now) {
73 afs_CheckServers(1, (struct cell *) 0); /* check down servers */
74 lastCheck = now = osi_Time();
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();
83 if (afs_termState == AFSOP_STOP_CS) {
84 afs_termState = AFSOP_STOP_BKG;
85 afs_osi_Wakeup(&afs_termState);
89 /* Compute time to next probe. */
90 delay = PROBE_INTERVAL + lastCheck;
91 if (delay > 600 + last10MinCheck)
92 delay = 600 + last10MinCheck;
96 afs_osi_Wait(delay * 1000, &AFS_CSWaitHandler, 0);
98 afs_CheckServerDaemonStarted = 0;
103 extern struct afs_exporter *root_exported;
104 struct afs_exporter *exporter;
106 afs_int32 last3MinCheck, last10MinCheck, last60MinCheck, lastNMinCheck;
107 afs_int32 last1MinCheck;
108 afs_uint32 lastCBSlotBump;
111 AFS_STATCNT(afs_Daemon);
112 last1MinCheck = last3MinCheck = last60MinCheck = last10MinCheck = lastNMinCheck = 0;
114 afs_rootFid.Fid.Volume = 0;
115 while (afs_initState < 101) afs_osi_Sleep(&afs_initState);
118 lastCBSlotBump = now;
120 /* when a lot of clients are booted simultaneously, they develop
121 * annoying synchronous VL server bashing behaviors. So we stagger them.
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);
129 /* start off with afs_initState >= 101 (basic init done) */
131 afs_CheckCallbacks(20); /* unstat anything which will expire soon */
133 /* things to do every 20 seconds or less - required by protocol spec */
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)
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...
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);
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 */
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 */
171 if (last3MinCheck + 180 < now) {
172 afs_CheckTokenCache(); /* check for access cache resets due to expired
176 if (!afs_CheckServerDaemonStarted) {
177 /* Do the check here if the correct afsd is not installed. */
180 printf("Please install afsd with check server daemon.\n");
182 if (lastNMinCheck + PROBE_INTERVAL < now) {
183 /* only check down servers */
184 afs_CheckServers(1, (struct cell *) 0);
188 if (last10MinCheck + 600 < now) {
189 #ifdef AFS_USERSPACE_IP_ADDR
190 extern int rxi_GetcbiInfo(void);
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 */
198 #else /* AFS_USERSPACE_IP_ADDR */
199 if (rxi_GetIFInfo()) { /* addresses changed from last time */
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 */
213 afs_CheckVolumeNames(AFS_VOLCHECK_EXPIRED |
217 afs_CheckVolumeNames(AFS_VOLCHECK_EXPIRED |
222 last10MinCheck = now;
224 if (last60MinCheck + 3600 < now) {
225 afs_Trace1(afs_iclSetp, CM_TRACE_PROBEVOLUME,
226 ICL_TYPE_INT32, 3600);
227 afs_CheckRootVolume();
229 if (afs_gcpags == AFS_GCPAGS_OK) {
234 last60MinCheck = now;
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);
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);
250 afs_osi_Wait(now, &AFS_WaitHandler, 0);
253 if (afs_termState == AFSOP_STOP_AFS) {
254 if (afs_CheckServerDaemonStarted)
255 afs_termState = AFSOP_STOP_CS;
257 afs_termState = AFSOP_STOP_BKG;
258 afs_osi_Wakeup(&afs_termState);
264 afs_CheckRootVolume () {
265 char rootVolName[32];
266 register struct volume *tvp;
268 AFS_STATCNT(afs_CheckRootVolume);
269 if (*afs_rootVolumeName == 0) {
270 strcpy(rootVolName, "root.afs");
273 strcpy(rootVolName, afs_rootVolumeName);
275 tvp = afs_GetVolumeByName(rootVolName, LOCALCELL, 1, (struct vrequest *) 0, READ_LOCK);
278 int len = strlen(rootVolName);
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);
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
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
298 AFS_FAST_RELE(afs_globalVp);
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);
309 /* This is to make sure that we update the root gnode */
310 /* every time root volume gets released */
312 extern struct vfs *afs_globalVFS;
313 extern int afs_root();
314 struct gnode *rootgp;
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
323 if (afs_globalVFS && afs_initState >= 200) {
324 if (code = afs_root(afs_globalVFS, &rootgp))
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 */
333 if (afs_rootFid.Fid.Volume) return 0;
337 /* parm 0 is the pathname, parm 1 to the fetch is the chunk number */
339 register struct brequest *ab; {
340 register struct dcache *tdc;
343 #ifdef AFS_LINUX22_ENV
344 struct dentry *dp = NULL;
346 afs_int32 offset, len;
347 struct vrequest treq;
351 if (code = afs_InitReq(&treq, ab->cred)) return;
353 #ifdef AFS_LINUX22_ENV
354 code = gop_lookupname((char *)ab->parm[0], AFS_UIOSYS, 1, (struct vnode **) 0, &dp);
356 tvn = (struct vcache*)dp->d_inode;
358 code = gop_lookupname((char *)ab->parm[0], AFS_UIOSYS, 1, (struct vnode **) 0, (struct vnode **)&tvn);
361 osi_FreeSmallSpace((char *)ab->parm[0]); /* free path name buffer here */
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 */
370 #ifdef AFS_LINUX22_ENV
373 AFS_RELE((struct vnode *) tvn);
380 tvc = (struct vcache *) afs_gntovn(tvn);
382 tvc = (struct vcache *) tvn;
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);
392 #ifdef AFS_LINUX22_ENV
395 AFS_RELE((struct vnode *) tvn);
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.
404 register struct brequest *ab; {
405 register struct dcache *tdc;
406 register struct vcache *tvc;
407 afs_int32 offset, len;
408 struct vrequest treq;
410 AFS_STATCNT(BPrefetch);
411 if (len = afs_InitReq(&treq, ab->cred)) return;
413 tdc = afs_GetDCache(tvc, (afs_int32)ab->parm[0], &treq, &offset, &len, 1);
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.
421 tdc = (struct dcache *) (ab->parm[1]);
422 tdc->flags &= ~DFFetchReq;
423 afs_osi_Wakeup(&tdc->validPos);
426 mutex_enter(&tdc->lock);
428 mutex_exit(&tdc->lock);
430 afs_PutDCache(tdc); /* put this one back, too */
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;
446 if (code = afs_InitReq(&treq, ab->cred)) return;
449 #if defined(AFS_SGI_ENV)
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
455 tmpcred = OSI_GET_CURRENT_CRED();
456 OSI_SET_CURRENT_CRED(ab->cred);
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
463 AFS_RWLOCK((vnode_t *)tvc, 1);
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);
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;
483 /* release a held request buffer */
484 void afs_BRelease(ab)
485 register struct brequest *ab; {
487 AFS_STATCNT(afs_BRelease);
488 MObtainWriteLock(&afs_xbrs,294);
489 if (--ab->refCount <= 0) {
492 if (afs_brsWaiters) afs_osi_Wakeup(&afs_brsWaiters);
493 MReleaseWriteLock(&afs_xbrs);
496 /* return true if bkg fetch daemons are all busy */
498 AFS_STATCNT(afs_BBusy);
499 if (afs_brsDaemons > 0) return 0;
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;
512 register struct brequest *tb;
514 AFS_STATCNT(afs_BQueue);
515 MObtainWriteLock(&afs_xbrs,296);
518 for(i=0;i<NBRS;i++,tb++) {
519 if (tb->refCount == 0) break;
523 tb->opcode = aopcode;
531 VN_HOLD((struct vnode *)avc);
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;
541 /* if daemons are waiting for work, wake them up */
542 if (afs_brsDaemons > 0) {
543 afs_osi_Wakeup(&afs_brsDaemons);
545 MReleaseWriteLock(&afs_xbrs);
549 MReleaseWriteLock(&afs_xbrs);
550 return (struct brequest *)0;
552 /* no free buffers, sleep a while */
554 MReleaseWriteLock(&afs_xbrs);
555 afs_osi_Sleep(&afs_brsWaiters);
556 MObtainWriteLock(&afs_xbrs,301);
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.
566 struct buf *afs_asyncbuf = (struct buf*)0;
567 afs_int32 afs_asyncbuf_cv = EVENT_NULL;
568 afs_int32 afs_biodcnt = 0;
570 /* in implementing this, I assumed that all external linked lists were
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
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.
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.
596 Simple_lock afs_asyncbuf_lock;
597 /*static*/ struct buf *afs_get_bioreq()
599 struct buf *bp = (struct buf *) 0;
601 struct buf **bestlbpP, **lbpP;
603 struct buf *t1P, *t2P; /* temp pointers for list manipulation */
606 struct afs_bioqueue *s;
608 /* ??? Does the forward pointer of the returned buffer need to be NULL?
611 /* Disable interrupts from the strategy function, and save the
612 * prior priority level and lock access to the afs_asyncbuf.
615 oldPriority = disable_lock(INTMAX, &afs_asyncbuf_lock) ;
619 /* look for oldest buffer */
620 bp = bestbp = afs_asyncbuf;
621 bestage = (int) bestbp->av_back;
622 bestlbpP = &afs_asyncbuf;
627 if ((int) bp->av_back - bestage < 0) {
630 bestage = (int) bp->av_back;
634 *bestlbpP = bp->av_forw;
638 /* If afs_asyncbuf is null, it is necessary to go to sleep.
639 * e_wakeup_one() ensures that only one thread wakes.
642 /* The LOCK_HANDLER indicates to e_sleep_thread to only drop the
643 * lock on an MP machine.
645 interrupted = e_sleep_thread(&afs_asyncbuf_cv,
647 LOCK_HANDLER|INTERRUPTIBLE);
648 if (interrupted==THREAD_INTERRUPTED) {
649 /* re-enable interrupts from strategy */
650 unlock_enable(oldPriority, &afs_asyncbuf_lock);
654 } /* end of "else asyncbuf is empty" */
655 } /* end of "inner loop" */
659 unlock_enable(oldPriority, &afs_asyncbuf_lock);
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.
666 * ??? what happens to the gnodes? They're not just cut loose,
670 t2P = (struct buf *) t1P->b_work;
671 t1P->b_vp = ((struct gnode *) t1P->b_vp)->gn_vnode;
675 t1P = (struct buf *) t2P->b_work;
676 t2P->b_vp = ((struct gnode *) t2P->b_vp)->gn_vnode;
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.
688 if (bp->b_flags & B_PFPROT) {
693 } /* end of function get_bioreq() */
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.
705 static int afs_initbiod = 0; /* this is self-initializing code */
707 afs_BioDaemon (nbiods)
710 afs_int32 code, s, pflg = 0;
712 struct buf *bp, *bp1, *tbp1, *tbp2; /* temp pointers only */
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));
727 /* Ignore HUP signals... */
730 sigset_t sigbits, osigbits;
732 * add SIGHUP to the set of already masked signals
734 SIGFILLSET(sigbits); /* allow all signals */
735 SIGDELSET(sigbits, SIGHUP); /* except SIGHUP */
736 limit_sigs(&sigbits, &osigbits); /* and already masked */
739 SIGDELSET(u.u_procp->p_sig, SIGHUP);
740 SIGADDSET(u.u_procp->p_sigignore, SIGHUP);
741 SIGDELSET(u.u_procp->p_sigcatch, SIGHUP);
743 /* Main body starts here -- this is an intentional infinite loop, and
746 * Now, the loop will exit if get_bioreq() returns NULL, indicating
747 * that we've been interrupted.
750 bp = afs_get_bioreq();
752 break; /* we were interrupted */
753 if (code = setjmpx(&jmpbuf)) {
754 /* This should not have happend, maybe a lack of resources */
756 s = disable_lock(INTMAX, &afs_asyncbuf_lock);
757 for (bp1 = bp; bp ; bp = bp1) {
759 bp1 = (struct buf *) bp1->b_work;
762 bp->b_flags |= B_ERROR;
765 unlock_enable(s, &afs_asyncbuf_lock);
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);
776 ReleaseWriteLock(&vcp->lock);
778 /* If the buffer represents a protection violation, rather than
779 * an actual request for I/O, no special action need be taken.
781 if ( bp->b_flags & B_PFPROT ) {
782 iodone (bp); /* Notify all users of the buffer that we're done */
787 ObtainWriteLock(&vcp->pvmlock,211);
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.
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;
802 /* Unmap the buffer's data area by calling vm_det. Reset data area
803 * to the value that we saved above.
805 vm_det(bp->b_un.b_addr);
806 bp->b_baddr = tmpaddr;
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.
816 tbp2 = (struct buf *) tbp1->b_work;
821 tbp1 = (struct buf *) tbp2->b_work;
827 ReleaseWriteLock(&vcp->pvmlock); /* Unlock the vnode. */
829 } /* infinite loop (unless we're interrupted) */
830 } /* end of afs_BioDaemon() */
832 #else /* AFS_AIX41_ENV */
836 struct afs_bioqueue {
841 struct afs_bioqueue afs_bioqueue;
842 struct buf *afs_busyq = NULL;
843 struct buf *afs_asyncbuf;
844 afs_int32 afs_biodcnt = 0;
846 /* in implementing this, I assumed that all external linked lists were
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
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.
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.
880 /*static*/ struct buf *afs_get_bioreq(self)
881 struct afs_bioqueue *self; /* address on which to sleep */
884 struct buf *bp = (struct buf *) 0;
886 struct buf **bestlbpP, **lbpP;
888 struct buf *t1P, *t2P; /* temp pointers for list manipulation */
891 struct afs_bioqueue *s;
893 /* ??? Does the forward pointer of the returned buffer need to be NULL?
896 /* Disable interrupts from the strategy function, and save the
897 * prior priority level
899 oldPriority = i_disable ( INTMAX ) ;
901 /* Each iteration of following loop either pulls
902 * a buffer off afs_asyncbuf, or sleeps.
904 while (1) { /* inner loop */
906 /* look for oldest buffer */
907 bp = bestbp = afs_asyncbuf;
908 bestage = (int) bestbp->av_back;
909 bestlbpP = &afs_asyncbuf;
914 if ((int) bp->av_back - bestage < 0) {
917 bestage = (int) bp->av_back;
921 *bestlbpP = bp->av_forw;
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,
932 if (afs_bioqueue.sleeper) {
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 */
940 afs_bioqueue.sleeper = FALSE;
942 /* re-enable interrupts from strategy */
943 i_enable (oldPriority);
948 afs_bioqueue.sleeper = TRUE;
949 interrupted = sleep ((caddr_t) &afs_asyncbuf, PCATCH|(PZERO + 1));
950 afs_bioqueue.sleeper = FALSE;
954 * We need to wakeup another daemon if present
955 * since we were waiting on afs_asyncbuf.
957 #ifdef notdef /* The following doesn't work as advertised */
958 if (afs_bioqueue.lruq.next != &afs_bioqueue.lruq)
960 struct squeue *bq = afs_bioqueue.lruq.next;
965 /* re-enable interrupts from strategy */
966 i_enable (oldPriority);
972 } /* end of "else asyncbuf is empty" */
973 } /* end of "inner loop" */
977 i_enable (oldPriority); /* re-enable interrupts from strategy */
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.
983 * ??? what happens to the gnodes? They're not just cut loose,
987 t2P = (struct buf *) t1P->b_work;
988 t1P->b_vp = ((struct gnode *) t1P->b_vp)->gn_vnode;
992 t1P = (struct buf *) t2P->b_work;
993 t2P->b_vp = ((struct gnode *) t2P->b_vp)->gn_vnode;
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.
1005 if (bp->b_flags & B_PFPROT) {
1009 /* wake up another process to handle the next buffer, and return
1012 oldPriority = i_disable ( INTMAX ) ;
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.
1020 if (afs_bioqueue.sleeper) {
1021 wakeup (&afs_asyncbuf);
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? */;
1030 struct squeue *bq = afs_bioqueue.lruq.next;
1034 afs_bioqueue.sleeper = TRUE;
1037 i_enable (oldPriority); /* re-enable interrupts from strategy */
1040 } /* end of function get_bioreq() */
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.
1052 static int afs_initbiod = 0; /* this is self-initializing code */
1054 afs_BioDaemon (nbiods)
1057 struct afs_bioqueue *self;
1058 afs_int32 code, s, pflg = 0;
1060 struct buf *bp, *bp1, *tbp1, *tbp2; /* temp pointers only */
1065 if (!afs_initbiod) {
1068 /* Initialize the queue of waiting processes, afs_bioqueue. */
1069 QInit (&(afs_bioqueue.lruq));
1072 /* establish ourself as a kernel process so shutdown won't kill us */
1073 /* u.u_procp->p_flag |= SKPROC;*/
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 */
1082 /* Ignore HUP signals... */
1083 #ifdef AFS_AIX41_ENV
1085 sigset_t sigbits, osigbits;
1087 * add SIGHUP to the set of already masked signals
1089 SIGFILLSET(sigbits); /* allow all signals */
1090 SIGDELSET(sigbits, SIGHUP); /* except SIGHUP */
1091 limit_sigs(&sigbits, &osigbits); /* and already masked */
1094 SIGDELSET(u.u_procp->p_sig, SIGHUP);
1095 SIGADDSET(u.u_procp->p_sigignore, SIGHUP);
1096 SIGDELSET(u.u_procp->p_sigcatch, SIGHUP);
1098 /* Main body starts here -- this is an intentional infinite loop, and
1101 * Now, the loop will exit if get_bioreq() returns NULL, indicating
1102 * that we've been interrupted.
1105 bp = afs_get_bioreq(self);
1107 break; /* we were interrupted */
1108 if (code = setjmpx(&jmpbuf)) {
1109 /* This should not have happend, maybe a lack of resources */
1111 for (bp1 = bp; bp ; bp = bp1) {
1116 bp->b_flags |= B_ERROR;
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);
1129 ReleaseWriteLock(&vcp->lock);
1131 /* If the buffer represents a protection violation, rather than
1132 * an actual request for I/O, no special action need be taken.
1134 if ( bp->b_flags & B_PFPROT ) {
1135 iodone (bp); /* Notify all users of the buffer that we're done */
1139 ObtainWriteLock(&vcp->pvmlock,558);
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.
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;
1154 /* Unmap the buffer's data area by calling vm_det. Reset data area
1155 * to the value that we saved above.
1157 vm_det(bp->b_un.b_addr);
1158 bp->b_baddr = tmpaddr;
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.
1168 tbp2 = (struct buf *) tbp1->b_work;
1173 tbp1 = (struct buf *) tbp2->b_work;
1179 ReleaseWriteLock(&vcp->pvmlock); /* Unlock the vnode. */
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 */
1190 void afs_BackgroundDaemon() {
1191 struct brequest *tb;
1195 AFS_STATCNT(afs_BackgroundDaemon);
1196 /* initialize subsystem */
1198 LOCK_INIT(&afs_xbrs, "afs_xbrs");
1199 bzero((char *)afs_brs, sizeof(afs_brs));
1201 #if defined (AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1203 * steal the first daemon for doing delayed DSlot flushing
1204 * (see afs_GetDownDSlot)
1213 MObtainWriteLock(&afs_xbrs,302);
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);
1223 /* find a request */
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);
1233 afs_Trace1(afs_iclSetp, CM_TRACE_BKG1,
1234 ICL_TYPE_INT32, tb->opcode);
1235 if (tb->opcode == BOP_FETCH)
1237 else if (tb->opcode == BOP_STORE)
1239 else if (tb->opcode == BOP_PATH)
1241 else panic("background bop");
1244 tb->vnode->vrefCount--; /* fix up reference count */
1246 AFS_RELE((struct vnode *)(tb->vnode)); /* MUST call vnode layer or could lose vnodes */
1248 tb->vnode = (struct vcache *) 0;
1252 tb->cred = (struct AFS_UCRED *) 0;
1254 afs_BRelease(tb); /* this grabs and releases afs_xbrs lock */
1255 MObtainWriteLock(&afs_xbrs,305);
1259 /* wait for new request */
1261 MReleaseWriteLock(&afs_xbrs);
1262 afs_osi_Sleep(&afs_brsDaemons);
1263 MObtainWriteLock(&afs_xbrs,307);
1270 void shutdown_daemons()
1272 extern int afs_cold_shutdown;
1274 register struct brequest *tb;
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));
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 */
1291 bzero((char *)&afs_bioqueue, sizeof(struct afs_bioqueue));
1298 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1300 * sgi - daemon - handles certain operations that otherwise
1301 * would use up too much kernel stack space
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.
1307 SV_TYPE afs_sgibksync;
1308 SV_TYPE afs_sgibkwait;
1309 lock_t afs_sgibklock;
1310 struct dcache *afs_sgibklist;
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");
1323 s = SPLOCK(afs_sgibklock);
1325 /* wait for something to do */
1326 SP_WAIT(afs_sgibklock, s, &afs_sgibksync, PINOD);
1327 osi_Assert(afs_sgibklist);
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);
1336 tdc->flags &= ~DFEntryMod;
1337 afs_WriteDCache(tdc, 1);
1339 s = SPLOCK(afs_sgibklock);
1342 /* done all the work - wake everyone up */
1343 while (SV_SIGNAL(&afs_sgibkwait))