xref: /netbsd-src/sys/nfs/nfs_bio.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: nfs_bio.c,v 1.98 2003/05/03 17:27:20 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.98 2003/05/03 17:27:20 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, &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 	while (np->n_flag & NFLUSHINPROG) {
749 		np->n_flag |= NFLUSHWANT;
750 		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
751 			slptimeo);
752 		if (error && intrflg && nfs_sigintr(nmp, NULL, p))
753 			return (EINTR);
754 	}
755 
756 	/*
757 	 * Now, flush as required.
758 	 */
759 	np->n_flag |= NFLUSHINPROG;
760 	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
761 	while (error) {
762 		if (intrflg && nfs_sigintr(nmp, NULL, p)) {
763 			np->n_flag &= ~NFLUSHINPROG;
764 			if (np->n_flag & NFLUSHWANT) {
765 				np->n_flag &= ~NFLUSHWANT;
766 				wakeup((caddr_t)&np->n_flag);
767 			}
768 			return (EINTR);
769 		}
770 		error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
771 	}
772 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
773 	if (np->n_flag & NFLUSHWANT) {
774 		np->n_flag &= ~NFLUSHWANT;
775 		wakeup((caddr_t)&np->n_flag);
776 	}
777 	return (0);
778 }
779 
780 /*
781  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
782  * This is mainly to avoid queueing async I/O requests when the nfsiods
783  * are all hung on a dead server.
784  */
785 
786 int
787 nfs_asyncio(bp)
788 	struct buf *bp;
789 {
790 	int i;
791 	struct nfsmount *nmp;
792 	int gotiod, slpflag = 0, slptimeo = 0, error;
793 
794 	if (nfs_numasync == 0)
795 		return (EIO);
796 
797 	nmp = VFSTONFS(bp->b_vp->v_mount);
798 again:
799 	if (nmp->nm_flag & NFSMNT_INT)
800 		slpflag = PCATCH;
801 	gotiod = FALSE;
802 
803 	/*
804 	 * Find a free iod to process this request.
805 	 */
806 
807 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
808 		struct nfs_iod *iod = &nfs_asyncdaemon[i];
809 
810 		if (iod->nid_want) {
811 			/*
812 			 * Found one, so wake it up and tell it which
813 			 * mount to process.
814 			 */
815 			iod->nid_want = NULL;
816 			iod->nid_mount = nmp;
817 			nmp->nm_bufqiods++;
818 			wakeup((caddr_t)&iod->nid_want);
819 			gotiod = TRUE;
820 			break;
821 		}
822 	}
823 
824 	/*
825 	 * If none are free, we may already have an iod working on this mount
826 	 * point.  If so, it will process our request.
827 	 */
828 
829 	if (!gotiod && nmp->nm_bufqiods > 0)
830 		gotiod = TRUE;
831 
832 	/*
833 	 * If we have an iod which can process the request, then queue
834 	 * the buffer.
835 	 */
836 
837 	if (gotiod) {
838 
839 		/*
840 		 * Ensure that the queue never grows too large.
841 		 */
842 
843 		while (nmp->nm_bufqlen >= 2*nfs_numasync) {
844 			nmp->nm_bufqwant = TRUE;
845 			error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
846 				"nfsaio", slptimeo);
847 			if (error) {
848 				if (nfs_sigintr(nmp, NULL, curproc))
849 					return (EINTR);
850 				if (slpflag == PCATCH) {
851 					slpflag = 0;
852 					slptimeo = 2 * hz;
853 				}
854 			}
855 
856 			/*
857 			 * We might have lost our iod while sleeping,
858 			 * so check and loop if nescessary.
859 			 */
860 
861 			if (nmp->nm_bufqiods == 0)
862 				goto again;
863 		}
864 		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
865 		nmp->nm_bufqlen++;
866 		return (0);
867 	}
868 
869 	/*
870 	 * All the iods are busy on other mounts, so return EIO to
871 	 * force the caller to process the i/o synchronously.
872 	 */
873 
874 	return (EIO);
875 }
876 
877 /*
878  * nfs_doio for read.
879  */
880 static int
881 nfs_doio_read(bp, uiop)
882 	struct buf *bp;
883 	struct uio *uiop;
884 {
885 	struct vnode *vp = bp->b_vp;
886 	struct nfsnode *np = VTONFS(vp);
887 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
888 	int error = 0;
889 
890 	uiop->uio_rw = UIO_READ;
891 	switch (vp->v_type) {
892 	case VREG:
893 		nfsstats.read_bios++;
894 		error = nfs_readrpc(vp, uiop);
895 		if (!error && uiop->uio_resid) {
896 			int diff, len;
897 
898 			/*
899 			 * If len > 0, there is a hole in the file and
900 			 * no writes after the hole have been pushed to
901 			 * the server yet.
902 			 * Just zero fill the rest of the valid area.
903 			 */
904 
905 			diff = bp->b_bcount - uiop->uio_resid;
906 			len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
907 				+ diff);
908 			if (len > 0) {
909 				len = MIN(len, uiop->uio_resid);
910 				memset((char *)bp->b_data + diff, 0, len);
911 			}
912 		}
913 		if (uiop->uio_procp && (vp->v_flag & VTEXT) &&
914 			(((nmp->nm_flag & NFSMNT_NQNFS) &&
915 			  NQNFS_CKINVALID(vp, np, ND_READ) &&
916 			  np->n_lrev != np->n_brev) ||
917 			 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
918 			  np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
919 			uprintf("Process killed due to "
920 				"text file modification\n");
921 			psignal(uiop->uio_procp, SIGKILL);
922 #if 0 /* XXX NJWLWP */
923 			uiop->uio_procp->p_holdcnt++;
924 #endif
925 		}
926 		break;
927 	case VLNK:
928 		KASSERT(uiop->uio_offset == (off_t)0);
929 		nfsstats.readlink_bios++;
930 		error = nfs_readlinkrpc(vp, uiop, curproc->p_ucred);
931 		break;
932 	case VDIR:
933 		nfsstats.readdir_bios++;
934 		uiop->uio_offset = bp->b_dcookie;
935 		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
936 			error = nfs_readdirplusrpc(vp, uiop, curproc->p_ucred);
937 			if (error == NFSERR_NOTSUPP)
938 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
939 		}
940 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
941 			error = nfs_readdirrpc(vp, uiop, curproc->p_ucred);
942 		if (!error) {
943 			bp->b_dcookie = uiop->uio_offset;
944 		}
945 		break;
946 	default:
947 		printf("nfs_doio:  type %x unexpected\n", vp->v_type);
948 		break;
949 	}
950 	if (error) {
951 		bp->b_flags |= B_ERROR;
952 		bp->b_error = error;
953 	}
954 	return error;
955 }
956 
957 /*
958  * nfs_doio for write.
959  */
960 static int
961 nfs_doio_write(bp, uiop)
962 	struct buf *bp;
963 	struct uio *uiop;
964 {
965 	struct vnode *vp = bp->b_vp;
966 	struct nfsnode *np = VTONFS(vp);
967 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
968 	int iomode;
969 	boolean_t stalewriteverf = FALSE;
970 	int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
971 	struct vm_page *pgs[npages];
972 	boolean_t needcommit = TRUE;
973 	struct uvm_object *uobj = &vp->v_uobj;
974 	int error;
975 	off_t off, cnt;
976 
977 	if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
978 		iomode = NFSV3WRITE_UNSTABLE;
979 	} else {
980 		iomode = NFSV3WRITE_FILESYNC;
981 	}
982 
983 again:
984 	lockmgr(&nmp->nm_writeverflock, LK_SHARED, NULL);
985 
986 	for (i = 0; i < npages; i++) {
987 		pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
988 		KASSERT((pgs[i]->flags & PG_BUSY) || pgs[i]->uobject != uobj);
989 		if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0) {
990 			needcommit = FALSE;
991 		}
992 		if ((pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT)) ||
993 		    pgs[i]->uobject != uobj ||
994 		    pgs[i]->offset != uiop->uio_offset + (i << PAGE_SHIFT)) {
995 			iomode = NFSV3WRITE_FILESYNC;
996 		}
997 	}
998 	if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
999 		for (i = 0; i < npages; i++) {
1000 			pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
1001 			pmap_page_protect(pgs[i], VM_PROT_READ);
1002 		}
1003 	}
1004 
1005 	/*
1006 	 * Send the data to the server if necessary,
1007 	 * otherwise just send a commit rpc.
1008 	 */
1009 
1010 	if (needcommit) {
1011 
1012 		/*
1013 		 * If the buffer is in the range that we already committed,
1014 		 * there's nothing to do.
1015 		 *
1016 		 * If it's in the range that we need to commit, push the
1017 		 * whole range at once, otherwise only push the buffer.
1018 		 * In both these cases, acquire the commit lock to avoid
1019 		 * other processes modifying the range.
1020 		 */
1021 
1022 		off = uiop->uio_offset;
1023 		cnt = bp->b_bcount;
1024 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1025 		if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1026 			boolean_t pushedrange;
1027 			if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1028 				pushedrange = TRUE;
1029 				off = np->n_pushlo;
1030 				cnt = np->n_pushhi - np->n_pushlo;
1031 			} else {
1032 				pushedrange = FALSE;
1033 			}
1034 			error = nfs_commit(vp, off, cnt, curproc);
1035 			if (error == 0) {
1036 				if (pushedrange) {
1037 					nfs_merge_commit_ranges(vp);
1038 				} else {
1039 					nfs_add_committed_range(vp, off, cnt);
1040 				}
1041 			}
1042 		} else {
1043 			error = 0;
1044 		}
1045 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1046 		lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1047 		if (!error) {
1048 			/*
1049 			 * pages are now on stable storage.
1050 			 */
1051 			uiop->uio_resid = 0;
1052 			simple_lock(&uobj->vmobjlock);
1053 			for (i = 0; i < npages; i++) {
1054 				pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1055 			}
1056 			simple_unlock(&uobj->vmobjlock);
1057 			return 0;
1058 		} else if (error == NFSERR_STALEWRITEVERF) {
1059 			nfs_clearcommit(vp->v_mount);
1060 			goto again;
1061 		}
1062 		if (error) {
1063 			bp->b_flags |= B_ERROR;
1064 			bp->b_error = np->n_error = error;
1065 			np->n_flag |= NWRITEERR;
1066 		}
1067 		return error;
1068 	}
1069 	off = uiop->uio_offset;
1070 	cnt = bp->b_bcount;
1071 	uiop->uio_rw = UIO_WRITE;
1072 	nfsstats.write_bios++;
1073 	error = nfs_writerpc(vp, uiop, &iomode, &stalewriteverf);
1074 	if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1075 		/*
1076 		 * we need to commit pages later.
1077 		 */
1078 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1079 		nfs_add_tobecommitted_range(vp, off, cnt);
1080 		/*
1081 		 * if there can be too many uncommitted pages, commit them now.
1082 		 */
1083 		if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1084 			off = np->n_pushlo;
1085 			cnt = nfs_commitsize >> 1;
1086 			error = nfs_commit(vp, off, cnt, curproc);
1087 			if (!error) {
1088 				nfs_add_committed_range(vp, off, cnt);
1089 				nfs_del_tobecommitted_range(vp, off, cnt);
1090 			}
1091 			if (error == NFSERR_STALEWRITEVERF) {
1092 				stalewriteverf = TRUE;
1093 				error = 0; /* it isn't a real error */
1094 			}
1095 		} else {
1096 			/*
1097 			 * re-dirty pages so that they will be passed
1098 			 * to us later again.
1099 			 */
1100 			simple_lock(&uobj->vmobjlock);
1101 			for (i = 0; i < npages; i++) {
1102 				pgs[i]->flags &= ~PG_CLEAN;
1103 			}
1104 			simple_unlock(&uobj->vmobjlock);
1105 		}
1106 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1107 	} else if (!error) {
1108 		/*
1109 		 * pages are now on stable storage.
1110 		 */
1111 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1112 		nfs_del_committed_range(vp, off, cnt);
1113 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1114 		simple_lock(&uobj->vmobjlock);
1115 		for (i = 0; i < npages; i++) {
1116 			pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1117 		}
1118 		simple_unlock(&uobj->vmobjlock);
1119 	} else {
1120 		/*
1121 		 * we got an error.
1122 		 */
1123 		bp->b_flags |= B_ERROR;
1124 		bp->b_error = np->n_error = error;
1125 		np->n_flag |= NWRITEERR;
1126 	}
1127 
1128 	lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1129 
1130 	if (stalewriteverf) {
1131 		nfs_clearcommit(vp->v_mount);
1132 	}
1133 	return error;
1134 }
1135 
1136 /*
1137  * nfs_doio for B_PHYS.
1138  */
1139 static int
1140 nfs_doio_phys(bp, uiop)
1141 	struct buf *bp;
1142 	struct uio *uiop;
1143 {
1144 	struct vnode *vp = bp->b_vp;
1145 	int error;
1146 
1147 	uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
1148 	if (bp->b_flags & B_READ) {
1149 		uiop->uio_rw = UIO_READ;
1150 		nfsstats.read_physios++;
1151 		error = nfs_readrpc(vp, uiop);
1152 	} else {
1153 		int iomode = NFSV3WRITE_DATASYNC;
1154 		boolean_t stalewriteverf;
1155 		struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1156 
1157 		uiop->uio_rw = UIO_WRITE;
1158 		nfsstats.write_physios++;
1159 		lockmgr(&nmp->nm_writeverflock, LK_SHARED, NULL);
1160 		error = nfs_writerpc(vp, uiop, &iomode, &stalewriteverf);
1161 		lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1162 		if (stalewriteverf) {
1163 			nfs_clearcommit(bp->b_vp->v_mount);
1164 		}
1165 	}
1166 	if (error) {
1167 		bp->b_flags |= B_ERROR;
1168 		bp->b_error = error;
1169 	}
1170 	return error;
1171 }
1172 
1173 /*
1174  * Do an I/O operation to/from a cache block. This may be called
1175  * synchronously or from an nfsiod.
1176  */
1177 int
1178 nfs_doio(bp, p)
1179 	struct buf *bp;
1180 	struct proc *p;
1181 {
1182 	int error;
1183 	struct uio uio;
1184 	struct uio *uiop = &uio;
1185 	struct iovec io;
1186 	UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
1187 
1188 	uiop->uio_iov = &io;
1189 	uiop->uio_iovcnt = 1;
1190 	uiop->uio_segflg = UIO_SYSSPACE;
1191 	uiop->uio_procp = p;
1192 	uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
1193 	io.iov_base = bp->b_data;
1194 	io.iov_len = uiop->uio_resid = bp->b_bcount;
1195 
1196 	/*
1197 	 * Historically, paging was done with physio, but no more...
1198 	 */
1199 	if (bp->b_flags & B_PHYS) {
1200 		/*
1201 		 * ...though reading /dev/drum still gets us here.
1202 		 */
1203 		error = nfs_doio_phys(bp, uiop);
1204 	} else if (bp->b_flags & B_READ) {
1205 		error = nfs_doio_read(bp, uiop);
1206 	} else {
1207 		error = nfs_doio_write(bp, uiop);
1208 	}
1209 	bp->b_resid = uiop->uio_resid;
1210 	biodone(bp);
1211 	return (error);
1212 }
1213 
1214 /*
1215  * Vnode op for VM getpages.
1216  */
1217 
1218 int
1219 nfs_getpages(v)
1220 	void *v;
1221 {
1222 	struct vop_getpages_args /* {
1223 		struct vnode *a_vp;
1224 		voff_t a_offset;
1225 		struct vm_page **a_m;
1226 		int *a_count;
1227 		int a_centeridx;
1228 		vm_prot_t a_access_type;
1229 		int a_advice;
1230 		int a_flags;
1231 	} */ *ap = v;
1232 
1233 	struct vnode *vp = ap->a_vp;
1234 	struct uvm_object *uobj = &vp->v_uobj;
1235 	struct nfsnode *np = VTONFS(vp);
1236 	const int npages = *ap->a_count;
1237 	struct vm_page *pg, **pgs, *opgs[npages];
1238 	off_t origoffset, len;
1239 	int i, error;
1240 	boolean_t v3 = NFS_ISV3(vp);
1241 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1242 	boolean_t locked = (ap->a_flags & PGO_LOCKED) != 0;
1243 
1244 	/*
1245 	 * update the cached read creds for this node.
1246 	 */
1247 
1248 	if (np->n_rcred) {
1249 		crfree(np->n_rcred);
1250 	}
1251 	np->n_rcred = curproc->p_ucred;
1252 	crhold(np->n_rcred);
1253 
1254 	/*
1255 	 * if we have delayed truncation and it's safe, do it now.
1256 	 */
1257 
1258 	if (ap->a_flags & PGO_SYNCIO) {
1259 		nfs_delayedtruncate(vp);
1260 	}
1261 
1262 	/*
1263 	 * call the genfs code to get the pages.  `pgs' may be NULL
1264 	 * when doing read-ahead.
1265 	 */
1266 
1267 	pgs = ap->a_m;
1268 	if (write && locked && v3) {
1269 		KASSERT(pgs != NULL);
1270 #ifdef DEBUG
1271 
1272 		/*
1273 		 * If PGO_LOCKED is set, real pages shouldn't exists
1274 		 * in the array.
1275 		 */
1276 
1277 		for (i = 0; i < npages; i++)
1278 			KDASSERT(pgs[i] == NULL || pgs[i] == PGO_DONTCARE);
1279 #endif
1280 		memcpy(opgs, pgs, npages * sizeof(struct vm_pages *));
1281 	}
1282 	error = genfs_getpages(v);
1283 	if (error) {
1284 		return (error);
1285 	}
1286 
1287 	/*
1288 	 * for read faults where the nfs node is not yet marked NMODIFIED,
1289 	 * set PG_RDONLY on the pages so that we come back here if someone
1290 	 * tries to modify later via the mapping that will be entered for
1291 	 * this fault.
1292 	 */
1293 
1294 	if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
1295 		if (!locked) {
1296 			simple_lock(&uobj->vmobjlock);
1297 		}
1298 		for (i = 0; i < npages; i++) {
1299 			pg = pgs[i];
1300 			if (pg == NULL || pg == PGO_DONTCARE) {
1301 				continue;
1302 			}
1303 			pg->flags |= PG_RDONLY;
1304 		}
1305 		if (!locked) {
1306 			simple_unlock(&uobj->vmobjlock);
1307 		}
1308 	}
1309 	if (!write) {
1310 		return (0);
1311 	}
1312 
1313 	/*
1314 	 * this is a write fault, update the commit info.
1315 	 */
1316 
1317 	origoffset = ap->a_offset;
1318 	len = npages << PAGE_SHIFT;
1319 
1320 	if (v3) {
1321 		error = lockmgr(&np->n_commitlock,
1322 		    LK_EXCLUSIVE | (locked ? LK_NOWAIT : 0), NULL);
1323 		if (error) {
1324 			KASSERT(locked != 0);
1325 
1326 			/*
1327 			 * Since PGO_LOCKED is set, we need to unbusy
1328 			 * all pages fetched by genfs_getpages() above,
1329 			 * tell the caller that there are no pages
1330 			 * available and put back original pgs array.
1331 			 */
1332 
1333 			uvm_lock_pageq();
1334 			uvm_page_unbusy(pgs, npages);
1335 			uvm_unlock_pageq();
1336 			*ap->a_count = 0;
1337 			memcpy(pgs, opgs,
1338 			    npages * sizeof(struct vm_pages *));
1339 			return (error);
1340 		}
1341 		nfs_del_committed_range(vp, origoffset, len);
1342 		nfs_del_tobecommitted_range(vp, origoffset, len);
1343 	}
1344 	np->n_flag |= NMODIFIED;
1345 	if (!locked) {
1346 		simple_lock(&uobj->vmobjlock);
1347 	}
1348 	for (i = 0; i < npages; i++) {
1349 		pg = pgs[i];
1350 		if (pg == NULL || pg == PGO_DONTCARE) {
1351 			continue;
1352 		}
1353 		pg->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1354 	}
1355 	if (!locked) {
1356 		simple_unlock(&uobj->vmobjlock);
1357 	}
1358 	if (v3) {
1359 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1360 	}
1361 	return (0);
1362 }
1363