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