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