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