ufs_bmap.c revision 186261
1185222Ssam/* $NetBSD: ufs_bmap.c,v 1.14 2004/06/20 22:20:18 jmc Exp $ */ 2185222Ssam/* From: NetBSD: ufs_bmap.c,v 1.14 2001/11/08 05:00:51 chs Exp */ 3185222Ssam 4185222Ssam/* 5185222Ssam * Copyright (c) 1989, 1991, 1993 6185222Ssam * The Regents of the University of California. All rights reserved. 7185222Ssam * (c) UNIX System Laboratories, Inc. 8185222Ssam * All or some portions of this file are derived from material licensed 9185222Ssam * to the University of California by American Telephone and Telegraph 10185222Ssam * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11185222Ssam * the permission of UNIX System Laboratories, Inc. 12185222Ssam * 13185222Ssam * Redistribution and use in source and binary forms, with or without 14185222Ssam * modification, are permitted provided that the following conditions 15185222Ssam * are met: 16185222Ssam * 1. Redistributions of source code must retain the above copyright 17185222Ssam * notice, this list of conditions and the following disclaimer. 18185222Ssam * 2. Redistributions in binary form must reproduce the above copyright 19185222Ssam * notice, this list of conditions and the following disclaimer in the 20185222Ssam * documentation and/or other materials provided with the distribution. 21185222Ssam * 3. Neither the name of the University nor the names of its contributors 22185222Ssam * may be used to endorse or promote products derived from this software 23185222Ssam * without specific prior written permission. 24185222Ssam * 25185222Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26185222Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27185222Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28185222Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29185222Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30185222Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31185222Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32185222Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33185222Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34185222Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35185222Ssam * SUCH DAMAGE. 36185222Ssam * 37185222Ssam * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95 38185222Ssam */ 39185222Ssam 40185222Ssam#include <sys/cdefs.h> 41185222Ssam#if defined(__RCSID) && !defined(__lint) 42185222Ssam__RCSID("$NetBSD: ufs_bmap.c,v 1.14 2004/06/20 22:20:18 jmc Exp $"); 43185222Ssam#endif /* !__lint */ 44185222Ssam 45185222Ssam#include <sys/param.h> 46185222Ssam#include <sys/time.h> 47185222Ssam 48185222Ssam#include <assert.h> 49185222Ssam#include <errno.h> 50185222Ssam#include <strings.h> 51185222Ssam 52185222Ssam#include "makefs.h" 53185222Ssam 54185222Ssam#include <ufs/ufs/dinode.h> 55185222Ssam#include <ufs/ffs/fs.h> 56185222Ssam 57186261Ssam#include "ffs/ufs_bswap.h" 58185222Ssam#include "ffs/ufs_inode.h" 59185222Ssam#include "ffs/ffs_extern.h" 60185222Ssam 61185222Ssam/* 62185222Ssam * Create an array of logical block number/offset pairs which represent the 63185222Ssam * path of indirect blocks required to access a data block. The first "pair" 64185222Ssam * contains the logical block number of the appropriate single, double or 65185222Ssam * triple indirect block and the offset into the inode indirect block array. 66185222Ssam * Note, the logical block number of the inode single/double/triple indirect 67185222Ssam * block appears twice in the array, once with the offset into the i_ffs_ib and 68185222Ssam * once with the offset into the page itself. 69185222Ssam */ 70185222Ssamint 71185222Ssamufs_getlbns(struct inode *ip, daddr_t bn, struct indir *ap, int *nump) 72185222Ssam{ 73185222Ssam daddr_t metalbn, realbn; 74185222Ssam int64_t blockcnt; 75185222Ssam int lbc; 76185222Ssam int i, numlevels, off; 77185222Ssam u_long lognindir; 78185222Ssam 79185222Ssam lognindir = ffs(NINDIR(ip->i_fs)) - 1; 80185222Ssam if (nump) 81185222Ssam *nump = 0; 82185222Ssam numlevels = 0; 83185222Ssam realbn = bn; 84185222Ssam if ((long)bn < 0) 85185222Ssam bn = -(long)bn; 86185222Ssam 87185222Ssam assert (bn >= NDADDR); 88185222Ssam 89185222Ssam /* 90185222Ssam * Determine the number of levels of indirection. After this loop 91185222Ssam * is done, blockcnt indicates the number of data blocks possible 92185222Ssam * at the given level of indirection, and NIADDR - i is the number 93185222Ssam * of levels of indirection needed to locate the requested block. 94185222Ssam */ 95185222Ssam 96185222Ssam bn -= NDADDR; 97185222Ssam for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) { 98185222Ssam if (i == 0) 99185222Ssam return (EFBIG); 100185222Ssam 101185222Ssam lbc += lognindir; 102185222Ssam blockcnt = (int64_t)1 << lbc; 103185222Ssam 104185222Ssam if (bn < blockcnt) 105185222Ssam break; 106185222Ssam } 107185222Ssam 108185222Ssam /* Calculate the address of the first meta-block. */ 109185222Ssam if (realbn >= 0) 110185222Ssam metalbn = -(realbn - bn + NIADDR - i); 111185222Ssam else 112185222Ssam metalbn = -(-realbn - bn + NIADDR - i); 113185222Ssam 114185222Ssam /* 115185222Ssam * At each iteration, off is the offset into the bap array which is 116185222Ssam * an array of disk addresses at the current level of indirection. 117185222Ssam * The logical block number and the offset in that block are stored 118185222Ssam * into the argument array. 119185222Ssam */ 120185222Ssam ap->in_lbn = metalbn; 121185222Ssam ap->in_off = off = NIADDR - i; 122185222Ssam ap->in_exists = 0; 123185222Ssam ap++; 124185222Ssam for (++numlevels; i <= NIADDR; i++) { 125185222Ssam /* If searching for a meta-data block, quit when found. */ 126185222Ssam if (metalbn == realbn) 127185222Ssam break; 128185222Ssam 129185222Ssam lbc -= lognindir; 130185222Ssam blockcnt = (int64_t)1 << lbc; 131185222Ssam off = (bn >> lbc) & (NINDIR(ip->i_fs) - 1); 132185222Ssam 133185222Ssam ++numlevels; 134185222Ssam ap->in_lbn = metalbn; 135185222Ssam ap->in_off = off; 136185222Ssam ap->in_exists = 0; 137185222Ssam ++ap; 138185222Ssam 139185222Ssam metalbn -= -1 + (off << lbc); 140185222Ssam } 141185222Ssam if (nump) 142185222Ssam *nump = numlevels; 143185222Ssam return (0); 144185222Ssam} 145