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;
59 struct rx_connection *rxconnp;
66 cm_bulkIO_t biod; /* bulk IO descriptor */
68 osi_assert(userp != NULL);
70 /* now, the buffer may or may not be filled with good data (buf_GetNew
71 * drops lots of locks, and may indeed return a properly initialized
72 * buffer, although more likely it will just return a new, empty, buffer.
74 scp = cm_FindSCache(fidp);
76 return CM_ERROR_NOSUCHFILE; /* shouldn't happen */
78 cm_AFSFidFromFid(&tfid, fidp);
80 lock_ObtainMutex(&scp->mx);
82 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
84 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
85 lock_ReleaseMutex(&scp->mx);
86 cm_ReleaseSCache(scp);
90 if (biod.length == 0) {
91 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
92 lock_ReleaseMutex(&scp->mx);
93 cm_ReleaseBIOD(&biod, 1); /* should be a NOOP */
94 cm_ReleaseSCache(scp);
98 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
99 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
101 /* prepare the output status for the store */
102 scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
103 cm_StatusFromAttr(&inStatus, scp, NULL);
104 truncPos = scp->length.LowPart;
105 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
106 && scp->truncPos.LowPart < (unsigned long) truncPos)
107 truncPos = scp->truncPos.LowPart;
108 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
110 /* compute how many bytes to write from this buffer */
111 thyper = LargeIntegerSubtract(scp->length, biod.offset);
112 if (LargeIntegerLessThanZero(thyper)) {
113 /* entire buffer is past EOF */
117 /* otherwise write out part of buffer before EOF, but not
118 * more than bufferSize bytes.
120 nbytes = thyper.LowPart;
121 if (nbytes > biod.length)
122 nbytes = biod.length;
125 lock_ReleaseMutex(&scp->mx);
127 /* now we're ready to do the store operation */
129 code = cm_Conn(&scp->fid, userp, reqp, &connp);
133 rxconnp = cm_GetRxConn(connp);
134 callp = rx_NewCall(rxconnp);
135 rx_PutConnection(rxconnp);
137 osi_Log3(afsd_logp, "CALL StoreData scp 0x%x, off 0x%x, size 0x%x",
138 (long) scp, biod.offset.LowPart, nbytes);
140 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
141 biod.offset.LowPart, nbytes, truncPos);
144 /* write the data from the the list of buffers */
148 qdp = biod.bufListEndp;
150 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
151 osi_assert(qdp != NULL);
152 bufp = osi_GetQData(qdp);
153 bufferp = bufp->datap;
155 if (wbytes > cm_data.buf_blockSize)
156 wbytes = cm_data.buf_blockSize;
158 /* write out wbytes of data from bufferp */
159 temp = rx_Write(callp, bufferp, wbytes);
160 if (temp != wbytes) {
161 osi_Log2(afsd_logp, "rx_Write failed %d != %d",temp,wbytes);
165 osi_Log1(afsd_logp, "rx_Write succeeded %d",temp);
168 } /* while more bytes to write */
169 } /* if RPC started successfully */
171 osi_Log1(afsd_logp, "StartRXAFS_StoreData failed (%lX)",code);
174 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
176 osi_Log1(afsd_logp, "EndRXAFS_StoreData failed (%lX)",code);
178 code = rx_EndCall(callp, code);
180 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
181 code = cm_MapRPCError(code, reqp);
184 osi_Log1(afsd_logp, "CALL StoreData FAILURE, code 0x%x", code);
186 osi_Log0(afsd_logp, "CALL StoreData SUCCESS");
188 /* now, clean up our state */
189 lock_ObtainMutex(&scp->mx);
191 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
194 /* now, here's something a little tricky: in AFS 3, a dirty
195 * length can't be directly stored, instead, a dirty chunk is
196 * stored that sets the file's size (by writing and by using
197 * the truncate-first option in the store call).
199 * At this point, we've just finished a store, and so the trunc
200 * pos field is clean. If the file's size at the server is at
201 * least as big as we think it should be, then we turn off the
202 * length dirty bit, since all the other dirty buffers must
203 * precede this one in the file.
205 * The file's desired size shouldn't be smaller than what's
206 * stored at the server now, since we just did the trunc pos
209 * We have to turn off the length dirty bit as soon as we can,
210 * so that we see updates made by other machines.
212 if (outStatus.Length >= scp->length.LowPart)
213 scp->mask &= ~CM_SCACHEMASK_LENGTH;
214 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
216 if (code == CM_ERROR_SPACE)
217 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
218 else if (code == CM_ERROR_QUOTA)
219 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
221 lock_ReleaseMutex(&scp->mx);
222 cm_ReleaseBIOD(&biod, 1);
223 cm_ReleaseSCache(scp);
229 * Truncate the file, by sending a StoreData RPC with zero length.
231 * Called with scp locked. Releases and re-obtains the lock.
233 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
235 AFSFetchStatus outStatus;
236 AFSStoreStatus inStatus;
242 struct rx_call *callp;
243 struct rx_connection *rxconnp;
245 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
246 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
247 CM_SCACHESYNC_STOREDATA_EXCL);
249 /* prepare the output status for the store */
250 inStatus.Mask = AFS_SETMODTIME;
251 inStatus.ClientModTime = scp->clientModTime;
252 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
254 /* calculate truncation position */
255 truncPos = scp->length.LowPart;
256 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
257 && scp->truncPos.LowPart < (unsigned long) truncPos)
258 truncPos = scp->truncPos.LowPart;
259 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
261 lock_ReleaseMutex(&scp->mx);
263 cm_AFSFidFromFid(&tfid, &scp->fid);
265 /* now we're ready to do the store operation */
267 code = cm_Conn(&scp->fid, userp, reqp, &connp);
271 rxconnp = cm_GetRxConn(connp);
272 callp = rx_NewCall(rxconnp);
273 rx_PutConnection(rxconnp);
275 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
279 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
280 code = rx_EndCall(callp, code);
282 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
283 code = cm_MapRPCError(code, reqp);
285 /* now, clean up our state */
286 lock_ObtainMutex(&scp->mx);
288 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
292 * For explanation of handling of CM_SCACHEMASK_LENGTH,
295 if (outStatus.Length >= scp->length.LowPart)
296 scp->mask &= ~CM_SCACHEMASK_LENGTH;
297 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
303 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
305 *bytesReadp = cm_data.buf_blockSize;
307 /* now return a code that means that I/O is done */
311 /* stabilize scache entry, and return with it locked so
314 long cm_BufStabilize(void *parmp, cm_user_t *userp, cm_req_t *reqp)
321 lock_ObtainMutex(&scp->mx);
322 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
323 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
325 lock_ReleaseMutex(&scp->mx);
332 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
333 long cm_BufUnstabilize(void *parmp, cm_user_t *userp)
339 lock_ReleaseMutex(&scp->mx);
341 /* always succeeds */
345 cm_buf_ops_t cm_bufOps = {
352 long cm_ValidateDCache(void)
354 return buf_ValidateBuffers();
357 long cm_ShutdownDCache(void)
362 int cm_InitDCache(int newFile, long chunkSize, long nbuffers)
364 lock_InitializeMutex(&cm_bufGetMutex, "buf_Get mutex");
365 return buf_Init(newFile, &cm_bufOps, nbuffers);
368 /* check to see if we have an up-to-date buffer. The buffer must have
369 * previously been obtained by calling buf_Get.
371 * Make sure we have a callback, and that the dataversion matches.
373 * Scp must be locked.
375 * Bufp *may* be locked.
377 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
380 if (!cm_HaveCallback(scp))
382 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
384 if (bufp->dataVersion == scp->dataVersion)
387 code = lock_TryMutex(&bufp->mx);
389 /* don't have the lock, and can't lock it, then
396 /* remember dirty flag for later */
397 code = bufp->flags & CM_BUF_DIRTY;
399 /* release lock if we obtained it here */
401 lock_ReleaseMutex(&bufp->mx);
403 /* if buffer was dirty, buffer is acceptable for use */
410 /* used when deciding whether to do a prefetch or not */
411 long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, long length,
412 cm_user_t *up, cm_req_t *reqp, osi_hyper_t *realBasep)
420 /* now scan all buffers in the range, looking for any that look like
425 lock_ObtainMutex(&scp->mx);
427 /* get callback so we can do a meaningful dataVersion comparison */
428 code = cm_SyncOp(scp, NULL, up, reqp, 0,
429 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
431 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
432 lock_ReleaseMutex(&scp->mx);
436 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
437 /* we're past the end of file */
441 bp = buf_Find(scp, &tbase);
442 /* We cheat slightly by not locking the bp mutex. */
444 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
445 && bp->dataVersion != scp->dataVersion)
452 /* if this buffer is essentially guaranteed to require a fetch,
453 * break out here and return this position.
458 toffset.LowPart = cm_data.buf_blockSize;
459 toffset.HighPart = 0;
460 tbase = LargeIntegerAdd(toffset, tbase);
461 length -= cm_data.buf_blockSize;
464 /* if we get here, either everything is fine or stop stopped us at a
465 * particular buffer in the range that definitely needs to be fetched.
468 /* return non-zero code since realBasep won't be valid */
469 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
473 /* successfully found a page that will need fetching */
477 lock_ReleaseMutex(&scp->mx);
481 void cm_BkgStore(cm_scache_t *scp, long p1, long p2, long p3, long p4,
489 req.flags |= CM_REQ_NORETRY;
491 toffset.LowPart = p1;
492 toffset.HighPart = p2;
495 osi_Log2(afsd_logp, "Starting BKG store vp 0x%x, base 0x%x", scp, p1);
497 cm_BufWrite(&scp->fid, &toffset, length, /* flags */ 0, userp, &req);
499 lock_ObtainMutex(&scp->mx);
500 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
501 lock_ReleaseMutex(&scp->mx);
504 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base)
509 thyper.LowPart = cm_chunkSize;
511 thyper = LargeIntegerAdd(*base, thyper);
512 thyper.LowPart &= (-cm_chunkSize);
513 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
514 scp->prefetch.base = *base;
515 if (LargeIntegerGreaterThan(thyper, scp->prefetch.end))
516 scp->prefetch.end = thyper;
518 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
521 /* do the prefetch */
522 void cm_BkgPrefetch(cm_scache_t *scp, long p1, long p2, long p3, long p4,
529 int cpff = 0; /* cleared prefetch flag */
533 req.flags |= CM_REQ_NORETRY;
539 osi_Log2(afsd_logp, "Starting BKG prefetch vp 0x%x, base 0x%x", scp, p1);
541 code = buf_Get(scp, &base, &bp);
543 lock_ObtainMutex(&scp->mx);
545 if (code || (bp->cmFlags & CM_BUF_CMFETCHING)) {
546 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
547 lock_ReleaseMutex(&scp->mx);
551 code = cm_GetBuffer(scp, bp, &cpff, userp, &req);
553 cm_ClearPrefetchFlag(code, scp, &base);
554 lock_ReleaseMutex(&scp->mx);
559 /* a read was issued to offsetp, and we have to determine whether we should
562 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp,
563 cm_user_t *userp, cm_req_t *reqp)
566 osi_hyper_t realBase;
567 osi_hyper_t readBase;
570 /* round up to chunk boundary */
571 readBase.LowPart += (cm_chunkSize-1);
572 readBase.LowPart &= (-cm_chunkSize);
574 lock_ObtainMutex(&scp->mx);
575 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
576 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
577 lock_ReleaseMutex(&scp->mx);
580 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
582 /* start the scan at the latter of the end of this read or
583 * the end of the last fetched region.
585 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
586 readBase = scp->prefetch.end;
588 lock_ReleaseMutex(&scp->mx);
590 code = cm_CheckFetchRange(scp, &readBase, cm_chunkSize, userp, reqp,
593 return; /* can't find something to prefetch */
595 osi_Log2(afsd_logp, "BKG Prefetch request vp 0x%x, base 0x%x",
596 scp, realBase.LowPart);
598 cm_QueueBKGRequest(scp, cm_BkgPrefetch, realBase.LowPart,
599 realBase.HighPart, cm_chunkSize, 0, userp);
602 /* scp must be locked; temporarily unlocked during processing.
603 * If returns 0, returns buffers held in biop, and with
604 * CM_BUF_CMSTORING set.
606 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
607 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
608 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
609 * must be woken, and the event must be set when the I/O is done. All of this
610 * is required so that buf_WaitIO synchronizes properly with the buffer as it
611 * is being written out.
613 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
614 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
617 osi_queueData_t *qdp;
620 osi_hyper_t scanStart; /* where to start scan for dirty pages */
621 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
622 osi_hyper_t firstModOffset; /* offset of first modified page in range */
625 long flags; /* flags to cm_SyncOp */
627 /* clear things out */
628 biop->scp = scp; /* don't hold */
629 biop->offset = *inOffsetp;
631 biop->bufListp = NULL;
632 biop->bufListEndp = NULL;
635 /* reserve a chunk's worth of buffers */
636 lock_ReleaseMutex(&scp->mx);
637 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
638 lock_ObtainMutex(&scp->mx);
641 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize, bufp = NULL) {
643 thyper.LowPart = temp;
644 tbase = LargeIntegerAdd(*inOffsetp, thyper);
646 bufp = buf_Find(scp, &tbase);
648 /* get buffer mutex and scp mutex safely */
649 lock_ReleaseMutex(&scp->mx);
650 lock_ObtainMutex(&bufp->mx);
651 lock_ObtainMutex(&scp->mx);
653 flags = CM_SCACHESYNC_NEEDCALLBACK
654 | CM_SCACHESYNC_GETSTATUS
655 | CM_SCACHESYNC_STOREDATA
656 | CM_SCACHESYNC_BUFLOCKED;
657 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
659 lock_ReleaseMutex(&bufp->mx);
661 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
665 /* if the buffer is dirty, we're done */
666 if (bufp->flags & CM_BUF_DIRTY) {
667 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
668 "WRITING w/o CMSTORING in SetupStoreBIOD");
669 bufp->flags |= CM_BUF_WRITING;
673 /* this buffer is clean, so there's no reason to process it */
674 cm_SyncOpDone(scp, bufp, flags);
675 lock_ReleaseMutex(&bufp->mx);
682 /* if we get here, if bufp is null, we didn't find any dirty buffers
683 * that weren't already being stored back, so we just quit now.
689 /* don't need buffer mutex any more */
690 lock_ReleaseMutex(&bufp->mx);
692 /* put this element in the list */
694 osi_SetQData(qdp, bufp);
695 /* don't have to hold bufp, since held by buf_Find above */
696 osi_QAddH((osi_queue_t **) &biop->bufListp,
697 (osi_queue_t **) &biop->bufListEndp,
699 biop->length = cm_data.buf_blockSize;
700 firstModOffset = bufp->offset;
701 biop->offset = firstModOffset;
703 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
704 scanStart = *inOffsetp;
705 scanStart.LowPart &= (-cm_chunkSize);
706 thyper.LowPart = cm_chunkSize;
708 scanEnd = LargeIntegerAdd(scanStart, thyper);
710 flags = CM_SCACHESYNC_NEEDCALLBACK
711 | CM_SCACHESYNC_GETSTATUS
712 | CM_SCACHESYNC_STOREDATA
713 | CM_SCACHESYNC_BUFLOCKED
714 | CM_SCACHESYNC_NOWAIT;
716 /* start by looking backwards until scanStart */
717 thyper.HighPart = 0; /* hyper version of cm_data.buf_blockSize */
718 thyper.LowPart = cm_data.buf_blockSize;
719 tbase = LargeIntegerSubtract(firstModOffset, thyper);
720 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
721 /* see if we can find the buffer */
722 bufp = buf_Find(scp, &tbase);
726 /* try to lock it, and quit if we can't (simplifies locking) */
727 lock_ReleaseMutex(&scp->mx);
728 code = lock_TryMutex(&bufp->mx);
729 lock_ObtainMutex(&scp->mx);
735 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
737 lock_ReleaseMutex(&bufp->mx);
742 if (!(bufp->flags & CM_BUF_DIRTY)) {
743 /* buffer is clean, so we shouldn't add it */
744 cm_SyncOpDone(scp, bufp, flags);
745 lock_ReleaseMutex(&bufp->mx);
750 /* don't need buffer mutex any more */
751 lock_ReleaseMutex(&bufp->mx);
753 /* we have a dirty buffer ready for storing. Add it to the tail
754 * of the list, since it immediately precedes all of the disk
755 * addresses we've already collected.
758 osi_SetQData(qdp, bufp);
759 /* no buf_hold necessary, since we have it held from buf_Find */
760 osi_QAddT((osi_queue_t **) &biop->bufListp,
761 (osi_queue_t **) &biop->bufListEndp,
764 /* update biod info describing the transfer */
765 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
766 biop->length += cm_data.buf_blockSize;
768 /* update loop pointer */
769 tbase = LargeIntegerSubtract(tbase, thyper);
770 } /* while loop looking for pages preceding the one we found */
772 /* now, find later dirty, contiguous pages, and add them to the list */
773 thyper.HighPart = 0; /* hyper version of cm_data.buf_blockSize */
774 thyper.LowPart = cm_data.buf_blockSize;
775 tbase = LargeIntegerAdd(firstModOffset, thyper);
776 while(LargeIntegerLessThan(tbase, scanEnd)) {
777 /* see if we can find the buffer */
778 bufp = buf_Find(scp, &tbase);
782 /* try to lock it, and quit if we can't (simplifies locking) */
783 lock_ReleaseMutex(&scp->mx);
784 code = lock_TryMutex(&bufp->mx);
785 lock_ObtainMutex(&scp->mx);
791 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
793 lock_ReleaseMutex(&bufp->mx);
798 if (!(bufp->flags & CM_BUF_DIRTY)) {
799 /* buffer is clean, so we shouldn't add it */
800 cm_SyncOpDone(scp, bufp, flags);
801 lock_ReleaseMutex(&bufp->mx);
806 /* don't need buffer mutex any more */
807 lock_ReleaseMutex(&bufp->mx);
809 /* we have a dirty buffer ready for storing. Add it to the head
810 * of the list, since it immediately follows all of the disk
811 * addresses we've already collected.
814 osi_SetQData(qdp, bufp);
815 /* no buf_hold necessary, since we have it held from buf_Find */
816 osi_QAddH((osi_queue_t **) &biop->bufListp,
817 (osi_queue_t **) &biop->bufListEndp,
820 /* update biod info describing the transfer */
821 biop->length += cm_data.buf_blockSize;
823 /* update loop pointer */
824 tbase = LargeIntegerAdd(tbase, thyper);
825 } /* while loop looking for pages following the first page we found */
827 /* finally, we're done */
831 /* scp must be locked; temporarily unlocked during processing.
832 * If returns 0, returns buffers held in biop, and with
833 * CM_BUF_CMFETCHING flags set.
834 * If an error is returned, we don't return any buffers.
836 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
837 cm_bulkIO_t *biop, cm_user_t *up, cm_req_t *reqp)
841 osi_hyper_t toffset; /* a long long temp variable */
842 osi_hyper_t pageBase; /* base offset we're looking at */
843 osi_queueData_t *qdp; /* one temp queue structure */
844 osi_queueData_t *tqdp; /* another temp queue structure */
845 long collected; /* how many bytes have been collected */
848 osi_hyper_t fileSize; /* the # of bytes in the file */
849 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
850 osi_queueData_t *heldBufListEndp; /* first one */
854 biop->offset = *offsetp;
855 /* null out the list of buffers */
856 biop->bufListp = biop->bufListEndp = NULL;
859 /* first lookup the file's length, so we know when to stop */
860 code = cm_SyncOp(scp, NULL, up, reqp, 0,
861 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
865 /* copy out size, since it may change */
866 fileSize = scp->serverLength;
868 lock_ReleaseMutex(&scp->mx);
871 collected = pageBase.LowPart & (cm_chunkSize - 1);
873 heldBufListEndp = NULL;
876 * Obtaining buffers can cause dirty buffers to be recycled, which
877 * can cause a storeback, so cannot be done while we have buffers
880 * To get around this, we get buffers twice. Before reserving buffers,
881 * we obtain and release each one individually. After reserving
882 * buffers, we try to obtain them again, but only by lookup, not by
883 * recycling. If a buffer has gone away while we were waiting for
884 * the others, we just use whatever buffers we already have.
886 * On entry to this function, we are already holding a buffer, so we
887 * can't wait for reservation. So we call buf_TryReserveBuffers()
888 * instead. Not only that, we can't really even call buf_Get(), for
889 * the same reason. We can't avoid that, though. To avoid deadlock
890 * we allow only one thread to be executing the buf_Get()-buf_Release()
891 * sequence at a time.
894 // lock_ObtainMutex(&cm_bufGetMutex);
895 /* first hold all buffers, since we can't hold any locks in buf_Get */
897 /* stop at chunk boundary */
898 if (collected >= cm_chunkSize)
901 /* see if the next page would be past EOF */
902 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
905 code = buf_Get(scp, &pageBase, &tbp);
907 //lock_ReleaseMutex(&cm_bufGetMutex);
908 lock_ObtainMutex(&scp->mx);
909 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
915 toffset.HighPart = 0;
916 toffset.LowPart = cm_data.buf_blockSize;
917 pageBase = LargeIntegerAdd(toffset, pageBase);
918 collected += cm_data.buf_blockSize;
921 /* reserve a chunk's worth of buffers if possible */
922 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
924 // lock_ReleaseMutex(&cm_bufGetMutex);
927 collected = pageBase.LowPart & (cm_chunkSize - 1);
929 /* now hold all buffers, if they are still there */
931 /* stop at chunk boundary */
932 if (collected >= cm_chunkSize)
935 /* see if the next page would be past EOF */
936 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
939 tbp = buf_Find(scp, &pageBase);
943 /* add the buffer to the list */
945 osi_SetQData(qdp, tbp);
946 osi_QAdd((osi_queue_t **)&heldBufListp, &qdp->q);
947 if (!heldBufListEndp) heldBufListEndp = qdp;
948 /* leave tbp held (from buf_Get) */
953 collected += cm_data.buf_blockSize;
954 toffset.HighPart = 0;
955 toffset.LowPart = cm_data.buf_blockSize;
956 pageBase = LargeIntegerAdd(toffset, pageBase);
959 /* look at each buffer, adding it into the list if it looks idle and
960 * filled with old data. One special case: wait for idle if it is the
961 * first buffer since we really need that one for our caller to make
965 collected = 0; /* now count how many we'll really use */
966 for (tqdp = heldBufListEndp;
968 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
969 /* get a ptr to the held buffer */
970 tbp = osi_GetQData(tqdp);
971 pageBase = tbp->offset;
973 /* now lock the buffer lock */
974 lock_ObtainMutex(&tbp->mx);
975 lock_ObtainMutex(&scp->mx);
977 /* don't bother fetching over data that is already current */
978 if (tbp->dataVersion == scp->dataVersion) {
979 /* we don't need this buffer, since it is current */
980 lock_ReleaseMutex(&scp->mx);
981 lock_ReleaseMutex(&tbp->mx);
985 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_FETCHDATA
986 | CM_SCACHESYNC_BUFLOCKED;
988 flags |= CM_SCACHESYNC_NOWAIT;
990 /* wait for the buffer to serialize, if required. Doesn't
991 * release the scp or buffer lock(s) if NOWAIT is specified.
993 code = cm_SyncOp(scp, tbp, up, reqp, 0, flags);
995 lock_ReleaseMutex(&scp->mx);
996 lock_ReleaseMutex(&tbp->mx);
1000 /* don't fetch over dirty buffers */
1001 if (tbp->flags & CM_BUF_DIRTY) {
1002 cm_SyncOpDone(scp, tbp, flags);
1003 lock_ReleaseMutex(&scp->mx);
1004 lock_ReleaseMutex(&tbp->mx);
1009 lock_ReleaseMutex(&scp->mx);
1010 lock_ReleaseMutex(&tbp->mx);
1012 /* add the buffer to the list */
1013 qdp = osi_QDAlloc();
1014 osi_SetQData(qdp, tbp);
1015 osi_QAdd((osi_queue_t **)&biop->bufListp, &qdp->q);
1016 if (!biop->bufListEndp)
1017 biop->bufListEndp = qdp;
1020 /* from now on, a failure just stops our collection process, but
1021 * we still do the I/O to whatever we've already managed to collect.
1024 collected += cm_data.buf_blockSize;
1027 /* now, we've held in biop->bufListp all the buffer's we're really
1028 * interested in. We also have holds left from heldBufListp, and we
1029 * now release those holds on the buffers.
1031 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1032 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1033 tbp = osi_GetQData(qdp);
1038 /* Caller expects this */
1039 lock_ObtainMutex(&scp->mx);
1041 /* if we got a failure setting up the first buffer, then we don't have
1042 * any side effects yet, and we also have failed an operation that the
1043 * caller requires to make any progress. Give up now.
1045 if (code && isFirst) {
1046 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1050 /* otherwise, we're still OK, and should just return the I/O setup we've
1053 biop->length = collected;
1054 biop->reserved = reserving;
1058 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1061 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore)
1065 osi_queueData_t *qdp;
1066 osi_queueData_t *nqdp;
1069 /* Give back reserved buffers */
1071 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1073 flags = CM_SCACHESYNC_NEEDCALLBACK;
1075 flags |= CM_SCACHESYNC_STOREDATA;
1077 flags |= CM_SCACHESYNC_FETCHDATA;
1080 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1081 /* lookup next guy first, since we're going to free this one */
1082 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1084 /* extract buffer and free queue data */
1085 bufp = osi_GetQData(qdp);
1088 /* now, mark I/O as done, unlock the buffer and release it */
1089 lock_ObtainMutex(&bufp->mx);
1090 lock_ObtainMutex(&scp->mx);
1091 cm_SyncOpDone(scp, bufp, flags);
1092 lock_ReleaseMutex(&scp->mx);
1094 /* turn off writing and wakeup users */
1096 if (bufp->flags & CM_BUF_WAITING) {
1097 osi_Log1(afsd_logp, "cm_ReleaseBIOD Waking bp 0x%x", bufp);
1098 osi_Wakeup((long) bufp);
1100 bufp->flags &= ~(CM_BUF_WRITING | CM_BUF_DIRTY);
1103 lock_ReleaseMutex(&bufp->mx);
1107 /* clean things out */
1108 biop->bufListp = NULL;
1109 biop->bufListEndp = NULL;
1112 /* Fetch a buffer. Called with scp locked.
1113 * The scp is locked on return.
1115 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *up,
1119 long nbytes; /* bytes in transfer */
1120 long rbytes; /* bytes in rx_Read call */
1122 AFSFetchStatus afsStatus;
1123 AFSCallBack callback;
1126 cm_buf_t *tbufp; /* buf we're filling */
1127 osi_queueData_t *qdp; /* q element we're scanning */
1129 struct rx_call *callp;
1130 struct rx_connection *rxconnp;
1131 cm_bulkIO_t biod; /* bulk IO descriptor */
1136 /* now, the buffer may or may not be filled with good data (buf_GetNew
1137 * drops lots of locks, and may indeed return a properly initialized
1138 * buffer, although more likely it will just return a new, empty, buffer.
1141 #ifdef AFS_FREELANCE_CLIENT
1143 // yj: if they're trying to get the /afs directory, we need to
1144 // handle it differently, since it's local rather than on any
1147 getroot = (scp==cm_data.rootSCachep);
1149 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1152 cm_AFSFidFromFid(&tfid, &scp->fid);
1154 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, up, reqp);
1156 /* couldn't even get the first page setup properly */
1157 osi_Log1(afsd_logp, "SetupFetchBIOD failure code %d", code);
1161 /* once we get here, we have the callback in place, we know that no one
1162 * is fetching the data now. Check one last time that we still have
1163 * the wrong data, and then fetch it if we're still wrong.
1165 * We can lose a race condition and end up with biod.length zero, in
1166 * which case we just retry.
1168 if (bufp->dataVersion == scp->dataVersion || biod.length == 0) {
1169 osi_Log3(afsd_logp, "Bad DVs %d, %d or length 0x%x",
1170 bufp->dataVersion, scp->dataVersion, biod.length);
1171 if ((bufp->dataVersion == -1
1172 || bufp->dataVersion < scp->dataVersion)
1173 && LargeIntegerGreaterThanOrEqualTo(bufp->offset,
1174 scp->serverLength)) {
1175 if (bufp->dataVersion == -1)
1176 memset(bufp->datap, 0, cm_data.buf_blockSize);
1177 bufp->dataVersion = scp->dataVersion;
1179 lock_ReleaseMutex(&scp->mx);
1180 cm_ReleaseBIOD(&biod, 0);
1181 lock_ObtainMutex(&scp->mx);
1185 lock_ReleaseMutex(&scp->mx);
1188 DPRINTF("cm_GetBuffer: fetching data scpDV=%d bufDV=%d scp=%x bp=%x dcp=%x\n",
1189 scp->dataVersion, bufp->dataVersion, scp, bufp, bufp->dcp);
1190 #endif /* DISKCACHE95 */
1192 #ifdef AFS_FREELANCE_CLIENT
1195 // if getroot then we don't need to make any calls
1196 // just return fake data
1198 if (cm_freelanceEnabled && getroot) {
1199 // setup the fake status
1200 afsStatus.InterfaceVersion = 0x1;
1201 afsStatus.FileType = 0x2;
1202 afsStatus.LinkCount = scp->linkCount;
1203 afsStatus.Length = cm_fakeDirSize;
1204 afsStatus.DataVersion = cm_data.fakeDirVersion;
1205 afsStatus.Author = 0x1;
1206 afsStatus.Owner = 0x0;
1207 afsStatus.CallerAccess = 0x9;
1208 afsStatus.AnonymousAccess = 0x9;
1209 afsStatus.UnixModeBits = 0x1ff;
1210 afsStatus.ParentVnode = 0x1;
1211 afsStatus.ParentUnique = 0x1;
1212 afsStatus.ResidencyMask = 0;
1213 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1214 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1215 afsStatus.Group = 0;
1216 afsStatus.SyncCounter = 0;
1217 afsStatus.dataVersionHigh = 0;
1218 afsStatus.lockCount = 0;
1219 afsStatus.Length_hi = 0;
1220 afsStatus.errorCode = 0;
1222 // once we're done setting up the status info,
1223 // we just fill the buffer pages with fakedata
1224 // from cm_FakeRootDir. Extra pages are set to
1227 lock_ObtainMutex(&cm_Freelance_Lock);
1228 t1 = bufp->offset.LowPart;
1229 qdp = biod.bufListEndp;
1231 tbufp = osi_GetQData(qdp);
1232 bufferp=tbufp->datap;
1233 memset(bufferp, 0, cm_data.buf_blockSize);
1234 t2 = cm_fakeDirSize - t1;
1235 if (t2>cm_data.buf_blockSize) t2=cm_data.buf_blockSize;
1237 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1242 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1245 lock_ReleaseMutex(&cm_Freelance_Lock);
1247 // once we're done, we skip over the part of the
1248 // code that does the ACTUAL fetching of data for
1251 goto fetchingcompleted;
1254 #endif /* AFS_FREELANCE_CLIENT */
1256 /* now make the call */
1258 code = cm_Conn(&scp->fid, up, reqp, &connp);
1262 rxconnp = cm_GetRxConn(connp);
1263 callp = rx_NewCall(rxconnp);
1264 rx_PutConnection(rxconnp);
1266 osi_Log3(afsd_logp, "CALL FetchData vp %x, off 0x%x, size 0x%x",
1267 (long) scp, biod.offset.LowPart, biod.length);
1269 code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
1272 /* now copy the data out of the pipe and put it in the buffer */
1273 temp = rx_Read(callp, (char *)&nbytes, 4);
1275 nbytes = ntohl(nbytes);
1276 if (nbytes > biod.length)
1277 code = (callp->error < 0) ? callp->error : -1;
1280 code = (callp->error < 0) ? callp->error : -1;
1283 qdp = biod.bufListEndp;
1285 tbufp = osi_GetQData(qdp);
1286 bufferp = tbufp->datap;
1290 /* fill nbytes of data from the pipe into the pages.
1291 * When we stop, qdp will point at the last page we're
1292 * dealing with, and bufferp will tell us where we
1293 * stopped. We'll need this info below when we clear
1294 * the remainder of the last page out (and potentially
1295 * clear later pages out, if we fetch past EOF).
1297 while (nbytes > 0) {
1298 /* assert that there are still more buffers;
1299 * our check above for nbytes being less than
1300 * biod.length should ensure this.
1302 osi_assert(bufferp != NULL);
1304 /* read rbytes of data */
1305 rbytes = (nbytes > cm_data.buf_blockSize? cm_data.buf_blockSize : nbytes);
1306 temp = rx_Read(callp, bufferp, rbytes);
1307 if (temp < rbytes) {
1308 code = (callp->error < 0) ? callp->error : -1;
1312 /* allow read-while-fetching.
1313 * if this is the last buffer, clear the
1314 * PREFETCHING flag, so the reader waiting for
1315 * this buffer will start a prefetch.
1317 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1318 lock_ObtainMutex(&scp->mx);
1319 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1320 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%x", scp);
1321 osi_Wakeup((long) &scp->flags);
1323 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1325 cm_ClearPrefetchFlag(0, scp, &biod.offset);
1327 lock_ReleaseMutex(&scp->mx);
1329 /* and adjust counters */
1332 /* and move to the next buffer */
1334 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1336 tbufp = osi_GetQData(qdp);
1337 bufferp = tbufp->datap;
1345 /* zero out remainder of last pages, in case we are
1346 * fetching past EOF. We were fetching an integral #
1347 * of pages, but stopped, potentially in the middle of
1348 * a page. Zero the remainder of that page, and then
1349 * all of the rest of the pages.
1352 rbytes = bufferp - tbufp->datap;
1353 /* bytes left to zero */
1354 rbytes = cm_data.buf_blockSize - rbytes;
1357 memset(bufferp, 0, rbytes);
1358 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1361 tbufp = osi_GetQData(qdp);
1362 bufferp = tbufp->datap;
1363 /* bytes to clear in this page */
1364 rbytes = cm_data.buf_blockSize;
1369 code = EndRXAFS_FetchData(callp, &afsStatus, &callback, &volSync);
1371 osi_Log0(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error");
1372 code = rx_EndCall(callp, code);
1373 if (code == RXKADUNKNOWNKEY)
1374 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
1375 osi_Log0(afsd_logp, "CALL FetchData DONE");
1377 } while (cm_Analyze(connp, up, reqp, &scp->fid, &volSync, NULL, NULL, code));
1380 code = cm_MapRPCError(code, reqp);
1382 lock_ObtainMutex(&scp->mx);
1384 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1386 /* we know that no one else has changed the buffer, since we still have
1387 * the fetching flag on the buffers, and we have the scp locked again.
1388 * Copy in the version # into the buffer if we got code 0 back from the
1392 for(qdp = biod.bufListp;
1394 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1395 tbufp = osi_GetQData(qdp);
1396 tbufp->dataVersion = afsStatus.DataVersion;
1399 /* write buffer out to disk cache */
1400 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
1401 tbufp->dataVersion);
1402 #endif /* DISKCACHE95 */
1406 /* release scatter/gather I/O structure (buffers, locks) */
1407 lock_ReleaseMutex(&scp->mx);
1408 cm_ReleaseBIOD(&biod, 0);
1409 lock_ObtainMutex(&scp->mx);
1412 cm_MergeStatus(scp, &afsStatus, &volSync, up, 0);