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