xref: /netbsd-src/sys/nfs/nfs_bio.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: nfs_bio.c,v 1.119 2004/07/18 07:43:00 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.119 2004/07/18 07:43:00 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 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 uio_resid > 0, there is a hole in the file and
898 			 * no writes after the hole have been pushed to
899 			 * the server yet or the file has been truncated
900 			 * on the server.
901 			 * Just zero fill the rest of the valid area.
902 			 */
903 
904 			KASSERT(vp->v_size >=
905 			    uiop->uio_offset + uiop->uio_resid);
906 			diff = bp->b_bcount - uiop->uio_resid;
907 			len = uiop->uio_resid;
908 			memset((char *)bp->b_data + diff, 0, len);
909 		}
910 		if (uiop->uio_procp && (vp->v_flag & VTEXT) &&
911 		    (((nmp->nm_flag & NFSMNT_NQNFS) &&
912 		      NQNFS_CKINVALID(vp, np, ND_READ) &&
913 		      np->n_lrev != np->n_brev) ||
914 		     (!(nmp->nm_flag & NFSMNT_NQNFS) &&
915 		      timespeccmp(&np->n_mtime, &np->n_vattr->va_mtime, !=)))) {
916 			uprintf("Process killed due to "
917 				"text file modification\n");
918 			psignal(uiop->uio_procp, SIGKILL);
919 #if 0 /* XXX NJWLWP */
920 			uiop->uio_procp->p_holdcnt++;
921 #endif
922 		}
923 		break;
924 	case VLNK:
925 		KASSERT(uiop->uio_offset == (off_t)0);
926 		nfsstats.readlink_bios++;
927 		error = nfs_readlinkrpc(vp, uiop, curproc->p_ucred);
928 		break;
929 	case VDIR:
930 		nfsstats.readdir_bios++;
931 		uiop->uio_offset = bp->b_dcookie;
932 #ifndef NFS_V2_ONLY
933 		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
934 			error = nfs_readdirplusrpc(vp, uiop, np->n_rcred);
935 			if (error == NFSERR_NOTSUPP)
936 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
937 		}
938 #else
939 		nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
940 #endif
941 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
942 			error = nfs_readdirrpc(vp, uiop, np->n_rcred);
943 		if (!error) {
944 			bp->b_dcookie = uiop->uio_offset;
945 		}
946 		break;
947 	default:
948 		printf("nfs_doio:  type %x unexpected\n", vp->v_type);
949 		break;
950 	}
951 	if (error) {
952 		bp->b_flags |= B_ERROR;
953 		bp->b_error = error;
954 	}
955 	return error;
956 }
957 
958 /*
959  * nfs_doio for write.
960  */
961 static int
962 nfs_doio_write(bp, uiop)
963 	struct buf *bp;
964 	struct uio *uiop;
965 {
966 	struct vnode *vp = bp->b_vp;
967 	struct nfsnode *np = VTONFS(vp);
968 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
969 	int iomode;
970 	boolean_t stalewriteverf = FALSE;
971 	int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
972 	struct vm_page *pgs[npages];
973 #ifndef NFS_V2_ONLY
974 	boolean_t needcommit = TRUE; /* need only COMMIT RPC */
975 #else
976 	boolean_t needcommit = FALSE; /* need only COMMIT RPC */
977 #endif
978 	boolean_t pageprotected;
979 	struct uvm_object *uobj = &vp->v_uobj;
980 	int error;
981 	off_t off, cnt;
982 
983 	if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
984 		iomode = NFSV3WRITE_UNSTABLE;
985 	} else {
986 		iomode = NFSV3WRITE_FILESYNC;
987 	}
988 
989 #ifndef NFS_V2_ONLY
990 again:
991 #endif
992 	lockmgr(&nmp->nm_writeverflock, LK_SHARED, NULL);
993 
994 	for (i = 0; i < npages; i++) {
995 		pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
996 		if (pgs[i]->uobject == uobj &&
997 		    pgs[i]->offset == uiop->uio_offset + (i << PAGE_SHIFT)) {
998 			KASSERT(pgs[i]->flags & PG_BUSY);
999 			/*
1000 			 * this page belongs to our object.
1001 			 */
1002 			simple_lock(&uobj->vmobjlock);
1003 			/*
1004 			 * write out the page stably if it's about to
1005 			 * be released because we can't resend it
1006 			 * on the server crash.
1007 			 *
1008 			 * XXX assuming PG_RELEASE|PG_PAGEOUT won't be
1009 			 * changed until unbusy the page.
1010 			 */
1011 			if (pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT))
1012 				iomode = NFSV3WRITE_FILESYNC;
1013 			/*
1014 			 * if we met a page which hasn't been sent yet,
1015 			 * we need do WRITE RPC.
1016 			 */
1017 			if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0)
1018 				needcommit = FALSE;
1019 			simple_unlock(&uobj->vmobjlock);
1020 		} else {
1021 			iomode = NFSV3WRITE_FILESYNC;
1022 			needcommit = FALSE;
1023 		}
1024 	}
1025 	if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
1026 		simple_lock(&uobj->vmobjlock);
1027 		for (i = 0; i < npages; i++) {
1028 			pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
1029 			pmap_page_protect(pgs[i], VM_PROT_READ);
1030 		}
1031 		simple_unlock(&uobj->vmobjlock);
1032 		pageprotected = TRUE; /* pages can't be modified during i/o. */
1033 	} else
1034 		pageprotected = FALSE;
1035 
1036 	/*
1037 	 * Send the data to the server if necessary,
1038 	 * otherwise just send a commit rpc.
1039 	 */
1040 #ifndef NFS_V2_ONLY
1041 	if (needcommit) {
1042 
1043 		/*
1044 		 * If the buffer is in the range that we already committed,
1045 		 * there's nothing to do.
1046 		 *
1047 		 * If it's in the range that we need to commit, push the
1048 		 * whole range at once, otherwise only push the buffer.
1049 		 * In both these cases, acquire the commit lock to avoid
1050 		 * other processes modifying the range.
1051 		 */
1052 
1053 		off = uiop->uio_offset;
1054 		cnt = bp->b_bcount;
1055 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1056 		if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1057 			boolean_t pushedrange;
1058 			if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1059 				pushedrange = TRUE;
1060 				off = np->n_pushlo;
1061 				cnt = np->n_pushhi - np->n_pushlo;
1062 			} else {
1063 				pushedrange = FALSE;
1064 			}
1065 			error = nfs_commit(vp, off, cnt, curproc);
1066 			if (error == 0) {
1067 				if (pushedrange) {
1068 					nfs_merge_commit_ranges(vp);
1069 				} else {
1070 					nfs_add_committed_range(vp, off, cnt);
1071 				}
1072 			}
1073 		} else {
1074 			error = 0;
1075 		}
1076 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1077 		lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1078 		if (!error) {
1079 			/*
1080 			 * pages are now on stable storage.
1081 			 */
1082 			uiop->uio_resid = 0;
1083 			simple_lock(&uobj->vmobjlock);
1084 			for (i = 0; i < npages; i++) {
1085 				pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1086 			}
1087 			simple_unlock(&uobj->vmobjlock);
1088 			return 0;
1089 		} else if (error == NFSERR_STALEWRITEVERF) {
1090 			nfs_clearcommit(vp->v_mount);
1091 			goto again;
1092 		}
1093 		if (error) {
1094 			bp->b_flags |= B_ERROR;
1095 			bp->b_error = np->n_error = error;
1096 			np->n_flag |= NWRITEERR;
1097 		}
1098 		return error;
1099 	}
1100 #endif
1101 	off = uiop->uio_offset;
1102 	cnt = bp->b_bcount;
1103 	uiop->uio_rw = UIO_WRITE;
1104 	nfsstats.write_bios++;
1105 	error = nfs_writerpc(vp, uiop, &iomode, pageprotected, &stalewriteverf);
1106 #ifndef NFS_V2_ONLY
1107 	if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1108 		/*
1109 		 * we need to commit pages later.
1110 		 */
1111 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1112 		nfs_add_tobecommitted_range(vp, off, cnt);
1113 		/*
1114 		 * if there can be too many uncommitted pages, commit them now.
1115 		 */
1116 		if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1117 			off = np->n_pushlo;
1118 			cnt = nfs_commitsize >> 1;
1119 			error = nfs_commit(vp, off, cnt, curproc);
1120 			if (!error) {
1121 				nfs_add_committed_range(vp, off, cnt);
1122 				nfs_del_tobecommitted_range(vp, off, cnt);
1123 			}
1124 			if (error == NFSERR_STALEWRITEVERF) {
1125 				stalewriteverf = TRUE;
1126 				error = 0; /* it isn't a real error */
1127 			}
1128 		} else {
1129 			/*
1130 			 * re-dirty pages so that they will be passed
1131 			 * to us later again.
1132 			 */
1133 			simple_lock(&uobj->vmobjlock);
1134 			for (i = 0; i < npages; i++) {
1135 				pgs[i]->flags &= ~PG_CLEAN;
1136 			}
1137 			simple_unlock(&uobj->vmobjlock);
1138 		}
1139 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1140 	} else
1141 #endif
1142 	if (!error) {
1143 		/*
1144 		 * pages are now on stable storage.
1145 		 */
1146 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1147 		nfs_del_committed_range(vp, off, cnt);
1148 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1149 		simple_lock(&uobj->vmobjlock);
1150 		for (i = 0; i < npages; i++) {
1151 			pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1152 		}
1153 		simple_unlock(&uobj->vmobjlock);
1154 	} else {
1155 		/*
1156 		 * we got an error.
1157 		 */
1158 		bp->b_flags |= B_ERROR;
1159 		bp->b_error = np->n_error = error;
1160 		np->n_flag |= NWRITEERR;
1161 	}
1162 
1163 	lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1164 
1165 	if (stalewriteverf) {
1166 		nfs_clearcommit(vp->v_mount);
1167 	}
1168 	return error;
1169 }
1170 
1171 /*
1172  * nfs_doio for B_PHYS.
1173  */
1174 static int
1175 nfs_doio_phys(bp, uiop)
1176 	struct buf *bp;
1177 	struct uio *uiop;
1178 {
1179 	struct vnode *vp = bp->b_vp;
1180 	int error;
1181 
1182 	uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
1183 	if (bp->b_flags & B_READ) {
1184 		uiop->uio_rw = UIO_READ;
1185 		nfsstats.read_physios++;
1186 		error = nfs_readrpc(vp, uiop);
1187 	} else {
1188 		int iomode = NFSV3WRITE_DATASYNC;
1189 		boolean_t stalewriteverf;
1190 		struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1191 
1192 		uiop->uio_rw = UIO_WRITE;
1193 		nfsstats.write_physios++;
1194 		lockmgr(&nmp->nm_writeverflock, LK_SHARED, NULL);
1195 		error = nfs_writerpc(vp, uiop, &iomode, FALSE, &stalewriteverf);
1196 		lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1197 		if (stalewriteverf) {
1198 			nfs_clearcommit(bp->b_vp->v_mount);
1199 		}
1200 	}
1201 	if (error) {
1202 		bp->b_flags |= B_ERROR;
1203 		bp->b_error = error;
1204 	}
1205 	return error;
1206 }
1207 
1208 /*
1209  * Do an I/O operation to/from a cache block. This may be called
1210  * synchronously or from an nfsiod.
1211  */
1212 int
1213 nfs_doio(bp, p)
1214 	struct buf *bp;
1215 	struct proc *p;
1216 {
1217 	int error;
1218 	struct uio uio;
1219 	struct uio *uiop = &uio;
1220 	struct iovec io;
1221 	UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
1222 
1223 	uiop->uio_iov = &io;
1224 	uiop->uio_iovcnt = 1;
1225 	uiop->uio_segflg = UIO_SYSSPACE;
1226 	uiop->uio_procp = p;
1227 	uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
1228 	io.iov_base = bp->b_data;
1229 	io.iov_len = uiop->uio_resid = bp->b_bcount;
1230 
1231 	/*
1232 	 * Historically, paging was done with physio, but no more...
1233 	 */
1234 	if (bp->b_flags & B_PHYS) {
1235 		/*
1236 		 * ...though reading /dev/drum still gets us here.
1237 		 */
1238 		error = nfs_doio_phys(bp, uiop);
1239 	} else if (bp->b_flags & B_READ) {
1240 		error = nfs_doio_read(bp, uiop);
1241 	} else {
1242 		error = nfs_doio_write(bp, uiop);
1243 	}
1244 	bp->b_resid = uiop->uio_resid;
1245 	biodone(bp);
1246 	return (error);
1247 }
1248 
1249 /*
1250  * Vnode op for VM getpages.
1251  */
1252 
1253 int
1254 nfs_getpages(v)
1255 	void *v;
1256 {
1257 	struct vop_getpages_args /* {
1258 		struct vnode *a_vp;
1259 		voff_t a_offset;
1260 		struct vm_page **a_m;
1261 		int *a_count;
1262 		int a_centeridx;
1263 		vm_prot_t a_access_type;
1264 		int a_advice;
1265 		int a_flags;
1266 	} */ *ap = v;
1267 
1268 	struct vnode *vp = ap->a_vp;
1269 	struct uvm_object *uobj = &vp->v_uobj;
1270 	struct nfsnode *np = VTONFS(vp);
1271 	const int npages = *ap->a_count;
1272 	struct vm_page *pg, **pgs, *opgs[npages];
1273 	off_t origoffset, len;
1274 	int i, error;
1275 	boolean_t v3 = NFS_ISV3(vp);
1276 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1277 	boolean_t locked = (ap->a_flags & PGO_LOCKED) != 0;
1278 
1279 	/*
1280 	 * call the genfs code to get the pages.  `pgs' may be NULL
1281 	 * when doing read-ahead.
1282 	 */
1283 
1284 	pgs = ap->a_m;
1285 	if (write && locked && v3) {
1286 		KASSERT(pgs != NULL);
1287 #ifdef DEBUG
1288 
1289 		/*
1290 		 * If PGO_LOCKED is set, real pages shouldn't exists
1291 		 * in the array.
1292 		 */
1293 
1294 		for (i = 0; i < npages; i++)
1295 			KDASSERT(pgs[i] == NULL || pgs[i] == PGO_DONTCARE);
1296 #endif
1297 		memcpy(opgs, pgs, npages * sizeof(struct vm_pages *));
1298 	}
1299 	error = genfs_getpages(v);
1300 	if (error) {
1301 		return (error);
1302 	}
1303 
1304 	/*
1305 	 * for read faults where the nfs node is not yet marked NMODIFIED,
1306 	 * set PG_RDONLY on the pages so that we come back here if someone
1307 	 * tries to modify later via the mapping that will be entered for
1308 	 * this fault.
1309 	 */
1310 
1311 	if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
1312 		if (!locked) {
1313 			simple_lock(&uobj->vmobjlock);
1314 		}
1315 		for (i = 0; i < npages; i++) {
1316 			pg = pgs[i];
1317 			if (pg == NULL || pg == PGO_DONTCARE) {
1318 				continue;
1319 			}
1320 			pg->flags |= PG_RDONLY;
1321 		}
1322 		if (!locked) {
1323 			simple_unlock(&uobj->vmobjlock);
1324 		}
1325 	}
1326 	if (!write) {
1327 		return (0);
1328 	}
1329 
1330 	/*
1331 	 * this is a write fault, update the commit info.
1332 	 */
1333 
1334 	origoffset = ap->a_offset;
1335 	len = npages << PAGE_SHIFT;
1336 
1337 	if (v3) {
1338 		error = lockmgr(&np->n_commitlock,
1339 		    LK_EXCLUSIVE | (locked ? LK_NOWAIT : 0), NULL);
1340 		if (error) {
1341 			KASSERT(locked != 0);
1342 
1343 			/*
1344 			 * Since PGO_LOCKED is set, we need to unbusy
1345 			 * all pages fetched by genfs_getpages() above,
1346 			 * tell the caller that there are no pages
1347 			 * available and put back original pgs array.
1348 			 */
1349 
1350 			uvm_lock_pageq();
1351 			uvm_page_unbusy(pgs, npages);
1352 			uvm_unlock_pageq();
1353 			*ap->a_count = 0;
1354 			memcpy(pgs, opgs,
1355 			    npages * sizeof(struct vm_pages *));
1356 			return (error);
1357 		}
1358 		nfs_del_committed_range(vp, origoffset, len);
1359 		nfs_del_tobecommitted_range(vp, origoffset, len);
1360 	}
1361 	np->n_flag |= NMODIFIED;
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_NEEDCOMMIT | PG_RDONLY);
1371 	}
1372 	if (!locked) {
1373 		simple_unlock(&uobj->vmobjlock);
1374 	}
1375 	if (v3) {
1376 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1377 	}
1378 	return (0);
1379 }
1380