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