Home | History | Annotate | Line # | Download | only in nfs
nfs_bio.c revision 1.63.2.3
      1  1.63.2.3   nathanw /*	$NetBSD: nfs_bio.c,v 1.63.2.3 2001/06/21 20:09:31 nathanw Exp $	*/
      2      1.15       cgd 
      3       1.1       cgd /*
      4      1.12   mycroft  * Copyright (c) 1989, 1993
      5      1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * Rick Macklem at The University of Guelph.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19       1.1       cgd  *    must display the following acknowledgement:
     20       1.1       cgd  *	This product includes software developed by the University of
     21       1.1       cgd  *	California, Berkeley and its contributors.
     22       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23       1.1       cgd  *    may be used to endorse or promote products derived from this software
     24       1.1       cgd  *    without specific prior written permission.
     25       1.1       cgd  *
     26       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36       1.1       cgd  * SUCH DAMAGE.
     37       1.1       cgd  *
     38      1.24      fvdl  *	@(#)nfs_bio.c	8.9 (Berkeley) 3/30/95
     39       1.1       cgd  */
     40       1.1       cgd 
     41      1.51     bjh21 #include "opt_nfs.h"
     42      1.54       chs #include "opt_ddb.h"
     43      1.51     bjh21 
     44       1.8   mycroft #include <sys/param.h>
     45       1.8   mycroft #include <sys/systm.h>
     46      1.12   mycroft #include <sys/resourcevar.h>
     47      1.24      fvdl #include <sys/signalvar.h>
     48  1.63.2.1   nathanw #include <sys/lwp.h>
     49       1.8   mycroft #include <sys/proc.h>
     50       1.8   mycroft #include <sys/buf.h>
     51       1.8   mycroft #include <sys/vnode.h>
     52       1.8   mycroft #include <sys/mount.h>
     53      1.12   mycroft #include <sys/kernel.h>
     54      1.23  christos #include <sys/namei.h>
     55      1.34      fvdl #include <sys/dirent.h>
     56      1.54       chs #include <sys/malloc.h>
     57       1.1       cgd 
     58      1.41       mrg #include <uvm/uvm_extern.h>
     59      1.54       chs #include <uvm/uvm.h>
     60      1.41       mrg 
     61      1.12   mycroft #include <nfs/rpcv2.h>
     62      1.24      fvdl #include <nfs/nfsproto.h>
     63       1.8   mycroft #include <nfs/nfs.h>
     64       1.8   mycroft #include <nfs/nfsmount.h>
     65      1.12   mycroft #include <nfs/nqnfs.h>
     66      1.24      fvdl #include <nfs/nfsnode.h>
     67      1.23  christos #include <nfs/nfs_var.h>
     68       1.1       cgd 
     69      1.12   mycroft extern int nfs_numasync;
     70      1.24      fvdl extern struct nfsstats nfsstats;
     71       1.1       cgd 
     72       1.1       cgd /*
     73       1.1       cgd  * Vnode op for read using bio
     74       1.1       cgd  * Any similarity to readip() is purely coincidental
     75       1.1       cgd  */
     76      1.23  christos int
     77      1.34      fvdl nfs_bioread(vp, uio, ioflag, cred, cflag)
     78      1.48  augustss 	struct vnode *vp;
     79      1.48  augustss 	struct uio *uio;
     80      1.34      fvdl 	int ioflag, cflag;
     81       1.1       cgd 	struct ucred *cred;
     82       1.1       cgd {
     83      1.48  augustss 	struct nfsnode *np = VTONFS(vp);
     84      1.54       chs 	int biosize;
     85      1.23  christos 	struct buf *bp = NULL, *rabp;
     86       1.1       cgd 	struct vattr vattr;
     87      1.12   mycroft 	struct proc *p;
     88      1.24      fvdl 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
     89      1.35      fvdl 	struct nfsdircache *ndp = NULL, *nndp = NULL;
     90      1.34      fvdl 	caddr_t baddr, ep, edp;
     91      1.54       chs 	int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
     92      1.34      fvdl 	int enough = 0;
     93      1.34      fvdl 	struct dirent *dp, *pdp;
     94      1.54       chs 	off_t curoff = 0;
     95       1.1       cgd 
     96       1.1       cgd #ifdef DIAGNOSTIC
     97       1.1       cgd 	if (uio->uio_rw != UIO_READ)
     98       1.1       cgd 		panic("nfs_read mode");
     99       1.1       cgd #endif
    100       1.1       cgd 	if (uio->uio_resid == 0)
    101       1.1       cgd 		return (0);
    102      1.34      fvdl 	if (vp->v_type != VDIR && uio->uio_offset < 0)
    103       1.1       cgd 		return (EINVAL);
    104      1.24      fvdl 	p = uio->uio_procp;
    105      1.51     bjh21 #ifndef NFS_V2_ONLY
    106      1.34      fvdl 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
    107      1.34      fvdl 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
    108      1.24      fvdl 		(void)nfs_fsinfo(nmp, vp, cred, p);
    109      1.51     bjh21 #endif
    110      1.34      fvdl 	if (vp->v_type != VDIR &&
    111      1.34      fvdl 	    (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
    112      1.33      fvdl 		return (EFBIG);
    113      1.12   mycroft 	biosize = nmp->nm_rsize;
    114      1.54       chs 
    115       1.1       cgd 	/*
    116      1.12   mycroft 	 * For nfs, cache consistency can only be maintained approximately.
    117      1.12   mycroft 	 * Although RFC1094 does not specify the criteria, the following is
    118      1.12   mycroft 	 * believed to be compatible with the reference port.
    119      1.12   mycroft 	 * For nqnfs, full cache consistency is maintained within the loop.
    120      1.12   mycroft 	 * For nfs:
    121       1.1       cgd 	 * If the file's modify time on the server has changed since the
    122       1.1       cgd 	 * last read rpc or you have written to the file,
    123       1.1       cgd 	 * you may have lost data cache consistency with the
    124       1.1       cgd 	 * server, so flush all of the file's data out of the cache.
    125       1.1       cgd 	 * Then force a getattr rpc to ensure that you have up to date
    126       1.1       cgd 	 * attributes.
    127       1.1       cgd 	 * NB: This implies that cache data can be read when up to
    128       1.1       cgd 	 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
    129       1.1       cgd 	 * attributes this could be forced by setting n_attrstamp to 0 before
    130      1.12   mycroft 	 * the VOP_GETATTR() call.
    131       1.1       cgd 	 */
    132      1.54       chs 
    133      1.12   mycroft 	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
    134       1.1       cgd 		if (np->n_flag & NMODIFIED) {
    135      1.24      fvdl 			if (vp->v_type != VREG) {
    136      1.24      fvdl 				if (vp->v_type != VDIR)
    137      1.24      fvdl 					panic("nfs: bioread, not dir");
    138      1.35      fvdl 				nfs_invaldircache(vp, 0);
    139      1.35      fvdl 				np->n_direofoffset = 0;
    140      1.23  christos 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    141      1.23  christos 				if (error)
    142      1.12   mycroft 					return (error);
    143      1.12   mycroft 			}
    144       1.1       cgd 			np->n_attrstamp = 0;
    145      1.23  christos 			error = VOP_GETATTR(vp, &vattr, cred, p);
    146      1.23  christos 			if (error)
    147       1.1       cgd 				return (error);
    148      1.22       jtc 			np->n_mtime = vattr.va_mtime.tv_sec;
    149       1.1       cgd 		} else {
    150      1.24      fvdl 			error = VOP_GETATTR(vp, &vattr, cred, p);
    151      1.24      fvdl 			if (error)
    152       1.1       cgd 				return (error);
    153      1.22       jtc 			if (np->n_mtime != vattr.va_mtime.tv_sec) {
    154      1.35      fvdl 				if (vp->v_type == VDIR) {
    155      1.35      fvdl 					nfs_invaldircache(vp, 0);
    156      1.35      fvdl 					np->n_direofoffset = 0;
    157      1.35      fvdl 				}
    158      1.23  christos 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    159      1.23  christos 				if (error)
    160      1.12   mycroft 					return (error);
    161      1.22       jtc 				np->n_mtime = vattr.va_mtime.tv_sec;
    162       1.1       cgd 			}
    163       1.1       cgd 		}
    164       1.1       cgd 	}
    165      1.54       chs 
    166      1.54       chs 	/*
    167      1.54       chs 	 * update the cached read creds for this node.
    168      1.54       chs 	 */
    169      1.54       chs 
    170      1.54       chs 	if (np->n_rcred) {
    171      1.54       chs 		crfree(np->n_rcred);
    172      1.54       chs 	}
    173      1.54       chs 	np->n_rcred = cred;
    174      1.54       chs 	crhold(cred);
    175      1.54       chs 
    176       1.1       cgd 	do {
    177      1.53     bjh21 #ifndef NFS_V2_ONLY
    178      1.12   mycroft 	    /*
    179      1.12   mycroft 	     * Get a valid lease. If cached data is stale, flush it.
    180      1.12   mycroft 	     */
    181      1.12   mycroft 	    if (nmp->nm_flag & NFSMNT_NQNFS) {
    182      1.24      fvdl 		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
    183      1.12   mycroft 		    do {
    184      1.24      fvdl 			error = nqnfs_getlease(vp, ND_READ, cred, p);
    185      1.12   mycroft 		    } while (error == NQNFS_EXPIRED);
    186      1.12   mycroft 		    if (error)
    187      1.12   mycroft 			return (error);
    188      1.12   mycroft 		    if (np->n_lrev != np->n_brev ||
    189      1.12   mycroft 			(np->n_flag & NQNFSNONCACHE) ||
    190      1.12   mycroft 			((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
    191      1.35      fvdl 			if (vp->v_type == VDIR) {
    192      1.35      fvdl 				nfs_invaldircache(vp, 0);
    193      1.35      fvdl 				np->n_direofoffset = 0;
    194      1.35      fvdl 			}
    195      1.23  christos 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    196      1.23  christos 			if (error)
    197      1.12   mycroft 			    return (error);
    198      1.12   mycroft 			np->n_brev = np->n_lrev;
    199      1.12   mycroft 		    }
    200      1.12   mycroft 		} else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
    201      1.35      fvdl 		    nfs_invaldircache(vp, 0);
    202      1.23  christos 		    error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    203      1.35      fvdl 		    np->n_direofoffset = 0;
    204      1.23  christos 		    if (error)
    205      1.12   mycroft 			return (error);
    206      1.12   mycroft 		}
    207      1.12   mycroft 	    }
    208      1.53     bjh21 #endif
    209      1.26      fvdl 	    /*
    210      1.26      fvdl 	     * Don't cache symlinks.
    211      1.26      fvdl 	     */
    212      1.26      fvdl 	    if (np->n_flag & NQNFSNONCACHE
    213      1.26      fvdl 		|| ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
    214      1.12   mycroft 		switch (vp->v_type) {
    215      1.12   mycroft 		case VREG:
    216      1.54       chs 			return (nfs_readrpc(vp, uio));
    217      1.12   mycroft 		case VLNK:
    218      1.24      fvdl 			return (nfs_readlinkrpc(vp, uio, cred));
    219      1.12   mycroft 		case VDIR:
    220      1.23  christos 			break;
    221      1.24      fvdl 		default:
    222      1.29  christos 			printf(" NQNFSNONCACHE: type %x unexpected\n",
    223      1.28  christos 			    vp->v_type);
    224      1.12   mycroft 		};
    225      1.12   mycroft 	    }
    226      1.12   mycroft 	    baddr = (caddr_t)0;
    227       1.1       cgd 	    switch (vp->v_type) {
    228       1.1       cgd 	    case VREG:
    229       1.1       cgd 		nfsstats.biocache_reads++;
    230      1.12   mycroft 
    231      1.54       chs 		error = 0;
    232  1.63.2.3   nathanw 		if (uio->uio_offset >= np->n_size) {
    233  1.63.2.3   nathanw 			break;
    234  1.63.2.3   nathanw 		}
    235      1.54       chs 		while (uio->uio_resid > 0) {
    236      1.54       chs 			void *win;
    237      1.61       chs 			vsize_t bytelen = MIN(np->n_size - uio->uio_offset,
    238      1.54       chs 					      uio->uio_resid);
    239      1.54       chs 
    240      1.54       chs 			if (bytelen == 0)
    241      1.54       chs 				break;
    242      1.54       chs 			win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset,
    243      1.54       chs 					&bytelen, UBC_READ);
    244      1.54       chs 			error = uiomove(win, bytelen, uio);
    245      1.54       chs 			ubc_release(win, 0);
    246      1.54       chs 			if (error) {
    247      1.54       chs 				break;
    248      1.12   mycroft 			}
    249      1.12   mycroft 		}
    250      1.54       chs 		n = 0;
    251      1.54       chs 		break;
    252      1.12   mycroft 
    253       1.1       cgd 	    case VLNK:
    254       1.1       cgd 		nfsstats.biocache_readlinks++;
    255      1.12   mycroft 		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
    256      1.12   mycroft 		if (!bp)
    257      1.12   mycroft 			return (EINTR);
    258      1.12   mycroft 		if ((bp->b_flags & B_DONE) == 0) {
    259      1.12   mycroft 			bp->b_flags |= B_READ;
    260      1.54       chs 			error = nfs_doio(bp, p);
    261      1.24      fvdl 			if (error) {
    262      1.12   mycroft 				brelse(bp);
    263      1.12   mycroft 				return (error);
    264      1.12   mycroft 			}
    265      1.12   mycroft 		}
    266      1.63       chs 		n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
    267      1.12   mycroft 		got_buf = 1;
    268       1.1       cgd 		on = 0;
    269       1.1       cgd 		break;
    270       1.1       cgd 	    case VDIR:
    271      1.34      fvdl diragain:
    272      1.34      fvdl 		nfsstats.biocache_readdirs++;
    273      1.35      fvdl 		ndp = nfs_searchdircache(vp, uio->uio_offset,
    274      1.35      fvdl 			(nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
    275      1.35      fvdl 		if (!ndp) {
    276      1.35      fvdl 			/*
    277      1.35      fvdl 			 * We've been handed a cookie that is not
    278      1.35      fvdl 			 * in the cache. If we're not translating
    279      1.35      fvdl 			 * 32 <-> 64, it may be a value that was
    280      1.35      fvdl 			 * flushed out of the cache because it grew
    281      1.35      fvdl 			 * too big. Let the server judge if it's
    282      1.35      fvdl 			 * valid or not. In the translation case,
    283      1.35      fvdl 			 * we have no way of validating this value,
    284      1.35      fvdl 			 * so punt.
    285      1.35      fvdl 			 */
    286      1.35      fvdl 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
    287      1.35      fvdl 				return (EINVAL);
    288      1.35      fvdl 			ndp = nfs_enterdircache(vp, uio->uio_offset,
    289      1.35      fvdl 				uio->uio_offset, 0, 0);
    290      1.35      fvdl 		}
    291      1.35      fvdl 
    292      1.34      fvdl 		if (uio->uio_offset != 0 &&
    293      1.35      fvdl 		    ndp->dc_cookie == np->n_direofoffset) {
    294      1.35      fvdl 			nfsstats.direofcache_hits++;
    295      1.18   mycroft 			return (0);
    296      1.35      fvdl 		}
    297      1.35      fvdl 
    298      1.34      fvdl 		bp = nfs_getcacheblk(vp, ndp->dc_blkno, NFS_DIRBLKSIZ, p);
    299      1.12   mycroft 		if (!bp)
    300      1.24      fvdl 		    return (EINTR);
    301      1.12   mycroft 		if ((bp->b_flags & B_DONE) == 0) {
    302      1.24      fvdl 		    bp->b_flags |= B_READ;
    303      1.35      fvdl 		    bp->b_dcookie = ndp->dc_blkcookie;
    304      1.54       chs 		    error = nfs_doio(bp, p);
    305      1.24      fvdl 		    if (error) {
    306      1.34      fvdl 			/*
    307      1.34      fvdl 			 * Yuck! The directory has been modified on the
    308      1.34      fvdl 			 * server. Punt and let the userland code
    309      1.34      fvdl 			 * deal with it.
    310      1.34      fvdl 			 */
    311      1.24      fvdl 			brelse(bp);
    312      1.34      fvdl 			if (error == NFSERR_BAD_COOKIE) {
    313      1.35      fvdl 			    nfs_invaldircache(vp, 0);
    314      1.34      fvdl 			    nfs_vinvalbuf(vp, 0, cred, p, 1);
    315      1.34      fvdl 			    error = EINVAL;
    316      1.12   mycroft 			}
    317      1.34      fvdl 			return (error);
    318      1.38      fvdl 		    }
    319      1.40      fvdl 		}
    320      1.40      fvdl 
    321      1.40      fvdl 		/*
    322      1.40      fvdl 		 * Just return if we hit EOF right away with this
    323      1.40      fvdl 		 * block. Always check here, because direofoffset
    324      1.40      fvdl 		 * may have been set by an nfsiod since the last
    325      1.40      fvdl 		 * check.
    326      1.40      fvdl 		 */
    327      1.40      fvdl 		if (np->n_direofoffset != 0 &&
    328      1.39      fvdl 			ndp->dc_blkcookie == np->n_direofoffset) {
    329      1.40      fvdl 			brelse(bp);
    330      1.40      fvdl 			return (0);
    331      1.12   mycroft 		}
    332      1.12   mycroft 
    333      1.12   mycroft 		/*
    334      1.34      fvdl 		 * Find the entry we were looking for in the block.
    335      1.34      fvdl 		 */
    336      1.34      fvdl 
    337      1.34      fvdl 		en = ndp->dc_entry;
    338      1.34      fvdl 
    339      1.34      fvdl 		pdp = dp = (struct dirent *)bp->b_data;
    340  1.63.2.2   nathanw 		edp = bp->b_data + bp->b_bcount - bp->b_resid;
    341      1.34      fvdl 		enn = 0;
    342      1.34      fvdl 		while (enn < en && (caddr_t)dp < edp) {
    343      1.34      fvdl 			pdp = dp;
    344      1.34      fvdl 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
    345      1.34      fvdl 			enn++;
    346      1.34      fvdl 		}
    347      1.34      fvdl 
    348      1.34      fvdl 		/*
    349      1.34      fvdl 		 * If the entry number was bigger than the number of
    350      1.34      fvdl 		 * entries in the block, or the cookie of the previous
    351      1.34      fvdl 		 * entry doesn't match, the directory cache is
    352      1.34      fvdl 		 * stale. Flush it and try again (i.e. go to
    353      1.34      fvdl 		 * the server).
    354      1.34      fvdl 		 */
    355      1.34      fvdl 		if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
    356      1.35      fvdl 		    (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
    357      1.34      fvdl #ifdef DEBUG
    358      1.37   thorpej 		    	printf("invalid cache: %p %p %p off %lx %lx\n",
    359      1.37   thorpej 				pdp, dp, edp,
    360      1.34      fvdl 				(unsigned long)uio->uio_offset,
    361      1.34      fvdl 				(unsigned long)NFS_GETCOOKIE(pdp));
    362      1.34      fvdl #endif
    363      1.34      fvdl 			brelse(bp);
    364      1.35      fvdl 			nfs_invaldircache(vp, 0);
    365      1.34      fvdl 			nfs_vinvalbuf(vp, 0, cred, p, 0);
    366      1.34      fvdl 			goto diragain;
    367      1.34      fvdl 		}
    368      1.34      fvdl 
    369      1.34      fvdl 		on = (caddr_t)dp - bp->b_data;
    370      1.34      fvdl 
    371      1.34      fvdl 		/*
    372      1.34      fvdl 		 * Cache all entries that may be exported to the
    373      1.34      fvdl 		 * user, as they may be thrown back at us. The
    374      1.34      fvdl 		 * NFSBIO_CACHECOOKIES flag indicates that all
    375      1.34      fvdl 		 * entries are being 'exported', so cache them all.
    376      1.34      fvdl 		 */
    377      1.34      fvdl 
    378      1.34      fvdl 		if (en == 0 && pdp == dp) {
    379      1.34      fvdl 			dp = (struct dirent *)
    380      1.34      fvdl 			    ((caddr_t)dp + dp->d_reclen);
    381      1.34      fvdl 			enn++;
    382      1.34      fvdl 		}
    383      1.34      fvdl 
    384  1.63.2.2   nathanw 		if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
    385      1.34      fvdl 			n = uio->uio_resid;
    386      1.34      fvdl 			enough = 1;
    387      1.34      fvdl 		} else
    388  1.63.2.2   nathanw 			n = bp->b_bcount - bp->b_resid - on;
    389      1.34      fvdl 
    390      1.34      fvdl 		ep = bp->b_data + on + n;
    391      1.34      fvdl 
    392      1.34      fvdl 		/*
    393      1.34      fvdl 		 * Find last complete entry to copy, caching entries
    394      1.34      fvdl 		 * (if requested) as we go.
    395      1.34      fvdl 		 */
    396      1.34      fvdl 
    397      1.34      fvdl 		while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
    398      1.35      fvdl 			if (cflag & NFSBIO_CACHECOOKIES) {
    399      1.35      fvdl 				nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
    400      1.35      fvdl 				    ndp->dc_blkcookie, enn, bp->b_lblkno);
    401      1.35      fvdl 				if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    402      1.35      fvdl 					NFS_STASHCOOKIE32(pdp,
    403      1.35      fvdl 					    nndp->dc_cookie32);
    404      1.35      fvdl 				}
    405      1.35      fvdl 			}
    406      1.34      fvdl 			pdp = dp;
    407      1.34      fvdl 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
    408      1.34      fvdl 			enn++;
    409      1.34      fvdl 		}
    410      1.34      fvdl 
    411      1.34      fvdl 		/*
    412      1.34      fvdl 		 * If the last requested entry was not the last in the
    413      1.34      fvdl 		 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
    414      1.34      fvdl 		 * cache the cookie of the last requested one, and
    415      1.34      fvdl 		 * set of the offset to it.
    416      1.34      fvdl 		 */
    417      1.34      fvdl 
    418  1.63.2.2   nathanw 		if ((on + n) < bp->b_bcount - bp->b_resid) {
    419      1.34      fvdl 			curoff = NFS_GETCOOKIE(pdp);
    420      1.35      fvdl 			nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
    421      1.35      fvdl 			    enn, bp->b_lblkno);
    422      1.35      fvdl 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    423      1.35      fvdl 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
    424      1.35      fvdl 				curoff = nndp->dc_cookie32;
    425      1.35      fvdl 			}
    426      1.34      fvdl 		} else
    427      1.34      fvdl 			curoff = bp->b_dcookie;
    428      1.34      fvdl 
    429      1.35      fvdl 		/*
    430      1.35      fvdl 		 * Always cache the entry for the next block,
    431      1.35      fvdl 		 * so that readaheads can use it.
    432      1.35      fvdl 		 */
    433      1.35      fvdl 		nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
    434      1.35      fvdl 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    435      1.35      fvdl 			if (curoff == bp->b_dcookie) {
    436      1.35      fvdl 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
    437      1.35      fvdl 				curoff = nndp->dc_cookie32;
    438      1.35      fvdl 			}
    439      1.35      fvdl 		}
    440      1.35      fvdl 
    441      1.34      fvdl 		n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
    442      1.34      fvdl 
    443      1.34      fvdl 		/*
    444      1.12   mycroft 		 * If not eof and read aheads are enabled, start one.
    445      1.12   mycroft 		 * (You need the current block first, so that you have the
    446      1.24      fvdl 		 *  directory offset cookie of the next block.)
    447      1.12   mycroft 		 */
    448      1.12   mycroft 		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
    449      1.34      fvdl 		    np->n_direofoffset == 0 && !(np->n_flag & NQNFSNONCACHE)) {
    450      1.35      fvdl 			rabp = nfs_getcacheblk(vp, nndp->dc_blkno,
    451      1.34      fvdl 						NFS_DIRBLKSIZ, p);
    452      1.12   mycroft 			if (rabp) {
    453      1.12   mycroft 			    if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
    454      1.35      fvdl 				rabp->b_dcookie = nndp->dc_cookie;
    455      1.12   mycroft 				rabp->b_flags |= (B_READ | B_ASYNC);
    456      1.54       chs 				if (nfs_asyncio(rabp)) {
    457      1.12   mycroft 				    rabp->b_flags |= B_INVAL;
    458      1.12   mycroft 				    brelse(rabp);
    459      1.12   mycroft 				}
    460      1.19   mycroft 			    } else
    461      1.19   mycroft 				brelse(rabp);
    462      1.12   mycroft 			}
    463      1.12   mycroft 		}
    464      1.12   mycroft 		got_buf = 1;
    465       1.1       cgd 		break;
    466      1.24      fvdl 	    default:
    467      1.29  christos 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
    468      1.23  christos 		break;
    469      1.54       chs 	    }
    470      1.12   mycroft 
    471      1.12   mycroft 	    if (n > 0) {
    472      1.12   mycroft 		if (!baddr)
    473      1.12   mycroft 			baddr = bp->b_data;
    474      1.12   mycroft 		error = uiomove(baddr + on, (int)n, uio);
    475       1.1       cgd 	    }
    476       1.1       cgd 	    switch (vp->v_type) {
    477      1.24      fvdl 	    case VREG:
    478      1.24      fvdl 		break;
    479       1.1       cgd 	    case VLNK:
    480       1.1       cgd 		n = 0;
    481       1.1       cgd 		break;
    482       1.1       cgd 	    case VDIR:
    483      1.24      fvdl 		if (np->n_flag & NQNFSNONCACHE)
    484      1.24      fvdl 			bp->b_flags |= B_INVAL;
    485      1.34      fvdl 		uio->uio_offset = curoff;
    486      1.34      fvdl 		if (enough)
    487      1.34      fvdl 			n = 0;
    488       1.1       cgd 		break;
    489      1.24      fvdl 	    default:
    490      1.29  christos 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
    491      1.24      fvdl 	    }
    492      1.12   mycroft 	    if (got_buf)
    493      1.12   mycroft 		brelse(bp);
    494      1.12   mycroft 	} while (error == 0 && uio->uio_resid > 0 && n > 0);
    495       1.1       cgd 	return (error);
    496       1.1       cgd }
    497       1.1       cgd 
    498       1.1       cgd /*
    499       1.1       cgd  * Vnode op for write using bio
    500       1.1       cgd  */
    501      1.23  christos int
    502      1.23  christos nfs_write(v)
    503      1.23  christos 	void *v;
    504      1.23  christos {
    505      1.12   mycroft 	struct vop_write_args /* {
    506      1.24      fvdl 		struct vnode *a_vp;
    507      1.12   mycroft 		struct uio *a_uio;
    508      1.12   mycroft 		int  a_ioflag;
    509      1.12   mycroft 		struct ucred *a_cred;
    510      1.23  christos 	} */ *ap = v;
    511      1.48  augustss 	struct uio *uio = ap->a_uio;
    512       1.1       cgd 	struct proc *p = uio->uio_procp;
    513      1.48  augustss 	struct vnode *vp = ap->a_vp;
    514      1.12   mycroft 	struct nfsnode *np = VTONFS(vp);
    515      1.48  augustss 	struct ucred *cred = ap->a_cred;
    516      1.12   mycroft 	int ioflag = ap->a_ioflag;
    517       1.1       cgd 	struct vattr vattr;
    518      1.24      fvdl 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    519      1.54       chs 	int error = 0, iomode, must_commit;
    520      1.54       chs 	int rv;
    521       1.1       cgd 
    522       1.1       cgd #ifdef DIAGNOSTIC
    523       1.1       cgd 	if (uio->uio_rw != UIO_WRITE)
    524       1.1       cgd 		panic("nfs_write mode");
    525  1.63.2.1   nathanw 	if (uio->uio_segflg == UIO_USERSPACE &&
    526  1.63.2.1   nathanw 	    uio->uio_procp != curproc->l_proc)
    527       1.1       cgd 		panic("nfs_write proc");
    528       1.1       cgd #endif
    529       1.1       cgd 	if (vp->v_type != VREG)
    530       1.1       cgd 		return (EIO);
    531      1.12   mycroft 	if (np->n_flag & NWRITEERR) {
    532      1.12   mycroft 		np->n_flag &= ~NWRITEERR;
    533      1.12   mycroft 		return (np->n_error);
    534      1.12   mycroft 	}
    535      1.51     bjh21 #ifndef NFS_V2_ONLY
    536      1.34      fvdl 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
    537      1.34      fvdl 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
    538      1.24      fvdl 		(void)nfs_fsinfo(nmp, vp, cred, p);
    539      1.51     bjh21 #endif
    540       1.1       cgd 	if (ioflag & (IO_APPEND | IO_SYNC)) {
    541       1.1       cgd 		if (np->n_flag & NMODIFIED) {
    542      1.12   mycroft 			np->n_attrstamp = 0;
    543      1.23  christos 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    544      1.23  christos 			if (error)
    545      1.12   mycroft 				return (error);
    546       1.1       cgd 		}
    547       1.1       cgd 		if (ioflag & IO_APPEND) {
    548       1.1       cgd 			np->n_attrstamp = 0;
    549      1.23  christos 			error = VOP_GETATTR(vp, &vattr, cred, p);
    550      1.23  christos 			if (error)
    551       1.1       cgd 				return (error);
    552       1.1       cgd 			uio->uio_offset = np->n_size;
    553       1.1       cgd 		}
    554       1.1       cgd 	}
    555       1.1       cgd 	if (uio->uio_offset < 0)
    556       1.1       cgd 		return (EINVAL);
    557      1.33      fvdl 	if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
    558      1.33      fvdl 		return (EFBIG);
    559       1.1       cgd 	if (uio->uio_resid == 0)
    560       1.1       cgd 		return (0);
    561       1.1       cgd 	/*
    562       1.1       cgd 	 * Maybe this should be above the vnode op call, but so long as
    563       1.1       cgd 	 * file servers have no limits, i don't think it matters
    564       1.1       cgd 	 */
    565      1.12   mycroft 	if (p && uio->uio_offset + uio->uio_resid >
    566       1.1       cgd 	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    567       1.1       cgd 		psignal(p, SIGXFSZ);
    568       1.1       cgd 		return (EFBIG);
    569       1.1       cgd 	}
    570      1.54       chs 
    571       1.1       cgd 	/*
    572      1.54       chs 	 * update the cached write creds for this node.
    573       1.1       cgd 	 */
    574      1.54       chs 
    575      1.54       chs 	if (np->n_wcred) {
    576      1.54       chs 		crfree(np->n_wcred);
    577      1.54       chs 	}
    578      1.54       chs 	np->n_wcred = cred;
    579      1.54       chs 	crhold(cred);
    580      1.54       chs 
    581      1.54       chs 	if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
    582      1.54       chs 		iomode = NFSV3WRITE_FILESYNC;
    583      1.54       chs 		error = nfs_writerpc(vp, uio, &iomode, &must_commit);
    584      1.54       chs 		if (must_commit)
    585      1.54       chs 			nfs_clearcommit(vp->v_mount);
    586      1.54       chs 		return (error);
    587      1.54       chs 	}
    588      1.54       chs 
    589       1.1       cgd 	do {
    590      1.54       chs 		void *win;
    591      1.54       chs 		voff_t oldoff = uio->uio_offset;
    592      1.54       chs 		vsize_t bytelen = uio->uio_resid;
    593      1.12   mycroft 
    594      1.53     bjh21 #ifndef NFS_V2_ONLY
    595      1.12   mycroft 		/*
    596      1.12   mycroft 		 * Check for a valid write lease.
    597      1.12   mycroft 		 */
    598      1.12   mycroft 		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
    599      1.24      fvdl 		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
    600      1.12   mycroft 			do {
    601      1.24      fvdl 				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
    602      1.12   mycroft 			} while (error == NQNFS_EXPIRED);
    603      1.12   mycroft 			if (error)
    604      1.12   mycroft 				return (error);
    605      1.12   mycroft 			if (np->n_lrev != np->n_brev ||
    606      1.12   mycroft 			    (np->n_flag & NQNFSNONCACHE)) {
    607      1.23  christos 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    608      1.23  christos 				if (error)
    609      1.12   mycroft 					return (error);
    610      1.12   mycroft 				np->n_brev = np->n_lrev;
    611      1.12   mycroft 			}
    612      1.12   mycroft 		}
    613      1.53     bjh21 #endif
    614       1.1       cgd 		nfsstats.biocache_writes++;
    615      1.54       chs 
    616      1.12   mycroft 		np->n_flag |= NMODIFIED;
    617      1.54       chs 		if (np->n_size < uio->uio_offset + bytelen) {
    618      1.54       chs 			np->n_size = uio->uio_offset + bytelen;
    619      1.41       mrg 			uvm_vnp_setsize(vp, np->n_size);
    620      1.12   mycroft 		}
    621      1.54       chs 		win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset, &bytelen,
    622      1.54       chs 				UBC_WRITE);
    623      1.54       chs 		error = uiomove(win, bytelen, uio);
    624      1.54       chs 		ubc_release(win, 0);
    625      1.54       chs 		rv = 1;
    626      1.54       chs 		if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
    627      1.54       chs 			simple_lock(&vp->v_uvm.u_obj.vmobjlock);
    628      1.54       chs 			rv = vp->v_uvm.u_obj.pgops->pgo_flush(
    629      1.54       chs 			    &vp->v_uvm.u_obj,
    630      1.54       chs 			    oldoff & ~(nmp->nm_wsize - 1),
    631      1.54       chs 			    uio->uio_offset & ~(nmp->nm_wsize - 1),
    632      1.54       chs 			    PGO_CLEANIT|PGO_SYNCIO);
    633      1.54       chs 			simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    634      1.54       chs 		} else if ((oldoff & ~(nmp->nm_wsize - 1)) !=
    635      1.54       chs 		    (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
    636      1.54       chs 			simple_lock(&vp->v_uvm.u_obj.vmobjlock);
    637      1.54       chs 			rv = vp->v_uvm.u_obj.pgops->pgo_flush(
    638      1.54       chs 			    &vp->v_uvm.u_obj,
    639      1.54       chs 			    oldoff & ~(nmp->nm_wsize - 1),
    640      1.54       chs 			    uio->uio_offset & ~(nmp->nm_wsize - 1),
    641      1.54       chs 			    PGO_CLEANIT|PGO_WEAK);
    642      1.54       chs 			simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    643       1.1       cgd 		}
    644      1.54       chs 		if (!rv) {
    645      1.54       chs 			error = EIO;
    646      1.58       chs 		}
    647      1.58       chs 		if (error) {
    648      1.54       chs 			break;
    649      1.52      fvdl 		}
    650      1.54       chs 	} while (uio->uio_resid > 0);
    651      1.54       chs 	return error;
    652      1.12   mycroft }
    653      1.12   mycroft 
    654      1.12   mycroft /*
    655      1.12   mycroft  * Get an nfs cache block.
    656      1.12   mycroft  * Allocate a new one if the block isn't currently in the cache
    657      1.12   mycroft  * and return the block marked busy. If the calling process is
    658      1.12   mycroft  * interrupted by a signal for an interruptible mount point, return
    659      1.12   mycroft  * NULL.
    660      1.12   mycroft  */
    661      1.12   mycroft struct buf *
    662      1.12   mycroft nfs_getcacheblk(vp, bn, size, p)
    663      1.12   mycroft 	struct vnode *vp;
    664      1.12   mycroft 	daddr_t bn;
    665      1.12   mycroft 	int size;
    666      1.12   mycroft 	struct proc *p;
    667      1.12   mycroft {
    668      1.48  augustss 	struct buf *bp;
    669      1.12   mycroft 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    670      1.12   mycroft 
    671      1.12   mycroft 	if (nmp->nm_flag & NFSMNT_INT) {
    672      1.12   mycroft 		bp = getblk(vp, bn, size, PCATCH, 0);
    673      1.54       chs 		while (bp == NULL) {
    674      1.54       chs 			if (nfs_sigintr(nmp, NULL, p))
    675      1.54       chs 				return (NULL);
    676      1.12   mycroft 			bp = getblk(vp, bn, size, 0, 2 * hz);
    677      1.12   mycroft 		}
    678      1.12   mycroft 	} else
    679      1.12   mycroft 		bp = getblk(vp, bn, size, 0, 0);
    680      1.12   mycroft 	return (bp);
    681      1.12   mycroft }
    682      1.12   mycroft 
    683      1.12   mycroft /*
    684      1.12   mycroft  * Flush and invalidate all dirty buffers. If another process is already
    685      1.12   mycroft  * doing the flush, just wait for completion.
    686      1.12   mycroft  */
    687      1.23  christos int
    688      1.12   mycroft nfs_vinvalbuf(vp, flags, cred, p, intrflg)
    689      1.12   mycroft 	struct vnode *vp;
    690      1.12   mycroft 	int flags;
    691      1.12   mycroft 	struct ucred *cred;
    692      1.12   mycroft 	struct proc *p;
    693      1.12   mycroft 	int intrflg;
    694      1.12   mycroft {
    695      1.48  augustss 	struct nfsnode *np = VTONFS(vp);
    696      1.12   mycroft 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    697      1.12   mycroft 	int error = 0, slpflag, slptimeo;
    698      1.12   mycroft 
    699      1.12   mycroft 	if ((nmp->nm_flag & NFSMNT_INT) == 0)
    700      1.12   mycroft 		intrflg = 0;
    701      1.12   mycroft 	if (intrflg) {
    702      1.12   mycroft 		slpflag = PCATCH;
    703      1.12   mycroft 		slptimeo = 2 * hz;
    704      1.12   mycroft 	} else {
    705      1.12   mycroft 		slpflag = 0;
    706      1.12   mycroft 		slptimeo = 0;
    707      1.12   mycroft 	}
    708      1.12   mycroft 	/*
    709      1.12   mycroft 	 * First wait for any other process doing a flush to complete.
    710      1.12   mycroft 	 */
    711      1.12   mycroft 	while (np->n_flag & NFLUSHINPROG) {
    712      1.12   mycroft 		np->n_flag |= NFLUSHWANT;
    713      1.12   mycroft 		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
    714      1.12   mycroft 			slptimeo);
    715      1.54       chs 		if (error && intrflg && nfs_sigintr(nmp, NULL, p))
    716      1.12   mycroft 			return (EINTR);
    717      1.12   mycroft 	}
    718      1.12   mycroft 
    719      1.12   mycroft 	/*
    720      1.12   mycroft 	 * Now, flush as required.
    721      1.12   mycroft 	 */
    722      1.12   mycroft 	np->n_flag |= NFLUSHINPROG;
    723      1.12   mycroft 	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
    724      1.12   mycroft 	while (error) {
    725      1.54       chs 		if (intrflg && nfs_sigintr(nmp, NULL, p)) {
    726      1.12   mycroft 			np->n_flag &= ~NFLUSHINPROG;
    727      1.12   mycroft 			if (np->n_flag & NFLUSHWANT) {
    728      1.12   mycroft 				np->n_flag &= ~NFLUSHWANT;
    729      1.12   mycroft 				wakeup((caddr_t)&np->n_flag);
    730      1.12   mycroft 			}
    731      1.12   mycroft 			return (EINTR);
    732      1.12   mycroft 		}
    733      1.12   mycroft 		error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
    734      1.12   mycroft 	}
    735      1.12   mycroft 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
    736      1.12   mycroft 	if (np->n_flag & NFLUSHWANT) {
    737      1.12   mycroft 		np->n_flag &= ~NFLUSHWANT;
    738      1.12   mycroft 		wakeup((caddr_t)&np->n_flag);
    739      1.12   mycroft 	}
    740      1.12   mycroft 	return (0);
    741      1.12   mycroft }
    742      1.12   mycroft 
    743      1.12   mycroft /*
    744      1.12   mycroft  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
    745      1.12   mycroft  * This is mainly to avoid queueing async I/O requests when the nfsiods
    746      1.12   mycroft  * are all hung on a dead server.
    747      1.12   mycroft  */
    748      1.23  christos int
    749      1.54       chs nfs_asyncio(bp)
    750      1.48  augustss 	struct buf *bp;
    751      1.12   mycroft {
    752      1.48  augustss 	int i;
    753      1.48  augustss 	struct nfsmount *nmp;
    754      1.30   thorpej 	int gotiod, slpflag = 0, slptimeo = 0, error;
    755      1.12   mycroft 
    756      1.12   mycroft 	if (nfs_numasync == 0)
    757      1.12   mycroft 		return (EIO);
    758      1.30   thorpej 
    759      1.30   thorpej 
    760      1.30   thorpej 	nmp = VFSTONFS(bp->b_vp->v_mount);
    761      1.30   thorpej again:
    762      1.30   thorpej 	if (nmp->nm_flag & NFSMNT_INT)
    763      1.30   thorpej 		slpflag = PCATCH;
    764      1.30   thorpej 	gotiod = FALSE;
    765      1.30   thorpej 
    766      1.30   thorpej 	/*
    767      1.30   thorpej 	 * Find a free iod to process this request.
    768      1.30   thorpej 	 */
    769      1.30   thorpej 
    770      1.12   mycroft 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
    771      1.30   thorpej 		if (nfs_iodwant[i]) {
    772      1.30   thorpej 			/*
    773      1.30   thorpej 			 * Found one, so wake it up and tell it which
    774      1.30   thorpej 			 * mount to process.
    775      1.30   thorpej 			 */
    776      1.54       chs 			nfs_iodwant[i] = NULL;
    777      1.30   thorpej 			nfs_iodmount[i] = nmp;
    778      1.30   thorpej 			nmp->nm_bufqiods++;
    779      1.30   thorpej 			wakeup((caddr_t)&nfs_iodwant[i]);
    780      1.30   thorpej 			gotiod = TRUE;
    781      1.31      fvdl 			break;
    782      1.30   thorpej 		}
    783      1.30   thorpej 	/*
    784      1.30   thorpej 	 * If none are free, we may already have an iod working on this mount
    785      1.30   thorpej 	 * point.  If so, it will process our request.
    786      1.30   thorpej 	 */
    787      1.30   thorpej 	if (!gotiod && nmp->nm_bufqiods > 0)
    788      1.30   thorpej 		gotiod = TRUE;
    789      1.30   thorpej 
    790      1.30   thorpej 	/*
    791      1.30   thorpej 	 * If we have an iod which can process the request, then queue
    792      1.30   thorpej 	 * the buffer.
    793      1.30   thorpej 	 */
    794      1.30   thorpej 	if (gotiod) {
    795      1.30   thorpej 		/*
    796      1.30   thorpej 		 * Ensure that the queue never grows too large.
    797      1.30   thorpej 		 */
    798      1.30   thorpej 		while (nmp->nm_bufqlen >= 2*nfs_numasync) {
    799      1.30   thorpej 			nmp->nm_bufqwant = TRUE;
    800      1.30   thorpej 			error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
    801      1.30   thorpej 				"nfsaio", slptimeo);
    802      1.30   thorpej 			if (error) {
    803      1.30   thorpej 				if (nfs_sigintr(nmp, NULL, bp->b_proc))
    804      1.30   thorpej 					return (EINTR);
    805      1.30   thorpej 				if (slpflag == PCATCH) {
    806      1.30   thorpej 					slpflag = 0;
    807      1.30   thorpej 					slptimeo = 2 * hz;
    808      1.30   thorpej 				}
    809      1.30   thorpej 			}
    810      1.30   thorpej 			/*
    811      1.30   thorpej 			 * We might have lost our iod while sleeping,
    812      1.30   thorpej 			 * so check and loop if nescessary.
    813      1.30   thorpej 			 */
    814      1.30   thorpej 			if (nmp->nm_bufqiods == 0)
    815      1.30   thorpej 				goto again;
    816      1.30   thorpej 		}
    817      1.30   thorpej 		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
    818      1.30   thorpej 		nmp->nm_bufqlen++;
    819      1.12   mycroft 		return (0);
    820      1.12   mycroft 	    }
    821      1.24      fvdl 
    822      1.24      fvdl 	/*
    823      1.30   thorpej 	 * All the iods are busy on other mounts, so return EIO to
    824      1.30   thorpej 	 * force the caller to process the i/o synchronously.
    825      1.24      fvdl 	 */
    826      1.30   thorpej 	return (EIO);
    827      1.12   mycroft }
    828      1.12   mycroft 
    829      1.12   mycroft /*
    830      1.12   mycroft  * Do an I/O operation to/from a cache block. This may be called
    831      1.12   mycroft  * synchronously or from an nfsiod.
    832      1.12   mycroft  */
    833      1.12   mycroft int
    834      1.54       chs nfs_doio(bp, p)
    835      1.48  augustss 	struct buf *bp;
    836      1.12   mycroft 	struct proc *p;
    837      1.12   mycroft {
    838      1.48  augustss 	struct uio *uiop;
    839      1.48  augustss 	struct vnode *vp;
    840      1.12   mycroft 	struct nfsnode *np;
    841      1.12   mycroft 	struct nfsmount *nmp;
    842      1.54       chs 	int error = 0, diff, len, iomode, must_commit = 0;
    843      1.12   mycroft 	struct uio uio;
    844      1.12   mycroft 	struct iovec io;
    845      1.12   mycroft 
    846      1.12   mycroft 	vp = bp->b_vp;
    847      1.12   mycroft 	np = VTONFS(vp);
    848      1.12   mycroft 	nmp = VFSTONFS(vp->v_mount);
    849      1.12   mycroft 	uiop = &uio;
    850      1.12   mycroft 	uiop->uio_iov = &io;
    851      1.12   mycroft 	uiop->uio_iovcnt = 1;
    852      1.12   mycroft 	uiop->uio_segflg = UIO_SYSSPACE;
    853      1.12   mycroft 	uiop->uio_procp = p;
    854      1.12   mycroft 
    855      1.12   mycroft 	/*
    856      1.14        pk 	 * Historically, paging was done with physio, but no more...
    857      1.12   mycroft 	 */
    858      1.14        pk 	if (bp->b_flags & B_PHYS) {
    859      1.14        pk 	    /*
    860      1.14        pk 	     * ...though reading /dev/drum still gets us here.
    861      1.14        pk 	     */
    862      1.14        pk 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
    863      1.14        pk 	    /* mapping was done by vmapbuf() */
    864      1.14        pk 	    io.iov_base = bp->b_data;
    865      1.54       chs 	    uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
    866      1.14        pk 	    if (bp->b_flags & B_READ) {
    867      1.14        pk 		uiop->uio_rw = UIO_READ;
    868      1.14        pk 		nfsstats.read_physios++;
    869      1.54       chs 		error = nfs_readrpc(vp, uiop);
    870      1.14        pk 	    } else {
    871      1.24      fvdl 		iomode = NFSV3WRITE_DATASYNC;
    872      1.14        pk 		uiop->uio_rw = UIO_WRITE;
    873      1.14        pk 		nfsstats.write_physios++;
    874      1.54       chs 		error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
    875      1.14        pk 	    }
    876      1.14        pk 	    if (error) {
    877      1.14        pk 		bp->b_flags |= B_ERROR;
    878      1.14        pk 		bp->b_error = error;
    879      1.14        pk 	    }
    880      1.14        pk 	} else if (bp->b_flags & B_READ) {
    881      1.12   mycroft 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
    882      1.12   mycroft 	    io.iov_base = bp->b_data;
    883      1.12   mycroft 	    uiop->uio_rw = UIO_READ;
    884      1.12   mycroft 	    switch (vp->v_type) {
    885      1.12   mycroft 	    case VREG:
    886      1.54       chs 		uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
    887      1.12   mycroft 		nfsstats.read_bios++;
    888      1.54       chs 		error = nfs_readrpc(vp, uiop);
    889      1.54       chs 		if (!error && uiop->uio_resid) {
    890      1.54       chs 
    891      1.12   mycroft 			/*
    892      1.12   mycroft 			 * If len > 0, there is a hole in the file and
    893      1.12   mycroft 			 * no writes after the hole have been pushed to
    894      1.12   mycroft 			 * the server yet.
    895      1.12   mycroft 			 * Just zero fill the rest of the valid area.
    896      1.12   mycroft 			 */
    897      1.54       chs 
    898      1.12   mycroft 			diff = bp->b_bcount - uiop->uio_resid;
    899      1.54       chs 			len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
    900      1.12   mycroft 				+ diff);
    901      1.12   mycroft 			if (len > 0) {
    902      1.63       chs 				len = MIN(len, uiop->uio_resid);
    903      1.54       chs 				memset((char *)bp->b_data + diff, 0, len);
    904      1.54       chs 			}
    905      1.12   mycroft 		}
    906      1.12   mycroft 		if (p && (vp->v_flag & VTEXT) &&
    907      1.12   mycroft 			(((nmp->nm_flag & NFSMNT_NQNFS) &&
    908      1.24      fvdl 			  NQNFS_CKINVALID(vp, np, ND_READ) &&
    909      1.12   mycroft 			  np->n_lrev != np->n_brev) ||
    910      1.12   mycroft 			 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
    911      1.35      fvdl 			  np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
    912      1.54       chs 			uprintf("Process killed due to "
    913      1.54       chs 				"text file modification\n");
    914      1.12   mycroft 			psignal(p, SIGKILL);
    915  1.63.2.1   nathanw #if 0 /* XXX NJWLWP */
    916      1.13   mycroft 			p->p_holdcnt++;
    917  1.63.2.1   nathanw #endif
    918      1.12   mycroft 		}
    919      1.12   mycroft 		break;
    920      1.12   mycroft 	    case VLNK:
    921      1.24      fvdl 		uiop->uio_offset = (off_t)0;
    922      1.12   mycroft 		nfsstats.readlink_bios++;
    923  1.63.2.1   nathanw 		error = nfs_readlinkrpc(vp, uiop, curproc->l_proc->p_ucred);
    924      1.12   mycroft 		break;
    925      1.12   mycroft 	    case VDIR:
    926      1.12   mycroft 		nfsstats.readdir_bios++;
    927      1.34      fvdl 		uiop->uio_offset = bp->b_dcookie;
    928      1.24      fvdl 		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
    929  1.63.2.1   nathanw 			error = nfs_readdirplusrpc(vp, uiop, curproc->l_proc->p_ucred);
    930      1.24      fvdl 			if (error == NFSERR_NOTSUPP)
    931      1.24      fvdl 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
    932      1.24      fvdl 		}
    933      1.24      fvdl 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
    934  1.63.2.1   nathanw 			error = nfs_readdirrpc(vp, uiop, curproc->l_proc->p_ucred);
    935      1.34      fvdl 		if (!error) {
    936      1.34      fvdl 			bp->b_dcookie = uiop->uio_offset;
    937      1.34      fvdl 		}
    938      1.24      fvdl 		break;
    939      1.24      fvdl 	    default:
    940      1.29  christos 		printf("nfs_doio:  type %x unexpected\n",vp->v_type);
    941      1.12   mycroft 		break;
    942      1.54       chs 	    }
    943      1.12   mycroft 	    if (error) {
    944      1.12   mycroft 		bp->b_flags |= B_ERROR;
    945      1.12   mycroft 		bp->b_error = error;
    946      1.12   mycroft 	    }
    947      1.12   mycroft 	} else {
    948      1.52      fvdl 	    /*
    949      1.52      fvdl 	     * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
    950      1.52      fvdl 	     * an actual write will have to be scheduled.
    951      1.52      fvdl 	     */
    952      1.54       chs 
    953      1.54       chs 	    io.iov_base = bp->b_data;
    954      1.54       chs 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
    955      1.54       chs 	    uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
    956      1.54       chs 	    uiop->uio_rw = UIO_WRITE;
    957      1.54       chs 	    nfsstats.write_bios++;
    958      1.54       chs 	    iomode = NFSV3WRITE_UNSTABLE;
    959      1.54       chs 	    error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
    960      1.54       chs 	}
    961      1.54       chs 	bp->b_resid = uiop->uio_resid;
    962      1.54       chs 	if (must_commit)
    963      1.54       chs 		nfs_clearcommit(vp->v_mount);
    964      1.54       chs 	biodone(bp);
    965      1.54       chs 	return (error);
    966      1.54       chs }
    967      1.54       chs 
    968      1.54       chs /*
    969      1.54       chs  * Vnode op for VM getpages.
    970      1.54       chs  */
    971      1.54       chs int
    972      1.54       chs nfs_getpages(v)
    973      1.54       chs 	void *v;
    974      1.54       chs {
    975      1.54       chs 	struct vop_getpages_args /* {
    976      1.54       chs 		struct vnode *a_vp;
    977      1.54       chs 		voff_t a_offset;
    978  1.63.2.3   nathanw 		struct vm_page **a_m;
    979      1.54       chs 		int *a_count;
    980      1.54       chs 		int a_centeridx;
    981      1.54       chs 		vm_prot_t a_access_type;
    982      1.54       chs 		int a_advice;
    983      1.54       chs 		int a_flags;
    984      1.54       chs 	} */ *ap = v;
    985      1.54       chs 
    986      1.54       chs 	off_t eof, offset, origoffset, startoffset, endoffset;
    987      1.54       chs 	int s, i, error, npages, orignpages, npgs, ridx, pidx, pcount;
    988      1.54       chs 	vaddr_t kva;
    989      1.54       chs 	struct buf *bp, *mbp;
    990      1.54       chs 	struct vnode *vp = ap->a_vp;
    991      1.54       chs 	struct nfsnode *np = VTONFS(vp);
    992      1.54       chs 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
    993      1.54       chs 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    994      1.54       chs 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    995      1.54       chs 	int flags = ap->a_flags;
    996      1.54       chs 	int bsize;
    997      1.54       chs 	struct vm_page *pgs[16];			/* XXXUBC 16 */
    998      1.54       chs 	boolean_t v3 = NFS_ISV3(vp);
    999      1.54       chs 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1000      1.54       chs 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   1001      1.54       chs 
   1002      1.54       chs 	UVMHIST_FUNC("nfs_getpages"); UVMHIST_CALLED(ubchist);
   1003      1.54       chs 	UVMHIST_LOG(ubchist, "vp %p off 0x%x count %d", vp, (int)ap->a_offset,
   1004      1.54       chs 		    *ap->a_count,0);
   1005      1.54       chs 
   1006      1.54       chs #ifdef DIAGNOSTIC
   1007      1.54       chs 	if (ap->a_centeridx < 0 || ap->a_centeridx >= *ap->a_count) {
   1008      1.54       chs 		panic("nfs_getpages: centeridx %d out of range",
   1009      1.54       chs 		      ap->a_centeridx);
   1010      1.54       chs 	}
   1011      1.54       chs #endif
   1012      1.54       chs 
   1013      1.54       chs 	error = 0;
   1014      1.54       chs 	origoffset = ap->a_offset;
   1015      1.54       chs 	eof = vp->v_uvm.u_size;
   1016      1.54       chs 	if (origoffset >= eof) {
   1017      1.54       chs 		if ((flags & PGO_LOCKED) == 0) {
   1018      1.54       chs 			simple_unlock(&uobj->vmobjlock);
   1019      1.54       chs 		}
   1020      1.54       chs 		UVMHIST_LOG(ubchist, "off 0x%x past EOF 0x%x",
   1021      1.54       chs 			    (int)origoffset, (int)eof,0,0);
   1022      1.54       chs 		return EINVAL;
   1023      1.54       chs 	}
   1024      1.54       chs 
   1025      1.54       chs 	if (flags & PGO_LOCKED) {
   1026      1.54       chs 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
   1027      1.54       chs 			      UFP_NOWAIT|UFP_NOALLOC);
   1028      1.54       chs 		return 0;
   1029      1.54       chs 	}
   1030      1.54       chs 
   1031      1.54       chs 	/* vnode is VOP_LOCKed, uobj is locked */
   1032      1.54       chs 
   1033      1.54       chs 	bsize = nmp->nm_rsize;
   1034      1.61       chs 	orignpages = MIN(*ap->a_count,
   1035      1.54       chs 			 round_page(eof - origoffset) >> PAGE_SHIFT);
   1036      1.54       chs 	npages = orignpages;
   1037      1.54       chs 	startoffset = origoffset & ~(bsize - 1);
   1038      1.54       chs 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
   1039      1.54       chs 				+ bsize - 1) & ~(bsize - 1));
   1040      1.61       chs 	endoffset = MIN(endoffset, round_page(eof));
   1041      1.54       chs 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
   1042      1.54       chs 
   1043      1.54       chs 	if (!async && !write) {
   1044      1.61       chs 		int rapages = MAX(PAGE_SIZE, nmp->nm_rsize) >> PAGE_SHIFT;
   1045      1.54       chs 
   1046      1.54       chs 		(void) VOP_GETPAGES(vp, endoffset, NULL, &rapages, 0,
   1047      1.54       chs 				    VM_PROT_READ, 0, 0);
   1048      1.54       chs 		simple_lock(&uobj->vmobjlock);
   1049      1.54       chs 	}
   1050      1.54       chs 
   1051      1.54       chs 	UVMHIST_LOG(ubchist, "npages %d offset 0x%x", npages,
   1052      1.54       chs 		    (int)origoffset, 0,0);
   1053      1.54       chs 	memset(pgs, 0, sizeof(pgs));
   1054      1.54       chs 	uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
   1055      1.54       chs 
   1056      1.54       chs 	if (flags & PGO_OVERWRITE) {
   1057      1.54       chs 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
   1058      1.54       chs 
   1059      1.54       chs 		/* XXXUBC for now, zero the page if we allocated it */
   1060      1.54       chs 		for (i = 0; i < npages; i++) {
   1061      1.54       chs 			struct vm_page *pg = pgs[ridx + i];
   1062      1.54       chs 
   1063      1.54       chs 			if (pg->flags & PG_FAKE) {
   1064      1.54       chs 				uvm_pagezero(pg);
   1065      1.54       chs 				pg->flags &= ~(PG_FAKE);
   1066      1.54       chs 			}
   1067      1.54       chs 		}
   1068      1.61       chs 		npages += ridx;
   1069      1.58       chs 		if (v3) {
   1070      1.58       chs 			simple_unlock(&uobj->vmobjlock);
   1071      1.58       chs 			goto uncommit;
   1072      1.58       chs 		}
   1073      1.54       chs 		goto out;
   1074      1.54       chs 	}
   1075      1.54       chs 
   1076      1.54       chs 	/*
   1077      1.54       chs 	 * if the pages are already resident, just return them.
   1078      1.54       chs 	 */
   1079      1.54       chs 
   1080      1.54       chs 	for (i = 0; i < npages; i++) {
   1081      1.54       chs 		struct vm_page *pg = pgs[ridx + i];
   1082      1.54       chs 
   1083      1.54       chs 		if ((pg->flags & PG_FAKE) != 0 ||
   1084      1.54       chs 		    ((ap->a_access_type & VM_PROT_WRITE) &&
   1085      1.54       chs 		      (pg->flags & PG_RDONLY))) {
   1086      1.54       chs 			break;
   1087      1.54       chs 		}
   1088      1.54       chs 	}
   1089      1.54       chs 	if (i == npages) {
   1090      1.54       chs 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
   1091      1.61       chs 		npages += ridx;
   1092      1.54       chs 		goto out;
   1093      1.54       chs 	}
   1094      1.54       chs 
   1095      1.54       chs 	/*
   1096      1.54       chs 	 * the page wasn't resident and we're not overwriting,
   1097      1.54       chs 	 * so we're going to have to do some i/o.
   1098      1.54       chs 	 * find any additional pages needed to cover the expanded range.
   1099      1.54       chs 	 */
   1100      1.54       chs 
   1101      1.54       chs 	if (startoffset != origoffset ||
   1102      1.54       chs 	    startoffset + (npages << PAGE_SHIFT) != endoffset) {
   1103      1.58       chs 
   1104      1.58       chs 		/*
   1105      1.58       chs 		 * XXXUBC we need to avoid deadlocks caused by locking
   1106      1.58       chs 		 * additional pages at lower offsets than pages we
   1107      1.58       chs 		 * already have locked.  for now, unlock them all and
   1108      1.58       chs 		 * start over.
   1109      1.58       chs 		 */
   1110      1.58       chs 
   1111      1.58       chs 		for (i = 0; i < npages; i++) {
   1112      1.58       chs 			struct vm_page *pg = pgs[ridx + i];
   1113      1.58       chs 
   1114      1.58       chs 			if (pg->flags & PG_FAKE) {
   1115      1.58       chs 				pg->flags |= PG_RELEASED;
   1116      1.58       chs 			}
   1117      1.58       chs 		}
   1118      1.58       chs 		uvm_page_unbusy(&pgs[ridx], npages);
   1119      1.58       chs 		memset(pgs, 0, sizeof(pgs));
   1120      1.58       chs 
   1121      1.54       chs 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
   1122      1.58       chs 			    startoffset, endoffset, 0,0);
   1123      1.54       chs 		npages = (endoffset - startoffset) >> PAGE_SHIFT;
   1124      1.54       chs 		npgs = npages;
   1125      1.54       chs 		uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
   1126      1.54       chs 	}
   1127      1.54       chs 	simple_unlock(&uobj->vmobjlock);
   1128      1.54       chs 
   1129      1.54       chs 	/*
   1130      1.54       chs 	 * update the cached read creds for this node.
   1131      1.54       chs 	 */
   1132      1.54       chs 
   1133      1.54       chs 	if (np->n_rcred) {
   1134      1.54       chs 		crfree(np->n_rcred);
   1135      1.54       chs 	}
   1136  1.63.2.1   nathanw 	np->n_rcred = curproc->l_proc->p_ucred;
   1137      1.54       chs 	crhold(np->n_rcred);
   1138      1.54       chs 
   1139      1.54       chs 	/*
   1140      1.54       chs 	 * read the desired page(s).
   1141      1.54       chs 	 */
   1142      1.54       chs 
   1143      1.54       chs 	totalbytes = npages << PAGE_SHIFT;
   1144      1.61       chs 	bytes = MIN(totalbytes, vp->v_uvm.u_size - startoffset);
   1145      1.54       chs 	tailbytes = totalbytes - bytes;
   1146      1.54       chs 	skipbytes = 0;
   1147      1.54       chs 
   1148      1.54       chs 	kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
   1149      1.54       chs 			     UVMPAGER_MAPIN_READ);
   1150      1.54       chs 
   1151      1.54       chs 	s = splbio();
   1152      1.54       chs 	mbp = pool_get(&bufpool, PR_WAITOK);
   1153      1.54       chs 	splx(s);
   1154      1.54       chs 	mbp->b_bufsize = totalbytes;
   1155      1.54       chs 	mbp->b_data = (void *)kva;
   1156      1.54       chs 	mbp->b_resid = mbp->b_bcount = bytes;
   1157      1.54       chs 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
   1158      1.54       chs 	mbp->b_iodone = uvm_aio_biodone;
   1159      1.54       chs 	mbp->b_vp = vp;
   1160      1.60   thorpej 	mbp->b_proc = NULL;		/* XXXUBC */
   1161      1.54       chs 	LIST_INIT(&mbp->b_dep);
   1162      1.54       chs 
   1163      1.54       chs 	/*
   1164      1.54       chs 	 * if EOF is in the middle of the last page, zero the part past EOF.
   1165      1.54       chs 	 */
   1166      1.54       chs 
   1167      1.56       chs 	if (tailbytes > 0 && (pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE)) {
   1168      1.54       chs 		memset((char *)kva + bytes, 0, tailbytes);
   1169      1.54       chs 	}
   1170      1.54       chs 
   1171      1.54       chs 	/*
   1172      1.54       chs 	 * now loop over the pages, reading as needed.
   1173      1.54       chs 	 */
   1174      1.54       chs 
   1175      1.54       chs 	bp = NULL;
   1176      1.54       chs 	for (offset = startoffset;
   1177      1.54       chs 	     bytes > 0;
   1178      1.54       chs 	     offset += iobytes, bytes -= iobytes) {
   1179      1.54       chs 
   1180      1.54       chs 		/*
   1181      1.54       chs 		 * skip pages which don't need to be read.
   1182      1.54       chs 		 */
   1183      1.54       chs 
   1184      1.54       chs 		pidx = (offset - startoffset) >> PAGE_SHIFT;
   1185      1.54       chs 		UVMHIST_LOG(ubchist, "pidx %d offset 0x%x startoffset 0x%x",
   1186      1.54       chs 			    pidx, (int)offset, (int)startoffset,0);
   1187      1.54       chs 		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
   1188      1.54       chs 			size_t b;
   1189      1.54       chs 
   1190      1.58       chs 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
   1191      1.61       chs 			b = MIN(PAGE_SIZE, bytes);
   1192      1.54       chs 			offset += b;
   1193      1.54       chs 			bytes -= b;
   1194      1.54       chs 			skipbytes += b;
   1195      1.54       chs 			pidx++;
   1196      1.54       chs 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
   1197      1.54       chs 				    (int)offset, 0,0,0);
   1198      1.54       chs 			if (bytes == 0) {
   1199      1.54       chs 				goto loopdone;
   1200      1.54       chs 			}
   1201      1.54       chs 		}
   1202      1.54       chs 
   1203      1.54       chs 		/*
   1204      1.54       chs 		 * see how many pages can be read with this i/o.
   1205      1.54       chs 		 * reduce the i/o size if necessary.
   1206      1.54       chs 		 */
   1207      1.54       chs 
   1208      1.54       chs 		iobytes = bytes;
   1209      1.54       chs 		if (offset + iobytes > round_page(offset)) {
   1210      1.54       chs 			pcount = 1;
   1211      1.54       chs 			while (pidx + pcount < npages &&
   1212      1.54       chs 			       pgs[pidx + pcount]->flags & PG_FAKE) {
   1213      1.54       chs 				pcount++;
   1214      1.54       chs 			}
   1215      1.61       chs 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
   1216      1.54       chs 				      (offset - trunc_page(offset)));
   1217      1.54       chs 		}
   1218      1.61       chs 		iobytes = MIN(iobytes, nmp->nm_rsize);
   1219      1.54       chs 
   1220      1.52      fvdl 		/*
   1221      1.54       chs 		 * allocate a sub-buf for this piece of the i/o
   1222      1.54       chs 		 * (or just use mbp if there's only 1 piece),
   1223      1.54       chs 		 * and start it going.
   1224      1.52      fvdl 		 */
   1225      1.54       chs 
   1226      1.54       chs 		if (offset == startoffset && iobytes == bytes) {
   1227      1.54       chs 			bp = mbp;
   1228      1.54       chs 		} else {
   1229      1.54       chs 			s = splbio();
   1230      1.54       chs 			bp = pool_get(&bufpool, PR_WAITOK);
   1231      1.54       chs 			splx(s);
   1232      1.54       chs 			bp->b_data = (char *)kva + offset - startoffset;
   1233      1.54       chs 			bp->b_resid = bp->b_bcount = iobytes;
   1234      1.54       chs 			bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
   1235      1.54       chs 			bp->b_iodone = uvm_aio_biodone1;
   1236      1.54       chs 			bp->b_vp = vp;
   1237      1.60   thorpej 			bp->b_proc = NULL;	/* XXXUBC */
   1238      1.54       chs 			LIST_INIT(&bp->b_dep);
   1239      1.54       chs 		}
   1240      1.54       chs 		bp->b_private = mbp;
   1241      1.54       chs 		bp->b_lblkno = bp->b_blkno = offset >> DEV_BSHIFT;
   1242      1.54       chs 
   1243      1.54       chs 		UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1244      1.54       chs 			    bp, offset, iobytes, bp->b_blkno);
   1245      1.54       chs 
   1246      1.54       chs 		VOP_STRATEGY(bp);
   1247      1.54       chs 	}
   1248      1.54       chs 
   1249      1.54       chs loopdone:
   1250      1.54       chs 	if (skipbytes) {
   1251      1.54       chs 		s = splbio();
   1252      1.54       chs 		mbp->b_resid -= skipbytes;
   1253      1.54       chs 		if (mbp->b_resid == 0) {
   1254      1.54       chs 			biodone(mbp);
   1255      1.54       chs 		}
   1256      1.54       chs 		splx(s);
   1257      1.54       chs 	}
   1258      1.54       chs 	if (async) {
   1259  1.63.2.2   nathanw 		UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
   1260  1.63.2.2   nathanw 		return 0;
   1261      1.54       chs 	}
   1262      1.54       chs 	if (bp != NULL) {
   1263      1.54       chs 		error = biowait(mbp);
   1264      1.54       chs 	}
   1265      1.54       chs 	s = splbio();
   1266      1.54       chs 	pool_put(&bufpool, mbp);
   1267      1.54       chs 	splx(s);
   1268      1.54       chs 	uvm_pagermapout(kva, npages);
   1269      1.54       chs 
   1270      1.54       chs 	if (write && v3) {
   1271      1.58       chs uncommit:
   1272      1.52      fvdl 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1273      1.54       chs 		nfs_del_committed_range(vp, origoffset, npages);
   1274      1.54       chs 		nfs_del_tobecommitted_range(vp, origoffset, npages);
   1275      1.58       chs 		simple_lock(&uobj->vmobjlock);
   1276      1.54       chs 		for (i = 0; i < npages; i++) {
   1277      1.54       chs 			if (pgs[i] == NULL) {
   1278      1.54       chs 				continue;
   1279      1.52      fvdl 			}
   1280      1.54       chs 			pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
   1281      1.52      fvdl 		}
   1282      1.58       chs 		simple_unlock(&uobj->vmobjlock);
   1283      1.52      fvdl 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1284      1.54       chs 	}
   1285      1.54       chs 
   1286      1.54       chs 	simple_lock(&uobj->vmobjlock);
   1287      1.54       chs 
   1288      1.54       chs out:
   1289      1.54       chs 	if (error) {
   1290      1.61       chs 		uvm_lock_pageq();
   1291      1.54       chs 		for (i = 0; i < npages; i++) {
   1292      1.54       chs 			if (pgs[i] == NULL) {
   1293      1.54       chs 				continue;
   1294      1.54       chs 			}
   1295      1.54       chs 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
   1296      1.54       chs 				    pgs[i], pgs[i]->flags, 0,0);
   1297      1.61       chs 			if (pgs[i]->flags & PG_WANTED) {
   1298      1.61       chs 				wakeup(pgs[i]);
   1299      1.61       chs 			}
   1300      1.61       chs 			if (pgs[i]->flags & PG_RELEASED) {
   1301      1.61       chs 				uvm_unlock_pageq();
   1302      1.61       chs 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
   1303      1.61       chs 				uvm_lock_pageq();
   1304      1.54       chs 				continue;
   1305      1.54       chs 			}
   1306      1.61       chs 			if (pgs[i]->flags & PG_FAKE) {
   1307      1.61       chs 				uvm_pagefree(pgs[i]);
   1308      1.62       chs 				continue;
   1309      1.54       chs 			}
   1310      1.62       chs 			uvm_pageactivate(pgs[i]);
   1311      1.62       chs 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
   1312      1.62       chs 			UVM_PAGE_OWN(pgs[i], NULL);
   1313      1.54       chs 		}
   1314      1.61       chs 		uvm_unlock_pageq();
   1315      1.61       chs 		simple_unlock(&uobj->vmobjlock);
   1316      1.61       chs 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
   1317      1.61       chs 		return error;
   1318      1.54       chs 	}
   1319      1.54       chs 
   1320      1.54       chs 	UVMHIST_LOG(ubchist, "ridx %d count %d", ridx, npages, 0,0);
   1321      1.61       chs 	uvm_lock_pageq();
   1322      1.54       chs 	for (i = 0; i < npages; i++) {
   1323      1.54       chs 		if (pgs[i] == NULL) {
   1324      1.54       chs 			continue;
   1325      1.54       chs 		}
   1326      1.54       chs 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
   1327      1.54       chs 			    pgs[i], pgs[i]->flags, 0,0);
   1328      1.54       chs 		if (pgs[i]->flags & PG_FAKE) {
   1329      1.54       chs 			UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
   1330      1.54       chs 				    pgs[i], (int)pgs[i]->offset,0,0);
   1331      1.54       chs 			pgs[i]->flags &= ~(PG_FAKE);
   1332      1.54       chs 			pmap_clear_modify(pgs[i]);
   1333      1.54       chs 			pmap_clear_reference(pgs[i]);
   1334      1.54       chs 		}
   1335      1.54       chs 		if (i < ridx || i >= ridx + orignpages || async) {
   1336      1.54       chs 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
   1337      1.54       chs 				    pgs[i], (int)pgs[i]->offset,0,0);
   1338      1.54       chs 			if (pgs[i]->flags & PG_WANTED) {
   1339      1.54       chs 				wakeup(pgs[i]);
   1340      1.54       chs 			}
   1341      1.61       chs 			if (pgs[i]->flags & PG_RELEASED) {
   1342      1.61       chs 				uvm_unlock_pageq();
   1343      1.61       chs 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
   1344      1.61       chs 				uvm_lock_pageq();
   1345      1.61       chs 				continue;
   1346      1.54       chs 			}
   1347      1.61       chs 			uvm_pageactivate(pgs[i]);
   1348      1.54       chs 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
   1349      1.54       chs 			UVM_PAGE_OWN(pgs[i], NULL);
   1350      1.54       chs 		}
   1351      1.54       chs 	}
   1352      1.54       chs 	uvm_unlock_pageq();
   1353      1.54       chs 	simple_unlock(&uobj->vmobjlock);
   1354      1.54       chs 	if (ap->a_m != NULL) {
   1355      1.54       chs 		memcpy(ap->a_m, &pgs[ridx],
   1356      1.54       chs 		       *ap->a_count * sizeof(struct vm_page *));
   1357      1.54       chs 	}
   1358      1.61       chs 	return 0;
   1359      1.54       chs }
   1360      1.54       chs 
   1361      1.54       chs /*
   1362      1.54       chs  * Vnode op for VM putpages.
   1363      1.54       chs  */
   1364      1.54       chs int
   1365      1.54       chs nfs_putpages(v)
   1366      1.54       chs 	void *v;
   1367      1.54       chs {
   1368      1.54       chs 	struct vop_putpages_args /* {
   1369      1.54       chs 		struct vnode *a_vp;
   1370      1.54       chs 		struct vm_page **a_m;
   1371      1.54       chs 		int a_count;
   1372      1.54       chs 		int a_flags;
   1373      1.54       chs 		int *a_rtvals;
   1374      1.54       chs 	} */ *ap = v;
   1375      1.54       chs 
   1376      1.54       chs 	struct vnode *vp = ap->a_vp;
   1377      1.54       chs 	struct nfsnode *np = VTONFS(vp);
   1378      1.54       chs 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1379      1.54       chs 	struct buf *bp, *mbp;
   1380      1.54       chs 	struct vm_page **pgs = ap->a_m;
   1381      1.54       chs 	int flags = ap->a_flags;
   1382      1.54       chs 	int npages = ap->a_count;
   1383      1.62       chs 	int s, error, i;
   1384      1.54       chs 	size_t bytes, iobytes, skipbytes;
   1385      1.54       chs 	vaddr_t kva;
   1386      1.54       chs 	off_t offset, origoffset, commitoff;
   1387      1.54       chs 	uint32_t commitbytes;
   1388      1.54       chs 	boolean_t v3 = NFS_ISV3(vp);
   1389      1.54       chs 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1390      1.54       chs 	boolean_t weak = (flags & PGO_WEAK) && v3;
   1391      1.54       chs 	UVMHIST_FUNC("nfs_putpages"); UVMHIST_CALLED(ubchist);
   1392      1.54       chs 
   1393      1.54       chs 	UVMHIST_LOG(ubchist, "vp %p pgp %p count %d",
   1394      1.54       chs 		    vp, ap->a_m, ap->a_count,0);
   1395      1.54       chs 
   1396      1.54       chs 	simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
   1397      1.54       chs 
   1398      1.62       chs 	error = 0;
   1399      1.54       chs 	origoffset = pgs[0]->offset;
   1400      1.61       chs 	bytes = MIN(ap->a_count << PAGE_SHIFT, vp->v_uvm.u_size - origoffset);
   1401      1.54       chs 	skipbytes = 0;
   1402      1.54       chs 
   1403      1.54       chs 	/*
   1404      1.54       chs 	 * if the range has been committed already, mark the pages thus.
   1405      1.54       chs 	 * if the range just needs to be committed, we're done
   1406      1.54       chs 	 * if it's a weak putpage, otherwise commit the range.
   1407      1.54       chs 	 */
   1408      1.52      fvdl 
   1409      1.54       chs 	if (v3) {
   1410      1.52      fvdl 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1411      1.54       chs 		if (nfs_in_committed_range(vp, origoffset, bytes)) {
   1412      1.54       chs 			goto committed;
   1413      1.54       chs 		}
   1414      1.54       chs 		if (nfs_in_tobecommitted_range(vp, origoffset, bytes)) {
   1415      1.54       chs 			if (weak) {
   1416      1.54       chs 				lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1417      1.54       chs 				return 0;
   1418      1.54       chs 			} else {
   1419      1.54       chs 				commitoff = np->n_pushlo;
   1420      1.54       chs 				commitbytes = (uint32_t)(np->n_pushhi -
   1421      1.54       chs 							 np->n_pushlo);
   1422      1.54       chs 				goto commit;
   1423      1.54       chs 			}
   1424      1.54       chs 		}
   1425      1.52      fvdl 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1426      1.54       chs 	}
   1427      1.54       chs 
   1428      1.54       chs 	/*
   1429      1.54       chs 	 * otherwise write or commit all the pages.
   1430      1.54       chs 	 */
   1431      1.54       chs 
   1432      1.54       chs 	kva = uvm_pagermapin(pgs, ap->a_count, UVMPAGER_MAPIN_WAITOK|
   1433      1.54       chs 			     UVMPAGER_MAPIN_WRITE);
   1434      1.54       chs 
   1435      1.54       chs 	s = splbio();
   1436      1.54       chs 	vp->v_numoutput += 2;
   1437      1.54       chs 	mbp = pool_get(&bufpool, PR_WAITOK);
   1438      1.54       chs 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1439      1.54       chs 		    vp, mbp, vp->v_numoutput, bytes);
   1440      1.54       chs 	splx(s);
   1441      1.54       chs 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1442      1.54       chs 	mbp->b_data = (void *)kva;
   1443      1.54       chs 	mbp->b_resid = mbp->b_bcount = bytes;
   1444      1.54       chs 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
   1445      1.54       chs 		(async ? B_CALL|B_ASYNC : 0) |
   1446      1.54       chs 		(curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
   1447      1.59     enami 	mbp->b_iodone = uvm_aio_biodone;
   1448      1.54       chs 	mbp->b_vp = vp;
   1449      1.60   thorpej 	mbp->b_proc = NULL;		/* XXXUBC */
   1450      1.54       chs 	LIST_INIT(&mbp->b_dep);
   1451      1.54       chs 
   1452      1.54       chs 	for (offset = origoffset;
   1453      1.54       chs 	     bytes > 0;
   1454      1.54       chs 	     offset += iobytes, bytes -= iobytes) {
   1455      1.61       chs 		iobytes = MIN(nmp->nm_wsize, bytes);
   1456      1.12   mycroft 
   1457      1.12   mycroft 		/*
   1458      1.54       chs 		 * skip writing any pages which only need a commit.
   1459      1.12   mycroft 		 */
   1460      1.54       chs 
   1461      1.54       chs 		if ((pgs[(offset - origoffset) >> PAGE_SHIFT]->flags &
   1462      1.54       chs 		     PG_NEEDCOMMIT) != 0) {
   1463      1.58       chs 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
   1464      1.61       chs 			iobytes = MIN(PAGE_SIZE, bytes);
   1465      1.58       chs 			skipbytes += iobytes;
   1466      1.54       chs 			continue;
   1467      1.54       chs 		}
   1468      1.54       chs 
   1469      1.54       chs 		/* if it's really one i/o, don't make a second buf */
   1470      1.54       chs 		if (offset == origoffset && iobytes == bytes) {
   1471      1.54       chs 			bp = mbp;
   1472      1.54       chs 		} else {
   1473      1.54       chs 			s = splbio();
   1474      1.54       chs 			vp->v_numoutput++;
   1475      1.54       chs 			bp = pool_get(&bufpool, PR_WAITOK);
   1476      1.54       chs 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1477      1.54       chs 				    vp, bp, vp->v_numoutput, 0);
   1478      1.54       chs 			splx(s);
   1479      1.54       chs 			bp->b_data = (char *)kva + (offset - origoffset);
   1480      1.54       chs 			bp->b_resid = bp->b_bcount = iobytes;
   1481      1.54       chs 			bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
   1482      1.54       chs 			bp->b_iodone = uvm_aio_biodone1;
   1483      1.54       chs 			bp->b_vp = vp;
   1484      1.60   thorpej 			bp->b_proc = NULL;	/* XXXUBC */
   1485      1.54       chs 			LIST_INIT(&bp->b_dep);
   1486      1.54       chs 		}
   1487      1.54       chs 		bp->b_private = mbp;
   1488      1.54       chs 		bp->b_lblkno = bp->b_blkno = (daddr_t)(offset >> DEV_BSHIFT);
   1489      1.54       chs 		UVMHIST_LOG(ubchist, "bp %p numout %d",
   1490      1.54       chs 			    bp, vp->v_numoutput,0,0);
   1491      1.54       chs 		VOP_STRATEGY(bp);
   1492      1.54       chs 	}
   1493      1.54       chs 	if (skipbytes) {
   1494      1.54       chs 		UVMHIST_LOG(ubchist, "skipbytes %d", bytes, 0,0,0);
   1495      1.54       chs 		s = splbio();
   1496      1.54       chs 		mbp->b_resid -= skipbytes;
   1497      1.54       chs 		if (mbp->b_resid == 0) {
   1498      1.54       chs 			biodone(mbp);
   1499      1.54       chs 		}
   1500      1.54       chs 		splx(s);
   1501      1.54       chs 	}
   1502      1.54       chs 	if (async) {
   1503  1.63.2.2   nathanw 		return 0;
   1504      1.54       chs 	}
   1505      1.62       chs 	if (bp != NULL) {
   1506      1.62       chs 		error = biowait(mbp);
   1507      1.62       chs 	}
   1508      1.54       chs 
   1509      1.54       chs 	s = splbio();
   1510      1.54       chs 	vwakeup(mbp);
   1511      1.54       chs 	pool_put(&bufpool, mbp);
   1512      1.54       chs 	splx(s);
   1513      1.54       chs 
   1514      1.54       chs 	uvm_pagermapout(kva, ap->a_count);
   1515      1.54       chs 	if (error || !v3) {
   1516      1.54       chs 		UVMHIST_LOG(ubchist, "returning error %d", error, 0,0,0);
   1517      1.54       chs 		return error;
   1518      1.54       chs 	}
   1519      1.54       chs 
   1520      1.54       chs 	/*
   1521      1.54       chs 	 * for a weak put, mark the range as "to be committed"
   1522      1.54       chs 	 * and mark the pages read-only so that we will be notified
   1523      1.54       chs 	 * to remove the pages from the "to be committed" range
   1524      1.54       chs 	 * if they are made dirty again.
   1525      1.54       chs 	 * for a strong put, commit the pages and remove them from the
   1526      1.54       chs 	 * "to be committed" range.  also, mark them as writable
   1527      1.54       chs 	 * and not cleanable with just a commit.
   1528      1.54       chs 	 */
   1529      1.54       chs 
   1530      1.54       chs 	lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1531      1.54       chs 	if (weak) {
   1532      1.54       chs 		nfs_add_tobecommitted_range(vp, origoffset,
   1533      1.54       chs 					    npages << PAGE_SHIFT);
   1534      1.54       chs 		for (i = 0; i < npages; i++) {
   1535      1.54       chs 			pgs[i]->flags |= PG_NEEDCOMMIT|PG_RDONLY;
   1536      1.54       chs 		}
   1537      1.54       chs 	} else {
   1538      1.54       chs 		commitoff = origoffset;
   1539      1.54       chs 		commitbytes = npages << PAGE_SHIFT;
   1540      1.54       chs commit:
   1541  1.63.2.1   nathanw 		error = nfs_commit(vp, commitoff, commitbytes, curproc->l_proc);
   1542      1.54       chs 		nfs_del_tobecommitted_range(vp, commitoff, commitbytes);
   1543      1.54       chs committed:
   1544      1.54       chs 		for (i = 0; i < npages; i++) {
   1545      1.54       chs 			pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
   1546       1.1       cgd 		}
   1547       1.1       cgd 	}
   1548      1.54       chs 	lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1549      1.54       chs 	return error;
   1550       1.1       cgd }
   1551