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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95 33 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_bio.c,v 1.130 2004/04/14 23:23:55 peadar Exp $ 34 */ 35 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/resourcevar.h> 40 #include <sys/signalvar.h> 41 #include <sys/proc.h> 42 #include <sys/buf.h> 43 #include <sys/vnode.h> 44 #include <sys/mount.h> 45 #include <sys/kernel.h> 46 #include <sys/mbuf.h> 47 48 #include <vm/vm.h> 49 #include <vm/vm_extern.h> 50 #include <vm/vm_page.h> 51 #include <vm/vm_object.h> 52 #include <vm/vm_pager.h> 53 #include <vm/vnode_pager.h> 54 55 #include <sys/buf2.h> 56 #include <sys/thread2.h> 57 #include <vm/vm_page2.h> 58 59 #include "rpcv2.h" 60 #include "nfsproto.h" 61 #include "nfs.h" 62 #include "nfsmount.h" 63 #include "nfsnode.h" 64 #include "xdr_subs.h" 65 #include "nfsm_subs.h" 66 67 68 static struct buf *nfs_getcacheblk(struct vnode *vp, off_t loffset, 69 int size, struct thread *td); 70 static int nfs_check_dirent(struct nfs_dirent *dp, int maxlen); 71 static void nfsiodone_sync(struct bio *bio); 72 static void nfs_readrpc_bio_done(nfsm_info_t info); 73 static void nfs_writerpc_bio_done(nfsm_info_t info); 74 static void nfs_commitrpc_bio_done(nfsm_info_t info); 75 76 /* 77 * Vnode op for read using bio 78 */ 79 int 80 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag) 81 { 82 struct nfsnode *np = VTONFS(vp); 83 int biosize, i; 84 struct buf *bp, *rabp; 85 struct vattr vattr; 86 struct thread *td; 87 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 88 off_t lbn, rabn; 89 off_t raoffset; 90 off_t loffset; 91 int seqcount; 92 int nra, error = 0; 93 int boff = 0; 94 size_t n; 95 96 #ifdef DIAGNOSTIC 97 if (uio->uio_rw != UIO_READ) 98 panic("nfs_read mode"); 99 #endif 100 if (uio->uio_resid == 0) 101 return (0); 102 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */ 103 return (EINVAL); 104 td = uio->uio_td; 105 106 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 107 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 108 (void)nfs_fsinfo(nmp, vp, td); 109 if (vp->v_type != VDIR && 110 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 111 return (EFBIG); 112 biosize = vp->v_mount->mnt_stat.f_iosize; 113 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE); 114 115 /* 116 * For nfs, cache consistency can only be maintained approximately. 117 * Although RFC1094 does not specify the criteria, the following is 118 * believed to be compatible with the reference port. 119 * 120 * NFS: If local changes have been made and this is a 121 * directory, the directory must be invalidated and 122 * the attribute cache must be cleared. 123 * 124 * GETATTR is called to synchronize the file size. 125 * 126 * If remote changes are detected local data is flushed 127 * and the cache is invalidated. 128 * 129 * NOTE: In the normal case the attribute cache is not 130 * cleared which means GETATTR may use cached data and 131 * not immediately detect changes made on the server. 132 */ 133 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) { 134 nfs_invaldir(vp); 135 error = nfs_vinvalbuf(vp, V_SAVE, 1); 136 if (error) 137 return (error); 138 np->n_attrstamp = 0; 139 } 140 error = VOP_GETATTR(vp, &vattr); 141 if (error) 142 return (error); 143 144 /* 145 * This can deadlock getpages/putpages for regular 146 * files. Only do it for directories. 147 */ 148 if (np->n_flag & NRMODIFIED) { 149 if (vp->v_type == VDIR) { 150 nfs_invaldir(vp); 151 error = nfs_vinvalbuf(vp, V_SAVE, 1); 152 if (error) 153 return (error); 154 np->n_flag &= ~NRMODIFIED; 155 } 156 } 157 158 /* 159 * Loop until uio exhausted or we hit EOF 160 */ 161 do { 162 bp = NULL; 163 164 switch (vp->v_type) { 165 case VREG: 166 nfsstats.biocache_reads++; 167 lbn = uio->uio_offset / biosize; 168 boff = uio->uio_offset & (biosize - 1); 169 loffset = lbn * biosize; 170 171 /* 172 * Start the read ahead(s), as required. 173 */ 174 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) { 175 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount && 176 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) { 177 rabn = lbn + 1 + nra; 178 raoffset = rabn * biosize; 179 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) { 180 rabp = nfs_getcacheblk(vp, raoffset, biosize, td); 181 if (!rabp) 182 return (EINTR); 183 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 184 rabp->b_cmd = BUF_CMD_READ; 185 vfs_busy_pages(vp, rabp); 186 nfs_asyncio(vp, &rabp->b_bio2); 187 } else { 188 brelse(rabp); 189 } 190 } 191 } 192 } 193 194 /* 195 * Obtain the buffer cache block. Figure out the buffer size 196 * when we are at EOF. If we are modifying the size of the 197 * buffer based on an EOF condition we need to hold 198 * nfs_rslock() through obtaining the buffer to prevent 199 * a potential writer-appender from messing with n_size. 200 * Otherwise we may accidently truncate the buffer and 201 * lose dirty data. 202 * 203 * Note that bcount is *not* DEV_BSIZE aligned. 204 */ 205 if (loffset + boff >= np->n_size) { 206 n = 0; 207 break; 208 } 209 bp = nfs_getcacheblk(vp, loffset, biosize, td); 210 211 if (bp == NULL) 212 return (EINTR); 213 214 /* 215 * If B_CACHE is not set, we must issue the read. If this 216 * fails, we return an error. 217 */ 218 if ((bp->b_flags & B_CACHE) == 0) { 219 bp->b_cmd = BUF_CMD_READ; 220 bp->b_bio2.bio_done = nfsiodone_sync; 221 bp->b_bio2.bio_flags |= BIO_SYNC; 222 vfs_busy_pages(vp, bp); 223 error = nfs_doio(vp, &bp->b_bio2, td); 224 if (error) { 225 brelse(bp); 226 return (error); 227 } 228 } 229 230 /* 231 * on is the offset into the current bp. Figure out how many 232 * bytes we can copy out of the bp. Note that bcount is 233 * NOT DEV_BSIZE aligned. 234 * 235 * Then figure out how many bytes we can copy into the uio. 236 */ 237 n = biosize - boff; 238 if (n > uio->uio_resid) 239 n = uio->uio_resid; 240 if (loffset + boff + n > np->n_size) 241 n = np->n_size - loffset - boff; 242 break; 243 case VLNK: 244 biosize = min(NFS_MAXPATHLEN, np->n_size); 245 nfsstats.biocache_readlinks++; 246 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td); 247 if (bp == NULL) 248 return (EINTR); 249 if ((bp->b_flags & B_CACHE) == 0) { 250 bp->b_cmd = BUF_CMD_READ; 251 bp->b_bio2.bio_done = nfsiodone_sync; 252 bp->b_bio2.bio_flags |= BIO_SYNC; 253 vfs_busy_pages(vp, bp); 254 error = nfs_doio(vp, &bp->b_bio2, td); 255 if (error) { 256 bp->b_flags |= B_ERROR | B_INVAL; 257 brelse(bp); 258 return (error); 259 } 260 } 261 n = szmin(uio->uio_resid, (size_t)bp->b_bcount - bp->b_resid); 262 boff = 0; 263 break; 264 case VDIR: 265 nfsstats.biocache_readdirs++; 266 if (np->n_direofoffset && 267 uio->uio_offset >= np->n_direofoffset 268 ) { 269 return (0); 270 } 271 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ; 272 boff = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 273 loffset = uio->uio_offset - boff; 274 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td); 275 if (bp == NULL) 276 return (EINTR); 277 278 if ((bp->b_flags & B_CACHE) == 0) { 279 bp->b_cmd = BUF_CMD_READ; 280 bp->b_bio2.bio_done = nfsiodone_sync; 281 bp->b_bio2.bio_flags |= BIO_SYNC; 282 vfs_busy_pages(vp, bp); 283 error = nfs_doio(vp, &bp->b_bio2, td); 284 if (error) 285 brelse(bp); 286 while (error == NFSERR_BAD_COOKIE) { 287 kprintf("got bad cookie vp %p bp %p\n", vp, bp); 288 nfs_invaldir(vp); 289 error = nfs_vinvalbuf(vp, 0, 1); 290 /* 291 * Yuck! The directory has been modified on the 292 * server. The only way to get the block is by 293 * reading from the beginning to get all the 294 * offset cookies. 295 * 296 * Leave the last bp intact unless there is an error. 297 * Loop back up to the while if the error is another 298 * NFSERR_BAD_COOKIE (double yuch!). 299 */ 300 for (i = 0; i <= lbn && !error; i++) { 301 if (np->n_direofoffset 302 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) 303 return (0); 304 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ, 305 NFS_DIRBLKSIZ, td); 306 if (!bp) 307 return (EINTR); 308 if ((bp->b_flags & B_CACHE) == 0) { 309 bp->b_cmd = BUF_CMD_READ; 310 bp->b_bio2.bio_done = nfsiodone_sync; 311 bp->b_bio2.bio_flags |= BIO_SYNC; 312 vfs_busy_pages(vp, bp); 313 error = nfs_doio(vp, &bp->b_bio2, td); 314 /* 315 * no error + B_INVAL == directory EOF, 316 * use the block. 317 */ 318 if (error == 0 && (bp->b_flags & B_INVAL)) 319 break; 320 } 321 /* 322 * An error will throw away the block and the 323 * for loop will break out. If no error and this 324 * is not the block we want, we throw away the 325 * block and go for the next one via the for loop. 326 */ 327 if (error || i < lbn) 328 brelse(bp); 329 } 330 } 331 /* 332 * The above while is repeated if we hit another cookie 333 * error. If we hit an error and it wasn't a cookie error, 334 * we give up. 335 */ 336 if (error) 337 return (error); 338 } 339 340 /* 341 * If not eof and read aheads are enabled, start one. 342 * (You need the current block first, so that you have the 343 * directory offset cookie of the next block.) 344 */ 345 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) && 346 (bp->b_flags & B_INVAL) == 0 && 347 (np->n_direofoffset == 0 || 348 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) && 349 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL 350 ) { 351 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ, 352 NFS_DIRBLKSIZ, td); 353 if (rabp) { 354 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 355 rabp->b_cmd = BUF_CMD_READ; 356 vfs_busy_pages(vp, rabp); 357 nfs_asyncio(vp, &rabp->b_bio2); 358 } else { 359 brelse(rabp); 360 } 361 } 362 } 363 /* 364 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is 365 * chopped for the EOF condition, we cannot tell how large 366 * NFS directories are going to be until we hit EOF. So 367 * an NFS directory buffer is *not* chopped to its EOF. Now, 368 * it just so happens that b_resid will effectively chop it 369 * to EOF. *BUT* this information is lost if the buffer goes 370 * away and is reconstituted into a B_CACHE state ( due to 371 * being VMIO ) later. So we keep track of the directory eof 372 * in np->n_direofoffset and chop it off as an extra step 373 * right here. 374 * 375 * NOTE: boff could already be beyond EOF. 376 */ 377 if ((size_t)boff > NFS_DIRBLKSIZ - bp->b_resid) { 378 n = 0; 379 } else { 380 n = szmin(uio->uio_resid, 381 NFS_DIRBLKSIZ - bp->b_resid - (size_t)boff); 382 } 383 if (np->n_direofoffset && 384 n > (size_t)(np->n_direofoffset - uio->uio_offset)) { 385 n = (size_t)(np->n_direofoffset - uio->uio_offset); 386 } 387 break; 388 default: 389 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type); 390 n = 0; 391 break; 392 } 393 394 switch (vp->v_type) { 395 case VREG: 396 if (n > 0) 397 error = uiomovebp(bp, bp->b_data + boff, n, uio); 398 break; 399 case VLNK: 400 if (n > 0) 401 error = uiomovebp(bp, bp->b_data + boff, n, uio); 402 n = 0; 403 break; 404 case VDIR: 405 if (n > 0) { 406 off_t old_off = uio->uio_offset; 407 caddr_t cpos, epos; 408 struct nfs_dirent *dp; 409 410 /* 411 * We are casting cpos to nfs_dirent, it must be 412 * int-aligned. 413 */ 414 if (boff & 3) { 415 error = EINVAL; 416 break; 417 } 418 419 cpos = bp->b_data + boff; 420 epos = bp->b_data + boff + n; 421 while (cpos < epos && error == 0 && uio->uio_resid > 0) { 422 dp = (struct nfs_dirent *)cpos; 423 error = nfs_check_dirent(dp, (int)(epos - cpos)); 424 if (error) 425 break; 426 if (vop_write_dirent(&error, uio, dp->nfs_ino, 427 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) { 428 break; 429 } 430 cpos += dp->nfs_reclen; 431 } 432 n = 0; 433 if (error == 0) { 434 uio->uio_offset = old_off + cpos - 435 bp->b_data - boff; 436 } 437 } 438 break; 439 default: 440 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type); 441 } 442 if (bp) 443 brelse(bp); 444 } while (error == 0 && uio->uio_resid > 0 && n > 0); 445 return (error); 446 } 447 448 /* 449 * Userland can supply any 'seek' offset when reading a NFS directory. 450 * Validate the structure so we don't panic the kernel. Note that 451 * the element name is nul terminated and the nul is not included 452 * in nfs_namlen. 453 */ 454 static 455 int 456 nfs_check_dirent(struct nfs_dirent *dp, int maxlen) 457 { 458 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]); 459 460 if (nfs_name_off >= maxlen) 461 return (EINVAL); 462 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen) 463 return (EINVAL); 464 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen) 465 return (EINVAL); 466 if (dp->nfs_reclen & 3) 467 return (EINVAL); 468 return (0); 469 } 470 471 /* 472 * Vnode op for write using bio 473 * 474 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 475 * struct ucred *a_cred) 476 */ 477 int 478 nfs_write(struct vop_write_args *ap) 479 { 480 struct uio *uio = ap->a_uio; 481 struct thread *td = uio->uio_td; 482 struct vnode *vp = ap->a_vp; 483 struct nfsnode *np = VTONFS(vp); 484 int ioflag = ap->a_ioflag; 485 struct buf *bp; 486 struct vattr vattr; 487 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 488 off_t loffset; 489 int boff, bytes; 490 int error = 0; 491 int haverslock = 0; 492 int bcount; 493 int biosize; 494 int trivial; 495 496 #ifdef DIAGNOSTIC 497 if (uio->uio_rw != UIO_WRITE) 498 panic("nfs_write mode"); 499 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread) 500 panic("nfs_write proc"); 501 #endif 502 if (vp->v_type != VREG) 503 return (EIO); 504 505 lwkt_gettoken(&nmp->nm_token); 506 507 if (np->n_flag & NWRITEERR) { 508 np->n_flag &= ~NWRITEERR; 509 lwkt_reltoken(&nmp->nm_token); 510 return (np->n_error); 511 } 512 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 513 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 514 (void)nfs_fsinfo(nmp, vp, td); 515 } 516 517 /* 518 * Synchronously flush pending buffers if we are in synchronous 519 * mode or if we are appending. 520 */ 521 if (ioflag & (IO_APPEND | IO_SYNC)) { 522 if (np->n_flag & NLMODIFIED) { 523 np->n_attrstamp = 0; 524 error = nfs_flush(vp, MNT_WAIT, td, 0); 525 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */ 526 if (error) 527 goto done; 528 } 529 } 530 531 /* 532 * If IO_APPEND then load uio_offset. We restart here if we cannot 533 * get the append lock. 534 */ 535 restart: 536 if (ioflag & IO_APPEND) { 537 np->n_attrstamp = 0; 538 error = VOP_GETATTR(vp, &vattr); 539 if (error) 540 goto done; 541 uio->uio_offset = np->n_size; 542 } 543 544 if (uio->uio_offset < 0) { 545 error = EINVAL; 546 goto done; 547 } 548 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) { 549 error = EFBIG; 550 goto done; 551 } 552 if (uio->uio_resid == 0) { 553 error = 0; 554 goto done; 555 } 556 557 /* 558 * We need to obtain the rslock if we intend to modify np->n_size 559 * in order to guarentee the append point with multiple contending 560 * writers, to guarentee that no other appenders modify n_size 561 * while we are trying to obtain a truncated buffer (i.e. to avoid 562 * accidently truncating data written by another appender due to 563 * the race), and to ensure that the buffer is populated prior to 564 * our extending of the file. We hold rslock through the entire 565 * operation. 566 * 567 * Note that we do not synchronize the case where someone truncates 568 * the file while we are appending to it because attempting to lock 569 * this case may deadlock other parts of the system unexpectedly. 570 */ 571 if ((ioflag & IO_APPEND) || 572 uio->uio_offset + uio->uio_resid > np->n_size) { 573 switch(nfs_rslock(np)) { 574 case ENOLCK: 575 goto restart; 576 /* not reached */ 577 case EINTR: 578 case ERESTART: 579 error = EINTR; 580 goto done; 581 /* not reached */ 582 default: 583 break; 584 } 585 haverslock = 1; 586 } 587 588 /* 589 * Maybe this should be above the vnode op call, but so long as 590 * file servers have no limits, i don't think it matters 591 */ 592 if (td && td->td_proc && uio->uio_offset + uio->uio_resid > 593 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 594 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ); 595 if (haverslock) 596 nfs_rsunlock(np); 597 error = EFBIG; 598 goto done; 599 } 600 601 biosize = vp->v_mount->mnt_stat.f_iosize; 602 603 do { 604 nfsstats.biocache_writes++; 605 boff = uio->uio_offset & (biosize-1); 606 loffset = uio->uio_offset - boff; 607 bytes = (int)szmin((unsigned)(biosize - boff), uio->uio_resid); 608 again: 609 /* 610 * Handle direct append and file extension cases, calculate 611 * unaligned buffer size. When extending B_CACHE will be 612 * set if possible. See UIO_NOCOPY note below. 613 */ 614 if (uio->uio_offset + bytes > np->n_size) { 615 np->n_flag |= NLMODIFIED; 616 trivial = (uio->uio_segflg != UIO_NOCOPY && 617 uio->uio_offset <= np->n_size); 618 nfs_meta_setsize(vp, td, uio->uio_offset + bytes, 619 trivial); 620 } 621 bp = nfs_getcacheblk(vp, loffset, biosize, td); 622 if (bp == NULL) { 623 error = EINTR; 624 break; 625 } 626 627 /* 628 * Actual bytes in buffer which we care about 629 */ 630 if (loffset + biosize < np->n_size) 631 bcount = biosize; 632 else 633 bcount = (int)(np->n_size - loffset); 634 635 /* 636 * Avoid a read by setting B_CACHE where the data we 637 * intend to write covers the entire buffer. Note 638 * that the buffer may have been set to B_CACHE by 639 * nfs_meta_setsize() above or otherwise inherited the 640 * flag, but if B_CACHE isn't set the buffer may be 641 * uninitialized and must be zero'd to accomodate 642 * future seek+write's. 643 * 644 * See the comments in kern/vfs_bio.c's getblk() for 645 * more information. 646 * 647 * When doing a UIO_NOCOPY write the buffer is not 648 * overwritten and we cannot just set B_CACHE unconditionally 649 * for full-block writes. 650 */ 651 if (boff == 0 && bytes == biosize && 652 uio->uio_segflg != UIO_NOCOPY) { 653 bp->b_flags |= B_CACHE; 654 bp->b_flags &= ~(B_ERROR | B_INVAL); 655 } 656 657 /* 658 * b_resid may be set due to file EOF if we extended out. 659 * The NFS bio code will zero the difference anyway so 660 * just acknowledged the fact and set b_resid to 0. 661 */ 662 if ((bp->b_flags & B_CACHE) == 0) { 663 bp->b_cmd = BUF_CMD_READ; 664 bp->b_bio2.bio_done = nfsiodone_sync; 665 bp->b_bio2.bio_flags |= BIO_SYNC; 666 vfs_busy_pages(vp, bp); 667 error = nfs_doio(vp, &bp->b_bio2, td); 668 if (error) { 669 brelse(bp); 670 break; 671 } 672 bp->b_resid = 0; 673 } 674 np->n_flag |= NLMODIFIED; 675 676 /* 677 * If dirtyend exceeds file size, chop it down. This should 678 * not normally occur but there is an append race where it 679 * might occur XXX, so we log it. 680 * 681 * If the chopping creates a reverse-indexed or degenerate 682 * situation with dirtyoff/end, we 0 both of them. 683 */ 684 if (bp->b_dirtyend > bcount) { 685 kprintf("NFS append race @%08llx:%d\n", 686 (long long)bp->b_bio2.bio_offset, 687 bp->b_dirtyend - bcount); 688 bp->b_dirtyend = bcount; 689 } 690 691 if (bp->b_dirtyoff >= bp->b_dirtyend) 692 bp->b_dirtyoff = bp->b_dirtyend = 0; 693 694 /* 695 * If the new write will leave a contiguous dirty 696 * area, just update the b_dirtyoff and b_dirtyend, 697 * otherwise force a write rpc of the old dirty area. 698 * 699 * While it is possible to merge discontiguous writes due to 700 * our having a B_CACHE buffer ( and thus valid read data 701 * for the hole), we don't because it could lead to 702 * significant cache coherency problems with multiple clients, 703 * especially if locking is implemented later on. 704 * 705 * as an optimization we could theoretically maintain 706 * a linked list of discontinuous areas, but we would still 707 * have to commit them separately so there isn't much 708 * advantage to it except perhaps a bit of asynchronization. 709 */ 710 if (bp->b_dirtyend > 0 && 711 (boff > bp->b_dirtyend || 712 (boff + bytes) < bp->b_dirtyoff) 713 ) { 714 if (bwrite(bp) == EINTR) { 715 error = EINTR; 716 break; 717 } 718 goto again; 719 } 720 721 error = uiomovebp(bp, bp->b_data + boff, bytes, uio); 722 723 /* 724 * Since this block is being modified, it must be written 725 * again and not just committed. Since write clustering does 726 * not work for the stage 1 data write, only the stage 2 727 * commit rpc, we have to clear B_CLUSTEROK as well. 728 */ 729 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 730 731 if (error) { 732 brelse(bp); 733 break; 734 } 735 736 /* 737 * Only update dirtyoff/dirtyend if not a degenerate 738 * condition. 739 * 740 * The underlying VM pages have been marked valid by 741 * virtue of acquiring the bp. Because the entire buffer 742 * is marked dirty we do not have to worry about cleaning 743 * out the related dirty bits (and wouldn't really know 744 * how to deal with byte ranges anyway) 745 */ 746 if (bytes) { 747 if (bp->b_dirtyend > 0) { 748 bp->b_dirtyoff = imin(boff, bp->b_dirtyoff); 749 bp->b_dirtyend = imax(boff + bytes, 750 bp->b_dirtyend); 751 } else { 752 bp->b_dirtyoff = boff; 753 bp->b_dirtyend = boff + bytes; 754 } 755 } 756 757 /* 758 * If the lease is non-cachable or IO_SYNC do bwrite(). 759 * 760 * IO_INVAL appears to be unused. The idea appears to be 761 * to turn off caching in this case. Very odd. XXX 762 * 763 * If nfs_async is set bawrite() will use an unstable write 764 * (build dirty bufs on the server), so we might as well 765 * push it out with bawrite(). If nfs_async is not set we 766 * use bdwrite() to cache dirty bufs on the client. 767 */ 768 if (ioflag & IO_SYNC) { 769 if (ioflag & IO_INVAL) 770 bp->b_flags |= B_NOCACHE; 771 error = bwrite(bp); 772 if (error) 773 break; 774 } else if (boff + bytes == biosize && nfs_async) { 775 bawrite(bp); 776 } else { 777 bdwrite(bp); 778 } 779 } while (uio->uio_resid > 0 && bytes > 0); 780 781 if (haverslock) 782 nfs_rsunlock(np); 783 784 done: 785 lwkt_reltoken(&nmp->nm_token); 786 return (error); 787 } 788 789 /* 790 * Get an nfs cache block. 791 * 792 * Allocate a new one if the block isn't currently in the cache 793 * and return the block marked busy. If the calling process is 794 * interrupted by a signal for an interruptible mount point, return 795 * NULL. 796 * 797 * The caller must carefully deal with the possible B_INVAL state of 798 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it 799 * indirectly), so synchronous reads can be issued without worrying about 800 * the B_INVAL state. We have to be a little more careful when dealing 801 * with writes (see comments in nfs_write()) when extending a file past 802 * its EOF. 803 */ 804 static struct buf * 805 nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td) 806 { 807 struct buf *bp; 808 struct mount *mp; 809 struct nfsmount *nmp; 810 811 mp = vp->v_mount; 812 nmp = VFSTONFS(mp); 813 814 if (nmp->nm_flag & NFSMNT_INT) { 815 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0); 816 while (bp == NULL) { 817 if (nfs_sigintr(nmp, NULL, td)) 818 return (NULL); 819 bp = getblk(vp, loffset, size, 0, 2 * hz); 820 } 821 } else { 822 bp = getblk(vp, loffset, size, 0, 0); 823 } 824 825 /* 826 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets 827 * now, no translation is necessary. 828 */ 829 bp->b_bio2.bio_offset = loffset; 830 return (bp); 831 } 832 833 /* 834 * Flush and invalidate all dirty buffers. If another process is already 835 * doing the flush, just wait for completion. 836 */ 837 int 838 nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg) 839 { 840 struct nfsnode *np = VTONFS(vp); 841 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 842 int error = 0, slpflag, slptimeo; 843 thread_t td = curthread; 844 845 if (vp->v_flag & VRECLAIMED) 846 return (0); 847 848 if ((nmp->nm_flag & NFSMNT_INT) == 0) 849 intrflg = 0; 850 if (intrflg) { 851 slpflag = PCATCH; 852 slptimeo = 2 * hz; 853 } else { 854 slpflag = 0; 855 slptimeo = 0; 856 } 857 /* 858 * First wait for any other process doing a flush to complete. 859 */ 860 while (np->n_flag & NFLUSHINPROG) { 861 np->n_flag |= NFLUSHWANT; 862 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo); 863 if (error && intrflg && nfs_sigintr(nmp, NULL, td)) 864 return (EINTR); 865 } 866 867 /* 868 * Now, flush as required. 869 */ 870 np->n_flag |= NFLUSHINPROG; 871 error = vinvalbuf(vp, flags, slpflag, 0); 872 while (error) { 873 if (intrflg && nfs_sigintr(nmp, NULL, td)) { 874 np->n_flag &= ~NFLUSHINPROG; 875 if (np->n_flag & NFLUSHWANT) { 876 np->n_flag &= ~NFLUSHWANT; 877 wakeup((caddr_t)&np->n_flag); 878 } 879 return (EINTR); 880 } 881 error = vinvalbuf(vp, flags, 0, slptimeo); 882 } 883 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG); 884 if (np->n_flag & NFLUSHWANT) { 885 np->n_flag &= ~NFLUSHWANT; 886 wakeup((caddr_t)&np->n_flag); 887 } 888 return (0); 889 } 890 891 /* 892 * Return true (non-zero) if the txthread and rxthread are operational 893 * and we do not already have too many not-yet-started BIO's built up. 894 */ 895 int 896 nfs_asyncok(struct nfsmount *nmp) 897 { 898 return (nmp->nm_bioqlen < nfs_maxasyncbio && 899 nmp->nm_bioqlen < nmp->nm_maxasync_scaled / NFS_ASYSCALE && 900 nmp->nm_rxstate <= NFSSVC_PENDING && 901 nmp->nm_txstate <= NFSSVC_PENDING); 902 } 903 904 /* 905 * The read-ahead code calls this to queue a bio to the txthread. 906 * 907 * We don't touch the bio otherwise... that is, we do not even 908 * construct or send the initial rpc. The txthread will do it 909 * for us. 910 * 911 * NOTE! nm_bioqlen is not decremented until the request completes, 912 * so it does not reflect the number of bio's on bioq. 913 */ 914 void 915 nfs_asyncio(struct vnode *vp, struct bio *bio) 916 { 917 struct buf *bp = bio->bio_buf; 918 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 919 920 KKASSERT(vp->v_tag == VT_NFS); 921 BUF_KERNPROC(bp); 922 923 /* 924 * Shortcut swap cache (not done automatically because we are not 925 * using bread()). 926 */ 927 if (vn_cache_strategy(vp, bio)) 928 return; 929 930 bio->bio_driver_info = vp; 931 crit_enter(); 932 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act); 933 atomic_add_int(&nmp->nm_bioqlen, 1); 934 crit_exit(); 935 nfssvc_iod_writer_wakeup(nmp); 936 } 937 938 /* 939 * nfs_doio() - Execute a BIO operation synchronously. The BIO will be 940 * completed and its error returned. The caller is responsible 941 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise 942 * our error probe will be against an invalid pointer. 943 * 944 * nfs_startio()- Execute a BIO operation assynchronously. 945 * 946 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation, 947 * which basically just queues it to the txthread. nfs_startio() 948 * actually initiates the I/O AFTER it has gotten to the txthread. 949 * 950 * NOTE: td might be NULL. 951 * 952 * NOTE: Caller has already busied the I/O. 953 */ 954 void 955 nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td) 956 { 957 struct buf *bp = bio->bio_buf; 958 959 KKASSERT(vp->v_tag == VT_NFS); 960 961 /* 962 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We 963 * do this here so we do not have to do it in all the code that 964 * calls us. 965 */ 966 bp->b_flags &= ~(B_ERROR | B_INVAL); 967 968 KASSERT(bp->b_cmd != BUF_CMD_DONE, 969 ("nfs_doio: bp %p already marked done!", bp)); 970 971 if (bp->b_cmd == BUF_CMD_READ) { 972 switch (vp->v_type) { 973 case VREG: 974 nfsstats.read_bios++; 975 nfs_readrpc_bio(vp, bio); 976 break; 977 case VLNK: 978 #if 0 979 bio->bio_offset = 0; 980 nfsstats.readlink_bios++; 981 nfs_readlinkrpc_bio(vp, bio); 982 #else 983 nfs_doio(vp, bio, td); 984 #endif 985 break; 986 case VDIR: 987 /* 988 * NOTE: If nfs_readdirplusrpc_bio() is requested but 989 * not supported, it will chain to 990 * nfs_readdirrpc_bio(). 991 */ 992 #if 0 993 nfsstats.readdir_bios++; 994 uiop->uio_offset = bio->bio_offset; 995 if (nmp->nm_flag & NFSMNT_RDIRPLUS) 996 nfs_readdirplusrpc_bio(vp, bio); 997 else 998 nfs_readdirrpc_bio(vp, bio); 999 #else 1000 nfs_doio(vp, bio, td); 1001 #endif 1002 break; 1003 default: 1004 kprintf("nfs_doio: type %x unexpected\n",vp->v_type); 1005 bp->b_flags |= B_ERROR; 1006 bp->b_error = EINVAL; 1007 biodone(bio); 1008 break; 1009 } 1010 } else { 1011 /* 1012 * If we only need to commit, try to commit. If this fails 1013 * it will chain through to the write. Basically all the logic 1014 * in nfs_doio() is replicated. 1015 */ 1016 KKASSERT(bp->b_cmd == BUF_CMD_WRITE); 1017 if (bp->b_flags & B_NEEDCOMMIT) 1018 nfs_commitrpc_bio(vp, bio); 1019 else 1020 nfs_writerpc_bio(vp, bio); 1021 } 1022 } 1023 1024 int 1025 nfs_doio(struct vnode *vp, struct bio *bio, struct thread *td) 1026 { 1027 struct buf *bp = bio->bio_buf; 1028 struct uio *uiop; 1029 struct nfsnode *np; 1030 struct nfsmount *nmp; 1031 int error = 0; 1032 int iomode, must_commit; 1033 size_t n; 1034 struct uio uio; 1035 struct iovec io; 1036 1037 #if 0 1038 /* 1039 * Shortcut swap cache (not done automatically because we are not 1040 * using bread()). 1041 * 1042 * XXX The biowait is a hack until we can figure out how to stop a 1043 * biodone chain when a middle element is BIO_SYNC. BIO_SYNC is 1044 * set so the bp shouldn't get ripped out from under us. The only 1045 * use-cases are fully synchronous I/O cases. 1046 * 1047 * XXX This is having problems, give up for now. 1048 */ 1049 if (vn_cache_strategy(vp, bio)) { 1050 error = biowait(&bio->bio_buf->b_bio1, "nfsrsw"); 1051 return (error); 1052 } 1053 #endif 1054 1055 KKASSERT(vp->v_tag == VT_NFS); 1056 np = VTONFS(vp); 1057 nmp = VFSTONFS(vp->v_mount); 1058 uiop = &uio; 1059 uiop->uio_iov = &io; 1060 uiop->uio_iovcnt = 1; 1061 uiop->uio_segflg = UIO_SYSSPACE; 1062 uiop->uio_td = td; 1063 1064 /* 1065 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We 1066 * do this here so we do not have to do it in all the code that 1067 * calls us. 1068 */ 1069 bp->b_flags &= ~(B_ERROR | B_INVAL); 1070 1071 KASSERT(bp->b_cmd != BUF_CMD_DONE, 1072 ("nfs_doio: bp %p already marked done!", bp)); 1073 1074 if (bp->b_cmd == BUF_CMD_READ) { 1075 io.iov_len = uiop->uio_resid = (size_t)bp->b_bcount; 1076 io.iov_base = bp->b_data; 1077 uiop->uio_rw = UIO_READ; 1078 1079 switch (vp->v_type) { 1080 case VREG: 1081 /* 1082 * When reading from a regular file zero-fill any residual. 1083 * Note that this residual has nothing to do with NFS short 1084 * reads, which nfs_readrpc_uio() will handle for us. 1085 * 1086 * We have to do this because when we are write extending 1087 * a file the server may not have the same notion of 1088 * filesize as we do. Our BIOs should already be sized 1089 * (b_bcount) to account for the file EOF. 1090 */ 1091 nfsstats.read_bios++; 1092 uiop->uio_offset = bio->bio_offset; 1093 error = nfs_readrpc_uio(vp, uiop); 1094 if (error == 0 && uiop->uio_resid) { 1095 n = (size_t)bp->b_bcount - uiop->uio_resid; 1096 bzero(bp->b_data + n, bp->b_bcount - n); 1097 uiop->uio_resid = 0; 1098 } 1099 if (td && td->td_proc && (vp->v_flag & VTEXT) && 1100 np->n_mtime != np->n_vattr.va_mtime.tv_sec) { 1101 uprintf("Process killed due to text file modification\n"); 1102 ksignal(td->td_proc, SIGKILL); 1103 } 1104 break; 1105 case VLNK: 1106 uiop->uio_offset = 0; 1107 nfsstats.readlink_bios++; 1108 error = nfs_readlinkrpc_uio(vp, uiop); 1109 break; 1110 case VDIR: 1111 nfsstats.readdir_bios++; 1112 uiop->uio_offset = bio->bio_offset; 1113 if (nmp->nm_flag & NFSMNT_RDIRPLUS) { 1114 error = nfs_readdirplusrpc_uio(vp, uiop); 1115 if (error == NFSERR_NOTSUPP) 1116 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 1117 } 1118 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 1119 error = nfs_readdirrpc_uio(vp, uiop); 1120 /* 1121 * end-of-directory sets B_INVAL but does not generate an 1122 * error. 1123 */ 1124 if (error == 0 && uiop->uio_resid == bp->b_bcount) 1125 bp->b_flags |= B_INVAL; 1126 break; 1127 default: 1128 kprintf("nfs_doio: type %x unexpected\n",vp->v_type); 1129 break; 1130 } 1131 if (error) { 1132 bp->b_flags |= B_ERROR; 1133 bp->b_error = error; 1134 } 1135 bp->b_resid = uiop->uio_resid; 1136 } else { 1137 /* 1138 * If we only need to commit, try to commit. 1139 * 1140 * NOTE: The I/O has already been staged for the write and 1141 * its pages busied, so b_dirtyoff/end is valid. 1142 */ 1143 KKASSERT(bp->b_cmd == BUF_CMD_WRITE); 1144 if (bp->b_flags & B_NEEDCOMMIT) { 1145 int retv; 1146 off_t off; 1147 1148 off = bio->bio_offset + bp->b_dirtyoff; 1149 retv = nfs_commitrpc_uio(vp, off, 1150 bp->b_dirtyend - bp->b_dirtyoff, 1151 td); 1152 if (retv == 0) { 1153 bp->b_dirtyoff = bp->b_dirtyend = 0; 1154 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1155 bp->b_resid = 0; 1156 biodone(bio); 1157 return(0); 1158 } 1159 if (retv == NFSERR_STALEWRITEVERF) { 1160 nfs_clearcommit(vp->v_mount); 1161 } 1162 } 1163 1164 /* 1165 * Setup for actual write 1166 */ 1167 if (bio->bio_offset + bp->b_dirtyend > np->n_size) 1168 bp->b_dirtyend = np->n_size - bio->bio_offset; 1169 1170 if (bp->b_dirtyend > bp->b_dirtyoff) { 1171 io.iov_len = uiop->uio_resid = bp->b_dirtyend 1172 - bp->b_dirtyoff; 1173 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff; 1174 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 1175 uiop->uio_rw = UIO_WRITE; 1176 nfsstats.write_bios++; 1177 1178 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0) 1179 iomode = NFSV3WRITE_UNSTABLE; 1180 else 1181 iomode = NFSV3WRITE_FILESYNC; 1182 1183 must_commit = 0; 1184 error = nfs_writerpc_uio(vp, uiop, &iomode, &must_commit); 1185 1186 /* 1187 * We no longer try to use kern/vfs_bio's cluster code to 1188 * cluster commits, so B_CLUSTEROK is no longer set with 1189 * B_NEEDCOMMIT. The problem is that a vfs_busy_pages() 1190 * may have to clear B_NEEDCOMMIT if it finds underlying 1191 * pages have been redirtied through a memory mapping 1192 * and doing this on a clustered bp will probably cause 1193 * a panic, plus the flag in the underlying NFS bufs 1194 * making up the cluster bp will not be properly cleared. 1195 */ 1196 if (!error && iomode == NFSV3WRITE_UNSTABLE) { 1197 bp->b_flags |= B_NEEDCOMMIT; 1198 #if 0 1199 /* XXX do not enable commit clustering */ 1200 if (bp->b_dirtyoff == 0 1201 && bp->b_dirtyend == bp->b_bcount) 1202 bp->b_flags |= B_CLUSTEROK; 1203 #endif 1204 } else { 1205 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1206 } 1207 1208 /* 1209 * For an interrupted write, the buffer is still valid 1210 * and the write hasn't been pushed to the server yet, 1211 * so we can't set B_ERROR and report the interruption 1212 * by setting B_EINTR. For the async case, B_EINTR 1213 * is not relevant, so the rpc attempt is essentially 1214 * a noop. For the case of a V3 write rpc not being 1215 * committed to stable storage, the block is still 1216 * dirty and requires either a commit rpc or another 1217 * write rpc with iomode == NFSV3WRITE_FILESYNC before 1218 * the block is reused. This is indicated by setting 1219 * the B_DELWRI and B_NEEDCOMMIT flags. 1220 * 1221 * If the buffer is marked B_PAGING, it does not reside on 1222 * the vp's paging queues so we cannot call bdirty(). The 1223 * bp in this case is not an NFS cache block so we should 1224 * be safe. XXX 1225 */ 1226 if (error == EINTR 1227 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 1228 crit_enter(); 1229 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 1230 if ((bp->b_flags & B_PAGING) == 0) 1231 bdirty(bp); 1232 if (error) 1233 bp->b_flags |= B_EINTR; 1234 crit_exit(); 1235 } else { 1236 if (error) { 1237 bp->b_flags |= B_ERROR; 1238 bp->b_error = np->n_error = error; 1239 np->n_flag |= NWRITEERR; 1240 } 1241 bp->b_dirtyoff = bp->b_dirtyend = 0; 1242 } 1243 if (must_commit) 1244 nfs_clearcommit(vp->v_mount); 1245 bp->b_resid = uiop->uio_resid; 1246 } else { 1247 bp->b_resid = 0; 1248 } 1249 } 1250 1251 /* 1252 * I/O was run synchronously, biodone() it and calculate the 1253 * error to return. 1254 */ 1255 biodone(bio); 1256 KKASSERT(bp->b_cmd == BUF_CMD_DONE); 1257 if (bp->b_flags & B_EINTR) 1258 return (EINTR); 1259 if (bp->b_flags & B_ERROR) 1260 return (bp->b_error ? bp->b_error : EIO); 1261 return (0); 1262 } 1263 1264 /* 1265 * Handle all truncation, write-extend, and ftruncate()-extend operations 1266 * on the NFS lcient side. 1267 * 1268 * We use the new API in kern/vfs_vm.c to perform these operations in a 1269 * VM-friendly way. With this API VM pages are properly zerod and pages 1270 * still mapped into the buffer straddling EOF are not invalidated. 1271 */ 1272 int 1273 nfs_meta_setsize(struct vnode *vp, struct thread *td, off_t nsize, int trivial) 1274 { 1275 struct nfsnode *np = VTONFS(vp); 1276 off_t osize; 1277 int biosize = vp->v_mount->mnt_stat.f_iosize; 1278 int error; 1279 1280 osize = np->n_size; 1281 np->n_size = nsize; 1282 1283 if (nsize < osize) { 1284 error = nvtruncbuf(vp, nsize, biosize, -1, 0); 1285 } else { 1286 error = nvextendbuf(vp, osize, nsize, 1287 biosize, biosize, -1, -1, 1288 trivial); 1289 } 1290 return(error); 1291 } 1292 1293 /* 1294 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE. 1295 * Caller is responsible for brelse()'ing the bp. 1296 */ 1297 static void 1298 nfsiodone_sync(struct bio *bio) 1299 { 1300 bio->bio_flags = 0; 1301 bpdone(bio->bio_buf, 0); 1302 } 1303 1304 /* 1305 * nfs read rpc - BIO version 1306 */ 1307 void 1308 nfs_readrpc_bio(struct vnode *vp, struct bio *bio) 1309 { 1310 struct buf *bp = bio->bio_buf; 1311 u_int32_t *tl; 1312 struct nfsmount *nmp; 1313 int error = 0, len, tsiz; 1314 struct nfsm_info *info; 1315 1316 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK); 1317 info->mrep = NULL; 1318 info->v3 = NFS_ISV3(vp); 1319 1320 nmp = VFSTONFS(vp->v_mount); 1321 tsiz = bp->b_bcount; 1322 KKASSERT(tsiz <= nmp->nm_rsize); 1323 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) { 1324 error = EFBIG; 1325 goto nfsmout; 1326 } 1327 nfsstats.rpccnt[NFSPROC_READ]++; 1328 len = tsiz; 1329 nfsm_reqhead(info, vp, NFSPROC_READ, 1330 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3); 1331 ERROROUT(nfsm_fhtom(info, vp)); 1332 tl = nfsm_build(info, NFSX_UNSIGNED * 3); 1333 if (info->v3) { 1334 txdr_hyper(bio->bio_offset, tl); 1335 *(tl + 2) = txdr_unsigned(len); 1336 } else { 1337 *tl++ = txdr_unsigned(bio->bio_offset); 1338 *tl++ = txdr_unsigned(len); 1339 *tl = 0; 1340 } 1341 info->bio = bio; 1342 info->done = nfs_readrpc_bio_done; 1343 nfsm_request_bio(info, vp, NFSPROC_READ, NULL, 1344 nfs_vpcred(vp, ND_READ)); 1345 return; 1346 nfsmout: 1347 kfree(info, M_NFSREQ); 1348 bp->b_error = error; 1349 bp->b_flags |= B_ERROR; 1350 biodone(bio); 1351 } 1352 1353 static void 1354 nfs_readrpc_bio_done(nfsm_info_t info) 1355 { 1356 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount); 1357 struct bio *bio = info->bio; 1358 struct buf *bp = bio->bio_buf; 1359 u_int32_t *tl; 1360 int attrflag; 1361 int retlen; 1362 int eof; 1363 int error = 0; 1364 1365 KKASSERT(info->state == NFSM_STATE_DONE); 1366 1367 lwkt_gettoken(&nmp->nm_token); 1368 1369 ERROROUT(info->error); 1370 if (info->v3) { 1371 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag, 1372 NFS_LATTR_NOSHRINK)); 1373 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED)); 1374 eof = fxdr_unsigned(int, *(tl + 1)); 1375 } else { 1376 ERROROUT(nfsm_loadattr(info, info->vp, NULL)); 1377 eof = 0; 1378 } 1379 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize)); 1380 ERROROUT(nfsm_mtobio(info, bio, retlen)); 1381 m_freem(info->mrep); 1382 info->mrep = NULL; 1383 1384 /* 1385 * No error occured, if retlen is less then bcount and no EOF 1386 * and NFSv3 a zero-fill short read occured. 1387 * 1388 * For NFSv2 a short-read indicates EOF. 1389 */ 1390 if (retlen < bp->b_bcount && info->v3 && eof == 0) { 1391 bzero(bp->b_data + retlen, bp->b_bcount - retlen); 1392 retlen = bp->b_bcount; 1393 } 1394 1395 /* 1396 * If we hit an EOF we still zero-fill, but return the expected 1397 * b_resid anyway. This should normally not occur since async 1398 * BIOs are not used for read-before-write case. Races against 1399 * the server can cause it though and we don't want to leave 1400 * garbage in the buffer. 1401 */ 1402 if (retlen < bp->b_bcount) { 1403 bzero(bp->b_data + retlen, bp->b_bcount - retlen); 1404 } 1405 bp->b_resid = 0; 1406 /* bp->b_resid = bp->b_bcount - retlen; */ 1407 nfsmout: 1408 lwkt_reltoken(&nmp->nm_token); 1409 kfree(info, M_NFSREQ); 1410 if (error) { 1411 bp->b_error = error; 1412 bp->b_flags |= B_ERROR; 1413 } 1414 biodone(bio); 1415 } 1416 1417 /* 1418 * nfs write call - BIO version 1419 * 1420 * NOTE: Caller has already busied the I/O. 1421 */ 1422 void 1423 nfs_writerpc_bio(struct vnode *vp, struct bio *bio) 1424 { 1425 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1426 struct nfsnode *np = VTONFS(vp); 1427 struct buf *bp = bio->bio_buf; 1428 u_int32_t *tl; 1429 int len; 1430 int iomode; 1431 int error = 0; 1432 struct nfsm_info *info; 1433 off_t offset; 1434 1435 /* 1436 * Setup for actual write. Just clean up the bio if there 1437 * is nothing to do. b_dirtyoff/end have already been staged 1438 * by the bp's pages getting busied. 1439 */ 1440 if (bio->bio_offset + bp->b_dirtyend > np->n_size) 1441 bp->b_dirtyend = np->n_size - bio->bio_offset; 1442 1443 if (bp->b_dirtyend <= bp->b_dirtyoff) { 1444 bp->b_resid = 0; 1445 biodone(bio); 1446 return; 1447 } 1448 len = bp->b_dirtyend - bp->b_dirtyoff; 1449 offset = bio->bio_offset + bp->b_dirtyoff; 1450 if (offset + len > nmp->nm_maxfilesize) { 1451 bp->b_flags |= B_ERROR; 1452 bp->b_error = EFBIG; 1453 biodone(bio); 1454 return; 1455 } 1456 bp->b_resid = len; 1457 nfsstats.write_bios++; 1458 1459 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK); 1460 info->mrep = NULL; 1461 info->v3 = NFS_ISV3(vp); 1462 info->info_writerpc.must_commit = 0; 1463 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0) 1464 iomode = NFSV3WRITE_UNSTABLE; 1465 else 1466 iomode = NFSV3WRITE_FILESYNC; 1467 1468 KKASSERT(len <= nmp->nm_wsize); 1469 1470 nfsstats.rpccnt[NFSPROC_WRITE]++; 1471 nfsm_reqhead(info, vp, NFSPROC_WRITE, 1472 NFSX_FH(info->v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len)); 1473 ERROROUT(nfsm_fhtom(info, vp)); 1474 if (info->v3) { 1475 tl = nfsm_build(info, 5 * NFSX_UNSIGNED); 1476 txdr_hyper(offset, tl); 1477 tl += 2; 1478 *tl++ = txdr_unsigned(len); 1479 *tl++ = txdr_unsigned(iomode); 1480 *tl = txdr_unsigned(len); 1481 } else { 1482 u_int32_t x; 1483 1484 tl = nfsm_build(info, 4 * NFSX_UNSIGNED); 1485 /* Set both "begin" and "current" to non-garbage. */ 1486 x = txdr_unsigned((u_int32_t)offset); 1487 *tl++ = x; /* "begin offset" */ 1488 *tl++ = x; /* "current offset" */ 1489 x = txdr_unsigned(len); 1490 *tl++ = x; /* total to this offset */ 1491 *tl = x; /* size of this write */ 1492 } 1493 ERROROUT(nfsm_biotom(info, bio, bp->b_dirtyoff, len)); 1494 info->bio = bio; 1495 info->done = nfs_writerpc_bio_done; 1496 nfsm_request_bio(info, vp, NFSPROC_WRITE, NULL, 1497 nfs_vpcred(vp, ND_WRITE)); 1498 return; 1499 nfsmout: 1500 kfree(info, M_NFSREQ); 1501 bp->b_error = error; 1502 bp->b_flags |= B_ERROR; 1503 biodone(bio); 1504 } 1505 1506 static void 1507 nfs_writerpc_bio_done(nfsm_info_t info) 1508 { 1509 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount); 1510 struct nfsnode *np = VTONFS(info->vp); 1511 struct bio *bio = info->bio; 1512 struct buf *bp = bio->bio_buf; 1513 int wccflag = NFSV3_WCCRATTR; 1514 int iomode = NFSV3WRITE_FILESYNC; 1515 int commit; 1516 int rlen; 1517 int error; 1518 int len = bp->b_resid; /* b_resid was set to shortened length */ 1519 u_int32_t *tl; 1520 1521 lwkt_gettoken(&nmp->nm_token); 1522 1523 ERROROUT(info->error); 1524 if (info->v3) { 1525 /* 1526 * The write RPC returns a before and after mtime. The 1527 * nfsm_wcc_data() macro checks the before n_mtime 1528 * against the before time and stores the after time 1529 * in the nfsnode's cached vattr and n_mtime field. 1530 * The NRMODIFIED bit will be set if the before 1531 * time did not match the original mtime. 1532 */ 1533 wccflag = NFSV3_WCCCHK; 1534 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag)); 1535 if (error == 0) { 1536 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF)); 1537 rlen = fxdr_unsigned(int, *tl++); 1538 if (rlen == 0) { 1539 error = NFSERR_IO; 1540 m_freem(info->mrep); 1541 info->mrep = NULL; 1542 goto nfsmout; 1543 } else if (rlen < len) { 1544 #if 0 1545 /* 1546 * XXX what do we do here? 1547 */ 1548 backup = len - rlen; 1549 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup; 1550 uiop->uio_iov->iov_len += backup; 1551 uiop->uio_offset -= backup; 1552 uiop->uio_resid += backup; 1553 len = rlen; 1554 #endif 1555 } 1556 commit = fxdr_unsigned(int, *tl++); 1557 1558 /* 1559 * Return the lowest committment level 1560 * obtained by any of the RPCs. 1561 */ 1562 if (iomode == NFSV3WRITE_FILESYNC) 1563 iomode = commit; 1564 else if (iomode == NFSV3WRITE_DATASYNC && 1565 commit == NFSV3WRITE_UNSTABLE) 1566 iomode = commit; 1567 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){ 1568 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF); 1569 nmp->nm_state |= NFSSTA_HASWRITEVERF; 1570 } else if (bcmp(tl, nmp->nm_verf, NFSX_V3WRITEVERF)) { 1571 info->info_writerpc.must_commit = 1; 1572 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF); 1573 } 1574 } 1575 } else { 1576 ERROROUT(nfsm_loadattr(info, info->vp, NULL)); 1577 } 1578 m_freem(info->mrep); 1579 info->mrep = NULL; 1580 len = 0; 1581 nfsmout: 1582 if (info->vp->v_mount->mnt_flag & MNT_ASYNC) 1583 iomode = NFSV3WRITE_FILESYNC; 1584 bp->b_resid = len; 1585 1586 /* 1587 * End of RPC. Now clean up the bp. 1588 * 1589 * We no longer enable write clustering for commit operations, 1590 * See around line 1157 for a more detailed comment. 1591 */ 1592 if (!error && iomode == NFSV3WRITE_UNSTABLE) { 1593 bp->b_flags |= B_NEEDCOMMIT; 1594 #if 0 1595 /* XXX do not enable commit clustering */ 1596 if (bp->b_dirtyoff == 0 && bp->b_dirtyend == bp->b_bcount) 1597 bp->b_flags |= B_CLUSTEROK; 1598 #endif 1599 } else { 1600 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1601 } 1602 1603 /* 1604 * For an interrupted write, the buffer is still valid 1605 * and the write hasn't been pushed to the server yet, 1606 * so we can't set B_ERROR and report the interruption 1607 * by setting B_EINTR. For the async case, B_EINTR 1608 * is not relevant, so the rpc attempt is essentially 1609 * a noop. For the case of a V3 write rpc not being 1610 * committed to stable storage, the block is still 1611 * dirty and requires either a commit rpc or another 1612 * write rpc with iomode == NFSV3WRITE_FILESYNC before 1613 * the block is reused. This is indicated by setting 1614 * the B_DELWRI and B_NEEDCOMMIT flags. 1615 * 1616 * If the buffer is marked B_PAGING, it does not reside on 1617 * the vp's paging queues so we cannot call bdirty(). The 1618 * bp in this case is not an NFS cache block so we should 1619 * be safe. XXX 1620 */ 1621 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 1622 crit_enter(); 1623 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 1624 if ((bp->b_flags & B_PAGING) == 0) 1625 bdirty(bp); 1626 if (error) 1627 bp->b_flags |= B_EINTR; 1628 crit_exit(); 1629 } else { 1630 if (error) { 1631 bp->b_flags |= B_ERROR; 1632 bp->b_error = np->n_error = error; 1633 np->n_flag |= NWRITEERR; 1634 } 1635 bp->b_dirtyoff = bp->b_dirtyend = 0; 1636 } 1637 if (info->info_writerpc.must_commit) 1638 nfs_clearcommit(info->vp->v_mount); 1639 lwkt_reltoken(&nmp->nm_token); 1640 1641 kfree(info, M_NFSREQ); 1642 if (error) { 1643 bp->b_flags |= B_ERROR; 1644 bp->b_error = error; 1645 } 1646 biodone(bio); 1647 } 1648 1649 /* 1650 * Nfs Version 3 commit rpc - BIO version 1651 * 1652 * This function issues the commit rpc and will chain to a write 1653 * rpc if necessary. 1654 */ 1655 void 1656 nfs_commitrpc_bio(struct vnode *vp, struct bio *bio) 1657 { 1658 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1659 struct buf *bp = bio->bio_buf; 1660 struct nfsm_info *info; 1661 int error = 0; 1662 u_int32_t *tl; 1663 1664 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) { 1665 bp->b_dirtyoff = bp->b_dirtyend = 0; 1666 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1667 bp->b_resid = 0; 1668 biodone(bio); 1669 return; 1670 } 1671 1672 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK); 1673 info->mrep = NULL; 1674 info->v3 = 1; 1675 1676 nfsstats.rpccnt[NFSPROC_COMMIT]++; 1677 nfsm_reqhead(info, vp, NFSPROC_COMMIT, NFSX_FH(1)); 1678 ERROROUT(nfsm_fhtom(info, vp)); 1679 tl = nfsm_build(info, 3 * NFSX_UNSIGNED); 1680 txdr_hyper(bio->bio_offset + bp->b_dirtyoff, tl); 1681 tl += 2; 1682 *tl = txdr_unsigned(bp->b_dirtyend - bp->b_dirtyoff); 1683 info->bio = bio; 1684 info->done = nfs_commitrpc_bio_done; 1685 nfsm_request_bio(info, vp, NFSPROC_COMMIT, NULL, 1686 nfs_vpcred(vp, ND_WRITE)); 1687 return; 1688 nfsmout: 1689 /* 1690 * Chain to write RPC on (early) error 1691 */ 1692 kfree(info, M_NFSREQ); 1693 nfs_writerpc_bio(vp, bio); 1694 } 1695 1696 static void 1697 nfs_commitrpc_bio_done(nfsm_info_t info) 1698 { 1699 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount); 1700 struct bio *bio = info->bio; 1701 struct buf *bp = bio->bio_buf; 1702 u_int32_t *tl; 1703 int wccflag = NFSV3_WCCRATTR; 1704 int error = 0; 1705 1706 lwkt_gettoken(&nmp->nm_token); 1707 1708 ERROROUT(info->error); 1709 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag)); 1710 if (error == 0) { 1711 NULLOUT(tl = nfsm_dissect(info, NFSX_V3WRITEVERF)); 1712 if (bcmp(nmp->nm_verf, tl, NFSX_V3WRITEVERF)) { 1713 bcopy(tl, nmp->nm_verf, NFSX_V3WRITEVERF); 1714 error = NFSERR_STALEWRITEVERF; 1715 } 1716 } 1717 m_freem(info->mrep); 1718 info->mrep = NULL; 1719 1720 /* 1721 * On completion we must chain to a write bio if an 1722 * error occurred. 1723 */ 1724 nfsmout: 1725 if (error == 0) { 1726 bp->b_dirtyoff = bp->b_dirtyend = 0; 1727 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1728 bp->b_resid = 0; 1729 biodone(bio); 1730 } else { 1731 nfs_writerpc_bio(info->vp, bio); 1732 } 1733 kfree(info, M_NFSREQ); 1734 lwkt_reltoken(&nmp->nm_token); 1735 } 1736 1737