nfs_bio.c revision 1.105.2.10 1 /* $NetBSD: nfs_bio.c,v 1.105.2.10 2005/03/04 16:54:20 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.10 2005/03/04 16:54:20 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 }
158 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
159 if (error)
160 return (error);
161 np->n_brev = np->n_lrev;
162 }
163 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
164 nfs_invaldircache(vp, 0);
165 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
166 if (error)
167 return (error);
168 }
169 }
170 #endif
171 /*
172 * Don't cache symlinks.
173 */
174 if (np->n_flag & NQNFSNONCACHE
175 || ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
176 switch (vp->v_type) {
177 case VREG:
178 return (nfs_readrpc(vp, uio));
179 case VLNK:
180 return (nfs_readlinkrpc(vp, uio, cred));
181 case VDIR:
182 break;
183 default:
184 printf(" NQNFSNONCACHE: type %x unexpected\n",
185 vp->v_type);
186 };
187 }
188 baddr = (caddr_t)0;
189 switch (vp->v_type) {
190 case VREG:
191 nfsstats.biocache_reads++;
192
193 error = 0;
194 if (uio->uio_offset >= np->n_size) {
195 break;
196 }
197 while (uio->uio_resid > 0) {
198 void *win;
199 int flags;
200 vsize_t bytelen = MIN(np->n_size - uio->uio_offset,
201 uio->uio_resid);
202
203 if (bytelen == 0)
204 break;
205 win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
206 &bytelen, UBC_READ);
207 error = uiomove(win, bytelen, uio);
208 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
209 ubc_release(win, flags);
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 (NFS_EOFVALID(np) &&
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 * also, empty block implies EOF.
294 */
295
296 if (bp->b_bcount == bp->b_resid ||
297 (NFS_EOFVALID(np) &&
298 ndp->dc_blkcookie == np->n_direofoffset)) {
299 KASSERT(bp->b_bcount != bp->b_resid ||
300 ndp->dc_blkcookie == bp->b_dcookie);
301 nfs_putdircache(np, ndp);
302 bp->b_flags |= B_NOCACHE;
303 brelse(bp);
304 return 0;
305 }
306
307 /*
308 * Find the entry we were looking for in the block.
309 */
310
311 en = ndp->dc_entry;
312
313 pdp = dp = (struct dirent *)bp->b_data;
314 edp = bp->b_data + bp->b_bcount - bp->b_resid;
315 enn = 0;
316 while (enn < en && (caddr_t)dp < edp) {
317 pdp = dp;
318 dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
319 enn++;
320 }
321
322 /*
323 * If the entry number was bigger than the number of
324 * entries in the block, or the cookie of the previous
325 * entry doesn't match, the directory cache is
326 * stale. Flush it and try again (i.e. go to
327 * the server).
328 */
329 if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
330 (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
331 #ifdef DEBUG
332 printf("invalid cache: %p %p %p off %lx %lx\n",
333 pdp, dp, edp,
334 (unsigned long)uio->uio_offset,
335 (unsigned long)NFS_GETCOOKIE(pdp));
336 #endif
337 nfs_putdircache(np, ndp);
338 brelse(bp);
339 nfs_invaldircache(vp, 0);
340 nfs_vinvalbuf(vp, 0, cred, l, 0);
341 goto diragain;
342 }
343
344 on = (caddr_t)dp - bp->b_data;
345
346 /*
347 * Cache all entries that may be exported to the
348 * user, as they may be thrown back at us. The
349 * NFSBIO_CACHECOOKIES flag indicates that all
350 * entries are being 'exported', so cache them all.
351 */
352
353 if (en == 0 && pdp == dp) {
354 dp = (struct dirent *)
355 ((caddr_t)dp + dp->d_reclen);
356 enn++;
357 }
358
359 if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
360 n = uio->uio_resid;
361 enough = 1;
362 } else
363 n = bp->b_bcount - bp->b_resid - on;
364
365 ep = bp->b_data + on + n;
366
367 /*
368 * Find last complete entry to copy, caching entries
369 * (if requested) as we go.
370 */
371
372 while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
373 if (cflag & NFSBIO_CACHECOOKIES) {
374 nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
375 ndp->dc_blkcookie, enn, bp->b_lblkno);
376 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
377 NFS_STASHCOOKIE32(pdp,
378 nndp->dc_cookie32);
379 }
380 nfs_putdircache(np, nndp);
381 }
382 pdp = dp;
383 dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
384 enn++;
385 }
386 nfs_putdircache(np, ndp);
387
388 /*
389 * If the last requested entry was not the last in the
390 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
391 * cache the cookie of the last requested one, and
392 * set of the offset to it.
393 */
394
395 if ((on + n) < bp->b_bcount - bp->b_resid) {
396 curoff = NFS_GETCOOKIE(pdp);
397 nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
398 enn, bp->b_lblkno);
399 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
400 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
401 curoff = nndp->dc_cookie32;
402 }
403 nfs_putdircache(np, nndp);
404 } else
405 curoff = bp->b_dcookie;
406
407 /*
408 * Always cache the entry for the next block,
409 * so that readaheads can use it.
410 */
411 nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
412 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
413 if (curoff == bp->b_dcookie) {
414 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
415 curoff = nndp->dc_cookie32;
416 }
417 }
418
419 n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
420
421 /*
422 * If not eof and read aheads are enabled, start one.
423 * (You need the current block first, so that you have the
424 * directory offset cookie of the next block.)
425 */
426 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
427 !NFS_EOFVALID(np) && !(np->n_flag & NQNFSNONCACHE)) {
428 rabp = nfs_getcacheblk(vp, NFSDC_BLKNO(nndp),
429 NFS_DIRBLKSIZ, l);
430 if (rabp) {
431 if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
432 rabp->b_dcookie = nndp->dc_cookie;
433 rabp->b_flags |= (B_READ | B_ASYNC);
434 if (nfs_asyncio(rabp)) {
435 rabp->b_flags |= B_INVAL;
436 brelse(rabp);
437 }
438 } else
439 brelse(rabp);
440 }
441 }
442 nfs_putdircache(np, nndp);
443 got_buf = 1;
444 break;
445 default:
446 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
447 break;
448 }
449
450 if (n > 0) {
451 if (!baddr)
452 baddr = bp->b_data;
453 error = uiomove(baddr + on, (int)n, uio);
454 }
455 switch (vp->v_type) {
456 case VREG:
457 break;
458 case VLNK:
459 n = 0;
460 break;
461 case VDIR:
462 if (np->n_flag & NQNFSNONCACHE)
463 bp->b_flags |= B_INVAL;
464 uio->uio_offset = curoff;
465 if (enough)
466 n = 0;
467 break;
468 default:
469 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
470 }
471 if (got_buf)
472 brelse(bp);
473 } while (error == 0 && uio->uio_resid > 0 && n > 0);
474 return (error);
475 }
476
477 /*
478 * Vnode op for write using bio
479 */
480 int
481 nfs_write(v)
482 void *v;
483 {
484 struct vop_write_args /* {
485 struct vnode *a_vp;
486 struct uio *a_uio;
487 int a_ioflag;
488 struct ucred *a_cred;
489 } */ *ap = v;
490 struct uio *uio = ap->a_uio;
491 struct lwp *l = uio->uio_lwp;
492 struct proc *p = l->l_proc;
493 struct vnode *vp = ap->a_vp;
494 struct nfsnode *np = VTONFS(vp);
495 struct ucred *cred = ap->a_cred;
496 struct vattr vattr;
497 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
498 void *win;
499 voff_t oldoff, origoff;
500 vsize_t bytelen;
501 int flags, error = 0;
502 int ioflag = ap->a_ioflag;
503 int extended = 0, wrotedata = 0;
504
505 #ifdef DIAGNOSTIC
506 if (uio->uio_rw != UIO_WRITE)
507 panic("nfs_write mode");
508 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_lwp != curlwp)
509 panic("nfs_write lwp");
510 #endif
511 if (vp->v_type != VREG)
512 return (EIO);
513 if (np->n_flag & NWRITEERR) {
514 np->n_flag &= ~NWRITEERR;
515 return (np->n_error);
516 }
517 #ifndef NFS_V2_ONLY
518 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
519 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
520 (void)nfs_fsinfo(nmp, vp, cred, l);
521 #endif
522 if (ioflag & (IO_APPEND | IO_SYNC)) {
523 if (np->n_flag & NMODIFIED) {
524 NFS_INVALIDATE_ATTRCACHE(np);
525 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
526 if (error)
527 return (error);
528 }
529 if (ioflag & IO_APPEND) {
530 NFS_INVALIDATE_ATTRCACHE(np);
531 error = VOP_GETATTR(vp, &vattr, cred, l);
532 if (error)
533 return (error);
534 uio->uio_offset = np->n_size;
535 }
536 }
537 if (uio->uio_offset < 0)
538 return (EINVAL);
539 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
540 return (EFBIG);
541 if (uio->uio_resid == 0)
542 return (0);
543 /*
544 * Maybe this should be above the vnode op call, but so long as
545 * file servers have no limits, i don't think it matters
546 */
547 if (p && uio->uio_offset + uio->uio_resid >
548 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
549 psignal(p, SIGXFSZ);
550 return (EFBIG);
551 }
552
553 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
554 int iomode = NFSV3WRITE_FILESYNC;
555 boolean_t stalewriteverf = FALSE;
556
557 lockmgr(&nmp->nm_writeverflock, LK_SHARED, NULL);
558 error = nfs_writerpc(vp, uio, &iomode, FALSE, &stalewriteverf);
559 lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
560 if (stalewriteverf)
561 nfs_clearcommit(vp->v_mount);
562 return (error);
563 }
564
565 origoff = uio->uio_offset;
566 do {
567 boolean_t extending; /* if we are extending whole pages */
568 u_quad_t oldsize;
569 oldoff = uio->uio_offset;
570 bytelen = uio->uio_resid;
571
572 #ifndef NFS_V2_ONLY
573 /*
574 * Check for a valid write lease.
575 */
576 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
577 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
578 do {
579 error = nqnfs_getlease(vp, ND_WRITE, cred, l);
580 } while (error == NQNFS_EXPIRED);
581 if (error)
582 return (error);
583 if (np->n_lrev != np->n_brev ||
584 (np->n_flag & NQNFSNONCACHE)) {
585 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
586 if (error)
587 return (error);
588 np->n_brev = np->n_lrev;
589 }
590 }
591 #endif
592 nfsstats.biocache_writes++;
593
594 oldsize = np->n_size;
595 np->n_flag |= NMODIFIED;
596 if (np->n_size < uio->uio_offset + bytelen) {
597 np->n_size = uio->uio_offset + bytelen;
598 }
599 extending = ((uio->uio_offset & PAGE_MASK) == 0 &&
600 (bytelen & PAGE_MASK) == 0 &&
601 uio->uio_offset >= vp->v_size);
602 win = ubc_alloc(&vp->v_uobj, uio->uio_offset, &bytelen,
603 UBC_WRITE | (extending ? UBC_FAULTBUSY : 0));
604 error = uiomove(win, bytelen, uio);
605 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
606 ubc_release(win, flags);
607 if (error) {
608 if (extending) {
609 /*
610 * backout size and free pages past eof.
611 */
612 np->n_size = oldsize;
613 simple_lock(&vp->v_interlock);
614 (void)VOP_PUTPAGES(vp, round_page(vp->v_size),
615 0, PGO_SYNCIO | PGO_FREE);
616 }
617 break;
618 }
619 wrotedata = 1;
620
621 /*
622 * update UVM's notion of the size now that we've
623 * copied the data into the vnode's pages.
624 */
625
626 if (vp->v_size < uio->uio_offset) {
627 uvm_vnp_setsize(vp, uio->uio_offset);
628 extended = 1;
629 }
630
631 if ((oldoff & ~(nmp->nm_wsize - 1)) !=
632 (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
633 simple_lock(&vp->v_interlock);
634 error = VOP_PUTPAGES(vp,
635 trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
636 round_page((uio->uio_offset + nmp->nm_wsize - 1) &
637 ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
638 }
639 } while (uio->uio_resid > 0);
640 if (wrotedata)
641 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
642 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
643 simple_lock(&vp->v_interlock);
644 error = VOP_PUTPAGES(vp,
645 trunc_page(origoff & ~(nmp->nm_wsize - 1)),
646 round_page((uio->uio_offset + nmp->nm_wsize - 1) &
647 ~(nmp->nm_wsize - 1)),
648 PGO_CLEANIT | PGO_SYNCIO);
649 }
650 return error;
651 }
652
653 /*
654 * Get an nfs cache block.
655 * Allocate a new one if the block isn't currently in the cache
656 * and return the block marked busy. If the calling process is
657 * interrupted by a signal for an interruptible mount point, return
658 * NULL.
659 */
660 struct buf *
661 nfs_getcacheblk(vp, bn, size, l)
662 struct vnode *vp;
663 daddr_t bn;
664 int size;
665 struct lwp *l;
666 {
667 struct buf *bp;
668 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
669
670 if (nmp->nm_flag & NFSMNT_INT) {
671 bp = getblk(vp, bn, size, PCATCH, 0);
672 while (bp == NULL) {
673 if (nfs_sigintr(nmp, NULL, l))
674 return (NULL);
675 bp = getblk(vp, bn, size, 0, 2 * hz);
676 }
677 } else
678 bp = getblk(vp, bn, size, 0, 0);
679 return (bp);
680 }
681
682 /*
683 * Flush and invalidate all dirty buffers. If another process is already
684 * doing the flush, just wait for completion.
685 */
686 int
687 nfs_vinvalbuf(vp, flags, cred, l, intrflg)
688 struct vnode *vp;
689 int flags;
690 struct ucred *cred;
691 struct lwp *l;
692 int intrflg;
693 {
694 struct nfsnode *np = VTONFS(vp);
695 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
696 int error = 0, slpflag, slptimeo;
697
698 if ((nmp->nm_flag & NFSMNT_INT) == 0)
699 intrflg = 0;
700 if (intrflg) {
701 slpflag = PCATCH;
702 slptimeo = 2 * hz;
703 } else {
704 slpflag = 0;
705 slptimeo = 0;
706 }
707 /*
708 * First wait for any other process doing a flush to complete.
709 */
710 simple_lock(&vp->v_interlock);
711 while (np->n_flag & NFLUSHINPROG) {
712 np->n_flag |= NFLUSHWANT;
713 error = ltsleep(&np->n_flag, PRIBIO + 2, "nfsvinval",
714 slptimeo, &vp->v_interlock);
715 if (error && intrflg && nfs_sigintr(nmp, NULL, l)) {
716 simple_unlock(&vp->v_interlock);
717 return EINTR;
718 }
719 }
720
721 /*
722 * Now, flush as required.
723 */
724 np->n_flag |= NFLUSHINPROG;
725 simple_unlock(&vp->v_interlock);
726 error = vinvalbuf(vp, flags, cred, l, slpflag, 0);
727 while (error) {
728 if (intrflg && nfs_sigintr(nmp, NULL, l)) {
729 error = EINTR;
730 break;
731 }
732 error = vinvalbuf(vp, flags, cred, l, 0, slptimeo);
733 }
734 simple_lock(&vp->v_interlock);
735 if (error == 0)
736 np->n_flag &= ~NMODIFIED;
737 np->n_flag &= ~NFLUSHINPROG;
738 if (np->n_flag & NFLUSHWANT) {
739 np->n_flag &= ~NFLUSHWANT;
740 wakeup(&np->n_flag);
741 }
742 simple_unlock(&vp->v_interlock);
743 return error;
744 }
745
746 /*
747 * nfs_flushstalebuf: flush cache if it's stale.
748 *
749 * => caller shouldn't own any pages or buffers which belong to the vnode.
750 */
751
752 int
753 nfs_flushstalebuf(struct vnode *vp, struct ucred *cred, struct lwp *l,
754 int flags)
755 {
756 struct nfsnode *np = VTONFS(vp);
757 struct vattr vattr;
758 int error;
759
760 if (np->n_flag & NMODIFIED) {
761 if ((flags & NFS_FLUSHSTALEBUF_MYWRITE) == 0
762 || vp->v_type != VREG) {
763 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
764 if (error)
765 return error;
766 if (vp->v_type == VDIR) {
767 nfs_invaldircache(vp, 0);
768 }
769 } else {
770 /*
771 * XXX assuming writes are ours.
772 */
773 }
774 NFS_INVALIDATE_ATTRCACHE(np);
775 error = VOP_GETATTR(vp, &vattr, cred, l);
776 if (error)
777 return error;
778 np->n_mtime = vattr.va_mtime;
779 } else {
780 error = VOP_GETATTR(vp, &vattr, cred, l);
781 if (error)
782 return error;
783 if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
784 if (vp->v_type == VDIR) {
785 nfs_invaldircache(vp, 0);
786 }
787 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
788 if (error)
789 return error;
790 np->n_mtime = vattr.va_mtime;
791 }
792 }
793
794 return error;
795 }
796
797 /*
798 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
799 * This is mainly to avoid queueing async I/O requests when the nfsiods
800 * are all hung on a dead server.
801 */
802
803 int
804 nfs_asyncio(bp)
805 struct buf *bp;
806 {
807 int i;
808 struct nfsmount *nmp;
809 int gotiod, slpflag = 0, slptimeo = 0, error;
810
811 if (nfs_numasync == 0)
812 return (EIO);
813
814 nmp = VFSTONFS(bp->b_vp->v_mount);
815 again:
816 if (nmp->nm_flag & NFSMNT_INT)
817 slpflag = PCATCH;
818 gotiod = FALSE;
819
820 /*
821 * Find a free iod to process this request.
822 */
823
824 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
825 struct nfs_iod *iod = &nfs_asyncdaemon[i];
826
827 simple_lock(&iod->nid_slock);
828 if (iod->nid_want) {
829 /*
830 * Found one, so wake it up and tell it which
831 * mount to process.
832 */
833 iod->nid_want = NULL;
834 iod->nid_mount = nmp;
835 wakeup(&iod->nid_want);
836 simple_lock(&nmp->nm_slock);
837 simple_unlock(&iod->nid_slock);
838 nmp->nm_bufqiods++;
839 gotiod = TRUE;
840 break;
841 }
842 simple_unlock(&iod->nid_slock);
843 }
844
845 /*
846 * If none are free, we may already have an iod working on this mount
847 * point. If so, it will process our request.
848 */
849
850 if (!gotiod) {
851 simple_lock(&nmp->nm_slock);
852 if (nmp->nm_bufqiods > 0)
853 gotiod = TRUE;
854 }
855
856 LOCK_ASSERT(simple_lock_held(&nmp->nm_slock));
857
858 /*
859 * If we have an iod which can process the request, then queue
860 * the buffer. However, even if we have an iod, do not initiate
861 * queue cleaning if curproc is the pageout daemon. if the NFS mount
862 * is via local loopback, we may put curproc (pagedaemon) to sleep
863 * waiting for the writes to complete. But the server (ourself)
864 * may block the write, waiting for its (ie., our) pagedaemon
865 * to produce clean pages to handle the write: deadlock.
866 * XXX: start non-loopback mounts straight away? If "lots free",
867 * let pagedaemon start loopback writes anyway?
868 */
869 if (gotiod) {
870
871 /*
872 * Ensure that the queue never grows too large.
873 */
874 if (curproc == uvm.pagedaemon_proc) {
875 /* Enque for later, to avoid free-page deadlock */
876 (void) 0;
877 } else while (nmp->nm_bufqlen >= 2*nfs_numasync) {
878 nmp->nm_bufqwant = TRUE;
879 error = ltsleep(&nmp->nm_bufq,
880 slpflag | PRIBIO | PNORELOCK,
881 "nfsaio", slptimeo, &nmp->nm_slock);
882 if (error) {
883 if (nfs_sigintr(nmp, NULL, curlwp))
884 return (EINTR);
885 if (slpflag == PCATCH) {
886 slpflag = 0;
887 slptimeo = 2 * hz;
888 }
889 }
890
891 /*
892 * We might have lost our iod while sleeping,
893 * so check and loop if nescessary.
894 */
895
896 if (nmp->nm_bufqiods == 0)
897 goto again;
898
899 simple_lock(&nmp->nm_slock);
900 }
901 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
902 nmp->nm_bufqlen++;
903 simple_unlock(&nmp->nm_slock);
904 return (0);
905 }
906 simple_unlock(&nmp->nm_slock);
907
908 /*
909 * All the iods are busy on other mounts, so return EIO to
910 * force the caller to process the i/o synchronously.
911 */
912
913 return (EIO);
914 }
915
916 /*
917 * nfs_doio for read.
918 */
919 static int
920 nfs_doio_read(bp, uiop)
921 struct buf *bp;
922 struct uio *uiop;
923 {
924 struct vnode *vp = bp->b_vp;
925 struct nfsnode *np = VTONFS(vp);
926 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
927 int error = 0;
928
929 uiop->uio_rw = UIO_READ;
930 switch (vp->v_type) {
931 case VREG:
932 nfsstats.read_bios++;
933 error = nfs_readrpc(vp, uiop);
934 if (!error && uiop->uio_resid) {
935 int diff, len;
936
937 /*
938 * If uio_resid > 0, there is a hole in the file and
939 * no writes after the hole have been pushed to
940 * the server yet or the file has been truncated
941 * on the server.
942 * Just zero fill the rest of the valid area.
943 */
944
945 KASSERT(vp->v_size >=
946 uiop->uio_offset + uiop->uio_resid);
947 diff = bp->b_bcount - uiop->uio_resid;
948 len = uiop->uio_resid;
949 memset((char *)bp->b_data + diff, 0, len);
950 }
951 if (uiop->uio_lwp && (vp->v_flag & VTEXT) &&
952 (((nmp->nm_flag & NFSMNT_NQNFS) &&
953 NQNFS_CKINVALID(vp, np, ND_READ) &&
954 np->n_lrev != np->n_brev) ||
955 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
956 timespeccmp(&np->n_mtime, &np->n_vattr->va_mtime, !=)))) {
957 uprintf("Process killed due to "
958 "text file modification\n");
959 psignal(uiop->uio_lwp->l_proc, SIGKILL);
960 #if 0 /* XXX NJWLWP */
961 uiop->uio_lwp->l_proc->p_holdcnt++;
962 #endif
963 }
964 break;
965 case VLNK:
966 KASSERT(uiop->uio_offset == (off_t)0);
967 nfsstats.readlink_bios++;
968 error = nfs_readlinkrpc(vp, uiop, curlwp->l_proc->p_ucred);
969 break;
970 case VDIR:
971 nfsstats.readdir_bios++;
972 uiop->uio_offset = bp->b_dcookie;
973 #ifndef NFS_V2_ONLY
974 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
975 error = nfs_readdirplusrpc(vp, uiop,
976 curlwp->l_proc->p_ucred);
977 if (error == NFSERR_NOTSUPP)
978 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
979 }
980 #else
981 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
982 #endif
983 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
984 error = nfs_readdirrpc(vp, uiop,
985 curlwp->l_proc->p_ucred);
986 if (!error) {
987 bp->b_dcookie = uiop->uio_offset;
988 }
989 break;
990 default:
991 printf("nfs_doio: type %x unexpected\n", vp->v_type);
992 break;
993 }
994 if (error) {
995 bp->b_flags |= B_ERROR;
996 bp->b_error = error;
997 }
998 return error;
999 }
1000
1001 /*
1002 * nfs_doio for write.
1003 */
1004 static int
1005 nfs_doio_write(bp, uiop)
1006 struct buf *bp;
1007 struct uio *uiop;
1008 {
1009 struct vnode *vp = bp->b_vp;
1010 struct nfsnode *np = VTONFS(vp);
1011 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1012 int iomode;
1013 boolean_t stalewriteverf = FALSE;
1014 int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
1015 struct vm_page *pgs[npages];
1016 #ifndef NFS_V2_ONLY
1017 boolean_t needcommit = TRUE; /* need only COMMIT RPC */
1018 #else
1019 boolean_t needcommit = FALSE; /* need only COMMIT RPC */
1020 #endif
1021 boolean_t pageprotected;
1022 struct uvm_object *uobj = &vp->v_uobj;
1023 int error;
1024 off_t off, cnt;
1025
1026 if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
1027 iomode = NFSV3WRITE_UNSTABLE;
1028 } else {
1029 iomode = NFSV3WRITE_FILESYNC;
1030 }
1031
1032 #ifndef NFS_V2_ONLY
1033 again:
1034 #endif
1035 lockmgr(&nmp->nm_writeverflock, LK_SHARED, NULL);
1036
1037 for (i = 0; i < npages; i++) {
1038 pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
1039 if (pgs[i]->uobject == uobj &&
1040 pgs[i]->offset == uiop->uio_offset + (i << PAGE_SHIFT)) {
1041 KASSERT(pgs[i]->flags & PG_BUSY);
1042 /*
1043 * this page belongs to our object.
1044 */
1045 simple_lock(&uobj->vmobjlock);
1046 /*
1047 * write out the page stably if it's about to
1048 * be released because we can't resend it
1049 * on the server crash.
1050 *
1051 * XXX assuming PG_RELEASE|PG_PAGEOUT won't be
1052 * changed until unbusy the page.
1053 */
1054 if (pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT))
1055 iomode = NFSV3WRITE_FILESYNC;
1056 /*
1057 * if we met a page which hasn't been sent yet,
1058 * we need do WRITE RPC.
1059 */
1060 if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0)
1061 needcommit = FALSE;
1062 simple_unlock(&uobj->vmobjlock);
1063 } else {
1064 iomode = NFSV3WRITE_FILESYNC;
1065 needcommit = FALSE;
1066 }
1067 }
1068 if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
1069 simple_lock(&uobj->vmobjlock);
1070 for (i = 0; i < npages; i++) {
1071 pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
1072 pmap_page_protect(pgs[i], VM_PROT_READ);
1073 }
1074 simple_unlock(&uobj->vmobjlock);
1075 pageprotected = TRUE; /* pages can't be modified during i/o. */
1076 } else
1077 pageprotected = FALSE;
1078
1079 /*
1080 * Send the data to the server if necessary,
1081 * otherwise just send a commit rpc.
1082 */
1083 #ifndef NFS_V2_ONLY
1084 if (needcommit) {
1085
1086 /*
1087 * If the buffer is in the range that we already committed,
1088 * there's nothing to do.
1089 *
1090 * If it's in the range that we need to commit, push the
1091 * whole range at once, otherwise only push the buffer.
1092 * In both these cases, acquire the commit lock to avoid
1093 * other processes modifying the range.
1094 */
1095
1096 off = uiop->uio_offset;
1097 cnt = bp->b_bcount;
1098 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1099 if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1100 boolean_t pushedrange;
1101 if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1102 pushedrange = TRUE;
1103 off = np->n_pushlo;
1104 cnt = np->n_pushhi - np->n_pushlo;
1105 } else {
1106 pushedrange = FALSE;
1107 }
1108 error = nfs_commit(vp, off, cnt, curlwp);
1109 if (error == 0) {
1110 if (pushedrange) {
1111 nfs_merge_commit_ranges(vp);
1112 } else {
1113 nfs_add_committed_range(vp, off, cnt);
1114 }
1115 }
1116 } else {
1117 error = 0;
1118 }
1119 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1120 lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1121 if (!error) {
1122 /*
1123 * pages are now on stable storage.
1124 */
1125 uiop->uio_resid = 0;
1126 simple_lock(&uobj->vmobjlock);
1127 for (i = 0; i < npages; i++) {
1128 pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1129 }
1130 simple_unlock(&uobj->vmobjlock);
1131 return 0;
1132 } else if (error == NFSERR_STALEWRITEVERF) {
1133 nfs_clearcommit(vp->v_mount);
1134 goto again;
1135 }
1136 if (error) {
1137 bp->b_flags |= B_ERROR;
1138 bp->b_error = np->n_error = error;
1139 np->n_flag |= NWRITEERR;
1140 }
1141 return error;
1142 }
1143 #endif
1144 off = uiop->uio_offset;
1145 cnt = bp->b_bcount;
1146 uiop->uio_rw = UIO_WRITE;
1147 nfsstats.write_bios++;
1148 error = nfs_writerpc(vp, uiop, &iomode, pageprotected, &stalewriteverf);
1149 #ifndef NFS_V2_ONLY
1150 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1151 /*
1152 * we need to commit pages later.
1153 */
1154 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1155 nfs_add_tobecommitted_range(vp, off, cnt);
1156 /*
1157 * if there can be too many uncommitted pages, commit them now.
1158 */
1159 if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1160 off = np->n_pushlo;
1161 cnt = nfs_commitsize >> 1;
1162 error = nfs_commit(vp, off, cnt, curlwp);
1163 if (!error) {
1164 nfs_add_committed_range(vp, off, cnt);
1165 nfs_del_tobecommitted_range(vp, off, cnt);
1166 }
1167 if (error == NFSERR_STALEWRITEVERF) {
1168 stalewriteverf = TRUE;
1169 error = 0; /* it isn't a real error */
1170 }
1171 } else {
1172 /*
1173 * re-dirty pages so that they will be passed
1174 * to us later again.
1175 */
1176 simple_lock(&uobj->vmobjlock);
1177 for (i = 0; i < npages; i++) {
1178 pgs[i]->flags &= ~PG_CLEAN;
1179 }
1180 simple_unlock(&uobj->vmobjlock);
1181 }
1182 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1183 } else
1184 #endif
1185 if (!error) {
1186 /*
1187 * pages are now on stable storage.
1188 */
1189 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1190 nfs_del_committed_range(vp, off, cnt);
1191 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1192 simple_lock(&uobj->vmobjlock);
1193 for (i = 0; i < npages; i++) {
1194 pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1195 }
1196 simple_unlock(&uobj->vmobjlock);
1197 } else {
1198 /*
1199 * we got an error.
1200 */
1201 bp->b_flags |= B_ERROR;
1202 bp->b_error = np->n_error = error;
1203 np->n_flag |= NWRITEERR;
1204 }
1205
1206 lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1207
1208 if (stalewriteverf) {
1209 nfs_clearcommit(vp->v_mount);
1210 }
1211 return error;
1212 }
1213
1214 /*
1215 * nfs_doio for B_PHYS.
1216 */
1217 static int
1218 nfs_doio_phys(bp, uiop)
1219 struct buf *bp;
1220 struct uio *uiop;
1221 {
1222 struct vnode *vp = bp->b_vp;
1223 int error;
1224
1225 uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
1226 if (bp->b_flags & B_READ) {
1227 uiop->uio_rw = UIO_READ;
1228 nfsstats.read_physios++;
1229 error = nfs_readrpc(vp, uiop);
1230 } else {
1231 int iomode = NFSV3WRITE_DATASYNC;
1232 boolean_t stalewriteverf;
1233 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1234
1235 uiop->uio_rw = UIO_WRITE;
1236 nfsstats.write_physios++;
1237 lockmgr(&nmp->nm_writeverflock, LK_SHARED, NULL);
1238 error = nfs_writerpc(vp, uiop, &iomode, FALSE, &stalewriteverf);
1239 lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL);
1240 if (stalewriteverf) {
1241 nfs_clearcommit(bp->b_vp->v_mount);
1242 }
1243 }
1244 if (error) {
1245 bp->b_flags |= B_ERROR;
1246 bp->b_error = error;
1247 }
1248 return error;
1249 }
1250
1251 /*
1252 * Do an I/O operation to/from a cache block. This may be called
1253 * synchronously or from an nfsiod.
1254 */
1255 int
1256 nfs_doio(bp, l)
1257 struct buf *bp;
1258 struct lwp *l;
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_lwp = 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