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