xref: /netbsd-src/sys/nfs/nfs_bio.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: nfs_bio.c,v 1.188 2011/09/27 01:07:38 christos 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 2011/09/27 01:07:38 christos 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, advice,
161 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
162 			if (error) {
163 				/*
164 				 * XXXkludge
165 				 * the file has been truncated on the server.
166 				 * there isn't much we can do.
167 				 */
168 				if (uio->uio_offset >= np->n_size) {
169 					/* end of file */
170 					error = 0;
171 				} else {
172 					break;
173 				}
174 			}
175 		}
176 		break;
177 
178 	    case VLNK:
179 		nfsstats.biocache_readlinks++;
180 		bp = nfs_getcacheblk(vp, (daddr_t)0, MAXPATHLEN, l);
181 		if (!bp)
182 			return (EINTR);
183 		if ((bp->b_oflags & BO_DONE) == 0) {
184 			bp->b_flags |= B_READ;
185 			error = nfs_doio(bp);
186 			if (error) {
187 				brelse(bp, 0);
188 				return (error);
189 			}
190 		}
191 		n = MIN(uio->uio_resid, MAXPATHLEN - bp->b_resid);
192 		got_buf = 1;
193 		on = 0;
194 		break;
195 	    case VDIR:
196 diragain:
197 		nfsstats.biocache_readdirs++;
198 		ndp = nfs_searchdircache(vp, uio->uio_offset,
199 			(nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
200 		if (!ndp) {
201 			/*
202 			 * We've been handed a cookie that is not
203 			 * in the cache. If we're not translating
204 			 * 32 <-> 64, it may be a value that was
205 			 * flushed out of the cache because it grew
206 			 * too big. Let the server judge if it's
207 			 * valid or not. In the translation case,
208 			 * we have no way of validating this value,
209 			 * so punt.
210 			 */
211 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
212 				return (EINVAL);
213 			ndp = nfs_enterdircache(vp, uio->uio_offset,
214 				uio->uio_offset, 0, 0);
215 		}
216 
217 		if (NFS_EOFVALID(np) &&
218 		    ndp->dc_cookie == np->n_direofoffset) {
219 			nfs_putdircache(np, ndp);
220 			nfsstats.direofcache_hits++;
221 			return (0);
222 		}
223 
224 		bp = nfs_getcacheblk(vp, NFSDC_BLKNO(ndp), NFS_DIRBLKSIZ, l);
225 		if (!bp)
226 		    return (EINTR);
227 		if ((bp->b_oflags & BO_DONE) == 0) {
228 		    bp->b_flags |= B_READ;
229 		    bp->b_dcookie = ndp->dc_blkcookie;
230 		    error = nfs_doio(bp);
231 		    if (error) {
232 			/*
233 			 * Yuck! The directory has been modified on the
234 			 * server. Punt and let the userland code
235 			 * deal with it.
236 			 */
237 			nfs_putdircache(np, ndp);
238 			brelse(bp, 0);
239 			/*
240 			 * nfs_request maps NFSERR_BAD_COOKIE to EINVAL.
241 			 */
242 			if (error == EINVAL) { /* NFSERR_BAD_COOKIE */
243 			    nfs_invaldircache(vp, 0);
244 			    nfs_vinvalbuf(vp, 0, cred, l, 1);
245 			}
246 			return (error);
247 		    }
248 		}
249 
250 		/*
251 		 * Just return if we hit EOF right away with this
252 		 * block. Always check here, because direofoffset
253 		 * may have been set by an nfsiod since the last
254 		 * check.
255 		 *
256 		 * also, empty block implies EOF.
257 		 */
258 
259 		if (bp->b_bcount == bp->b_resid ||
260 		    (NFS_EOFVALID(np) &&
261 		    ndp->dc_blkcookie == np->n_direofoffset)) {
262 			KASSERT(bp->b_bcount != bp->b_resid ||
263 			    ndp->dc_blkcookie == bp->b_dcookie);
264 			nfs_putdircache(np, ndp);
265 			brelse(bp, BC_NOCACHE);
266 			return 0;
267 		}
268 
269 		/*
270 		 * Find the entry we were looking for in the block.
271 		 */
272 
273 		en = ndp->dc_entry;
274 
275 		pdp = dp = (struct dirent *)bp->b_data;
276 		edp = (struct dirent *)(void *)((char *)bp->b_data + bp->b_bcount -
277 		    bp->b_resid);
278 		enn = 0;
279 		while (enn < en && dp < edp) {
280 			pdp = dp;
281 			dp = _DIRENT_NEXT(dp);
282 			enn++;
283 		}
284 
285 		/*
286 		 * If the entry number was bigger than the number of
287 		 * entries in the block, or the cookie of the previous
288 		 * entry doesn't match, the directory cache is
289 		 * stale. Flush it and try again (i.e. go to
290 		 * the server).
291 		 */
292 		if (dp >= edp || (struct dirent *)_DIRENT_NEXT(dp) > edp ||
293 		    (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
294 #ifdef DEBUG
295 		    	printf("invalid cache: %p %p %p off %jx %jx\n",
296 				pdp, dp, edp,
297 				(uintmax_t)uio->uio_offset,
298 				(uintmax_t)NFS_GETCOOKIE(pdp));
299 #endif
300 			nfs_putdircache(np, ndp);
301 			brelse(bp, 0);
302 			nfs_invaldircache(vp, 0);
303 			nfs_vinvalbuf(vp, 0, cred, l, 0);
304 			goto diragain;
305 		}
306 
307 		on = (char *)dp - (char *)bp->b_data;
308 
309 		/*
310 		 * Cache all entries that may be exported to the
311 		 * user, as they may be thrown back at us. The
312 		 * NFSBIO_CACHECOOKIES flag indicates that all
313 		 * entries are being 'exported', so cache them all.
314 		 */
315 
316 		if (en == 0 && pdp == dp) {
317 			dp = _DIRENT_NEXT(dp);
318 			enn++;
319 		}
320 
321 		if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
322 			n = uio->uio_resid;
323 			enough = 1;
324 		} else
325 			n = bp->b_bcount - bp->b_resid - on;
326 
327 		ep = (struct dirent *)(void *)((char *)bp->b_data + on + n);
328 
329 		/*
330 		 * Find last complete entry to copy, caching entries
331 		 * (if requested) as we go.
332 		 */
333 
334 		while (dp < ep && (struct dirent *)_DIRENT_NEXT(dp) <= ep) {
335 			if (cflag & NFSBIO_CACHECOOKIES) {
336 				nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
337 				    ndp->dc_blkcookie, enn, bp->b_lblkno);
338 				if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
339 					NFS_STASHCOOKIE32(pdp,
340 					    nndp->dc_cookie32);
341 				}
342 				nfs_putdircache(np, nndp);
343 			}
344 			pdp = dp;
345 			dp = _DIRENT_NEXT(dp);
346 			enn++;
347 		}
348 		nfs_putdircache(np, ndp);
349 
350 		/*
351 		 * If the last requested entry was not the last in the
352 		 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
353 		 * cache the cookie of the last requested one, and
354 		 * set of the offset to it.
355 		 */
356 
357 		if ((on + n) < bp->b_bcount - bp->b_resid) {
358 			curoff = NFS_GETCOOKIE(pdp);
359 			nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
360 			    enn, bp->b_lblkno);
361 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
362 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
363 				curoff = nndp->dc_cookie32;
364 			}
365 			nfs_putdircache(np, nndp);
366 		} else
367 			curoff = bp->b_dcookie;
368 
369 		/*
370 		 * Always cache the entry for the next block,
371 		 * so that readaheads can use it.
372 		 */
373 		nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
374 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
375 			if (curoff == bp->b_dcookie) {
376 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
377 				curoff = nndp->dc_cookie32;
378 			}
379 		}
380 
381 		n = (char *)_DIRENT_NEXT(pdp) - ((char *)bp->b_data + on);
382 
383 		/*
384 		 * If not eof and read aheads are enabled, start one.
385 		 * (You need the current block first, so that you have the
386 		 *  directory offset cookie of the next block.)
387 		 */
388 		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
389 		    !NFS_EOFVALID(np)) {
390 			rabp = nfs_getcacheblk(vp, NFSDC_BLKNO(nndp),
391 						NFS_DIRBLKSIZ, l);
392 			if (rabp) {
393 			    if ((rabp->b_oflags & (BO_DONE | BO_DELWRI)) == 0) {
394 				rabp->b_dcookie = nndp->dc_cookie;
395 				rabp->b_flags |= (B_READ | B_ASYNC);
396 				if (nfs_asyncio(rabp)) {
397 				    brelse(rabp, BC_INVAL);
398 				}
399 			    } else
400 				brelse(rabp, 0);
401 			}
402 		}
403 		nfs_putdircache(np, nndp);
404 		got_buf = 1;
405 		break;
406 	    default:
407 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
408 		break;
409 	    }
410 
411 	    if (n > 0) {
412 		if (!baddr)
413 			baddr = bp->b_data;
414 		error = uiomove((char *)baddr + on, (int)n, uio);
415 	    }
416 	    switch (vp->v_type) {
417 	    case VREG:
418 		break;
419 	    case VLNK:
420 		n = 0;
421 		break;
422 	    case VDIR:
423 		uio->uio_offset = curoff;
424 		if (enough)
425 			n = 0;
426 		break;
427 	    default:
428 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
429 	    }
430 	    if (got_buf)
431 		brelse(bp, 0);
432 	} while (error == 0 && uio->uio_resid > 0 && n > 0);
433 	return (error);
434 }
435 
436 /*
437  * Vnode op for write using bio
438  */
439 int
440 nfs_write(void *v)
441 {
442 	struct vop_write_args /* {
443 		struct vnode *a_vp;
444 		struct uio *a_uio;
445 		int  a_ioflag;
446 		kauth_cred_t a_cred;
447 	} */ *ap = v;
448 	struct uio *uio = ap->a_uio;
449 	struct lwp *l = curlwp;
450 	struct vnode *vp = ap->a_vp;
451 	struct nfsnode *np = VTONFS(vp);
452 	kauth_cred_t cred = ap->a_cred;
453 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
454 	voff_t oldoff, origoff;
455 	vsize_t bytelen;
456 	int error = 0;
457 	int ioflag = ap->a_ioflag;
458 	int extended = 0, wrotedata = 0;
459 
460 #ifdef DIAGNOSTIC
461 	if (uio->uio_rw != UIO_WRITE)
462 		panic("nfs_write mode");
463 #endif
464 	if (vp->v_type != VREG)
465 		return (EIO);
466 	if (np->n_flag & NWRITEERR) {
467 		np->n_flag &= ~NWRITEERR;
468 		return (np->n_error);
469 	}
470 #ifndef NFS_V2_ONLY
471 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
472 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
473 		(void)nfs_fsinfo(nmp, vp, cred, l);
474 #endif
475 	if (ioflag & IO_APPEND) {
476 		NFS_INVALIDATE_ATTRCACHE(np);
477 		error = nfs_flushstalebuf(vp, cred, l,
478 		    NFS_FLUSHSTALEBUF_MYWRITE);
479 		if (error)
480 			return (error);
481 		uio->uio_offset = np->n_size;
482 
483 		/*
484 		 * This is already checked above VOP_WRITE, but recheck
485 		 * the append case here to make sure our idea of the
486 		 * file size is as fresh as possible.
487 		 */
488 		if (uio->uio_offset + uio->uio_resid >
489 		      l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
490 			mutex_enter(proc_lock);
491 			psignal(l->l_proc, SIGXFSZ);
492 			mutex_exit(proc_lock);
493 			return (EFBIG);
494 		}
495 	}
496 	if (uio->uio_offset < 0)
497 		return (EINVAL);
498 	if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
499 		return (EFBIG);
500 	if (uio->uio_resid == 0)
501 		return (0);
502 
503 	origoff = uio->uio_offset;
504 	do {
505 		bool overwrite; /* if we are overwriting whole pages */
506 		u_quad_t oldsize;
507 		oldoff = uio->uio_offset;
508 		bytelen = uio->uio_resid;
509 
510 		nfsstats.biocache_writes++;
511 
512 		oldsize = np->n_size;
513 		np->n_flag |= NMODIFIED;
514 		if (np->n_size < uio->uio_offset + bytelen) {
515 			np->n_size = uio->uio_offset + bytelen;
516 		}
517 		overwrite = false;
518 		if ((uio->uio_offset & PAGE_MASK) == 0) {
519 			if ((vp->v_vflag & VV_MAPPED) == 0 &&
520 			    bytelen > PAGE_SIZE) {
521 				bytelen = trunc_page(bytelen);
522 				overwrite = true;
523 			} else if ((bytelen & PAGE_MASK) == 0 &&
524 			    uio->uio_offset >= vp->v_size) {
525 				overwrite = true;
526 			}
527 		}
528 		if (vp->v_size < uio->uio_offset + bytelen) {
529 			uvm_vnp_setwritesize(vp, uio->uio_offset + bytelen);
530 		}
531 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
532 		    UVM_ADV_RANDOM, UBC_WRITE | UBC_PARTIALOK |
533 		    (overwrite ? UBC_FAULTBUSY : 0) |
534 		    UBC_UNMAP_FLAG(vp));
535 		if (error) {
536 			uvm_vnp_setwritesize(vp, vp->v_size);
537 			if (overwrite && np->n_size != oldsize) {
538 				/*
539 				 * backout size and free pages past eof.
540 				 */
541 				np->n_size = oldsize;
542 				mutex_enter(vp->v_interlock);
543 				(void)VOP_PUTPAGES(vp, round_page(vp->v_size),
544 				    0, PGO_SYNCIO | PGO_FREE);
545 			}
546 			break;
547 		}
548 		wrotedata = 1;
549 
550 		/*
551 		 * update UVM's notion of the size now that we've
552 		 * copied the data into the vnode's pages.
553 		 */
554 
555 		if (vp->v_size < uio->uio_offset) {
556 			uvm_vnp_setsize(vp, uio->uio_offset);
557 			extended = 1;
558 		}
559 
560 		if ((oldoff & ~(nmp->nm_wsize - 1)) !=
561 		    (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
562 			mutex_enter(vp->v_interlock);
563 			error = VOP_PUTPAGES(vp,
564 			    trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
565 			    round_page((uio->uio_offset + nmp->nm_wsize - 1) &
566 				       ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
567 		}
568 	} while (uio->uio_resid > 0);
569 	if (wrotedata)
570 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
571 	if (error == 0 && (ioflag & IO_SYNC) != 0) {
572 		mutex_enter(vp->v_interlock);
573 		error = VOP_PUTPAGES(vp,
574 		    trunc_page(origoff & ~(nmp->nm_wsize - 1)),
575 		    round_page((uio->uio_offset + nmp->nm_wsize - 1) &
576 			       ~(nmp->nm_wsize - 1)),
577 		    PGO_CLEANIT | PGO_SYNCIO);
578 	}
579 	return error;
580 }
581 
582 /*
583  * Get an nfs cache block.
584  * Allocate a new one if the block isn't currently in the cache
585  * and return the block marked busy. If the calling process is
586  * interrupted by a signal for an interruptible mount point, return
587  * NULL.
588  */
589 struct buf *
590 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct lwp *l)
591 {
592 	struct buf *bp;
593 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
594 
595 	if (nmp->nm_flag & NFSMNT_INT) {
596 		bp = getblk(vp, bn, size, PCATCH, 0);
597 		while (bp == NULL) {
598 			if (nfs_sigintr(nmp, NULL, l))
599 				return (NULL);
600 			bp = getblk(vp, bn, size, 0, 2 * hz);
601 		}
602 	} else
603 		bp = getblk(vp, bn, size, 0, 0);
604 	return (bp);
605 }
606 
607 /*
608  * Flush and invalidate all dirty buffers. If another process is already
609  * doing the flush, just wait for completion.
610  */
611 int
612 nfs_vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred,
613 		struct lwp *l, int intrflg)
614 {
615 	struct nfsnode *np = VTONFS(vp);
616 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
617 	int error = 0, slptimeo;
618 	bool catch;
619 
620 	if ((nmp->nm_flag & NFSMNT_INT) == 0)
621 		intrflg = 0;
622 	if (intrflg) {
623 		catch = true;
624 		slptimeo = 2 * hz;
625 	} else {
626 		catch = false;
627 		slptimeo = 0;
628 	}
629 	/*
630 	 * First wait for any other process doing a flush to complete.
631 	 */
632 	mutex_enter(vp->v_interlock);
633 	while (np->n_flag & NFLUSHINPROG) {
634 		np->n_flag |= NFLUSHWANT;
635 		error = mtsleep(&np->n_flag, PRIBIO + 2, "nfsvinval",
636 			slptimeo, vp->v_interlock);
637 		if (error && intrflg && nfs_sigintr(nmp, NULL, l)) {
638 			mutex_exit(vp->v_interlock);
639 			return EINTR;
640 		}
641 	}
642 
643 	/*
644 	 * Now, flush as required.
645 	 */
646 	np->n_flag |= NFLUSHINPROG;
647 	mutex_exit(vp->v_interlock);
648 	error = vinvalbuf(vp, flags, cred, l, catch, 0);
649 	while (error) {
650 		if (intrflg && nfs_sigintr(nmp, NULL, l)) {
651 			error = EINTR;
652 			break;
653 		}
654 		error = vinvalbuf(vp, flags, cred, l, 0, slptimeo);
655 	}
656 	mutex_enter(vp->v_interlock);
657 	if (error == 0)
658 		np->n_flag &= ~NMODIFIED;
659 	np->n_flag &= ~NFLUSHINPROG;
660 	if (np->n_flag & NFLUSHWANT) {
661 		np->n_flag &= ~NFLUSHWANT;
662 		wakeup(&np->n_flag);
663 	}
664 	mutex_exit(vp->v_interlock);
665 	return error;
666 }
667 
668 /*
669  * nfs_flushstalebuf: flush cache if it's stale.
670  *
671  * => caller shouldn't own any pages or buffers which belong to the vnode.
672  */
673 
674 int
675 nfs_flushstalebuf(struct vnode *vp, kauth_cred_t cred, struct lwp *l,
676     int flags)
677 {
678 	struct nfsnode *np = VTONFS(vp);
679 	struct vattr vattr;
680 	int error;
681 
682 	if (np->n_flag & NMODIFIED) {
683 		if ((flags & NFS_FLUSHSTALEBUF_MYWRITE) == 0
684 		    || vp->v_type != VREG) {
685 			error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
686 			if (error)
687 				return error;
688 			if (vp->v_type == VDIR) {
689 				nfs_invaldircache(vp, 0);
690 			}
691 		} else {
692 			/*
693 			 * XXX assuming writes are ours.
694 			 */
695 		}
696 		NFS_INVALIDATE_ATTRCACHE(np);
697 		error = VOP_GETATTR(vp, &vattr, cred);
698 		if (error)
699 			return error;
700 		np->n_mtime = vattr.va_mtime;
701 	} else {
702 		error = VOP_GETATTR(vp, &vattr, cred);
703 		if (error)
704 			return error;
705 		if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
706 			if (vp->v_type == VDIR) {
707 				nfs_invaldircache(vp, 0);
708 			}
709 			error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
710 			if (error)
711 				return error;
712 			np->n_mtime = vattr.va_mtime;
713 		}
714 	}
715 
716 	return error;
717 }
718 
719 /*
720  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
721  * This is mainly to avoid queueing async I/O requests when the nfsiods
722  * are all hung on a dead server.
723  */
724 
725 int
726 nfs_asyncio(struct buf *bp)
727 {
728 	struct nfs_iod *iod;
729 	struct nfsmount *nmp;
730 	int slptimeo = 0, error;
731 	bool catch = false;
732 
733 	if (nfs_numasync == 0)
734 		return (EIO);
735 
736 	nmp = VFSTONFS(bp->b_vp->v_mount);
737 again:
738 	if (nmp->nm_flag & NFSMNT_INT)
739 		catch = true;
740 
741 	/*
742 	 * Find a free iod to process this request.
743 	 */
744 
745 	mutex_enter(&nfs_iodlist_lock);
746 	iod = LIST_FIRST(&nfs_iodlist_idle);
747 	if (iod) {
748 		/*
749 		 * Found one, so wake it up and tell it which
750 		 * mount to process.
751 		 */
752 		LIST_REMOVE(iod, nid_idle);
753 		mutex_enter(&iod->nid_lock);
754 		mutex_exit(&nfs_iodlist_lock);
755 		KASSERT(iod->nid_mount == NULL);
756 		iod->nid_mount = nmp;
757 		cv_signal(&iod->nid_cv);
758 		mutex_enter(&nmp->nm_lock);
759 		mutex_exit(&iod->nid_lock);
760 		nmp->nm_bufqiods++;
761 		if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
762 			cv_broadcast(&nmp->nm_aiocv);
763 		}
764 	} else {
765 		mutex_exit(&nfs_iodlist_lock);
766 		mutex_enter(&nmp->nm_lock);
767 	}
768 
769 	KASSERT(mutex_owned(&nmp->nm_lock));
770 
771 	/*
772 	 * If we have an iod which can process the request, then queue
773 	 * the buffer.  However, even if we have an iod, do not initiate
774 	 * queue cleaning if curproc is the pageout daemon. if the NFS mount
775 	 * is via local loopback, we may put curproc (pagedaemon) to sleep
776 	 * waiting for the writes to complete. But the server (ourself)
777 	 * may block the write, waiting for its (ie., our) pagedaemon
778 	 * to produce clean pages to handle the write: deadlock.
779 	 * XXX: start non-loopback mounts straight away?  If "lots free",
780 	 * let pagedaemon start loopback writes anyway?
781 	 */
782 	if (nmp->nm_bufqiods > 0) {
783 
784 		/*
785 		 * Ensure that the queue never grows too large.
786 		 */
787 		if (curlwp == uvm.pagedaemon_lwp) {
788 	  		/* Enque for later, to avoid free-page deadlock */
789 		} else while (nmp->nm_bufqlen >= 2 * nmp->nm_bufqiods) {
790 			if (catch) {
791 				error = cv_timedwait_sig(&nmp->nm_aiocv,
792 				    &nmp->nm_lock, slptimeo);
793 			} else {
794 				error = cv_timedwait(&nmp->nm_aiocv,
795 				    &nmp->nm_lock, slptimeo);
796 			}
797 			if (error) {
798 				if (nfs_sigintr(nmp, NULL, curlwp)) {
799 					mutex_exit(&nmp->nm_lock);
800 					return (EINTR);
801 				}
802 				if (catch) {
803 					catch = false;
804 					slptimeo = 2 * hz;
805 				}
806 			}
807 
808 			/*
809 			 * We might have lost our iod while sleeping,
810 			 * so check and loop if necessary.
811 			 */
812 
813 			if (nmp->nm_bufqiods == 0) {
814 				mutex_exit(&nmp->nm_lock);
815 				goto again;
816 			}
817 		}
818 		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
819 		nmp->nm_bufqlen++;
820 		mutex_exit(&nmp->nm_lock);
821 		return (0);
822 	}
823 	mutex_exit(&nmp->nm_lock);
824 
825 	/*
826 	 * All the iods are busy on other mounts, so return EIO to
827 	 * force the caller to process the i/o synchronously.
828 	 */
829 
830 	return (EIO);
831 }
832 
833 /*
834  * nfs_doio for read.
835  */
836 static int
837 nfs_doio_read(struct buf *bp, struct uio *uiop)
838 {
839 	struct vnode *vp = bp->b_vp;
840 	struct nfsnode *np = VTONFS(vp);
841 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
842 	int error = 0;
843 
844 	uiop->uio_rw = UIO_READ;
845 	switch (vp->v_type) {
846 	case VREG:
847 		nfsstats.read_bios++;
848 		error = nfs_readrpc(vp, uiop);
849 		if (!error && uiop->uio_resid) {
850 			int diff, len;
851 
852 			/*
853 			 * If uio_resid > 0, there is a hole in the file and
854 			 * no writes after the hole have been pushed to
855 			 * the server yet or the file has been truncated
856 			 * on the server.
857 			 * Just zero fill the rest of the valid area.
858 			 */
859 
860 			KASSERT(vp->v_size >=
861 			    uiop->uio_offset + uiop->uio_resid);
862 			diff = bp->b_bcount - uiop->uio_resid;
863 			len = uiop->uio_resid;
864 			memset((char *)bp->b_data + diff, 0, len);
865 			uiop->uio_resid = 0;
866 		}
867 #if 0
868 		if (uiop->uio_lwp && (vp->v_iflag & VI_TEXT) &&
869 		    timespeccmp(&np->n_mtime, &np->n_vattr->va_mtime, !=)) {
870 		    	mutex_enter(proc_lock);
871 			killproc(uiop->uio_lwp->l_proc, "process text file was modified");
872 		    	mutex_exit(proc_lock);
873 #if 0 /* XXX NJWLWP */
874 			uiop->uio_lwp->l_proc->p_holdcnt++;
875 #endif
876 		}
877 #endif
878 		break;
879 	case VLNK:
880 		KASSERT(uiop->uio_offset == (off_t)0);
881 		nfsstats.readlink_bios++;
882 		error = nfs_readlinkrpc(vp, uiop, np->n_rcred);
883 		break;
884 	case VDIR:
885 		nfsstats.readdir_bios++;
886 		uiop->uio_offset = bp->b_dcookie;
887 #ifndef NFS_V2_ONLY
888 		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
889 			error = nfs_readdirplusrpc(vp, uiop,
890 			    curlwp->l_cred);
891 			/*
892 			 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
893 			 */
894 			if (error == ENOTSUP)
895 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
896 		}
897 #else
898 		nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
899 #endif
900 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
901 			error = nfs_readdirrpc(vp, uiop,
902 			    curlwp->l_cred);
903 		if (!error) {
904 			bp->b_dcookie = uiop->uio_offset;
905 		}
906 		break;
907 	default:
908 		printf("nfs_doio:  type %x unexpected\n", vp->v_type);
909 		break;
910 	}
911 	bp->b_error = error;
912 	return error;
913 }
914 
915 /*
916  * nfs_doio for write.
917  */
918 static int
919 nfs_doio_write(struct buf *bp, struct uio *uiop)
920 {
921 	struct vnode *vp = bp->b_vp;
922 	struct nfsnode *np = VTONFS(vp);
923 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
924 	int iomode;
925 	bool stalewriteverf = false;
926 	int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
927 	struct vm_page **pgs, *spgs[UBC_MAX_PAGES];
928 #ifndef NFS_V2_ONLY
929 	bool needcommit = true; /* need only COMMIT RPC */
930 #else
931 	bool needcommit = false; /* need only COMMIT RPC */
932 #endif
933 	bool pageprotected;
934 	struct uvm_object *uobj = &vp->v_uobj;
935 	int error;
936 	off_t off, cnt;
937 
938 	if (npages < __arraycount(spgs))
939 		pgs = spgs;
940 	else {
941 		if ((pgs = kmem_alloc(sizeof(*pgs) * npages, KM_NOSLEEP)) ==
942 		    NULL)
943 			return ENOMEM;
944 	}
945 
946 	if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
947 		iomode = NFSV3WRITE_UNSTABLE;
948 	} else {
949 		iomode = NFSV3WRITE_FILESYNC;
950 	}
951 
952 #ifndef NFS_V2_ONLY
953 again:
954 #endif
955 	rw_enter(&nmp->nm_writeverflock, RW_READER);
956 
957 	for (i = 0; i < npages; i++) {
958 		pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
959 		if (pgs[i]->uobject == uobj &&
960 		    pgs[i]->offset == uiop->uio_offset + (i << PAGE_SHIFT)) {
961 			KASSERT(pgs[i]->flags & PG_BUSY);
962 			/*
963 			 * this page belongs to our object.
964 			 */
965 			mutex_enter(uobj->vmobjlock);
966 			/*
967 			 * write out the page stably if it's about to
968 			 * be released because we can't resend it
969 			 * on the server crash.
970 			 *
971 			 * XXX assuming PG_RELEASE|PG_PAGEOUT won't be
972 			 * changed until unbusy the page.
973 			 */
974 			if (pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT))
975 				iomode = NFSV3WRITE_FILESYNC;
976 			/*
977 			 * if we met a page which hasn't been sent yet,
978 			 * we need do WRITE RPC.
979 			 */
980 			if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0)
981 				needcommit = false;
982 			mutex_exit(uobj->vmobjlock);
983 		} else {
984 			iomode = NFSV3WRITE_FILESYNC;
985 			needcommit = false;
986 		}
987 	}
988 	if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
989 		mutex_enter(uobj->vmobjlock);
990 		for (i = 0; i < npages; i++) {
991 			pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
992 			pmap_page_protect(pgs[i], VM_PROT_READ);
993 		}
994 		mutex_exit(uobj->vmobjlock);
995 		pageprotected = true; /* pages can't be modified during i/o. */
996 	} else
997 		pageprotected = false;
998 
999 	/*
1000 	 * Send the data to the server if necessary,
1001 	 * otherwise just send a commit rpc.
1002 	 */
1003 #ifndef NFS_V2_ONLY
1004 	if (needcommit) {
1005 
1006 		/*
1007 		 * If the buffer is in the range that we already committed,
1008 		 * there's nothing to do.
1009 		 *
1010 		 * If it's in the range that we need to commit, push the
1011 		 * whole range at once, otherwise only push the buffer.
1012 		 * In both these cases, acquire the commit lock to avoid
1013 		 * other processes modifying the range.
1014 		 */
1015 
1016 		off = uiop->uio_offset;
1017 		cnt = bp->b_bcount;
1018 		mutex_enter(&np->n_commitlock);
1019 		if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1020 			bool pushedrange;
1021 			if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1022 				pushedrange = true;
1023 				off = np->n_pushlo;
1024 				cnt = np->n_pushhi - np->n_pushlo;
1025 			} else {
1026 				pushedrange = false;
1027 			}
1028 			error = nfs_commit(vp, off, cnt, curlwp);
1029 			if (error == 0) {
1030 				if (pushedrange) {
1031 					nfs_merge_commit_ranges(vp);
1032 				} else {
1033 					nfs_add_committed_range(vp, off, cnt);
1034 				}
1035 			}
1036 		} else {
1037 			error = 0;
1038 		}
1039 		mutex_exit(&np->n_commitlock);
1040 		rw_exit(&nmp->nm_writeverflock);
1041 		if (!error) {
1042 			/*
1043 			 * pages are now on stable storage.
1044 			 */
1045 			uiop->uio_resid = 0;
1046 			mutex_enter(uobj->vmobjlock);
1047 			for (i = 0; i < npages; i++) {
1048 				pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1049 			}
1050 			mutex_exit(uobj->vmobjlock);
1051 			goto out;
1052 		} else if (error == NFSERR_STALEWRITEVERF) {
1053 			nfs_clearcommit(vp->v_mount);
1054 			goto again;
1055 		}
1056 		if (error) {
1057 			bp->b_error = np->n_error = error;
1058 			np->n_flag |= NWRITEERR;
1059 		}
1060 		goto out;
1061 	}
1062 #endif
1063 	off = uiop->uio_offset;
1064 	cnt = bp->b_bcount;
1065 	uiop->uio_rw = UIO_WRITE;
1066 	nfsstats.write_bios++;
1067 	error = nfs_writerpc(vp, uiop, &iomode, pageprotected, &stalewriteverf);
1068 #ifndef NFS_V2_ONLY
1069 	if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1070 		/*
1071 		 * we need to commit pages later.
1072 		 */
1073 		mutex_enter(&np->n_commitlock);
1074 		nfs_add_tobecommitted_range(vp, off, cnt);
1075 		/*
1076 		 * if there can be too many uncommitted pages, commit them now.
1077 		 */
1078 		if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1079 			off = np->n_pushlo;
1080 			cnt = nfs_commitsize >> 1;
1081 			error = nfs_commit(vp, off, cnt, curlwp);
1082 			if (!error) {
1083 				nfs_add_committed_range(vp, off, cnt);
1084 				nfs_del_tobecommitted_range(vp, off, cnt);
1085 			}
1086 			if (error == NFSERR_STALEWRITEVERF) {
1087 				stalewriteverf = true;
1088 				error = 0; /* it isn't a real error */
1089 			}
1090 		} else {
1091 			/*
1092 			 * re-dirty pages so that they will be passed
1093 			 * to us later again.
1094 			 */
1095 			mutex_enter(uobj->vmobjlock);
1096 			for (i = 0; i < npages; i++) {
1097 				pgs[i]->flags &= ~PG_CLEAN;
1098 			}
1099 			mutex_exit(uobj->vmobjlock);
1100 		}
1101 		mutex_exit(&np->n_commitlock);
1102 	} else
1103 #endif
1104 	if (!error) {
1105 		/*
1106 		 * pages are now on stable storage.
1107 		 */
1108 		mutex_enter(&np->n_commitlock);
1109 		nfs_del_committed_range(vp, off, cnt);
1110 		mutex_exit(&np->n_commitlock);
1111 		mutex_enter(uobj->vmobjlock);
1112 		for (i = 0; i < npages; i++) {
1113 			pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1114 		}
1115 		mutex_exit(uobj->vmobjlock);
1116 	} else {
1117 		/*
1118 		 * we got an error.
1119 		 */
1120 		bp->b_error = np->n_error = error;
1121 		np->n_flag |= NWRITEERR;
1122 	}
1123 
1124 	rw_exit(&nmp->nm_writeverflock);
1125 
1126 
1127 	if (stalewriteverf) {
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) {
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