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