| 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 |
|
|---|
| 38 | #include <byteorder.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <malloc.h>
|
|---|
| 41 | #include "libext4.h"
|
|---|
| 42 |
|
|---|
| 43 | int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id)
|
|---|
| 44 | {
|
|---|
| 45 |
|
|---|
| 46 | int rc;
|
|---|
| 47 | ext4_superblock_t *temp_superblock;
|
|---|
| 48 | size_t block_size;
|
|---|
| 49 |
|
|---|
| 50 | fs->device = service_id;
|
|---|
| 51 |
|
|---|
| 52 | // TODO what does constant 2048 mean?
|
|---|
| 53 | rc = block_init(EXCHANGE_SERIALIZE, fs->device, 2048);
|
|---|
| 54 | if (rc != EOK) {
|
|---|
| 55 | return rc;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /* Read superblock from device */
|
|---|
| 59 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
|---|
| 60 | if (rc != EOK) {
|
|---|
| 61 | block_fini(fs->device);
|
|---|
| 62 | return rc;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /* Read block size from superblock and check */
|
|---|
| 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 |
|
|---|
| 72 | /* Initialize block caching */
|
|---|
| 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 |
|
|---|
| 79 | /* Return loaded superblock */
|
|---|
| 80 | fs->superblock = temp_superblock;
|
|---|
| 81 |
|
|---|
| 82 | return EOK;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void ext4_filesystem_fini(ext4_filesystem_t *fs)
|
|---|
| 86 | {
|
|---|
| 87 | free(fs->superblock);
|
|---|
| 88 | block_fini(fs->device);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
|
|---|
| 92 | {
|
|---|
| 93 | int rc;
|
|---|
| 94 |
|
|---|
| 95 | rc = ext4_superblock_check_sanity(fs->superblock);
|
|---|
| 96 | if (rc != EOK) {
|
|---|
| 97 | return rc;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | return EOK;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
|
|---|
| 104 | {
|
|---|
| 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 |
|
|---|
| 126 | return EOK;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 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 |
|
|---|
| 161 | int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
|---|
| 162 | ext4_block_group_ref_t **ref)
|
|---|
| 163 | {
|
|---|
| 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 */
|
|---|
| 179 | block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
|---|
| 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;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
|---|
| 199 | ext4_inode_ref_t **ref)
|
|---|
| 200 | {
|
|---|
| 201 | int rc;
|
|---|
| 202 | aoff64_t block_id;
|
|---|
| 203 | uint32_t block_group;
|
|---|
| 204 | uint32_t offset_in_group;
|
|---|
| 205 | uint32_t byte_offset_in_group;
|
|---|
| 206 | size_t offset_in_block;
|
|---|
| 207 | uint32_t inodes_per_group;
|
|---|
| 208 | uint32_t inode_table_start;
|
|---|
| 209 | uint16_t inode_size;
|
|---|
| 210 | uint32_t block_size;
|
|---|
| 211 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 212 | ext4_inode_ref_t *newref;
|
|---|
| 213 |
|
|---|
| 214 | newref = malloc(sizeof(ext4_inode_ref_t));
|
|---|
| 215 | if (newref == NULL) {
|
|---|
| 216 | return ENOMEM;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
|
|---|
| 220 |
|
|---|
| 221 | /* inode numbers are 1-based, but it is simpler to work with 0-based
|
|---|
| 222 | * when computing indices
|
|---|
| 223 | */
|
|---|
| 224 | index -= 1;
|
|---|
| 225 | block_group = index / inodes_per_group;
|
|---|
| 226 | offset_in_group = index % inodes_per_group;
|
|---|
| 227 |
|
|---|
| 228 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
|---|
| 229 | if (rc != EOK) {
|
|---|
| 230 | free(newref);
|
|---|
| 231 | return rc;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | inode_table_start = ext4_block_group_get_inode_table_first_block(
|
|---|
| 235 | bg_ref->block_group);
|
|---|
| 236 |
|
|---|
| 237 | inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
|---|
| 238 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
|---|
| 239 |
|
|---|
| 240 | byte_offset_in_group = offset_in_group * inode_size;
|
|---|
| 241 |
|
|---|
| 242 | block_id = inode_table_start + (byte_offset_in_group / block_size);
|
|---|
| 243 | offset_in_block = byte_offset_in_group % block_size;
|
|---|
| 244 |
|
|---|
| 245 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
|---|
| 246 | if (rc != EOK) {
|
|---|
| 247 | free(newref);
|
|---|
| 248 | return rc;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | newref->inode = newref->block->data + offset_in_block;
|
|---|
| 252 | /* we decremented index above, but need to store the original value
|
|---|
| 253 | * in the reference
|
|---|
| 254 | */
|
|---|
| 255 | newref->index = index+1;
|
|---|
| 256 |
|
|---|
| 257 | *ref = newref;
|
|---|
| 258 |
|
|---|
| 259 | return EOK;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 | int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
|---|
| 264 | {
|
|---|
| 265 | int rc;
|
|---|
| 266 |
|
|---|
| 267 | rc = block_put(ref->block);
|
|---|
| 268 | free(ref);
|
|---|
| 269 |
|
|---|
| 270 | return rc;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
|
|---|
| 274 | aoff64_t iblock, uint32_t* fblock)
|
|---|
| 275 | {
|
|---|
| 276 | int rc;
|
|---|
| 277 | aoff64_t limits[4];
|
|---|
| 278 | uint32_t block_ids_per_block;
|
|---|
| 279 | aoff64_t blocks_per_level[4];
|
|---|
| 280 | uint32_t offset_in_block;
|
|---|
| 281 | uint32_t current_block;
|
|---|
| 282 | aoff64_t block_offset_in_level;
|
|---|
| 283 | int i;
|
|---|
| 284 | int level;
|
|---|
| 285 | block_t *block;
|
|---|
| 286 |
|
|---|
| 287 | /* Handle simple case when we are dealing with direct reference */
|
|---|
| 288 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
|---|
| 289 | current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
|
|---|
| 290 | *fblock = current_block;
|
|---|
| 291 | return EOK;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /* Compute limits for indirect block levels
|
|---|
| 295 | * TODO: compute this once when loading filesystem and store in ext2_filesystem_t
|
|---|
| 296 | */
|
|---|
| 297 | block_ids_per_block = ext4_superblock_get_block_size(fs->superblock) / sizeof(uint32_t);
|
|---|
| 298 | limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
|---|
| 299 | blocks_per_level[0] = 1;
|
|---|
| 300 | for (i = 1; i < 4; i++) {
|
|---|
| 301 | blocks_per_level[i] = blocks_per_level[i-1] *
|
|---|
| 302 | block_ids_per_block;
|
|---|
| 303 | limits[i] = limits[i-1] + blocks_per_level[i];
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | /* Determine the indirection level needed to get the desired block */
|
|---|
| 307 | level = -1;
|
|---|
| 308 | for (i = 1; i < 4; i++) {
|
|---|
| 309 | if (iblock < limits[i]) {
|
|---|
| 310 | level = i;
|
|---|
| 311 | break;
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | if (level == -1) {
|
|---|
| 316 | return EIO;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /* Compute offsets for the topmost level */
|
|---|
| 320 | block_offset_in_level = iblock - limits[level-1];
|
|---|
| 321 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
|---|
| 322 | offset_in_block = block_offset_in_level / blocks_per_level[level-1];
|
|---|
| 323 |
|
|---|
| 324 | /* Navigate through other levels, until we find the block number
|
|---|
| 325 | * or find null reference meaning we are dealing with sparse file
|
|---|
| 326 | */
|
|---|
| 327 | while (level > 0) {
|
|---|
| 328 | rc = block_get(&block, fs->device, current_block, 0);
|
|---|
| 329 | if (rc != EOK) {
|
|---|
| 330 | return rc;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | assert(offset_in_block < block_ids_per_block);
|
|---|
| 334 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
|---|
| 335 |
|
|---|
| 336 | rc = block_put(block);
|
|---|
| 337 | if (rc != EOK) {
|
|---|
| 338 | return rc;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | if (current_block == 0) {
|
|---|
| 342 | /* This is a sparse file */
|
|---|
| 343 | *fblock = 0;
|
|---|
| 344 | return EOK;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | level -= 1;
|
|---|
| 348 |
|
|---|
| 349 | /* If we are on the last level, break here as
|
|---|
| 350 | * there is no next level to visit
|
|---|
| 351 | */
|
|---|
| 352 | if (level == 0) {
|
|---|
| 353 | break;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | /* Visit the next level */
|
|---|
| 357 | block_offset_in_level %= blocks_per_level[level];
|
|---|
| 358 | offset_in_block = block_offset_in_level / blocks_per_level[level-1];
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | *fblock = current_block;
|
|---|
| 362 |
|
|---|
| 363 | return EOK;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | /**
|
|---|
| 367 | * @}
|
|---|
| 368 | */
|
|---|