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