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