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