Move context of afs_CacheStoreProc() call from afs_segments to afs_fetchstore
[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 extern unsigned int storeallmissing;
231 /*!
232  *      Called upon store.
233  *
234  * \param acall Ptr to the Rx call structure involved.
235  * \param dclist pointer to the list of dcaches
236  * \param avc Ptr to the vcache entry.
237  * \param bytes per chunk
238  * \param nchunks number of chunks to store
239  * \param nomoreP pointer to the "nomore" flag
240  * \param abytesToXferP Set to the number of bytes to xfer.
241  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
242  * \param abytesXferredP Set to the number of bytes actually xferred.
243  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
244  *
245  * \note Environment: Nothing interesting.
246  */
247 int
248 afs_CacheStoreProc(register struct rx_call *acall,
249                         struct dcache **dclist,
250                         struct vcache *avc,
251                         afs_size_t bytes,
252                         afs_uint32 nchunks,
253                         int *nomoreP,
254                         afs_size_t * abytesToXferP,
255                         afs_size_t * abytesXferredP)
256 {
257     afs_int32 code = 0;
258     afs_uint32 tlen;
259     struct storeOps *ops;
260     void * rock = NULL;
261     struct osi_file *fP;
262     int *shouldwake;
263     int nomore = *nomoreP;
264     struct dcache *tdc;
265     int stored = 0;
266     unsigned int i;
267     afs_int32 alen;
268
269     code =  rxfs_storeInit(avc, &ops, &rock);
270     if ( code ) {
271         osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed");
272     }
273     ((struct rxfs_storeVariables *)rock)->call = acall;
274
275     for (i = 0; i < nchunks && !code; i++) {
276         int offset = 0;
277         tdc = dclist[i];
278         alen = tdc->f.chunkBytes;
279         if (!tdc) {
280             afs_warn("afs: missing dcache!\n");
281             storeallmissing++;
282             continue;   /* panic? */
283         }
284         afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2,
285                    ICL_TYPE_POINTER, avc, ICL_TYPE_INT32,
286                    tdc->f.chunk, ICL_TYPE_INT32,
287                    tdc->index, ICL_TYPE_INT32,
288                    afs_inode2trace(&tdc->f.inode));
289         shouldwake = 0;
290         if (nomore) {
291             if (avc->asynchrony == -1) {
292                 if (afs_defaultAsynchrony >
293                     (bytes - stored)) {
294                     shouldwake = &nomore;
295                 }
296             } else if ((afs_uint32) avc->asynchrony >=
297                        (bytes - stored)) {
298                 shouldwake = &nomore;
299             }
300         }
301         fP = afs_CFileOpen(&tdc->f.inode);
302
303         afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
304                   ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
305                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
306
307         AFS_STATCNT(CacheStoreProc);
308 #ifndef AFS_NOSTATS
309         /*
310          * In this case, alen is *always* the amount of data we'll be trying
311          * to ship here.
312          */
313         *(abytesToXferP) = alen;
314         *(abytesXferredP) = 0;
315 #endif /* AFS_NOSTATS */
316
317         while ( alen > 0 ) {
318             afs_int32 bytesread, byteswritten;
319             code = (*ops->prepare)(rock, alen, &tlen);
320             if ( code )
321                 break;
322
323             code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
324             if (code)
325                 break;
326
327             tlen = bytesread;
328             code = (*ops->write)(rock, tlen, &byteswritten);
329             if (code)
330                 break;
331 #ifndef AFS_NOSTATS
332             (*abytesXferredP) += byteswritten;
333 #endif /* AFS_NOSTATS */
334
335             offset += tlen;
336             alen -= tlen;
337             /*
338              * if file has been locked on server, can allow
339              * store to continue
340              */
341             if (shouldwake && *shouldwake && ((*ops->status)(rock) == 0)) {
342                 *shouldwake = 0;        /* only do this once */
343                 afs_wakeup(avc);
344             }
345         }
346         afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
347                   ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
348                   ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
349
350         afs_CFileClose(fP);
351         if ((tdc->f.chunkBytes < afs_OtherCSize)
352             && (i < (nchunks - 1)) && code == 0) {
353             int bsent, tlen, sbytes =
354                 afs_OtherCSize - tdc->f.chunkBytes;
355             char *tbuffer =
356                 osi_AllocLargeSpace(AFS_LRALLOCSIZ);
357
358             while (sbytes > 0) {
359                 tlen =
360                     (sbytes >
361                      AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ :
362                      sbytes);
363                 memset(tbuffer, 0, tlen);
364                 RX_AFS_GUNLOCK();
365                 bsent = rx_Write(acall, tbuffer, tlen);
366                 RX_AFS_GLOCK();
367
368                 if (bsent != tlen) {
369                     code = -33; /* XXX */
370                     break;
371                 }
372                 sbytes -= tlen;
373             }
374             osi_FreeLargeSpace(tbuffer);
375         }
376         stored += tdc->f.chunkBytes;
377
378         /* ideally, I'd like to unlock the dcache and turn
379          * off the writing bit here, but that would
380          * require being able to retry StoreAllSegments in
381          * the event of a failure. It only really matters
382          * if user can't read from a 'locked' dcache or
383          * one which has the writing bit turned on. */
384     }
385     code = (*ops->destroy)(&rock, code);
386
387     *nomoreP = nomore;
388     return code;
389 }
390
391 /* rock and operations for RX_FILESERVER */
392
393 struct rxfs_fetchVariables {
394     struct rx_call *call;
395     char *tbuffer;
396     struct iovec *iov;
397     afs_uint32 nio;
398     afs_int32 hasNo64bit;
399     afs_int32 iovno;
400     afs_int32 iovmax;
401 };
402
403 afs_int32
404 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
405 {
406     afs_int32 code;
407     afs_uint32 tlen;
408     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
409
410     *bytesread = 0;
411     tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
412     RX_AFS_GUNLOCK();
413     code = rx_Read(v->call, v->tbuffer, tlen);
414     RX_AFS_GLOCK();
415     if (code <= 0)
416         return -34;
417     *bytesread = code;
418     return 0;
419 }
420
421 afs_int32
422 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
423 {
424     afs_int32 code;
425     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
426
427     *bytesread = 0;
428     RX_AFS_GUNLOCK();
429     code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
430     RX_AFS_GLOCK();
431     if (code <= 0)
432         return -34;
433     *bytesread = code;
434     return 0;
435 }
436
437
438 afs_int32
439 rxfs_fetchMemWrite(void *r, struct osi_file *fP,
440                         afs_uint32 offset, afs_uint32 tlen,
441                         afs_uint32 *byteswritten)
442 {
443     afs_int32 code;
444     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
445     struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
446
447     code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
448     if (code != tlen) {
449         return EIO;
450     }
451     *byteswritten = code;
452     return 0;
453 }
454
455 afs_int32
456 rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
457                         afs_uint32 offset, afs_uint32 tlen,
458                         afs_uint32 *byteswritten)
459 {
460     afs_int32 code;
461     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
462
463     code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
464     if (code != tlen) {
465         return EIO;
466     }
467     *byteswritten = code;
468     return 0;
469 }
470
471
472 afs_int32
473 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
474                                         struct afs_FetchOutput *tsmall)
475 {
476     afs_int32 code, code1 = 0;
477     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
478
479     if (!v->call)
480         return -1;
481
482     RX_AFS_GUNLOCK();
483     code = EndRXAFS_FetchData(v->call, &tsmall->OutStatus,
484                               &tsmall->CallBack,
485                               &tsmall->tsync);
486     RX_AFS_GLOCK();
487
488     RX_AFS_GUNLOCK();
489     if (v->call)
490         code1 = rx_EndCall(v->call, code);
491     RX_AFS_GLOCK();
492     if (!code && code1)
493         code = code1;
494
495     v->call = NULL;
496
497     return code;
498 }
499
500 afs_int32
501 rxfs_fetchDestroy(void **r, afs_int32 error)
502 {
503     afs_int32 code = error;
504     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
505
506     *r = NULL;
507     if (v->tbuffer)
508         osi_FreeLargeSpace(v->tbuffer);
509     if (v->iov)
510         osi_FreeSmallSpace(v->iov);
511     osi_FreeSmallSpace(v);
512     return code;
513 }
514
515 afs_int32
516 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
517 {
518     afs_int32 code;
519     register struct rxfs_fetchVariables *v
520             = (struct rxfs_fetchVariables *)r;
521
522     RX_AFS_GUNLOCK();
523     code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
524     *length = ntohl(*length);
525     RX_AFS_GLOCK();
526     if (code != sizeof(afs_int32)) {
527         code = rx_Error(v->call);
528         return (code ? code : -1);      /* try to return code, not -1 */
529     }
530     return 0;
531 }
532
533 static
534 struct fetchOps rxfs_fetchUfsOps = {
535     rxfs_fetchMore,
536     rxfs_fetchUfsRead,
537     rxfs_fetchUfsWrite,
538     rxfs_fetchClose,
539     rxfs_fetchDestroy
540 };
541
542 static
543 struct fetchOps rxfs_fetchMemOps = {
544     rxfs_fetchMore,
545     rxfs_fetchMemRead,
546     rxfs_fetchMemWrite,
547     rxfs_fetchClose,
548     rxfs_fetchDestroy
549 };
550
551 afs_int32
552 rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
553                 afs_uint32 size, afs_uint32 *out_length, struct dcache *adc,
554                 struct osi_file *fP, struct fetchOps **ops, void **rock)
555 {
556     struct rxfs_fetchVariables *v;
557     int code, code1;
558     afs_int32 length_hi, length, bytes;
559 #ifdef AFS_64BIT_CLIENT
560     afs_size_t tsize;
561     afs_size_t lengthFound;     /* as returned from server */
562 #endif /* AFS_64BIT_CLIENT */
563
564     v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
565     if (!v)
566         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
567     memset(v, 0, sizeof(struct rxfs_fetchVariables));
568
569     RX_AFS_GUNLOCK();
570     v->call = rx_NewCall(tc->id);
571     RX_AFS_GLOCK();
572
573 #ifdef AFS_64BIT_CLIENT
574     length_hi = code = 0;
575     if (!afs_serverHasNo64Bit(tc)) {
576         tsize = size;
577         RX_AFS_GUNLOCK();
578         code = StartRXAFS_FetchData64(v->call, (struct AFSFid *)&avc->f.fid.Fid,
579                                         base, tsize);
580         if (code != 0) {
581             RX_AFS_GLOCK();
582             afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
583                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
584         } else {
585             bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
586             RX_AFS_GLOCK();
587             if (bytes == sizeof(afs_int32)) {
588                 length_hi = ntohl(length_hi);
589             } else {
590                 length_hi = 0;
591                 code = rx_Error(v->call);
592                 RX_AFS_GUNLOCK();
593                 code1 = rx_EndCall(v->call, code);
594                 RX_AFS_GLOCK();
595                 v->call = NULL;
596             }
597         }
598     }
599     if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
600         if (base > 0x7FFFFFFF) {
601             code = EFBIG;
602         } else {
603             afs_int32 pos;
604             pos = base;
605             RX_AFS_GUNLOCK();
606             if (!v->call)
607                 v->call = rx_NewCall(tc->id);
608             code =
609                 StartRXAFS_FetchData(v->call, (struct AFSFid *)
610                                      &avc->f.fid.Fid, pos,
611                                      size);
612             RX_AFS_GLOCK();
613         }
614         afs_serverSetNo64Bit(tc);
615     }
616     if (!code) {
617         RX_AFS_GUNLOCK();
618         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
619         RX_AFS_GLOCK();
620         if (bytes == sizeof(afs_int32))
621             length = ntohl(length);
622         else {
623             code = rx_Error(v->call);
624         }
625     }
626     FillInt64(lengthFound, length_hi, length);
627     afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
628                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
629                ICL_TYPE_OFFSET,
630                ICL_HANDLE_OFFSET(lengthFound));
631 #else /* AFS_64BIT_CLIENT */
632     RX_AFS_GUNLOCK();
633     code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
634                                  base, size);
635     RX_AFS_GLOCK();
636     if (code == 0) {
637         RX_AFS_GUNLOCK();
638         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
639         RX_AFS_GLOCK();
640         if (bytes == sizeof(afs_int32))
641             length = ntohl(length);
642         else
643             code = rx_Error(v->call);
644     }
645 #endif /* AFS_64BIT_CLIENT */
646     if (code) {
647         osi_FreeSmallSpace(v);
648         return code;
649     }
650
651     if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
652         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
653         if (!v->tbuffer)
654             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
655         osi_Assert(WriteLocked(&adc->lock));
656         fP->offset = 0;
657         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
658     }
659     else {
660         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
661                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
662                    ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
663         /*
664          * We need to alloc the iovecs on the heap so that they are "pinned"
665          * rather than declare them on the stack - defect 11272
666          */
667         v->iov = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
668                                                 RX_MAXIOVECS);
669         if (!v->iov)
670             osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
671         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
672     }
673     *rock = (void *)v;
674     *out_length = length;
675     return 0;
676 }
677
678
679 /*!
680  * Routine called on fetch; also tells people waiting for data
681  *      that more has arrived.
682  *
683  * \param tc Ptr to the Rx connection structure.
684  * \param fP File descriptor for the cache file.
685  * \param abase Base offset to fetch.
686  * \param adc Ptr to the dcache entry for the file, write-locked.
687  * \param avc Ptr to the vcache entry for the file.
688  * \param size Amount of data that should be fetched.
689  * \param tsmall Ptr to the afs_FetchOutput structure.
690  *
691  * \note Environment: Nothing interesting.
692  */
693 int
694 afs_CacheFetchProc(register struct afs_conn *tc,
695                     register struct osi_file *fP, afs_size_t abase,
696                     struct dcache *adc, struct vcache *avc,
697                     afs_int32 size,
698                     struct afs_FetchOutput *tsmall)
699 {
700     register afs_int32 code;
701     afs_uint32 length;
702     afs_uint32 bytesread, byteswritten;
703     struct fetchOps *ops = NULL;
704     void *rock = NULL;
705     int moredata = 0;
706     register int offset = 0;
707
708     XSTATS_DECLS;
709 #ifndef AFS_NOSTATS
710     struct afs_stats_xferData *xferP;   /* Ptr to this op's xfer struct */
711     osi_timeval_t xferStartTime,        /*FS xfer start time */
712                     xferStopTime;       /*FS xfer stop time */
713     afs_size_t bytesToXfer = 0, bytesXferred = 0;
714 #endif
715
716     AFS_STATCNT(CacheFetchProc);
717
718     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
719
720     code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
721
722 #ifndef AFS_NOSTATS
723     xferP =
724         &(afs_stats_cmfullperf.rpc.fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
725     osi_GetuTime(&xferStartTime);
726 #endif /* AFS_NOSTATS */
727
728     adc->validPos = abase;
729
730     if ( !code ) do {
731         if (moredata) {
732             code = (*ops->more)(rock, &length, &moredata);
733             if ( code )
734                 break;
735         }
736         /*
737          * The fetch protocol is extended for the AFS/DFS translator
738          * to allow multiple blocks of data, each with its own length,
739          * to be returned. As long as the top bit is set, there are more
740          * blocks expected.
741          *
742          * We do not do this for AFS file servers because they sometimes
743          * return large negative numbers as the transfer size.
744          */
745         if (avc->f.states & CForeign) {
746             moredata = length & 0x80000000;
747             length &= ~0x80000000;
748         } else {
749             moredata = 0;
750         }
751 #ifndef AFS_NOSTATS
752         bytesToXfer += length;
753 #endif /* AFS_NOSTATS */
754         while (length > 0) {
755 #ifdef RX_KERNEL_TRACE
756             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
757                        "before rx_Read");
758 #endif
759             code = (*ops->read)(rock, length, &bytesread);
760 #ifdef RX_KERNEL_TRACE
761             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
762                        "after rx_Read");
763 #endif
764 #ifndef AFS_NOSTATS
765             bytesXferred += bytesread;
766 #endif /* AFS_NOSTATS */
767             if ( code ) {
768                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
769                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
770                            ICL_TYPE_INT32, length);
771                 code = -34;
772                 break;
773             }
774             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
775             if ( code )
776                 break;
777             offset += bytesread;
778             abase += bytesread;
779             length -= bytesread;
780             adc->validPos = abase;
781             if (afs_osi_Wakeup(&adc->validPos) == 0)
782                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
783                            __FILE__, ICL_TYPE_INT32, __LINE__,
784                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
785                            adc->dflags);
786         }
787         code = 0;
788     } while (moredata);
789     if (!code)
790         code = (*ops->close)(rock, avc, adc, tsmall);
791     (*ops->destroy)(&rock, code);
792
793 #ifndef AFS_NOSTATS
794     osi_GetuTime(&xferStopTime);
795     (xferP->numXfers)++;
796     if (!code) {
797         (xferP->numSuccesses)++;
798         afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] += bytesXferred;
799         (xferP->sumBytes) +=
800                 (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
801         afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
802         if (bytesXferred < xferP->minBytes)
803             xferP->minBytes = bytesXferred;
804         if (bytesXferred > xferP->maxBytes)
805             xferP->maxBytes = bytesXferred;
806
807         /*
808          * Tally the size of the object.  Note: we tally the actual size,
809          * NOT the number of bytes that made it out over the wire.
810          */
811         if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
812             (xferP->count[0])++;
813         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
814             (xferP->count[1])++;
815         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
816             (xferP->count[2])++;
817         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
818             (xferP->count[3])++;
819         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
820             (xferP->count[4])++;
821         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
822             (xferP->count[5])++;
823         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
824             (xferP->count[6])++;
825         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
826             (xferP->count[7])++;
827         else
828             (xferP->count[8])++;
829
830         afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
831         afs_stats_AddTo((xferP->sumTime), elapsedTime);
832         afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
833         if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
834             afs_stats_TimeAssign((xferP->minTime), elapsedTime);
835         }
836         if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
837             afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
838         }
839     }
840 #endif
841     XSTATS_END_TIME;
842     return code;
843 }
844