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