Move context of CacheFetchProc from afs_dcache.c to afs_fetchstore.c
[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 Position,
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 =
502             StartRXAFS_FetchData64(v->call, (struct AFSFid *)&avc->f.fid.Fid,
503                                         Position, tsize);
504         if (code != 0) {
505             RX_AFS_GLOCK();
506             afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
507                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
508         } else {
509             bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
510             RX_AFS_GLOCK();
511             if (bytes == sizeof(afs_int32)) {
512                 length_hi = ntohl(length_hi);
513             } else {
514                 length_hi = 0;
515                 code = rx_Error(v->call);
516                 RX_AFS_GUNLOCK();
517                 code1 = rx_EndCall(v->call, code);
518                 RX_AFS_GLOCK();
519                 v->call = NULL;
520             }
521         }
522     }
523     if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
524         if (Position > 0x7FFFFFFF) {
525             code = EFBIG;
526         } else {
527             afs_int32 pos;
528             pos = Position;
529             RX_AFS_GUNLOCK();
530             if (!v->call)
531                 v->call = rx_NewCall(tc->id);
532             code =
533                 StartRXAFS_FetchData(v->call, (struct AFSFid *)
534                                      &avc->f.fid.Fid, pos,
535                                      size);
536             RX_AFS_GLOCK();
537         }
538         afs_serverSetNo64Bit(tc);
539     }
540     if (code == 0) {
541         RX_AFS_GUNLOCK();
542         bytes =
543             rx_Read(v->call, (char *)&length,
544                     sizeof(afs_int32));
545         RX_AFS_GLOCK();
546         if (bytes == sizeof(afs_int32)) {
547             length = ntohl(length);
548         } else {
549             code = rx_Error(v->call);
550         }
551     }
552     FillInt64(lengthFound, length_hi, length);
553     afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
554                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
555                ICL_TYPE_OFFSET,
556                ICL_HANDLE_OFFSET(lengthFound));
557 #else /* AFS_64BIT_CLIENT */
558     RX_AFS_GUNLOCK();
559     code =
560         StartRXAFS_FetchData(v->call,
561                              (struct AFSFid *)&avc->f.fid.Fid,
562                              Position, size);
563     RX_AFS_GLOCK();
564     if (code == 0) {
565         RX_AFS_GUNLOCK();
566         bytes =
567             rx_Read(v->call, (char *)&length,
568                     sizeof(afs_int32));
569         RX_AFS_GLOCK();
570         if (bytes == sizeof(afs_int32)) {
571             length = ntohl(length);
572         } else {
573             code = rx_Error(v->call);
574         }
575     }
576 #endif /* AFS_64BIT_CLIENT */
577     if (code) {
578         osi_FreeSmallSpace(v);
579         return code;
580     }
581
582     if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
583         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
584         if (!v->tbuffer)
585             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
586         osi_Assert(WriteLocked(&adc->lock));
587         fP->offset = 0;
588         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
589     }
590     else {
591         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
592                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
593                    ICL_HANDLE_OFFSET(Position), ICL_TYPE_INT32, length);
594         /*
595          * We need to alloc the iovecs on the heap so that they are "pinned"
596          * rather than declare them on the stack - defect 11272
597          */
598         v->iov =
599             (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
600                                                 RX_MAXIOVECS);
601         if (!v->iov)
602             osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
603         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
604     }
605     *rock = (void *)v;
606     *out_length = length;
607     return 0;
608 }
609
610
611 /*!
612  * Routine called on fetch; also tells people waiting for data
613  *      that more has arrived.
614  *
615  * \param tc Ptr to the Rx connection structure.
616  * \param fP File descriptor for the cache file.
617  * \param abase Base offset to fetch.
618  * \param adc Ptr to the dcache entry for the file, write-locked.
619  * \param avc Ptr to the vcache entry for the file.
620  * \param abytesToXferP Set to the number of bytes to xfer.
621  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
622  * \param abytesXferredP Set to the number of bytes actually xferred.
623  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
624  * \param size Amount of data that should be fetched.
625  * \param tsmall Ptr to the afs_FetchOutput structure.
626  *
627  * \note Environment: Nothing interesting.
628  */
629 int
630 afs_CacheFetchProc(register struct afs_conn *tc,
631                     register struct osi_file *fP, afs_size_t abase,
632                     struct dcache *adc, struct vcache *avc,
633                     afs_size_t * abytesToXferP, afs_size_t * abytesXferredP,
634                     afs_int32 size,
635                     struct afs_FetchOutput *tsmall)
636 {
637     register afs_int32 code;
638     afs_uint32 length;
639     afs_uint32 bytesread, byteswritten;
640     struct fetchOps *ops = NULL;
641     void *rock = NULL;
642     int moredata = 0;
643     register int offset = 0;
644
645     AFS_STATCNT(CacheFetchProc);
646
647 #ifndef AFS_NOSTATS
648     (*abytesToXferP) = 0;
649     (*abytesXferredP) = 0;
650 #endif /* AFS_NOSTATS */
651
652     adc->validPos = abase;
653
654     code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
655     if ( !code ) do {
656         if (moredata) {
657             code = (*ops->more)(rock, &length, &moredata);
658             if ( code )
659                 break;
660         }
661         /*
662          * The fetch protocol is extended for the AFS/DFS translator
663          * to allow multiple blocks of data, each with its own length,
664          * to be returned. As long as the top bit is set, there are more
665          * blocks expected.
666          *
667          * We do not do this for AFS file servers because they sometimes
668          * return large negative numbers as the transfer size.
669          */
670         if (avc->f.states & CForeign) {
671             moredata = length & 0x80000000;
672             length &= ~0x80000000;
673         } else {
674             moredata = 0;
675         }
676 #ifndef AFS_NOSTATS
677         (*abytesToXferP) += length;
678 #endif /* AFS_NOSTATS */
679         while (length > 0) {
680 #ifdef RX_KERNEL_TRACE
681             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
682                        "before rx_Read");
683 #endif
684             code = (*ops->read)(rock, length, &bytesread);
685 #ifdef RX_KERNEL_TRACE
686             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
687                        "after rx_Read");
688 #endif
689 #ifndef AFS_NOSTATS
690             (*abytesXferredP) += bytesread;
691 #endif /* AFS_NOSTATS */
692             if ( code ) {
693                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
694                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
695                            ICL_TYPE_INT32, length);
696                 code = -34;
697                 break;
698             }
699             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
700             if ( code )
701                 break;
702             offset += bytesread;
703             abase += bytesread;
704             length -= bytesread;
705             adc->validPos = abase;
706             if (afs_osi_Wakeup(&adc->validPos) == 0)
707                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
708                            __FILE__, ICL_TYPE_INT32, __LINE__,
709                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
710                            adc->dflags);
711         }
712         code = 0;
713     } while (moredata);
714     if (!code)
715         code = (*ops->close)(rock, avc, adc, tsmall);
716     (*ops->destroy)(&rock, code);
717     return code;
718 }
719