| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 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 | * @file libext4_extent.c
|
|---|
| 34 | * @brief Ext4 extent structures operations.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <byteorder.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <malloc.h>
|
|---|
| 40 | #include "libext4.h"
|
|---|
| 41 |
|
|---|
| 42 | /** Get logical number of the block covered by extent.
|
|---|
| 43 | *
|
|---|
| 44 | * @param extent Extent to load number from
|
|---|
| 45 | *
|
|---|
| 46 | * @return Logical number of the first block covered by extent
|
|---|
| 47 | *
|
|---|
| 48 | */
|
|---|
| 49 | uint32_t ext4_extent_get_first_block(ext4_extent_t *extent)
|
|---|
| 50 | {
|
|---|
| 51 | return uint32_t_le2host(extent->first_block);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /** Set logical number of the first block covered by extent.
|
|---|
| 55 | *
|
|---|
| 56 | * @param extent Extent to set number to
|
|---|
| 57 | * @param iblock Logical number of the first block covered by extent
|
|---|
| 58 | *
|
|---|
| 59 | */
|
|---|
| 60 | void ext4_extent_set_first_block(ext4_extent_t *extent, uint32_t iblock)
|
|---|
| 61 | {
|
|---|
| 62 | extent->first_block = host2uint32_t_le(iblock);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /** Get number of blocks covered by extent.
|
|---|
| 66 | *
|
|---|
| 67 | * @param extent Extent to load count from
|
|---|
| 68 | *
|
|---|
| 69 | * @return Number of blocks covered by extent
|
|---|
| 70 | *
|
|---|
| 71 | */
|
|---|
| 72 | uint16_t ext4_extent_get_block_count(ext4_extent_t *extent)
|
|---|
| 73 | {
|
|---|
| 74 | return uint16_t_le2host(extent->block_count);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Set number of blocks covered by extent.
|
|---|
| 78 | *
|
|---|
| 79 | * @param extent Extent to load count from
|
|---|
| 80 | * @param count Number of blocks covered by extent
|
|---|
| 81 | *
|
|---|
| 82 | */
|
|---|
| 83 | void ext4_extent_set_block_count(ext4_extent_t *extent, uint16_t count)
|
|---|
| 84 | {
|
|---|
| 85 | extent->block_count = host2uint16_t_le(count);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /** Get physical number of the first block covered by extent.
|
|---|
| 89 | *
|
|---|
| 90 | * @param extent Extent to load number
|
|---|
| 91 | *
|
|---|
| 92 | * @return Physical number of the first block covered by extent
|
|---|
| 93 | *
|
|---|
| 94 | */
|
|---|
| 95 | uint64_t ext4_extent_get_start(ext4_extent_t *extent)
|
|---|
| 96 | {
|
|---|
| 97 | return ((uint64_t)uint16_t_le2host(extent->start_hi)) << 32 |
|
|---|
| 98 | ((uint64_t)uint32_t_le2host(extent->start_lo));
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** Set physical number of the first block covered by extent.
|
|---|
| 102 | *
|
|---|
| 103 | * @param extent Extent to load number
|
|---|
| 104 | * @param fblock Physical number of the first block covered by extent
|
|---|
| 105 | *
|
|---|
| 106 | */
|
|---|
| 107 | void ext4_extent_set_start(ext4_extent_t *extent, uint64_t fblock)
|
|---|
| 108 | {
|
|---|
| 109 | extent->start_lo = host2uint32_t_le((fblock << 32) >> 32);
|
|---|
| 110 | extent->start_hi = host2uint16_t_le((uint16_t)(fblock >> 32));
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** Get logical number of the block covered by extent index.
|
|---|
| 114 | *
|
|---|
| 115 | * @param index Extent index to load number from
|
|---|
| 116 | *
|
|---|
| 117 | * @return Logical number of the first block covered by extent index
|
|---|
| 118 | *
|
|---|
| 119 | */
|
|---|
| 120 | uint32_t ext4_extent_index_get_first_block(ext4_extent_index_t *index)
|
|---|
| 121 | {
|
|---|
| 122 | return uint32_t_le2host(index->first_block);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /** Set logical number of the block covered by extent index.
|
|---|
| 126 | *
|
|---|
| 127 | * @param index Extent index to set number to
|
|---|
| 128 | * @param iblock Logical number of the first block covered by extent index
|
|---|
| 129 | *
|
|---|
| 130 | */
|
|---|
| 131 | void ext4_extent_index_set_first_block(ext4_extent_index_t *index,
|
|---|
| 132 | uint32_t iblock)
|
|---|
| 133 | {
|
|---|
| 134 | index->first_block = host2uint32_t_le(iblock);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** Get physical number of block where the child node is located.
|
|---|
| 138 | *
|
|---|
| 139 | * @param index Extent index to load number from
|
|---|
| 140 | *
|
|---|
| 141 | * @return Physical number of the block with child node
|
|---|
| 142 | *
|
|---|
| 143 | */
|
|---|
| 144 | uint64_t ext4_extent_index_get_leaf(ext4_extent_index_t *index)
|
|---|
| 145 | {
|
|---|
| 146 | return ((uint64_t) uint16_t_le2host(index->leaf_hi)) << 32 |
|
|---|
| 147 | ((uint64_t)uint32_t_le2host(index->leaf_lo));
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /** Set physical number of block where the child node is located.
|
|---|
| 151 | *
|
|---|
| 152 | * @param index Extent index to set number to
|
|---|
| 153 | * @param fblock Ohysical number of the block with child node
|
|---|
| 154 | *
|
|---|
| 155 | */
|
|---|
| 156 | void ext4_extent_index_set_leaf(ext4_extent_index_t *index, uint64_t fblock)
|
|---|
| 157 | {
|
|---|
| 158 | index->leaf_lo = host2uint32_t_le((fblock << 32) >> 32);
|
|---|
| 159 | index->leaf_hi = host2uint16_t_le((uint16_t) (fblock >> 32));
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /** Get magic value from extent header.
|
|---|
| 163 | *
|
|---|
| 164 | * @param header Extent header to load value from
|
|---|
| 165 | *
|
|---|
| 166 | * @return Magic value of extent header
|
|---|
| 167 | *
|
|---|
| 168 | */
|
|---|
| 169 | uint16_t ext4_extent_header_get_magic(ext4_extent_header_t *header)
|
|---|
| 170 | {
|
|---|
| 171 | return uint16_t_le2host(header->magic);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /** Set magic value to extent header.
|
|---|
| 175 | *
|
|---|
| 176 | * @param header Extent header to set value to
|
|---|
| 177 | * @param magic Magic value of extent header
|
|---|
| 178 | *
|
|---|
| 179 | */
|
|---|
| 180 | void ext4_extent_header_set_magic(ext4_extent_header_t *header, uint16_t magic)
|
|---|
| 181 | {
|
|---|
| 182 | header->magic = host2uint16_t_le(magic);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /** Get number of entries from extent header
|
|---|
| 186 | *
|
|---|
| 187 | * @param header Extent header to get value from
|
|---|
| 188 | *
|
|---|
| 189 | * @return Number of entries covered by extent header
|
|---|
| 190 | *
|
|---|
| 191 | */
|
|---|
| 192 | uint16_t ext4_extent_header_get_entries_count(ext4_extent_header_t *header)
|
|---|
| 193 | {
|
|---|
| 194 | return uint16_t_le2host(header->entries_count);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /** Set number of entries to extent header
|
|---|
| 198 | *
|
|---|
| 199 | * @param header Extent header to set value to
|
|---|
| 200 | * @param count Number of entries covered by extent header
|
|---|
| 201 | *
|
|---|
| 202 | */
|
|---|
| 203 | void ext4_extent_header_set_entries_count(ext4_extent_header_t *header,
|
|---|
| 204 | uint16_t count)
|
|---|
| 205 | {
|
|---|
| 206 | header->entries_count = host2uint16_t_le(count);
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /** Get maximum number of entries from extent header
|
|---|
| 210 | *
|
|---|
| 211 | * @param header Extent header to get value from
|
|---|
| 212 | *
|
|---|
| 213 | * @return Maximum number of entries covered by extent header
|
|---|
| 214 | *
|
|---|
| 215 | */
|
|---|
| 216 | uint16_t ext4_extent_header_get_max_entries_count(ext4_extent_header_t *header)
|
|---|
| 217 | {
|
|---|
| 218 | return uint16_t_le2host(header->max_entries_count);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /** Set maximum number of entries to extent header
|
|---|
| 222 | *
|
|---|
| 223 | * @param header Extent header to set value to
|
|---|
| 224 | * @param max_count Maximum number of entries covered by extent header
|
|---|
| 225 | *
|
|---|
| 226 | */
|
|---|
| 227 | void ext4_extent_header_set_max_entries_count(ext4_extent_header_t *header,
|
|---|
| 228 | uint16_t max_count)
|
|---|
| 229 | {
|
|---|
| 230 | header->max_entries_count = host2uint16_t_le(max_count);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /** Get depth of extent subtree.
|
|---|
| 234 | *
|
|---|
| 235 | * @param header Extent header to get value from
|
|---|
| 236 | *
|
|---|
| 237 | * @return Depth of extent subtree
|
|---|
| 238 | *
|
|---|
| 239 | */
|
|---|
| 240 | uint16_t ext4_extent_header_get_depth(ext4_extent_header_t *header)
|
|---|
| 241 | {
|
|---|
| 242 | return uint16_t_le2host(header->depth);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /** Set depth of extent subtree.
|
|---|
| 246 | *
|
|---|
| 247 | * @param header Extent header to set value to
|
|---|
| 248 | * @param depth Depth of extent subtree
|
|---|
| 249 | *
|
|---|
| 250 | */
|
|---|
| 251 | void ext4_extent_header_set_depth(ext4_extent_header_t *header, uint16_t depth)
|
|---|
| 252 | {
|
|---|
| 253 | header->depth = host2uint16_t_le(depth);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /** Get generation from extent header
|
|---|
| 257 | *
|
|---|
| 258 | * @param header Extent header to get value from
|
|---|
| 259 | *
|
|---|
| 260 | * @return Generation
|
|---|
| 261 | *
|
|---|
| 262 | */
|
|---|
| 263 | uint32_t ext4_extent_header_get_generation(ext4_extent_header_t *header)
|
|---|
| 264 | {
|
|---|
| 265 | return uint32_t_le2host(header->generation);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /** Set generation to extent header
|
|---|
| 269 | *
|
|---|
| 270 | * @param header Extent header to set value to
|
|---|
| 271 | * @param generation Generation
|
|---|
| 272 | *
|
|---|
| 273 | */
|
|---|
| 274 | void ext4_extent_header_set_generation(ext4_extent_header_t *header,
|
|---|
| 275 | uint32_t generation)
|
|---|
| 276 | {
|
|---|
| 277 | header->generation = host2uint32_t_le(generation);
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /** Binary search in extent index node.
|
|---|
| 281 | *
|
|---|
| 282 | * @param header Extent header of index node
|
|---|
| 283 | * @param index Output value - found index will be set here
|
|---|
| 284 | * @param iblock Logical block number to find in index node
|
|---|
| 285 | *
|
|---|
| 286 | */
|
|---|
| 287 | static void ext4_extent_binsearch_idx(ext4_extent_header_t *header,
|
|---|
| 288 | ext4_extent_index_t **index, uint32_t iblock)
|
|---|
| 289 | {
|
|---|
| 290 | ext4_extent_index_t *r;
|
|---|
| 291 | ext4_extent_index_t *l;
|
|---|
| 292 | ext4_extent_index_t *m;
|
|---|
| 293 |
|
|---|
| 294 | uint16_t entries_count =
|
|---|
| 295 | ext4_extent_header_get_entries_count(header);
|
|---|
| 296 |
|
|---|
| 297 | /* Initialize bounds */
|
|---|
| 298 | l = EXT4_EXTENT_FIRST_INDEX(header) + 1;
|
|---|
| 299 | r = EXT4_EXTENT_FIRST_INDEX(header) + entries_count - 1;
|
|---|
| 300 |
|
|---|
| 301 | /* Do binary search */
|
|---|
| 302 | while (l <= r) {
|
|---|
| 303 | m = l + (r - l) / 2;
|
|---|
| 304 | uint32_t first_block = ext4_extent_index_get_first_block(m);
|
|---|
| 305 |
|
|---|
| 306 | if (iblock < first_block)
|
|---|
| 307 | r = m - 1;
|
|---|
| 308 | else
|
|---|
| 309 | l = m + 1;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | /* Set output value */
|
|---|
| 313 | *index = l - 1;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | /** Binary search in extent leaf node.
|
|---|
| 317 | *
|
|---|
| 318 | * @param header Extent header of leaf node
|
|---|
| 319 | * @param extent Output value - found extent will be set here,
|
|---|
| 320 | * or NULL if node is empty
|
|---|
| 321 | * @param iblock Logical block number to find in leaf node
|
|---|
| 322 | *
|
|---|
| 323 | */
|
|---|
| 324 | static void ext4_extent_binsearch(ext4_extent_header_t *header,
|
|---|
| 325 | ext4_extent_t **extent, uint32_t iblock)
|
|---|
| 326 | {
|
|---|
| 327 | ext4_extent_t *r;
|
|---|
| 328 | ext4_extent_t *l;
|
|---|
| 329 | ext4_extent_t *m;
|
|---|
| 330 |
|
|---|
| 331 | uint16_t entries_count =
|
|---|
| 332 | ext4_extent_header_get_entries_count(header);
|
|---|
| 333 |
|
|---|
| 334 | if (entries_count == 0) {
|
|---|
| 335 | /* this leaf is empty */
|
|---|
| 336 | *extent = NULL;
|
|---|
| 337 | return;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /* Initialize bounds */
|
|---|
| 341 | l = EXT4_EXTENT_FIRST(header) + 1;
|
|---|
| 342 | r = EXT4_EXTENT_FIRST(header) + entries_count - 1;
|
|---|
| 343 |
|
|---|
| 344 | /* Do binary search */
|
|---|
| 345 | while (l <= r) {
|
|---|
| 346 | m = l + (r - l) / 2;
|
|---|
| 347 | uint32_t first_block = ext4_extent_get_first_block(m);
|
|---|
| 348 |
|
|---|
| 349 | if (iblock < first_block)
|
|---|
| 350 | r = m - 1;
|
|---|
| 351 | else
|
|---|
| 352 | l = m + 1;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | /* Set output value */
|
|---|
| 356 | *extent = l - 1;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | /** Find physical block in the extent tree by logical block number.
|
|---|
| 360 | *
|
|---|
| 361 | * There is no need to save path in the tree during this algorithm.
|
|---|
| 362 | *
|
|---|
| 363 | * @param inode_ref I-node to load block from
|
|---|
| 364 | * @param iblock Logical block number to find
|
|---|
| 365 | * @param fblock Output value for physical block number
|
|---|
| 366 | *
|
|---|
| 367 | * @return Error code
|
|---|
| 368 | *
|
|---|
| 369 | */
|
|---|
| 370 | int ext4_extent_find_block(ext4_inode_ref_t *inode_ref, uint32_t iblock,
|
|---|
| 371 | uint32_t *fblock)
|
|---|
| 372 | {
|
|---|
| 373 | int rc;
|
|---|
| 374 | /* Compute bound defined by i-node size */
|
|---|
| 375 | uint64_t inode_size =
|
|---|
| 376 | ext4_inode_get_size(inode_ref->fs->superblock, inode_ref->inode);
|
|---|
| 377 |
|
|---|
| 378 | uint32_t block_size =
|
|---|
| 379 | ext4_superblock_get_block_size(inode_ref->fs->superblock);
|
|---|
| 380 |
|
|---|
| 381 | uint32_t last_idx = (inode_size - 1) / block_size;
|
|---|
| 382 |
|
|---|
| 383 | /* Check if requested iblock is not over size of i-node */
|
|---|
| 384 | if (iblock > last_idx) {
|
|---|
| 385 | *fblock = 0;
|
|---|
| 386 | return EOK;
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | block_t *block = NULL;
|
|---|
| 390 |
|
|---|
| 391 | /* Walk through extent tree */
|
|---|
| 392 | ext4_extent_header_t *header =
|
|---|
| 393 | ext4_inode_get_extent_header(inode_ref->inode);
|
|---|
| 394 |
|
|---|
| 395 | while (ext4_extent_header_get_depth(header) != 0) {
|
|---|
| 396 | /* Search index in node */
|
|---|
| 397 | ext4_extent_index_t *index;
|
|---|
| 398 | ext4_extent_binsearch_idx(header, &index, iblock);
|
|---|
| 399 |
|
|---|
| 400 | /* Load child node and set values for the next iteration */
|
|---|
| 401 | uint64_t child = ext4_extent_index_get_leaf(index);
|
|---|
| 402 |
|
|---|
| 403 | if (block != NULL) {
|
|---|
| 404 | rc = block_put(block);
|
|---|
| 405 | if (rc != EOK)
|
|---|
| 406 | return rc;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | rc = block_get(&block, inode_ref->fs->device, child,
|
|---|
| 410 | BLOCK_FLAGS_NONE);
|
|---|
| 411 | if (rc != EOK)
|
|---|
| 412 | return rc;
|
|---|
| 413 |
|
|---|
| 414 | header = (ext4_extent_header_t *)block->data;
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | /* Search extent in the leaf block */
|
|---|
| 418 | ext4_extent_t* extent = NULL;
|
|---|
| 419 | ext4_extent_binsearch(header, &extent, iblock);
|
|---|
| 420 |
|
|---|
| 421 | /* Prevent empty leaf */
|
|---|
| 422 | if (extent == NULL) {
|
|---|
| 423 | *fblock = 0;
|
|---|
| 424 | } else {
|
|---|
| 425 | /* Compute requested physical block address */
|
|---|
| 426 | uint32_t phys_block;
|
|---|
| 427 | uint32_t first = ext4_extent_get_first_block(extent);
|
|---|
| 428 | phys_block = ext4_extent_get_start(extent) + iblock - first;
|
|---|
| 429 |
|
|---|
| 430 | *fblock = phys_block;
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | /* Cleanup */
|
|---|
| 434 | if (block != NULL)
|
|---|
| 435 | rc = block_put(block);
|
|---|
| 436 |
|
|---|
| 437 | return rc;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | /** Find extent for specified iblock.
|
|---|
| 441 | *
|
|---|
| 442 | * This function is used for finding block in the extent tree with
|
|---|
| 443 | * saving the path through the tree for possible future modifications.
|
|---|
| 444 | *
|
|---|
| 445 | * @param inode_ref I-node to read extent tree from
|
|---|
| 446 | * @param iblock Iblock to find extent for
|
|---|
| 447 | * @param ret_path Output value for loaded path from extent tree
|
|---|
| 448 | *
|
|---|
| 449 | * @return Error code
|
|---|
| 450 | *
|
|---|
| 451 | */
|
|---|
| 452 | static int ext4_extent_find_extent(ext4_inode_ref_t *inode_ref, uint32_t iblock,
|
|---|
| 453 | ext4_extent_path_t **ret_path)
|
|---|
| 454 | {
|
|---|
| 455 | ext4_extent_header_t *eh =
|
|---|
| 456 | ext4_inode_get_extent_header(inode_ref->inode);
|
|---|
| 457 |
|
|---|
| 458 | uint16_t depth = ext4_extent_header_get_depth(eh);
|
|---|
| 459 |
|
|---|
| 460 | ext4_extent_path_t *tmp_path;
|
|---|
| 461 |
|
|---|
| 462 | /* Added 2 for possible tree growing */
|
|---|
| 463 | tmp_path = malloc(sizeof(ext4_extent_path_t) * (depth + 2));
|
|---|
| 464 | if (tmp_path == NULL)
|
|---|
| 465 | return ENOMEM;
|
|---|
| 466 |
|
|---|
| 467 | /* Initialize structure for algorithm start */
|
|---|
| 468 | tmp_path[0].block = inode_ref->block;
|
|---|
| 469 | tmp_path[0].header = eh;
|
|---|
| 470 |
|
|---|
| 471 | /* Walk through the extent tree */
|
|---|
| 472 | uint16_t pos = 0;
|
|---|
| 473 | int rc;
|
|---|
| 474 | while (ext4_extent_header_get_depth(eh) != 0) {
|
|---|
| 475 | /* Search index in index node by iblock */
|
|---|
| 476 | ext4_extent_binsearch_idx(tmp_path[pos].header,
|
|---|
| 477 | &tmp_path[pos].index, iblock);
|
|---|
| 478 |
|
|---|
| 479 | tmp_path[pos].depth = depth;
|
|---|
| 480 | tmp_path[pos].extent = NULL;
|
|---|
| 481 |
|
|---|
| 482 | assert(tmp_path[pos].index != NULL);
|
|---|
| 483 |
|
|---|
| 484 | /* Load information for the next iteration */
|
|---|
| 485 | uint64_t fblock = ext4_extent_index_get_leaf(tmp_path[pos].index);
|
|---|
| 486 |
|
|---|
| 487 | block_t *block;
|
|---|
| 488 | rc = block_get(&block, inode_ref->fs->device, fblock,
|
|---|
| 489 | BLOCK_FLAGS_NONE);
|
|---|
| 490 | if (rc != EOK)
|
|---|
| 491 | goto cleanup;
|
|---|
| 492 |
|
|---|
| 493 | pos++;
|
|---|
| 494 |
|
|---|
| 495 | eh = (ext4_extent_header_t *)block->data;
|
|---|
| 496 | tmp_path[pos].block = block;
|
|---|
| 497 | tmp_path[pos].header = eh;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | tmp_path[pos].depth = 0;
|
|---|
| 501 | tmp_path[pos].extent = NULL;
|
|---|
| 502 | tmp_path[pos].index = NULL;
|
|---|
| 503 |
|
|---|
| 504 | /* Find extent in the leaf node */
|
|---|
| 505 | ext4_extent_binsearch(tmp_path[pos].header, &tmp_path[pos].extent, iblock);
|
|---|
| 506 | *ret_path = tmp_path;
|
|---|
| 507 |
|
|---|
| 508 | return EOK;
|
|---|
| 509 |
|
|---|
| 510 | cleanup:
|
|---|
| 511 | ;
|
|---|
| 512 |
|
|---|
| 513 | int rc2 = EOK;
|
|---|
| 514 |
|
|---|
| 515 | /*
|
|---|
| 516 | * Put loaded blocks
|
|---|
| 517 | * From 1: 0 is a block with inode data
|
|---|
| 518 | */
|
|---|
| 519 | for (uint16_t i = 1; i < tmp_path->depth; ++i) {
|
|---|
| 520 | if (tmp_path[i].block) {
|
|---|
| 521 | rc2 = block_put(tmp_path[i].block);
|
|---|
| 522 | if (rc == EOK && rc2 != EOK)
|
|---|
| 523 | rc = rc2;
|
|---|
| 524 | }
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | /* Destroy temporary data structure */
|
|---|
| 528 | free(tmp_path);
|
|---|
| 529 |
|
|---|
| 530 | return rc;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | /** Release extent and all data blocks covered by the extent.
|
|---|
| 534 | *
|
|---|
| 535 | * @param inode_ref I-node to release extent and block from
|
|---|
| 536 | * @param extent Extent to release
|
|---|
| 537 | *
|
|---|
| 538 | * @return Error code
|
|---|
| 539 | *
|
|---|
| 540 | */
|
|---|
| 541 | static int ext4_extent_release(ext4_inode_ref_t *inode_ref,
|
|---|
| 542 | ext4_extent_t *extent)
|
|---|
| 543 | {
|
|---|
| 544 | /* Compute number of the first physical block to release */
|
|---|
| 545 | uint64_t start = ext4_extent_get_start(extent);
|
|---|
| 546 | uint16_t block_count = ext4_extent_get_block_count(extent);
|
|---|
| 547 |
|
|---|
| 548 | return ext4_balloc_free_blocks(inode_ref, start, block_count);
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | /** Recursively release the whole branch of the extent tree.
|
|---|
| 552 | *
|
|---|
| 553 | * For each entry of the node release the subbranch and finally release
|
|---|
| 554 | * the node. In the leaf node all extents will be released.
|
|---|
| 555 | *
|
|---|
| 556 | * @param inode_ref I-node where the branch is released
|
|---|
| 557 | * @param index Index in the non-leaf node to be released
|
|---|
| 558 | * with the whole subtree
|
|---|
| 559 | *
|
|---|
| 560 | * @return Error code
|
|---|
| 561 | *
|
|---|
| 562 | */
|
|---|
| 563 | static int ext4_extent_release_branch(ext4_inode_ref_t *inode_ref,
|
|---|
| 564 | ext4_extent_index_t *index)
|
|---|
| 565 | {
|
|---|
| 566 | uint32_t fblock = ext4_extent_index_get_leaf(index);
|
|---|
| 567 |
|
|---|
| 568 | block_t* block;
|
|---|
| 569 | int rc = block_get(&block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
|
|---|
| 570 | if (rc != EOK)
|
|---|
| 571 | return rc;
|
|---|
| 572 |
|
|---|
| 573 | ext4_extent_header_t *header = block->data;
|
|---|
| 574 |
|
|---|
| 575 | if (ext4_extent_header_get_depth(header)) {
|
|---|
| 576 | /* The node is non-leaf, do recursion */
|
|---|
| 577 | ext4_extent_index_t *idx = EXT4_EXTENT_FIRST_INDEX(header);
|
|---|
| 578 |
|
|---|
| 579 | /* Release all subbranches */
|
|---|
| 580 | for (uint32_t i = 0;
|
|---|
| 581 | i < ext4_extent_header_get_entries_count(header);
|
|---|
| 582 | ++i, ++idx) {
|
|---|
| 583 | rc = ext4_extent_release_branch(inode_ref, idx);
|
|---|
| 584 | if (rc != EOK)
|
|---|
| 585 | return rc;
|
|---|
| 586 | }
|
|---|
| 587 | } else {
|
|---|
| 588 | /* Leaf node reached */
|
|---|
| 589 | ext4_extent_t *ext = EXT4_EXTENT_FIRST(header);
|
|---|
| 590 |
|
|---|
| 591 | /* Release all extents and stop recursion */
|
|---|
| 592 | for (uint32_t i = 0;
|
|---|
| 593 | i < ext4_extent_header_get_entries_count(header);
|
|---|
| 594 | ++i, ++ext) {
|
|---|
| 595 | rc = ext4_extent_release(inode_ref, ext);
|
|---|
| 596 | if (rc != EOK)
|
|---|
| 597 | return rc;
|
|---|
| 598 | }
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | /* Release data block where the node was stored */
|
|---|
| 602 |
|
|---|
| 603 | rc = block_put(block);
|
|---|
| 604 | if (rc != EOK)
|
|---|
| 605 | return rc;
|
|---|
| 606 |
|
|---|
| 607 | return ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | /** Release all data blocks starting from specified logical block.
|
|---|
| 611 | *
|
|---|
| 612 | * @param inode_ref I-node to release blocks from
|
|---|
| 613 | * @param iblock_from First logical block to release
|
|---|
| 614 | *
|
|---|
| 615 | */
|
|---|
| 616 | int ext4_extent_release_blocks_from(ext4_inode_ref_t *inode_ref,
|
|---|
| 617 | uint32_t iblock_from)
|
|---|
| 618 | {
|
|---|
| 619 | /* Find the first extent to modify */
|
|---|
| 620 | ext4_extent_path_t *path;
|
|---|
| 621 | int rc = ext4_extent_find_extent(inode_ref, iblock_from, &path);
|
|---|
| 622 | if (rc != EOK)
|
|---|
| 623 | return rc;
|
|---|
| 624 |
|
|---|
| 625 | /* Jump to last item of the path (extent) */
|
|---|
| 626 | ext4_extent_path_t *path_ptr = path;
|
|---|
| 627 | while (path_ptr->depth != 0)
|
|---|
| 628 | path_ptr++;
|
|---|
| 629 |
|
|---|
| 630 | assert(path_ptr->extent != NULL);
|
|---|
| 631 |
|
|---|
| 632 | /* First extent maybe released partially */
|
|---|
| 633 | uint32_t first_iblock =
|
|---|
| 634 | ext4_extent_get_first_block(path_ptr->extent);
|
|---|
| 635 | uint32_t first_fblock =
|
|---|
| 636 | ext4_extent_get_start(path_ptr->extent) + iblock_from - first_iblock;
|
|---|
| 637 |
|
|---|
| 638 | uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
|
|---|
| 639 |
|
|---|
| 640 | uint16_t delete_count = block_count -
|
|---|
| 641 | (ext4_extent_get_start(path_ptr->extent) - first_fblock);
|
|---|
| 642 |
|
|---|
| 643 | /* Release all blocks */
|
|---|
| 644 | rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
|
|---|
| 645 | if (rc != EOK)
|
|---|
| 646 | goto cleanup;
|
|---|
| 647 |
|
|---|
| 648 | /* Correct counter */
|
|---|
| 649 | block_count -= delete_count;
|
|---|
| 650 | ext4_extent_set_block_count(path_ptr->extent, block_count);
|
|---|
| 651 |
|
|---|
| 652 | /* Initialize the following loop */
|
|---|
| 653 | uint16_t entries =
|
|---|
| 654 | ext4_extent_header_get_entries_count(path_ptr->header);
|
|---|
| 655 | ext4_extent_t *tmp_ext = path_ptr->extent + 1;
|
|---|
| 656 | ext4_extent_t *stop_ext = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
|
|---|
| 657 |
|
|---|
| 658 | /* If first extent empty, release it */
|
|---|
| 659 | if (block_count == 0)
|
|---|
| 660 | entries--;
|
|---|
| 661 |
|
|---|
| 662 | /* Release all successors of the first extent in the same node */
|
|---|
| 663 | while (tmp_ext < stop_ext) {
|
|---|
| 664 | first_fblock = ext4_extent_get_start(tmp_ext);
|
|---|
| 665 | delete_count = ext4_extent_get_block_count(tmp_ext);
|
|---|
| 666 |
|
|---|
| 667 | rc = ext4_balloc_free_blocks(inode_ref, first_fblock, delete_count);
|
|---|
| 668 | if (rc != EOK)
|
|---|
| 669 | goto cleanup;
|
|---|
| 670 |
|
|---|
| 671 | entries--;
|
|---|
| 672 | tmp_ext++;
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 | ext4_extent_header_set_entries_count(path_ptr->header, entries);
|
|---|
| 676 | path_ptr->block->dirty = true;
|
|---|
| 677 |
|
|---|
| 678 | /* If leaf node is empty, parent entry must be modified */
|
|---|
| 679 | bool remove_parent_record = false;
|
|---|
| 680 |
|
|---|
| 681 | /* Don't release root block (including inode data) !!! */
|
|---|
| 682 | if ((path_ptr != path) && (entries == 0)) {
|
|---|
| 683 | rc = ext4_balloc_free_block(inode_ref, path_ptr->block->lba);
|
|---|
| 684 | if (rc != EOK)
|
|---|
| 685 | goto cleanup;
|
|---|
| 686 |
|
|---|
| 687 | remove_parent_record = true;
|
|---|
| 688 | }
|
|---|
| 689 |
|
|---|
| 690 | /* Jump to the parent */
|
|---|
| 691 | --path_ptr;
|
|---|
| 692 |
|
|---|
| 693 | /* Release all successors in all tree levels */
|
|---|
| 694 | while (path_ptr >= path) {
|
|---|
| 695 | entries = ext4_extent_header_get_entries_count(path_ptr->header);
|
|---|
| 696 | ext4_extent_index_t *index = path_ptr->index + 1;
|
|---|
| 697 | ext4_extent_index_t *stop =
|
|---|
| 698 | EXT4_EXTENT_FIRST_INDEX(path_ptr->header) + entries;
|
|---|
| 699 |
|
|---|
| 700 | /* Correct entries count because of changes in the previous iteration */
|
|---|
| 701 | if (remove_parent_record)
|
|---|
| 702 | entries--;
|
|---|
| 703 |
|
|---|
| 704 | /* Iterate over all entries and release the whole subtrees */
|
|---|
| 705 | while (index < stop) {
|
|---|
| 706 | rc = ext4_extent_release_branch(inode_ref, index);
|
|---|
| 707 | if (rc != EOK)
|
|---|
| 708 | goto cleanup;
|
|---|
| 709 |
|
|---|
| 710 | ++index;
|
|---|
| 711 | --entries;
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | ext4_extent_header_set_entries_count(path_ptr->header, entries);
|
|---|
| 715 | path_ptr->block->dirty = true;
|
|---|
| 716 |
|
|---|
| 717 | /* Free the node if it is empty */
|
|---|
| 718 | if ((entries == 0) && (path_ptr != path)) {
|
|---|
| 719 | rc = ext4_balloc_free_block(inode_ref, path_ptr->block->lba);
|
|---|
| 720 | if (rc != EOK)
|
|---|
| 721 | goto cleanup;
|
|---|
| 722 |
|
|---|
| 723 | /* Mark parent to be checked */
|
|---|
| 724 | remove_parent_record = true;
|
|---|
| 725 | } else
|
|---|
| 726 | remove_parent_record = false;
|
|---|
| 727 |
|
|---|
| 728 | --path_ptr;
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | cleanup:
|
|---|
| 732 | ;
|
|---|
| 733 |
|
|---|
| 734 | int rc2 = EOK;
|
|---|
| 735 |
|
|---|
| 736 | /*
|
|---|
| 737 | * Put loaded blocks
|
|---|
| 738 | * starting from 1: 0 is a block with inode data
|
|---|
| 739 | */
|
|---|
| 740 | for (uint16_t i = 1; i <= path->depth; ++i) {
|
|---|
| 741 | if (path[i].block) {
|
|---|
| 742 | rc2 = block_put(path[i].block);
|
|---|
| 743 | if (rc == EOK && rc2 != EOK)
|
|---|
| 744 | rc = rc2;
|
|---|
| 745 | }
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | /* Destroy temporary data structure */
|
|---|
| 749 | free(path);
|
|---|
| 750 |
|
|---|
| 751 | return rc;
|
|---|
| 752 | }
|
|---|
| 753 |
|
|---|
| 754 | /** Append new extent to the i-node and do some splitting if necessary.
|
|---|
| 755 | *
|
|---|
| 756 | * @param inode_ref I-node to append extent to
|
|---|
| 757 | * @param path Path in the extent tree for possible splitting
|
|---|
| 758 | * @param last_path_item Input/output parameter for pointer to the last
|
|---|
| 759 | * valid item in the extent tree path
|
|---|
| 760 | * @param iblock Logical index of block to append extent for
|
|---|
| 761 | *
|
|---|
| 762 | * @return Error code
|
|---|
| 763 | *
|
|---|
| 764 | */
|
|---|
| 765 | static int ext4_extent_append_extent(ext4_inode_ref_t *inode_ref,
|
|---|
| 766 | ext4_extent_path_t *path, uint32_t iblock)
|
|---|
| 767 | {
|
|---|
| 768 | ext4_extent_path_t *path_ptr = path + path->depth;
|
|---|
| 769 |
|
|---|
| 770 | uint32_t block_size =
|
|---|
| 771 | ext4_superblock_get_block_size(inode_ref->fs->superblock);
|
|---|
| 772 |
|
|---|
| 773 | /* Start splitting */
|
|---|
| 774 | while (path_ptr > path) {
|
|---|
| 775 | uint16_t entries =
|
|---|
| 776 | ext4_extent_header_get_entries_count(path_ptr->header);
|
|---|
| 777 | uint16_t limit =
|
|---|
| 778 | ext4_extent_header_get_max_entries_count(path_ptr->header);
|
|---|
| 779 |
|
|---|
| 780 | if (entries == limit) {
|
|---|
| 781 | /* Full node - allocate block for new one */
|
|---|
| 782 | uint32_t fblock;
|
|---|
| 783 | int rc = ext4_balloc_alloc_block(inode_ref, &fblock);
|
|---|
| 784 | if (rc != EOK)
|
|---|
| 785 | return rc;
|
|---|
| 786 |
|
|---|
| 787 | block_t *block;
|
|---|
| 788 | rc = block_get(&block, inode_ref->fs->device, fblock,
|
|---|
| 789 | BLOCK_FLAGS_NOREAD);
|
|---|
| 790 | if (rc != EOK) {
|
|---|
| 791 | ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 792 | return rc;
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | /* Put back not modified old block */
|
|---|
| 796 | rc = block_put(path_ptr->block);
|
|---|
| 797 | if (rc != EOK) {
|
|---|
| 798 | ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 799 | return rc;
|
|---|
| 800 | }
|
|---|
| 801 |
|
|---|
| 802 | /* Initialize newly allocated block and remember it */
|
|---|
| 803 | memset(block->data, 0, block_size);
|
|---|
| 804 | path_ptr->block = block;
|
|---|
| 805 |
|
|---|
| 806 | /* Update pointers in extent path structure */
|
|---|
| 807 | path_ptr->header = block->data;
|
|---|
| 808 | if (path_ptr->depth) {
|
|---|
| 809 | path_ptr->index = EXT4_EXTENT_FIRST_INDEX(path_ptr->header);
|
|---|
| 810 | ext4_extent_index_set_first_block(path_ptr->index, iblock);
|
|---|
| 811 | ext4_extent_index_set_leaf(path_ptr->index, (path_ptr + 1)->block->lba);
|
|---|
| 812 | limit = (block_size - sizeof(ext4_extent_header_t)) /
|
|---|
| 813 | sizeof(ext4_extent_index_t);
|
|---|
| 814 | } else {
|
|---|
| 815 | path_ptr->extent = EXT4_EXTENT_FIRST(path_ptr->header);
|
|---|
| 816 | ext4_extent_set_first_block(path_ptr->extent, iblock);
|
|---|
| 817 | limit = (block_size - sizeof(ext4_extent_header_t)) /
|
|---|
| 818 | sizeof(ext4_extent_t);
|
|---|
| 819 | }
|
|---|
| 820 |
|
|---|
| 821 | /* Initialize on-disk structure (header) */
|
|---|
| 822 | ext4_extent_header_set_entries_count(path_ptr->header, 1);
|
|---|
| 823 | ext4_extent_header_set_max_entries_count(path_ptr->header, limit);
|
|---|
| 824 | ext4_extent_header_set_magic(path_ptr->header, EXT4_EXTENT_MAGIC);
|
|---|
| 825 | ext4_extent_header_set_depth(path_ptr->header, path_ptr->depth);
|
|---|
| 826 | ext4_extent_header_set_generation(path_ptr->header, 0);
|
|---|
| 827 |
|
|---|
| 828 | path_ptr->block->dirty = true;
|
|---|
| 829 |
|
|---|
| 830 | /* Jump to the preceeding item */
|
|---|
| 831 | path_ptr--;
|
|---|
| 832 | } else {
|
|---|
| 833 | /* Node with free space */
|
|---|
| 834 | if (path_ptr->depth) {
|
|---|
| 835 | path_ptr->index = EXT4_EXTENT_FIRST_INDEX(path_ptr->header) + entries;
|
|---|
| 836 | ext4_extent_index_set_first_block(path_ptr->index, iblock);
|
|---|
| 837 | ext4_extent_index_set_leaf(path_ptr->index, (path_ptr + 1)->block->lba);
|
|---|
| 838 | } else {
|
|---|
| 839 | path_ptr->extent = EXT4_EXTENT_FIRST(path_ptr->header) + entries;
|
|---|
| 840 | ext4_extent_set_first_block(path_ptr->extent, iblock);
|
|---|
| 841 | }
|
|---|
| 842 |
|
|---|
| 843 | ext4_extent_header_set_entries_count(path_ptr->header, entries + 1);
|
|---|
| 844 | path_ptr->block->dirty = true;
|
|---|
| 845 |
|
|---|
| 846 | /* No more splitting needed */
|
|---|
| 847 | return EOK;
|
|---|
| 848 | }
|
|---|
| 849 | }
|
|---|
| 850 |
|
|---|
| 851 | assert(path_ptr == path);
|
|---|
| 852 |
|
|---|
| 853 | /* Should be the root split too? */
|
|---|
| 854 |
|
|---|
| 855 | uint16_t entries = ext4_extent_header_get_entries_count(path->header);
|
|---|
| 856 | uint16_t limit = ext4_extent_header_get_max_entries_count(path->header);
|
|---|
| 857 |
|
|---|
| 858 | if (entries == limit) {
|
|---|
| 859 | uint32_t new_fblock;
|
|---|
| 860 | int rc = ext4_balloc_alloc_block(inode_ref, &new_fblock);
|
|---|
| 861 | if (rc != EOK)
|
|---|
| 862 | return rc;
|
|---|
| 863 |
|
|---|
| 864 | block_t *block;
|
|---|
| 865 | rc = block_get(&block, inode_ref->fs->device, new_fblock,
|
|---|
| 866 | BLOCK_FLAGS_NOREAD);
|
|---|
| 867 | if (rc != EOK)
|
|---|
| 868 | return rc;
|
|---|
| 869 |
|
|---|
| 870 | /* Initialize newly allocated block */
|
|---|
| 871 | memset(block->data, 0, block_size);
|
|---|
| 872 |
|
|---|
| 873 | /* Move data from root to the new block */
|
|---|
| 874 | memcpy(block->data, inode_ref->inode->blocks,
|
|---|
| 875 | EXT4_INODE_BLOCKS * sizeof(uint32_t));
|
|---|
| 876 |
|
|---|
| 877 | /* Data block is initialized */
|
|---|
| 878 |
|
|---|
| 879 | block_t *root_block = path->block;
|
|---|
| 880 | uint16_t root_depth = path->depth;
|
|---|
| 881 | ext4_extent_header_t *root_header = path->header;
|
|---|
| 882 |
|
|---|
| 883 | /* Make space for tree growing */
|
|---|
| 884 | ext4_extent_path_t *new_root = path;
|
|---|
| 885 | ext4_extent_path_t *old_root = path + 1;
|
|---|
| 886 |
|
|---|
| 887 | size_t nbytes = sizeof(ext4_extent_path_t) * (path->depth + 1);
|
|---|
| 888 | memmove(old_root, new_root, nbytes);
|
|---|
| 889 | memset(new_root, 0, sizeof(ext4_extent_path_t));
|
|---|
| 890 |
|
|---|
| 891 | /* Update old root structure */
|
|---|
| 892 | old_root->block = block;
|
|---|
| 893 | old_root->header = (ext4_extent_header_t *)block->data;
|
|---|
| 894 |
|
|---|
| 895 | /* Add new entry and update limit for entries */
|
|---|
| 896 | if (old_root->depth) {
|
|---|
| 897 | limit = (block_size - sizeof(ext4_extent_header_t)) /
|
|---|
| 898 | sizeof(ext4_extent_index_t);
|
|---|
| 899 | old_root->index = EXT4_EXTENT_FIRST_INDEX(old_root->header) + entries;
|
|---|
| 900 | ext4_extent_index_set_first_block(old_root->index, iblock);
|
|---|
| 901 | ext4_extent_index_set_leaf(old_root->index, (old_root + 1)->block->lba);
|
|---|
| 902 | old_root->extent = NULL;
|
|---|
| 903 | } else {
|
|---|
| 904 | limit = (block_size - sizeof(ext4_extent_header_t)) /
|
|---|
| 905 | sizeof(ext4_extent_t);
|
|---|
| 906 | old_root->extent = EXT4_EXTENT_FIRST(old_root->header) + entries;
|
|---|
| 907 | ext4_extent_set_first_block(old_root->extent, iblock);
|
|---|
| 908 | old_root->index = NULL;
|
|---|
| 909 | }
|
|---|
| 910 |
|
|---|
| 911 | ext4_extent_header_set_entries_count(old_root->header, entries + 1);
|
|---|
| 912 | ext4_extent_header_set_max_entries_count(old_root->header, limit);
|
|---|
| 913 |
|
|---|
| 914 | old_root->block->dirty = true;
|
|---|
| 915 |
|
|---|
| 916 | /* Re-initialize new root metadata */
|
|---|
| 917 | new_root->depth = root_depth + 1;
|
|---|
| 918 | new_root->block = root_block;
|
|---|
| 919 | new_root->header = root_header;
|
|---|
| 920 | new_root->extent = NULL;
|
|---|
| 921 | new_root->index = EXT4_EXTENT_FIRST_INDEX(new_root->header);
|
|---|
| 922 |
|
|---|
| 923 | ext4_extent_header_set_depth(new_root->header, new_root->depth);
|
|---|
| 924 |
|
|---|
| 925 | /* Create new entry in root */
|
|---|
| 926 | ext4_extent_header_set_entries_count(new_root->header, 1);
|
|---|
| 927 | ext4_extent_index_set_first_block(new_root->index, 0);
|
|---|
| 928 | ext4_extent_index_set_leaf(new_root->index, new_fblock);
|
|---|
| 929 |
|
|---|
| 930 | new_root->block->dirty = true;
|
|---|
| 931 | } else {
|
|---|
| 932 | if (path->depth) {
|
|---|
| 933 | path->index = EXT4_EXTENT_FIRST_INDEX(path->header) + entries;
|
|---|
| 934 | ext4_extent_index_set_first_block(path->index, iblock);
|
|---|
| 935 | ext4_extent_index_set_leaf(path->index, (path + 1)->block->lba);
|
|---|
| 936 | } else {
|
|---|
| 937 | path->extent = EXT4_EXTENT_FIRST(path->header) + entries;
|
|---|
| 938 | ext4_extent_set_first_block(path->extent, iblock);
|
|---|
| 939 | }
|
|---|
| 940 |
|
|---|
| 941 | ext4_extent_header_set_entries_count(path->header, entries + 1);
|
|---|
| 942 | path->block->dirty = true;
|
|---|
| 943 | }
|
|---|
| 944 |
|
|---|
| 945 | return EOK;
|
|---|
| 946 | }
|
|---|
| 947 |
|
|---|
| 948 | /** Append data block to the i-node.
|
|---|
| 949 | *
|
|---|
| 950 | * This function allocates data block, tries to append it
|
|---|
| 951 | * to some existing extent or creates new extents.
|
|---|
| 952 | * It includes possible extent tree modifications (splitting).
|
|---|
| 953 | *<
|
|---|
| 954 | * @param inode_ref I-node to append block to
|
|---|
| 955 | * @param iblock Output logical number of newly allocated block
|
|---|
| 956 | * @param fblock Output physical block address of newly allocated block
|
|---|
| 957 | *
|
|---|
| 958 | * @return Error code
|
|---|
| 959 | *
|
|---|
| 960 | */
|
|---|
| 961 | int ext4_extent_append_block(ext4_inode_ref_t *inode_ref, uint32_t *iblock,
|
|---|
| 962 | uint32_t *fblock, bool update_size)
|
|---|
| 963 | {
|
|---|
| 964 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
|---|
| 965 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
|---|
| 966 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 967 |
|
|---|
| 968 | /* Calculate number of new logical block */
|
|---|
| 969 | uint32_t new_block_idx = 0;
|
|---|
| 970 | if (inode_size > 0) {
|
|---|
| 971 | if ((inode_size % block_size) != 0)
|
|---|
| 972 | inode_size += block_size - (inode_size % block_size);
|
|---|
| 973 |
|
|---|
| 974 | new_block_idx = inode_size / block_size;
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| 977 | /* Load the nearest leaf (with extent) */
|
|---|
| 978 | ext4_extent_path_t *path;
|
|---|
| 979 | int rc = ext4_extent_find_extent(inode_ref, new_block_idx, &path);
|
|---|
| 980 | if (rc != EOK)
|
|---|
| 981 | return rc;
|
|---|
| 982 |
|
|---|
| 983 | /* Jump to last item of the path (extent) */
|
|---|
| 984 | ext4_extent_path_t *path_ptr = path;
|
|---|
| 985 | while (path_ptr->depth != 0)
|
|---|
| 986 | path_ptr++;
|
|---|
| 987 |
|
|---|
| 988 | /* Add new extent to the node if not present */
|
|---|
| 989 | if (path_ptr->extent == NULL)
|
|---|
| 990 | goto append_extent;
|
|---|
| 991 |
|
|---|
| 992 | uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
|
|---|
| 993 | uint16_t block_limit = (1 << 15);
|
|---|
| 994 |
|
|---|
| 995 | uint32_t phys_block = 0;
|
|---|
| 996 | if (block_count < block_limit) {
|
|---|
| 997 | /* There is space for new block in the extent */
|
|---|
| 998 | if (block_count == 0) {
|
|---|
| 999 | /* Existing extent is empty */
|
|---|
| 1000 | rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
|
|---|
| 1001 | if (rc != EOK)
|
|---|
| 1002 | goto finish;
|
|---|
| 1003 |
|
|---|
| 1004 | /* Initialize extent */
|
|---|
| 1005 | ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
|
|---|
| 1006 | ext4_extent_set_start(path_ptr->extent, phys_block);
|
|---|
| 1007 | ext4_extent_set_block_count(path_ptr->extent, 1);
|
|---|
| 1008 |
|
|---|
| 1009 | /* Update i-node */
|
|---|
| 1010 | if (update_size) {
|
|---|
| 1011 | ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
|
|---|
| 1012 | inode_ref->dirty = true;
|
|---|
| 1013 | }
|
|---|
| 1014 |
|
|---|
| 1015 | path_ptr->block->dirty = true;
|
|---|
| 1016 |
|
|---|
| 1017 | goto finish;
|
|---|
| 1018 | } else {
|
|---|
| 1019 | /* Existing extent contains some blocks */
|
|---|
| 1020 | phys_block = ext4_extent_get_start(path_ptr->extent);
|
|---|
| 1021 | phys_block += ext4_extent_get_block_count(path_ptr->extent);
|
|---|
| 1022 |
|
|---|
| 1023 | /* Check if the following block is free for allocation */
|
|---|
| 1024 | bool free;
|
|---|
| 1025 | rc = ext4_balloc_try_alloc_block(inode_ref, phys_block, &free);
|
|---|
| 1026 | if (rc != EOK)
|
|---|
| 1027 | goto finish;
|
|---|
| 1028 |
|
|---|
| 1029 | if (!free) {
|
|---|
| 1030 | /* Target is not free, new block must be appended to new extent */
|
|---|
| 1031 | goto append_extent;
|
|---|
| 1032 | }
|
|---|
| 1033 |
|
|---|
| 1034 | /* Update extent */
|
|---|
| 1035 | ext4_extent_set_block_count(path_ptr->extent, block_count + 1);
|
|---|
| 1036 |
|
|---|
| 1037 | /* Update i-node */
|
|---|
| 1038 | if (update_size) {
|
|---|
| 1039 | ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
|
|---|
| 1040 | inode_ref->dirty = true;
|
|---|
| 1041 | }
|
|---|
| 1042 |
|
|---|
| 1043 | path_ptr->block->dirty = true;
|
|---|
| 1044 |
|
|---|
| 1045 | goto finish;
|
|---|
| 1046 | }
|
|---|
| 1047 | }
|
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 | append_extent:
|
|---|
| 1051 | /* Append new extent to the tree */
|
|---|
| 1052 | phys_block = 0;
|
|---|
| 1053 |
|
|---|
| 1054 | /* Allocate new data block */
|
|---|
| 1055 | rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
|
|---|
| 1056 | if (rc != EOK)
|
|---|
| 1057 | goto finish;
|
|---|
| 1058 |
|
|---|
| 1059 | /* Append extent for new block (includes tree splitting if needed) */
|
|---|
| 1060 | rc = ext4_extent_append_extent(inode_ref, path, new_block_idx);
|
|---|
| 1061 | if (rc != EOK) {
|
|---|
| 1062 | ext4_balloc_free_block(inode_ref, phys_block);
|
|---|
| 1063 | goto finish;
|
|---|
| 1064 | }
|
|---|
| 1065 |
|
|---|
| 1066 | uint32_t tree_depth = ext4_extent_header_get_depth(path->header);
|
|---|
| 1067 | path_ptr = path + tree_depth;
|
|---|
| 1068 |
|
|---|
| 1069 | /* Initialize newly created extent */
|
|---|
| 1070 | ext4_extent_set_block_count(path_ptr->extent, 1);
|
|---|
| 1071 | ext4_extent_set_first_block(path_ptr->extent, new_block_idx);
|
|---|
| 1072 | ext4_extent_set_start(path_ptr->extent, phys_block);
|
|---|
| 1073 |
|
|---|
| 1074 | /* Update i-node */
|
|---|
| 1075 | if (update_size) {
|
|---|
| 1076 | ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
|
|---|
| 1077 | inode_ref->dirty = true;
|
|---|
| 1078 | }
|
|---|
| 1079 |
|
|---|
| 1080 | path_ptr->block->dirty = true;
|
|---|
| 1081 |
|
|---|
| 1082 | finish:
|
|---|
| 1083 | ;
|
|---|
| 1084 |
|
|---|
| 1085 | int rc2 = EOK;
|
|---|
| 1086 |
|
|---|
| 1087 | /* Set return values */
|
|---|
| 1088 | *iblock = new_block_idx;
|
|---|
| 1089 | *fblock = phys_block;
|
|---|
| 1090 |
|
|---|
| 1091 | /*
|
|---|
| 1092 | * Put loaded blocks
|
|---|
| 1093 | * starting from 1: 0 is a block with inode data
|
|---|
| 1094 | */
|
|---|
| 1095 | for (uint16_t i = 1; i <= path->depth; ++i) {
|
|---|
| 1096 | if (path[i].block) {
|
|---|
| 1097 | rc2 = block_put(path[i].block);
|
|---|
| 1098 | if (rc == EOK && rc2 != EOK)
|
|---|
| 1099 | rc = rc2;
|
|---|
| 1100 | }
|
|---|
| 1101 | }
|
|---|
| 1102 |
|
|---|
| 1103 | /* Destroy temporary data structure */
|
|---|
| 1104 | free(path);
|
|---|
| 1105 |
|
|---|
| 1106 | return rc;
|
|---|
| 1107 | }
|
|---|
| 1108 |
|
|---|
| 1109 | /**
|
|---|
| 1110 | * @}
|
|---|
| 1111 | */
|
|---|