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 *got)
75 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
77 *got = afs_osi_Read(tfile, -1, v->tbuffer, tlen);
79 #if defined(KERNEL_HAVE_UERROR)
80 || (*got != tlen && getuerror())
88 rxfs_storeMemRead(void *r, struct osi_file *tfile, afs_uint32 offset,
89 afs_uint32 tlen, afs_uint32 *bytesread)
92 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
93 struct memCacheEntry *mceP = (struct memCacheEntry *)tfile;
96 code = afs_MemReadvBlk(mceP, offset, v->tiov, v->tnio, tlen);
104 rxfs_storeMemWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
107 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
110 code = rx_Writev(v->call, v->tiov, v->tnio, l);
113 code = rx_Error(v->call);
114 return (code ? code : -33);
116 *byteswritten = code;
121 rxfs_storeUfsWrite(void *r, afs_uint32 l, afs_uint32 *byteswritten)
124 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)r;
127 code = rx_Write(v->call, v->tbuffer, l);
128 /* writing 0 bytes will
129 * push a short packet. Is that really what we want, just because the
130 * data didn't come back from the disk yet? Let's try it and see. */
133 code = rx_Error(v->call);
134 return (code ? code : -33);
136 *byteswritten = code;
141 rxfs_storeStatus(void *rock)
143 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)rock;
145 if (rx_GetRemoteStatus(v->call) & 1)
151 rxfs_storeDestroy(void **r, afs_int32 error)
153 afs_int32 code = error;
154 struct rxfs_storeVariables *v = (struct rxfs_storeVariables *)*r;
158 osi_FreeLargeSpace(v->tbuffer);
160 osi_FreeSmallSpace(v->tiov);
161 osi_FreeSmallSpace(v);
166 struct storeOps rxfs_storeUfsOps = {
167 rxfs_storeUfsPrepare,
175 struct storeOps rxfs_storeMemOps = {
176 rxfs_storeMemPrepare,
184 rxfs_storeInit(struct vcache *avc, struct storeOps **ops, void **rock)
186 struct rxfs_storeVariables *v;
188 v = (struct rxfs_storeVariables *) osi_AllocSmallSpace(sizeof(struct rxfs_storeVariables));
190 osi_Panic("rxfs_storeInit: osi_AllocSmallSpace returned NULL\n");
191 memset(v, 0, sizeof(struct rxfs_storeVariables));
193 v->InStatus.ClientModTime = avc->f.m.Date;
194 v->InStatus.Mask = AFS_SETMODTIME;
196 if (cacheDiskType == AFS_FCACHE_TYPE_UFS) {
197 v->tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
200 ("rxfs_storeInit: osi_AllocLargeSpace for iovecs returned NULL\n");
201 *ops = (struct storeOps *) &rxfs_storeUfsOps;
203 v->tiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
206 ("rxfs_storeInit: osi_AllocSmallSpace for iovecs returned NULL\n");
207 *ops = (struct storeOps *) &rxfs_storeMemOps;
209 /* do this at a higher level now -- it's a parameter */
210 /* for now, only do 'continue from close' code if file fits in one
211 * chunk. Could clearly do better: if only one modified chunk
212 * then can still do this. can do this on *last* modified chunk */
213 tlen = avc->f.m.Length - 1; /* byte position of last byte we'll store */
215 if (AFS_CHUNK(tlen) != 0)
227 * afs_UFSCacheFetchProc
230 * Routine called on fetch; also tells people waiting for data
231 * that more has arrived.
234 * acall : Ptr to the Rx call structure.
235 * afile : File descriptor for the cache file.
236 * abase : Base offset to fetch.
237 * adc : Ptr to the dcache entry for the file, write-locked.
238 * avc : Ptr to the vcache entry for the file.
239 * abytesToXferP : Set to the number of bytes to xfer.
240 * NOTE: This parameter is only used if AFS_NOSTATS
242 * abytesXferredP : Set to the number of bytes actually xferred.
243 * NOTE: This parameter is only used if AFS_NOSTATS
247 * Nothing interesting.
251 afs_UFSCacheFetchProc(register struct rx_call *acall, struct osi_file *afile,
252 afs_size_t abase, struct dcache *adc,
253 struct vcache *avc, afs_size_t * abytesToXferP,
254 afs_size_t * abytesXferredP, afs_int32 lengthFound)
257 register afs_int32 code;
258 register char *tbuffer;
262 AFS_STATCNT(UFS_CacheFetchProc);
263 osi_Assert(WriteLocked(&adc->lock));
264 afile->offset = 0; /* Each time start from the beginning */
265 length = lengthFound;
267 (*abytesToXferP) = 0;
268 (*abytesXferredP) = 0;
269 #endif /* AFS_NOSTATS */
270 tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
271 adc->validPos = abase;
275 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
277 length = ntohl(length);
278 if (code != sizeof(afs_int32)) {
279 osi_FreeLargeSpace(tbuffer);
280 code = rx_Error(acall);
281 return (code ? code : -1); /* try to return code, not -1 */
285 * The fetch protocol is extended for the AFS/DFS translator
286 * to allow multiple blocks of data, each with its own length,
287 * to be returned. As long as the top bit is set, there are more
290 * We do not do this for AFS file servers because they sometimes
291 * return large negative numbers as the transfer size.
293 if (avc->f.states & CForeign) {
294 moredata = length & 0x80000000;
295 length &= ~0x80000000;
300 (*abytesToXferP) += length;
301 #endif /* AFS_NOSTATS */
303 tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
304 #ifdef RX_KERNEL_TRACE
305 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
309 code = rx_Read(acall, tbuffer, tlen);
311 #ifdef RX_KERNEL_TRACE
312 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
316 (*abytesXferredP) += code;
317 #endif /* AFS_NOSTATS */
319 osi_FreeLargeSpace(tbuffer);
320 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
321 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
322 ICL_TYPE_INT32, length);
325 code = afs_osi_Write(afile, -1, tbuffer, tlen);
327 osi_FreeLargeSpace(tbuffer);
332 adc->validPos = abase;
333 if (afs_osi_Wakeup(&adc->validPos) == 0)
334 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
335 __FILE__, ICL_TYPE_INT32, __LINE__,
336 ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
340 osi_FreeLargeSpace(tbuffer);
343 } /* afs_UFSCacheFetchProc */
348 * \param acall Ptr to the Rx call structure involved.
349 * \param fP Ptr to the related file descriptor.
350 * \param alen Size of the file in bytes.
351 * \param avc Ptr to the vcache entry.
352 * \param shouldWake is it "safe" to return early from close() ?
353 * \param abytesToXferP Set to the number of bytes to xfer.
354 * NOTE: This parameter is only used if AFS_NOSTATS is not defined.
355 * \param abytesXferredP Set to the number of bytes actually xferred.
356 * NOTE: This parameter is only used if AFS_NOSTATS is not defined.
358 * \note Environment: Nothing interesting.
361 afs_CacheStoreProc(register struct rx_call *acall,
362 register struct osi_file *fP,
363 register afs_int32 alen, struct vcache *avc,
364 int *shouldWake, afs_size_t * abytesToXferP,
365 afs_size_t * abytesXferredP)
370 struct storeOps *ops;
373 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
374 ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
375 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
376 code = rxfs_storeInit(avc, &ops, &rock);
378 osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed");
380 ((struct rxfs_storeVariables *)rock)->call = acall;
382 AFS_STATCNT(CacheStoreProc);
385 * In this case, alen is *always* the amount of data we'll be trying
388 *(abytesToXferP) = alen;
389 *(abytesXferredP) = 0;
390 #endif /* AFS_NOSTATS */
393 afs_int32 bytesread, byteswritten;
394 code = (*ops->prepare)(rock, alen, &tlen);
398 code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
403 code = (*ops->write)(rock, tlen, &byteswritten);
407 (*abytesXferredP) += byteswritten;
408 #endif /* AFS_NOSTATS */
413 * if file has been locked on server, can allow
416 if (shouldWake && *shouldWake && ((*ops->status)(rock) == 0)) {
417 *shouldWake = 0; /* only do this once */
421 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
422 ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
423 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_INT32, alen);
424 code = (*ops->destroy)(&rock, code);
429 afs_MemCacheFetchProc(register struct rx_call *acall,
430 register struct osi_file *fP, afs_size_t abase,
431 struct dcache *adc, struct vcache *avc,
432 afs_size_t * abytesToXferP, afs_size_t * abytesXferredP,
433 afs_int32 lengthFound)
435 register struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
436 register afs_int32 code;
439 struct iovec *tiov; /* no data copying with iovec */
440 register int tlen, offset = 0;
441 int tnio; /* temp for iovec size */
443 AFS_STATCNT(afs_MemCacheFetchProc);
444 length = lengthFound;
445 afs_Trace4(afs_iclSetp, CM_TRACE_MEMFETCH, ICL_TYPE_POINTER, avc,
446 ICL_TYPE_POINTER, mceP, ICL_TYPE_OFFSET,
447 ICL_HANDLE_OFFSET(abase), ICL_TYPE_INT32, length);
449 (*abytesToXferP) = 0;
450 (*abytesXferredP) = 0;
451 #endif /* AFS_NOSTATS */
453 * We need to alloc the iovecs on the heap so that they are "pinned" rather than
454 * declare them on the stack - defect 11272
457 (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
461 ("afs_MemCacheFetchProc: osi_AllocSmallSpace for iovecs returned NULL\n");
463 adc->validPos = abase;
467 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
468 length = ntohl(length);
470 if (code != sizeof(afs_int32)) {
471 code = rx_Error(acall);
472 osi_FreeSmallSpace(tiov);
473 return (code ? code : -1); /* try to return code, not -1 */
477 * The fetch protocol is extended for the AFS/DFS translator
478 * to allow multiple blocks of data, each with its own length,
479 * to be returned. As long as the top bit is set, there are more
482 * We do not do this for AFS file servers because they sometimes
483 * return large negative numbers as the transfer size.
485 if (avc->f.states & CForeign) {
486 moredata = length & 0x80000000;
487 length &= ~0x80000000;
492 (*abytesToXferP) += length;
493 #endif /* AFS_NOSTATS */
495 tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
497 code = rx_Readv(acall, tiov, &tnio, RX_MAXIOVECS, tlen);
500 (*abytesXferredP) += code;
501 #endif /* AFS_NOSTATS */
503 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
504 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
505 ICL_TYPE_INT32, length);
506 osi_FreeSmallSpace(tiov);
510 afs_MemWritevBlk(mceP, offset, tiov, tnio, tlen);
514 adc->validPos = abase;
515 if (afs_osi_Wakeup(&adc->validPos) == 0)
516 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
517 __FILE__, ICL_TYPE_INT32, __LINE__,
518 ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
522 /* max of two sizes */
523 osi_FreeSmallSpace(tiov);