[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
|
---|
| 35 | * @brief TODO
|
---|
| 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 | ext4_superblock_t *temp_superblock;
|
---|
| 47 | size_t block_size;
|
---|
[a9a0982] | 48 | uint32_t block_ids_per_block;
|
---|
| 49 | int i;
|
---|
[01ab41b] | 50 |
|
---|
| 51 | fs->device = service_id;
|
---|
| 52 |
|
---|
[9c0c0e1] | 53 | // TODO what does constant 2048 mean?
|
---|
[01ab41b] | 54 | rc = block_init(EXCHANGE_SERIALIZE, fs->device, 2048);
|
---|
| 55 | if (rc != EOK) {
|
---|
| 56 | return rc;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[9c0c0e1] | 59 | /* Read superblock from device */
|
---|
[01ab41b] | 60 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
---|
| 61 | if (rc != EOK) {
|
---|
| 62 | block_fini(fs->device);
|
---|
| 63 | return rc;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[9c0c0e1] | 66 | /* Read block size from superblock and check */
|
---|
[01ab41b] | 67 | block_size = ext4_superblock_get_block_size(temp_superblock);
|
---|
| 68 | if (block_size > EXT4_MAX_BLOCK_SIZE) {
|
---|
| 69 | block_fini(fs->device);
|
---|
| 70 | return ENOTSUP;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[9c0c0e1] | 73 | /* Initialize block caching */
|
---|
[b12ca16] | 74 | rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
|
---|
[01ab41b] | 75 | if (rc != EOK) {
|
---|
| 76 | block_fini(fs->device);
|
---|
| 77 | return rc;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[a9a0982] | 80 | block_ids_per_block = block_size / sizeof(uint32_t);
|
---|
| 81 | fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
---|
| 82 | fs->inode_blocks_per_level[0] = 1;
|
---|
| 83 | for (i = 1; i < 4; i++) {
|
---|
[6088193] | 84 | fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i-1] *
|
---|
[a9a0982] | 85 | block_ids_per_block;
|
---|
| 86 | fs->inode_block_limits[i] = fs->inode_block_limits[i-1] +
|
---|
| 87 | fs->inode_blocks_per_level[i];
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[9c0c0e1] | 90 | /* Return loaded superblock */
|
---|
[01ab41b] | 91 | fs->superblock = temp_superblock;
|
---|
| 92 |
|
---|
[6c501f8] | 93 | return EOK;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[ae3d4f8] | 96 | int ext4_filesystem_fini(ext4_filesystem_t *fs, bool write_sb)
|
---|
[3711e7e] | 97 | {
|
---|
[ae3d4f8] | 98 | int rc = EOK;
|
---|
| 99 | if (write_sb) {
|
---|
| 100 | rc = ext4_superblock_write_direct(fs->device, fs->superblock);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[3711e7e] | 103 | free(fs->superblock);
|
---|
| 104 | block_fini(fs->device);
|
---|
[ae3d4f8] | 105 |
|
---|
| 106 | return rc;
|
---|
[3711e7e] | 107 | }
|
---|
| 108 |
|
---|
[6c501f8] | 109 | int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
|
---|
| 110 | {
|
---|
[01ab41b] | 111 | int rc;
|
---|
| 112 |
|
---|
| 113 | rc = ext4_superblock_check_sanity(fs->superblock);
|
---|
| 114 | if (rc != EOK) {
|
---|
| 115 | return rc;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[6c501f8] | 118 | return EOK;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[9c0c0e1] | 121 | int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
|
---|
[6c501f8] | 122 | {
|
---|
[9c0c0e1] | 123 | /* Feature flags are present in rev 1 and later */
|
---|
| 124 | if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
|
---|
| 125 | *o_read_only = false;
|
---|
| 126 | return EOK;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | uint32_t incompatible_features;
|
---|
| 130 | incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
|
---|
| 131 | incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
|
---|
| 132 | if (incompatible_features > 0) {
|
---|
| 133 | *o_read_only = true;
|
---|
| 134 | return ENOTSUP;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | uint32_t compatible_read_only;
|
---|
| 138 | compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
|
---|
| 139 | compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
|
---|
| 140 | if (compatible_read_only > 0) {
|
---|
| 141 | *o_read_only = true;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[6c501f8] | 144 | return EOK;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[3711e7e] | 147 | int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
---|
| 148 | ext4_block_group_ref_t **ref)
|
---|
[6c501f8] | 149 | {
|
---|
[3711e7e] | 150 | int rc;
|
---|
| 151 | aoff64_t block_id;
|
---|
| 152 | uint32_t descriptors_per_block;
|
---|
| 153 | size_t offset;
|
---|
| 154 | ext4_block_group_ref_t *newref;
|
---|
| 155 |
|
---|
| 156 | newref = malloc(sizeof(ext4_block_group_ref_t));
|
---|
| 157 | if (newref == NULL) {
|
---|
| 158 | return ENOMEM;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
|
---|
[c25e39b] | 162 | / ext4_superblock_get_desc_size(fs->superblock);
|
---|
[3711e7e] | 163 |
|
---|
| 164 | /* Block group descriptor table starts at the next block after superblock */
|
---|
[3712434] | 165 | block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
---|
[3711e7e] | 166 |
|
---|
| 167 | /* Find the block containing the descriptor we are looking for */
|
---|
| 168 | block_id += bgid / descriptors_per_block;
|
---|
[c25e39b] | 169 | offset = (bgid % descriptors_per_block) * ext4_superblock_get_desc_size(fs->superblock);
|
---|
[3711e7e] | 170 |
|
---|
| 171 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
| 172 | if (rc != EOK) {
|
---|
| 173 | free(newref);
|
---|
| 174 | return rc;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | newref->block_group = newref->block->data + offset;
|
---|
[12b4a7f] | 178 | newref->dirty = false;
|
---|
[3711e7e] | 179 |
|
---|
| 180 | *ref = newref;
|
---|
| 181 |
|
---|
| 182 | return EOK;
|
---|
[6c501f8] | 183 | }
|
---|
| 184 |
|
---|
[829d238] | 185 | int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
---|
| 186 | {
|
---|
| 187 | int rc;
|
---|
| 188 |
|
---|
[12b4a7f] | 189 | if (ref->dirty) {
|
---|
| 190 | ref->block->dirty = true;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[829d238] | 193 | rc = block_put(ref->block);
|
---|
| 194 | free(ref);
|
---|
| 195 |
|
---|
| 196 | return rc;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[3711e7e] | 199 | int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
---|
| 200 | ext4_inode_ref_t **ref)
|
---|
| 201 | {
|
---|
| 202 | int rc;
|
---|
| 203 | aoff64_t block_id;
|
---|
| 204 | uint32_t block_group;
|
---|
| 205 | uint32_t offset_in_group;
|
---|
| 206 | uint32_t byte_offset_in_group;
|
---|
| 207 | size_t offset_in_block;
|
---|
| 208 | uint32_t inodes_per_group;
|
---|
| 209 | uint32_t inode_table_start;
|
---|
| 210 | uint16_t inode_size;
|
---|
| 211 | uint32_t block_size;
|
---|
| 212 | ext4_block_group_ref_t *bg_ref;
|
---|
| 213 | ext4_inode_ref_t *newref;
|
---|
| 214 |
|
---|
| 215 | newref = malloc(sizeof(ext4_inode_ref_t));
|
---|
| 216 | if (newref == NULL) {
|
---|
| 217 | return ENOMEM;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
| 221 |
|
---|
| 222 | /* inode numbers are 1-based, but it is simpler to work with 0-based
|
---|
| 223 | * when computing indices
|
---|
| 224 | */
|
---|
| 225 | index -= 1;
|
---|
| 226 | block_group = index / inodes_per_group;
|
---|
| 227 | offset_in_group = index % inodes_per_group;
|
---|
| 228 |
|
---|
| 229 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 230 | if (rc != EOK) {
|
---|
| 231 | free(newref);
|
---|
| 232 | return rc;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | inode_table_start = ext4_block_group_get_inode_table_first_block(
|
---|
[fe27eb4] | 236 | bg_ref->block_group, fs->superblock);
|
---|
[3711e7e] | 237 |
|
---|
[829d238] | 238 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 239 | if (rc != EOK) {
|
---|
| 240 | free(newref);
|
---|
| 241 | return rc;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[3711e7e] | 244 | inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
---|
| 245 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 246 |
|
---|
| 247 | byte_offset_in_group = offset_in_group * inode_size;
|
---|
| 248 |
|
---|
| 249 | block_id = inode_table_start + (byte_offset_in_group / block_size);
|
---|
| 250 | offset_in_block = byte_offset_in_group % block_size;
|
---|
| 251 |
|
---|
| 252 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
| 253 | if (rc != EOK) {
|
---|
| 254 | free(newref);
|
---|
| 255 | return rc;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | newref->inode = newref->block->data + offset_in_block;
|
---|
| 259 | /* we decremented index above, but need to store the original value
|
---|
| 260 | * in the reference
|
---|
| 261 | */
|
---|
| 262 | newref->index = index+1;
|
---|
[052e82d] | 263 | newref->dirty = false;
|
---|
[3711e7e] | 264 |
|
---|
| 265 | *ref = newref;
|
---|
| 266 |
|
---|
[9b9d37bb] | 267 | return EOK;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[e68c834] | 270 |
|
---|
[9b9d37bb] | 271 | int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
---|
| 272 | {
|
---|
| 273 | int rc;
|
---|
| 274 |
|
---|
[052e82d] | 275 | if (ref->dirty) {
|
---|
| 276 | ref->block->dirty = true;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[9b9d37bb] | 279 | rc = block_put(ref->block);
|
---|
| 280 | free(ref);
|
---|
| 281 |
|
---|
| 282 | return rc;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[3d4fd2c] | 285 | int ext4_filesystem_free_inode(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
|
---|
| 286 | {
|
---|
| 287 | int rc;
|
---|
| 288 | // release all indirect blocks
|
---|
| 289 |
|
---|
| 290 | uint32_t fblock;
|
---|
| 291 |
|
---|
| 292 | // 1) Single indirect
|
---|
| 293 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
|
---|
| 294 | if (fblock != 0) {
|
---|
| 295 | rc = ext4_balloc_free_block(fs, inode_ref, fblock);
|
---|
| 296 | if (rc != EOK) {
|
---|
| 297 | // TODO error
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | block_t *block;
|
---|
| 304 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 305 | uint32_t count = block_size / sizeof(uint32_t);
|
---|
| 306 |
|
---|
| 307 | // 2) Double indirect
|
---|
| 308 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
|
---|
| 309 | if (fblock != 0) {
|
---|
| 310 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 311 | if (rc != EOK) {
|
---|
| 312 | // TODO error
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | uint32_t ind_block;
|
---|
| 316 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
| 317 | ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
|
---|
| 318 |
|
---|
| 319 | if (ind_block != 0) {
|
---|
| 320 | rc = ext4_balloc_free_block(fs, inode_ref, ind_block);
|
---|
| 321 | if (rc != EOK) {
|
---|
| 322 | // TODO error
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | block_put(block);
|
---|
| 328 | rc = ext4_balloc_free_block(fs, inode_ref, fblock);
|
---|
| 329 | if (rc != EOK) {
|
---|
| 330 | // TODO error
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 |
|
---|
| 337 | // 3) Tripple indirect
|
---|
| 338 | block_t *subblock;
|
---|
| 339 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
|
---|
| 340 | if (fblock != 0) {
|
---|
| 341 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
| 342 | if (rc != EOK) {
|
---|
| 343 | // TODO error
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | uint32_t ind_block;
|
---|
| 347 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
| 348 | ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
|
---|
| 349 |
|
---|
| 350 | if (ind_block != 0) {
|
---|
| 351 | rc = block_get(&subblock, fs->device, ind_block, BLOCK_FLAGS_NONE);
|
---|
| 352 | if (rc != EOK) {
|
---|
| 353 | // TODO error
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | uint32_t ind_subblock;
|
---|
| 357 | for (uint32_t suboffset = 0; suboffset < count; ++suboffset) {
|
---|
| 358 | ind_subblock = uint32_t_le2host(((uint32_t*)subblock->data)[suboffset]);
|
---|
| 359 |
|
---|
| 360 | if (ind_subblock != 0) {
|
---|
| 361 | rc = ext4_balloc_free_block(fs, inode_ref, ind_subblock);
|
---|
| 362 | if (rc != EOK) {
|
---|
| 363 | // TODO error
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | }
|
---|
| 368 | block_put(subblock);
|
---|
| 369 |
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | rc = ext4_balloc_free_block(fs, inode_ref, ind_block);
|
---|
| 373 | if (rc != EOK) {
|
---|
| 374 | // TODO error
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 |
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | block_put(block);
|
---|
| 381 | rc = ext4_balloc_free_block(fs, inode_ref, fblock);
|
---|
| 382 | if (rc != EOK) {
|
---|
| 383 | // TODO error
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[e5f8762] | 389 | inode_ref->dirty = true;
|
---|
| 390 |
|
---|
[3d4fd2c] | 391 | // Free inode
|
---|
| 392 | rc = ext4_ialloc_free_inode(fs, inode_ref);
|
---|
| 393 | if (rc != EOK) {
|
---|
| 394 | return rc;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | return EOK;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | int ext4_filesystem_truncate_inode(ext4_filesystem_t *fs,
|
---|
| 401 | ext4_inode_ref_t *inode_ref, aoff64_t new_size)
|
---|
| 402 | {
|
---|
| 403 | aoff64_t old_size;
|
---|
| 404 | aoff64_t size_diff;
|
---|
| 405 |
|
---|
| 406 | if (! ext4_inode_can_truncate(fs->superblock, inode_ref->inode)) {
|
---|
| 407 | // Unable to truncate
|
---|
| 408 | return EINVAL;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | old_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
|
---|
| 412 |
|
---|
| 413 | if (old_size == new_size) {
|
---|
| 414 | // Nothing to do
|
---|
| 415 | return EOK;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | uint32_t block_size;
|
---|
| 419 | uint32_t blocks_count, total_blocks;
|
---|
| 420 | uint32_t i;
|
---|
| 421 |
|
---|
| 422 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 423 |
|
---|
| 424 | if (old_size < new_size) {
|
---|
| 425 | // Currently not supported to expand the file
|
---|
| 426 | // TODO
|
---|
| 427 | EXT4FS_DBG("trying to expand the file");
|
---|
| 428 | return EINVAL;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | size_diff = old_size - new_size;
|
---|
| 432 | blocks_count = size_diff / block_size;
|
---|
| 433 | if (size_diff % block_size != 0) {
|
---|
| 434 | blocks_count++;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | total_blocks = old_size / block_size;
|
---|
| 438 | if (old_size % block_size != 0) {
|
---|
| 439 | total_blocks++;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | // starting from 1 because of logical blocks are numbered from 0
|
---|
| 443 | for (i = 1; i <= blocks_count; ++i) {
|
---|
| 444 | // TODO check retval
|
---|
| 445 | ext4_filesystem_release_inode_block(fs, inode_ref, total_blocks - i);
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | ext4_inode_set_size(inode_ref->inode, new_size);
|
---|
| 449 |
|
---|
| 450 | inode_ref->dirty = true;
|
---|
| 451 |
|
---|
| 452 | return EOK;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[9b9d37bb] | 455 | int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
|
---|
| 456 | aoff64_t iblock, uint32_t* fblock)
|
---|
| 457 | {
|
---|
| 458 | int rc;
|
---|
| 459 | uint32_t offset_in_block;
|
---|
| 460 | uint32_t current_block;
|
---|
| 461 | aoff64_t block_offset_in_level;
|
---|
| 462 | int i;
|
---|
| 463 | int level;
|
---|
| 464 | block_t *block;
|
---|
| 465 |
|
---|
[8958a26] | 466 | /* Handle inode using extents */
|
---|
[c25e39b] | 467 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
| 468 | ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
[1a7756a] | 469 | current_block = ext4_inode_get_extent_block(inode, iblock, fs->device);
|
---|
[acd869e] | 470 | *fblock = current_block;
|
---|
| 471 | return EOK;
|
---|
| 472 |
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[9b9d37bb] | 475 | /* Handle simple case when we are dealing with direct reference */
|
---|
[e68c834] | 476 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[9b9d37bb] | 477 | current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
|
---|
| 478 | *fblock = current_block;
|
---|
| 479 | return EOK;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | /* Determine the indirection level needed to get the desired block */
|
---|
| 483 | level = -1;
|
---|
| 484 | for (i = 1; i < 4; i++) {
|
---|
[a9a0982] | 485 | if (iblock < fs->inode_block_limits[i]) {
|
---|
[9b9d37bb] | 486 | level = i;
|
---|
| 487 | break;
|
---|
| 488 | }
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | if (level == -1) {
|
---|
| 492 | return EIO;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | /* Compute offsets for the topmost level */
|
---|
[a9a0982] | 496 | block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
[9b9d37bb] | 497 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
[a9a0982] | 498 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[9b9d37bb] | 499 |
|
---|
[6088193] | 500 | if (current_block == 0) {
|
---|
| 501 | *fblock = 0;
|
---|
| 502 | return EOK;
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[9b9d37bb] | 505 | /* Navigate through other levels, until we find the block number
|
---|
| 506 | * or find null reference meaning we are dealing with sparse file
|
---|
| 507 | */
|
---|
| 508 | while (level > 0) {
|
---|
| 509 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 510 | if (rc != EOK) {
|
---|
| 511 | return rc;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
| 515 |
|
---|
| 516 | rc = block_put(block);
|
---|
| 517 | if (rc != EOK) {
|
---|
| 518 | return rc;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | if (current_block == 0) {
|
---|
| 522 | /* This is a sparse file */
|
---|
| 523 | *fblock = 0;
|
---|
| 524 | return EOK;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | level -= 1;
|
---|
| 528 |
|
---|
| 529 | /* If we are on the last level, break here as
|
---|
| 530 | * there is no next level to visit
|
---|
| 531 | */
|
---|
| 532 | if (level == 0) {
|
---|
| 533 | break;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | /* Visit the next level */
|
---|
[a9a0982] | 537 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 538 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[9b9d37bb] | 539 | }
|
---|
| 540 |
|
---|
| 541 | *fblock = current_block;
|
---|
| 542 |
|
---|
[3711e7e] | 543 | return EOK;
|
---|
| 544 | }
|
---|
[6c501f8] | 545 |
|
---|
[d5a78e28] | 546 |
|
---|
[35f48f2] | 547 | int ext4_filesystem_set_inode_data_block_index(ext4_filesystem_t *fs,
|
---|
[1e65444] | 548 | ext4_inode_ref_t *inode_ref, aoff64_t iblock, uint32_t fblock)
|
---|
[35f48f2] | 549 | {
|
---|
| 550 |
|
---|
[1e65444] | 551 | int rc;
|
---|
| 552 | uint32_t offset_in_block;
|
---|
| 553 | uint32_t current_block, new_block_addr;
|
---|
| 554 | uint32_t block_size;
|
---|
| 555 | aoff64_t block_offset_in_level;
|
---|
| 556 | int i;
|
---|
| 557 | int level;
|
---|
| 558 | block_t *block, *new_block;
|
---|
[35f48f2] | 559 |
|
---|
| 560 | /* Handle inode using extents */
|
---|
| 561 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
[1e65444] | 562 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
[35f48f2] | 563 | // TODO
|
---|
| 564 | return ENOTSUP;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | /* Handle simple case when we are dealing with direct reference */
|
---|
| 568 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[1e65444] | 569 | ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
|
---|
| 570 | inode_ref->dirty = true;
|
---|
[35f48f2] | 571 | return EOK;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
[1e65444] | 574 | /* Determine the indirection level needed to get the desired block */
|
---|
| 575 | level = -1;
|
---|
| 576 | for (i = 1; i < 4; i++) {
|
---|
| 577 | if (iblock < fs->inode_block_limits[i]) {
|
---|
| 578 | level = i;
|
---|
| 579 | break;
|
---|
| 580 | }
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | if (level == -1) {
|
---|
| 584 | return EIO;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 588 |
|
---|
| 589 | /* Compute offsets for the topmost level */
|
---|
| 590 | block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
| 591 | current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
|
---|
| 592 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 593 |
|
---|
| 594 | if (current_block == 0) {
|
---|
[b12ca16] | 595 | rc = ext4_balloc_alloc_block(fs, inode_ref, &new_block_addr);
|
---|
[1e65444] | 596 | if (rc != EOK) {
|
---|
| 597 | // TODO error
|
---|
[6088193] | 598 | EXT4FS_DBG("error in allocation");
|
---|
[1e65444] | 599 | }
|
---|
[6088193] | 600 | // EXT4FS_DBG("AAA: new addr \%u, level = \%u", new_block_addr, level);
|
---|
[1e65444] | 601 |
|
---|
| 602 | ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
|
---|
| 603 |
|
---|
| 604 | inode_ref->dirty = true;
|
---|
| 605 |
|
---|
| 606 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
| 607 | if (rc != EOK) {
|
---|
| 608 | EXT4FS_DBG("block load error");
|
---|
| 609 | // TODO error
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | memset(new_block->data, 0, block_size);
|
---|
| 613 | new_block->dirty = true;
|
---|
| 614 |
|
---|
| 615 | rc = block_put(new_block);
|
---|
| 616 | if (rc != EOK) {
|
---|
| 617 | EXT4FS_DBG("block put error");
|
---|
| 618 | }
|
---|
| 619 |
|
---|
[6088193] | 620 | // EXT4FS_DBG("allocated indirect block for level \%u, during setting iblock \%u", level, (uint32_t)iblock);
|
---|
| 621 |
|
---|
[1e65444] | 622 | current_block = new_block_addr;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | /* Navigate through other levels, until we find the block number
|
---|
| 626 | * or find null reference meaning we are dealing with sparse file
|
---|
| 627 | */
|
---|
| 628 | while (level > 0) {
|
---|
| 629 |
|
---|
| 630 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 631 | if (rc != EOK) {
|
---|
| 632 | return rc;
|
---|
| 633 | }
|
---|
| 634 |
|
---|
| 635 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
| 636 |
|
---|
[b12ca16] | 637 | if ((level > 1) && (current_block == 0)) {
|
---|
| 638 | rc = ext4_balloc_alloc_block(fs, inode_ref, &new_block_addr);
|
---|
| 639 | if (rc != EOK) {
|
---|
| 640 | // TODO error
|
---|
| 641 | EXT4FS_DBG("allocation error");
|
---|
| 642 | }
|
---|
[ae3d4f8] | 643 | // EXT4FS_DBG("BBB: new addr \%u, offset = \%u, level = \%u", new_block_addr, offset_in_block, level);
|
---|
[1e65444] | 644 |
|
---|
[b12ca16] | 645 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
| 646 | if (rc != EOK) {
|
---|
| 647 | // TODO error
|
---|
[1e65444] | 648 |
|
---|
[b12ca16] | 649 | EXT4FS_DBG("BBB: error block loading");
|
---|
[6088193] | 650 |
|
---|
[b12ca16] | 651 | }
|
---|
| 652 | memset(new_block->data, 0, block_size);
|
---|
| 653 | new_block->dirty = true;
|
---|
[6088193] | 654 |
|
---|
[b12ca16] | 655 | rc = block_put(new_block);
|
---|
| 656 | if (rc != EOK) {
|
---|
| 657 | EXT4FS_DBG("BBB: error indirect block saving");
|
---|
| 658 | }
|
---|
[1e65444] | 659 |
|
---|
[b12ca16] | 660 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
|
---|
| 661 | block->dirty = true;
|
---|
| 662 | current_block = new_block_addr;
|
---|
| 663 | }
|
---|
[1e65444] | 664 |
|
---|
[b12ca16] | 665 | if (level == 1) {
|
---|
| 666 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
|
---|
| 667 | block->dirty = true;
|
---|
[1e65444] | 668 | }
|
---|
| 669 |
|
---|
| 670 | rc = block_put(block);
|
---|
| 671 | if (rc != EOK) {
|
---|
| 672 | return rc;
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | level -= 1;
|
---|
| 676 |
|
---|
| 677 | /* If we are on the last level, break here as
|
---|
| 678 | * there is no next level to visit
|
---|
| 679 | */
|
---|
| 680 | if (level == 0) {
|
---|
| 681 | break;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | /* Visit the next level */
|
---|
| 685 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 686 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 687 | }
|
---|
[35f48f2] | 688 |
|
---|
| 689 | return EOK;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[052e82d] | 692 | int ext4_filesystem_release_inode_block(ext4_filesystem_t *fs,
|
---|
| 693 | ext4_inode_ref_t *inode_ref, uint32_t iblock)
|
---|
[d5a78e28] | 694 | {
|
---|
| 695 | int rc;
|
---|
| 696 | uint32_t fblock;
|
---|
[052e82d] | 697 | int i;
|
---|
| 698 | int level;
|
---|
| 699 | aoff64_t block_offset_in_level;
|
---|
| 700 | uint32_t current_block;
|
---|
| 701 | uint32_t offset_in_block;
|
---|
| 702 | block_t *block;
|
---|
| 703 | ext4_inode_t *inode = inode_ref->inode;
|
---|
[d5a78e28] | 704 |
|
---|
[052e82d] | 705 | /* TODO handle extents */
|
---|
[d5a78e28] | 706 |
|
---|
[12b4a7f] | 707 |
|
---|
[052e82d] | 708 | /* Handle simple case when we are dealing with direct reference */
|
---|
| 709 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[12b4a7f] | 710 | fblock = ext4_inode_get_direct_block(inode, iblock);
|
---|
[052e82d] | 711 | // Sparse file
|
---|
| 712 | if (fblock == 0) {
|
---|
| 713 | return EOK;
|
---|
| 714 | }
|
---|
[d5a78e28] | 715 |
|
---|
[052e82d] | 716 | ext4_inode_set_direct_block(inode, iblock, 0);
|
---|
[b12ca16] | 717 | return ext4_balloc_free_block(fs, inode_ref, fblock);
|
---|
[12b4a7f] | 718 | }
|
---|
| 719 |
|
---|
| 720 |
|
---|
| 721 | /* Determine the indirection level needed to get the desired block */
|
---|
| 722 | level = -1;
|
---|
| 723 | for (i = 1; i < 4; i++) {
|
---|
| 724 | if (iblock < fs->inode_block_limits[i]) {
|
---|
| 725 | level = i;
|
---|
| 726 | break;
|
---|
[052e82d] | 727 | }
|
---|
[12b4a7f] | 728 | }
|
---|
| 729 |
|
---|
| 730 | if (level == -1) {
|
---|
| 731 | return EIO;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | /* Compute offsets for the topmost level */
|
---|
| 735 | block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
| 736 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
| 737 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
[d5a78e28] | 738 |
|
---|
[12b4a7f] | 739 | /* Navigate through other levels, until we find the block number
|
---|
| 740 | * or find null reference meaning we are dealing with sparse file
|
---|
| 741 | */
|
---|
| 742 | while (level > 0) {
|
---|
| 743 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 744 | if (rc != EOK) {
|
---|
| 745 | return rc;
|
---|
[052e82d] | 746 | }
|
---|
[d5a78e28] | 747 |
|
---|
[12b4a7f] | 748 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
[d5a78e28] | 749 |
|
---|
[12b4a7f] | 750 | // Set zero
|
---|
| 751 | if (level == 1) {
|
---|
| 752 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
|
---|
| 753 | block->dirty = true;
|
---|
[052e82d] | 754 | }
|
---|
[d5a78e28] | 755 |
|
---|
[12b4a7f] | 756 | rc = block_put(block);
|
---|
| 757 | if (rc != EOK) {
|
---|
| 758 | return rc;
|
---|
| 759 | }
|
---|
[d5a78e28] | 760 |
|
---|
[12b4a7f] | 761 | level -= 1;
|
---|
| 762 |
|
---|
| 763 | /* If we are on the last level, break here as
|
---|
| 764 | * there is no next level to visit
|
---|
| 765 | */
|
---|
| 766 | if (level == 0) {
|
---|
| 767 | break;
|
---|
[052e82d] | 768 | }
|
---|
[12b4a7f] | 769 |
|
---|
| 770 | /* Visit the next level */
|
---|
| 771 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
| 772 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
| 773 | }
|
---|
| 774 |
|
---|
| 775 | fblock = current_block;
|
---|
| 776 |
|
---|
| 777 | if (fblock == 0) {
|
---|
| 778 | return EOK;
|
---|
[052e82d] | 779 | }
|
---|
[d5a78e28] | 780 |
|
---|
[b12ca16] | 781 | return ext4_balloc_free_block(fs, inode_ref, fblock);
|
---|
[d5a78e28] | 782 |
|
---|
| 783 | }
|
---|
| 784 |
|
---|
[6c501f8] | 785 | /**
|
---|
| 786 | * @}
|
---|
| 787 | */
|
---|