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