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