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