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  * $FreeBSD$
   40  */
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/buf.h>
   45 #include <sys/proc.h>
   46 #include <sys/vnode.h>
   47 #include <sys/mount.h>
   48 #include <sys/resourcevar.h>
   49 #include <sys/conf.h>
   50 
   51 #include <ufs/ufs/quota.h>
   52 #include <ufs/ufs/inode.h>
   53 #include <ufs/ufs/ufsmount.h>
   54 #include <ufs/ufs/ufs_extern.h>
   55 #include <miscfs/specfs/specdev.h>
   56 
   57 /*
   58  * Bmap converts a the logical block number of a file to its physical block
   59  * number on the disk. The conversion is done by using the logical block
   60  * number to index into the array of block pointers described by the dinode.
   61  */
   62 int
   63 ufs_bmap(ap)
   64         struct vop_bmap_args /* {
   65                 struct vnode *a_vp;
   66                 ufs_daddr_t a_bn;
   67                 struct vnode **a_vpp;
   68                 ufs_daddr_t *a_bnp;
   69                 int *a_runp;
   70                 int *a_runb;
   71         } */ *ap;
   72 {
   73         /*
   74          * Check for underlying vnode requests and ensure that logical
   75          * to physical mapping is requested.
   76          */
   77         if (ap->a_vpp != NULL)
   78                 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
   79         if (ap->a_bnp == NULL)
   80                 return (0);
   81 
   82         return (ufs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
   83             ap->a_runp, ap->a_runb));
   84 }
   85 
   86 /*
   87  * Indirect blocks are now on the vnode for the file.  They are given negative
   88  * logical block numbers.  Indirect blocks are addressed by the negative
   89  * address of the first data block to which they point.  Double indirect blocks
   90  * are addressed by one less than the address of the first indirect block to
   91  * which they point.  Triple indirect blocks are addressed by one less than
   92  * the address of the first double indirect block to which they point.
   93  *
   94  * ufs_bmaparray does the bmap conversion, and if requested returns the
   95  * array of logical blocks which must be traversed to get to a block.
   96  * Each entry contains the offset into that block that gets you to the
   97  * next block and the disk address of the block (if it is assigned).
   98  */
   99 
  100 int
  101 ufs_bmaparray(vp, bn, bnp, ap, nump, runp, runb)
  102         struct vnode *vp;
  103         ufs_daddr_t bn;
  104         ufs_daddr_t *bnp;
  105         struct indir *ap;
  106         int *nump;
  107         int *runp;
  108         int *runb;
  109 {
  110         register struct inode *ip;
  111         struct buf *bp;
  112         struct ufsmount *ump;
  113         struct mount *mp;
  114         struct vnode *devvp;
  115         struct indir a[NIADDR+1], *xap;
  116         ufs_daddr_t daddr;
  117         long metalbn;
  118         int error, maxrun = 0, num;
  119 
  120         ip = VTOI(vp);
  121         mp = vp->v_mount;
  122         ump = VFSTOUFS(mp);
  123 #ifdef DIAGNOSTIC
  124         if (ap != NULL && nump == NULL || ap == NULL && nump != NULL)
  125                 panic("ufs_bmaparray: invalid arguments");
  126 #endif
  127 
  128         if (runp) {
  129                 *runp = 0;
  130         }
  131 
  132         if (runb) {
  133                 *runb = 0;
  134         }
  135 
  136         maxrun = 0;
  137         if (runp || runb || (vp->v_maxio == 0)) {
  138 
  139                 struct vnode *devvp;
  140                 int blksize;
  141 
  142                 blksize = mp->mnt_stat.f_iosize;
  143 
  144                 /*
  145                  * XXX
  146                  * If MAXPHYS is the largest transfer the disks can handle,
  147                  * we probably want maxrun to be 1 block less so that we
  148                  * don't create a block larger than the device can handle.
  149                  */
  150                 devvp = ip->i_devvp;
  151 
  152                 if (devvp != NULL && devvp->v_tag != VT_MFS &&
  153                     devvp->v_type == VBLK) {
  154                         if (bdevsw[major(devvp->v_rdev)]->d_maxio > MAXPHYS) {
  155                                 maxrun = MAXPHYS;
  156                                 vp->v_maxio = MAXPHYS;
  157                         } else {
  158                                 maxrun = bdevsw[major(devvp->v_rdev)]->d_maxio;
  159                                 vp->v_maxio = bdevsw[major(devvp->v_rdev)]->d_maxio;
  160                         }
  161                         maxrun = maxrun / blksize;
  162                         maxrun -= 1;
  163                 }
  164 
  165                 if (maxrun <= 0) {
  166                         vp->v_maxio = DFLTPHYS;
  167                         maxrun = DFLTPHYS / blksize;
  168                         maxrun -= 1;
  169                 }
  170         }
  171 
  172         xap = ap == NULL ? a : ap;
  173         if (!nump)
  174                 nump = &num;
  175         error = ufs_getlbns(vp, bn, xap, nump);
  176         if (error)
  177                 return (error);
  178 
  179         num = *nump;
  180         if (num == 0) {
  181                 *bnp = blkptrtodb(ump, ip->i_db[bn]);
  182                 if (*bnp == 0)
  183                         *bnp = -1;
  184                 else if (runp) {
  185                         daddr_t bnb = bn;
  186                         for (++bn; bn < NDADDR && *runp < maxrun &&
  187                             is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
  188                             ++bn, ++*runp);
  189                         bn = bnb;
  190                         if (runb && (bn > 0)) {
  191                                 for (--bn; (bn >= 0) && (*runb < maxrun) &&
  192                                         is_sequential(ump, ip->i_db[bn],
  193                                                 ip->i_db[bn+1]);
  194                                                 --bn, ++*runb);
  195                         }
  196                 }
  197                 return (0);
  198         }
  199 
  200 
  201         /* Get disk address out of indirect block array */
  202         daddr = ip->i_ib[xap->in_off];
  203 
  204         devvp = VFSTOUFS(vp->v_mount)->um_devvp;
  205         for (bp = NULL, ++xap; --num; ++xap) {
  206                 /*
  207                  * Exit the loop if there is no disk address assigned yet and
  208                  * the indirect block isn't in the cache, or if we were
  209                  * looking for an indirect block and we've found it.
  210                  */
  211 
  212                 metalbn = xap->in_lbn;
  213                 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
  214                         break;
  215                 /*
  216                  * If we get here, we've either got the block in the cache
  217                  * or we have a disk address for it, go fetch it.
  218                  */
  219                 if (bp)
  220                         bqrelse(bp);
  221 
  222                 xap->in_exists = 1;
  223                 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
  224                 if ((bp->b_flags & B_CACHE) == 0) {
  225 #ifdef DIAGNOSTIC
  226                         if (!daddr)
  227                                 panic("ufs_bmaparray: indirect block not in cache");
  228 #endif
  229                         bp->b_blkno = blkptrtodb(ump, daddr);
  230                         bp->b_flags |= B_READ;
  231                         vfs_busy_pages(bp, 0);
  232                         VOP_STRATEGY(bp->b_vp, bp);
  233                         curproc->p_stats->p_ru.ru_inblock++;    /* XXX */
  234                         error = biowait(bp);
  235                         if (error) {
  236                                 brelse(bp);
  237                                 return (error);
  238                         }
  239                 }
  240 
  241                 daddr = ((ufs_daddr_t *)bp->b_data)[xap->in_off];
  242                 if (num == 1 && daddr && runp) {
  243                         for (bn = xap->in_off + 1;
  244                             bn < MNINDIR(ump) && *runp < maxrun &&
  245                             is_sequential(ump,
  246                             ((ufs_daddr_t *)bp->b_data)[bn - 1],
  247                             ((ufs_daddr_t *)bp->b_data)[bn]);
  248                             ++bn, ++*runp);
  249                         bn = xap->in_off;
  250                         if (runb && bn) {
  251                                 for(--bn; bn > 0 && *runb < maxrun &&
  252                                         is_sequential(ump, ((daddr_t *)bp->b_data)[bn],
  253                                             ((daddr_t *)bp->b_data)[bn+1]);
  254                                         --bn, ++*runb);
  255                         }
  256                 }
  257         }
  258         if (bp)
  259                 bqrelse(bp);
  260 
  261         daddr = blkptrtodb(ump, daddr);
  262         *bnp = daddr == 0 ? -1 : daddr;
  263         return (0);
  264 }
  265 
  266 /*
  267  * Create an array of logical block number/offset pairs which represent the
  268  * path of indirect blocks required to access a data block.  The first "pair"
  269  * contains the logical block number of the appropriate single, double or
  270  * triple indirect block and the offset into the inode indirect block array.
  271  * Note, the logical block number of the inode single/double/triple indirect
  272  * block appears twice in the array, once with the offset into the i_ib and
  273  * once with the offset into the page itself.
  274  */
  275 int
  276 ufs_getlbns(vp, bn, ap, nump)
  277         struct vnode *vp;
  278         ufs_daddr_t bn;
  279         struct indir *ap;
  280         int *nump;
  281 {
  282         long blockcnt, metalbn, realbn;
  283         struct ufsmount *ump;
  284         int i, numlevels, off;
  285         int64_t qblockcnt;
  286 
  287         ump = VFSTOUFS(vp->v_mount);
  288         if (nump)
  289                 *nump = 0;
  290         numlevels = 0;
  291         realbn = bn;
  292         if ((long)bn < 0)
  293                 bn = -(long)bn;
  294 
  295         /* The first NDADDR blocks are direct blocks. */
  296         if (bn < NDADDR)
  297                 return (0);
  298 
  299         /*
  300          * Determine the number of levels of indirection.  After this loop
  301          * is done, blockcnt indicates the number of data blocks possible
  302          * at the previous level of indirection, and NIADDR - i is the number
  303          * of levels of indirection needed to locate the requested block.
  304          */
  305         for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
  306                 if (i == 0)
  307                         return (EFBIG);
  308                 /*
  309                  * Use int64_t's here to avoid overflow for triple indirect
  310                  * blocks when longs have 32 bits and the block size is more
  311                  * than 4K.
  312                  */
  313                 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
  314                 if (bn < qblockcnt)
  315                         break;
  316                 blockcnt = qblockcnt;
  317         }
  318 
  319         /* Calculate the address of the first meta-block. */
  320         if (realbn >= 0)
  321                 metalbn = -(realbn - bn + NIADDR - i);
  322         else
  323                 metalbn = -(-realbn - bn + NIADDR - i);
  324 
  325         /*
  326          * At each iteration, off is the offset into the bap array which is
  327          * an array of disk addresses at the current level of indirection.
  328          * The logical block number and the offset in that block are stored
  329          * into the argument array.
  330          */
  331         ap->in_lbn = metalbn;
  332         ap->in_off = off = NIADDR - i;
  333         ap->in_exists = 0;
  334         ap++;
  335         for (++numlevels; i <= NIADDR; i++) {
  336                 /* If searching for a meta-data block, quit when found. */
  337                 if (metalbn == realbn)
  338                         break;
  339 
  340                 off = (bn / blockcnt) % MNINDIR(ump);
  341 
  342                 ++numlevels;
  343                 ap->in_lbn = metalbn;
  344                 ap->in_off = off;
  345                 ap->in_exists = 0;
  346                 ++ap;
  347 
  348                 metalbn -= -1 + off * blockcnt;
  349                 blockcnt /= MNINDIR(ump);
  350         }
  351         if (nump)
  352                 *nump = numlevels;
  353         return (0);
  354 }

Cache object: 9a8a17c3853e4aeddf28f36cac9f564f


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