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>
28 extern void afsi_log(char *pattern, ...);
31 osi_mutex_t cm_bufGetMutex;
32 #ifdef AFS_FREELANCE_CLIENT
33 extern osi_mutex_t cm_Freelance_Lock;
36 /* functions called back from the buffer package when reading or writing data,
37 * or when holding or releasing a vnode pointer.
39 long cm_BufWrite(void *vfidp, osi_hyper_t *offsetp, long length, long flags,
40 cm_user_t *userp, cm_req_t *reqp)
42 /* store the data back from this buffer; the buffer is locked and held,
43 * but the vnode involved isn't locked, yet. It is held by its
44 * reference from the buffer, which won't change until the buffer is
45 * released by our caller. Thus, we don't have to worry about holding
49 cm_fid_t *fidp = vfidp;
53 AFSFetchStatus outStatus;
54 AFSStoreStatus inStatus;
58 struct rx_call *callp;
65 cm_bulkIO_t biod; /* bulk IO descriptor */
67 osi_assert(userp != NULL);
69 /* now, the buffer may or may not be filled with good data (buf_GetNew
70 * drops lots of locks, and may indeed return a properly initialized
71 * buffer, although more likely it will just return a new, empty, buffer.
73 scp = cm_FindSCache(fidp);
75 return CM_ERROR_NOSUCHFILE; /* shouldn't happen */
77 cm_AFSFidFromFid(&tfid, fidp);
79 lock_ObtainMutex(&scp->mx);
81 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
83 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
84 lock_ReleaseMutex(&scp->mx);
85 cm_ReleaseSCache(scp);
89 if (biod.length == 0) {
90 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
91 lock_ReleaseMutex(&scp->mx);
92 cm_ReleaseBIOD(&biod, 1); /* should be a NOOP */
93 cm_ReleaseSCache(scp);
97 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
98 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
100 /* prepare the output status for the store */
101 scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
102 cm_StatusFromAttr(&inStatus, scp, NULL);
103 truncPos = scp->length.LowPart;
104 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
105 && scp->truncPos.LowPart < (unsigned long) truncPos)
106 truncPos = scp->truncPos.LowPart;
107 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
109 /* compute how many bytes to write from this buffer */
110 thyper = LargeIntegerSubtract(scp->length, biod.offset);
111 if (LargeIntegerLessThanZero(thyper)) {
112 /* entire buffer is past EOF */
116 /* otherwise write out part of buffer before EOF, but not
117 * more than bufferSize bytes.
119 nbytes = thyper.LowPart;
120 if (nbytes > biod.length)
121 nbytes = biod.length;
124 lock_ReleaseMutex(&scp->mx);
126 /* now we're ready to do the store operation */
128 code = cm_Conn(&scp->fid, userp, reqp, &connp);
132 callp = rx_NewCall(connp->callp);
134 osi_Log3(afsd_logp, "CALL StoreData vp %x, off 0x%x, size 0x%x",
135 (long) scp, biod.offset.LowPart, nbytes);
137 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
138 biod.offset.LowPart, nbytes, truncPos);
141 /* write the data from the the list of buffers */
145 qdp = biod.bufListEndp;
147 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
148 osi_assert(qdp != NULL);
149 bufp = osi_GetQData(qdp);
150 bufferp = bufp->datap;
152 if (wbytes > buf_bufferSize)
153 wbytes = buf_bufferSize;
155 /* write out wbytes of data from bufferp */
156 temp = rx_Write(callp, bufferp, wbytes);
157 if (temp != wbytes) {
158 osi_Log2(afsd_logp, "rx_Write failed %d != %d",temp,wbytes);
162 osi_Log1(afsd_logp, "rx_Write succeeded %d",temp);
165 } /* while more bytes to write */
166 } /* if RPC started successfully */
168 osi_Log1(afsd_logp, "StartRXAFS_StoreData failed (%lX)",code);
171 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
173 osi_Log1(afsd_logp, "EndRXAFS_StoreData failed (%lX)",code);
175 code = rx_EndCall(callp, code);
176 osi_Log0(afsd_logp, "CALL StoreData DONE");
178 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
179 code = cm_MapRPCError(code, reqp);
181 /* now, clean up our state */
182 lock_ObtainMutex(&scp->mx);
184 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
187 /* now, here's something a little tricky: in AFS 3, a dirty
188 * length can't be directly stored, instead, a dirty chunk is
189 * stored that sets the file's size (by writing and by using
190 * the truncate-first option in the store call).
192 * At this point, we've just finished a store, and so the trunc
193 * pos field is clean. If the file's size at the server is at
194 * least as big as we think it should be, then we turn off the
195 * length dirty bit, since all the other dirty buffers must
196 * precede this one in the file.
198 * The file's desired size shouldn't be smaller than what's
199 * stored at the server now, since we just did the trunc pos
202 * We have to turn off the length dirty bit as soon as we can,
203 * so that we see updates made by other machines.
205 if (outStatus.Length >= scp->length.LowPart)
206 scp->mask &= ~CM_SCACHEMASK_LENGTH;
207 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
209 if (code == CM_ERROR_SPACE)
210 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
211 else if (code == CM_ERROR_QUOTA)
212 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
214 lock_ReleaseMutex(&scp->mx);
215 cm_ReleaseBIOD(&biod, 1);
216 cm_ReleaseSCache(scp);
222 * Truncate the file, by sending a StoreData RPC with zero length.
224 * Called with scp locked. Releases and re-obtains the lock.
226 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
228 AFSFetchStatus outStatus;
229 AFSStoreStatus inStatus;
235 struct rx_call *callp;
237 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
238 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
239 CM_SCACHESYNC_STOREDATA_EXCL);
241 /* prepare the output status for the store */
242 inStatus.Mask = AFS_SETMODTIME;
243 inStatus.ClientModTime = scp->clientModTime;
244 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
246 /* calculate truncation position */
247 truncPos = scp->length.LowPart;
248 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
249 && scp->truncPos.LowPart < (unsigned long) truncPos)
250 truncPos = scp->truncPos.LowPart;
251 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
253 lock_ReleaseMutex(&scp->mx);
255 cm_AFSFidFromFid(&tfid, &scp->fid);
257 /* now we're ready to do the store operation */
259 code = cm_Conn(&scp->fid, userp, reqp, &connp);
263 callp = rx_NewCall(connp->callp);
265 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
269 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
270 code = rx_EndCall(callp, code);
271 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
272 code = cm_MapRPCError(code, reqp);
274 /* now, clean up our state */
275 lock_ObtainMutex(&scp->mx);
277 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
281 * For explanation of handling of CM_SCACHEMASK_LENGTH,
284 if (outStatus.Length >= scp->length.LowPart)
285 scp->mask &= ~CM_SCACHEMASK_LENGTH;
286 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
292 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
294 *bytesReadp = buf_bufferSize;
296 /* now return a code that means that I/O is done */
300 /* stabilize scache entry, and return with it locked so
303 long cm_BufStabilize(void *parmp, cm_user_t *userp, cm_req_t *reqp)
310 lock_ObtainMutex(&scp->mx);
311 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
312 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
314 lock_ReleaseMutex(&scp->mx);
321 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
322 long cm_BufUnstabilize(void *parmp, cm_user_t *userp)
328 lock_ReleaseMutex(&scp->mx);
330 /* always succeeds */
334 cm_buf_ops_t cm_bufOps = {
341 int cm_InitDCache(long chunkSize, long nbuffers)
343 lock_InitializeMutex(&cm_bufGetMutex, "buf_Get mutex");
345 buf_nbuffers = nbuffers;
346 return buf_Init(&cm_bufOps);
349 /* check to see if we have an up-to-date buffer. The buffer must have
350 * previously been obtained by calling buf_Get.
352 * Make sure we have a callback, and that the dataversion matches.
354 * Scp must be locked.
356 * Bufp *may* be locked.
358 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
361 if (!cm_HaveCallback(scp))
364 & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
365 == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
367 if (bufp->dataVersion == scp->dataVersion)
370 code = lock_TryMutex(&bufp->mx);
372 /* don't have the lock, and can't lock it, then
379 /* remember dirty flag for later */
380 code = bufp->flags & CM_BUF_DIRTY;
382 /* release lock if we obtained it here */
384 lock_ReleaseMutex(&bufp->mx);
386 /* if buffer was dirty, buffer is acceptable for use */
393 /* used when deciding whether to do a prefetch or not */
394 long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, long length,
395 cm_user_t *up, cm_req_t *reqp, osi_hyper_t *realBasep)
403 /* now scan all buffers in the range, looking for any that look like
408 lock_ObtainMutex(&scp->mx);
410 /* get callback so we can do a meaningful dataVersion comparison */
411 code = cm_SyncOp(scp, NULL, up, reqp, 0,
412 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
414 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
415 lock_ReleaseMutex(&scp->mx);
419 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
420 /* we're past the end of file */
424 bp = buf_Find(scp, &tbase);
425 /* We cheat slightly by not locking the bp mutex. */
428 & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
429 && bp->dataVersion != scp->dataVersion)
436 /* if this buffer is essentially guaranteed to require a fetch,
437 * break out here and return this position.
442 toffset.LowPart = buf_bufferSize;
443 toffset.HighPart = 0;
444 tbase = LargeIntegerAdd(toffset, tbase);
445 length -= buf_bufferSize;
448 /* if we get here, either everything is fine or stop stopped us at a
449 * particular buffer in the range that definitely needs to be fetched.
452 /* return non-zero code since realBasep won't be valid */
453 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
457 /* successfully found a page that will need fetching */
461 lock_ReleaseMutex(&scp->mx);
465 void cm_BkgStore(cm_scache_t *scp, long p1, long p2, long p3, long p4,
473 req.flags |= CM_REQ_NORETRY;
475 toffset.LowPart = p1;
476 toffset.HighPart = p2;
479 osi_Log2(afsd_logp, "Starting BKG store vp 0x%x, base 0x%x", scp, p1);
481 cm_BufWrite(&scp->fid, &toffset, length, /* flags */ 0, userp, &req);
483 lock_ObtainMutex(&scp->mx);
484 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
485 lock_ReleaseMutex(&scp->mx);
488 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base)
493 thyper.LowPart = cm_chunkSize;
495 thyper = LargeIntegerAdd(*base, thyper);
496 thyper.LowPart &= (-cm_chunkSize);
497 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
498 scp->prefetch.base = *base;
499 if (LargeIntegerGreaterThan(thyper, scp->prefetch.end))
500 scp->prefetch.end = thyper;
502 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
505 /* do the prefetch */
506 void cm_BkgPrefetch(cm_scache_t *scp, long p1, long p2, long p3, long p4,
513 int cpff = 0; /* cleared prefetch flag */
517 req.flags |= CM_REQ_NORETRY;
523 osi_Log2(afsd_logp, "Starting BKG prefetch vp 0x%x, base 0x%x", scp, p1);
525 code = buf_Get(scp, &base, &bp);
527 lock_ObtainMutex(&scp->mx);
529 if (code || (bp->cmFlags & CM_BUF_CMFETCHING)) {
530 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
531 lock_ReleaseMutex(&scp->mx);
535 code = cm_GetBuffer(scp, bp, &cpff, userp, &req);
537 cm_ClearPrefetchFlag(code, scp, &base);
538 lock_ReleaseMutex(&scp->mx);
543 /* a read was issued to offsetp, and we have to determine whether we should
546 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp,
547 cm_user_t *userp, cm_req_t *reqp)
550 osi_hyper_t realBase;
551 osi_hyper_t readBase;
554 /* round up to chunk boundary */
555 readBase.LowPart += (cm_chunkSize-1);
556 readBase.LowPart &= (-cm_chunkSize);
558 lock_ObtainMutex(&scp->mx);
559 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
560 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
561 lock_ReleaseMutex(&scp->mx);
564 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
566 /* start the scan at the latter of the end of this read or
567 * the end of the last fetched region.
569 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
570 readBase = scp->prefetch.end;
572 lock_ReleaseMutex(&scp->mx);
574 code = cm_CheckFetchRange(scp, &readBase, cm_chunkSize, userp, reqp,
577 return; /* can't find something to prefetch */
579 osi_Log2(afsd_logp, "BKG Prefetch request vp 0x%x, base 0x%x",
580 scp, realBase.LowPart);
582 cm_QueueBKGRequest(scp, cm_BkgPrefetch, realBase.LowPart,
583 realBase.HighPart, cm_chunkSize, 0, userp);
586 /* scp must be locked; temporarily unlocked during processing.
587 * If returns 0, returns buffers held in biop, and with
588 * CM_BUF_CMSTORING set.
590 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
591 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
592 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
593 * must be woken, and the event must be set when the I/O is done. All of this
594 * is required so that buf_WaitIO synchronizes properly with the buffer as it
595 * is being written out.
597 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
598 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
601 osi_queueData_t *qdp;
604 osi_hyper_t scanStart; /* where to start scan for dirty pages */
605 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
606 osi_hyper_t firstModOffset; /* offset of first modified page in range */
609 long flags; /* flags to cm_SyncOp */
611 /* clear things out */
612 biop->scp = scp; /* don't hold */
613 biop->offset = *inOffsetp;
615 biop->bufListp = NULL;
616 biop->bufListEndp = NULL;
619 /* reserve a chunk's worth of buffers */
620 lock_ReleaseMutex(&scp->mx);
621 buf_ReserveBuffers(cm_chunkSize / buf_bufferSize);
622 lock_ObtainMutex(&scp->mx);
625 for(temp = 0; temp < inSize; temp += buf_bufferSize, bufp = NULL) {
627 thyper.LowPart = temp;
628 tbase = LargeIntegerAdd(*inOffsetp, thyper);
630 bufp = buf_Find(scp, &tbase);
632 /* get buffer mutex and scp mutex safely */
633 lock_ReleaseMutex(&scp->mx);
634 lock_ObtainMutex(&bufp->mx);
635 lock_ObtainMutex(&scp->mx);
637 flags = CM_SCACHESYNC_NEEDCALLBACK
638 | CM_SCACHESYNC_GETSTATUS
639 | CM_SCACHESYNC_STOREDATA
640 | CM_SCACHESYNC_BUFLOCKED;
641 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
643 lock_ReleaseMutex(&bufp->mx);
645 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
649 /* if the buffer is dirty, we're done */
650 if (bufp->flags & CM_BUF_DIRTY) {
651 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
652 "WRITING w/o CMSTORING in SetupStoreBIOD");
653 bufp->flags |= CM_BUF_WRITING;
657 /* this buffer is clean, so there's no reason to process it */
658 cm_SyncOpDone(scp, bufp, flags);
659 lock_ReleaseMutex(&bufp->mx);
666 /* if we get here, if bufp is null, we didn't find any dirty buffers
667 * that weren't already being stored back, so we just quit now.
673 /* don't need buffer mutex any more */
674 lock_ReleaseMutex(&bufp->mx);
676 /* put this element in the list */
678 osi_SetQData(qdp, bufp);
679 /* don't have to hold bufp, since held by buf_Find above */
680 osi_QAddH((osi_queue_t **) &biop->bufListp,
681 (osi_queue_t **) &biop->bufListEndp,
683 biop->length = buf_bufferSize;
684 firstModOffset = bufp->offset;
685 biop->offset = firstModOffset;
687 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
688 scanStart = *inOffsetp;
689 scanStart.LowPart &= (-cm_chunkSize);
690 thyper.LowPart = cm_chunkSize;
692 scanEnd = LargeIntegerAdd(scanStart, thyper);
694 flags = CM_SCACHESYNC_NEEDCALLBACK
695 | CM_SCACHESYNC_GETSTATUS
696 | CM_SCACHESYNC_STOREDATA
697 | CM_SCACHESYNC_BUFLOCKED
698 | CM_SCACHESYNC_NOWAIT;
700 /* start by looking backwards until scanStart */
701 thyper.HighPart = 0; /* hyper version of buf_bufferSize */
702 thyper.LowPart = buf_bufferSize;
703 tbase = LargeIntegerSubtract(firstModOffset, thyper);
704 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
705 /* see if we can find the buffer */
706 bufp = buf_Find(scp, &tbase);
710 /* try to lock it, and quit if we can't (simplifies locking) */
711 code = lock_TryMutex(&bufp->mx);
717 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
719 lock_ReleaseMutex(&bufp->mx);
724 if (!(bufp->flags & CM_BUF_DIRTY)) {
725 /* buffer is clean, so we shouldn't add it */
726 cm_SyncOpDone(scp, bufp, flags);
727 lock_ReleaseMutex(&bufp->mx);
732 /* don't need buffer mutex any more */
733 lock_ReleaseMutex(&bufp->mx);
735 /* we have a dirty buffer ready for storing. Add it to the tail
736 * of the list, since it immediately precedes all of the disk
737 * addresses we've already collected.
740 osi_SetQData(qdp, bufp);
741 /* no buf_hold necessary, since we have it held from buf_Find */
742 osi_QAddT((osi_queue_t **) &biop->bufListp,
743 (osi_queue_t **) &biop->bufListEndp,
746 /* update biod info describing the transfer */
747 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
748 biop->length += buf_bufferSize;
750 /* update loop pointer */
751 tbase = LargeIntegerSubtract(tbase, thyper);
752 } /* while loop looking for pages preceding the one we found */
754 /* now, find later dirty, contiguous pages, and add them to the list */
755 thyper.HighPart = 0; /* hyper version of buf_bufferSize */
756 thyper.LowPart = buf_bufferSize;
757 tbase = LargeIntegerAdd(firstModOffset, thyper);
758 while(LargeIntegerLessThan(tbase, scanEnd)) {
759 /* see if we can find the buffer */
760 bufp = buf_Find(scp, &tbase);
764 /* try to lock it, and quit if we can't (simplifies locking) */
765 code = lock_TryMutex(&bufp->mx);
771 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
773 lock_ReleaseMutex(&bufp->mx);
778 if (!(bufp->flags & CM_BUF_DIRTY)) {
779 /* buffer is clean, so we shouldn't add it */
780 cm_SyncOpDone(scp, bufp, flags);
781 lock_ReleaseMutex(&bufp->mx);
786 /* don't need buffer mutex any more */
787 lock_ReleaseMutex(&bufp->mx);
789 /* we have a dirty buffer ready for storing. Add it to the head
790 * of the list, since it immediately follows all of the disk
791 * addresses we've already collected.
794 osi_SetQData(qdp, bufp);
795 /* no buf_hold necessary, since we have it held from buf_Find */
796 osi_QAddH((osi_queue_t **) &biop->bufListp,
797 (osi_queue_t **) &biop->bufListEndp,
800 /* update biod info describing the transfer */
801 biop->length += buf_bufferSize;
803 /* update loop pointer */
804 tbase = LargeIntegerAdd(tbase, thyper);
805 } /* while loop looking for pages following the first page we found */
807 /* finally, we're done */
811 /* scp must be locked; temporarily unlocked during processing.
812 * If returns 0, returns buffers held in biop, and with
813 * CM_BUF_CMFETCHING flags set.
814 * If an error is returned, we don't return any buffers.
816 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
817 cm_bulkIO_t *biop, cm_user_t *up, cm_req_t *reqp)
821 osi_hyper_t toffset; /* a long long temp variable */
822 osi_hyper_t pageBase; /* base offset we're looking at */
823 osi_queueData_t *qdp; /* one temp queue structure */
824 osi_queueData_t *tqdp; /* another temp queue structure */
825 long collected; /* how many bytes have been collected */
828 osi_hyper_t fileSize; /* the # of bytes in the file */
829 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
830 osi_queueData_t *heldBufListEndp; /* first one */
834 biop->offset = *offsetp;
835 /* null out the list of buffers */
836 biop->bufListp = biop->bufListEndp = NULL;
839 /* first lookup the file's length, so we know when to stop */
840 code = cm_SyncOp(scp, NULL, up, reqp, 0,
841 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
845 /* copy out size, since it may change */
846 fileSize = scp->serverLength;
848 lock_ReleaseMutex(&scp->mx);
851 collected = pageBase.LowPart & (cm_chunkSize - 1);
853 heldBufListEndp = NULL;
856 * Obtaining buffers can cause dirty buffers to be recycled, which
857 * can cause a storeback, so cannot be done while we have buffers
860 * To get around this, we get buffers twice. Before reserving buffers,
861 * we obtain and release each one individually. After reserving
862 * buffers, we try to obtain them again, but only by lookup, not by
863 * recycling. If a buffer has gone away while we were waiting for
864 * the others, we just use whatever buffers we already have.
866 * On entry to this function, we are already holding a buffer, so we
867 * can't wait for reservation. So we call buf_TryReserveBuffers()
868 * instead. Not only that, we can't really even call buf_Get(), for
869 * the same reason. We can't avoid that, though. To avoid deadlock
870 * we allow only one thread to be executing the buf_Get()-buf_Release()
871 * sequence at a time.
874 /* first hold all buffers, since we can't hold any locks in buf_Get */
876 /* stop at chunk boundary */
877 if (collected >= cm_chunkSize) break;
879 /* see if the next page would be past EOF */
880 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize)) break;
882 lock_ObtainMutex(&cm_bufGetMutex);
884 code = buf_Get(scp, &pageBase, &tbp);
886 lock_ReleaseMutex(&cm_bufGetMutex);
887 lock_ObtainMutex(&scp->mx);
893 lock_ReleaseMutex(&cm_bufGetMutex);
895 toffset.HighPart = 0;
896 toffset.LowPart = buf_bufferSize;
897 pageBase = LargeIntegerAdd(toffset, pageBase);
898 collected += buf_bufferSize;
901 /* reserve a chunk's worth of buffers if possible */
902 reserving = buf_TryReserveBuffers(cm_chunkSize / buf_bufferSize);
905 collected = pageBase.LowPart & (cm_chunkSize - 1);
907 /* now hold all buffers, if they are still there */
909 /* stop at chunk boundary */
910 if (collected >= cm_chunkSize)
913 /* see if the next page would be past EOF */
914 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
917 tbp = buf_Find(scp, &pageBase);
921 /* add the buffer to the list */
923 osi_SetQData(qdp, tbp);
924 osi_QAdd((osi_queue_t **)&heldBufListp, &qdp->q);
925 if (!heldBufListEndp) heldBufListEndp = qdp;
926 /* leave tbp held (from buf_Get) */
931 collected += buf_bufferSize;
932 toffset.HighPart = 0;
933 toffset.LowPart = buf_bufferSize;
934 pageBase = LargeIntegerAdd(toffset, pageBase);
937 /* look at each buffer, adding it into the list if it looks idle and
938 * filled with old data. One special case: wait for idle if it is the
939 * first buffer since we really need that one for our caller to make
943 collected = 0; /* now count how many we'll really use */
944 for(tqdp = heldBufListEndp;
946 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
947 /* get a ptr to the held buffer */
948 tbp = osi_GetQData(tqdp);
949 pageBase = tbp->offset;
951 /* now lock the buffer lock */
952 lock_ObtainMutex(&tbp->mx);
953 lock_ObtainMutex(&scp->mx);
955 /* don't bother fetching over data that is already current */
956 if (tbp->dataVersion == scp->dataVersion) {
957 /* we don't need this buffer, since it is current */
958 lock_ReleaseMutex(&scp->mx);
959 lock_ReleaseMutex(&tbp->mx);
963 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_FETCHDATA
964 | CM_SCACHESYNC_BUFLOCKED;
966 flags |= CM_SCACHESYNC_NOWAIT;
968 /* wait for the buffer to serialize, if required. Doesn't
969 * release the scp or buffer lock(s) if NOWAIT is specified.
971 code = cm_SyncOp(scp, tbp, up, reqp, 0, flags);
973 lock_ReleaseMutex(&scp->mx);
974 lock_ReleaseMutex(&tbp->mx);
978 /* don't fetch over dirty buffers */
979 if (tbp->flags & CM_BUF_DIRTY) {
980 cm_SyncOpDone(scp, tbp, flags);
981 lock_ReleaseMutex(&scp->mx);
982 lock_ReleaseMutex(&tbp->mx);
987 lock_ReleaseMutex(&scp->mx);
988 lock_ReleaseMutex(&tbp->mx);
990 /* add the buffer to the list */
992 osi_SetQData(qdp, tbp);
993 osi_QAdd((osi_queue_t **)&biop->bufListp, &qdp->q);
994 if (!biop->bufListEndp)
995 biop->bufListEndp = qdp;
998 /* from now on, a failure just stops our collection process, but
999 * we still do the I/O to whatever we've already managed to collect.
1002 collected += buf_bufferSize;
1005 /* now, we've held in biop->bufListp all the buffer's we're really
1006 * interested in. We also have holds left from heldBufListp, and we
1007 * now release those holds on the buffers.
1009 for(qdp = heldBufListp; qdp; qdp = tqdp) {
1010 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1011 tbp = osi_GetQData(qdp);
1016 /* Caller expects this */
1017 lock_ObtainMutex(&scp->mx);
1019 /* if we got a failure setting up the first buffer, then we don't have
1020 * any side effects yet, and we also have failed an operation that the
1021 * caller requires to make any progress. Give up now.
1023 if (code && isFirst) {
1024 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
1028 /* otherwise, we're still OK, and should just return the I/O setup we've
1031 biop->length = collected;
1032 biop->reserved = reserving;
1036 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1039 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore)
1043 osi_queueData_t *qdp;
1044 osi_queueData_t *nqdp;
1047 /* Give back reserved buffers */
1049 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
1051 flags = CM_SCACHESYNC_NEEDCALLBACK;
1053 flags |= CM_SCACHESYNC_STOREDATA;
1055 flags |= CM_SCACHESYNC_FETCHDATA;
1058 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1059 /* lookup next guy first, since we're going to free this one */
1060 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1062 /* extract buffer and free queue data */
1063 bufp = osi_GetQData(qdp);
1066 /* now, mark I/O as done, unlock the buffer and release it */
1067 lock_ObtainMutex(&bufp->mx);
1068 lock_ObtainMutex(&scp->mx);
1069 cm_SyncOpDone(scp, bufp, flags);
1070 lock_ReleaseMutex(&scp->mx);
1072 /* turn off writing and wakeup users */
1074 if (bufp->flags & CM_BUF_WAITING) {
1075 osi_Wakeup((long) bufp);
1077 bufp->flags &= ~(CM_BUF_WAITING | CM_BUF_WRITING | CM_BUF_DIRTY);
1080 lock_ReleaseMutex(&bufp->mx);
1084 /* clean things out */
1085 biop->bufListp = NULL;
1086 biop->bufListEndp = NULL;
1089 /* Fetch a buffer. Called with scp locked.
1090 * The scp is locked on return.
1092 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *up,
1096 long nbytes; /* bytes in transfer */
1097 long rbytes; /* bytes in rx_Read call */
1099 AFSFetchStatus afsStatus;
1100 AFSCallBack callback;
1103 cm_buf_t *tbufp; /* buf we're filling */
1104 osi_queueData_t *qdp; /* q element we're scanning */
1106 struct rx_call *callp;
1107 cm_bulkIO_t biod; /* bulk IO descriptor */
1112 /* now, the buffer may or may not be filled with good data (buf_GetNew
1113 * drops lots of locks, and may indeed return a properly initialized
1114 * buffer, although more likely it will just return a new, empty, buffer.
1117 #ifdef AFS_FREELANCE_CLIENT
1119 // yj: if they're trying to get the /afs directory, we need to
1120 // handle it differently, since it's local rather than on any
1123 getroot = (scp==cm_rootSCachep);
1125 osi_Log1(afsd_logp,"GetBuffer returns cm_rootSCachep=%x",cm_rootSCachep);
1128 cm_AFSFidFromFid(&tfid, &scp->fid);
1130 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, up, reqp);
1132 /* couldn't even get the first page setup properly */
1133 osi_Log1(afsd_logp, "SetupFetchBIOD failure code %d", code);
1137 /* once we get here, we have the callback in place, we know that no one
1138 * is fetching the data now. Check one last time that we still have
1139 * the wrong data, and then fetch it if we're still wrong.
1141 * We can lose a race condition and end up with biod.length zero, in
1142 * which case we just retry.
1144 if (bufp->dataVersion == scp->dataVersion || biod.length == 0) {
1145 osi_Log3(afsd_logp, "Bad DVs %d, %d or length 0x%x",
1146 bufp->dataVersion, scp->dataVersion, biod.length);
1147 if ((bufp->dataVersion == -1
1148 || bufp->dataVersion < scp->dataVersion)
1149 && LargeIntegerGreaterThanOrEqualTo(bufp->offset,
1150 scp->serverLength)) {
1151 if (bufp->dataVersion == -1)
1152 memset(bufp->datap, 0, buf_bufferSize);
1153 bufp->dataVersion = scp->dataVersion;
1155 lock_ReleaseMutex(&scp->mx);
1156 cm_ReleaseBIOD(&biod, 0);
1157 lock_ObtainMutex(&scp->mx);
1161 lock_ReleaseMutex(&scp->mx);
1164 DPRINTF("cm_GetBuffer: fetching data scpDV=%d bufDV=%d scp=%x bp=%x dcp=%x\n",
1165 scp->dataVersion, bufp->dataVersion, scp, bufp, bufp->dcp);
1166 #endif /* DISKCACHE95 */
1168 #ifdef AFS_FREELANCE_CLIENT
1171 // if getroot then we don't need to make any calls
1172 // just return fake data
1174 if (cm_freelanceEnabled && getroot) {
1175 // setup the fake status
1176 afsStatus.InterfaceVersion = 0x1;
1177 afsStatus.FileType = 0x2;
1178 afsStatus.LinkCount = scp->linkCount;
1179 afsStatus.Length = cm_fakeDirSize;
1180 afsStatus.DataVersion = cm_fakeDirVersion;
1181 afsStatus.Author = 0x1;
1182 afsStatus.Owner = 0x0;
1183 afsStatus.CallerAccess = 0x9;
1184 afsStatus.AnonymousAccess = 0x9;
1185 afsStatus.UnixModeBits = 0x1ff;
1186 afsStatus.ParentVnode = 0x1;
1187 afsStatus.ParentUnique = 0x1;
1188 afsStatus.ResidencyMask = 0;
1189 afsStatus.ClientModTime = FakeFreelanceModTime;
1190 afsStatus.ServerModTime = FakeFreelanceModTime;
1191 afsStatus.Group = 0;
1192 afsStatus.SyncCounter = 0;
1193 afsStatus.dataVersionHigh = 0;
1195 // once we're done setting up the status info,
1196 // we just fill the buffer pages with fakedata
1197 // from cm_FakeRootDir. Extra pages are set to
1200 lock_ObtainMutex(&cm_Freelance_Lock);
1201 t1 = bufp->offset.LowPart;
1202 qdp = biod.bufListEndp;
1204 tbufp = osi_GetQData(qdp);
1205 bufferp=tbufp->datap;
1206 memset(bufferp, 0, buf_bufferSize);
1207 t2 = cm_fakeDirSize - t1;
1208 if (t2>buf_bufferSize) t2=buf_bufferSize;
1210 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1215 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1218 lock_ReleaseMutex(&cm_Freelance_Lock);
1220 // once we're done, we skip over the part of the
1221 // code that does the ACTUAL fetching of data for
1224 goto fetchingcompleted;
1227 #endif /* AFS_FREELANCE_CLIENT */
1229 /* now make the call */
1231 code = cm_Conn(&scp->fid, up, reqp, &connp);
1235 callp = rx_NewCall(connp->callp);
1237 osi_Log3(afsd_logp, "CALL FetchData vp %x, off 0x%x, size 0x%x",
1238 (long) scp, biod.offset.LowPart, biod.length);
1240 code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
1243 /* now copy the data out of the pipe and put it in the buffer */
1244 temp = rx_Read(callp, (char *)&nbytes, 4);
1246 nbytes = ntohl(nbytes);
1247 if (nbytes > biod.length)
1248 code = (callp->error < 0) ? callp->error : -1;
1251 code = (callp->error < 0) ? callp->error : -1;
1254 qdp = biod.bufListEndp;
1256 tbufp = osi_GetQData(qdp);
1257 bufferp = tbufp->datap;
1261 /* fill nbytes of data from the pipe into the pages.
1262 * When we stop, qdp will point at the last page we're
1263 * dealing with, and bufferp will tell us where we
1264 * stopped. We'll need this info below when we clear
1265 * the remainder of the last page out (and potentially
1266 * clear later pages out, if we fetch past EOF).
1269 /* assert that there are still more buffers;
1270 * our check above for nbytes being less than
1271 * biod.length should ensure this.
1273 osi_assert(bufferp != NULL);
1275 /* read rbytes of data */
1276 rbytes = (nbytes > buf_bufferSize? buf_bufferSize : nbytes);
1277 temp = rx_Read(callp, bufferp, rbytes);
1278 if (temp < rbytes) {
1279 code = (callp->error < 0) ? callp->error : -1;
1283 /* allow read-while-fetching.
1284 * if this is the last buffer, clear the
1285 * PREFETCHING flag, so the reader waiting for
1286 * this buffer will start a prefetch.
1288 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1289 lock_ObtainMutex(&scp->mx);
1290 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1291 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1292 osi_Wakeup((long) &scp->flags);
1294 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1296 cm_ClearPrefetchFlag(0, scp, &biod.offset);
1298 lock_ReleaseMutex(&scp->mx);
1300 /* and adjust counters */
1303 /* and move to the next buffer */
1305 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1307 tbufp = osi_GetQData(qdp);
1308 bufferp = tbufp->datap;
1316 /* zero out remainder of last pages, in case we are
1317 * fetching past EOF. We were fetching an integral #
1318 * of pages, but stopped, potentially in the middle of
1319 * a page. Zero the remainder of that page, and then
1320 * all of the rest of the pages.
1323 rbytes = bufferp - tbufp->datap;
1324 /* bytes left to zero */
1325 rbytes = buf_bufferSize - rbytes;
1328 memset(bufferp, 0, rbytes);
1329 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1332 tbufp = osi_GetQData(qdp);
1333 bufferp = tbufp->datap;
1334 /* bytes to clear in this page */
1335 rbytes = buf_bufferSize;
1340 code = EndRXAFS_FetchData(callp, &afsStatus, &callback, &volSync);
1342 osi_Log0(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error");
1343 code = rx_EndCall(callp, code);
1344 if (code == RXKADUNKNOWNKEY)
1345 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
1346 osi_Log0(afsd_logp, "CALL FetchData DONE");
1348 } while (cm_Analyze(connp, up, reqp, &scp->fid, &volSync, NULL, NULL, code));
1351 code = cm_MapRPCError(code, reqp);
1353 lock_ObtainMutex(&scp->mx);
1354 /* we know that no one else has changed the buffer, since we still have
1355 * the fetching flag on the buffers, and we have the scp locked again.
1356 * Copy in the version # into the buffer if we got code 0 back from the
1360 for(qdp = biod.bufListp;
1362 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1363 tbufp = osi_GetQData(qdp);
1364 tbufp->dataVersion = afsStatus.DataVersion;
1367 /* write buffer out to disk cache */
1368 diskcache_Update(tbufp->dcp, tbufp->datap, buf_bufferSize,
1369 tbufp->dataVersion);
1370 #endif /* DISKCACHE95 */
1374 /* release scatter/gather I/O structure (buffers, locks) */
1375 lock_ReleaseMutex(&scp->mx);
1376 cm_ReleaseBIOD(&biod, 0);
1377 lock_ObtainMutex(&scp->mx);
1380 cm_MergeStatus(scp, &afsStatus, &volSync, up, 0);