blob: b22fe3dbf66583b99006d37bb4d889ade2454651 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger1fd98e02005-05-09 22:10:42 +00002/*
3 * bmap.c --- logical to physical block mapping
4 *
5 * Copyright (C) 1997 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13#include <stdio.h>
14#include <string.h>
15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18
19#include "ext2_fs.h"
20#include "ext2fs.h"
21
Mike Frysinger1fd98e02005-05-09 22:10:42 +000022extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000023 struct ext2_inode *inode,
Mike Frysinger1fd98e02005-05-09 22:10:42 +000024 char *block_buf, int bmap_flags,
25 blk_t block, blk_t *phys_blk);
26
27#define inode_bmap(inode, nr) ((inode)->i_block[(nr)])
28
Rob Landley7c94bed2006-05-03 21:58:45 +000029static errcode_t block_ind_bmap(ext2_filsys fs, int flags,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000030 blk_t ind, char *block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +000031 int *blocks_alloc,
32 blk_t nr, blk_t *ret_blk)
33{
34 errcode_t retval;
35 blk_t b;
36
37 if (!ind) {
38 if (flags & BMAP_SET)
39 return EXT2_ET_SET_BMAP_NO_IND;
40 *ret_blk = 0;
41 return 0;
42 }
43 retval = io_channel_read_blk(fs->io, ind, 1, block_buf);
44 if (retval)
45 return retval;
46
47 if (flags & BMAP_SET) {
48 b = *ret_blk;
Rob Landley7c94bed2006-05-03 21:58:45 +000049#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000050 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
51 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
52 b = ext2fs_swab32(b);
53#endif
54 ((blk_t *) block_buf)[nr] = b;
55 return io_channel_write_blk(fs->io, ind, 1, block_buf);
56 }
57
58 b = ((blk_t *) block_buf)[nr];
59
Rob Landley7c94bed2006-05-03 21:58:45 +000060#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000061 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
62 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
63 b = ext2fs_swab32(b);
64#endif
65
66 if (!b && (flags & BMAP_ALLOC)) {
67 b = nr ? ((blk_t *) block_buf)[nr-1] : 0;
68 retval = ext2fs_alloc_block(fs, b,
69 block_buf + fs->blocksize, &b);
70 if (retval)
71 return retval;
72
Rob Landley7c94bed2006-05-03 21:58:45 +000073#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000074 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
75 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
76 ((blk_t *) block_buf)[nr] = ext2fs_swab32(b);
77 else
78#endif
79 ((blk_t *) block_buf)[nr] = b;
80
81 retval = io_channel_write_blk(fs->io, ind, 1, block_buf);
82 if (retval)
83 return retval;
84
85 (*blocks_alloc)++;
86 }
87
88 *ret_blk = b;
89 return 0;
90}
91
Rob Landley7c94bed2006-05-03 21:58:45 +000092static errcode_t block_dind_bmap(ext2_filsys fs, int flags,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000093 blk_t dind, char *block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +000094 int *blocks_alloc,
95 blk_t nr, blk_t *ret_blk)
96{
97 blk_t b;
98 errcode_t retval;
99 blk_t addr_per_block;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000100
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000101 addr_per_block = (blk_t) fs->blocksize >> 2;
102
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000103 retval = block_ind_bmap(fs, flags & ~BMAP_SET, dind, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000104 blocks_alloc, nr / addr_per_block, &b);
105 if (retval)
106 return retval;
107 retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
108 nr % addr_per_block, ret_blk);
109 return retval;
110}
111
Rob Landley7c94bed2006-05-03 21:58:45 +0000112static errcode_t block_tind_bmap(ext2_filsys fs, int flags,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000113 blk_t tind, char *block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000114 int *blocks_alloc,
115 blk_t nr, blk_t *ret_blk)
116{
117 blk_t b;
118 errcode_t retval;
119 blk_t addr_per_block;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000120
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000121 addr_per_block = (blk_t) fs->blocksize >> 2;
122
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000123 retval = block_dind_bmap(fs, flags & ~BMAP_SET, tind, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000124 blocks_alloc, nr / addr_per_block, &b);
125 if (retval)
126 return retval;
127 retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
128 nr % addr_per_block, ret_blk);
129 return retval;
130}
131
132errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
133 char *block_buf, int bmap_flags, blk_t block,
134 blk_t *phys_blk)
135{
136 struct ext2_inode inode_buf;
137 blk_t addr_per_block;
138 blk_t b;
139 char *buf = 0;
140 errcode_t retval = 0;
141 int blocks_alloc = 0, inode_dirty = 0;
142
143 if (!(bmap_flags & BMAP_SET))
144 *phys_blk = 0;
145
146 /* Read inode structure if necessary */
147 if (!inode) {
148 retval = ext2fs_read_inode(fs, ino, &inode_buf);
149 if (retval)
150 return retval;
151 inode = &inode_buf;
152 }
153 addr_per_block = (blk_t) fs->blocksize >> 2;
154
155 if (!block_buf) {
156 retval = ext2fs_get_mem(fs->blocksize * 2, &buf);
157 if (retval)
158 return retval;
159 block_buf = buf;
160 }
161
162 if (block < EXT2_NDIR_BLOCKS) {
163 if (bmap_flags & BMAP_SET) {
164 b = *phys_blk;
Rob Landley7c94bed2006-05-03 21:58:45 +0000165#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000166 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
167 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
168 b = ext2fs_swab32(b);
169#endif
170 inode_bmap(inode, block) = b;
171 inode_dirty++;
172 goto done;
173 }
174
175 *phys_blk = inode_bmap(inode, block);
176 b = block ? inode_bmap(inode, block-1) : 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000177
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000178 if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
179 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
180 if (retval)
181 goto done;
182 inode_bmap(inode, block) = b;
183 blocks_alloc++;
184 *phys_blk = b;
185 }
186 goto done;
187 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000188
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000189 /* Indirect block */
190 block -= EXT2_NDIR_BLOCKS;
191 if (block < addr_per_block) {
192 b = inode_bmap(inode, EXT2_IND_BLOCK);
193 if (!b) {
194 if (!(bmap_flags & BMAP_ALLOC)) {
195 if (bmap_flags & BMAP_SET)
196 retval = EXT2_ET_SET_BMAP_NO_IND;
197 goto done;
198 }
199
200 b = inode_bmap(inode, EXT2_IND_BLOCK-1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000201 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000202 if (retval)
203 goto done;
204 inode_bmap(inode, EXT2_IND_BLOCK) = b;
205 blocks_alloc++;
206 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000207 retval = block_ind_bmap(fs, bmap_flags, b, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000208 &blocks_alloc, block, phys_blk);
209 goto done;
210 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000211
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000212 /* Doubly indirect block */
213 block -= addr_per_block;
214 if (block < addr_per_block * addr_per_block) {
215 b = inode_bmap(inode, EXT2_DIND_BLOCK);
216 if (!b) {
217 if (!(bmap_flags & BMAP_ALLOC)) {
218 if (bmap_flags & BMAP_SET)
219 retval = EXT2_ET_SET_BMAP_NO_IND;
220 goto done;
221 }
222
223 b = inode_bmap(inode, EXT2_IND_BLOCK);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000224 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000225 if (retval)
226 goto done;
227 inode_bmap(inode, EXT2_DIND_BLOCK) = b;
228 blocks_alloc++;
229 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000230 retval = block_dind_bmap(fs, bmap_flags, b, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000231 &blocks_alloc, block, phys_blk);
232 goto done;
233 }
234
235 /* Triply indirect block */
236 block -= addr_per_block * addr_per_block;
237 b = inode_bmap(inode, EXT2_TIND_BLOCK);
238 if (!b) {
239 if (!(bmap_flags & BMAP_ALLOC)) {
240 if (bmap_flags & BMAP_SET)
241 retval = EXT2_ET_SET_BMAP_NO_IND;
242 goto done;
243 }
244
245 b = inode_bmap(inode, EXT2_DIND_BLOCK);
246 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
247 if (retval)
248 goto done;
249 inode_bmap(inode, EXT2_TIND_BLOCK) = b;
250 blocks_alloc++;
251 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000252 retval = block_tind_bmap(fs, bmap_flags, b, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000253 &blocks_alloc, block, phys_blk);
254done:
Rob Landleye7c43b62006-03-01 16:39:45 +0000255 ext2fs_free_mem(&buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000256 if ((retval == 0) && (blocks_alloc || inode_dirty)) {
257 inode->i_blocks += (blocks_alloc * fs->blocksize) / 512;
258 retval = ext2fs_write_inode(fs, ino, inode);
259 }
260 return retval;
261}
262
263
264