[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 |
|
---|
| 46 | int rc;
|
---|
| 47 | ext4_superblock_t *temp_superblock;
|
---|
| 48 | size_t block_size;
|
---|
| 49 |
|
---|
| 50 | fs->device = service_id;
|
---|
| 51 |
|
---|
[9c0c0e1] | 52 | // TODO what does constant 2048 mean?
|
---|
[01ab41b] | 53 | rc = block_init(EXCHANGE_SERIALIZE, fs->device, 2048);
|
---|
| 54 | if (rc != EOK) {
|
---|
| 55 | return rc;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[9c0c0e1] | 58 | /* Read superblock from device */
|
---|
[01ab41b] | 59 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
---|
| 60 | if (rc != EOK) {
|
---|
| 61 | block_fini(fs->device);
|
---|
| 62 | return rc;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[9c0c0e1] | 65 | /* Read block size from superblock and check */
|
---|
[01ab41b] | 66 | block_size = ext4_superblock_get_block_size(temp_superblock);
|
---|
| 67 | if (block_size > EXT4_MAX_BLOCK_SIZE) {
|
---|
| 68 | block_fini(fs->device);
|
---|
| 69 | return ENOTSUP;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[9c0c0e1] | 72 | /* Initialize block caching */
|
---|
[01ab41b] | 73 | rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
|
---|
| 74 | if (rc != EOK) {
|
---|
| 75 | block_fini(fs->device);
|
---|
| 76 | return rc;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[9c0c0e1] | 79 | /* Return loaded superblock */
|
---|
[01ab41b] | 80 | fs->superblock = temp_superblock;
|
---|
| 81 |
|
---|
[6c501f8] | 82 | return EOK;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[3711e7e] | 85 | void ext4_filesystem_fini(ext4_filesystem_t *fs)
|
---|
| 86 | {
|
---|
| 87 | free(fs->superblock);
|
---|
| 88 | block_fini(fs->device);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[6c501f8] | 91 | int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
|
---|
| 92 | {
|
---|
[01ab41b] | 93 | int rc;
|
---|
| 94 |
|
---|
| 95 | rc = ext4_superblock_check_sanity(fs->superblock);
|
---|
| 96 | if (rc != EOK) {
|
---|
| 97 | return rc;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[6c501f8] | 100 | return EOK;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[9c0c0e1] | 103 | int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
|
---|
[6c501f8] | 104 | {
|
---|
[9c0c0e1] | 105 | /* Feature flags are present in rev 1 and later */
|
---|
| 106 | if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
|
---|
| 107 | *o_read_only = false;
|
---|
| 108 | return EOK;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | uint32_t incompatible_features;
|
---|
| 112 | incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
|
---|
| 113 | incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
|
---|
| 114 | if (incompatible_features > 0) {
|
---|
| 115 | *o_read_only = true;
|
---|
| 116 | return ENOTSUP;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | uint32_t compatible_read_only;
|
---|
| 120 | compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
|
---|
| 121 | compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
|
---|
| 122 | if (compatible_read_only > 0) {
|
---|
| 123 | *o_read_only = true;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[6c501f8] | 126 | return EOK;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[2ea6392] | 129 | // Feature checkers
|
---|
| 130 | bool ext4_filesystem_has_feature_compatible(ext4_filesystem_t *fs, uint32_t feature)
|
---|
| 131 | {
|
---|
| 132 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 133 |
|
---|
| 134 | if (ext4_superblock_get_features_compatible(sb) & feature) {
|
---|
| 135 | return true;
|
---|
| 136 | }
|
---|
| 137 | return false;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | bool ext4_filesystem_has_feature_incompatible(ext4_filesystem_t *fs, uint32_t feature)
|
---|
| 141 | {
|
---|
| 142 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 143 |
|
---|
| 144 | if (ext4_superblock_get_features_incompatible(sb) & feature) {
|
---|
| 145 | return true;
|
---|
| 146 | }
|
---|
| 147 | return false;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | bool ext4_filesystem_has_feature_read_only(ext4_filesystem_t *fs, uint32_t feature)
|
---|
| 151 | {
|
---|
| 152 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 153 |
|
---|
| 154 | if (ext4_superblock_get_features_read_only(sb) & feature) {
|
---|
| 155 | return true;
|
---|
| 156 | }
|
---|
| 157 | return false;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
[3711e7e] | 161 | int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
---|
| 162 | ext4_block_group_ref_t **ref)
|
---|
[6c501f8] | 163 | {
|
---|
[3711e7e] | 164 | int rc;
|
---|
| 165 | aoff64_t block_id;
|
---|
| 166 | uint32_t descriptors_per_block;
|
---|
| 167 | size_t offset;
|
---|
| 168 | ext4_block_group_ref_t *newref;
|
---|
| 169 |
|
---|
| 170 | newref = malloc(sizeof(ext4_block_group_ref_t));
|
---|
| 171 | if (newref == NULL) {
|
---|
| 172 | return ENOMEM;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
|
---|
| 176 | / EXT4_BLOCK_GROUP_DESCRIPTOR_SIZE;
|
---|
| 177 |
|
---|
| 178 | /* Block group descriptor table starts at the next block after superblock */
|
---|
[3712434] | 179 | block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
---|
[3711e7e] | 180 |
|
---|
| 181 | /* Find the block containing the descriptor we are looking for */
|
---|
| 182 | block_id += bgid / descriptors_per_block;
|
---|
| 183 | offset = (bgid % descriptors_per_block) * EXT4_BLOCK_GROUP_DESCRIPTOR_SIZE;
|
---|
| 184 |
|
---|
| 185 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
| 186 | if (rc != EOK) {
|
---|
| 187 | free(newref);
|
---|
| 188 | return rc;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | newref->block_group = newref->block->data + offset;
|
---|
| 192 |
|
---|
| 193 | *ref = newref;
|
---|
| 194 |
|
---|
| 195 | return EOK;
|
---|
[6c501f8] | 196 | }
|
---|
| 197 |
|
---|
[829d238] | 198 | int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
---|
| 199 | {
|
---|
| 200 | int rc;
|
---|
| 201 |
|
---|
| 202 | rc = block_put(ref->block);
|
---|
| 203 | free(ref);
|
---|
| 204 |
|
---|
| 205 | return rc;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[3711e7e] | 208 | int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
---|
| 209 | ext4_inode_ref_t **ref)
|
---|
| 210 | {
|
---|
| 211 | int rc;
|
---|
| 212 | aoff64_t block_id;
|
---|
| 213 | uint32_t block_group;
|
---|
| 214 | uint32_t offset_in_group;
|
---|
| 215 | uint32_t byte_offset_in_group;
|
---|
| 216 | size_t offset_in_block;
|
---|
| 217 | uint32_t inodes_per_group;
|
---|
| 218 | uint32_t inode_table_start;
|
---|
| 219 | uint16_t inode_size;
|
---|
| 220 | uint32_t block_size;
|
---|
| 221 | ext4_block_group_ref_t *bg_ref;
|
---|
| 222 | ext4_inode_ref_t *newref;
|
---|
| 223 |
|
---|
| 224 | newref = malloc(sizeof(ext4_inode_ref_t));
|
---|
| 225 | if (newref == NULL) {
|
---|
| 226 | return ENOMEM;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
| 230 |
|
---|
| 231 | /* inode numbers are 1-based, but it is simpler to work with 0-based
|
---|
| 232 | * when computing indices
|
---|
| 233 | */
|
---|
| 234 | index -= 1;
|
---|
| 235 | block_group = index / inodes_per_group;
|
---|
| 236 | offset_in_group = index % inodes_per_group;
|
---|
| 237 |
|
---|
| 238 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 239 | if (rc != EOK) {
|
---|
| 240 | free(newref);
|
---|
| 241 | return rc;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | inode_table_start = ext4_block_group_get_inode_table_first_block(
|
---|
| 245 | bg_ref->block_group);
|
---|
| 246 |
|
---|
[829d238] | 247 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 248 | if (rc != EOK) {
|
---|
| 249 | free(newref);
|
---|
| 250 | return rc;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[3711e7e] | 253 | inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
---|
| 254 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 255 |
|
---|
| 256 | byte_offset_in_group = offset_in_group * inode_size;
|
---|
| 257 |
|
---|
| 258 | block_id = inode_table_start + (byte_offset_in_group / block_size);
|
---|
| 259 | offset_in_block = byte_offset_in_group % block_size;
|
---|
| 260 |
|
---|
| 261 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
| 262 | if (rc != EOK) {
|
---|
| 263 | free(newref);
|
---|
| 264 | return rc;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | newref->inode = newref->block->data + offset_in_block;
|
---|
| 268 | /* we decremented index above, but need to store the original value
|
---|
| 269 | * in the reference
|
---|
| 270 | */
|
---|
| 271 | newref->index = index+1;
|
---|
| 272 |
|
---|
| 273 | *ref = newref;
|
---|
| 274 |
|
---|
[9b9d37bb] | 275 | return EOK;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[e68c834] | 278 |
|
---|
[9b9d37bb] | 279 | int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
---|
| 280 | {
|
---|
| 281 | int rc;
|
---|
| 282 |
|
---|
| 283 | rc = block_put(ref->block);
|
---|
| 284 | free(ref);
|
---|
| 285 |
|
---|
| 286 | return rc;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
|
---|
| 290 | aoff64_t iblock, uint32_t* fblock)
|
---|
| 291 | {
|
---|
| 292 | int rc;
|
---|
| 293 | aoff64_t limits[4];
|
---|
| 294 | uint32_t block_ids_per_block;
|
---|
| 295 | aoff64_t blocks_per_level[4];
|
---|
| 296 | uint32_t offset_in_block;
|
---|
| 297 | uint32_t current_block;
|
---|
| 298 | aoff64_t block_offset_in_level;
|
---|
| 299 | int i;
|
---|
| 300 | int level;
|
---|
| 301 | block_t *block;
|
---|
| 302 |
|
---|
[8958a26] | 303 | /* Handle inode using extents */
|
---|
| 304 | // TODO check "extents" feature in superblock ???
|
---|
[acd869e] | 305 | if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
| 306 | current_block = ext4_inode_get_extent_block(inode, iblock);
|
---|
| 307 | *fblock = current_block;
|
---|
| 308 | return EOK;
|
---|
| 309 |
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[9b9d37bb] | 312 | /* Handle simple case when we are dealing with direct reference */
|
---|
[e68c834] | 313 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[9b9d37bb] | 314 | current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
|
---|
| 315 | *fblock = current_block;
|
---|
| 316 | return EOK;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /* Compute limits for indirect block levels
|
---|
| 320 | * TODO: compute this once when loading filesystem and store in ext2_filesystem_t
|
---|
| 321 | */
|
---|
| 322 | block_ids_per_block = ext4_superblock_get_block_size(fs->superblock) / sizeof(uint32_t);
|
---|
[e68c834] | 323 | limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
---|
[9b9d37bb] | 324 | blocks_per_level[0] = 1;
|
---|
| 325 | for (i = 1; i < 4; i++) {
|
---|
| 326 | blocks_per_level[i] = blocks_per_level[i-1] *
|
---|
| 327 | block_ids_per_block;
|
---|
| 328 | limits[i] = limits[i-1] + blocks_per_level[i];
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /* Determine the indirection level needed to get the desired block */
|
---|
| 332 | level = -1;
|
---|
| 333 | for (i = 1; i < 4; i++) {
|
---|
| 334 | if (iblock < limits[i]) {
|
---|
| 335 | level = i;
|
---|
| 336 | break;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | if (level == -1) {
|
---|
| 341 | return EIO;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /* Compute offsets for the topmost level */
|
---|
| 345 | block_offset_in_level = iblock - limits[level-1];
|
---|
| 346 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
| 347 | offset_in_block = block_offset_in_level / blocks_per_level[level-1];
|
---|
| 348 |
|
---|
| 349 | /* Navigate through other levels, until we find the block number
|
---|
| 350 | * or find null reference meaning we are dealing with sparse file
|
---|
| 351 | */
|
---|
| 352 | while (level > 0) {
|
---|
| 353 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
| 354 | if (rc != EOK) {
|
---|
| 355 | return rc;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | assert(offset_in_block < block_ids_per_block);
|
---|
| 359 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
| 360 |
|
---|
| 361 | rc = block_put(block);
|
---|
| 362 | if (rc != EOK) {
|
---|
| 363 | return rc;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | if (current_block == 0) {
|
---|
| 367 | /* This is a sparse file */
|
---|
| 368 | *fblock = 0;
|
---|
| 369 | return EOK;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | level -= 1;
|
---|
| 373 |
|
---|
| 374 | /* If we are on the last level, break here as
|
---|
| 375 | * there is no next level to visit
|
---|
| 376 | */
|
---|
| 377 | if (level == 0) {
|
---|
| 378 | break;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | /* Visit the next level */
|
---|
| 382 | block_offset_in_level %= blocks_per_level[level];
|
---|
| 383 | offset_in_block = block_offset_in_level / blocks_per_level[level-1];
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | *fblock = current_block;
|
---|
| 387 |
|
---|
[3711e7e] | 388 | return EOK;
|
---|
| 389 | }
|
---|
[6c501f8] | 390 |
|
---|
| 391 | /**
|
---|
| 392 | * @}
|
---|
| 393 | */
|
---|