nfs_bio.c revision 58345
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95 37 * $FreeBSD: head/sys/nfsclient/nfs_bio.c 58345 2000-03-20 10:44:49Z phk $ 38 */ 39 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/resourcevar.h> 44#include <sys/signalvar.h> 45#include <sys/proc.h> 46#include <sys/buf.h> 47#include <sys/vnode.h> 48#include <sys/mount.h> 49#include <sys/kernel.h> 50 51#include <vm/vm.h> 52#include <vm/vm_extern.h> 53#include <vm/vm_page.h> 54#include <vm/vm_object.h> 55#include <vm/vm_pager.h> 56#include <vm/vnode_pager.h> 57 58#include <nfs/rpcv2.h> 59#include <nfs/nfsproto.h> 60#include <nfs/nfs.h> 61#include <nfs/nfsmount.h> 62#include <nfs/nqnfs.h> 63#include <nfs/nfsnode.h> 64 65static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size, 66 struct proc *p)); 67 68extern int nfs_numasync; 69extern int nfs_pbuf_freecnt; 70extern struct nfsstats nfsstats; 71 72/* 73 * Vnode op for VM getpages. 74 */ 75int 76nfs_getpages(ap) 77 struct vop_getpages_args /* { 78 struct vnode *a_vp; 79 vm_page_t *a_m; 80 int a_count; 81 int a_reqpage; 82 vm_ooffset_t a_offset; 83 } */ *ap; 84{ 85 int i, error, nextoff, size, toff, count, npages; 86 struct uio uio; 87 struct iovec iov; 88 vm_offset_t kva; 89 struct buf *bp; 90 struct vnode *vp; 91 struct proc *p; 92 struct ucred *cred; 93 struct nfsmount *nmp; 94 vm_page_t *pages; 95 96 vp = ap->a_vp; 97 p = curproc; /* XXX */ 98 cred = curproc->p_ucred; /* XXX */ 99 nmp = VFSTONFS(vp->v_mount); 100 pages = ap->a_m; 101 count = ap->a_count; 102 103 if (vp->v_object == NULL) { 104 printf("nfs_getpages: called with non-merged cache vnode??\n"); 105 return VM_PAGER_ERROR; 106 } 107 108 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 109 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 110 (void)nfs_fsinfo(nmp, vp, cred, p); 111 112 npages = btoc(count); 113 114 /* 115 * If the requested page is partially valid, just return it and 116 * allow the pager to zero-out the blanks. Partially valid pages 117 * can only occur at the file EOF. 118 */ 119 120 { 121 vm_page_t m = pages[ap->a_reqpage]; 122 123 if (m->valid != 0) { 124 /* handled by vm_fault now */ 125 /* vm_page_zero_invalid(m, TRUE); */ 126 for (i = 0; i < npages; ++i) { 127 if (i != ap->a_reqpage) 128 vnode_pager_freepage(pages[i]); 129 } 130 return(0); 131 } 132 } 133 134 /* 135 * We use only the kva address for the buffer, but this is extremely 136 * convienient and fast. 137 */ 138 bp = getpbuf(&nfs_pbuf_freecnt); 139 140 kva = (vm_offset_t) bp->b_data; 141 pmap_qenter(kva, pages, npages); 142 143 iov.iov_base = (caddr_t) kva; 144 iov.iov_len = count; 145 uio.uio_iov = &iov; 146 uio.uio_iovcnt = 1; 147 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex); 148 uio.uio_resid = count; 149 uio.uio_segflg = UIO_SYSSPACE; 150 uio.uio_rw = UIO_READ; 151 uio.uio_procp = p; 152 153 error = nfs_readrpc(vp, &uio, cred); 154 pmap_qremove(kva, npages); 155 156 relpbuf(bp, &nfs_pbuf_freecnt); 157 158 if (error && (uio.uio_resid == count)) { 159 printf("nfs_getpages: error %d\n", error); 160 for (i = 0; i < npages; ++i) { 161 if (i != ap->a_reqpage) 162 vnode_pager_freepage(pages[i]); 163 } 164 return VM_PAGER_ERROR; 165 } 166 167 /* 168 * Calculate the number of bytes read and validate only that number 169 * of bytes. Note that due to pending writes, size may be 0. This 170 * does not mean that the remaining data is invalid! 171 */ 172 173 size = count - uio.uio_resid; 174 175 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) { 176 vm_page_t m; 177 nextoff = toff + PAGE_SIZE; 178 m = pages[i]; 179 180 m->flags &= ~PG_ZERO; 181 182 if (nextoff <= size) { 183 /* 184 * Read operation filled an entire page 185 */ 186 m->valid = VM_PAGE_BITS_ALL; 187 vm_page_undirty(m); 188 } else if (size > toff) { 189 /* 190 * Read operation filled a partial page. 191 */ 192 m->valid = 0; 193 vm_page_set_validclean(m, 0, size - toff); 194 /* handled by vm_fault now */ 195 /* vm_page_zero_invalid(m, TRUE); */ 196 } 197 198 if (i != ap->a_reqpage) { 199 /* 200 * Whether or not to leave the page activated is up in 201 * the air, but we should put the page on a page queue 202 * somewhere (it already is in the object). Result: 203 * It appears that emperical results show that 204 * deactivating pages is best. 205 */ 206 207 /* 208 * Just in case someone was asking for this page we 209 * now tell them that it is ok to use. 210 */ 211 if (!error) { 212 if (m->flags & PG_WANTED) 213 vm_page_activate(m); 214 else 215 vm_page_deactivate(m); 216 vm_page_wakeup(m); 217 } else { 218 vnode_pager_freepage(m); 219 } 220 } 221 } 222 return 0; 223} 224 225/* 226 * Vnode op for VM putpages. 227 */ 228int 229nfs_putpages(ap) 230 struct vop_putpages_args /* { 231 struct vnode *a_vp; 232 vm_page_t *a_m; 233 int a_count; 234 int a_sync; 235 int *a_rtvals; 236 vm_ooffset_t a_offset; 237 } */ *ap; 238{ 239 struct uio uio; 240 struct iovec iov; 241 vm_offset_t kva; 242 struct buf *bp; 243 int iomode, must_commit, i, error, npages, count; 244 off_t offset; 245 int *rtvals; 246 struct vnode *vp; 247 struct proc *p; 248 struct ucred *cred; 249 struct nfsmount *nmp; 250 struct nfsnode *np; 251 vm_page_t *pages; 252 253 vp = ap->a_vp; 254 np = VTONFS(vp); 255 p = curproc; /* XXX */ 256 cred = curproc->p_ucred; /* XXX */ 257 nmp = VFSTONFS(vp->v_mount); 258 pages = ap->a_m; 259 count = ap->a_count; 260 rtvals = ap->a_rtvals; 261 npages = btoc(count); 262 offset = IDX_TO_OFF(pages[0]->pindex); 263 264 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 265 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 266 (void)nfs_fsinfo(nmp, vp, cred, p); 267 268 for (i = 0; i < npages; i++) { 269 rtvals[i] = VM_PAGER_AGAIN; 270 } 271 272 /* 273 * When putting pages, do not extend file past EOF. 274 */ 275 276 if (offset + count > np->n_size) { 277 count = np->n_size - offset; 278 if (count < 0) 279 count = 0; 280 } 281 282 /* 283 * We use only the kva address for the buffer, but this is extremely 284 * convienient and fast. 285 */ 286 bp = getpbuf(&nfs_pbuf_freecnt); 287 288 kva = (vm_offset_t) bp->b_data; 289 pmap_qenter(kva, pages, npages); 290 291 iov.iov_base = (caddr_t) kva; 292 iov.iov_len = count; 293 uio.uio_iov = &iov; 294 uio.uio_iovcnt = 1; 295 uio.uio_offset = offset; 296 uio.uio_resid = count; 297 uio.uio_segflg = UIO_SYSSPACE; 298 uio.uio_rw = UIO_WRITE; 299 uio.uio_procp = p; 300 301 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0) 302 iomode = NFSV3WRITE_UNSTABLE; 303 else 304 iomode = NFSV3WRITE_FILESYNC; 305 306 error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit); 307 308 pmap_qremove(kva, npages); 309 relpbuf(bp, &nfs_pbuf_freecnt); 310 311 if (!error) { 312 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE; 313 for (i = 0; i < nwritten; i++) { 314 rtvals[i] = VM_PAGER_OK; 315 vm_page_undirty(pages[i]); 316 } 317 if (must_commit) 318 nfs_clearcommit(vp->v_mount); 319 } 320 return rtvals[0]; 321} 322 323/* 324 * Vnode op for read using bio 325 */ 326int 327nfs_bioread(vp, uio, ioflag, cred) 328 register struct vnode *vp; 329 register struct uio *uio; 330 int ioflag; 331 struct ucred *cred; 332{ 333 register struct nfsnode *np = VTONFS(vp); 334 register int biosize, i; 335 struct buf *bp = 0, *rabp; 336 struct vattr vattr; 337 struct proc *p; 338 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 339 daddr_t lbn, rabn; 340 int bcount; 341 int seqcount; 342 int nra, error = 0, n = 0, on = 0; 343 344#ifdef DIAGNOSTIC 345 if (uio->uio_rw != UIO_READ) 346 panic("nfs_read mode"); 347#endif 348 if (uio->uio_resid == 0) 349 return (0); 350 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */ 351 return (EINVAL); 352 p = uio->uio_procp; 353 354 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 355 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 356 (void)nfs_fsinfo(nmp, vp, cred, p); 357 if (vp->v_type != VDIR && 358 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 359 return (EFBIG); 360 biosize = vp->v_mount->mnt_stat.f_iosize; 361 seqcount = (int)((off_t)(ioflag >> 16) * biosize / BKVASIZE); 362 /* 363 * For nfs, cache consistency can only be maintained approximately. 364 * Although RFC1094 does not specify the criteria, the following is 365 * believed to be compatible with the reference port. 366 * For nqnfs, full cache consistency is maintained within the loop. 367 * For nfs: 368 * If the file's modify time on the server has changed since the 369 * last read rpc or you have written to the file, 370 * you may have lost data cache consistency with the 371 * server, so flush all of the file's data out of the cache. 372 * Then force a getattr rpc to ensure that you have up to date 373 * attributes. 374 * NB: This implies that cache data can be read when up to 375 * NFS_ATTRTIMEO seconds out of date. If you find that you need current 376 * attributes this could be forced by setting n_attrstamp to 0 before 377 * the VOP_GETATTR() call. 378 */ 379 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) { 380 if (np->n_flag & NMODIFIED) { 381 if (vp->v_type != VREG) { 382 if (vp->v_type != VDIR) 383 panic("nfs: bioread, not dir"); 384 nfs_invaldir(vp); 385 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 386 if (error) 387 return (error); 388 } 389 np->n_attrstamp = 0; 390 error = VOP_GETATTR(vp, &vattr, cred, p); 391 if (error) 392 return (error); 393 np->n_mtime = vattr.va_mtime.tv_sec; 394 } else { 395 error = VOP_GETATTR(vp, &vattr, cred, p); 396 if (error) 397 return (error); 398 if (np->n_mtime != vattr.va_mtime.tv_sec) { 399 if (vp->v_type == VDIR) 400 nfs_invaldir(vp); 401 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 402 if (error) 403 return (error); 404 np->n_mtime = vattr.va_mtime.tv_sec; 405 } 406 } 407 } 408 do { 409 410 /* 411 * Get a valid lease. If cached data is stale, flush it. 412 */ 413 if (nmp->nm_flag & NFSMNT_NQNFS) { 414 if (NQNFS_CKINVALID(vp, np, ND_READ)) { 415 do { 416 error = nqnfs_getlease(vp, ND_READ, cred, p); 417 } while (error == NQNFS_EXPIRED); 418 if (error) 419 return (error); 420 if (np->n_lrev != np->n_brev || 421 (np->n_flag & NQNFSNONCACHE) || 422 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) { 423 if (vp->v_type == VDIR) 424 nfs_invaldir(vp); 425 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 426 if (error) 427 return (error); 428 np->n_brev = np->n_lrev; 429 } 430 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) { 431 nfs_invaldir(vp); 432 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 433 if (error) 434 return (error); 435 } 436 } 437 if (np->n_flag & NQNFSNONCACHE) { 438 switch (vp->v_type) { 439 case VREG: 440 return (nfs_readrpc(vp, uio, cred)); 441 case VLNK: 442 return (nfs_readlinkrpc(vp, uio, cred)); 443 case VDIR: 444 break; 445 default: 446 printf(" NQNFSNONCACHE: type %x unexpected\n", 447 vp->v_type); 448 }; 449 } 450 switch (vp->v_type) { 451 case VREG: 452 nfsstats.biocache_reads++; 453 lbn = uio->uio_offset / biosize; 454 on = uio->uio_offset & (biosize - 1); 455 456 /* 457 * Start the read ahead(s), as required. 458 */ 459 if (nfs_numasync > 0 && nmp->nm_readahead > 0) { 460 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount && 461 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) { 462 rabn = lbn + 1 + nra; 463 if (!incore(vp, rabn)) { 464 rabp = nfs_getcacheblk(vp, rabn, biosize, p); 465 if (!rabp) 466 return (EINTR); 467 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 468 rabp->b_flags |= B_ASYNC; 469 rabp->b_iocmd = BIO_READ; 470 vfs_busy_pages(rabp, 0); 471 if (nfs_asyncio(rabp, cred, p)) { 472 rabp->b_flags |= B_INVAL|B_ERROR; 473 vfs_unbusy_pages(rabp); 474 brelse(rabp); 475 break; 476 } 477 } else { 478 brelse(rabp); 479 } 480 } 481 } 482 } 483 484 /* 485 * Obtain the buffer cache block. Figure out the buffer size 486 * when we are at EOF. If we are modifying the size of the 487 * buffer based on an EOF condition we need to hold 488 * nfs_rslock() through obtaining the buffer to prevent 489 * a potential writer-appender from messing with n_size. 490 * Otherwise we may accidently truncate the buffer and 491 * lose dirty data. 492 * 493 * Note that bcount is *not* DEV_BSIZE aligned. 494 */ 495 496again: 497 bcount = biosize; 498 if ((off_t)lbn * biosize >= np->n_size) { 499 bcount = 0; 500 } else if ((off_t)(lbn + 1) * biosize > np->n_size) { 501 bcount = np->n_size - (off_t)lbn * biosize; 502 } 503 if (bcount != biosize) { 504 switch(nfs_rslock(np, p)) { 505 case ENOLCK: 506 goto again; 507 /* not reached */ 508 case EINTR: 509 case ERESTART: 510 return(EINTR); 511 /* not reached */ 512 default: 513 break; 514 } 515 } 516 517 bp = nfs_getcacheblk(vp, lbn, bcount, p); 518 519 if (bcount != biosize) 520 nfs_rsunlock(np, p); 521 if (!bp) 522 return (EINTR); 523 524 /* 525 * If B_CACHE is not set, we must issue the read. If this 526 * fails, we return an error. 527 */ 528 529 if ((bp->b_flags & B_CACHE) == 0) { 530 bp->b_iocmd = BIO_READ; 531 vfs_busy_pages(bp, 0); 532 error = nfs_doio(bp, cred, p); 533 if (error) { 534 brelse(bp); 535 return (error); 536 } 537 } 538 539 /* 540 * on is the offset into the current bp. Figure out how many 541 * bytes we can copy out of the bp. Note that bcount is 542 * NOT DEV_BSIZE aligned. 543 * 544 * Then figure out how many bytes we can copy into the uio. 545 */ 546 547 n = 0; 548 if (on < bcount) 549 n = min((unsigned)(bcount - on), uio->uio_resid); 550 break; 551 case VLNK: 552 nfsstats.biocache_readlinks++; 553 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p); 554 if (!bp) 555 return (EINTR); 556 if ((bp->b_flags & B_CACHE) == 0) { 557 bp->b_iocmd = BIO_READ; 558 vfs_busy_pages(bp, 0); 559 error = nfs_doio(bp, cred, p); 560 if (error) { 561 bp->b_flags |= B_ERROR; 562 brelse(bp); 563 return (error); 564 } 565 } 566 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid); 567 on = 0; 568 break; 569 case VDIR: 570 nfsstats.biocache_readdirs++; 571 if (np->n_direofoffset 572 && uio->uio_offset >= np->n_direofoffset) { 573 return (0); 574 } 575 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ; 576 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 577 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p); 578 if (!bp) 579 return (EINTR); 580 if ((bp->b_flags & B_CACHE) == 0) { 581 bp->b_iocmd = BIO_READ; 582 vfs_busy_pages(bp, 0); 583 error = nfs_doio(bp, cred, p); 584 if (error) { 585 brelse(bp); 586 } 587 while (error == NFSERR_BAD_COOKIE) { 588 printf("got bad cookie vp %p bp %p\n", vp, bp); 589 nfs_invaldir(vp); 590 error = nfs_vinvalbuf(vp, 0, cred, p, 1); 591 /* 592 * Yuck! The directory has been modified on the 593 * server. The only way to get the block is by 594 * reading from the beginning to get all the 595 * offset cookies. 596 * 597 * Leave the last bp intact unless there is an error. 598 * Loop back up to the while if the error is another 599 * NFSERR_BAD_COOKIE (double yuch!). 600 */ 601 for (i = 0; i <= lbn && !error; i++) { 602 if (np->n_direofoffset 603 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) 604 return (0); 605 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p); 606 if (!bp) 607 return (EINTR); 608 if ((bp->b_flags & B_CACHE) == 0) { 609 bp->b_iocmd = BIO_READ; 610 vfs_busy_pages(bp, 0); 611 error = nfs_doio(bp, cred, p); 612 /* 613 * no error + B_INVAL == directory EOF, 614 * use the block. 615 */ 616 if (error == 0 && (bp->b_flags & B_INVAL)) 617 break; 618 } 619 /* 620 * An error will throw away the block and the 621 * for loop will break out. If no error and this 622 * is not the block we want, we throw away the 623 * block and go for the next one via the for loop. 624 */ 625 if (error || i < lbn) 626 brelse(bp); 627 } 628 } 629 /* 630 * The above while is repeated if we hit another cookie 631 * error. If we hit an error and it wasn't a cookie error, 632 * we give up. 633 */ 634 if (error) 635 return (error); 636 } 637 638 /* 639 * If not eof and read aheads are enabled, start one. 640 * (You need the current block first, so that you have the 641 * directory offset cookie of the next block.) 642 */ 643 if (nfs_numasync > 0 && nmp->nm_readahead > 0 && 644 (bp->b_flags & B_INVAL) == 0 && 645 (np->n_direofoffset == 0 || 646 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) && 647 !(np->n_flag & NQNFSNONCACHE) && 648 !incore(vp, lbn + 1)) { 649 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p); 650 if (rabp) { 651 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 652 rabp->b_flags |= B_ASYNC; 653 rabp->b_iocmd = BIO_READ; 654 vfs_busy_pages(rabp, 0); 655 if (nfs_asyncio(rabp, cred, p)) { 656 rabp->b_flags |= B_INVAL|B_ERROR; 657 vfs_unbusy_pages(rabp); 658 brelse(rabp); 659 } 660 } else { 661 brelse(rabp); 662 } 663 } 664 } 665 /* 666 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is 667 * chopped for the EOF condition, we cannot tell how large 668 * NFS directories are going to be until we hit EOF. So 669 * an NFS directory buffer is *not* chopped to its EOF. Now, 670 * it just so happens that b_resid will effectively chop it 671 * to EOF. *BUT* this information is lost if the buffer goes 672 * away and is reconstituted into a B_CACHE state ( due to 673 * being VMIO ) later. So we keep track of the directory eof 674 * in np->n_direofoffset and chop it off as an extra step 675 * right here. 676 */ 677 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on); 678 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset) 679 n = np->n_direofoffset - uio->uio_offset; 680 break; 681 default: 682 printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 683 break; 684 }; 685 686 if (n > 0) { 687 error = uiomove(bp->b_data + on, (int)n, uio); 688 } 689 switch (vp->v_type) { 690 case VREG: 691 break; 692 case VLNK: 693 n = 0; 694 break; 695 case VDIR: 696 /* 697 * Invalidate buffer if caching is disabled, forcing a 698 * re-read from the remote later. 699 */ 700 if (np->n_flag & NQNFSNONCACHE) 701 bp->b_flags |= B_INVAL; 702 break; 703 default: 704 printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 705 } 706 brelse(bp); 707 } while (error == 0 && uio->uio_resid > 0 && n > 0); 708 return (error); 709} 710 711/* 712 * Vnode op for write using bio 713 */ 714int 715nfs_write(ap) 716 struct vop_write_args /* { 717 struct vnode *a_vp; 718 struct uio *a_uio; 719 int a_ioflag; 720 struct ucred *a_cred; 721 } */ *ap; 722{ 723 int biosize; 724 struct uio *uio = ap->a_uio; 725 struct proc *p = uio->uio_procp; 726 struct vnode *vp = ap->a_vp; 727 struct nfsnode *np = VTONFS(vp); 728 struct ucred *cred = ap->a_cred; 729 int ioflag = ap->a_ioflag; 730 struct buf *bp; 731 struct vattr vattr; 732 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 733 daddr_t lbn; 734 int bcount; 735 int n, on, error = 0, iomode, must_commit; 736 int haverslock = 0; 737 738#ifdef DIAGNOSTIC 739 if (uio->uio_rw != UIO_WRITE) 740 panic("nfs_write mode"); 741 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc) 742 panic("nfs_write proc"); 743#endif 744 if (vp->v_type != VREG) 745 return (EIO); 746 if (np->n_flag & NWRITEERR) { 747 np->n_flag &= ~NWRITEERR; 748 return (np->n_error); 749 } 750 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 751 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 752 (void)nfs_fsinfo(nmp, vp, cred, p); 753 754 /* 755 * Synchronously flush pending buffers if we are in synchronous 756 * mode or if we are appending. 757 */ 758 if (ioflag & (IO_APPEND | IO_SYNC)) { 759 if (np->n_flag & NMODIFIED) { 760 np->n_attrstamp = 0; 761 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 762 if (error) 763 return (error); 764 } 765 } 766 767 /* 768 * If IO_APPEND then load uio_offset. We restart here if we cannot 769 * get the append lock. 770 */ 771restart: 772 if (ioflag & IO_APPEND) { 773 np->n_attrstamp = 0; 774 error = VOP_GETATTR(vp, &vattr, cred, p); 775 if (error) 776 return (error); 777 uio->uio_offset = np->n_size; 778 } 779 780 if (uio->uio_offset < 0) 781 return (EINVAL); 782 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 783 return (EFBIG); 784 if (uio->uio_resid == 0) 785 return (0); 786 787 /* 788 * We need to obtain the rslock if we intend to modify np->n_size 789 * in order to guarentee the append point with multiple contending 790 * writers, to guarentee that no other appenders modify n_size 791 * while we are trying to obtain a truncated buffer (i.e. to avoid 792 * accidently truncating data written by another appender due to 793 * the race), and to ensure that the buffer is populated prior to 794 * our extending of the file. We hold rslock through the entire 795 * operation. 796 * 797 * Note that we do not synchronize the case where someone truncates 798 * the file while we are appending to it because attempting to lock 799 * this case may deadlock other parts of the system unexpectedly. 800 */ 801 if ((ioflag & IO_APPEND) || 802 uio->uio_offset + uio->uio_resid > np->n_size) { 803 switch(nfs_rslock(np, p)) { 804 case ENOLCK: 805 goto restart; 806 /* not reached */ 807 case EINTR: 808 case ERESTART: 809 return(EINTR); 810 /* not reached */ 811 default: 812 break; 813 } 814 haverslock = 1; 815 } 816 817 /* 818 * Maybe this should be above the vnode op call, but so long as 819 * file servers have no limits, i don't think it matters 820 */ 821 if (p && uio->uio_offset + uio->uio_resid > 822 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 823 psignal(p, SIGXFSZ); 824 if (haverslock) 825 nfs_rsunlock(np, p); 826 return (EFBIG); 827 } 828 829 biosize = vp->v_mount->mnt_stat.f_iosize; 830 831 do { 832 /* 833 * Check for a valid write lease. 834 */ 835 if ((nmp->nm_flag & NFSMNT_NQNFS) && 836 NQNFS_CKINVALID(vp, np, ND_WRITE)) { 837 do { 838 error = nqnfs_getlease(vp, ND_WRITE, cred, p); 839 } while (error == NQNFS_EXPIRED); 840 if (error) 841 break; 842 if (np->n_lrev != np->n_brev || 843 (np->n_flag & NQNFSNONCACHE)) { 844 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 845 if (error) 846 break; 847 np->n_brev = np->n_lrev; 848 } 849 } 850 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) { 851 iomode = NFSV3WRITE_FILESYNC; 852 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit); 853 if (must_commit) 854 nfs_clearcommit(vp->v_mount); 855 break; 856 } 857 nfsstats.biocache_writes++; 858 lbn = uio->uio_offset / biosize; 859 on = uio->uio_offset & (biosize-1); 860 n = min((unsigned)(biosize - on), uio->uio_resid); 861again: 862 /* 863 * Handle direct append and file extension cases, calculate 864 * unaligned buffer size. 865 */ 866 867 if (uio->uio_offset == np->n_size && n) { 868 /* 869 * Get the buffer (in its pre-append state to maintain 870 * B_CACHE if it was previously set). Resize the 871 * nfsnode after we have locked the buffer to prevent 872 * readers from reading garbage. 873 */ 874 bcount = on; 875 bp = nfs_getcacheblk(vp, lbn, bcount, p); 876 877 if (bp != NULL) { 878 long save; 879 880 np->n_size = uio->uio_offset + n; 881 np->n_flag |= NMODIFIED; 882 vnode_pager_setsize(vp, np->n_size); 883 884 save = bp->b_flags & B_CACHE; 885 bcount += n; 886 allocbuf(bp, bcount); 887 bp->b_flags |= save; 888 } 889 } else { 890 /* 891 * Obtain the locked cache block first, and then 892 * adjust the file's size as appropriate. 893 */ 894 bcount = on + n; 895 if ((off_t)lbn * biosize + bcount < np->n_size) { 896 if ((off_t)(lbn + 1) * biosize < np->n_size) 897 bcount = biosize; 898 else 899 bcount = np->n_size - (off_t)lbn * biosize; 900 } 901 902 bp = nfs_getcacheblk(vp, lbn, bcount, p); 903 904 if (uio->uio_offset + n > np->n_size) { 905 np->n_size = uio->uio_offset + n; 906 np->n_flag |= NMODIFIED; 907 vnode_pager_setsize(vp, np->n_size); 908 } 909 } 910 911 if (!bp) { 912 error = EINTR; 913 break; 914 } 915 916 /* 917 * Issue a READ if B_CACHE is not set. In special-append 918 * mode, B_CACHE is based on the buffer prior to the write 919 * op and is typically set, avoiding the read. If a read 920 * is required in special append mode, the server will 921 * probably send us a short-read since we extended the file 922 * on our end, resulting in b_resid == 0 and, thusly, 923 * B_CACHE getting set. 924 * 925 * We can also avoid issuing the read if the write covers 926 * the entire buffer. We have to make sure the buffer state 927 * is reasonable in this case since we will not be initiating 928 * I/O. See the comments in kern/vfs_bio.c's getblk() for 929 * more information. 930 * 931 * B_CACHE may also be set due to the buffer being cached 932 * normally. 933 */ 934 935 if (on == 0 && n == bcount) { 936 bp->b_flags |= B_CACHE; 937 bp->b_flags &= ~(B_ERROR | B_INVAL); 938 } 939 940 if ((bp->b_flags & B_CACHE) == 0) { 941 bp->b_iocmd = BIO_READ; 942 vfs_busy_pages(bp, 0); 943 error = nfs_doio(bp, cred, p); 944 if (error) { 945 brelse(bp); 946 break; 947 } 948 } 949 if (!bp) { 950 error = EINTR; 951 break; 952 } 953 if (bp->b_wcred == NOCRED) { 954 crhold(cred); 955 bp->b_wcred = cred; 956 } 957 np->n_flag |= NMODIFIED; 958 959 /* 960 * If dirtyend exceeds file size, chop it down. This should 961 * not normally occur but there is an append race where it 962 * might occur XXX, so we log it. 963 * 964 * If the chopping creates a reverse-indexed or degenerate 965 * situation with dirtyoff/end, we 0 both of them. 966 */ 967 968 if (bp->b_dirtyend > bcount) { 969 printf("NFS append race @%lx:%d\n", 970 (long)bp->b_blkno * DEV_BSIZE, 971 bp->b_dirtyend - bcount); 972 bp->b_dirtyend = bcount; 973 } 974 975 if (bp->b_dirtyoff >= bp->b_dirtyend) 976 bp->b_dirtyoff = bp->b_dirtyend = 0; 977 978 /* 979 * If the new write will leave a contiguous dirty 980 * area, just update the b_dirtyoff and b_dirtyend, 981 * otherwise force a write rpc of the old dirty area. 982 * 983 * While it is possible to merge discontiguous writes due to 984 * our having a B_CACHE buffer ( and thus valid read data 985 * for the hole), we don't because it could lead to 986 * significant cache coherency problems with multiple clients, 987 * especially if locking is implemented later on. 988 * 989 * as an optimization we could theoretically maintain 990 * a linked list of discontinuous areas, but we would still 991 * have to commit them separately so there isn't much 992 * advantage to it except perhaps a bit of asynchronization. 993 */ 994 995 if (bp->b_dirtyend > 0 && 996 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 997 if (VOP_BWRITE(bp->b_vp, bp) == EINTR) 998 return (EINTR); 999 goto again; 1000 } 1001 1002 /* 1003 * Check for valid write lease and get one as required. 1004 * In case getblk() and/or bwrite() delayed us. 1005 */ 1006 if ((nmp->nm_flag & NFSMNT_NQNFS) && 1007 NQNFS_CKINVALID(vp, np, ND_WRITE)) { 1008 do { 1009 error = nqnfs_getlease(vp, ND_WRITE, cred, p); 1010 } while (error == NQNFS_EXPIRED); 1011 if (error) { 1012 brelse(bp); 1013 break; 1014 } 1015 if (np->n_lrev != np->n_brev || 1016 (np->n_flag & NQNFSNONCACHE)) { 1017 brelse(bp); 1018 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 1019 if (error) 1020 break; 1021 np->n_brev = np->n_lrev; 1022 goto again; 1023 } 1024 } 1025 1026 error = uiomove((char *)bp->b_data + on, n, uio); 1027 1028 /* 1029 * Since this block is being modified, it must be written 1030 * again and not just committed. Since write clustering does 1031 * not work for the stage 1 data write, only the stage 2 1032 * commit rpc, we have to clear B_CLUSTEROK as well. 1033 */ 1034 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1035 1036 if (error) { 1037 bp->b_flags |= B_ERROR; 1038 brelse(bp); 1039 break; 1040 } 1041 1042 /* 1043 * Only update dirtyoff/dirtyend if not a degenerate 1044 * condition. 1045 */ 1046 if (n) { 1047 if (bp->b_dirtyend > 0) { 1048 bp->b_dirtyoff = min(on, bp->b_dirtyoff); 1049 bp->b_dirtyend = max((on + n), bp->b_dirtyend); 1050 } else { 1051 bp->b_dirtyoff = on; 1052 bp->b_dirtyend = on + n; 1053 } 1054 vfs_bio_set_validclean(bp, on, n); 1055 } 1056 1057 /* 1058 * If the lease is non-cachable or IO_SYNC do bwrite(). 1059 * 1060 * IO_INVAL appears to be unused. The idea appears to be 1061 * to turn off caching in this case. Very odd. XXX 1062 */ 1063 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) { 1064 if (ioflag & IO_INVAL) 1065 bp->b_flags |= B_NOCACHE; 1066 error = VOP_BWRITE(bp->b_vp, bp); 1067 if (error) 1068 break; 1069 if (np->n_flag & NQNFSNONCACHE) { 1070 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 1071 if (error) 1072 break; 1073 } 1074 } else if ((n + on) == biosize && 1075 (nmp->nm_flag & NFSMNT_NQNFS) == 0) { 1076 bp->b_flags |= B_ASYNC; 1077 (void)nfs_writebp(bp, 0, 0); 1078 } else { 1079 bdwrite(bp); 1080 } 1081 } while (uio->uio_resid > 0 && n > 0); 1082 1083 if (haverslock) 1084 nfs_rsunlock(np, p); 1085 1086 return (error); 1087} 1088 1089/* 1090 * Get an nfs cache block. 1091 * 1092 * Allocate a new one if the block isn't currently in the cache 1093 * and return the block marked busy. If the calling process is 1094 * interrupted by a signal for an interruptible mount point, return 1095 * NULL. 1096 * 1097 * The caller must carefully deal with the possible B_INVAL state of 1098 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it 1099 * indirectly), so synchronous reads can be issued without worrying about 1100 * the B_INVAL state. We have to be a little more careful when dealing 1101 * with writes (see comments in nfs_write()) when extending a file past 1102 * its EOF. 1103 */ 1104static struct buf * 1105nfs_getcacheblk(vp, bn, size, p) 1106 struct vnode *vp; 1107 daddr_t bn; 1108 int size; 1109 struct proc *p; 1110{ 1111 register struct buf *bp; 1112 struct mount *mp; 1113 struct nfsmount *nmp; 1114 1115 mp = vp->v_mount; 1116 nmp = VFSTONFS(mp); 1117 1118 if (nmp->nm_flag & NFSMNT_INT) { 1119 bp = getblk(vp, bn, size, PCATCH, 0); 1120 while (bp == (struct buf *)0) { 1121 if (nfs_sigintr(nmp, (struct nfsreq *)0, p)) 1122 return ((struct buf *)0); 1123 bp = getblk(vp, bn, size, 0, 2 * hz); 1124 } 1125 } else { 1126 bp = getblk(vp, bn, size, 0, 0); 1127 } 1128 1129 if (vp->v_type == VREG) { 1130 int biosize; 1131 1132 biosize = mp->mnt_stat.f_iosize; 1133 bp->b_blkno = bn * (biosize / DEV_BSIZE); 1134 } 1135 return (bp); 1136} 1137 1138/* 1139 * Flush and invalidate all dirty buffers. If another process is already 1140 * doing the flush, just wait for completion. 1141 */ 1142int 1143nfs_vinvalbuf(vp, flags, cred, p, intrflg) 1144 struct vnode *vp; 1145 int flags; 1146 struct ucred *cred; 1147 struct proc *p; 1148 int intrflg; 1149{ 1150 register struct nfsnode *np = VTONFS(vp); 1151 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1152 int error = 0, slpflag, slptimeo; 1153 1154 if (vp->v_flag & VXLOCK) { 1155 return (0); 1156 } 1157 1158 if ((nmp->nm_flag & NFSMNT_INT) == 0) 1159 intrflg = 0; 1160 if (intrflg) { 1161 slpflag = PCATCH; 1162 slptimeo = 2 * hz; 1163 } else { 1164 slpflag = 0; 1165 slptimeo = 0; 1166 } 1167 /* 1168 * First wait for any other process doing a flush to complete. 1169 */ 1170 while (np->n_flag & NFLUSHINPROG) { 1171 np->n_flag |= NFLUSHWANT; 1172 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval", 1173 slptimeo); 1174 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) 1175 return (EINTR); 1176 } 1177 1178 /* 1179 * Now, flush as required. 1180 */ 1181 np->n_flag |= NFLUSHINPROG; 1182 error = vinvalbuf(vp, flags, cred, p, slpflag, 0); 1183 while (error) { 1184 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) { 1185 np->n_flag &= ~NFLUSHINPROG; 1186 if (np->n_flag & NFLUSHWANT) { 1187 np->n_flag &= ~NFLUSHWANT; 1188 wakeup((caddr_t)&np->n_flag); 1189 } 1190 return (EINTR); 1191 } 1192 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo); 1193 } 1194 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG); 1195 if (np->n_flag & NFLUSHWANT) { 1196 np->n_flag &= ~NFLUSHWANT; 1197 wakeup((caddr_t)&np->n_flag); 1198 } 1199 return (0); 1200} 1201 1202/* 1203 * Initiate asynchronous I/O. Return an error if no nfsiods are available. 1204 * This is mainly to avoid queueing async I/O requests when the nfsiods 1205 * are all hung on a dead server. 1206 * 1207 * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp 1208 * is eventually dequeued by the async daemon, nfs_doio() *will*. 1209 */ 1210int 1211nfs_asyncio(bp, cred, procp) 1212 register struct buf *bp; 1213 struct ucred *cred; 1214 struct proc *procp; 1215{ 1216 struct nfsmount *nmp; 1217 int i; 1218 int gotiod; 1219 int slpflag = 0; 1220 int slptimeo = 0; 1221 int error; 1222 1223 /* 1224 * If no async daemons then return EIO to force caller to run the rpc 1225 * synchronously. 1226 */ 1227 if (nfs_numasync == 0) 1228 return (EIO); 1229 1230 nmp = VFSTONFS(bp->b_vp->v_mount); 1231 1232 /* 1233 * Commits are usually short and sweet so lets save some cpu and 1234 * leave the async daemons for more important rpc's (such as reads 1235 * and writes). 1236 */ 1237 if (bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) && 1238 (nmp->nm_bufqiods > nfs_numasync / 2)) { 1239 return(EIO); 1240 } 1241 1242again: 1243 if (nmp->nm_flag & NFSMNT_INT) 1244 slpflag = PCATCH; 1245 gotiod = FALSE; 1246 1247 /* 1248 * Find a free iod to process this request. 1249 */ 1250 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 1251 if (nfs_iodwant[i]) { 1252 /* 1253 * Found one, so wake it up and tell it which 1254 * mount to process. 1255 */ 1256 NFS_DPF(ASYNCIO, 1257 ("nfs_asyncio: waking iod %d for mount %p\n", 1258 i, nmp)); 1259 nfs_iodwant[i] = (struct proc *)0; 1260 nfs_iodmount[i] = nmp; 1261 nmp->nm_bufqiods++; 1262 wakeup((caddr_t)&nfs_iodwant[i]); 1263 gotiod = TRUE; 1264 break; 1265 } 1266 1267 /* 1268 * If none are free, we may already have an iod working on this mount 1269 * point. If so, it will process our request. 1270 */ 1271 if (!gotiod) { 1272 if (nmp->nm_bufqiods > 0) { 1273 NFS_DPF(ASYNCIO, 1274 ("nfs_asyncio: %d iods are already processing mount %p\n", 1275 nmp->nm_bufqiods, nmp)); 1276 gotiod = TRUE; 1277 } 1278 } 1279 1280 /* 1281 * If we have an iod which can process the request, then queue 1282 * the buffer. 1283 */ 1284 if (gotiod) { 1285 /* 1286 * Ensure that the queue never grows too large. We still want 1287 * to asynchronize so we block rather then return EIO. 1288 */ 1289 while (nmp->nm_bufqlen >= 2*nfs_numasync) { 1290 NFS_DPF(ASYNCIO, 1291 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp)); 1292 nmp->nm_bufqwant = TRUE; 1293 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO, 1294 "nfsaio", slptimeo); 1295 if (error) { 1296 if (nfs_sigintr(nmp, NULL, procp)) 1297 return (EINTR); 1298 if (slpflag == PCATCH) { 1299 slpflag = 0; 1300 slptimeo = 2 * hz; 1301 } 1302 } 1303 /* 1304 * We might have lost our iod while sleeping, 1305 * so check and loop if nescessary. 1306 */ 1307 if (nmp->nm_bufqiods == 0) { 1308 NFS_DPF(ASYNCIO, 1309 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 1310 goto again; 1311 } 1312 } 1313 1314 if (bp->b_iocmd == BIO_READ) { 1315 if (bp->b_rcred == NOCRED && cred != NOCRED) { 1316 crhold(cred); 1317 bp->b_rcred = cred; 1318 } 1319 } else { 1320 bp->b_flags |= B_WRITEINPROG; 1321 if (bp->b_wcred == NOCRED && cred != NOCRED) { 1322 crhold(cred); 1323 bp->b_wcred = cred; 1324 } 1325 } 1326 1327 BUF_KERNPROC(bp); 1328 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 1329 nmp->nm_bufqlen++; 1330 return (0); 1331 } 1332 1333 /* 1334 * All the iods are busy on other mounts, so return EIO to 1335 * force the caller to process the i/o synchronously. 1336 */ 1337 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n")); 1338 return (EIO); 1339} 1340 1341/* 1342 * Do an I/O operation to/from a cache block. This may be called 1343 * synchronously or from an nfsiod. 1344 */ 1345int 1346nfs_doio(bp, cr, p) 1347 struct buf *bp; 1348 struct ucred *cr; 1349 struct proc *p; 1350{ 1351 struct uio *uiop; 1352 struct vnode *vp; 1353 struct nfsnode *np; 1354 struct nfsmount *nmp; 1355 int error = 0, iomode, must_commit = 0; 1356 struct uio uio; 1357 struct iovec io; 1358 1359 vp = bp->b_vp; 1360 np = VTONFS(vp); 1361 nmp = VFSTONFS(vp->v_mount); 1362 uiop = &uio; 1363 uiop->uio_iov = &io; 1364 uiop->uio_iovcnt = 1; 1365 uiop->uio_segflg = UIO_SYSSPACE; 1366 uiop->uio_procp = p; 1367 1368 /* 1369 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We 1370 * do this here so we do not have to do it in all the code that 1371 * calls us. 1372 */ 1373 bp->b_flags &= ~(B_ERROR | B_INVAL); 1374 1375 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp)); 1376 1377 /* 1378 * Historically, paging was done with physio, but no more. 1379 */ 1380 if (bp->b_flags & B_PHYS) { 1381 /* 1382 * ...though reading /dev/drum still gets us here. 1383 */ 1384 io.iov_len = uiop->uio_resid = bp->b_bcount; 1385 /* mapping was done by vmapbuf() */ 1386 io.iov_base = bp->b_data; 1387 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1388 if (bp->b_iocmd == BIO_READ) { 1389 uiop->uio_rw = UIO_READ; 1390 nfsstats.read_physios++; 1391 error = nfs_readrpc(vp, uiop, cr); 1392 } else { 1393 int com; 1394 1395 iomode = NFSV3WRITE_DATASYNC; 1396 uiop->uio_rw = UIO_WRITE; 1397 nfsstats.write_physios++; 1398 error = nfs_writerpc(vp, uiop, cr, &iomode, &com); 1399 } 1400 if (error) { 1401 bp->b_flags |= B_ERROR; 1402 bp->b_error = error; 1403 } 1404 } else if (bp->b_iocmd == BIO_READ) { 1405 io.iov_len = uiop->uio_resid = bp->b_bcount; 1406 io.iov_base = bp->b_data; 1407 uiop->uio_rw = UIO_READ; 1408 switch (vp->v_type) { 1409 case VREG: 1410 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1411 nfsstats.read_bios++; 1412 error = nfs_readrpc(vp, uiop, cr); 1413 if (!error) { 1414 if (uiop->uio_resid) { 1415 /* 1416 * If we had a short read with no error, we must have 1417 * hit a file hole. We should zero-fill the remainder. 1418 * This can also occur if the server hits the file EOF. 1419 * 1420 * Holes used to be able to occur due to pending 1421 * writes, but that is not possible any longer. 1422 */ 1423 int nread = bp->b_bcount - uiop->uio_resid; 1424 int left = bp->b_bcount - nread; 1425 1426 if (left > 0) 1427 bzero((char *)bp->b_data + nread, left); 1428 uiop->uio_resid = 0; 1429 } 1430 } 1431 if (p && (vp->v_flag & VTEXT) && 1432 (((nmp->nm_flag & NFSMNT_NQNFS) && 1433 NQNFS_CKINVALID(vp, np, ND_READ) && 1434 np->n_lrev != np->n_brev) || 1435 (!(nmp->nm_flag & NFSMNT_NQNFS) && 1436 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) { 1437 uprintf("Process killed due to text file modification\n"); 1438 psignal(p, SIGKILL); 1439 PHOLD(p); 1440 } 1441 break; 1442 case VLNK: 1443 uiop->uio_offset = (off_t)0; 1444 nfsstats.readlink_bios++; 1445 error = nfs_readlinkrpc(vp, uiop, cr); 1446 break; 1447 case VDIR: 1448 nfsstats.readdir_bios++; 1449 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 1450 if (nmp->nm_flag & NFSMNT_RDIRPLUS) { 1451 error = nfs_readdirplusrpc(vp, uiop, cr); 1452 if (error == NFSERR_NOTSUPP) 1453 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 1454 } 1455 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 1456 error = nfs_readdirrpc(vp, uiop, cr); 1457 /* 1458 * end-of-directory sets B_INVAL but does not generate an 1459 * error. 1460 */ 1461 if (error == 0 && uiop->uio_resid == bp->b_bcount) 1462 bp->b_flags |= B_INVAL; 1463 break; 1464 default: 1465 printf("nfs_doio: type %x unexpected\n",vp->v_type); 1466 break; 1467 }; 1468 if (error) { 1469 bp->b_flags |= B_ERROR; 1470 bp->b_error = error; 1471 } 1472 } else { 1473 /* 1474 * If we only need to commit, try to commit 1475 */ 1476 if (bp->b_flags & B_NEEDCOMMIT) { 1477 int retv; 1478 off_t off; 1479 1480 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff; 1481 bp->b_flags |= B_WRITEINPROG; 1482 retv = nfs_commit( 1483 bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff, 1484 bp->b_wcred, p); 1485 bp->b_flags &= ~B_WRITEINPROG; 1486 if (retv == 0) { 1487 bp->b_dirtyoff = bp->b_dirtyend = 0; 1488 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1489 bp->b_resid = 0; 1490 biodone(bp); 1491 return (0); 1492 } 1493 if (retv == NFSERR_STALEWRITEVERF) { 1494 nfs_clearcommit(bp->b_vp->v_mount); 1495 } 1496 } 1497 1498 /* 1499 * Setup for actual write 1500 */ 1501 1502 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size) 1503 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE; 1504 1505 if (bp->b_dirtyend > bp->b_dirtyoff) { 1506 io.iov_len = uiop->uio_resid = bp->b_dirtyend 1507 - bp->b_dirtyoff; 1508 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE 1509 + bp->b_dirtyoff; 1510 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 1511 uiop->uio_rw = UIO_WRITE; 1512 nfsstats.write_bios++; 1513 1514 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 1515 iomode = NFSV3WRITE_UNSTABLE; 1516 else 1517 iomode = NFSV3WRITE_FILESYNC; 1518 1519 bp->b_flags |= B_WRITEINPROG; 1520 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit); 1521 1522 /* 1523 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try 1524 * to cluster the buffers needing commit. This will allow 1525 * the system to submit a single commit rpc for the whole 1526 * cluster. We can do this even if the buffer is not 100% 1527 * dirty (relative to the NFS blocksize), so we optimize the 1528 * append-to-file-case. 1529 * 1530 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be 1531 * cleared because write clustering only works for commit 1532 * rpc's, not for the data portion of the write). 1533 */ 1534 1535 if (!error && iomode == NFSV3WRITE_UNSTABLE) { 1536 bp->b_flags |= B_NEEDCOMMIT; 1537 if (bp->b_dirtyoff == 0 1538 && bp->b_dirtyend == bp->b_bcount) 1539 bp->b_flags |= B_CLUSTEROK; 1540 } else { 1541 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1542 } 1543 bp->b_flags &= ~B_WRITEINPROG; 1544 1545 /* 1546 * For an interrupted write, the buffer is still valid 1547 * and the write hasn't been pushed to the server yet, 1548 * so we can't set B_ERROR and report the interruption 1549 * by setting B_EINTR. For the B_ASYNC case, B_EINTR 1550 * is not relevant, so the rpc attempt is essentially 1551 * a noop. For the case of a V3 write rpc not being 1552 * committed to stable storage, the block is still 1553 * dirty and requires either a commit rpc or another 1554 * write rpc with iomode == NFSV3WRITE_FILESYNC before 1555 * the block is reused. This is indicated by setting 1556 * the B_DELWRI and B_NEEDCOMMIT flags. 1557 * 1558 * If the buffer is marked B_PAGING, it does not reside on 1559 * the vp's paging queues so we cannot call bdirty(). The 1560 * bp in this case is not an NFS cache block so we should 1561 * be safe. XXX 1562 */ 1563 if (error == EINTR 1564 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 1565 int s; 1566 1567 s = splbio(); 1568 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 1569 if ((bp->b_flags & B_PAGING) == 0) { 1570 bdirty(bp); 1571 bp->b_flags &= ~B_DONE; 1572 } 1573 if (error && (bp->b_flags & B_ASYNC) == 0) 1574 bp->b_flags |= B_EINTR; 1575 splx(s); 1576 } else { 1577 if (error) { 1578 bp->b_flags |= B_ERROR; 1579 bp->b_error = np->n_error = error; 1580 np->n_flag |= NWRITEERR; 1581 } 1582 bp->b_dirtyoff = bp->b_dirtyend = 0; 1583 } 1584 } else { 1585 bp->b_resid = 0; 1586 biodone(bp); 1587 return (0); 1588 } 1589 } 1590 bp->b_resid = uiop->uio_resid; 1591 if (must_commit) 1592 nfs_clearcommit(vp->v_mount); 1593 biodone(bp); 1594 return (error); 1595} 1596