| 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.c
|
|---|
| 35 | * @brief Ext4 directory structure operations.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <byteorder.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <malloc.h>
|
|---|
| 41 | #include <string.h>
|
|---|
| 42 | #include "libext4.h"
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | /** Get i-node number from directory entry.
|
|---|
| 46 | *
|
|---|
| 47 | * @param de directory entry
|
|---|
| 48 | * @return i-node number
|
|---|
| 49 | */
|
|---|
| 50 | uint32_t ext4_directory_entry_ll_get_inode(ext4_directory_entry_ll_t *de)
|
|---|
| 51 | {
|
|---|
| 52 | return uint32_t_le2host(de->inode);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /** Set i-node number to directory entry.
|
|---|
| 56 | *
|
|---|
| 57 | * @param de directory entry
|
|---|
| 58 | * @param inode i-node number
|
|---|
| 59 | */
|
|---|
| 60 | void ext4_directory_entry_ll_set_inode(ext4_directory_entry_ll_t *de,
|
|---|
| 61 | uint32_t inode)
|
|---|
| 62 | {
|
|---|
| 63 | de->inode = host2uint32_t_le(inode);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /** Get directory entry length.
|
|---|
| 67 | *
|
|---|
| 68 | * @param de directory entry
|
|---|
| 69 | * @return entry length
|
|---|
| 70 | */
|
|---|
| 71 | uint16_t ext4_directory_entry_ll_get_entry_length(
|
|---|
| 72 | ext4_directory_entry_ll_t *de)
|
|---|
| 73 | {
|
|---|
| 74 | return uint16_t_le2host(de->entry_length);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Set directory entry length.
|
|---|
| 78 | *
|
|---|
| 79 | * @param de directory entry
|
|---|
| 80 | * @param length entry length
|
|---|
| 81 | */
|
|---|
| 82 |
|
|---|
| 83 | void ext4_directory_entry_ll_set_entry_length(ext4_directory_entry_ll_t *de,
|
|---|
| 84 | uint16_t length)
|
|---|
| 85 | {
|
|---|
| 86 | de->entry_length = host2uint16_t_le(length);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /** Get directory entry name length.
|
|---|
| 90 | *
|
|---|
| 91 | * @param sb superblock
|
|---|
| 92 | * @param de directory entry
|
|---|
| 93 | * @return entry name length
|
|---|
| 94 | */
|
|---|
| 95 | uint16_t ext4_directory_entry_ll_get_name_length(
|
|---|
| 96 | ext4_superblock_t *sb, ext4_directory_entry_ll_t *de)
|
|---|
| 97 | {
|
|---|
| 98 | if (ext4_superblock_get_rev_level(sb) == 0 &&
|
|---|
| 99 | ext4_superblock_get_minor_rev_level(sb) < 5) {
|
|---|
| 100 |
|
|---|
| 101 | return ((uint16_t)de->name_length_high) << 8 |
|
|---|
| 102 | ((uint16_t)de->name_length);
|
|---|
| 103 |
|
|---|
| 104 | }
|
|---|
| 105 | return de->name_length;
|
|---|
| 106 |
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** Set directory entry name length.
|
|---|
| 110 | *
|
|---|
| 111 | * @param sb superblock
|
|---|
| 112 | * @param de directory entry
|
|---|
| 113 | * @param length entry name length
|
|---|
| 114 | */
|
|---|
| 115 | void ext4_directory_entry_ll_set_name_length(ext4_superblock_t *sb,
|
|---|
| 116 | ext4_directory_entry_ll_t *de, uint16_t length)
|
|---|
| 117 | {
|
|---|
| 118 | de->name_length = (length << 8) >> 8;
|
|---|
| 119 |
|
|---|
| 120 | if (ext4_superblock_get_rev_level(sb) == 0 &&
|
|---|
| 121 | ext4_superblock_get_minor_rev_level(sb) < 5) {
|
|---|
| 122 |
|
|---|
| 123 | de->name_length_high = length >> 8;
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /** Get i-node type of directory entry.
|
|---|
| 128 | *
|
|---|
| 129 | * @param sb superblock
|
|---|
| 130 | * @param de directory entry
|
|---|
| 131 | * @return i-node type (file, dir, etc.)
|
|---|
| 132 | */
|
|---|
| 133 | uint8_t ext4_directory_entry_ll_get_inode_type(
|
|---|
| 134 | ext4_superblock_t *sb, ext4_directory_entry_ll_t *de)
|
|---|
| 135 | {
|
|---|
| 136 | if (ext4_superblock_get_rev_level(sb) > 0 ||
|
|---|
| 137 | ext4_superblock_get_minor_rev_level(sb) >= 5) {
|
|---|
| 138 |
|
|---|
| 139 | return de->inode_type;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | return EXT4_DIRECTORY_FILETYPE_UNKNOWN;
|
|---|
| 143 |
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /** Set i-node type of directory entry.
|
|---|
| 147 | *
|
|---|
| 148 | * @param sb superblock
|
|---|
| 149 | * @param de directory entry
|
|---|
| 150 | * @param type i-node type (file, dir, etc.)
|
|---|
| 151 | */
|
|---|
| 152 | void ext4_directory_entry_ll_set_inode_type(
|
|---|
| 153 | ext4_superblock_t *sb, ext4_directory_entry_ll_t *de, uint8_t type)
|
|---|
| 154 | {
|
|---|
| 155 | if (ext4_superblock_get_rev_level(sb) > 0 ||
|
|---|
| 156 | ext4_superblock_get_minor_rev_level(sb) >= 5) {
|
|---|
| 157 |
|
|---|
| 158 | de->inode_type = type;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /* else do nothing */
|
|---|
| 162 |
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | static int ext4_directory_iterator_seek(
|
|---|
| 166 | ext4_directory_iterator_t *, aoff64_t);
|
|---|
| 167 | static int ext4_directory_iterator_set(
|
|---|
| 168 | ext4_directory_iterator_t *, uint32_t);
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | /** Initialize directory iterator.
|
|---|
| 172 | *
|
|---|
| 173 | * Set position to the first valid entry from the required position.
|
|---|
| 174 | *
|
|---|
| 175 | * @param it pointer to iterator to be initialized
|
|---|
| 176 | * @param inode_ref directory i-node
|
|---|
| 177 | * @param pos position to start reading entries from
|
|---|
| 178 | * @return error code
|
|---|
| 179 | */
|
|---|
| 180 | int ext4_directory_iterator_init(ext4_directory_iterator_t *it,
|
|---|
| 181 | ext4_inode_ref_t *inode_ref, aoff64_t pos)
|
|---|
| 182 | {
|
|---|
| 183 | it->inode_ref = inode_ref;
|
|---|
| 184 | it->current = NULL;
|
|---|
| 185 | it->current_offset = 0;
|
|---|
| 186 | it->current_block = NULL;
|
|---|
| 187 |
|
|---|
| 188 | return ext4_directory_iterator_seek(it, pos);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /** Jump to the next valid entry
|
|---|
| 192 | *
|
|---|
| 193 | * @param it initialized iterator
|
|---|
| 194 | * @return error code
|
|---|
| 195 | */
|
|---|
| 196 | int ext4_directory_iterator_next(ext4_directory_iterator_t *it)
|
|---|
| 197 | {
|
|---|
| 198 | uint16_t skip;
|
|---|
| 199 |
|
|---|
| 200 | assert(it->current != NULL);
|
|---|
| 201 |
|
|---|
| 202 | skip = ext4_directory_entry_ll_get_entry_length(it->current);
|
|---|
| 203 |
|
|---|
| 204 | return ext4_directory_iterator_seek(it, it->current_offset + skip);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /** Seek to next valid directory entry.
|
|---|
| 208 | *
|
|---|
| 209 | * Here can be jumped to the next data block.
|
|---|
| 210 | *
|
|---|
| 211 | * @param it initialized iterator
|
|---|
| 212 | * @param pos position of the next entry
|
|---|
| 213 | * @return error code
|
|---|
| 214 | */
|
|---|
| 215 | int ext4_directory_iterator_seek(ext4_directory_iterator_t *it, aoff64_t pos)
|
|---|
| 216 | {
|
|---|
| 217 | int rc;
|
|---|
| 218 |
|
|---|
| 219 | uint64_t size = ext4_inode_get_size(
|
|---|
| 220 | it->inode_ref->fs->superblock, it->inode_ref->inode);
|
|---|
| 221 |
|
|---|
| 222 | /* The iterator is not valid until we seek to the desired position */
|
|---|
| 223 | it->current = NULL;
|
|---|
| 224 |
|
|---|
| 225 | /* Are we at the end? */
|
|---|
| 226 | if (pos >= size) {
|
|---|
| 227 | if (it->current_block) {
|
|---|
| 228 | rc = block_put(it->current_block);
|
|---|
| 229 | it->current_block = NULL;
|
|---|
| 230 | if (rc != EOK) {
|
|---|
| 231 | return rc;
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | it->current_offset = pos;
|
|---|
| 236 | return EOK;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /* Compute next block address */
|
|---|
| 240 | uint32_t block_size = ext4_superblock_get_block_size(
|
|---|
| 241 | it->inode_ref->fs->superblock);
|
|---|
| 242 | aoff64_t current_block_idx = it->current_offset / block_size;
|
|---|
| 243 | aoff64_t next_block_idx = pos / block_size;
|
|---|
| 244 |
|
|---|
| 245 | /* If we don't have a block or are moving accross block boundary,
|
|---|
| 246 | * we need to get another block
|
|---|
| 247 | */
|
|---|
| 248 | if (it->current_block == NULL || current_block_idx != next_block_idx) {
|
|---|
| 249 | if (it->current_block) {
|
|---|
| 250 | rc = block_put(it->current_block);
|
|---|
| 251 | it->current_block = NULL;
|
|---|
| 252 | if (rc != EOK) {
|
|---|
| 253 | return rc;
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | uint32_t next_block_phys_idx;
|
|---|
| 258 | rc = ext4_filesystem_get_inode_data_block_index(it->inode_ref,
|
|---|
| 259 | next_block_idx, &next_block_phys_idx);
|
|---|
| 260 | if (rc != EOK) {
|
|---|
| 261 | return rc;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | rc = block_get(&it->current_block, it->inode_ref->fs->device,
|
|---|
| 265 | next_block_phys_idx, BLOCK_FLAGS_NONE);
|
|---|
| 266 | if (rc != EOK) {
|
|---|
| 267 | it->current_block = NULL;
|
|---|
| 268 | return rc;
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | it->current_offset = pos;
|
|---|
| 273 |
|
|---|
| 274 | return ext4_directory_iterator_set(it, block_size);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /** Do some checks before returning iterator.
|
|---|
| 278 | *
|
|---|
| 279 | * @param it iterator to be checked
|
|---|
| 280 | * @param block_size size of data block
|
|---|
| 281 | * @return error code
|
|---|
| 282 | */
|
|---|
| 283 | static int ext4_directory_iterator_set(ext4_directory_iterator_t *it,
|
|---|
| 284 | uint32_t block_size)
|
|---|
| 285 | {
|
|---|
| 286 |
|
|---|
| 287 | it->current = NULL;
|
|---|
| 288 |
|
|---|
| 289 | uint32_t offset_in_block = it->current_offset % block_size;
|
|---|
| 290 |
|
|---|
| 291 | /* Ensure proper alignment */
|
|---|
| 292 | if ((offset_in_block % 4) != 0) {
|
|---|
| 293 | return EIO;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /* Ensure that the core of the entry does not overflow the block */
|
|---|
| 297 | if (offset_in_block > block_size - 8) {
|
|---|
| 298 | return EIO;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | ext4_directory_entry_ll_t *entry = it->current_block->data + offset_in_block;
|
|---|
| 302 |
|
|---|
| 303 | /* Ensure that the whole entry does not overflow the block */
|
|---|
| 304 | uint16_t length = ext4_directory_entry_ll_get_entry_length(entry);
|
|---|
| 305 | if (offset_in_block + length > block_size) {
|
|---|
| 306 | return EIO;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | /* Ensure the name length is not too large */
|
|---|
| 310 | if (ext4_directory_entry_ll_get_name_length(it->inode_ref->fs->superblock,
|
|---|
| 311 | entry) > length-8) {
|
|---|
| 312 | return EIO;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /* Everything OK - "publish" the entry */
|
|---|
| 316 | it->current = entry;
|
|---|
| 317 | return EOK;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 | /** Uninitialize directory iterator.
|
|---|
| 322 | *
|
|---|
| 323 | * Release all allocated structures.
|
|---|
| 324 | *
|
|---|
| 325 | * @param it iterator to be finished
|
|---|
| 326 | * @return error code
|
|---|
| 327 | */
|
|---|
| 328 | int ext4_directory_iterator_fini(ext4_directory_iterator_t *it)
|
|---|
| 329 | {
|
|---|
| 330 | int rc;
|
|---|
| 331 |
|
|---|
| 332 | it->inode_ref = NULL;
|
|---|
| 333 | it->current = NULL;
|
|---|
| 334 |
|
|---|
| 335 | if (it->current_block) {
|
|---|
| 336 | rc = block_put(it->current_block);
|
|---|
| 337 | if (rc != EOK) {
|
|---|
| 338 | return rc;
|
|---|
| 339 | }
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | return EOK;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | /** Write directory entry to concrete data block.
|
|---|
| 346 | *
|
|---|
| 347 | * @param sb superblock
|
|---|
| 348 | * @param entry pointer to entry to be written
|
|---|
| 349 | * @param entry_len lenght of new entry
|
|---|
| 350 | * @param child child i-node to be written to new entry
|
|---|
| 351 | * @param name name of the new entry
|
|---|
| 352 | * @param name_len length of entry name
|
|---|
| 353 | */
|
|---|
| 354 | void ext4_directory_write_entry(ext4_superblock_t *sb,
|
|---|
| 355 | ext4_directory_entry_ll_t *entry, uint16_t entry_len,
|
|---|
| 356 | ext4_inode_ref_t *child, const char *name, size_t name_len)
|
|---|
| 357 | {
|
|---|
| 358 |
|
|---|
| 359 | /* Check maximum entry length */
|
|---|
| 360 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 361 | assert(entry_len <= block_size);
|
|---|
| 362 |
|
|---|
| 363 | /* Set basic attributes */
|
|---|
| 364 | ext4_directory_entry_ll_set_inode(entry, child->index);
|
|---|
| 365 | ext4_directory_entry_ll_set_entry_length(entry, entry_len);
|
|---|
| 366 | ext4_directory_entry_ll_set_name_length(sb, entry, name_len);
|
|---|
| 367 |
|
|---|
| 368 | /* Write name */
|
|---|
| 369 | memcpy(entry->name, name, name_len);
|
|---|
| 370 |
|
|---|
| 371 | /* Set type of entry */
|
|---|
| 372 | if (ext4_inode_is_type(sb, child->inode, EXT4_INODE_MODE_DIRECTORY)) {
|
|---|
| 373 | ext4_directory_entry_ll_set_inode_type(
|
|---|
| 374 | sb, entry, EXT4_DIRECTORY_FILETYPE_DIR);
|
|---|
| 375 | } else {
|
|---|
| 376 | ext4_directory_entry_ll_set_inode_type(
|
|---|
| 377 | sb, entry, EXT4_DIRECTORY_FILETYPE_REG_FILE);
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | /** Add new entry to the directory.
|
|---|
| 383 | *
|
|---|
| 384 | * @param parent directory i-node
|
|---|
| 385 | * @param name name of new entry
|
|---|
| 386 | * @param child i-node to be referenced from new entry
|
|---|
| 387 | * @return error code
|
|---|
| 388 | */
|
|---|
| 389 | int ext4_directory_add_entry(ext4_inode_ref_t * parent,
|
|---|
| 390 | const char *name, ext4_inode_ref_t *child)
|
|---|
| 391 | {
|
|---|
| 392 | int rc;
|
|---|
| 393 |
|
|---|
| 394 | ext4_filesystem_t *fs = parent->fs;
|
|---|
| 395 |
|
|---|
| 396 | /* Index adding (if allowed) */
|
|---|
| 397 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
|
|---|
| 398 | ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX)) {
|
|---|
| 399 |
|
|---|
| 400 | rc = ext4_directory_dx_add_entry(parent, child, name);
|
|---|
| 401 |
|
|---|
| 402 | /* Check if index is not corrupted */
|
|---|
| 403 | if (rc != EXT4_ERR_BAD_DX_DIR) {
|
|---|
| 404 |
|
|---|
| 405 | if (rc != EOK) {
|
|---|
| 406 | return rc;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | return EOK;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | /* Needed to clear dir index flag if corrupted */
|
|---|
| 413 | ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
|
|---|
| 414 | parent->dirty = true;
|
|---|
| 415 |
|
|---|
| 416 | EXT4FS_DBG("index is corrupted - doing linear algorithm, index flag cleared");
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | /* Linear algorithm */
|
|---|
| 420 |
|
|---|
| 421 | uint32_t iblock = 0, fblock = 0;
|
|---|
| 422 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
|---|
| 423 | uint32_t inode_size = ext4_inode_get_size(fs->superblock, parent->inode);
|
|---|
| 424 | uint32_t total_blocks = inode_size / block_size;
|
|---|
| 425 |
|
|---|
| 426 | uint32_t name_len = strlen(name);
|
|---|
| 427 |
|
|---|
| 428 | /* Find block, where is space for new entry and try to add */
|
|---|
| 429 | bool success = false;
|
|---|
| 430 | for (iblock = 0; iblock < total_blocks; ++iblock) {
|
|---|
| 431 |
|
|---|
| 432 | rc = ext4_filesystem_get_inode_data_block_index(parent, iblock, &fblock);
|
|---|
| 433 | if (rc != EOK) {
|
|---|
| 434 | return rc;
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | block_t *block;
|
|---|
| 438 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
|---|
| 439 | if (rc != EOK) {
|
|---|
| 440 | return rc;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | /* If adding is successful, function can finish */
|
|---|
| 444 | rc = ext4_directory_try_insert_entry(fs->superblock, block, child, name, name_len);
|
|---|
| 445 | if (rc == EOK) {
|
|---|
| 446 | success = true;
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | rc = block_put(block);
|
|---|
| 450 | if (rc != EOK) {
|
|---|
| 451 | return rc;
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | if (success) {
|
|---|
| 455 | return EOK;
|
|---|
| 456 | }
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | /* No free block found - needed to allocate next data block */
|
|---|
| 460 |
|
|---|
| 461 | iblock = 0;
|
|---|
| 462 | fblock = 0;
|
|---|
| 463 | rc = ext4_filesystem_append_inode_block(parent, &fblock, &iblock);
|
|---|
| 464 | if (rc != EOK) {
|
|---|
| 465 | return rc;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | /* Load new block */
|
|---|
| 469 | block_t *new_block;
|
|---|
| 470 | rc = block_get(&new_block, fs->device, fblock, BLOCK_FLAGS_NOREAD);
|
|---|
| 471 | if (rc != EOK) {
|
|---|
| 472 | return rc;
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | /* Fill block with zeroes */
|
|---|
| 476 | memset(new_block->data, 0, block_size);
|
|---|
| 477 | ext4_directory_entry_ll_t *block_entry = new_block->data;
|
|---|
| 478 | ext4_directory_write_entry(fs->superblock, block_entry, block_size, child, name, name_len);
|
|---|
| 479 |
|
|---|
| 480 | /* Save new block */
|
|---|
| 481 | new_block->dirty = true;
|
|---|
| 482 | rc = block_put(new_block);
|
|---|
| 483 | if (rc != EOK) {
|
|---|
| 484 | return rc;
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | return EOK;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | /** Find directory entry with passed name.
|
|---|
| 491 | *
|
|---|
| 492 | * @param result result structure to be returned if entry found
|
|---|
| 493 | * @param parent directory i-node
|
|---|
| 494 | * @param name name of entry to be found
|
|---|
| 495 | * @return error code
|
|---|
| 496 | */
|
|---|
| 497 | int ext4_directory_find_entry(ext4_directory_search_result_t *result,
|
|---|
| 498 | ext4_inode_ref_t *parent, const char *name)
|
|---|
| 499 | {
|
|---|
| 500 | int rc;
|
|---|
| 501 | uint32_t name_len = strlen(name);
|
|---|
| 502 |
|
|---|
| 503 | ext4_superblock_t *sb = parent->fs->superblock;
|
|---|
| 504 |
|
|---|
| 505 | /* Index search */
|
|---|
| 506 | if (ext4_superblock_has_feature_compatible(sb, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
|
|---|
| 507 | ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX)) {
|
|---|
| 508 |
|
|---|
| 509 | rc = ext4_directory_dx_find_entry(result, parent, name_len, name);
|
|---|
| 510 |
|
|---|
| 511 | /* Check if index is not corrupted */
|
|---|
| 512 | if (rc != EXT4_ERR_BAD_DX_DIR) {
|
|---|
| 513 |
|
|---|
| 514 | if (rc != EOK) {
|
|---|
| 515 | return rc;
|
|---|
| 516 | }
|
|---|
| 517 | return EOK;
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | /* Needed to clear dir index flag if corrupted */
|
|---|
| 521 | ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
|
|---|
| 522 | parent->dirty = true;
|
|---|
| 523 |
|
|---|
| 524 | EXT4FS_DBG("index is corrupted - doing linear search");
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | /* Linear algorithm */
|
|---|
| 528 |
|
|---|
| 529 | uint32_t iblock, fblock;
|
|---|
| 530 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 531 | uint32_t inode_size = ext4_inode_get_size(sb, parent->inode);
|
|---|
| 532 | uint32_t total_blocks = inode_size / block_size;
|
|---|
| 533 |
|
|---|
| 534 | /* Walk through all data blocks */
|
|---|
| 535 | for (iblock = 0; iblock < total_blocks; ++iblock) {
|
|---|
| 536 |
|
|---|
| 537 | /* Load block address */
|
|---|
| 538 | rc = ext4_filesystem_get_inode_data_block_index(parent, iblock, &fblock);
|
|---|
| 539 | if (rc != EOK) {
|
|---|
| 540 | return rc;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | /* Load data block */
|
|---|
| 544 | block_t *block;
|
|---|
| 545 | rc = block_get(&block, parent->fs->device, fblock, BLOCK_FLAGS_NONE);
|
|---|
| 546 | if (rc != EOK) {
|
|---|
| 547 | return rc;
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | /* Try to find entry in block */
|
|---|
| 551 | ext4_directory_entry_ll_t *res_entry;
|
|---|
| 552 | rc = ext4_directory_find_in_block(block, sb, name_len, name, &res_entry);
|
|---|
| 553 | if (rc == EOK) {
|
|---|
| 554 | result->block = block;
|
|---|
| 555 | result->dentry = res_entry;
|
|---|
| 556 | return EOK;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | /* Entry not found - put block and continue to the next block */
|
|---|
| 560 |
|
|---|
| 561 | rc = block_put(block);
|
|---|
| 562 | if (rc != EOK) {
|
|---|
| 563 | return rc;
|
|---|
| 564 | }
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | /* Entry was not found */
|
|---|
| 568 |
|
|---|
| 569 | result->block = NULL;
|
|---|
| 570 | result->dentry = NULL;
|
|---|
| 571 |
|
|---|
| 572 | return ENOENT;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 | /** Remove directory entry.
|
|---|
| 577 | *
|
|---|
| 578 | * @param parent directory i-node
|
|---|
| 579 | * @param name name of the entry to be removed
|
|---|
| 580 | * @return error code
|
|---|
| 581 | */
|
|---|
| 582 | int ext4_directory_remove_entry(ext4_inode_ref_t *parent, const char *name)
|
|---|
| 583 | {
|
|---|
| 584 | int rc;
|
|---|
| 585 |
|
|---|
| 586 | /* Check if removing from directory */
|
|---|
| 587 | if (!ext4_inode_is_type(parent->fs->superblock, parent->inode,
|
|---|
| 588 | EXT4_INODE_MODE_DIRECTORY)) {
|
|---|
| 589 | return ENOTDIR;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | /* Try to find entry */
|
|---|
| 593 | ext4_directory_search_result_t result;
|
|---|
| 594 | rc = ext4_directory_find_entry(&result, parent, name);
|
|---|
| 595 | if (rc != EOK) {
|
|---|
| 596 | return rc;
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | /* Invalidate entry */
|
|---|
| 600 | ext4_directory_entry_ll_set_inode(result.dentry, 0);
|
|---|
| 601 |
|
|---|
| 602 | /* Store entry position in block */
|
|---|
| 603 | uint32_t pos = (void *)result.dentry - result.block->data;
|
|---|
| 604 |
|
|---|
| 605 | /* If entry is not the first in block, it must be merged
|
|---|
| 606 | * with previous entry
|
|---|
| 607 | */
|
|---|
| 608 | if (pos != 0) {
|
|---|
| 609 |
|
|---|
| 610 | uint32_t offset = 0;
|
|---|
| 611 |
|
|---|
| 612 | /* Start from the first entry in block */
|
|---|
| 613 | ext4_directory_entry_ll_t *tmp_dentry = result.block->data;
|
|---|
| 614 | uint16_t tmp_dentry_length =
|
|---|
| 615 | ext4_directory_entry_ll_get_entry_length(tmp_dentry);
|
|---|
| 616 |
|
|---|
| 617 | /* Find direct predecessor of removed entry */
|
|---|
| 618 | while ((offset + tmp_dentry_length) < pos) {
|
|---|
| 619 | offset += ext4_directory_entry_ll_get_entry_length(tmp_dentry);
|
|---|
| 620 | tmp_dentry = result.block->data + offset;
|
|---|
| 621 | tmp_dentry_length =
|
|---|
| 622 | ext4_directory_entry_ll_get_entry_length(tmp_dentry);
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | assert(tmp_dentry_length + offset == pos);
|
|---|
| 626 |
|
|---|
| 627 | /* Add to removed entry length to predecessor's length */
|
|---|
| 628 | uint16_t del_entry_length =
|
|---|
| 629 | ext4_directory_entry_ll_get_entry_length(result.dentry);
|
|---|
| 630 | ext4_directory_entry_ll_set_entry_length(tmp_dentry,
|
|---|
| 631 | tmp_dentry_length + del_entry_length);
|
|---|
| 632 |
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | result.block->dirty = true;
|
|---|
| 636 |
|
|---|
| 637 | return ext4_directory_destroy_result(&result);
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | /** Try to insert entry to concrete data block.
|
|---|
| 641 | *
|
|---|
| 642 | * @param sb superblock
|
|---|
| 643 | * @param target_block block to try to insert entry to
|
|---|
| 644 | * @param child child i-node to be inserted by new entry
|
|---|
| 645 | * @param name name of the new entry
|
|---|
| 646 | * @param name_len length of the new entry name
|
|---|
| 647 | * @return error code
|
|---|
| 648 | */
|
|---|
| 649 | int ext4_directory_try_insert_entry(ext4_superblock_t *sb,
|
|---|
| 650 | block_t *target_block, ext4_inode_ref_t *child,
|
|---|
| 651 | const char *name, uint32_t name_len)
|
|---|
| 652 | {
|
|---|
| 653 | /* Compute required length entry and align it to 4 bytes */
|
|---|
| 654 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 655 | uint16_t required_len = sizeof(ext4_fake_directory_entry_t) + name_len;
|
|---|
| 656 | if ((required_len % 4) != 0) {
|
|---|
| 657 | required_len += 4 - (required_len % 4);
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | /* Initialize pointers, stop means to upper bound */
|
|---|
| 661 | ext4_directory_entry_ll_t *dentry = target_block->data;
|
|---|
| 662 | ext4_directory_entry_ll_t *stop = target_block->data + block_size;
|
|---|
| 663 |
|
|---|
| 664 | /* Walk through the block and check for invalid entries
|
|---|
| 665 | * or entries with free space for new entry
|
|---|
| 666 | */
|
|---|
| 667 | while (dentry < stop) {
|
|---|
| 668 |
|
|---|
| 669 | uint32_t inode = ext4_directory_entry_ll_get_inode(dentry);
|
|---|
| 670 | uint16_t rec_len = ext4_directory_entry_ll_get_entry_length(dentry);
|
|---|
| 671 |
|
|---|
| 672 | /* If invalid and large enough entry, use it */
|
|---|
| 673 | if ((inode == 0) && (rec_len >= required_len)) {
|
|---|
| 674 | ext4_directory_write_entry(sb, dentry, rec_len, child, name, name_len);
|
|---|
| 675 | target_block->dirty = true;
|
|---|
| 676 | return EOK;
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | /* Valid entry, try to split it */
|
|---|
| 680 | if (inode != 0) {
|
|---|
| 681 | uint16_t used_name_len =
|
|---|
| 682 | ext4_directory_entry_ll_get_name_length(sb, dentry);
|
|---|
| 683 |
|
|---|
| 684 | uint16_t used_space =
|
|---|
| 685 | sizeof(ext4_fake_directory_entry_t) + used_name_len;
|
|---|
| 686 | if ((used_name_len % 4) != 0) {
|
|---|
| 687 | used_space += 4 - (used_name_len % 4);
|
|---|
| 688 | }
|
|---|
| 689 | uint16_t free_space = rec_len - used_space;
|
|---|
| 690 |
|
|---|
| 691 | /* There is free space for new entry */
|
|---|
| 692 | if (free_space >= required_len) {
|
|---|
| 693 |
|
|---|
| 694 | /* Cut tail of current entry */
|
|---|
| 695 | ext4_directory_entry_ll_set_entry_length(dentry, used_space);
|
|---|
| 696 | ext4_directory_entry_ll_t *new_entry =
|
|---|
| 697 | (void *)dentry + used_space;
|
|---|
| 698 | ext4_directory_write_entry(sb, new_entry,
|
|---|
| 699 | free_space, child, name, name_len);
|
|---|
| 700 |
|
|---|
| 701 | target_block->dirty = true;
|
|---|
| 702 | return EOK;
|
|---|
| 703 | }
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | /* Jump to the next entry */
|
|---|
| 707 | dentry = (void *)dentry + rec_len;
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | /* No free space found for new entry */
|
|---|
| 711 |
|
|---|
| 712 | return ENOSPC;
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | /** Try to find entry in block by name.
|
|---|
| 716 | *
|
|---|
| 717 | * @param block block containing entries
|
|---|
| 718 | * @param sb superblock
|
|---|
| 719 | * @param name_len length of entry name
|
|---|
| 720 | * @param name name of entry to be found
|
|---|
| 721 | * @param res_entry output pointer to found entry, NULL if not found
|
|---|
| 722 | * @return error code
|
|---|
| 723 | */
|
|---|
| 724 | int ext4_directory_find_in_block(block_t *block,
|
|---|
| 725 | ext4_superblock_t *sb, size_t name_len, const char *name,
|
|---|
| 726 | ext4_directory_entry_ll_t **res_entry)
|
|---|
| 727 | {
|
|---|
| 728 | /* Start from the first entry in block */
|
|---|
| 729 | ext4_directory_entry_ll_t *dentry = (ext4_directory_entry_ll_t *)block->data;
|
|---|
| 730 | /*Set upper bound for cycling */
|
|---|
| 731 | uint8_t *addr_limit = block->data + ext4_superblock_get_block_size(sb);
|
|---|
| 732 |
|
|---|
| 733 | /* Walk through the block and check entries */
|
|---|
| 734 | while ((uint8_t *)dentry < addr_limit) {
|
|---|
| 735 |
|
|---|
| 736 | /* Termination condition */
|
|---|
| 737 | if ((uint8_t*) dentry + name_len > addr_limit) {
|
|---|
| 738 | break;
|
|---|
| 739 | }
|
|---|
| 740 |
|
|---|
| 741 | /* Valid entry - check it */
|
|---|
| 742 | if (dentry->inode != 0) {
|
|---|
| 743 |
|
|---|
| 744 | /* For more effectivity compare firstly only lengths */
|
|---|
| 745 | if (name_len == ext4_directory_entry_ll_get_name_length(sb, dentry)) {
|
|---|
| 746 | /* Compare names */
|
|---|
| 747 | if (bcmp((uint8_t *)name, dentry->name, name_len) == 0) {
|
|---|
| 748 | *res_entry = dentry;
|
|---|
| 749 | return EOK;
|
|---|
| 750 | }
|
|---|
| 751 | }
|
|---|
| 752 | }
|
|---|
| 753 |
|
|---|
| 754 | uint16_t dentry_len = ext4_directory_entry_ll_get_entry_length(dentry);
|
|---|
| 755 |
|
|---|
| 756 | /* Corrupted entry */
|
|---|
| 757 | if (dentry_len == 0) {
|
|---|
| 758 | return EINVAL;
|
|---|
| 759 | }
|
|---|
| 760 |
|
|---|
| 761 | /* Jump to next entry */
|
|---|
| 762 | dentry = (ext4_directory_entry_ll_t *)((uint8_t *)dentry + dentry_len);
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | /* Entry not found */
|
|---|
| 766 | return ENOENT;
|
|---|
| 767 | }
|
|---|
| 768 |
|
|---|
| 769 | /** Simple function to release allocated data from result.
|
|---|
| 770 | *
|
|---|
| 771 | * @param result search result to destroy
|
|---|
| 772 | * @return error code
|
|---|
| 773 | */
|
|---|
| 774 | int ext4_directory_destroy_result(ext4_directory_search_result_t *result)
|
|---|
| 775 | {
|
|---|
| 776 | if (result->block) {
|
|---|
| 777 | return block_put(result->block);
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | return EOK;
|
|---|
| 781 | }
|
|---|
| 782 |
|
|---|
| 783 | /**
|
|---|
| 784 | * @}
|
|---|
| 785 | */
|
|---|