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 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_debug_cluster.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/proc.h> 43 #include <sys/bio.h> 44 #include <sys/buf.h> 45 #include <sys/vnode.h> 46 #include <sys/malloc.h> 47 #include <sys/mount.h> 48 #include <sys/resourcevar.h> 49 #include <sys/rwlock.h> 50 #include <sys/vmmeter.h> 51 #include <vm/vm.h> 52 #include <vm/vm_object.h> 53 #include <vm/vm_page.h> 54 #include <sys/sysctl.h> 55 56 #if defined(CLUSTERDEBUG) 57 static int rcluster= 0; 58 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, 59 "Debug VFS clustering code"); 60 #endif 61 62 static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer"); 63 64 static struct cluster_save *cluster_collectbufs(struct vnode *vp, 65 struct buf *last_bp, int gbflags); 66 static struct buf *cluster_rbuild(struct vnode *vp, u_quad_t filesize, 67 daddr_t lbn, daddr_t blkno, long size, int run, int gbflags, 68 struct buf *fbp); 69 static void cluster_callback(struct buf *); 70 71 static int write_behind = 1; 72 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, 73 "Cluster write-behind; 0: disable, 1: enable, 2: backed off"); 74 75 static int read_max = 64; 76 SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0, 77 "Cluster read-ahead max block count"); 78 79 static int read_min = 1; 80 SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0, 81 "Cluster read min block count"); 82 83 /* Page expended to mark partially backed buffers */ 84 extern vm_page_t bogus_page; 85 86 /* 87 * Read data to a buf, including read-ahead if we find this to be beneficial. 88 * cluster_read replaces bread. 89 */ 90 int 91 cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size, 92 struct ucred *cred, long totread, int seqcount, int gbflags, 93 struct buf **bpp) 94 { 95 struct buf *bp, *rbp, *reqbp; 96 struct bufobj *bo; 97 daddr_t blkno, origblkno; 98 int maxra, racluster; 99 int error, ncontig; 100 int i; 101 102 error = 0; 103 bo = &vp->v_bufobj; 104 if (!unmapped_buf_allowed) 105 gbflags &= ~GB_UNMAPPED; 106 107 /* 108 * Try to limit the amount of read-ahead by a few 109 * ad-hoc parameters. This needs work!!! 110 */ 111 racluster = vp->v_mount->mnt_iosize_max / size; 112 maxra = seqcount; 113 maxra = min(read_max, maxra); 114 maxra = min(nbuf/8, maxra); 115 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize) 116 maxra = (filesize / size) - lblkno; 117 118 /* 119 * get the requested block 120 */ 121 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0, gbflags); 122 origblkno = lblkno; 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 bp->b_flags &= ~B_RAM; 136 BO_LOCK(bo); 137 for (i = 1; i < maxra; i++) { 138 /* 139 * Stop if the buffer does not exist or it 140 * is invalid (about to go away?) 141 */ 142 rbp = gbincore(&vp->v_bufobj, lblkno+i); 143 if (rbp == NULL || (rbp->b_flags & B_INVAL)) 144 break; 145 146 /* 147 * Set another read-ahead mark so we know 148 * to check again. (If we can lock the 149 * buffer without waiting) 150 */ 151 if ((((i % racluster) == (racluster - 1)) || 152 (i == (maxra - 1))) 153 && (0 == BUF_LOCK(rbp, 154 LK_EXCLUSIVE | LK_NOWAIT, NULL))) { 155 rbp->b_flags |= B_RAM; 156 BUF_UNLOCK(rbp); 157 } 158 } 159 BO_UNLOCK(bo); 160 if (i >= maxra) { 161 return 0; 162 } 163 lblkno += i; 164 } 165 reqbp = bp = NULL; 166 /* 167 * If it isn't in the cache, then get a chunk from 168 * disk if sequential, otherwise just get the block. 169 */ 170 } else { 171 off_t firstread = bp->b_offset; 172 int nblks; 173 long minread; 174 175 KASSERT(bp->b_offset != NOOFFSET, 176 ("cluster_read: no buffer offset")); 177 178 ncontig = 0; 179 180 /* 181 * Adjust totread if needed 182 */ 183 minread = read_min * size; 184 if (minread > totread) 185 totread = minread; 186 187 /* 188 * Compute the total number of blocks that we should read 189 * synchronously. 190 */ 191 if (firstread + totread > filesize) 192 totread = filesize - firstread; 193 nblks = howmany(totread, size); 194 if (nblks > racluster) 195 nblks = racluster; 196 197 /* 198 * Now compute the number of contiguous blocks. 199 */ 200 if (nblks > 1) { 201 error = VOP_BMAP(vp, lblkno, NULL, 202 &blkno, &ncontig, NULL); 203 /* 204 * If this failed to map just do the original block. 205 */ 206 if (error || blkno == -1) 207 ncontig = 0; 208 } 209 210 /* 211 * If we have contiguous data available do a cluster 212 * otherwise just read the requested block. 213 */ 214 if (ncontig) { 215 /* Account for our first block. */ 216 ncontig = min(ncontig + 1, nblks); 217 if (ncontig < nblks) 218 nblks = ncontig; 219 bp = cluster_rbuild(vp, filesize, lblkno, 220 blkno, size, nblks, gbflags, bp); 221 lblkno += (bp->b_bufsize / size); 222 } else { 223 bp->b_flags |= B_RAM; 224 bp->b_iocmd = BIO_READ; 225 lblkno += 1; 226 } 227 } 228 229 /* 230 * handle the synchronous read so that it is available ASAP. 231 */ 232 if (bp) { 233 if ((bp->b_flags & B_CLUSTER) == 0) { 234 vfs_busy_pages(bp, 0); 235 } 236 bp->b_flags &= ~B_INVAL; 237 bp->b_ioflags &= ~BIO_ERROR; 238 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL) 239 BUF_KERNPROC(bp); 240 bp->b_iooffset = dbtob(bp->b_blkno); 241 bstrategy(bp); 242 curthread->td_ru.ru_inblock++; 243 } 244 245 /* 246 * If we have been doing sequential I/O, then do some read-ahead. 247 */ 248 while (lblkno < (origblkno + maxra)) { 249 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL); 250 if (error) 251 break; 252 253 if (blkno == -1) 254 break; 255 256 /* 257 * We could throttle ncontig here by maxra but we might as 258 * well read the data if it is contiguous. We're throttled 259 * by racluster anyway. 260 */ 261 if (ncontig) { 262 ncontig = min(ncontig + 1, racluster); 263 rbp = cluster_rbuild(vp, filesize, lblkno, blkno, 264 size, ncontig, gbflags, NULL); 265 lblkno += (rbp->b_bufsize / size); 266 if (rbp->b_flags & B_DELWRI) { 267 bqrelse(rbp); 268 continue; 269 } 270 } else { 271 rbp = getblk(vp, lblkno, size, 0, 0, gbflags); 272 lblkno += 1; 273 if (rbp->b_flags & B_DELWRI) { 274 bqrelse(rbp); 275 continue; 276 } 277 rbp->b_flags |= B_ASYNC | B_RAM; 278 rbp->b_iocmd = BIO_READ; 279 rbp->b_blkno = blkno; 280 } 281 if (rbp->b_flags & B_CACHE) { 282 rbp->b_flags &= ~B_ASYNC; 283 bqrelse(rbp); 284 continue; 285 } 286 if ((rbp->b_flags & B_CLUSTER) == 0) { 287 vfs_busy_pages(rbp, 0); 288 } 289 rbp->b_flags &= ~B_INVAL; 290 rbp->b_ioflags &= ~BIO_ERROR; 291 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL) 292 BUF_KERNPROC(rbp); 293 rbp->b_iooffset = dbtob(rbp->b_blkno); 294 bstrategy(rbp); 295 curthread->td_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 */ 309 static struct buf * 310 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn, 311 daddr_t blkno, long size, int run, int gbflags, struct buf *fbp) 312 { 313 struct bufobj *bo; 314 struct buf *bp, *tbp; 315 daddr_t bn; 316 off_t off; 317 long tinc, tsize; 318 int i, inc, j, toff; 319 320 KASSERT(size == vp->v_mount->mnt_stat.f_iosize, 321 ("cluster_rbuild: size %ld != f_iosize %jd\n", 322 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize)); 323 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_iocmd = BIO_READ; 334 } else { 335 tbp = getblk(vp, lbn, size, 0, 0, gbflags); 336 if (tbp->b_flags & B_CACHE) 337 return tbp; 338 tbp->b_flags |= B_ASYNC | B_RAM; 339 tbp->b_iocmd = BIO_READ; 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(&cluster_pbuf_freecnt); 347 if (bp == 0) 348 return tbp; 349 350 /* 351 * We are synthesizing a buffer out of vm_page_t's, but 352 * if the block size is not page aligned then the starting 353 * address may not be either. Inherit the b_data offset 354 * from the original buffer. 355 */ 356 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO; 357 if ((gbflags & GB_UNMAPPED) != 0) { 358 bp->b_flags |= B_UNMAPPED; 359 bp->b_data = unmapped_buf; 360 } else { 361 bp->b_data = (char *)((vm_offset_t)bp->b_data | 362 ((vm_offset_t)tbp->b_data & PAGE_MASK)); 363 } 364 bp->b_iocmd = BIO_READ; 365 bp->b_iodone = cluster_callback; 366 bp->b_blkno = blkno; 367 bp->b_lblkno = lbn; 368 bp->b_offset = tbp->b_offset; 369 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset")); 370 pbgetvp(vp, bp); 371 372 TAILQ_INIT(&bp->b_cluster.cluster_head); 373 374 bp->b_bcount = 0; 375 bp->b_bufsize = 0; 376 bp->b_npages = 0; 377 378 inc = btodb(size); 379 bo = &vp->v_bufobj; 380 for (bn = blkno, i = 0; i < run; ++i, bn += inc) { 381 if (i != 0) { 382 if ((bp->b_npages * PAGE_SIZE) + 383 round_page(size) > vp->v_mount->mnt_iosize_max) { 384 break; 385 } 386 387 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT | 388 (gbflags & GB_UNMAPPED)); 389 390 /* Don't wait around for locked bufs. */ 391 if (tbp == NULL) 392 break; 393 394 /* 395 * Stop scanning if the buffer is fully valid 396 * (marked B_CACHE), or locked (may be doing a 397 * background write), or if the buffer is not 398 * VMIO backed. The clustering code can only deal 399 * with VMIO-backed buffers. 400 */ 401 BO_LOCK(bo); 402 if ((tbp->b_vflags & BV_BKGRDINPROG) || 403 (tbp->b_flags & B_CACHE) || 404 (tbp->b_flags & B_VMIO) == 0) { 405 BO_UNLOCK(bo); 406 bqrelse(tbp); 407 break; 408 } 409 BO_UNLOCK(bo); 410 411 /* 412 * The buffer must be completely invalid in order to 413 * take part in the cluster. If it is partially valid 414 * then we stop. 415 */ 416 off = tbp->b_offset; 417 tsize = size; 418 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object); 419 for (j = 0; tsize > 0; j++) { 420 toff = off & PAGE_MASK; 421 tinc = tsize; 422 if (toff + tinc > PAGE_SIZE) 423 tinc = PAGE_SIZE - toff; 424 VM_OBJECT_ASSERT_WLOCKED(tbp->b_pages[j]->object); 425 if ((tbp->b_pages[j]->valid & 426 vm_page_bits(toff, tinc)) != 0) 427 break; 428 off += tinc; 429 tsize -= tinc; 430 } 431 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object); 432 if (tsize > 0) { 433 bqrelse(tbp); 434 break; 435 } 436 437 /* 438 * Set a read-ahead mark as appropriate 439 */ 440 if ((fbp && (i == 1)) || (i == (run - 1))) 441 tbp->b_flags |= B_RAM; 442 443 /* 444 * Set the buffer up for an async read (XXX should 445 * we do this only if we do not wind up brelse()ing?). 446 * Set the block number if it isn't set, otherwise 447 * if it is make sure it matches the block number we 448 * expect. 449 */ 450 tbp->b_flags |= B_ASYNC; 451 tbp->b_iocmd = BIO_READ; 452 if (tbp->b_blkno == tbp->b_lblkno) { 453 tbp->b_blkno = bn; 454 } else if (tbp->b_blkno != bn) { 455 brelse(tbp); 456 break; 457 } 458 } 459 /* 460 * XXX fbp from caller may not be B_ASYNC, but we are going 461 * to biodone() it in cluster_callback() anyway 462 */ 463 BUF_KERNPROC(tbp); 464 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head, 465 tbp, b_cluster.cluster_entry); 466 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object); 467 for (j = 0; j < tbp->b_npages; j += 1) { 468 vm_page_t m; 469 m = tbp->b_pages[j]; 470 vm_page_io_start(m); 471 vm_object_pip_add(m->object, 1); 472 if ((bp->b_npages == 0) || 473 (bp->b_pages[bp->b_npages-1] != m)) { 474 bp->b_pages[bp->b_npages] = m; 475 bp->b_npages++; 476 } 477 if (m->valid == VM_PAGE_BITS_ALL) 478 tbp->b_pages[j] = bogus_page; 479 } 480 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object); 481 /* 482 * Don't inherit tbp->b_bufsize as it may be larger due to 483 * a non-page-aligned size. Instead just aggregate using 484 * 'size'. 485 */ 486 if (tbp->b_bcount != size) 487 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size); 488 if (tbp->b_bufsize != size) 489 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size); 490 bp->b_bcount += size; 491 bp->b_bufsize += size; 492 } 493 494 /* 495 * Fully valid pages in the cluster are already good and do not need 496 * to be re-read from disk. Replace the page with bogus_page 497 */ 498 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object); 499 for (j = 0; j < bp->b_npages; j++) { 500 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[j]->object); 501 if (bp->b_pages[j]->valid == VM_PAGE_BITS_ALL) 502 bp->b_pages[j] = bogus_page; 503 } 504 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object); 505 if (bp->b_bufsize > bp->b_kvasize) 506 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n", 507 bp->b_bufsize, bp->b_kvasize); 508 bp->b_kvasize = bp->b_bufsize; 509 510 if ((bp->b_flags & B_UNMAPPED) == 0) { 511 pmap_qenter(trunc_page((vm_offset_t) bp->b_data), 512 (vm_page_t *)bp->b_pages, bp->b_npages); 513 } 514 return (bp); 515 } 516 517 /* 518 * Cleanup after a clustered read or write. 519 * This is complicated by the fact that any of the buffers might have 520 * extra memory (if there were no empty buffer headers at allocbuf time) 521 * that we will need to shift around. 522 */ 523 static void 524 cluster_callback(bp) 525 struct buf *bp; 526 { 527 struct buf *nbp, *tbp; 528 int error = 0; 529 530 /* 531 * Must propogate errors to all the components. 532 */ 533 if (bp->b_ioflags & BIO_ERROR) 534 error = bp->b_error; 535 536 if ((bp->b_flags & B_UNMAPPED) == 0) { 537 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), 538 bp->b_npages); 539 } 540 /* 541 * Move memory from the large cluster buffer into the component 542 * buffers and mark IO as done on these. 543 */ 544 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head); 545 tbp; tbp = nbp) { 546 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry); 547 if (error) { 548 tbp->b_ioflags |= BIO_ERROR; 549 tbp->b_error = error; 550 } else { 551 tbp->b_dirtyoff = tbp->b_dirtyend = 0; 552 tbp->b_flags &= ~B_INVAL; 553 tbp->b_ioflags &= ~BIO_ERROR; 554 /* 555 * XXX the bdwrite()/bqrelse() issued during 556 * cluster building clears B_RELBUF (see bqrelse() 557 * comment). If direct I/O was specified, we have 558 * to restore it here to allow the buffer and VM 559 * to be freed. 560 */ 561 if (tbp->b_flags & B_DIRECT) 562 tbp->b_flags |= B_RELBUF; 563 } 564 bufdone(tbp); 565 } 566 pbrelvp(bp); 567 relpbuf(bp, &cluster_pbuf_freecnt); 568 } 569 570 /* 571 * cluster_wbuild_wb: 572 * 573 * Implement modified write build for cluster. 574 * 575 * write_behind = 0 write behind disabled 576 * write_behind = 1 write behind normal (default) 577 * write_behind = 2 write behind backed-off 578 */ 579 580 static __inline int 581 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len, 582 int gbflags) 583 { 584 int r = 0; 585 586 switch (write_behind) { 587 case 2: 588 if (start_lbn < len) 589 break; 590 start_lbn -= len; 591 /* FALLTHROUGH */ 592 case 1: 593 r = cluster_wbuild(vp, size, start_lbn, len, gbflags); 594 /* FALLTHROUGH */ 595 default: 596 /* FALLTHROUGH */ 597 break; 598 } 599 return(r); 600 } 601 602 /* 603 * Do clustered write for FFS. 604 * 605 * Three cases: 606 * 1. Write is not sequential (write asynchronously) 607 * Write is sequential: 608 * 2. beginning of cluster - begin cluster 609 * 3. middle of a cluster - add to cluster 610 * 4. end of a cluster - asynchronously write cluster 611 */ 612 void 613 cluster_write(struct vnode *vp, struct buf *bp, u_quad_t filesize, int seqcount, 614 int gbflags) 615 { 616 daddr_t lbn; 617 int maxclen, cursize; 618 int lblocksize; 619 int async; 620 621 if (!unmapped_buf_allowed) 622 gbflags &= ~GB_UNMAPPED; 623 624 if (vp->v_type == VREG) { 625 async = DOINGASYNC(vp); 626 lblocksize = vp->v_mount->mnt_stat.f_iosize; 627 } else { 628 async = 0; 629 lblocksize = bp->b_bufsize; 630 } 631 lbn = bp->b_lblkno; 632 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset")); 633 634 /* Initialize vnode to beginning of file. */ 635 if (lbn == 0) 636 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 637 638 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 || 639 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) { 640 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1; 641 if (vp->v_clen != 0) { 642 /* 643 * Next block is not sequential. 644 * 645 * If we are not writing at end of file, the process 646 * seeked to another point in the file since its last 647 * write, or we have reached our maximum cluster size, 648 * then push the previous cluster. Otherwise try 649 * reallocating to make it sequential. 650 * 651 * Change to algorithm: only push previous cluster if 652 * it was sequential from the point of view of the 653 * seqcount heuristic, otherwise leave the buffer 654 * intact so we can potentially optimize the I/O 655 * later on in the buf_daemon or update daemon 656 * flush. 657 */ 658 cursize = vp->v_lastw - vp->v_cstart + 1; 659 if (((u_quad_t) bp->b_offset + lblocksize) != filesize || 660 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) { 661 if (!async && seqcount > 0) { 662 cluster_wbuild_wb(vp, lblocksize, 663 vp->v_cstart, cursize, gbflags); 664 } 665 } else { 666 struct buf **bpp, **endbp; 667 struct cluster_save *buflist; 668 669 buflist = cluster_collectbufs(vp, bp, gbflags); 670 endbp = &buflist->bs_children 671 [buflist->bs_nchildren - 1]; 672 if (VOP_REALLOCBLKS(vp, buflist)) { 673 /* 674 * Failed, push the previous cluster 675 * if *really* writing sequentially 676 * in the logical file (seqcount > 1), 677 * otherwise delay it in the hopes that 678 * the low level disk driver can 679 * optimize the write ordering. 680 */ 681 for (bpp = buflist->bs_children; 682 bpp < endbp; bpp++) 683 brelse(*bpp); 684 free(buflist, M_SEGMENT); 685 if (seqcount > 1) { 686 cluster_wbuild_wb(vp, 687 lblocksize, vp->v_cstart, 688 cursize, gbflags); 689 } 690 } else { 691 /* 692 * Succeeded, keep building cluster. 693 */ 694 for (bpp = buflist->bs_children; 695 bpp <= endbp; bpp++) 696 bdwrite(*bpp); 697 free(buflist, M_SEGMENT); 698 vp->v_lastw = lbn; 699 vp->v_lasta = bp->b_blkno; 700 return; 701 } 702 } 703 } 704 /* 705 * Consider beginning a cluster. If at end of file, make 706 * cluster as large as possible, otherwise find size of 707 * existing cluster. 708 */ 709 if ((vp->v_type == VREG) && 710 ((u_quad_t) bp->b_offset + lblocksize) != filesize && 711 (bp->b_blkno == bp->b_lblkno) && 712 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) || 713 bp->b_blkno == -1)) { 714 bawrite(bp); 715 vp->v_clen = 0; 716 vp->v_lasta = bp->b_blkno; 717 vp->v_cstart = lbn + 1; 718 vp->v_lastw = lbn; 719 return; 720 } 721 vp->v_clen = maxclen; 722 if (!async && maxclen == 0) { /* I/O not contiguous */ 723 vp->v_cstart = lbn + 1; 724 bawrite(bp); 725 } else { /* Wait for rest of cluster */ 726 vp->v_cstart = lbn; 727 bdwrite(bp); 728 } 729 } else if (lbn == vp->v_cstart + vp->v_clen) { 730 /* 731 * At end of cluster, write it out if seqcount tells us we 732 * are operating sequentially, otherwise let the buf or 733 * update daemon handle it. 734 */ 735 bdwrite(bp); 736 if (seqcount > 1) { 737 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, 738 vp->v_clen + 1, gbflags); 739 } 740 vp->v_clen = 0; 741 vp->v_cstart = lbn + 1; 742 } else if (vm_page_count_severe()) { 743 /* 744 * We are low on memory, get it going NOW 745 */ 746 bawrite(bp); 747 } else { 748 /* 749 * In the middle of a cluster, so just delay the I/O for now. 750 */ 751 bdwrite(bp); 752 } 753 vp->v_lastw = lbn; 754 vp->v_lasta = bp->b_blkno; 755 } 756 757 758 /* 759 * This is an awful lot like cluster_rbuild...wish they could be combined. 760 * The last lbn argument is the current block on which I/O is being 761 * performed. Check to see that it doesn't fall in the middle of 762 * the current block (if last_bp == NULL). 763 */ 764 int 765 cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len, 766 int gbflags) 767 { 768 struct buf *bp, *tbp; 769 struct bufobj *bo; 770 int i, j; 771 int totalwritten = 0; 772 int dbsize = btodb(size); 773 774 if (!unmapped_buf_allowed) 775 gbflags &= ~GB_UNMAPPED; 776 777 bo = &vp->v_bufobj; 778 while (len > 0) { 779 /* 780 * If the buffer is not delayed-write (i.e. dirty), or it 781 * is delayed-write but either locked or inval, it cannot 782 * partake in the clustered write. 783 */ 784 BO_LOCK(bo); 785 if ((tbp = gbincore(&vp->v_bufobj, start_lbn)) == NULL || 786 (tbp->b_vflags & BV_BKGRDINPROG)) { 787 BO_UNLOCK(bo); 788 ++start_lbn; 789 --len; 790 continue; 791 } 792 if (BUF_LOCK(tbp, 793 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_MTX(bo))) { 794 ++start_lbn; 795 --len; 796 continue; 797 } 798 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) { 799 BUF_UNLOCK(tbp); 800 ++start_lbn; 801 --len; 802 continue; 803 } 804 if (tbp->b_pin_count > 0) { 805 BUF_UNLOCK(tbp); 806 ++start_lbn; 807 --len; 808 continue; 809 } 810 bremfree(tbp); 811 tbp->b_flags &= ~B_DONE; 812 813 /* 814 * Extra memory in the buffer, punt on this buffer. 815 * XXX we could handle this in most cases, but we would 816 * have to push the extra memory down to after our max 817 * possible cluster size and then potentially pull it back 818 * up if the cluster was terminated prematurely--too much 819 * hassle. 820 */ 821 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) != 822 (B_CLUSTEROK | B_VMIO)) || 823 (tbp->b_bcount != tbp->b_bufsize) || 824 (tbp->b_bcount != size) || 825 (len == 1) || 826 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) { 827 totalwritten += tbp->b_bufsize; 828 bawrite(tbp); 829 ++start_lbn; 830 --len; 831 continue; 832 } 833 834 /* 835 * We got a pbuf to make the cluster in. 836 * so initialise it. 837 */ 838 TAILQ_INIT(&bp->b_cluster.cluster_head); 839 bp->b_bcount = 0; 840 bp->b_bufsize = 0; 841 bp->b_npages = 0; 842 if (tbp->b_wcred != NOCRED) 843 bp->b_wcred = crhold(tbp->b_wcred); 844 845 bp->b_blkno = tbp->b_blkno; 846 bp->b_lblkno = tbp->b_lblkno; 847 bp->b_offset = tbp->b_offset; 848 849 /* 850 * We are synthesizing a buffer out of vm_page_t's, but 851 * if the block size is not page aligned then the starting 852 * address may not be either. Inherit the b_data offset 853 * from the original buffer. 854 */ 855 if ((gbflags & GB_UNMAPPED) == 0 || 856 (tbp->b_flags & B_VMIO) == 0) { 857 bp->b_data = (char *)((vm_offset_t)bp->b_data | 858 ((vm_offset_t)tbp->b_data & PAGE_MASK)); 859 } else { 860 bp->b_flags |= B_UNMAPPED; 861 bp->b_data = unmapped_buf; 862 } 863 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO | 864 B_NEEDCOMMIT)); 865 bp->b_iodone = cluster_callback; 866 pbgetvp(vp, bp); 867 /* 868 * From this location in the file, scan forward to see 869 * if there are buffers with adjacent data that need to 870 * be written as well. 871 */ 872 for (i = 0; i < len; ++i, ++start_lbn) { 873 if (i != 0) { /* If not the first buffer */ 874 /* 875 * If the adjacent data is not even in core it 876 * can't need to be written. 877 */ 878 BO_LOCK(bo); 879 if ((tbp = gbincore(bo, start_lbn)) == NULL || 880 (tbp->b_vflags & BV_BKGRDINPROG)) { 881 BO_UNLOCK(bo); 882 break; 883 } 884 885 /* 886 * If it IS in core, but has different 887 * characteristics, or is locked (which 888 * means it could be undergoing a background 889 * I/O or be in a weird state), then don't 890 * cluster with it. 891 */ 892 if (BUF_LOCK(tbp, 893 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, 894 BO_MTX(bo))) 895 break; 896 897 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK | 898 B_INVAL | B_DELWRI | B_NEEDCOMMIT)) 899 != (B_DELWRI | B_CLUSTEROK | 900 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) || 901 tbp->b_wcred != bp->b_wcred) { 902 BUF_UNLOCK(tbp); 903 break; 904 } 905 906 /* 907 * Check that the combined cluster 908 * would make sense with regard to pages 909 * and would not be too large 910 */ 911 if ((tbp->b_bcount != size) || 912 ((bp->b_blkno + (dbsize * i)) != 913 tbp->b_blkno) || 914 ((tbp->b_npages + bp->b_npages) > 915 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) { 916 BUF_UNLOCK(tbp); 917 break; 918 } 919 920 /* 921 * Do not pull in pinned buffers. 922 */ 923 if (tbp->b_pin_count > 0) { 924 BUF_UNLOCK(tbp); 925 break; 926 } 927 928 /* 929 * Ok, it's passed all the tests, 930 * so remove it from the free list 931 * and mark it busy. We will use it. 932 */ 933 bremfree(tbp); 934 tbp->b_flags &= ~B_DONE; 935 } /* end of code for non-first buffers only */ 936 /* 937 * If the IO is via the VM then we do some 938 * special VM hackery (yuck). Since the buffer's 939 * block size may not be page-aligned it is possible 940 * for a page to be shared between two buffers. We 941 * have to get rid of the duplication when building 942 * the cluster. 943 */ 944 if (tbp->b_flags & B_VMIO) { 945 vm_page_t m; 946 947 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object); 948 if (i != 0) { /* if not first buffer */ 949 for (j = 0; j < tbp->b_npages; j += 1) { 950 m = tbp->b_pages[j]; 951 if (m->oflags & VPO_BUSY) { 952 VM_OBJECT_WUNLOCK( 953 tbp->b_object); 954 bqrelse(tbp); 955 goto finishcluster; 956 } 957 } 958 } 959 for (j = 0; j < tbp->b_npages; j += 1) { 960 m = tbp->b_pages[j]; 961 vm_page_io_start(m); 962 vm_object_pip_add(m->object, 1); 963 if ((bp->b_npages == 0) || 964 (bp->b_pages[bp->b_npages - 1] != m)) { 965 bp->b_pages[bp->b_npages] = m; 966 bp->b_npages++; 967 } 968 } 969 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object); 970 } 971 bp->b_bcount += size; 972 bp->b_bufsize += size; 973 /* 974 * If any of the clustered buffers have their 975 * B_BARRIER flag set, transfer that request to 976 * the cluster. 977 */ 978 bp->b_flags |= (tbp->b_flags & B_BARRIER); 979 tbp->b_flags &= ~(B_DONE | B_BARRIER); 980 tbp->b_flags |= B_ASYNC; 981 tbp->b_ioflags &= ~BIO_ERROR; 982 tbp->b_iocmd = BIO_WRITE; 983 bundirty(tbp); 984 reassignbuf(tbp); /* put on clean list */ 985 bufobj_wref(tbp->b_bufobj); 986 BUF_KERNPROC(tbp); 987 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head, 988 tbp, b_cluster.cluster_entry); 989 } 990 finishcluster: 991 if ((bp->b_flags & B_UNMAPPED) == 0) { 992 pmap_qenter(trunc_page((vm_offset_t) bp->b_data), 993 (vm_page_t *)bp->b_pages, bp->b_npages); 994 } 995 if (bp->b_bufsize > bp->b_kvasize) 996 panic( 997 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n", 998 bp->b_bufsize, bp->b_kvasize); 999 bp->b_kvasize = bp->b_bufsize; 1000 totalwritten += bp->b_bufsize; 1001 bp->b_dirtyoff = 0; 1002 bp->b_dirtyend = bp->b_bufsize; 1003 bawrite(bp); 1004 1005 len -= i; 1006 } 1007 return totalwritten; 1008 } 1009 1010 /* 1011 * Collect together all the buffers in a cluster. 1012 * Plus add one additional buffer. 1013 */ 1014 static struct cluster_save * 1015 cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int gbflags) 1016 { 1017 struct cluster_save *buflist; 1018 struct buf *bp; 1019 daddr_t lbn; 1020 int i, len; 1021 1022 len = vp->v_lastw - vp->v_cstart + 1; 1023 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist), 1024 M_SEGMENT, M_WAITOK); 1025 buflist->bs_nchildren = 0; 1026 buflist->bs_children = (struct buf **) (buflist + 1); 1027 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) { 1028 (void)bread_gb(vp, lbn, last_bp->b_bcount, NOCRED, 1029 gbflags, &bp); 1030 buflist->bs_children[i] = bp; 1031 if (bp->b_blkno == bp->b_lblkno) 1032 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, 1033 NULL, NULL); 1034 } 1035 buflist->bs_children[i] = bp = last_bp; 1036 if (bp->b_blkno == bp->b_lblkno) 1037 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL); 1038 buflist->bs_nchildren = i + 1; 1039 return (buflist); 1040 } 1041