Home | History | Annotate | Line # | Download | only in nfs
nfs_bio.c revision 1.164.6.2
      1  1.164.6.2  ad /*	$NetBSD: nfs_bio.c,v 1.164.6.2 2007/07/29 13:31:13 ad Exp $	*/
      2  1.164.6.2  ad 
      3  1.164.6.2  ad /*
      4  1.164.6.2  ad  * Copyright (c) 1989, 1993
      5  1.164.6.2  ad  *	The Regents of the University of California.  All rights reserved.
      6  1.164.6.2  ad  *
      7  1.164.6.2  ad  * This code is derived from software contributed to Berkeley by
      8  1.164.6.2  ad  * Rick Macklem at The University of Guelph.
      9  1.164.6.2  ad  *
     10  1.164.6.2  ad  * Redistribution and use in source and binary forms, with or without
     11  1.164.6.2  ad  * modification, are permitted provided that the following conditions
     12  1.164.6.2  ad  * are met:
     13  1.164.6.2  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.164.6.2  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.164.6.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.164.6.2  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.164.6.2  ad  *    documentation and/or other materials provided with the distribution.
     18  1.164.6.2  ad  * 3. Neither the name of the University nor the names of its contributors
     19  1.164.6.2  ad  *    may be used to endorse or promote products derived from this software
     20  1.164.6.2  ad  *    without specific prior written permission.
     21  1.164.6.2  ad  *
     22  1.164.6.2  ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.164.6.2  ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.164.6.2  ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.164.6.2  ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.164.6.2  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.164.6.2  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.164.6.2  ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.164.6.2  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.164.6.2  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.164.6.2  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.164.6.2  ad  * SUCH DAMAGE.
     33  1.164.6.2  ad  *
     34  1.164.6.2  ad  *	@(#)nfs_bio.c	8.9 (Berkeley) 3/30/95
     35  1.164.6.2  ad  */
     36  1.164.6.2  ad 
     37  1.164.6.2  ad #include <sys/cdefs.h>
     38  1.164.6.2  ad __KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.164.6.2 2007/07/29 13:31:13 ad Exp $");
     39  1.164.6.2  ad 
     40  1.164.6.2  ad #include "opt_nfs.h"
     41  1.164.6.2  ad #include "opt_ddb.h"
     42  1.164.6.2  ad 
     43  1.164.6.2  ad #include <sys/param.h>
     44  1.164.6.2  ad #include <sys/systm.h>
     45  1.164.6.2  ad #include <sys/resourcevar.h>
     46  1.164.6.2  ad #include <sys/signalvar.h>
     47  1.164.6.2  ad #include <sys/proc.h>
     48  1.164.6.2  ad #include <sys/buf.h>
     49  1.164.6.2  ad #include <sys/vnode.h>
     50  1.164.6.2  ad #include <sys/mount.h>
     51  1.164.6.2  ad #include <sys/kernel.h>
     52  1.164.6.2  ad #include <sys/namei.h>
     53  1.164.6.2  ad #include <sys/dirent.h>
     54  1.164.6.2  ad #include <sys/malloc.h>
     55  1.164.6.2  ad #include <sys/kauth.h>
     56  1.164.6.2  ad 
     57  1.164.6.2  ad #include <uvm/uvm_extern.h>
     58  1.164.6.2  ad #include <uvm/uvm.h>
     59  1.164.6.2  ad 
     60  1.164.6.2  ad #include <nfs/rpcv2.h>
     61  1.164.6.2  ad #include <nfs/nfsproto.h>
     62  1.164.6.2  ad #include <nfs/nfs.h>
     63  1.164.6.2  ad #include <nfs/nfsmount.h>
     64  1.164.6.2  ad #include <nfs/nfsnode.h>
     65  1.164.6.2  ad #include <nfs/nfs_var.h>
     66  1.164.6.2  ad 
     67  1.164.6.2  ad extern int nfs_numasync;
     68  1.164.6.2  ad extern int nfs_commitsize;
     69  1.164.6.2  ad extern struct nfsstats nfsstats;
     70  1.164.6.2  ad 
     71  1.164.6.2  ad static int nfs_doio_read __P((struct buf *, struct uio *));
     72  1.164.6.2  ad static int nfs_doio_write __P((struct buf *, struct uio *));
     73  1.164.6.2  ad static int nfs_doio_phys __P((struct buf *, struct uio *));
     74  1.164.6.2  ad 
     75  1.164.6.2  ad /*
     76  1.164.6.2  ad  * Vnode op for read using bio
     77  1.164.6.2  ad  * Any similarity to readip() is purely coincidental
     78  1.164.6.2  ad  */
     79  1.164.6.2  ad int
     80  1.164.6.2  ad nfs_bioread(vp, uio, ioflag, cred, cflag)
     81  1.164.6.2  ad 	struct vnode *vp;
     82  1.164.6.2  ad 	struct uio *uio;
     83  1.164.6.2  ad 	int ioflag, cflag;
     84  1.164.6.2  ad 	kauth_cred_t cred;
     85  1.164.6.2  ad {
     86  1.164.6.2  ad 	struct nfsnode *np = VTONFS(vp);
     87  1.164.6.2  ad 	struct buf *bp = NULL, *rabp;
     88  1.164.6.2  ad 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
     89  1.164.6.2  ad 	struct nfsdircache *ndp = NULL, *nndp = NULL;
     90  1.164.6.2  ad 	void *baddr;
     91  1.164.6.2  ad 	int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
     92  1.164.6.2  ad 	int enough = 0;
     93  1.164.6.2  ad 	struct dirent *dp, *pdp, *edp, *ep;
     94  1.164.6.2  ad 	off_t curoff = 0;
     95  1.164.6.2  ad 	int advice;
     96  1.164.6.2  ad 	struct lwp *l = curlwp;
     97  1.164.6.2  ad 
     98  1.164.6.2  ad #ifdef DIAGNOSTIC
     99  1.164.6.2  ad 	if (uio->uio_rw != UIO_READ)
    100  1.164.6.2  ad 		panic("nfs_read mode");
    101  1.164.6.2  ad #endif
    102  1.164.6.2  ad 	if (uio->uio_resid == 0)
    103  1.164.6.2  ad 		return (0);
    104  1.164.6.2  ad 	if (vp->v_type != VDIR && uio->uio_offset < 0)
    105  1.164.6.2  ad 		return (EINVAL);
    106  1.164.6.2  ad #ifndef NFS_V2_ONLY
    107  1.164.6.2  ad 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
    108  1.164.6.2  ad 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
    109  1.164.6.2  ad 		(void)nfs_fsinfo(nmp, vp, cred, l);
    110  1.164.6.2  ad #endif
    111  1.164.6.2  ad 	if (vp->v_type != VDIR &&
    112  1.164.6.2  ad 	    (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
    113  1.164.6.2  ad 		return (EFBIG);
    114  1.164.6.2  ad 
    115  1.164.6.2  ad 	/*
    116  1.164.6.2  ad 	 * For nfs, cache consistency can only be maintained approximately.
    117  1.164.6.2  ad 	 * Although RFC1094 does not specify the criteria, the following is
    118  1.164.6.2  ad 	 * believed to be compatible with the reference port.
    119  1.164.6.2  ad 	 *
    120  1.164.6.2  ad 	 * If the file's modify time on the server has changed since the
    121  1.164.6.2  ad 	 * last read rpc or you have written to the file,
    122  1.164.6.2  ad 	 * you may have lost data cache consistency with the
    123  1.164.6.2  ad 	 * server, so flush all of the file's data out of the cache.
    124  1.164.6.2  ad 	 * Then force a getattr rpc to ensure that you have up to date
    125  1.164.6.2  ad 	 * attributes.
    126  1.164.6.2  ad 	 * NB: This implies that cache data can be read when up to
    127  1.164.6.2  ad 	 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
    128  1.164.6.2  ad 	 * attributes this could be forced by setting n_attrstamp to 0 before
    129  1.164.6.2  ad 	 * the VOP_GETATTR() call.
    130  1.164.6.2  ad 	 */
    131  1.164.6.2  ad 
    132  1.164.6.2  ad 	if (vp->v_type != VLNK) {
    133  1.164.6.2  ad 		error = nfs_flushstalebuf(vp, cred, l,
    134  1.164.6.2  ad 		    NFS_FLUSHSTALEBUF_MYWRITE);
    135  1.164.6.2  ad 		if (error)
    136  1.164.6.2  ad 			return error;
    137  1.164.6.2  ad 	}
    138  1.164.6.2  ad 
    139  1.164.6.2  ad 	do {
    140  1.164.6.2  ad 	    /*
    141  1.164.6.2  ad 	     * Don't cache symlinks.
    142  1.164.6.2  ad 	     */
    143  1.164.6.2  ad 	    if ((vp->v_flag & VROOT) && vp->v_type == VLNK) {
    144  1.164.6.2  ad 		return (nfs_readlinkrpc(vp, uio, cred));
    145  1.164.6.2  ad 	    }
    146  1.164.6.2  ad 	    baddr = (void *)0;
    147  1.164.6.2  ad 	    switch (vp->v_type) {
    148  1.164.6.2  ad 	    case VREG:
    149  1.164.6.2  ad 		nfsstats.biocache_reads++;
    150  1.164.6.2  ad 
    151  1.164.6.2  ad 		advice = IO_ADV_DECODE(ioflag);
    152  1.164.6.2  ad 		error = 0;
    153  1.164.6.2  ad 		while (uio->uio_resid > 0) {
    154  1.164.6.2  ad 			vsize_t bytelen;
    155  1.164.6.2  ad 
    156  1.164.6.2  ad 			nfs_delayedtruncate(vp);
    157  1.164.6.2  ad 			if (np->n_size <= uio->uio_offset) {
    158  1.164.6.2  ad 				break;
    159  1.164.6.2  ad 			}
    160  1.164.6.2  ad 			bytelen =
    161  1.164.6.2  ad 			    MIN(np->n_size - uio->uio_offset, uio->uio_resid);
    162  1.164.6.2  ad 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
    163  1.164.6.2  ad 			    advice, UBC_READ | UBC_PARTIALOK |
    164  1.164.6.2  ad 			    (UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0));
    165  1.164.6.2  ad 			if (error) {
    166  1.164.6.2  ad 				/*
    167  1.164.6.2  ad 				 * XXXkludge
    168  1.164.6.2  ad 				 * the file has been truncated on the server.
    169  1.164.6.2  ad 				 * there isn't much we can do.
    170  1.164.6.2  ad 				 */
    171  1.164.6.2  ad 				if (uio->uio_offset >= np->n_size) {
    172  1.164.6.2  ad 					/* end of file */
    173  1.164.6.2  ad 					error = 0;
    174  1.164.6.2  ad 				} else {
    175  1.164.6.2  ad 					break;
    176  1.164.6.2  ad 				}
    177  1.164.6.2  ad 			}
    178  1.164.6.2  ad 		}
    179  1.164.6.2  ad 		break;
    180  1.164.6.2  ad 
    181  1.164.6.2  ad 	    case VLNK:
    182  1.164.6.2  ad 		nfsstats.biocache_readlinks++;
    183  1.164.6.2  ad 		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, l);
    184  1.164.6.2  ad 		if (!bp)
    185  1.164.6.2  ad 			return (EINTR);
    186  1.164.6.2  ad 		if ((bp->b_flags & B_DONE) == 0) {
    187  1.164.6.2  ad 			bp->b_flags |= B_READ;
    188  1.164.6.2  ad 			error = nfs_doio(bp);
    189  1.164.6.2  ad 			if (error) {
    190  1.164.6.2  ad 				brelse(bp);
    191  1.164.6.2  ad 				return (error);
    192  1.164.6.2  ad 			}
    193  1.164.6.2  ad 		}
    194  1.164.6.2  ad 		n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
    195  1.164.6.2  ad 		got_buf = 1;
    196  1.164.6.2  ad 		on = 0;
    197  1.164.6.2  ad 		break;
    198  1.164.6.2  ad 	    case VDIR:
    199  1.164.6.2  ad diragain:
    200  1.164.6.2  ad 		nfsstats.biocache_readdirs++;
    201  1.164.6.2  ad 		ndp = nfs_searchdircache(vp, uio->uio_offset,
    202  1.164.6.2  ad 			(nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
    203  1.164.6.2  ad 		if (!ndp) {
    204  1.164.6.2  ad 			/*
    205  1.164.6.2  ad 			 * We've been handed a cookie that is not
    206  1.164.6.2  ad 			 * in the cache. If we're not translating
    207  1.164.6.2  ad 			 * 32 <-> 64, it may be a value that was
    208  1.164.6.2  ad 			 * flushed out of the cache because it grew
    209  1.164.6.2  ad 			 * too big. Let the server judge if it's
    210  1.164.6.2  ad 			 * valid or not. In the translation case,
    211  1.164.6.2  ad 			 * we have no way of validating this value,
    212  1.164.6.2  ad 			 * so punt.
    213  1.164.6.2  ad 			 */
    214  1.164.6.2  ad 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
    215  1.164.6.2  ad 				return (EINVAL);
    216  1.164.6.2  ad 			ndp = nfs_enterdircache(vp, uio->uio_offset,
    217  1.164.6.2  ad 				uio->uio_offset, 0, 0);
    218  1.164.6.2  ad 		}
    219  1.164.6.2  ad 
    220  1.164.6.2  ad 		if (NFS_EOFVALID(np) &&
    221  1.164.6.2  ad 		    ndp->dc_cookie == np->n_direofoffset) {
    222  1.164.6.2  ad 			nfs_putdircache(np, ndp);
    223  1.164.6.2  ad 			nfsstats.direofcache_hits++;
    224  1.164.6.2  ad 			return (0);
    225  1.164.6.2  ad 		}
    226  1.164.6.2  ad 
    227  1.164.6.2  ad 		bp = nfs_getcacheblk(vp, NFSDC_BLKNO(ndp), NFS_DIRBLKSIZ, l);
    228  1.164.6.2  ad 		if (!bp)
    229  1.164.6.2  ad 		    return (EINTR);
    230  1.164.6.2  ad 		if ((bp->b_flags & B_DONE) == 0) {
    231  1.164.6.2  ad 		    bp->b_flags |= B_READ;
    232  1.164.6.2  ad 		    bp->b_dcookie = ndp->dc_blkcookie;
    233  1.164.6.2  ad 		    error = nfs_doio(bp);
    234  1.164.6.2  ad 		    if (error) {
    235  1.164.6.2  ad 			/*
    236  1.164.6.2  ad 			 * Yuck! The directory has been modified on the
    237  1.164.6.2  ad 			 * server. Punt and let the userland code
    238  1.164.6.2  ad 			 * deal with it.
    239  1.164.6.2  ad 			 */
    240  1.164.6.2  ad 			nfs_putdircache(np, ndp);
    241  1.164.6.2  ad 			brelse(bp);
    242  1.164.6.2  ad 			/*
    243  1.164.6.2  ad 			 * nfs_request maps NFSERR_BAD_COOKIE to EINVAL.
    244  1.164.6.2  ad 			 */
    245  1.164.6.2  ad 			if (error == EINVAL) { /* NFSERR_BAD_COOKIE */
    246  1.164.6.2  ad 			    nfs_invaldircache(vp, 0);
    247  1.164.6.2  ad 			    nfs_vinvalbuf(vp, 0, cred, l, 1);
    248  1.164.6.2  ad 			}
    249  1.164.6.2  ad 			return (error);
    250  1.164.6.2  ad 		    }
    251  1.164.6.2  ad 		}
    252  1.164.6.2  ad 
    253  1.164.6.2  ad 		/*
    254  1.164.6.2  ad 		 * Just return if we hit EOF right away with this
    255  1.164.6.2  ad 		 * block. Always check here, because direofoffset
    256  1.164.6.2  ad 		 * may have been set by an nfsiod since the last
    257  1.164.6.2  ad 		 * check.
    258  1.164.6.2  ad 		 *
    259  1.164.6.2  ad 		 * also, empty block implies EOF.
    260  1.164.6.2  ad 		 */
    261  1.164.6.2  ad 
    262  1.164.6.2  ad 		if (bp->b_bcount == bp->b_resid ||
    263  1.164.6.2  ad 		    (NFS_EOFVALID(np) &&
    264  1.164.6.2  ad 		    ndp->dc_blkcookie == np->n_direofoffset)) {
    265  1.164.6.2  ad 			KASSERT(bp->b_bcount != bp->b_resid ||
    266  1.164.6.2  ad 			    ndp->dc_blkcookie == bp->b_dcookie);
    267  1.164.6.2  ad 			nfs_putdircache(np, ndp);
    268  1.164.6.2  ad 			bp->b_flags |= B_NOCACHE;
    269  1.164.6.2  ad 			brelse(bp);
    270  1.164.6.2  ad 			return 0;
    271  1.164.6.2  ad 		}
    272  1.164.6.2  ad 
    273  1.164.6.2  ad 		/*
    274  1.164.6.2  ad 		 * Find the entry we were looking for in the block.
    275  1.164.6.2  ad 		 */
    276  1.164.6.2  ad 
    277  1.164.6.2  ad 		en = ndp->dc_entry;
    278  1.164.6.2  ad 
    279  1.164.6.2  ad 		pdp = dp = (struct dirent *)bp->b_data;
    280  1.164.6.2  ad 		edp = (struct dirent *)(void *)((char *)bp->b_data + bp->b_bcount -
    281  1.164.6.2  ad 		    bp->b_resid);
    282  1.164.6.2  ad 		enn = 0;
    283  1.164.6.2  ad 		while (enn < en && dp < edp) {
    284  1.164.6.2  ad 			pdp = dp;
    285  1.164.6.2  ad 			dp = _DIRENT_NEXT(dp);
    286  1.164.6.2  ad 			enn++;
    287  1.164.6.2  ad 		}
    288  1.164.6.2  ad 
    289  1.164.6.2  ad 		/*
    290  1.164.6.2  ad 		 * If the entry number was bigger than the number of
    291  1.164.6.2  ad 		 * entries in the block, or the cookie of the previous
    292  1.164.6.2  ad 		 * entry doesn't match, the directory cache is
    293  1.164.6.2  ad 		 * stale. Flush it and try again (i.e. go to
    294  1.164.6.2  ad 		 * the server).
    295  1.164.6.2  ad 		 */
    296  1.164.6.2  ad 		if (dp >= edp || (struct dirent *)_DIRENT_NEXT(dp) > edp ||
    297  1.164.6.2  ad 		    (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
    298  1.164.6.2  ad #ifdef DEBUG
    299  1.164.6.2  ad 		    	printf("invalid cache: %p %p %p off %lx %lx\n",
    300  1.164.6.2  ad 				pdp, dp, edp,
    301  1.164.6.2  ad 				(unsigned long)uio->uio_offset,
    302  1.164.6.2  ad 				(unsigned long)NFS_GETCOOKIE(pdp));
    303  1.164.6.2  ad #endif
    304  1.164.6.2  ad 			nfs_putdircache(np, ndp);
    305  1.164.6.2  ad 			brelse(bp);
    306  1.164.6.2  ad 			nfs_invaldircache(vp, 0);
    307  1.164.6.2  ad 			nfs_vinvalbuf(vp, 0, cred, l, 0);
    308  1.164.6.2  ad 			goto diragain;
    309  1.164.6.2  ad 		}
    310  1.164.6.2  ad 
    311  1.164.6.2  ad 		on = (char *)dp - (char *)bp->b_data;
    312  1.164.6.2  ad 
    313  1.164.6.2  ad 		/*
    314  1.164.6.2  ad 		 * Cache all entries that may be exported to the
    315  1.164.6.2  ad 		 * user, as they may be thrown back at us. The
    316  1.164.6.2  ad 		 * NFSBIO_CACHECOOKIES flag indicates that all
    317  1.164.6.2  ad 		 * entries are being 'exported', so cache them all.
    318  1.164.6.2  ad 		 */
    319  1.164.6.2  ad 
    320  1.164.6.2  ad 		if (en == 0 && pdp == dp) {
    321  1.164.6.2  ad 			dp = _DIRENT_NEXT(dp);
    322  1.164.6.2  ad 			enn++;
    323  1.164.6.2  ad 		}
    324  1.164.6.2  ad 
    325  1.164.6.2  ad 		if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
    326  1.164.6.2  ad 			n = uio->uio_resid;
    327  1.164.6.2  ad 			enough = 1;
    328  1.164.6.2  ad 		} else
    329  1.164.6.2  ad 			n = bp->b_bcount - bp->b_resid - on;
    330  1.164.6.2  ad 
    331  1.164.6.2  ad 		ep = (struct dirent *)(void *)((char *)bp->b_data + on + n);
    332  1.164.6.2  ad 
    333  1.164.6.2  ad 		/*
    334  1.164.6.2  ad 		 * Find last complete entry to copy, caching entries
    335  1.164.6.2  ad 		 * (if requested) as we go.
    336  1.164.6.2  ad 		 */
    337  1.164.6.2  ad 
    338  1.164.6.2  ad 		while (dp < ep && (struct dirent *)_DIRENT_NEXT(dp) <= ep) {
    339  1.164.6.2  ad 			if (cflag & NFSBIO_CACHECOOKIES) {
    340  1.164.6.2  ad 				nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
    341  1.164.6.2  ad 				    ndp->dc_blkcookie, enn, bp->b_lblkno);
    342  1.164.6.2  ad 				if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    343  1.164.6.2  ad 					NFS_STASHCOOKIE32(pdp,
    344  1.164.6.2  ad 					    nndp->dc_cookie32);
    345  1.164.6.2  ad 				}
    346  1.164.6.2  ad 				nfs_putdircache(np, nndp);
    347  1.164.6.2  ad 			}
    348  1.164.6.2  ad 			pdp = dp;
    349  1.164.6.2  ad 			dp = _DIRENT_NEXT(dp);
    350  1.164.6.2  ad 			enn++;
    351  1.164.6.2  ad 		}
    352  1.164.6.2  ad 		nfs_putdircache(np, ndp);
    353  1.164.6.2  ad 
    354  1.164.6.2  ad 		/*
    355  1.164.6.2  ad 		 * If the last requested entry was not the last in the
    356  1.164.6.2  ad 		 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
    357  1.164.6.2  ad 		 * cache the cookie of the last requested one, and
    358  1.164.6.2  ad 		 * set of the offset to it.
    359  1.164.6.2  ad 		 */
    360  1.164.6.2  ad 
    361  1.164.6.2  ad 		if ((on + n) < bp->b_bcount - bp->b_resid) {
    362  1.164.6.2  ad 			curoff = NFS_GETCOOKIE(pdp);
    363  1.164.6.2  ad 			nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
    364  1.164.6.2  ad 			    enn, bp->b_lblkno);
    365  1.164.6.2  ad 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    366  1.164.6.2  ad 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
    367  1.164.6.2  ad 				curoff = nndp->dc_cookie32;
    368  1.164.6.2  ad 			}
    369  1.164.6.2  ad 			nfs_putdircache(np, nndp);
    370  1.164.6.2  ad 		} else
    371  1.164.6.2  ad 			curoff = bp->b_dcookie;
    372  1.164.6.2  ad 
    373  1.164.6.2  ad 		/*
    374  1.164.6.2  ad 		 * Always cache the entry for the next block,
    375  1.164.6.2  ad 		 * so that readaheads can use it.
    376  1.164.6.2  ad 		 */
    377  1.164.6.2  ad 		nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
    378  1.164.6.2  ad 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    379  1.164.6.2  ad 			if (curoff == bp->b_dcookie) {
    380  1.164.6.2  ad 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
    381  1.164.6.2  ad 				curoff = nndp->dc_cookie32;
    382  1.164.6.2  ad 			}
    383  1.164.6.2  ad 		}
    384  1.164.6.2  ad 
    385  1.164.6.2  ad 		n = (char *)_DIRENT_NEXT(pdp) - ((char *)bp->b_data + on);
    386  1.164.6.2  ad 
    387  1.164.6.2  ad 		/*
    388  1.164.6.2  ad 		 * If not eof and read aheads are enabled, start one.
    389  1.164.6.2  ad 		 * (You need the current block first, so that you have the
    390  1.164.6.2  ad 		 *  directory offset cookie of the next block.)
    391  1.164.6.2  ad 		 */
    392  1.164.6.2  ad 		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
    393  1.164.6.2  ad 		    !NFS_EOFVALID(np)) {
    394  1.164.6.2  ad 			rabp = nfs_getcacheblk(vp, NFSDC_BLKNO(nndp),
    395  1.164.6.2  ad 						NFS_DIRBLKSIZ, l);
    396  1.164.6.2  ad 			if (rabp) {
    397  1.164.6.2  ad 			    if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
    398  1.164.6.2  ad 				rabp->b_dcookie = nndp->dc_cookie;
    399  1.164.6.2  ad 				rabp->b_flags |= (B_READ | B_ASYNC);
    400  1.164.6.2  ad 				if (nfs_asyncio(rabp)) {
    401  1.164.6.2  ad 				    rabp->b_flags |= B_INVAL;
    402  1.164.6.2  ad 				    brelse(rabp);
    403  1.164.6.2  ad 				}
    404  1.164.6.2  ad 			    } else
    405  1.164.6.2  ad 				brelse(rabp);
    406  1.164.6.2  ad 			}
    407  1.164.6.2  ad 		}
    408  1.164.6.2  ad 		nfs_putdircache(np, nndp);
    409  1.164.6.2  ad 		got_buf = 1;
    410  1.164.6.2  ad 		break;
    411  1.164.6.2  ad 	    default:
    412  1.164.6.2  ad 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
    413  1.164.6.2  ad 		break;
    414  1.164.6.2  ad 	    }
    415  1.164.6.2  ad 
    416  1.164.6.2  ad 	    if (n > 0) {
    417  1.164.6.2  ad 		if (!baddr)
    418  1.164.6.2  ad 			baddr = bp->b_data;
    419  1.164.6.2  ad 		error = uiomove((char *)baddr + on, (int)n, uio);
    420  1.164.6.2  ad 	    }
    421  1.164.6.2  ad 	    switch (vp->v_type) {
    422  1.164.6.2  ad 	    case VREG:
    423  1.164.6.2  ad 		break;
    424  1.164.6.2  ad 	    case VLNK:
    425  1.164.6.2  ad 		n = 0;
    426  1.164.6.2  ad 		break;
    427  1.164.6.2  ad 	    case VDIR:
    428  1.164.6.2  ad 		uio->uio_offset = curoff;
    429  1.164.6.2  ad 		if (enough)
    430  1.164.6.2  ad 			n = 0;
    431  1.164.6.2  ad 		break;
    432  1.164.6.2  ad 	    default:
    433  1.164.6.2  ad 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
    434  1.164.6.2  ad 	    }
    435  1.164.6.2  ad 	    if (got_buf)
    436  1.164.6.2  ad 		brelse(bp);
    437  1.164.6.2  ad 	} while (error == 0 && uio->uio_resid > 0 && n > 0);
    438  1.164.6.2  ad 	return (error);
    439  1.164.6.2  ad }
    440  1.164.6.2  ad 
    441  1.164.6.2  ad /*
    442  1.164.6.2  ad  * Vnode op for write using bio
    443  1.164.6.2  ad  */
    444  1.164.6.2  ad int
    445  1.164.6.2  ad nfs_write(v)
    446  1.164.6.2  ad 	void *v;
    447  1.164.6.2  ad {
    448  1.164.6.2  ad 	struct vop_write_args /* {
    449  1.164.6.2  ad 		struct vnode *a_vp;
    450  1.164.6.2  ad 		struct uio *a_uio;
    451  1.164.6.2  ad 		int  a_ioflag;
    452  1.164.6.2  ad 		kauth_cred_t a_cred;
    453  1.164.6.2  ad 	} */ *ap = v;
    454  1.164.6.2  ad 	struct uio *uio = ap->a_uio;
    455  1.164.6.2  ad 	struct lwp *l = curlwp;
    456  1.164.6.2  ad 	struct vnode *vp = ap->a_vp;
    457  1.164.6.2  ad 	struct nfsnode *np = VTONFS(vp);
    458  1.164.6.2  ad 	kauth_cred_t cred = ap->a_cred;
    459  1.164.6.2  ad 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    460  1.164.6.2  ad 	voff_t oldoff, origoff;
    461  1.164.6.2  ad 	vsize_t bytelen;
    462  1.164.6.2  ad 	int error = 0;
    463  1.164.6.2  ad 	int ioflag = ap->a_ioflag;
    464  1.164.6.2  ad 	int extended = 0, wrotedata = 0;
    465  1.164.6.2  ad 
    466  1.164.6.2  ad #ifdef DIAGNOSTIC
    467  1.164.6.2  ad 	if (uio->uio_rw != UIO_WRITE)
    468  1.164.6.2  ad 		panic("nfs_write mode");
    469  1.164.6.2  ad #endif
    470  1.164.6.2  ad 	if (vp->v_type != VREG)
    471  1.164.6.2  ad 		return (EIO);
    472  1.164.6.2  ad 	if (np->n_flag & NWRITEERR) {
    473  1.164.6.2  ad 		np->n_flag &= ~NWRITEERR;
    474  1.164.6.2  ad 		return (np->n_error);
    475  1.164.6.2  ad 	}
    476  1.164.6.2  ad #ifndef NFS_V2_ONLY
    477  1.164.6.2  ad 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
    478  1.164.6.2  ad 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
    479  1.164.6.2  ad 		(void)nfs_fsinfo(nmp, vp, cred, l);
    480  1.164.6.2  ad #endif
    481  1.164.6.2  ad 	if (ioflag & IO_APPEND) {
    482  1.164.6.2  ad 		NFS_INVALIDATE_ATTRCACHE(np);
    483  1.164.6.2  ad 		error = nfs_flushstalebuf(vp, cred, l,
    484  1.164.6.2  ad 		    NFS_FLUSHSTALEBUF_MYWRITE);
    485  1.164.6.2  ad 		if (error)
    486  1.164.6.2  ad 			return (error);
    487  1.164.6.2  ad 		uio->uio_offset = np->n_size;
    488  1.164.6.2  ad 	}
    489  1.164.6.2  ad 	if (uio->uio_offset < 0)
    490  1.164.6.2  ad 		return (EINVAL);
    491  1.164.6.2  ad 	if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
    492  1.164.6.2  ad 		return (EFBIG);
    493  1.164.6.2  ad 	if (uio->uio_resid == 0)
    494  1.164.6.2  ad 		return (0);
    495  1.164.6.2  ad 	/*
    496  1.164.6.2  ad 	 * Maybe this should be above the vnode op call, but so long as
    497  1.164.6.2  ad 	 * file servers have no limits, i don't think it matters
    498  1.164.6.2  ad 	 */
    499  1.164.6.2  ad 	if (l && l->l_proc && uio->uio_offset + uio->uio_resid >
    500  1.164.6.2  ad 	      l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    501  1.164.6.2  ad 		mutex_enter(&proclist_mutex);
    502  1.164.6.2  ad 		psignal(l->l_proc, SIGXFSZ);
    503  1.164.6.2  ad 		mutex_exit(&proclist_mutex);
    504  1.164.6.2  ad 		return (EFBIG);
    505  1.164.6.2  ad 	}
    506  1.164.6.2  ad 
    507  1.164.6.2  ad 	origoff = uio->uio_offset;
    508  1.164.6.2  ad 	do {
    509  1.164.6.2  ad 		bool overwrite; /* if we are overwriting whole pages */
    510  1.164.6.2  ad 		u_quad_t oldsize;
    511  1.164.6.2  ad 		oldoff = uio->uio_offset;
    512  1.164.6.2  ad 		bytelen = uio->uio_resid;
    513  1.164.6.2  ad 
    514  1.164.6.2  ad 		nfsstats.biocache_writes++;
    515  1.164.6.2  ad 
    516  1.164.6.2  ad 		oldsize = np->n_size;
    517  1.164.6.2  ad 		np->n_flag |= NMODIFIED;
    518  1.164.6.2  ad 		if (np->n_size < uio->uio_offset + bytelen) {
    519  1.164.6.2  ad 			np->n_size = uio->uio_offset + bytelen;
    520  1.164.6.2  ad 		}
    521  1.164.6.2  ad 		overwrite = false;
    522  1.164.6.2  ad 		if ((uio->uio_offset & PAGE_MASK) == 0) {
    523  1.164.6.2  ad 			if ((vp->v_flag & VMAPPED) == 0 &&
    524  1.164.6.2  ad 			    bytelen > PAGE_SIZE) {
    525  1.164.6.2  ad 				bytelen = trunc_page(bytelen);
    526  1.164.6.2  ad 				overwrite = true;
    527  1.164.6.2  ad 			} else if ((bytelen & PAGE_MASK) == 0 &&
    528  1.164.6.2  ad 			    uio->uio_offset >= vp->v_size) {
    529  1.164.6.2  ad 				overwrite = true;
    530  1.164.6.2  ad 			}
    531  1.164.6.2  ad 		}
    532  1.164.6.2  ad 		if (vp->v_size < uio->uio_offset + bytelen) {
    533  1.164.6.2  ad 			uvm_vnp_setwritesize(vp, uio->uio_offset + bytelen);
    534  1.164.6.2  ad 		}
    535  1.164.6.2  ad 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
    536  1.164.6.2  ad 		    UVM_ADV_RANDOM, UBC_WRITE | UBC_PARTIALOK |
    537  1.164.6.2  ad 		    (overwrite ? UBC_FAULTBUSY : 0) |
    538  1.164.6.2  ad 		    (UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0));
    539  1.164.6.2  ad 		if (error) {
    540  1.164.6.2  ad 			uvm_vnp_setwritesize(vp, vp->v_size);
    541  1.164.6.2  ad 			if (overwrite && np->n_size != oldsize) {
    542  1.164.6.2  ad 				/*
    543  1.164.6.2  ad 				 * backout size and free pages past eof.
    544  1.164.6.2  ad 				 */
    545  1.164.6.2  ad 				np->n_size = oldsize;
    546  1.164.6.2  ad 				simple_lock(&vp->v_interlock);
    547  1.164.6.2  ad 				(void)VOP_PUTPAGES(vp, round_page(vp->v_size),
    548  1.164.6.2  ad 				    0, PGO_SYNCIO | PGO_FREE);
    549  1.164.6.2  ad 			}
    550  1.164.6.2  ad 			break;
    551  1.164.6.2  ad 		}
    552  1.164.6.2  ad 		wrotedata = 1;
    553  1.164.6.2  ad 
    554  1.164.6.2  ad 		/*
    555  1.164.6.2  ad 		 * update UVM's notion of the size now that we've
    556  1.164.6.2  ad 		 * copied the data into the vnode's pages.
    557  1.164.6.2  ad 		 */
    558  1.164.6.2  ad 
    559  1.164.6.2  ad 		if (vp->v_size < uio->uio_offset) {
    560  1.164.6.2  ad 			uvm_vnp_setsize(vp, uio->uio_offset);
    561  1.164.6.2  ad 			extended = 1;
    562  1.164.6.2  ad 		}
    563  1.164.6.2  ad 
    564  1.164.6.2  ad 		if ((oldoff & ~(nmp->nm_wsize - 1)) !=
    565  1.164.6.2  ad 		    (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
    566  1.164.6.2  ad 			simple_lock(&vp->v_interlock);
    567  1.164.6.2  ad 			error = VOP_PUTPAGES(vp,
    568  1.164.6.2  ad 			    trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
    569  1.164.6.2  ad 			    round_page((uio->uio_offset + nmp->nm_wsize - 1) &
    570  1.164.6.2  ad 				       ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
    571  1.164.6.2  ad 		}
    572  1.164.6.2  ad 	} while (uio->uio_resid > 0);
    573  1.164.6.2  ad 	if (wrotedata)
    574  1.164.6.2  ad 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    575  1.164.6.2  ad 	if (error == 0 && (ioflag & IO_SYNC) != 0) {
    576  1.164.6.2  ad 		simple_lock(&vp->v_interlock);
    577  1.164.6.2  ad 		error = VOP_PUTPAGES(vp,
    578  1.164.6.2  ad 		    trunc_page(origoff & ~(nmp->nm_wsize - 1)),
    579  1.164.6.2  ad 		    round_page((uio->uio_offset + nmp->nm_wsize - 1) &
    580  1.164.6.2  ad 			       ~(nmp->nm_wsize - 1)),
    581  1.164.6.2  ad 		    PGO_CLEANIT | PGO_SYNCIO);
    582  1.164.6.2  ad 	}
    583  1.164.6.2  ad 	return error;
    584  1.164.6.2  ad }
    585  1.164.6.2  ad 
    586  1.164.6.2  ad /*
    587  1.164.6.2  ad  * Get an nfs cache block.
    588  1.164.6.2  ad  * Allocate a new one if the block isn't currently in the cache
    589  1.164.6.2  ad  * and return the block marked busy. If the calling process is
    590  1.164.6.2  ad  * interrupted by a signal for an interruptible mount point, return
    591  1.164.6.2  ad  * NULL.
    592  1.164.6.2  ad  */
    593  1.164.6.2  ad struct buf *
    594  1.164.6.2  ad nfs_getcacheblk(vp, bn, size, l)
    595  1.164.6.2  ad 	struct vnode *vp;
    596  1.164.6.2  ad 	daddr_t bn;
    597  1.164.6.2  ad 	int size;
    598  1.164.6.2  ad 	struct lwp *l;
    599  1.164.6.2  ad {
    600  1.164.6.2  ad 	struct buf *bp;
    601  1.164.6.2  ad 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    602  1.164.6.2  ad 
    603  1.164.6.2  ad 	if (nmp->nm_flag & NFSMNT_INT) {
    604  1.164.6.2  ad 		bp = getblk(vp, bn, size, PCATCH, 0);
    605  1.164.6.2  ad 		while (bp == NULL) {
    606  1.164.6.2  ad 			if (nfs_sigintr(nmp, NULL, l))
    607  1.164.6.2  ad 				return (NULL);
    608  1.164.6.2  ad 			bp = getblk(vp, bn, size, 0, 2 * hz);
    609  1.164.6.2  ad 		}
    610  1.164.6.2  ad 	} else
    611  1.164.6.2  ad 		bp = getblk(vp, bn, size, 0, 0);
    612  1.164.6.2  ad 	return (bp);
    613  1.164.6.2  ad }
    614  1.164.6.2  ad 
    615  1.164.6.2  ad /*
    616  1.164.6.2  ad  * Flush and invalidate all dirty buffers. If another process is already
    617  1.164.6.2  ad  * doing the flush, just wait for completion.
    618  1.164.6.2  ad  */
    619  1.164.6.2  ad int
    620  1.164.6.2  ad nfs_vinvalbuf(vp, flags, cred, l, intrflg)
    621  1.164.6.2  ad 	struct vnode *vp;
    622  1.164.6.2  ad 	int flags;
    623  1.164.6.2  ad 	kauth_cred_t cred;
    624  1.164.6.2  ad 	struct lwp *l;
    625  1.164.6.2  ad 	int intrflg;
    626  1.164.6.2  ad {
    627  1.164.6.2  ad 	struct nfsnode *np = VTONFS(vp);
    628  1.164.6.2  ad 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    629  1.164.6.2  ad 	int error = 0, slpflag, slptimeo;
    630  1.164.6.2  ad 
    631  1.164.6.2  ad 	if ((nmp->nm_flag & NFSMNT_INT) == 0)
    632  1.164.6.2  ad 		intrflg = 0;
    633  1.164.6.2  ad 	if (intrflg) {
    634  1.164.6.2  ad 		slpflag = PCATCH;
    635  1.164.6.2  ad 		slptimeo = 2 * hz;
    636  1.164.6.2  ad 	} else {
    637  1.164.6.2  ad 		slpflag = 0;
    638  1.164.6.2  ad 		slptimeo = 0;
    639  1.164.6.2  ad 	}
    640  1.164.6.2  ad 	/*
    641  1.164.6.2  ad 	 * First wait for any other process doing a flush to complete.
    642  1.164.6.2  ad 	 */
    643  1.164.6.2  ad 	simple_lock(&vp->v_interlock);
    644  1.164.6.2  ad 	while (np->n_flag & NFLUSHINPROG) {
    645  1.164.6.2  ad 		np->n_flag |= NFLUSHWANT;
    646  1.164.6.2  ad 		error = ltsleep(&np->n_flag, PRIBIO + 2, "nfsvinval",
    647  1.164.6.2  ad 			slptimeo, &vp->v_interlock);
    648  1.164.6.2  ad 		if (error && intrflg && nfs_sigintr(nmp, NULL, l)) {
    649  1.164.6.2  ad 			simple_unlock(&vp->v_interlock);
    650  1.164.6.2  ad 			return EINTR;
    651  1.164.6.2  ad 		}
    652  1.164.6.2  ad 	}
    653  1.164.6.2  ad 
    654  1.164.6.2  ad 	/*
    655  1.164.6.2  ad 	 * Now, flush as required.
    656  1.164.6.2  ad 	 */
    657  1.164.6.2  ad 	np->n_flag |= NFLUSHINPROG;
    658  1.164.6.2  ad 	simple_unlock(&vp->v_interlock);
    659  1.164.6.2  ad 	error = vinvalbuf(vp, flags, cred, l, slpflag, 0);
    660  1.164.6.2  ad 	while (error) {
    661  1.164.6.2  ad 		if (intrflg && nfs_sigintr(nmp, NULL, l)) {
    662  1.164.6.2  ad 			error = EINTR;
    663  1.164.6.2  ad 			break;
    664  1.164.6.2  ad 		}
    665  1.164.6.2  ad 		error = vinvalbuf(vp, flags, cred, l, 0, slptimeo);
    666  1.164.6.2  ad 	}
    667  1.164.6.2  ad 	simple_lock(&vp->v_interlock);
    668  1.164.6.2  ad 	if (error == 0)
    669  1.164.6.2  ad 		np->n_flag &= ~NMODIFIED;
    670  1.164.6.2  ad 	np->n_flag &= ~NFLUSHINPROG;
    671  1.164.6.2  ad 	if (np->n_flag & NFLUSHWANT) {
    672  1.164.6.2  ad 		np->n_flag &= ~NFLUSHWANT;
    673  1.164.6.2  ad 		wakeup(&np->n_flag);
    674  1.164.6.2  ad 	}
    675  1.164.6.2  ad 	simple_unlock(&vp->v_interlock);
    676  1.164.6.2  ad 	return error;
    677  1.164.6.2  ad }
    678  1.164.6.2  ad 
    679  1.164.6.2  ad /*
    680  1.164.6.2  ad  * nfs_flushstalebuf: flush cache if it's stale.
    681  1.164.6.2  ad  *
    682  1.164.6.2  ad  * => caller shouldn't own any pages or buffers which belong to the vnode.
    683  1.164.6.2  ad  */
    684  1.164.6.2  ad 
    685  1.164.6.2  ad int
    686  1.164.6.2  ad nfs_flushstalebuf(struct vnode *vp, kauth_cred_t cred, struct lwp *l,
    687  1.164.6.2  ad     int flags)
    688  1.164.6.2  ad {
    689  1.164.6.2  ad 	struct nfsnode *np = VTONFS(vp);
    690  1.164.6.2  ad 	struct vattr vattr;
    691  1.164.6.2  ad 	int error;
    692  1.164.6.2  ad 
    693  1.164.6.2  ad 	if (np->n_flag & NMODIFIED) {
    694  1.164.6.2  ad 		if ((flags & NFS_FLUSHSTALEBUF_MYWRITE) == 0
    695  1.164.6.2  ad 		    || vp->v_type != VREG) {
    696  1.164.6.2  ad 			error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
    697  1.164.6.2  ad 			if (error)
    698  1.164.6.2  ad 				return error;
    699  1.164.6.2  ad 			if (vp->v_type == VDIR) {
    700  1.164.6.2  ad 				nfs_invaldircache(vp, 0);
    701  1.164.6.2  ad 			}
    702  1.164.6.2  ad 		} else {
    703  1.164.6.2  ad 			/*
    704  1.164.6.2  ad 			 * XXX assuming writes are ours.
    705  1.164.6.2  ad 			 */
    706  1.164.6.2  ad 		}
    707  1.164.6.2  ad 		NFS_INVALIDATE_ATTRCACHE(np);
    708  1.164.6.2  ad 		error = VOP_GETATTR(vp, &vattr, cred, l);
    709  1.164.6.2  ad 		if (error)
    710  1.164.6.2  ad 			return error;
    711  1.164.6.2  ad 		np->n_mtime = vattr.va_mtime;
    712  1.164.6.2  ad 	} else {
    713  1.164.6.2  ad 		error = VOP_GETATTR(vp, &vattr, cred, l);
    714  1.164.6.2  ad 		if (error)
    715  1.164.6.2  ad 			return error;
    716  1.164.6.2  ad 		if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
    717  1.164.6.2  ad 			if (vp->v_type == VDIR) {
    718  1.164.6.2  ad 				nfs_invaldircache(vp, 0);
    719  1.164.6.2  ad 			}
    720  1.164.6.2  ad 			error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
    721  1.164.6.2  ad 			if (error)
    722  1.164.6.2  ad 				return error;
    723  1.164.6.2  ad 			np->n_mtime = vattr.va_mtime;
    724  1.164.6.2  ad 		}
    725  1.164.6.2  ad 	}
    726  1.164.6.2  ad 
    727  1.164.6.2  ad 	return error;
    728  1.164.6.2  ad }
    729  1.164.6.2  ad 
    730  1.164.6.2  ad /*
    731  1.164.6.2  ad  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
    732  1.164.6.2  ad  * This is mainly to avoid queueing async I/O requests when the nfsiods
    733  1.164.6.2  ad  * are all hung on a dead server.
    734  1.164.6.2  ad  */
    735  1.164.6.2  ad 
    736  1.164.6.2  ad int
    737  1.164.6.2  ad nfs_asyncio(bp)
    738  1.164.6.2  ad 	struct buf *bp;
    739  1.164.6.2  ad {
    740  1.164.6.2  ad 	int i;
    741  1.164.6.2  ad 	struct nfsmount *nmp;
    742  1.164.6.2  ad 	int gotiod, slptimeo = 0, error;
    743  1.164.6.2  ad 	bool catch = false;
    744  1.164.6.2  ad 
    745  1.164.6.2  ad 	if (nfs_numasync == 0)
    746  1.164.6.2  ad 		return (EIO);
    747  1.164.6.2  ad 
    748  1.164.6.2  ad 	nmp = VFSTONFS(bp->b_vp->v_mount);
    749  1.164.6.2  ad again:
    750  1.164.6.2  ad 	if (nmp->nm_flag & NFSMNT_INT)
    751  1.164.6.2  ad 		catch = true;
    752  1.164.6.2  ad 	gotiod = false;
    753  1.164.6.2  ad 
    754  1.164.6.2  ad 	/*
    755  1.164.6.2  ad 	 * Find a free iod to process this request.
    756  1.164.6.2  ad 	 */
    757  1.164.6.2  ad 
    758  1.164.6.2  ad 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
    759  1.164.6.2  ad 		struct nfs_iod *iod = &nfs_asyncdaemon[i];
    760  1.164.6.2  ad 
    761  1.164.6.2  ad 		mutex_enter(&iod->nid_lock);
    762  1.164.6.2  ad 		if (iod->nid_want) {
    763  1.164.6.2  ad 			/*
    764  1.164.6.2  ad 			 * Found one, so wake it up and tell it which
    765  1.164.6.2  ad 			 * mount to process.
    766  1.164.6.2  ad 			 */
    767  1.164.6.2  ad 			iod->nid_want = false;
    768  1.164.6.2  ad 			iod->nid_mount = nmp;
    769  1.164.6.2  ad 			cv_signal(&iod->nid_cv);
    770  1.164.6.2  ad 			mutex_enter(&nmp->nm_lock);
    771  1.164.6.2  ad 			mutex_exit(&iod->nid_lock);
    772  1.164.6.2  ad 			nmp->nm_bufqiods++;
    773  1.164.6.2  ad 			gotiod = true;
    774  1.164.6.2  ad 			break;
    775  1.164.6.2  ad 		}
    776  1.164.6.2  ad 		mutex_exit(&iod->nid_lock);
    777  1.164.6.2  ad 	}
    778  1.164.6.2  ad 
    779  1.164.6.2  ad 	/*
    780  1.164.6.2  ad 	 * If none are free, we may already have an iod working on this mount
    781  1.164.6.2  ad 	 * point.  If so, it will process our request.
    782  1.164.6.2  ad 	 */
    783  1.164.6.2  ad 
    784  1.164.6.2  ad 	if (!gotiod) {
    785  1.164.6.2  ad 		mutex_enter(&nmp->nm_lock);
    786  1.164.6.2  ad 		if (nmp->nm_bufqiods > 0)
    787  1.164.6.2  ad 			gotiod = true;
    788  1.164.6.2  ad 	}
    789  1.164.6.2  ad 
    790  1.164.6.2  ad 	KASSERT(mutex_owned(&nmp->nm_lock));
    791  1.164.6.2  ad 
    792  1.164.6.2  ad 	/*
    793  1.164.6.2  ad 	 * If we have an iod which can process the request, then queue
    794  1.164.6.2  ad 	 * the buffer.  However, even if we have an iod, do not initiate
    795  1.164.6.2  ad 	 * queue cleaning if curproc is the pageout daemon. if the NFS mount
    796  1.164.6.2  ad 	 * is via local loopback, we may put curproc (pagedaemon) to sleep
    797  1.164.6.2  ad 	 * waiting for the writes to complete. But the server (ourself)
    798  1.164.6.2  ad 	 * may block the write, waiting for its (ie., our) pagedaemon
    799  1.164.6.2  ad 	 * to produce clean pages to handle the write: deadlock.
    800  1.164.6.2  ad 	 * XXX: start non-loopback mounts straight away?  If "lots free",
    801  1.164.6.2  ad 	 * let pagedaemon start loopback writes anyway?
    802  1.164.6.2  ad 	 */
    803  1.164.6.2  ad 	if (gotiod) {
    804  1.164.6.2  ad 
    805  1.164.6.2  ad 		/*
    806  1.164.6.2  ad 		 * Ensure that the queue never grows too large.
    807  1.164.6.2  ad 		 */
    808  1.164.6.2  ad 		if (curlwp == uvm.pagedaemon_lwp) {
    809  1.164.6.2  ad 	  		/* Enque for later, to avoid free-page deadlock */
    810  1.164.6.2  ad 		} else while (nmp->nm_bufqlen >= 2*nfs_numasync) {
    811  1.164.6.2  ad 			if (catch) {
    812  1.164.6.2  ad 				error = cv_timedwait_sig(&nmp->nm_aiocv,
    813  1.164.6.2  ad 				    &nmp->nm_lock, slptimeo);
    814  1.164.6.2  ad 			} else {
    815  1.164.6.2  ad 				error = cv_timedwait(&nmp->nm_aiocv,
    816  1.164.6.2  ad 				    &nmp->nm_lock, slptimeo);
    817  1.164.6.2  ad 			}
    818  1.164.6.2  ad 			if (error) {
    819  1.164.6.2  ad 				if (nfs_sigintr(nmp, NULL, curlwp)) {
    820  1.164.6.2  ad 					mutex_exit(&nmp->nm_lock);
    821  1.164.6.2  ad 					return (EINTR);
    822  1.164.6.2  ad 				}
    823  1.164.6.2  ad 				if (catch) {
    824  1.164.6.2  ad 					catch = false;
    825  1.164.6.2  ad 					slptimeo = 2 * hz;
    826  1.164.6.2  ad 				}
    827  1.164.6.2  ad 			}
    828  1.164.6.2  ad 
    829  1.164.6.2  ad 			/*
    830  1.164.6.2  ad 			 * We might have lost our iod while sleeping,
    831  1.164.6.2  ad 			 * so check and loop if necessary.
    832  1.164.6.2  ad 			 */
    833  1.164.6.2  ad 
    834  1.164.6.2  ad 			if (nmp->nm_bufqiods == 0) {
    835  1.164.6.2  ad 				mutex_exit(&nmp->nm_lock);
    836  1.164.6.2  ad 				goto again;
    837  1.164.6.2  ad 			}
    838  1.164.6.2  ad 		}
    839  1.164.6.2  ad 		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
    840  1.164.6.2  ad 		nmp->nm_bufqlen++;
    841  1.164.6.2  ad 		mutex_exit(&nmp->nm_lock);
    842  1.164.6.2  ad 		return (0);
    843  1.164.6.2  ad 	}
    844  1.164.6.2  ad 	mutex_exit(&nmp->nm_lock);
    845  1.164.6.2  ad 
    846  1.164.6.2  ad 	/*
    847  1.164.6.2  ad 	 * All the iods are busy on other mounts, so return EIO to
    848  1.164.6.2  ad 	 * force the caller to process the i/o synchronously.
    849  1.164.6.2  ad 	 */
    850  1.164.6.2  ad 
    851  1.164.6.2  ad 	return (EIO);
    852  1.164.6.2  ad }
    853  1.164.6.2  ad 
    854  1.164.6.2  ad /*
    855  1.164.6.2  ad  * nfs_doio for read.
    856  1.164.6.2  ad  */
    857  1.164.6.2  ad static int
    858  1.164.6.2  ad nfs_doio_read(bp, uiop)
    859  1.164.6.2  ad 	struct buf *bp;
    860  1.164.6.2  ad 	struct uio *uiop;
    861  1.164.6.2  ad {
    862  1.164.6.2  ad 	struct vnode *vp = bp->b_vp;
    863  1.164.6.2  ad 	struct nfsnode *np = VTONFS(vp);
    864  1.164.6.2  ad 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    865  1.164.6.2  ad 	int error = 0;
    866  1.164.6.2  ad 
    867  1.164.6.2  ad 	uiop->uio_rw = UIO_READ;
    868  1.164.6.2  ad 	switch (vp->v_type) {
    869  1.164.6.2  ad 	case VREG:
    870  1.164.6.2  ad 		nfsstats.read_bios++;
    871  1.164.6.2  ad 		error = nfs_readrpc(vp, uiop);
    872  1.164.6.2  ad 		if (!error && uiop->uio_resid) {
    873  1.164.6.2  ad 			int diff, len;
    874  1.164.6.2  ad 
    875  1.164.6.2  ad 			/*
    876  1.164.6.2  ad 			 * If uio_resid > 0, there is a hole in the file and
    877  1.164.6.2  ad 			 * no writes after the hole have been pushed to
    878  1.164.6.2  ad 			 * the server yet or the file has been truncated
    879  1.164.6.2  ad 			 * on the server.
    880  1.164.6.2  ad 			 * Just zero fill the rest of the valid area.
    881  1.164.6.2  ad 			 */
    882  1.164.6.2  ad 
    883  1.164.6.2  ad 			KASSERT(vp->v_size >=
    884  1.164.6.2  ad 			    uiop->uio_offset + uiop->uio_resid);
    885  1.164.6.2  ad 			diff = bp->b_bcount - uiop->uio_resid;
    886  1.164.6.2  ad 			len = uiop->uio_resid;
    887  1.164.6.2  ad 			memset((char *)bp->b_data + diff, 0, len);
    888  1.164.6.2  ad 			uiop->uio_resid = 0;
    889  1.164.6.2  ad 		}
    890  1.164.6.2  ad #if 0
    891  1.164.6.2  ad 		if (uiop->uio_lwp && (vp->v_flag & VTEXT) &&
    892  1.164.6.2  ad 		    timespeccmp(&np->n_mtime, &np->n_vattr->va_mtime, !=)) {
    893  1.164.6.2  ad 			killproc(uiop->uio_lwp->l_proc, "process text file was modified");
    894  1.164.6.2  ad #if 0 /* XXX NJWLWP */
    895  1.164.6.2  ad 			uiop->uio_lwp->l_proc->p_holdcnt++;
    896  1.164.6.2  ad #endif
    897  1.164.6.2  ad 		}
    898  1.164.6.2  ad #endif
    899  1.164.6.2  ad 		break;
    900  1.164.6.2  ad 	case VLNK:
    901  1.164.6.2  ad 		KASSERT(uiop->uio_offset == (off_t)0);
    902  1.164.6.2  ad 		nfsstats.readlink_bios++;
    903  1.164.6.2  ad 		error = nfs_readlinkrpc(vp, uiop, np->n_rcred);
    904  1.164.6.2  ad 		break;
    905  1.164.6.2  ad 	case VDIR:
    906  1.164.6.2  ad 		nfsstats.readdir_bios++;
    907  1.164.6.2  ad 		uiop->uio_offset = bp->b_dcookie;
    908  1.164.6.2  ad #ifndef NFS_V2_ONLY
    909  1.164.6.2  ad 		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
    910  1.164.6.2  ad 			error = nfs_readdirplusrpc(vp, uiop,
    911  1.164.6.2  ad 			    curlwp->l_cred);
    912  1.164.6.2  ad 			/*
    913  1.164.6.2  ad 			 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
    914  1.164.6.2  ad 			 */
    915  1.164.6.2  ad 			if (error == ENOTSUP)
    916  1.164.6.2  ad 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
    917  1.164.6.2  ad 		}
    918  1.164.6.2  ad #else
    919  1.164.6.2  ad 		nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
    920  1.164.6.2  ad #endif
    921  1.164.6.2  ad 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
    922  1.164.6.2  ad 			error = nfs_readdirrpc(vp, uiop,
    923  1.164.6.2  ad 			    curlwp->l_cred);
    924  1.164.6.2  ad 		if (!error) {
    925  1.164.6.2  ad 			bp->b_dcookie = uiop->uio_offset;
    926  1.164.6.2  ad 		}
    927  1.164.6.2  ad 		break;
    928  1.164.6.2  ad 	default:
    929  1.164.6.2  ad 		printf("nfs_doio:  type %x unexpected\n", vp->v_type);
    930  1.164.6.2  ad 		break;
    931  1.164.6.2  ad 	}
    932  1.164.6.2  ad 	if (error) {
    933  1.164.6.2  ad 		bp->b_error = error;
    934  1.164.6.2  ad 	}
    935  1.164.6.2  ad 	return error;
    936  1.164.6.2  ad }
    937  1.164.6.2  ad 
    938  1.164.6.2  ad /*
    939  1.164.6.2  ad  * nfs_doio for write.
    940  1.164.6.2  ad  */
    941  1.164.6.2  ad static int
    942  1.164.6.2  ad nfs_doio_write(bp, uiop)
    943  1.164.6.2  ad 	struct buf *bp;
    944  1.164.6.2  ad 	struct uio *uiop;
    945  1.164.6.2  ad {
    946  1.164.6.2  ad 	struct vnode *vp = bp->b_vp;
    947  1.164.6.2  ad 	struct nfsnode *np = VTONFS(vp);
    948  1.164.6.2  ad 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    949  1.164.6.2  ad 	int iomode;
    950  1.164.6.2  ad 	bool stalewriteverf = false;
    951  1.164.6.2  ad 	int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
    952  1.164.6.2  ad 	struct vm_page *pgs[npages];
    953  1.164.6.2  ad #ifndef NFS_V2_ONLY
    954  1.164.6.2  ad 	bool needcommit = true; /* need only COMMIT RPC */
    955  1.164.6.2  ad #else
    956  1.164.6.2  ad 	bool needcommit = false; /* need only COMMIT RPC */
    957  1.164.6.2  ad #endif
    958  1.164.6.2  ad 	bool pageprotected;
    959  1.164.6.2  ad 	struct uvm_object *uobj = &vp->v_uobj;
    960  1.164.6.2  ad 	int error;
    961  1.164.6.2  ad 	off_t off, cnt;
    962  1.164.6.2  ad 
    963  1.164.6.2  ad 	if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
    964  1.164.6.2  ad 		iomode = NFSV3WRITE_UNSTABLE;
    965  1.164.6.2  ad 	} else {
    966  1.164.6.2  ad 		iomode = NFSV3WRITE_FILESYNC;
    967  1.164.6.2  ad 	}
    968  1.164.6.2  ad 
    969  1.164.6.2  ad #ifndef NFS_V2_ONLY
    970  1.164.6.2  ad again:
    971  1.164.6.2  ad #endif
    972  1.164.6.2  ad 	rw_enter(&nmp->nm_writeverflock, RW_READER);
    973  1.164.6.2  ad 
    974  1.164.6.2  ad 	for (i = 0; i < npages; i++) {
    975  1.164.6.2  ad 		pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
    976  1.164.6.2  ad 		if (pgs[i]->uobject == uobj &&
    977  1.164.6.2  ad 		    pgs[i]->offset == uiop->uio_offset + (i << PAGE_SHIFT)) {
    978  1.164.6.2  ad 			KASSERT(pgs[i]->flags & PG_BUSY);
    979  1.164.6.2  ad 			/*
    980  1.164.6.2  ad 			 * this page belongs to our object.
    981  1.164.6.2  ad 			 */
    982  1.164.6.2  ad 			simple_lock(&uobj->vmobjlock);
    983  1.164.6.2  ad 			/*
    984  1.164.6.2  ad 			 * write out the page stably if it's about to
    985  1.164.6.2  ad 			 * be released because we can't resend it
    986  1.164.6.2  ad 			 * on the server crash.
    987  1.164.6.2  ad 			 *
    988  1.164.6.2  ad 			 * XXX assuming PG_RELEASE|PG_PAGEOUT won't be
    989  1.164.6.2  ad 			 * changed until unbusy the page.
    990  1.164.6.2  ad 			 */
    991  1.164.6.2  ad 			if (pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT))
    992  1.164.6.2  ad 				iomode = NFSV3WRITE_FILESYNC;
    993  1.164.6.2  ad 			/*
    994  1.164.6.2  ad 			 * if we met a page which hasn't been sent yet,
    995  1.164.6.2  ad 			 * we need do WRITE RPC.
    996  1.164.6.2  ad 			 */
    997  1.164.6.2  ad 			if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0)
    998  1.164.6.2  ad 				needcommit = false;
    999  1.164.6.2  ad 			simple_unlock(&uobj->vmobjlock);
   1000  1.164.6.2  ad 		} else {
   1001  1.164.6.2  ad 			iomode = NFSV3WRITE_FILESYNC;
   1002  1.164.6.2  ad 			needcommit = false;
   1003  1.164.6.2  ad 		}
   1004  1.164.6.2  ad 	}
   1005  1.164.6.2  ad 	if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
   1006  1.164.6.2  ad 		simple_lock(&uobj->vmobjlock);
   1007  1.164.6.2  ad 		for (i = 0; i < npages; i++) {
   1008  1.164.6.2  ad 			pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
   1009  1.164.6.2  ad 			pmap_page_protect(pgs[i], VM_PROT_READ);
   1010  1.164.6.2  ad 		}
   1011  1.164.6.2  ad 		simple_unlock(&uobj->vmobjlock);
   1012  1.164.6.2  ad 		pageprotected = true; /* pages can't be modified during i/o. */
   1013  1.164.6.2  ad 	} else
   1014  1.164.6.2  ad 		pageprotected = false;
   1015  1.164.6.2  ad 
   1016  1.164.6.2  ad 	/*
   1017  1.164.6.2  ad 	 * Send the data to the server if necessary,
   1018  1.164.6.2  ad 	 * otherwise just send a commit rpc.
   1019  1.164.6.2  ad 	 */
   1020  1.164.6.2  ad #ifndef NFS_V2_ONLY
   1021  1.164.6.2  ad 	if (needcommit) {
   1022  1.164.6.2  ad 
   1023  1.164.6.2  ad 		/*
   1024  1.164.6.2  ad 		 * If the buffer is in the range that we already committed,
   1025  1.164.6.2  ad 		 * there's nothing to do.
   1026  1.164.6.2  ad 		 *
   1027  1.164.6.2  ad 		 * If it's in the range that we need to commit, push the
   1028  1.164.6.2  ad 		 * whole range at once, otherwise only push the buffer.
   1029  1.164.6.2  ad 		 * In both these cases, acquire the commit lock to avoid
   1030  1.164.6.2  ad 		 * other processes modifying the range.
   1031  1.164.6.2  ad 		 */
   1032  1.164.6.2  ad 
   1033  1.164.6.2  ad 		off = uiop->uio_offset;
   1034  1.164.6.2  ad 		cnt = bp->b_bcount;
   1035  1.164.6.2  ad 		mutex_enter(&np->n_commitlock);
   1036  1.164.6.2  ad 		if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
   1037  1.164.6.2  ad 			bool pushedrange;
   1038  1.164.6.2  ad 			if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
   1039  1.164.6.2  ad 				pushedrange = true;
   1040  1.164.6.2  ad 				off = np->n_pushlo;
   1041  1.164.6.2  ad 				cnt = np->n_pushhi - np->n_pushlo;
   1042  1.164.6.2  ad 			} else {
   1043  1.164.6.2  ad 				pushedrange = false;
   1044  1.164.6.2  ad 			}
   1045  1.164.6.2  ad 			error = nfs_commit(vp, off, cnt, curlwp);
   1046  1.164.6.2  ad 			if (error == 0) {
   1047  1.164.6.2  ad 				if (pushedrange) {
   1048  1.164.6.2  ad 					nfs_merge_commit_ranges(vp);
   1049  1.164.6.2  ad 				} else {
   1050  1.164.6.2  ad 					nfs_add_committed_range(vp, off, cnt);
   1051  1.164.6.2  ad 				}
   1052  1.164.6.2  ad 			}
   1053  1.164.6.2  ad 		} else {
   1054  1.164.6.2  ad 			error = 0;
   1055  1.164.6.2  ad 		}
   1056  1.164.6.2  ad 		mutex_exit(&np->n_commitlock);
   1057  1.164.6.2  ad 		rw_exit(&nmp->nm_writeverflock);
   1058  1.164.6.2  ad 		if (!error) {
   1059  1.164.6.2  ad 			/*
   1060  1.164.6.2  ad 			 * pages are now on stable storage.
   1061  1.164.6.2  ad 			 */
   1062  1.164.6.2  ad 			uiop->uio_resid = 0;
   1063  1.164.6.2  ad 			simple_lock(&uobj->vmobjlock);
   1064  1.164.6.2  ad 			for (i = 0; i < npages; i++) {
   1065  1.164.6.2  ad 				pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
   1066  1.164.6.2  ad 			}
   1067  1.164.6.2  ad 			simple_unlock(&uobj->vmobjlock);
   1068  1.164.6.2  ad 			return 0;
   1069  1.164.6.2  ad 		} else if (error == NFSERR_STALEWRITEVERF) {
   1070  1.164.6.2  ad 			nfs_clearcommit(vp->v_mount);
   1071  1.164.6.2  ad 			goto again;
   1072  1.164.6.2  ad 		}
   1073  1.164.6.2  ad 		if (error) {
   1074  1.164.6.2  ad 			bp->b_error = np->n_error = error;
   1075  1.164.6.2  ad 			np->n_flag |= NWRITEERR;
   1076  1.164.6.2  ad 		}
   1077  1.164.6.2  ad 		return error;
   1078  1.164.6.2  ad 	}
   1079  1.164.6.2  ad #endif
   1080  1.164.6.2  ad 	off = uiop->uio_offset;
   1081  1.164.6.2  ad 	cnt = bp->b_bcount;
   1082  1.164.6.2  ad 	uiop->uio_rw = UIO_WRITE;
   1083  1.164.6.2  ad 	nfsstats.write_bios++;
   1084  1.164.6.2  ad 	error = nfs_writerpc(vp, uiop, &iomode, pageprotected, &stalewriteverf);
   1085  1.164.6.2  ad #ifndef NFS_V2_ONLY
   1086  1.164.6.2  ad 	if (!error && iomode == NFSV3WRITE_UNSTABLE) {
   1087  1.164.6.2  ad 		/*
   1088  1.164.6.2  ad 		 * we need to commit pages later.
   1089  1.164.6.2  ad 		 */
   1090  1.164.6.2  ad 		mutex_enter(&np->n_commitlock);
   1091  1.164.6.2  ad 		nfs_add_tobecommitted_range(vp, off, cnt);
   1092  1.164.6.2  ad 		/*
   1093  1.164.6.2  ad 		 * if there can be too many uncommitted pages, commit them now.
   1094  1.164.6.2  ad 		 */
   1095  1.164.6.2  ad 		if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
   1096  1.164.6.2  ad 			off = np->n_pushlo;
   1097  1.164.6.2  ad 			cnt = nfs_commitsize >> 1;
   1098  1.164.6.2  ad 			error = nfs_commit(vp, off, cnt, curlwp);
   1099  1.164.6.2  ad 			if (!error) {
   1100  1.164.6.2  ad 				nfs_add_committed_range(vp, off, cnt);
   1101  1.164.6.2  ad 				nfs_del_tobecommitted_range(vp, off, cnt);
   1102  1.164.6.2  ad 			}
   1103  1.164.6.2  ad 			if (error == NFSERR_STALEWRITEVERF) {
   1104  1.164.6.2  ad 				stalewriteverf = true;
   1105  1.164.6.2  ad 				error = 0; /* it isn't a real error */
   1106  1.164.6.2  ad 			}
   1107  1.164.6.2  ad 		} else {
   1108  1.164.6.2  ad 			/*
   1109  1.164.6.2  ad 			 * re-dirty pages so that they will be passed
   1110  1.164.6.2  ad 			 * to us later again.
   1111  1.164.6.2  ad 			 */
   1112  1.164.6.2  ad 			simple_lock(&uobj->vmobjlock);
   1113  1.164.6.2  ad 			for (i = 0; i < npages; i++) {
   1114  1.164.6.2  ad 				pgs[i]->flags &= ~PG_CLEAN;
   1115  1.164.6.2  ad 			}
   1116  1.164.6.2  ad 			simple_unlock(&uobj->vmobjlock);
   1117  1.164.6.2  ad 		}
   1118  1.164.6.2  ad 		mutex_exit(&np->n_commitlock);
   1119  1.164.6.2  ad 	} else
   1120  1.164.6.2  ad #endif
   1121  1.164.6.2  ad 	if (!error) {
   1122  1.164.6.2  ad 		/*
   1123  1.164.6.2  ad 		 * pages are now on stable storage.
   1124  1.164.6.2  ad 		 */
   1125  1.164.6.2  ad 		mutex_enter(&np->n_commitlock);
   1126  1.164.6.2  ad 		nfs_del_committed_range(vp, off, cnt);
   1127  1.164.6.2  ad 		mutex_exit(&np->n_commitlock);
   1128  1.164.6.2  ad 		simple_lock(&uobj->vmobjlock);
   1129  1.164.6.2  ad 		for (i = 0; i < npages; i++) {
   1130  1.164.6.2  ad 			pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
   1131  1.164.6.2  ad 		}
   1132  1.164.6.2  ad 		simple_unlock(&uobj->vmobjlock);
   1133  1.164.6.2  ad 	} else {
   1134  1.164.6.2  ad 		/*
   1135  1.164.6.2  ad 		 * we got an error.
   1136  1.164.6.2  ad 		 */
   1137  1.164.6.2  ad 		bp->b_error = np->n_error = error;
   1138  1.164.6.2  ad 		np->n_flag |= NWRITEERR;
   1139  1.164.6.2  ad 	}
   1140  1.164.6.2  ad 
   1141  1.164.6.2  ad 	rw_exit(&nmp->nm_writeverflock);
   1142  1.164.6.2  ad 
   1143  1.164.6.2  ad 	if (stalewriteverf) {
   1144  1.164.6.2  ad 		nfs_clearcommit(vp->v_mount);
   1145  1.164.6.2  ad 	}
   1146  1.164.6.2  ad 	return error;
   1147  1.164.6.2  ad }
   1148  1.164.6.2  ad 
   1149  1.164.6.2  ad /*
   1150  1.164.6.2  ad  * nfs_doio for B_PHYS.
   1151  1.164.6.2  ad  */
   1152  1.164.6.2  ad static int
   1153  1.164.6.2  ad nfs_doio_phys(bp, uiop)
   1154  1.164.6.2  ad 	struct buf *bp;
   1155  1.164.6.2  ad 	struct uio *uiop;
   1156  1.164.6.2  ad {
   1157  1.164.6.2  ad 	struct vnode *vp = bp->b_vp;
   1158  1.164.6.2  ad 	int error;
   1159  1.164.6.2  ad 
   1160  1.164.6.2  ad 	uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
   1161  1.164.6.2  ad 	if (bp->b_flags & B_READ) {
   1162  1.164.6.2  ad 		uiop->uio_rw = UIO_READ;
   1163  1.164.6.2  ad 		nfsstats.read_physios++;
   1164  1.164.6.2  ad 		error = nfs_readrpc(vp, uiop);
   1165  1.164.6.2  ad 	} else {
   1166  1.164.6.2  ad 		int iomode = NFSV3WRITE_DATASYNC;
   1167  1.164.6.2  ad 		bool stalewriteverf;
   1168  1.164.6.2  ad 		struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1169  1.164.6.2  ad 
   1170  1.164.6.2  ad 		uiop->uio_rw = UIO_WRITE;
   1171  1.164.6.2  ad 		nfsstats.write_physios++;
   1172  1.164.6.2  ad 		rw_enter(&nmp->nm_writeverflock, RW_READER);
   1173  1.164.6.2  ad 		error = nfs_writerpc(vp, uiop, &iomode, false, &stalewriteverf);
   1174  1.164.6.2  ad 		rw_exit(&nmp->nm_writeverflock);
   1175  1.164.6.2  ad 		if (stalewriteverf) {
   1176  1.164.6.2  ad 			nfs_clearcommit(bp->b_vp->v_mount);
   1177  1.164.6.2  ad 		}
   1178  1.164.6.2  ad 	}
   1179  1.164.6.2  ad 	if (error) {
   1180  1.164.6.2  ad 		bp->b_error = error;
   1181  1.164.6.2  ad 	}
   1182  1.164.6.2  ad 	return error;
   1183  1.164.6.2  ad }
   1184  1.164.6.2  ad 
   1185  1.164.6.2  ad /*
   1186  1.164.6.2  ad  * Do an I/O operation to/from a cache block. This may be called
   1187  1.164.6.2  ad  * synchronously or from an nfsiod.
   1188  1.164.6.2  ad  */
   1189  1.164.6.2  ad int
   1190  1.164.6.2  ad nfs_doio(bp)
   1191  1.164.6.2  ad 	struct buf *bp;
   1192  1.164.6.2  ad {
   1193  1.164.6.2  ad 	int error;
   1194  1.164.6.2  ad 	struct uio uio;
   1195  1.164.6.2  ad 	struct uio *uiop = &uio;
   1196  1.164.6.2  ad 	struct iovec io;
   1197  1.164.6.2  ad 	UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
   1198  1.164.6.2  ad 
   1199  1.164.6.2  ad 	uiop->uio_iov = &io;
   1200  1.164.6.2  ad 	uiop->uio_iovcnt = 1;
   1201  1.164.6.2  ad 	uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
   1202  1.164.6.2  ad 	UIO_SETUP_SYSSPACE(uiop);
   1203  1.164.6.2  ad 	io.iov_base = bp->b_data;
   1204  1.164.6.2  ad 	io.iov_len = uiop->uio_resid = bp->b_bcount;
   1205  1.164.6.2  ad 
   1206  1.164.6.2  ad 	/*
   1207  1.164.6.2  ad 	 * Historically, paging was done with physio, but no more...
   1208  1.164.6.2  ad 	 */
   1209  1.164.6.2  ad 	if (bp->b_flags & B_PHYS) {
   1210  1.164.6.2  ad 		/*
   1211  1.164.6.2  ad 		 * ...though reading /dev/drum still gets us here.
   1212  1.164.6.2  ad 		 */
   1213  1.164.6.2  ad 		error = nfs_doio_phys(bp, uiop);
   1214  1.164.6.2  ad 	} else if (bp->b_flags & B_READ) {
   1215  1.164.6.2  ad 		error = nfs_doio_read(bp, uiop);
   1216  1.164.6.2  ad 	} else {
   1217  1.164.6.2  ad 		error = nfs_doio_write(bp, uiop);
   1218  1.164.6.2  ad 	}
   1219  1.164.6.2  ad 	bp->b_resid = uiop->uio_resid;
   1220  1.164.6.2  ad 	biodone(bp);
   1221  1.164.6.2  ad 	return (error);
   1222  1.164.6.2  ad }
   1223  1.164.6.2  ad 
   1224  1.164.6.2  ad /*
   1225  1.164.6.2  ad  * Vnode op for VM getpages.
   1226  1.164.6.2  ad  */
   1227  1.164.6.2  ad 
   1228  1.164.6.2  ad int
   1229  1.164.6.2  ad nfs_getpages(v)
   1230  1.164.6.2  ad 	void *v;
   1231  1.164.6.2  ad {
   1232  1.164.6.2  ad 	struct vop_getpages_args /* {
   1233  1.164.6.2  ad 		struct vnode *a_vp;
   1234  1.164.6.2  ad 		voff_t a_offset;
   1235  1.164.6.2  ad 		struct vm_page **a_m;
   1236  1.164.6.2  ad 		int *a_count;
   1237  1.164.6.2  ad 		int a_centeridx;
   1238  1.164.6.2  ad 		vm_prot_t a_access_type;
   1239  1.164.6.2  ad 		int a_advice;
   1240  1.164.6.2  ad 		int a_flags;
   1241  1.164.6.2  ad 	} */ *ap = v;
   1242  1.164.6.2  ad 
   1243  1.164.6.2  ad 	struct vnode *vp = ap->a_vp;
   1244  1.164.6.2  ad 	struct uvm_object *uobj = &vp->v_uobj;
   1245  1.164.6.2  ad 	struct nfsnode *np = VTONFS(vp);
   1246  1.164.6.2  ad 	const int npages = *ap->a_count;
   1247  1.164.6.2  ad 	struct vm_page *pg, **pgs, *opgs[npages];
   1248  1.164.6.2  ad 	off_t origoffset, len;
   1249  1.164.6.2  ad 	int i, error;
   1250  1.164.6.2  ad 	bool v3 = NFS_ISV3(vp);
   1251  1.164.6.2  ad 	bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   1252  1.164.6.2  ad 	bool locked = (ap->a_flags & PGO_LOCKED) != 0;
   1253  1.164.6.2  ad 
   1254  1.164.6.2  ad 	/*
   1255  1.164.6.2  ad 	 * call the genfs code to get the pages.  `pgs' may be NULL
   1256  1.164.6.2  ad 	 * when doing read-ahead.
   1257  1.164.6.2  ad 	 */
   1258  1.164.6.2  ad 
   1259  1.164.6.2  ad 	pgs = ap->a_m;
   1260  1.164.6.2  ad 	if (write && locked && v3) {
   1261  1.164.6.2  ad 		KASSERT(pgs != NULL);
   1262  1.164.6.2  ad #ifdef DEBUG
   1263  1.164.6.2  ad 
   1264  1.164.6.2  ad 		/*
   1265  1.164.6.2  ad 		 * If PGO_LOCKED is set, real pages shouldn't exists
   1266  1.164.6.2  ad 		 * in the array.
   1267  1.164.6.2  ad 		 */
   1268  1.164.6.2  ad 
   1269  1.164.6.2  ad 		for (i = 0; i < npages; i++)
   1270  1.164.6.2  ad 			KDASSERT(pgs[i] == NULL || pgs[i] == PGO_DONTCARE);
   1271  1.164.6.2  ad #endif
   1272  1.164.6.2  ad 		memcpy(opgs, pgs, npages * sizeof(struct vm_pages *));
   1273  1.164.6.2  ad 	}
   1274  1.164.6.2  ad 	error = genfs_getpages(v);
   1275  1.164.6.2  ad 	if (error) {
   1276  1.164.6.2  ad 		return (error);
   1277  1.164.6.2  ad 	}
   1278  1.164.6.2  ad 
   1279  1.164.6.2  ad 	/*
   1280  1.164.6.2  ad 	 * for read faults where the nfs node is not yet marked NMODIFIED,
   1281  1.164.6.2  ad 	 * set PG_RDONLY on the pages so that we come back here if someone
   1282  1.164.6.2  ad 	 * tries to modify later via the mapping that will be entered for
   1283  1.164.6.2  ad 	 * this fault.
   1284  1.164.6.2  ad 	 */
   1285  1.164.6.2  ad 
   1286  1.164.6.2  ad 	if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
   1287  1.164.6.2  ad 		if (!locked) {
   1288  1.164.6.2  ad 			simple_lock(&uobj->vmobjlock);
   1289  1.164.6.2  ad 		}
   1290  1.164.6.2  ad 		for (i = 0; i < npages; i++) {
   1291  1.164.6.2  ad 			pg = pgs[i];
   1292  1.164.6.2  ad 			if (pg == NULL || pg == PGO_DONTCARE) {
   1293  1.164.6.2  ad 				continue;
   1294  1.164.6.2  ad 			}
   1295  1.164.6.2  ad 			pg->flags |= PG_RDONLY;
   1296  1.164.6.2  ad 		}
   1297  1.164.6.2  ad 		if (!locked) {
   1298  1.164.6.2  ad 			simple_unlock(&uobj->vmobjlock);
   1299  1.164.6.2  ad 		}
   1300  1.164.6.2  ad 	}
   1301  1.164.6.2  ad 	if (!write) {
   1302  1.164.6.2  ad 		return (0);
   1303  1.164.6.2  ad 	}
   1304  1.164.6.2  ad 
   1305  1.164.6.2  ad 	/*
   1306  1.164.6.2  ad 	 * this is a write fault, update the commit info.
   1307  1.164.6.2  ad 	 */
   1308  1.164.6.2  ad 
   1309  1.164.6.2  ad 	origoffset = ap->a_offset;
   1310  1.164.6.2  ad 	len = npages << PAGE_SHIFT;
   1311  1.164.6.2  ad 
   1312  1.164.6.2  ad 	if (v3) {
   1313  1.164.6.2  ad 		if (!locked) {
   1314  1.164.6.2  ad 			mutex_enter(&np->n_commitlock);
   1315  1.164.6.2  ad 		} else {
   1316  1.164.6.2  ad 			if (!mutex_tryenter(&np->n_commitlock)) {
   1317  1.164.6.2  ad 
   1318  1.164.6.2  ad 				/*
   1319  1.164.6.2  ad 				 * Since PGO_LOCKED is set, we need to unbusy
   1320  1.164.6.2  ad 				 * all pages fetched by genfs_getpages() above,
   1321  1.164.6.2  ad 				 * tell the caller that there are no pages
   1322  1.164.6.2  ad 				 * available and put back original pgs array.
   1323  1.164.6.2  ad 				 */
   1324  1.164.6.2  ad 
   1325  1.164.6.2  ad 				uvm_lock_pageq();
   1326  1.164.6.2  ad 				uvm_page_unbusy(pgs, npages);
   1327  1.164.6.2  ad 				uvm_unlock_pageq();
   1328  1.164.6.2  ad 				*ap->a_count = 0;
   1329  1.164.6.2  ad 				memcpy(pgs, opgs,
   1330  1.164.6.2  ad 				    npages * sizeof(struct vm_pages *));
   1331  1.164.6.2  ad 				return EBUSY;
   1332  1.164.6.2  ad 			}
   1333  1.164.6.2  ad 		}
   1334  1.164.6.2  ad 		nfs_del_committed_range(vp, origoffset, len);
   1335  1.164.6.2  ad 		nfs_del_tobecommitted_range(vp, origoffset, len);
   1336  1.164.6.2  ad 	}
   1337  1.164.6.2  ad 	np->n_flag |= NMODIFIED;
   1338  1.164.6.2  ad 	if (!locked) {
   1339  1.164.6.2  ad 		simple_lock(&uobj->vmobjlock);
   1340  1.164.6.2  ad 	}
   1341  1.164.6.2  ad 	for (i = 0; i < npages; i++) {
   1342  1.164.6.2  ad 		pg = pgs[i];
   1343  1.164.6.2  ad 		if (pg == NULL || pg == PGO_DONTCARE) {
   1344  1.164.6.2  ad 			continue;
   1345  1.164.6.2  ad 		}
   1346  1.164.6.2  ad 		pg->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
   1347  1.164.6.2  ad 	}
   1348  1.164.6.2  ad 	if (!locked) {
   1349  1.164.6.2  ad 		simple_unlock(&uobj->vmobjlock);
   1350  1.164.6.2  ad 	}
   1351  1.164.6.2  ad 	if (v3) {
   1352  1.164.6.2  ad 		mutex_exit(&np->n_commitlock);
   1353  1.164.6.2  ad 	}
   1354  1.164.6.2  ad 	return (0);
   1355  1.164.6.2  ad }
   1356