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