root/ufs/ext2fs/ext2fs_bmap.c

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

DEFINITIONS

This source file includes following definitions.
  1. ext2fs_bmap
  2. ext2fs_bmaparray

    1 /*      $OpenBSD: ext2fs_bmap.c,v 1.15 2007/06/17 20:15:25 jasper Exp $ */
    2 /*      $NetBSD: ext2fs_bmap.c,v 1.5 2000/03/30 12:41:11 augustss Exp $ */
    3 
    4 /*
    5  * Copyright (c) 1997 Manuel Bouyer.
    6  * Copyright (c) 1989, 1991, 1993
    7  *      The Regents of the University of California.  All rights reserved.
    8  * (c) UNIX System Laboratories, Inc.
    9  * All or some portions of this file are derived from material licensed
   10  * to the University of California by American Telephone and Telegraph
   11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   12  * the permission of UNIX System Laboratories, Inc.
   13  *
   14  * Redistribution and use in source and binary forms, with or without
   15  * modification, are permitted provided that the following conditions
   16  * are met:
   17  * 1. Redistributions of source code must retain the above copyright
   18  *    notice, this list of conditions and the following disclaimer.
   19  * 2. Redistributions in binary form must reproduce the above copyright
   20  *    notice, this list of conditions and the following disclaimer in the
   21  *    documentation and/or other materials provided with the distribution.
   22  * 3. 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.6 (Berkeley) 1/21/94
   39  * Modified for ext2fs by Manuel Bouyer.
   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 
   50 #include <miscfs/specfs/specdev.h>
   51 
   52 #include <ufs/ufs/quota.h>
   53 #include <ufs/ufs/inode.h>
   54 #include <ufs/ufs/ufsmount.h>
   55 #include <ufs/ufs/ufs_extern.h>
   56 #include <ufs/ext2fs/ext2fs.h>
   57 #include <ufs/ext2fs/ext2fs_extern.h>
   58 
   59 static int ext2fs_bmaparray(struct vnode *, int32_t, daddr64_t *,
   60     struct indir *, int *, int *);
   61 
   62 /*
   63  * Bmap converts a the logical block number of a file to its physical block
   64  * number on the disk. The conversion is done by using the logical block
   65  * number to index into the array of block pointers described by the dinode.
   66  */
   67 int
   68 ext2fs_bmap(void *v)
   69 {
   70         struct vop_bmap_args *ap = v;
   71         /*
   72          * Check for underlying vnode requests and ensure that logical
   73          * to physical mapping is requested.
   74          */
   75         if (ap->a_vpp != NULL)
   76                 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
   77         if (ap->a_bnp == NULL)
   78                 return (0);
   79 
   80         return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
   81                 ap->a_runp));
   82 }
   83 
   84 /*
   85  * Indirect blocks are now on the vnode for the file.  They are given negative
   86  * logical block numbers.  Indirect blocks are addressed by the negative
   87  * address of the first data block to which they point.  Double indirect blocks
   88  * are addressed by one less than the address of the first indirect block to
   89  * which they point.  Triple indirect blocks are addressed by one less than
   90  * the address of the first double indirect block to which they point.
   91  *
   92  * ext2fs_bmaparray does the bmap conversion, and if requested returns the
   93  * array of logical blocks which must be traversed to get to a block.
   94  * Each entry contains the offset into that block that gets you to the
   95  * next block and the disk address of the block (if it is assigned).
   96  */
   97 
   98 int
   99 ext2fs_bmaparray(struct vnode *vp, int32_t bn, daddr64_t *bnp,
  100     struct indir *ap, int *nump, int *runp)
  101 {
  102         struct inode *ip;
  103         struct buf *bp;
  104         struct ufsmount *ump;
  105         struct mount *mp;
  106         struct vnode *devvp;
  107         struct indir a[NIADDR+1], *xap;
  108         int32_t daddr;
  109         long metalbn;
  110         int error, maxrun = 0, num;
  111 
  112         ip = VTOI(vp);
  113         mp = vp->v_mount;
  114         ump = VFSTOUFS(mp);
  115 #ifdef DIAGNOSTIC
  116         if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
  117                 panic("ext2fs_bmaparray: invalid arguments");
  118 #endif
  119 
  120         if (runp) {
  121                 /*
  122                  * XXX
  123                  * If MAXBSIZE is the largest transfer the disks can handle,
  124                  * we probably want maxrun to be 1 block less so that we
  125                  * don't create a block larger than the device can handle.
  126                  */
  127                 *runp = 0;
  128                 maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
  129         }
  130 
  131         xap = ap == NULL ? a : ap;
  132         if (!nump)
  133                 nump = &num;
  134         if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
  135                 return (error);
  136 
  137         num = *nump;
  138         if (num == 0) {
  139                 *bnp = blkptrtodb(ump, fs2h32(ip->i_e2fs_blocks[bn]));
  140                 if (*bnp == 0)
  141                         *bnp = -1;
  142                 else if (runp)
  143                         for (++bn; bn < NDADDR && *runp < maxrun &&
  144                                 is_sequential(ump, fs2h32(ip->i_e2fs_blocks[bn - 1]),
  145                                                           fs2h32(ip->i_e2fs_blocks[bn]));
  146                                 ++bn, ++*runp);
  147                 return (0);
  148         }
  149 
  150 
  151         /* Get disk address out of indirect block array */
  152         daddr = fs2h32(ip->i_e2fs_blocks[NDADDR + xap->in_off]);
  153 
  154         devvp = VFSTOUFS(vp->v_mount)->um_devvp;
  155 
  156 #ifdef DIAGNOSTIC
  157     if (num > NIADDR + 1 || num < 1) {
  158                 printf("ext2fs_bmaparray: num=%d\n", num);
  159                 panic("ext2fs_bmaparray: num");
  160         }
  161 #endif
  162         for (bp = NULL, ++xap; --num; ++xap) {
  163                 /* 
  164                  * Exit the loop if there is no disk address assigned yet and
  165                  * the indirect block isn't in the cache, or if we were
  166                  * looking for an indirect block and we've found it.
  167                  */
  168 
  169                 metalbn = xap->in_lbn;
  170                 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
  171                         break;
  172                 /*
  173                  * If we get here, we've either got the block in the cache
  174                  * or we have a disk address for it, go fetch it.
  175                  */
  176                 if (bp)
  177                         brelse(bp);
  178 
  179                 xap->in_exists = 1;
  180                 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
  181                 if (bp->b_flags & (B_DONE | B_DELWRI)) {
  182                         ;
  183                 }
  184 #ifdef DIAGNOSTIC
  185                 else if (!daddr)
  186                         panic("ext2fs_bmaparry: indirect block not in cache");
  187 #endif
  188                 else {
  189                         bp->b_blkno = blkptrtodb(ump, daddr);
  190                         bp->b_flags |= B_READ;
  191                         VOP_STRATEGY(bp);
  192                         curproc->p_stats->p_ru.ru_inblock++;    /* XXX */
  193                         if ((error = biowait(bp)) != 0) {
  194                                 brelse(bp);
  195                                 return (error);
  196                         }
  197                 }
  198 
  199                 daddr = fs2h32(((int32_t *)bp->b_data)[xap->in_off]);
  200                 if (num == 1 && daddr && runp)
  201                         for (bn = xap->in_off + 1;
  202                                 bn < MNINDIR(ump) && *runp < maxrun &&
  203                                 is_sequential(ump, ((int32_t *)bp->b_data)[bn - 1],
  204                                 ((int32_t *)bp->b_data)[bn]);
  205                                 ++bn, ++*runp);
  206         }
  207         if (bp)
  208                 brelse(bp);
  209 
  210         daddr = blkptrtodb(ump, daddr);
  211         *bnp = daddr == 0 ? -1 : daddr;
  212         return (0);
  213 }

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