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