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

Cache object: e69beeb1015c8f10d21430aca0d9904e


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