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