Initialize xferStartTime for StoreData timings
[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         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                 if ( code ) {
616                     osi_Panic(
617                     "afs_CacheStoreProc: rxfs_storeInit failed with %d", code);
618                 }
619
620                 code = afs_CacheStoreDCaches(avc, dclist, bytes, anewDV,
621                         &doProcessFS, &OutStatus, nchunks, nomore, ops, rock);
622
623 #ifdef AFS_64BIT_CLIENT
624                 if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
625                     afs_serverSetNo64Bit(tc);
626                     goto restart;
627                 }
628 #endif /* AFS_64BIT_CLIENT */
629             } while (afs_Analyze
630                      (tc, code, &avc->f.fid, areq,
631                       AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
632                       NULL));
633
634             /* put back all remaining locked dcache entries */
635             for (i = 0; i < nchunks; i++) {
636                 struct dcache *tdc = dclist[i];
637                 if (!code) {
638                     if (afs_indexFlags[tdc->index] & IFDataMod) {
639                         /*
640                          * LOCKXXX -- should hold afs_xdcache(W) when
641                          * modifying afs_indexFlags.
642                          */
643                         afs_indexFlags[tdc->index] &= ~IFDataMod;
644                         afs_stats_cmperf.cacheCurrDirtyChunks--;
645                         afs_indexFlags[tdc->index] &= ~IFDirtyPages;
646                         if (sync & AFS_VMSYNC_INVAL) {
647                             /* since we have invalidated all the pages of this
648                              ** vnode by calling osi_VM_TryToSmush, we can
649                              ** safely mark this dcache entry as not having
650                              ** any pages. This vnode now becomes eligible for
651                              ** reclamation by getDownD.
652                              */
653                             afs_indexFlags[tdc->index] &= ~IFAnyPages;
654                         }
655                     }
656                 }
657                 UpgradeSToWLock(&tdc->lock, 628);
658                 tdc->f.states &= ~DWriting;     /* correct? */
659                 tdc->dflags |= DFEntryMod;
660                 ReleaseWriteLock(&tdc->lock);
661                 afs_PutDCache(tdc);
662                 /* Mark the entry as released */
663                 dclist[i] = NULL;
664             }
665
666             if (doProcessFS) {
667                 /* Now copy out return params */
668                 UpgradeSToWLock(&avc->lock, 28);        /* keep out others for a while */
669                 afs_ProcessFS(avc, &OutStatus, areq);
670                 /* Keep last (max) size of file on server to see if
671                  * we need to call afs_StoreMini to extend the file.
672                  */
673                 if (!moredata)
674                     *amaxStoredLength = OutStatus.Length;
675                 ConvertWToSLock(&avc->lock);
676                 doProcessFS = 0;
677             }
678
679             if (code) {
680                 for (j++; j <= high; j++) {
681                     if (dcList[j]) {
682                         ReleaseSharedLock(&(dcList[j]->lock));
683                         afs_PutDCache(dcList[j]);
684                         /* Releasing entry */
685                         dcList[j] = NULL;
686                     }
687                 }
688             }
689
690             afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
691                        ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
692             bytes = 0;
693         }
694     }
695
696     return code;
697 }
698
699 /* rock and operations for RX_FILESERVER */
700
701 struct rxfs_fetchVariables {
702     struct rx_call *call;
703     char *tbuffer;
704     struct iovec *iov;
705     afs_uint32 nio;
706     afs_int32 hasNo64bit;
707     afs_int32 iovno;
708     afs_int32 iovmax;
709 };
710
711 afs_int32
712 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
713 {
714     afs_int32 code;
715     afs_uint32 tlen;
716     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
717
718     *bytesread = 0;
719     tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
720     RX_AFS_GUNLOCK();
721     code = rx_Read(v->call, v->tbuffer, tlen);
722     RX_AFS_GLOCK();
723     if (code <= 0)
724         return -34;
725     *bytesread = code;
726     return 0;
727 }
728
729 afs_int32
730 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
731 {
732     afs_int32 code;
733     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
734
735     *bytesread = 0;
736     RX_AFS_GUNLOCK();
737     code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
738     RX_AFS_GLOCK();
739     if (code <= 0)
740         return -34;
741     *bytesread = code;
742     return 0;
743 }
744
745
746 afs_int32
747 rxfs_fetchMemWrite(void *r, struct osi_file *fP,
748                         afs_uint32 offset, afs_uint32 tlen,
749                         afs_uint32 *byteswritten)
750 {
751     afs_int32 code;
752     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
753     struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
754
755     code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
756     if (code != tlen) {
757         return EIO;
758     }
759     *byteswritten = code;
760     return 0;
761 }
762
763 afs_int32
764 rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
765                         afs_uint32 offset, afs_uint32 tlen,
766                         afs_uint32 *byteswritten)
767 {
768     afs_int32 code;
769     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
770
771     code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
772     if (code != tlen) {
773         return EIO;
774     }
775     *byteswritten = code;
776     return 0;
777 }
778
779
780 afs_int32
781 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
782                                         struct afs_FetchOutput *tsmall)
783 {
784     afs_int32 code, code1 = 0;
785     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
786
787     if (!v->call)
788         return -1;
789
790     RX_AFS_GUNLOCK();
791     code = EndRXAFS_FetchData(v->call, &tsmall->OutStatus,
792                               &tsmall->CallBack,
793                               &tsmall->tsync);
794     RX_AFS_GLOCK();
795
796     RX_AFS_GUNLOCK();
797     if (v->call)
798         code1 = rx_EndCall(v->call, code);
799     RX_AFS_GLOCK();
800     if (!code && code1)
801         code = code1;
802
803     v->call = NULL;
804
805     return code;
806 }
807
808 afs_int32
809 rxfs_fetchDestroy(void **r, afs_int32 error)
810 {
811     afs_int32 code = error;
812     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
813
814     *r = NULL;
815     if (v->tbuffer)
816         osi_FreeLargeSpace(v->tbuffer);
817     if (v->iov)
818         osi_FreeSmallSpace(v->iov);
819     osi_FreeSmallSpace(v);
820     return code;
821 }
822
823 afs_int32
824 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
825 {
826     afs_int32 code;
827     register struct rxfs_fetchVariables *v
828             = (struct rxfs_fetchVariables *)r;
829
830     RX_AFS_GUNLOCK();
831     code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
832     *length = ntohl(*length);
833     RX_AFS_GLOCK();
834     if (code != sizeof(afs_int32)) {
835         code = rx_Error(v->call);
836         return (code ? code : -1);      /* try to return code, not -1 */
837     }
838     return 0;
839 }
840
841 static
842 struct fetchOps rxfs_fetchUfsOps = {
843     rxfs_fetchMore,
844     rxfs_fetchUfsRead,
845     rxfs_fetchUfsWrite,
846     rxfs_fetchClose,
847     rxfs_fetchDestroy
848 };
849
850 static
851 struct fetchOps rxfs_fetchMemOps = {
852     rxfs_fetchMore,
853     rxfs_fetchMemRead,
854     rxfs_fetchMemWrite,
855     rxfs_fetchClose,
856     rxfs_fetchDestroy
857 };
858
859 afs_int32
860 rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
861                 afs_uint32 size, afs_uint32 *alength, struct dcache *adc,
862                 struct osi_file *fP, struct fetchOps **ops, void **rock)
863 {
864     struct rxfs_fetchVariables *v;
865     int code, code1;
866     afs_int32 length_hi, length, bytes;
867 #ifdef AFS_64BIT_CLIENT
868     afs_size_t tsize;
869     afs_size_t lengthFound;     /* as returned from server */
870 #endif /* AFS_64BIT_CLIENT */
871
872     v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
873     if (!v)
874         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
875     memset(v, 0, sizeof(struct rxfs_fetchVariables));
876
877     RX_AFS_GUNLOCK();
878     v->call = rx_NewCall(tc->id);
879     RX_AFS_GLOCK();
880
881 #ifdef AFS_64BIT_CLIENT
882     length_hi = code = 0;
883     if (!afs_serverHasNo64Bit(tc)) {
884         tsize = size;
885         RX_AFS_GUNLOCK();
886         code = StartRXAFS_FetchData64(v->call, (struct AFSFid *)&avc->f.fid.Fid,
887                                         base, tsize);
888         if (code != 0) {
889             RX_AFS_GLOCK();
890             afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
891                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
892         } else {
893             bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
894             RX_AFS_GLOCK();
895             if (bytes == sizeof(afs_int32)) {
896                 length_hi = ntohl(length_hi);
897             } else {
898                 length_hi = 0;
899                 code = rx_Error(v->call);
900                 RX_AFS_GUNLOCK();
901                 code1 = rx_EndCall(v->call, code);
902                 RX_AFS_GLOCK();
903                 v->call = NULL;
904             }
905         }
906     }
907     if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
908         if (base > 0x7FFFFFFF) {
909             code = EFBIG;
910         } else {
911             afs_int32 pos;
912             pos = base;
913             RX_AFS_GUNLOCK();
914             if (!v->call)
915                 v->call = rx_NewCall(tc->id);
916             code =
917                 StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
918                                         pos, size);
919             RX_AFS_GLOCK();
920         }
921         afs_serverSetNo64Bit(tc);
922     }
923     if (!code) {
924         RX_AFS_GUNLOCK();
925         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
926         RX_AFS_GLOCK();
927         if (bytes == sizeof(afs_int32))
928             length = ntohl(length);
929         else {
930             code = rx_Error(v->call);
931         }
932     }
933     FillInt64(lengthFound, length_hi, length);
934     afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
935                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
936                ICL_TYPE_OFFSET,
937                ICL_HANDLE_OFFSET(lengthFound));
938 #else /* AFS_64BIT_CLIENT */
939     RX_AFS_GUNLOCK();
940     code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
941                                  base, size);
942     RX_AFS_GLOCK();
943     if (code == 0) {
944         RX_AFS_GUNLOCK();
945         bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
946         RX_AFS_GLOCK();
947         if (bytes == sizeof(afs_int32))
948             length = ntohl(length);
949         else
950             code = rx_Error(v->call);
951     }
952 #endif /* AFS_64BIT_CLIENT */
953     if (code) {
954         osi_FreeSmallSpace(v);
955         return code;
956     }
957
958     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
959         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
960         if (!v->tbuffer)
961             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
962         osi_Assert(WriteLocked(&adc->lock));
963         fP->offset = 0;
964         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
965     }
966     else {
967         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
968                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
969                    ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
970         /*
971          * We need to alloc the iovecs on the heap so that they are "pinned"
972          * rather than declare them on the stack - defect 11272
973          */
974         v->iov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
975         if (!v->iov)
976             osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace for iovecs returned NULL\n");
977         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
978     }
979     *rock = (void *)v;
980     *alength = length;
981     return 0;
982 }
983
984
985 /*!
986  * Routine called on fetch; also tells people waiting for data
987  *      that more has arrived.
988  *
989  * \param tc Ptr to the Rx connection structure.
990  * \param fP File descriptor for the cache file.
991  * \param abase Base offset to fetch.
992  * \param adc Ptr to the dcache entry for the file, write-locked.
993  * \param avc Ptr to the vcache entry for the file.
994  * \param size Amount of data that should be fetched.
995  * \param tsmall Ptr to the afs_FetchOutput structure.
996  *
997  * \note Environment: Nothing interesting.
998  */
999 int
1000 afs_CacheFetchProc(register struct afs_conn *tc,
1001                     register struct osi_file *fP, afs_size_t abase,
1002                     struct dcache *adc, struct vcache *avc,
1003                     afs_int32 size,
1004                     struct afs_FetchOutput *tsmall)
1005 {
1006     register afs_int32 code;
1007     afs_uint32 length;
1008     afs_uint32 bytesread, byteswritten;
1009     struct fetchOps *ops = NULL;
1010     void *rock = NULL;
1011     int moredata = 0;
1012     register int offset = 0;
1013
1014     XSTATS_DECLS;
1015 #ifndef AFS_NOSTATS
1016     osi_timeval_t xferStartTime;        /*FS xfer start time */
1017     afs_size_t bytesToXfer = 0, bytesXferred = 0;
1018 #endif
1019
1020     AFS_STATCNT(CacheFetchProc);
1021
1022     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
1023
1024     code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
1025
1026 #ifndef AFS_NOSTATS
1027     osi_GetuTime(&xferStartTime);
1028 #endif /* AFS_NOSTATS */
1029
1030     adc->validPos = abase;
1031
1032     if ( !code ) do {
1033         if (moredata) {
1034             code = (*ops->more)(rock, &length, &moredata);
1035             if ( code )
1036                 break;
1037         }
1038         /*
1039          * The fetch protocol is extended for the AFS/DFS translator
1040          * to allow multiple blocks of data, each with its own length,
1041          * to be returned. As long as the top bit is set, there are more
1042          * blocks expected.
1043          *
1044          * We do not do this for AFS file servers because they sometimes
1045          * return large negative numbers as the transfer size.
1046          */
1047         if (avc->f.states & CForeign) {
1048             moredata = length & 0x80000000;
1049             length &= ~0x80000000;
1050         } else {
1051             moredata = 0;
1052         }
1053 #ifndef AFS_NOSTATS
1054         bytesToXfer += length;
1055 #endif /* AFS_NOSTATS */
1056         while (length > 0) {
1057 #ifdef RX_KERNEL_TRACE
1058             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1059                        "before rx_Read");
1060 #endif
1061             code = (*ops->read)(rock, length, &bytesread);
1062 #ifdef RX_KERNEL_TRACE
1063             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1064                        "after rx_Read");
1065 #endif
1066 #ifndef AFS_NOSTATS
1067             bytesXferred += bytesread;
1068 #endif /* AFS_NOSTATS */
1069             if ( code ) {
1070                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1071                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1072                            ICL_TYPE_INT32, length);
1073                 code = -34;
1074                 break;
1075             }
1076             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
1077             if ( code )
1078                 break;
1079             offset += bytesread;
1080             abase += bytesread;
1081             length -= bytesread;
1082             adc->validPos = abase;
1083             if (afs_osi_Wakeup(&adc->validPos) == 0)
1084                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
1085                            __FILE__, ICL_TYPE_INT32, __LINE__,
1086                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
1087                            adc->dflags);
1088         }
1089         code = 0;
1090     } while (moredata);
1091     if (!code)
1092         code = (*ops->close)(rock, avc, adc, tsmall);
1093     (*ops->destroy)(&rock, code);
1094
1095 #ifndef AFS_NOSTATS
1096     FillStoreStats(code, AFS_STATS_FS_XFERIDX_FETCHDATA,&xferStartTime,
1097                         bytesToXfer, bytesXferred);
1098 #endif
1099     XSTATS_END_TIME;
1100     return code;
1101 }