[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 |
|
---|
[343ccfd] | 45 |
|
---|
| 46 | uint8_t ext4_directory_dx_root_info_get_hash_version(
|
---|
| 47 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 48 | {
|
---|
| 49 | return root_info->hash_version;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void ext4_directory_dx_root_info_set_hash_version(
|
---|
| 53 | ext4_directory_dx_root_info_t *root_info, uint8_t version)
|
---|
| 54 | {
|
---|
| 55 | root_info->hash_version = version;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | uint8_t ext4_directory_dx_root_info_get_info_length(
|
---|
| 59 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 60 | {
|
---|
| 61 | return root_info->info_length;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | void ext4_directory_dx_root_info_set_info_length(
|
---|
| 65 | ext4_directory_dx_root_info_t *root_info, uint8_t info_length)
|
---|
| 66 | {
|
---|
| 67 | root_info->info_length = info_length;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | uint8_t ext4_directory_dx_root_info_get_indirect_levels(
|
---|
| 71 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 72 | {
|
---|
| 73 | return root_info->indirect_levels;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void ext4_directory_dx_root_info_set_indirect_levels(
|
---|
| 77 | ext4_directory_dx_root_info_t *root_info, uint8_t levels)
|
---|
| 78 | {
|
---|
| 79 | root_info->indirect_levels = levels;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | uint16_t ext4_directory_dx_countlimit_get_limit(
|
---|
| 83 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
| 84 | {
|
---|
| 85 | return uint16_t_le2host(countlimit->limit);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void ext4_directory_dx_countlimit_set_limit(
|
---|
| 89 | ext4_directory_dx_countlimit_t *countlimit, uint16_t limit)
|
---|
| 90 | {
|
---|
| 91 | countlimit->limit = host2uint16_t_le(limit);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | uint16_t ext4_directory_dx_countlimit_get_count(
|
---|
| 95 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
| 96 | {
|
---|
| 97 | return uint16_t_le2host(countlimit->count);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | void ext4_directory_dx_countlimit_set_count(
|
---|
| 101 | ext4_directory_dx_countlimit_t *countlimit, uint16_t count)
|
---|
| 102 | {
|
---|
| 103 | countlimit->count = host2uint16_t_le(count);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
|
---|
| 107 | {
|
---|
| 108 | return uint32_t_le2host(entry->hash);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void ext4_directory_dx_entry_set_hash(ext4_directory_dx_entry_t *entry,
|
---|
| 112 | uint32_t hash)
|
---|
| 113 | {
|
---|
| 114 | entry->hash = host2uint32_t_le(hash);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
|
---|
| 118 | {
|
---|
| 119 | return uint32_t_le2host(entry->block);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void ext4_directory_dx_entry_set_block(ext4_directory_dx_entry_t *entry,
|
---|
| 123 | uint32_t block)
|
---|
| 124 | {
|
---|
| 125 | entry->block = host2uint32_t_le(block);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | /**************************************************************************/
|
---|
| 130 |
|
---|
| 131 |
|
---|
[0dc91833] | 132 | static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
|
---|
| 133 | ext4_superblock_t *sb, size_t name_len, const char *name)
|
---|
| 134 | {
|
---|
| 135 |
|
---|
[d9bbe45] | 136 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
[0dc91833] | 137 |
|
---|
| 138 | if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
|
---|
| 139 | root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
|
---|
| 140 | root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
|
---|
| 141 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | // Check unused flags
|
---|
| 145 | if (root->info.unused_flags != 0) {
|
---|
| 146 | EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
|
---|
| 147 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // Check indirect levels
|
---|
| 151 | if (root->info.indirect_levels > 1) {
|
---|
| 152 | EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
|
---|
| 153 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[d9bbe45] | 156 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[0dc91833] | 157 |
|
---|
[d9bbe45] | 158 | uint32_t entry_space = block_size;
|
---|
[0dc91833] | 159 | entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
|
---|
| 160 | entry_space -= sizeof(ext4_directory_dx_root_info_t);
|
---|
| 161 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 162 |
|
---|
[d9bbe45] | 163 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
|
---|
[0dc91833] | 164 | if (limit != entry_space) {
|
---|
| 165 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
|
---|
| 169 | if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
|
---|
| 170 | && (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
|
---|
| 171 | // 3 is magic from ext4 linux implementation
|
---|
| 172 | hinfo->hash_version += 3;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | hinfo->seed = ext4_superblock_get_hash_seed(sb);
|
---|
| 176 |
|
---|
| 177 | if (name) {
|
---|
| 178 | ext4_hash_string(hinfo, name_len, name);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | return EOK;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
|
---|
| 185 | ext4_filesystem_t *fs, ext4_inode_t *inode, block_t *root_block,
|
---|
| 186 | ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
|
---|
| 187 | {
|
---|
| 188 | int rc;
|
---|
[d9bbe45] | 189 |
|
---|
[0dc91833] | 190 | ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
|
---|
| 191 |
|
---|
[d9bbe45] | 192 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
| 193 | ext4_directory_dx_entry_t *entries = (ext4_directory_dx_entry_t *)&root->entries;
|
---|
[0dc91833] | 194 |
|
---|
[d9bbe45] | 195 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
|
---|
| 196 | uint8_t indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
|
---|
[0dc91833] | 197 |
|
---|
[d9bbe45] | 198 | block_t *tmp_block = root_block;
|
---|
| 199 | ext4_directory_dx_entry_t *p, *q, *m, *at;
|
---|
[0dc91833] | 200 | while (true) {
|
---|
| 201 |
|
---|
[d9bbe45] | 202 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
|
---|
[0dc91833] | 203 | if ((count == 0) || (count > limit)) {
|
---|
| 204 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | p = entries + 1;
|
---|
| 208 | q = entries + count - 1;
|
---|
| 209 |
|
---|
| 210 | while (p <= q) {
|
---|
| 211 | m = p + (q - p) / 2;
|
---|
| 212 | if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
|
---|
| 213 | q = m - 1;
|
---|
| 214 | } else {
|
---|
| 215 | p = m + 1;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | at = p - 1;
|
---|
| 220 |
|
---|
| 221 | tmp_dx_block->block = tmp_block;
|
---|
| 222 | tmp_dx_block->entries = entries;
|
---|
| 223 | tmp_dx_block->position = at;
|
---|
| 224 |
|
---|
| 225 | if (indirect_level == 0) {
|
---|
| 226 | *dx_block = tmp_dx_block;
|
---|
| 227 | return EOK;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[d9bbe45] | 230 | uint32_t next_block = ext4_directory_dx_entry_get_block(at);
|
---|
[0dc91833] | 231 |
|
---|
| 232 | indirect_level--;
|
---|
| 233 |
|
---|
[d9bbe45] | 234 | uint32_t fblock;
|
---|
[0dc91833] | 235 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode, next_block, &fblock);
|
---|
| 236 | if (rc != EOK) {
|
---|
| 237 | return rc;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | rc = block_get(&tmp_block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 241 | if (rc != EOK) {
|
---|
| 242 | return rc;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
|
---|
| 246 | limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
|
---|
| 247 |
|
---|
[d9bbe45] | 248 | uint16_t entry_space = ext4_superblock_get_block_size(fs->superblock)
|
---|
| 249 | - sizeof(ext4_directory_dx_dot_entry_t);
|
---|
[0dc91833] | 250 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | if (limit != entry_space) {
|
---|
| 254 | block_put(tmp_block);
|
---|
| 255 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | ++tmp_dx_block;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | // Unreachable
|
---|
| 262 | return EOK;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
[d9bbe45] | 266 | static int ext4_directory_dx_find_dir_entry(block_t *block,
|
---|
[0dc91833] | 267 | ext4_superblock_t *sb, size_t name_len, const char *name,
|
---|
| 268 | ext4_directory_entry_ll_t **res_entry, aoff64_t *block_offset)
|
---|
| 269 | {
|
---|
| 270 |
|
---|
[d9bbe45] | 271 | aoff64_t offset = 0;
|
---|
| 272 | ext4_directory_entry_ll_t *dentry = (ext4_directory_entry_ll_t *)block->data;
|
---|
| 273 | uint8_t *addr_limit = block->data + ext4_superblock_get_block_size(sb);
|
---|
[0dc91833] | 274 |
|
---|
| 275 | while ((uint8_t *)dentry < addr_limit) {
|
---|
| 276 |
|
---|
| 277 | if ((uint8_t*) dentry + name_len > addr_limit) {
|
---|
| 278 | break;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | if (dentry->inode != 0) {
|
---|
| 282 | if (name_len == ext4_directory_entry_ll_get_name_length(sb, dentry)) {
|
---|
| 283 | // Compare names
|
---|
| 284 | if (bcmp((uint8_t *)name, dentry->name, name_len) == 0) {
|
---|
| 285 | *block_offset = offset;
|
---|
| 286 | *res_entry = dentry;
|
---|
| 287 | return 1;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 | // Goto next entry
|
---|
[d9bbe45] | 294 | uint16_t dentry_len = ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
[0dc91833] | 295 |
|
---|
| 296 | if (dentry_len == 0) {
|
---|
[ccac91e] | 297 | // Error
|
---|
[0dc91833] | 298 | return -1;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | offset += dentry_len;
|
---|
| 302 | dentry = (ext4_directory_entry_ll_t *)((uint8_t *)dentry + dentry_len);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | return 0;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | static int ext4_directory_dx_next_block(ext4_filesystem_t *fs, ext4_inode_t *inode, uint32_t hash,
|
---|
| 309 | ext4_directory_dx_block_t *handle, ext4_directory_dx_block_t *handles)
|
---|
| 310 | {
|
---|
[d9bbe45] | 311 | int rc;
|
---|
| 312 |
|
---|
[0dc91833] | 313 |
|
---|
[d9bbe45] | 314 | uint32_t num_handles = 0;
|
---|
| 315 | ext4_directory_dx_block_t *p = handle;
|
---|
[0dc91833] | 316 |
|
---|
| 317 | while (1) {
|
---|
| 318 |
|
---|
| 319 | p->position++;
|
---|
[d9bbe45] | 320 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
|
---|
[0dc91833] | 321 |
|
---|
| 322 | if (p->position < p->entries + count) {
|
---|
| 323 | break;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | if (p == handles) {
|
---|
| 327 | return 0;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | num_handles++;
|
---|
| 331 | p--;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[d9bbe45] | 334 | uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
|
---|
[0dc91833] | 335 |
|
---|
| 336 | if ((hash & 1) == 0) {
|
---|
| 337 | if ((current_hash & ~1) != hash) {
|
---|
| 338 | return 0;
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[d9bbe45] | 342 |
|
---|
[0dc91833] | 343 | while (num_handles--) {
|
---|
| 344 |
|
---|
[d9bbe45] | 345 | uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
|
---|
| 346 | uint32_t block_addr;
|
---|
[0dc91833] | 347 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode, block_idx, &block_addr);
|
---|
| 348 | if (rc != EOK) {
|
---|
| 349 | return rc;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
[d9bbe45] | 352 | block_t *block;
|
---|
[0dc91833] | 353 | rc = block_get(&block, fs->device, block_addr, BLOCK_FLAGS_NONE);
|
---|
| 354 | if (rc != EOK) {
|
---|
| 355 | return rc;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | p++;
|
---|
| 359 |
|
---|
| 360 | block_put(p->block);
|
---|
| 361 | p->block = block;
|
---|
| 362 | p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
|
---|
| 363 | p->position = p->entries;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | return 1;
|
---|
| 367 |
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | int ext4_directory_dx_find_entry(ext4_directory_iterator_t *it,
|
---|
| 371 | ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, size_t len, const char *name)
|
---|
| 372 | {
|
---|
| 373 | int rc;
|
---|
| 374 |
|
---|
| 375 | // get direct block 0 (index root)
|
---|
[d9bbe45] | 376 | uint32_t root_block_addr;
|
---|
[0dc91833] | 377 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, 0, &root_block_addr);
|
---|
| 378 | if (rc != EOK) {
|
---|
| 379 | return rc;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[d9bbe45] | 382 | block_t *root_block;
|
---|
[0dc91833] | 383 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 384 | if (rc != EOK) {
|
---|
| 385 | it->current_block = NULL;
|
---|
| 386 | return rc;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[d9bbe45] | 389 | ext4_hash_info_t hinfo;
|
---|
[0dc91833] | 390 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, len, name);
|
---|
| 391 | if (rc != EOK) {
|
---|
| 392 | block_put(root_block);
|
---|
| 393 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
[565b6ff] | 396 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
[d9bbe45] | 397 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
| 398 | ext4_directory_dx_block_t *dx_block;
|
---|
[0dc91833] | 399 | rc = ext4_directory_dx_get_leaf(&hinfo, fs, inode_ref->inode, root_block, &dx_block, dx_blocks);
|
---|
| 400 | if (rc != EOK) {
|
---|
| 401 | block_put(root_block);
|
---|
| 402 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 |
|
---|
[d9bbe45] | 406 | do {
|
---|
[0dc91833] | 407 |
|
---|
[d9bbe45] | 408 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
| 409 | uint32_t leaf_block_addr;
|
---|
[0dc91833] | 410 | rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, leaf_block_idx, &leaf_block_addr);
|
---|
| 411 | if (rc != EOK) {
|
---|
| 412 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[d9bbe45] | 415 | block_t *leaf_block;
|
---|
[0dc91833] | 416 | rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 417 | if (rc != EOK) {
|
---|
| 418 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
[d9bbe45] | 421 | aoff64_t block_offset;
|
---|
| 422 | ext4_directory_entry_ll_t *res_dentry;
|
---|
| 423 | rc = ext4_directory_dx_find_dir_entry(leaf_block, fs->superblock, len, name,
|
---|
[0dc91833] | 424 | &res_dentry, &block_offset);
|
---|
| 425 |
|
---|
| 426 | // Found => return it
|
---|
| 427 | if (rc == 1) {
|
---|
| 428 | it->fs = fs;
|
---|
| 429 | it->inode_ref = inode_ref;
|
---|
| 430 | it->current_block = leaf_block;
|
---|
| 431 | it->current_offset = block_offset;
|
---|
| 432 | it->current = res_dentry;
|
---|
| 433 | return EOK;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | block_put(leaf_block);
|
---|
| 437 |
|
---|
| 438 | // ERROR - corrupted index
|
---|
| 439 | if (rc == -1) {
|
---|
| 440 | // TODO cleanup
|
---|
| 441 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | rc = ext4_directory_dx_next_block(fs, inode_ref->inode, hinfo.hash, dx_block, &dx_blocks[0]);
|
---|
| 445 | if (rc < 0) {
|
---|
| 446 | // TODO cleanup
|
---|
| 447 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | } while (rc == 1);
|
---|
| 451 |
|
---|
| 452 | return ENOENT;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[f577058] | 455 | typedef struct ext4_dx_sort_entry {
|
---|
| 456 | uint32_t hash;
|
---|
[ccac91e] | 457 | uint32_t rec_len;
|
---|
[c4f318d6] | 458 | void *dentry;
|
---|
[f577058] | 459 | } ext4_dx_sort_entry_t;
|
---|
| 460 |
|
---|
| 461 | static int dx_entry_comparator(void *arg1, void *arg2, void *dummy)
|
---|
| 462 | {
|
---|
| 463 | ext4_dx_sort_entry_t *entry1 = arg1;
|
---|
| 464 | ext4_dx_sort_entry_t *entry2 = arg2;
|
---|
| 465 |
|
---|
| 466 | if (entry1->hash == entry2->hash) {
|
---|
| 467 | return 0;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | if (entry1->hash < entry2->hash) {
|
---|
| 471 | return -1;
|
---|
| 472 | } else {
|
---|
| 473 | return 1;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[c0964cd7] | 478 | static void ext4_directory_dx_insert_entry(
|
---|
| 479 | ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
|
---|
| 480 | {
|
---|
| 481 | ext4_directory_dx_entry_t *old_index_entry = index_block->position;
|
---|
| 482 | ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
|
---|
| 483 |
|
---|
| 484 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
| 485 | (ext4_directory_dx_countlimit_t *)index_block->entries;
|
---|
| 486 | uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
| 487 |
|
---|
| 488 | ext4_directory_dx_entry_t *start_index = index_block->entries;
|
---|
| 489 | size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
|
---|
| 490 |
|
---|
| 491 | memmove(new_index_entry + 1, new_index_entry, bytes);
|
---|
| 492 |
|
---|
| 493 | ext4_directory_dx_entry_set_block(new_index_entry, iblock);
|
---|
| 494 | ext4_directory_dx_entry_set_hash(new_index_entry, hash);
|
---|
| 495 |
|
---|
| 496 | ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
|
---|
| 497 |
|
---|
| 498 | index_block->block->dirty = true;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
[f577058] | 501 | static int ext4_directory_dx_split_data(ext4_filesystem_t *fs,
|
---|
| 502 | ext4_inode_ref_t *inode_ref, ext4_hash_info_t *hinfo,
|
---|
[ccac91e] | 503 | block_t *old_data_block, ext4_directory_dx_block_t *index_block, block_t **new_data_block)
|
---|
[f577058] | 504 | {
|
---|
| 505 | int rc;
|
---|
| 506 |
|
---|
| 507 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 508 | void *entry_buffer = malloc(block_size);
|
---|
| 509 | if (entry_buffer == NULL) {
|
---|
| 510 | return ENOMEM;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | // Copy data to buffer
|
---|
[ccac91e] | 514 | memcpy(entry_buffer, old_data_block->data, block_size);
|
---|
[f577058] | 515 |
|
---|
[1d68621] | 516 | // dot entry has the smallest size available
|
---|
| 517 | uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
|
---|
[c4f318d6] | 518 |
|
---|
[1d68621] | 519 | ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
|
---|
[f577058] | 520 | if (sort_array == NULL) {
|
---|
| 521 | free(entry_buffer);
|
---|
| 522 | return ENOMEM;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[ccac91e] | 525 | ext4_directory_entry_ll_t *dentry = old_data_block->data;
|
---|
[f577058] | 526 |
|
---|
[1d68621] | 527 | uint32_t idx = 0;
|
---|
[ccac91e] | 528 | uint32_t real_size = 0;
|
---|
| 529 | void *entry_buffer_ptr = entry_buffer;
|
---|
[1d68621] | 530 |
|
---|
| 531 | ext4_hash_info_t tmp_hinfo;
|
---|
| 532 | memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
|
---|
| 533 |
|
---|
[ccac91e] | 534 | while ((void *)dentry < old_data_block->data + block_size) {
|
---|
[c4f318d6] | 535 |
|
---|
[412f813] | 536 | // Read only valid entries
|
---|
| 537 | if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
|
---|
| 538 | char *name = (char *)dentry->name;
|
---|
[f577058] | 539 |
|
---|
[412f813] | 540 | uint8_t len = ext4_directory_entry_ll_get_name_length(fs->superblock, dentry);
|
---|
| 541 | ext4_hash_string(&tmp_hinfo, len, name);
|
---|
[ccac91e] | 542 |
|
---|
[412f813] | 543 | uint32_t rec_len = 8 + len;
|
---|
| 544 |
|
---|
| 545 | if ((rec_len % 4) != 0) {
|
---|
| 546 | rec_len += 4 - (rec_len % 4);
|
---|
| 547 | }
|
---|
[ccac91e] | 548 |
|
---|
[412f813] | 549 | memcpy(entry_buffer_ptr, dentry, rec_len);
|
---|
[ccac91e] | 550 |
|
---|
[412f813] | 551 | sort_array[idx].dentry = entry_buffer_ptr;
|
---|
| 552 | sort_array[idx].rec_len = rec_len;
|
---|
| 553 | sort_array[idx].hash = tmp_hinfo.hash;
|
---|
[f577058] | 554 |
|
---|
[412f813] | 555 | entry_buffer_ptr += rec_len;
|
---|
| 556 | real_size += rec_len;
|
---|
| 557 | idx++;
|
---|
| 558 | }
|
---|
[ccac91e] | 559 |
|
---|
[f577058] | 560 | dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[1d68621] | 563 | qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t), dx_entry_comparator, NULL);
|
---|
[f577058] | 564 |
|
---|
| 565 | uint32_t new_fblock;
|
---|
[c4f318d6] | 566 | uint32_t new_iblock;
|
---|
| 567 | rc = ext4_directory_append_block(fs, inode_ref, &new_fblock, &new_iblock);
|
---|
[f577058] | 568 | if (rc != EOK) {
|
---|
| 569 | free(sort_array);
|
---|
| 570 | free(entry_buffer);
|
---|
| 571 | return rc;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
[1d68621] | 574 | // EXT4FS_DBG("new block appended (iblock = \%u, fblock = \%u)", new_iblock, new_fblock);
|
---|
[c4f318d6] | 575 |
|
---|
[ccac91e] | 576 | // Load new block
|
---|
| 577 | block_t *new_data_block_tmp;
|
---|
| 578 | rc = block_get(&new_data_block_tmp, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 579 | if (rc != EOK) {
|
---|
| 580 | free(sort_array);
|
---|
| 581 | free(entry_buffer);
|
---|
| 582 | return rc;
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | // Distribute entries to splitted blocks (by size)
|
---|
| 586 |
|
---|
| 587 | uint32_t new_hash = 0;
|
---|
| 588 | uint32_t current_size = 0;
|
---|
[1d68621] | 589 | uint32_t mid = 0;
|
---|
| 590 | for (uint32_t i = 0; i < idx; ++i) {
|
---|
| 591 | if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
|
---|
[ccac91e] | 592 | new_hash = sort_array[i].hash;
|
---|
| 593 | mid = i;
|
---|
| 594 | break;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | current_size += sort_array[i].rec_len;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[412f813] | 600 | uint32_t continued = 0;
|
---|
| 601 | if (new_hash == sort_array[mid-1].hash) {
|
---|
| 602 | continued = 1;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
[ccac91e] | 605 | uint32_t offset = 0;
|
---|
| 606 | void *ptr;
|
---|
[f577058] | 607 |
|
---|
[ccac91e] | 608 | // First part - to the old block
|
---|
[1d68621] | 609 | for (uint32_t i = 0; i < mid; ++i) {
|
---|
[ccac91e] | 610 | ptr = old_data_block->data + offset;
|
---|
| 611 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
| 612 |
|
---|
| 613 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
| 614 | if (i < (mid - 1)) {
|
---|
| 615 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
| 616 | } else {
|
---|
| 617 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | offset += sort_array[i].rec_len;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | // Second part - to the new block
|
---|
| 624 | offset = 0;
|
---|
[1d68621] | 625 | for (uint32_t i = mid; i < idx; ++i) {
|
---|
[ccac91e] | 626 | ptr = new_data_block_tmp->data + offset;
|
---|
| 627 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
| 628 |
|
---|
| 629 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
| 630 | if (i < (idx - 1)) {
|
---|
| 631 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
| 632 | } else {
|
---|
| 633 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | offset += sort_array[i].rec_len;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | old_data_block->dirty = true;
|
---|
| 640 | new_data_block_tmp->dirty = true;
|
---|
| 641 |
|
---|
| 642 | free(sort_array);
|
---|
| 643 | free(entry_buffer);
|
---|
[f577058] | 644 |
|
---|
[c0964cd7] | 645 | ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
|
---|
[ccac91e] | 646 |
|
---|
| 647 | *new_data_block = new_data_block_tmp;
|
---|
[f577058] | 648 |
|
---|
| 649 | return EOK;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 |
|
---|
[565b6ff] | 653 | int ext4_directory_dx_add_entry(ext4_filesystem_t *fs,
|
---|
[5c83612b] | 654 | ext4_inode_ref_t *parent, ext4_inode_ref_t *child,
|
---|
| 655 | size_t name_size, const char *name)
|
---|
[565b6ff] | 656 | {
|
---|
| 657 | int rc;
|
---|
| 658 |
|
---|
| 659 | // get direct block 0 (index root)
|
---|
| 660 | uint32_t root_block_addr;
|
---|
| 661 | rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, 0, &root_block_addr);
|
---|
| 662 | if (rc != EOK) {
|
---|
| 663 | return rc;
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | block_t *root_block;
|
---|
| 667 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 668 | if (rc != EOK) {
|
---|
| 669 | return rc;
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 | ext4_hash_info_t hinfo;
|
---|
| 673 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_size, name);
|
---|
| 674 | if (rc != EOK) {
|
---|
| 675 | block_put(root_block);
|
---|
| 676 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
| 680 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
| 681 | ext4_directory_dx_block_t *dx_block;
|
---|
| 682 | rc = ext4_directory_dx_get_leaf(&hinfo, fs, parent->inode, root_block, &dx_block, dx_blocks);
|
---|
| 683 | if (rc != EOK) {
|
---|
| 684 | block_put(root_block);
|
---|
| 685 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 |
|
---|
[5c83612b] | 689 | // Try to insert to existing data block
|
---|
| 690 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
| 691 | uint32_t leaf_block_addr;
|
---|
| 692 | rc = ext4_filesystem_get_inode_data_block_index(fs, parent->inode, leaf_block_idx, &leaf_block_addr);
|
---|
| 693 | if (rc != EOK) {
|
---|
| 694 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 |
|
---|
[ccac91e] | 698 | block_t *target_block;
|
---|
| 699 | rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
[5c83612b] | 700 | if (rc != EOK) {
|
---|
| 701 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 705 | uint16_t required_len = 8 + name_size + (4 - name_size % 4);
|
---|
| 706 |
|
---|
[ccac91e] | 707 | ext4_directory_entry_ll_t *de = target_block->data;
|
---|
| 708 | ext4_directory_entry_ll_t *stop = target_block->data + block_size;
|
---|
[5c83612b] | 709 |
|
---|
| 710 | while (de < stop) {
|
---|
| 711 |
|
---|
| 712 | uint32_t de_inode = ext4_directory_entry_ll_get_inode(de);
|
---|
| 713 | uint16_t de_rec_len = ext4_directory_entry_ll_get_entry_length(de);
|
---|
| 714 |
|
---|
| 715 | if ((de_inode == 0) && (de_rec_len >= required_len)) {
|
---|
| 716 | ext4_directory_write_entry(fs->superblock, de, de_rec_len,
|
---|
| 717 | child, name, name_size);
|
---|
[c4f318d6] | 718 |
|
---|
| 719 | // TODO cleanup
|
---|
[ccac91e] | 720 | target_block->dirty = true;
|
---|
| 721 | rc = block_put(target_block);
|
---|
[5c83612b] | 722 | if (rc != EOK) {
|
---|
| 723 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 724 | }
|
---|
| 725 | return EOK;
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | if (de_inode != 0) {
|
---|
| 729 | uint16_t used_name_len = ext4_directory_entry_ll_get_name_length(
|
---|
| 730 | fs->superblock, de);
|
---|
| 731 |
|
---|
| 732 | uint16_t used_space = 8 + used_name_len;
|
---|
| 733 | if ((used_name_len % 4) != 0) {
|
---|
| 734 | used_space += 4 - (used_name_len % 4);
|
---|
| 735 | }
|
---|
| 736 | uint16_t free_space = de_rec_len - used_space;
|
---|
| 737 |
|
---|
| 738 | if (free_space >= required_len) {
|
---|
| 739 |
|
---|
| 740 | // Cut tail of current entry
|
---|
| 741 | ext4_directory_entry_ll_set_entry_length(de, used_space);
|
---|
| 742 | ext4_directory_entry_ll_t *new_entry = (void *)de + used_space;
|
---|
| 743 | ext4_directory_write_entry(fs->superblock, new_entry,
|
---|
| 744 | free_space, child, name, name_size);
|
---|
[c4f318d6] | 745 |
|
---|
| 746 | // TODO cleanup
|
---|
[ccac91e] | 747 | target_block->dirty = true;
|
---|
| 748 | rc = block_put(target_block);
|
---|
[5c83612b] | 749 | if (rc != EOK) {
|
---|
| 750 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 751 | }
|
---|
| 752 | return EOK;
|
---|
| 753 |
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | de = (void *)de + de_rec_len;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
[1ebb66f] | 761 | EXT4FS_DBG("no free space found");
|
---|
[5c83612b] | 762 |
|
---|
| 763 | ext4_directory_dx_entry_t *entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
|
---|
[1ebb66f] | 764 | uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
|
---|
| 765 | uint16_t leaf_count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
|
---|
[5c83612b] | 766 |
|
---|
[c0964cd7] | 767 | ext4_directory_dx_entry_t *root_entries = ((ext4_directory_dx_node_t *) dx_blocks[0].block->data)->entries;
|
---|
[5c83612b] | 768 |
|
---|
[1ebb66f] | 769 | if (leaf_limit == leaf_count) {
|
---|
[5c83612b] | 770 | EXT4FS_DBG("need to split index block !!!");
|
---|
| 771 |
|
---|
[c0964cd7] | 772 | unsigned int levels = dx_block - dx_blocks;
|
---|
| 773 |
|
---|
| 774 | uint16_t root_limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)root_entries);
|
---|
| 775 | uint16_t root_count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)root_entries);
|
---|
| 776 |
|
---|
| 777 | if ((levels > 0) && (root_limit == root_count)) {
|
---|
| 778 | EXT4FS_DBG("Directory index is full");
|
---|
| 779 |
|
---|
| 780 | // ENOSPC - cleanup !!!
|
---|
| 781 | return ENOSPC;
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | uint32_t new_fblock;
|
---|
| 785 | uint32_t new_iblock;
|
---|
| 786 | rc = ext4_directory_append_block(fs, parent, &new_fblock, &new_iblock);
|
---|
| 787 | if (rc != EOK) {
|
---|
| 788 | // TODO error
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | // New block allocated
|
---|
| 792 | block_t * new_block;
|
---|
| 793 | rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 794 | if (rc != EOK) {
|
---|
| 795 | // TODO error
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | // Initialize block
|
---|
| 799 | memset(new_block->data, 0, block_size);
|
---|
| 800 |
|
---|
[6f731f0a] | 801 | ext4_directory_dx_node_t *new_node = new_block->data;
|
---|
| 802 | ext4_directory_dx_entry_t *new_entries = new_node->entries;
|
---|
| 803 |
|
---|
[c0964cd7] | 804 | if (levels > 0) {
|
---|
| 805 | EXT4FS_DBG("split index");
|
---|
| 806 | uint32_t count_left = leaf_count / 2;
|
---|
| 807 | uint32_t count_right = leaf_count - count_left;
|
---|
| 808 | uint32_t hash_right = ext4_directory_dx_entry_get_hash(entries);
|
---|
| 809 |
|
---|
| 810 | memcpy((void *) new_entries, (void *) (entries + count_left),
|
---|
| 811 | count_right * sizeof(ext4_directory_dx_entry_t));
|
---|
| 812 |
|
---|
| 813 | ext4_directory_dx_countlimit_t *left_countlimit = (ext4_directory_dx_countlimit_t *)entries;
|
---|
| 814 | ext4_directory_dx_countlimit_t *right_countlimit = (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
| 815 |
|
---|
| 816 | ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
|
---|
| 817 | ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
|
---|
| 818 |
|
---|
| 819 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 820 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 821 | ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
|
---|
| 822 |
|
---|
| 823 | // Which index block is target for new entry
|
---|
| 824 | uint32_t position_index = (dx_block->position - dx_block->entries);
|
---|
| 825 | if (position_index >= count_left) {
|
---|
| 826 | dx_block->position = new_entries + position_index - count_left;
|
---|
| 827 | dx_block->entries = new_entries;
|
---|
| 828 | entries = dx_block->entries;
|
---|
| 829 |
|
---|
| 830 | dx_block->block->dirty = true;
|
---|
| 831 | block_put(dx_block->block);
|
---|
| 832 | dx_block->block = new_block;
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
|
---|
| 836 |
|
---|
| 837 |
|
---|
| 838 | } else {
|
---|
| 839 | EXT4FS_DBG("create second level");
|
---|
[6f731f0a] | 840 |
|
---|
| 841 | memcpy((void *) new_entries, (void *) entries,
|
---|
| 842 | leaf_count * sizeof(ext4_directory_dx_entry_t));
|
---|
| 843 |
|
---|
| 844 | ext4_directory_dx_countlimit_t *new_countlimit =
|
---|
| 845 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
| 846 |
|
---|
| 847 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 848 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 849 | ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
|
---|
| 850 |
|
---|
| 851 | // Set values in root node
|
---|
| 852 | ext4_directory_dx_countlimit_t *new_root_countlimit =
|
---|
| 853 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
| 854 |
|
---|
| 855 | ext4_directory_dx_countlimit_set_limit(new_root_countlimit, 1);
|
---|
| 856 | ext4_directory_dx_entry_set_block(entries, new_iblock);
|
---|
| 857 |
|
---|
| 858 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
|
---|
| 859 |
|
---|
| 860 | /* Add new access path frame */
|
---|
| 861 | dx_block = dx_blocks + 1;
|
---|
| 862 | dx_block->position = dx_block->position - entries + new_entries;
|
---|
| 863 | dx_block->entries = new_entries;
|
---|
| 864 | entries = dx_block->entries;
|
---|
| 865 | dx_block->block = new_block;
|
---|
[c0964cd7] | 866 | }
|
---|
[c4f318d6] | 867 | }
|
---|
[5c83612b] | 868 |
|
---|
[c4f318d6] | 869 | block_t *new_block = NULL;
|
---|
| 870 | rc = ext4_directory_dx_split_data(fs, parent, &hinfo, target_block, dx_block, &new_block);
|
---|
| 871 | if (rc != EOK) {
|
---|
| 872 | // TODO error
|
---|
| 873 | }
|
---|
[5c83612b] | 874 |
|
---|
[c4f318d6] | 875 | // TODO Where to save new entry
|
---|
[1d68621] | 876 | uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
|
---|
[c4f318d6] | 877 | if (hinfo.hash >= new_block_hash) {
|
---|
| 878 | de = new_block->data;
|
---|
| 879 | stop = new_block->data + block_size;
|
---|
| 880 | } else {
|
---|
| 881 | de = target_block->data;
|
---|
| 882 | stop = target_block->data + block_size;
|
---|
| 883 | }
|
---|
[5c83612b] | 884 |
|
---|
[c4f318d6] | 885 | while (de < stop) {
|
---|
[5c83612b] | 886 |
|
---|
[c4f318d6] | 887 | uint32_t de_inode = ext4_directory_entry_ll_get_inode(de);
|
---|
| 888 | uint16_t de_rec_len = ext4_directory_entry_ll_get_entry_length(de);
|
---|
[1ebb66f] | 889 |
|
---|
[c4f318d6] | 890 | if ((de_inode == 0) && (de_rec_len >= required_len)) {
|
---|
| 891 | ext4_directory_write_entry(fs->superblock, de, de_rec_len,
|
---|
| 892 | child, name, name_size);
|
---|
| 893 | goto success;
|
---|
| 894 | }
|
---|
[5c83612b] | 895 |
|
---|
[c4f318d6] | 896 | if (de_inode != 0) {
|
---|
| 897 | uint16_t used_name_len = ext4_directory_entry_ll_get_name_length(
|
---|
| 898 | fs->superblock, de);
|
---|
[1ebb66f] | 899 |
|
---|
[c4f318d6] | 900 | uint16_t used_space = 8 + used_name_len;
|
---|
| 901 | if ((used_name_len % 4) != 0) {
|
---|
| 902 | used_space += 4 - (used_name_len % 4);
|
---|
| 903 | }
|
---|
| 904 | uint16_t free_space = de_rec_len - used_space;
|
---|
[5c83612b] | 905 |
|
---|
[c4f318d6] | 906 | if (free_space >= required_len) {
|
---|
[f577058] | 907 |
|
---|
[c4f318d6] | 908 | // Cut tail of current entry
|
---|
| 909 | ext4_directory_entry_ll_set_entry_length(de, used_space);
|
---|
| 910 | ext4_directory_entry_ll_t *new_entry = (void *)de + used_space;
|
---|
| 911 | ext4_directory_write_entry(fs->superblock, new_entry,
|
---|
| 912 | free_space, child, name, name_size);
|
---|
[f577058] | 913 |
|
---|
[c4f318d6] | 914 | goto success;
|
---|
| 915 | }
|
---|
| 916 |
|
---|
| 917 | }
|
---|
[565b6ff] | 918 |
|
---|
[c4f318d6] | 919 | de = (void *)de + de_rec_len;
|
---|
| 920 | }
|
---|
| 921 |
|
---|
| 922 | success:
|
---|
| 923 |
|
---|
[412f813] | 924 | rc = block_put(target_block);
|
---|
| 925 | if (rc != EOK) {
|
---|
| 926 | EXT4FS_DBG("error writing target block");
|
---|
| 927 | }
|
---|
| 928 | rc = block_put(new_block);
|
---|
| 929 | if (rc != EOK) {
|
---|
| 930 | EXT4FS_DBG("error writing new block");
|
---|
| 931 | }
|
---|
[c4f318d6] | 932 |
|
---|
| 933 | ext4_directory_dx_block_t *dx_it = dx_blocks;
|
---|
| 934 |
|
---|
| 935 | while (dx_it <= dx_block) {
|
---|
[412f813] | 936 | rc = block_put(dx_it->block);
|
---|
| 937 | if (rc != EOK) {
|
---|
| 938 | EXT4FS_DBG("error writing index block \%u", (uint32_t)dx_it->block->pba);
|
---|
| 939 | }
|
---|
[c4f318d6] | 940 | dx_it++;
|
---|
| 941 | }
|
---|
| 942 |
|
---|
| 943 | return EOK;
|
---|
[565b6ff] | 944 | }
|
---|
[0dc91833] | 945 |
|
---|
| 946 |
|
---|
| 947 | /**
|
---|
| 948 | * @}
|
---|
| 949 | */
|
---|