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