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