Use bigger I/O sizes for the memcache
[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 #endif /* AFS_ALPHA_ENV */
23 #include "afsincludes.h"        /* Afs-based standard headers */
24 #include "afs/afs_stats.h"      /* statistics */
25 #include "afs_prototypes.h"
26
27 extern int cacheDiskType;
28
29 #ifndef AFS_NOSTATS
30 void
31 FillStoreStats(int code, int idx, osi_timeval_t *xferStartTime,
32                afs_size_t bytesToXfer, afs_size_t bytesXferred)
33 {
34     struct afs_stats_xferData *xferP;
35     osi_timeval_t xferStopTime;
36     osi_timeval_t elapsedTime;
37
38     xferP = &(afs_stats_cmfullperf.rpc.fsXferTimes[idx]);
39     osi_GetuTime(&xferStopTime);
40     (xferP->numXfers)++;
41     if (!code) {
42         (xferP->numSuccesses)++;
43         afs_stats_XferSumBytes[idx] += bytesXferred;
44         (xferP->sumBytes) += (afs_stats_XferSumBytes[idx] >> 10);
45         afs_stats_XferSumBytes[idx] &= 0x3FF;
46         if (bytesXferred < xferP->minBytes)
47             xferP->minBytes = bytesXferred;
48         if (bytesXferred > xferP->maxBytes)
49             xferP->maxBytes = bytesXferred;
50
51         /*
52          * Tally the size of the object.  Note: we tally the actual size,
53          * NOT the number of bytes that made it out over the wire.
54          */
55         if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0) (xferP->count[0])++;
56         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1) (xferP->count[1])++;
57         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2) (xferP->count[2])++;
58         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3) (xferP->count[3])++;
59         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4) (xferP->count[4])++;
60         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5) (xferP->count[5])++;
61         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6) (xferP->count[6])++;
62         else if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7) (xferP->count[7])++;
63         else
64             (xferP->count[8])++;
65
66         afs_stats_GetDiff(elapsedTime, (*xferStartTime), xferStopTime);
67         afs_stats_AddTo((xferP->sumTime), elapsedTime);
68         afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
69         if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
70             afs_stats_TimeAssign((xferP->minTime), elapsedTime);
71         }
72         if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
73             afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
74         }
75     }
76 }
77 #endif /* AFS_NOSTATS */
78
79 /* rock and operations for RX_FILESERVER */
80
81
82
83 afs_int32
84 rxfs_storeUfsPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
85 {
86     *tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
87     return 0;
88 }
89
90 afs_int32
91 rxfs_storeMemPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
92 {
93     afs_int32 code;
94     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *) r;
95
96     RX_AFS_GUNLOCK();
97     code = rx_WritevAlloc(v->call, v->tiov, &v->tnio, RX_MAXIOVECS, size);
98     RX_AFS_GLOCK();
99     if (code <= 0) {
100         code = rx_Error(v->call);
101         if (!code)
102             code = -33;
103     }
104     else {
105         *tlen = code;
106         code = 0;
107     }
108     return code;
109 }
110
111 afs_int32
112 rxfs_storeUfsRead(void *r, struct osi_file *tfile, afs_uint32 offset,
113                   afs_uint32 tlen, afs_uint32 *bytesread)
114 {
115     afs_int32 code;
116     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
117
118     *bytesread = 0;
119     code = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
120     if (code < 0)
121         return EIO;
122     *bytesread = code;
123     if (code == tlen)
124         return 0;
125 #if defined(KERNEL_HAVE_UERROR)
126     if (getuerror())
127         return EIO;
128 #endif
129     return 0;
130 }
131
132 afs_int32
133 rxfs_storeMemRead(void *r, struct osi_file *tfile, afs_uint32 offset,
134                   afs_uint32 tlen, afs_uint32 *bytesread)
135 {
136     afs_int32 code;
137     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
138     struct memCacheEntry *mceP = (struct memCacheEntry *)tfile;
139
140     *bytesread = 0;
141     code = afs_MemReadvBlk(mceP, offset, v->tiov, v->tnio, tlen);
142     if (code != tlen)
143         return -33;
144     *bytesread = code;
145     return 0;
146 }
147
148 afs_int32
149 rxfs_storeMemWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
150 {
151     afs_int32 code;
152     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
153
154     RX_AFS_GUNLOCK();
155     code = rx_Writev(v->call, v->tiov, v->tnio, l);
156     RX_AFS_GLOCK();
157     if (code != l) {
158         code = rx_Error(v->call);
159         return (code ? code : -33);
160     }
161     *byteswritten = code;
162     return 0;
163 }
164
165 afs_int32
166 rxfs_storeUfsWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
167 {
168     afs_int32 code;
169     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
170
171     RX_AFS_GUNLOCK();
172     code = rx_Write(v->call, v->tbuffer, l);
173         /* writing 0 bytes will
174          * push a short packet.  Is that really what we want, just because the
175          * data didn't come back from the disk yet?  Let's try it and see. */
176     RX_AFS_GLOCK();
177     if (code != l) {
178         code = rx_Error(v->call);
179         return (code ? code : -33);
180     }
181     *byteswritten = code;
182     return 0;
183 }
184
185 afs_int32
186 rxfs_storePadd(void *rock, afs_uint32 size)
187 {
188     afs_int32 code = 0;
189     afs_uint32 tlen;
190     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
191
192     if (!v->tbuffer)
193         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
194     memset(v->tbuffer, 0, AFS_LRALLOCSIZ);
195
196     while (size) {
197         tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
198         RX_AFS_GUNLOCK();
199         code = rx_Write(v->call, v->tbuffer, tlen);
200         RX_AFS_GLOCK();
201
202         if (code != tlen)
203             return -33; /* XXX */
204         size -= tlen;
205     }
206     return 0;
207 }
208
209 afs_int32
210 rxfs_storeStatus(void *rock)
211 {
212     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
213
214     if (rx_GetRemoteStatus(v->call) & 1)
215         return 0;
216     return 1;
217 }
218
219 afs_int32
220 rxfs_storeClose(void *r, struct AFSFetchStatus *OutStatus, int *doProcessFS)
221 {
222     afs_int32 code;
223     struct AFSVolSync tsync;
224     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
225
226     if (!v->call)
227         return -1;
228     RX_AFS_GUNLOCK();
229 #ifdef AFS_64BIT_CLIENT
230     if (!v->hasNo64bit)
231         code = EndRXAFS_StoreData64(v->call, OutStatus, &tsync);
232     else
233 #endif
234         code = EndRXAFS_StoreData(v->call, OutStatus, &tsync);
235     RX_AFS_GLOCK();
236     if (!code)
237         *doProcessFS = 1;       /* Flag to run afs_ProcessFS() later on */
238
239     return code;
240 }
241
242 afs_int32
243 rxfs_storeDestroy(void **r, afs_int32 error)
244 {
245     afs_int32 code = error;
246     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
247
248     *r = NULL;
249     if (v->call) {
250         RX_AFS_GUNLOCK();
251         code = rx_EndCall(v->call, error);
252         RX_AFS_GLOCK();
253         if (!code && error)
254             code = error;
255     }
256     if (v->tbuffer)
257         osi_FreeLargeSpace(v->tbuffer);
258     if (v->tiov)
259         osi_FreeSmallSpace(v->tiov);
260     osi_FreeSmallSpace(v);
261     return code;
262 }
263
264 afs_int32
265 afs_GenericStoreProc(struct storeOps *ops, void *rock,
266                      struct dcache *tdc, int *shouldwake,
267                      afs_size_t *bytesXferred)
268 {
269     struct rxfs_storeVariables *svar = rock;
270     afs_uint32 tlen, bytesread, byteswritten;
271     afs_int32 code = 0;
272     int offset = 0;
273     afs_size_t size;
274     struct osi_file *tfile;
275
276     size = tdc->f.chunkBytes;
277
278     tfile = afs_CFileOpen(&tdc->f.inode);
279
280     while ( size > 0 ) {
281         code = (*ops->prepare)(rock, size, &tlen);
282         if ( code )
283             break;
284
285         code = (*ops->read)(rock, tfile, offset, tlen, &bytesread);
286         if (code)
287             break;
288
289         tlen = bytesread;
290         code = (*ops->write)(rock, tlen, &byteswritten);
291         if (code)
292             break;
293 #ifndef AFS_NOSTATS
294         *bytesXferred += byteswritten;
295 #endif /* AFS_NOSTATS */
296
297         offset += tlen;
298         size -= tlen;
299         /*
300          * if file has been locked on server, can allow
301          * store to continue
302          */
303         if (shouldwake && *shouldwake && ((*ops->status)(rock) == 0)) {
304             *shouldwake = 0;    /* only do this once */
305             afs_wakeup(svar->vcache);
306         }
307     }
308     afs_CFileClose(tfile);
309
310     return code;
311 }
312
313 static
314 struct storeOps rxfs_storeUfsOps = {
315 #ifndef HAVE_STRUCT_LABEL_SUPPORT
316     rxfs_storeUfsPrepare,
317     rxfs_storeUfsRead,
318     rxfs_storeUfsWrite,
319     rxfs_storeStatus,
320     rxfs_storePadd,
321     rxfs_storeClose,
322     rxfs_storeDestroy,
323     afs_GenericStoreProc
324 #else
325     .prepare =  rxfs_storeUfsPrepare,
326     .read =     rxfs_storeUfsRead,
327     .write =    rxfs_storeUfsWrite,
328     .status =   rxfs_storeStatus,
329     .padd =     rxfs_storePadd,
330     .close =    rxfs_storeClose,
331     .destroy =  rxfs_storeDestroy,
332 #ifdef AFS_LINUX26_ENV
333     .storeproc = afs_linux_storeproc
334 #else
335     .storeproc = afs_GenericStoreProc
336 #endif
337 #endif
338 };
339
340 static
341 struct storeOps rxfs_storeMemOps = {
342 #ifndef HAVE_STRUCT_LABEL_SUPPORT
343     rxfs_storeMemPrepare,
344     rxfs_storeMemRead,
345     rxfs_storeMemWrite,
346     rxfs_storeStatus,
347     rxfs_storePadd,
348     rxfs_storeClose,
349     rxfs_storeDestroy,
350     afs_GenericStoreProc
351 #else
352     .prepare =  rxfs_storeMemPrepare,
353     .read =     rxfs_storeMemRead,
354     .write =    rxfs_storeMemWrite,
355     .status =   rxfs_storeStatus,
356     .padd =     rxfs_storePadd,
357     .close =    rxfs_storeClose,
358     .destroy =  rxfs_storeDestroy,
359     .storeproc = afs_GenericStoreProc
360 #endif
361 };
362
363 afs_int32
364 rxfs_storeInit(struct vcache *avc, struct afs_conn *tc, afs_size_t base,
365                 afs_size_t bytes, afs_size_t length,
366                 int sync, struct storeOps **ops, void **rock)
367 {
368     afs_int32 code;
369     struct rxfs_storeVariables *v;
370
371     if ( !tc )
372         return -1;
373
374     v = (struct rxfs_storeVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
375     if (!v)
376         osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
377     memset(v, 0, sizeof(struct rxfs_storeVariables));
378
379     v->InStatus.ClientModTime = avc->f.m.Date;
380     v->InStatus.Mask = AFS_SETMODTIME;
381     v->vcache = avc;
382     if (sync & AFS_SYNC)
383         v->InStatus.Mask |= AFS_FSYNC;
384     RX_AFS_GUNLOCK();
385     v->call = rx_NewCall(tc->id);
386     if (v->call) {
387 #ifdef AFS_64BIT_CLIENT
388         if (!afs_serverHasNo64Bit(tc))
389             code = StartRXAFS_StoreData64(
390                                 v->call, (struct AFSFid*)&avc->f.fid.Fid,
391                                 &v->InStatus, base, bytes, length);
392         else
393             if (length > 0xFFFFFFFF)
394                 code = EFBIG;
395             else {
396                 afs_int32 t1 = base, t2 = bytes, t3 = length;
397                 code = StartRXAFS_StoreData(v->call,
398                                         (struct AFSFid *) &avc->f.fid.Fid,
399                                          &v->InStatus, t1, t2, t3);
400             }
401 #else /* AFS_64BIT_CLIENT */
402         code = StartRXAFS_StoreData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
403                                     &v->InStatus, base, bytes, length);
404 #endif /* AFS_64BIT_CLIENT */
405     } else
406         code = -1;
407     RX_AFS_GLOCK();
408     if (code) {
409         osi_FreeSmallSpace(v);
410         return code;
411     }
412     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
413         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
414         if (!v->tbuffer)
415             osi_Panic
416             ("rxfs_storeInit: osi_AllocLargeSpace for iovecs returned NULL\n");
417         *ops = (struct storeOps *) &rxfs_storeUfsOps;
418     } else {
419         v->tiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
420         if (!v->tiov)
421             osi_Panic
422             ("rxfs_storeInit: osi_AllocSmallSpace for iovecs returned NULL\n");
423         *ops = (struct storeOps *) &rxfs_storeMemOps;
424 #ifdef notdef
425         /* do this at a higher level now -- it's a parameter */
426         /* for now, only do 'continue from close' code if file fits in one
427          * chunk.  Could clearly do better: if only one modified chunk
428          * then can still do this.  can do this on *last* modified chunk */
429         length = avc->f.m.Length - 1; /* byte position of last byte we'll store */
430         if (shouldWake) {
431             if (AFS_CHUNK(length) != 0)
432                 *shouldWake = 0;
433             else
434                 *shouldWake = 1;
435         }
436 #endif /* notdef */
437     }
438
439     *rock = (void *)v;
440     return 0;
441 }
442 unsigned int storeallmissing = 0;
443 /*!
444  *      Called for each chunk upon store.
445  *
446  * \param avc Ptr to the vcache entry of the file being stored.
447  * \param dclist pointer to the list of dcaches
448  * \param bytes total number of bytes for the current operation
449  * \param anewDV Ptr to the dataversion after store
450  * \param doProcessFS pointer to the "do process FetchStatus" flag
451  * \param OutStatus pointer to the FetchStatus as returned by the fileserver
452  * \param nchunks number of dcaches to consider
453  * \param nomore copy of the "no more data" flag
454  * \param ops pointer to the block of storeOps to be used for this operation
455  * \param rock pointer to the opaque protocol-specific data of this operation
456  */
457 afs_int32
458 afs_CacheStoreDCaches(struct vcache *avc, struct dcache **dclist,
459                       afs_size_t bytes, afs_hyper_t *anewDV, int *doProcessFS,
460                       struct AFSFetchStatus *OutStatus, afs_uint32 nchunks,
461                       int nomore, struct storeOps *ops, void *rock)
462 {
463     int *shouldwake = NULL;
464     unsigned int i;
465     afs_int32 code = 0;
466     afs_size_t bytesXferred;
467
468 #ifndef AFS_NOSTATS
469     osi_timeval_t xferStartTime;        /*FS xfer start time */
470     afs_size_t bytesToXfer = 10000;     /* # bytes to xfer */
471 #endif /* AFS_NOSTATS */
472     XSTATS_DECLS;
473
474     for (i = 0; i < nchunks && !code; i++) {
475         int stored = 0;
476         struct dcache *tdc = dclist[i];
477         afs_int32 size = tdc->f.chunkBytes;
478         if (!tdc) {
479             afs_warn("afs: missing dcache!\n");
480             storeallmissing++;
481             continue;   /* panic? */
482         }
483         afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2, ICL_TYPE_POINTER, avc,
484                     ICL_TYPE_INT32, tdc->f.chunk, ICL_TYPE_INT32, tdc->index,
485                     ICL_TYPE_INT32, afs_inode2trace(&tdc->f.inode));
486         shouldwake = 0;
487         if (nomore) {
488             if (avc->asynchrony == -1) {
489                 if (afs_defaultAsynchrony > (bytes - stored))
490                     shouldwake = &nomore;
491             }
492             else if ((afs_uint32) avc->asynchrony >= (bytes - stored))
493                 shouldwake = &nomore;
494         }
495
496         afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
497                     ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
498                     ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, size);
499
500         AFS_STATCNT(CacheStoreProc);
501
502         XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
503         avc->f.truncPos = AFS_NOTRUNC;
504 #ifndef AFS_NOSTATS
505         /*
506          * In this case, size is *always* the amount of data we'll be trying
507          * to ship here.
508          */
509         bytesToXfer = size;
510
511         osi_GetuTime(&xferStartTime);
512 #endif /* AFS_NOSTATS */
513         bytesXferred = 0;
514
515         code = (*ops->storeproc)(ops, rock, tdc, shouldwake,
516                                      &bytesXferred);
517
518         afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
519                     ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
520                     ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, size);
521
522 #ifndef AFS_NOSTATS
523         FillStoreStats(code, AFS_STATS_FS_XFERIDX_STOREDATA,
524                     &xferStartTime, bytesToXfer, bytesXferred);
525 #endif /* AFS_NOSTATS */
526
527         if ((tdc->f.chunkBytes < afs_OtherCSize)
528                 && (i < (nchunks - 1)) && code == 0) {
529             code = (*ops->padd)(rock, afs_OtherCSize - tdc->f.chunkBytes);
530         }
531         stored += tdc->f.chunkBytes;
532         /* ideally, I'd like to unlock the dcache and turn
533          * off the writing bit here, but that would
534          * require being able to retry StoreAllSegments in
535          * the event of a failure. It only really matters
536          * if user can't read from a 'locked' dcache or
537          * one which has the writing bit turned on. */
538     }
539
540     if (!code) {
541         code = (*ops->close)(rock, OutStatus, doProcessFS);
542         if (*doProcessFS) {
543             hadd32(*anewDV, 1);
544         }
545         XSTATS_END_TIME;
546     }
547     if (ops)
548         code = (*ops->destroy)(&rock, code);
549     return code;
550 }
551
552 #define lmin(a,b) (((a) < (b)) ? (a) : (b))
553 /*!
554  *      Called upon store.
555  *
556  * \param dclist pointer to the list of dcaches
557  * \param avc Ptr to the vcache entry.
558  * \param areq Ptr to the request structure
559  * \param sync sync flag
560  * \param minj the chunk offset for this call
561  * \param high index of last dcache to store
562  * \param moredata the moredata flag
563  * \param anewDV Ptr to the dataversion after store
564  * \param amaxStoredLength Ptr to the amount of that is actually stored
565  *
566  * \note Environment: Nothing interesting.
567  */
568 int
569 afs_CacheStoreVCache(struct dcache **dcList, struct vcache *avc,
570                      struct vrequest *areq, int sync, unsigned int minj,
571                      unsigned int high, unsigned int moredata,
572                      afs_hyper_t *anewDV, afs_size_t *amaxStoredLength)
573 {
574     afs_int32 code = 0;
575     struct storeOps *ops;
576     void * rock = NULL;
577     unsigned int i, j;
578
579     struct AFSFetchStatus OutStatus;
580     int doProcessFS = 0;
581     afs_size_t base, bytes, length;
582     int nomore;
583     unsigned int first = 0;
584     struct afs_conn *tc;
585
586     for (bytes = 0, j = 0; !code && j <= high; j++) {
587         if (dcList[j]) {
588             ObtainSharedLock(&(dcList[j]->lock), 629);
589             if (!bytes)
590                 first = j;
591             bytes += dcList[j]->f.chunkBytes;
592             if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
593                         && (dcList[j]->f.chunk - minj < high)
594                         && dcList[j + 1]) {
595                 int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
596                 bytes += sbytes;
597             }
598         }
599         if (bytes && (j == high || !dcList[j + 1])) {
600             afs_uint32 nchunks;
601             struct dcache **dclist = &dcList[first];
602             /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
603             base = AFS_CHUNKTOBASE(first + minj);
604             /*
605              *
606              * take a list of dcache structs and send them all off to the server
607              * the list must be in order, and the chunks contiguous.
608              * Note - there is no locking done by this code currently.  For
609              * safety's sake, xdcache could be locked over the entire call.
610              * However, that pretty well ties up all the threads.  Meantime, all
611              * the chunks _MUST_ have their refcounts bumped.
612              * The writes done before a store back will clear setuid-ness
613              * in cache file.
614              * We can permit CacheStoreProc to wake up the user process IFF we
615              * are doing the last RPC for this close, ie, storing back the last
616              * set of contiguous chunks of a file.
617              */
618
619             nchunks = 1 + j - first;
620             nomore = !(moredata || (j != high));
621             length = lmin(avc->f.m.Length, avc->f.truncPos);
622             afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64,
623                        ICL_TYPE_FID, &avc->f.fid.Fid, ICL_TYPE_OFFSET,
624                        ICL_HANDLE_OFFSET(base), ICL_TYPE_OFFSET,
625                        ICL_HANDLE_OFFSET(bytes), ICL_TYPE_OFFSET,
626                        ICL_HANDLE_OFFSET(length));
627
628             do {
629                 tc = afs_Conn(&avc->f.fid, areq, 0);
630
631 #ifdef AFS_64BIT_CLIENT
632               restart:
633 #endif
634                 code = rxfs_storeInit(avc, tc, base, bytes, length,
635                                       sync, &ops, &rock);
636                 if ( !code ) {
637                     code = afs_CacheStoreDCaches(avc, dclist, bytes, anewDV,
638                                                  &doProcessFS, &OutStatus,
639                                                  nchunks, nomore, ops, rock);
640                 }
641
642 #ifdef AFS_64BIT_CLIENT
643                 if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
644                     afs_serverSetNo64Bit(tc);
645                     goto restart;
646                 }
647 #endif /* AFS_64BIT_CLIENT */
648             } while (afs_Analyze
649                      (tc, code, &avc->f.fid, areq,
650                       AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
651                       NULL));
652
653             /* put back all remaining locked dcache entries */
654             for (i = 0; i < nchunks; i++) {
655                 struct dcache *tdc = dclist[i];
656                 if (!code) {
657                     if (afs_indexFlags[tdc->index] & IFDataMod) {
658                         /*
659                          * LOCKXXX -- should hold afs_xdcache(W) when
660                          * modifying afs_indexFlags.
661                          */
662                         afs_indexFlags[tdc->index] &= ~IFDataMod;
663                         afs_stats_cmperf.cacheCurrDirtyChunks--;
664                         afs_indexFlags[tdc->index] &= ~IFDirtyPages;
665                         if (sync & AFS_VMSYNC_INVAL) {
666                             /* since we have invalidated all the pages of this
667                              ** vnode by calling osi_VM_TryToSmush, we can
668                              ** safely mark this dcache entry as not having
669                              ** any pages. This vnode now becomes eligible for
670                              ** reclamation by getDownD.
671                              */
672                             afs_indexFlags[tdc->index] &= ~IFAnyPages;
673                         }
674                     }
675                 }
676                 UpgradeSToWLock(&tdc->lock, 628);
677                 tdc->f.states &= ~DWriting;     /* correct? */
678                 tdc->dflags |= DFEntryMod;
679                 ReleaseWriteLock(&tdc->lock);
680                 afs_PutDCache(tdc);
681                 /* Mark the entry as released */
682                 dclist[i] = NULL;
683             }
684
685             if (doProcessFS) {
686                 /* Now copy out return params */
687                 UpgradeSToWLock(&avc->lock, 28);        /* keep out others for a while */
688                 afs_ProcessFS(avc, &OutStatus, areq);
689                 /* Keep last (max) size of file on server to see if
690                  * we need to call afs_StoreMini to extend the file.
691                  */
692                 if (!moredata)
693                     *amaxStoredLength = OutStatus.Length;
694                 ConvertWToSLock(&avc->lock);
695                 doProcessFS = 0;
696             }
697
698             if (code) {
699                 for (j++; j <= high; j++) {
700                     if (dcList[j]) {
701                         ReleaseSharedLock(&(dcList[j]->lock));
702                         afs_PutDCache(dcList[j]);
703                         /* Releasing entry */
704                         dcList[j] = NULL;
705                     }
706                 }
707             }
708
709             afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
710                        ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
711             bytes = 0;
712         }
713     }
714
715     return code;
716 }
717
718 /* rock and operations for RX_FILESERVER */
719
720 struct rxfs_fetchVariables {
721     struct rx_call *call;
722     char *tbuffer;
723     struct iovec *iov;
724     afs_int32 nio;
725     afs_int32 hasNo64bit;
726     afs_int32 iovno;
727     afs_int32 iovmax;
728 };
729
730 afs_int32
731 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
732 {
733     afs_int32 code;
734     afs_uint32 tlen;
735     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
736
737     *bytesread = 0;
738     tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
739     RX_AFS_GUNLOCK();
740     code = rx_Read(v->call, v->tbuffer, tlen);
741     RX_AFS_GLOCK();
742     if (code <= 0)
743         return -34;
744     *bytesread = code;
745     return 0;
746 }
747
748 afs_int32
749 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
750 {
751     afs_int32 code;
752     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
753
754     *bytesread = 0;
755     RX_AFS_GUNLOCK();
756     code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
757     RX_AFS_GLOCK();
758     if (code <= 0)
759         return -34;
760     *bytesread = code;
761     return 0;
762 }
763
764
765 afs_int32
766 rxfs_fetchMemWrite(void *r, struct osi_file *fP, afs_uint32 offset,
767                    afs_uint32 tlen, afs_uint32 *byteswritten)
768 {
769     afs_int32 code;
770     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
771     struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
772
773     code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
774     if (code != tlen) {
775         return EIO;
776     }
777     *byteswritten = code;
778     return 0;
779 }
780
781 afs_int32
782 rxfs_fetchUfsWrite(void *r, struct osi_file *fP, afs_uint32 offset,
783                    afs_uint32 tlen, afs_uint32 *byteswritten)
784 {
785     afs_int32 code;
786     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
787
788     code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
789     if (code != tlen) {
790         return EIO;
791     }
792     *byteswritten = code;
793     return 0;
794 }
795
796
797 afs_int32
798 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
799                 struct afs_FetchOutput *o)
800 {
801     afs_int32 code, code1 = 0;
802     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
803
804     if (!v->call)
805         return -1;
806
807     RX_AFS_GUNLOCK();
808 #ifdef AFS_64BIT_CLIENT
809     if (!v->hasNo64bit)
810         code = EndRXAFS_FetchData64(v->call, &o->OutStatus, &o->CallBack,
811                                 &o->tsync);
812     else
813 #endif
814         code = EndRXAFS_FetchData(v->call, &o->OutStatus, &o->CallBack,
815                                 &o->tsync);
816     code1 = rx_EndCall(v->call, code);
817     RX_AFS_GLOCK();
818     if (!code && code1)
819         code = code1;
820
821     v->call = NULL;
822
823     return code;
824 }
825
826 afs_int32
827 rxfs_fetchDestroy(void **r, afs_int32 error)
828 {
829     afs_int32 code = error;
830     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
831
832     *r = NULL;
833     if (v->call) {
834         RX_AFS_GUNLOCK();
835         code = rx_EndCall(v->call, error);
836         RX_AFS_GLOCK();
837         if (error)
838             code = error;
839     }
840     if (v->tbuffer)
841         osi_FreeLargeSpace(v->tbuffer);
842     if (v->iov)
843         osi_FreeSmallSpace(v->iov);
844     osi_FreeSmallSpace(v);
845     return code;
846 }
847
848 afs_int32
849 rxfs_fetchMore(void *r, afs_int32 *length, afs_uint32 *moredata)
850 {
851     afs_int32 code;
852     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
853
854     /*
855      * The fetch protocol is extended for the AFS/DFS translator
856      * to allow multiple blocks of data, each with its own length,
857      * to be returned. As long as the top bit is set, there are more
858      * blocks expected.
859      *
860      * We do not do this for AFS file servers because they sometimes
861      * return large negative numbers as the transfer size.
862      */
863     if (*moredata) {
864         RX_AFS_GUNLOCK();
865         code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
866         RX_AFS_GLOCK();
867         *length = ntohl(*length);
868         if (code != sizeof(afs_int32)) {
869             code = rx_Error(v->call);
870             *moredata = 0;
871             return (code ? code : -1);  /* try to return code, not -1 */
872         }
873     }
874     *moredata = *length & 0x80000000;
875     *length &= ~0x80000000;
876     return 0;
877 }
878
879 static
880 struct fetchOps rxfs_fetchUfsOps = {
881     rxfs_fetchMore,
882     rxfs_fetchUfsRead,
883     rxfs_fetchUfsWrite,
884     rxfs_fetchClose,
885     rxfs_fetchDestroy
886 };
887
888 static
889 struct fetchOps rxfs_fetchMemOps = {
890     rxfs_fetchMore,
891     rxfs_fetchMemRead,
892     rxfs_fetchMemWrite,
893     rxfs_fetchClose,
894     rxfs_fetchDestroy
895 };
896
897 afs_int32
898 rxfs_fetchInit(struct afs_conn *tc, struct vcache *avc, afs_offs_t base,
899                afs_uint32 size, afs_int32 *alength, struct dcache *adc,
900                struct osi_file *fP, struct fetchOps **ops, void **rock)
901 {
902     struct rxfs_fetchVariables *v;
903     int code = 0, code1 = 0;
904 #ifdef AFS_64BIT_CLIENT
905     afs_uint32 length_hi = 0;
906 #endif
907     afs_uint32 length, bytes;
908
909     v = (struct rxfs_fetchVariables *)
910             osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
911     if (!v)
912         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
913     memset(v, 0, sizeof(struct rxfs_fetchVariables));
914
915     RX_AFS_GUNLOCK();
916     v->call = rx_NewCall(tc->id);
917     RX_AFS_GLOCK();
918     if (v->call) {
919 #ifdef AFS_64BIT_CLIENT
920         afs_size_t length64;     /* as returned from server */
921         if (!afs_serverHasNo64Bit(tc)) {
922             afs_uint64 llbytes = size;
923             RX_AFS_GUNLOCK();
924             code = StartRXAFS_FetchData64(v->call,
925                                           (struct AFSFid *) &avc->f.fid.Fid,
926                                           base, llbytes);
927             if (code != 0) {
928                 RX_AFS_GLOCK();
929                 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
930                                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
931             } else {
932                 bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
933                 RX_AFS_GLOCK();
934                 if (bytes == sizeof(afs_int32)) {
935                     length_hi = ntohl(length_hi);
936                 } else {
937                     code = rx_Error(v->call);
938                     RX_AFS_GUNLOCK();
939                     code1 = rx_EndCall(v->call, code);
940                     RX_AFS_GLOCK();
941                     v->call = NULL;
942                 }
943             }
944         }
945         if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
946             if (base > 0x7FFFFFFF) {
947                 code = EFBIG;
948             } else {
949                 afs_uint32 pos;
950                 pos = base;
951                 RX_AFS_GUNLOCK();
952                 if (!v->call)
953                     v->call = rx_NewCall(tc->id);
954                 code =
955                     StartRXAFS_FetchData(
956                                 v->call, (struct AFSFid*)&avc->f.fid.Fid,
957                                 pos, size);
958                 RX_AFS_GLOCK();
959             }
960             afs_serverSetNo64Bit(tc);
961         }
962         if (!code) {
963             RX_AFS_GUNLOCK();
964             bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
965             RX_AFS_GLOCK();
966             if (bytes == sizeof(afs_int32))
967                 length = ntohl(length);
968             else {
969                 RX_AFS_GUNLOCK();
970                 code = rx_Error(v->call);
971                 code1 = rx_EndCall(v->call, code);
972                 v->call = NULL;
973                 RX_AFS_GLOCK();
974             }
975         }
976         FillInt64(length64, length_hi, length);
977         afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
978                    ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
979                    ICL_TYPE_OFFSET,
980                    ICL_HANDLE_OFFSET(length64));
981         *alength = length;
982 #else /* AFS_64BIT_CLIENT */
983         RX_AFS_GUNLOCK();
984         code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
985                                      base, size);
986         RX_AFS_GLOCK();
987         if (code == 0) {
988             RX_AFS_GUNLOCK();
989             bytes =
990                 rx_Read(v->call, (char *)&length, sizeof(afs_int32));
991             RX_AFS_GLOCK();
992             if (bytes == sizeof(afs_int32)) {
993                 *alength = ntohl(length);
994             } else {
995                 code = rx_Error(v->call);
996                 code1 = rx_EndCall(v->call, code);
997                 v->call = NULL;
998             }
999         }
1000 #endif /* AFS_64BIT_CLIENT */
1001     } else
1002         code = -1;
1003
1004     /* We need to cast here, in order to avoid issues if *alength is
1005      * negative. Some, older, fileservers can return a negative length,
1006      * which the rest of the code deals correctly with. */
1007     if (code == 0 && *alength > (afs_int32) size) {
1008         /* The fileserver told us it is going to send more data than we
1009          * requested. It shouldn't do that, and accepting that much data
1010          * can make us take up more cache space than we're supposed to,
1011          * so error. */
1012         code = rx_Error(v->call);
1013         RX_AFS_GUNLOCK();
1014         code1 = rx_EndCall(v->call, code);
1015         RX_AFS_GLOCK();
1016         v->call = NULL;
1017         code = EIO;
1018     }
1019
1020     if (!code && code1)
1021         code = code1;
1022
1023     if (code) {
1024         osi_FreeSmallSpace(v);
1025         return code;
1026     }
1027     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
1028         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1029         if (!v->tbuffer)
1030             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
1031         osi_Assert(WriteLocked(&adc->lock));
1032         fP->offset = 0;
1033         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
1034     }
1035     else {
1036         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
1037                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
1038                    ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
1039         /*
1040          * We need to alloc the iovecs on the heap so that they are "pinned"
1041          * rather than declare them on the stack - defect 11272
1042          */
1043         v->iov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
1044         if (!v->iov)
1045             osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace for iovecs returned NULL\n");
1046         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
1047     }
1048     *rock = (void *)v;
1049     return 0;
1050 }
1051
1052
1053 /*!
1054  * Routine called on fetch; also tells people waiting for data
1055  *      that more has arrived.
1056  *
1057  * \param tc Ptr to the Rx connection structure.
1058  * \param fP File descriptor for the cache file.
1059  * \param base Base offset to fetch.
1060  * \param adc Ptr to the dcache entry for the file, write-locked.
1061  * \param avc Ptr to the vcache entry for the file.
1062  * \param size Amount of data that should be fetched.
1063  * \param tsmall Ptr to the afs_FetchOutput structure.
1064  *
1065  * \note Environment: Nothing interesting.
1066  */
1067 int
1068 afs_CacheFetchProc(struct afs_conn *tc, struct osi_file *fP, afs_size_t base,
1069                    struct dcache *adc, struct vcache *avc, afs_int32 size,
1070                    struct afs_FetchOutput *tsmall)
1071 {
1072     afs_int32 code;
1073     afs_int32 length;
1074     afs_uint32 bytesread, byteswritten;
1075     struct fetchOps *ops = NULL;
1076     void *rock = NULL;
1077     afs_uint32 moredata = 0;
1078     int offset = 0;
1079
1080     XSTATS_DECLS;
1081 #ifndef AFS_NOSTATS
1082     osi_timeval_t xferStartTime;        /*FS xfer start time */
1083     afs_size_t bytesToXfer = 0, bytesXferred = 0;
1084 #endif
1085
1086     AFS_STATCNT(CacheFetchProc);
1087
1088     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
1089
1090     /*
1091      * Locks held:
1092      * avc->lock(R) if setLocks && !slowPass
1093      * avc->lock(W) if !setLocks || slowPass
1094      * adc->lock(W)
1095      */
1096     code = rxfs_fetchInit(
1097                 tc, avc, base, size, &length, adc, fP, &ops, &rock);
1098
1099 #ifndef AFS_NOSTATS
1100     osi_GetuTime(&xferStartTime);
1101 #endif /* AFS_NOSTATS */
1102
1103     if (adc) {
1104         adc->validPos = base;
1105     }
1106
1107     if ( !code ) do {
1108         if (avc->f.states & CForeign) {
1109             code = (*ops->more)(rock, &length, &moredata);
1110             if ( code )
1111                 break;
1112         }
1113 #ifndef AFS_NOSTATS
1114         bytesToXfer += length;
1115 #endif /* AFS_NOSTATS */
1116         while (length > 0) {
1117 #ifdef RX_KERNEL_TRACE
1118             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1119                        "before rx_Read");
1120 #endif
1121             code = (*ops->read)(rock, length, &bytesread);
1122 #ifdef RX_KERNEL_TRACE
1123             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1124                        "after rx_Read");
1125 #endif
1126 #ifndef AFS_NOSTATS
1127             bytesXferred += bytesread;
1128 #endif /* AFS_NOSTATS */
1129             if ( code ) {
1130                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1131                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1132                            ICL_TYPE_INT32, length);
1133                 code = -34;
1134                 break;
1135             }
1136             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
1137             if ( code )
1138                 break;
1139             offset += bytesread;
1140             base += bytesread;
1141             length -= bytesread;
1142             adc->validPos = base;
1143             if (afs_osi_Wakeup(&adc->validPos) == 0)
1144                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
1145                            __FILE__, ICL_TYPE_INT32, __LINE__,
1146                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
1147                            adc->dflags);
1148         }
1149         code = 0;
1150     } while (moredata);
1151     if (!code)
1152         code = (*ops->close)(rock, avc, adc, tsmall);
1153     if (ops)
1154         (*ops->destroy)(&rock, code);
1155
1156 #ifndef AFS_NOSTATS
1157     FillStoreStats(code, AFS_STATS_FS_XFERIDX_FETCHDATA, &xferStartTime,
1158                         bytesToXfer, bytesXferred);
1159 #endif
1160     XSTATS_END_TIME;
1161     return code;
1162 }