[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) {
|
---|
[5b0a3946] | 218 | uint16_t checksum = ext4_filesystem_bg_checksum(
|
---|
[1ac1ab4] | 219 | ref->fs->superblock, ref->index, ref->block_group);
|
---|
| 220 |
|
---|
[5b0a3946] | 221 | ext4_block_group_set_checksum(ref->block_group, checksum);
|
---|
[1ac1ab4] | 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 |
|
---|
[ce6de59] | 325 | // TODO dir_index initialization
|
---|
[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 |
|
---|
[936132f] | 355 | // Reset blocks array
|
---|
[2d2c6ce] | 356 | for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++) {
|
---|
| 357 | inode->blocks[i] = 0;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[936132f] | 360 | if (ext4_superblock_has_feature_incompatible(
|
---|
| 361 | fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
|
---|
| 362 |
|
---|
| 363 | ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
|
---|
| 364 |
|
---|
| 365 | // Initialize extent root header
|
---|
| 366 | ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
|
---|
| 367 | ext4_extent_header_set_depth(header, 0);
|
---|
| 368 | ext4_extent_header_set_entries_count(header, 0);
|
---|
| 369 | ext4_extent_header_set_generation(header, 0);
|
---|
| 370 | ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
|
---|
| 371 |
|
---|
| 372 | uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof (uint32_t) - sizeof(ext4_extent_header_t))
|
---|
| 373 | / sizeof(ext4_extent_t);
|
---|
| 374 |
|
---|
| 375 | ext4_extent_header_set_max_entries_count(header, max_entries);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
[304faab] | 378 | (*inode_ref)->dirty = true;
|
---|
| 379 |
|
---|
[2d2c6ce] | 380 | return EOK;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[1ac1ab4] | 383 | int ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
|
---|
[3d4fd2c] | 384 | {
|
---|
| 385 | int rc;
|
---|
| 386 |
|
---|
[3e2952b] | 387 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 388 |
|
---|
| 389 | if (ext4_superblock_has_feature_incompatible(
|
---|
| 390 | fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
| 391 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
| 392 |
|
---|
| 393 | // Data structures are released during truncate operation...
|
---|
| 394 | goto finish;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[ca3d77a] | 397 | // release all indirect (no data) blocks
|
---|
[3d4fd2c] | 398 |
|
---|
| 399 | // 1) Single indirect
|
---|
[d9bbe45] | 400 | uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
|
---|
[3d4fd2c] | 401 | if (fblock != 0) {
|
---|
[1ac1ab4] | 402 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[3d4fd2c] | 403 | if (rc != EOK) {
|
---|
[ca3d77a] | 404 | return rc;
|
---|
[3d4fd2c] | 405 | }
|
---|
| 406 |
|
---|
| 407 | ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | block_t *block;
|
---|
| 411 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 412 | uint32_t count = block_size / sizeof(uint32_t);
|
---|
| 413 |
|
---|
| 414 | // 2) Double indirect
|
---|
| 415 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
|
---|
| 416 | if (fblock != 0) {
|
---|
| 417 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 418 | if (rc != EOK) {
|
---|
[e63ce679] | 419 | return rc;
|
---|
[3d4fd2c] | 420 | }
|
---|
| 421 |
|
---|
| 422 | uint32_t ind_block;
|
---|
| 423 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
| 424 | ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
|
---|
| 425 |
|
---|
| 426 | if (ind_block != 0) {
|
---|
[1ac1ab4] | 427 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
---|
[3d4fd2c] | 428 | if (rc != EOK) {
|
---|
[e63ce679] | 429 | block_put(block);
|
---|
| 430 | return rc;
|
---|
[3d4fd2c] | 431 | }
|
---|
| 432 | }
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | block_put(block);
|
---|
[1ac1ab4] | 436 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[3d4fd2c] | 437 | if (rc != EOK) {
|
---|
[e63ce679] | 438 | return rc;
|
---|
[3d4fd2c] | 439 | }
|
---|
| 440 |
|
---|
| 441 | ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 |
|
---|
| 445 | // 3) Tripple indirect
|
---|
| 446 | block_t *subblock;
|
---|
| 447 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
|
---|
| 448 | if (fblock != 0) {
|
---|
| 449 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 450 | if (rc != EOK) {
|
---|
[e63ce679] | 451 | return rc;
|
---|
[3d4fd2c] | 452 | }
|
---|
| 453 |
|
---|
| 454 | uint32_t ind_block;
|
---|
| 455 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
| 456 | ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
|
---|
| 457 |
|
---|
| 458 | if (ind_block != 0) {
|
---|
| 459 | rc = block_get(&subblock, fs->device, ind_block, BLOCK_FLAGS_NONE);
|
---|
| 460 | if (rc != EOK) {
|
---|
[e63ce679] | 461 | block_put(block);
|
---|
| 462 | return rc;
|
---|
[3d4fd2c] | 463 | }
|
---|
| 464 |
|
---|
| 465 | uint32_t ind_subblock;
|
---|
| 466 | for (uint32_t suboffset = 0; suboffset < count; ++suboffset) {
|
---|
| 467 | ind_subblock = uint32_t_le2host(((uint32_t*)subblock->data)[suboffset]);
|
---|
| 468 |
|
---|
| 469 | if (ind_subblock != 0) {
|
---|
[1ac1ab4] | 470 | rc = ext4_balloc_free_block(inode_ref, ind_subblock);
|
---|
[3d4fd2c] | 471 | if (rc != EOK) {
|
---|
[e63ce679] | 472 | block_put(subblock);
|
---|
| 473 | block_put(block);
|
---|
| 474 | return rc;
|
---|
[3d4fd2c] | 475 | }
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | }
|
---|
| 479 | block_put(subblock);
|
---|
| 480 |
|
---|
| 481 | }
|
---|
| 482 |
|
---|
[1ac1ab4] | 483 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
---|
[3d4fd2c] | 484 | if (rc != EOK) {
|
---|
[e63ce679] | 485 | block_put(block);
|
---|
| 486 | return rc;
|
---|
[3d4fd2c] | 487 | }
|
---|
| 488 |
|
---|
| 489 |
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | block_put(block);
|
---|
[1ac1ab4] | 493 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[3d4fd2c] | 494 | if (rc != EOK) {
|
---|
[e63ce679] | 495 | return rc;
|
---|
[3d4fd2c] | 496 | }
|
---|
| 497 |
|
---|
| 498 | ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
|
---|
| 499 | }
|
---|
| 500 |
|
---|
[3e2952b] | 501 | finish:
|
---|
[e5f8762] | 502 | inode_ref->dirty = true;
|
---|
| 503 |
|
---|
[3d4fd2c] | 504 | // Free inode
|
---|
[304faab] | 505 | if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
|
---|
| 506 | EXT4_INODE_MODE_DIRECTORY)) {
|
---|
| 507 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
|
---|
| 508 | } else {
|
---|
| 509 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
|
---|
| 510 | }
|
---|
[3d4fd2c] | 511 | if (rc != EOK) {
|
---|
| 512 | return rc;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | return EOK;
|
---|
| 516 | }
|
---|
| 517 |
|
---|
[1ac1ab4] | 518 | int ext4_filesystem_truncate_inode(
|
---|
[3d4fd2c] | 519 | ext4_inode_ref_t *inode_ref, aoff64_t new_size)
|
---|
| 520 | {
|
---|
[ca3d77a] | 521 | int rc;
|
---|
| 522 |
|
---|
[1ac1ab4] | 523 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
| 524 |
|
---|
| 525 | if (! ext4_inode_can_truncate(sb, inode_ref->inode)) {
|
---|
[3d4fd2c] | 526 | // Unable to truncate
|
---|
| 527 | return EINVAL;
|
---|
| 528 | }
|
---|
| 529 |
|
---|
[1ac1ab4] | 530 | aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
[3d4fd2c] | 531 | if (old_size == new_size) {
|
---|
| 532 | // Nothing to do
|
---|
| 533 | return EOK;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
[ca3d77a] | 536 | // It's not suppported to make the larger file
|
---|
[3d4fd2c] | 537 | if (old_size < new_size) {
|
---|
| 538 | return EINVAL;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[d9bbe45] | 541 | aoff64_t size_diff = old_size - new_size;
|
---|
[1ac1ab4] | 542 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[ca3d77a] | 543 | uint32_t diff_blocks_count = size_diff / block_size;
|
---|
[3d4fd2c] | 544 | if (size_diff % block_size != 0) {
|
---|
[ca3d77a] | 545 | diff_blocks_count++;
|
---|
[3d4fd2c] | 546 | }
|
---|
| 547 |
|
---|
[ca3d77a] | 548 | uint32_t old_blocks_count = old_size / block_size;
|
---|
[3d4fd2c] | 549 | if (old_size % block_size != 0) {
|
---|
[ca3d77a] | 550 | old_blocks_count++;
|
---|
[3d4fd2c] | 551 | }
|
---|
| 552 |
|
---|
[5b0a3946] | 553 | if (ext4_superblock_has_feature_incompatible(
|
---|
| 554 | inode_ref->fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
| 555 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
| 556 |
|
---|
[1196df6] | 557 | rc = ext4_extent_release_blocks_from(inode_ref,
|
---|
| 558 | old_blocks_count - diff_blocks_count);
|
---|
[ca3d77a] | 559 | if (rc != EOK) {
|
---|
| 560 | return rc;
|
---|
| 561 | }
|
---|
[5b0a3946] | 562 | } else {
|
---|
| 563 | // starting from 1 because of logical blocks are numbered from 0
|
---|
| 564 | for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
|
---|
| 565 | rc = ext4_filesystem_release_inode_block(inode_ref, old_blocks_count - i);
|
---|
| 566 | if (rc != EOK) {
|
---|
| 567 | return rc;
|
---|
| 568 | }
|
---|
| 569 | }
|
---|
[3d4fd2c] | 570 | }
|
---|
| 571 |
|
---|
| 572 | ext4_inode_set_size(inode_ref->inode, new_size);
|
---|
| 573 |
|
---|
| 574 | inode_ref->dirty = true;
|
---|
| 575 |
|
---|
| 576 | return EOK;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
[1ac1ab4] | 579 | int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
---|
[b73530a] | 580 | aoff64_t iblock, uint32_t *fblock)
|
---|
[9b9d37bb] | 581 | {
|
---|
| 582 | int rc;
|
---|
[d9bbe45] | 583 |
|
---|
[1ac1ab4] | 584 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[d9bbe45] | 585 |
|
---|
[b73530a] | 586 | if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
|
---|
| 587 | *fblock = 0;
|
---|
| 588 | return EOK;
|
---|
| 589 | }
|
---|
| 590 |
|
---|
[9b9d37bb] | 591 | uint32_t current_block;
|
---|
| 592 |
|
---|
[8958a26] | 593 | /* Handle inode using extents */
|
---|
[a872fc09] | 594 | if (ext4_superblock_has_feature_incompatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
[9104bb5] | 595 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
[1ac1ab4] | 596 | rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
|
---|
[9104bb5] | 597 |
|
---|
| 598 | if (rc != EOK) {
|
---|
| 599 | return rc;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[acd869e] | 602 | *fblock = current_block;
|
---|
| 603 | return EOK;
|
---|
| 604 |
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[9104bb5] | 607 | ext4_inode_t *inode = inode_ref->inode;
|
---|
| 608 |
|
---|
[9b9d37bb] | 609 | /* Handle simple case when we are dealing with direct reference */
|
---|
[e68c834] | 610 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[9b9d37bb] | 611 | current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
|
---|
| 612 | *fblock = current_block;
|
---|
| 613 | return EOK;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | /* Determine the indirection level needed to get the desired block */
|
---|
[d9bbe45] | 617 | int level = -1;
|
---|
| 618 | for (int i = 1; i < 4; i++) {
|
---|
[a9a0982] | 619 | if (iblock < fs->inode_block_limits[i]) {
|
---|
[9b9d37bb] | 620 | level = i;
|
---|
| 621 | break;
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | if (level == -1) {
|
---|
| 626 | return EIO;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | /* Compute offsets for the topmost level */
|
---|
[d9bbe45] | 630 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
[9b9d37bb] | 631 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
[d9bbe45] | 632 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[9b9d37bb] | 633 |
|
---|
[6088193] | 634 | if (current_block == 0) {
|
---|
| 635 | *fblock = 0;
|
---|
| 636 | return EOK;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
[d9bbe45] | 639 | block_t *block;
|
---|
| 640 |
|
---|
[9b9d37bb] | 641 | /* Navigate through other levels, until we find the block number
|
---|
| 642 | * or find null reference meaning we are dealing with sparse file
|
---|
| 643 | */
|
---|
| 644 | while (level > 0) {
|
---|
| 645 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 646 | if (rc != EOK) {
|
---|
| 647 | return rc;
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
| 651 |
|
---|
| 652 | rc = block_put(block);
|
---|
| 653 | if (rc != EOK) {
|
---|
| 654 | return rc;
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | if (current_block == 0) {
|
---|
| 658 | /* This is a sparse file */
|
---|
| 659 | *fblock = 0;
|
---|
| 660 | return EOK;
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | level -= 1;
|
---|
| 664 |
|
---|
| 665 | /* If we are on the last level, break here as
|
---|
| 666 | * there is no next level to visit
|
---|
| 667 | */
|
---|
| 668 | if (level == 0) {
|
---|
| 669 | break;
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 | /* Visit the next level */
|
---|
[a9a0982] | 673 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 674 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[9b9d37bb] | 675 | }
|
---|
| 676 |
|
---|
| 677 | *fblock = current_block;
|
---|
| 678 |
|
---|
[3711e7e] | 679 | return EOK;
|
---|
| 680 | }
|
---|
[6c501f8] | 681 |
|
---|
[d5a78e28] | 682 |
|
---|
[1ac1ab4] | 683 | int ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
---|
| 684 | aoff64_t iblock, uint32_t fblock)
|
---|
[35f48f2] | 685 | {
|
---|
[1e65444] | 686 | int rc;
|
---|
[d9bbe45] | 687 |
|
---|
[1ac1ab4] | 688 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[35f48f2] | 689 |
|
---|
| 690 | /* Handle inode using extents */
|
---|
| 691 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
[1e65444] | 692 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
[ce6de59] | 693 | // not reachable !!!
|
---|
[35f48f2] | 694 | return ENOTSUP;
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | /* Handle simple case when we are dealing with direct reference */
|
---|
| 698 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[1e65444] | 699 | ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
|
---|
| 700 | inode_ref->dirty = true;
|
---|
[35f48f2] | 701 | return EOK;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
[1e65444] | 704 | /* Determine the indirection level needed to get the desired block */
|
---|
[d9bbe45] | 705 | int level = -1;
|
---|
| 706 | for (int i = 1; i < 4; i++) {
|
---|
[1e65444] | 707 | if (iblock < fs->inode_block_limits[i]) {
|
---|
| 708 | level = i;
|
---|
| 709 | break;
|
---|
| 710 | }
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | if (level == -1) {
|
---|
| 714 | return EIO;
|
---|
| 715 | }
|
---|
| 716 |
|
---|
[d9bbe45] | 717 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
[1e65444] | 718 |
|
---|
| 719 | /* Compute offsets for the topmost level */
|
---|
[d9bbe45] | 720 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
| 721 | uint32_t current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
|
---|
| 722 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 723 |
|
---|
| 724 | uint32_t new_block_addr;
|
---|
| 725 | block_t *block, *new_block;
|
---|
[1e65444] | 726 |
|
---|
| 727 | if (current_block == 0) {
|
---|
[1ac1ab4] | 728 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
---|
[1e65444] | 729 | if (rc != EOK) {
|
---|
[e63ce679] | 730 | return rc;
|
---|
[1e65444] | 731 | }
|
---|
| 732 |
|
---|
| 733 | ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
|
---|
| 734 |
|
---|
| 735 | inode_ref->dirty = true;
|
---|
| 736 |
|
---|
| 737 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
| 738 | if (rc != EOK) {
|
---|
[1ac1ab4] | 739 | ext4_balloc_free_block(inode_ref, new_block_addr);
|
---|
[e63ce679] | 740 | return rc;
|
---|
[1e65444] | 741 | }
|
---|
| 742 |
|
---|
| 743 | memset(new_block->data, 0, block_size);
|
---|
| 744 | new_block->dirty = true;
|
---|
| 745 |
|
---|
| 746 | rc = block_put(new_block);
|
---|
| 747 | if (rc != EOK) {
|
---|
[e63ce679] | 748 | return rc;
|
---|
[1e65444] | 749 | }
|
---|
| 750 |
|
---|
| 751 | current_block = new_block_addr;
|
---|
| 752 | }
|
---|
| 753 |
|
---|
| 754 | /* Navigate through other levels, until we find the block number
|
---|
| 755 | * or find null reference meaning we are dealing with sparse file
|
---|
| 756 | */
|
---|
| 757 | while (level > 0) {
|
---|
| 758 |
|
---|
| 759 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 760 | if (rc != EOK) {
|
---|
| 761 | return rc;
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
| 765 |
|
---|
[b12ca16] | 766 | if ((level > 1) && (current_block == 0)) {
|
---|
[1ac1ab4] | 767 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
---|
[b12ca16] | 768 | if (rc != EOK) {
|
---|
[e63ce679] | 769 | block_put(block);
|
---|
| 770 | return rc;
|
---|
[b12ca16] | 771 | }
|
---|
[1e65444] | 772 |
|
---|
[b12ca16] | 773 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
| 774 | if (rc != EOK) {
|
---|
[e63ce679] | 775 | block_put(block);
|
---|
| 776 | return rc;
|
---|
[b12ca16] | 777 | }
|
---|
[e63ce679] | 778 |
|
---|
[b12ca16] | 779 | memset(new_block->data, 0, block_size);
|
---|
| 780 | new_block->dirty = true;
|
---|
[6088193] | 781 |
|
---|
[b12ca16] | 782 | rc = block_put(new_block);
|
---|
| 783 | if (rc != EOK) {
|
---|
[e63ce679] | 784 | block_put(block);
|
---|
| 785 | return rc;
|
---|
[b12ca16] | 786 | }
|
---|
[1e65444] | 787 |
|
---|
[b12ca16] | 788 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
|
---|
| 789 | block->dirty = true;
|
---|
| 790 | current_block = new_block_addr;
|
---|
| 791 | }
|
---|
[1e65444] | 792 |
|
---|
[b12ca16] | 793 | if (level == 1) {
|
---|
| 794 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
|
---|
| 795 | block->dirty = true;
|
---|
[1e65444] | 796 | }
|
---|
| 797 |
|
---|
| 798 | rc = block_put(block);
|
---|
| 799 | if (rc != EOK) {
|
---|
| 800 | return rc;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | level -= 1;
|
---|
| 804 |
|
---|
| 805 | /* If we are on the last level, break here as
|
---|
| 806 | * there is no next level to visit
|
---|
| 807 | */
|
---|
| 808 | if (level == 0) {
|
---|
| 809 | break;
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | /* Visit the next level */
|
---|
| 813 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 814 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 815 | }
|
---|
[35f48f2] | 816 |
|
---|
| 817 | return EOK;
|
---|
| 818 | }
|
---|
| 819 |
|
---|
[1ac1ab4] | 820 | int ext4_filesystem_release_inode_block(
|
---|
[052e82d] | 821 | ext4_inode_ref_t *inode_ref, uint32_t iblock)
|
---|
[d5a78e28] | 822 | {
|
---|
| 823 | int rc;
|
---|
[d9bbe45] | 824 |
|
---|
[fffb061] | 825 | uint32_t fblock;
|
---|
| 826 |
|
---|
[1ac1ab4] | 827 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
| 828 |
|
---|
[ce6de59] | 829 | // EXTENTS are handled otherwise
|
---|
[5b0a3946] | 830 | assert(! (ext4_superblock_has_feature_incompatible(fs->superblock,
|
---|
| 831 | EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
| 832 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)));
|
---|
[12b4a7f] | 833 |
|
---|
[d9bbe45] | 834 | ext4_inode_t *inode = inode_ref->inode;
|
---|
| 835 |
|
---|
[052e82d] | 836 | /* Handle simple case when we are dealing with direct reference */
|
---|
| 837 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[12b4a7f] | 838 | fblock = ext4_inode_get_direct_block(inode, iblock);
|
---|
[052e82d] | 839 | // Sparse file
|
---|
| 840 | if (fblock == 0) {
|
---|
| 841 | return EOK;
|
---|
| 842 | }
|
---|
[d5a78e28] | 843 |
|
---|
[052e82d] | 844 | ext4_inode_set_direct_block(inode, iblock, 0);
|
---|
[1ac1ab4] | 845 | return ext4_balloc_free_block(inode_ref, fblock);
|
---|
[12b4a7f] | 846 | }
|
---|
| 847 |
|
---|
| 848 |
|
---|
| 849 | /* Determine the indirection level needed to get the desired block */
|
---|
[d9bbe45] | 850 | int level = -1;
|
---|
| 851 | for (int i = 1; i < 4; i++) {
|
---|
[12b4a7f] | 852 | if (iblock < fs->inode_block_limits[i]) {
|
---|
| 853 | level = i;
|
---|
| 854 | break;
|
---|
[052e82d] | 855 | }
|
---|
[12b4a7f] | 856 | }
|
---|
| 857 |
|
---|
| 858 | if (level == -1) {
|
---|
| 859 | return EIO;
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | /* Compute offsets for the topmost level */
|
---|
[d9bbe45] | 863 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
| 864 | uint32_t current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
| 865 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[d5a78e28] | 866 |
|
---|
[12b4a7f] | 867 | /* Navigate through other levels, until we find the block number
|
---|
| 868 | * or find null reference meaning we are dealing with sparse file
|
---|
| 869 | */
|
---|
[d9bbe45] | 870 | block_t *block;
|
---|
[12b4a7f] | 871 | while (level > 0) {
|
---|
| 872 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 873 | if (rc != EOK) {
|
---|
| 874 | return rc;
|
---|
[052e82d] | 875 | }
|
---|
[d5a78e28] | 876 |
|
---|
[12b4a7f] | 877 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
[d5a78e28] | 878 |
|
---|
[12b4a7f] | 879 | // Set zero
|
---|
| 880 | if (level == 1) {
|
---|
| 881 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
|
---|
| 882 | block->dirty = true;
|
---|
[052e82d] | 883 | }
|
---|
[d5a78e28] | 884 |
|
---|
[12b4a7f] | 885 | rc = block_put(block);
|
---|
| 886 | if (rc != EOK) {
|
---|
| 887 | return rc;
|
---|
| 888 | }
|
---|
[d5a78e28] | 889 |
|
---|
[12b4a7f] | 890 | level -= 1;
|
---|
| 891 |
|
---|
| 892 | /* If we are on the last level, break here as
|
---|
| 893 | * there is no next level to visit
|
---|
| 894 | */
|
---|
| 895 | if (level == 0) {
|
---|
| 896 | break;
|
---|
[052e82d] | 897 | }
|
---|
[12b4a7f] | 898 |
|
---|
| 899 | /* Visit the next level */
|
---|
| 900 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 901 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 902 | }
|
---|
| 903 |
|
---|
| 904 | fblock = current_block;
|
---|
| 905 |
|
---|
| 906 | if (fblock == 0) {
|
---|
| 907 | return EOK;
|
---|
[052e82d] | 908 | }
|
---|
[d5a78e28] | 909 |
|
---|
[1ac1ab4] | 910 | return ext4_balloc_free_block(inode_ref, fblock);
|
---|
[d5a78e28] | 911 |
|
---|
| 912 | }
|
---|
| 913 |
|
---|
[5b16912] | 914 | int ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
|
---|
| 915 | uint32_t *fblock, uint32_t *iblock)
|
---|
| 916 | {
|
---|
[ce6de59] | 917 | int rc;
|
---|
| 918 |
|
---|
| 919 | // Handle extents separately
|
---|
| 920 | if (ext4_superblock_has_feature_incompatible(
|
---|
| 921 | inode_ref->fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
| 922 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
[5b16912] | 923 |
|
---|
[ce6de59] | 924 | return ext4_extent_append_block(inode_ref, iblock, fblock);
|
---|
[5b16912] | 925 |
|
---|
[ce6de59] | 926 | }
|
---|
[5b16912] | 927 |
|
---|
| 928 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
| 929 |
|
---|
| 930 | // Compute next block index and allocate data block
|
---|
| 931 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
| 932 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 933 |
|
---|
| 934 | assert(inode_size % block_size == 0);
|
---|
| 935 |
|
---|
| 936 | // Logical blocks are numbered from 0
|
---|
| 937 | uint32_t new_block_idx = inode_size / block_size;
|
---|
| 938 |
|
---|
| 939 | uint32_t phys_block;
|
---|
| 940 | rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
|
---|
| 941 | if (rc != EOK) {
|
---|
| 942 | return rc;
|
---|
| 943 | }
|
---|
| 944 |
|
---|
| 945 | rc = ext4_filesystem_set_inode_data_block_index(inode_ref, new_block_idx, phys_block);
|
---|
| 946 | if (rc != EOK) {
|
---|
| 947 | ext4_balloc_free_block(inode_ref, phys_block);
|
---|
| 948 | return rc;
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
|
---|
| 952 |
|
---|
| 953 | inode_ref->dirty = true;
|
---|
| 954 |
|
---|
| 955 | *fblock = phys_block;
|
---|
| 956 | *iblock = new_block_idx;
|
---|
| 957 | return EOK;
|
---|
| 958 | }
|
---|
| 959 |
|
---|
[1ac1ab4] | 960 | int ext4_filesystem_add_orphan(ext4_inode_ref_t *inode_ref)
|
---|
[ebcaff4] | 961 | {
|
---|
[1ac1ab4] | 962 | uint32_t next_orphan = ext4_superblock_get_last_orphan(
|
---|
| 963 | inode_ref->fs->superblock);
|
---|
[ebcaff4] | 964 | ext4_inode_set_deletion_time(inode_ref->inode, next_orphan);
|
---|
[1ac1ab4] | 965 | ext4_superblock_set_last_orphan(
|
---|
| 966 | inode_ref->fs->superblock, inode_ref->index);
|
---|
[ebcaff4] | 967 | inode_ref->dirty = true;
|
---|
| 968 |
|
---|
| 969 | return EOK;
|
---|
| 970 | }
|
---|
| 971 |
|
---|
[1ac1ab4] | 972 | int ext4_filesystem_delete_orphan(ext4_inode_ref_t *inode_ref)
|
---|
[ebcaff4] | 973 | {
|
---|
| 974 | int rc;
|
---|
| 975 |
|
---|
[1ac1ab4] | 976 | uint32_t last_orphan = ext4_superblock_get_last_orphan(
|
---|
| 977 | inode_ref->fs->superblock);
|
---|
[ebcaff4] | 978 | assert(last_orphan > 0);
|
---|
| 979 |
|
---|
| 980 | uint32_t next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
|
---|
| 981 |
|
---|
| 982 | if (last_orphan == inode_ref->index) {
|
---|
[1ac1ab4] | 983 | ext4_superblock_set_last_orphan(inode_ref->fs->superblock, next_orphan);
|
---|
[ebcaff4] | 984 | ext4_inode_set_deletion_time(inode_ref->inode, 0);
|
---|
| 985 | inode_ref->dirty = true;
|
---|
| 986 | return EOK;
|
---|
| 987 | }
|
---|
| 988 |
|
---|
| 989 | ext4_inode_ref_t *current;
|
---|
[1ac1ab4] | 990 | rc = ext4_filesystem_get_inode_ref(inode_ref->fs, last_orphan, ¤t);
|
---|
[ebcaff4] | 991 | if (rc != EOK) {
|
---|
| 992 | return rc;
|
---|
| 993 | }
|
---|
| 994 | next_orphan = ext4_inode_get_deletion_time(current->inode);
|
---|
| 995 |
|
---|
| 996 | bool found;
|
---|
| 997 |
|
---|
| 998 | while (next_orphan != 0) {
|
---|
| 999 | if (next_orphan == inode_ref->index) {
|
---|
| 1000 | next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
|
---|
| 1001 | ext4_inode_set_deletion_time(current->inode, next_orphan);
|
---|
| 1002 | current->dirty = true;
|
---|
| 1003 | found = true;
|
---|
| 1004 | break;
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
| 1007 | ext4_filesystem_put_inode_ref(current);
|
---|
| 1008 |
|
---|
[1ac1ab4] | 1009 | rc = ext4_filesystem_get_inode_ref(inode_ref->fs, next_orphan, ¤t);
|
---|
[ebcaff4] | 1010 | if (rc != EOK) {
|
---|
| 1011 | return rc;
|
---|
| 1012 | }
|
---|
| 1013 | next_orphan = ext4_inode_get_deletion_time(current->inode);
|
---|
| 1014 |
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | ext4_inode_set_deletion_time(inode_ref->inode, 0);
|
---|
| 1018 |
|
---|
| 1019 | rc = ext4_filesystem_put_inode_ref(current);
|
---|
| 1020 | if (rc != EOK) {
|
---|
| 1021 | return rc;
|
---|
| 1022 | }
|
---|
| 1023 |
|
---|
| 1024 | if (!found) {
|
---|
| 1025 | return ENOENT;
|
---|
| 1026 | }
|
---|
| 1027 |
|
---|
| 1028 | return EOK;
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
[6c501f8] | 1031 | /**
|
---|
| 1032 | * @}
|
---|
| 1033 | */
|
---|