[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) */
|
---|
[e5a1ace3] | 529 | rc = block_put(p->block);
|
---|
| 530 | if (rc != EOK)
|
---|
| 531 | return rc;
|
---|
[38542dc] | 532 |
|
---|
| 533 | p->block = block;
|
---|
| 534 | p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
|
---|
| 535 | p->position = p->entries;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | return ENOENT;
|
---|
[0dc91833] | 539 | }
|
---|
| 540 |
|
---|
[e6f6260] | 541 | /** Try to find directory entry using directory index.
|
---|
| 542 | *
|
---|
[38542dc] | 543 | * @param result Output value - if entry will be found,
|
---|
| 544 | * than will be passed through this parameter
|
---|
| 545 | * @param inode_ref Directory i-node
|
---|
| 546 | * @param name_len Length of name to be found
|
---|
| 547 | * @param name Name to be found
|
---|
| 548 | *
|
---|
| 549 | * @return Error code
|
---|
| 550 | *
|
---|
[e6f6260] | 551 | */
|
---|
[7689590] | 552 | int ext4_directory_dx_find_entry(ext4_directory_search_result_t *result,
|
---|
[38542dc] | 553 | ext4_inode_ref_t *inode_ref, size_t name_len, const char *name)
|
---|
[0dc91833] | 554 | {
|
---|
[06d85e5] | 555 | /* Load direct block 0 (index root) */
|
---|
[d9bbe45] | 556 | uint32_t root_block_addr;
|
---|
[e5a1ace3] | 557 | int rc2;
|
---|
[38542dc] | 558 | int rc = ext4_filesystem_get_inode_data_block_index(inode_ref, 0,
|
---|
| 559 | &root_block_addr);
|
---|
| 560 | if (rc != EOK)
|
---|
[0dc91833] | 561 | return rc;
|
---|
[38542dc] | 562 |
|
---|
[1ac1ab4] | 563 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[38542dc] | 564 |
|
---|
[d9bbe45] | 565 | block_t *root_block;
|
---|
[38542dc] | 566 | rc = block_get(&root_block, fs->device, root_block_addr,
|
---|
| 567 | BLOCK_FLAGS_NONE);
|
---|
| 568 | if (rc != EOK)
|
---|
[0dc91833] | 569 | return rc;
|
---|
[38542dc] | 570 |
|
---|
[06d85e5] | 571 | /* Initialize hash info (compute hash value) */
|
---|
[d9bbe45] | 572 | ext4_hash_info_t hinfo;
|
---|
[38542dc] | 573 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock,
|
---|
| 574 | name_len, name);
|
---|
[0dc91833] | 575 | if (rc != EOK) {
|
---|
| 576 | block_put(root_block);
|
---|
| 577 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 578 | }
|
---|
[38542dc] | 579 |
|
---|
| 580 | /*
|
---|
| 581 | * Hardcoded number 2 means maximum height of index tree,
|
---|
| 582 | * specified in the Linux driver.
|
---|
| 583 | */
|
---|
[d9bbe45] | 584 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
[38542dc] | 585 | ext4_directory_dx_block_t *dx_block;
|
---|
| 586 | ext4_directory_dx_block_t *tmp;
|
---|
| 587 |
|
---|
| 588 | rc = ext4_directory_dx_get_leaf(&hinfo, inode_ref, root_block,
|
---|
| 589 | &dx_block, dx_blocks);
|
---|
[0dc91833] | 590 | if (rc != EOK) {
|
---|
| 591 | block_put(root_block);
|
---|
| 592 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 593 | }
|
---|
[38542dc] | 594 |
|
---|
[d9bbe45] | 595 | do {
|
---|
[06d85e5] | 596 | /* Load leaf block */
|
---|
[38542dc] | 597 | uint32_t leaf_block_idx =
|
---|
| 598 | ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
[d9bbe45] | 599 | uint32_t leaf_block_addr;
|
---|
[38542dc] | 600 |
|
---|
| 601 | rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
|
---|
| 602 | leaf_block_idx, &leaf_block_addr);
|
---|
| 603 | if (rc != EOK)
|
---|
[e63ce679] | 604 | goto cleanup;
|
---|
[38542dc] | 605 |
|
---|
| 606 | block_t *leaf_block;
|
---|
| 607 | rc = block_get(&leaf_block, fs->device, leaf_block_addr,
|
---|
| 608 | BLOCK_FLAGS_NONE);
|
---|
| 609 | if (rc != EOK)
|
---|
| 610 | goto cleanup;
|
---|
| 611 |
|
---|
[06d85e5] | 612 | /* Linear search inside block */
|
---|
[d9bbe45] | 613 | ext4_directory_entry_ll_t *res_dentry;
|
---|
[38542dc] | 614 | rc = ext4_directory_find_in_block(leaf_block, fs->superblock,
|
---|
| 615 | name_len, name, &res_dentry);
|
---|
| 616 |
|
---|
[06d85e5] | 617 | /* Found => return it */
|
---|
[7689590] | 618 | if (rc == EOK) {
|
---|
| 619 | result->block = leaf_block;
|
---|
| 620 | result->dentry = res_dentry;
|
---|
[e8d054a] | 621 | goto cleanup;
|
---|
[0dc91833] | 622 | }
|
---|
[38542dc] | 623 |
|
---|
[06d85e5] | 624 | /* Not found, leave untouched */
|
---|
[e5a1ace3] | 625 | rc2 = block_put(leaf_block);
|
---|
| 626 | if (rc2 != EOK)
|
---|
| 627 | goto cleanup;
|
---|
[38542dc] | 628 |
|
---|
| 629 | if (rc != ENOENT)
|
---|
[e63ce679] | 630 | goto cleanup;
|
---|
[38542dc] | 631 |
|
---|
[06d85e5] | 632 | /* check if the next block could be checked */
|
---|
[38542dc] | 633 | rc = ext4_directory_dx_next_block(inode_ref, hinfo.hash,
|
---|
| 634 | dx_block, &dx_blocks[0]);
|
---|
[e5a1ace3] | 635 | if (rc != EOK)
|
---|
[e63ce679] | 636 | goto cleanup;
|
---|
[e5a1ace3] | 637 |
|
---|
[38542dc] | 638 | } while (rc == ENOENT);
|
---|
| 639 |
|
---|
[06d85e5] | 640 | /* Entry not found */
|
---|
[e8d054a] | 641 | rc = ENOENT;
|
---|
[38542dc] | 642 |
|
---|
[e63ce679] | 643 | cleanup:
|
---|
[06d85e5] | 644 | /* The whole path must be released (preventing memory leak) */
|
---|
[e63ce679] | 645 | tmp = dx_blocks;
|
---|
[38542dc] | 646 |
|
---|
[e63ce679] | 647 | while (tmp <= dx_block) {
|
---|
[e5a1ace3] | 648 | rc2 = block_put(tmp->block);
|
---|
| 649 | if (rc == EOK && rc2 != EOK)
|
---|
| 650 | rc = rc2;
|
---|
[e63ce679] | 651 | ++tmp;
|
---|
| 652 | }
|
---|
[38542dc] | 653 |
|
---|
[e63ce679] | 654 | return rc;
|
---|
[0dc91833] | 655 | }
|
---|
| 656 |
|
---|
[e6f6260] | 657 | /** Compare function used to pass in quicksort implementation.
|
---|
| 658 | *
|
---|
| 659 | * It can compare two entries by hash value.
|
---|
| 660 | *
|
---|
[38542dc] | 661 | * @param arg1 First entry
|
---|
| 662 | * @param arg2 Second entry
|
---|
| 663 | * @param dummy Unused parameter, can be NULL
|
---|
| 664 | *
|
---|
| 665 | * @return Classic compare result
|
---|
| 666 | * (0: equal, -1: arg1 < arg2, 1: arg1 > arg2)
|
---|
| 667 | *
|
---|
[e6f6260] | 668 | */
|
---|
[b191acae] | 669 | static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy)
|
---|
[f577058] | 670 | {
|
---|
| 671 | ext4_dx_sort_entry_t *entry1 = arg1;
|
---|
| 672 | ext4_dx_sort_entry_t *entry2 = arg2;
|
---|
[38542dc] | 673 |
|
---|
| 674 | if (entry1->hash == entry2->hash)
|
---|
[f577058] | 675 | return 0;
|
---|
[38542dc] | 676 |
|
---|
| 677 | if (entry1->hash < entry2->hash)
|
---|
[f577058] | 678 | return -1;
|
---|
[38542dc] | 679 | else
|
---|
[f577058] | 680 | return 1;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[e6f6260] | 683 | /** Insert new index entry to block.
|
---|
| 684 | *
|
---|
| 685 | * Note that space for new entry must be checked by caller.
|
---|
| 686 | *
|
---|
[38542dc] | 687 | * @param index_block Block where to insert new entry
|
---|
| 688 | * @param hash Hash value covered by child node
|
---|
| 689 | * @param iblock Logical number of child block
|
---|
[e6f6260] | 690 | *
|
---|
| 691 | */
|
---|
[c0964cd7] | 692 | static void ext4_directory_dx_insert_entry(
|
---|
[38542dc] | 693 | ext4_directory_dx_block_t *index_block, uint32_t hash, uint32_t iblock)
|
---|
[c0964cd7] | 694 | {
|
---|
| 695 | ext4_directory_dx_entry_t *old_index_entry = index_block->position;
|
---|
| 696 | ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
|
---|
[38542dc] | 697 |
|
---|
[c0964cd7] | 698 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
[38542dc] | 699 | (ext4_directory_dx_countlimit_t *) index_block->entries;
|
---|
[c0964cd7] | 700 | uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
[38542dc] | 701 |
|
---|
[c0964cd7] | 702 | ext4_directory_dx_entry_t *start_index = index_block->entries;
|
---|
[38542dc] | 703 | size_t bytes = (void *) (start_index + count) - (void *) (new_index_entry);
|
---|
| 704 |
|
---|
[c0964cd7] | 705 | memmove(new_index_entry + 1, new_index_entry, bytes);
|
---|
[38542dc] | 706 |
|
---|
[c0964cd7] | 707 | ext4_directory_dx_entry_set_block(new_index_entry, iblock);
|
---|
| 708 | ext4_directory_dx_entry_set_hash(new_index_entry, hash);
|
---|
[38542dc] | 709 |
|
---|
[c0964cd7] | 710 | ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
|
---|
[38542dc] | 711 |
|
---|
[c0964cd7] | 712 | index_block->block->dirty = true;
|
---|
| 713 | }
|
---|
| 714 |
|
---|
[e6f6260] | 715 | /** Split directory entries to two parts preventing node overflow.
|
---|
| 716 | *
|
---|
[38542dc] | 717 | * @param inode_ref Directory i-node
|
---|
| 718 | * @param hinfo Hash info
|
---|
| 719 | * @param old_data_block Block with data to be split
|
---|
| 720 | * @param index_block Block where index entries are located
|
---|
| 721 | * @param new_data_block Output value for newly allocated data block
|
---|
| 722 | *
|
---|
[e6f6260] | 723 | */
|
---|
| 724 | static int ext4_directory_dx_split_data(ext4_inode_ref_t *inode_ref,
|
---|
[38542dc] | 725 | ext4_hash_info_t *hinfo, block_t *old_data_block,
|
---|
| 726 | ext4_directory_dx_block_t *index_block, block_t **new_data_block)
|
---|
[f577058] | 727 | {
|
---|
[d0d7afb] | 728 | int rc = EOK;
|
---|
[38542dc] | 729 |
|
---|
[06d85e5] | 730 | /* Allocate buffer for directory entries */
|
---|
[e6f6260] | 731 | uint32_t block_size =
|
---|
[38542dc] | 732 | ext4_superblock_get_block_size(inode_ref->fs->superblock);
|
---|
[f577058] | 733 | void *entry_buffer = malloc(block_size);
|
---|
[38542dc] | 734 | if (entry_buffer == NULL)
|
---|
[f577058] | 735 | return ENOMEM;
|
---|
[38542dc] | 736 |
|
---|
[06d85e5] | 737 | /* dot entry has the smallest size available */
|
---|
[38542dc] | 738 | uint32_t max_entry_count =
|
---|
| 739 | block_size / sizeof(ext4_directory_dx_dot_entry_t);
|
---|
| 740 |
|
---|
[06d85e5] | 741 | /* Allocate sort entry */
|
---|
[38542dc] | 742 | ext4_dx_sort_entry_t *sort_array =
|
---|
| 743 | malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
|
---|
[f577058] | 744 | if (sort_array == NULL) {
|
---|
| 745 | free(entry_buffer);
|
---|
| 746 | return ENOMEM;
|
---|
| 747 | }
|
---|
[38542dc] | 748 |
|
---|
[1d68621] | 749 | uint32_t idx = 0;
|
---|
[ccac91e] | 750 | uint32_t real_size = 0;
|
---|
[38542dc] | 751 |
|
---|
[06d85e5] | 752 | /* Initialize hinfo */
|
---|
[1d68621] | 753 | ext4_hash_info_t tmp_hinfo;
|
---|
| 754 | memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
|
---|
[38542dc] | 755 |
|
---|
[06d85e5] | 756 | /* Load all valid entries to the buffer */
|
---|
[e8d054a] | 757 | ext4_directory_entry_ll_t *dentry = old_data_block->data;
|
---|
| 758 | void *entry_buffer_ptr = entry_buffer;
|
---|
[ccac91e] | 759 | while ((void *)dentry < old_data_block->data + block_size) {
|
---|
[06d85e5] | 760 | /* Read only valid entries */
|
---|
[412f813] | 761 | if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
|
---|
[e6f6260] | 762 | uint8_t len = ext4_directory_entry_ll_get_name_length(
|
---|
[38542dc] | 763 | inode_ref->fs->superblock, dentry);
|
---|
| 764 | ext4_hash_string(&tmp_hinfo, len, (char *) dentry->name);
|
---|
| 765 |
|
---|
[412f813] | 766 | uint32_t rec_len = 8 + len;
|
---|
[38542dc] | 767 |
|
---|
| 768 | if ((rec_len % 4) != 0)
|
---|
[412f813] | 769 | rec_len += 4 - (rec_len % 4);
|
---|
[38542dc] | 770 |
|
---|
[412f813] | 771 | memcpy(entry_buffer_ptr, dentry, rec_len);
|
---|
[38542dc] | 772 |
|
---|
[412f813] | 773 | sort_array[idx].dentry = entry_buffer_ptr;
|
---|
| 774 | sort_array[idx].rec_len = rec_len;
|
---|
| 775 | sort_array[idx].hash = tmp_hinfo.hash;
|
---|
[38542dc] | 776 |
|
---|
[412f813] | 777 | entry_buffer_ptr += rec_len;
|
---|
| 778 | real_size += rec_len;
|
---|
| 779 | idx++;
|
---|
| 780 | }
|
---|
[38542dc] | 781 |
|
---|
| 782 | dentry = (void *) dentry +
|
---|
| 783 | ext4_directory_entry_ll_get_entry_length(dentry);
|
---|
[f577058] | 784 | }
|
---|
[38542dc] | 785 |
|
---|
[06d85e5] | 786 | /* Sort all entries */
|
---|
[5b16912] | 787 | qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t),
|
---|
[38542dc] | 788 | ext4_directory_dx_entry_comparator, NULL);
|
---|
| 789 |
|
---|
[06d85e5] | 790 | /* Allocate new block for store the second part of entries */
|
---|
[f577058] | 791 | uint32_t new_fblock;
|
---|
[c4f318d6] | 792 | uint32_t new_iblock;
|
---|
[38542dc] | 793 | rc = ext4_filesystem_append_inode_block(inode_ref, &new_fblock,
|
---|
| 794 | &new_iblock);
|
---|
[f577058] | 795 | if (rc != EOK) {
|
---|
| 796 | free(sort_array);
|
---|
| 797 | free(entry_buffer);
|
---|
| 798 | return rc;
|
---|
| 799 | }
|
---|
[38542dc] | 800 |
|
---|
[06d85e5] | 801 | /* Load new block */
|
---|
[ccac91e] | 802 | block_t *new_data_block_tmp;
|
---|
[e6f6260] | 803 | rc = block_get(&new_data_block_tmp, inode_ref->fs->device,
|
---|
[38542dc] | 804 | new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
[ccac91e] | 805 | if (rc != EOK) {
|
---|
| 806 | free(sort_array);
|
---|
| 807 | free(entry_buffer);
|
---|
| 808 | return rc;
|
---|
| 809 | }
|
---|
[38542dc] | 810 |
|
---|
| 811 | /*
|
---|
| 812 | * Distribute entries to two blocks (by size)
|
---|
[06d85e5] | 813 | * - compute the half
|
---|
| 814 | */
|
---|
[ccac91e] | 815 | uint32_t new_hash = 0;
|
---|
| 816 | uint32_t current_size = 0;
|
---|
[1d68621] | 817 | uint32_t mid = 0;
|
---|
| 818 | for (uint32_t i = 0; i < idx; ++i) {
|
---|
| 819 | if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
|
---|
[ccac91e] | 820 | new_hash = sort_array[i].hash;
|
---|
| 821 | mid = i;
|
---|
| 822 | break;
|
---|
| 823 | }
|
---|
[38542dc] | 824 |
|
---|
[ccac91e] | 825 | current_size += sort_array[i].rec_len;
|
---|
| 826 | }
|
---|
[38542dc] | 827 |
|
---|
[06d85e5] | 828 | /* Check hash collision */
|
---|
[412f813] | 829 | uint32_t continued = 0;
|
---|
[38542dc] | 830 | if (new_hash == sort_array[mid-1].hash)
|
---|
[412f813] | 831 | continued = 1;
|
---|
[38542dc] | 832 |
|
---|
[ccac91e] | 833 | uint32_t offset = 0;
|
---|
| 834 | void *ptr;
|
---|
[38542dc] | 835 |
|
---|
[06d85e5] | 836 | /* First part - to the old block */
|
---|
[1d68621] | 837 | for (uint32_t i = 0; i < mid; ++i) {
|
---|
[ccac91e] | 838 | ptr = old_data_block->data + offset;
|
---|
| 839 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
[38542dc] | 840 |
|
---|
[ccac91e] | 841 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
[38542dc] | 842 | if (i < (mid - 1))
|
---|
| 843 | ext4_directory_entry_ll_set_entry_length(tmp,
|
---|
| 844 | sort_array[i].rec_len);
|
---|
| 845 | else
|
---|
| 846 | ext4_directory_entry_ll_set_entry_length(tmp,
|
---|
| 847 | block_size - offset);
|
---|
| 848 |
|
---|
[ccac91e] | 849 | offset += sort_array[i].rec_len;
|
---|
| 850 | }
|
---|
[38542dc] | 851 |
|
---|
[06d85e5] | 852 | /* Second part - to the new block */
|
---|
[ccac91e] | 853 | offset = 0;
|
---|
[1d68621] | 854 | for (uint32_t i = mid; i < idx; ++i) {
|
---|
[ccac91e] | 855 | ptr = new_data_block_tmp->data + offset;
|
---|
| 856 | memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
|
---|
[38542dc] | 857 |
|
---|
[ccac91e] | 858 | ext4_directory_entry_ll_t *tmp = ptr;
|
---|
[38542dc] | 859 | if (i < (idx - 1))
|
---|
| 860 | ext4_directory_entry_ll_set_entry_length(tmp,
|
---|
| 861 | sort_array[i].rec_len);
|
---|
| 862 | else
|
---|
| 863 | ext4_directory_entry_ll_set_entry_length(tmp,
|
---|
| 864 | block_size - offset);
|
---|
| 865 |
|
---|
[ccac91e] | 866 | offset += sort_array[i].rec_len;
|
---|
| 867 | }
|
---|
[38542dc] | 868 |
|
---|
[06d85e5] | 869 | /* Do some steps to finish operation */
|
---|
[ccac91e] | 870 | old_data_block->dirty = true;
|
---|
| 871 | new_data_block_tmp->dirty = true;
|
---|
[38542dc] | 872 |
|
---|
[ccac91e] | 873 | free(sort_array);
|
---|
| 874 | free(entry_buffer);
|
---|
[38542dc] | 875 |
|
---|
| 876 | ext4_directory_dx_insert_entry(index_block, new_hash + continued,
|
---|
| 877 | new_iblock);
|
---|
| 878 |
|
---|
[ccac91e] | 879 | *new_data_block = new_data_block_tmp;
|
---|
[38542dc] | 880 |
|
---|
[f577058] | 881 | return EOK;
|
---|
| 882 | }
|
---|
| 883 |
|
---|
[dfac604] | 884 | /** Split index node and maybe some parent nodes in the tree hierarchy.
|
---|
[e6f6260] | 885 | *
|
---|
[38542dc] | 886 | * @param inode_ref Directory i-node
|
---|
| 887 | * @param dx_blocks Array with path from root to leaf node
|
---|
| 888 | * @param dx_block Leaf block to be split if needed
|
---|
| 889 | *
|
---|
| 890 | * @return Error code
|
---|
| 891 | *
|
---|
[e6f6260] | 892 | */
|
---|
[dfac604] | 893 | static int ext4_directory_dx_split_index(ext4_inode_ref_t *inode_ref,
|
---|
| 894 | ext4_directory_dx_block_t *dx_blocks, ext4_directory_dx_block_t *dx_block)
|
---|
[565b6ff] | 895 | {
|
---|
[2f2feadb] | 896 | ext4_directory_dx_entry_t *entries;
|
---|
[38542dc] | 897 | if (dx_block == dx_blocks)
|
---|
| 898 | entries =
|
---|
| 899 | ((ext4_directory_dx_root_t *) dx_block->block->data)->entries;
|
---|
| 900 | else
|
---|
| 901 | entries =
|
---|
| 902 | ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
|
---|
| 903 |
|
---|
[2f2feadb] | 904 | ext4_directory_dx_countlimit_t *countlimit =
|
---|
[38542dc] | 905 | (ext4_directory_dx_countlimit_t *) entries;
|
---|
| 906 |
|
---|
| 907 | uint16_t leaf_limit =
|
---|
| 908 | ext4_directory_dx_countlimit_get_limit(countlimit);
|
---|
| 909 | uint16_t leaf_count =
|
---|
| 910 | ext4_directory_dx_countlimit_get_count(countlimit);
|
---|
| 911 |
|
---|
[06d85e5] | 912 | /* Check if is necessary to split index block */
|
---|
[1ebb66f] | 913 | if (leaf_limit == leaf_count) {
|
---|
[38542dc] | 914 | size_t levels = dx_block - dx_blocks;
|
---|
| 915 |
|
---|
[2f2feadb] | 916 | ext4_directory_dx_entry_t *root_entries =
|
---|
[38542dc] | 917 | ((ext4_directory_dx_root_t *) dx_blocks[0].block->data)->entries;
|
---|
| 918 |
|
---|
[2f2feadb] | 919 | ext4_directory_dx_countlimit_t *root_countlimit =
|
---|
[38542dc] | 920 | (ext4_directory_dx_countlimit_t *) root_entries;
|
---|
[2f2feadb] | 921 | uint16_t root_limit =
|
---|
[38542dc] | 922 | ext4_directory_dx_countlimit_get_limit(root_countlimit);
|
---|
[2f2feadb] | 923 | uint16_t root_count =
|
---|
[38542dc] | 924 | ext4_directory_dx_countlimit_get_count(root_countlimit);
|
---|
| 925 |
|
---|
[06d85e5] | 926 | /* Linux limitation */
|
---|
[38542dc] | 927 | if ((levels > 0) && (root_limit == root_count))
|
---|
[2f2feadb] | 928 | return ENOSPC;
|
---|
[38542dc] | 929 |
|
---|
[06d85e5] | 930 | /* Add new block to directory */
|
---|
[c0964cd7] | 931 | uint32_t new_fblock;
|
---|
| 932 | uint32_t new_iblock;
|
---|
[38542dc] | 933 | int rc = ext4_filesystem_append_inode_block(inode_ref,
|
---|
| 934 | &new_fblock, &new_iblock);
|
---|
| 935 | if (rc != EOK)
|
---|
[2f2feadb] | 936 | return rc;
|
---|
[38542dc] | 937 |
|
---|
[06d85e5] | 938 | /* load new block */
|
---|
[38542dc] | 939 | block_t *new_block;
|
---|
[dfac604] | 940 | rc = block_get(&new_block, inode_ref->fs->device,
|
---|
[38542dc] | 941 | new_fblock, BLOCK_FLAGS_NOREAD);
|
---|
| 942 | if (rc != EOK)
|
---|
[2f2feadb] | 943 | return rc;
|
---|
[38542dc] | 944 |
|
---|
[6f731f0a] | 945 | ext4_directory_dx_node_t *new_node = new_block->data;
|
---|
| 946 | ext4_directory_dx_entry_t *new_entries = new_node->entries;
|
---|
[38542dc] | 947 |
|
---|
| 948 | uint32_t block_size =
|
---|
| 949 | ext4_superblock_get_block_size(inode_ref->fs->superblock);
|
---|
| 950 |
|
---|
[06d85e5] | 951 | /* Split leaf node */
|
---|
[c0964cd7] | 952 | if (levels > 0) {
|
---|
| 953 | uint32_t count_left = leaf_count / 2;
|
---|
| 954 | uint32_t count_right = leaf_count - count_left;
|
---|
[2f2feadb] | 955 | uint32_t hash_right =
|
---|
[38542dc] | 956 | ext4_directory_dx_entry_get_hash(entries + count_left);
|
---|
| 957 |
|
---|
[06d85e5] | 958 | /* Copy data to new node */
|
---|
[c0964cd7] | 959 | memcpy((void *) new_entries, (void *) (entries + count_left),
|
---|
[38542dc] | 960 | count_right * sizeof(ext4_directory_dx_entry_t));
|
---|
| 961 |
|
---|
[06d85e5] | 962 | /* Initialize new node */
|
---|
[2f2feadb] | 963 | ext4_directory_dx_countlimit_t *left_countlimit =
|
---|
[38542dc] | 964 | (ext4_directory_dx_countlimit_t *) entries;
|
---|
[2f2feadb] | 965 | ext4_directory_dx_countlimit_t *right_countlimit =
|
---|
[38542dc] | 966 | (ext4_directory_dx_countlimit_t *) new_entries;
|
---|
| 967 |
|
---|
[c0964cd7] | 968 | ext4_directory_dx_countlimit_set_count(left_countlimit, count_left);
|
---|
| 969 | ext4_directory_dx_countlimit_set_count(right_countlimit, count_right);
|
---|
[38542dc] | 970 |
|
---|
| 971 | uint32_t entry_space =
|
---|
| 972 | block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 973 | uint32_t node_limit =
|
---|
| 974 | entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
[2f2feadb] | 975 | ext4_directory_dx_countlimit_set_limit(right_countlimit, node_limit);
|
---|
[38542dc] | 976 |
|
---|
[06d85e5] | 977 | /* Which index block is target for new entry */
|
---|
[2f2feadb] | 978 | uint32_t position_index = (dx_block->position - dx_block->entries);
|
---|
| 979 | if (position_index >= count_left) {
|
---|
| 980 | dx_block->block->dirty = true;
|
---|
[38542dc] | 981 |
|
---|
[2f2feadb] | 982 | block_t *block_tmp = dx_block->block;
|
---|
| 983 | dx_block->block = new_block;
|
---|
[38542dc] | 984 | dx_block->position =
|
---|
| 985 | new_entries + position_index - count_left;
|
---|
[2f2feadb] | 986 | dx_block->entries = new_entries;
|
---|
[38542dc] | 987 |
|
---|
[2f2feadb] | 988 | new_block = block_tmp;
|
---|
| 989 | }
|
---|
[38542dc] | 990 |
|
---|
[06d85e5] | 991 | /* Finally insert new entry */
|
---|
[2f2feadb] | 992 | ext4_directory_dx_insert_entry(dx_blocks, hash_right, new_iblock);
|
---|
[38542dc] | 993 |
|
---|
[2f2feadb] | 994 | return block_put(new_block);
|
---|
[c0964cd7] | 995 | } else {
|
---|
[06d85e5] | 996 | /* Create second level index */
|
---|
[38542dc] | 997 |
|
---|
[06d85e5] | 998 | /* Copy data from root to child block */
|
---|
[6f731f0a] | 999 | memcpy((void *) new_entries, (void *) entries,
|
---|
[38542dc] | 1000 | leaf_count * sizeof(ext4_directory_dx_entry_t));
|
---|
| 1001 |
|
---|
[6f731f0a] | 1002 | ext4_directory_dx_countlimit_t *new_countlimit =
|
---|
[38542dc] | 1003 | (ext4_directory_dx_countlimit_t *) new_entries;
|
---|
| 1004 |
|
---|
| 1005 | uint32_t entry_space =
|
---|
| 1006 | block_size - sizeof(ext4_fake_directory_entry_t);
|
---|
| 1007 | uint32_t node_limit =
|
---|
| 1008 | entry_space / sizeof(ext4_directory_dx_entry_t);
|
---|
[2f2feadb] | 1009 | ext4_directory_dx_countlimit_set_limit(new_countlimit, node_limit);
|
---|
[38542dc] | 1010 |
|
---|
[06d85e5] | 1011 | /* Set values in root node */
|
---|
[2f2feadb] | 1012 | ext4_directory_dx_countlimit_t *new_root_countlimit =
|
---|
[38542dc] | 1013 | (ext4_directory_dx_countlimit_t *) entries;
|
---|
| 1014 |
|
---|
[2f2feadb] | 1015 | ext4_directory_dx_countlimit_set_count(new_root_countlimit, 1);
|
---|
| 1016 | ext4_directory_dx_entry_set_block(entries, new_iblock);
|
---|
[38542dc] | 1017 |
|
---|
| 1018 | ((ext4_directory_dx_root_t *)
|
---|
| 1019 | dx_blocks[0].block->data)->info.indirect_levels = 1;
|
---|
| 1020 |
|
---|
[06d85e5] | 1021 | /* Add new entry to the path */
|
---|
[2f2feadb] | 1022 | dx_block = dx_blocks + 1;
|
---|
| 1023 | dx_block->position = dx_block->position - entries + new_entries;
|
---|
| 1024 | dx_block->entries = new_entries;
|
---|
| 1025 | dx_block->block = new_block;
|
---|
[c0964cd7] | 1026 | }
|
---|
[2f2feadb] | 1027 | }
|
---|
[38542dc] | 1028 |
|
---|
[2f2feadb] | 1029 | return EOK;
|
---|
| 1030 | }
|
---|
| 1031 |
|
---|
[dfac604] | 1032 | /** Add new entry to indexed directory
|
---|
| 1033 | *
|
---|
[38542dc] | 1034 | * @param parent Directory i-node
|
---|
| 1035 | * @param child I-node to be referenced from directory entry
|
---|
| 1036 | * @param name Name of new directory entry
|
---|
| 1037 | *
|
---|
| 1038 | * @return Error code
|
---|
| 1039 | *
|
---|
[dfac604] | 1040 | */
|
---|
[1ac1ab4] | 1041 | int ext4_directory_dx_add_entry(ext4_inode_ref_t *parent,
|
---|
[38542dc] | 1042 | ext4_inode_ref_t *child, const char *name)
|
---|
[2f2feadb] | 1043 | {
|
---|
| 1044 | int rc2 = EOK;
|
---|
[38542dc] | 1045 |
|
---|
| 1046 | /* Get direct block 0 (index root) */
|
---|
[2f2feadb] | 1047 | uint32_t root_block_addr;
|
---|
[38542dc] | 1048 | int rc = ext4_filesystem_get_inode_data_block_index(parent, 0,
|
---|
| 1049 | &root_block_addr);
|
---|
| 1050 | if (rc != EOK)
|
---|
[2f2feadb] | 1051 | return rc;
|
---|
[38542dc] | 1052 |
|
---|
[1ac1ab4] | 1053 | ext4_filesystem_t *fs = parent->fs;
|
---|
[38542dc] | 1054 |
|
---|
[2f2feadb] | 1055 | block_t *root_block;
|
---|
[38542dc] | 1056 | rc = block_get(&root_block, fs->device, root_block_addr,
|
---|
| 1057 | BLOCK_FLAGS_NONE);
|
---|
| 1058 | if (rc != EOK)
|
---|
[2f2feadb] | 1059 | return rc;
|
---|
[38542dc] | 1060 |
|
---|
[06d85e5] | 1061 | /* Initialize hinfo structure (mainly compute hash) */
|
---|
[38542dc] | 1062 | uint32_t name_len = str_size(name);
|
---|
[2f2feadb] | 1063 | ext4_hash_info_t hinfo;
|
---|
[38542dc] | 1064 | rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock,
|
---|
| 1065 | name_len, name);
|
---|
[2f2feadb] | 1066 | if (rc != EOK) {
|
---|
| 1067 | block_put(root_block);
|
---|
| 1068 | return EXT4_ERR_BAD_DX_DIR;
|
---|
| 1069 | }
|
---|
[38542dc] | 1070 |
|
---|
| 1071 | /*
|
---|
| 1072 | * Hardcoded number 2 means maximum height of index
|
---|
| 1073 | * tree defined in Linux.
|
---|
| 1074 | */
|
---|
[2f2feadb] | 1075 | ext4_directory_dx_block_t dx_blocks[2];
|
---|
[38542dc] | 1076 | ext4_directory_dx_block_t *dx_block;
|
---|
| 1077 | ext4_directory_dx_block_t *dx_it;
|
---|
| 1078 |
|
---|
| 1079 | rc = ext4_directory_dx_get_leaf(&hinfo, parent, root_block,
|
---|
| 1080 | &dx_block, dx_blocks);
|
---|
[2f2feadb] | 1081 | if (rc != EOK) {
|
---|
| 1082 | rc = EXT4_ERR_BAD_DX_DIR;
|
---|
| 1083 | goto release_index;
|
---|
| 1084 | }
|
---|
[38542dc] | 1085 |
|
---|
[06d85e5] | 1086 | /* Try to insert to existing data block */
|
---|
[38542dc] | 1087 | uint32_t leaf_block_idx =
|
---|
| 1088 | ext4_directory_dx_entry_get_block(dx_block->position);
|
---|
[2f2feadb] | 1089 | uint32_t leaf_block_addr;
|
---|
[38542dc] | 1090 | rc = ext4_filesystem_get_inode_data_block_index(parent, leaf_block_idx,
|
---|
| 1091 | &leaf_block_addr);
|
---|
| 1092 | if (rc != EOK)
|
---|
| 1093 | goto release_index;
|
---|
| 1094 |
|
---|
| 1095 | block_t *target_block;
|
---|
| 1096 | rc = block_get(&target_block, fs->device, leaf_block_addr,
|
---|
| 1097 | BLOCK_FLAGS_NONE);
|
---|
| 1098 | if (rc != EOK)
|
---|
| 1099 | goto release_index;
|
---|
| 1100 |
|
---|
| 1101 | /* Check if insert operation passed */
|
---|
| 1102 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child,
|
---|
| 1103 | name, name_len);
|
---|
| 1104 | if (rc == EOK)
|
---|
| 1105 | goto release_target_index;
|
---|
| 1106 |
|
---|
| 1107 | /*
|
---|
| 1108 | * Check if there is needed to split index node
|
---|
| 1109 | * (and recursively also parent nodes)
|
---|
| 1110 | */
|
---|
[dfac604] | 1111 | rc = ext4_directory_dx_split_index(parent, dx_blocks, dx_block);
|
---|
[38542dc] | 1112 | if (rc != EOK)
|
---|
[2f2feadb] | 1113 | goto release_target_index;
|
---|
[38542dc] | 1114 |
|
---|
[06d85e5] | 1115 | /* Split entries to two blocks (includes sorting by hash value) */
|
---|
[c4f318d6] | 1116 | block_t *new_block = NULL;
|
---|
[38542dc] | 1117 | rc = ext4_directory_dx_split_data(parent, &hinfo, target_block,
|
---|
| 1118 | dx_block, &new_block);
|
---|
[c4f318d6] | 1119 | if (rc != EOK) {
|
---|
[e63ce679] | 1120 | rc2 = rc;
|
---|
| 1121 | goto release_target_index;
|
---|
[c4f318d6] | 1122 | }
|
---|
[38542dc] | 1123 |
|
---|
[06d85e5] | 1124 | /* Where to save new entry */
|
---|
[38542dc] | 1125 | uint32_t new_block_hash =
|
---|
| 1126 | ext4_directory_dx_entry_get_hash(dx_block->position + 1);
|
---|
| 1127 | if (hinfo.hash >= new_block_hash)
|
---|
| 1128 | rc = ext4_directory_try_insert_entry(fs->superblock, new_block,
|
---|
| 1129 | child, name, name_len);
|
---|
| 1130 | else
|
---|
| 1131 | rc = ext4_directory_try_insert_entry(fs->superblock, target_block,
|
---|
| 1132 | child, name, name_len);
|
---|
| 1133 |
|
---|
[06d85e5] | 1134 | /* Cleanup */
|
---|
[d0d7afb] | 1135 | rc = block_put(new_block);
|
---|
[38542dc] | 1136 | if (rc != EOK)
|
---|
[60b8b99] | 1137 | return rc;
|
---|
[38542dc] | 1138 |
|
---|
[06d85e5] | 1139 | /* Cleanup operations */
|
---|
[38542dc] | 1140 |
|
---|
[e63ce679] | 1141 | release_target_index:
|
---|
[d0d7afb] | 1142 | rc2 = rc;
|
---|
[38542dc] | 1143 |
|
---|
[412f813] | 1144 | rc = block_put(target_block);
|
---|
[38542dc] | 1145 | if (rc != EOK)
|
---|
[60b8b99] | 1146 | return rc;
|
---|
[38542dc] | 1147 |
|
---|
[e63ce679] | 1148 | release_index:
|
---|
[38542dc] | 1149 | if (rc != EOK)
|
---|
[60b8b99] | 1150 | rc2 = rc;
|
---|
[38542dc] | 1151 |
|
---|
[e63ce679] | 1152 | dx_it = dx_blocks;
|
---|
[38542dc] | 1153 |
|
---|
[c4f318d6] | 1154 | while (dx_it <= dx_block) {
|
---|
[412f813] | 1155 | rc = block_put(dx_it->block);
|
---|
[38542dc] | 1156 | if (rc != EOK)
|
---|
[60b8b99] | 1157 | return rc;
|
---|
[38542dc] | 1158 |
|
---|
[c4f318d6] | 1159 | dx_it++;
|
---|
| 1160 | }
|
---|
[38542dc] | 1161 |
|
---|
[d0d7afb] | 1162 | return rc2;
|
---|
[565b6ff] | 1163 | }
|
---|
[0dc91833] | 1164 |
|
---|
| 1165 | /**
|
---|
| 1166 | * @}
|
---|
[38542dc] | 1167 | */
|
---|