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