2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 #include <afsconfig.h>
12 #include <afs/param.h>
27 extern void afsi_log(char *pattern, ...);
30 #ifdef AFS_FREELANCE_CLIENT
31 extern osi_mutex_t cm_Freelance_Lock;
34 #define USE_RX_IOVEC 1
36 /* we can access connp->serverp without holding a lock because that
37 never changes since the connection is made. */
38 #define SERVERHAS64BIT(connp) (!((connp)->serverp->flags & CM_SERVERFLAG_NO64BIT))
39 #define SET_SERVERHASNO64BIT(connp) (cm_SetServerNo64Bit((connp)->serverp, TRUE))
41 /* functions called back from the buffer package when reading or writing data,
42 * or when holding or releasing a vnode pointer.
44 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
45 cm_user_t *userp, cm_req_t *reqp)
47 /* store the data back from this buffer; the buffer is locked and held,
48 * but the vnode involved isn't locked, yet. It is held by its
49 * reference from the buffer, which won't change until the buffer is
50 * released by our caller. Thus, we don't have to worry about holding
54 cm_scache_t *scp = vscp;
56 afs_int32 save_nbytes;
58 AFSFetchStatus outStatus;
59 AFSStoreStatus inStatus;
63 struct rx_call *rxcallp;
64 struct rx_connection *rxconnp;
71 cm_bulkIO_t biod; /* bulk IO descriptor */
72 int require_64bit_ops = 0;
73 int call_was_64bit = 0;
74 int scp_locked = flags & CM_BUF_WRITE_SCP_LOCKED;
76 osi_assertx(userp != NULL, "null cm_user_t");
77 osi_assertx(scp != NULL, "null cm_scache_t");
79 memset(&volSync, 0, sizeof(volSync));
81 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
82 * drops lots of locks, and may indeed return a properly initialized
83 * buffer, although more likely it will just return a new, empty, buffer.
86 lock_ObtainWrite(&scp->rw);
87 if (scp->flags & CM_SCACHEFLAG_DELETED) {
89 lock_ReleaseWrite(&scp->rw);
90 return CM_ERROR_NOSUCHFILE;
93 cm_AFSFidFromFid(&tfid, &scp->fid);
95 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
96 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
98 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
100 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
101 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
103 lock_ReleaseWrite(&scp->rw);
107 if (biod.length == 0) {
108 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
109 cm_ReleaseBIOD(&biod, 1, 0, 1); /* should be a NOOP */
110 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
112 lock_ReleaseWrite(&scp->rw);
116 /* prepare the output status for the store */
117 scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
118 cm_StatusFromAttr(&inStatus, scp, NULL);
119 truncPos = scp->length;
120 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
121 && LargeIntegerLessThan(scp->truncPos, truncPos))
122 truncPos = scp->truncPos;
123 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
125 /* compute how many bytes to write from this buffer */
126 thyper = LargeIntegerSubtract(scp->length, biod.offset);
127 if (LargeIntegerLessThanZero(thyper)) {
128 /* entire buffer is past EOF */
132 /* otherwise write out part of buffer before EOF, but not
133 * more than bufferSize bytes.
135 if (LargeIntegerGreaterThan(thyper,
136 ConvertLongToLargeInteger(biod.length))) {
137 nbytes = biod.length;
139 /* if thyper is less than or equal to biod.length, then we
140 can safely assume that the value fits in a long. */
141 nbytes = thyper.LowPart;
145 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
146 ConvertLongToLargeInteger(nbytes)),
147 ConvertLongToLargeInteger(LONG_MAX)) ||
148 LargeIntegerGreaterThan(truncPos,
149 ConvertLongToLargeInteger(LONG_MAX))) {
150 require_64bit_ops = 1;
153 lock_ReleaseWrite(&scp->rw);
155 /* now we're ready to do the store operation */
156 save_nbytes = nbytes;
158 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
163 rxconnp = cm_GetRxConn(connp);
164 rxcallp = rx_NewCall(rxconnp);
165 rx_PutConnection(rxconnp);
167 if (SERVERHAS64BIT(connp)) {
170 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
171 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
172 osi_Log2(afsd_logp, "... truncPos 0x%x:%08x", truncPos.HighPart, truncPos.LowPart);
174 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
175 biod.offset.QuadPart,
179 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
181 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
185 if (require_64bit_ops) {
186 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData. The operation requires large file support in the server.");
187 code = CM_ERROR_TOOBIG;
189 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
190 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
192 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
193 biod.offset.LowPart, nbytes, truncPos.LowPart);
195 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
197 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
202 afs_uint32 buf_offset = 0, bytes_copied = 0;
204 /* write the data from the the list of buffers */
208 struct iovec tiov[RX_MAXIOVECS];
209 afs_int32 tnio, vlen, vbytes, iov, voffset;
212 vbytes = rx_WritevAlloc(rxcallp, tiov, &tnio, RX_MAXIOVECS, nbytes);
214 code = RX_PROTOCOL_ERROR;
218 for ( iov = voffset = vlen = 0;
219 vlen < vbytes && iov < tnio; vlen += wbytes) {
221 qdp = biod.bufListEndp;
222 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
223 } else if (buf_offset == cm_data.buf_blockSize) {
224 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
228 osi_assertx(qdp != NULL, "null osi_queueData_t");
229 bufp = osi_GetQData(qdp);
230 bufferp = bufp->datap + buf_offset;
231 wbytes = vbytes - vlen;
232 if (wbytes > cm_data.buf_blockSize - buf_offset)
233 wbytes = cm_data.buf_blockSize - buf_offset;
235 vleft = tiov[iov].iov_len - voffset;
236 while (wbytes > vleft && iov < tnio) {
237 memcpy(tiov[iov].iov_base + voffset, bufferp, vleft);
238 bytes_copied += vleft;
246 vleft = tiov[iov].iov_len;
250 memcpy(tiov[iov].iov_base + voffset, bufferp, wbytes);
251 bytes_copied += wbytes;
252 if (tiov[iov].iov_len == voffset + wbytes) {
255 vleft = (iov < tnio) ? tiov[iov].iov_len : 0;
261 buf_offset += wbytes;
267 osi_assertx(iov == tnio, "incorrect iov count");
268 osi_assertx(vlen == vbytes, "bytes_copied != vbytes");
269 osi_assertx(bufp->offset.QuadPart + buf_offset == biod.offset.QuadPart + bytes_copied,
270 "begin and end offsets don't match");
272 temp = rx_Writev(rxcallp, tiov, tnio, vbytes);
273 if (temp != vbytes) {
274 osi_Log3(afsd_logp, "rx_Writev failed bp 0x%p, %d != %d", bufp, temp, vbytes);
275 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
279 osi_Log2(afsd_logp, "rx_Writev succeeded iov offset 0x%x, wrote 0x%x",
280 (unsigned long)(bufp->offset.QuadPart + buf_offset - vbytes), vbytes);
282 #else /* USE_RX_IOVEC */
284 qdp = biod.bufListEndp;
285 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
287 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
291 osi_assertx(qdp != NULL, "null osi_queueData_t");
292 bufp = osi_GetQData(qdp);
293 bufferp = bufp->datap + buf_offset;
295 if (wbytes > cm_data.buf_blockSize - buf_offset)
296 wbytes = cm_data.buf_blockSize - buf_offset;
298 /* write out wbytes of data from bufferp */
299 temp = rx_Write(rxcallp, bufferp, wbytes);
300 if (temp != wbytes) {
301 osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d", bufp, temp, wbytes);
302 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
305 osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p written %d", bufp, temp);
308 #endif /* USE_RX_IOVEC */
309 } /* while more bytes to write */
310 } /* if RPC started successfully */
313 if (call_was_64bit) {
314 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
316 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
318 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
320 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
322 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
324 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
328 code1 = rx_EndCall(rxcallp, code);
330 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
331 SET_SERVERHASNO64BIT(connp);
333 nbytes = save_nbytes;
337 /* Prefer StoreData error over rx_EndCall error */
340 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
342 code = cm_MapRPCError(code, reqp);
345 osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
347 osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
349 /* now, clean up our state */
350 lock_ObtainWrite(&scp->rw);
352 cm_ReleaseBIOD(&biod, 1, code, 1);
356 /* now, here's something a little tricky: in AFS 3, a dirty
357 * length can't be directly stored, instead, a dirty chunk is
358 * stored that sets the file's size (by writing and by using
359 * the truncate-first option in the store call).
361 * At this point, we've just finished a store, and so the trunc
362 * pos field is clean. If the file's size at the server is at
363 * least as big as we think it should be, then we turn off the
364 * length dirty bit, since all the other dirty buffers must
365 * precede this one in the file.
367 * The file's desired size shouldn't be smaller than what's
368 * stored at the server now, since we just did the trunc pos
371 * We have to turn off the length dirty bit as soon as we can,
372 * so that we see updates made by other machines.
375 if (call_was_64bit) {
376 t.LowPart = outStatus.Length;
377 t.HighPart = outStatus.Length_hi;
379 t = ConvertLongToLargeInteger(outStatus.Length);
382 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
383 scp->mask &= ~CM_SCACHEMASK_LENGTH;
385 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
387 if (code == CM_ERROR_SPACE)
388 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
389 else if (code == CM_ERROR_QUOTA)
390 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
392 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
395 lock_ReleaseWrite(&scp->rw);
401 * Truncate the file, by sending a StoreData RPC with zero length.
403 * Called with scp locked. Releases and re-obtains the lock.
405 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
407 AFSFetchStatus outStatus;
408 AFSStoreStatus inStatus;
412 osi_hyper_t truncPos;
414 struct rx_call *rxcallp;
415 struct rx_connection *rxconnp;
416 int require_64bit_ops = 0;
417 int call_was_64bit = 0;
419 memset(&volSync, 0, sizeof(volSync));
421 osi_Log2(afsd_logp, "cm_StoreMini scp 0x%p userp 0x%p", scp, userp);
423 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
424 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
425 CM_SCACHESYNC_STOREDATA_EXCL);
427 /* prepare the output status for the store */
428 inStatus.Mask = AFS_SETMODTIME;
429 inStatus.ClientModTime = scp->clientModTime;
430 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
432 /* calculate truncation position */
433 truncPos = scp->length;
434 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
435 && LargeIntegerLessThan(scp->truncPos, truncPos))
436 truncPos = scp->truncPos;
437 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
439 if (LargeIntegerGreaterThan(truncPos,
440 ConvertLongToLargeInteger(LONG_MAX))) {
442 require_64bit_ops = 1;
445 lock_ReleaseWrite(&scp->rw);
447 cm_AFSFidFromFid(&tfid, &scp->fid);
449 /* now we're ready to do the store operation */
451 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
456 rxconnp = cm_GetRxConn(connp);
457 rxcallp = rx_NewCall(rxconnp);
458 rx_PutConnection(rxconnp);
460 if (SERVERHAS64BIT(connp)) {
463 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, truncPos 0x%x:%08x",
464 scp, truncPos.HighPart, truncPos.LowPart);
466 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
467 0, 0, truncPos.QuadPart);
469 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
471 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
475 if (require_64bit_ops) {
476 code = CM_ERROR_TOOBIG;
478 osi_Log3(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, truncPos 0x%x:%08x",
479 scp, truncPos.HighPart, truncPos.LowPart);
481 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
482 0, 0, truncPos.LowPart);
484 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
486 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
491 if (call_was_64bit) {
492 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
494 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
496 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
498 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
500 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX", scp, code);
502 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
505 code1 = rx_EndCall(rxcallp, code);
507 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
508 SET_SERVERHASNO64BIT(connp);
512 /* prefer StoreData error over rx_EndCall error */
513 if (code == 0 && code1 != 0)
515 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
516 code = cm_MapRPCError(code, reqp);
518 /* now, clean up our state */
519 lock_ObtainWrite(&scp->rw);
524 * For explanation of handling of CM_SCACHEMASK_LENGTH,
527 if (call_was_64bit) {
528 t.HighPart = outStatus.Length_hi;
529 t.LowPart = outStatus.Length;
531 t = ConvertLongToLargeInteger(outStatus.Length);
534 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
535 scp->mask &= ~CM_SCACHEMASK_LENGTH;
536 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
538 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
543 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
547 /* now return a code that means that I/O is done */
552 * stabilize scache entry with CM_SCACHESYNC_SETSIZE. This prevents any new
553 * data buffers to be allocated, new data to be fetched from the file server,
554 * and writes to be accepted from the application but permits dirty buffers
555 * to be written to the file server.
557 * Stabilize uses cm_SyncOp to maintain the cm_scache_t in this stable state
558 * instead of holding the rwlock exclusively. This permits background stores
559 * to be performed in parallel and in particular allow FlushFile to be
560 * implemented without violating the locking hierarchy.
562 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
564 cm_scache_t *scp = vscp;
567 lock_ObtainWrite(&scp->rw);
568 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
569 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
570 lock_ReleaseWrite(&scp->rw);
575 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
576 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
578 cm_scache_t *scp = vscp;
580 lock_ObtainWrite(&scp->rw);
581 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
583 lock_ReleaseWrite(&scp->rw);
585 /* always succeeds */
589 cm_buf_ops_t cm_bufOps = {
596 long cm_ValidateDCache(void)
598 return buf_ValidateBuffers();
601 long cm_ShutdownDCache(void)
606 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
608 return buf_Init(newFile, &cm_bufOps, nbuffers);
611 /* check to see if we have an up-to-date buffer. The buffer must have
612 * previously been obtained by calling buf_Get.
614 * Make sure we have a callback, and that the dataversion matches.
616 * Scp must be locked.
618 * Bufp *may* be locked.
620 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
623 if (!cm_HaveCallback(scp))
625 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
627 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
629 if (bufp->offset.QuadPart >= scp->serverLength.QuadPart)
632 code = lock_TryMutex(&bufp->mx);
634 /* don't have the lock, and can't lock it, then
641 /* remember dirty flag for later */
642 code = bufp->flags & CM_BUF_DIRTY;
644 /* release lock if we obtained it here */
646 lock_ReleaseMutex(&bufp->mx);
648 /* if buffer was dirty, buffer is acceptable for use */
656 * used when deciding whether to do a background fetch or not.
657 * call with scp->rw write-locked.
660 cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
661 cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
665 osi_hyper_t tblocksize;
670 /* now scan all buffers in the range, looking for any that look like
675 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
677 while (LargeIntegerGreaterThanZero(tlength)) {
678 /* get callback so we can do a meaningful dataVersion comparison */
679 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
680 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
684 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
685 /* we're past the end of file */
689 bp = buf_Find(scp, &tbase);
690 /* We cheat slightly by not locking the bp mutex. */
692 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMBKGFETCH)) == 0
693 && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
701 /* if this buffer is essentially guaranteed to require a fetch,
702 * break out here and return this position.
707 tbase = LargeIntegerAdd(tbase, tblocksize);
708 tlength = LargeIntegerSubtract(tlength, tblocksize);
711 /* if we get here, either everything is fine or 'stop' stopped us at a
712 * particular buffer in the range that definitely needs to be fetched.
715 /* return non-zero code since realBasep won't be valid */
719 /* successfully found a page that will need fetching */
727 cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
735 if (scp->flags & CM_SCACHEFLAG_DELETED) {
736 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
740 /* Retries will be performed by the BkgDaemon thread if appropriate */
741 req.flags |= CM_REQ_NORETRY;
743 toffset.LowPart = p1;
744 toffset.HighPart = p2;
747 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
749 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, &req);
751 osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
755 * Keep the following list synchronized with the
756 * error code list in cm_BkgDaemon
759 case CM_ERROR_TIMEDOUT: /* or server restarting */
761 case CM_ERROR_WOULDBLOCK:
762 case CM_ERROR_ALLBUSY:
763 case CM_ERROR_ALLDOWN:
764 case CM_ERROR_ALLOFFLINE:
765 case CM_ERROR_PARTIALWRITE:
766 break; /* cm_BkgDaemon will re-insert the request in the queue */
769 lock_ObtainWrite(&scp->rw);
770 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
771 lock_ReleaseWrite(&scp->rw);
776 /* Called with scp locked */
777 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
782 end = LargeIntegerAdd(*base, *length);
783 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
784 scp->prefetch.base = *base;
785 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
786 scp->prefetch.end = end;
788 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
791 /* do the prefetch. if the prefetch fails, return 0 (success)
792 * because there is no harm done. */
794 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
802 osi_hyper_t tblocksize;
810 /* Retries will be performed by the BkgDaemon thread if appropriate */
811 req.flags |= CM_REQ_NORETRY;
814 fetched.HighPart = 0;
815 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
819 length.HighPart = p4;
821 end = LargeIntegerAdd(base, length);
823 osi_Log5(afsd_logp, "Starting BKG prefetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
824 scp, p2, p1, p4, p3);
826 for ( code = 0, offset = base;
827 code == 0 && LargeIntegerLessThan(offset, end);
828 offset = LargeIntegerAdd(offset, tblocksize) )
831 lock_ReleaseWrite(&scp->rw);
835 code = buf_Get(scp, &offset, &req, &bp);
839 if (bp->cmFlags & CM_BUF_CMFETCHING) {
840 /* skip this buffer as another thread is already fetching it */
842 lock_ObtainWrite(&scp->rw);
845 bp->cmFlags &= ~CM_BUF_CMBKGFETCH;
852 lock_ObtainWrite(&scp->rw);
856 code = cm_GetBuffer(scp, bp, NULL, userp, &req);
858 fetched = LargeIntegerAdd(fetched, tblocksize);
860 bp->cmFlags &= ~CM_BUF_CMBKGFETCH;
864 lock_ObtainWrite(&scp->rw);
868 /* Clear flag from any remaining buffers */
870 LargeIntegerLessThan(offset, end);
871 offset = LargeIntegerAdd(offset, tblocksize) )
873 bp = buf_Find(scp, &offset);
875 bp->cmFlags &= ~CM_BUF_CMBKGFETCH;
879 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
880 scp, &base, &fetched);
882 /* wakeup anyone who is waiting */
883 if (scp->flags & CM_SCACHEFLAG_WAITING) {
884 osi_Log1(afsd_logp, "CM BkgPrefetch Waking scp 0x%p", scp);
885 osi_Wakeup((LONG_PTR) &scp->flags);
887 lock_ReleaseWrite(&scp->rw);
889 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p code 0x%x fetched 0x%x:%x",
890 scp, code, fetched.HighPart, fetched.LowPart);
894 /* a read was issued to offsetp, and we have to determine whether we should
895 * do a prefetch of the next chunk.
897 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
898 cm_user_t *userp, cm_req_t *reqp)
902 osi_hyper_t realBase;
903 osi_hyper_t readBase;
904 osi_hyper_t readLength;
907 osi_hyper_t tblocksize; /* a long long temp variable */
910 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
913 /* round up to chunk boundary */
914 readBase.LowPart += (cm_chunkSize-1);
915 readBase.LowPart &= (-cm_chunkSize);
917 readLength = ConvertLongToLargeInteger(count);
919 lock_ObtainWrite(&scp->rw);
921 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
922 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
923 lock_ReleaseWrite(&scp->rw);
926 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
928 /* start the scan at the latter of the end of this read or
929 * the end of the last fetched region.
931 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
932 readBase = scp->prefetch.end;
934 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
937 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
938 lock_ReleaseWrite(&scp->rw);
939 return; /* can't find something to prefetch */
942 readEnd = LargeIntegerAdd(realBase, readLength);
945 * Mark each buffer in the range as queued for a
948 for ( offset = realBase;
949 LargeIntegerLessThan(offset, readEnd);
950 offset = LargeIntegerAdd(offset, tblocksize) )
953 lock_ReleaseWrite(&scp->rw);
957 bp = buf_Find(scp, &offset);
962 lock_ObtainWrite(&scp->rw);
966 bp->cmFlags |= CM_BUF_CMBKGFETCH;
971 lock_ReleaseWrite(&scp->rw);
973 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
974 scp, realBase.LowPart);
976 cm_QueueBKGRequest(scp, cm_BkgPrefetch,
977 realBase.LowPart, realBase.HighPart,
978 readLength.LowPart, readLength.HighPart,
982 /* scp must be locked; temporarily unlocked during processing.
983 * If returns 0, returns buffers held in biop, and with
984 * CM_BUF_CMSTORING set.
986 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
987 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
988 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
989 * must be woken, and the event must be set when the I/O is done. All of this
990 * is required so that buf_WaitIO synchronizes properly with the buffer as it
991 * is being written out.
993 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
994 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
997 osi_queueData_t *qdp;
1000 osi_hyper_t scanStart; /* where to start scan for dirty pages */
1001 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
1002 osi_hyper_t firstModOffset; /* offset of first modified page in range */
1005 long flags; /* flags to cm_SyncOp */
1007 /* clear things out */
1008 biop->scp = scp; /* do not hold; held by caller */
1009 biop->offset = *inOffsetp;
1011 biop->bufListp = NULL;
1012 biop->bufListEndp = NULL;
1015 /* reserve a chunk's worth of buffers */
1016 lock_ReleaseWrite(&scp->rw);
1017 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1018 lock_ObtainWrite(&scp->rw);
1021 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
1022 thyper = ConvertLongToLargeInteger(temp);
1023 tbase = LargeIntegerAdd(*inOffsetp, thyper);
1025 bufp = buf_Find(scp, &tbase);
1027 /* get buffer mutex and scp mutex safely */
1028 lock_ReleaseWrite(&scp->rw);
1029 lock_ObtainMutex(&bufp->mx);
1032 * if the buffer is actively involved in I/O
1033 * we wait for the I/O to complete.
1035 if (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING))
1036 buf_WaitIO(scp, bufp);
1038 lock_ObtainWrite(&scp->rw);
1039 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
1040 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1042 lock_ReleaseMutex(&bufp->mx);
1045 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1049 /* if the buffer is dirty, we're done */
1050 if (bufp->flags & CM_BUF_DIRTY) {
1051 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
1052 "WRITING w/o CMSTORING in SetupStoreBIOD");
1053 bufp->flags |= CM_BUF_WRITING;
1057 /* this buffer is clean, so there's no reason to process it */
1058 cm_SyncOpDone(scp, bufp, flags);
1059 lock_ReleaseMutex(&bufp->mx);
1067 /* if we get here, if bufp is null, we didn't find any dirty buffers
1068 * that weren't already being stored back, so we just quit now.
1074 /* don't need buffer mutex any more */
1075 lock_ReleaseMutex(&bufp->mx);
1077 /* put this element in the list */
1078 qdp = osi_QDAlloc();
1079 osi_SetQData(qdp, bufp);
1080 /* don't have to hold bufp, since held by buf_Find above */
1081 osi_QAddH((osi_queue_t **) &biop->bufListp,
1082 (osi_queue_t **) &biop->bufListEndp,
1084 biop->length = cm_data.buf_blockSize;
1085 firstModOffset = bufp->offset;
1086 biop->offset = firstModOffset;
1087 bufp = NULL; /* this buffer and reference added to the queue */
1089 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
1090 scanStart = *inOffsetp;
1091 scanStart.LowPart &= (-cm_chunkSize);
1092 thyper = ConvertLongToLargeInteger(cm_chunkSize);
1093 scanEnd = LargeIntegerAdd(scanStart, thyper);
1095 flags = CM_SCACHESYNC_GETSTATUS
1096 | CM_SCACHESYNC_STOREDATA
1097 | CM_SCACHESYNC_BUFLOCKED
1098 | CM_SCACHESYNC_NOWAIT;
1100 /* start by looking backwards until scanStart */
1101 /* hyper version of cm_data.buf_blockSize */
1102 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1103 tbase = LargeIntegerSubtract(firstModOffset, thyper);
1104 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
1105 /* see if we can find the buffer */
1106 bufp = buf_Find(scp, &tbase);
1110 /* try to lock it, and quit if we can't (simplifies locking) */
1111 lock_ReleaseWrite(&scp->rw);
1112 code = lock_TryMutex(&bufp->mx);
1113 lock_ObtainWrite(&scp->rw);
1120 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1122 lock_ReleaseMutex(&bufp->mx);
1128 if (!(bufp->flags & CM_BUF_DIRTY)) {
1129 /* buffer is clean, so we shouldn't add it */
1130 cm_SyncOpDone(scp, bufp, flags);
1131 lock_ReleaseMutex(&bufp->mx);
1137 /* don't need buffer mutex any more */
1138 lock_ReleaseMutex(&bufp->mx);
1140 /* we have a dirty buffer ready for storing. Add it to the tail
1141 * of the list, since it immediately precedes all of the disk
1142 * addresses we've already collected.
1144 qdp = osi_QDAlloc();
1145 osi_SetQData(qdp, bufp);
1146 /* no buf_hold necessary, since we have it held from buf_Find */
1147 osi_QAddT((osi_queue_t **) &biop->bufListp,
1148 (osi_queue_t **) &biop->bufListEndp,
1150 bufp = NULL; /* added to the queue */
1152 /* update biod info describing the transfer */
1153 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
1154 biop->length += cm_data.buf_blockSize;
1156 /* update loop pointer */
1157 tbase = LargeIntegerSubtract(tbase, thyper);
1158 } /* while loop looking for pages preceding the one we found */
1160 /* now, find later dirty, contiguous pages, and add them to the list */
1161 /* hyper version of cm_data.buf_blockSize */
1162 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1163 tbase = LargeIntegerAdd(firstModOffset, thyper);
1164 while(LargeIntegerLessThan(tbase, scanEnd)) {
1165 /* see if we can find the buffer */
1166 bufp = buf_Find(scp, &tbase);
1170 /* try to lock it, and quit if we can't (simplifies locking) */
1171 lock_ReleaseWrite(&scp->rw);
1172 code = lock_TryMutex(&bufp->mx);
1173 lock_ObtainWrite(&scp->rw);
1180 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1182 lock_ReleaseMutex(&bufp->mx);
1188 if (!(bufp->flags & CM_BUF_DIRTY)) {
1189 /* buffer is clean, so we shouldn't add it */
1190 cm_SyncOpDone(scp, bufp, flags);
1191 lock_ReleaseMutex(&bufp->mx);
1197 /* don't need buffer mutex any more */
1198 lock_ReleaseMutex(&bufp->mx);
1200 /* we have a dirty buffer ready for storing. Add it to the head
1201 * of the list, since it immediately follows all of the disk
1202 * addresses we've already collected.
1204 qdp = osi_QDAlloc();
1205 osi_SetQData(qdp, bufp);
1206 /* no buf_hold necessary, since we have it held from buf_Find */
1207 osi_QAddH((osi_queue_t **) &biop->bufListp,
1208 (osi_queue_t **) &biop->bufListEndp,
1212 /* update biod info describing the transfer */
1213 biop->length += cm_data.buf_blockSize;
1215 /* update loop pointer */
1216 tbase = LargeIntegerAdd(tbase, thyper);
1217 } /* while loop looking for pages following the first page we found */
1219 /* finally, we're done */
1223 /* scp must be locked; temporarily unlocked during processing.
1224 * If returns 0, returns buffers held in biop, and with
1225 * CM_BUF_CMFETCHING flags set.
1226 * If an error is returned, we don't return any buffers.
1228 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1229 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1233 osi_hyper_t tblocksize; /* a long long temp variable */
1234 osi_hyper_t pageBase; /* base offset we're looking at */
1235 osi_queueData_t *qdp; /* one temp queue structure */
1236 osi_queueData_t *tqdp; /* another temp queue structure */
1237 long collected; /* how many bytes have been collected */
1240 osi_hyper_t fileSize; /* the # of bytes in the file */
1241 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1242 osi_queueData_t *heldBufListEndp; /* first one */
1245 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1247 biop->scp = scp; /* do not hold; held by caller */
1248 biop->offset = *offsetp;
1249 /* null out the list of buffers */
1250 biop->bufListp = biop->bufListEndp = NULL;
1253 /* first lookup the file's length, so we know when to stop */
1254 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1255 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1259 /* copy out size, since it may change */
1260 fileSize = scp->serverLength;
1262 lock_ReleaseWrite(&scp->rw);
1264 pageBase = *offsetp;
1265 collected = pageBase.LowPart & (cm_chunkSize - 1);
1266 heldBufListp = NULL;
1267 heldBufListEndp = NULL;
1270 * Obtaining buffers can cause dirty buffers to be recycled, which
1271 * can cause a storeback, so cannot be done while we have buffers
1274 * To get around this, we get buffers twice. Before reserving buffers,
1275 * we obtain and release each one individually. After reserving
1276 * buffers, we try to obtain them again, but only by lookup, not by
1277 * recycling. If a buffer has gone away while we were waiting for
1278 * the others, we just use whatever buffers we already have.
1280 * On entry to this function, we are already holding a buffer, so we
1281 * can't wait for reservation. So we call buf_TryReserveBuffers()
1282 * instead. Not only that, we can't really even call buf_Get(), for
1283 * the same reason. We can't avoid that, though. To avoid deadlock
1284 * we allow only one thread to be executing the buf_Get()-buf_Release()
1285 * sequence at a time.
1288 /* first hold all buffers, since we can't hold any locks in buf_Get */
1290 /* stop at chunk boundary */
1291 if (collected >= cm_chunkSize)
1294 /* see if the next page would be past EOF */
1295 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1298 code = buf_Get(scp, &pageBase, reqp, &tbp);
1300 lock_ObtainWrite(&scp->rw);
1301 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1308 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1309 collected += cm_data.buf_blockSize;
1312 /* reserve a chunk's worth of buffers if possible */
1313 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1315 pageBase = *offsetp;
1316 collected = pageBase.LowPart & (cm_chunkSize - 1);
1318 /* now hold all buffers, if they are still there */
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 tbp = buf_Find(scp, &pageBase);
1332 /* add the buffer to the list */
1333 qdp = osi_QDAlloc();
1334 osi_SetQData(qdp, tbp);
1335 osi_QAddH((osi_queue_t **)&heldBufListp,
1336 (osi_queue_t **)&heldBufListEndp,
1338 /* leave tbp held (from buf_Get) */
1343 collected += cm_data.buf_blockSize;
1344 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1347 /* look at each buffer, adding it into the list if it looks idle and
1348 * filled with old data. One special case: wait for idle if it is the
1349 * first buffer since we really need that one for our caller to make
1353 collected = 0; /* now count how many we'll really use */
1354 for (tqdp = heldBufListEndp;
1356 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1357 /* get a ptr to the held buffer */
1358 tbp = osi_GetQData(tqdp);
1359 pageBase = tbp->offset;
1361 /* now lock the buffer lock */
1362 lock_ObtainMutex(&tbp->mx);
1363 lock_ObtainWrite(&scp->rw);
1365 /* don't bother fetching over data that is already current */
1366 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1367 /* we don't need this buffer, since it is current */
1368 lock_ReleaseWrite(&scp->rw);
1369 lock_ReleaseMutex(&tbp->mx);
1373 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1375 flags |= CM_SCACHESYNC_NOWAIT;
1377 /* wait for the buffer to serialize, if required. Doesn't
1378 * release the scp or buffer lock(s) if NOWAIT is specified.
1380 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1382 lock_ReleaseWrite(&scp->rw);
1383 lock_ReleaseMutex(&tbp->mx);
1387 /* don't fetch over dirty buffers */
1388 if (tbp->flags & CM_BUF_DIRTY) {
1389 cm_SyncOpDone(scp, tbp, flags);
1390 lock_ReleaseWrite(&scp->rw);
1391 lock_ReleaseMutex(&tbp->mx);
1396 lock_ReleaseWrite(&scp->rw);
1397 lock_ReleaseMutex(&tbp->mx);
1399 /* add the buffer to the list */
1400 qdp = osi_QDAlloc();
1401 osi_SetQData(qdp, tbp);
1402 osi_QAddH((osi_queue_t **)&biop->bufListp,
1403 (osi_queue_t **)&biop->bufListEndp,
1407 /* from now on, a failure just stops our collection process, but
1408 * we still do the I/O to whatever we've already managed to collect.
1411 collected += cm_data.buf_blockSize;
1414 /* now, we've held in biop->bufListp all the buffer's we're really
1415 * interested in. We also have holds left from heldBufListp, and we
1416 * now release those holds on the buffers.
1418 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1419 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1420 tbp = osi_GetQData(qdp);
1421 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1422 (osi_queue_t **) &heldBufListEndp,
1429 /* Caller expects this */
1430 lock_ObtainWrite(&scp->rw);
1432 /* if we got a failure setting up the first buffer, then we don't have
1433 * any side effects yet, and we also have failed an operation that the
1434 * caller requires to make any progress. Give up now.
1436 if (code && isFirst) {
1437 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1441 /* otherwise, we're still OK, and should just return the I/O setup we've
1444 biop->length = collected;
1445 biop->reserved = reserving;
1449 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1452 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
1454 cm_scache_t *scp; /* do not release; not held in biop */
1456 osi_queueData_t *qdp;
1457 osi_queueData_t *nqdp;
1460 /* Give back reserved buffers */
1462 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1465 flags = CM_SCACHESYNC_STOREDATA;
1467 flags = CM_SCACHESYNC_FETCHDATA;
1470 if (biop->bufListp) {
1471 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1472 /* lookup next guy first, since we're going to free this one */
1473 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1475 /* extract buffer and free queue data */
1476 bufp = osi_GetQData(qdp);
1477 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1478 (osi_queue_t **) &biop->bufListEndp,
1482 /* now, mark I/O as done, unlock the buffer and release it */
1484 lock_ReleaseWrite(&scp->rw);
1485 lock_ObtainMutex(&bufp->mx);
1486 lock_ObtainWrite(&scp->rw);
1487 cm_SyncOpDone(scp, bufp, flags);
1489 /* turn off writing and wakeup users */
1491 if (bufp->flags & CM_BUF_WAITING) {
1492 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1493 osi_Wakeup((LONG_PTR) bufp);
1496 bufp->flags &= ~CM_BUF_WRITING;
1498 case CM_ERROR_NOSUCHFILE:
1499 case CM_ERROR_BADFD:
1500 case CM_ERROR_NOACCESS:
1501 case CM_ERROR_QUOTA:
1502 case CM_ERROR_SPACE:
1503 case CM_ERROR_TOOBIG:
1504 case CM_ERROR_READONLY:
1505 case CM_ERROR_NOSUCHPATH:
1507 * Apply the fatal error to this buffer.
1509 bufp->flags &= ~CM_BUF_DIRTY;
1510 bufp->flags |= CM_BUF_ERROR;
1511 bufp->dirty_offset = 0;
1512 bufp->dirty_length = 0;
1514 bufp->dataVersion = CM_BUF_VERSION_BAD;
1515 bufp->dirtyCounter++;
1517 case CM_ERROR_TIMEDOUT:
1518 case CM_ERROR_ALLDOWN:
1519 case CM_ERROR_ALLBUSY:
1520 case CM_ERROR_ALLOFFLINE:
1521 case CM_ERROR_CLOCKSKEW:
1523 /* do not mark the buffer in error state but do
1524 * not attempt to complete the rest either.
1529 bufp->flags &= ~(CM_BUF_WRITING | CM_BUF_DIRTY);
1530 bufp->dirty_offset = bufp->dirty_length = 0;
1535 lock_ReleaseWrite(&scp->rw);
1536 lock_ReleaseMutex(&bufp->mx);
1542 lock_ObtainWrite(&scp->rw);
1543 cm_SyncOpDone(scp, NULL, flags);
1545 lock_ReleaseWrite(&scp->rw);
1548 /* clean things out */
1549 biop->bufListp = NULL;
1550 biop->bufListEndp = NULL;
1554 cm_CloneStatus(cm_scache_t *scp, cm_user_t *userp, int scp_locked,
1555 AFSFetchStatus *afsStatusp, AFSVolSync *volSyncp)
1557 // setup the status based upon the scp data
1558 afsStatusp->InterfaceVersion = 0x1;
1559 switch (scp->fileType) {
1560 case CM_SCACHETYPE_FILE:
1561 afsStatusp->FileType = File;
1563 case CM_SCACHETYPE_DIRECTORY:
1564 afsStatusp->FileType = Directory;
1566 case CM_SCACHETYPE_MOUNTPOINT:
1567 afsStatusp->FileType = SymbolicLink;
1569 case CM_SCACHETYPE_SYMLINK:
1570 case CM_SCACHETYPE_DFSLINK:
1571 afsStatusp->FileType = SymbolicLink;
1574 afsStatusp->FileType = -1; /* an invalid value */
1576 afsStatusp->LinkCount = scp->linkCount;
1577 afsStatusp->Length = scp->length.LowPart;
1578 afsStatusp->DataVersion = (afs_uint32)(scp->dataVersion & MAX_AFS_UINT32);
1579 afsStatusp->Author = 0x1;
1580 afsStatusp->Owner = scp->owner;
1582 lock_ObtainWrite(&scp->rw);
1585 if (cm_FindACLCache(scp, userp, &afsStatusp->CallerAccess))
1586 afsStatusp->CallerAccess = scp->anyAccess;
1587 afsStatusp->AnonymousAccess = scp->anyAccess;
1588 afsStatusp->UnixModeBits = scp->unixModeBits;
1589 afsStatusp->ParentVnode = scp->parentVnode;
1590 afsStatusp->ParentUnique = scp->parentUnique;
1591 afsStatusp->ResidencyMask = 0;
1592 afsStatusp->ClientModTime = scp->clientModTime;
1593 afsStatusp->ServerModTime = scp->serverModTime;
1594 afsStatusp->Group = scp->group;
1595 afsStatusp->SyncCounter = 0;
1596 afsStatusp->dataVersionHigh = (afs_uint32)(scp->dataVersion >> 32);
1597 afsStatusp->lockCount = 0;
1598 afsStatusp->Length_hi = scp->length.HighPart;
1599 afsStatusp->errorCode = 0;
1601 volSyncp->spare1 = scp->volumeCreationDate;
1606 /* Fetch a buffer. Called with scp locked.
1607 * The scp is locked on return.
1609 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1612 long code=0, code1=0;
1613 afs_uint32 nbytes; /* bytes in transfer */
1614 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1615 afs_uint64 length_found = 0;
1616 long rbytes; /* bytes in rx_Read call */
1618 AFSFetchStatus afsStatus;
1619 AFSCallBack callback;
1622 afs_uint32 buffer_offset;
1623 cm_buf_t *tbufp; /* buf we're filling */
1624 osi_queueData_t *qdp; /* q element we're scanning */
1626 struct rx_call *rxcallp;
1627 struct rx_connection *rxconnp;
1628 cm_bulkIO_t biod; /* bulk IO descriptor */
1632 int require_64bit_ops = 0;
1633 int call_was_64bit = 0;
1634 int fs_fetchdata_offset_bug = 0;
1638 memset(&volSync, 0, sizeof(volSync));
1640 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
1641 * drops lots of locks, and may indeed return a properly initialized
1642 * buffer, although more likely it will just return a new, empty, buffer.
1645 #ifdef AFS_FREELANCE_CLIENT
1647 // yj: if they're trying to get the /afs directory, we need to
1648 // handle it differently, since it's local rather than on any
1651 getroot = (scp==cm_data.rootSCachep);
1653 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1656 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1657 /* We already have this buffer don't do extra work */
1661 cm_AFSFidFromFid(&tfid, &scp->fid);
1663 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1665 /* couldn't even get the first page setup properly */
1666 osi_Log1(afsd_logp, "GetBuffer: SetupFetchBIOD failure code %d", code);
1670 /* once we get here, we have the callback in place, we know that no one
1671 * is fetching the data now. Check one last time that we still have
1672 * the wrong data, and then fetch it if we're still wrong.
1674 * We can lose a race condition and end up with biod.length zero, in
1675 * which case we just retry.
1677 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
1678 if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow) &&
1679 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength))
1681 osi_Log4(afsd_logp, "Bad DVs 0x%x != (0x%x -> 0x%x) or length 0x%x",
1682 bufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion, biod.length);
1684 if (bufp->dataVersion == CM_BUF_VERSION_BAD)
1685 memset(bufp->datap, 0, cm_data.buf_blockSize);
1686 bufp->dataVersion = scp->dataVersion;
1688 cm_ReleaseBIOD(&biod, 0, 0, 1);
1690 } else if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow)
1691 && (scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1692 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->truncPos)) {
1693 memset(bufp->datap, 0, cm_data.buf_blockSize);
1694 bufp->dataVersion = scp->dataVersion;
1695 cm_ReleaseBIOD(&biod, 0, 0, 1);
1699 lock_ReleaseWrite(&scp->rw);
1702 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1703 ConvertLongToLargeInteger(biod.length)),
1704 ConvertLongToLargeInteger(LONG_MAX))) {
1705 require_64bit_ops = 1;
1708 osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1709 osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1710 scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1712 #ifdef AFS_FREELANCE_CLIENT
1715 // if getroot then we don't need to make any calls
1716 // just return fake data
1718 if (cm_freelanceEnabled && getroot) {
1719 // setup the fake status
1720 afsStatus.InterfaceVersion = 0x1;
1721 afsStatus.FileType = 0x2;
1722 afsStatus.LinkCount = scp->linkCount;
1723 afsStatus.Length = cm_fakeDirSize;
1724 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1725 afsStatus.Author = 0x1;
1726 afsStatus.Owner = 0x0;
1727 afsStatus.CallerAccess = 0x9;
1728 afsStatus.AnonymousAccess = 0x9;
1729 afsStatus.UnixModeBits = 0x1ff;
1730 afsStatus.ParentVnode = 0x1;
1731 afsStatus.ParentUnique = 0x1;
1732 afsStatus.ResidencyMask = 0;
1733 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1734 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1735 afsStatus.Group = 0;
1736 afsStatus.SyncCounter = 0;
1737 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1738 afsStatus.lockCount = 0;
1739 afsStatus.Length_hi = 0;
1740 afsStatus.errorCode = 0;
1741 memset(&volSync, 0, sizeof(volSync));
1743 // once we're done setting up the status info,
1744 // we just fill the buffer pages with fakedata
1745 // from cm_FakeRootDir. Extra pages are set to
1748 lock_ObtainMutex(&cm_Freelance_Lock);
1749 t1 = bufp->offset.LowPart;
1750 qdp = biod.bufListEndp;
1752 tbufp = osi_GetQData(qdp);
1753 bufferp=tbufp->datap;
1754 memset(bufferp, 0, cm_data.buf_blockSize);
1755 t2 = cm_fakeDirSize - t1;
1756 if (t2> (afs_int32)cm_data.buf_blockSize)
1757 t2=cm_data.buf_blockSize;
1759 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1764 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1767 lock_ReleaseMutex(&cm_Freelance_Lock);
1769 // once we're done, we skip over the part of the
1770 // code that does the ACTUAL fetching of data for
1773 goto fetchingcompleted;
1776 #endif /* AFS_FREELANCE_CLIENT */
1779 * if the requested offset is greater than the file length,
1780 * the file server will return zero bytes of data and the
1781 * current status for the file which we already have since
1782 * we have just obtained a callback. Instead, we can avoid
1783 * the network round trip by allocating zeroed buffers and
1784 * faking the status info.
1786 if (biod.offset.QuadPart >= scp->length.QuadPart) {
1787 osi_Log5(afsd_logp, "SKIP FetchData64 scp 0x%p, off 0x%x:%08x > length 0x%x:%08x",
1788 scp, biod.offset.HighPart, biod.offset.LowPart,
1789 scp->length.HighPart, scp->length.LowPart);
1791 /* Clone the current status info */
1792 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
1794 /* status info complete, fill pages with zeros */
1795 for (qdp = biod.bufListEndp;
1797 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1798 tbufp = osi_GetQData(qdp);
1799 bufferp=tbufp->datap;
1800 memset(bufferp, 0, cm_data.buf_blockSize);
1803 /* no need to contact the file server */
1804 goto fetchingcompleted;
1808 lock_ReleaseWrite(&scp->rw);
1812 /* now make the call */
1814 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1818 rxconnp = cm_GetRxConn(connp);
1819 rxcallp = rx_NewCall(rxconnp);
1820 rx_PutConnection(rxconnp);
1822 nbytes = nbytes_hi = 0;
1824 if (SERVERHAS64BIT(connp)) {
1827 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1828 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1830 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1833 temp = rx_Read32(rxcallp, &nbytes_hi);
1834 if (temp == sizeof(afs_int32)) {
1835 nbytes_hi = ntohl(nbytes_hi);
1838 code = rxcallp->error;
1839 code1 = rx_EndCall(rxcallp, code);
1847 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1848 if (require_64bit_ops) {
1849 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1850 code = CM_ERROR_TOOBIG;
1853 rxconnp = cm_GetRxConn(connp);
1854 rxcallp = rx_NewCall(rxconnp);
1855 rx_PutConnection(rxconnp);
1858 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1859 scp, biod.offset.LowPart, biod.length);
1861 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1864 SET_SERVERHASNO64BIT(connp);
1869 temp = rx_Read32(rxcallp, &nbytes);
1870 if (temp == sizeof(afs_int32)) {
1871 nbytes = ntohl(nbytes);
1872 FillInt64(length_found, nbytes_hi, nbytes);
1873 if (length_found > biod.length) {
1875 * prior to 1.4.12 and 1.5.65 the file server would return
1876 * (filesize - offset) if the requested offset was greater than
1877 * the filesize. The correct return value would have been zero.
1878 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
1879 * is a race between two RPCs issues by this cache manager, the
1880 * correct thing will happen the second time.
1882 osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1883 fs_fetchdata_offset_bug = 1;
1886 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1887 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1890 /* for the moment, nbytes_hi will always be 0 if code == 0
1891 because biod.length is a 32-bit quantity. */
1894 qdp = biod.bufListEndp;
1896 tbufp = osi_GetQData(qdp);
1897 bufferp = tbufp->datap;
1903 /* fill length_found of data from the pipe into the pages.
1904 * When we stop, qdp will point at the last page we're
1905 * dealing with, and bufferp will tell us where we
1906 * stopped. We'll need this info below when we clear
1907 * the remainder of the last page out (and potentially
1908 * clear later pages out, if we fetch past EOF).
1910 while (length_found > 0) {
1912 struct iovec tiov[RX_MAXIOVECS];
1913 afs_int32 tnio, iov, iov_offset;
1915 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
1916 osi_Log1(afsd_logp, "cm_GetBuffer rx_Readv returns %d", temp);
1917 if (temp != length_found && temp < cm_data.buf_blockSize) {
1919 * If the file server returned (filesize - offset),
1920 * then the first rx_Read will return zero octets of data.
1921 * If it does, do not treat it as an error. Correct the
1922 * length_found and continue as if the file server said
1923 * it was sending us zero octets of data.
1925 if (fs_fetchdata_offset_bug && first_read)
1928 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1936 while (rbytes > 0) {
1939 osi_assertx(bufferp != NULL, "null cm_buf_t");
1941 len = MIN(tiov[iov].iov_len - iov_offset, cm_data.buf_blockSize - buffer_offset);
1942 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
1944 buffer_offset += len;
1947 if (iov_offset == tiov[iov].iov_len) {
1952 if (buffer_offset == cm_data.buf_blockSize) {
1953 /* allow read-while-fetching.
1954 * if this is the last buffer, clear the
1955 * PREFETCHING flag, so the reader waiting for
1956 * this buffer will start a prefetch.
1958 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1959 lock_ObtainWrite(&scp->rw);
1960 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1961 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
1962 osi_Wakeup((LONG_PTR) &scp->flags);
1964 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1965 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
1967 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
1969 lock_ReleaseWrite(&scp->rw);
1971 /* Advance the buffer */
1972 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1974 tbufp = osi_GetQData(qdp);
1975 bufferp = tbufp->datap;
1983 length_found -= temp;
1984 #else /* USE_RX_IOVEC */
1985 /* assert that there are still more buffers;
1986 * our check above for length_found being less than
1987 * biod.length should ensure this.
1989 osi_assertx(bufferp != NULL, "null cm_buf_t");
1991 /* read rbytes of data */
1992 rbytes = (afs_uint32)(length_found > cm_data.buf_blockSize ? cm_data.buf_blockSize : length_found);
1993 temp = rx_Read(rxcallp, bufferp, rbytes);
1994 if (temp < rbytes) {
1996 * If the file server returned (filesize - offset),
1997 * then the first rx_Read will return zero octets of data.
1998 * If it does, do not treat it as an error. Correct the
1999 * length_found and continue as if the file server said
2000 * it was sending us zero octets of data.
2002 if (fs_fetchdata_offset_bug && first_read)
2005 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
2010 /* allow read-while-fetching.
2011 * if this is the last buffer, clear the
2012 * PREFETCHING flag, so the reader waiting for
2013 * this buffer will start a prefetch.
2015 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
2016 lock_ObtainWrite(&scp->rw);
2017 if (scp->flags & CM_SCACHEFLAG_WAITING) {
2018 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
2019 osi_Wakeup((LONG_PTR) &scp->flags);
2021 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
2022 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
2024 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
2026 lock_ReleaseWrite(&scp->rw);
2028 /* and adjust counters */
2029 length_found -= temp;
2031 /* and move to the next buffer */
2032 if (length_found != 0) {
2033 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2035 tbufp = osi_GetQData(qdp);
2036 bufferp = tbufp->datap;
2042 #endif /* USE_RX_IOVEC */
2045 /* zero out remainder of last pages, in case we are
2046 * fetching past EOF. We were fetching an integral #
2047 * of pages, but stopped, potentially in the middle of
2048 * a page. Zero the remainder of that page, and then
2049 * all of the rest of the pages.
2052 rbytes = cm_data.buf_blockSize - buffer_offset;
2053 bufferp = tbufp->datap + buffer_offset;
2054 #else /* USE_RX_IOVEC */
2056 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
2057 rbytes = (long) (bufferp - tbufp->datap);
2059 /* bytes left to zero */
2060 rbytes = cm_data.buf_blockSize - rbytes;
2061 #endif /* USE_RX_IOVEC */
2064 memset(bufferp, 0, rbytes);
2065 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
2068 tbufp = osi_GetQData(qdp);
2069 bufferp = tbufp->datap;
2070 /* bytes to clear in this page */
2071 rbytes = cm_data.buf_blockSize;
2077 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2079 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2082 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2084 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2088 code1 = rx_EndCall(rxcallp, code);
2090 if (code1 == RXKADUNKNOWNKEY)
2091 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2093 /* If we are avoiding a file server bug, ignore the error state */
2094 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2095 /* Clone the current status info and clear the error state */
2096 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2098 lock_ReleaseWrite(&scp->rw);
2102 /* Prefer the error value from FetchData over rx_EndCall */
2103 } else if (code == 0 && code1 != 0)
2105 osi_Log0(afsd_logp, "CALL FetchData DONE");
2107 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
2110 code = cm_MapRPCError(code, reqp);
2113 lock_ObtainWrite(&scp->rw);
2115 /* we know that no one else has changed the buffer, since we still have
2116 * the fetching flag on the buffers, and we have the scp locked again.
2117 * Copy in the version # into the buffer if we got code 0 back from the
2121 for(qdp = biod.bufListp;
2123 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
2124 tbufp = osi_GetQData(qdp);
2125 tbufp->dataVersion = afsStatus.dataVersionHigh;
2126 tbufp->dataVersion <<= 32;
2127 tbufp->dataVersion |= afsStatus.DataVersion;
2130 /* write buffer out to disk cache */
2131 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
2132 tbufp->dataVersion);
2133 #endif /* DISKCACHE95 */
2137 /* release scatter/gather I/O structure (buffers, locks) */
2138 cm_ReleaseBIOD(&biod, 0, code, 1);
2141 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
2147 * Similar to cm_GetBuffer but doesn't use an allocated cm_buf_t object.
2148 * Instead the data is read from the file server and copied directly into
2149 * a provided buffer. Called with scp locked. The scp is locked on return.
2151 long cm_GetData(cm_scache_t *scp, osi_hyper_t *offsetp, char *datap, int data_length,
2152 cm_user_t *userp, cm_req_t *reqp)
2154 long code=0, code1=0;
2155 afs_uint32 nbytes; /* bytes in transfer */
2156 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
2157 afs_uint64 length_found = 0;
2158 char *bufferp = datap;
2159 afs_uint32 buffer_offset = 0;
2160 long rbytes; /* bytes in rx_Read call */
2162 AFSFetchStatus afsStatus;
2163 AFSCallBack callback;
2166 struct rx_call *rxcallp;
2167 struct rx_connection *rxconnp;
2171 int require_64bit_ops = 0;
2172 int call_was_64bit = 0;
2173 int fs_fetchdata_offset_bug = 0;
2177 memset(&volSync, 0, sizeof(volSync));
2179 /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
2180 * drops lots of locks, and may indeed return a properly initialized
2181 * buffer, although more likely it will just return a new, empty, buffer.
2184 #ifdef AFS_FREELANCE_CLIENT
2186 // yj: if they're trying to get the /afs directory, we need to
2187 // handle it differently, since it's local rather than on any
2190 getroot = (scp==cm_data.rootSCachep);
2192 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
2195 cm_AFSFidFromFid(&tfid, &scp->fid);
2197 if (LargeIntegerGreaterThan(LargeIntegerAdd(*offsetp,
2198 ConvertLongToLargeInteger(data_length)),
2199 ConvertLongToLargeInteger(LONG_MAX))) {
2200 require_64bit_ops = 1;
2203 osi_Log2(afsd_logp, "cm_GetData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
2205 #ifdef AFS_FREELANCE_CLIENT
2208 // if getroot then we don't need to make any calls
2209 // just return fake data
2211 if (cm_freelanceEnabled && getroot) {
2212 // setup the fake status
2213 afsStatus.InterfaceVersion = 0x1;
2214 afsStatus.FileType = 0x2;
2215 afsStatus.LinkCount = scp->linkCount;
2216 afsStatus.Length = cm_fakeDirSize;
2217 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
2218 afsStatus.Author = 0x1;
2219 afsStatus.Owner = 0x0;
2220 afsStatus.CallerAccess = 0x9;
2221 afsStatus.AnonymousAccess = 0x9;
2222 afsStatus.UnixModeBits = 0x1ff;
2223 afsStatus.ParentVnode = 0x1;
2224 afsStatus.ParentUnique = 0x1;
2225 afsStatus.ResidencyMask = 0;
2226 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
2227 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
2228 afsStatus.Group = 0;
2229 afsStatus.SyncCounter = 0;
2230 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
2231 afsStatus.lockCount = 0;
2232 afsStatus.Length_hi = 0;
2233 afsStatus.errorCode = 0;
2234 memset(&volSync, 0, sizeof(volSync));
2236 // once we're done setting up the status info,
2237 // we just fill the buffer pages with fakedata
2238 // from cm_FakeRootDir. Extra pages are set to
2241 lock_ObtainMutex(&cm_Freelance_Lock);
2242 t1 = offsetp->LowPart;
2243 memset(datap, 0, data_length);
2244 t2 = cm_fakeDirSize - t1;
2245 if (t2 > data_length)
2248 memcpy(datap, cm_FakeRootDir+t1, t2);
2249 lock_ReleaseMutex(&cm_Freelance_Lock);
2251 // once we're done, we skip over the part of the
2252 // code that does the ACTUAL fetching of data for
2255 goto fetchingcompleted;
2258 #endif /* AFS_FREELANCE_CLIENT */
2261 lock_ReleaseWrite(&scp->rw);
2265 /* now make the call */
2267 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2271 rxconnp = cm_GetRxConn(connp);
2272 rxcallp = rx_NewCall(rxconnp);
2273 rx_PutConnection(rxconnp);
2275 nbytes = nbytes_hi = 0;
2277 if (SERVERHAS64BIT(connp)) {
2280 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
2281 scp, offsetp->HighPart, offsetp->LowPart, data_length);
2283 code = StartRXAFS_FetchData64(rxcallp, &tfid, offsetp->QuadPart, data_length);
2286 temp = rx_Read32(rxcallp, &nbytes_hi);
2287 if (temp == sizeof(afs_int32)) {
2288 nbytes_hi = ntohl(nbytes_hi);
2291 code = rxcallp->error;
2292 code1 = rx_EndCall(rxcallp, code);
2300 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
2301 if (require_64bit_ops) {
2302 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
2303 code = CM_ERROR_TOOBIG;
2306 rxconnp = cm_GetRxConn(connp);
2307 rxcallp = rx_NewCall(rxconnp);
2308 rx_PutConnection(rxconnp);
2311 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
2312 scp, offsetp->LowPart, data_length);
2314 code = StartRXAFS_FetchData(rxcallp, &tfid, offsetp->LowPart, data_length);
2316 SET_SERVERHASNO64BIT(connp);
2321 temp = rx_Read32(rxcallp, &nbytes);
2322 if (temp == sizeof(afs_int32)) {
2323 nbytes = ntohl(nbytes);
2324 FillInt64(length_found, nbytes_hi, nbytes);
2325 if (length_found > data_length) {
2327 * prior to 1.4.12 and 1.5.65 the file server would return
2328 * (filesize - offset) if the requested offset was greater than
2329 * the filesize. The correct return value would have been zero.
2330 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
2331 * is a race between two RPCs issues by this cache manager, the
2332 * correct thing will happen the second time.
2334 osi_Log0(afsd_logp, "cm_GetData length_found > data_length");
2335 fs_fetchdata_offset_bug = 1;
2338 osi_Log1(afsd_logp, "cm_GetData rx_Read32 returns %d != 4", temp);
2339 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
2342 /* for the moment, nbytes_hi will always be 0 if code == 0
2343 because data_length is a 32-bit quantity. */
2346 /* fill length_found of data from the pipe into the pages.
2347 * When we stop, qdp will point at the last page we're
2348 * dealing with, and bufferp will tell us where we
2349 * stopped. We'll need this info below when we clear
2350 * the remainder of the last page out (and potentially
2351 * clear later pages out, if we fetch past EOF).
2353 while (length_found > 0) {
2355 struct iovec tiov[RX_MAXIOVECS];
2356 afs_int32 tnio, iov, iov_offset;
2358 temp = rx_Readv(rxcallp, tiov, &tnio, RX_MAXIOVECS, length_found);
2359 osi_Log1(afsd_logp, "cm_GetData rx_Readv returns %d", temp);
2360 if (temp != length_found && temp < data_length) {
2362 * If the file server returned (filesize - offset),
2363 * then the first rx_Read will return zero octets of data.
2364 * If it does, do not treat it as an error. Correct the
2365 * length_found and continue as if the file server said
2366 * it was sending us zero octets of data.
2368 if (fs_fetchdata_offset_bug && first_read)
2371 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
2379 while (rbytes > 0) {
2382 osi_assertx(bufferp != NULL, "null cm_buf_t");
2384 len = MIN(tiov[iov].iov_len - iov_offset, data_length - buffer_offset);
2385 memcpy(bufferp + buffer_offset, tiov[iov].iov_base + iov_offset, len);
2387 buffer_offset += len;
2390 if (iov_offset == tiov[iov].iov_len) {
2396 length_found -= temp;
2397 #else /* USE_RX_IOVEC */
2398 /* assert that there are still more buffers;
2399 * our check above for length_found being less than
2400 * data_length should ensure this.
2402 osi_assertx(bufferp != NULL, "null cm_buf_t");
2404 /* read rbytes of data */
2405 rbytes = (afs_uint32)(length_found > data_length ? data_length : length_found);
2406 temp = rx_Read(rxcallp, bufferp, rbytes);
2407 if (temp < rbytes) {
2409 * If the file server returned (filesize - offset),
2410 * then the first rx_Read will return zero octets of data.
2411 * If it does, do not treat it as an error. Correct the
2412 * length_found and continue as if the file server said
2413 * it was sending us zero octets of data.
2415 if (fs_fetchdata_offset_bug && first_read)
2418 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
2423 /* and adjust counters */
2424 length_found -= temp;
2425 #endif /* USE_RX_IOVEC */
2428 /* zero out remainder of last pages, in case we are
2429 * fetching past EOF. We were fetching an integral #
2430 * of pages, but stopped, potentially in the middle of
2431 * a page. Zero the remainder of that page, and then
2432 * all of the rest of the pages.
2435 rbytes = data_length - buffer_offset;
2436 bufferp = datap + buffer_offset;
2437 #else /* USE_RX_IOVEC */
2439 osi_assertx((bufferp - datap) < LONG_MAX, "data >= LONG_MAX");
2440 rbytes = (long) (bufferp - datap);
2442 /* bytes left to zero */
2443 rbytes = data_length - rbytes;
2444 #endif /* USE_RX_IOVEC */
2446 memset(bufferp, 0, rbytes);
2451 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
2453 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
2456 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
2458 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
2462 code1 = rx_EndCall(rxcallp, code);
2464 if (code1 == RXKADUNKNOWNKEY)
2465 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
2467 /* If we are avoiding a file server bug, ignore the error state */
2468 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
2469 /* Clone the current status info and clear the error state */
2470 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
2472 lock_ReleaseWrite(&scp->rw);
2476 /* Prefer the error value from FetchData over rx_EndCall */
2477 } else if (code == 0 && code1 != 0)
2479 osi_Log0(afsd_logp, "CALL FetchData DONE");
2481 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
2484 code = cm_MapRPCError(code, reqp);
2487 lock_ObtainWrite(&scp->rw);
2490 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);