[6c501f8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Frantisek Princ
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libext4
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file libext4_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 |
|
---|
| 43 | int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id)
|
---|
| 44 | {
|
---|
[01ab41b] | 45 | int rc;
|
---|
| 46 |
|
---|
| 47 | fs->device = service_id;
|
---|
| 48 |
|
---|
[fffb061] | 49 | rc = block_init(EXCHANGE_SERIALIZE, fs->device, 4096);
|
---|
[01ab41b] | 50 | if (rc != EOK) {
|
---|
| 51 | return rc;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[9c0c0e1] | 54 | /* Read superblock from device */
|
---|
[d9bbe45] | 55 | ext4_superblock_t *temp_superblock;
|
---|
[01ab41b] | 56 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
---|
| 57 | if (rc != EOK) {
|
---|
| 58 | block_fini(fs->device);
|
---|
| 59 | return rc;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[9c0c0e1] | 62 | /* Read block size from superblock and check */
|
---|
[d9bbe45] | 63 | uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
|
---|
[01ab41b] | 64 | if (block_size > EXT4_MAX_BLOCK_SIZE) {
|
---|
| 65 | block_fini(fs->device);
|
---|
| 66 | return ENOTSUP;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[9c0c0e1] | 69 | /* Initialize block caching */
|
---|
[b12ca16] | 70 | rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
|
---|
[01ab41b] | 71 | if (rc != EOK) {
|
---|
| 72 | block_fini(fs->device);
|
---|
| 73 | return rc;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[d9bbe45] | 76 | uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
|
---|
[a9a0982] | 77 | fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
---|
| 78 | fs->inode_blocks_per_level[0] = 1;
|
---|
[d9bbe45] | 79 | for (int i = 1; i < 4; i++) {
|
---|
[6088193] | 80 | fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i-1] *
|
---|
[a9a0982] | 81 | block_ids_per_block;
|
---|
| 82 | fs->inode_block_limits[i] = fs->inode_block_limits[i-1] +
|
---|
| 83 | fs->inode_blocks_per_level[i];
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[9c0c0e1] | 86 | /* Return loaded superblock */
|
---|
[01ab41b] | 87 | fs->superblock = temp_superblock;
|
---|
| 88 |
|
---|
[6c501f8] | 89 | return EOK;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[ae3d4f8] | 92 | int ext4_filesystem_fini(ext4_filesystem_t *fs, bool write_sb)
|
---|
[3711e7e] | 93 | {
|
---|
[ae3d4f8] | 94 | int rc = EOK;
|
---|
[d9bbe45] | 95 |
|
---|
[ae3d4f8] | 96 | if (write_sb) {
|
---|
| 97 | rc = ext4_superblock_write_direct(fs->device, fs->superblock);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[3711e7e] | 100 | free(fs->superblock);
|
---|
| 101 | block_fini(fs->device);
|
---|
[ae3d4f8] | 102 |
|
---|
| 103 | return rc;
|
---|
[3711e7e] | 104 | }
|
---|
| 105 |
|
---|
[6c501f8] | 106 | int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
|
---|
| 107 | {
|
---|
[01ab41b] | 108 | int rc;
|
---|
| 109 |
|
---|
| 110 | rc = ext4_superblock_check_sanity(fs->superblock);
|
---|
| 111 | if (rc != EOK) {
|
---|
| 112 | return rc;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[6c501f8] | 115 | return EOK;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[9c0c0e1] | 118 | int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
|
---|
[6c501f8] | 119 | {
|
---|
[9c0c0e1] | 120 | /* Feature flags are present in rev 1 and later */
|
---|
| 121 | if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
|
---|
| 122 | *o_read_only = false;
|
---|
| 123 | return EOK;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | uint32_t incompatible_features;
|
---|
| 127 | incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
|
---|
| 128 | incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
|
---|
| 129 | if (incompatible_features > 0) {
|
---|
| 130 | *o_read_only = true;
|
---|
| 131 | return ENOTSUP;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | uint32_t compatible_read_only;
|
---|
| 135 | compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
|
---|
| 136 | compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
|
---|
| 137 | if (compatible_read_only > 0) {
|
---|
| 138 | *o_read_only = true;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[6c501f8] | 141 | return EOK;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[3711e7e] | 144 | int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
---|
| 145 | ext4_block_group_ref_t **ref)
|
---|
[6c501f8] | 146 | {
|
---|
[3711e7e] | 147 | int rc;
|
---|
| 148 |
|
---|
[d9bbe45] | 149 | ext4_block_group_ref_t *newref = malloc(sizeof(ext4_block_group_ref_t));
|
---|
[3711e7e] | 150 | if (newref == NULL) {
|
---|
| 151 | return ENOMEM;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[d9bbe45] | 154 | uint32_t descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
|
---|
[c25e39b] | 155 | / ext4_superblock_get_desc_size(fs->superblock);
|
---|
[3711e7e] | 156 |
|
---|
| 157 | /* Block group descriptor table starts at the next block after superblock */
|
---|
[d9bbe45] | 158 | aoff64_t block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
---|
[3711e7e] | 159 |
|
---|
| 160 | /* Find the block containing the descriptor we are looking for */
|
---|
| 161 | block_id += bgid / descriptors_per_block;
|
---|
[d9bbe45] | 162 | uint32_t offset = (bgid % descriptors_per_block) * ext4_superblock_get_desc_size(fs->superblock);
|
---|
[3711e7e] | 163 |
|
---|
| 164 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
| 165 | if (rc != EOK) {
|
---|
| 166 | free(newref);
|
---|
| 167 | return rc;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | newref->block_group = newref->block->data + offset;
|
---|
[1ac1ab4] | 171 | newref->fs = fs;
|
---|
| 172 | newref->index = bgid;
|
---|
[12b4a7f] | 173 | newref->dirty = false;
|
---|
[3711e7e] | 174 |
|
---|
| 175 | *ref = newref;
|
---|
| 176 |
|
---|
| 177 | return EOK;
|
---|
[6c501f8] | 178 | }
|
---|
| 179 |
|
---|
[1ac1ab4] | 180 | static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
|
---|
| 181 | ext4_block_group_t *bg)
|
---|
| 182 | {
|
---|
| 183 | uint16_t crc = 0;
|
---|
| 184 |
|
---|
| 185 | if (ext4_superblock_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
|
---|
| 186 |
|
---|
| 187 | void *base = bg;
|
---|
| 188 | void *checksum = &bg->checksum;
|
---|
| 189 |
|
---|
| 190 | uint32_t offset = (uint32_t)(checksum - base);
|
---|
| 191 |
|
---|
| 192 | uint32_t le_group = host2uint32_t_le(bgid);
|
---|
| 193 |
|
---|
| 194 | crc = crc16(~0, sb->uuid, sizeof(sb->uuid));
|
---|
| 195 | crc = crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
|
---|
| 196 | crc = crc16(crc, (uint8_t *)bg, offset);
|
---|
| 197 |
|
---|
| 198 | offset += sizeof(bg->checksum); /* skip checksum */
|
---|
| 199 |
|
---|
| 200 | /* for checksum of struct ext4_group_desc do the rest...*/
|
---|
| 201 | if ((ext4_superblock_has_feature_incompatible(sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
|
---|
| 202 | offset < ext4_superblock_get_desc_size(sb)) {
|
---|
| 203 |
|
---|
| 204 | crc = crc16(crc, ((uint8_t *)bg) + offset, ext4_superblock_get_desc_size(sb) - offset);
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | return crc;
|
---|
| 209 |
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 |
|
---|
[829d238] | 213 | int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
---|
| 214 | {
|
---|
| 215 | int rc;
|
---|
| 216 |
|
---|
[12b4a7f] | 217 | if (ref->dirty) {
|
---|
[1ac1ab4] | 218 | uint16_t checksum = ext4_filesystem_bg_checksum(
|
---|
| 219 | ref->fs->superblock, ref->index, ref->block_group);
|
---|
| 220 |
|
---|
| 221 | ext4_block_group_set_checksum(ref->block_group, checksum);
|
---|
| 222 |
|
---|
[12b4a7f] | 223 | ref->block->dirty = true;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[829d238] | 226 | rc = block_put(ref->block);
|
---|
| 227 | free(ref);
|
---|
| 228 |
|
---|
| 229 | return rc;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[3711e7e] | 232 | int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
---|
| 233 | ext4_inode_ref_t **ref)
|
---|
| 234 | {
|
---|
| 235 | int rc;
|
---|
| 236 |
|
---|
[d9bbe45] | 237 | ext4_inode_ref_t *newref = malloc(sizeof(ext4_inode_ref_t));
|
---|
[3711e7e] | 238 | if (newref == NULL) {
|
---|
| 239 | return ENOMEM;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[d9bbe45] | 242 | uint32_t inodes_per_group =
|
---|
| 243 | ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
[3711e7e] | 244 |
|
---|
| 245 | /* inode numbers are 1-based, but it is simpler to work with 0-based
|
---|
| 246 | * when computing indices
|
---|
| 247 | */
|
---|
| 248 | index -= 1;
|
---|
[d9bbe45] | 249 | uint32_t block_group = index / inodes_per_group;
|
---|
| 250 | uint32_t offset_in_group = index % inodes_per_group;
|
---|
[3711e7e] | 251 |
|
---|
[d9bbe45] | 252 | ext4_block_group_ref_t *bg_ref;
|
---|
[3711e7e] | 253 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 254 | if (rc != EOK) {
|
---|
| 255 | free(newref);
|
---|
| 256 | return rc;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[d9bbe45] | 259 | uint32_t inode_table_start = ext4_block_group_get_inode_table_first_block(
|
---|
[fe27eb4] | 260 | bg_ref->block_group, fs->superblock);
|
---|
[3711e7e] | 261 |
|
---|
[829d238] | 262 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 263 | if (rc != EOK) {
|
---|
| 264 | free(newref);
|
---|
| 265 | return rc;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[d9bbe45] | 268 | uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
---|
| 269 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 270 | uint32_t byte_offset_in_group = offset_in_group * inode_size;
|
---|
[3711e7e] | 271 |
|
---|
[d9bbe45] | 272 | aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
|
---|
[3711e7e] | 273 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
| 274 | if (rc != EOK) {
|
---|
| 275 | free(newref);
|
---|
| 276 | return rc;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[d9bbe45] | 279 | uint32_t offset_in_block = byte_offset_in_group % block_size;
|
---|
[3711e7e] | 280 | newref->inode = newref->block->data + offset_in_block;
|
---|
| 281 | /* we decremented index above, but need to store the original value
|
---|
| 282 | * in the reference
|
---|
| 283 | */
|
---|
[1ac1ab4] | 284 | newref->index = index + 1;
|
---|
| 285 | newref->fs = fs;
|
---|
[052e82d] | 286 | newref->dirty = false;
|
---|
[3711e7e] | 287 |
|
---|
| 288 | *ref = newref;
|
---|
| 289 |
|
---|
[9b9d37bb] | 290 | return EOK;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[e68c834] | 293 |
|
---|
[9b9d37bb] | 294 | int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
---|
| 295 | {
|
---|
| 296 | int rc;
|
---|
| 297 |
|
---|
[052e82d] | 298 | if (ref->dirty) {
|
---|
| 299 | ref->block->dirty = true;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[9b9d37bb] | 302 | rc = block_put(ref->block);
|
---|
| 303 | free(ref);
|
---|
| 304 |
|
---|
| 305 | return rc;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[304faab] | 308 | int ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
|
---|
| 309 | ext4_inode_ref_t **inode_ref, int flags)
|
---|
[2d2c6ce] | 310 | {
|
---|
[304faab] | 311 | int rc;
|
---|
[2d2c6ce] | 312 |
|
---|
[304faab] | 313 | bool is_dir = false;
|
---|
[2d2c6ce] | 314 | if (flags & L_DIRECTORY) {
|
---|
[304faab] | 315 | is_dir = true;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | // allocate inode
|
---|
| 319 | uint32_t index;
|
---|
| 320 | rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
|
---|
| 321 | if (rc != EOK) {
|
---|
| 322 | return rc;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | // TODO extents, dir_index etc...
|
---|
[ca3d77a] | 326 |
|
---|
[304faab] | 327 | rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
|
---|
| 328 | if (rc != EOK) {
|
---|
| 329 | ext4_ialloc_free_inode(fs, index, is_dir);
|
---|
| 330 | return rc;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | // init inode
|
---|
| 334 | ext4_inode_t *inode = (*inode_ref)->inode;
|
---|
| 335 |
|
---|
| 336 | if (is_dir) {
|
---|
[2d2c6ce] | 337 | ext4_inode_set_mode(fs->superblock, inode, EXT4_INODE_MODE_DIRECTORY);
|
---|
| 338 | ext4_inode_set_links_count(inode, 1); // '.' entry
|
---|
| 339 | } else {
|
---|
| 340 | ext4_inode_set_mode(fs->superblock, inode, EXT4_INODE_MODE_FILE);
|
---|
| 341 | ext4_inode_set_links_count(inode, 0);
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | ext4_inode_set_uid(inode, 0);
|
---|
| 345 | ext4_inode_set_gid(inode, 0);
|
---|
| 346 | ext4_inode_set_size(inode, 0);
|
---|
| 347 | ext4_inode_set_access_time(inode, 0);
|
---|
| 348 | ext4_inode_set_change_inode_time(inode, 0);
|
---|
| 349 | ext4_inode_set_modification_time(inode, 0);
|
---|
| 350 | ext4_inode_set_deletion_time(inode, 0);
|
---|
| 351 | ext4_inode_set_blocks_count(fs->superblock, inode, 0);
|
---|
| 352 | ext4_inode_set_flags(inode, 0);
|
---|
| 353 | ext4_inode_set_generation(inode, 0);
|
---|
| 354 |
|
---|
| 355 | for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++) {
|
---|
| 356 | inode->blocks[i] = 0;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[304faab] | 359 | (*inode_ref)->dirty = true;
|
---|
| 360 |
|
---|
[2d2c6ce] | 361 | return EOK;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[1ac1ab4] | 364 | int ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
|
---|
[3d4fd2c] | 365 | {
|
---|
| 366 | int rc;
|
---|
| 367 |
|
---|
[ca3d77a] | 368 | // release all indirect (no data) blocks
|
---|
[3d4fd2c] | 369 |
|
---|
| 370 | // 1) Single indirect
|
---|
[d9bbe45] | 371 | uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
|
---|
[3d4fd2c] | 372 | if (fblock != 0) {
|
---|
[1ac1ab4] | 373 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[3d4fd2c] | 374 | if (rc != EOK) {
|
---|
[ca3d77a] | 375 | return rc;
|
---|
[3d4fd2c] | 376 | }
|
---|
| 377 |
|
---|
| 378 | ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[1ac1ab4] | 381 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 382 |
|
---|
[3d4fd2c] | 383 | block_t *block;
|
---|
| 384 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 385 | uint32_t count = block_size / sizeof(uint32_t);
|
---|
| 386 |
|
---|
| 387 | // 2) Double indirect
|
---|
| 388 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
|
---|
| 389 | if (fblock != 0) {
|
---|
| 390 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 391 | if (rc != EOK) {
|
---|
[e63ce679] | 392 | return rc;
|
---|
[3d4fd2c] | 393 | }
|
---|
| 394 |
|
---|
| 395 | uint32_t ind_block;
|
---|
| 396 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
| 397 | ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
|
---|
| 398 |
|
---|
| 399 | if (ind_block != 0) {
|
---|
[1ac1ab4] | 400 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
---|
[3d4fd2c] | 401 | if (rc != EOK) {
|
---|
[e63ce679] | 402 | block_put(block);
|
---|
| 403 | return rc;
|
---|
[3d4fd2c] | 404 | }
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | block_put(block);
|
---|
[1ac1ab4] | 409 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[3d4fd2c] | 410 | if (rc != EOK) {
|
---|
[e63ce679] | 411 | return rc;
|
---|
[3d4fd2c] | 412 | }
|
---|
| 413 |
|
---|
| 414 | ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 |
|
---|
| 418 | // 3) Tripple indirect
|
---|
| 419 | block_t *subblock;
|
---|
| 420 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
|
---|
| 421 | if (fblock != 0) {
|
---|
| 422 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 423 | if (rc != EOK) {
|
---|
[e63ce679] | 424 | return rc;
|
---|
[3d4fd2c] | 425 | }
|
---|
| 426 |
|
---|
| 427 | uint32_t ind_block;
|
---|
| 428 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
| 429 | ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
|
---|
| 430 |
|
---|
| 431 | if (ind_block != 0) {
|
---|
| 432 | rc = block_get(&subblock, fs->device, ind_block, BLOCK_FLAGS_NONE);
|
---|
| 433 | if (rc != EOK) {
|
---|
[e63ce679] | 434 | block_put(block);
|
---|
| 435 | return rc;
|
---|
[3d4fd2c] | 436 | }
|
---|
| 437 |
|
---|
| 438 | uint32_t ind_subblock;
|
---|
| 439 | for (uint32_t suboffset = 0; suboffset < count; ++suboffset) {
|
---|
| 440 | ind_subblock = uint32_t_le2host(((uint32_t*)subblock->data)[suboffset]);
|
---|
| 441 |
|
---|
| 442 | if (ind_subblock != 0) {
|
---|
[1ac1ab4] | 443 | rc = ext4_balloc_free_block(inode_ref, ind_subblock);
|
---|
[3d4fd2c] | 444 | if (rc != EOK) {
|
---|
[e63ce679] | 445 | block_put(subblock);
|
---|
| 446 | block_put(block);
|
---|
| 447 | return rc;
|
---|
[3d4fd2c] | 448 | }
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | }
|
---|
| 452 | block_put(subblock);
|
---|
| 453 |
|
---|
| 454 | }
|
---|
| 455 |
|
---|
[1ac1ab4] | 456 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
---|
[3d4fd2c] | 457 | if (rc != EOK) {
|
---|
[e63ce679] | 458 | block_put(block);
|
---|
| 459 | return rc;
|
---|
[3d4fd2c] | 460 | }
|
---|
| 461 |
|
---|
| 462 |
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | block_put(block);
|
---|
[1ac1ab4] | 466 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[3d4fd2c] | 467 | if (rc != EOK) {
|
---|
[e63ce679] | 468 | return rc;
|
---|
[3d4fd2c] | 469 | }
|
---|
| 470 |
|
---|
| 471 | ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[e5f8762] | 474 | inode_ref->dirty = true;
|
---|
| 475 |
|
---|
[3d4fd2c] | 476 | // Free inode
|
---|
[304faab] | 477 | if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
|
---|
| 478 | EXT4_INODE_MODE_DIRECTORY)) {
|
---|
| 479 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
|
---|
| 480 | } else {
|
---|
| 481 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
|
---|
| 482 | }
|
---|
[3d4fd2c] | 483 | if (rc != EOK) {
|
---|
| 484 | return rc;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | return EOK;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[1ac1ab4] | 490 | int ext4_filesystem_truncate_inode(
|
---|
[3d4fd2c] | 491 | ext4_inode_ref_t *inode_ref, aoff64_t new_size)
|
---|
| 492 | {
|
---|
[ca3d77a] | 493 | int rc;
|
---|
| 494 |
|
---|
[1ac1ab4] | 495 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
| 496 |
|
---|
| 497 | if (! ext4_inode_can_truncate(sb, inode_ref->inode)) {
|
---|
[3d4fd2c] | 498 | // Unable to truncate
|
---|
| 499 | return EINVAL;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[1ac1ab4] | 502 | aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
[3d4fd2c] | 503 | if (old_size == new_size) {
|
---|
| 504 | // Nothing to do
|
---|
| 505 | return EOK;
|
---|
| 506 | }
|
---|
| 507 |
|
---|
[ca3d77a] | 508 | // It's not suppported to make the larger file
|
---|
[3d4fd2c] | 509 | if (old_size < new_size) {
|
---|
| 510 | return EINVAL;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[d9bbe45] | 513 | aoff64_t size_diff = old_size - new_size;
|
---|
[1ac1ab4] | 514 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[ca3d77a] | 515 | uint32_t diff_blocks_count = size_diff / block_size;
|
---|
[3d4fd2c] | 516 | if (size_diff % block_size != 0) {
|
---|
[ca3d77a] | 517 | diff_blocks_count++;
|
---|
[3d4fd2c] | 518 | }
|
---|
| 519 |
|
---|
[ca3d77a] | 520 | uint32_t old_blocks_count = old_size / block_size;
|
---|
[3d4fd2c] | 521 | if (old_size % block_size != 0) {
|
---|
[ca3d77a] | 522 | old_blocks_count++;
|
---|
[3d4fd2c] | 523 | }
|
---|
| 524 |
|
---|
| 525 | // starting from 1 because of logical blocks are numbered from 0
|
---|
[ca3d77a] | 526 | for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
|
---|
[1ac1ab4] | 527 | rc = ext4_filesystem_release_inode_block(inode_ref, old_blocks_count - i);
|
---|
[ca3d77a] | 528 | if (rc != EOK) {
|
---|
| 529 | return rc;
|
---|
| 530 | }
|
---|
[3d4fd2c] | 531 | }
|
---|
| 532 |
|
---|
| 533 | ext4_inode_set_size(inode_ref->inode, new_size);
|
---|
| 534 |
|
---|
| 535 | inode_ref->dirty = true;
|
---|
| 536 |
|
---|
| 537 | return EOK;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
[1ac1ab4] | 540 | int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
---|
| 541 | aoff64_t iblock, uint32_t* fblock)
|
---|
[9b9d37bb] | 542 | {
|
---|
| 543 | int rc;
|
---|
[d9bbe45] | 544 |
|
---|
[1ac1ab4] | 545 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[d9bbe45] | 546 |
|
---|
[9b9d37bb] | 547 | uint32_t current_block;
|
---|
| 548 |
|
---|
[8958a26] | 549 | /* Handle inode using extents */
|
---|
[a872fc09] | 550 | if (ext4_superblock_has_feature_incompatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
[9104bb5] | 551 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
[1ac1ab4] | 552 | rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
|
---|
[9104bb5] | 553 |
|
---|
| 554 | if (rc != EOK) {
|
---|
| 555 | return rc;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[acd869e] | 558 | *fblock = current_block;
|
---|
| 559 | return EOK;
|
---|
| 560 |
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[9104bb5] | 563 | ext4_inode_t *inode = inode_ref->inode;
|
---|
| 564 |
|
---|
[9b9d37bb] | 565 | /* Handle simple case when we are dealing with direct reference */
|
---|
[e68c834] | 566 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[9b9d37bb] | 567 | current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
|
---|
| 568 | *fblock = current_block;
|
---|
| 569 | return EOK;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | /* Determine the indirection level needed to get the desired block */
|
---|
[d9bbe45] | 573 | int level = -1;
|
---|
| 574 | for (int i = 1; i < 4; i++) {
|
---|
[a9a0982] | 575 | if (iblock < fs->inode_block_limits[i]) {
|
---|
[9b9d37bb] | 576 | level = i;
|
---|
| 577 | break;
|
---|
| 578 | }
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | if (level == -1) {
|
---|
| 582 | return EIO;
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | /* Compute offsets for the topmost level */
|
---|
[d9bbe45] | 586 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
[9b9d37bb] | 587 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
[d9bbe45] | 588 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[9b9d37bb] | 589 |
|
---|
[6088193] | 590 | if (current_block == 0) {
|
---|
| 591 | *fblock = 0;
|
---|
| 592 | return EOK;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
[d9bbe45] | 595 | block_t *block;
|
---|
| 596 |
|
---|
[9b9d37bb] | 597 | /* Navigate through other levels, until we find the block number
|
---|
| 598 | * or find null reference meaning we are dealing with sparse file
|
---|
| 599 | */
|
---|
| 600 | while (level > 0) {
|
---|
| 601 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 602 | if (rc != EOK) {
|
---|
| 603 | return rc;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
| 607 |
|
---|
| 608 | rc = block_put(block);
|
---|
| 609 | if (rc != EOK) {
|
---|
| 610 | return rc;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | if (current_block == 0) {
|
---|
| 614 | /* This is a sparse file */
|
---|
| 615 | *fblock = 0;
|
---|
| 616 | return EOK;
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | level -= 1;
|
---|
| 620 |
|
---|
| 621 | /* If we are on the last level, break here as
|
---|
| 622 | * there is no next level to visit
|
---|
| 623 | */
|
---|
| 624 | if (level == 0) {
|
---|
| 625 | break;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | /* Visit the next level */
|
---|
[a9a0982] | 629 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 630 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[9b9d37bb] | 631 | }
|
---|
| 632 |
|
---|
| 633 | *fblock = current_block;
|
---|
| 634 |
|
---|
[3711e7e] | 635 | return EOK;
|
---|
| 636 | }
|
---|
[6c501f8] | 637 |
|
---|
[d5a78e28] | 638 |
|
---|
[1ac1ab4] | 639 | int ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
---|
| 640 | aoff64_t iblock, uint32_t fblock)
|
---|
[35f48f2] | 641 | {
|
---|
[1e65444] | 642 | int rc;
|
---|
[d9bbe45] | 643 |
|
---|
[1ac1ab4] | 644 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[35f48f2] | 645 |
|
---|
| 646 | /* Handle inode using extents */
|
---|
| 647 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
[1e65444] | 648 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
[35f48f2] | 649 | // TODO
|
---|
| 650 | return ENOTSUP;
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | /* Handle simple case when we are dealing with direct reference */
|
---|
| 654 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[1e65444] | 655 | ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
|
---|
| 656 | inode_ref->dirty = true;
|
---|
[35f48f2] | 657 | return EOK;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
[1e65444] | 660 | /* Determine the indirection level needed to get the desired block */
|
---|
[d9bbe45] | 661 | int level = -1;
|
---|
| 662 | for (int i = 1; i < 4; i++) {
|
---|
[1e65444] | 663 | if (iblock < fs->inode_block_limits[i]) {
|
---|
| 664 | level = i;
|
---|
| 665 | break;
|
---|
| 666 | }
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | if (level == -1) {
|
---|
| 670 | return EIO;
|
---|
| 671 | }
|
---|
| 672 |
|
---|
[d9bbe45] | 673 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
[1e65444] | 674 |
|
---|
| 675 | /* Compute offsets for the topmost level */
|
---|
[d9bbe45] | 676 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
| 677 | uint32_t current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
|
---|
| 678 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 679 |
|
---|
| 680 | uint32_t new_block_addr;
|
---|
| 681 | block_t *block, *new_block;
|
---|
[1e65444] | 682 |
|
---|
| 683 | if (current_block == 0) {
|
---|
[1ac1ab4] | 684 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
---|
[1e65444] | 685 | if (rc != EOK) {
|
---|
[e63ce679] | 686 | return rc;
|
---|
[1e65444] | 687 | }
|
---|
| 688 |
|
---|
| 689 | ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
|
---|
| 690 |
|
---|
| 691 | inode_ref->dirty = true;
|
---|
| 692 |
|
---|
| 693 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
| 694 | if (rc != EOK) {
|
---|
[1ac1ab4] | 695 | ext4_balloc_free_block(inode_ref, new_block_addr);
|
---|
[e63ce679] | 696 | return rc;
|
---|
[1e65444] | 697 | }
|
---|
| 698 |
|
---|
| 699 | memset(new_block->data, 0, block_size);
|
---|
| 700 | new_block->dirty = true;
|
---|
| 701 |
|
---|
| 702 | rc = block_put(new_block);
|
---|
| 703 | if (rc != EOK) {
|
---|
[e63ce679] | 704 | return rc;
|
---|
[1e65444] | 705 | }
|
---|
| 706 |
|
---|
| 707 | current_block = new_block_addr;
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | /* Navigate through other levels, until we find the block number
|
---|
| 711 | * or find null reference meaning we are dealing with sparse file
|
---|
| 712 | */
|
---|
| 713 | while (level > 0) {
|
---|
| 714 |
|
---|
| 715 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 716 | if (rc != EOK) {
|
---|
| 717 | return rc;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
| 721 |
|
---|
[b12ca16] | 722 | if ((level > 1) && (current_block == 0)) {
|
---|
[1ac1ab4] | 723 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
---|
[b12ca16] | 724 | if (rc != EOK) {
|
---|
[e63ce679] | 725 | block_put(block);
|
---|
| 726 | return rc;
|
---|
[b12ca16] | 727 | }
|
---|
[1e65444] | 728 |
|
---|
[b12ca16] | 729 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
| 730 | if (rc != EOK) {
|
---|
[e63ce679] | 731 | block_put(block);
|
---|
| 732 | return rc;
|
---|
[b12ca16] | 733 | }
|
---|
[e63ce679] | 734 |
|
---|
[b12ca16] | 735 | memset(new_block->data, 0, block_size);
|
---|
| 736 | new_block->dirty = true;
|
---|
[6088193] | 737 |
|
---|
[b12ca16] | 738 | rc = block_put(new_block);
|
---|
| 739 | if (rc != EOK) {
|
---|
[e63ce679] | 740 | block_put(block);
|
---|
| 741 | return rc;
|
---|
[b12ca16] | 742 | }
|
---|
[1e65444] | 743 |
|
---|
[b12ca16] | 744 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
|
---|
| 745 | block->dirty = true;
|
---|
| 746 | current_block = new_block_addr;
|
---|
| 747 | }
|
---|
[1e65444] | 748 |
|
---|
[b12ca16] | 749 | if (level == 1) {
|
---|
| 750 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
|
---|
| 751 | block->dirty = true;
|
---|
[1e65444] | 752 | }
|
---|
| 753 |
|
---|
| 754 | rc = block_put(block);
|
---|
| 755 | if (rc != EOK) {
|
---|
| 756 | return rc;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | level -= 1;
|
---|
| 760 |
|
---|
| 761 | /* If we are on the last level, break here as
|
---|
| 762 | * there is no next level to visit
|
---|
| 763 | */
|
---|
| 764 | if (level == 0) {
|
---|
| 765 | break;
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | /* Visit the next level */
|
---|
| 769 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 770 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 771 | }
|
---|
[35f48f2] | 772 |
|
---|
| 773 | return EOK;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
[1ac1ab4] | 776 | int ext4_filesystem_release_inode_block(
|
---|
[052e82d] | 777 | ext4_inode_ref_t *inode_ref, uint32_t iblock)
|
---|
[d5a78e28] | 778 | {
|
---|
| 779 | int rc;
|
---|
[d9bbe45] | 780 |
|
---|
[fffb061] | 781 | uint32_t fblock;
|
---|
| 782 |
|
---|
[1ac1ab4] | 783 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 784 |
|
---|
[fffb061] | 785 | /* TODO Handle extents */
|
---|
| 786 | // if (ext4_superblock_has_feature_incompatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
| 787 | // ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
| 788 | //// rc = ext4_extent_find_block(fs, inode_ref, iblock, ¤t_block);
|
---|
| 789 | ////
|
---|
| 790 | //// if (rc != EOK) {
|
---|
| 791 | //// return rc;
|
---|
| 792 | //// }
|
---|
| 793 | ////
|
---|
| 794 | //// *fblock = current_block;
|
---|
| 795 | //
|
---|
| 796 | // // TODO release block from extents (and return fblock)
|
---|
| 797 | //
|
---|
| 798 | // fblock = 0;
|
---|
| 799 | //
|
---|
| 800 | // if (fblock == 0) {
|
---|
| 801 | // return EOK;
|
---|
| 802 | // }
|
---|
| 803 | //
|
---|
| 804 | // return ext4_balloc_free_block(fs, inode_ref, fblock);
|
---|
| 805 | //
|
---|
| 806 | // return EOK;
|
---|
| 807 | //
|
---|
| 808 | // }
|
---|
[d5a78e28] | 809 |
|
---|
| 810 |
|
---|
[12b4a7f] | 811 |
|
---|
[d9bbe45] | 812 | ext4_inode_t *inode = inode_ref->inode;
|
---|
| 813 |
|
---|
[052e82d] | 814 | /* Handle simple case when we are dealing with direct reference */
|
---|
| 815 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[12b4a7f] | 816 | fblock = ext4_inode_get_direct_block(inode, iblock);
|
---|
[052e82d] | 817 | // Sparse file
|
---|
| 818 | if (fblock == 0) {
|
---|
| 819 | return EOK;
|
---|
| 820 | }
|
---|
[d5a78e28] | 821 |
|
---|
[052e82d] | 822 | ext4_inode_set_direct_block(inode, iblock, 0);
|
---|
[1ac1ab4] | 823 | return ext4_balloc_free_block(inode_ref, fblock);
|
---|
[12b4a7f] | 824 | }
|
---|
| 825 |
|
---|
| 826 |
|
---|
| 827 | /* Determine the indirection level needed to get the desired block */
|
---|
[d9bbe45] | 828 | int level = -1;
|
---|
| 829 | for (int i = 1; i < 4; i++) {
|
---|
[12b4a7f] | 830 | if (iblock < fs->inode_block_limits[i]) {
|
---|
| 831 | level = i;
|
---|
| 832 | break;
|
---|
[052e82d] | 833 | }
|
---|
[12b4a7f] | 834 | }
|
---|
| 835 |
|
---|
| 836 | if (level == -1) {
|
---|
| 837 | return EIO;
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | /* Compute offsets for the topmost level */
|
---|
[d9bbe45] | 841 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
| 842 | uint32_t current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
| 843 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[d5a78e28] | 844 |
|
---|
[12b4a7f] | 845 | /* Navigate through other levels, until we find the block number
|
---|
| 846 | * or find null reference meaning we are dealing with sparse file
|
---|
| 847 | */
|
---|
[d9bbe45] | 848 | block_t *block;
|
---|
[12b4a7f] | 849 | while (level > 0) {
|
---|
| 850 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 851 | if (rc != EOK) {
|
---|
| 852 | return rc;
|
---|
[052e82d] | 853 | }
|
---|
[d5a78e28] | 854 |
|
---|
[12b4a7f] | 855 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
[d5a78e28] | 856 |
|
---|
[12b4a7f] | 857 | // Set zero
|
---|
| 858 | if (level == 1) {
|
---|
| 859 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
|
---|
| 860 | block->dirty = true;
|
---|
[052e82d] | 861 | }
|
---|
[d5a78e28] | 862 |
|
---|
[12b4a7f] | 863 | rc = block_put(block);
|
---|
| 864 | if (rc != EOK) {
|
---|
| 865 | return rc;
|
---|
| 866 | }
|
---|
[d5a78e28] | 867 |
|
---|
[12b4a7f] | 868 | level -= 1;
|
---|
| 869 |
|
---|
| 870 | /* If we are on the last level, break here as
|
---|
| 871 | * there is no next level to visit
|
---|
| 872 | */
|
---|
| 873 | if (level == 0) {
|
---|
| 874 | break;
|
---|
[052e82d] | 875 | }
|
---|
[12b4a7f] | 876 |
|
---|
| 877 | /* Visit the next level */
|
---|
| 878 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 879 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 880 | }
|
---|
| 881 |
|
---|
| 882 | fblock = current_block;
|
---|
| 883 |
|
---|
| 884 | if (fblock == 0) {
|
---|
| 885 | return EOK;
|
---|
[052e82d] | 886 | }
|
---|
[d5a78e28] | 887 |
|
---|
[1ac1ab4] | 888 | return ext4_balloc_free_block(inode_ref, fblock);
|
---|
[d5a78e28] | 889 |
|
---|
| 890 | }
|
---|
| 891 |
|
---|
[1ac1ab4] | 892 | int ext4_filesystem_add_orphan(ext4_inode_ref_t *inode_ref)
|
---|
[ebcaff4] | 893 | {
|
---|
[1ac1ab4] | 894 | uint32_t next_orphan = ext4_superblock_get_last_orphan(
|
---|
| 895 | inode_ref->fs->superblock);
|
---|
[ebcaff4] | 896 | ext4_inode_set_deletion_time(inode_ref->inode, next_orphan);
|
---|
[1ac1ab4] | 897 | ext4_superblock_set_last_orphan(
|
---|
| 898 | inode_ref->fs->superblock, inode_ref->index);
|
---|
[ebcaff4] | 899 | inode_ref->dirty = true;
|
---|
| 900 |
|
---|
| 901 | return EOK;
|
---|
| 902 | }
|
---|
| 903 |
|
---|
[1ac1ab4] | 904 | int ext4_filesystem_delete_orphan(ext4_inode_ref_t *inode_ref)
|
---|
[ebcaff4] | 905 | {
|
---|
| 906 | int rc;
|
---|
| 907 |
|
---|
[1ac1ab4] | 908 | uint32_t last_orphan = ext4_superblock_get_last_orphan(
|
---|
| 909 | inode_ref->fs->superblock);
|
---|
[ebcaff4] | 910 | assert(last_orphan > 0);
|
---|
| 911 |
|
---|
| 912 | uint32_t next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
|
---|
| 913 |
|
---|
| 914 | if (last_orphan == inode_ref->index) {
|
---|
[1ac1ab4] | 915 | ext4_superblock_set_last_orphan(inode_ref->fs->superblock, next_orphan);
|
---|
[ebcaff4] | 916 | ext4_inode_set_deletion_time(inode_ref->inode, 0);
|
---|
| 917 | inode_ref->dirty = true;
|
---|
| 918 | return EOK;
|
---|
| 919 | }
|
---|
| 920 |
|
---|
| 921 | ext4_inode_ref_t *current;
|
---|
[1ac1ab4] | 922 | rc = ext4_filesystem_get_inode_ref(inode_ref->fs, last_orphan, ¤t);
|
---|
[ebcaff4] | 923 | if (rc != EOK) {
|
---|
| 924 | return rc;
|
---|
| 925 | }
|
---|
| 926 | next_orphan = ext4_inode_get_deletion_time(current->inode);
|
---|
| 927 |
|
---|
| 928 | bool found;
|
---|
| 929 |
|
---|
| 930 | while (next_orphan != 0) {
|
---|
| 931 | if (next_orphan == inode_ref->index) {
|
---|
| 932 | next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
|
---|
| 933 | ext4_inode_set_deletion_time(current->inode, next_orphan);
|
---|
| 934 | current->dirty = true;
|
---|
| 935 | found = true;
|
---|
| 936 | break;
|
---|
| 937 | }
|
---|
| 938 |
|
---|
| 939 | ext4_filesystem_put_inode_ref(current);
|
---|
| 940 |
|
---|
[1ac1ab4] | 941 | rc = ext4_filesystem_get_inode_ref(inode_ref->fs, next_orphan, ¤t);
|
---|
[ebcaff4] | 942 | if (rc != EOK) {
|
---|
| 943 | return rc;
|
---|
| 944 | }
|
---|
| 945 | next_orphan = ext4_inode_get_deletion_time(current->inode);
|
---|
| 946 |
|
---|
| 947 | }
|
---|
| 948 |
|
---|
| 949 | ext4_inode_set_deletion_time(inode_ref->inode, 0);
|
---|
| 950 |
|
---|
| 951 | rc = ext4_filesystem_put_inode_ref(current);
|
---|
| 952 | if (rc != EOK) {
|
---|
| 953 | return rc;
|
---|
| 954 | }
|
---|
| 955 |
|
---|
| 956 | if (!found) {
|
---|
| 957 | return ENOENT;
|
---|
| 958 | }
|
---|
| 959 |
|
---|
| 960 | return EOK;
|
---|
| 961 | }
|
---|
| 962 |
|
---|
[6c501f8] | 963 | /**
|
---|
| 964 | * @}
|
---|
| 965 | */
|
---|