| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Martin Sucha
|
|---|
| 3 | * Copyright (c) 2012 Frantisek Princ
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libext4
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file libext4_filesystem.c
|
|---|
| 35 | * @brief More complex filesystem operations.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <byteorder.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <malloc.h>
|
|---|
| 41 | #include <ipc/vfs.h>
|
|---|
| 42 | #include <align.h>
|
|---|
| 43 | #include <crypto.h>
|
|---|
| 44 | #include "libext4.h"
|
|---|
| 45 |
|
|---|
| 46 | /** Initialize filesystem and read all needed data.
|
|---|
| 47 | *
|
|---|
| 48 | * @param fs Filesystem instance to be initialized
|
|---|
| 49 | * @param service_id Identifier if device with the filesystem
|
|---|
| 50 | *
|
|---|
| 51 | * @return Error code
|
|---|
| 52 | *
|
|---|
| 53 | */
|
|---|
| 54 | int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id,
|
|---|
| 55 | enum cache_mode cmode)
|
|---|
| 56 | {
|
|---|
| 57 | int rc;
|
|---|
| 58 | ext4_superblock_t *temp_superblock = NULL;
|
|---|
| 59 |
|
|---|
| 60 | fs->device = service_id;
|
|---|
| 61 |
|
|---|
| 62 | /* Initialize block library (4096 is size of communication channel) */
|
|---|
| 63 | rc = block_init(fs->device, 4096);
|
|---|
| 64 | if (rc != EOK)
|
|---|
| 65 | goto err;
|
|---|
| 66 |
|
|---|
| 67 | /* Read superblock from device to memory */
|
|---|
| 68 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
|---|
| 69 | if (rc != EOK)
|
|---|
| 70 | goto err_1;
|
|---|
| 71 |
|
|---|
| 72 | /* Read block size from superblock and check */
|
|---|
| 73 | uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
|
|---|
| 74 | if (block_size > EXT4_MAX_BLOCK_SIZE) {
|
|---|
| 75 | rc = ENOTSUP;
|
|---|
| 76 | goto err_1;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /* Initialize block caching by libblock */
|
|---|
| 80 | rc = block_cache_init(service_id, block_size, 0, cmode);
|
|---|
| 81 | if (rc != EOK)
|
|---|
| 82 | goto err_1;
|
|---|
| 83 |
|
|---|
| 84 | /* Compute limits for indirect block levels */
|
|---|
| 85 | uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
|
|---|
| 86 | fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
|---|
| 87 | fs->inode_blocks_per_level[0] = 1;
|
|---|
| 88 | for (unsigned int i = 1; i < 4; i++) {
|
|---|
| 89 | fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
|
|---|
| 90 | block_ids_per_block;
|
|---|
| 91 | fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
|
|---|
| 92 | fs->inode_blocks_per_level[i];
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /* Return loaded superblock */
|
|---|
| 96 | fs->superblock = temp_superblock;
|
|---|
| 97 |
|
|---|
| 98 | uint16_t state = ext4_superblock_get_state(fs->superblock);
|
|---|
| 99 |
|
|---|
| 100 | if (((state & EXT4_SUPERBLOCK_STATE_VALID_FS) !=
|
|---|
| 101 | EXT4_SUPERBLOCK_STATE_VALID_FS) ||
|
|---|
| 102 | ((state & EXT4_SUPERBLOCK_STATE_ERROR_FS) ==
|
|---|
| 103 | EXT4_SUPERBLOCK_STATE_ERROR_FS)) {
|
|---|
| 104 | rc = ENOTSUP;
|
|---|
| 105 | goto err_2;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /* Mark system as mounted */
|
|---|
| 109 | ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_ERROR_FS);
|
|---|
| 110 | rc = ext4_superblock_write_direct(fs->device, fs->superblock);
|
|---|
| 111 | if (rc != EOK)
|
|---|
| 112 | goto err_2;
|
|---|
| 113 |
|
|---|
| 114 | uint16_t mnt_count = ext4_superblock_get_mount_count(fs->superblock);
|
|---|
| 115 | ext4_superblock_set_mount_count(fs->superblock, mnt_count + 1);
|
|---|
| 116 |
|
|---|
| 117 | return EOK;
|
|---|
| 118 |
|
|---|
| 119 | err_2:
|
|---|
| 120 | block_cache_fini(fs->device);
|
|---|
| 121 | err_1:
|
|---|
| 122 | block_fini(fs->device);
|
|---|
| 123 | err:
|
|---|
| 124 | if (temp_superblock)
|
|---|
| 125 | ext4_superblock_release(temp_superblock);
|
|---|
| 126 | return rc;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** Destroy filesystem instance (used by unmount operation).
|
|---|
| 130 | *
|
|---|
| 131 | * @param fs Filesystem to be destroyed
|
|---|
| 132 | *
|
|---|
| 133 | * @return Error code
|
|---|
| 134 | *
|
|---|
| 135 | */
|
|---|
| 136 | int ext4_filesystem_fini(ext4_filesystem_t *fs)
|
|---|
| 137 | {
|
|---|
| 138 | /* Write the superblock to the device */
|
|---|
| 139 | ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_VALID_FS);
|
|---|
| 140 | int rc = ext4_superblock_write_direct(fs->device, fs->superblock);
|
|---|
| 141 |
|
|---|
| 142 | /* Release memory space for superblock */
|
|---|
| 143 | free(fs->superblock);
|
|---|
| 144 |
|
|---|
| 145 | /* Finish work with block library */
|
|---|
| 146 | block_cache_fini(fs->device);
|
|---|
| 147 | block_fini(fs->device);
|
|---|
| 148 |
|
|---|
| 149 | return rc;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /** Check sanity of the filesystem.
|
|---|
| 153 | *
|
|---|
| 154 | * Main is the check of the superblock structure.
|
|---|
| 155 | *
|
|---|
| 156 | * @param fs Filesystem to be checked
|
|---|
| 157 | *
|
|---|
| 158 | * @return Error code
|
|---|
| 159 | *
|
|---|
| 160 | */
|
|---|
| 161 | int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
|
|---|
| 162 | {
|
|---|
| 163 | /* Check superblock */
|
|---|
| 164 | return ext4_superblock_check_sanity(fs->superblock);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /** Check filesystem's features, if supported by this driver
|
|---|
| 168 | *
|
|---|
| 169 | * Function can return EOK and set read_only flag. It mean's that
|
|---|
| 170 | * there are some not-supported features, that can cause problems
|
|---|
| 171 | * during some write operations.
|
|---|
| 172 | *
|
|---|
| 173 | * @param fs Filesystem to be checked
|
|---|
| 174 | * @param read_only Flag if filesystem should be mounted only for reading
|
|---|
| 175 | *
|
|---|
| 176 | * @return Error code
|
|---|
| 177 | *
|
|---|
| 178 | */
|
|---|
| 179 | int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *read_only)
|
|---|
| 180 | {
|
|---|
| 181 | /* Feature flags are present only in higher revisions */
|
|---|
| 182 | if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
|
|---|
| 183 | *read_only = false;
|
|---|
| 184 | return EOK;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /*
|
|---|
| 188 | * Check incompatible features - if filesystem has some,
|
|---|
| 189 | * volume can't be mounted
|
|---|
| 190 | */
|
|---|
| 191 | uint32_t incompatible_features;
|
|---|
| 192 | incompatible_features =
|
|---|
| 193 | ext4_superblock_get_features_incompatible(fs->superblock);
|
|---|
| 194 | incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
|
|---|
| 195 | if (incompatible_features > 0)
|
|---|
| 196 | return ENOTSUP;
|
|---|
| 197 |
|
|---|
| 198 | /*
|
|---|
| 199 | * Check read-only features, if filesystem has some,
|
|---|
| 200 | * volume can be mount only in read-only mode
|
|---|
| 201 | */
|
|---|
| 202 | uint32_t compatible_read_only;
|
|---|
| 203 | compatible_read_only =
|
|---|
| 204 | ext4_superblock_get_features_read_only(fs->superblock);
|
|---|
| 205 | compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
|
|---|
| 206 | if (compatible_read_only > 0) {
|
|---|
| 207 | *read_only = true;
|
|---|
| 208 | return EOK;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | return EOK;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 | /** Convert block address to relative index in block group.
|
|---|
| 216 | *
|
|---|
| 217 | * @param sb Superblock pointer
|
|---|
| 218 | * @param block_addr Block number to convert
|
|---|
| 219 | *
|
|---|
| 220 | * @return Relative number of block
|
|---|
| 221 | *
|
|---|
| 222 | */
|
|---|
| 223 | uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *sb,
|
|---|
| 224 | uint32_t block_addr)
|
|---|
| 225 | {
|
|---|
| 226 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 227 | uint32_t first_block = ext4_superblock_get_first_data_block(sb);
|
|---|
| 228 |
|
|---|
| 229 | /* First block == 0 or 1 */
|
|---|
| 230 | if (first_block == 0)
|
|---|
| 231 | return block_addr % blocks_per_group;
|
|---|
| 232 | else
|
|---|
| 233 | return (block_addr - 1) % blocks_per_group;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | /** Convert relative block address in group to absolute address.
|
|---|
| 238 | *
|
|---|
| 239 | * @param sb Superblock pointer
|
|---|
| 240 | *
|
|---|
| 241 | * @return Absolute block address
|
|---|
| 242 | *
|
|---|
| 243 | */
|
|---|
| 244 | uint32_t ext4_filesystem_index_in_group2blockaddr(ext4_superblock_t *sb,
|
|---|
| 245 | uint32_t index, uint32_t bgid)
|
|---|
| 246 | {
|
|---|
| 247 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 248 |
|
|---|
| 249 | if (ext4_superblock_get_first_data_block(sb) == 0)
|
|---|
| 250 | return bgid * blocks_per_group + index;
|
|---|
| 251 | else
|
|---|
| 252 | return bgid * blocks_per_group + index + 1;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /** Convert the absolute block number to group number
|
|---|
| 256 | *
|
|---|
| 257 | * @param sb Pointer to the superblock
|
|---|
| 258 | * @param b Absolute block number
|
|---|
| 259 | *
|
|---|
| 260 | * @return Group number
|
|---|
| 261 | */
|
|---|
| 262 | uint32_t ext4_filesystem_blockaddr2group(ext4_superblock_t *sb, uint64_t b)
|
|---|
| 263 | {
|
|---|
| 264 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 265 | uint32_t first_block = ext4_superblock_get_first_data_block(sb);
|
|---|
| 266 |
|
|---|
| 267 | return (b - first_block) / blocks_per_group;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /** Initialize block bitmap in block group.
|
|---|
| 271 | *
|
|---|
| 272 | * @param bg_ref Reference to block group
|
|---|
| 273 | *
|
|---|
| 274 | * @return Error code
|
|---|
| 275 | *
|
|---|
| 276 | */
|
|---|
| 277 | static int ext4_filesystem_init_block_bitmap(ext4_block_group_ref_t *bg_ref)
|
|---|
| 278 | {
|
|---|
| 279 | uint64_t itb;
|
|---|
| 280 | uint32_t sz;
|
|---|
| 281 | uint32_t i;
|
|---|
| 282 |
|
|---|
| 283 | /* Load bitmap */
|
|---|
| 284 | ext4_superblock_t *sb = bg_ref->fs->superblock;
|
|---|
| 285 | uint64_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
|---|
| 286 | bg_ref->block_group, bg_ref->fs->superblock);
|
|---|
| 287 | uint64_t bitmap_inode_addr = ext4_block_group_get_inode_bitmap(
|
|---|
| 288 | bg_ref->block_group, bg_ref->fs->superblock);
|
|---|
| 289 |
|
|---|
| 290 | block_t *bitmap_block;
|
|---|
| 291 | int rc = block_get(&bitmap_block, bg_ref->fs->device,
|
|---|
| 292 | bitmap_block_addr, BLOCK_FLAGS_NOREAD);
|
|---|
| 293 | if (rc != EOK)
|
|---|
| 294 | return rc;
|
|---|
| 295 |
|
|---|
| 296 | uint8_t *bitmap = bitmap_block->data;
|
|---|
| 297 |
|
|---|
| 298 | /* Initialize all bitmap bits to zero */
|
|---|
| 299 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 300 | memset(bitmap, 0, block_size);
|
|---|
| 301 |
|
|---|
| 302 | /* Determine the number of reserved blocks in the group */
|
|---|
| 303 | uint32_t reserved_cnt = ext4_filesystem_bg_get_backup_blocks(bg_ref);
|
|---|
| 304 |
|
|---|
| 305 | /* Set bits from to first block to first data block - 1 to one (allocated) */
|
|---|
| 306 | for (uint32_t block = 0; block < reserved_cnt; ++block)
|
|---|
| 307 | ext4_bitmap_set_bit(bitmap, block);
|
|---|
| 308 |
|
|---|
| 309 | uint32_t bitmap_block_gid = ext4_filesystem_blockaddr2group(sb,
|
|---|
| 310 | bitmap_block_addr);
|
|---|
| 311 | if (bitmap_block_gid == bg_ref->index) {
|
|---|
| 312 | ext4_bitmap_set_bit(bitmap,
|
|---|
| 313 | ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_block_addr));
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | uint32_t bitmap_inode_gid = ext4_filesystem_blockaddr2group(sb,
|
|---|
| 317 | bitmap_inode_addr);
|
|---|
| 318 | if (bitmap_inode_gid == bg_ref->index) {
|
|---|
| 319 | ext4_bitmap_set_bit(bitmap,
|
|---|
| 320 | ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_inode_addr));
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | itb = ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
|
|---|
| 324 | sb);
|
|---|
| 325 | sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref);
|
|---|
| 326 |
|
|---|
| 327 | for (i = 0; i < sz; ++i, ++itb) {
|
|---|
| 328 | uint32_t gid = ext4_filesystem_blockaddr2group(sb, itb);
|
|---|
| 329 | if (gid == bg_ref->index) {
|
|---|
| 330 | ext4_bitmap_set_bit(bitmap,
|
|---|
| 331 | ext4_filesystem_blockaddr2_index_in_group(sb, itb));
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | bitmap_block->dirty = true;
|
|---|
| 336 |
|
|---|
| 337 | /* Save bitmap */
|
|---|
| 338 | return block_put(bitmap_block);
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /** Initialize i-node bitmap in block group.
|
|---|
| 342 | *
|
|---|
| 343 | * @param bg_ref Reference to block group
|
|---|
| 344 | *
|
|---|
| 345 | * @return Error code
|
|---|
| 346 | *
|
|---|
| 347 | */
|
|---|
| 348 | static int ext4_filesystem_init_inode_bitmap(ext4_block_group_ref_t *bg_ref)
|
|---|
| 349 | {
|
|---|
| 350 | /* Load bitmap */
|
|---|
| 351 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
|---|
| 352 | bg_ref->block_group, bg_ref->fs->superblock);
|
|---|
| 353 | block_t *bitmap_block;
|
|---|
| 354 |
|
|---|
| 355 | int rc = block_get(&bitmap_block, bg_ref->fs->device,
|
|---|
| 356 | bitmap_block_addr, BLOCK_FLAGS_NOREAD);
|
|---|
| 357 | if (rc != EOK)
|
|---|
| 358 | return rc;
|
|---|
| 359 |
|
|---|
| 360 | uint8_t *bitmap = bitmap_block->data;
|
|---|
| 361 |
|
|---|
| 362 | /* Initialize all bitmap bits to zero */
|
|---|
| 363 | uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
|
|---|
| 364 | uint32_t inodes_per_group =
|
|---|
| 365 | ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
|
|---|
| 366 | memset(bitmap, 0, (inodes_per_group + 7) / 8);
|
|---|
| 367 |
|
|---|
| 368 | uint32_t start_bit = inodes_per_group;
|
|---|
| 369 | uint32_t end_bit = block_size * 8;
|
|---|
| 370 |
|
|---|
| 371 | uint32_t i;
|
|---|
| 372 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
|
|---|
| 373 | ext4_bitmap_set_bit(bitmap, i);
|
|---|
| 374 |
|
|---|
| 375 | if (i < end_bit)
|
|---|
| 376 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
|
|---|
| 377 |
|
|---|
| 378 | bitmap_block->dirty = true;
|
|---|
| 379 |
|
|---|
| 380 | /* Save bitmap */
|
|---|
| 381 | return block_put(bitmap_block);
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /** Initialize i-node table in block group.
|
|---|
| 385 | *
|
|---|
| 386 | * @param bg_ref Reference to block group
|
|---|
| 387 | *
|
|---|
| 388 | * @return Error code
|
|---|
| 389 | *
|
|---|
| 390 | */
|
|---|
| 391 | static int ext4_filesystem_init_inode_table(ext4_block_group_ref_t *bg_ref)
|
|---|
| 392 | {
|
|---|
| 393 | ext4_superblock_t *sb = bg_ref->fs->superblock;
|
|---|
| 394 |
|
|---|
| 395 | uint32_t inode_size = ext4_superblock_get_inode_size(sb);
|
|---|
| 396 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 397 | uint32_t inodes_per_block = block_size / inode_size;
|
|---|
| 398 |
|
|---|
| 399 | uint32_t inodes_in_group =
|
|---|
| 400 | ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
|
|---|
| 401 |
|
|---|
| 402 | uint32_t table_blocks = inodes_in_group / inodes_per_block;
|
|---|
| 403 |
|
|---|
| 404 | if (inodes_in_group % inodes_per_block)
|
|---|
| 405 | table_blocks++;
|
|---|
| 406 |
|
|---|
| 407 | /* Compute initialization bounds */
|
|---|
| 408 | uint32_t first_block = ext4_block_group_get_inode_table_first_block(
|
|---|
| 409 | bg_ref->block_group, sb);
|
|---|
| 410 |
|
|---|
| 411 | uint32_t last_block = first_block + table_blocks - 1;
|
|---|
| 412 |
|
|---|
| 413 | /* Initialization of all itable blocks */
|
|---|
| 414 | for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
|
|---|
| 415 | block_t *block;
|
|---|
| 416 | int rc = block_get(&block, bg_ref->fs->device, fblock,
|
|---|
| 417 | BLOCK_FLAGS_NOREAD);
|
|---|
| 418 | if (rc != EOK)
|
|---|
| 419 | return rc;
|
|---|
| 420 |
|
|---|
| 421 | memset(block->data, 0, block_size);
|
|---|
| 422 | block->dirty = true;
|
|---|
| 423 |
|
|---|
| 424 | rc = block_put(block);
|
|---|
| 425 | if (rc != EOK)
|
|---|
| 426 | return rc;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | return EOK;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | /** Get reference to block group specified by index.
|
|---|
| 433 | *
|
|---|
| 434 | * @param fs Filesystem to find block group on
|
|---|
| 435 | * @param bgid Index of block group to load
|
|---|
| 436 | * @param ref Output pointer for reference
|
|---|
| 437 | *
|
|---|
| 438 | * @return Error code
|
|---|
| 439 | *
|
|---|
| 440 | */
|
|---|
| 441 | int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
|---|
| 442 | ext4_block_group_ref_t **ref)
|
|---|
| 443 | {
|
|---|
| 444 | /* Allocate memory for new structure */
|
|---|
| 445 | ext4_block_group_ref_t *newref =
|
|---|
| 446 | malloc(sizeof(ext4_block_group_ref_t));
|
|---|
| 447 | if (newref == NULL)
|
|---|
| 448 | return ENOMEM;
|
|---|
| 449 |
|
|---|
| 450 | /* Compute number of descriptors, that fits in one data block */
|
|---|
| 451 | uint32_t descriptors_per_block =
|
|---|
| 452 | ext4_superblock_get_block_size(fs->superblock) /
|
|---|
| 453 | ext4_superblock_get_desc_size(fs->superblock);
|
|---|
| 454 |
|
|---|
| 455 | /* Block group descriptor table starts at the next block after superblock */
|
|---|
| 456 | aoff64_t block_id =
|
|---|
| 457 | ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
|---|
| 458 |
|
|---|
| 459 | /* Find the block containing the descriptor we are looking for */
|
|---|
| 460 | block_id += bgid / descriptors_per_block;
|
|---|
| 461 | uint32_t offset = (bgid % descriptors_per_block) *
|
|---|
| 462 | ext4_superblock_get_desc_size(fs->superblock);
|
|---|
| 463 |
|
|---|
| 464 | /* Load block with descriptors */
|
|---|
| 465 | int rc = block_get(&newref->block, fs->device, block_id, 0);
|
|---|
| 466 | if (rc != EOK) {
|
|---|
| 467 | free(newref);
|
|---|
| 468 | return rc;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | /* Initialize in-memory representation */
|
|---|
| 472 | newref->block_group = newref->block->data + offset;
|
|---|
| 473 | newref->fs = fs;
|
|---|
| 474 | newref->index = bgid;
|
|---|
| 475 | newref->dirty = false;
|
|---|
| 476 |
|
|---|
| 477 | *ref = newref;
|
|---|
| 478 |
|
|---|
| 479 | if (ext4_block_group_has_flag(newref->block_group,
|
|---|
| 480 | EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
|
|---|
| 481 | rc = ext4_filesystem_init_block_bitmap(newref);
|
|---|
| 482 | if (rc != EOK) {
|
|---|
| 483 | block_put(newref->block);
|
|---|
| 484 | free(newref);
|
|---|
| 485 | return rc;
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | ext4_block_group_clear_flag(newref->block_group,
|
|---|
| 489 | EXT4_BLOCK_GROUP_BLOCK_UNINIT);
|
|---|
| 490 |
|
|---|
| 491 | newref->dirty = true;
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | if (ext4_block_group_has_flag(newref->block_group,
|
|---|
| 495 | EXT4_BLOCK_GROUP_INODE_UNINIT)) {
|
|---|
| 496 | rc = ext4_filesystem_init_inode_bitmap(newref);
|
|---|
| 497 | if (rc != EOK) {
|
|---|
| 498 | block_put(newref->block);
|
|---|
| 499 | free(newref);
|
|---|
| 500 | return rc;
|
|---|
| 501 | }
|
|---|
| 502 |
|
|---|
| 503 | ext4_block_group_clear_flag(newref->block_group,
|
|---|
| 504 | EXT4_BLOCK_GROUP_INODE_UNINIT);
|
|---|
| 505 |
|
|---|
| 506 | if (!ext4_block_group_has_flag(newref->block_group,
|
|---|
| 507 | EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
|
|---|
| 508 | rc = ext4_filesystem_init_inode_table(newref);
|
|---|
| 509 | if (rc != EOK)
|
|---|
| 510 | return rc;
|
|---|
| 511 |
|
|---|
| 512 | ext4_block_group_set_flag(newref->block_group,
|
|---|
| 513 | EXT4_BLOCK_GROUP_ITABLE_ZEROED);
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | newref->dirty = true;
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | return EOK;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | /** Compute checksum of block group descriptor.
|
|---|
| 523 | *
|
|---|
| 524 | * @param sb Superblock
|
|---|
| 525 | * @param bgid Index of block group in the filesystem
|
|---|
| 526 | * @param bg Block group to compute checksum for
|
|---|
| 527 | *
|
|---|
| 528 | * @return Checksum value
|
|---|
| 529 | *
|
|---|
| 530 | */
|
|---|
| 531 | static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
|
|---|
| 532 | ext4_block_group_t *bg)
|
|---|
| 533 | {
|
|---|
| 534 | /* If checksum not supported, 0 will be returned */
|
|---|
| 535 | uint16_t crc = 0;
|
|---|
| 536 |
|
|---|
| 537 | /* Compute the checksum only if the filesystem supports it */
|
|---|
| 538 | if (ext4_superblock_has_feature_read_only(sb,
|
|---|
| 539 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
|
|---|
| 540 | void *base = bg;
|
|---|
| 541 | void *checksum = &bg->checksum;
|
|---|
| 542 |
|
|---|
| 543 | uint32_t offset = (uint32_t) (checksum - base);
|
|---|
| 544 |
|
|---|
| 545 | /* Convert block group index to little endian */
|
|---|
| 546 | uint32_t le_group = host2uint32_t_le(bgid);
|
|---|
| 547 |
|
|---|
| 548 | /* Initialization */
|
|---|
| 549 | crc = crc16_ibm(~0, sb->uuid, sizeof(sb->uuid));
|
|---|
| 550 |
|
|---|
| 551 | /* Include index of block group */
|
|---|
| 552 | crc = crc16_ibm(crc, (uint8_t *) &le_group, sizeof(le_group));
|
|---|
| 553 |
|
|---|
| 554 | /* Compute crc from the first part (stop before checksum field) */
|
|---|
| 555 | crc = crc16_ibm(crc, (uint8_t *) bg, offset);
|
|---|
| 556 |
|
|---|
| 557 | /* Skip checksum */
|
|---|
| 558 | offset += sizeof(bg->checksum);
|
|---|
| 559 |
|
|---|
| 560 | /* Checksum of the rest of block group descriptor */
|
|---|
| 561 | if ((ext4_superblock_has_feature_incompatible(sb,
|
|---|
| 562 | EXT4_FEATURE_INCOMPAT_64BIT)) &&
|
|---|
| 563 | (offset < ext4_superblock_get_desc_size(sb)))
|
|---|
| 564 | crc = crc16_ibm(crc, ((uint8_t *) bg) + offset,
|
|---|
| 565 | ext4_superblock_get_desc_size(sb) - offset);
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | return crc;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | /** Get the size of the block group's inode table
|
|---|
| 572 | *
|
|---|
| 573 | * @param sb Pointer to the superblock
|
|---|
| 574 | * @param bg_ref Pointer to the block group reference
|
|---|
| 575 | *
|
|---|
| 576 | * @return Size of the inode table in blocks.
|
|---|
| 577 | */
|
|---|
| 578 | uint32_t ext4_filesystem_bg_get_itable_size(ext4_superblock_t *sb,
|
|---|
| 579 | ext4_block_group_ref_t *bg_ref)
|
|---|
| 580 | {
|
|---|
| 581 | uint32_t itable_size;
|
|---|
| 582 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
|---|
| 583 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
|---|
| 584 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
|---|
| 585 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 586 |
|
|---|
| 587 | if (bg_ref->index < block_group_count - 1) {
|
|---|
| 588 | itable_size = inodes_per_group * inode_table_item_size;
|
|---|
| 589 | } else {
|
|---|
| 590 | /* Last block group could be smaller */
|
|---|
| 591 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
|---|
| 592 | itable_size =
|
|---|
| 593 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
|
|---|
| 594 | inode_table_item_size;
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | return ROUND_UP(itable_size, block_size) / block_size;
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | /* Check if n is a power of p */
|
|---|
| 601 | static bool is_power_of(uint32_t n, unsigned p)
|
|---|
| 602 | {
|
|---|
| 603 | if (p == 1 && n != p)
|
|---|
| 604 | return false;
|
|---|
| 605 |
|
|---|
| 606 | while (n != p) {
|
|---|
| 607 | if (n < p)
|
|---|
| 608 | return false;
|
|---|
| 609 | else if ((n % p) != 0)
|
|---|
| 610 | return false;
|
|---|
| 611 |
|
|---|
| 612 | n /= p;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | return true;
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | /** Get the number of blocks used by superblock + gdt + reserved gdt backups
|
|---|
| 619 | *
|
|---|
| 620 | * @param bg Pointer to block group
|
|---|
| 621 | *
|
|---|
| 622 | * @return Number of blocks
|
|---|
| 623 | */
|
|---|
| 624 | uint32_t ext4_filesystem_bg_get_backup_blocks(ext4_block_group_ref_t *bg)
|
|---|
| 625 | {
|
|---|
| 626 | uint32_t const idx = bg->index;
|
|---|
| 627 | uint32_t r = 0;
|
|---|
| 628 | bool has_backups = false;
|
|---|
| 629 | ext4_superblock_t *sb = bg->fs->superblock;
|
|---|
| 630 |
|
|---|
| 631 | /* First step: determine if the block group contains the backups */
|
|---|
| 632 |
|
|---|
| 633 | if (idx <= 1)
|
|---|
| 634 | has_backups = true;
|
|---|
| 635 | else {
|
|---|
| 636 | if (ext4_superblock_has_feature_compatible(sb,
|
|---|
| 637 | EXT4_FEATURE_COMPAT_SPARSE_SUPER2)) {
|
|---|
| 638 | uint32_t g1, g2;
|
|---|
| 639 |
|
|---|
| 640 | ext4_superblock_get_backup_groups_sparse2(sb,
|
|---|
| 641 | &g1, &g2);
|
|---|
| 642 |
|
|---|
| 643 | if (idx == g1 || idx == g2)
|
|---|
| 644 | has_backups = true;
|
|---|
| 645 | } else if (!ext4_superblock_has_feature_read_only(sb,
|
|---|
| 646 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
|
|---|
| 647 | /* Very old fs were all block groups have
|
|---|
| 648 | * superblock and block descriptors backups.
|
|---|
| 649 | */
|
|---|
| 650 | has_backups = true;
|
|---|
| 651 | } else {
|
|---|
| 652 | if ((idx & 1) && (is_power_of(idx, 3) ||
|
|---|
| 653 | is_power_of(idx, 5) || is_power_of(idx, 7)))
|
|---|
| 654 | has_backups = true;
|
|---|
| 655 | }
|
|---|
| 656 | }
|
|---|
| 657 |
|
|---|
| 658 | if (has_backups) {
|
|---|
| 659 | uint32_t bg_count;
|
|---|
| 660 | uint32_t bg_desc_sz;
|
|---|
| 661 | uint32_t gdt_table; /* Size of the GDT in blocks */
|
|---|
| 662 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 663 |
|
|---|
| 664 | /* Now we know that this block group has backups,
|
|---|
| 665 | * we have to compute how many blocks are reserved
|
|---|
| 666 | * for them
|
|---|
| 667 | */
|
|---|
| 668 |
|
|---|
| 669 | if (idx == 0 && block_size == 1024) {
|
|---|
| 670 | /* Special case for first group were the boot block
|
|---|
| 671 | * resides
|
|---|
| 672 | */
|
|---|
| 673 | r++;
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | /* This accounts for the superblock */
|
|---|
| 677 | r++;
|
|---|
| 678 |
|
|---|
| 679 | /* Add the number of blocks used for the GDT */
|
|---|
| 680 | bg_count = ext4_superblock_get_block_group_count(sb);
|
|---|
| 681 | bg_desc_sz = ext4_superblock_get_desc_size(sb);
|
|---|
| 682 | gdt_table = ROUND_UP(bg_count * bg_desc_sz, block_size) /
|
|---|
| 683 | block_size;
|
|---|
| 684 |
|
|---|
| 685 | r += gdt_table;
|
|---|
| 686 |
|
|---|
| 687 | /* And now the number of reserved GDT blocks */
|
|---|
| 688 | r += ext4_superblock_get_reserved_gdt_blocks(sb);
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | return r;
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| 694 | /** Put reference to block group.
|
|---|
| 695 | *
|
|---|
| 696 | * @param ref Pointer for reference to be put back
|
|---|
| 697 | *
|
|---|
| 698 | * @return Error code
|
|---|
| 699 | *
|
|---|
| 700 | */
|
|---|
| 701 | int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
|---|
| 702 | {
|
|---|
| 703 | /* Check if reference modified */
|
|---|
| 704 | if (ref->dirty) {
|
|---|
| 705 | /* Compute new checksum of block group */
|
|---|
| 706 | uint16_t checksum =
|
|---|
| 707 | ext4_filesystem_bg_checksum(ref->fs->superblock, ref->index,
|
|---|
| 708 | ref->block_group);
|
|---|
| 709 | ext4_block_group_set_checksum(ref->block_group, checksum);
|
|---|
| 710 |
|
|---|
| 711 | /* Mark block dirty for writing changes to physical device */
|
|---|
| 712 | ref->block->dirty = true;
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | /* Put back block, that contains block group descriptor */
|
|---|
| 716 | int rc = block_put(ref->block);
|
|---|
| 717 | free(ref);
|
|---|
| 718 |
|
|---|
| 719 | return rc;
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | /** Get reference to i-node specified by index.
|
|---|
| 723 | *
|
|---|
| 724 | * @param fs Filesystem to find i-node on
|
|---|
| 725 | * @param index Index of i-node to load
|
|---|
| 726 | * @oaram ref Output pointer for reference
|
|---|
| 727 | *
|
|---|
| 728 | * @return Error code
|
|---|
| 729 | *
|
|---|
| 730 | */
|
|---|
| 731 | int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
|---|
| 732 | ext4_inode_ref_t **ref)
|
|---|
| 733 | {
|
|---|
| 734 | /* Allocate memory for new structure */
|
|---|
| 735 | ext4_inode_ref_t *newref =
|
|---|
| 736 | malloc(sizeof(ext4_inode_ref_t));
|
|---|
| 737 | if (newref == NULL)
|
|---|
| 738 | return ENOMEM;
|
|---|
| 739 |
|
|---|
| 740 | /* Compute number of i-nodes, that fits in one data block */
|
|---|
| 741 | uint32_t inodes_per_group =
|
|---|
| 742 | ext4_superblock_get_inodes_per_group(fs->superblock);
|
|---|
| 743 |
|
|---|
| 744 | /*
|
|---|
| 745 | * Inode numbers are 1-based, but it is simpler to work with 0-based
|
|---|
| 746 | * when computing indices
|
|---|
| 747 | */
|
|---|
| 748 | index -= 1;
|
|---|
| 749 | uint32_t block_group = index / inodes_per_group;
|
|---|
| 750 | uint32_t offset_in_group = index % inodes_per_group;
|
|---|
| 751 |
|
|---|
| 752 | /* Load block group, where i-node is located */
|
|---|
| 753 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 754 | int rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
|---|
| 755 | if (rc != EOK) {
|
|---|
| 756 | free(newref);
|
|---|
| 757 | return rc;
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 | /* Load block address, where i-node table is located */
|
|---|
| 761 | uint32_t inode_table_start =
|
|---|
| 762 | ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
|
|---|
| 763 | fs->superblock);
|
|---|
| 764 |
|
|---|
| 765 | /* Put back block group reference (not needed more) */
|
|---|
| 766 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 767 | if (rc != EOK) {
|
|---|
| 768 | free(newref);
|
|---|
| 769 | return rc;
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | /* Compute position of i-node in the block group */
|
|---|
| 773 | uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
|---|
| 774 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
|---|
| 775 | uint32_t byte_offset_in_group = offset_in_group * inode_size;
|
|---|
| 776 |
|
|---|
| 777 | /* Compute block address */
|
|---|
| 778 | aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
|
|---|
| 779 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
|---|
| 780 | if (rc != EOK) {
|
|---|
| 781 | free(newref);
|
|---|
| 782 | return rc;
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | /* Compute position of i-node in the data block */
|
|---|
| 786 | uint32_t offset_in_block = byte_offset_in_group % block_size;
|
|---|
| 787 | newref->inode = newref->block->data + offset_in_block;
|
|---|
| 788 |
|
|---|
| 789 | /* We need to store the original value of index in the reference */
|
|---|
| 790 | newref->index = index + 1;
|
|---|
| 791 | newref->fs = fs;
|
|---|
| 792 | newref->dirty = false;
|
|---|
| 793 |
|
|---|
| 794 | *ref = newref;
|
|---|
| 795 |
|
|---|
| 796 | return EOK;
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 | /** Put reference to i-node.
|
|---|
| 800 | *
|
|---|
| 801 | * @param ref Pointer for reference to be put back
|
|---|
| 802 | *
|
|---|
| 803 | * @return Error code
|
|---|
| 804 | *
|
|---|
| 805 | */
|
|---|
| 806 | int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
|---|
| 807 | {
|
|---|
| 808 | /* Check if reference modified */
|
|---|
| 809 | if (ref->dirty) {
|
|---|
| 810 | /* Mark block dirty for writing changes to physical device */
|
|---|
| 811 | ref->block->dirty = true;
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | /* Put back block, that contains i-node */
|
|---|
| 815 | int rc = block_put(ref->block);
|
|---|
| 816 | free(ref);
|
|---|
| 817 |
|
|---|
| 818 | return rc;
|
|---|
| 819 | }
|
|---|
| 820 |
|
|---|
| 821 | /** Allocate new i-node in the filesystem.
|
|---|
| 822 | *
|
|---|
| 823 | * @param fs Filesystem to allocated i-node on
|
|---|
| 824 | * @param inode_ref Output pointer to return reference to allocated i-node
|
|---|
| 825 | * @param flags Flags to be set for newly created i-node
|
|---|
| 826 | *
|
|---|
| 827 | * @return Error code
|
|---|
| 828 | *
|
|---|
| 829 | */
|
|---|
| 830 | int ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
|
|---|
| 831 | ext4_inode_ref_t **inode_ref, int flags)
|
|---|
| 832 | {
|
|---|
| 833 | /* Check if newly allocated i-node will be a directory */
|
|---|
| 834 | bool is_dir = false;
|
|---|
| 835 | if (flags & L_DIRECTORY)
|
|---|
| 836 | is_dir = true;
|
|---|
| 837 |
|
|---|
| 838 | /* Allocate inode by allocation algorithm */
|
|---|
| 839 | uint32_t index;
|
|---|
| 840 | int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
|
|---|
| 841 | if (rc != EOK)
|
|---|
| 842 | return rc;
|
|---|
| 843 |
|
|---|
| 844 | /* Load i-node from on-disk i-node table */
|
|---|
| 845 | rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
|
|---|
| 846 | if (rc != EOK) {
|
|---|
| 847 | ext4_ialloc_free_inode(fs, index, is_dir);
|
|---|
| 848 | return rc;
|
|---|
| 849 | }
|
|---|
| 850 |
|
|---|
| 851 | /* Initialize i-node */
|
|---|
| 852 | ext4_inode_t *inode = (*inode_ref)->inode;
|
|---|
| 853 |
|
|---|
| 854 | uint16_t mode;
|
|---|
| 855 | if (is_dir) {
|
|---|
| 856 | /*
|
|---|
| 857 | * Default directory permissions to be compatible with other systems
|
|---|
| 858 | * 0777 (octal) == rwxrwxrwx
|
|---|
| 859 | */
|
|---|
| 860 |
|
|---|
| 861 | mode = 0777;
|
|---|
| 862 | mode |= EXT4_INODE_MODE_DIRECTORY;
|
|---|
| 863 | ext4_inode_set_mode(fs->superblock, inode, mode);
|
|---|
| 864 | ext4_inode_set_links_count(inode, 1); /* '.' entry */
|
|---|
| 865 | } else {
|
|---|
| 866 | /*
|
|---|
| 867 | * Default file permissions to be compatible with other systems
|
|---|
| 868 | * 0666 (octal) == rw-rw-rw-
|
|---|
| 869 | */
|
|---|
| 870 |
|
|---|
| 871 | mode = 0666;
|
|---|
| 872 | mode |= EXT4_INODE_MODE_FILE;
|
|---|
| 873 | ext4_inode_set_mode(fs->superblock, inode, mode);
|
|---|
| 874 | ext4_inode_set_links_count(inode, 0);
|
|---|
| 875 | }
|
|---|
| 876 |
|
|---|
| 877 | ext4_inode_set_uid(inode, 0);
|
|---|
| 878 | ext4_inode_set_gid(inode, 0);
|
|---|
| 879 | ext4_inode_set_size(inode, 0);
|
|---|
| 880 | ext4_inode_set_access_time(inode, 0);
|
|---|
| 881 | ext4_inode_set_change_inode_time(inode, 0);
|
|---|
| 882 | ext4_inode_set_modification_time(inode, 0);
|
|---|
| 883 | ext4_inode_set_deletion_time(inode, 0);
|
|---|
| 884 | ext4_inode_set_blocks_count(fs->superblock, inode, 0);
|
|---|
| 885 | ext4_inode_set_flags(inode, 0);
|
|---|
| 886 | ext4_inode_set_generation(inode, 0);
|
|---|
| 887 |
|
|---|
| 888 | /* Reset blocks array */
|
|---|
| 889 | for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
|
|---|
| 890 | inode->blocks[i] = 0;
|
|---|
| 891 |
|
|---|
| 892 | /* Initialize extents if needed */
|
|---|
| 893 | if (ext4_superblock_has_feature_incompatible(
|
|---|
| 894 | fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
|
|---|
| 895 | ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
|
|---|
| 896 |
|
|---|
| 897 | /* Initialize extent root header */
|
|---|
| 898 | ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
|
|---|
| 899 | ext4_extent_header_set_depth(header, 0);
|
|---|
| 900 | ext4_extent_header_set_entries_count(header, 0);
|
|---|
| 901 | ext4_extent_header_set_generation(header, 0);
|
|---|
| 902 | ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
|
|---|
| 903 |
|
|---|
| 904 | uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
|
|---|
| 905 | sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
|
|---|
| 906 |
|
|---|
| 907 | ext4_extent_header_set_max_entries_count(header, max_entries);
|
|---|
| 908 | }
|
|---|
| 909 |
|
|---|
| 910 | (*inode_ref)->dirty = true;
|
|---|
| 911 |
|
|---|
| 912 | return EOK;
|
|---|
| 913 | }
|
|---|
| 914 |
|
|---|
| 915 | /** Release i-node and mark it as free.
|
|---|
| 916 | *
|
|---|
| 917 | * @param inode_ref I-node to be released
|
|---|
| 918 | *
|
|---|
| 919 | * @return Error code
|
|---|
| 920 | *
|
|---|
| 921 | */
|
|---|
| 922 | int ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
|
|---|
| 923 | {
|
|---|
| 924 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 925 |
|
|---|
| 926 | /* For extents must be data block destroyed by other way */
|
|---|
| 927 | if ((ext4_superblock_has_feature_incompatible(fs->superblock,
|
|---|
| 928 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 929 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
|---|
| 930 | /* Data structures are released during truncate operation... */
|
|---|
| 931 | goto finish;
|
|---|
| 932 | }
|
|---|
| 933 |
|
|---|
| 934 | /* Release all indirect (no data) blocks */
|
|---|
| 935 |
|
|---|
| 936 | /* 1) Single indirect */
|
|---|
| 937 | uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
|
|---|
| 938 | if (fblock != 0) {
|
|---|
| 939 | int rc = ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 940 | if (rc != EOK)
|
|---|
| 941 | return rc;
|
|---|
| 942 |
|
|---|
| 943 | ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
|
|---|
| 944 | }
|
|---|
| 945 |
|
|---|
| 946 | block_t *block;
|
|---|
| 947 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
|---|
| 948 | uint32_t count = block_size / sizeof(uint32_t);
|
|---|
| 949 |
|
|---|
| 950 | /* 2) Double indirect */
|
|---|
| 951 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
|
|---|
| 952 | if (fblock != 0) {
|
|---|
| 953 | int rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
|---|
| 954 | if (rc != EOK)
|
|---|
| 955 | return rc;
|
|---|
| 956 |
|
|---|
| 957 | uint32_t ind_block;
|
|---|
| 958 | for (uint32_t offset = 0; offset < count; ++offset) {
|
|---|
| 959 | ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
|
|---|
| 960 |
|
|---|
| 961 | if (ind_block != 0) {
|
|---|
| 962 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
|---|
| 963 | if (rc != EOK) {
|
|---|
| 964 | block_put(block);
|
|---|
| 965 | return rc;
|
|---|
| 966 | }
|
|---|
| 967 | }
|
|---|
| 968 | }
|
|---|
| 969 |
|
|---|
| 970 | rc = block_put(block);
|
|---|
| 971 | if (rc != EOK)
|
|---|
| 972 | return rc;
|
|---|
| 973 |
|
|---|
| 974 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 975 | if (rc != EOK)
|
|---|
| 976 | return rc;
|
|---|
| 977 |
|
|---|
| 978 | ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
|
|---|
| 979 | }
|
|---|
| 980 |
|
|---|
| 981 | /* 3) Tripple indirect */
|
|---|
| 982 | block_t *subblock;
|
|---|
| 983 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
|
|---|
| 984 | if (fblock != 0) {
|
|---|
| 985 | int rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
|---|
| 986 | if (rc != EOK)
|
|---|
| 987 | return rc;
|
|---|
| 988 |
|
|---|
| 989 | uint32_t ind_block;
|
|---|
| 990 | for (uint32_t offset = 0; offset < count; ++offset) {
|
|---|
| 991 | ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
|
|---|
| 992 |
|
|---|
| 993 | if (ind_block != 0) {
|
|---|
| 994 | rc = block_get(&subblock, fs->device, ind_block,
|
|---|
| 995 | BLOCK_FLAGS_NONE);
|
|---|
| 996 | if (rc != EOK) {
|
|---|
| 997 | block_put(block);
|
|---|
| 998 | return rc;
|
|---|
| 999 | }
|
|---|
| 1000 |
|
|---|
| 1001 | uint32_t ind_subblock;
|
|---|
| 1002 | for (uint32_t suboffset = 0; suboffset < count;
|
|---|
| 1003 | ++suboffset) {
|
|---|
| 1004 | ind_subblock = uint32_t_le2host(((uint32_t *)
|
|---|
| 1005 | subblock->data)[suboffset]);
|
|---|
| 1006 |
|
|---|
| 1007 | if (ind_subblock != 0) {
|
|---|
| 1008 | rc = ext4_balloc_free_block(inode_ref, ind_subblock);
|
|---|
| 1009 | if (rc != EOK) {
|
|---|
| 1010 | block_put(subblock);
|
|---|
| 1011 | block_put(block);
|
|---|
| 1012 | return rc;
|
|---|
| 1013 | }
|
|---|
| 1014 | }
|
|---|
| 1015 | }
|
|---|
| 1016 |
|
|---|
| 1017 | rc = block_put(subblock);
|
|---|
| 1018 | if (rc != EOK) {
|
|---|
| 1019 | block_put(block);
|
|---|
| 1020 | return rc;
|
|---|
| 1021 | }
|
|---|
| 1022 | }
|
|---|
| 1023 |
|
|---|
| 1024 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
|---|
| 1025 | if (rc != EOK) {
|
|---|
| 1026 | block_put(block);
|
|---|
| 1027 | return rc;
|
|---|
| 1028 | }
|
|---|
| 1029 | }
|
|---|
| 1030 |
|
|---|
| 1031 | rc = block_put(block);
|
|---|
| 1032 | if (rc != EOK)
|
|---|
| 1033 | return rc;
|
|---|
| 1034 |
|
|---|
| 1035 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 1036 | if (rc != EOK)
|
|---|
| 1037 | return rc;
|
|---|
| 1038 |
|
|---|
| 1039 | ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
|
|---|
| 1040 | }
|
|---|
| 1041 |
|
|---|
| 1042 | finish:
|
|---|
| 1043 | /* Mark inode dirty for writing to the physical device */
|
|---|
| 1044 | inode_ref->dirty = true;
|
|---|
| 1045 |
|
|---|
| 1046 | /* Free block with extended attributes if present */
|
|---|
| 1047 | uint32_t xattr_block = ext4_inode_get_file_acl(
|
|---|
| 1048 | inode_ref->inode, fs->superblock);
|
|---|
| 1049 | if (xattr_block) {
|
|---|
| 1050 | int rc = ext4_balloc_free_block(inode_ref, xattr_block);
|
|---|
| 1051 | if (rc != EOK)
|
|---|
| 1052 | return rc;
|
|---|
| 1053 |
|
|---|
| 1054 | ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
|
|---|
| 1055 | }
|
|---|
| 1056 |
|
|---|
| 1057 | /* Free inode by allocator */
|
|---|
| 1058 | int rc;
|
|---|
| 1059 | if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
|
|---|
| 1060 | EXT4_INODE_MODE_DIRECTORY))
|
|---|
| 1061 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
|
|---|
| 1062 | else
|
|---|
| 1063 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
|
|---|
| 1064 |
|
|---|
| 1065 | return rc;
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | /** Truncate i-node data blocks.
|
|---|
| 1069 | *
|
|---|
| 1070 | * @param inode_ref I-node to be truncated
|
|---|
| 1071 | * @param new_size New size of inode (must be < current size)
|
|---|
| 1072 | *
|
|---|
| 1073 | * @return Error code
|
|---|
| 1074 | *
|
|---|
| 1075 | */
|
|---|
| 1076 | int ext4_filesystem_truncate_inode(ext4_inode_ref_t *inode_ref,
|
|---|
| 1077 | aoff64_t new_size)
|
|---|
| 1078 | {
|
|---|
| 1079 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
|---|
| 1080 |
|
|---|
| 1081 | /* Check flags, if i-node can be truncated */
|
|---|
| 1082 | if (!ext4_inode_can_truncate(sb, inode_ref->inode))
|
|---|
| 1083 | return EINVAL;
|
|---|
| 1084 |
|
|---|
| 1085 | /* If sizes are equal, nothing has to be done. */
|
|---|
| 1086 | aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
|
|---|
| 1087 | if (old_size == new_size)
|
|---|
| 1088 | return EOK;
|
|---|
| 1089 |
|
|---|
| 1090 | /* It's not suppported to make the larger file by truncate operation */
|
|---|
| 1091 | if (old_size < new_size)
|
|---|
| 1092 | return EINVAL;
|
|---|
| 1093 |
|
|---|
| 1094 | /* Compute how many blocks will be released */
|
|---|
| 1095 | aoff64_t size_diff = old_size - new_size;
|
|---|
| 1096 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 1097 | uint32_t diff_blocks_count = size_diff / block_size;
|
|---|
| 1098 | if (size_diff % block_size != 0)
|
|---|
| 1099 | diff_blocks_count++;
|
|---|
| 1100 |
|
|---|
| 1101 | uint32_t old_blocks_count = old_size / block_size;
|
|---|
| 1102 | if (old_size % block_size != 0)
|
|---|
| 1103 | old_blocks_count++;
|
|---|
| 1104 |
|
|---|
| 1105 | if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
|
|---|
| 1106 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1107 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
|---|
| 1108 | /* Extents require special operation */
|
|---|
| 1109 | int rc = ext4_extent_release_blocks_from(inode_ref,
|
|---|
| 1110 | old_blocks_count - diff_blocks_count);
|
|---|
| 1111 | if (rc != EOK)
|
|---|
| 1112 | return rc;
|
|---|
| 1113 | } else {
|
|---|
| 1114 | /* Release data blocks from the end of file */
|
|---|
| 1115 |
|
|---|
| 1116 | /* Starting from 1 because of logical blocks are numbered from 0 */
|
|---|
| 1117 | for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
|
|---|
| 1118 | int rc = ext4_filesystem_release_inode_block(inode_ref,
|
|---|
| 1119 | old_blocks_count - i);
|
|---|
| 1120 | if (rc != EOK)
|
|---|
| 1121 | return rc;
|
|---|
| 1122 | }
|
|---|
| 1123 | }
|
|---|
| 1124 |
|
|---|
| 1125 | /* Update i-node */
|
|---|
| 1126 | ext4_inode_set_size(inode_ref->inode, new_size);
|
|---|
| 1127 | inode_ref->dirty = true;
|
|---|
| 1128 |
|
|---|
| 1129 | return EOK;
|
|---|
| 1130 | }
|
|---|
| 1131 |
|
|---|
| 1132 | /** Get physical block address by logical index of the block.
|
|---|
| 1133 | *
|
|---|
| 1134 | * @param inode_ref I-node to read block address from
|
|---|
| 1135 | * @param iblock Logical index of block
|
|---|
| 1136 | * @param fblock Output pointer for return physical block address
|
|---|
| 1137 | *
|
|---|
| 1138 | * @return Error code
|
|---|
| 1139 | *
|
|---|
| 1140 | */
|
|---|
| 1141 | int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
|---|
| 1142 | aoff64_t iblock, uint32_t *fblock)
|
|---|
| 1143 | {
|
|---|
| 1144 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 1145 |
|
|---|
| 1146 | /* For empty file is situation simple */
|
|---|
| 1147 | if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
|
|---|
| 1148 | *fblock = 0;
|
|---|
| 1149 | return EOK;
|
|---|
| 1150 | }
|
|---|
| 1151 |
|
|---|
| 1152 | uint32_t current_block;
|
|---|
| 1153 |
|
|---|
| 1154 | /* Handle i-node using extents */
|
|---|
| 1155 | if ((ext4_superblock_has_feature_incompatible(fs->superblock,
|
|---|
| 1156 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1157 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
|---|
| 1158 | int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
|
|---|
| 1159 | if (rc != EOK)
|
|---|
| 1160 | return rc;
|
|---|
| 1161 |
|
|---|
| 1162 | *fblock = current_block;
|
|---|
| 1163 | return EOK;
|
|---|
| 1164 | }
|
|---|
| 1165 |
|
|---|
| 1166 | ext4_inode_t *inode = inode_ref->inode;
|
|---|
| 1167 |
|
|---|
| 1168 | /* Direct block are read directly from array in i-node structure */
|
|---|
| 1169 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
|---|
| 1170 | current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
|
|---|
| 1171 | *fblock = current_block;
|
|---|
| 1172 | return EOK;
|
|---|
| 1173 | }
|
|---|
| 1174 |
|
|---|
| 1175 | /* Determine indirection level of the target block */
|
|---|
| 1176 | unsigned int level = 0;
|
|---|
| 1177 | for (unsigned int i = 1; i < 4; i++) {
|
|---|
| 1178 | if (iblock < fs->inode_block_limits[i]) {
|
|---|
| 1179 | level = i;
|
|---|
| 1180 | break;
|
|---|
| 1181 | }
|
|---|
| 1182 | }
|
|---|
| 1183 |
|
|---|
| 1184 | if (level == 0)
|
|---|
| 1185 | return EIO;
|
|---|
| 1186 |
|
|---|
| 1187 | /* Compute offsets for the topmost level */
|
|---|
| 1188 | aoff64_t block_offset_in_level =
|
|---|
| 1189 | iblock - fs->inode_block_limits[level - 1];
|
|---|
| 1190 | current_block = ext4_inode_get_indirect_block(inode, level - 1);
|
|---|
| 1191 | uint32_t offset_in_block =
|
|---|
| 1192 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1193 |
|
|---|
| 1194 | /* Sparse file */
|
|---|
| 1195 | if (current_block == 0) {
|
|---|
| 1196 | *fblock = 0;
|
|---|
| 1197 | return EOK;
|
|---|
| 1198 | }
|
|---|
| 1199 |
|
|---|
| 1200 | block_t *block;
|
|---|
| 1201 |
|
|---|
| 1202 | /*
|
|---|
| 1203 | * Navigate through other levels, until we find the block number
|
|---|
| 1204 | * or find null reference meaning we are dealing with sparse file
|
|---|
| 1205 | */
|
|---|
| 1206 | while (level > 0) {
|
|---|
| 1207 | /* Load indirect block */
|
|---|
| 1208 | int rc = block_get(&block, fs->device, current_block, 0);
|
|---|
| 1209 | if (rc != EOK)
|
|---|
| 1210 | return rc;
|
|---|
| 1211 |
|
|---|
| 1212 | /* Read block address from indirect block */
|
|---|
| 1213 | current_block =
|
|---|
| 1214 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
|---|
| 1215 |
|
|---|
| 1216 | /* Put back indirect block untouched */
|
|---|
| 1217 | rc = block_put(block);
|
|---|
| 1218 | if (rc != EOK)
|
|---|
| 1219 | return rc;
|
|---|
| 1220 |
|
|---|
| 1221 | /* Check for sparse file */
|
|---|
| 1222 | if (current_block == 0) {
|
|---|
| 1223 | *fblock = 0;
|
|---|
| 1224 | return EOK;
|
|---|
| 1225 | }
|
|---|
| 1226 |
|
|---|
| 1227 | /* Jump to the next level */
|
|---|
| 1228 | level--;
|
|---|
| 1229 |
|
|---|
| 1230 | /* Termination condition - we have address of data block loaded */
|
|---|
| 1231 | if (level == 0)
|
|---|
| 1232 | break;
|
|---|
| 1233 |
|
|---|
| 1234 | /* Visit the next level */
|
|---|
| 1235 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
|---|
| 1236 | offset_in_block =
|
|---|
| 1237 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1238 | }
|
|---|
| 1239 |
|
|---|
| 1240 | *fblock = current_block;
|
|---|
| 1241 |
|
|---|
| 1242 | return EOK;
|
|---|
| 1243 | }
|
|---|
| 1244 |
|
|---|
| 1245 | /** Set physical block address for the block logical address into the i-node.
|
|---|
| 1246 | *
|
|---|
| 1247 | * @param inode_ref I-node to set block address to
|
|---|
| 1248 | * @param iblock Logical index of block
|
|---|
| 1249 | * @param fblock Physical block address
|
|---|
| 1250 | *
|
|---|
| 1251 | * @return Error code
|
|---|
| 1252 | *
|
|---|
| 1253 | */
|
|---|
| 1254 | int ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
|---|
| 1255 | aoff64_t iblock, uint32_t fblock)
|
|---|
| 1256 | {
|
|---|
| 1257 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 1258 |
|
|---|
| 1259 | /* Handle inode using extents */
|
|---|
| 1260 | if ((ext4_superblock_has_feature_compatible(fs->superblock,
|
|---|
| 1261 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1262 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
|---|
| 1263 | /* Not reachable */
|
|---|
| 1264 | return ENOTSUP;
|
|---|
| 1265 | }
|
|---|
| 1266 |
|
|---|
| 1267 | /* Handle simple case when we are dealing with direct reference */
|
|---|
| 1268 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
|---|
| 1269 | ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
|
|---|
| 1270 | inode_ref->dirty = true;
|
|---|
| 1271 |
|
|---|
| 1272 | return EOK;
|
|---|
| 1273 | }
|
|---|
| 1274 |
|
|---|
| 1275 | /* Determine the indirection level needed to get the desired block */
|
|---|
| 1276 | unsigned int level = 0;
|
|---|
| 1277 | for (unsigned int i = 1; i < 4; i++) {
|
|---|
| 1278 | if (iblock < fs->inode_block_limits[i]) {
|
|---|
| 1279 | level = i;
|
|---|
| 1280 | break;
|
|---|
| 1281 | }
|
|---|
| 1282 | }
|
|---|
| 1283 |
|
|---|
| 1284 | if (level == 0)
|
|---|
| 1285 | return EIO;
|
|---|
| 1286 |
|
|---|
| 1287 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
|---|
| 1288 |
|
|---|
| 1289 | /* Compute offsets for the topmost level */
|
|---|
| 1290 | aoff64_t block_offset_in_level =
|
|---|
| 1291 | iblock - fs->inode_block_limits[level - 1];
|
|---|
| 1292 | uint32_t current_block =
|
|---|
| 1293 | ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
|
|---|
| 1294 | uint32_t offset_in_block =
|
|---|
| 1295 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1296 |
|
|---|
| 1297 | uint32_t new_block_addr;
|
|---|
| 1298 | block_t *block;
|
|---|
| 1299 | block_t *new_block;
|
|---|
| 1300 |
|
|---|
| 1301 | /* Is needed to allocate indirect block on the i-node level */
|
|---|
| 1302 | if (current_block == 0) {
|
|---|
| 1303 | /* Allocate new indirect block */
|
|---|
| 1304 | int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
|---|
| 1305 | if (rc != EOK)
|
|---|
| 1306 | return rc;
|
|---|
| 1307 |
|
|---|
| 1308 | /* Update i-node */
|
|---|
| 1309 | ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
|
|---|
| 1310 | new_block_addr);
|
|---|
| 1311 | inode_ref->dirty = true;
|
|---|
| 1312 |
|
|---|
| 1313 | /* Load newly allocated block */
|
|---|
| 1314 | rc = block_get(&new_block, fs->device, new_block_addr,
|
|---|
| 1315 | BLOCK_FLAGS_NOREAD);
|
|---|
| 1316 | if (rc != EOK) {
|
|---|
| 1317 | ext4_balloc_free_block(inode_ref, new_block_addr);
|
|---|
| 1318 | return rc;
|
|---|
| 1319 | }
|
|---|
| 1320 |
|
|---|
| 1321 | /* Initialize new block */
|
|---|
| 1322 | memset(new_block->data, 0, block_size);
|
|---|
| 1323 | new_block->dirty = true;
|
|---|
| 1324 |
|
|---|
| 1325 | /* Put back the allocated block */
|
|---|
| 1326 | rc = block_put(new_block);
|
|---|
| 1327 | if (rc != EOK)
|
|---|
| 1328 | return rc;
|
|---|
| 1329 |
|
|---|
| 1330 | current_block = new_block_addr;
|
|---|
| 1331 | }
|
|---|
| 1332 |
|
|---|
| 1333 | /*
|
|---|
| 1334 | * Navigate through other levels, until we find the block number
|
|---|
| 1335 | * or find null reference meaning we are dealing with sparse file
|
|---|
| 1336 | */
|
|---|
| 1337 | while (level > 0) {
|
|---|
| 1338 | int rc = block_get(&block, fs->device, current_block, 0);
|
|---|
| 1339 | if (rc != EOK)
|
|---|
| 1340 | return rc;
|
|---|
| 1341 |
|
|---|
| 1342 | current_block =
|
|---|
| 1343 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
|---|
| 1344 |
|
|---|
| 1345 | if ((level > 1) && (current_block == 0)) {
|
|---|
| 1346 | /* Allocate new block */
|
|---|
| 1347 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
|---|
| 1348 | if (rc != EOK) {
|
|---|
| 1349 | block_put(block);
|
|---|
| 1350 | return rc;
|
|---|
| 1351 | }
|
|---|
| 1352 |
|
|---|
| 1353 | /* Load newly allocated block */
|
|---|
| 1354 | rc = block_get(&new_block, fs->device, new_block_addr,
|
|---|
| 1355 | BLOCK_FLAGS_NOREAD);
|
|---|
| 1356 | if (rc != EOK) {
|
|---|
| 1357 | block_put(block);
|
|---|
| 1358 | return rc;
|
|---|
| 1359 | }
|
|---|
| 1360 |
|
|---|
| 1361 | /* Initialize allocated block */
|
|---|
| 1362 | memset(new_block->data, 0, block_size);
|
|---|
| 1363 | new_block->dirty = true;
|
|---|
| 1364 |
|
|---|
| 1365 | rc = block_put(new_block);
|
|---|
| 1366 | if (rc != EOK) {
|
|---|
| 1367 | block_put(block);
|
|---|
| 1368 | return rc;
|
|---|
| 1369 | }
|
|---|
| 1370 |
|
|---|
| 1371 | /* Write block address to the parent */
|
|---|
| 1372 | ((uint32_t *) block->data)[offset_in_block] =
|
|---|
| 1373 | host2uint32_t_le(new_block_addr);
|
|---|
| 1374 | block->dirty = true;
|
|---|
| 1375 | current_block = new_block_addr;
|
|---|
| 1376 | }
|
|---|
| 1377 |
|
|---|
| 1378 | /* Will be finished, write the fblock address */
|
|---|
| 1379 | if (level == 1) {
|
|---|
| 1380 | ((uint32_t *) block->data)[offset_in_block] =
|
|---|
| 1381 | host2uint32_t_le(fblock);
|
|---|
| 1382 | block->dirty = true;
|
|---|
| 1383 | }
|
|---|
| 1384 |
|
|---|
| 1385 | rc = block_put(block);
|
|---|
| 1386 | if (rc != EOK)
|
|---|
| 1387 | return rc;
|
|---|
| 1388 |
|
|---|
| 1389 | level--;
|
|---|
| 1390 |
|
|---|
| 1391 | /*
|
|---|
| 1392 | * If we are on the last level, break here as
|
|---|
| 1393 | * there is no next level to visit
|
|---|
| 1394 | */
|
|---|
| 1395 | if (level == 0)
|
|---|
| 1396 | break;
|
|---|
| 1397 |
|
|---|
| 1398 | /* Visit the next level */
|
|---|
| 1399 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
|---|
| 1400 | offset_in_block =
|
|---|
| 1401 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1402 | }
|
|---|
| 1403 |
|
|---|
| 1404 | return EOK;
|
|---|
| 1405 | }
|
|---|
| 1406 |
|
|---|
| 1407 | /** Release data block from i-node
|
|---|
| 1408 | *
|
|---|
| 1409 | * @param inode_ref I-node to release block from
|
|---|
| 1410 | * @param iblock Logical block to be released
|
|---|
| 1411 | *
|
|---|
| 1412 | * @return Error code
|
|---|
| 1413 | *
|
|---|
| 1414 | */
|
|---|
| 1415 | int ext4_filesystem_release_inode_block(ext4_inode_ref_t *inode_ref,
|
|---|
| 1416 | uint32_t iblock)
|
|---|
| 1417 | {
|
|---|
| 1418 | uint32_t fblock;
|
|---|
| 1419 |
|
|---|
| 1420 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 1421 |
|
|---|
| 1422 | /* Extents are handled otherwise = there is not support in this function */
|
|---|
| 1423 | assert(!(ext4_superblock_has_feature_incompatible(fs->superblock,
|
|---|
| 1424 | EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
|---|
| 1425 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
|
|---|
| 1426 |
|
|---|
| 1427 | ext4_inode_t *inode = inode_ref->inode;
|
|---|
| 1428 |
|
|---|
| 1429 | /* Handle simple case when we are dealing with direct reference */
|
|---|
| 1430 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
|---|
| 1431 | fblock = ext4_inode_get_direct_block(inode, iblock);
|
|---|
| 1432 |
|
|---|
| 1433 | /* Sparse file */
|
|---|
| 1434 | if (fblock == 0)
|
|---|
| 1435 | return EOK;
|
|---|
| 1436 |
|
|---|
| 1437 | ext4_inode_set_direct_block(inode, iblock, 0);
|
|---|
| 1438 | return ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 1439 | }
|
|---|
| 1440 |
|
|---|
| 1441 | /* Determine the indirection level needed to get the desired block */
|
|---|
| 1442 | unsigned int level = 0;
|
|---|
| 1443 | for (unsigned int i = 1; i < 4; i++) {
|
|---|
| 1444 | if (iblock < fs->inode_block_limits[i]) {
|
|---|
| 1445 | level = i;
|
|---|
| 1446 | break;
|
|---|
| 1447 | }
|
|---|
| 1448 | }
|
|---|
| 1449 |
|
|---|
| 1450 | if (level == 0)
|
|---|
| 1451 | return EIO;
|
|---|
| 1452 |
|
|---|
| 1453 | /* Compute offsets for the topmost level */
|
|---|
| 1454 | aoff64_t block_offset_in_level =
|
|---|
| 1455 | iblock - fs->inode_block_limits[level - 1];
|
|---|
| 1456 | uint32_t current_block =
|
|---|
| 1457 | ext4_inode_get_indirect_block(inode, level - 1);
|
|---|
| 1458 | uint32_t offset_in_block =
|
|---|
| 1459 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1460 |
|
|---|
| 1461 | /*
|
|---|
| 1462 | * Navigate through other levels, until we find the block number
|
|---|
| 1463 | * or find null reference meaning we are dealing with sparse file
|
|---|
| 1464 | */
|
|---|
| 1465 | block_t *block;
|
|---|
| 1466 | while (level > 0) {
|
|---|
| 1467 |
|
|---|
| 1468 | /* Sparse check */
|
|---|
| 1469 | if (current_block == 0)
|
|---|
| 1470 | return EOK;
|
|---|
| 1471 |
|
|---|
| 1472 | int rc = block_get(&block, fs->device, current_block, 0);
|
|---|
| 1473 | if (rc != EOK)
|
|---|
| 1474 | return rc;
|
|---|
| 1475 |
|
|---|
| 1476 | current_block =
|
|---|
| 1477 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
|---|
| 1478 |
|
|---|
| 1479 | /* Set zero if physical data block address found */
|
|---|
| 1480 | if (level == 1) {
|
|---|
| 1481 | ((uint32_t *) block->data)[offset_in_block] =
|
|---|
| 1482 | host2uint32_t_le(0);
|
|---|
| 1483 | block->dirty = true;
|
|---|
| 1484 | }
|
|---|
| 1485 |
|
|---|
| 1486 | rc = block_put(block);
|
|---|
| 1487 | if (rc != EOK)
|
|---|
| 1488 | return rc;
|
|---|
| 1489 |
|
|---|
| 1490 | level--;
|
|---|
| 1491 |
|
|---|
| 1492 | /*
|
|---|
| 1493 | * If we are on the last level, break here as
|
|---|
| 1494 | * there is no next level to visit
|
|---|
| 1495 | */
|
|---|
| 1496 | if (level == 0)
|
|---|
| 1497 | break;
|
|---|
| 1498 |
|
|---|
| 1499 | /* Visit the next level */
|
|---|
| 1500 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
|---|
| 1501 | offset_in_block =
|
|---|
| 1502 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1503 | }
|
|---|
| 1504 |
|
|---|
| 1505 | fblock = current_block;
|
|---|
| 1506 | if (fblock == 0)
|
|---|
| 1507 | return EOK;
|
|---|
| 1508 |
|
|---|
| 1509 | /* Physical block is not referenced, it can be released */
|
|---|
| 1510 | return ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 1511 | }
|
|---|
| 1512 |
|
|---|
| 1513 | /** Append following logical block to the i-node.
|
|---|
| 1514 | *
|
|---|
| 1515 | * @param inode_ref I-node to append block to
|
|---|
| 1516 | * @param fblock Output physical block address of newly allocated block
|
|---|
| 1517 | * @param iblock Output logical number of newly allocated block
|
|---|
| 1518 | *
|
|---|
| 1519 | * @return Error code
|
|---|
| 1520 | *
|
|---|
| 1521 | */
|
|---|
| 1522 | int ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
|
|---|
| 1523 | uint32_t *fblock, uint32_t *iblock)
|
|---|
| 1524 | {
|
|---|
| 1525 | /* Handle extents separately */
|
|---|
| 1526 | if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
|
|---|
| 1527 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1528 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))
|
|---|
| 1529 | return ext4_extent_append_block(inode_ref, iblock, fblock, true);
|
|---|
| 1530 |
|
|---|
| 1531 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
|---|
| 1532 |
|
|---|
| 1533 | /* Compute next block index and allocate data block */
|
|---|
| 1534 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
|---|
| 1535 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 1536 |
|
|---|
| 1537 | /* Align size i-node size */
|
|---|
| 1538 | if ((inode_size % block_size) != 0)
|
|---|
| 1539 | inode_size += block_size - (inode_size % block_size);
|
|---|
| 1540 |
|
|---|
| 1541 | /* Logical blocks are numbered from 0 */
|
|---|
| 1542 | uint32_t new_block_idx = inode_size / block_size;
|
|---|
| 1543 |
|
|---|
| 1544 | /* Allocate new physical block */
|
|---|
| 1545 | uint32_t phys_block;
|
|---|
| 1546 | int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
|
|---|
| 1547 | if (rc != EOK)
|
|---|
| 1548 | return rc;
|
|---|
| 1549 |
|
|---|
| 1550 | /* Add physical block address to the i-node */
|
|---|
| 1551 | rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
|
|---|
| 1552 | new_block_idx, phys_block);
|
|---|
| 1553 | if (rc != EOK) {
|
|---|
| 1554 | ext4_balloc_free_block(inode_ref, phys_block);
|
|---|
| 1555 | return rc;
|
|---|
| 1556 | }
|
|---|
| 1557 |
|
|---|
| 1558 | /* Update i-node */
|
|---|
| 1559 | ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
|
|---|
| 1560 | inode_ref->dirty = true;
|
|---|
| 1561 |
|
|---|
| 1562 | *fblock = phys_block;
|
|---|
| 1563 | *iblock = new_block_idx;
|
|---|
| 1564 |
|
|---|
| 1565 | return EOK;
|
|---|
| 1566 | }
|
|---|
| 1567 |
|
|---|
| 1568 | /**
|
|---|
| 1569 | * @}
|
|---|
| 1570 | */
|
|---|