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