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
18 #include <afsconfig.h>
19 #include "afs/param.h"
22 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #include "afsincludes.h" /* Afs-based standard headers */
24 #include "afs/afs_stats.h" /* statistics */
25 #include "afs/afs_cbqueue.h"
26 #include "afs/nfsclient.h"
27 #include "afs/afs_osidnlc.h"
28 #include "afs/afs_osi.h"
31 extern char afs_zeros[AFS_ZEROS];
33 /* Imported variables */
34 extern afs_rwlock_t afs_xdcache;
35 extern unsigned char *afs_indexFlags;
36 extern afs_hyper_t *afs_indexTimes; /* Dcache entry Access times */
37 extern afs_hyper_t afs_indexCounter; /* Fake time for marking index */
40 /* Forward declarations */
41 void afs_PrefetchChunk(struct vcache *avc, struct dcache *adc,
42 afs_ucred_t *acred, struct vrequest *areq);
45 afs_MemRead(register struct vcache *avc, struct uio *auio,
46 afs_ucred_t *acred, daddr_t albn, struct buf **abpp,
49 afs_size_t totalLength;
50 afs_size_t transferLength;
52 afs_size_t offset, len, tlen;
54 struct dcache *tdc = 0;
55 afs_int32 error, trybusy = 1;
56 #ifdef AFS_DARWIN80_ENV
60 struct uio *tuiop = &tuio;
66 AFS_STATCNT(afs_MemRead);
70 /* check that we have the latest status info in the vnode cache */
71 if ((code = afs_InitReq(&treq, acred)))
74 code = afs_VerifyVCache(avc, &treq);
76 code = afs_CheckCode(code, &treq, 8); /* failed to get it */
80 #ifndef AFS_VM_RDWR_ENV
81 if (AFS_NFSXLATORREQ(acred)) {
83 (avc, PRSFS_READ, &treq,
84 CHECK_MODE_BITS | CMB_ALLOW_EXEC_AS_READ)) {
85 return afs_CheckCode(EACCES, &treq, 9);
90 #ifndef AFS_DARWIN80_ENV
91 tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
93 totalLength = AFS_UIO_RESID(auio);
94 filePos = AFS_UIO_OFFSET(auio);
95 afs_Trace4(afs_iclSetp, CM_TRACE_READ, ICL_TYPE_POINTER, avc,
96 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_INT32,
97 totalLength, ICL_TYPE_OFFSET,
98 ICL_HANDLE_OFFSET(avc->f.m.Length));
102 ObtainReadLock(&avc->lock);
103 #if defined(AFS_TEXT_ENV) && !defined(AFS_VM_RDWR_ENV)
104 if (avc->flushDV.high == AFS_MAXDV && avc->flushDV.low == AFS_MAXDV) {
105 hset(avc->flushDV, avc->f.m.DataVersion);
113 if (filePos >= avc->f.m.Length) {
115 len = sizeof(afs_zeros); /* and in 0 buffer */
117 #ifdef AFS_DARWIN80_ENV
119 tuiop = afsio_darwin_partialcopy(auio, trimlen);
121 afsio_copy(auio, &tuio, tvec);
123 afsio_trim(&tuio, trimlen);
125 AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
128 while (avc->f.m.Length > 0 && totalLength > 0) {
129 /* read all of the cached info */
130 if (filePos >= avc->f.m.Length)
131 break; /* all done */
134 ReleaseReadLock(&tdc->lock);
137 tdc = afs_FindDCache(avc, filePos);
139 ObtainReadLock(&tdc->lock);
140 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
141 len = tdc->f.chunkBytes - offset;
144 /* a tricky question: does the presence of the DFFetching flag
145 * mean that we're fetching the latest version of the file? No.
146 * The server could update the file as soon as the fetch responsible
147 * for the setting of the DFFetching flag completes.
149 * However, the presence of the DFFetching flag (visible under
150 * a dcache read lock since it is set and cleared only under a
151 * dcache write lock) means that we're fetching as good a version
152 * as was known to this client at the time of the last call to
153 * afs_VerifyVCache, since the latter updates the stat cache's
154 * m.DataVersion field under a vcache write lock, and from the
155 * time that the DFFetching flag goes on in afs_GetDCache (before
156 * the fetch starts), to the time it goes off (after the fetch
157 * completes), afs_GetDCache keeps at least a read lock on the
160 * This means that if the DFFetching flag is set, we can use that
161 * data for any reads that must come from the current version of
162 * the file (current == m.DataVersion).
164 * Another way of looking at this same point is this: if we're
165 * fetching some data and then try do an afs_VerifyVCache, the
166 * VerifyVCache operation will not complete until after the
167 * DFFetching flag is turned off and the dcache entry's f.versionNo
170 * Note, by the way, that if DFFetching is set,
171 * m.DataVersion > f.versionNo (the latter is not updated until
172 * after the fetch completes).
175 ReleaseReadLock(&tdc->lock);
176 afs_PutDCache(tdc); /* before reusing tdc */
178 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 2);
179 ObtainReadLock(&tdc->lock);
180 /* now, first try to start transfer, if we'll need the data. If
181 * data already coming, we don't need to do this, obviously. Type
182 * 2 requests never return a null dcache entry, btw.
184 if (!(tdc->dflags & DFFetching)
185 && !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
186 /* have cache entry, it is not coming in now,
187 * and we'll need new data */
189 if (trybusy && !afs_BBusy()) {
191 /* daemon is not busy */
192 ObtainSharedLock(&tdc->mflock, 665);
193 if (!(tdc->mflags & DFFetchReq)) {
194 /* start the daemon (may already be running, however) */
195 UpgradeSToWLock(&tdc->mflock, 666);
196 tdc->mflags |= DFFetchReq;
197 bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
198 (afs_size_t) filePos, (afs_size_t) 0,
201 tdc->mflags &= ~DFFetchReq;
202 trybusy = 0; /* Avoid bkg daemon since they're too busy */
203 ReleaseWriteLock(&tdc->mflock);
206 ConvertWToSLock(&tdc->mflock);
207 /* don't use bp pointer! */
210 ConvertSToRLock(&tdc->mflock);
211 while (!code && tdc->mflags & DFFetchReq) {
212 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT,
213 ICL_TYPE_STRING, __FILE__, ICL_TYPE_INT32,
214 __LINE__, ICL_TYPE_POINTER, tdc,
215 ICL_TYPE_INT32, tdc->dflags);
216 /* don't need waiting flag on this one */
217 ReleaseReadLock(&tdc->mflock);
218 ReleaseReadLock(&tdc->lock);
219 ReleaseReadLock(&avc->lock);
220 code = afs_osi_SleepSig(&tdc->validPos);
221 ObtainReadLock(&avc->lock);
222 ObtainReadLock(&tdc->lock);
223 ObtainReadLock(&tdc->mflock);
225 ReleaseReadLock(&tdc->mflock);
232 /* now data may have started flowing in (if DFFetching is on). If
233 * data is now streaming in, then wait for some interesting stuff.
236 while (!code && (tdc->dflags & DFFetching)
237 && tdc->validPos <= filePos) {
238 /* too early: wait for DFFetching flag to vanish,
239 * or data to appear */
240 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT, ICL_TYPE_STRING,
241 __FILE__, ICL_TYPE_INT32, __LINE__,
242 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
244 ReleaseReadLock(&tdc->lock);
245 ReleaseReadLock(&avc->lock);
246 code = afs_osi_SleepSig(&tdc->validPos);
247 ObtainReadLock(&avc->lock);
248 ObtainReadLock(&tdc->lock);
254 /* fetching flag gone, data is here, or we never tried
255 * (BBusy for instance) */
256 if (tdc->dflags & DFFetching) {
257 /* still fetching, some new data is here:
258 * compute length and offset */
259 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
260 len = tdc->validPos - filePos;
262 /* no longer fetching, verify data version
263 * (avoid new GetDCache call) */
264 if (hsame(avc->f.m.DataVersion, tdc->f.versionNo)
265 && ((len = tdc->validPos - filePos) > 0)) {
266 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
268 /* don't have current data, so get it below */
269 afs_Trace3(afs_iclSetp, CM_TRACE_VERSIONNO,
270 ICL_TYPE_INT64, ICL_HANDLE_OFFSET(filePos),
271 ICL_TYPE_HYPER, &avc->f.m.DataVersion,
272 ICL_TYPE_HYPER, &tdc->f.versionNo);
273 ReleaseReadLock(&tdc->lock);
280 /* If we get, it was not possible to start the
281 * background daemon. With flag == 1 afs_GetDCache
282 * does the FetchData rpc synchronously.
284 ReleaseReadLock(&avc->lock);
285 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 1);
286 ObtainReadLock(&avc->lock);
288 ObtainReadLock(&tdc->lock);
292 afs_Trace3(afs_iclSetp, CM_TRACE_VNODEREAD, ICL_TYPE_POINTER, tdc,
293 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(offset),
294 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(len));
306 if (len > totalLength)
307 len = totalLength; /* will read len bytes */
308 if (len <= 0) { /* shouldn't get here if DFFetching is on */
309 /* read past the end of a chunk, may not be at next chunk yet, and yet
310 * also not at eof, so may have to supply fake zeros */
311 len = AFS_CHUNKTOSIZE(tdc->f.chunk) - offset; /* bytes left in chunk addr space */
312 if (len > totalLength)
313 len = totalLength; /* and still within xfr request */
314 tlen = avc->f.m.Length - offset; /* and still within file */
318 len = sizeof(afs_zeros); /* and in 0 buffer */
319 #ifdef AFS_DARWIN80_ENV
321 tuiop = afsio_darwin_partialcopy(auio, trimlen);
323 afsio_copy(auio, &tuio, tvec);
325 afsio_trim(&tuio, trimlen);
327 AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
333 /* get the data from the mem cache */
335 /* mung uio structure to be right for this transfer */
336 #ifdef AFS_DARWIN80_ENV
338 tuiop = afsio_darwin_partialcopy(auio, trimlen);
339 uio_setoffset(tuiop, offset);
341 afsio_copy(auio, &tuio, tvec);
343 afsio_trim(&tuio, trimlen);
344 tuio.afsio_offset = offset;
347 code = afs_MemReadUIO(&tdc->f.inode, tuiop);
354 /* otherwise we've read some, fixup length, etc and continue with next seg */
355 len = len - AFS_UIO_RESID(tuiop); /* compute amount really transferred */
357 afsio_skip(auio, trimlen); /* update input uio structure */
359 transferLength += len;
363 break; /* surprise eof */
364 #ifdef AFS_DARWIN80_ENV
370 } /* the whole while loop */
375 * tdc->lock(R) if tdc
378 /* if we make it here with tdc non-zero, then it is the last chunk we
379 * dealt with, and we have to release it when we're done. We hold on
380 * to it in case we need to do a prefetch.
383 ReleaseReadLock(&tdc->lock);
384 /* try to queue prefetch, if needed */
386 #ifndef AFS_VM_RDWR_ENV
392 afs_PrefetchChunk(avc, tdc, acred, &treq);
397 ReleaseReadLock(&avc->lock);
398 #ifdef AFS_DARWIN80_ENV
402 osi_FreeSmallSpace(tvec);
404 error = afs_CheckCode(error, &treq, 10);
408 /* called with the dcache entry triggering the fetch, the vcache entry involved,
409 * and a vrequest for the read call. Marks the dcache entry as having already
410 * triggered a prefetch, starts the prefetch going and sets the DFFetchReq
411 * flag in the prefetched block, so that the next call to read knows to wait
412 * for the daemon to start doing things.
414 * This function must be called with the vnode at least read-locked, and
415 * no locks on the dcache, because it plays around with dcache entries.
418 afs_PrefetchChunk(struct vcache *avc, struct dcache *adc,
419 afs_ucred_t *acred, struct vrequest *areq)
421 register struct dcache *tdc;
423 afs_size_t j1, j2; /* junk vbls for GetDCache to trash */
425 offset = adc->f.chunk + 1; /* next chunk we'll need */
426 offset = AFS_CHUNKTOBASE(offset); /* base of next chunk */
427 ObtainReadLock(&adc->lock);
428 ObtainSharedLock(&adc->mflock, 662);
429 if (offset < avc->f.m.Length && !(adc->mflags & DFNextStarted)
433 UpgradeSToWLock(&adc->mflock, 663);
434 adc->mflags |= DFNextStarted; /* we've tried to prefetch for this guy */
435 ReleaseWriteLock(&adc->mflock);
436 ReleaseReadLock(&adc->lock);
438 tdc = afs_GetDCache(avc, offset, areq, &j1, &j2, 2); /* type 2 never returns 0 */
439 #ifdef AFS_DISCON_ENV
441 * In disconnected mode, type 2 can return 0 because it doesn't
442 * make any sense to allocate a dcache we can never fill
446 #endif /* AFS_DISCON_ENV */
448 ObtainSharedLock(&tdc->mflock, 651);
449 if (!(tdc->mflags & DFFetchReq)) {
450 /* ask the daemon to do the work */
451 UpgradeSToWLock(&tdc->mflock, 652);
452 tdc->mflags |= DFFetchReq; /* guaranteed to be cleared by BKG or GetDCache */
453 /* last parm (1) tells bkg daemon to do an afs_PutDCache when it is done,
454 * since we don't want to wait for it to finish before doing so ourselves.
456 bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
457 (afs_size_t) offset, (afs_size_t) 1, tdc);
459 /* Bkg table full; just abort non-important prefetching to avoid deadlocks */
460 tdc->mflags &= ~DFFetchReq;
461 ReleaseWriteLock(&tdc->mflock);
465 * DCLOCKXXX: This is a little sketchy, since someone else
466 * could have already started a prefetch.. In practice,
467 * this probably doesn't matter; at most it would cause an
468 * extra slot in the BKG table to be used up when someone
469 * prefetches this for the second time.
471 ObtainReadLock(&adc->lock);
472 ObtainWriteLock(&adc->mflock, 664);
473 adc->mflags &= ~DFNextStarted;
474 ReleaseWriteLock(&adc->mflock);
475 ReleaseReadLock(&adc->lock);
477 ReleaseWriteLock(&tdc->mflock);
480 ReleaseSharedLock(&tdc->mflock);
484 ReleaseSharedLock(&adc->mflock);
485 ReleaseReadLock(&adc->lock);
490 afs_UFSRead(register struct vcache *avc, struct uio *auio,
491 afs_ucred_t *acred, daddr_t albn, struct buf **abpp,
494 afs_size_t totalLength;
495 afs_size_t transferLength;
497 afs_size_t offset, len, tlen;
499 struct dcache *tdc = 0;
501 #ifdef AFS_DARWIN80_ENV
505 struct uio *tuiop = &tuio;
508 struct osi_file *tfile;
511 struct vrequest treq;
513 AFS_STATCNT(afs_UFSRead);
514 if (avc && avc->vc_error)
519 /* check that we have the latest status info in the vnode cache */
520 if ((code = afs_InitReq(&treq, acred)))
524 osi_Panic("null avc in afs_UFSRead");
526 code = afs_VerifyVCache(avc, &treq);
528 code = afs_CheckCode(code, &treq, 11); /* failed to get it */
534 #ifndef AFS_VM_RDWR_ENV
535 if (AFS_NFSXLATORREQ(acred)) {
537 (avc, PRSFS_READ, &treq,
538 CHECK_MODE_BITS | CMB_ALLOW_EXEC_AS_READ)) {
540 return afs_CheckCode(EACCES, &treq, 12);
545 #ifndef AFS_DARWIN80_ENV
546 tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
548 totalLength = AFS_UIO_RESID(auio);
549 filePos = AFS_UIO_OFFSET(auio);
550 afs_Trace4(afs_iclSetp, CM_TRACE_READ, ICL_TYPE_POINTER, avc,
551 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_INT32,
552 totalLength, ICL_TYPE_OFFSET,
553 ICL_HANDLE_OFFSET(avc->f.m.Length));
557 ObtainReadLock(&avc->lock);
558 #if defined(AFS_TEXT_ENV) && !defined(AFS_VM_RDWR_ENV)
559 if (avc->flushDV.high == AFS_MAXDV && avc->flushDV.low == AFS_MAXDV) {
560 hset(avc->flushDV, avc->f.m.DataVersion);
564 if (filePos >= avc->f.m.Length) {
566 len = sizeof(afs_zeros); /* and in 0 buffer */
568 #ifdef AFS_DARWIN80_ENV
570 tuiop = afsio_darwin_partialcopy(auio, trimlen);
572 afsio_copy(auio, &tuio, tvec);
574 afsio_trim(&tuio, trimlen);
576 AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
579 while (avc->f.m.Length > 0 && totalLength > 0) {
580 /* read all of the cached info */
581 if (filePos >= avc->f.m.Length)
582 break; /* all done */
585 ReleaseReadLock(&tdc->lock);
588 tdc = afs_FindDCache(avc, filePos);
590 ObtainReadLock(&tdc->lock);
591 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
592 len = tdc->validPos - filePos;
595 /* a tricky question: does the presence of the DFFetching flag
596 * mean that we're fetching the latest version of the file? No.
597 * The server could update the file as soon as the fetch responsible
598 * for the setting of the DFFetching flag completes.
600 * However, the presence of the DFFetching flag (visible under
601 * a dcache read lock since it is set and cleared only under a
602 * dcache write lock) means that we're fetching as good a version
603 * as was known to this client at the time of the last call to
604 * afs_VerifyVCache, since the latter updates the stat cache's
605 * m.DataVersion field under a vcache write lock, and from the
606 * time that the DFFetching flag goes on in afs_GetDCache (before
607 * the fetch starts), to the time it goes off (after the fetch
608 * completes), afs_GetDCache keeps at least a read lock on the
611 * This means that if the DFFetching flag is set, we can use that
612 * data for any reads that must come from the current version of
613 * the file (current == m.DataVersion).
615 * Another way of looking at this same point is this: if we're
616 * fetching some data and then try do an afs_VerifyVCache, the
617 * VerifyVCache operation will not complete until after the
618 * DFFetching flag is turned off and the dcache entry's f.versionNo
621 * Note, by the way, that if DFFetching is set,
622 * m.DataVersion > f.versionNo (the latter is not updated until
623 * after the fetch completes).
626 ReleaseReadLock(&tdc->lock);
627 afs_PutDCache(tdc); /* before reusing tdc */
629 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 2);
630 #ifdef AFS_DISCON_ENV
632 printf("Network down in afs_read");
636 #endif /* AFS_DISCON_ENV */
638 ObtainReadLock(&tdc->lock);
639 /* now, first try to start transfer, if we'll need the data. If
640 * data already coming, we don't need to do this, obviously. Type
641 * 2 requests never return a null dcache entry, btw. */
642 if (!(tdc->dflags & DFFetching)
643 && !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
644 /* have cache entry, it is not coming in now, and we'll need new data */
646 if (trybusy && !afs_BBusy()) {
648 /* daemon is not busy */
649 ObtainSharedLock(&tdc->mflock, 667);
650 if (!(tdc->mflags & DFFetchReq)) {
651 UpgradeSToWLock(&tdc->mflock, 668);
652 tdc->mflags |= DFFetchReq;
653 bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
654 (afs_size_t) filePos, (afs_size_t) 0,
657 /* Bkg table full; retry deadlocks */
658 tdc->mflags &= ~DFFetchReq;
659 trybusy = 0; /* Avoid bkg daemon since they're too busy */
660 ReleaseWriteLock(&tdc->mflock);
663 ConvertWToSLock(&tdc->mflock);
666 ConvertSToRLock(&tdc->mflock);
667 while (!code && tdc->mflags & DFFetchReq) {
668 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT,
669 ICL_TYPE_STRING, __FILE__, ICL_TYPE_INT32,
670 __LINE__, ICL_TYPE_POINTER, tdc,
671 ICL_TYPE_INT32, tdc->dflags);
672 /* don't need waiting flag on this one */
673 ReleaseReadLock(&tdc->mflock);
674 ReleaseReadLock(&tdc->lock);
675 ReleaseReadLock(&avc->lock);
676 code = afs_osi_SleepSig(&tdc->validPos);
677 ObtainReadLock(&avc->lock);
678 ObtainReadLock(&tdc->lock);
679 ObtainReadLock(&tdc->mflock);
681 ReleaseReadLock(&tdc->mflock);
688 /* now data may have started flowing in (if DFFetching is on). If
689 * data is now streaming in, then wait for some interesting stuff.
692 while (!code && (tdc->dflags & DFFetching)
693 && tdc->validPos <= filePos) {
694 /* too early: wait for DFFetching flag to vanish,
695 * or data to appear */
696 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT, ICL_TYPE_STRING,
697 __FILE__, ICL_TYPE_INT32, __LINE__,
698 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
700 ReleaseReadLock(&tdc->lock);
701 ReleaseReadLock(&avc->lock);
702 code = afs_osi_SleepSig(&tdc->validPos);
703 ObtainReadLock(&avc->lock);
704 ObtainReadLock(&tdc->lock);
710 /* fetching flag gone, data is here, or we never tried
711 * (BBusy for instance) */
712 if (tdc->dflags & DFFetching) {
713 /* still fetching, some new data is here:
714 * compute length and offset */
715 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
716 len = tdc->validPos - filePos;
718 /* no longer fetching, verify data version (avoid new
720 if (hsame(avc->f.m.DataVersion, tdc->f.versionNo)
721 && ((len = tdc->validPos - filePos) > 0)) {
722 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
724 /* don't have current data, so get it below */
725 afs_Trace3(afs_iclSetp, CM_TRACE_VERSIONNO,
726 ICL_TYPE_INT64, ICL_HANDLE_OFFSET(filePos),
727 ICL_TYPE_HYPER, &avc->f.m.DataVersion,
728 ICL_TYPE_HYPER, &tdc->f.versionNo);
729 ReleaseReadLock(&tdc->lock);
736 /* If we get, it was not possible to start the
737 * background daemon. With flag == 1 afs_GetDCache
738 * does the FetchData rpc synchronously.
740 ReleaseReadLock(&avc->lock);
741 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 1);
742 ObtainReadLock(&avc->lock);
744 ObtainReadLock(&tdc->lock);
752 len = tdc->validPos - filePos;
753 afs_Trace3(afs_iclSetp, CM_TRACE_VNODEREAD, ICL_TYPE_POINTER, tdc,
754 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(offset),
755 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(len));
756 if (len > totalLength)
757 len = totalLength; /* will read len bytes */
758 if (len <= 0) { /* shouldn't get here if DFFetching is on */
759 afs_Trace4(afs_iclSetp, CM_TRACE_VNODEREAD2, ICL_TYPE_POINTER,
760 tdc, ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(tdc->validPos),
761 ICL_TYPE_INT32, tdc->f.chunkBytes, ICL_TYPE_INT32,
763 /* read past the end of a chunk, may not be at next chunk yet, and yet
764 * also not at eof, so may have to supply fake zeros */
765 len = AFS_CHUNKTOSIZE(tdc->f.chunk) - offset; /* bytes left in chunk addr space */
766 if (len > totalLength)
767 len = totalLength; /* and still within xfr request */
768 tlen = avc->f.m.Length - offset; /* and still within file */
772 len = sizeof(afs_zeros); /* and in 0 buffer */
773 #ifdef AFS_DARWIN80_ENV
775 tuiop = afsio_darwin_partialcopy(auio, trimlen);
777 afsio_copy(auio, &tuio, tvec);
779 afsio_trim(&tuio, trimlen);
781 AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
787 /* get the data from the file */
788 tfile = (struct osi_file *)osi_UFSOpen(&tdc->f.inode);
789 #ifdef AFS_DARWIN80_ENV
791 tuiop = afsio_darwin_partialcopy(auio, trimlen);
792 uio_setoffset(tuiop, offset);
794 /* mung uio structure to be right for this transfer */
795 afsio_copy(auio, &tuio, tvec);
797 afsio_trim(&tuio, trimlen);
798 tuio.afsio_offset = offset;
801 #if defined(AFS_AIX41_ENV)
804 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, &tuio, NULL, NULL,
805 NULL, afs_osi_credp);
807 #elif defined(AFS_AIX32_ENV)
809 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, &tuio, NULL, NULL);
810 /* Flush all JFS pages now for big performance gain in big file cases
811 * If we do something like this, must check to be sure that AFS file
812 * isn't mmapped... see afs_gn_map() for why.
815 if (tfile->vnode->v_gnode && tfile->vnode->v_gnode->gn_seg) {
816 many different ways to do similar things:
817 so far, the best performing one is #2, but #1 might match it if we
818 straighten out the confusion regarding which pages to flush. It
820 1. vm_flushp(tfile->vnode->v_gnode->gn_seg, 0, len/PAGESIZE - 1);
821 2. vm_releasep(tfile->vnode->v_gnode->gn_seg, offset/PAGESIZE,
822 (len + PAGESIZE-1)/PAGESIZE);
823 3. vms_inactive(tfile->vnode->v_gnode->gn_seg) Doesn't work correctly
824 4. vms_delete(tfile->vnode->v_gnode->gn_seg) probably also fails
825 tfile->vnode->v_gnode->gn_seg = NULL;
829 Unfortunately, this seems to cause frequent "cache corruption" episodes.
830 vm_releasep(tfile->vnode->v_gnode->gn_seg, offset/PAGESIZE,
831 (len + PAGESIZE-1)/PAGESIZE);
834 #elif defined(AFS_AIX_ENV)
836 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, (off_t) & offset,
837 &tuio, NULL, NULL, -1);
838 #elif defined(AFS_SUN5_ENV)
840 #ifdef AFS_SUN510_ENV
844 VOP_RWLOCK(tfile->vnode, 0, &ct);
845 code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp, &ct);
846 VOP_RWUNLOCK(tfile->vnode, 0, &ct);
849 VOP_RWLOCK(tfile->vnode, 0);
850 code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp);
851 VOP_RWUNLOCK(tfile->vnode, 0);
854 #elif defined(AFS_SGI_ENV)
856 AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_READ);
857 AFS_VOP_READ(tfile->vnode, &tuio, IO_ISLOCKED, afs_osi_credp,
859 AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_READ);
861 #elif defined(AFS_HPUX100_ENV)
863 code = VOP_RDWR(tfile->vnode, &tuio, UIO_READ, 0, afs_osi_credp);
865 #elif defined(AFS_LINUX20_ENV)
867 code = osi_rdwr(tfile, &tuio, UIO_READ);
869 #elif defined(AFS_DARWIN80_ENV)
871 code = VNOP_READ(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
873 #elif defined(AFS_DARWIN_ENV)
875 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
876 code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp);
877 VOP_UNLOCK(tfile->vnode, 0, current_proc());
879 #elif defined(AFS_FBSD80_ENV)
881 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
882 code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp);
883 VOP_UNLOCK(tfile->vnode, 0);
885 #elif defined(AFS_FBSD50_ENV)
887 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curthread);
888 code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp);
889 VOP_UNLOCK(tfile->vnode, 0, curthread);
891 #elif defined(AFS_XBSD_ENV)
893 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
894 code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp);
895 VOP_UNLOCK(tfile->vnode, 0, curproc);
898 code = VOP_RDWR(tfile->vnode, &tuio, UIO_READ, 0, afs_osi_credp);
907 /* otherwise we've read some, fixup length, etc and continue with next seg */
908 len = len - AFS_UIO_RESID(tuiop); /* compute amount really transferred */
910 afsio_skip(auio, trimlen); /* update input uio structure */
912 transferLength += len;
915 break; /* surprise eof */
916 #ifdef AFS_DARWIN80_ENV
924 /* if we make it here with tdc non-zero, then it is the last chunk we
925 * dealt with, and we have to release it when we're done. We hold on
926 * to it in case we need to do a prefetch, obviously.
929 ReleaseReadLock(&tdc->lock);
930 #if !defined(AFS_VM_RDWR_ENV)
931 /* try to queue prefetch, if needed */
933 if (!(tdc->mflags & DFNextStarted))
934 afs_PrefetchChunk(avc, tdc, acred, &treq);
940 ReleaseReadLock(&avc->lock);
942 #ifdef AFS_DARWIN80_ENV
946 osi_FreeSmallSpace(tvec);
949 error = afs_CheckCode(error, &treq, 13);