| 1 | /*
|
|---|
| 2 | * Copyright (c) 2018 Jiri Svoboda
|
|---|
| 3 | * Copyright (c) 2011 Martin Sucha
|
|---|
| 4 | * Copyright (c) 2012 Frantisek Princ
|
|---|
| 5 | * All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 8 | * modification, are permitted provided that the following conditions
|
|---|
| 9 | * are met:
|
|---|
| 10 | *
|
|---|
| 11 | * - Redistributions of source code must retain the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 15 | * documentation and/or other materials provided with the distribution.
|
|---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 17 | * derived from this software without specific prior written permission.
|
|---|
| 18 | *
|
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | /** @addtogroup libext4
|
|---|
| 32 | * @{
|
|---|
| 33 | */
|
|---|
| 34 | /**
|
|---|
| 35 | * @file filesystem.c
|
|---|
| 36 | * @brief More complex filesystem operations.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <byteorder.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <mem.h>
|
|---|
| 42 | #include <align.h>
|
|---|
| 43 | #include <crypto.h>
|
|---|
| 44 | #include <ipc/vfs.h>
|
|---|
| 45 | #include <libfs.h>
|
|---|
| 46 | #include <stdlib.h>
|
|---|
| 47 | #include "ext4/balloc.h"
|
|---|
| 48 | #include "ext4/bitmap.h"
|
|---|
| 49 | #include "ext4/block_group.h"
|
|---|
| 50 | #include "ext4/cfg.h"
|
|---|
| 51 | #include "ext4/directory.h"
|
|---|
| 52 | #include "ext4/extent.h"
|
|---|
| 53 | #include "ext4/filesystem.h"
|
|---|
| 54 | #include "ext4/ialloc.h"
|
|---|
| 55 | #include "ext4/inode.h"
|
|---|
| 56 | #include "ext4/ops.h"
|
|---|
| 57 | #include "ext4/superblock.h"
|
|---|
| 58 |
|
|---|
| 59 | static errno_t ext4_filesystem_check_features(ext4_filesystem_t *, bool *);
|
|---|
| 60 | static errno_t ext4_filesystem_init_block_groups(ext4_filesystem_t *);
|
|---|
| 61 | static errno_t ext4_filesystem_alloc_this_inode(ext4_filesystem_t *,
|
|---|
| 62 | uint32_t, ext4_inode_ref_t **, int);
|
|---|
| 63 | static uint32_t ext4_filesystem_inodes_per_block(ext4_superblock_t *);
|
|---|
| 64 |
|
|---|
| 65 | /** Initialize filesystem for opening.
|
|---|
| 66 | *
|
|---|
| 67 | * But do not mark mounted just yet.
|
|---|
| 68 | *
|
|---|
| 69 | * @param fs Filesystem instance to be initialized
|
|---|
| 70 | * @param service_id Block device to open
|
|---|
| 71 | * @param cmode Cache mode
|
|---|
| 72 | *
|
|---|
| 73 | * @return Error code
|
|---|
| 74 | *
|
|---|
| 75 | */
|
|---|
| 76 | static errno_t ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id,
|
|---|
| 77 | enum cache_mode cmode)
|
|---|
| 78 | {
|
|---|
| 79 | errno_t rc;
|
|---|
| 80 | ext4_superblock_t *temp_superblock = NULL;
|
|---|
| 81 |
|
|---|
| 82 | fs->device = service_id;
|
|---|
| 83 |
|
|---|
| 84 | /* Initialize block library (4096 is size of communication channel) */
|
|---|
| 85 | rc = block_init(fs->device, 4096);
|
|---|
| 86 | if (rc != EOK)
|
|---|
| 87 | goto err;
|
|---|
| 88 |
|
|---|
| 89 | /* Read superblock from device to memory */
|
|---|
| 90 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
|---|
| 91 | if (rc != EOK)
|
|---|
| 92 | goto err_1;
|
|---|
| 93 |
|
|---|
| 94 | /* Read block size from superblock and check */
|
|---|
| 95 | uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
|
|---|
| 96 | if (block_size > EXT4_MAX_BLOCK_SIZE) {
|
|---|
| 97 | rc = ENOTSUP;
|
|---|
| 98 | goto err_1;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /* Initialize block caching by libblock */
|
|---|
| 102 | rc = block_cache_init(service_id, block_size, 0, cmode);
|
|---|
| 103 | if (rc != EOK)
|
|---|
| 104 | goto err_1;
|
|---|
| 105 |
|
|---|
| 106 | /* Compute limits for indirect block levels */
|
|---|
| 107 | uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
|
|---|
| 108 | fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
|---|
| 109 | fs->inode_blocks_per_level[0] = 1;
|
|---|
| 110 | for (unsigned int i = 1; i < 4; i++) {
|
|---|
| 111 | fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
|
|---|
| 112 | block_ids_per_block;
|
|---|
| 113 | fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
|
|---|
| 114 | fs->inode_blocks_per_level[i];
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /* Return loaded superblock */
|
|---|
| 118 | fs->superblock = temp_superblock;
|
|---|
| 119 |
|
|---|
| 120 | uint16_t state = ext4_superblock_get_state(fs->superblock);
|
|---|
| 121 |
|
|---|
| 122 | if (((state & EXT4_SUPERBLOCK_STATE_VALID_FS) !=
|
|---|
| 123 | EXT4_SUPERBLOCK_STATE_VALID_FS) ||
|
|---|
| 124 | ((state & EXT4_SUPERBLOCK_STATE_ERROR_FS) ==
|
|---|
| 125 | EXT4_SUPERBLOCK_STATE_ERROR_FS)) {
|
|---|
| 126 | rc = ENOTSUP;
|
|---|
| 127 | goto err_2;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | rc = ext4_superblock_check_sanity(fs->superblock);
|
|---|
| 131 | if (rc != EOK)
|
|---|
| 132 | goto err_2;
|
|---|
| 133 |
|
|---|
| 134 | /* Check flags */
|
|---|
| 135 | bool read_only;
|
|---|
| 136 | rc = ext4_filesystem_check_features(fs, &read_only);
|
|---|
| 137 | if (rc != EOK)
|
|---|
| 138 | goto err_2;
|
|---|
| 139 |
|
|---|
| 140 | return EOK;
|
|---|
| 141 | err_2:
|
|---|
| 142 | block_cache_fini(fs->device);
|
|---|
| 143 | err_1:
|
|---|
| 144 | block_fini(fs->device);
|
|---|
| 145 | err:
|
|---|
| 146 | if (temp_superblock)
|
|---|
| 147 | ext4_superblock_release(temp_superblock);
|
|---|
| 148 | return rc;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Finalize filesystem.
|
|---|
| 152 | *
|
|---|
| 153 | * @param fs Filesystem to be finalized
|
|---|
| 154 | *
|
|---|
| 155 | */
|
|---|
| 156 | static void ext4_filesystem_fini(ext4_filesystem_t *fs)
|
|---|
| 157 | {
|
|---|
| 158 | /* Release memory space for superblock */
|
|---|
| 159 | free(fs->superblock);
|
|---|
| 160 |
|
|---|
| 161 | /* Finish work with block library */
|
|---|
| 162 | block_cache_fini(fs->device);
|
|---|
| 163 | block_fini(fs->device);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** Create lost+found directory.
|
|---|
| 167 | *
|
|---|
| 168 | * @param fs Filesystem
|
|---|
| 169 | * @return EOK on success or error code
|
|---|
| 170 | */
|
|---|
| 171 | static errno_t ext4_filesystem_create_lost_found(ext4_filesystem_t *fs,
|
|---|
| 172 | ext4_inode_ref_t *root_dir_ref)
|
|---|
| 173 | {
|
|---|
| 174 | errno_t rc;
|
|---|
| 175 | ext4_inode_ref_t *inode_ref;
|
|---|
| 176 |
|
|---|
| 177 | rc = ext4_filesystem_alloc_inode(fs, &inode_ref, L_DIRECTORY);
|
|---|
| 178 | if (rc != EOK)
|
|---|
| 179 | goto error;
|
|---|
| 180 |
|
|---|
| 181 | rc = ext4_directory_add_entry(inode_ref, ".", inode_ref);
|
|---|
| 182 | if (rc != EOK)
|
|---|
| 183 | goto error;
|
|---|
| 184 |
|
|---|
| 185 | rc = ext4_directory_add_entry(inode_ref, "..", root_dir_ref);
|
|---|
| 186 | if (rc != EOK)
|
|---|
| 187 | goto error;
|
|---|
| 188 |
|
|---|
| 189 | rc = ext4_directory_add_entry(root_dir_ref, "lost+found", inode_ref);
|
|---|
| 190 | if (rc != EOK)
|
|---|
| 191 | goto error;
|
|---|
| 192 |
|
|---|
| 193 | inode_ref->dirty = true;
|
|---|
| 194 |
|
|---|
| 195 | uint16_t nlinks = ext4_inode_get_links_count(inode_ref->inode);
|
|---|
| 196 | ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
|
|---|
| 197 |
|
|---|
| 198 | rc = ext4_filesystem_put_inode_ref(inode_ref);
|
|---|
| 199 | if (rc != EOK)
|
|---|
| 200 | goto error;
|
|---|
| 201 |
|
|---|
| 202 | error:
|
|---|
| 203 | return rc;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /** Create root directory.
|
|---|
| 207 | *
|
|---|
| 208 | * @param fs Filesystem
|
|---|
| 209 | * @return EOK on success or error code
|
|---|
| 210 | */
|
|---|
| 211 | static errno_t ext4_filesystem_create_root_dir(ext4_filesystem_t *fs)
|
|---|
| 212 | {
|
|---|
| 213 | errno_t rc;
|
|---|
| 214 | ext4_inode_ref_t *inode_ref;
|
|---|
| 215 |
|
|---|
| 216 | rc = ext4_filesystem_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX,
|
|---|
| 217 | &inode_ref);
|
|---|
| 218 | if (rc != EOK)
|
|---|
| 219 | goto error;
|
|---|
| 220 |
|
|---|
| 221 | inode_ref->dirty = true;
|
|---|
| 222 |
|
|---|
| 223 | rc = ext4_directory_add_entry(inode_ref, ".", inode_ref);
|
|---|
| 224 | if (rc != EOK)
|
|---|
| 225 | goto error;
|
|---|
| 226 |
|
|---|
| 227 | rc = ext4_directory_add_entry(inode_ref, "..", inode_ref);
|
|---|
| 228 | if (rc != EOK)
|
|---|
| 229 | goto error;
|
|---|
| 230 |
|
|---|
| 231 | uint16_t nlinks = ext4_inode_get_links_count(inode_ref->inode);
|
|---|
| 232 | ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
|
|---|
| 233 |
|
|---|
| 234 | rc = ext4_filesystem_create_lost_found(fs, inode_ref);
|
|---|
| 235 | if (rc != EOK)
|
|---|
| 236 | goto error;
|
|---|
| 237 |
|
|---|
| 238 | nlinks = ext4_inode_get_links_count(inode_ref->inode);
|
|---|
| 239 | ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
|
|---|
| 240 |
|
|---|
| 241 | rc = ext4_filesystem_put_inode_ref(inode_ref);
|
|---|
| 242 | if (rc != EOK)
|
|---|
| 243 | goto error;
|
|---|
| 244 |
|
|---|
| 245 | error:
|
|---|
| 246 | return rc;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /** Create new filesystem.
|
|---|
| 250 | *
|
|---|
| 251 | * @param cfg Configuration of new file system
|
|---|
| 252 | * @param service_id Block device where to create new file system
|
|---|
| 253 | */
|
|---|
| 254 | errno_t ext4_filesystem_create(ext4_cfg_t *cfg, service_id_t service_id)
|
|---|
| 255 | {
|
|---|
| 256 | errno_t rc;
|
|---|
| 257 | ext4_superblock_t *superblock = NULL;
|
|---|
| 258 | ext4_filesystem_t *fs = NULL;
|
|---|
| 259 | size_t dev_bsize;
|
|---|
| 260 | aoff64_t dev_nblocks;
|
|---|
| 261 | ext4_inode_ref_t *inode_ref = NULL;
|
|---|
| 262 | bool block_inited = false;
|
|---|
| 263 | bool fs_inited = false;
|
|---|
| 264 | uint32_t idx;
|
|---|
| 265 |
|
|---|
| 266 | /* Initialize block library (4096 is size of communication channel) */
|
|---|
| 267 | rc = block_init(service_id, 4096);
|
|---|
| 268 | if (rc != EOK)
|
|---|
| 269 | goto err;
|
|---|
| 270 |
|
|---|
| 271 | block_inited = true;
|
|---|
| 272 |
|
|---|
| 273 | /* Get device block size */
|
|---|
| 274 | rc = block_get_bsize(service_id, &dev_bsize);
|
|---|
| 275 | if (rc != EOK)
|
|---|
| 276 | goto err;
|
|---|
| 277 |
|
|---|
| 278 | /* Get device number of blocks */
|
|---|
| 279 | rc = block_get_nblocks(service_id, &dev_nblocks);
|
|---|
| 280 | if (rc != EOK)
|
|---|
| 281 | goto err;
|
|---|
| 282 |
|
|---|
| 283 | /* Create superblock */
|
|---|
| 284 | rc = ext4_superblock_create(dev_bsize, dev_nblocks, cfg, &superblock);
|
|---|
| 285 | if (rc != EOK)
|
|---|
| 286 | goto err;
|
|---|
| 287 |
|
|---|
| 288 | /* Write superblock to device */
|
|---|
| 289 | rc = ext4_superblock_write_direct(service_id, superblock);
|
|---|
| 290 | if (rc != EOK)
|
|---|
| 291 | goto err;
|
|---|
| 292 |
|
|---|
| 293 | block_fini(service_id);
|
|---|
| 294 | block_inited = false;
|
|---|
| 295 | ext4_superblock_release(superblock);
|
|---|
| 296 | superblock = NULL;
|
|---|
| 297 |
|
|---|
| 298 | fs = calloc(1, sizeof(ext4_filesystem_t));
|
|---|
| 299 | if (fs == NULL)
|
|---|
| 300 | goto err;
|
|---|
| 301 |
|
|---|
| 302 | /* Open file system */
|
|---|
| 303 | rc = ext4_filesystem_init(fs, service_id, CACHE_MODE_WT);
|
|---|
| 304 | if (rc != EOK)
|
|---|
| 305 | goto err;
|
|---|
| 306 |
|
|---|
| 307 | fs_inited = true;
|
|---|
| 308 |
|
|---|
| 309 | /* Init block groups */
|
|---|
| 310 | rc = ext4_filesystem_init_block_groups(fs);
|
|---|
| 311 | if (rc != EOK)
|
|---|
| 312 | goto err;
|
|---|
| 313 |
|
|---|
| 314 | /* Reserved i-nodes */
|
|---|
| 315 | for (idx = 1; idx < EXT4_REV0_FIRST_INO; idx++) {
|
|---|
| 316 | if (idx == EXT4_INODE_ROOT_INDEX) {
|
|---|
| 317 | rc = ext4_filesystem_alloc_this_inode(fs, idx,
|
|---|
| 318 | &inode_ref, L_DIRECTORY);
|
|---|
| 319 | if (rc != EOK)
|
|---|
| 320 | goto error;
|
|---|
| 321 |
|
|---|
| 322 | rc = ext4_filesystem_put_inode_ref(inode_ref);
|
|---|
| 323 | if (rc != EOK)
|
|---|
| 324 | goto error;
|
|---|
| 325 | } else {
|
|---|
| 326 | /* Allocate inode by allocation algorithm */
|
|---|
| 327 | errno_t rc = ext4_ialloc_alloc_this_inode(fs, idx,
|
|---|
| 328 | false);
|
|---|
| 329 | if (rc != EOK)
|
|---|
| 330 | return rc;
|
|---|
| 331 |
|
|---|
| 332 | rc = ext4_filesystem_get_inode_ref(fs, idx,
|
|---|
| 333 | &inode_ref);
|
|---|
| 334 | if (rc != EOK)
|
|---|
| 335 | goto error;
|
|---|
| 336 |
|
|---|
| 337 | memset(inode_ref->inode, 0, ext4_superblock_get_inode_size(fs->superblock));
|
|---|
| 338 | inode_ref->dirty = true;
|
|---|
| 339 |
|
|---|
| 340 | rc = ext4_filesystem_put_inode_ref(inode_ref);
|
|---|
| 341 | if (rc != EOK)
|
|---|
| 342 | goto error;
|
|---|
| 343 | }
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | /* Create root directory */
|
|---|
| 347 | rc = ext4_filesystem_create_root_dir(fs);
|
|---|
| 348 | if (rc != EOK)
|
|---|
| 349 | goto err;
|
|---|
| 350 |
|
|---|
| 351 | /* Write superblock to device */
|
|---|
| 352 | rc = ext4_superblock_write_direct(service_id, fs->superblock);
|
|---|
| 353 | if (rc != EOK)
|
|---|
| 354 | goto err;
|
|---|
| 355 |
|
|---|
| 356 | ext4_filesystem_fini(fs);
|
|---|
| 357 | free(fs);
|
|---|
| 358 | return EOK;
|
|---|
| 359 | err:
|
|---|
| 360 | if (fs_inited)
|
|---|
| 361 | ext4_filesystem_fini(fs);
|
|---|
| 362 | if (fs != NULL)
|
|---|
| 363 | free(fs);
|
|---|
| 364 | if (superblock != NULL)
|
|---|
| 365 | ext4_superblock_release(superblock);
|
|---|
| 366 | if (block_inited)
|
|---|
| 367 | block_fini(service_id);
|
|---|
| 368 | return rc;
|
|---|
| 369 | error:
|
|---|
| 370 | return rc;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | /** Probe filesystem.
|
|---|
| 374 | *
|
|---|
| 375 | * @param service_id Block device to probe
|
|---|
| 376 | * @param info Place to store probe information
|
|---|
| 377 | *
|
|---|
| 378 | * @return EOK or an error code.
|
|---|
| 379 | *
|
|---|
| 380 | */
|
|---|
| 381 | errno_t ext4_filesystem_probe(service_id_t service_id,
|
|---|
| 382 | ext4_fs_probe_info_t *info)
|
|---|
| 383 | {
|
|---|
| 384 | ext4_filesystem_t *fs = NULL;
|
|---|
| 385 | errno_t rc;
|
|---|
| 386 |
|
|---|
| 387 | fs = calloc(1, sizeof(ext4_filesystem_t));
|
|---|
| 388 | if (fs == NULL)
|
|---|
| 389 | return ENOMEM;
|
|---|
| 390 |
|
|---|
| 391 | /* Initialize the file system for opening */
|
|---|
| 392 | rc = ext4_filesystem_init(fs, service_id, CACHE_MODE_WT);
|
|---|
| 393 | if (rc != EOK) {
|
|---|
| 394 | free(fs);
|
|---|
| 395 | return rc;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | rc = ext4_superblock_get_volume_name(fs->superblock, info->vol_name,
|
|---|
| 399 | sizeof(info->vol_name));
|
|---|
| 400 | if (rc != EOK) {
|
|---|
| 401 | ext4_filesystem_fini(fs);
|
|---|
| 402 | return rc;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | ext4_filesystem_fini(fs);
|
|---|
| 406 | return EOK;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | /** Open filesystem and read all needed data.
|
|---|
| 410 | *
|
|---|
| 411 | * @param fs Filesystem to be initialized
|
|---|
| 412 | * @param inst Instance
|
|---|
| 413 | * @param service_id Identifier if device with the filesystem
|
|---|
| 414 | * @param cmode Cache mode
|
|---|
| 415 | * @param size Output value - size of root node
|
|---|
| 416 | *
|
|---|
| 417 | * @return Error code
|
|---|
| 418 | *
|
|---|
| 419 | */
|
|---|
| 420 | errno_t ext4_filesystem_open(ext4_instance_t *inst, service_id_t service_id,
|
|---|
| 421 | enum cache_mode cmode, aoff64_t *size, ext4_filesystem_t **rfs)
|
|---|
| 422 | {
|
|---|
| 423 | ext4_filesystem_t *fs = NULL;
|
|---|
| 424 | fs_node_t *root_node = NULL;
|
|---|
| 425 | errno_t rc;
|
|---|
| 426 |
|
|---|
| 427 | fs = calloc(1, sizeof(ext4_filesystem_t));
|
|---|
| 428 | if (fs == NULL) {
|
|---|
| 429 | rc = ENOMEM;
|
|---|
| 430 | goto error;
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | inst->filesystem = fs;
|
|---|
| 434 |
|
|---|
| 435 | /* Initialize the file system for opening */
|
|---|
| 436 | rc = ext4_filesystem_init(fs, service_id, cmode);
|
|---|
| 437 | if (rc != EOK)
|
|---|
| 438 | goto error;
|
|---|
| 439 |
|
|---|
| 440 | /* Read root node */
|
|---|
| 441 | rc = ext4_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
|
|---|
| 442 | if (rc != EOK)
|
|---|
| 443 | goto error;
|
|---|
| 444 |
|
|---|
| 445 | /* Mark system as mounted */
|
|---|
| 446 | ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_ERROR_FS);
|
|---|
| 447 | rc = ext4_superblock_write_direct(fs->device, fs->superblock);
|
|---|
| 448 | if (rc != EOK)
|
|---|
| 449 | goto error;
|
|---|
| 450 |
|
|---|
| 451 | uint16_t mnt_count = ext4_superblock_get_mount_count(fs->superblock);
|
|---|
| 452 | ext4_superblock_set_mount_count(fs->superblock, mnt_count + 1);
|
|---|
| 453 |
|
|---|
| 454 | ext4_node_t *enode = EXT4_NODE(root_node);
|
|---|
| 455 |
|
|---|
| 456 | *size = ext4_inode_get_size(fs->superblock, enode->inode_ref->inode);
|
|---|
| 457 |
|
|---|
| 458 | ext4_node_put(root_node);
|
|---|
| 459 | *rfs = fs;
|
|---|
| 460 | return EOK;
|
|---|
| 461 | error:
|
|---|
| 462 | if (root_node != NULL)
|
|---|
| 463 | ext4_node_put(root_node);
|
|---|
| 464 |
|
|---|
| 465 | if (fs != NULL) {
|
|---|
| 466 | ext4_filesystem_fini(fs);
|
|---|
| 467 | free(fs);
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | return rc;
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | /** Close filesystem.
|
|---|
| 474 | *
|
|---|
| 475 | * @param fs Filesystem to be destroyed
|
|---|
| 476 | *
|
|---|
| 477 | * @return EOK or an error code. On error the state of the file
|
|---|
| 478 | * system is unchanged.
|
|---|
| 479 | *
|
|---|
| 480 | */
|
|---|
| 481 | errno_t ext4_filesystem_close(ext4_filesystem_t *fs)
|
|---|
| 482 | {
|
|---|
| 483 | /* Write the superblock to the device */
|
|---|
| 484 | ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_VALID_FS);
|
|---|
| 485 | errno_t rc = ext4_superblock_write_direct(fs->device, fs->superblock);
|
|---|
| 486 | if (rc != EOK)
|
|---|
| 487 | return rc;
|
|---|
| 488 |
|
|---|
| 489 | ext4_filesystem_fini(fs);
|
|---|
| 490 | return EOK;
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | /** Check filesystem's features, if supported by this driver
|
|---|
| 494 | *
|
|---|
| 495 | * Function can return EOK and set read_only flag. It mean's that
|
|---|
| 496 | * there are some not-supported features, that can cause problems
|
|---|
| 497 | * during some write operations.
|
|---|
| 498 | *
|
|---|
| 499 | * @param fs Filesystem to be checked
|
|---|
| 500 | * @param read_only Place to write flag saying whether filesystem
|
|---|
| 501 | * should be mounted only for reading
|
|---|
| 502 | *
|
|---|
| 503 | * @return Error code
|
|---|
| 504 | *
|
|---|
| 505 | */
|
|---|
| 506 | static errno_t ext4_filesystem_check_features(ext4_filesystem_t *fs,
|
|---|
| 507 | bool *read_only)
|
|---|
| 508 | {
|
|---|
| 509 | /* Feature flags are present only in higher revisions */
|
|---|
| 510 | if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
|
|---|
| 511 | *read_only = false;
|
|---|
| 512 | return EOK;
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | /*
|
|---|
| 516 | * Check incompatible features - if filesystem has some,
|
|---|
| 517 | * volume can't be mounted
|
|---|
| 518 | */
|
|---|
| 519 | uint32_t incompatible_features;
|
|---|
| 520 | incompatible_features =
|
|---|
| 521 | ext4_superblock_get_features_incompatible(fs->superblock);
|
|---|
| 522 | incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
|
|---|
| 523 | if (incompatible_features > 0)
|
|---|
| 524 | return ENOTSUP;
|
|---|
| 525 |
|
|---|
| 526 | /*
|
|---|
| 527 | * Check read-only features, if filesystem has some,
|
|---|
| 528 | * volume can be mount only in read-only mode
|
|---|
| 529 | */
|
|---|
| 530 | uint32_t compatible_read_only;
|
|---|
| 531 | compatible_read_only =
|
|---|
| 532 | ext4_superblock_get_features_read_only(fs->superblock);
|
|---|
| 533 | compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
|
|---|
| 534 | if (compatible_read_only > 0) {
|
|---|
| 535 | *read_only = true;
|
|---|
| 536 | return EOK;
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | return EOK;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | /** Convert block address to relative index in block group.
|
|---|
| 543 | *
|
|---|
| 544 | * @param sb Superblock pointer
|
|---|
| 545 | * @param block_addr Block number to convert
|
|---|
| 546 | *
|
|---|
| 547 | * @return Relative number of block
|
|---|
| 548 | *
|
|---|
| 549 | */
|
|---|
| 550 | uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *sb,
|
|---|
| 551 | uint32_t block_addr)
|
|---|
| 552 | {
|
|---|
| 553 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 554 | uint32_t first_block = ext4_superblock_get_first_data_block(sb);
|
|---|
| 555 |
|
|---|
| 556 | /* First block == 0 or 1 */
|
|---|
| 557 | if (first_block == 0)
|
|---|
| 558 | return block_addr % blocks_per_group;
|
|---|
| 559 | else
|
|---|
| 560 | return (block_addr - 1) % blocks_per_group;
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | /** Convert relative block address in group to absolute address.
|
|---|
| 564 | *
|
|---|
| 565 | * @param sb Superblock pointer
|
|---|
| 566 | *
|
|---|
| 567 | * @return Absolute block address
|
|---|
| 568 | *
|
|---|
| 569 | */
|
|---|
| 570 | uint32_t ext4_filesystem_index_in_group2blockaddr(ext4_superblock_t *sb,
|
|---|
| 571 | uint32_t index, uint32_t bgid)
|
|---|
| 572 | {
|
|---|
| 573 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 574 |
|
|---|
| 575 | if (ext4_superblock_get_first_data_block(sb) == 0)
|
|---|
| 576 | return bgid * blocks_per_group + index;
|
|---|
| 577 | else
|
|---|
| 578 | return bgid * blocks_per_group + index + 1;
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | /** Convert the absolute block number to group number
|
|---|
| 582 | *
|
|---|
| 583 | * @param sb Pointer to the superblock
|
|---|
| 584 | * @param b Absolute block number
|
|---|
| 585 | *
|
|---|
| 586 | * @return Group number
|
|---|
| 587 | */
|
|---|
| 588 | uint32_t ext4_filesystem_blockaddr2group(ext4_superblock_t *sb, uint64_t b)
|
|---|
| 589 | {
|
|---|
| 590 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 591 | uint32_t first_block = ext4_superblock_get_first_data_block(sb);
|
|---|
| 592 |
|
|---|
| 593 | return (b - first_block) / blocks_per_group;
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | /** Initialize block group structures
|
|---|
| 597 | */
|
|---|
| 598 | static errno_t ext4_filesystem_init_block_groups(ext4_filesystem_t *fs)
|
|---|
| 599 | {
|
|---|
| 600 | errno_t rc;
|
|---|
| 601 | block_t *block;
|
|---|
| 602 | aoff64_t b;
|
|---|
| 603 | ext4_block_group_t *bg;
|
|---|
| 604 | ext4_superblock_t *sb = fs->superblock;
|
|---|
| 605 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 606 |
|
|---|
| 607 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
|---|
| 608 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 609 | uint32_t desc_size = ext4_superblock_get_desc_size(fs->superblock);
|
|---|
| 610 | /* Number of descriptors per block */
|
|---|
| 611 | uint32_t descriptors_per_block =
|
|---|
| 612 | ext4_superblock_get_block_size(fs->superblock) / desc_size;
|
|---|
| 613 | /* Block where block group descriptor (and first block group) starts */
|
|---|
| 614 | aoff64_t block_id =
|
|---|
| 615 | ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
|---|
| 616 | /* Number of blocks containing descriptor table */
|
|---|
| 617 | uint32_t dtable_blocks =
|
|---|
| 618 | (block_group_count + descriptors_per_block - 1) /
|
|---|
| 619 | descriptors_per_block;
|
|---|
| 620 |
|
|---|
| 621 | uint32_t bg_index;
|
|---|
| 622 | aoff64_t bg_block0;
|
|---|
| 623 | uint32_t dcnt;
|
|---|
| 624 | uint32_t i;
|
|---|
| 625 | uint32_t now;
|
|---|
| 626 |
|
|---|
| 627 | aoff64_t block_bitmap;
|
|---|
| 628 | aoff64_t inode_bitmap;
|
|---|
| 629 | aoff64_t inode_table;
|
|---|
| 630 | uint32_t free_blocks;
|
|---|
| 631 | uint32_t free_inodes;
|
|---|
| 632 | uint32_t used_dirs;
|
|---|
| 633 | uint32_t reserved;
|
|---|
| 634 | uint32_t inode_table_blocks;
|
|---|
| 635 |
|
|---|
| 636 | dcnt = block_group_count;
|
|---|
| 637 |
|
|---|
| 638 | /* Fill in block descriptors */
|
|---|
| 639 | b = block_id;
|
|---|
| 640 | bg_index = 0;
|
|---|
| 641 | bg_block0 = block_id;
|
|---|
| 642 | while (dcnt > 0) {
|
|---|
| 643 | rc = block_get(&block, fs->device, b, BLOCK_FLAGS_NOREAD);
|
|---|
| 644 | if (rc != EOK)
|
|---|
| 645 | return rc;
|
|---|
| 646 |
|
|---|
| 647 | if (dcnt > descriptors_per_block)
|
|---|
| 648 | now = descriptors_per_block;
|
|---|
| 649 | else
|
|---|
| 650 | now = dcnt;
|
|---|
| 651 |
|
|---|
| 652 | memset(block->data, 0, block_size);
|
|---|
| 653 |
|
|---|
| 654 | for (i = 0; i < now; i++) {
|
|---|
| 655 | bg = (ext4_block_group_t *) (block->data + i *
|
|---|
| 656 | desc_size);
|
|---|
| 657 |
|
|---|
| 658 | block_bitmap = bg_block0 + dtable_blocks;
|
|---|
| 659 | inode_bitmap = block_bitmap + 1;
|
|---|
| 660 | inode_table = inode_bitmap + 1;
|
|---|
| 661 |
|
|---|
| 662 | free_blocks = ext4_superblock_get_blocks_in_group(sb,
|
|---|
| 663 | bg_index);
|
|---|
| 664 |
|
|---|
| 665 | free_inodes =
|
|---|
| 666 | ext4_filesystem_bg_get_itable_size(sb, bg_index) *
|
|---|
| 667 | ext4_filesystem_inodes_per_block(sb);
|
|---|
| 668 | used_dirs = 0;
|
|---|
| 669 |
|
|---|
| 670 | ext4_block_group_set_block_bitmap(bg, sb, block_bitmap);
|
|---|
| 671 | ext4_block_group_set_inode_bitmap(bg, sb, inode_bitmap);
|
|---|
| 672 | ext4_block_group_set_inode_table_first_block(bg, sb,
|
|---|
| 673 | inode_table);
|
|---|
| 674 | ext4_block_group_set_free_blocks_count(bg, sb,
|
|---|
| 675 | free_blocks);
|
|---|
| 676 | ext4_block_group_set_free_inodes_count(bg, sb,
|
|---|
| 677 | free_inodes);
|
|---|
| 678 | ext4_block_group_set_used_dirs_count(bg, sb,
|
|---|
| 679 | used_dirs);
|
|---|
| 680 |
|
|---|
| 681 | /// XX Lazy
|
|---|
| 682 | ext4_block_group_set_flag(bg,
|
|---|
| 683 | EXT4_BLOCK_GROUP_BLOCK_UNINIT);
|
|---|
| 684 | ext4_block_group_set_flag(bg,
|
|---|
| 685 | EXT4_BLOCK_GROUP_INODE_UNINIT);
|
|---|
| 686 |
|
|---|
| 687 | bg_index++;
|
|---|
| 688 | bg_block0 += ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | block->dirty = true;
|
|---|
| 692 |
|
|---|
| 693 | rc = block_put(block);
|
|---|
| 694 | if (rc != EOK)
|
|---|
| 695 | return rc;
|
|---|
| 696 |
|
|---|
| 697 | ++b;
|
|---|
| 698 | dcnt -= now;
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| 701 | /* This initializes the bitmaps and inode table */
|
|---|
| 702 | for (bg_index = 0; bg_index < block_group_count; bg_index++) {
|
|---|
| 703 | rc = ext4_filesystem_get_block_group_ref(fs, bg_index, &bg_ref);
|
|---|
| 704 | if (rc != EOK)
|
|---|
| 705 | return rc;
|
|---|
| 706 |
|
|---|
| 707 | /*
|
|---|
| 708 | * Adjust number of free blocks
|
|---|
| 709 | */
|
|---|
| 710 | free_blocks = ext4_superblock_get_blocks_in_group(sb, bg_index);
|
|---|
| 711 | reserved = ext4_filesystem_bg_get_backup_blocks(bg_ref);
|
|---|
| 712 | inode_table_blocks = ext4_filesystem_bg_get_itable_size(sb,
|
|---|
| 713 | bg_ref->index);
|
|---|
| 714 | /* One for block bitmap one for inode bitmap */
|
|---|
| 715 | free_blocks = free_blocks - reserved - 2 - inode_table_blocks;
|
|---|
| 716 | if (bg_index == 0)
|
|---|
| 717 | ++free_blocks; /* XXX Why? */
|
|---|
| 718 |
|
|---|
| 719 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
|---|
| 720 | sb, free_blocks);
|
|---|
| 721 | bg_ref->dirty = true;
|
|---|
| 722 |
|
|---|
| 723 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 724 | if (rc != EOK)
|
|---|
| 725 | return rc;
|
|---|
| 726 | }
|
|---|
| 727 |
|
|---|
| 728 | return EOK;
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | /** Initialize block bitmap in block group.
|
|---|
| 732 | *
|
|---|
| 733 | * @param bg_ref Reference to block group
|
|---|
| 734 | *
|
|---|
| 735 | * @return Error code
|
|---|
| 736 | *
|
|---|
| 737 | */
|
|---|
| 738 | static errno_t ext4_filesystem_init_block_bitmap(ext4_block_group_ref_t *bg_ref)
|
|---|
| 739 | {
|
|---|
| 740 | uint64_t itb;
|
|---|
| 741 | uint32_t sz;
|
|---|
| 742 | uint32_t i;
|
|---|
| 743 |
|
|---|
| 744 | /* Load bitmap */
|
|---|
| 745 | ext4_superblock_t *sb = bg_ref->fs->superblock;
|
|---|
| 746 | uint64_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
|---|
| 747 | bg_ref->block_group, bg_ref->fs->superblock);
|
|---|
| 748 | uint64_t bitmap_inode_addr = ext4_block_group_get_inode_bitmap(
|
|---|
| 749 | bg_ref->block_group, bg_ref->fs->superblock);
|
|---|
| 750 | uint32_t blocks_group = ext4_superblock_get_blocks_per_group(sb);
|
|---|
| 751 | uint32_t bg_blocks = ext4_superblock_get_blocks_in_group(sb,
|
|---|
| 752 | bg_ref->index);
|
|---|
| 753 |
|
|---|
| 754 | block_t *bitmap_block;
|
|---|
| 755 | errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
|
|---|
| 756 | bitmap_block_addr, BLOCK_FLAGS_NOREAD);
|
|---|
| 757 | if (rc != EOK)
|
|---|
| 758 | return rc;
|
|---|
| 759 |
|
|---|
| 760 | uint8_t *bitmap = bitmap_block->data;
|
|---|
| 761 |
|
|---|
| 762 | /* Initialize all bitmap bits to zero */
|
|---|
| 763 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 764 | memset(bitmap, 0, block_size);
|
|---|
| 765 |
|
|---|
| 766 | /* Determine the number of reserved blocks in the group */
|
|---|
| 767 | uint32_t reserved_cnt = ext4_filesystem_bg_get_backup_blocks(bg_ref);
|
|---|
| 768 |
|
|---|
| 769 | /* Set bits from to first block to first data block - 1 to one (allocated) */
|
|---|
| 770 | for (uint32_t block = 0; block < reserved_cnt; ++block)
|
|---|
| 771 | ext4_bitmap_set_bit(bitmap, block);
|
|---|
| 772 |
|
|---|
| 773 | uint32_t bitmap_block_gid = ext4_filesystem_blockaddr2group(sb,
|
|---|
| 774 | bitmap_block_addr);
|
|---|
| 775 | if (bitmap_block_gid == bg_ref->index) {
|
|---|
| 776 | ext4_bitmap_set_bit(bitmap,
|
|---|
| 777 | ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_block_addr));
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | uint32_t bitmap_inode_gid = ext4_filesystem_blockaddr2group(sb,
|
|---|
| 781 | bitmap_inode_addr);
|
|---|
| 782 | if (bitmap_inode_gid == bg_ref->index) {
|
|---|
| 783 | ext4_bitmap_set_bit(bitmap,
|
|---|
| 784 | ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_inode_addr));
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | itb = ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
|
|---|
| 788 | sb);
|
|---|
| 789 | sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref->index);
|
|---|
| 790 |
|
|---|
| 791 | for (i = 0; i < sz; ++i, ++itb) {
|
|---|
| 792 | uint32_t gid = ext4_filesystem_blockaddr2group(sb, itb);
|
|---|
| 793 | if (gid == bg_ref->index) {
|
|---|
| 794 | ext4_bitmap_set_bit(bitmap,
|
|---|
| 795 | ext4_filesystem_blockaddr2_index_in_group(sb, itb));
|
|---|
| 796 | }
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 | /* For last group need to mark blocks which are outside of the FS */
|
|---|
| 800 | for (uint32_t block = bg_blocks; block < blocks_group; block++) {
|
|---|
| 801 | ext4_bitmap_set_bit(bitmap, block);
|
|---|
| 802 | }
|
|---|
| 803 |
|
|---|
| 804 | bitmap_block->dirty = true;
|
|---|
| 805 |
|
|---|
| 806 | /* Save bitmap */
|
|---|
| 807 | return block_put(bitmap_block);
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | /** Initialize i-node bitmap in block group.
|
|---|
| 811 | *
|
|---|
| 812 | * @param bg_ref Reference to block group
|
|---|
| 813 | *
|
|---|
| 814 | * @return Error code
|
|---|
| 815 | *
|
|---|
| 816 | */
|
|---|
| 817 | static errno_t ext4_filesystem_init_inode_bitmap(ext4_block_group_ref_t *bg_ref)
|
|---|
| 818 | {
|
|---|
| 819 | /* Load bitmap */
|
|---|
| 820 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
|---|
| 821 | bg_ref->block_group, bg_ref->fs->superblock);
|
|---|
| 822 | block_t *bitmap_block;
|
|---|
| 823 |
|
|---|
| 824 | errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
|
|---|
| 825 | bitmap_block_addr, BLOCK_FLAGS_NOREAD);
|
|---|
| 826 | if (rc != EOK)
|
|---|
| 827 | return rc;
|
|---|
| 828 |
|
|---|
| 829 | uint8_t *bitmap = bitmap_block->data;
|
|---|
| 830 |
|
|---|
| 831 | /* Initialize all bitmap bits to zero */
|
|---|
| 832 | uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
|
|---|
| 833 | uint32_t inodes_per_group =
|
|---|
| 834 | ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
|
|---|
| 835 | memset(bitmap, 0, (inodes_per_group + 7) / 8);
|
|---|
| 836 |
|
|---|
| 837 | uint32_t start_bit = inodes_per_group;
|
|---|
| 838 | uint32_t end_bit = block_size * 8;
|
|---|
| 839 |
|
|---|
| 840 | uint32_t i;
|
|---|
| 841 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
|
|---|
| 842 | ext4_bitmap_set_bit(bitmap, i);
|
|---|
| 843 |
|
|---|
| 844 | if (i < end_bit)
|
|---|
| 845 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
|
|---|
| 846 |
|
|---|
| 847 | bitmap_block->dirty = true;
|
|---|
| 848 |
|
|---|
| 849 | /* Save bitmap */
|
|---|
| 850 | return block_put(bitmap_block);
|
|---|
| 851 | }
|
|---|
| 852 |
|
|---|
| 853 | /** Initialize i-node table in block group.
|
|---|
| 854 | *
|
|---|
| 855 | * @param bg_ref Reference to block group
|
|---|
| 856 | *
|
|---|
| 857 | * @return Error code
|
|---|
| 858 | *
|
|---|
| 859 | */
|
|---|
| 860 | static errno_t ext4_filesystem_init_inode_table(ext4_block_group_ref_t *bg_ref)
|
|---|
| 861 | {
|
|---|
| 862 | ext4_superblock_t *sb = bg_ref->fs->superblock;
|
|---|
| 863 |
|
|---|
| 864 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 865 | uint32_t inodes_per_block = ext4_filesystem_inodes_per_block(sb);
|
|---|
| 866 |
|
|---|
| 867 | uint32_t inodes_in_group =
|
|---|
| 868 | ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
|
|---|
| 869 |
|
|---|
| 870 | uint32_t table_blocks = inodes_in_group / inodes_per_block;
|
|---|
| 871 |
|
|---|
| 872 | if (inodes_in_group % inodes_per_block)
|
|---|
| 873 | table_blocks++;
|
|---|
| 874 |
|
|---|
| 875 | /* Compute initialization bounds */
|
|---|
| 876 | uint32_t first_block = ext4_block_group_get_inode_table_first_block(
|
|---|
| 877 | bg_ref->block_group, sb);
|
|---|
| 878 |
|
|---|
| 879 | uint32_t last_block = first_block + table_blocks - 1;
|
|---|
| 880 |
|
|---|
| 881 | /* Initialization of all itable blocks */
|
|---|
| 882 | for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
|
|---|
| 883 | block_t *block;
|
|---|
| 884 | errno_t rc = block_get(&block, bg_ref->fs->device, fblock,
|
|---|
| 885 | BLOCK_FLAGS_NOREAD);
|
|---|
| 886 | if (rc != EOK)
|
|---|
| 887 | return rc;
|
|---|
| 888 |
|
|---|
| 889 | memset(block->data, 0, block_size);
|
|---|
| 890 | block->dirty = true;
|
|---|
| 891 |
|
|---|
| 892 | rc = block_put(block);
|
|---|
| 893 | if (rc != EOK)
|
|---|
| 894 | return rc;
|
|---|
| 895 | }
|
|---|
| 896 |
|
|---|
| 897 | return EOK;
|
|---|
| 898 | }
|
|---|
| 899 |
|
|---|
| 900 | /** Get reference to block group specified by index.
|
|---|
| 901 | *
|
|---|
| 902 | * @param fs Filesystem to find block group on
|
|---|
| 903 | * @param bgid Index of block group to load
|
|---|
| 904 | * @param ref Output pointer for reference
|
|---|
| 905 | *
|
|---|
| 906 | * @return Error code
|
|---|
| 907 | *
|
|---|
| 908 | */
|
|---|
| 909 | errno_t ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
|---|
| 910 | ext4_block_group_ref_t **ref)
|
|---|
| 911 | {
|
|---|
| 912 | /* Allocate memory for new structure */
|
|---|
| 913 | ext4_block_group_ref_t *newref =
|
|---|
| 914 | malloc(sizeof(ext4_block_group_ref_t));
|
|---|
| 915 | if (newref == NULL)
|
|---|
| 916 | return ENOMEM;
|
|---|
| 917 |
|
|---|
| 918 | /* Compute number of descriptors, that fits in one data block */
|
|---|
| 919 | uint32_t descriptors_per_block =
|
|---|
| 920 | ext4_superblock_get_block_size(fs->superblock) /
|
|---|
| 921 | ext4_superblock_get_desc_size(fs->superblock);
|
|---|
| 922 |
|
|---|
| 923 | /* Block group descriptor table starts at the next block after superblock */
|
|---|
| 924 | aoff64_t block_id =
|
|---|
| 925 | ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
|---|
| 926 |
|
|---|
| 927 | /* Find the block containing the descriptor we are looking for */
|
|---|
| 928 | block_id += bgid / descriptors_per_block;
|
|---|
| 929 | uint32_t offset = (bgid % descriptors_per_block) *
|
|---|
| 930 | ext4_superblock_get_desc_size(fs->superblock);
|
|---|
| 931 |
|
|---|
| 932 | /* Load block with descriptors */
|
|---|
| 933 | errno_t rc = block_get(&newref->block, fs->device, block_id, 0);
|
|---|
| 934 | if (rc != EOK) {
|
|---|
| 935 | free(newref);
|
|---|
| 936 | return rc;
|
|---|
| 937 | }
|
|---|
| 938 |
|
|---|
| 939 | /* Initialize in-memory representation */
|
|---|
| 940 | newref->block_group = newref->block->data + offset;
|
|---|
| 941 | newref->fs = fs;
|
|---|
| 942 | newref->index = bgid;
|
|---|
| 943 | newref->dirty = false;
|
|---|
| 944 |
|
|---|
| 945 | *ref = newref;
|
|---|
| 946 |
|
|---|
| 947 | if (ext4_block_group_has_flag(newref->block_group,
|
|---|
| 948 | EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
|
|---|
| 949 | rc = ext4_filesystem_init_block_bitmap(newref);
|
|---|
| 950 | if (rc != EOK) {
|
|---|
| 951 | block_put(newref->block);
|
|---|
| 952 | free(newref);
|
|---|
| 953 | return rc;
|
|---|
| 954 | }
|
|---|
| 955 |
|
|---|
| 956 | ext4_block_group_clear_flag(newref->block_group,
|
|---|
| 957 | EXT4_BLOCK_GROUP_BLOCK_UNINIT);
|
|---|
| 958 |
|
|---|
| 959 | newref->dirty = true;
|
|---|
| 960 | }
|
|---|
| 961 |
|
|---|
| 962 | if (ext4_block_group_has_flag(newref->block_group,
|
|---|
| 963 | EXT4_BLOCK_GROUP_INODE_UNINIT)) {
|
|---|
| 964 | rc = ext4_filesystem_init_inode_bitmap(newref);
|
|---|
| 965 | if (rc != EOK) {
|
|---|
| 966 | block_put(newref->block);
|
|---|
| 967 | free(newref);
|
|---|
| 968 | return rc;
|
|---|
| 969 | }
|
|---|
| 970 |
|
|---|
| 971 | ext4_block_group_clear_flag(newref->block_group,
|
|---|
| 972 | EXT4_BLOCK_GROUP_INODE_UNINIT);
|
|---|
| 973 |
|
|---|
| 974 | if (!ext4_block_group_has_flag(newref->block_group,
|
|---|
| 975 | EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
|
|---|
| 976 | rc = ext4_filesystem_init_inode_table(newref);
|
|---|
| 977 | if (rc != EOK) {
|
|---|
| 978 | block_put(newref->block);
|
|---|
| 979 | free(newref);
|
|---|
| 980 | return rc;
|
|---|
| 981 | }
|
|---|
| 982 |
|
|---|
| 983 | ext4_block_group_set_flag(newref->block_group,
|
|---|
| 984 | EXT4_BLOCK_GROUP_ITABLE_ZEROED);
|
|---|
| 985 | }
|
|---|
| 986 |
|
|---|
| 987 | newref->dirty = true;
|
|---|
| 988 | }
|
|---|
| 989 |
|
|---|
| 990 | return EOK;
|
|---|
| 991 | }
|
|---|
| 992 |
|
|---|
| 993 | /** Compute checksum of block group descriptor.
|
|---|
| 994 | *
|
|---|
| 995 | * @param sb Superblock
|
|---|
| 996 | * @param bgid Index of block group in the filesystem
|
|---|
| 997 | * @param bg Block group to compute checksum for
|
|---|
| 998 | *
|
|---|
| 999 | * @return Checksum value
|
|---|
| 1000 | *
|
|---|
| 1001 | */
|
|---|
| 1002 | static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
|
|---|
| 1003 | ext4_block_group_t *bg)
|
|---|
| 1004 | {
|
|---|
| 1005 | /* If checksum not supported, 0 will be returned */
|
|---|
| 1006 | uint16_t crc = 0;
|
|---|
| 1007 |
|
|---|
| 1008 | /* Compute the checksum only if the filesystem supports it */
|
|---|
| 1009 | if (ext4_superblock_has_feature_read_only(sb,
|
|---|
| 1010 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
|
|---|
| 1011 | void *base = bg;
|
|---|
| 1012 | void *checksum = &bg->checksum;
|
|---|
| 1013 |
|
|---|
| 1014 | uint32_t offset = (uint32_t) (checksum - base);
|
|---|
| 1015 |
|
|---|
| 1016 | /* Convert block group index to little endian */
|
|---|
| 1017 | uint32_t le_group = host2uint32_t_le(bgid);
|
|---|
| 1018 |
|
|---|
| 1019 | /* Initialization */
|
|---|
| 1020 | crc = crc16_ibm(~0, sb->uuid, sizeof(sb->uuid));
|
|---|
| 1021 |
|
|---|
| 1022 | /* Include index of block group */
|
|---|
| 1023 | crc = crc16_ibm(crc, (uint8_t *) &le_group, sizeof(le_group));
|
|---|
| 1024 |
|
|---|
| 1025 | /* Compute crc from the first part (stop before checksum field) */
|
|---|
| 1026 | crc = crc16_ibm(crc, (uint8_t *) bg, offset);
|
|---|
| 1027 |
|
|---|
| 1028 | /* Skip checksum */
|
|---|
| 1029 | offset += sizeof(bg->checksum);
|
|---|
| 1030 |
|
|---|
| 1031 | /* Checksum of the rest of block group descriptor */
|
|---|
| 1032 | if ((ext4_superblock_has_feature_incompatible(sb,
|
|---|
| 1033 | EXT4_FEATURE_INCOMPAT_64BIT)) &&
|
|---|
| 1034 | (offset < ext4_superblock_get_desc_size(sb)))
|
|---|
| 1035 | crc = crc16_ibm(crc, ((uint8_t *) bg) + offset,
|
|---|
| 1036 | ext4_superblock_get_desc_size(sb) - offset);
|
|---|
| 1037 | }
|
|---|
| 1038 |
|
|---|
| 1039 | return crc;
|
|---|
| 1040 | }
|
|---|
| 1041 |
|
|---|
| 1042 | /** Get the size of the block group's inode table
|
|---|
| 1043 | *
|
|---|
| 1044 | * @param sb Pointer to the superblock
|
|---|
| 1045 | * @param bg_index Block group index
|
|---|
| 1046 | *
|
|---|
| 1047 | * @return Size of the inode table in blocks.
|
|---|
| 1048 | */
|
|---|
| 1049 | uint32_t ext4_filesystem_bg_get_itable_size(ext4_superblock_t *sb,
|
|---|
| 1050 | uint32_t bg_index)
|
|---|
| 1051 | {
|
|---|
| 1052 | uint32_t itable_size;
|
|---|
| 1053 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
|---|
| 1054 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
|---|
| 1055 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
|---|
| 1056 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 1057 |
|
|---|
| 1058 | if (bg_index < block_group_count - 1) {
|
|---|
| 1059 | itable_size = inodes_per_group * inode_table_item_size;
|
|---|
| 1060 | } else {
|
|---|
| 1061 | /* Last block group could be smaller */
|
|---|
| 1062 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
|---|
| 1063 | itable_size =
|
|---|
| 1064 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
|
|---|
| 1065 | inode_table_item_size;
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | return ROUND_UP(itable_size, block_size) / block_size;
|
|---|
| 1069 | }
|
|---|
| 1070 |
|
|---|
| 1071 | /** Get the number of blocks used by superblock + gdt + reserved gdt backups
|
|---|
| 1072 | *
|
|---|
| 1073 | * @param bg Pointer to block group
|
|---|
| 1074 | *
|
|---|
| 1075 | * @return Number of blocks
|
|---|
| 1076 | */
|
|---|
| 1077 | uint32_t ext4_filesystem_bg_get_backup_blocks(ext4_block_group_ref_t *bg)
|
|---|
| 1078 | {
|
|---|
| 1079 | return ext4_superblock_get_group_backup_blocks(bg->fs->superblock,
|
|---|
| 1080 | bg->index);
|
|---|
| 1081 | }
|
|---|
| 1082 |
|
|---|
| 1083 | /** Put reference to block group.
|
|---|
| 1084 | *
|
|---|
| 1085 | * @param ref Pointer for reference to be put back
|
|---|
| 1086 | *
|
|---|
| 1087 | * @return Error code
|
|---|
| 1088 | *
|
|---|
| 1089 | */
|
|---|
| 1090 | errno_t ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
|---|
| 1091 | {
|
|---|
| 1092 | /* Check if reference modified */
|
|---|
| 1093 | if (ref->dirty) {
|
|---|
| 1094 | /* Compute new checksum of block group */
|
|---|
| 1095 | uint16_t checksum =
|
|---|
| 1096 | ext4_filesystem_bg_checksum(ref->fs->superblock, ref->index,
|
|---|
| 1097 | ref->block_group);
|
|---|
| 1098 | ext4_block_group_set_checksum(ref->block_group, checksum);
|
|---|
| 1099 |
|
|---|
| 1100 | /* Mark block dirty for writing changes to physical device */
|
|---|
| 1101 | ref->block->dirty = true;
|
|---|
| 1102 | }
|
|---|
| 1103 |
|
|---|
| 1104 | /* Put back block, that contains block group descriptor */
|
|---|
| 1105 | errno_t rc = block_put(ref->block);
|
|---|
| 1106 | free(ref);
|
|---|
| 1107 |
|
|---|
| 1108 | return rc;
|
|---|
| 1109 | }
|
|---|
| 1110 |
|
|---|
| 1111 | /** Get reference to i-node specified by index.
|
|---|
| 1112 | *
|
|---|
| 1113 | * @param fs Filesystem to find i-node on
|
|---|
| 1114 | * @param index Index of i-node to load
|
|---|
| 1115 | * @oaram ref Output pointer for reference
|
|---|
| 1116 | *
|
|---|
| 1117 | * @return Error code
|
|---|
| 1118 | *
|
|---|
| 1119 | */
|
|---|
| 1120 | errno_t ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
|---|
| 1121 | ext4_inode_ref_t **ref)
|
|---|
| 1122 | {
|
|---|
| 1123 | /* Allocate memory for new structure */
|
|---|
| 1124 | ext4_inode_ref_t *newref =
|
|---|
| 1125 | malloc(sizeof(ext4_inode_ref_t));
|
|---|
| 1126 | if (newref == NULL)
|
|---|
| 1127 | return ENOMEM;
|
|---|
| 1128 |
|
|---|
| 1129 | /* Compute number of i-nodes, that fits in one data block */
|
|---|
| 1130 | uint32_t inodes_per_group =
|
|---|
| 1131 | ext4_superblock_get_inodes_per_group(fs->superblock);
|
|---|
| 1132 |
|
|---|
| 1133 | /*
|
|---|
| 1134 | * Inode numbers are 1-based, but it is simpler to work with 0-based
|
|---|
| 1135 | * when computing indices
|
|---|
| 1136 | */
|
|---|
| 1137 | index -= 1;
|
|---|
| 1138 | uint32_t block_group = index / inodes_per_group;
|
|---|
| 1139 | uint32_t offset_in_group = index % inodes_per_group;
|
|---|
| 1140 |
|
|---|
| 1141 | /* Load block group, where i-node is located */
|
|---|
| 1142 | ext4_block_group_ref_t *bg_ref;
|
|---|
| 1143 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
|---|
| 1144 | if (rc != EOK) {
|
|---|
| 1145 | free(newref);
|
|---|
| 1146 | return rc;
|
|---|
| 1147 | }
|
|---|
| 1148 |
|
|---|
| 1149 | /* Load block address, where i-node table is located */
|
|---|
| 1150 | uint32_t inode_table_start =
|
|---|
| 1151 | ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
|
|---|
| 1152 | fs->superblock);
|
|---|
| 1153 |
|
|---|
| 1154 | /* Put back block group reference (not needed more) */
|
|---|
| 1155 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
|---|
| 1156 | if (rc != EOK) {
|
|---|
| 1157 | free(newref);
|
|---|
| 1158 | return rc;
|
|---|
| 1159 | }
|
|---|
| 1160 |
|
|---|
| 1161 | /* Compute position of i-node in the block group */
|
|---|
| 1162 | uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
|---|
| 1163 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
|---|
| 1164 | uint32_t byte_offset_in_group = offset_in_group * inode_size;
|
|---|
| 1165 |
|
|---|
| 1166 | /* Compute block address */
|
|---|
| 1167 | aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
|
|---|
| 1168 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
|---|
| 1169 | if (rc != EOK) {
|
|---|
| 1170 | free(newref);
|
|---|
| 1171 | return rc;
|
|---|
| 1172 | }
|
|---|
| 1173 |
|
|---|
| 1174 | /* Compute position of i-node in the data block */
|
|---|
| 1175 | uint32_t offset_in_block = byte_offset_in_group % block_size;
|
|---|
| 1176 | newref->inode = newref->block->data + offset_in_block;
|
|---|
| 1177 |
|
|---|
| 1178 | /* We need to store the original value of index in the reference */
|
|---|
| 1179 | newref->index = index + 1;
|
|---|
| 1180 | newref->fs = fs;
|
|---|
| 1181 | newref->dirty = false;
|
|---|
| 1182 |
|
|---|
| 1183 | *ref = newref;
|
|---|
| 1184 |
|
|---|
| 1185 | return EOK;
|
|---|
| 1186 | }
|
|---|
| 1187 |
|
|---|
| 1188 | /** Put reference to i-node.
|
|---|
| 1189 | *
|
|---|
| 1190 | * @param ref Pointer for reference to be put back
|
|---|
| 1191 | *
|
|---|
| 1192 | * @return Error code
|
|---|
| 1193 | *
|
|---|
| 1194 | */
|
|---|
| 1195 | errno_t ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
|---|
| 1196 | {
|
|---|
| 1197 | /* Check if reference modified */
|
|---|
| 1198 | if (ref->dirty) {
|
|---|
| 1199 | /* Mark block dirty for writing changes to physical device */
|
|---|
| 1200 | ref->block->dirty = true;
|
|---|
| 1201 | }
|
|---|
| 1202 |
|
|---|
| 1203 | /* Put back block, that contains i-node */
|
|---|
| 1204 | errno_t rc = block_put(ref->block);
|
|---|
| 1205 | free(ref);
|
|---|
| 1206 |
|
|---|
| 1207 | return rc;
|
|---|
| 1208 | }
|
|---|
| 1209 |
|
|---|
| 1210 | /** Initialize newly allocated i-node in the filesystem.
|
|---|
| 1211 | *
|
|---|
| 1212 | * @param fs Filesystem to initialize i-node on
|
|---|
| 1213 | * @param index I-node index
|
|---|
| 1214 | * @param inode_ref Output pointer to return reference to allocated i-node
|
|---|
| 1215 | * @param flags Flags to be set for newly created i-node
|
|---|
| 1216 | *
|
|---|
| 1217 | * @return Error code
|
|---|
| 1218 | *
|
|---|
| 1219 | */
|
|---|
| 1220 | static errno_t ext4_filesystem_init_inode(ext4_filesystem_t *fs, uint32_t index,
|
|---|
| 1221 | ext4_inode_ref_t **inode_ref, int flags)
|
|---|
| 1222 | {
|
|---|
| 1223 | /* Check if newly allocated i-node will be a directory */
|
|---|
| 1224 | bool is_dir = false;
|
|---|
| 1225 | if (flags & L_DIRECTORY)
|
|---|
| 1226 | is_dir = true;
|
|---|
| 1227 |
|
|---|
| 1228 | /* Load i-node from on-disk i-node table */
|
|---|
| 1229 | errno_t rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
|
|---|
| 1230 | if (rc != EOK) {
|
|---|
| 1231 | ext4_ialloc_free_inode(fs, index, is_dir);
|
|---|
| 1232 | return rc;
|
|---|
| 1233 | }
|
|---|
| 1234 |
|
|---|
| 1235 | /* Initialize i-node */
|
|---|
| 1236 | ext4_inode_t *inode = (*inode_ref)->inode;
|
|---|
| 1237 |
|
|---|
| 1238 | uint16_t mode;
|
|---|
| 1239 | if (is_dir) {
|
|---|
| 1240 | /*
|
|---|
| 1241 | * Default directory permissions to be compatible with other systems
|
|---|
| 1242 | * 0777 (octal) == rwxrwxrwx
|
|---|
| 1243 | */
|
|---|
| 1244 |
|
|---|
| 1245 | mode = 0777;
|
|---|
| 1246 | mode |= EXT4_INODE_MODE_DIRECTORY;
|
|---|
| 1247 | ext4_inode_set_mode(fs->superblock, inode, mode);
|
|---|
| 1248 | ext4_inode_set_links_count(inode, 1); /* '.' entry */
|
|---|
| 1249 | } else {
|
|---|
| 1250 | /*
|
|---|
| 1251 | * Default file permissions to be compatible with other systems
|
|---|
| 1252 | * 0666 (octal) == rw-rw-rw-
|
|---|
| 1253 | */
|
|---|
| 1254 |
|
|---|
| 1255 | mode = 0666;
|
|---|
| 1256 | mode |= EXT4_INODE_MODE_FILE;
|
|---|
| 1257 | ext4_inode_set_mode(fs->superblock, inode, mode);
|
|---|
| 1258 | ext4_inode_set_links_count(inode, 0);
|
|---|
| 1259 | }
|
|---|
| 1260 |
|
|---|
| 1261 | ext4_inode_set_uid(inode, 0);
|
|---|
| 1262 | ext4_inode_set_gid(inode, 0);
|
|---|
| 1263 | ext4_inode_set_size(inode, 0);
|
|---|
| 1264 | ext4_inode_set_access_time(inode, 0);
|
|---|
| 1265 | ext4_inode_set_change_inode_time(inode, 0);
|
|---|
| 1266 | ext4_inode_set_modification_time(inode, 0);
|
|---|
| 1267 | ext4_inode_set_deletion_time(inode, 0);
|
|---|
| 1268 | ext4_inode_set_blocks_count(fs->superblock, inode, 0);
|
|---|
| 1269 | ext4_inode_set_flags(inode, 0);
|
|---|
| 1270 | ext4_inode_set_generation(inode, 0);
|
|---|
| 1271 |
|
|---|
| 1272 | /* Reset blocks array */
|
|---|
| 1273 | for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
|
|---|
| 1274 | inode->blocks[i] = 0;
|
|---|
| 1275 |
|
|---|
| 1276 | /* Initialize extents if needed */
|
|---|
| 1277 | if (ext4_superblock_has_feature_incompatible(
|
|---|
| 1278 | fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
|
|---|
| 1279 | ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
|
|---|
| 1280 |
|
|---|
| 1281 | /* Initialize extent root header */
|
|---|
| 1282 | ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
|
|---|
| 1283 | ext4_extent_header_set_depth(header, 0);
|
|---|
| 1284 | ext4_extent_header_set_entries_count(header, 0);
|
|---|
| 1285 | ext4_extent_header_set_generation(header, 0);
|
|---|
| 1286 | ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
|
|---|
| 1287 |
|
|---|
| 1288 | uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
|
|---|
| 1289 | sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
|
|---|
| 1290 |
|
|---|
| 1291 | ext4_extent_header_set_max_entries_count(header, max_entries);
|
|---|
| 1292 | }
|
|---|
| 1293 |
|
|---|
| 1294 | (*inode_ref)->dirty = true;
|
|---|
| 1295 |
|
|---|
| 1296 | return EOK;
|
|---|
| 1297 | }
|
|---|
| 1298 |
|
|---|
| 1299 | /** Allocate new i-node in the filesystem.
|
|---|
| 1300 | *
|
|---|
| 1301 | * @param fs Filesystem to allocated i-node on
|
|---|
| 1302 | * @param inode_ref Output pointer to return reference to allocated i-node
|
|---|
| 1303 | * @param flags Flags to be set for newly created i-node
|
|---|
| 1304 | *
|
|---|
| 1305 | * @return Error code
|
|---|
| 1306 | *
|
|---|
| 1307 | */
|
|---|
| 1308 | errno_t ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
|
|---|
| 1309 | ext4_inode_ref_t **inode_ref, int flags)
|
|---|
| 1310 | {
|
|---|
| 1311 | /* Check if newly allocated i-node will be a directory */
|
|---|
| 1312 | bool is_dir = false;
|
|---|
| 1313 | if (flags & L_DIRECTORY)
|
|---|
| 1314 | is_dir = true;
|
|---|
| 1315 |
|
|---|
| 1316 | /* Allocate inode by allocation algorithm */
|
|---|
| 1317 | uint32_t index;
|
|---|
| 1318 | errno_t rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
|
|---|
| 1319 | if (rc != EOK)
|
|---|
| 1320 | return rc;
|
|---|
| 1321 |
|
|---|
| 1322 | rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
|
|---|
| 1323 | if (rc != EOK) {
|
|---|
| 1324 | ext4_ialloc_free_inode(fs, index, is_dir);
|
|---|
| 1325 | return rc;
|
|---|
| 1326 | }
|
|---|
| 1327 |
|
|---|
| 1328 | return EOK;
|
|---|
| 1329 | }
|
|---|
| 1330 |
|
|---|
| 1331 | /** Allocate specific i-node in the filesystem.
|
|---|
| 1332 | *
|
|---|
| 1333 | * @param fs Filesystem to allocated i-node on
|
|---|
| 1334 | * @param index Index of i-node to allocate
|
|---|
| 1335 | * @param inode_ref Output pointer to return reference to allocated i-node
|
|---|
| 1336 | * @param flags Flags to be set for newly created i-node
|
|---|
| 1337 | *
|
|---|
| 1338 | * @return Error code
|
|---|
| 1339 | *
|
|---|
| 1340 | */
|
|---|
| 1341 | static errno_t ext4_filesystem_alloc_this_inode(ext4_filesystem_t *fs,
|
|---|
| 1342 | uint32_t index, ext4_inode_ref_t **inode_ref, int flags)
|
|---|
| 1343 | {
|
|---|
| 1344 | /* Check if newly allocated i-node will be a directory */
|
|---|
| 1345 | bool is_dir = false;
|
|---|
| 1346 | if (flags & L_DIRECTORY)
|
|---|
| 1347 | is_dir = true;
|
|---|
| 1348 |
|
|---|
| 1349 | /* Allocate inode by allocation algorithm */
|
|---|
| 1350 | errno_t rc = ext4_ialloc_alloc_this_inode(fs, index, is_dir);
|
|---|
| 1351 | if (rc != EOK)
|
|---|
| 1352 | return rc;
|
|---|
| 1353 |
|
|---|
| 1354 | rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
|
|---|
| 1355 | if (rc != EOK) {
|
|---|
| 1356 | ext4_ialloc_free_inode(fs, index, is_dir);
|
|---|
| 1357 | return rc;
|
|---|
| 1358 | }
|
|---|
| 1359 |
|
|---|
| 1360 | return EOK;
|
|---|
| 1361 | }
|
|---|
| 1362 |
|
|---|
| 1363 | /** Release i-node and mark it as free.
|
|---|
| 1364 | *
|
|---|
| 1365 | * @param inode_ref I-node to be released
|
|---|
| 1366 | *
|
|---|
| 1367 | * @return Error code
|
|---|
| 1368 | *
|
|---|
| 1369 | */
|
|---|
| 1370 | errno_t ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
|
|---|
| 1371 | {
|
|---|
| 1372 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 1373 |
|
|---|
| 1374 | /* For extents must be data block destroyed by other way */
|
|---|
| 1375 | if ((ext4_superblock_has_feature_incompatible(fs->superblock,
|
|---|
| 1376 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1377 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
|---|
| 1378 | /* Data structures are released during truncate operation... */
|
|---|
| 1379 | goto finish;
|
|---|
| 1380 | }
|
|---|
| 1381 |
|
|---|
| 1382 | /* Release all indirect (no data) blocks */
|
|---|
| 1383 |
|
|---|
| 1384 | /* 1) Single indirect */
|
|---|
| 1385 | uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
|
|---|
| 1386 | if (fblock != 0) {
|
|---|
| 1387 | errno_t rc = ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 1388 | if (rc != EOK)
|
|---|
| 1389 | return rc;
|
|---|
| 1390 |
|
|---|
| 1391 | ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
|
|---|
| 1392 | }
|
|---|
| 1393 |
|
|---|
| 1394 | block_t *block;
|
|---|
| 1395 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
|---|
| 1396 | uint32_t count = block_size / sizeof(uint32_t);
|
|---|
| 1397 |
|
|---|
| 1398 | /* 2) Double indirect */
|
|---|
| 1399 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
|
|---|
| 1400 | if (fblock != 0) {
|
|---|
| 1401 | errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
|---|
| 1402 | if (rc != EOK)
|
|---|
| 1403 | return rc;
|
|---|
| 1404 |
|
|---|
| 1405 | uint32_t ind_block;
|
|---|
| 1406 | for (uint32_t offset = 0; offset < count; ++offset) {
|
|---|
| 1407 | ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
|
|---|
| 1408 |
|
|---|
| 1409 | if (ind_block != 0) {
|
|---|
| 1410 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
|---|
| 1411 | if (rc != EOK) {
|
|---|
| 1412 | block_put(block);
|
|---|
| 1413 | return rc;
|
|---|
| 1414 | }
|
|---|
| 1415 | }
|
|---|
| 1416 | }
|
|---|
| 1417 |
|
|---|
| 1418 | rc = block_put(block);
|
|---|
| 1419 | if (rc != EOK)
|
|---|
| 1420 | return rc;
|
|---|
| 1421 |
|
|---|
| 1422 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 1423 | if (rc != EOK)
|
|---|
| 1424 | return rc;
|
|---|
| 1425 |
|
|---|
| 1426 | ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
|
|---|
| 1427 | }
|
|---|
| 1428 |
|
|---|
| 1429 | /* 3) Tripple indirect */
|
|---|
| 1430 | block_t *subblock;
|
|---|
| 1431 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
|
|---|
| 1432 | if (fblock != 0) {
|
|---|
| 1433 | errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
|---|
| 1434 | if (rc != EOK)
|
|---|
| 1435 | return rc;
|
|---|
| 1436 |
|
|---|
| 1437 | uint32_t ind_block;
|
|---|
| 1438 | for (uint32_t offset = 0; offset < count; ++offset) {
|
|---|
| 1439 | ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
|
|---|
| 1440 |
|
|---|
| 1441 | if (ind_block != 0) {
|
|---|
| 1442 | rc = block_get(&subblock, fs->device, ind_block,
|
|---|
| 1443 | BLOCK_FLAGS_NONE);
|
|---|
| 1444 | if (rc != EOK) {
|
|---|
| 1445 | block_put(block);
|
|---|
| 1446 | return rc;
|
|---|
| 1447 | }
|
|---|
| 1448 |
|
|---|
| 1449 | uint32_t ind_subblock;
|
|---|
| 1450 | for (uint32_t suboffset = 0; suboffset < count;
|
|---|
| 1451 | ++suboffset) {
|
|---|
| 1452 | ind_subblock = uint32_t_le2host(((uint32_t *)
|
|---|
| 1453 | subblock->data)[suboffset]);
|
|---|
| 1454 |
|
|---|
| 1455 | if (ind_subblock != 0) {
|
|---|
| 1456 | rc = ext4_balloc_free_block(inode_ref, ind_subblock);
|
|---|
| 1457 | if (rc != EOK) {
|
|---|
| 1458 | block_put(subblock);
|
|---|
| 1459 | block_put(block);
|
|---|
| 1460 | return rc;
|
|---|
| 1461 | }
|
|---|
| 1462 | }
|
|---|
| 1463 | }
|
|---|
| 1464 |
|
|---|
| 1465 | rc = block_put(subblock);
|
|---|
| 1466 | if (rc != EOK) {
|
|---|
| 1467 | block_put(block);
|
|---|
| 1468 | return rc;
|
|---|
| 1469 | }
|
|---|
| 1470 | }
|
|---|
| 1471 |
|
|---|
| 1472 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
|---|
| 1473 | if (rc != EOK) {
|
|---|
| 1474 | block_put(block);
|
|---|
| 1475 | return rc;
|
|---|
| 1476 | }
|
|---|
| 1477 | }
|
|---|
| 1478 |
|
|---|
| 1479 | rc = block_put(block);
|
|---|
| 1480 | if (rc != EOK)
|
|---|
| 1481 | return rc;
|
|---|
| 1482 |
|
|---|
| 1483 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 1484 | if (rc != EOK)
|
|---|
| 1485 | return rc;
|
|---|
| 1486 |
|
|---|
| 1487 | ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
|
|---|
| 1488 | }
|
|---|
| 1489 |
|
|---|
| 1490 | finish:
|
|---|
| 1491 | /* Mark inode dirty for writing to the physical device */
|
|---|
| 1492 | inode_ref->dirty = true;
|
|---|
| 1493 |
|
|---|
| 1494 | /* Free block with extended attributes if present */
|
|---|
| 1495 | uint32_t xattr_block = ext4_inode_get_file_acl(
|
|---|
| 1496 | inode_ref->inode, fs->superblock);
|
|---|
| 1497 | if (xattr_block) {
|
|---|
| 1498 | errno_t rc = ext4_balloc_free_block(inode_ref, xattr_block);
|
|---|
| 1499 | if (rc != EOK)
|
|---|
| 1500 | return rc;
|
|---|
| 1501 |
|
|---|
| 1502 | ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
|
|---|
| 1503 | }
|
|---|
| 1504 |
|
|---|
| 1505 | /* Free inode by allocator */
|
|---|
| 1506 | errno_t rc;
|
|---|
| 1507 | if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
|
|---|
| 1508 | EXT4_INODE_MODE_DIRECTORY))
|
|---|
| 1509 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
|
|---|
| 1510 | else
|
|---|
| 1511 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
|
|---|
| 1512 |
|
|---|
| 1513 | return rc;
|
|---|
| 1514 | }
|
|---|
| 1515 |
|
|---|
| 1516 | /** Truncate i-node data blocks.
|
|---|
| 1517 | *
|
|---|
| 1518 | * @param inode_ref I-node to be truncated
|
|---|
| 1519 | * @param new_size New size of inode (must be < current size)
|
|---|
| 1520 | *
|
|---|
| 1521 | * @return Error code
|
|---|
| 1522 | *
|
|---|
| 1523 | */
|
|---|
| 1524 | errno_t ext4_filesystem_truncate_inode(ext4_inode_ref_t *inode_ref,
|
|---|
| 1525 | aoff64_t new_size)
|
|---|
| 1526 | {
|
|---|
| 1527 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
|---|
| 1528 |
|
|---|
| 1529 | /* Check flags, if i-node can be truncated */
|
|---|
| 1530 | if (!ext4_inode_can_truncate(sb, inode_ref->inode))
|
|---|
| 1531 | return EINVAL;
|
|---|
| 1532 |
|
|---|
| 1533 | /* If sizes are equal, nothing has to be done. */
|
|---|
| 1534 | aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
|
|---|
| 1535 | if (old_size == new_size)
|
|---|
| 1536 | return EOK;
|
|---|
| 1537 |
|
|---|
| 1538 | /* It's not suppported to make the larger file by truncate operation */
|
|---|
| 1539 | if (old_size < new_size)
|
|---|
| 1540 | return EINVAL;
|
|---|
| 1541 |
|
|---|
| 1542 | /* Compute how many blocks will be released */
|
|---|
| 1543 | aoff64_t size_diff = old_size - new_size;
|
|---|
| 1544 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 1545 | uint32_t diff_blocks_count = size_diff / block_size;
|
|---|
| 1546 | if (size_diff % block_size != 0)
|
|---|
| 1547 | diff_blocks_count++;
|
|---|
| 1548 |
|
|---|
| 1549 | uint32_t old_blocks_count = old_size / block_size;
|
|---|
| 1550 | if (old_size % block_size != 0)
|
|---|
| 1551 | old_blocks_count++;
|
|---|
| 1552 |
|
|---|
| 1553 | if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
|
|---|
| 1554 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1555 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
|---|
| 1556 | /* Extents require special operation */
|
|---|
| 1557 | errno_t rc = ext4_extent_release_blocks_from(inode_ref,
|
|---|
| 1558 | old_blocks_count - diff_blocks_count);
|
|---|
| 1559 | if (rc != EOK)
|
|---|
| 1560 | return rc;
|
|---|
| 1561 | } else {
|
|---|
| 1562 | /* Release data blocks from the end of file */
|
|---|
| 1563 |
|
|---|
| 1564 | /* Starting from 1 because of logical blocks are numbered from 0 */
|
|---|
| 1565 | for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
|
|---|
| 1566 | errno_t rc = ext4_filesystem_release_inode_block(inode_ref,
|
|---|
| 1567 | old_blocks_count - i);
|
|---|
| 1568 | if (rc != EOK)
|
|---|
| 1569 | return rc;
|
|---|
| 1570 | }
|
|---|
| 1571 | }
|
|---|
| 1572 |
|
|---|
| 1573 | /* Update i-node */
|
|---|
| 1574 | ext4_inode_set_size(inode_ref->inode, new_size);
|
|---|
| 1575 | inode_ref->dirty = true;
|
|---|
| 1576 |
|
|---|
| 1577 | return EOK;
|
|---|
| 1578 | }
|
|---|
| 1579 |
|
|---|
| 1580 | /** Get physical block address by logical index of the block.
|
|---|
| 1581 | *
|
|---|
| 1582 | * @param inode_ref I-node to read block address from
|
|---|
| 1583 | * @param iblock Logical index of block
|
|---|
| 1584 | * @param fblock Output pointer for return physical block address
|
|---|
| 1585 | *
|
|---|
| 1586 | * @return Error code
|
|---|
| 1587 | *
|
|---|
| 1588 | */
|
|---|
| 1589 | errno_t ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
|---|
| 1590 | aoff64_t iblock, uint32_t *fblock)
|
|---|
| 1591 | {
|
|---|
| 1592 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 1593 |
|
|---|
| 1594 | /* For empty file is situation simple */
|
|---|
| 1595 | if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
|
|---|
| 1596 | *fblock = 0;
|
|---|
| 1597 | return EOK;
|
|---|
| 1598 | }
|
|---|
| 1599 |
|
|---|
| 1600 | uint32_t current_block;
|
|---|
| 1601 |
|
|---|
| 1602 | /* Handle i-node using extents */
|
|---|
| 1603 | if ((ext4_superblock_has_feature_incompatible(fs->superblock,
|
|---|
| 1604 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1605 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
|---|
| 1606 | errno_t rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
|
|---|
| 1607 | if (rc != EOK)
|
|---|
| 1608 | return rc;
|
|---|
| 1609 |
|
|---|
| 1610 | *fblock = current_block;
|
|---|
| 1611 | return EOK;
|
|---|
| 1612 | }
|
|---|
| 1613 |
|
|---|
| 1614 | ext4_inode_t *inode = inode_ref->inode;
|
|---|
| 1615 |
|
|---|
| 1616 | /* Direct block are read directly from array in i-node structure */
|
|---|
| 1617 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
|---|
| 1618 | current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
|
|---|
| 1619 | *fblock = current_block;
|
|---|
| 1620 | return EOK;
|
|---|
| 1621 | }
|
|---|
| 1622 |
|
|---|
| 1623 | /* Determine indirection level of the target block */
|
|---|
| 1624 | unsigned int level = 0;
|
|---|
| 1625 | for (unsigned int i = 1; i < 4; i++) {
|
|---|
| 1626 | if (iblock < fs->inode_block_limits[i]) {
|
|---|
| 1627 | level = i;
|
|---|
| 1628 | break;
|
|---|
| 1629 | }
|
|---|
| 1630 | }
|
|---|
| 1631 |
|
|---|
| 1632 | if (level == 0)
|
|---|
| 1633 | return EIO;
|
|---|
| 1634 |
|
|---|
| 1635 | /* Compute offsets for the topmost level */
|
|---|
| 1636 | aoff64_t block_offset_in_level =
|
|---|
| 1637 | iblock - fs->inode_block_limits[level - 1];
|
|---|
| 1638 | current_block = ext4_inode_get_indirect_block(inode, level - 1);
|
|---|
| 1639 | uint32_t offset_in_block =
|
|---|
| 1640 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1641 |
|
|---|
| 1642 | /* Sparse file */
|
|---|
| 1643 | if (current_block == 0) {
|
|---|
| 1644 | *fblock = 0;
|
|---|
| 1645 | return EOK;
|
|---|
| 1646 | }
|
|---|
| 1647 |
|
|---|
| 1648 | block_t *block;
|
|---|
| 1649 |
|
|---|
| 1650 | /*
|
|---|
| 1651 | * Navigate through other levels, until we find the block number
|
|---|
| 1652 | * or find null reference meaning we are dealing with sparse file
|
|---|
| 1653 | */
|
|---|
| 1654 | while (level > 0) {
|
|---|
| 1655 | /* Load indirect block */
|
|---|
| 1656 | errno_t rc = block_get(&block, fs->device, current_block, 0);
|
|---|
| 1657 | if (rc != EOK)
|
|---|
| 1658 | return rc;
|
|---|
| 1659 |
|
|---|
| 1660 | /* Read block address from indirect block */
|
|---|
| 1661 | current_block =
|
|---|
| 1662 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
|---|
| 1663 |
|
|---|
| 1664 | /* Put back indirect block untouched */
|
|---|
| 1665 | rc = block_put(block);
|
|---|
| 1666 | if (rc != EOK)
|
|---|
| 1667 | return rc;
|
|---|
| 1668 |
|
|---|
| 1669 | /* Check for sparse file */
|
|---|
| 1670 | if (current_block == 0) {
|
|---|
| 1671 | *fblock = 0;
|
|---|
| 1672 | return EOK;
|
|---|
| 1673 | }
|
|---|
| 1674 |
|
|---|
| 1675 | /* Jump to the next level */
|
|---|
| 1676 | level--;
|
|---|
| 1677 |
|
|---|
| 1678 | /* Termination condition - we have address of data block loaded */
|
|---|
| 1679 | if (level == 0)
|
|---|
| 1680 | break;
|
|---|
| 1681 |
|
|---|
| 1682 | /* Visit the next level */
|
|---|
| 1683 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
|---|
| 1684 | offset_in_block =
|
|---|
| 1685 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1686 | }
|
|---|
| 1687 |
|
|---|
| 1688 | *fblock = current_block;
|
|---|
| 1689 |
|
|---|
| 1690 | return EOK;
|
|---|
| 1691 | }
|
|---|
| 1692 |
|
|---|
| 1693 | /** Set physical block address for the block logical address into the i-node.
|
|---|
| 1694 | *
|
|---|
| 1695 | * @param inode_ref I-node to set block address to
|
|---|
| 1696 | * @param iblock Logical index of block
|
|---|
| 1697 | * @param fblock Physical block address
|
|---|
| 1698 | *
|
|---|
| 1699 | * @return Error code
|
|---|
| 1700 | *
|
|---|
| 1701 | */
|
|---|
| 1702 | errno_t ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
|---|
| 1703 | aoff64_t iblock, uint32_t fblock)
|
|---|
| 1704 | {
|
|---|
| 1705 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 1706 |
|
|---|
| 1707 | /* Handle inode using extents */
|
|---|
| 1708 | if ((ext4_superblock_has_feature_compatible(fs->superblock,
|
|---|
| 1709 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1710 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
|---|
| 1711 | /* Not reachable */
|
|---|
| 1712 | return ENOTSUP;
|
|---|
| 1713 | }
|
|---|
| 1714 |
|
|---|
| 1715 | /* Handle simple case when we are dealing with direct reference */
|
|---|
| 1716 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
|---|
| 1717 | ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
|
|---|
| 1718 | inode_ref->dirty = true;
|
|---|
| 1719 |
|
|---|
| 1720 | return EOK;
|
|---|
| 1721 | }
|
|---|
| 1722 |
|
|---|
| 1723 | /* Determine the indirection level needed to get the desired block */
|
|---|
| 1724 | unsigned int level = 0;
|
|---|
| 1725 | for (unsigned int i = 1; i < 4; i++) {
|
|---|
| 1726 | if (iblock < fs->inode_block_limits[i]) {
|
|---|
| 1727 | level = i;
|
|---|
| 1728 | break;
|
|---|
| 1729 | }
|
|---|
| 1730 | }
|
|---|
| 1731 |
|
|---|
| 1732 | if (level == 0)
|
|---|
| 1733 | return EIO;
|
|---|
| 1734 |
|
|---|
| 1735 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
|---|
| 1736 |
|
|---|
| 1737 | /* Compute offsets for the topmost level */
|
|---|
| 1738 | aoff64_t block_offset_in_level =
|
|---|
| 1739 | iblock - fs->inode_block_limits[level - 1];
|
|---|
| 1740 | uint32_t current_block =
|
|---|
| 1741 | ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
|
|---|
| 1742 | uint32_t offset_in_block =
|
|---|
| 1743 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1744 |
|
|---|
| 1745 | uint32_t new_block_addr;
|
|---|
| 1746 | block_t *block;
|
|---|
| 1747 | block_t *new_block;
|
|---|
| 1748 |
|
|---|
| 1749 | /* Is needed to allocate indirect block on the i-node level */
|
|---|
| 1750 | if (current_block == 0) {
|
|---|
| 1751 | /* Allocate new indirect block */
|
|---|
| 1752 | errno_t rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
|---|
| 1753 | if (rc != EOK)
|
|---|
| 1754 | return rc;
|
|---|
| 1755 |
|
|---|
| 1756 | /* Update i-node */
|
|---|
| 1757 | ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
|
|---|
| 1758 | new_block_addr);
|
|---|
| 1759 | inode_ref->dirty = true;
|
|---|
| 1760 |
|
|---|
| 1761 | /* Load newly allocated block */
|
|---|
| 1762 | rc = block_get(&new_block, fs->device, new_block_addr,
|
|---|
| 1763 | BLOCK_FLAGS_NOREAD);
|
|---|
| 1764 | if (rc != EOK) {
|
|---|
| 1765 | ext4_balloc_free_block(inode_ref, new_block_addr);
|
|---|
| 1766 | return rc;
|
|---|
| 1767 | }
|
|---|
| 1768 |
|
|---|
| 1769 | /* Initialize new block */
|
|---|
| 1770 | memset(new_block->data, 0, block_size);
|
|---|
| 1771 | new_block->dirty = true;
|
|---|
| 1772 |
|
|---|
| 1773 | /* Put back the allocated block */
|
|---|
| 1774 | rc = block_put(new_block);
|
|---|
| 1775 | if (rc != EOK)
|
|---|
| 1776 | return rc;
|
|---|
| 1777 |
|
|---|
| 1778 | current_block = new_block_addr;
|
|---|
| 1779 | }
|
|---|
| 1780 |
|
|---|
| 1781 | /*
|
|---|
| 1782 | * Navigate through other levels, until we find the block number
|
|---|
| 1783 | * or find null reference meaning we are dealing with sparse file
|
|---|
| 1784 | */
|
|---|
| 1785 | while (level > 0) {
|
|---|
| 1786 | errno_t rc = block_get(&block, fs->device, current_block, 0);
|
|---|
| 1787 | if (rc != EOK)
|
|---|
| 1788 | return rc;
|
|---|
| 1789 |
|
|---|
| 1790 | current_block =
|
|---|
| 1791 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
|---|
| 1792 |
|
|---|
| 1793 | if ((level > 1) && (current_block == 0)) {
|
|---|
| 1794 | /* Allocate new block */
|
|---|
| 1795 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
|---|
| 1796 | if (rc != EOK) {
|
|---|
| 1797 | block_put(block);
|
|---|
| 1798 | return rc;
|
|---|
| 1799 | }
|
|---|
| 1800 |
|
|---|
| 1801 | /* Load newly allocated block */
|
|---|
| 1802 | rc = block_get(&new_block, fs->device, new_block_addr,
|
|---|
| 1803 | BLOCK_FLAGS_NOREAD);
|
|---|
| 1804 | if (rc != EOK) {
|
|---|
| 1805 | block_put(block);
|
|---|
| 1806 | return rc;
|
|---|
| 1807 | }
|
|---|
| 1808 |
|
|---|
| 1809 | /* Initialize allocated block */
|
|---|
| 1810 | memset(new_block->data, 0, block_size);
|
|---|
| 1811 | new_block->dirty = true;
|
|---|
| 1812 |
|
|---|
| 1813 | rc = block_put(new_block);
|
|---|
| 1814 | if (rc != EOK) {
|
|---|
| 1815 | block_put(block);
|
|---|
| 1816 | return rc;
|
|---|
| 1817 | }
|
|---|
| 1818 |
|
|---|
| 1819 | /* Write block address to the parent */
|
|---|
| 1820 | ((uint32_t *) block->data)[offset_in_block] =
|
|---|
| 1821 | host2uint32_t_le(new_block_addr);
|
|---|
| 1822 | block->dirty = true;
|
|---|
| 1823 | current_block = new_block_addr;
|
|---|
| 1824 | }
|
|---|
| 1825 |
|
|---|
| 1826 | /* Will be finished, write the fblock address */
|
|---|
| 1827 | if (level == 1) {
|
|---|
| 1828 | ((uint32_t *) block->data)[offset_in_block] =
|
|---|
| 1829 | host2uint32_t_le(fblock);
|
|---|
| 1830 | block->dirty = true;
|
|---|
| 1831 | }
|
|---|
| 1832 |
|
|---|
| 1833 | rc = block_put(block);
|
|---|
| 1834 | if (rc != EOK)
|
|---|
| 1835 | return rc;
|
|---|
| 1836 |
|
|---|
| 1837 | level--;
|
|---|
| 1838 |
|
|---|
| 1839 | /*
|
|---|
| 1840 | * If we are on the last level, break here as
|
|---|
| 1841 | * there is no next level to visit
|
|---|
| 1842 | */
|
|---|
| 1843 | if (level == 0)
|
|---|
| 1844 | break;
|
|---|
| 1845 |
|
|---|
| 1846 | /* Visit the next level */
|
|---|
| 1847 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
|---|
| 1848 | offset_in_block =
|
|---|
| 1849 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1850 | }
|
|---|
| 1851 |
|
|---|
| 1852 | return EOK;
|
|---|
| 1853 | }
|
|---|
| 1854 |
|
|---|
| 1855 | /** Release data block from i-node
|
|---|
| 1856 | *
|
|---|
| 1857 | * @param inode_ref I-node to release block from
|
|---|
| 1858 | * @param iblock Logical block to be released
|
|---|
| 1859 | *
|
|---|
| 1860 | * @return Error code
|
|---|
| 1861 | *
|
|---|
| 1862 | */
|
|---|
| 1863 | errno_t ext4_filesystem_release_inode_block(ext4_inode_ref_t *inode_ref,
|
|---|
| 1864 | uint32_t iblock)
|
|---|
| 1865 | {
|
|---|
| 1866 | uint32_t fblock;
|
|---|
| 1867 |
|
|---|
| 1868 | ext4_filesystem_t *fs = inode_ref->fs;
|
|---|
| 1869 |
|
|---|
| 1870 | /* Extents are handled otherwise = there is not support in this function */
|
|---|
| 1871 | assert(!(ext4_superblock_has_feature_incompatible(fs->superblock,
|
|---|
| 1872 | EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
|---|
| 1873 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
|
|---|
| 1874 |
|
|---|
| 1875 | ext4_inode_t *inode = inode_ref->inode;
|
|---|
| 1876 |
|
|---|
| 1877 | /* Handle simple case when we are dealing with direct reference */
|
|---|
| 1878 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
|---|
| 1879 | fblock = ext4_inode_get_direct_block(inode, iblock);
|
|---|
| 1880 |
|
|---|
| 1881 | /* Sparse file */
|
|---|
| 1882 | if (fblock == 0)
|
|---|
| 1883 | return EOK;
|
|---|
| 1884 |
|
|---|
| 1885 | ext4_inode_set_direct_block(inode, iblock, 0);
|
|---|
| 1886 | return ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 1887 | }
|
|---|
| 1888 |
|
|---|
| 1889 | /* Determine the indirection level needed to get the desired block */
|
|---|
| 1890 | unsigned int level = 0;
|
|---|
| 1891 | for (unsigned int i = 1; i < 4; i++) {
|
|---|
| 1892 | if (iblock < fs->inode_block_limits[i]) {
|
|---|
| 1893 | level = i;
|
|---|
| 1894 | break;
|
|---|
| 1895 | }
|
|---|
| 1896 | }
|
|---|
| 1897 |
|
|---|
| 1898 | if (level == 0)
|
|---|
| 1899 | return EIO;
|
|---|
| 1900 |
|
|---|
| 1901 | /* Compute offsets for the topmost level */
|
|---|
| 1902 | aoff64_t block_offset_in_level =
|
|---|
| 1903 | iblock - fs->inode_block_limits[level - 1];
|
|---|
| 1904 | uint32_t current_block =
|
|---|
| 1905 | ext4_inode_get_indirect_block(inode, level - 1);
|
|---|
| 1906 | uint32_t offset_in_block =
|
|---|
| 1907 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1908 |
|
|---|
| 1909 | /*
|
|---|
| 1910 | * Navigate through other levels, until we find the block number
|
|---|
| 1911 | * or find null reference meaning we are dealing with sparse file
|
|---|
| 1912 | */
|
|---|
| 1913 | block_t *block;
|
|---|
| 1914 | while (level > 0) {
|
|---|
| 1915 |
|
|---|
| 1916 | /* Sparse check */
|
|---|
| 1917 | if (current_block == 0)
|
|---|
| 1918 | return EOK;
|
|---|
| 1919 |
|
|---|
| 1920 | errno_t rc = block_get(&block, fs->device, current_block, 0);
|
|---|
| 1921 | if (rc != EOK)
|
|---|
| 1922 | return rc;
|
|---|
| 1923 |
|
|---|
| 1924 | current_block =
|
|---|
| 1925 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
|---|
| 1926 |
|
|---|
| 1927 | /* Set zero if physical data block address found */
|
|---|
| 1928 | if (level == 1) {
|
|---|
| 1929 | ((uint32_t *) block->data)[offset_in_block] =
|
|---|
| 1930 | host2uint32_t_le(0);
|
|---|
| 1931 | block->dirty = true;
|
|---|
| 1932 | }
|
|---|
| 1933 |
|
|---|
| 1934 | rc = block_put(block);
|
|---|
| 1935 | if (rc != EOK)
|
|---|
| 1936 | return rc;
|
|---|
| 1937 |
|
|---|
| 1938 | level--;
|
|---|
| 1939 |
|
|---|
| 1940 | /*
|
|---|
| 1941 | * If we are on the last level, break here as
|
|---|
| 1942 | * there is no next level to visit
|
|---|
| 1943 | */
|
|---|
| 1944 | if (level == 0)
|
|---|
| 1945 | break;
|
|---|
| 1946 |
|
|---|
| 1947 | /* Visit the next level */
|
|---|
| 1948 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
|---|
| 1949 | offset_in_block =
|
|---|
| 1950 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
|---|
| 1951 | }
|
|---|
| 1952 |
|
|---|
| 1953 | fblock = current_block;
|
|---|
| 1954 | if (fblock == 0)
|
|---|
| 1955 | return EOK;
|
|---|
| 1956 |
|
|---|
| 1957 | /* Physical block is not referenced, it can be released */
|
|---|
| 1958 | return ext4_balloc_free_block(inode_ref, fblock);
|
|---|
| 1959 | }
|
|---|
| 1960 |
|
|---|
| 1961 | /** Append following logical block to the i-node.
|
|---|
| 1962 | *
|
|---|
| 1963 | * @param inode_ref I-node to append block to
|
|---|
| 1964 | * @param fblock Output physical block address of newly allocated block
|
|---|
| 1965 | * @param iblock Output logical number of newly allocated block
|
|---|
| 1966 | *
|
|---|
| 1967 | * @return Error code
|
|---|
| 1968 | *
|
|---|
| 1969 | */
|
|---|
| 1970 | errno_t ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
|
|---|
| 1971 | uint32_t *fblock, uint32_t *iblock)
|
|---|
| 1972 | {
|
|---|
| 1973 | /* Handle extents separately */
|
|---|
| 1974 | if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
|
|---|
| 1975 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
|---|
| 1976 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))
|
|---|
| 1977 | return ext4_extent_append_block(inode_ref, iblock, fblock, true);
|
|---|
| 1978 |
|
|---|
| 1979 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
|---|
| 1980 |
|
|---|
| 1981 | /* Compute next block index and allocate data block */
|
|---|
| 1982 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
|---|
| 1983 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 1984 |
|
|---|
| 1985 | /* Align size i-node size */
|
|---|
| 1986 | if ((inode_size % block_size) != 0)
|
|---|
| 1987 | inode_size += block_size - (inode_size % block_size);
|
|---|
| 1988 |
|
|---|
| 1989 | /* Logical blocks are numbered from 0 */
|
|---|
| 1990 | uint32_t new_block_idx = inode_size / block_size;
|
|---|
| 1991 |
|
|---|
| 1992 | /* Allocate new physical block */
|
|---|
| 1993 | uint32_t phys_block;
|
|---|
| 1994 | errno_t rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
|
|---|
| 1995 | if (rc != EOK)
|
|---|
| 1996 | return rc;
|
|---|
| 1997 |
|
|---|
| 1998 | /* Add physical block address to the i-node */
|
|---|
| 1999 | rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
|
|---|
| 2000 | new_block_idx, phys_block);
|
|---|
| 2001 | if (rc != EOK) {
|
|---|
| 2002 | ext4_balloc_free_block(inode_ref, phys_block);
|
|---|
| 2003 | return rc;
|
|---|
| 2004 | }
|
|---|
| 2005 |
|
|---|
| 2006 | /* Update i-node */
|
|---|
| 2007 | ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
|
|---|
| 2008 | inode_ref->dirty = true;
|
|---|
| 2009 |
|
|---|
| 2010 | *fblock = phys_block;
|
|---|
| 2011 | *iblock = new_block_idx;
|
|---|
| 2012 |
|
|---|
| 2013 | return EOK;
|
|---|
| 2014 | }
|
|---|
| 2015 |
|
|---|
| 2016 | /** Get the number of inodes per block.
|
|---|
| 2017 | *
|
|---|
| 2018 | * @param sb Superblock
|
|---|
| 2019 | * @return Number of inodes per block
|
|---|
| 2020 | */
|
|---|
| 2021 | static uint32_t ext4_filesystem_inodes_per_block(ext4_superblock_t *sb)
|
|---|
| 2022 | {
|
|---|
| 2023 | uint32_t inode_size = ext4_superblock_get_inode_size(sb);
|
|---|
| 2024 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
|---|
| 2025 |
|
|---|
| 2026 | return block_size / inode_size;
|
|---|
| 2027 | }
|
|---|
| 2028 |
|
|---|
| 2029 | /**
|
|---|
| 2030 | * @}
|
|---|
| 2031 | */
|
|---|