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
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
3 * Copyright (c) 2011 Martin Sucha
4 * Copyright (c) 2012 Frantisek Princ
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 * @{
33 */
34/**
35 * @file filesystem.c
36 * @brief More complex filesystem operations.
37 */
38
39#include <byteorder.h>
40#include <errno.h>
41#include <mem.h>
42#include <align.h>
43#include <crypto.h>
44#include <ipc/vfs.h>
45#include <libfs.h>
46#include <stdlib.h>
47#include "ext4/balloc.h"
48#include "ext4/bitmap.h"
49#include "ext4/block_group.h"
50#include "ext4/cfg.h"
51#include "ext4/directory.h"
52#include "ext4/extent.h"
53#include "ext4/filesystem.h"
54#include "ext4/ialloc.h"
55#include "ext4/inode.h"
56#include "ext4/ops.h"
57#include "ext4/superblock.h"
58
59static errno_t ext4_filesystem_check_features(ext4_filesystem_t *, bool *);
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 *);
64
65/** Initialize filesystem for opening.
66 *
67 * But do not mark mounted just yet.
68 *
69 * @param fs Filesystem instance to be initialized
70 * @param service_id Block device to open
71 * @param cmode Cache mode
72 *
73 * @return Error code
74 *
75 */
76static errno_t ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id,
77 enum cache_mode cmode)
78{
79 errno_t rc;
80 ext4_superblock_t *temp_superblock = NULL;
81
82 fs->device = service_id;
83
84 /* Initialize block library (4096 is size of communication channel) */
85 rc = block_init(fs->device, 4096);
86 if (rc != EOK)
87 goto err;
88
89 /* Read superblock from device to memory */
90 rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
91 if (rc != EOK)
92 goto err_1;
93
94 /* Read block size from superblock and check */
95 uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
96 if (block_size > EXT4_MAX_BLOCK_SIZE) {
97 rc = ENOTSUP;
98 goto err_1;
99 }
100
101 /* Initialize block caching by libblock */
102 rc = block_cache_init(service_id, block_size, 0, cmode);
103 if (rc != EOK)
104 goto err_1;
105
106 /* Compute limits for indirect block levels */
107 uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
108 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
109 fs->inode_blocks_per_level[0] = 1;
110 for (unsigned int i = 1; i < 4; i++) {
111 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
112 block_ids_per_block;
113 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
114 fs->inode_blocks_per_level[i];
115 }
116
117 /* Return loaded superblock */
118 fs->superblock = temp_superblock;
119
120 uint16_t state = ext4_superblock_get_state(fs->superblock);
121
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)) {
126 rc = ENOTSUP;
127 goto err_2;
128 }
129
130 rc = ext4_superblock_check_sanity(fs->superblock);
131 if (rc != EOK)
132 goto err_2;
133
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
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
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 *
251 * @param ver Filesystem version
252 * @param service_id Block device where to create new filesystem
253 */
254errno_t ext4_filesystem_create(ext4_cfg_ver_t ver, service_id_t service_id)
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 */
284 rc = ext4_superblock_create(dev_bsize, dev_nblocks, ver, &superblock);
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
373/** Probe filesystem.
374 *
375 * @param service_id Block device to probe
376 *
377 * @return EOK or an error code.
378 *
379 */
380errno_t ext4_filesystem_probe(service_id_t service_id)
381{
382 ext4_filesystem_t *fs = NULL;
383 errno_t rc;
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
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 */
411errno_t ext4_filesystem_open(ext4_instance_t *inst, service_id_t service_id,
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;
416 errno_t rc;
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
431 /* Read root node */
432 rc = ext4_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
433 if (rc != EOK)
434 goto error;
435
436 /* Mark system as mounted */
437 ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_ERROR_FS);
438 rc = ext4_superblock_write_direct(fs->device, fs->superblock);
439 if (rc != EOK)
440 goto error;
441
442 uint16_t mnt_count = ext4_superblock_get_mount_count(fs->superblock);
443 ext4_superblock_set_mount_count(fs->superblock, mnt_count + 1);
444
445 ext4_node_t *enode = EXT4_NODE(root_node);
446
447 *size = ext4_inode_get_size(fs->superblock, enode->inode_ref->inode);
448
449 ext4_node_put(root_node);
450 *rfs = fs;
451 return EOK;
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
461 return rc;
462}
463
464/** Close filesystem.
465 *
466 * @param fs Filesystem to be destroyed
467 *
468 * @return EOK or an error code. On error the state of the file
469 * system is unchanged.
470 *
471 */
472errno_t ext4_filesystem_close(ext4_filesystem_t *fs)
473{
474 /* Write the superblock to the device */
475 ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_VALID_FS);
476 errno_t rc = ext4_superblock_write_direct(fs->device, fs->superblock);
477 if (rc != EOK)
478 return rc;
479
480 ext4_filesystem_fini(fs);
481 return EOK;
482}
483
484/** Check filesystem's features, if supported by this driver
485 *
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 *
490 * @param fs Filesystem to be checked
491 * @param read_only Place to write flag saying whether filesystem
492 * should be mounted only for reading
493 *
494 * @return Error code
495 *
496 */
497static errno_t ext4_filesystem_check_features(ext4_filesystem_t *fs,
498 bool *read_only)
499{
500 /* Feature flags are present only in higher revisions */
501 if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
502 *read_only = false;
503 return EOK;
504 }
505
506 /*
507 * Check incompatible features - if filesystem has some,
508 * volume can't be mounted
509 */
510 uint32_t incompatible_features;
511 incompatible_features =
512 ext4_superblock_get_features_incompatible(fs->superblock);
513 incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
514 if (incompatible_features > 0)
515 return ENOTSUP;
516
517 /*
518 * Check read-only features, if filesystem has some,
519 * volume can be mount only in read-only mode
520 */
521 uint32_t compatible_read_only;
522 compatible_read_only =
523 ext4_superblock_get_features_read_only(fs->superblock);
524 compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
525 if (compatible_read_only > 0) {
526 *read_only = true;
527 return EOK;
528 }
529
530 return EOK;
531}
532
533/** Convert block address to relative index in block group.
534 *
535 * @param sb Superblock pointer
536 * @param block_addr Block number to convert
537 *
538 * @return Relative number of block
539 *
540 */
541uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *sb,
542 uint32_t block_addr)
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);
546
547 /* First block == 0 or 1 */
548 if (first_block == 0)
549 return block_addr % blocks_per_group;
550 else
551 return (block_addr - 1) % blocks_per_group;
552}
553
554/** Convert relative block address in group to absolute address.
555 *
556 * @param sb Superblock pointer
557 *
558 * @return Absolute block address
559 *
560 */
561uint32_t ext4_filesystem_index_in_group2blockaddr(ext4_superblock_t *sb,
562 uint32_t index, uint32_t bgid)
563{
564 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
565
566 if (ext4_superblock_get_first_data_block(sb) == 0)
567 return bgid * blocks_per_group + index;
568 else
569 return bgid * blocks_per_group + index + 1;
570}
571
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);
582 uint32_t first_block = ext4_superblock_get_first_data_block(sb);
583
584 return (b - first_block) / blocks_per_group;
585}
586
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;
707 if (bg_index == 0)
708 ++free_blocks; /* XXX Why? */
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
722/** Initialize block bitmap in block group.
723 *
724 * @param bg_ref Reference to block group
725 *
726 * @return Error code
727 *
728 */
729static errno_t ext4_filesystem_init_block_bitmap(ext4_block_group_ref_t *bg_ref)
730{
731 uint64_t itb;
732 uint32_t sz;
733 uint32_t i;
734
735 /* Load bitmap */
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(
740 bg_ref->block_group, bg_ref->fs->superblock);
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);
744
745 block_t *bitmap_block;
746 errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
747 bitmap_block_addr, BLOCK_FLAGS_NOREAD);
748 if (rc != EOK)
749 return rc;
750
751 uint8_t *bitmap = bitmap_block->data;
752
753 /* Initialize all bitmap bits to zero */
754 uint32_t block_size = ext4_superblock_get_block_size(sb);
755 memset(bitmap, 0, block_size);
756
757 /* Determine the number of reserved blocks in the group */
758 uint32_t reserved_cnt = ext4_filesystem_bg_get_backup_blocks(bg_ref);
759
760 /* Set bits from to first block to first data block - 1 to one (allocated) */
761 for (uint32_t block = 0; block < reserved_cnt; ++block)
762 ext4_bitmap_set_bit(bitmap, block);
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);
780 sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref->index);
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
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
795 bitmap_block->dirty = true;
796
797 /* Save bitmap */
798 return block_put(bitmap_block);
799}
800
801/** Initialize i-node bitmap in block group.
802 *
803 * @param bg_ref Reference to block group
804 *
805 * @return Error code
806 *
807 */
808static errno_t ext4_filesystem_init_inode_bitmap(ext4_block_group_ref_t *bg_ref)
809{
810 /* Load bitmap */
811 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
812 bg_ref->block_group, bg_ref->fs->superblock);
813 block_t *bitmap_block;
814
815 errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
816 bitmap_block_addr, BLOCK_FLAGS_NOREAD);
817 if (rc != EOK)
818 return rc;
819
820 uint8_t *bitmap = bitmap_block->data;
821
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 =
825 ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
826 memset(bitmap, 0, (inodes_per_group + 7) / 8);
827
828 uint32_t start_bit = inodes_per_group;
829 uint32_t end_bit = block_size * 8;
830
831 uint32_t i;
832 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
833 ext4_bitmap_set_bit(bitmap, i);
834
835 if (i < end_bit)
836 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
837
838 bitmap_block->dirty = true;
839
840 /* Save bitmap */
841 return block_put(bitmap_block);
842}
843
844/** Initialize i-node table in block group.
845 *
846 * @param bg_ref Reference to block group
847 *
848 * @return Error code
849 *
850 */
851static errno_t ext4_filesystem_init_inode_table(ext4_block_group_ref_t *bg_ref)
852{
853 ext4_superblock_t *sb = bg_ref->fs->superblock;
854
855 uint32_t block_size = ext4_superblock_get_block_size(sb);
856 uint32_t inodes_per_block = ext4_filesystem_inodes_per_block(sb);
857
858 uint32_t inodes_in_group =
859 ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
860
861 uint32_t table_blocks = inodes_in_group / inodes_per_block;
862
863 if (inodes_in_group % inodes_per_block)
864 table_blocks++;
865
866 /* Compute initialization bounds */
867 uint32_t first_block = ext4_block_group_get_inode_table_first_block(
868 bg_ref->block_group, sb);
869
870 uint32_t last_block = first_block + table_blocks - 1;
871
872 /* Initialization of all itable blocks */
873 for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
874 block_t *block;
875 errno_t rc = block_get(&block, bg_ref->fs->device, fblock,
876 BLOCK_FLAGS_NOREAD);
877 if (rc != EOK)
878 return rc;
879
880 memset(block->data, 0, block_size);
881 block->dirty = true;
882
883 rc = block_put(block);
884 if (rc != EOK)
885 return rc;
886 }
887
888 return EOK;
889}
890
891/** Get reference to block group specified by index.
892 *
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 *
899 */
900errno_t ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
901 ext4_block_group_ref_t **ref)
902{
903 /* Allocate memory for new structure */
904 ext4_block_group_ref_t *newref =
905 malloc(sizeof(ext4_block_group_ref_t));
906 if (newref == NULL)
907 return ENOMEM;
908
909 /* Compute number of descriptors, that fits in one data block */
910 uint32_t descriptors_per_block =
911 ext4_superblock_get_block_size(fs->superblock) /
912 ext4_superblock_get_desc_size(fs->superblock);
913
914 /* Block group descriptor table starts at the next block after superblock */
915 aoff64_t block_id =
916 ext4_superblock_get_first_data_block(fs->superblock) + 1;
917
918 /* Find the block containing the descriptor we are looking for */
919 block_id += bgid / descriptors_per_block;
920 uint32_t offset = (bgid % descriptors_per_block) *
921 ext4_superblock_get_desc_size(fs->superblock);
922
923 /* Load block with descriptors */
924 errno_t rc = block_get(&newref->block, fs->device, block_id, 0);
925 if (rc != EOK) {
926 free(newref);
927 return rc;
928 }
929
930 /* Initialize in-memory representation */
931 newref->block_group = newref->block->data + offset;
932 newref->fs = fs;
933 newref->index = bgid;
934 newref->dirty = false;
935
936 *ref = newref;
937
938 if (ext4_block_group_has_flag(newref->block_group,
939 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
940 rc = ext4_filesystem_init_block_bitmap(newref);
941 if (rc != EOK) {
942 block_put(newref->block);
943 free(newref);
944 return rc;
945 }
946
947 ext4_block_group_clear_flag(newref->block_group,
948 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
949
950 newref->dirty = true;
951 }
952
953 if (ext4_block_group_has_flag(newref->block_group,
954 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
955 rc = ext4_filesystem_init_inode_bitmap(newref);
956 if (rc != EOK) {
957 block_put(newref->block);
958 free(newref);
959 return rc;
960 }
961
962 ext4_block_group_clear_flag(newref->block_group,
963 EXT4_BLOCK_GROUP_INODE_UNINIT);
964
965 if (!ext4_block_group_has_flag(newref->block_group,
966 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
967 rc = ext4_filesystem_init_inode_table(newref);
968 if (rc != EOK) {
969 block_put(newref->block);
970 free(newref);
971 return rc;
972 }
973
974 ext4_block_group_set_flag(newref->block_group,
975 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
976 }
977
978 newref->dirty = true;
979 }
980
981 return EOK;
982}
983
984/** Compute checksum of block group descriptor.
985 *
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 *
992 */
993static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
994 ext4_block_group_t *bg)
995{
996 /* If checksum not supported, 0 will be returned */
997 uint16_t crc = 0;
998
999 /* Compute the checksum only if the filesystem supports it */
1000 if (ext4_superblock_has_feature_read_only(sb,
1001 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1002 void *base = bg;
1003 void *checksum = &bg->checksum;
1004
1005 uint32_t offset = (uint32_t) (checksum - base);
1006
1007 /* Convert block group index to little endian */
1008 uint32_t le_group = host2uint32_t_le(bgid);
1009
1010 /* Initialization */
1011 crc = crc16_ibm(~0, sb->uuid, sizeof(sb->uuid));
1012
1013 /* Include index of block group */
1014 crc = crc16_ibm(crc, (uint8_t *) &le_group, sizeof(le_group));
1015
1016 /* Compute crc from the first part (stop before checksum field) */
1017 crc = crc16_ibm(crc, (uint8_t *) bg, offset);
1018
1019 /* Skip checksum */
1020 offset += sizeof(bg->checksum);
1021
1022 /* Checksum of the rest of block group descriptor */
1023 if ((ext4_superblock_has_feature_incompatible(sb,
1024 EXT4_FEATURE_INCOMPAT_64BIT)) &&
1025 (offset < ext4_superblock_get_desc_size(sb)))
1026 crc = crc16_ibm(crc, ((uint8_t *) bg) + offset,
1027 ext4_superblock_get_desc_size(sb) - offset);
1028 }
1029
1030 return crc;
1031}
1032
1033/** Get the size of the block group's inode table
1034 *
1035 * @param sb Pointer to the superblock
1036 * @param bg_index Block group index
1037 *
1038 * @return Size of the inode table in blocks.
1039 */
1040uint32_t ext4_filesystem_bg_get_itable_size(ext4_superblock_t *sb,
1041 uint32_t bg_index)
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
1049 if (bg_index < block_group_count - 1) {
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
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 */
1068uint32_t ext4_filesystem_bg_get_backup_blocks(ext4_block_group_ref_t *bg)
1069{
1070 return ext4_superblock_get_group_backup_blocks(bg->fs->superblock,
1071 bg->index);
1072}
1073
1074/** Put reference to block group.
1075 *
1076 * @param ref Pointer for reference to be put back
1077 *
1078 * @return Error code
1079 *
1080 */
1081errno_t ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
1082{
1083 /* Check if reference modified */
1084 if (ref->dirty) {
1085 /* Compute new checksum of block group */
1086 uint16_t checksum =
1087 ext4_filesystem_bg_checksum(ref->fs->superblock, ref->index,
1088 ref->block_group);
1089 ext4_block_group_set_checksum(ref->block_group, checksum);
1090
1091 /* Mark block dirty for writing changes to physical device */
1092 ref->block->dirty = true;
1093 }
1094
1095 /* Put back block, that contains block group descriptor */
1096 errno_t rc = block_put(ref->block);
1097 free(ref);
1098
1099 return rc;
1100}
1101
1102/** Get reference to i-node specified by index.
1103 *
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 *
1110 */
1111errno_t ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
1112 ext4_inode_ref_t **ref)
1113{
1114 /* Allocate memory for new structure */
1115 ext4_inode_ref_t *newref =
1116 malloc(sizeof(ext4_inode_ref_t));
1117 if (newref == NULL)
1118 return ENOMEM;
1119
1120 /* Compute number of i-nodes, that fits in one data block */
1121 uint32_t inodes_per_group =
1122 ext4_superblock_get_inodes_per_group(fs->superblock);
1123
1124 /*
1125 * Inode numbers are 1-based, but it is simpler to work with 0-based
1126 * when computing indices
1127 */
1128 index -= 1;
1129 uint32_t block_group = index / inodes_per_group;
1130 uint32_t offset_in_group = index % inodes_per_group;
1131
1132 /* Load block group, where i-node is located */
1133 ext4_block_group_ref_t *bg_ref;
1134 errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
1135 if (rc != EOK) {
1136 free(newref);
1137 return rc;
1138 }
1139
1140 /* Load block address, where i-node table is located */
1141 uint32_t inode_table_start =
1142 ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
1143 fs->superblock);
1144
1145 /* Put back block group reference (not needed more) */
1146 rc = ext4_filesystem_put_block_group_ref(bg_ref);
1147 if (rc != EOK) {
1148 free(newref);
1149 return rc;
1150 }
1151
1152 /* Compute position of i-node in the block group */
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;
1156
1157 /* Compute block address */
1158 aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
1159 rc = block_get(&newref->block, fs->device, block_id, 0);
1160 if (rc != EOK) {
1161 free(newref);
1162 return rc;
1163 }
1164
1165 /* Compute position of i-node in the data block */
1166 uint32_t offset_in_block = byte_offset_in_group % block_size;
1167 newref->inode = newref->block->data + offset_in_block;
1168
1169 /* We need to store the original value of index in the reference */
1170 newref->index = index + 1;
1171 newref->fs = fs;
1172 newref->dirty = false;
1173
1174 *ref = newref;
1175
1176 return EOK;
1177}
1178
1179/** Put reference to i-node.
1180 *
1181 * @param ref Pointer for reference to be put back
1182 *
1183 * @return Error code
1184 *
1185 */
1186errno_t ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
1187{
1188 /* Check if reference modified */
1189 if (ref->dirty) {
1190 /* Mark block dirty for writing changes to physical device */
1191 ref->block->dirty = true;
1192 }
1193
1194 /* Put back block, that contains i-node */
1195 errno_t rc = block_put(ref->block);
1196 free(ref);
1197
1198 return rc;
1199}
1200
1201/** Initialize newly allocated i-node in the filesystem.
1202 *
1203 * @param fs Filesystem to initialize i-node on
1204 * @param index I-node index
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 *
1210 */
1211static errno_t ext4_filesystem_init_inode(ext4_filesystem_t *fs, uint32_t index,
1212 ext4_inode_ref_t **inode_ref, int flags)
1213{
1214 /* Check if newly allocated i-node will be a directory */
1215 bool is_dir = false;
1216 if (flags & L_DIRECTORY)
1217 is_dir = true;
1218
1219 /* Load i-node from on-disk i-node table */
1220 errno_t rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
1221 if (rc != EOK) {
1222 ext4_ialloc_free_inode(fs, index, is_dir);
1223 return rc;
1224 }
1225
1226 /* Initialize i-node */
1227 ext4_inode_t *inode = (*inode_ref)->inode;
1228
1229 uint16_t mode;
1230 if (is_dir) {
1231 /*
1232 * Default directory permissions to be compatible with other systems
1233 * 0777 (octal) == rwxrwxrwx
1234 */
1235
1236 mode = 0777;
1237 mode |= EXT4_INODE_MODE_DIRECTORY;
1238 ext4_inode_set_mode(fs->superblock, inode, mode);
1239 ext4_inode_set_links_count(inode, 1); /* '.' entry */
1240 } else {
1241 /*
1242 * Default file permissions to be compatible with other systems
1243 * 0666 (octal) == rw-rw-rw-
1244 */
1245
1246 mode = 0666;
1247 mode |= EXT4_INODE_MODE_FILE;
1248 ext4_inode_set_mode(fs->superblock, inode, mode);
1249 ext4_inode_set_links_count(inode, 0);
1250 }
1251
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);
1262
1263 /* Reset blocks array */
1264 for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
1265 inode->blocks[i] = 0;
1266
1267 /* Initialize extents if needed */
1268 if (ext4_superblock_has_feature_incompatible(
1269 fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1270 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
1271
1272 /* Initialize extent root header */
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);
1278
1279 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
1280 sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
1281
1282 ext4_extent_header_set_max_entries_count(header, max_entries);
1283 }
1284
1285 (*inode_ref)->dirty = true;
1286
1287 return EOK;
1288}
1289
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
1354/** Release i-node and mark it as free.
1355 *
1356 * @param inode_ref I-node to be released
1357 *
1358 * @return Error code
1359 *
1360 */
1361errno_t ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
1362{
1363 ext4_filesystem_t *fs = inode_ref->fs;
1364
1365 /* For extents must be data block destroyed by other way */
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))) {
1369 /* Data structures are released during truncate operation... */
1370 goto finish;
1371 }
1372
1373 /* Release all indirect (no data) blocks */
1374
1375 /* 1) Single indirect */
1376 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
1377 if (fblock != 0) {
1378 errno_t rc = ext4_balloc_free_block(inode_ref, fblock);
1379 if (rc != EOK)
1380 return rc;
1381
1382 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
1383 }
1384
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);
1388
1389 /* 2) Double indirect */
1390 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
1391 if (fblock != 0) {
1392 errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
1393 if (rc != EOK)
1394 return rc;
1395
1396 uint32_t ind_block;
1397 for (uint32_t offset = 0; offset < count; ++offset) {
1398 ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
1399
1400 if (ind_block != 0) {
1401 rc = ext4_balloc_free_block(inode_ref, ind_block);
1402 if (rc != EOK) {
1403 block_put(block);
1404 return rc;
1405 }
1406 }
1407 }
1408
1409 rc = block_put(block);
1410 if (rc != EOK)
1411 return rc;
1412
1413 rc = ext4_balloc_free_block(inode_ref, fblock);
1414 if (rc != EOK)
1415 return rc;
1416
1417 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
1418 }
1419
1420 /* 3) Tripple indirect */
1421 block_t *subblock;
1422 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
1423 if (fblock != 0) {
1424 errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
1425 if (rc != EOK)
1426 return rc;
1427
1428 uint32_t ind_block;
1429 for (uint32_t offset = 0; offset < count; ++offset) {
1430 ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
1431
1432 if (ind_block != 0) {
1433 rc = block_get(&subblock, fs->device, ind_block,
1434 BLOCK_FLAGS_NONE);
1435 if (rc != EOK) {
1436 block_put(block);
1437 return rc;
1438 }
1439
1440 uint32_t ind_subblock;
1441 for (uint32_t suboffset = 0; suboffset < count;
1442 ++suboffset) {
1443 ind_subblock = uint32_t_le2host(((uint32_t *)
1444 subblock->data)[suboffset]);
1445
1446 if (ind_subblock != 0) {
1447 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
1448 if (rc != EOK) {
1449 block_put(subblock);
1450 block_put(block);
1451 return rc;
1452 }
1453 }
1454 }
1455
1456 rc = block_put(subblock);
1457 if (rc != EOK) {
1458 block_put(block);
1459 return rc;
1460 }
1461 }
1462
1463 rc = ext4_balloc_free_block(inode_ref, ind_block);
1464 if (rc != EOK) {
1465 block_put(block);
1466 return rc;
1467 }
1468 }
1469
1470 rc = block_put(block);
1471 if (rc != EOK)
1472 return rc;
1473
1474 rc = ext4_balloc_free_block(inode_ref, fblock);
1475 if (rc != EOK)
1476 return rc;
1477
1478 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
1479 }
1480
1481finish:
1482 /* Mark inode dirty for writing to the physical device */
1483 inode_ref->dirty = true;
1484
1485 /* Free block with extended attributes if present */
1486 uint32_t xattr_block = ext4_inode_get_file_acl(
1487 inode_ref->inode, fs->superblock);
1488 if (xattr_block) {
1489 errno_t rc = ext4_balloc_free_block(inode_ref, xattr_block);
1490 if (rc != EOK)
1491 return rc;
1492
1493 ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
1494 }
1495
1496 /* Free inode by allocator */
1497 errno_t rc;
1498 if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
1499 EXT4_INODE_MODE_DIRECTORY))
1500 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
1501 else
1502 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
1503
1504 return rc;
1505}
1506
1507/** Truncate i-node data blocks.
1508 *
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 *
1514 */
1515errno_t ext4_filesystem_truncate_inode(ext4_inode_ref_t *inode_ref,
1516 aoff64_t new_size)
1517{
1518 ext4_superblock_t *sb = inode_ref->fs->superblock;
1519
1520 /* Check flags, if i-node can be truncated */
1521 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
1522 return EINVAL;
1523
1524 /* If sizes are equal, nothing has to be done. */
1525 aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
1526 if (old_size == new_size)
1527 return EOK;
1528
1529 /* It's not suppported to make the larger file by truncate operation */
1530 if (old_size < new_size)
1531 return EINVAL;
1532
1533 /* Compute how many blocks will be released */
1534 aoff64_t size_diff = old_size - new_size;
1535 uint32_t block_size = ext4_superblock_get_block_size(sb);
1536 uint32_t diff_blocks_count = size_diff / block_size;
1537 if (size_diff % block_size != 0)
1538 diff_blocks_count++;
1539
1540 uint32_t old_blocks_count = old_size / block_size;
1541 if (old_size % block_size != 0)
1542 old_blocks_count++;
1543
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))) {
1547 /* Extents require special operation */
1548 errno_t rc = ext4_extent_release_blocks_from(inode_ref,
1549 old_blocks_count - diff_blocks_count);
1550 if (rc != EOK)
1551 return rc;
1552 } else {
1553 /* Release data blocks from the end of file */
1554
1555 /* Starting from 1 because of logical blocks are numbered from 0 */
1556 for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
1557 errno_t rc = ext4_filesystem_release_inode_block(inode_ref,
1558 old_blocks_count - i);
1559 if (rc != EOK)
1560 return rc;
1561 }
1562 }
1563
1564 /* Update i-node */
1565 ext4_inode_set_size(inode_ref->inode, new_size);
1566 inode_ref->dirty = true;
1567
1568 return EOK;
1569}
1570
1571/** Get physical block address by logical index of the block.
1572 *
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 *
1579 */
1580errno_t ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
1581 aoff64_t iblock, uint32_t *fblock)
1582{
1583 ext4_filesystem_t *fs = inode_ref->fs;
1584
1585 /* For empty file is situation simple */
1586 if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
1587 *fblock = 0;
1588 return EOK;
1589 }
1590
1591 uint32_t current_block;
1592
1593 /* Handle i-node using extents */
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))) {
1597 errno_t rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
1598 if (rc != EOK)
1599 return rc;
1600
1601 *fblock = current_block;
1602 return EOK;
1603 }
1604
1605 ext4_inode_t *inode = inode_ref->inode;
1606
1607 /* Direct block are read directly from array in i-node structure */
1608 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1609 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
1610 *fblock = current_block;
1611 return EOK;
1612 }
1613
1614 /* Determine indirection level of the target block */
1615 unsigned int level = 0;
1616 for (unsigned int i = 1; i < 4; i++) {
1617 if (iblock < fs->inode_block_limits[i]) {
1618 level = i;
1619 break;
1620 }
1621 }
1622
1623 if (level == 0)
1624 return EIO;
1625
1626 /* Compute offsets for the topmost level */
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];
1632
1633 /* Sparse file */
1634 if (current_block == 0) {
1635 *fblock = 0;
1636 return EOK;
1637 }
1638
1639 block_t *block;
1640
1641 /*
1642 * Navigate through other levels, until we find the block number
1643 * or find null reference meaning we are dealing with sparse file
1644 */
1645 while (level > 0) {
1646 /* Load indirect block */
1647 errno_t rc = block_get(&block, fs->device, current_block, 0);
1648 if (rc != EOK)
1649 return rc;
1650
1651 /* Read block address from indirect block */
1652 current_block =
1653 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1654
1655 /* Put back indirect block untouched */
1656 rc = block_put(block);
1657 if (rc != EOK)
1658 return rc;
1659
1660 /* Check for sparse file */
1661 if (current_block == 0) {
1662 *fblock = 0;
1663 return EOK;
1664 }
1665
1666 /* Jump to the next level */
1667 level--;
1668
1669 /* Termination condition - we have address of data block loaded */
1670 if (level == 0)
1671 break;
1672
1673 /* Visit the next level */
1674 block_offset_in_level %= fs->inode_blocks_per_level[level];
1675 offset_in_block =
1676 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1677 }
1678
1679 *fblock = current_block;
1680
1681 return EOK;
1682}
1683
1684/** Set physical block address for the block logical address into the i-node.
1685 *
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 *
1692 */
1693errno_t ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
1694 aoff64_t iblock, uint32_t fblock)
1695{
1696 ext4_filesystem_t *fs = inode_ref->fs;
1697
1698 /* Handle inode using extents */
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 */
1703 return ENOTSUP;
1704 }
1705
1706 /* Handle simple case when we are dealing with direct reference */
1707 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1708 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
1709 inode_ref->dirty = true;
1710
1711 return EOK;
1712 }
1713
1714 /* Determine the indirection level needed to get the desired block */
1715 unsigned int level = 0;
1716 for (unsigned int i = 1; i < 4; i++) {
1717 if (iblock < fs->inode_block_limits[i]) {
1718 level = i;
1719 break;
1720 }
1721 }
1722
1723 if (level == 0)
1724 return EIO;
1725
1726 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1727
1728 /* Compute offsets for the topmost level */
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];
1735
1736 uint32_t new_block_addr;
1737 block_t *block;
1738 block_t *new_block;
1739
1740 /* Is needed to allocate indirect block on the i-node level */
1741 if (current_block == 0) {
1742 /* Allocate new indirect block */
1743 errno_t rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1744 if (rc != EOK)
1745 return rc;
1746
1747 /* Update i-node */
1748 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1749 new_block_addr);
1750 inode_ref->dirty = true;
1751
1752 /* Load newly allocated block */
1753 rc = block_get(&new_block, fs->device, new_block_addr,
1754 BLOCK_FLAGS_NOREAD);
1755 if (rc != EOK) {
1756 ext4_balloc_free_block(inode_ref, new_block_addr);
1757 return rc;
1758 }
1759
1760 /* Initialize new block */
1761 memset(new_block->data, 0, block_size);
1762 new_block->dirty = true;
1763
1764 /* Put back the allocated block */
1765 rc = block_put(new_block);
1766 if (rc != EOK)
1767 return rc;
1768
1769 current_block = new_block_addr;
1770 }
1771
1772 /*
1773 * Navigate through other levels, until we find the block number
1774 * or find null reference meaning we are dealing with sparse file
1775 */
1776 while (level > 0) {
1777 errno_t rc = block_get(&block, fs->device, current_block, 0);
1778 if (rc != EOK)
1779 return rc;
1780
1781 current_block =
1782 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1783
1784 if ((level > 1) && (current_block == 0)) {
1785 /* Allocate new block */
1786 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1787 if (rc != EOK) {
1788 block_put(block);
1789 return rc;
1790 }
1791
1792 /* Load newly allocated block */
1793 rc = block_get(&new_block, fs->device, new_block_addr,
1794 BLOCK_FLAGS_NOREAD);
1795 if (rc != EOK) {
1796 block_put(block);
1797 return rc;
1798 }
1799
1800 /* Initialize allocated block */
1801 memset(new_block->data, 0, block_size);
1802 new_block->dirty = true;
1803
1804 rc = block_put(new_block);
1805 if (rc != EOK) {
1806 block_put(block);
1807 return rc;
1808 }
1809
1810 /* Write block address to the parent */
1811 ((uint32_t *) block->data)[offset_in_block] =
1812 host2uint32_t_le(new_block_addr);
1813 block->dirty = true;
1814 current_block = new_block_addr;
1815 }
1816
1817 /* Will be finished, write the fblock address */
1818 if (level == 1) {
1819 ((uint32_t *) block->data)[offset_in_block] =
1820 host2uint32_t_le(fblock);
1821 block->dirty = true;
1822 }
1823
1824 rc = block_put(block);
1825 if (rc != EOK)
1826 return rc;
1827
1828 level--;
1829
1830 /*
1831 * If we are on the last level, break here as
1832 * there is no next level to visit
1833 */
1834 if (level == 0)
1835 break;
1836
1837 /* Visit the next level */
1838 block_offset_in_level %= fs->inode_blocks_per_level[level];
1839 offset_in_block =
1840 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1841 }
1842
1843 return EOK;
1844}
1845
1846/** Release data block from i-node
1847 *
1848 * @param inode_ref I-node to release block from
1849 * @param iblock Logical block to be released
1850 *
1851 * @return Error code
1852 *
1853 */
1854errno_t ext4_filesystem_release_inode_block(ext4_inode_ref_t *inode_ref,
1855 uint32_t iblock)
1856{
1857 uint32_t fblock;
1858
1859 ext4_filesystem_t *fs = inode_ref->fs;
1860
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))));
1865
1866 ext4_inode_t *inode = inode_ref->inode;
1867
1868 /* Handle simple case when we are dealing with direct reference */
1869 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1870 fblock = ext4_inode_get_direct_block(inode, iblock);
1871
1872 /* Sparse file */
1873 if (fblock == 0)
1874 return EOK;
1875
1876 ext4_inode_set_direct_block(inode, iblock, 0);
1877 return ext4_balloc_free_block(inode_ref, fblock);
1878 }
1879
1880 /* Determine the indirection level needed to get the desired block */
1881 unsigned int level = 0;
1882 for (unsigned int i = 1; i < 4; i++) {
1883 if (iblock < fs->inode_block_limits[i]) {
1884 level = i;
1885 break;
1886 }
1887 }
1888
1889 if (level == 0)
1890 return EIO;
1891
1892 /* Compute offsets for the topmost level */
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];
1899
1900 /*
1901 * Navigate through other levels, until we find the block number
1902 * or find null reference meaning we are dealing with sparse file
1903 */
1904 block_t *block;
1905 while (level > 0) {
1906
1907 /* Sparse check */
1908 if (current_block == 0)
1909 return EOK;
1910
1911 errno_t rc = block_get(&block, fs->device, current_block, 0);
1912 if (rc != EOK)
1913 return rc;
1914
1915 current_block =
1916 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1917
1918 /* Set zero if physical data block address found */
1919 if (level == 1) {
1920 ((uint32_t *) block->data)[offset_in_block] =
1921 host2uint32_t_le(0);
1922 block->dirty = true;
1923 }
1924
1925 rc = block_put(block);
1926 if (rc != EOK)
1927 return rc;
1928
1929 level--;
1930
1931 /*
1932 * If we are on the last level, break here as
1933 * there is no next level to visit
1934 */
1935 if (level == 0)
1936 break;
1937
1938 /* Visit the next level */
1939 block_offset_in_level %= fs->inode_blocks_per_level[level];
1940 offset_in_block =
1941 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1942 }
1943
1944 fblock = current_block;
1945 if (fblock == 0)
1946 return EOK;
1947
1948 /* Physical block is not referenced, it can be released */
1949 return ext4_balloc_free_block(inode_ref, fblock);
1950}
1951
1952/** Append following logical block to the i-node.
1953 *
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 *
1960 */
1961errno_t ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
1962 uint32_t *fblock, uint32_t *iblock)
1963{
1964 /* Handle extents separately */
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)))
1968 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1969
1970 ext4_superblock_t *sb = inode_ref->fs->superblock;
1971
1972 /* Compute next block index and allocate data block */
1973 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1974 uint32_t block_size = ext4_superblock_get_block_size(sb);
1975
1976 /* Align size i-node size */
1977 if ((inode_size % block_size) != 0)
1978 inode_size += block_size - (inode_size % block_size);
1979
1980 /* Logical blocks are numbered from 0 */
1981 uint32_t new_block_idx = inode_size / block_size;
1982
1983 /* Allocate new physical block */
1984 uint32_t phys_block;
1985 errno_t rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1986 if (rc != EOK)
1987 return rc;
1988
1989 /* Add physical block address to the i-node */
1990 rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
1991 new_block_idx, phys_block);
1992 if (rc != EOK) {
1993 ext4_balloc_free_block(inode_ref, phys_block);
1994 return rc;
1995 }
1996
1997 /* Update i-node */
1998 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1999 inode_ref->dirty = true;
2000
2001 *fblock = phys_block;
2002 *iblock = new_block_idx;
2003
2004 return EOK;
2005}
2006
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
2020/**
2021 * @}
2022 */
Note: See TracBrowser for help on using the repository browser.