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, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
777 cm_user_t *userp, cm_req_t *reqp)
782 afs_uint32 req_flags = reqp->flags;
784 if (scp->flags & CM_SCACHEFLAG_DELETED) {
785 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
787 /* Retries will be performed by the BkgDaemon thread if appropriate */
788 reqp->flags |= CM_REQ_NORETRY;
790 toffset.LowPart = p1;
791 toffset.HighPart = p2;
794 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
796 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, reqp);
798 osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
800 reqp->flags = req_flags;
804 * Keep the following list synchronized with the
805 * error code list in cm_BkgDaemon
808 case CM_ERROR_TIMEDOUT: /* or server restarting */
810 case CM_ERROR_WOULDBLOCK:
811 case CM_ERROR_ALLBUSY:
812 case CM_ERROR_ALLDOWN:
813 case CM_ERROR_ALLOFFLINE:
814 case CM_ERROR_PARTIALWRITE:
815 break; /* cm_BkgDaemon will re-insert the request in the queue */
818 lock_ObtainWrite(&scp->rw);
819 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
820 lock_ReleaseWrite(&scp->rw);
826 /* Called with scp locked */
827 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
832 end = LargeIntegerAdd(*base, *length);
833 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
834 scp->prefetch.base = *base;
835 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
836 scp->prefetch.end = end;
838 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
841 /* do the prefetch. if the prefetch fails, return 0 (success)
842 * because there is no harm done. */
844 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
845 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);
868 length.HighPart = p4;
870 end = LargeIntegerAdd(base, length);
872 osi_Log5(afsd_logp, "Starting BKG prefetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
873 scp, p2, p1, p4, p3);
875 for ( code = 0, offset = base;
876 code == 0 && LargeIntegerLessThan(offset, end);
877 offset = LargeIntegerAdd(offset, tblocksize) )
880 lock_ReleaseWrite(&scp->rw);
884 code = buf_Get(scp, &offset, reqp, &bp);
888 if (bp->cmFlags & CM_BUF_CMFETCHING) {
889 /* skip this buffer as another thread is already fetching it */
891 lock_ObtainWrite(&scp->rw);
900 lock_ObtainWrite(&scp->rw);
904 code = cm_GetBuffer(scp, bp, NULL, userp, reqp);
906 fetched = LargeIntegerAdd(fetched, tblocksize);
911 lock_ObtainWrite(&scp->rw);
915 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
916 scp, &base, &fetched);
918 /* wakeup anyone who is waiting */
919 if (!osi_QIsEmpty(&scp->waitQueueH)) {
920 osi_Log1(afsd_logp, "CM BkgPrefetch Waking scp 0x%p", scp);
921 osi_Wakeup((LONG_PTR) &scp->flags);
923 lock_ReleaseWrite(&scp->rw);
925 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p code 0x%x fetched 0x%x:%x",
926 scp, code, fetched.HighPart, fetched.LowPart);
928 reqp->flags = req_flags;
932 /* a read was issued to offsetp, and we have to determine whether we should
933 * do a prefetch of the next chunk.
935 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
936 cm_user_t *userp, cm_req_t *reqp)
940 osi_hyper_t realBase;
941 osi_hyper_t readBase;
942 osi_hyper_t readLength;
944 osi_hyper_t tblocksize; /* a long long temp variable */
946 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
949 /* round up to chunk boundary */
950 readBase.LowPart += (cm_chunkSize-1);
951 readBase.LowPart &= (-cm_chunkSize);
953 readLength = ConvertLongToLargeInteger(count);
955 lock_ObtainWrite(&scp->rw);
957 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
958 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
959 lock_ReleaseWrite(&scp->rw);
962 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_PREFETCHING);
964 /* start the scan at the latter of the end of this read or
965 * the end of the last fetched region.
967 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
968 readBase = scp->prefetch.end;
970 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
973 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
974 lock_ReleaseWrite(&scp->rw);
975 return; /* can't find something to prefetch */
978 readEnd = LargeIntegerAdd(realBase, readLength);
981 lock_ReleaseWrite(&scp->rw);
983 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
984 scp, realBase.LowPart);
986 cm_QueueBKGRequest(scp, cm_BkgPrefetch,
987 realBase.LowPart, realBase.HighPart,
988 readLength.LowPart, readLength.HighPart,
992 /* scp must be locked; temporarily unlocked during processing.
993 * If returns 0, returns buffers held in biop, and with
994 * CM_BUF_CMSTORING set.
996 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
997 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
998 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
999 * must be woken, and the event must be set when the I/O is done. All of this
1000 * is required so that buf_WaitIO synchronizes properly with the buffer as it
1001 * is being written out.
1003 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
1004 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1007 osi_queueData_t *qdp;
1010 osi_hyper_t scanStart; /* where to start scan for dirty pages */
1011 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
1012 osi_hyper_t firstModOffset; /* offset of first modified page in range */
1015 long flags; /* flags to cm_SyncOp */
1017 /* clear things out */
1018 biop->scp = scp; /* do not hold; held by caller */
1019 biop->userp = userp; /* do not hold; held by caller */
1021 biop->offset = *inOffsetp;
1023 biop->bufListp = NULL;
1024 biop->bufListEndp = NULL;
1027 /* reserve a chunk's worth of buffers */
1028 lock_ReleaseWrite(&scp->rw);
1029 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1030 lock_ObtainWrite(&scp->rw);
1033 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
1034 thyper = ConvertLongToLargeInteger(temp);
1035 tbase = LargeIntegerAdd(*inOffsetp, thyper);
1037 bufp = buf_Find(&scp->fid, &tbase);
1039 /* get buffer mutex and scp mutex safely */
1040 lock_ReleaseWrite(&scp->rw);
1041 lock_ObtainMutex(&bufp->mx);
1044 * if the buffer is actively involved in I/O
1045 * we wait for the I/O to complete.
1047 if (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING))
1048 buf_WaitIO(scp, bufp);
1050 lock_ObtainWrite(&scp->rw);
1051 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
1052 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1054 lock_ReleaseMutex(&bufp->mx);
1057 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1061 /* if the buffer is dirty, we're done */
1062 if (bufp->flags & CM_BUF_DIRTY) {
1063 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
1064 "WRITING w/o CMSTORING in SetupStoreBIOD");
1065 _InterlockedOr(&bufp->flags, CM_BUF_WRITING);
1069 /* this buffer is clean, so there's no reason to process it */
1070 cm_SyncOpDone(scp, bufp, flags);
1071 lock_ReleaseMutex(&bufp->mx);
1079 /* if we get here, if bufp is null, we didn't find any dirty buffers
1080 * that weren't already being stored back, so we just quit now.
1086 /* don't need buffer mutex any more */
1087 lock_ReleaseMutex(&bufp->mx);
1089 /* put this element in the list */
1090 qdp = osi_QDAlloc();
1091 osi_SetQData(qdp, bufp);
1093 if ( cm_verifyData )
1094 buf_ComputeCheckSum(bufp);
1096 /* don't have to hold bufp, since held by buf_Find above */
1097 osi_QAddH((osi_queue_t **) &biop->bufListp,
1098 (osi_queue_t **) &biop->bufListEndp,
1100 biop->length = cm_data.buf_blockSize;
1101 firstModOffset = bufp->offset;
1102 biop->offset = firstModOffset;
1103 bufp = NULL; /* this buffer and reference added to the queue */
1105 /* compute the window surrounding firstModOffset of size cm_chunkSize */
1106 scanStart = firstModOffset;
1107 scanStart.LowPart &= (-cm_chunkSize);
1108 thyper = ConvertLongToLargeInteger(cm_chunkSize);
1109 scanEnd = LargeIntegerAdd(scanStart, thyper);
1111 flags = CM_SCACHESYNC_GETSTATUS
1112 | CM_SCACHESYNC_STOREDATA
1113 | CM_SCACHESYNC_BUFLOCKED;
1115 /* start by looking backwards until scanStart */
1116 /* hyper version of cm_data.buf_blockSize */
1117 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1118 tbase = LargeIntegerSubtract(firstModOffset, thyper);
1119 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
1120 /* see if we can find the buffer */
1121 bufp = buf_Find(&scp->fid, &tbase);
1125 /* try to lock it, and quit if we can't (simplifies locking) */
1126 lock_ReleaseWrite(&scp->rw);
1127 code = lock_TryMutex(&bufp->mx);
1128 lock_ObtainWrite(&scp->rw);
1135 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1137 lock_ReleaseMutex(&bufp->mx);
1143 if (!(bufp->flags & CM_BUF_DIRTY)) {
1144 /* buffer is clean, so we shouldn't add it */
1145 cm_SyncOpDone(scp, bufp, flags);
1146 lock_ReleaseMutex(&bufp->mx);
1152 /* don't need buffer mutex any more */
1153 lock_ReleaseMutex(&bufp->mx);
1155 /* we have a dirty buffer ready for storing. Add it to the tail
1156 * of the list, since it immediately precedes all of the disk
1157 * addresses we've already collected.
1159 qdp = osi_QDAlloc();
1160 osi_SetQData(qdp, bufp);
1162 if ( cm_verifyData )
1163 buf_ComputeCheckSum(bufp);
1165 /* no buf_hold necessary, since we have it held from buf_Find */
1166 osi_QAddT((osi_queue_t **) &biop->bufListp,
1167 (osi_queue_t **) &biop->bufListEndp,
1169 bufp = NULL; /* added to the queue */
1171 /* update biod info describing the transfer */
1172 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
1173 biop->length += cm_data.buf_blockSize;
1175 /* update loop pointer */
1176 tbase = LargeIntegerSubtract(tbase, thyper);
1177 } /* while loop looking for pages preceding the one we found */
1179 /* now, find later dirty, contiguous pages, and add them to the list */
1180 /* hyper version of cm_data.buf_blockSize */
1181 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1182 tbase = LargeIntegerAdd(firstModOffset, thyper);
1183 while(LargeIntegerLessThan(tbase, scanEnd)) {
1184 /* see if we can find the buffer */
1185 bufp = buf_Find(&scp->fid, &tbase);
1189 /* try to lock it, and quit if we can't (simplifies locking) */
1190 lock_ReleaseWrite(&scp->rw);
1191 code = lock_TryMutex(&bufp->mx);
1192 lock_ObtainWrite(&scp->rw);
1199 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1201 lock_ReleaseMutex(&bufp->mx);
1207 if (!(bufp->flags & CM_BUF_DIRTY)) {
1208 /* buffer is clean, so we shouldn't add it */
1209 cm_SyncOpDone(scp, bufp, flags);
1210 lock_ReleaseMutex(&bufp->mx);
1216 /* don't need buffer mutex any more */
1217 lock_ReleaseMutex(&bufp->mx);
1219 /* we have a dirty buffer ready for storing. Add it to the head
1220 * of the list, since it immediately follows all of the disk
1221 * addresses we've already collected.
1223 qdp = osi_QDAlloc();
1224 osi_SetQData(qdp, bufp);
1226 if ( cm_verifyData )
1227 buf_ComputeCheckSum(bufp);
1229 /* no buf_hold necessary, since we have it held from buf_Find */
1230 osi_QAddH((osi_queue_t **) &biop->bufListp,
1231 (osi_queue_t **) &biop->bufListEndp,
1235 /* update biod info describing the transfer */
1236 biop->length += cm_data.buf_blockSize;
1238 /* update loop pointer */
1239 tbase = LargeIntegerAdd(tbase, thyper);
1240 } /* while loop looking for pages following the first page we found */
1242 /* finally, we're done */
1246 /* scp must be locked; temporarily unlocked during processing.
1247 * If returns 0, returns buffers held in biop, and with
1248 * CM_BUF_CMFETCHING flags set.
1249 * If an error is returned, we don't return any buffers.
1251 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1252 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1256 osi_hyper_t tblocksize; /* a long long temp variable */
1257 osi_hyper_t pageBase; /* base offset we're looking at */
1258 osi_queueData_t *qdp; /* one temp queue structure */
1259 osi_queueData_t *tqdp; /* another temp queue structure */
1260 long collected; /* how many bytes have been collected */
1263 osi_hyper_t fileSize; /* the # of bytes in the file */
1264 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1265 osi_queueData_t *heldBufListEndp; /* first one */
1268 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1270 biop->scp = scp; /* do not hold; held by caller */
1271 biop->userp = userp; /* do not hold; held by caller */
1273 biop->offset = *offsetp;
1274 /* null out the list of buffers */
1275 biop->bufListp = biop->bufListEndp = NULL;
1278 /* first lookup the file's length, so we know when to stop */
1279 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1280 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1284 /* copy out size, since it may change */
1285 fileSize = scp->serverLength;
1287 lock_ReleaseWrite(&scp->rw);
1289 pageBase = *offsetp;
1290 collected = pageBase.LowPart & (cm_chunkSize - 1);
1291 heldBufListp = NULL;
1292 heldBufListEndp = NULL;
1295 * Obtaining buffers can cause dirty buffers to be recycled, which
1296 * can cause a storeback, so cannot be done while we have buffers
1299 * To get around this, we get buffers twice. Before reserving buffers,
1300 * we obtain and release each one individually. After reserving
1301 * buffers, we try to obtain them again, but only by lookup, not by
1302 * recycling. If a buffer has gone away while we were waiting for
1303 * the others, we just use whatever buffers we already have.
1305 * On entry to this function, we are already holding a buffer, so we
1306 * can't wait for reservation. So we call buf_TryReserveBuffers()
1307 * instead. Not only that, we can't really even call buf_Get(), for
1308 * the same reason. We can't avoid that, though. To avoid deadlock
1309 * we allow only one thread to be executing the buf_Get()-buf_Release()
1310 * sequence at a time.
1313 /* first hold all buffers, since we can't hold any locks in buf_Get */
1315 /* stop at chunk boundary */
1316 if (collected >= cm_chunkSize)
1319 /* see if the next page would be past EOF */
1320 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1323 code = buf_Get(scp, &pageBase, reqp, &tbp);
1325 lock_ObtainWrite(&scp->rw);
1326 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1333 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1334 collected += cm_data.buf_blockSize;
1337 /* reserve a chunk's worth of buffers if possible */
1338 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1340 pageBase = *offsetp;
1341 collected = pageBase.LowPart & (cm_chunkSize - 1);
1343 /* now hold all buffers, if they are still there */
1345 /* stop at chunk boundary */
1346 if (collected >= cm_chunkSize)
1349 /* see if the next page would be past EOF */
1350 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1353 tbp = buf_Find(&scp->fid, &pageBase);
1357 /* add the buffer to the list */
1358 qdp = osi_QDAlloc();
1359 osi_SetQData(qdp, tbp);
1360 osi_QAddH((osi_queue_t **)&heldBufListp,
1361 (osi_queue_t **)&heldBufListEndp,
1363 /* leave tbp held (from buf_Get) */
1368 collected += cm_data.buf_blockSize;
1369 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1372 /* look at each buffer, adding it into the list if it looks idle and
1373 * filled with old data. One special case: wait for idle if it is the
1374 * first buffer since we really need that one for our caller to make
1378 collected = 0; /* now count how many we'll really use */
1379 for (tqdp = heldBufListEndp;
1381 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1382 /* get a ptr to the held buffer */
1383 tbp = osi_GetQData(tqdp);
1384 pageBase = tbp->offset;
1386 /* now lock the buffer lock */
1387 lock_ObtainMutex(&tbp->mx);
1388 lock_ObtainWrite(&scp->rw);
1390 /* don't bother fetching over data that is already current */
1391 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1392 /* we don't need this buffer, since it is current */
1393 lock_ReleaseWrite(&scp->rw);
1394 lock_ReleaseMutex(&tbp->mx);
1398 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1400 flags |= CM_SCACHESYNC_NOWAIT;
1402 /* wait for the buffer to serialize, if required. Doesn't
1403 * release the scp or buffer lock(s) if NOWAIT is specified.
1405 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1407 lock_ReleaseWrite(&scp->rw);
1408 lock_ReleaseMutex(&tbp->mx);
1412 /* don't fetch over dirty buffers */
1413 if (tbp->flags & CM_BUF_DIRTY) {
1414 cm_SyncOpDone(scp, tbp, flags);
1415 lock_ReleaseWrite(&scp->rw);
1416 lock_ReleaseMutex(&tbp->mx);
1421 lock_ReleaseWrite(&scp->rw);
1422 lock_ReleaseMutex(&tbp->mx);
1424 /* add the buffer to the list */
1425 qdp = osi_QDAlloc();
1426 osi_SetQData(qdp, tbp);
1427 osi_QAddH((osi_queue_t **)&biop->bufListp,
1428 (osi_queue_t **)&biop->bufListEndp,
1432 /* from now on, a failure just stops our collection process, but
1433 * we still do the I/O to whatever we've already managed to collect.
1436 collected += cm_data.buf_blockSize;
1439 /* now, we've held in biop->bufListp all the buffer's we're really
1440 * interested in. We also have holds left from heldBufListp, and we
1441 * now release those holds on the buffers.
1443 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1444 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1445 tbp = osi_GetQData(qdp);
1446 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1447 (osi_queue_t **) &heldBufListEndp,
1454 /* Caller expects this */
1455 lock_ObtainWrite(&scp->rw);
1457 /* if we got a failure setting up the first buffer, then we don't have
1458 * any side effects yet, and we also have failed an operation that the
1459 * caller requires to make any progress. Give up now.
1461 if (code && isFirst) {
1462 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1466 /* otherwise, we're still OK, and should just return the I/O setup we've
1469 biop->length = collected;
1470 biop->reserved = reserving;
1474 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1477 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
1479 cm_scache_t *scp; /* do not release; not held in biop */
1481 osi_queueData_t *qdp;
1482 osi_queueData_t *nqdp;
1484 int reportErrorToRedir = 0;
1486 /* Give back reserved buffers */
1488 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1491 flags = CM_SCACHESYNC_STOREDATA;
1493 flags = CM_SCACHESYNC_FETCHDATA;
1496 if (biop->bufListp) {
1497 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1498 /* lookup next guy first, since we're going to free this one */
1499 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1501 /* extract buffer and free queue data */
1502 bufp = osi_GetQData(qdp);
1503 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1504 (osi_queue_t **) &biop->bufListEndp,
1508 /* now, mark I/O as done, unlock the buffer and release it */
1510 lock_ReleaseWrite(&scp->rw);
1511 lock_ObtainMutex(&bufp->mx);
1512 lock_ObtainWrite(&scp->rw);
1513 cm_SyncOpDone(scp, bufp, flags);
1515 /* turn off writing and wakeup users */
1517 if (bufp->flags & CM_BUF_WAITING) {
1518 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1519 osi_Wakeup((LONG_PTR) bufp);
1522 _InterlockedAnd(&bufp->flags, ~CM_BUF_WRITING);
1524 case CM_ERROR_NOSUCHFILE:
1525 case CM_ERROR_BADFD:
1526 case CM_ERROR_NOACCESS:
1527 case CM_ERROR_QUOTA:
1528 case CM_ERROR_SPACE:
1529 case CM_ERROR_TOOBIG:
1530 case CM_ERROR_READONLY:
1531 case CM_ERROR_NOSUCHPATH:
1533 case CM_ERROR_INVAL_NET_RESP:
1534 case CM_ERROR_UNKNOWN:
1536 * Apply the fatal error to this buffer.
1538 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
1539 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
1540 bufp->dirty_length = 0;
1542 bufp->dataVersion = CM_BUF_VERSION_BAD;
1543 bufp->dirtyCounter++;
1544 reportErrorToRedir = 1;
1546 case CM_ERROR_TIMEDOUT:
1547 case CM_ERROR_ALLDOWN:
1548 case CM_ERROR_ALLBUSY:
1549 case CM_ERROR_ALLOFFLINE:
1550 case CM_ERROR_CLOCKSKEW:
1552 /* do not mark the buffer in error state but do
1553 * not attempt to complete the rest either.
1558 _InterlockedAnd(&bufp->flags, ~(CM_BUF_WRITING | CM_BUF_DIRTY));
1559 bufp->dirty_offset = bufp->dirty_length = 0;
1564 lock_ReleaseWrite(&scp->rw);
1565 lock_ReleaseMutex(&bufp->mx);
1570 if (RDR_Initialized && reportErrorToRedir) {
1572 smb_MapNTError(cm_MapRPCError(code, biop->reqp), &status, TRUE);
1573 RDR_SetFileStatus( &scp->fid, &biop->userp->authgroup, status);
1577 lock_ObtainWrite(&scp->rw);
1578 cm_SyncOpDone(scp, NULL, flags);
1580 lock_ReleaseWrite(&scp->rw);
1583 /* clean things out */
1584 biop->bufListp = NULL;
1585 biop->bufListEndp = NULL;
1589 cm_CloneStatus(cm_scache_t *scp, cm_user_t *userp, int scp_locked,
1590 AFSFetchStatus *afsStatusp, AFSVolSync *volSyncp)
1592 // setup the status based upon the scp data
1593 afsStatusp->InterfaceVersion = 0x1;
1594 switch (scp->fileType) {
1595 case CM_SCACHETYPE_FILE:
1596 afsStatusp->FileType = File;
1598 case CM_SCACHETYPE_DIRECTORY:
1599 afsStatusp->FileType = Directory;
1601 case CM_SCACHETYPE_MOUNTPOINT:
1602 afsStatusp->FileType = SymbolicLink;
1604 case CM_SCACHETYPE_SYMLINK:
1605 case CM_SCACHETYPE_DFSLINK:
1606 afsStatusp->FileType = SymbolicLink;
1609 afsStatusp->FileType = -1; /* an invalid value */
1611 afsStatusp->LinkCount = scp->linkCount;
1612 afsStatusp->Length = scp->length.LowPart;
1613 afsStatusp->DataVersion = (afs_uint32)(scp->dataVersion & MAX_AFS_UINT32);
1614 afsStatusp->Author = 0x1;
1615 afsStatusp->Owner = scp->owner;
1617 lock_ObtainWrite(&scp->rw);
1620 if (cm_FindACLCache(scp, userp, &afsStatusp->CallerAccess))
1621 afsStatusp->CallerAccess = scp->anyAccess;
1622 afsStatusp->AnonymousAccess = scp->anyAccess;
1623 afsStatusp->UnixModeBits = scp->unixModeBits;
1624 afsStatusp->ParentVnode = scp->parentVnode;
1625 afsStatusp->ParentUnique = scp->parentUnique;
1626 afsStatusp->ResidencyMask = 0;
1627 afsStatusp->ClientModTime = scp->clientModTime;
1628 afsStatusp->ServerModTime = scp->serverModTime;
1629 afsStatusp->Group = scp->group;
1630 afsStatusp->SyncCounter = 0;
1631 afsStatusp->dataVersionHigh = (afs_uint32)(scp->dataVersion >> 32);
1632 afsStatusp->lockCount = 0;
1633 afsStatusp->Length_hi = scp->length.HighPart;
1634 afsStatusp->errorCode = 0;
1636 volSyncp->spare1 = scp->volumeCreationDate;
1641 /* Fetch a buffer. Called with scp locked.
1642 * The scp is locked on return.
1644 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1647 long code=0, code1=0;
1648 afs_uint32 nbytes; /* bytes in transfer */
1649 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1650 afs_uint64 length_found = 0;
1651 long rbytes; /* bytes in rx_Read call */
1653 AFSFetchStatus afsStatus;
1654 AFSCallBack callback;
1657 afs_uint32 buffer_offset;
1658 cm_buf_t *tbufp; /* buf we're filling */
1659 osi_queueData_t *qdp; /* q element we're scanning */
1661 struct rx_call *rxcallp;
1662 struct rx_connection *rxconnp;
1663 cm_bulkIO_t biod; /* bulk IO descriptor */
1667 int require_64bit_ops = 0;
1668 int call_was_64bit = 0;
1669 int fs_fetchdata_offset_bug = 0;
1673 memset(&afsStatus, 0, sizeof(afsStatus));
1674 memset(&callback, 0, sizeof(callback));
1675 memset(&volSync, 0, sizeof(volSync));
1677 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
1678 * drops lots of locks, and may indeed return a properly initialized
1679 * buffer, although more likely it will just return a new, empty, buffer.
1682 #ifdef AFS_FREELANCE_CLIENT
1684 // yj: if they're trying to get the /afs directory, we need to
1685 // handle it differently, since it's local rather than on any
1688 getroot = (scp==cm_data.rootSCachep);
1690 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1693 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1694 /* We already have this buffer don't do extra work */
1698 cm_AFSFidFromFid(&tfid, &scp->fid);
1700 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1702 /* couldn't even get the first page setup properly */
1703 osi_Log1(afsd_logp, "cm_GetBuffer: SetupFetchBIOD failure code %d", code);
1707 /* once we get here, we have the callback in place, we know that no one
1708 * is fetching the data now. Check one last time that we still have
1709 * the wrong data, and then fetch it if we're still wrong.
1711 * We can lose a race condition and end up with biod.length zero, in
1712 * which case we just retry.
1715 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1716 /* We obtained the buffer while we were waiting */
1720 if (biod.length == 0) {
1721 osi_Log2(afsd_logp, "cm_GetBuffer BIOD length 0 scp 0x%p bufp 0x%p",
1726 /* Check for buffers we can fill locally */
1727 for (qdp = biod.bufListEndp;
1729 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1730 tbufp = osi_GetQData(qdp);
1732 if ( tbufp->dataVersion == CM_BUF_VERSION_BAD ||
1733 tbufp->dataVersion < scp->bufDataVersionLow ||
1734 tbufp->dataVersion > scp->dataVersion) {
1736 osi_Log4(afsd_logp, "cm_GetBuffer bufp 0x%p DVs 0x%x != (0x%x -> 0x%x)",
1737 tbufp, tbufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion);
1739 if (LargeIntegerGreaterThanOrEqualTo(tbufp->offset, scp->length))
1741 if (tbufp->dataVersion == CM_BUF_VERSION_BAD)
1742 memset(tbufp->datap, 0, cm_data.buf_blockSize);
1743 tbufp->dataVersion = scp->dataVersion;
1746 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1747 LargeIntegerGreaterThanOrEqualTo(tbufp->offset, scp->truncPos)) {
1748 memset(tbufp->datap, 0, cm_data.buf_blockSize);
1749 tbufp->dataVersion = scp->dataVersion;
1754 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1755 /* We locally populated the buffer we wanted */
1759 InterlockedIncrement(&scp->activeRPCs);
1760 lock_ReleaseWrite(&scp->rw);
1763 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1764 ConvertLongToLargeInteger(biod.length)),
1765 ConvertLongToLargeInteger(LONG_MAX))) {
1766 require_64bit_ops = 1;
1769 osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1770 osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1771 scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1773 #ifdef AFS_FREELANCE_CLIENT
1776 // if getroot then we don't need to make any calls
1777 // just return fake data
1779 if (cm_freelanceEnabled && getroot) {
1780 // setup the fake status
1781 afsStatus.InterfaceVersion = 0x1;
1782 afsStatus.FileType = 0x2;
1783 afsStatus.LinkCount = scp->linkCount;
1784 afsStatus.Length = cm_fakeDirSize;
1785 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1786 afsStatus.Author = 0x1;
1787 afsStatus.Owner = 0x0;
1788 afsStatus.CallerAccess = 0x9;
1789 afsStatus.AnonymousAccess = 0x9;
1790 afsStatus.UnixModeBits = 0x1ff;
1791 afsStatus.ParentVnode = 0x1;
1792 afsStatus.ParentUnique = 0x1;
1793 afsStatus.ResidencyMask = 0;
1794 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1795 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1796 afsStatus.Group = 0;
1797 afsStatus.SyncCounter = 0;
1798 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1799 afsStatus.lockCount = 0;
1800 afsStatus.Length_hi = 0;
1801 afsStatus.errorCode = 0;
1802 memset(&volSync, 0, sizeof(volSync));
1804 // once we're done setting up the status info,
1805 // we just fill the buffer pages with fakedata
1806 // from cm_FakeRootDir. Extra pages are set to
1809 lock_ObtainMutex(&cm_Freelance_Lock);
1810 t1 = bufp->offset.LowPart;
1811 qdp = biod.bufListEndp;
1813 tbufp = osi_GetQData(qdp);
1814 bufferp=tbufp->datap;
1815 memset(bufferp, 0, cm_data.buf_blockSize);
1816 t2 = cm_fakeDirSize - t1;
1817 if (t2> (afs_int32)cm_data.buf_blockSize)
1818 t2=cm_data.buf_blockSize;
1820 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1825 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1828 lock_ReleaseMutex(&cm_Freelance_Lock);
1830 // once we're done, we skip over the part of the
1831 // code that does the ACTUAL fetching of data for
1834 goto fetchingcompleted;
1837 #endif /* AFS_FREELANCE_CLIENT */
1840 * if the requested offset is greater than the file length,
1841 * the file server will return zero bytes of data and the
1842 * current status for the file which we already have since
1843 * we have just obtained a callback. Instead, we can avoid
1844 * the network round trip by allocating zeroed buffers and
1845 * faking the status info.
1847 if (biod.offset.QuadPart >= scp->serverLength.QuadPart ||
1848 ((scp->mask & CM_SCACHEMASK_TRUNCPOS) && biod.offset.QuadPart > scp->truncPos.QuadPart)) {
1849 osi_Log5(afsd_logp, "SKIP FetchData64 scp 0x%p, off 0x%x:%08x > length 0x%x:%08x",
1850 scp, biod.offset.HighPart, biod.offset.LowPart,
1851 scp->length.HighPart, scp->length.LowPart);
1853 /* Clone the current status info */
1854 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
1856 /* status info complete, fill pages with zeros */
1857 for (qdp = biod.bufListEndp;
1859 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1860 tbufp = osi_GetQData(qdp);
1861 bufferp=tbufp->datap;
1862 memset(bufferp, 0, cm_data.buf_blockSize);
1865 /* no need to contact the file server */
1866 goto fetchingcompleted;
1870 lock_ReleaseWrite(&scp->rw);
1874 /* now make the call */
1876 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1880 rxconnp = cm_GetRxConn(connp);
1881 rxcallp = rx_NewCall(rxconnp);
1882 rx_PutConnection(rxconnp);
1884 nbytes = nbytes_hi = 0;
1886 if (SERVERHAS64BIT(connp)) {
1889 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1890 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1892 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1895 temp = rx_Read32(rxcallp, &nbytes_hi);
1896 if (temp == sizeof(afs_int32)) {
1897 nbytes_hi = ntohl(nbytes_hi);
1900 code = rx_Error(rxcallp);
1901 code1 = rx_EndCall(rxcallp, code);
1909 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1910 if (require_64bit_ops) {
1911 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1912 code = CM_ERROR_TOOBIG;
1915 rxconnp = cm_GetRxConn(connp);
1916 rxcallp = rx_NewCall(rxconnp);
1917 rx_PutConnection(rxconnp);
1920 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1921 scp, biod.offset.LowPart, biod.length);
1923 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1926 SET_SERVERHASNO64BIT(connp);
1931 temp = rx_Read32(rxcallp, &nbytes);
1932 if (temp == sizeof(afs_int32)) {
1933 nbytes = ntohl(nbytes);
1934 FillInt64(length_found, nbytes_hi, nbytes);
1935 if (length_found > biod.length) {
1937 * prior to 1.4.12 and 1.5.65 the file server would return
1938 * (filesize - offset) if the requested offset was greater than
1939 * the filesize. The correct return value would have been zero.
1940 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
1941 * is a race between two RPCs issues by this cache manager, the
1942 * correct thing will happen the second time.
1944 osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1945 fs_fetchdata_offset_bug = 1;
1948 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1949 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
1952 /* for the moment, nbytes_hi will always be 0 if code == 0
1953 because biod.length is a 32-bit quantity. */
1956 qdp = biod.bufListEndp;
1958 tbufp = osi_GetQData(qdp);
1959 bufferp = tbufp->datap;
1965 /* fill length_found of data from the pipe into the pages.
1966 * When we stop, qdp will point at the last page we're
1967 * dealing with, and bufferp will tell us where we
1968 * stopped. We'll need this info below when we clear
1969 * the remainder of the last page out (and potentially
1970 * clear later pages out, if we fetch past EOF).
1972 while (length_found > 0) {
1974 struct iovec tiov[RX_MAXIOVECS];
1975 afs_int32 tnio, iov, iov_offset;
1977 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
1978 osi_Log1(afsd_logp, "cm_GetBuffer rx_Readv returns %d", temp);
1979 if (temp != length_found && temp < cm_data.buf_blockSize) {
1981 * If the file server returned (filesize - offset),
1982 * then the first rx_Read will return zero octets of data.
1983 * If it does, do not treat it as an error. Correct the
1984 * length_found and continue as if the file server said
1985 * it was sending us zero octets of data.
1987 if (fs_fetchdata_offset_bug && first_read) {
1990 } else if (temp <= 0) {
1991 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2000 while (rbytes > 0) {
2003 osi_assertx(bufferp != NULL, "null cm_buf_t");
2005 len = min(tiov[iov].iov_len - iov_offset, cm_data.buf_blockSize - buffer_offset);
2006 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
2008 buffer_offset += len;
2011 if (iov_offset == tiov[iov].iov_len) {
2016 if (buffer_offset == cm_data.buf_blockSize) {
2017 /* allow read-while-fetching.
2018 * if this is the last buffer, clear the
2019 * PREFETCHING flag, so the reader waiting for
2020 * this buffer will start a prefetch.
2022 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
2023 lock_ObtainWrite(&scp->rw);
2024 if (!osi_QIsEmpty(&scp->waitQueueH)) {
2025 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
2026 osi_Wakeup((LONG_PTR) &scp->flags);
2028 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
2029 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
2031 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
2033 lock_ReleaseWrite(&scp->rw);
2035 /* Advance the buffer */
2036 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2038 tbufp = osi_GetQData(qdp);
2039 bufferp = tbufp->datap;
2047 length_found -= temp;
2048 #else /* USE_RX_IOVEC */
2049 /* assert that there are still more buffers;
2050 * our check above for length_found being less than
2051 * biod.length should ensure this.
2053 osi_assertx(bufferp != NULL, "null cm_buf_t");
2055 /* read rbytes of data */
2056 rbytes = (afs_uint32)(length_found > cm_data.buf_blockSize ? cm_data.buf_blockSize : length_found);
2057 temp = rx_Read(rxcallp, bufferp, rbytes);
2058 if (temp < rbytes) {
2060 * If the file server returned (filesize - offset),
2061 * then the first rx_Read will return zero octets of data.
2062 * If it does, do not treat it as an error. Correct the
2063 * length_found and continue as if the file server said
2064 * it was sending us zero octets of data.
2066 if (fs_fetchdata_offset_bug && first_read) {
2069 } else if (temp <= 0) {
2070 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2076 /* allow read-while-fetching.
2077 * if this is the last buffer, clear the
2078 * PREFETCHING flag, so the reader waiting for
2079 * this buffer will start a prefetch.
2081 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
2082 lock_ObtainWrite(&scp->rw);
2083 if (!osi_QIsEmpty(&scp->waitQueueH)) {
2084 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
2085 osi_Wakeup((LONG_PTR) &scp->flags);
2087 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
2088 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
2090 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
2092 lock_ReleaseWrite(&scp->rw);
2094 /* and adjust counters */
2095 length_found -= temp;
2097 /* and move to the next buffer */
2098 if (length_found != 0) {
2099 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2101 tbufp = osi_GetQData(qdp);
2102 bufferp = tbufp->datap;
2108 #endif /* USE_RX_IOVEC */
2111 /* zero out remainder of last pages, in case we are
2112 * fetching past EOF. We were fetching an integral #
2113 * of pages, but stopped, potentially in the middle of
2114 * a page. Zero the remainder of that page, and then
2115 * all of the rest of the pages.
2118 rbytes = cm_data.buf_blockSize - buffer_offset;
2119 bufferp = tbufp->datap + buffer_offset;
2120 #else /* USE_RX_IOVEC */
2122 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
2123 rbytes = (long) (bufferp - tbufp->datap);
2125 /* bytes left to zero */
2126 rbytes = cm_data.buf_blockSize - rbytes;
2127 #endif /* USE_RX_IOVEC */
2130 memset(bufferp, 0, rbytes);
2131 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2134 tbufp = osi_GetQData(qdp);
2135 bufferp = tbufp->datap;
2136 /* bytes to clear in this page */
2137 rbytes = cm_data.buf_blockSize;
2143 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2145 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2148 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2150 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2154 code1 = rx_EndCall(rxcallp, code);
2156 if (code1 == RXKADUNKNOWNKEY)
2157 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2159 /* If we are avoiding a file server bug, ignore the error state */
2160 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2161 /* Clone the current status info and clear the error state */
2162 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2164 lock_ReleaseWrite(&scp->rw);
2168 /* Prefer the error value from FetchData over rx_EndCall */
2169 } else if (code == 0 && code1 != 0)
2171 osi_Log0(afsd_logp, "CALL FetchData DONE");
2173 } while (cm_Analyze(connp, userp, reqp, &scp->fid, NULL, 0, &afsStatus, &volSync, NULL, NULL, code));
2176 code = cm_MapRPCError(code, reqp);
2179 lock_ObtainWrite(&scp->rw);
2181 /* we know that no one else has changed the buffer, since we still have
2182 * the fetching flag on the buffers, and we have the scp locked again.
2183 * Copy in the version # into the buffer if we got code 0 back from the
2187 for(qdp = biod.bufListp;
2189 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
2190 tbufp = osi_GetQData(qdp);
2191 tbufp->dataVersion = afsStatus.dataVersionHigh;
2192 tbufp->dataVersion <<= 32;
2193 tbufp->dataVersion |= afsStatus.DataVersion;
2196 /* write buffer out to disk cache */
2197 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
2198 tbufp->dataVersion);
2199 #endif /* DISKCACHE95 */
2204 code = cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2206 InterlockedDecrement(&scp->activeRPCs);
2209 /* release scatter/gather I/O structure (buffers, locks) */
2210 cm_ReleaseBIOD(&biod, 0, code, 1);
2216 * Similar to cm_GetBuffer but doesn't use an allocated cm_buf_t object.
2217 * Instead the data is read from the file server and copied directly into
2218 * a provided buffer. Called with scp locked. The scp is locked on return.
2220 long cm_GetData(cm_scache_t *scp, osi_hyper_t *offsetp, char *datap, int data_length,
2221 cm_user_t *userp, cm_req_t *reqp)
2223 long code=0, code1=0;
2224 afs_uint32 nbytes; /* bytes in transfer */
2225 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2226 afs_uint64 length_found = 0;
2227 char *bufferp = datap;
2228 afs_uint32 buffer_offset = 0;
2229 long rbytes; /* bytes in rx_Read call */
2231 AFSFetchStatus afsStatus;
2232 AFSCallBack callback;
2235 struct rx_call *rxcallp;
2236 struct rx_connection *rxconnp;
2240 int require_64bit_ops = 0;
2241 int call_was_64bit = 0;
2242 int fs_fetchdata_offset_bug = 0;
2246 memset(&afsStatus, 0, sizeof(afsStatus));
2247 memset(&callback, 0, sizeof(callback));
2248 memset(&volSync, 0, sizeof(volSync));
2250 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
2251 * drops lots of locks, and may indeed return a properly initialized
2252 * buffer, although more likely it will just return a new, empty, buffer.
2255 #ifdef AFS_FREELANCE_CLIENT
2257 // yj: if they're trying to get the /afs directory, we need to
2258 // handle it differently, since it's local rather than on any
2261 getroot = (scp==cm_data.rootSCachep);
2263 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
2266 cm_AFSFidFromFid(&tfid, &scp->fid);
2268 if (LargeIntegerGreaterThan(LargeIntegerAdd(*offsetp,
2269 ConvertLongToLargeInteger(data_length)),
2270 ConvertLongToLargeInteger(LONG_MAX))) {
2271 require_64bit_ops = 1;
2274 InterlockedIncrement(&scp->activeRPCs);
2275 osi_Log2(afsd_logp, "cm_GetData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2277 #ifdef AFS_FREELANCE_CLIENT
2280 // if getroot then we don't need to make any calls
2281 // just return fake data
2283 if (cm_freelanceEnabled && getroot) {
2284 // setup the fake status
2285 afsStatus.InterfaceVersion = 0x1;
2286 afsStatus.FileType = 0x2;
2287 afsStatus.LinkCount = scp->linkCount;
2288 afsStatus.Length = cm_fakeDirSize;
2289 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
2290 afsStatus.Author = 0x1;
2291 afsStatus.Owner = 0x0;
2292 afsStatus.CallerAccess = 0x9;
2293 afsStatus.AnonymousAccess = 0x9;
2294 afsStatus.UnixModeBits = 0x1ff;
2295 afsStatus.ParentVnode = 0x1;
2296 afsStatus.ParentUnique = 0x1;
2297 afsStatus.ResidencyMask = 0;
2298 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
2299 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
2300 afsStatus.Group = 0;
2301 afsStatus.SyncCounter = 0;
2302 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
2303 afsStatus.lockCount = 0;
2304 afsStatus.Length_hi = 0;
2305 afsStatus.errorCode = 0;
2306 memset(&volSync, 0, sizeof(volSync));
2308 // once we're done setting up the status info,
2309 // we just fill the buffer pages with fakedata
2310 // from cm_FakeRootDir. Extra pages are set to
2313 lock_ObtainMutex(&cm_Freelance_Lock);
2314 t1 = offsetp->LowPart;
2315 memset(datap, 0, data_length);
2316 t2 = cm_fakeDirSize - t1;
2317 if (t2 > data_length)
2320 memcpy(datap, cm_FakeRootDir+t1, t2);
2321 lock_ReleaseMutex(&cm_Freelance_Lock);
2323 // once we're done, we skip over the part of the
2324 // code that does the ACTUAL fetching of data for
2327 goto fetchingcompleted;
2330 #endif /* AFS_FREELANCE_CLIENT */
2333 lock_ReleaseWrite(&scp->rw);
2337 /* now make the call */
2339 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2343 rxconnp = cm_GetRxConn(connp);
2344 rxcallp = rx_NewCall(rxconnp);
2345 rx_PutConnection(rxconnp);
2347 nbytes = nbytes_hi = 0;
2349 if (SERVERHAS64BIT(connp)) {
2352 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2353 scp, offsetp->HighPart, offsetp->LowPart, data_length);
2355 code = StartRXAFS_FetchData64(rxcallp, &tfid, offsetp->QuadPart, data_length);
2358 temp = rx_Read32(rxcallp, &nbytes_hi);
2359 if (temp == sizeof(afs_int32)) {
2360 nbytes_hi = ntohl(nbytes_hi);
2363 code = rx_Error(rxcallp);
2364 code1 = rx_EndCall(rxcallp, code);
2372 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2373 if (require_64bit_ops) {
2374 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2375 code = CM_ERROR_TOOBIG;
2378 rxconnp = cm_GetRxConn(connp);
2379 rxcallp = rx_NewCall(rxconnp);
2380 rx_PutConnection(rxconnp);
2383 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2384 scp, offsetp->LowPart, data_length);
2386 code = StartRXAFS_FetchData(rxcallp, &tfid, offsetp->LowPart, data_length);
2388 SET_SERVERHASNO64BIT(connp);
2393 temp = rx_Read32(rxcallp, &nbytes);
2394 if (temp == sizeof(afs_int32)) {
2395 nbytes = ntohl(nbytes);
2396 FillInt64(length_found, nbytes_hi, nbytes);
2397 if (length_found > data_length) {
2399 * prior to 1.4.12 and 1.5.65 the file server would return
2400 * (filesize - offset) if the requested offset was greater than
2401 * the filesize. The correct return value would have been zero.
2402 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2403 * is a race between two RPCs issues by this cache manager, the
2404 * correct thing will happen the second time.
2406 osi_Log0(afsd_logp, "cm_GetData length_found > data_length");
2407 fs_fetchdata_offset_bug = 1;
2410 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2411 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2414 /* for the moment, nbytes_hi will always be 0 if code == 0
2415 because data_length is a 32-bit quantity. */
2418 /* fill length_found of data from the pipe into the pages.
2419 * When we stop, qdp will point at the last page we're
2420 * dealing with, and bufferp will tell us where we
2421 * stopped. We'll need this info below when we clear
2422 * the remainder of the last page out (and potentially
2423 * clear later pages out, if we fetch past EOF).
2425 while (length_found > 0) {
2427 struct iovec tiov[RX_MAXIOVECS];
2428 afs_int32 tnio, iov, iov_offset;
2430 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
2431 osi_Log1(afsd_logp, "cm_GetData rx_Readv returns %d", temp);
2432 if (temp != length_found && temp < data_length) {
2434 * If the file server returned (filesize - offset),
2435 * then the first rx_Read will return zero octets of data.
2436 * If it does, do not treat it as an error. Correct the
2437 * length_found and continue as if the file server said
2438 * it was sending us zero octets of data.
2440 if (fs_fetchdata_offset_bug && first_read) {
2443 } else if (temp <= 0) {
2444 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2453 while (rbytes > 0) {
2456 osi_assertx(bufferp != NULL, "null cm_buf_t");
2458 len = min(tiov[iov].iov_len - iov_offset, data_length - buffer_offset);
2459 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
2461 buffer_offset += len;
2464 if (iov_offset == tiov[iov].iov_len) {
2470 length_found -= temp;
2471 #else /* USE_RX_IOVEC */
2472 /* assert that there are still more buffers;
2473 * our check above for length_found being less than
2474 * data_length should ensure this.
2476 osi_assertx(bufferp != NULL, "null cm_buf_t");
2478 /* read rbytes of data */
2479 rbytes = (afs_uint32)(length_found > data_length ? data_length : length_found);
2480 temp = rx_Read(rxcallp, bufferp, rbytes);
2481 if (temp < rbytes) {
2483 * If the file server returned (filesize - offset),
2484 * then the first rx_Read will return zero octets of data.
2485 * If it does, do not treat it as an error. Correct the
2486 * length_found and continue as if the file server said
2487 * it was sending us zero octets of data.
2489 if (fs_fetchdata_offset_bug && first_read) {
2492 } else if (temp <= 0) {
2493 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2499 /* and adjust counters */
2500 length_found -= temp;
2501 #endif /* USE_RX_IOVEC */
2504 /* zero out remainder of last pages, in case we are
2505 * fetching past EOF. We were fetching an integral #
2506 * of pages, but stopped, potentially in the middle of
2507 * a page. Zero the remainder of that page, and then
2508 * all of the rest of the pages.
2511 rbytes = data_length - buffer_offset;
2512 bufferp = datap + buffer_offset;
2513 #else /* USE_RX_IOVEC */
2515 osi_assertx((bufferp - datap) < LONG_MAX, "data >= LONG_MAX");
2516 rbytes = (long) (bufferp - datap);
2518 /* bytes left to zero */
2519 rbytes = data_length - rbytes;
2520 #endif /* USE_RX_IOVEC */
2522 memset(bufferp, 0, rbytes);
2527 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2529 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2532 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2534 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2538 code1 = rx_EndCall(rxcallp, code);
2540 if (code1 == RXKADUNKNOWNKEY)
2541 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2543 /* If we are avoiding a file server bug, ignore the error state */
2544 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2545 /* Clone the current status info and clear the error state */
2546 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2548 lock_ReleaseWrite(&scp->rw);
2552 /* Prefer the error value from FetchData over rx_EndCall */
2553 } else if (code == 0 && code1 != 0)
2555 osi_Log0(afsd_logp, "CALL FetchData DONE");
2557 } while (cm_Analyze(connp, userp, reqp, &scp->fid, NULL, 0, &afsStatus, &volSync, NULL, NULL, code));
2560 code = cm_MapRPCError(code, reqp);
2563 lock_ObtainWrite(&scp->rw);
2566 code = cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2568 InterlockedDecrement(&scp->activeRPCs);
2574 * cm_VerifyStoreData. Function passed a rw locked cm_scache_t and a store data biod.
2576 * Return 1 if the data verifies; 0 if not.
2580 cm_VerifyStoreData(cm_bulkIO_t *biod, cm_scache_t *savedScp)
2582 long code=0, code1=0;
2583 afs_uint32 nbytes; /* bytes in transfer */
2584 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2585 afs_uint64 length_found = 0;
2586 long rbytes; /* bytes in rx_Read call */
2588 AFSFetchStatus afsStatus;
2589 AFSCallBack callback;
2592 struct rx_call *rxcallp;
2593 struct rx_connection *rxconnp;
2595 int require_64bit_ops = 0;
2596 int call_was_64bit = 0;
2597 int fs_fetchdata_offset_bug = 0;
2600 char * bufferp = malloc(biod->length);
2602 cm_scache_t *scp = biod->scp;
2603 cm_user_t *userp = biod->userp;
2604 cm_req_t *reqp = biod->reqp;
2605 afs_uint64 dataVersion = scp->dataVersion;
2607 memset(&afsStatus, 0, sizeof(afsStatus));
2608 memset(&callback, 0, sizeof(callback));
2609 memset(&volSync, 0, sizeof(volSync));
2610 memset(bufferp, 0, biod->length);
2612 cm_AFSFidFromFid(&tfid, &scp->fid);
2614 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod->offset,
2615 ConvertLongToLargeInteger(biod->length)),
2616 ConvertLongToLargeInteger(LONG_MAX))) {
2617 require_64bit_ops = 1;
2620 InterlockedIncrement(&scp->activeRPCs);
2621 osi_Log2(afsd_logp, "cm_VerifyStoreData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2624 lock_ReleaseWrite(&scp->rw);
2628 /* now make the call */
2630 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2634 rxconnp = cm_GetRxConn(connp);
2635 rxcallp = rx_NewCall(rxconnp);
2636 rx_PutConnection(rxconnp);
2638 nbytes = nbytes_hi = 0;
2640 if (SERVERHAS64BIT(connp)) {
2643 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2644 scp, biod->offset.HighPart, biod->offset.LowPart, biod->length);
2646 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod->offset.QuadPart, biod->length);
2649 temp = rx_Read32(rxcallp, &nbytes_hi);
2650 if (temp == sizeof(afs_int32)) {
2651 nbytes_hi = ntohl(nbytes_hi);
2654 code = rx_Error(rxcallp);
2655 code1 = rx_EndCall(rxcallp, code);
2663 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2664 if (require_64bit_ops) {
2665 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2666 code = CM_ERROR_TOOBIG;
2669 rxconnp = cm_GetRxConn(connp);
2670 rxcallp = rx_NewCall(rxconnp);
2671 rx_PutConnection(rxconnp);
2674 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2675 scp, biod->offset.LowPart, biod->length);
2677 code = StartRXAFS_FetchData(rxcallp, &tfid, biod->offset.LowPart, biod->length);
2679 SET_SERVERHASNO64BIT(connp);
2684 temp = rx_Read32(rxcallp, &nbytes);
2685 if (temp == sizeof(afs_int32)) {
2686 nbytes = ntohl(nbytes);
2687 FillInt64(length_found, nbytes_hi, nbytes);
2688 if (length_found > biod->length) {
2690 * prior to 1.4.12 and 1.5.65 the file server would return
2691 * (filesize - offset) if the requested offset was greater than
2692 * the filesize. The correct return value would have been zero.
2693 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2694 * is a race between two RPCs issues by this cache manager, the
2695 * correct thing will happen the second time.
2697 osi_Log0(afsd_logp, "cm_GetData length_found > biod.length");
2698 fs_fetchdata_offset_bug = 1;
2701 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2702 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2705 /* for the moment, nbytes_hi will always be 0 if code == 0
2706 because data_length is a 32-bit quantity. */
2709 /* fill length_found of data from the pipe into the pages.
2710 * When we stop, qdp will point at the last page we're
2711 * dealing with, and bufferp will tell us where we
2712 * stopped. We'll need this info below when we clear
2713 * the remainder of the last page out (and potentially
2714 * clear later pages out, if we fetch past EOF).
2716 while (length_found > 0) {
2717 /* assert that there are still more buffers;
2718 * our check above for length_found being less than
2719 * data_length should ensure this.
2721 osi_assertx(bufferp != NULL, "null cm_buf_t");
2723 /* read rbytes of data */
2724 rbytes = (afs_uint32)(length_found > biod->length ? biod->length : length_found);
2725 temp = rx_Read(rxcallp, bufferp, rbytes);
2726 if (temp < rbytes) {
2728 * If the file server returned (filesize - offset),
2729 * then the first rx_Read will return zero octets of data.
2730 * If it does, do not treat it as an error. Correct the
2731 * length_found and continue as if the file server said
2732 * it was sending us zero octets of data.
2734 if (fs_fetchdata_offset_bug && first_read) {
2737 } else if (temp <= 0) {
2738 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2744 /* and adjust counters */
2745 length_found -= temp;
2751 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2753 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2756 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2758 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2762 code1 = rx_EndCall(rxcallp, code);
2764 if (code1 == RXKADUNKNOWNKEY)
2765 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2767 /* If we are avoiding a file server bug, ignore the error state */
2768 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2769 /* Clone the current status info and clear the error state */
2770 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2772 lock_ReleaseWrite(&scp->rw);
2776 /* Prefer the error value from FetchData over rx_EndCall */
2777 } else if (code == 0 && code1 != 0)
2779 osi_Log0(afsd_logp, "CALL FetchData DONE");
2781 } while (cm_Analyze(connp, userp, reqp, &scp->fid, NULL, 0, &afsStatus, &volSync, NULL, NULL, code));
2784 code = cm_MapRPCError(code, reqp);
2787 lock_ObtainWrite(&scp->rw);
2790 code = cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2792 InterlockedDecrement(&scp->activeRPCs);
2796 if (dataVersion == scp->dataVersion)
2798 osi_queueData_t *qdp = NULL;
2800 afs_uint32 buf_offset;
2801 afs_uint32 bytes_compared = 0;
2802 afs_uint32 cmp_length;
2807 while ( bytes_compared < biod->length )
2810 qdp = biod->bufListEndp;
2811 buf_offset = biod->offset.LowPart % cm_data.buf_blockSize;
2813 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2816 cmp_length = cm_data.buf_blockSize - buf_offset;
2818 osi_assertx(qdp != NULL, "null osi_queueData_t");
2819 bufp = osi_GetQData(qdp);
2821 if (memcmp(bufferp+bytes_compared, bufp->datap+buf_offset, cmp_length) != 0)
2824 md5_match = buf_ValidateCheckSum(bufp);
2826 osi_Log5(afsd_logp, "cm_VerifyDataStore verification failed scp 0x%p bufp 0x%p offset 0x%x:%08x md5 %s",
2827 scp, bufp, bufp->offset.HighPart, bufp->offset.LowPart, md5_match ? "match" : "no-match");
2829 bytes_compared += cmp_length;
2832 osi_Log4(afsd_logp, "cm_VerifyStoreData unable to verify due to data version change scp 0x%p, off 0x%x:%08x, size 0x%x",
2833 scp, biod->offset.HighPart, biod->offset.LowPart, biod->length);