blob: e56a99acb537bcf1939557df004234256ed0aadb [file] [log] [blame]
Theodore Ts'o30fab291997-10-25 22:37:42 +00001/*
Theodore Ts'o80e808f2000-02-02 16:19:59 +00002 * bmap.c --- logical to physical block mapping
Theodore Ts'o30fab291997-10-25 22:37:42 +00003 *
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
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -050017#include <errno.h>
Theodore Ts'o30fab291997-10-25 22:37:42 +000018
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000019#include "ext2_fs.h"
Theodore Ts'o30fab291997-10-25 22:37:42 +000020#include "ext2fs.h"
21
Theodore Ts'o78d8f901997-10-26 01:53:39 +000022#if defined(__GNUC__) && !defined(NO_INLINE_FUNCS)
Theodore Ts'o30fab291997-10-25 22:37:42 +000023#define _BMAP_INLINE_ __inline__
24#else
25#define _BMAP_INLINE_
26#endif
27
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000028extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o30fab291997-10-25 22:37:42 +000029 struct ext2_inode *inode,
30 char *block_buf, int bmap_flags,
31 blk_t block, blk_t *phys_blk);
32
Theodore Ts'o30fab291997-10-25 22:37:42 +000033#define inode_bmap(inode, nr) ((inode)->i_block[(nr)])
34
Theodore Ts'o546a1ff2002-03-07 23:52:56 -050035static _BMAP_INLINE_ errcode_t block_ind_bmap(ext2_filsys fs, int flags,
Theodore Ts'o30fab291997-10-25 22:37:42 +000036 blk_t ind, char *block_buf,
37 int *blocks_alloc,
38 blk_t nr, blk_t *ret_blk)
39{
40 errcode_t retval;
41 blk_t b;
42
43 if (!ind) {
Theodore Ts'o1d667532004-12-23 13:55:34 -050044 if (flags & BMAP_SET)
45 return EXT2_ET_SET_BMAP_NO_IND;
Theodore Ts'o30fab291997-10-25 22:37:42 +000046 *ret_blk = 0;
47 return 0;
48 }
49 retval = io_channel_read_blk(fs->io, ind, 1, block_buf);
50 if (retval)
51 return retval;
52
Theodore Ts'o1d667532004-12-23 13:55:34 -050053 if (flags & BMAP_SET) {
54 b = *ret_blk;
Theodore Ts'o126a2912007-08-11 01:56:48 -040055#ifdef WORDS_BIGENDIAN
56 b = ext2fs_swab32(b);
Theodore Ts'o1d667532004-12-23 13:55:34 -050057#endif
58 ((blk_t *) block_buf)[nr] = b;
59 return io_channel_write_blk(fs->io, ind, 1, block_buf);
60 }
61
Theodore Ts'o30fab291997-10-25 22:37:42 +000062 b = ((blk_t *) block_buf)[nr];
63
Theodore Ts'o126a2912007-08-11 01:56:48 -040064#ifdef WORDS_BIGENDIAN
65 b = ext2fs_swab32(b);
Theodore Ts'o5df55d72001-06-11 07:00:04 +000066#endif
Theodore Ts'o30fab291997-10-25 22:37:42 +000067
68 if (!b && (flags & BMAP_ALLOC)) {
69 b = nr ? ((blk_t *) block_buf)[nr-1] : 0;
70 retval = ext2fs_alloc_block(fs, b,
71 block_buf + fs->blocksize, &b);
72 if (retval)
73 return retval;
74
Theodore Ts'o126a2912007-08-11 01:56:48 -040075#ifdef WORDS_BIGENDIAN
76 ((blk_t *) block_buf)[nr] = ext2fs_swab32(b);
77#else
78 ((blk_t *) block_buf)[nr] = b;
Theodore Ts'o5df55d72001-06-11 07:00:04 +000079#endif
Theodore Ts'o30fab291997-10-25 22:37:42 +000080
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
Theodore Ts'o546a1ff2002-03-07 23:52:56 -050092static _BMAP_INLINE_ errcode_t block_dind_bmap(ext2_filsys fs, int flags,
Theodore Ts'o30fab291997-10-25 22:37:42 +000093 blk_t dind, char *block_buf,
94 int *blocks_alloc,
95 blk_t nr, blk_t *ret_blk)
96{
97 blk_t b;
98 errcode_t retval;
Theodore Ts'o2eb374c1998-09-03 01:22:57 +000099 blk_t addr_per_block;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000100
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000101 addr_per_block = (blk_t) fs->blocksize >> 2;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000102
Theodore Ts'o1d667532004-12-23 13:55:34 -0500103 retval = block_ind_bmap(fs, flags & ~BMAP_SET, dind, block_buf,
104 blocks_alloc, nr / addr_per_block, &b);
Theodore Ts'o30fab291997-10-25 22:37:42 +0000105 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
Theodore Ts'o546a1ff2002-03-07 23:52:56 -0500112static _BMAP_INLINE_ errcode_t block_tind_bmap(ext2_filsys fs, int flags,
Theodore Ts'o30fab291997-10-25 22:37:42 +0000113 blk_t tind, char *block_buf,
114 int *blocks_alloc,
115 blk_t nr, blk_t *ret_blk)
116{
117 blk_t b;
118 errcode_t retval;
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000119 blk_t addr_per_block;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000120
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000121 addr_per_block = (blk_t) fs->blocksize >> 2;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000122
Theodore Ts'o1d667532004-12-23 13:55:34 -0500123 retval = block_dind_bmap(fs, flags & ~BMAP_SET, tind, block_buf,
124 blocks_alloc, nr / addr_per_block, &b);
Theodore Ts'o30fab291997-10-25 22:37:42 +0000125 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
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500132errcode_t ext2fs_bmap2(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
133 char *block_buf, int bmap_flags, blk64_t block,
134 int *ret_flags, blk64_t *phys_blk)
Theodore Ts'o30fab291997-10-25 22:37:42 +0000135{
136 struct ext2_inode inode_buf;
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500137 ext2_extent_handle_t handle = 0;
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000138 blk_t addr_per_block;
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500139 blk_t b, blk32;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000140 char *buf = 0;
141 errcode_t retval = 0;
Theodore Ts'o1d667532004-12-23 13:55:34 -0500142 int blocks_alloc = 0, inode_dirty = 0;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000143
Theodore Ts'o1d667532004-12-23 13:55:34 -0500144 if (!(bmap_flags & BMAP_SET))
145 *phys_blk = 0;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000146
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500147 if (ret_flags)
148 *ret_flags = 0;
149
Theodore Ts'o30fab291997-10-25 22:37:42 +0000150 /* Read inode structure if necessary */
151 if (!inode) {
152 retval = ext2fs_read_inode(fs, ino, &inode_buf);
Theodore Ts'ob38cd282002-05-11 22:13:20 -0400153 if (retval)
Theodore Ts'o30fab291997-10-25 22:37:42 +0000154 return retval;
155 inode = &inode_buf;
156 }
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000157 addr_per_block = (blk_t) fs->blocksize >> 2;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000158
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500159 if (inode->i_flags & EXT4_EXTENTS_FL) {
160 struct ext2fs_extent extent;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400161 unsigned int offset;
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500162
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500163 retval = ext2fs_extent_open(fs, ino, &handle);
164 if (retval)
165 goto done;
Theodore Ts'oec9d6dd2008-05-27 07:00:48 -0400166 if (bmap_flags & BMAP_SET) {
167 retval = ext2fs_extent_set_bmap(handle, block,
168 *phys_blk, 0);
169 goto done;
170 }
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500171 retval = ext2fs_extent_goto(handle, block);
172 if (retval) {
173 /* If the extent is not found, return phys_blk = 0 */
174 if (retval == EXT2_ET_EXTENT_NOT_FOUND)
175 retval = 0;
176 goto done;
177 }
178 retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
179 if (retval)
180 goto done;
181 offset = block - extent.e_lblk;
182 if (block >= extent.e_lblk && (offset <= extent.e_len)) {
183 *phys_blk = extent.e_pblk + offset;
184 if (ret_flags && extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
185 *ret_flags |= BMAP_RET_UNINIT;
186 }
Theodore Ts'oec9d6dd2008-05-27 07:00:48 -0400187 if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
188 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
189 if (retval)
190 goto done;
191 retval = ext2fs_extent_set_bmap(handle, block,
192 (blk64_t) b, 0);
193 if (retval)
194 goto done;
195 blocks_alloc++;
196 *phys_blk = b;
197 }
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500198 retval = 0;
199 goto done;
200 }
201
Theodore Ts'o30fab291997-10-25 22:37:42 +0000202 if (!block_buf) {
Theodore Ts'oee010792007-11-09 19:01:06 -0500203 retval = ext2fs_get_array(2, fs->blocksize, &buf);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000204 if (retval)
205 return retval;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000206 block_buf = buf;
207 }
208
209 if (block < EXT2_NDIR_BLOCKS) {
Theodore Ts'o1d667532004-12-23 13:55:34 -0500210 if (bmap_flags & BMAP_SET) {
211 b = *phys_blk;
Theodore Ts'o126a2912007-08-11 01:56:48 -0400212#ifdef WORDS_BIGENDIAN
213 b = ext2fs_swab32(b);
Theodore Ts'o1d667532004-12-23 13:55:34 -0500214#endif
215 inode_bmap(inode, block) = b;
216 inode_dirty++;
217 goto done;
218 }
219
Theodore Ts'o30fab291997-10-25 22:37:42 +0000220 *phys_blk = inode_bmap(inode, block);
221 b = block ? inode_bmap(inode, block-1) : 0;
222
223 if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
224 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
225 if (retval)
226 goto done;
227 inode_bmap(inode, block) = b;
228 blocks_alloc++;
229 *phys_blk = b;
230 }
231 goto done;
232 }
233
234 /* Indirect block */
235 block -= EXT2_NDIR_BLOCKS;
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500236 blk32 = *phys_blk;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000237 if (block < addr_per_block) {
238 b = inode_bmap(inode, EXT2_IND_BLOCK);
239 if (!b) {
Theodore Ts'o1d667532004-12-23 13:55:34 -0500240 if (!(bmap_flags & BMAP_ALLOC)) {
241 if (bmap_flags & BMAP_SET)
242 retval = EXT2_ET_SET_BMAP_NO_IND;
243 goto done;
244 }
Theodore Ts'o30fab291997-10-25 22:37:42 +0000245
246 b = inode_bmap(inode, EXT2_IND_BLOCK-1);
247 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
248 if (retval)
249 goto done;
250 inode_bmap(inode, EXT2_IND_BLOCK) = b;
251 blocks_alloc++;
252 }
253 retval = block_ind_bmap(fs, bmap_flags, b, block_buf,
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500254 &blocks_alloc, block, &blk32);
255 if (retval == 0)
256 *phys_blk = blk32;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000257 goto done;
258 }
259
260 /* Doubly indirect block */
261 block -= addr_per_block;
262 if (block < addr_per_block * addr_per_block) {
263 b = inode_bmap(inode, EXT2_DIND_BLOCK);
264 if (!b) {
Theodore Ts'o1d667532004-12-23 13:55:34 -0500265 if (!(bmap_flags & BMAP_ALLOC)) {
266 if (bmap_flags & BMAP_SET)
267 retval = EXT2_ET_SET_BMAP_NO_IND;
268 goto done;
269 }
Theodore Ts'o30fab291997-10-25 22:37:42 +0000270
271 b = inode_bmap(inode, EXT2_IND_BLOCK);
272 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
273 if (retval)
274 goto done;
275 inode_bmap(inode, EXT2_DIND_BLOCK) = b;
276 blocks_alloc++;
277 }
278 retval = block_dind_bmap(fs, bmap_flags, b, block_buf,
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500279 &blocks_alloc, block, &blk32);
280 if (retval == 0)
281 *phys_blk = blk32;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000282 goto done;
283 }
284
285 /* Triply indirect block */
286 block -= addr_per_block * addr_per_block;
287 b = inode_bmap(inode, EXT2_TIND_BLOCK);
288 if (!b) {
Theodore Ts'o1d667532004-12-23 13:55:34 -0500289 if (!(bmap_flags & BMAP_ALLOC)) {
290 if (bmap_flags & BMAP_SET)
291 retval = EXT2_ET_SET_BMAP_NO_IND;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000292 goto done;
Theodore Ts'o1d667532004-12-23 13:55:34 -0500293 }
Theodore Ts'o30fab291997-10-25 22:37:42 +0000294
295 b = inode_bmap(inode, EXT2_DIND_BLOCK);
296 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
297 if (retval)
298 goto done;
299 inode_bmap(inode, EXT2_TIND_BLOCK) = b;
300 blocks_alloc++;
301 }
302 retval = block_tind_bmap(fs, bmap_flags, b, block_buf,
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500303 &blocks_alloc, block, &blk32);
304 if (retval == 0)
305 *phys_blk = blk32;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000306done:
307 if (buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400308 ext2fs_free_mem(&buf);
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500309 if (handle)
310 ext2fs_extent_free(handle);
Theodore Ts'o1d667532004-12-23 13:55:34 -0500311 if ((retval == 0) && (blocks_alloc || inode_dirty)) {
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400312 ext2fs_iblk_add_blocks(fs, inode, blocks_alloc);
Theodore Ts'o30fab291997-10-25 22:37:42 +0000313 retval = ext2fs_write_inode(fs, ino, inode);
314 }
315 return retval;
316}
317
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500318errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
319 char *block_buf, int bmap_flags, blk_t block,
320 blk_t *phys_blk)
321{
322 errcode_t ret;
323 blk64_t ret_blk;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000324
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500325 ret = ext2fs_bmap2(fs, ino, inode, block_buf, bmap_flags, block,
326 0, &ret_blk);
327 if (ret)
328 return ret;
329 if (ret_blk >= ((long long) 1 << 32))
330 return EOVERFLOW;
331 *phys_blk = ret_blk;
332 return 0;
333}