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