[829d238] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Frantisek Princ
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libext4
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file libext4_extent.c
|
---|
[c25e39b] | 35 | * @brief Ext4 extent structures operations.
|
---|
[829d238] | 36 | */
|
---|
| 37 |
|
---|
[acd869e] | 38 | #include <byteorder.h>
|
---|
[e2629b08] | 39 | #include <errno.h>
|
---|
| 40 | #include <malloc.h>
|
---|
| 41 | #include "libext4.h"
|
---|
[829d238] | 42 |
|
---|
[8958a26] | 43 | uint32_t ext4_extent_get_first_block(ext4_extent_t *extent)
|
---|
| 44 | {
|
---|
| 45 | return uint32_t_le2host(extent->first_block);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[343ccfd] | 48 | void ext4_extent_set_first_block(ext4_extent_t *extent, uint32_t first_block)
|
---|
| 49 | {
|
---|
| 50 | extent->first_block = host2uint32_t_le(first_block);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[8958a26] | 53 | uint16_t ext4_extent_get_block_count(ext4_extent_t *extent)
|
---|
| 54 | {
|
---|
| 55 | return uint16_t_le2host(extent->block_count);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[343ccfd] | 58 | void ext4_extent_set_block_count(ext4_extent_t *extent, uint16_t block_count)
|
---|
| 59 | {
|
---|
| 60 | extent->block_count = host2uint16_t_le(block_count);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[8958a26] | 63 | uint64_t ext4_extent_get_start(ext4_extent_t *extent)
|
---|
| 64 | {
|
---|
| 65 | return ((uint64_t)uint16_t_le2host(extent->start_hi)) << 32 |
|
---|
| 66 | ((uint64_t)uint32_t_le2host(extent->start_lo));
|
---|
[343ccfd] | 67 | }
|
---|
[8958a26] | 68 |
|
---|
[343ccfd] | 69 | void ext4_extent_set_start(ext4_extent_t *extent, uint64_t start)
|
---|
| 70 | {
|
---|
| 71 | extent->start_lo = host2uint32_t_le((start << 32) >> 32);
|
---|
| 72 | extent->start_hi = host2uint16_t_le((uint16_t)(start >> 32));
|
---|
[8958a26] | 73 | }
|
---|
| 74 |
|
---|
[1a7756a] | 75 | uint32_t ext4_extent_index_get_first_block(ext4_extent_index_t *index)
|
---|
| 76 | {
|
---|
| 77 | return uint32_t_le2host(index->first_block);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[343ccfd] | 80 | void ext4_extent_index_set_first_block(ext4_extent_index_t *index,
|
---|
| 81 | uint32_t first)
|
---|
| 82 | {
|
---|
| 83 | index->first_block = host2uint32_t_le(first);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[1a7756a] | 86 | uint64_t ext4_extent_index_get_leaf(ext4_extent_index_t *index)
|
---|
| 87 | {
|
---|
| 88 | return ((uint64_t)uint16_t_le2host(index->leaf_hi)) << 32 |
|
---|
[343ccfd] | 89 | ((uint64_t)uint32_t_le2host(index->leaf_lo));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | void ext4_extent_index_set_leaf(ext4_extent_index_t *index, uint64_t leaf)
|
---|
| 93 | {
|
---|
| 94 | index->leaf_lo = host2uint32_t_le((leaf << 32) >> 32);
|
---|
| 95 | index->leaf_hi = host2uint16_t_le((uint16_t)(leaf >> 32));
|
---|
[1a7756a] | 96 | }
|
---|
| 97 |
|
---|
[acd869e] | 98 | uint16_t ext4_extent_header_get_magic(ext4_extent_header_t *header)
|
---|
| 99 | {
|
---|
| 100 | return uint16_t_le2host(header->magic);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[343ccfd] | 103 | void ext4_extent_header_set_magic(ext4_extent_header_t *header, uint16_t magic)
|
---|
| 104 | {
|
---|
| 105 | header->magic = host2uint16_t_le(magic);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[acd869e] | 108 | uint16_t ext4_extent_header_get_entries_count(ext4_extent_header_t *header)
|
---|
| 109 | {
|
---|
| 110 | return uint16_t_le2host(header->entries_count);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[343ccfd] | 113 | void ext4_extent_header_set_entries_count(ext4_extent_header_t *header,
|
---|
| 114 | uint16_t count)
|
---|
| 115 | {
|
---|
| 116 | header->entries_count = host2uint16_t_le(count);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[acd869e] | 119 | uint16_t ext4_extent_header_get_max_entries_count(ext4_extent_header_t *header)
|
---|
| 120 | {
|
---|
| 121 | return uint16_t_le2host(header->max_entries_count);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[343ccfd] | 124 | void ext4_extent_header_set_max_entries_count(ext4_extent_header_t *header,
|
---|
| 125 | uint16_t max_count)
|
---|
| 126 | {
|
---|
| 127 | header->max_entries_count = host2uint16_t_le(max_count);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[acd869e] | 130 | uint16_t ext4_extent_header_get_depth(ext4_extent_header_t *header)
|
---|
| 131 | {
|
---|
| 132 | return uint16_t_le2host(header->depth);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[343ccfd] | 135 | void ext4_extent_header_set_depth(ext4_extent_header_t *header, uint16_t depth)
|
---|
| 136 | {
|
---|
| 137 | header->depth = host2uint16_t_le(depth);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[acd869e] | 140 | uint32_t ext4_extent_header_get_generation(ext4_extent_header_t *header)
|
---|
| 141 | {
|
---|
| 142 | return uint32_t_le2host(header->generation);
|
---|
| 143 | }
|
---|
[829d238] | 144 |
|
---|
[343ccfd] | 145 | void ext4_extent_header_set_generation(ext4_extent_header_t *header,
|
---|
| 146 | uint32_t generation)
|
---|
| 147 | {
|
---|
| 148 | header->generation = host2uint32_t_le(generation);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[fffb061] | 151 | /**
|
---|
| 152 | * Binary search in extent index node
|
---|
| 153 | */
|
---|
[47faec1] | 154 | static void ext4_extent_binsearch_idx(ext4_extent_header_t *header,
|
---|
| 155 | ext4_extent_index_t **index, uint32_t iblock)
|
---|
| 156 | {
|
---|
| 157 | ext4_extent_index_t *r, *l, *m;
|
---|
| 158 |
|
---|
| 159 | uint16_t entries_count = ext4_extent_header_get_entries_count(header);
|
---|
| 160 |
|
---|
| 161 | if (entries_count == 1) {
|
---|
| 162 | *index = EXT4_EXTENT_FIRST_INDEX(header);
|
---|
| 163 | return;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | l = EXT4_EXTENT_FIRST_INDEX(header) + 1;
|
---|
| 167 | r = l + entries_count - 1;
|
---|
| 168 |
|
---|
| 169 | while (l <= r) {
|
---|
| 170 | m = l + (r - l) / 2;
|
---|
| 171 | uint32_t first_block = ext4_extent_index_get_first_block(m);
|
---|
| 172 | if (iblock < first_block) {
|
---|
| 173 | r = m - 1;
|
---|
| 174 | } else {
|
---|
| 175 | l = m + 1;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | *index = l - 1;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[fffb061] | 182 | /**
|
---|
| 183 | * Binary search in extent leaf node
|
---|
| 184 | */
|
---|
[30ac3c3] | 185 | static void ext4_extent_binsearch(ext4_extent_header_t *header,
|
---|
| 186 | ext4_extent_t **extent, uint32_t iblock)
|
---|
[a4419e7] | 187 | {
|
---|
| 188 | ext4_extent_t *r, *l, *m;
|
---|
| 189 |
|
---|
| 190 | uint16_t entries_count = ext4_extent_header_get_entries_count(header);
|
---|
| 191 |
|
---|
| 192 | if (entries_count == 0) {
|
---|
| 193 | // this leaf is empty
|
---|
[47faec1] | 194 | EXT4FS_DBG("EMPTY LEAF");
|
---|
| 195 | *extent = NULL;
|
---|
| 196 | return;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | if (entries_count == 1) {
|
---|
| 200 | *extent = EXT4_EXTENT_FIRST(header);
|
---|
[a4419e7] | 201 | return;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | l = EXT4_EXTENT_FIRST(header) + 1;
|
---|
| 205 | r = l + entries_count - 1;
|
---|
[9104bb5] | 206 |
|
---|
[0d4db0f] | 207 | while (l < r) {
|
---|
[a4419e7] | 208 | m = l + (r - l) / 2;
|
---|
[47faec1] | 209 | uint32_t first_block = ext4_extent_get_first_block(m);
|
---|
| 210 | if (iblock < first_block) {
|
---|
[a4419e7] | 211 | r = m - 1;
|
---|
| 212 | } else {
|
---|
| 213 | l = m + 1;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[30ac3c3] | 217 | *extent = l - 1;
|
---|
[a4419e7] | 218 | }
|
---|
| 219 |
|
---|
[1ac1ab4] | 220 | // Reading routine without saving blocks to path - for saving memory during finding block
|
---|
| 221 | int ext4_extent_find_block(ext4_inode_ref_t *inode_ref, uint32_t iblock, uint32_t *fblock)
|
---|
[e2629b08] | 222 | {
|
---|
[9104bb5] | 223 | int rc;
|
---|
[e2629b08] | 224 |
|
---|
[1ac1ab4] | 225 | block_t* block = NULL;
|
---|
[e2629b08] | 226 |
|
---|
[1ac1ab4] | 227 | ext4_extent_header_t *header = ext4_inode_get_extent_header(inode_ref->inode);
|
---|
| 228 | while (ext4_extent_header_get_depth(header) != 0) {
|
---|
[e2629b08] | 229 |
|
---|
[1ac1ab4] | 230 | ext4_extent_index_t *index;
|
---|
| 231 | ext4_extent_binsearch_idx(header, &index, iblock);
|
---|
[fffb061] | 232 |
|
---|
[1ac1ab4] | 233 | uint64_t child = ext4_extent_index_get_leaf(index);
|
---|
[fffb061] | 234 |
|
---|
[1ac1ab4] | 235 | if (block != NULL) {
|
---|
| 236 | block_put(block);
|
---|
| 237 | }
|
---|
[e2629b08] | 238 |
|
---|
[1ac1ab4] | 239 | rc = block_get(&block, inode_ref->fs->device, child, BLOCK_FLAGS_NONE);
|
---|
[47faec1] | 240 | if (rc != EOK) {
|
---|
| 241 | return rc;
|
---|
| 242 | }
|
---|
[e2629b08] | 243 |
|
---|
[1ac1ab4] | 244 | header = (ext4_extent_header_t *)block->data;
|
---|
[47faec1] | 245 | }
|
---|
[e2629b08] | 246 |
|
---|
| 247 |
|
---|
[0d4db0f] | 248 | ext4_extent_t* extent = NULL;
|
---|
[1ac1ab4] | 249 | ext4_extent_binsearch(header, &extent, iblock);
|
---|
[e2629b08] | 250 |
|
---|
[0d4db0f] | 251 | assert(extent != NULL);
|
---|
[fffb061] | 252 |
|
---|
[9104bb5] | 253 | uint32_t phys_block;
|
---|
| 254 | phys_block = ext4_extent_get_start(extent) + iblock;
|
---|
| 255 | phys_block -= ext4_extent_get_first_block(extent);
|
---|
[e2629b08] | 256 |
|
---|
[9104bb5] | 257 | *fblock = phys_block;
|
---|
[e2629b08] | 258 |
|
---|
[1ac1ab4] | 259 | if (block != NULL) {
|
---|
| 260 | block_put(block);
|
---|
[fffb061] | 261 | }
|
---|
[9104bb5] | 262 |
|
---|
[fffb061] | 263 |
|
---|
| 264 | return EOK;
|
---|
[e2629b08] | 265 | }
|
---|
| 266 |
|
---|
[0d4db0f] | 267 | static int ext4_extent_find_extent(ext4_inode_ref_t *inode_ref,
|
---|
| 268 | uint32_t iblock, ext4_extent_path_t **ret_path)
|
---|
| 269 | {
|
---|
| 270 | int rc;
|
---|
| 271 |
|
---|
| 272 | ext4_extent_header_t *eh =
|
---|
| 273 | ext4_inode_get_extent_header(inode_ref->inode);
|
---|
| 274 |
|
---|
| 275 | uint16_t depth = ext4_extent_header_get_depth(eh);
|
---|
| 276 |
|
---|
| 277 | ext4_extent_path_t *tmp_path;
|
---|
| 278 |
|
---|
| 279 | // Added 2 for possible tree growing
|
---|
| 280 | tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
|
---|
| 281 | if (tmp_path == NULL) {
|
---|
| 282 | return ENOMEM;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | tmp_path[0].block = inode_ref->block;
|
---|
| 286 | tmp_path[0].header = eh;
|
---|
| 287 |
|
---|
| 288 | uint16_t pos = 0;
|
---|
| 289 | while (ext4_extent_header_get_depth(eh) != 0) {
|
---|
| 290 |
|
---|
| 291 | ext4_extent_binsearch_idx(tmp_path[pos].header, &tmp_path[pos].index, iblock);
|
---|
| 292 |
|
---|
| 293 | tmp_path[pos].depth = depth;
|
---|
| 294 | tmp_path[pos].extent = NULL;
|
---|
| 295 |
|
---|
| 296 | assert(tmp_path[pos].index != NULL);
|
---|
| 297 |
|
---|
| 298 | uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
|
---|
| 299 |
|
---|
| 300 | block_t *block;
|
---|
| 301 | rc = block_get(&block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 302 | if (rc != EOK) {
|
---|
| 303 | // TODO cleanup
|
---|
| 304 | EXT4FS_DBG("ERRRR");
|
---|
| 305 | return rc;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | pos++;
|
---|
| 309 |
|
---|
| 310 | eh = (ext4_extent_header_t *)block->data;
|
---|
| 311 | tmp_path[pos].block = block;
|
---|
| 312 | tmp_path[pos].header = eh;
|
---|
| 313 |
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | tmp_path[pos].depth = 0;
|
---|
| 317 | tmp_path[pos].extent = NULL;
|
---|
| 318 | tmp_path[pos].index = NULL;
|
---|
| 319 |
|
---|
| 320 | /* find extent */
|
---|
| 321 | ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
|
---|
| 322 |
|
---|
| 323 | *ret_path = tmp_path;
|
---|
| 324 |
|
---|
| 325 | return EOK;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | int ext4_extent_release_block(ext4_inode_ref_t *inode_ref, uint32_t iblock)
|
---|
| 329 | {
|
---|
| 330 | int rc;
|
---|
| 331 |
|
---|
| 332 | ext4_extent_path_t *path;
|
---|
| 333 | rc = ext4_extent_find_extent(inode_ref, iblock, &path);
|
---|
| 334 | if (rc != EOK) {
|
---|
| 335 | return rc;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | ext4_extent_path_t *path_ptr = path;
|
---|
| 339 | while (path_ptr->depth != 0) {
|
---|
| 340 | EXT4FS_DBG("depth = \%u", path_ptr->depth);
|
---|
| 341 | path_ptr++;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | assert(path_ptr->extent != NULL);
|
---|
| 345 |
|
---|
| 346 | uint32_t fblock;
|
---|
| 347 | fblock = ext4_extent_get_start(path_ptr->extent) + iblock;
|
---|
| 348 | fblock -= ext4_extent_get_first_block(path_ptr->extent);
|
---|
| 349 |
|
---|
| 350 | uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
|
---|
| 351 |
|
---|
| 352 | assert((ext4_extent_get_first_block(path_ptr->extent) + block_count - 1) == iblock);
|
---|
| 353 |
|
---|
| 354 | block_count--;
|
---|
| 355 | ext4_extent_set_block_count(path_ptr->extent, block_count);
|
---|
| 356 |
|
---|
[001307cf] | 357 | path_ptr->block->dirty = true;
|
---|
| 358 |
|
---|
| 359 | bool check_tree = false;
|
---|
| 360 |
|
---|
[0d4db0f] | 361 | if (block_count == 0) {
|
---|
| 362 | uint16_t entries = ext4_extent_header_get_entries_count(path_ptr->header);
|
---|
| 363 | entries--;
|
---|
| 364 | ext4_extent_header_set_entries_count(path_ptr->header, entries);
|
---|
| 365 |
|
---|
[001307cf] | 366 | // If empty leaf, will be released and the whole tree must be checked
|
---|
| 367 | check_tree = true;
|
---|
[0d4db0f] | 368 | }
|
---|
| 369 |
|
---|
[001307cf] | 370 | while (check_tree) {
|
---|
| 371 |
|
---|
| 372 | if (path_ptr > path) {
|
---|
| 373 | // TODO
|
---|
| 374 |
|
---|
| 375 | // zahodit fblock
|
---|
| 376 | rc = ext4_balloc_free_block(inode_ref, path_ptr->block->pba);
|
---|
| 377 | if (rc != EOK) {
|
---|
| 378 | EXT4FS_DBG("ERROR");
|
---|
| 379 | // TODO
|
---|
| 380 | }
|
---|
| 381 | } else {
|
---|
| 382 | check_tree = false;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | path_ptr--;
|
---|
| 386 |
|
---|
| 387 | uint16_t entries = ext4_extent_header_get_entries_count(path_ptr->header);
|
---|
| 388 | entries--;
|
---|
| 389 | ext4_extent_header_set_entries_count(path_ptr->header, entries);
|
---|
| 390 |
|
---|
| 391 | if (entries > 0) {
|
---|
| 392 | check_tree = false;
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
[0d4db0f] | 395 |
|
---|
| 396 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
| 397 | if (rc != EOK) {
|
---|
[001307cf] | 398 | EXT4FS_DBG("ERROR");
|
---|
[0d4db0f] | 399 | // TODO handle error
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | uint16_t depth = path->depth;
|
---|
| 403 |
|
---|
| 404 | // Put loaded blocks
|
---|
| 405 | // From 1 -> 0 is a block with inode data
|
---|
| 406 | for (uint16_t i = 1; i < depth; ++i) {
|
---|
| 407 | if (path[i].block) {
|
---|
| 408 | block_put(path[i].block);
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | // Destroy temporary data structure
|
---|
| 413 | free(path);
|
---|
| 414 |
|
---|
| 415 |
|
---|
| 416 | return EOK;
|
---|
| 417 | }
|
---|
[1ac1ab4] | 418 |
|
---|
[829d238] | 419 | /**
|
---|
| 420 | * @}
|
---|
| 421 | */
|
---|