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