struct nfsmount *nmp;
struct nfsmount *nmp = VFSTONFS(vp->v_mount);
nfs_bioread(vp, uio, ioflag, cred)	register struct vnode *vp;	register struct uio *uio;	int ioflag;	struct ucred *cred;{	register struct nfsnode *np = VTONFS(vp);	register int biosize, diff;	struct buf *bp = NULL, *rabp;	struct vattr vattr;	struct proc *p;	struct nfsmount *nmp;	daddr_t lbn, bn, rabn;	caddr_t baddr;	int got_buf = 0, nra, error = 0, n = 0, on = 0, not_readin;#ifdef lint	ioflag = ioflag;#endif /* lint */#ifdef DIAGNOSTIC	if (uio->uio_rw != UIO_READ)		panic("nfs_read mode");#endif	if (uio->uio_resid == 0)		return (0);	if (uio->uio_offset < 0 && vp->v_type != VDIR)		return (EINVAL);	nmp = VFSTONFS(vp->v_mount);	biosize = nmp->nm_rsize;	p = uio->uio_procp;	/*	 * For nfs, cache consistency can only be maintained approximately.	 * Although RFC1094 does not specify the criteria, the following is	 * believed to be compatible with the reference port.	 * For nqnfs, full cache consistency is maintained within the loop.	 * For nfs:	 * If the file's modify time on the server has changed since the	 * last read rpc or you have written to the file,	 * you may have lost data cache consistency with the	 * server, so flush all of the file's data out of the cache.	 * Then force a getattr rpc to ensure that you have up to date	 * attributes.	 * The mount flag NFSMNT_MYWRITE says "Assume that my writes are	 * the ones changing the modify time.	 * NB: This implies that cache data can be read when up to	 * NFS_ATTRTIMEO seconds out of date. If you find that you need current	 * attributes this could be forced by setting n_attrstamp to 0 before	 * the VOP_GETATTR() call.	 */	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {		if (np->n_flag & NMODIFIED) {			if ((nmp->nm_flag & NFSMNT_MYWRITE) == 0 ||			     vp->v_type != VREG) {				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);				if (error)					return (error);			}			np->n_attrstamp = 0;			np->n_direofoffset = 0;			error = VOP_GETATTR(vp, &vattr, cred, p);			if (error)				return (error);			np->n_mtime = vattr.va_mtime.tv_sec;		} else {			if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0)				return (error);			if (np->n_mtime != vattr.va_mtime.tv_sec) {				np->n_direofoffset = 0;				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);				if (error)					return (error);				np->n_mtime = vattr.va_mtime.tv_sec;			}		}	}	do {	    /*	     * Get a valid lease. If cached data is stale, flush it.	     */	    if (nmp->nm_flag & NFSMNT_NQNFS) {		if (NQNFS_CKINVALID(vp, np, NQL_READ)) {		    do {			error = nqnfs_getlease(vp, NQL_READ, cred, p);		    } while (error == NQNFS_EXPIRED);		    if (error)			return (error);		    if (np->n_lrev != np->n_brev ||			(np->n_flag & NQNFSNONCACHE) ||			((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {			if (vp->v_type == VDIR) {			    np->n_direofoffset = 0;			    cache_purge(vp);			}			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);			if (error)			    return (error);			np->n_brev = np->n_lrev;		    }		} else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {		    np->n_direofoffset = 0;		    cache_purge(vp);		    error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);		    if (error)			return (error);		}	    }	    /*	     * Don't cache magic amd symlinks.	     */	    if (np->n_flag & NQNFSNONCACHE		|| ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {		switch (vp->v_type) {		case VREG:			error = nfs_readrpc(vp, uio, cred);			break;		case VLNK:			error = nfs_readlinkrpc(vp, uio, cred);			break;		case VDIR:			error = nfs_readdirrpc(vp, uio, cred);			break;		case VCHR:		case VSOCK:		case VFIFO:		case VBAD:		case VNON:		case VBLK:			break;		};		return (error);	    }	    baddr = (caddr_t)0;	    switch (vp->v_type) {	    case VREG:		nfsstats.biocache_reads++;		lbn = uio->uio_offset / biosize;		on = uio->uio_offset & (biosize-1);		bn = lbn * (biosize / DEV_BSIZE);		not_readin = 1;		/*		 * Start the read ahead(s), as required.		 */		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&		    lbn == vp->v_lastr + 1) {		    for (nra = 0; nra < nmp->nm_readahead &&			(lbn + 1 + nra) * biosize < np->n_size; nra++) {			rabn = (lbn + 1 + nra) * (biosize / DEV_BSIZE);			if (!incore(vp, rabn)) {			    rabp = nfs_getcacheblk(vp, rabn, biosize, p);			    if (!rabp)				return (EINTR);			    if ((rabp->b_flags & (B_DELWRI | B_DONE)) == 0) {				rabp->b_flags |= (B_READ | B_ASYNC);				if (nfs_asyncio(rabp, cred)) {				    rabp->b_flags |= B_INVAL;				    brelse(rabp);				}			    } else				brelse(rabp);			}		    }		}		/*		 * If the block is in the cache and has the required data		 * in a valid region, just copy it out.		 * Otherwise, get the block and write back/read in,		 * as required.		 */		if ((bp = incore(vp, bn)) &&		    (bp->b_flags & (B_BUSY | B_WRITEINPROG)) ==		    (B_BUSY | B_WRITEINPROG))			got_buf = 0;		else {again:			bp = nfs_getcacheblk(vp, bn, biosize, p);			if (!bp)				return (EINTR);			got_buf = 1;			if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0) {				bp->b_flags |= B_READ;				not_readin = 0;				error = nfs_doio(bp, cred, p);				if (error) {				    brelse(bp);				    return (error);				}			}		}		n = min((unsigned)(biosize - on), uio->uio_resid);		diff = np->n_size - uio->uio_offset;		if (diff < n)			n = diff;		if (not_readin && n > 0) {			if (on < bp->b_validoff || (on + n) > bp->b_validend) {				if (!got_buf) {				    bp = nfs_getcacheblk(vp, bn, biosize, p);				    if (!bp)					return (EINTR);				    got_buf = 1;				}				bp->b_flags |= B_INVAL;				if (bp->b_dirtyend > 0) {				    if ((bp->b_flags & B_DELWRI) == 0)					panic("nfsbioread");				    if (VOP_BWRITE(bp) == EINTR)					return (EINTR);				} else				    brelse(bp);				goto again;			}		}		vp->v_lastr = lbn;		diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on);		if (diff < n)			n = diff;		break;	    case VLNK:		nfsstats.biocache_readlinks++;		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);		if (!bp)			return (EINTR);		if ((bp->b_flags & B_DONE) == 0) {			bp->b_flags |= B_READ;			if ((error = nfs_doio(bp, cred, p)) != 0) {				brelse(bp);				return (error);			}		}		n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);		got_buf = 1;		on = 0;		break;	    case VDIR:		if (uio->uio_resid < NFS_DIRBLKSIZ)			return (0);		nfsstats.biocache_readdirs++;		bn = (daddr_t)uio->uio_offset;		bp = nfs_getcacheblk(vp, bn, NFS_DIRBLKSIZ, p);		if (!bp)			return (EINTR);		if ((bp->b_flags & B_DONE) == 0) {			bp->b_flags |= B_READ;			if ((error = nfs_doio(bp, cred, p)) != 0) {				brelse(bp);				return (error);			}		}		/*		 * If not eof and read aheads are enabled, start one.		 * (You need the current block first, so that you have the		 *  directory offset cookie of the next block.		 */		rabn = bp->b_blkno;		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&		    rabn != 0 && rabn != np->n_direofoffset &&		    !incore(vp, rabn)) {			rabp = nfs_getcacheblk(vp, rabn, NFS_DIRBLKSIZ, p);			if (rabp) {			    if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {				rabp->b_flags |= (B_READ | B_ASYNC);				if (nfs_asyncio(rabp, cred)) {				    rabp->b_flags |= B_INVAL;				    brelse(rabp);				}			    } else				brelse(rabp);			}		}		on = 0;		n = min(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid);		got_buf = 1;		break;	    case VBAD:	    case VSOCK:	    case VCHR:	    case VBLK:	    case VNON:	    case VFIFO:		break;	    };	    if (n > 0) {		if (!baddr)			baddr = bp->b_data;		error = uiomove(baddr + on, (int)n, uio);	    }	    switch (vp->v_type) {	    case VLNK:		n = 0;		break;	    case VDIR:		uio->uio_offset = bp->b_blkno;		break;	    case VREG:	    case VBAD:	    case VFIFO:	    case VSOCK:	    case VCHR:	    case VBLK:	    case VNON:		break;	    };	    if (got_buf)		brelse(bp);	} while (error == 0 && uio->uio_resid > 0 && n > 0);	return (error);}