nfs_bio.c revision 120264
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 120264 2003-09-19 23:37:49Z 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 if (vp->v_iflag & VI_XLOCK) { 1071#ifdef INVARIANTS 1072 backtrace(); 1073#endif 1074 return (0); 1075 } 1076 1077 if ((nmp->nm_flag & NFSMNT_INT) == 0) 1078 intrflg = 0; 1079 if (intrflg) { 1080 slpflag = PCATCH; 1081 slptimeo = 2 * hz; 1082 } else { 1083 slpflag = 0; 1084 slptimeo = 0; 1085 } 1086 /* 1087 * First wait for any other process doing a flush to complete. 1088 */ 1089 while (np->n_flag & NFLUSHINPROG) { 1090 np->n_flag |= NFLUSHWANT; 1091 error = tsleep(&np->n_flag, PRIBIO + 2, "nfsvinval", 1092 slptimeo); 1093 if (error && intrflg && 1094 nfs_sigintr(nmp, NULL, td)) 1095 return (EINTR); 1096 } 1097 1098 /* 1099 * Now, flush as required. 1100 */ 1101 np->n_flag |= NFLUSHINPROG; 1102 error = vinvalbuf(vp, flags, cred, td, slpflag, 0); 1103 while (error) { 1104 if (intrflg && 1105 nfs_sigintr(nmp, NULL, td)) { 1106 np->n_flag &= ~NFLUSHINPROG; 1107 if (np->n_flag & NFLUSHWANT) { 1108 np->n_flag &= ~NFLUSHWANT; 1109 wakeup(&np->n_flag); 1110 } 1111 return (EINTR); 1112 } 1113 error = vinvalbuf(vp, flags, cred, td, 0, slptimeo); 1114 } 1115 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG); 1116 if (np->n_flag & NFLUSHWANT) { 1117 np->n_flag &= ~NFLUSHWANT; 1118 wakeup(&np->n_flag); 1119 } 1120 return (0); 1121} 1122 1123/* 1124 * Initiate asynchronous I/O. Return an error if no nfsiods are available. 1125 * This is mainly to avoid queueing async I/O requests when the nfsiods 1126 * are all hung on a dead server. 1127 * 1128 * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp 1129 * is eventually dequeued by the async daemon, nfs_doio() *will*. 1130 */ 1131int 1132nfs_asyncio(struct buf *bp, struct ucred *cred, struct thread *td) 1133{ 1134 struct nfsmount *nmp; 1135 int iod; 1136 int gotiod; 1137 int slpflag = 0; 1138 int slptimeo = 0; 1139 int error; 1140 1141 nmp = VFSTONFS(bp->b_vp->v_mount); 1142 1143 /* 1144 * Commits are usually short and sweet so lets save some cpu and 1145 * leave the async daemons for more important rpc's (such as reads 1146 * and writes). 1147 */ 1148 if (bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) && 1149 (nmp->nm_bufqiods > nfs_numasync / 2)) { 1150 return(EIO); 1151 } 1152 1153again: 1154 if (nmp->nm_flag & NFSMNT_INT) 1155 slpflag = PCATCH; 1156 gotiod = FALSE; 1157 1158 /* 1159 * Find a free iod to process this request. 1160 */ 1161 for (iod = 0; iod < nfs_numasync; iod++) 1162 if (nfs_iodwant[iod]) { 1163 gotiod = TRUE; 1164 break; 1165 } 1166 1167 /* 1168 * Try to create one if none are free. 1169 */ 1170 if (!gotiod) { 1171 iod = nfs_nfsiodnew(); 1172 if (iod != -1) 1173 gotiod = TRUE; 1174 } 1175 1176 if (gotiod) { 1177 /* 1178 * Found one, so wake it up and tell it which 1179 * mount to process. 1180 */ 1181 NFS_DPF(ASYNCIO, ("nfs_asyncio: waking iod %d for mount %p\n", 1182 iod, nmp)); 1183 nfs_iodwant[iod] = NULL; 1184 nfs_iodmount[iod] = nmp; 1185 nmp->nm_bufqiods++; 1186 wakeup(&nfs_iodwant[iod]); 1187 } 1188 1189 /* 1190 * If none are free, we may already have an iod working on this mount 1191 * point. If so, it will process our request. 1192 */ 1193 if (!gotiod) { 1194 if (nmp->nm_bufqiods > 0) { 1195 NFS_DPF(ASYNCIO, 1196 ("nfs_asyncio: %d iods are already processing mount %p\n", 1197 nmp->nm_bufqiods, nmp)); 1198 gotiod = TRUE; 1199 } 1200 } 1201 1202 /* 1203 * If we have an iod which can process the request, then queue 1204 * the buffer. 1205 */ 1206 if (gotiod) { 1207 /* 1208 * Ensure that the queue never grows too large. We still want 1209 * to asynchronize so we block rather then return EIO. 1210 */ 1211 while (nmp->nm_bufqlen >= 2*nfs_numasync) { 1212 NFS_DPF(ASYNCIO, 1213 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp)); 1214 nmp->nm_bufqwant = TRUE; 1215 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO, 1216 "nfsaio", slptimeo); 1217 if (error) { 1218 if (nfs_sigintr(nmp, NULL, td)) 1219 return (EINTR); 1220 if (slpflag == PCATCH) { 1221 slpflag = 0; 1222 slptimeo = 2 * hz; 1223 } 1224 } 1225 /* 1226 * We might have lost our iod while sleeping, 1227 * so check and loop if nescessary. 1228 */ 1229 if (nmp->nm_bufqiods == 0) { 1230 NFS_DPF(ASYNCIO, 1231 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 1232 goto again; 1233 } 1234 } 1235 1236 if (bp->b_iocmd == BIO_READ) { 1237 if (bp->b_rcred == NOCRED && cred != NOCRED) 1238 bp->b_rcred = crhold(cred); 1239 } else { 1240 bp->b_flags |= B_WRITEINPROG; 1241 if (bp->b_wcred == NOCRED && cred != NOCRED) 1242 bp->b_wcred = crhold(cred); 1243 } 1244 1245 BUF_KERNPROC(bp); 1246 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 1247 nmp->nm_bufqlen++; 1248 return (0); 1249 } 1250 1251 /* 1252 * All the iods are busy on other mounts, so return EIO to 1253 * force the caller to process the i/o synchronously. 1254 */ 1255 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n")); 1256 return (EIO); 1257} 1258 1259/* 1260 * Do an I/O operation to/from a cache block. This may be called 1261 * synchronously or from an nfsiod. 1262 */ 1263int 1264nfs_doio(struct buf *bp, struct ucred *cr, struct thread *td) 1265{ 1266 struct uio *uiop; 1267 struct vnode *vp; 1268 struct nfsnode *np; 1269 struct nfsmount *nmp; 1270 int error = 0, iomode, must_commit = 0; 1271 struct uio uio; 1272 struct iovec io; 1273 struct proc *p = td ? td->td_proc : NULL; 1274 1275 vp = bp->b_vp; 1276 np = VTONFS(vp); 1277 nmp = VFSTONFS(vp->v_mount); 1278 uiop = &uio; 1279 uiop->uio_iov = &io; 1280 uiop->uio_iovcnt = 1; 1281 uiop->uio_segflg = UIO_SYSSPACE; 1282 uiop->uio_td = td; 1283 1284 /* 1285 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We 1286 * do this here so we do not have to do it in all the code that 1287 * calls us. 1288 */ 1289 bp->b_flags &= ~B_INVAL; 1290 bp->b_ioflags &= ~BIO_ERROR; 1291 1292 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp)); 1293 1294 /* 1295 * Historically, paging was done with physio, but no more. 1296 */ 1297 if (bp->b_flags & B_PHYS) { 1298 /* 1299 * ...though reading /dev/drum still gets us here. 1300 */ 1301 io.iov_len = uiop->uio_resid = bp->b_bcount; 1302 /* mapping was done by vmapbuf() */ 1303 io.iov_base = bp->b_data; 1304 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1305 if (bp->b_iocmd == BIO_READ) { 1306 uiop->uio_rw = UIO_READ; 1307 nfsstats.read_physios++; 1308 error = nfs_readrpc(vp, uiop, cr); 1309 } else { 1310 int com; 1311 1312 iomode = NFSV3WRITE_DATASYNC; 1313 uiop->uio_rw = UIO_WRITE; 1314 nfsstats.write_physios++; 1315 error = nfs_writerpc(vp, uiop, cr, &iomode, &com); 1316 } 1317 if (error) { 1318 bp->b_ioflags |= BIO_ERROR; 1319 bp->b_error = error; 1320 } 1321 } else if (bp->b_iocmd == BIO_READ) { 1322 io.iov_len = uiop->uio_resid = bp->b_bcount; 1323 io.iov_base = bp->b_data; 1324 uiop->uio_rw = UIO_READ; 1325 1326 switch (vp->v_type) { 1327 case VREG: 1328 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1329 nfsstats.read_bios++; 1330 error = nfs_readrpc(vp, uiop, cr); 1331 1332 if (!error) { 1333 if (uiop->uio_resid) { 1334 /* 1335 * If we had a short read with no error, we must have 1336 * hit a file hole. We should zero-fill the remainder. 1337 * This can also occur if the server hits the file EOF. 1338 * 1339 * Holes used to be able to occur due to pending 1340 * writes, but that is not possible any longer. 1341 */ 1342 int nread = bp->b_bcount - uiop->uio_resid; 1343 int left = uiop->uio_resid; 1344 1345 if (left > 0) 1346 bzero((char *)bp->b_data + nread, left); 1347 uiop->uio_resid = 0; 1348 } 1349 } 1350 /* ASSERT_VOP_LOCKED(vp, "nfs_doio"); */ 1351 if (p && (vp->v_vflag & VV_TEXT) && 1352 (np->n_mtime != np->n_vattr.va_mtime.tv_sec)) { 1353 uprintf("Process killed due to text file modification\n"); 1354 PROC_LOCK(p); 1355 psignal(p, SIGKILL); 1356 _PHOLD(p); 1357 PROC_UNLOCK(p); 1358 } 1359 break; 1360 case VLNK: 1361 uiop->uio_offset = (off_t)0; 1362 nfsstats.readlink_bios++; 1363 error = nfs_readlinkrpc(vp, uiop, cr); 1364 break; 1365 case VDIR: 1366 nfsstats.readdir_bios++; 1367 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 1368 if (nmp->nm_flag & NFSMNT_RDIRPLUS) { 1369 error = nfs_readdirplusrpc(vp, uiop, cr); 1370 if (error == NFSERR_NOTSUPP) 1371 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 1372 } 1373 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 1374 error = nfs_readdirrpc(vp, uiop, cr); 1375 /* 1376 * end-of-directory sets B_INVAL but does not generate an 1377 * error. 1378 */ 1379 if (error == 0 && uiop->uio_resid == bp->b_bcount) 1380 bp->b_flags |= B_INVAL; 1381 break; 1382 default: 1383 printf("nfs_doio: type %x unexpected\n", vp->v_type); 1384 break; 1385 }; 1386 if (error) { 1387 bp->b_ioflags |= BIO_ERROR; 1388 bp->b_error = error; 1389 } 1390 } else { 1391 /* 1392 * If we only need to commit, try to commit 1393 */ 1394 if (bp->b_flags & B_NEEDCOMMIT) { 1395 int retv; 1396 off_t off; 1397 1398 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff; 1399 bp->b_flags |= B_WRITEINPROG; 1400 retv = nfs_commit( 1401 bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff, 1402 bp->b_wcred, td); 1403 bp->b_flags &= ~B_WRITEINPROG; 1404 if (retv == 0) { 1405 bp->b_dirtyoff = bp->b_dirtyend = 0; 1406 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1407 bp->b_resid = 0; 1408 bufdone(bp); 1409 return (0); 1410 } 1411 if (retv == NFSERR_STALEWRITEVERF) { 1412 nfs_clearcommit(bp->b_vp->v_mount); 1413 } 1414 } 1415 1416 /* 1417 * Setup for actual write 1418 */ 1419 1420 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size) 1421 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE; 1422 1423 if (bp->b_dirtyend > bp->b_dirtyoff) { 1424 io.iov_len = uiop->uio_resid = bp->b_dirtyend 1425 - bp->b_dirtyoff; 1426 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE 1427 + bp->b_dirtyoff; 1428 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 1429 uiop->uio_rw = UIO_WRITE; 1430 nfsstats.write_bios++; 1431 1432 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 1433 iomode = NFSV3WRITE_UNSTABLE; 1434 else 1435 iomode = NFSV3WRITE_FILESYNC; 1436 1437 bp->b_flags |= B_WRITEINPROG; 1438 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit); 1439 1440 /* 1441 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try 1442 * to cluster the buffers needing commit. This will allow 1443 * the system to submit a single commit rpc for the whole 1444 * cluster. We can do this even if the buffer is not 100% 1445 * dirty (relative to the NFS blocksize), so we optimize the 1446 * append-to-file-case. 1447 * 1448 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be 1449 * cleared because write clustering only works for commit 1450 * rpc's, not for the data portion of the write). 1451 */ 1452 1453 if (!error && iomode == NFSV3WRITE_UNSTABLE) { 1454 bp->b_flags |= B_NEEDCOMMIT; 1455 if (bp->b_dirtyoff == 0 1456 && bp->b_dirtyend == bp->b_bcount) 1457 bp->b_flags |= B_CLUSTEROK; 1458 } else { 1459 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1460 } 1461 bp->b_flags &= ~B_WRITEINPROG; 1462 1463 /* 1464 * For an interrupted write, the buffer is still valid 1465 * and the write hasn't been pushed to the server yet, 1466 * so we can't set BIO_ERROR and report the interruption 1467 * by setting B_EINTR. For the B_ASYNC case, B_EINTR 1468 * is not relevant, so the rpc attempt is essentially 1469 * a noop. For the case of a V3 write rpc not being 1470 * committed to stable storage, the block is still 1471 * dirty and requires either a commit rpc or another 1472 * write rpc with iomode == NFSV3WRITE_FILESYNC before 1473 * the block is reused. This is indicated by setting 1474 * the B_DELWRI and B_NEEDCOMMIT flags. 1475 * 1476 * If the buffer is marked B_PAGING, it does not reside on 1477 * the vp's paging queues so we cannot call bdirty(). The 1478 * bp in this case is not an NFS cache block so we should 1479 * be safe. XXX 1480 */ 1481 if (error == EINTR 1482 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 1483 int s; 1484 1485 s = splbio(); 1486 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 1487 if ((bp->b_flags & B_PAGING) == 0) { 1488 bdirty(bp); 1489 bp->b_flags &= ~B_DONE; 1490 } 1491 if (error && (bp->b_flags & B_ASYNC) == 0) 1492 bp->b_flags |= B_EINTR; 1493 splx(s); 1494 } else { 1495 if (error) { 1496 bp->b_ioflags |= BIO_ERROR; 1497 bp->b_error = np->n_error = error; 1498 np->n_flag |= NWRITEERR; 1499 } 1500 bp->b_dirtyoff = bp->b_dirtyend = 0; 1501 } 1502 } else { 1503 bp->b_resid = 0; 1504 bufdone(bp); 1505 return (0); 1506 } 1507 } 1508 bp->b_resid = uiop->uio_resid; 1509 if (must_commit) 1510 nfs_clearcommit(vp->v_mount); 1511 bufdone(bp); 1512 return (error); 1513} 1514 1515/* 1516 * Used to aid in handling ftruncate() operations on the NFS client side. 1517 * Truncation creates a number of special problems for NFS. We have to 1518 * throw away VM pages and buffer cache buffers that are beyond EOF, and 1519 * we have to properly handle VM pages or (potentially dirty) buffers 1520 * that straddle the truncation point. 1521 */ 1522 1523int 1524nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize) 1525{ 1526 struct nfsnode *np = VTONFS(vp); 1527 u_quad_t tsize = np->n_size; 1528 int biosize = vp->v_mount->mnt_stat.f_iosize; 1529 int error = 0; 1530 1531 np->n_size = nsize; 1532 1533 if (np->n_size < tsize) { 1534 struct buf *bp; 1535 daddr_t lbn; 1536 int bufsize; 1537 1538 /* 1539 * vtruncbuf() doesn't get the buffer overlapping the 1540 * truncation point. We may have a B_DELWRI and/or B_CACHE 1541 * buffer that now needs to be truncated. 1542 */ 1543 error = vtruncbuf(vp, cred, td, nsize, biosize); 1544 lbn = nsize / biosize; 1545 bufsize = nsize & (biosize - 1); 1546 bp = nfs_getcacheblk(vp, lbn, bufsize, td); 1547 if (bp->b_dirtyoff > bp->b_bcount) 1548 bp->b_dirtyoff = bp->b_bcount; 1549 if (bp->b_dirtyend > bp->b_bcount) 1550 bp->b_dirtyend = bp->b_bcount; 1551 bp->b_flags |= B_RELBUF; /* don't leave garbage around */ 1552 brelse(bp); 1553 } else { 1554 vnode_pager_setsize(vp, nsize); 1555 } 1556 return(error); 1557} 1558 1559