Home | History | Annotate | Line # | Download | only in nfs
nfs_bio.c revision 1.188.2.3
      1 /*	$NetBSD: nfs_bio.c,v 1.188.2.3 2012/01/25 00:41:36 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.3 2012/01/25 00:41:36 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, 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 (intrflg && nfs_sigintr(nmp, NULL, l)) {
    654 			error = EINTR;
    655 			break;
    656 		}
    657 		error = vinvalbuf(vp, flags, cred, l, 0, slptimeo);
    658 	}
    659 	mutex_enter(vp->v_interlock);
    660 	if (error == 0)
    661 		np->n_flag &= ~NMODIFIED;
    662 	np->n_flag &= ~NFLUSHINPROG;
    663 	if (np->n_flag & NFLUSHWANT) {
    664 		np->n_flag &= ~NFLUSHWANT;
    665 		wakeup(&np->n_flag);
    666 	}
    667 	mutex_exit(vp->v_interlock);
    668 	return error;
    669 }
    670 
    671 /*
    672  * nfs_flushstalebuf: flush cache if it's stale.
    673  *
    674  * => caller shouldn't own any pages or buffers which belong to the vnode.
    675  */
    676 
    677 int
    678 nfs_flushstalebuf(struct vnode *vp, kauth_cred_t cred, struct lwp *l,
    679     int flags)
    680 {
    681 	struct nfsnode *np = VTONFS(vp);
    682 	struct vattr vattr;
    683 	int error;
    684 
    685 	if (np->n_flag & NMODIFIED) {
    686 		if ((flags & NFS_FLUSHSTALEBUF_MYWRITE) == 0
    687 		    || vp->v_type != VREG) {
    688 			error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
    689 			if (error)
    690 				return error;
    691 			if (vp->v_type == VDIR) {
    692 				nfs_invaldircache(vp, 0);
    693 			}
    694 		} else {
    695 			/*
    696 			 * XXX assuming writes are ours.
    697 			 */
    698 		}
    699 		NFS_INVALIDATE_ATTRCACHE(np);
    700 		error = VOP_GETATTR(vp, &vattr, cred);
    701 		if (error)
    702 			return error;
    703 		np->n_mtime = vattr.va_mtime;
    704 	} else {
    705 		error = VOP_GETATTR(vp, &vattr, cred);
    706 		if (error)
    707 			return error;
    708 		if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
    709 			if (vp->v_type == VDIR) {
    710 				nfs_invaldircache(vp, 0);
    711 			}
    712 			error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
    713 			if (error)
    714 				return error;
    715 			np->n_mtime = vattr.va_mtime;
    716 		}
    717 	}
    718 
    719 	return error;
    720 }
    721 
    722 /*
    723  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
    724  * This is mainly to avoid queueing async I/O requests when the nfsiods
    725  * are all hung on a dead server.
    726  */
    727 
    728 int
    729 nfs_asyncio(struct buf *bp)
    730 {
    731 	struct nfs_iod *iod;
    732 	struct nfsmount *nmp;
    733 	int slptimeo = 0, error;
    734 	bool catch = false;
    735 
    736 	if (nfs_numasync == 0)
    737 		return (EIO);
    738 
    739 	nmp = VFSTONFS(bp->b_vp->v_mount);
    740 again:
    741 	if (nmp->nm_flag & NFSMNT_INT)
    742 		catch = true;
    743 
    744 	/*
    745 	 * Find a free iod to process this request.
    746 	 */
    747 
    748 	mutex_enter(&nfs_iodlist_lock);
    749 	iod = LIST_FIRST(&nfs_iodlist_idle);
    750 	if (iod) {
    751 		/*
    752 		 * Found one, so wake it up and tell it which
    753 		 * mount to process.
    754 		 */
    755 		LIST_REMOVE(iod, nid_idle);
    756 		mutex_enter(&iod->nid_lock);
    757 		mutex_exit(&nfs_iodlist_lock);
    758 		KASSERT(iod->nid_mount == NULL);
    759 		iod->nid_mount = nmp;
    760 		cv_signal(&iod->nid_cv);
    761 		mutex_enter(&nmp->nm_lock);
    762 		mutex_exit(&iod->nid_lock);
    763 		nmp->nm_bufqiods++;
    764 		if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
    765 			cv_broadcast(&nmp->nm_aiocv);
    766 		}
    767 	} else {
    768 		mutex_exit(&nfs_iodlist_lock);
    769 		mutex_enter(&nmp->nm_lock);
    770 	}
    771 
    772 	KASSERT(mutex_owned(&nmp->nm_lock));
    773 
    774 	/*
    775 	 * If we have an iod which can process the request, then queue
    776 	 * the buffer.  However, even if we have an iod, do not initiate
    777 	 * queue cleaning if curproc is the pageout daemon. if the NFS mount
    778 	 * is via local loopback, we may put curproc (pagedaemon) to sleep
    779 	 * waiting for the writes to complete. But the server (ourself)
    780 	 * may block the write, waiting for its (ie., our) pagedaemon
    781 	 * to produce clean pages to handle the write: deadlock.
    782 	 * XXX: start non-loopback mounts straight away?  If "lots free",
    783 	 * let pagedaemon start loopback writes anyway?
    784 	 */
    785 	if (nmp->nm_bufqiods > 0) {
    786 
    787 		/*
    788 		 * Ensure that the queue never grows too large.
    789 		 */
    790 		if (curlwp == uvm.pagedaemon_lwp) {
    791 	  		/* Enque for later, to avoid free-page deadlock */
    792 		} else while (nmp->nm_bufqlen >= 2 * nmp->nm_bufqiods) {
    793 			if (catch) {
    794 				error = cv_timedwait_sig(&nmp->nm_aiocv,
    795 				    &nmp->nm_lock, slptimeo);
    796 			} else {
    797 				error = cv_timedwait(&nmp->nm_aiocv,
    798 				    &nmp->nm_lock, slptimeo);
    799 			}
    800 			if (error) {
    801 				if (nfs_sigintr(nmp, NULL, curlwp)) {
    802 					mutex_exit(&nmp->nm_lock);
    803 					return (EINTR);
    804 				}
    805 				if (catch) {
    806 					catch = false;
    807 					slptimeo = 2 * hz;
    808 				}
    809 			}
    810 
    811 			/*
    812 			 * We might have lost our iod while sleeping,
    813 			 * so check and loop if necessary.
    814 			 */
    815 
    816 			if (nmp->nm_bufqiods == 0) {
    817 				mutex_exit(&nmp->nm_lock);
    818 				goto again;
    819 			}
    820 		}
    821 		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
    822 		nmp->nm_bufqlen++;
    823 		mutex_exit(&nmp->nm_lock);
    824 		return (0);
    825 	}
    826 	mutex_exit(&nmp->nm_lock);
    827 
    828 	/*
    829 	 * All the iods are busy on other mounts, so return EIO to
    830 	 * force the caller to process the i/o synchronously.
    831 	 */
    832 
    833 	return (EIO);
    834 }
    835 
    836 /*
    837  * nfs_doio for read.
    838  */
    839 static int
    840 nfs_doio_read(struct buf *bp, struct uio *uiop)
    841 {
    842 	struct vnode *vp = bp->b_vp;
    843 	struct nfsnode *np = VTONFS(vp);
    844 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    845 	int error = 0;
    846 
    847 	uiop->uio_rw = UIO_READ;
    848 	switch (vp->v_type) {
    849 	case VREG:
    850 		nfsstats.read_bios++;
    851 		error = nfs_readrpc(vp, uiop);
    852 		if (!error && uiop->uio_resid) {
    853 			int diff, len;
    854 
    855 			/*
    856 			 * If uio_resid > 0, there is a hole in the file and
    857 			 * no writes after the hole have been pushed to
    858 			 * the server yet or the file has been truncated
    859 			 * on the server.
    860 			 * Just zero fill the rest of the valid area.
    861 			 */
    862 
    863 			KASSERT(vp->v_size >=
    864 			    uiop->uio_offset + uiop->uio_resid);
    865 			diff = bp->b_bcount - uiop->uio_resid;
    866 			len = uiop->uio_resid;
    867 			memset((char *)bp->b_data + diff, 0, len);
    868 			uiop->uio_resid = 0;
    869 		}
    870 #if 0
    871 		if (uiop->uio_lwp && (vp->v_iflag & VI_TEXT) &&
    872 		    timespeccmp(&np->n_mtime, &np->n_vattr->va_mtime, !=)) {
    873 		    	mutex_enter(proc_lock);
    874 			killproc(uiop->uio_lwp->l_proc, "process text file was modified");
    875 		    	mutex_exit(proc_lock);
    876 #if 0 /* XXX NJWLWP */
    877 			uiop->uio_lwp->l_proc->p_holdcnt++;
    878 #endif
    879 		}
    880 #endif
    881 		break;
    882 	case VLNK:
    883 		KASSERT(uiop->uio_offset == (off_t)0);
    884 		nfsstats.readlink_bios++;
    885 		error = nfs_readlinkrpc(vp, uiop, np->n_rcred);
    886 		break;
    887 	case VDIR:
    888 		nfsstats.readdir_bios++;
    889 		uiop->uio_offset = bp->b_dcookie;
    890 #ifndef NFS_V2_ONLY
    891 		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
    892 			error = nfs_readdirplusrpc(vp, uiop,
    893 			    curlwp->l_cred);
    894 			/*
    895 			 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
    896 			 */
    897 			if (error == ENOTSUP)
    898 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
    899 		}
    900 #else
    901 		nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
    902 #endif
    903 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
    904 			error = nfs_readdirrpc(vp, uiop,
    905 			    curlwp->l_cred);
    906 		if (!error) {
    907 			bp->b_dcookie = uiop->uio_offset;
    908 		}
    909 		break;
    910 	default:
    911 		printf("nfs_doio:  type %x unexpected\n", vp->v_type);
    912 		break;
    913 	}
    914 	bp->b_error = error;
    915 	return error;
    916 }
    917 
    918 /*
    919  * nfs_doio for write.
    920  */
    921 static int
    922 nfs_doio_write(struct buf *bp, struct uio *uiop)
    923 {
    924 	struct vnode *vp = bp->b_vp;
    925 	struct nfsnode *np = VTONFS(vp);
    926 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    927 	int iomode;
    928 	bool stalewriteverf = false;
    929 	int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
    930 	struct vm_page **pgs, *spgs[UBC_MAX_PAGES];
    931 #ifndef NFS_V2_ONLY
    932 	bool needcommit = true; /* need only COMMIT RPC */
    933 #else
    934 	bool needcommit = false; /* need only COMMIT RPC */
    935 #endif
    936 	bool pageprotected;
    937 	struct uvm_object *uobj = &vp->v_uobj;
    938 	int error;
    939 	off_t off, cnt;
    940 
    941 	if (npages < __arraycount(spgs))
    942 		pgs = spgs;
    943 	else {
    944 		if ((pgs = kmem_alloc(sizeof(*pgs) * npages, KM_NOSLEEP)) ==
    945 		    NULL)
    946 			return ENOMEM;
    947 	}
    948 
    949 	if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
    950 		iomode = NFSV3WRITE_UNSTABLE;
    951 	} else {
    952 		iomode = NFSV3WRITE_FILESYNC;
    953 	}
    954 
    955 #ifndef NFS_V2_ONLY
    956 again:
    957 #endif
    958 	rw_enter(&nmp->nm_writeverflock, RW_READER);
    959 
    960 	for (i = 0; i < npages; i++) {
    961 		pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
    962 		if (pgs[i]->uobject == uobj &&
    963 		    pgs[i]->offset == uiop->uio_offset + (i << PAGE_SHIFT)) {
    964 			KASSERT(pgs[i]->flags & PG_BUSY);
    965 			/*
    966 			 * this page belongs to our object.
    967 			 */
    968 			mutex_enter(uobj->vmobjlock);
    969 			/*
    970 			 * write out the page stably if it's about to
    971 			 * be released because we can't resend it
    972 			 * on the server crash.
    973 			 *
    974 			 * XXX assuming PG_RELEASE|PG_PAGEOUT won't be
    975 			 * changed until unbusy the page.
    976 			 */
    977 			if (pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT))
    978 				iomode = NFSV3WRITE_FILESYNC;
    979 			/*
    980 			 * if we met a page which hasn't been sent yet,
    981 			 * we need do WRITE RPC.
    982 			 */
    983 			if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0)
    984 				needcommit = false;
    985 			mutex_exit(uobj->vmobjlock);
    986 		} else {
    987 			iomode = NFSV3WRITE_FILESYNC;
    988 			needcommit = false;
    989 		}
    990 	}
    991 	if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
    992 		mutex_enter(uobj->vmobjlock);
    993 		for (i = 0; i < npages; i++) {
    994 			pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
    995 			pmap_page_protect(pgs[i], VM_PROT_READ);
    996 		}
    997 		mutex_exit(uobj->vmobjlock);
    998 		pageprotected = true; /* pages can't be modified during i/o. */
    999 	} else
   1000 		pageprotected = false;
   1001 
   1002 	/*
   1003 	 * Send the data to the server if necessary,
   1004 	 * otherwise just send a commit rpc.
   1005 	 */
   1006 #ifndef NFS_V2_ONLY
   1007 	if (needcommit) {
   1008 
   1009 		/*
   1010 		 * If the buffer is in the range that we already committed,
   1011 		 * there's nothing to do.
   1012 		 *
   1013 		 * If it's in the range that we need to commit, push the
   1014 		 * whole range at once, otherwise only push the buffer.
   1015 		 * In both these cases, acquire the commit lock to avoid
   1016 		 * other processes modifying the range.
   1017 		 */
   1018 
   1019 		off = uiop->uio_offset;
   1020 		cnt = bp->b_bcount;
   1021 		mutex_enter(&np->n_commitlock);
   1022 		if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
   1023 			bool pushedrange;
   1024 			if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
   1025 				pushedrange = true;
   1026 				off = np->n_pushlo;
   1027 				cnt = np->n_pushhi - np->n_pushlo;
   1028 			} else {
   1029 				pushedrange = false;
   1030 			}
   1031 			error = nfs_commit(vp, off, cnt, curlwp);
   1032 			if (error == 0) {
   1033 				if (pushedrange) {
   1034 					nfs_merge_commit_ranges(vp);
   1035 				} else {
   1036 					nfs_add_committed_range(vp, off, cnt);
   1037 					nfs_del_tobecommitted_range(vp, off,
   1038 					    cnt);
   1039 				}
   1040 			}
   1041 		} else {
   1042 			error = 0;
   1043 		}
   1044 		mutex_exit(&np->n_commitlock);
   1045 		rw_exit(&nmp->nm_writeverflock);
   1046 		if (!error) {
   1047 			/*
   1048 			 * pages are now on stable storage.
   1049 			 */
   1050 			uiop->uio_resid = 0;
   1051 			mutex_enter(uobj->vmobjlock);
   1052 			for (i = 0; i < npages; i++) {
   1053 				pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
   1054 			}
   1055 			mutex_exit(uobj->vmobjlock);
   1056 			goto out;
   1057 		} else if (error == NFSERR_STALEWRITEVERF) {
   1058 			nfs_clearcommit(vp->v_mount);
   1059 			goto again;
   1060 		}
   1061 		if (error) {
   1062 			bp->b_error = np->n_error = error;
   1063 			np->n_flag |= NWRITEERR;
   1064 		}
   1065 		goto out;
   1066 	}
   1067 #endif
   1068 	off = uiop->uio_offset;
   1069 	cnt = bp->b_bcount;
   1070 	uiop->uio_rw = UIO_WRITE;
   1071 	nfsstats.write_bios++;
   1072 	error = nfs_writerpc(vp, uiop, &iomode, pageprotected, &stalewriteverf);
   1073 #ifndef NFS_V2_ONLY
   1074 	if (!error && iomode == NFSV3WRITE_UNSTABLE) {
   1075 		/*
   1076 		 * we need to commit pages later.
   1077 		 */
   1078 		mutex_enter(&np->n_commitlock);
   1079 		nfs_add_tobecommitted_range(vp, off, cnt);
   1080 		nfs_del_committed_range(vp, off, cnt);
   1081 		/*
   1082 		 * if there can be too many uncommitted pages, commit them now.
   1083 		 */
   1084 		if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
   1085 			off = np->n_pushlo;
   1086 			cnt = nfs_commitsize >> 1;
   1087 			error = nfs_commit(vp, off, cnt, curlwp);
   1088 			if (!error) {
   1089 				nfs_add_committed_range(vp, off, cnt);
   1090 				nfs_del_tobecommitted_range(vp, off, cnt);
   1091 			}
   1092 			if (error == NFSERR_STALEWRITEVERF) {
   1093 				stalewriteverf = true;
   1094 				error = 0; /* it isn't a real error */
   1095 			}
   1096 		} else {
   1097 			/*
   1098 			 * re-dirty pages so that they will be passed
   1099 			 * to us later again.
   1100 			 */
   1101 			mutex_enter(uobj->vmobjlock);
   1102 			for (i = 0; i < npages; i++) {
   1103 				uvm_pagemarkdirty(pgs[i],
   1104 				    UVM_PAGE_STATUS_DIRTY);
   1105 			}
   1106 			mutex_exit(uobj->vmobjlock);
   1107 		}
   1108 		mutex_exit(&np->n_commitlock);
   1109 	} else
   1110 #endif
   1111 	if (!error) {
   1112 		/*
   1113 		 * pages are now on stable storage.
   1114 		 */
   1115 		mutex_enter(&np->n_commitlock);
   1116 		nfs_del_tobecommitted_range(vp, off, cnt);
   1117 		mutex_exit(&np->n_commitlock);
   1118 		mutex_enter(uobj->vmobjlock);
   1119 		for (i = 0; i < npages; i++) {
   1120 			pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
   1121 		}
   1122 		mutex_exit(uobj->vmobjlock);
   1123 	} else {
   1124 		/*
   1125 		 * we got an error.
   1126 		 */
   1127 		bp->b_error = np->n_error = error;
   1128 		np->n_flag |= NWRITEERR;
   1129 	}
   1130 
   1131 	rw_exit(&nmp->nm_writeverflock);
   1132 
   1133 
   1134 	if (stalewriteverf) {
   1135 		nfs_clearcommit(vp->v_mount);
   1136 	}
   1137 #ifndef NFS_V2_ONLY
   1138 out:
   1139 #endif
   1140 	if (pgs != spgs)
   1141 		kmem_free(pgs, sizeof(*pgs) * npages);
   1142 	return error;
   1143 }
   1144 
   1145 /*
   1146  * nfs_doio for B_PHYS.
   1147  */
   1148 static int
   1149 nfs_doio_phys(struct buf *bp, struct uio *uiop)
   1150 {
   1151 	struct vnode *vp = bp->b_vp;
   1152 	int error;
   1153 
   1154 	uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
   1155 	if (bp->b_flags & B_READ) {
   1156 		uiop->uio_rw = UIO_READ;
   1157 		nfsstats.read_physios++;
   1158 		error = nfs_readrpc(vp, uiop);
   1159 	} else {
   1160 		int iomode = NFSV3WRITE_DATASYNC;
   1161 		bool stalewriteverf;
   1162 		struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1163 
   1164 		uiop->uio_rw = UIO_WRITE;
   1165 		nfsstats.write_physios++;
   1166 		rw_enter(&nmp->nm_writeverflock, RW_READER);
   1167 		error = nfs_writerpc(vp, uiop, &iomode, false, &stalewriteverf);
   1168 		rw_exit(&nmp->nm_writeverflock);
   1169 		if (stalewriteverf) {
   1170 			nfs_clearcommit(bp->b_vp->v_mount);
   1171 		}
   1172 	}
   1173 	bp->b_error = error;
   1174 	return error;
   1175 }
   1176 
   1177 /*
   1178  * Do an I/O operation to/from a cache block. This may be called
   1179  * synchronously or from an nfsiod.
   1180  */
   1181 int
   1182 nfs_doio(struct buf *bp)
   1183 {
   1184 	int error;
   1185 	struct uio uio;
   1186 	struct uio *uiop = &uio;
   1187 	struct iovec io;
   1188 	UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
   1189 
   1190 	uiop->uio_iov = &io;
   1191 	uiop->uio_iovcnt = 1;
   1192 	uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
   1193 	UIO_SETUP_SYSSPACE(uiop);
   1194 	io.iov_base = bp->b_data;
   1195 	io.iov_len = uiop->uio_resid = bp->b_bcount;
   1196 
   1197 	/*
   1198 	 * Historically, paging was done with physio, but no more...
   1199 	 */
   1200 	if (bp->b_flags & B_PHYS) {
   1201 		/*
   1202 		 * ...though reading /dev/drum still gets us here.
   1203 		 */
   1204 		error = nfs_doio_phys(bp, uiop);
   1205 	} else if (bp->b_flags & B_READ) {
   1206 		error = nfs_doio_read(bp, uiop);
   1207 	} else {
   1208 		error = nfs_doio_write(bp, uiop);
   1209 	}
   1210 	bp->b_resid = uiop->uio_resid;
   1211 	biodone(bp);
   1212 	return (error);
   1213 }
   1214 
   1215 /*
   1216  * Vnode op for VM getpages.
   1217  */
   1218 
   1219 int
   1220 nfs_getpages(void *v)
   1221 {
   1222 	struct vop_getpages_args /* {
   1223 		struct vnode *a_vp;
   1224 		voff_t a_offset;
   1225 		struct vm_page **a_m;
   1226 		int *a_count;
   1227 		int a_centeridx;
   1228 		vm_prot_t a_access_type;
   1229 		int a_advice;
   1230 		int a_flags;
   1231 	} */ *ap = v;
   1232 
   1233 	struct vnode *vp = ap->a_vp;
   1234 	struct uvm_object *uobj = &vp->v_uobj;
   1235 	struct nfsnode *np = VTONFS(vp);
   1236 	const int npages = *ap->a_count;
   1237 	struct vm_page *pg, **pgs, **opgs, *spgs[UBC_MAX_PAGES];
   1238 	off_t origoffset, len;
   1239 	int i, error;
   1240 	bool v3 = NFS_ISV3(vp);
   1241 	bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   1242 	bool locked = (ap->a_flags & PGO_LOCKED) != 0;
   1243 
   1244 	/*
   1245 	 * If we are not locked we are not really using opgs,
   1246 	 * so just initialize it
   1247 	 */
   1248 	if (!locked || npages < __arraycount(spgs))
   1249 		opgs = spgs;
   1250 	else {
   1251 		if ((opgs = kmem_alloc(npages * sizeof(*opgs), KM_NOSLEEP)) ==
   1252 		    NULL)
   1253 			return ENOMEM;
   1254 	}
   1255 
   1256 	/*
   1257 	 * call the genfs code to get the pages.  `pgs' may be NULL
   1258 	 * when doing read-ahead.
   1259 	 */
   1260 	pgs = ap->a_m;
   1261 	if (write && locked && v3) {
   1262 		KASSERT(pgs != NULL);
   1263 #ifdef DEBUG
   1264 
   1265 		/*
   1266 		 * If PGO_LOCKED is set, real pages shouldn't exists
   1267 		 * in the array.
   1268 		 */
   1269 
   1270 		for (i = 0; i < npages; i++)
   1271 			KDASSERT(pgs[i] == NULL || pgs[i] == PGO_DONTCARE);
   1272 #endif
   1273 		memcpy(opgs, pgs, npages * sizeof(struct vm_pages *));
   1274 	}
   1275 	error = genfs_getpages(v);
   1276 	if (error)
   1277 		goto out;
   1278 
   1279 	/*
   1280 	 * for read faults where the nfs node is not yet marked NMODIFIED,
   1281 	 * set PG_RDONLY on the pages so that we come back here if someone
   1282 	 * tries to modify later via the mapping that will be entered for
   1283 	 * this fault.
   1284 	 */
   1285 
   1286 	if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
   1287 		if (!locked) {
   1288 			mutex_enter(uobj->vmobjlock);
   1289 		}
   1290 		for (i = 0; i < npages; i++) {
   1291 			pg = pgs[i];
   1292 			if (pg == NULL || pg == PGO_DONTCARE) {
   1293 				continue;
   1294 			}
   1295 			pg->flags |= PG_RDONLY;
   1296 		}
   1297 		if (!locked) {
   1298 			mutex_exit(uobj->vmobjlock);
   1299 		}
   1300 	}
   1301 	if (!write)
   1302 		goto out;
   1303 
   1304 	/*
   1305 	 * this is a write fault, update the commit info.
   1306 	 */
   1307 
   1308 	origoffset = ap->a_offset;
   1309 	len = npages << PAGE_SHIFT;
   1310 
   1311 	if (v3) {
   1312 		if (!locked) {
   1313 			mutex_enter(&np->n_commitlock);
   1314 		} else {
   1315 			if (!mutex_tryenter(&np->n_commitlock)) {
   1316 
   1317 				/*
   1318 				 * Since PGO_LOCKED is set, we need to unbusy
   1319 				 * all pages fetched by genfs_getpages() above,
   1320 				 * tell the caller that there are no pages
   1321 				 * available and put back original pgs array.
   1322 				 */
   1323 
   1324 				mutex_enter(&uvm_pageqlock);
   1325 				uvm_page_unbusy(pgs, npages);
   1326 				mutex_exit(&uvm_pageqlock);
   1327 				*ap->a_count = 0;
   1328 				memcpy(pgs, opgs,
   1329 				    npages * sizeof(struct vm_pages *));
   1330 				error = EBUSY;
   1331 				goto out;
   1332 			}
   1333 		}
   1334 		nfs_del_committed_range(vp, origoffset, len);
   1335 		nfs_del_tobecommitted_range(vp, origoffset, len);
   1336 	}
   1337 	np->n_flag |= NMODIFIED;
   1338 	if (!locked) {
   1339 		mutex_enter(uobj->vmobjlock);
   1340 	}
   1341 	for (i = 0; i < npages; i++) {
   1342 		pg = pgs[i];
   1343 		if (pg == NULL || pg == PGO_DONTCARE) {
   1344 			continue;
   1345 		}
   1346 		pg->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
   1347 	}
   1348 	if (!locked) {
   1349 		mutex_exit(uobj->vmobjlock);
   1350 	}
   1351 	if (v3) {
   1352 		mutex_exit(&np->n_commitlock);
   1353 	}
   1354 out:
   1355 	if (opgs != spgs)
   1356 		kmem_free(opgs, sizeof(*opgs) * npages);
   1357 	return error;
   1358 }
   1359