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