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