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