vfs_cluster.c revision 79224
11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 45455Sdg * Modifications/enhancements: 55455Sdg * Copyright (c) 1995 John S. Dyson. All rights reserved. 61541Srgrimes * 71541Srgrimes * Redistribution and use in source and binary forms, with or without 81541Srgrimes * modification, are permitted provided that the following conditions 91541Srgrimes * are met: 101541Srgrimes * 1. Redistributions of source code must retain the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer. 121541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 131541Srgrimes * notice, this list of conditions and the following disclaimer in the 141541Srgrimes * documentation and/or other materials provided with the distribution. 151541Srgrimes * 3. All advertising materials mentioning features or use of this software 161541Srgrimes * must display the following acknowledgement: 171541Srgrimes * This product includes software developed by the University of 181541Srgrimes * California, Berkeley and its contributors. 191541Srgrimes * 4. Neither the name of the University nor the names of its contributors 201541Srgrimes * may be used to endorse or promote products derived from this software 211541Srgrimes * without specific prior written permission. 221541Srgrimes * 231541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 241541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 251541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 261541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 271541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 281541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 291541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 301541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 311541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 321541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 331541Srgrimes * SUCH DAMAGE. 341541Srgrimes * 351541Srgrimes * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94 3650477Speter * $FreeBSD: head/sys/kern/vfs_cluster.c 79224 2001-07-04 16:20:28Z dillon $ 371541Srgrimes */ 381541Srgrimes 3932929Seivind#include "opt_debug_cluster.h" 4032929Seivind 411541Srgrimes#include <sys/param.h> 421549Srgrimes#include <sys/systm.h> 4341168Sbde#include <sys/kernel.h> 441541Srgrimes#include <sys/proc.h> 4560041Sphk#include <sys/bio.h> 461541Srgrimes#include <sys/buf.h> 471541Srgrimes#include <sys/vnode.h> 4841124Sdg#include <sys/malloc.h> 491541Srgrimes#include <sys/mount.h> 501541Srgrimes#include <sys/resourcevar.h> 5168885Sdillon#include <sys/vmmeter.h> 526621Sdg#include <vm/vm.h> 5310541Sdyson#include <vm/vm_object.h> 5410541Sdyson#include <vm/vm_page.h> 5548545Smckusick#include <sys/sysctl.h> 561541Srgrimes 5721002Sdyson#if defined(CLUSTERDEBUG) 5821002Sdyson#include <sys/sysctl.h> 5921002Sdysonstatic int rcluster= 0; 6024484SbdeSYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, ""); 6121002Sdyson#endif 6221002Sdyson 6341124Sdgstatic MALLOC_DEFINE(M_SEGMENT, "cluster_save buffer", "cluster_save buffer"); 6441124Sdg 6512973Sbdestatic struct cluster_save * 6612973Sbde cluster_collectbufs __P((struct vnode *vp, struct buf *last_bp)); 6712973Sbdestatic struct buf * 6812973Sbde cluster_rbuild __P((struct vnode *vp, u_quad_t filesize, daddr_t lbn, 6921002Sdyson daddr_t blkno, long size, int run, struct buf *fbp)); 701541Srgrimes 7148545Smckusickstatic int write_behind = 1; 7248545SmckusickSYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, ""); 7348545Smckusick 7412973Sbdeextern vm_page_t bogus_page; 755455Sdg 7642957Sdillonextern int cluster_pbuf_freecnt; 7742957Sdillon 781541Srgrimes/* 7921002Sdyson * Maximum number of blocks for read-ahead. 801541Srgrimes */ 8121002Sdyson#define MAXRA 32 825455Sdg 831541Srgrimes/* 8421002Sdyson * This replaces bread. 8510541Sdyson */ 861549Srgrimesint 8721002Sdysoncluster_read(vp, filesize, lblkno, size, cred, totread, seqcount, bpp) 881541Srgrimes struct vnode *vp; 891541Srgrimes u_quad_t filesize; 901541Srgrimes daddr_t lblkno; 911541Srgrimes long size; 921541Srgrimes struct ucred *cred; 9321002Sdyson long totread; 9421002Sdyson int seqcount; 951541Srgrimes struct buf **bpp; 961541Srgrimes{ 9721002Sdyson struct buf *bp, *rbp, *reqbp; 9831016Sphk daddr_t blkno, origblkno; 9921002Sdyson int error, num_ra; 10010541Sdyson int i; 10121002Sdyson int maxra, racluster; 10221002Sdyson long origtotread; 1031541Srgrimes 1041541Srgrimes error = 0; 10521002Sdyson 1065455Sdg /* 10721002Sdyson * Try to limit the amount of read-ahead by a few 10821002Sdyson * ad-hoc parameters. This needs work!!! 10921002Sdyson */ 11051797Sphk racluster = vp->v_mount->mnt_iosize_max / size; 11121002Sdyson maxra = 2 * racluster + (totread / size); 11221002Sdyson if (maxra > MAXRA) 11321002Sdyson maxra = MAXRA; 11421002Sdyson if (maxra > nbuf/8) 11521002Sdyson maxra = nbuf/8; 11621002Sdyson 11721002Sdyson /* 1185455Sdg * get the requested block 1195455Sdg */ 12021002Sdyson *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0); 12121002Sdyson origblkno = lblkno; 12221002Sdyson origtotread = totread; 12312767Sdyson 1245455Sdg /* 1255455Sdg * if it is in the cache, then check to see if the reads have been 1265455Sdg * sequential. If they have, then try some read-ahead, otherwise 1275455Sdg * back-off on prospective read-aheads. 1285455Sdg */ 1291541Srgrimes if (bp->b_flags & B_CACHE) { 13021002Sdyson if (!seqcount) { 1315455Sdg return 0; 13221002Sdyson } else if ((bp->b_flags & B_RAM) == 0) { 13321002Sdyson return 0; 13421002Sdyson } else { 13521002Sdyson int s; 13621002Sdyson struct buf *tbp; 13721002Sdyson bp->b_flags &= ~B_RAM; 13821002Sdyson /* 13921002Sdyson * We do the spl here so that there is no window 14021002Sdyson * between the incore and the b_usecount increment 14121002Sdyson * below. We opt to keep the spl out of the loop 14221002Sdyson * for efficiency. 14321002Sdyson */ 14421002Sdyson s = splbio(); 14548225Smckusick for (i = 1; i < maxra; i++) { 14621002Sdyson 14721002Sdyson if (!(tbp = incore(vp, lblkno+i))) { 14821002Sdyson break; 14921002Sdyson } 15021002Sdyson 15121002Sdyson /* 15248677Smckusick * Set another read-ahead mark so we know 15348677Smckusick * to check again. 15421002Sdyson */ 15521002Sdyson if (((i % racluster) == (racluster - 1)) || 15621002Sdyson (i == (maxra - 1))) 15721002Sdyson tbp->b_flags |= B_RAM; 15821002Sdyson } 15921002Sdyson splx(s); 16021002Sdyson if (i >= maxra) { 1615839Sdg return 0; 16210541Sdyson } 16321002Sdyson lblkno += i; 16421002Sdyson } 16521002Sdyson reqbp = bp = NULL; 16621002Sdyson } else { 16742453Seivind off_t firstread = bp->b_offset; 16842453Seivind 16942408Seivind KASSERT(bp->b_offset != NOOFFSET, 17042453Seivind ("cluster_read: no buffer offset")); 17121002Sdyson if (firstread + totread > filesize) 17221002Sdyson totread = filesize - firstread; 17321002Sdyson if (totread > size) { 17421002Sdyson int nblks = 0; 17521002Sdyson int ncontigafter; 17621002Sdyson while (totread > 0) { 17721002Sdyson nblks++; 17821002Sdyson totread -= size; 17921002Sdyson } 18021002Sdyson if (nblks == 1) 18121002Sdyson goto single_block_read; 18221002Sdyson if (nblks > racluster) 18321002Sdyson nblks = racluster; 18421002Sdyson 18521002Sdyson error = VOP_BMAP(vp, lblkno, NULL, 18621002Sdyson &blkno, &ncontigafter, NULL); 18721002Sdyson if (error) 18821002Sdyson goto single_block_read; 18921002Sdyson if (blkno == -1) 19021002Sdyson goto single_block_read; 19121002Sdyson if (ncontigafter == 0) 19221002Sdyson goto single_block_read; 19321002Sdyson if (ncontigafter + 1 < nblks) 19421002Sdyson nblks = ncontigafter + 1; 19521002Sdyson 19621002Sdyson bp = cluster_rbuild(vp, filesize, lblkno, 19721002Sdyson blkno, size, nblks, bp); 19834694Sdyson lblkno += (bp->b_bufsize / size); 19910541Sdyson } else { 20021002Sdysonsingle_block_read: 20121002Sdyson /* 20221002Sdyson * if it isn't in the cache, then get a chunk from 20321002Sdyson * disk if sequential, otherwise just get the block. 20421002Sdyson */ 20558345Sphk bp->b_flags |= B_RAM; 20658345Sphk bp->b_iocmd = BIO_READ; 20710541Sdyson lblkno += 1; 2088876Srgrimes } 2091541Srgrimes } 2105455Sdg 2115455Sdg /* 2125455Sdg * if we have been doing sequential I/O, then do some read-ahead 2135455Sdg */ 21421002Sdyson rbp = NULL; 21521002Sdyson if (seqcount && (lblkno < (origblkno + seqcount))) { 2161541Srgrimes /* 21721002Sdyson * we now build the read-ahead buffer if it is desirable. 2181541Srgrimes */ 21921002Sdyson if (((u_quad_t)(lblkno + 1) * size) <= filesize && 22021002Sdyson !(error = VOP_BMAP(vp, lblkno, NULL, &blkno, &num_ra, NULL)) && 22121002Sdyson blkno != -1) { 22221002Sdyson int nblksread; 22321002Sdyson int ntoread = num_ra + 1; 22421002Sdyson nblksread = (origtotread + size - 1) / size; 22521002Sdyson if (seqcount < nblksread) 22621002Sdyson seqcount = nblksread; 22721002Sdyson if (seqcount < ntoread) 22821002Sdyson ntoread = seqcount; 22921002Sdyson if (num_ra) { 23021002Sdyson rbp = cluster_rbuild(vp, filesize, lblkno, 23121002Sdyson blkno, size, ntoread, NULL); 23221002Sdyson } else { 23321002Sdyson rbp = getblk(vp, lblkno, size, 0, 0); 23458345Sphk rbp->b_flags |= B_ASYNC | B_RAM; 23558345Sphk rbp->b_iocmd = BIO_READ; 23621002Sdyson rbp->b_blkno = blkno; 2375455Sdg } 2381541Srgrimes } 2395455Sdg } 2401541Srgrimes 2415455Sdg /* 24210541Sdyson * handle the synchronous read 2435455Sdg */ 2445455Sdg if (bp) { 24521002Sdyson#if defined(CLUSTERDEBUG) 24636275Sdyson if (rcluster) 24737951Sbde printf("S(%ld,%ld,%d) ", 24837951Sbde (long)bp->b_lblkno, bp->b_bcount, seqcount); 24921002Sdyson#endif 25070374Sdillon if ((bp->b_flags & B_CLUSTER) == 0) { 25136275Sdyson vfs_busy_pages(bp, 0); 25270374Sdillon } 25358934Sphk bp->b_flags &= ~B_INVAL; 25458934Sphk bp->b_ioflags &= ~BIO_ERROR; 25558345Sphk if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL) 25648333Speter BUF_KERNPROC(bp); 25737384Sjulian error = VOP_STRATEGY(vp, bp); 25836275Sdyson curproc->p_stats->p_ru.ru_inblock++; 2595455Sdg } 26034611Sdyson 2615455Sdg /* 2625455Sdg * and if we have read-aheads, do them too 2635455Sdg */ 2645455Sdg if (rbp) { 26513490Sdyson if (error) { 26658345Sphk rbp->b_flags &= ~B_ASYNC; 2671541Srgrimes brelse(rbp); 26813490Sdyson } else if (rbp->b_flags & B_CACHE) { 26958345Sphk rbp->b_flags &= ~B_ASYNC; 27013490Sdyson bqrelse(rbp); 2715455Sdg } else { 27221002Sdyson#if defined(CLUSTERDEBUG) 27321002Sdyson if (rcluster) { 27421002Sdyson if (bp) 27537951Sbde printf("A+(%ld,%ld,%ld,%d) ", 27637951Sbde (long)rbp->b_lblkno, rbp->b_bcount, 27737951Sbde (long)(rbp->b_lblkno - origblkno), 27837951Sbde seqcount); 27921002Sdyson else 28037951Sbde printf("A(%ld,%ld,%ld,%d) ", 28137951Sbde (long)rbp->b_lblkno, rbp->b_bcount, 28237951Sbde (long)(rbp->b_lblkno - origblkno), 28337951Sbde seqcount); 28421002Sdyson } 28521002Sdyson#endif 28621002Sdyson 28770374Sdillon if ((rbp->b_flags & B_CLUSTER) == 0) { 28810541Sdyson vfs_busy_pages(rbp, 0); 28970374Sdillon } 29058934Sphk rbp->b_flags &= ~B_INVAL; 29158934Sphk rbp->b_ioflags &= ~BIO_ERROR; 29258345Sphk if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL) 29348333Speter BUF_KERNPROC(rbp); 29437384Sjulian (void) VOP_STRATEGY(vp, rbp); 2955455Sdg curproc->p_stats->p_ru.ru_inblock++; 2965455Sdg } 2975455Sdg } 29821002Sdyson if (reqbp) 29959762Sphk return (bufwait(reqbp)); 30021002Sdyson else 30121002Sdyson return (error); 3021541Srgrimes} 3031541Srgrimes 3041541Srgrimes/* 3051541Srgrimes * If blocks are contiguous on disk, use this to provide clustered 3061541Srgrimes * read ahead. We will read as many blocks as possible sequentially 3071541Srgrimes * and then parcel them up into logical blocks in the buffer hash table. 3081541Srgrimes */ 30910541Sdysonstatic struct buf * 31021002Sdysoncluster_rbuild(vp, filesize, lbn, blkno, size, run, fbp) 3111541Srgrimes struct vnode *vp; 3121541Srgrimes u_quad_t filesize; 3131541Srgrimes daddr_t lbn; 3141541Srgrimes daddr_t blkno; 3151541Srgrimes long size; 3161541Srgrimes int run; 31721002Sdyson struct buf *fbp; 3181541Srgrimes{ 31910541Sdyson struct buf *bp, *tbp; 3201541Srgrimes daddr_t bn; 32140648Sphk int i, inc, j; 3221541Srgrimes 32379224Sdillon GIANT_REQUIRED; 32479224Sdillon 32542408Seivind KASSERT(size == vp->v_mount->mnt_stat.f_iosize, 32642453Seivind ("cluster_rbuild: size %ld != filesize %ld\n", 32742453Seivind size, vp->v_mount->mnt_stat.f_iosize)); 32842453Seivind 32912767Sdyson /* 33012767Sdyson * avoid a division 33112767Sdyson */ 33212767Sdyson while ((u_quad_t) size * (lbn + run) > filesize) { 3331541Srgrimes --run; 33412767Sdyson } 33510541Sdyson 33621002Sdyson if (fbp) { 33721002Sdyson tbp = fbp; 33858345Sphk tbp->b_iocmd = BIO_READ; 33921002Sdyson } else { 34021002Sdyson tbp = getblk(vp, lbn, size, 0, 0); 34121002Sdyson if (tbp->b_flags & B_CACHE) 34221002Sdyson return tbp; 34358345Sphk tbp->b_flags |= B_ASYNC | B_RAM; 34458345Sphk tbp->b_iocmd = BIO_READ; 34521002Sdyson } 34610541Sdyson 34710541Sdyson tbp->b_blkno = blkno; 34816086Sdyson if( (tbp->b_flags & B_MALLOC) || 34916086Sdyson ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) ) 35010541Sdyson return tbp; 35110541Sdyson 35242957Sdillon bp = trypbuf(&cluster_pbuf_freecnt); 35310541Sdyson if (bp == 0) 35410541Sdyson return tbp; 35510541Sdyson 35637467Sbde bp->b_data = (char *)((vm_offset_t)bp->b_data | 35737467Sbde ((vm_offset_t)tbp->b_data & PAGE_MASK)); 35858345Sphk bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO; 35958345Sphk bp->b_iocmd = BIO_READ; 3605455Sdg bp->b_iodone = cluster_callback; 3615455Sdg bp->b_blkno = blkno; 3625455Sdg bp->b_lblkno = lbn; 36334611Sdyson bp->b_offset = tbp->b_offset; 36442453Seivind KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset")); 3655455Sdg pbgetvp(vp, bp); 3661541Srgrimes 36712404Sdyson TAILQ_INIT(&bp->b_cluster.cluster_head); 3681541Srgrimes 3695455Sdg bp->b_bcount = 0; 3705455Sdg bp->b_bufsize = 0; 3715455Sdg bp->b_npages = 0; 3725455Sdg 3731541Srgrimes inc = btodb(size); 37410541Sdyson for (bn = blkno, i = 0; i < run; ++i, bn += inc) { 3755455Sdg if (i != 0) { 37612767Sdyson if ((bp->b_npages * PAGE_SIZE) + 37751797Sphk round_page(size) > vp->v_mount->mnt_iosize_max) 37810541Sdyson break; 37910978Sdyson 38043301Sdillon if ((tbp = incore(vp, lbn + i)) != NULL) { 38148225Smckusick if (BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) 38234611Sdyson break; 38348225Smckusick BUF_UNLOCK(tbp); 38412767Sdyson 38534611Sdyson for (j = 0; j < tbp->b_npages; j++) 38634611Sdyson if (tbp->b_pages[j]->valid) 38734611Sdyson break; 38834611Sdyson 38934611Sdyson if (j != tbp->b_npages) 39034611Sdyson break; 39134611Sdyson 39234611Sdyson if (tbp->b_bcount != size) 39334611Sdyson break; 39434611Sdyson } 39534611Sdyson 3965455Sdg tbp = getblk(vp, lbn + i, size, 0, 0); 39710541Sdyson 39871230Sdillon /* 39971230Sdillon * If the buffer is already fully valid or locked 40071230Sdillon * (which could also mean that a background write is 40171230Sdillon * in progress), or the buffer is not backed by VMIO, 40271230Sdillon * stop. 40371230Sdillon */ 40471230Sdillon if ((tbp->b_flags & (B_CACHE|B_LOCKED)) || 40510541Sdyson (tbp->b_flags & B_VMIO) == 0) { 40613490Sdyson bqrelse(tbp); 4075455Sdg break; 4085455Sdg } 40910541Sdyson 41071230Sdillon for (j = 0;j < tbp->b_npages; j++) { 41134611Sdyson if (tbp->b_pages[j]->valid) 41210541Sdyson break; 41371230Sdillon } 41410541Sdyson 41510541Sdyson if (j != tbp->b_npages) { 41634611Sdyson bqrelse(tbp); 41710541Sdyson break; 41810541Sdyson } 41910541Sdyson 42021002Sdyson if ((fbp && (i == 1)) || (i == (run - 1))) 42121002Sdyson tbp->b_flags |= B_RAM; 42258345Sphk tbp->b_flags |= B_ASYNC; 42358345Sphk tbp->b_iocmd = BIO_READ; 42412767Sdyson if (tbp->b_blkno == tbp->b_lblkno) { 42510541Sdyson tbp->b_blkno = bn; 42610541Sdyson } else if (tbp->b_blkno != bn) { 42710541Sdyson brelse(tbp); 42810541Sdyson break; 42910541Sdyson } 4301541Srgrimes } 43148333Speter /* 43248333Speter * XXX fbp from caller may not be B_ASYNC, but we are going 43348333Speter * to biodone() it in cluster_callback() anyway 43448333Speter */ 43548333Speter BUF_KERNPROC(tbp); 43612404Sdyson TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head, 43712404Sdyson tbp, b_cluster.cluster_entry); 4385455Sdg for (j = 0; j < tbp->b_npages; j += 1) { 43910541Sdyson vm_page_t m; 44010541Sdyson m = tbp->b_pages[j]; 44138799Sdfr vm_page_io_start(m); 44238517Sdfr vm_object_pip_add(m->object, 1); 44310541Sdyson if ((bp->b_npages == 0) || 44412413Sdyson (bp->b_pages[bp->b_npages-1] != m)) { 44510541Sdyson bp->b_pages[bp->b_npages] = m; 44610541Sdyson bp->b_npages++; 44710541Sdyson } 44818737Sdyson if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) 44918737Sdyson tbp->b_pages[j] = bogus_page; 4501541Srgrimes } 45110541Sdyson bp->b_bcount += tbp->b_bcount; 45210541Sdyson bp->b_bufsize += tbp->b_bufsize; 4531541Srgrimes } 45418737Sdyson 45518737Sdyson for(j=0;j<bp->b_npages;j++) { 45618737Sdyson if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) == 45718737Sdyson VM_PAGE_BITS_ALL) 45818737Sdyson bp->b_pages[j] = bogus_page; 45918737Sdyson } 46020054Sdyson if (bp->b_bufsize > bp->b_kvasize) 46137559Sbde panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n", 46237559Sbde bp->b_bufsize, bp->b_kvasize); 46320054Sdyson bp->b_kvasize = bp->b_bufsize; 46418737Sdyson 46510541Sdyson pmap_qenter(trunc_page((vm_offset_t) bp->b_data), 46610541Sdyson (vm_page_t *)bp->b_pages, bp->b_npages); 4675455Sdg return (bp); 4681541Srgrimes} 4691541Srgrimes 4701541Srgrimes/* 4711541Srgrimes * Cleanup after a clustered read or write. 4721541Srgrimes * This is complicated by the fact that any of the buffers might have 4731541Srgrimes * extra memory (if there were no empty buffer headers at allocbuf time) 4741541Srgrimes * that we will need to shift around. 4751541Srgrimes */ 4761541Srgrimesvoid 4771541Srgrimescluster_callback(bp) 4781541Srgrimes struct buf *bp; 4791541Srgrimes{ 48012404Sdyson struct buf *nbp, *tbp; 4811541Srgrimes int error = 0; 4821541Srgrimes 48379224Sdillon GIANT_REQUIRED; 48479224Sdillon 4851541Srgrimes /* 4861541Srgrimes * Must propogate errors to all the components. 4871541Srgrimes */ 48858934Sphk if (bp->b_ioflags & BIO_ERROR) 4891541Srgrimes error = bp->b_error; 4901541Srgrimes 49110541Sdyson pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages); 4921541Srgrimes /* 4931541Srgrimes * Move memory from the large cluster buffer into the component 4941541Srgrimes * buffers and mark IO as done on these. 4951541Srgrimes */ 49621002Sdyson for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head); 49712404Sdyson tbp; tbp = nbp) { 49821002Sdyson nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry); 4991541Srgrimes if (error) { 50058934Sphk tbp->b_ioflags |= BIO_ERROR; 5011541Srgrimes tbp->b_error = error; 50246349Salc } else { 50346349Salc tbp->b_dirtyoff = tbp->b_dirtyend = 0; 50458934Sphk tbp->b_flags &= ~B_INVAL; 50558934Sphk tbp->b_ioflags &= ~BIO_ERROR; 50677115Sdillon /* 50777115Sdillon * XXX the bdwrite()/bqrelse() issued during 50877115Sdillon * cluster building clears B_RELBUF (see bqrelse() 50977115Sdillon * comment). If direct I/O was specified, we have 51077115Sdillon * to restore it here to allow the buffer and VM 51177115Sdillon * to be freed. 51277115Sdillon */ 51377115Sdillon if (tbp->b_flags & B_DIRECT) 51477115Sdillon tbp->b_flags |= B_RELBUF; 51546349Salc } 51659249Sphk bufdone(tbp); 5171541Srgrimes } 51842957Sdillon relpbuf(bp, &cluster_pbuf_freecnt); 5191541Srgrimes} 5201541Srgrimes 5211541Srgrimes/* 52248545Smckusick * cluster_wbuild_wb: 52348545Smckusick * 52448545Smckusick * Implement modified write build for cluster. 52548545Smckusick * 52648545Smckusick * write_behind = 0 write behind disabled 52748545Smckusick * write_behind = 1 write behind normal (default) 52848545Smckusick * write_behind = 2 write behind backed-off 52948545Smckusick */ 53048545Smckusick 53148545Smckusickstatic __inline int 53248545Smckusickcluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len) 53348545Smckusick{ 53448545Smckusick int r = 0; 53548545Smckusick 53648545Smckusick switch(write_behind) { 53748545Smckusick case 2: 53848545Smckusick if (start_lbn < len) 53948545Smckusick break; 54048545Smckusick start_lbn -= len; 54148545Smckusick /* fall through */ 54248545Smckusick case 1: 54348545Smckusick r = cluster_wbuild(vp, size, start_lbn, len); 54448545Smckusick /* fall through */ 54548545Smckusick default: 54648545Smckusick /* fall through */ 54748545Smckusick break; 54848545Smckusick } 54948545Smckusick return(r); 55048545Smckusick} 55148545Smckusick 55248545Smckusick/* 5531541Srgrimes * Do clustered write for FFS. 5541541Srgrimes * 5551541Srgrimes * Three cases: 5561541Srgrimes * 1. Write is not sequential (write asynchronously) 5571541Srgrimes * Write is sequential: 5581541Srgrimes * 2. beginning of cluster - begin cluster 5591541Srgrimes * 3. middle of a cluster - add to cluster 5601541Srgrimes * 4. end of a cluster - asynchronously write cluster 5611541Srgrimes */ 5621541Srgrimesvoid 56358909Sdilloncluster_write(bp, filesize, seqcount) 5645455Sdg struct buf *bp; 5651541Srgrimes u_quad_t filesize; 56658909Sdillon int seqcount; 5671541Srgrimes{ 5685455Sdg struct vnode *vp; 5695455Sdg daddr_t lbn; 5705455Sdg int maxclen, cursize; 5715455Sdg int lblocksize; 57212404Sdyson int async; 5731541Srgrimes 5745455Sdg vp = bp->b_vp; 57532286Sdyson if (vp->v_type == VREG) { 57632286Sdyson async = vp->v_mount->mnt_flag & MNT_ASYNC; 57732286Sdyson lblocksize = vp->v_mount->mnt_stat.f_iosize; 57832286Sdyson } else { 57932286Sdyson async = 0; 58032286Sdyson lblocksize = bp->b_bufsize; 58132286Sdyson } 5825455Sdg lbn = bp->b_lblkno; 58342408Seivind KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset")); 58434694Sdyson 5851541Srgrimes /* Initialize vnode to beginning of file. */ 5861541Srgrimes if (lbn == 0) 5871541Srgrimes vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0; 5881541Srgrimes 5895455Sdg if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 || 5905455Sdg (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) { 59151797Sphk maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1; 5921541Srgrimes if (vp->v_clen != 0) { 5931541Srgrimes /* 5941541Srgrimes * Next block is not sequential. 5958876Srgrimes * 5961541Srgrimes * If we are not writing at end of file, the process 5975455Sdg * seeked to another point in the file since its last 5985455Sdg * write, or we have reached our maximum cluster size, 5995455Sdg * then push the previous cluster. Otherwise try 6005455Sdg * reallocating to make it sequential. 60158909Sdillon * 60258909Sdillon * Change to algorithm: only push previous cluster if 60358909Sdillon * it was sequential from the point of view of the 60458909Sdillon * seqcount heuristic, otherwise leave the buffer 60558909Sdillon * intact so we can potentially optimize the I/O 60658909Sdillon * later on in the buf_daemon or update daemon 60758909Sdillon * flush. 6081541Srgrimes */ 6091541Srgrimes cursize = vp->v_lastw - vp->v_cstart + 1; 61034611Sdyson if (((u_quad_t) bp->b_offset + lblocksize) != filesize || 61110541Sdyson lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) { 61258909Sdillon if (!async && seqcount > 0) { 61348677Smckusick cluster_wbuild_wb(vp, lblocksize, 61412404Sdyson vp->v_cstart, cursize); 61558909Sdillon } 61610541Sdyson } else { 61710541Sdyson struct buf **bpp, **endbp; 61810541Sdyson struct cluster_save *buflist; 61910541Sdyson 62010541Sdyson buflist = cluster_collectbufs(vp, bp); 62110541Sdyson endbp = &buflist->bs_children 62210541Sdyson [buflist->bs_nchildren - 1]; 62310541Sdyson if (VOP_REALLOCBLKS(vp, buflist)) { 62410541Sdyson /* 62558909Sdillon * Failed, push the previous cluster 62658909Sdillon * if *really* writing sequentially 62758909Sdillon * in the logical file (seqcount > 1), 62858909Sdillon * otherwise delay it in the hopes that 62958909Sdillon * the low level disk driver can 63058909Sdillon * optimize the write ordering. 63110541Sdyson */ 63210541Sdyson for (bpp = buflist->bs_children; 63310541Sdyson bpp < endbp; bpp++) 63410541Sdyson brelse(*bpp); 63510541Sdyson free(buflist, M_SEGMENT); 63658909Sdillon if (seqcount > 1) { 63758909Sdillon cluster_wbuild_wb(vp, 63858909Sdillon lblocksize, vp->v_cstart, 63958909Sdillon cursize); 64058909Sdillon } 64110541Sdyson } else { 64210541Sdyson /* 64310541Sdyson * Succeeded, keep building cluster. 64410541Sdyson */ 64510541Sdyson for (bpp = buflist->bs_children; 64610541Sdyson bpp <= endbp; bpp++) 64710541Sdyson bdwrite(*bpp); 64810541Sdyson free(buflist, M_SEGMENT); 64910541Sdyson vp->v_lastw = lbn; 65010541Sdyson vp->v_lasta = bp->b_blkno; 65110541Sdyson return; 65210541Sdyson } 65310541Sdyson } 6541541Srgrimes } 6551541Srgrimes /* 6565455Sdg * Consider beginning a cluster. If at end of file, make 6575455Sdg * cluster as large as possible, otherwise find size of 6585455Sdg * existing cluster. 6591541Srgrimes */ 66032286Sdyson if ((vp->v_type == VREG) && 66134611Sdyson ((u_quad_t) bp->b_offset + lblocksize) != filesize && 6627613Sdg (bp->b_blkno == bp->b_lblkno) && 66310551Sdyson (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) || 66410541Sdyson bp->b_blkno == -1)) { 6651541Srgrimes bawrite(bp); 6661541Srgrimes vp->v_clen = 0; 6671541Srgrimes vp->v_lasta = bp->b_blkno; 6681541Srgrimes vp->v_cstart = lbn + 1; 6691541Srgrimes vp->v_lastw = lbn; 6701541Srgrimes return; 6711541Srgrimes } 6725455Sdg vp->v_clen = maxclen; 67312404Sdyson if (!async && maxclen == 0) { /* I/O not contiguous */ 6741541Srgrimes vp->v_cstart = lbn + 1; 67513490Sdyson bawrite(bp); 6765455Sdg } else { /* Wait for rest of cluster */ 6771541Srgrimes vp->v_cstart = lbn; 6785455Sdg bdwrite(bp); 6791541Srgrimes } 6801541Srgrimes } else if (lbn == vp->v_cstart + vp->v_clen) { 6811541Srgrimes /* 68258909Sdillon * At end of cluster, write it out if seqcount tells us we 68358909Sdillon * are operating sequentially, otherwise let the buf or 68458909Sdillon * update daemon handle it. 6851541Srgrimes */ 68612404Sdyson bdwrite(bp); 68758909Sdillon if (seqcount > 1) 68858909Sdillon cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, vp->v_clen + 1); 6891541Srgrimes vp->v_clen = 0; 6901541Srgrimes vp->v_cstart = lbn + 1; 69168885Sdillon } else if (vm_page_count_severe()) { 69268885Sdillon /* 69368885Sdillon * We are low on memory, get it going NOW 69468885Sdillon */ 69568885Sdillon bawrite(bp); 69658909Sdillon } else { 6971541Srgrimes /* 6985455Sdg * In the middle of a cluster, so just delay the I/O for now. 6991541Srgrimes */ 7001541Srgrimes bdwrite(bp); 70158909Sdillon } 7021541Srgrimes vp->v_lastw = lbn; 7031541Srgrimes vp->v_lasta = bp->b_blkno; 7041541Srgrimes} 7051541Srgrimes 7061541Srgrimes 7071541Srgrimes/* 7081541Srgrimes * This is an awful lot like cluster_rbuild...wish they could be combined. 7091541Srgrimes * The last lbn argument is the current block on which I/O is being 7101541Srgrimes * performed. Check to see that it doesn't fall in the middle of 7111541Srgrimes * the current block (if last_bp == NULL). 7121541Srgrimes */ 71312767Sdysonint 71412404Sdysoncluster_wbuild(vp, size, start_lbn, len) 7151541Srgrimes struct vnode *vp; 7161541Srgrimes long size; 7171541Srgrimes daddr_t start_lbn; 7181541Srgrimes int len; 7191541Srgrimes{ 72012404Sdyson struct buf *bp, *tbp; 7215455Sdg int i, j, s; 72212767Sdyson int totalwritten = 0; 72312404Sdyson int dbsize = btodb(size); 72435595Sbde 72579224Sdillon GIANT_REQUIRED; 72679224Sdillon 72712767Sdyson while (len > 0) { 72812767Sdyson s = splbio(); 72971230Sdillon /* 73071230Sdillon * If the buffer is not delayed-write (i.e. dirty), or it 73171230Sdillon * is delayed-write but either locked or inval, it cannot 73272080Sasmodai * partake in the clustered write. 73371230Sdillon */ 73432286Sdyson if (((tbp = gbincore(vp, start_lbn)) == NULL) || 73571230Sdillon ((tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI) || 73648225Smckusick BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) { 73712767Sdyson ++start_lbn; 73812767Sdyson --len; 73912767Sdyson splx(s); 74012767Sdyson continue; 74112767Sdyson } 74212767Sdyson bremfree(tbp); 74312767Sdyson tbp->b_flags &= ~B_DONE; 74412767Sdyson splx(s); 7451541Srgrimes 74647967Sjulian /* 74747967Sjulian * Extra memory in the buffer, punt on this buffer. 74847967Sjulian * XXX we could handle this in most cases, but we would 74947967Sjulian * have to push the extra memory down to after our max 75047967Sjulian * possible cluster size and then potentially pull it back 75147967Sjulian * up if the cluster was terminated prematurely--too much 75247967Sjulian * hassle. 75347967Sjulian */ 75468868Stegge if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) != 75568868Stegge (B_CLUSTEROK | B_VMIO)) || 75634630Sjulian (tbp->b_bcount != tbp->b_bufsize) || 75734630Sjulian (tbp->b_bcount != size) || 75834630Sjulian (len == 1) || 75947948Sdg ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) { 76012767Sdyson totalwritten += tbp->b_bufsize; 76112767Sdyson bawrite(tbp); 76212767Sdyson ++start_lbn; 76312767Sdyson --len; 76412767Sdyson continue; 76512767Sdyson } 76612404Sdyson 76734630Sjulian /* 76834630Sjulian * We got a pbuf to make the cluster in. 76934630Sjulian * so initialise it. 77034630Sjulian */ 77112767Sdyson TAILQ_INIT(&bp->b_cluster.cluster_head); 77212767Sdyson bp->b_bcount = 0; 77375580Sphk bp->b_magic = tbp->b_magic; 77475580Sphk bp->b_op = tbp->b_op; 77512767Sdyson bp->b_bufsize = 0; 77612767Sdyson bp->b_npages = 0; 77717304Sdyson if (tbp->b_wcred != NOCRED) { 77817304Sdyson bp->b_wcred = tbp->b_wcred; 77917304Sdyson crhold(bp->b_wcred); 78017304Sdyson } 7811541Srgrimes 78212767Sdyson bp->b_blkno = tbp->b_blkno; 78312767Sdyson bp->b_lblkno = tbp->b_lblkno; 78434611Sdyson bp->b_offset = tbp->b_offset; 78537467Sbde bp->b_data = (char *)((vm_offset_t)bp->b_data | 78637467Sbde ((vm_offset_t)tbp->b_data & PAGE_MASK)); 78758345Sphk bp->b_flags |= B_CLUSTER | 78834694Sdyson (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT)); 78912767Sdyson bp->b_iodone = cluster_callback; 79012767Sdyson pbgetvp(vp, bp); 79134630Sjulian /* 79234630Sjulian * From this location in the file, scan forward to see 79334630Sjulian * if there are buffers with adjacent data that need to 79434630Sjulian * be written as well. 79534630Sjulian */ 79612767Sdyson for (i = 0; i < len; ++i, ++start_lbn) { 79734630Sjulian if (i != 0) { /* If not the first buffer */ 79812767Sdyson s = splbio(); 79934630Sjulian /* 80034630Sjulian * If the adjacent data is not even in core it 80134630Sjulian * can't need to be written. 80234630Sjulian */ 80312767Sdyson if ((tbp = gbincore(vp, start_lbn)) == NULL) { 80412767Sdyson splx(s); 80512767Sdyson break; 80612767Sdyson } 8071541Srgrimes 80834630Sjulian /* 80934630Sjulian * If it IS in core, but has different 81071230Sdillon * characteristics, or is locked (which 81171230Sdillon * means it could be undergoing a background 81271230Sdillon * I/O or be in a weird state), then don't 81371230Sdillon * cluster with it. 81434630Sjulian */ 81548225Smckusick if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK | 81648225Smckusick B_INVAL | B_DELWRI | B_NEEDCOMMIT)) 81734694Sdyson != (B_DELWRI | B_CLUSTEROK | 81848225Smckusick (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) || 81971230Sdillon (tbp->b_flags & B_LOCKED) || 82048225Smckusick tbp->b_wcred != bp->b_wcred || 82148225Smckusick BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) { 82212767Sdyson splx(s); 82312767Sdyson break; 82412767Sdyson } 82512767Sdyson 82634630Sjulian /* 82734630Sjulian * Check that the combined cluster 82834630Sjulian * would make sense with regard to pages 82934630Sjulian * and would not be too large 83034630Sjulian */ 83112767Sdyson if ((tbp->b_bcount != size) || 83234630Sjulian ((bp->b_blkno + (dbsize * i)) != 83334694Sdyson tbp->b_blkno) || 83434630Sjulian ((tbp->b_npages + bp->b_npages) > 83551797Sphk (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) { 83648225Smckusick BUF_UNLOCK(tbp); 83712767Sdyson splx(s); 83812767Sdyson break; 83912767Sdyson } 84034630Sjulian /* 84134630Sjulian * Ok, it's passed all the tests, 84234630Sjulian * so remove it from the free list 84334630Sjulian * and mark it busy. We will use it. 84434630Sjulian */ 84512767Sdyson bremfree(tbp); 84612767Sdyson tbp->b_flags &= ~B_DONE; 84712404Sdyson splx(s); 84834630Sjulian } /* end of code for non-first buffers only */ 84934266Sjulian /* check for latent dependencies to be handled */ 85061724Sphk if ((LIST_FIRST(&tbp->b_dep)) != NULL) 85161724Sphk buf_start(tbp); 85234630Sjulian /* 85334630Sjulian * If the IO is via the VM then we do some 85434630Sjulian * special VM hackery. (yuck) 85534630Sjulian */ 85613490Sdyson if (tbp->b_flags & B_VMIO) { 85732937Sdyson vm_page_t m; 85832937Sdyson 85934630Sjulian if (i != 0) { /* if not first buffer */ 86032937Sdyson for (j = 0; j < tbp->b_npages; j += 1) { 86132937Sdyson m = tbp->b_pages[j]; 86250701Stegge if (m->flags & PG_BUSY) { 86350701Stegge bqrelse(tbp); 86432937Sdyson goto finishcluster; 86550701Stegge } 86632937Sdyson } 86732937Sdyson } 86832937Sdyson 86913490Sdyson for (j = 0; j < tbp->b_npages; j += 1) { 87013490Sdyson m = tbp->b_pages[j]; 87138799Sdfr vm_page_io_start(m); 87238517Sdfr vm_object_pip_add(m->object, 1); 87313490Sdyson if ((bp->b_npages == 0) || 87434630Sjulian (bp->b_pages[bp->b_npages - 1] != m)) { 87513490Sdyson bp->b_pages[bp->b_npages] = m; 87613490Sdyson bp->b_npages++; 87713490Sdyson } 87812767Sdyson } 87912767Sdyson } 88012767Sdyson bp->b_bcount += size; 88112767Sdyson bp->b_bufsize += size; 8821541Srgrimes 88338299Sdfr s = splbio(); 88444679Sjulian bundirty(tbp); 88558934Sphk tbp->b_flags &= ~B_DONE; 88658934Sphk tbp->b_ioflags &= ~BIO_ERROR; 88712767Sdyson tbp->b_flags |= B_ASYNC; 88858345Sphk tbp->b_iocmd = BIO_WRITE; 88912767Sdyson reassignbuf(tbp, tbp->b_vp); /* put on clean list */ 89012767Sdyson ++tbp->b_vp->v_numoutput; 89138299Sdfr splx(s); 89248333Speter BUF_KERNPROC(tbp); 89312767Sdyson TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head, 89412767Sdyson tbp, b_cluster.cluster_entry); 8951541Srgrimes } 89632937Sdyson finishcluster: 89712767Sdyson pmap_qenter(trunc_page((vm_offset_t) bp->b_data), 89812767Sdyson (vm_page_t *) bp->b_pages, bp->b_npages); 89920054Sdyson if (bp->b_bufsize > bp->b_kvasize) 90037559Sbde panic( 90137559Sbde "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n", 90237559Sbde bp->b_bufsize, bp->b_kvasize); 90320054Sdyson bp->b_kvasize = bp->b_bufsize; 90412767Sdyson totalwritten += bp->b_bufsize; 90517304Sdyson bp->b_dirtyoff = 0; 90617304Sdyson bp->b_dirtyend = bp->b_bufsize; 90712767Sdyson bawrite(bp); 9081541Srgrimes 90912767Sdyson len -= i; 9101541Srgrimes } 91112767Sdyson return totalwritten; 9121541Srgrimes} 9131541Srgrimes 9141541Srgrimes/* 9151541Srgrimes * Collect together all the buffers in a cluster. 9161541Srgrimes * Plus add one additional buffer. 9171541Srgrimes */ 91812973Sbdestatic struct cluster_save * 9191541Srgrimescluster_collectbufs(vp, last_bp) 9201541Srgrimes struct vnode *vp; 9211541Srgrimes struct buf *last_bp; 9221541Srgrimes{ 9231541Srgrimes struct cluster_save *buflist; 92441205Smckusick struct buf *bp; 9255455Sdg daddr_t lbn; 9261541Srgrimes int i, len; 9271541Srgrimes 9281541Srgrimes len = vp->v_lastw - vp->v_cstart + 1; 9291541Srgrimes buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist), 9301541Srgrimes M_SEGMENT, M_WAITOK); 9311541Srgrimes buflist->bs_nchildren = 0; 9325455Sdg buflist->bs_children = (struct buf **) (buflist + 1); 93341205Smckusick for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) { 93441205Smckusick (void) bread(vp, lbn, last_bp->b_bcount, NOCRED, &bp); 93541205Smckusick buflist->bs_children[i] = bp; 93641205Smckusick if (bp->b_blkno == bp->b_lblkno) 93741205Smckusick VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, 93841205Smckusick NULL, NULL); 93941205Smckusick } 94041529Smckusick buflist->bs_children[i] = bp = last_bp; 94141529Smckusick if (bp->b_blkno == bp->b_lblkno) 94241529Smckusick VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, 94341529Smckusick NULL, NULL); 9441541Srgrimes buflist->bs_nchildren = i + 1; 9451541Srgrimes return (buflist); 9461541Srgrimes} 947