[0dc91833] | 1 | /*
|
---|
[f22d5ef0] | 2 | * Copyright (c) 2012 Frantisek Princ
|
---|
[0dc91833] | 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 |
|
---|
[db8fb43] | 45 | /** Type entry to pass to sorting algorithm.
|
---|
| 46 | *
|
---|
| 47 | */
|
---|
[b191acae] | 48 | typedef struct ext4_dx_sort_entry {
|
---|
| 49 | uint32_t hash;
|
---|
| 50 | uint32_t rec_len;
|
---|
| 51 | void *dentry;
|
---|
| 52 | } ext4_dx_sort_entry_t;
|
---|
| 53 |
|
---|
[343ccfd] | 54 |
|
---|
[db8fb43] | 55 | /** Get hash version used in directory index.
|
---|
| 56 | *
|
---|
| 57 | * @param root_info pointer to root info structure of index
|
---|
| 58 | * @return hash algorithm version
|
---|
| 59 | */
|
---|
[343ccfd] | 60 | uint8_t ext4_directory_dx_root_info_get_hash_version(
|
---|
| 61 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 62 | {
|
---|
| 63 | return root_info->hash_version;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[db8fb43] | 66 | /** Set hash version, that will be used in directory index.
|
---|
| 67 | *
|
---|
| 68 | * @param root_info pointer to root info structure of index
|
---|
| 69 | * @param version hash algorithm version
|
---|
| 70 | */
|
---|
[343ccfd] | 71 | void ext4_directory_dx_root_info_set_hash_version(
|
---|
| 72 | ext4_directory_dx_root_info_t *root_info, uint8_t version)
|
---|
| 73 | {
|
---|
| 74 | root_info->hash_version = version;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[db8fb43] | 77 | /** Get length of root_info structure in bytes.
|
---|
| 78 | *
|
---|
| 79 | * @param root_info pointer to root info structure of index
|
---|
| 80 | * @return length of the structure
|
---|
| 81 | */
|
---|
[343ccfd] | 82 | uint8_t ext4_directory_dx_root_info_get_info_length(
|
---|
| 83 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 84 | {
|
---|
| 85 | return root_info->info_length;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[db8fb43] | 88 | /** Set length of root_info structure in bytes.
|
---|
| 89 | *
|
---|
| 90 | * @param root_info pointer to root info structure of index
|
---|
| 91 | * @param info_length length of the structure
|
---|
| 92 | */
|
---|
[343ccfd] | 93 | void ext4_directory_dx_root_info_set_info_length(
|
---|
| 94 | ext4_directory_dx_root_info_t *root_info, uint8_t info_length)
|
---|
| 95 | {
|
---|
| 96 | root_info->info_length = info_length;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[db8fb43] | 99 | /** Get number of indirect levels of HTree.
|
---|
| 100 | *
|
---|
| 101 | * @param root_info pointer to root info structure of index
|
---|
| 102 | * @return height of HTree (actually only 0 or 1)
|
---|
| 103 | */
|
---|
[343ccfd] | 104 | uint8_t ext4_directory_dx_root_info_get_indirect_levels(
|
---|
| 105 | ext4_directory_dx_root_info_t *root_info)
|
---|
| 106 | {
|
---|
| 107 | return root_info->indirect_levels;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[db8fb43] | 110 | /** Set number of indirect levels of HTree.
|
---|
| 111 | *
|
---|
| 112 | * @param root_info pointer to root info structure of index
|
---|
| 113 | * @param levels height of HTree (actually only 0 or 1)
|
---|
| 114 | */
|
---|
[343ccfd] | 115 | void ext4_directory_dx_root_info_set_indirect_levels(
|
---|
| 116 | ext4_directory_dx_root_info_t *root_info, uint8_t levels)
|
---|
| 117 | {
|
---|
| 118 | root_info->indirect_levels = levels;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[db8fb43] | 121 | /** Get maximum number of index node entries.
|
---|
| 122 | *
|
---|
| 123 | * @param countlimit pointer to counlimit structure
|
---|
| 124 | * @return maximum of entries in node
|
---|
| 125 | */
|
---|
[343ccfd] | 126 | uint16_t ext4_directory_dx_countlimit_get_limit(
|
---|
| 127 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
| 128 | {
|
---|
| 129 | return uint16_t_le2host(countlimit->limit);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[db8fb43] | 132 | /** Set maximum number of index node entries.
|
---|
| 133 | *
|
---|
| 134 | * @param countlimit pointer to counlimit structure
|
---|
| 135 | * @param limit maximum of entries in node
|
---|
| 136 | */
|
---|
[343ccfd] | 137 | void ext4_directory_dx_countlimit_set_limit(
|
---|
| 138 | ext4_directory_dx_countlimit_t *countlimit, uint16_t limit)
|
---|
| 139 | {
|
---|
| 140 | countlimit->limit = host2uint16_t_le(limit);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[db8fb43] | 143 | /** Get current number of index node entries.
|
---|
| 144 | *
|
---|
| 145 | * @param countlimit pointer to counlimit structure
|
---|
| 146 | * @return number of entries in node
|
---|
| 147 | */
|
---|
[343ccfd] | 148 | uint16_t ext4_directory_dx_countlimit_get_count(
|
---|
| 149 | ext4_directory_dx_countlimit_t *countlimit)
|
---|
| 150 | {
|
---|
| 151 | return uint16_t_le2host(countlimit->count);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[db8fb43] | 154 | /** Set current number of index node entries.
|
---|
| 155 | *
|
---|
| 156 | * @param countlimit pointer to counlimit structure
|
---|
| 157 | * @param count number of entries in node
|
---|
| 158 | */
|
---|
[343ccfd] | 159 | void ext4_directory_dx_countlimit_set_count(
|
---|
| 160 | ext4_directory_dx_countlimit_t *countlimit, uint16_t count)
|
---|
| 161 | {
|
---|
| 162 | countlimit->count = host2uint16_t_le(count);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[db8fb43] | 165 | /** Get hash value of index entry.
|
---|
| 166 | *
|
---|
| 167 | * @param entry pointer to index entry
|
---|
| 168 | * @return hash value
|
---|
| 169 | */
|
---|
[343ccfd] | 170 | uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
|
---|
| 171 | {
|
---|
| 172 | return uint32_t_le2host(entry->hash);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[db8fb43] | 175 |
|
---|
| 176 | /** Set hash value of index entry.
|
---|
| 177 | *
|
---|
| 178 | * @param entry pointer to index entry
|
---|
| 179 | * @param hash hash value
|
---|
| 180 | */
|
---|
[343ccfd] | 181 | void ext4_directory_dx_entry_set_hash(ext4_directory_dx_entry_t *entry,
|
---|
| 182 | uint32_t hash)
|
---|
| 183 | {
|
---|
| 184 | entry->hash = host2uint32_t_le(hash);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[db8fb43] | 187 | /** Get block address where child node is located.
|
---|
| 188 | *
|
---|
| 189 | * @param entry pointer to index entry
|
---|
| 190 | * @return block address of child node
|
---|
| 191 | */
|
---|
[343ccfd] | 192 | uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
|
---|
| 193 | {
|
---|
| 194 | return uint32_t_le2host(entry->block);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[db8fb43] | 197 | /** Set block address where child node is located.
|
---|
| 198 | *
|
---|
| 199 | * @param entry pointer to index entry
|
---|
| 200 | * @param block block address of child node
|
---|
| 201 | */
|
---|
[343ccfd] | 202 | void ext4_directory_dx_entry_set_block(ext4_directory_dx_entry_t *entry,
|
---|
| 203 | uint32_t block)
|
---|
| 204 | {
|
---|
| 205 | entry->block = host2uint32_t_le(block);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | /**************************************************************************/
|
---|
| 210 |
|
---|
[e6f6260] | 211 | /** Initialize index structure of new directory.
|
---|
[db8fb43] | 212 | *
|
---|
[e6f6260] | 213 | * @param dir pointer to directory i-node
|
---|
| 214 | * @return error code
|
---|
[db8fb43] | 215 | */
|
---|
[7eb033ce] | 216 | int ext4_directory_dx_init(ext4_inode_ref_t *dir)
|
---|
| 217 | {
|
---|
| 218 | int rc;
|
---|
| 219 |
|
---|
[e6f6260] | 220 | // Load block 0, where will be index root located
|
---|
[7eb033ce] | 221 | uint32_t fblock;
|
---|
| 222 | rc = ext4_filesystem_get_inode_data_block_index(dir, 0, &fblock);
|
---|
| 223 | if (rc != EOK) {
|
---|
| 224 | return rc;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | block_t *block;
|
---|
| 228 | rc = block_get(&block, dir->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 229 | if (rc != EOK) {
|
---|
| 230 | return rc;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[e6f6260] | 233 | // Initialize pointers to data structures
|
---|
[7eb033ce] | 234 | ext4_directory_dx_root_t *root = block->data;
|
---|
| 235 | ext4_directory_dx_root_info_t *info = &(root->info);
|
---|
| 236 |
|
---|
[e6f6260] | 237 | // Initialize root info structure
|
---|
[7eb033ce] | 238 | uint8_t hash_version =
|
---|
| 239 | ext4_superblock_get_default_hash_version(dir->fs->superblock);
|
---|
| 240 |
|
---|
| 241 | ext4_directory_dx_root_info_set_hash_version(info, hash_version);
|
---|
| 242 | ext4_directory_dx_root_info_set_indirect_levels(info, 0);
|
---|
| 243 | ext4_directory_dx_root_info_set_info_length(info, 8);
|
---|
| 244 |
|
---|
[e6f6260] | 245 | // Set limit and current number of entries
|
---|
[7eb033ce] | 246 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
| 247 | (ext4_directory_dx_countlimit_t *)&root->entries;
|
---|
| 248 | ext4_directory_dx_countlimit_set_count(countlimit, 1);
|
---|
| 249 |
|
---|
| 250 | uint32_t block_size = ext4_superblock_get_block_size(dir->fs->superblock);
|
---|
| 251 | uint32_t entry_space = block_size - 2 * sizeof(ext4_directory_dx_dot_entry_t)
|
---|
| 252 | - sizeof(ext4_directory_dx_root_info_t);
|
---|
| 253 | uint16_t root_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 254 | ext4_directory_dx_countlimit_set_limit(countlimit, root_limit);
|
---|
| 255 |
|
---|
[e6f6260] | 256 | // Append new block, where will be new entries inserted in the future
|
---|
[7eb033ce] | 257 | uint32_t iblock;
|
---|
| 258 | rc = ext4_filesystem_append_inode_block(dir, &fblock, &iblock);
|
---|
| 259 | if (rc != EOK) {
|
---|
| 260 | block_put(block);
|
---|
| 261 | return rc;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | block_t *new_block;
|
---|
| 265 | rc = block_get(&new_block, dir->fs->device, fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 266 | if (rc != EOK) {
|
---|
| 267 | block_put(block);
|
---|
| 268 | return rc;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[e6f6260] | 271 | // Fill the whole block with empty entry
|
---|
[7eb033ce] | 272 | ext4_directory_entry_ll_t *block_entry = new_block->data;
|
---|
| 273 | ext4_directory_entry_ll_set_entry_length(block_entry, block_size);
|
---|
| 274 | ext4_directory_entry_ll_set_inode(block_entry, 0);
|
---|
| 275 |
|
---|
| 276 | new_block->dirty = true;
|
---|
| 277 | rc = block_put(new_block);
|
---|
| 278 | if (rc != EOK) {
|
---|
| 279 | block_put(block);
|
---|
| 280 | return rc;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[e6f6260] | 283 | // Connect new block to the only entry in index
|
---|
[7eb033ce] | 284 | ext4_directory_dx_entry_t *entry = root->entries;
|
---|
| 285 | ext4_directory_dx_entry_set_block(entry, iblock);
|
---|
| 286 |
|
---|
| 287 | block->dirty = true;
|
---|
| 288 |
|
---|
| 289 | rc = block_put(block);
|
---|
| 290 | if (rc != EOK) {
|
---|
| 291 | return rc;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | return EOK;
|
---|
| 295 | }
|
---|
[343ccfd] | 296 |
|
---|
[e6f6260] | 297 | /** Initialize hash info structure necessary for index operations.
|
---|
| 298 | *
|
---|
| 299 | * @param hinfo pointer to hinfo to be initialized
|
---|
| 300 | * @param root_block root block (number 0) of index
|
---|
| 301 | * @param sb pointer to superblock
|
---|
| 302 | * @param name_len length of name to be computed hash value from
|
---|
| 303 | * @param name name to be computed hash value from
|
---|
| 304 | * @return error code
|
---|
| 305 | */
|
---|
[0dc91833] | 306 | static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
|
---|
| 307 | ext4_superblock_t *sb, size_t name_len, const char *name)
|
---|
| 308 | {
|
---|
| 309 |
|
---|
[d9bbe45] | 310 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
[0dc91833] | 311 |
|
---|
| 312 | if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
|
---|
| 313 | root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
|
---|
| 314 | root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
|
---|
| 315 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | // Check unused flags
|
---|
| 319 | if (root->info.unused_flags != 0) {
|
---|
| 320 | EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
|
---|
| 321 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | // Check indirect levels
|
---|
| 325 | if (root->info.indirect_levels > 1) {
|
---|
| 326 | EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
|
---|
| 327 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[e6f6260] | 330 | // Check if node limit is correct
|
---|
[d9bbe45] | 331 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 332 | uint32_t entry_space = block_size;
|
---|
[0dc91833] | 333 | entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
|
---|
| 334 | entry_space -= sizeof(ext4_directory_dx_root_info_t);
|
---|
| 335 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 336 |
|
---|
[d9bbe45] | 337 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
|
---|
[0dc91833] | 338 | if (limit != entry_space) {
|
---|
| 339 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[e6f6260] | 342 | // Check hash version and modify if necessary
|
---|
[0dc91833] | 343 | hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
|
---|
| 344 | if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
|
---|
| 345 | && (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
|
---|
| 346 | // 3 is magic from ext4 linux implementation
|
---|
| 347 | hinfo->hash_version += 3;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[e6f6260] | 350 | // Load hash seed from superblock
|
---|
[0dc91833] | 351 | hinfo->seed = ext4_superblock_get_hash_seed(sb);
|
---|
| 352 |
|
---|
[e6f6260] | 353 | // Compute hash value of name
|
---|
[0dc91833] | 354 | if (name) {
|
---|
| 355 | ext4_hash_string(hinfo, name_len, name);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | return EOK;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[e6f6260] | 361 | /** Walk through index tree and load leaf with corresponding hash value.
|
---|
| 362 | *
|
---|
| 363 | * @param hinfo initialized hash info structure
|
---|
| 364 | * @param inode_ref current i-node
|
---|
| 365 | * @param root_block root block (iblock 0), where is root node located
|
---|
| 366 | * @param dx_block pointer to leaf node in dx_blocks array
|
---|
| 367 | * @param dx_blocks array with the whole path from root to leaf
|
---|
| 368 | * @return error code
|
---|
| 369 | */
|
---|
[0dc91833] | 370 | static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
|
---|
[1ac1ab4] | 371 | ext4_inode_ref_t *inode_ref, block_t *root_block,
|
---|
[0dc91833] | 372 | ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
|
---|
| 373 | {
|
---|
| 374 | int rc;
|
---|
[d9bbe45] | 375 |
|
---|
[0dc91833] | 376 | ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
|
---|
| 377 |
|
---|
[d9bbe45] | 378 | ext4_directory_dx_root_t *root = (ext4_directory_dx_root_t *)root_block->data;
|
---|
| 379 | ext4_directory_dx_entry_t *entries = (ext4_directory_dx_entry_t *)&root->entries;
|
---|
[0dc91833] | 380 |
|
---|
[d9bbe45] | 381 | uint16_t limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
|
---|
| 382 | uint8_t indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
|
---|
[0dc91833] | 383 |
|
---|
[d9bbe45] | 384 | block_t *tmp_block = root_block;
|
---|
| 385 | ext4_directory_dx_entry_t *p, *q, *m, *at;
|
---|
[e6f6260] | 386 |
|
---|
| 387 | // Walk through the index tree
|
---|
[0dc91833] | 388 | while (true) {
|
---|
| 389 |
|
---|
[d9bbe45] | 390 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
|
---|
[0dc91833] | 391 | if ((count == 0) || (count > limit)) {
|
---|
| 392 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[e6f6260] | 395 |
|
---|
| 396 | // Do binary search in every node
|
---|
[0dc91833] | 397 | p = entries + 1;
|
---|
| 398 | q = entries + count - 1;
|
---|
| 399 |
|
---|
| 400 | while (p <= q) {
|
---|
| 401 | m = p + (q - p) / 2;
|
---|
| 402 | if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
|
---|
| 403 | q = m - 1;
|
---|
| 404 | } else {
|
---|
| 405 | p = m + 1;
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | at = p - 1;
|
---|
| 410 |
|
---|
[e6f6260] | 411 | // Write results
|
---|
[0dc91833] | 412 | tmp_dx_block->block = tmp_block;
|
---|
| 413 | tmp_dx_block->entries = entries;
|
---|
| 414 | tmp_dx_block->position = at;
|
---|
| 415 |
|
---|
[e6f6260] | 416 | // Is algorithm in the leaf?
|
---|
[0dc91833] | 417 | if (indirect_level == 0) {
|
---|
| 418 | *dx_block = tmp_dx_block;
|
---|
| 419 | return EOK;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[e6f6260] | 422 | // Goto child node
|
---|
[d9bbe45] | 423 | uint32_t next_block = ext4_directory_dx_entry_get_block(at);
|
---|
[0dc91833] | 424 |
|
---|
| 425 | indirect_level--;
|
---|
| 426 |
|
---|
[d9bbe45] | 427 | uint32_t fblock;
|
---|
[1ac1ab4] | 428 | rc = ext4_filesystem_get_inode_data_block_index(
|
---|
| 429 | inode_ref, next_block, &fblock);
|
---|
[0dc91833] | 430 | if (rc != EOK) {
|
---|
| 431 | return rc;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
[1ac1ab4] | 434 | rc = block_get(&tmp_block, inode_ref->fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
[0dc91833] | 435 | if (rc != EOK) {
|
---|
| 436 | return rc;
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
|
---|
[1ac1ab4] | 440 | limit = ext4_directory_dx_countlimit_get_limit(
|
---|
| 441 | (ext4_directory_dx_countlimit_t *)entries);
|
---|
[0dc91833] | 442 |
|
---|
[1ac1ab4] | 443 | uint16_t entry_space = ext4_superblock_get_block_size(inode_ref->fs->superblock)
|
---|
[d9bbe45] | 444 | - sizeof(ext4_directory_dx_dot_entry_t);
|
---|
[0dc91833] | 445 | entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 446 |
|
---|
| 447 |
|
---|
| 448 | if (limit != entry_space) {
|
---|
| 449 | block_put(tmp_block);
|
---|
| 450 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | ++tmp_dx_block;
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | // Unreachable
|
---|
| 457 | return EOK;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 |
|
---|
[e6f6260] | 461 | /** Check if the the next block would be checked during entry search.
|
---|
| 462 | *
|
---|
| 463 | * @param inode_ref directory i-node
|
---|
| 464 | * @param hash hash value to check
|
---|
| 465 | * @param dx_block current block
|
---|
| 466 | * @param dx_blocks aray with path from root to leaf node
|
---|
| 467 | * @return error code
|
---|
| 468 | */
|
---|
[1ac1ab4] | 469 | static int ext4_directory_dx_next_block(ext4_inode_ref_t *inode_ref, uint32_t hash,
|
---|
[e6f6260] | 470 | ext4_directory_dx_block_t *dx_block, ext4_directory_dx_block_t *dx_blocks)
|
---|
[0dc91833] | 471 | {
|
---|
[d9bbe45] | 472 | int rc;
|
---|
| 473 |
|
---|
| 474 | uint32_t num_handles = 0;
|
---|
[e6f6260] | 475 | ext4_directory_dx_block_t *p = dx_block;
|
---|
[0dc91833] | 476 |
|
---|
[e6f6260] | 477 | // TODO comment
|
---|
[0dc91833] | 478 | while (1) {
|
---|
| 479 |
|
---|
| 480 | p->position++;
|
---|
[d9bbe45] | 481 | uint16_t count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
|
---|
[0dc91833] | 482 |
|
---|
| 483 | if (p->position < p->entries + count) {
|
---|
| 484 | break;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
[e6f6260] | 487 | if (p == dx_blocks) {
|
---|
[0dc91833] | 488 | return 0;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | num_handles++;
|
---|
| 492 | p--;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
[e6f6260] | 495 | // TODO comment
|
---|
[d9bbe45] | 496 | uint32_t current_hash = ext4_directory_dx_entry_get_hash(p->position);
|
---|
[0dc91833] | 497 | if ((hash & 1) == 0) {
|
---|
| 498 | if ((current_hash & ~1) != hash) {
|
---|
| 499 | return 0;
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[e6f6260] | 503 | // TODO comment
|
---|
[0dc91833] | 504 | while (num_handles--) {
|
---|
| 505 |
|
---|
[d9bbe45] | 506 | uint32_t block_idx = ext4_directory_dx_entry_get_block(p->position);
|
---|
| 507 | uint32_t block_addr;
|
---|
[1ac1ab4] | 508 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, block_idx, &block_addr);
|
---|
[0dc91833] | 509 | if (rc != EOK) {
|
---|
| 510 | return rc;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[d9bbe45] | 513 | block_t *block;
|
---|
[1ac1ab4] | 514 | rc = block_get(&block, inode_ref->fs->device, block_addr, BLOCK_FLAGS_NONE);
|
---|
[0dc91833] | 515 | if (rc != EOK) {
|
---|
| 516 | return rc;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | p++;
|
---|
| 520 |
|
---|
| 521 | block_put(p->block);
|
---|
| 522 | p->block = block;
|
---|
| 523 | p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
|
---|
| 524 | p->position = p->entries;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | return 1;
|
---|
| 528 |
|
---|
| 529 | }
|
---|
| 530 |
|
---|
[e6f6260] | 531 | /** Try to find directory entry using directory index.
|
---|
| 532 | *
|
---|
| 533 | * @param result output value - if entry will be found,
|
---|
| 534 | * than will be passed through this parameter
|
---|
| 535 | * @param inode_ref directory i-node
|
---|
| 536 | * @param name_len length of name to be found
|
---|
| 537 | * @param name name to be found
|
---|
| 538 | * @return error code
|
---|
| 539 | */
|
---|
[7689590] | 540 | int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
|
---|
[1ac1ab4] | 541 | ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
|
---|
[0dc91833] | 542 | {
|
---|
| 543 | int rc;
|
---|
| 544 |
|
---|
[e6f6260] | 545 | // Load direct block 0 (index root)
|
---|
[d9bbe45] | 546 | uint32_t root_block_addr;
|
---|
[1ac1ab4] | 547 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, 0, &root_block_addr);
|
---|
[0dc91833] | 548 | if (rc != EOK) {
|
---|
| 549 | return rc;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
[1ac1ab4] | 552 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 553 |
|
---|
[d9bbe45] | 554 | block_t *root_block;
|
---|
[0dc91833] | 555 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 556 | if (rc != EOK) {
|
---|
| 557 | return rc;
|
---|
| 558 | }
|
---|
| 559 |
|
---|
[e6f6260] | 560 | // Initialize hash info (compute hash value)
|
---|
[d9bbe45] | 561 | ext4_hash_info_t hinfo;
|
---|
[7689590] | 562 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
[0dc91833] | 563 | if (rc != EOK) {
|
---|
| 564 | block_put(root_block);
|
---|
| 565 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 566 | }
|
---|
| 567 |
|
---|
[e6f6260] | 568 | // Hardcoded number 2 means maximum height of index tree, specified in linux driver
|
---|
[d9bbe45] | 569 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
[e63ce679] | 570 | ext4_directory_dx_block_t *dx_block, *tmp;
|
---|
[1ac1ab4] | 571 | rc = ext4_directory_dx_get_leaf(&hinfo, inode_ref, root_block, &dx_block, dx_blocks);
|
---|
[0dc91833] | 572 | if (rc != EOK) {
|
---|
| 573 | block_put(root_block);
|
---|
| 574 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
[d9bbe45] | 577 | do {
|
---|
[e6f6260] | 578 | // Load leaf block
|
---|
[d9bbe45] | 579 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
| 580 | uint32_t leaf_block_addr;
|
---|
[1ac1ab4] | 581 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref, leaf_block_idx, &leaf_block_addr);
|
---|
[0dc91833] | 582 | if (rc != EOK) {
|
---|
[e63ce679] | 583 | goto cleanup;
|
---|
[0dc91833] | 584 | }
|
---|
| 585 |
|
---|
[d9bbe45] | 586 | block_t *leaf_block;
|
---|
[0dc91833] | 587 | rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 588 | if (rc != EOK) {
|
---|
[e63ce679] | 589 | goto cleanup;
|
---|
[0dc91833] | 590 | }
|
---|
| 591 |
|
---|
[e6f6260] | 592 | // Linear search inside block
|
---|
[d9bbe45] | 593 | ext4_directory_entry_ll_t *res_dentry;
|
---|
[7689590] | 594 | rc = ext4_directory_find_in_block(leaf_block, fs->superblock, name_len, name, &res_dentry);
|
---|
| 595 |
|
---|
[0dc91833] | 596 | // Found => return it
|
---|
[7689590] | 597 | if (rc == EOK) {
|
---|
| 598 | result->block = leaf_block;
|
---|
| 599 | result->dentry = res_dentry;
|
---|
[e8d054a] | 600 | goto cleanup;
|
---|
[0dc91833] | 601 | }
|
---|
| 602 |
|
---|
[e63ce679] | 603 | // Not found, leave untouched
|
---|
[0dc91833] | 604 | block_put(leaf_block);
|
---|
| 605 |
|
---|
[e63ce679] | 606 | if (rc != ENOENT) {
|
---|
| 607 | goto cleanup;
|
---|
[0dc91833] | 608 | }
|
---|
| 609 |
|
---|
[e6f6260] | 610 | // check if the next block could be checked
|
---|
[1ac1ab4] | 611 | rc = ext4_directory_dx_next_block(inode_ref, hinfo.hash, dx_block, &dx_blocks[0]);
|
---|
[0dc91833] | 612 | if (rc < 0) {
|
---|
[e63ce679] | 613 | goto cleanup;
|
---|
[0dc91833] | 614 | }
|
---|
| 615 |
|
---|
| 616 | } while (rc == 1);
|
---|
| 617 |
|
---|
[e6f6260] | 618 | // Entry not found
|
---|
[e8d054a] | 619 | rc = ENOENT;
|
---|
[e63ce679] | 620 |
|
---|
| 621 | cleanup:
|
---|
| 622 |
|
---|
[e6f6260] | 623 | // The whole path must be released (preventing memory leak)
|
---|
[e63ce679] | 624 | tmp = dx_blocks;
|
---|
| 625 | while (tmp <= dx_block) {
|
---|
| 626 | block_put(tmp->block);
|
---|
| 627 | ++tmp;
|
---|
| 628 | }
|
---|
| 629 | return rc;
|
---|
[0dc91833] | 630 | }
|
---|
| 631 |
|
---|
[e6f6260] | 632 | /** Compare function used to pass in quicksort implementation.
|
---|
| 633 | *
|
---|
| 634 | * It can compare two entries by hash value.
|
---|
| 635 | *
|
---|
| 636 | * @param arg1 first entry
|
---|
| 637 | * @param arg2 second entry
|
---|
| 638 | * @param dummy unused parameter, can be NULL
|
---|
| 639 | * @return classic compare result (0: equal, -1: arg1 < arg2, 1: arg1 > arg2)
|
---|
| 640 | */
|
---|
[b191acae] | 641 | static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy)
|
---|
[f577058] | 642 | {
|
---|
| 643 | ext4_dx_sort_entry_t *entry1 = arg1;
|
---|
| 644 | ext4_dx_sort_entry_t *entry2 = arg2;
|
---|
| 645 |
|
---|
| 646 | if (entry1->hash == entry2->hash) {
|
---|
| 647 | return 0;
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | if (entry1->hash < entry2->hash) {
|
---|
| 651 | return -1;
|
---|
| 652 | } else {
|
---|
| 653 | return 1;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 | }
|
---|
| 657 |
|
---|
[e6f6260] | 658 | /** Insert new index entry to block.
|
---|
| 659 | *
|
---|
| 660 | * Note that space for new entry must be checked by caller.
|
---|
| 661 | *
|
---|
| 662 | * @param index_block block where to insert new entry
|
---|
| 663 | * @param hash hash value covered by child node
|
---|
| 664 | * @param iblock logical number of child block
|
---|
| 665 | *
|
---|
| 666 | */
|
---|
[c0964cd7] | 667 | static void ext4_directory_dx_insert_entry(
|
---|
| 668 | ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
|
---|
| 669 | {
|
---|
| 670 | ext4_directory_dx_entry_t *old_index_entry = index_block->position;
|
---|
| 671 | ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
|
---|
| 672 |
|
---|
| 673 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
| 674 | (ext4_directory_dx_countlimit_t *)index_block->entries;
|
---|
| 675 | uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
| 676 |
|
---|
| 677 | ext4_directory_dx_entry_t *start_index = index_block->entries;
|
---|
| 678 | size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
|
---|
| 679 |
|
---|
| 680 | memmove(new_index_entry + 1, new_index_entry, bytes);
|
---|
| 681 |
|
---|
| 682 | ext4_directory_dx_entry_set_block(new_index_entry, iblock);
|
---|
| 683 | ext4_directory_dx_entry_set_hash(new_index_entry, hash);
|
---|
| 684 |
|
---|
| 685 | ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
|
---|
| 686 |
|
---|
| 687 | index_block->block->dirty = true;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
[e6f6260] | 690 | /** Split directory entries to two parts preventing node overflow.
|
---|
| 691 | *
|
---|
| 692 | * @param inode_ref directory i-node
|
---|
| 693 | * @param hinfo hash info
|
---|
| 694 | * @param old_data_block block with data to be split
|
---|
| 695 | * @param index_block block where index entries are located
|
---|
| 696 | * @param new_data_block output value for newly allocated data block
|
---|
| 697 | */
|
---|
| 698 | static int ext4_directory_dx_split_data(ext4_inode_ref_t *inode_ref,
|
---|
| 699 | ext4_hash_info_t *hinfo, block_t *old_data_block,
|
---|
| 700 | ext4_directory_dx_block_t *index_block, block_t **new_data_block)
|
---|
[f577058] | 701 | {
|
---|
[d0d7afb] | 702 | int rc = EOK;
|
---|
[f577058] | 703 |
|
---|
[e8d054a] | 704 | // Allocate buffer for directory entries
|
---|
[e6f6260] | 705 | uint32_t block_size =
|
---|
| 706 | ext4_superblock_get_block_size(inode_ref->fs->superblock);
|
---|
[f577058] | 707 | void *entry_buffer = malloc(block_size);
|
---|
| 708 | if (entry_buffer == NULL) {
|
---|
| 709 | return ENOMEM;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
[1d68621] | 712 | // dot entry has the smallest size available
|
---|
| 713 | uint32_t max_entry_count = block_size / sizeof(ext4_directory_dx_dot_entry_t);
|
---|
[c4f318d6] | 714 |
|
---|
[e8d054a] | 715 | // Allocate sort entry
|
---|
[1d68621] | 716 | ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
|
---|
[f577058] | 717 | if (sort_array == NULL) {
|
---|
| 718 | free(entry_buffer);
|
---|
| 719 | return ENOMEM;
|
---|
| 720 | }
|
---|
| 721 |
|
---|
[1d68621] | 722 | uint32_t idx = 0;
|
---|
[ccac91e] | 723 | uint32_t real_size = 0;
|
---|
[1d68621] | 724 |
|
---|
[e8d054a] | 725 | // Initialize hinfo
|
---|
[1d68621] | 726 | ext4_hash_info_t tmp_hinfo;
|
---|
| 727 | memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
|
---|
| 728 |
|
---|
[e8d054a] | 729 | // Load all valid entries to the buffer
|
---|
| 730 | ext4_directory_entry_ll_t *dentry = old_data_block->data;
|
---|
| 731 | void *entry_buffer_ptr = entry_buffer;
|
---|
[ccac91e] | 732 | while ((void *)dentry < old_data_block->data + block_size) {
|
---|
[c4f318d6] | 733 |
|
---|
[412f813] | 734 | // Read only valid entries
|
---|
| 735 | if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
|
---|
[f577058] | 736 |
|
---|
[e6f6260] | 737 | uint8_t len = ext4_directory_entry_ll_get_name_length(
|
---|
| 738 | inode_ref->fs->superblock, dentry);
|
---|
[e8d054a] | 739 | ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
|
---|
[ccac91e] | 740 |
|
---|
[412f813] | 741 | uint32_t rec_len = 8 + len;
|
---|
| 742 |
|
---|
| 743 | if ((rec_len % 4) != 0) {
|
---|
| 744 | rec_len += 4 - (rec_len % 4);
|
---|
| 745 | }
|
---|
[ccac91e] | 746 |
|
---|
[412f813] | 747 | memcpy(entry_buffer_ptr, dentry, rec_len);
|
---|
[ccac91e] | 748 |
|
---|
[412f813] | 749 | sort_array[idx].dentry = entry_buffer_ptr;
|
---|
| 750 | sort_array[idx].rec_len = rec_len;
|
---|
| 751 | sort_array[idx].hash = tmp_hinfo.hash;
|
---|
[f577058] | 752 |
|
---|
[412f813] | 753 | entry_buffer_ptr += rec_len;
|
---|
| 754 | real_size += rec_len;
|
---|
| 755 | idx++;
|
---|
| 756 | }
|
---|
[ccac91e] | 757 |
|
---|
[f577058] | 758 | dentry = (void *)dentry + ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
| 759 | }
|
---|
| 760 |
|
---|
[e6f6260] | 761 | // Sort all entries
|
---|
[5b16912] | 762 | qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t),
|
---|
| 763 | ext4_directory_dx_entry_comparator, NULL);
|
---|
[f577058] | 764 |
|
---|
[e6f6260] | 765 | // Allocate new block for store the second part of entries
|
---|
[f577058] | 766 | uint32_t new_fblock;
|
---|
[c4f318d6] | 767 | uint32_t new_iblock;
|
---|
[5b16912] | 768 | rc = ext4_filesystem_append_inode_block(inode_ref, &new_fblock, &new_iblock);
|
---|
[f577058] | 769 | if (rc != EOK) {
|
---|
| 770 | free(sort_array);
|
---|
| 771 | free(entry_buffer);
|
---|
| 772 | return rc;
|
---|
| 773 | }
|
---|
| 774 |
|
---|
[ccac91e] | 775 | // Load new block
|
---|
| 776 | block_t *new_data_block_tmp;
|
---|
[e6f6260] | 777 | rc = block_get(&new_data_block_tmp, inode_ref->fs->device,
|
---|
| 778 | new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
[ccac91e] | 779 | if (rc != EOK) {
|
---|
| 780 | free(sort_array);
|
---|
| 781 | free(entry_buffer);
|
---|
| 782 | return rc;
|
---|
| 783 | }
|
---|
| 784 |
|
---|
[e6f6260] | 785 | // Distribute entries to two blocks (by size)
|
---|
| 786 | // Compute the half
|
---|
[ccac91e] | 787 | uint32_t new_hash = 0;
|
---|
| 788 | uint32_t current_size = 0;
|
---|
[1d68621] | 789 | uint32_t mid = 0;
|
---|
| 790 | for (uint32_t i = 0; i < idx; ++i) {
|
---|
| 791 | if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
|
---|
[ccac91e] | 792 | new_hash = sort_array[i].hash;
|
---|
| 793 | mid = i;
|
---|
| 794 | break;
|
---|
| 795 | }
|
---|
| 796 |
|
---|
| 797 | current_size += sort_array[i].rec_len;
|
---|
| 798 | }
|
---|
| 799 |
|
---|
[e6f6260] | 800 | // Check hash collision
|
---|
[412f813] | 801 | uint32_t continued = 0;
|
---|
| 802 | if (new_hash == sort_array[mid-1].hash) {
|
---|
| 803 | continued = 1;
|
---|
| 804 | }
|
---|
| 805 |
|
---|
[ccac91e] | 806 | uint32_t offset = 0;
|
---|
| 807 | void *ptr;
|
---|
[f577058] | 808 |
|
---|
[ccac91e] | 809 | // First part - to the old block
|
---|
[1d68621] | 810 | for (uint32_t i = 0; i < mid; ++i) {
|
---|
[ccac91e] | 811 | ptr = old_data_block->data + offset;
|
---|
| 812 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
| 813 |
|
---|
| 814 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
| 815 | if (i < (mid - 1)) {
|
---|
| 816 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
| 817 | } else {
|
---|
| 818 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
| 819 | }
|
---|
| 820 |
|
---|
| 821 | offset += sort_array[i].rec_len;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | // Second part - to the new block
|
---|
| 825 | offset = 0;
|
---|
[1d68621] | 826 | for (uint32_t i = mid; i < idx; ++i) {
|
---|
[ccac91e] | 827 | ptr = new_data_block_tmp->data + offset;
|
---|
| 828 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
| 829 |
|
---|
| 830 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
| 831 | if (i < (idx - 1)) {
|
---|
| 832 | ext4_directory_entry_ll_set_entry_length(tmp, sort_array[i].rec_len);
|
---|
| 833 | } else {
|
---|
| 834 | ext4_directory_entry_ll_set_entry_length(tmp, block_size - offset);
|
---|
| 835 | }
|
---|
| 836 |
|
---|
| 837 | offset += sort_array[i].rec_len;
|
---|
| 838 | }
|
---|
| 839 |
|
---|
[e6f6260] | 840 | // Do some steps to finish operation
|
---|
[ccac91e] | 841 | old_data_block->dirty = true;
|
---|
| 842 | new_data_block_tmp->dirty = true;
|
---|
| 843 |
|
---|
| 844 | free(sort_array);
|
---|
| 845 | free(entry_buffer);
|
---|
[f577058] | 846 |
|
---|
[c0964cd7] | 847 | ext4_directory_dx_insert_entry(index_block, new_hash + continued, new_iblock);
|
---|
[ccac91e] | 848 |
|
---|
| 849 | *new_data_block = new_data_block_tmp;
|
---|
[f577058] | 850 |
|
---|
| 851 | return EOK;
|
---|
| 852 | }
|
---|
| 853 |
|
---|
[e6f6260] | 854 | /** TODO start comments from here
|
---|
| 855 | *
|
---|
| 856 | */
|
---|
[2f2feadb] | 857 | static int ext4_directory_dx_split_index(ext4_filesystem_t *fs,
|
---|
| 858 | ext4_inode_ref_t *inode_ref, ext4_directory_dx_block_t *dx_blocks,
|
---|
| 859 | ext4_directory_dx_block_t *dx_block)
|
---|
[565b6ff] | 860 | {
|
---|
[2f2feadb] | 861 | int rc;
|
---|
[565b6ff] | 862 |
|
---|
[2f2feadb] | 863 | ext4_directory_dx_entry_t *entries;
|
---|
| 864 | if (dx_block == dx_blocks) {
|
---|
| 865 | entries = ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
|
---|
| 866 | } else {
|
---|
| 867 | entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
|
---|
[565b6ff] | 868 | }
|
---|
| 869 |
|
---|
[2f2feadb] | 870 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
| 871 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
| 872 | uint16_t leaf_limit = ext4_directory_dx_countlimit_get_limit(countlimit);
|
---|
| 873 | uint16_t leaf_count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
[565b6ff] | 874 |
|
---|
[2f2feadb] | 875 | // Check if is necessary to split index block
|
---|
[1ebb66f] | 876 | if (leaf_limit == leaf_count) {
|
---|
[5c83612b] | 877 | EXT4FS_DBG("need to split index block !!!");
|
---|
| 878 |
|
---|
[c0964cd7] | 879 | unsigned int levels = dx_block - dx_blocks;
|
---|
| 880 |
|
---|
[2f2feadb] | 881 | ext4_directory_dx_entry_t *root_entries =
|
---|
| 882 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->entries;
|
---|
| 883 |
|
---|
| 884 | ext4_directory_dx_countlimit_t *root_countlimit =
|
---|
| 885 | (ext4_directory_dx_countlimit_t *)root_entries;
|
---|
| 886 | uint16_t root_limit =
|
---|
| 887 | ext4_directory_dx_countlimit_get_limit(root_countlimit);
|
---|
| 888 | uint16_t root_count =
|
---|
| 889 | ext4_directory_dx_countlimit_get_count(root_countlimit);
|
---|
[c0964cd7] | 890 |
|
---|
| 891 | if ((levels > 0) && (root_limit == root_count)) {
|
---|
| 892 | EXT4FS_DBG("Directory index is full");
|
---|
[2f2feadb] | 893 | return ENOSPC;
|
---|
[c0964cd7] | 894 | }
|
---|
| 895 |
|
---|
| 896 | uint32_t new_fblock;
|
---|
| 897 | uint32_t new_iblock;
|
---|
[5b16912] | 898 | rc = ext4_filesystem_append_inode_block(
|
---|
| 899 | inode_ref, &new_fblock, &new_iblock);
|
---|
[c0964cd7] | 900 | if (rc != EOK) {
|
---|
[2f2feadb] | 901 | return rc;
|
---|
[c0964cd7] | 902 | }
|
---|
| 903 |
|
---|
| 904 | // New block allocated
|
---|
| 905 | block_t * new_block;
|
---|
| 906 | rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 907 | if (rc != EOK) {
|
---|
[2f2feadb] | 908 | return rc;
|
---|
[c0964cd7] | 909 | }
|
---|
| 910 |
|
---|
[6f731f0a] | 911 | ext4_directory_dx_node_t *new_node = new_block->data;
|
---|
| 912 | ext4_directory_dx_entry_t *new_entries = new_node->entries;
|
---|
| 913 |
|
---|
[2f2feadb] | 914 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 915 |
|
---|
[c0964cd7] | 916 | if (levels > 0) {
|
---|
[2f2feadb] | 917 | EXT4FS_DBG("split index leaf node");
|
---|
[c0964cd7] | 918 | uint32_t count_left = leaf_count / 2;
|
---|
| 919 | uint32_t count_right = leaf_count - count_left;
|
---|
[2f2feadb] | 920 | uint32_t hash_right =
|
---|
| 921 | ext4_directory_dx_entry_get_hash(entries + count_left);
|
---|
[c0964cd7] | 922 |
|
---|
| 923 | memcpy((void *) new_entries, (void *) (entries + count_left),
|
---|
| 924 | count_right * sizeof(ext4_directory_dx_entry_t));
|
---|
| 925 |
|
---|
[2f2feadb] | 926 | ext4_directory_dx_countlimit_t *left_countlimit =
|
---|
| 927 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
| 928 | ext4_directory_dx_countlimit_t *right_countlimit =
|
---|
| 929 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
[c0964cd7] | 930 |
|
---|
| 931 | ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
|
---|
| 932 | ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
|
---|
| 933 |
|
---|
[2f2feadb] | 934 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 935 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 936 | ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
|
---|
[c0964cd7] | 937 |
|
---|
[2f2feadb] | 938 | // Which index block is target for new entry
|
---|
| 939 | uint32_t position_index = (dx_block->position - dx_block->entries);
|
---|
| 940 | if (position_index >= count_left) {
|
---|
[c0964cd7] | 941 |
|
---|
[2f2feadb] | 942 | dx_block->block->dirty = true;
|
---|
| 943 |
|
---|
| 944 | block_t *block_tmp = dx_block->block;
|
---|
| 945 | dx_block->block = new_block;
|
---|
| 946 | dx_block->position = new_entries + position_index - count_left;
|
---|
| 947 | dx_block->entries = new_entries;
|
---|
| 948 |
|
---|
| 949 | new_block = block_tmp;
|
---|
| 950 |
|
---|
| 951 | }
|
---|
[c0964cd7] | 952 |
|
---|
[2f2feadb] | 953 | ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
|
---|
[c0964cd7] | 954 |
|
---|
[2f2feadb] | 955 | return block_put(new_block);
|
---|
[c0964cd7] | 956 |
|
---|
| 957 | } else {
|
---|
| 958 | EXT4FS_DBG("create second level");
|
---|
[6f731f0a] | 959 |
|
---|
| 960 | memcpy((void *) new_entries, (void *) entries,
|
---|
| 961 | leaf_count * sizeof(ext4_directory_dx_entry_t));
|
---|
| 962 |
|
---|
| 963 | ext4_directory_dx_countlimit_t *new_countlimit =
|
---|
| 964 | (ext4_directory_dx_countlimit_t *)new_entries;
|
---|
| 965 |
|
---|
[2f2feadb] | 966 | uint32_t entry_space = block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 967 | uint32_t node_limit = entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
| 968 | ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
|
---|
[6f731f0a] | 969 |
|
---|
[2f2feadb] | 970 | // Set values in root node
|
---|
| 971 | ext4_directory_dx_countlimit_t *new_root_countlimit =
|
---|
| 972 | (ext4_directory_dx_countlimit_t *)entries;
|
---|
[6f731f0a] | 973 |
|
---|
[2f2feadb] | 974 | ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
|
---|
| 975 | ext4_directory_dx_entry_set_block(entries, new_iblock);
|
---|
[6f731f0a] | 976 |
|
---|
[2f2feadb] | 977 | ((ext4_directory_dx_root_t *)dx_blocks[0].block->data)->info.indirect_levels = 1;
|
---|
[6f731f0a] | 978 |
|
---|
[2f2feadb] | 979 | /* Add new access path frame */
|
---|
| 980 | dx_block = dx_blocks + 1;
|
---|
| 981 | dx_block->position = dx_block->position - entries + new_entries;
|
---|
| 982 | dx_block->entries = new_entries;
|
---|
| 983 | dx_block->block = new_block;
|
---|
[c0964cd7] | 984 | }
|
---|
[2f2feadb] | 985 |
|
---|
| 986 | }
|
---|
| 987 |
|
---|
| 988 | return EOK;
|
---|
| 989 | }
|
---|
| 990 |
|
---|
[1ac1ab4] | 991 | int ext4_directory_dx_add_entry(ext4_inode_ref_t *parent,
|
---|
| 992 | ext4_inode_ref_t *child, const char *name)
|
---|
[2f2feadb] | 993 | {
|
---|
| 994 | int rc = EOK;
|
---|
| 995 | int rc2 = EOK;
|
---|
| 996 |
|
---|
| 997 | // get direct block 0 (index root)
|
---|
| 998 | uint32_t root_block_addr;
|
---|
[1ac1ab4] | 999 | rc = ext4_filesystem_get_inode_data_block_index(parent, 0, &root_block_addr);
|
---|
[2f2feadb] | 1000 | if (rc != EOK) {
|
---|
| 1001 | return rc;
|
---|
| 1002 | }
|
---|
| 1003 |
|
---|
[1ac1ab4] | 1004 | ext4_filesystem_t *fs = parent->fs;
|
---|
| 1005 |
|
---|
[2f2feadb] | 1006 | block_t *root_block;
|
---|
| 1007 | rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 1008 | if (rc != EOK) {
|
---|
| 1009 | return rc;
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
| 1012 | uint32_t name_len = strlen(name);
|
---|
| 1013 | ext4_hash_info_t hinfo;
|
---|
| 1014 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
|
---|
| 1015 | if (rc != EOK) {
|
---|
| 1016 | block_put(root_block);
|
---|
[7eb033ce] | 1017 | EXT4FS_DBG("hinfo initialization error");
|
---|
[2f2feadb] | 1018 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 1019 | }
|
---|
| 1020 |
|
---|
| 1021 | // Hardcoded number 2 means maximum height of index tree !!!
|
---|
| 1022 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
| 1023 | ext4_directory_dx_block_t *dx_block, *dx_it;
|
---|
[1ac1ab4] | 1024 | rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block, &dx_block, dx_blocks);
|
---|
[2f2feadb] | 1025 | if (rc != EOK) {
|
---|
[7eb033ce] | 1026 | EXT4FS_DBG("get leaf error");
|
---|
[2f2feadb] | 1027 | rc = EXT4_ERR_BAD_DX_DIR;
|
---|
| 1028 | goto release_index;
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
| 1031 |
|
---|
| 1032 | // Try to insert to existing data block
|
---|
| 1033 | uint32_t leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
| 1034 | uint32_t leaf_block_addr;
|
---|
[1ac1ab4] | 1035 | rc = ext4_filesystem_get_inode_data_block_index(parent, leaf_block_idx, &leaf_block_addr);
|
---|
[2f2feadb] | 1036 | if (rc != EOK) {
|
---|
| 1037 | goto release_index;
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 |
|
---|
| 1041 | block_t *target_block;
|
---|
| 1042 | rc = block_get(&target_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
|
---|
| 1043 | if (rc != EOK) {
|
---|
| 1044 | goto release_index;
|
---|
| 1045 | }
|
---|
| 1046 |
|
---|
| 1047 |
|
---|
| 1048 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
| 1049 | if (rc == EOK) {
|
---|
| 1050 | goto release_target_index;
|
---|
| 1051 | }
|
---|
| 1052 |
|
---|
| 1053 | EXT4FS_DBG("no free space found");
|
---|
| 1054 |
|
---|
| 1055 | rc = ext4_directory_dx_split_index(fs, parent, dx_blocks, dx_block);
|
---|
| 1056 | if (rc != EOK) {
|
---|
| 1057 | goto release_target_index;
|
---|
[c4f318d6] | 1058 | }
|
---|
[5c83612b] | 1059 |
|
---|
[c4f318d6] | 1060 | block_t *new_block = NULL;
|
---|
[e6f6260] | 1061 | rc = ext4_directory_dx_split_data(parent, &hinfo, target_block, dx_block, &new_block);
|
---|
[c4f318d6] | 1062 | if (rc != EOK) {
|
---|
[e63ce679] | 1063 | rc2 = rc;
|
---|
| 1064 | goto release_target_index;
|
---|
[c4f318d6] | 1065 | }
|
---|
[5c83612b] | 1066 |
|
---|
[d0d7afb] | 1067 | // Where to save new entry
|
---|
[1d68621] | 1068 | uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
|
---|
[c4f318d6] | 1069 | if (hinfo.hash >= new_block_hash) {
|
---|
[d0d7afb] | 1070 | rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
|
---|
[c4f318d6] | 1071 | } else {
|
---|
[d0d7afb] | 1072 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
|
---|
[c4f318d6] | 1073 | }
|
---|
[5c83612b] | 1074 |
|
---|
[60b8b99] | 1075 |
|
---|
| 1076 |
|
---|
| 1077 |
|
---|
[d0d7afb] | 1078 | rc = block_put(new_block);
|
---|
| 1079 | if (rc != EOK) {
|
---|
| 1080 | EXT4FS_DBG("error writing new block");
|
---|
[60b8b99] | 1081 | return rc;
|
---|
[d0d7afb] | 1082 | }
|
---|
[c4f318d6] | 1083 |
|
---|
[e63ce679] | 1084 | release_target_index:
|
---|
[c4f318d6] | 1085 |
|
---|
[d0d7afb] | 1086 | rc2 = rc;
|
---|
[c4f318d6] | 1087 |
|
---|
[412f813] | 1088 | rc = block_put(target_block);
|
---|
| 1089 | if (rc != EOK) {
|
---|
[e8d054a] | 1090 | EXT4FS_DBG("error writing target block");
|
---|
[60b8b99] | 1091 | return rc;
|
---|
[412f813] | 1092 | }
|
---|
[c4f318d6] | 1093 |
|
---|
[e63ce679] | 1094 | release_index:
|
---|
[60b8b99] | 1095 |
|
---|
| 1096 | if (rc != EOK) {
|
---|
| 1097 | rc2 = rc;
|
---|
| 1098 | }
|
---|
| 1099 |
|
---|
[e63ce679] | 1100 | dx_it = dx_blocks;
|
---|
[c4f318d6] | 1101 |
|
---|
| 1102 | while (dx_it <= dx_block) {
|
---|
[412f813] | 1103 | rc = block_put(dx_it->block);
|
---|
| 1104 | if (rc != EOK) {
|
---|
[e8d054a] | 1105 | EXT4FS_DBG("error writing index block");
|
---|
[60b8b99] | 1106 | return rc;
|
---|
[412f813] | 1107 | }
|
---|
[c4f318d6] | 1108 | dx_it++;
|
---|
| 1109 | }
|
---|
| 1110 |
|
---|
[d0d7afb] | 1111 | return rc2;
|
---|
[565b6ff] | 1112 | }
|
---|
[0dc91833] | 1113 |
|
---|
| 1114 |
|
---|
| 1115 | /**
|
---|
| 1116 | * @}
|
---|
| 1117 | */
|
---|