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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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: releng/9.2/sys/fs/ext2fs/ext2_bmap.c 252234 2013-06-26 04:34:16Z pfg $
   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/resourcevar.h>
   46 #include <sys/stat.h>
   47 
   48 #include <fs/ext2fs/inode.h>
   49 #include <fs/ext2fs/ext2fs.h>
   50 #include <fs/ext2fs/ext2_extern.h>
   51 #include <fs/ext2fs/ext2_mount.h>
   52 
   53 /*
   54  * Bmap converts the logical block number of a file to its physical block
   55  * number on the disk. The conversion is done by using the logical block
   56  * number to index into the array of block pointers described by the dinode.
   57  */
   58 int
   59 ext2_bmap(struct vop_bmap_args *ap)
   60 {
   61         int32_t blkno;
   62         int error;
   63 
   64         /*
   65          * Check for underlying vnode requests and ensure that logical
   66          * to physical mapping is requested.
   67          */
   68         if (ap->a_bop != NULL)
   69                 *ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
   70         if (ap->a_bnp == NULL)
   71                 return (0);
   72 
   73         error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
   74             ap->a_runp, ap->a_runb);
   75         *ap->a_bnp = blkno;
   76         return (error);
   77 }
   78 
   79 /*
   80  * Indirect blocks are now on the vnode for the file.  They are given negative
   81  * logical block numbers.  Indirect blocks are addressed by the negative
   82  * address of the first data block to which they point.  Double indirect blocks
   83  * are addressed by one less than the address of the first indirect block to
   84  * which they point.  Triple indirect blocks are addressed by one less than
   85  * the address of the first double indirect block to which they point.
   86  *
   87  * ext2_bmaparray does the bmap conversion, and if requested returns the
   88  * array of logical blocks which must be traversed to get to a block.
   89  * Each entry contains the offset into that block that gets you to the
   90  * next block and the disk address of the block (if it is assigned).
   91  */
   92 
   93 int
   94 ext2_bmaparray(struct vnode *vp, int32_t bn, int32_t *bnp, int *runp, int *runb)
   95 {
   96         struct inode *ip;
   97         struct buf *bp;
   98         struct ext2mount *ump;
   99         struct mount *mp;
  100         struct vnode *devvp;
  101         struct indir a[NIADDR+1], *ap;
  102         daddr_t daddr;
  103         e2fs_lbn_t metalbn;
  104         int error, num, maxrun = 0, bsize;
  105         int *nump;
  106 
  107         ap = NULL;
  108         ip = VTOI(vp);
  109         mp = vp->v_mount;
  110         ump = VFSTOEXT2(mp);
  111         devvp = ump->um_devvp;
  112 
  113         bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
  114 
  115         if (runp) {
  116                 maxrun = mp->mnt_iosize_max / bsize - 1;
  117                 *runp = 0;
  118         }
  119 
  120         if (runb) {
  121                 *runb = 0;
  122         }
  123 
  124 
  125         ap = a;
  126         nump = &num;
  127         error = ext2_getlbns(vp, bn, ap, nump);
  128         if (error)
  129                 return (error);
  130 
  131         num = *nump;
  132         if (num == 0) {
  133                 *bnp = blkptrtodb(ump, ip->i_db[bn]);
  134                 if (*bnp == 0) {
  135                         *bnp = -1;
  136                 } else if (runp) {
  137                         int32_t bnb = bn;
  138                         for (++bn; bn < NDADDR && *runp < maxrun &&
  139                             is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
  140                             ++bn, ++*runp);
  141                         bn = bnb;
  142                         if (runb && (bn > 0)) {
  143                                 for (--bn; (bn >= 0) && (*runb < maxrun) &&
  144                                         is_sequential(ump, ip->i_db[bn],
  145                                                 ip->i_db[bn+1]);
  146                                                 --bn, ++*runb);
  147                         }
  148                 }
  149                 return (0);
  150         }
  151 
  152 
  153         /* Get disk address out of indirect block array */
  154         daddr = ip->i_ib[ap->in_off];
  155 
  156         for (bp = NULL, ++ap; --num; ++ap) {
  157                 /*
  158                  * Exit the loop if there is no disk address assigned yet and
  159                  * the indirect block isn't in the cache, or if we were
  160                  * looking for an indirect block and we've found it.
  161                  */
  162 
  163                 metalbn = ap->in_lbn;
  164                 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
  165                         break;
  166                 /*
  167                  * If we get here, we've either got the block in the cache
  168                  * or we have a disk address for it, go fetch it.
  169                  */
  170                 if (bp)
  171                         bqrelse(bp);
  172 
  173                 bp = getblk(vp, metalbn, bsize, 0, 0, 0);
  174                 if ((bp->b_flags & B_CACHE) == 0) {
  175 #ifdef INVARIANTS
  176                         if (!daddr)
  177                                 panic("ext2_bmaparray: indirect block not in cache");
  178 #endif
  179                         bp->b_blkno = blkptrtodb(ump, daddr);
  180                         bp->b_iocmd = BIO_READ;
  181                         bp->b_flags &= ~B_INVAL;
  182                         bp->b_ioflags &= ~BIO_ERROR;
  183                         vfs_busy_pages(bp, 0);
  184                         bp->b_iooffset = dbtob(bp->b_blkno);
  185                         bstrategy(bp);
  186                         curthread->td_ru.ru_inblock++;
  187                         error = bufwait(bp);
  188                         if (error) {
  189                                 brelse(bp);
  190                                 return (error);
  191                         }
  192                 }
  193 
  194                 daddr = ((int32_t *)bp->b_data)[ap->in_off];
  195                 if (num == 1 && daddr && runp) {
  196                         for (bn = ap->in_off + 1;
  197                             bn < MNINDIR(ump) && *runp < maxrun &&
  198                             is_sequential(ump,
  199                             ((int32_t *)bp->b_data)[bn - 1],
  200                             ((int32_t *)bp->b_data)[bn]);
  201                             ++bn, ++*runp);
  202                         bn = ap->in_off;
  203                         if (runb && bn) {
  204                                 for (--bn; bn >= 0 && *runb < maxrun &&
  205                                         is_sequential(ump, ((int32_t *)bp->b_data)[bn],
  206                                             ((int32_t *)bp->b_data)[bn+1]);
  207                                         --bn, ++*runb);
  208                         }
  209                 }
  210         }
  211         if (bp)
  212                 bqrelse(bp);
  213 
  214         /*
  215          * Since this is FFS independent code, we are out of scope for the
  216          * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
  217          * will fall in the range 1..um_seqinc, so we use that test and
  218          * return a request for a zeroed out buffer if attempts are made
  219          * to read a BLK_NOCOPY or BLK_SNAP block.
  220          */
  221         if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){
  222                 *bnp = -1;
  223                 return (0);
  224         }
  225         *bnp = blkptrtodb(ump, daddr);
  226         if (*bnp == 0) {
  227                 *bnp = -1;
  228         }
  229         return (0);
  230 }
  231 
  232 /*
  233  * Create an array of logical block number/offset pairs which represent the
  234  * path of indirect blocks required to access a data block.  The first "pair"
  235  * contains the logical block number of the appropriate single, double or
  236  * triple indirect block and the offset into the inode indirect block array.
  237  * Note, the logical block number of the inode single/double/triple indirect
  238  * block appears twice in the array, once with the offset into the i_ib and
  239  * once with the offset into the page itself.
  240  */
  241 int
  242 ext2_getlbns(struct vnode *vp, int32_t bn, struct indir *ap, int *nump)
  243 {
  244         long blockcnt;
  245         e2fs_lbn_t metalbn, realbn;
  246         struct ext2mount *ump;
  247         int i, numlevels, off;
  248         int64_t qblockcnt;
  249 
  250         ump = VFSTOEXT2(vp->v_mount);
  251         if (nump)
  252                 *nump = 0;
  253         numlevels = 0;
  254         realbn = bn;
  255         if ((long)bn < 0)
  256                 bn = -(long)bn;
  257 
  258         /* The first NDADDR blocks are direct blocks. */
  259         if (bn < NDADDR)
  260                 return (0);
  261 
  262         /*
  263          * Determine the number of levels of indirection.  After this loop
  264          * is done, blockcnt indicates the number of data blocks possible
  265          * at the previous level of indirection, and NIADDR - i is the number
  266          * of levels of indirection needed to locate the requested block.
  267          */
  268         for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
  269                 if (i == 0)
  270                         return (EFBIG);
  271                 /*
  272                  * Use int64_t's here to avoid overflow for triple indirect
  273                  * blocks when longs have 32 bits and the block size is more
  274                  * than 4K.
  275                  */
  276                 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
  277                 if (bn < qblockcnt)
  278                         break;
  279                 blockcnt = qblockcnt;
  280         }
  281 
  282         /* Calculate the address of the first meta-block. */
  283         if (realbn >= 0)
  284                 metalbn = -(realbn - bn + NIADDR - i);
  285         else
  286                 metalbn = -(-realbn - bn + NIADDR - i);
  287 
  288         /*
  289          * At each iteration, off is the offset into the bap array which is
  290          * an array of disk addresses at the current level of indirection.
  291          * The logical block number and the offset in that block are stored
  292          * into the argument array.
  293          */
  294         ap->in_lbn = metalbn;
  295         ap->in_off = off = NIADDR - i;
  296         ap++;
  297         for (++numlevels; i <= NIADDR; i++) {
  298                 /* If searching for a meta-data block, quit when found. */
  299                 if (metalbn == realbn)
  300                         break;
  301 
  302                 off = (bn / blockcnt) % MNINDIR(ump);
  303 
  304                 ++numlevels;
  305                 ap->in_lbn = metalbn;
  306                 ap->in_off = off;
  307                 ++ap;
  308 
  309                 metalbn -= -1 + off * blockcnt;
  310                 blockcnt /= MNINDIR(ump);
  311         }
  312         if (nump)
  313                 *nump = numlevels;
  314         return (0);
  315 }

Cache object: 74dabc7c2251b64302d43a4ebcea50c3


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