⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nfs_bio.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 1989, 1993 *	The Regents of the University of California.  All rights reserved. * * This code is derived from software contributed to Berkeley by * Rick Macklem at The University of Guelph. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * *	@(#)nfs_bio.c	8.5 (Berkeley) 1/4/94 */#include <sys/param.h>#include <sys/systm.h>#include <sys/resourcevar.h>#include <sys/proc.h>#include <sys/buf.h>#include <sys/vnode.h>#include <sys/trace.h>#include <sys/mount.h>#include <sys/kernel.h>#include <vm/vm.h>#include <nfs/nfsnode.h>#include <nfs/rpcv2.h>#include <nfs/nfsv2.h>#include <nfs/nfs.h>#include <nfs/nfsmount.h>#include <nfs/nqnfs.h>struct buf *incore(), *nfs_getcacheblk();extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];extern int nfs_numasync;/* * Vnode op for read using bio * Any similarity to readip() is purely coincidental */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, *rabp;	struct vattr vattr;	struct proc *p;	struct nfsmount *nmp;	daddr_t lbn, bn, rabn;	caddr_t baddr;	int got_buf, nra, error = 0, n, on, 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) {				if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))					return (error);			}			np->n_attrstamp = 0;			np->n_direofoffset = 0;			if (error = VOP_GETATTR(vp, &vattr, cred, p))				return (error);			np->n_mtime = vattr.va_mtime.ts_sec;		} else {			if (error = VOP_GETATTR(vp, &vattr, cred, p))				return (error);			if (np->n_mtime != vattr.va_mtime.ts_sec) {				np->n_direofoffset = 0;				if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))					return (error);				np->n_mtime = vattr.va_mtime.ts_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);			}			if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))			    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);		    if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))			return (error);		}	    }	    if (np->n_flag & NQNFSNONCACHE) {		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;		};		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);				}			    }			}		    }		}		/*		 * 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;				if (error = nfs_doio(bp, cred, p)) {				    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)) {				brelse(bp);				return (error);			}		}		n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);		got_buf = 1;		on = 0;		break;	    case VDIR:		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)) {				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);				}			    }			}		}		on = 0;		n = min(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid);		got_buf = 1;		break;	    };	    if (n > 0) {		if (!baddr)			baddr = bp->b_data;		error = uiomove(baddr + on, (int)n, uio);	    }	    switch (vp->v_type) {	    case VREG:		if (n + on == biosize || uio->uio_offset == np->n_size)			bp->b_flags |= B_AGE;		break;	    case VLNK:		n = 0;		break;	    case VDIR:		uio->uio_offset = bp->b_blkno;		break;	    };	    if (got_buf)		brelse(bp);	} while (error == 0 && uio->uio_resid > 0 && n > 0);	return (error);}/* * Vnode op for write using bio */nfs_write(ap)	struct vop_write_args /* {		struct vnode *a_vp;		struct uio *a_uio;		int  a_ioflag;		struct ucred *a_cred;	} */ *ap;{	register int biosize;	register struct uio *uio = ap->a_uio;	struct proc *p = uio->uio_procp;	register struct vnode *vp = ap->a_vp;	struct nfsnode *np = VTONFS(vp);	register struct ucred *cred = ap->a_cred;	int ioflag = ap->a_ioflag;	struct buf *bp;	struct vattr vattr;	struct nfsmount *nmp;	daddr_t lbn, bn;	int n, on, error = 0;#ifdef DIAGNOSTIC	if (uio->uio_rw != UIO_WRITE)		panic("nfs_write mode");	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)		panic("nfs_write proc");#endif	if (vp->v_type != VREG)		return (EIO);	if (np->n_flag & NWRITEERR) {		np->n_flag &= ~NWRITEERR;		return (np->n_error);	}	if (ioflag & (IO_APPEND | IO_SYNC)) {		if (np->n_flag & NMODIFIED) {			np->n_attrstamp = 0;			if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))				return (error);		}		if (ioflag & IO_APPEND) {			np->n_attrstamp = 0;			if (error = VOP_GETATTR(vp, &vattr, cred, p))				return (error);			uio->uio_offset = np->n_size;		}	}	nmp = VFSTONFS(vp->v_mount);	if (uio->uio_offset < 0)		return (EINVAL);	if (uio->uio_resid == 0)		return (0);	/*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -