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