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