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