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