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/fs/ext2fs/ext2_bmap.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) 1989, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * (c) UNIX System Laboratories, Inc.
    5  * All or some portions of this file are derived from material licensed
    6  * to the University of California by American Telephone and Telegraph
    7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8  * the permission of UNIX System Laboratories, Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)ufs_bmap.c  8.7 (Berkeley) 3/21/95
   35  * $FreeBSD$
   36  */
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/bio.h>
   41 #include <sys/buf.h>
   42 #include <sys/proc.h>
   43 #include <sys/vnode.h>
   44 #include <sys/mount.h>
   45 #include <sys/racct.h>
   46 #include <sys/resourcevar.h>
   47 #include <sys/stat.h>
   48 
   49 #include <fs/ext2fs/inode.h>
   50 #include <fs/ext2fs/fs.h>
   51 #include <fs/ext2fs/ext2fs.h>
   52 #include <fs/ext2fs/ext2_dinode.h>
   53 #include <fs/ext2fs/ext2_extern.h>
   54 #include <fs/ext2fs/ext2_mount.h>
   55 
   56 static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
   57 
   58 /*
   59  * Bmap converts the logical block number of a file to its physical block
   60  * number on the disk. The conversion is done by using the logical block
   61  * number to index into the array of block pointers described by the dinode.
   62  */
   63 int
   64 ext2_bmap(struct vop_bmap_args *ap)
   65 {
   66         daddr_t blkno;
   67         int error;
   68 
   69         /*
   70          * Check for underlying vnode requests and ensure that logical
   71          * to physical mapping is requested.
   72          */
   73         if (ap->a_bop != NULL)
   74                 *ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
   75         if (ap->a_bnp == NULL)
   76                 return (0);
   77 
   78         if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
   79                 error = ext4_bmapext(ap->a_vp, ap->a_bn, &blkno,
   80                     ap->a_runp, ap->a_runb);
   81         else
   82                 error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
   83                     ap->a_runp, ap->a_runb);
   84         *ap->a_bnp = blkno;
   85         return (error);
   86 }
   87 
   88 /*
   89  * Convert the logical block number of a file to its physical block number
   90  * on the disk within ext4 extents.
   91  */
   92 static int
   93 ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
   94 {
   95         struct inode *ip;
   96         struct m_ext2fs *fs;
   97         struct ext4_extent *ep;
   98         struct ext4_extent_path path = {.ep_bp = NULL};
   99         daddr_t lbn;
  100         int error;
  101 
  102         ip = VTOI(vp);
  103         fs = ip->i_e2fs;
  104         lbn = bn;
  105 
  106         if (runp != NULL)
  107                 *runp = 0;
  108         if (runb != NULL)
  109                 *runb = 0;
  110         error = 0;
  111 
  112         ext4_ext_find_extent(fs, ip, lbn, &path);
  113         if (path.ep_is_sparse) {
  114                 *bnp = -1;
  115                 if (runp != NULL)
  116                         *runp = path.ep_sparse_ext.e_len -
  117                             (lbn - path.ep_sparse_ext.e_blk) - 1;
  118                 if (runb != NULL)
  119                         *runb = lbn - path.ep_sparse_ext.e_blk;
  120         } else {
  121                 if (path.ep_ext == NULL) {
  122                         error = EIO;
  123                         goto out;
  124                 }
  125                 ep = path.ep_ext;
  126                 *bnp = fsbtodb(fs, lbn - ep->e_blk +
  127                     (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
  128 
  129                 if (*bnp == 0)
  130                         *bnp = -1;
  131 
  132                 if (runp != NULL)
  133                         *runp = ep->e_len - (lbn - ep->e_blk) - 1;
  134                 if (runb != NULL)
  135                         *runb = lbn - ep->e_blk;
  136         }
  137 
  138 out:
  139         if (path.ep_bp != NULL)
  140                 brelse(path.ep_bp);
  141 
  142         return (error);
  143 }
  144 
  145 /*
  146  * Indirect blocks are now on the vnode for the file.  They are given negative
  147  * logical block numbers.  Indirect blocks are addressed by the negative
  148  * address of the first data block to which they point.  Double indirect blocks
  149  * are addressed by one less than the address of the first indirect block to
  150  * which they point.  Triple indirect blocks are addressed by one less than
  151  * the address of the first double indirect block to which they point.
  152  *
  153  * ext2_bmaparray does the bmap conversion, and if requested returns the
  154  * array of logical blocks which must be traversed to get to a block.
  155  * Each entry contains the offset into that block that gets you to the
  156  * next block and the disk address of the block (if it is assigned).
  157  */
  158 
  159 int
  160 ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
  161 {
  162         struct inode *ip;
  163         struct buf *bp;
  164         struct ext2mount *ump;
  165         struct mount *mp;
  166         struct indir a[NIADDR + 1], *ap;
  167         daddr_t daddr;
  168         e2fs_lbn_t metalbn;
  169         int error, num, maxrun = 0, bsize;
  170         int *nump;
  171 
  172         ap = NULL;
  173         ip = VTOI(vp);
  174         mp = vp->v_mount;
  175         ump = VFSTOEXT2(mp);
  176 
  177         bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
  178 
  179         if (runp) {
  180                 maxrun = mp->mnt_iosize_max / bsize - 1;
  181                 *runp = 0;
  182         }
  183         if (runb)
  184                 *runb = 0;
  185 
  186 
  187         ap = a;
  188         nump = &num;
  189         error = ext2_getlbns(vp, bn, ap, nump);
  190         if (error)
  191                 return (error);
  192 
  193         num = *nump;
  194         if (num == 0) {
  195                 *bnp = blkptrtodb(ump, ip->i_db[bn]);
  196                 if (*bnp == 0) {
  197                         *bnp = -1;
  198                 } else if (runp) {
  199                         daddr_t bnb = bn;
  200 
  201                         for (++bn; bn < NDADDR && *runp < maxrun &&
  202                             is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
  203                             ++bn, ++*runp);
  204                         bn = bnb;
  205                         if (runb && (bn > 0)) {
  206                                 for (--bn; (bn >= 0) && (*runb < maxrun) &&
  207                                         is_sequential(ump, ip->i_db[bn],
  208                                                 ip->i_db[bn + 1]);
  209                                                 --bn, ++*runb);
  210                         }
  211                 }
  212                 return (0);
  213         }
  214 
  215         /* Get disk address out of indirect block array */
  216         daddr = ip->i_ib[ap->in_off];
  217 
  218         for (bp = NULL, ++ap; --num; ++ap) {
  219                 /*
  220                  * Exit the loop if there is no disk address assigned yet and
  221                  * the indirect block isn't in the cache, or if we were
  222                  * looking for an indirect block and we've found it.
  223                  */
  224 
  225                 metalbn = ap->in_lbn;
  226                 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
  227                         break;
  228                 /*
  229                  * If we get here, we've either got the block in the cache
  230                  * or we have a disk address for it, go fetch it.
  231                  */
  232                 if (bp)
  233                         bqrelse(bp);
  234 
  235                 bp = getblk(vp, metalbn, bsize, 0, 0, 0);
  236                 if ((bp->b_flags & B_CACHE) == 0) {
  237 #ifdef INVARIANTS
  238                         if (!daddr)
  239                                 panic("ext2_bmaparray: indirect block not in cache");
  240 #endif
  241                         bp->b_blkno = blkptrtodb(ump, daddr);
  242                         bp->b_iocmd = BIO_READ;
  243                         bp->b_flags &= ~B_INVAL;
  244                         bp->b_ioflags &= ~BIO_ERROR;
  245                         vfs_busy_pages(bp, 0);
  246                         bp->b_iooffset = dbtob(bp->b_blkno);
  247                         bstrategy(bp);
  248 #ifdef RACCT
  249                         if (racct_enable) {
  250                                 PROC_LOCK(curproc);
  251                                 racct_add_buf(curproc, bp, 0);
  252                                 PROC_UNLOCK(curproc);
  253                         }
  254 #endif
  255                         curthread->td_ru.ru_inblock++;
  256                         error = bufwait(bp);
  257                         if (error) {
  258                                 brelse(bp);
  259                                 return (error);
  260                         }
  261                 }
  262 
  263                 daddr = ((e2fs_daddr_t *)bp->b_data)[ap->in_off];
  264                 if (num == 1 && daddr && runp) {
  265                         for (bn = ap->in_off + 1;
  266                             bn < MNINDIR(ump) && *runp < maxrun &&
  267                             is_sequential(ump,
  268                             ((e2fs_daddr_t *)bp->b_data)[bn - 1],
  269                             ((e2fs_daddr_t *)bp->b_data)[bn]);
  270                             ++bn, ++*runp);
  271                         bn = ap->in_off;
  272                         if (runb && bn) {
  273                                 for (--bn; bn >= 0 && *runb < maxrun &&
  274                                         is_sequential(ump,
  275                                         ((e2fs_daddr_t *)bp->b_data)[bn],
  276                                         ((e2fs_daddr_t *)bp->b_data)[bn + 1]);
  277                                         --bn, ++*runb);
  278                         }
  279                 }
  280         }
  281         if (bp)
  282                 bqrelse(bp);
  283 
  284         /*
  285          * Since this is FFS independent code, we are out of scope for the
  286          * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
  287          * will fall in the range 1..um_seqinc, so we use that test and
  288          * return a request for a zeroed out buffer if attempts are made
  289          * to read a BLK_NOCOPY or BLK_SNAP block.
  290          */
  291         if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc) {
  292                 *bnp = -1;
  293                 return (0);
  294         }
  295         *bnp = blkptrtodb(ump, daddr);
  296         if (*bnp == 0) {
  297                 *bnp = -1;
  298         }
  299         return (0);
  300 }
  301 
  302 /*
  303  * Create an array of logical block number/offset pairs which represent the
  304  * path of indirect blocks required to access a data block.  The first "pair"
  305  * contains the logical block number of the appropriate single, double or
  306  * triple indirect block and the offset into the inode indirect block array.
  307  * Note, the logical block number of the inode single/double/triple indirect
  308  * block appears twice in the array, once with the offset into the i_ib and
  309  * once with the offset into the page itself.
  310  */
  311 int
  312 ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
  313 {
  314         long blockcnt;
  315         e2fs_lbn_t metalbn, realbn;
  316         struct ext2mount *ump;
  317         int i, numlevels, off;
  318         int64_t qblockcnt;
  319 
  320         ump = VFSTOEXT2(vp->v_mount);
  321         if (nump)
  322                 *nump = 0;
  323         numlevels = 0;
  324         realbn = bn;
  325         if ((long)bn < 0)
  326                 bn = -(long)bn;
  327 
  328         /* The first NDADDR blocks are direct blocks. */
  329         if (bn < NDADDR)
  330                 return (0);
  331 
  332         /*
  333          * Determine the number of levels of indirection.  After this loop
  334          * is done, blockcnt indicates the number of data blocks possible
  335          * at the previous level of indirection, and NIADDR - i is the number
  336          * of levels of indirection needed to locate the requested block.
  337          */
  338         for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
  339                 if (i == 0)
  340                         return (EFBIG);
  341                 /*
  342                  * Use int64_t's here to avoid overflow for triple indirect
  343                  * blocks when longs have 32 bits and the block size is more
  344                  * than 4K.
  345                  */
  346                 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
  347                 if (bn < qblockcnt)
  348                         break;
  349                 blockcnt = qblockcnt;
  350         }
  351 
  352         /* Calculate the address of the first meta-block. */
  353         if (realbn >= 0)
  354                 metalbn = -(realbn - bn + NIADDR - i);
  355         else
  356                 metalbn = -(-realbn - bn + NIADDR - i);
  357 
  358         /*
  359          * At each iteration, off is the offset into the bap array which is
  360          * an array of disk addresses at the current level of indirection.
  361          * The logical block number and the offset in that block are stored
  362          * into the argument array.
  363          */
  364         ap->in_lbn = metalbn;
  365         ap->in_off = off = NIADDR - i;
  366         ap++;
  367         for (++numlevels; i <= NIADDR; i++) {
  368                 /* If searching for a meta-data block, quit when found. */
  369                 if (metalbn == realbn)
  370                         break;
  371 
  372                 off = (bn / blockcnt) % MNINDIR(ump);
  373 
  374                 ++numlevels;
  375                 ap->in_lbn = metalbn;
  376                 ap->in_off = off;
  377                 ++ap;
  378 
  379                 metalbn -= -1 + off * blockcnt;
  380                 blockcnt /= MNINDIR(ump);
  381         }
  382         if (nump)
  383                 *nump = numlevels;
  384         return (0);
  385 }

Cache object: 698ac58136fe0452d2ec1ae24e8da78f


[ 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.