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