nfs_bio.c revision 36248
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95 37 * $Id: nfs_bio.c,v 1.55 1998/05/19 07:11:22 peter Exp $ 38 */ 39 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/resourcevar.h> 44#include <sys/signalvar.h> 45#include <sys/proc.h> 46#include <sys/buf.h> 47#include <sys/vnode.h> 48#include <sys/mount.h> 49#include <sys/kernel.h> 50 51#include <vm/vm.h> 52#include <vm/vm_extern.h> 53#include <vm/vm_prot.h> 54#include <vm/vm_page.h> 55#include <vm/vm_object.h> 56#include <vm/vm_pager.h> 57#include <vm/vnode_pager.h> 58 59#include <nfs/rpcv2.h> 60#include <nfs/nfsproto.h> 61#include <nfs/nfs.h> 62#include <nfs/nfsmount.h> 63#include <nfs/nqnfs.h> 64#include <nfs/nfsnode.h> 65 66static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size, 67 struct proc *p)); 68static void nfs_prot_buf __P((struct buf *bp, int off, int n)); 69 70extern int nfs_numasync; 71extern struct nfsstats nfsstats; 72 73/* 74 * Vnode op for VM getpages. 75 */ 76int 77nfs_getpages(ap) 78 struct vop_getpages_args *ap; 79{ 80 int i, error, nextoff, size, toff, npages; 81 struct uio uio; 82 struct iovec iov; 83 vm_page_t m; 84 vm_offset_t kva; 85 struct buf *bp; 86 87 if ((ap->a_vp->v_object) == NULL) { 88 printf("nfs_getpages: called with non-merged cache vnode??\n"); 89 return EOPNOTSUPP; 90 } 91 92 /* 93 * We use only the kva address for the buffer, but this is extremely 94 * convienient and fast. 95 */ 96 bp = getpbuf(); 97 98 npages = btoc(ap->a_count); 99 kva = (vm_offset_t) bp->b_data; 100 pmap_qenter(kva, ap->a_m, npages); 101 102 iov.iov_base = (caddr_t) kva; 103 iov.iov_len = ap->a_count; 104 uio.uio_iov = &iov; 105 uio.uio_iovcnt = 1; 106 uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex); 107 uio.uio_resid = ap->a_count; 108 uio.uio_segflg = UIO_SYSSPACE; 109 uio.uio_rw = UIO_READ; 110 uio.uio_procp = curproc; 111 112 error = nfs_readrpc(ap->a_vp, &uio, curproc->p_ucred); 113 pmap_qremove(kva, npages); 114 115 relpbuf(bp); 116 117 if (error && (uio.uio_resid == ap->a_count)) 118 return VM_PAGER_ERROR; 119 120 size = ap->a_count - uio.uio_resid; 121 122 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) { 123 vm_page_t m; 124 nextoff = toff + PAGE_SIZE; 125 m = ap->a_m[i]; 126 127 m->flags &= ~PG_ZERO; 128 129 if (nextoff <= size) { 130 m->valid = VM_PAGE_BITS_ALL; 131 m->dirty = 0; 132 } else { 133 int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1); 134 vm_page_set_validclean(m, 0, nvalid); 135 } 136 137 if (i != ap->a_reqpage) { 138 /* 139 * Whether or not to leave the page activated is up in 140 * the air, but we should put the page on a page queue 141 * somewhere (it already is in the object). Result: 142 * It appears that emperical results show that 143 * deactivating pages is best. 144 */ 145 146 /* 147 * Just in case someone was asking for this page we 148 * now tell them that it is ok to use. 149 */ 150 if (!error) { 151 if (m->flags & PG_WANTED) 152 vm_page_activate(m); 153 else 154 vm_page_deactivate(m); 155 PAGE_WAKEUP(m); 156 } else { 157 vnode_pager_freepage(m); 158 } 159 } 160 } 161 return 0; 162} 163 164/* 165 * Vnode op for VM putpages. 166 */ 167int 168nfs_putpages(ap) 169 struct vop_putpages_args *ap; 170{ 171 struct uio uio; 172 struct iovec iov; 173 vm_page_t m; 174 vm_offset_t kva; 175 struct buf *bp; 176 int iomode, must_commit, i, error, npages; 177 int *rtvals; 178 179 rtvals = ap->a_rtvals; 180 181 npages = btoc(ap->a_count); 182 183 for (i = 0; i < npages; i++) { 184 rtvals[i] = VM_PAGER_AGAIN; 185 } 186 187 /* 188 * We use only the kva address for the buffer, but this is extremely 189 * convienient and fast. 190 */ 191 bp = getpbuf(); 192 193 kva = (vm_offset_t) bp->b_data; 194 pmap_qenter(kva, ap->a_m, npages); 195 196 iov.iov_base = (caddr_t) kva; 197 iov.iov_len = ap->a_count; 198 uio.uio_iov = &iov; 199 uio.uio_iovcnt = 1; 200 uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex); 201 uio.uio_resid = ap->a_count; 202 uio.uio_segflg = UIO_SYSSPACE; 203 uio.uio_rw = UIO_WRITE; 204 uio.uio_procp = curproc; 205 206 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0) 207 iomode = NFSV3WRITE_UNSTABLE; 208 else 209 iomode = NFSV3WRITE_FILESYNC; 210 211 error = nfs_writerpc(ap->a_vp, &uio, 212 curproc->p_ucred, &iomode, &must_commit); 213 214 pmap_qremove(kva, npages); 215 relpbuf(bp); 216 217 if (!error) { 218 int nwritten = round_page(ap->a_count - uio.uio_resid) / PAGE_SIZE; 219 for (i = 0; i < nwritten; i++) { 220 rtvals[i] = VM_PAGER_OK; 221 ap->a_m[i]->dirty = 0; 222 } 223 if (must_commit) 224 nfs_clearcommit(ap->a_vp->v_mount); 225 } 226 return ap->a_rtvals[0]; 227} 228 229/* 230 * Vnode op for read using bio 231 * Any similarity to readip() is purely coincidental 232 */ 233int 234nfs_bioread(vp, uio, ioflag, cred, getpages) 235 register struct vnode *vp; 236 register struct uio *uio; 237 int ioflag; 238 struct ucred *cred; 239 int getpages; 240{ 241 register struct nfsnode *np = VTONFS(vp); 242 register int biosize, diff, i; 243 struct buf *bp = 0, *rabp; 244 struct vattr vattr; 245 struct proc *p; 246 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 247 daddr_t lbn, rabn; 248 int bufsize; 249 int nra, error = 0, n = 0, on = 0, not_readin; 250 251#ifdef DIAGNOSTIC 252 if (uio->uio_rw != UIO_READ) 253 panic("nfs_read mode"); 254#endif 255 if (uio->uio_resid == 0) 256 return (0); 257 if (uio->uio_offset < 0) 258 return (EINVAL); 259 p = uio->uio_procp; 260 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 261 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 262 (void)nfs_fsinfo(nmp, vp, cred, p); 263 biosize = vp->v_mount->mnt_stat.f_iosize; 264 /* 265 * For nfs, cache consistency can only be maintained approximately. 266 * Although RFC1094 does not specify the criteria, the following is 267 * believed to be compatible with the reference port. 268 * For nqnfs, full cache consistency is maintained within the loop. 269 * For nfs: 270 * If the file's modify time on the server has changed since the 271 * last read rpc or you have written to the file, 272 * you may have lost data cache consistency with the 273 * server, so flush all of the file's data out of the cache. 274 * Then force a getattr rpc to ensure that you have up to date 275 * attributes. 276 * NB: This implies that cache data can be read when up to 277 * NFS_ATTRTIMEO seconds out of date. If you find that you need current 278 * attributes this could be forced by setting n_attrstamp to 0 before 279 * the VOP_GETATTR() call. 280 */ 281 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) { 282 if (np->n_flag & NMODIFIED) { 283 if (vp->v_type != VREG) { 284 if (vp->v_type != VDIR) 285 panic("nfs: bioread, not dir"); 286 nfs_invaldir(vp); 287 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 288 if (error) 289 return (error); 290 } 291 np->n_attrstamp = 0; 292 error = VOP_GETATTR(vp, &vattr, cred, p); 293 if (error) 294 return (error); 295 np->n_mtime = vattr.va_mtime.tv_sec; 296 } else { 297 error = VOP_GETATTR(vp, &vattr, cred, p); 298 if (error) 299 return (error); 300 if (np->n_mtime != vattr.va_mtime.tv_sec) { 301 if (vp->v_type == VDIR) 302 nfs_invaldir(vp); 303 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 304 if (error) 305 return (error); 306 np->n_mtime = vattr.va_mtime.tv_sec; 307 } 308 } 309 } 310 do { 311 312 /* 313 * Get a valid lease. If cached data is stale, flush it. 314 */ 315 if (nmp->nm_flag & NFSMNT_NQNFS) { 316 if (NQNFS_CKINVALID(vp, np, ND_READ)) { 317 do { 318 error = nqnfs_getlease(vp, ND_READ, cred, p); 319 } while (error == NQNFS_EXPIRED); 320 if (error) 321 return (error); 322 if (np->n_lrev != np->n_brev || 323 (np->n_flag & NQNFSNONCACHE) || 324 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) { 325 if (vp->v_type == VDIR) 326 nfs_invaldir(vp); 327 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 328 if (error) 329 return (error); 330 np->n_brev = np->n_lrev; 331 } 332 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) { 333 nfs_invaldir(vp); 334 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 335 if (error) 336 return (error); 337 } 338 } 339 if (np->n_flag & NQNFSNONCACHE) { 340 switch (vp->v_type) { 341 case VREG: 342 return (nfs_readrpc(vp, uio, cred)); 343 case VLNK: 344 return (nfs_readlinkrpc(vp, uio, cred)); 345 case VDIR: 346 break; 347 default: 348 printf(" NQNFSNONCACHE: type %x unexpected\n", 349 vp->v_type); 350 }; 351 } 352 switch (vp->v_type) { 353 case VREG: 354 nfsstats.biocache_reads++; 355 lbn = uio->uio_offset / biosize; 356 on = uio->uio_offset & (biosize - 1); 357 not_readin = 1; 358 359 /* 360 * Start the read ahead(s), as required. 361 */ 362 if (nfs_numasync > 0 && nmp->nm_readahead > 0) { 363 for (nra = 0; nra < nmp->nm_readahead && 364 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) { 365 rabn = lbn + 1 + nra; 366 if (!incore(vp, rabn)) { 367 rabp = nfs_getcacheblk(vp, rabn, biosize, p); 368 if (!rabp) 369 return (EINTR); 370 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 371 rabp->b_flags |= (B_READ | B_ASYNC); 372 vfs_busy_pages(rabp, 0); 373 if (nfs_asyncio(rabp, cred)) { 374 rabp->b_flags |= B_INVAL|B_ERROR; 375 vfs_unbusy_pages(rabp); 376 brelse(rabp); 377 } 378 } else 379 brelse(rabp); 380 } 381 } 382 } 383 384 /* 385 * If the block is in the cache and has the required data 386 * in a valid region, just copy it out. 387 * Otherwise, get the block and write back/read in, 388 * as required. 389 */ 390again: 391 bufsize = biosize; 392 if ((off_t)(lbn + 1) * biosize > np->n_size && 393 (off_t)(lbn + 1) * biosize - np->n_size < biosize) { 394 bufsize = np->n_size - lbn * biosize; 395 bufsize = (bufsize + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 396 } 397 bp = nfs_getcacheblk(vp, lbn, bufsize, p); 398 if (!bp) 399 return (EINTR); 400 /* 401 * If we are being called from nfs_getpages, we must 402 * make sure the buffer is a vmio buffer. The vp will 403 * already be setup for vmio but there may be some old 404 * non-vmio buffers attached to it. 405 */ 406 if (getpages && !(bp->b_flags & B_VMIO)) { 407#ifdef DIAGNOSTIC 408 printf("nfs_bioread: non vmio buf found, discarding\n"); 409#endif 410 bp->b_flags |= B_NOCACHE; 411 bp->b_flags |= B_INVAFTERWRITE; 412 if (bp->b_dirtyend > 0) { 413 if ((bp->b_flags & B_DELWRI) == 0) 414 panic("nfsbioread"); 415 if (VOP_BWRITE(bp) == EINTR) 416 return (EINTR); 417 } else 418 brelse(bp); 419 goto again; 420 } 421 if ((bp->b_flags & B_CACHE) == 0) { 422 bp->b_flags |= B_READ; 423 bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL); 424 not_readin = 0; 425 vfs_busy_pages(bp, 0); 426 error = nfs_doio(bp, cred, p); 427 if (error) { 428 brelse(bp); 429 return (error); 430 } 431 } 432 if (bufsize > on) { 433 n = min((unsigned)(bufsize - on), uio->uio_resid); 434 } else { 435 n = 0; 436 } 437 diff = np->n_size - uio->uio_offset; 438 if (diff < n) 439 n = diff; 440 if (not_readin && n > 0) { 441 if (on < bp->b_validoff || (on + n) > bp->b_validend) { 442 bp->b_flags |= B_NOCACHE; 443 bp->b_flags |= B_INVAFTERWRITE; 444 if (bp->b_dirtyend > 0) { 445 if ((bp->b_flags & B_DELWRI) == 0) 446 panic("nfsbioread"); 447 if (VOP_BWRITE(bp) == EINTR) 448 return (EINTR); 449 } else 450 brelse(bp); 451 goto again; 452 } 453 } 454 vp->v_lastr = lbn; 455 diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on); 456 if (diff < n) 457 n = diff; 458 break; 459 case VLNK: 460 nfsstats.biocache_readlinks++; 461 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p); 462 if (!bp) 463 return (EINTR); 464 if ((bp->b_flags & B_CACHE) == 0) { 465 bp->b_flags |= B_READ; 466 vfs_busy_pages(bp, 0); 467 error = nfs_doio(bp, cred, p); 468 if (error) { 469 bp->b_flags |= B_ERROR; 470 brelse(bp); 471 return (error); 472 } 473 } 474 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid); 475 on = 0; 476 break; 477 case VDIR: 478 nfsstats.biocache_readdirs++; 479 if (np->n_direofoffset 480 && uio->uio_offset >= np->n_direofoffset) { 481 return (0); 482 } 483 lbn = uio->uio_offset / NFS_DIRBLKSIZ; 484 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 485 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p); 486 if (!bp) 487 return (EINTR); 488 if ((bp->b_flags & B_CACHE) == 0) { 489 bp->b_flags |= B_READ; 490 vfs_busy_pages(bp, 0); 491 error = nfs_doio(bp, cred, p); 492 if (error) { 493 brelse(bp); 494 } 495 while (error == NFSERR_BAD_COOKIE) { 496 nfs_invaldir(vp); 497 error = nfs_vinvalbuf(vp, 0, cred, p, 1); 498 /* 499 * Yuck! The directory has been modified on the 500 * server. The only way to get the block is by 501 * reading from the beginning to get all the 502 * offset cookies. 503 */ 504 for (i = 0; i <= lbn && !error; i++) { 505 if (np->n_direofoffset 506 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) 507 return (0); 508 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p); 509 if (!bp) 510 return (EINTR); 511 if ((bp->b_flags & B_DONE) == 0) { 512 bp->b_flags |= B_READ; 513 vfs_busy_pages(bp, 0); 514 error = nfs_doio(bp, cred, p); 515 if (error) { 516 brelse(bp); 517 } else if (i < lbn) { 518 brelse(bp); 519 } 520 } 521 } 522 } 523 if (error) 524 return (error); 525 } 526 527 /* 528 * If not eof and read aheads are enabled, start one. 529 * (You need the current block first, so that you have the 530 * directory offset cookie of the next block.) 531 */ 532 if (nfs_numasync > 0 && nmp->nm_readahead > 0 && 533 (np->n_direofoffset == 0 || 534 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) && 535 !(np->n_flag & NQNFSNONCACHE) && 536 !incore(vp, lbn + 1)) { 537 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p); 538 if (rabp) { 539 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 540 rabp->b_flags |= (B_READ | B_ASYNC); 541 vfs_busy_pages(rabp, 0); 542 if (nfs_asyncio(rabp, cred)) { 543 rabp->b_flags |= B_INVAL|B_ERROR; 544 vfs_unbusy_pages(rabp); 545 brelse(rabp); 546 } 547 } else { 548 brelse(rabp); 549 } 550 } 551 } 552 /* 553 * Make sure we use a signed variant of min() since 554 * the second term may be negative. 555 */ 556 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on); 557 break; 558 default: 559 printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 560 break; 561 }; 562 563 if (n > 0) { 564 error = uiomove(bp->b_data + on, (int)n, uio); 565 } 566 switch (vp->v_type) { 567 case VREG: 568 break; 569 case VLNK: 570 n = 0; 571 break; 572 case VDIR: 573 if (np->n_flag & NQNFSNONCACHE) 574 bp->b_flags |= B_INVAL; 575 break; 576 default: 577 printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 578 } 579 brelse(bp); 580 } while (error == 0 && uio->uio_resid > 0 && n > 0); 581 return (error); 582} 583 584static void 585nfs_prot_buf(bp, off, n) 586 struct buf *bp; 587 int off; 588 int n; 589{ 590 int pindex, boff, end; 591 592 if ((bp->b_flags & B_VMIO) == 0) 593 return; 594 595 end = round_page(off + n); 596 for (boff = trunc_page(off); boff < end; boff += PAGE_SIZE) { 597 pindex = boff >> PAGE_SHIFT; 598 vm_page_protect(bp->b_pages[pindex], VM_PROT_NONE); 599 } 600} 601 602/* 603 * Vnode op for write using bio 604 */ 605int 606nfs_write(ap) 607 struct vop_write_args /* { 608 struct vnode *a_vp; 609 struct uio *a_uio; 610 int a_ioflag; 611 struct ucred *a_cred; 612 } */ *ap; 613{ 614 register int biosize; 615 register struct uio *uio = ap->a_uio; 616 struct proc *p = uio->uio_procp; 617 register struct vnode *vp = ap->a_vp; 618 struct nfsnode *np = VTONFS(vp); 619 register struct ucred *cred = ap->a_cred; 620 int ioflag = ap->a_ioflag; 621 struct buf *bp; 622 struct vattr vattr; 623 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 624 daddr_t lbn; 625 int bufsize; 626 int n, on, error = 0, iomode, must_commit; 627 628#ifdef DIAGNOSTIC 629 if (uio->uio_rw != UIO_WRITE) 630 panic("nfs_write mode"); 631 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc) 632 panic("nfs_write proc"); 633#endif 634 if (vp->v_type != VREG) 635 return (EIO); 636 if (np->n_flag & NWRITEERR) { 637 np->n_flag &= ~NWRITEERR; 638 return (np->n_error); 639 } 640 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 641 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 642 (void)nfs_fsinfo(nmp, vp, cred, p); 643 if (ioflag & (IO_APPEND | IO_SYNC)) { 644 if (np->n_flag & NMODIFIED) { 645 np->n_attrstamp = 0; 646 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 647 if (error) 648 return (error); 649 } 650 if (ioflag & IO_APPEND) { 651 np->n_attrstamp = 0; 652 error = VOP_GETATTR(vp, &vattr, cred, p); 653 if (error) 654 return (error); 655 uio->uio_offset = np->n_size; 656 } 657 } 658 if (uio->uio_offset < 0) 659 return (EINVAL); 660 if (uio->uio_resid == 0) 661 return (0); 662 /* 663 * Maybe this should be above the vnode op call, but so long as 664 * file servers have no limits, i don't think it matters 665 */ 666 if (p && uio->uio_offset + uio->uio_resid > 667 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 668 psignal(p, SIGXFSZ); 669 return (EFBIG); 670 } 671 /* 672 * I use nm_rsize, not nm_wsize so that all buffer cache blocks 673 * will be the same size within a filesystem. nfs_writerpc will 674 * still use nm_wsize when sizing the rpc's. 675 */ 676 biosize = vp->v_mount->mnt_stat.f_iosize; 677 do { 678 /* 679 * Check for a valid write lease. 680 */ 681 if ((nmp->nm_flag & NFSMNT_NQNFS) && 682 NQNFS_CKINVALID(vp, np, ND_WRITE)) { 683 do { 684 error = nqnfs_getlease(vp, ND_WRITE, cred, p); 685 } while (error == NQNFS_EXPIRED); 686 if (error) 687 return (error); 688 if (np->n_lrev != np->n_brev || 689 (np->n_flag & NQNFSNONCACHE)) { 690 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 691 if (error) 692 return (error); 693 np->n_brev = np->n_lrev; 694 } 695 } 696 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) { 697 iomode = NFSV3WRITE_FILESYNC; 698 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit); 699 if (must_commit) 700 nfs_clearcommit(vp->v_mount); 701 return (error); 702 } 703 nfsstats.biocache_writes++; 704 lbn = uio->uio_offset / biosize; 705 on = uio->uio_offset & (biosize-1); 706 n = min((unsigned)(biosize - on), uio->uio_resid); 707again: 708 if (uio->uio_offset + n > np->n_size) { 709 np->n_size = uio->uio_offset + n; 710 np->n_flag |= NMODIFIED; 711 vnode_pager_setsize(vp, (u_long)np->n_size); 712 } 713 bufsize = biosize; 714 if ((lbn + 1) * biosize > np->n_size) { 715 bufsize = np->n_size - lbn * biosize; 716 bufsize = (bufsize + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 717 } 718 bp = nfs_getcacheblk(vp, lbn, bufsize, p); 719 if (!bp) 720 return (EINTR); 721 if (bp->b_wcred == NOCRED) { 722 crhold(cred); 723 bp->b_wcred = cred; 724 } 725 np->n_flag |= NMODIFIED; 726 727 if ((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend > np->n_size) { 728 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE); 729 } 730 731 /* 732 * If the new write will leave a contiguous dirty 733 * area, just update the b_dirtyoff and b_dirtyend, 734 * otherwise force a write rpc of the old dirty area. 735 */ 736 if (bp->b_dirtyend > 0 && 737 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 738 bp->b_proc = p; 739 if (VOP_BWRITE(bp) == EINTR) 740 return (EINTR); 741 goto again; 742 } 743 744 /* 745 * Check for valid write lease and get one as required. 746 * In case getblk() and/or bwrite() delayed us. 747 */ 748 if ((nmp->nm_flag & NFSMNT_NQNFS) && 749 NQNFS_CKINVALID(vp, np, ND_WRITE)) { 750 do { 751 error = nqnfs_getlease(vp, ND_WRITE, cred, p); 752 } while (error == NQNFS_EXPIRED); 753 if (error) { 754 brelse(bp); 755 return (error); 756 } 757 if (np->n_lrev != np->n_brev || 758 (np->n_flag & NQNFSNONCACHE)) { 759 brelse(bp); 760 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 761 if (error) 762 return (error); 763 np->n_brev = np->n_lrev; 764 goto again; 765 } 766 } 767 768 error = uiomove((char *)bp->b_data + on, n, uio); 769 if (error) { 770 bp->b_flags |= B_ERROR; 771 brelse(bp); 772 return (error); 773 } 774 775 /* 776 * This will keep the buffer and mmaped regions more coherent. 777 */ 778 nfs_prot_buf(bp, on, n); 779 780 if (bp->b_dirtyend > 0) { 781 bp->b_dirtyoff = min(on, bp->b_dirtyoff); 782 bp->b_dirtyend = max((on + n), bp->b_dirtyend); 783 } else { 784 bp->b_dirtyoff = on; 785 bp->b_dirtyend = on + n; 786 } 787 if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff || 788 bp->b_validoff > bp->b_dirtyend) { 789 bp->b_validoff = bp->b_dirtyoff; 790 bp->b_validend = bp->b_dirtyend; 791 } else { 792 bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff); 793 bp->b_validend = max(bp->b_validend, bp->b_dirtyend); 794 } 795 796 /* 797 * Since this block is being modified, it must be written 798 * again and not just committed. 799 */ 800 bp->b_flags &= ~B_NEEDCOMMIT; 801 802 /* 803 * If the lease is non-cachable or IO_SYNC do bwrite(). 804 */ 805 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) { 806 bp->b_proc = p; 807 if (ioflag & IO_INVAL) 808 bp->b_flags |= B_INVAL; 809 error = VOP_BWRITE(bp); 810 if (error) 811 return (error); 812 if (np->n_flag & NQNFSNONCACHE) { 813 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 814 if (error) 815 return (error); 816 } 817 } else if ((n + on) == biosize && 818 (nmp->nm_flag & NFSMNT_NQNFS) == 0) { 819 bp->b_proc = (struct proc *)0; 820 bp->b_flags |= B_ASYNC; 821 (void)nfs_writebp(bp, 0); 822 } else 823 bdwrite(bp); 824 } while (uio->uio_resid > 0 && n > 0); 825 return (0); 826} 827 828/* 829 * Get an nfs cache block. 830 * Allocate a new one if the block isn't currently in the cache 831 * and return the block marked busy. If the calling process is 832 * interrupted by a signal for an interruptible mount point, return 833 * NULL. 834 */ 835static struct buf * 836nfs_getcacheblk(vp, bn, size, p) 837 struct vnode *vp; 838 daddr_t bn; 839 int size; 840 struct proc *p; 841{ 842 register struct buf *bp; 843 struct mount *mp; 844 struct nfsmount *nmp; 845 846 mp = vp->v_mount; 847 nmp = VFSTONFS(mp); 848 849 if (nmp->nm_flag & NFSMNT_INT) { 850 bp = getblk(vp, bn, size, PCATCH, 0); 851 while (bp == (struct buf *)0) { 852 if (nfs_sigintr(nmp, (struct nfsreq *)0, p)) 853 return ((struct buf *)0); 854 bp = getblk(vp, bn, size, 0, 2 * hz); 855 } 856 } else 857 bp = getblk(vp, bn, size, 0, 0); 858 859 if( vp->v_type == VREG) { 860 int biosize; 861 biosize = mp->mnt_stat.f_iosize; 862 bp->b_blkno = (bn * biosize) / DEV_BSIZE; 863 } 864 865 return (bp); 866} 867 868/* 869 * Flush and invalidate all dirty buffers. If another process is already 870 * doing the flush, just wait for completion. 871 */ 872int 873nfs_vinvalbuf(vp, flags, cred, p, intrflg) 874 struct vnode *vp; 875 int flags; 876 struct ucred *cred; 877 struct proc *p; 878 int intrflg; 879{ 880 register struct nfsnode *np = VTONFS(vp); 881 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 882 int error = 0, slpflag, slptimeo; 883 884 if (vp->v_flag & VXLOCK) { 885 return (0); 886 } 887 888 if ((nmp->nm_flag & NFSMNT_INT) == 0) 889 intrflg = 0; 890 if (intrflg) { 891 slpflag = PCATCH; 892 slptimeo = 2 * hz; 893 } else { 894 slpflag = 0; 895 slptimeo = 0; 896 } 897 /* 898 * First wait for any other process doing a flush to complete. 899 */ 900 while (np->n_flag & NFLUSHINPROG) { 901 np->n_flag |= NFLUSHWANT; 902 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval", 903 slptimeo); 904 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) 905 return (EINTR); 906 } 907 908 /* 909 * Now, flush as required. 910 */ 911 np->n_flag |= NFLUSHINPROG; 912 error = vinvalbuf(vp, flags, cred, p, slpflag, 0); 913 while (error) { 914 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) { 915 np->n_flag &= ~NFLUSHINPROG; 916 if (np->n_flag & NFLUSHWANT) { 917 np->n_flag &= ~NFLUSHWANT; 918 wakeup((caddr_t)&np->n_flag); 919 } 920 return (EINTR); 921 } 922 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo); 923 } 924 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG); 925 if (np->n_flag & NFLUSHWANT) { 926 np->n_flag &= ~NFLUSHWANT; 927 wakeup((caddr_t)&np->n_flag); 928 } 929 return (0); 930} 931 932/* 933 * Initiate asynchronous I/O. Return an error if no nfsiods are available. 934 * This is mainly to avoid queueing async I/O requests when the nfsiods 935 * are all hung on a dead server. 936 */ 937int 938nfs_asyncio(bp, cred) 939 register struct buf *bp; 940 struct ucred *cred; 941{ 942 struct nfsmount *nmp; 943 int i; 944 int gotiod; 945 int slpflag = 0; 946 int slptimeo = 0; 947 int error; 948 949 if (nfs_numasync == 0) 950 return (EIO); 951 952 nmp = VFSTONFS(bp->b_vp->v_mount); 953again: 954 if (nmp->nm_flag & NFSMNT_INT) 955 slpflag = PCATCH; 956 gotiod = FALSE; 957 958 /* 959 * Find a free iod to process this request. 960 */ 961 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 962 if (nfs_iodwant[i]) { 963 /* 964 * Found one, so wake it up and tell it which 965 * mount to process. 966 */ 967 NFS_DPF(ASYNCIO, 968 ("nfs_asyncio: waking iod %d for mount %p\n", 969 i, nmp)); 970 nfs_iodwant[i] = (struct proc *)0; 971 nfs_iodmount[i] = nmp; 972 nmp->nm_bufqiods++; 973 wakeup((caddr_t)&nfs_iodwant[i]); 974 gotiod = TRUE; 975 break; 976 } 977 978 /* 979 * If none are free, we may already have an iod working on this mount 980 * point. If so, it will process our request. 981 */ 982 if (!gotiod) { 983 if (nmp->nm_bufqiods > 0) { 984 NFS_DPF(ASYNCIO, 985 ("nfs_asyncio: %d iods are already processing mount %p\n", 986 nmp->nm_bufqiods, nmp)); 987 gotiod = TRUE; 988 } 989 } 990 991 /* 992 * If we have an iod which can process the request, then queue 993 * the buffer. 994 */ 995 if (gotiod) { 996 /* 997 * Ensure that the queue never grows too large. 998 */ 999 while (nmp->nm_bufqlen >= 2*nfs_numasync) { 1000 NFS_DPF(ASYNCIO, 1001 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp)); 1002 nmp->nm_bufqwant = TRUE; 1003 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO, 1004 "nfsaio", slptimeo); 1005 if (error) { 1006 if (nfs_sigintr(nmp, NULL, bp->b_proc)) 1007 return (EINTR); 1008 if (slpflag == PCATCH) { 1009 slpflag = 0; 1010 slptimeo = 2 * hz; 1011 } 1012 } 1013 /* 1014 * We might have lost our iod while sleeping, 1015 * so check and loop if nescessary. 1016 */ 1017 if (nmp->nm_bufqiods == 0) { 1018 NFS_DPF(ASYNCIO, 1019 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 1020 goto again; 1021 } 1022 } 1023 1024 if (bp->b_flags & B_READ) { 1025 if (bp->b_rcred == NOCRED && cred != NOCRED) { 1026 crhold(cred); 1027 bp->b_rcred = cred; 1028 } 1029 } else { 1030 bp->b_flags |= B_WRITEINPROG; 1031 if (bp->b_wcred == NOCRED && cred != NOCRED) { 1032 crhold(cred); 1033 bp->b_wcred = cred; 1034 } 1035 } 1036 1037 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 1038 nmp->nm_bufqlen++; 1039 return (0); 1040 } 1041 1042 /* 1043 * All the iods are busy on other mounts, so return EIO to 1044 * force the caller to process the i/o synchronously. 1045 */ 1046 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n")); 1047 return (EIO); 1048} 1049 1050/* 1051 * Do an I/O operation to/from a cache block. This may be called 1052 * synchronously or from an nfsiod. 1053 */ 1054int 1055nfs_doio(bp, cr, p) 1056 register struct buf *bp; 1057 struct ucred *cr; 1058 struct proc *p; 1059{ 1060 register struct uio *uiop; 1061 register struct vnode *vp; 1062 struct nfsnode *np; 1063 struct nfsmount *nmp; 1064 int error = 0, diff, len, iomode, must_commit = 0; 1065 struct uio uio; 1066 struct iovec io; 1067 1068 vp = bp->b_vp; 1069 np = VTONFS(vp); 1070 nmp = VFSTONFS(vp->v_mount); 1071 uiop = &uio; 1072 uiop->uio_iov = &io; 1073 uiop->uio_iovcnt = 1; 1074 uiop->uio_segflg = UIO_SYSSPACE; 1075 uiop->uio_procp = p; 1076 1077 /* 1078 * Historically, paging was done with physio, but no more. 1079 */ 1080 if (bp->b_flags & B_PHYS) { 1081 /* 1082 * ...though reading /dev/drum still gets us here. 1083 */ 1084 io.iov_len = uiop->uio_resid = bp->b_bcount; 1085 /* mapping was done by vmapbuf() */ 1086 io.iov_base = bp->b_data; 1087 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1088 if (bp->b_flags & B_READ) { 1089 uiop->uio_rw = UIO_READ; 1090 nfsstats.read_physios++; 1091 error = nfs_readrpc(vp, uiop, cr); 1092 } else { 1093 int com; 1094 1095 iomode = NFSV3WRITE_DATASYNC; 1096 uiop->uio_rw = UIO_WRITE; 1097 nfsstats.write_physios++; 1098 error = nfs_writerpc(vp, uiop, cr, &iomode, &com); 1099 } 1100 if (error) { 1101 bp->b_flags |= B_ERROR; 1102 bp->b_error = error; 1103 } 1104 } else if (bp->b_flags & B_READ) { 1105 io.iov_len = uiop->uio_resid = bp->b_bcount; 1106 io.iov_base = bp->b_data; 1107 uiop->uio_rw = UIO_READ; 1108 switch (vp->v_type) { 1109 case VREG: 1110 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 1111 nfsstats.read_bios++; 1112 error = nfs_readrpc(vp, uiop, cr); 1113 if (!error) { 1114 bp->b_validoff = 0; 1115 if (uiop->uio_resid) { 1116 /* 1117 * If len > 0, there is a hole in the file and 1118 * no writes after the hole have been pushed to 1119 * the server yet. 1120 * Just zero fill the rest of the valid area. 1121 */ 1122 diff = bp->b_bcount - uiop->uio_resid; 1123 len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE 1124 + diff); 1125 if (len > 0) { 1126 len = min(len, uiop->uio_resid); 1127 bzero((char *)bp->b_data + diff, len); 1128 bp->b_validend = diff + len; 1129 } else 1130 bp->b_validend = diff; 1131 } else 1132 bp->b_validend = bp->b_bcount; 1133 } 1134 if (p && (vp->v_flag & VTEXT) && 1135 (((nmp->nm_flag & NFSMNT_NQNFS) && 1136 NQNFS_CKINVALID(vp, np, ND_READ) && 1137 np->n_lrev != np->n_brev) || 1138 (!(nmp->nm_flag & NFSMNT_NQNFS) && 1139 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) { 1140 uprintf("Process killed due to text file modification\n"); 1141 psignal(p, SIGKILL); 1142 p->p_flag |= P_NOSWAP; 1143 } 1144 break; 1145 case VLNK: 1146 uiop->uio_offset = (off_t)0; 1147 nfsstats.readlink_bios++; 1148 error = nfs_readlinkrpc(vp, uiop, cr); 1149 break; 1150 case VDIR: 1151 nfsstats.readdir_bios++; 1152 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 1153 if (nmp->nm_flag & NFSMNT_RDIRPLUS) { 1154 error = nfs_readdirplusrpc(vp, uiop, cr); 1155 if (error == NFSERR_NOTSUPP) 1156 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 1157 } 1158 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 1159 error = nfs_readdirrpc(vp, uiop, cr); 1160 break; 1161 default: 1162 printf("nfs_doio: type %x unexpected\n",vp->v_type); 1163 break; 1164 }; 1165 if (error) { 1166 bp->b_flags |= B_ERROR; 1167 bp->b_error = error; 1168 } 1169 } else { 1170 if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size) 1171 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE); 1172 1173 if (bp->b_dirtyend > bp->b_dirtyoff) { 1174 io.iov_len = uiop->uio_resid = bp->b_dirtyend 1175 - bp->b_dirtyoff; 1176 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE 1177 + bp->b_dirtyoff; 1178 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 1179 uiop->uio_rw = UIO_WRITE; 1180 nfsstats.write_bios++; 1181 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 1182 iomode = NFSV3WRITE_UNSTABLE; 1183 else 1184 iomode = NFSV3WRITE_FILESYNC; 1185 bp->b_flags |= B_WRITEINPROG; 1186 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit); 1187 if (!error && iomode == NFSV3WRITE_UNSTABLE) { 1188 bp->b_flags |= B_NEEDCOMMIT; 1189 if (bp->b_dirtyoff == 0 1190 && bp->b_dirtyend == bp->b_bufsize) 1191 bp->b_flags |= B_CLUSTEROK; 1192 } else 1193 bp->b_flags &= ~B_NEEDCOMMIT; 1194 bp->b_flags &= ~B_WRITEINPROG; 1195 1196 /* 1197 * For an interrupted write, the buffer is still valid 1198 * and the write hasn't been pushed to the server yet, 1199 * so we can't set B_ERROR and report the interruption 1200 * by setting B_EINTR. For the B_ASYNC case, B_EINTR 1201 * is not relevant, so the rpc attempt is essentially 1202 * a noop. For the case of a V3 write rpc not being 1203 * committed to stable storage, the block is still 1204 * dirty and requires either a commit rpc or another 1205 * write rpc with iomode == NFSV3WRITE_FILESYNC before 1206 * the block is reused. This is indicated by setting 1207 * the B_DELWRI and B_NEEDCOMMIT flags. 1208 */ 1209 if (error == EINTR 1210 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 1211 int s; 1212 1213 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 1214 ++numdirtybuffers; 1215 bp->b_flags |= B_DELWRI; 1216 s = splbio(); 1217 reassignbuf(bp, vp); 1218 splx(s); 1219 if ((bp->b_flags & B_ASYNC) == 0) 1220 bp->b_flags |= B_EINTR; 1221 } else { 1222 if (error) { 1223 bp->b_flags |= B_ERROR; 1224 bp->b_error = np->n_error = error; 1225 np->n_flag |= NWRITEERR; 1226 } 1227 bp->b_dirtyoff = bp->b_dirtyend = 0; 1228 } 1229 } else { 1230 bp->b_resid = 0; 1231 biodone(bp); 1232 return (0); 1233 } 1234 } 1235 bp->b_resid = uiop->uio_resid; 1236 if (must_commit) 1237 nfs_clearcommit(vp->v_mount); 1238 biodone(bp); 1239 return (error); 1240} 1241