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;
396 rxfs_fetchClose(void *r, struct vcache *avc, struct dcache * adc,
397 struct afs_FetchOutput *tsmall)
399 afs_int32 code, code1 = 0;
400 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)r;
406 code = EndRXAFS_FetchData(v->call, &tsmall->OutStatus,
413 code1 = rx_EndCall(v->call, code);
424 rxfs_fetchDestroy(void **r, afs_int32 error)
426 afs_int32 code = error;
427 struct rxfs_fetchVariables *v = (struct rxfs_fetchVariables *)*r;
431 osi_FreeLargeSpace(v->tbuffer);
433 osi_FreeSmallSpace(v->iov);
434 osi_FreeSmallSpace(v);
439 rxfs_fetchMore(void *r, afs_uint32 *length, afs_uint32 *moredata)
442 register struct rxfs_fetchVariables *v
443 = (struct rxfs_fetchVariables *)r;
446 code = rx_Read(v->call, (void *)length, sizeof(afs_int32));
447 *length = ntohl(*length);
449 if (code != sizeof(afs_int32)) {
450 code = rx_Error(v->call);
451 return (code ? code : -1); /* try to return code, not -1 */
457 struct fetchOps rxfs_fetchUfsOps = {
466 struct fetchOps rxfs_fetchMemOps = {
475 rxfs_fetchInit(register struct afs_conn *tc, struct vcache *avc,afs_offs_t base,
476 afs_uint32 size, afs_uint32 *out_length, struct dcache *adc,
477 struct osi_file *fP, struct fetchOps **ops, void **rock)
479 struct rxfs_fetchVariables *v;
481 afs_int32 length_hi, length, bytes;
482 #ifdef AFS_64BIT_CLIENT
484 afs_size_t lengthFound; /* as returned from server */
485 #endif /* AFS_64BIT_CLIENT */
487 v = (struct rxfs_fetchVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_fetchVariables));
489 osi_Panic("rxfs_fetchInit: osi_AllocSmallSpace returned NULL\n");
490 memset(v, 0, sizeof(struct rxfs_fetchVariables));
493 v->call = rx_NewCall(tc->id);
496 #ifdef AFS_64BIT_CLIENT
497 length_hi = code = 0;
498 if (!afs_serverHasNo64Bit(tc)) {
501 code = StartRXAFS_FetchData64(v->call, (struct AFSFid *)&avc->f.fid.Fid,
505 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
506 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
508 bytes = rx_Read(v->call, (char *)&length_hi, sizeof(afs_int32));
510 if (bytes == sizeof(afs_int32)) {
511 length_hi = ntohl(length_hi);
514 code = rx_Error(v->call);
516 code1 = rx_EndCall(v->call, code);
522 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
523 if (base > 0x7FFFFFFF) {
530 v->call = rx_NewCall(tc->id);
532 StartRXAFS_FetchData(v->call, (struct AFSFid *)
533 &avc->f.fid.Fid, pos,
537 afs_serverSetNo64Bit(tc);
541 bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
543 if (bytes == sizeof(afs_int32))
544 length = ntohl(length);
546 code = rx_Error(v->call);
549 FillInt64(lengthFound, length_hi, length);
550 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
551 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
553 ICL_HANDLE_OFFSET(lengthFound));
554 #else /* AFS_64BIT_CLIENT */
556 code = StartRXAFS_FetchData(v->call, (struct AFSFid *)&avc->f.fid.Fid,
561 bytes = rx_Read(v->call, (char *)&length, sizeof(afs_int32));
563 if (bytes == sizeof(afs_int32))
564 length = ntohl(length);
566 code = rx_Error(v->call);
568 #endif /* AFS_64BIT_CLIENT */
570 osi_FreeSmallSpace(v);
574 if ( cacheDiskType == AFS_FCACHE_TYPE_UFS ) {
575 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
577 osi_Panic("rxfs_fetchInit: osi_AllocLargeSpace for iovecs returned NULL\n");
578 osi_Assert(WriteLocked(&adc->lock));
580 *ops = (struct fetchOps *) &rxfs_fetchUfsOps;
583 afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
584 ICL_TYPE_POINTER, fP, ICL_TYPE_OFFSET,
585 ICL_HANDLE_OFFSET(base), ICL_TYPE_INT32, length);
587 * We need to alloc the iovecs on the heap so that they are "pinned"
588 * rather than declare them on the stack - defect 11272
590 v->iov = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
593 osi_Panic("afs_CacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
594 *ops = (struct fetchOps *) &rxfs_fetchMemOps;
597 *out_length = length;
603 * Routine called on fetch; also tells people waiting for data
604 * that more has arrived.
606 * \param tc Ptr to the Rx connection structure.
607 * \param fP File descriptor for the cache file.
608 * \param abase Base offset to fetch.
609 * \param adc Ptr to the dcache entry for the file, write-locked.
610 * \param avc Ptr to the vcache entry for the file.
611 * \param abytesToXferP Set to the number of bytes to xfer.
612 * NOTE: This parameter is only used if AFS_NOSTATS is not defined.
613 * \param abytesXferredP Set to the number of bytes actually xferred.
614 * NOTE: This parameter is only used if AFS_NOSTATS is not defined.
615 * \param size Amount of data that should be fetched.
616 * \param tsmall Ptr to the afs_FetchOutput structure.
618 * \note Environment: Nothing interesting.
621 afs_CacheFetchProc(register struct afs_conn *tc,
622 register struct osi_file *fP, afs_size_t abase,
623 struct dcache *adc, struct vcache *avc,
624 afs_size_t * abytesToXferP, afs_size_t * abytesXferredP,
626 struct afs_FetchOutput *tsmall)
628 register afs_int32 code;
630 afs_uint32 bytesread, byteswritten;
631 struct fetchOps *ops = NULL;
634 register int offset = 0;
636 AFS_STATCNT(CacheFetchProc);
639 (*abytesToXferP) = 0;
640 (*abytesXferredP) = 0;
641 #endif /* AFS_NOSTATS */
643 adc->validPos = abase;
645 code = rxfs_fetchInit(tc, avc, abase, size, &length, adc, fP, &ops, &rock);
648 code = (*ops->more)(rock, &length, &moredata);
653 * The fetch protocol is extended for the AFS/DFS translator
654 * to allow multiple blocks of data, each with its own length,
655 * to be returned. As long as the top bit is set, there are more
658 * We do not do this for AFS file servers because they sometimes
659 * return large negative numbers as the transfer size.
661 if (avc->f.states & CForeign) {
662 moredata = length & 0x80000000;
663 length &= ~0x80000000;
668 (*abytesToXferP) += length;
669 #endif /* AFS_NOSTATS */
671 #ifdef RX_KERNEL_TRACE
672 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
675 code = (*ops->read)(rock, length, &bytesread);
676 #ifdef RX_KERNEL_TRACE
677 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
681 (*abytesXferredP) += bytesread;
682 #endif /* AFS_NOSTATS */
684 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
685 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
686 ICL_TYPE_INT32, length);
690 code = (*ops->write)(rock, fP, offset, bytesread, &byteswritten);
696 adc->validPos = abase;
697 if (afs_osi_Wakeup(&adc->validPos) == 0)
698 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
699 __FILE__, ICL_TYPE_INT32, __LINE__,
700 ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
706 code = (*ops->close)(rock, avc, adc, tsmall);
707 (*ops->destroy)(&rock, code);