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