vfs_cluster.c revision 38135
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 * $Id: vfs_cluster.c,v 1.66 1998/07/29 17:38:14 bde Exp $ 37 */ 38 39#include "opt_debug_cluster.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/proc.h> 44#include <sys/buf.h> 45#include <sys/vnode.h> 46#include <sys/mount.h> 47#include <sys/resourcevar.h> 48#include <vm/vm.h> 49#include <vm/vm_prot.h> 50#include <vm/vm_object.h> 51#include <vm/vm_page.h> 52 53#if defined(CLUSTERDEBUG) 54#include <sys/sysctl.h> 55#include <sys/kernel.h> 56static int rcluster= 0; 57SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, ""); 58#endif 59 60#ifdef notyet_block_reallocation_enabled 61static struct cluster_save * 62 cluster_collectbufs __P((struct vnode *vp, struct buf *last_bp)); 63#endif 64static struct buf * 65 cluster_rbuild __P((struct vnode *vp, u_quad_t filesize, daddr_t lbn, 66 daddr_t blkno, long size, int run, struct buf *fbp)); 67 68extern vm_page_t bogus_page; 69 70/* 71 * Maximum number of blocks for read-ahead. 72 */ 73#define MAXRA 32 74 75/* 76 * This replaces bread. 77 */ 78int 79cluster_read(vp, filesize, lblkno, size, cred, totread, seqcount, bpp) 80 struct vnode *vp; 81 u_quad_t filesize; 82 daddr_t lblkno; 83 long size; 84 struct ucred *cred; 85 long totread; 86 int seqcount; 87 struct buf **bpp; 88{ 89 struct buf *bp, *rbp, *reqbp; 90 daddr_t blkno, origblkno; 91 int error, num_ra; 92 int i; 93 int maxra, racluster; 94 long origtotread; 95 96 error = 0; 97 if (vp->v_maxio == 0) 98 vp->v_maxio = DFLTPHYS; 99 100 /* 101 * Try to limit the amount of read-ahead by a few 102 * ad-hoc parameters. This needs work!!! 103 */ 104 racluster = vp->v_maxio/size; 105 maxra = 2 * racluster + (totread / size); 106 if (maxra > MAXRA) 107 maxra = MAXRA; 108 if (maxra > nbuf/8) 109 maxra = nbuf/8; 110 111 /* 112 * get the requested block 113 */ 114 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0); 115 origblkno = lblkno; 116 origtotread = totread; 117 118 /* 119 * if it is in the cache, then check to see if the reads have been 120 * sequential. If they have, then try some read-ahead, otherwise 121 * back-off on prospective read-aheads. 122 */ 123 if (bp->b_flags & B_CACHE) { 124 if (!seqcount) { 125 return 0; 126 } else if ((bp->b_flags & B_RAM) == 0) { 127 return 0; 128 } else { 129 int s; 130 struct buf *tbp; 131 bp->b_flags &= ~B_RAM; 132 /* 133 * We do the spl here so that there is no window 134 * between the incore and the b_usecount increment 135 * below. We opt to keep the spl out of the loop 136 * for efficiency. 137 */ 138 s = splbio(); 139 for(i=1;i<maxra;i++) { 140 141 if (!(tbp = incore(vp, lblkno+i))) { 142 break; 143 } 144 145 /* 146 * Set another read-ahead mark so we know to check 147 * again. 148 */ 149 if (((i % racluster) == (racluster - 1)) || 150 (i == (maxra - 1))) 151 tbp->b_flags |= B_RAM; 152 153 if ((tbp->b_usecount < 1) && 154 ((tbp->b_flags & B_BUSY) == 0) && 155 (tbp->b_qindex == QUEUE_LRU)) { 156 TAILQ_REMOVE(&bufqueues[QUEUE_LRU], tbp, b_freelist); 157 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], tbp, b_freelist); 158 } 159 } 160 splx(s); 161 if (i >= maxra) { 162 return 0; 163 } 164 lblkno += i; 165 } 166 reqbp = bp = NULL; 167 } else { 168 off_t firstread; 169 firstread = bp->b_offset; 170#ifdef DIAGNOSTIC 171 if (bp->b_offset == NOOFFSET) 172 panic("cluster_read: no buffer offset"); 173#endif 174 if (firstread + totread > filesize) 175 totread = filesize - firstread; 176 if (totread > size) { 177 int nblks = 0; 178 int ncontigafter; 179 while (totread > 0) { 180 nblks++; 181 totread -= size; 182 } 183 if (nblks == 1) 184 goto single_block_read; 185 if (nblks > racluster) 186 nblks = racluster; 187 188 error = VOP_BMAP(vp, lblkno, NULL, 189 &blkno, &ncontigafter, NULL); 190 if (error) 191 goto single_block_read; 192 if (blkno == -1) 193 goto single_block_read; 194 if (ncontigafter == 0) 195 goto single_block_read; 196 if (ncontigafter + 1 < nblks) 197 nblks = ncontigafter + 1; 198 199 bp = cluster_rbuild(vp, filesize, lblkno, 200 blkno, size, nblks, bp); 201 lblkno += (bp->b_bufsize / size); 202 } else { 203single_block_read: 204 /* 205 * if it isn't in the cache, then get a chunk from 206 * disk if sequential, otherwise just get the block. 207 */ 208 bp->b_flags |= B_READ | B_RAM; 209 lblkno += 1; 210 } 211 } 212 213 /* 214 * if we have been doing sequential I/O, then do some read-ahead 215 */ 216 rbp = NULL; 217 if (seqcount && (lblkno < (origblkno + seqcount))) { 218 /* 219 * we now build the read-ahead buffer if it is desirable. 220 */ 221 if (((u_quad_t)(lblkno + 1) * size) <= filesize && 222 !(error = VOP_BMAP(vp, lblkno, NULL, &blkno, &num_ra, NULL)) && 223 blkno != -1) { 224 int nblksread; 225 int ntoread = num_ra + 1; 226 nblksread = (origtotread + size - 1) / size; 227 if (seqcount < nblksread) 228 seqcount = nblksread; 229 if (seqcount < ntoread) 230 ntoread = seqcount; 231 if (num_ra) { 232 rbp = cluster_rbuild(vp, filesize, lblkno, 233 blkno, size, ntoread, NULL); 234 } else { 235 rbp = getblk(vp, lblkno, size, 0, 0); 236 rbp->b_flags |= B_READ | B_ASYNC | B_RAM; 237 rbp->b_blkno = blkno; 238 } 239 } 240 } 241 242 /* 243 * handle the synchronous read 244 */ 245 if (bp) { 246#if defined(CLUSTERDEBUG) 247 if (rcluster) 248 printf("S(%ld,%ld,%d) ", 249 (long)bp->b_lblkno, bp->b_bcount, seqcount); 250#endif 251 if ((bp->b_flags & B_CLUSTER) == 0) 252 vfs_busy_pages(bp, 0); 253 error = VOP_STRATEGY(vp, bp); 254 curproc->p_stats->p_ru.ru_inblock++; 255 } 256 257 /* 258 * and if we have read-aheads, do them too 259 */ 260 if (rbp) { 261 if (error) { 262 rbp->b_flags &= ~(B_ASYNC | B_READ); 263 brelse(rbp); 264 } else if (rbp->b_flags & B_CACHE) { 265 rbp->b_flags &= ~(B_ASYNC | B_READ); 266 bqrelse(rbp); 267 } else { 268#if defined(CLUSTERDEBUG) 269 if (rcluster) { 270 if (bp) 271 printf("A+(%ld,%ld,%ld,%d) ", 272 (long)rbp->b_lblkno, rbp->b_bcount, 273 (long)(rbp->b_lblkno - origblkno), 274 seqcount); 275 else 276 printf("A(%ld,%ld,%ld,%d) ", 277 (long)rbp->b_lblkno, rbp->b_bcount, 278 (long)(rbp->b_lblkno - origblkno), 279 seqcount); 280 } 281#endif 282 283 if ((rbp->b_flags & B_CLUSTER) == 0) 284 vfs_busy_pages(rbp, 0); 285 (void) VOP_STRATEGY(vp, rbp); 286 curproc->p_stats->p_ru.ru_inblock++; 287 } 288 } 289 if (reqbp) 290 return (biowait(reqbp)); 291 else 292 return (error); 293} 294 295/* 296 * If blocks are contiguous on disk, use this to provide clustered 297 * read ahead. We will read as many blocks as possible sequentially 298 * and then parcel them up into logical blocks in the buffer hash table. 299 */ 300static struct buf * 301cluster_rbuild(vp, filesize, lbn, blkno, size, run, fbp) 302 struct vnode *vp; 303 u_quad_t filesize; 304 daddr_t lbn; 305 daddr_t blkno; 306 long size; 307 int run; 308 struct buf *fbp; 309{ 310 struct buf *bp, *tbp; 311 daddr_t bn; 312 int i, inc, j, s; 313 314#ifdef DIAGNOSTIC 315 if (size != vp->v_mount->mnt_stat.f_iosize) 316 panic("cluster_rbuild: size %ld != filesize %ld\n", 317 size, vp->v_mount->mnt_stat.f_iosize); 318#endif 319 /* 320 * avoid a division 321 */ 322 while ((u_quad_t) size * (lbn + run) > filesize) { 323 --run; 324 } 325 326 if (fbp) { 327 tbp = fbp; 328 tbp->b_flags |= B_READ; 329 } else { 330 tbp = getblk(vp, lbn, size, 0, 0); 331 if (tbp->b_flags & B_CACHE) 332 return tbp; 333 tbp->b_flags |= B_ASYNC | B_READ | B_RAM; 334 } 335 336 tbp->b_blkno = blkno; 337 if( (tbp->b_flags & B_MALLOC) || 338 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) ) 339 return tbp; 340 341 bp = trypbuf(); 342 if (bp == 0) 343 return tbp; 344 345 bp->b_data = (char *)((vm_offset_t)bp->b_data | 346 ((vm_offset_t)tbp->b_data & PAGE_MASK)); 347 bp->b_flags = B_ASYNC | B_READ | B_CALL | B_BUSY | B_CLUSTER | B_VMIO; 348 bp->b_iodone = cluster_callback; 349 bp->b_blkno = blkno; 350 bp->b_lblkno = lbn; 351 bp->b_offset = tbp->b_offset; 352#ifdef DIAGNOSTIC 353 if (bp->b_offset == NOOFFSET) 354 panic("cluster_rbuild: no buffer offset"); 355#endif 356 pbgetvp(vp, bp); 357 358 TAILQ_INIT(&bp->b_cluster.cluster_head); 359 360 bp->b_bcount = 0; 361 bp->b_bufsize = 0; 362 bp->b_npages = 0; 363 364 if (vp->v_maxio == 0) 365 vp->v_maxio = DFLTPHYS; 366 inc = btodb(size); 367 for (bn = blkno, i = 0; i < run; ++i, bn += inc) { 368 if (i != 0) { 369 if ((bp->b_npages * PAGE_SIZE) + 370 round_page(size) > vp->v_maxio) 371 break; 372 373 if (tbp = incore(vp, lbn + i)) { 374 if (tbp->b_flags & B_BUSY) 375 break; 376 377 for (j = 0; j < tbp->b_npages; j++) 378 if (tbp->b_pages[j]->valid) 379 break; 380 381 if (j != tbp->b_npages) 382 break; 383 384 if (tbp->b_bcount != size) 385 break; 386 } 387 388 tbp = getblk(vp, lbn + i, size, 0, 0); 389 390 if ((tbp->b_flags & B_CACHE) || 391 (tbp->b_flags & B_VMIO) == 0) { 392 bqrelse(tbp); 393 break; 394 } 395 396 for (j = 0;j < tbp->b_npages; j++) 397 if (tbp->b_pages[j]->valid) 398 break; 399 400 if (j != tbp->b_npages) { 401 bqrelse(tbp); 402 break; 403 } 404 405 if ((fbp && (i == 1)) || (i == (run - 1))) 406 tbp->b_flags |= B_RAM; 407 tbp->b_flags |= B_READ | B_ASYNC; 408 if (tbp->b_blkno == tbp->b_lblkno) { 409 tbp->b_blkno = bn; 410 } else if (tbp->b_blkno != bn) { 411 brelse(tbp); 412 break; 413 } 414 } 415 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head, 416 tbp, b_cluster.cluster_entry); 417 for (j = 0; j < tbp->b_npages; j += 1) { 418 vm_page_t m; 419 m = tbp->b_pages[j]; 420 s = splvm(); 421 ++m->busy; 422 ++m->object->paging_in_progress; 423 splx(s); 424 if ((bp->b_npages == 0) || 425 (bp->b_pages[bp->b_npages-1] != m)) { 426 bp->b_pages[bp->b_npages] = m; 427 bp->b_npages++; 428 } 429 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) 430 tbp->b_pages[j] = bogus_page; 431 } 432 bp->b_bcount += tbp->b_bcount; 433 bp->b_bufsize += tbp->b_bufsize; 434 } 435 436 for(j=0;j<bp->b_npages;j++) { 437 if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) == 438 VM_PAGE_BITS_ALL) 439 bp->b_pages[j] = bogus_page; 440 } 441 if (bp->b_bufsize > bp->b_kvasize) 442 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n", 443 bp->b_bufsize, bp->b_kvasize); 444 bp->b_kvasize = bp->b_bufsize; 445 446 pmap_qenter(trunc_page((vm_offset_t) bp->b_data), 447 (vm_page_t *)bp->b_pages, bp->b_npages); 448 return (bp); 449} 450 451/* 452 * Cleanup after a clustered read or write. 453 * This is complicated by the fact that any of the buffers might have 454 * extra memory (if there were no empty buffer headers at allocbuf time) 455 * that we will need to shift around. 456 */ 457void 458cluster_callback(bp) 459 struct buf *bp; 460{ 461 struct buf *nbp, *tbp; 462 int error = 0; 463 464 /* 465 * Must propogate errors to all the components. 466 */ 467 if (bp->b_flags & B_ERROR) 468 error = bp->b_error; 469 470 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages); 471 /* 472 * Move memory from the large cluster buffer into the component 473 * buffers and mark IO as done on these. 474 */ 475 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head); 476 tbp; tbp = nbp) { 477 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry); 478 if (error) { 479 tbp->b_flags |= B_ERROR; 480 tbp->b_error = error; 481 } else 482 tbp->b_dirtyoff = tbp->b_dirtyend = 0; 483 biodone(tbp); 484 } 485 relpbuf(bp); 486} 487 488/* 489 * Do clustered write for FFS. 490 * 491 * Three cases: 492 * 1. Write is not sequential (write asynchronously) 493 * Write is sequential: 494 * 2. beginning of cluster - begin cluster 495 * 3. middle of a cluster - add to cluster 496 * 4. end of a cluster - asynchronously write cluster 497 */ 498void 499cluster_write(bp, filesize) 500 struct buf *bp; 501 u_quad_t filesize; 502{ 503 struct vnode *vp; 504 daddr_t lbn; 505 int maxclen, cursize; 506 int lblocksize; 507 int async; 508 509 vp = bp->b_vp; 510 if (vp->v_maxio == 0) 511 vp->v_maxio = DFLTPHYS; 512 if (vp->v_type == VREG) { 513 async = vp->v_mount->mnt_flag & MNT_ASYNC; 514 lblocksize = vp->v_mount->mnt_stat.f_iosize; 515 } else { 516 async = 0; 517 lblocksize = bp->b_bufsize; 518 } 519 lbn = bp->b_lblkno; 520 521#ifdef DIAGNOSTIC 522 if (bp->b_offset == NOOFFSET) 523 panic("cluster_write: no buffer offset"); 524#endif 525 526 /* Initialize vnode to beginning of file. */ 527 if (lbn == 0) 528 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 529 530 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 || 531 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) { 532 maxclen = vp->v_maxio / lblocksize - 1; 533 if (vp->v_clen != 0) { 534 /* 535 * Next block is not sequential. 536 * 537 * If we are not writing at end of file, the process 538 * seeked to another point in the file since its last 539 * write, or we have reached our maximum cluster size, 540 * then push the previous cluster. Otherwise try 541 * reallocating to make it sequential. 542 */ 543 cursize = vp->v_lastw - vp->v_cstart + 1; 544#ifndef notyet_block_reallocation_enabled 545 if (((u_quad_t) bp->b_offset + lblocksize) != filesize || 546 lbn != vp->v_lastw + 1 || 547 vp->v_clen <= cursize) { 548 if (!async) 549 cluster_wbuild(vp, lblocksize, 550 vp->v_cstart, cursize); 551 } 552#else 553 if ((lbn + 1) * lblocksize != filesize || 554 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) { 555 if (!async) 556 cluster_wbuild(vp, lblocksize, 557 vp->v_cstart, cursize); 558 } else { 559 struct buf **bpp, **endbp; 560 struct cluster_save *buflist; 561 562 buflist = cluster_collectbufs(vp, bp); 563 endbp = &buflist->bs_children 564 [buflist->bs_nchildren - 1]; 565 if (VOP_REALLOCBLKS(vp, buflist)) { 566 /* 567 * Failed, push the previous cluster. 568 */ 569 for (bpp = buflist->bs_children; 570 bpp < endbp; bpp++) 571 brelse(*bpp); 572 free(buflist, M_SEGMENT); 573 cluster_wbuild(vp, lblocksize, 574 vp->v_cstart, cursize); 575 } else { 576 /* 577 * Succeeded, keep building cluster. 578 */ 579 for (bpp = buflist->bs_children; 580 bpp <= endbp; bpp++) 581 bdwrite(*bpp); 582 free(buflist, M_SEGMENT); 583 vp->v_lastw = lbn; 584 vp->v_lasta = bp->b_blkno; 585 return; 586 } 587 } 588#endif /* notyet_block_reallocation_enabled */ 589 } 590 /* 591 * Consider beginning a cluster. If at end of file, make 592 * cluster as large as possible, otherwise find size of 593 * existing cluster. 594 */ 595 if ((vp->v_type == VREG) && 596 ((u_quad_t) bp->b_offset + lblocksize) != filesize && 597 (bp->b_blkno == bp->b_lblkno) && 598 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) || 599 bp->b_blkno == -1)) { 600 bawrite(bp); 601 vp->v_clen = 0; 602 vp->v_lasta = bp->b_blkno; 603 vp->v_cstart = lbn + 1; 604 vp->v_lastw = lbn; 605 return; 606 } 607 vp->v_clen = maxclen; 608 if (!async && maxclen == 0) { /* I/O not contiguous */ 609 vp->v_cstart = lbn + 1; 610 bawrite(bp); 611 } else { /* Wait for rest of cluster */ 612 vp->v_cstart = lbn; 613 bdwrite(bp); 614 } 615 } else if (lbn == vp->v_cstart + vp->v_clen) { 616 /* 617 * At end of cluster, write it out. 618 */ 619 bdwrite(bp); 620 cluster_wbuild(vp, lblocksize, vp->v_cstart, vp->v_clen + 1); 621 vp->v_clen = 0; 622 vp->v_cstart = lbn + 1; 623 } else 624 /* 625 * In the middle of a cluster, so just delay the I/O for now. 626 */ 627 bdwrite(bp); 628 vp->v_lastw = lbn; 629 vp->v_lasta = bp->b_blkno; 630} 631 632 633/* 634 * This is an awful lot like cluster_rbuild...wish they could be combined. 635 * The last lbn argument is the current block on which I/O is being 636 * performed. Check to see that it doesn't fall in the middle of 637 * the current block (if last_bp == NULL). 638 */ 639int 640cluster_wbuild(vp, size, start_lbn, len) 641 struct vnode *vp; 642 long size; 643 daddr_t start_lbn; 644 int len; 645{ 646 struct buf *bp, *tbp; 647 int i, j, s; 648 int totalwritten = 0; 649 int dbsize = btodb(size); 650 651 if (vp->v_maxio == 0) 652 vp->v_maxio = DFLTPHYS; 653 while (len > 0) { 654 s = splbio(); 655 if (((tbp = gbincore(vp, start_lbn)) == NULL) || 656 ((tbp->b_flags & (B_INVAL|B_BUSY|B_DELWRI)) != B_DELWRI)) { 657 ++start_lbn; 658 --len; 659 splx(s); 660 continue; 661 } 662 bremfree(tbp); 663 tbp->b_flags |= B_BUSY; 664 tbp->b_flags &= ~B_DONE; 665 splx(s); 666 667 /* 668 * Extra memory in the buffer, punt on this buffer. XXX we could 669 * handle this in most cases, but we would have to push the extra 670 * memory down to after our max possible cluster size and then 671 * potentially pull it back up if the cluster was terminated 672 * prematurely--too much hassle. 673 */ 674 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) || 675 (tbp->b_bcount != tbp->b_bufsize) || 676 (tbp->b_bcount != size) || 677 (len == 1) || 678 ((bp = trypbuf()) == NULL)) { 679 totalwritten += tbp->b_bufsize; 680 bawrite(tbp); 681 ++start_lbn; 682 --len; 683 continue; 684 } 685 686 /* 687 * We got a pbuf to make the cluster in. 688 * so initialise it. 689 */ 690 TAILQ_INIT(&bp->b_cluster.cluster_head); 691 bp->b_bcount = 0; 692 bp->b_bufsize = 0; 693 bp->b_npages = 0; 694 if (tbp->b_wcred != NOCRED) { 695 bp->b_wcred = tbp->b_wcred; 696 crhold(bp->b_wcred); 697 } 698 699 bp->b_blkno = tbp->b_blkno; 700 bp->b_lblkno = tbp->b_lblkno; 701 bp->b_offset = tbp->b_offset; 702 bp->b_data = (char *)((vm_offset_t)bp->b_data | 703 ((vm_offset_t)tbp->b_data & PAGE_MASK)); 704 bp->b_flags |= B_CALL | B_BUSY | B_CLUSTER | 705 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT)); 706 bp->b_iodone = cluster_callback; 707 pbgetvp(vp, bp); 708 /* 709 * From this location in the file, scan forward to see 710 * if there are buffers with adjacent data that need to 711 * be written as well. 712 */ 713 for (i = 0; i < len; ++i, ++start_lbn) { 714 if (i != 0) { /* If not the first buffer */ 715 s = splbio(); 716 /* 717 * If the adjacent data is not even in core it 718 * can't need to be written. 719 */ 720 if ((tbp = gbincore(vp, start_lbn)) == NULL) { 721 splx(s); 722 break; 723 } 724 725 /* 726 * If it IS in core, but has different 727 * characteristics, don't cluster with it. 728 */ 729 if ((tbp->b_flags & 730 (B_VMIO | B_CLUSTEROK | B_INVAL | B_BUSY | 731 B_DELWRI | B_NEEDCOMMIT)) 732 != (B_DELWRI | B_CLUSTEROK | 733 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT)))) { 734 splx(s); 735 break; 736 } 737 738 if (tbp->b_wcred != bp->b_wcred) { 739 splx(s); 740 break; 741 } 742 743 /* 744 * Check that the combined cluster 745 * would make sense with regard to pages 746 * and would not be too large 747 */ 748 if ((tbp->b_bcount != size) || 749 ((bp->b_blkno + (dbsize * i)) != 750 tbp->b_blkno) || 751 ((tbp->b_npages + bp->b_npages) > 752 (vp->v_maxio / PAGE_SIZE))) { 753 splx(s); 754 break; 755 } 756 /* 757 * Ok, it's passed all the tests, 758 * so remove it from the free list 759 * and mark it busy. We will use it. 760 */ 761 bremfree(tbp); 762 tbp->b_flags |= B_BUSY; 763 tbp->b_flags &= ~B_DONE; 764 splx(s); 765 } /* end of code for non-first buffers only */ 766 /* check for latent dependencies to be handled */ 767 if ((LIST_FIRST(&tbp->b_dep)) != NULL && 768 bioops.io_start) 769 (*bioops.io_start)(tbp); 770 /* 771 * If the IO is via the VM then we do some 772 * special VM hackery. (yuck) 773 */ 774 if (tbp->b_flags & B_VMIO) { 775 vm_page_t m; 776 777 if (i != 0) { /* if not first buffer */ 778 for (j = 0; j < tbp->b_npages; j += 1) { 779 m = tbp->b_pages[j]; 780 if (m->flags & PG_BUSY) 781 goto finishcluster; 782 } 783 } 784 785 for (j = 0; j < tbp->b_npages; j += 1) { 786 m = tbp->b_pages[j]; 787 s = splvm(); 788 ++m->busy; 789 ++m->object->paging_in_progress; 790 splx(s); 791 if ((bp->b_npages == 0) || 792 (bp->b_pages[bp->b_npages - 1] != m)) { 793 bp->b_pages[bp->b_npages] = m; 794 bp->b_npages++; 795 } 796 } 797 } 798 bp->b_bcount += size; 799 bp->b_bufsize += size; 800 801 --numdirtybuffers; 802 tbp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI); 803 tbp->b_flags |= B_ASYNC; 804 reassignbuf(tbp, tbp->b_vp); /* put on clean list */ 805 ++tbp->b_vp->v_numoutput; 806 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head, 807 tbp, b_cluster.cluster_entry); 808 } 809 finishcluster: 810 pmap_qenter(trunc_page((vm_offset_t) bp->b_data), 811 (vm_page_t *) bp->b_pages, bp->b_npages); 812 if (bp->b_bufsize > bp->b_kvasize) 813 panic( 814 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n", 815 bp->b_bufsize, bp->b_kvasize); 816 bp->b_kvasize = bp->b_bufsize; 817 totalwritten += bp->b_bufsize; 818 bp->b_dirtyoff = 0; 819 bp->b_dirtyend = bp->b_bufsize; 820 bawrite(bp); 821 822 len -= i; 823 } 824 return totalwritten; 825} 826 827#ifdef notyet_block_reallocation_enabled 828/* 829 * Collect together all the buffers in a cluster. 830 * Plus add one additional buffer. 831 */ 832static struct cluster_save * 833cluster_collectbufs(vp, last_bp) 834 struct vnode *vp; 835 struct buf *last_bp; 836{ 837 struct cluster_save *buflist; 838 daddr_t lbn; 839 int i, len; 840 841 len = vp->v_lastw - vp->v_cstart + 1; 842 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist), 843 M_SEGMENT, M_WAITOK); 844 buflist->bs_nchildren = 0; 845 buflist->bs_children = (struct buf **) (buflist + 1); 846 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) 847 (void) bread(vp, lbn, last_bp->b_bcount, NOCRED, 848 &buflist->bs_children[i]); 849 buflist->bs_children[i] = last_bp; 850 buflist->bs_nchildren = i + 1; 851 return (buflist); 852} 853#endif /* notyet_block_reallocation_enabled */ 854