1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95 37 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_bio.c,v 1.130 2004/04/14 23:23:55 peadar Exp $ 38 * $DragonFly: src/sys/vfs/nfs/nfs_bio.c,v 1.22 2005/04/15 19:08:21 dillon Exp $ 39 */ 40 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/resourcevar.h> 45 #include <sys/signalvar.h> 46 #include <sys/proc.h> 47 #include <sys/buf.h> 48 #include <sys/vnode.h> 49 #include <sys/mount.h> 50 #include <sys/kernel.h> 51 #include <sys/buf2.h> 52 #include <sys/msfbuf.h> 53 54 #include <vm/vm.h> 55 #include <vm/vm_extern.h> 56 #include <vm/vm_page.h> 57 #include <vm/vm_object.h> 58 #include <vm/vm_pager.h> 59 #include <vm/vnode_pager.h> 60 61 #include "rpcv2.h" 62 #include "nfsproto.h" 63 #include "nfs.h" 64 #include "nfsmount.h" 65 #include "nqnfs.h" 66 #include "nfsnode.h" 67 68 static struct buf *nfs_getcacheblk (struct vnode *vp, daddr_t bn, int size, 69 struct thread *td); 70 71 extern int nfs_numasync; 72 extern int nfs_pbuf_freecnt; 73 extern struct nfsstats nfsstats; 74 75 /* 76 * Vnode op for VM getpages. 77 * 78 * nfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, 79 * int a_reqpage, vm_ooffset_t a_offset) 80 */ 81 int 82 nfs_getpages(struct vop_getpages_args *ap) 83 { 84 struct thread *td = curthread; /* XXX */ 85 int i, error, nextoff, size, toff, count, npages; 86 struct uio uio; 87 struct iovec iov; 88 char *kva; 89 struct vnode *vp; 90 struct nfsmount *nmp; 91 vm_page_t *pages; 92 vm_page_t m; 93 struct msf_buf *msf; 94 95 vp = ap->a_vp; 96 nmp = VFSTONFS(vp->v_mount); 97 pages = ap->a_m; 98 count = ap->a_count; 99 100 if (vp->v_object == NULL) { 101 printf("nfs_getpages: called with non-merged cache vnode??\n"); 102 return VM_PAGER_ERROR; 103 } 104 105 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 106 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 107 (void)nfs_fsinfo(nmp, vp, td); 108 109 npages = btoc(count); 110 111 /* 112 * NOTE that partially valid pages may occur in cases other 113 * then file EOF, such as when a file is partially written and 114 * ftruncate()-extended to a larger size. It is also possible 115 * for the valid bits to be set on garbage beyond the file EOF and 116 * clear in the area before EOF (e.g. m->valid == 0xfc), which can 117 * occur due to vtruncbuf() and the buffer cache's handling of 118 * pages which 'straddle' buffers or when b_bufsize is not a 119 * multiple of PAGE_SIZE.... the buffer cache cannot normally 120 * clear the extra bits. This kind of situation occurs when you 121 * make a small write() (m->valid == 0x03) and then mmap() and 122 * fault in the buffer(m->valid = 0xFF). When NFS flushes the 123 * buffer (vinvalbuf() m->valid = 0xFC) we are left with a mess. 124 * 125 * This is combined with the possibility that the pages are partially 126 * dirty or that there is a buffer backing the pages that is dirty 127 * (even if m->dirty is 0). 128 * 129 * To solve this problem several hacks have been made: (1) NFS 130 * guarentees that the IO block size is a multiple of PAGE_SIZE and 131 * (2) The buffer cache, when invalidating an NFS buffer, will 132 * disregard the buffer's fragmentory b_bufsize and invalidate 133 * the whole page rather then just the piece the buffer owns. 134 * 135 * This allows us to assume that a partially valid page found here 136 * is fully valid (vm_fault will zero'd out areas of the page not 137 * marked as valid). 138 */ 139 m = pages[ap->a_reqpage]; 140 if (m->valid != 0) { 141 for (i = 0; i < npages; ++i) { 142 if (i != ap->a_reqpage) 143 vnode_pager_freepage(pages[i]); 144 } 145 return(0); 146 } 147 148 /* 149 * Use an MSF_BUF as a medium to retrieve data from the pages. 150 */ 151 msf_map_pagelist(&msf, pages, npages, 0); 152 KKASSERT(msf); 153 kva = msf_buf_kva(msf); 154 155 iov.iov_base = kva; 156 iov.iov_len = count; 157 uio.uio_iov = &iov; 158 uio.uio_iovcnt = 1; 159 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex); 160 uio.uio_resid = count; 161 uio.uio_segflg = UIO_SYSSPACE; 162 uio.uio_rw = UIO_READ; 163 uio.uio_td = td; 164 165 error = nfs_readrpc(vp, &uio); 166 msf_buf_free(msf); 167 168 if (error && (uio.uio_resid == count)) { 169 printf("nfs_getpages: error %d\n", error); 170 for (i = 0; i < npages; ++i) { 171 if (i != ap->a_reqpage) 172 vnode_pager_freepage(pages[i]); 173 } 174 return VM_PAGER_ERROR; 175 } 176 177 /* 178 * Calculate the number of bytes read and validate only that number 179 * of bytes. Note that due to pending writes, size may be 0. This 180 * does not mean that the remaining data is invalid! 181 */ 182 183 size = count - uio.uio_resid; 184 185 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) { 186 nextoff = toff + PAGE_SIZE; 187 m = pages[i]; 188 189 m->flags &= ~PG_ZERO; 190 191 if (nextoff <= size) { 192 /* 193 * Read operation filled an entire page 194 */ 195 m->valid = VM_PAGE_BITS_ALL; 196 vm_page_undirty(m); 197 } else if (size > toff) { 198 /* 199 * Read operation filled a partial page. 200 */ 201 m->valid = 0; 202 vm_page_set_validclean(m, 0, size - toff); 203 /* handled by vm_fault now */ 204 /* vm_page_zero_invalid(m, TRUE); */ 205 } else { 206 /* 207 * Read operation was short. If no error occured 208 * we may have hit a zero-fill section. We simply 209 * leave valid set to 0. 210 */ 211 ; 212 } 213 if (i != ap->a_reqpage) { 214 /* 215 * Whether or not to leave the page activated is up in 216 * the air, but we should put the page on a page queue 217 * somewhere (it already is in the object). Result: 218 * It appears that emperical results show that 219 * deactivating pages is best. 220 */ 221 222 /* 223 * Just in case someone was asking for this page we 224 * now tell them that it is ok to use. 225 */ 226 if (!error) { 227 if (m->flags & PG_WANTED) 228 vm_page_activate(m); 229 else 230 vm_page_deactivate(m); 231 vm_page_wakeup(m); 232 } else { 233 vnode_pager_freepage(m); 234 } 235 } 236 } 237 return 0; 238 } 239 240 /* 241 * Vnode op for VM putpages. 242 * 243 * nfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync, 244 * int *a_rtvals, vm_ooffset_t a_offset) 245 */ 246 int 247 nfs_putpages(struct vop_putpages_args *ap) 248 { 249 struct thread *td = curthread; 250 struct uio uio; 251 struct iovec iov; 252 char *kva; 253 int iomode, must_commit, i, error, npages, count; 254 off_t offset; 255 int *rtvals; 256 struct vnode *vp; 257 struct nfsmount *nmp; 258 struct nfsnode *np; 259 vm_page_t *pages; 260 struct msf_buf *msf; 261 262 vp = ap->a_vp; 263 np = VTONFS(vp); 264 nmp = VFSTONFS(vp->v_mount); 265 pages = ap->a_m; 266 count = ap->a_count; 267 rtvals = ap->a_rtvals; 268 npages = btoc(count); 269 offset = IDX_TO_OFF(pages[0]->pindex); 270 271 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 272 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 273 (void)nfs_fsinfo(nmp, vp, td); 274 275 for (i = 0; i < npages; i++) { 276 rtvals[i] = VM_PAGER_AGAIN; 277 } 278 279 /* 280 * When putting pages, do not extend file past EOF. 281 */ 282 283 if (offset + count > np->n_size) { 284 count = np->n_size - offset; 285 if (count < 0) 286 count = 0; 287 } 288 289 /* 290 * Use an MSF_BUF as a medium to retrieve data from the pages. 291 */ 292 msf_map_pagelist(&msf, pages, npages, 0); 293 KKASSERT(msf); 294 kva = msf_buf_kva(msf); 295 296 iov.iov_base = kva; 297 iov.iov_len = count; 298 uio.uio_iov = &iov; 299 uio.uio_iovcnt = 1; 300 uio.uio_offset = offset; 301 uio.uio_resid = count; 302 uio.uio_segflg = UIO_SYSSPACE; 303 uio.uio_rw = UIO_WRITE; 304 uio.uio_td = td; 305 306 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0) 307 iomode = NFSV3WRITE_UNSTABLE; 308 else 309 iomode = NFSV3WRITE_FILESYNC; 310 311 error = nfs_writerpc(vp, &uio, &iomode, &must_commit); 312 313 msf_buf_free(msf); 314 315 if (!error) { 316 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE; 317 for (i = 0; i < nwritten; i++) { 318 rtvals[i] = VM_PAGER_OK; 319 vm_page_undirty(pages[i]); 320 } 321 if (must_commit) 322 nfs_clearcommit(vp->v_mount); 323 } 324 return rtvals[0]; 325 } 326 327 /* 328 * Vnode op for read using bio 329 */ 330 int 331 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag) 332 { 333 struct nfsnode *np = VTONFS(vp); 334 int biosize, i; 335 struct buf *bp = 0, *rabp; 336 struct vattr vattr; 337 struct thread *td; 338 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 339 daddr_t lbn, rabn; 340 int bcount; 341 int seqcount; 342 int nra, error = 0, n = 0, on = 0; 343 344 #ifdef DIAGNOSTIC 345 if (uio->uio_rw != UIO_READ) 346 panic("nfs_read mode"); 347 #endif 348 if (uio->uio_resid == 0) 349 return (0); 350 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */ 351 return (EINVAL); 352 td = uio->uio_td; 353 354 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 355 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 356 (void)nfs_fsinfo(nmp, vp, td); 357 if (vp->v_type != VDIR && 358 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 359 return (EFBIG); 360 biosize = vp->v_mount->mnt_stat.f_iosize; 361 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE); 362 363 /* 364 * For nfs, cache consistency can only be maintained approximately. 365 * Although RFC1094 does not specify the criteria, the following is 366 * believed to be compatible with the reference port. 367 * 368 * NQNFS: Full cache coherency is maintained within the loop. 369 * 370 * NFS: If local changes have been made and this is a 371 * directory, the directory must be invalidated and 372 * the attribute cache must be cleared. 373 * 374 * GETATTR is called to synchronize the file size. 375 * 376 * If remote changes are detected local data is flushed 377 * and the cache is invalidated. 378 * 379 * 380 * NOTE: In the normal case the attribute cache is not 381 * cleared which means GETATTR may use cached data and 382 * not immediately detect changes made on the server. 383 */ 384 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) { 385 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) { 386 nfs_invaldir(vp); 387 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 388 if (error) 389 return (error); 390 np->n_attrstamp = 0; 391 } 392 error = VOP_GETATTR(vp, &vattr, td); 393 if (error) 394 return (error); 395 if (np->n_flag & NRMODIFIED) { 396 if (vp->v_type == VDIR) 397 nfs_invaldir(vp); 398 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 399 if (error) 400 return (error); 401 np->n_flag &= ~NRMODIFIED; 402 } 403 } 404 do { 405 406 /* 407 * Get a valid lease. If cached data is stale, flush it. 408 */ 409 if (nmp->nm_flag & NFSMNT_NQNFS) { 410 if (NQNFS_CKINVALID(vp, np, ND_READ)) { 411 do { 412 error = nqnfs_getlease(vp, ND_READ, td); 413 } while (error == NQNFS_EXPIRED); 414 if (error) 415 return (error); 416 if (np->n_lrev != np->n_brev || 417 (np->n_flag & NQNFSNONCACHE) || 418 ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR)) { 419 if (vp->v_type == VDIR) 420 nfs_invaldir(vp); 421 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 422 if (error) 423 return (error); 424 np->n_brev = np->n_lrev; 425 } 426 } else if (vp->v_type == VDIR && (np->n_flag & NLMODIFIED)) { 427 nfs_invaldir(vp); 428 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 429 if (error) 430 return (error); 431 } 432 } 433 if (np->n_flag & NQNFSNONCACHE) { 434 switch (vp->v_type) { 435 case VREG: 436 return (nfs_readrpc(vp, uio)); 437 case VLNK: 438 return (nfs_readlinkrpc(vp, uio)); 439 case VDIR: 440 break; 441 default: 442 printf(" NQNFSNONCACHE: type %x unexpected\n", 443 vp->v_type); 444 }; 445 } 446 switch (vp->v_type) { 447 case VREG: 448 nfsstats.biocache_reads++; 449 lbn = uio->uio_offset / biosize; 450 on = uio->uio_offset & (biosize - 1); 451 452 /* 453 * Start the read ahead(s), as required. 454 */ 455 if (nfs_numasync > 0 && nmp->nm_readahead > 0) { 456 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount && 457 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) { 458 rabn = lbn + 1 + nra; 459 if (!incore(vp, rabn)) { 460 rabp = nfs_getcacheblk(vp, rabn, biosize, td); 461 if (!rabp) 462 return (EINTR); 463 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 464 rabp->b_flags |= (B_READ | B_ASYNC); 465 vfs_busy_pages(rabp, 0); 466 if (nfs_asyncio(rabp, td)) { 467 rabp->b_flags |= B_INVAL|B_ERROR; 468 vfs_unbusy_pages(rabp); 469 brelse(rabp); 470 break; 471 } 472 } else { 473 brelse(rabp); 474 } 475 } 476 } 477 } 478 479 /* 480 * Obtain the buffer cache block. Figure out the buffer size 481 * when we are at EOF. If we are modifying the size of the 482 * buffer based on an EOF condition we need to hold 483 * nfs_rslock() through obtaining the buffer to prevent 484 * a potential writer-appender from messing with n_size. 485 * Otherwise we may accidently truncate the buffer and 486 * lose dirty data. 487 * 488 * Note that bcount is *not* DEV_BSIZE aligned. 489 */ 490 491 again: 492 bcount = biosize; 493 if ((off_t)lbn * biosize >= np->n_size) { 494 bcount = 0; 495 } else if ((off_t)(lbn + 1) * biosize > np->n_size) { 496 bcount = np->n_size - (off_t)lbn * biosize; 497 } 498 if (bcount != biosize) { 499 switch(nfs_rslock(np, td)) { 500 case ENOLCK: 501 goto again; 502 /* not reached */ 503 case EINTR: 504 case ERESTART: 505 return(EINTR); 506 /* not reached */ 507 default: 508 break; 509 } 510 } 511 512 bp = nfs_getcacheblk(vp, lbn, bcount, td); 513 514 if (bcount != biosize) 515 nfs_rsunlock(np, td); 516 if (!bp) 517 return (EINTR); 518 519 /* 520 * If B_CACHE is not set, we must issue the read. If this 521 * fails, we return an error. 522 */ 523 524 if ((bp->b_flags & B_CACHE) == 0) { 525 bp->b_flags |= B_READ; 526 vfs_busy_pages(bp, 0); 527 error = nfs_doio(bp, td); 528 if (error) { 529 brelse(bp); 530 return (error); 531 } 532 } 533 534 /* 535 * on is the offset into the current bp. Figure out how many 536 * bytes we can copy out of the bp. Note that bcount is 537 * NOT DEV_BSIZE aligned. 538 * 539 * Then figure out how many bytes we can copy into the uio. 540 */ 541 542 n = 0; 543 if (on < bcount) 544 n = min((unsigned)(bcount - on), uio->uio_resid); 545 break; 546 case VLNK: 547 nfsstats.biocache_readlinks++; 548 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td); 549 if (!bp) 550 return (EINTR); 551 if ((bp->b_flags & B_CACHE) == 0) { 552 bp->b_flags |= B_READ; 553 vfs_busy_pages(bp, 0); 554 error = nfs_doio(bp, td); 555 if (error) { 556 bp->b_flags |= B_ERROR; 557 brelse(bp); 558 return (error); 559 } 560 } 561 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid); 562 on = 0; 563 break; 564 case VDIR: 565 nfsstats.biocache_readdirs++; 566 if (np->n_direofoffset 567 && uio->uio_offset >= np->n_direofoffset) { 568 return (0); 569 } 570 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ; 571 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 572 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td); 573 if (!bp) 574 return (EINTR); 575 if ((bp->b_flags & B_CACHE) == 0) { 576 bp->b_flags |= B_READ; 577 vfs_busy_pages(bp, 0); 578 error = nfs_doio(bp, td); 579 if (error) { 580 brelse(bp); 581 } 582 while (error == NFSERR_BAD_COOKIE) { 583 printf("got bad cookie vp %p bp %p\n", vp, bp); 584 nfs_invaldir(vp); 585 error = nfs_vinvalbuf(vp, 0, td, 1); 586 /* 587 * Yuck! The directory has been modified on the 588 * server. The only way to get the block is by 589 * reading from the beginning to get all the 590 * offset cookies. 591 * 592 * Leave the last bp intact unless there is an error. 593 * Loop back up to the while if the error is another 594 * NFSERR_BAD_COOKIE (double yuch!). 595 */ 596 for (i = 0; i <= lbn && !error; i++) { 597 if (np->n_direofoffset 598 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) 599 return (0); 600 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td); 601 if (!bp) 602 return (EINTR); 603 if ((bp->b_flags & B_CACHE) == 0) { 604 bp->b_flags |= B_READ; 605 vfs_busy_pages(bp, 0); 606 error = nfs_doio(bp, td); 607 /* 608 * no error + B_INVAL == directory EOF, 609 * use the block. 610 */ 611 if (error == 0 && (bp->b_flags & B_INVAL)) 612 break; 613 } 614 /* 615 * An error will throw away the block and the 616 * for loop will break out. If no error and this 617 * is not the block we want, we throw away the 618 * block and go for the next one via the for loop. 619 */ 620 if (error || i < lbn) 621 brelse(bp); 622 } 623 } 624 /* 625 * The above while is repeated if we hit another cookie 626 * error. If we hit an error and it wasn't a cookie error, 627 * we give up. 628 */ 629 if (error) 630 return (error); 631 } 632 633 /* 634 * If not eof and read aheads are enabled, start one. 635 * (You need the current block first, so that you have the 636 * directory offset cookie of the next block.) 637 */ 638 if (nfs_numasync > 0 && nmp->nm_readahead > 0 && 639 (bp->b_flags & B_INVAL) == 0 && 640 (np->n_direofoffset == 0 || 641 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) && 642 !(np->n_flag & NQNFSNONCACHE) && 643 !incore(vp, lbn + 1)) { 644 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td); 645 if (rabp) { 646 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 647 rabp->b_flags |= (B_READ | B_ASYNC); 648 vfs_busy_pages(rabp, 0); 649 if (nfs_asyncio(rabp, td)) { 650 rabp->b_flags |= B_INVAL|B_ERROR; 651 vfs_unbusy_pages(rabp); 652 brelse(rabp); 653 } 654 } else { 655 brelse(rabp); 656 } 657 } 658 } 659 /* 660 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is 661 * chopped for the EOF condition, we cannot tell how large 662 * NFS directories are going to be until we hit EOF. So 663 * an NFS directory buffer is *not* chopped to its EOF. Now, 664 * it just so happens that b_resid will effectively chop it 665 * to EOF. *BUT* this information is lost if the buffer goes 666 * away and is reconstituted into a B_CACHE state ( due to 667 * being VMIO ) later. So we keep track of the directory eof 668 * in np->n_direofoffset and chop it off as an extra step 669 * right here. 670 */ 671 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on); 672 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset) 673 n = np->n_direofoffset - uio->uio_offset; 674 break; 675 default: 676 printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 677 break; 678 }; 679 680 if (n > 0) { 681 error = uiomove(bp->b_data + on, (int)n, uio); 682 } 683 switch (vp->v_type) { 684 case VREG: 685 break; 686 case VLNK: 687 n = 0; 688 break; 689 case VDIR: 690 /* 691 * Invalidate buffer if caching is disabled, forcing a 692 * re-read from the remote later. 693 */ 694 if (np->n_flag & NQNFSNONCACHE) 695 bp->b_flags |= B_INVAL; 696 break; 697 default: 698 printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 699 } 700 brelse(bp); 701 } while (error == 0 && uio->uio_resid > 0 && n > 0); 702 return (error); 703 } 704 705 /* 706 * Vnode op for write using bio 707 * 708 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 709 * struct ucred *a_cred) 710 */ 711 int 712 nfs_write(struct vop_write_args *ap) 713 { 714 int biosize; 715 struct uio *uio = ap->a_uio; 716 struct thread *td = uio->uio_td; 717 struct vnode *vp = ap->a_vp; 718 struct nfsnode *np = VTONFS(vp); 719 int ioflag = ap->a_ioflag; 720 struct buf *bp; 721 struct vattr vattr; 722 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 723 daddr_t lbn; 724 int bcount; 725 int n, on, error = 0, iomode, must_commit; 726 int haverslock = 0; 727 728 #ifdef DIAGNOSTIC 729 if (uio->uio_rw != UIO_WRITE) 730 panic("nfs_write mode"); 731 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread) 732 panic("nfs_write proc"); 733 #endif 734 if (vp->v_type != VREG) 735 return (EIO); 736 if (np->n_flag & NWRITEERR) { 737 np->n_flag &= ~NWRITEERR; 738 return (np->n_error); 739 } 740 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 741 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 742 (void)nfs_fsinfo(nmp, vp, td); 743 744 /* 745 * Synchronously flush pending buffers if we are in synchronous 746 * mode or if we are appending. 747 */ 748 if (ioflag & (IO_APPEND | IO_SYNC)) { 749 if (np->n_flag & NLMODIFIED) { 750 np->n_attrstamp = 0; 751 error = nfs_flush(vp, MNT_WAIT, td, 0); 752 /* error = nfs_vinvalbuf(vp, V_SAVE, td, 1); */ 753 if (error) 754 return (error); 755 } 756 } 757 758 /* 759 * If IO_APPEND then load uio_offset. We restart here if we cannot 760 * get the append lock. 761 */ 762 restart: 763 if (ioflag & IO_APPEND) { 764 np->n_attrstamp = 0; 765 error = VOP_GETATTR(vp, &vattr, td); 766 if (error) 767 return (error); 768 uio->uio_offset = np->n_size; 769 } 770 771 if (uio->uio_offset < 0) 772 return (EINVAL); 773 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 774 return (EFBIG); 775 if (uio->uio_resid == 0) 776 return (0); 777 778 /* 779 * We need to obtain the rslock if we intend to modify np->n_size 780 * in order to guarentee the append point with multiple contending 781 * writers, to guarentee that no other appenders modify n_size 782 * while we are trying to obtain a truncated buffer (i.e. to avoid 783 * accidently truncating data written by another appender due to 784 * the race), and to ensure that the buffer is populated prior to 785 * our extending of the file. We hold rslock through the entire 786 * operation. 787 * 788 * Note that we do not synchronize the case where someone truncates 789 * the file while we are appending to it because attempting to lock 790 * this case may deadlock other parts of the system unexpectedly. 791 */ 792 if ((ioflag & IO_APPEND) || 793 uio->uio_offset + uio->uio_resid > np->n_size) { 794 switch(nfs_rslock(np, td)) { 795 case ENOLCK: 796 goto restart; 797 /* not reached */ 798 case EINTR: 799 case ERESTART: 800 return(EINTR); 801 /* not reached */ 802 default: 803 break; 804 } 805 haverslock = 1; 806 } 807 808 /* 809 * Maybe this should be above the vnode op call, but so long as 810 * file servers have no limits, i don't think it matters 811 */ 812 if (td->td_proc && uio->uio_offset + uio->uio_resid > 813 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 814 psignal(td->td_proc, SIGXFSZ); 815 if (haverslock) 816 nfs_rsunlock(np, td); 817 return (EFBIG); 818 } 819 820 biosize = vp->v_mount->mnt_stat.f_iosize; 821 822 do { 823 /* 824 * Check for a valid write lease. 825 */ 826 if ((nmp->nm_flag & NFSMNT_NQNFS) && 827 NQNFS_CKINVALID(vp, np, ND_WRITE)) { 828 do { 829 error = nqnfs_getlease(vp, ND_WRITE, td); 830 } while (error == NQNFS_EXPIRED); 831 if (error) 832 break; 833 if (np->n_lrev != np->n_brev || 834 (np->n_flag & NQNFSNONCACHE)) { 835 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 836 if (error) 837 break; 838 np->n_brev = np->n_lrev; 839 } 840 } 841 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) { 842 iomode = NFSV3WRITE_FILESYNC; 843 error = nfs_writerpc(vp, uio, &iomode, &must_commit); 844 if (must_commit) 845 nfs_clearcommit(vp->v_mount); 846 break; 847 } 848 nfsstats.biocache_writes++; 849 lbn = uio->uio_offset / biosize; 850 on = uio->uio_offset & (biosize-1); 851 n = min((unsigned)(biosize - on), uio->uio_resid); 852 again: 853 /* 854 * Handle direct append and file extension cases, calculate 855 * unaligned buffer size. 856 */ 857 858 if (uio->uio_offset == np->n_size && n) { 859 /* 860 * Get the buffer (in its pre-append state to maintain 861 * B_CACHE if it was previously set). Resize the 862 * nfsnode after we have locked the buffer to prevent 863 * readers from reading garbage. 864 */ 865 bcount = on; 866 bp = nfs_getcacheblk(vp, lbn, bcount, td); 867 868 if (bp != NULL) { 869 long save; 870 871 np->n_size = uio->uio_offset + n; 872 np->n_flag |= NLMODIFIED; 873 vnode_pager_setsize(vp, np->n_size); 874 875 save = bp->b_flags & B_CACHE; 876 bcount += n; 877 allocbuf(bp, bcount); 878 bp->b_flags |= save; 879 } 880 } else { 881 /* 882 * Obtain the locked cache block first, and then 883 * adjust the file's size as appropriate. 884 */ 885 bcount = on + n; 886 if ((off_t)lbn * biosize + bcount < np->n_size) { 887 if ((off_t)(lbn + 1) * biosize < np->n_size) 888 bcount = biosize; 889 else 890 bcount = np->n_size - (off_t)lbn * biosize; 891 } 892 bp = nfs_getcacheblk(vp, lbn, bcount, td); 893 if (uio->uio_offset + n > np->n_size) { 894 np->n_size = uio->uio_offset + n; 895 np->n_flag |= NLMODIFIED; 896 vnode_pager_setsize(vp, np->n_size); 897 } 898 } 899 900 if (!bp) { 901 error = EINTR; 902 break; 903 } 904 905 /* 906 * Issue a READ if B_CACHE is not set. In special-append 907 * mode, B_CACHE is based on the buffer prior to the write 908 * op and is typically set, avoiding the read. If a read 909 * is required in special append mode, the server will 910 * probably send us a short-read since we extended the file 911 * on our end, resulting in b_resid == 0 and, thusly, 912 * B_CACHE getting set. 913 * 914 * We can also avoid issuing the read if the write covers 915 * the entire buffer. We have to make sure the buffer state 916 * is reasonable in this case since we will not be initiating 917 * I/O. See the comments in kern/vfs_bio.c's getblk() for 918 * more information. 919 * 920 * B_CACHE may also be set due to the buffer being cached 921 * normally. 922 */ 923 924 if (on == 0 && n == bcount) { 925 bp->b_flags |= B_CACHE; 926 bp->b_flags &= ~(B_ERROR | B_INVAL); 927 } 928 929 if ((bp->b_flags & B_CACHE) == 0) { 930 bp->b_flags |= B_READ; 931 vfs_busy_pages(bp, 0); 932 error = nfs_doio(bp, td); 933 if (error) { 934 brelse(bp); 935 break; 936 } 937 } 938 if (!bp) { 939 error = EINTR; 940 break; 941 } 942 np->n_flag |= NLMODIFIED; 943 944 /* 945 * If dirtyend exceeds file size, chop it down. This should 946 * not normally occur but there is an append race where it 947 * might occur XXX, so we log it. 948 * 949 * If the chopping creates a reverse-indexed or degenerate 950 * situation with dirtyoff/end, we 0 both of them. 951 */ 952 953 if (bp->b_dirtyend > bcount) { 954 printf("NFS append race @%lx:%d\n", 955 (long)bp->b_blkno * DEV_BSIZE, 956 bp->b_dirtyend - bcount); 957 bp->b_dirtyend = bcount; 958 } 959 960 if (bp->b_dirtyoff >= bp->b_dirtyend) 961 bp->b_dirtyoff = bp->b_dirtyend = 0; 962 963 /* 964 * If the new write will leave a contiguous dirty 965 * area, just update the b_dirtyoff and b_dirtyend, 966 * otherwise force a write rpc of the old dirty area. 967 * 968 * While it is possible to merge discontiguous writes due to 969 * our having a B_CACHE buffer ( and thus valid read data 970 * for the hole), we don't because it could lead to 971 * significant cache coherency problems with multiple clients, 972 * especially if locking is implemented later on. 973 * 974 * as an optimization we could theoretically maintain 975 * a linked list of discontinuous areas, but we would still 976 * have to commit them separately so there isn't much 977 * advantage to it except perhaps a bit of asynchronization. 978 */ 979 980 if (bp->b_dirtyend > 0 && 981 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 982 if (VOP_BWRITE(bp->b_vp, bp) == EINTR) { 983 error = EINTR; 984 break; 985 } 986 goto again; 987 } 988 989 /* 990 * Check for valid write lease and get one as required. 991 * In case getblk() and/or bwrite() delayed us. 992 */ 993 if ((nmp->nm_flag & NFSMNT_NQNFS) && 994 NQNFS_CKINVALID(vp, np, ND_WRITE)) { 995 do { 996 error = nqnfs_getlease(vp, ND_WRITE, td); 997 } while (error == NQNFS_EXPIRED); 998 if (error) { 999 brelse(bp); 1000 break; 1001 } 1002 if (np->n_lrev != np->n_brev || 1003 (np->n_flag & NQNFSNONCACHE)) { 1004 brelse(bp); 1005 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 1006 if (error) 1007 break; 1008 np->n_brev = np->n_lrev; 1009 goto again; 1010 } 1011 } 1012 1013 error = uiomove((char *)bp->b_data + on, n, uio); 1014 1015 /* 1016 * Since this block is being modified, it must be written 1017 * again and not just committed. Since write clustering does 1018 * not work for the stage 1 data write, only the stage 2 1019 * commit rpc, we have to clear B_CLUSTEROK as well. 1020 */ 1021 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1022 1023 if (error) { 1024 bp->b_flags |= B_ERROR; 1025 brelse(bp); 1026 break; 1027 } 1028 1029 /* 1030 * Only update dirtyoff/dirtyend if not a degenerate 1031 * condition. 1032 */ 1033 if (n) { 1034 if (bp->b_dirtyend > 0) { 1035 bp->b_dirtyoff = min(on, bp->b_dirtyoff); 1036 bp->b_dirtyend = max((on + n), bp->b_dirtyend); 1037 } else { 1038 bp->b_dirtyoff = on; 1039 bp->b_dirtyend = on + n; 1040 } 1041 vfs_bio_set_validclean(bp, on, n); 1042 } 1043 /* 1044 * If IO_NOWDRAIN then set B_NOWDRAIN (e.g. nfs-backed VN 1045 * filesystem). XXX also use for loopback NFS mounts. 1046 */ 1047 if (ioflag & IO_NOWDRAIN) 1048 bp->b_flags |= B_NOWDRAIN; 1049 1050 /* 1051 * If the lease is non-cachable or IO_SYNC do bwrite(). 1052 * 1053 * IO_INVAL appears to be unused. The idea appears to be 1054 * to turn off caching in this case. Very odd. XXX 1055 */ 1056 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) { 1057 if (ioflag & IO_INVAL) 1058 bp->b_flags |= B_NOCACHE; 1059 error = VOP_BWRITE(bp->b_vp, bp); 1060 if (error) 1061 break; 1062 if (np->n_flag & NQNFSNONCACHE) { 1063 error = nfs_vinvalbuf(vp, V_SAVE, td, 1); 1064 if (error) 1065 break; 1066 } 1067 } else if ((n + on) == biosize && 1068 (nmp->nm_flag & NFSMNT_NQNFS) == 0) { 1069 bp->b_flags |= B_ASYNC; 1070 (void)nfs_writebp(bp, 0, 0); 1071 } else { 1072 bdwrite(bp); 1073 } 1074 } while (uio->uio_resid > 0 && n > 0); 1075 1076 if (haverslock) 1077 nfs_rsunlock(np, td); 1078 1079 return (error); 1080 } 1081 1082 /* 1083 * Get an nfs cache block. 1084 * 1085 * Allocate a new one if the block isn't currently in the cache 1086 * and return the block marked busy. If the calling process is 1087 * interrupted by a signal for an interruptible mount point, return 1088 * NULL. 1089 * 1090 * The caller must carefully deal with the possible B_INVAL state of 1091 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it 1092 * indirectly), so synchronous reads can be issued without worrying about 1093 * the B_INVAL state. We have to be a little more careful when dealing 1094 * with writes (see comments in nfs_write()) when extending a file past 1095 * its EOF. 1096 */ 1097 static struct buf * 1098 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td) 1099 { 1100 struct buf *bp; 1101 struct mount *mp; 1102 struct nfsmount *nmp; 1103 1104 mp = vp->v_mount; 1105 nmp = VFSTONFS(mp); 1106 1107 if (nmp->nm_flag & NFSMNT_INT) { 1108 bp = getblk(vp, bn, size, PCATCH, 0); 1109 while (bp == (struct buf *)0) { 1110 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) 1111 return ((struct buf *)0); 1112 bp = getblk(vp, bn, size, 0, 2 * hz); 1113 } 1114 } else { 1115 bp = getblk(vp, bn, size, 0, 0); 1116 } 1117 1118 if (vp->v_type == VREG) { 1119 int biosize; 1120 1121 biosize = mp->mnt_stat.f_iosize; 1122 bp->b_blkno = bn * (biosize / DEV_BSIZE); 1123 } 1124 return (bp); 1125 } 1126 1127 /* 1128 * Flush and invalidate all dirty buffers. If another process is already 1129 * doing the flush, just wait for completion. 1130 */ 1131 int 1132 nfs_vinvalbuf(struct vnode *vp, int flags, 1133 struct thread *td, int intrflg) 1134 { 1135 struct nfsnode *np = VTONFS(vp); 1136 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1137 int error = 0, slpflag, slptimeo; 1138 1139 if (vp->v_flag & VRECLAIMED) 1140 return (0); 1141 1142 if ((nmp->nm_flag & NFSMNT_INT) == 0) 1143 intrflg = 0; 1144 if (intrflg) { 1145 slpflag = PCATCH; 1146 slptimeo = 2 * hz; 1147 } else { 1148 slpflag = 0; 1149 slptimeo = 0; 1150 } 1151 /* 1152 * First wait for any other process doing a flush to complete. 1153 */ 1154 while (np->n_flag & NFLUSHINPROG) { 1155 np->n_flag |= NFLUSHWANT; 1156 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo); 1157 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, td)) 1158 return (EINTR); 1159 } 1160 1161 /* 1162 * Now, flush as required. 1163 */ 1164 np->n_flag |= NFLUSHINPROG; 1165 error = vinvalbuf(vp, flags, td, slpflag, 0); 1166 while (error) { 1167 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, td)) { 1168 np->n_flag &= ~NFLUSHINPROG; 1169 if (np->n_flag & NFLUSHWANT) { 1170 np->n_flag &= ~NFLUSHWANT; 1171 wakeup((caddr_t)&np->n_flag); 1172 } 1173 return (EINTR); 1174 } 1175 error = vinvalbuf(vp, flags, td, 0, slptimeo); 1176 } 1177 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG); 1178 if (np->n_flag & NFLUSHWANT) { 1179 np->n_flag &= ~NFLUSHWANT; 1180 wakeup((caddr_t)&np->n_flag); 1181 } 1182 return (0); 1183 } 1184 1185 /* 1186 * Initiate asynchronous I/O. Return an error if no nfsiods are available. 1187 * This is mainly to avoid queueing async I/O requests when the nfsiods 1188 * are all hung on a dead server. 1189 * 1190 * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp 1191 * is eventually dequeued by the async daemon, nfs_doio() *will*. 1192 */ 1193 int 1194 nfs_asyncio(struct buf *bp, struct thread *td) 1195 { 1196 struct nfsmount *nmp; 1197 int i; 1198 int gotiod; 1199 int slpflag = 0; 1200 int slptimeo = 0; 1201 int error; 1202 1203 /* 1204 * If no async daemons then return EIO to force caller to run the rpc 1205 * synchronously. 1206 */ 1207 if (nfs_numasync == 0) 1208 return (EIO); 1209 1210 nmp = VFSTONFS(bp->b_vp->v_mount); 1211 1212 /* 1213 * Commits are usually short and sweet so lets save some cpu and 1214 * leave the async daemons for more important rpc's (such as reads 1215 * and writes). 1216 */ 1217 if ((bp->b_flags & (B_READ|B_NEEDCOMMIT)) == B_NEEDCOMMIT && 1218 (nmp->nm_bufqiods > nfs_numasync / 2)) { 1219 return(EIO); 1220 } 1221 1222 again: 1223 if (nmp->nm_flag & NFSMNT_INT) 1224 slpflag = PCATCH; 1225 gotiod = FALSE; 1226 1227 /* 1228 * Find a free iod to process this request. 1229 */ 1230 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 1231 if (nfs_iodwant[i]) { 1232 /* 1233 * Found one, so wake it up and tell it which 1234 * mount to process. 1235 */ 1236 NFS_DPF(ASYNCIO, 1237 ("nfs_asyncio: waking iod %d for mount %p\n", 1238 i, nmp)); 1239 nfs_iodwant[i] = NULL; 1240 nfs_iodmount[i] = nmp; 1241 nmp->nm_bufqiods++; 1242 wakeup((caddr_t)&nfs_iodwant[i]); 1243 gotiod = TRUE; 1244 break; 1245 } 1246 1247 /* 1248 * If none are free, we may already have an iod working on this mount 1249 * point. If so, it will process our request. 1250 */ 1251 if (!gotiod) { 1252 if (nmp->nm_bufqiods > 0) { 1253 NFS_DPF(ASYNCIO, 1254 ("nfs_asyncio: %d iods are already processing mount %p\n", 1255 nmp->nm_bufqiods, nmp)); 1256 gotiod = TRUE; 1257 } 1258 } 1259 1260 /* 1261 * If we have an iod which can process the request, then queue 1262 * the buffer. 1263 */ 1264 if (gotiod) { 1265 /* 1266 * Ensure that the queue never grows too large. We still want 1267 * to asynchronize so we block rather then return EIO. 1268 */ 1269 while (nmp->nm_bufqlen >= 2*nfs_numasync) { 1270 NFS_DPF(ASYNCIO, 1271 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp)); 1272 nmp->nm_bufqwant = TRUE; 1273 error = tsleep(&nmp->nm_bufq, slpflag, 1274 "nfsaio", slptimeo); 1275 if (error) { 1276 if (nfs_sigintr(nmp, NULL, td)) 1277 return (EINTR); 1278 if (slpflag == PCATCH) { 1279 slpflag = 0; 1280 slptimeo = 2 * hz; 1281 } 1282 } 1283 /* 1284 * We might have lost our iod while sleeping, 1285 * so check and loop if nescessary. 1286 */ 1287 if (nmp->nm_bufqiods == 0) { 1288 NFS_DPF(ASYNCIO, 1289 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 1290 goto again; 1291 } 1292 } 1293 BUF_KERNPROC(bp); 1294 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 1295 nmp->nm_bufqlen++; 1296 return (0); 1297 } 1298 1299 /* 1300 * All the iods are busy on other mounts, so return EIO to 1301 * force the caller to process the i/o synchronously. 1302 */ 1303 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n")); 1304 return (EIO); 1305 } 1306 1307 /* 1308 * Do an I/O operation to/from a cache block. This may be called 1309 * synchronously or from an nfsiod. 1310 * 1311 * NOTE! TD MIGHT BE NULL 1312 */ 1313 int 1314 nfs_doio(struct buf *bp, struct thread *td) 1315 { 1316 struct uio *uiop; 1317 struct vnode *vp; 1318 struct nfsnode *np; 1319 struct nfsmount *nmp; 1320 int error = 0, iomode, must_commit = 0; 1321 struct uio uio; 1322 struct iovec io; 1323 1324 vp = bp->b_vp; 1325 np = VTONFS(vp); 1326 nmp = VFSTONFS(vp->v_mount); 1327 uiop = &uio; 1328 uiop->uio_iov = &io; 1329 uiop->uio_iovcnt = 1; 1330 uiop->uio_segflg = UIO_SYSSPACE; 1331 uiop->uio_td = td; 1332 1333 /* 1334 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We 1335 * do this here so we do not have to do it in all the code that 1336 * calls us. 1337 */ 1338 bp->b_flags &= ~(B_ERROR | B_INVAL); 1339 1340 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp)); 1341 1342 /* 1343 * Historically, paging was done with physio, but no more. 1344 */ 1345 if (bp->b_flags & B_PHYS) { 1346 /* 1347 * ...though reading /dev/drum still gets us here. 1348 */ 1349 io.iov_len = uiop->uio_resid = bp->b_bcount; 1350 /* mapping was done by vmapbuf() */ 1351 io.iov_base = bp->b_data; 1352 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1353 if (bp->b_flags & B_READ) { 1354 uiop->uio_rw = UIO_READ; 1355 nfsstats.read_physios++; 1356 error = nfs_readrpc(vp, uiop); 1357 } else { 1358 int com; 1359 1360 iomode = NFSV3WRITE_DATASYNC; 1361 uiop->uio_rw = UIO_WRITE; 1362 nfsstats.write_physios++; 1363 error = nfs_writerpc(vp, uiop, &iomode, &com); 1364 } 1365 if (error) { 1366 bp->b_flags |= B_ERROR; 1367 bp->b_error = error; 1368 } 1369 } else if (bp->b_flags & B_READ) { 1370 io.iov_len = uiop->uio_resid = bp->b_bcount; 1371 io.iov_base = bp->b_data; 1372 uiop->uio_rw = UIO_READ; 1373 1374 switch (vp->v_type) { 1375 case VREG: 1376 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1377 nfsstats.read_bios++; 1378 error = nfs_readrpc(vp, uiop); 1379 1380 if (!error) { 1381 if (uiop->uio_resid) { 1382 /* 1383 * If we had a short read with no error, we must have 1384 * hit a file hole. We should zero-fill the remainder. 1385 * This can also occur if the server hits the file EOF. 1386 * 1387 * Holes used to be able to occur due to pending 1388 * writes, but that is not possible any longer. 1389 */ 1390 int nread = bp->b_bcount - uiop->uio_resid; 1391 int left = uiop->uio_resid; 1392 1393 if (left > 0) 1394 bzero((char *)bp->b_data + nread, left); 1395 uiop->uio_resid = 0; 1396 } 1397 } 1398 if (td && td->td_proc && (vp->v_flag & VTEXT) && 1399 (((nmp->nm_flag & NFSMNT_NQNFS) && 1400 NQNFS_CKINVALID(vp, np, ND_READ) && 1401 np->n_lrev != np->n_brev) || 1402 (!(nmp->nm_flag & NFSMNT_NQNFS) && 1403 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) { 1404 uprintf("Process killed due to text file modification\n"); 1405 psignal(td->td_proc, SIGKILL); 1406 PHOLD(td->td_proc); 1407 } 1408 break; 1409 case VLNK: 1410 uiop->uio_offset = (off_t)0; 1411 nfsstats.readlink_bios++; 1412 error = nfs_readlinkrpc(vp, uiop); 1413 break; 1414 case VDIR: 1415 nfsstats.readdir_bios++; 1416 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 1417 if (nmp->nm_flag & NFSMNT_RDIRPLUS) { 1418 error = nfs_readdirplusrpc(vp, uiop); 1419 if (error == NFSERR_NOTSUPP) 1420 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 1421 } 1422 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 1423 error = nfs_readdirrpc(vp, uiop); 1424 /* 1425 * end-of-directory sets B_INVAL but does not generate an 1426 * error. 1427 */ 1428 if (error == 0 && uiop->uio_resid == bp->b_bcount) 1429 bp->b_flags |= B_INVAL; 1430 break; 1431 default: 1432 printf("nfs_doio: type %x unexpected\n",vp->v_type); 1433 break; 1434 }; 1435 if (error) { 1436 bp->b_flags |= B_ERROR; 1437 bp->b_error = error; 1438 } 1439 } else { 1440 /* 1441 * If we only need to commit, try to commit 1442 */ 1443 if (bp->b_flags & B_NEEDCOMMIT) { 1444 int retv; 1445 off_t off; 1446 1447 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff; 1448 retv = nfs_commit(bp->b_vp, off, 1449 bp->b_dirtyend - bp->b_dirtyoff, td); 1450 if (retv == 0) { 1451 bp->b_dirtyoff = bp->b_dirtyend = 0; 1452 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1453 bp->b_resid = 0; 1454 biodone(bp); 1455 return (0); 1456 } 1457 if (retv == NFSERR_STALEWRITEVERF) { 1458 nfs_clearcommit(bp->b_vp->v_mount); 1459 } 1460 } 1461 1462 /* 1463 * Setup for actual write 1464 */ 1465 1466 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size) 1467 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE; 1468 1469 if (bp->b_dirtyend > bp->b_dirtyoff) { 1470 io.iov_len = uiop->uio_resid = bp->b_dirtyend 1471 - bp->b_dirtyoff; 1472 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE 1473 + bp->b_dirtyoff; 1474 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 1475 uiop->uio_rw = UIO_WRITE; 1476 nfsstats.write_bios++; 1477 1478 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 1479 iomode = NFSV3WRITE_UNSTABLE; 1480 else 1481 iomode = NFSV3WRITE_FILESYNC; 1482 1483 error = nfs_writerpc(vp, uiop, &iomode, &must_commit); 1484 1485 /* 1486 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try 1487 * to cluster the buffers needing commit. This will allow 1488 * the system to submit a single commit rpc for the whole 1489 * cluster. We can do this even if the buffer is not 100% 1490 * dirty (relative to the NFS blocksize), so we optimize the 1491 * append-to-file-case. 1492 * 1493 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be 1494 * cleared because write clustering only works for commit 1495 * rpc's, not for the data portion of the write). 1496 */ 1497 1498 if (!error && iomode == NFSV3WRITE_UNSTABLE) { 1499 bp->b_flags |= B_NEEDCOMMIT; 1500 if (bp->b_dirtyoff == 0 1501 && bp->b_dirtyend == bp->b_bcount) 1502 bp->b_flags |= B_CLUSTEROK; 1503 } else { 1504 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1505 } 1506 1507 /* 1508 * For an interrupted write, the buffer is still valid 1509 * and the write hasn't been pushed to the server yet, 1510 * so we can't set B_ERROR and report the interruption 1511 * by setting B_EINTR. For the B_ASYNC case, B_EINTR 1512 * is not relevant, so the rpc attempt is essentially 1513 * a noop. For the case of a V3 write rpc not being 1514 * committed to stable storage, the block is still 1515 * dirty and requires either a commit rpc or another 1516 * write rpc with iomode == NFSV3WRITE_FILESYNC before 1517 * the block is reused. This is indicated by setting 1518 * the B_DELWRI and B_NEEDCOMMIT flags. 1519 * 1520 * If the buffer is marked B_PAGING, it does not reside on 1521 * the vp's paging queues so we cannot call bdirty(). The 1522 * bp in this case is not an NFS cache block so we should 1523 * be safe. XXX 1524 */ 1525 if (error == EINTR 1526 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 1527 int s; 1528 1529 s = splbio(); 1530 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 1531 if ((bp->b_flags & B_PAGING) == 0) { 1532 bdirty(bp); 1533 bp->b_flags &= ~B_DONE; 1534 } 1535 if (error && (bp->b_flags & B_ASYNC) == 0) 1536 bp->b_flags |= B_EINTR; 1537 splx(s); 1538 } else { 1539 if (error) { 1540 bp->b_flags |= B_ERROR; 1541 bp->b_error = np->n_error = error; 1542 np->n_flag |= NWRITEERR; 1543 } 1544 bp->b_dirtyoff = bp->b_dirtyend = 0; 1545 } 1546 } else { 1547 bp->b_resid = 0; 1548 biodone(bp); 1549 return (0); 1550 } 1551 } 1552 bp->b_resid = uiop->uio_resid; 1553 if (must_commit) 1554 nfs_clearcommit(vp->v_mount); 1555 biodone(bp); 1556 return (error); 1557 } 1558 1559 /* 1560 * Used to aid in handling ftruncate() operations on the NFS client side. 1561 * Truncation creates a number of special problems for NFS. We have to 1562 * throw away VM pages and buffer cache buffers that are beyond EOF, and 1563 * we have to properly handle VM pages or (potentially dirty) buffers 1564 * that straddle the truncation point. 1565 */ 1566 1567 int 1568 nfs_meta_setsize(struct vnode *vp, struct thread *td, u_quad_t nsize) 1569 { 1570 struct nfsnode *np = VTONFS(vp); 1571 u_quad_t tsize = np->n_size; 1572 int biosize = vp->v_mount->mnt_stat.f_iosize; 1573 int error = 0; 1574 1575 np->n_size = nsize; 1576 1577 if (np->n_size < tsize) { 1578 struct buf *bp; 1579 daddr_t lbn; 1580 int bufsize; 1581 1582 /* 1583 * vtruncbuf() doesn't get the buffer overlapping the 1584 * truncation point. We may have a B_DELWRI and/or B_CACHE 1585 * buffer that now needs to be truncated. 1586 */ 1587 error = vtruncbuf(vp, td, nsize, biosize); 1588 lbn = nsize / biosize; 1589 bufsize = nsize & (biosize - 1); 1590 bp = nfs_getcacheblk(vp, lbn, bufsize, td); 1591 if (bp->b_dirtyoff > bp->b_bcount) 1592 bp->b_dirtyoff = bp->b_bcount; 1593 if (bp->b_dirtyend > bp->b_bcount) 1594 bp->b_dirtyend = bp->b_bcount; 1595 bp->b_flags |= B_RELBUF; /* don't leave garbage around */ 1596 brelse(bp); 1597 } else { 1598 vnode_pager_setsize(vp, nsize); 1599 } 1600 return(error); 1601 } 1602 1603