[0dc91833] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Frantisek Princ
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libext4
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file libext4_directory_index.c
|
---|
[c25e39b] | 35 | * @brief Ext4 directory index operations.
|
---|
[0dc91833] | 36 | */
|
---|
| 37 |
|
---|
[343ccfd] | 38 | #include <byteorder.h>
|
---|
[0dc91833] | 39 | #include <errno.h>
|
---|
[f577058] | 40 | #include <malloc.h>
|
---|
| 41 | #include <sort.h>
|
---|
[5c83612b] | 42 | #include <string.h>
|
---|
[0dc91833] | 43 | #include "libext4.h"
|
---|
| 44 |
|
---|
[b191acae] | 45 | typedef struct ext4_dx_sort_entry {
|
---|
| 46 | uint32_t hash;
|
---|
| 47 | uint32_t rec_len;
|
---|
| 48 | void *dentry;
|
---|
| 49 | } ext4_dx_sort_entry_t;
|
---|
| 50 |
|
---|
[343ccfd] | 51 |
|
---|
| 52 | uint8_t ext4_directory_dx_root_info_get_hash_version(
|
---|
| 53 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 54 | {
|
---|
| 55 | return root_info->hash_version;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | void ext4_directory_dx_root_info_set_hash_version(
|
---|
| 59 | ext4_directory_dx_root_info_t *root_info, uint8_t version)
|
---|
| 60 | {
|
---|
| 61 | root_info->hash_version = version;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | uint8_t ext4_directory_dx_root_info_get_info_length(
|
---|
| 65 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 66 | {
|
---|
| 67 | return root_info->info_length;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void ext4_directory_dx_root_info_set_info_length(
|
---|
| 71 | ext4_directory_dx_root_info_t *root_info, uint8_t info_length)
|
---|
| 72 | {
|
---|
| 73 | root_info->info_length = info_length;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | uint8_t ext4_directory_dx_root_info_get_indirect_levels(
|
---|
| 77 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 78 | {
|
---|
| 79 | return root_info->indirect_levels;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void ext4_directory_dx_root_info_set_indirect_levels(
|
---|
| 83 | ext4_directory_dx_root_info_t *root_info, uint8_t levels)
|
---|
| 84 | {
|
---|
| 85 | root_info->indirect_levels = levels;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | uint16_t ext4_directory_dx_countlimit_get_limit(
|
---|
| 89 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
| 90 | {
|
---|
| 91 | return uint16_t_le2host(countlimit->limit);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | void ext4_directory_dx_countlimit_set_limit(
|
---|
| 95 | ext4_directory_dx_countlimit_t *countlimit, uint16_t limit)
|
---|
| 96 | {
|
---|
| 97 | countlimit->limit = host2uint16_t_le(limit);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | uint16_t ext4_directory_dx_countlimit_get_count(
|
---|
| 101 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
| 102 | {
|
---|
| 103 | return uint16_t_le2host(countlimit->count);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void ext4_directory_dx_countlimit_set_count(
|
---|
| 107 | ext4_directory_dx_countlimit_t *countlimit, uint16_t count)
|
---|
| 108 | {
|
---|
| 109 | countlimit->count = host2uint16_t_le(count);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
|
---|
| 113 | {
|
---|
| 114 | return uint32_t_le2host(entry->hash);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | void ext4_directory_dx_entry_set_hash(ext4_directory_dx_entry_t *entry,
|
---|
| 118 | uint32_t hash)
|
---|
| 119 | {
|
---|
| 120 | entry->hash = host2uint32_t_le(hash);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
|
---|
| 124 | {
|
---|
| 125 | return uint32_t_le2host(entry->block);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void ext4_directory_dx_entry_set_block(ext4_directory_dx_entry_t *entry,
|
---|
| 129 | uint32_t block)
|
---|
| 130 | {
|
---|
| 131 | entry->block = host2uint32_t_le(block);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | /**************************************************************************/
|
---|
| 136 |
|
---|
| 137 |
|
---|
[0dc91833] | 138 | static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
|
---|
| 139 | ext4_superblock_t *sb, size_t name_len, const char *name)
|
---|
| 140 | {
|
---|
| 141 |
|
---|
[d9bbe45] | 142 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
[0dc91833] | 143 |
|
---|
| 144 | if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
|
---|
| 145 | root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
|
---|
| 146 | root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
|
---|
| 147 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // Check unused flags
|
---|
| 151 | if (root->info.unused_flags != 0) {
|
---|
| 152 | EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
|
---|
| 153 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | // Check indirect levels
|
---|
| 157 | if (root->info.indirect_levels > 1) {
|
---|
| 158 | EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
|
---|
| 159 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[d9bbe45] | 162 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[0dc91833] | 163 |
|
---|
[d9bbe45] | 164 | uint32_t entry_space = block_size;
|
---|
[0dc91833] | 165 | entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
|
---|
| 166 | entry_space -= sizeof(ext4_directory_dx_root_info_t);
|
---|
| 167 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 168 |
|
---|
[d9bbe45] | 169 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
|
---|
[0dc91833] | 170 | if (limit != entry_space) {
|
---|
| 171 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
|
---|
| 175 | if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
|
---|
| 176 | && (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
|
---|
| 177 | // 3 is magic from ext4 linux implementation
|
---|
| 178 | hinfo->hash_version += 3;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | hinfo->seed = ext4_superblock_get_hash_seed(sb);
|
---|
| 182 |
|
---|
| 183 | if (name) {
|
---|
| 184 | ext4_hash_string(hinfo, name_len, name);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | return EOK;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
|
---|
[9104bb5] | 191 | ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, block_t *root_block,
|
---|
[0dc91833] | 192 | ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
|
---|
| 193 | {
|
---|
| 194 | int rc;
|
---|
[d9bbe45] | 195 |
|
---|
[0dc91833] | 196 | ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
|
---|
| 197 |
|
---|
[d9bbe45] | 198 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
| 199 | ext4_directory_dx_entry_t *entries = (ext4_directory_dx_entry_t *)&root->entries;
|
---|
[0dc91833] | 200 |
|
---|
[d9bbe45] | 201 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
|
---|
| 202 | uint8_t indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
|
---|
[0dc91833] | 203 |
|
---|
[d9bbe45] | 204 | block_t *tmp_block = root_block;
|
---|
| 205 | ext4_directory_dx_entry_t *p, *q, *m, *at;
|
---|
[0dc91833] | 206 | while (true) {
|
---|
| 207 |
|
---|
[d9bbe45] | 208 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
|
---|
[0dc91833] | 209 | if ((count == 0) || (count > limit)) {
|
---|
| 210 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | p = entries + 1;
|
---|
| 214 | q = entries + count - 1;
|
---|
| 215 |
|
---|
| 216 | while (p <= q) {
|
---|
| 217 | m = p + (q - p) / 2;
|
---|
| 218 | if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
|
---|
| 219 | q = m - 1;
|
---|
| 220 | } else {
|
---|
| 221 | p = m + 1;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | at = p - 1;
|
---|
| 226 |
|
---|
| 227 | tmp_dx_block->block = tmp_block;
|
---|
| 228 | tmp_dx_block->entries = entries;
|
---|
| 229 | tmp_dx_block->position = at;
|
---|
| 230 |
|
---|
| 231 | if (indirect_level == 0) {
|
---|
| 232 | *dx_block = tmp_dx_block;
|
---|
| 233 | return EOK;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[d9bbe45] | 236 | uint32_t next_block = ext4_directory_dx_entry_get_block(at);
|
---|
[0dc91833] | 237 |
|
---|
| 238 | indirect_level--;
|
---|
| 239 |
|
---|
[d9bbe45] | 240 | uint32_t fblock;
|
---|
[9104bb5] | 241 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, next_block, &fblock);
|
---|
[0dc91833] | 242 | if (rc != EOK) {
|
---|
| 243 | return rc;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | rc = block_get(&tmp_block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 247 | if (rc != EOK) {
|
---|
| 248 | return rc;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
|
---|
| 252 | limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
|
---|
| 253 |
|
---|
[d9bbe45] | 254 | uint16_t entry_space = ext4_superblock_get_block_size(fs->superblock)
|
---|
| 255 | - sizeof(ext4_directory_dx_dot_entry_t);
|
---|
[0dc91833] | 256 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | if (limit != entry_space) {
|
---|
| 260 | block_put(tmp_block);
|
---|
| 261 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | ++tmp_dx_block;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | // Unreachable
|
---|
| 268 | return EOK;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 |
|
---|
[9104bb5] | 272 | static int ext4_directory_dx_next_block(ext4_filesystem_t *fs,
|
---|
| 273 | ext4_inode_ref_t *inode_ref, uint32_t hash,
|
---|
[0dc91833] | 274 | ext4_directory_dx_block_t *handle, ext4_directory_dx_block_t *handles)
|
---|
| 275 | {
|
---|
[d9bbe45] | 276 | int rc;
|
---|
| 277 |
|
---|
[0dc91833] | 278 |
|
---|
[d9bbe45] | 279 | uint32_t num_handles = 0;
|
---|
| 280 | ext4_directory_dx_block_t *p = handle;
|
---|
[0dc91833] | 281 |
|
---|
| 282 | while (1) {
|
---|
| 283 |
|
---|
| 284 | p->position++;
|
---|
[d9bbe45] | 285 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
|
---|
[0dc91833] | 286 |
|
---|
| 287 | if (p->position < p->entries + count) {
|
---|
| 288 | break;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | if (p == handles) {
|
---|
| 292 | return 0;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | num_handles++;
|
---|
| 296 | p--;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[d9bbe45] | 299 | uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
|
---|
[0dc91833] | 300 |
|
---|
| 301 | if ((hash & 1) == 0) {
|
---|
| 302 | if ((current_hash & ~1) != hash) {
|
---|
| 303 | return 0;
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[d9bbe45] | 307 |
|
---|
[0dc91833] | 308 | while (num_handles--) {
|
---|
| 309 |
|
---|
[d9bbe45] | 310 | uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
|
---|
| 311 | uint32_t block_addr;
|
---|
[9104bb5] | 312 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, block_idx, &block_addr);
|
---|
[0dc91833] | 313 | if (rc != EOK) {
|
---|
| 314 | return rc;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[d9bbe45] | 317 | block_t *block;
|
---|
[0dc91833] | 318 | rc = block_get(&block, fs->device, block_addr, BLOCK_FLAGS_NONE);
|
---|
| 319 | if (rc != EOK) {
|
---|
| 320 | return rc;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | p++;
|
---|
| 324 |
|
---|
| 325 | block_put(p->block);
|
---|
| 326 | p->block = block;
|
---|
| 327 | p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
|
---|
| 328 | p->position = p->entries;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | return 1;
|
---|
| 332 |
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[7689590] | 335 | int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
|
---|
| 336 | ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
|
---|
[0dc91833] | 337 | {
|
---|
| 338 | int rc;
|
---|
| 339 |
|
---|
| 340 | // get direct block 0 (index root)
|
---|
[d9bbe45] | 341 | uint32_t root_block_addr;
|
---|
[9104bb5] | 342 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, 0, &root_block_addr);
|
---|
[0dc91833] | 343 | if (rc != EOK) {
|
---|
| 344 | return rc;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[d9bbe45] | 347 | block_t *root_block;
|
---|
[0dc91833] | 348 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 349 | if (rc != EOK) {
|
---|
| 350 | return rc;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[d9bbe45] | 353 | ext4_hash_info_t hinfo;
|
---|
[7689590] | 354 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
[0dc91833] | 355 | if (rc != EOK) {
|
---|
| 356 | block_put(root_block);
|
---|
| 357 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[565b6ff] | 360 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
[d9bbe45] | 361 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
[e63ce679] | 362 | ext4_directory_dx_block_t *dx_block, *tmp;
|
---|
[9104bb5] | 363 | rc = ext4_directory_dx_get_leaf(&hinfo, fs, inode_ref, root_block, &dx_block, dx_blocks);
|
---|
[0dc91833] | 364 | if (rc != EOK) {
|
---|
| 365 | block_put(root_block);
|
---|
| 366 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 |
|
---|
[d9bbe45] | 370 | do {
|
---|
[0dc91833] | 371 |
|
---|
[d9bbe45] | 372 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
| 373 | uint32_t leaf_block_addr;
|
---|
[9104bb5] | 374 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref, leaf_block_idx, &leaf_block_addr);
|
---|
[0dc91833] | 375 | if (rc != EOK) {
|
---|
[e63ce679] | 376 | goto cleanup;
|
---|
[0dc91833] | 377 | }
|
---|
| 378 |
|
---|
[d9bbe45] | 379 | block_t *leaf_block;
|
---|
[0dc91833] | 380 | rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 381 | if (rc != EOK) {
|
---|
[e63ce679] | 382 | goto cleanup;
|
---|
[0dc91833] | 383 | }
|
---|
| 384 |
|
---|
[d9bbe45] | 385 | ext4_directory_entry_ll_t *res_dentry;
|
---|
[7689590] | 386 | rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
|
---|
| 387 |
|
---|
[0dc91833] | 388 | // Found => return it
|
---|
[7689590] | 389 | if (rc == EOK) {
|
---|
| 390 | result->block = leaf_block;
|
---|
| 391 | result->dentry = res_dentry;
|
---|
[e8d054a] | 392 | goto cleanup;
|
---|
[0dc91833] | 393 | }
|
---|
| 394 |
|
---|
[e63ce679] | 395 | // Not found, leave untouched
|
---|
[0dc91833] | 396 | block_put(leaf_block);
|
---|
| 397 |
|
---|
[e63ce679] | 398 | if (rc != ENOENT) {
|
---|
| 399 | goto cleanup;
|
---|
[0dc91833] | 400 | }
|
---|
| 401 |
|
---|
[9104bb5] | 402 | rc = ext4_directory_dx_next_block(fs, inode_ref, hinfo.hash, dx_block, &dx_blocks[0]);
|
---|
[0dc91833] | 403 | if (rc < 0) {
|
---|
[e63ce679] | 404 | goto cleanup;
|
---|
[0dc91833] | 405 | }
|
---|
| 406 |
|
---|
| 407 | } while (rc == 1);
|
---|
| 408 |
|
---|
[e8d054a] | 409 | rc = ENOENT;
|
---|
[e63ce679] | 410 |
|
---|
| 411 | cleanup:
|
---|
| 412 |
|
---|
| 413 | tmp = dx_blocks;
|
---|
| 414 | while (tmp <= dx_block) {
|
---|
| 415 | block_put(tmp->block);
|
---|
| 416 | ++tmp;
|
---|
| 417 | }
|
---|
| 418 | return rc;
|
---|
[0dc91833] | 419 | }
|
---|
| 420 |
|
---|
[b191acae] | 421 | static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy)
|
---|
[f577058] | 422 | {
|
---|
| 423 | ext4_dx_sort_entry_t *entry1 = arg1;
|
---|
| 424 | ext4_dx_sort_entry_t *entry2 = arg2;
|
---|
| 425 |
|
---|
| 426 | if (entry1->hash == entry2->hash) {
|
---|
| 427 | return 0;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | if (entry1->hash < entry2->hash) {
|
---|
| 431 | return -1;
|
---|
| 432 | } else {
|
---|
| 433 | return 1;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[c0964cd7] | 438 | static void ext4_directory_dx_insert_entry(
|
---|
| 439 | ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
|
---|
| 440 | {
|
---|
| 441 | ext4_directory_dx_entry_t *old_index_entry = index_block->position;
|
---|
| 442 | ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
|
---|
| 443 |
|
---|
| 444 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
| 445 | (ext4_directory_dx_countlimit_t *)index_block->entries;
|
---|
| 446 | uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
| 447 |
|
---|
| 448 | ext4_directory_dx_entry_t *start_index = index_block->entries;
|
---|
| 449 | size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
|
---|
| 450 |
|
---|
| 451 | memmove(new_index_entry + 1, new_index_entry, bytes);
|
---|
| 452 |
|
---|
| 453 | ext4_directory_dx_entry_set_block(new_index_entry, iblock);
|
---|
| 454 | ext4_directory_dx_entry_set_hash(new_index_entry, hash);
|
---|
| 455 |
|
---|
| 456 | ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
|
---|
| 457 |
|
---|
| 458 | index_block->block->dirty = true;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
[f577058] | 461 | static int ext4_directory_dx_split_data(ext4_filesystem_t *fs,
|
---|
| 462 | ext4_inode_ref_t *inode_ref, ext4_hash_info_t *hinfo,
|
---|
[ccac91e] | 463 | block_t *old_data_block, ext4_directory_dx_block_t *index_block, block_t **new_data_block)
|
---|
[f577058] | 464 | {
|
---|
[d0d7afb] | 465 | int rc = EOK;
|
---|
[f577058] | 466 |
|
---|
[e8d054a] | 467 | // Allocate buffer for directory entries
|
---|
[f577058] | 468 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 469 | void *entry_buffer = malloc(block_size);
|
---|
| 470 | if (entry_buffer == NULL) {
|
---|
| 471 | return ENOMEM;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[1d68621] | 474 | // dot entry has the smallest size available
|
---|
| 475 | uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
|
---|
[c4f318d6] | 476 |
|
---|
[e8d054a] | 477 | // Allocate sort entry
|
---|
[1d68621] | 478 | ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
|
---|
[f577058] | 479 | if (sort_array == NULL) {
|
---|
| 480 | free(entry_buffer);
|
---|
| 481 | return ENOMEM;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
[1d68621] | 484 | uint32_t idx = 0;
|
---|
[ccac91e] | 485 | uint32_t real_size = 0;
|
---|
[1d68621] | 486 |
|
---|
[e8d054a] | 487 | // Initialize hinfo
|
---|
[1d68621] | 488 | ext4_hash_info_t tmp_hinfo;
|
---|
| 489 | memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
|
---|
| 490 |
|
---|
[e8d054a] | 491 | // Load all valid entries to the buffer
|
---|
| 492 | ext4_directory_entry_ll_t *dentry = old_data_block->data;
|
---|
| 493 | void *entry_buffer_ptr = entry_buffer;
|
---|
[ccac91e] | 494 | while ((void *)dentry < old_data_block->data + block_size) {
|
---|
[c4f318d6] | 495 |
|
---|
[412f813] | 496 | // Read only valid entries
|
---|
| 497 | if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
|
---|
[f577058] | 498 |
|
---|
[412f813] | 499 | uint8_t len = ext4_directory_entry_ll_get_name_length(fs->superblock, dentry);
|
---|
[e8d054a] | 500 | ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
|
---|
[ccac91e] | 501 |
|
---|
[412f813] | 502 | uint32_t rec_len = 8 + len;
|
---|
| 503 |
|
---|
| 504 | if ((rec_len % 4) != 0) {
|
---|
| 505 | rec_len += 4 - (rec_len % 4);
|
---|
| 506 | }
|
---|
[ccac91e] | 507 |
|
---|
[412f813] | 508 | memcpy(entry_buffer_ptr, dentry, rec_len);
|
---|
[ccac91e] | 509 |
|
---|
[412f813] | 510 | sort_array[idx].dentry = entry_buffer_ptr;
|
---|
| 511 | sort_array[idx].rec_len = rec_len;
|
---|
| 512 | sort_array[idx].hash = tmp_hinfo.hash;
|
---|
[f577058] | 513 |
|
---|
[412f813] | 514 | entry_buffer_ptr += rec_len;
|
---|
| 515 | real_size += rec_len;
|
---|
| 516 | idx++;
|
---|
| 517 | }
|
---|
[ccac91e] | 518 |
|
---|
[f577058] | 519 | dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
| 520 | }
|
---|
| 521 |
|
---|
[b191acae] | 522 | qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t), ext4_directory_dx_entry_comparator, NULL);
|
---|
[f577058] | 523 |
|
---|
| 524 | uint32_t new_fblock;
|
---|
[c4f318d6] | 525 | uint32_t new_iblock;
|
---|
| 526 | rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
|
---|
[f577058] | 527 | if (rc != EOK) {
|
---|
| 528 | free(sort_array);
|
---|
| 529 | free(entry_buffer);
|
---|
| 530 | return rc;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
[ccac91e] | 533 | // Load new block
|
---|
| 534 | block_t *new_data_block_tmp;
|
---|
| 535 | rc = block_get(&new_data_block_tmp, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 536 | if (rc != EOK) {
|
---|
| 537 | free(sort_array);
|
---|
| 538 | free(entry_buffer);
|
---|
| 539 | return rc;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | // Distribute entries to splitted blocks (by size)
|
---|
| 543 | uint32_t new_hash = 0;
|
---|
| 544 | uint32_t current_size = 0;
|
---|
[1d68621] | 545 | uint32_t mid = 0;
|
---|
| 546 | for (uint32_t i = 0; i < idx; ++i) {
|
---|
| 547 | if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
|
---|
[ccac91e] | 548 | new_hash = sort_array[i].hash;
|
---|
| 549 | mid = i;
|
---|
| 550 | break;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | current_size += sort_array[i].rec_len;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[412f813] | 556 | uint32_t continued = 0;
|
---|
| 557 | if (new_hash == sort_array[mid-1].hash) {
|
---|
| 558 | continued = 1;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
[ccac91e] | 561 | uint32_t offset = 0;
|
---|
| 562 | void *ptr;
|
---|
[f577058] | 563 |
|
---|
[ccac91e] | 564 | // First part - to the old block
|
---|
[1d68621] | 565 | for (uint32_t i = 0; i < mid; ++i) {
|
---|
[ccac91e] | 566 | ptr = old_data_block->data + offset;
|
---|
| 567 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
| 568 |
|
---|
| 569 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
| 570 | if (i < (mid - 1)) {
|
---|
| 571 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
| 572 | } else {
|
---|
| 573 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | offset += sort_array[i].rec_len;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | // Second part - to the new block
|
---|
| 580 | offset = 0;
|
---|
[1d68621] | 581 | for (uint32_t i = mid; i < idx; ++i) {
|
---|
[ccac91e] | 582 | ptr = new_data_block_tmp->data + offset;
|
---|
| 583 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
| 584 |
|
---|
| 585 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
| 586 | if (i < (idx - 1)) {
|
---|
| 587 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
| 588 | } else {
|
---|
| 589 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | offset += sort_array[i].rec_len;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | old_data_block->dirty = true;
|
---|
| 596 | new_data_block_tmp->dirty = true;
|
---|
| 597 |
|
---|
| 598 | free(sort_array);
|
---|
| 599 | free(entry_buffer);
|
---|
[f577058] | 600 |
|
---|
[c0964cd7] | 601 | ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
|
---|
[ccac91e] | 602 |
|
---|
| 603 | *new_data_block = new_data_block_tmp;
|
---|
[f577058] | 604 |
|
---|
| 605 | return EOK;
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 |
|
---|
[2f2feadb] | 609 | static int ext4_directory_dx_split_index(ext4_filesystem_t *fs,
|
---|
| 610 | ext4_inode_ref_t *inode_ref, ext4_directory_dx_block_t *dx_blocks,
|
---|
| 611 | ext4_directory_dx_block_t *dx_block)
|
---|
[565b6ff] | 612 | {
|
---|
[2f2feadb] | 613 | int rc;
|
---|
[565b6ff] | 614 |
|
---|
[2f2feadb] | 615 | ext4_directory_dx_entry_t *entries;
|
---|
| 616 | if (dx_block == dx_blocks) {
|
---|
| 617 | entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
|
---|
| 618 | } else {
|
---|
| 619 | entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
|
---|
[565b6ff] | 620 | }
|
---|
| 621 |
|
---|
[2f2feadb] | 622 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
| 623 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
| 624 | uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
|
---|
| 625 | uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
[565b6ff] | 626 |
|
---|
[2f2feadb] | 627 | // Check if is necessary to split index block
|
---|
[1ebb66f] | 628 | if (leaf_limit == leaf_count) {
|
---|
[5c83612b] | 629 | EXT4FS_DBG("need to split index block !!!");
|
---|
| 630 |
|
---|
[c0964cd7] | 631 | unsigned int levels = dx_block - dx_blocks;
|
---|
| 632 |
|
---|
[2f2feadb] | 633 | ext4_directory_dx_entry_t *root_entries =
|
---|
| 634 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
|
---|
| 635 |
|
---|
| 636 | ext4_directory_dx_countlimit_t *root_countlimit =
|
---|
| 637 | (ext4_directory_dx_countlimit_t *)root_entries;
|
---|
| 638 | uint16_t root_limit =
|
---|
| 639 | ext4_directory_dx_countlimit_get_limit(root_countlimit);
|
---|
| 640 | uint16_t root_count =
|
---|
| 641 | ext4_directory_dx_countlimit_get_count(root_countlimit);
|
---|
[c0964cd7] | 642 |
|
---|
| 643 | if ((levels > 0) && (root_limit == root_count)) {
|
---|
| 644 | EXT4FS_DBG("Directory index is full");
|
---|
[2f2feadb] | 645 | return ENOSPC;
|
---|
[c0964cd7] | 646 | }
|
---|
| 647 |
|
---|
| 648 | uint32_t new_fblock;
|
---|
| 649 | uint32_t new_iblock;
|
---|
[2f2feadb] | 650 | rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
|
---|
[c0964cd7] | 651 | if (rc != EOK) {
|
---|
[2f2feadb] | 652 | return rc;
|
---|
[c0964cd7] | 653 | }
|
---|
| 654 |
|
---|
| 655 | // New block allocated
|
---|
| 656 | block_t * new_block;
|
---|
| 657 | rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 658 | if (rc != EOK) {
|
---|
[2f2feadb] | 659 | return rc;
|
---|
[c0964cd7] | 660 | }
|
---|
| 661 |
|
---|
[6f731f0a] | 662 | ext4_directory_dx_node_t *new_node = new_block->data;
|
---|
| 663 | ext4_directory_dx_entry_t *new_entries = new_node->entries;
|
---|
| 664 |
|
---|
[2f2feadb] | 665 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 666 |
|
---|
[c0964cd7] | 667 | if (levels > 0) {
|
---|
[2f2feadb] | 668 | EXT4FS_DBG("split index leaf node");
|
---|
[c0964cd7] | 669 | uint32_t count_left = leaf_count / 2;
|
---|
| 670 | uint32_t count_right = leaf_count - count_left;
|
---|
[2f2feadb] | 671 | uint32_t hash_right =
|
---|
| 672 | ext4_directory_dx_entry_get_hash(entries + count_left);
|
---|
[c0964cd7] | 673 |
|
---|
| 674 | memcpy((void *) new_entries, (void *) (entries + count_left),
|
---|
| 675 | count_right * sizeof(ext4_directory_dx_entry_t));
|
---|
| 676 |
|
---|
[2f2feadb] | 677 | ext4_directory_dx_countlimit_t *left_countlimit =
|
---|
| 678 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
| 679 | ext4_directory_dx_countlimit_t *right_countlimit =
|
---|
| 680 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
[c0964cd7] | 681 |
|
---|
| 682 | ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
|
---|
| 683 | ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
|
---|
| 684 |
|
---|
[2f2feadb] | 685 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 686 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 687 | ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
|
---|
[c0964cd7] | 688 |
|
---|
[2f2feadb] | 689 | // Which index block is target for new entry
|
---|
| 690 | uint32_t position_index = (dx_block->position - dx_block->entries);
|
---|
| 691 | if (position_index >= count_left) {
|
---|
[c0964cd7] | 692 |
|
---|
[2f2feadb] | 693 | dx_block->block->dirty = true;
|
---|
| 694 |
|
---|
| 695 | block_t *block_tmp = dx_block->block;
|
---|
| 696 | dx_block->block = new_block;
|
---|
| 697 | dx_block->position = new_entries + position_index - count_left;
|
---|
| 698 | dx_block->entries = new_entries;
|
---|
| 699 |
|
---|
| 700 | new_block = block_tmp;
|
---|
| 701 |
|
---|
| 702 | }
|
---|
[c0964cd7] | 703 |
|
---|
[2f2feadb] | 704 | ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
|
---|
[c0964cd7] | 705 |
|
---|
[2f2feadb] | 706 | return block_put(new_block);
|
---|
[c0964cd7] | 707 |
|
---|
| 708 | } else {
|
---|
| 709 | EXT4FS_DBG("create second level");
|
---|
[6f731f0a] | 710 |
|
---|
| 711 | memcpy((void *) new_entries, (void *) entries,
|
---|
| 712 | leaf_count * sizeof(ext4_directory_dx_entry_t));
|
---|
| 713 |
|
---|
| 714 | ext4_directory_dx_countlimit_t *new_countlimit =
|
---|
| 715 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
| 716 |
|
---|
[2f2feadb] | 717 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 718 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 719 | ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
|
---|
[6f731f0a] | 720 |
|
---|
[2f2feadb] | 721 | // Set values in root node
|
---|
| 722 | ext4_directory_dx_countlimit_t *new_root_countlimit =
|
---|
| 723 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
[6f731f0a] | 724 |
|
---|
[2f2feadb] | 725 | ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
|
---|
| 726 | ext4_directory_dx_entry_set_block(entries, new_iblock);
|
---|
[6f731f0a] | 727 |
|
---|
[2f2feadb] | 728 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
|
---|
[6f731f0a] | 729 |
|
---|
[2f2feadb] | 730 | /* Add new access path frame */
|
---|
| 731 | dx_block = dx_blocks + 1;
|
---|
| 732 | dx_block->position = dx_block->position - entries + new_entries;
|
---|
| 733 | dx_block->entries = new_entries;
|
---|
| 734 | dx_block->block = new_block;
|
---|
[c0964cd7] | 735 | }
|
---|
[2f2feadb] | 736 |
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | return EOK;
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | int ext4_directory_dx_add_entry(ext4_filesystem_t *fs,
|
---|
| 743 | ext4_inode_ref_t *parent, ext4_inode_ref_t *child, const char *name)
|
---|
| 744 | {
|
---|
| 745 | int rc = EOK;
|
---|
| 746 | int rc2 = EOK;
|
---|
| 747 |
|
---|
| 748 | // get direct block 0 (index root)
|
---|
| 749 | uint32_t root_block_addr;
|
---|
[9104bb5] | 750 | rc = ext4_filesystem_get_inode_data_block_index(fs, parent, 0, &root_block_addr);
|
---|
[2f2feadb] | 751 | if (rc != EOK) {
|
---|
| 752 | return rc;
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 | block_t *root_block;
|
---|
| 756 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 757 | if (rc != EOK) {
|
---|
| 758 | return rc;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | uint32_t name_len = strlen(name);
|
---|
| 762 | ext4_hash_info_t hinfo;
|
---|
| 763 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
| 764 | if (rc != EOK) {
|
---|
| 765 | block_put(root_block);
|
---|
| 766 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 767 | }
|
---|
| 768 |
|
---|
| 769 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
| 770 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
| 771 | ext4_directory_dx_block_t *dx_block, *dx_it;
|
---|
[9104bb5] | 772 | rc = ext4_directory_dx_get_leaf(&hinfo, fs, parent, root_block, &dx_block, dx_blocks);
|
---|
[2f2feadb] | 773 | if (rc != EOK) {
|
---|
| 774 | rc = EXT4_ERR_BAD_DX_DIR;
|
---|
| 775 | goto release_index;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 |
|
---|
| 779 | // Try to insert to existing data block
|
---|
| 780 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
| 781 | uint32_t leaf_block_addr;
|
---|
[9104bb5] | 782 | rc = ext4_filesystem_get_inode_data_block_index(fs, parent, leaf_block_idx, &leaf_block_addr);
|
---|
[2f2feadb] | 783 | if (rc != EOK) {
|
---|
| 784 | goto release_index;
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 |
|
---|
| 788 | block_t *target_block;
|
---|
| 789 | rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 790 | if (rc != EOK) {
|
---|
| 791 | goto release_index;
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 |
|
---|
| 795 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
| 796 | if (rc == EOK) {
|
---|
| 797 | goto release_target_index;
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | EXT4FS_DBG("no free space found");
|
---|
| 801 |
|
---|
| 802 | rc = ext4_directory_dx_split_index(fs, parent, dx_blocks, dx_block);
|
---|
| 803 | if (rc != EOK) {
|
---|
| 804 | goto release_target_index;
|
---|
[c4f318d6] | 805 | }
|
---|
[5c83612b] | 806 |
|
---|
[c4f318d6] | 807 | block_t *new_block = NULL;
|
---|
| 808 | rc = ext4_directory_dx_split_data(fs, parent, &hinfo, target_block, dx_block, &new_block);
|
---|
| 809 | if (rc != EOK) {
|
---|
[e63ce679] | 810 | rc2 = rc;
|
---|
| 811 | goto release_target_index;
|
---|
[c4f318d6] | 812 | }
|
---|
[5c83612b] | 813 |
|
---|
[d0d7afb] | 814 | // Where to save new entry
|
---|
[1d68621] | 815 | uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
|
---|
[c4f318d6] | 816 | if (hinfo.hash >= new_block_hash) {
|
---|
[d0d7afb] | 817 | rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
|
---|
[c4f318d6] | 818 | } else {
|
---|
[d0d7afb] | 819 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
[c4f318d6] | 820 | }
|
---|
[5c83612b] | 821 |
|
---|
[60b8b99] | 822 |
|
---|
| 823 |
|
---|
| 824 |
|
---|
[d0d7afb] | 825 | rc = block_put(new_block);
|
---|
| 826 | if (rc != EOK) {
|
---|
| 827 | EXT4FS_DBG("error writing new block");
|
---|
[60b8b99] | 828 | return rc;
|
---|
[d0d7afb] | 829 | }
|
---|
[c4f318d6] | 830 |
|
---|
[e63ce679] | 831 | release_target_index:
|
---|
[c4f318d6] | 832 |
|
---|
[d0d7afb] | 833 | rc2 = rc;
|
---|
[c4f318d6] | 834 |
|
---|
[412f813] | 835 | rc = block_put(target_block);
|
---|
| 836 | if (rc != EOK) {
|
---|
[e8d054a] | 837 | EXT4FS_DBG("error writing target block");
|
---|
[60b8b99] | 838 | return rc;
|
---|
[412f813] | 839 | }
|
---|
[c4f318d6] | 840 |
|
---|
[e63ce679] | 841 | release_index:
|
---|
[60b8b99] | 842 |
|
---|
| 843 | if (rc != EOK) {
|
---|
| 844 | rc2 = rc;
|
---|
| 845 | }
|
---|
| 846 |
|
---|
[e63ce679] | 847 | dx_it = dx_blocks;
|
---|
[c4f318d6] | 848 |
|
---|
| 849 | while (dx_it <= dx_block) {
|
---|
[412f813] | 850 | rc = block_put(dx_it->block);
|
---|
| 851 | if (rc != EOK) {
|
---|
[e8d054a] | 852 | EXT4FS_DBG("error writing index block");
|
---|
[60b8b99] | 853 | return rc;
|
---|
[412f813] | 854 | }
|
---|
[c4f318d6] | 855 | dx_it++;
|
---|
| 856 | }
|
---|
| 857 |
|
---|
[d0d7afb] | 858 | return rc2;
|
---|
[565b6ff] | 859 | }
|
---|
[0dc91833] | 860 |
|
---|
| 861 |
|
---|
| 862 | /**
|
---|
| 863 | * @}
|
---|
| 864 | */
|
---|