[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 | /** Convert block address to relative index in block group.
|
---|
| 532 | *
|
---|
[38542dc] | 533 | * @param sb Superblock pointer
|
---|
| 534 | * @param block_addr Block number to convert
|
---|
| 535 | *
|
---|
| 536 | * @return Relative number of block
|
---|
| 537 | *
|
---|
[4cdac68] | 538 | */
|
---|
| 539 | uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *sb,
|
---|
[38542dc] | 540 | uint32_t block_addr)
|
---|
[4cdac68] | 541 | {
|
---|
| 542 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
| 543 | uint32_t first_block = ext4_superblock_get_first_data_block(sb);
|
---|
[a35b458] | 544 |
|
---|
[4cdac68] | 545 | /* First block == 0 or 1 */
|
---|
[38542dc] | 546 | if (first_block == 0)
|
---|
[4cdac68] | 547 | return block_addr % blocks_per_group;
|
---|
[38542dc] | 548 | else
|
---|
[4cdac68] | 549 | return (block_addr - 1) % blocks_per_group;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | /** Convert relative block address in group to absolute address.
|
---|
| 553 | *
|
---|
[38542dc] | 554 | * @param sb Superblock pointer
|
---|
| 555 | *
|
---|
| 556 | * @return Absolute block address
|
---|
| 557 | *
|
---|
[4cdac68] | 558 | */
|
---|
| 559 | uint32_t ext4_filesystem_index_in_group2blockaddr(ext4_superblock_t *sb,
|
---|
[38542dc] | 560 | uint32_t index, uint32_t bgid)
|
---|
[4cdac68] | 561 | {
|
---|
| 562 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
[a35b458] | 563 |
|
---|
[38542dc] | 564 | if (ext4_superblock_get_first_data_block(sb) == 0)
|
---|
[4cdac68] | 565 | return bgid * blocks_per_group + index;
|
---|
[38542dc] | 566 | else
|
---|
[4cdac68] | 567 | return bgid * blocks_per_group + index + 1;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
[d76973c] | 570 | /** Convert the absolute block number to group number
|
---|
| 571 | *
|
---|
| 572 | * @param sb Pointer to the superblock
|
---|
| 573 | * @param b Absolute block number
|
---|
| 574 | *
|
---|
| 575 | * @return Group number
|
---|
| 576 | */
|
---|
| 577 | uint32_t ext4_filesystem_blockaddr2group(ext4_superblock_t *sb, uint64_t b)
|
---|
| 578 | {
|
---|
| 579 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
[60c409c] | 580 | uint32_t first_block = ext4_superblock_get_first_data_block(sb);
|
---|
[d76973c] | 581 |
|
---|
[60c409c] | 582 | return (b - first_block) / blocks_per_group;
|
---|
[d76973c] | 583 | }
|
---|
| 584 |
|
---|
[aab85d90] | 585 | /** Initialize block group structures
|
---|
| 586 | */
|
---|
| 587 | static errno_t ext4_filesystem_init_block_groups(ext4_filesystem_t *fs)
|
---|
| 588 | {
|
---|
| 589 | errno_t rc;
|
---|
| 590 | block_t *block;
|
---|
| 591 | aoff64_t b;
|
---|
| 592 | ext4_block_group_t *bg;
|
---|
| 593 | ext4_superblock_t *sb = fs->superblock;
|
---|
| 594 | ext4_block_group_ref_t *bg_ref;
|
---|
| 595 |
|
---|
| 596 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
| 597 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 598 | uint32_t desc_size = ext4_superblock_get_desc_size(fs->superblock);
|
---|
| 599 | /* Number of descriptors per block */
|
---|
| 600 | uint32_t descriptors_per_block =
|
---|
| 601 | ext4_superblock_get_block_size(fs->superblock) / desc_size;
|
---|
| 602 | /* Block where block group descriptor (and first block group) starts */
|
---|
| 603 | aoff64_t block_id =
|
---|
| 604 | ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
---|
| 605 | /* Number of blocks containing descriptor table */
|
---|
| 606 | uint32_t dtable_blocks =
|
---|
| 607 | (block_group_count + descriptors_per_block - 1) /
|
---|
| 608 | descriptors_per_block;
|
---|
| 609 |
|
---|
| 610 | uint32_t bg_index;
|
---|
| 611 | aoff64_t bg_block0;
|
---|
| 612 | uint32_t dcnt;
|
---|
| 613 | uint32_t i;
|
---|
| 614 | uint32_t now;
|
---|
| 615 |
|
---|
| 616 | aoff64_t block_bitmap;
|
---|
| 617 | aoff64_t inode_bitmap;
|
---|
| 618 | aoff64_t inode_table;
|
---|
| 619 | uint32_t free_blocks;
|
---|
| 620 | uint32_t free_inodes;
|
---|
| 621 | uint32_t used_dirs;
|
---|
| 622 | uint32_t reserved;
|
---|
| 623 | uint32_t inode_table_blocks;
|
---|
| 624 |
|
---|
| 625 | dcnt = block_group_count;
|
---|
| 626 |
|
---|
| 627 | /* Fill in block descriptors */
|
---|
| 628 | b = block_id;
|
---|
| 629 | bg_index = 0;
|
---|
| 630 | bg_block0 = block_id;
|
---|
| 631 | while (dcnt > 0) {
|
---|
| 632 | rc = block_get(&block, fs->device, b, BLOCK_FLAGS_NOREAD);
|
---|
| 633 | if (rc != EOK)
|
---|
| 634 | return rc;
|
---|
| 635 |
|
---|
| 636 | if (dcnt > descriptors_per_block)
|
---|
| 637 | now = descriptors_per_block;
|
---|
| 638 | else
|
---|
| 639 | now = dcnt;
|
---|
| 640 |
|
---|
| 641 | memset(block->data, 0, block_size);
|
---|
| 642 |
|
---|
| 643 | for (i = 0; i < now; i++) {
|
---|
| 644 | bg = (ext4_block_group_t *) (block->data + i *
|
---|
| 645 | desc_size);
|
---|
| 646 |
|
---|
| 647 | block_bitmap = bg_block0 + dtable_blocks;
|
---|
| 648 | inode_bitmap = block_bitmap + 1;
|
---|
| 649 | inode_table = inode_bitmap + 1;
|
---|
| 650 |
|
---|
| 651 | free_blocks = ext4_superblock_get_blocks_in_group(sb,
|
---|
| 652 | bg_index);
|
---|
| 653 |
|
---|
| 654 | free_inodes =
|
---|
| 655 | ext4_filesystem_bg_get_itable_size(sb, bg_index) *
|
---|
| 656 | ext4_filesystem_inodes_per_block(sb);
|
---|
| 657 | used_dirs = 0;
|
---|
| 658 |
|
---|
| 659 | ext4_block_group_set_block_bitmap(bg, sb, block_bitmap);
|
---|
| 660 | ext4_block_group_set_inode_bitmap(bg, sb, inode_bitmap);
|
---|
| 661 | ext4_block_group_set_inode_table_first_block(bg, sb,
|
---|
| 662 | inode_table);
|
---|
| 663 | ext4_block_group_set_free_blocks_count(bg, sb,
|
---|
| 664 | free_blocks);
|
---|
| 665 | ext4_block_group_set_free_inodes_count(bg, sb,
|
---|
| 666 | free_inodes);
|
---|
| 667 | ext4_block_group_set_used_dirs_count(bg, sb,
|
---|
| 668 | used_dirs);
|
---|
| 669 |
|
---|
| 670 | /// XX Lazy
|
---|
| 671 | ext4_block_group_set_flag(bg,
|
---|
| 672 | EXT4_BLOCK_GROUP_BLOCK_UNINIT);
|
---|
| 673 | ext4_block_group_set_flag(bg,
|
---|
| 674 | EXT4_BLOCK_GROUP_INODE_UNINIT);
|
---|
| 675 |
|
---|
| 676 | bg_index++;
|
---|
| 677 | bg_block0 += ext4_superblock_get_blocks_per_group(sb);
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | block->dirty = true;
|
---|
| 681 |
|
---|
| 682 | rc = block_put(block);
|
---|
| 683 | if (rc != EOK)
|
---|
| 684 | return rc;
|
---|
| 685 |
|
---|
| 686 | ++b;
|
---|
| 687 | dcnt -= now;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 | /* This initializes the bitmaps and inode table */
|
---|
| 691 | for (bg_index = 0; bg_index < block_group_count; bg_index++) {
|
---|
| 692 | rc = ext4_filesystem_get_block_group_ref(fs, bg_index, &bg_ref);
|
---|
| 693 | if (rc != EOK)
|
---|
| 694 | return rc;
|
---|
| 695 |
|
---|
| 696 | /*
|
---|
| 697 | * Adjust number of free blocks
|
---|
| 698 | */
|
---|
| 699 | free_blocks = ext4_superblock_get_blocks_in_group(sb, bg_index);
|
---|
| 700 | reserved = ext4_filesystem_bg_get_backup_blocks(bg_ref);
|
---|
| 701 | inode_table_blocks = ext4_filesystem_bg_get_itable_size(sb,
|
---|
| 702 | bg_ref->index);
|
---|
| 703 | /* One for block bitmap one for inode bitmap */
|
---|
| 704 | free_blocks = free_blocks - reserved - 2 - inode_table_blocks;
|
---|
| 705 |
|
---|
| 706 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
| 707 | sb, free_blocks);
|
---|
| 708 | bg_ref->dirty = true;
|
---|
| 709 |
|
---|
| 710 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 711 | if (rc != EOK)
|
---|
| 712 | return rc;
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 | return EOK;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
[4cdac68] | 718 | /** Initialize block bitmap in block group.
|
---|
[38542dc] | 719 | *
|
---|
| 720 | * @param bg_ref Reference to block group
|
---|
| 721 | *
|
---|
| 722 | * @return Error code
|
---|
| 723 | *
|
---|
[4cdac68] | 724 | */
|
---|
[b7fd2a0] | 725 | static errno_t ext4_filesystem_init_block_bitmap(ext4_block_group_ref_t *bg_ref)
|
---|
[4cdac68] | 726 | {
|
---|
[d76973c] | 727 | uint64_t itb;
|
---|
| 728 | uint32_t sz;
|
---|
| 729 | uint32_t i;
|
---|
| 730 |
|
---|
[4cdac68] | 731 | /* Load bitmap */
|
---|
[d76973c] | 732 | ext4_superblock_t *sb = bg_ref->fs->superblock;
|
---|
| 733 | uint64_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
| 734 | bg_ref->block_group, bg_ref->fs->superblock);
|
---|
| 735 | uint64_t bitmap_inode_addr = ext4_block_group_get_inode_bitmap(
|
---|
[38542dc] | 736 | bg_ref->block_group, bg_ref->fs->superblock);
|
---|
[aab85d90] | 737 | uint32_t blocks_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
| 738 | uint32_t bg_blocks = ext4_superblock_get_blocks_in_group(sb,
|
---|
| 739 | bg_ref->index);
|
---|
[a35b458] | 740 |
|
---|
[4cdac68] | 741 | block_t *bitmap_block;
|
---|
[b7fd2a0] | 742 | errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
|
---|
[38542dc] | 743 | bitmap_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
| 744 | if (rc != EOK)
|
---|
[4cdac68] | 745 | return rc;
|
---|
[a35b458] | 746 |
|
---|
[4cdac68] | 747 | uint8_t *bitmap = bitmap_block->data;
|
---|
[a35b458] | 748 |
|
---|
[4cdac68] | 749 | /* Initialize all bitmap bits to zero */
|
---|
[d76973c] | 750 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[4cdac68] | 751 | memset(bitmap, 0, block_size);
|
---|
[a35b458] | 752 |
|
---|
[d76973c] | 753 | /* Determine the number of reserved blocks in the group */
|
---|
| 754 | uint32_t reserved_cnt = ext4_filesystem_bg_get_backup_blocks(bg_ref);
|
---|
| 755 |
|
---|
[4cdac68] | 756 | /* Set bits from to first block to first data block - 1 to one (allocated) */
|
---|
[d76973c] | 757 | for (uint32_t block = 0; block < reserved_cnt; ++block)
|
---|
[4cdac68] | 758 | ext4_bitmap_set_bit(bitmap, block);
|
---|
[d76973c] | 759 |
|
---|
| 760 | uint32_t bitmap_block_gid = ext4_filesystem_blockaddr2group(sb,
|
---|
| 761 | bitmap_block_addr);
|
---|
| 762 | if (bitmap_block_gid == bg_ref->index) {
|
---|
| 763 | ext4_bitmap_set_bit(bitmap,
|
---|
| 764 | ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_block_addr));
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | uint32_t bitmap_inode_gid = ext4_filesystem_blockaddr2group(sb,
|
---|
| 768 | bitmap_inode_addr);
|
---|
| 769 | if (bitmap_inode_gid == bg_ref->index) {
|
---|
| 770 | ext4_bitmap_set_bit(bitmap,
|
---|
| 771 | ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_inode_addr));
|
---|
| 772 | }
|
---|
| 773 |
|
---|
| 774 | itb = ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
|
---|
| 775 | sb);
|
---|
[aab85d90] | 776 | sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref->index);
|
---|
[d76973c] | 777 |
|
---|
| 778 | for (i = 0; i < sz; ++i, ++itb) {
|
---|
| 779 | uint32_t gid = ext4_filesystem_blockaddr2group(sb, itb);
|
---|
| 780 | if (gid == bg_ref->index) {
|
---|
| 781 | ext4_bitmap_set_bit(bitmap,
|
---|
| 782 | ext4_filesystem_blockaddr2_index_in_group(sb, itb));
|
---|
| 783 | }
|
---|
| 784 | }
|
---|
| 785 |
|
---|
[aab85d90] | 786 | /* For last group need to mark blocks which are outside of the FS */
|
---|
| 787 | for (uint32_t block = bg_blocks; block < blocks_group; block++) {
|
---|
| 788 | ext4_bitmap_set_bit(bitmap, block);
|
---|
| 789 | }
|
---|
| 790 |
|
---|
[4cdac68] | 791 | bitmap_block->dirty = true;
|
---|
[a35b458] | 792 |
|
---|
[4cdac68] | 793 | /* Save bitmap */
|
---|
[38542dc] | 794 | return block_put(bitmap_block);
|
---|
[4cdac68] | 795 | }
|
---|
| 796 |
|
---|
| 797 | /** Initialize i-node bitmap in block group.
|
---|
[38542dc] | 798 | *
|
---|
| 799 | * @param bg_ref Reference to block group
|
---|
| 800 | *
|
---|
| 801 | * @return Error code
|
---|
| 802 | *
|
---|
[4cdac68] | 803 | */
|
---|
[b7fd2a0] | 804 | static errno_t ext4_filesystem_init_inode_bitmap(ext4_block_group_ref_t *bg_ref)
|
---|
[4cdac68] | 805 | {
|
---|
| 806 | /* Load bitmap */
|
---|
| 807 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
[38542dc] | 808 | bg_ref->block_group, bg_ref->fs->superblock);
|
---|
[4cdac68] | 809 | block_t *bitmap_block;
|
---|
[a35b458] | 810 |
|
---|
[b7fd2a0] | 811 | errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
|
---|
[38542dc] | 812 | bitmap_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
| 813 | if (rc != EOK)
|
---|
[4cdac68] | 814 | return rc;
|
---|
[a35b458] | 815 |
|
---|
[4cdac68] | 816 | uint8_t *bitmap = bitmap_block->data;
|
---|
[a35b458] | 817 |
|
---|
[4cdac68] | 818 | /* Initialize all bitmap bits to zero */
|
---|
| 819 | uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
|
---|
| 820 | uint32_t inodes_per_group =
|
---|
[38542dc] | 821 | ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
|
---|
[4cdac68] | 822 | memset(bitmap, 0, (inodes_per_group + 7) / 8);
|
---|
[a35b458] | 823 |
|
---|
[4cdac68] | 824 | uint32_t start_bit = inodes_per_group;
|
---|
| 825 | uint32_t end_bit = block_size * 8;
|
---|
[a35b458] | 826 |
|
---|
[4cdac68] | 827 | uint32_t i;
|
---|
[38542dc] | 828 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
|
---|
[4cdac68] | 829 | ext4_bitmap_set_bit(bitmap, i);
|
---|
[a35b458] | 830 |
|
---|
[38542dc] | 831 | if (i < end_bit)
|
---|
[4cdac68] | 832 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
|
---|
[a35b458] | 833 |
|
---|
[4cdac68] | 834 | bitmap_block->dirty = true;
|
---|
[a35b458] | 835 |
|
---|
[4cdac68] | 836 | /* Save bitmap */
|
---|
[38542dc] | 837 | return block_put(bitmap_block);
|
---|
[4cdac68] | 838 | }
|
---|
| 839 |
|
---|
| 840 | /** Initialize i-node table in block group.
|
---|
[38542dc] | 841 | *
|
---|
| 842 | * @param bg_ref Reference to block group
|
---|
| 843 | *
|
---|
| 844 | * @return Error code
|
---|
| 845 | *
|
---|
[fe61181] | 846 | */
|
---|
[b7fd2a0] | 847 | static errno_t ext4_filesystem_init_inode_table(ext4_block_group_ref_t *bg_ref)
|
---|
[4cdac68] | 848 | {
|
---|
| 849 | ext4_superblock_t *sb = bg_ref->fs->superblock;
|
---|
[a35b458] | 850 |
|
---|
[4cdac68] | 851 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[aab85d90] | 852 | uint32_t inodes_per_block = ext4_filesystem_inodes_per_block(sb);
|
---|
[a35b458] | 853 |
|
---|
[4cdac68] | 854 | uint32_t inodes_in_group =
|
---|
[38542dc] | 855 | ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
|
---|
[a35b458] | 856 |
|
---|
[4cdac68] | 857 | uint32_t table_blocks = inodes_in_group / inodes_per_block;
|
---|
[a35b458] | 858 |
|
---|
[38542dc] | 859 | if (inodes_in_group % inodes_per_block)
|
---|
[4cdac68] | 860 | table_blocks++;
|
---|
[a35b458] | 861 |
|
---|
[4cdac68] | 862 | /* Compute initialization bounds */
|
---|
| 863 | uint32_t first_block = ext4_block_group_get_inode_table_first_block(
|
---|
[38542dc] | 864 | bg_ref->block_group, sb);
|
---|
[a35b458] | 865 |
|
---|
[4cdac68] | 866 | uint32_t last_block = first_block + table_blocks - 1;
|
---|
[a35b458] | 867 |
|
---|
[4cdac68] | 868 | /* Initialization of all itable blocks */
|
---|
| 869 | for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
|
---|
| 870 | block_t *block;
|
---|
[b7fd2a0] | 871 | errno_t rc = block_get(&block, bg_ref->fs->device, fblock,
|
---|
[38542dc] | 872 | BLOCK_FLAGS_NOREAD);
|
---|
| 873 | if (rc != EOK)
|
---|
[4cdac68] | 874 | return rc;
|
---|
[a35b458] | 875 |
|
---|
[4cdac68] | 876 | memset(block->data, 0, block_size);
|
---|
| 877 | block->dirty = true;
|
---|
[a35b458] | 878 |
|
---|
[4cdac68] | 879 | rc = block_put(block);
|
---|
[38542dc] | 880 | if (rc != EOK)
|
---|
[4cdac68] | 881 | return rc;
|
---|
| 882 | }
|
---|
[a35b458] | 883 |
|
---|
[4cdac68] | 884 | return EOK;
|
---|
| 885 | }
|
---|
| 886 |
|
---|
[5b26747] | 887 | /** Get reference to block group specified by index.
|
---|
[9fc72fb3] | 888 | *
|
---|
[38542dc] | 889 | * @param fs Filesystem to find block group on
|
---|
| 890 | * @param bgid Index of block group to load
|
---|
| 891 | * @param ref Output pointer for reference
|
---|
| 892 | *
|
---|
| 893 | * @return Error code
|
---|
| 894 | *
|
---|
[9fc72fb3] | 895 | */
|
---|
[b7fd2a0] | 896 | errno_t ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
---|
[3711e7e] | 897 | ext4_block_group_ref_t **ref)
|
---|
[6c501f8] | 898 | {
|
---|
[06d85e5] | 899 | /* Allocate memory for new structure */
|
---|
[38542dc] | 900 | ext4_block_group_ref_t *newref =
|
---|
| 901 | malloc(sizeof(ext4_block_group_ref_t));
|
---|
| 902 | if (newref == NULL)
|
---|
[3711e7e] | 903 | return ENOMEM;
|
---|
[a35b458] | 904 |
|
---|
[06d85e5] | 905 | /* Compute number of descriptors, that fits in one data block */
|
---|
[38542dc] | 906 | uint32_t descriptors_per_block =
|
---|
| 907 | ext4_superblock_get_block_size(fs->superblock) /
|
---|
| 908 | ext4_superblock_get_desc_size(fs->superblock);
|
---|
[a35b458] | 909 |
|
---|
[06d85e5] | 910 | /* Block group descriptor table starts at the next block after superblock */
|
---|
[38542dc] | 911 | aoff64_t block_id =
|
---|
| 912 | ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
---|
[a35b458] | 913 |
|
---|
[06d85e5] | 914 | /* Find the block containing the descriptor we are looking for */
|
---|
[3711e7e] | 915 | block_id += bgid / descriptors_per_block;
|
---|
[38542dc] | 916 | uint32_t offset = (bgid % descriptors_per_block) *
|
---|
| 917 | ext4_superblock_get_desc_size(fs->superblock);
|
---|
[a35b458] | 918 |
|
---|
[06d85e5] | 919 | /* Load block with descriptors */
|
---|
[b7fd2a0] | 920 | errno_t rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
[3711e7e] | 921 | if (rc != EOK) {
|
---|
| 922 | free(newref);
|
---|
| 923 | return rc;
|
---|
| 924 | }
|
---|
[a35b458] | 925 |
|
---|
[7f29575] | 926 | /* Initialize in-memory representation */
|
---|
[3711e7e] | 927 | newref->block_group = newref->block->data + offset;
|
---|
[1ac1ab4] | 928 | newref->fs = fs;
|
---|
| 929 | newref->index = bgid;
|
---|
[12b4a7f] | 930 | newref->dirty = false;
|
---|
[a35b458] | 931 |
|
---|
[3711e7e] | 932 | *ref = newref;
|
---|
[a35b458] | 933 |
|
---|
[4cdac68] | 934 | if (ext4_block_group_has_flag(newref->block_group,
|
---|
[38542dc] | 935 | EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
|
---|
[4cdac68] | 936 | rc = ext4_filesystem_init_block_bitmap(newref);
|
---|
| 937 | if (rc != EOK) {
|
---|
| 938 | block_put(newref->block);
|
---|
| 939 | free(newref);
|
---|
| 940 | return rc;
|
---|
| 941 | }
|
---|
[a35b458] | 942 |
|
---|
[4cdac68] | 943 | ext4_block_group_clear_flag(newref->block_group,
|
---|
[38542dc] | 944 | EXT4_BLOCK_GROUP_BLOCK_UNINIT);
|
---|
[a35b458] | 945 |
|
---|
[4cdac68] | 946 | newref->dirty = true;
|
---|
| 947 | }
|
---|
[a35b458] | 948 |
|
---|
[4cdac68] | 949 | if (ext4_block_group_has_flag(newref->block_group,
|
---|
[38542dc] | 950 | EXT4_BLOCK_GROUP_INODE_UNINIT)) {
|
---|
[4cdac68] | 951 | rc = ext4_filesystem_init_inode_bitmap(newref);
|
---|
| 952 | if (rc != EOK) {
|
---|
| 953 | block_put(newref->block);
|
---|
| 954 | free(newref);
|
---|
| 955 | return rc;
|
---|
| 956 | }
|
---|
[a35b458] | 957 |
|
---|
[4cdac68] | 958 | ext4_block_group_clear_flag(newref->block_group,
|
---|
[38542dc] | 959 | EXT4_BLOCK_GROUP_INODE_UNINIT);
|
---|
[a35b458] | 960 |
|
---|
[38542dc] | 961 | if (!ext4_block_group_has_flag(newref->block_group,
|
---|
| 962 | EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
|
---|
[4cdac68] | 963 | rc = ext4_filesystem_init_inode_table(newref);
|
---|
[aab85d90] | 964 | if (rc != EOK) {
|
---|
| 965 | block_put(newref->block);
|
---|
| 966 | free(newref);
|
---|
[4cdac68] | 967 | return rc;
|
---|
[aab85d90] | 968 | }
|
---|
[a35b458] | 969 |
|
---|
[4cdac68] | 970 | ext4_block_group_set_flag(newref->block_group,
|
---|
[38542dc] | 971 | EXT4_BLOCK_GROUP_ITABLE_ZEROED);
|
---|
[4cdac68] | 972 | }
|
---|
[a35b458] | 973 |
|
---|
[4cdac68] | 974 | newref->dirty = true;
|
---|
| 975 | }
|
---|
[a35b458] | 976 |
|
---|
[3711e7e] | 977 | return EOK;
|
---|
[6c501f8] | 978 | }
|
---|
| 979 |
|
---|
[81a7858] | 980 | /** Compute checksum of block group descriptor.
|
---|
[9fc72fb3] | 981 | *
|
---|
[38542dc] | 982 | * @param sb Superblock
|
---|
| 983 | * @param bgid Index of block group in the filesystem
|
---|
| 984 | * @param bg Block group to compute checksum for
|
---|
| 985 | *
|
---|
| 986 | * @return Checksum value
|
---|
| 987 | *
|
---|
[9fc72fb3] | 988 | */
|
---|
[1ac1ab4] | 989 | static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
|
---|
[38542dc] | 990 | ext4_block_group_t *bg)
|
---|
[1ac1ab4] | 991 | {
|
---|
[06d85e5] | 992 | /* If checksum not supported, 0 will be returned */
|
---|
[1ac1ab4] | 993 | uint16_t crc = 0;
|
---|
[2b5d966] | 994 |
|
---|
[06d85e5] | 995 | /* Compute the checksum only if the filesystem supports it */
|
---|
[38542dc] | 996 | if (ext4_superblock_has_feature_read_only(sb,
|
---|
| 997 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
|
---|
[1ac1ab4] | 998 | void *base = bg;
|
---|
| 999 | void *checksum = &bg->checksum;
|
---|
[a35b458] | 1000 |
|
---|
[38542dc] | 1001 | uint32_t offset = (uint32_t) (checksum - base);
|
---|
[a35b458] | 1002 |
|
---|
[06d85e5] | 1003 | /* Convert block group index to little endian */
|
---|
[1ac1ab4] | 1004 | uint32_t le_group = host2uint32_t_le(bgid);
|
---|
[a35b458] | 1005 |
|
---|
[06d85e5] | 1006 | /* Initialization */
|
---|
[2b5d966] | 1007 | crc = crc16_ibm(~0, sb->uuid, sizeof(sb->uuid));
|
---|
[a35b458] | 1008 |
|
---|
[06d85e5] | 1009 | /* Include index of block group */
|
---|
[2b5d966] | 1010 | crc = crc16_ibm(crc, (uint8_t *) &le_group, sizeof(le_group));
|
---|
[a35b458] | 1011 |
|
---|
[06d85e5] | 1012 | /* Compute crc from the first part (stop before checksum field) */
|
---|
[2b5d966] | 1013 | crc = crc16_ibm(crc, (uint8_t *) bg, offset);
|
---|
[a35b458] | 1014 |
|
---|
[06d85e5] | 1015 | /* Skip checksum */
|
---|
[81a7858] | 1016 | offset += sizeof(bg->checksum);
|
---|
[a35b458] | 1017 |
|
---|
[06d85e5] | 1018 | /* Checksum of the rest of block group descriptor */
|
---|
[38542dc] | 1019 | if ((ext4_superblock_has_feature_incompatible(sb,
|
---|
| 1020 | EXT4_FEATURE_INCOMPAT_64BIT)) &&
|
---|
| 1021 | (offset < ext4_superblock_get_desc_size(sb)))
|
---|
[2b5d966] | 1022 | crc = crc16_ibm(crc, ((uint8_t *) bg) + offset,
|
---|
[38542dc] | 1023 | ext4_superblock_get_desc_size(sb) - offset);
|
---|
[1ac1ab4] | 1024 | }
|
---|
[a35b458] | 1025 |
|
---|
[1ac1ab4] | 1026 | return crc;
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
[d76973c] | 1029 | /** Get the size of the block group's inode table
|
---|
| 1030 | *
|
---|
[aab85d90] | 1031 | * @param sb Pointer to the superblock
|
---|
| 1032 | * @param bg_index Block group index
|
---|
[d76973c] | 1033 | *
|
---|
[aab85d90] | 1034 | * @return Size of the inode table in blocks.
|
---|
[d76973c] | 1035 | */
|
---|
| 1036 | uint32_t ext4_filesystem_bg_get_itable_size(ext4_superblock_t *sb,
|
---|
[aab85d90] | 1037 | uint32_t bg_index)
|
---|
[d76973c] | 1038 | {
|
---|
| 1039 | uint32_t itable_size;
|
---|
| 1040 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
| 1041 | uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
|
---|
| 1042 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
| 1043 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 1044 |
|
---|
[aab85d90] | 1045 | if (bg_index < block_group_count - 1) {
|
---|
[d76973c] | 1046 | itable_size = inodes_per_group * inode_table_item_size;
|
---|
| 1047 | } else {
|
---|
| 1048 | /* Last block group could be smaller */
|
---|
| 1049 | uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
|
---|
| 1050 | itable_size =
|
---|
| 1051 | (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
|
---|
| 1052 | inode_table_item_size;
|
---|
| 1053 | }
|
---|
| 1054 |
|
---|
| 1055 | return ROUND_UP(itable_size, block_size) / block_size;
|
---|
| 1056 | }
|
---|
| 1057 |
|
---|
[447201e] | 1058 | /** Get the number of blocks used by superblock + gdt + reserved gdt backups
|
---|
| 1059 | *
|
---|
| 1060 | * @param bg Pointer to block group
|
---|
| 1061 | *
|
---|
| 1062 | * @return Number of blocks
|
---|
| 1063 | */
|
---|
[d76973c] | 1064 | uint32_t ext4_filesystem_bg_get_backup_blocks(ext4_block_group_ref_t *bg)
|
---|
[447201e] | 1065 | {
|
---|
[aab85d90] | 1066 | return ext4_superblock_get_group_backup_blocks(bg->fs->superblock,
|
---|
| 1067 | bg->index);
|
---|
[447201e] | 1068 | }
|
---|
| 1069 |
|
---|
[5b26747] | 1070 | /** Put reference to block group.
|
---|
[9fc72fb3] | 1071 | *
|
---|
[c1fd281] | 1072 | * @param ref Pointer for reference to be put back
|
---|
[38542dc] | 1073 | *
|
---|
| 1074 | * @return Error code
|
---|
| 1075 | *
|
---|
[9fc72fb3] | 1076 | */
|
---|
[b7fd2a0] | 1077 | errno_t ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
---|
[829d238] | 1078 | {
|
---|
[06d85e5] | 1079 | /* Check if reference modified */
|
---|
[12b4a7f] | 1080 | if (ref->dirty) {
|
---|
[06d85e5] | 1081 | /* Compute new checksum of block group */
|
---|
[38542dc] | 1082 | uint16_t checksum =
|
---|
| 1083 | ext4_filesystem_bg_checksum(ref->fs->superblock, ref->index,
|
---|
| 1084 | ref->block_group);
|
---|
[5b0a3946] | 1085 | ext4_block_group_set_checksum(ref->block_group, checksum);
|
---|
[a35b458] | 1086 |
|
---|
[06d85e5] | 1087 | /* Mark block dirty for writing changes to physical device */
|
---|
[12b4a7f] | 1088 | ref->block->dirty = true;
|
---|
| 1089 | }
|
---|
[a35b458] | 1090 |
|
---|
[06d85e5] | 1091 | /* Put back block, that contains block group descriptor */
|
---|
[b7fd2a0] | 1092 | errno_t rc = block_put(ref->block);
|
---|
[829d238] | 1093 | free(ref);
|
---|
[a35b458] | 1094 |
|
---|
[829d238] | 1095 | return rc;
|
---|
| 1096 | }
|
---|
| 1097 |
|
---|
[5b26747] | 1098 | /** Get reference to i-node specified by index.
|
---|
[9fc72fb3] | 1099 | *
|
---|
[38542dc] | 1100 | * @param fs Filesystem to find i-node on
|
---|
| 1101 | * @param index Index of i-node to load
|
---|
| 1102 | * @oaram ref Output pointer for reference
|
---|
| 1103 | *
|
---|
| 1104 | * @return Error code
|
---|
| 1105 | *
|
---|
[9fc72fb3] | 1106 | */
|
---|
[b7fd2a0] | 1107 | errno_t ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
---|
[3711e7e] | 1108 | ext4_inode_ref_t **ref)
|
---|
| 1109 | {
|
---|
[06d85e5] | 1110 | /* Allocate memory for new structure */
|
---|
[38542dc] | 1111 | ext4_inode_ref_t *newref =
|
---|
| 1112 | malloc(sizeof(ext4_inode_ref_t));
|
---|
| 1113 | if (newref == NULL)
|
---|
[3711e7e] | 1114 | return ENOMEM;
|
---|
[a35b458] | 1115 |
|
---|
[06d85e5] | 1116 | /* Compute number of i-nodes, that fits in one data block */
|
---|
[d9bbe45] | 1117 | uint32_t inodes_per_group =
|
---|
[38542dc] | 1118 | ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
[a35b458] | 1119 |
|
---|
[38542dc] | 1120 | /*
|
---|
| 1121 | * Inode numbers are 1-based, but it is simpler to work with 0-based
|
---|
[3711e7e] | 1122 | * when computing indices
|
---|
| 1123 | */
|
---|
| 1124 | index -= 1;
|
---|
[d9bbe45] | 1125 | uint32_t block_group = index / inodes_per_group;
|
---|
| 1126 | uint32_t offset_in_group = index % inodes_per_group;
|
---|
[a35b458] | 1127 |
|
---|
[06d85e5] | 1128 | /* Load block group, where i-node is located */
|
---|
[d9bbe45] | 1129 | ext4_block_group_ref_t *bg_ref;
|
---|
[b7fd2a0] | 1130 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
[3711e7e] | 1131 | if (rc != EOK) {
|
---|
| 1132 | free(newref);
|
---|
| 1133 | return rc;
|
---|
| 1134 | }
|
---|
[a35b458] | 1135 |
|
---|
[06d85e5] | 1136 | /* Load block address, where i-node table is located */
|
---|
[38542dc] | 1137 | uint32_t inode_table_start =
|
---|
| 1138 | ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
|
---|
| 1139 | fs->superblock);
|
---|
[a35b458] | 1140 |
|
---|
[06d85e5] | 1141 | /* Put back block group reference (not needed more) */
|
---|
[829d238] | 1142 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 1143 | if (rc != EOK) {
|
---|
| 1144 | free(newref);
|
---|
| 1145 | return rc;
|
---|
| 1146 | }
|
---|
[a35b458] | 1147 |
|
---|
[06d85e5] | 1148 | /* Compute position of i-node in the block group */
|
---|
[d9bbe45] | 1149 | uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
---|
| 1150 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 1151 | uint32_t byte_offset_in_group = offset_in_group * inode_size;
|
---|
[a35b458] | 1152 |
|
---|
[06d85e5] | 1153 | /* Compute block address */
|
---|
[d9bbe45] | 1154 | aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
|
---|
[3711e7e] | 1155 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
| 1156 | if (rc != EOK) {
|
---|
| 1157 | free(newref);
|
---|
| 1158 | return rc;
|
---|
| 1159 | }
|
---|
[a35b458] | 1160 |
|
---|
[06d85e5] | 1161 | /* Compute position of i-node in the data block */
|
---|
[d9bbe45] | 1162 | uint32_t offset_in_block = byte_offset_in_group % block_size;
|
---|
[3711e7e] | 1163 | newref->inode = newref->block->data + offset_in_block;
|
---|
[a35b458] | 1164 |
|
---|
[06d85e5] | 1165 | /* We need to store the original value of index in the reference */
|
---|
[1ac1ab4] | 1166 | newref->index = index + 1;
|
---|
| 1167 | newref->fs = fs;
|
---|
[052e82d] | 1168 | newref->dirty = false;
|
---|
[a35b458] | 1169 |
|
---|
[3711e7e] | 1170 | *ref = newref;
|
---|
[a35b458] | 1171 |
|
---|
[9b9d37bb] | 1172 | return EOK;
|
---|
| 1173 | }
|
---|
| 1174 |
|
---|
[81a7858] | 1175 | /** Put reference to i-node.
|
---|
[9fc72fb3] | 1176 | *
|
---|
[38542dc] | 1177 | * @param ref Pointer for reference to be put back
|
---|
| 1178 | *
|
---|
| 1179 | * @return Error code
|
---|
| 1180 | *
|
---|
[9fc72fb3] | 1181 | */
|
---|
[b7fd2a0] | 1182 | errno_t ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
---|
[9b9d37bb] | 1183 | {
|
---|
[06d85e5] | 1184 | /* Check if reference modified */
|
---|
[052e82d] | 1185 | if (ref->dirty) {
|
---|
[06d85e5] | 1186 | /* Mark block dirty for writing changes to physical device */
|
---|
[052e82d] | 1187 | ref->block->dirty = true;
|
---|
| 1188 | }
|
---|
[a35b458] | 1189 |
|
---|
[06d85e5] | 1190 | /* Put back block, that contains i-node */
|
---|
[b7fd2a0] | 1191 | errno_t rc = block_put(ref->block);
|
---|
[9b9d37bb] | 1192 | free(ref);
|
---|
[a35b458] | 1193 |
|
---|
[9b9d37bb] | 1194 | return rc;
|
---|
| 1195 | }
|
---|
| 1196 |
|
---|
[aab85d90] | 1197 | /** Initialize newly allocated i-node in the filesystem.
|
---|
[9fc72fb3] | 1198 | *
|
---|
[aab85d90] | 1199 | * @param fs Filesystem to initialize i-node on
|
---|
| 1200 | * @param index I-node index
|
---|
[38542dc] | 1201 | * @param inode_ref Output pointer to return reference to allocated i-node
|
---|
| 1202 | * @param flags Flags to be set for newly created i-node
|
---|
| 1203 | *
|
---|
| 1204 | * @return Error code
|
---|
| 1205 | *
|
---|
[9fc72fb3] | 1206 | */
|
---|
[aab85d90] | 1207 | static errno_t ext4_filesystem_init_inode(ext4_filesystem_t *fs, uint32_t index,
|
---|
[38542dc] | 1208 | ext4_inode_ref_t **inode_ref, int flags)
|
---|
[2d2c6ce] | 1209 | {
|
---|
[06d85e5] | 1210 | /* Check if newly allocated i-node will be a directory */
|
---|
[304faab] | 1211 | bool is_dir = false;
|
---|
[38542dc] | 1212 | if (flags & L_DIRECTORY)
|
---|
[304faab] | 1213 | is_dir = true;
|
---|
[a35b458] | 1214 |
|
---|
[06d85e5] | 1215 | /* Load i-node from on-disk i-node table */
|
---|
[aab85d90] | 1216 | errno_t rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
|
---|
[304faab] | 1217 | if (rc != EOK) {
|
---|
| 1218 | ext4_ialloc_free_inode(fs, index, is_dir);
|
---|
| 1219 | return rc;
|
---|
| 1220 | }
|
---|
[a35b458] | 1221 |
|
---|
[06d85e5] | 1222 | /* Initialize i-node */
|
---|
[304faab] | 1223 | ext4_inode_t *inode = (*inode_ref)->inode;
|
---|
[a35b458] | 1224 |
|
---|
[8847142] | 1225 | uint16_t mode;
|
---|
[304faab] | 1226 | if (is_dir) {
|
---|
[8847142] | 1227 | /*
|
---|
| 1228 | * Default directory permissions to be compatible with other systems
|
---|
| 1229 | * 0777 (octal) == rwxrwxrwx
|
---|
| 1230 | */
|
---|
[a35b458] | 1231 |
|
---|
[8847142] | 1232 | mode = 0777;
|
---|
| 1233 | mode |= EXT4_INODE_MODE_DIRECTORY;
|
---|
| 1234 | ext4_inode_set_mode(fs->superblock, inode, mode);
|
---|
[38542dc] | 1235 | ext4_inode_set_links_count(inode, 1); /* '.' entry */
|
---|
[2d2c6ce] | 1236 | } else {
|
---|
[8847142] | 1237 | /*
|
---|
| 1238 | * Default file permissions to be compatible with other systems
|
---|
| 1239 | * 0666 (octal) == rw-rw-rw-
|
---|
| 1240 | */
|
---|
[a35b458] | 1241 |
|
---|
[8847142] | 1242 | mode = 0666;
|
---|
| 1243 | mode |= EXT4_INODE_MODE_FILE;
|
---|
| 1244 | ext4_inode_set_mode(fs->superblock, inode, mode);
|
---|
[2d2c6ce] | 1245 | ext4_inode_set_links_count(inode, 0);
|
---|
| 1246 | }
|
---|
[a35b458] | 1247 |
|
---|
[2d2c6ce] | 1248 | ext4_inode_set_uid(inode, 0);
|
---|
| 1249 | ext4_inode_set_gid(inode, 0);
|
---|
| 1250 | ext4_inode_set_size(inode, 0);
|
---|
| 1251 | ext4_inode_set_access_time(inode, 0);
|
---|
| 1252 | ext4_inode_set_change_inode_time(inode, 0);
|
---|
| 1253 | ext4_inode_set_modification_time(inode, 0);
|
---|
| 1254 | ext4_inode_set_deletion_time(inode, 0);
|
---|
| 1255 | ext4_inode_set_blocks_count(fs->superblock, inode, 0);
|
---|
| 1256 | ext4_inode_set_flags(inode, 0);
|
---|
| 1257 | ext4_inode_set_generation(inode, 0);
|
---|
[a35b458] | 1258 |
|
---|
[06d85e5] | 1259 | /* Reset blocks array */
|
---|
[38542dc] | 1260 | for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
|
---|
[2d2c6ce] | 1261 | inode->blocks[i] = 0;
|
---|
[a35b458] | 1262 |
|
---|
[06d85e5] | 1263 | /* Initialize extents if needed */
|
---|
[936132f] | 1264 | if (ext4_superblock_has_feature_incompatible(
|
---|
[38542dc] | 1265 | fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
|
---|
[936132f] | 1266 | ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
|
---|
[a35b458] | 1267 |
|
---|
[06d85e5] | 1268 | /* Initialize extent root header */
|
---|
[936132f] | 1269 | ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
|
---|
| 1270 | ext4_extent_header_set_depth(header, 0);
|
---|
| 1271 | ext4_extent_header_set_entries_count(header, 0);
|
---|
| 1272 | ext4_extent_header_set_generation(header, 0);
|
---|
| 1273 | ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
|
---|
[a35b458] | 1274 |
|
---|
[38542dc] | 1275 | uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
|
---|
| 1276 | sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
|
---|
[a35b458] | 1277 |
|
---|
[936132f] | 1278 | ext4_extent_header_set_max_entries_count(header, max_entries);
|
---|
| 1279 | }
|
---|
[a35b458] | 1280 |
|
---|
[304faab] | 1281 | (*inode_ref)->dirty = true;
|
---|
[a35b458] | 1282 |
|
---|
[2d2c6ce] | 1283 | return EOK;
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
[aab85d90] | 1286 | /** Allocate new i-node in the filesystem.
|
---|
| 1287 | *
|
---|
| 1288 | * @param fs Filesystem to allocated i-node on
|
---|
| 1289 | * @param inode_ref Output pointer to return reference to allocated i-node
|
---|
| 1290 | * @param flags Flags to be set for newly created i-node
|
---|
| 1291 | *
|
---|
| 1292 | * @return Error code
|
---|
| 1293 | *
|
---|
| 1294 | */
|
---|
| 1295 | errno_t ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
|
---|
| 1296 | ext4_inode_ref_t **inode_ref, int flags)
|
---|
| 1297 | {
|
---|
| 1298 | /* Check if newly allocated i-node will be a directory */
|
---|
| 1299 | bool is_dir = false;
|
---|
| 1300 | if (flags & L_DIRECTORY)
|
---|
| 1301 | is_dir = true;
|
---|
| 1302 |
|
---|
| 1303 | /* Allocate inode by allocation algorithm */
|
---|
| 1304 | uint32_t index;
|
---|
| 1305 | errno_t rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
|
---|
| 1306 | if (rc != EOK)
|
---|
| 1307 | return rc;
|
---|
| 1308 |
|
---|
| 1309 | rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
|
---|
| 1310 | if (rc != EOK) {
|
---|
| 1311 | ext4_ialloc_free_inode(fs, index, is_dir);
|
---|
| 1312 | return rc;
|
---|
| 1313 | }
|
---|
| 1314 |
|
---|
| 1315 | return EOK;
|
---|
| 1316 | }
|
---|
| 1317 |
|
---|
| 1318 | /** Allocate specific i-node in the filesystem.
|
---|
| 1319 | *
|
---|
| 1320 | * @param fs Filesystem to allocated i-node on
|
---|
| 1321 | * @param index Index of i-node to allocate
|
---|
| 1322 | * @param inode_ref Output pointer to return reference to allocated i-node
|
---|
| 1323 | * @param flags Flags to be set for newly created i-node
|
---|
| 1324 | *
|
---|
| 1325 | * @return Error code
|
---|
| 1326 | *
|
---|
| 1327 | */
|
---|
| 1328 | static errno_t ext4_filesystem_alloc_this_inode(ext4_filesystem_t *fs,
|
---|
| 1329 | uint32_t index, ext4_inode_ref_t **inode_ref, int flags)
|
---|
| 1330 | {
|
---|
| 1331 | /* Check if newly allocated i-node will be a directory */
|
---|
| 1332 | bool is_dir = false;
|
---|
| 1333 | if (flags & L_DIRECTORY)
|
---|
| 1334 | is_dir = true;
|
---|
| 1335 |
|
---|
| 1336 | /* Allocate inode by allocation algorithm */
|
---|
| 1337 | errno_t rc = ext4_ialloc_alloc_this_inode(fs, index, is_dir);
|
---|
| 1338 | if (rc != EOK)
|
---|
| 1339 | return rc;
|
---|
| 1340 |
|
---|
| 1341 | rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
|
---|
| 1342 | if (rc != EOK) {
|
---|
| 1343 | ext4_ialloc_free_inode(fs, index, is_dir);
|
---|
| 1344 | return rc;
|
---|
| 1345 | }
|
---|
| 1346 |
|
---|
| 1347 | return EOK;
|
---|
| 1348 | }
|
---|
| 1349 |
|
---|
[81a7858] | 1350 | /** Release i-node and mark it as free.
|
---|
[9fc72fb3] | 1351 | *
|
---|
[38542dc] | 1352 | * @param inode_ref I-node to be released
|
---|
| 1353 | *
|
---|
| 1354 | * @return Error code
|
---|
| 1355 | *
|
---|
[9fc72fb3] | 1356 | */
|
---|
[b7fd2a0] | 1357 | errno_t ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
|
---|
[3d4fd2c] | 1358 | {
|
---|
[3e2952b] | 1359 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[a35b458] | 1360 |
|
---|
[06d85e5] | 1361 | /* For extents must be data block destroyed by other way */
|
---|
[38542dc] | 1362 | if ((ext4_superblock_has_feature_incompatible(fs->superblock,
|
---|
| 1363 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
---|
| 1364 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
---|
[06d85e5] | 1365 | /* Data structures are released during truncate operation... */
|
---|
[3e2952b] | 1366 | goto finish;
|
---|
| 1367 | }
|
---|
[a35b458] | 1368 |
|
---|
[06d85e5] | 1369 | /* Release all indirect (no data) blocks */
|
---|
[a35b458] | 1370 |
|
---|
[06d85e5] | 1371 | /* 1) Single indirect */
|
---|
[d9bbe45] | 1372 | uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
|
---|
[3d4fd2c] | 1373 | if (fblock != 0) {
|
---|
[b7fd2a0] | 1374 | errno_t rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[38542dc] | 1375 | if (rc != EOK)
|
---|
[ca3d77a] | 1376 | return rc;
|
---|
[a35b458] | 1377 |
|
---|
[3d4fd2c] | 1378 | ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
|
---|
| 1379 | }
|
---|
[a35b458] | 1380 |
|
---|
[3d4fd2c] | 1381 | block_t *block;
|
---|
| 1382 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
| 1383 | uint32_t count = block_size / sizeof(uint32_t);
|
---|
[a35b458] | 1384 |
|
---|
[06d85e5] | 1385 | /* 2) Double indirect */
|
---|
[3d4fd2c] | 1386 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
|
---|
| 1387 | if (fblock != 0) {
|
---|
[b7fd2a0] | 1388 | errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
[38542dc] | 1389 | if (rc != EOK)
|
---|
[e63ce679] | 1390 | return rc;
|
---|
[a35b458] | 1391 |
|
---|
[3d4fd2c] | 1392 | uint32_t ind_block;
|
---|
| 1393 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
[38542dc] | 1394 | ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
|
---|
[a35b458] | 1395 |
|
---|
[3d4fd2c] | 1396 | if (ind_block != 0) {
|
---|
[1ac1ab4] | 1397 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
---|
[3d4fd2c] | 1398 | if (rc != EOK) {
|
---|
[e63ce679] | 1399 | block_put(block);
|
---|
| 1400 | return rc;
|
---|
[3d4fd2c] | 1401 | }
|
---|
| 1402 | }
|
---|
| 1403 | }
|
---|
[a35b458] | 1404 |
|
---|
[532f53d] | 1405 | rc = block_put(block);
|
---|
| 1406 | if (rc != EOK)
|
---|
| 1407 | return rc;
|
---|
| 1408 |
|
---|
[1ac1ab4] | 1409 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[38542dc] | 1410 | if (rc != EOK)
|
---|
[e63ce679] | 1411 | return rc;
|
---|
[a35b458] | 1412 |
|
---|
[3d4fd2c] | 1413 | ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
|
---|
| 1414 | }
|
---|
[a35b458] | 1415 |
|
---|
[06d85e5] | 1416 | /* 3) Tripple indirect */
|
---|
[3d4fd2c] | 1417 | block_t *subblock;
|
---|
| 1418 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
|
---|
| 1419 | if (fblock != 0) {
|
---|
[b7fd2a0] | 1420 | errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
[38542dc] | 1421 | if (rc != EOK)
|
---|
[e63ce679] | 1422 | return rc;
|
---|
[a35b458] | 1423 |
|
---|
[3d4fd2c] | 1424 | uint32_t ind_block;
|
---|
| 1425 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
[38542dc] | 1426 | ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
|
---|
[a35b458] | 1427 |
|
---|
[3d4fd2c] | 1428 | if (ind_block != 0) {
|
---|
[38542dc] | 1429 | rc = block_get(&subblock, fs->device, ind_block,
|
---|
| 1430 | BLOCK_FLAGS_NONE);
|
---|
[3d4fd2c] | 1431 | if (rc != EOK) {
|
---|
[e63ce679] | 1432 | block_put(block);
|
---|
| 1433 | return rc;
|
---|
[3d4fd2c] | 1434 | }
|
---|
[a35b458] | 1435 |
|
---|
[3d4fd2c] | 1436 | uint32_t ind_subblock;
|
---|
[38542dc] | 1437 | for (uint32_t suboffset = 0; suboffset < count;
|
---|
| 1438 | ++suboffset) {
|
---|
| 1439 | ind_subblock = uint32_t_le2host(((uint32_t *)
|
---|
| 1440 | subblock->data)[suboffset]);
|
---|
[a35b458] | 1441 |
|
---|
[3d4fd2c] | 1442 | if (ind_subblock != 0) {
|
---|
[1ac1ab4] | 1443 | rc = ext4_balloc_free_block(inode_ref, ind_subblock);
|
---|
[3d4fd2c] | 1444 | if (rc != EOK) {
|
---|
[e63ce679] | 1445 | block_put(subblock);
|
---|
| 1446 | block_put(block);
|
---|
| 1447 | return rc;
|
---|
[3d4fd2c] | 1448 | }
|
---|
| 1449 | }
|
---|
| 1450 | }
|
---|
[a35b458] | 1451 |
|
---|
[532f53d] | 1452 | rc = block_put(subblock);
|
---|
[f5c03a8] | 1453 | if (rc != EOK) {
|
---|
| 1454 | block_put(block);
|
---|
[532f53d] | 1455 | return rc;
|
---|
[f5c03a8] | 1456 | }
|
---|
[3d4fd2c] | 1457 | }
|
---|
[a35b458] | 1458 |
|
---|
[1ac1ab4] | 1459 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
---|
[3d4fd2c] | 1460 | if (rc != EOK) {
|
---|
[e63ce679] | 1461 | block_put(block);
|
---|
| 1462 | return rc;
|
---|
[3d4fd2c] | 1463 | }
|
---|
| 1464 | }
|
---|
[a35b458] | 1465 |
|
---|
[532f53d] | 1466 | rc = block_put(block);
|
---|
| 1467 | if (rc != EOK)
|
---|
| 1468 | return rc;
|
---|
| 1469 |
|
---|
[1ac1ab4] | 1470 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
[38542dc] | 1471 | if (rc != EOK)
|
---|
[e63ce679] | 1472 | return rc;
|
---|
[a35b458] | 1473 |
|
---|
[3d4fd2c] | 1474 | ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
|
---|
| 1475 | }
|
---|
[a35b458] | 1476 |
|
---|
[3e2952b] | 1477 | finish:
|
---|
[06d85e5] | 1478 | /* Mark inode dirty for writing to the physical device */
|
---|
[e5f8762] | 1479 | inode_ref->dirty = true;
|
---|
[a35b458] | 1480 |
|
---|
[0b293a6] | 1481 | /* Free block with extended attributes if present */
|
---|
| 1482 | uint32_t xattr_block = ext4_inode_get_file_acl(
|
---|
[38542dc] | 1483 | inode_ref->inode, fs->superblock);
|
---|
[0b293a6] | 1484 | if (xattr_block) {
|
---|
[b7fd2a0] | 1485 | errno_t rc = ext4_balloc_free_block(inode_ref, xattr_block);
|
---|
[38542dc] | 1486 | if (rc != EOK)
|
---|
[0b293a6] | 1487 | return rc;
|
---|
[a35b458] | 1488 |
|
---|
[0b293a6] | 1489 | ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
|
---|
| 1490 | }
|
---|
[a35b458] | 1491 |
|
---|
[06d85e5] | 1492 | /* Free inode by allocator */
|
---|
[b7fd2a0] | 1493 | errno_t rc;
|
---|
[304faab] | 1494 | if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
|
---|
[38542dc] | 1495 | EXT4_INODE_MODE_DIRECTORY))
|
---|
[304faab] | 1496 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
|
---|
[38542dc] | 1497 | else
|
---|
[304faab] | 1498 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
|
---|
[a35b458] | 1499 |
|
---|
[38542dc] | 1500 | return rc;
|
---|
[3d4fd2c] | 1501 | }
|
---|
| 1502 |
|
---|
[81a7858] | 1503 | /** Truncate i-node data blocks.
|
---|
[9fc72fb3] | 1504 | *
|
---|
[38542dc] | 1505 | * @param inode_ref I-node to be truncated
|
---|
| 1506 | * @param new_size New size of inode (must be < current size)
|
---|
| 1507 | *
|
---|
| 1508 | * @return Error code
|
---|
| 1509 | *
|
---|
[9fc72fb3] | 1510 | */
|
---|
[b7fd2a0] | 1511 | errno_t ext4_filesystem_truncate_inode(ext4_inode_ref_t *inode_ref,
|
---|
[38542dc] | 1512 | aoff64_t new_size)
|
---|
[3d4fd2c] | 1513 | {
|
---|
[1ac1ab4] | 1514 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
[a35b458] | 1515 |
|
---|
[06d85e5] | 1516 | /* Check flags, if i-node can be truncated */
|
---|
[38542dc] | 1517 | if (!ext4_inode_can_truncate(sb, inode_ref->inode))
|
---|
[3d4fd2c] | 1518 | return EINVAL;
|
---|
[a35b458] | 1519 |
|
---|
[06d85e5] | 1520 | /* If sizes are equal, nothing has to be done. */
|
---|
[1ac1ab4] | 1521 | aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
[38542dc] | 1522 | if (old_size == new_size)
|
---|
[3d4fd2c] | 1523 | return EOK;
|
---|
[a35b458] | 1524 |
|
---|
[06d85e5] | 1525 | /* It's not suppported to make the larger file by truncate operation */
|
---|
[38542dc] | 1526 | if (old_size < new_size)
|
---|
[3d4fd2c] | 1527 | return EINVAL;
|
---|
[a35b458] | 1528 |
|
---|
[06d85e5] | 1529 | /* Compute how many blocks will be released */
|
---|
[d9bbe45] | 1530 | aoff64_t size_diff = old_size - new_size;
|
---|
[1ac1ab4] | 1531 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[ca3d77a] | 1532 | uint32_t diff_blocks_count = size_diff / block_size;
|
---|
[38542dc] | 1533 | if (size_diff % block_size != 0)
|
---|
[ca3d77a] | 1534 | diff_blocks_count++;
|
---|
[a35b458] | 1535 |
|
---|
[ca3d77a] | 1536 | uint32_t old_blocks_count = old_size / block_size;
|
---|
[38542dc] | 1537 | if (old_size % block_size != 0)
|
---|
[ca3d77a] | 1538 | old_blocks_count++;
|
---|
[a35b458] | 1539 |
|
---|
[38542dc] | 1540 | if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
|
---|
| 1541 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
---|
| 1542 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
---|
[06d85e5] | 1543 | /* Extents require special operation */
|
---|
[b7fd2a0] | 1544 | errno_t rc = ext4_extent_release_blocks_from(inode_ref,
|
---|
[38542dc] | 1545 | old_blocks_count - diff_blocks_count);
|
---|
| 1546 | if (rc != EOK)
|
---|
[ca3d77a] | 1547 | return rc;
|
---|
[5b0a3946] | 1548 | } else {
|
---|
[06d85e5] | 1549 | /* Release data blocks from the end of file */
|
---|
[a35b458] | 1550 |
|
---|
[06d85e5] | 1551 | /* Starting from 1 because of logical blocks are numbered from 0 */
|
---|
[5b0a3946] | 1552 | for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
|
---|
[b7fd2a0] | 1553 | errno_t rc = ext4_filesystem_release_inode_block(inode_ref,
|
---|
[38542dc] | 1554 | old_blocks_count - i);
|
---|
| 1555 | if (rc != EOK)
|
---|
[5b0a3946] | 1556 | return rc;
|
---|
| 1557 | }
|
---|
[3d4fd2c] | 1558 | }
|
---|
[a35b458] | 1559 |
|
---|
[06d85e5] | 1560 | /* Update i-node */
|
---|
[3d4fd2c] | 1561 | ext4_inode_set_size(inode_ref->inode, new_size);
|
---|
| 1562 | inode_ref->dirty = true;
|
---|
[a35b458] | 1563 |
|
---|
[3d4fd2c] | 1564 | return EOK;
|
---|
| 1565 | }
|
---|
| 1566 |
|
---|
[81a7858] | 1567 | /** Get physical block address by logical index of the block.
|
---|
[9fc72fb3] | 1568 | *
|
---|
[38542dc] | 1569 | * @param inode_ref I-node to read block address from
|
---|
| 1570 | * @param iblock Logical index of block
|
---|
| 1571 | * @param fblock Output pointer for return physical block address
|
---|
| 1572 | *
|
---|
| 1573 | * @return Error code
|
---|
| 1574 | *
|
---|
[9fc72fb3] | 1575 | */
|
---|
[b7fd2a0] | 1576 | errno_t ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
---|
[38542dc] | 1577 | aoff64_t iblock, uint32_t *fblock)
|
---|
[9b9d37bb] | 1578 | {
|
---|
[1ac1ab4] | 1579 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[a35b458] | 1580 |
|
---|
[06d85e5] | 1581 | /* For empty file is situation simple */
|
---|
[b73530a] | 1582 | if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
|
---|
| 1583 | *fblock = 0;
|
---|
| 1584 | return EOK;
|
---|
| 1585 | }
|
---|
[a35b458] | 1586 |
|
---|
[9b9d37bb] | 1587 | uint32_t current_block;
|
---|
[a35b458] | 1588 |
|
---|
[06d85e5] | 1589 | /* Handle i-node using extents */
|
---|
[38542dc] | 1590 | if ((ext4_superblock_has_feature_incompatible(fs->superblock,
|
---|
| 1591 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
---|
| 1592 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
---|
[b7fd2a0] | 1593 | errno_t rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
|
---|
[38542dc] | 1594 | if (rc != EOK)
|
---|
[9104bb5] | 1595 | return rc;
|
---|
[a35b458] | 1596 |
|
---|
[acd869e] | 1597 | *fblock = current_block;
|
---|
| 1598 | return EOK;
|
---|
| 1599 | }
|
---|
[a35b458] | 1600 |
|
---|
[9104bb5] | 1601 | ext4_inode_t *inode = inode_ref->inode;
|
---|
[a35b458] | 1602 |
|
---|
[06d85e5] | 1603 | /* Direct block are read directly from array in i-node structure */
|
---|
[e68c834] | 1604 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[38542dc] | 1605 | current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
|
---|
[9b9d37bb] | 1606 | *fblock = current_block;
|
---|
| 1607 | return EOK;
|
---|
| 1608 | }
|
---|
[a35b458] | 1609 |
|
---|
[06d85e5] | 1610 | /* Determine indirection level of the target block */
|
---|
[38542dc] | 1611 | unsigned int level = 0;
|
---|
| 1612 | for (unsigned int i = 1; i < 4; i++) {
|
---|
[a9a0982] | 1613 | if (iblock < fs->inode_block_limits[i]) {
|
---|
[9b9d37bb] | 1614 | level = i;
|
---|
| 1615 | break;
|
---|
| 1616 | }
|
---|
| 1617 | }
|
---|
[a35b458] | 1618 |
|
---|
[38542dc] | 1619 | if (level == 0)
|
---|
[9b9d37bb] | 1620 | return EIO;
|
---|
[a35b458] | 1621 |
|
---|
[06d85e5] | 1622 | /* Compute offsets for the topmost level */
|
---|
[38542dc] | 1623 | aoff64_t block_offset_in_level =
|
---|
| 1624 | iblock - fs->inode_block_limits[level - 1];
|
---|
| 1625 | current_block = ext4_inode_get_indirect_block(inode, level - 1);
|
---|
| 1626 | uint32_t offset_in_block =
|
---|
| 1627 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
---|
[a35b458] | 1628 |
|
---|
[06d85e5] | 1629 | /* Sparse file */
|
---|
[6088193] | 1630 | if (current_block == 0) {
|
---|
| 1631 | *fblock = 0;
|
---|
| 1632 | return EOK;
|
---|
| 1633 | }
|
---|
[a35b458] | 1634 |
|
---|
[d9bbe45] | 1635 | block_t *block;
|
---|
[a35b458] | 1636 |
|
---|
[38542dc] | 1637 | /*
|
---|
| 1638 | * Navigate through other levels, until we find the block number
|
---|
[9b9d37bb] | 1639 | * or find null reference meaning we are dealing with sparse file
|
---|
| 1640 | */
|
---|
| 1641 | while (level > 0) {
|
---|
[06d85e5] | 1642 | /* Load indirect block */
|
---|
[b7fd2a0] | 1643 | errno_t rc = block_get(&block, fs->device, current_block, 0);
|
---|
[38542dc] | 1644 | if (rc != EOK)
|
---|
[9b9d37bb] | 1645 | return rc;
|
---|
[a35b458] | 1646 |
|
---|
[06d85e5] | 1647 | /* Read block address from indirect block */
|
---|
[38542dc] | 1648 | current_block =
|
---|
| 1649 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
---|
[a35b458] | 1650 |
|
---|
[06d85e5] | 1651 | /* Put back indirect block untouched */
|
---|
[9b9d37bb] | 1652 | rc = block_put(block);
|
---|
[38542dc] | 1653 | if (rc != EOK)
|
---|
[9b9d37bb] | 1654 | return rc;
|
---|
[a35b458] | 1655 |
|
---|
[06d85e5] | 1656 | /* Check for sparse file */
|
---|
[9b9d37bb] | 1657 | if (current_block == 0) {
|
---|
| 1658 | *fblock = 0;
|
---|
| 1659 | return EOK;
|
---|
| 1660 | }
|
---|
[a35b458] | 1661 |
|
---|
[06d85e5] | 1662 | /* Jump to the next level */
|
---|
[38542dc] | 1663 | level--;
|
---|
[a35b458] | 1664 |
|
---|
[06d85e5] | 1665 | /* Termination condition - we have address of data block loaded */
|
---|
[38542dc] | 1666 | if (level == 0)
|
---|
[9b9d37bb] | 1667 | break;
|
---|
[a35b458] | 1668 |
|
---|
[06d85e5] | 1669 | /* Visit the next level */
|
---|
[a9a0982] | 1670 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
[38542dc] | 1671 | offset_in_block =
|
---|
| 1672 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
---|
[9b9d37bb] | 1673 | }
|
---|
[a35b458] | 1674 |
|
---|
[9b9d37bb] | 1675 | *fblock = current_block;
|
---|
[a35b458] | 1676 |
|
---|
[3711e7e] | 1677 | return EOK;
|
---|
| 1678 | }
|
---|
[6c501f8] | 1679 |
|
---|
[8060341a] | 1680 | /** Set physical block address for the block logical address into the i-node.
|
---|
[9fc72fb3] | 1681 | *
|
---|
[38542dc] | 1682 | * @param inode_ref I-node to set block address to
|
---|
| 1683 | * @param iblock Logical index of block
|
---|
| 1684 | * @param fblock Physical block address
|
---|
| 1685 | *
|
---|
| 1686 | * @return Error code
|
---|
| 1687 | *
|
---|
[9fc72fb3] | 1688 | */
|
---|
[b7fd2a0] | 1689 | errno_t ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
---|
[38542dc] | 1690 | aoff64_t iblock, uint32_t fblock)
|
---|
[35f48f2] | 1691 | {
|
---|
[1ac1ab4] | 1692 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[a35b458] | 1693 |
|
---|
[06d85e5] | 1694 | /* Handle inode using extents */
|
---|
[38542dc] | 1695 | if ((ext4_superblock_has_feature_compatible(fs->superblock,
|
---|
| 1696 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
---|
| 1697 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
|
---|
| 1698 | /* Not reachable */
|
---|
[35f48f2] | 1699 | return ENOTSUP;
|
---|
| 1700 | }
|
---|
[a35b458] | 1701 |
|
---|
[06d85e5] | 1702 | /* Handle simple case when we are dealing with direct reference */
|
---|
[35f48f2] | 1703 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[38542dc] | 1704 | ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
|
---|
[1e65444] | 1705 | inode_ref->dirty = true;
|
---|
[a35b458] | 1706 |
|
---|
[35f48f2] | 1707 | return EOK;
|
---|
| 1708 | }
|
---|
[a35b458] | 1709 |
|
---|
[06d85e5] | 1710 | /* Determine the indirection level needed to get the desired block */
|
---|
[38542dc] | 1711 | unsigned int level = 0;
|
---|
| 1712 | for (unsigned int i = 1; i < 4; i++) {
|
---|
[1e65444] | 1713 | if (iblock < fs->inode_block_limits[i]) {
|
---|
| 1714 | level = i;
|
---|
| 1715 | break;
|
---|
| 1716 | }
|
---|
| 1717 | }
|
---|
[a35b458] | 1718 |
|
---|
[38542dc] | 1719 | if (level == 0)
|
---|
[1e65444] | 1720 | return EIO;
|
---|
[a35b458] | 1721 |
|
---|
[d9bbe45] | 1722 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
[a35b458] | 1723 |
|
---|
[06d85e5] | 1724 | /* Compute offsets for the topmost level */
|
---|
[38542dc] | 1725 | aoff64_t block_offset_in_level =
|
---|
| 1726 | iblock - fs->inode_block_limits[level - 1];
|
---|
| 1727 | uint32_t current_block =
|
---|
| 1728 | ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
|
---|
| 1729 | uint32_t offset_in_block =
|
---|
| 1730 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
---|
[a35b458] | 1731 |
|
---|
[d9bbe45] | 1732 | uint32_t new_block_addr;
|
---|
[38542dc] | 1733 | block_t *block;
|
---|
| 1734 | block_t *new_block;
|
---|
[a35b458] | 1735 |
|
---|
[06d85e5] | 1736 | /* Is needed to allocate indirect block on the i-node level */
|
---|
[1e65444] | 1737 | if (current_block == 0) {
|
---|
[06d85e5] | 1738 | /* Allocate new indirect block */
|
---|
[b7fd2a0] | 1739 | errno_t rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
---|
[38542dc] | 1740 | if (rc != EOK)
|
---|
[e63ce679] | 1741 | return rc;
|
---|
[a35b458] | 1742 |
|
---|
[06d85e5] | 1743 | /* Update i-node */
|
---|
[38542dc] | 1744 | ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
|
---|
| 1745 | new_block_addr);
|
---|
[1e65444] | 1746 | inode_ref->dirty = true;
|
---|
[a35b458] | 1747 |
|
---|
[06d85e5] | 1748 | /* Load newly allocated block */
|
---|
[38542dc] | 1749 | rc = block_get(&new_block, fs->device, new_block_addr,
|
---|
| 1750 | BLOCK_FLAGS_NOREAD);
|
---|
[1e65444] | 1751 | if (rc != EOK) {
|
---|
[1ac1ab4] | 1752 | ext4_balloc_free_block(inode_ref, new_block_addr);
|
---|
[e63ce679] | 1753 | return rc;
|
---|
[1e65444] | 1754 | }
|
---|
[a35b458] | 1755 |
|
---|
[06d85e5] | 1756 | /* Initialize new block */
|
---|
[1e65444] | 1757 | memset(new_block->data, 0, block_size);
|
---|
| 1758 | new_block->dirty = true;
|
---|
[a35b458] | 1759 |
|
---|
[06d85e5] | 1760 | /* Put back the allocated block */
|
---|
[1e65444] | 1761 | rc = block_put(new_block);
|
---|
[38542dc] | 1762 | if (rc != EOK)
|
---|
[e63ce679] | 1763 | return rc;
|
---|
[a35b458] | 1764 |
|
---|
[1e65444] | 1765 | current_block = new_block_addr;
|
---|
| 1766 | }
|
---|
[a35b458] | 1767 |
|
---|
[38542dc] | 1768 | /*
|
---|
| 1769 | * Navigate through other levels, until we find the block number
|
---|
[1e65444] | 1770 | * or find null reference meaning we are dealing with sparse file
|
---|
| 1771 | */
|
---|
| 1772 | while (level > 0) {
|
---|
[b7fd2a0] | 1773 | errno_t rc = block_get(&block, fs->device, current_block, 0);
|
---|
[38542dc] | 1774 | if (rc != EOK)
|
---|
[1e65444] | 1775 | return rc;
|
---|
[a35b458] | 1776 |
|
---|
[38542dc] | 1777 | current_block =
|
---|
| 1778 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
---|
[a35b458] | 1779 |
|
---|
[b12ca16] | 1780 | if ((level > 1) && (current_block == 0)) {
|
---|
[06d85e5] | 1781 | /* Allocate new block */
|
---|
[1ac1ab4] | 1782 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
---|
[b12ca16] | 1783 | if (rc != EOK) {
|
---|
[e63ce679] | 1784 | block_put(block);
|
---|
| 1785 | return rc;
|
---|
[b12ca16] | 1786 | }
|
---|
[a35b458] | 1787 |
|
---|
[06d85e5] | 1788 | /* Load newly allocated block */
|
---|
[38542dc] | 1789 | rc = block_get(&new_block, fs->device, new_block_addr,
|
---|
| 1790 | BLOCK_FLAGS_NOREAD);
|
---|
[b12ca16] | 1791 | if (rc != EOK) {
|
---|
[e63ce679] | 1792 | block_put(block);
|
---|
| 1793 | return rc;
|
---|
[b12ca16] | 1794 | }
|
---|
[a35b458] | 1795 |
|
---|
[06d85e5] | 1796 | /* Initialize allocated block */
|
---|
[b12ca16] | 1797 | memset(new_block->data, 0, block_size);
|
---|
| 1798 | new_block->dirty = true;
|
---|
[a35b458] | 1799 |
|
---|
[b12ca16] | 1800 | rc = block_put(new_block);
|
---|
| 1801 | if (rc != EOK) {
|
---|
[e63ce679] | 1802 | block_put(block);
|
---|
| 1803 | return rc;
|
---|
[b12ca16] | 1804 | }
|
---|
[a35b458] | 1805 |
|
---|
[06d85e5] | 1806 | /* Write block address to the parent */
|
---|
[38542dc] | 1807 | ((uint32_t *) block->data)[offset_in_block] =
|
---|
| 1808 | host2uint32_t_le(new_block_addr);
|
---|
[b12ca16] | 1809 | block->dirty = true;
|
---|
| 1810 | current_block = new_block_addr;
|
---|
| 1811 | }
|
---|
[a35b458] | 1812 |
|
---|
[06d85e5] | 1813 | /* Will be finished, write the fblock address */
|
---|
[b12ca16] | 1814 | if (level == 1) {
|
---|
[38542dc] | 1815 | ((uint32_t *) block->data)[offset_in_block] =
|
---|
| 1816 | host2uint32_t_le(fblock);
|
---|
[b12ca16] | 1817 | block->dirty = true;
|
---|
[1e65444] | 1818 | }
|
---|
[a35b458] | 1819 |
|
---|
[1e65444] | 1820 | rc = block_put(block);
|
---|
[38542dc] | 1821 | if (rc != EOK)
|
---|
[1e65444] | 1822 | return rc;
|
---|
[a35b458] | 1823 |
|
---|
[38542dc] | 1824 | level--;
|
---|
[a35b458] | 1825 |
|
---|
[38542dc] | 1826 | /*
|
---|
| 1827 | * If we are on the last level, break here as
|
---|
[1e65444] | 1828 | * there is no next level to visit
|
---|
| 1829 | */
|
---|
[38542dc] | 1830 | if (level == 0)
|
---|
[1e65444] | 1831 | break;
|
---|
[a35b458] | 1832 |
|
---|
[1e65444] | 1833 | /* Visit the next level */
|
---|
| 1834 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
[38542dc] | 1835 | offset_in_block =
|
---|
| 1836 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
---|
[1e65444] | 1837 | }
|
---|
[a35b458] | 1838 |
|
---|
[35f48f2] | 1839 | return EOK;
|
---|
| 1840 | }
|
---|
| 1841 |
|
---|
[8060341a] | 1842 | /** Release data block from i-node
|
---|
[9fc72fb3] | 1843 | *
|
---|
[38542dc] | 1844 | * @param inode_ref I-node to release block from
|
---|
| 1845 | * @param iblock Logical block to be released
|
---|
| 1846 | *
|
---|
| 1847 | * @return Error code
|
---|
| 1848 | *
|
---|
[9fc72fb3] | 1849 | */
|
---|
[b7fd2a0] | 1850 | errno_t ext4_filesystem_release_inode_block(ext4_inode_ref_t *inode_ref,
|
---|
[38542dc] | 1851 | uint32_t iblock)
|
---|
[d5a78e28] | 1852 | {
|
---|
[fffb061] | 1853 | uint32_t fblock;
|
---|
[a35b458] | 1854 |
|
---|
[1ac1ab4] | 1855 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
[a35b458] | 1856 |
|
---|
[38542dc] | 1857 | /* Extents are handled otherwise = there is not support in this function */
|
---|
| 1858 | assert(!(ext4_superblock_has_feature_incompatible(fs->superblock,
|
---|
| 1859 | EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
| 1860 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
|
---|
[a35b458] | 1861 |
|
---|
[d9bbe45] | 1862 | ext4_inode_t *inode = inode_ref->inode;
|
---|
[a35b458] | 1863 |
|
---|
[06d85e5] | 1864 | /* Handle simple case when we are dealing with direct reference */
|
---|
[052e82d] | 1865 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
[12b4a7f] | 1866 | fblock = ext4_inode_get_direct_block(inode, iblock);
|
---|
[a35b458] | 1867 |
|
---|
[06d85e5] | 1868 | /* Sparse file */
|
---|
[38542dc] | 1869 | if (fblock == 0)
|
---|
[052e82d] | 1870 | return EOK;
|
---|
[a35b458] | 1871 |
|
---|
[052e82d] | 1872 | ext4_inode_set_direct_block(inode, iblock, 0);
|
---|
[1ac1ab4] | 1873 | return ext4_balloc_free_block(inode_ref, fblock);
|
---|
[12b4a7f] | 1874 | }
|
---|
[a35b458] | 1875 |
|
---|
[06d85e5] | 1876 | /* Determine the indirection level needed to get the desired block */
|
---|
[38542dc] | 1877 | unsigned int level = 0;
|
---|
| 1878 | for (unsigned int i = 1; i < 4; i++) {
|
---|
[12b4a7f] | 1879 | if (iblock < fs->inode_block_limits[i]) {
|
---|
| 1880 | level = i;
|
---|
| 1881 | break;
|
---|
[052e82d] | 1882 | }
|
---|
[12b4a7f] | 1883 | }
|
---|
[a35b458] | 1884 |
|
---|
[38542dc] | 1885 | if (level == 0)
|
---|
[12b4a7f] | 1886 | return EIO;
|
---|
[a35b458] | 1887 |
|
---|
[06d85e5] | 1888 | /* Compute offsets for the topmost level */
|
---|
[38542dc] | 1889 | aoff64_t block_offset_in_level =
|
---|
| 1890 | iblock - fs->inode_block_limits[level - 1];
|
---|
| 1891 | uint32_t current_block =
|
---|
| 1892 | ext4_inode_get_indirect_block(inode, level - 1);
|
---|
| 1893 | uint32_t offset_in_block =
|
---|
| 1894 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
---|
[a35b458] | 1895 |
|
---|
[38542dc] | 1896 | /*
|
---|
| 1897 | * Navigate through other levels, until we find the block number
|
---|
[12b4a7f] | 1898 | * or find null reference meaning we are dealing with sparse file
|
---|
| 1899 | */
|
---|
[d9bbe45] | 1900 | block_t *block;
|
---|
[12b4a7f] | 1901 | while (level > 0) {
|
---|
[a35b458] | 1902 |
|
---|
[041ab64] | 1903 | /* Sparse check */
|
---|
| 1904 | if (current_block == 0)
|
---|
| 1905 | return EOK;
|
---|
[a35b458] | 1906 |
|
---|
[b7fd2a0] | 1907 | errno_t rc = block_get(&block, fs->device, current_block, 0);
|
---|
[38542dc] | 1908 | if (rc != EOK)
|
---|
[12b4a7f] | 1909 | return rc;
|
---|
[a35b458] | 1910 |
|
---|
[38542dc] | 1911 | current_block =
|
---|
| 1912 | uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
|
---|
[a35b458] | 1913 |
|
---|
[06d85e5] | 1914 | /* Set zero if physical data block address found */
|
---|
[12b4a7f] | 1915 | if (level == 1) {
|
---|
[38542dc] | 1916 | ((uint32_t *) block->data)[offset_in_block] =
|
---|
| 1917 | host2uint32_t_le(0);
|
---|
[12b4a7f] | 1918 | block->dirty = true;
|
---|
[052e82d] | 1919 | }
|
---|
[a35b458] | 1920 |
|
---|
[12b4a7f] | 1921 | rc = block_put(block);
|
---|
[38542dc] | 1922 | if (rc != EOK)
|
---|
[12b4a7f] | 1923 | return rc;
|
---|
[a35b458] | 1924 |
|
---|
[38542dc] | 1925 | level--;
|
---|
[a35b458] | 1926 |
|
---|
[38542dc] | 1927 | /*
|
---|
| 1928 | * If we are on the last level, break here as
|
---|
[12b4a7f] | 1929 | * there is no next level to visit
|
---|
| 1930 | */
|
---|
[38542dc] | 1931 | if (level == 0)
|
---|
[12b4a7f] | 1932 | break;
|
---|
[a35b458] | 1933 |
|
---|
[12b4a7f] | 1934 | /* Visit the next level */
|
---|
| 1935 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
[38542dc] | 1936 | offset_in_block =
|
---|
| 1937 | block_offset_in_level / fs->inode_blocks_per_level[level - 1];
|
---|
[12b4a7f] | 1938 | }
|
---|
[a35b458] | 1939 |
|
---|
[12b4a7f] | 1940 | fblock = current_block;
|
---|
[38542dc] | 1941 | if (fblock == 0)
|
---|
[12b4a7f] | 1942 | return EOK;
|
---|
[a35b458] | 1943 |
|
---|
[06d85e5] | 1944 | /* Physical block is not referenced, it can be released */
|
---|
[1ac1ab4] | 1945 | return ext4_balloc_free_block(inode_ref, fblock);
|
---|
[d5a78e28] | 1946 | }
|
---|
| 1947 |
|
---|
[81a7858] | 1948 | /** Append following logical block to the i-node.
|
---|
[9fc72fb3] | 1949 | *
|
---|
[38542dc] | 1950 | * @param inode_ref I-node to append block to
|
---|
| 1951 | * @param fblock Output physical block address of newly allocated block
|
---|
| 1952 | * @param iblock Output logical number of newly allocated block
|
---|
| 1953 | *
|
---|
| 1954 | * @return Error code
|
---|
| 1955 | *
|
---|
[9fc72fb3] | 1956 | */
|
---|
[b7fd2a0] | 1957 | errno_t ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
|
---|
[38542dc] | 1958 | uint32_t *fblock, uint32_t *iblock)
|
---|
[5b16912] | 1959 | {
|
---|
[06d85e5] | 1960 | /* Handle extents separately */
|
---|
[38542dc] | 1961 | if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
|
---|
| 1962 | EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
|
---|
| 1963 | (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))
|
---|
[d510ac01] | 1964 | return ext4_extent_append_block(inode_ref, iblock, fblock, true);
|
---|
[a35b458] | 1965 |
|
---|
[5b16912] | 1966 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
[a35b458] | 1967 |
|
---|
[06d85e5] | 1968 | /* Compute next block index and allocate data block */
|
---|
[5b16912] | 1969 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
| 1970 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
[a35b458] | 1971 |
|
---|
[06d85e5] | 1972 | /* Align size i-node size */
|
---|
[38542dc] | 1973 | if ((inode_size % block_size) != 0)
|
---|
[81a7858] | 1974 | inode_size += block_size - (inode_size % block_size);
|
---|
[a35b458] | 1975 |
|
---|
[06d85e5] | 1976 | /* Logical blocks are numbered from 0 */
|
---|
[5b16912] | 1977 | uint32_t new_block_idx = inode_size / block_size;
|
---|
[a35b458] | 1978 |
|
---|
[06d85e5] | 1979 | /* Allocate new physical block */
|
---|
[5b16912] | 1980 | uint32_t phys_block;
|
---|
[b7fd2a0] | 1981 | errno_t rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
|
---|
[38542dc] | 1982 | if (rc != EOK)
|
---|
[5b16912] | 1983 | return rc;
|
---|
[a35b458] | 1984 |
|
---|
[06d85e5] | 1985 | /* Add physical block address to the i-node */
|
---|
[38542dc] | 1986 | rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
|
---|
| 1987 | new_block_idx, phys_block);
|
---|
[5b16912] | 1988 | if (rc != EOK) {
|
---|
| 1989 | ext4_balloc_free_block(inode_ref, phys_block);
|
---|
| 1990 | return rc;
|
---|
| 1991 | }
|
---|
[a35b458] | 1992 |
|
---|
[06d85e5] | 1993 | /* Update i-node */
|
---|
[5b16912] | 1994 | ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
|
---|
| 1995 | inode_ref->dirty = true;
|
---|
[a35b458] | 1996 |
|
---|
[5b16912] | 1997 | *fblock = phys_block;
|
---|
| 1998 | *iblock = new_block_idx;
|
---|
[a35b458] | 1999 |
|
---|
[5b16912] | 2000 | return EOK;
|
---|
| 2001 | }
|
---|
| 2002 |
|
---|
[aab85d90] | 2003 | /** Get the number of inodes per block.
|
---|
| 2004 | *
|
---|
| 2005 | * @param sb Superblock
|
---|
| 2006 | * @return Number of inodes per block
|
---|
| 2007 | */
|
---|
| 2008 | static uint32_t ext4_filesystem_inodes_per_block(ext4_superblock_t *sb)
|
---|
| 2009 | {
|
---|
| 2010 | uint32_t inode_size = ext4_superblock_get_inode_size(sb);
|
---|
| 2011 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
| 2012 |
|
---|
| 2013 | return block_size / inode_size;
|
---|
| 2014 | }
|
---|
| 2015 |
|
---|
[6c501f8] | 2016 | /**
|
---|
| 2017 | * @}
|
---|
[38542dc] | 2018 | */
|
---|