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