[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,
|
---|
| 191 | ext4_filesystem_t *fs, ext4_inode_t *inode, block_t *root_block,
|
---|
| 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;
|
---|
[0dc91833] | 241 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode, next_block, &fblock);
|
---|
| 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 |
|
---|
| 272 | static int ext4_directory_dx_next_block(ext4_filesystem_t *fs, ext4_inode_t *inode, uint32_t hash,
|
---|
| 273 | ext4_directory_dx_block_t *handle, ext4_directory_dx_block_t *handles)
|
---|
| 274 | {
|
---|
[d9bbe45] | 275 | int rc;
|
---|
| 276 |
|
---|
[0dc91833] | 277 |
|
---|
[d9bbe45] | 278 | uint32_t num_handles = 0;
|
---|
| 279 | ext4_directory_dx_block_t *p = handle;
|
---|
[0dc91833] | 280 |
|
---|
| 281 | while (1) {
|
---|
| 282 |
|
---|
| 283 | p->position++;
|
---|
[d9bbe45] | 284 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
|
---|
[0dc91833] | 285 |
|
---|
| 286 | if (p->position < p->entries + count) {
|
---|
| 287 | break;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | if (p == handles) {
|
---|
| 291 | return 0;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | num_handles++;
|
---|
| 295 | p--;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[d9bbe45] | 298 | uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
|
---|
[0dc91833] | 299 |
|
---|
| 300 | if ((hash & 1) == 0) {
|
---|
| 301 | if ((current_hash & ~1) != hash) {
|
---|
| 302 | return 0;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[d9bbe45] | 306 |
|
---|
[0dc91833] | 307 | while (num_handles--) {
|
---|
| 308 |
|
---|
[d9bbe45] | 309 | uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
|
---|
| 310 | uint32_t block_addr;
|
---|
[0dc91833] | 311 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode, block_idx, &block_addr);
|
---|
| 312 | if (rc != EOK) {
|
---|
| 313 | return rc;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[d9bbe45] | 316 | block_t *block;
|
---|
[0dc91833] | 317 | rc = block_get(&block, fs->device, block_addr, BLOCK_FLAGS_NONE);
|
---|
| 318 | if (rc != EOK) {
|
---|
| 319 | return rc;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | p++;
|
---|
| 323 |
|
---|
| 324 | block_put(p->block);
|
---|
| 325 | p->block = block;
|
---|
| 326 | p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
|
---|
| 327 | p->position = p->entries;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | return 1;
|
---|
| 331 |
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[7689590] | 334 | int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
|
---|
| 335 | ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
|
---|
[0dc91833] | 336 | {
|
---|
| 337 | int rc;
|
---|
| 338 |
|
---|
| 339 | // get direct block 0 (index root)
|
---|
[d9bbe45] | 340 | uint32_t root_block_addr;
|
---|
[0dc91833] | 341 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, 0, &root_block_addr);
|
---|
| 342 | if (rc != EOK) {
|
---|
| 343 | return rc;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[d9bbe45] | 346 | block_t *root_block;
|
---|
[0dc91833] | 347 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 348 | if (rc != EOK) {
|
---|
| 349 | return rc;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
[d9bbe45] | 352 | ext4_hash_info_t hinfo;
|
---|
[7689590] | 353 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
[0dc91833] | 354 | if (rc != EOK) {
|
---|
| 355 | block_put(root_block);
|
---|
| 356 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[565b6ff] | 359 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
[d9bbe45] | 360 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
[e63ce679] | 361 | ext4_directory_dx_block_t *dx_block, *tmp;
|
---|
[0dc91833] | 362 | rc = ext4_directory_dx_get_leaf(&hinfo, fs, inode_ref->inode, root_block, &dx_block, dx_blocks);
|
---|
| 363 | if (rc != EOK) {
|
---|
| 364 | block_put(root_block);
|
---|
| 365 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 |
|
---|
[d9bbe45] | 369 | do {
|
---|
[0dc91833] | 370 |
|
---|
[d9bbe45] | 371 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
| 372 | uint32_t leaf_block_addr;
|
---|
[0dc91833] | 373 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, leaf_block_idx, &leaf_block_addr);
|
---|
| 374 | if (rc != EOK) {
|
---|
[e63ce679] | 375 | goto cleanup;
|
---|
[0dc91833] | 376 | }
|
---|
| 377 |
|
---|
[d9bbe45] | 378 | block_t *leaf_block;
|
---|
[0dc91833] | 379 | rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 380 | if (rc != EOK) {
|
---|
[e63ce679] | 381 | goto cleanup;
|
---|
[0dc91833] | 382 | }
|
---|
| 383 |
|
---|
[d9bbe45] | 384 | ext4_directory_entry_ll_t *res_dentry;
|
---|
[7689590] | 385 | rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
|
---|
| 386 |
|
---|
[0dc91833] | 387 | // Found => return it
|
---|
[7689590] | 388 | if (rc == EOK) {
|
---|
| 389 | result->block = leaf_block;
|
---|
| 390 | result->dentry = res_dentry;
|
---|
[e8d054a] | 391 | goto cleanup;
|
---|
[0dc91833] | 392 | }
|
---|
| 393 |
|
---|
[e63ce679] | 394 | // Not found, leave untouched
|
---|
[0dc91833] | 395 | block_put(leaf_block);
|
---|
| 396 |
|
---|
[e63ce679] | 397 | if (rc != ENOENT) {
|
---|
| 398 | goto cleanup;
|
---|
[0dc91833] | 399 | }
|
---|
| 400 |
|
---|
| 401 | rc = ext4_directory_dx_next_block(fs, inode_ref->inode, hinfo.hash, dx_block, &dx_blocks[0]);
|
---|
| 402 | if (rc < 0) {
|
---|
[e63ce679] | 403 | goto cleanup;
|
---|
[0dc91833] | 404 | }
|
---|
| 405 |
|
---|
| 406 | } while (rc == 1);
|
---|
| 407 |
|
---|
[e8d054a] | 408 | rc = ENOENT;
|
---|
[e63ce679] | 409 |
|
---|
| 410 | cleanup:
|
---|
| 411 |
|
---|
| 412 | tmp = dx_blocks;
|
---|
| 413 | while (tmp <= dx_block) {
|
---|
| 414 | block_put(tmp->block);
|
---|
| 415 | ++tmp;
|
---|
| 416 | }
|
---|
| 417 | return rc;
|
---|
[0dc91833] | 418 | }
|
---|
| 419 |
|
---|
[b191acae] | 420 | static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy)
|
---|
[f577058] | 421 | {
|
---|
| 422 | ext4_dx_sort_entry_t *entry1 = arg1;
|
---|
| 423 | ext4_dx_sort_entry_t *entry2 = arg2;
|
---|
| 424 |
|
---|
| 425 | if (entry1->hash == entry2->hash) {
|
---|
| 426 | return 0;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | if (entry1->hash < entry2->hash) {
|
---|
| 430 | return -1;
|
---|
| 431 | } else {
|
---|
| 432 | return 1;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | }
|
---|
| 436 |
|
---|
[c0964cd7] | 437 | static void ext4_directory_dx_insert_entry(
|
---|
| 438 | ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
|
---|
| 439 | {
|
---|
| 440 | ext4_directory_dx_entry_t *old_index_entry = index_block->position;
|
---|
| 441 | ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
|
---|
| 442 |
|
---|
| 443 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
| 444 | (ext4_directory_dx_countlimit_t *)index_block->entries;
|
---|
| 445 | uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
| 446 |
|
---|
| 447 | ext4_directory_dx_entry_t *start_index = index_block->entries;
|
---|
| 448 | size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
|
---|
| 449 |
|
---|
| 450 | memmove(new_index_entry + 1, new_index_entry, bytes);
|
---|
| 451 |
|
---|
| 452 | ext4_directory_dx_entry_set_block(new_index_entry, iblock);
|
---|
| 453 | ext4_directory_dx_entry_set_hash(new_index_entry, hash);
|
---|
| 454 |
|
---|
| 455 | ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
|
---|
| 456 |
|
---|
| 457 | index_block->block->dirty = true;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[f577058] | 460 | static int ext4_directory_dx_split_data(ext4_filesystem_t *fs,
|
---|
| 461 | ext4_inode_ref_t *inode_ref, ext4_hash_info_t *hinfo,
|
---|
[ccac91e] | 462 | block_t *old_data_block, ext4_directory_dx_block_t *index_block, block_t **new_data_block)
|
---|
[f577058] | 463 | {
|
---|
[d0d7afb] | 464 | int rc = EOK;
|
---|
[f577058] | 465 |
|
---|
[e8d054a] | 466 | // Allocate buffer for directory entries
|
---|
[f577058] | 467 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 468 | void *entry_buffer = malloc(block_size);
|
---|
| 469 | if (entry_buffer == NULL) {
|
---|
| 470 | return ENOMEM;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[1d68621] | 473 | // dot entry has the smallest size available
|
---|
| 474 | uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
|
---|
[c4f318d6] | 475 |
|
---|
[e8d054a] | 476 | // Allocate sort entry
|
---|
[1d68621] | 477 | ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
|
---|
[f577058] | 478 | if (sort_array == NULL) {
|
---|
| 479 | free(entry_buffer);
|
---|
| 480 | return ENOMEM;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
[1d68621] | 483 | uint32_t idx = 0;
|
---|
[ccac91e] | 484 | uint32_t real_size = 0;
|
---|
[1d68621] | 485 |
|
---|
[e8d054a] | 486 | // Initialize hinfo
|
---|
[1d68621] | 487 | ext4_hash_info_t tmp_hinfo;
|
---|
| 488 | memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
|
---|
| 489 |
|
---|
[e8d054a] | 490 | // Load all valid entries to the buffer
|
---|
| 491 | ext4_directory_entry_ll_t *dentry = old_data_block->data;
|
---|
| 492 | void *entry_buffer_ptr = entry_buffer;
|
---|
[ccac91e] | 493 | while ((void *)dentry < old_data_block->data + block_size) {
|
---|
[c4f318d6] | 494 |
|
---|
[412f813] | 495 | // Read only valid entries
|
---|
| 496 | if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
|
---|
[f577058] | 497 |
|
---|
[412f813] | 498 | uint8_t len = ext4_directory_entry_ll_get_name_length(fs->superblock, dentry);
|
---|
[e8d054a] | 499 | ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
|
---|
[ccac91e] | 500 |
|
---|
[412f813] | 501 | uint32_t rec_len = 8 + len;
|
---|
| 502 |
|
---|
| 503 | if ((rec_len % 4) != 0) {
|
---|
| 504 | rec_len += 4 - (rec_len % 4);
|
---|
| 505 | }
|
---|
[ccac91e] | 506 |
|
---|
[412f813] | 507 | memcpy(entry_buffer_ptr, dentry, rec_len);
|
---|
[ccac91e] | 508 |
|
---|
[412f813] | 509 | sort_array[idx].dentry = entry_buffer_ptr;
|
---|
| 510 | sort_array[idx].rec_len = rec_len;
|
---|
| 511 | sort_array[idx].hash = tmp_hinfo.hash;
|
---|
[f577058] | 512 |
|
---|
[412f813] | 513 | entry_buffer_ptr += rec_len;
|
---|
| 514 | real_size += rec_len;
|
---|
| 515 | idx++;
|
---|
| 516 | }
|
---|
[ccac91e] | 517 |
|
---|
[f577058] | 518 | dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[b191acae] | 521 | qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t), ext4_directory_dx_entry_comparator, NULL);
|
---|
[f577058] | 522 |
|
---|
| 523 | uint32_t new_fblock;
|
---|
[c4f318d6] | 524 | uint32_t new_iblock;
|
---|
| 525 | rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
|
---|
[f577058] | 526 | if (rc != EOK) {
|
---|
| 527 | free(sort_array);
|
---|
| 528 | free(entry_buffer);
|
---|
| 529 | return rc;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
[ccac91e] | 532 | // Load new block
|
---|
| 533 | block_t *new_data_block_tmp;
|
---|
| 534 | rc = block_get(&new_data_block_tmp, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 535 | if (rc != EOK) {
|
---|
| 536 | free(sort_array);
|
---|
| 537 | free(entry_buffer);
|
---|
| 538 | return rc;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | // Distribute entries to splitted blocks (by size)
|
---|
| 542 | uint32_t new_hash = 0;
|
---|
| 543 | uint32_t current_size = 0;
|
---|
[1d68621] | 544 | uint32_t mid = 0;
|
---|
| 545 | for (uint32_t i = 0; i < idx; ++i) {
|
---|
| 546 | if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
|
---|
[ccac91e] | 547 | new_hash = sort_array[i].hash;
|
---|
| 548 | mid = i;
|
---|
| 549 | break;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | current_size += sort_array[i].rec_len;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
[412f813] | 555 | uint32_t continued = 0;
|
---|
| 556 | if (new_hash == sort_array[mid-1].hash) {
|
---|
| 557 | continued = 1;
|
---|
| 558 | }
|
---|
| 559 |
|
---|
[ccac91e] | 560 | uint32_t offset = 0;
|
---|
| 561 | void *ptr;
|
---|
[f577058] | 562 |
|
---|
[ccac91e] | 563 | // First part - to the old block
|
---|
[1d68621] | 564 | for (uint32_t i = 0; i < mid; ++i) {
|
---|
[ccac91e] | 565 | ptr = old_data_block->data + offset;
|
---|
| 566 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
| 567 |
|
---|
| 568 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
| 569 | if (i < (mid - 1)) {
|
---|
| 570 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
| 571 | } else {
|
---|
| 572 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | offset += sort_array[i].rec_len;
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | // Second part - to the new block
|
---|
| 579 | offset = 0;
|
---|
[1d68621] | 580 | for (uint32_t i = mid; i < idx; ++i) {
|
---|
[ccac91e] | 581 | ptr = new_data_block_tmp->data + offset;
|
---|
| 582 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
| 583 |
|
---|
| 584 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
| 585 | if (i < (idx - 1)) {
|
---|
| 586 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
| 587 | } else {
|
---|
| 588 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | offset += sort_array[i].rec_len;
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | old_data_block->dirty = true;
|
---|
| 595 | new_data_block_tmp->dirty = true;
|
---|
| 596 |
|
---|
| 597 | free(sort_array);
|
---|
| 598 | free(entry_buffer);
|
---|
[f577058] | 599 |
|
---|
[c0964cd7] | 600 | ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
|
---|
[ccac91e] | 601 |
|
---|
| 602 | *new_data_block = new_data_block_tmp;
|
---|
[f577058] | 603 |
|
---|
| 604 | return EOK;
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 |
|
---|
[2f2feadb] | 608 | static int ext4_directory_dx_split_index(ext4_filesystem_t *fs,
|
---|
| 609 | ext4_inode_ref_t *inode_ref, ext4_directory_dx_block_t *dx_blocks,
|
---|
| 610 | ext4_directory_dx_block_t *dx_block)
|
---|
[565b6ff] | 611 | {
|
---|
[2f2feadb] | 612 | int rc;
|
---|
[565b6ff] | 613 |
|
---|
[2f2feadb] | 614 | ext4_directory_dx_entry_t *entries;
|
---|
| 615 | if (dx_block == dx_blocks) {
|
---|
| 616 | entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
|
---|
| 617 | } else {
|
---|
| 618 | entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
|
---|
[565b6ff] | 619 | }
|
---|
| 620 |
|
---|
[2f2feadb] | 621 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
| 622 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
| 623 | uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
|
---|
| 624 | uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
[565b6ff] | 625 |
|
---|
[2f2feadb] | 626 | // Check if is necessary to split index block
|
---|
[1ebb66f] | 627 | if (leaf_limit == leaf_count) {
|
---|
[5c83612b] | 628 | EXT4FS_DBG("need to split index block !!!");
|
---|
| 629 |
|
---|
[c0964cd7] | 630 | unsigned int levels = dx_block - dx_blocks;
|
---|
| 631 |
|
---|
[2f2feadb] | 632 | ext4_directory_dx_entry_t *root_entries =
|
---|
| 633 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
|
---|
| 634 |
|
---|
| 635 | ext4_directory_dx_countlimit_t *root_countlimit =
|
---|
| 636 | (ext4_directory_dx_countlimit_t *)root_entries;
|
---|
| 637 | uint16_t root_limit =
|
---|
| 638 | ext4_directory_dx_countlimit_get_limit(root_countlimit);
|
---|
| 639 | uint16_t root_count =
|
---|
| 640 | ext4_directory_dx_countlimit_get_count(root_countlimit);
|
---|
[c0964cd7] | 641 |
|
---|
| 642 | if ((levels > 0) && (root_limit == root_count)) {
|
---|
| 643 | EXT4FS_DBG("Directory index is full");
|
---|
[2f2feadb] | 644 | return ENOSPC;
|
---|
[c0964cd7] | 645 | }
|
---|
| 646 |
|
---|
| 647 | uint32_t new_fblock;
|
---|
| 648 | uint32_t new_iblock;
|
---|
[2f2feadb] | 649 | rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
|
---|
[c0964cd7] | 650 | if (rc != EOK) {
|
---|
[2f2feadb] | 651 | return rc;
|
---|
[c0964cd7] | 652 | }
|
---|
| 653 |
|
---|
| 654 | // New block allocated
|
---|
| 655 | block_t * new_block;
|
---|
| 656 | rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 657 | if (rc != EOK) {
|
---|
[2f2feadb] | 658 | return rc;
|
---|
[c0964cd7] | 659 | }
|
---|
| 660 |
|
---|
[6f731f0a] | 661 | ext4_directory_dx_node_t *new_node = new_block->data;
|
---|
| 662 | ext4_directory_dx_entry_t *new_entries = new_node->entries;
|
---|
| 663 |
|
---|
[2f2feadb] | 664 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 665 |
|
---|
[c0964cd7] | 666 | if (levels > 0) {
|
---|
[2f2feadb] | 667 | EXT4FS_DBG("split index leaf node");
|
---|
[c0964cd7] | 668 | uint32_t count_left = leaf_count / 2;
|
---|
| 669 | uint32_t count_right = leaf_count - count_left;
|
---|
[2f2feadb] | 670 | uint32_t hash_right =
|
---|
| 671 | ext4_directory_dx_entry_get_hash(entries + count_left);
|
---|
[c0964cd7] | 672 |
|
---|
| 673 | memcpy((void *) new_entries, (void *) (entries + count_left),
|
---|
| 674 | count_right * sizeof(ext4_directory_dx_entry_t));
|
---|
| 675 |
|
---|
[2f2feadb] | 676 | ext4_directory_dx_countlimit_t *left_countlimit =
|
---|
| 677 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
| 678 | ext4_directory_dx_countlimit_t *right_countlimit =
|
---|
| 679 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
[c0964cd7] | 680 |
|
---|
| 681 | ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
|
---|
| 682 | ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
|
---|
| 683 |
|
---|
[2f2feadb] | 684 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 685 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 686 | ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
|
---|
[c0964cd7] | 687 |
|
---|
[2f2feadb] | 688 | // Which index block is target for new entry
|
---|
| 689 | uint32_t position_index = (dx_block->position - dx_block->entries);
|
---|
| 690 | if (position_index >= count_left) {
|
---|
[c0964cd7] | 691 |
|
---|
[2f2feadb] | 692 | dx_block->block->dirty = true;
|
---|
| 693 |
|
---|
| 694 | block_t *block_tmp = dx_block->block;
|
---|
| 695 | dx_block->block = new_block;
|
---|
| 696 | dx_block->position = new_entries + position_index - count_left;
|
---|
| 697 | dx_block->entries = new_entries;
|
---|
| 698 |
|
---|
| 699 | new_block = block_tmp;
|
---|
| 700 |
|
---|
| 701 | }
|
---|
[c0964cd7] | 702 |
|
---|
[2f2feadb] | 703 | ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
|
---|
[c0964cd7] | 704 |
|
---|
[2f2feadb] | 705 | return block_put(new_block);
|
---|
[c0964cd7] | 706 |
|
---|
| 707 | } else {
|
---|
| 708 | EXT4FS_DBG("create second level");
|
---|
[6f731f0a] | 709 |
|
---|
| 710 | memcpy((void *) new_entries, (void *) entries,
|
---|
| 711 | leaf_count * sizeof(ext4_directory_dx_entry_t));
|
---|
| 712 |
|
---|
| 713 | ext4_directory_dx_countlimit_t *new_countlimit =
|
---|
| 714 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
| 715 |
|
---|
[2f2feadb] | 716 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 717 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 718 | ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
|
---|
[6f731f0a] | 719 |
|
---|
[2f2feadb] | 720 | // Set values in root node
|
---|
| 721 | ext4_directory_dx_countlimit_t *new_root_countlimit =
|
---|
| 722 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
[6f731f0a] | 723 |
|
---|
[2f2feadb] | 724 | ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
|
---|
| 725 | ext4_directory_dx_entry_set_block(entries, new_iblock);
|
---|
[6f731f0a] | 726 |
|
---|
[2f2feadb] | 727 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
|
---|
[6f731f0a] | 728 |
|
---|
[2f2feadb] | 729 | /* Add new access path frame */
|
---|
| 730 | dx_block = dx_blocks + 1;
|
---|
| 731 | dx_block->position = dx_block->position - entries + new_entries;
|
---|
| 732 | dx_block->entries = new_entries;
|
---|
| 733 | dx_block->block = new_block;
|
---|
[c0964cd7] | 734 | }
|
---|
[2f2feadb] | 735 |
|
---|
| 736 | }
|
---|
| 737 |
|
---|
| 738 | return EOK;
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | int ext4_directory_dx_add_entry(ext4_filesystem_t *fs,
|
---|
| 742 | ext4_inode_ref_t *parent, ext4_inode_ref_t *child, const char *name)
|
---|
| 743 | {
|
---|
| 744 | int rc = EOK;
|
---|
| 745 | int rc2 = EOK;
|
---|
| 746 |
|
---|
| 747 | // get direct block 0 (index root)
|
---|
| 748 | uint32_t root_block_addr;
|
---|
| 749 | rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, 0, &root_block_addr);
|
---|
| 750 | if (rc != EOK) {
|
---|
| 751 | return rc;
|
---|
| 752 | }
|
---|
| 753 |
|
---|
| 754 | block_t *root_block;
|
---|
| 755 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 756 | if (rc != EOK) {
|
---|
| 757 | return rc;
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | uint32_t name_len = strlen(name);
|
---|
| 761 | ext4_hash_info_t hinfo;
|
---|
| 762 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
| 763 | if (rc != EOK) {
|
---|
| 764 | block_put(root_block);
|
---|
| 765 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
| 769 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
| 770 | ext4_directory_dx_block_t *dx_block, *dx_it;
|
---|
| 771 | rc = ext4_directory_dx_get_leaf(&hinfo, fs, parent->inode, root_block, &dx_block, dx_blocks);
|
---|
| 772 | if (rc != EOK) {
|
---|
| 773 | rc = EXT4_ERR_BAD_DX_DIR;
|
---|
| 774 | goto release_index;
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 |
|
---|
| 778 | // Try to insert to existing data block
|
---|
| 779 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
| 780 | uint32_t leaf_block_addr;
|
---|
| 781 | rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, leaf_block_idx, &leaf_block_addr);
|
---|
| 782 | if (rc != EOK) {
|
---|
| 783 | goto release_index;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 |
|
---|
| 787 | block_t *target_block;
|
---|
| 788 | rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 789 | if (rc != EOK) {
|
---|
| 790 | goto release_index;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 |
|
---|
| 794 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
| 795 | if (rc == EOK) {
|
---|
| 796 | goto release_target_index;
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 | EXT4FS_DBG("no free space found");
|
---|
| 800 |
|
---|
| 801 | rc = ext4_directory_dx_split_index(fs, parent, dx_blocks, dx_block);
|
---|
| 802 | if (rc != EOK) {
|
---|
| 803 | goto release_target_index;
|
---|
[c4f318d6] | 804 | }
|
---|
[5c83612b] | 805 |
|
---|
[c4f318d6] | 806 | block_t *new_block = NULL;
|
---|
| 807 | rc = ext4_directory_dx_split_data(fs, parent, &hinfo, target_block, dx_block, &new_block);
|
---|
| 808 | if (rc != EOK) {
|
---|
[e63ce679] | 809 | rc2 = rc;
|
---|
| 810 | goto release_target_index;
|
---|
[c4f318d6] | 811 | }
|
---|
[5c83612b] | 812 |
|
---|
[d0d7afb] | 813 | // Where to save new entry
|
---|
[1d68621] | 814 | uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
|
---|
[c4f318d6] | 815 | if (hinfo.hash >= new_block_hash) {
|
---|
[d0d7afb] | 816 | rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
|
---|
[c4f318d6] | 817 | } else {
|
---|
[d0d7afb] | 818 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
[c4f318d6] | 819 | }
|
---|
[5c83612b] | 820 |
|
---|
[60b8b99] | 821 |
|
---|
| 822 |
|
---|
| 823 |
|
---|
[d0d7afb] | 824 | rc = block_put(new_block);
|
---|
| 825 | if (rc != EOK) {
|
---|
| 826 | EXT4FS_DBG("error writing new block");
|
---|
[60b8b99] | 827 | return rc;
|
---|
[d0d7afb] | 828 | }
|
---|
[c4f318d6] | 829 |
|
---|
[e63ce679] | 830 | release_target_index:
|
---|
[c4f318d6] | 831 |
|
---|
[d0d7afb] | 832 | rc2 = rc;
|
---|
[c4f318d6] | 833 |
|
---|
[412f813] | 834 | rc = block_put(target_block);
|
---|
| 835 | if (rc != EOK) {
|
---|
[e8d054a] | 836 | EXT4FS_DBG("error writing target block");
|
---|
[60b8b99] | 837 | return rc;
|
---|
[412f813] | 838 | }
|
---|
[c4f318d6] | 839 |
|
---|
[e63ce679] | 840 | release_index:
|
---|
[60b8b99] | 841 |
|
---|
| 842 | if (rc != EOK) {
|
---|
| 843 | rc2 = rc;
|
---|
| 844 | }
|
---|
| 845 |
|
---|
[e63ce679] | 846 | dx_it = dx_blocks;
|
---|
[c4f318d6] | 847 |
|
---|
| 848 | while (dx_it <= dx_block) {
|
---|
[412f813] | 849 | rc = block_put(dx_it->block);
|
---|
| 850 | if (rc != EOK) {
|
---|
[e8d054a] | 851 | EXT4FS_DBG("error writing index block");
|
---|
[60b8b99] | 852 | return rc;
|
---|
[412f813] | 853 | }
|
---|
[c4f318d6] | 854 | dx_it++;
|
---|
| 855 | }
|
---|
| 856 |
|
---|
[d0d7afb] | 857 | return rc2;
|
---|
[565b6ff] | 858 | }
|
---|
[0dc91833] | 859 |
|
---|
| 860 |
|
---|
| 861 | /**
|
---|
| 862 | * @}
|
---|
| 863 | */
|
---|