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