2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * - The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /** @addtogroup lwext4
38 * @brief More complex filesystem functions.
41 #include <ext4_config.h>
42 #include <ext4_types.h>
44 #include <ext4_errno.h>
45 #include <ext4_blockdev.h>
46 #include <ext4_super.h>
47 #include <ext4_debug.h>
48 #include <ext4_block_group.h>
49 #include <ext4_balloc.h>
50 #include <ext4_bitmap.h>
51 #include <ext4_inode.h>
52 #include <ext4_ialloc.h>
53 #include <ext4_extent.h>
56 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
61 bool read_only = false;
63 ext4_assert(fs && bdev);
67 r = ext4_sb_read(fs->bdev, &fs->sb);
71 if(!ext4_sb_check(&fs->sb))
74 bsize = ext4_sb_get_block_size(&fs->sb);
75 if (bsize > EXT4_MAX_BLOCK_SIZE)
78 r = ext4_fs_check_features(fs, &read_only);
85 /* Compute limits for indirect block levels */
86 uint32_t blocks_id = bsize / sizeof(uint32_t);
88 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
89 fs->inode_blocks_per_level[0] = 1;
91 for (i = 1; i < 4; i++) {
92 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
94 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
95 fs->inode_blocks_per_level[i];
99 tmp = ext4_get16(&fs->sb, state);
100 if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) {
101 ext4_dprintf(EXT4_DEBUG_FS,
102 "Filesystem was not cleanly unmounted before \n");
105 /* Mark system as mounted */
106 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
107 r = ext4_sb_write(fs->bdev, &fs->sb);
112 /*Update mount count*/
113 ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
119 int ext4_fs_fini(struct ext4_fs *fs)
123 /*Set superblock state*/
124 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
126 return ext4_sb_write(fs->bdev, &fs->sb);
129 static void ext4_fs_debug_features_incomp(uint32_t features_incompatible)
132 if(features_incompatible &
133 EXT4_FEATURE_INCOMPAT_COMPRESSION){
134 ext4_dprintf(EXT4_DEBUG_FS, "COMPRESSION\n");
136 if(features_incompatible &
137 EXT4_FEATURE_INCOMPAT_FILETYPE){
138 ext4_dprintf(EXT4_DEBUG_FS, "FILETYPE\n");
140 if(features_incompatible &
141 EXT4_FEATURE_INCOMPAT_RECOVER){
142 ext4_dprintf(EXT4_DEBUG_FS, "RECOVER\n");
144 if(features_incompatible &
145 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV){
146 ext4_dprintf(EXT4_DEBUG_FS,"JOURNAL_DEV\n");
148 if(features_incompatible &
149 EXT4_FEATURE_INCOMPAT_META_BG){
150 ext4_dprintf(EXT4_DEBUG_FS, "META_BG\n");
152 if(features_incompatible &
153 EXT4_FEATURE_INCOMPAT_EXTENTS){
154 ext4_dprintf(EXT4_DEBUG_FS, "EXTENTS\n");
156 if(features_incompatible &
157 EXT4_FEATURE_INCOMPAT_64BIT){
158 ext4_dprintf(EXT4_DEBUG_FS, "64BIT\n");
160 if(features_incompatible &
161 EXT4_FEATURE_INCOMPAT_MMP){
162 ext4_dprintf(EXT4_DEBUG_FS, "MMP\n");
164 if(features_incompatible &
165 EXT4_FEATURE_INCOMPAT_FLEX_BG){
166 ext4_dprintf(EXT4_DEBUG_FS, "FLEX_BG\n");
168 if(features_incompatible &
169 EXT4_FEATURE_INCOMPAT_EA_INODE){
170 ext4_dprintf(EXT4_DEBUG_FS, "EA_INODE\n");
172 if(features_incompatible &
173 EXT4_FEATURE_INCOMPAT_DIRDATA){
174 ext4_dprintf(EXT4_DEBUG_FS, "DIRDATA\n");
177 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
179 if(features_compatible &
180 EXT4_FEATURE_COMPAT_DIR_PREALLOC){
181 ext4_dprintf(EXT4_DEBUG_FS, "DIR_PREALLOC\n");
183 if(features_compatible &
184 EXT4_FEATURE_COMPAT_IMAGIC_INODES){
185 ext4_dprintf(EXT4_DEBUG_FS, "IMAGIC_INODES\n");
187 if(features_compatible &
188 EXT4_FEATURE_COMPAT_HAS_JOURNAL){
189 ext4_dprintf(EXT4_DEBUG_FS, "HAS_JOURNAL\n");
191 if(features_compatible &
192 EXT4_FEATURE_COMPAT_EXT_ATTR){
193 ext4_dprintf(EXT4_DEBUG_FS, "EXT_ATTR\n");
195 if(features_compatible &
196 EXT4_FEATURE_COMPAT_RESIZE_INODE){
197 ext4_dprintf(EXT4_DEBUG_FS, "RESIZE_INODE\n");
199 if(features_compatible &
200 EXT4_FEATURE_COMPAT_DIR_INDEX){
201 ext4_dprintf(EXT4_DEBUG_FS, "DIR_INDEX\n");
205 static void ext4_fs_debug_features_ro(uint32_t features_ro)
208 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER){
209 ext4_dprintf(EXT4_DEBUG_FS, "SPARSE_SUPER\n");
212 EXT4_FEATURE_RO_COMPAT_LARGE_FILE){
213 ext4_dprintf(EXT4_DEBUG_FS, "LARGE_FILE\n");
216 EXT4_FEATURE_RO_COMPAT_BTREE_DIR){
217 ext4_dprintf(EXT4_DEBUG_FS, "BTREE_DIR\n");
220 EXT4_FEATURE_RO_COMPAT_HUGE_FILE){
221 ext4_dprintf(EXT4_DEBUG_FS, "HUGE_FILE\n");
224 EXT4_FEATURE_RO_COMPAT_GDT_CSUM){
225 ext4_dprintf(EXT4_DEBUG_FS, "GDT_CSUM\n");
228 EXT4_FEATURE_RO_COMPAT_DIR_NLINK){
229 ext4_dprintf(EXT4_DEBUG_FS, "DIR_NLINK\n");
232 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE){
233 ext4_dprintf(EXT4_DEBUG_FS, "EXTRA_ISIZE\n");
237 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
239 ext4_assert(fs && read_only);
241 if(ext4_get32(&fs->sb, rev_level) == 0){
245 ext4_dprintf(EXT4_DEBUG_FS,
246 "\nSblock rev_level: \n%d\n", (int)ext4_get32(&fs->sb, rev_level));
248 ext4_dprintf(EXT4_DEBUG_FS,
249 "\nSblock minor_rev_level: \n%d\n",
250 ext4_get32(&fs->sb, minor_rev_level));
252 ext4_dprintf(EXT4_DEBUG_FS,
253 "\nSblock features_incompatible:\n");
254 ext4_fs_debug_features_incomp(ext4_get32(&fs->sb, features_incompatible));
256 ext4_dprintf(EXT4_DEBUG_FS,
257 "\nSblock features_compatible:\n");
258 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
260 ext4_dprintf(EXT4_DEBUG_FS,
261 "\nSblock features_read_only:\n");
262 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
264 /*Check features_incompatible*/
265 v = (ext4_get32(&fs->sb, features_incompatible) &
266 (~EXT4_FEATURE_INCOMPAT_SUPP));
268 ext4_dprintf(EXT4_DEBUG_FS,
269 "\nERROR sblock features_incompatible. Unsupported:\n");
270 ext4_fs_debug_features_incomp(v);
275 /*Check features_read_only*/
276 v = (ext4_get32(&fs->sb, features_read_only) &
277 (~EXT4_FEATURE_RO_COMPAT_SUPP));
279 ext4_dprintf(EXT4_DEBUG_FS,
280 "\nERROR sblock features_read_only . Unsupported:\n");
281 ext4_fs_debug_features_incomp(v);
291 uint32_t ext4_fs_baddr2_index_in_group(struct ext4_sblock *s, uint32_t baddr)
294 if(ext4_get32(s, first_data_block))
297 return baddr % ext4_get32(s, blocks_per_group);
302 uint32_t ext4_fs_index_in_group2_baddr(struct ext4_sblock *s, uint32_t index,
305 if(ext4_get32(s, first_data_block))
308 return ext4_get32(s, blocks_per_group) * bgid + index;
313 /**@brief Initialize block bitmap in block group.
314 * @param bg_ref Reference to block group
317 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
320 uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap(
321 bg_ref->block_group, &bg_ref->fs->sb);
323 struct ext4_block block_bitmap;
324 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
330 memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
332 /* Determine first block and first data block in group */
333 uint32_t first_idx = 0;
335 uint32_t first_data = ext4_balloc_get_first_data_block_in_group(
336 &bg_ref->fs->sb, bg_ref);
337 uint32_t first_data_idx = ext4_fs_baddr2_index_in_group(
338 &bg_ref->fs->sb, first_data);
340 /*Set bits from to first block to first data block - 1 to one (allocated)*/
341 /*TODO: Optimize it*/
342 for (i = first_idx; i < first_data_idx; ++i)
343 ext4_bmap_bit_set(block_bitmap.data, i);
346 block_bitmap.dirty = true;
349 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
352 /**@brief Initialize i-node bitmap in block group.
353 * @param bg_ref Reference to block group
356 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
359 uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
360 bg_ref->block_group, &bg_ref->fs->sb);
362 struct ext4_block block_bitmap;
363 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
368 /* Initialize all bitmap bits to zero */
369 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
370 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
372 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
374 uint32_t start_bit = inodes_per_group;
375 uint32_t end_bit = block_size * 8;
378 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
379 ext4_bmap_bit_set(block_bitmap.data, i);
382 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
384 block_bitmap.dirty = true;
387 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
390 /**@brief Initialize i-node table in block group.
391 * @param bg_ref Reference to block group
394 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
396 struct ext4_sblock *sb = &bg_ref->fs->sb;
398 uint32_t inode_size = ext4_get32(sb, inode_size);
399 uint32_t block_size = ext4_sb_get_block_size(sb);
400 uint32_t inodes_per_block = block_size / inode_size;
401 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
402 uint32_t table_blocks = inodes_in_group / inodes_per_block;
405 if (inodes_in_group % inodes_per_block)
408 /* Compute initialization bounds */
409 uint32_t first_block = ext4_bg_get_inode_table_first_block(
410 bg_ref->block_group, sb);
412 uint32_t last_block = first_block + table_blocks - 1;
414 /* Initialization of all itable blocks */
415 for (fblock = first_block; fblock <= last_block; ++fblock) {
417 struct ext4_block block;
418 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
422 memset(block.data, 0, block_size);
425 ext4_block_set(bg_ref->fs->bdev, &block);
434 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
435 struct ext4_block_group_ref *ref)
437 /* Compute number of descriptors, that fits in one data block */
438 uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) /
439 ext4_sb_get_desc_size(&fs->sb);
441 /* Block group descriptor table starts at the next block after superblock */
442 uint64_t block_id = ext4_get32(&fs->sb, first_data_block) + 1;
444 /* Find the block containing the descriptor we are looking for */
445 block_id += bgid / dsc_per_block;
446 uint32_t offset = (bgid % dsc_per_block) *
447 ext4_sb_get_desc_size(&fs->sb);
450 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
454 ref->block_group = (void *)(ref->block.data + offset);
459 if (ext4_bg_has_flag(ref->block_group,
460 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
461 rc = ext4_fs_init_block_bitmap(ref);
463 ext4_block_set(fs->bdev, &ref->block);
466 ext4_bg_clear_flag(ref->block_group,
467 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
472 if (ext4_bg_has_flag(ref->block_group,
473 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
474 rc = ext4_fs_init_inode_bitmap(ref);
476 ext4_block_set(ref->fs->bdev, &ref->block);
480 ext4_bg_clear_flag(ref->block_group,
481 EXT4_BLOCK_GROUP_INODE_UNINIT);
483 if (!ext4_bg_has_flag(ref->block_group,
484 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
485 rc = ext4_fs_init_inode_table(ref);
487 ext4_block_set(fs->bdev, &ref->block);
491 ext4_bg_set_flag(ref->block_group,
492 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
501 /**@brief Compute checksum of block group descriptor.
502 * @param sb Superblock
503 * @param bgid Index of block group in the filesystem
504 * @param bg Block group to compute checksum for
505 * @return Checksum value
507 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
508 struct ext4_bgroup *bg)
510 /* If checksum not supported, 0 will be returned */
513 /* Compute the checksum only if the filesystem supports it */
514 if (ext4_sb_check_read_only(sb,
515 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
516 uint8_t *base = (uint8_t *)bg;
517 uint8_t *checksum = (uint8_t *)&bg->checksum;
519 uint32_t offset = (uint32_t) (checksum - base);
521 /* Convert block group index to little endian */
522 uint32_t le_group = to_le32(bgid);
525 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
527 /* Include index of block group */
528 crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group));
530 /* Compute crc from the first part (stop before checksum field) */
531 crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset);
534 offset += sizeof(bg->checksum);
536 /* Checksum of the rest of block group descriptor */
537 if ((ext4_sb_check_feature_incompatible(sb,
538 EXT4_FEATURE_INCOMPAT_64BIT)) &&
539 (offset < ext4_sb_get_desc_size(sb)))
541 crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset,
542 ext4_sb_get_desc_size(sb) - offset);
547 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
549 /* Check if reference modified */
551 /* Compute new checksum of block group */
553 ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
556 ref->block_group->checksum = to_le16(checksum);
558 /* Mark block dirty for writing changes to physical device */
559 ref->block.dirty = true;
562 /* Put back block, that contains block group descriptor */
563 return ext4_block_set(ref->fs->bdev, &ref->block);
566 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
567 struct ext4_inode_ref *ref)
569 /* Compute number of i-nodes, that fits in one data block */
570 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
573 * Inode numbers are 1-based, but it is simpler to work with 0-based
574 * when computing indices
577 uint32_t block_group = index / inodes_per_group;
578 uint32_t offset_in_group = index % inodes_per_group;
580 /* Load block group, where i-node is located */
581 struct ext4_block_group_ref bg_ref;
583 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
588 /* Load block address, where i-node table is located */
589 uint32_t inode_table_start =
590 ext4_bg_get_inode_table_first_block(bg_ref.block_group,
593 /* Put back block group reference (not needed more) */
594 rc = ext4_fs_put_block_group_ref(&bg_ref);
599 /* Compute position of i-node in the block group */
600 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
601 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
602 uint32_t byte_offset_in_group = offset_in_group * inode_size;
604 /* Compute block address */
605 uint64_t block_id = inode_table_start +
606 (byte_offset_in_group / block_size);
609 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
614 /* Compute position of i-node in the data block */
615 uint32_t offset_in_block = byte_offset_in_group % block_size;
616 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
618 /* We need to store the original value of index in the reference */
619 ref->index = index + 1;
626 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
628 /* Check if reference modified */
630 /* Mark block dirty for writing changes to physical device */
631 ref->block.dirty = true;
634 /* Put back block, that contains i-node */
635 return ext4_block_set(ref->fs->bdev, &ref->block);
638 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
641 /* Check if newly allocated i-node will be a directory */
645 is_dir = is_directory;
647 /* Allocate inode by allocation algorithm */
649 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
653 /* Load i-node from on-disk i-node table */
654 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
656 ext4_ialloc_free_inode(fs, index, is_dir);
660 /* Initialize i-node */
661 struct ext4_inode *inode = inode_ref->inode;
666 * Default directory permissions to be compatible with other systems
667 * 0777 (octal) == rwxrwxrwx
671 mode |= EXT4_INODE_MODE_DIRECTORY;
672 ext4_inode_set_mode(&fs->sb, inode, mode);
673 ext4_inode_set_links_count(inode, 1); /* '.' entry */
677 * Default file permissions to be compatible with other systems
678 * 0666 (octal) == rw-rw-rw-
682 mode |= EXT4_INODE_MODE_FILE;
683 ext4_inode_set_mode(&fs->sb, inode, mode);
684 ext4_inode_set_links_count(inode, 0);
687 ext4_inode_set_uid(inode, 0);
688 ext4_inode_set_gid(inode, 0);
689 ext4_inode_set_size(inode, 0);
690 ext4_inode_set_access_time(inode, 0);
691 ext4_inode_set_change_inode_time(inode, 0);
692 ext4_inode_set_modification_time(inode, 0);
693 ext4_inode_set_deletion_time(inode, 0);
694 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
695 ext4_inode_set_flags(inode, 0);
696 ext4_inode_set_generation(inode, 0);
698 /* Reset blocks array */
699 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
700 inode->blocks[i] = 0;
702 #if CONFIG_EXTENT_ENABLE
703 /* Initialize extents if needed */
704 if (ext4_sb_check_feature_incompatible(
705 &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
706 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
709 /* Initialize extent root header */
710 struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
711 ext4_extent_header_set_depth(header, 0);
712 ext4_extent_header_set_entries_count(header, 0);
713 ext4_extent_header_set_generation(header, 0);
714 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
716 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
717 sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent);
719 ext4_extent_header_set_max_entries_count(header, max_entries);
723 inode_ref->dirty = true;
728 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
730 struct ext4_fs *fs = inode_ref->fs;
733 #if CONFIG_EXTENT_ENABLE
734 /* For extents must be data block destroyed by other way */
735 if ((ext4_sb_check_feature_incompatible(&fs->sb,
736 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
737 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
738 /* Data structures are released during truncate operation... */
742 /* Release all indirect (no data) blocks */
744 /* 1) Single indirect */
745 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
747 int rc = ext4_balloc_free_block(inode_ref, fblock);
751 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
754 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
755 uint32_t count = block_size / sizeof(uint32_t);
757 struct ext4_block block;
759 /* 2) Double indirect */
760 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
762 int rc = ext4_block_get(fs->bdev, &block, fblock);
767 for (offset = 0; offset < count; ++offset) {
768 ind_block = to_le32(((uint32_t *) block.data)[offset]);
770 if (ind_block != 0) {
771 rc = ext4_balloc_free_block(inode_ref, ind_block);
773 ext4_block_set(fs->bdev, &block);
779 ext4_block_set(fs->bdev, &block);
780 rc = ext4_balloc_free_block(inode_ref, fblock);
784 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
787 /* 3) Tripple indirect */
788 struct ext4_block subblock;
789 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
791 int rc = ext4_block_get(fs->bdev, &block, fblock);
796 for ( offset = 0; offset < count; ++offset) {
797 ind_block = to_le32(((uint32_t *) block.data)[offset]);
799 if (ind_block != 0) {
800 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
802 ext4_block_set(fs->bdev, &block);
806 uint32_t ind_subblock;
807 for (suboffset = 0; suboffset < count;
809 ind_subblock = to_le32(((uint32_t *)
810 subblock.data)[suboffset]);
812 if (ind_subblock != 0) {
813 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
815 ext4_block_set(fs->bdev, &subblock);
816 ext4_block_set(fs->bdev, &block);
822 ext4_block_set(fs->bdev, &subblock);
825 rc = ext4_balloc_free_block(inode_ref, ind_block);
827 ext4_block_set(fs->bdev, &block);
833 ext4_block_set(fs->bdev, &block);
834 rc = ext4_balloc_free_block(inode_ref, fblock);
838 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
842 /* Mark inode dirty for writing to the physical device */
843 inode_ref->dirty = true;
845 /* Free block with extended attributes if present */
846 uint32_t xattr_block = ext4_inode_get_file_acl(
847 inode_ref->inode, &fs->sb);
849 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
853 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
856 /* Free inode by allocator */
858 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
859 EXT4_INODE_MODE_DIRECTORY))
860 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
862 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
867 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
870 struct ext4_sblock *sb = &inode_ref->fs->sb;
873 /* Check flags, if i-node can be truncated */
874 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
877 /* If sizes are equal, nothing has to be done. */
878 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
879 if (old_size == new_size)
882 /* It's not suppported to make the larger file by truncate operation */
883 if (old_size < new_size)
886 /* Compute how many blocks will be released */
887 uint64_t size_diff = old_size - new_size;
888 uint32_t block_size = ext4_sb_get_block_size(sb);
889 uint32_t diff_blocks_count = size_diff / block_size;
890 if (size_diff % block_size != 0)
893 uint32_t old_blocks_count = old_size / block_size;
894 if (old_size % block_size != 0)
896 #if CONFIG_EXTENT_ENABLE
897 if ((ext4_sb_check_feature_incompatible(sb,
898 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
899 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
901 /* Extents require special operation */
902 int rc = ext4_extent_release_blocks_from(inode_ref,
903 old_blocks_count - diff_blocks_count);
910 /* Release data blocks from the end of file */
912 /* Starting from 1 because of logical blocks are numbered from 0 */
913 for (i = 1; i <= diff_blocks_count; ++i) {
914 int rc = ext4_fs_release_inode_block(inode_ref,
915 old_blocks_count - i);
922 ext4_inode_set_size(inode_ref->inode, new_size);
923 inode_ref->dirty = true;
928 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
929 uint64_t iblock, uint32_t *fblock)
931 struct ext4_fs *fs = inode_ref->fs;
933 /* For empty file is situation simple */
934 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
939 uint32_t current_block;
940 #if CONFIG_EXTENT_ENABLE
941 /* Handle i-node using extents */
942 if ((ext4_sb_check_feature_incompatible(&fs->sb,
943 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
944 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
947 int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
951 *fblock = current_block;
956 struct ext4_inode *inode = inode_ref->inode;
958 /* Direct block are read directly from array in i-node structure */
959 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
960 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
961 *fblock = current_block;
965 /* Determine indirection level of the target block */
966 unsigned int level = 0;
968 for (i = 1; i < 4; i++) {
969 if (iblock < fs->inode_block_limits[i]) {
978 /* Compute offsets for the topmost level */
979 uint64_t block_offset_in_level =
980 iblock - fs->inode_block_limits[level - 1];
981 current_block = ext4_inode_get_indirect_block(inode, level - 1);
982 uint32_t offset_in_block =
983 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
986 if (current_block == 0) {
991 struct ext4_block block;
994 * Navigate through other levels, until we find the block number
995 * or find null reference meaning we are dealing with sparse file
998 /* Load indirect block */
999 int rc = ext4_block_get(fs->bdev, &block, current_block);
1003 /* Read block address from indirect block */
1005 to_le32(((uint32_t *) block.data)[offset_in_block]);
1007 /* Put back indirect block untouched */
1008 rc = ext4_block_set(fs->bdev, &block);
1012 /* Check for sparse file */
1013 if (current_block == 0) {
1018 /* Jump to the next level */
1021 /* Termination condition - we have address of data block loaded */
1025 /* Visit the next level */
1026 block_offset_in_level %= fs->inode_blocks_per_level[level];
1028 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1031 *fblock = current_block;
1036 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1037 uint64_t iblock, uint32_t fblock)
1039 struct ext4_fs *fs = inode_ref->fs;
1041 #if CONFIG_EXTENT_ENABLE
1042 /* Handle inode using extents */
1043 if ((ext4_sb_check_feature_incompatible(&fs->sb,
1044 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1045 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1051 /* Handle simple case when we are dealing with direct reference */
1052 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1053 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1055 inode_ref->dirty = true;
1060 /* Determine the indirection level needed to get the desired block */
1061 unsigned int level = 0;
1063 for (i = 1; i < 4; i++) {
1064 if (iblock < fs->inode_block_limits[i]) {
1073 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1075 /* Compute offsets for the topmost level */
1076 uint64_t block_offset_in_level =
1077 iblock - fs->inode_block_limits[level - 1];
1078 uint32_t current_block =
1079 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1080 uint32_t offset_in_block =
1081 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1083 uint32_t new_block_addr;
1085 struct ext4_block block;
1086 struct ext4_block new_block;
1088 /* Is needed to allocate indirect block on the i-node level */
1089 if (current_block == 0) {
1090 /* Allocate new indirect block */
1091 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1096 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1098 inode_ref->dirty = true;
1100 /* Load newly allocated block */
1101 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1103 ext4_balloc_free_block(inode_ref, new_block_addr);
1107 /* Initialize new block */
1108 memset(new_block.data, 0, block_size);
1109 new_block.dirty = true;
1111 /* Put back the allocated block */
1112 rc = ext4_block_set(fs->bdev, &new_block);
1116 current_block = new_block_addr;
1120 * Navigate through other levels, until we find the block number
1121 * or find null reference meaning we are dealing with sparse file
1124 int rc = ext4_block_get(fs->bdev, &block, current_block);
1129 to_le32(((uint32_t *) block.data)[offset_in_block]);
1131 if ((level > 1) && (current_block == 0)) {
1132 /* Allocate new block */
1133 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1135 ext4_block_set(fs->bdev, &block);
1139 /* Load newly allocated block */
1140 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1143 ext4_block_set(fs->bdev, &block);
1147 /* Initialize allocated block */
1148 memset(new_block.data, 0, block_size);
1149 new_block.dirty = true;
1151 rc = ext4_block_set(fs->bdev, &new_block);
1153 ext4_block_set(fs->bdev, &block);
1157 /* Write block address to the parent */
1158 ((uint32_t *) block.data)[offset_in_block] =
1159 to_le32(new_block_addr);
1161 current_block = new_block_addr;
1164 /* Will be finished, write the fblock address */
1166 ((uint32_t *) block.data)[offset_in_block] =
1171 rc = ext4_block_set(fs->bdev, &block);
1178 * If we are on the last level, break here as
1179 * there is no next level to visit
1184 /* Visit the next level */
1185 block_offset_in_level %= fs->inode_blocks_per_level[level];
1187 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1193 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1198 struct ext4_fs *fs = inode_ref->fs;
1200 /* Extents are handled otherwise = there is not support in this function */
1201 ext4_assert(!(ext4_sb_check_feature_incompatible(&fs->sb,
1202 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1203 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1205 struct ext4_inode *inode = inode_ref->inode;
1207 /* Handle simple case when we are dealing with direct reference */
1208 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1209 fblock = ext4_inode_get_direct_block(inode, iblock);
1215 ext4_inode_set_direct_block(inode, iblock, 0);
1216 return ext4_balloc_free_block(inode_ref, fblock);
1219 /* Determine the indirection level needed to get the desired block */
1220 unsigned int level = 0;
1222 for (i = 1; i < 4; i++) {
1223 if (iblock < fs->inode_block_limits[i]) {
1232 /* Compute offsets for the topmost level */
1233 uint64_t block_offset_in_level =
1234 iblock - fs->inode_block_limits[level - 1];
1235 uint32_t current_block =
1236 ext4_inode_get_indirect_block(inode, level - 1);
1237 uint32_t offset_in_block =
1238 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1241 * Navigate through other levels, until we find the block number
1242 * or find null reference meaning we are dealing with sparse file
1244 struct ext4_block block;
1249 if (current_block == 0)
1252 int rc = ext4_block_get(fs->bdev, &block, current_block);
1257 to_le32(((uint32_t *) block.data)[offset_in_block]);
1259 /* Set zero if physical data block address found */
1261 ((uint32_t *) block.data)[offset_in_block] =
1266 rc = ext4_block_set(fs->bdev, &block);
1273 * If we are on the last level, break here as
1274 * there is no next level to visit
1279 /* Visit the next level */
1280 block_offset_in_level %= fs->inode_blocks_per_level[level];
1282 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1285 fblock = current_block;
1289 /* Physical block is not referenced, it can be released */
1290 return ext4_balloc_free_block(inode_ref, fblock);
1294 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1295 uint32_t *fblock, uint32_t *iblock)
1297 #if CONFIG_EXTENT_ENABLE
1298 /* Handle extents separately */
1299 if ((ext4_sb_check_feature_incompatible(&inode_ref->fs->sb,
1300 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1301 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1302 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1305 struct ext4_sblock *sb = &inode_ref->fs->sb;
1307 /* Compute next block index and allocate data block */
1308 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1309 uint32_t block_size = ext4_sb_get_block_size(sb);
1311 /* Align size i-node size */
1312 if ((inode_size % block_size) != 0)
1313 inode_size += block_size - (inode_size % block_size);
1315 /* Logical blocks are numbered from 0 */
1316 uint32_t new_block_idx = inode_size / block_size;
1318 /* Allocate new physical block */
1319 uint32_t phys_block;
1320 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1324 /* Add physical block address to the i-node */
1325 rc = ext4_fs_set_inode_data_block_index(inode_ref,
1326 new_block_idx, phys_block);
1328 ext4_balloc_free_block(inode_ref, phys_block);
1333 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1334 inode_ref->dirty = true;
1336 *fblock = phys_block;
1337 *iblock = new_block_idx;