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
10 #include <afs/param.h>
26 extern void afsi_log(char *pattern, ...);
29 #ifdef AFS_FREELANCE_CLIENT
30 extern osi_mutex_t cm_Freelance_Lock;
33 /* we can access connp->serverp without holding a lock because that
34 never changes since the connection is made. */
35 #define SERVERHAS64BIT(connp) (!((connp)->serverp->flags & CM_SERVERFLAG_NO64BIT))
36 #define SET_SERVERHASNO64BIT(connp) (cm_SetServerNo64Bit((connp)->serverp, TRUE))
38 /* functions called back from the buffer package when reading or writing data,
39 * or when holding or releasing a vnode pointer.
41 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
42 cm_user_t *userp, cm_req_t *reqp)
44 /* store the data back from this buffer; the buffer is locked and held,
45 * but the vnode involved isn't locked, yet. It is held by its
46 * reference from the buffer, which won't change until the buffer is
47 * released by our caller. Thus, we don't have to worry about holding
51 cm_scache_t *scp = vscp;
53 afs_int32 save_nbytes;
55 AFSFetchStatus outStatus;
56 AFSStoreStatus inStatus;
60 struct rx_call *rxcallp;
61 struct rx_connection *rxconnp;
68 cm_bulkIO_t biod; /* bulk IO descriptor */
69 int require_64bit_ops = 0;
70 int call_was_64bit = 0;
71 int scp_locked = flags & CM_BUF_WRITE_SCP_LOCKED;
73 osi_assertx(userp != NULL, "null cm_user_t");
74 osi_assertx(scp != NULL, "null cm_scache_t");
76 memset(&volSync, 0, sizeof(volSync));
78 /* now, the buffer may or may not be filled with good data (buf_GetNew
79 * drops lots of locks, and may indeed return a properly initialized
80 * buffer, although more likely it will just return a new, empty, buffer.
83 lock_ObtainWrite(&scp->rw);
84 if (scp->flags & CM_SCACHEFLAG_DELETED) {
86 lock_ReleaseWrite(&scp->rw);
87 return CM_ERROR_NOSUCHFILE;
90 cm_AFSFidFromFid(&tfid, &scp->fid);
92 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
93 (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
95 code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
97 osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
98 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
100 lock_ReleaseWrite(&scp->rw);
104 if (biod.length == 0) {
105 osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
106 cm_ReleaseBIOD(&biod, 1, 0, 1); /* should be a NOOP */
107 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
109 lock_ReleaseWrite(&scp->rw);
113 /* prepare the output status for the store */
114 scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
115 cm_StatusFromAttr(&inStatus, scp, NULL);
116 truncPos = scp->length;
117 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
118 && LargeIntegerLessThan(scp->truncPos, truncPos))
119 truncPos = scp->truncPos;
120 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
122 /* compute how many bytes to write from this buffer */
123 thyper = LargeIntegerSubtract(scp->length, biod.offset);
124 if (LargeIntegerLessThanZero(thyper)) {
125 /* entire buffer is past EOF */
129 /* otherwise write out part of buffer before EOF, but not
130 * more than bufferSize bytes.
132 if (LargeIntegerGreaterThan(thyper,
133 ConvertLongToLargeInteger(biod.length))) {
134 nbytes = biod.length;
136 /* if thyper is less than or equal to biod.length, then we
137 can safely assume that the value fits in a long. */
138 nbytes = thyper.LowPart;
142 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
143 ConvertLongToLargeInteger(nbytes)),
144 ConvertLongToLargeInteger(LONG_MAX)) ||
145 LargeIntegerGreaterThan(truncPos,
146 ConvertLongToLargeInteger(LONG_MAX))) {
147 require_64bit_ops = 1;
150 lock_ReleaseWrite(&scp->rw);
152 /* now we're ready to do the store operation */
153 save_nbytes = nbytes;
155 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
160 rxconnp = cm_GetRxConn(connp);
161 rxcallp = rx_NewCall(rxconnp);
162 rx_PutConnection(rxconnp);
164 if (SERVERHAS64BIT(connp)) {
167 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
168 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
170 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
171 biod.offset.QuadPart,
175 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
177 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
181 if (require_64bit_ops) {
182 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData. The operation requires large file support in the server.");
183 code = CM_ERROR_TOOBIG;
185 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
186 scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
188 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
189 biod.offset.LowPart, nbytes, truncPos.LowPart);
191 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
193 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
198 /* write the data from the the list of buffers */
201 afs_uint32 buf_offset;
203 qdp = biod.bufListEndp;
204 buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
206 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
209 osi_assertx(qdp != NULL, "null osi_queueData_t");
210 bufp = osi_GetQData(qdp);
211 bufferp = bufp->datap + buf_offset;
213 if (wbytes > cm_data.buf_blockSize - buf_offset)
214 wbytes = cm_data.buf_blockSize - buf_offset;
216 /* write out wbytes of data from bufferp */
217 temp = rx_Write(rxcallp, bufferp, wbytes);
218 if (temp != wbytes) {
219 osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d",bufp,temp,wbytes);
220 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
223 osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p, %d",bufp,temp);
226 } /* while more bytes to write */
227 } /* if RPC started successfully */
230 if (call_was_64bit) {
231 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
233 osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
235 osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
237 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
239 osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
241 osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
245 code1 = rx_EndCall(rxcallp, code);
247 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
248 SET_SERVERHASNO64BIT(connp);
250 nbytes = save_nbytes;
254 /* Prefer StoreData error over rx_EndCall error */
257 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
259 code = cm_MapRPCError(code, reqp);
262 osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
264 osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
266 /* now, clean up our state */
267 lock_ObtainWrite(&scp->rw);
269 cm_ReleaseBIOD(&biod, 1, code, 1);
270 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
274 /* now, here's something a little tricky: in AFS 3, a dirty
275 * length can't be directly stored, instead, a dirty chunk is
276 * stored that sets the file's size (by writing and by using
277 * the truncate-first option in the store call).
279 * At this point, we've just finished a store, and so the trunc
280 * pos field is clean. If the file's size at the server is at
281 * least as big as we think it should be, then we turn off the
282 * length dirty bit, since all the other dirty buffers must
283 * precede this one in the file.
285 * The file's desired size shouldn't be smaller than what's
286 * stored at the server now, since we just did the trunc pos
289 * We have to turn off the length dirty bit as soon as we can,
290 * so that we see updates made by other machines.
293 if (call_was_64bit) {
294 t.LowPart = outStatus.Length;
295 t.HighPart = outStatus.Length_hi;
297 t = ConvertLongToLargeInteger(outStatus.Length);
300 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
301 scp->mask &= ~CM_SCACHEMASK_LENGTH;
303 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
305 if (code == CM_ERROR_SPACE)
306 scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
307 else if (code == CM_ERROR_QUOTA)
308 scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
311 lock_ReleaseWrite(&scp->rw);
317 * Truncate the file, by sending a StoreData RPC with zero length.
319 * Called with scp locked. Releases and re-obtains the lock.
321 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
323 AFSFetchStatus outStatus;
324 AFSStoreStatus inStatus;
328 osi_hyper_t truncPos;
330 struct rx_call *rxcallp;
331 struct rx_connection *rxconnp;
332 int require_64bit_ops = 0;
333 int call_was_64bit = 0;
335 memset(&volSync, 0, sizeof(volSync));
337 /* Serialize StoreData RPC's; for rationale see cm_scache.c */
338 (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
339 CM_SCACHESYNC_STOREDATA_EXCL);
341 /* prepare the output status for the store */
342 inStatus.Mask = AFS_SETMODTIME;
343 inStatus.ClientModTime = scp->clientModTime;
344 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
346 /* calculate truncation position */
347 truncPos = scp->length;
348 if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
349 && LargeIntegerLessThan(scp->truncPos, truncPos))
350 truncPos = scp->truncPos;
351 scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
353 if (LargeIntegerGreaterThan(truncPos,
354 ConvertLongToLargeInteger(LONG_MAX))) {
356 require_64bit_ops = 1;
359 lock_ReleaseWrite(&scp->rw);
361 cm_AFSFidFromFid(&tfid, &scp->fid);
363 /* now we're ready to do the store operation */
365 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
370 rxconnp = cm_GetRxConn(connp);
371 rxcallp = rx_NewCall(rxconnp);
372 rx_PutConnection(rxconnp);
374 if (SERVERHAS64BIT(connp)) {
377 code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
378 0, 0, truncPos.QuadPart);
382 if (require_64bit_ops) {
383 code = CM_ERROR_TOOBIG;
385 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
386 0, 0, truncPos.LowPart);
392 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
394 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
396 code1 = rx_EndCall(rxcallp, code);
398 if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
399 SET_SERVERHASNO64BIT(connp);
403 /* prefer StoreData error over rx_EndCall error */
404 if (code == 0 && code1 != 0)
406 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
407 code = cm_MapRPCError(code, reqp);
409 /* now, clean up our state */
410 lock_ObtainWrite(&scp->rw);
412 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
417 * For explanation of handling of CM_SCACHEMASK_LENGTH,
420 if (call_was_64bit) {
421 t.HighPart = outStatus.Length_hi;
422 t.LowPart = outStatus.Length;
424 t = ConvertLongToLargeInteger(outStatus.Length);
427 if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
428 scp->mask &= ~CM_SCACHEMASK_LENGTH;
429 cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
435 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
439 /* now return a code that means that I/O is done */
444 * stabilize scache entry with CM_SCACHESYNC_SETSIZE. This prevents any new
445 * data buffers to be allocated, new data to be fetched from the file server,
446 * and writes to be accepted from the application but permits dirty buffers
447 * to be written to the file server.
449 * Stabilize uses cm_SyncOp to maintain the cm_scache_t in this stable state
450 * instead of holding the rwlock exclusively. This permits background stores
451 * to be performed in parallel and in particular allow FlushFile to be
452 * implemented without violating the locking hierarchy.
454 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
456 cm_scache_t *scp = vscp;
459 lock_ObtainWrite(&scp->rw);
460 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
461 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
462 lock_ReleaseWrite(&scp->rw);
467 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
468 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
470 cm_scache_t *scp = vscp;
472 lock_ObtainWrite(&scp->rw);
473 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
475 lock_ReleaseWrite(&scp->rw);
477 /* always succeeds */
481 cm_buf_ops_t cm_bufOps = {
488 long cm_ValidateDCache(void)
490 return buf_ValidateBuffers();
493 long cm_ShutdownDCache(void)
498 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
500 return buf_Init(newFile, &cm_bufOps, nbuffers);
503 /* check to see if we have an up-to-date buffer. The buffer must have
504 * previously been obtained by calling buf_Get.
506 * Make sure we have a callback, and that the dataversion matches.
508 * Scp must be locked.
510 * Bufp *may* be locked.
512 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
515 if (!cm_HaveCallback(scp))
517 if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
519 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
521 if (bufp->offset.QuadPart >= scp->serverLength.QuadPart)
524 code = lock_TryMutex(&bufp->mx);
526 /* don't have the lock, and can't lock it, then
533 /* remember dirty flag for later */
534 code = bufp->flags & CM_BUF_DIRTY;
536 /* release lock if we obtained it here */
538 lock_ReleaseMutex(&bufp->mx);
540 /* if buffer was dirty, buffer is acceptable for use */
548 * used when deciding whether to do a background fetch or not.
549 * call with scp->rw write-locked.
552 cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
553 cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
557 osi_hyper_t tblocksize;
562 /* now scan all buffers in the range, looking for any that look like
567 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
569 while (LargeIntegerGreaterThanZero(tlength)) {
570 /* get callback so we can do a meaningful dataVersion comparison */
571 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
572 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
576 if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
577 /* we're past the end of file */
581 bp = buf_Find(scp, &tbase);
582 /* We cheat slightly by not locking the bp mutex. */
584 if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING | CM_BUF_CMBKGFETCH)) == 0
585 && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
593 /* if this buffer is essentially guaranteed to require a fetch,
594 * break out here and return this position.
599 tbase = LargeIntegerAdd(tbase, tblocksize);
600 tlength = LargeIntegerSubtract(tlength, tblocksize);
603 /* if we get here, either everything is fine or 'stop' stopped us at a
604 * particular buffer in the range that definitely needs to be fetched.
607 /* return non-zero code since realBasep won't be valid */
611 /* successfully found a page that will need fetching */
619 cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
627 if (scp->flags & CM_SCACHEFLAG_DELETED) {
628 osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
632 /* Retries will be performed by the BkgDaemon thread if appropriate */
633 req.flags |= CM_REQ_NORETRY;
635 toffset.LowPart = p1;
636 toffset.HighPart = p2;
639 osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
641 code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, &req);
643 osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
647 * Keep the following list synchronized with the
648 * error code list in cm_BkgDaemon
651 case CM_ERROR_TIMEDOUT: /* or server restarting */
653 case CM_ERROR_WOULDBLOCK:
654 case CM_ERROR_ALLBUSY:
655 case CM_ERROR_ALLDOWN:
656 case CM_ERROR_ALLOFFLINE:
657 case CM_ERROR_PARTIALWRITE:
658 break; /* cm_BkgDaemon will re-insert the request in the queue */
661 lock_ObtainWrite(&scp->rw);
662 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
663 lock_ReleaseWrite(&scp->rw);
668 /* Called with scp locked */
669 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
674 end = LargeIntegerAdd(*base, *length);
675 if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
676 scp->prefetch.base = *base;
677 if (LargeIntegerGreaterThan(end, scp->prefetch.end))
678 scp->prefetch.end = end;
680 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
683 /* do the prefetch. if the prefetch fails, return 0 (success)
684 * because there is no harm done. */
686 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
694 osi_hyper_t tblocksize;
702 /* Retries will be performed by the BkgDaemon thread if appropriate */
703 req.flags |= CM_REQ_NORETRY;
706 fetched.HighPart = 0;
707 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
711 length.HighPart = p4;
713 end = LargeIntegerAdd(base, length);
715 osi_Log5(afsd_logp, "Starting BKG prefetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
716 scp, p2, p1, p4, p3);
718 for ( code = 0, offset = base;
719 code == 0 && LargeIntegerLessThan(offset, end);
720 offset = LargeIntegerAdd(offset, tblocksize) )
723 lock_ReleaseWrite(&scp->rw);
727 code = buf_Get(scp, &offset, &req, &bp);
731 if (bp->cmFlags & CM_BUF_CMFETCHING) {
732 /* skip this buffer as another thread is already fetching it */
734 lock_ObtainWrite(&scp->rw);
737 bp->cmFlags &= ~CM_BUF_CMBKGFETCH;
744 lock_ObtainWrite(&scp->rw);
748 code = cm_GetBuffer(scp, bp, NULL, userp, &req);
750 fetched = LargeIntegerAdd(fetched, tblocksize);
752 bp->cmFlags &= ~CM_BUF_CMBKGFETCH;
756 lock_ObtainWrite(&scp->rw);
760 /* Clear flag from any remaining buffers */
762 LargeIntegerLessThan(offset, end);
763 offset = LargeIntegerAdd(offset, tblocksize) )
765 bp = buf_Find(scp, &offset);
767 bp->cmFlags &= ~CM_BUF_CMBKGFETCH;
771 cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code,
772 scp, &base, &fetched);
774 /* wakeup anyone who is waiting */
775 if (scp->flags & CM_SCACHEFLAG_WAITING) {
776 osi_Log1(afsd_logp, "CM BkgPrefetch Waking scp 0x%p", scp);
777 osi_Wakeup((LONG_PTR) &scp->flags);
779 lock_ReleaseWrite(&scp->rw);
781 osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p code 0x%x fetched 0x%x:%x",
782 scp, code, fetched.HighPart, fetched.LowPart);
786 /* a read was issued to offsetp, and we have to determine whether we should
787 * do a prefetch of the next chunk.
789 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
790 cm_user_t *userp, cm_req_t *reqp)
794 osi_hyper_t realBase;
795 osi_hyper_t readBase;
796 osi_hyper_t readLength;
799 osi_hyper_t tblocksize; /* a long long temp variable */
802 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
805 /* round up to chunk boundary */
806 readBase.LowPart += (cm_chunkSize-1);
807 readBase.LowPart &= (-cm_chunkSize);
809 readLength = ConvertLongToLargeInteger(count);
811 lock_ObtainWrite(&scp->rw);
813 if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
814 || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
815 lock_ReleaseWrite(&scp->rw);
818 scp->flags |= CM_SCACHEFLAG_PREFETCHING;
820 /* start the scan at the latter of the end of this read or
821 * the end of the last fetched region.
823 if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
824 readBase = scp->prefetch.end;
826 code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
829 scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
830 lock_ReleaseWrite(&scp->rw);
831 return; /* can't find something to prefetch */
834 readEnd = LargeIntegerAdd(realBase, readLength);
837 * Mark each buffer in the range as queued for a
840 for ( offset = realBase;
841 LargeIntegerLessThan(offset, readEnd);
842 offset = LargeIntegerAdd(offset, tblocksize) )
845 lock_ReleaseWrite(&scp->rw);
849 bp = buf_Find(scp, &offset);
854 lock_ObtainWrite(&scp->rw);
858 bp->cmFlags |= CM_BUF_CMBKGFETCH;
863 lock_ReleaseWrite(&scp->rw);
865 osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
866 scp, realBase.LowPart);
868 cm_QueueBKGRequest(scp, cm_BkgPrefetch,
869 realBase.LowPart, realBase.HighPart,
870 readLength.LowPart, readLength.HighPart,
874 /* scp must be locked; temporarily unlocked during processing.
875 * If returns 0, returns buffers held in biop, and with
876 * CM_BUF_CMSTORING set.
878 * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
879 * buffer is ever unlocked before CM_BUF_DIRTY is cleared. And if
880 * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
881 * must be woken, and the event must be set when the I/O is done. All of this
882 * is required so that buf_WaitIO synchronizes properly with the buffer as it
883 * is being written out.
885 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
886 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
889 osi_queueData_t *qdp;
892 osi_hyper_t scanStart; /* where to start scan for dirty pages */
893 osi_hyper_t scanEnd; /* where to stop scan for dirty pages */
894 osi_hyper_t firstModOffset; /* offset of first modified page in range */
897 long flags; /* flags to cm_SyncOp */
899 /* clear things out */
900 biop->scp = scp; /* do not hold; held by caller */
901 biop->offset = *inOffsetp;
903 biop->bufListp = NULL;
904 biop->bufListEndp = NULL;
907 /* reserve a chunk's worth of buffers */
908 lock_ReleaseWrite(&scp->rw);
909 buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
910 lock_ObtainWrite(&scp->rw);
913 for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
914 thyper = ConvertLongToLargeInteger(temp);
915 tbase = LargeIntegerAdd(*inOffsetp, thyper);
917 bufp = buf_Find(scp, &tbase);
919 /* get buffer mutex and scp mutex safely */
920 lock_ReleaseWrite(&scp->rw);
921 lock_ObtainMutex(&bufp->mx);
924 * if the buffer is actively involved in I/O
925 * we wait for the I/O to complete.
927 if (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING))
928 buf_WaitIO(scp, bufp);
930 lock_ObtainWrite(&scp->rw);
931 flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
932 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
934 lock_ReleaseMutex(&bufp->mx);
937 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
941 /* if the buffer is dirty, we're done */
942 if (bufp->flags & CM_BUF_DIRTY) {
943 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
944 "WRITING w/o CMSTORING in SetupStoreBIOD");
945 bufp->flags |= CM_BUF_WRITING;
949 /* this buffer is clean, so there's no reason to process it */
950 cm_SyncOpDone(scp, bufp, flags);
951 lock_ReleaseMutex(&bufp->mx);
959 /* if we get here, if bufp is null, we didn't find any dirty buffers
960 * that weren't already being stored back, so we just quit now.
966 /* don't need buffer mutex any more */
967 lock_ReleaseMutex(&bufp->mx);
969 /* put this element in the list */
971 osi_SetQData(qdp, bufp);
972 /* don't have to hold bufp, since held by buf_Find above */
973 osi_QAddH((osi_queue_t **) &biop->bufListp,
974 (osi_queue_t **) &biop->bufListEndp,
976 biop->length = cm_data.buf_blockSize;
977 firstModOffset = bufp->offset;
978 biop->offset = firstModOffset;
979 bufp = NULL; /* this buffer and reference added to the queue */
981 /* compute the window surrounding *inOffsetp of size cm_chunkSize */
982 scanStart = *inOffsetp;
983 scanStart.LowPart &= (-cm_chunkSize);
984 thyper = ConvertLongToLargeInteger(cm_chunkSize);
985 scanEnd = LargeIntegerAdd(scanStart, thyper);
987 flags = CM_SCACHESYNC_GETSTATUS
988 | CM_SCACHESYNC_STOREDATA
989 | CM_SCACHESYNC_BUFLOCKED
990 | CM_SCACHESYNC_NOWAIT;
992 /* start by looking backwards until scanStart */
993 /* hyper version of cm_data.buf_blockSize */
994 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
995 tbase = LargeIntegerSubtract(firstModOffset, thyper);
996 while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
997 /* see if we can find the buffer */
998 bufp = buf_Find(scp, &tbase);
1002 /* try to lock it, and quit if we can't (simplifies locking) */
1003 lock_ReleaseWrite(&scp->rw);
1004 code = lock_TryMutex(&bufp->mx);
1005 lock_ObtainWrite(&scp->rw);
1012 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1014 lock_ReleaseMutex(&bufp->mx);
1020 if (!(bufp->flags & CM_BUF_DIRTY)) {
1021 /* buffer is clean, so we shouldn't add it */
1022 cm_SyncOpDone(scp, bufp, flags);
1023 lock_ReleaseMutex(&bufp->mx);
1029 /* don't need buffer mutex any more */
1030 lock_ReleaseMutex(&bufp->mx);
1032 /* we have a dirty buffer ready for storing. Add it to the tail
1033 * of the list, since it immediately precedes all of the disk
1034 * addresses we've already collected.
1036 qdp = osi_QDAlloc();
1037 osi_SetQData(qdp, bufp);
1038 /* no buf_hold necessary, since we have it held from buf_Find */
1039 osi_QAddT((osi_queue_t **) &biop->bufListp,
1040 (osi_queue_t **) &biop->bufListEndp,
1042 bufp = NULL; /* added to the queue */
1044 /* update biod info describing the transfer */
1045 biop->offset = LargeIntegerSubtract(biop->offset, thyper);
1046 biop->length += cm_data.buf_blockSize;
1048 /* update loop pointer */
1049 tbase = LargeIntegerSubtract(tbase, thyper);
1050 } /* while loop looking for pages preceding the one we found */
1052 /* now, find later dirty, contiguous pages, and add them to the list */
1053 /* hyper version of cm_data.buf_blockSize */
1054 thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1055 tbase = LargeIntegerAdd(firstModOffset, thyper);
1056 while(LargeIntegerLessThan(tbase, scanEnd)) {
1057 /* see if we can find the buffer */
1058 bufp = buf_Find(scp, &tbase);
1062 /* try to lock it, and quit if we can't (simplifies locking) */
1063 lock_ReleaseWrite(&scp->rw);
1064 code = lock_TryMutex(&bufp->mx);
1065 lock_ObtainWrite(&scp->rw);
1072 code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1074 lock_ReleaseMutex(&bufp->mx);
1080 if (!(bufp->flags & CM_BUF_DIRTY)) {
1081 /* buffer is clean, so we shouldn't add it */
1082 cm_SyncOpDone(scp, bufp, flags);
1083 lock_ReleaseMutex(&bufp->mx);
1089 /* don't need buffer mutex any more */
1090 lock_ReleaseMutex(&bufp->mx);
1092 /* we have a dirty buffer ready for storing. Add it to the head
1093 * of the list, since it immediately follows all of the disk
1094 * addresses we've already collected.
1096 qdp = osi_QDAlloc();
1097 osi_SetQData(qdp, bufp);
1098 /* no buf_hold necessary, since we have it held from buf_Find */
1099 osi_QAddH((osi_queue_t **) &biop->bufListp,
1100 (osi_queue_t **) &biop->bufListEndp,
1104 /* update biod info describing the transfer */
1105 biop->length += cm_data.buf_blockSize;
1107 /* update loop pointer */
1108 tbase = LargeIntegerAdd(tbase, thyper);
1109 } /* while loop looking for pages following the first page we found */
1111 /* finally, we're done */
1115 /* scp must be locked; temporarily unlocked during processing.
1116 * If returns 0, returns buffers held in biop, and with
1117 * CM_BUF_CMFETCHING flags set.
1118 * If an error is returned, we don't return any buffers.
1120 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1121 cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1125 osi_hyper_t tblocksize; /* a long long temp variable */
1126 osi_hyper_t pageBase; /* base offset we're looking at */
1127 osi_queueData_t *qdp; /* one temp queue structure */
1128 osi_queueData_t *tqdp; /* another temp queue structure */
1129 long collected; /* how many bytes have been collected */
1132 osi_hyper_t fileSize; /* the # of bytes in the file */
1133 osi_queueData_t *heldBufListp; /* we hold all buffers in this list */
1134 osi_queueData_t *heldBufListEndp; /* first one */
1137 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1139 biop->scp = scp; /* do not hold; held by caller */
1140 biop->offset = *offsetp;
1141 /* null out the list of buffers */
1142 biop->bufListp = biop->bufListEndp = NULL;
1145 /* first lookup the file's length, so we know when to stop */
1146 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
1147 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1151 /* copy out size, since it may change */
1152 fileSize = scp->serverLength;
1154 lock_ReleaseWrite(&scp->rw);
1156 pageBase = *offsetp;
1157 collected = pageBase.LowPart & (cm_chunkSize - 1);
1158 heldBufListp = NULL;
1159 heldBufListEndp = NULL;
1162 * Obtaining buffers can cause dirty buffers to be recycled, which
1163 * can cause a storeback, so cannot be done while we have buffers
1166 * To get around this, we get buffers twice. Before reserving buffers,
1167 * we obtain and release each one individually. After reserving
1168 * buffers, we try to obtain them again, but only by lookup, not by
1169 * recycling. If a buffer has gone away while we were waiting for
1170 * the others, we just use whatever buffers we already have.
1172 * On entry to this function, we are already holding a buffer, so we
1173 * can't wait for reservation. So we call buf_TryReserveBuffers()
1174 * instead. Not only that, we can't really even call buf_Get(), for
1175 * the same reason. We can't avoid that, though. To avoid deadlock
1176 * we allow only one thread to be executing the buf_Get()-buf_Release()
1177 * sequence at a time.
1180 /* first hold all buffers, since we can't hold any locks in buf_Get */
1182 /* stop at chunk boundary */
1183 if (collected >= cm_chunkSize)
1186 /* see if the next page would be past EOF */
1187 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1190 code = buf_Get(scp, &pageBase, reqp, &tbp);
1192 lock_ObtainWrite(&scp->rw);
1193 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1200 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1201 collected += cm_data.buf_blockSize;
1204 /* reserve a chunk's worth of buffers if possible */
1205 reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1207 pageBase = *offsetp;
1208 collected = pageBase.LowPart & (cm_chunkSize - 1);
1210 /* now hold all buffers, if they are still there */
1212 /* stop at chunk boundary */
1213 if (collected >= cm_chunkSize)
1216 /* see if the next page would be past EOF */
1217 if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize))
1220 tbp = buf_Find(scp, &pageBase);
1224 /* add the buffer to the list */
1225 qdp = osi_QDAlloc();
1226 osi_SetQData(qdp, tbp);
1227 osi_QAddH((osi_queue_t **)&heldBufListp,
1228 (osi_queue_t **)&heldBufListEndp,
1230 /* leave tbp held (from buf_Get) */
1235 collected += cm_data.buf_blockSize;
1236 pageBase = LargeIntegerAdd(tblocksize, pageBase);
1239 /* look at each buffer, adding it into the list if it looks idle and
1240 * filled with old data. One special case: wait for idle if it is the
1241 * first buffer since we really need that one for our caller to make
1245 collected = 0; /* now count how many we'll really use */
1246 for (tqdp = heldBufListEndp;
1248 tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1249 /* get a ptr to the held buffer */
1250 tbp = osi_GetQData(tqdp);
1251 pageBase = tbp->offset;
1253 /* now lock the buffer lock */
1254 lock_ObtainMutex(&tbp->mx);
1255 lock_ObtainWrite(&scp->rw);
1257 /* don't bother fetching over data that is already current */
1258 if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1259 /* we don't need this buffer, since it is current */
1260 lock_ReleaseWrite(&scp->rw);
1261 lock_ReleaseMutex(&tbp->mx);
1265 flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1267 flags |= CM_SCACHESYNC_NOWAIT;
1269 /* wait for the buffer to serialize, if required. Doesn't
1270 * release the scp or buffer lock(s) if NOWAIT is specified.
1272 code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1274 lock_ReleaseWrite(&scp->rw);
1275 lock_ReleaseMutex(&tbp->mx);
1279 /* don't fetch over dirty buffers */
1280 if (tbp->flags & CM_BUF_DIRTY) {
1281 cm_SyncOpDone(scp, tbp, flags);
1282 lock_ReleaseWrite(&scp->rw);
1283 lock_ReleaseMutex(&tbp->mx);
1288 lock_ReleaseWrite(&scp->rw);
1289 lock_ReleaseMutex(&tbp->mx);
1291 /* add the buffer to the list */
1292 qdp = osi_QDAlloc();
1293 osi_SetQData(qdp, tbp);
1294 osi_QAddH((osi_queue_t **)&biop->bufListp,
1295 (osi_queue_t **)&biop->bufListEndp,
1299 /* from now on, a failure just stops our collection process, but
1300 * we still do the I/O to whatever we've already managed to collect.
1303 collected += cm_data.buf_blockSize;
1306 /* now, we've held in biop->bufListp all the buffer's we're really
1307 * interested in. We also have holds left from heldBufListp, and we
1308 * now release those holds on the buffers.
1310 for (qdp = heldBufListp; qdp; qdp = tqdp) {
1311 tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1312 tbp = osi_GetQData(qdp);
1313 osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1314 (osi_queue_t **) &heldBufListEndp,
1321 /* Caller expects this */
1322 lock_ObtainWrite(&scp->rw);
1324 /* if we got a failure setting up the first buffer, then we don't have
1325 * any side effects yet, and we also have failed an operation that the
1326 * caller requires to make any progress. Give up now.
1328 if (code && isFirst) {
1329 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1333 /* otherwise, we're still OK, and should just return the I/O setup we've
1336 biop->length = collected;
1337 biop->reserved = reserving;
1341 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1344 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
1346 cm_scache_t *scp; /* do not release; not held in biop */
1348 osi_queueData_t *qdp;
1349 osi_queueData_t *nqdp;
1352 /* Give back reserved buffers */
1354 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1357 flags = CM_SCACHESYNC_STOREDATA;
1359 flags = CM_SCACHESYNC_FETCHDATA;
1362 if (biop->bufListp) {
1363 for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1364 /* lookup next guy first, since we're going to free this one */
1365 nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1367 /* extract buffer and free queue data */
1368 bufp = osi_GetQData(qdp);
1369 osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1370 (osi_queue_t **) &biop->bufListEndp,
1374 /* now, mark I/O as done, unlock the buffer and release it */
1376 lock_ReleaseWrite(&scp->rw);
1377 lock_ObtainMutex(&bufp->mx);
1378 lock_ObtainWrite(&scp->rw);
1379 cm_SyncOpDone(scp, bufp, flags);
1381 /* turn off writing and wakeup users */
1383 if (bufp->flags & CM_BUF_WAITING) {
1384 osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1385 osi_Wakeup((LONG_PTR) bufp);
1388 bufp->flags &= ~CM_BUF_WRITING;
1390 case CM_ERROR_NOSUCHFILE:
1391 case CM_ERROR_BADFD:
1392 case CM_ERROR_NOACCESS:
1393 case CM_ERROR_QUOTA:
1394 case CM_ERROR_SPACE:
1395 case CM_ERROR_TOOBIG:
1396 case CM_ERROR_READONLY:
1397 case CM_ERROR_NOSUCHPATH:
1399 * Apply the fatal error to this buffer.
1401 bufp->flags &= ~CM_BUF_DIRTY;
1402 bufp->flags |= CM_BUF_ERROR;
1403 bufp->dirty_offset = 0;
1404 bufp->dirty_length = 0;
1406 bufp->dataVersion = CM_BUF_VERSION_BAD;
1407 bufp->dirtyCounter++;
1409 case CM_ERROR_TIMEDOUT:
1410 case CM_ERROR_ALLDOWN:
1411 case CM_ERROR_ALLBUSY:
1412 case CM_ERROR_ALLOFFLINE:
1413 case CM_ERROR_CLOCKSKEW:
1415 /* do not mark the buffer in error state but do
1416 * not attempt to complete the rest either.
1421 bufp->flags &= ~(CM_BUF_WRITING | CM_BUF_DIRTY);
1422 bufp->dirty_offset = bufp->dirty_length = 0;
1427 lock_ReleaseWrite(&scp->rw);
1428 lock_ReleaseMutex(&bufp->mx);
1434 lock_ObtainWrite(&scp->rw);
1435 cm_SyncOpDone(scp, NULL, flags);
1437 lock_ReleaseWrite(&scp->rw);
1440 /* clean things out */
1441 biop->bufListp = NULL;
1442 biop->bufListEndp = NULL;
1446 cm_CloneStatus(cm_scache_t *scp, cm_user_t *userp, int scp_locked,
1447 AFSFetchStatus *afsStatusp, AFSVolSync *volSyncp)
1449 // setup the status based upon the scp data
1450 afsStatusp->InterfaceVersion = 0x1;
1451 switch (scp->fileType) {
1452 case CM_SCACHETYPE_FILE:
1453 afsStatusp->FileType = File;
1455 case CM_SCACHETYPE_DIRECTORY:
1456 afsStatusp->FileType = Directory;
1458 case CM_SCACHETYPE_MOUNTPOINT:
1459 afsStatusp->FileType = SymbolicLink;
1461 case CM_SCACHETYPE_SYMLINK:
1462 case CM_SCACHETYPE_DFSLINK:
1463 afsStatusp->FileType = SymbolicLink;
1466 afsStatusp->FileType = -1; /* an invalid value */
1468 afsStatusp->LinkCount = scp->linkCount;
1469 afsStatusp->Length = scp->length.LowPart;
1470 afsStatusp->DataVersion = (afs_uint32)(scp->dataVersion & MAX_AFS_UINT32);
1471 afsStatusp->Author = 0x1;
1472 afsStatusp->Owner = scp->owner;
1474 lock_ObtainWrite(&scp->rw);
1477 if (cm_FindACLCache(scp, userp, &afsStatusp->CallerAccess))
1478 afsStatusp->CallerAccess = scp->anyAccess;
1479 afsStatusp->AnonymousAccess = scp->anyAccess;
1480 afsStatusp->UnixModeBits = scp->unixModeBits;
1481 afsStatusp->ParentVnode = scp->parentVnode;
1482 afsStatusp->ParentUnique = scp->parentUnique;
1483 afsStatusp->ResidencyMask = 0;
1484 afsStatusp->ClientModTime = scp->clientModTime;
1485 afsStatusp->ServerModTime = scp->serverModTime;
1486 afsStatusp->Group = scp->group;
1487 afsStatusp->SyncCounter = 0;
1488 afsStatusp->dataVersionHigh = (afs_uint32)(scp->dataVersion >> 32);
1489 afsStatusp->lockCount = 0;
1490 afsStatusp->Length_hi = scp->length.HighPart;
1491 afsStatusp->errorCode = 0;
1493 volSyncp->spare1 = scp->volumeCreationDate;
1498 /* Fetch a buffer. Called with scp locked.
1499 * The scp is locked on return.
1501 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1504 long code=0, code1=0;
1505 afs_uint32 nbytes; /* bytes in transfer */
1506 afs_uint32 nbytes_hi = 0; /* high-order 32 bits of bytes in transfer */
1507 afs_uint64 length_found = 0;
1508 long rbytes; /* bytes in rx_Read call */
1510 AFSFetchStatus afsStatus;
1511 AFSCallBack callback;
1514 cm_buf_t *tbufp; /* buf we're filling */
1515 osi_queueData_t *qdp; /* q element we're scanning */
1517 struct rx_call *rxcallp;
1518 struct rx_connection *rxconnp;
1519 cm_bulkIO_t biod; /* bulk IO descriptor */
1523 int require_64bit_ops = 0;
1524 int call_was_64bit = 0;
1525 int fs_fetchdata_offset_bug = 0;
1529 memset(&volSync, 0, sizeof(volSync));
1531 /* now, the buffer may or may not be filled with good data (buf_GetNew
1532 * drops lots of locks, and may indeed return a properly initialized
1533 * buffer, although more likely it will just return a new, empty, buffer.
1536 #ifdef AFS_FREELANCE_CLIENT
1538 // yj: if they're trying to get the /afs directory, we need to
1539 // handle it differently, since it's local rather than on any
1542 getroot = (scp==cm_data.rootSCachep);
1544 osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1547 if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1548 /* We already have this buffer don't do extra work */
1552 cm_AFSFidFromFid(&tfid, &scp->fid);
1554 code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1556 /* couldn't even get the first page setup properly */
1557 osi_Log1(afsd_logp, "GetBuffer: SetupFetchBIOD failure code %d", code);
1561 /* once we get here, we have the callback in place, we know that no one
1562 * is fetching the data now. Check one last time that we still have
1563 * the wrong data, and then fetch it if we're still wrong.
1565 * We can lose a race condition and end up with biod.length zero, in
1566 * which case we just retry.
1568 if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
1569 if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow) &&
1570 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength))
1572 osi_Log4(afsd_logp, "Bad DVs 0x%x != (0x%x -> 0x%x) or length 0x%x",
1573 bufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion, biod.length);
1575 if (bufp->dataVersion == CM_BUF_VERSION_BAD)
1576 memset(bufp->datap, 0, cm_data.buf_blockSize);
1577 bufp->dataVersion = scp->dataVersion;
1579 cm_ReleaseBIOD(&biod, 0, 0, 1);
1581 } else if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow)
1582 && (scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1583 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->truncPos)) {
1584 memset(bufp->datap, 0, cm_data.buf_blockSize);
1585 bufp->dataVersion = scp->dataVersion;
1586 cm_ReleaseBIOD(&biod, 0, 0, 1);
1590 lock_ReleaseWrite(&scp->rw);
1593 if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1594 ConvertLongToLargeInteger(biod.length)),
1595 ConvertLongToLargeInteger(LONG_MAX))) {
1596 require_64bit_ops = 1;
1599 osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1600 osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1601 scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1603 #ifdef AFS_FREELANCE_CLIENT
1606 // if getroot then we don't need to make any calls
1607 // just return fake data
1609 if (cm_freelanceEnabled && getroot) {
1610 // setup the fake status
1611 afsStatus.InterfaceVersion = 0x1;
1612 afsStatus.FileType = 0x2;
1613 afsStatus.LinkCount = scp->linkCount;
1614 afsStatus.Length = cm_fakeDirSize;
1615 afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1616 afsStatus.Author = 0x1;
1617 afsStatus.Owner = 0x0;
1618 afsStatus.CallerAccess = 0x9;
1619 afsStatus.AnonymousAccess = 0x9;
1620 afsStatus.UnixModeBits = 0x1ff;
1621 afsStatus.ParentVnode = 0x1;
1622 afsStatus.ParentUnique = 0x1;
1623 afsStatus.ResidencyMask = 0;
1624 afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1625 afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1626 afsStatus.Group = 0;
1627 afsStatus.SyncCounter = 0;
1628 afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1629 afsStatus.lockCount = 0;
1630 afsStatus.Length_hi = 0;
1631 afsStatus.errorCode = 0;
1632 memset(&volSync, 0, sizeof(volSync));
1634 // once we're done setting up the status info,
1635 // we just fill the buffer pages with fakedata
1636 // from cm_FakeRootDir. Extra pages are set to
1639 lock_ObtainMutex(&cm_Freelance_Lock);
1640 t1 = bufp->offset.LowPart;
1641 qdp = biod.bufListEndp;
1643 tbufp = osi_GetQData(qdp);
1644 bufferp=tbufp->datap;
1645 memset(bufferp, 0, cm_data.buf_blockSize);
1646 t2 = cm_fakeDirSize - t1;
1647 if (t2> (afs_int32)cm_data.buf_blockSize)
1648 t2=cm_data.buf_blockSize;
1650 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1655 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1658 lock_ReleaseMutex(&cm_Freelance_Lock);
1660 // once we're done, we skip over the part of the
1661 // code that does the ACTUAL fetching of data for
1664 goto fetchingcompleted;
1667 #endif /* AFS_FREELANCE_CLIENT */
1670 * if the requested offset is greater than the file length,
1671 * the file server will return zero bytes of data and the
1672 * current status for the file which we already have since
1673 * we have just obtained a callback. Instead, we can avoid
1674 * the network round trip by allocating zeroed buffers and
1675 * faking the status info.
1677 if (biod.offset.QuadPart >= scp->length.QuadPart) {
1678 osi_Log5(afsd_logp, "SKIP FetchData64 scp 0x%p, off 0x%x:%08x > length 0x%x:%08x",
1679 scp, biod.offset.HighPart, biod.offset.LowPart,
1680 scp->length.HighPart, scp->length.LowPart);
1682 /* Clone the current status info */
1683 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
1685 /* status info complete, fill pages with zeros */
1686 for (qdp = biod.bufListEndp;
1688 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
1689 tbufp = osi_GetQData(qdp);
1690 bufferp=tbufp->datap;
1691 memset(bufferp, 0, cm_data.buf_blockSize);
1694 /* no need to contact the file server */
1695 goto fetchingcompleted;
1699 lock_ReleaseWrite(&scp->rw);
1703 /* now make the call */
1705 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1709 rxconnp = cm_GetRxConn(connp);
1710 rxcallp = rx_NewCall(rxconnp);
1711 rx_PutConnection(rxconnp);
1713 nbytes = nbytes_hi = 0;
1715 if (SERVERHAS64BIT(connp)) {
1718 osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1719 scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1721 code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1724 temp = rx_Read32(rxcallp, &nbytes_hi);
1725 if (temp == sizeof(afs_int32)) {
1726 nbytes_hi = ntohl(nbytes_hi);
1729 code = rxcallp->error;
1730 code1 = rx_EndCall(rxcallp, code);
1738 if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1739 if (require_64bit_ops) {
1740 osi_Log0(afsd_logp, "Skipping FetchData. Operation requires FetchData64");
1741 code = CM_ERROR_TOOBIG;
1744 rxconnp = cm_GetRxConn(connp);
1745 rxcallp = rx_NewCall(rxconnp);
1746 rx_PutConnection(rxconnp);
1749 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1750 scp, biod.offset.LowPart, biod.length);
1752 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1755 SET_SERVERHASNO64BIT(connp);
1760 temp = rx_Read32(rxcallp, &nbytes);
1761 if (temp == sizeof(afs_int32)) {
1762 nbytes = ntohl(nbytes);
1763 FillInt64(length_found, nbytes_hi, nbytes);
1764 if (length_found > biod.length) {
1766 * prior to 1.4.12 and 1.5.65 the file server would return
1767 * (filesize - offset) if the requested offset was greater than
1768 * the filesize. The correct return value would have been zero.
1769 * Force a retry by returning an RX_PROTOCOL_ERROR. If the cause
1770 * is a race between two RPCs issues by this cache manager, the
1771 * correct thing will happen the second time.
1773 osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1774 fs_fetchdata_offset_bug = 1;
1777 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1778 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1781 /* for the moment, nbytes_hi will always be 0 if code == 0
1782 because biod.length is a 32-bit quantity. */
1785 qdp = biod.bufListEndp;
1787 tbufp = osi_GetQData(qdp);
1788 bufferp = tbufp->datap;
1792 /* fill length_found of data from the pipe into the pages.
1793 * When we stop, qdp will point at the last page we're
1794 * dealing with, and bufferp will tell us where we
1795 * stopped. We'll need this info below when we clear
1796 * the remainder of the last page out (and potentially
1797 * clear later pages out, if we fetch past EOF).
1799 while (length_found > 0) {
1800 /* assert that there are still more buffers;
1801 * our check above for length_found being less than
1802 * biod.length should ensure this.
1804 osi_assertx(bufferp != NULL, "null cm_buf_t");
1806 /* read rbytes of data */
1807 rbytes = (afs_uint32)(length_found > cm_data.buf_blockSize ? cm_data.buf_blockSize : length_found);
1808 temp = rx_Read(rxcallp, bufferp, rbytes);
1809 if (temp < rbytes) {
1811 * If the file server returned (filesize - offset),
1812 * then the first rx_Read will return zero octets of data.
1813 * If it does, do not treat it as an error. Correct the
1814 * length_found and continue as if the file server said
1815 * it was sending us zero octets of data.
1817 if (fs_fetchdata_offset_bug && first_read)
1820 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1825 /* allow read-while-fetching.
1826 * if this is the last buffer, clear the
1827 * PREFETCHING flag, so the reader waiting for
1828 * this buffer will start a prefetch.
1830 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1831 lock_ObtainWrite(&scp->rw);
1832 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1833 osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
1834 osi_Wakeup((LONG_PTR) &scp->flags);
1836 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1837 osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
1839 cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
1841 lock_ReleaseWrite(&scp->rw);
1843 /* and adjust counters */
1844 length_found -= temp;
1846 /* and move to the next buffer */
1847 if (length_found != 0) {
1848 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1850 tbufp = osi_GetQData(qdp);
1851 bufferp = tbufp->datap;
1859 /* zero out remainder of last pages, in case we are
1860 * fetching past EOF. We were fetching an integral #
1861 * of pages, but stopped, potentially in the middle of
1862 * a page. Zero the remainder of that page, and then
1863 * all of the rest of the pages.
1866 osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
1867 rbytes = (long) (bufferp - tbufp->datap);
1869 /* bytes left to zero */
1870 rbytes = cm_data.buf_blockSize - rbytes;
1873 memset(bufferp, 0, rbytes);
1874 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1877 tbufp = osi_GetQData(qdp);
1878 bufferp = tbufp->datap;
1879 /* bytes to clear in this page */
1880 rbytes = cm_data.buf_blockSize;
1886 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
1888 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
1891 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
1893 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
1897 code1 = rx_EndCall(rxcallp, code);
1899 if (code1 == RXKADUNKNOWNKEY)
1900 osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
1902 /* If we are avoiding a file server bug, ignore the error state */
1903 if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
1904 /* Clone the current status info and clear the error state */
1905 scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
1907 lock_ReleaseWrite(&scp->rw);
1911 /* Prefer the error value from FetchData over rx_EndCall */
1912 } else if (code == 0 && code1 != 0)
1914 osi_Log0(afsd_logp, "CALL FetchData DONE");
1916 } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
1919 code = cm_MapRPCError(code, reqp);
1922 lock_ObtainWrite(&scp->rw);
1924 /* we know that no one else has changed the buffer, since we still have
1925 * the fetching flag on the buffers, and we have the scp locked again.
1926 * Copy in the version # into the buffer if we got code 0 back from the
1930 for(qdp = biod.bufListp;
1932 qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1933 tbufp = osi_GetQData(qdp);
1934 tbufp->dataVersion = afsStatus.dataVersionHigh;
1935 tbufp->dataVersion <<= 32;
1936 tbufp->dataVersion |= afsStatus.DataVersion;
1939 /* write buffer out to disk cache */
1940 diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
1941 tbufp->dataVersion);
1942 #endif /* DISKCACHE95 */
1946 /* release scatter/gather I/O structure (buffers, locks) */
1947 cm_ReleaseBIOD(&biod, 0, code, 1);
1950 cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, 0);