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;
58 AFSFetchStatus outStatus;
59 AFSStoreStatus inStatus;
63 struct rx_call *rxcallp;
64 struct rx_connection *rxconnp;
71 cm_bulkIO_t biod; /* bulk IO descriptor */
72 int require_64bit_ops = 0;
73 int call_was_64bit = 0;
74 int scp_locked = flags & CM_BUF_WRITE_SCP_LOCKED;
75 int storedata_excl = 0;
77 osi_assertx(userp != NULL, "null cm_user_t");
78 osi_assertx(scp != NULL, "null cm_scache_t");
80 memset(&volSync, 0, sizeof(volSync));
83 * now, the buffer may or may not be filled with good data (buf_GetNewLocked
84 * drops lots of locks, and may indeed return a properly initialized
85 * buffer, although more likely it will just return a new, empty, buffer.
88 lock_ObtainWrite(&scp->rw);
89 if (scp->flags & CM_SCACHEFLAG_DELETED) {
91 lock_ReleaseWrite(&scp->rw);
92 return CM_ERROR_NOSUCHFILE;
95 cm_AFSFidFromFid(&tfid, &scp->fid);
97 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
98 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
101 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
103 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
104 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
106 lock_ReleaseWrite(&scp->rw);
110 if (biod.length == 0) {
111 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
112 cm_ReleaseBIOD(&biod, 1, 0, 1); /* should be a NOOP */
114 goto exit_storedata_excl;
117 /* prepare the output status for the store */
118 _InterlockedOr(&scp->mask, CM_SCACHEMASK_CLIENTMODTIME);
119 cm_StatusFromAttr(&inStatus, scp, NULL);
120 truncPos = scp->length;
121 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
122 && LargeIntegerLessThan(scp->truncPos, truncPos)) {
123 truncPos = scp->truncPos;
124 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_TRUNCPOS);
127 /* compute how many bytes to write from this buffer */
128 thyper = LargeIntegerSubtract(scp->length, biod.offset);
129 if (LargeIntegerLessThanZero(thyper)) {
130 /* entire buffer is past EOF */
134 /* otherwise write out part of buffer before EOF, but not
135 * more than bufferSize bytes.
137 if (LargeIntegerGreaterThan(thyper,
138 ConvertLongToLargeInteger(biod.length))) {
139 nbytes = biod.length;
141 /* if thyper is less than or equal to biod.length, then we
142 can safely assume that the value fits in a long. */
143 nbytes = thyper.LowPart;
147 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
148 ConvertLongToLargeInteger(nbytes)),
149 ConvertLongToLargeInteger(LONG_MAX)) ||
150 LargeIntegerGreaterThan(truncPos,
151 ConvertLongToLargeInteger(LONG_MAX))) {
152 require_64bit_ops = 1;
155 InterlockedIncrement(&scp->activeRPCs);
156 lock_ReleaseWrite(&scp->rw);
158 /* now we're ready to do the store operation */
159 save_nbytes = nbytes;
161 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
166 rxconnp = cm_GetRxConn(connp);
167 rxcallp = rx_NewCall(rxconnp);
168 rx_PutConnection(rxconnp);
170 if (SERVERHAS64BIT(connp)) {
173 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
174 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
175 osi_Log2(afsd_logp, "... truncPos 0x%x:%08x", truncPos.HighPart, truncPos.LowPart);
177 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
178 biod.offset.QuadPart,
182 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
184 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
188 if (require_64bit_ops) {
189 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData. The operation requires large file support in the server.");
190 code = CM_ERROR_TOOBIG;
192 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
193 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
195 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
196 biod.offset.LowPart, nbytes, truncPos.LowPart);
198 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
200 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
205 afs_uint32 buf_offset = 0, bytes_copied = 0;
207 /* write the data from the the list of buffers */
211 struct iovec tiov[RX_MAXIOVECS];
212 afs_int32 tnio, vlen, vbytes, iov, voffset;
215 vbytes = rx_WritevAlloc(rxcallp, tiov, &tnio, RX_MAXIOVECS, nbytes);
217 code = RX_PROTOCOL_ERROR;
221 for ( iov = voffset = vlen = 0;
222 vlen < vbytes && iov < tnio; vlen += wbytes) {
224 qdp = biod.bufListEndp;
225 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
226 } else if (buf_offset == cm_data.buf_blockSize) {
227 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
231 osi_assertx(qdp != NULL, "null osi_queueData_t");
232 bufp = osi_GetQData(qdp);
233 bufferp = bufp->datap + buf_offset;
234 wbytes = vbytes - vlen;
235 if (wbytes > cm_data.buf_blockSize - buf_offset)
236 wbytes = cm_data.buf_blockSize - buf_offset;
238 vleft = tiov[iov].iov_len - voffset;
239 while (wbytes > vleft && iov < tnio) {
240 memcpy(tiov[iov].iov_base + voffset, bufferp, vleft);
241 bytes_copied += vleft;
249 vleft = tiov[iov].iov_len;
253 memcpy(tiov[iov].iov_base + voffset, bufferp, wbytes);
254 bytes_copied += wbytes;
255 if (tiov[iov].iov_len == voffset + wbytes) {
258 vleft = (iov < tnio) ? tiov[iov].iov_len : 0;
264 buf_offset += wbytes;
270 osi_assertx(iov == tnio, "incorrect iov count");
271 osi_assertx(vlen == vbytes, "bytes_copied != vbytes");
272 osi_assertx(bufp->offset.QuadPart + buf_offset == biod.offset.QuadPart + bytes_copied,
273 "begin and end offsets don't match");
275 temp = rx_Writev(rxcallp, tiov, tnio, vbytes);
276 if (temp != vbytes) {
277 osi_Log3(afsd_logp, "rx_Writev failed bp 0x%p, %d != %d", bufp, temp, vbytes);
278 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
282 osi_Log2(afsd_logp, "rx_Writev succeeded iov offset 0x%x, wrote 0x%x",
283 (unsigned long)(bufp->offset.QuadPart + buf_offset - vbytes), vbytes);
285 #else /* USE_RX_IOVEC */
287 qdp = biod.bufListEndp;
288 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
290 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
294 osi_assertx(qdp != NULL, "null osi_queueData_t");
295 bufp = osi_GetQData(qdp);
296 bufferp = bufp->datap + buf_offset;
298 if (wbytes > cm_data.buf_blockSize - buf_offset)
299 wbytes = cm_data.buf_blockSize - buf_offset;
301 /* write out wbytes of data from bufferp */
302 temp = rx_Write(rxcallp, bufferp, wbytes);
303 if (temp != wbytes) {
304 osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d", bufp, temp, wbytes);
305 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
308 osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p written %d", bufp, temp);
311 #endif /* USE_RX_IOVEC */
314 * Rx supports an out of band signalling mechanism that permits
315 * RPC specific status information to be communicated in the
316 * reverse direction of the channel. For RXAFS_StoreData, the
317 * 0-bit is set once all of the permission checks have completed
318 * and the volume/vnode locks have been obtained by the file
319 * server. The signal is intended to notify the Unix afs client
320 * that is performing store-on-close that it is safe to permit
321 * the close operation to complete while the store continues
322 * in the background. All of the callbacks have been broken
323 * and the locks will not be dropped until the RPC completes
324 * which prevents any other operation from being initiated on
325 * the vnode until the store is finished.
327 * The Windows client does not perform store-on-close. Instead
328 * it uses the CM_SCACHESYNC_STOREDATA_EXCL request flag and
329 * CM_SCACHEFLAG_DATASTORING scache state to ensure that store
330 * operations are serialized. The 0-bit signal permits the
331 * CM_SCACHEFLAG_DATASTORING state to the dropped which in
332 * turn permits another thread to prep its own BIOD in parallel.
333 * This is safe because it is impossible for that second store
334 * RPC to complete before this one does.
336 if ( storedata_excl && (rx_GetRemoteStatus(rxcallp) & 1)) {
337 lock_ObtainWrite(&scp->rw);
338 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
339 lock_ReleaseWrite(&scp->rw);
342 } /* while more bytes to write */
343 } /* if RPC started successfully */
346 if (call_was_64bit) {
347 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
349 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
351 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
353 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
355 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
357 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
361 code1 = rx_EndCall(rxcallp, code);
363 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
364 SET_SERVERHASNO64BIT(connp);
366 nbytes = save_nbytes;
369 /* Prefer rx_EndCall error over StoreData error */
371 osi_Log2(afsd_logp, "rx_EndCall converted 0x%x to 0x%x", code, code1);
374 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
376 code = cm_MapRPCError(code, reqp);
379 osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
381 osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
383 /* now, clean up our state */
384 lock_ObtainWrite(&scp->rw);
386 cm_ReleaseBIOD(&biod, 1, code, 1);
390 /* now, here's something a little tricky: in AFS 3, a dirty
391 * length can't be directly stored, instead, a dirty chunk is
392 * stored that sets the file's size (by writing and by using
393 * the truncate-first option in the store call).
395 * At this point, we've just finished a store, and so the trunc
396 * pos field is clean. If the file's size at the server is at
397 * least as big as we think it should be, then we turn off the
398 * length dirty bit, since all the other dirty buffers must
399 * precede this one in the file.
401 * The file's desired size shouldn't be smaller than what's
402 * stored at the server now, since we just did the trunc pos
405 * We have to turn off the length dirty bit as soon as we can,
406 * so that we see updates made by other machines.
409 if (call_was_64bit) {
410 t.LowPart = outStatus.Length;
411 t.HighPart = outStatus.Length_hi;
413 t = ConvertLongToLargeInteger(outStatus.Length);
416 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
417 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
419 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
421 InterlockedDecrement(&scp->activeRPCs);
422 if (code == CM_ERROR_SPACE)
423 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OUTOFSPACE);
424 else if (code == CM_ERROR_QUOTA)
425 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OVERQUOTA);
430 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
433 lock_ReleaseWrite(&scp->rw);
439 * Truncate the file, by sending a StoreData RPC with zero length.
441 * Called with scp locked. Releases and re-obtains the lock.
443 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
445 AFSFetchStatus outStatus;
446 AFSStoreStatus inStatus;
450 osi_hyper_t truncPos;
452 struct rx_call *rxcallp;
453 struct rx_connection *rxconnp;
454 int require_64bit_ops = 0;
455 int call_was_64bit = 0;
457 memset(&volSync, 0, sizeof(volSync));
459 osi_Log2(afsd_logp, "cm_StoreMini scp 0x%p userp 0x%p", scp, userp);
461 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
462 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
463 CM_SCACHESYNC_STOREDATA_EXCL);
465 /* prepare the output status for the store */
466 inStatus.Mask = AFS_SETMODTIME;
467 inStatus.ClientModTime = scp->clientModTime;
468 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_CLIENTMODTIME);
470 /* calculate truncation position */
471 truncPos = scp->length;
472 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
473 && LargeIntegerLessThan(scp->truncPos, truncPos))
474 truncPos = scp->truncPos;
475 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_TRUNCPOS);
477 if (LargeIntegerGreaterThan(truncPos,
478 ConvertLongToLargeInteger(LONG_MAX))) {
480 require_64bit_ops = 1;
483 InterlockedIncrement(&scp->activeRPCs);
484 lock_ReleaseWrite(&scp->rw);
486 cm_AFSFidFromFid(&tfid, &scp->fid);
488 /* now we're ready to do the store operation */
490 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
495 rxconnp = cm_GetRxConn(connp);
496 rxcallp = rx_NewCall(rxconnp);
497 rx_PutConnection(rxconnp);
499 if (SERVERHAS64BIT(connp)) {
502 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, truncPos 0x%x:%08x",
503 scp, truncPos.HighPart, truncPos.LowPart);
505 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
506 0, 0, truncPos.QuadPart);
508 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
510 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
514 if (require_64bit_ops) {
515 code = CM_ERROR_TOOBIG;
517 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, truncPos 0x%x:%08x",
518 scp, truncPos.HighPart, truncPos.LowPart);
520 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
521 0, 0, truncPos.LowPart);
523 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
525 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
530 if (call_was_64bit) {
531 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
533 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
535 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
537 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
539 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX", scp, code);
541 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
544 code1 = rx_EndCall(rxcallp, code);
546 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
547 SET_SERVERHASNO64BIT(connp);
551 /* prefer StoreData error over rx_EndCall error */
552 if (code == 0 && code1 != 0)
554 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
555 code = cm_MapRPCError(code, reqp);
557 /* now, clean up our state */
558 lock_ObtainWrite(&scp->rw);
563 * For explanation of handling of CM_SCACHEMASK_LENGTH,
566 if (call_was_64bit) {
567 t.HighPart = outStatus.Length_hi;
568 t.LowPart = outStatus.Length;
570 t = ConvertLongToLargeInteger(outStatus.Length);
573 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
574 _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
575 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
577 InterlockedDecrement(&scp->activeRPCs);
579 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
584 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
588 /* now return a code that means that I/O is done */
593 * stabilize scache entry with CM_SCACHESYNC_SETSIZE. This prevents any new
594 * data buffers to be allocated, new data to be fetched from the file server,
595 * and writes to be accepted from the application but permits dirty buffers
596 * to be written to the file server.
598 * Stabilize uses cm_SyncOp to maintain the cm_scache_t in this stable state
599 * instead of holding the rwlock exclusively. This permits background stores
600 * to be performed in parallel and in particular allow FlushFile to be
601 * implemented without violating the locking hierarchy.
603 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
605 cm_scache_t *scp = vscp;
608 lock_ObtainWrite(&scp->rw);
609 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
610 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
611 lock_ReleaseWrite(&scp->rw);
616 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
617 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
619 cm_scache_t *scp = vscp;
621 lock_ObtainWrite(&scp->rw);
622 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
624 lock_ReleaseWrite(&scp->rw);
626 /* always succeeds */
630 cm_buf_ops_t cm_bufOps = {
637 long cm_ValidateDCache(void)
639 return buf_ValidateBuffers();
642 long cm_ShutdownDCache(void)
647 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
649 return buf_Init(newFile, &cm_bufOps, nbuffers);
652 /* check to see if we have an up-to-date buffer. The buffer must have
653 * previously been obtained by calling buf_Get.
655 * Make sure we have a callback, and that the dataversion matches.
657 * Scp must be locked.
659 * Bufp *may* be locked.
661 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
664 if (!cm_HaveCallback(scp))
666 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
668 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
670 if (bufp->offset.QuadPart >= scp->serverLength.QuadPart)
673 code = lock_TryMutex(&bufp->mx);
675 /* don't have the lock, and can't lock it, then
682 /* remember dirty flag for later */
683 code = bufp->flags & CM_BUF_DIRTY;
685 /* release lock if we obtained it here */
687 lock_ReleaseMutex(&bufp->mx);
689 /* if buffer was dirty, buffer is acceptable for use */
697 * used when deciding whether to do a background fetch or not.
698 * call with scp->rw write-locked.
701 cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
702 cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
706 osi_hyper_t tblocksize;
711 /* now scan all buffers in the range, looking for any that look like
716 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
718 while (LargeIntegerGreaterThanZero(tlength)) {
719 /* get callback so we can do a meaningful dataVersion comparison */
720 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
721 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
725 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
726 /* we're past the end of file */
730 bp = buf_Find(&scp->fid, &tbase);
731 /* We cheat slightly by not locking the bp mutex. */
733 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
734 && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
742 /* if this buffer is essentially guaranteed to require a fetch,
743 * break out here and return this position.
748 tbase = LargeIntegerAdd(tbase, tblocksize);
749 tlength = LargeIntegerSubtract(tlength, tblocksize);
752 /* if we get here, either everything is fine or 'stop' stopped us at a
753 * particular buffer in the range that definitely needs to be fetched.
756 /* return non-zero code since realBasep won't be valid */
760 /* successfully found a page that will need fetching */
768 cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
769 cm_user_t *userp, cm_req_t *reqp)
774 afs_uint32 req_flags = reqp->flags;
776 if (scp->flags & CM_SCACHEFLAG_DELETED) {
777 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
779 /* Retries will be performed by the BkgDaemon thread if appropriate */
780 reqp->flags |= CM_REQ_NORETRY;
782 toffset.LowPart = p1;
783 toffset.HighPart = p2;
786 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
788 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, reqp);
790 osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
792 reqp->flags = req_flags;
796 * Keep the following list synchronized with the
797 * error code list in cm_BkgDaemon
800 case CM_ERROR_TIMEDOUT: /* or server restarting */
802 case CM_ERROR_WOULDBLOCK:
803 case CM_ERROR_ALLBUSY:
804 case CM_ERROR_ALLDOWN:
805 case CM_ERROR_ALLOFFLINE:
806 case CM_ERROR_PARTIALWRITE:
807 break; /* cm_BkgDaemon will re-insert the request in the queue */
810 lock_ObtainWrite(&scp->rw);
811 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
812 lock_ReleaseWrite(&scp->rw);
818 /* Called with scp locked */
819 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
824 end = LargeIntegerAdd(*base, *length);
825 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
826 scp->prefetch.base = *base;
827 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
828 scp->prefetch.end = end;
830 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
833 /* do the prefetch. if the prefetch fails, return 0 (success)
834 * because there is no harm done. */
836 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
837 cm_user_t *userp, cm_req_t *reqp)
844 osi_hyper_t tblocksize;
848 afs_uint32 req_flags;
850 /* Retries will be performed by the BkgDaemon thread if appropriate */
851 req_flags = reqp->flags;
852 reqp->flags |= CM_REQ_NORETRY;
855 fetched.HighPart = 0;
856 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
860 length.HighPart = p4;
862 end = LargeIntegerAdd(base, length);
864 osi_Log5(afsd_logp, "Starting BKG prefetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
865 scp, p2, p1, p4, p3);
867 for ( code = 0, offset = base;
868 code == 0 && LargeIntegerLessThan(offset, end);
869 offset = LargeIntegerAdd(offset, tblocksize) )
872 lock_ReleaseWrite(&scp->rw);
876 code = buf_Get(scp, &offset, reqp, &bp);
880 if (bp->cmFlags & CM_BUF_CMFETCHING) {
881 /* skip this buffer as another thread is already fetching it */
883 lock_ObtainWrite(&scp->rw);
892 lock_ObtainWrite(&scp->rw);
896 code = cm_GetBuffer(scp, bp, NULL, userp, reqp);
898 fetched = LargeIntegerAdd(fetched, tblocksize);
903 lock_ObtainWrite(&scp->rw);
907 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
908 scp, &base, &fetched);
910 /* wakeup anyone who is waiting */
911 if (scp->flags & CM_SCACHEFLAG_WAITING) {
912 osi_Log1(afsd_logp, "CM BkgPrefetch Waking scp 0x%p", scp);
913 osi_Wakeup((LONG_PTR) &scp->flags);
915 lock_ReleaseWrite(&scp->rw);
917 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p code 0x%x fetched 0x%x:%x",
918 scp, code, fetched.HighPart, fetched.LowPart);
920 reqp->flags = req_flags;
924 /* a read was issued to offsetp, and we have to determine whether we should
925 * do a prefetch of the next chunk.
927 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
928 cm_user_t *userp, cm_req_t *reqp)
932 osi_hyper_t realBase;
933 osi_hyper_t readBase;
934 osi_hyper_t readLength;
936 osi_hyper_t tblocksize; /* a long long temp variable */
938 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
941 /* round up to chunk boundary */
942 readBase.LowPart += (cm_chunkSize-1);
943 readBase.LowPart &= (-cm_chunkSize);
945 readLength = ConvertLongToLargeInteger(count);
947 lock_ObtainWrite(&scp->rw);
949 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
950 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
951 lock_ReleaseWrite(&scp->rw);
954 _InterlockedOr(&scp->flags, CM_SCACHEFLAG_PREFETCHING);
956 /* start the scan at the latter of the end of this read or
957 * the end of the last fetched region.
959 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
960 readBase = scp->prefetch.end;
962 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
965 _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_PREFETCHING);
966 lock_ReleaseWrite(&scp->rw);
967 return; /* can't find something to prefetch */
970 readEnd = LargeIntegerAdd(realBase, readLength);
973 lock_ReleaseWrite(&scp->rw);
975 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
976 scp, realBase.LowPart);
978 cm_QueueBKGRequest(scp, cm_BkgPrefetch,
979 realBase.LowPart, realBase.HighPart,
980 readLength.LowPart, readLength.HighPart,
984 /* scp must be locked; temporarily unlocked during processing.
985 * If returns 0, returns buffers held in biop, and with
986 * CM_BUF_CMSTORING set.
988 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
989 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
990 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
991 * must be woken, and the event must be set when the I/O is done. All of this
992 * is required so that buf_WaitIO synchronizes properly with the buffer as it
993 * is being written out.
995 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
996 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
999 osi_queueData_t *qdp;
1002 osi_hyper_t scanStart; /* where to start scan for dirty pages */
1003 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
1004 osi_hyper_t firstModOffset; /* offset of first modified page in range */
1007 long flags; /* flags to cm_SyncOp */
1009 /* clear things out */
1010 biop->scp = scp; /* do not hold; held by caller */
1011 biop->userp = userp; /* do not hold; held by caller */
1012 biop->offset = *inOffsetp;
1014 biop->bufListp = NULL;
1015 biop->bufListEndp = NULL;
1018 /* reserve a chunk's worth of buffers */
1019 lock_ReleaseWrite(&scp->rw);
1020 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1021 lock_ObtainWrite(&scp->rw);
1024 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
1025 thyper = ConvertLongToLargeInteger(temp);
1026 tbase = LargeIntegerAdd(*inOffsetp, thyper);
1028 bufp = buf_Find(&scp->fid, &tbase);
1030 /* get buffer mutex and scp mutex safely */
1031 lock_ReleaseWrite(&scp->rw);
1032 lock_ObtainMutex(&bufp->mx);
1035 * if the buffer is actively involved in I/O
1036 * we wait for the I/O to complete.
1038 if (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING))
1039 buf_WaitIO(scp, bufp);
1041 lock_ObtainWrite(&scp->rw);
1042 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
1043 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1045 lock_ReleaseMutex(&bufp->mx);
1048 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1052 /* if the buffer is dirty, we're done */
1053 if (bufp->flags & CM_BUF_DIRTY) {
1054 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
1055 "WRITING w/o CMSTORING in SetupStoreBIOD");
1056 _InterlockedOr(&bufp->flags, CM_BUF_WRITING);
1060 /* this buffer is clean, so there's no reason to process it */
1061 cm_SyncOpDone(scp, bufp, flags);
1062 lock_ReleaseMutex(&bufp->mx);
1070 /* if we get here, if bufp is null, we didn't find any dirty buffers
1071 * that weren't already being stored back, so we just quit now.
1077 /* don't need buffer mutex any more */
1078 lock_ReleaseMutex(&bufp->mx);
1080 /* put this element in the list */
1081 qdp = osi_QDAlloc();
1082 osi_SetQData(qdp, bufp);
1083 /* don't have to hold bufp, since held by buf_Find above */
1084 osi_QAddH((osi_queue_t **) &biop->bufListp,
1085 (osi_queue_t **) &biop->bufListEndp,
1087 biop->length = cm_data.buf_blockSize;
1088 firstModOffset = bufp->offset;
1089 biop->offset = firstModOffset;
1090 bufp = NULL; /* this buffer and reference added to the queue */
1092 /* compute the window surrounding firstModOffset of size cm_chunkSize */
1093 scanStart = firstModOffset;
1094 scanStart.LowPart &= (-cm_chunkSize);
1095 thyper = ConvertLongToLargeInteger(cm_chunkSize);
1096 scanEnd = LargeIntegerAdd(scanStart, thyper);
1098 flags = CM_SCACHESYNC_GETSTATUS
1099 | CM_SCACHESYNC_STOREDATA
1100 | CM_SCACHESYNC_BUFLOCKED
1101 | CM_SCACHESYNC_NOWAIT;
1103 /* start by looking backwards until scanStart */
1104 /* hyper version of cm_data.buf_blockSize */
1105 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1106 tbase = LargeIntegerSubtract(firstModOffset, thyper);
1107 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
1108 /* see if we can find the buffer */
1109 bufp = buf_Find(&scp->fid, &tbase);
1113 /* try to lock it, and quit if we can't (simplifies locking) */
1114 lock_ReleaseWrite(&scp->rw);
1115 code = lock_TryMutex(&bufp->mx);
1116 lock_ObtainWrite(&scp->rw);
1123 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1125 lock_ReleaseMutex(&bufp->mx);
1131 if (!(bufp->flags & CM_BUF_DIRTY)) {
1132 /* buffer is clean, so we shouldn't add it */
1133 cm_SyncOpDone(scp, bufp, flags);
1134 lock_ReleaseMutex(&bufp->mx);
1140 /* don't need buffer mutex any more */
1141 lock_ReleaseMutex(&bufp->mx);
1143 /* we have a dirty buffer ready for storing. Add it to the tail
1144 * of the list, since it immediately precedes all of the disk
1145 * addresses we've already collected.
1147 qdp = osi_QDAlloc();
1148 osi_SetQData(qdp, bufp);
1149 /* no buf_hold necessary, since we have it held from buf_Find */
1150 osi_QAddT((osi_queue_t **) &biop->bufListp,
1151 (osi_queue_t **) &biop->bufListEndp,
1153 bufp = NULL; /* added to the queue */
1155 /* update biod info describing the transfer */
1156 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
1157 biop->length += cm_data.buf_blockSize;
1159 /* update loop pointer */
1160 tbase = LargeIntegerSubtract(tbase, thyper);
1161 } /* while loop looking for pages preceding the one we found */
1163 /* now, find later dirty, contiguous pages, and add them to the list */
1164 /* hyper version of cm_data.buf_blockSize */
1165 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1166 tbase = LargeIntegerAdd(firstModOffset, thyper);
1167 while(LargeIntegerLessThan(tbase, scanEnd)) {
1168 /* see if we can find the buffer */
1169 bufp = buf_Find(&scp->fid, &tbase);
1173 /* try to lock it, and quit if we can't (simplifies locking) */
1174 lock_ReleaseWrite(&scp->rw);
1175 code = lock_TryMutex(&bufp->mx);
1176 lock_ObtainWrite(&scp->rw);
1183 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1185 lock_ReleaseMutex(&bufp->mx);
1191 if (!(bufp->flags & CM_BUF_DIRTY)) {
1192 /* buffer is clean, so we shouldn't add it */
1193 cm_SyncOpDone(scp, bufp, flags);
1194 lock_ReleaseMutex(&bufp->mx);
1200 /* don't need buffer mutex any more */
1201 lock_ReleaseMutex(&bufp->mx);
1203 /* we have a dirty buffer ready for storing. Add it to the head
1204 * of the list, since it immediately follows all of the disk
1205 * addresses we've already collected.
1207 qdp = osi_QDAlloc();
1208 osi_SetQData(qdp, bufp);
1209 /* no buf_hold necessary, since we have it held from buf_Find */
1210 osi_QAddH((osi_queue_t **) &biop->bufListp,
1211 (osi_queue_t **) &biop->bufListEndp,
1215 /* update biod info describing the transfer */
1216 biop->length += cm_data.buf_blockSize;
1218 /* update loop pointer */
1219 tbase = LargeIntegerAdd(tbase, thyper);
1220 } /* while loop looking for pages following the first page we found */
1222 /* finally, we're done */
1226 /* scp must be locked; temporarily unlocked during processing.
1227 * If returns 0, returns buffers held in biop, and with
1228 * CM_BUF_CMFETCHING flags set.
1229 * If an error is returned, we don't return any buffers.
1231 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1232 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1236 osi_hyper_t tblocksize; /* a long long temp variable */
1237 osi_hyper_t pageBase; /* base offset we're looking at */
1238 osi_queueData_t *qdp; /* one temp queue structure */
1239 osi_queueData_t *tqdp; /* another temp queue structure */
1240 long collected; /* how many bytes have been collected */
1243 osi_hyper_t fileSize; /* the # of bytes in the file */
1244 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1245 osi_queueData_t *heldBufListEndp; /* first one */
1248 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1250 biop->scp = scp; /* do not hold; held by caller */
1251 biop->userp = userp; /* do not hold; held by caller */
1253 biop->offset = *offsetp;
1254 /* null out the list of buffers */
1255 biop->bufListp = biop->bufListEndp = NULL;
1258 /* first lookup the file's length, so we know when to stop */
1259 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1260 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1264 /* copy out size, since it may change */
1265 fileSize = scp->serverLength;
1267 lock_ReleaseWrite(&scp->rw);
1269 pageBase = *offsetp;
1270 collected = pageBase.LowPart & (cm_chunkSize - 1);
1271 heldBufListp = NULL;
1272 heldBufListEndp = NULL;
1275 * Obtaining buffers can cause dirty buffers to be recycled, which
1276 * can cause a storeback, so cannot be done while we have buffers
1279 * To get around this, we get buffers twice. Before reserving buffers,
1280 * we obtain and release each one individually. After reserving
1281 * buffers, we try to obtain them again, but only by lookup, not by
1282 * recycling. If a buffer has gone away while we were waiting for
1283 * the others, we just use whatever buffers we already have.
1285 * On entry to this function, we are already holding a buffer, so we
1286 * can't wait for reservation. So we call buf_TryReserveBuffers()
1287 * instead. Not only that, we can't really even call buf_Get(), for
1288 * the same reason. We can't avoid that, though. To avoid deadlock
1289 * we allow only one thread to be executing the buf_Get()-buf_Release()
1290 * sequence at a time.
1293 /* first hold all buffers, since we can't hold any locks in buf_Get */
1295 /* stop at chunk boundary */
1296 if (collected >= cm_chunkSize)
1299 /* see if the next page would be past EOF */
1300 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1303 code = buf_Get(scp, &pageBase, reqp, &tbp);
1305 lock_ObtainWrite(&scp->rw);
1306 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1313 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1314 collected += cm_data.buf_blockSize;
1317 /* reserve a chunk's worth of buffers if possible */
1318 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1320 pageBase = *offsetp;
1321 collected = pageBase.LowPart & (cm_chunkSize - 1);
1323 /* now hold all buffers, if they are still there */
1325 /* stop at chunk boundary */
1326 if (collected >= cm_chunkSize)
1329 /* see if the next page would be past EOF */
1330 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1333 tbp = buf_Find(&scp->fid, &pageBase);
1337 /* add the buffer to the list */
1338 qdp = osi_QDAlloc();
1339 osi_SetQData(qdp, tbp);
1340 osi_QAddH((osi_queue_t **)&heldBufListp,
1341 (osi_queue_t **)&heldBufListEndp,
1343 /* leave tbp held (from buf_Get) */
1348 collected += cm_data.buf_blockSize;
1349 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1352 /* look at each buffer, adding it into the list if it looks idle and
1353 * filled with old data. One special case: wait for idle if it is the
1354 * first buffer since we really need that one for our caller to make
1358 collected = 0; /* now count how many we'll really use */
1359 for (tqdp = heldBufListEndp;
1361 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1362 /* get a ptr to the held buffer */
1363 tbp = osi_GetQData(tqdp);
1364 pageBase = tbp->offset;
1366 /* now lock the buffer lock */
1367 lock_ObtainMutex(&tbp->mx);
1368 lock_ObtainWrite(&scp->rw);
1370 /* don't bother fetching over data that is already current */
1371 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1372 /* we don't need this buffer, since it is current */
1373 lock_ReleaseWrite(&scp->rw);
1374 lock_ReleaseMutex(&tbp->mx);
1378 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1380 flags |= CM_SCACHESYNC_NOWAIT;
1382 /* wait for the buffer to serialize, if required. Doesn't
1383 * release the scp or buffer lock(s) if NOWAIT is specified.
1385 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1387 lock_ReleaseWrite(&scp->rw);
1388 lock_ReleaseMutex(&tbp->mx);
1392 /* don't fetch over dirty buffers */
1393 if (tbp->flags & CM_BUF_DIRTY) {
1394 cm_SyncOpDone(scp, tbp, flags);
1395 lock_ReleaseWrite(&scp->rw);
1396 lock_ReleaseMutex(&tbp->mx);
1401 lock_ReleaseWrite(&scp->rw);
1402 lock_ReleaseMutex(&tbp->mx);
1404 /* add the buffer to the list */
1405 qdp = osi_QDAlloc();
1406 osi_SetQData(qdp, tbp);
1407 osi_QAddH((osi_queue_t **)&biop->bufListp,
1408 (osi_queue_t **)&biop->bufListEndp,
1412 /* from now on, a failure just stops our collection process, but
1413 * we still do the I/O to whatever we've already managed to collect.
1416 collected += cm_data.buf_blockSize;
1419 /* now, we've held in biop->bufListp all the buffer's we're really
1420 * interested in. We also have holds left from heldBufListp, and we
1421 * now release those holds on the buffers.
1423 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1424 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1425 tbp = osi_GetQData(qdp);
1426 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1427 (osi_queue_t **) &heldBufListEndp,
1434 /* Caller expects this */
1435 lock_ObtainWrite(&scp->rw);
1437 /* if we got a failure setting up the first buffer, then we don't have
1438 * any side effects yet, and we also have failed an operation that the
1439 * caller requires to make any progress. Give up now.
1441 if (code && isFirst) {
1442 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1446 /* otherwise, we're still OK, and should just return the I/O setup we've
1449 biop->length = collected;
1450 biop->reserved = reserving;
1454 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1457 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
1459 cm_scache_t *scp; /* do not release; not held in biop */
1461 osi_queueData_t *qdp;
1462 osi_queueData_t *nqdp;
1464 int reportErrorToRedir = 0;
1466 /* Give back reserved buffers */
1468 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1471 flags = CM_SCACHESYNC_STOREDATA;
1473 flags = CM_SCACHESYNC_FETCHDATA;
1476 if (biop->bufListp) {
1477 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1478 /* lookup next guy first, since we're going to free this one */
1479 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1481 /* extract buffer and free queue data */
1482 bufp = osi_GetQData(qdp);
1483 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1484 (osi_queue_t **) &biop->bufListEndp,
1488 /* now, mark I/O as done, unlock the buffer and release it */
1490 lock_ReleaseWrite(&scp->rw);
1491 lock_ObtainMutex(&bufp->mx);
1492 lock_ObtainWrite(&scp->rw);
1493 cm_SyncOpDone(scp, bufp, flags);
1495 /* turn off writing and wakeup users */
1497 if (bufp->flags & CM_BUF_WAITING) {
1498 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1499 osi_Wakeup((LONG_PTR) bufp);
1502 _InterlockedAnd(&bufp->flags, ~CM_BUF_WRITING);
1504 case CM_ERROR_NOSUCHFILE:
1505 case CM_ERROR_BADFD:
1506 case CM_ERROR_NOACCESS:
1507 case CM_ERROR_QUOTA:
1508 case CM_ERROR_SPACE:
1509 case CM_ERROR_TOOBIG:
1510 case CM_ERROR_READONLY:
1511 case CM_ERROR_NOSUCHPATH:
1513 * Apply the fatal error to this buffer.
1515 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
1516 _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
1517 bufp->dirty_offset = 0;
1518 bufp->dirty_length = 0;
1520 bufp->dataVersion = CM_BUF_VERSION_BAD;
1521 bufp->dirtyCounter++;
1522 reportErrorToRedir = 1;
1524 case CM_ERROR_TIMEDOUT:
1525 case CM_ERROR_ALLDOWN:
1526 case CM_ERROR_ALLBUSY:
1527 case CM_ERROR_ALLOFFLINE:
1528 case CM_ERROR_CLOCKSKEW:
1530 /* do not mark the buffer in error state but do
1531 * not attempt to complete the rest either.
1536 _InterlockedAnd(&bufp->flags, ~(CM_BUF_WRITING | CM_BUF_DIRTY));
1537 bufp->dirty_offset = bufp->dirty_length = 0;
1542 lock_ReleaseWrite(&scp->rw);
1543 lock_ReleaseMutex(&bufp->mx);
1548 if (RDR_Initialized && reportErrorToRedir) {
1550 smb_MapNTError(cm_MapRPCError(code, biop->reqp), &status, TRUE);
1551 RDR_SetFileStatus( &scp->fid, &biop->userp->authgroup, status);
1555 lock_ObtainWrite(&scp->rw);
1556 cm_SyncOpDone(scp, NULL, flags);
1558 lock_ReleaseWrite(&scp->rw);
1561 /* clean things out */
1562 biop->bufListp = NULL;
1563 biop->bufListEndp = NULL;
1567 cm_CloneStatus(cm_scache_t *scp, cm_user_t *userp, int scp_locked,
1568 AFSFetchStatus *afsStatusp, AFSVolSync *volSyncp)
1570 // setup the status based upon the scp data
1571 afsStatusp->InterfaceVersion = 0x1;
1572 switch (scp->fileType) {
1573 case CM_SCACHETYPE_FILE:
1574 afsStatusp->FileType = File;
1576 case CM_SCACHETYPE_DIRECTORY:
1577 afsStatusp->FileType = Directory;
1579 case CM_SCACHETYPE_MOUNTPOINT:
1580 afsStatusp->FileType = SymbolicLink;
1582 case CM_SCACHETYPE_SYMLINK:
1583 case CM_SCACHETYPE_DFSLINK:
1584 afsStatusp->FileType = SymbolicLink;
1587 afsStatusp->FileType = -1; /* an invalid value */
1589 afsStatusp->LinkCount = scp->linkCount;
1590 afsStatusp->Length = scp->length.LowPart;
1591 afsStatusp->DataVersion = (afs_uint32)(scp->dataVersion & MAX_AFS_UINT32);
1592 afsStatusp->Author = 0x1;
1593 afsStatusp->Owner = scp->owner;
1595 lock_ObtainWrite(&scp->rw);
1598 if (cm_FindACLCache(scp, userp, &afsStatusp->CallerAccess))
1599 afsStatusp->CallerAccess = scp->anyAccess;
1600 afsStatusp->AnonymousAccess = scp->anyAccess;
1601 afsStatusp->UnixModeBits = scp->unixModeBits;
1602 afsStatusp->ParentVnode = scp->parentVnode;
1603 afsStatusp->ParentUnique = scp->parentUnique;
1604 afsStatusp->ResidencyMask = 0;
1605 afsStatusp->ClientModTime = scp->clientModTime;
1606 afsStatusp->ServerModTime = scp->serverModTime;
1607 afsStatusp->Group = scp->group;
1608 afsStatusp->SyncCounter = 0;
1609 afsStatusp->dataVersionHigh = (afs_uint32)(scp->dataVersion >> 32);
1610 afsStatusp->lockCount = 0;
1611 afsStatusp->Length_hi = scp->length.HighPart;
1612 afsStatusp->errorCode = 0;
1614 volSyncp->spare1 = scp->volumeCreationDate;
1619 /* Fetch a buffer. Called with scp locked.
1620 * The scp is locked on return.
1622 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1625 long code=0, code1=0;
1626 afs_uint32 nbytes; /* bytes in transfer */
1627 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1628 afs_uint64 length_found = 0;
1629 long rbytes; /* bytes in rx_Read call */
1631 AFSFetchStatus afsStatus;
1632 AFSCallBack callback;
1635 afs_uint32 buffer_offset;
1636 cm_buf_t *tbufp; /* buf we're filling */
1637 osi_queueData_t *qdp; /* q element we're scanning */
1639 struct rx_call *rxcallp;
1640 struct rx_connection *rxconnp;
1641 cm_bulkIO_t biod; /* bulk IO descriptor */
1645 int require_64bit_ops = 0;
1646 int call_was_64bit = 0;
1647 int fs_fetchdata_offset_bug = 0;
1651 memset(&volSync, 0, sizeof(volSync));
1653 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
1654 * drops lots of locks, and may indeed return a properly initialized
1655 * buffer, although more likely it will just return a new, empty, buffer.
1658 #ifdef AFS_FREELANCE_CLIENT
1660 // yj: if they're trying to get the /afs directory, we need to
1661 // handle it differently, since it's local rather than on any
1664 getroot = (scp==cm_data.rootSCachep);
1666 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1669 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1670 /* We already have this buffer don't do extra work */
1674 cm_AFSFidFromFid(&tfid, &scp->fid);
1676 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1678 /* couldn't even get the first page setup properly */
1679 osi_Log1(afsd_logp, "GetBuffer: SetupFetchBIOD failure code %d", code);
1683 /* once we get here, we have the callback in place, we know that no one
1684 * is fetching the data now. Check one last time that we still have
1685 * the wrong data, and then fetch it if we're still wrong.
1687 * We can lose a race condition and end up with biod.length zero, in
1688 * which case we just retry.
1690 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
1691 if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow) &&
1692 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength))
1694 osi_Log4(afsd_logp, "Bad DVs 0x%x != (0x%x -> 0x%x) or length 0x%x",
1695 bufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion, biod.length);
1697 if (bufp->dataVersion == CM_BUF_VERSION_BAD)
1698 memset(bufp->datap, 0, cm_data.buf_blockSize);
1699 bufp->dataVersion = scp->dataVersion;
1701 cm_ReleaseBIOD(&biod, 0, 0, 1);
1703 } else if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow)
1704 && (scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1705 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->truncPos)) {
1706 memset(bufp->datap, 0, cm_data.buf_blockSize);
1707 bufp->dataVersion = scp->dataVersion;
1708 cm_ReleaseBIOD(&biod, 0, 0, 1);
1712 InterlockedIncrement(&scp->activeRPCs);
1713 lock_ReleaseWrite(&scp->rw);
1716 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1717 ConvertLongToLargeInteger(biod.length)),
1718 ConvertLongToLargeInteger(LONG_MAX))) {
1719 require_64bit_ops = 1;
1722 osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1723 osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1724 scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1726 #ifdef AFS_FREELANCE_CLIENT
1729 // if getroot then we don't need to make any calls
1730 // just return fake data
1732 if (cm_freelanceEnabled && getroot) {
1733 // setup the fake status
1734 afsStatus.InterfaceVersion = 0x1;
1735 afsStatus.FileType = 0x2;
1736 afsStatus.LinkCount = scp->linkCount;
1737 afsStatus.Length = cm_fakeDirSize;
1738 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1739 afsStatus.Author = 0x1;
1740 afsStatus.Owner = 0x0;
1741 afsStatus.CallerAccess = 0x9;
1742 afsStatus.AnonymousAccess = 0x9;
1743 afsStatus.UnixModeBits = 0x1ff;
1744 afsStatus.ParentVnode = 0x1;
1745 afsStatus.ParentUnique = 0x1;
1746 afsStatus.ResidencyMask = 0;
1747 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1748 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1749 afsStatus.Group = 0;
1750 afsStatus.SyncCounter = 0;
1751 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1752 afsStatus.lockCount = 0;
1753 afsStatus.Length_hi = 0;
1754 afsStatus.errorCode = 0;
1755 memset(&volSync, 0, sizeof(volSync));
1757 // once we're done setting up the status info,
1758 // we just fill the buffer pages with fakedata
1759 // from cm_FakeRootDir. Extra pages are set to
1762 lock_ObtainMutex(&cm_Freelance_Lock);
1763 t1 = bufp->offset.LowPart;
1764 qdp = biod.bufListEndp;
1766 tbufp = osi_GetQData(qdp);
1767 bufferp=tbufp->datap;
1768 memset(bufferp, 0, cm_data.buf_blockSize);
1769 t2 = cm_fakeDirSize - t1;
1770 if (t2> (afs_int32)cm_data.buf_blockSize)
1771 t2=cm_data.buf_blockSize;
1773 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1778 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1781 lock_ReleaseMutex(&cm_Freelance_Lock);
1783 // once we're done, we skip over the part of the
1784 // code that does the ACTUAL fetching of data for
1787 goto fetchingcompleted;
1790 #endif /* AFS_FREELANCE_CLIENT */
1793 * if the requested offset is greater than the file length,
1794 * the file server will return zero bytes of data and the
1795 * current status for the file which we already have since
1796 * we have just obtained a callback. Instead, we can avoid
1797 * the network round trip by allocating zeroed buffers and
1798 * faking the status info.
1800 if (biod.offset.QuadPart >= scp->length.QuadPart) {
1801 osi_Log5(afsd_logp, "SKIP FetchData64 scp 0x%p, off 0x%x:%08x > length 0x%x:%08x",
1802 scp, biod.offset.HighPart, biod.offset.LowPart,
1803 scp->length.HighPart, scp->length.LowPart);
1805 /* Clone the current status info */
1806 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
1808 /* status info complete, fill pages with zeros */
1809 for (qdp = biod.bufListEndp;
1811 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1812 tbufp = osi_GetQData(qdp);
1813 bufferp=tbufp->datap;
1814 memset(bufferp, 0, cm_data.buf_blockSize);
1817 /* no need to contact the file server */
1818 goto fetchingcompleted;
1822 lock_ReleaseWrite(&scp->rw);
1826 /* now make the call */
1828 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1832 rxconnp = cm_GetRxConn(connp);
1833 rxcallp = rx_NewCall(rxconnp);
1834 rx_PutConnection(rxconnp);
1836 nbytes = nbytes_hi = 0;
1838 if (SERVERHAS64BIT(connp)) {
1841 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1842 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1844 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1847 temp = rx_Read32(rxcallp, &nbytes_hi);
1848 if (temp == sizeof(afs_int32)) {
1849 nbytes_hi = ntohl(nbytes_hi);
1852 code = rx_Error(rxcallp);
1853 code1 = rx_EndCall(rxcallp, code);
1861 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1862 if (require_64bit_ops) {
1863 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1864 code = CM_ERROR_TOOBIG;
1867 rxconnp = cm_GetRxConn(connp);
1868 rxcallp = rx_NewCall(rxconnp);
1869 rx_PutConnection(rxconnp);
1872 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1873 scp, biod.offset.LowPart, biod.length);
1875 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1878 SET_SERVERHASNO64BIT(connp);
1883 temp = rx_Read32(rxcallp, &nbytes);
1884 if (temp == sizeof(afs_int32)) {
1885 nbytes = ntohl(nbytes);
1886 FillInt64(length_found, nbytes_hi, nbytes);
1887 if (length_found > biod.length) {
1889 * prior to 1.4.12 and 1.5.65 the file server would return
1890 * (filesize - offset) if the requested offset was greater than
1891 * the filesize. The correct return value would have been zero.
1892 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
1893 * is a race between two RPCs issues by this cache manager, the
1894 * correct thing will happen the second time.
1896 osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1897 fs_fetchdata_offset_bug = 1;
1900 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1901 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
1904 /* for the moment, nbytes_hi will always be 0 if code == 0
1905 because biod.length is a 32-bit quantity. */
1908 qdp = biod.bufListEndp;
1910 tbufp = osi_GetQData(qdp);
1911 bufferp = tbufp->datap;
1917 /* fill length_found of data from the pipe into the pages.
1918 * When we stop, qdp will point at the last page we're
1919 * dealing with, and bufferp will tell us where we
1920 * stopped. We'll need this info below when we clear
1921 * the remainder of the last page out (and potentially
1922 * clear later pages out, if we fetch past EOF).
1924 while (length_found > 0) {
1926 struct iovec tiov[RX_MAXIOVECS];
1927 afs_int32 tnio, iov, iov_offset;
1929 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
1930 osi_Log1(afsd_logp, "cm_GetBuffer rx_Readv returns %d", temp);
1931 if (temp != length_found && temp < cm_data.buf_blockSize) {
1933 * If the file server returned (filesize - offset),
1934 * then the first rx_Read will return zero octets of data.
1935 * If it does, do not treat it as an error. Correct the
1936 * length_found and continue as if the file server said
1937 * it was sending us zero octets of data.
1939 if (fs_fetchdata_offset_bug && first_read)
1942 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
1950 while (rbytes > 0) {
1953 osi_assertx(bufferp != NULL, "null cm_buf_t");
1955 len = min(tiov[iov].iov_len - iov_offset, cm_data.buf_blockSize - buffer_offset);
1956 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
1958 buffer_offset += len;
1961 if (iov_offset == tiov[iov].iov_len) {
1966 if (buffer_offset == cm_data.buf_blockSize) {
1967 /* allow read-while-fetching.
1968 * if this is the last buffer, clear the
1969 * PREFETCHING flag, so the reader waiting for
1970 * this buffer will start a prefetch.
1972 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
1973 lock_ObtainWrite(&scp->rw);
1974 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1975 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
1976 osi_Wakeup((LONG_PTR) &scp->flags);
1978 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1979 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
1981 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
1983 lock_ReleaseWrite(&scp->rw);
1985 /* Advance the buffer */
1986 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1988 tbufp = osi_GetQData(qdp);
1989 bufferp = tbufp->datap;
1997 length_found -= temp;
1998 #else /* USE_RX_IOVEC */
1999 /* assert that there are still more buffers;
2000 * our check above for length_found being less than
2001 * biod.length should ensure this.
2003 osi_assertx(bufferp != NULL, "null cm_buf_t");
2005 /* read rbytes of data */
2006 rbytes = (afs_uint32)(length_found > cm_data.buf_blockSize ? cm_data.buf_blockSize : length_found);
2007 temp = rx_Read(rxcallp, bufferp, rbytes);
2008 if (temp < rbytes) {
2010 * If the file server returned (filesize - offset),
2011 * then the first rx_Read will return zero octets of data.
2012 * If it does, do not treat it as an error. Correct the
2013 * length_found and continue as if the file server said
2014 * it was sending us zero octets of data.
2016 if (fs_fetchdata_offset_bug && first_read)
2019 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2024 /* allow read-while-fetching.
2025 * if this is the last buffer, clear the
2026 * PREFETCHING flag, so the reader waiting for
2027 * this buffer will start a prefetch.
2029 _InterlockedOr(&tbufp->cmFlags, CM_BUF_CMFULLYFETCHED);
2030 lock_ObtainWrite(&scp->rw);
2031 if (scp->flags & CM_SCACHEFLAG_WAITING) {
2032 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
2033 osi_Wakeup((LONG_PTR) &scp->flags);
2035 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
2036 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
2038 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
2040 lock_ReleaseWrite(&scp->rw);
2042 /* and adjust counters */
2043 length_found -= temp;
2045 /* and move to the next buffer */
2046 if (length_found != 0) {
2047 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2049 tbufp = osi_GetQData(qdp);
2050 bufferp = tbufp->datap;
2056 #endif /* USE_RX_IOVEC */
2059 /* zero out remainder of last pages, in case we are
2060 * fetching past EOF. We were fetching an integral #
2061 * of pages, but stopped, potentially in the middle of
2062 * a page. Zero the remainder of that page, and then
2063 * all of the rest of the pages.
2066 rbytes = cm_data.buf_blockSize - buffer_offset;
2067 bufferp = tbufp->datap + buffer_offset;
2068 #else /* USE_RX_IOVEC */
2070 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
2071 rbytes = (long) (bufferp - tbufp->datap);
2073 /* bytes left to zero */
2074 rbytes = cm_data.buf_blockSize - rbytes;
2075 #endif /* USE_RX_IOVEC */
2078 memset(bufferp, 0, rbytes);
2079 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2082 tbufp = osi_GetQData(qdp);
2083 bufferp = tbufp->datap;
2084 /* bytes to clear in this page */
2085 rbytes = cm_data.buf_blockSize;
2091 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2093 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2096 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2098 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2102 code1 = rx_EndCall(rxcallp, code);
2104 if (code1 == RXKADUNKNOWNKEY)
2105 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2107 /* If we are avoiding a file server bug, ignore the error state */
2108 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2109 /* Clone the current status info and clear the error state */
2110 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2112 lock_ReleaseWrite(&scp->rw);
2116 /* Prefer the error value from FetchData over rx_EndCall */
2117 } else if (code == 0 && code1 != 0)
2119 osi_Log0(afsd_logp, "CALL FetchData DONE");
2121 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
2124 code = cm_MapRPCError(code, reqp);
2127 lock_ObtainWrite(&scp->rw);
2129 /* we know that no one else has changed the buffer, since we still have
2130 * the fetching flag on the buffers, and we have the scp locked again.
2131 * Copy in the version # into the buffer if we got code 0 back from the
2135 for(qdp = biod.bufListp;
2137 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
2138 tbufp = osi_GetQData(qdp);
2139 tbufp->dataVersion = afsStatus.dataVersionHigh;
2140 tbufp->dataVersion <<= 32;
2141 tbufp->dataVersion |= afsStatus.DataVersion;
2144 /* write buffer out to disk cache */
2145 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
2146 tbufp->dataVersion);
2147 #endif /* DISKCACHE95 */
2151 /* release scatter/gather I/O structure (buffers, locks) */
2152 cm_ReleaseBIOD(&biod, 0, code, 1);
2155 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2157 InterlockedDecrement(&scp->activeRPCs);
2163 * Similar to cm_GetBuffer but doesn't use an allocated cm_buf_t object.
2164 * Instead the data is read from the file server and copied directly into
2165 * a provided buffer. Called with scp locked. The scp is locked on return.
2167 long cm_GetData(cm_scache_t *scp, osi_hyper_t *offsetp, char *datap, int data_length,
2168 cm_user_t *userp, cm_req_t *reqp)
2170 long code=0, code1=0;
2171 afs_uint32 nbytes; /* bytes in transfer */
2172 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2173 afs_uint64 length_found = 0;
2174 char *bufferp = datap;
2175 afs_uint32 buffer_offset = 0;
2176 long rbytes; /* bytes in rx_Read call */
2178 AFSFetchStatus afsStatus;
2179 AFSCallBack callback;
2182 struct rx_call *rxcallp;
2183 struct rx_connection *rxconnp;
2187 int require_64bit_ops = 0;
2188 int call_was_64bit = 0;
2189 int fs_fetchdata_offset_bug = 0;
2193 memset(&volSync, 0, sizeof(volSync));
2195 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
2196 * drops lots of locks, and may indeed return a properly initialized
2197 * buffer, although more likely it will just return a new, empty, buffer.
2200 #ifdef AFS_FREELANCE_CLIENT
2202 // yj: if they're trying to get the /afs directory, we need to
2203 // handle it differently, since it's local rather than on any
2206 getroot = (scp==cm_data.rootSCachep);
2208 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
2211 cm_AFSFidFromFid(&tfid, &scp->fid);
2213 if (LargeIntegerGreaterThan(LargeIntegerAdd(*offsetp,
2214 ConvertLongToLargeInteger(data_length)),
2215 ConvertLongToLargeInteger(LONG_MAX))) {
2216 require_64bit_ops = 1;
2219 InterlockedIncrement(&scp->activeRPCs);
2220 osi_Log2(afsd_logp, "cm_GetData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2222 #ifdef AFS_FREELANCE_CLIENT
2225 // if getroot then we don't need to make any calls
2226 // just return fake data
2228 if (cm_freelanceEnabled && getroot) {
2229 // setup the fake status
2230 afsStatus.InterfaceVersion = 0x1;
2231 afsStatus.FileType = 0x2;
2232 afsStatus.LinkCount = scp->linkCount;
2233 afsStatus.Length = cm_fakeDirSize;
2234 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
2235 afsStatus.Author = 0x1;
2236 afsStatus.Owner = 0x0;
2237 afsStatus.CallerAccess = 0x9;
2238 afsStatus.AnonymousAccess = 0x9;
2239 afsStatus.UnixModeBits = 0x1ff;
2240 afsStatus.ParentVnode = 0x1;
2241 afsStatus.ParentUnique = 0x1;
2242 afsStatus.ResidencyMask = 0;
2243 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
2244 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
2245 afsStatus.Group = 0;
2246 afsStatus.SyncCounter = 0;
2247 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
2248 afsStatus.lockCount = 0;
2249 afsStatus.Length_hi = 0;
2250 afsStatus.errorCode = 0;
2251 memset(&volSync, 0, sizeof(volSync));
2253 // once we're done setting up the status info,
2254 // we just fill the buffer pages with fakedata
2255 // from cm_FakeRootDir. Extra pages are set to
2258 lock_ObtainMutex(&cm_Freelance_Lock);
2259 t1 = offsetp->LowPart;
2260 memset(datap, 0, data_length);
2261 t2 = cm_fakeDirSize - t1;
2262 if (t2 > data_length)
2265 memcpy(datap, cm_FakeRootDir+t1, t2);
2266 lock_ReleaseMutex(&cm_Freelance_Lock);
2268 // once we're done, we skip over the part of the
2269 // code that does the ACTUAL fetching of data for
2272 goto fetchingcompleted;
2275 #endif /* AFS_FREELANCE_CLIENT */
2278 lock_ReleaseWrite(&scp->rw);
2282 /* now make the call */
2284 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2288 rxconnp = cm_GetRxConn(connp);
2289 rxcallp = rx_NewCall(rxconnp);
2290 rx_PutConnection(rxconnp);
2292 nbytes = nbytes_hi = 0;
2294 if (SERVERHAS64BIT(connp)) {
2297 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2298 scp, offsetp->HighPart, offsetp->LowPart, data_length);
2300 code = StartRXAFS_FetchData64(rxcallp, &tfid, offsetp->QuadPart, data_length);
2303 temp = rx_Read32(rxcallp, &nbytes_hi);
2304 if (temp == sizeof(afs_int32)) {
2305 nbytes_hi = ntohl(nbytes_hi);
2308 code = rx_Error(rxcallp);
2309 code1 = rx_EndCall(rxcallp, code);
2317 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2318 if (require_64bit_ops) {
2319 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2320 code = CM_ERROR_TOOBIG;
2323 rxconnp = cm_GetRxConn(connp);
2324 rxcallp = rx_NewCall(rxconnp);
2325 rx_PutConnection(rxconnp);
2328 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2329 scp, offsetp->LowPart, data_length);
2331 code = StartRXAFS_FetchData(rxcallp, &tfid, offsetp->LowPart, data_length);
2333 SET_SERVERHASNO64BIT(connp);
2338 temp = rx_Read32(rxcallp, &nbytes);
2339 if (temp == sizeof(afs_int32)) {
2340 nbytes = ntohl(nbytes);
2341 FillInt64(length_found, nbytes_hi, nbytes);
2342 if (length_found > data_length) {
2344 * prior to 1.4.12 and 1.5.65 the file server would return
2345 * (filesize - offset) if the requested offset was greater than
2346 * the filesize. The correct return value would have been zero.
2347 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2348 * is a race between two RPCs issues by this cache manager, the
2349 * correct thing will happen the second time.
2351 osi_Log0(afsd_logp, "cm_GetData length_found > data_length");
2352 fs_fetchdata_offset_bug = 1;
2355 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2356 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2359 /* for the moment, nbytes_hi will always be 0 if code == 0
2360 because data_length is a 32-bit quantity. */
2363 /* fill length_found of data from the pipe into the pages.
2364 * When we stop, qdp will point at the last page we're
2365 * dealing with, and bufferp will tell us where we
2366 * stopped. We'll need this info below when we clear
2367 * the remainder of the last page out (and potentially
2368 * clear later pages out, if we fetch past EOF).
2370 while (length_found > 0) {
2372 struct iovec tiov[RX_MAXIOVECS];
2373 afs_int32 tnio, iov, iov_offset;
2375 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
2376 osi_Log1(afsd_logp, "cm_GetData rx_Readv returns %d", temp);
2377 if (temp != length_found && temp < data_length) {
2379 * If the file server returned (filesize - offset),
2380 * then the first rx_Read will return zero octets of data.
2381 * If it does, do not treat it as an error. Correct the
2382 * length_found and continue as if the file server said
2383 * it was sending us zero octets of data.
2385 if (fs_fetchdata_offset_bug && first_read)
2388 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2396 while (rbytes > 0) {
2399 osi_assertx(bufferp != NULL, "null cm_buf_t");
2401 len = min(tiov[iov].iov_len - iov_offset, data_length - buffer_offset);
2402 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
2404 buffer_offset += len;
2407 if (iov_offset == tiov[iov].iov_len) {
2413 length_found -= temp;
2414 #else /* USE_RX_IOVEC */
2415 /* assert that there are still more buffers;
2416 * our check above for length_found being less than
2417 * data_length should ensure this.
2419 osi_assertx(bufferp != NULL, "null cm_buf_t");
2421 /* read rbytes of data */
2422 rbytes = (afs_uint32)(length_found > data_length ? data_length : length_found);
2423 temp = rx_Read(rxcallp, bufferp, rbytes);
2424 if (temp < rbytes) {
2426 * If the file server returned (filesize - offset),
2427 * then the first rx_Read will return zero octets of data.
2428 * If it does, do not treat it as an error. Correct the
2429 * length_found and continue as if the file server said
2430 * it was sending us zero octets of data.
2432 if (fs_fetchdata_offset_bug && first_read)
2435 code = (rx_Error(rxcallp) < 0) ? rx_Error(rxcallp) : RX_PROTOCOL_ERROR;
2440 /* and adjust counters */
2441 length_found -= temp;
2442 #endif /* USE_RX_IOVEC */
2445 /* zero out remainder of last pages, in case we are
2446 * fetching past EOF. We were fetching an integral #
2447 * of pages, but stopped, potentially in the middle of
2448 * a page. Zero the remainder of that page, and then
2449 * all of the rest of the pages.
2452 rbytes = data_length - buffer_offset;
2453 bufferp = datap + buffer_offset;
2454 #else /* USE_RX_IOVEC */
2456 osi_assertx((bufferp - datap) < LONG_MAX, "data >= LONG_MAX");
2457 rbytes = (long) (bufferp - datap);
2459 /* bytes left to zero */
2460 rbytes = data_length - rbytes;
2461 #endif /* USE_RX_IOVEC */
2463 memset(bufferp, 0, rbytes);
2468 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2470 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2473 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2475 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2479 code1 = rx_EndCall(rxcallp, code);
2481 if (code1 == RXKADUNKNOWNKEY)
2482 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2484 /* If we are avoiding a file server bug, ignore the error state */
2485 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2486 /* Clone the current status info and clear the error state */
2487 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2489 lock_ReleaseWrite(&scp->rw);
2493 /* Prefer the error value from FetchData over rx_EndCall */
2494 } else if (code == 0 && code1 != 0)
2496 osi_Log0(afsd_logp, "CALL FetchData DONE");
2498 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
2501 code = cm_MapRPCError(code, reqp);
2504 lock_ObtainWrite(&scp->rw);
2507 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2509 InterlockedDecrement(&scp->activeRPCs);