nfs_bio.c revision 25785
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.36 1997/04/19 14:28:36 dfr 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#include <sys/sysctl.h> 51 52#include <vm/vm.h> 53#include <vm/vm_param.h> 54#include <vm/vm_extern.h> 55 56#include <nfs/rpcv2.h> 57#include <nfs/nfsproto.h> 58#include <nfs/nfs.h> 59#include <nfs/nfsmount.h> 60#include <nfs/nqnfs.h> 61#include <nfs/nfsnode.h> 62 63static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size, 64 struct proc *p)); 65 66extern int nfs_numasync; 67extern struct nfsstats nfsstats; 68 69/* 70 * Vnode op for read using bio 71 * Any similarity to readip() is purely coincidental 72 */ 73int 74nfs_bioread(vp, uio, ioflag, cred) 75 register struct vnode *vp; 76 register struct uio *uio; 77 int ioflag; 78 struct ucred *cred; 79{ 80 register struct nfsnode *np = VTONFS(vp); 81 register int biosize, diff, i; 82 struct buf *bp = 0, *rabp; 83 struct vattr vattr; 84 struct proc *p; 85 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 86 daddr_t lbn, rabn; 87 int bufsize; 88 int nra, error = 0, n = 0, on = 0, not_readin; 89 90#ifdef DIAGNOSTIC 91 if (uio->uio_rw != UIO_READ) 92 panic("nfs_read mode"); 93#endif 94 if (uio->uio_resid == 0) 95 return (0); 96 if (uio->uio_offset < 0) 97 return (EINVAL); 98 p = uio->uio_procp; 99 if ((nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_GOTFSINFO)) == NFSMNT_NFSV3) 100 (void)nfs_fsinfo(nmp, vp, cred, p); 101 biosize = vp->v_mount->mnt_stat.f_iosize; 102 /* 103 * For nfs, cache consistency can only be maintained approximately. 104 * Although RFC1094 does not specify the criteria, the following is 105 * believed to be compatible with the reference port. 106 * For nqnfs, full cache consistency is maintained within the loop. 107 * For nfs: 108 * If the file's modify time on the server has changed since the 109 * last read rpc or you have written to the file, 110 * you may have lost data cache consistency with the 111 * server, so flush all of the file's data out of the cache. 112 * Then force a getattr rpc to ensure that you have up to date 113 * attributes. 114 * NB: This implies that cache data can be read when up to 115 * NFS_ATTRTIMEO seconds out of date. If you find that you need current 116 * attributes this could be forced by setting n_attrstamp to 0 before 117 * the VOP_GETATTR() call. 118 */ 119 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) { 120 if (np->n_flag & NMODIFIED) { 121 if (vp->v_type != VREG) { 122 if (vp->v_type != VDIR) 123 panic("nfs: bioread, not dir"); 124 nfs_invaldir(vp); 125 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 126 if (error) 127 return (error); 128 } 129 np->n_attrstamp = 0; 130 error = VOP_GETATTR(vp, &vattr, cred, p); 131 if (error) 132 return (error); 133 np->n_mtime = vattr.va_mtime.tv_sec; 134 } else { 135 error = VOP_GETATTR(vp, &vattr, cred, p); 136 if (error) 137 return (error); 138 if (np->n_mtime != vattr.va_mtime.tv_sec) { 139 if (vp->v_type == VDIR) 140 nfs_invaldir(vp); 141 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 142 if (error) 143 return (error); 144 np->n_mtime = vattr.va_mtime.tv_sec; 145 } 146 } 147 } 148 do { 149 150 /* 151 * Get a valid lease. If cached data is stale, flush it. 152 */ 153 if (nmp->nm_flag & NFSMNT_NQNFS) { 154 if (NQNFS_CKINVALID(vp, np, ND_READ)) { 155 do { 156 error = nqnfs_getlease(vp, ND_READ, cred, p); 157 } while (error == NQNFS_EXPIRED); 158 if (error) 159 return (error); 160 if (np->n_lrev != np->n_brev || 161 (np->n_flag & NQNFSNONCACHE) || 162 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) { 163 if (vp->v_type == VDIR) 164 nfs_invaldir(vp); 165 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 166 if (error) 167 return (error); 168 np->n_brev = np->n_lrev; 169 } 170 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) { 171 nfs_invaldir(vp); 172 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 173 if (error) 174 return (error); 175 } 176 } 177 if (np->n_flag & NQNFSNONCACHE) { 178 switch (vp->v_type) { 179 case VREG: 180 return (nfs_readrpc(vp, uio, cred)); 181 case VLNK: 182 return (nfs_readlinkrpc(vp, uio, cred)); 183 case VDIR: 184 break; 185 default: 186 printf(" NQNFSNONCACHE: type %x unexpected\n", 187 vp->v_type); 188 }; 189 } 190 switch (vp->v_type) { 191 case VREG: 192 nfsstats.biocache_reads++; 193 lbn = uio->uio_offset / biosize; 194 on = uio->uio_offset & (biosize - 1); 195 not_readin = 1; 196 197 /* 198 * Start the read ahead(s), as required. 199 */ 200 if (nfs_numasync > 0 && nmp->nm_readahead > 0) { 201 for (nra = 0; nra < nmp->nm_readahead && 202 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) { 203 rabn = lbn + 1 + nra; 204 if (!incore(vp, rabn)) { 205 rabp = nfs_getcacheblk(vp, rabn, biosize, p); 206 if (!rabp) 207 return (EINTR); 208 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 209 rabp->b_flags |= (B_READ | B_ASYNC); 210 vfs_busy_pages(rabp, 0); 211 if (nfs_asyncio(rabp, cred)) { 212 rabp->b_flags |= B_INVAL|B_ERROR; 213 vfs_unbusy_pages(rabp); 214 brelse(rabp); 215 } 216 } else 217 brelse(rabp); 218 } 219 } 220 } 221 222 /* 223 * If the block is in the cache and has the required data 224 * in a valid region, just copy it out. 225 * Otherwise, get the block and write back/read in, 226 * as required. 227 */ 228again: 229 bufsize = biosize; 230 if ((off_t)(lbn + 1) * biosize > np->n_size && 231 (off_t)(lbn + 1) * biosize - np->n_size < biosize) { 232 bufsize = np->n_size - lbn * biosize; 233 bufsize = (bufsize + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 234 } 235 bp = nfs_getcacheblk(vp, lbn, bufsize, p); 236 if (!bp) 237 return (EINTR); 238 if ((bp->b_flags & B_CACHE) == 0) { 239 bp->b_flags |= B_READ; 240 bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL); 241 not_readin = 0; 242 vfs_busy_pages(bp, 0); 243 error = nfs_doio(bp, cred, p); 244 if (error) { 245 brelse(bp); 246 return (error); 247 } 248 } 249 if (bufsize > on) { 250 n = min((unsigned)(bufsize - on), uio->uio_resid); 251 } else { 252 n = 0; 253 } 254 diff = np->n_size - uio->uio_offset; 255 if (diff < n) 256 n = diff; 257 if (not_readin && n > 0) { 258 if (on < bp->b_validoff || (on + n) > bp->b_validend) { 259 bp->b_flags |= B_NOCACHE; 260 bp->b_flags |= B_INVAFTERWRITE; 261 if (bp->b_dirtyend > 0) { 262 if ((bp->b_flags & B_DELWRI) == 0) 263 panic("nfsbioread"); 264 if (VOP_BWRITE(bp) == EINTR) 265 return (EINTR); 266 } else 267 brelse(bp); 268 goto again; 269 } 270 } 271 vp->v_lastr = lbn; 272 diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on); 273 if (diff < n) 274 n = diff; 275 break; 276 case VLNK: 277 nfsstats.biocache_readlinks++; 278 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p); 279 if (!bp) 280 return (EINTR); 281 if ((bp->b_flags & B_CACHE) == 0) { 282 bp->b_flags |= B_READ; 283 vfs_busy_pages(bp, 0); 284 error = nfs_doio(bp, cred, p); 285 if (error) { 286 bp->b_flags |= B_ERROR; 287 brelse(bp); 288 return (error); 289 } 290 } 291 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid); 292 on = 0; 293 break; 294 case VDIR: 295 nfsstats.biocache_readdirs++; 296 if (np->n_direofoffset 297 && uio->uio_offset >= np->n_direofoffset) { 298 return (0); 299 } 300 lbn = uio->uio_offset / NFS_DIRBLKSIZ; 301 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 302 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p); 303 if (!bp) 304 return (EINTR); 305 if ((bp->b_flags & B_CACHE) == 0) { 306 bp->b_flags |= B_READ; 307 vfs_busy_pages(bp, 0); 308 error = nfs_doio(bp, cred, p); 309 if (error) { 310 vfs_unbusy_pages(bp); 311 brelse(bp); 312 while (error == NFSERR_BAD_COOKIE) { 313 nfs_invaldir(vp); 314 error = nfs_vinvalbuf(vp, 0, cred, p, 1); 315 /* 316 * Yuck! The directory has been modified on the 317 * server. The only way to get the block is by 318 * reading from the beginning to get all the 319 * offset cookies. 320 */ 321 for (i = 0; i <= lbn && !error; i++) { 322 if (np->n_direofoffset 323 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) 324 return (0); 325 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p); 326 if (!bp) 327 return (EINTR); 328 if ((bp->b_flags & B_DONE) == 0) { 329 bp->b_flags |= B_READ; 330 vfs_busy_pages(bp, 0); 331 error = nfs_doio(bp, cred, p); 332 if (error) { 333 vfs_unbusy_pages(bp); 334 brelse(bp); 335 } else if (i < lbn) 336 brelse(bp); 337 } 338 } 339 } 340 if (error) 341 return (error); 342 } 343 } 344 345 /* 346 * If not eof and read aheads are enabled, start one. 347 * (You need the current block first, so that you have the 348 * directory offset cookie of the next block.) 349 */ 350 if (nfs_numasync > 0 && nmp->nm_readahead > 0 && 351 (np->n_direofoffset == 0 || 352 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) && 353 !(np->n_flag & NQNFSNONCACHE) && 354 !incore(vp, lbn + 1)) { 355 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p); 356 if (rabp) { 357 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 358 rabp->b_flags |= (B_READ | B_ASYNC); 359 vfs_busy_pages(rabp, 0); 360 if (nfs_asyncio(rabp, cred)) { 361 rabp->b_flags |= B_INVAL|B_ERROR; 362 vfs_unbusy_pages(rabp); 363 brelse(rabp); 364 } 365 } else { 366 brelse(rabp); 367 } 368 } 369 } 370 n = min(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on); 371 break; 372 default: 373 printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 374 break; 375 }; 376 377 if (n > 0) { 378 error = uiomove(bp->b_data + on, (int)n, uio); 379 } 380 switch (vp->v_type) { 381 case VREG: 382 break; 383 case VLNK: 384 n = 0; 385 break; 386 case VDIR: 387 if (np->n_flag & NQNFSNONCACHE) 388 bp->b_flags |= B_INVAL; 389 break; 390 default: 391 printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 392 } 393 brelse(bp); 394 } while (error == 0 && uio->uio_resid > 0 && n > 0); 395 return (error); 396} 397 398/* 399 * Vnode op for write using bio 400 */ 401int 402nfs_write(ap) 403 struct vop_write_args /* { 404 struct vnode *a_vp; 405 struct uio *a_uio; 406 int a_ioflag; 407 struct ucred *a_cred; 408 } */ *ap; 409{ 410 register int biosize; 411 register struct uio *uio = ap->a_uio; 412 struct proc *p = uio->uio_procp; 413 register struct vnode *vp = ap->a_vp; 414 struct nfsnode *np = VTONFS(vp); 415 register struct ucred *cred = ap->a_cred; 416 int ioflag = ap->a_ioflag; 417 struct buf *bp; 418 struct vattr vattr; 419 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 420 daddr_t lbn; 421 int bufsize; 422 int n, on, error = 0, iomode, must_commit; 423 424#ifdef DIAGNOSTIC 425 if (uio->uio_rw != UIO_WRITE) 426 panic("nfs_write mode"); 427 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc) 428 panic("nfs_write proc"); 429#endif 430 if (vp->v_type != VREG) 431 return (EIO); 432 if (np->n_flag & NWRITEERR) { 433 np->n_flag &= ~NWRITEERR; 434 return (np->n_error); 435 } 436 if ((nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_GOTFSINFO)) == NFSMNT_NFSV3) 437 (void)nfs_fsinfo(nmp, vp, cred, p); 438 if (ioflag & (IO_APPEND | IO_SYNC)) { 439 if (np->n_flag & NMODIFIED) { 440 np->n_attrstamp = 0; 441 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 442 if (error) 443 return (error); 444 } 445 if (ioflag & IO_APPEND) { 446 np->n_attrstamp = 0; 447 error = VOP_GETATTR(vp, &vattr, cred, p); 448 if (error) 449 return (error); 450 uio->uio_offset = np->n_size; 451 } 452 } 453 if (uio->uio_offset < 0) 454 return (EINVAL); 455 if (uio->uio_resid == 0) 456 return (0); 457 /* 458 * Maybe this should be above the vnode op call, but so long as 459 * file servers have no limits, i don't think it matters 460 */ 461 if (p && uio->uio_offset + uio->uio_resid > 462 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 463 psignal(p, SIGXFSZ); 464 return (EFBIG); 465 } 466 /* 467 * I use nm_rsize, not nm_wsize so that all buffer cache blocks 468 * will be the same size within a filesystem. nfs_writerpc will 469 * still use nm_wsize when sizing the rpc's. 470 */ 471 biosize = vp->v_mount->mnt_stat.f_iosize; 472 do { 473 /* 474 * Check for a valid write lease. 475 */ 476 if ((nmp->nm_flag & NFSMNT_NQNFS) && 477 NQNFS_CKINVALID(vp, np, ND_WRITE)) { 478 do { 479 error = nqnfs_getlease(vp, ND_WRITE, cred, p); 480 } while (error == NQNFS_EXPIRED); 481 if (error) 482 return (error); 483 if (np->n_lrev != np->n_brev || 484 (np->n_flag & NQNFSNONCACHE)) { 485 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 486 if (error) 487 return (error); 488 np->n_brev = np->n_lrev; 489 } 490 } 491 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) { 492 iomode = NFSV3WRITE_FILESYNC; 493 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit); 494 if (must_commit) 495 nfs_clearcommit(vp->v_mount); 496 return (error); 497 } 498 nfsstats.biocache_writes++; 499 lbn = uio->uio_offset / biosize; 500 on = uio->uio_offset & (biosize-1); 501 n = min((unsigned)(biosize - on), uio->uio_resid); 502again: 503 if (uio->uio_offset + n > np->n_size) { 504 np->n_size = uio->uio_offset + n; 505 np->n_flag |= NMODIFIED; 506 vnode_pager_setsize(vp, (u_long)np->n_size); 507 } 508 bufsize = biosize; 509 if ((lbn + 1) * biosize > np->n_size) { 510 bufsize = np->n_size - lbn * biosize; 511 bufsize = (bufsize + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); 512 } 513 bp = nfs_getcacheblk(vp, lbn, bufsize, p); 514 if (!bp) 515 return (EINTR); 516 if (bp->b_wcred == NOCRED) { 517 crhold(cred); 518 bp->b_wcred = cred; 519 } 520 np->n_flag |= NMODIFIED; 521 522 if ((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend > np->n_size) { 523 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE); 524 } 525 526 /* 527 * If the new write will leave a contiguous dirty 528 * area, just update the b_dirtyoff and b_dirtyend, 529 * otherwise force a write rpc of the old dirty area. 530 */ 531 if (bp->b_dirtyend > 0 && 532 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 533 bp->b_proc = p; 534 if (VOP_BWRITE(bp) == EINTR) 535 return (EINTR); 536 goto again; 537 } 538 539 /* 540 * Check for valid write lease and get one as required. 541 * In case getblk() and/or bwrite() delayed us. 542 */ 543 if ((nmp->nm_flag & NFSMNT_NQNFS) && 544 NQNFS_CKINVALID(vp, np, ND_WRITE)) { 545 do { 546 error = nqnfs_getlease(vp, ND_WRITE, cred, p); 547 } while (error == NQNFS_EXPIRED); 548 if (error) { 549 brelse(bp); 550 return (error); 551 } 552 if (np->n_lrev != np->n_brev || 553 (np->n_flag & NQNFSNONCACHE)) { 554 brelse(bp); 555 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 556 if (error) 557 return (error); 558 np->n_brev = np->n_lrev; 559 goto again; 560 } 561 } 562 error = uiomove((char *)bp->b_data + on, n, uio); 563 if (error) { 564 bp->b_flags |= B_ERROR; 565 brelse(bp); 566 return (error); 567 } 568 if (bp->b_dirtyend > 0) { 569 bp->b_dirtyoff = min(on, bp->b_dirtyoff); 570 bp->b_dirtyend = max((on + n), bp->b_dirtyend); 571 } else { 572 bp->b_dirtyoff = on; 573 bp->b_dirtyend = on + n; 574 } 575 if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff || 576 bp->b_validoff > bp->b_dirtyend) { 577 bp->b_validoff = bp->b_dirtyoff; 578 bp->b_validend = bp->b_dirtyend; 579 } else { 580 bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff); 581 bp->b_validend = max(bp->b_validend, bp->b_dirtyend); 582 } 583 584 /* 585 * Since this block is being modified, it must be written 586 * again and not just committed. 587 */ 588 bp->b_flags &= ~B_NEEDCOMMIT; 589 590 /* 591 * If the lease is non-cachable or IO_SYNC do bwrite(). 592 */ 593 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) { 594 bp->b_proc = p; 595 error = VOP_BWRITE(bp); 596 if (error) 597 return (error); 598 if (np->n_flag & NQNFSNONCACHE) { 599 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 600 if (error) 601 return (error); 602 } 603 } else if ((n + on) == biosize && 604 (nmp->nm_flag & NFSMNT_NQNFS) == 0) { 605 bp->b_proc = (struct proc *)0; 606 bp->b_flags |= B_ASYNC; 607 (void)nfs_writebp(bp, 0); 608 } else 609 bdwrite(bp); 610 } while (uio->uio_resid > 0 && n > 0); 611 return (0); 612} 613 614/* 615 * Get an nfs cache block. 616 * Allocate a new one if the block isn't currently in the cache 617 * and return the block marked busy. If the calling process is 618 * interrupted by a signal for an interruptible mount point, return 619 * NULL. 620 */ 621static struct buf * 622nfs_getcacheblk(vp, bn, size, p) 623 struct vnode *vp; 624 daddr_t bn; 625 int size; 626 struct proc *p; 627{ 628 register struct buf *bp; 629 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 630 int biosize = vp->v_mount->mnt_stat.f_iosize; 631 632 if (nmp->nm_flag & NFSMNT_INT) { 633 bp = getblk(vp, bn, size, PCATCH, 0); 634 while (bp == (struct buf *)0) { 635 if (nfs_sigintr(nmp, (struct nfsreq *)0, p)) 636 return ((struct buf *)0); 637 bp = getblk(vp, bn, size, 0, 2 * hz); 638 } 639 } else 640 bp = getblk(vp, bn, size, 0, 0); 641 642 if( vp->v_type == VREG) 643 bp->b_blkno = (bn * biosize) / DEV_BSIZE; 644 645 return (bp); 646} 647 648/* 649 * Flush and invalidate all dirty buffers. If another process is already 650 * doing the flush, just wait for completion. 651 */ 652int 653nfs_vinvalbuf(vp, flags, cred, p, intrflg) 654 struct vnode *vp; 655 int flags; 656 struct ucred *cred; 657 struct proc *p; 658 int intrflg; 659{ 660 register struct nfsnode *np = VTONFS(vp); 661 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 662 int error = 0, slpflag, slptimeo; 663 664 if ((nmp->nm_flag & NFSMNT_INT) == 0) 665 intrflg = 0; 666 if (intrflg) { 667 slpflag = PCATCH; 668 slptimeo = 2 * hz; 669 } else { 670 slpflag = 0; 671 slptimeo = 0; 672 } 673 /* 674 * First wait for any other process doing a flush to complete. 675 */ 676 while (np->n_flag & NFLUSHINPROG) { 677 np->n_flag |= NFLUSHWANT; 678 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval", 679 slptimeo); 680 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) 681 return (EINTR); 682 } 683 684 /* 685 * Now, flush as required. 686 */ 687 np->n_flag |= NFLUSHINPROG; 688 error = vinvalbuf(vp, flags, cred, p, slpflag, 0); 689 while (error) { 690 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) { 691 np->n_flag &= ~NFLUSHINPROG; 692 if (np->n_flag & NFLUSHWANT) { 693 np->n_flag &= ~NFLUSHWANT; 694 wakeup((caddr_t)&np->n_flag); 695 } 696 return (EINTR); 697 } 698 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo); 699 } 700 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG); 701 if (np->n_flag & NFLUSHWANT) { 702 np->n_flag &= ~NFLUSHWANT; 703 wakeup((caddr_t)&np->n_flag); 704 } 705 return (0); 706} 707 708/* 709 * Initiate asynchronous I/O. Return an error if no nfsiods are available. 710 * This is mainly to avoid queueing async I/O requests when the nfsiods 711 * are all hung on a dead server. 712 */ 713int 714nfs_asyncio(bp, cred) 715 register struct buf *bp; 716 struct ucred *cred; 717{ 718 struct nfsmount *nmp; 719 int i; 720 int gotiod; 721 int slpflag = 0; 722 int slptimeo = 0; 723 int error; 724 725 if (nfs_numasync == 0) 726 return (EIO); 727 728 nmp = VFSTONFS(bp->b_vp->v_mount); 729again: 730 if (nmp->nm_flag & NFSMNT_INT) 731 slpflag = PCATCH; 732 gotiod = FALSE; 733 734 /* 735 * Find a free iod to process this request. 736 */ 737 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 738 if (nfs_iodwant[i]) { 739 /* 740 * Found one, so wake it up and tell it which 741 * mount to process. 742 */ 743 NFS_DPF(ASYNCIO, 744 ("nfs_asyncio: waking iod %d for mount %p\n", 745 i, nmp)); 746 nfs_iodwant[i] = (struct proc *)0; 747 nfs_iodmount[i] = nmp; 748 nmp->nm_bufqiods++; 749 wakeup((caddr_t)&nfs_iodwant[i]); 750 gotiod = TRUE; 751 break; 752 } 753 754 /* 755 * If none are free, we may already have an iod working on this mount 756 * point. If so, it will process our request. 757 */ 758 if (!gotiod) { 759 if (nmp->nm_bufqiods > 0) { 760 NFS_DPF(ASYNCIO, 761 ("nfs_asyncio: %d iods are already processing mount %p\n", 762 nmp->nm_bufqiods, nmp)); 763 gotiod = TRUE; 764 } 765 } 766 767 /* 768 * If we have an iod which can process the request, then queue 769 * the buffer. 770 */ 771 if (gotiod) { 772 /* 773 * Ensure that the queue never grows too large. 774 */ 775 while (nmp->nm_bufqlen >= 2*nfs_numasync) { 776 NFS_DPF(ASYNCIO, 777 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp)); 778 nmp->nm_bufqwant = TRUE; 779 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO, 780 "nfsaio", slptimeo); 781 if (error) { 782 if (nfs_sigintr(nmp, NULL, bp->b_proc)) 783 return (EINTR); 784 if (slpflag == PCATCH) { 785 slpflag = 0; 786 slptimeo = 2 * hz; 787 } 788 } 789 /* 790 * We might have lost our iod while sleeping, 791 * so check and loop if nescessary. 792 */ 793 if (nmp->nm_bufqiods == 0) { 794 NFS_DPF(ASYNCIO, 795 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 796 goto again; 797 } 798 } 799 800 if (bp->b_flags & B_READ) { 801 if (bp->b_rcred == NOCRED && cred != NOCRED) { 802 crhold(cred); 803 bp->b_rcred = cred; 804 } 805 } else { 806 bp->b_flags |= B_WRITEINPROG; 807 if (bp->b_wcred == NOCRED && cred != NOCRED) { 808 crhold(cred); 809 bp->b_wcred = cred; 810 } 811 } 812 813 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 814 nmp->nm_bufqlen++; 815 return (0); 816 } 817 818 /* 819 * All the iods are busy on other mounts, so return EIO to 820 * force the caller to process the i/o synchronously. 821 */ 822 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n")); 823 return (EIO); 824} 825 826/* 827 * Do an I/O operation to/from a cache block. This may be called 828 * synchronously or from an nfsiod. 829 */ 830int 831nfs_doio(bp, cr, p) 832 register struct buf *bp; 833 struct ucred *cr; 834 struct proc *p; 835{ 836 register struct uio *uiop; 837 register struct vnode *vp; 838 struct nfsnode *np; 839 struct nfsmount *nmp; 840 int error = 0, diff, len, iomode, must_commit = 0; 841 struct uio uio; 842 struct iovec io; 843 844 vp = bp->b_vp; 845 np = VTONFS(vp); 846 nmp = VFSTONFS(vp->v_mount); 847 uiop = &uio; 848 uiop->uio_iov = &io; 849 uiop->uio_iovcnt = 1; 850 uiop->uio_segflg = UIO_SYSSPACE; 851 uiop->uio_procp = p; 852 853 /* 854 * Historically, paging was done with physio, but no more. 855 */ 856 if (bp->b_flags & B_PHYS) { 857 /* 858 * ...though reading /dev/drum still gets us here. 859 */ 860 io.iov_len = uiop->uio_resid = bp->b_bcount; 861 /* mapping was done by vmapbuf() */ 862 io.iov_base = bp->b_data; 863 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 864 if (bp->b_flags & B_READ) { 865 uiop->uio_rw = UIO_READ; 866 nfsstats.read_physios++; 867 error = nfs_readrpc(vp, uiop, cr); 868 } else { 869 int com; 870 871 iomode = NFSV3WRITE_DATASYNC; 872 uiop->uio_rw = UIO_WRITE; 873 nfsstats.write_physios++; 874 error = nfs_writerpc(vp, uiop, cr, &iomode, &com); 875 } 876 if (error) { 877 bp->b_flags |= B_ERROR; 878 bp->b_error = error; 879 } 880 } else if (bp->b_flags & B_READ) { 881 io.iov_len = uiop->uio_resid = bp->b_bcount; 882 io.iov_base = bp->b_data; 883 uiop->uio_rw = UIO_READ; 884 switch (vp->v_type) { 885 case VREG: 886 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 887 nfsstats.read_bios++; 888 error = nfs_readrpc(vp, uiop, cr); 889 if (!error) { 890 bp->b_validoff = 0; 891 if (uiop->uio_resid) { 892 /* 893 * If len > 0, there is a hole in the file and 894 * no writes after the hole have been pushed to 895 * the server yet. 896 * Just zero fill the rest of the valid area. 897 */ 898 diff = bp->b_bcount - uiop->uio_resid; 899 len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE 900 + diff); 901 if (len > 0) { 902 len = min(len, uiop->uio_resid); 903 bzero((char *)bp->b_data + diff, len); 904 bp->b_validend = diff + len; 905 } else 906 bp->b_validend = diff; 907 } else 908 bp->b_validend = bp->b_bcount; 909 } 910 if (p && (vp->v_flag & VTEXT) && 911 (((nmp->nm_flag & NFSMNT_NQNFS) && 912 NQNFS_CKINVALID(vp, np, ND_READ) && 913 np->n_lrev != np->n_brev) || 914 (!(nmp->nm_flag & NFSMNT_NQNFS) && 915 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) { 916 uprintf("Process killed due to text file modification\n"); 917 psignal(p, SIGKILL); 918#ifdef __NetBSD__ 919 p->p_holdcnt++; 920#else 921 p->p_flag |= P_NOSWAP; 922#endif 923 } 924 break; 925 case VLNK: 926 uiop->uio_offset = (off_t)0; 927 nfsstats.readlink_bios++; 928 error = nfs_readlinkrpc(vp, uiop, cr); 929 break; 930 case VDIR: 931 nfsstats.readdir_bios++; 932 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 933 if (nmp->nm_flag & NFSMNT_RDIRPLUS) { 934 error = nfs_readdirplusrpc(vp, uiop, cr); 935 if (error == NFSERR_NOTSUPP) 936 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 937 } 938 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 939 error = nfs_readdirrpc(vp, uiop, cr); 940 break; 941 default: 942 printf("nfs_doio: type %x unexpected\n",vp->v_type); 943 break; 944 }; 945 if (error) { 946 bp->b_flags |= B_ERROR; 947 bp->b_error = error; 948 } 949 } else { 950 if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size) 951 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE); 952 953 if (bp->b_dirtyend > bp->b_dirtyoff) { 954 io.iov_len = uiop->uio_resid = bp->b_dirtyend 955 - bp->b_dirtyoff; 956 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE 957 + bp->b_dirtyoff; 958 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 959 uiop->uio_rw = UIO_WRITE; 960 nfsstats.write_bios++; 961 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 962 iomode = NFSV3WRITE_UNSTABLE; 963 else 964 iomode = NFSV3WRITE_FILESYNC; 965 bp->b_flags |= B_WRITEINPROG; 966 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit); 967 if (!error && iomode == NFSV3WRITE_UNSTABLE) { 968 bp->b_flags |= B_NEEDCOMMIT; 969 if (bp->b_dirtyoff == 0 970 && bp->b_dirtyend == bp->b_bufsize) 971 bp->b_flags |= B_CLUSTEROK; 972 } else 973 bp->b_flags &= ~B_NEEDCOMMIT; 974 bp->b_flags &= ~B_WRITEINPROG; 975 976 /* 977 * For an interrupted write, the buffer is still valid 978 * and the write hasn't been pushed to the server yet, 979 * so we can't set B_ERROR and report the interruption 980 * by setting B_EINTR. For the B_ASYNC case, B_EINTR 981 * is not relevant, so the rpc attempt is essentially 982 * a noop. For the case of a V3 write rpc not being 983 * committed to stable storage, the block is still 984 * dirty and requires either a commit rpc or another 985 * write rpc with iomode == NFSV3WRITE_FILESYNC before 986 * the block is reused. This is indicated by setting 987 * the B_DELWRI and B_NEEDCOMMIT flags. 988 */ 989 if (error == EINTR 990 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 991 bp->b_flags &= ~(B_INVAL|B_NOCACHE); 992 bp->b_flags |= B_DELWRI; 993 994 /* 995 * Since for the B_ASYNC case, nfs_bwrite() has reassigned the 996 * buffer to the clean list, we have to reassign it back to the 997 * dirty one. Ugh. 998 */ 999 if (bp->b_flags & B_ASYNC) 1000 reassignbuf(bp, vp); 1001 else 1002 bp->b_flags |= B_EINTR; 1003 } else { 1004 if (error) { 1005 bp->b_flags |= B_ERROR; 1006 bp->b_error = np->n_error = error; 1007 np->n_flag |= NWRITEERR; 1008 } 1009 bp->b_dirtyoff = bp->b_dirtyend = 0; 1010 } 1011 } else { 1012 bp->b_resid = 0; 1013 biodone(bp); 1014 return (0); 1015 } 1016 } 1017 bp->b_resid = uiop->uio_resid; 1018 if (must_commit) 1019 nfs_clearcommit(vp->v_mount); 1020 biodone(bp); 1021 return (error); 1022} 1023