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 lock_ObtainMutex(&connp->mx);
138 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
139 biod.offset.LowPart, nbytes, truncPos);
142 /* write the data from the the list of buffers */
146 qdp = biod.bufListEndp;
148 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
149 osi_assert(qdp != NULL);
150 bufp = osi_GetQData(qdp);
151 bufferp = bufp->datap;
153 if (wbytes > buf_bufferSize)
154 wbytes = buf_bufferSize;
156 /* write out wbytes of data from bufferp */
157 temp = rx_Write(callp, bufferp, wbytes);
158 if (temp != wbytes) {
159 osi_Log2(afsd_logp, "rx_Write failed %d != %d",temp,wbytes);
163 osi_Log1(afsd_logp, "rx_Write succeeded %d",temp);
166 } /* while more bytes to write */
167 } /* if RPC started successfully */
169 osi_Log1(afsd_logp, "StartRXAFS_StoreData failed (%lX)",code);
172 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
174 osi_Log1(afsd_logp, "EndRXAFS_StoreData failed (%lX)",code);
176 code = rx_EndCall(callp, code);
177 lock_ReleaseMutex(&connp->mx);
179 osi_Log0(afsd_logp, "CALL StoreData DONE");
181 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
182 code = cm_MapRPCError(code, reqp);
184 /* now, clean up our state */
185 lock_ObtainMutex(&scp->mx);
187 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
190 /* now, here's something a little tricky: in AFS 3, a dirty
191 * length can't be directly stored, instead, a dirty chunk is
192 * stored that sets the file's size (by writing and by using
193 * the truncate-first option in the store call).
195 * At this point, we've just finished a store, and so the trunc
196 * pos field is clean. If the file's size at the server is at
197 * least as big as we think it should be, then we turn off the
198 * length dirty bit, since all the other dirty buffers must
199 * precede this one in the file.
201 * The file's desired size shouldn't be smaller than what's
202 * stored at the server now, since we just did the trunc pos
205 * We have to turn off the length dirty bit as soon as we can,
206 * so that we see updates made by other machines.
208 if (outStatus.Length >= scp->length.LowPart)
209 scp->mask &= ~CM_SCACHEMASK_LENGTH;
210 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
212 if (code == CM_ERROR_SPACE)
213 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
214 else if (code == CM_ERROR_QUOTA)
215 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
217 lock_ReleaseMutex(&scp->mx);
218 cm_ReleaseBIOD(&biod, 1);
219 cm_ReleaseSCache(scp);
225 * Truncate the file, by sending a StoreData RPC with zero length.
227 * Called with scp locked. Releases and re-obtains the lock.
229 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
231 AFSFetchStatus outStatus;
232 AFSStoreStatus inStatus;
238 struct rx_call *callp;
240 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
241 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
242 CM_SCACHESYNC_STOREDATA_EXCL);
244 /* prepare the output status for the store */
245 inStatus.Mask = AFS_SETMODTIME;
246 inStatus.ClientModTime = scp->clientModTime;
247 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
249 /* calculate truncation position */
250 truncPos = scp->length.LowPart;
251 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
252 && scp->truncPos.LowPart < (unsigned long) truncPos)
253 truncPos = scp->truncPos.LowPart;
254 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
256 lock_ReleaseMutex(&scp->mx);
258 cm_AFSFidFromFid(&tfid, &scp->fid);
260 /* now we're ready to do the store operation */
262 code = cm_Conn(&scp->fid, userp, reqp, &connp);
266 callp = rx_NewCall(connp->callp);
268 lock_ObtainMutex(&connp->mx);
269 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
273 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
274 code = rx_EndCall(callp, code);
276 lock_ReleaseMutex(&connp->mx);
278 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
279 code = cm_MapRPCError(code, reqp);
281 /* now, clean up our state */
282 lock_ObtainMutex(&scp->mx);
284 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
288 * For explanation of handling of CM_SCACHEMASK_LENGTH,
291 if (outStatus.Length >= scp->length.LowPart)
292 scp->mask &= ~CM_SCACHEMASK_LENGTH;
293 cm_MergeStatus(scp, &outStatus, &volSync, userp, 0);
299 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
301 *bytesReadp = buf_bufferSize;
303 /* now return a code that means that I/O is done */
307 /* stabilize scache entry, and return with it locked so
310 long cm_BufStabilize(void *parmp, cm_user_t *userp, cm_req_t *reqp)
317 lock_ObtainMutex(&scp->mx);
318 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
319 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
321 lock_ReleaseMutex(&scp->mx);
328 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
329 long cm_BufUnstabilize(void *parmp, cm_user_t *userp)
335 lock_ReleaseMutex(&scp->mx);
337 /* always succeeds */
341 cm_buf_ops_t cm_bufOps = {
348 int cm_InitDCache(long chunkSize, long nbuffers)
350 lock_InitializeMutex(&cm_bufGetMutex, "buf_Get mutex");
352 buf_nbuffers = nbuffers;
353 return buf_Init(&cm_bufOps);
356 /* check to see if we have an up-to-date buffer. The buffer must have
357 * previously been obtained by calling buf_Get.
359 * Make sure we have a callback, and that the dataversion matches.
361 * Scp must be locked.
363 * Bufp *may* be locked.
365 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
368 if (!cm_HaveCallback(scp))
371 & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
372 == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
374 if (bufp->dataVersion == scp->dataVersion)
377 code = lock_TryMutex(&bufp->mx);
379 /* don't have the lock, and can't lock it, then
386 /* remember dirty flag for later */
387 code = bufp->flags & CM_BUF_DIRTY;
389 /* release lock if we obtained it here */
391 lock_ReleaseMutex(&bufp->mx);
393 /* if buffer was dirty, buffer is acceptable for use */
400 /* used when deciding whether to do a prefetch or not */
401 long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, long length,
402 cm_user_t *up, cm_req_t *reqp, osi_hyper_t *realBasep)
410 /* now scan all buffers in the range, looking for any that look like
415 lock_ObtainMutex(&scp->mx);
417 /* get callback so we can do a meaningful dataVersion comparison */
418 code = cm_SyncOp(scp, NULL, up, reqp, 0,
419 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
421 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
422 lock_ReleaseMutex(&scp->mx);
426 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
427 /* we're past the end of file */
431 bp = buf_Find(scp, &tbase);
432 /* We cheat slightly by not locking the bp mutex. */
435 & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
436 && bp->dataVersion != scp->dataVersion)
443 /* if this buffer is essentially guaranteed to require a fetch,
444 * break out here and return this position.
449 toffset.LowPart = buf_bufferSize;
450 toffset.HighPart = 0;
451 tbase = LargeIntegerAdd(toffset, tbase);
452 length -= buf_bufferSize;
455 /* if we get here, either everything is fine or stop stopped us at a
456 * particular buffer in the range that definitely needs to be fetched.
459 /* return non-zero code since realBasep won't be valid */
460 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
464 /* successfully found a page that will need fetching */
468 lock_ReleaseMutex(&scp->mx);
472 void cm_BkgStore(cm_scache_t *scp, long p1, long p2, long p3, long p4,
480 req.flags |= CM_REQ_NORETRY;
482 toffset.LowPart = p1;
483 toffset.HighPart = p2;
486 osi_Log2(afsd_logp, "Starting BKG store vp 0x%x, base 0x%x", scp, p1);
488 cm_BufWrite(&scp->fid, &toffset, length, /* flags */ 0, userp, &req);
490 lock_ObtainMutex(&scp->mx);
491 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
492 lock_ReleaseMutex(&scp->mx);
495 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base)
500 thyper.LowPart = cm_chunkSize;
502 thyper = LargeIntegerAdd(*base, thyper);
503 thyper.LowPart &= (-cm_chunkSize);
504 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
505 scp->prefetch.base = *base;
506 if (LargeIntegerGreaterThan(thyper, scp->prefetch.end))
507 scp->prefetch.end = thyper;
509 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
512 /* do the prefetch */
513 void cm_BkgPrefetch(cm_scache_t *scp, long p1, long p2, long p3, long p4,
520 int cpff = 0; /* cleared prefetch flag */
524 req.flags |= CM_REQ_NORETRY;
530 osi_Log2(afsd_logp, "Starting BKG prefetch vp 0x%x, base 0x%x", scp, p1);
532 code = buf_Get(scp, &base, &bp);
534 lock_ObtainMutex(&scp->mx);
536 if (code || (bp->cmFlags & CM_BUF_CMFETCHING)) {
537 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
538 lock_ReleaseMutex(&scp->mx);
542 code = cm_GetBuffer(scp, bp, &cpff, userp, &req);
544 cm_ClearPrefetchFlag(code, scp, &base);
545 lock_ReleaseMutex(&scp->mx);
550 /* a read was issued to offsetp, and we have to determine whether we should
553 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp,
554 cm_user_t *userp, cm_req_t *reqp)
557 osi_hyper_t realBase;
558 osi_hyper_t readBase;
561 /* round up to chunk boundary */
562 readBase.LowPart += (cm_chunkSize-1);
563 readBase.LowPart &= (-cm_chunkSize);
565 lock_ObtainMutex(&scp->mx);
566 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
567 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
568 lock_ReleaseMutex(&scp->mx);
571 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
573 /* start the scan at the latter of the end of this read or
574 * the end of the last fetched region.
576 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
577 readBase = scp->prefetch.end;
579 lock_ReleaseMutex(&scp->mx);
581 code = cm_CheckFetchRange(scp, &readBase, cm_chunkSize, userp, reqp,
584 return; /* can't find something to prefetch */
586 osi_Log2(afsd_logp, "BKG Prefetch request vp 0x%x, base 0x%x",
587 scp, realBase.LowPart);
589 cm_QueueBKGRequest(scp, cm_BkgPrefetch, realBase.LowPart,
590 realBase.HighPart, cm_chunkSize, 0, userp);
593 /* scp must be locked; temporarily unlocked during processing.
594 * If returns 0, returns buffers held in biop, and with
595 * CM_BUF_CMSTORING set.
597 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
598 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
599 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
600 * must be woken, and the event must be set when the I/O is done. All of this
601 * is required so that buf_WaitIO synchronizes properly with the buffer as it
602 * is being written out.
604 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
605 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
608 osi_queueData_t *qdp;
611 osi_hyper_t scanStart; /* where to start scan for dirty pages */
612 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
613 osi_hyper_t firstModOffset; /* offset of first modified page in range */
616 long flags; /* flags to cm_SyncOp */
618 /* clear things out */
619 biop->scp = scp; /* don't hold */
620 biop->offset = *inOffsetp;
622 biop->bufListp = NULL;
623 biop->bufListEndp = NULL;
626 /* reserve a chunk's worth of buffers */
627 lock_ReleaseMutex(&scp->mx);
628 buf_ReserveBuffers(cm_chunkSize / buf_bufferSize);
629 lock_ObtainMutex(&scp->mx);
632 for (temp = 0; temp < inSize; temp += buf_bufferSize, bufp = NULL) {
634 thyper.LowPart = temp;
635 tbase = LargeIntegerAdd(*inOffsetp, thyper);
637 bufp = buf_Find(scp, &tbase);
639 /* get buffer mutex and scp mutex safely */
640 lock_ReleaseMutex(&scp->mx);
641 lock_ObtainMutex(&bufp->mx);
642 lock_ObtainMutex(&scp->mx);
644 flags = CM_SCACHESYNC_NEEDCALLBACK
645 | CM_SCACHESYNC_GETSTATUS
646 | CM_SCACHESYNC_STOREDATA
647 | CM_SCACHESYNC_BUFLOCKED;
648 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
650 lock_ReleaseMutex(&bufp->mx);
652 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
656 /* if the buffer is dirty, we're done */
657 if (bufp->flags & CM_BUF_DIRTY) {
658 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
659 "WRITING w/o CMSTORING in SetupStoreBIOD");
660 bufp->flags |= CM_BUF_WRITING;
664 /* this buffer is clean, so there's no reason to process it */
665 cm_SyncOpDone(scp, bufp, flags);
666 lock_ReleaseMutex(&bufp->mx);
673 /* if we get here, if bufp is null, we didn't find any dirty buffers
674 * that weren't already being stored back, so we just quit now.
680 /* don't need buffer mutex any more */
681 lock_ReleaseMutex(&bufp->mx);
683 /* put this element in the list */
685 osi_SetQData(qdp, bufp);
686 /* don't have to hold bufp, since held by buf_Find above */
687 osi_QAddH((osi_queue_t **) &biop->bufListp,
688 (osi_queue_t **) &biop->bufListEndp,
690 biop->length = buf_bufferSize;
691 firstModOffset = bufp->offset;
692 biop->offset = firstModOffset;
694 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
695 scanStart = *inOffsetp;
696 scanStart.LowPart &= (-cm_chunkSize);
697 thyper.LowPart = cm_chunkSize;
699 scanEnd = LargeIntegerAdd(scanStart, thyper);
701 flags = CM_SCACHESYNC_NEEDCALLBACK
702 | CM_SCACHESYNC_GETSTATUS
703 | CM_SCACHESYNC_STOREDATA
704 | CM_SCACHESYNC_BUFLOCKED
705 | CM_SCACHESYNC_NOWAIT;
707 /* start by looking backwards until scanStart */
708 thyper.HighPart = 0; /* hyper version of buf_bufferSize */
709 thyper.LowPart = buf_bufferSize;
710 tbase = LargeIntegerSubtract(firstModOffset, thyper);
711 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
712 /* see if we can find the buffer */
713 bufp = buf_Find(scp, &tbase);
717 /* try to lock it, and quit if we can't (simplifies locking) */
718 code = lock_TryMutex(&bufp->mx);
724 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
726 lock_ReleaseMutex(&bufp->mx);
731 if (!(bufp->flags & CM_BUF_DIRTY)) {
732 /* buffer is clean, so we shouldn't add it */
733 cm_SyncOpDone(scp, bufp, flags);
734 lock_ReleaseMutex(&bufp->mx);
739 /* don't need buffer mutex any more */
740 lock_ReleaseMutex(&bufp->mx);
742 /* we have a dirty buffer ready for storing. Add it to the tail
743 * of the list, since it immediately precedes all of the disk
744 * addresses we've already collected.
747 osi_SetQData(qdp, bufp);
748 /* no buf_hold necessary, since we have it held from buf_Find */
749 osi_QAddT((osi_queue_t **) &biop->bufListp,
750 (osi_queue_t **) &biop->bufListEndp,
753 /* update biod info describing the transfer */
754 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
755 biop->length += buf_bufferSize;
757 /* update loop pointer */
758 tbase = LargeIntegerSubtract(tbase, thyper);
759 } /* while loop looking for pages preceding the one we found */
761 /* now, find later dirty, contiguous pages, and add them to the list */
762 thyper.HighPart = 0; /* hyper version of buf_bufferSize */
763 thyper.LowPart = buf_bufferSize;
764 tbase = LargeIntegerAdd(firstModOffset, thyper);
765 while(LargeIntegerLessThan(tbase, scanEnd)) {
766 /* see if we can find the buffer */
767 bufp = buf_Find(scp, &tbase);
771 /* try to lock it, and quit if we can't (simplifies locking) */
772 code = lock_TryMutex(&bufp->mx);
778 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
780 lock_ReleaseMutex(&bufp->mx);
785 if (!(bufp->flags & CM_BUF_DIRTY)) {
786 /* buffer is clean, so we shouldn't add it */
787 cm_SyncOpDone(scp, bufp, flags);
788 lock_ReleaseMutex(&bufp->mx);
793 /* don't need buffer mutex any more */
794 lock_ReleaseMutex(&bufp->mx);
796 /* we have a dirty buffer ready for storing. Add it to the head
797 * of the list, since it immediately follows all of the disk
798 * addresses we've already collected.
801 osi_SetQData(qdp, bufp);
802 /* no buf_hold necessary, since we have it held from buf_Find */
803 osi_QAddH((osi_queue_t **) &biop->bufListp,
804 (osi_queue_t **) &biop->bufListEndp,
807 /* update biod info describing the transfer */
808 biop->length += buf_bufferSize;
810 /* update loop pointer */
811 tbase = LargeIntegerAdd(tbase, thyper);
812 } /* while loop looking for pages following the first page we found */
814 /* finally, we're done */
818 /* scp must be locked; temporarily unlocked during processing.
819 * If returns 0, returns buffers held in biop, and with
820 * CM_BUF_CMFETCHING flags set.
821 * If an error is returned, we don't return any buffers.
823 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
824 cm_bulkIO_t *biop, cm_user_t *up, cm_req_t *reqp)
828 osi_hyper_t toffset; /* a long long temp variable */
829 osi_hyper_t pageBase; /* base offset we're looking at */
830 osi_queueData_t *qdp; /* one temp queue structure */
831 osi_queueData_t *tqdp; /* another temp queue structure */
832 long collected; /* how many bytes have been collected */
835 osi_hyper_t fileSize; /* the # of bytes in the file */
836 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
837 osi_queueData_t *heldBufListEndp; /* first one */
841 biop->offset = *offsetp;
842 /* null out the list of buffers */
843 biop->bufListp = biop->bufListEndp = NULL;
846 /* first lookup the file's length, so we know when to stop */
847 code = cm_SyncOp(scp, NULL, up, reqp, 0,
848 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
852 /* copy out size, since it may change */
853 fileSize = scp->serverLength;
855 lock_ReleaseMutex(&scp->mx);
858 collected = pageBase.LowPart & (cm_chunkSize - 1);
860 heldBufListEndp = NULL;
863 * Obtaining buffers can cause dirty buffers to be recycled, which
864 * can cause a storeback, so cannot be done while we have buffers
867 * To get around this, we get buffers twice. Before reserving buffers,
868 * we obtain and release each one individually. After reserving
869 * buffers, we try to obtain them again, but only by lookup, not by
870 * recycling. If a buffer has gone away while we were waiting for
871 * the others, we just use whatever buffers we already have.
873 * On entry to this function, we are already holding a buffer, so we
874 * can't wait for reservation. So we call buf_TryReserveBuffers()
875 * instead. Not only that, we can't really even call buf_Get(), for
876 * the same reason. We can't avoid that, though. To avoid deadlock
877 * we allow only one thread to be executing the buf_Get()-buf_Release()
878 * sequence at a time.
881 /* first hold all buffers, since we can't hold any locks in buf_Get */
883 /* stop at chunk boundary */
884 if (collected >= cm_chunkSize) break;
886 /* see if the next page would be past EOF */
887 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize)) break;
889 lock_ObtainMutex(&cm_bufGetMutex);
891 code = buf_Get(scp, &pageBase, &tbp);
893 lock_ReleaseMutex(&cm_bufGetMutex);
894 lock_ObtainMutex(&scp->mx);
900 lock_ReleaseMutex(&cm_bufGetMutex);
902 toffset.HighPart = 0;
903 toffset.LowPart = buf_bufferSize;
904 pageBase = LargeIntegerAdd(toffset, pageBase);
905 collected += buf_bufferSize;
908 /* reserve a chunk's worth of buffers if possible */
909 reserving = buf_TryReserveBuffers(cm_chunkSize / buf_bufferSize);
912 collected = pageBase.LowPart & (cm_chunkSize - 1);
914 /* now hold all buffers, if they are still there */
916 /* stop at chunk boundary */
917 if (collected >= cm_chunkSize)
920 /* see if the next page would be past EOF */
921 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
924 tbp = buf_Find(scp, &pageBase);
928 /* add the buffer to the list */
930 osi_SetQData(qdp, tbp);
931 osi_QAdd((osi_queue_t **)&heldBufListp, &qdp->q);
932 if (!heldBufListEndp) heldBufListEndp = qdp;
933 /* leave tbp held (from buf_Get) */
938 collected += buf_bufferSize;
939 toffset.HighPart = 0;
940 toffset.LowPart = buf_bufferSize;
941 pageBase = LargeIntegerAdd(toffset, pageBase);
944 /* look at each buffer, adding it into the list if it looks idle and
945 * filled with old data. One special case: wait for idle if it is the
946 * first buffer since we really need that one for our caller to make
950 collected = 0; /* now count how many we'll really use */
951 for (tqdp = heldBufListEndp;
953 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
954 /* get a ptr to the held buffer */
955 tbp = osi_GetQData(tqdp);
956 pageBase = tbp->offset;
958 /* now lock the buffer lock */
959 lock_ObtainMutex(&tbp->mx);
960 lock_ObtainMutex(&scp->mx);
962 /* don't bother fetching over data that is already current */
963 if (tbp->dataVersion == scp->dataVersion) {
964 /* we don't need this buffer, since it is current */
965 lock_ReleaseMutex(&scp->mx);
966 lock_ReleaseMutex(&tbp->mx);
970 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_FETCHDATA
971 | CM_SCACHESYNC_BUFLOCKED;
973 flags |= CM_SCACHESYNC_NOWAIT;
975 /* wait for the buffer to serialize, if required. Doesn't
976 * release the scp or buffer lock(s) if NOWAIT is specified.
978 code = cm_SyncOp(scp, tbp, up, reqp, 0, flags);
980 lock_ReleaseMutex(&scp->mx);
981 lock_ReleaseMutex(&tbp->mx);
985 /* don't fetch over dirty buffers */
986 if (tbp->flags & CM_BUF_DIRTY) {
987 cm_SyncOpDone(scp, tbp, flags);
988 lock_ReleaseMutex(&scp->mx);
989 lock_ReleaseMutex(&tbp->mx);
994 lock_ReleaseMutex(&scp->mx);
995 lock_ReleaseMutex(&tbp->mx);
997 /* add the buffer to the list */
999 osi_SetQData(qdp, tbp);
1000 osi_QAdd((osi_queue_t **)&biop->bufListp, &qdp->q);
1001 if (!biop->bufListEndp)
1002 biop->bufListEndp = qdp;
1005 /* from now on, a failure just stops our collection process, but
1006 * we still do the I/O to whatever we've already managed to collect.
1009 collected += buf_bufferSize;
1012 /* now, we've held in biop->bufListp all the buffer's we're really
1013 * interested in. We also have holds left from heldBufListp, and we
1014 * now release those holds on the buffers.
1016 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1017 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1018 tbp = osi_GetQData(qdp);
1023 /* Caller expects this */
1024 lock_ObtainMutex(&scp->mx);
1026 /* if we got a failure setting up the first buffer, then we don't have
1027 * any side effects yet, and we also have failed an operation that the
1028 * caller requires to make any progress. Give up now.
1030 if (code && isFirst) {
1031 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
1035 /* otherwise, we're still OK, and should just return the I/O setup we've
1038 biop->length = collected;
1039 biop->reserved = reserving;
1043 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1046 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore)
1050 osi_queueData_t *qdp;
1051 osi_queueData_t *nqdp;
1054 /* Give back reserved buffers */
1056 buf_UnreserveBuffers(cm_chunkSize / buf_bufferSize);
1058 flags = CM_SCACHESYNC_NEEDCALLBACK;
1060 flags |= CM_SCACHESYNC_STOREDATA;
1062 flags |= CM_SCACHESYNC_FETCHDATA;
1065 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1066 /* lookup next guy first, since we're going to free this one */
1067 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1069 /* extract buffer and free queue data */
1070 bufp = osi_GetQData(qdp);
1073 /* now, mark I/O as done, unlock the buffer and release it */
1074 lock_ObtainMutex(&bufp->mx);
1075 lock_ObtainMutex(&scp->mx);
1076 cm_SyncOpDone(scp, bufp, flags);
1077 lock_ReleaseMutex(&scp->mx);
1079 /* turn off writing and wakeup users */
1081 if (bufp->flags & CM_BUF_WAITING) {
1082 osi_Wakeup((long) bufp);
1084 bufp->flags &= ~(CM_BUF_WAITING | CM_BUF_WRITING | CM_BUF_DIRTY);
1087 lock_ReleaseMutex(&bufp->mx);
1091 /* clean things out */
1092 biop->bufListp = NULL;
1093 biop->bufListEndp = NULL;
1096 /* Fetch a buffer. Called with scp locked.
1097 * The scp is locked on return.
1099 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *up,
1103 long nbytes; /* bytes in transfer */
1104 long rbytes; /* bytes in rx_Read call */
1106 AFSFetchStatus afsStatus;
1107 AFSCallBack callback;
1110 cm_buf_t *tbufp; /* buf we're filling */
1111 osi_queueData_t *qdp; /* q element we're scanning */
1113 struct rx_call *callp;
1114 cm_bulkIO_t biod; /* bulk IO descriptor */
1119 /* now, the buffer may or may not be filled with good data (buf_GetNew
1120 * drops lots of locks, and may indeed return a properly initialized
1121 * buffer, although more likely it will just return a new, empty, buffer.
1124 #ifdef AFS_FREELANCE_CLIENT
1126 // yj: if they're trying to get the /afs directory, we need to
1127 // handle it differently, since it's local rather than on any
1130 getroot = (scp==cm_rootSCachep);
1132 osi_Log1(afsd_logp,"GetBuffer returns cm_rootSCachep=%x",cm_rootSCachep);
1135 cm_AFSFidFromFid(&tfid, &scp->fid);
1137 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, up, reqp);
1139 /* couldn't even get the first page setup properly */
1140 osi_Log1(afsd_logp, "SetupFetchBIOD failure code %d", code);
1144 /* once we get here, we have the callback in place, we know that no one
1145 * is fetching the data now. Check one last time that we still have
1146 * the wrong data, and then fetch it if we're still wrong.
1148 * We can lose a race condition and end up with biod.length zero, in
1149 * which case we just retry.
1151 if (bufp->dataVersion == scp->dataVersion || biod.length == 0) {
1152 osi_Log3(afsd_logp, "Bad DVs %d, %d or length 0x%x",
1153 bufp->dataVersion, scp->dataVersion, biod.length);
1154 if ((bufp->dataVersion == -1
1155 || bufp->dataVersion < scp->dataVersion)
1156 && LargeIntegerGreaterThanOrEqualTo(bufp->offset,
1157 scp->serverLength)) {
1158 if (bufp->dataVersion == -1)
1159 memset(bufp->datap, 0, buf_bufferSize);
1160 bufp->dataVersion = scp->dataVersion;
1162 lock_ReleaseMutex(&scp->mx);
1163 cm_ReleaseBIOD(&biod, 0);
1164 lock_ObtainMutex(&scp->mx);
1168 lock_ReleaseMutex(&scp->mx);
1171 DPRINTF("cm_GetBuffer: fetching data scpDV=%d bufDV=%d scp=%x bp=%x dcp=%x\n",
1172 scp->dataVersion, bufp->dataVersion, scp, bufp, bufp->dcp);
1173 #endif /* DISKCACHE95 */
1175 #ifdef AFS_FREELANCE_CLIENT
1178 // if getroot then we don't need to make any calls
1179 // just return fake data
1181 if (cm_freelanceEnabled && getroot) {
1182 // setup the fake status
1183 afsStatus.InterfaceVersion = 0x1;
1184 afsStatus.FileType = 0x2;
1185 afsStatus.LinkCount = scp->linkCount;
1186 afsStatus.Length = cm_fakeDirSize;
1187 afsStatus.DataVersion = cm_fakeDirVersion;
1188 afsStatus.Author = 0x1;
1189 afsStatus.Owner = 0x0;
1190 afsStatus.CallerAccess = 0x9;
1191 afsStatus.AnonymousAccess = 0x9;
1192 afsStatus.UnixModeBits = 0x1ff;
1193 afsStatus.ParentVnode = 0x1;
1194 afsStatus.ParentUnique = 0x1;
1195 afsStatus.ResidencyMask = 0;
1196 afsStatus.ClientModTime = FakeFreelanceModTime;
1197 afsStatus.ServerModTime = FakeFreelanceModTime;
1198 afsStatus.Group = 0;
1199 afsStatus.SyncCounter = 0;
1200 afsStatus.dataVersionHigh = 0;
1202 // once we're done setting up the status info,
1203 // we just fill the buffer pages with fakedata
1204 // from cm_FakeRootDir. Extra pages are set to
1207 lock_ObtainMutex(&cm_Freelance_Lock);
1208 t1 = bufp->offset.LowPart;
1209 qdp = biod.bufListEndp;
1211 tbufp = osi_GetQData(qdp);
1212 bufferp=tbufp->datap;
1213 memset(bufferp, 0, buf_bufferSize);
1214 t2 = cm_fakeDirSize - t1;
1215 if (t2>buf_bufferSize) t2=buf_bufferSize;
1217 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1222 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1225 lock_ReleaseMutex(&cm_Freelance_Lock);
1227 // once we're done, we skip over the part of the
1228 // code that does the ACTUAL fetching of data for
1231 goto fetchingcompleted;
1234 #endif /* AFS_FREELANCE_CLIENT */
1236 /* now make the call */
1238 code = cm_Conn(&scp->fid, up, reqp, &connp);
1242 lock_ObtainMutex(&connp->mx);
1243 callp = rx_NewCall(connp->callp);
1245 osi_Log3(afsd_logp, "CALL FetchData vp %x, off 0x%x, size 0x%x",
1246 (long) scp, biod.offset.LowPart, biod.length);
1248 code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
1251 /* now copy the data out of the pipe and put it in the buffer */
1252 temp = rx_Read(callp, (char *)&nbytes, 4);
1254 nbytes = ntohl(nbytes);
1255 if (nbytes > biod.length)
1256 code = (callp->error < 0) ? callp->error : -1;
1259 code = (callp->error < 0) ? callp->error : -1;
1262 qdp = biod.bufListEndp;
1264 tbufp = osi_GetQData(qdp);
1265 bufferp = tbufp->datap;
1269 /* fill nbytes of data from the pipe into the pages.
1270 * When we stop, qdp will point at the last page we're
1271 * dealing with, and bufferp will tell us where we
1272 * stopped. We'll need this info below when we clear
1273 * the remainder of the last page out (and potentially
1274 * clear later pages out, if we fetch past EOF).
1276 while (nbytes > 0) {
1277 /* assert that there are still more buffers;
1278 * our check above for nbytes being less than
1279 * biod.length should ensure this.
1281 osi_assert(bufferp != NULL);
1283 /* read rbytes of data */
1284 rbytes = (nbytes > buf_bufferSize? buf_bufferSize : nbytes);
1285 temp = rx_Read(callp, bufferp, rbytes);
1286 if (temp < rbytes) {
1287 code = (callp->error < 0) ? callp->error : -1;
1291 /* allow read-while-fetching.
1292 * if this is the last buffer, clear the
1293 * PREFETCHING flag, so the reader waiting for
1294 * this buffer will start a prefetch.
1296 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1297 lock_ObtainMutex(&scp->mx);
1298 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1299 scp->flags &= ~CM_SCACHEFLAG_WAITING;
1300 osi_Wakeup((long) &scp->flags);
1302 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1304 cm_ClearPrefetchFlag(0, scp, &biod.offset);
1306 lock_ReleaseMutex(&scp->mx);
1308 /* and adjust counters */
1311 /* and move to the next buffer */
1313 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1315 tbufp = osi_GetQData(qdp);
1316 bufferp = tbufp->datap;
1324 /* zero out remainder of last pages, in case we are
1325 * fetching past EOF. We were fetching an integral #
1326 * of pages, but stopped, potentially in the middle of
1327 * a page. Zero the remainder of that page, and then
1328 * all of the rest of the pages.
1331 rbytes = bufferp - tbufp->datap;
1332 /* bytes left to zero */
1333 rbytes = buf_bufferSize - rbytes;
1336 memset(bufferp, 0, rbytes);
1337 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1340 tbufp = osi_GetQData(qdp);
1341 bufferp = tbufp->datap;
1342 /* bytes to clear in this page */
1343 rbytes = buf_bufferSize;
1348 code = EndRXAFS_FetchData(callp, &afsStatus, &callback, &volSync);
1350 osi_Log0(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error");
1351 code = rx_EndCall(callp, code);
1352 if (code == RXKADUNKNOWNKEY)
1353 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
1354 osi_Log0(afsd_logp, "CALL FetchData DONE");
1356 lock_ReleaseMutex(&connp->mx);
1358 } while (cm_Analyze(connp, up, reqp, &scp->fid, &volSync, NULL, NULL, code));
1361 code = cm_MapRPCError(code, reqp);
1363 lock_ObtainMutex(&scp->mx);
1364 /* we know that no one else has changed the buffer, since we still have
1365 * the fetching flag on the buffers, and we have the scp locked again.
1366 * Copy in the version # into the buffer if we got code 0 back from the
1370 for(qdp = biod.bufListp;
1372 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1373 tbufp = osi_GetQData(qdp);
1374 tbufp->dataVersion = afsStatus.DataVersion;
1377 /* write buffer out to disk cache */
1378 diskcache_Update(tbufp->dcp, tbufp->datap, buf_bufferSize,
1379 tbufp->dataVersion);
1380 #endif /* DISKCACHE95 */
1384 /* release scatter/gather I/O structure (buffers, locks) */
1385 lock_ReleaseMutex(&scp->mx);
1386 cm_ReleaseBIOD(&biod, 0);
1387 lock_ObtainMutex(&scp->mx);
1390 cm_MergeStatus(scp, &afsStatus, &volSync, up, 0);