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