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