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 /* we can access connp->serverp without holding a lock because that
38 never changes since the connection is made. */
39 #define SERVERHAS64BIT(connp) (!((connp)->serverp->flags & CM_SERVERFLAG_NO64BIT))
40 #define SET_SERVERHASNO64BIT(connp) (cm_SetServerNo64Bit((connp)->serverp, TRUE))
42 /* functions called back from the buffer package when reading or writing data,
43 * or when holding or releasing a vnode pointer.
45 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
46 cm_user_t *userp, cm_req_t *reqp)
49 * store the data back from this buffer; the buffer is locked and held,
50 * but the vnode involved may or may not be locked depending on whether
51 * or not the CM_BUF_WRITE_SCP_LOCKED flag is set.
54 cm_scache_t *scp = vscp;
56 afs_int32 save_nbytes;
57 cm_scache_t save_scache;
59 AFSFetchStatus outStatus;
60 AFSStoreStatus inStatus;
64 struct rx_call *rxcallp;
65 struct rx_connection *rxconnp;
72 cm_bulkIO_t biod; /* bulk IO descriptor */
73 int require_64bit_ops = 0;
74 int call_was_64bit = 0;
75 int scp_locked = flags & CM_BUF_WRITE_SCP_LOCKED;
76 int storedata_excl = 0;
78 osi_assertx(userp != NULL, "null cm_user_t");
79 osi_assertx(scp != NULL, "null cm_scache_t");
81 memset(&volSync, 0, sizeof(volSync));
84 * now, the buffer may or may not be filled with good data (buf_GetNewLocked
85 * drops lots of locks, and may indeed return a properly initialized
86 * buffer, although more likely it will just return a new, empty, buffer.
89 lock_ObtainWrite(&scp->rw);
90 if (scp->flags & CM_SCACHEFLAG_DELETED) {
92 lock_ReleaseWrite(&scp->rw);
93 return CM_ERROR_NOSUCHFILE;
96 cm_AFSFidFromFid(&tfid, &scp->fid);
98 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
99 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
102 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
104 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
105 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
107 lock_ReleaseWrite(&scp->rw);
111 if (biod.length == 0) {
112 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
113 cm_ReleaseBIOD(&biod, 1, 0, 1); /* should be a NOOP */
115 goto exit_storedata_excl;
118 /* prepare the output status for the store */
119 _InterlockedOr(&scp->mask, CM_SCACHEMASK_CLIENTMODTIME);
120 cm_StatusFromAttr(&inStatus, scp, NULL);
121 truncPos = scp->length;
122 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
123 && LargeIntegerLessThan(scp->truncPos, truncPos)) {
124 truncPos = scp->truncPos;
125 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_TRUNCPOS);
128 /* compute how many bytes to write from this buffer */
129 thyper = LargeIntegerSubtract(scp->length, biod.offset);
130 if (LargeIntegerLessThanZero(thyper)) {
131 /* entire buffer is past EOF */
135 /* otherwise write out part of buffer before EOF, but not
136 * more than bufferSize bytes.
138 if (LargeIntegerGreaterThan(thyper,
139 ConvertLongToLargeInteger(biod.length))) {
140 nbytes = biod.length;
142 /* if thyper is less than or equal to biod.length, then we
143 can safely assume that the value fits in a long. */
144 nbytes = thyper.LowPart;
148 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
149 ConvertLongToLargeInteger(nbytes)),
150 ConvertLongToLargeInteger(LONG_MAX)) ||
151 LargeIntegerGreaterThan(truncPos,
152 ConvertLongToLargeInteger(LONG_MAX))) {
153 require_64bit_ops = 1;
156 /* now we're ready to do the store operation */
158 InterlockedIncrement(&scp->activeRPCs);
159 lock_ReleaseWrite(&scp->rw);
161 save_nbytes = nbytes;
165 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
170 rxconnp = cm_GetRxConn(connp);
171 rxcallp = rx_NewCall(rxconnp);
172 rx_PutConnection(rxconnp);
174 if (SERVERHAS64BIT(connp)) {
177 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
178 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
179 osi_Log2(afsd_logp, "... truncPos 0x%x:%08x", truncPos.HighPart, truncPos.LowPart);
181 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
182 biod.offset.QuadPart,
186 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
188 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
192 if (require_64bit_ops) {
193 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData. The operation requires large file support in the server.");
194 code = CM_ERROR_TOOBIG;
196 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
197 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
199 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
200 biod.offset.LowPart, nbytes, truncPos.LowPart);
202 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
204 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
209 afs_uint32 buf_offset = 0, bytes_copied = 0;
211 /* write the data from the the list of buffers */
215 struct iovec tiov[RX_MAXIOVECS];
216 afs_int32 tnio, vlen, vbytes, iov, voffset;
219 vbytes = rx_WritevAlloc(rxcallp, tiov, &tnio, RX_MAXIOVECS, nbytes);
221 code = RX_PROTOCOL_ERROR;
225 for ( iov = voffset = vlen = 0;
226 vlen < vbytes && iov < tnio; vlen += wbytes) {
228 qdp = biod.bufListEndp;
229 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
230 } else if (buf_offset == cm_data.buf_blockSize) {
231 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
235 osi_assertx(qdp != NULL, "null osi_queueData_t");
236 bufp = osi_GetQData(qdp);
237 bufferp = bufp->datap + buf_offset;
238 wbytes = vbytes - vlen;
239 if (wbytes > cm_data.buf_blockSize - buf_offset)
240 wbytes = cm_data.buf_blockSize - buf_offset;
242 vleft = tiov[iov].iov_len - voffset;
243 while (wbytes > vleft && iov < tnio) {
244 memcpy(tiov[iov].iov_base + voffset, bufferp, vleft);
245 bytes_copied += vleft;
253 vleft = tiov[iov].iov_len;
257 memcpy(tiov[iov].iov_base + voffset, bufferp, wbytes);
258 bytes_copied += wbytes;
259 if (tiov[iov].iov_len == voffset + wbytes) {
262 vleft = (iov < tnio) ? tiov[iov].iov_len : 0;
268 buf_offset += wbytes;
274 osi_assertx(iov == tnio, "incorrect iov count");
275 osi_assertx(vlen == vbytes, "bytes_copied != vbytes");
276 osi_assertx(bufp->offset.QuadPart + buf_offset == biod.offset.QuadPart + bytes_copied,
277 "begin and end offsets don't match");
279 temp = rx_Writev(rxcallp, tiov, tnio, vbytes);
280 if (temp != vbytes) {
281 osi_Log3(afsd_logp, "rx_Writev failed bp 0x%p, %d != %d", bufp, temp, vbytes);
282 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
286 osi_Log2(afsd_logp, "rx_Writev succeeded iov offset 0x%x, wrote 0x%x",
287 (unsigned long)(bufp->offset.QuadPart + buf_offset - vbytes), vbytes);
289 #else /* USE_RX_IOVEC */
291 qdp = biod.bufListEndp;
292 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
294 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
298 osi_assertx(qdp != NULL, "null osi_queueData_t");
299 bufp = osi_GetQData(qdp);
300 bufferp = bufp->datap + buf_offset;
302 if (wbytes > cm_data.buf_blockSize - buf_offset)
303 wbytes = cm_data.buf_blockSize - buf_offset;
305 /* write out wbytes of data from bufferp */
306 temp = rx_Write(rxcallp, bufferp, wbytes);
307 if (temp != wbytes) {
308 osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d", bufp, temp, wbytes);
309 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
312 osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p written %d", bufp, temp);
315 #endif /* USE_RX_IOVEC */
318 * Rx supports an out of band signalling mechanism that permits
319 * RPC specific status information to be communicated in the
320 * reverse direction of the channel. For RXAFS_StoreData, the
321 * 0-bit is set once all of the permission checks have completed
322 * and the volume/vnode locks have been obtained by the file
323 * server. The signal is intended to notify the Unix afs client
324 * that is performing store-on-close that it is safe to permit
325 * the close operation to complete while the store continues
326 * in the background. All of the callbacks have been broken
327 * and the locks will not be dropped until the RPC completes
328 * which prevents any other operation from being initiated on
329 * the vnode until the store is finished.
331 * The Windows client does not perform store-on-close. Instead
332 * it uses the CM_SCACHESYNC_STOREDATA_EXCL request flag and
333 * CM_SCACHEFLAG_DATASTORING scache state to ensure that store
334 * operations are serialized. The 0-bit signal permits the
335 * CM_SCACHEFLAG_DATASTORING state to the dropped which in
336 * turn permits another thread to prep its own BIOD in parallel.
337 * This is safe because it is impossible for that second store
338 * RPC to complete before this one does.
340 if ( storedata_excl && (rx_GetRemoteStatus(rxcallp) & 1)) {
341 lock_ObtainWrite(&scp->rw);
342 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
343 lock_ReleaseWrite(&scp->rw);
346 } /* while more bytes to write */
347 } /* if RPC started successfully */
350 if (call_was_64bit) {
351 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
353 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
355 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
357 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
359 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
361 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
365 code1 = rx_EndCall(rxcallp, code);
367 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
368 SET_SERVERHASNO64BIT(connp);
370 nbytes = save_nbytes;
373 /* Prefer rx_EndCall error over StoreData error */
375 osi_Log2(afsd_logp, "rx_EndCall converted 0x%x to 0x%x", code, code1);
378 } while (cm_Analyze(connp, userp, reqp, &scp->fid, 1, &volSync, NULL, NULL, code));
380 code = cm_MapRPCError(code, reqp);
383 osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
385 osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
387 /* now, clean up our state */
388 lock_ObtainWrite(&scp->rw);
392 /* now, here's something a little tricky: in AFS 3, a dirty
393 * length can't be directly stored, instead, a dirty chunk is
394 * stored that sets the file's size (by writing and by using
395 * the truncate-first option in the store call).
397 * At this point, we've just finished a store, and so the trunc
398 * pos field is clean. If the file's size at the server is at
399 * least as big as we think it should be, then we turn off the
400 * length dirty bit, since all the other dirty buffers must
401 * precede this one in the file.
403 * The file's desired size shouldn't be smaller than what's
404 * stored at the server now, since we just did the trunc pos
407 * We have to turn off the length dirty bit as soon as we can,
408 * so that we see updates made by other machines.
411 if (call_was_64bit) {
412 t.LowPart = outStatus.Length;
413 t.HighPart = outStatus.Length_hi;
415 t = ConvertLongToLargeInteger(outStatus.Length);
418 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
419 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
421 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
423 InterlockedDecrement(&scp->activeRPCs);
424 if (code == CM_ERROR_SPACE)
425 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OUTOFSPACE);
426 else if (code == CM_ERROR_QUOTA)
427 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OVERQUOTA);
432 if (!cm_VerifyStoreData(&biod, &save_scache)) {
433 /* file server content doesn't match what we sent. */
434 nbytes = save_nbytes;
439 cm_ReleaseBIOD(&biod, 1, code, 1);
443 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
446 lock_ReleaseWrite(&scp->rw);
452 * Truncate the file, by sending a StoreData RPC with zero length.
454 * Called with scp locked. Releases and re-obtains the lock.
456 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
458 AFSFetchStatus outStatus;
459 AFSStoreStatus inStatus;
463 osi_hyper_t truncPos;
465 struct rx_call *rxcallp;
466 struct rx_connection *rxconnp;
467 int require_64bit_ops = 0;
468 int call_was_64bit = 0;
470 memset(&volSync, 0, sizeof(volSync));
472 osi_Log2(afsd_logp, "cm_StoreMini scp 0x%p userp 0x%p", scp, userp);
474 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
475 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
476 CM_SCACHESYNC_STOREDATA_EXCL);
478 /* prepare the output status for the store */
479 inStatus.Mask = AFS_SETMODTIME;
480 inStatus.ClientModTime = scp->clientModTime;
481 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_CLIENTMODTIME);
483 /* calculate truncation position */
484 truncPos = scp->length;
485 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
486 && LargeIntegerLessThan(scp->truncPos, truncPos))
487 truncPos = scp->truncPos;
488 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_TRUNCPOS);
490 if (LargeIntegerGreaterThan(truncPos,
491 ConvertLongToLargeInteger(LONG_MAX))) {
493 require_64bit_ops = 1;
496 InterlockedIncrement(&scp->activeRPCs);
497 lock_ReleaseWrite(&scp->rw);
499 cm_AFSFidFromFid(&tfid, &scp->fid);
501 /* now we're ready to do the store operation */
503 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
508 rxconnp = cm_GetRxConn(connp);
509 rxcallp = rx_NewCall(rxconnp);
510 rx_PutConnection(rxconnp);
512 if (SERVERHAS64BIT(connp)) {
515 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, truncPos 0x%x:%08x",
516 scp, truncPos.HighPart, truncPos.LowPart);
518 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
519 0, 0, truncPos.QuadPart);
521 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
523 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
527 if (require_64bit_ops) {
528 code = CM_ERROR_TOOBIG;
530 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, truncPos 0x%x:%08x",
531 scp, truncPos.HighPart, truncPos.LowPart);
533 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
534 0, 0, truncPos.LowPart);
536 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
538 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
543 if (call_was_64bit) {
544 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
546 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
548 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
550 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
552 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX", scp, code);
554 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
557 code1 = rx_EndCall(rxcallp, code);
559 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
560 SET_SERVERHASNO64BIT(connp);
564 /* prefer StoreData error over rx_EndCall error */
565 if (code == 0 && code1 != 0)
567 } while (cm_Analyze(connp, userp, reqp, &scp->fid, 1, &volSync, NULL, NULL, code));
568 code = cm_MapRPCError(code, reqp);
570 /* now, clean up our state */
571 lock_ObtainWrite(&scp->rw);
576 * For explanation of handling of CM_SCACHEMASK_LENGTH,
579 if (call_was_64bit) {
580 t.HighPart = outStatus.Length_hi;
581 t.LowPart = outStatus.Length;
583 t = ConvertLongToLargeInteger(outStatus.Length);
586 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
587 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
588 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
590 InterlockedDecrement(&scp->activeRPCs);
592 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
597 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
601 /* now return a code that means that I/O is done */
606 * stabilize scache entry with CM_SCACHESYNC_SETSIZE. This prevents any new
607 * data buffers to be allocated, new data to be fetched from the file server,
608 * and writes to be accepted from the application but permits dirty buffers
609 * to be written to the file server.
611 * Stabilize uses cm_SyncOp to maintain the cm_scache_t in this stable state
612 * instead of holding the rwlock exclusively. This permits background stores
613 * to be performed in parallel and in particular allow FlushFile to be
614 * implemented without violating the locking hierarchy.
616 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
618 cm_scache_t *scp = vscp;
621 lock_ObtainWrite(&scp->rw);
622 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
623 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
624 lock_ReleaseWrite(&scp->rw);
629 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
630 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
632 cm_scache_t *scp = vscp;
634 lock_ObtainWrite(&scp->rw);
635 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
637 lock_ReleaseWrite(&scp->rw);
639 /* always succeeds */
643 cm_buf_ops_t cm_bufOps = {
650 long cm_ValidateDCache(void)
652 return buf_ValidateBuffers();
655 long cm_ShutdownDCache(void)
660 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
662 return buf_Init(newFile, &cm_bufOps, nbuffers);
665 /* check to see if we have an up-to-date buffer. The buffer must have
666 * previously been obtained by calling buf_Get.
668 * Make sure we have a callback, and that the dataversion matches.
670 * Scp must be locked.
672 * Bufp *may* be locked.
674 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
677 if (!cm_HaveCallback(scp))
679 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
681 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
683 if (bufp->offset.QuadPart >= scp->serverLength.QuadPart)
686 code = lock_TryMutex(&bufp->mx);
688 /* don't have the lock, and can't lock it, then
695 /* remember dirty flag for later */
696 code = bufp->flags & CM_BUF_DIRTY;
698 /* release lock if we obtained it here */
700 lock_ReleaseMutex(&bufp->mx);
702 /* if buffer was dirty, buffer is acceptable for use */
710 * used when deciding whether to do a background fetch or not.
711 * call with scp->rw write-locked.
714 cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
715 cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
719 osi_hyper_t tblocksize;
724 /* now scan all buffers in the range, looking for any that look like
729 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
731 while (LargeIntegerGreaterThanZero(tlength)) {
732 /* get callback so we can do a meaningful dataVersion comparison */
733 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
734 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
738 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
739 /* we're past the end of file */
743 bp = buf_Find(&scp->fid, &tbase);
744 /* We cheat slightly by not locking the bp mutex. */
746 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
747 && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
755 /* if this buffer is essentially guaranteed to require a fetch,
756 * break out here and return this position.
761 tbase = LargeIntegerAdd(tbase, tblocksize);
762 tlength = LargeIntegerSubtract(tlength, tblocksize);
765 /* if we get here, either everything is fine or 'stop' stopped us at a
766 * particular buffer in the range that definitely needs to be fetched.
769 /* return non-zero code since realBasep won't be valid */
773 /* successfully found a page that will need fetching */
781 cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
782 cm_user_t *userp, cm_req_t *reqp)
787 afs_uint32 req_flags = reqp->flags;
789 if (scp->flags & CM_SCACHEFLAG_DELETED) {
790 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
792 /* Retries will be performed by the BkgDaemon thread if appropriate */
793 reqp->flags |= CM_REQ_NORETRY;
795 toffset.LowPart = p1;
796 toffset.HighPart = p2;
799 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
801 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, reqp);
803 osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
805 reqp->flags = req_flags;
809 * Keep the following list synchronized with the
810 * error code list in cm_BkgDaemon
813 case CM_ERROR_TIMEDOUT: /* or server restarting */
815 case CM_ERROR_WOULDBLOCK:
816 case CM_ERROR_ALLBUSY:
817 case CM_ERROR_ALLDOWN:
818 case CM_ERROR_ALLOFFLINE:
819 case CM_ERROR_PARTIALWRITE:
820 break; /* cm_BkgDaemon will re-insert the request in the queue */
823 lock_ObtainWrite(&scp->rw);
824 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
825 lock_ReleaseWrite(&scp->rw);
831 /* Called with scp locked */
832 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
837 end = LargeIntegerAdd(*base, *length);
838 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
839 scp->prefetch.base = *base;
840 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
841 scp->prefetch.end = end;
843 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
846 /* do the prefetch. if the prefetch fails, return 0 (success)
847 * because there is no harm done. */
849 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
850 cm_user_t *userp, cm_req_t *reqp)
857 osi_hyper_t tblocksize;
861 afs_uint32 req_flags;
863 /* Retries will be performed by the BkgDaemon thread if appropriate */
864 req_flags = reqp->flags;
865 reqp->flags |= CM_REQ_NORETRY;
868 fetched.HighPart = 0;
869 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
873 length.HighPart = p4;
875 end = LargeIntegerAdd(base, length);
877 osi_Log5(afsd_logp, "Starting BKG prefetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
878 scp, p2, p1, p4, p3);
880 for ( code = 0, offset = base;
881 code == 0 && LargeIntegerLessThan(offset, end);
882 offset = LargeIntegerAdd(offset, tblocksize) )
885 lock_ReleaseWrite(&scp->rw);
889 code = buf_Get(scp, &offset, reqp, &bp);
893 if (bp->cmFlags & CM_BUF_CMFETCHING) {
894 /* skip this buffer as another thread is already fetching it */
896 lock_ObtainWrite(&scp->rw);
905 lock_ObtainWrite(&scp->rw);
909 code = cm_GetBuffer(scp, bp, NULL, userp, reqp);
911 fetched = LargeIntegerAdd(fetched, tblocksize);
916 lock_ObtainWrite(&scp->rw);
920 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
921 scp, &base, &fetched);
923 /* wakeup anyone who is waiting */
924 if (scp->flags & CM_SCACHEFLAG_WAITING) {
925 osi_Log1(afsd_logp, "CM BkgPrefetch Waking scp 0x%p", scp);
926 osi_Wakeup((LONG_PTR) &scp->flags);
928 lock_ReleaseWrite(&scp->rw);
930 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p code 0x%x fetched 0x%x:%x",
931 scp, code, fetched.HighPart, fetched.LowPart);
933 reqp->flags = req_flags;
937 /* a read was issued to offsetp, and we have to determine whether we should
938 * do a prefetch of the next chunk.
940 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
941 cm_user_t *userp, cm_req_t *reqp)
945 osi_hyper_t realBase;
946 osi_hyper_t readBase;
947 osi_hyper_t readLength;
949 osi_hyper_t tblocksize; /* a long long temp variable */
951 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
954 /* round up to chunk boundary */
955 readBase.LowPart += (cm_chunkSize-1);
956 readBase.LowPart &= (-cm_chunkSize);
958 readLength = ConvertLongToLargeInteger(count);
960 lock_ObtainWrite(&scp->rw);
962 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
963 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
964 lock_ReleaseWrite(&scp->rw);
967 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_PREFETCHING);
969 /* start the scan at the latter of the end of this read or
970 * the end of the last fetched region.
972 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
973 readBase = scp->prefetch.end;
975 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
978 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
979 lock_ReleaseWrite(&scp->rw);
980 return; /* can't find something to prefetch */
983 readEnd = LargeIntegerAdd(realBase, readLength);
986 lock_ReleaseWrite(&scp->rw);
988 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
989 scp, realBase.LowPart);
991 cm_QueueBKGRequest(scp, cm_BkgPrefetch,
992 realBase.LowPart, realBase.HighPart,
993 readLength.LowPart, readLength.HighPart,
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 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1035 lock_ObtainWrite(&scp->rw);
1038 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
1039 thyper = ConvertLongToLargeInteger(temp);
1040 tbase = LargeIntegerAdd(*inOffsetp, thyper);
1042 bufp = buf_Find(&scp->fid, &tbase);
1044 /* get buffer mutex and scp mutex safely */
1045 lock_ReleaseWrite(&scp->rw);
1046 lock_ObtainMutex(&bufp->mx);
1049 * if the buffer is actively involved in I/O
1050 * we wait for the I/O to complete.
1052 if (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING))
1053 buf_WaitIO(scp, bufp);
1055 lock_ObtainWrite(&scp->rw);
1056 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
1057 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1059 lock_ReleaseMutex(&bufp->mx);
1062 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1066 /* if the buffer is dirty, we're done */
1067 if (bufp->flags & CM_BUF_DIRTY) {
1068 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
1069 "WRITING w/o CMSTORING in SetupStoreBIOD");
1070 _InterlockedOr(&bufp->flags, CM_BUF_WRITING);
1074 /* this buffer is clean, so there's no reason to process it */
1075 cm_SyncOpDone(scp, bufp, flags);
1076 lock_ReleaseMutex(&bufp->mx);
1084 /* if we get here, if bufp is null, we didn't find any dirty buffers
1085 * that weren't already being stored back, so we just quit now.
1091 /* don't need buffer mutex any more */
1092 lock_ReleaseMutex(&bufp->mx);
1094 /* put this element in the list */
1095 qdp = osi_QDAlloc();
1096 osi_SetQData(qdp, bufp);
1098 if ( cm_verifyData )
1099 buf_ComputeCheckSum(bufp);
1101 /* don't have to hold bufp, since held by buf_Find above */
1102 osi_QAddH((osi_queue_t **) &biop->bufListp,
1103 (osi_queue_t **) &biop->bufListEndp,
1105 biop->length = cm_data.buf_blockSize;
1106 firstModOffset = bufp->offset;
1107 biop->offset = firstModOffset;
1108 bufp = NULL; /* this buffer and reference added to the queue */
1110 /* compute the window surrounding firstModOffset of size cm_chunkSize */
1111 scanStart = firstModOffset;
1112 scanStart.LowPart &= (-cm_chunkSize);
1113 thyper = ConvertLongToLargeInteger(cm_chunkSize);
1114 scanEnd = LargeIntegerAdd(scanStart, thyper);
1116 flags = CM_SCACHESYNC_GETSTATUS
1117 | CM_SCACHESYNC_STOREDATA
1118 | CM_SCACHESYNC_BUFLOCKED;
1120 /* start by looking backwards until scanStart */
1121 /* hyper version of cm_data.buf_blockSize */
1122 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1123 tbase = LargeIntegerSubtract(firstModOffset, thyper);
1124 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
1125 /* see if we can find the buffer */
1126 bufp = buf_Find(&scp->fid, &tbase);
1130 /* try to lock it, and quit if we can't (simplifies locking) */
1131 lock_ReleaseWrite(&scp->rw);
1132 code = lock_TryMutex(&bufp->mx);
1133 lock_ObtainWrite(&scp->rw);
1140 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1142 lock_ReleaseMutex(&bufp->mx);
1148 if (!(bufp->flags & CM_BUF_DIRTY)) {
1149 /* buffer is clean, so we shouldn't add it */
1150 cm_SyncOpDone(scp, bufp, flags);
1151 lock_ReleaseMutex(&bufp->mx);
1157 /* don't need buffer mutex any more */
1158 lock_ReleaseMutex(&bufp->mx);
1160 /* we have a dirty buffer ready for storing. Add it to the tail
1161 * of the list, since it immediately precedes all of the disk
1162 * addresses we've already collected.
1164 qdp = osi_QDAlloc();
1165 osi_SetQData(qdp, bufp);
1167 if ( cm_verifyData )
1168 buf_ComputeCheckSum(bufp);
1170 /* no buf_hold necessary, since we have it held from buf_Find */
1171 osi_QAddT((osi_queue_t **) &biop->bufListp,
1172 (osi_queue_t **) &biop->bufListEndp,
1174 bufp = NULL; /* added to the queue */
1176 /* update biod info describing the transfer */
1177 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
1178 biop->length += cm_data.buf_blockSize;
1180 /* update loop pointer */
1181 tbase = LargeIntegerSubtract(tbase, thyper);
1182 } /* while loop looking for pages preceding the one we found */
1184 /* now, find later dirty, contiguous pages, and add them to the list */
1185 /* hyper version of cm_data.buf_blockSize */
1186 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1187 tbase = LargeIntegerAdd(firstModOffset, thyper);
1188 while(LargeIntegerLessThan(tbase, scanEnd)) {
1189 /* see if we can find the buffer */
1190 bufp = buf_Find(&scp->fid, &tbase);
1194 /* try to lock it, and quit if we can't (simplifies locking) */
1195 lock_ReleaseWrite(&scp->rw);
1196 code = lock_TryMutex(&bufp->mx);
1197 lock_ObtainWrite(&scp->rw);
1204 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1206 lock_ReleaseMutex(&bufp->mx);
1212 if (!(bufp->flags & CM_BUF_DIRTY)) {
1213 /* buffer is clean, so we shouldn't add it */
1214 cm_SyncOpDone(scp, bufp, flags);
1215 lock_ReleaseMutex(&bufp->mx);
1221 /* don't need buffer mutex any more */
1222 lock_ReleaseMutex(&bufp->mx);
1224 /* we have a dirty buffer ready for storing. Add it to the head
1225 * of the list, since it immediately follows all of the disk
1226 * addresses we've already collected.
1228 qdp = osi_QDAlloc();
1229 osi_SetQData(qdp, bufp);
1231 if ( cm_verifyData )
1232 buf_ComputeCheckSum(bufp);
1234 /* no buf_hold necessary, since we have it held from buf_Find */
1235 osi_QAddH((osi_queue_t **) &biop->bufListp,
1236 (osi_queue_t **) &biop->bufListEndp,
1240 /* update biod info describing the transfer */
1241 biop->length += cm_data.buf_blockSize;
1243 /* update loop pointer */
1244 tbase = LargeIntegerAdd(tbase, thyper);
1245 } /* while loop looking for pages following the first page we found */
1247 /* finally, we're done */
1251 /* scp must be locked; temporarily unlocked during processing.
1252 * If returns 0, returns buffers held in biop, and with
1253 * CM_BUF_CMFETCHING flags set.
1254 * If an error is returned, we don't return any buffers.
1256 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1257 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1261 osi_hyper_t tblocksize; /* a long long temp variable */
1262 osi_hyper_t pageBase; /* base offset we're looking at */
1263 osi_queueData_t *qdp; /* one temp queue structure */
1264 osi_queueData_t *tqdp; /* another temp queue structure */
1265 long collected; /* how many bytes have been collected */
1268 osi_hyper_t fileSize; /* the # of bytes in the file */
1269 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1270 osi_queueData_t *heldBufListEndp; /* first one */
1273 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1275 biop->scp = scp; /* do not hold; held by caller */
1276 biop->userp = userp; /* do not hold; held by caller */
1278 biop->offset = *offsetp;
1279 /* null out the list of buffers */
1280 biop->bufListp = biop->bufListEndp = NULL;
1283 /* first lookup the file's length, so we know when to stop */
1284 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1285 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1289 /* copy out size, since it may change */
1290 fileSize = scp->serverLength;
1292 lock_ReleaseWrite(&scp->rw);
1294 pageBase = *offsetp;
1295 collected = pageBase.LowPart & (cm_chunkSize - 1);
1296 heldBufListp = NULL;
1297 heldBufListEndp = NULL;
1300 * Obtaining buffers can cause dirty buffers to be recycled, which
1301 * can cause a storeback, so cannot be done while we have buffers
1304 * To get around this, we get buffers twice. Before reserving buffers,
1305 * we obtain and release each one individually. After reserving
1306 * buffers, we try to obtain them again, but only by lookup, not by
1307 * recycling. If a buffer has gone away while we were waiting for
1308 * the others, we just use whatever buffers we already have.
1310 * On entry to this function, we are already holding a buffer, so we
1311 * can't wait for reservation. So we call buf_TryReserveBuffers()
1312 * instead. Not only that, we can't really even call buf_Get(), for
1313 * the same reason. We can't avoid that, though. To avoid deadlock
1314 * we allow only one thread to be executing the buf_Get()-buf_Release()
1315 * sequence at a time.
1318 /* first hold all buffers, since we can't hold any locks in buf_Get */
1320 /* stop at chunk boundary */
1321 if (collected >= cm_chunkSize)
1324 /* see if the next page would be past EOF */
1325 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1328 code = buf_Get(scp, &pageBase, reqp, &tbp);
1330 lock_ObtainWrite(&scp->rw);
1331 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1338 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1339 collected += cm_data.buf_blockSize;
1342 /* reserve a chunk's worth of buffers if possible */
1343 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1345 pageBase = *offsetp;
1346 collected = pageBase.LowPart & (cm_chunkSize - 1);
1348 /* now hold all buffers, if they are still there */
1350 /* stop at chunk boundary */
1351 if (collected >= cm_chunkSize)
1354 /* see if the next page would be past EOF */
1355 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1358 tbp = buf_Find(&scp->fid, &pageBase);
1362 /* add the buffer to the list */
1363 qdp = osi_QDAlloc();
1364 osi_SetQData(qdp, tbp);
1365 osi_QAddH((osi_queue_t **)&heldBufListp,
1366 (osi_queue_t **)&heldBufListEndp,
1368 /* leave tbp held (from buf_Get) */
1373 collected += cm_data.buf_blockSize;
1374 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1377 /* look at each buffer, adding it into the list if it looks idle and
1378 * filled with old data. One special case: wait for idle if it is the
1379 * first buffer since we really need that one for our caller to make
1383 collected = 0; /* now count how many we'll really use */
1384 for (tqdp = heldBufListEndp;
1386 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1387 /* get a ptr to the held buffer */
1388 tbp = osi_GetQData(tqdp);
1389 pageBase = tbp->offset;
1391 /* now lock the buffer lock */
1392 lock_ObtainMutex(&tbp->mx);
1393 lock_ObtainWrite(&scp->rw);
1395 /* don't bother fetching over data that is already current */
1396 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1397 /* we don't need this buffer, since it is current */
1398 lock_ReleaseWrite(&scp->rw);
1399 lock_ReleaseMutex(&tbp->mx);
1403 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1405 flags |= CM_SCACHESYNC_NOWAIT;
1407 /* wait for the buffer to serialize, if required. Doesn't
1408 * release the scp or buffer lock(s) if NOWAIT is specified.
1410 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1412 lock_ReleaseWrite(&scp->rw);
1413 lock_ReleaseMutex(&tbp->mx);
1417 /* don't fetch over dirty buffers */
1418 if (tbp->flags & CM_BUF_DIRTY) {
1419 cm_SyncOpDone(scp, tbp, flags);
1420 lock_ReleaseWrite(&scp->rw);
1421 lock_ReleaseMutex(&tbp->mx);
1426 lock_ReleaseWrite(&scp->rw);
1427 lock_ReleaseMutex(&tbp->mx);
1429 /* add the buffer to the list */
1430 qdp = osi_QDAlloc();
1431 osi_SetQData(qdp, tbp);
1432 osi_QAddH((osi_queue_t **)&biop->bufListp,
1433 (osi_queue_t **)&biop->bufListEndp,
1437 /* from now on, a failure just stops our collection process, but
1438 * we still do the I/O to whatever we've already managed to collect.
1441 collected += cm_data.buf_blockSize;
1444 /* now, we've held in biop->bufListp all the buffer's we're really
1445 * interested in. We also have holds left from heldBufListp, and we
1446 * now release those holds on the buffers.
1448 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1449 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1450 tbp = osi_GetQData(qdp);
1451 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1452 (osi_queue_t **) &heldBufListEndp,
1459 /* Caller expects this */
1460 lock_ObtainWrite(&scp->rw);
1462 /* if we got a failure setting up the first buffer, then we don't have
1463 * any side effects yet, and we also have failed an operation that the
1464 * caller requires to make any progress. Give up now.
1466 if (code && isFirst) {
1467 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1471 /* otherwise, we're still OK, and should just return the I/O setup we've
1474 biop->length = collected;
1475 biop->reserved = reserving;
1479 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1482 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
1484 cm_scache_t *scp; /* do not release; not held in biop */
1486 osi_queueData_t *qdp;
1487 osi_queueData_t *nqdp;
1489 int reportErrorToRedir = 0;
1491 /* Give back reserved buffers */
1493 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1496 flags = CM_SCACHESYNC_STOREDATA;
1498 flags = CM_SCACHESYNC_FETCHDATA;
1501 if (biop->bufListp) {
1502 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1503 /* lookup next guy first, since we're going to free this one */
1504 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1506 /* extract buffer and free queue data */
1507 bufp = osi_GetQData(qdp);
1508 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1509 (osi_queue_t **) &biop->bufListEndp,
1513 /* now, mark I/O as done, unlock the buffer and release it */
1515 lock_ReleaseWrite(&scp->rw);
1516 lock_ObtainMutex(&bufp->mx);
1517 lock_ObtainWrite(&scp->rw);
1518 cm_SyncOpDone(scp, bufp, flags);
1520 /* turn off writing and wakeup users */
1522 if (bufp->flags & CM_BUF_WAITING) {
1523 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1524 osi_Wakeup((LONG_PTR) bufp);
1527 _InterlockedAnd(&bufp->flags, ~CM_BUF_WRITING);
1529 case CM_ERROR_NOSUCHFILE:
1530 case CM_ERROR_BADFD:
1531 case CM_ERROR_NOACCESS:
1532 case CM_ERROR_QUOTA:
1533 case CM_ERROR_SPACE:
1534 case CM_ERROR_TOOBIG:
1535 case CM_ERROR_READONLY:
1536 case CM_ERROR_NOSUCHPATH:
1538 * Apply the fatal error to this buffer.
1540 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
1541 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
1542 bufp->dirty_offset = 0;
1543 bufp->dirty_length = 0;
1545 bufp->dataVersion = CM_BUF_VERSION_BAD;
1546 bufp->dirtyCounter++;
1547 reportErrorToRedir = 1;
1549 case CM_ERROR_TIMEDOUT:
1550 case CM_ERROR_ALLDOWN:
1551 case CM_ERROR_ALLBUSY:
1552 case CM_ERROR_ALLOFFLINE:
1553 case CM_ERROR_CLOCKSKEW:
1555 /* do not mark the buffer in error state but do
1556 * not attempt to complete the rest either.
1561 _InterlockedAnd(&bufp->flags, ~(CM_BUF_WRITING | CM_BUF_DIRTY));
1562 bufp->dirty_offset = bufp->dirty_length = 0;
1567 lock_ReleaseWrite(&scp->rw);
1568 lock_ReleaseMutex(&bufp->mx);
1573 if (RDR_Initialized && reportErrorToRedir) {
1575 smb_MapNTError(cm_MapRPCError(code, biop->reqp), &status, TRUE);
1576 RDR_SetFileStatus( &scp->fid, &biop->userp->authgroup, status);
1580 lock_ObtainWrite(&scp->rw);
1581 cm_SyncOpDone(scp, NULL, flags);
1583 lock_ReleaseWrite(&scp->rw);
1586 /* clean things out */
1587 biop->bufListp = NULL;
1588 biop->bufListEndp = NULL;
1592 cm_CloneStatus(cm_scache_t *scp, cm_user_t *userp, int scp_locked,
1593 AFSFetchStatus *afsStatusp, AFSVolSync *volSyncp)
1595 // setup the status based upon the scp data
1596 afsStatusp->InterfaceVersion = 0x1;
1597 switch (scp->fileType) {
1598 case CM_SCACHETYPE_FILE:
1599 afsStatusp->FileType = File;
1601 case CM_SCACHETYPE_DIRECTORY:
1602 afsStatusp->FileType = Directory;
1604 case CM_SCACHETYPE_MOUNTPOINT:
1605 afsStatusp->FileType = SymbolicLink;
1607 case CM_SCACHETYPE_SYMLINK:
1608 case CM_SCACHETYPE_DFSLINK:
1609 afsStatusp->FileType = SymbolicLink;
1612 afsStatusp->FileType = -1; /* an invalid value */
1614 afsStatusp->LinkCount = scp->linkCount;
1615 afsStatusp->Length = scp->length.LowPart;
1616 afsStatusp->DataVersion = (afs_uint32)(scp->dataVersion & MAX_AFS_UINT32);
1617 afsStatusp->Author = 0x1;
1618 afsStatusp->Owner = scp->owner;
1620 lock_ObtainWrite(&scp->rw);
1623 if (cm_FindACLCache(scp, userp, &afsStatusp->CallerAccess))
1624 afsStatusp->CallerAccess = scp->anyAccess;
1625 afsStatusp->AnonymousAccess = scp->anyAccess;
1626 afsStatusp->UnixModeBits = scp->unixModeBits;
1627 afsStatusp->ParentVnode = scp->parentVnode;
1628 afsStatusp->ParentUnique = scp->parentUnique;
1629 afsStatusp->ResidencyMask = 0;
1630 afsStatusp->ClientModTime = scp->clientModTime;
1631 afsStatusp->ServerModTime = scp->serverModTime;
1632 afsStatusp->Group = scp->group;
1633 afsStatusp->SyncCounter = 0;
1634 afsStatusp->dataVersionHigh = (afs_uint32)(scp->dataVersion >> 32);
1635 afsStatusp->lockCount = 0;
1636 afsStatusp->Length_hi = scp->length.HighPart;
1637 afsStatusp->errorCode = 0;
1639 volSyncp->spare1 = scp->volumeCreationDate;
1644 /* Fetch a buffer. Called with scp locked.
1645 * The scp is locked on return.
1647 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1650 long code=0, code1=0;
1651 afs_uint32 nbytes; /* bytes in transfer */
1652 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1653 afs_uint64 length_found = 0;
1654 long rbytes; /* bytes in rx_Read call */
1656 AFSFetchStatus afsStatus;
1657 AFSCallBack callback;
1660 afs_uint32 buffer_offset;
1661 cm_buf_t *tbufp; /* buf we're filling */
1662 osi_queueData_t *qdp; /* q element we're scanning */
1664 struct rx_call *rxcallp;
1665 struct rx_connection *rxconnp;
1666 cm_bulkIO_t biod; /* bulk IO descriptor */
1670 int require_64bit_ops = 0;
1671 int call_was_64bit = 0;
1672 int fs_fetchdata_offset_bug = 0;
1676 memset(&volSync, 0, sizeof(volSync));
1678 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
1679 * drops lots of locks, and may indeed return a properly initialized
1680 * buffer, although more likely it will just return a new, empty, buffer.
1683 #ifdef AFS_FREELANCE_CLIENT
1685 // yj: if they're trying to get the /afs directory, we need to
1686 // handle it differently, since it's local rather than on any
1689 getroot = (scp==cm_data.rootSCachep);
1691 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1694 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1695 /* We already have this buffer don't do extra work */
1699 cm_AFSFidFromFid(&tfid, &scp->fid);
1701 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1703 /* couldn't even get the first page setup properly */
1704 osi_Log1(afsd_logp, "GetBuffer: SetupFetchBIOD failure code %d", code);
1708 /* once we get here, we have the callback in place, we know that no one
1709 * is fetching the data now. Check one last time that we still have
1710 * the wrong data, and then fetch it if we're still wrong.
1712 * We can lose a race condition and end up with biod.length zero, in
1713 * which case we just retry.
1715 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
1716 if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow) &&
1717 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength))
1719 osi_Log4(afsd_logp, "Bad DVs 0x%x != (0x%x -> 0x%x) or length 0x%x",
1720 bufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion, biod.length);
1722 if (bufp->dataVersion == CM_BUF_VERSION_BAD)
1723 memset(bufp->datap, 0, cm_data.buf_blockSize);
1724 bufp->dataVersion = scp->dataVersion;
1726 cm_ReleaseBIOD(&biod, 0, 0, 1);
1728 } else if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow)
1729 && (scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1730 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->truncPos)) {
1731 memset(bufp->datap, 0, cm_data.buf_blockSize);
1732 bufp->dataVersion = scp->dataVersion;
1733 cm_ReleaseBIOD(&biod, 0, 0, 1);
1737 InterlockedIncrement(&scp->activeRPCs);
1738 lock_ReleaseWrite(&scp->rw);
1741 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1742 ConvertLongToLargeInteger(biod.length)),
1743 ConvertLongToLargeInteger(LONG_MAX))) {
1744 require_64bit_ops = 1;
1747 osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1748 osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1749 scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1751 #ifdef AFS_FREELANCE_CLIENT
1754 // if getroot then we don't need to make any calls
1755 // just return fake data
1757 if (cm_freelanceEnabled && getroot) {
1758 // setup the fake status
1759 afsStatus.InterfaceVersion = 0x1;
1760 afsStatus.FileType = 0x2;
1761 afsStatus.LinkCount = scp->linkCount;
1762 afsStatus.Length = cm_fakeDirSize;
1763 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1764 afsStatus.Author = 0x1;
1765 afsStatus.Owner = 0x0;
1766 afsStatus.CallerAccess = 0x9;
1767 afsStatus.AnonymousAccess = 0x9;
1768 afsStatus.UnixModeBits = 0x1ff;
1769 afsStatus.ParentVnode = 0x1;
1770 afsStatus.ParentUnique = 0x1;
1771 afsStatus.ResidencyMask = 0;
1772 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1773 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1774 afsStatus.Group = 0;
1775 afsStatus.SyncCounter = 0;
1776 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1777 afsStatus.lockCount = 0;
1778 afsStatus.Length_hi = 0;
1779 afsStatus.errorCode = 0;
1780 memset(&volSync, 0, sizeof(volSync));
1782 // once we're done setting up the status info,
1783 // we just fill the buffer pages with fakedata
1784 // from cm_FakeRootDir. Extra pages are set to
1787 lock_ObtainMutex(&cm_Freelance_Lock);
1788 t1 = bufp->offset.LowPart;
1789 qdp = biod.bufListEndp;
1791 tbufp = osi_GetQData(qdp);
1792 bufferp=tbufp->datap;
1793 memset(bufferp, 0, cm_data.buf_blockSize);
1794 t2 = cm_fakeDirSize - t1;
1795 if (t2> (afs_int32)cm_data.buf_blockSize)
1796 t2=cm_data.buf_blockSize;
1798 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1803 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1806 lock_ReleaseMutex(&cm_Freelance_Lock);
1808 // once we're done, we skip over the part of the
1809 // code that does the ACTUAL fetching of data for
1812 goto fetchingcompleted;
1815 #endif /* AFS_FREELANCE_CLIENT */
1818 * if the requested offset is greater than the file length,
1819 * the file server will return zero bytes of data and the
1820 * current status for the file which we already have since
1821 * we have just obtained a callback. Instead, we can avoid
1822 * the network round trip by allocating zeroed buffers and
1823 * faking the status info.
1825 if (biod.offset.QuadPart >= scp->length.QuadPart) {
1826 osi_Log5(afsd_logp, "SKIP FetchData64 scp 0x%p, off 0x%x:%08x > length 0x%x:%08x",
1827 scp, biod.offset.HighPart, biod.offset.LowPart,
1828 scp->length.HighPart, scp->length.LowPart);
1830 /* Clone the current status info */
1831 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
1833 /* status info complete, fill pages with zeros */
1834 for (qdp = biod.bufListEndp;
1836 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1837 tbufp = osi_GetQData(qdp);
1838 bufferp=tbufp->datap;
1839 memset(bufferp, 0, cm_data.buf_blockSize);
1842 /* no need to contact the file server */
1843 goto fetchingcompleted;
1847 lock_ReleaseWrite(&scp->rw);
1851 /* now make the call */
1853 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1857 rxconnp = cm_GetRxConn(connp);
1858 rxcallp = rx_NewCall(rxconnp);
1859 rx_PutConnection(rxconnp);
1861 nbytes = nbytes_hi = 0;
1863 if (SERVERHAS64BIT(connp)) {
1866 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1867 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1869 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1872 temp = rx_Read32(rxcallp, &nbytes_hi);
1873 if (temp == sizeof(afs_int32)) {
1874 nbytes_hi = ntohl(nbytes_hi);
1877 code = rx_Error(rxcallp);
1878 code1 = rx_EndCall(rxcallp, code);
1886 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1887 if (require_64bit_ops) {
1888 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1889 code = CM_ERROR_TOOBIG;
1892 rxconnp = cm_GetRxConn(connp);
1893 rxcallp = rx_NewCall(rxconnp);
1894 rx_PutConnection(rxconnp);
1897 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1898 scp, biod.offset.LowPart, biod.length);
1900 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1903 SET_SERVERHASNO64BIT(connp);
1908 temp = rx_Read32(rxcallp, &nbytes);
1909 if (temp == sizeof(afs_int32)) {
1910 nbytes = ntohl(nbytes);
1911 FillInt64(length_found, nbytes_hi, nbytes);
1912 if (length_found > biod.length) {
1914 * prior to 1.4.12 and 1.5.65 the file server would return
1915 * (filesize - offset) if the requested offset was greater than
1916 * the filesize. The correct return value would have been zero.
1917 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
1918 * is a race between two RPCs issues by this cache manager, the
1919 * correct thing will happen the second time.
1921 osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1922 fs_fetchdata_offset_bug = 1;
1925 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1926 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
1929 /* for the moment, nbytes_hi will always be 0 if code == 0
1930 because biod.length is a 32-bit quantity. */
1933 qdp = biod.bufListEndp;
1935 tbufp = osi_GetQData(qdp);
1936 bufferp = tbufp->datap;
1942 /* fill length_found of data from the pipe into the pages.
1943 * When we stop, qdp will point at the last page we're
1944 * dealing with, and bufferp will tell us where we
1945 * stopped. We'll need this info below when we clear
1946 * the remainder of the last page out (and potentially
1947 * clear later pages out, if we fetch past EOF).
1949 while (length_found > 0) {
1951 struct iovec tiov[RX_MAXIOVECS];
1952 afs_int32 tnio, iov, iov_offset;
1954 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
1955 osi_Log1(afsd_logp, "cm_GetBuffer rx_Readv returns %d", temp);
1956 if (temp != length_found && temp < cm_data.buf_blockSize) {
1958 * If the file server returned (filesize - offset),
1959 * then the first rx_Read will return zero octets of data.
1960 * If it does, do not treat it as an error. Correct the
1961 * length_found and continue as if the file server said
1962 * it was sending us zero octets of data.
1964 if (fs_fetchdata_offset_bug && first_read)
1967 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
1975 while (rbytes > 0) {
1978 osi_assertx(bufferp != NULL, "null cm_buf_t");
1980 len = min(tiov[iov].iov_len - iov_offset, cm_data.buf_blockSize - buffer_offset);
1981 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
1983 buffer_offset += len;
1986 if (iov_offset == tiov[iov].iov_len) {
1991 if (buffer_offset == cm_data.buf_blockSize) {
1992 /* allow read-while-fetching.
1993 * if this is the last buffer, clear the
1994 * PREFETCHING flag, so the reader waiting for
1995 * this buffer will start a prefetch.
1997 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
1998 lock_ObtainWrite(&scp->rw);
1999 if (scp->flags & CM_SCACHEFLAG_WAITING) {
2000 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
2001 osi_Wakeup((LONG_PTR) &scp->flags);
2003 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
2004 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
2006 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
2008 lock_ReleaseWrite(&scp->rw);
2010 /* Advance the buffer */
2011 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2013 tbufp = osi_GetQData(qdp);
2014 bufferp = tbufp->datap;
2022 length_found -= temp;
2023 #else /* USE_RX_IOVEC */
2024 /* assert that there are still more buffers;
2025 * our check above for length_found being less than
2026 * biod.length should ensure this.
2028 osi_assertx(bufferp != NULL, "null cm_buf_t");
2030 /* read rbytes of data */
2031 rbytes = (afs_uint32)(length_found > cm_data.buf_blockSize ? cm_data.buf_blockSize : length_found);
2032 temp = rx_Read(rxcallp, bufferp, rbytes);
2033 if (temp < rbytes) {
2035 * If the file server returned (filesize - offset),
2036 * then the first rx_Read will return zero octets of data.
2037 * If it does, do not treat it as an error. Correct the
2038 * length_found and continue as if the file server said
2039 * it was sending us zero octets of data.
2041 if (fs_fetchdata_offset_bug && first_read)
2044 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2049 /* allow read-while-fetching.
2050 * if this is the last buffer, clear the
2051 * PREFETCHING flag, so the reader waiting for
2052 * this buffer will start a prefetch.
2054 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
2055 lock_ObtainWrite(&scp->rw);
2056 if (scp->flags & CM_SCACHEFLAG_WAITING) {
2057 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
2058 osi_Wakeup((LONG_PTR) &scp->flags);
2060 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
2061 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
2063 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
2065 lock_ReleaseWrite(&scp->rw);
2067 /* and adjust counters */
2068 length_found -= temp;
2070 /* and move to the next buffer */
2071 if (length_found != 0) {
2072 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2074 tbufp = osi_GetQData(qdp);
2075 bufferp = tbufp->datap;
2081 #endif /* USE_RX_IOVEC */
2084 /* zero out remainder of last pages, in case we are
2085 * fetching past EOF. We were fetching an integral #
2086 * of pages, but stopped, potentially in the middle of
2087 * a page. Zero the remainder of that page, and then
2088 * all of the rest of the pages.
2091 rbytes = cm_data.buf_blockSize - buffer_offset;
2092 bufferp = tbufp->datap + buffer_offset;
2093 #else /* USE_RX_IOVEC */
2095 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
2096 rbytes = (long) (bufferp - tbufp->datap);
2098 /* bytes left to zero */
2099 rbytes = cm_data.buf_blockSize - rbytes;
2100 #endif /* USE_RX_IOVEC */
2103 memset(bufferp, 0, rbytes);
2104 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2107 tbufp = osi_GetQData(qdp);
2108 bufferp = tbufp->datap;
2109 /* bytes to clear in this page */
2110 rbytes = cm_data.buf_blockSize;
2116 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2118 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2121 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2123 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2127 code1 = rx_EndCall(rxcallp, code);
2129 if (code1 == RXKADUNKNOWNKEY)
2130 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2132 /* If we are avoiding a file server bug, ignore the error state */
2133 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2134 /* Clone the current status info and clear the error state */
2135 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2137 lock_ReleaseWrite(&scp->rw);
2141 /* Prefer the error value from FetchData over rx_EndCall */
2142 } else if (code == 0 && code1 != 0)
2144 osi_Log0(afsd_logp, "CALL FetchData DONE");
2146 } while (cm_Analyze(connp, userp, reqp, &scp->fid, 0, &volSync, NULL, NULL, code));
2149 code = cm_MapRPCError(code, reqp);
2152 lock_ObtainWrite(&scp->rw);
2154 /* we know that no one else has changed the buffer, since we still have
2155 * the fetching flag on the buffers, and we have the scp locked again.
2156 * Copy in the version # into the buffer if we got code 0 back from the
2160 for(qdp = biod.bufListp;
2162 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
2163 tbufp = osi_GetQData(qdp);
2164 tbufp->dataVersion = afsStatus.dataVersionHigh;
2165 tbufp->dataVersion <<= 32;
2166 tbufp->dataVersion |= afsStatus.DataVersion;
2169 /* write buffer out to disk cache */
2170 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
2171 tbufp->dataVersion);
2172 #endif /* DISKCACHE95 */
2177 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2179 InterlockedDecrement(&scp->activeRPCs);
2181 /* release scatter/gather I/O structure (buffers, locks) */
2182 cm_ReleaseBIOD(&biod, 0, code, 1);
2188 * Similar to cm_GetBuffer but doesn't use an allocated cm_buf_t object.
2189 * Instead the data is read from the file server and copied directly into
2190 * a provided buffer. Called with scp locked. The scp is locked on return.
2192 long cm_GetData(cm_scache_t *scp, osi_hyper_t *offsetp, char *datap, int data_length,
2193 cm_user_t *userp, cm_req_t *reqp)
2195 long code=0, code1=0;
2196 afs_uint32 nbytes; /* bytes in transfer */
2197 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2198 afs_uint64 length_found = 0;
2199 char *bufferp = datap;
2200 afs_uint32 buffer_offset = 0;
2201 long rbytes; /* bytes in rx_Read call */
2203 AFSFetchStatus afsStatus;
2204 AFSCallBack callback;
2207 struct rx_call *rxcallp;
2208 struct rx_connection *rxconnp;
2212 int require_64bit_ops = 0;
2213 int call_was_64bit = 0;
2214 int fs_fetchdata_offset_bug = 0;
2218 memset(&volSync, 0, sizeof(volSync));
2220 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
2221 * drops lots of locks, and may indeed return a properly initialized
2222 * buffer, although more likely it will just return a new, empty, buffer.
2225 #ifdef AFS_FREELANCE_CLIENT
2227 // yj: if they're trying to get the /afs directory, we need to
2228 // handle it differently, since it's local rather than on any
2231 getroot = (scp==cm_data.rootSCachep);
2233 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
2236 cm_AFSFidFromFid(&tfid, &scp->fid);
2238 if (LargeIntegerGreaterThan(LargeIntegerAdd(*offsetp,
2239 ConvertLongToLargeInteger(data_length)),
2240 ConvertLongToLargeInteger(LONG_MAX))) {
2241 require_64bit_ops = 1;
2244 InterlockedIncrement(&scp->activeRPCs);
2245 osi_Log2(afsd_logp, "cm_GetData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2247 #ifdef AFS_FREELANCE_CLIENT
2250 // if getroot then we don't need to make any calls
2251 // just return fake data
2253 if (cm_freelanceEnabled && getroot) {
2254 // setup the fake status
2255 afsStatus.InterfaceVersion = 0x1;
2256 afsStatus.FileType = 0x2;
2257 afsStatus.LinkCount = scp->linkCount;
2258 afsStatus.Length = cm_fakeDirSize;
2259 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
2260 afsStatus.Author = 0x1;
2261 afsStatus.Owner = 0x0;
2262 afsStatus.CallerAccess = 0x9;
2263 afsStatus.AnonymousAccess = 0x9;
2264 afsStatus.UnixModeBits = 0x1ff;
2265 afsStatus.ParentVnode = 0x1;
2266 afsStatus.ParentUnique = 0x1;
2267 afsStatus.ResidencyMask = 0;
2268 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
2269 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
2270 afsStatus.Group = 0;
2271 afsStatus.SyncCounter = 0;
2272 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
2273 afsStatus.lockCount = 0;
2274 afsStatus.Length_hi = 0;
2275 afsStatus.errorCode = 0;
2276 memset(&volSync, 0, sizeof(volSync));
2278 // once we're done setting up the status info,
2279 // we just fill the buffer pages with fakedata
2280 // from cm_FakeRootDir. Extra pages are set to
2283 lock_ObtainMutex(&cm_Freelance_Lock);
2284 t1 = offsetp->LowPart;
2285 memset(datap, 0, data_length);
2286 t2 = cm_fakeDirSize - t1;
2287 if (t2 > data_length)
2290 memcpy(datap, cm_FakeRootDir+t1, t2);
2291 lock_ReleaseMutex(&cm_Freelance_Lock);
2293 // once we're done, we skip over the part of the
2294 // code that does the ACTUAL fetching of data for
2297 goto fetchingcompleted;
2300 #endif /* AFS_FREELANCE_CLIENT */
2303 lock_ReleaseWrite(&scp->rw);
2307 /* now make the call */
2309 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2313 rxconnp = cm_GetRxConn(connp);
2314 rxcallp = rx_NewCall(rxconnp);
2315 rx_PutConnection(rxconnp);
2317 nbytes = nbytes_hi = 0;
2319 if (SERVERHAS64BIT(connp)) {
2322 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2323 scp, offsetp->HighPart, offsetp->LowPart, data_length);
2325 code = StartRXAFS_FetchData64(rxcallp, &tfid, offsetp->QuadPart, data_length);
2328 temp = rx_Read32(rxcallp, &nbytes_hi);
2329 if (temp == sizeof(afs_int32)) {
2330 nbytes_hi = ntohl(nbytes_hi);
2333 code = rx_Error(rxcallp);
2334 code1 = rx_EndCall(rxcallp, code);
2342 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2343 if (require_64bit_ops) {
2344 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2345 code = CM_ERROR_TOOBIG;
2348 rxconnp = cm_GetRxConn(connp);
2349 rxcallp = rx_NewCall(rxconnp);
2350 rx_PutConnection(rxconnp);
2353 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2354 scp, offsetp->LowPart, data_length);
2356 code = StartRXAFS_FetchData(rxcallp, &tfid, offsetp->LowPart, data_length);
2358 SET_SERVERHASNO64BIT(connp);
2363 temp = rx_Read32(rxcallp, &nbytes);
2364 if (temp == sizeof(afs_int32)) {
2365 nbytes = ntohl(nbytes);
2366 FillInt64(length_found, nbytes_hi, nbytes);
2367 if (length_found > data_length) {
2369 * prior to 1.4.12 and 1.5.65 the file server would return
2370 * (filesize - offset) if the requested offset was greater than
2371 * the filesize. The correct return value would have been zero.
2372 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2373 * is a race between two RPCs issues by this cache manager, the
2374 * correct thing will happen the second time.
2376 osi_Log0(afsd_logp, "cm_GetData length_found > data_length");
2377 fs_fetchdata_offset_bug = 1;
2380 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2381 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2384 /* for the moment, nbytes_hi will always be 0 if code == 0
2385 because data_length is a 32-bit quantity. */
2388 /* fill length_found of data from the pipe into the pages.
2389 * When we stop, qdp will point at the last page we're
2390 * dealing with, and bufferp will tell us where we
2391 * stopped. We'll need this info below when we clear
2392 * the remainder of the last page out (and potentially
2393 * clear later pages out, if we fetch past EOF).
2395 while (length_found > 0) {
2397 struct iovec tiov[RX_MAXIOVECS];
2398 afs_int32 tnio, iov, iov_offset;
2400 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
2401 osi_Log1(afsd_logp, "cm_GetData rx_Readv returns %d", temp);
2402 if (temp != length_found && temp < data_length) {
2404 * If the file server returned (filesize - offset),
2405 * then the first rx_Read will return zero octets of data.
2406 * If it does, do not treat it as an error. Correct the
2407 * length_found and continue as if the file server said
2408 * it was sending us zero octets of data.
2410 if (fs_fetchdata_offset_bug && first_read)
2413 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2421 while (rbytes > 0) {
2424 osi_assertx(bufferp != NULL, "null cm_buf_t");
2426 len = min(tiov[iov].iov_len - iov_offset, data_length - buffer_offset);
2427 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
2429 buffer_offset += len;
2432 if (iov_offset == tiov[iov].iov_len) {
2438 length_found -= temp;
2439 #else /* USE_RX_IOVEC */
2440 /* assert that there are still more buffers;
2441 * our check above for length_found being less than
2442 * data_length should ensure this.
2444 osi_assertx(bufferp != NULL, "null cm_buf_t");
2446 /* read rbytes of data */
2447 rbytes = (afs_uint32)(length_found > data_length ? data_length : length_found);
2448 temp = rx_Read(rxcallp, bufferp, rbytes);
2449 if (temp < rbytes) {
2451 * If the file server returned (filesize - offset),
2452 * then the first rx_Read will return zero octets of data.
2453 * If it does, do not treat it as an error. Correct the
2454 * length_found and continue as if the file server said
2455 * it was sending us zero octets of data.
2457 if (fs_fetchdata_offset_bug && first_read)
2460 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2465 /* and adjust counters */
2466 length_found -= temp;
2467 #endif /* USE_RX_IOVEC */
2470 /* zero out remainder of last pages, in case we are
2471 * fetching past EOF. We were fetching an integral #
2472 * of pages, but stopped, potentially in the middle of
2473 * a page. Zero the remainder of that page, and then
2474 * all of the rest of the pages.
2477 rbytes = data_length - buffer_offset;
2478 bufferp = datap + buffer_offset;
2479 #else /* USE_RX_IOVEC */
2481 osi_assertx((bufferp - datap) < LONG_MAX, "data >= LONG_MAX");
2482 rbytes = (long) (bufferp - datap);
2484 /* bytes left to zero */
2485 rbytes = data_length - rbytes;
2486 #endif /* USE_RX_IOVEC */
2488 memset(bufferp, 0, rbytes);
2493 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2495 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2498 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2500 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2504 code1 = rx_EndCall(rxcallp, code);
2506 if (code1 == RXKADUNKNOWNKEY)
2507 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2509 /* If we are avoiding a file server bug, ignore the error state */
2510 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2511 /* Clone the current status info and clear the error state */
2512 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2514 lock_ReleaseWrite(&scp->rw);
2518 /* Prefer the error value from FetchData over rx_EndCall */
2519 } else if (code == 0 && code1 != 0)
2521 osi_Log0(afsd_logp, "CALL FetchData DONE");
2523 } while (cm_Analyze(connp, userp, reqp, &scp->fid, 0, &volSync, NULL, NULL, code));
2526 code = cm_MapRPCError(code, reqp);
2529 lock_ObtainWrite(&scp->rw);
2532 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2534 InterlockedDecrement(&scp->activeRPCs);
2540 * cm_VerifyStoreData. Function passed a rw locked cm_scache_t and a store data biod.
2542 * Return 1 if the data verifies; 0 if not.
2546 cm_VerifyStoreData(cm_bulkIO_t *biod, cm_scache_t *savedScp)
2548 long code=0, code1=0;
2549 afs_uint32 nbytes; /* bytes in transfer */
2550 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2551 afs_uint64 length_found = 0;
2552 long rbytes; /* bytes in rx_Read call */
2554 AFSFetchStatus afsStatus;
2555 AFSCallBack callback;
2558 struct rx_call *rxcallp;
2559 struct rx_connection *rxconnp;
2561 int require_64bit_ops = 0;
2562 int call_was_64bit = 0;
2563 int fs_fetchdata_offset_bug = 0;
2566 char * bufferp = malloc(biod->length);
2568 cm_scache_t *scp = biod->scp;
2569 cm_user_t *userp = biod->userp;
2570 cm_req_t *reqp = biod->reqp;
2571 afs_uint64 dataVersion = scp->dataVersion;
2573 memset(&volSync, 0, sizeof(volSync));
2574 memset(bufferp, 0, biod->length);
2576 cm_AFSFidFromFid(&tfid, &scp->fid);
2578 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod->offset,
2579 ConvertLongToLargeInteger(biod->length)),
2580 ConvertLongToLargeInteger(LONG_MAX))) {
2581 require_64bit_ops = 1;
2584 InterlockedIncrement(&scp->activeRPCs);
2585 osi_Log2(afsd_logp, "cm_VerifyStoreData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2588 lock_ReleaseWrite(&scp->rw);
2592 /* now make the call */
2594 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2598 rxconnp = cm_GetRxConn(connp);
2599 rxcallp = rx_NewCall(rxconnp);
2600 rx_PutConnection(rxconnp);
2602 nbytes = nbytes_hi = 0;
2604 if (SERVERHAS64BIT(connp)) {
2607 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2608 scp, biod->offset.HighPart, biod->offset.LowPart, biod->length);
2610 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod->offset.QuadPart, biod->length);
2613 temp = rx_Read32(rxcallp, &nbytes_hi);
2614 if (temp == sizeof(afs_int32)) {
2615 nbytes_hi = ntohl(nbytes_hi);
2618 code = rx_Error(rxcallp);
2619 code1 = rx_EndCall(rxcallp, code);
2627 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2628 if (require_64bit_ops) {
2629 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2630 code = CM_ERROR_TOOBIG;
2633 rxconnp = cm_GetRxConn(connp);
2634 rxcallp = rx_NewCall(rxconnp);
2635 rx_PutConnection(rxconnp);
2638 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2639 scp, biod->offset.LowPart, biod->length);
2641 code = StartRXAFS_FetchData(rxcallp, &tfid, biod->offset.LowPart, biod->length);
2643 SET_SERVERHASNO64BIT(connp);
2648 temp = rx_Read32(rxcallp, &nbytes);
2649 if (temp == sizeof(afs_int32)) {
2650 nbytes = ntohl(nbytes);
2651 FillInt64(length_found, nbytes_hi, nbytes);
2652 if (length_found > biod->length) {
2654 * prior to 1.4.12 and 1.5.65 the file server would return
2655 * (filesize - offset) if the requested offset was greater than
2656 * the filesize. The correct return value would have been zero.
2657 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2658 * is a race between two RPCs issues by this cache manager, the
2659 * correct thing will happen the second time.
2661 osi_Log0(afsd_logp, "cm_GetData length_found > biod.length");
2662 fs_fetchdata_offset_bug = 1;
2665 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2666 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2669 /* for the moment, nbytes_hi will always be 0 if code == 0
2670 because data_length is a 32-bit quantity. */
2673 /* fill length_found of data from the pipe into the pages.
2674 * When we stop, qdp will point at the last page we're
2675 * dealing with, and bufferp will tell us where we
2676 * stopped. We'll need this info below when we clear
2677 * the remainder of the last page out (and potentially
2678 * clear later pages out, if we fetch past EOF).
2680 while (length_found > 0) {
2681 /* assert that there are still more buffers;
2682 * our check above for length_found being less than
2683 * data_length should ensure this.
2685 osi_assertx(bufferp != NULL, "null cm_buf_t");
2687 /* read rbytes of data */
2688 rbytes = (afs_uint32)(length_found > biod->length ? biod->length : length_found);
2689 temp = rx_Read(rxcallp, bufferp, rbytes);
2690 if (temp < rbytes) {
2692 * If the file server returned (filesize - offset),
2693 * then the first rx_Read will return zero octets of data.
2694 * If it does, do not treat it as an error. Correct the
2695 * length_found and continue as if the file server said
2696 * it was sending us zero octets of data.
2698 if (fs_fetchdata_offset_bug && first_read)
2701 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2706 /* and adjust counters */
2707 length_found -= temp;
2713 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2715 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2718 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2720 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2724 code1 = rx_EndCall(rxcallp, code);
2726 if (code1 == RXKADUNKNOWNKEY)
2727 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2729 /* If we are avoiding a file server bug, ignore the error state */
2730 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2731 /* Clone the current status info and clear the error state */
2732 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2734 lock_ReleaseWrite(&scp->rw);
2738 /* Prefer the error value from FetchData over rx_EndCall */
2739 } else if (code == 0 && code1 != 0)
2741 osi_Log0(afsd_logp, "CALL FetchData DONE");
2743 } while (cm_Analyze(connp, userp, reqp, &scp->fid, 0, &volSync, NULL, NULL, code));
2746 code = cm_MapRPCError(code, reqp);
2749 lock_ObtainWrite(&scp->rw);
2752 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2754 InterlockedDecrement(&scp->activeRPCs);
2758 if (dataVersion == scp->dataVersion)
2760 osi_queueData_t *qdp = NULL;
2762 afs_uint32 buf_offset;
2763 afs_uint32 bytes_compared = 0;
2764 afs_uint32 cmp_length;
2769 while ( bytes_compared < biod->length )
2772 qdp = biod->bufListEndp;
2773 buf_offset = biod->offset.LowPart % cm_data.buf_blockSize;
2775 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2778 cmp_length = cm_data.buf_blockSize - buf_offset;
2780 osi_assertx(qdp != NULL, "null osi_queueData_t");
2781 bufp = osi_GetQData(qdp);
2783 if (memcmp(bufferp+bytes_compared, bufp->datap+buf_offset, cmp_length) != 0)
2786 md5_match = buf_ValidateCheckSum(bufp);
2788 osi_Log5(afsd_logp, "cm_VerifyDataStore verification failed scp 0x%p bufp 0x%p offset 0x%x:%08x md5 %s",
2789 scp, bufp, bufp->offset.HighPart, bufp->offset.LowPart, md5_match ? "match" : "no-match");
2791 bytes_compared += cmp_length;
2794 osi_Log4(afsd_logp, "cm_VerifyStoreData unable to verify due to data version change scp 0x%p, off 0x%x:%08x, size 0x%x",
2795 scp, biod->offset.HighPart, biod->offset.LowPart, biod->length);