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