The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_cluster.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 630ff29172e1219d704d67365ceaf6ea


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.