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 #ifdef AFS_FREELANCE_CLIENT
30 extern osi_mutex_t cm_Freelance_Lock;
34 /* we can access connp->serverp without holding a lock because that
35 never changes since the connection is made. */
36 #define SERVERHAS64BIT(connp) (!((connp)->serverp->flags & CM_SERVERFLAG_NO64BIT))
37 #define SET_SERVERHASNO64BIT(connp) (cm_SetServerNo64Bit((connp)->serverp, TRUE))
39 #define SERVERHAS64BIT(connp) (FALSE)
40 #define SET_SERVERHASNO64BIT(connp) (FALSE)
43 /* functions called back from the buffer package when reading or writing data,
44 * or when holding or releasing a vnode pointer.
46 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
47 cm_user_t *userp, cm_req_t *reqp)
49 /* store the data back from this buffer; the buffer is locked and held,
50 * but the vnode involved isn't locked, yet. It is held by its
51 * reference from the buffer, which won't change until the buffer is
52 * released by our caller. Thus, we don't have to worry about holding
56 cm_scache_t *scp = vscp;
59 AFSFetchStatus outStatus;
60 AFSStoreStatus inStatus;
64 struct rx_call *rxcallp;
65 struct rx_connection *rxconnp;
72 cm_bulkIO_t biod; /* bulk IO descriptor */
73 int require_64bit_ops = 0;
75 osi_assertx(userp != NULL, "null cm_user_t");
76 osi_assertx(scp != NULL, "null cm_scache_t");
78 /* now, the buffer may or may not be filled with good data (buf_GetNew
79 * drops lots of locks, and may indeed return a properly initialized
80 * buffer, although more likely it will just return a new, empty, buffer.
83 lock_ObtainWrite(&scp->rw);
84 if (scp->flags & CM_SCACHEFLAG_DELETED) {
85 lock_ReleaseWrite(&scp->rw);
86 return CM_ERROR_NOSUCHFILE;
89 cm_AFSFidFromFid(&tfid, &scp->fid);
91 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
93 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
94 lock_ReleaseWrite(&scp->rw);
98 if (biod.length == 0) {
99 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
100 lock_ReleaseWrite(&scp->rw);
101 cm_ReleaseBIOD(&biod, 1, 0); /* should be a NOOP */
105 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
106 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
108 /* prepare the output status for the store */
109 scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
110 cm_StatusFromAttr(&inStatus, scp, NULL);
111 truncPos = scp->length;
112 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
113 && LargeIntegerLessThan(scp->truncPos, truncPos))
114 truncPos = scp->truncPos;
115 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
117 /* compute how many bytes to write from this buffer */
118 thyper = LargeIntegerSubtract(scp->length, biod.offset);
119 if (LargeIntegerLessThanZero(thyper)) {
120 /* entire buffer is past EOF */
124 /* otherwise write out part of buffer before EOF, but not
125 * more than bufferSize bytes.
127 if (LargeIntegerGreaterThan(thyper,
128 ConvertLongToLargeInteger(biod.length))) {
129 nbytes = biod.length;
131 /* if thyper is less than or equal to biod.length, then we
132 can safely assume that the value fits in a long. */
133 nbytes = thyper.LowPart;
137 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
138 ConvertLongToLargeInteger(nbytes)),
139 ConvertLongToLargeInteger(LONG_MAX)) ||
140 LargeIntegerGreaterThan(truncPos,
141 ConvertLongToLargeInteger(LONG_MAX))) {
142 require_64bit_ops = 1;
145 lock_ReleaseWrite(&scp->rw);
147 /* now we're ready to do the store operation */
149 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
154 rxconnp = cm_GetRxConn(connp);
155 rxcallp = rx_NewCall(rxconnp);
156 rx_PutConnection(rxconnp);
158 #ifdef AFS_LARGEFILES
159 if (SERVERHAS64BIT(connp)) {
160 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
161 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
163 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
164 biod.offset.QuadPart,
168 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
170 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
172 if (require_64bit_ops) {
173 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData. The operation requires large file support in the server.");
174 code = CM_ERROR_TOOBIG;
176 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
177 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
179 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
180 biod.offset.LowPart, nbytes, truncPos.LowPart);
182 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
184 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
188 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
189 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
191 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
192 biod.offset.LowPart, nbytes, truncPos.LowPart);
194 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
196 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
200 /* write the data from the the list of buffers */
204 qdp = biod.bufListEndp;
206 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
207 osi_assertx(qdp != NULL, "null osi_queueData_t");
208 bufp = osi_GetQData(qdp);
209 bufferp = bufp->datap;
211 if (wbytes > cm_data.buf_blockSize)
212 wbytes = cm_data.buf_blockSize;
214 /* write out wbytes of data from bufferp */
215 temp = rx_Write(rxcallp, bufferp, wbytes);
216 if (temp != wbytes) {
217 osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d",bufp,temp,wbytes);
221 osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p, %d",bufp,temp);
224 } /* while more bytes to write */
225 } /* if RPC started successfully */
228 if (SERVERHAS64BIT(connp)) {
229 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
231 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
233 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
235 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
237 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
239 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
243 code = rx_EndCall(rxcallp, code);
245 #ifdef AFS_LARGEFILES
246 if (code == RXGEN_OPCODE && SERVERHAS64BIT(connp)) {
247 SET_SERVERHASNO64BIT(connp);
252 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
254 code = cm_MapRPCError(code, reqp);
257 osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
259 osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
261 /* now, clean up our state */
262 lock_ObtainWrite(&scp->rw);
264 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
268 /* now, here's something a little tricky: in AFS 3, a dirty
269 * length can't be directly stored, instead, a dirty chunk is
270 * stored that sets the file's size (by writing and by using
271 * the truncate-first option in the store call).
273 * At this point, we've just finished a store, and so the trunc
274 * pos field is clean. If the file's size at the server is at
275 * least as big as we think it should be, then we turn off the
276 * length dirty bit, since all the other dirty buffers must
277 * precede this one in the file.
279 * The file's desired size shouldn't be smaller than what's
280 * stored at the server now, since we just did the trunc pos
283 * We have to turn off the length dirty bit as soon as we can,
284 * so that we see updates made by other machines.
287 if (SERVERHAS64BIT(connp)) {
288 t.LowPart = outStatus.Length;
289 t.HighPart = outStatus.Length_hi;
291 t = ConvertLongToLargeInteger(outStatus.Length);
294 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
295 scp->mask &= ~CM_SCACHEMASK_LENGTH;
297 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, CM_MERGEFLAG_STOREDATA);
299 if (code == CM_ERROR_SPACE)
300 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
301 else if (code == CM_ERROR_QUOTA)
302 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
304 lock_ReleaseWrite(&scp->rw);
305 cm_ReleaseBIOD(&biod, 1, code);
311 * Truncate the file, by sending a StoreData RPC with zero length.
313 * Called with scp locked. Releases and re-obtains the lock.
315 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
317 AFSFetchStatus outStatus;
318 AFSStoreStatus inStatus;
322 osi_hyper_t truncPos;
324 struct rx_call *rxcallp;
325 struct rx_connection *rxconnp;
326 int require_64bit_ops = 0;
328 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
329 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
330 CM_SCACHESYNC_STOREDATA_EXCL);
332 /* prepare the output status for the store */
333 inStatus.Mask = AFS_SETMODTIME;
334 inStatus.ClientModTime = scp->clientModTime;
335 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
337 /* calculate truncation position */
338 truncPos = scp->length;
339 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
340 && LargeIntegerLessThan(scp->truncPos, truncPos))
341 truncPos = scp->truncPos;
342 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
344 if (LargeIntegerGreaterThan(truncPos,
345 ConvertLongToLargeInteger(LONG_MAX))) {
347 require_64bit_ops = 1;
350 lock_ReleaseWrite(&scp->rw);
352 cm_AFSFidFromFid(&tfid, &scp->fid);
354 /* now we're ready to do the store operation */
356 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
361 rxconnp = cm_GetRxConn(connp);
362 rxcallp = rx_NewCall(rxconnp);
363 rx_PutConnection(rxconnp);
365 #ifdef AFS_LARGEFILES
366 if (SERVERHAS64BIT(connp)) {
367 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
368 0, 0, truncPos.QuadPart);
370 if (require_64bit_ops) {
371 code = CM_ERROR_TOOBIG;
373 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
374 0, 0, truncPos.LowPart);
378 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
379 0, 0, truncPos.LowPart);
383 if (SERVERHAS64BIT(connp))
384 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
386 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
388 code = rx_EndCall(rxcallp, code);
390 #ifdef AFS_LARGEFILES
391 if (code == RXGEN_OPCODE && SERVERHAS64BIT(connp)) {
392 SET_SERVERHASNO64BIT(connp);
397 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
398 code = cm_MapRPCError(code, reqp);
400 /* now, clean up our state */
401 lock_ObtainWrite(&scp->rw);
403 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
408 * For explanation of handling of CM_SCACHEMASK_LENGTH,
411 if (SERVERHAS64BIT(connp)) {
412 t.HighPart = outStatus.Length_hi;
413 t.LowPart = outStatus.Length;
415 t = ConvertLongToLargeInteger(outStatus.Length);
418 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
419 scp->mask &= ~CM_SCACHEMASK_LENGTH;
420 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, CM_MERGEFLAG_STOREDATA);
426 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
428 *bytesReadp = cm_data.buf_blockSize;
430 /* now return a code that means that I/O is done */
434 /* stabilize scache entry, and return with it locked so
437 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
439 cm_scache_t *scp = vscp;
442 lock_ObtainWrite(&scp->rw);
443 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
444 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
446 lock_ReleaseWrite(&scp->rw);
453 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
454 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
456 cm_scache_t *scp = vscp;
458 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
460 lock_ReleaseWrite(&scp->rw);
462 /* always succeeds */
466 cm_buf_ops_t cm_bufOps = {
473 long cm_ValidateDCache(void)
475 return buf_ValidateBuffers();
478 long cm_ShutdownDCache(void)
483 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
485 return buf_Init(newFile, &cm_bufOps, nbuffers);
488 /* check to see if we have an up-to-date buffer. The buffer must have
489 * previously been obtained by calling buf_Get.
491 * Make sure we have a callback, and that the dataversion matches.
493 * Scp must be locked.
495 * Bufp *may* be locked.
497 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
500 if (!cm_HaveCallback(scp))
502 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
504 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
507 code = lock_TryMutex(&bufp->mx);
509 /* don't have the lock, and can't lock it, then
516 /* remember dirty flag for later */
517 code = bufp->flags & CM_BUF_DIRTY;
519 /* release lock if we obtained it here */
521 lock_ReleaseMutex(&bufp->mx);
523 /* if buffer was dirty, buffer is acceptable for use */
530 /* used when deciding whether to do a prefetch or not */
531 long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
532 cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
536 osi_hyper_t tblocksize;
541 /* now scan all buffers in the range, looking for any that look like
546 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
548 lock_ObtainWrite(&scp->rw);
549 while (LargeIntegerGreaterThanZero(tlength)) {
550 /* get callback so we can do a meaningful dataVersion comparison */
551 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
552 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
554 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
555 lock_ReleaseWrite(&scp->rw);
559 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
560 /* we're past the end of file */
564 bp = buf_Find(scp, &tbase);
565 /* We cheat slightly by not locking the bp mutex. */
567 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
568 && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
576 /* if this buffer is essentially guaranteed to require a fetch,
577 * break out here and return this position.
582 tbase = LargeIntegerAdd(tbase, tblocksize);
583 tlength = LargeIntegerSubtract(tlength, tblocksize);
586 /* if we get here, either everything is fine or 'stop' stopped us at a
587 * particular buffer in the range that definitely needs to be fetched.
590 /* return non-zero code since realBasep won't be valid */
591 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
595 /* successfully found a page that will need fetching */
599 lock_ReleaseWrite(&scp->rw);
604 cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
612 if (scp->flags & CM_SCACHEFLAG_DELETED) {
613 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
617 /* Retries will be performed by the BkgDaemon thread if appropriate */
618 req.flags |= CM_REQ_NORETRY;
620 toffset.LowPart = p1;
621 toffset.HighPart = p2;
624 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
626 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, &req);
628 osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
632 * Keep the following list synchronized with the
633 * error code list in cm_BkgDaemon
636 case CM_ERROR_TIMEDOUT: /* or server restarting */
638 case CM_ERROR_WOULDBLOCK:
639 case CM_ERROR_ALLBUSY:
640 case CM_ERROR_ALLDOWN:
641 case CM_ERROR_ALLOFFLINE:
642 case CM_ERROR_PARTIALWRITE:
643 break; /* cm_BkgDaemon will re-insert the request in the queue */
646 lock_ObtainWrite(&scp->rw);
647 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
648 lock_ReleaseWrite(&scp->rw);
653 /* Called with scp locked */
654 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
659 end = LargeIntegerAdd(*base, *length);
660 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
661 scp->prefetch.base = *base;
662 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
663 scp->prefetch.end = end;
665 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
668 /* do the prefetch. if the prefetch fails, return 0 (success)
669 * because there is no harm done. */
671 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
679 osi_hyper_t tblocksize;
687 /* Retries will be performed by the BkgDaemon thread if appropriate */
688 req.flags |= CM_REQ_NORETRY;
691 fetched.HighPart = 0;
692 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
696 length.HighPart = p4;
698 end = LargeIntegerAdd(base, length);
700 osi_Log3(afsd_logp, "Starting BKG prefetch scp 0x%p, base 0x%x:%x", scp, p2, p1);
702 for ( code = 0, offset = base;
703 code == 0 && LargeIntegerLessThan(offset, end);
704 offset = LargeIntegerAdd(offset, tblocksize) )
707 lock_ReleaseWrite(&scp->rw);
711 code = buf_Get(scp, &offset, &bp);
715 if (bp->cmFlags & CM_BUF_CMFETCHING) {
716 /* skip this buffer as another thread is already fetching it */
723 lock_ObtainWrite(&scp->rw);
727 code = cm_GetBuffer(scp, bp, NULL, userp, &req);
729 fetched = LargeIntegerAdd(fetched, tblocksize);
734 lock_ObtainWrite(&scp->rw);
737 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
738 scp, &base, &fetched);
739 lock_ReleaseWrite(&scp->rw);
741 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p, code %d bytes 0x%x:%x",
742 scp, code, fetched.HighPart, fetched.LowPart);
746 /* a read was issued to offsetp, and we have to determine whether we should
747 * do a prefetch of the next chunk.
749 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
750 cm_user_t *userp, cm_req_t *reqp)
753 osi_hyper_t realBase;
754 osi_hyper_t readBase;
755 osi_hyper_t readLength;
758 /* round up to chunk boundary */
759 readBase.LowPart += (cm_chunkSize-1);
760 readBase.LowPart &= (-cm_chunkSize);
762 readLength = ConvertLongToLargeInteger(count);
764 lock_ObtainWrite(&scp->rw);
765 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
766 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
767 lock_ReleaseWrite(&scp->rw);
770 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
772 /* start the scan at the latter of the end of this read or
773 * the end of the last fetched region.
775 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
776 readBase = scp->prefetch.end;
778 lock_ReleaseWrite(&scp->rw);
780 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
783 return; /* can't find something to prefetch */
785 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
786 scp, realBase.LowPart);
788 cm_QueueBKGRequest(scp, cm_BkgPrefetch,
789 realBase.LowPart, realBase.HighPart,
790 readLength.LowPart, readLength.HighPart,
794 /* scp must be locked; temporarily unlocked during processing.
795 * If returns 0, returns buffers held in biop, and with
796 * CM_BUF_CMSTORING set.
798 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
799 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
800 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
801 * must be woken, and the event must be set when the I/O is done. All of this
802 * is required so that buf_WaitIO synchronizes properly with the buffer as it
803 * is being written out.
805 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
806 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
809 osi_queueData_t *qdp;
812 osi_hyper_t scanStart; /* where to start scan for dirty pages */
813 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
814 osi_hyper_t firstModOffset; /* offset of first modified page in range */
817 long flags; /* flags to cm_SyncOp */
819 /* clear things out */
820 biop->scp = scp; /* do not hold; held by caller */
821 biop->offset = *inOffsetp;
823 biop->bufListp = NULL;
824 biop->bufListEndp = NULL;
827 /* reserve a chunk's worth of buffers */
828 lock_ReleaseWrite(&scp->rw);
829 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
830 lock_ObtainWrite(&scp->rw);
833 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
834 thyper = ConvertLongToLargeInteger(temp);
835 tbase = LargeIntegerAdd(*inOffsetp, thyper);
837 bufp = buf_Find(scp, &tbase);
839 /* get buffer mutex and scp mutex safely */
840 lock_ReleaseWrite(&scp->rw);
841 lock_ObtainMutex(&bufp->mx);
842 lock_ObtainWrite(&scp->rw);
844 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
845 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
847 lock_ReleaseMutex(&bufp->mx);
850 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
854 /* if the buffer is dirty, we're done */
855 if (bufp->flags & CM_BUF_DIRTY) {
856 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
857 "WRITING w/o CMSTORING in SetupStoreBIOD");
858 bufp->flags |= CM_BUF_WRITING;
862 /* this buffer is clean, so there's no reason to process it */
863 cm_SyncOpDone(scp, bufp, flags);
864 lock_ReleaseMutex(&bufp->mx);
872 /* if we get here, if bufp is null, we didn't find any dirty buffers
873 * that weren't already being stored back, so we just quit now.
879 /* don't need buffer mutex any more */
880 lock_ReleaseMutex(&bufp->mx);
882 /* put this element in the list */
884 osi_SetQData(qdp, bufp);
885 /* don't have to hold bufp, since held by buf_Find above */
886 osi_QAddH((osi_queue_t **) &biop->bufListp,
887 (osi_queue_t **) &biop->bufListEndp,
889 biop->length = cm_data.buf_blockSize;
890 firstModOffset = bufp->offset;
891 biop->offset = firstModOffset;
892 bufp = NULL; /* this buffer and reference added to the queue */
894 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
895 scanStart = *inOffsetp;
896 scanStart.LowPart &= (-cm_chunkSize);
897 thyper = ConvertLongToLargeInteger(cm_chunkSize);
898 scanEnd = LargeIntegerAdd(scanStart, thyper);
900 flags = CM_SCACHESYNC_GETSTATUS
901 | CM_SCACHESYNC_STOREDATA
902 | CM_SCACHESYNC_BUFLOCKED
903 | CM_SCACHESYNC_NOWAIT;
905 /* start by looking backwards until scanStart */
906 /* hyper version of cm_data.buf_blockSize */
907 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
908 tbase = LargeIntegerSubtract(firstModOffset, thyper);
909 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
910 /* see if we can find the buffer */
911 bufp = buf_Find(scp, &tbase);
915 /* try to lock it, and quit if we can't (simplifies locking) */
916 lock_ReleaseWrite(&scp->rw);
917 code = lock_TryMutex(&bufp->mx);
918 lock_ObtainWrite(&scp->rw);
925 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
927 lock_ReleaseMutex(&bufp->mx);
933 if (!(bufp->flags & CM_BUF_DIRTY)) {
934 /* buffer is clean, so we shouldn't add it */
935 cm_SyncOpDone(scp, bufp, flags);
936 lock_ReleaseMutex(&bufp->mx);
942 /* don't need buffer mutex any more */
943 lock_ReleaseMutex(&bufp->mx);
945 /* we have a dirty buffer ready for storing. Add it to the tail
946 * of the list, since it immediately precedes all of the disk
947 * addresses we've already collected.
950 osi_SetQData(qdp, bufp);
951 /* no buf_hold necessary, since we have it held from buf_Find */
952 osi_QAddT((osi_queue_t **) &biop->bufListp,
953 (osi_queue_t **) &biop->bufListEndp,
955 bufp = NULL; /* added to the queue */
957 /* update biod info describing the transfer */
958 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
959 biop->length += cm_data.buf_blockSize;
961 /* update loop pointer */
962 tbase = LargeIntegerSubtract(tbase, thyper);
963 } /* while loop looking for pages preceding the one we found */
965 /* now, find later dirty, contiguous pages, and add them to the list */
966 /* hyper version of cm_data.buf_blockSize */
967 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
968 tbase = LargeIntegerAdd(firstModOffset, thyper);
969 while(LargeIntegerLessThan(tbase, scanEnd)) {
970 /* see if we can find the buffer */
971 bufp = buf_Find(scp, &tbase);
975 /* try to lock it, and quit if we can't (simplifies locking) */
976 lock_ReleaseWrite(&scp->rw);
977 code = lock_TryMutex(&bufp->mx);
978 lock_ObtainWrite(&scp->rw);
985 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
987 lock_ReleaseMutex(&bufp->mx);
993 if (!(bufp->flags & CM_BUF_DIRTY)) {
994 /* buffer is clean, so we shouldn't add it */
995 cm_SyncOpDone(scp, bufp, flags);
996 lock_ReleaseMutex(&bufp->mx);
1002 /* don't need buffer mutex any more */
1003 lock_ReleaseMutex(&bufp->mx);
1005 /* we have a dirty buffer ready for storing. Add it to the head
1006 * of the list, since it immediately follows all of the disk
1007 * addresses we've already collected.
1009 qdp = osi_QDAlloc();
1010 osi_SetQData(qdp, bufp);
1011 /* no buf_hold necessary, since we have it held from buf_Find */
1012 osi_QAddH((osi_queue_t **) &biop->bufListp,
1013 (osi_queue_t **) &biop->bufListEndp,
1017 /* update biod info describing the transfer */
1018 biop->length += cm_data.buf_blockSize;
1020 /* update loop pointer */
1021 tbase = LargeIntegerAdd(tbase, thyper);
1022 } /* while loop looking for pages following the first page we found */
1024 /* finally, we're done */
1028 /* scp must be locked; temporarily unlocked during processing.
1029 * If returns 0, returns buffers held in biop, and with
1030 * CM_BUF_CMFETCHING flags set.
1031 * If an error is returned, we don't return any buffers.
1033 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1034 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1038 osi_hyper_t tblocksize; /* a long long temp variable */
1039 osi_hyper_t pageBase; /* base offset we're looking at */
1040 osi_queueData_t *qdp; /* one temp queue structure */
1041 osi_queueData_t *tqdp; /* another temp queue structure */
1042 long collected; /* how many bytes have been collected */
1045 osi_hyper_t fileSize; /* the # of bytes in the file */
1046 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1047 osi_queueData_t *heldBufListEndp; /* first one */
1050 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1052 biop->scp = scp; /* do not hold; held by caller */
1053 biop->offset = *offsetp;
1054 /* null out the list of buffers */
1055 biop->bufListp = biop->bufListEndp = NULL;
1058 /* first lookup the file's length, so we know when to stop */
1059 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1060 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1064 /* copy out size, since it may change */
1065 fileSize = scp->serverLength;
1067 lock_ReleaseWrite(&scp->rw);
1069 pageBase = *offsetp;
1070 collected = pageBase.LowPart & (cm_chunkSize - 1);
1071 heldBufListp = NULL;
1072 heldBufListEndp = NULL;
1075 * Obtaining buffers can cause dirty buffers to be recycled, which
1076 * can cause a storeback, so cannot be done while we have buffers
1079 * To get around this, we get buffers twice. Before reserving buffers,
1080 * we obtain and release each one individually. After reserving
1081 * buffers, we try to obtain them again, but only by lookup, not by
1082 * recycling. If a buffer has gone away while we were waiting for
1083 * the others, we just use whatever buffers we already have.
1085 * On entry to this function, we are already holding a buffer, so we
1086 * can't wait for reservation. So we call buf_TryReserveBuffers()
1087 * instead. Not only that, we can't really even call buf_Get(), for
1088 * the same reason. We can't avoid that, though. To avoid deadlock
1089 * we allow only one thread to be executing the buf_Get()-buf_Release()
1090 * sequence at a time.
1093 /* first hold all buffers, since we can't hold any locks in buf_Get */
1095 /* stop at chunk boundary */
1096 if (collected >= cm_chunkSize)
1099 /* see if the next page would be past EOF */
1100 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1103 code = buf_Get(scp, &pageBase, &tbp);
1105 lock_ObtainWrite(&scp->rw);
1106 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1113 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1114 collected += cm_data.buf_blockSize;
1117 /* reserve a chunk's worth of buffers if possible */
1118 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1120 pageBase = *offsetp;
1121 collected = pageBase.LowPart & (cm_chunkSize - 1);
1123 /* now hold all buffers, if they are still there */
1125 /* stop at chunk boundary */
1126 if (collected >= cm_chunkSize)
1129 /* see if the next page would be past EOF */
1130 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1133 tbp = buf_Find(scp, &pageBase);
1137 /* add the buffer to the list */
1138 qdp = osi_QDAlloc();
1139 osi_SetQData(qdp, tbp);
1140 osi_QAddH((osi_queue_t **)&heldBufListp,
1141 (osi_queue_t **)&heldBufListEndp,
1143 /* leave tbp held (from buf_Get) */
1148 collected += cm_data.buf_blockSize;
1149 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1152 /* look at each buffer, adding it into the list if it looks idle and
1153 * filled with old data. One special case: wait for idle if it is the
1154 * first buffer since we really need that one for our caller to make
1158 collected = 0; /* now count how many we'll really use */
1159 for (tqdp = heldBufListEndp;
1161 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1162 /* get a ptr to the held buffer */
1163 tbp = osi_GetQData(tqdp);
1164 pageBase = tbp->offset;
1166 /* now lock the buffer lock */
1167 lock_ObtainMutex(&tbp->mx);
1168 lock_ObtainWrite(&scp->rw);
1170 /* don't bother fetching over data that is already current */
1171 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1172 /* we don't need this buffer, since it is current */
1173 lock_ReleaseWrite(&scp->rw);
1174 lock_ReleaseMutex(&tbp->mx);
1178 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1180 flags |= CM_SCACHESYNC_NOWAIT;
1182 /* wait for the buffer to serialize, if required. Doesn't
1183 * release the scp or buffer lock(s) if NOWAIT is specified.
1185 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1187 lock_ReleaseWrite(&scp->rw);
1188 lock_ReleaseMutex(&tbp->mx);
1192 /* don't fetch over dirty buffers */
1193 if (tbp->flags & CM_BUF_DIRTY) {
1194 cm_SyncOpDone(scp, tbp, flags);
1195 lock_ReleaseWrite(&scp->rw);
1196 lock_ReleaseMutex(&tbp->mx);
1201 lock_ReleaseWrite(&scp->rw);
1202 lock_ReleaseMutex(&tbp->mx);
1204 /* add the buffer to the list */
1205 qdp = osi_QDAlloc();
1206 osi_SetQData(qdp, tbp);
1207 osi_QAddH((osi_queue_t **)&biop->bufListp,
1208 (osi_queue_t **)&biop->bufListEndp,
1212 /* from now on, a failure just stops our collection process, but
1213 * we still do the I/O to whatever we've already managed to collect.
1216 collected += cm_data.buf_blockSize;
1219 /* now, we've held in biop->bufListp all the buffer's we're really
1220 * interested in. We also have holds left from heldBufListp, and we
1221 * now release those holds on the buffers.
1223 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1224 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1225 tbp = osi_GetQData(qdp);
1226 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1227 (osi_queue_t **) &heldBufListEndp,
1234 /* Caller expects this */
1235 lock_ObtainWrite(&scp->rw);
1237 /* if we got a failure setting up the first buffer, then we don't have
1238 * any side effects yet, and we also have failed an operation that the
1239 * caller requires to make any progress. Give up now.
1241 if (code && isFirst) {
1242 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1246 /* otherwise, we're still OK, and should just return the I/O setup we've
1249 biop->length = collected;
1250 biop->reserved = reserving;
1254 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1257 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
1259 cm_scache_t *scp; /* do not release; not held in biop */
1261 osi_queueData_t *qdp;
1262 osi_queueData_t *nqdp;
1265 /* Give back reserved buffers */
1267 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1270 flags = CM_SCACHESYNC_STOREDATA;
1272 flags = CM_SCACHESYNC_FETCHDATA;
1275 if (biop->bufListp) {
1276 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1277 /* lookup next guy first, since we're going to free this one */
1278 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1280 /* extract buffer and free queue data */
1281 bufp = osi_GetQData(qdp);
1282 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1283 (osi_queue_t **) &biop->bufListEndp,
1287 /* now, mark I/O as done, unlock the buffer and release it */
1288 lock_ObtainMutex(&bufp->mx);
1289 lock_ObtainWrite(&scp->rw);
1290 cm_SyncOpDone(scp, bufp, flags);
1292 /* turn off writing and wakeup users */
1294 if (bufp->flags & CM_BUF_WAITING) {
1295 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1296 osi_Wakeup((LONG_PTR) bufp);
1299 bufp->flags &= ~CM_BUF_WRITING;
1301 bufp->flags &= ~(CM_BUF_WRITING | CM_BUF_DIRTY);
1302 bufp->dirty_offset = bufp->dirty_length = 0;
1306 lock_ReleaseWrite(&scp->rw);
1307 lock_ReleaseMutex(&bufp->mx);
1312 lock_ObtainWrite(&scp->rw);
1313 cm_SyncOpDone(scp, NULL, flags);
1314 lock_ReleaseWrite(&scp->rw);
1317 /* clean things out */
1318 biop->bufListp = NULL;
1319 biop->bufListEndp = NULL;
1322 /* Fetch a buffer. Called with scp locked.
1323 * The scp is locked on return.
1325 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1329 afs_int32 nbytes; /* bytes in transfer */
1330 afs_int32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1331 afs_int64 length_found = 0;
1332 long rbytes; /* bytes in rx_Read call */
1334 AFSFetchStatus afsStatus;
1335 AFSCallBack callback;
1338 cm_buf_t *tbufp; /* buf we're filling */
1339 osi_queueData_t *qdp; /* q element we're scanning */
1341 struct rx_call *rxcallp;
1342 struct rx_connection *rxconnp;
1343 cm_bulkIO_t biod; /* bulk IO descriptor */
1347 int require_64bit_ops = 0;
1349 /* now, the buffer may or may not be filled with good data (buf_GetNew
1350 * drops lots of locks, and may indeed return a properly initialized
1351 * buffer, although more likely it will just return a new, empty, buffer.
1354 #ifdef AFS_FREELANCE_CLIENT
1356 // yj: if they're trying to get the /afs directory, we need to
1357 // handle it differently, since it's local rather than on any
1360 getroot = (scp==cm_data.rootSCachep);
1362 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1365 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1366 /* We already have this buffer don't do extra work */
1370 cm_AFSFidFromFid(&tfid, &scp->fid);
1372 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1374 /* couldn't even get the first page setup properly */
1375 osi_Log1(afsd_logp, "GetBuffer: SetupFetchBIOD failure code %d", code);
1379 /* once we get here, we have the callback in place, we know that no one
1380 * is fetching the data now. Check one last time that we still have
1381 * the wrong data, and then fetch it if we're still wrong.
1383 * We can lose a race condition and end up with biod.length zero, in
1384 * which case we just retry.
1386 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
1387 if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow) &&
1388 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength))
1390 osi_Log4(afsd_logp, "Bad DVs 0x%x != (0x%x -> 0x%x) or length 0x%x",
1391 bufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion, biod.length);
1393 if (bufp->dataVersion == CM_BUF_VERSION_BAD)
1394 memset(bufp->datap, 0, cm_data.buf_blockSize);
1395 bufp->dataVersion = scp->dataVersion;
1397 lock_ReleaseWrite(&scp->rw);
1398 cm_ReleaseBIOD(&biod, 0, 0);
1399 lock_ObtainWrite(&scp->rw);
1401 } else if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow)
1402 && (scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1403 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->truncPos)) {
1404 memset(bufp->datap, 0, cm_data.buf_blockSize);
1405 bufp->dataVersion = scp->dataVersion;
1406 lock_ReleaseWrite(&scp->rw);
1407 cm_ReleaseBIOD(&biod, 0, 0);
1408 lock_ObtainWrite(&scp->rw);
1412 lock_ReleaseWrite(&scp->rw);
1414 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1415 ConvertLongToLargeInteger(biod.length)),
1416 ConvertLongToLargeInteger(LONG_MAX))) {
1417 require_64bit_ops = 1;
1420 osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1421 osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1422 scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1424 #ifdef AFS_FREELANCE_CLIENT
1427 // if getroot then we don't need to make any calls
1428 // just return fake data
1430 if (cm_freelanceEnabled && getroot) {
1431 // setup the fake status
1432 afsStatus.InterfaceVersion = 0x1;
1433 afsStatus.FileType = 0x2;
1434 afsStatus.LinkCount = scp->linkCount;
1435 afsStatus.Length = cm_fakeDirSize;
1436 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1437 afsStatus.Author = 0x1;
1438 afsStatus.Owner = 0x0;
1439 afsStatus.CallerAccess = 0x9;
1440 afsStatus.AnonymousAccess = 0x9;
1441 afsStatus.UnixModeBits = 0x1ff;
1442 afsStatus.ParentVnode = 0x1;
1443 afsStatus.ParentUnique = 0x1;
1444 afsStatus.ResidencyMask = 0;
1445 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1446 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1447 afsStatus.Group = 0;
1448 afsStatus.SyncCounter = 0;
1449 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1450 afsStatus.lockCount = 0;
1451 afsStatus.Length_hi = 0;
1452 afsStatus.errorCode = 0;
1454 // once we're done setting up the status info,
1455 // we just fill the buffer pages with fakedata
1456 // from cm_FakeRootDir. Extra pages are set to
1459 lock_ObtainMutex(&cm_Freelance_Lock);
1460 t1 = bufp->offset.LowPart;
1461 qdp = biod.bufListEndp;
1463 tbufp = osi_GetQData(qdp);
1464 bufferp=tbufp->datap;
1465 memset(bufferp, 0, cm_data.buf_blockSize);
1466 t2 = cm_fakeDirSize - t1;
1467 if (t2> (afs_int32)cm_data.buf_blockSize)
1468 t2=cm_data.buf_blockSize;
1470 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1475 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1478 lock_ReleaseMutex(&cm_Freelance_Lock);
1480 // once we're done, we skip over the part of the
1481 // code that does the ACTUAL fetching of data for
1484 goto fetchingcompleted;
1487 #endif /* AFS_FREELANCE_CLIENT */
1489 /* now make the call */
1491 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1495 rxconnp = cm_GetRxConn(connp);
1496 rxcallp = rx_NewCall(rxconnp);
1497 rx_PutConnection(rxconnp);
1499 #ifdef AFS_LARGEFILES
1500 nbytes = nbytes_hi = 0;
1502 if (SERVERHAS64BIT(connp)) {
1503 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1504 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1506 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1509 temp = rx_Read32(rxcallp, &nbytes_hi);
1510 if (temp == sizeof(afs_int32)) {
1511 nbytes_hi = ntohl(nbytes_hi);
1514 code = rxcallp->error;
1515 rx_EndCall(rxcallp, code);
1521 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1522 if (require_64bit_ops) {
1523 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1524 code = CM_ERROR_TOOBIG;
1527 rxconnp = cm_GetRxConn(connp);
1528 rxcallp = rx_NewCall(rxconnp);
1529 rx_PutConnection(rxconnp);
1532 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1533 scp, biod.offset.LowPart, biod.length);
1535 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1538 SET_SERVERHASNO64BIT(connp);
1543 temp = rx_Read32(rxcallp, &nbytes);
1544 if (temp == sizeof(afs_int32)) {
1545 nbytes = ntohl(nbytes);
1546 FillInt64(length_found, nbytes_hi, nbytes);
1547 if (length_found > biod.length)
1548 code = (rxcallp->error < 0) ? rxcallp->error : -1;
1550 code = (rxcallp->error < 0) ? rxcallp->error : -1;
1553 /* for the moment, nbytes_hi will always be 0 if code == 0
1554 because biod.length is a 32-bit quantity. */
1556 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1557 scp, biod.offset.LowPart, biod.length);
1559 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1562 /* now copy the data out of the pipe and put it in the buffer */
1564 temp = rx_Read32(rxcallp, &nbytes);
1565 if (temp == sizeof(afs_int32)) {
1566 nbytes = ntohl(nbytes);
1567 if (nbytes > biod.length)
1568 code = (rxcallp->error < 0) ? rxcallp->error : -1;
1571 code = (rxcallp->error < 0) ? rxcallp->error : -1;
1576 qdp = biod.bufListEndp;
1578 tbufp = osi_GetQData(qdp);
1579 bufferp = tbufp->datap;
1583 /* fill nbytes of data from the pipe into the pages.
1584 * When we stop, qdp will point at the last page we're
1585 * dealing with, and bufferp will tell us where we
1586 * stopped. We'll need this info below when we clear
1587 * the remainder of the last page out (and potentially
1588 * clear later pages out, if we fetch past EOF).
1590 while (nbytes > 0) {
1591 /* assert that there are still more buffers;
1592 * our check above for nbytes being less than
1593 * biod.length should ensure this.
1595 osi_assertx(bufferp != NULL, "null cm_buf_t");
1597 /* read rbytes of data */
1598 rbytes = (nbytes > cm_data.buf_blockSize? cm_data.buf_blockSize : nbytes);
1599 temp = rx_Read(rxcallp, bufferp, rbytes);
1600 if (temp < rbytes) {
1601 code = (rxcallp->error < 0) ? rxcallp->error : -1;
1605 /* allow read-while-fetching.
1606 * if this is the last buffer, clear the
1607 * PREFETCHING flag, so the reader waiting for
1608 * this buffer will start a prefetch.
1610 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1611 lock_ObtainWrite(&scp->rw);
1612 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1613 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
1614 osi_Wakeup((LONG_PTR) &scp->flags);
1616 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1617 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
1619 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
1621 lock_ReleaseWrite(&scp->rw);
1623 /* and adjust counters */
1626 /* and move to the next buffer */
1628 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1630 tbufp = osi_GetQData(qdp);
1631 bufferp = tbufp->datap;
1639 /* zero out remainder of last pages, in case we are
1640 * fetching past EOF. We were fetching an integral #
1641 * of pages, but stopped, potentially in the middle of
1642 * a page. Zero the remainder of that page, and then
1643 * all of the rest of the pages.
1646 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
1647 rbytes = (long) (bufferp - tbufp->datap);
1649 /* bytes left to zero */
1650 rbytes = cm_data.buf_blockSize - rbytes;
1653 memset(bufferp, 0, rbytes);
1654 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1657 tbufp = osi_GetQData(qdp);
1658 bufferp = tbufp->datap;
1659 /* bytes to clear in this page */
1660 rbytes = cm_data.buf_blockSize;
1665 if (SERVERHAS64BIT(connp))
1666 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
1668 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
1670 if (SERVERHAS64BIT(connp))
1671 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
1673 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
1677 code = rx_EndCall(rxcallp, code);
1679 if (code == RXKADUNKNOWNKEY)
1680 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
1682 osi_Log0(afsd_logp, "CALL FetchData DONE");
1684 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
1687 code = cm_MapRPCError(code, reqp);
1689 lock_ObtainWrite(&scp->rw);
1691 /* we know that no one else has changed the buffer, since we still have
1692 * the fetching flag on the buffers, and we have the scp locked again.
1693 * Copy in the version # into the buffer if we got code 0 back from the
1697 for(qdp = biod.bufListp;
1699 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1700 tbufp = osi_GetQData(qdp);
1701 tbufp->dataVersion = afsStatus.dataVersionHigh;
1702 tbufp->dataVersion <<= 32;
1703 tbufp->dataVersion |= afsStatus.DataVersion;
1706 /* write buffer out to disk cache */
1707 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
1708 tbufp->dataVersion);
1709 #endif /* DISKCACHE95 */
1713 /* release scatter/gather I/O structure (buffers, locks) */
1714 lock_ReleaseWrite(&scp->rw);
1715 cm_ReleaseBIOD(&biod, 0, code);
1716 lock_ObtainWrite(&scp->rw);
1719 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, 0);