blob: fbcb37538960501259dd6f443ee108deec467c58 [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%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o30fab291997-10-25 22:37:42 +00009 * %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'oefc6f622008-08-27 23:07:54 -040029 struct ext2_inode *inode,
Theodore Ts'o30fab291997-10-25 22:37:42 +000030 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'oefc6f622008-08-27 23:07:54 -040035static _BMAP_INLINE_ errcode_t block_ind_bmap(ext2_filsys fs, int flags,
36 blk_t ind, char *block_buf,
Theodore Ts'o30fab291997-10-25 22:37:42 +000037 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'oefc6f622008-08-27 23:07:54 -040093 blk_t dind, char *block_buf,
Theodore Ts'o30fab291997-10-25 22:37:42 +000094 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'oefc6f622008-08-27 23:07:54 -0400100
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'oefc6f622008-08-27 23:07:54 -0400103 retval = block_ind_bmap(fs, flags & ~BMAP_SET, dind, block_buf,
Theodore Ts'o1d667532004-12-23 13:55:34 -0500104 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'oefc6f622008-08-27 23:07:54 -0400113 blk_t tind, char *block_buf,
Theodore Ts'o30fab291997-10-25 22:37:42 +0000114 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'oefc6f622008-08-27 23:07:54 -0400120
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'oefc6f622008-08-27 23:07:54 -0400123 retval = block_dind_bmap(fs, flags & ~BMAP_SET, tind, block_buf,
Theodore Ts'o1d667532004-12-23 13:55:34 -0500124 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
number965284b239a2009-05-19 13:34:12 -0700163 retval = ext2fs_extent_open2(fs, ino, inode, &handle);
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500164 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)
Theodore Ts'o9033faf2008-08-27 18:37:11 -0400175 goto got_block;
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500176 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'o9033faf2008-08-27 18:37:11 -0400187 got_block:
Theodore Ts'oec9d6dd2008-05-27 07:00:48 -0400188 if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
189 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
190 if (retval)
191 goto done;
192 retval = ext2fs_extent_set_bmap(handle, block,
193 (blk64_t) b, 0);
194 if (retval)
195 goto done;
Theodore Ts'o9033faf2008-08-27 18:37:11 -0400196 /* Update inode after setting extent */
197 retval = ext2fs_read_inode(fs, ino, inode);
198 if (retval)
199 return retval;
Theodore Ts'oec9d6dd2008-05-27 07:00:48 -0400200 blocks_alloc++;
201 *phys_blk = b;
202 }
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500203 retval = 0;
204 goto done;
205 }
206
Theodore Ts'o30fab291997-10-25 22:37:42 +0000207 if (!block_buf) {
Theodore Ts'oee010792007-11-09 19:01:06 -0500208 retval = ext2fs_get_array(2, fs->blocksize, &buf);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000209 if (retval)
210 return retval;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000211 block_buf = buf;
212 }
213
214 if (block < EXT2_NDIR_BLOCKS) {
Theodore Ts'o1d667532004-12-23 13:55:34 -0500215 if (bmap_flags & BMAP_SET) {
216 b = *phys_blk;
Theodore Ts'o1d667532004-12-23 13:55:34 -0500217 inode_bmap(inode, block) = b;
218 inode_dirty++;
219 goto done;
220 }
221
Theodore Ts'o30fab291997-10-25 22:37:42 +0000222 *phys_blk = inode_bmap(inode, block);
223 b = block ? inode_bmap(inode, block-1) : 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400224
Theodore Ts'o30fab291997-10-25 22:37:42 +0000225 if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
226 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
227 if (retval)
228 goto done;
229 inode_bmap(inode, block) = b;
230 blocks_alloc++;
231 *phys_blk = b;
232 }
233 goto done;
234 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400235
Theodore Ts'o30fab291997-10-25 22:37:42 +0000236 /* Indirect block */
237 block -= EXT2_NDIR_BLOCKS;
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500238 blk32 = *phys_blk;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000239 if (block < addr_per_block) {
240 b = inode_bmap(inode, EXT2_IND_BLOCK);
241 if (!b) {
Theodore Ts'o1d667532004-12-23 13:55:34 -0500242 if (!(bmap_flags & BMAP_ALLOC)) {
243 if (bmap_flags & BMAP_SET)
244 retval = EXT2_ET_SET_BMAP_NO_IND;
245 goto done;
246 }
Theodore Ts'o30fab291997-10-25 22:37:42 +0000247
248 b = inode_bmap(inode, EXT2_IND_BLOCK-1);
249 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
250 if (retval)
251 goto done;
252 inode_bmap(inode, EXT2_IND_BLOCK) = b;
253 blocks_alloc++;
254 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400255 retval = block_ind_bmap(fs, bmap_flags, b, block_buf,
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500256 &blocks_alloc, block, &blk32);
257 if (retval == 0)
258 *phys_blk = blk32;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000259 goto done;
260 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400261
Theodore Ts'o30fab291997-10-25 22:37:42 +0000262 /* Doubly indirect block */
263 block -= addr_per_block;
264 if (block < addr_per_block * addr_per_block) {
265 b = inode_bmap(inode, EXT2_DIND_BLOCK);
266 if (!b) {
Theodore Ts'o1d667532004-12-23 13:55:34 -0500267 if (!(bmap_flags & BMAP_ALLOC)) {
268 if (bmap_flags & BMAP_SET)
269 retval = EXT2_ET_SET_BMAP_NO_IND;
270 goto done;
271 }
Theodore Ts'o30fab291997-10-25 22:37:42 +0000272
273 b = inode_bmap(inode, EXT2_IND_BLOCK);
274 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
275 if (retval)
276 goto done;
277 inode_bmap(inode, EXT2_DIND_BLOCK) = b;
278 blocks_alloc++;
279 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400280 retval = block_dind_bmap(fs, bmap_flags, b, block_buf,
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500281 &blocks_alloc, block, &blk32);
282 if (retval == 0)
283 *phys_blk = blk32;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000284 goto done;
285 }
286
287 /* Triply indirect block */
288 block -= addr_per_block * addr_per_block;
289 b = inode_bmap(inode, EXT2_TIND_BLOCK);
290 if (!b) {
Theodore Ts'o1d667532004-12-23 13:55:34 -0500291 if (!(bmap_flags & BMAP_ALLOC)) {
292 if (bmap_flags & BMAP_SET)
293 retval = EXT2_ET_SET_BMAP_NO_IND;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000294 goto done;
Theodore Ts'o1d667532004-12-23 13:55:34 -0500295 }
Theodore Ts'o30fab291997-10-25 22:37:42 +0000296
297 b = inode_bmap(inode, EXT2_DIND_BLOCK);
298 retval = ext2fs_alloc_block(fs, b, block_buf, &b);
299 if (retval)
300 goto done;
301 inode_bmap(inode, EXT2_TIND_BLOCK) = b;
302 blocks_alloc++;
303 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400304 retval = block_tind_bmap(fs, bmap_flags, b, block_buf,
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500305 &blocks_alloc, block, &blk32);
306 if (retval == 0)
307 *phys_blk = blk32;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000308done:
309 if (buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400310 ext2fs_free_mem(&buf);
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500311 if (handle)
312 ext2fs_extent_free(handle);
Theodore Ts'o1d667532004-12-23 13:55:34 -0500313 if ((retval == 0) && (blocks_alloc || inode_dirty)) {
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400314 ext2fs_iblk_add_blocks(fs, inode, blocks_alloc);
Theodore Ts'o30fab291997-10-25 22:37:42 +0000315 retval = ext2fs_write_inode(fs, ino, inode);
316 }
317 return retval;
318}
319
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500320errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
321 char *block_buf, int bmap_flags, blk_t block,
322 blk_t *phys_blk)
323{
324 errcode_t ret;
Theodore Ts'o9c9e1d52009-11-29 00:08:54 -0500325 blk64_t ret_blk = *phys_blk;
Theodore Ts'o30fab291997-10-25 22:37:42 +0000326
Theodore Ts'occ9bf5d2008-02-18 14:59:45 -0500327 ret = ext2fs_bmap2(fs, ino, inode, block_buf, bmap_flags, block,
328 0, &ret_blk);
329 if (ret)
330 return ret;
331 if (ret_blk >= ((long long) 1 << 32))
332 return EOVERFLOW;
333 *phys_blk = ret_blk;
334 return 0;
335}