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