root/ufs/ufs/ufs_bmap.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ufs_bmap
  2. ufs_bmaparray
  3. ufs_getlbns

    1 /*      $OpenBSD: ufs_bmap.c,v 1.25 2007/06/01 23:47:57 deraadt Exp $   */
    2 /*      $NetBSD: ufs_bmap.c,v 1.3 1996/02/09 22:36:00 christos Exp $    */
    3 
    4 /*
    5  * Copyright (c) 1989, 1991, 1993
    6  *      The Regents of the University of California.  All rights reserved.
    7  * (c) UNIX System Laboratories, Inc.
    8  * All or some portions of this file are derived from material licensed
    9  * to the University of California by American Telephone and Telegraph
   10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   11  * the permission of UNIX System Laboratories, Inc.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. Neither the name of the University nor the names of its contributors
   22  *    may be used to endorse or promote products derived from this software
   23  *    without specific prior written permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   35  * SUCH DAMAGE.
   36  *
   37  *      @(#)ufs_bmap.c  8.6 (Berkeley) 1/21/94
   38  */
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/buf.h>
   43 #include <sys/proc.h>
   44 #include <sys/vnode.h>
   45 #include <sys/mount.h>
   46 #include <sys/resourcevar.h>
   47 
   48 #include <miscfs/specfs/specdev.h>
   49 
   50 #include <ufs/ufs/quota.h>
   51 #include <ufs/ufs/inode.h>
   52 #include <ufs/ufs/ufsmount.h>
   53 #include <ufs/ufs/ufs_extern.h>
   54 
   55 /*
   56  * Bmap converts a the logical block number of a file to its physical block
   57  * number on the disk. The conversion is done by using the logical block
   58  * number to index into the array of block pointers described by the dinode.
   59  */
   60 int
   61 ufs_bmap(void *v)
   62 {
   63         struct vop_bmap_args *ap = v;
   64 
   65         /*
   66          * Check for underlying vnode requests and ensure that logical
   67          * to physical mapping is requested.
   68          */
   69         if (ap->a_vpp != NULL)
   70                 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
   71         if (ap->a_bnp == NULL)
   72                 return (0);
   73 
   74         return (ufs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
   75             ap->a_runp));
   76 }
   77 
   78 /*
   79  * Indirect blocks are now on the vnode for the file.  They are given negative
   80  * logical block numbers.  Indirect blocks are addressed by the negative
   81  * address of the first data block to which they point.  Double indirect blocks
   82  * are addressed by one less than the address of the first indirect block to
   83  * which they point.  Triple indirect blocks are addressed by one less than
   84  * the address of the first double indirect block to which they point.
   85  *
   86  * ufs_bmaparray does the bmap conversion, and if requested returns the
   87  * array of logical blocks which must be traversed to get to a block.
   88  * Each entry contains the offset into that block that gets you to the
   89  * next block and the disk address of the block (if it is assigned).
   90  */
   91 int
   92 ufs_bmaparray(struct vnode *vp, daddr_t bn, daddr64_t *bnp, struct indir *ap,
   93     int *nump, int *runp)
   94 {
   95         struct inode *ip;
   96         struct buf *bp;
   97         struct ufsmount *ump;
   98         struct mount *mp;
   99         struct vnode *devvp;
  100         struct indir a[NIADDR+1], *xap;
  101         daddr64_t daddr;
  102         long metalbn;
  103         int error, maxrun = 0, num;
  104 
  105         ip = VTOI(vp);
  106         mp = vp->v_mount;
  107         ump = VFSTOUFS(mp);
  108 #ifdef DIAGNOSTIC
  109         if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
  110                 panic("ufs_bmaparray: invalid arguments");
  111 #endif
  112 
  113         if (runp) {
  114                 /*
  115                  * XXX
  116                  * If MAXBSIZE is the largest transfer the disks can handle,
  117                  * we probably want maxrun to be 1 block less so that we
  118                  * don't create a block larger than the device can handle.
  119                  */
  120                 *runp = 0;
  121                 maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
  122         }
  123 
  124         xap = ap == NULL ? a : ap;
  125         if (!nump)
  126                 nump = &num;
  127         if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
  128                 return (error);
  129 
  130         num = *nump;
  131         if (num == 0) {
  132                 *bnp = blkptrtodb(ump, DIP(ip, db[bn]));
  133                 if (*bnp == 0)
  134                         *bnp = -1;
  135                 else if (runp)
  136                         for (++bn; bn < NDADDR && *runp < maxrun &&
  137                             is_sequential(ump, DIP(ip, db[bn - 1]),
  138                                 DIP(ip, db[bn]));
  139                             ++bn, ++*runp);
  140                 return (0);
  141         }
  142 
  143 
  144         /* Get disk address out of indirect block array */
  145         daddr = DIP(ip, ib[xap->in_off]);
  146 
  147         devvp = VFSTOUFS(vp->v_mount)->um_devvp;
  148         for (bp = NULL, ++xap; --num; ++xap) {
  149                 /* 
  150                  * Exit the loop if there is no disk address assigned yet and
  151                  * the indirect block isn't in the cache, or if we were
  152                  * looking for an indirect block and we've found it.
  153                  */
  154 
  155                 metalbn = xap->in_lbn;
  156                 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
  157                         break;
  158                 /*
  159                  * If we get here, we've either got the block in the cache
  160                  * or we have a disk address for it, go fetch it.
  161                  */
  162                 if (bp)
  163                         brelse(bp);
  164 
  165                 xap->in_exists = 1;
  166                 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
  167                 if (bp->b_flags & (B_DONE | B_DELWRI)) {
  168                         ;
  169                 }
  170 #ifdef DIAGNOSTIC
  171                 else if (!daddr)
  172                         panic("ufs_bmaparray: indirect block not in cache");
  173 #endif
  174                 else {
  175                         bp->b_blkno = blkptrtodb(ump, daddr);
  176                         bp->b_flags |= B_READ;
  177                         VOP_STRATEGY(bp);
  178                         curproc->p_stats->p_ru.ru_inblock++;    /* XXX */
  179                         if ((error = biowait(bp)) != 0) {
  180                                 brelse(bp);
  181                                 return (error);
  182                         }
  183                 }
  184 
  185 #ifdef FFS2
  186                 if (ip->i_ump->um_fstype == UM_UFS2) {
  187                         daddr = ((daddr64_t *)bp->b_data)[xap->in_off];
  188                         if (num == 1 && daddr && runp)
  189                                 for (bn = xap->in_off + 1;
  190                                     bn < MNINDIR(ump) && *runp < maxrun &&
  191                                     is_sequential(ump,
  192                                         ((daddr64_t *)bp->b_data)[bn - 1],
  193                                         ((daddr64_t *)bp->b_data)[bn]);
  194                                     ++bn, ++*runp);
  195 
  196                         continue;
  197                 }
  198 
  199 #endif /* FFS2 */
  200 
  201                 daddr = ((daddr_t *)bp->b_data)[xap->in_off];
  202                 if (num == 1 && daddr && runp)
  203                         for (bn = xap->in_off + 1;
  204                             bn < MNINDIR(ump) && *runp < maxrun &&
  205                             is_sequential(ump,
  206                                 ((daddr_t *)bp->b_data)[bn - 1],
  207                                 ((daddr_t *)bp->b_data)[bn]);
  208                             ++bn, ++*runp);
  209         }
  210         if (bp)
  211                 brelse(bp);
  212 
  213         daddr = blkptrtodb(ump, daddr);
  214         *bnp = daddr == 0 ? -1 : daddr;
  215         return (0);
  216 }
  217 
  218 /*
  219  * Create an array of logical block number/offset pairs which represent the
  220  * path of indirect blocks required to access a data block.  The first "pair"
  221  * contains the logical block number of the appropriate single, double or
  222  * triple indirect block and the offset into the inode indirect block array.
  223  * Note, the logical block number of the inode single/double/triple indirect
  224  * block appears twice in the array, once with the offset into the i_ffs_ib and
  225  * once with the offset into the page itself.
  226  */
  227 int
  228 ufs_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
  229 {
  230         long metalbn, realbn;
  231         struct ufsmount *ump;
  232         int64_t blockcnt;
  233         int i, numlevels, off;
  234 
  235         ump = VFSTOUFS(vp->v_mount);
  236         if (nump)
  237                 *nump = 0;
  238         numlevels = 0;
  239         realbn = bn;
  240         if ((long)bn < 0)
  241                 bn = -(long)bn;
  242 
  243 #ifdef DIAGNOSTIC
  244         if (realbn < 0 && realbn > -NDADDR) {
  245                 panic ("ufs_getlbns: Invalid indirect block %d specified",
  246                     realbn);
  247         }
  248 #endif
  249 
  250         /* The first NDADDR blocks are direct blocks. */
  251         if (bn < NDADDR)
  252                 return (0);
  253 
  254         /* 
  255          * Determine the number of levels of indirection.  After this loop
  256          * is done, blockcnt indicates the number of data blocks possible
  257          * at the given level of indirection, and NIADDR - i is the number
  258          * of levels of indirection needed to locate the requested block.
  259          */
  260         for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
  261                 if (i == 0)
  262                         return (EFBIG);
  263                 blockcnt *= MNINDIR(ump);
  264                 if (bn < blockcnt)
  265                         break;
  266         }
  267 
  268         /* Calculate the address of the first meta-block. */
  269         if (realbn >= 0)
  270                 metalbn = -(realbn - bn + NIADDR - i);
  271         else
  272                 metalbn = -(-realbn - bn + NIADDR - i);
  273 
  274         /* 
  275          * At each iteration, off is the offset into the bap array which is
  276          * an array of disk addresses at the current level of indirection.
  277          * The logical block number and the offset in that block are stored
  278          * into the argument array.
  279          */
  280         ap->in_lbn = metalbn;
  281         ap->in_off = off = NIADDR - i;
  282         ap->in_exists = 0;
  283         ap++;
  284         for (++numlevels; i <= NIADDR; i++) {
  285                 /* If searching for a meta-data block, quit when found. */
  286                 if (metalbn == realbn)
  287                         break;
  288 
  289                 blockcnt /= MNINDIR(ump);
  290                 off = (bn / blockcnt) % MNINDIR(ump);
  291 
  292                 ++numlevels;
  293                 ap->in_lbn = metalbn;
  294                 ap->in_off = off;
  295                 ap->in_exists = 0;
  296                 ++ap;
  297 
  298                 metalbn -= -1 + off * blockcnt;
  299         }
  300 #ifdef DIAGNOSTIC
  301         if (realbn < 0 && metalbn != realbn) {
  302                 panic("ufs_getlbns: indirect block %d not found", realbn);
  303         }
  304 #endif
  305         if (nump)
  306                 *nump = numlevels;
  307         return (0);
  308 }

/* [<][>][^][v][top][bottom][index][help] */