Limited the scope of some local variables in afs_CacheStoreProc
[openafs.git] / src / afs / afs_fetchstore.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 #include "afs/sysincludes.h"    /* Standard vendor system headers */
14 #ifndef AFS_LINUX22_ENV
15 #include "rpc/types.h"
16 #endif
17 #ifdef  AFS_ALPHA_ENV
18 #undef kmem_alloc
19 #undef kmem_free
20 #undef mem_alloc
21 #undef mem_free
22 #undef register
23 #endif /* AFS_ALPHA_ENV */
24 #include "afsincludes.h"        /* Afs-based standard headers */
25 #include "afs/afs_stats.h"      /* statistics */
26 #include "afs_prototypes.h"
27
28 extern int cacheDiskType;
29
30 /* rock and operations for RX_FILESERVER */
31
32 struct rxfs_storeVariables {
33     struct rx_call *call;
34     char *tbuffer;
35     struct iovec *tiov;
36     afs_uint32 tnio;
37     afs_int32 hasNo64bit;
38     struct AFSStoreStatus InStatus;
39 };
40
41 afs_int32
42 rxfs_storeUfsPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
43 {
44     *tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
45     return 0;
46 }
47
48 afs_int32
49 rxfs_storeMemPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
50 {
51     afs_int32 code;
52     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *) r;
53
54     *tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
55     RX_AFS_GUNLOCK();
56     code = rx_WritevAlloc(v->call, v->tiov, &v->tnio, RX_MAXIOVECS, *tlen);
57     RX_AFS_GLOCK();
58     if (code <= 0) {
59         code = rx_Error(v->call);
60         if ( !code )
61             code = -33;
62     }
63     else {
64         *tlen = code;
65         code = 0;
66     }
67     return code;
68 }
69
70 afs_int32
71 rxfs_storeUfsRead(void *r, struct osi_file *tfile, afs_uint32 offset,
72                   afs_uint32 tlen, afs_uint32 *bytesread)
73 {
74     afs_int32 code;
75     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
76
77     *bytesread = 0;
78     code = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
79     if (code < 0)
80         return EIO;
81     *bytesread = code;
82     if (code == tlen)
83         return 0;
84 #if defined(KERNEL_HAVE_UERROR)
85     if (getuerror())
86         return EIO;
87 #endif
88     return 0;
89 }
90
91 afs_int32
92 rxfs_storeMemRead(void *r, struct osi_file *tfile, afs_uint32 offset,
93                   afs_uint32 tlen, afs_uint32 *bytesread)
94 {
95     afs_int32 code;
96     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
97     struct memCacheEntry *mceP = (struct memCacheEntry *)tfile;
98
99     *bytesread = 0;
100     code = afs_MemReadvBlk(mceP, offset, v->tiov, v->tnio, tlen);
101     if (code != tlen)
102         return -33;
103     *bytesread = code;
104     return 0;
105 }
106
107 afs_int32
108 rxfs_storeMemWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
109 {
110     afs_int32 code;
111     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
112
113     RX_AFS_GUNLOCK();
114     code = rx_Writev(v->call, v->tiov, v->tnio, l);
115     RX_AFS_GLOCK();
116     if (code != l) {
117         code = rx_Error(v->call);
118         return (code ? code : -33);
119     }
120     *byteswritten = code;
121     return 0;
122 }
123
124 afs_int32
125 rxfs_storeUfsWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
126 {
127     afs_int32 code;
128     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
129
130     RX_AFS_GUNLOCK();
131     code = rx_Write(v->call, v->tbuffer, l);
132         /* writing 0 bytes will
133          * push a short packet.  Is that really what we want, just because the
134          * data didn't come back from the disk yet?  Let's try it and see. */
135     RX_AFS_GLOCK();
136     if (code != l) {
137         code = rx_Error(v->call);
138         return (code ? code : -33);
139     }
140     *byteswritten = code;
141     return 0;
142 }
143
144 afs_int32
145 rxfs_storeStatus(void *rock)
146 {
147     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
148
149     if (rx_GetRemoteStatus(v->call) & 1)
150         return 0;
151     return 1;
152 }
153
154 afs_int32
155 rxfs_storeClose(void *r, struct AFSFetchStatus *OutStatus, int *doProcessFS)
156 {
157     afs_int32 code;
158     struct AFSVolSync tsync;
159     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
160
161     RX_AFS_GUNLOCK();
162     code = EndRXAFS_StoreData(v->call, OutStatus, &tsync);
163     RX_AFS_GLOCK();
164     if (!code)
165         *doProcessFS = 1;       /* Flag to run afs_ProcessFS() later on */
166
167     return code;
168 }
169
170 afs_int32
171 rxfs_storeDestroy(void **r, afs_int32 error)
172 {
173     afs_int32 code = error;
174     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
175
176     *r = NULL;
177     if (v->call) {
178         afs_int32 code2;
179         RX_AFS_GUNLOCK();
180         code2 = rx_EndCall(v->call, code);
181         RX_AFS_GLOCK();
182         if (code2)
183             code = code2;
184     }
185     if (v->tbuffer)
186         osi_FreeLargeSpace(v->tbuffer);
187     if (v->tiov)
188         osi_FreeSmallSpace(v->tiov);
189     osi_FreeSmallSpace(v);
190     return code;
191 }
192
193 static
194 struct storeOps rxfs_storeUfsOps = {
195     rxfs_storeUfsPrepare,
196     rxfs_storeUfsRead,
197     rxfs_storeUfsWrite,
198     rxfs_storeStatus,
199     rxfs_storeClose,
200     rxfs_storeDestroy
201 };
202
203 static
204 struct storeOps rxfs_storeMemOps = {
205     rxfs_storeMemPrepare,
206     rxfs_storeMemRead,
207     rxfs_storeMemWrite,
208     rxfs_storeStatus,
209     rxfs_storeClose,
210     rxfs_storeDestroy
211 };
212
213 afs_int32
214 rxfs_storeInit(struct vcache *avc, struct afs_conn *tc, afs_size_t tlen,
215                 afs_size_t bytes, afs_size_t base,
216                 struct storeOps **ops, void **rock)
217 {
218     afs_int32 code;
219     struct rxfs_storeVariables *v;
220     struct rx_call *tcall;
221
222     if ( !tc )
223         return -1;
224
225     v = (struct rxfs_storeVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
226     if (!v)
227         osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
228     memset(v, 0, sizeof(struct rxfs_storeVariables));
229
230     v->InStatus.ClientModTime = avc->f.m.Date;
231     v->InStatus.Mask = AFS_SETMODTIME;
232
233     RX_AFS_GUNLOCK();
234     tcall = rx_NewCall(tc->id);
235 #ifdef AFS_64BIT_CLIENT
236     if (!afs_serverHasNo64Bit(tc))
237         code = StartRXAFS_StoreData64(tcall, (struct AFSFid *) &avc->f.fid.Fid,
238                                    &v->InStatus, base, bytes, tlen);
239     else {
240         if (tlen > 0xFFFFFFFF) {
241             code = EFBIG;
242         }
243         else {
244             afs_int32 t1, t2, t3;
245             t1 = base;
246             t2 = bytes;
247             t3 = tlen;
248             code = StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->f.fid.Fid,
249                                      &v->InStatus, t1, t2, t3);
250         }
251     }
252 #else /* AFS_64BIT_CLIENT */
253     code = StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->f.fid.Fid,
254                                 &v->InStatus, base, bytes, tlen);
255 #endif /* AFS_64BIT_CLIENT */
256     RX_AFS_GLOCK();
257
258     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
259         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
260         if (!v->tbuffer)
261             osi_Panic
262               ("rxfs_storeInit: osi_AllocLargeSpace for iovecs returned NULL\n");
263         *ops = (struct storeOps *) &rxfs_storeUfsOps;
264     } else {
265         v->tiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
266         if (!v->tiov)
267             osi_Panic
268               ("rxfs_storeInit: osi_AllocSmallSpace for iovecs returned NULL\n");
269         *ops = (struct storeOps *) &rxfs_storeMemOps;
270 #ifdef notdef
271         /* do this at a higher level now -- it's a parameter */
272         /* for now, only do 'continue from close' code if file fits in one
273          * chunk.  Could clearly do better: if only one modified chunk
274          * then can still do this.  can do this on *last* modified chunk */
275         tlen = avc->f.m.Length - 1;     /* byte position of last byte we'll store */
276         if (shouldWake) {
277             if (AFS_CHUNK(tlen) != 0)
278                 *shouldWake = 0;
279             else
280                 *shouldWake = 1;
281         }
282 #endif /* notdef */
283     }
284
285     v->call = tcall;
286     *rock = (void *)v;
287     return 0;
288 }
289
290 extern unsigned int storeallmissing;
291 /*!
292  *      Called upon store.
293  *
294  * \param tc Ptr to the Rx connection structure involved.
295  * \param dclist pointer to the list of dcaches
296  * \param avc Ptr to the vcache entry.
297  * \param bytes per chunk
298  * \param base where to start the store
299  * \param length number of bytes to store
300  * \param anewDV Ptr to the dataversion after store
301  * \param doProcessFS Ptr to the processFS flag
302  * \param OutStatus Ptr to the OutStatus structure
303  * \param nchunks number of chunks to store
304  * \param nomoreP pointer to the "nomore" flag
305  *
306  * \note Environment: Nothing interesting.
307  */
308 int
309 afs_CacheStoreProc(register struct afs_conn *tc,
310                         struct dcache **dclist,
311                         struct vcache *avc,
312                         afs_size_t bytes,
313                         afs_size_t base,
314                         afs_size_t length,
315                         afs_hyper_t *anewDV,
316                         int *doProcessFS,
317                         struct AFSFetchStatus *OutStatus,
318                         afs_uint32 nchunks,
319                         int *nomoreP)
320 {
321     afs_int32 code = 0;
322     struct storeOps *ops;
323     void * rock = NULL;
324     int nomore = *nomoreP;
325     unsigned int i;
326 #ifndef AFS_NOSTATS
327     struct afs_stats_xferData *xferP;   /* Ptr to this op's xfer struct */
328     osi_timeval_t xferStartTime,        /*FS xfer start time */
329       xferStopTime;                     /*FS xfer stop time */
330     afs_size_t bytesToXfer = 10000;     /* # bytes to xfer */
331     afs_size_t bytesXferred = 10000;    /* # bytes actually xferred */
332 #endif /* AFS_NOSTATS */
333     XSTATS_DECLS;
334
335     code =  rxfs_storeInit(avc, tc, length, bytes, base, &ops, &rock);
336     if ( code ) {
337         osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed with %d", code);
338     }
339
340     for (i = 0; i < nchunks && !code; i++) {
341         int stored = 0;
342         struct osi_file *fP;
343         int offset = 0;
344         struct dcache *tdc = dclist[i];
345         afs_int32 alen = tdc->f.chunkBytes;
346         int *shouldwake;
347         if (!tdc) {
348             afs_warn("afs: missing dcache!\n");
349             storeallmissing++;
350             continue;   /* panic? */
351         }
352         afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2, ICL_TYPE_POINTER, avc,
353                     ICL_TYPE_INT32, tdc->f.chunk, ICL_TYPE_INT32, tdc->index,
354                     ICL_TYPE_INT32, afs_inode2trace(&tdc->f.inode));
355         shouldwake = 0;
356         if (nomore) {
357             if (avc->asynchrony == -1) {
358                 if (afs_defaultAsynchrony > (bytes - stored))
359                     shouldwake = &nomore;
360             }
361             else if ((afs_uint32) avc->asynchrony >= (bytes - stored))
362                 shouldwake = &nomore;
363         }
364         fP = afs_CFileOpen(&tdc->f.inode);
365
366         afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
367                   ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
368                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
369
370         AFS_STATCNT(CacheStoreProc);
371
372         XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
373         avc->f.truncPos = AFS_NOTRUNC;
374 #ifndef AFS_NOSTATS
375         /*
376          * In this case, alen is *always* the amount of data we'll be trying
377          * to ship here.
378          */
379         bytesToXfer = alen;
380         bytesXferred = 0;
381
382         xferP = &(afs_stats_cmfullperf.rpc.
383                         fsXferTimes[AFS_STATS_FS_XFERIDX_STOREDATA]);
384         osi_GetuTime(&xferStartTime);
385 #endif /* AFS_NOSTATS */
386
387         while ( alen > 0 ) {
388             afs_uint32 tlen;
389             afs_int32 bytesread, byteswritten;
390             code = (*ops->prepare)(rock, alen, &tlen);
391             if ( code )
392                 break;
393
394             code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
395             if (code)
396                 break;
397
398             tlen = bytesread;
399             code = (*ops->write)(rock, tlen, &byteswritten);
400             if (code)
401                 break;
402 #ifndef AFS_NOSTATS
403             bytesXferred += byteswritten;
404 #endif /* AFS_NOSTATS */
405
406             offset += tlen;
407             alen -= tlen;
408             /*
409              * if file has been locked on server, can allow
410              * store to continue
411              */
412             if (shouldwake && *shouldwake && ((*ops->status)(rock) == 0)) {
413                 *shouldwake = 0;        /* only do this once */
414                 afs_wakeup(avc);
415             }
416         }
417         afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
418                   ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
419                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
420
421 #ifndef AFS_NOSTATS
422         osi_GetuTime(&xferStopTime);
423         (xferP->numXfers)++;
424         if (!code) {
425             (xferP->numSuccesses)++;
426             afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] +=
427                 bytesXferred;
428             (xferP->sumBytes) +=
429                 (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] >> 10);
430             afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] &= 0x3FF;
431             if (bytesXferred < xferP->minBytes)
432                 xferP->minBytes = bytesXferred;
433             if (bytesXferred > xferP->maxBytes)
434                 xferP->maxBytes = bytesXferred;
435
436             /*
437              * Tally the size of the object.  Note: we tally the actual size,
438              * NOT the number of bytes that made it out over the wire.
439              */
440             if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
441                 (xferP->count[0])++;
442             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
443                 (xferP->count[1])++;
444             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
445                 (xferP->count[2])++;
446             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
447                 (xferP->count[3])++;
448             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
449                 (xferP->count[4])++;
450             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
451                 (xferP->count[5])++;
452             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
453                 (xferP->count[6])++;
454             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
455                 (xferP->count[7])++;
456             else
457                 (xferP->count[8])++;
458
459             afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
460             afs_stats_AddTo((xferP->sumTime), elapsedTime);
461             afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
462             if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
463                 afs_stats_TimeAssign((xferP->minTime), elapsedTime);
464             }
465             if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
466                 afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
467             }
468         }
469 #endif /* AFS_NOSTATS */
470
471         afs_CFileClose(fP);
472         if ((tdc->f.chunkBytes < afs_OtherCSize)
473             && (i < (nchunks - 1)) && code == 0) {
474             int bsent, tlen, sbytes =
475                 afs_OtherCSize - tdc->f.chunkBytes;
476             char *tbuffer =
477                 osi_AllocLargeSpace(AFS_LRALLOCSIZ);
478
479             while (sbytes > 0) {
480                 tlen = (sbytes > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : sbytes);
481                 memset(tbuffer, 0, tlen);
482                 RX_AFS_GUNLOCK();
483                 bsent = rx_Write(((struct rxfs_storeVariables*)rock)->call,
484                                         tbuffer, tlen);
485                 RX_AFS_GLOCK();
486
487                 if (bsent != tlen) {
488                     code = -33; /* XXX */
489                     break;
490                 }
491                 sbytes -= tlen;
492             }
493             osi_FreeLargeSpace(tbuffer);
494         }
495         stored += tdc->f.chunkBytes;
496
497         /* ideally, I'd like to unlock the dcache and turn
498          * off the writing bit here, but that would
499          * require being able to retry StoreAllSegments in
500          * the event of a failure. It only really matters
501          * if user can't read from a 'locked' dcache or
502          * one which has the writing bit turned on. */
503     }
504     if (!code) {
505         code = (*ops->close)(rock, OutStatus, doProcessFS);
506         if (*doProcessFS) {
507             hadd32(*anewDV, 1);
508         }
509         XSTATS_END_TIME;
510     }
511     code = (*ops->destroy)(&rock, code);
512
513     *nomoreP = nomore;
514     return code;
515 }
516
517 /* rock and operations for RX_FILESERVER */
518
519 struct rxfs_fetchVariables {
520     struct rx_call *call;
521     char *tbuffer;
522     struct iovec *iov;
523     afs_uint32 nio;
524     afs_int32 hasNo64bit;
525     afs_int32 iovno;
526     afs_int32 iovmax;
527 };
528
529 afs_int32
530 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
531 {
532     afs_int32 code;
533     afs_uint32 tlen;
534     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
535
536     *bytesread = 0;
537     tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
538     RX_AFS_GUNLOCK();
539     code = rx_Read(v->call, v->tbuffer, tlen);
540     RX_AFS_GLOCK();
541     if (code <= 0)
542         return -34;
543     *bytesread = code;
544     return 0;
545 }
546
547 afs_int32
548 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
549 {
550     afs_int32 code;
551     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
552
553     *bytesread = 0;
554     RX_AFS_GUNLOCK();
555     code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
556     RX_AFS_GLOCK();
557     if (code <= 0)
558         return -34;
559     *bytesread = code;
560     return 0;
561 }
562
563
564 afs_int32
565 rxfs_fetchMemWrite(void *r, struct osi_file *fP,
566                         afs_uint32 offset, afs_uint32 tlen,
567                         afs_uint32 *byteswritten)
568 {
569     afs_int32 code;
570     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
571     struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
572
573     code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
574     if (code != tlen) {
575         return EIO;
576     }
577     *byteswritten = code;
578     return 0;
579 }
580
581 afs_int32
582 rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
583                         afs_uint32 offset, afs_uint32 tlen,
584                         afs_uint32 *byteswritten)
585 {
586     afs_int32 code;
587     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
588
589     code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
590     if (code != tlen) {
591         return EIO;
592     }
593     *byteswritten = code;
594     return 0;
595 }
596
597
598 afs_int32
599 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
600                                         struct afs_FetchOutput *tsmall)
601 {
602     afs_int32 code, code1 = 0;
603     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
604
605     if (!v->call)
606         return -1;
607
608     RX_AFS_GUNLOCK();
609     code = EndRXAFS_FetchData(v->call, &tsmall->OutStatus,
610                               &tsmall->CallBack,
611                               &tsmall->tsync);
612     RX_AFS_GLOCK();
613
614     RX_AFS_GUNLOCK();
615     if (v->call)
616         code1 = rx_EndCall(v->call, code);
617     RX_AFS_GLOCK();
618     if (!code && code1)
619         code = code1;
620
621     v->call = NULL;
622
623     return code;
624 }
625
626 afs_int32
627 rxfs_fetchDestroy(void **r, afs_int32 error)
628 {
629     afs_int32 code = error;
630     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
631
632     *r = NULL;
633     if (v->tbuffer)
634         osi_FreeLargeSpace(v->tbuffer);
635     if (v->iov)
636         osi_FreeSmallSpace(v->iov);
637     osi_FreeSmallSpace(v);
638     return code;
639 }
640
641 afs_int32
642 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
643 {
644     afs_int32 code;
645     register struct rxfs_fetchVariables *v
646             = (struct rxfs_fetchVariables *)r;
647
648     RX_AFS_GUNLOCK();
649     code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
650     *length = ntohl(*length);
651     RX_AFS_GLOCK();
652     if (code != sizeof(afs_int32)) {
653         code = rx_Error(v->call);
654         return (code ? code : -1);      /* try to return code, not -1 */
655     }
656     return 0;
657 }
658
659 static
660 struct fetchOps rxfs_fetchUfsOps = {
661     rxfs_fetchMore,
662     rxfs_fetchUfsRead,
663     rxfs_fetchUfsWrite,
664     rxfs_fetchClose,
665     rxfs_fetchDestroy
666 };
667
668 static
669 struct fetchOps rxfs_fetchMemOps = {
670     rxfs_fetchMore,
671     rxfs_fetchMemRead,
672     rxfs_fetchMemWrite,
673     rxfs_fetchClose,
674     rxfs_fetchDestroy
675 };
676
677 afs_int32
678 rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
679                 afs_uint32 size, afs_uint32 *out_length, struct dcache *adc,
680                 struct osi_file *fP, struct fetchOps **ops, void **rock)
681 {
682     struct rxfs_fetchVariables *v;
683     int code, code1;
684     afs_int32 length_hi, length, bytes;
685 #ifdef AFS_64BIT_CLIENT
686     afs_size_t tsize;
687     afs_size_t lengthFound;     /* as returned from server */
688 #endif /* AFS_64BIT_CLIENT */
689
690     v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
691     if (!v)
692         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
693     memset(v, 0, sizeof(struct rxfs_fetchVariables));
694
695     RX_AFS_GUNLOCK();
696     v->call = rx_NewCall(tc->id);
697     RX_AFS_GLOCK();
698
699 #ifdef AFS_64BIT_CLIENT
700     length_hi = code = 0;
701     if (!afs_serverHasNo64Bit(tc)) {
702         tsize = size;
703         RX_AFS_GUNLOCK();
704         code = StartRXAFS_FetchData64(v->call, (struct AFSFid *)&avc->f.fid.Fid,
705                                         base, tsize);
706         if (code != 0) {
707             RX_AFS_GLOCK();
708             afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
709                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
710         } else {
711             bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
712             RX_AFS_GLOCK();
713             if (bytes == sizeof(afs_int32)) {
714                 length_hi = ntohl(length_hi);
715             } else {
716                 length_hi = 0;
717                 code = rx_Error(v->call);
718                 RX_AFS_GUNLOCK();
719                 code1 = rx_EndCall(v->call, code);
720                 RX_AFS_GLOCK();
721                 v->call = NULL;
722             }
723         }
724     }
725     if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
726         if (base > 0x7FFFFFFF) {
727             code = EFBIG;
728         } else {
729             afs_int32 pos;
730             pos = base;
731             RX_AFS_GUNLOCK();
732             if (!v->call)
733                 v->call = rx_NewCall(tc->id);
734             code =
735                 StartRXAFS_FetchData(v->call, (struct AFSFid *)
736                                      &avc->f.fid.Fid, pos,
737                                      size);
738             RX_AFS_GLOCK();
739         }
740         afs_serverSetNo64Bit(tc);
741     }
742     if (!code) {
743         RX_AFS_GUNLOCK();
744         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
745         RX_AFS_GLOCK();
746         if (bytes == sizeof(afs_int32))
747             length = ntohl(length);
748         else {
749             code = rx_Error(v->call);
750         }
751     }
752     FillInt64(lengthFound, length_hi, length);
753     afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
754                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
755                ICL_TYPE_OFFSET,
756                ICL_HANDLE_OFFSET(lengthFound));
757 #else /* AFS_64BIT_CLIENT */
758     RX_AFS_GUNLOCK();
759     code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
760                                  base, size);
761     RX_AFS_GLOCK();
762     if (code == 0) {
763         RX_AFS_GUNLOCK();
764         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
765         RX_AFS_GLOCK();
766         if (bytes == sizeof(afs_int32))
767             length = ntohl(length);
768         else
769             code = rx_Error(v->call);
770     }
771 #endif /* AFS_64BIT_CLIENT */
772     if (code) {
773         osi_FreeSmallSpace(v);
774         return code;
775     }
776
777     if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
778         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
779         if (!v->tbuffer)
780             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
781         osi_Assert(WriteLocked(&adc->lock));
782         fP->offset = 0;
783         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
784     }
785     else {
786         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
787                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
788                    ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
789         /*
790          * We need to alloc the iovecs on the heap so that they are "pinned"
791          * rather than declare them on the stack - defect 11272
792          */
793         v->iov = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
794                                                 RX_MAXIOVECS);
795         if (!v->iov)
796             osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
797         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
798     }
799     *rock = (void *)v;
800     *out_length = length;
801     return 0;
802 }
803
804
805 /*!
806  * Routine called on fetch; also tells people waiting for data
807  *      that more has arrived.
808  *
809  * \param tc Ptr to the Rx connection structure.
810  * \param fP File descriptor for the cache file.
811  * \param abase Base offset to fetch.
812  * \param adc Ptr to the dcache entry for the file, write-locked.
813  * \param avc Ptr to the vcache entry for the file.
814  * \param size Amount of data that should be fetched.
815  * \param tsmall Ptr to the afs_FetchOutput structure.
816  *
817  * \note Environment: Nothing interesting.
818  */
819 int
820 afs_CacheFetchProc(register struct afs_conn *tc,
821                     register struct osi_file *fP, afs_size_t abase,
822                     struct dcache *adc, struct vcache *avc,
823                     afs_int32 size,
824                     struct afs_FetchOutput *tsmall)
825 {
826     register afs_int32 code;
827     afs_uint32 length;
828     afs_uint32 bytesread, byteswritten;
829     struct fetchOps *ops = NULL;
830     void *rock = NULL;
831     int moredata = 0;
832     register int offset = 0;
833
834     XSTATS_DECLS;
835 #ifndef AFS_NOSTATS
836     struct afs_stats_xferData *xferP;   /* Ptr to this op's xfer struct */
837     osi_timeval_t xferStartTime,        /*FS xfer start time */
838                     xferStopTime;       /*FS xfer stop time */
839     afs_size_t bytesToXfer = 0, bytesXferred = 0;
840 #endif
841
842     AFS_STATCNT(CacheFetchProc);
843
844     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
845
846     code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
847
848 #ifndef AFS_NOSTATS
849     xferP =
850         &(afs_stats_cmfullperf.rpc.fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
851     osi_GetuTime(&xferStartTime);
852 #endif /* AFS_NOSTATS */
853
854     adc->validPos = abase;
855
856     if ( !code ) do {
857         if (moredata) {
858             code = (*ops->more)(rock, &length, &moredata);
859             if ( code )
860                 break;
861         }
862         /*
863          * The fetch protocol is extended for the AFS/DFS translator
864          * to allow multiple blocks of data, each with its own length,
865          * to be returned. As long as the top bit is set, there are more
866          * blocks expected.
867          *
868          * We do not do this for AFS file servers because they sometimes
869          * return large negative numbers as the transfer size.
870          */
871         if (avc->f.states & CForeign) {
872             moredata = length & 0x80000000;
873             length &= ~0x80000000;
874         } else {
875             moredata = 0;
876         }
877 #ifndef AFS_NOSTATS
878         bytesToXfer += length;
879 #endif /* AFS_NOSTATS */
880         while (length > 0) {
881 #ifdef RX_KERNEL_TRACE
882             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
883                        "before rx_Read");
884 #endif
885             code = (*ops->read)(rock, length, &bytesread);
886 #ifdef RX_KERNEL_TRACE
887             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
888                        "after rx_Read");
889 #endif
890 #ifndef AFS_NOSTATS
891             bytesXferred += bytesread;
892 #endif /* AFS_NOSTATS */
893             if ( code ) {
894                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
895                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
896                            ICL_TYPE_INT32, length);
897                 code = -34;
898                 break;
899             }
900             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
901             if ( code )
902                 break;
903             offset += bytesread;
904             abase += bytesread;
905             length -= bytesread;
906             adc->validPos = abase;
907             if (afs_osi_Wakeup(&adc->validPos) == 0)
908                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
909                            __FILE__, ICL_TYPE_INT32, __LINE__,
910                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
911                            adc->dflags);
912         }
913         code = 0;
914     } while (moredata);
915     if (!code)
916         code = (*ops->close)(rock, avc, adc, tsmall);
917     (*ops->destroy)(&rock, code);
918
919 #ifndef AFS_NOSTATS
920     osi_GetuTime(&xferStopTime);
921     (xferP->numXfers)++;
922     if (!code) {
923         (xferP->numSuccesses)++;
924         afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] += bytesXferred;
925         (xferP->sumBytes) +=
926                 (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
927         afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
928         if (bytesXferred < xferP->minBytes)
929             xferP->minBytes = bytesXferred;
930         if (bytesXferred > xferP->maxBytes)
931             xferP->maxBytes = bytesXferred;
932
933         /*
934          * Tally the size of the object.  Note: we tally the actual size,
935          * NOT the number of bytes that made it out over the wire.
936          */
937         if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
938             (xferP->count[0])++;
939         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
940             (xferP->count[1])++;
941         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
942             (xferP->count[2])++;
943         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
944             (xferP->count[3])++;
945         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
946             (xferP->count[4])++;
947         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
948             (xferP->count[5])++;
949         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
950             (xferP->count[6])++;
951         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
952             (xferP->count[7])++;
953         else
954             (xferP->count[8])++;
955
956         afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
957         afs_stats_AddTo((xferP->sumTime), elapsedTime);
958         afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
959         if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
960             afs_stats_TimeAssign((xferP->minTime), elapsedTime);
961         }
962         if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
963             afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
964         }
965     }
966 #endif
967     XSTATS_END_TIME;
968     return code;
969 }
970