07eea44f698bc84b0379f9ad3c7a9712e36ba7f0
[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  * afs_UFSCacheFetchProc
232  *
233  * Description:
234  *      Routine called on fetch; also tells people waiting for data
235  *      that more has arrived.
236  *
237  * Parameters:
238  *      acall : Ptr to the Rx call structure.
239  *      afile : File descriptor for the cache file.
240  *      abase : Base offset to fetch.
241  *      adc   : Ptr to the dcache entry for the file, write-locked.
242  *      avc   : Ptr to the vcache entry for the file.
243  *      abytesToXferP  : Set to the number of bytes to xfer.
244  *                       NOTE: This parameter is only used if AFS_NOSTATS
245  *                              is not defined.
246  *      abytesXferredP : Set to the number of bytes actually xferred.
247  *                       NOTE: This parameter is only used if AFS_NOSTATS
248  *                              is not defined.
249  *
250  * Environment:
251  *      Nothing interesting.
252  */
253
254 int
255 afs_UFSCacheFetchProc(register struct rx_call *acall, struct osi_file *afile,
256                       afs_size_t abase, struct dcache *adc,
257                       struct vcache *avc, afs_size_t * abytesToXferP,
258                       afs_size_t * abytesXferredP, afs_int32 lengthFound)
259 {
260     afs_int32 length;
261     register afs_int32 code;
262     register char *tbuffer;
263     register int tlen;
264     int moredata = 0;
265
266     AFS_STATCNT(UFS_CacheFetchProc);
267     osi_Assert(WriteLocked(&adc->lock));
268     afile->offset = 0;          /* Each time start from the beginning */
269     length = lengthFound;
270 #ifndef AFS_NOSTATS
271     (*abytesToXferP) = 0;
272     (*abytesXferredP) = 0;
273 #endif /* AFS_NOSTATS */
274     tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
275     adc->validPos = abase;
276     do {
277         if (moredata) {
278             RX_AFS_GUNLOCK();
279             code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
280             RX_AFS_GLOCK();
281             length = ntohl(length);
282             if (code != sizeof(afs_int32)) {
283                 osi_FreeLargeSpace(tbuffer);
284                 code = rx_Error(acall);
285                 return (code ? code : -1);      /* try to return code, not -1 */
286             }
287         }
288         /*
289          * The fetch protocol is extended for the AFS/DFS translator
290          * to allow multiple blocks of data, each with its own length,
291          * to be returned. As long as the top bit is set, there are more
292          * blocks expected.
293          *
294          * We do not do this for AFS file servers because they sometimes
295          * return large negative numbers as the transfer size.
296          */
297         if (avc->f.states & CForeign) {
298             moredata = length & 0x80000000;
299             length &= ~0x80000000;
300         } else {
301             moredata = 0;
302         }
303 #ifndef AFS_NOSTATS
304         (*abytesToXferP) += length;
305 #endif /* AFS_NOSTATS */
306         while (length > 0) {
307             tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
308 #ifdef RX_KERNEL_TRACE
309             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
310                        "before rx_Read");
311 #endif
312             RX_AFS_GUNLOCK();
313             code = rx_Read(acall, tbuffer, tlen);
314             RX_AFS_GLOCK();
315 #ifdef RX_KERNEL_TRACE
316             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
317                        "after rx_Read");
318 #endif
319 #ifndef AFS_NOSTATS
320             (*abytesXferredP) += code;
321 #endif /* AFS_NOSTATS */
322             if (code != tlen) {
323                 osi_FreeLargeSpace(tbuffer);
324                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
325                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
326                            ICL_TYPE_INT32, length);
327                 return -34;
328             }
329             code = afs_osi_Write(afile, -1, tbuffer, tlen);
330             if (code != tlen) {
331                 osi_FreeLargeSpace(tbuffer);
332                 return EIO;
333             }
334             abase += tlen;
335             length -= tlen;
336             adc->validPos = abase;
337             if (afs_osi_Wakeup(&adc->validPos) == 0)
338                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
339                            __FILE__, ICL_TYPE_INT32, __LINE__,
340                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
341                            adc->dflags);
342         }
343     } while (moredata);
344     osi_FreeLargeSpace(tbuffer);
345     return 0;
346
347 }                               /* afs_UFSCacheFetchProc */
348
349 /*!
350  *      Called upon store.
351  *
352  * \param acall Ptr to the Rx call structure involved.
353  * \param fP Ptr to the related file descriptor.
354  * \param alen Size of the file in bytes.
355  * \param avc Ptr to the vcache entry.
356  * \param shouldWake is it "safe" to return early from close() ?
357  * \param abytesToXferP Set to the number of bytes to xfer.
358  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
359  * \param abytesXferredP Set to the number of bytes actually xferred.
360  *      NOTE: This parameter is only used if AFS_NOSTATS is not defined.
361  *
362  * \note Environment: Nothing interesting.
363  */
364 int
365 afs_CacheStoreProc(register struct rx_call *acall,
366                       register struct osi_file *fP,
367                       register afs_int32 alen, struct vcache *avc,
368                       int *shouldWake, afs_size_t * abytesToXferP,
369                       afs_size_t * abytesXferredP)
370 {
371     afs_int32 code;
372     afs_uint32 tlen;
373     int offset = 0;
374     struct storeOps *ops;
375     void * rock = NULL;
376
377     afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
378                ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
379                ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
380     code =  rxfs_storeInit(avc, &ops, &rock);
381     if ( code ) {
382         osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed");
383     }
384     ((struct rxfs_storeVariables *)rock)->call = acall;
385
386     AFS_STATCNT(CacheStoreProc);
387 #ifndef AFS_NOSTATS
388     /*
389      * In this case, alen is *always* the amount of data we'll be trying
390      * to ship here.
391      */
392     *(abytesToXferP) = alen;
393     *(abytesXferredP) = 0;
394 #endif /* AFS_NOSTATS */
395
396     while ( alen > 0 ) {
397         afs_int32 bytesread, byteswritten;
398         code = (*ops->prepare)(rock, alen, &tlen);
399         if ( code )
400             break;
401
402         code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
403         if (code)
404             break;
405
406         tlen = bytesread;
407         code = (*ops->write)(rock, tlen, &byteswritten);
408         if (code)
409             break;
410 #ifndef AFS_NOSTATS
411         (*abytesXferredP) += byteswritten;
412 #endif /* AFS_NOSTATS */
413
414         offset += tlen;
415         alen -= tlen;
416         /*
417          * if file has been locked on server, can allow
418          * store to continue
419          */
420         if (shouldWake && *shouldWake && ((*ops->status)(rock) == 0)) {
421             *shouldWake = 0;    /* only do this once */
422             afs_wakeup(avc);
423         }
424     }
425     afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
426                ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
427                ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
428     code = (*ops->destroy)(&rock, code);
429     return code;
430 }
431
432 int
433 afs_MemCacheFetchProc(register struct rx_call *acall,
434                       register struct osi_file *fP, afs_size_t abase,
435                       struct dcache *adc, struct vcache *avc,
436                       afs_size_t * abytesToXferP, afs_size_t * abytesXferredP,
437                       afs_int32 lengthFound)
438 {
439     register struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
440     register afs_int32 code;
441     afs_int32 length;
442     int moredata = 0;
443     struct iovec *tiov;         /* no data copying with iovec */
444     register int tlen, offset = 0;
445     int tnio;                   /* temp for iovec size */
446
447     AFS_STATCNT(afs_MemCacheFetchProc);
448     length = lengthFound;
449     afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
450                ICL_TYPE_POINTER, mceP, ICL_TYPE_OFFSET,
451                ICL_HANDLE_OFFSET(abase), ICL_TYPE_INT32, length);
452 #ifndef AFS_NOSTATS
453     (*abytesToXferP) = 0;
454     (*abytesXferredP) = 0;
455 #endif /* AFS_NOSTATS */
456     /*
457      * We need to alloc the iovecs on the heap so that they are "pinned" rather than
458      * declare them on the stack - defect 11272
459      */
460     tiov =
461         (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
462                                             RX_MAXIOVECS);
463     if (!tiov) {
464         osi_Panic
465             ("afs_MemCacheFetchProc: osi_AllocSmallSpace returned NULL\n");
466     }
467     adc->validPos = abase;
468     do {
469         if (moredata) {
470             RX_AFS_GUNLOCK();
471             code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
472             length = ntohl(length);
473             RX_AFS_GLOCK();
474             if (code != sizeof(afs_int32)) {
475                 code = rx_Error(acall);
476                 osi_FreeSmallSpace(tiov);
477                 return (code ? code : -1);      /* try to return code, not -1 */
478             }
479         }
480         /*
481          * The fetch protocol is extended for the AFS/DFS translator
482          * to allow multiple blocks of data, each with its own length,
483          * to be returned. As long as the top bit is set, there are more
484          * blocks expected.
485          *
486          * We do not do this for AFS file servers because they sometimes
487          * return large negative numbers as the transfer size.
488          */
489         if (avc->f.states & CForeign) {
490             moredata = length & 0x80000000;
491             length &= ~0x80000000;
492         } else {
493             moredata = 0;
494         }
495 #ifndef AFS_NOSTATS
496         (*abytesToXferP) += length;
497 #endif /* AFS_NOSTATS */
498         while (length > 0) {
499             tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
500             RX_AFS_GUNLOCK();
501             code = rx_Readv(acall, tiov, &tnio, RX_MAXIOVECS, tlen);
502             RX_AFS_GLOCK();
503 #ifndef AFS_NOSTATS
504             (*abytesXferredP) += code;
505 #endif /* AFS_NOSTATS */
506             if (code <= 0) {
507                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
508                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
509                            ICL_TYPE_INT32, length);
510                 osi_FreeSmallSpace(tiov);
511                 return -34;
512             }
513             tlen = code;
514             afs_MemWritevBlk(mceP, offset, tiov, tnio, tlen);
515             offset += tlen;
516             abase += tlen;
517             length -= tlen;
518             adc->validPos = abase;
519             if (afs_osi_Wakeup(&adc->validPos) == 0)
520                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
521                            __FILE__, ICL_TYPE_INT32, __LINE__,
522                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
523                            adc->dflags);
524         }
525     } while (moredata);
526     /* max of two sizes */
527     osi_FreeSmallSpace(tiov);
528     return 0;
529 }
530