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