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