4e59a6d12bfe8befbdb5d7d51068f3f8c981dfaf
[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_storeDestroy(void **r, afs_int32 error)
156 {
157     afs_int32 code = error;
158     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
159
160     *r = NULL;
161     if (v->tbuffer)
162         osi_FreeLargeSpace(v->tbuffer);
163     if (v->tiov)
164         osi_FreeSmallSpace(v->tiov);
165     osi_FreeSmallSpace(v);
166     return code;
167 }
168
169 static
170 struct storeOps rxfs_storeUfsOps = {
171     rxfs_storeUfsPrepare,
172     rxfs_storeUfsRead,
173     rxfs_storeUfsWrite,
174     rxfs_storeStatus,
175     rxfs_storeDestroy
176 };
177
178 static
179 struct storeOps rxfs_storeMemOps = {
180     rxfs_storeMemPrepare,
181     rxfs_storeMemRead,
182     rxfs_storeMemWrite,
183     rxfs_storeStatus,
184     rxfs_storeDestroy
185 };
186
187 afs_int32
188 rxfs_storeInit(struct vcache *avc, struct storeOps **ops, void **rock)
189 {
190     struct rxfs_storeVariables *v;
191
192     v = (struct rxfs_storeVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
193     if (!v)
194         osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
195     memset(v, 0, sizeof(struct rxfs_storeVariables));
196
197     v->InStatus.ClientModTime = avc->f.m.Date;
198     v->InStatus.Mask = AFS_SETMODTIME;
199
200     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
201         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
202         if (!v->tbuffer)
203             osi_Panic
204               ("rxfs_storeInit: osi_AllocLargeSpace for iovecs returned NULL\n");
205         *ops = (struct storeOps *) &rxfs_storeUfsOps;
206     } else {
207         v->tiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
208         if (!v->tiov)
209             osi_Panic
210               ("rxfs_storeInit: osi_AllocSmallSpace for iovecs returned NULL\n");
211         *ops = (struct storeOps *) &rxfs_storeMemOps;
212 #ifdef notdef
213         /* do this at a higher level now -- it's a parameter */
214         /* for now, only do 'continue from close' code if file fits in one
215          * chunk.  Could clearly do better: if only one modified chunk
216          * then can still do this.  can do this on *last* modified chunk */
217         tlen = avc->f.m.Length - 1;     /* byte position of last byte we'll store */
218         if (shouldWake) {
219             if (AFS_CHUNK(tlen) != 0)
220                 *shouldWake = 0;
221             else
222                 *shouldWake = 1;
223         }
224 #endif /* notdef */
225     }
226     *rock = (void *)v;
227     return 0;
228 }
229
230
231 /*!
232  *      Called upon store.
233  *
234  * \param acall Ptr to the Rx call structure involved.
235  * \param fP Ptr to the related file descriptor.
236  * \param alen Size of the file in bytes.
237  * \param avc Ptr to the vcache entry.
238  * \param shouldWake is it "safe" to return early from close() ?
239  * \param abytesToXferP Set to the number of bytes to xfer.
240  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
241  * \param abytesXferredP Set to the number of bytes actually xferred.
242  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
243  *
244  * \note Environment: Nothing interesting.
245  */
246 int
247 afs_CacheStoreProc(register struct rx_call *acall,
248                       register struct osi_file *fP,
249                       register afs_int32 alen, struct vcache *avc,
250                       int *shouldWake, afs_size_t * abytesToXferP,
251                       afs_size_t * abytesXferredP)
252 {
253     afs_int32 code;
254     afs_uint32 tlen;
255     int offset = 0;
256     struct storeOps *ops;
257     void * rock = NULL;
258
259     afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
260                ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
261                ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
262     code =  rxfs_storeInit(avc, &ops, &rock);
263     if ( code ) {
264         osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed");
265     }
266     ((struct rxfs_storeVariables *)rock)->call = acall;
267
268     AFS_STATCNT(CacheStoreProc);
269 #ifndef AFS_NOSTATS
270     /*
271      * In this case, alen is *always* the amount of data we'll be trying
272      * to ship here.
273      */
274     *(abytesToXferP) = alen;
275     *(abytesXferredP) = 0;
276 #endif /* AFS_NOSTATS */
277
278     while ( alen > 0 ) {
279         afs_int32 bytesread, byteswritten;
280         code = (*ops->prepare)(rock, alen, &tlen);
281         if ( code )
282             break;
283
284         code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
285         if (code)
286             break;
287
288         tlen = bytesread;
289         code = (*ops->write)(rock, tlen, &byteswritten);
290         if (code)
291             break;
292 #ifndef AFS_NOSTATS
293         (*abytesXferredP) += byteswritten;
294 #endif /* AFS_NOSTATS */
295
296         offset += tlen;
297         alen -= tlen;
298         /*
299          * if file has been locked on server, can allow
300          * store to continue
301          */
302         if (shouldWake && *shouldWake && ((*ops->status)(rock) == 0)) {
303             *shouldWake = 0;    /* only do this once */
304             afs_wakeup(avc);
305         }
306     }
307     afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
308                ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
309                ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
310     code = (*ops->destroy)(&rock, code);
311     return code;
312 }
313
314 /* rock and operations for RX_FILESERVER */
315
316 struct rxfs_fetchVariables {
317     struct rx_call *call;
318     char *tbuffer;
319     struct iovec *iov;
320     afs_uint32 nio;
321     afs_int32 hasNo64bit;
322     afs_int32 iovno;
323     afs_int32 iovmax;
324 };
325
326 afs_int32
327 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
328 {
329     afs_int32 code;
330     afs_uint32 tlen;
331     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
332
333     *bytesread = 0;
334     tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
335     RX_AFS_GUNLOCK();
336     code = rx_Read(v->call, v->tbuffer, tlen);
337     RX_AFS_GLOCK();
338     if (code <= 0)
339         return -34;
340     *bytesread = code;
341     return 0;
342 }
343
344 afs_int32
345 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
346 {
347     afs_int32 code;
348     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
349
350     *bytesread = 0;
351     RX_AFS_GUNLOCK();
352     code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
353     RX_AFS_GLOCK();
354     if (code <= 0)
355         return -34;
356     *bytesread = code;
357     return 0;
358 }
359
360
361 afs_int32
362 rxfs_fetchMemWrite(void *r, struct osi_file *fP,
363                         afs_uint32 offset, afs_uint32 tlen,
364                         afs_uint32 *byteswritten)
365 {
366     afs_int32 code;
367     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
368     struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
369
370     code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
371     if (code != tlen) {
372         return EIO;
373     }
374     *byteswritten = code;
375     return 0;
376 }
377
378 afs_int32
379 rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
380                         afs_uint32 offset, afs_uint32 tlen,
381                         afs_uint32 *byteswritten)
382 {
383     afs_int32 code;
384     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
385
386     code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
387     if (code != tlen) {
388         return EIO;
389     }
390     *byteswritten = code;
391     return 0;
392 }
393
394
395 afs_int32
396 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
397                                         struct afs_FetchOutput *tsmall)
398 {
399     afs_int32 code, code1 = 0;
400     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
401
402     if (!v->call)
403         return -1;
404
405     RX_AFS_GUNLOCK();
406     code = EndRXAFS_FetchData(v->call, &tsmall->OutStatus,
407                               &tsmall->CallBack,
408                               &tsmall->tsync);
409     RX_AFS_GLOCK();
410
411     RX_AFS_GUNLOCK();
412     if (v->call)
413         code1 = rx_EndCall(v->call, code);
414     RX_AFS_GLOCK();
415     if (!code && code1)
416         code = code1;
417
418     v->call = NULL;
419
420     return code;
421 }
422
423 afs_int32
424 rxfs_fetchDestroy(void **r, afs_int32 error)
425 {
426     afs_int32 code = error;
427     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
428
429     *r = NULL;
430     if (v->tbuffer)
431         osi_FreeLargeSpace(v->tbuffer);
432     if (v->iov)
433         osi_FreeSmallSpace(v->iov);
434     osi_FreeSmallSpace(v);
435     return code;
436 }
437
438 afs_int32
439 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
440 {
441     afs_int32 code;
442     register struct rxfs_fetchVariables *v
443             = (struct rxfs_fetchVariables *)r;
444
445     RX_AFS_GUNLOCK();
446     code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
447     *length = ntohl(*length);
448     RX_AFS_GLOCK();
449     if (code != sizeof(afs_int32)) {
450         code = rx_Error(v->call);
451         return (code ? code : -1);      /* try to return code, not -1 */
452     }
453     return 0;
454 }
455
456 static
457 struct fetchOps rxfs_fetchUfsOps = {
458     rxfs_fetchMore,
459     rxfs_fetchUfsRead,
460     rxfs_fetchUfsWrite,
461     rxfs_fetchClose,
462     rxfs_fetchDestroy
463 };
464
465 static
466 struct fetchOps rxfs_fetchMemOps = {
467     rxfs_fetchMore,
468     rxfs_fetchMemRead,
469     rxfs_fetchMemWrite,
470     rxfs_fetchClose,
471     rxfs_fetchDestroy
472 };
473
474 afs_int32
475 rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
476                 afs_uint32 size, afs_uint32 *out_length, struct dcache *adc,
477                 struct osi_file *fP, struct fetchOps **ops, void **rock)
478 {
479     struct rxfs_fetchVariables *v;
480     int code, code1;
481     afs_int32 length_hi, length, bytes;
482 #ifdef AFS_64BIT_CLIENT
483     afs_size_t tsize;
484     afs_size_t lengthFound;     /* as returned from server */
485 #endif /* AFS_64BIT_CLIENT */
486
487     v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
488     if (!v)
489         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
490     memset(v, 0, sizeof(struct rxfs_fetchVariables));
491
492     RX_AFS_GUNLOCK();
493     v->call = rx_NewCall(tc->id);
494     RX_AFS_GLOCK();
495
496 #ifdef AFS_64BIT_CLIENT
497     length_hi = code = 0;
498     if (!afs_serverHasNo64Bit(tc)) {
499         tsize = size;
500         RX_AFS_GUNLOCK();
501         code = StartRXAFS_FetchData64(v->call, (struct AFSFid *)&avc->f.fid.Fid,
502                                         base, tsize);
503         if (code != 0) {
504             RX_AFS_GLOCK();
505             afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
506                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
507         } else {
508             bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
509             RX_AFS_GLOCK();
510             if (bytes == sizeof(afs_int32)) {
511                 length_hi = ntohl(length_hi);
512             } else {
513                 length_hi = 0;
514                 code = rx_Error(v->call);
515                 RX_AFS_GUNLOCK();
516                 code1 = rx_EndCall(v->call, code);
517                 RX_AFS_GLOCK();
518                 v->call = NULL;
519             }
520         }
521     }
522     if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
523         if (base > 0x7FFFFFFF) {
524             code = EFBIG;
525         } else {
526             afs_int32 pos;
527             pos = base;
528             RX_AFS_GUNLOCK();
529             if (!v->call)
530                 v->call = rx_NewCall(tc->id);
531             code =
532                 StartRXAFS_FetchData(v->call, (struct AFSFid *)
533                                      &avc->f.fid.Fid, pos,
534                                      size);
535             RX_AFS_GLOCK();
536         }
537         afs_serverSetNo64Bit(tc);
538     }
539     if (!code) {
540         RX_AFS_GUNLOCK();
541         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
542         RX_AFS_GLOCK();
543         if (bytes == sizeof(afs_int32))
544             length = ntohl(length);
545         else {
546             code = rx_Error(v->call);
547         }
548     }
549     FillInt64(lengthFound, length_hi, length);
550     afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
551                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
552                ICL_TYPE_OFFSET,
553                ICL_HANDLE_OFFSET(lengthFound));
554 #else /* AFS_64BIT_CLIENT */
555     RX_AFS_GUNLOCK();
556     code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
557                                  base, size);
558     RX_AFS_GLOCK();
559     if (code == 0) {
560         RX_AFS_GUNLOCK();
561         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
562         RX_AFS_GLOCK();
563         if (bytes == sizeof(afs_int32))
564             length = ntohl(length);
565         else
566             code = rx_Error(v->call);
567     }
568 #endif /* AFS_64BIT_CLIENT */
569     if (code) {
570         osi_FreeSmallSpace(v);
571         return code;
572     }
573
574     if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
575         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
576         if (!v->tbuffer)
577             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
578         osi_Assert(WriteLocked(&adc->lock));
579         fP->offset = 0;
580         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
581     }
582     else {
583         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
584                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
585                    ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
586         /*
587          * We need to alloc the iovecs on the heap so that they are "pinned"
588          * rather than declare them on the stack - defect 11272
589          */
590         v->iov = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
591                                                 RX_MAXIOVECS);
592         if (!v->iov)
593             osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
594         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
595     }
596     *rock = (void *)v;
597     *out_length = length;
598     return 0;
599 }
600
601
602 /*!
603  * Routine called on fetch; also tells people waiting for data
604  *      that more has arrived.
605  *
606  * \param tc Ptr to the Rx connection structure.
607  * \param fP File descriptor for the cache file.
608  * \param abase Base offset to fetch.
609  * \param adc Ptr to the dcache entry for the file, write-locked.
610  * \param avc Ptr to the vcache entry for the file.
611  * \param size Amount of data that should be fetched.
612  * \param tsmall Ptr to the afs_FetchOutput structure.
613  *
614  * \note Environment: Nothing interesting.
615  */
616 int
617 afs_CacheFetchProc(register struct afs_conn *tc,
618                     register struct osi_file *fP, afs_size_t abase,
619                     struct dcache *adc, struct vcache *avc,
620                     afs_int32 size,
621                     struct afs_FetchOutput *tsmall)
622 {
623     register afs_int32 code;
624     afs_uint32 length;
625     afs_uint32 bytesread, byteswritten;
626     struct fetchOps *ops = NULL;
627     void *rock = NULL;
628     int moredata = 0;
629     register int offset = 0;
630
631     XSTATS_DECLS;
632 #ifndef AFS_NOSTATS
633     struct afs_stats_xferData *xferP;   /* Ptr to this op's xfer struct */
634     osi_timeval_t xferStartTime,        /*FS xfer start time */
635                     xferStopTime;       /*FS xfer stop time */
636     afs_size_t bytesToXfer = 0, bytesXferred = 0;
637 #endif
638
639     AFS_STATCNT(CacheFetchProc);
640
641     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
642
643     code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
644
645 #ifndef AFS_NOSTATS
646     xferP =
647         &(afs_stats_cmfullperf.rpc.fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
648     osi_GetuTime(&xferStartTime);
649 #endif /* AFS_NOSTATS */
650
651     adc->validPos = abase;
652
653     if ( !code ) do {
654         if (moredata) {
655             code = (*ops->more)(rock, &length, &moredata);
656             if ( code )
657                 break;
658         }
659         /*
660          * The fetch protocol is extended for the AFS/DFS translator
661          * to allow multiple blocks of data, each with its own length,
662          * to be returned. As long as the top bit is set, there are more
663          * blocks expected.
664          *
665          * We do not do this for AFS file servers because they sometimes
666          * return large negative numbers as the transfer size.
667          */
668         if (avc->f.states & CForeign) {
669             moredata = length & 0x80000000;
670             length &= ~0x80000000;
671         } else {
672             moredata = 0;
673         }
674 #ifndef AFS_NOSTATS
675         bytesToXfer += length;
676 #endif /* AFS_NOSTATS */
677         while (length > 0) {
678 #ifdef RX_KERNEL_TRACE
679             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
680                        "before rx_Read");
681 #endif
682             code = (*ops->read)(rock, length, &bytesread);
683 #ifdef RX_KERNEL_TRACE
684             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
685                        "after rx_Read");
686 #endif
687 #ifndef AFS_NOSTATS
688             bytesXferred += bytesread;
689 #endif /* AFS_NOSTATS */
690             if ( code ) {
691                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
692                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
693                            ICL_TYPE_INT32, length);
694                 code = -34;
695                 break;
696             }
697             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
698             if ( code )
699                 break;
700             offset += bytesread;
701             abase += bytesread;
702             length -= bytesread;
703             adc->validPos = abase;
704             if (afs_osi_Wakeup(&adc->validPos) == 0)
705                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
706                            __FILE__, ICL_TYPE_INT32, __LINE__,
707                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
708                            adc->dflags);
709         }
710         code = 0;
711     } while (moredata);
712     if (!code)
713         code = (*ops->close)(rock, avc, adc, tsmall);
714     (*ops->destroy)(&rock, code);
715
716 #ifndef AFS_NOSTATS
717     osi_GetuTime(&xferStopTime);
718     (xferP->numXfers)++;
719     if (!code) {
720         (xferP->numSuccesses)++;
721         afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] += bytesXferred;
722         (xferP->sumBytes) +=
723                 (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
724         afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
725         if (bytesXferred < xferP->minBytes)
726             xferP->minBytes = bytesXferred;
727         if (bytesXferred > xferP->maxBytes)
728             xferP->maxBytes = bytesXferred;
729
730         /*
731          * Tally the size of the object.  Note: we tally the actual size,
732          * NOT the number of bytes that made it out over the wire.
733          */
734         if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
735             (xferP->count[0])++;
736         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
737             (xferP->count[1])++;
738         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
739             (xferP->count[2])++;
740         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
741             (xferP->count[3])++;
742         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
743             (xferP->count[4])++;
744         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
745             (xferP->count[5])++;
746         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
747             (xferP->count[6])++;
748         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
749             (xferP->count[7])++;
750         else
751             (xferP->count[8])++;
752
753         afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
754         afs_stats_AddTo((xferP->sumTime), elapsedTime);
755         afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
756         if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
757             afs_stats_TimeAssign((xferP->minTime), elapsedTime);
758         }
759         if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
760             afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
761         }
762     }
763 #endif
764     XSTATS_END_TIME;
765     return code;
766 }
767