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