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