source: mainline/uspace/lib/ext4/src/filesystem.c@ 2175178

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2175178 was 2175178, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Allow creating ext2 dynamic revision. Allow choosing ext filesystem version by mkext4 argument. Fix support for filesystems without filetype feature.

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