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)
231 * afs_UFSCacheFetchProc
234 * Routine called on fetch; also tells people waiting for data
235 * that more has arrived.
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
246 * abytesXferredP : Set to the number of bytes actually xferred.
247 * NOTE: This parameter is only used if AFS_NOSTATS
251 * Nothing interesting.
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)
261 register afs_int32 code;
262 register char *tbuffer;
266 AFS_STATCNT(UFS_CacheFetchProc);
267 osi_Assert(WriteLocked(&adc->lock));
268 afile->offset = 0; /* Each time start from the beginning */
269 length = lengthFound;
271 (*abytesToXferP) = 0;
272 (*abytesXferredP) = 0;
273 #endif /* AFS_NOSTATS */
274 tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
275 adc->validPos = abase;
279 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
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 */
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
294 * We do not do this for AFS file servers because they sometimes
295 * return large negative numbers as the transfer size.
297 if (avc->f.states & CForeign) {
298 moredata = length & 0x80000000;
299 length &= ~0x80000000;
304 (*abytesToXferP) += length;
305 #endif /* AFS_NOSTATS */
307 tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
308 #ifdef RX_KERNEL_TRACE
309 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
313 code = rx_Read(acall, tbuffer, tlen);
315 #ifdef RX_KERNEL_TRACE
316 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
320 (*abytesXferredP) += code;
321 #endif /* AFS_NOSTATS */
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);
329 code = afs_osi_Write(afile, -1, tbuffer, tlen);
331 osi_FreeLargeSpace(tbuffer);
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,
344 osi_FreeLargeSpace(tbuffer);
347 } /* afs_UFSCacheFetchProc */
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.
362 * \note Environment: Nothing interesting.
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)
374 struct storeOps *ops;
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);
382 osi_Panic("afs_CacheStoreProc: rxfs_storeInit failed");
384 ((struct rxfs_storeVariables *)rock)->call = acall;
386 AFS_STATCNT(CacheStoreProc);
389 * In this case, alen is *always* the amount of data we'll be trying
392 *(abytesToXferP) = alen;
393 *(abytesXferredP) = 0;
394 #endif /* AFS_NOSTATS */
397 afs_int32 bytesread, byteswritten;
398 code = (*ops->prepare)(rock, alen, &tlen);
402 code = (*ops->read)(rock, fP, offset, tlen, &bytesread);
407 code = (*ops->write)(rock, tlen, &byteswritten);
411 (*abytesXferredP) += byteswritten;
412 #endif /* AFS_NOSTATS */
417 * if file has been locked on server, can allow
420 if (shouldWake && *shouldWake && ((*ops->status)(rock) == 0)) {
421 *shouldWake = 0; /* only do this once */
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);
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)
439 register struct memCacheEntry *mceP = (struct memCacheEntry *)fP;
440 register afs_int32 code;
443 struct iovec *tiov; /* no data copying with iovec */
444 register int tlen, offset = 0;
445 int tnio; /* temp for iovec size */
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);
453 (*abytesToXferP) = 0;
454 (*abytesXferredP) = 0;
455 #endif /* AFS_NOSTATS */
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
461 (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec) *
465 ("afs_MemCacheFetchProc: osi_AllocSmallSpace returned NULL\n");
467 adc->validPos = abase;
471 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
472 length = ntohl(length);
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 */
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
486 * We do not do this for AFS file servers because they sometimes
487 * return large negative numbers as the transfer size.
489 if (avc->f.states & CForeign) {
490 moredata = length & 0x80000000;
491 length &= ~0x80000000;
496 (*abytesToXferP) += length;
497 #endif /* AFS_NOSTATS */
499 tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
501 code = rx_Readv(acall, tiov, &tnio, RX_MAXIOVECS, tlen);
504 (*abytesXferredP) += code;
505 #endif /* AFS_NOSTATS */
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);
514 afs_MemWritevBlk(mceP, offset, tiov, tnio, 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,
526 /* max of two sizes */
527 osi_FreeSmallSpace(tiov);