nfs_bio.c revision 239065
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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_bio.c 239065 2012-08-05 14:11:42Z kib $"); 37 38#include "opt_kdtrace.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/bio.h> 43#include <sys/buf.h> 44#include <sys/kernel.h> 45#include <sys/mbuf.h> 46#include <sys/mount.h> 47#include <sys/proc.h> 48#include <sys/vmmeter.h> 49#include <sys/vnode.h> 50 51#include <vm/vm.h> 52#include <vm/vm_param.h> 53#include <vm/vm_extern.h> 54#include <vm/vm_page.h> 55#include <vm/vm_object.h> 56#include <vm/vm_pager.h> 57#include <vm/vnode_pager.h> 58 59#include <nfs/nfsproto.h> 60#include <nfsclient/nfs.h> 61#include <nfsclient/nfsmount.h> 62#include <nfsclient/nfsnode.h> 63#include <nfs/nfs_kdtrace.h> 64 65static struct buf *nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, 66 struct thread *td); 67static int nfs_directio_write(struct vnode *vp, struct uio *uiop, 68 struct ucred *cred, int ioflag); 69 70extern int nfs_directio_enable; 71extern int nfs_directio_allow_mmap; 72 73/* 74 * Vnode op for VM getpages. 75 */ 76int 77nfs_getpages(struct vop_getpages_args *ap) 78{ 79 int i, error, nextoff, size, toff, count, npages; 80 struct uio uio; 81 struct iovec iov; 82 vm_offset_t kva; 83 struct buf *bp; 84 struct vnode *vp; 85 struct thread *td; 86 struct ucred *cred; 87 struct nfsmount *nmp; 88 vm_object_t object; 89 vm_page_t *pages; 90 struct nfsnode *np; 91 92 vp = ap->a_vp; 93 np = VTONFS(vp); 94 td = curthread; /* XXX */ 95 cred = curthread->td_ucred; /* XXX */ 96 nmp = VFSTONFS(vp->v_mount); 97 pages = ap->a_m; 98 count = ap->a_count; 99 100 if ((object = vp->v_object) == NULL) { 101 nfs_printf("nfs_getpages: called with non-merged cache vnode??\n"); 102 return (VM_PAGER_ERROR); 103 } 104 105 if (nfs_directio_enable && !nfs_directio_allow_mmap) { 106 mtx_lock(&np->n_mtx); 107 if ((np->n_flag & NNONCACHE) && (vp->v_type == VREG)) { 108 mtx_unlock(&np->n_mtx); 109 nfs_printf("nfs_getpages: called on non-cacheable vnode??\n"); 110 return (VM_PAGER_ERROR); 111 } else 112 mtx_unlock(&np->n_mtx); 113 } 114 115 mtx_lock(&nmp->nm_mtx); 116 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 117 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 118 mtx_unlock(&nmp->nm_mtx); 119 /* We'll never get here for v4, because we always have fsinfo */ 120 (void)nfs_fsinfo(nmp, vp, cred, td); 121 } else 122 mtx_unlock(&nmp->nm_mtx); 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 VM_OBJECT_LOCK(object); 132 if (pages[ap->a_reqpage]->valid != 0) { 133 for (i = 0; i < npages; ++i) { 134 if (i != ap->a_reqpage) { 135 vm_page_lock(pages[i]); 136 vm_page_free(pages[i]); 137 vm_page_unlock(pages[i]); 138 } 139 } 140 VM_OBJECT_UNLOCK(object); 141 return (0); 142 } 143 VM_OBJECT_UNLOCK(object); 144 145 /* 146 * We use only the kva address for the buffer, but this is extremely 147 * convienient and fast. 148 */ 149 bp = getpbuf(&nfs_pbuf_freecnt); 150 151 kva = (vm_offset_t) bp->b_data; 152 pmap_qenter(kva, pages, npages); 153 PCPU_INC(cnt.v_vnodein); 154 PCPU_ADD(cnt.v_vnodepgsin, npages); 155 156 iov.iov_base = (caddr_t) kva; 157 iov.iov_len = count; 158 uio.uio_iov = &iov; 159 uio.uio_iovcnt = 1; 160 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex); 161 uio.uio_resid = count; 162 uio.uio_segflg = UIO_SYSSPACE; 163 uio.uio_rw = UIO_READ; 164 uio.uio_td = td; 165 166 error = (nmp->nm_rpcops->nr_readrpc)(vp, &uio, cred); 167 pmap_qremove(kva, npages); 168 169 relpbuf(bp, &nfs_pbuf_freecnt); 170 171 if (error && (uio.uio_resid == count)) { 172 nfs_printf("nfs_getpages: error %d\n", error); 173 VM_OBJECT_LOCK(object); 174 for (i = 0; i < npages; ++i) { 175 if (i != ap->a_reqpage) { 176 vm_page_lock(pages[i]); 177 vm_page_free(pages[i]); 178 vm_page_unlock(pages[i]); 179 } 180 } 181 VM_OBJECT_UNLOCK(object); 182 return (VM_PAGER_ERROR); 183 } 184 185 /* 186 * Calculate the number of bytes read and validate only that number 187 * of bytes. Note that due to pending writes, size may be 0. This 188 * does not mean that the remaining data is invalid! 189 */ 190 191 size = count - uio.uio_resid; 192 VM_OBJECT_LOCK(object); 193 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) { 194 vm_page_t m; 195 nextoff = toff + PAGE_SIZE; 196 m = pages[i]; 197 198 if (nextoff <= size) { 199 /* 200 * Read operation filled an entire page 201 */ 202 m->valid = VM_PAGE_BITS_ALL; 203 KASSERT(m->dirty == 0, 204 ("nfs_getpages: page %p is dirty", m)); 205 } else if (size > toff) { 206 /* 207 * Read operation filled a partial page. 208 */ 209 m->valid = 0; 210 vm_page_set_valid_range(m, 0, size - toff); 211 KASSERT(m->dirty == 0, 212 ("nfs_getpages: page %p is dirty", m)); 213 } else { 214 /* 215 * Read operation was short. If no error occured 216 * we may have hit a zero-fill section. We simply 217 * leave valid set to 0. 218 */ 219 ; 220 } 221 if (i != ap->a_reqpage) 222 vm_page_readahead_finish(m, error); 223 } 224 VM_OBJECT_UNLOCK(object); 225 return (0); 226} 227 228/* 229 * Vnode op for VM putpages. 230 */ 231int 232nfs_putpages(struct vop_putpages_args *ap) 233{ 234 struct uio uio; 235 struct iovec iov; 236 vm_offset_t kva; 237 struct buf *bp; 238 int iomode, must_commit, i, error, npages, count; 239 off_t offset; 240 int *rtvals; 241 struct vnode *vp; 242 struct thread *td; 243 struct ucred *cred; 244 struct nfsmount *nmp; 245 struct nfsnode *np; 246 vm_page_t *pages; 247 248 vp = ap->a_vp; 249 np = VTONFS(vp); 250 td = curthread; /* XXX */ 251 /* Set the cred to n_writecred for the write rpcs. */ 252 if (np->n_writecred != NULL) 253 cred = crhold(np->n_writecred); 254 else 255 cred = crhold(curthread->td_ucred); /* XXX */ 256 nmp = VFSTONFS(vp->v_mount); 257 pages = ap->a_m; 258 count = ap->a_count; 259 rtvals = ap->a_rtvals; 260 npages = btoc(count); 261 offset = IDX_TO_OFF(pages[0]->pindex); 262 263 mtx_lock(&nmp->nm_mtx); 264 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 265 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 266 mtx_unlock(&nmp->nm_mtx); 267 (void)nfs_fsinfo(nmp, vp, cred, td); 268 } else 269 mtx_unlock(&nmp->nm_mtx); 270 271 mtx_lock(&np->n_mtx); 272 if (nfs_directio_enable && !nfs_directio_allow_mmap && 273 (np->n_flag & NNONCACHE) && (vp->v_type == VREG)) { 274 mtx_unlock(&np->n_mtx); 275 nfs_printf("nfs_putpages: called on noncache-able vnode??\n"); 276 mtx_lock(&np->n_mtx); 277 } 278 279 for (i = 0; i < npages; i++) 280 rtvals[i] = VM_PAGER_ERROR; 281 282 /* 283 * When putting pages, do not extend file past EOF. 284 */ 285 if (offset + count > np->n_size) { 286 count = np->n_size - offset; 287 if (count < 0) 288 count = 0; 289 } 290 mtx_unlock(&np->n_mtx); 291 292 /* 293 * We use only the kva address for the buffer, but this is extremely 294 * convienient and fast. 295 */ 296 bp = getpbuf(&nfs_pbuf_freecnt); 297 298 kva = (vm_offset_t) bp->b_data; 299 pmap_qenter(kva, pages, npages); 300 PCPU_INC(cnt.v_vnodeout); 301 PCPU_ADD(cnt.v_vnodepgsout, count); 302 303 iov.iov_base = (caddr_t) kva; 304 iov.iov_len = count; 305 uio.uio_iov = &iov; 306 uio.uio_iovcnt = 1; 307 uio.uio_offset = offset; 308 uio.uio_resid = count; 309 uio.uio_segflg = UIO_SYSSPACE; 310 uio.uio_rw = UIO_WRITE; 311 uio.uio_td = td; 312 313 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0) 314 iomode = NFSV3WRITE_UNSTABLE; 315 else 316 iomode = NFSV3WRITE_FILESYNC; 317 318 error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred, &iomode, &must_commit); 319 crfree(cred); 320 321 pmap_qremove(kva, npages); 322 relpbuf(bp, &nfs_pbuf_freecnt); 323 324 if (!error) { 325 vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid); 326 if (must_commit) { 327 nfs_clearcommit(vp->v_mount); 328 } 329 } 330 return rtvals[0]; 331} 332 333/* 334 * For nfs, cache consistency can only be maintained approximately. 335 * Although RFC1094 does not specify the criteria, the following is 336 * believed to be compatible with the reference port. 337 * For nfs: 338 * If the file's modify time on the server has changed since the 339 * last read rpc or you have written to the file, 340 * you may have lost data cache consistency with the 341 * server, so flush all of the file's data out of the cache. 342 * Then force a getattr rpc to ensure that you have up to date 343 * attributes. 344 * NB: This implies that cache data can be read when up to 345 * NFS_ATTRTIMEO seconds out of date. If you find that you need current 346 * attributes this could be forced by setting n_attrstamp to 0 before 347 * the VOP_GETATTR() call. 348 */ 349static inline int 350nfs_bioread_check_cons(struct vnode *vp, struct thread *td, struct ucred *cred) 351{ 352 int error = 0; 353 struct vattr vattr; 354 struct nfsnode *np = VTONFS(vp); 355 int old_lock; 356 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 357 358 /* 359 * Grab the exclusive lock before checking whether the cache is 360 * consistent. 361 * XXX - We can make this cheaper later (by acquiring cheaper locks). 362 * But for now, this suffices. 363 */ 364 old_lock = nfs_upgrade_vnlock(vp); 365 if (vp->v_iflag & VI_DOOMED) { 366 nfs_downgrade_vnlock(vp, old_lock); 367 return (EBADF); 368 } 369 370 mtx_lock(&np->n_mtx); 371 if (np->n_flag & NMODIFIED) { 372 mtx_unlock(&np->n_mtx); 373 if (vp->v_type != VREG) { 374 if (vp->v_type != VDIR) 375 panic("nfs: bioread, not dir"); 376 (nmp->nm_rpcops->nr_invaldir)(vp); 377 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 378 if (error) 379 goto out; 380 } 381 np->n_attrstamp = 0; 382 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 383 error = VOP_GETATTR(vp, &vattr, cred); 384 if (error) 385 goto out; 386 mtx_lock(&np->n_mtx); 387 np->n_mtime = vattr.va_mtime; 388 mtx_unlock(&np->n_mtx); 389 } else { 390 mtx_unlock(&np->n_mtx); 391 error = VOP_GETATTR(vp, &vattr, cred); 392 if (error) 393 return (error); 394 mtx_lock(&np->n_mtx); 395 if ((np->n_flag & NSIZECHANGED) 396 || (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime))) { 397 mtx_unlock(&np->n_mtx); 398 if (vp->v_type == VDIR) 399 (nmp->nm_rpcops->nr_invaldir)(vp); 400 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 401 if (error) 402 goto out; 403 mtx_lock(&np->n_mtx); 404 np->n_mtime = vattr.va_mtime; 405 np->n_flag &= ~NSIZECHANGED; 406 } 407 mtx_unlock(&np->n_mtx); 408 } 409out: 410 nfs_downgrade_vnlock(vp, old_lock); 411 return error; 412} 413 414/* 415 * Vnode op for read using bio 416 */ 417int 418nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred) 419{ 420 struct nfsnode *np = VTONFS(vp); 421 int biosize, i; 422 struct buf *bp, *rabp; 423 struct thread *td; 424 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 425 daddr_t lbn, rabn; 426 off_t end; 427 int bcount; 428 int seqcount; 429 int nra, error = 0, n = 0, on = 0; 430 431 KASSERT(uio->uio_rw == UIO_READ, ("nfs_read mode")); 432 if (uio->uio_resid == 0) 433 return (0); 434 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */ 435 return (EINVAL); 436 td = uio->uio_td; 437 438 mtx_lock(&nmp->nm_mtx); 439 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 440 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 441 mtx_unlock(&nmp->nm_mtx); 442 (void)nfs_fsinfo(nmp, vp, cred, td); 443 } else 444 mtx_unlock(&nmp->nm_mtx); 445 446 end = uio->uio_offset + uio->uio_resid; 447 if (vp->v_type != VDIR && 448 (end > nmp->nm_maxfilesize || end < uio->uio_offset)) 449 return (EFBIG); 450 451 if (nfs_directio_enable && (ioflag & IO_DIRECT) && (vp->v_type == VREG)) 452 /* No caching/ no readaheads. Just read data into the user buffer */ 453 return nfs_readrpc(vp, uio, cred); 454 455 biosize = vp->v_bufobj.bo_bsize; 456 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE); 457 458 error = nfs_bioread_check_cons(vp, td, cred); 459 if (error) 460 return error; 461 462 do { 463 u_quad_t nsize; 464 465 mtx_lock(&np->n_mtx); 466 nsize = np->n_size; 467 mtx_unlock(&np->n_mtx); 468 469 switch (vp->v_type) { 470 case VREG: 471 nfsstats.biocache_reads++; 472 lbn = uio->uio_offset / biosize; 473 on = uio->uio_offset & (biosize - 1); 474 475 /* 476 * Start the read ahead(s), as required. 477 */ 478 if (nmp->nm_readahead > 0) { 479 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount && 480 (off_t)(lbn + 1 + nra) * biosize < nsize; nra++) { 481 rabn = lbn + 1 + nra; 482 if (incore(&vp->v_bufobj, rabn) == NULL) { 483 rabp = nfs_getcacheblk(vp, rabn, biosize, td); 484 if (!rabp) { 485 error = nfs_sigintr(nmp, td); 486 return (error ? error : EINTR); 487 } 488 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 489 rabp->b_flags |= B_ASYNC; 490 rabp->b_iocmd = BIO_READ; 491 vfs_busy_pages(rabp, 0); 492 if (nfs_asyncio(nmp, rabp, cred, td)) { 493 rabp->b_flags |= B_INVAL; 494 rabp->b_ioflags |= BIO_ERROR; 495 vfs_unbusy_pages(rabp); 496 brelse(rabp); 497 break; 498 } 499 } else { 500 brelse(rabp); 501 } 502 } 503 } 504 } 505 506 /* Note that bcount is *not* DEV_BSIZE aligned. */ 507 bcount = biosize; 508 if ((off_t)lbn * biosize >= nsize) { 509 bcount = 0; 510 } else if ((off_t)(lbn + 1) * biosize > nsize) { 511 bcount = nsize - (off_t)lbn * biosize; 512 } 513 bp = nfs_getcacheblk(vp, lbn, bcount, td); 514 515 if (!bp) { 516 error = nfs_sigintr(nmp, td); 517 return (error ? error : EINTR); 518 } 519 520 /* 521 * If B_CACHE is not set, we must issue the read. If this 522 * fails, we return an error. 523 */ 524 525 if ((bp->b_flags & B_CACHE) == 0) { 526 bp->b_iocmd = BIO_READ; 527 vfs_busy_pages(bp, 0); 528 error = nfs_doio(vp, bp, cred, td); 529 if (error) { 530 brelse(bp); 531 return (error); 532 } 533 } 534 535 /* 536 * on is the offset into the current bp. Figure out how many 537 * bytes we can copy out of the bp. Note that bcount is 538 * NOT DEV_BSIZE aligned. 539 * 540 * Then figure out how many bytes we can copy into the uio. 541 */ 542 543 n = 0; 544 if (on < bcount) 545 n = MIN((unsigned)(bcount - on), uio->uio_resid); 546 break; 547 case VLNK: 548 nfsstats.biocache_readlinks++; 549 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td); 550 if (!bp) { 551 error = nfs_sigintr(nmp, td); 552 return (error ? error : EINTR); 553 } 554 if ((bp->b_flags & B_CACHE) == 0) { 555 bp->b_iocmd = BIO_READ; 556 vfs_busy_pages(bp, 0); 557 error = nfs_doio(vp, bp, cred, td); 558 if (error) { 559 bp->b_ioflags |= BIO_ERROR; 560 brelse(bp); 561 return (error); 562 } 563 } 564 n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid); 565 on = 0; 566 break; 567 case VDIR: 568 nfsstats.biocache_readdirs++; 569 if (np->n_direofoffset 570 && uio->uio_offset >= np->n_direofoffset) { 571 return (0); 572 } 573 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ; 574 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 575 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td); 576 if (!bp) { 577 error = nfs_sigintr(nmp, td); 578 return (error ? error : EINTR); 579 } 580 if ((bp->b_flags & B_CACHE) == 0) { 581 bp->b_iocmd = BIO_READ; 582 vfs_busy_pages(bp, 0); 583 error = nfs_doio(vp, bp, cred, td); 584 if (error) { 585 brelse(bp); 586 } 587 while (error == NFSERR_BAD_COOKIE) { 588 (nmp->nm_rpcops->nr_invaldir)(vp); 589 error = nfs_vinvalbuf(vp, 0, td, 1); 590 /* 591 * Yuck! The directory has been modified on the 592 * server. The only way to get the block is by 593 * reading from the beginning to get all the 594 * offset cookies. 595 * 596 * Leave the last bp intact unless there is an error. 597 * Loop back up to the while if the error is another 598 * NFSERR_BAD_COOKIE (double yuch!). 599 */ 600 for (i = 0; i <= lbn && !error; i++) { 601 if (np->n_direofoffset 602 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) 603 return (0); 604 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td); 605 if (!bp) { 606 error = nfs_sigintr(nmp, td); 607 return (error ? error : EINTR); 608 } 609 if ((bp->b_flags & B_CACHE) == 0) { 610 bp->b_iocmd = BIO_READ; 611 vfs_busy_pages(bp, 0); 612 error = nfs_doio(vp, bp, cred, td); 613 /* 614 * no error + B_INVAL == directory EOF, 615 * use the block. 616 */ 617 if (error == 0 && (bp->b_flags & B_INVAL)) 618 break; 619 } 620 /* 621 * An error will throw away the block and the 622 * for loop will break out. If no error and this 623 * is not the block we want, we throw away the 624 * block and go for the next one via the for loop. 625 */ 626 if (error || i < lbn) 627 brelse(bp); 628 } 629 } 630 /* 631 * The above while is repeated if we hit another cookie 632 * error. If we hit an error and it wasn't a cookie error, 633 * we give up. 634 */ 635 if (error) 636 return (error); 637 } 638 639 /* 640 * If not eof and read aheads are enabled, start one. 641 * (You need the current block first, so that you have the 642 * directory offset cookie of the next block.) 643 */ 644 if (nmp->nm_readahead > 0 && 645 (bp->b_flags & B_INVAL) == 0 && 646 (np->n_direofoffset == 0 || 647 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) && 648 incore(&vp->v_bufobj, lbn + 1) == NULL) { 649 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td); 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(nmp, rabp, cred, td)) { 656 rabp->b_flags |= B_INVAL; 657 rabp->b_ioflags |= BIO_ERROR; 658 vfs_unbusy_pages(rabp); 659 brelse(rabp); 660 } 661 } else { 662 brelse(rabp); 663 } 664 } 665 } 666 /* 667 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is 668 * chopped for the EOF condition, we cannot tell how large 669 * NFS directories are going to be until we hit EOF. So 670 * an NFS directory buffer is *not* chopped to its EOF. Now, 671 * it just so happens that b_resid will effectively chop it 672 * to EOF. *BUT* this information is lost if the buffer goes 673 * away and is reconstituted into a B_CACHE state ( due to 674 * being VMIO ) later. So we keep track of the directory eof 675 * in np->n_direofoffset and chop it off as an extra step 676 * right here. 677 */ 678 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on); 679 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset) 680 n = np->n_direofoffset - uio->uio_offset; 681 break; 682 default: 683 nfs_printf(" nfs_bioread: type %x unexpected\n", vp->v_type); 684 bp = NULL; 685 break; 686 }; 687 688 if (n > 0) { 689 error = uiomove(bp->b_data + on, (int)n, uio); 690 } 691 if (vp->v_type == VLNK) 692 n = 0; 693 if (bp != NULL) 694 brelse(bp); 695 } while (error == 0 && uio->uio_resid > 0 && n > 0); 696 return (error); 697} 698 699/* 700 * The NFS write path cannot handle iovecs with len > 1. So we need to 701 * break up iovecs accordingly (restricting them to wsize). 702 * For the SYNC case, we can do this with 1 copy (user buffer -> mbuf). 703 * For the ASYNC case, 2 copies are needed. The first a copy from the 704 * user buffer to a staging buffer and then a second copy from the staging 705 * buffer to mbufs. This can be optimized by copying from the user buffer 706 * directly into mbufs and passing the chain down, but that requires a 707 * fair amount of re-working of the relevant codepaths (and can be done 708 * later). 709 */ 710static int 711nfs_directio_write(vp, uiop, cred, ioflag) 712 struct vnode *vp; 713 struct uio *uiop; 714 struct ucred *cred; 715 int ioflag; 716{ 717 int error; 718 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 719 struct thread *td = uiop->uio_td; 720 int size; 721 int wsize; 722 723 mtx_lock(&nmp->nm_mtx); 724 wsize = nmp->nm_wsize; 725 mtx_unlock(&nmp->nm_mtx); 726 if (ioflag & IO_SYNC) { 727 int iomode, must_commit; 728 struct uio uio; 729 struct iovec iov; 730do_sync: 731 while (uiop->uio_resid > 0) { 732 size = MIN(uiop->uio_resid, wsize); 733 size = MIN(uiop->uio_iov->iov_len, size); 734 iov.iov_base = uiop->uio_iov->iov_base; 735 iov.iov_len = size; 736 uio.uio_iov = &iov; 737 uio.uio_iovcnt = 1; 738 uio.uio_offset = uiop->uio_offset; 739 uio.uio_resid = size; 740 uio.uio_segflg = UIO_USERSPACE; 741 uio.uio_rw = UIO_WRITE; 742 uio.uio_td = td; 743 iomode = NFSV3WRITE_FILESYNC; 744 error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred, 745 &iomode, &must_commit); 746 KASSERT((must_commit == 0), 747 ("nfs_directio_write: Did not commit write")); 748 if (error) 749 return (error); 750 uiop->uio_offset += size; 751 uiop->uio_resid -= size; 752 if (uiop->uio_iov->iov_len <= size) { 753 uiop->uio_iovcnt--; 754 uiop->uio_iov++; 755 } else { 756 uiop->uio_iov->iov_base = 757 (char *)uiop->uio_iov->iov_base + size; 758 uiop->uio_iov->iov_len -= size; 759 } 760 } 761 } else { 762 struct uio *t_uio; 763 struct iovec *t_iov; 764 struct buf *bp; 765 766 /* 767 * Break up the write into blocksize chunks and hand these 768 * over to nfsiod's for write back. 769 * Unfortunately, this incurs a copy of the data. Since 770 * the user could modify the buffer before the write is 771 * initiated. 772 * 773 * The obvious optimization here is that one of the 2 copies 774 * in the async write path can be eliminated by copying the 775 * data here directly into mbufs and passing the mbuf chain 776 * down. But that will require a fair amount of re-working 777 * of the code and can be done if there's enough interest 778 * in NFS directio access. 779 */ 780 while (uiop->uio_resid > 0) { 781 size = MIN(uiop->uio_resid, wsize); 782 size = MIN(uiop->uio_iov->iov_len, size); 783 bp = getpbuf(&nfs_pbuf_freecnt); 784 t_uio = malloc(sizeof(struct uio), M_NFSDIRECTIO, M_WAITOK); 785 t_iov = malloc(sizeof(struct iovec), M_NFSDIRECTIO, M_WAITOK); 786 t_iov->iov_base = malloc(size, M_NFSDIRECTIO, M_WAITOK); 787 t_iov->iov_len = size; 788 t_uio->uio_iov = t_iov; 789 t_uio->uio_iovcnt = 1; 790 t_uio->uio_offset = uiop->uio_offset; 791 t_uio->uio_resid = size; 792 t_uio->uio_segflg = UIO_SYSSPACE; 793 t_uio->uio_rw = UIO_WRITE; 794 t_uio->uio_td = td; 795 KASSERT(uiop->uio_segflg == UIO_USERSPACE || 796 uiop->uio_segflg == UIO_SYSSPACE, 797 ("nfs_directio_write: Bad uio_segflg")); 798 if (uiop->uio_segflg == UIO_USERSPACE) { 799 error = copyin(uiop->uio_iov->iov_base, 800 t_iov->iov_base, size); 801 if (error != 0) 802 goto err_free; 803 } else 804 /* 805 * UIO_SYSSPACE may never happen, but handle 806 * it just in case it does. 807 */ 808 bcopy(uiop->uio_iov->iov_base, t_iov->iov_base, 809 size); 810 bp->b_flags |= B_DIRECT; 811 bp->b_iocmd = BIO_WRITE; 812 if (cred != NOCRED) { 813 crhold(cred); 814 bp->b_wcred = cred; 815 } else 816 bp->b_wcred = NOCRED; 817 bp->b_caller1 = (void *)t_uio; 818 bp->b_vp = vp; 819 error = nfs_asyncio(nmp, bp, NOCRED, td); 820err_free: 821 if (error) { 822 free(t_iov->iov_base, M_NFSDIRECTIO); 823 free(t_iov, M_NFSDIRECTIO); 824 free(t_uio, M_NFSDIRECTIO); 825 bp->b_vp = NULL; 826 relpbuf(bp, &nfs_pbuf_freecnt); 827 if (error == EINTR) 828 return (error); 829 goto do_sync; 830 } 831 uiop->uio_offset += size; 832 uiop->uio_resid -= size; 833 if (uiop->uio_iov->iov_len <= size) { 834 uiop->uio_iovcnt--; 835 uiop->uio_iov++; 836 } else { 837 uiop->uio_iov->iov_base = 838 (char *)uiop->uio_iov->iov_base + size; 839 uiop->uio_iov->iov_len -= size; 840 } 841 } 842 } 843 return (0); 844} 845 846/* 847 * Vnode op for write using bio 848 */ 849int 850nfs_write(struct vop_write_args *ap) 851{ 852 int biosize; 853 struct uio *uio = ap->a_uio; 854 struct thread *td = uio->uio_td; 855 struct vnode *vp = ap->a_vp; 856 struct nfsnode *np = VTONFS(vp); 857 struct ucred *cred = ap->a_cred; 858 int ioflag = ap->a_ioflag; 859 struct buf *bp; 860 struct vattr vattr; 861 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 862 daddr_t lbn; 863 off_t end; 864 int bcount; 865 int n, on, error = 0; 866 867 KASSERT(uio->uio_rw == UIO_WRITE, ("nfs_write mode")); 868 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 869 ("nfs_write proc")); 870 if (vp->v_type != VREG) 871 return (EIO); 872 mtx_lock(&np->n_mtx); 873 if (np->n_flag & NWRITEERR) { 874 np->n_flag &= ~NWRITEERR; 875 mtx_unlock(&np->n_mtx); 876 return (np->n_error); 877 } else 878 mtx_unlock(&np->n_mtx); 879 mtx_lock(&nmp->nm_mtx); 880 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 881 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 882 mtx_unlock(&nmp->nm_mtx); 883 (void)nfs_fsinfo(nmp, vp, cred, td); 884 } else 885 mtx_unlock(&nmp->nm_mtx); 886 887 /* 888 * Synchronously flush pending buffers if we are in synchronous 889 * mode or if we are appending. 890 */ 891 if (ioflag & (IO_APPEND | IO_SYNC)) { 892 mtx_lock(&np->n_mtx); 893 if (np->n_flag & NMODIFIED) { 894 mtx_unlock(&np->n_mtx); 895#ifdef notyet /* Needs matching nonblock semantics elsewhere, too. */ 896 /* 897 * Require non-blocking, synchronous writes to 898 * dirty files to inform the program it needs 899 * to fsync(2) explicitly. 900 */ 901 if (ioflag & IO_NDELAY) 902 return (EAGAIN); 903#endif 904flush_and_restart: 905 np->n_attrstamp = 0; 906 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 907 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 908 if (error) 909 return (error); 910 } else 911 mtx_unlock(&np->n_mtx); 912 } 913 914 /* 915 * If IO_APPEND then load uio_offset. We restart here if we cannot 916 * get the append lock. 917 */ 918 if (ioflag & IO_APPEND) { 919 np->n_attrstamp = 0; 920 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 921 error = VOP_GETATTR(vp, &vattr, cred); 922 if (error) 923 return (error); 924 mtx_lock(&np->n_mtx); 925 uio->uio_offset = np->n_size; 926 mtx_unlock(&np->n_mtx); 927 } 928 929 if (uio->uio_offset < 0) 930 return (EINVAL); 931 end = uio->uio_offset + uio->uio_resid; 932 if (end > nmp->nm_maxfilesize || end < uio->uio_offset) 933 return (EFBIG); 934 if (uio->uio_resid == 0) 935 return (0); 936 937 if (nfs_directio_enable && (ioflag & IO_DIRECT) && vp->v_type == VREG) 938 return nfs_directio_write(vp, uio, cred, ioflag); 939 940 /* 941 * Maybe this should be above the vnode op call, but so long as 942 * file servers have no limits, i don't think it matters 943 */ 944 if (vn_rlimit_fsize(vp, uio, td)) 945 return (EFBIG); 946 947 biosize = vp->v_bufobj.bo_bsize; 948 /* 949 * Find all of this file's B_NEEDCOMMIT buffers. If our writes 950 * would exceed the local maximum per-file write commit size when 951 * combined with those, we must decide whether to flush, 952 * go synchronous, or return error. We don't bother checking 953 * IO_UNIT -- we just make all writes atomic anyway, as there's 954 * no point optimizing for something that really won't ever happen. 955 */ 956 if (!(ioflag & IO_SYNC)) { 957 int nflag; 958 959 mtx_lock(&np->n_mtx); 960 nflag = np->n_flag; 961 mtx_unlock(&np->n_mtx); 962 int needrestart = 0; 963 if (nmp->nm_wcommitsize < uio->uio_resid) { 964 /* 965 * If this request could not possibly be completed 966 * without exceeding the maximum outstanding write 967 * commit size, see if we can convert it into a 968 * synchronous write operation. 969 */ 970 if (ioflag & IO_NDELAY) 971 return (EAGAIN); 972 ioflag |= IO_SYNC; 973 if (nflag & NMODIFIED) 974 needrestart = 1; 975 } else if (nflag & NMODIFIED) { 976 int wouldcommit = 0; 977 BO_LOCK(&vp->v_bufobj); 978 if (vp->v_bufobj.bo_dirty.bv_cnt != 0) { 979 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, 980 b_bobufs) { 981 if (bp->b_flags & B_NEEDCOMMIT) 982 wouldcommit += bp->b_bcount; 983 } 984 } 985 BO_UNLOCK(&vp->v_bufobj); 986 /* 987 * Since we're not operating synchronously and 988 * bypassing the buffer cache, we are in a commit 989 * and holding all of these buffers whether 990 * transmitted or not. If not limited, this 991 * will lead to the buffer cache deadlocking, 992 * as no one else can flush our uncommitted buffers. 993 */ 994 wouldcommit += uio->uio_resid; 995 /* 996 * If we would initially exceed the maximum 997 * outstanding write commit size, flush and restart. 998 */ 999 if (wouldcommit > nmp->nm_wcommitsize) 1000 needrestart = 1; 1001 } 1002 if (needrestart) 1003 goto flush_and_restart; 1004 } 1005 1006 do { 1007 nfsstats.biocache_writes++; 1008 lbn = uio->uio_offset / biosize; 1009 on = uio->uio_offset & (biosize-1); 1010 n = MIN((unsigned)(biosize - on), uio->uio_resid); 1011again: 1012 /* 1013 * Handle direct append and file extension cases, calculate 1014 * unaligned buffer size. 1015 */ 1016 mtx_lock(&np->n_mtx); 1017 if (uio->uio_offset == np->n_size && n) { 1018 mtx_unlock(&np->n_mtx); 1019 /* 1020 * Get the buffer (in its pre-append state to maintain 1021 * B_CACHE if it was previously set). Resize the 1022 * nfsnode after we have locked the buffer to prevent 1023 * readers from reading garbage. 1024 */ 1025 bcount = on; 1026 bp = nfs_getcacheblk(vp, lbn, bcount, td); 1027 1028 if (bp != NULL) { 1029 long save; 1030 1031 mtx_lock(&np->n_mtx); 1032 np->n_size = uio->uio_offset + n; 1033 np->n_flag |= NMODIFIED; 1034 vnode_pager_setsize(vp, np->n_size); 1035 mtx_unlock(&np->n_mtx); 1036 1037 save = bp->b_flags & B_CACHE; 1038 bcount += n; 1039 allocbuf(bp, bcount); 1040 bp->b_flags |= save; 1041 } 1042 } else { 1043 /* 1044 * Obtain the locked cache block first, and then 1045 * adjust the file's size as appropriate. 1046 */ 1047 bcount = on + n; 1048 if ((off_t)lbn * biosize + bcount < np->n_size) { 1049 if ((off_t)(lbn + 1) * biosize < np->n_size) 1050 bcount = biosize; 1051 else 1052 bcount = np->n_size - (off_t)lbn * biosize; 1053 } 1054 mtx_unlock(&np->n_mtx); 1055 bp = nfs_getcacheblk(vp, lbn, bcount, td); 1056 mtx_lock(&np->n_mtx); 1057 if (uio->uio_offset + n > np->n_size) { 1058 np->n_size = uio->uio_offset + n; 1059 np->n_flag |= NMODIFIED; 1060 vnode_pager_setsize(vp, np->n_size); 1061 } 1062 mtx_unlock(&np->n_mtx); 1063 } 1064 1065 if (!bp) { 1066 error = nfs_sigintr(nmp, td); 1067 if (!error) 1068 error = EINTR; 1069 break; 1070 } 1071 1072 /* 1073 * Issue a READ if B_CACHE is not set. In special-append 1074 * mode, B_CACHE is based on the buffer prior to the write 1075 * op and is typically set, avoiding the read. If a read 1076 * is required in special append mode, the server will 1077 * probably send us a short-read since we extended the file 1078 * on our end, resulting in b_resid == 0 and, thusly, 1079 * B_CACHE getting set. 1080 * 1081 * We can also avoid issuing the read if the write covers 1082 * the entire buffer. We have to make sure the buffer state 1083 * is reasonable in this case since we will not be initiating 1084 * I/O. See the comments in kern/vfs_bio.c's getblk() for 1085 * more information. 1086 * 1087 * B_CACHE may also be set due to the buffer being cached 1088 * normally. 1089 */ 1090 1091 if (on == 0 && n == bcount) { 1092 bp->b_flags |= B_CACHE; 1093 bp->b_flags &= ~B_INVAL; 1094 bp->b_ioflags &= ~BIO_ERROR; 1095 } 1096 1097 if ((bp->b_flags & B_CACHE) == 0) { 1098 bp->b_iocmd = BIO_READ; 1099 vfs_busy_pages(bp, 0); 1100 error = nfs_doio(vp, bp, cred, td); 1101 if (error) { 1102 brelse(bp); 1103 break; 1104 } 1105 } 1106 if (bp->b_wcred == NOCRED) 1107 bp->b_wcred = crhold(cred); 1108 mtx_lock(&np->n_mtx); 1109 np->n_flag |= NMODIFIED; 1110 mtx_unlock(&np->n_mtx); 1111 1112 /* 1113 * If dirtyend exceeds file size, chop it down. This should 1114 * not normally occur but there is an append race where it 1115 * might occur XXX, so we log it. 1116 * 1117 * If the chopping creates a reverse-indexed or degenerate 1118 * situation with dirtyoff/end, we 0 both of them. 1119 */ 1120 1121 if (bp->b_dirtyend > bcount) { 1122 nfs_printf("NFS append race @%lx:%d\n", 1123 (long)bp->b_blkno * DEV_BSIZE, 1124 bp->b_dirtyend - bcount); 1125 bp->b_dirtyend = bcount; 1126 } 1127 1128 if (bp->b_dirtyoff >= bp->b_dirtyend) 1129 bp->b_dirtyoff = bp->b_dirtyend = 0; 1130 1131 /* 1132 * If the new write will leave a contiguous dirty 1133 * area, just update the b_dirtyoff and b_dirtyend, 1134 * otherwise force a write rpc of the old dirty area. 1135 * 1136 * While it is possible to merge discontiguous writes due to 1137 * our having a B_CACHE buffer ( and thus valid read data 1138 * for the hole), we don't because it could lead to 1139 * significant cache coherency problems with multiple clients, 1140 * especially if locking is implemented later on. 1141 * 1142 * as an optimization we could theoretically maintain 1143 * a linked list of discontinuous areas, but we would still 1144 * have to commit them separately so there isn't much 1145 * advantage to it except perhaps a bit of asynchronization. 1146 */ 1147 1148 if (bp->b_dirtyend > 0 && 1149 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 1150 if (bwrite(bp) == EINTR) { 1151 error = EINTR; 1152 break; 1153 } 1154 goto again; 1155 } 1156 1157 error = uiomove((char *)bp->b_data + on, n, uio); 1158 1159 /* 1160 * Since this block is being modified, it must be written 1161 * again and not just committed. Since write clustering does 1162 * not work for the stage 1 data write, only the stage 2 1163 * commit rpc, we have to clear B_CLUSTEROK as well. 1164 */ 1165 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1166 1167 if (error) { 1168 bp->b_ioflags |= BIO_ERROR; 1169 brelse(bp); 1170 break; 1171 } 1172 1173 /* 1174 * Only update dirtyoff/dirtyend if not a degenerate 1175 * condition. 1176 */ 1177 if (n) { 1178 if (bp->b_dirtyend > 0) { 1179 bp->b_dirtyoff = min(on, bp->b_dirtyoff); 1180 bp->b_dirtyend = max((on + n), bp->b_dirtyend); 1181 } else { 1182 bp->b_dirtyoff = on; 1183 bp->b_dirtyend = on + n; 1184 } 1185 vfs_bio_set_valid(bp, on, n); 1186 } 1187 1188 /* 1189 * If IO_SYNC do bwrite(). 1190 * 1191 * IO_INVAL appears to be unused. The idea appears to be 1192 * to turn off caching in this case. Very odd. XXX 1193 */ 1194 if ((ioflag & IO_SYNC)) { 1195 if (ioflag & IO_INVAL) 1196 bp->b_flags |= B_NOCACHE; 1197 error = bwrite(bp); 1198 if (error) 1199 break; 1200 } else if ((n + on) == biosize) { 1201 bp->b_flags |= B_ASYNC; 1202 (void) (nmp->nm_rpcops->nr_writebp)(bp, 0, NULL); 1203 } else { 1204 bdwrite(bp); 1205 } 1206 } while (uio->uio_resid > 0 && n > 0); 1207 1208 return (error); 1209} 1210 1211/* 1212 * Get an nfs cache block. 1213 * 1214 * Allocate a new one if the block isn't currently in the cache 1215 * and return the block marked busy. If the calling process is 1216 * interrupted by a signal for an interruptible mount point, return 1217 * NULL. 1218 * 1219 * The caller must carefully deal with the possible B_INVAL state of 1220 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it 1221 * indirectly), so synchronous reads can be issued without worrying about 1222 * the B_INVAL state. We have to be a little more careful when dealing 1223 * with writes (see comments in nfs_write()) when extending a file past 1224 * its EOF. 1225 */ 1226static struct buf * 1227nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td) 1228{ 1229 struct buf *bp; 1230 struct mount *mp; 1231 struct nfsmount *nmp; 1232 1233 mp = vp->v_mount; 1234 nmp = VFSTONFS(mp); 1235 1236 if (nmp->nm_flag & NFSMNT_INT) { 1237 sigset_t oldset; 1238 1239 nfs_set_sigmask(td, &oldset); 1240 bp = getblk(vp, bn, size, NFS_PCATCH, 0, 0); 1241 nfs_restore_sigmask(td, &oldset); 1242 while (bp == NULL) { 1243 if (nfs_sigintr(nmp, td)) 1244 return (NULL); 1245 bp = getblk(vp, bn, size, 0, 2 * hz, 0); 1246 } 1247 } else { 1248 bp = getblk(vp, bn, size, 0, 0, 0); 1249 } 1250 1251 if (vp->v_type == VREG) 1252 bp->b_blkno = bn * (vp->v_bufobj.bo_bsize / DEV_BSIZE); 1253 return (bp); 1254} 1255 1256/* 1257 * Flush and invalidate all dirty buffers. If another process is already 1258 * doing the flush, just wait for completion. 1259 */ 1260int 1261nfs_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg) 1262{ 1263 struct nfsnode *np = VTONFS(vp); 1264 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1265 int error = 0, slpflag, slptimeo; 1266 int old_lock = 0; 1267 1268 ASSERT_VOP_LOCKED(vp, "nfs_vinvalbuf"); 1269 1270 if ((nmp->nm_flag & NFSMNT_INT) == 0) 1271 intrflg = 0; 1272 if (intrflg) { 1273 slpflag = NFS_PCATCH; 1274 slptimeo = 2 * hz; 1275 } else { 1276 slpflag = 0; 1277 slptimeo = 0; 1278 } 1279 1280 old_lock = nfs_upgrade_vnlock(vp); 1281 if (vp->v_iflag & VI_DOOMED) { 1282 /* 1283 * Since vgonel() uses the generic vinvalbuf() to flush 1284 * dirty buffers and it does not call this function, it 1285 * is safe to just return OK when VI_DOOMED is set. 1286 */ 1287 nfs_downgrade_vnlock(vp, old_lock); 1288 return (0); 1289 } 1290 1291 /* 1292 * Now, flush as required. 1293 */ 1294 if ((flags & V_SAVE) && (vp->v_bufobj.bo_object != NULL)) { 1295 VM_OBJECT_LOCK(vp->v_bufobj.bo_object); 1296 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC); 1297 VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object); 1298 /* 1299 * If the page clean was interrupted, fail the invalidation. 1300 * Not doing so, we run the risk of losing dirty pages in the 1301 * vinvalbuf() call below. 1302 */ 1303 if (intrflg && (error = nfs_sigintr(nmp, td))) 1304 goto out; 1305 } 1306 1307 error = vinvalbuf(vp, flags, slpflag, 0); 1308 while (error) { 1309 if (intrflg && (error = nfs_sigintr(nmp, td))) 1310 goto out; 1311 error = vinvalbuf(vp, flags, 0, slptimeo); 1312 } 1313 mtx_lock(&np->n_mtx); 1314 if (np->n_directio_asyncwr == 0) 1315 np->n_flag &= ~NMODIFIED; 1316 mtx_unlock(&np->n_mtx); 1317out: 1318 nfs_downgrade_vnlock(vp, old_lock); 1319 return error; 1320} 1321 1322/* 1323 * Initiate asynchronous I/O. Return an error if no nfsiods are available. 1324 * This is mainly to avoid queueing async I/O requests when the nfsiods 1325 * are all hung on a dead server. 1326 * 1327 * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp 1328 * is eventually dequeued by the async daemon, nfs_doio() *will*. 1329 */ 1330int 1331nfs_asyncio(struct nfsmount *nmp, struct buf *bp, struct ucred *cred, struct thread *td) 1332{ 1333 int iod; 1334 int gotiod; 1335 int slpflag = 0; 1336 int slptimeo = 0; 1337 int error, error2; 1338 1339 /* 1340 * Commits are usually short and sweet so lets save some cpu and 1341 * leave the async daemons for more important rpc's (such as reads 1342 * and writes). 1343 */ 1344 mtx_lock(&nfs_iod_mtx); 1345 if (bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) && 1346 (nmp->nm_bufqiods > nfs_numasync / 2)) { 1347 mtx_unlock(&nfs_iod_mtx); 1348 return(EIO); 1349 } 1350again: 1351 if (nmp->nm_flag & NFSMNT_INT) 1352 slpflag = NFS_PCATCH; 1353 gotiod = FALSE; 1354 1355 /* 1356 * Find a free iod to process this request. 1357 */ 1358 for (iod = 0; iod < nfs_numasync; iod++) 1359 if (nfs_iodwant[iod] == NFSIOD_AVAILABLE) { 1360 gotiod = TRUE; 1361 break; 1362 } 1363 1364 /* 1365 * Try to create one if none are free. 1366 */ 1367 if (!gotiod) 1368 nfs_nfsiodnew(); 1369 else { 1370 /* 1371 * Found one, so wake it up and tell it which 1372 * mount to process. 1373 */ 1374 NFS_DPF(ASYNCIO, ("nfs_asyncio: waking iod %d for mount %p\n", 1375 iod, nmp)); 1376 nfs_iodwant[iod] = NFSIOD_NOT_AVAILABLE; 1377 nfs_iodmount[iod] = nmp; 1378 nmp->nm_bufqiods++; 1379 wakeup(&nfs_iodwant[iod]); 1380 } 1381 1382 /* 1383 * If none are free, we may already have an iod working on this mount 1384 * point. If so, it will process our request. 1385 */ 1386 if (!gotiod) { 1387 if (nmp->nm_bufqiods > 0) { 1388 NFS_DPF(ASYNCIO, 1389 ("nfs_asyncio: %d iods are already processing mount %p\n", 1390 nmp->nm_bufqiods, nmp)); 1391 gotiod = TRUE; 1392 } 1393 } 1394 1395 /* 1396 * If we have an iod which can process the request, then queue 1397 * the buffer. 1398 */ 1399 if (gotiod) { 1400 /* 1401 * Ensure that the queue never grows too large. We still want 1402 * to asynchronize so we block rather then return EIO. 1403 */ 1404 while (nmp->nm_bufqlen >= 2 * nfs_numasync) { 1405 NFS_DPF(ASYNCIO, 1406 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp)); 1407 nmp->nm_bufqwant = TRUE; 1408 error = nfs_msleep(td, &nmp->nm_bufq, &nfs_iod_mtx, 1409 slpflag | PRIBIO, 1410 "nfsaio", slptimeo); 1411 if (error) { 1412 error2 = nfs_sigintr(nmp, td); 1413 if (error2) { 1414 mtx_unlock(&nfs_iod_mtx); 1415 return (error2); 1416 } 1417 if (slpflag == NFS_PCATCH) { 1418 slpflag = 0; 1419 slptimeo = 2 * hz; 1420 } 1421 } 1422 /* 1423 * We might have lost our iod while sleeping, 1424 * so check and loop if nescessary. 1425 */ 1426 goto again; 1427 } 1428 1429 /* We might have lost our nfsiod */ 1430 if (nmp->nm_bufqiods == 0) { 1431 NFS_DPF(ASYNCIO, 1432("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 1433 goto again; 1434 } 1435 1436 if (bp->b_iocmd == BIO_READ) { 1437 if (bp->b_rcred == NOCRED && cred != NOCRED) 1438 bp->b_rcred = crhold(cred); 1439 } else { 1440 if (bp->b_wcred == NOCRED && cred != NOCRED) 1441 bp->b_wcred = crhold(cred); 1442 } 1443 1444 if (bp->b_flags & B_REMFREE) 1445 bremfreef(bp); 1446 BUF_KERNPROC(bp); 1447 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 1448 nmp->nm_bufqlen++; 1449 if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) { 1450 mtx_lock(&(VTONFS(bp->b_vp))->n_mtx); 1451 VTONFS(bp->b_vp)->n_flag |= NMODIFIED; 1452 VTONFS(bp->b_vp)->n_directio_asyncwr++; 1453 mtx_unlock(&(VTONFS(bp->b_vp))->n_mtx); 1454 } 1455 mtx_unlock(&nfs_iod_mtx); 1456 return (0); 1457 } 1458 1459 mtx_unlock(&nfs_iod_mtx); 1460 1461 /* 1462 * All the iods are busy on other mounts, so return EIO to 1463 * force the caller to process the i/o synchronously. 1464 */ 1465 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n")); 1466 return (EIO); 1467} 1468 1469void 1470nfs_doio_directwrite(struct buf *bp) 1471{ 1472 int iomode, must_commit; 1473 struct uio *uiop = (struct uio *)bp->b_caller1; 1474 char *iov_base = uiop->uio_iov->iov_base; 1475 struct nfsmount *nmp = VFSTONFS(bp->b_vp->v_mount); 1476 1477 iomode = NFSV3WRITE_FILESYNC; 1478 uiop->uio_td = NULL; /* NULL since we're in nfsiod */ 1479 (nmp->nm_rpcops->nr_writerpc)(bp->b_vp, uiop, bp->b_wcred, &iomode, &must_commit); 1480 KASSERT((must_commit == 0), ("nfs_doio_directwrite: Did not commit write")); 1481 free(iov_base, M_NFSDIRECTIO); 1482 free(uiop->uio_iov, M_NFSDIRECTIO); 1483 free(uiop, M_NFSDIRECTIO); 1484 if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) { 1485 struct nfsnode *np = VTONFS(bp->b_vp); 1486 mtx_lock(&np->n_mtx); 1487 np->n_directio_asyncwr--; 1488 if (np->n_directio_asyncwr == 0) { 1489 VTONFS(bp->b_vp)->n_flag &= ~NMODIFIED; 1490 if ((np->n_flag & NFSYNCWAIT)) { 1491 np->n_flag &= ~NFSYNCWAIT; 1492 wakeup((caddr_t)&np->n_directio_asyncwr); 1493 } 1494 } 1495 mtx_unlock(&np->n_mtx); 1496 } 1497 bp->b_vp = NULL; 1498 relpbuf(bp, &nfs_pbuf_freecnt); 1499} 1500 1501/* 1502 * Do an I/O operation to/from a cache block. This may be called 1503 * synchronously or from an nfsiod. 1504 */ 1505int 1506nfs_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td) 1507{ 1508 struct uio *uiop; 1509 struct nfsnode *np; 1510 struct nfsmount *nmp; 1511 int error = 0, iomode, must_commit = 0; 1512 struct uio uio; 1513 struct iovec io; 1514 struct proc *p = td ? td->td_proc : NULL; 1515 uint8_t iocmd; 1516 1517 np = VTONFS(vp); 1518 nmp = VFSTONFS(vp->v_mount); 1519 uiop = &uio; 1520 uiop->uio_iov = &io; 1521 uiop->uio_iovcnt = 1; 1522 uiop->uio_segflg = UIO_SYSSPACE; 1523 uiop->uio_td = td; 1524 1525 /* 1526 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We 1527 * do this here so we do not have to do it in all the code that 1528 * calls us. 1529 */ 1530 bp->b_flags &= ~B_INVAL; 1531 bp->b_ioflags &= ~BIO_ERROR; 1532 1533 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp)); 1534 iocmd = bp->b_iocmd; 1535 if (iocmd == BIO_READ) { 1536 io.iov_len = uiop->uio_resid = bp->b_bcount; 1537 io.iov_base = bp->b_data; 1538 uiop->uio_rw = UIO_READ; 1539 1540 switch (vp->v_type) { 1541 case VREG: 1542 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1543 nfsstats.read_bios++; 1544 error = (nmp->nm_rpcops->nr_readrpc)(vp, uiop, cr); 1545 1546 if (!error) { 1547 if (uiop->uio_resid) { 1548 /* 1549 * If we had a short read with no error, we must have 1550 * hit a file hole. We should zero-fill the remainder. 1551 * This can also occur if the server hits the file EOF. 1552 * 1553 * Holes used to be able to occur due to pending 1554 * writes, but that is not possible any longer. 1555 */ 1556 int nread = bp->b_bcount - uiop->uio_resid; 1557 int left = uiop->uio_resid; 1558 1559 if (left > 0) 1560 bzero((char *)bp->b_data + nread, left); 1561 uiop->uio_resid = 0; 1562 } 1563 } 1564 /* ASSERT_VOP_LOCKED(vp, "nfs_doio"); */ 1565 if (p && (vp->v_vflag & VV_TEXT)) { 1566 mtx_lock(&np->n_mtx); 1567 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &np->n_vattr.va_mtime)) { 1568 mtx_unlock(&np->n_mtx); 1569 PROC_LOCK(p); 1570 killproc(p, "text file modification"); 1571 PROC_UNLOCK(p); 1572 } else 1573 mtx_unlock(&np->n_mtx); 1574 } 1575 break; 1576 case VLNK: 1577 uiop->uio_offset = (off_t)0; 1578 nfsstats.readlink_bios++; 1579 error = (nmp->nm_rpcops->nr_readlinkrpc)(vp, uiop, cr); 1580 break; 1581 case VDIR: 1582 nfsstats.readdir_bios++; 1583 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 1584 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) != 0) { 1585 error = nfs_readdirplusrpc(vp, uiop, cr); 1586 if (error == NFSERR_NOTSUPP) 1587 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 1588 } 1589 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 1590 error = nfs_readdirrpc(vp, uiop, cr); 1591 /* 1592 * end-of-directory sets B_INVAL but does not generate an 1593 * error. 1594 */ 1595 if (error == 0 && uiop->uio_resid == bp->b_bcount) 1596 bp->b_flags |= B_INVAL; 1597 break; 1598 default: 1599 nfs_printf("nfs_doio: type %x unexpected\n", vp->v_type); 1600 break; 1601 }; 1602 if (error) { 1603 bp->b_ioflags |= BIO_ERROR; 1604 bp->b_error = error; 1605 } 1606 } else { 1607 /* 1608 * If we only need to commit, try to commit 1609 */ 1610 if (bp->b_flags & B_NEEDCOMMIT) { 1611 int retv; 1612 off_t off; 1613 1614 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff; 1615 retv = (nmp->nm_rpcops->nr_commit)( 1616 vp, off, bp->b_dirtyend-bp->b_dirtyoff, 1617 bp->b_wcred, td); 1618 if (retv == 0) { 1619 bp->b_dirtyoff = bp->b_dirtyend = 0; 1620 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1621 bp->b_resid = 0; 1622 bufdone(bp); 1623 return (0); 1624 } 1625 if (retv == NFSERR_STALEWRITEVERF) { 1626 nfs_clearcommit(vp->v_mount); 1627 } 1628 } 1629 1630 /* 1631 * Setup for actual write 1632 */ 1633 mtx_lock(&np->n_mtx); 1634 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size) 1635 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE; 1636 mtx_unlock(&np->n_mtx); 1637 1638 if (bp->b_dirtyend > bp->b_dirtyoff) { 1639 io.iov_len = uiop->uio_resid = bp->b_dirtyend 1640 - bp->b_dirtyoff; 1641 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE 1642 + bp->b_dirtyoff; 1643 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 1644 uiop->uio_rw = UIO_WRITE; 1645 nfsstats.write_bios++; 1646 1647 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 1648 iomode = NFSV3WRITE_UNSTABLE; 1649 else 1650 iomode = NFSV3WRITE_FILESYNC; 1651 1652 error = (nmp->nm_rpcops->nr_writerpc)(vp, uiop, cr, &iomode, &must_commit); 1653 1654 /* 1655 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try 1656 * to cluster the buffers needing commit. This will allow 1657 * the system to submit a single commit rpc for the whole 1658 * cluster. We can do this even if the buffer is not 100% 1659 * dirty (relative to the NFS blocksize), so we optimize the 1660 * append-to-file-case. 1661 * 1662 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be 1663 * cleared because write clustering only works for commit 1664 * rpc's, not for the data portion of the write). 1665 */ 1666 1667 if (!error && iomode == NFSV3WRITE_UNSTABLE) { 1668 bp->b_flags |= B_NEEDCOMMIT; 1669 if (bp->b_dirtyoff == 0 1670 && bp->b_dirtyend == bp->b_bcount) 1671 bp->b_flags |= B_CLUSTEROK; 1672 } else { 1673 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1674 } 1675 1676 /* 1677 * For an interrupted write, the buffer is still valid 1678 * and the write hasn't been pushed to the server yet, 1679 * so we can't set BIO_ERROR and report the interruption 1680 * by setting B_EINTR. For the B_ASYNC case, B_EINTR 1681 * is not relevant, so the rpc attempt is essentially 1682 * a noop. For the case of a V3 write rpc not being 1683 * committed to stable storage, the block is still 1684 * dirty and requires either a commit rpc or another 1685 * write rpc with iomode == NFSV3WRITE_FILESYNC before 1686 * the block is reused. This is indicated by setting 1687 * the B_DELWRI and B_NEEDCOMMIT flags. 1688 * 1689 * If the buffer is marked B_PAGING, it does not reside on 1690 * the vp's paging queues so we cannot call bdirty(). The 1691 * bp in this case is not an NFS cache block so we should 1692 * be safe. XXX 1693 * 1694 * The logic below breaks up errors into recoverable and 1695 * unrecoverable. For the former, we clear B_INVAL|B_NOCACHE 1696 * and keep the buffer around for potential write retries. 1697 * For the latter (eg ESTALE), we toss the buffer away (B_INVAL) 1698 * and save the error in the nfsnode. This is less than ideal 1699 * but necessary. Keeping such buffers around could potentially 1700 * cause buffer exhaustion eventually (they can never be written 1701 * out, so will get constantly be re-dirtied). It also causes 1702 * all sorts of vfs panics. For non-recoverable write errors, 1703 * also invalidate the attrcache, so we'll be forced to go over 1704 * the wire for this object, returning an error to user on next 1705 * call (most of the time). 1706 */ 1707 if (error == EINTR || error == EIO || error == ETIMEDOUT 1708 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 1709 int s; 1710 1711 s = splbio(); 1712 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 1713 if ((bp->b_flags & B_PAGING) == 0) { 1714 bdirty(bp); 1715 bp->b_flags &= ~B_DONE; 1716 } 1717 if (error && (bp->b_flags & B_ASYNC) == 0) 1718 bp->b_flags |= B_EINTR; 1719 splx(s); 1720 } else { 1721 if (error) { 1722 bp->b_ioflags |= BIO_ERROR; 1723 bp->b_flags |= B_INVAL; 1724 bp->b_error = np->n_error = error; 1725 mtx_lock(&np->n_mtx); 1726 np->n_flag |= NWRITEERR; 1727 np->n_attrstamp = 0; 1728 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 1729 mtx_unlock(&np->n_mtx); 1730 } 1731 bp->b_dirtyoff = bp->b_dirtyend = 0; 1732 } 1733 } else { 1734 bp->b_resid = 0; 1735 bufdone(bp); 1736 return (0); 1737 } 1738 } 1739 bp->b_resid = uiop->uio_resid; 1740 if (must_commit) 1741 nfs_clearcommit(vp->v_mount); 1742 bufdone(bp); 1743 return (error); 1744} 1745 1746/* 1747 * Used to aid in handling ftruncate() operations on the NFS client side. 1748 * Truncation creates a number of special problems for NFS. We have to 1749 * throw away VM pages and buffer cache buffers that are beyond EOF, and 1750 * we have to properly handle VM pages or (potentially dirty) buffers 1751 * that straddle the truncation point. 1752 */ 1753 1754int 1755nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize) 1756{ 1757 struct nfsnode *np = VTONFS(vp); 1758 u_quad_t tsize; 1759 int biosize = vp->v_bufobj.bo_bsize; 1760 int error = 0; 1761 1762 mtx_lock(&np->n_mtx); 1763 tsize = np->n_size; 1764 np->n_size = nsize; 1765 mtx_unlock(&np->n_mtx); 1766 1767 if (nsize < tsize) { 1768 struct buf *bp; 1769 daddr_t lbn; 1770 int bufsize; 1771 1772 /* 1773 * vtruncbuf() doesn't get the buffer overlapping the 1774 * truncation point. We may have a B_DELWRI and/or B_CACHE 1775 * buffer that now needs to be truncated. 1776 */ 1777 error = vtruncbuf(vp, cred, nsize, biosize); 1778 lbn = nsize / biosize; 1779 bufsize = nsize & (biosize - 1); 1780 bp = nfs_getcacheblk(vp, lbn, bufsize, td); 1781 if (!bp) 1782 return EINTR; 1783 if (bp->b_dirtyoff > bp->b_bcount) 1784 bp->b_dirtyoff = bp->b_bcount; 1785 if (bp->b_dirtyend > bp->b_bcount) 1786 bp->b_dirtyend = bp->b_bcount; 1787 bp->b_flags |= B_RELBUF; /* don't leave garbage around */ 1788 brelse(bp); 1789 } else { 1790 vnode_pager_setsize(vp, nsize); 1791 } 1792 return(error); 1793} 1794 1795