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