vfs_cluster.c revision 76827
1/*- 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * Modifications/enhancements: 5 * Copyright (c) 1995 John S. Dyson. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94 36 * $FreeBSD: head/sys/kern/vfs_cluster.c 76827 2001-05-19 01:28:09Z alfred $ 37 */ 38 39#include "opt_debug_cluster.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/proc.h> 45#include <sys/bio.h> 46#include <sys/buf.h> 47#include <sys/vnode.h> 48#include <sys/malloc.h> 49#include <sys/mount.h> 50#include <sys/resourcevar.h> 51#include <sys/vmmeter.h> 52#include <vm/vm.h> 53#include <vm/vm_object.h> 54#include <vm/vm_page.h> 55#include <sys/sysctl.h> 56 57#if defined(CLUSTERDEBUG) 58#include <sys/sysctl.h> 59static int rcluster= 0; 60SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, ""); 61#endif 62 63static MALLOC_DEFINE(M_SEGMENT, "cluster_save buffer", "cluster_save buffer"); 64 65static struct cluster_save * 66 cluster_collectbufs __P((struct vnode *vp, struct buf *last_bp)); 67static struct buf * 68 cluster_rbuild __P((struct vnode *vp, u_quad_t filesize, daddr_t lbn, 69 daddr_t blkno, long size, int run, struct buf *fbp)); 70 71static int write_behind = 1; 72SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, ""); 73 74extern vm_page_t bogus_page; 75 76extern int cluster_pbuf_freecnt; 77 78/* 79 * Maximum number of blocks for read-ahead. 80 */ 81#define MAXRA 32 82 83/* 84 * This replaces bread. 85 */ 86int 87cluster_read(vp, filesize, lblkno, size, cred, totread, seqcount, bpp) 88 struct vnode *vp; 89 u_quad_t filesize; 90 daddr_t lblkno; 91 long size; 92 struct ucred *cred; 93 long totread; 94 int seqcount; 95 struct buf **bpp; 96{ 97 struct buf *bp, *rbp, *reqbp; 98 daddr_t blkno, origblkno; 99 int error, num_ra; 100 int i; 101 int maxra, racluster; 102 long origtotread; 103 104 error = 0; 105 106 /* 107 * Try to limit the amount of read-ahead by a few 108 * ad-hoc parameters. This needs work!!! 109 */ 110 racluster = vp->v_mount->mnt_iosize_max / size; 111 maxra = 2 * racluster + (totread / size); 112 if (maxra > MAXRA) 113 maxra = MAXRA; 114 if (maxra > nbuf/8) 115 maxra = nbuf/8; 116 117 /* 118 * get the requested block 119 */ 120 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0); 121 origblkno = lblkno; 122 origtotread = totread; 123 124 /* 125 * if it is in the cache, then check to see if the reads have been 126 * sequential. If they have, then try some read-ahead, otherwise 127 * back-off on prospective read-aheads. 128 */ 129 if (bp->b_flags & B_CACHE) { 130 if (!seqcount) { 131 return 0; 132 } else if ((bp->b_flags & B_RAM) == 0) { 133 return 0; 134 } else { 135 int s; 136 struct buf *tbp; 137 bp->b_flags &= ~B_RAM; 138 /* 139 * We do the spl here so that there is no window 140 * between the incore and the b_usecount increment 141 * below. We opt to keep the spl out of the loop 142 * for efficiency. 143 */ 144 s = splbio(); 145 for (i = 1; i < maxra; i++) { 146 147 if (!(tbp = incore(vp, lblkno+i))) { 148 break; 149 } 150 151 /* 152 * Set another read-ahead mark so we know 153 * to check again. 154 */ 155 if (((i % racluster) == (racluster - 1)) || 156 (i == (maxra - 1))) 157 tbp->b_flags |= B_RAM; 158 } 159 splx(s); 160 if (i >= maxra) { 161 return 0; 162 } 163 lblkno += i; 164 } 165 reqbp = bp = NULL; 166 } else { 167 off_t firstread = bp->b_offset; 168 169 KASSERT(bp->b_offset != NOOFFSET, 170 ("cluster_read: no buffer offset")); 171 if (firstread + totread > filesize) 172 totread = filesize - firstread; 173 if (totread > size) { 174 int nblks = 0; 175 int ncontigafter; 176 while (totread > 0) { 177 nblks++; 178 totread -= size; 179 } 180 if (nblks == 1) 181 goto single_block_read; 182 if (nblks > racluster) 183 nblks = racluster; 184 185 error = VOP_BMAP(vp, lblkno, NULL, 186 &blkno, &ncontigafter, NULL); 187 if (error) 188 goto single_block_read; 189 if (blkno == -1) 190 goto single_block_read; 191 if (ncontigafter == 0) 192 goto single_block_read; 193 if (ncontigafter + 1 < nblks) 194 nblks = ncontigafter + 1; 195 196 bp = cluster_rbuild(vp, filesize, lblkno, 197 blkno, size, nblks, bp); 198 lblkno += (bp->b_bufsize / size); 199 } else { 200single_block_read: 201 /* 202 * if it isn't in the cache, then get a chunk from 203 * disk if sequential, otherwise just get the block. 204 */ 205 bp->b_flags |= B_RAM; 206 bp->b_iocmd = BIO_READ; 207 lblkno += 1; 208 } 209 } 210 211 /* 212 * if we have been doing sequential I/O, then do some read-ahead 213 */ 214 rbp = NULL; 215 if (seqcount && (lblkno < (origblkno + seqcount))) { 216 /* 217 * we now build the read-ahead buffer if it is desirable. 218 */ 219 if (((u_quad_t)(lblkno + 1) * size) <= filesize && 220 !(error = VOP_BMAP(vp, lblkno, NULL, &blkno, &num_ra, NULL)) && 221 blkno != -1) { 222 int nblksread; 223 int ntoread = num_ra + 1; 224 nblksread = (origtotread + size - 1) / size; 225 if (seqcount < nblksread) 226 seqcount = nblksread; 227 if (seqcount < ntoread) 228 ntoread = seqcount; 229 if (num_ra) { 230 rbp = cluster_rbuild(vp, filesize, lblkno, 231 blkno, size, ntoread, NULL); 232 } else { 233 rbp = getblk(vp, lblkno, size, 0, 0); 234 rbp->b_flags |= B_ASYNC | B_RAM; 235 rbp->b_iocmd = BIO_READ; 236 rbp->b_blkno = blkno; 237 } 238 } 239 } 240 241 /* 242 * handle the synchronous read 243 */ 244 if (bp) { 245#if defined(CLUSTERDEBUG) 246 if (rcluster) 247 printf("S(%ld,%ld,%d) ", 248 (long)bp->b_lblkno, bp->b_bcount, seqcount); 249#endif 250 if ((bp->b_flags & B_CLUSTER) == 0) { 251 vfs_busy_pages(bp, 0); 252 } 253 bp->b_flags &= ~B_INVAL; 254 bp->b_ioflags &= ~BIO_ERROR; 255 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL) 256 BUF_KERNPROC(bp); 257 error = VOP_STRATEGY(vp, bp); 258 curproc->p_stats->p_ru.ru_inblock++; 259 } 260 261 /* 262 * and if we have read-aheads, do them too 263 */ 264 if (rbp) { 265 if (error) { 266 rbp->b_flags &= ~B_ASYNC; 267 brelse(rbp); 268 } else if (rbp->b_flags & B_CACHE) { 269 rbp->b_flags &= ~B_ASYNC; 270 bqrelse(rbp); 271 } else { 272#if defined(CLUSTERDEBUG) 273 if (rcluster) { 274 if (bp) 275 printf("A+(%ld,%ld,%ld,%d) ", 276 (long)rbp->b_lblkno, rbp->b_bcount, 277 (long)(rbp->b_lblkno - origblkno), 278 seqcount); 279 else 280 printf("A(%ld,%ld,%ld,%d) ", 281 (long)rbp->b_lblkno, rbp->b_bcount, 282 (long)(rbp->b_lblkno - origblkno), 283 seqcount); 284 } 285#endif 286 287 if ((rbp->b_flags & B_CLUSTER) == 0) { 288 vfs_busy_pages(rbp, 0); 289 } 290 rbp->b_flags &= ~B_INVAL; 291 rbp->b_ioflags &= ~BIO_ERROR; 292 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL) 293 BUF_KERNPROC(rbp); 294 (void) VOP_STRATEGY(vp, rbp); 295 curproc->p_stats->p_ru.ru_inblock++; 296 } 297 } 298 if (reqbp) 299 return (bufwait(reqbp)); 300 else 301 return (error); 302} 303 304/* 305 * If blocks are contiguous on disk, use this to provide clustered 306 * read ahead. We will read as many blocks as possible sequentially 307 * and then parcel them up into logical blocks in the buffer hash table. 308 */ 309static struct buf * 310cluster_rbuild(vp, filesize, lbn, blkno, size, run, fbp) 311 struct vnode *vp; 312 u_quad_t filesize; 313 daddr_t lbn; 314 daddr_t blkno; 315 long size; 316 int run; 317 struct buf *fbp; 318{ 319 struct buf *bp, *tbp; 320 daddr_t bn; 321 int i, inc, j; 322 323 KASSERT(size == vp->v_mount->mnt_stat.f_iosize, 324 ("cluster_rbuild: size %ld != filesize %ld\n", 325 size, vp->v_mount->mnt_stat.f_iosize)); 326 327 /* 328 * avoid a division 329 */ 330 while ((u_quad_t) size * (lbn + run) > filesize) { 331 --run; 332 } 333 334 if (fbp) { 335 tbp = fbp; 336 tbp->b_iocmd = BIO_READ; 337 } else { 338 tbp = getblk(vp, lbn, size, 0, 0); 339 if (tbp->b_flags & B_CACHE) 340 return tbp; 341 tbp->b_flags |= B_ASYNC | B_RAM; 342 tbp->b_iocmd = BIO_READ; 343 } 344 345 tbp->b_blkno = blkno; 346 if( (tbp->b_flags & B_MALLOC) || 347 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) ) 348 return tbp; 349 350 bp = trypbuf(&cluster_pbuf_freecnt); 351 if (bp == 0) 352 return tbp; 353 354 bp->b_data = (char *)((vm_offset_t)bp->b_data | 355 ((vm_offset_t)tbp->b_data & PAGE_MASK)); 356 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO; 357 bp->b_iocmd = BIO_READ; 358 bp->b_iodone = cluster_callback; 359 bp->b_blkno = blkno; 360 bp->b_lblkno = lbn; 361 bp->b_offset = tbp->b_offset; 362 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset")); 363 pbgetvp(vp, bp); 364 365 TAILQ_INIT(&bp->b_cluster.cluster_head); 366 367 bp->b_bcount = 0; 368 bp->b_bufsize = 0; 369 bp->b_npages = 0; 370 371 inc = btodb(size); 372 for (bn = blkno, i = 0; i < run; ++i, bn += inc) { 373 if (i != 0) { 374 if ((bp->b_npages * PAGE_SIZE) + 375 round_page(size) > vp->v_mount->mnt_iosize_max) 376 break; 377 378 if ((tbp = incore(vp, lbn + i)) != NULL) { 379 if (BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) 380 break; 381 BUF_UNLOCK(tbp); 382 383 for (j = 0; j < tbp->b_npages; j++) 384 if (tbp->b_pages[j]->valid) 385 break; 386 387 if (j != tbp->b_npages) 388 break; 389 390 if (tbp->b_bcount != size) 391 break; 392 } 393 394 tbp = getblk(vp, lbn + i, size, 0, 0); 395 396 /* 397 * If the buffer is already fully valid or locked 398 * (which could also mean that a background write is 399 * in progress), or the buffer is not backed by VMIO, 400 * stop. 401 */ 402 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) || 403 (tbp->b_flags & B_VMIO) == 0) { 404 bqrelse(tbp); 405 break; 406 } 407 408 for (j = 0;j < tbp->b_npages; j++) { 409 if (tbp->b_pages[j]->valid) 410 break; 411 } 412 413 if (j != tbp->b_npages) { 414 bqrelse(tbp); 415 break; 416 } 417 418 if ((fbp && (i == 1)) || (i == (run - 1))) 419 tbp->b_flags |= B_RAM; 420 tbp->b_flags |= B_ASYNC; 421 tbp->b_iocmd = BIO_READ; 422 if (tbp->b_blkno == tbp->b_lblkno) { 423 tbp->b_blkno = bn; 424 } else if (tbp->b_blkno != bn) { 425 brelse(tbp); 426 break; 427 } 428 } 429 /* 430 * XXX fbp from caller may not be B_ASYNC, but we are going 431 * to biodone() it in cluster_callback() anyway 432 */ 433 BUF_KERNPROC(tbp); 434 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head, 435 tbp, b_cluster.cluster_entry); 436 mtx_lock(&vm_mtx); 437 for (j = 0; j < tbp->b_npages; j += 1) { 438 vm_page_t m; 439 m = tbp->b_pages[j]; 440 vm_page_io_start(m); 441 vm_object_pip_add(m->object, 1); 442 if ((bp->b_npages == 0) || 443 (bp->b_pages[bp->b_npages-1] != m)) { 444 bp->b_pages[bp->b_npages] = m; 445 bp->b_npages++; 446 } 447 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) 448 tbp->b_pages[j] = bogus_page; 449 } 450 mtx_unlock(&vm_mtx); 451 bp->b_bcount += tbp->b_bcount; 452 bp->b_bufsize += tbp->b_bufsize; 453 } 454 455 mtx_lock(&vm_mtx); 456 for(j=0;j<bp->b_npages;j++) { 457 if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) == 458 VM_PAGE_BITS_ALL) 459 bp->b_pages[j] = bogus_page; 460 } 461 if (bp->b_bufsize > bp->b_kvasize) 462 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n", 463 bp->b_bufsize, bp->b_kvasize); 464 bp->b_kvasize = bp->b_bufsize; 465 466 pmap_qenter(trunc_page((vm_offset_t) bp->b_data), 467 (vm_page_t *)bp->b_pages, bp->b_npages); 468 mtx_unlock(&vm_mtx); 469 return (bp); 470} 471 472/* 473 * Cleanup after a clustered read or write. 474 * This is complicated by the fact that any of the buffers might have 475 * extra memory (if there were no empty buffer headers at allocbuf time) 476 * that we will need to shift around. 477 */ 478void 479cluster_callback(bp) 480 struct buf *bp; 481{ 482 struct buf *nbp, *tbp; 483 int error = 0; 484 485 /* 486 * Must propogate errors to all the components. 487 */ 488 if (bp->b_ioflags & BIO_ERROR) 489 error = bp->b_error; 490 491 mtx_lock(&vm_mtx); 492 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages); 493 mtx_unlock(&vm_mtx); 494 /* 495 * Move memory from the large cluster buffer into the component 496 * buffers and mark IO as done on these. 497 */ 498 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head); 499 tbp; tbp = nbp) { 500 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry); 501 if (error) { 502 tbp->b_ioflags |= BIO_ERROR; 503 tbp->b_error = error; 504 } else { 505 tbp->b_dirtyoff = tbp->b_dirtyend = 0; 506 tbp->b_flags &= ~B_INVAL; 507 tbp->b_ioflags &= ~BIO_ERROR; 508 } 509 bufdone(tbp); 510 } 511 relpbuf(bp, &cluster_pbuf_freecnt); 512} 513 514/* 515 * cluster_wbuild_wb: 516 * 517 * Implement modified write build for cluster. 518 * 519 * write_behind = 0 write behind disabled 520 * write_behind = 1 write behind normal (default) 521 * write_behind = 2 write behind backed-off 522 */ 523 524static __inline int 525cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len) 526{ 527 int r = 0; 528 529 switch(write_behind) { 530 case 2: 531 if (start_lbn < len) 532 break; 533 start_lbn -= len; 534 /* fall through */ 535 case 1: 536 r = cluster_wbuild(vp, size, start_lbn, len); 537 /* fall through */ 538 default: 539 /* fall through */ 540 break; 541 } 542 return(r); 543} 544 545/* 546 * Do clustered write for FFS. 547 * 548 * Three cases: 549 * 1. Write is not sequential (write asynchronously) 550 * Write is sequential: 551 * 2. beginning of cluster - begin cluster 552 * 3. middle of a cluster - add to cluster 553 * 4. end of a cluster - asynchronously write cluster 554 */ 555void 556cluster_write(bp, filesize, seqcount) 557 struct buf *bp; 558 u_quad_t filesize; 559 int seqcount; 560{ 561 struct vnode *vp; 562 daddr_t lbn; 563 int maxclen, cursize; 564 int lblocksize; 565 int async; 566 567 vp = bp->b_vp; 568 if (vp->v_type == VREG) { 569 async = vp->v_mount->mnt_flag & MNT_ASYNC; 570 lblocksize = vp->v_mount->mnt_stat.f_iosize; 571 } else { 572 async = 0; 573 lblocksize = bp->b_bufsize; 574 } 575 lbn = bp->b_lblkno; 576 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset")); 577 578 /* Initialize vnode to beginning of file. */ 579 if (lbn == 0) 580 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 581 582 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 || 583 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) { 584 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1; 585 if (vp->v_clen != 0) { 586 /* 587 * Next block is not sequential. 588 * 589 * If we are not writing at end of file, the process 590 * seeked to another point in the file since its last 591 * write, or we have reached our maximum cluster size, 592 * then push the previous cluster. Otherwise try 593 * reallocating to make it sequential. 594 * 595 * Change to algorithm: only push previous cluster if 596 * it was sequential from the point of view of the 597 * seqcount heuristic, otherwise leave the buffer 598 * intact so we can potentially optimize the I/O 599 * later on in the buf_daemon or update daemon 600 * flush. 601 */ 602 cursize = vp->v_lastw - vp->v_cstart + 1; 603 if (((u_quad_t) bp->b_offset + lblocksize) != filesize || 604 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) { 605 if (!async && seqcount > 0) { 606 cluster_wbuild_wb(vp, lblocksize, 607 vp->v_cstart, cursize); 608 } 609 } else { 610 struct buf **bpp, **endbp; 611 struct cluster_save *buflist; 612 613 buflist = cluster_collectbufs(vp, bp); 614 endbp = &buflist->bs_children 615 [buflist->bs_nchildren - 1]; 616 if (VOP_REALLOCBLKS(vp, buflist)) { 617 /* 618 * Failed, push the previous cluster 619 * if *really* writing sequentially 620 * in the logical file (seqcount > 1), 621 * otherwise delay it in the hopes that 622 * the low level disk driver can 623 * optimize the write ordering. 624 */ 625 for (bpp = buflist->bs_children; 626 bpp < endbp; bpp++) 627 brelse(*bpp); 628 free(buflist, M_SEGMENT); 629 if (seqcount > 1) { 630 cluster_wbuild_wb(vp, 631 lblocksize, vp->v_cstart, 632 cursize); 633 } 634 } else { 635 /* 636 * Succeeded, keep building cluster. 637 */ 638 for (bpp = buflist->bs_children; 639 bpp <= endbp; bpp++) 640 bdwrite(*bpp); 641 free(buflist, M_SEGMENT); 642 vp->v_lastw = lbn; 643 vp->v_lasta = bp->b_blkno; 644 return; 645 } 646 } 647 } 648 /* 649 * Consider beginning a cluster. If at end of file, make 650 * cluster as large as possible, otherwise find size of 651 * existing cluster. 652 */ 653 if ((vp->v_type == VREG) && 654 ((u_quad_t) bp->b_offset + lblocksize) != filesize && 655 (bp->b_blkno == bp->b_lblkno) && 656 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) || 657 bp->b_blkno == -1)) { 658 bawrite(bp); 659 vp->v_clen = 0; 660 vp->v_lasta = bp->b_blkno; 661 vp->v_cstart = lbn + 1; 662 vp->v_lastw = lbn; 663 return; 664 } 665 vp->v_clen = maxclen; 666 if (!async && maxclen == 0) { /* I/O not contiguous */ 667 vp->v_cstart = lbn + 1; 668 bawrite(bp); 669 } else { /* Wait for rest of cluster */ 670 vp->v_cstart = lbn; 671 bdwrite(bp); 672 } 673 } else if (lbn == vp->v_cstart + vp->v_clen) { 674 /* 675 * At end of cluster, write it out if seqcount tells us we 676 * are operating sequentially, otherwise let the buf or 677 * update daemon handle it. 678 */ 679 bdwrite(bp); 680 if (seqcount > 1) 681 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, vp->v_clen + 1); 682 vp->v_clen = 0; 683 vp->v_cstart = lbn + 1; 684 } else if (vm_page_count_severe()) { 685 /* 686 * We are low on memory, get it going NOW 687 */ 688 bawrite(bp); 689 } else { 690 /* 691 * In the middle of a cluster, so just delay the I/O for now. 692 */ 693 bdwrite(bp); 694 } 695 vp->v_lastw = lbn; 696 vp->v_lasta = bp->b_blkno; 697} 698 699 700/* 701 * This is an awful lot like cluster_rbuild...wish they could be combined. 702 * The last lbn argument is the current block on which I/O is being 703 * performed. Check to see that it doesn't fall in the middle of 704 * the current block (if last_bp == NULL). 705 */ 706int 707cluster_wbuild(vp, size, start_lbn, len) 708 struct vnode *vp; 709 long size; 710 daddr_t start_lbn; 711 int len; 712{ 713 struct buf *bp, *tbp; 714 int i, j, s; 715 int totalwritten = 0; 716 int dbsize = btodb(size); 717 718 while (len > 0) { 719 s = splbio(); 720 /* 721 * If the buffer is not delayed-write (i.e. dirty), or it 722 * is delayed-write but either locked or inval, it cannot 723 * partake in the clustered write. 724 */ 725 if (((tbp = gbincore(vp, start_lbn)) == NULL) || 726 ((tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI) || 727 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) { 728 ++start_lbn; 729 --len; 730 splx(s); 731 continue; 732 } 733 bremfree(tbp); 734 tbp->b_flags &= ~B_DONE; 735 splx(s); 736 737 /* 738 * Extra memory in the buffer, punt on this buffer. 739 * XXX we could handle this in most cases, but we would 740 * have to push the extra memory down to after our max 741 * possible cluster size and then potentially pull it back 742 * up if the cluster was terminated prematurely--too much 743 * hassle. 744 */ 745 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) != 746 (B_CLUSTEROK | B_VMIO)) || 747 (tbp->b_bcount != tbp->b_bufsize) || 748 (tbp->b_bcount != size) || 749 (len == 1) || 750 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) { 751 totalwritten += tbp->b_bufsize; 752 bawrite(tbp); 753 ++start_lbn; 754 --len; 755 continue; 756 } 757 758 /* 759 * We got a pbuf to make the cluster in. 760 * so initialise it. 761 */ 762 TAILQ_INIT(&bp->b_cluster.cluster_head); 763 bp->b_bcount = 0; 764 bp->b_magic = tbp->b_magic; 765 bp->b_op = tbp->b_op; 766 bp->b_bufsize = 0; 767 bp->b_npages = 0; 768 if (tbp->b_wcred != NOCRED) { 769 bp->b_wcred = tbp->b_wcred; 770 crhold(bp->b_wcred); 771 } 772 773 bp->b_blkno = tbp->b_blkno; 774 bp->b_lblkno = tbp->b_lblkno; 775 bp->b_offset = tbp->b_offset; 776 bp->b_data = (char *)((vm_offset_t)bp->b_data | 777 ((vm_offset_t)tbp->b_data & PAGE_MASK)); 778 bp->b_flags |= B_CLUSTER | 779 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT)); 780 bp->b_iodone = cluster_callback; 781 pbgetvp(vp, bp); 782 /* 783 * From this location in the file, scan forward to see 784 * if there are buffers with adjacent data that need to 785 * be written as well. 786 */ 787 for (i = 0; i < len; ++i, ++start_lbn) { 788 if (i != 0) { /* If not the first buffer */ 789 s = splbio(); 790 /* 791 * If the adjacent data is not even in core it 792 * can't need to be written. 793 */ 794 if ((tbp = gbincore(vp, start_lbn)) == NULL) { 795 splx(s); 796 break; 797 } 798 799 /* 800 * If it IS in core, but has different 801 * characteristics, or is locked (which 802 * means it could be undergoing a background 803 * I/O or be in a weird state), then don't 804 * cluster with it. 805 */ 806 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK | 807 B_INVAL | B_DELWRI | B_NEEDCOMMIT)) 808 != (B_DELWRI | B_CLUSTEROK | 809 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) || 810 (tbp->b_flags & B_LOCKED) || 811 tbp->b_wcred != bp->b_wcred || 812 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) { 813 splx(s); 814 break; 815 } 816 817 /* 818 * Check that the combined cluster 819 * would make sense with regard to pages 820 * and would not be too large 821 */ 822 if ((tbp->b_bcount != size) || 823 ((bp->b_blkno + (dbsize * i)) != 824 tbp->b_blkno) || 825 ((tbp->b_npages + bp->b_npages) > 826 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) { 827 BUF_UNLOCK(tbp); 828 splx(s); 829 break; 830 } 831 /* 832 * Ok, it's passed all the tests, 833 * so remove it from the free list 834 * and mark it busy. We will use it. 835 */ 836 bremfree(tbp); 837 tbp->b_flags &= ~B_DONE; 838 splx(s); 839 } /* end of code for non-first buffers only */ 840 /* check for latent dependencies to be handled */ 841 if ((LIST_FIRST(&tbp->b_dep)) != NULL) 842 buf_start(tbp); 843 /* 844 * If the IO is via the VM then we do some 845 * special VM hackery. (yuck) 846 */ 847 if (tbp->b_flags & B_VMIO) { 848 vm_page_t m; 849 850 if (i != 0) { /* if not first buffer */ 851 for (j = 0; j < tbp->b_npages; j += 1) { 852 m = tbp->b_pages[j]; 853 if (m->flags & PG_BUSY) { 854 bqrelse(tbp); 855 goto finishcluster; 856 } 857 } 858 } 859 860 mtx_lock(&vm_mtx); 861 for (j = 0; j < tbp->b_npages; j += 1) { 862 m = tbp->b_pages[j]; 863 vm_page_io_start(m); 864 vm_object_pip_add(m->object, 1); 865 if ((bp->b_npages == 0) || 866 (bp->b_pages[bp->b_npages - 1] != m)) { 867 bp->b_pages[bp->b_npages] = m; 868 bp->b_npages++; 869 } 870 } 871 mtx_unlock(&vm_mtx); 872 } 873 bp->b_bcount += size; 874 bp->b_bufsize += size; 875 876 s = splbio(); 877 bundirty(tbp); 878 tbp->b_flags &= ~B_DONE; 879 tbp->b_ioflags &= ~BIO_ERROR; 880 tbp->b_flags |= B_ASYNC; 881 tbp->b_iocmd = BIO_WRITE; 882 reassignbuf(tbp, tbp->b_vp); /* put on clean list */ 883 ++tbp->b_vp->v_numoutput; 884 splx(s); 885 BUF_KERNPROC(tbp); 886 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head, 887 tbp, b_cluster.cluster_entry); 888 } 889 finishcluster: 890 mtx_lock(&vm_mtx); 891 pmap_qenter(trunc_page((vm_offset_t) bp->b_data), 892 (vm_page_t *) bp->b_pages, bp->b_npages); 893 mtx_unlock(&vm_mtx); 894 if (bp->b_bufsize > bp->b_kvasize) 895 panic( 896 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n", 897 bp->b_bufsize, bp->b_kvasize); 898 bp->b_kvasize = bp->b_bufsize; 899 totalwritten += bp->b_bufsize; 900 bp->b_dirtyoff = 0; 901 bp->b_dirtyend = bp->b_bufsize; 902 bawrite(bp); 903 904 len -= i; 905 } 906 return totalwritten; 907} 908 909/* 910 * Collect together all the buffers in a cluster. 911 * Plus add one additional buffer. 912 */ 913static struct cluster_save * 914cluster_collectbufs(vp, last_bp) 915 struct vnode *vp; 916 struct buf *last_bp; 917{ 918 struct cluster_save *buflist; 919 struct buf *bp; 920 daddr_t lbn; 921 int i, len; 922 923 len = vp->v_lastw - vp->v_cstart + 1; 924 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist), 925 M_SEGMENT, M_WAITOK); 926 buflist->bs_nchildren = 0; 927 buflist->bs_children = (struct buf **) (buflist + 1); 928 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) { 929 (void) bread(vp, lbn, last_bp->b_bcount, NOCRED, &bp); 930 buflist->bs_children[i] = bp; 931 if (bp->b_blkno == bp->b_lblkno) 932 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, 933 NULL, NULL); 934 } 935 buflist->bs_children[i] = bp = last_bp; 936 if (bp->b_blkno == bp->b_lblkno) 937 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, 938 NULL, NULL); 939 buflist->bs_nchildren = i + 1; 940 return (buflist); 941} 942