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