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_read(struct vcache *avc, struct uio *auio, afs_ucred_t *acred,
48 afs_size_t totalLength;
50 afs_size_t offset, tlen;
53 struct dcache *tdc = 0;
54 afs_int32 error, trybusy = 1;
55 struct uio *tuiop = NULL;
57 struct vrequest *treq = NULL;
59 AFS_STATCNT(afs_read);
66 /* check that we have the latest status info in the vnode cache */
67 if ((code = afs_CreateReq(&treq, acred)))
72 osi_Panic("null avc in afs_GenericRead");
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 code = afs_CheckCode(EACCES, treq, 9);
91 totalLength = AFS_UIO_RESID(auio);
92 filePos = AFS_UIO_OFFSET(auio);
93 afs_Trace4(afs_iclSetp, CM_TRACE_READ, ICL_TYPE_POINTER, avc,
94 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_INT32,
95 totalLength, ICL_TYPE_OFFSET,
96 ICL_HANDLE_OFFSET(avc->f.m.Length));
99 ObtainReadLock(&avc->lock);
100 #if defined(AFS_TEXT_ENV) && !defined(AFS_VM_RDWR_ENV)
101 if (avc->flushDV.high == AFS_MAXDV && avc->flushDV.low == AFS_MAXDV) {
102 hset(avc->flushDV, avc->f.m.DataVersion);
111 /* This bit is bogus. We're checking to see if the read goes past the
112 * end of the file. If so, we should be zeroing out all of the buffers
113 * that the client has passed into us (there is a danger that we may leak
114 * kernel memory if we do not). However, this behaviour is disabled by
115 * not setting len before this segment runs, and by setting len to 0
116 * immediately we enter it. In addition, we also need to check for a read
117 * which partially goes off the end of the file in the while loop below.
120 if (filePos >= avc->f.m.Length) {
122 len = sizeof(afs_zeros); /* and in 0 buffer */
125 tuiop = afsio_partialcopy(auio, trimlen);
126 AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
129 while (avc->f.m.Length > 0 && totalLength > 0) {
130 /* read all of the cached info */
131 if (filePos >= avc->f.m.Length)
132 break; /* all done */
135 ReleaseReadLock(&tdc->lock);
138 tdc = afs_FindDCache(avc, filePos);
140 ObtainReadLock(&tdc->lock);
141 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
142 len = tdc->validPos - filePos;
145 /* a tricky question: does the presence of the DFFetching flag
146 * mean that we're fetching the latest version of the file? No.
147 * The server could update the file as soon as the fetch responsible
148 * for the setting of the DFFetching flag completes.
150 * However, the presence of the DFFetching flag (visible under
151 * a dcache read lock since it is set and cleared only under a
152 * dcache write lock) means that we're fetching as good a version
153 * as was known to this client at the time of the last call to
154 * afs_VerifyVCache, since the latter updates the stat cache's
155 * m.DataVersion field under a vcache write lock, and from the
156 * time that the DFFetching flag goes on in afs_GetDCache (before
157 * the fetch starts), to the time it goes off (after the fetch
158 * completes), afs_GetDCache keeps at least a read lock on the
161 * This means that if the DFFetching flag is set, we can use that
162 * data for any reads that must come from the current version of
163 * the file (current == m.DataVersion).
165 * Another way of looking at this same point is this: if we're
166 * fetching some data and then try do an afs_VerifyVCache, the
167 * VerifyVCache operation will not complete until after the
168 * DFFetching flag is turned off and the dcache entry's f.versionNo
171 * Note, by the way, that if DFFetching is set,
172 * m.DataVersion > f.versionNo (the latter is not updated until
173 * after the fetch completes).
176 ReleaseReadLock(&tdc->lock);
177 afs_PutDCache(tdc); /* before reusing tdc */
179 tdc = afs_GetDCache(avc, filePos, treq, &offset, &len, 2);
185 ObtainReadLock(&tdc->lock);
186 /* now, first try to start transfer, if we'll need the data. If
187 * data already coming, we don't need to do this, obviously. Type
188 * 2 requests never return a null dcache entry, btw.
190 if (!(tdc->dflags & DFFetching)
191 && !afs_IsDCacheFresh(tdc, avc)) {
192 /* have cache entry, it is not coming in now,
193 * and we'll need new data */
195 if (trybusy && !afs_BBusy()) {
197 /* daemon is not busy */
198 ObtainSharedLock(&tdc->mflock, 665);
199 if (!(tdc->mflags & DFFetchReq)) {
200 /* start the daemon (may already be running, however) */
201 UpgradeSToWLock(&tdc->mflock, 666);
202 tdc->mflags |= DFFetchReq;
203 bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
204 (afs_size_t) filePos, (afs_size_t) 0,
207 /* Bkg table full; retry deadlocks */
208 tdc->mflags &= ~DFFetchReq;
209 trybusy = 0; /* Avoid bkg daemon since they're too busy */
210 ReleaseWriteLock(&tdc->mflock);
213 ConvertWToSLock(&tdc->mflock);
214 /* don't use bp pointer! */
217 ConvertSToRLock(&tdc->mflock);
218 while (!code && tdc->mflags & DFFetchReq) {
219 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT,
220 ICL_TYPE_STRING, __FILE__, ICL_TYPE_INT32,
221 __LINE__, ICL_TYPE_POINTER, tdc,
222 ICL_TYPE_INT32, tdc->dflags);
223 /* don't need waiting flag on this one */
224 ReleaseReadLock(&tdc->mflock);
225 ReleaseReadLock(&tdc->lock);
226 ReleaseReadLock(&avc->lock);
227 code = afs_osi_SleepSig(&tdc->validPos);
228 ObtainReadLock(&avc->lock);
229 ObtainReadLock(&tdc->lock);
230 ObtainReadLock(&tdc->mflock);
232 ReleaseReadLock(&tdc->mflock);
239 /* now data may have started flowing in (if DFFetching is on). If
240 * data is now streaming in, then wait for some interesting stuff.
243 while (!code && (tdc->dflags & DFFetching)
244 && tdc->validPos <= filePos) {
245 /* too early: wait for DFFetching flag to vanish,
246 * or data to appear */
247 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT, ICL_TYPE_STRING,
248 __FILE__, ICL_TYPE_INT32, __LINE__,
249 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
251 ReleaseReadLock(&tdc->lock);
252 ReleaseReadLock(&avc->lock);
253 code = afs_osi_SleepSig(&tdc->validPos);
254 ObtainReadLock(&avc->lock);
255 ObtainReadLock(&tdc->lock);
261 /* fetching flag gone, data is here, or we never tried
262 * (BBusy for instance) */
263 if (tdc->dflags & DFFetching) {
264 /* still fetching, some new data is here:
265 * compute length and offset */
266 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
267 len = tdc->validPos - filePos;
269 /* no longer fetching, verify data version
270 * (avoid new GetDCache call) */
271 if (afs_IsDCacheFresh(tdc, avc)
272 && ((len = tdc->validPos - filePos) > 0)) {
273 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
275 /* don't have current data, so get it below */
276 afs_Trace3(afs_iclSetp, CM_TRACE_VERSIONNO,
277 ICL_TYPE_INT64, ICL_HANDLE_OFFSET(filePos),
278 ICL_TYPE_HYPER, &avc->f.m.DataVersion,
279 ICL_TYPE_HYPER, &tdc->f.versionNo);
280 ReleaseReadLock(&tdc->lock);
287 /* If we get, it was not possible to start the
288 * background daemon. With flag == 1 afs_GetDCache
289 * does the FetchData rpc synchronously.
291 ReleaseReadLock(&avc->lock);
292 tdc = afs_GetDCache(avc, filePos, treq, &offset, &len, 1);
293 ObtainReadLock(&avc->lock);
295 ObtainReadLock(&tdc->lock);
299 afs_Trace3(afs_iclSetp, CM_TRACE_VNODEREAD, ICL_TYPE_POINTER, tdc,
300 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(offset),
301 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(len));
313 if (len > totalLength)
314 len = totalLength; /* will read len bytes */
315 if (len <= 0) { /* shouldn't get here if DFFetching is on */
316 /* read past the end of a chunk, may not be at next chunk yet, and yet
317 * also not at eof, so may have to supply fake zeros */
318 len = AFS_CHUNKTOSIZE(tdc->f.chunk) - offset; /* bytes left in chunk addr space */
319 if (len > totalLength)
320 len = totalLength; /* and still within xfr request */
321 tlen = avc->f.m.Length - offset; /* and still within file */
325 len = sizeof(afs_zeros); /* and in 0 buffer */
327 tuiop = afsio_partialcopy(auio, trimlen);
328 AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
334 /* get the data from the cache */
336 /* mung uio structure to be right for this transfer */
338 tuiop = afsio_partialcopy(auio, trimlen);
339 AFS_UIO_SETOFFSET(tuiop, offset);
341 code = (*(afs_cacheType->vreadUIO))(&tdc->f.inode, tuiop);
348 /* otherwise we've read some, fixup length, etc and continue with next seg */
349 len = len - AFS_UIO_RESID(tuiop); /* compute amount really transferred */
351 afsio_skip(auio, trimlen); /* update input uio structure */
356 break; /* surprise eof */
361 } /* the whole while loop */
366 * tdc->lock(R) if tdc
369 /* if we make it here with tdc non-zero, then it is the last chunk we
370 * dealt with, and we have to release it when we're done. We hold on
371 * to it in case we need to do a prefetch.
374 ReleaseReadLock(&tdc->lock);
375 #if !defined(AFS_VM_RDWR_ENV)
376 /* try to queue prefetch, if needed */
378 if (!(tdc->mflags &DFNextStarted))
379 afs_PrefetchChunk(avc, tdc, acred, treq);
385 ReleaseReadLock(&avc->lock);
387 code = afs_CheckCode(error, treq, 10);
394 afs_DestroyReq(treq);
398 /* called with the dcache entry triggering the fetch, the vcache entry involved,
399 * and a vrequest for the read call. Marks the dcache entry as having already
400 * triggered a prefetch, starts the prefetch going and sets the DFFetchReq
401 * flag in the prefetched block, so that the next call to read knows to wait
402 * for the daemon to start doing things.
404 * This function must be called with the vnode at least read-locked, and
405 * no locks on the dcache, because it plays around with dcache entries.
408 afs_PrefetchChunk(struct vcache *avc, struct dcache *adc,
409 afs_ucred_t *acred, struct vrequest *areq)
413 afs_size_t j1, j2; /* junk vbls for GetDCache to trash */
415 offset = adc->f.chunk + 1; /* next chunk we'll need */
416 offset = AFS_CHUNKTOBASE(offset); /* base of next chunk */
417 ObtainReadLock(&adc->lock);
418 ObtainSharedLock(&adc->mflock, 662);
419 if (offset < avc->f.m.Length && !(adc->mflags & DFNextStarted)
423 UpgradeSToWLock(&adc->mflock, 663);
424 adc->mflags |= DFNextStarted; /* we've tried to prefetch for this guy */
425 ReleaseWriteLock(&adc->mflock);
426 ReleaseReadLock(&adc->lock);
428 tdc = afs_GetDCache(avc, offset, areq, &j1, &j2, 2); /* type 2 never returns 0 */
430 * In disconnected mode, type 2 can return 0 because it doesn't
431 * make any sense to allocate a dcache we can never fill
436 ObtainSharedLock(&tdc->mflock, 651);
437 if (!(tdc->mflags & DFFetchReq)) {
438 /* ask the daemon to do the work */
439 UpgradeSToWLock(&tdc->mflock, 652);
440 tdc->mflags |= DFFetchReq; /* guaranteed to be cleared by BKG or GetDCache */
441 /* last parm (1) tells bkg daemon to do an afs_PutDCache when it is done,
442 * since we don't want to wait for it to finish before doing so ourselves.
444 bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
445 (afs_size_t) offset, (afs_size_t) 1, tdc,
446 (void *)0, (void *)0);
448 /* Bkg table full; just abort non-important prefetching to avoid deadlocks */
449 tdc->mflags &= ~DFFetchReq;
450 ReleaseWriteLock(&tdc->mflock);
454 * DCLOCKXXX: This is a little sketchy, since someone else
455 * could have already started a prefetch.. In practice,
456 * this probably doesn't matter; at most it would cause an
457 * extra slot in the BKG table to be used up when someone
458 * prefetches this for the second time.
460 ObtainReadLock(&adc->lock);
461 ObtainWriteLock(&adc->mflock, 664);
462 adc->mflags &= ~DFNextStarted;
463 ReleaseWriteLock(&adc->mflock);
464 ReleaseReadLock(&adc->lock);
466 ReleaseWriteLock(&tdc->mflock);
469 ReleaseSharedLock(&tdc->mflock);
473 ReleaseSharedLock(&adc->mflock);
474 ReleaseReadLock(&adc->lock);
479 afs_UFSReadUIO(afs_dcache_id_t *cacheId, struct uio *tuiop)
482 struct osi_file *tfile;
484 tfile = (struct osi_file *) osi_UFSOpen(cacheId);
488 #if defined(AFS_AIX41_ENV)
491 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, tuiop, NULL, NULL,
492 NULL, afs_osi_credp);
494 #elif defined(AFS_AIX32_ENV)
496 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, tuiop, NULL, NULL);
497 /* Flush all JFS pages now for big performance gain in big file cases
498 * If we do something like this, must check to be sure that AFS file
499 * isn't mmapped... see afs_gn_map() for why.
502 if (tfile->vnode->v_gnode && tfile->vnode->v_gnode->gn_seg) {
503 any different ways to do similar things:
504 so far, the best performing one is #2, but #1 might match it if we
505 straighten out the confusion regarding which pages to flush. It
507 1. vm_flushp(tfile->vnode->v_gnode->gn_seg, 0, len/PAGESIZE - 1);
508 2. vm_releasep(tfile->vnode->v_gnode->gn_seg, offset/PAGESIZE,
509 (len + PAGESIZE-1)/PAGESIZE);
510 3. vms_inactive(tfile->vnode->v_gnode->gn_seg) Doesn't work correctly
511 4. vms_delete(tfile->vnode->v_gnode->gn_seg) probably also fails
512 tfile->vnode->v_gnode->gn_seg = NULL;
516 Unfortunately, this seems to cause frequent "cache corruption" episodes.
517 vm_releasep(tfile->vnode->v_gnode->gn_seg, offset/PAGESIZE,
518 (len + PAGESIZE-1)/PAGESIZE);
521 #elif defined(AFS_AIX_ENV)
523 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, (off_t) & offset,
524 tuiop, NULL, NULL, -1);
525 #elif defined(AFS_SUN5_ENV)
527 #ifdef AFS_SUN510_ENV
528 VOP_RWLOCK(tfile->vnode, 0, NULL);
529 code = VOP_READ(tfile->vnode, tuiop, 0, afs_osi_credp, NULL);
530 VOP_RWUNLOCK(tfile->vnode, 0, NULL);
532 VOP_RWLOCK(tfile->vnode, 0);
533 code = VOP_READ(tfile->vnode, tuiop, 0, afs_osi_credp);
534 VOP_RWUNLOCK(tfile->vnode, 0);
537 #elif defined(AFS_SGI_ENV)
539 AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_READ);
540 AFS_VOP_READ(tfile->vnode, tuiop, IO_ISLOCKED, afs_osi_credp,
542 AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_READ);
544 #elif defined(AFS_HPUX100_ENV)
546 code = VOP_RDWR(tfile->vnode, tuiop, UIO_READ, 0, afs_osi_credp);
548 #elif defined(AFS_LINUX_ENV)
550 code = osi_rdwr(tfile, tuiop, UIO_READ);
552 #elif defined(AFS_DARWIN80_ENV)
554 code = VNOP_READ(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
556 #elif defined(AFS_DARWIN_ENV)
558 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
559 code = VOP_READ(tfile->vnode, tuiop, 0, afs_osi_credp);
560 VOP_UNLOCK(tfile->vnode, 0, current_proc());
562 #elif defined(AFS_FBSD_ENV)
564 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
565 code = VOP_READ(tfile->vnode, tuiop, 0, afs_osi_credp);
566 VOP_UNLOCK(tfile->vnode, 0);
568 #elif defined(AFS_NBSD_ENV)
569 tuiop->uio_rw = UIO_READ;
571 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
572 code = VOP_READ(tfile->vnode, tuiop, 0, afs_osi_credp);
573 # if defined(AFS_NBSD60_ENV)
574 VOP_UNLOCK(tfile->vnode);
576 VOP_UNLOCK(tfile->vnode, 0);
579 #elif defined(AFS_XBSD_ENV)
581 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
582 code = VOP_READ(tfile->vnode, tuiop, 0, afs_osi_credp);
583 VOP_UNLOCK(tfile->vnode, 0, curproc);
586 code = VOP_RDWR(tfile->vnode, tuiop, UIO_READ, 0, afs_osi_credp);