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