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