17bf1bc767dff445442816f028b496e010bfc254
[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     afs_uint32 tlen;
323     struct storeOps *ops;
324     void * rock = NULL;
325     struct osi_file *fP;
326     int *shouldwake;
327     int nomore = *nomoreP;
328     struct dcache *tdc;
329     int stored = 0;
330     unsigned int i;
331     afs_int32 alen;
332 #ifndef AFS_NOSTATS
333     struct afs_stats_xferData *xferP;   /* Ptr to this op's xfer struct */
334     osi_timeval_t xferStartTime,        /*FS xfer start time */
335       xferStopTime;             /*FS xfer stop time */
336     afs_size_t bytesToXfer = 10000;     /* # bytes to xfer */
337     afs_size_t bytesXferred = 10000;    /* # bytes actually xferred */
338 #endif /* AFS_NOSTATS */
339     XSTATS_DECLS;
340
341     code =  rxfs_storeInit(avc, tc, length, bytes, base, &ops, &rock);
342     if ( code ) {
343         osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed with %d", code);
344     }
345
346     for (i = 0; i < nchunks && !code; i++) {
347         int offset = 0;
348         tdc = dclist[i];
349         alen = tdc->f.chunkBytes;
350         if (!tdc) {
351             afs_warn("afs: missing dcache!\n");
352             storeallmissing++;
353             continue;   /* panic? */
354         }
355         afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2, ICL_TYPE_POINTER, avc,
356                     ICL_TYPE_INT32, tdc->f.chunk, ICL_TYPE_INT32, tdc->index,
357                     ICL_TYPE_INT32, afs_inode2trace(&tdc->f.inode));
358         shouldwake = 0;
359         if (nomore) {
360             if (avc->asynchrony == -1) {
361                 if (afs_defaultAsynchrony > (bytes - stored))
362                     shouldwake = &nomore;
363             }
364             else if ((afs_uint32) avc->asynchrony >= (bytes - stored))
365                 shouldwake = &nomore;
366         }
367         fP = afs_CFileOpen(&tdc->f.inode);
368
369         afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
370                   ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
371                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
372
373         AFS_STATCNT(CacheStoreProc);
374
375         XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
376         avc->f.truncPos = AFS_NOTRUNC;
377 #ifndef AFS_NOSTATS
378         /*
379          * In this case, alen is *always* the amount of data we'll be trying
380          * to ship here.
381          */
382         bytesToXfer = alen;
383         bytesXferred = 0;
384
385         xferP = &(afs_stats_cmfullperf.rpc.
386                         fsXferTimes[AFS_STATS_FS_XFERIDX_STOREDATA]);
387         osi_GetuTime(&xferStartTime);
388 #endif /* AFS_NOSTATS */
389
390     while ( alen > 0 ) {
391             afs_int32 bytesread, byteswritten;
392             code = (*ops->prepare)(rock, alen, &tlen);
393             if ( code )
394                 break;
395
396             code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
397             if (code)
398                 break;
399
400             tlen = bytesread;
401             code = (*ops->write)(rock, tlen, &byteswritten);
402             if (code)
403                 break;
404 #ifndef AFS_NOSTATS
405             bytesXferred += byteswritten;
406 #endif /* AFS_NOSTATS */
407
408             offset += tlen;
409             alen -= tlen;
410             /*
411              * if file has been locked on server, can allow
412              * store to continue
413              */
414             if (shouldwake && *shouldwake && ((*ops->status)(rock) == 0)) {
415                 *shouldwake = 0;        /* only do this once */
416                 afs_wakeup(avc);
417             }
418         }
419         afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
420                   ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
421                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
422
423 #ifndef AFS_NOSTATS
424         osi_GetuTime(&xferStopTime);
425         (xferP->numXfers)++;
426         if (!code) {
427             (xferP->numSuccesses)++;
428             afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] +=
429                 bytesXferred;
430             (xferP->sumBytes) +=
431                 (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] >> 10);
432             afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_STOREDATA] &= 0x3FF;
433             if (bytesXferred < xferP->minBytes)
434                 xferP->minBytes = bytesXferred;
435             if (bytesXferred > xferP->maxBytes)
436                 xferP->maxBytes = bytesXferred;
437
438             /*
439              * Tally the size of the object.  Note: we tally the actual size,
440              * NOT the number of bytes that made it out over the wire.
441              */
442             if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
443                 (xferP->count[0])++;
444             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
445                 (xferP->count[1])++;
446             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
447                 (xferP->count[2])++;
448             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
449                 (xferP->count[3])++;
450             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
451                 (xferP->count[4])++;
452             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
453                 (xferP->count[5])++;
454             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
455                 (xferP->count[6])++;
456             else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
457                 (xferP->count[7])++;
458             else
459                 (xferP->count[8])++;
460
461             afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
462             afs_stats_AddTo((xferP->sumTime), elapsedTime);
463             afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
464             if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
465                 afs_stats_TimeAssign((xferP->minTime), elapsedTime);
466             }
467             if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
468                 afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
469             }
470         }
471 #endif /* AFS_NOSTATS */
472
473         afs_CFileClose(fP);
474         if ((tdc->f.chunkBytes < afs_OtherCSize)
475             && (i < (nchunks - 1)) && code == 0) {
476             int bsent, tlen, sbytes =
477                 afs_OtherCSize - tdc->f.chunkBytes;
478             char *tbuffer =
479                 osi_AllocLargeSpace(AFS_LRALLOCSIZ);
480
481             while (sbytes > 0) {
482                 tlen = (sbytes > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : sbytes);
483                 memset(tbuffer, 0, tlen);
484                 RX_AFS_GUNLOCK();
485                 bsent = rx_Write(((struct rxfs_storeVariables*)rock)->call,
486                                         tbuffer, tlen);
487                 RX_AFS_GLOCK();
488
489                 if (bsent != tlen) {
490                     code = -33; /* XXX */
491                     break;
492                 }
493                 sbytes -= tlen;
494             }
495             osi_FreeLargeSpace(tbuffer);
496         }
497         stored += tdc->f.chunkBytes;
498
499         /* ideally, I'd like to unlock the dcache and turn
500          * off the writing bit here, but that would
501          * require being able to retry StoreAllSegments in
502          * the event of a failure. It only really matters
503          * if user can't read from a 'locked' dcache or
504          * one which has the writing bit turned on. */
505     }
506     if (!code) {
507         code = (*ops->close)(rock, OutStatus, doProcessFS);
508         if (*doProcessFS) {
509             hadd32(*anewDV, 1);
510         }
511         XSTATS_END_TIME;
512     }
513     code = (*ops->destroy)(&rock, code);
514
515     *nomoreP = nomore;
516     return code;
517 }
518
519 /* rock and operations for RX_FILESERVER */
520
521 struct rxfs_fetchVariables {
522     struct rx_call *call;
523     char *tbuffer;
524     struct iovec *iov;
525     afs_uint32 nio;
526     afs_int32 hasNo64bit;
527     afs_int32 iovno;
528     afs_int32 iovmax;
529 };
530
531 afs_int32
532 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
533 {
534     afs_int32 code;
535     afs_uint32 tlen;
536     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
537
538     *bytesread = 0;
539     tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
540     RX_AFS_GUNLOCK();
541     code = rx_Read(v->call, v->tbuffer, tlen);
542     RX_AFS_GLOCK();
543     if (code <= 0)
544         return -34;
545     *bytesread = code;
546     return 0;
547 }
548
549 afs_int32
550 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
551 {
552     afs_int32 code;
553     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
554
555     *bytesread = 0;
556     RX_AFS_GUNLOCK();
557     code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
558     RX_AFS_GLOCK();
559     if (code <= 0)
560         return -34;
561     *bytesread = code;
562     return 0;
563 }
564
565
566 afs_int32
567 rxfs_fetchMemWrite(void *r, struct osi_file *fP,
568                         afs_uint32 offset, afs_uint32 tlen,
569                         afs_uint32 *byteswritten)
570 {
571     afs_int32 code;
572     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
573     struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
574
575     code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
576     if (code != tlen) {
577         return EIO;
578     }
579     *byteswritten = code;
580     return 0;
581 }
582
583 afs_int32
584 rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
585                         afs_uint32 offset, afs_uint32 tlen,
586                         afs_uint32 *byteswritten)
587 {
588     afs_int32 code;
589     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
590
591     code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
592     if (code != tlen) {
593         return EIO;
594     }
595     *byteswritten = code;
596     return 0;
597 }
598
599
600 afs_int32
601 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
602                                         struct afs_FetchOutput *tsmall)
603 {
604     afs_int32 code, code1 = 0;
605     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
606
607     if (!v->call)
608         return -1;
609
610     RX_AFS_GUNLOCK();
611     code = EndRXAFS_FetchData(v->call, &tsmall->OutStatus,
612                               &tsmall->CallBack,
613                               &tsmall->tsync);
614     RX_AFS_GLOCK();
615
616     RX_AFS_GUNLOCK();
617     if (v->call)
618         code1 = rx_EndCall(v->call, code);
619     RX_AFS_GLOCK();
620     if (!code && code1)
621         code = code1;
622
623     v->call = NULL;
624
625     return code;
626 }
627
628 afs_int32
629 rxfs_fetchDestroy(void **r, afs_int32 error)
630 {
631     afs_int32 code = error;
632     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
633
634     *r = NULL;
635     if (v->tbuffer)
636         osi_FreeLargeSpace(v->tbuffer);
637     if (v->iov)
638         osi_FreeSmallSpace(v->iov);
639     osi_FreeSmallSpace(v);
640     return code;
641 }
642
643 afs_int32
644 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
645 {
646     afs_int32 code;
647     register struct rxfs_fetchVariables *v
648             = (struct rxfs_fetchVariables *)r;
649
650     RX_AFS_GUNLOCK();
651     code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
652     *length = ntohl(*length);
653     RX_AFS_GLOCK();
654     if (code != sizeof(afs_int32)) {
655         code = rx_Error(v->call);
656         return (code ? code : -1);      /* try to return code, not -1 */
657     }
658     return 0;
659 }
660
661 static
662 struct fetchOps rxfs_fetchUfsOps = {
663     rxfs_fetchMore,
664     rxfs_fetchUfsRead,
665     rxfs_fetchUfsWrite,
666     rxfs_fetchClose,
667     rxfs_fetchDestroy
668 };
669
670 static
671 struct fetchOps rxfs_fetchMemOps = {
672     rxfs_fetchMore,
673     rxfs_fetchMemRead,
674     rxfs_fetchMemWrite,
675     rxfs_fetchClose,
676     rxfs_fetchDestroy
677 };
678
679 afs_int32
680 rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
681                 afs_uint32 size, afs_uint32 *out_length, struct dcache *adc,
682                 struct osi_file *fP, struct fetchOps **ops, void **rock)
683 {
684     struct rxfs_fetchVariables *v;
685     int code, code1;
686     afs_int32 length_hi, length, bytes;
687 #ifdef AFS_64BIT_CLIENT
688     afs_size_t tsize;
689     afs_size_t lengthFound;     /* as returned from server */
690 #endif /* AFS_64BIT_CLIENT */
691
692     v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
693     if (!v)
694         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
695     memset(v, 0, sizeof(struct rxfs_fetchVariables));
696
697     RX_AFS_GUNLOCK();
698     v->call = rx_NewCall(tc->id);
699     RX_AFS_GLOCK();
700
701 #ifdef AFS_64BIT_CLIENT
702     length_hi = code = 0;
703     if (!afs_serverHasNo64Bit(tc)) {
704         tsize = size;
705         RX_AFS_GUNLOCK();
706         code = StartRXAFS_FetchData64(v->call, (struct AFSFid *)&avc->f.fid.Fid,
707                                         base, tsize);
708         if (code != 0) {
709             RX_AFS_GLOCK();
710             afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
711                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
712         } else {
713             bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
714             RX_AFS_GLOCK();
715             if (bytes == sizeof(afs_int32)) {
716                 length_hi = ntohl(length_hi);
717             } else {
718                 length_hi = 0;
719                 code = rx_Error(v->call);
720                 RX_AFS_GUNLOCK();
721                 code1 = rx_EndCall(v->call, code);
722                 RX_AFS_GLOCK();
723                 v->call = NULL;
724             }
725         }
726     }
727     if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
728         if (base > 0x7FFFFFFF) {
729             code = EFBIG;
730         } else {
731             afs_int32 pos;
732             pos = base;
733             RX_AFS_GUNLOCK();
734             if (!v->call)
735                 v->call = rx_NewCall(tc->id);
736             code =
737                 StartRXAFS_FetchData(v->call, (struct AFSFid *)
738                                      &avc->f.fid.Fid, pos,
739                                      size);
740             RX_AFS_GLOCK();
741         }
742         afs_serverSetNo64Bit(tc);
743     }
744     if (!code) {
745         RX_AFS_GUNLOCK();
746         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
747         RX_AFS_GLOCK();
748         if (bytes == sizeof(afs_int32))
749             length = ntohl(length);
750         else {
751             code = rx_Error(v->call);
752         }
753     }
754     FillInt64(lengthFound, length_hi, length);
755     afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
756                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
757                ICL_TYPE_OFFSET,
758                ICL_HANDLE_OFFSET(lengthFound));
759 #else /* AFS_64BIT_CLIENT */
760     RX_AFS_GUNLOCK();
761     code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
762                                  base, size);
763     RX_AFS_GLOCK();
764     if (code == 0) {
765         RX_AFS_GUNLOCK();
766         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
767         RX_AFS_GLOCK();
768         if (bytes == sizeof(afs_int32))
769             length = ntohl(length);
770         else
771             code = rx_Error(v->call);
772     }
773 #endif /* AFS_64BIT_CLIENT */
774     if (code) {
775         osi_FreeSmallSpace(v);
776         return code;
777     }
778
779     if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
780         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
781         if (!v->tbuffer)
782             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
783         osi_Assert(WriteLocked(&adc->lock));
784         fP->offset = 0;
785         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
786     }
787     else {
788         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
789                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
790                    ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
791         /*
792          * We need to alloc the iovecs on the heap so that they are "pinned"
793          * rather than declare them on the stack - defect 11272
794          */
795         v->iov = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
796                                                 RX_MAXIOVECS);
797         if (!v->iov)
798             osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
799         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
800     }
801     *rock = (void *)v;
802     *out_length = length;
803     return 0;
804 }
805
806
807 /*!
808  * Routine called on fetch; also tells people waiting for data
809  *      that more has arrived.
810  *
811  * \param tc Ptr to the Rx connection structure.
812  * \param fP File descriptor for the cache file.
813  * \param abase Base offset to fetch.
814  * \param adc Ptr to the dcache entry for the file, write-locked.
815  * \param avc Ptr to the vcache entry for the file.
816  * \param size Amount of data that should be fetched.
817  * \param tsmall Ptr to the afs_FetchOutput structure.
818  *
819  * \note Environment: Nothing interesting.
820  */
821 int
822 afs_CacheFetchProc(register struct afs_conn *tc,
823                     register struct osi_file *fP, afs_size_t abase,
824                     struct dcache *adc, struct vcache *avc,
825                     afs_int32 size,
826                     struct afs_FetchOutput *tsmall)
827 {
828     register afs_int32 code;
829     afs_uint32 length;
830     afs_uint32 bytesread, byteswritten;
831     struct fetchOps *ops = NULL;
832     void *rock = NULL;
833     int moredata = 0;
834     register int offset = 0;
835
836     XSTATS_DECLS;
837 #ifndef AFS_NOSTATS
838     struct afs_stats_xferData *xferP;   /* Ptr to this op's xfer struct */
839     osi_timeval_t xferStartTime,        /*FS xfer start time */
840                     xferStopTime;       /*FS xfer stop time */
841     afs_size_t bytesToXfer = 0, bytesXferred = 0;
842 #endif
843
844     AFS_STATCNT(CacheFetchProc);
845
846     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
847
848     code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
849
850 #ifndef AFS_NOSTATS
851     xferP =
852         &(afs_stats_cmfullperf.rpc.fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
853     osi_GetuTime(&xferStartTime);
854 #endif /* AFS_NOSTATS */
855
856     adc->validPos = abase;
857
858     if ( !code ) do {
859         if (moredata) {
860             code = (*ops->more)(rock, &length, &moredata);
861             if ( code )
862                 break;
863         }
864         /*
865          * The fetch protocol is extended for the AFS/DFS translator
866          * to allow multiple blocks of data, each with its own length,
867          * to be returned. As long as the top bit is set, there are more
868          * blocks expected.
869          *
870          * We do not do this for AFS file servers because they sometimes
871          * return large negative numbers as the transfer size.
872          */
873         if (avc->f.states & CForeign) {
874             moredata = length & 0x80000000;
875             length &= ~0x80000000;
876         } else {
877             moredata = 0;
878         }
879 #ifndef AFS_NOSTATS
880         bytesToXfer += length;
881 #endif /* AFS_NOSTATS */
882         while (length > 0) {
883 #ifdef RX_KERNEL_TRACE
884             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
885                        "before rx_Read");
886 #endif
887             code = (*ops->read)(rock, length, &bytesread);
888 #ifdef RX_KERNEL_TRACE
889             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
890                        "after rx_Read");
891 #endif
892 #ifndef AFS_NOSTATS
893             bytesXferred += bytesread;
894 #endif /* AFS_NOSTATS */
895             if ( code ) {
896                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
897                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
898                            ICL_TYPE_INT32, length);
899                 code = -34;
900                 break;
901             }
902             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
903             if ( code )
904                 break;
905             offset += bytesread;
906             abase += bytesread;
907             length -= bytesread;
908             adc->validPos = abase;
909             if (afs_osi_Wakeup(&adc->validPos) == 0)
910                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
911                            __FILE__, ICL_TYPE_INT32, __LINE__,
912                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
913                            adc->dflags);
914         }
915         code = 0;
916     } while (moredata);
917     if (!code)
918         code = (*ops->close)(rock, avc, adc, tsmall);
919     (*ops->destroy)(&rock, code);
920
921 #ifndef AFS_NOSTATS
922     osi_GetuTime(&xferStopTime);
923     (xferP->numXfers)++;
924     if (!code) {
925         (xferP->numSuccesses)++;
926         afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] += bytesXferred;
927         (xferP->sumBytes) +=
928                 (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
929         afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
930         if (bytesXferred < xferP->minBytes)
931             xferP->minBytes = bytesXferred;
932         if (bytesXferred > xferP->maxBytes)
933             xferP->maxBytes = bytesXferred;
934
935         /*
936          * Tally the size of the object.  Note: we tally the actual size,
937          * NOT the number of bytes that made it out over the wire.
938          */
939         if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
940             (xferP->count[0])++;
941         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
942             (xferP->count[1])++;
943         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
944             (xferP->count[2])++;
945         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
946             (xferP->count[3])++;
947         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
948             (xferP->count[4])++;
949         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
950             (xferP->count[5])++;
951         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
952             (xferP->count[6])++;
953         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
954             (xferP->count[7])++;
955         else
956             (xferP->count[8])++;
957
958         afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
959         afs_stats_AddTo((xferP->sumTime), elapsedTime);
960         afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
961         if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
962             afs_stats_TimeAssign((xferP->minTime), elapsedTime);
963         }
964         if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
965             afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
966         }
967     }
968 #endif
969     XSTATS_END_TIME;
970     return code;
971 }
972