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 exclusive 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 static bool 418 ncl_bioread_dora(struct vnode *vp) 419 { 420 vm_object_t obj; 421 422 obj = vp->v_object; 423 if (obj == NULL) 424 return (true); 425 return (!vm_object_mightbedirty(vp->v_object) && 426 vp->v_object->un_pager.vnp.writemappings == 0); 427 } 428 429 /* 430 * Vnode op for read using bio 431 */ 432 int 433 ncl_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred) 434 { 435 struct nfsnode *np = VTONFS(vp); 436 struct buf *bp, *rabp; 437 struct thread *td; 438 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 439 daddr_t lbn, rabn; 440 int biosize, bcount, error, i, n, nra, on, save2, seqcount; 441 off_t tmp_off; 442 443 KASSERT(uio->uio_rw == UIO_READ, ("ncl_read mode")); 444 if (uio->uio_resid == 0) 445 return (0); 446 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */ 447 return (EINVAL); 448 td = uio->uio_td; 449 450 mtx_lock(&nmp->nm_mtx); 451 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 452 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 453 mtx_unlock(&nmp->nm_mtx); 454 (void)ncl_fsinfo(nmp, vp, cred, td); 455 mtx_lock(&nmp->nm_mtx); 456 } 457 if (nmp->nm_rsize == 0 || nmp->nm_readdirsize == 0) 458 (void) newnfs_iosize(nmp); 459 460 tmp_off = uio->uio_offset + uio->uio_resid; 461 if (vp->v_type != VDIR && 462 (tmp_off > nmp->nm_maxfilesize || tmp_off < uio->uio_offset)) { 463 mtx_unlock(&nmp->nm_mtx); 464 return (EFBIG); 465 } 466 mtx_unlock(&nmp->nm_mtx); 467 468 if (newnfs_directio_enable && (ioflag & IO_DIRECT) && (vp->v_type == VREG)) 469 /* No caching/ no readaheads. Just read data into the user buffer */ 470 return ncl_readrpc(vp, uio, cred); 471 472 n = 0; 473 on = 0; 474 biosize = vp->v_bufobj.bo_bsize; 475 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE); 476 477 error = nfs_bioread_check_cons(vp, td, cred); 478 if (error) 479 return error; 480 481 save2 = curthread_pflags2_set(TDP2_SBPAGES); 482 do { 483 u_quad_t nsize; 484 485 NFSLOCKNODE(np); 486 nsize = np->n_size; 487 NFSUNLOCKNODE(np); 488 489 switch (vp->v_type) { 490 case VREG: 491 NFSINCRGLOBAL(nfsstatsv1.biocache_reads); 492 lbn = uio->uio_offset / biosize; 493 on = uio->uio_offset - (lbn * biosize); 494 495 /* 496 * Start the read ahead(s), as required. Do not do 497 * read-ahead if there are writeable mappings, since 498 * unlocked read by nfsiod could obliterate changes 499 * done by userspace. 500 */ 501 if (nmp->nm_readahead > 0 && ncl_bioread_dora(vp)) { 502 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount && 503 (off_t)(lbn + 1 + nra) * biosize < nsize; nra++) { 504 rabn = lbn + 1 + nra; 505 if (incore(&vp->v_bufobj, rabn) == NULL) { 506 rabp = nfs_getcacheblk(vp, rabn, biosize, td); 507 if (!rabp) { 508 error = newnfs_sigintr(nmp, td); 509 if (error == 0) 510 error = EINTR; 511 goto out; 512 } 513 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 514 rabp->b_flags |= B_ASYNC; 515 rabp->b_iocmd = BIO_READ; 516 vfs_busy_pages(rabp, 0); 517 if (ncl_asyncio(nmp, rabp, cred, td)) { 518 rabp->b_flags |= B_INVAL; 519 rabp->b_ioflags |= BIO_ERROR; 520 vfs_unbusy_pages(rabp); 521 brelse(rabp); 522 break; 523 } 524 } else { 525 brelse(rabp); 526 } 527 } 528 } 529 } 530 531 /* Note that bcount is *not* DEV_BSIZE aligned. */ 532 bcount = biosize; 533 if ((off_t)lbn * biosize >= nsize) { 534 bcount = 0; 535 } else if ((off_t)(lbn + 1) * biosize > nsize) { 536 bcount = nsize - (off_t)lbn * biosize; 537 } 538 bp = nfs_getcacheblk(vp, lbn, bcount, td); 539 540 if (!bp) { 541 error = newnfs_sigintr(nmp, td); 542 if (error == 0) 543 error = EINTR; 544 goto out; 545 } 546 547 /* 548 * If B_CACHE is not set, we must issue the read. If this 549 * fails, we return an error. 550 */ 551 552 if ((bp->b_flags & B_CACHE) == 0) { 553 bp->b_iocmd = BIO_READ; 554 vfs_busy_pages(bp, 0); 555 error = ncl_doio(vp, bp, cred, td, 0); 556 if (error) { 557 brelse(bp); 558 goto out; 559 } 560 } 561 562 /* 563 * on is the offset into the current bp. Figure out how many 564 * bytes we can copy out of the bp. Note that bcount is 565 * NOT DEV_BSIZE aligned. 566 * 567 * Then figure out how many bytes we can copy into the uio. 568 */ 569 570 n = 0; 571 if (on < bcount) 572 n = MIN((unsigned)(bcount - on), uio->uio_resid); 573 break; 574 case VLNK: 575 NFSINCRGLOBAL(nfsstatsv1.biocache_readlinks); 576 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td); 577 if (!bp) { 578 error = newnfs_sigintr(nmp, td); 579 if (error == 0) 580 error = EINTR; 581 goto out; 582 } 583 if ((bp->b_flags & B_CACHE) == 0) { 584 bp->b_iocmd = BIO_READ; 585 vfs_busy_pages(bp, 0); 586 error = ncl_doio(vp, bp, cred, td, 0); 587 if (error) { 588 bp->b_ioflags |= BIO_ERROR; 589 brelse(bp); 590 goto out; 591 } 592 } 593 n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid); 594 on = 0; 595 break; 596 case VDIR: 597 NFSINCRGLOBAL(nfsstatsv1.biocache_readdirs); 598 NFSLOCKNODE(np); 599 if (np->n_direofoffset 600 && uio->uio_offset >= np->n_direofoffset) { 601 NFSUNLOCKNODE(np); 602 error = 0; 603 goto out; 604 } 605 NFSUNLOCKNODE(np); 606 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ; 607 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 608 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td); 609 if (!bp) { 610 error = newnfs_sigintr(nmp, td); 611 if (error == 0) 612 error = EINTR; 613 goto out; 614 } 615 if ((bp->b_flags & B_CACHE) == 0) { 616 bp->b_iocmd = BIO_READ; 617 vfs_busy_pages(bp, 0); 618 error = ncl_doio(vp, bp, cred, td, 0); 619 if (error) { 620 brelse(bp); 621 } 622 while (error == NFSERR_BAD_COOKIE) { 623 ncl_invaldir(vp); 624 error = ncl_vinvalbuf(vp, 0, td, 1); 625 626 /* 627 * Yuck! The directory has been modified on the 628 * server. The only way to get the block is by 629 * reading from the beginning to get all the 630 * offset cookies. 631 * 632 * Leave the last bp intact unless there is an error. 633 * Loop back up to the while if the error is another 634 * NFSERR_BAD_COOKIE (double yuch!). 635 */ 636 for (i = 0; i <= lbn && !error; i++) { 637 NFSLOCKNODE(np); 638 if (np->n_direofoffset 639 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) { 640 NFSUNLOCKNODE(np); 641 error = 0; 642 goto out; 643 } 644 NFSUNLOCKNODE(np); 645 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td); 646 if (!bp) { 647 error = newnfs_sigintr(nmp, td); 648 if (error == 0) 649 error = EINTR; 650 goto out; 651 } 652 if ((bp->b_flags & B_CACHE) == 0) { 653 bp->b_iocmd = BIO_READ; 654 vfs_busy_pages(bp, 0); 655 error = ncl_doio(vp, bp, cred, td, 0); 656 /* 657 * no error + B_INVAL == directory EOF, 658 * use the block. 659 */ 660 if (error == 0 && (bp->b_flags & B_INVAL)) 661 break; 662 } 663 /* 664 * An error will throw away the block and the 665 * for loop will break out. If no error and this 666 * is not the block we want, we throw away the 667 * block and go for the next one via the for loop. 668 */ 669 if (error || i < lbn) 670 brelse(bp); 671 } 672 } 673 /* 674 * The above while is repeated if we hit another cookie 675 * error. If we hit an error and it wasn't a cookie error, 676 * we give up. 677 */ 678 if (error) 679 goto out; 680 } 681 682 /* 683 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is 684 * chopped for the EOF condition, we cannot tell how large 685 * NFS directories are going to be until we hit EOF. So 686 * an NFS directory buffer is *not* chopped to its EOF. Now, 687 * it just so happens that b_resid will effectively chop it 688 * to EOF. *BUT* this information is lost if the buffer goes 689 * away and is reconstituted into a B_CACHE state ( due to 690 * being VMIO ) later. So we keep track of the directory eof 691 * in np->n_direofoffset and chop it off as an extra step 692 * right here. 693 */ 694 NFSLOCKNODE(np); 695 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on); 696 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset) 697 n = np->n_direofoffset - uio->uio_offset; 698 NFSUNLOCKNODE(np); 699 break; 700 default: 701 printf(" ncl_bioread: type %x unexpected\n", vp->v_type); 702 bp = NULL; 703 break; 704 } 705 706 if (n > 0) { 707 error = vn_io_fault_uiomove(bp->b_data + on, (int)n, uio); 708 } 709 if (vp->v_type == VLNK) 710 n = 0; 711 if (bp != NULL) 712 brelse(bp); 713 } while (error == 0 && uio->uio_resid > 0 && n > 0); 714 out: 715 curthread_pflags2_restore(save2); 716 if ((curthread->td_pflags2 & TDP2_SBPAGES) == 0) { 717 NFSLOCKNODE(np); 718 ncl_pager_setsize(vp, NULL); 719 } 720 return (error); 721 } 722 723 /* 724 * The NFS write path cannot handle iovecs with len > 1. So we need to 725 * break up iovecs accordingly (restricting them to wsize). 726 * For the SYNC case, we can do this with 1 copy (user buffer -> mbuf). 727 * For the ASYNC case, 2 copies are needed. The first a copy from the 728 * user buffer to a staging buffer and then a second copy from the staging 729 * buffer to mbufs. This can be optimized by copying from the user buffer 730 * directly into mbufs and passing the chain down, but that requires a 731 * fair amount of re-working of the relevant codepaths (and can be done 732 * later). 733 */ 734 static int 735 nfs_directio_write(struct vnode *vp, struct uio *uiop, struct ucred *cred, 736 int ioflag) 737 { 738 struct uio uio; 739 struct iovec iov; 740 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 741 struct thread *td = uiop->uio_td; 742 int error, iomode, must_commit, size, wsize; 743 744 KASSERT((ioflag & IO_SYNC) != 0, ("nfs_directio_write: not sync")); 745 mtx_lock(&nmp->nm_mtx); 746 wsize = nmp->nm_wsize; 747 mtx_unlock(&nmp->nm_mtx); 748 while (uiop->uio_resid > 0) { 749 size = MIN(uiop->uio_resid, wsize); 750 size = MIN(uiop->uio_iov->iov_len, size); 751 iov.iov_base = uiop->uio_iov->iov_base; 752 iov.iov_len = size; 753 uio.uio_iov = &iov; 754 uio.uio_iovcnt = 1; 755 uio.uio_offset = uiop->uio_offset; 756 uio.uio_resid = size; 757 uio.uio_segflg = uiop->uio_segflg; 758 uio.uio_rw = UIO_WRITE; 759 uio.uio_td = td; 760 iomode = NFSWRITE_FILESYNC; 761 /* 762 * When doing direct I/O we do not care if the 763 * server's write verifier has changed, but we 764 * do not want to update the verifier if it has 765 * changed, since that hides the change from 766 * writes being done through the buffer cache. 767 * By passing must_commit in set to two, the code 768 * in nfsrpc_writerpc() will not update the 769 * verifier on the mount point. 770 */ 771 must_commit = 2; 772 error = ncl_writerpc(vp, &uio, cred, &iomode, 773 &must_commit, 0, ioflag); 774 KASSERT(must_commit == 2, 775 ("ncl_directio_write: Updated write verifier")); 776 if (error != 0) 777 return (error); 778 if (iomode != NFSWRITE_FILESYNC) 779 printf("nfs_directio_write: Broken server " 780 "did not reply FILE_SYNC\n"); 781 uiop->uio_offset += size; 782 uiop->uio_resid -= size; 783 if (uiop->uio_iov->iov_len <= size) { 784 uiop->uio_iovcnt--; 785 uiop->uio_iov++; 786 } else { 787 uiop->uio_iov->iov_base = 788 (char *)uiop->uio_iov->iov_base + size; 789 uiop->uio_iov->iov_len -= size; 790 } 791 } 792 return (0); 793 } 794 795 /* 796 * Vnode op for write using bio 797 */ 798 int 799 ncl_write(struct vop_write_args *ap) 800 { 801 int biosize; 802 struct uio *uio = ap->a_uio; 803 struct thread *td = uio->uio_td; 804 struct vnode *vp = ap->a_vp; 805 struct nfsnode *np = VTONFS(vp); 806 struct ucred *cred = ap->a_cred; 807 int ioflag = ap->a_ioflag; 808 struct buf *bp; 809 struct vattr vattr; 810 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 811 daddr_t lbn; 812 int bcount, noncontig_write, obcount; 813 int bp_cached, n, on, error = 0, error1, save2, wouldcommit; 814 size_t orig_resid, local_resid; 815 off_t orig_size, tmp_off; 816 struct timespec ts; 817 818 KASSERT(uio->uio_rw == UIO_WRITE, ("ncl_write mode")); 819 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread, 820 ("ncl_write proc")); 821 if (vp->v_type != VREG) 822 return (EIO); 823 NFSLOCKNODE(np); 824 if (np->n_flag & NWRITEERR) { 825 np->n_flag &= ~NWRITEERR; 826 NFSUNLOCKNODE(np); 827 return (np->n_error); 828 } else 829 NFSUNLOCKNODE(np); 830 mtx_lock(&nmp->nm_mtx); 831 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 832 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 833 mtx_unlock(&nmp->nm_mtx); 834 (void)ncl_fsinfo(nmp, vp, cred, td); 835 mtx_lock(&nmp->nm_mtx); 836 } 837 if (nmp->nm_wsize == 0) 838 (void) newnfs_iosize(nmp); 839 mtx_unlock(&nmp->nm_mtx); 840 841 /* 842 * Synchronously flush pending buffers if we are in synchronous 843 * mode or if we are appending. 844 */ 845 if ((ioflag & IO_APPEND) || ((ioflag & IO_SYNC) && (np->n_flag & 846 NMODIFIED))) { 847 /* 848 * For the case where IO_APPEND is being done using a 849 * direct output (to the NFS server) RPC and 850 * newnfs_directio_enable is 0, all buffer cache buffers, 851 * including ones not modified, must be invalidated. 852 * This ensures that stale data is not read out of the 853 * buffer cache. The call also invalidates all mapped 854 * pages and, since the exclusive lock is held on the vnode, 855 * new pages cannot be faulted in. 856 * 857 * For the case where newnfs_directio_enable is set 858 * (which is not the default), it is not obvious that 859 * stale data should be left in the buffer cache, but 860 * the code has been this way for over a decade without 861 * complaints. Note that, unlike doing IO_APPEND via 862 * a direct write RPC when newnfs_directio_enable is not set, 863 * when newnfs_directio_enable is set, reading is done via 864 * direct to NFS server RPCs as well. 865 */ 866 np->n_attrstamp = 0; 867 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 868 error = ncl_vinvalbuf(vp, V_SAVE | ((ioflag & 869 IO_VMIO) != 0 ? V_VMIO : 0), td, 1); 870 if (error != 0) 871 return (error); 872 } 873 874 orig_resid = uio->uio_resid; 875 NFSLOCKNODE(np); 876 orig_size = np->n_size; 877 NFSUNLOCKNODE(np); 878 879 /* 880 * If IO_APPEND then load uio_offset. We restart here if we cannot 881 * get the append lock. 882 */ 883 if (ioflag & IO_APPEND) { 884 /* 885 * For NFSv4, the AppendWrite will Verify the size against 886 * the file's size on the server. If not the same, the 887 * write will then be retried, using the file size returned 888 * by the AppendWrite. However, for NFSv2 and NFSv3, the 889 * size must be acquired here via a Getattr RPC. 890 * The AppendWrite is not done for a pNFS mount. 891 */ 892 if (!NFSHASNFSV4(nmp) || NFSHASPNFS(nmp)) { 893 np->n_attrstamp = 0; 894 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 895 error = VOP_GETATTR(vp, &vattr, cred); 896 if (error) 897 return (error); 898 } 899 NFSLOCKNODE(np); 900 uio->uio_offset = np->n_size; 901 NFSUNLOCKNODE(np); 902 } 903 904 if (uio->uio_offset < 0) 905 return (EINVAL); 906 tmp_off = uio->uio_offset + uio->uio_resid; 907 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uio->uio_offset) 908 return (EFBIG); 909 if (uio->uio_resid == 0) 910 return (0); 911 912 /* 913 * Do IO_APPEND writing via a synchronous direct write. 914 * This can result in a significant performance improvement. 915 */ 916 if ((newnfs_directio_enable && (ioflag & IO_DIRECT)) || 917 (ioflag & IO_APPEND)) { 918 /* 919 * Direct writes to the server must be done NFSWRITE_FILESYNC, 920 * because the write data is not cached and, therefore, the 921 * write cannot be redone after a server reboot. 922 * Set IO_SYNC to make this happen. 923 */ 924 ioflag |= IO_SYNC; 925 return (nfs_directio_write(vp, uio, cred, ioflag)); 926 } 927 928 /* 929 * Maybe this should be above the vnode op call, but so long as 930 * file servers have no limits, i don't think it matters 931 */ 932 error = vn_rlimit_fsize(vp, uio, td); 933 if (error != 0) 934 return (error); 935 936 save2 = curthread_pflags2_set(TDP2_SBPAGES); 937 biosize = vp->v_bufobj.bo_bsize; 938 /* 939 * Find all of this file's B_NEEDCOMMIT buffers. If our writes 940 * would exceed the local maximum per-file write commit size when 941 * combined with those, we must decide whether to flush, 942 * go synchronous, or return error. We don't bother checking 943 * IO_UNIT -- we just make all writes atomic anyway, as there's 944 * no point optimizing for something that really won't ever happen. 945 */ 946 wouldcommit = 0; 947 if (!(ioflag & IO_SYNC)) { 948 int nflag; 949 950 NFSLOCKNODE(np); 951 nflag = np->n_flag; 952 NFSUNLOCKNODE(np); 953 if (nflag & NMODIFIED) { 954 BO_LOCK(&vp->v_bufobj); 955 if (vp->v_bufobj.bo_dirty.bv_cnt != 0) { 956 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, 957 b_bobufs) { 958 if (bp->b_flags & B_NEEDCOMMIT) 959 wouldcommit += bp->b_bcount; 960 } 961 } 962 BO_UNLOCK(&vp->v_bufobj); 963 } 964 } 965 966 do { 967 if (!(ioflag & IO_SYNC)) { 968 wouldcommit += biosize; 969 if (wouldcommit > nmp->nm_wcommitsize) { 970 np->n_attrstamp = 0; 971 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 972 error = ncl_vinvalbuf(vp, V_SAVE | ((ioflag & 973 IO_VMIO) != 0 ? V_VMIO : 0), td, 1); 974 if (error != 0) 975 goto out; 976 wouldcommit = biosize; 977 } 978 } 979 980 NFSINCRGLOBAL(nfsstatsv1.biocache_writes); 981 lbn = uio->uio_offset / biosize; 982 on = uio->uio_offset - (lbn * biosize); 983 n = MIN((unsigned)(biosize - on), uio->uio_resid); 984 again: 985 /* 986 * Handle direct append and file extension cases, calculate 987 * unaligned buffer size. 988 */ 989 NFSLOCKNODE(np); 990 if ((np->n_flag & NHASBEENLOCKED) == 0 && 991 (nmp->nm_flag & NFSMNT_NONCONTIGWR) != 0) 992 noncontig_write = 1; 993 else 994 noncontig_write = 0; 995 if ((uio->uio_offset == np->n_size || 996 (noncontig_write != 0 && 997 lbn == (np->n_size / biosize) && 998 uio->uio_offset + n > np->n_size)) && n) { 999 NFSUNLOCKNODE(np); 1000 /* 1001 * Get the buffer (in its pre-append state to maintain 1002 * B_CACHE if it was previously set). Resize the 1003 * nfsnode after we have locked the buffer to prevent 1004 * readers from reading garbage. 1005 */ 1006 obcount = np->n_size - (lbn * biosize); 1007 bp = nfs_getcacheblk(vp, lbn, obcount, td); 1008 1009 if (bp != NULL) { 1010 long save; 1011 1012 NFSLOCKNODE(np); 1013 np->n_size = uio->uio_offset + n; 1014 np->n_flag |= NMODIFIED; 1015 np->n_flag &= ~NVNSETSZSKIP; 1016 vnode_pager_setsize(vp, np->n_size); 1017 NFSUNLOCKNODE(np); 1018 1019 save = bp->b_flags & B_CACHE; 1020 bcount = on + n; 1021 allocbuf(bp, bcount); 1022 bp->b_flags |= save; 1023 if (noncontig_write != 0 && on > obcount) 1024 vfs_bio_bzero_buf(bp, obcount, on - 1025 obcount); 1026 } 1027 } else { 1028 /* 1029 * Obtain the locked cache block first, and then 1030 * adjust the file's size as appropriate. 1031 */ 1032 bcount = on + n; 1033 if ((off_t)lbn * biosize + bcount < np->n_size) { 1034 if ((off_t)(lbn + 1) * biosize < np->n_size) 1035 bcount = biosize; 1036 else 1037 bcount = np->n_size - (off_t)lbn * biosize; 1038 } 1039 NFSUNLOCKNODE(np); 1040 bp = nfs_getcacheblk(vp, lbn, bcount, td); 1041 NFSLOCKNODE(np); 1042 if (uio->uio_offset + n > np->n_size) { 1043 np->n_size = uio->uio_offset + n; 1044 np->n_flag |= NMODIFIED; 1045 np->n_flag &= ~NVNSETSZSKIP; 1046 vnode_pager_setsize(vp, np->n_size); 1047 } 1048 NFSUNLOCKNODE(np); 1049 } 1050 1051 if (!bp) { 1052 error = newnfs_sigintr(nmp, td); 1053 if (!error) 1054 error = EINTR; 1055 break; 1056 } 1057 1058 /* 1059 * Issue a READ if B_CACHE is not set. In special-append 1060 * mode, B_CACHE is based on the buffer prior to the write 1061 * op and is typically set, avoiding the read. If a read 1062 * is required in special append mode, the server will 1063 * probably send us a short-read since we extended the file 1064 * on our end, resulting in b_resid == 0 and, thusly, 1065 * B_CACHE getting set. 1066 * 1067 * We can also avoid issuing the read if the write covers 1068 * the entire buffer. We have to make sure the buffer state 1069 * is reasonable in this case since we will not be initiating 1070 * I/O. See the comments in kern/vfs_bio.c's getblk() for 1071 * more information. 1072 * 1073 * B_CACHE may also be set due to the buffer being cached 1074 * normally. 1075 */ 1076 1077 bp_cached = 1; 1078 if (on == 0 && n == bcount) { 1079 if ((bp->b_flags & B_CACHE) == 0) 1080 bp_cached = 0; 1081 bp->b_flags |= B_CACHE; 1082 bp->b_flags &= ~B_INVAL; 1083 bp->b_ioflags &= ~BIO_ERROR; 1084 } 1085 1086 if ((bp->b_flags & B_CACHE) == 0) { 1087 bp->b_iocmd = BIO_READ; 1088 vfs_busy_pages(bp, 0); 1089 error = ncl_doio(vp, bp, cred, td, 0); 1090 if (error) { 1091 brelse(bp); 1092 break; 1093 } 1094 } 1095 if (bp->b_wcred == NOCRED) 1096 bp->b_wcred = crhold(cred); 1097 NFSLOCKNODE(np); 1098 np->n_flag |= NMODIFIED; 1099 NFSUNLOCKNODE(np); 1100 1101 /* 1102 * If dirtyend exceeds file size, chop it down. This should 1103 * not normally occur but there is an append race where it 1104 * might occur XXX, so we log it. 1105 * 1106 * If the chopping creates a reverse-indexed or degenerate 1107 * situation with dirtyoff/end, we 0 both of them. 1108 */ 1109 1110 if (bp->b_dirtyend > bcount) { 1111 printf("NFS append race @%lx:%d\n", 1112 (long)bp->b_blkno * DEV_BSIZE, 1113 bp->b_dirtyend - bcount); 1114 bp->b_dirtyend = bcount; 1115 } 1116 1117 if (bp->b_dirtyoff >= bp->b_dirtyend) 1118 bp->b_dirtyoff = bp->b_dirtyend = 0; 1119 1120 /* 1121 * If the new write will leave a contiguous dirty 1122 * area, just update the b_dirtyoff and b_dirtyend, 1123 * otherwise force a write rpc of the old dirty area. 1124 * 1125 * If there has been a file lock applied to this file 1126 * or vfs.nfs.old_noncontig_writing is set, do the following: 1127 * While it is possible to merge discontiguous writes due to 1128 * our having a B_CACHE buffer ( and thus valid read data 1129 * for the hole), we don't because it could lead to 1130 * significant cache coherency problems with multiple clients, 1131 * especially if locking is implemented later on. 1132 * 1133 * If vfs.nfs.old_noncontig_writing is not set and there has 1134 * not been file locking done on this file: 1135 * Relax coherency a bit for the sake of performance and 1136 * expand the current dirty region to contain the new 1137 * write even if it means we mark some non-dirty data as 1138 * dirty. 1139 */ 1140 1141 if (noncontig_write == 0 && bp->b_dirtyend > 0 && 1142 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 1143 if (bwrite(bp) == EINTR) { 1144 error = EINTR; 1145 break; 1146 } 1147 goto again; 1148 } 1149 1150 local_resid = uio->uio_resid; 1151 error = vn_io_fault_uiomove((char *)bp->b_data + on, n, uio); 1152 1153 if (error != 0 && !bp_cached) { 1154 /* 1155 * This block has no other content then what 1156 * possibly was written by the faulty uiomove. 1157 * Release it, forgetting the data pages, to 1158 * prevent the leak of uninitialized data to 1159 * usermode. 1160 */ 1161 bp->b_ioflags |= BIO_ERROR; 1162 brelse(bp); 1163 uio->uio_offset -= local_resid - uio->uio_resid; 1164 uio->uio_resid = local_resid; 1165 break; 1166 } 1167 1168 /* 1169 * Since this block is being modified, it must be written 1170 * again and not just committed. Since write clustering does 1171 * not work for the stage 1 data write, only the stage 2 1172 * commit rpc, we have to clear B_CLUSTEROK as well. 1173 */ 1174 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1175 1176 /* 1177 * Get the partial update on the progress made from 1178 * uiomove, if an error occurred. 1179 */ 1180 if (error != 0) 1181 n = local_resid - uio->uio_resid; 1182 1183 /* 1184 * Only update dirtyoff/dirtyend if not a degenerate 1185 * condition. 1186 */ 1187 if (n > 0) { 1188 if (bp->b_dirtyend > 0) { 1189 bp->b_dirtyoff = min(on, bp->b_dirtyoff); 1190 bp->b_dirtyend = max((on + n), bp->b_dirtyend); 1191 } else { 1192 bp->b_dirtyoff = on; 1193 bp->b_dirtyend = on + n; 1194 } 1195 vfs_bio_set_valid(bp, on, n); 1196 } 1197 1198 /* 1199 * If IO_SYNC do bwrite(). 1200 * 1201 * IO_INVAL appears to be unused. The idea appears to be 1202 * to turn off caching in this case. Very odd. XXX 1203 */ 1204 if ((ioflag & IO_SYNC)) { 1205 if (ioflag & IO_INVAL) 1206 bp->b_flags |= B_NOCACHE; 1207 error1 = bwrite(bp); 1208 if (error1 != 0) { 1209 if (error == 0) 1210 error = error1; 1211 break; 1212 } 1213 } else if ((n + on) == biosize || (ioflag & IO_ASYNC) != 0) { 1214 bp->b_flags |= B_ASYNC; 1215 (void) bwrite(bp); 1216 } else { 1217 bdwrite(bp); 1218 } 1219 1220 if (error != 0) 1221 break; 1222 } while (uio->uio_resid > 0 && n > 0); 1223 1224 if (error == 0) { 1225 nanouptime(&ts); 1226 NFSLOCKNODE(np); 1227 np->n_localmodtime = ts; 1228 NFSUNLOCKNODE(np); 1229 } else { 1230 if (ioflag & IO_UNIT) { 1231 VATTR_NULL(&vattr); 1232 vattr.va_size = orig_size; 1233 /* IO_SYNC is handled implicitely */ 1234 (void)VOP_SETATTR(vp, &vattr, cred); 1235 uio->uio_offset -= orig_resid - uio->uio_resid; 1236 uio->uio_resid = orig_resid; 1237 } 1238 } 1239 1240 out: 1241 curthread_pflags2_restore(save2); 1242 return (error); 1243 } 1244 1245 /* 1246 * Get an nfs cache block. 1247 * 1248 * Allocate a new one if the block isn't currently in the cache 1249 * and return the block marked busy. If the calling process is 1250 * interrupted by a signal for an interruptible mount point, return 1251 * NULL. 1252 * 1253 * The caller must carefully deal with the possible B_INVAL state of 1254 * the buffer. ncl_doio() clears B_INVAL (and ncl_asyncio() clears it 1255 * indirectly), so synchronous reads can be issued without worrying about 1256 * the B_INVAL state. We have to be a little more careful when dealing 1257 * with writes (see comments in nfs_write()) when extending a file past 1258 * its EOF. 1259 */ 1260 static struct buf * 1261 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td) 1262 { 1263 struct buf *bp; 1264 struct mount *mp; 1265 struct nfsmount *nmp; 1266 1267 mp = vp->v_mount; 1268 nmp = VFSTONFS(mp); 1269 1270 if (nmp->nm_flag & NFSMNT_INT) { 1271 sigset_t oldset; 1272 1273 newnfs_set_sigmask(td, &oldset); 1274 bp = getblk(vp, bn, size, PCATCH, 0, 0); 1275 newnfs_restore_sigmask(td, &oldset); 1276 while (bp == NULL) { 1277 if (newnfs_sigintr(nmp, td)) 1278 return (NULL); 1279 bp = getblk(vp, bn, size, 0, 2 * hz, 0); 1280 } 1281 } else { 1282 bp = getblk(vp, bn, size, 0, 0, 0); 1283 } 1284 1285 if (vp->v_type == VREG) 1286 bp->b_blkno = bn * (vp->v_bufobj.bo_bsize / DEV_BSIZE); 1287 return (bp); 1288 } 1289 1290 /* 1291 * Flush and invalidate all dirty buffers. If another process is already 1292 * doing the flush, just wait for completion. 1293 */ 1294 int 1295 ncl_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg) 1296 { 1297 struct nfsnode *np = VTONFS(vp); 1298 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1299 int error = 0, slpflag, slptimeo; 1300 bool old_lock; 1301 struct timespec ts; 1302 1303 ASSERT_VOP_LOCKED(vp, "ncl_vinvalbuf"); 1304 1305 if ((nmp->nm_flag & NFSMNT_INT) == 0) 1306 intrflg = 0; 1307 if (NFSCL_FORCEDISM(nmp->nm_mountp)) 1308 intrflg = 1; 1309 if (intrflg) { 1310 slpflag = PCATCH; 1311 slptimeo = 2 * hz; 1312 } else { 1313 slpflag = 0; 1314 slptimeo = 0; 1315 } 1316 1317 old_lock = ncl_excl_start(vp); 1318 if (old_lock) 1319 flags |= V_ALLOWCLEAN; 1320 1321 /* 1322 * Now, flush as required. 1323 */ 1324 if ((flags & (V_SAVE | V_VMIO)) == V_SAVE) { 1325 vnode_pager_clean_sync(vp); 1326 1327 /* 1328 * If the page clean was interrupted, fail the invalidation. 1329 * Not doing so, we run the risk of losing dirty pages in the 1330 * vinvalbuf() call below. 1331 */ 1332 if (intrflg && (error = newnfs_sigintr(nmp, td))) 1333 goto out; 1334 } 1335 1336 error = vinvalbuf(vp, flags, slpflag, 0); 1337 while (error) { 1338 if (intrflg && (error = newnfs_sigintr(nmp, td))) 1339 goto out; 1340 error = vinvalbuf(vp, flags, 0, slptimeo); 1341 } 1342 if (NFSHASPNFS(nmp)) { 1343 nfscl_layoutcommit(vp, td); 1344 nanouptime(&ts); 1345 /* 1346 * Invalidate the attribute cache, since writes to a DS 1347 * won't update the size attribute. 1348 */ 1349 NFSLOCKNODE(np); 1350 np->n_attrstamp = 0; 1351 } else { 1352 nanouptime(&ts); 1353 NFSLOCKNODE(np); 1354 } 1355 if ((np->n_flag & NMODIFIED) != 0) { 1356 np->n_localmodtime = ts; 1357 np->n_flag &= ~NMODIFIED; 1358 } 1359 NFSUNLOCKNODE(np); 1360 out: 1361 ncl_excl_finish(vp, old_lock); 1362 return error; 1363 } 1364 1365 /* 1366 * Initiate asynchronous I/O. Return an error if no nfsiods are available. 1367 * This is mainly to avoid queueing async I/O requests when the nfsiods 1368 * are all hung on a dead server. 1369 * 1370 * Note: ncl_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp 1371 * is eventually dequeued by the async daemon, ncl_doio() *will*. 1372 */ 1373 int 1374 ncl_asyncio(struct nfsmount *nmp, struct buf *bp, struct ucred *cred, struct thread *td) 1375 { 1376 int iod; 1377 int gotiod; 1378 int slpflag = 0; 1379 int slptimeo = 0; 1380 int error, error2; 1381 1382 /* 1383 * Commits are usually short and sweet so lets save some cpu and 1384 * leave the async daemons for more important rpc's (such as reads 1385 * and writes). 1386 * 1387 * Readdirplus RPCs do vget()s to acquire the vnodes for entries 1388 * in the directory in order to update attributes. This can deadlock 1389 * with another thread that is waiting for async I/O to be done by 1390 * an nfsiod thread while holding a lock on one of these vnodes. 1391 * To avoid this deadlock, don't allow the async nfsiod threads to 1392 * perform Readdirplus RPCs. 1393 */ 1394 NFSLOCKIOD(); 1395 if ((bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) && 1396 (nmp->nm_bufqiods > ncl_numasync / 2)) || 1397 (bp->b_vp->v_type == VDIR && (nmp->nm_flag & NFSMNT_RDIRPLUS))) { 1398 NFSUNLOCKIOD(); 1399 return(EIO); 1400 } 1401 again: 1402 if (nmp->nm_flag & NFSMNT_INT) 1403 slpflag = PCATCH; 1404 gotiod = FALSE; 1405 1406 /* 1407 * Find a free iod to process this request. 1408 */ 1409 for (iod = 0; iod < ncl_numasync; iod++) 1410 if (ncl_iodwant[iod] == NFSIOD_AVAILABLE) { 1411 gotiod = TRUE; 1412 break; 1413 } 1414 1415 /* 1416 * Try to create one if none are free. 1417 */ 1418 if (!gotiod) 1419 ncl_nfsiodnew(); 1420 else { 1421 /* 1422 * Found one, so wake it up and tell it which 1423 * mount to process. 1424 */ 1425 NFS_DPF(ASYNCIO, ("ncl_asyncio: waking iod %d for mount %p\n", 1426 iod, nmp)); 1427 ncl_iodwant[iod] = NFSIOD_NOT_AVAILABLE; 1428 ncl_iodmount[iod] = nmp; 1429 nmp->nm_bufqiods++; 1430 wakeup(&ncl_iodwant[iod]); 1431 } 1432 1433 /* 1434 * If none are free, we may already have an iod working on this mount 1435 * point. If so, it will process our request. 1436 */ 1437 if (!gotiod) { 1438 if (nmp->nm_bufqiods > 0) { 1439 NFS_DPF(ASYNCIO, 1440 ("ncl_asyncio: %d iods are already processing mount %p\n", 1441 nmp->nm_bufqiods, nmp)); 1442 gotiod = TRUE; 1443 } 1444 } 1445 1446 /* 1447 * If we have an iod which can process the request, then queue 1448 * the buffer. 1449 */ 1450 if (gotiod) { 1451 /* 1452 * Ensure that the queue never grows too large. We still want 1453 * to asynchronize so we block rather then return EIO. 1454 */ 1455 while (nmp->nm_bufqlen >= 2*ncl_numasync) { 1456 NFS_DPF(ASYNCIO, 1457 ("ncl_asyncio: waiting for mount %p queue to drain\n", nmp)); 1458 nmp->nm_bufqwant = TRUE; 1459 error = newnfs_msleep(td, &nmp->nm_bufq, 1460 &ncl_iod_mutex, slpflag | PRIBIO, "nfsaio", 1461 slptimeo); 1462 if (error) { 1463 error2 = newnfs_sigintr(nmp, td); 1464 if (error2) { 1465 NFSUNLOCKIOD(); 1466 return (error2); 1467 } 1468 if (slpflag == PCATCH) { 1469 slpflag = 0; 1470 slptimeo = 2 * hz; 1471 } 1472 } 1473 /* 1474 * We might have lost our iod while sleeping, 1475 * so check and loop if necessary. 1476 */ 1477 goto again; 1478 } 1479 1480 /* We might have lost our nfsiod */ 1481 if (nmp->nm_bufqiods == 0) { 1482 NFS_DPF(ASYNCIO, 1483 ("ncl_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 1484 goto again; 1485 } 1486 1487 if (bp->b_iocmd == BIO_READ) { 1488 if (bp->b_rcred == NOCRED && cred != NOCRED) 1489 bp->b_rcred = crhold(cred); 1490 } else { 1491 if (bp->b_wcred == NOCRED && cred != NOCRED) 1492 bp->b_wcred = crhold(cred); 1493 } 1494 1495 if (bp->b_flags & B_REMFREE) 1496 bremfreef(bp); 1497 BUF_KERNPROC(bp); 1498 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 1499 nmp->nm_bufqlen++; 1500 KASSERT((bp->b_flags & B_DIRECT) == 0, 1501 ("ncl_asyncio: B_DIRECT set")); 1502 NFSUNLOCKIOD(); 1503 return (0); 1504 } 1505 1506 NFSUNLOCKIOD(); 1507 1508 /* 1509 * All the iods are busy on other mounts, so return EIO to 1510 * force the caller to process the i/o synchronously. 1511 */ 1512 NFS_DPF(ASYNCIO, ("ncl_asyncio: no iods available, i/o is synchronous\n")); 1513 return (EIO); 1514 } 1515 1516 /* 1517 * Do an I/O operation to/from a cache block. This may be called 1518 * synchronously or from an nfsiod. 1519 */ 1520 int 1521 ncl_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td, 1522 int called_from_strategy) 1523 { 1524 struct uio *uiop; 1525 struct nfsnode *np; 1526 struct nfsmount *nmp; 1527 int error = 0, iomode, must_commit = 0; 1528 struct uio uio; 1529 struct iovec io; 1530 struct proc *p = td ? td->td_proc : NULL; 1531 uint8_t iocmd; 1532 1533 np = VTONFS(vp); 1534 nmp = VFSTONFS(vp->v_mount); 1535 uiop = &uio; 1536 uiop->uio_iov = &io; 1537 uiop->uio_iovcnt = 1; 1538 uiop->uio_segflg = UIO_SYSSPACE; 1539 uiop->uio_td = td; 1540 1541 /* 1542 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We 1543 * do this here so we do not have to do it in all the code that 1544 * calls us. 1545 */ 1546 bp->b_flags &= ~B_INVAL; 1547 bp->b_ioflags &= ~BIO_ERROR; 1548 1549 KASSERT(!(bp->b_flags & B_DONE), ("ncl_doio: bp %p already marked done", bp)); 1550 iocmd = bp->b_iocmd; 1551 if (iocmd == BIO_READ) { 1552 io.iov_len = uiop->uio_resid = bp->b_bcount; 1553 io.iov_base = bp->b_data; 1554 uiop->uio_rw = UIO_READ; 1555 1556 switch (vp->v_type) { 1557 case VREG: 1558 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1559 NFSINCRGLOBAL(nfsstatsv1.read_bios); 1560 error = ncl_readrpc(vp, uiop, cr); 1561 1562 if (!error) { 1563 if (uiop->uio_resid) { 1564 /* 1565 * If we had a short read with no error, we must have 1566 * hit a file hole. We should zero-fill the remainder. 1567 * This can also occur if the server hits the file EOF. 1568 * 1569 * Holes used to be able to occur due to pending 1570 * writes, but that is not possible any longer. 1571 */ 1572 int nread = bp->b_bcount - uiop->uio_resid; 1573 ssize_t left = uiop->uio_resid; 1574 1575 if (left > 0) 1576 bzero((char *)bp->b_data + nread, left); 1577 uiop->uio_resid = 0; 1578 } 1579 } 1580 /* ASSERT_VOP_LOCKED(vp, "ncl_doio"); */ 1581 if (p && vp->v_writecount <= -1) { 1582 NFSLOCKNODE(np); 1583 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &np->n_vattr.na_mtime)) { 1584 NFSUNLOCKNODE(np); 1585 PROC_LOCK(p); 1586 killproc(p, "text file modification"); 1587 PROC_UNLOCK(p); 1588 } else 1589 NFSUNLOCKNODE(np); 1590 } 1591 break; 1592 case VLNK: 1593 uiop->uio_offset = (off_t)0; 1594 NFSINCRGLOBAL(nfsstatsv1.readlink_bios); 1595 error = ncl_readlinkrpc(vp, uiop, cr); 1596 break; 1597 case VDIR: 1598 NFSINCRGLOBAL(nfsstatsv1.readdir_bios); 1599 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 1600 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) != 0) { 1601 error = ncl_readdirplusrpc(vp, uiop, cr, td); 1602 if (error == NFSERR_NOTSUPP) 1603 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 1604 } 1605 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 1606 error = ncl_readdirrpc(vp, uiop, cr, td); 1607 /* 1608 * end-of-directory sets B_INVAL but does not generate an 1609 * error. 1610 */ 1611 if (error == 0 && uiop->uio_resid == bp->b_bcount) 1612 bp->b_flags |= B_INVAL; 1613 break; 1614 default: 1615 printf("ncl_doio: type %x unexpected\n", vp->v_type); 1616 break; 1617 } 1618 if (error) { 1619 bp->b_ioflags |= BIO_ERROR; 1620 bp->b_error = error; 1621 } 1622 } else { 1623 /* 1624 * If we only need to commit, try to commit 1625 */ 1626 if (bp->b_flags & B_NEEDCOMMIT) { 1627 int retv; 1628 off_t off; 1629 1630 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff; 1631 retv = ncl_commit(vp, off, bp->b_dirtyend-bp->b_dirtyoff, 1632 bp->b_wcred, td); 1633 if (NFSCL_FORCEDISM(vp->v_mount) || retv == 0) { 1634 bp->b_dirtyoff = bp->b_dirtyend = 0; 1635 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1636 bp->b_resid = 0; 1637 bufdone(bp); 1638 return (0); 1639 } 1640 if (retv == NFSERR_STALEWRITEVERF) { 1641 ncl_clearcommit(vp->v_mount); 1642 } 1643 } 1644 1645 /* 1646 * Setup for actual write 1647 */ 1648 NFSLOCKNODE(np); 1649 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size) 1650 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE; 1651 NFSUNLOCKNODE(np); 1652 1653 if (bp->b_dirtyend > bp->b_dirtyoff) { 1654 io.iov_len = uiop->uio_resid = bp->b_dirtyend 1655 - bp->b_dirtyoff; 1656 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE 1657 + bp->b_dirtyoff; 1658 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 1659 uiop->uio_rw = UIO_WRITE; 1660 NFSINCRGLOBAL(nfsstatsv1.write_bios); 1661 1662 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 1663 iomode = NFSWRITE_UNSTABLE; 1664 else 1665 iomode = NFSWRITE_FILESYNC; 1666 1667 error = ncl_writerpc(vp, uiop, cr, &iomode, &must_commit, 1668 called_from_strategy, 0); 1669 1670 /* 1671 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try 1672 * to cluster the buffers needing commit. This will allow 1673 * the system to submit a single commit rpc for the whole 1674 * cluster. We can do this even if the buffer is not 100% 1675 * dirty (relative to the NFS blocksize), so we optimize the 1676 * append-to-file-case. 1677 * 1678 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be 1679 * cleared because write clustering only works for commit 1680 * rpc's, not for the data portion of the write). 1681 */ 1682 1683 if (!error && iomode == NFSWRITE_UNSTABLE) { 1684 bp->b_flags |= B_NEEDCOMMIT; 1685 if (bp->b_dirtyoff == 0 1686 && bp->b_dirtyend == bp->b_bcount) 1687 bp->b_flags |= B_CLUSTEROK; 1688 } else { 1689 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1690 } 1691 1692 /* 1693 * For an interrupted write, the buffer is still valid 1694 * and the write hasn't been pushed to the server yet, 1695 * so we can't set BIO_ERROR and report the interruption 1696 * by setting B_EINTR. For the B_ASYNC case, B_EINTR 1697 * is not relevant, so the rpc attempt is essentially 1698 * a noop. For the case of a V3 write rpc not being 1699 * committed to stable storage, the block is still 1700 * dirty and requires either a commit rpc or another 1701 * write rpc with iomode == NFSV3WRITE_FILESYNC before 1702 * the block is reused. This is indicated by setting 1703 * the B_DELWRI and B_NEEDCOMMIT flags. 1704 * 1705 * EIO is returned by ncl_writerpc() to indicate a recoverable 1706 * write error and is handled as above, except that 1707 * B_EINTR isn't set. One cause of this is a stale stateid 1708 * error for the RPC that indicates recovery is required, 1709 * when called with called_from_strategy != 0. 1710 * 1711 * If the buffer is marked B_PAGING, it does not reside on 1712 * the vp's paging queues so we cannot call bdirty(). The 1713 * bp in this case is not an NFS cache block so we should 1714 * be safe. XXX 1715 * 1716 * The logic below breaks up errors into recoverable and 1717 * unrecoverable. For the former, we clear B_INVAL|B_NOCACHE 1718 * and keep the buffer around for potential write retries. 1719 * For the latter (eg ESTALE), we toss the buffer away (B_INVAL) 1720 * and save the error in the nfsnode. This is less than ideal 1721 * but necessary. Keeping such buffers around could potentially 1722 * cause buffer exhaustion eventually (they can never be written 1723 * out, so will get constantly be re-dirtied). It also causes 1724 * all sorts of vfs panics. For non-recoverable write errors, 1725 * also invalidate the attrcache, so we'll be forced to go over 1726 * the wire for this object, returning an error to user on next 1727 * call (most of the time). 1728 */ 1729 if (error == EINTR || error == EIO || error == ETIMEDOUT 1730 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 1731 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 1732 if ((bp->b_flags & B_PAGING) == 0) { 1733 bdirty(bp); 1734 bp->b_flags &= ~B_DONE; 1735 } 1736 if ((error == EINTR || error == ETIMEDOUT) && 1737 (bp->b_flags & B_ASYNC) == 0) 1738 bp->b_flags |= B_EINTR; 1739 } else { 1740 if (error) { 1741 bp->b_ioflags |= BIO_ERROR; 1742 bp->b_flags |= B_INVAL; 1743 bp->b_error = np->n_error = error; 1744 NFSLOCKNODE(np); 1745 np->n_flag |= NWRITEERR; 1746 np->n_attrstamp = 0; 1747 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 1748 NFSUNLOCKNODE(np); 1749 } 1750 bp->b_dirtyoff = bp->b_dirtyend = 0; 1751 } 1752 } else { 1753 bp->b_resid = 0; 1754 bufdone(bp); 1755 return (0); 1756 } 1757 } 1758 bp->b_resid = uiop->uio_resid; 1759 if (must_commit == 1) 1760 ncl_clearcommit(vp->v_mount); 1761 bufdone(bp); 1762 return (error); 1763 } 1764 1765 /* 1766 * Used to aid in handling ftruncate() operations on the NFS client side. 1767 * Truncation creates a number of special problems for NFS. We have to 1768 * throw away VM pages and buffer cache buffers that are beyond EOF, and 1769 * we have to properly handle VM pages or (potentially dirty) buffers 1770 * that straddle the truncation point. 1771 */ 1772 1773 int 1774 ncl_meta_setsize(struct vnode *vp, struct thread *td, u_quad_t nsize) 1775 { 1776 struct nfsnode *np = VTONFS(vp); 1777 u_quad_t tsize; 1778 int biosize = vp->v_bufobj.bo_bsize; 1779 int error = 0; 1780 1781 NFSLOCKNODE(np); 1782 tsize = np->n_size; 1783 np->n_size = nsize; 1784 NFSUNLOCKNODE(np); 1785 1786 if (nsize < tsize) { 1787 struct buf *bp; 1788 daddr_t lbn; 1789 int bufsize; 1790 1791 /* 1792 * vtruncbuf() doesn't get the buffer overlapping the 1793 * truncation point. We may have a B_DELWRI and/or B_CACHE 1794 * buffer that now needs to be truncated. 1795 */ 1796 error = vtruncbuf(vp, nsize, biosize); 1797 lbn = nsize / biosize; 1798 bufsize = nsize - (lbn * biosize); 1799 bp = nfs_getcacheblk(vp, lbn, bufsize, td); 1800 if (!bp) 1801 return EINTR; 1802 if (bp->b_dirtyoff > bp->b_bcount) 1803 bp->b_dirtyoff = bp->b_bcount; 1804 if (bp->b_dirtyend > bp->b_bcount) 1805 bp->b_dirtyend = bp->b_bcount; 1806 bp->b_flags |= B_RELBUF; /* don't leave garbage around */ 1807 brelse(bp); 1808 } else { 1809 vnode_pager_setsize(vp, nsize); 1810 } 1811 return(error); 1812 } 1813