[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 |
|
---|
[b73530a] | 225 | uint64_t inode_size = ext4_inode_get_size(
|
---|
| 226 | inode_ref->fs->superblock, inode_ref->inode);
|
---|
| 227 |
|
---|
| 228 | uint32_t block_size = ext4_superblock_get_block_size(
|
---|
| 229 | inode_ref->fs->superblock);
|
---|
| 230 |
|
---|
| 231 | uint32_t last_idx = (inode_size - 1) / block_size;
|
---|
| 232 |
|
---|
| 233 | if (iblock > last_idx) {
|
---|
| 234 | *fblock = 0;
|
---|
| 235 | return EOK;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[1ac1ab4] | 238 | block_t* block = NULL;
|
---|
[e2629b08] | 239 |
|
---|
[1ac1ab4] | 240 | ext4_extent_header_t *header = ext4_inode_get_extent_header(inode_ref->inode);
|
---|
| 241 | while (ext4_extent_header_get_depth(header) != 0) {
|
---|
[e2629b08] | 242 |
|
---|
[1ac1ab4] | 243 | ext4_extent_index_t *index;
|
---|
| 244 | ext4_extent_binsearch_idx(header, &index, iblock);
|
---|
[fffb061] | 245 |
|
---|
[1ac1ab4] | 246 | uint64_t child = ext4_extent_index_get_leaf(index);
|
---|
[fffb061] | 247 |
|
---|
[1ac1ab4] | 248 | if (block != NULL) {
|
---|
| 249 | block_put(block);
|
---|
| 250 | }
|
---|
[e2629b08] | 251 |
|
---|
[1ac1ab4] | 252 | rc = block_get(&block, inode_ref->fs->device, child, BLOCK_FLAGS_NONE);
|
---|
[47faec1] | 253 | if (rc != EOK) {
|
---|
| 254 | return rc;
|
---|
| 255 | }
|
---|
[e2629b08] | 256 |
|
---|
[1ac1ab4] | 257 | header = (ext4_extent_header_t *)block->data;
|
---|
[47faec1] | 258 | }
|
---|
[e2629b08] | 259 |
|
---|
| 260 |
|
---|
[0d4db0f] | 261 | ext4_extent_t* extent = NULL;
|
---|
[1ac1ab4] | 262 | ext4_extent_binsearch(header, &extent, iblock);
|
---|
[e2629b08] | 263 |
|
---|
[1196df6] | 264 | if (extent == NULL) {
|
---|
| 265 | *fblock = 0;
|
---|
| 266 | } else {
|
---|
| 267 | uint32_t phys_block;
|
---|
| 268 | phys_block = ext4_extent_get_start(extent) + iblock;
|
---|
| 269 | phys_block -= ext4_extent_get_first_block(extent);
|
---|
[e2629b08] | 270 |
|
---|
[1196df6] | 271 | *fblock = phys_block;
|
---|
| 272 | }
|
---|
[e2629b08] | 273 |
|
---|
[1ac1ab4] | 274 | if (block != NULL) {
|
---|
| 275 | block_put(block);
|
---|
[fffb061] | 276 | }
|
---|
[9104bb5] | 277 |
|
---|
[fffb061] | 278 | return EOK;
|
---|
[e2629b08] | 279 | }
|
---|
| 280 |
|
---|
[0d4db0f] | 281 | static int ext4_extent_find_extent(ext4_inode_ref_t *inode_ref,
|
---|
| 282 | uint32_t iblock, ext4_extent_path_t **ret_path)
|
---|
| 283 | {
|
---|
| 284 | int rc;
|
---|
| 285 |
|
---|
| 286 | ext4_extent_header_t *eh =
|
---|
| 287 | ext4_inode_get_extent_header(inode_ref->inode);
|
---|
| 288 |
|
---|
| 289 | uint16_t depth = ext4_extent_header_get_depth(eh);
|
---|
| 290 |
|
---|
| 291 | ext4_extent_path_t *tmp_path;
|
---|
| 292 |
|
---|
| 293 | // Added 2 for possible tree growing
|
---|
| 294 | tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
|
---|
| 295 | if (tmp_path == NULL) {
|
---|
| 296 | return ENOMEM;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | tmp_path[0].block = inode_ref->block;
|
---|
| 300 | tmp_path[0].header = eh;
|
---|
| 301 |
|
---|
| 302 | uint16_t pos = 0;
|
---|
| 303 | while (ext4_extent_header_get_depth(eh) != 0) {
|
---|
| 304 |
|
---|
| 305 | ext4_extent_binsearch_idx(tmp_path[pos].header, &tmp_path[pos].index, iblock);
|
---|
| 306 |
|
---|
| 307 | tmp_path[pos].depth = depth;
|
---|
| 308 | tmp_path[pos].extent = NULL;
|
---|
| 309 |
|
---|
| 310 | assert(tmp_path[pos].index != NULL);
|
---|
| 311 |
|
---|
| 312 | uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
|
---|
| 313 |
|
---|
| 314 | block_t *block;
|
---|
| 315 | rc = block_get(&block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 316 | if (rc != EOK) {
|
---|
[6f413125] | 317 | goto cleanup;
|
---|
[0d4db0f] | 318 | }
|
---|
| 319 |
|
---|
| 320 | pos++;
|
---|
| 321 |
|
---|
| 322 | eh = (ext4_extent_header_t *)block->data;
|
---|
| 323 | tmp_path[pos].block = block;
|
---|
| 324 | tmp_path[pos].header = eh;
|
---|
| 325 |
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | tmp_path[pos].depth = 0;
|
---|
| 329 | tmp_path[pos].extent = NULL;
|
---|
| 330 | tmp_path[pos].index = NULL;
|
---|
| 331 |
|
---|
| 332 | /* find extent */
|
---|
| 333 | ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
|
---|
| 334 |
|
---|
| 335 | *ret_path = tmp_path;
|
---|
| 336 |
|
---|
| 337 | return EOK;
|
---|
[6f413125] | 338 |
|
---|
| 339 | cleanup:
|
---|
| 340 | // Put loaded blocks
|
---|
| 341 | // From 1 -> 0 is a block with inode data
|
---|
| 342 | for (uint16_t i = 1; i < tmp_path->depth; ++i) {
|
---|
| 343 | if (tmp_path[i].block) {
|
---|
| 344 | block_put(tmp_path[i].block);
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | // Destroy temporary data structure
|
---|
| 349 | free(tmp_path);
|
---|
| 350 |
|
---|
| 351 | return rc;
|
---|
| 352 |
|
---|
[0d4db0f] | 353 | }
|
---|
| 354 |
|
---|
[5b0a3946] | 355 | static int ext4_extent_release(ext4_inode_ref_t *inode_ref, ext4_extent_t* extent)
|
---|
[0d4db0f] | 356 | {
|
---|
| 357 | int rc;
|
---|
| 358 |
|
---|
[5b0a3946] | 359 | uint64_t start = ext4_extent_get_start(extent);
|
---|
| 360 | uint16_t block_count = ext4_extent_get_block_count(extent);
|
---|
| 361 |
|
---|
| 362 | rc = ext4_balloc_free_blocks(inode_ref, start, block_count);
|
---|
| 363 | if (rc != EOK) {
|
---|
| 364 | EXT4FS_DBG("ERROR");
|
---|
| 365 | return rc;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | return EOK;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | // Recursive release
|
---|
| 372 | static int ext4_extent_release_branch(ext4_inode_ref_t *inode_ref,
|
---|
| 373 | ext4_extent_index_t *index)
|
---|
| 374 | {
|
---|
| 375 | int rc;
|
---|
| 376 |
|
---|
| 377 | block_t* block;
|
---|
| 378 |
|
---|
| 379 | uint32_t fblock = ext4_extent_index_get_leaf(index);
|
---|
| 380 |
|
---|
| 381 | EXT4FS_DBG("fblock = \%u", fblock);
|
---|
| 382 |
|
---|
| 383 | rc = block_get(&block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 384 | if (rc != EOK) {
|
---|
| 385 | EXT4FS_DBG("ERROR get_block");
|
---|
| 386 | return rc;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | ext4_extent_header_t *header = block->data;
|
---|
| 390 |
|
---|
| 391 | if (ext4_extent_header_get_depth(header)) {
|
---|
| 392 |
|
---|
| 393 | ext4_extent_index_t *idx = EXT4_EXTENT_FIRST_INDEX(header);
|
---|
| 394 |
|
---|
| 395 | for (uint32_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i, ++idx) {
|
---|
| 396 | rc = ext4_extent_release_branch(inode_ref, idx);
|
---|
| 397 | if (rc != EOK) {
|
---|
| 398 | EXT4FS_DBG("error recursion");
|
---|
| 399 | return rc;
|
---|
| 400 | }
|
---|
| 401 | }
|
---|
| 402 | } else {
|
---|
| 403 | ext4_extent_t *ext = EXT4_EXTENT_FIRST(header);
|
---|
| 404 |
|
---|
| 405 | for (uint32_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i, ++ext) {
|
---|
| 406 | rc = ext4_extent_release(inode_ref, ext);
|
---|
| 407 | if (rc != EOK) {
|
---|
| 408 | EXT4FS_DBG("error recursion");
|
---|
| 409 | return rc;
|
---|
| 410 | }
|
---|
| 411 | }
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | rc = block_put(block);
|
---|
| 415 | if (rc != EOK) {
|
---|
| 416 | EXT4FS_DBG("ERROR put_block");
|
---|
| 417 | return rc;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | ext4_balloc_free_block(inode_ref, fblock);
|
---|
| 421 |
|
---|
| 422 | return EOK;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[1196df6] | 425 | int ext4_extent_release_blocks_from(ext4_inode_ref_t *inode_ref,
|
---|
| 426 | uint32_t iblock_from)
|
---|
[5b0a3946] | 427 | {
|
---|
[6f413125] | 428 | int rc = EOK;
|
---|
[5b0a3946] | 429 |
|
---|
[3e2952b] | 430 | // Find the first extent to modify
|
---|
[0d4db0f] | 431 | ext4_extent_path_t *path;
|
---|
[5b0a3946] | 432 | rc = ext4_extent_find_extent(inode_ref, iblock_from, &path);
|
---|
[0d4db0f] | 433 | if (rc != EOK) {
|
---|
| 434 | return rc;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
[3e2952b] | 437 | // Jump to last item of the path (extent)
|
---|
[0d4db0f] | 438 | ext4_extent_path_t *path_ptr = path;
|
---|
| 439 | while (path_ptr->depth != 0) {
|
---|
| 440 | path_ptr++;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | assert(path_ptr->extent != NULL);
|
---|
| 444 |
|
---|
[3e2952b] | 445 | // First extent maybe released partially
|
---|
[5b0a3946] | 446 | uint32_t first_fblock;
|
---|
| 447 | first_fblock = ext4_extent_get_start(path_ptr->extent) + iblock_from;
|
---|
| 448 | first_fblock -= ext4_extent_get_first_block(path_ptr->extent);
|
---|
[0d4db0f] | 449 |
|
---|
| 450 | uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
|
---|
| 451 |
|
---|
[5b0a3946] | 452 | uint16_t delete_count = block_count - first_fblock +
|
---|
| 453 | ext4_extent_get_start(path_ptr->extent);
|
---|
| 454 |
|
---|
| 455 | rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
|
---|
[3e2952b] | 456 | if (rc != EOK) {
|
---|
[6f413125] | 457 | goto cleanup;
|
---|
[3e2952b] | 458 | }
|
---|
[0d4db0f] | 459 |
|
---|
[5b0a3946] | 460 | block_count -= delete_count;
|
---|
[0d4db0f] | 461 | ext4_extent_set_block_count(path_ptr->extent, block_count);
|
---|
| 462 |
|
---|
[3e2952b] | 463 | uint16_t entries = ext4_extent_header_get_entries_count(path_ptr->header);
|
---|
| 464 | ext4_extent_t *tmp_ext = path_ptr->extent + 1;
|
---|
| 465 | ext4_extent_t *stop_ext = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
|
---|
[5b0a3946] | 466 |
|
---|
[3e2952b] | 467 | // If first extent empty, release it
|
---|
[0d4db0f] | 468 | if (block_count == 0) {
|
---|
| 469 | entries--;
|
---|
| 470 | ext4_extent_header_set_entries_count(path_ptr->header, entries);
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[3e2952b] | 473 | // Release all successors of the first extent in the same node
|
---|
| 474 | while (tmp_ext < stop_ext) {
|
---|
| 475 | first_fblock = ext4_extent_get_start(tmp_ext);
|
---|
| 476 | delete_count = ext4_extent_get_block_count(tmp_ext);
|
---|
[5b0a3946] | 477 |
|
---|
[3e2952b] | 478 | rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
|
---|
| 479 | if (rc != EOK) {
|
---|
[6f413125] | 480 | goto cleanup;
|
---|
[3e2952b] | 481 | }
|
---|
[5b0a3946] | 482 |
|
---|
[3e2952b] | 483 | entries--;
|
---|
| 484 | ext4_extent_header_set_entries_count(path_ptr->header, entries);
|
---|
[5b0a3946] | 485 |
|
---|
[3e2952b] | 486 | tmp_ext++;
|
---|
| 487 | }
|
---|
[5b0a3946] | 488 |
|
---|
[3e2952b] | 489 | // If leaf node is empty, the whole tree must be checked and the node will be released
|
---|
| 490 | bool check_tree = false;
|
---|
[5b0a3946] | 491 |
|
---|
[3e2952b] | 492 | // Don't release root block (including inode data) !!!
|
---|
| 493 | if ((path_ptr != path) && (entries == 0)) {
|
---|
| 494 | rc = ext4_balloc_free_block(inode_ref, path_ptr->block->lba);
|
---|
| 495 | if (rc != EOK) {
|
---|
[6f413125] | 496 | goto cleanup;
|
---|
[001307cf] | 497 | }
|
---|
[3e2952b] | 498 | check_tree = true;
|
---|
[5b0a3946] | 499 | }
|
---|
[001307cf] | 500 |
|
---|
[3e2952b] | 501 | // Jump to the parent
|
---|
| 502 | --path_ptr;
|
---|
[5b0a3946] | 503 |
|
---|
[3e2952b] | 504 | // release all successors in all levels
|
---|
| 505 | while(path_ptr >= path) {
|
---|
| 506 | entries = ext4_extent_header_get_entries_count(path_ptr->header);
|
---|
| 507 | ext4_extent_index_t *index = path_ptr->index + 1;
|
---|
| 508 | ext4_extent_index_t *stop =
|
---|
| 509 | EXT4_EXTENT_FIRST_INDEX(path_ptr->header) + entries;
|
---|
[001307cf] | 510 |
|
---|
[3e2952b] | 511 | if (check_tree) {
|
---|
| 512 | entries--;
|
---|
| 513 | ext4_extent_header_set_entries_count(path_ptr->header, entries);
|
---|
[001307cf] | 514 | }
|
---|
[0d4db0f] | 515 |
|
---|
[5b0a3946] | 516 | while (index < stop) {
|
---|
| 517 | rc = ext4_extent_release_branch(inode_ref, index);
|
---|
| 518 | if (rc != EOK) {
|
---|
[6f413125] | 519 | goto cleanup;
|
---|
[5b0a3946] | 520 | }
|
---|
| 521 | ++index;
|
---|
[3e2952b] | 522 | --entries;
|
---|
| 523 | ext4_extent_header_set_entries_count(path_ptr->header, entries);
|
---|
[5b0a3946] | 524 | }
|
---|
| 525 |
|
---|
[3e2952b] | 526 | path_ptr->block->dirty = true;
|
---|
[5b0a3946] | 527 |
|
---|
[3e2952b] | 528 | if ((entries == 0) && (path_ptr != path)) {
|
---|
| 529 | rc = ext4_balloc_free_block(inode_ref, path_ptr->block->lba);
|
---|
[5b0a3946] | 530 | if (rc != EOK) {
|
---|
[6f413125] | 531 | goto cleanup;
|
---|
[5b0a3946] | 532 | }
|
---|
[3e2952b] | 533 | check_tree = true;
|
---|
| 534 | } else {
|
---|
| 535 | check_tree = false;
|
---|
[5b0a3946] | 536 | }
|
---|
[3e2952b] | 537 |
|
---|
| 538 | --path_ptr;
|
---|
[0d4db0f] | 539 | }
|
---|
| 540 |
|
---|
[3e2952b] | 541 |
|
---|
[6f413125] | 542 | cleanup:
|
---|
[0d4db0f] | 543 | // Put loaded blocks
|
---|
| 544 | // From 1 -> 0 is a block with inode data
|
---|
[6f413125] | 545 | for (uint16_t i = 1; i < path->depth; ++i) {
|
---|
[0d4db0f] | 546 | if (path[i].block) {
|
---|
| 547 | block_put(path[i].block);
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | // Destroy temporary data structure
|
---|
| 552 | free(path);
|
---|
| 553 |
|
---|
[6f413125] | 554 | return rc;
|
---|
[0d4db0f] | 555 | }
|
---|
[1ac1ab4] | 556 |
|
---|
[ce6de59] | 557 | int ext4_extent_append_block(ext4_inode_ref_t *inode_ref,
|
---|
| 558 | uint32_t *iblock, uint32_t *fblock)
|
---|
| 559 | {
|
---|
[6f413125] | 560 | int rc = EOK;
|
---|
[ce6de59] | 561 |
|
---|
| 562 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
| 563 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
| 564 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[b73530a] | 565 |
|
---|
| 566 | uint32_t new_block_idx = 0;
|
---|
| 567 | if (inode_size > 0) {
|
---|
| 568 | if ((inode_size % block_size) != 0) {
|
---|
| 569 | inode_size += block_size - (inode_size % block_size);
|
---|
| 570 | }
|
---|
| 571 | new_block_idx = inode_size / block_size;
|
---|
| 572 | }
|
---|
[ce6de59] | 573 |
|
---|
| 574 | ext4_extent_path_t *path;
|
---|
| 575 | rc = ext4_extent_find_extent(inode_ref, new_block_idx, &path);
|
---|
| 576 | if (rc != EOK) {
|
---|
| 577 | return rc;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | // Jump to last item of the path (extent)
|
---|
| 581 | ext4_extent_path_t *path_ptr = path;
|
---|
| 582 | while (path_ptr->depth != 0) {
|
---|
| 583 | path_ptr++;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[b73530a] | 586 | // if extent == NULL -> add extent to empty leaf
|
---|
[936132f] | 587 | if (path_ptr->extent == NULL) {
|
---|
[b73530a] | 588 |
|
---|
| 589 | EXT4FS_DBG("NULL extent");
|
---|
| 590 |
|
---|
[936132f] | 591 | ext4_extent_t *ext = EXT4_EXTENT_FIRST(path_ptr->header);
|
---|
| 592 | ext4_extent_set_block_count(ext, 0);
|
---|
| 593 |
|
---|
| 594 | ext4_extent_header_set_entries_count(path_ptr->header, 1);
|
---|
| 595 |
|
---|
| 596 | path_ptr->extent = ext;
|
---|
| 597 | }
|
---|
[ce6de59] | 598 |
|
---|
| 599 | uint32_t phys_block;
|
---|
| 600 |
|
---|
| 601 | if (ext4_extent_get_block_count(path_ptr->extent) == 0) {
|
---|
| 602 |
|
---|
[b73530a] | 603 | EXT4FS_DBG("no blocks in extent");
|
---|
| 604 |
|
---|
[ce6de59] | 605 | // Add first block to the extent
|
---|
| 606 |
|
---|
| 607 | rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
|
---|
| 608 | if (rc != EOK) {
|
---|
[b73530a] | 609 | EXT4FS_DBG("error in allocation");
|
---|
[6f413125] | 610 | goto finish;
|
---|
[ce6de59] | 611 | }
|
---|
| 612 |
|
---|
| 613 | ext4_extent_set_block_count(path_ptr->extent, 1);
|
---|
| 614 | ext4_extent_set_start(path_ptr->extent, phys_block);
|
---|
[b73530a] | 615 | ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
|
---|
[ce6de59] | 616 |
|
---|
| 617 | path_ptr->block->dirty = true;
|
---|
| 618 |
|
---|
| 619 | goto finish;
|
---|
| 620 |
|
---|
| 621 | } else {
|
---|
[b73530a] | 622 | // try allocate succeeding extent block
|
---|
| 623 |
|
---|
| 624 | EXT4FS_DBG("existing extent");
|
---|
| 625 |
|
---|
| 626 | uint16_t blocks = ext4_extent_get_block_count(path_ptr->extent);
|
---|
| 627 |
|
---|
| 628 | if (blocks < (1 << 15)) {
|
---|
| 629 | // there is place for more blocks
|
---|
| 630 |
|
---|
| 631 | EXT4FS_DBG("appending block to existing extent");
|
---|
| 632 |
|
---|
| 633 | uint64_t last = ext4_extent_get_start(path_ptr->extent) + blocks - 1;
|
---|
[ce6de59] | 634 |
|
---|
[b73530a] | 635 | rc = ext4_balloc_try_alloc_block(inode_ref, last + 1);
|
---|
| 636 | if (rc == EOK) {
|
---|
| 637 | path_ptr->block->dirty = true;
|
---|
| 638 | ext4_extent_set_block_count(path_ptr->extent, blocks + 1);
|
---|
| 639 | phys_block = last + 1;
|
---|
| 640 | goto finish;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | if (rc != EINVAL) {
|
---|
| 644 | goto finish;
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | // Add new extent
|
---|
[ce6de59] | 649 | // TODO
|
---|
| 650 | assert(false);
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 |
|
---|
| 654 | finish:
|
---|
[1196df6] | 655 |
|
---|
| 656 | *iblock = new_block_idx;
|
---|
| 657 | *fblock = phys_block;
|
---|
| 658 |
|
---|
[ce6de59] | 659 | // Put loaded blocks
|
---|
| 660 | // From 1 -> 0 is a block with inode data
|
---|
| 661 | for (uint16_t i = 1; i < path->depth; ++i) {
|
---|
| 662 | if (path[i].block) {
|
---|
| 663 | block_put(path[i].block);
|
---|
| 664 | }
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 | // Destroy temporary data structure
|
---|
| 668 | free(path);
|
---|
| 669 |
|
---|
[6f413125] | 670 | return rc;
|
---|
[ce6de59] | 671 | }
|
---|
| 672 |
|
---|
[829d238] | 673 | /**
|
---|
| 674 | * @}
|
---|
| 675 | */
|
---|