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