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