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