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 */
14 #ifndef AFS_LINUX22_ENV
15 #include "rpc/types.h"
22 #endif /* AFS_ALPHA_ENV */
23 #include "afsincludes.h" /* Afs-based standard headers */
24 #include "afs/afs_stats.h" /* statistics */
25 #include "afs_prototypes.h"
27 extern int cacheDiskType;
31 FillStoreStats(int code, int idx, osi_timeval_t xferStartTime,
32 afs_size_t bytesToXfer, afs_size_t bytesXferred)
34 struct afs_stats_xferData *xferP;
35 osi_timeval_t xferStopTime;
36 osi_timeval_t elapsedTime;
38 xferP = &(afs_stats_cmfullperf.rpc.fsXferTimes[idx]);
39 osi_GetuTime(&xferStopTime);
42 (xferP->numSuccesses)++;
43 afs_stats_XferSumBytes[idx] += bytesXferred;
44 (xferP->sumBytes) += (afs_stats_XferSumBytes[idx] >> 10);
45 afs_stats_XferSumBytes[idx] &= 0x3FF;
46 if (bytesXferred < xferP->minBytes)
47 xferP->minBytes = bytesXferred;
48 if (bytesXferred > xferP->maxBytes)
49 xferP->maxBytes = bytesXferred;
52 * Tally the size of the object. Note: we tally the actual size,
53 * NOT the number of bytes that made it out over the wire.
55 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0) (xferP->count[0])++;
56 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1) (xferP->count[1])++;
57 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2) (xferP->count[2])++;
58 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3) (xferP->count[3])++;
59 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4) (xferP->count[4])++;
60 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5) (xferP->count[5])++;
61 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6) (xferP->count[6])++;
62 else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7) (xferP->count[7])++;
66 afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
67 afs_stats_AddTo((xferP->sumTime), elapsedTime);
68 afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
69 if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
70 afs_stats_TimeAssign((xferP->minTime), elapsedTime);
72 if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
73 afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
77 #endif /* AFS_NOSTATS */
79 /* rock and operations for RX_FILESERVER */
84 rxfs_storeUfsPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
86 *tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
91 rxfs_storeMemPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
94 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *) r;
97 code = rx_WritevAlloc(v->call, v->tiov, &v->tnio, RX_MAXIOVECS, size);
100 code = rx_Error(v->call);
112 rxfs_storeUfsRead(void *r, struct osi_file *tfile, afs_uint32 offset,
113 afs_uint32 tlen, afs_uint32 *bytesread)
116 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
119 code = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
125 #if defined(KERNEL_HAVE_UERROR)
133 rxfs_storeMemRead(void *r, struct osi_file *tfile, afs_uint32 offset,
134 afs_uint32 tlen, afs_uint32 *bytesread)
137 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
138 struct memCacheEntry *mceP = (struct memCacheEntry *)tfile;
141 code = afs_MemReadvBlk(mceP, offset, v->tiov, v->tnio, tlen);
149 rxfs_storeMemWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
152 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
155 code = rx_Writev(v->call, v->tiov, v->tnio, l);
158 code = rx_Error(v->call);
159 return (code ? code : -33);
161 *byteswritten = code;
166 rxfs_storeUfsWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
169 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
172 code = rx_Write(v->call, v->tbuffer, l);
173 /* writing 0 bytes will
174 * push a short packet. Is that really what we want, just because the
175 * data didn't come back from the disk yet? Let's try it and see. */
178 code = rx_Error(v->call);
179 return (code ? code : -33);
181 *byteswritten = code;
186 rxfs_storePadd(void *rock, afs_uint32 size)
190 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
193 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
194 memset(v->tbuffer, 0, AFS_LRALLOCSIZ);
197 tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
199 code = rx_Write(v->call, v->tbuffer, tlen);
203 return -33; /* XXX */
210 rxfs_storeStatus(void *rock)
212 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
214 if (rx_GetRemoteStatus(v->call) & 1)
220 rxfs_storeClose(void *r, struct AFSFetchStatus *OutStatus, int *doProcessFS)
223 struct AFSVolSync tsync;
224 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
229 #ifdef AFS_64BIT_CLIENT
231 code = EndRXAFS_StoreData64(v->call, OutStatus, &tsync);
234 code = EndRXAFS_StoreData(v->call, OutStatus, &tsync);
237 *doProcessFS = 1; /* Flag to run afs_ProcessFS() later on */
243 rxfs_storeDestroy(void **r, afs_int32 error)
245 afs_int32 code = error;
246 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
251 code = rx_EndCall(v->call, error);
257 osi_FreeLargeSpace(v->tbuffer);
259 osi_FreeSmallSpace(v->tiov);
260 osi_FreeSmallSpace(v);
265 afs_GenericStoreProc(struct storeOps *ops, void *rock,
266 struct dcache *tdc, int *shouldwake,
267 afs_size_t *bytesXferred)
269 struct rxfs_storeVariables *svar = rock;
270 afs_uint32 tlen, bytesread, byteswritten;
274 struct osi_file *tfile;
276 size = tdc->f.chunkBytes;
278 tfile = afs_CFileOpen(&tdc->f.inode);
281 code = (*ops->prepare)(rock, size, &tlen);
285 code = (*ops->read)(rock, tfile, offset, tlen, &bytesread);
290 code = (*ops->write)(rock, tlen, &byteswritten);
294 *bytesXferred += byteswritten;
295 #endif /* AFS_NOSTATS */
300 * if file has been locked on server, can allow
303 if (shouldwake && *shouldwake && ((*ops->status)(rock) == 0)) {
304 *shouldwake = 0; /* only do this once */
305 afs_wakeup(svar->vcache);
308 afs_CFileClose(tfile);
314 struct storeOps rxfs_storeUfsOps = {
315 #ifndef HAVE_STRUCT_LABEL_SUPPORT
316 rxfs_storeUfsPrepare,
325 .prepare = rxfs_storeUfsPrepare,
326 .read = rxfs_storeUfsRead,
327 .write = rxfs_storeUfsWrite,
328 .status = rxfs_storeStatus,
329 .padd = rxfs_storePadd,
330 .close = rxfs_storeClose,
331 .destroy = rxfs_storeDestroy,
332 #ifdef AFS_LINUX26_ENV
333 .storeproc = afs_linux_storeproc
335 .storeproc = afs_GenericStoreProc
341 struct storeOps rxfs_storeMemOps = {
342 #ifndef HAVE_STRUCT_LABEL_SUPPORT
343 rxfs_storeMemPrepare,
352 .prepare = rxfs_storeMemPrepare,
353 .read = rxfs_storeMemRead,
354 .write = rxfs_storeMemWrite,
355 .status = rxfs_storeStatus,
356 .padd = rxfs_storePadd,
357 .close = rxfs_storeClose,
358 .destroy = rxfs_storeDestroy,
359 .storeproc = afs_GenericStoreProc
364 rxfs_storeInit(struct vcache *avc, struct afs_conn *tc,
365 struct rx_connection *rxconn, afs_size_t base,
366 afs_size_t bytes, afs_size_t length,
367 int sync, struct storeOps **ops, void **rock)
370 struct rxfs_storeVariables *v;
375 v = osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
377 osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
378 memset(v, 0, sizeof(struct rxfs_storeVariables));
380 v->InStatus.ClientModTime = avc->f.m.Date;
381 v->InStatus.Mask = AFS_SETMODTIME;
384 v->InStatus.Mask |= AFS_FSYNC;
386 v->call = rx_NewCall(rxconn);
388 #ifdef AFS_64BIT_CLIENT
389 if (!afs_serverHasNo64Bit(tc))
390 code = StartRXAFS_StoreData64(
391 v->call, (struct AFSFid*)&avc->f.fid.Fid,
392 &v->InStatus, base, bytes, length);
394 if (length > 0xFFFFFFFF)
397 afs_int32 t1 = base, t2 = bytes, t3 = length;
398 code = StartRXAFS_StoreData(v->call,
399 (struct AFSFid *) &avc->f.fid.Fid,
400 &v->InStatus, t1, t2, t3);
402 #else /* AFS_64BIT_CLIENT */
403 code = StartRXAFS_StoreData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
404 &v->InStatus, base, bytes, length);
405 #endif /* AFS_64BIT_CLIENT */
410 osi_FreeSmallSpace(v);
413 if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
414 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
417 ("rxfs_storeInit: osi_AllocLargeSpace for iovecs returned NULL\n");
418 *ops = (struct storeOps *) &rxfs_storeUfsOps;
420 v->tiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
423 ("rxfs_storeInit: osi_AllocSmallSpace for iovecs returned NULL\n");
424 *ops = (struct storeOps *) &rxfs_storeMemOps;
426 /* do this at a higher level now -- it's a parameter */
427 /* for now, only do 'continue from close' code if file fits in one
428 * chunk. Could clearly do better: if only one modified chunk
429 * then can still do this. can do this on *last* modified chunk */
430 length = avc->f.m.Length - 1; /* byte position of last byte we'll store */
432 if (AFS_CHUNK(length) != 0)
443 unsigned int storeallmissing = 0;
445 * Called for each chunk upon store.
447 * \param avc Ptr to the vcache entry of the file being stored.
448 * \param dclist pointer to the list of dcaches
449 * \param bytes total number of bytes for the current operation
450 * \param anewDV Ptr to the dataversion after store
451 * \param doProcessFS pointer to the "do process FetchStatus" flag
452 * \param OutStatus pointer to the FetchStatus as returned by the fileserver
453 * \param nchunks number of dcaches to consider
454 * \param nomore copy of the "no more data" flag
455 * \param ops pointer to the block of storeOps to be used for this operation
456 * \param rock pointer to the opaque protocol-specific data of this operation
459 afs_CacheStoreDCaches(struct vcache *avc, struct dcache **dclist,
460 afs_size_t bytes, afs_hyper_t *anewDV, int *doProcessFS,
461 struct AFSFetchStatus *OutStatus, afs_uint32 nchunks,
462 int nomore, struct storeOps *ops, void *rock)
464 int *shouldwake = NULL;
467 afs_size_t bytesXferred;
470 osi_timeval_t xferStartTime; /*FS xfer start time */
471 afs_size_t bytesToXfer = 10000; /* # bytes to xfer */
472 #endif /* AFS_NOSTATS */
474 osi_Assert(nchunks != 0);
476 for (i = 0; i < nchunks && !code; i++) {
478 struct dcache *tdc = dclist[i];
482 afs_warn("afs: missing dcache!\n");
484 continue; /* panic? */
486 size = tdc->f.chunkBytes;
487 afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2, ICL_TYPE_POINTER, avc,
488 ICL_TYPE_INT32, tdc->f.chunk, ICL_TYPE_INT32, tdc->index,
489 ICL_TYPE_INT32, afs_inode2trace(&tdc->f.inode));
492 if (avc->asynchrony == -1) {
493 if (afs_defaultAsynchrony > (bytes - stored))
494 shouldwake = &nomore;
496 else if ((afs_uint32) avc->asynchrony >= (bytes - stored))
497 shouldwake = &nomore;
500 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
501 ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
502 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, size);
504 AFS_STATCNT(CacheStoreProc);
506 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
507 avc->f.truncPos = AFS_NOTRUNC;
510 * In this case, size is *always* the amount of data we'll be trying
515 osi_GetuTime(&xferStartTime);
516 #endif /* AFS_NOSTATS */
519 code = (*ops->storeproc)(ops, rock, tdc, shouldwake,
522 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
523 ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
524 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, size);
527 FillStoreStats(code, AFS_STATS_FS_XFERIDX_STOREDATA,
528 xferStartTime, bytesToXfer, bytesXferred);
529 #endif /* AFS_NOSTATS */
531 if ((tdc->f.chunkBytes < afs_OtherCSize)
532 && (i < (nchunks - 1)) && code == 0) {
533 code = (*ops->padd)(rock, afs_OtherCSize - tdc->f.chunkBytes);
535 stored += tdc->f.chunkBytes;
536 /* ideally, I'd like to unlock the dcache and turn
537 * off the writing bit here, but that would
538 * require being able to retry StoreAllSegments in
539 * the event of a failure. It only really matters
540 * if user can't read from a 'locked' dcache or
541 * one which has the writing bit turned on. */
545 code = (*ops->close)(rock, OutStatus, doProcessFS);
546 /* if this succeeds, dv has been bumped. */
553 code = (*ops->destroy)(&rock, code);
555 /* if we errored, can't trust this. */
562 #define lmin(a,b) (((a) < (b)) ? (a) : (b))
566 * \param dclist pointer to the list of dcaches
567 * \param avc Ptr to the vcache entry.
568 * \param areq Ptr to the request structure
569 * \param sync sync flag
570 * \param minj the chunk offset for this call
571 * \param high index of last dcache to store
572 * \param moredata the moredata flag
573 * \param anewDV Ptr to the dataversion after store
574 * \param amaxStoredLength Ptr to the amount of that is actually stored
576 * \note Environment: Nothing interesting.
579 afs_CacheStoreVCache(struct dcache **dcList, struct vcache *avc,
580 struct vrequest *areq, int sync, unsigned int minj,
581 unsigned int high, unsigned int moredata,
582 afs_hyper_t *anewDV, afs_size_t *amaxStoredLength)
585 struct storeOps *ops;
589 struct AFSFetchStatus OutStatus;
591 afs_size_t base, bytes, length;
593 unsigned int first = 0;
595 struct rx_connection *rxconn;
597 for (bytes = 0, j = 0; !code && j <= high; j++) {
599 ObtainSharedLock(&(dcList[j]->lock), 629);
602 bytes += dcList[j]->f.chunkBytes;
603 if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
604 && (dcList[j]->f.chunk - minj < high)
606 int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
610 if (bytes && (j == high || !dcList[j + 1])) {
612 struct dcache **dclist = &dcList[first];
613 /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
614 base = AFS_CHUNKTOBASE(first + minj);
617 * take a list of dcache structs and send them all off to the server
618 * the list must be in order, and the chunks contiguous.
619 * Note - there is no locking done by this code currently. For
620 * safety's sake, xdcache could be locked over the entire call.
621 * However, that pretty well ties up all the threads. Meantime, all
622 * the chunks _MUST_ have their refcounts bumped.
623 * The writes done before a store back will clear setuid-ness
625 * We can permit CacheStoreProc to wake up the user process IFF we
626 * are doing the last RPC for this close, ie, storing back the last
627 * set of contiguous chunks of a file.
630 nchunks = 1 + j - first;
631 nomore = !(moredata || (j != high));
632 length = lmin(avc->f.m.Length, avc->f.truncPos);
633 afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64,
634 ICL_TYPE_FID, &avc->f.fid.Fid, ICL_TYPE_OFFSET,
635 ICL_HANDLE_OFFSET(base), ICL_TYPE_OFFSET,
636 ICL_HANDLE_OFFSET(bytes), ICL_TYPE_OFFSET,
637 ICL_HANDLE_OFFSET(length));
640 tc = afs_Conn(&avc->f.fid, areq, 0, &rxconn);
642 #ifdef AFS_64BIT_CLIENT
645 code = rxfs_storeInit(avc, tc, rxconn, base, bytes, length,
648 code = afs_CacheStoreDCaches(avc, dclist, bytes, anewDV,
649 &doProcessFS, &OutStatus,
650 nchunks, nomore, ops, rock);
653 #ifdef AFS_64BIT_CLIENT
654 if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
655 afs_serverSetNo64Bit(tc);
658 #endif /* AFS_64BIT_CLIENT */
660 (tc, rxconn, code, &avc->f.fid, areq,
661 AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
664 /* put back all remaining locked dcache entries */
665 for (i = 0; i < nchunks; i++) {
666 struct dcache *tdc = dclist[i];
668 if (afs_indexFlags[tdc->index] & IFDataMod) {
670 * LOCKXXX -- should hold afs_xdcache(W) when
671 * modifying afs_indexFlags.
673 afs_indexFlags[tdc->index] &= ~IFDataMod;
674 afs_stats_cmperf.cacheCurrDirtyChunks--;
675 afs_indexFlags[tdc->index] &= ~IFDirtyPages;
676 if (sync & AFS_VMSYNC_INVAL) {
677 /* since we have invalidated all the pages of this
678 ** vnode by calling osi_VM_TryToSmush, we can
679 ** safely mark this dcache entry as not having
680 ** any pages. This vnode now becomes eligible for
681 ** reclamation by getDownD.
683 afs_indexFlags[tdc->index] &= ~IFAnyPages;
687 UpgradeSToWLock(&tdc->lock, 628);
688 tdc->f.states &= ~DWriting; /* correct? */
689 tdc->dflags |= DFEntryMod;
690 ReleaseWriteLock(&tdc->lock);
692 /* Mark the entry as released */
697 /* Now copy out return params */
698 UpgradeSToWLock(&avc->lock, 28); /* keep out others for a while */
699 afs_ProcessFS(avc, &OutStatus, areq);
700 /* Keep last (max) size of file on server to see if
701 * we need to call afs_StoreMini to extend the file.
704 *amaxStoredLength = OutStatus.Length;
705 ConvertWToSLock(&avc->lock);
710 for (j++; j <= high; j++) {
712 ReleaseSharedLock(&(dcList[j]->lock));
713 afs_PutDCache(dcList[j]);
714 /* Releasing entry */
720 afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
721 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
729 /* rock and operations for RX_FILESERVER */
731 struct rxfs_fetchVariables {
732 struct rx_call *call;
736 afs_int32 hasNo64bit;
742 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
746 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
749 tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
751 code = rx_Read(v->call, v->tbuffer, tlen);
760 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
763 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
767 code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
777 rxfs_fetchMemWrite(void *r, struct osi_file *fP, afs_uint32 offset,
778 afs_uint32 tlen, afs_uint32 *byteswritten)
781 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
782 struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
784 code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
788 *byteswritten = code;
793 rxfs_fetchUfsWrite(void *r, struct osi_file *fP, afs_uint32 offset,
794 afs_uint32 tlen, afs_uint32 *byteswritten)
797 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
799 code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
803 *byteswritten = code;
809 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
810 struct afs_FetchOutput *o)
812 afs_int32 code, code1 = 0;
813 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
819 #ifdef AFS_64BIT_CLIENT
821 code = EndRXAFS_FetchData64(v->call, &o->OutStatus, &o->CallBack,
825 code = EndRXAFS_FetchData(v->call, &o->OutStatus, &o->CallBack,
827 code1 = rx_EndCall(v->call, code);
838 rxfs_fetchDestroy(void **r, afs_int32 error)
840 afs_int32 code = error;
841 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
846 code = rx_EndCall(v->call, error);
852 osi_FreeLargeSpace(v->tbuffer);
854 osi_FreeSmallSpace(v->iov);
855 osi_FreeSmallSpace(v);
860 rxfs_fetchMore(void *r, afs_int32 *length, afs_uint32 *moredata)
863 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
866 * The fetch protocol is extended for the AFS/DFS translator
867 * to allow multiple blocks of data, each with its own length,
868 * to be returned. As long as the top bit is set, there are more
871 * We do not do this for AFS file servers because they sometimes
872 * return large negative numbers as the transfer size.
876 code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
878 *length = ntohl(*length);
879 if (code != sizeof(afs_int32)) {
880 code = rx_Error(v->call);
882 return (code ? code : -1); /* try to return code, not -1 */
885 *moredata = *length & 0x80000000;
886 *length &= ~0x80000000;
891 struct fetchOps rxfs_fetchUfsOps = {
900 struct fetchOps rxfs_fetchMemOps = {
909 rxfs_fetchInit(struct afs_conn *tc, struct rx_connection *rxconn,
910 struct vcache *avc, afs_offs_t base,
911 afs_uint32 size, afs_int32 *alength, struct dcache *adc,
912 struct osi_file *fP, struct fetchOps **ops, void **rock)
914 struct rxfs_fetchVariables *v;
915 int code = 0, code1 = 0;
916 #ifdef AFS_64BIT_CLIENT
917 afs_uint32 length_hi = 0;
919 afs_uint32 length, bytes;
921 v = (struct rxfs_fetchVariables *)
922 osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
924 osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
925 memset(v, 0, sizeof(struct rxfs_fetchVariables));
928 v->call = rx_NewCall(rxconn);
931 #ifdef AFS_64BIT_CLIENT
932 afs_size_t length64; /* as returned from server */
933 if (!afs_serverHasNo64Bit(tc)) {
934 afs_uint64 llbytes = size;
936 code = StartRXAFS_FetchData64(v->call,
937 (struct AFSFid *) &avc->f.fid.Fid,
941 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
942 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
944 bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
946 if (bytes == sizeof(afs_int32)) {
947 length_hi = ntohl(length_hi);
949 code = rx_Error(v->call);
951 code1 = rx_EndCall(v->call, code);
957 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
958 if (base > 0x7FFFFFFF) {
965 v->call = rx_NewCall(rxconn);
967 StartRXAFS_FetchData(
968 v->call, (struct AFSFid*)&avc->f.fid.Fid,
972 afs_serverSetNo64Bit(tc);
976 bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
978 if (bytes == sizeof(afs_int32))
979 length = ntohl(length);
982 code = rx_Error(v->call);
983 code1 = rx_EndCall(v->call, code);
988 FillInt64(length64, length_hi, length);
989 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
990 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
992 ICL_HANDLE_OFFSET(length64));
994 #else /* AFS_64BIT_CLIENT */
996 code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
1002 rx_Read(v->call, (char *)&length, sizeof(afs_int32));
1004 if (bytes == sizeof(afs_int32)) {
1005 *alength = ntohl(length);
1007 code = rx_Error(v->call);
1008 code1 = rx_EndCall(v->call, code);
1012 #endif /* AFS_64BIT_CLIENT */
1016 /* We need to cast here, in order to avoid issues if *alength is
1017 * negative. Some, older, fileservers can return a negative length,
1018 * which the rest of the code deals correctly with. */
1019 if (code == 0 && *alength > (afs_int32) size) {
1020 /* The fileserver told us it is going to send more data than we
1021 * requested. It shouldn't do that, and accepting that much data
1022 * can make us take up more cache space than we're supposed to,
1024 code = rx_Error(v->call);
1026 code1 = rx_EndCall(v->call, code);
1036 osi_FreeSmallSpace(v);
1039 if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
1040 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1042 osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
1043 osi_Assert(WriteLocked(&adc->lock));
1045 *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
1048 afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
1049 ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
1050 ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
1052 * We need to alloc the iovecs on the heap so that they are "pinned"
1053 * rather than declare them on the stack - defect 11272
1055 v->iov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
1057 osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace for iovecs returned NULL\n");
1058 *ops = (struct fetchOps *) &rxfs_fetchMemOps;
1066 * Routine called on fetch; also tells people waiting for data
1067 * that more has arrived.
1069 * \param tc Ptr to the AFS connection structure.
1070 * \param rxconn Ptr to the Rx connection structure.
1071 * \param fP File descriptor for the cache file.
1072 * \param base Base offset to fetch.
1073 * \param adc Ptr to the dcache entry for the file, write-locked.
1074 * \param avc Ptr to the vcache entry for the file.
1075 * \param size Amount of data that should be fetched.
1076 * \param tsmall Ptr to the afs_FetchOutput structure.
1078 * \note Environment: Nothing interesting.
1081 afs_CacheFetchProc(struct afs_conn *tc, struct rx_connection *rxconn,
1082 struct osi_file *fP, afs_size_t base,
1083 struct dcache *adc, struct vcache *avc, afs_int32 size,
1084 struct afs_FetchOutput *tsmall)
1088 afs_uint32 bytesread, byteswritten;
1089 struct fetchOps *ops = NULL;
1091 afs_uint32 moredata = 0;
1096 osi_timeval_t xferStartTime; /*FS xfer start time */
1097 afs_size_t bytesToXfer = 0, bytesXferred = 0;
1100 AFS_STATCNT(CacheFetchProc);
1102 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
1106 * avc->lock(R) if setLocks && !slowPass
1107 * avc->lock(W) if !setLocks || slowPass
1110 code = rxfs_fetchInit(
1111 tc, rxconn, avc, base, size, &length, adc, fP, &ops, &rock);
1114 osi_GetuTime(&xferStartTime);
1115 #endif /* AFS_NOSTATS */
1118 adc->validPos = base;
1122 if (avc->f.states & CForeign) {
1123 code = (*ops->more)(rock, &length, &moredata);
1128 bytesToXfer += length;
1129 #endif /* AFS_NOSTATS */
1130 while (length > 0) {
1131 #ifdef RX_KERNEL_TRACE
1132 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1135 code = (*ops->read)(rock, length, &bytesread);
1136 #ifdef RX_KERNEL_TRACE
1137 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1141 bytesXferred += bytesread;
1142 #endif /* AFS_NOSTATS */
1144 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1145 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1146 ICL_TYPE_INT32, length);
1150 code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
1153 offset += bytesread;
1155 length -= bytesread;
1156 adc->validPos = base;
1157 if (afs_osi_Wakeup(&adc->validPos) == 0)
1158 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
1159 __FILE__, ICL_TYPE_INT32, __LINE__,
1160 ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
1166 code = (*ops->close)(rock, avc, adc, tsmall);
1168 (*ops->destroy)(&rock, code);
1171 FillStoreStats(code, AFS_STATS_FS_XFERIDX_FETCHDATA, xferStartTime,
1172 bytesToXfer, bytesXferred);