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