Fix CacheFetchProc in cases where the fileserver hates us
[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     afs_uint32 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) {
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         RX_AFS_GUNLOCK();
261         code = rx_EndCall(v->call, error);
262         RX_AFS_GLOCK();
263         if (!code && error)
264             code = error;
265     }
266     if (v->tbuffer)
267         osi_FreeLargeSpace(v->tbuffer);
268     if (v->tiov)
269         osi_FreeSmallSpace(v->tiov);
270     osi_FreeSmallSpace(v);
271     return code;
272 }
273
274 static
275 struct storeOps rxfs_storeUfsOps = {
276     rxfs_storeUfsPrepare,
277     rxfs_storeUfsRead,
278     rxfs_storeUfsWrite,
279     rxfs_storeStatus,
280     rxfs_storePadd,
281     rxfs_storeClose,
282     rxfs_storeDestroy
283 };
284
285 static
286 struct storeOps rxfs_storeMemOps = {
287     rxfs_storeMemPrepare,
288     rxfs_storeMemRead,
289     rxfs_storeMemWrite,
290     rxfs_storeStatus,
291     rxfs_storePadd,
292     rxfs_storeClose,
293     rxfs_storeDestroy
294 };
295
296 afs_int32
297 rxfs_storeInit(struct vcache *avc, struct afs_conn *tc, afs_size_t base,
298                 afs_size_t bytes, afs_size_t length,
299                 int sync, struct storeOps **ops, void **rock)
300 {
301     afs_int32 code;
302     struct rxfs_storeVariables *v;
303
304     if ( !tc )
305         return -1;
306
307     v = (struct rxfs_storeVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
308     if (!v)
309         osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
310     memset(v, 0, sizeof(struct rxfs_storeVariables));
311
312     v->InStatus.ClientModTime = avc->f.m.Date;
313     v->InStatus.Mask = AFS_SETMODTIME;
314     if (sync & AFS_SYNC)
315         v->InStatus.Mask |= AFS_FSYNC;
316     RX_AFS_GUNLOCK();
317     v->call = rx_NewCall(tc->id);
318     if (v->call) {
319 #ifdef AFS_64BIT_CLIENT
320         if (!afs_serverHasNo64Bit(tc))
321             code = StartRXAFS_StoreData64(
322                                 v->call, (struct AFSFid*)&avc->f.fid.Fid,
323                                 &v->InStatus, base, bytes, length);
324         else
325             if (length > 0xFFFFFFFF)
326                 code = EFBIG;
327             else {
328                 afs_int32 t1 = base, t2 = bytes, t3 = length;
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, length);
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         length = avc->f.m.Length - 1; /* byte position of last byte we'll store */
362         if (shouldWake) {
363             if (AFS_CHUNK(length) != 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 *tfile;
414         int offset = 0;
415         struct dcache *tdc = dclist[i];
416         afs_int32 size = 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         tfile = 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, size);
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, size is *always* the amount of data we'll be trying
447          * to ship here.
448          */
449         bytesToXfer = size;
450         bytesXferred = 0;
451
452         osi_GetuTime(&xferStartTime);
453 #endif /* AFS_NOSTATS */
454
455         while ( size > 0 ) {
456             afs_uint32 tlen;
457             afs_int32 bytesread, byteswritten;
458             code = (*ops->prepare)(rock, size, &tlen);
459             if ( code )
460                 break;
461
462             code = (*ops->read)(rock, tfile, 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             size -= 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, size);
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(tfile);
495         if ((tdc->f.chunkBytes < afs_OtherCSize)
496                 && (i < (nchunks - 1)) && code == 0) {
497             code = (*ops->padd)(rock, afs_OtherCSize - tdc->f.chunkBytes);
498         }
499         stored += tdc->f.chunkBytes;
500         /* ideally, I'd like to unlock the dcache and turn
501          * off the writing bit here, but that would
502          * require being able to retry StoreAllSegments in
503          * the event of a failure. It only really matters
504          * if user can't read from a 'locked' dcache or
505          * one which has the writing bit turned on. */
506     }
507
508     if (!code) {
509         code = (*ops->close)(rock, OutStatus, doProcessFS);
510         if (*doProcessFS) {
511             hadd32(*anewDV, 1);
512         }
513         XSTATS_END_TIME;
514     }
515     if (ops)
516         code = (*ops->destroy)(&rock, code);
517     return code;
518 }
519
520 #define lmin(a,b) (((a) < (b)) ? (a) : (b))
521 /*!
522  *      Called upon store.
523  *
524  * \param dclist pointer to the list of dcaches
525  * \param avc Ptr to the vcache entry.
526  * \param areq Ptr to the request structure
527  * \param sync sync flag
528  * \param minj the chunk offset for this call
529  * \param high index of last dcache to store
530  * \param moredata the moredata flag
531  * \param anewDV Ptr to the dataversion after store
532  * \param amaxStoredLength Ptr to the amount of that is actually stored
533  *
534  * \note Environment: Nothing interesting.
535  */
536 int
537 afs_CacheStoreVCache(struct dcache **dcList, struct vcache *avc,
538                         struct vrequest *areq, int sync,
539                         unsigned int minj, unsigned int high,
540                         unsigned int moredata,
541                         afs_hyper_t *anewDV, afs_size_t *amaxStoredLength)
542 {
543     afs_int32 code = 0;
544     struct storeOps *ops;
545     void * rock = NULL;
546     unsigned int i, j;
547
548     struct AFSFetchStatus OutStatus;
549     int doProcessFS = 0;
550     afs_size_t base, bytes, length;
551     int nomore;
552     unsigned int first = 0;
553     struct afs_conn *tc;
554
555     for (bytes = 0, j = 0; !code && j <= high; j++) {
556         if (dcList[j]) {
557             ObtainSharedLock(&(dcList[j]->lock), 629);
558             if (!bytes)
559                 first = j;
560             bytes += dcList[j]->f.chunkBytes;
561             if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
562                         && (dcList[j]->f.chunk - minj < high)
563                         && dcList[j + 1]) {
564                 int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
565                 bytes += sbytes;
566             }
567         }
568         if (bytes && (j == high || !dcList[j + 1])) {
569             afs_uint32 nchunks;
570             struct dcache **dclist = &dcList[first];
571             /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
572             base = AFS_CHUNKTOBASE(first + minj);
573             /*
574              *
575              * take a list of dcache structs and send them all off to the server
576              * the list must be in order, and the chunks contiguous.
577              * Note - there is no locking done by this code currently.  For
578              * safety's sake, xdcache could be locked over the entire call.
579              * However, that pretty well ties up all the threads.  Meantime, all
580              * the chunks _MUST_ have their refcounts bumped.
581              * The writes done before a store back will clear setuid-ness
582              * in cache file.
583              * We can permit CacheStoreProc to wake up the user process IFF we
584              * are doing the last RPC for this close, ie, storing back the last
585              * set of contiguous chunks of a file.
586              */
587
588             nchunks = 1 + j - first;
589             nomore = !(moredata || (j != high));
590             length = lmin(avc->f.m.Length, avc->f.truncPos);
591             afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64,
592                        ICL_TYPE_FID, &avc->f.fid.Fid, ICL_TYPE_OFFSET,
593                        ICL_HANDLE_OFFSET(base), ICL_TYPE_OFFSET,
594                        ICL_HANDLE_OFFSET(bytes), ICL_TYPE_OFFSET,
595                        ICL_HANDLE_OFFSET(length));
596             tc = afs_Conn(&avc->f.fid, areq, 0);
597
598             do {
599 #ifdef AFS_64BIT_CLIENT
600               restart:
601 #endif
602                 code = rxfs_storeInit(avc, tc, base, bytes, length,
603                                         sync, &ops, &rock);
604                 if ( code )
605                     goto nocall;
606
607                 code = afs_CacheStoreDCaches(avc, dclist, bytes, anewDV,
608                         &doProcessFS, &OutStatus, nchunks, nomore, ops, rock);
609
610 nocall:
611 #ifdef AFS_64BIT_CLIENT
612                 if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
613                     afs_serverSetNo64Bit(tc);
614                     goto restart;
615                 }
616 #else
617                 continue;       /* dummy, label before block end won't work */
618 #endif /* AFS_64BIT_CLIENT */
619             } while (afs_Analyze
620                      (tc, code, &avc->f.fid, areq,
621                       AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
622                       NULL));
623
624             /* put back all remaining locked dcache entries */
625             for (i = 0; i < nchunks; i++) {
626                 struct dcache *tdc = dclist[i];
627                 if (!code) {
628                     if (afs_indexFlags[tdc->index] & IFDataMod) {
629                         /*
630                          * LOCKXXX -- should hold afs_xdcache(W) when
631                          * modifying afs_indexFlags.
632                          */
633                         afs_indexFlags[tdc->index] &= ~IFDataMod;
634                         afs_stats_cmperf.cacheCurrDirtyChunks--;
635                         afs_indexFlags[tdc->index] &= ~IFDirtyPages;
636                         if (sync & AFS_VMSYNC_INVAL) {
637                             /* since we have invalidated all the pages of this
638                              ** vnode by calling osi_VM_TryToSmush, we can
639                              ** safely mark this dcache entry as not having
640                              ** any pages. This vnode now becomes eligible for
641                              ** reclamation by getDownD.
642                              */
643                             afs_indexFlags[tdc->index] &= ~IFAnyPages;
644                         }
645                     }
646                 }
647                 UpgradeSToWLock(&tdc->lock, 628);
648                 tdc->f.states &= ~DWriting;     /* correct? */
649                 tdc->dflags |= DFEntryMod;
650                 ReleaseWriteLock(&tdc->lock);
651                 afs_PutDCache(tdc);
652                 /* Mark the entry as released */
653                 dclist[i] = NULL;
654             }
655
656             if (doProcessFS) {
657                 /* Now copy out return params */
658                 UpgradeSToWLock(&avc->lock, 28);        /* keep out others for a while */
659                 afs_ProcessFS(avc, &OutStatus, areq);
660                 /* Keep last (max) size of file on server to see if
661                  * we need to call afs_StoreMini to extend the file.
662                  */
663                 if (!moredata)
664                     *amaxStoredLength = OutStatus.Length;
665                 ConvertWToSLock(&avc->lock);
666                 doProcessFS = 0;
667             }
668
669             if (code) {
670                 for (j++; j <= high; j++) {
671                     if (dcList[j]) {
672                         ReleaseSharedLock(&(dcList[j]->lock));
673                         afs_PutDCache(dcList[j]);
674                         /* Releasing entry */
675                         dcList[j] = NULL;
676                     }
677                 }
678             }
679
680             afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
681                        ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
682             bytes = 0;
683         }
684     }
685
686     return code;
687 }
688
689 /* rock and operations for RX_FILESERVER */
690
691 struct rxfs_fetchVariables {
692     struct rx_call *call;
693     char *tbuffer;
694     struct iovec *iov;
695     afs_uint32 nio;
696     afs_int32 hasNo64bit;
697     afs_int32 iovno;
698     afs_int32 iovmax;
699 };
700
701 afs_int32
702 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
703 {
704     afs_int32 code;
705     afs_uint32 tlen;
706     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
707
708     *bytesread = 0;
709     tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
710     RX_AFS_GUNLOCK();
711     code = rx_Read(v->call, v->tbuffer, tlen);
712     RX_AFS_GLOCK();
713     if (code <= 0)
714         return -34;
715     *bytesread = code;
716     return 0;
717 }
718
719 afs_int32
720 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
721 {
722     afs_int32 code;
723     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
724
725     *bytesread = 0;
726     RX_AFS_GUNLOCK();
727     code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
728     RX_AFS_GLOCK();
729     if (code <= 0)
730         return -34;
731     *bytesread = code;
732     return 0;
733 }
734
735
736 afs_int32
737 rxfs_fetchMemWrite(void *r, struct osi_file *fP,
738                         afs_uint32 offset, afs_uint32 tlen,
739                         afs_uint32 *byteswritten)
740 {
741     afs_int32 code;
742     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
743     struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
744
745     code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
746     if (code != tlen) {
747         return EIO;
748     }
749     *byteswritten = code;
750     return 0;
751 }
752
753 afs_int32
754 rxfs_fetchUfsWrite(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
761     code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
762     if (code != tlen) {
763         return EIO;
764     }
765     *byteswritten = code;
766     return 0;
767 }
768
769
770 afs_int32
771 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
772                                         struct afs_FetchOutput *o)
773 {
774     afs_int32 code, code1 = 0;
775     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
776
777     if (!v->call)
778         return -1;
779
780     RX_AFS_GUNLOCK();
781 #ifdef AFS_64BIT_CLIENT
782     if (!v->hasNo64bit)
783         code = EndRXAFS_FetchData64(v->call, &o->OutStatus, &o->CallBack,
784                                 &o->tsync);
785     else
786 #endif
787         code = EndRXAFS_FetchData(v->call, &o->OutStatus, &o->CallBack,
788                                 &o->tsync);
789     code1 = rx_EndCall(v->call, code);
790     RX_AFS_GLOCK();
791     if (!code && code1)
792         code = code1;
793
794     v->call = NULL;
795
796     return code;
797 }
798
799 afs_int32
800 rxfs_fetchDestroy(void **r, afs_int32 error)
801 {
802     afs_int32 code = error;
803     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
804
805     *r = NULL;
806     if (v->call) {
807         RX_AFS_GUNLOCK();
808         code = rx_EndCall(v->call, error);
809         RX_AFS_GLOCK();
810         if (error)
811             code = error;
812     }
813     if (v->tbuffer)
814         osi_FreeLargeSpace(v->tbuffer);
815     if (v->iov)
816         osi_FreeSmallSpace(v->iov);
817     osi_FreeSmallSpace(v);
818     return code;
819 }
820
821 afs_int32
822 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
823 {
824     afs_int32 code;
825     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
826
827     /*
828      * The fetch protocol is extended for the AFS/DFS translator
829      * to allow multiple blocks of data, each with its own length,
830      * to be returned. As long as the top bit is set, there are more
831      * blocks expected.
832      *
833      * We do not do this for AFS file servers because they sometimes
834      * return large negative numbers as the transfer size.
835      */
836     RX_AFS_GUNLOCK();
837     code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
838     RX_AFS_GLOCK();
839     *length = ntohl(*length);
840     if (code != sizeof(afs_int32)) {
841         code = rx_Error(v->call);
842         *moredata = 0;
843         return (code ? code : -1);      /* try to return code, not -1 */
844     }
845     *moredata = *length & 0x80000000;
846     *length &= ~0x80000000;
847     return 0;
848 }
849
850 static
851 struct fetchOps rxfs_fetchUfsOps = {
852     rxfs_fetchMore,
853     rxfs_fetchUfsRead,
854     rxfs_fetchUfsWrite,
855     rxfs_fetchClose,
856     rxfs_fetchDestroy
857 };
858
859 static
860 struct fetchOps rxfs_fetchMemOps = {
861     rxfs_fetchMore,
862     rxfs_fetchMemRead,
863     rxfs_fetchMemWrite,
864     rxfs_fetchClose,
865     rxfs_fetchDestroy
866 };
867
868 afs_int32
869 rxfs_fetchInit(struct afs_conn *tc, struct vcache *avc, afs_offs_t base,
870                 afs_uint32 size, afs_int32 *alength, afs_uint32 *moredata,
871                 struct dcache *adc,
872                 struct osi_file *fP, struct fetchOps **ops, void **rock)
873 {
874     struct rxfs_fetchVariables *v;
875     int code = 0, code1;
876     afs_uint32 length_hi = 0, length, bytes;
877
878     v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
879     if (!v)
880         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
881     memset(v, 0, sizeof(struct rxfs_fetchVariables));
882
883     RX_AFS_GUNLOCK();
884     v->call = rx_NewCall(tc->id);
885     RX_AFS_GLOCK();
886     if (v->call) {
887 #ifdef AFS_64BIT_CLIENT
888         afs_size_t length64;     /* as returned from server */
889         if (!afs_serverHasNo64Bit(tc)) {
890             afs_uint64 llbytes = size;
891             RX_AFS_GUNLOCK();
892             code = StartRXAFS_FetchData64(v->call, (struct AFSFid *) &avc->f.fid.Fid,
893                                                base, llbytes);
894             if (code != 0) {
895                 RX_AFS_GLOCK();
896                 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
897                                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
898             } else {
899                 bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
900                 RX_AFS_GLOCK();
901                 if (bytes == sizeof(afs_int32)) {
902                     length_hi = ntohl(length_hi);
903                 } else {
904                     code = rx_Error(v->call);
905                     RX_AFS_GUNLOCK();
906                     code1 = rx_EndCall(v->call, code);
907                     RX_AFS_GLOCK();
908                     v->call = NULL;
909                 }
910             }
911         }
912         if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
913             if (base > 0x7FFFFFFF) {
914                 code = EFBIG;
915             } else {
916                 afs_uint32 pos;
917                 pos = base;
918                 RX_AFS_GUNLOCK();
919                 if (!v->call)
920                     v->call = rx_NewCall(tc->id);
921                 code =
922                     StartRXAFS_FetchData(
923                                 v->call, (struct AFSFid*)&avc->f.fid.Fid,
924                                 pos, size);
925                 RX_AFS_GLOCK();
926             }
927             afs_serverSetNo64Bit(tc);
928         }
929         if (!code) {
930             RX_AFS_GUNLOCK();
931             bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
932             RX_AFS_GLOCK();
933             if (bytes == sizeof(afs_int32))
934                 length = ntohl(length);
935             else {
936                 RX_AFS_GUNLOCK();
937                 code = rx_Error(v->call);
938                 code1 = rx_EndCall(v->call, code);
939                 v->call = NULL;
940                 RX_AFS_GLOCK();
941             }
942         }
943         FillInt64(length64, length_hi, length);
944         afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
945                    ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
946                    ICL_TYPE_OFFSET,
947                    ICL_HANDLE_OFFSET(length64));
948         *alength = length;
949 #else /* AFS_64BIT_CLIENT */
950         RX_AFS_GUNLOCK();
951         code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
952                                      base, size);
953         RX_AFS_GLOCK();
954         if (code == 0) {
955             RX_AFS_GUNLOCK();
956             bytes =
957                 rx_Read(v->call, (char *)&length, sizeof(afs_int32));
958             RX_AFS_GLOCK();
959             if (bytes == sizeof(afs_int32)) {
960                 *alength = ntohl(length);
961             } else {
962                 code = rx_Error(v->call);
963                 code1 = rx_EndCall(v->call, code);
964                 v->call = NULL;
965             }
966         }
967 #endif /* AFS_64BIT_CLIENT */
968     } else
969         code = -1;
970     if (code) {
971         osi_FreeSmallSpace(v);
972         return code;
973     }
974     /*
975      * The fetch protocol is extended for the AFS/DFS translator
976      * to allow multiple blocks of data, each with its own length,
977      * to be returned. As long as the top bit is set, there are more
978      * blocks expected.
979      *
980      * We do not do this for AFS file servers because they sometimes
981      * return large negative numbers as the transfer size.
982      *
983      * Also omit if length > 2^32, as bit 31 is part of the number then.
984      * Known to never be the case ifndef AFS_64BIT_CLIENT.
985      */
986     if (avc->f.states & CForeign && !length_hi) {
987         *moredata = length & 0x80000000;
988         length &= ~0x80000000;
989     }
990     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
991         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
992         if (!v->tbuffer)
993             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
994         osi_Assert(WriteLocked(&adc->lock));
995         fP->offset = 0;
996         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
997     }
998     else {
999         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
1000                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
1001                    ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
1002         /*
1003          * We need to alloc the iovecs on the heap so that they are "pinned"
1004          * rather than declare them on the stack - defect 11272
1005          */
1006         v->iov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
1007         if (!v->iov)
1008             osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace for iovecs returned NULL\n");
1009         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
1010     }
1011     *rock = (void *)v;
1012     return 0;
1013 }
1014
1015
1016 /*!
1017  * Routine called on fetch; also tells people waiting for data
1018  *      that more has arrived.
1019  *
1020  * \param tc Ptr to the Rx connection structure.
1021  * \param fP File descriptor for the cache file.
1022  * \param base Base offset to fetch.
1023  * \param adc Ptr to the dcache entry for the file, write-locked.
1024  * \param avc Ptr to the vcache entry for the file.
1025  * \param size Amount of data that should be fetched.
1026  * \param tsmall Ptr to the afs_FetchOutput structure.
1027  *
1028  * \note Environment: Nothing interesting.
1029  */
1030 int
1031 afs_CacheFetchProc(struct afs_conn *tc, struct osi_file *fP, afs_size_t base,
1032                     struct dcache *adc, struct vcache *avc, afs_int32 size,
1033                     struct afs_FetchOutput *tsmall)
1034 {
1035     afs_int32 code;
1036     afs_int32 length;
1037     afs_uint32 bytesread, byteswritten;
1038     struct fetchOps *ops = NULL;
1039     void *rock = NULL;
1040     int moredata = 0;
1041     int offset = 0;
1042
1043     XSTATS_DECLS;
1044 #ifndef AFS_NOSTATS
1045     osi_timeval_t xferStartTime;        /*FS xfer start time */
1046     afs_size_t bytesToXfer = 0, bytesXferred = 0;
1047 #endif
1048
1049     AFS_STATCNT(CacheFetchProc);
1050
1051     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
1052
1053     /*
1054      * Locks held:
1055      * avc->lock(R) if setLocks && !slowPass
1056      * avc->lock(W) if !setLocks || slowPass
1057      * adc->lock(W)
1058      */
1059     code = rxfs_fetchInit(
1060                 tc, avc, base, size, &length, &moredata, adc, fP, &ops, &rock);
1061
1062 #ifndef AFS_NOSTATS
1063     osi_GetuTime(&xferStartTime);
1064 #endif /* AFS_NOSTATS */
1065
1066     if (adc) {
1067         adc->validPos = base;
1068     }
1069
1070     if ( !code ) do {
1071         if ((avc->f.states & CForeign) && moredata && !length) {
1072             code = (*ops->more)(rock, &length, &moredata);
1073             if ( code )
1074                 break;
1075         }
1076 #ifndef AFS_NOSTATS
1077         bytesToXfer += length;
1078 #endif /* AFS_NOSTATS */
1079         while (length > 0) {
1080 #ifdef RX_KERNEL_TRACE
1081             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1082                        "before rx_Read");
1083 #endif
1084             code = (*ops->read)(rock, length, &bytesread);
1085 #ifdef RX_KERNEL_TRACE
1086             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1087                        "after rx_Read");
1088 #endif
1089 #ifndef AFS_NOSTATS
1090             bytesXferred += bytesread;
1091 #endif /* AFS_NOSTATS */
1092             if ( code ) {
1093                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1094                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1095                            ICL_TYPE_INT32, length);
1096                 code = -34;
1097                 break;
1098             }
1099             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
1100             if ( code )
1101                 break;
1102             offset += bytesread;
1103             base += bytesread;
1104             length -= bytesread;
1105             adc->validPos = base;
1106             if (afs_osi_Wakeup(&adc->validPos) == 0)
1107                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
1108                            __FILE__, ICL_TYPE_INT32, __LINE__,
1109                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
1110                            adc->dflags);
1111         }
1112         code = 0;
1113     } while (moredata);
1114     if (!code)
1115         code = (*ops->close)(rock, avc, adc, tsmall);
1116     if (ops)
1117         (*ops->destroy)(&rock, code);
1118
1119 #ifndef AFS_NOSTATS
1120     FillStoreStats(code, AFS_STATS_FS_XFERIDX_FETCHDATA, &xferStartTime,
1121                         bytesToXfer, bytesXferred);
1122 #endif
1123     XSTATS_END_TIME;
1124     return code;
1125 }