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