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