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