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