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