Unite CacheFetchProcs and add abstraction calls.
[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 /* rock and operations for RX_FILESERVER */
31
32 struct rxfs_storeVariables {
33     struct rx_call *call;
34     char *tbuffer;
35     struct iovec *tiov;
36     afs_uint32 tnio;
37     afs_int32 hasNo64bit;
38     struct AFSStoreStatus InStatus;
39 };
40
41 afs_int32
42 rxfs_storeUfsPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
43 {
44     *tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
45     return 0;
46 }
47
48 afs_int32
49 rxfs_storeMemPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
50 {
51     afs_int32 code;
52     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *) r;
53
54     *tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
55     RX_AFS_GUNLOCK();
56     code = rx_WritevAlloc(v->call, v->tiov, &v->tnio, RX_MAXIOVECS, *tlen);
57     RX_AFS_GLOCK();
58     if (code <= 0) {
59         code = rx_Error(v->call);
60         if ( !code )
61             code = -33;
62     }
63     else {
64         *tlen = code;
65         code = 0;
66     }
67     return code;
68 }
69
70 afs_int32
71 rxfs_storeUfsRead(void *r, struct osi_file *tfile, afs_uint32 offset,
72                   afs_uint32 tlen, afs_uint32 *bytesread)
73 {
74     afs_int32 code;
75     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
76
77     *bytesread = 0;
78     code = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
79     if (code < 0)
80         return EIO;
81     *bytesread = code;
82     if (code == tlen)
83         return 0;
84 #if defined(KERNEL_HAVE_UERROR)
85     if (getuerror())
86         return EIO;
87 #endif
88     return 0;
89 }
90
91 afs_int32
92 rxfs_storeMemRead(void *r, struct osi_file *tfile, afs_uint32 offset,
93                   afs_uint32 tlen, afs_uint32 *bytesread)
94 {
95     afs_int32 code;
96     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
97     struct memCacheEntry *mceP = (struct memCacheEntry *)tfile;
98
99     *bytesread = 0;
100     code = afs_MemReadvBlk(mceP, offset, v->tiov, v->tnio, tlen);
101     if (code != tlen)
102         return -33;
103     *bytesread = code;
104     return 0;
105 }
106
107 afs_int32
108 rxfs_storeMemWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
109 {
110     afs_int32 code;
111     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
112
113     RX_AFS_GUNLOCK();
114     code = rx_Writev(v->call, v->tiov, v->tnio, l);
115     RX_AFS_GLOCK();
116     if (code != l) {
117         code = rx_Error(v->call);
118         return (code ? code : -33);
119     }
120     *byteswritten = code;
121     return 0;
122 }
123
124 afs_int32
125 rxfs_storeUfsWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
126 {
127     afs_int32 code;
128     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
129
130     RX_AFS_GUNLOCK();
131     code = rx_Write(v->call, v->tbuffer, l);
132         /* writing 0 bytes will
133          * push a short packet.  Is that really what we want, just because the
134          * data didn't come back from the disk yet?  Let's try it and see. */
135     RX_AFS_GLOCK();
136     if (code != l) {
137         code = rx_Error(v->call);
138         return (code ? code : -33);
139     }
140     *byteswritten = code;
141     return 0;
142 }
143
144 afs_int32
145 rxfs_storeStatus(void *rock)
146 {
147     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
148
149     if (rx_GetRemoteStatus(v->call) & 1)
150         return 0;
151     return 1;
152 }
153
154 afs_int32
155 rxfs_storeDestroy(void **r, afs_int32 error)
156 {
157     afs_int32 code = error;
158     struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
159
160     *r = NULL;
161     if (v->tbuffer)
162         osi_FreeLargeSpace(v->tbuffer);
163     if (v->tiov)
164         osi_FreeSmallSpace(v->tiov);
165     osi_FreeSmallSpace(v);
166     return code;
167 }
168
169 static
170 struct storeOps rxfs_storeUfsOps = {
171     rxfs_storeUfsPrepare,
172     rxfs_storeUfsRead,
173     rxfs_storeUfsWrite,
174     rxfs_storeStatus,
175     rxfs_storeDestroy
176 };
177
178 static
179 struct storeOps rxfs_storeMemOps = {
180     rxfs_storeMemPrepare,
181     rxfs_storeMemRead,
182     rxfs_storeMemWrite,
183     rxfs_storeStatus,
184     rxfs_storeDestroy
185 };
186
187 afs_int32
188 rxfs_storeInit(struct vcache *avc, struct storeOps **ops, void **rock)
189 {
190     struct rxfs_storeVariables *v;
191
192     v = (struct rxfs_storeVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
193     if (!v)
194         osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
195     memset(v, 0, sizeof(struct rxfs_storeVariables));
196
197     v->InStatus.ClientModTime = avc->f.m.Date;
198     v->InStatus.Mask = AFS_SETMODTIME;
199
200     if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
201         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
202         if (!v->tbuffer)
203             osi_Panic
204               ("rxfs_storeInit: osi_AllocLargeSpace for iovecs returned NULL\n");
205         *ops = (struct storeOps *) &rxfs_storeUfsOps;
206     } else {
207         v->tiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
208         if (!v->tiov)
209             osi_Panic
210               ("rxfs_storeInit: osi_AllocSmallSpace for iovecs returned NULL\n");
211         *ops = (struct storeOps *) &rxfs_storeMemOps;
212 #ifdef notdef
213         /* do this at a higher level now -- it's a parameter */
214         /* for now, only do 'continue from close' code if file fits in one
215          * chunk.  Could clearly do better: if only one modified chunk
216          * then can still do this.  can do this on *last* modified chunk */
217         tlen = avc->f.m.Length - 1;     /* byte position of last byte we'll store */
218         if (shouldWake) {
219             if (AFS_CHUNK(tlen) != 0)
220                 *shouldWake = 0;
221             else
222                 *shouldWake = 1;
223         }
224 #endif /* notdef */
225     }
226     *rock = (void *)v;
227     return 0;
228 }
229
230
231 /*!
232  *      Called upon store.
233  *
234  * \param acall Ptr to the Rx call structure involved.
235  * \param fP Ptr to the related file descriptor.
236  * \param alen Size of the file in bytes.
237  * \param avc Ptr to the vcache entry.
238  * \param shouldWake is it "safe" to return early from close() ?
239  * \param abytesToXferP Set to the number of bytes to xfer.
240  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
241  * \param abytesXferredP Set to the number of bytes actually xferred.
242  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
243  *
244  * \note Environment: Nothing interesting.
245  */
246 int
247 afs_CacheStoreProc(register struct rx_call *acall,
248                       register struct osi_file *fP,
249                       register afs_int32 alen, struct vcache *avc,
250                       int *shouldWake, afs_size_t * abytesToXferP,
251                       afs_size_t * abytesXferredP)
252 {
253     afs_int32 code;
254     afs_uint32 tlen;
255     int offset = 0;
256     struct storeOps *ops;
257     void * rock = NULL;
258
259     afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
260                ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
261                ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
262     code =  rxfs_storeInit(avc, &ops, &rock);
263     if ( code ) {
264         osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed");
265     }
266     ((struct rxfs_storeVariables *)rock)->call = acall;
267
268     AFS_STATCNT(CacheStoreProc);
269 #ifndef AFS_NOSTATS
270     /*
271      * In this case, alen is *always* the amount of data we'll be trying
272      * to ship here.
273      */
274     *(abytesToXferP) = alen;
275     *(abytesXferredP) = 0;
276 #endif /* AFS_NOSTATS */
277
278     while ( alen > 0 ) {
279         afs_int32 bytesread, byteswritten;
280         code = (*ops->prepare)(rock, alen, &tlen);
281         if ( code )
282             break;
283
284         code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
285         if (code)
286             break;
287
288         tlen = bytesread;
289         code = (*ops->write)(rock, tlen, &byteswritten);
290         if (code)
291             break;
292 #ifndef AFS_NOSTATS
293         (*abytesXferredP) += byteswritten;
294 #endif /* AFS_NOSTATS */
295
296         offset += tlen;
297         alen -= tlen;
298         /*
299          * if file has been locked on server, can allow
300          * store to continue
301          */
302         if (shouldWake && *shouldWake && ((*ops->status)(rock) == 0)) {
303             *shouldWake = 0;    /* only do this once */
304             afs_wakeup(avc);
305         }
306     }
307     afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
308                ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
309                ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
310     code = (*ops->destroy)(&rock, code);
311     return code;
312 }
313
314 /* rock and operations for RX_FILESERVER */
315
316 struct rxfs_fetchVariables {
317     struct rx_call *call;
318     char *tbuffer;
319     struct iovec *iov;
320     afs_uint32 nio;
321     afs_int32 hasNo64bit;
322     afs_int32 iovno;
323     afs_int32 iovmax;
324 };
325
326 afs_int32
327 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
328 {
329     afs_int32 code;
330     afs_uint32 tlen;
331     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
332
333     *bytesread = 0;
334     tlen = (size > AFS_LRALLOCSIZ ?  AFS_LRALLOCSIZ : size);
335     RX_AFS_GUNLOCK();
336     code = rx_Read(v->call, v->tbuffer, tlen);
337     RX_AFS_GLOCK();
338     if (code <= 0)
339         return -34;
340     *bytesread = code;
341     return 0;
342 }
343
344 afs_int32
345 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
346 {
347     afs_int32 code;
348     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
349
350     *bytesread = 0;
351     RX_AFS_GUNLOCK();
352     code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
353     RX_AFS_GLOCK();
354     if (code <= 0)
355         return -34;
356     *bytesread = code;
357     return 0;
358 }
359
360
361 afs_int32
362 rxfs_fetchMemWrite(void *r, struct osi_file *fP,
363                         afs_uint32 offset, afs_uint32 tlen,
364                         afs_uint32 *byteswritten)
365 {
366     afs_int32 code;
367     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
368     struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
369
370     code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
371     if (code != tlen) {
372         return EIO;
373     }
374     *byteswritten = code;
375     return 0;
376 }
377
378 afs_int32
379 rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
380                         afs_uint32 offset, afs_uint32 tlen,
381                         afs_uint32 *byteswritten)
382 {
383     afs_int32 code;
384     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
385
386     code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
387     if (code != tlen) {
388         return EIO;
389     }
390     *byteswritten = code;
391     return 0;
392 }
393
394 afs_int32
395 rxfs_fetchDestroy(void **r, afs_int32 error)
396 {
397     afs_int32 code = error;
398     struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
399
400     *r = NULL;
401     if (v->tbuffer)
402         osi_FreeLargeSpace(v->tbuffer);
403     if (v->iov)
404         osi_FreeSmallSpace(v->iov);
405     osi_FreeSmallSpace(v);
406     return code;
407 }
408
409 afs_int32
410 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
411 {
412     afs_int32 code;
413     register struct rxfs_fetchVariables *v
414             = (struct rxfs_fetchVariables *)r;
415
416     RX_AFS_GUNLOCK();
417     code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
418     *length = ntohl(*length);
419     RX_AFS_GLOCK();
420     if (code != sizeof(afs_int32)) {
421         code = rx_Error(v->call);
422         return (code ? code : -1);      /* try to return code, not -1 */
423     }
424     return 0;
425 }
426
427 static
428 struct fetchOps rxfs_fetchUfsOps = {
429     rxfs_fetchMore,
430     rxfs_fetchUfsRead,
431     rxfs_fetchUfsWrite,
432     rxfs_fetchDestroy
433 };
434
435 static
436 struct fetchOps rxfs_fetchMemOps = {
437     rxfs_fetchMore,
438     rxfs_fetchMemRead,
439     rxfs_fetchMemWrite,
440     rxfs_fetchDestroy
441 };
442
443 afs_int32
444 rxfs_fetchInit(register struct rx_call *acall, struct vcache *avc,
445                 afs_offs_t abase, afs_uint32 *length,  struct dcache *adc,
446                 struct osi_file *fP, struct fetchOps **ops, void **rock)
447 {
448     struct rxfs_fetchVariables *v;
449
450     v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
451     if (!v)
452         osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
453     memset(v, 0, sizeof(struct rxfs_fetchVariables));
454
455     v->call = acall;
456
457     if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
458         v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
459         if (!v->tbuffer)
460             osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
461         osi_Assert(WriteLocked(&adc->lock));
462         fP->offset = 0;
463         *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
464     }
465     else {
466         afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
467                    ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
468                    ICL_HANDLE_OFFSET(abase), ICL_TYPE_INT32, *length);
469         /*
470          * We need to alloc the iovecs on the heap so that they are "pinned"
471          * rather than declare them on the stack - defect 11272
472          */
473         v->iov =
474             (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
475                                                 RX_MAXIOVECS);
476         if (!v->iov)
477             osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
478         *ops = (struct fetchOps *) &rxfs_fetchMemOps;
479     }
480     *rock = (void *)v;
481     return 0;
482 }
483
484
485 /*!
486  * Routine called on fetch; also tells people waiting for data
487  *      that more has arrived.
488  *
489  * \param acall Ptr to the Rx call structure.
490  * \param fP File descriptor for the cache file.
491  * \param abase Base offset to fetch.
492  * \param adc Ptr to the dcache entry for the file, write-locked.
493  * \param avc Ptr to the vcache entry for the file.
494  * \param abytesToXferP Set to the number of bytes to xfer.
495  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
496  * \param abytesXferredP Set to the number of bytes actually xferred.
497  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
498  *
499  * \note Environment: Nothing interesting.
500  */
501 int
502 afs_CacheFetchProc(register struct rx_call *acall,
503                       register struct osi_file *fP, afs_size_t abase,
504                       struct dcache *adc, struct vcache *avc,
505                       afs_size_t * abytesToXferP, afs_size_t * abytesXferredP,
506                       afs_int32 lengthFound)
507 {
508     register afs_int32 code;
509     afs_uint32 length;
510     afs_uint32 bytesread, byteswritten;
511     struct fetchOps *ops = NULL;
512     void *rock = NULL;
513     int moredata = 0;
514     register int offset = 0;
515
516     AFS_STATCNT(CacheFetchProc);
517
518     length = lengthFound;
519
520     if ( cacheDiskType != AFS_FCACHE_TYPE_UFS ) {
521     }
522 #ifndef AFS_NOSTATS
523     (*abytesToXferP) = 0;
524     (*abytesXferredP) = 0;
525 #endif /* AFS_NOSTATS */
526
527     adc->validPos = abase;
528
529     code = rxfs_fetchInit(acall, avc, abase, &length, adc, fP,
530                 (struct fetchOps **)&ops, (char**)&rock);
531     if ( !code ) do {
532         if (moredata) {
533             code = (*ops->more)(rock, &length, &moredata);
534             if ( code )
535                 break;
536         }
537         /*
538          * The fetch protocol is extended for the AFS/DFS translator
539          * to allow multiple blocks of data, each with its own length,
540          * to be returned. As long as the top bit is set, there are more
541          * blocks expected.
542          *
543          * We do not do this for AFS file servers because they sometimes
544          * return large negative numbers as the transfer size.
545          */
546         if (avc->f.states & CForeign) {
547             moredata = length & 0x80000000;
548             length &= ~0x80000000;
549         } else {
550             moredata = 0;
551         }
552 #ifndef AFS_NOSTATS
553         (*abytesToXferP) += length;
554 #endif /* AFS_NOSTATS */
555         while (length > 0) {
556 #ifdef RX_KERNEL_TRACE
557             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
558                        "before rx_Read");
559 #endif
560             code = (*ops->read)(rock, length, &bytesread);
561 #ifdef RX_KERNEL_TRACE
562             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
563                        "after rx_Read");
564 #endif
565 #ifndef AFS_NOSTATS
566             (*abytesXferredP) += bytesread;
567 #endif /* AFS_NOSTATS */
568             if ( code ) {
569                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
570                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
571                            ICL_TYPE_INT32, length);
572                 code = -34;
573                 break;
574             }
575             code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
576             if ( code )
577                 break;
578             offset += bytesread;
579             abase += bytesread;
580             length -= bytesread;
581             adc->validPos = abase;
582             if (afs_osi_Wakeup(&adc->validPos) == 0)
583                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
584                            __FILE__, ICL_TYPE_INT32, __LINE__,
585                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
586                            adc->dflags);
587         }
588         code = 0;
589     } while (moredata);
590     (*ops->destroy)(&rock, code);
591     return code;
592 }
593