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