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
11 #include <afsconfig.h>
12 #include <afs/param.h>
27 extern void afsi_log(char *pattern, ...);
30 #ifdef AFS_FREELANCE_CLIENT
31 extern osi_mutex_t cm_Freelance_Lock;
34 #define USE_RX_IOVEC 1
36 /* we can access connp->serverp without holding a lock because that
37 never changes since the connection is made. */
38 #define SERVERHAS64BIT(connp) (!((connp)->serverp->flags & CM_SERVERFLAG_NO64BIT))
39 #define SET_SERVERHASNO64BIT(connp) (cm_SetServerNo64Bit((connp)->serverp, TRUE))
41 /* functions called back from the buffer package when reading or writing data,
42 * or when holding or releasing a vnode pointer.
44 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
45 cm_user_t *userp, cm_req_t *reqp)
47 /* store the data back from this buffer; the buffer is locked and held,
48 * but the vnode involved isn't locked, yet. It is held by its
49 * reference from the buffer, which won't change until the buffer is
50 * released by our caller. Thus, we don't have to worry about holding
54 cm_scache_t *scp = vscp;
56 afs_int32 save_nbytes;
58 AFSFetchStatus outStatus;
59 AFSStoreStatus inStatus;
63 struct rx_call *rxcallp;
64 struct rx_connection *rxconnp;
71 cm_bulkIO_t biod; /* bulk IO descriptor */
72 int require_64bit_ops = 0;
73 int call_was_64bit = 0;
74 int scp_locked = flags & CM_BUF_WRITE_SCP_LOCKED;
76 osi_assertx(userp != NULL, "null cm_user_t");
77 osi_assertx(scp != NULL, "null cm_scache_t");
79 memset(&volSync, 0, sizeof(volSync));
81 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
82 * drops lots of locks, and may indeed return a properly initialized
83 * buffer, although more likely it will just return a new, empty, buffer.
86 lock_ObtainWrite(&scp->rw);
87 if (scp->flags & CM_SCACHEFLAG_DELETED) {
89 lock_ReleaseWrite(&scp->rw);
90 return CM_ERROR_NOSUCHFILE;
93 cm_AFSFidFromFid(&tfid, &scp->fid);
95 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
96 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
98 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
100 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
101 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
103 lock_ReleaseWrite(&scp->rw);
107 if (biod.length == 0) {
108 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
109 cm_ReleaseBIOD(&biod, 1, 0, 1); /* should be a NOOP */
110 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
112 lock_ReleaseWrite(&scp->rw);
116 /* prepare the output status for the store */
117 _InterlockedOr(&scp->mask, CM_SCACHEMASK_CLIENTMODTIME);
118 cm_StatusFromAttr(&inStatus, scp, NULL);
119 truncPos = scp->length;
120 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
121 && LargeIntegerLessThan(scp->truncPos, truncPos)) {
122 truncPos = scp->truncPos;
123 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_TRUNCPOS);
126 /* compute how many bytes to write from this buffer */
127 thyper = LargeIntegerSubtract(scp->length, biod.offset);
128 if (LargeIntegerLessThanZero(thyper)) {
129 /* entire buffer is past EOF */
133 /* otherwise write out part of buffer before EOF, but not
134 * more than bufferSize bytes.
136 if (LargeIntegerGreaterThan(thyper,
137 ConvertLongToLargeInteger(biod.length))) {
138 nbytes = biod.length;
140 /* if thyper is less than or equal to biod.length, then we
141 can safely assume that the value fits in a long. */
142 nbytes = thyper.LowPart;
146 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
147 ConvertLongToLargeInteger(nbytes)),
148 ConvertLongToLargeInteger(LONG_MAX)) ||
149 LargeIntegerGreaterThan(truncPos,
150 ConvertLongToLargeInteger(LONG_MAX))) {
151 require_64bit_ops = 1;
154 lock_ReleaseWrite(&scp->rw);
156 /* now we're ready to do the store operation */
157 save_nbytes = nbytes;
159 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
164 rxconnp = cm_GetRxConn(connp);
165 rxcallp = rx_NewCall(rxconnp);
166 rx_PutConnection(rxconnp);
168 if (SERVERHAS64BIT(connp)) {
171 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
172 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
173 osi_Log2(afsd_logp, "... truncPos 0x%x:%08x", truncPos.HighPart, truncPos.LowPart);
175 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
176 biod.offset.QuadPart,
180 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
182 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
186 if (require_64bit_ops) {
187 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData. The operation requires large file support in the server.");
188 code = CM_ERROR_TOOBIG;
190 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
191 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
193 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
194 biod.offset.LowPart, nbytes, truncPos.LowPart);
196 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
198 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
203 afs_uint32 buf_offset = 0, bytes_copied = 0;
205 /* write the data from the the list of buffers */
209 struct iovec tiov[RX_MAXIOVECS];
210 afs_int32 tnio, vlen, vbytes, iov, voffset;
213 vbytes = rx_WritevAlloc(rxcallp, tiov, &tnio, RX_MAXIOVECS, nbytes);
215 code = RX_PROTOCOL_ERROR;
219 for ( iov = voffset = vlen = 0;
220 vlen < vbytes && iov < tnio; vlen += wbytes) {
222 qdp = biod.bufListEndp;
223 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
224 } else if (buf_offset == cm_data.buf_blockSize) {
225 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
229 osi_assertx(qdp != NULL, "null osi_queueData_t");
230 bufp = osi_GetQData(qdp);
231 bufferp = bufp->datap + buf_offset;
232 wbytes = vbytes - vlen;
233 if (wbytes > cm_data.buf_blockSize - buf_offset)
234 wbytes = cm_data.buf_blockSize - buf_offset;
236 vleft = tiov[iov].iov_len - voffset;
237 while (wbytes > vleft && iov < tnio) {
238 memcpy(tiov[iov].iov_base + voffset, bufferp, vleft);
239 bytes_copied += vleft;
247 vleft = tiov[iov].iov_len;
251 memcpy(tiov[iov].iov_base + voffset, bufferp, wbytes);
252 bytes_copied += wbytes;
253 if (tiov[iov].iov_len == voffset + wbytes) {
256 vleft = (iov < tnio) ? tiov[iov].iov_len : 0;
262 buf_offset += wbytes;
268 osi_assertx(iov == tnio, "incorrect iov count");
269 osi_assertx(vlen == vbytes, "bytes_copied != vbytes");
270 osi_assertx(bufp->offset.QuadPart + buf_offset == biod.offset.QuadPart + bytes_copied,
271 "begin and end offsets don't match");
273 temp = rx_Writev(rxcallp, tiov, tnio, vbytes);
274 if (temp != vbytes) {
275 osi_Log3(afsd_logp, "rx_Writev failed bp 0x%p, %d != %d", bufp, temp, vbytes);
276 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
280 osi_Log2(afsd_logp, "rx_Writev succeeded iov offset 0x%x, wrote 0x%x",
281 (unsigned long)(bufp->offset.QuadPart + buf_offset - vbytes), vbytes);
283 #else /* USE_RX_IOVEC */
285 qdp = biod.bufListEndp;
286 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
288 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
292 osi_assertx(qdp != NULL, "null osi_queueData_t");
293 bufp = osi_GetQData(qdp);
294 bufferp = bufp->datap + buf_offset;
296 if (wbytes > cm_data.buf_blockSize - buf_offset)
297 wbytes = cm_data.buf_blockSize - buf_offset;
299 /* write out wbytes of data from bufferp */
300 temp = rx_Write(rxcallp, bufferp, wbytes);
301 if (temp != wbytes) {
302 osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d", bufp, temp, wbytes);
303 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
306 osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p written %d", bufp, temp);
309 #endif /* USE_RX_IOVEC */
310 } /* while more bytes to write */
311 } /* if RPC started successfully */
314 if (call_was_64bit) {
315 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
317 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
319 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
321 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
323 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
325 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
329 code1 = rx_EndCall(rxcallp, code);
331 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
332 SET_SERVERHASNO64BIT(connp);
334 nbytes = save_nbytes;
338 /* Prefer StoreData error over rx_EndCall error */
341 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
343 code = cm_MapRPCError(code, reqp);
346 osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
348 osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
350 /* now, clean up our state */
351 lock_ObtainWrite(&scp->rw);
353 cm_ReleaseBIOD(&biod, 1, code, 1);
357 /* now, here's something a little tricky: in AFS 3, a dirty
358 * length can't be directly stored, instead, a dirty chunk is
359 * stored that sets the file's size (by writing and by using
360 * the truncate-first option in the store call).
362 * At this point, we've just finished a store, and so the trunc
363 * pos field is clean. If the file's size at the server is at
364 * least as big as we think it should be, then we turn off the
365 * length dirty bit, since all the other dirty buffers must
366 * precede this one in the file.
368 * The file's desired size shouldn't be smaller than what's
369 * stored at the server now, since we just did the trunc pos
372 * We have to turn off the length dirty bit as soon as we can,
373 * so that we see updates made by other machines.
376 if (call_was_64bit) {
377 t.LowPart = outStatus.Length;
378 t.HighPart = outStatus.Length_hi;
380 t = ConvertLongToLargeInteger(outStatus.Length);
383 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
384 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
386 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
388 if (code == CM_ERROR_SPACE)
389 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OUTOFSPACE);
390 else if (code == CM_ERROR_QUOTA)
391 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OVERQUOTA);
393 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
396 lock_ReleaseWrite(&scp->rw);
402 * Truncate the file, by sending a StoreData RPC with zero length.
404 * Called with scp locked. Releases and re-obtains the lock.
406 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
408 AFSFetchStatus outStatus;
409 AFSStoreStatus inStatus;
413 osi_hyper_t truncPos;
415 struct rx_call *rxcallp;
416 struct rx_connection *rxconnp;
417 int require_64bit_ops = 0;
418 int call_was_64bit = 0;
420 memset(&volSync, 0, sizeof(volSync));
422 osi_Log2(afsd_logp, "cm_StoreMini scp 0x%p userp 0x%p", scp, userp);
424 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
425 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
426 CM_SCACHESYNC_STOREDATA_EXCL);
428 /* prepare the output status for the store */
429 inStatus.Mask = AFS_SETMODTIME;
430 inStatus.ClientModTime = scp->clientModTime;
431 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_CLIENTMODTIME);
433 /* calculate truncation position */
434 truncPos = scp->length;
435 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
436 && LargeIntegerLessThan(scp->truncPos, truncPos))
437 truncPos = scp->truncPos;
438 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_TRUNCPOS);
440 if (LargeIntegerGreaterThan(truncPos,
441 ConvertLongToLargeInteger(LONG_MAX))) {
443 require_64bit_ops = 1;
446 lock_ReleaseWrite(&scp->rw);
448 cm_AFSFidFromFid(&tfid, &scp->fid);
450 /* now we're ready to do the store operation */
452 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
457 rxconnp = cm_GetRxConn(connp);
458 rxcallp = rx_NewCall(rxconnp);
459 rx_PutConnection(rxconnp);
461 if (SERVERHAS64BIT(connp)) {
464 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, truncPos 0x%x:%08x",
465 scp, truncPos.HighPart, truncPos.LowPart);
467 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
468 0, 0, truncPos.QuadPart);
470 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
472 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
476 if (require_64bit_ops) {
477 code = CM_ERROR_TOOBIG;
479 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, truncPos 0x%x:%08x",
480 scp, truncPos.HighPart, truncPos.LowPart);
482 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
483 0, 0, truncPos.LowPart);
485 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
487 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
492 if (call_was_64bit) {
493 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
495 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
497 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
499 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
501 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX", scp, code);
503 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
506 code1 = rx_EndCall(rxcallp, code);
508 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
509 SET_SERVERHASNO64BIT(connp);
513 /* prefer StoreData error over rx_EndCall error */
514 if (code == 0 && code1 != 0)
516 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
517 code = cm_MapRPCError(code, reqp);
519 /* now, clean up our state */
520 lock_ObtainWrite(&scp->rw);
525 * For explanation of handling of CM_SCACHEMASK_LENGTH,
528 if (call_was_64bit) {
529 t.HighPart = outStatus.Length_hi;
530 t.LowPart = outStatus.Length;
532 t = ConvertLongToLargeInteger(outStatus.Length);
535 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
536 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
537 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
539 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
544 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
548 /* now return a code that means that I/O is done */
553 * stabilize scache entry with CM_SCACHESYNC_SETSIZE. This prevents any new
554 * data buffers to be allocated, new data to be fetched from the file server,
555 * and writes to be accepted from the application but permits dirty buffers
556 * to be written to the file server.
558 * Stabilize uses cm_SyncOp to maintain the cm_scache_t in this stable state
559 * instead of holding the rwlock exclusively. This permits background stores
560 * to be performed in parallel and in particular allow FlushFile to be
561 * implemented without violating the locking hierarchy.
563 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
565 cm_scache_t *scp = vscp;
568 lock_ObtainWrite(&scp->rw);
569 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
570 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
571 lock_ReleaseWrite(&scp->rw);
576 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
577 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
579 cm_scache_t *scp = vscp;
581 lock_ObtainWrite(&scp->rw);
582 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
584 lock_ReleaseWrite(&scp->rw);
586 /* always succeeds */
590 cm_buf_ops_t cm_bufOps = {
597 long cm_ValidateDCache(void)
599 return buf_ValidateBuffers();
602 long cm_ShutdownDCache(void)
607 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
609 return buf_Init(newFile, &cm_bufOps, nbuffers);
612 /* check to see if we have an up-to-date buffer. The buffer must have
613 * previously been obtained by calling buf_Get.
615 * Make sure we have a callback, and that the dataversion matches.
617 * Scp must be locked.
619 * Bufp *may* be locked.
621 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
624 if (!cm_HaveCallback(scp))
626 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
628 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
630 if (bufp->offset.QuadPart >= scp->serverLength.QuadPart)
633 code = lock_TryMutex(&bufp->mx);
635 /* don't have the lock, and can't lock it, then
642 /* remember dirty flag for later */
643 code = bufp->flags & CM_BUF_DIRTY;
645 /* release lock if we obtained it here */
647 lock_ReleaseMutex(&bufp->mx);
649 /* if buffer was dirty, buffer is acceptable for use */
657 * used when deciding whether to do a background fetch or not.
658 * call with scp->rw write-locked.
661 cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
662 cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
666 osi_hyper_t tblocksize;
671 /* now scan all buffers in the range, looking for any that look like
676 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
678 while (LargeIntegerGreaterThanZero(tlength)) {
679 /* get callback so we can do a meaningful dataVersion comparison */
680 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
681 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
685 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
686 /* we're past the end of file */
690 bp = buf_Find(&scp->fid, &tbase);
691 /* We cheat slightly by not locking the bp mutex. */
693 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
694 && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
702 /* if this buffer is essentially guaranteed to require a fetch,
703 * break out here and return this position.
708 tbase = LargeIntegerAdd(tbase, tblocksize);
709 tlength = LargeIntegerSubtract(tlength, tblocksize);
712 /* if we get here, either everything is fine or 'stop' stopped us at a
713 * particular buffer in the range that definitely needs to be fetched.
716 /* return non-zero code since realBasep won't be valid */
720 /* successfully found a page that will need fetching */
728 cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
729 cm_user_t *userp, cm_req_t *reqp)
735 if (scp->flags & CM_SCACHEFLAG_DELETED) {
736 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
738 /* Retries will be performed by the BkgDaemon thread if appropriate */
739 afs_uint32 req_flags = reqp->flags;
740 reqp->flags |= CM_REQ_NORETRY;
742 toffset.LowPart = p1;
743 toffset.HighPart = p2;
746 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
748 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, reqp);
750 osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
752 reqp->flags = req_flags;
756 * Keep the following list synchronized with the
757 * error code list in cm_BkgDaemon
760 case CM_ERROR_TIMEDOUT: /* or server restarting */
762 case CM_ERROR_WOULDBLOCK:
763 case CM_ERROR_ALLBUSY:
764 case CM_ERROR_ALLDOWN:
765 case CM_ERROR_ALLOFFLINE:
766 case CM_ERROR_PARTIALWRITE:
767 break; /* cm_BkgDaemon will re-insert the request in the queue */
770 lock_ObtainWrite(&scp->rw);
771 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
772 lock_ReleaseWrite(&scp->rw);
778 /* Called with scp locked */
779 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
784 end = LargeIntegerAdd(*base, *length);
785 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
786 scp->prefetch.base = *base;
787 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
788 scp->prefetch.end = end;
790 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
793 /* do the prefetch. if the prefetch fails, return 0 (success)
794 * because there is no harm done. */
796 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
797 cm_user_t *userp, cm_req_t *reqp)
804 osi_hyper_t tblocksize;
808 afs_uint32 req_flags;
810 /* Retries will be performed by the BkgDaemon thread if appropriate */
811 req_flags = reqp->flags;
812 reqp->flags |= CM_REQ_NORETRY;
815 fetched.HighPart = 0;
816 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
820 length.HighPart = p4;
822 end = LargeIntegerAdd(base, length);
824 osi_Log5(afsd_logp, "Starting BKG prefetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
825 scp, p2, p1, p4, p3);
827 for ( code = 0, offset = base;
828 code == 0 && LargeIntegerLessThan(offset, end);
829 offset = LargeIntegerAdd(offset, tblocksize) )
832 lock_ReleaseWrite(&scp->rw);
836 code = buf_Get(scp, &offset, reqp, &bp);
840 if (bp->cmFlags & CM_BUF_CMFETCHING) {
841 /* skip this buffer as another thread is already fetching it */
843 lock_ObtainWrite(&scp->rw);
852 lock_ObtainWrite(&scp->rw);
856 code = cm_GetBuffer(scp, bp, NULL, userp, reqp);
858 fetched = LargeIntegerAdd(fetched, tblocksize);
863 lock_ObtainWrite(&scp->rw);
867 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
868 scp, &base, &fetched);
870 /* wakeup anyone who is waiting */
871 if (scp->flags & CM_SCACHEFLAG_WAITING) {
872 osi_Log1(afsd_logp, "CM BkgPrefetch Waking scp 0x%p", scp);
873 osi_Wakeup((LONG_PTR) &scp->flags);
875 lock_ReleaseWrite(&scp->rw);
877 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p code 0x%x fetched 0x%x:%x",
878 scp, code, fetched.HighPart, fetched.LowPart);
880 reqp->flags = req_flags;
884 /* a read was issued to offsetp, and we have to determine whether we should
885 * do a prefetch of the next chunk.
887 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
888 cm_user_t *userp, cm_req_t *reqp)
892 osi_hyper_t realBase;
893 osi_hyper_t readBase;
894 osi_hyper_t readLength;
896 osi_hyper_t tblocksize; /* a long long temp variable */
898 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
901 /* round up to chunk boundary */
902 readBase.LowPart += (cm_chunkSize-1);
903 readBase.LowPart &= (-cm_chunkSize);
905 readLength = ConvertLongToLargeInteger(count);
907 lock_ObtainWrite(&scp->rw);
909 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
910 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
911 lock_ReleaseWrite(&scp->rw);
914 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_PREFETCHING);
916 /* start the scan at the latter of the end of this read or
917 * the end of the last fetched region.
919 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
920 readBase = scp->prefetch.end;
922 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
925 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
926 lock_ReleaseWrite(&scp->rw);
927 return; /* can't find something to prefetch */
930 readEnd = LargeIntegerAdd(realBase, readLength);
933 lock_ReleaseWrite(&scp->rw);
935 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
936 scp, realBase.LowPart);
938 cm_QueueBKGRequest(scp, cm_BkgPrefetch,
939 realBase.LowPart, realBase.HighPart,
940 readLength.LowPart, readLength.HighPart,
944 /* scp must be locked; temporarily unlocked during processing.
945 * If returns 0, returns buffers held in biop, and with
946 * CM_BUF_CMSTORING set.
948 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
949 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
950 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
951 * must be woken, and the event must be set when the I/O is done. All of this
952 * is required so that buf_WaitIO synchronizes properly with the buffer as it
953 * is being written out.
955 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
956 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
959 osi_queueData_t *qdp;
962 osi_hyper_t scanStart; /* where to start scan for dirty pages */
963 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
964 osi_hyper_t firstModOffset; /* offset of first modified page in range */
967 long flags; /* flags to cm_SyncOp */
969 /* clear things out */
970 biop->scp = scp; /* do not hold; held by caller */
971 biop->offset = *inOffsetp;
973 biop->bufListp = NULL;
974 biop->bufListEndp = NULL;
977 /* reserve a chunk's worth of buffers */
978 lock_ReleaseWrite(&scp->rw);
979 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
980 lock_ObtainWrite(&scp->rw);
983 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
984 thyper = ConvertLongToLargeInteger(temp);
985 tbase = LargeIntegerAdd(*inOffsetp, thyper);
987 bufp = buf_Find(&scp->fid, &tbase);
989 /* get buffer mutex and scp mutex safely */
990 lock_ReleaseWrite(&scp->rw);
991 lock_ObtainMutex(&bufp->mx);
994 * if the buffer is actively involved in I/O
995 * we wait for the I/O to complete.
997 if (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING))
998 buf_WaitIO(scp, bufp);
1000 lock_ObtainWrite(&scp->rw);
1001 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
1002 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1004 lock_ReleaseMutex(&bufp->mx);
1007 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1011 /* if the buffer is dirty, we're done */
1012 if (bufp->flags & CM_BUF_DIRTY) {
1013 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
1014 "WRITING w/o CMSTORING in SetupStoreBIOD");
1015 _InterlockedOr(&bufp->flags, CM_BUF_WRITING);
1019 /* this buffer is clean, so there's no reason to process it */
1020 cm_SyncOpDone(scp, bufp, flags);
1021 lock_ReleaseMutex(&bufp->mx);
1029 /* if we get here, if bufp is null, we didn't find any dirty buffers
1030 * that weren't already being stored back, so we just quit now.
1036 /* don't need buffer mutex any more */
1037 lock_ReleaseMutex(&bufp->mx);
1039 /* put this element in the list */
1040 qdp = osi_QDAlloc();
1041 osi_SetQData(qdp, bufp);
1042 /* don't have to hold bufp, since held by buf_Find above */
1043 osi_QAddH((osi_queue_t **) &biop->bufListp,
1044 (osi_queue_t **) &biop->bufListEndp,
1046 biop->length = cm_data.buf_blockSize;
1047 firstModOffset = bufp->offset;
1048 biop->offset = firstModOffset;
1049 bufp = NULL; /* this buffer and reference added to the queue */
1051 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
1052 scanStart = *inOffsetp;
1053 scanStart.LowPart &= (-cm_chunkSize);
1054 thyper = ConvertLongToLargeInteger(cm_chunkSize);
1055 scanEnd = LargeIntegerAdd(scanStart, thyper);
1057 flags = CM_SCACHESYNC_GETSTATUS
1058 | CM_SCACHESYNC_STOREDATA
1059 | CM_SCACHESYNC_BUFLOCKED
1060 | CM_SCACHESYNC_NOWAIT;
1062 /* start by looking backwards until scanStart */
1063 /* hyper version of cm_data.buf_blockSize */
1064 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1065 tbase = LargeIntegerSubtract(firstModOffset, thyper);
1066 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
1067 /* see if we can find the buffer */
1068 bufp = buf_Find(&scp->fid, &tbase);
1072 /* try to lock it, and quit if we can't (simplifies locking) */
1073 lock_ReleaseWrite(&scp->rw);
1074 code = lock_TryMutex(&bufp->mx);
1075 lock_ObtainWrite(&scp->rw);
1082 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1084 lock_ReleaseMutex(&bufp->mx);
1090 if (!(bufp->flags & CM_BUF_DIRTY)) {
1091 /* buffer is clean, so we shouldn't add it */
1092 cm_SyncOpDone(scp, bufp, flags);
1093 lock_ReleaseMutex(&bufp->mx);
1099 /* don't need buffer mutex any more */
1100 lock_ReleaseMutex(&bufp->mx);
1102 /* we have a dirty buffer ready for storing. Add it to the tail
1103 * of the list, since it immediately precedes all of the disk
1104 * addresses we've already collected.
1106 qdp = osi_QDAlloc();
1107 osi_SetQData(qdp, bufp);
1108 /* no buf_hold necessary, since we have it held from buf_Find */
1109 osi_QAddT((osi_queue_t **) &biop->bufListp,
1110 (osi_queue_t **) &biop->bufListEndp,
1112 bufp = NULL; /* added to the queue */
1114 /* update biod info describing the transfer */
1115 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
1116 biop->length += cm_data.buf_blockSize;
1118 /* update loop pointer */
1119 tbase = LargeIntegerSubtract(tbase, thyper);
1120 } /* while loop looking for pages preceding the one we found */
1122 /* now, find later dirty, contiguous pages, and add them to the list */
1123 /* hyper version of cm_data.buf_blockSize */
1124 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1125 tbase = LargeIntegerAdd(firstModOffset, thyper);
1126 while(LargeIntegerLessThan(tbase, scanEnd)) {
1127 /* see if we can find the buffer */
1128 bufp = buf_Find(&scp->fid, &tbase);
1132 /* try to lock it, and quit if we can't (simplifies locking) */
1133 lock_ReleaseWrite(&scp->rw);
1134 code = lock_TryMutex(&bufp->mx);
1135 lock_ObtainWrite(&scp->rw);
1142 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1144 lock_ReleaseMutex(&bufp->mx);
1150 if (!(bufp->flags & CM_BUF_DIRTY)) {
1151 /* buffer is clean, so we shouldn't add it */
1152 cm_SyncOpDone(scp, bufp, flags);
1153 lock_ReleaseMutex(&bufp->mx);
1159 /* don't need buffer mutex any more */
1160 lock_ReleaseMutex(&bufp->mx);
1162 /* we have a dirty buffer ready for storing. Add it to the head
1163 * of the list, since it immediately follows all of the disk
1164 * addresses we've already collected.
1166 qdp = osi_QDAlloc();
1167 osi_SetQData(qdp, bufp);
1168 /* no buf_hold necessary, since we have it held from buf_Find */
1169 osi_QAddH((osi_queue_t **) &biop->bufListp,
1170 (osi_queue_t **) &biop->bufListEndp,
1174 /* update biod info describing the transfer */
1175 biop->length += cm_data.buf_blockSize;
1177 /* update loop pointer */
1178 tbase = LargeIntegerAdd(tbase, thyper);
1179 } /* while loop looking for pages following the first page we found */
1181 /* finally, we're done */
1185 /* scp must be locked; temporarily unlocked during processing.
1186 * If returns 0, returns buffers held in biop, and with
1187 * CM_BUF_CMFETCHING flags set.
1188 * If an error is returned, we don't return any buffers.
1190 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1191 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1195 osi_hyper_t tblocksize; /* a long long temp variable */
1196 osi_hyper_t pageBase; /* base offset we're looking at */
1197 osi_queueData_t *qdp; /* one temp queue structure */
1198 osi_queueData_t *tqdp; /* another temp queue structure */
1199 long collected; /* how many bytes have been collected */
1202 osi_hyper_t fileSize; /* the # of bytes in the file */
1203 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1204 osi_queueData_t *heldBufListEndp; /* first one */
1207 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1209 biop->scp = scp; /* do not hold; held by caller */
1211 biop->offset = *offsetp;
1212 /* null out the list of buffers */
1213 biop->bufListp = biop->bufListEndp = NULL;
1216 /* first lookup the file's length, so we know when to stop */
1217 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1218 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1222 /* copy out size, since it may change */
1223 fileSize = scp->serverLength;
1225 lock_ReleaseWrite(&scp->rw);
1227 pageBase = *offsetp;
1228 collected = pageBase.LowPart & (cm_chunkSize - 1);
1229 heldBufListp = NULL;
1230 heldBufListEndp = NULL;
1233 * Obtaining buffers can cause dirty buffers to be recycled, which
1234 * can cause a storeback, so cannot be done while we have buffers
1237 * To get around this, we get buffers twice. Before reserving buffers,
1238 * we obtain and release each one individually. After reserving
1239 * buffers, we try to obtain them again, but only by lookup, not by
1240 * recycling. If a buffer has gone away while we were waiting for
1241 * the others, we just use whatever buffers we already have.
1243 * On entry to this function, we are already holding a buffer, so we
1244 * can't wait for reservation. So we call buf_TryReserveBuffers()
1245 * instead. Not only that, we can't really even call buf_Get(), for
1246 * the same reason. We can't avoid that, though. To avoid deadlock
1247 * we allow only one thread to be executing the buf_Get()-buf_Release()
1248 * sequence at a time.
1251 /* first hold all buffers, since we can't hold any locks in buf_Get */
1253 /* stop at chunk boundary */
1254 if (collected >= cm_chunkSize)
1257 /* see if the next page would be past EOF */
1258 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1261 code = buf_Get(scp, &pageBase, reqp, &tbp);
1263 lock_ObtainWrite(&scp->rw);
1264 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1271 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1272 collected += cm_data.buf_blockSize;
1275 /* reserve a chunk's worth of buffers if possible */
1276 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1278 pageBase = *offsetp;
1279 collected = pageBase.LowPart & (cm_chunkSize - 1);
1281 /* now hold all buffers, if they are still there */
1283 /* stop at chunk boundary */
1284 if (collected >= cm_chunkSize)
1287 /* see if the next page would be past EOF */
1288 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1291 tbp = buf_Find(&scp->fid, &pageBase);
1295 /* add the buffer to the list */
1296 qdp = osi_QDAlloc();
1297 osi_SetQData(qdp, tbp);
1298 osi_QAddH((osi_queue_t **)&heldBufListp,
1299 (osi_queue_t **)&heldBufListEndp,
1301 /* leave tbp held (from buf_Get) */
1306 collected += cm_data.buf_blockSize;
1307 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1310 /* look at each buffer, adding it into the list if it looks idle and
1311 * filled with old data. One special case: wait for idle if it is the
1312 * first buffer since we really need that one for our caller to make
1316 collected = 0; /* now count how many we'll really use */
1317 for (tqdp = heldBufListEndp;
1319 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1320 /* get a ptr to the held buffer */
1321 tbp = osi_GetQData(tqdp);
1322 pageBase = tbp->offset;
1324 /* now lock the buffer lock */
1325 lock_ObtainMutex(&tbp->mx);
1326 lock_ObtainWrite(&scp->rw);
1328 /* don't bother fetching over data that is already current */
1329 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1330 /* we don't need this buffer, since it is current */
1331 lock_ReleaseWrite(&scp->rw);
1332 lock_ReleaseMutex(&tbp->mx);
1336 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1338 flags |= CM_SCACHESYNC_NOWAIT;
1340 /* wait for the buffer to serialize, if required. Doesn't
1341 * release the scp or buffer lock(s) if NOWAIT is specified.
1343 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1345 lock_ReleaseWrite(&scp->rw);
1346 lock_ReleaseMutex(&tbp->mx);
1350 /* don't fetch over dirty buffers */
1351 if (tbp->flags & CM_BUF_DIRTY) {
1352 cm_SyncOpDone(scp, tbp, flags);
1353 lock_ReleaseWrite(&scp->rw);
1354 lock_ReleaseMutex(&tbp->mx);
1359 lock_ReleaseWrite(&scp->rw);
1360 lock_ReleaseMutex(&tbp->mx);
1362 /* add the buffer to the list */
1363 qdp = osi_QDAlloc();
1364 osi_SetQData(qdp, tbp);
1365 osi_QAddH((osi_queue_t **)&biop->bufListp,
1366 (osi_queue_t **)&biop->bufListEndp,
1370 /* from now on, a failure just stops our collection process, but
1371 * we still do the I/O to whatever we've already managed to collect.
1374 collected += cm_data.buf_blockSize;
1377 /* now, we've held in biop->bufListp all the buffer's we're really
1378 * interested in. We also have holds left from heldBufListp, and we
1379 * now release those holds on the buffers.
1381 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1382 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1383 tbp = osi_GetQData(qdp);
1384 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1385 (osi_queue_t **) &heldBufListEndp,
1392 /* Caller expects this */
1393 lock_ObtainWrite(&scp->rw);
1395 /* if we got a failure setting up the first buffer, then we don't have
1396 * any side effects yet, and we also have failed an operation that the
1397 * caller requires to make any progress. Give up now.
1399 if (code && isFirst) {
1400 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1404 /* otherwise, we're still OK, and should just return the I/O setup we've
1407 biop->length = collected;
1408 biop->reserved = reserving;
1412 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1415 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
1417 cm_scache_t *scp; /* do not release; not held in biop */
1419 osi_queueData_t *qdp;
1420 osi_queueData_t *nqdp;
1423 /* Give back reserved buffers */
1425 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1428 flags = CM_SCACHESYNC_STOREDATA;
1430 flags = CM_SCACHESYNC_FETCHDATA;
1433 if (biop->bufListp) {
1434 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1435 /* lookup next guy first, since we're going to free this one */
1436 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1438 /* extract buffer and free queue data */
1439 bufp = osi_GetQData(qdp);
1440 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1441 (osi_queue_t **) &biop->bufListEndp,
1445 /* now, mark I/O as done, unlock the buffer and release it */
1447 lock_ReleaseWrite(&scp->rw);
1448 lock_ObtainMutex(&bufp->mx);
1449 lock_ObtainWrite(&scp->rw);
1450 cm_SyncOpDone(scp, bufp, flags);
1452 /* turn off writing and wakeup users */
1454 if (bufp->flags & CM_BUF_WAITING) {
1455 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1456 osi_Wakeup((LONG_PTR) bufp);
1459 _InterlockedAnd(&bufp->flags, ~CM_BUF_WRITING);
1461 case CM_ERROR_NOSUCHFILE:
1462 case CM_ERROR_BADFD:
1463 case CM_ERROR_NOACCESS:
1464 case CM_ERROR_QUOTA:
1465 case CM_ERROR_SPACE:
1466 case CM_ERROR_TOOBIG:
1467 case CM_ERROR_READONLY:
1468 case CM_ERROR_NOSUCHPATH:
1470 * Apply the fatal error to this buffer.
1472 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
1473 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
1474 bufp->dirty_offset = 0;
1475 bufp->dirty_length = 0;
1477 bufp->dataVersion = CM_BUF_VERSION_BAD;
1478 bufp->dirtyCounter++;
1480 case CM_ERROR_TIMEDOUT:
1481 case CM_ERROR_ALLDOWN:
1482 case CM_ERROR_ALLBUSY:
1483 case CM_ERROR_ALLOFFLINE:
1484 case CM_ERROR_CLOCKSKEW:
1486 /* do not mark the buffer in error state but do
1487 * not attempt to complete the rest either.
1492 _InterlockedAnd(&bufp->flags, ~(CM_BUF_WRITING | CM_BUF_DIRTY));
1493 bufp->dirty_offset = bufp->dirty_length = 0;
1498 lock_ReleaseWrite(&scp->rw);
1499 lock_ReleaseMutex(&bufp->mx);
1505 lock_ObtainWrite(&scp->rw);
1506 cm_SyncOpDone(scp, NULL, flags);
1508 lock_ReleaseWrite(&scp->rw);
1511 /* clean things out */
1512 biop->bufListp = NULL;
1513 biop->bufListEndp = NULL;
1517 cm_CloneStatus(cm_scache_t *scp, cm_user_t *userp, int scp_locked,
1518 AFSFetchStatus *afsStatusp, AFSVolSync *volSyncp)
1520 // setup the status based upon the scp data
1521 afsStatusp->InterfaceVersion = 0x1;
1522 switch (scp->fileType) {
1523 case CM_SCACHETYPE_FILE:
1524 afsStatusp->FileType = File;
1526 case CM_SCACHETYPE_DIRECTORY:
1527 afsStatusp->FileType = Directory;
1529 case CM_SCACHETYPE_MOUNTPOINT:
1530 afsStatusp->FileType = SymbolicLink;
1532 case CM_SCACHETYPE_SYMLINK:
1533 case CM_SCACHETYPE_DFSLINK:
1534 afsStatusp->FileType = SymbolicLink;
1537 afsStatusp->FileType = -1; /* an invalid value */
1539 afsStatusp->LinkCount = scp->linkCount;
1540 afsStatusp->Length = scp->length.LowPart;
1541 afsStatusp->DataVersion = (afs_uint32)(scp->dataVersion & MAX_AFS_UINT32);
1542 afsStatusp->Author = 0x1;
1543 afsStatusp->Owner = scp->owner;
1545 lock_ObtainWrite(&scp->rw);
1548 if (cm_FindACLCache(scp, userp, &afsStatusp->CallerAccess))
1549 afsStatusp->CallerAccess = scp->anyAccess;
1550 afsStatusp->AnonymousAccess = scp->anyAccess;
1551 afsStatusp->UnixModeBits = scp->unixModeBits;
1552 afsStatusp->ParentVnode = scp->parentVnode;
1553 afsStatusp->ParentUnique = scp->parentUnique;
1554 afsStatusp->ResidencyMask = 0;
1555 afsStatusp->ClientModTime = scp->clientModTime;
1556 afsStatusp->ServerModTime = scp->serverModTime;
1557 afsStatusp->Group = scp->group;
1558 afsStatusp->SyncCounter = 0;
1559 afsStatusp->dataVersionHigh = (afs_uint32)(scp->dataVersion >> 32);
1560 afsStatusp->lockCount = 0;
1561 afsStatusp->Length_hi = scp->length.HighPart;
1562 afsStatusp->errorCode = 0;
1564 volSyncp->spare1 = scp->volumeCreationDate;
1569 /* Fetch a buffer. Called with scp locked.
1570 * The scp is locked on return.
1572 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1575 long code=0, code1=0;
1576 afs_uint32 nbytes; /* bytes in transfer */
1577 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1578 afs_uint64 length_found = 0;
1579 long rbytes; /* bytes in rx_Read call */
1581 AFSFetchStatus afsStatus;
1582 AFSCallBack callback;
1585 afs_uint32 buffer_offset;
1586 cm_buf_t *tbufp; /* buf we're filling */
1587 osi_queueData_t *qdp; /* q element we're scanning */
1589 struct rx_call *rxcallp;
1590 struct rx_connection *rxconnp;
1591 cm_bulkIO_t biod; /* bulk IO descriptor */
1595 int require_64bit_ops = 0;
1596 int call_was_64bit = 0;
1597 int fs_fetchdata_offset_bug = 0;
1601 memset(&volSync, 0, sizeof(volSync));
1603 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
1604 * drops lots of locks, and may indeed return a properly initialized
1605 * buffer, although more likely it will just return a new, empty, buffer.
1608 #ifdef AFS_FREELANCE_CLIENT
1610 // yj: if they're trying to get the /afs directory, we need to
1611 // handle it differently, since it's local rather than on any
1614 getroot = (scp==cm_data.rootSCachep);
1616 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1619 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1620 /* We already have this buffer don't do extra work */
1624 cm_AFSFidFromFid(&tfid, &scp->fid);
1626 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1628 /* couldn't even get the first page setup properly */
1629 osi_Log1(afsd_logp, "GetBuffer: SetupFetchBIOD failure code %d", code);
1633 /* once we get here, we have the callback in place, we know that no one
1634 * is fetching the data now. Check one last time that we still have
1635 * the wrong data, and then fetch it if we're still wrong.
1637 * We can lose a race condition and end up with biod.length zero, in
1638 * which case we just retry.
1640 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
1641 if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow) &&
1642 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength))
1644 osi_Log4(afsd_logp, "Bad DVs 0x%x != (0x%x -> 0x%x) or length 0x%x",
1645 bufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion, biod.length);
1647 if (bufp->dataVersion == CM_BUF_VERSION_BAD)
1648 memset(bufp->datap, 0, cm_data.buf_blockSize);
1649 bufp->dataVersion = scp->dataVersion;
1651 cm_ReleaseBIOD(&biod, 0, 0, 1);
1653 } else if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow)
1654 && (scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1655 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->truncPos)) {
1656 memset(bufp->datap, 0, cm_data.buf_blockSize);
1657 bufp->dataVersion = scp->dataVersion;
1658 cm_ReleaseBIOD(&biod, 0, 0, 1);
1662 lock_ReleaseWrite(&scp->rw);
1665 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1666 ConvertLongToLargeInteger(biod.length)),
1667 ConvertLongToLargeInteger(LONG_MAX))) {
1668 require_64bit_ops = 1;
1671 osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1672 osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1673 scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1675 #ifdef AFS_FREELANCE_CLIENT
1678 // if getroot then we don't need to make any calls
1679 // just return fake data
1681 if (cm_freelanceEnabled && getroot) {
1682 // setup the fake status
1683 afsStatus.InterfaceVersion = 0x1;
1684 afsStatus.FileType = 0x2;
1685 afsStatus.LinkCount = scp->linkCount;
1686 afsStatus.Length = cm_fakeDirSize;
1687 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1688 afsStatus.Author = 0x1;
1689 afsStatus.Owner = 0x0;
1690 afsStatus.CallerAccess = 0x9;
1691 afsStatus.AnonymousAccess = 0x9;
1692 afsStatus.UnixModeBits = 0x1ff;
1693 afsStatus.ParentVnode = 0x1;
1694 afsStatus.ParentUnique = 0x1;
1695 afsStatus.ResidencyMask = 0;
1696 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1697 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1698 afsStatus.Group = 0;
1699 afsStatus.SyncCounter = 0;
1700 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1701 afsStatus.lockCount = 0;
1702 afsStatus.Length_hi = 0;
1703 afsStatus.errorCode = 0;
1704 memset(&volSync, 0, sizeof(volSync));
1706 // once we're done setting up the status info,
1707 // we just fill the buffer pages with fakedata
1708 // from cm_FakeRootDir. Extra pages are set to
1711 lock_ObtainMutex(&cm_Freelance_Lock);
1712 t1 = bufp->offset.LowPart;
1713 qdp = biod.bufListEndp;
1715 tbufp = osi_GetQData(qdp);
1716 bufferp=tbufp->datap;
1717 memset(bufferp, 0, cm_data.buf_blockSize);
1718 t2 = cm_fakeDirSize - t1;
1719 if (t2> (afs_int32)cm_data.buf_blockSize)
1720 t2=cm_data.buf_blockSize;
1722 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1727 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1730 lock_ReleaseMutex(&cm_Freelance_Lock);
1732 // once we're done, we skip over the part of the
1733 // code that does the ACTUAL fetching of data for
1736 goto fetchingcompleted;
1739 #endif /* AFS_FREELANCE_CLIENT */
1742 * if the requested offset is greater than the file length,
1743 * the file server will return zero bytes of data and the
1744 * current status for the file which we already have since
1745 * we have just obtained a callback. Instead, we can avoid
1746 * the network round trip by allocating zeroed buffers and
1747 * faking the status info.
1749 if (biod.offset.QuadPart >= scp->length.QuadPart) {
1750 osi_Log5(afsd_logp, "SKIP FetchData64 scp 0x%p, off 0x%x:%08x > length 0x%x:%08x",
1751 scp, biod.offset.HighPart, biod.offset.LowPart,
1752 scp->length.HighPart, scp->length.LowPart);
1754 /* Clone the current status info */
1755 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
1757 /* status info complete, fill pages with zeros */
1758 for (qdp = biod.bufListEndp;
1760 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1761 tbufp = osi_GetQData(qdp);
1762 bufferp=tbufp->datap;
1763 memset(bufferp, 0, cm_data.buf_blockSize);
1766 /* no need to contact the file server */
1767 goto fetchingcompleted;
1771 lock_ReleaseWrite(&scp->rw);
1775 /* now make the call */
1777 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1781 rxconnp = cm_GetRxConn(connp);
1782 rxcallp = rx_NewCall(rxconnp);
1783 rx_PutConnection(rxconnp);
1785 nbytes = nbytes_hi = 0;
1787 if (SERVERHAS64BIT(connp)) {
1790 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1791 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1793 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1796 temp = rx_Read32(rxcallp, &nbytes_hi);
1797 if (temp == sizeof(afs_int32)) {
1798 nbytes_hi = ntohl(nbytes_hi);
1801 code = rxcallp->error;
1802 code1 = rx_EndCall(rxcallp, code);
1810 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1811 if (require_64bit_ops) {
1812 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1813 code = CM_ERROR_TOOBIG;
1816 rxconnp = cm_GetRxConn(connp);
1817 rxcallp = rx_NewCall(rxconnp);
1818 rx_PutConnection(rxconnp);
1821 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1822 scp, biod.offset.LowPart, biod.length);
1824 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1827 SET_SERVERHASNO64BIT(connp);
1832 temp = rx_Read32(rxcallp, &nbytes);
1833 if (temp == sizeof(afs_int32)) {
1834 nbytes = ntohl(nbytes);
1835 FillInt64(length_found, nbytes_hi, nbytes);
1836 if (length_found > biod.length) {
1838 * prior to 1.4.12 and 1.5.65 the file server would return
1839 * (filesize - offset) if the requested offset was greater than
1840 * the filesize. The correct return value would have been zero.
1841 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
1842 * is a race between two RPCs issues by this cache manager, the
1843 * correct thing will happen the second time.
1845 osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1846 fs_fetchdata_offset_bug = 1;
1849 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1850 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1853 /* for the moment, nbytes_hi will always be 0 if code == 0
1854 because biod.length is a 32-bit quantity. */
1857 qdp = biod.bufListEndp;
1859 tbufp = osi_GetQData(qdp);
1860 bufferp = tbufp->datap;
1866 /* fill length_found of data from the pipe into the pages.
1867 * When we stop, qdp will point at the last page we're
1868 * dealing with, and bufferp will tell us where we
1869 * stopped. We'll need this info below when we clear
1870 * the remainder of the last page out (and potentially
1871 * clear later pages out, if we fetch past EOF).
1873 while (length_found > 0) {
1875 struct iovec tiov[RX_MAXIOVECS];
1876 afs_int32 tnio, iov, iov_offset;
1878 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
1879 osi_Log1(afsd_logp, "cm_GetBuffer rx_Readv returns %d", temp);
1880 if (temp != length_found && temp < cm_data.buf_blockSize) {
1882 * If the file server returned (filesize - offset),
1883 * then the first rx_Read will return zero octets of data.
1884 * If it does, do not treat it as an error. Correct the
1885 * length_found and continue as if the file server said
1886 * it was sending us zero octets of data.
1888 if (fs_fetchdata_offset_bug && first_read)
1891 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1899 while (rbytes > 0) {
1902 osi_assertx(bufferp != NULL, "null cm_buf_t");
1904 len = MIN(tiov[iov].iov_len - iov_offset, cm_data.buf_blockSize - buffer_offset);
1905 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
1907 buffer_offset += len;
1910 if (iov_offset == tiov[iov].iov_len) {
1915 if (buffer_offset == cm_data.buf_blockSize) {
1916 /* allow read-while-fetching.
1917 * if this is the last buffer, clear the
1918 * PREFETCHING flag, so the reader waiting for
1919 * this buffer will start a prefetch.
1921 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
1922 lock_ObtainWrite(&scp->rw);
1923 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1924 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
1925 osi_Wakeup((LONG_PTR) &scp->flags);
1927 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1928 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
1930 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
1932 lock_ReleaseWrite(&scp->rw);
1934 /* Advance the buffer */
1935 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1937 tbufp = osi_GetQData(qdp);
1938 bufferp = tbufp->datap;
1946 length_found -= temp;
1947 #else /* USE_RX_IOVEC */
1948 /* assert that there are still more buffers;
1949 * our check above for length_found being less than
1950 * biod.length should ensure this.
1952 osi_assertx(bufferp != NULL, "null cm_buf_t");
1954 /* read rbytes of data */
1955 rbytes = (afs_uint32)(length_found > cm_data.buf_blockSize ? cm_data.buf_blockSize : length_found);
1956 temp = rx_Read(rxcallp, bufferp, rbytes);
1957 if (temp < rbytes) {
1959 * If the file server returned (filesize - offset),
1960 * then the first rx_Read will return zero octets of data.
1961 * If it does, do not treat it as an error. Correct the
1962 * length_found and continue as if the file server said
1963 * it was sending us zero octets of data.
1965 if (fs_fetchdata_offset_bug && first_read)
1968 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1973 /* allow read-while-fetching.
1974 * if this is the last buffer, clear the
1975 * PREFETCHING flag, so the reader waiting for
1976 * this buffer will start a prefetch.
1978 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
1979 lock_ObtainWrite(&scp->rw);
1980 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1981 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
1982 osi_Wakeup((LONG_PTR) &scp->flags);
1984 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1985 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
1987 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
1989 lock_ReleaseWrite(&scp->rw);
1991 /* and adjust counters */
1992 length_found -= temp;
1994 /* and move to the next buffer */
1995 if (length_found != 0) {
1996 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1998 tbufp = osi_GetQData(qdp);
1999 bufferp = tbufp->datap;
2005 #endif /* USE_RX_IOVEC */
2008 /* zero out remainder of last pages, in case we are
2009 * fetching past EOF. We were fetching an integral #
2010 * of pages, but stopped, potentially in the middle of
2011 * a page. Zero the remainder of that page, and then
2012 * all of the rest of the pages.
2015 rbytes = cm_data.buf_blockSize - buffer_offset;
2016 bufferp = tbufp->datap + buffer_offset;
2017 #else /* USE_RX_IOVEC */
2019 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
2020 rbytes = (long) (bufferp - tbufp->datap);
2022 /* bytes left to zero */
2023 rbytes = cm_data.buf_blockSize - rbytes;
2024 #endif /* USE_RX_IOVEC */
2027 memset(bufferp, 0, rbytes);
2028 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2031 tbufp = osi_GetQData(qdp);
2032 bufferp = tbufp->datap;
2033 /* bytes to clear in this page */
2034 rbytes = cm_data.buf_blockSize;
2040 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2042 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2045 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2047 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2051 code1 = rx_EndCall(rxcallp, code);
2053 if (code1 == RXKADUNKNOWNKEY)
2054 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2056 /* If we are avoiding a file server bug, ignore the error state */
2057 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2058 /* Clone the current status info and clear the error state */
2059 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2061 lock_ReleaseWrite(&scp->rw);
2065 /* Prefer the error value from FetchData over rx_EndCall */
2066 } else if (code == 0 && code1 != 0)
2068 osi_Log0(afsd_logp, "CALL FetchData DONE");
2070 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
2073 code = cm_MapRPCError(code, reqp);
2076 lock_ObtainWrite(&scp->rw);
2078 /* we know that no one else has changed the buffer, since we still have
2079 * the fetching flag on the buffers, and we have the scp locked again.
2080 * Copy in the version # into the buffer if we got code 0 back from the
2084 for(qdp = biod.bufListp;
2086 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
2087 tbufp = osi_GetQData(qdp);
2088 tbufp->dataVersion = afsStatus.dataVersionHigh;
2089 tbufp->dataVersion <<= 32;
2090 tbufp->dataVersion |= afsStatus.DataVersion;
2093 /* write buffer out to disk cache */
2094 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
2095 tbufp->dataVersion);
2096 #endif /* DISKCACHE95 */
2100 /* release scatter/gather I/O structure (buffers, locks) */
2101 cm_ReleaseBIOD(&biod, 0, code, 1);
2104 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2110 * Similar to cm_GetBuffer but doesn't use an allocated cm_buf_t object.
2111 * Instead the data is read from the file server and copied directly into
2112 * a provided buffer. Called with scp locked. The scp is locked on return.
2114 long cm_GetData(cm_scache_t *scp, osi_hyper_t *offsetp, char *datap, int data_length,
2115 cm_user_t *userp, cm_req_t *reqp)
2117 long code=0, code1=0;
2118 afs_uint32 nbytes; /* bytes in transfer */
2119 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2120 afs_uint64 length_found = 0;
2121 char *bufferp = datap;
2122 afs_uint32 buffer_offset = 0;
2123 long rbytes; /* bytes in rx_Read call */
2125 AFSFetchStatus afsStatus;
2126 AFSCallBack callback;
2129 struct rx_call *rxcallp;
2130 struct rx_connection *rxconnp;
2134 int require_64bit_ops = 0;
2135 int call_was_64bit = 0;
2136 int fs_fetchdata_offset_bug = 0;
2140 memset(&volSync, 0, sizeof(volSync));
2142 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
2143 * drops lots of locks, and may indeed return a properly initialized
2144 * buffer, although more likely it will just return a new, empty, buffer.
2147 #ifdef AFS_FREELANCE_CLIENT
2149 // yj: if they're trying to get the /afs directory, we need to
2150 // handle it differently, since it's local rather than on any
2153 getroot = (scp==cm_data.rootSCachep);
2155 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
2158 cm_AFSFidFromFid(&tfid, &scp->fid);
2160 if (LargeIntegerGreaterThan(LargeIntegerAdd(*offsetp,
2161 ConvertLongToLargeInteger(data_length)),
2162 ConvertLongToLargeInteger(LONG_MAX))) {
2163 require_64bit_ops = 1;
2166 osi_Log2(afsd_logp, "cm_GetData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2168 #ifdef AFS_FREELANCE_CLIENT
2171 // if getroot then we don't need to make any calls
2172 // just return fake data
2174 if (cm_freelanceEnabled && getroot) {
2175 // setup the fake status
2176 afsStatus.InterfaceVersion = 0x1;
2177 afsStatus.FileType = 0x2;
2178 afsStatus.LinkCount = scp->linkCount;
2179 afsStatus.Length = cm_fakeDirSize;
2180 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
2181 afsStatus.Author = 0x1;
2182 afsStatus.Owner = 0x0;
2183 afsStatus.CallerAccess = 0x9;
2184 afsStatus.AnonymousAccess = 0x9;
2185 afsStatus.UnixModeBits = 0x1ff;
2186 afsStatus.ParentVnode = 0x1;
2187 afsStatus.ParentUnique = 0x1;
2188 afsStatus.ResidencyMask = 0;
2189 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
2190 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
2191 afsStatus.Group = 0;
2192 afsStatus.SyncCounter = 0;
2193 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
2194 afsStatus.lockCount = 0;
2195 afsStatus.Length_hi = 0;
2196 afsStatus.errorCode = 0;
2197 memset(&volSync, 0, sizeof(volSync));
2199 // once we're done setting up the status info,
2200 // we just fill the buffer pages with fakedata
2201 // from cm_FakeRootDir. Extra pages are set to
2204 lock_ObtainMutex(&cm_Freelance_Lock);
2205 t1 = offsetp->LowPart;
2206 memset(datap, 0, data_length);
2207 t2 = cm_fakeDirSize - t1;
2208 if (t2 > data_length)
2211 memcpy(datap, cm_FakeRootDir+t1, t2);
2212 lock_ReleaseMutex(&cm_Freelance_Lock);
2214 // once we're done, we skip over the part of the
2215 // code that does the ACTUAL fetching of data for
2218 goto fetchingcompleted;
2221 #endif /* AFS_FREELANCE_CLIENT */
2224 lock_ReleaseWrite(&scp->rw);
2228 /* now make the call */
2230 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2234 rxconnp = cm_GetRxConn(connp);
2235 rxcallp = rx_NewCall(rxconnp);
2236 rx_PutConnection(rxconnp);
2238 nbytes = nbytes_hi = 0;
2240 if (SERVERHAS64BIT(connp)) {
2243 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2244 scp, offsetp->HighPart, offsetp->LowPart, data_length);
2246 code = StartRXAFS_FetchData64(rxcallp, &tfid, offsetp->QuadPart, data_length);
2249 temp = rx_Read32(rxcallp, &nbytes_hi);
2250 if (temp == sizeof(afs_int32)) {
2251 nbytes_hi = ntohl(nbytes_hi);
2254 code = rxcallp->error;
2255 code1 = rx_EndCall(rxcallp, code);
2263 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2264 if (require_64bit_ops) {
2265 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2266 code = CM_ERROR_TOOBIG;
2269 rxconnp = cm_GetRxConn(connp);
2270 rxcallp = rx_NewCall(rxconnp);
2271 rx_PutConnection(rxconnp);
2274 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2275 scp, offsetp->LowPart, data_length);
2277 code = StartRXAFS_FetchData(rxcallp, &tfid, offsetp->LowPart, data_length);
2279 SET_SERVERHASNO64BIT(connp);
2284 temp = rx_Read32(rxcallp, &nbytes);
2285 if (temp == sizeof(afs_int32)) {
2286 nbytes = ntohl(nbytes);
2287 FillInt64(length_found, nbytes_hi, nbytes);
2288 if (length_found > data_length) {
2290 * prior to 1.4.12 and 1.5.65 the file server would return
2291 * (filesize - offset) if the requested offset was greater than
2292 * the filesize. The correct return value would have been zero.
2293 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2294 * is a race between two RPCs issues by this cache manager, the
2295 * correct thing will happen the second time.
2297 osi_Log0(afsd_logp, "cm_GetData length_found > data_length");
2298 fs_fetchdata_offset_bug = 1;
2301 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2302 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
2305 /* for the moment, nbytes_hi will always be 0 if code == 0
2306 because data_length is a 32-bit quantity. */
2309 /* fill length_found of data from the pipe into the pages.
2310 * When we stop, qdp will point at the last page we're
2311 * dealing with, and bufferp will tell us where we
2312 * stopped. We'll need this info below when we clear
2313 * the remainder of the last page out (and potentially
2314 * clear later pages out, if we fetch past EOF).
2316 while (length_found > 0) {
2318 struct iovec tiov[RX_MAXIOVECS];
2319 afs_int32 tnio, iov, iov_offset;
2321 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
2322 osi_Log1(afsd_logp, "cm_GetData rx_Readv returns %d", temp);
2323 if (temp != length_found && temp < data_length) {
2325 * If the file server returned (filesize - offset),
2326 * then the first rx_Read will return zero octets of data.
2327 * If it does, do not treat it as an error. Correct the
2328 * length_found and continue as if the file server said
2329 * it was sending us zero octets of data.
2331 if (fs_fetchdata_offset_bug && first_read)
2334 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
2342 while (rbytes > 0) {
2345 osi_assertx(bufferp != NULL, "null cm_buf_t");
2347 len = MIN(tiov[iov].iov_len - iov_offset, data_length - buffer_offset);
2348 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
2350 buffer_offset += len;
2353 if (iov_offset == tiov[iov].iov_len) {
2359 length_found -= temp;
2360 #else /* USE_RX_IOVEC */
2361 /* assert that there are still more buffers;
2362 * our check above for length_found being less than
2363 * data_length should ensure this.
2365 osi_assertx(bufferp != NULL, "null cm_buf_t");
2367 /* read rbytes of data */
2368 rbytes = (afs_uint32)(length_found > data_length ? data_length : length_found);
2369 temp = rx_Read(rxcallp, bufferp, rbytes);
2370 if (temp < rbytes) {
2372 * If the file server returned (filesize - offset),
2373 * then the first rx_Read will return zero octets of data.
2374 * If it does, do not treat it as an error. Correct the
2375 * length_found and continue as if the file server said
2376 * it was sending us zero octets of data.
2378 if (fs_fetchdata_offset_bug && first_read)
2381 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
2386 /* and adjust counters */
2387 length_found -= temp;
2388 #endif /* USE_RX_IOVEC */
2391 /* zero out remainder of last pages, in case we are
2392 * fetching past EOF. We were fetching an integral #
2393 * of pages, but stopped, potentially in the middle of
2394 * a page. Zero the remainder of that page, and then
2395 * all of the rest of the pages.
2398 rbytes = data_length - buffer_offset;
2399 bufferp = datap + buffer_offset;
2400 #else /* USE_RX_IOVEC */
2402 osi_assertx((bufferp - datap) < LONG_MAX, "data >= LONG_MAX");
2403 rbytes = (long) (bufferp - datap);
2405 /* bytes left to zero */
2406 rbytes = data_length - rbytes;
2407 #endif /* USE_RX_IOVEC */
2409 memset(bufferp, 0, rbytes);
2414 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2416 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2419 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2421 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2425 code1 = rx_EndCall(rxcallp, code);
2427 if (code1 == RXKADUNKNOWNKEY)
2428 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2430 /* If we are avoiding a file server bug, ignore the error state */
2431 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2432 /* Clone the current status info and clear the error state */
2433 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2435 lock_ReleaseWrite(&scp->rw);
2439 /* Prefer the error value from FetchData over rx_EndCall */
2440 } else if (code == 0 && code1 != 0)
2442 osi_Log0(afsd_logp, "CALL FetchData DONE");
2444 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
2447 code = cm_MapRPCError(code, reqp);
2450 lock_ObtainWrite(&scp->rw);
2453 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);