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>
28 extern void afsi_log(char *pattern, ...);
31 #ifdef AFS_FREELANCE_CLIENT
32 extern osi_mutex_t cm_Freelance_Lock;
35 #define USE_RX_IOVEC 1
37 /* functions called back from the buffer package when reading or writing data,
38 * or when holding or releasing a vnode pointer.
40 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
41 cm_user_t *userp, cm_req_t *reqp)
44 * store the data back from this buffer; the buffer is locked and held,
45 * but the vnode involved may or may not be locked depending on whether
46 * or not the CM_BUF_WRITE_SCP_LOCKED flag is set.
49 cm_scache_t *scp = vscp;
51 afs_int32 save_nbytes;
52 cm_scache_t save_scache;
54 AFSFetchStatus outStatus;
55 AFSStoreStatus inStatus;
59 struct rx_call *rxcallp;
60 struct rx_connection *rxconnp;
67 cm_bulkIO_t biod; /* bulk IO descriptor */
68 int require_64bit_ops = 0;
69 int call_was_64bit = 0;
70 int scp_locked = flags & CM_BUF_WRITE_SCP_LOCKED;
71 int storedata_excl = 0;
73 osi_assertx(userp != NULL, "null cm_user_t");
74 osi_assertx(scp != NULL, "null cm_scache_t");
76 memset(&volSync, 0, sizeof(volSync));
79 * now, the buffer may or may not be filled with good data (buf_GetNewLocked
80 * drops lots of locks, and may indeed return a properly initialized
81 * buffer, although more likely it will just return a new, empty, buffer.
84 lock_ObtainWrite(&scp->rw);
85 if (scp->flags & CM_SCACHEFLAG_DELETED) {
87 lock_ReleaseWrite(&scp->rw);
88 return CM_ERROR_NOSUCHFILE;
91 cm_AFSFidFromFid(&tfid, &scp->fid);
93 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
94 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
97 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
99 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
100 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
102 lock_ReleaseWrite(&scp->rw);
106 if (biod.length == 0) {
107 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
108 cm_ReleaseBIOD(&biod, 1, 0, 1); /* should be a NOOP */
110 goto exit_storedata_excl;
113 /* prepare the output status for the store */
114 _InterlockedOr(&scp->mask, CM_SCACHEMASK_CLIENTMODTIME);
115 cm_StatusFromAttr(&inStatus, scp, NULL);
116 truncPos = scp->length;
117 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
118 && LargeIntegerLessThan(scp->truncPos, truncPos)) {
119 truncPos = scp->truncPos;
120 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_TRUNCPOS);
123 /* compute how many bytes to write from this buffer */
124 thyper = LargeIntegerSubtract(scp->length, biod.offset);
125 if (LargeIntegerLessThanZero(thyper)) {
126 /* entire buffer is past EOF */
130 /* otherwise write out part of buffer before EOF, but not
131 * more than bufferSize bytes.
133 if (LargeIntegerGreaterThan(thyper,
134 ConvertLongToLargeInteger(biod.length))) {
135 nbytes = biod.length;
137 /* if thyper is less than or equal to biod.length, then we
138 can safely assume that the value fits in a long. */
139 nbytes = thyper.LowPart;
143 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
144 ConvertLongToLargeInteger(nbytes)),
145 ConvertLongToLargeInteger(LONG_MAX)) ||
146 LargeIntegerGreaterThan(truncPos,
147 ConvertLongToLargeInteger(LONG_MAX))) {
148 require_64bit_ops = 1;
151 /* now we're ready to do the store operation */
153 InterlockedIncrement(&scp->activeRPCs);
154 lock_ReleaseWrite(&scp->rw);
156 save_nbytes = nbytes;
160 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
165 rxconnp = cm_GetRxConn(connp);
166 rxcallp = rx_NewCall(rxconnp);
167 rx_PutConnection(rxconnp);
169 if (SERVERHAS64BIT(connp)) {
172 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
173 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
174 osi_Log2(afsd_logp, "... truncPos 0x%x:%08x", truncPos.HighPart, truncPos.LowPart);
176 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
177 biod.offset.QuadPart,
181 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
183 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
187 if (require_64bit_ops) {
188 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData. The operation requires large file support in the server.");
189 code = CM_ERROR_TOOBIG;
191 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
192 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
194 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
195 biod.offset.LowPart, nbytes, truncPos.LowPart);
197 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
199 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
204 afs_uint32 buf_offset = 0, bytes_copied = 0;
206 /* write the data from the the list of buffers */
210 struct iovec tiov[RX_MAXIOVECS];
211 afs_int32 tnio, vlen, vbytes, iov, voffset;
214 vbytes = rx_WritevAlloc(rxcallp, tiov, &tnio, RX_MAXIOVECS, nbytes);
216 code = RX_PROTOCOL_ERROR;
220 for ( iov = voffset = vlen = 0;
221 vlen < vbytes && iov < tnio; vlen += wbytes) {
223 qdp = biod.bufListEndp;
224 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
225 } else if (buf_offset == cm_data.buf_blockSize) {
226 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
230 osi_assertx(qdp != NULL, "null osi_queueData_t");
231 bufp = osi_GetQData(qdp);
232 bufferp = bufp->datap + buf_offset;
233 wbytes = vbytes - vlen;
234 if (wbytes > cm_data.buf_blockSize - buf_offset)
235 wbytes = cm_data.buf_blockSize - buf_offset;
237 vleft = tiov[iov].iov_len - voffset;
238 while (wbytes > vleft && iov < tnio) {
239 memcpy(tiov[iov].iov_base + voffset, bufferp, vleft);
240 bytes_copied += vleft;
248 vleft = tiov[iov].iov_len;
252 memcpy(tiov[iov].iov_base + voffset, bufferp, wbytes);
253 bytes_copied += wbytes;
254 if (tiov[iov].iov_len == voffset + wbytes) {
257 vleft = (iov < tnio) ? tiov[iov].iov_len : 0;
263 buf_offset += wbytes;
269 osi_assertx(iov == tnio, "incorrect iov count");
270 osi_assertx(vlen == vbytes, "bytes_copied != vbytes");
271 osi_assertx(bufp->offset.QuadPart + buf_offset == biod.offset.QuadPart + bytes_copied,
272 "begin and end offsets don't match");
274 temp = rx_Writev(rxcallp, tiov, tnio, vbytes);
275 if (temp != vbytes) {
276 osi_Log3(afsd_logp, "rx_Writev failed bp 0x%p, %d != %d", bufp, temp, vbytes);
277 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
281 osi_Log2(afsd_logp, "rx_Writev succeeded iov offset 0x%x, wrote 0x%x",
282 (unsigned long)(bufp->offset.QuadPart + buf_offset - vbytes), vbytes);
284 #else /* USE_RX_IOVEC */
286 qdp = biod.bufListEndp;
287 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
289 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
293 osi_assertx(qdp != NULL, "null osi_queueData_t");
294 bufp = osi_GetQData(qdp);
295 bufferp = bufp->datap + buf_offset;
297 if (wbytes > cm_data.buf_blockSize - buf_offset)
298 wbytes = cm_data.buf_blockSize - buf_offset;
300 /* write out wbytes of data from bufferp */
301 temp = rx_Write(rxcallp, bufferp, wbytes);
302 if (temp != wbytes) {
303 osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d", bufp, temp, wbytes);
304 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
307 osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p written %d", bufp, temp);
310 #endif /* USE_RX_IOVEC */
313 * Rx supports an out of band signalling mechanism that permits
314 * RPC specific status information to be communicated in the
315 * reverse direction of the channel. For RXAFS_StoreData, the
316 * 0-bit is set once all of the permission checks have completed
317 * and the volume/vnode locks have been obtained by the file
318 * server. The signal is intended to notify the Unix afs client
319 * that is performing store-on-close that it is safe to permit
320 * the close operation to complete while the store continues
321 * in the background. All of the callbacks have been broken
322 * and the locks will not be dropped until the RPC completes
323 * which prevents any other operation from being initiated on
324 * the vnode until the store is finished.
326 * The Windows client does not perform store-on-close. Instead
327 * it uses the CM_SCACHESYNC_STOREDATA_EXCL request flag and
328 * CM_SCACHEFLAG_DATASTORING scache state to ensure that store
329 * operations are serialized. The 0-bit signal permits the
330 * CM_SCACHEFLAG_DATASTORING state to the dropped which in
331 * turn permits another thread to prep its own BIOD in parallel.
332 * This is safe because it is impossible for that second store
333 * RPC to complete before this one does.
335 if ( storedata_excl && (rx_GetRemoteStatus(rxcallp) & 1)) {
336 lock_ObtainWrite(&scp->rw);
337 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
338 lock_ReleaseWrite(&scp->rw);
341 } /* while more bytes to write */
342 } /* if RPC started successfully */
345 if (call_was_64bit) {
346 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
348 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
350 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
352 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
354 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
356 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
360 code1 = rx_EndCall(rxcallp, code);
362 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
363 SET_SERVERHASNO64BIT(connp);
365 nbytes = save_nbytes;
368 /* Prefer rx_EndCall error over StoreData error */
370 osi_Log2(afsd_logp, "rx_EndCall converted 0x%x to 0x%x", code, code1);
373 } while (cm_Analyze(connp, userp, reqp, &scp->fid, NULL, 1, &outStatus, &volSync, NULL, NULL, code));
375 code = cm_MapRPCError(code, reqp);
378 osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
380 osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
382 /* now, clean up our state */
383 lock_ObtainWrite(&scp->rw);
387 /* now, here's something a little tricky: in AFS 3, a dirty
388 * length can't be directly stored, instead, a dirty chunk is
389 * stored that sets the file's size (by writing and by using
390 * the truncate-first option in the store call).
392 * At this point, we've just finished a store, and so the trunc
393 * pos field is clean. If the file's size at the server is at
394 * least as big as we think it should be, then we turn off the
395 * length dirty bit, since all the other dirty buffers must
396 * precede this one in the file.
398 * The file's desired size shouldn't be smaller than what's
399 * stored at the server now, since we just did the trunc pos
402 * We have to turn off the length dirty bit as soon as we can,
403 * so that we see updates made by other machines.
406 if (call_was_64bit) {
407 t.LowPart = outStatus.Length;
408 t.HighPart = outStatus.Length_hi;
410 t = ConvertLongToLargeInteger(outStatus.Length);
413 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
414 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
416 code = cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
418 InterlockedDecrement(&scp->activeRPCs);
419 if (code == CM_ERROR_SPACE)
420 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OUTOFSPACE);
421 else if (code == CM_ERROR_QUOTA)
422 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OVERQUOTA);
427 if (!cm_VerifyStoreData(&biod, &save_scache)) {
428 /* file server content doesn't match what we sent. */
429 nbytes = save_nbytes;
434 cm_ReleaseBIOD(&biod, 1, code, 1);
438 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
441 lock_ReleaseWrite(&scp->rw);
447 * Truncate the file, by sending a StoreData RPC with zero length.
449 * Called with scp locked. Releases and re-obtains the lock.
451 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
453 AFSFetchStatus outStatus;
454 AFSStoreStatus inStatus;
458 osi_hyper_t truncPos;
460 struct rx_call *rxcallp;
461 struct rx_connection *rxconnp;
462 int require_64bit_ops = 0;
463 int call_was_64bit = 0;
465 memset(&volSync, 0, sizeof(volSync));
467 osi_Log2(afsd_logp, "cm_StoreMini scp 0x%p userp 0x%p", scp, userp);
469 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
470 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
471 CM_SCACHESYNC_STOREDATA_EXCL);
473 /* prepare the output status for the store */
474 inStatus.Mask = AFS_SETMODTIME;
475 inStatus.ClientModTime = scp->clientModTime;
476 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_CLIENTMODTIME);
478 /* calculate truncation position */
479 truncPos = scp->length;
480 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
481 && LargeIntegerLessThan(scp->truncPos, truncPos))
482 truncPos = scp->truncPos;
483 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_TRUNCPOS);
485 if (LargeIntegerGreaterThan(truncPos,
486 ConvertLongToLargeInteger(LONG_MAX))) {
488 require_64bit_ops = 1;
491 InterlockedIncrement(&scp->activeRPCs);
492 lock_ReleaseWrite(&scp->rw);
494 cm_AFSFidFromFid(&tfid, &scp->fid);
496 /* now we're ready to do the store operation */
498 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
503 rxconnp = cm_GetRxConn(connp);
504 rxcallp = rx_NewCall(rxconnp);
505 rx_PutConnection(rxconnp);
507 if (SERVERHAS64BIT(connp)) {
510 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, truncPos 0x%x:%08x",
511 scp, truncPos.HighPart, truncPos.LowPart);
513 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
514 0, 0, truncPos.QuadPart);
516 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
518 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
522 if (require_64bit_ops) {
523 code = CM_ERROR_TOOBIG;
525 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, truncPos 0x%x:%08x",
526 scp, truncPos.HighPart, truncPos.LowPart);
528 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
529 0, 0, truncPos.LowPart);
531 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
533 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
538 if (call_was_64bit) {
539 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
541 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
543 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
545 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
547 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX", scp, code);
549 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
552 code1 = rx_EndCall(rxcallp, code);
554 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
555 SET_SERVERHASNO64BIT(connp);
559 /* prefer StoreData error over rx_EndCall error */
560 if (code == 0 && code1 != 0)
562 } while (cm_Analyze(connp, userp, reqp, &scp->fid, NULL, 1, &outStatus, &volSync, NULL, NULL, code));
563 code = cm_MapRPCError(code, reqp);
565 /* now, clean up our state */
566 lock_ObtainWrite(&scp->rw);
571 * For explanation of handling of CM_SCACHEMASK_LENGTH,
574 if (call_was_64bit) {
575 t.HighPart = outStatus.Length_hi;
576 t.LowPart = outStatus.Length;
578 t = ConvertLongToLargeInteger(outStatus.Length);
581 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
582 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
583 code = cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
585 InterlockedDecrement(&scp->activeRPCs);
587 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
592 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
596 /* now return a code that means that I/O is done */
601 * stabilize scache entry with CM_SCACHESYNC_SETSIZE. This prevents any new
602 * data buffers to be allocated, new data to be fetched from the file server,
603 * and writes to be accepted from the application but permits dirty buffers
604 * to be written to the file server.
606 * Stabilize uses cm_SyncOp to maintain the cm_scache_t in this stable state
607 * instead of holding the rwlock exclusively. This permits background stores
608 * to be performed in parallel and in particular allow FlushFile to be
609 * implemented without violating the locking hierarchy.
611 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
613 cm_scache_t *scp = vscp;
616 lock_ObtainWrite(&scp->rw);
617 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
618 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
619 lock_ReleaseWrite(&scp->rw);
624 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
625 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
627 cm_scache_t *scp = vscp;
629 lock_ObtainWrite(&scp->rw);
630 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
632 lock_ReleaseWrite(&scp->rw);
634 /* always succeeds */
638 cm_buf_ops_t cm_bufOps = {
645 long cm_ValidateDCache(void)
647 return buf_ValidateBuffers();
650 long cm_ShutdownDCache(void)
655 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
657 return buf_Init(newFile, &cm_bufOps, nbuffers);
660 /* check to see if we have an up-to-date buffer. The buffer must have
661 * previously been obtained by calling buf_Get.
663 * Make sure we have a callback, and that the dataversion matches.
665 * Scp must be locked.
667 * Bufp *may* be locked.
669 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
672 if (!cm_HaveCallback(scp))
674 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
676 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
678 if (bufp->offset.QuadPart >= scp->serverLength.QuadPart)
681 code = lock_TryMutex(&bufp->mx);
683 /* don't have the lock, and can't lock it, then
690 /* remember dirty flag for later */
691 code = bufp->flags & CM_BUF_DIRTY;
693 /* release lock if we obtained it here */
695 lock_ReleaseMutex(&bufp->mx);
697 /* if buffer was dirty, buffer is acceptable for use */
705 * used when deciding whether to do a background fetch or not.
706 * call with scp->rw write-locked.
709 cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
710 cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
714 osi_hyper_t tblocksize;
719 /* now scan all buffers in the range, looking for any that look like
724 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
726 while (LargeIntegerGreaterThanZero(tlength)) {
727 /* get callback so we can do a meaningful dataVersion comparison */
728 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
729 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
733 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
734 /* we're past the end of file */
738 bp = buf_Find(&scp->fid, &tbase);
739 /* We cheat slightly by not locking the bp mutex. */
741 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
742 && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
750 /* if this buffer is essentially guaranteed to require a fetch,
751 * break out here and return this position.
756 tbase = LargeIntegerAdd(tbase, tblocksize);
757 tlength = LargeIntegerSubtract(tlength, tblocksize);
760 /* if we get here, either everything is fine or 'stop' stopped us at a
761 * particular buffer in the range that definitely needs to be fetched.
764 /* return non-zero code since realBasep won't be valid */
768 /* successfully found a page that will need fetching */
776 cm_BkgStore(cm_scache_t *scp, void *rockp, cm_user_t *userp, cm_req_t *reqp)
781 afs_uint32 req_flags = reqp->flags;
783 toffset = ((rock_BkgStore_t *)rockp)->offset;
784 length = ((rock_BkgStore_t *)rockp)->length;
786 if (scp->flags & CM_SCACHEFLAG_DELETED) {
787 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x",
788 scp, toffset.HighPart, toffset.LowPart, length);
790 /* Retries will be performed by the BkgDaemon thread if appropriate */
791 reqp->flags |= CM_REQ_NORETRY;
793 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x",
794 scp, toffset.HighPart, toffset.LowPart, length);
796 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, reqp);
798 osi_Log5(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x, code 0x%x",
799 scp, toffset.HighPart, toffset.LowPart, length, code);
801 reqp->flags = req_flags;
805 * Keep the following list synchronized with the
806 * error code list in cm_BkgDaemon
809 case CM_ERROR_TIMEDOUT: /* or server restarting */
811 case CM_ERROR_WOULDBLOCK:
812 case CM_ERROR_ALLBUSY:
813 case CM_ERROR_ALLDOWN:
814 case CM_ERROR_ALLOFFLINE:
815 case CM_ERROR_PARTIALWRITE:
816 break; /* cm_BkgDaemon will re-insert the request in the queue */
819 lock_ObtainWrite(&scp->rw);
820 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
821 lock_ReleaseWrite(&scp->rw);
827 /* Called with scp locked */
828 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
833 end = LargeIntegerAdd(*base, *length);
834 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
835 scp->prefetch.base = *base;
836 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
837 scp->prefetch.end = end;
839 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
842 /* do the prefetch. if the prefetch fails, return 0 (success)
843 * because there is no harm done. */
845 cm_BkgPrefetch(cm_scache_t *scp, void *rockp, cm_user_t *userp, cm_req_t *reqp)
852 osi_hyper_t tblocksize;
856 afs_uint32 req_flags;
858 /* Retries will be performed by the BkgDaemon thread if appropriate */
859 req_flags = reqp->flags;
860 reqp->flags |= CM_REQ_NORETRY;
863 fetched.HighPart = 0;
864 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
865 base = ((rock_BkgFetch_t *)rockp)->base;
866 length = ((rock_BkgFetch_t *)rockp)->length;
868 end = LargeIntegerAdd(base, length);
870 osi_Log5(afsd_logp, "Starting BKG prefetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
871 scp, base.HighPart, base.LowPart, length.HighPart, length.LowPart);
873 for ( code = 0, offset = base;
874 code == 0 && LargeIntegerLessThan(offset, end);
875 offset = LargeIntegerAdd(offset, tblocksize) )
878 lock_ReleaseWrite(&scp->rw);
882 code = buf_Get(scp, &offset, reqp, 0, &bp);
886 if (bp->cmFlags & CM_BUF_CMFETCHING) {
887 /* skip this buffer as another thread is already fetching it */
889 lock_ObtainWrite(&scp->rw);
898 lock_ObtainWrite(&scp->rw);
902 code = cm_GetBuffer(scp, bp, NULL, userp, reqp);
904 fetched = LargeIntegerAdd(fetched, tblocksize);
909 lock_ObtainWrite(&scp->rw);
913 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
914 scp, &base, &fetched);
916 /* wakeup anyone who is waiting */
917 if (!osi_QIsEmpty(&scp->waitQueueH)) {
918 osi_Log1(afsd_logp, "CM BkgPrefetch Waking scp 0x%p", scp);
919 osi_Wakeup((LONG_PTR) &scp->flags);
921 lock_ReleaseWrite(&scp->rw);
923 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p code 0x%x fetched 0x%x:%x",
924 scp, code, fetched.HighPart, fetched.LowPart);
926 reqp->flags = req_flags;
930 /* a read was issued to offsetp, and we have to determine whether we should
931 * do a prefetch of the next chunk.
933 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
934 cm_user_t *userp, cm_req_t *reqp)
938 osi_hyper_t realBase;
939 osi_hyper_t readBase;
940 osi_hyper_t readLength;
942 osi_hyper_t tblocksize; /* a long long temp variable */
943 rock_BkgFetch_t *rockp;
945 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
948 /* round up to chunk boundary */
949 readBase.LowPart += (cm_chunkSize-1);
950 readBase.LowPart &= (-cm_chunkSize);
952 readLength = ConvertLongToLargeInteger(count);
954 lock_ObtainWrite(&scp->rw);
956 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
957 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
958 lock_ReleaseWrite(&scp->rw);
961 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_PREFETCHING);
963 /* start the scan at the latter of the end of this read or
964 * the end of the last fetched region.
966 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
967 readBase = scp->prefetch.end;
969 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
972 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
973 lock_ReleaseWrite(&scp->rw);
974 return; /* can't find something to prefetch */
977 readEnd = LargeIntegerAdd(realBase, readLength);
980 lock_ReleaseWrite(&scp->rw);
982 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
983 scp, realBase.LowPart);
985 rockp = malloc(sizeof(*rockp));
987 return; /* can't proceed without a rock */
990 rockp->base = realBase;
991 rockp->length = readLength;
993 /* cm_BkgDaemon frees the rock */
994 cm_QueueBKGRequest(scp, cm_BkgPrefetch, rockp, userp, reqp);
997 /* scp must be locked; temporarily unlocked during processing.
998 * If returns 0, returns buffers held in biop, and with
999 * CM_BUF_CMSTORING set.
1001 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
1002 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
1003 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
1004 * must be woken, and the event must be set when the I/O is done. All of this
1005 * is required so that buf_WaitIO synchronizes properly with the buffer as it
1006 * is being written out.
1008 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
1009 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1012 osi_queueData_t *qdp;
1015 osi_hyper_t scanStart; /* where to start scan for dirty pages */
1016 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
1017 osi_hyper_t firstModOffset; /* offset of first modified page in range */
1020 long flags; /* flags to cm_SyncOp */
1022 /* clear things out */
1023 biop->scp = scp; /* do not hold; held by caller */
1024 biop->userp = userp; /* do not hold; held by caller */
1026 biop->offset = *inOffsetp;
1028 biop->bufListp = NULL;
1029 biop->bufListEndp = NULL;
1032 /* reserve a chunk's worth of buffers */
1033 lock_ReleaseWrite(&scp->rw);
1034 biop->reserved = (cm_chunkSize / cm_data.buf_blockSize);
1035 buf_ReserveBuffers(biop->reserved);
1036 lock_ObtainWrite(&scp->rw);
1039 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
1040 thyper = ConvertLongToLargeInteger(temp);
1041 tbase = LargeIntegerAdd(*inOffsetp, thyper);
1043 bufp = buf_Find(&scp->fid, &tbase);
1045 /* get buffer mutex and scp mutex safely */
1046 lock_ReleaseWrite(&scp->rw);
1047 lock_ObtainMutex(&bufp->mx);
1050 * if the buffer is actively involved in I/O
1051 * we wait for the I/O to complete.
1053 if (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING))
1054 buf_WaitIO(scp, bufp);
1056 lock_ObtainWrite(&scp->rw);
1057 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
1058 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1060 lock_ReleaseMutex(&bufp->mx);
1063 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1067 /* if the buffer is dirty, we're done */
1068 if (bufp->flags & CM_BUF_DIRTY) {
1069 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
1070 "WRITING w/o CMSTORING in SetupStoreBIOD");
1071 _InterlockedOr(&bufp->flags, CM_BUF_WRITING);
1075 /* this buffer is clean, so there's no reason to process it */
1076 cm_SyncOpDone(scp, bufp, flags);
1077 lock_ReleaseMutex(&bufp->mx);
1083 /* if we get here, if bufp is null, we didn't find any dirty buffers
1084 * that weren't already being stored back, so we just quit now.
1090 /* don't need buffer mutex any more */
1091 lock_ReleaseMutex(&bufp->mx);
1093 /* put this element in the list */
1094 qdp = osi_QDAlloc();
1095 osi_SetQData(qdp, bufp);
1097 if ( cm_verifyData )
1098 buf_ComputeCheckSum(bufp);
1100 /* don't have to hold bufp, since held by buf_Find above */
1101 osi_QAddH((osi_queue_t **) &biop->bufListp,
1102 (osi_queue_t **) &biop->bufListEndp,
1104 biop->length = cm_data.buf_blockSize;
1105 firstModOffset = bufp->offset;
1106 biop->offset = firstModOffset;
1107 bufp = NULL; /* this buffer and reference added to the queue */
1109 /* compute the window surrounding firstModOffset of size cm_chunkSize */
1110 scanStart = firstModOffset;
1111 scanStart.LowPart &= (-cm_chunkSize);
1112 thyper = ConvertLongToLargeInteger(cm_chunkSize);
1113 scanEnd = LargeIntegerAdd(scanStart, thyper);
1115 flags = CM_SCACHESYNC_GETSTATUS
1116 | CM_SCACHESYNC_STOREDATA
1117 | CM_SCACHESYNC_BUFLOCKED;
1119 /* start by looking backwards until scanStart */
1120 /* hyper version of cm_data.buf_blockSize */
1121 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1122 tbase = LargeIntegerSubtract(firstModOffset, thyper);
1123 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
1124 /* see if we can find the buffer */
1125 bufp = buf_Find(&scp->fid, &tbase);
1129 /* try to lock it, and quit if we can't (simplifies locking) */
1130 lock_ReleaseWrite(&scp->rw);
1131 code = lock_TryMutex(&bufp->mx);
1132 lock_ObtainWrite(&scp->rw);
1139 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1141 lock_ReleaseMutex(&bufp->mx);
1147 if (!(bufp->flags & CM_BUF_DIRTY)) {
1148 /* buffer is clean, so we shouldn't add it */
1149 cm_SyncOpDone(scp, bufp, flags);
1150 lock_ReleaseMutex(&bufp->mx);
1156 /* don't need buffer mutex any more */
1157 lock_ReleaseMutex(&bufp->mx);
1159 /* we have a dirty buffer ready for storing. Add it to the tail
1160 * of the list, since it immediately precedes all of the disk
1161 * addresses we've already collected.
1163 qdp = osi_QDAlloc();
1164 osi_SetQData(qdp, bufp);
1166 if ( cm_verifyData )
1167 buf_ComputeCheckSum(bufp);
1169 /* no buf_hold necessary, since we have it held from buf_Find */
1170 osi_QAddT((osi_queue_t **) &biop->bufListp,
1171 (osi_queue_t **) &biop->bufListEndp,
1173 bufp = NULL; /* added to the queue */
1175 /* update biod info describing the transfer */
1176 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
1177 biop->length += cm_data.buf_blockSize;
1179 /* update loop pointer */
1180 tbase = LargeIntegerSubtract(tbase, thyper);
1181 } /* while loop looking for pages preceding the one we found */
1183 /* now, find later dirty, contiguous pages, and add them to the list */
1184 /* hyper version of cm_data.buf_blockSize */
1185 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1186 tbase = LargeIntegerAdd(firstModOffset, thyper);
1187 while(LargeIntegerLessThan(tbase, scanEnd)) {
1188 /* see if we can find the buffer */
1189 bufp = buf_Find(&scp->fid, &tbase);
1193 /* try to lock it, and quit if we can't (simplifies locking) */
1194 lock_ReleaseWrite(&scp->rw);
1195 code = lock_TryMutex(&bufp->mx);
1196 lock_ObtainWrite(&scp->rw);
1203 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1205 lock_ReleaseMutex(&bufp->mx);
1211 if (!(bufp->flags & CM_BUF_DIRTY)) {
1212 /* buffer is clean, so we shouldn't add it */
1213 cm_SyncOpDone(scp, bufp, flags);
1214 lock_ReleaseMutex(&bufp->mx);
1220 /* don't need buffer mutex any more */
1221 lock_ReleaseMutex(&bufp->mx);
1223 /* we have a dirty buffer ready for storing. Add it to the head
1224 * of the list, since it immediately follows all of the disk
1225 * addresses we've already collected.
1227 qdp = osi_QDAlloc();
1228 osi_SetQData(qdp, bufp);
1230 if ( cm_verifyData )
1231 buf_ComputeCheckSum(bufp);
1233 /* no buf_hold necessary, since we have it held from buf_Find */
1234 osi_QAddH((osi_queue_t **) &biop->bufListp,
1235 (osi_queue_t **) &biop->bufListEndp,
1239 /* update biod info describing the transfer */
1240 biop->length += cm_data.buf_blockSize;
1242 /* update loop pointer */
1243 tbase = LargeIntegerAdd(tbase, thyper);
1244 } /* while loop looking for pages following the first page we found */
1246 /* finally, we're done */
1250 /* scp must be locked; temporarily unlocked during processing.
1251 * If returns 0, returns buffers held in biop, and with
1252 * CM_BUF_CMFETCHING flags set.
1253 * If an error is returned, we don't return any buffers.
1255 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1256 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1260 osi_hyper_t tblocksize; /* a long long temp variable */
1261 osi_hyper_t pageBase; /* base offset we're looking at */
1262 osi_queueData_t *qdp; /* one temp queue structure */
1263 osi_queueData_t *tqdp; /* another temp queue structure */
1264 long collected; /* how many bytes have been collected */
1267 osi_hyper_t fileSize; /* the # of bytes in the file */
1268 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1269 osi_queueData_t *heldBufListEndp; /* first one */
1270 afs_uint64 reserving;
1272 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1274 biop->scp = scp; /* do not hold; held by caller */
1275 biop->userp = userp; /* do not hold; held by caller */
1277 biop->offset = *offsetp;
1278 /* null out the list of buffers */
1279 biop->bufListp = biop->bufListEndp = NULL;
1282 /* first lookup the file's length, so we know when to stop */
1283 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1284 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1288 /* copy out size, since it may change */
1289 fileSize = scp->serverLength;
1291 lock_ReleaseWrite(&scp->rw);
1293 pageBase = *offsetp;
1294 collected = pageBase.LowPart & (cm_chunkSize - 1);
1295 heldBufListp = NULL;
1296 heldBufListEndp = NULL;
1299 * Obtaining buffers can cause dirty buffers to be recycled, which
1300 * can cause a storeback, so cannot be done while we have buffers
1303 * To get around this, we get buffers twice. Before reserving buffers,
1304 * we obtain and release each one individually. After reserving
1305 * buffers, we try to obtain them again, but only by lookup, not by
1306 * recycling. If a buffer has gone away while we were waiting for
1307 * the others, we just use whatever buffers we already have.
1309 * On entry to this function, we are already holding a buffer, so we
1310 * can't wait for reservation. So we call buf_TryReserveBuffers()
1311 * instead. Not only that, we can't really even call buf_Get(), for
1312 * the same reason. We can't avoid that, though. To avoid deadlock
1313 * we allow only one thread to be executing the buf_Get()-buf_Release()
1314 * sequence at a time.
1317 /* first hold all buffers, since we can't hold any locks in buf_Get */
1319 /* stop at chunk boundary */
1320 if (collected >= cm_chunkSize)
1323 /* see if the next page would be past EOF */
1324 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1327 code = buf_Get(scp, &pageBase, reqp, 0, &tbp);
1329 lock_ObtainWrite(&scp->rw);
1330 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1337 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1338 collected += cm_data.buf_blockSize;
1341 /* reserve a chunk's worth of buffers if possible */
1342 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1344 pageBase = *offsetp;
1345 collected = pageBase.LowPart & (cm_chunkSize - 1);
1347 /* now hold all buffers, if they are still there */
1349 /* stop at chunk boundary */
1350 if (collected >= cm_chunkSize)
1353 /* see if the next page would be past EOF */
1354 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1357 tbp = buf_Find(&scp->fid, &pageBase);
1361 /* add the buffer to the list */
1362 qdp = osi_QDAlloc();
1363 osi_SetQData(qdp, tbp);
1364 osi_QAddH((osi_queue_t **)&heldBufListp,
1365 (osi_queue_t **)&heldBufListEndp,
1367 /* leave tbp held (from buf_Get) */
1372 collected += cm_data.buf_blockSize;
1373 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1376 /* look at each buffer, adding it into the list if it looks idle and
1377 * filled with old data. One special case: wait for idle if it is the
1378 * first buffer since we really need that one for our caller to make
1382 collected = 0; /* now count how many we'll really use */
1383 for (tqdp = heldBufListEndp;
1385 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1386 /* get a ptr to the held buffer */
1387 tbp = osi_GetQData(tqdp);
1388 pageBase = tbp->offset;
1390 /* now lock the buffer lock */
1391 lock_ObtainMutex(&tbp->mx);
1392 lock_ObtainWrite(&scp->rw);
1394 /* don't bother fetching over data that is already current */
1395 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1396 /* we don't need this buffer, since it is current */
1397 lock_ReleaseWrite(&scp->rw);
1398 lock_ReleaseMutex(&tbp->mx);
1402 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1404 flags |= CM_SCACHESYNC_NOWAIT;
1406 /* wait for the buffer to serialize, if required. Doesn't
1407 * release the scp or buffer lock(s) if NOWAIT is specified.
1409 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1411 lock_ReleaseWrite(&scp->rw);
1412 lock_ReleaseMutex(&tbp->mx);
1416 /* don't fetch over dirty buffers */
1417 if (tbp->flags & CM_BUF_DIRTY) {
1418 cm_SyncOpDone(scp, tbp, flags);
1419 lock_ReleaseWrite(&scp->rw);
1420 lock_ReleaseMutex(&tbp->mx);
1425 lock_ReleaseWrite(&scp->rw);
1426 lock_ReleaseMutex(&tbp->mx);
1428 /* add the buffer to the list */
1429 qdp = osi_QDAlloc();
1430 osi_SetQData(qdp, tbp);
1431 osi_QAddH((osi_queue_t **)&biop->bufListp,
1432 (osi_queue_t **)&biop->bufListEndp,
1436 /* from now on, a failure just stops our collection process, but
1437 * we still do the I/O to whatever we've already managed to collect.
1440 collected += cm_data.buf_blockSize;
1443 /* now, we've held in biop->bufListp all the buffer's we're really
1444 * interested in. We also have holds left from heldBufListp, and we
1445 * now release those holds on the buffers.
1447 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1448 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1449 tbp = osi_GetQData(qdp);
1450 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1451 (osi_queue_t **) &heldBufListEndp,
1458 /* Caller expects this */
1459 lock_ObtainWrite(&scp->rw);
1461 /* if we got a failure setting up the first buffer, then we don't have
1462 * any side effects yet, and we also have failed an operation that the
1463 * caller requires to make any progress. Give up now.
1465 if (code && isFirst) {
1466 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1470 /* otherwise, we're still OK, and should just return the I/O setup we've
1473 biop->length = collected;
1474 biop->reserved = reserving;
1478 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1481 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
1483 cm_scache_t *scp; /* do not release; not held in biop */
1485 osi_queueData_t *qdp;
1486 osi_queueData_t *nqdp;
1488 int reportErrorToRedir = 0;
1490 /* Give back reserved buffers */
1492 buf_UnreserveBuffers(biop->reserved);
1495 flags = CM_SCACHESYNC_STOREDATA;
1497 flags = CM_SCACHESYNC_FETCHDATA;
1500 if (biop->bufListp) {
1501 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1502 /* lookup next guy first, since we're going to free this one */
1503 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1505 /* extract buffer and free queue data */
1506 bufp = osi_GetQData(qdp);
1507 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1508 (osi_queue_t **) &biop->bufListEndp,
1512 /* now, mark I/O as done, unlock the buffer and release it */
1514 lock_ReleaseWrite(&scp->rw);
1515 lock_ObtainMutex(&bufp->mx);
1516 lock_ObtainWrite(&scp->rw);
1517 cm_SyncOpDone(scp, bufp, flags);
1519 /* turn off writing and wakeup users */
1521 if (bufp->flags & CM_BUF_WAITING) {
1522 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1523 osi_Wakeup((LONG_PTR) bufp);
1526 _InterlockedAnd(&bufp->flags, ~CM_BUF_WRITING);
1528 case CM_ERROR_NOSUCHFILE:
1529 case CM_ERROR_BADFD:
1530 case CM_ERROR_NOACCESS:
1531 case CM_ERROR_QUOTA:
1532 case CM_ERROR_SPACE:
1533 case CM_ERROR_TOOBIG:
1534 case CM_ERROR_READONLY:
1535 case CM_ERROR_NOSUCHPATH:
1537 case CM_ERROR_INVAL_NET_RESP:
1538 case CM_ERROR_UNKNOWN:
1540 * Apply the fatal error to this buffer.
1542 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
1543 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
1544 bufp->dirty_length = 0;
1546 bufp->dataVersion = CM_BUF_VERSION_BAD;
1547 bufp->dirtyCounter++;
1548 reportErrorToRedir = 1;
1550 case CM_ERROR_TIMEDOUT:
1551 case CM_ERROR_ALLDOWN:
1552 case CM_ERROR_ALLBUSY:
1553 case CM_ERROR_ALLOFFLINE:
1554 case CM_ERROR_CLOCKSKEW:
1556 /* do not mark the buffer in error state but do
1557 * not attempt to complete the rest either.
1562 _InterlockedAnd(&bufp->flags, ~(CM_BUF_WRITING | CM_BUF_DIRTY));
1563 bufp->dirty_offset = bufp->dirty_length = 0;
1568 lock_ReleaseWrite(&scp->rw);
1569 lock_ReleaseMutex(&bufp->mx);
1574 if (RDR_Initialized && reportErrorToRedir) {
1576 smb_MapNTError(cm_MapRPCError(code, biop->reqp), &status, TRUE);
1577 RDR_SetFileStatus( &scp->fid, &biop->userp->authgroup, status);
1581 lock_ObtainWrite(&scp->rw);
1582 cm_SyncOpDone(scp, NULL, flags);
1584 lock_ReleaseWrite(&scp->rw);
1587 /* clean things out */
1588 biop->bufListp = NULL;
1589 biop->bufListEndp = NULL;
1593 cm_CloneStatus(cm_scache_t *scp, cm_user_t *userp, int scp_locked,
1594 AFSFetchStatus *afsStatusp, AFSVolSync *volSyncp)
1596 // setup the status based upon the scp data
1597 afsStatusp->InterfaceVersion = 0x1;
1598 switch (scp->fileType) {
1599 case CM_SCACHETYPE_FILE:
1600 afsStatusp->FileType = File;
1602 case CM_SCACHETYPE_DIRECTORY:
1603 afsStatusp->FileType = Directory;
1605 case CM_SCACHETYPE_MOUNTPOINT:
1606 afsStatusp->FileType = SymbolicLink;
1608 case CM_SCACHETYPE_SYMLINK:
1609 case CM_SCACHETYPE_DFSLINK:
1610 afsStatusp->FileType = SymbolicLink;
1613 afsStatusp->FileType = -1; /* an invalid value */
1615 afsStatusp->LinkCount = scp->linkCount;
1616 afsStatusp->Length = scp->length.LowPart;
1617 afsStatusp->DataVersion = (afs_uint32)(scp->dataVersion & MAX_AFS_UINT32);
1618 afsStatusp->Author = 0x1;
1619 afsStatusp->Owner = scp->owner;
1621 lock_ObtainWrite(&scp->rw);
1624 if (cm_FindACLCache(scp, userp, &afsStatusp->CallerAccess))
1625 afsStatusp->CallerAccess = scp->anyAccess;
1626 afsStatusp->AnonymousAccess = scp->anyAccess;
1627 afsStatusp->UnixModeBits = scp->unixModeBits;
1628 afsStatusp->ParentVnode = scp->parentVnode;
1629 afsStatusp->ParentUnique = scp->parentUnique;
1630 afsStatusp->ResidencyMask = 0;
1631 afsStatusp->ClientModTime = scp->clientModTime;
1632 afsStatusp->ServerModTime = scp->serverModTime;
1633 afsStatusp->Group = scp->group;
1634 afsStatusp->SyncCounter = 0;
1635 afsStatusp->dataVersionHigh = (afs_uint32)(scp->dataVersion >> 32);
1636 afsStatusp->lockCount = 0;
1637 afsStatusp->Length_hi = scp->length.HighPart;
1638 afsStatusp->errorCode = 0;
1640 volSyncp->spare1 = scp->volumeCreationDate;
1645 /* Fetch a buffer. Called with scp locked.
1646 * The scp is locked on return.
1648 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1651 long code=0, code1=0;
1652 afs_uint32 nbytes; /* bytes in transfer */
1653 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1654 afs_uint64 length_found = 0;
1655 long rxbytes; /* bytes in rx_Read call */
1657 AFSFetchStatus afsStatus;
1658 AFSCallBack callback;
1661 afs_uint32 buffer_offset;
1662 cm_buf_t *tbufp; /* buf we're filling */
1663 osi_queueData_t *qdp; /* q element we're scanning */
1665 struct rx_call *rxcallp;
1666 struct rx_connection *rxconnp;
1667 cm_bulkIO_t biod; /* bulk IO descriptor */
1671 int require_64bit_ops = 0;
1672 int call_was_64bit = 0;
1673 int fs_fetchdata_offset_bug = 0;
1677 memset(&afsStatus, 0, sizeof(afsStatus));
1678 memset(&callback, 0, sizeof(callback));
1679 memset(&volSync, 0, sizeof(volSync));
1681 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
1682 * drops lots of locks, and may indeed return a properly initialized
1683 * buffer, although more likely it will just return a new, empty, buffer.
1686 #ifdef AFS_FREELANCE_CLIENT
1688 // yj: if they're trying to get the /afs directory, we need to
1689 // handle it differently, since it's local rather than on any
1692 getroot = (scp==cm_data.rootSCachep);
1694 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1697 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1698 /* We already have this buffer don't do extra work */
1702 cm_AFSFidFromFid(&tfid, &scp->fid);
1704 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1706 /* couldn't even get the first page setup properly */
1707 osi_Log1(afsd_logp, "cm_GetBuffer: SetupFetchBIOD failure code %d", code);
1711 /* once we get here, we have the callback in place, we know that no one
1712 * is fetching the data now. Check one last time that we still have
1713 * the wrong data, and then fetch it if we're still wrong.
1715 * We can lose a race condition and end up with biod.length zero, in
1716 * which case we just retry.
1719 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1720 /* We obtained the buffer while we were waiting */
1724 if (biod.length == 0) {
1725 osi_Log2(afsd_logp, "cm_GetBuffer BIOD length 0 scp 0x%p bufp 0x%p",
1730 /* Check for buffers we can fill locally */
1731 for (qdp = biod.bufListEndp;
1733 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1734 tbufp = osi_GetQData(qdp);
1736 if ( tbufp->dataVersion == CM_BUF_VERSION_BAD ||
1737 tbufp->dataVersion < scp->bufDataVersionLow ||
1738 tbufp->dataVersion > scp->dataVersion) {
1740 osi_Log4(afsd_logp, "cm_GetBuffer bufp 0x%p DVs 0x%x != (0x%x -> 0x%x)",
1741 tbufp, tbufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion);
1743 if (LargeIntegerGreaterThanOrEqualTo(tbufp->offset, scp->length))
1745 if (tbufp->dataVersion == CM_BUF_VERSION_BAD)
1746 memset(tbufp->datap, 0, cm_data.buf_blockSize);
1747 tbufp->dataVersion = scp->dataVersion;
1750 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1751 LargeIntegerGreaterThanOrEqualTo(tbufp->offset, scp->truncPos)) {
1752 memset(tbufp->datap, 0, cm_data.buf_blockSize);
1753 tbufp->dataVersion = scp->dataVersion;
1758 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1759 /* We locally populated the buffer we wanted */
1763 InterlockedIncrement(&scp->activeRPCs);
1764 lock_ReleaseWrite(&scp->rw);
1767 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1768 ConvertLongToLargeInteger(biod.length)),
1769 ConvertLongToLargeInteger(LONG_MAX))) {
1770 require_64bit_ops = 1;
1773 osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1774 osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1775 scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1777 #ifdef AFS_FREELANCE_CLIENT
1780 // if getroot then we don't need to make any calls
1781 // just return fake data
1783 if (cm_freelanceEnabled && getroot) {
1784 // setup the fake status
1785 afsStatus.InterfaceVersion = 0x1;
1786 afsStatus.FileType = 0x2;
1787 afsStatus.LinkCount = scp->linkCount;
1788 afsStatus.Length = cm_fakeDirSize;
1789 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1790 afsStatus.Author = 0x1;
1791 afsStatus.Owner = 0x0;
1792 afsStatus.CallerAccess = 0x9;
1793 afsStatus.AnonymousAccess = 0x9;
1794 afsStatus.UnixModeBits = 0x1ff;
1795 afsStatus.ParentVnode = 0x1;
1796 afsStatus.ParentUnique = 0x1;
1797 afsStatus.ResidencyMask = 0;
1798 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1799 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1800 afsStatus.Group = 0;
1801 afsStatus.SyncCounter = 0;
1802 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1803 afsStatus.lockCount = 0;
1804 afsStatus.Length_hi = 0;
1805 afsStatus.errorCode = 0;
1806 memset(&volSync, 0, sizeof(volSync));
1808 // once we're done setting up the status info,
1809 // we just fill the buffer pages with fakedata
1810 // from cm_FakeRootDir. Extra pages are set to
1813 lock_ObtainMutex(&cm_Freelance_Lock);
1814 t1 = bufp->offset.LowPart;
1815 qdp = biod.bufListEndp;
1817 tbufp = osi_GetQData(qdp);
1818 bufferp=tbufp->datap;
1819 memset(bufferp, 0, cm_data.buf_blockSize);
1820 t2 = cm_fakeDirSize - t1;
1821 if (t2> (afs_int32)cm_data.buf_blockSize)
1822 t2=cm_data.buf_blockSize;
1824 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1829 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1832 lock_ReleaseMutex(&cm_Freelance_Lock);
1834 // once we're done, we skip over the part of the
1835 // code that does the ACTUAL fetching of data for
1838 goto fetchingcompleted;
1841 #endif /* AFS_FREELANCE_CLIENT */
1844 * if the requested offset is greater than the file length,
1845 * the file server will return zero bytes of data and the
1846 * current status for the file which we already have since
1847 * we have just obtained a callback. Instead, we can avoid
1848 * the network round trip by allocating zeroed buffers and
1849 * faking the status info.
1851 if (biod.offset.QuadPart >= scp->serverLength.QuadPart ||
1852 ((scp->mask & CM_SCACHEMASK_TRUNCPOS) && biod.offset.QuadPart > scp->truncPos.QuadPart)) {
1853 osi_Log5(afsd_logp, "SKIP FetchData64 scp 0x%p, off 0x%x:%08x > length 0x%x:%08x",
1854 scp, biod.offset.HighPart, biod.offset.LowPart,
1855 scp->length.HighPart, scp->length.LowPart);
1857 /* Clone the current status info */
1858 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
1860 /* status info complete, fill pages with zeros */
1861 for (qdp = biod.bufListEndp;
1863 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1864 tbufp = osi_GetQData(qdp);
1865 bufferp=tbufp->datap;
1866 memset(bufferp, 0, cm_data.buf_blockSize);
1869 /* no need to contact the file server */
1870 goto fetchingcompleted;
1874 lock_ReleaseWrite(&scp->rw);
1878 /* now make the call */
1880 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1884 rxconnp = cm_GetRxConn(connp);
1885 rxcallp = rx_NewCall(rxconnp);
1886 rx_PutConnection(rxconnp);
1888 nbytes = nbytes_hi = 0;
1890 if (SERVERHAS64BIT(connp)) {
1893 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1894 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1896 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1899 temp = rx_Read32(rxcallp, &nbytes_hi);
1900 if (temp == sizeof(afs_int32)) {
1901 nbytes_hi = ntohl(nbytes_hi);
1904 code = rx_Error(rxcallp);
1905 code1 = rx_EndCall(rxcallp, code);
1913 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1914 if (require_64bit_ops) {
1915 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1916 code = CM_ERROR_TOOBIG;
1919 rxconnp = cm_GetRxConn(connp);
1920 rxcallp = rx_NewCall(rxconnp);
1921 rx_PutConnection(rxconnp);
1924 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1925 scp, biod.offset.LowPart, biod.length);
1927 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1930 SET_SERVERHASNO64BIT(connp);
1935 temp = rx_Read32(rxcallp, &nbytes);
1936 if (temp == sizeof(afs_int32)) {
1937 nbytes = ntohl(nbytes);
1938 FillInt64(length_found, nbytes_hi, nbytes);
1939 if (length_found > biod.length) {
1941 * prior to 1.4.12 and 1.5.65 the file server would return
1942 * (filesize - offset) if the requested offset was greater than
1943 * the filesize. The correct return value would have been zero.
1944 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
1945 * is a race between two RPCs issues by this cache manager, the
1946 * correct thing will happen the second time.
1948 osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1949 fs_fetchdata_offset_bug = 1;
1952 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1953 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
1956 /* for the moment, nbytes_hi will always be 0 if code == 0
1957 because biod.length is a 32-bit quantity. */
1960 qdp = biod.bufListEndp;
1962 tbufp = osi_GetQData(qdp);
1963 bufferp = tbufp->datap;
1969 /* fill length_found of data from the pipe into the pages.
1970 * When we stop, qdp will point at the last page we're
1971 * dealing with, and bufferp will tell us where we
1972 * stopped. We'll need this info below when we clear
1973 * the remainder of the last page out (and potentially
1974 * clear later pages out, if we fetch past EOF).
1976 while (length_found > 0) {
1978 struct iovec tiov[RX_MAXIOVECS];
1979 afs_int32 tnio, iov, iov_offset;
1981 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
1982 osi_Log1(afsd_logp, "cm_GetBuffer rx_Readv returns %d", temp);
1983 if (temp != length_found && temp < cm_data.buf_blockSize) {
1985 * If the file server returned (filesize - offset),
1986 * then the first rx_Read will return zero octets of data.
1987 * If it does, do not treat it as an error. Correct the
1988 * length_found and continue as if the file server said
1989 * it was sending us zero octets of data.
1991 if (fs_fetchdata_offset_bug && first_read) {
1994 } else if (temp <= 0) {
1995 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2004 while (rxbytes > 0) {
2007 osi_assertx(bufferp != NULL, "null cm_buf_t");
2009 len = min(tiov[iov].iov_len - iov_offset, cm_data.buf_blockSize - buffer_offset);
2010 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
2012 buffer_offset += len;
2015 if (iov_offset == tiov[iov].iov_len) {
2020 if (buffer_offset == cm_data.buf_blockSize) {
2021 /* allow read-while-fetching.
2022 * if this is the last buffer, clear the
2023 * PREFETCHING flag, so the reader waiting for
2024 * this buffer will start a prefetch.
2026 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
2027 lock_ObtainWrite(&scp->rw);
2028 if (!osi_QIsEmpty(&scp->waitQueueH)) {
2029 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
2030 osi_Wakeup((LONG_PTR) &scp->flags);
2032 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
2033 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
2035 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
2037 lock_ReleaseWrite(&scp->rw);
2039 /* Advance the buffer */
2040 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2042 tbufp = osi_GetQData(qdp);
2043 bufferp = tbufp->datap;
2051 length_found -= temp;
2052 #else /* USE_RX_IOVEC */
2053 /* assert that there are still more buffers;
2054 * our check above for length_found being less than
2055 * biod.length should ensure this.
2057 osi_assertx(bufferp != NULL, "null cm_buf_t");
2059 /* read rxbytes of data */
2060 rxbytes = (afs_uint32)(length_found > cm_data.buf_blockSize ? cm_data.buf_blockSize : length_found);
2061 temp = rx_Read(rxcallp, bufferp, rxbytes);
2062 if (temp < rxbytes) {
2064 * If the file server returned (filesize - offset),
2065 * then the first rx_Read will return zero octets of data.
2066 * If it does, do not treat it as an error. Correct the
2067 * length_found and continue as if the file server said
2068 * it was sending us zero octets of data.
2070 if (fs_fetchdata_offset_bug && first_read) {
2073 } else if (temp <= 0) {
2074 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2080 /* allow read-while-fetching.
2081 * if this is the last buffer, clear the
2082 * PREFETCHING flag, so the reader waiting for
2083 * this buffer will start a prefetch.
2085 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
2086 lock_ObtainWrite(&scp->rw);
2087 if (!osi_QIsEmpty(&scp->waitQueueH)) {
2088 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
2089 osi_Wakeup((LONG_PTR) &scp->flags);
2091 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
2092 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
2094 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
2096 lock_ReleaseWrite(&scp->rw);
2098 /* and adjust counters */
2099 length_found -= temp;
2101 /* and move to the next buffer */
2102 if (length_found != 0) {
2103 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2105 tbufp = osi_GetQData(qdp);
2106 bufferp = tbufp->datap;
2112 #endif /* USE_RX_IOVEC */
2115 /* zero out remainder of last pages, in case we are
2116 * fetching past EOF. We were fetching an integral #
2117 * of pages, but stopped, potentially in the middle of
2118 * a page. Zero the remainder of that page, and then
2119 * all of the rest of the pages.
2122 rxbytes = cm_data.buf_blockSize - buffer_offset;
2123 bufferp = tbufp->datap + buffer_offset;
2124 #else /* USE_RX_IOVEC */
2126 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
2127 rxbytes = (long) (bufferp - tbufp->datap);
2129 /* bytes left to zero */
2130 rxbytes = cm_data.buf_blockSize - rxbytes;
2131 #endif /* USE_RX_IOVEC */
2134 memset(bufferp, 0, rxbytes);
2135 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2138 tbufp = osi_GetQData(qdp);
2139 bufferp = tbufp->datap;
2140 /* bytes to clear in this page */
2141 rxbytes = cm_data.buf_blockSize;
2147 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2149 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2152 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2154 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2158 code1 = rx_EndCall(rxcallp, code);
2160 if (code1 == RXKADUNKNOWNKEY)
2161 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2163 /* If we are avoiding a file server bug, ignore the error state */
2164 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2165 /* Clone the current status info and clear the error state */
2166 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2168 lock_ReleaseWrite(&scp->rw);
2172 /* Prefer the error value from FetchData over rx_EndCall */
2173 } else if (code == 0 && code1 != 0)
2175 osi_Log0(afsd_logp, "CALL FetchData DONE");
2177 } while (cm_Analyze(connp, userp, reqp, &scp->fid, NULL, 0, &afsStatus, &volSync, NULL, NULL, code));
2180 code = cm_MapRPCError(code, reqp);
2183 lock_ObtainWrite(&scp->rw);
2185 /* we know that no one else has changed the buffer, since we still have
2186 * the fetching flag on the buffers, and we have the scp locked again.
2187 * Copy in the version # into the buffer if we got code 0 back from the
2191 for(qdp = biod.bufListp;
2193 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
2194 tbufp = osi_GetQData(qdp);
2195 tbufp->dataVersion = afsStatus.dataVersionHigh;
2196 tbufp->dataVersion <<= 32;
2197 tbufp->dataVersion |= afsStatus.DataVersion;
2200 /* write buffer out to disk cache */
2201 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
2202 tbufp->dataVersion);
2203 #endif /* DISKCACHE95 */
2208 code = cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2210 InterlockedDecrement(&scp->activeRPCs);
2213 /* release scatter/gather I/O structure (buffers, locks) */
2214 cm_ReleaseBIOD(&biod, 0, code, 1);
2220 * Similar to cm_GetBuffer but doesn't use an allocated cm_buf_t object.
2221 * Instead the data is read from the file server and copied directly into
2222 * a provided buffer. Called with scp locked. The scp is locked on return.
2224 long cm_GetData(cm_scache_t *scp, osi_hyper_t *offsetp, char *datap, int data_length,
2225 cm_user_t *userp, cm_req_t *reqp)
2227 long code=0, code1=0;
2228 afs_uint32 nbytes; /* bytes in transfer */
2229 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2230 afs_uint64 length_found = 0;
2231 char *bufferp = datap;
2232 afs_uint32 buffer_offset = 0;
2233 long rxbytes; /* bytes in rx_Read call */
2235 AFSFetchStatus afsStatus;
2236 AFSCallBack callback;
2239 struct rx_call *rxcallp;
2240 struct rx_connection *rxconnp;
2244 int require_64bit_ops = 0;
2245 int call_was_64bit = 0;
2246 int fs_fetchdata_offset_bug = 0;
2250 memset(&afsStatus, 0, sizeof(afsStatus));
2251 memset(&callback, 0, sizeof(callback));
2252 memset(&volSync, 0, sizeof(volSync));
2254 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
2255 * drops lots of locks, and may indeed return a properly initialized
2256 * buffer, although more likely it will just return a new, empty, buffer.
2259 #ifdef AFS_FREELANCE_CLIENT
2261 // yj: if they're trying to get the /afs directory, we need to
2262 // handle it differently, since it's local rather than on any
2265 getroot = (scp==cm_data.rootSCachep);
2267 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
2270 cm_AFSFidFromFid(&tfid, &scp->fid);
2272 if (LargeIntegerGreaterThan(LargeIntegerAdd(*offsetp,
2273 ConvertLongToLargeInteger(data_length)),
2274 ConvertLongToLargeInteger(LONG_MAX))) {
2275 require_64bit_ops = 1;
2278 InterlockedIncrement(&scp->activeRPCs);
2279 osi_Log2(afsd_logp, "cm_GetData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2281 #ifdef AFS_FREELANCE_CLIENT
2284 // if getroot then we don't need to make any calls
2285 // just return fake data
2287 if (cm_freelanceEnabled && getroot) {
2288 // setup the fake status
2289 afsStatus.InterfaceVersion = 0x1;
2290 afsStatus.FileType = 0x2;
2291 afsStatus.LinkCount = scp->linkCount;
2292 afsStatus.Length = cm_fakeDirSize;
2293 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
2294 afsStatus.Author = 0x1;
2295 afsStatus.Owner = 0x0;
2296 afsStatus.CallerAccess = 0x9;
2297 afsStatus.AnonymousAccess = 0x9;
2298 afsStatus.UnixModeBits = 0x1ff;
2299 afsStatus.ParentVnode = 0x1;
2300 afsStatus.ParentUnique = 0x1;
2301 afsStatus.ResidencyMask = 0;
2302 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
2303 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
2304 afsStatus.Group = 0;
2305 afsStatus.SyncCounter = 0;
2306 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
2307 afsStatus.lockCount = 0;
2308 afsStatus.Length_hi = 0;
2309 afsStatus.errorCode = 0;
2310 memset(&volSync, 0, sizeof(volSync));
2312 // once we're done setting up the status info,
2313 // we just fill the buffer pages with fakedata
2314 // from cm_FakeRootDir. Extra pages are set to
2317 lock_ObtainMutex(&cm_Freelance_Lock);
2318 t1 = offsetp->LowPart;
2319 memset(datap, 0, data_length);
2320 t2 = cm_fakeDirSize - t1;
2321 if (t2 > data_length)
2324 memcpy(datap, cm_FakeRootDir+t1, t2);
2325 lock_ReleaseMutex(&cm_Freelance_Lock);
2327 // once we're done, we skip over the part of the
2328 // code that does the ACTUAL fetching of data for
2331 goto fetchingcompleted;
2334 #endif /* AFS_FREELANCE_CLIENT */
2337 lock_ReleaseWrite(&scp->rw);
2341 /* now make the call */
2343 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2347 rxconnp = cm_GetRxConn(connp);
2348 rxcallp = rx_NewCall(rxconnp);
2349 rx_PutConnection(rxconnp);
2351 nbytes = nbytes_hi = 0;
2353 if (SERVERHAS64BIT(connp)) {
2356 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2357 scp, offsetp->HighPart, offsetp->LowPart, data_length);
2359 code = StartRXAFS_FetchData64(rxcallp, &tfid, offsetp->QuadPart, data_length);
2362 temp = rx_Read32(rxcallp, &nbytes_hi);
2363 if (temp == sizeof(afs_int32)) {
2364 nbytes_hi = ntohl(nbytes_hi);
2367 code = rx_Error(rxcallp);
2368 code1 = rx_EndCall(rxcallp, code);
2376 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2377 if (require_64bit_ops) {
2378 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2379 code = CM_ERROR_TOOBIG;
2382 rxconnp = cm_GetRxConn(connp);
2383 rxcallp = rx_NewCall(rxconnp);
2384 rx_PutConnection(rxconnp);
2387 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2388 scp, offsetp->LowPart, data_length);
2390 code = StartRXAFS_FetchData(rxcallp, &tfid, offsetp->LowPart, data_length);
2392 SET_SERVERHASNO64BIT(connp);
2397 temp = rx_Read32(rxcallp, &nbytes);
2398 if (temp == sizeof(afs_int32)) {
2399 nbytes = ntohl(nbytes);
2400 FillInt64(length_found, nbytes_hi, nbytes);
2401 if (length_found > data_length) {
2403 * prior to 1.4.12 and 1.5.65 the file server would return
2404 * (filesize - offset) if the requested offset was greater than
2405 * the filesize. The correct return value would have been zero.
2406 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2407 * is a race between two RPCs issues by this cache manager, the
2408 * correct thing will happen the second time.
2410 osi_Log0(afsd_logp, "cm_GetData length_found > data_length");
2411 fs_fetchdata_offset_bug = 1;
2414 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2415 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2418 /* for the moment, nbytes_hi will always be 0 if code == 0
2419 because data_length is a 32-bit quantity. */
2422 /* fill length_found of data from the pipe into the pages.
2423 * When we stop, qdp will point at the last page we're
2424 * dealing with, and bufferp will tell us where we
2425 * stopped. We'll need this info below when we clear
2426 * the remainder of the last page out (and potentially
2427 * clear later pages out, if we fetch past EOF).
2429 while (length_found > 0) {
2431 struct iovec tiov[RX_MAXIOVECS];
2432 afs_int32 tnio, iov, iov_offset;
2434 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
2435 osi_Log1(afsd_logp, "cm_GetData rx_Readv returns %d", temp);
2436 if (temp != length_found && temp < data_length) {
2438 * If the file server returned (filesize - offset),
2439 * then the first rx_Read will return zero octets of data.
2440 * If it does, do not treat it as an error. Correct the
2441 * length_found and continue as if the file server said
2442 * it was sending us zero octets of data.
2444 if (fs_fetchdata_offset_bug && first_read) {
2447 } else if (temp <= 0) {
2448 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2457 while (rxbytes > 0) {
2460 osi_assertx(bufferp != NULL, "null cm_buf_t");
2462 len = min(tiov[iov].iov_len - iov_offset, data_length - buffer_offset);
2463 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
2465 buffer_offset += len;
2468 if (iov_offset == tiov[iov].iov_len) {
2474 length_found -= temp;
2475 #else /* USE_RX_IOVEC */
2476 /* assert that there are still more buffers;
2477 * our check above for length_found being less than
2478 * data_length should ensure this.
2480 osi_assertx(bufferp != NULL, "null cm_buf_t");
2482 /* read rxbytes of data */
2483 rxbytes = (afs_uint32)(length_found > data_length ? data_length : length_found);
2484 temp = rx_Read(rxcallp, bufferp, rxbytes);
2485 if (temp < rxbytes) {
2487 * If the file server returned (filesize - offset),
2488 * then the first rx_Read will return zero octets of data.
2489 * If it does, do not treat it as an error. Correct the
2490 * length_found and continue as if the file server said
2491 * it was sending us zero octets of data.
2493 if (fs_fetchdata_offset_bug && first_read) {
2496 } else if (temp <= 0) {
2497 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2503 /* and adjust counters */
2504 length_found -= temp;
2505 #endif /* USE_RX_IOVEC */
2508 /* zero out remainder of last pages, in case we are
2509 * fetching past EOF. We were fetching an integral #
2510 * of pages, but stopped, potentially in the middle of
2511 * a page. Zero the remainder of that page, and then
2512 * all of the rest of the pages.
2515 rxbytes = data_length - buffer_offset;
2516 bufferp = datap + buffer_offset;
2517 #else /* USE_RX_IOVEC */
2519 osi_assertx((bufferp - datap) < LONG_MAX, "data >= LONG_MAX");
2520 rxbytes = (long) (bufferp - datap);
2522 /* bytes left to zero */
2523 rxbytes = data_length - rxbytes;
2524 #endif /* USE_RX_IOVEC */
2526 memset(bufferp, 0, rxbytes);
2531 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2533 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2536 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2538 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2542 code1 = rx_EndCall(rxcallp, code);
2544 if (code1 == RXKADUNKNOWNKEY)
2545 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2547 /* If we are avoiding a file server bug, ignore the error state */
2548 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2549 /* Clone the current status info and clear the error state */
2550 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2552 lock_ReleaseWrite(&scp->rw);
2556 /* Prefer the error value from FetchData over rx_EndCall */
2557 } else if (code == 0 && code1 != 0)
2559 osi_Log0(afsd_logp, "CALL FetchData DONE");
2561 } while (cm_Analyze(connp, userp, reqp, &scp->fid, NULL, 0, &afsStatus, &volSync, NULL, NULL, code));
2564 code = cm_MapRPCError(code, reqp);
2567 lock_ObtainWrite(&scp->rw);
2570 code = cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2572 InterlockedDecrement(&scp->activeRPCs);
2578 * cm_VerifyStoreData. Function passed a rw locked cm_scache_t and a store data biod.
2580 * Return 1 if the data verifies; 0 if not.
2584 cm_VerifyStoreData(cm_bulkIO_t *biod, cm_scache_t *savedScp)
2586 long code=0, code1=0;
2587 afs_uint32 nbytes; /* bytes in transfer */
2588 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2589 afs_uint64 length_found = 0;
2590 long rxbytes; /* bytes in rx_Read call */
2592 AFSFetchStatus afsStatus;
2593 AFSCallBack callback;
2596 struct rx_call *rxcallp;
2597 struct rx_connection *rxconnp;
2599 int require_64bit_ops = 0;
2600 int call_was_64bit = 0;
2601 int fs_fetchdata_offset_bug = 0;
2604 char * bufferp = malloc(biod->length);
2606 cm_scache_t *scp = biod->scp;
2607 cm_user_t *userp = biod->userp;
2608 cm_req_t *reqp = biod->reqp;
2609 afs_uint64 dataVersion = scp->dataVersion;
2611 memset(&afsStatus, 0, sizeof(afsStatus));
2612 memset(&callback, 0, sizeof(callback));
2613 memset(&volSync, 0, sizeof(volSync));
2614 memset(bufferp, 0, biod->length);
2616 cm_AFSFidFromFid(&tfid, &scp->fid);
2618 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod->offset,
2619 ConvertLongToLargeInteger(biod->length)),
2620 ConvertLongToLargeInteger(LONG_MAX))) {
2621 require_64bit_ops = 1;
2624 InterlockedIncrement(&scp->activeRPCs);
2625 osi_Log2(afsd_logp, "cm_VerifyStoreData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2628 lock_ReleaseWrite(&scp->rw);
2632 /* now make the call */
2634 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2638 rxconnp = cm_GetRxConn(connp);
2639 rxcallp = rx_NewCall(rxconnp);
2640 rx_PutConnection(rxconnp);
2642 nbytes = nbytes_hi = 0;
2644 if (SERVERHAS64BIT(connp)) {
2647 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2648 scp, biod->offset.HighPart, biod->offset.LowPart, biod->length);
2650 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod->offset.QuadPart, biod->length);
2653 temp = rx_Read32(rxcallp, &nbytes_hi);
2654 if (temp == sizeof(afs_int32)) {
2655 nbytes_hi = ntohl(nbytes_hi);
2658 code = rx_Error(rxcallp);
2659 code1 = rx_EndCall(rxcallp, code);
2667 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2668 if (require_64bit_ops) {
2669 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2670 code = CM_ERROR_TOOBIG;
2673 rxconnp = cm_GetRxConn(connp);
2674 rxcallp = rx_NewCall(rxconnp);
2675 rx_PutConnection(rxconnp);
2678 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2679 scp, biod->offset.LowPart, biod->length);
2681 code = StartRXAFS_FetchData(rxcallp, &tfid, biod->offset.LowPart, biod->length);
2683 SET_SERVERHASNO64BIT(connp);
2688 temp = rx_Read32(rxcallp, &nbytes);
2689 if (temp == sizeof(afs_int32)) {
2690 nbytes = ntohl(nbytes);
2691 FillInt64(length_found, nbytes_hi, nbytes);
2692 if (length_found > biod->length) {
2694 * prior to 1.4.12 and 1.5.65 the file server would return
2695 * (filesize - offset) if the requested offset was greater than
2696 * the filesize. The correct return value would have been zero.
2697 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2698 * is a race between two RPCs issues by this cache manager, the
2699 * correct thing will happen the second time.
2701 osi_Log0(afsd_logp, "cm_GetData length_found > biod.length");
2702 fs_fetchdata_offset_bug = 1;
2705 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2706 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2709 /* for the moment, nbytes_hi will always be 0 if code == 0
2710 because data_length is a 32-bit quantity. */
2713 /* fill length_found of data from the pipe into the pages.
2714 * When we stop, qdp will point at the last page we're
2715 * dealing with, and bufferp will tell us where we
2716 * stopped. We'll need this info below when we clear
2717 * the remainder of the last page out (and potentially
2718 * clear later pages out, if we fetch past EOF).
2720 while (length_found > 0) {
2721 /* assert that there are still more buffers;
2722 * our check above for length_found being less than
2723 * data_length should ensure this.
2725 osi_assertx(bufferp != NULL, "null cm_buf_t");
2727 /* read rxbytes of data */
2728 rxbytes = (afs_uint32)(length_found > biod->length ? biod->length : length_found);
2729 temp = rx_Read(rxcallp, bufferp, rxbytes);
2730 if (temp < rxbytes) {
2732 * If the file server returned (filesize - offset),
2733 * then the first rx_Read will return zero octets of data.
2734 * If it does, do not treat it as an error. Correct the
2735 * length_found and continue as if the file server said
2736 * it was sending us zero octets of data.
2738 if (fs_fetchdata_offset_bug && first_read) {
2741 } else if (temp <= 0) {
2742 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2748 /* and adjust counters */
2749 length_found -= temp;
2755 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2757 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2760 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2762 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2766 code1 = rx_EndCall(rxcallp, code);
2768 if (code1 == RXKADUNKNOWNKEY)
2769 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2771 /* If we are avoiding a file server bug, ignore the error state */
2772 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2773 /* Clone the current status info and clear the error state */
2774 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2776 lock_ReleaseWrite(&scp->rw);
2780 /* Prefer the error value from FetchData over rx_EndCall */
2781 } else if (code == 0 && code1 != 0)
2783 osi_Log0(afsd_logp, "CALL FetchData DONE");
2785 } while (cm_Analyze(connp, userp, reqp, &scp->fid, NULL, 0, &afsStatus, &volSync, NULL, NULL, code));
2788 code = cm_MapRPCError(code, reqp);
2791 lock_ObtainWrite(&scp->rw);
2794 code = cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2796 InterlockedDecrement(&scp->activeRPCs);
2800 if (dataVersion == scp->dataVersion)
2802 osi_queueData_t *qdp = NULL;
2804 afs_uint32 buf_offset;
2805 afs_uint32 bytes_compared = 0;
2806 afs_uint32 cmp_length;
2811 while ( bytes_compared < biod->length )
2814 qdp = biod->bufListEndp;
2815 buf_offset = biod->offset.LowPart % cm_data.buf_blockSize;
2817 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2820 cmp_length = cm_data.buf_blockSize - buf_offset;
2821 if (cmp_length > biod->length - bytes_compared)
2822 cmp_length = biod->length - bytes_compared;
2824 osi_assertx(qdp != NULL, "null osi_queueData_t");
2825 bufp = osi_GetQData(qdp);
2827 if (memcmp(bufferp+bytes_compared, bufp->datap+buf_offset, cmp_length) != 0)
2830 md5_match = buf_ValidateCheckSum(bufp);
2832 osi_Log5(afsd_logp, "cm_VerifyDataStore verification failed scp 0x%p bufp 0x%p offset 0x%x:%08x md5 %s",
2833 scp, bufp, bufp->offset.HighPart, bufp->offset.LowPart, md5_match ? "match" : "no-match");
2835 bytes_compared += cmp_length;
2838 osi_Log4(afsd_logp, "cm_VerifyStoreData unable to verify due to data version change scp 0x%p, off 0x%x:%08x, size 0x%x",
2839 scp, biod->offset.HighPart, biod->offset.LowPart, biod->length);