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>
26 extern void afsi_log(char *pattern, ...);
29 osi_mutex_t cm_bufGetMutex;
30 #ifdef AFS_FREELANCE_CLIENT
31 extern osi_mutex_t cm_Freelance_Lock;
35 /* we can access connp->serverp without holding a lock because that
36 never changes since the connection is made. */
37 #define SERVERHAS64BIT(connp) (!((connp)->serverp->flags & CM_SERVERFLAG_NO64BIT))
38 #define SET_SERVERHASNO64BIT(connp) (cm_SetServerNo64Bit((connp)->serverp, TRUE))
40 #define SERVERHAS64BIT(connp) (FALSE)
41 #define SET_SERVERHASNO64BIT(connp) (FALSE)
44 /* functions called back from the buffer package when reading or writing data,
45 * or when holding or releasing a vnode pointer.
47 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
48 cm_user_t *userp, cm_req_t *reqp)
50 /* store the data back from this buffer; the buffer is locked and held,
51 * but the vnode involved isn't locked, yet. It is held by its
52 * reference from the buffer, which won't change until the buffer is
53 * released by our caller. Thus, we don't have to worry about holding
57 cm_scache_t *scp = vscp;
60 AFSFetchStatus outStatus;
61 AFSStoreStatus inStatus;
65 struct rx_call *callp;
66 struct rx_connection *rxconnp;
73 cm_bulkIO_t biod; /* bulk IO descriptor */
74 int require_64bit_ops = 0;
76 osi_assertx(userp != NULL, "null cm_user_t");
77 osi_assertx(scp != NULL, "null cm_scache_t");
79 /* now, the buffer may or may not be filled with good data (buf_GetNew
80 * drops lots of locks, and may indeed return a properly initialized
81 * buffer, although more likely it will just return a new, empty, buffer.
84 lock_ObtainMutex(&scp->mx);
85 if (scp->flags & CM_SCACHEFLAG_DELETED) {
86 lock_ReleaseMutex(&scp->mx);
87 return CM_ERROR_NOSUCHFILE;
90 cm_AFSFidFromFid(&tfid, &scp->fid);
92 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
94 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
95 lock_ReleaseMutex(&scp->mx);
99 if (biod.length == 0) {
100 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
101 lock_ReleaseMutex(&scp->mx);
102 cm_ReleaseBIOD(&biod, 1, 0); /* should be a NOOP */
106 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
107 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
109 /* prepare the output status for the store */
110 scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
111 cm_StatusFromAttr(&inStatus, scp, NULL);
112 truncPos = scp->length;
113 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
114 && LargeIntegerLessThan(scp->truncPos, truncPos))
115 truncPos = scp->truncPos;
116 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
118 /* compute how many bytes to write from this buffer */
119 thyper = LargeIntegerSubtract(scp->length, biod.offset);
120 if (LargeIntegerLessThanZero(thyper)) {
121 /* entire buffer is past EOF */
125 /* otherwise write out part of buffer before EOF, but not
126 * more than bufferSize bytes.
128 if (LargeIntegerGreaterThan(thyper,
129 ConvertLongToLargeInteger(biod.length))) {
130 nbytes = biod.length;
132 /* if thyper is less than or equal to biod.length, then we
133 can safely assume that the value fits in a long. */
134 nbytes = thyper.LowPart;
138 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
139 ConvertLongToLargeInteger(nbytes)),
140 ConvertLongToLargeInteger(LONG_MAX)) ||
141 LargeIntegerGreaterThan(truncPos,
142 ConvertLongToLargeInteger(LONG_MAX))) {
143 require_64bit_ops = 1;
146 lock_ReleaseMutex(&scp->mx);
148 /* now we're ready to do the store operation */
150 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
155 rxconnp = cm_GetRxConn(connp);
156 callp = rx_NewCall(rxconnp);
157 rx_PutConnection(rxconnp);
159 #ifdef AFS_LARGEFILES
160 if (SERVERHAS64BIT(connp)) {
161 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
162 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
164 code = StartRXAFS_StoreData64(callp, &tfid, &inStatus,
165 biod.offset.QuadPart,
169 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
171 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
173 if (require_64bit_ops) {
174 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData. The operation requires large file support in the server.");
175 code = CM_ERROR_TOOBIG;
177 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
178 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
180 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
181 biod.offset.LowPart, nbytes, truncPos.LowPart);
183 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
185 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
189 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
190 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
192 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
193 biod.offset.LowPart, nbytes, truncPos.LowPart);
195 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
197 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
201 /* write the data from the the list of buffers */
205 qdp = biod.bufListEndp;
207 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
208 osi_assertx(qdp != NULL, "null osi_queueData_t");
209 bufp = osi_GetQData(qdp);
210 bufferp = bufp->datap;
212 if (wbytes > cm_data.buf_blockSize)
213 wbytes = cm_data.buf_blockSize;
215 /* write out wbytes of data from bufferp */
216 temp = rx_Write(callp, bufferp, wbytes);
217 if (temp != wbytes) {
218 osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d",bufp,temp,wbytes);
222 osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p, %d",bufp,temp);
225 } /* while more bytes to write */
226 } /* if RPC started successfully */
229 if (SERVERHAS64BIT(connp)) {
230 code = EndRXAFS_StoreData64(callp, &outStatus, &volSync);
232 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
234 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
236 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
238 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
240 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
244 code = rx_EndCall(callp, code);
246 #ifdef AFS_LARGEFILES
247 if (code == RXGEN_OPCODE && SERVERHAS64BIT(connp)) {
248 SET_SERVERHASNO64BIT(connp);
253 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
255 code = cm_MapRPCError(code, reqp);
258 osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
260 osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
262 /* now, clean up our state */
263 lock_ObtainMutex(&scp->mx);
265 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
269 /* now, here's something a little tricky: in AFS 3, a dirty
270 * length can't be directly stored, instead, a dirty chunk is
271 * stored that sets the file's size (by writing and by using
272 * the truncate-first option in the store call).
274 * At this point, we've just finished a store, and so the trunc
275 * pos field is clean. If the file's size at the server is at
276 * least as big as we think it should be, then we turn off the
277 * length dirty bit, since all the other dirty buffers must
278 * precede this one in the file.
280 * The file's desired size shouldn't be smaller than what's
281 * stored at the server now, since we just did the trunc pos
284 * We have to turn off the length dirty bit as soon as we can,
285 * so that we see updates made by other machines.
288 if (SERVERHAS64BIT(connp)) {
289 t.LowPart = outStatus.Length;
290 t.HighPart = outStatus.Length_hi;
292 t = ConvertLongToLargeInteger(outStatus.Length);
295 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
296 scp->mask &= ~CM_SCACHEMASK_LENGTH;
298 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, CM_MERGEFLAG_STOREDATA);
300 if (code == CM_ERROR_SPACE)
301 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
302 else if (code == CM_ERROR_QUOTA)
303 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
305 lock_ReleaseMutex(&scp->mx);
306 cm_ReleaseBIOD(&biod, 1, code);
312 * Truncate the file, by sending a StoreData RPC with zero length.
314 * Called with scp locked. Releases and re-obtains the lock.
316 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
318 AFSFetchStatus outStatus;
319 AFSStoreStatus inStatus;
323 osi_hyper_t truncPos;
325 struct rx_call *callp;
326 struct rx_connection *rxconnp;
327 int require_64bit_ops = 0;
329 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
330 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
331 CM_SCACHESYNC_STOREDATA_EXCL);
333 /* prepare the output status for the store */
334 inStatus.Mask = AFS_SETMODTIME;
335 inStatus.ClientModTime = scp->clientModTime;
336 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
338 /* calculate truncation position */
339 truncPos = scp->length;
340 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
341 && LargeIntegerLessThan(scp->truncPos, truncPos))
342 truncPos = scp->truncPos;
343 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
345 if (LargeIntegerGreaterThan(truncPos,
346 ConvertLongToLargeInteger(LONG_MAX))) {
348 require_64bit_ops = 1;
351 lock_ReleaseMutex(&scp->mx);
353 cm_AFSFidFromFid(&tfid, &scp->fid);
355 /* now we're ready to do the store operation */
357 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
362 rxconnp = cm_GetRxConn(connp);
363 callp = rx_NewCall(rxconnp);
364 rx_PutConnection(rxconnp);
366 #ifdef AFS_LARGEFILES
367 if (SERVERHAS64BIT(connp)) {
368 code = StartRXAFS_StoreData64(callp, &tfid, &inStatus,
369 0, 0, truncPos.QuadPart);
371 if (require_64bit_ops) {
372 code = CM_ERROR_TOOBIG;
374 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
375 0, 0, truncPos.LowPart);
379 code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
380 0, 0, truncPos.LowPart);
384 if (SERVERHAS64BIT(connp))
385 code = EndRXAFS_StoreData64(callp, &outStatus, &volSync);
387 code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
389 code = rx_EndCall(callp, code);
391 #ifdef AFS_LARGEFILES
392 if (code == RXGEN_OPCODE && SERVERHAS64BIT(connp)) {
393 SET_SERVERHASNO64BIT(connp);
398 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
399 code = cm_MapRPCError(code, reqp);
401 /* now, clean up our state */
402 lock_ObtainMutex(&scp->mx);
404 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
409 * For explanation of handling of CM_SCACHEMASK_LENGTH,
412 if (SERVERHAS64BIT(connp)) {
413 t.HighPart = outStatus.Length_hi;
414 t.LowPart = outStatus.Length;
416 t = ConvertLongToLargeInteger(outStatus.Length);
419 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
420 scp->mask &= ~CM_SCACHEMASK_LENGTH;
421 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, CM_MERGEFLAG_STOREDATA);
427 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
429 *bytesReadp = cm_data.buf_blockSize;
431 /* now return a code that means that I/O is done */
435 /* stabilize scache entry, and return with it locked so
438 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
440 cm_scache_t *scp = vscp;
443 lock_ObtainMutex(&scp->mx);
444 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
445 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
447 lock_ReleaseMutex(&scp->mx);
454 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
455 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
457 cm_scache_t *scp = vscp;
459 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
461 lock_ReleaseMutex(&scp->mx);
463 /* always succeeds */
467 cm_buf_ops_t cm_bufOps = {
474 long cm_ValidateDCache(void)
476 return buf_ValidateBuffers();
479 long cm_ShutdownDCache(void)
484 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
486 lock_InitializeMutex(&cm_bufGetMutex, "buf_Get mutex");
487 return buf_Init(newFile, &cm_bufOps, nbuffers);
490 /* check to see if we have an up-to-date buffer. The buffer must have
491 * previously been obtained by calling buf_Get.
493 * Make sure we have a callback, and that the dataversion matches.
495 * Scp must be locked.
497 * Bufp *may* be locked.
499 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
502 if (!cm_HaveCallback(scp))
504 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
506 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
509 code = lock_TryMutex(&bufp->mx);
511 /* don't have the lock, and can't lock it, then
518 /* remember dirty flag for later */
519 code = bufp->flags & CM_BUF_DIRTY;
521 /* release lock if we obtained it here */
523 lock_ReleaseMutex(&bufp->mx);
525 /* if buffer was dirty, buffer is acceptable for use */
532 /* used when deciding whether to do a prefetch or not */
533 long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
534 cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
538 osi_hyper_t tblocksize;
543 /* now scan all buffers in the range, looking for any that look like
548 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
550 lock_ObtainMutex(&scp->mx);
551 while (LargeIntegerGreaterThanZero(tlength)) {
552 /* get callback so we can do a meaningful dataVersion comparison */
553 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
554 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
556 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
557 lock_ReleaseMutex(&scp->mx);
561 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
562 /* we're past the end of file */
566 bp = buf_Find(scp, &tbase);
567 /* We cheat slightly by not locking the bp mutex. */
569 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
570 && bp->dataVersion != scp->dataVersion)
578 /* if this buffer is essentially guaranteed to require a fetch,
579 * break out here and return this position.
584 tbase = LargeIntegerAdd(tbase, tblocksize);
585 tlength = LargeIntegerSubtract(tlength, tblocksize);
588 /* if we get here, either everything is fine or 'stop' stopped us at a
589 * particular buffer in the range that definitely needs to be fetched.
592 /* return non-zero code since realBasep won't be valid */
593 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
597 /* successfully found a page that will need fetching */
601 lock_ReleaseMutex(&scp->mx);
606 cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
614 if (scp->flags & CM_SCACHEFLAG_DELETED) {
615 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
619 /* Retries will be performed by the BkgDaemon thread if appropriate */
620 req.flags |= CM_REQ_NORETRY;
622 toffset.LowPart = p1;
623 toffset.HighPart = p2;
626 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
628 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, &req);
630 osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
633 lock_ObtainMutex(&scp->mx);
634 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
635 lock_ReleaseMutex(&scp->mx);
640 /* Called with scp locked */
641 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
646 end = LargeIntegerAdd(*base, *length);
647 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
648 scp->prefetch.base = *base;
649 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
650 scp->prefetch.end = end;
652 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
655 /* do the prefetch. if the prefetch fails, return 0 (success)
656 * because there is no harm done. */
658 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
666 osi_hyper_t tblocksize;
674 /* Retries will be performed by the BkgDaemon thread if appropriate */
675 req.flags |= CM_REQ_NORETRY;
678 fetched.HighPart = 0;
679 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
683 length.HighPart = p4;
685 end = LargeIntegerAdd(base, length);
687 osi_Log3(afsd_logp, "Starting BKG prefetch scp 0x%p, base 0x%x:%x", scp, p2, p1);
689 for ( code = 0, offset = base;
690 code == 0 && LargeIntegerLessThan(offset, end);
691 offset = LargeIntegerAdd(offset, tblocksize) )
694 lock_ReleaseMutex(&scp->mx);
698 code = buf_Get(scp, &offset, &bp);
702 if (bp->cmFlags & CM_BUF_CMFETCHING) {
703 /* skip this buffer as another thread is already fetching it */
710 lock_ObtainMutex(&scp->mx);
714 code = cm_GetBuffer(scp, bp, NULL, userp, &req);
716 fetched = LargeIntegerAdd(fetched, tblocksize);
721 lock_ObtainMutex(&scp->mx);
724 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
725 scp, &base, &fetched);
726 lock_ReleaseMutex(&scp->mx);
728 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p, code %d bytes 0x%x:%x",
729 scp, code, fetched.HighPart, fetched.LowPart);
733 /* a read was issued to offsetp, and we have to determine whether we should
734 * do a prefetch of the next chunk.
736 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
737 cm_user_t *userp, cm_req_t *reqp)
740 osi_hyper_t realBase;
741 osi_hyper_t readBase;
742 osi_hyper_t readLength;
745 /* round up to chunk boundary */
746 readBase.LowPart += (cm_chunkSize-1);
747 readBase.LowPart &= (-cm_chunkSize);
749 readLength = ConvertLongToLargeInteger(count);
751 lock_ObtainMutex(&scp->mx);
752 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
753 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
754 lock_ReleaseMutex(&scp->mx);
757 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
759 /* start the scan at the latter of the end of this read or
760 * the end of the last fetched region.
762 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
763 readBase = scp->prefetch.end;
765 lock_ReleaseMutex(&scp->mx);
767 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
770 return; /* can't find something to prefetch */
772 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
773 scp, realBase.LowPart);
775 cm_QueueBKGRequest(scp, cm_BkgPrefetch,
776 realBase.LowPart, realBase.HighPart,
777 readLength.LowPart, readLength.HighPart,
781 /* scp must be locked; temporarily unlocked during processing.
782 * If returns 0, returns buffers held in biop, and with
783 * CM_BUF_CMSTORING set.
785 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
786 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
787 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
788 * must be woken, and the event must be set when the I/O is done. All of this
789 * is required so that buf_WaitIO synchronizes properly with the buffer as it
790 * is being written out.
792 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
793 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
796 osi_queueData_t *qdp;
799 osi_hyper_t scanStart; /* where to start scan for dirty pages */
800 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
801 osi_hyper_t firstModOffset; /* offset of first modified page in range */
804 long flags; /* flags to cm_SyncOp */
806 /* clear things out */
807 biop->scp = scp; /* do not hold; held by caller */
808 biop->offset = *inOffsetp;
810 biop->bufListp = NULL;
811 biop->bufListEndp = NULL;
814 /* reserve a chunk's worth of buffers */
815 lock_ReleaseMutex(&scp->mx);
816 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
817 lock_ObtainMutex(&scp->mx);
820 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
821 thyper = ConvertLongToLargeInteger(temp);
822 tbase = LargeIntegerAdd(*inOffsetp, thyper);
824 bufp = buf_Find(scp, &tbase);
826 /* get buffer mutex and scp mutex safely */
827 lock_ReleaseMutex(&scp->mx);
828 lock_ObtainMutex(&bufp->mx);
829 lock_ObtainMutex(&scp->mx);
831 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
832 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
834 lock_ReleaseMutex(&bufp->mx);
837 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
841 /* if the buffer is dirty, we're done */
842 if (bufp->flags & CM_BUF_DIRTY) {
843 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
844 "WRITING w/o CMSTORING in SetupStoreBIOD");
845 bufp->flags |= CM_BUF_WRITING;
849 /* this buffer is clean, so there's no reason to process it */
850 cm_SyncOpDone(scp, bufp, flags);
851 lock_ReleaseMutex(&bufp->mx);
859 /* if we get here, if bufp is null, we didn't find any dirty buffers
860 * that weren't already being stored back, so we just quit now.
866 /* don't need buffer mutex any more */
867 lock_ReleaseMutex(&bufp->mx);
869 /* put this element in the list */
871 osi_SetQData(qdp, bufp);
872 /* don't have to hold bufp, since held by buf_Find above */
873 osi_QAddH((osi_queue_t **) &biop->bufListp,
874 (osi_queue_t **) &biop->bufListEndp,
876 biop->length = cm_data.buf_blockSize;
877 firstModOffset = bufp->offset;
878 biop->offset = firstModOffset;
879 bufp = NULL; /* this buffer and reference added to the queue */
881 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
882 scanStart = *inOffsetp;
883 scanStart.LowPart &= (-cm_chunkSize);
884 thyper = ConvertLongToLargeInteger(cm_chunkSize);
885 scanEnd = LargeIntegerAdd(scanStart, thyper);
887 flags = CM_SCACHESYNC_GETSTATUS
888 | CM_SCACHESYNC_STOREDATA
889 | CM_SCACHESYNC_BUFLOCKED
890 | CM_SCACHESYNC_NOWAIT;
892 /* start by looking backwards until scanStart */
893 /* hyper version of cm_data.buf_blockSize */
894 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
895 tbase = LargeIntegerSubtract(firstModOffset, thyper);
896 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
897 /* see if we can find the buffer */
898 bufp = buf_Find(scp, &tbase);
902 /* try to lock it, and quit if we can't (simplifies locking) */
903 lock_ReleaseMutex(&scp->mx);
904 code = lock_TryMutex(&bufp->mx);
905 lock_ObtainMutex(&scp->mx);
912 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
914 lock_ReleaseMutex(&bufp->mx);
920 if (!(bufp->flags & CM_BUF_DIRTY)) {
921 /* buffer is clean, so we shouldn't add it */
922 cm_SyncOpDone(scp, bufp, flags);
923 lock_ReleaseMutex(&bufp->mx);
929 /* don't need buffer mutex any more */
930 lock_ReleaseMutex(&bufp->mx);
932 /* we have a dirty buffer ready for storing. Add it to the tail
933 * of the list, since it immediately precedes all of the disk
934 * addresses we've already collected.
937 osi_SetQData(qdp, bufp);
938 /* no buf_hold necessary, since we have it held from buf_Find */
939 osi_QAddT((osi_queue_t **) &biop->bufListp,
940 (osi_queue_t **) &biop->bufListEndp,
942 bufp = NULL; /* added to the queue */
944 /* update biod info describing the transfer */
945 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
946 biop->length += cm_data.buf_blockSize;
948 /* update loop pointer */
949 tbase = LargeIntegerSubtract(tbase, thyper);
950 } /* while loop looking for pages preceding the one we found */
952 /* now, find later dirty, contiguous pages, and add them to the list */
953 /* hyper version of cm_data.buf_blockSize */
954 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
955 tbase = LargeIntegerAdd(firstModOffset, thyper);
956 while(LargeIntegerLessThan(tbase, scanEnd)) {
957 /* see if we can find the buffer */
958 bufp = buf_Find(scp, &tbase);
962 /* try to lock it, and quit if we can't (simplifies locking) */
963 lock_ReleaseMutex(&scp->mx);
964 code = lock_TryMutex(&bufp->mx);
965 lock_ObtainMutex(&scp->mx);
972 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
974 lock_ReleaseMutex(&bufp->mx);
980 if (!(bufp->flags & CM_BUF_DIRTY)) {
981 /* buffer is clean, so we shouldn't add it */
982 cm_SyncOpDone(scp, bufp, flags);
983 lock_ReleaseMutex(&bufp->mx);
989 /* don't need buffer mutex any more */
990 lock_ReleaseMutex(&bufp->mx);
992 /* we have a dirty buffer ready for storing. Add it to the head
993 * of the list, since it immediately follows all of the disk
994 * addresses we've already collected.
997 osi_SetQData(qdp, bufp);
998 /* no buf_hold necessary, since we have it held from buf_Find */
999 osi_QAddH((osi_queue_t **) &biop->bufListp,
1000 (osi_queue_t **) &biop->bufListEndp,
1004 /* update biod info describing the transfer */
1005 biop->length += cm_data.buf_blockSize;
1007 /* update loop pointer */
1008 tbase = LargeIntegerAdd(tbase, thyper);
1009 } /* while loop looking for pages following the first page we found */
1011 /* finally, we're done */
1015 /* scp must be locked; temporarily unlocked during processing.
1016 * If returns 0, returns buffers held in biop, and with
1017 * CM_BUF_CMFETCHING flags set.
1018 * If an error is returned, we don't return any buffers.
1020 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1021 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1025 osi_hyper_t tblocksize; /* a long long temp variable */
1026 osi_hyper_t pageBase; /* base offset we're looking at */
1027 osi_queueData_t *qdp; /* one temp queue structure */
1028 osi_queueData_t *tqdp; /* another temp queue structure */
1029 long collected; /* how many bytes have been collected */
1032 osi_hyper_t fileSize; /* the # of bytes in the file */
1033 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1034 osi_queueData_t *heldBufListEndp; /* first one */
1037 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1039 biop->scp = scp; /* do not hold; held by caller */
1040 biop->offset = *offsetp;
1041 /* null out the list of buffers */
1042 biop->bufListp = biop->bufListEndp = NULL;
1045 /* first lookup the file's length, so we know when to stop */
1046 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1047 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1051 /* copy out size, since it may change */
1052 fileSize = scp->serverLength;
1054 lock_ReleaseMutex(&scp->mx);
1056 pageBase = *offsetp;
1057 collected = pageBase.LowPart & (cm_chunkSize - 1);
1058 heldBufListp = NULL;
1059 heldBufListEndp = NULL;
1062 * Obtaining buffers can cause dirty buffers to be recycled, which
1063 * can cause a storeback, so cannot be done while we have buffers
1066 * To get around this, we get buffers twice. Before reserving buffers,
1067 * we obtain and release each one individually. After reserving
1068 * buffers, we try to obtain them again, but only by lookup, not by
1069 * recycling. If a buffer has gone away while we were waiting for
1070 * the others, we just use whatever buffers we already have.
1072 * On entry to this function, we are already holding a buffer, so we
1073 * can't wait for reservation. So we call buf_TryReserveBuffers()
1074 * instead. Not only that, we can't really even call buf_Get(), for
1075 * the same reason. We can't avoid that, though. To avoid deadlock
1076 * we allow only one thread to be executing the buf_Get()-buf_Release()
1077 * sequence at a time.
1080 // lock_ObtainMutex(&cm_bufGetMutex);
1081 /* first hold all buffers, since we can't hold any locks in buf_Get */
1083 /* stop at chunk boundary */
1084 if (collected >= cm_chunkSize)
1087 /* see if the next page would be past EOF */
1088 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1091 code = buf_Get(scp, &pageBase, &tbp);
1093 //lock_ReleaseMutex(&cm_bufGetMutex);
1094 lock_ObtainMutex(&scp->mx);
1095 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1102 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1103 collected += cm_data.buf_blockSize;
1106 /* reserve a chunk's worth of buffers if possible */
1107 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1109 // lock_ReleaseMutex(&cm_bufGetMutex);
1111 pageBase = *offsetp;
1112 collected = pageBase.LowPart & (cm_chunkSize - 1);
1114 /* now hold all buffers, if they are still there */
1116 /* stop at chunk boundary */
1117 if (collected >= cm_chunkSize)
1120 /* see if the next page would be past EOF */
1121 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1124 tbp = buf_Find(scp, &pageBase);
1128 /* add the buffer to the list */
1129 qdp = osi_QDAlloc();
1130 osi_SetQData(qdp, tbp);
1131 osi_QAddH((osi_queue_t **)&heldBufListp,
1132 (osi_queue_t **)&heldBufListEndp,
1134 /* leave tbp held (from buf_Get) */
1139 collected += cm_data.buf_blockSize;
1140 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1143 /* look at each buffer, adding it into the list if it looks idle and
1144 * filled with old data. One special case: wait for idle if it is the
1145 * first buffer since we really need that one for our caller to make
1149 collected = 0; /* now count how many we'll really use */
1150 for (tqdp = heldBufListEndp;
1152 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1153 /* get a ptr to the held buffer */
1154 tbp = osi_GetQData(tqdp);
1155 pageBase = tbp->offset;
1157 /* now lock the buffer lock */
1158 lock_ObtainMutex(&tbp->mx);
1159 lock_ObtainMutex(&scp->mx);
1161 /* don't bother fetching over data that is already current */
1162 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1163 /* we don't need this buffer, since it is current */
1164 lock_ReleaseMutex(&scp->mx);
1165 lock_ReleaseMutex(&tbp->mx);
1169 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1171 flags |= CM_SCACHESYNC_NOWAIT;
1173 /* wait for the buffer to serialize, if required. Doesn't
1174 * release the scp or buffer lock(s) if NOWAIT is specified.
1176 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1178 lock_ReleaseMutex(&scp->mx);
1179 lock_ReleaseMutex(&tbp->mx);
1183 /* don't fetch over dirty buffers */
1184 if (tbp->flags & CM_BUF_DIRTY) {
1185 cm_SyncOpDone(scp, tbp, flags);
1186 lock_ReleaseMutex(&scp->mx);
1187 lock_ReleaseMutex(&tbp->mx);
1192 lock_ReleaseMutex(&scp->mx);
1193 lock_ReleaseMutex(&tbp->mx);
1195 /* add the buffer to the list */
1196 qdp = osi_QDAlloc();
1197 osi_SetQData(qdp, tbp);
1198 osi_QAddH((osi_queue_t **)&biop->bufListp,
1199 (osi_queue_t **)&biop->bufListEndp,
1203 /* from now on, a failure just stops our collection process, but
1204 * we still do the I/O to whatever we've already managed to collect.
1207 collected += cm_data.buf_blockSize;
1210 /* now, we've held in biop->bufListp all the buffer's we're really
1211 * interested in. We also have holds left from heldBufListp, and we
1212 * now release those holds on the buffers.
1214 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1215 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1216 tbp = osi_GetQData(qdp);
1217 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1218 (osi_queue_t **) &heldBufListEndp,
1225 /* Caller expects this */
1226 lock_ObtainMutex(&scp->mx);
1228 /* if we got a failure setting up the first buffer, then we don't have
1229 * any side effects yet, and we also have failed an operation that the
1230 * caller requires to make any progress. Give up now.
1232 if (code && isFirst) {
1233 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1237 /* otherwise, we're still OK, and should just return the I/O setup we've
1240 biop->length = collected;
1241 biop->reserved = reserving;
1245 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1248 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
1250 cm_scache_t *scp; /* do not release; not held in biop */
1252 osi_queueData_t *qdp;
1253 osi_queueData_t *nqdp;
1256 /* Give back reserved buffers */
1258 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1261 flags = CM_SCACHESYNC_STOREDATA;
1263 flags = CM_SCACHESYNC_FETCHDATA;
1266 if (biop->bufListp) {
1267 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1268 /* lookup next guy first, since we're going to free this one */
1269 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1271 /* extract buffer and free queue data */
1272 bufp = osi_GetQData(qdp);
1273 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1274 (osi_queue_t **) &biop->bufListEndp,
1278 /* now, mark I/O as done, unlock the buffer and release it */
1279 lock_ObtainMutex(&bufp->mx);
1280 lock_ObtainMutex(&scp->mx);
1281 cm_SyncOpDone(scp, bufp, flags);
1283 /* turn off writing and wakeup users */
1285 if (bufp->flags & CM_BUF_WAITING) {
1286 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1287 osi_Wakeup((LONG_PTR) bufp);
1290 bufp->flags &= ~CM_BUF_WRITING;
1292 bufp->flags &= ~(CM_BUF_WRITING | CM_BUF_DIRTY);
1293 bufp->dirty_offset = bufp->dirty_length = 0;
1297 lock_ReleaseMutex(&scp->mx);
1298 lock_ReleaseMutex(&bufp->mx);
1303 lock_ObtainMutex(&scp->mx);
1304 cm_SyncOpDone(scp, NULL, flags);
1305 lock_ReleaseMutex(&scp->mx);
1308 /* clean things out */
1309 biop->bufListp = NULL;
1310 biop->bufListEndp = NULL;
1313 /* Fetch a buffer. Called with scp locked.
1314 * The scp is locked on return.
1316 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1320 afs_int32 nbytes; /* bytes in transfer */
1321 afs_int32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1322 afs_int64 length_found = 0;
1323 long rbytes; /* bytes in rx_Read call */
1325 AFSFetchStatus afsStatus;
1326 AFSCallBack callback;
1329 cm_buf_t *tbufp; /* buf we're filling */
1330 osi_queueData_t *qdp; /* q element we're scanning */
1332 struct rx_call *callp;
1333 struct rx_connection *rxconnp;
1334 cm_bulkIO_t biod; /* bulk IO descriptor */
1338 int require_64bit_ops = 0;
1340 /* now, the buffer may or may not be filled with good data (buf_GetNew
1341 * drops lots of locks, and may indeed return a properly initialized
1342 * buffer, although more likely it will just return a new, empty, buffer.
1345 #ifdef AFS_FREELANCE_CLIENT
1347 // yj: if they're trying to get the /afs directory, we need to
1348 // handle it differently, since it's local rather than on any
1351 getroot = (scp==cm_data.rootSCachep);
1353 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1356 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1357 /* We already have this buffer don't do extra work */
1361 cm_AFSFidFromFid(&tfid, &scp->fid);
1363 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1365 /* couldn't even get the first page setup properly */
1366 osi_Log1(afsd_logp, "GetBuffer: SetupFetchBIOD failure code %d", code);
1370 /* once we get here, we have the callback in place, we know that no one
1371 * is fetching the data now. Check one last time that we still have
1372 * the wrong data, and then fetch it if we're still wrong.
1374 * We can lose a race condition and end up with biod.length zero, in
1375 * which case we just retry.
1377 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
1378 if ((bufp->dataVersion == -1 || bufp->dataVersion < scp->dataVersion) &&
1379 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength))
1381 osi_Log3(afsd_logp, "Bad DVs %I64d, %I64d or length 0x%x",
1382 bufp->dataVersion, scp->dataVersion, biod.length);
1384 if (bufp->dataVersion == -1)
1385 memset(bufp->datap, 0, cm_data.buf_blockSize);
1386 bufp->dataVersion = scp->dataVersion;
1388 lock_ReleaseMutex(&scp->mx);
1389 cm_ReleaseBIOD(&biod, 0, 0);
1390 lock_ObtainMutex(&scp->mx);
1394 lock_ReleaseMutex(&scp->mx);
1396 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1397 ConvertLongToLargeInteger(biod.length)),
1398 ConvertLongToLargeInteger(LONG_MAX))) {
1399 require_64bit_ops = 1;
1403 DPRINTF("cm_GetBuffer: fetching data scpDV=%I64d bufDV=%I64d scp=%x bp=%x dcp=%x\n",
1404 scp->dataVersion, bufp->dataVersion, scp, bufp, bufp->dcp);
1405 #endif /* DISKCACHE95 */
1407 #ifdef AFS_FREELANCE_CLIENT
1410 // if getroot then we don't need to make any calls
1411 // just return fake data
1413 if (cm_freelanceEnabled && getroot) {
1414 // setup the fake status
1415 afsStatus.InterfaceVersion = 0x1;
1416 afsStatus.FileType = 0x2;
1417 afsStatus.LinkCount = scp->linkCount;
1418 afsStatus.Length = cm_fakeDirSize;
1419 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1420 afsStatus.Author = 0x1;
1421 afsStatus.Owner = 0x0;
1422 afsStatus.CallerAccess = 0x9;
1423 afsStatus.AnonymousAccess = 0x9;
1424 afsStatus.UnixModeBits = 0x1ff;
1425 afsStatus.ParentVnode = 0x1;
1426 afsStatus.ParentUnique = 0x1;
1427 afsStatus.ResidencyMask = 0;
1428 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1429 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1430 afsStatus.Group = 0;
1431 afsStatus.SyncCounter = 0;
1432 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1433 afsStatus.lockCount = 0;
1434 afsStatus.Length_hi = 0;
1435 afsStatus.errorCode = 0;
1437 // once we're done setting up the status info,
1438 // we just fill the buffer pages with fakedata
1439 // from cm_FakeRootDir. Extra pages are set to
1442 lock_ObtainMutex(&cm_Freelance_Lock);
1443 t1 = bufp->offset.LowPart;
1444 qdp = biod.bufListEndp;
1446 tbufp = osi_GetQData(qdp);
1447 bufferp=tbufp->datap;
1448 memset(bufferp, 0, cm_data.buf_blockSize);
1449 t2 = cm_fakeDirSize - t1;
1450 if (t2> (afs_int32)cm_data.buf_blockSize)
1451 t2=cm_data.buf_blockSize;
1453 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1458 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1461 lock_ReleaseMutex(&cm_Freelance_Lock);
1463 // once we're done, we skip over the part of the
1464 // code that does the ACTUAL fetching of data for
1467 goto fetchingcompleted;
1470 #endif /* AFS_FREELANCE_CLIENT */
1472 /* now make the call */
1474 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1478 rxconnp = cm_GetRxConn(connp);
1479 callp = rx_NewCall(rxconnp);
1480 rx_PutConnection(rxconnp);
1482 #ifdef AFS_LARGEFILES
1483 nbytes = nbytes_hi = 0;
1485 if (SERVERHAS64BIT(connp)) {
1486 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1487 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1489 code = StartRXAFS_FetchData64(callp, &tfid, biod.offset.QuadPart, biod.length);
1492 temp = rx_Read(callp, (char *) &nbytes_hi, sizeof(afs_int32));
1493 if (temp == sizeof(afs_int32)) {
1494 nbytes_hi = ntohl(nbytes_hi);
1497 code = callp->error;
1498 rx_EndCall(callp, code);
1504 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1505 if (require_64bit_ops) {
1506 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1507 code = CM_ERROR_TOOBIG;
1510 rxconnp = cm_GetRxConn(connp);
1511 callp = rx_NewCall(rxconnp);
1512 rx_PutConnection(rxconnp);
1515 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1516 scp, biod.offset.LowPart, biod.length);
1518 code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
1521 SET_SERVERHASNO64BIT(connp);
1526 temp = rx_Read(callp, (char *)&nbytes, sizeof(afs_int32));
1527 if (temp == sizeof(afs_int32)) {
1528 nbytes = ntohl(nbytes);
1529 FillInt64(length_found, nbytes_hi, nbytes);
1530 if (length_found > biod.length)
1531 code = (callp->error < 0) ? callp->error : -1;
1533 code = (callp->error < 0) ? callp->error : -1;
1536 /* for the moment, nbytes_hi will always be 0 if code == 0
1537 because biod.length is a 32-bit quantity. */
1539 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1540 scp, biod.offset.LowPart, biod.length);
1542 code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
1545 /* now copy the data out of the pipe and put it in the buffer */
1547 temp = rx_Read(callp, (char *)&nbytes, sizeof(afs_int32));
1548 if (temp == sizeof(afs_int32)) {
1549 nbytes = ntohl(nbytes);
1550 if (nbytes > biod.length)
1551 code = (callp->error < 0) ? callp->error : -1;
1554 code = (callp->error < 0) ? callp->error : -1;
1559 qdp = biod.bufListEndp;
1561 tbufp = osi_GetQData(qdp);
1562 bufferp = tbufp->datap;
1566 /* fill nbytes of data from the pipe into the pages.
1567 * When we stop, qdp will point at the last page we're
1568 * dealing with, and bufferp will tell us where we
1569 * stopped. We'll need this info below when we clear
1570 * the remainder of the last page out (and potentially
1571 * clear later pages out, if we fetch past EOF).
1573 while (nbytes > 0) {
1574 /* assert that there are still more buffers;
1575 * our check above for nbytes being less than
1576 * biod.length should ensure this.
1578 osi_assertx(bufferp != NULL, "null cm_buf_t");
1580 /* read rbytes of data */
1581 rbytes = (nbytes > cm_data.buf_blockSize? cm_data.buf_blockSize : nbytes);
1582 temp = rx_Read(callp, bufferp, rbytes);
1583 if (temp < rbytes) {
1584 code = (callp->error < 0) ? callp->error : -1;
1588 /* allow read-while-fetching.
1589 * if this is the last buffer, clear the
1590 * PREFETCHING flag, so the reader waiting for
1591 * this buffer will start a prefetch.
1593 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1594 lock_ObtainMutex(&scp->mx);
1595 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1596 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
1597 osi_Wakeup((LONG_PTR) &scp->flags);
1599 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1600 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
1602 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
1604 lock_ReleaseMutex(&scp->mx);
1606 /* and adjust counters */
1609 /* and move to the next buffer */
1611 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1613 tbufp = osi_GetQData(qdp);
1614 bufferp = tbufp->datap;
1622 /* zero out remainder of last pages, in case we are
1623 * fetching past EOF. We were fetching an integral #
1624 * of pages, but stopped, potentially in the middle of
1625 * a page. Zero the remainder of that page, and then
1626 * all of the rest of the pages.
1629 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
1630 rbytes = (long) (bufferp - tbufp->datap);
1632 /* bytes left to zero */
1633 rbytes = cm_data.buf_blockSize - rbytes;
1636 memset(bufferp, 0, rbytes);
1637 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1640 tbufp = osi_GetQData(qdp);
1641 bufferp = tbufp->datap;
1642 /* bytes to clear in this page */
1643 rbytes = cm_data.buf_blockSize;
1648 if (SERVERHAS64BIT(connp))
1649 code = EndRXAFS_FetchData64(callp, &afsStatus, &callback, &volSync);
1651 code = EndRXAFS_FetchData(callp, &afsStatus, &callback, &volSync);
1653 if (SERVERHAS64BIT(connp))
1654 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
1656 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
1660 code = rx_EndCall(callp, code);
1662 if (code == RXKADUNKNOWNKEY)
1663 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
1665 osi_Log0(afsd_logp, "CALL FetchData DONE");
1667 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
1670 code = cm_MapRPCError(code, reqp);
1672 lock_ObtainMutex(&scp->mx);
1674 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
1676 /* we know that no one else has changed the buffer, since we still have
1677 * the fetching flag on the buffers, and we have the scp locked again.
1678 * Copy in the version # into the buffer if we got code 0 back from the
1682 for(qdp = biod.bufListp;
1684 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1685 tbufp = osi_GetQData(qdp);
1686 tbufp->dataVersion = afsStatus.dataVersionHigh;
1687 tbufp->dataVersion <<= 32;
1688 tbufp->dataVersion |= afsStatus.DataVersion;
1691 /* write buffer out to disk cache */
1692 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
1693 tbufp->dataVersion);
1694 #endif /* DISKCACHE95 */
1698 /* release scatter/gather I/O structure (buffers, locks) */
1699 lock_ReleaseMutex(&scp->mx);
1700 cm_ReleaseBIOD(&biod, 0, code);
1701 lock_ObtainMutex(&scp->mx);
1704 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, 0);