ufs_bmap.c revision 1.1.1.1 1 /* $NetBSD: ufs_bmap.c,v 1.1.1.1 2001/10/26 06:22:21 lukem Exp $ */
2 /* From: NetBSD: ufs_bmap.c,v 1.10 2000/11/27 08:39:57 chs 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. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95
42 */
43
44 #include <sys/param.h>
45 #include <sys/time.h>
46
47 #include <errno.h>
48
49 #include <ufs/ufs/ufs_bswap.h>
50 #include <ufs/ufs/inode.h>
51 #include <ufs/ffs/fs.h>
52
53 #include "ffs/buf.h"
54 #include "ffs/ffs_extern.h"
55
56 /*
57 * Create an array of logical block number/offset pairs which represent the
58 * path of indirect blocks required to access a data block. The first "pair"
59 * contains the logical block number of the appropriate single, double or
60 * triple indirect block and the offset into the inode indirect block array.
61 * Note, the logical block number of the inode single/double/triple indirect
62 * block appears twice in the array, once with the offset into the i_ffs_ib and
63 * once with the offset into the page itself.
64 */
65 int
66 ufs_getlbns(struct inode *ip, ufs_daddr_t bn, struct indir *ap, int *nump)
67 {
68 long metalbn, realbn;
69 int64_t blockcnt;
70 int lbc;
71 int i, numlevels, off;
72 u_long lognindir;
73
74 lognindir = ffs(NINDIR(ip->i_fs)) - 1;
75 if (nump)
76 *nump = 0;
77 numlevels = 0;
78 realbn = bn;
79 if ((long)bn < 0)
80 bn = -(long)bn;
81
82 /* The first NDADDR blocks are direct blocks. */
83 if (bn < NDADDR)
84 return (0);
85
86 /*
87 * Determine the number of levels of indirection. After this loop
88 * is done, blockcnt indicates the number of data blocks possible
89 * at the given level of indirection, and NIADDR - i is the number
90 * of levels of indirection needed to locate the requested block.
91 */
92
93 bn -= NDADDR;
94 for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) {
95 if (i == 0)
96 return (EFBIG);
97
98 lbc += lognindir;
99 blockcnt = (int64_t)1 << lbc;
100
101 if (bn < blockcnt)
102 break;
103 }
104
105 /* Calculate the address of the first meta-block. */
106 if (realbn >= 0)
107 metalbn = -(realbn - bn + NIADDR - i);
108 else
109 metalbn = -(-realbn - bn + NIADDR - i);
110
111 /*
112 * At each iteration, off is the offset into the bap array which is
113 * an array of disk addresses at the current level of indirection.
114 * The logical block number and the offset in that block are stored
115 * into the argument array.
116 */
117 ap->in_lbn = metalbn;
118 ap->in_off = off = NIADDR - i;
119 ap->in_exists = 0;
120 ap++;
121 for (++numlevels; i <= NIADDR; i++) {
122 /* If searching for a meta-data block, quit when found. */
123 if (metalbn == realbn)
124 break;
125
126 lbc -= lognindir;
127 blockcnt = (int64_t)1 << lbc;
128 off = (bn >> lbc) & (NINDIR(ip->i_fs) - 1);
129
130 ++numlevels;
131 ap->in_lbn = metalbn;
132 ap->in_off = off;
133 ap->in_exists = 0;
134 ++ap;
135
136 metalbn -= -1 + off * blockcnt;
137 }
138 if (nump)
139 *nump = numlevels;
140 return (0);
141 }
142