blob: 0015ba288de974a257eab123443d4322f6d9e437 [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
2 * bmap.c --- logical to physical block mapping
3 *
4 * Copyright (C) 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <string.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17
18#include "ext2_fs.h"
19#include "ext2fs.h"
20
Mike Frysinger1fd98e02005-05-09 22:10:42 +000021extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000022 struct ext2_inode *inode,
Mike Frysinger1fd98e02005-05-09 22:10:42 +000023 char *block_buf, int bmap_flags,
24 blk_t block, blk_t *phys_blk);
25
26#define inode_bmap(inode, nr) ((inode)->i_block[(nr)])
27
Rob Landley7c94bed2006-05-03 21:58:45 +000028static errcode_t block_ind_bmap(ext2_filsys fs, int flags,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000029 blk_t ind, char *block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +000030 int *blocks_alloc,
31 blk_t nr, blk_t *ret_blk)
32{
33 errcode_t retval;
34 blk_t b;
35
36 if (!ind) {
37 if (flags & BMAP_SET)
38 return EXT2_ET_SET_BMAP_NO_IND;
39 *ret_blk = 0;
40 return 0;
41 }
42 retval = io_channel_read_blk(fs->io, ind, 1, block_buf);
43 if (retval)
44 return retval;
45
46 if (flags & BMAP_SET) {
47 b = *ret_blk;
Rob Landley7c94bed2006-05-03 21:58:45 +000048#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000049 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
50 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
51 b = ext2fs_swab32(b);
52#endif
53 ((blk_t *) block_buf)[nr] = b;
54 return io_channel_write_blk(fs->io, ind, 1, block_buf);
55 }
56
57 b = ((blk_t *) block_buf)[nr];
58
Rob Landley7c94bed2006-05-03 21:58:45 +000059#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000060 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
61 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
62 b = ext2fs_swab32(b);
63#endif
64
65 if (!b && (flags & BMAP_ALLOC)) {
66 b = nr ? ((blk_t *) block_buf)[nr-1] : 0;
67 retval = ext2fs_alloc_block(fs, b,
68 block_buf + fs->blocksize, &b);
69 if (retval)
70 return retval;
71
Rob Landley7c94bed2006-05-03 21:58:45 +000072#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000073 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
74 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
75 ((blk_t *) block_buf)[nr] = ext2fs_swab32(b);
76 else
77#endif
78 ((blk_t *) block_buf)[nr] = b;
79
80 retval = io_channel_write_blk(fs->io, ind, 1, block_buf);
81 if (retval)
82 return retval;
83
84 (*blocks_alloc)++;
85 }
86
87 *ret_blk = b;
88 return 0;
89}
90
Rob Landley7c94bed2006-05-03 21:58:45 +000091static errcode_t block_dind_bmap(ext2_filsys fs, int flags,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000092 blk_t dind, char *block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +000093 int *blocks_alloc,
94 blk_t nr, blk_t *ret_blk)
95{
96 blk_t b;
97 errcode_t retval;
98 blk_t addr_per_block;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000099
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000100 addr_per_block = (blk_t) fs->blocksize >> 2;
101
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000102 retval = block_ind_bmap(fs, flags & ~BMAP_SET, dind, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000103 blocks_alloc, nr / addr_per_block, &b);
104 if (retval)
105 return retval;
106 retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
107 nr % addr_per_block, ret_blk);
108 return retval;
109}
110
Rob Landley7c94bed2006-05-03 21:58:45 +0000111static errcode_t block_tind_bmap(ext2_filsys fs, int flags,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000112 blk_t tind, char *block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000113 int *blocks_alloc,
114 blk_t nr, blk_t *ret_blk)
115{
116 blk_t b;
117 errcode_t retval;
118 blk_t addr_per_block;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000119
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000120 addr_per_block = (blk_t) fs->blocksize >> 2;
121
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000122 retval = block_dind_bmap(fs, flags & ~BMAP_SET, tind, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000123 blocks_alloc, nr / addr_per_block, &b);
124 if (retval)
125 return retval;
126 retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
127 nr % addr_per_block, ret_blk);
128 return retval;
129}
130
131errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
132 char *block_buf, int bmap_flags, blk_t block,
133 blk_t *phys_blk)
134{
135 struct ext2_inode inode_buf;
136 blk_t addr_per_block;
137 blk_t b;
138 char *buf = 0;
139 errcode_t retval = 0;
140 int blocks_alloc = 0, inode_dirty = 0;
141
142 if (!(bmap_flags & BMAP_SET))
143 *phys_blk = 0;
144
145 /* Read inode structure if necessary */
146 if (!inode) {
147 retval = ext2fs_read_inode(fs, ino, &inode_buf);
148 if (retval)
149 return retval;
150 inode = &inode_buf;
151 }
152 addr_per_block = (blk_t) fs->blocksize >> 2;
153
154 if (!block_buf) {
155 retval = ext2fs_get_mem(fs->blocksize * 2, &buf);
156 if (retval)
157 return retval;
158 block_buf = buf;
159 }
160
161 if (block < EXT2_NDIR_BLOCKS) {
162 if (bmap_flags & BMAP_SET) {
163 b = *phys_blk;
Rob Landley7c94bed2006-05-03 21:58:45 +0000164#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000165 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
166 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
167 b = ext2fs_swab32(b);
168#endif
169 inode_bmap(inode, block) = b;
170 inode_dirty++;
171 goto done;
172 }
173
174 *phys_blk = inode_bmap(inode, block);
175 b = block ? inode_bmap(inode, block-1) : 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000176
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000177 if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
178 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
179 if (retval)
180 goto done;
181 inode_bmap(inode, block) = b;
182 blocks_alloc++;
183 *phys_blk = b;
184 }
185 goto done;
186 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000187
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000188 /* Indirect block */
189 block -= EXT2_NDIR_BLOCKS;
190 if (block < addr_per_block) {
191 b = inode_bmap(inode, EXT2_IND_BLOCK);
192 if (!b) {
193 if (!(bmap_flags & BMAP_ALLOC)) {
194 if (bmap_flags & BMAP_SET)
195 retval = EXT2_ET_SET_BMAP_NO_IND;
196 goto done;
197 }
198
199 b = inode_bmap(inode, EXT2_IND_BLOCK-1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000200 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000201 if (retval)
202 goto done;
203 inode_bmap(inode, EXT2_IND_BLOCK) = b;
204 blocks_alloc++;
205 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000206 retval = block_ind_bmap(fs, bmap_flags, b, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000207 &blocks_alloc, block, phys_blk);
208 goto done;
209 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000210
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000211 /* Doubly indirect block */
212 block -= addr_per_block;
213 if (block < addr_per_block * addr_per_block) {
214 b = inode_bmap(inode, EXT2_DIND_BLOCK);
215 if (!b) {
216 if (!(bmap_flags & BMAP_ALLOC)) {
217 if (bmap_flags & BMAP_SET)
218 retval = EXT2_ET_SET_BMAP_NO_IND;
219 goto done;
220 }
221
222 b = inode_bmap(inode, EXT2_IND_BLOCK);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000223 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000224 if (retval)
225 goto done;
226 inode_bmap(inode, EXT2_DIND_BLOCK) = b;
227 blocks_alloc++;
228 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000229 retval = block_dind_bmap(fs, bmap_flags, b, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000230 &blocks_alloc, block, phys_blk);
231 goto done;
232 }
233
234 /* Triply indirect block */
235 block -= addr_per_block * addr_per_block;
236 b = inode_bmap(inode, EXT2_TIND_BLOCK);
237 if (!b) {
238 if (!(bmap_flags & BMAP_ALLOC)) {
239 if (bmap_flags & BMAP_SET)
240 retval = EXT2_ET_SET_BMAP_NO_IND;
241 goto done;
242 }
243
244 b = inode_bmap(inode, EXT2_DIND_BLOCK);
245 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
246 if (retval)
247 goto done;
248 inode_bmap(inode, EXT2_TIND_BLOCK) = b;
249 blocks_alloc++;
250 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000251 retval = block_tind_bmap(fs, bmap_flags, b, block_buf,
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000252 &blocks_alloc, block, phys_blk);
253done:
Rob Landleye7c43b62006-03-01 16:39:45 +0000254 ext2fs_free_mem(&buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000255 if ((retval == 0) && (blocks_alloc || inode_dirty)) {
256 inode->i_blocks += (blocks_alloc * fs->blocksize) / 512;
257 retval = ext2fs_write_inode(fs, ino, inode);
258 }
259 return retval;
260}
261
262
263