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