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 <afsconfig.h>
11 #include "afs/param.h"
13 #include "afs/sysincludes.h" /* Standard vendor system headers */
19 #endif /* AFS_ALPHA_ENV */
20 #include "afsincludes.h" /* Afs-based standard headers */
21 #include "afs/afs_stats.h" /* statistics */
22 #include "afs_prototypes.h"
24 extern int cacheDiskType;
28 FillStoreStats(int code, int idx, osi_timeval32_t xferStartTime,
29 afs_size_t bytesToXfer, afs_size_t bytesXferred)
31 struct afs_stats_xferData *xferP;
32 osi_timeval32_t xferStopTime;
33 osi_timeval32_t elapsedTime;
35 xferP = &(afs_stats_cmfullperf.rpc.fsXferTimes[idx]);
36 osi_GetTime(&xferStopTime);
39 (xferP->numSuccesses)++;
40 afs_stats_XferSumBytes[idx] += bytesXferred;
41 (xferP->sumBytes) += (afs_stats_XferSumBytes[idx] >> 10);
42 afs_stats_XferSumBytes[idx] &= 0x3FF;
43 if (bytesXferred < xferP->minBytes)
44 xferP->minBytes = bytesXferred;
45 if (bytesXferred > xferP->maxBytes)
46 xferP->maxBytes = bytesXferred;
49 * Tally the size of the object. Note: we tally the actual size,
50 * NOT the number of bytes that made it out over the wire.
52 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0) (xferP->count[0])++;
53 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1) (xferP->count[1])++;
54 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2) (xferP->count[2])++;
55 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3) (xferP->count[3])++;
56 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4) (xferP->count[4])++;
57 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5) (xferP->count[5])++;
58 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6) (xferP->count[6])++;
59 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7) (xferP->count[7])++;
63 afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
64 afs_stats_AddTo((xferP->sumTime), elapsedTime);
65 afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
66 if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
67 afs_stats_TimeAssign((xferP->minTime), elapsedTime);
69 if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
70 afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
74 #endif /* AFS_NOSTATS */
76 /* rock and operations for RX_FILESERVER */
81 rxfs_storeUfsPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
83 *tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
88 rxfs_storeMemPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
91 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *) r;
94 code = rx_WritevAlloc(v->call, v->tiov, &v->tnio, RX_MAXIOVECS, size);
97 code = rx_Error(v->call);
109 rxfs_storeUfsRead(void *r, struct osi_file *tfile, afs_uint32 offset,
110 afs_uint32 tlen, afs_uint32 *bytesread)
113 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
116 code = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
122 #if defined(KERNEL_HAVE_UERROR)
130 rxfs_storeMemRead(void *r, struct osi_file *tfile, afs_uint32 offset,
131 afs_uint32 tlen, afs_uint32 *bytesread)
134 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
135 struct memCacheEntry *mceP = (struct memCacheEntry *)tfile;
138 code = afs_MemReadvBlk(mceP, offset, v->tiov, v->tnio, tlen);
146 rxfs_storeMemWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
149 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
152 code = rx_Writev(v->call, v->tiov, v->tnio, l);
155 code = rx_Error(v->call);
156 return (code ? code : -33);
158 *byteswritten = code;
163 rxfs_storeUfsWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
166 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
169 code = rx_Write(v->call, v->tbuffer, l);
170 /* writing 0 bytes will
171 * push a short packet. Is that really what we want, just because the
172 * data didn't come back from the disk yet? Let's try it and see. */
175 code = rx_Error(v->call);
176 return (code ? code : -33);
178 *byteswritten = code;
183 rxfs_storePadd(void *rock, afs_uint32 size)
187 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
190 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
191 memset(v->tbuffer, 0, AFS_LRALLOCSIZ);
194 tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
196 code = rx_Write(v->call, v->tbuffer, tlen);
200 return -33; /* XXX */
207 rxfs_storeStatus(void *rock)
209 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
211 if (rx_GetRemoteStatus(v->call) & 1)
217 rxfs_storeClose(void *r, struct AFSFetchStatus *OutStatus, int *doProcessFS)
220 struct AFSVolSync tsync;
221 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
226 #ifdef AFS_64BIT_CLIENT
228 code = EndRXAFS_StoreData64(v->call, OutStatus, &tsync);
231 code = EndRXAFS_StoreData(v->call, OutStatus, &tsync);
234 *doProcessFS = 1; /* Flag to run afs_ProcessFS() later on */
240 rxfs_storeDestroy(void **r, afs_int32 code)
242 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
247 code = rx_EndCall(v->call, code);
251 osi_FreeLargeSpace(v->tbuffer);
253 osi_FreeSmallSpace(v->tiov);
254 osi_FreeSmallSpace(v);
259 afs_GenericStoreProc(struct storeOps *ops, void *rock,
260 struct dcache *tdc, int *shouldwake,
261 afs_size_t *bytesXferred)
263 struct rxfs_storeVariables *svar = rock;
264 afs_uint32 tlen, bytesread, byteswritten;
268 struct osi_file *tfile;
270 size = tdc->f.chunkBytes;
272 tfile = afs_CFileOpen(&tdc->f.inode);
278 code = (*ops->prepare)(rock, size, &tlen);
282 code = (*ops->read)(rock, tfile, offset, tlen, &bytesread);
287 code = (*ops->write)(rock, tlen, &byteswritten);
291 *bytesXferred += byteswritten;
292 #endif /* AFS_NOSTATS */
297 * if file has been locked on server, can allow
300 if (shouldwake && *shouldwake && ((*ops->status)(rock) == 0)) {
301 *shouldwake = 0; /* only do this once */
302 afs_wakeup(svar->vcache);
305 afs_CFileClose(tfile);
311 struct storeOps rxfs_storeUfsOps = {
312 #ifndef HAVE_STRUCT_LABEL_SUPPORT
313 rxfs_storeUfsPrepare,
322 .prepare = rxfs_storeUfsPrepare,
323 .read = rxfs_storeUfsRead,
324 .write = rxfs_storeUfsWrite,
325 .status = rxfs_storeStatus,
326 .padd = rxfs_storePadd,
327 .close = rxfs_storeClose,
328 .destroy = rxfs_storeDestroy,
329 .storeproc = afs_GenericStoreProc
334 struct storeOps rxfs_storeMemOps = {
335 #ifndef HAVE_STRUCT_LABEL_SUPPORT
336 rxfs_storeMemPrepare,
345 .prepare = rxfs_storeMemPrepare,
346 .read = rxfs_storeMemRead,
347 .write = rxfs_storeMemWrite,
348 .status = rxfs_storeStatus,
349 .padd = rxfs_storePadd,
350 .close = rxfs_storeClose,
351 .destroy = rxfs_storeDestroy,
352 .storeproc = afs_GenericStoreProc
357 rxfs_storeInit(struct vcache *avc, struct afs_conn *tc,
358 struct rx_connection *rxconn, afs_size_t base,
359 afs_size_t bytes, afs_size_t length,
360 int sync, struct storeOps **ops, void **rock)
363 struct rxfs_storeVariables *v;
368 v = osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
370 osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
371 memset(v, 0, sizeof(struct rxfs_storeVariables));
373 v->InStatus.ClientModTime = avc->f.m.Date;
374 v->InStatus.Mask = AFS_SETMODTIME;
377 v->InStatus.Mask |= AFS_FSYNC;
379 v->call = rx_NewCall(rxconn);
381 #ifdef AFS_64BIT_CLIENT
382 if (!afs_serverHasNo64Bit(tc))
383 code = StartRXAFS_StoreData64(
384 v->call, (struct AFSFid*)&avc->f.fid.Fid,
385 &v->InStatus, base, bytes, length);
387 if (length > 0xFFFFFFFF)
390 afs_int32 t1 = base, t2 = bytes, t3 = length;
391 code = StartRXAFS_StoreData(v->call,
392 (struct AFSFid *) &avc->f.fid.Fid,
393 &v->InStatus, t1, t2, t3);
397 #else /* AFS_64BIT_CLIENT */
398 code = StartRXAFS_StoreData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
399 &v->InStatus, base, bytes, length);
400 #endif /* AFS_64BIT_CLIENT */
406 return rxfs_storeDestroy(rock, code);
408 if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
409 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
412 ("rxfs_storeInit: osi_AllocLargeSpace for iovecs returned NULL\n");
413 *ops = (struct storeOps *) &rxfs_storeUfsOps;
415 v->tiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
418 ("rxfs_storeInit: osi_AllocSmallSpace for iovecs returned NULL\n");
419 *ops = (struct storeOps *) &rxfs_storeMemOps;
425 unsigned int storeallmissing = 0;
427 * Called for each chunk upon store.
429 * \param avc Ptr to the vcache entry of the file being stored.
430 * \param dclist pointer to the list of dcaches
431 * \param bytes total number of bytes for the current operation
432 * \param anewDV Ptr to the dataversion after store
433 * \param doProcessFS pointer to the "do process FetchStatus" flag
434 * \param OutStatus pointer to the FetchStatus as returned by the fileserver
435 * \param nchunks number of dcaches to consider
436 * \param nomore copy of the "no more data" flag
437 * \param ops pointer to the block of storeOps to be used for this operation
438 * \param rock pointer to the opaque protocol-specific data of this operation
441 afs_CacheStoreDCaches(struct vcache *avc, struct dcache **dclist,
442 afs_size_t bytes, afs_hyper_t *anewDV, int *doProcessFS,
443 struct AFSFetchStatus *OutStatus, afs_uint32 nchunks,
444 int nomore, struct storeOps *ops, void *rock)
446 int *shouldwake = NULL;
450 afs_size_t bytesXferred;
453 osi_timeval32_t xferStartTime; /*FS xfer start time */
454 afs_size_t bytesToXfer = 10000; /* # bytes to xfer */
455 #endif /* AFS_NOSTATS */
457 osi_Assert(nchunks != 0);
459 for (i = 0; i < nchunks && !code; i++) {
460 struct dcache *tdc = dclist[i];
464 afs_warn("afs: missing dcache!\n");
466 continue; /* panic? */
468 size = tdc->f.chunkBytes;
469 afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2, ICL_TYPE_POINTER, avc,
470 ICL_TYPE_INT32, tdc->f.chunk, ICL_TYPE_INT32, tdc->index,
471 ICL_TYPE_INT32, afs_inode2trace(&tdc->f.inode));
474 if (avc->asynchrony == -1) {
475 if (afs_defaultAsynchrony > (bytes - stored))
476 shouldwake = &nomore;
478 else if ((afs_uint32) avc->asynchrony >= (bytes - stored))
479 shouldwake = &nomore;
482 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
483 ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
484 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, size);
486 AFS_STATCNT(CacheStoreProc);
488 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
489 avc->f.truncPos = AFS_NOTRUNC;
492 * In this case, size is *always* the amount of data we'll be trying
497 osi_GetTime(&xferStartTime);
498 #endif /* AFS_NOSTATS */
501 code = (*ops->storeproc)(ops, rock, tdc, shouldwake,
504 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
505 ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
506 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, size);
509 FillStoreStats(code, AFS_STATS_FS_XFERIDX_STOREDATA,
510 xferStartTime, bytesToXfer, bytesXferred);
511 #endif /* AFS_NOSTATS */
513 if ((tdc->f.chunkBytes < afs_OtherCSize)
514 && (i < (nchunks - 1)) && code == 0) {
515 code = (*ops->padd)(rock, afs_OtherCSize - tdc->f.chunkBytes);
517 stored += tdc->f.chunkBytes;
518 /* ideally, I'd like to unlock the dcache and turn
519 * off the writing bit here, but that would
520 * require being able to retry StoreAllSegments in
521 * the event of a failure. It only really matters
522 * if user can't read from a 'locked' dcache or
523 * one which has the writing bit turned on. */
527 code = (*ops->close)(rock, OutStatus, doProcessFS);
528 /* if this succeeds, dv has been bumped. */
535 code = (*ops->destroy)(&rock, code);
537 /* if we errored, can't trust this. */
544 #define lmin(a,b) (((a) < (b)) ? (a) : (b))
548 * \param dclist pointer to the list of dcaches
549 * \param avc Ptr to the vcache entry.
550 * \param areq Ptr to the request structure
551 * \param sync sync flag
552 * \param minj the chunk offset for this call
553 * \param high index of last dcache to store
554 * \param moredata the moredata flag
555 * \param anewDV Ptr to the dataversion after store
556 * \param amaxStoredLength Ptr to the amount of that is actually stored
558 * \note Environment: Nothing interesting.
561 afs_CacheStoreVCache(struct dcache **dcList, struct vcache *avc,
562 struct vrequest *areq, int sync, unsigned int minj,
563 unsigned int high, unsigned int moredata,
564 afs_hyper_t *anewDV, afs_size_t *amaxStoredLength)
567 struct storeOps *ops;
571 struct AFSFetchStatus OutStatus;
573 afs_size_t base, bytes, length;
575 unsigned int first = 0;
577 struct rx_connection *rxconn;
579 for (bytes = 0, j = 0; !code && j <= high; j++) {
581 ObtainSharedLock(&(dcList[j]->lock), 629);
584 bytes += dcList[j]->f.chunkBytes;
585 if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
586 && (dcList[j]->f.chunk - minj < high)
588 int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
592 if (bytes && (j == high || !dcList[j + 1])) {
594 struct dcache **dclist = &dcList[first];
595 /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
596 base = AFS_CHUNKTOBASE(first + minj);
599 * take a list of dcache structs and send them all off to the server
600 * the list must be in order, and the chunks contiguous.
601 * Note - there is no locking done by this code currently. For
602 * safety's sake, xdcache could be locked over the entire call.
603 * However, that pretty well ties up all the threads. Meantime, all
604 * the chunks _MUST_ have their refcounts bumped.
605 * The writes done before a store back will clear setuid-ness
607 * We can permit CacheStoreProc to wake up the user process IFF we
608 * are doing the last RPC for this close, ie, storing back the last
609 * set of contiguous chunks of a file.
612 nchunks = 1 + j - first;
613 nomore = !(moredata || (j != high));
614 length = lmin(avc->f.m.Length, avc->f.truncPos);
615 afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64,
616 ICL_TYPE_FID, &avc->f.fid.Fid, ICL_TYPE_OFFSET,
617 ICL_HANDLE_OFFSET(base), ICL_TYPE_OFFSET,
618 ICL_HANDLE_OFFSET(bytes), ICL_TYPE_OFFSET,
619 ICL_HANDLE_OFFSET(length));
622 tc = afs_Conn(&avc->f.fid, areq, 0, &rxconn);
624 #ifdef AFS_64BIT_CLIENT
627 code = rxfs_storeInit(avc, tc, rxconn, base, bytes, length,
630 code = afs_CacheStoreDCaches(avc, dclist, bytes, anewDV,
631 &doProcessFS, &OutStatus,
632 nchunks, nomore, ops, rock);
635 #ifdef AFS_64BIT_CLIENT
636 if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
637 afs_serverSetNo64Bit(tc);
640 #endif /* AFS_64BIT_CLIENT */
642 (tc, rxconn, code, &avc->f.fid, areq,
643 AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
646 /* put back all remaining locked dcache entries */
647 for (i = 0; i < nchunks; i++) {
648 struct dcache *tdc = dclist[i];
650 if (afs_indexFlags[tdc->index] & IFDataMod) {
652 * LOCKXXX -- should hold afs_xdcache(W) when
653 * modifying afs_indexFlags.
655 afs_indexFlags[tdc->index] &= ~IFDataMod;
656 afs_stats_cmperf.cacheCurrDirtyChunks--;
657 afs_indexFlags[tdc->index] &= ~IFDirtyPages;
658 if (sync & AFS_VMSYNC_INVAL) {
659 /* since we have invalidated all the pages of this
660 ** vnode by calling osi_VM_TryToSmush, we can
661 ** safely mark this dcache entry as not having
662 ** any pages. This vnode now becomes eligible for
663 ** reclamation by getDownD.
665 afs_indexFlags[tdc->index] &= ~IFAnyPages;
669 UpgradeSToWLock(&tdc->lock, 628);
670 tdc->f.states &= ~DWriting; /* correct? */
671 tdc->dflags |= DFEntryMod;
672 ReleaseWriteLock(&tdc->lock);
674 /* Mark the entry as released */
679 /* Now copy out return params */
680 UpgradeSToWLock(&avc->lock, 28); /* keep out others for a while */
681 afs_ProcessFS(avc, &OutStatus, areq);
682 /* Keep last (max) size of file on server to see if
683 * we need to call afs_StoreMini to extend the file.
686 *amaxStoredLength = OutStatus.Length;
687 ConvertWToSLock(&avc->lock);
692 for (j++; j <= high; j++) {
694 ReleaseSharedLock(&(dcList[j]->lock));
695 afs_PutDCache(dcList[j]);
696 /* Releasing entry */
702 afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
703 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
711 /* rock and operations for RX_FILESERVER */
713 struct rxfs_fetchVariables {
714 struct rx_call *call;
718 afs_int32 hasNo64bit;
724 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
728 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
731 tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
733 code = rx_Read(v->call, v->tbuffer, tlen);
742 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
745 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
749 code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
759 rxfs_fetchMemWrite(void *r, struct osi_file *fP, afs_uint32 offset,
760 afs_uint32 tlen, afs_uint32 *byteswritten)
763 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
764 struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
766 code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
770 *byteswritten = code;
775 rxfs_fetchUfsWrite(void *r, struct osi_file *fP, afs_uint32 offset,
776 afs_uint32 tlen, afs_uint32 *byteswritten)
779 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
781 code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
785 *byteswritten = code;
791 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
792 struct afs_FetchOutput *o)
795 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
801 #ifdef AFS_64BIT_CLIENT
803 code = EndRXAFS_FetchData64(v->call, &o->OutStatus, &o->CallBack,
807 code = EndRXAFS_FetchData(v->call, &o->OutStatus, &o->CallBack,
809 code = rx_EndCall(v->call, code);
818 rxfs_fetchDestroy(void **r, afs_int32 code)
820 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
825 code = rx_EndCall(v->call, code);
829 osi_FreeLargeSpace(v->tbuffer);
831 osi_FreeSmallSpace(v->iov);
832 osi_FreeSmallSpace(v);
837 rxfs_fetchMore(void *r, afs_int32 *length, afs_uint32 *moredata)
840 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
843 * The fetch protocol is extended for the AFS/DFS translator
844 * to allow multiple blocks of data, each with its own length,
845 * to be returned. As long as the top bit is set, there are more
848 * We do not do this for AFS file servers because they sometimes
849 * return large negative numbers as the transfer size.
853 code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
855 *length = ntohl(*length);
856 if (code != sizeof(afs_int32)) {
857 code = rx_Error(v->call);
859 return (code ? code : -1); /* try to return code, not -1 */
862 *moredata = *length & 0x80000000;
863 *length &= ~0x80000000;
868 struct fetchOps rxfs_fetchUfsOps = {
877 struct fetchOps rxfs_fetchMemOps = {
886 rxfs_fetchInit(struct afs_conn *tc, struct rx_connection *rxconn,
887 struct vcache *avc, afs_offs_t base,
888 afs_uint32 size, afs_int32 *alength, struct dcache *adc,
889 struct osi_file *fP, struct fetchOps **ops, void **rock)
891 struct rxfs_fetchVariables *v;
893 #ifdef AFS_64BIT_CLIENT
894 afs_uint32 length_hi = 0;
896 afs_uint32 length = 0, bytes;
898 v = (struct rxfs_fetchVariables *)
899 osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
901 osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
902 memset(v, 0, sizeof(struct rxfs_fetchVariables));
905 v->call = rx_NewCall(rxconn);
908 #ifdef AFS_64BIT_CLIENT
909 afs_size_t length64; /* as returned from server */
910 if (!afs_serverHasNo64Bit(tc)) {
911 afs_uint64 llbytes = size;
913 code = StartRXAFS_FetchData64(v->call,
914 (struct AFSFid *) &avc->f.fid.Fid,
918 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
919 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
921 bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
923 if (bytes == sizeof(afs_int32)) {
924 length_hi = ntohl(length_hi);
927 code = rx_EndCall(v->call, RX_PROTOCOL_ERROR);
933 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
934 if (base > 0x7FFFFFFF) {
941 v->call = rx_NewCall(rxconn);
943 StartRXAFS_FetchData(
944 v->call, (struct AFSFid*)&avc->f.fid.Fid,
948 afs_serverSetNo64Bit(tc);
953 bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
955 if (bytes == sizeof(afs_int32))
956 length = ntohl(length);
959 code = rx_EndCall(v->call, RX_PROTOCOL_ERROR);
965 FillInt64(length64, length_hi, length);
968 /* Check if the fileserver said our length is bigger than can fit
969 * in a signed 32-bit integer. If it is, we can't handle that, so
971 if (length64 > MAX_AFS_INT32) {
975 afs_warn("afs: Warning: FetchData64 returned too much data "
976 "(length64 %u.%u); this should not happen! "
977 "Aborting fetch request.\n",
981 code = rx_EndCall(v->call, RX_PROTOCOL_ERROR);
985 code = code != 0 ? code : EIO;
990 /* Check if the fileserver said our length was negative. If it
991 * is, just treat it as a 0 length, since some older fileservers
992 * returned negative numbers when they meant to return 0. Note
993 * that we must do this in this 64-bit-specific block, since
994 * length64 being negative will screw up our conversion to the
995 * 32-bit 'alength' below. */
997 length_hi = length = 0;
998 FillInt64(length64, 0, 0);
1002 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
1003 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1005 ICL_HANDLE_OFFSET(length64));
1008 #else /* AFS_64BIT_CLIENT */
1010 code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
1016 rx_Read(v->call, (char *)&length, sizeof(afs_int32));
1018 if (bytes == sizeof(afs_int32)) {
1019 *alength = ntohl(length);
1021 /* Older fileservers can return a negative length when they
1022 * meant to return 0; just assume negative lengths were
1023 * meant to be 0 lengths. */
1027 code = rx_EndCall(v->call, RX_PROTOCOL_ERROR);
1031 #endif /* AFS_64BIT_CLIENT */
1035 /* We need to cast here, in order to avoid issues if *alength is
1036 * negative. Some, older, fileservers can return a negative length,
1037 * which the rest of the code deals correctly with. */
1038 if (code == 0 && *alength > (afs_int32) size) {
1039 /* The fileserver told us it is going to send more data than we
1040 * requested. It shouldn't do that, and accepting that much data
1041 * can make us take up more cache space than we're supposed to,
1046 afs_warn("afs: Warning: FetchData64 returned more data than "
1047 "requested (requested %ld, got %ld); this should not "
1048 "happen! Aborting fetch request.\n",
1049 (long)size, (long)*alength);
1052 code = rx_EndCall(v->call, RX_PROTOCOL_ERROR);
1060 return rxfs_fetchDestroy(rock, code);
1062 if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
1063 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1065 osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
1066 osi_Assert(WriteLocked(&adc->lock));
1068 *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
1071 afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
1072 ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
1073 ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
1075 * We need to alloc the iovecs on the heap so that they are "pinned"
1076 * rather than declare them on the stack - defect 11272
1078 v->iov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
1080 osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace for iovecs returned NULL\n");
1081 *ops = (struct fetchOps *) &rxfs_fetchMemOps;
1089 * Routine called on fetch; also tells people waiting for data
1090 * that more has arrived.
1092 * \param tc Ptr to the AFS connection structure.
1093 * \param rxconn Ptr to the Rx connection structure.
1094 * \param fP File descriptor for the cache file.
1095 * \param base Base offset to fetch.
1096 * \param adc Ptr to the dcache entry for the file, write-locked.
1097 * \param avc Ptr to the vcache entry for the file.
1098 * \param size Amount of data that should be fetched.
1099 * \param tsmall Ptr to the afs_FetchOutput structure.
1101 * \note Environment: Nothing interesting.
1104 afs_CacheFetchProc(struct afs_conn *tc, struct rx_connection *rxconn,
1105 struct osi_file *fP, afs_size_t base,
1106 struct dcache *adc, struct vcache *avc, afs_int32 size,
1107 struct afs_FetchOutput *tsmall)
1111 afs_uint32 bytesread, byteswritten;
1112 struct fetchOps *ops = NULL;
1114 afs_uint32 moredata = 0;
1119 osi_timeval32_t xferStartTime; /*FS xfer start time */
1120 afs_size_t bytesToXfer = 0, bytesXferred = 0;
1123 AFS_STATCNT(CacheFetchProc);
1125 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
1129 * avc->lock(R) if setLocks && !slowPass
1130 * avc->lock(W) if !setLocks || slowPass
1133 code = rxfs_fetchInit(
1134 tc, rxconn, avc, base, size, &length, adc, fP, &ops, &rock);
1137 osi_GetTime(&xferStartTime);
1138 #endif /* AFS_NOSTATS */
1140 adc->validPos = base;
1147 if (avc->f.states & CForeign) {
1148 code = (*ops->more)(rock, &length, &moredata);
1153 bytesToXfer += length;
1154 #endif /* AFS_NOSTATS */
1155 while (length > 0) {
1156 #ifdef RX_KERNEL_TRACE
1157 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1160 code = (*ops->read)(rock, length, &bytesread);
1161 #ifdef RX_KERNEL_TRACE
1162 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1166 bytesXferred += bytesread;
1167 #endif /* AFS_NOSTATS */
1169 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1170 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1171 ICL_TYPE_INT32, length);
1175 code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
1178 offset += bytesread;
1180 length -= bytesread;
1181 adc->validPos = base;
1182 if (afs_osi_Wakeup(&adc->validPos) == 0)
1183 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
1184 __FILE__, ICL_TYPE_INT32, __LINE__,
1185 ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
1192 code = (*ops->close)(rock, avc, adc, tsmall);
1194 code = (*ops->destroy)(&rock, code);
1197 FillStoreStats(code, AFS_STATS_FS_XFERIDX_FETCHDATA, xferStartTime,
1198 bytesToXfer, bytesXferred);