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