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