2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
13 #include "afs/sysincludes.h" /* Standard vendor system headers */
14 #ifndef AFS_LINUX22_ENV
15 #include "rpc/types.h"
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"
28 extern int cacheDiskType;
30 /* rock and operations for RX_FILESERVER */
32 struct rxfs_storeVariables {
38 struct AFSStoreStatus InStatus;
42 rxfs_storeUfsPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
44 *tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
49 rxfs_storeMemPrepare(void *r, afs_uint32 size, afs_uint32 *tlen)
52 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *) r;
54 *tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
56 code = rx_WritevAlloc(v->call, v->tiov, &v->tnio, RX_MAXIOVECS, *tlen);
59 code = rx_Error(v->call);
71 rxfs_storeUfsRead(void *r, struct osi_file *tfile, afs_uint32 offset,
72 afs_uint32 tlen, afs_uint32 *bytesread)
75 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
78 code = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
84 #if defined(KERNEL_HAVE_UERROR)
92 rxfs_storeMemRead(void *r, struct osi_file *tfile, afs_uint32 offset,
93 afs_uint32 tlen, afs_uint32 *bytesread)
96 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
97 struct memCacheEntry *mceP = (struct memCacheEntry *)tfile;
100 code = afs_MemReadvBlk(mceP, offset, v->tiov, v->tnio, tlen);
108 rxfs_storeMemWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
111 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
114 code = rx_Writev(v->call, v->tiov, v->tnio, l);
117 code = rx_Error(v->call);
118 return (code ? code : -33);
120 *byteswritten = code;
125 rxfs_storeUfsWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
128 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
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. */
137 code = rx_Error(v->call);
138 return (code ? code : -33);
140 *byteswritten = code;
145 rxfs_storeStatus(void *rock)
147 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
149 if (rx_GetRemoteStatus(v->call) & 1)
155 rxfs_storeDestroy(void **r, afs_int32 error)
157 afs_int32 code = error;
158 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
162 osi_FreeLargeSpace(v->tbuffer);
164 osi_FreeSmallSpace(v->tiov);
165 osi_FreeSmallSpace(v);
170 struct storeOps rxfs_storeUfsOps = {
171 rxfs_storeUfsPrepare,
179 struct storeOps rxfs_storeMemOps = {
180 rxfs_storeMemPrepare,
188 rxfs_storeInit(struct vcache *avc, struct storeOps **ops, void **rock)
190 struct rxfs_storeVariables *v;
192 v = (struct rxfs_storeVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
194 osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
195 memset(v, 0, sizeof(struct rxfs_storeVariables));
197 v->InStatus.ClientModTime = avc->f.m.Date;
198 v->InStatus.Mask = AFS_SETMODTIME;
200 if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
201 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
204 ("rxfs_storeInit: osi_AllocLargeSpace for iovecs returned NULL\n");
205 *ops = (struct storeOps *) &rxfs_storeUfsOps;
207 v->tiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
210 ("rxfs_storeInit: osi_AllocSmallSpace for iovecs returned NULL\n");
211 *ops = (struct storeOps *) &rxfs_storeMemOps;
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 */
219 if (AFS_CHUNK(tlen) != 0)
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.
244 * \note Environment: Nothing interesting.
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)
256 struct storeOps *ops;
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);
264 osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed");
266 ((struct rxfs_storeVariables *)rock)->call = acall;
268 AFS_STATCNT(CacheStoreProc);
271 * In this case, alen is *always* the amount of data we'll be trying
274 *(abytesToXferP) = alen;
275 *(abytesXferredP) = 0;
276 #endif /* AFS_NOSTATS */
279 afs_int32 bytesread, byteswritten;
280 code = (*ops->prepare)(rock, alen, &tlen);
284 code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
289 code = (*ops->write)(rock, tlen, &byteswritten);
293 (*abytesXferredP) += byteswritten;
294 #endif /* AFS_NOSTATS */
299 * if file has been locked on server, can allow
302 if (shouldWake && *shouldWake && ((*ops->status)(rock) == 0)) {
303 *shouldWake = 0; /* only do this once */
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);
314 /* rock and operations for RX_FILESERVER */
316 struct rxfs_fetchVariables {
317 struct rx_call *call;
321 afs_int32 hasNo64bit;
327 rxfs_fetchUfsRead(void *r, afs_uint32 size, afs_uint32 *bytesread)
331 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
334 tlen = (size > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : size);
336 code = rx_Read(v->call, v->tbuffer, tlen);
345 rxfs_fetchMemRead(void *r, afs_uint32 tlen, afs_uint32 *bytesread)
348 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
352 code = rx_Readv(v->call, v->iov, &v->nio, RX_MAXIOVECS, tlen);
362 rxfs_fetchMemWrite(void *r, struct osi_file *fP,
363 afs_uint32 offset, afs_uint32 tlen,
364 afs_uint32 *byteswritten)
367 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
368 struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
370 code = afs_MemWritevBlk(mceP, offset, v->iov, v->nio, tlen);
374 *byteswritten = code;
379 rxfs_fetchUfsWrite(void *r, struct osi_file *fP,
380 afs_uint32 offset, afs_uint32 tlen,
381 afs_uint32 *byteswritten)
384 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
386 code = afs_osi_Write(fP, -1, v->tbuffer, tlen);
390 *byteswritten = code;
395 rxfs_fetchDestroy(void **r, afs_int32 error)
397 afs_int32 code = error;
398 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
402 osi_FreeLargeSpace(v->tbuffer);
404 osi_FreeSmallSpace(v->iov);
405 osi_FreeSmallSpace(v);
410 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
413 register struct rxfs_fetchVariables *v
414 = (struct rxfs_fetchVariables *)r;
417 code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
418 *length = ntohl(*length);
420 if (code != sizeof(afs_int32)) {
421 code = rx_Error(v->call);
422 return (code ? code : -1); /* try to return code, not -1 */
428 struct fetchOps rxfs_fetchUfsOps = {
436 struct fetchOps rxfs_fetchMemOps = {
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)
448 struct rxfs_fetchVariables *v;
450 v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
452 osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
453 memset(v, 0, sizeof(struct rxfs_fetchVariables));
457 if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
458 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
460 osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
461 osi_Assert(WriteLocked(&adc->lock));
463 *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
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);
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
474 (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
477 osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
478 *ops = (struct fetchOps *) &rxfs_fetchMemOps;
486 * Routine called on fetch; also tells people waiting for data
487 * that more has arrived.
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.
499 * \note Environment: Nothing interesting.
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)
508 register afs_int32 code;
510 afs_uint32 bytesread, byteswritten;
511 struct fetchOps *ops = NULL;
514 register int offset = 0;
516 AFS_STATCNT(CacheFetchProc);
518 length = lengthFound;
520 if ( cacheDiskType != AFS_FCACHE_TYPE_UFS ) {
523 (*abytesToXferP) = 0;
524 (*abytesXferredP) = 0;
525 #endif /* AFS_NOSTATS */
527 adc->validPos = abase;
529 code = rxfs_fetchInit(acall, avc, abase, &length, adc, fP,
530 (struct fetchOps **)&ops, (char**)&rock);
533 code = (*ops->more)(rock, &length, &moredata);
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
543 * We do not do this for AFS file servers because they sometimes
544 * return large negative numbers as the transfer size.
546 if (avc->f.states & CForeign) {
547 moredata = length & 0x80000000;
548 length &= ~0x80000000;
553 (*abytesToXferP) += length;
554 #endif /* AFS_NOSTATS */
556 #ifdef RX_KERNEL_TRACE
557 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
560 code = (*ops->read)(rock, length, &bytesread);
561 #ifdef RX_KERNEL_TRACE
562 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
566 (*abytesXferredP) += bytesread;
567 #endif /* AFS_NOSTATS */
569 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
570 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
571 ICL_TYPE_INT32, length);
575 code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
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,
590 (*ops->destroy)(&rock, code);