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