pmtu-and-prefetch-20080520
[openafs.git] / src / afs / VNOPS / afs_vnop_read.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * Implements:
12  * afs_MemRead
13  * afs_PrefetchChunk
14  * afs_UFSRead
15  * 
16  */
17
18 #include <afsconfig.h>
19 #include "afs/param.h"
20
21 RCSID
22     ("$Header$");
23
24 #include "afs/sysincludes.h"    /* Standard vendor system headers */
25 #include "afsincludes.h"        /* Afs-based standard headers */
26 #include "afs/afs_stats.h"      /* statistics */
27 #include "afs/afs_cbqueue.h"
28 #include "afs/nfsclient.h"
29 #include "afs/afs_osidnlc.h"
30 #include "afs/afs_osi.h"
31
32
33 extern char afs_zeros[AFS_ZEROS];
34
35 afs_int32 maxIHint;
36 afs_int32 nihints;              /* # of above actually in-use */
37 afs_int32 usedihint;
38
39
40 /* Imported variables */
41 extern afs_rwlock_t afs_xdcache;
42 extern unsigned char *afs_indexFlags;
43 extern afs_hyper_t *afs_indexTimes;     /* Dcache entry Access times */
44 extern afs_hyper_t afs_indexCounter;    /* Fake time for marking index */
45
46
47 /* Forward declarations */
48 void afs_PrefetchChunk(struct vcache *avc, struct dcache *adc,
49                        struct AFS_UCRED *acred, struct vrequest *areq);
50
51 int
52 afs_MemRead(register struct vcache *avc, struct uio *auio,
53             struct AFS_UCRED *acred, daddr_t albn, struct buf **abpp,
54             int noLock)
55 {
56     afs_size_t totalLength;
57     afs_size_t transferLength;
58     afs_size_t filePos;
59     afs_size_t offset, len, tlen;
60     afs_int32 trimlen;
61     struct dcache *tdc = 0;
62     afs_int32 error, trybusy = 1;
63 #ifdef AFS_DARWIN80_ENV
64     uio_t tuiop = NULL;
65 #else
66     struct uio tuio;
67     struct uio *tuiop = &tuio;
68     struct iovec *tvec;
69 #endif
70     afs_int32 code;
71     struct vrequest treq;
72
73     AFS_STATCNT(afs_MemRead);
74     if (avc->vc_error)
75         return EIO;
76
77     /* check that we have the latest status info in the vnode cache */
78     if ((code = afs_InitReq(&treq, acred)))
79         return code;
80     if (!noLock) {
81         code = afs_VerifyVCache(avc, &treq);
82         if (code) {
83             code = afs_CheckCode(code, &treq, 8);       /* failed to get it */
84             return code;
85         }
86     }
87 #ifndef AFS_VM_RDWR_ENV
88     if (AFS_NFSXLATORREQ(acred)) {
89         if (!afs_AccessOK
90             (avc, PRSFS_READ, &treq,
91              CHECK_MODE_BITS | CMB_ALLOW_EXEC_AS_READ)) {
92             return afs_CheckCode(EACCES, &treq, 9);
93         }
94     }
95 #endif
96
97 #ifndef AFS_DARWIN80_ENV
98     tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
99 #endif
100     totalLength = AFS_UIO_RESID(auio);
101     filePos = AFS_UIO_OFFSET(auio);
102     afs_Trace4(afs_iclSetp, CM_TRACE_READ, ICL_TYPE_POINTER, avc,
103                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_INT32,
104                totalLength, ICL_TYPE_OFFSET,
105                ICL_HANDLE_OFFSET(avc->m.Length));
106     error = 0;
107     transferLength = 0;
108     if (!noLock)
109         ObtainReadLock(&avc->lock);
110 #if     defined(AFS_TEXT_ENV) && !defined(AFS_VM_RDWR_ENV)
111     if (avc->flushDV.high == AFS_MAXDV && avc->flushDV.low == AFS_MAXDV) {
112         hset(avc->flushDV, avc->m.DataVersion);
113     }
114 #endif
115
116     /*
117      * Locks held:
118      * avc->lock(R)
119      */
120     if (filePos >= avc->m.Length) {
121         if (len > AFS_ZEROS)
122             len = sizeof(afs_zeros);    /* and in 0 buffer */
123         len = 0;
124 #ifdef AFS_DARWIN80_ENV
125         trimlen = len;
126         tuiop = afsio_darwin_partialcopy(auio, trimlen);
127 #else
128         afsio_copy(auio, &tuio, tvec);
129         trimlen = len;
130         afsio_trim(&tuio, trimlen);
131 #endif
132         AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
133     }
134
135     while (avc->m.Length > 0 && totalLength > 0) {
136         /* read all of the cached info */
137         if (filePos >= avc->m.Length)
138             break;              /* all done */
139         if (noLock) {
140             if (tdc) {
141                 ReleaseReadLock(&tdc->lock);
142                 afs_PutDCache(tdc);
143             }
144             tdc = afs_FindDCache(avc, filePos);
145             if (tdc) {
146                 ObtainReadLock(&tdc->lock);
147                 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
148                 len = tdc->f.chunkBytes - offset;
149             }
150         } else {
151             /* a tricky question: does the presence of the DFFetching flag
152              * mean that we're fetching the latest version of the file?  No.
153              * The server could update the file as soon as the fetch responsible
154              * for the setting of the DFFetching flag completes.
155              * 
156              * However, the presence of the DFFetching flag (visible under
157              * a dcache read lock since it is set and cleared only under a
158              * dcache write lock) means that we're fetching as good a version
159              * as was known to this client at the time of the last call to
160              * afs_VerifyVCache, since the latter updates the stat cache's
161              * m.DataVersion field under a vcache write lock, and from the
162              * time that the DFFetching flag goes on in afs_GetDCache (before
163              * the fetch starts), to the time it goes off (after the fetch
164              * completes), afs_GetDCache keeps at least a read lock on the
165              * vcache entry.
166              * 
167              * This means that if the DFFetching flag is set, we can use that
168              * data for any reads that must come from the current version of
169              * the file (current == m.DataVersion).
170              * 
171              * Another way of looking at this same point is this: if we're
172              * fetching some data and then try do an afs_VerifyVCache, the
173              * VerifyVCache operation will not complete until after the
174              * DFFetching flag is turned off and the dcache entry's f.versionNo
175              * field is updated.
176              * 
177              * Note, by the way, that if DFFetching is set,
178              * m.DataVersion > f.versionNo (the latter is not updated until
179              * after the fetch completes).
180              */
181             if (tdc) {
182                 ReleaseReadLock(&tdc->lock);
183                 afs_PutDCache(tdc);     /* before reusing tdc */
184             }
185             tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 2);
186             ObtainReadLock(&tdc->lock);
187             /* now, first try to start transfer, if we'll need the data.  If
188              * data already coming, we don't need to do this, obviously.  Type
189              * 2 requests never return a null dcache entry, btw.
190              */
191             if (!(tdc->dflags & DFFetching)
192                 && !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
193                 /* have cache entry, it is not coming in now,
194                  * and we'll need new data */
195               tagain:
196                 if (trybusy && !afs_BBusy()) {
197                     struct brequest *bp;
198                     /* daemon is not busy */
199                     ObtainSharedLock(&tdc->mflock, 665);
200                     if (!(tdc->mflags & DFFetchReq)) {
201                         /* start the daemon (may already be running, however) */
202                         UpgradeSToWLock(&tdc->mflock, 666);
203                         tdc->mflags |= DFFetchReq;
204                         bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
205                                         (afs_size_t) filePos, (afs_size_t) 0,
206                                         tdc);
207                         if (!bp) {
208                             tdc->mflags &= ~DFFetchReq;
209                             trybusy = 0;        /* Avoid bkg daemon since they're too busy */
210                             ReleaseWriteLock(&tdc->mflock);
211                             goto tagain;
212                         }
213                         ConvertWToSLock(&tdc->mflock);
214                         /* don't use bp pointer! */
215                     }
216                     code = 0;
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);
231                     }
232                     ReleaseReadLock(&tdc->mflock);
233                     if (code) {
234                         error = code;
235                         break;
236                     }
237                 }
238             }
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.
241              */
242             code = 0;
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,
250                            tdc->dflags);
251                 ReleaseReadLock(&tdc->lock);
252                 ReleaseReadLock(&avc->lock);
253                 code = afs_osi_SleepSig(&tdc->validPos);
254                 ObtainReadLock(&avc->lock);
255                 ObtainReadLock(&tdc->lock);
256             }
257             if (code) {
258                 error = code;
259                 break;
260             }
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;
268             } else {
269                 /* no longer fetching, verify data version 
270                  * (avoid new GetDCache call) */
271                 if (hsame(avc->m.DataVersion, tdc->f.versionNo)
272                     && ((len = tdc->validPos - filePos) > 0)) {
273                     offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
274                 } else {
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->m.DataVersion,
279                                ICL_TYPE_HYPER, &tdc->f.versionNo);
280                     ReleaseReadLock(&tdc->lock);
281                     afs_PutDCache(tdc);
282                     tdc = NULL;
283                 }
284             }
285
286             if (!tdc) {
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.
290                  */
291                 ReleaseReadLock(&avc->lock);
292                 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 1);
293                 ObtainReadLock(&avc->lock);
294                 if (tdc)
295                     ObtainReadLock(&tdc->lock);
296             }
297         }
298
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));
302         if (!tdc) {
303             error = EIO;
304             break;
305         }
306
307         /*
308          * Locks held:
309          * avc->lock(R)
310          * tdc->lock(R)
311          */
312
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->m.Length - offset;      /* and still within file */
322             if (len > tlen)
323                 len = tlen;
324             if (len > AFS_ZEROS)
325                 len = sizeof(afs_zeros);        /* and in 0 buffer */
326 #ifdef AFS_DARWIN80_ENV
327             trimlen = len;
328             tuiop = afsio_darwin_partialcopy(auio, trimlen);
329 #else
330             afsio_copy(auio, &tuio, tvec);
331             trimlen = len;
332             afsio_trim(&tuio, trimlen);
333 #endif
334             AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
335             if (code) {
336                 error = code;
337                 break;
338             }
339         } else {
340             /* get the data from the mem cache */
341
342             /* mung uio structure to be right for this transfer */
343 #ifdef AFS_DARWIN80_ENV
344             trimlen = len;
345             tuiop = afsio_darwin_partialcopy(auio, trimlen);
346             uio_setoffset(tuiop, offset);
347 #else
348             afsio_copy(auio, &tuio, tvec);
349             trimlen = len;
350             afsio_trim(&tuio, trimlen);
351             tuio.afsio_offset = offset;
352 #endif
353
354             code = afs_MemReadUIO(tdc->f.inode, tuiop);
355
356             if (code) {
357                 error = code;
358                 break;
359             }
360         }
361         /* otherwise we've read some, fixup length, etc and continue with next seg */
362         len = len - AFS_UIO_RESID(tuiop);       /* compute amount really transferred */
363         trimlen = len;
364         afsio_skip(auio, trimlen);      /* update input uio structure */
365         totalLength -= len;
366         transferLength += len;
367         filePos += len;
368
369         if (len <= 0)
370             break;              /* surprise eof */
371 #ifdef AFS_DARWIN80_ENV
372         if (tuiop) {
373             uio_free(tuiop);
374             tuiop = 0;
375         }
376 #endif
377     }                           /* the whole while loop */
378
379     /*
380      * Locks held:
381      * avc->lock(R)
382      * tdc->lock(R) if tdc
383      */
384
385     /* if we make it here with tdc non-zero, then it is the last chunk we
386      * dealt with, and we have to release it when we're done.  We hold on
387      * to it in case we need to do a prefetch.
388      */
389     if (tdc) {
390         ReleaseReadLock(&tdc->lock);
391         /* try to queue prefetch, if needed */
392         if (!noLock &&
393 #ifndef AFS_VM_RDWR_ENV
394             afs_preCache
395 #else
396             1
397 #endif
398             ) {
399             afs_PrefetchChunk(avc, tdc, acred, &treq);
400         }
401         afs_PutDCache(tdc);
402     }
403     if (!noLock)
404         ReleaseReadLock(&avc->lock);
405 #ifdef AFS_DARWIN80_ENV
406     if (tuiop)
407        uio_free(tuiop);
408 #else
409     osi_FreeSmallSpace(tvec);
410 #endif
411     error = afs_CheckCode(error, &treq, 10);
412     return error;
413 }
414
415 /* called with the dcache entry triggering the fetch, the vcache entry involved,
416  * and a vrequest for the read call.  Marks the dcache entry as having already
417  * triggered a prefetch, starts the prefetch going and sets the DFFetchReq
418  * flag in the prefetched block, so that the next call to read knows to wait
419  * for the daemon to start doing things.
420  *
421  * This function must be called with the vnode at least read-locked, and
422  * no locks on the dcache, because it plays around with dcache entries.
423  */
424 void
425 afs_PrefetchChunk(struct vcache *avc, struct dcache *adc,
426                   struct AFS_UCRED *acred, struct vrequest *areq)
427 {
428     register struct dcache *tdc;
429     afs_size_t offset;
430     afs_size_t j1, j2;          /* junk vbls for GetDCache to trash */
431
432     offset = adc->f.chunk + 1;  /* next chunk we'll need */
433     offset = AFS_CHUNKTOBASE(offset);   /* base of next chunk */
434     ObtainReadLock(&adc->lock);
435     ObtainSharedLock(&adc->mflock, 662);
436     if (offset < avc->m.Length && !(adc->mflags & DFNextStarted)
437         && !afs_BBusy()) {
438         struct brequest *bp;
439
440         UpgradeSToWLock(&adc->mflock, 663);
441         adc->mflags |= DFNextStarted;   /* we've tried to prefetch for this guy */
442         ReleaseWriteLock(&adc->mflock);
443         ReleaseReadLock(&adc->lock);
444
445         tdc = afs_GetDCache(avc, offset, areq, &j1, &j2, 2);    /* type 2 never returns 0 */
446         ObtainSharedLock(&tdc->mflock, 651);
447         if (!(tdc->mflags & DFFetchReq)) {
448             /* ask the daemon to do the work */
449             UpgradeSToWLock(&tdc->mflock, 652);
450             tdc->mflags |= DFFetchReq;  /* guaranteed to be cleared by BKG or GetDCache */
451             /* last parm (1) tells bkg daemon to do an afs_PutDCache when it is done,
452              * since we don't want to wait for it to finish before doing so ourselves.
453              */
454             bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
455                             (afs_size_t) offset, (afs_size_t) 1, tdc);
456             if (!bp) {
457                 /* Bkg table full; just abort non-important prefetching to avoid deadlocks */
458                 tdc->mflags &= ~DFFetchReq;
459                 ReleaseWriteLock(&tdc->mflock);
460                 afs_PutDCache(tdc);
461
462                 /*
463                  * DCLOCKXXX: This is a little sketchy, since someone else
464                  * could have already started a prefetch..  In practice,
465                  * this probably doesn't matter; at most it would cause an
466                  * extra slot in the BKG table to be used up when someone
467                  * prefetches this for the second time.
468                  */
469                 ObtainReadLock(&adc->lock);
470                 ObtainWriteLock(&adc->mflock, 664);
471                 adc->mflags &= ~DFNextStarted;
472                 ReleaseWriteLock(&adc->mflock);
473                 ReleaseReadLock(&adc->lock);
474             } else {
475                 ReleaseWriteLock(&tdc->mflock);
476             }
477         } else {
478             ReleaseSharedLock(&tdc->mflock);
479             afs_PutDCache(tdc);
480         }
481     } else {
482         ReleaseSharedLock(&adc->mflock);
483         ReleaseReadLock(&adc->lock);
484     }
485 }
486
487 int
488 afs_UFSRead(register struct vcache *avc, struct uio *auio,
489             struct AFS_UCRED *acred, daddr_t albn, struct buf **abpp,
490             int noLock)
491 {
492     afs_size_t totalLength;
493     afs_size_t transferLength;
494     afs_size_t filePos;
495     afs_size_t offset, len, tlen;
496     afs_int32 trimlen;
497     struct dcache *tdc = 0;
498     afs_int32 error;
499 #ifdef AFS_DARWIN80_ENV
500     uio_t tuiop=NULL;
501 #else
502     struct uio tuio;
503     struct uio *tuiop = &tuio;
504     struct iovec *tvec;
505 #endif
506     struct osi_file *tfile;
507     afs_int32 code;
508     int trybusy = 1;
509     struct vrequest treq;
510
511     AFS_STATCNT(afs_UFSRead);
512     if (avc && avc->vc_error)
513         return EIO;
514
515     /* check that we have the latest status info in the vnode cache */
516     if ((code = afs_InitReq(&treq, acred)))
517         return code;
518     if (!noLock) {
519         if (!avc)
520             osi_Panic("null avc in afs_UFSRead");
521         else {
522             code = afs_VerifyVCache(avc, &treq);
523             if (code) {
524                 code = afs_CheckCode(code, &treq, 11);  /* failed to get it */
525                 return code;
526             }
527         }
528     }
529 #ifndef AFS_VM_RDWR_ENV
530     if (AFS_NFSXLATORREQ(acred)) {
531         if (!afs_AccessOK
532             (avc, PRSFS_READ, &treq,
533              CHECK_MODE_BITS | CMB_ALLOW_EXEC_AS_READ)) {
534             return afs_CheckCode(EACCES, &treq, 12);
535         }
536     }
537 #endif
538
539 #ifndef AFS_DARWIN80_ENV
540     tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
541 #endif
542     totalLength = AFS_UIO_RESID(auio);
543     filePos = AFS_UIO_OFFSET(auio);
544     afs_Trace4(afs_iclSetp, CM_TRACE_READ, ICL_TYPE_POINTER, avc,
545                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_INT32,
546                totalLength, ICL_TYPE_OFFSET,
547                ICL_HANDLE_OFFSET(avc->m.Length));
548     error = 0;
549     transferLength = 0;
550     if (!noLock)
551         ObtainReadLock(&avc->lock);
552 #if     defined(AFS_TEXT_ENV) && !defined(AFS_VM_RDWR_ENV)
553     if (avc->flushDV.high == AFS_MAXDV && avc->flushDV.low == AFS_MAXDV) {
554         hset(avc->flushDV, avc->m.DataVersion);
555     }
556 #endif
557
558     if (filePos >= avc->m.Length) {
559         if (len > AFS_ZEROS)
560             len = sizeof(afs_zeros);    /* and in 0 buffer */
561         len = 0;
562 #ifdef AFS_DARWIN80_ENV
563         trimlen = len;
564         tuiop = afsio_darwin_partialcopy(auio, trimlen);
565 #else
566         afsio_copy(auio, &tuio, tvec);
567         trimlen = len;
568         afsio_trim(&tuio, trimlen);
569 #endif
570         AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
571     }
572
573     while (avc->m.Length > 0 && totalLength > 0) {
574         /* read all of the cached info */
575         if (filePos >= avc->m.Length)
576             break;              /* all done */
577         if (noLock) {
578             if (tdc) {
579                 ReleaseReadLock(&tdc->lock);
580                 afs_PutDCache(tdc);
581             }
582             tdc = afs_FindDCache(avc, filePos);
583             if (tdc) {
584                 ObtainReadLock(&tdc->lock);
585                 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
586                 len = tdc->validPos - filePos;
587             }
588         } else {
589             /* a tricky question: does the presence of the DFFetching flag
590              * mean that we're fetching the latest version of the file?  No.
591              * The server could update the file as soon as the fetch responsible
592              * for the setting of the DFFetching flag completes.
593              * 
594              * However, the presence of the DFFetching flag (visible under
595              * a dcache read lock since it is set and cleared only under a
596              * dcache write lock) means that we're fetching as good a version
597              * as was known to this client at the time of the last call to
598              * afs_VerifyVCache, since the latter updates the stat cache's
599              * m.DataVersion field under a vcache write lock, and from the
600              * time that the DFFetching flag goes on in afs_GetDCache (before
601              * the fetch starts), to the time it goes off (after the fetch
602              * completes), afs_GetDCache keeps at least a read lock on the
603              * vcache entry.
604              * 
605              * This means that if the DFFetching flag is set, we can use that
606              * data for any reads that must come from the current version of
607              * the file (current == m.DataVersion).
608              * 
609              * Another way of looking at this same point is this: if we're
610              * fetching some data and then try do an afs_VerifyVCache, the
611              * VerifyVCache operation will not complete until after the
612              * DFFetching flag is turned off and the dcache entry's f.versionNo
613              * field is updated.
614              * 
615              * Note, by the way, that if DFFetching is set,
616              * m.DataVersion > f.versionNo (the latter is not updated until
617              * after the fetch completes).
618              */
619             if (tdc) {
620                 ReleaseReadLock(&tdc->lock);
621                 afs_PutDCache(tdc);     /* before reusing tdc */
622             }
623             tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 2);
624             ObtainReadLock(&tdc->lock);
625             /* now, first try to start transfer, if we'll need the data.  If
626              * data already coming, we don't need to do this, obviously.  Type
627              * 2 requests never return a null dcache entry, btw. */
628             if (!(tdc->dflags & DFFetching)
629                 && !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
630                 /* have cache entry, it is not coming in now, and we'll need new data */
631               tagain:
632                 if (trybusy && !afs_BBusy()) {
633                     struct brequest *bp;
634                     /* daemon is not busy */
635                     ObtainSharedLock(&tdc->mflock, 667);
636                     if (!(tdc->mflags & DFFetchReq)) {
637                         UpgradeSToWLock(&tdc->mflock, 668);
638                         tdc->mflags |= DFFetchReq;
639                         bp = afs_BQueue(BOP_FETCH, avc, B_DONTWAIT, 0, acred,
640                                         (afs_size_t) filePos, (afs_size_t) 0,
641                                         tdc);
642                         if (!bp) {
643                             /* Bkg table full; retry deadlocks */
644                             tdc->mflags &= ~DFFetchReq;
645                             trybusy = 0;        /* Avoid bkg daemon since they're too busy */
646                             ReleaseWriteLock(&tdc->mflock);
647                             goto tagain;
648                         }
649                         ConvertWToSLock(&tdc->mflock);
650                     }
651                     code = 0;
652                     ConvertSToRLock(&tdc->mflock);
653                     while (!code && tdc->mflags & DFFetchReq) {
654                         afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT,
655                                    ICL_TYPE_STRING, __FILE__, ICL_TYPE_INT32,
656                                    __LINE__, ICL_TYPE_POINTER, tdc,
657                                    ICL_TYPE_INT32, tdc->dflags);
658                         /* don't need waiting flag on this one */
659                         ReleaseReadLock(&tdc->mflock);
660                         ReleaseReadLock(&tdc->lock);
661                         ReleaseReadLock(&avc->lock);
662                         code = afs_osi_SleepSig(&tdc->validPos);
663                         ObtainReadLock(&avc->lock);
664                         ObtainReadLock(&tdc->lock);
665                         ObtainReadLock(&tdc->mflock);
666                     }
667                     ReleaseReadLock(&tdc->mflock);
668                     if (code) {
669                         error = code;
670                         break;
671                     }
672                 }
673             }
674             /* now data may have started flowing in (if DFFetching is on).  If
675              * data is now streaming in, then wait for some interesting stuff.
676              */
677             code = 0;
678             while (!code && (tdc->dflags & DFFetching)
679                    && tdc->validPos <= filePos) {
680                 /* too early: wait for DFFetching flag to vanish,
681                  * or data to appear */
682                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT, ICL_TYPE_STRING,
683                            __FILE__, ICL_TYPE_INT32, __LINE__,
684                            ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
685                            tdc->dflags);
686                 ReleaseReadLock(&tdc->lock);
687                 ReleaseReadLock(&avc->lock);
688                 code = afs_osi_SleepSig(&tdc->validPos);
689                 ObtainReadLock(&avc->lock);
690                 ObtainReadLock(&tdc->lock);
691             }
692             if (code) {
693                 error = code;
694                 break;
695             }
696             /* fetching flag gone, data is here, or we never tried
697              * (BBusy for instance) */
698             if (tdc->dflags & DFFetching) {
699                 /* still fetching, some new data is here:
700                  * compute length and offset */
701                 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
702                 len = tdc->validPos - filePos;
703             } else {
704                 /* no longer fetching, verify data version (avoid new
705                  * GetDCache call) */
706                 if (hsame(avc->m.DataVersion, tdc->f.versionNo)
707                     && ((len = tdc->validPos - filePos) > 0)) {
708                     offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
709                 } else {
710                     /* don't have current data, so get it below */
711                     afs_Trace3(afs_iclSetp, CM_TRACE_VERSIONNO,
712                                ICL_TYPE_INT64, ICL_HANDLE_OFFSET(filePos),
713                                ICL_TYPE_HYPER, &avc->m.DataVersion,
714                                ICL_TYPE_HYPER, &tdc->f.versionNo);
715                     ReleaseReadLock(&tdc->lock);
716                     afs_PutDCache(tdc);
717                     tdc = NULL;
718                 }
719             }
720
721             if (!tdc) {
722                 /* If we get, it was not possible to start the 
723                  * background daemon. With flag == 1 afs_GetDCache
724                  * does the FetchData rpc synchronously.
725                  */
726                 ReleaseReadLock(&avc->lock);
727                 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 1);
728                 ObtainReadLock(&avc->lock);
729                 if (tdc)
730                     ObtainReadLock(&tdc->lock);
731             }
732         }
733
734         if (!tdc) {
735             error = EIO;
736             break;
737         }
738         len = tdc->validPos - filePos;
739         afs_Trace3(afs_iclSetp, CM_TRACE_VNODEREAD, ICL_TYPE_POINTER, tdc,
740                    ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(offset),
741                    ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(len));
742         if (len > totalLength)
743             len = totalLength;  /* will read len bytes */
744         if (len <= 0) {         /* shouldn't get here if DFFetching is on */
745             afs_Trace4(afs_iclSetp, CM_TRACE_VNODEREAD2, ICL_TYPE_POINTER,
746                        tdc, ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(tdc->validPos),
747                        ICL_TYPE_INT32, tdc->f.chunkBytes, ICL_TYPE_INT32,
748                        tdc->dflags);
749             /* read past the end of a chunk, may not be at next chunk yet, and yet
750              * also not at eof, so may have to supply fake zeros */
751             len = AFS_CHUNKTOSIZE(tdc->f.chunk) - offset;       /* bytes left in chunk addr space */
752             if (len > totalLength)
753                 len = totalLength;      /* and still within xfr request */
754             tlen = avc->m.Length - offset;      /* and still within file */
755             if (len > tlen)
756                 len = tlen;
757             if (len > AFS_ZEROS)
758                 len = sizeof(afs_zeros);        /* and in 0 buffer */
759 #ifdef AFS_DARWIN80_ENV
760             trimlen = len;
761             tuiop = afsio_darwin_partialcopy(auio, trimlen);
762 #else
763             afsio_copy(auio, &tuio, tvec);
764             trimlen = len;
765             afsio_trim(&tuio, trimlen);
766 #endif
767             AFS_UIOMOVE(afs_zeros, trimlen, UIO_READ, tuiop, code);
768             if (code) {
769                 error = code;
770                 break;
771             }
772         } else {
773             /* get the data from the file */
774 #ifdef IHINT
775             if (tfile = tdc->ihint) {
776                 if (tdc->f.inode != tfile->inum) {
777                     afs_warn("afs_UFSRead: %x hint mismatch tdc %d inum %d\n",
778                              tdc, tdc->f.inode, tfile->inum);
779                     osi_UFSClose(tfile);
780                     tdc->ihint = tfile = 0;
781                     nihints--;
782                 }
783             }
784             if (tfile != 0) {
785                 usedihint++;
786             } else
787 #endif /* IHINT */
788
789                 tfile = (struct osi_file *)osi_UFSOpen(tdc->f.inode);
790 #ifdef AFS_DARWIN80_ENV
791             trimlen = len;
792             tuiop = afsio_darwin_partialcopy(auio, trimlen);
793             uio_setoffset(tuiop, offset);
794 #else
795             /* mung uio structure to be right for this transfer */
796             afsio_copy(auio, &tuio, tvec);
797             trimlen = len;
798             afsio_trim(&tuio, trimlen);
799             tuio.afsio_offset = offset;
800 #endif
801
802 #if defined(AFS_AIX41_ENV)
803             AFS_GUNLOCK();
804             code =
805                 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, &tuio, NULL, NULL,
806                           NULL, afs_osi_credp);
807             AFS_GLOCK();
808 #elif defined(AFS_AIX32_ENV)
809             code =
810                 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, &tuio, NULL, NULL);
811             /* Flush all JFS pages now for big performance gain in big file cases
812              * If we do something like this, must check to be sure that AFS file 
813              * isn't mmapped... see afs_gn_map() for why.
814              */
815 /*
816           if (tfile->vnode->v_gnode && tfile->vnode->v_gnode->gn_seg) {
817  many different ways to do similar things:
818    so far, the best performing one is #2, but #1 might match it if we
819    straighten out the confusion regarding which pages to flush.  It 
820    really does matter.
821    1.       vm_flushp(tfile->vnode->v_gnode->gn_seg, 0, len/PAGESIZE - 1);
822    2.       vm_releasep(tfile->vnode->v_gnode->gn_seg, offset/PAGESIZE, 
823                         (len + PAGESIZE-1)/PAGESIZE);
824    3.       vms_inactive(tfile->vnode->v_gnode->gn_seg) Doesn't work correctly
825    4.       vms_delete(tfile->vnode->v_gnode->gn_seg) probably also fails
826             tfile->vnode->v_gnode->gn_seg = NULL;
827    5.       deletep
828    6.       ipgrlse
829    7.       ifreeseg
830           Unfortunately, this seems to cause frequent "cache corruption" episodes.
831             vm_releasep(tfile->vnode->v_gnode->gn_seg, offset/PAGESIZE, 
832                         (len + PAGESIZE-1)/PAGESIZE);
833           }     
834 */
835 #elif defined(AFS_AIX_ENV)
836             code =
837                 VNOP_RDWR(tfile->vnode, UIO_READ, FREAD, (off_t) & offset,
838                           &tuio, NULL, NULL, -1);
839 #elif defined(AFS_SUN5_ENV)
840             AFS_GUNLOCK();
841 #ifdef AFS_SUN510_ENV
842             {
843                 caller_context_t ct;
844
845                 VOP_RWLOCK(tfile->vnode, 0, &ct);
846                 code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp, &ct);
847                 VOP_RWUNLOCK(tfile->vnode, 0, &ct);
848             }
849 #else
850             VOP_RWLOCK(tfile->vnode, 0);
851             code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp);
852             VOP_RWUNLOCK(tfile->vnode, 0);
853 #endif
854             AFS_GLOCK();
855 #elif defined(AFS_SGI_ENV)
856             AFS_GUNLOCK();
857             AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_READ);
858             AFS_VOP_READ(tfile->vnode, &tuio, IO_ISLOCKED, afs_osi_credp,
859                          code);
860             AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_READ);
861             AFS_GLOCK();
862 #elif defined(AFS_OSF_ENV)
863             tuio.uio_rw = UIO_READ;
864             AFS_GUNLOCK();
865             VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp, code);
866             AFS_GLOCK();
867 #elif defined(AFS_HPUX100_ENV)
868             AFS_GUNLOCK();
869             code = VOP_RDWR(tfile->vnode, &tuio, UIO_READ, 0, afs_osi_credp);
870             AFS_GLOCK();
871 #elif defined(AFS_LINUX20_ENV)
872             AFS_GUNLOCK();
873             code = osi_rdwr(tfile, &tuio, UIO_READ);
874             AFS_GLOCK();
875 #elif defined(AFS_DARWIN80_ENV)
876             AFS_GUNLOCK();
877             code = VNOP_READ(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
878             AFS_GLOCK();
879 #elif defined(AFS_DARWIN_ENV)
880             AFS_GUNLOCK();
881             VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
882             code = VOP_READ(tfile->vnode, &tuio, 0, afs_osi_credp);
883             VOP_UNLOCK(tfile->vnode, 0, current_proc());
884             AFS_GLOCK();
885 #elif defined(AFS_FBSD50_ENV)
886             AFS_GUNLOCK();
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);
890             AFS_GLOCK();
891 #elif defined(AFS_XBSD_ENV)
892             AFS_GUNLOCK();
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);
896             AFS_GLOCK();
897 #else
898             code = VOP_RDWR(tfile->vnode, &tuio, UIO_READ, 0, afs_osi_credp);
899 #endif
900
901 #ifdef IHINT
902             if (!tdc->ihint && nihints < maxIHint) {
903                 tdc->ihint = tfile;
904                 nihints++;
905             } else
906 #endif /* IHINT */
907                 osi_UFSClose(tfile);
908
909             if (code) {
910                 error = code;
911                 break;
912             }
913         }
914         /* otherwise we've read some, fixup length, etc and continue with next seg */
915         len = len - AFS_UIO_RESID(tuiop);       /* compute amount really transferred */
916         trimlen = len;
917         afsio_skip(auio, trimlen);      /* update input uio structure */
918         totalLength -= len;
919         transferLength += len;
920         filePos += len;
921         if (len <= 0)
922             break;              /* surprise eof */
923 #ifdef AFS_DARWIN80_ENV
924         if (tuiop) {
925             uio_free(tuiop);
926             tuiop = 0;
927         }
928 #endif
929     }
930
931     /* if we make it here with tdc non-zero, then it is the last chunk we
932      * dealt with, and we have to release it when we're done.  We hold on
933      * to it in case we need to do a prefetch, obviously.
934      */
935     if (tdc) {
936         ReleaseReadLock(&tdc->lock);
937 #if !defined(AFS_VM_RDWR_ENV)
938         /* try to queue prefetch, if needed */
939         if (!noLock) {
940             if (!(tdc->mflags & DFNextStarted))
941                 afs_PrefetchChunk(avc, tdc, acred, &treq);
942         }
943 #endif
944         afs_PutDCache(tdc);
945     }
946     if (!noLock)
947         ReleaseReadLock(&avc->lock);
948
949 #ifdef AFS_DARWIN80_ENV
950     if (tuiop)
951        uio_free(tuiop);
952 #else
953     osi_FreeSmallSpace(tvec);
954 #endif
955     error = afs_CheckCode(error, &treq, 13);
956     return error;
957 }