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