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 <afsconfig.h>
11 #include "afs/param.h"
18 #include <sys/sleep.h>
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"
26 #include <sys/adspace.h> /* for vm_att(), vm_det() */
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 */
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); } }
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;
48 afs_int32 afs_CheckServerDaemonStarted = 0;
49 #ifdef DEFAULT_PROBE_INTERVAL
50 afs_int32 PROBE_INTERVAL = DEFAULT_PROBE_INTERVAL; /* overridding during compile */
52 afs_int32 PROBE_INTERVAL = 180; /* default to 3 min */
55 #define PROBE_WAIT() (1000 * (PROBE_INTERVAL - ((afs_random() & 0x7fffffff) \
56 % (PROBE_INTERVAL/2))))
59 afs_CheckServerDaemon(void)
61 afs_int32 now, delay, lastCheck, last10MinCheck;
63 afs_CheckServerDaemonStarted = 1;
65 while (afs_initState < 101)
66 afs_osi_Sleep(&afs_initState);
67 afs_osi_Wait(PROBE_WAIT(), &AFS_CSWaitHandler, 0);
69 last10MinCheck = lastCheck = osi_Time();
71 if (afs_termState == AFSOP_STOP_CS) {
72 afs_termState = AFSOP_STOP_BKG;
73 afs_osi_Wakeup(&afs_termState);
78 if (PROBE_INTERVAL + lastCheck <= now) {
79 afs_CheckServers(1, NULL); /* check down servers */
80 lastCheck = now = osi_Time();
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();
89 if (afs_termState == AFSOP_STOP_CS) {
90 afs_termState = AFSOP_STOP_BKG;
91 afs_osi_Wakeup(&afs_termState);
95 /* Compute time to next probe. */
96 delay = PROBE_INTERVAL + lastCheck;
97 if (delay > 600 + last10MinCheck)
98 delay = 600 + last10MinCheck;
102 afs_osi_Wait(delay * 1000, &AFS_CSWaitHandler, 0);
104 afs_CheckServerDaemonStarted = 0;
111 struct afs_exporter *exporter;
113 afs_int32 last3MinCheck, last10MinCheck, last60MinCheck, lastNMinCheck;
114 afs_int32 last1MinCheck;
115 afs_uint32 lastCBSlotBump;
118 AFS_STATCNT(afs_Daemon);
119 last1MinCheck = last3MinCheck = last60MinCheck = last10MinCheck =
122 afs_rootFid.Fid.Volume = 0;
123 while (afs_initState < 101)
124 afs_osi_Sleep(&afs_initState);
127 lastCBSlotBump = now;
129 /* when a lot of clients are booted simultaneously, they develop
130 * annoying synchronous VL server bashing behaviors. So we stagger them.
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);
138 /* start off with afs_initState >= 101 (basic init done) */
140 afs_CheckCallbacks(20); /* unstat anything which will expire soon */
142 /* things to do every 20 seconds or less - required by protocol spec */
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)
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...
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);
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 */
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();
185 if (last3MinCheck + 180 < now) {
186 afs_CheckTokenCache(); /* check for access cache resets due to expired
190 if (!afs_CheckServerDaemonStarted) {
191 /* Do the check here if the correct afsd is not installed. */
194 printf("Please install afsd with check server daemon.\n");
196 if (lastNMinCheck + PROBE_INTERVAL < now) {
197 /* only check down servers */
198 afs_CheckServers(1, NULL);
202 if (last10MinCheck + 600 < now) {
203 #ifdef AFS_USERSPACE_IP_ADDR
204 extern int rxi_GetcbiInfo(void);
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 */
211 #else /* AFS_USERSPACE_IP_ADDR */
212 if (rxi_GetIFInfo()) { /* addresses changed from last time */
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 */
227 afs_CheckVolumeNames(AFS_VOLCHECK_EXPIRED |
231 afs_CheckVolumeNames(AFS_VOLCHECK_EXPIRED |
236 last10MinCheck = now;
238 if (last60MinCheck + 3600 < now) {
239 afs_Trace1(afs_iclSetp, CM_TRACE_PROBEVOLUME, ICL_TYPE_INT32,
241 afs_CheckRootVolume();
243 if (afs_gcpags == AFS_GCPAGS_OK) {
248 last60MinCheck = now;
250 if (afs_initState < 300) { /* while things ain't rosy */
251 code = afs_CheckRootVolume();
253 afs_initState = 300; /* succeeded */
254 if (afs_initState < 200)
255 afs_initState = 200; /* tried once */
256 afs_osi_Wakeup(&afs_initState);
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);
266 afs_osi_Wait(now, &AFS_WaitHandler, 0);
269 if (afs_termState == AFSOP_STOP_AFS) {
270 if (afs_CheckServerDaemonStarted)
271 afs_termState = AFSOP_STOP_CS;
273 afs_termState = AFSOP_STOP_BKG;
274 afs_osi_Wakeup(&afs_termState);
281 afs_CheckRootVolume(void)
283 char rootVolName[32];
284 struct volume *tvp = NULL;
285 int usingDynroot = afs_GetDynrootEnable();
288 AFS_STATCNT(afs_CheckRootVolume);
289 if (*afs_rootVolumeName == 0) {
290 strcpy(rootVolName, "root.afs");
292 strcpy(rootVolName, afs_rootVolumeName);
296 afs_GetDynrootFid(&afs_rootFid);
297 tvp = afs_GetVolume(&afs_rootFid, NULL, READ_LOCK);
299 struct cell *lc = afs_GetPrimaryCell(READ_LOCK);
303 localcell = lc->cellNum;
304 afs_PutCell(lc, READ_LOCK);
305 tvp = afs_GetVolumeByName(rootVolName, localcell, 1, NULL, READ_LOCK);
308 int len = strlen(rootVolName);
310 if ((len < 9) || strcmp(&rootVolName[len - 9], ".readonly")) {
311 strcpy(buf, rootVolName);
312 afs_strcat(buf, ".readonly");
313 tvp = afs_GetVolumeByName(buf, localcell, 1, NULL, READ_LOCK);
317 int volid = (tvp->roVol ? tvp->roVol : tvp->volume);
318 afs_rootFid.Cell = localcell;
319 if (afs_rootFid.Fid.Volume && afs_rootFid.Fid.Volume != volid
321 /* If we had a root fid before and it changed location we reset
322 * the afs_globalVp so that it will be reevaluated.
323 * Just decrement the reference count. This only occurs during
324 * initial cell setup and can panic the machine if we set the
325 * count to zero and fs checkv is executed when the current
328 AFS_FAST_RELE(afs_globalVp);
331 afs_rootFid.Fid.Volume = volid;
332 afs_rootFid.Fid.Vnode = 1;
333 afs_rootFid.Fid.Unique = 1;
337 afs_initState = 300; /* won */
338 afs_osi_Wakeup(&afs_initState);
339 afs_PutVolume(tvp, READ_LOCK);
342 /* This is to make sure that we update the root gnode */
343 /* every time root volume gets released */
345 struct gnode *rootgp;
349 /* Only do this if afs_globalVFS is properly set due to race conditions
350 * this routine could be called before the gfs_mount is performed!
351 * Furthermore, afs_root (called below) *waits* until
352 * initState >= 200, so we don't try this until we've gotten
353 * at least that far */
354 if (afs_globalVFS && afs_initState >= 200) {
355 if (code = afs_root(afs_globalVFS, &rootgp))
357 mp = (struct mount *)afs_globalVFS->vfs_data;
358 mp->m_rootgp = gget(mp, 0, 0, (char *)rootgp);
359 afs_unlock(mp->m_rootgp); /* unlock basic gnode */
360 afs_vrele(VTOAFS(rootgp)); /* zap afs_root's vnode hold */
364 if (afs_rootFid.Fid.Volume)
370 /* ptr_parm 0 is the pathname, size_parm 0 to the fetch is the chunk number */
372 BPath(register struct brequest *ab)
374 register struct dcache *tdc = NULL;
375 struct vcache *tvc = NULL;
376 struct vnode *tvn = NULL;
377 #ifdef AFS_LINUX22_ENV
378 struct dentry *dp = NULL;
380 afs_size_t offset, len;
381 struct vrequest treq;
385 if ((code = afs_InitReq(&treq, ab->cred)))
388 #ifdef AFS_LINUX22_ENV
389 code = gop_lookupname((char *)ab->ptr_parm[0], AFS_UIOSYS, 1, NULL, &dp);
391 tvn = (struct vnode *)dp->d_inode;
393 code = gop_lookupname((char *)ab->ptr_parm[0], AFS_UIOSYS, 1, NULL, &tvn);
396 osi_FreeLargeSpace((char *)ab->ptr_parm[0]); /* free path name buffer here */
399 /* now path may not have been in afs, so check that before calling our cache manager */
400 if (!tvn || !IsAfsVnode(tvn)) {
401 /* release it and give up */
406 #ifdef AFS_LINUX22_ENV
416 tvc = VTOAFS(afs_gntovn(tvn));
420 /* here we know its an afs vnode, so we can get the data for the chunk */
421 tdc = afs_GetDCache(tvc, ab->size_parm[0], &treq, &offset, &len, 1);
428 #ifdef AFS_LINUX22_ENV
436 /* size_parm 0 to the fetch is the chunk number,
437 * ptr_parm 0 is the dcache entry to wakeup,
438 * size_parm 1 is true iff we should release the dcache entry here.
441 BPrefetch(register struct brequest *ab)
443 register struct dcache *tdc;
444 register struct vcache *tvc;
445 afs_size_t offset, len;
446 struct vrequest treq;
448 AFS_STATCNT(BPrefetch);
449 if ((len = afs_InitReq(&treq, ab->cred)))
452 tdc = afs_GetDCache(tvc, ab->size_parm[0], &treq, &offset, &len, 1);
456 /* now, dude may be waiting for us to clear DFFetchReq bit; do so. Can't
457 * use tdc from GetDCache since afs_GetDCache may fail, but someone may
458 * be waiting for our wakeup anyway.
460 tdc = (struct dcache *)(ab->ptr_parm[0]);
461 ObtainSharedLock(&tdc->lock, 640);
462 if (tdc->mflags & DFFetchReq) {
463 UpgradeSToWLock(&tdc->lock, 641);
464 tdc->mflags &= ~DFFetchReq;
465 ReleaseWriteLock(&tdc->lock);
467 ReleaseSharedLock(&tdc->lock);
469 afs_osi_Wakeup(&tdc->validPos);
470 if (ab->size_parm[1]) {
471 afs_PutDCache(tdc); /* put this one back, too */
477 BStore(register struct brequest *ab)
479 register struct vcache *tvc;
480 register afs_int32 code;
481 struct vrequest treq;
482 #if defined(AFS_SGI_ENV)
483 struct cred *tmpcred;
487 if ((code = afs_InitReq(&treq, ab->cred)))
491 #if defined(AFS_SGI_ENV)
493 * Since StoreOnLastReference can end up calling osi_SyncVM which
494 * calls into VM code that assumes that u.u_cred has the
495 * correct credentials, we set our to theirs for this xaction
497 tmpcred = OSI_GET_CURRENT_CRED();
498 OSI_SET_CURRENT_CRED(ab->cred);
501 * To avoid recursion since the WriteLock may be released during VM
502 * operations, we hold the VOP_RWLOCK across this transaction as
503 * do the other callers of StoreOnLastReference
505 AFS_RWLOCK((vnode_t *) tvc, 1);
507 ObtainWriteLock(&tvc->lock, 209);
508 code = afs_StoreOnLastReference(tvc, &treq);
509 ReleaseWriteLock(&tvc->lock);
510 #if defined(AFS_SGI_ENV)
511 OSI_SET_CURRENT_CRED(tmpcred);
512 AFS_RWUNLOCK((vnode_t *) tvc, 1);
514 /* now set final return code, and wakeup anyone waiting */
515 if ((ab->flags & BUVALID) == 0) {
516 ab->code = afs_CheckCode(code, &treq, 43); /* set final code, since treq doesn't go across processes */
517 ab->flags |= BUVALID;
518 if (ab->flags & BUWAIT) {
519 ab->flags &= ~BUWAIT;
525 /* release a held request buffer */
527 afs_BRelease(register struct brequest *ab)
530 AFS_STATCNT(afs_BRelease);
531 MObtainWriteLock(&afs_xbrs, 294);
532 if (--ab->refCount <= 0) {
536 afs_osi_Wakeup(&afs_brsWaiters);
537 MReleaseWriteLock(&afs_xbrs);
540 /* return true if bkg fetch daemons are all busy */
544 AFS_STATCNT(afs_BBusy);
545 if (afs_brsDaemons > 0)
551 afs_BQueue(register short aopcode, register struct vcache *avc,
552 afs_int32 dontwait, afs_int32 ause, struct AFS_UCRED *acred,
553 afs_size_t asparm0, afs_size_t asparm1, void *apparm0)
556 register struct brequest *tb;
558 AFS_STATCNT(afs_BQueue);
559 MObtainWriteLock(&afs_xbrs, 296);
562 for (i = 0; i < NBRS; i++, tb++) {
563 if (tb->refCount == 0)
568 tb->opcode = aopcode;
576 VN_HOLD(AFSTOV(avc));
579 tb->refCount = ause + 1;
580 tb->size_parm[0] = asparm0;
581 tb->size_parm[1] = asparm1;
582 tb->ptr_parm[0] = apparm0;
585 tb->ts = afs_brs_count++;
586 /* if daemons are waiting for work, wake them up */
587 if (afs_brsDaemons > 0) {
588 afs_osi_Wakeup(&afs_brsDaemons);
590 MReleaseWriteLock(&afs_xbrs);
594 MReleaseWriteLock(&afs_xbrs);
597 /* no free buffers, sleep a while */
599 MReleaseWriteLock(&afs_xbrs);
600 afs_osi_Sleep(&afs_brsWaiters);
601 MObtainWriteLock(&afs_xbrs, 301);
608 /* AIX 4.1 has a much different sleep/wakeup mechanism available for use.
609 * The modifications here will work for either a UP or MP machine.
611 struct buf *afs_asyncbuf = (struct buf *)0;
612 tid_t afs_asyncbuf_cv = EVENT_NULL;
613 afs_int32 afs_biodcnt = 0;
615 /* in implementing this, I assumed that all external linked lists were
618 * Several places in this code traverse a linked list. The algorithm
619 * used here is probably unfamiliar to most people. Careful examination
620 * will show that it eliminates an assignment inside the loop, as compared
621 * to the standard algorithm, at the cost of occasionally using an extra
627 * This function obtains, and returns, a pointer to a buffer for
628 * processing by a daemon. It sleeps until such a buffer is available.
629 * The source of buffers for it is the list afs_asyncbuf (see also
630 * naix_vm_strategy). This function may be invoked concurrently by
631 * several processes, that is, several instances of the same daemon.
632 * naix_vm_strategy, which adds buffers to the list, runs at interrupt
633 * level, while get_bioreq runs at process level.
635 * Since AIX 4.1 can wake just one process at a time, the separate sleep
636 * addresses have been removed.
637 * Note that the kernel_lock is held until the e_sleep_thread() occurs.
638 * The afs_asyncbuf_lock is primarily used to serialize access between
639 * process and interrupts.
641 Simple_lock afs_asyncbuf_lock;
642 /*static*/ struct buf *
645 struct buf *bp = NULL;
647 struct buf **bestlbpP, **lbpP;
649 struct buf *t1P, *t2P; /* temp pointers for list manipulation */
652 struct afs_bioqueue *s;
654 /* ??? Does the forward pointer of the returned buffer need to be NULL?
657 /* Disable interrupts from the strategy function, and save the
658 * prior priority level and lock access to the afs_asyncbuf.
661 oldPriority = disable_lock(INTMAX, &afs_asyncbuf_lock);
665 /* look for oldest buffer */
666 bp = bestbp = afs_asyncbuf;
667 bestage = (long)bestbp->av_back;
668 bestlbpP = &afs_asyncbuf;
674 if ((long)bp->av_back - bestage < 0) {
677 bestage = (long)bp->av_back;
681 *bestlbpP = bp->av_forw;
684 /* If afs_asyncbuf is null, it is necessary to go to sleep.
685 * e_wakeup_one() ensures that only one thread wakes.
688 /* The LOCK_HANDLER indicates to e_sleep_thread to only drop the
689 * lock on an MP machine.
692 e_sleep_thread(&afs_asyncbuf_cv, &afs_asyncbuf_lock,
693 LOCK_HANDLER | INTERRUPTIBLE);
694 if (interrupted == THREAD_INTERRUPTED) {
695 /* re-enable interrupts from strategy */
696 unlock_enable(oldPriority, &afs_asyncbuf_lock);
700 } /* end of "else asyncbuf is empty" */
701 } /* end of "inner loop" */
705 unlock_enable(oldPriority, &afs_asyncbuf_lock);
708 /* For the convenience of other code, replace the gnodes in
709 * the b_vp field of bp and the other buffers on the b_work
710 * chain with the corresponding vnodes.
712 * ??? what happens to the gnodes? They're not just cut loose,
716 t2P = (struct buf *)t1P->b_work;
717 t1P->b_vp = ((struct gnode *)t1P->b_vp)->gn_vnode;
721 t1P = (struct buf *)t2P->b_work;
722 t2P->b_vp = ((struct gnode *)t2P->b_vp)->gn_vnode;
727 /* If the buffer does not specify I/O, it may immediately
728 * be returned to the caller. This condition is detected
729 * by examining the buffer's flags (the b_flags field). If
730 * the B_PFPROT bit is set, the buffer represents a protection
731 * violation, rather than a request for I/O. The remainder
732 * of the outer loop handles the case where the B_PFPROT bit is clear.
734 if (bp->b_flags & B_PFPROT) {
739 } /* end of function get_bioreq() */
744 * This function is the daemon. It is called from the syscall
745 * interface. Ordinarily, a script or an administrator will run a
746 * daemon startup utility, specifying the number of I/O daemons to
747 * run. The utility will fork off that number of processes,
748 * each making the appropriate syscall, which will cause this
749 * function to be invoked.
751 static int afs_initbiod = 0; /* this is self-initializing code */
754 afs_BioDaemon(afs_int32 nbiods)
756 afs_int32 code, s, pflg = 0;
758 struct buf *bp, *bp1, *tbp1, *tbp2; /* temp pointers only */
766 /* pin lock, since we'll be using it in an interrupt. */
767 lock_alloc(&afs_asyncbuf_lock, LOCK_ALLOC_PIN, 2, 1);
768 simple_lock_init(&afs_asyncbuf_lock);
769 pin(&afs_asyncbuf, sizeof(struct buf *));
770 pin(&afs_asyncbuf_cv, sizeof(afs_int32));
773 /* Ignore HUP signals... */
775 sigset_t sigbits, osigbits;
777 * add SIGHUP to the set of already masked signals
779 SIGFILLSET(sigbits); /* allow all signals */
780 SIGDELSET(sigbits, SIGHUP); /* except SIGHUP */
781 limit_sigs(&sigbits, &osigbits); /* and already masked */
783 /* Main body starts here -- this is an intentional infinite loop, and
786 * Now, the loop will exit if get_bioreq() returns NULL, indicating
787 * that we've been interrupted.
790 bp = afs_get_bioreq();
792 break; /* we were interrupted */
793 if (code = setjmpx(&jmpbuf)) {
794 /* This should not have happend, maybe a lack of resources */
796 s = disable_lock(INTMAX, &afs_asyncbuf_lock);
797 for (bp1 = bp; bp; bp = bp1) {
799 bp1 = (struct buf *)bp1->b_work;
802 bp->b_flags |= B_ERROR;
805 unlock_enable(s, &afs_asyncbuf_lock);
809 vcp = VTOAFS(bp->b_vp);
810 if (bp->b_flags & B_PFSTORE) { /* XXXX */
811 ObtainWriteLock(&vcp->lock, 404);
812 if (vcp->v.v_gnode->gn_mwrcnt) {
813 afs_offs_t newlength =
814 (afs_offs_t) dbtob(bp->b_blkno) + bp->b_bcount;
815 if (vcp->m.Length < newlength) {
816 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH,
817 ICL_TYPE_STRING, __FILE__, ICL_TYPE_LONG,
818 __LINE__, ICL_TYPE_OFFSET,
819 ICL_HANDLE_OFFSET(vcp->m.Length),
820 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(newlength));
821 vcp->m.Length = newlength;
824 ReleaseWriteLock(&vcp->lock);
826 /* If the buffer represents a protection violation, rather than
827 * an actual request for I/O, no special action need be taken.
829 if (bp->b_flags & B_PFPROT) {
830 iodone(bp); /* Notify all users of the buffer that we're done */
835 ObtainWriteLock(&vcp->pvmlock, 211);
837 * First map its data area to a region in the current address space
838 * by calling vm_att with the subspace identifier, and a pointer to
839 * the data area. vm_att returns a new data area pointer, but we
840 * also want to hang onto the old one.
842 tmpaddr = bp->b_baddr;
843 bp->b_baddr = (caddr_t) vm_att(bp->b_xmemd.subspace_id, tmpaddr);
844 tmperr = afs_ustrategy(bp); /* temp variable saves offset calculation */
845 if (tmperr) { /* in non-error case */
846 bp->b_flags |= B_ERROR; /* should other flags remain set ??? */
847 bp->b_error = tmperr;
850 /* Unmap the buffer's data area by calling vm_det. Reset data area
851 * to the value that we saved above.
854 bp->b_baddr = tmpaddr;
857 * buffer may be linked with other buffers via the b_work field.
858 * See also naix_vm_strategy. For each buffer in the chain (including
859 * bp) notify all users of the buffer that the daemon is finished
860 * using it by calling iodone.
861 * assumes iodone can modify the b_work field.
864 tbp2 = (struct buf *)tbp1->b_work;
869 tbp1 = (struct buf *)tbp2->b_work;
875 ReleaseWriteLock(&vcp->pvmlock); /* Unlock the vnode. */
877 } /* infinite loop (unless we're interrupted) */
878 } /* end of afs_BioDaemon() */
880 #else /* AFS_AIX41_ENV */
884 struct afs_bioqueue {
889 struct afs_bioqueue afs_bioqueue;
890 struct buf *afs_busyq = NULL;
891 struct buf *afs_asyncbuf;
892 afs_int32 afs_biodcnt = 0;
894 /* in implementing this, I assumed that all external linked lists were
897 * Several places in this code traverse a linked list. The algorithm
898 * used here is probably unfamiliar to most people. Careful examination
899 * will show that it eliminates an assignment inside the loop, as compared
900 * to the standard algorithm, at the cost of occasionally using an extra
906 * This function obtains, and returns, a pointer to a buffer for
907 * processing by a daemon. It sleeps until such a buffer is available.
908 * The source of buffers for it is the list afs_asyncbuf (see also
909 * naix_vm_strategy). This function may be invoked concurrently by
910 * several processes, that is, several instances of the same daemon.
911 * naix_vm_strategy, which adds buffers to the list, runs at interrupt
912 * level, while get_bioreq runs at process level.
914 * The common kernel paradigm of sleeping and waking up, in which all the
915 * competing processes sleep waiting for wakeups on one address, is not
916 * followed here. Instead, the following paradigm is used: when a daemon
917 * goes to sleep, it checks for other sleeping daemons. If there aren't any,
918 * it sleeps on the address of variable afs_asyncbuf. But if there is
919 * already a daemon sleeping on that address, it threads its own unique
920 * address onto a list, and sleeps on that address. This way, every
921 * sleeper is sleeping on a different address, and every wakeup wakes up
922 * exactly one daemon. This prevents a whole bunch of daemons from waking
923 * up and then immediately having to go back to sleep. This provides a
924 * performance gain and makes the I/O scheduling a bit more deterministic.
925 * The list of sleepers is variable afs_bioqueue. The unique address
926 * on which to sleep is passed to get_bioreq as its parameter.
928 /*static*/ struct buf *
930 struct afs_bioqueue *self; /* address on which to sleep */
933 struct buf *bp = NULL;
935 struct buf **bestlbpP, **lbpP;
937 struct buf *t1P, *t2P; /* temp pointers for list manipulation */
940 struct afs_bioqueue *s;
942 /* ??? Does the forward pointer of the returned buffer need to be NULL?
945 /* Disable interrupts from the strategy function, and save the
946 * prior priority level
948 oldPriority = i_disable(INTMAX);
950 /* Each iteration of following loop either pulls
951 * a buffer off afs_asyncbuf, or sleeps.
953 while (1) { /* inner loop */
955 /* look for oldest buffer */
956 bp = bestbp = afs_asyncbuf;
957 bestage = (int)bestbp->av_back;
958 bestlbpP = &afs_asyncbuf;
964 if ((int)bp->av_back - bestage < 0) {
967 bestage = (int)bp->av_back;
971 *bestlbpP = bp->av_forw;
976 /* If afs_asyncbuf is null, it is necessary to go to sleep.
977 * There are two possibilities: either there is already a
978 * daemon that is sleeping on the address of afs_asyncbuf,
981 if (afs_bioqueue.sleeper) {
983 QAdd(&(afs_bioqueue.lruq), &(self->lruq));
984 interrupted = sleep((caddr_t) self, PCATCH | (PZERO + 1));
985 if (self->lruq.next != &self->lruq) { /* XXX ##3 XXX */
986 QRemove(&(self->lruq)); /* dequeue */
989 afs_bioqueue.sleeper = FALSE;
991 /* re-enable interrupts from strategy */
992 i_enable(oldPriority);
997 afs_bioqueue.sleeper = TRUE;
999 sleep((caddr_t) & afs_asyncbuf, PCATCH | (PZERO + 1));
1000 afs_bioqueue.sleeper = FALSE;
1003 * We need to wakeup another daemon if present
1004 * since we were waiting on afs_asyncbuf.
1006 #ifdef notdef /* The following doesn't work as advertised */
1007 if (afs_bioqueue.lruq.next != &afs_bioqueue.lruq) {
1008 struct squeue *bq = afs_bioqueue.lruq.next;
1013 /* re-enable interrupts from strategy */
1014 i_enable(oldPriority);
1020 } /* end of "else asyncbuf is empty" */
1021 } /* end of "inner loop" */
1025 i_enable(oldPriority); /* re-enable interrupts from strategy */
1027 /* For the convenience of other code, replace the gnodes in
1028 * the b_vp field of bp and the other buffers on the b_work
1029 * chain with the corresponding vnodes.
1031 * ??? what happens to the gnodes? They're not just cut loose,
1035 t2P = (struct buf *)t1P->b_work;
1036 t1P->b_vp = ((struct gnode *)t1P->b_vp)->gn_vnode;
1040 t1P = (struct buf *)t2P->b_work;
1041 t2P->b_vp = ((struct gnode *)t2P->b_vp)->gn_vnode;
1046 /* If the buffer does not specify I/O, it may immediately
1047 * be returned to the caller. This condition is detected
1048 * by examining the buffer's flags (the b_flags field). If
1049 * the B_PFPROT bit is set, the buffer represents a protection
1050 * violation, rather than a request for I/O. The remainder
1051 * of the outer loop handles the case where the B_PFPROT bit is clear.
1053 if (bp->b_flags & B_PFPROT) {
1057 /* wake up another process to handle the next buffer, and return
1060 oldPriority = i_disable(INTMAX);
1062 /* determine where to find the sleeping process.
1063 * There are two cases: either it is sleeping on
1064 * afs_asyncbuf, or it is sleeping on its own unique
1065 * address. These cases are distinguished by examining
1066 * the sleeper field of afs_bioqueue.
1068 if (afs_bioqueue.sleeper) {
1069 wakeup(&afs_asyncbuf);
1071 if (afs_bioqueue.lruq.next == &afs_bioqueue.lruq) {
1072 /* queue is empty, what now? ??? */
1073 /* Should this be impossible, or does */
1074 /* it just mean that nobody is sleeping? */ ;
1076 struct squeue *bq = afs_bioqueue.lruq.next;
1080 afs_bioqueue.sleeper = TRUE;
1083 i_enable(oldPriority); /* re-enable interrupts from strategy */
1086 } /* end of function get_bioreq() */
1091 * This function is the daemon. It is called from the syscall
1092 * interface. Ordinarily, a script or an administrator will run a
1093 * daemon startup utility, specifying the number of I/O daemons to
1094 * run. The utility will fork off that number of processes,
1095 * each making the appropriate syscall, which will cause this
1096 * function to be invoked.
1098 static int afs_initbiod = 0; /* this is self-initializing code */
1100 afs_BioDaemon(nbiods)
1103 struct afs_bioqueue *self;
1104 afs_int32 code, s, pflg = 0;
1106 struct buf *bp, *bp1, *tbp1, *tbp2; /* temp pointers only */
1111 if (!afs_initbiod) {
1114 /* Initialize the queue of waiting processes, afs_bioqueue. */
1115 QInit(&(afs_bioqueue.lruq));
1118 /* establish ourself as a kernel process so shutdown won't kill us */
1119 /* u.u_procp->p_flag |= SKPROC;*/
1121 /* Initialize a token (self) to use in the queue of sleeping processes. */
1122 self = (struct afs_bioqueue *)afs_osi_Alloc(sizeof(struct afs_bioqueue));
1123 pin(self, sizeof(struct afs_bioqueue)); /* fix in memory */
1124 memset(self, 0, sizeof(*self));
1125 QInit(&(self->lruq)); /* initialize queue entry pointers */
1128 /* Ignore HUP signals... */
1129 SIGDELSET(u.u_procp->p_sig, SIGHUP);
1130 SIGADDSET(u.u_procp->p_sigignore, SIGHUP);
1131 SIGDELSET(u.u_procp->p_sigcatch, SIGHUP);
1132 /* Main body starts here -- this is an intentional infinite loop, and
1135 * Now, the loop will exit if get_bioreq() returns NULL, indicating
1136 * that we've been interrupted.
1139 bp = afs_get_bioreq(self);
1141 break; /* we were interrupted */
1142 if (code = setjmpx(&jmpbuf)) {
1143 /* This should not have happend, maybe a lack of resources */
1145 for (bp1 = bp; bp; bp = bp1) {
1150 bp->b_flags |= B_ERROR;
1156 vcp = VTOAFS(bp->b_vp);
1157 if (bp->b_flags & B_PFSTORE) {
1158 ObtainWriteLock(&vcp->lock, 210);
1159 if (vcp->v.v_gnode->gn_mwrcnt) {
1160 afs_offs_t newlength =
1161 (afs_offs_t) dbtob(bp->b_blkno) + bp->b_bcount;
1162 if (vcp->m.Length < newlength) {
1163 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH,
1164 ICL_TYPE_STRING, __FILE__, ICL_TYPE_LONG,
1165 __LINE__, ICL_TYPE_OFFSET,
1166 ICL_HANDLE_OFFSET(vcp->m.Length),
1167 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(newlength));
1168 vcp->m.Length = newlength;
1171 ReleaseWriteLock(&vcp->lock);
1173 /* If the buffer represents a protection violation, rather than
1174 * an actual request for I/O, no special action need be taken.
1176 if (bp->b_flags & B_PFPROT) {
1177 iodone(bp); /* Notify all users of the buffer that we're done */
1181 ObtainWriteLock(&vcp->pvmlock, 558);
1183 * First map its data area to a region in the current address space
1184 * by calling vm_att with the subspace identifier, and a pointer to
1185 * the data area. vm_att returns a new data area pointer, but we
1186 * also want to hang onto the old one.
1188 tmpaddr = bp->b_baddr;
1189 bp->b_baddr = vm_att(bp->b_xmemd.subspace_id, tmpaddr);
1190 tmperr = afs_ustrategy(bp); /* temp variable saves offset calculation */
1191 if (tmperr) { /* in non-error case */
1192 bp->b_flags |= B_ERROR; /* should other flags remain set ??? */
1193 bp->b_error = tmperr;
1196 /* Unmap the buffer's data area by calling vm_det. Reset data area
1197 * to the value that we saved above.
1199 vm_det(bp->b_un.b_addr);
1200 bp->b_baddr = tmpaddr;
1203 * buffer may be linked with other buffers via the b_work field.
1204 * See also naix_vm_strategy. For each buffer in the chain (including
1205 * bp) notify all users of the buffer that the daemon is finished
1206 * using it by calling iodone.
1207 * assumes iodone can modify the b_work field.
1210 tbp2 = (struct buf *)tbp1->b_work;
1215 tbp1 = (struct buf *)tbp2->b_work;
1221 ReleaseWriteLock(&vcp->pvmlock); /* Unlock the vnode. */
1223 } /* infinite loop (unless we're interrupted) */
1224 unpin(self, sizeof(struct afs_bioqueue));
1225 afs_osi_Free(self, sizeof(struct afs_bioqueue));
1226 } /* end of afs_BioDaemon() */
1227 #endif /* AFS_AIX41_ENV */
1228 #endif /* AFS_AIX32_ENV */
1233 afs_BackgroundDaemon(void)
1235 struct brequest *tb;
1238 AFS_STATCNT(afs_BackgroundDaemon);
1239 /* initialize subsystem */
1241 LOCK_INIT(&afs_xbrs, "afs_xbrs");
1242 memset((char *)afs_brs, 0, sizeof(afs_brs));
1244 #if defined (AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1246 * steal the first daemon for doing delayed DSlot flushing
1247 * (see afs_GetDownDSlot)
1256 MObtainWriteLock(&afs_xbrs, 302);
1259 struct brequest *min_tb = NULL;
1261 if (afs_termState == AFSOP_STOP_BKG) {
1262 if (--afs_nbrs <= 0)
1263 afs_termState = AFSOP_STOP_TRUNCDAEMON;
1264 MReleaseWriteLock(&afs_xbrs);
1265 afs_osi_Wakeup(&afs_termState);
1269 /* find a request */
1272 for (i = 0; i < NBRS; i++, tb++) {
1273 /* look for request with smallest ts */
1274 if ((tb->refCount > 0) && !(tb->flags & BSTARTED)) {
1275 /* new request, not yet picked up */
1276 if ((min_tb && (min_ts - tb->ts > 0)) || !min_tb) {
1282 if ((tb = min_tb)) {
1283 /* claim and process this request */
1284 tb->flags |= BSTARTED;
1285 MReleaseWriteLock(&afs_xbrs);
1287 afs_Trace1(afs_iclSetp, CM_TRACE_BKG1, ICL_TYPE_INT32,
1289 if (tb->opcode == BOP_FETCH)
1291 else if (tb->opcode == BOP_STORE)
1293 else if (tb->opcode == BOP_PATH)
1296 panic("background bop");
1299 tb->vc->vrefCount--; /* fix up reference count */
1301 AFS_RELE(AFSTOV(tb->vc)); /* MUST call vnode layer or could lose vnodes */
1307 tb->cred = (struct AFS_UCRED *)0;
1309 afs_BRelease(tb); /* this grabs and releases afs_xbrs lock */
1310 MObtainWriteLock(&afs_xbrs, 305);
1313 /* wait for new request */
1315 MReleaseWriteLock(&afs_xbrs);
1316 afs_osi_Sleep(&afs_brsDaemons);
1317 MObtainWriteLock(&afs_xbrs, 307);
1325 shutdown_daemons(void)
1327 AFS_STATCNT(shutdown_daemons);
1328 if (afs_cold_shutdown) {
1329 afs_brsDaemons = brsInit = 0;
1330 rxepoch_checked = afs_nbrs = 0;
1331 memset((char *)afs_brs, 0, sizeof(afs_brs));
1332 memset((char *)&afs_xbrs, 0, sizeof(afs_lock_t));
1334 #ifdef AFS_AIX32_ENV
1335 #ifdef AFS_AIX41_ENV
1336 lock_free(&afs_asyncbuf_lock);
1337 unpin(&afs_asyncbuf, sizeof(struct buf *));
1338 unpin(&afs_asyncbuf_cv, sizeof(afs_int32));
1339 #else /* AFS_AIX41_ENV */
1342 memset((char *)&afs_bioqueue, 0, sizeof(struct afs_bioqueue));
1349 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1351 * sgi - daemon - handles certain operations that otherwise
1352 * would use up too much kernel stack space
1354 * This all assumes that since the caller must have the xdcache lock
1355 * exclusively that the list will never be more than one long
1356 * and noone else can attempt to add anything until we're done.
1358 SV_TYPE afs_sgibksync;
1359 SV_TYPE afs_sgibkwait;
1360 lock_t afs_sgibklock;
1361 struct dcache *afs_sgibklist;
1369 if (afs_sgibklock == NULL) {
1370 SV_INIT(&afs_sgibksync, "bksync", 0, 0);
1371 SV_INIT(&afs_sgibkwait, "bkwait", 0, 0);
1372 SPINLOCK_INIT(&afs_sgibklock, "bklock");
1374 s = SPLOCK(afs_sgibklock);
1376 /* wait for something to do */
1377 SP_WAIT(afs_sgibklock, s, &afs_sgibksync, PINOD);
1378 osi_Assert(afs_sgibklist);
1380 /* XX will probably need to generalize to real list someday */
1381 s = SPLOCK(afs_sgibklock);
1382 while (afs_sgibklist) {
1383 tdc = afs_sgibklist;
1384 afs_sgibklist = NULL;
1385 SPUNLOCK(afs_sgibklock, s);
1387 tdc->dflags &= ~DFEntryMod;
1388 afs_WriteDCache(tdc, 1);
1390 s = SPLOCK(afs_sgibklock);
1393 /* done all the work - wake everyone up */
1394 while (SV_SIGNAL(&afs_sgibkwait));