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;
314 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
317 uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap(
318 bg_ref->block_group, &bg_ref->fs->sb);
320 struct ext4_block block_bitmap;
321 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
327 memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
329 /* Determine first block and first data block in group */
330 uint32_t first_idx = 0;
332 uint32_t first_data = ext4_balloc_get_first_data_block_in_group(
333 &bg_ref->fs->sb, bg_ref);
334 uint32_t first_data_idx = ext4_fs_baddr2_index_in_group(
335 &bg_ref->fs->sb, first_data);
337 /*Set bits from to first block to first data block - 1 to one (allocated)*/
338 /*TODO: Optimize it*/
339 for (i = first_idx; i < first_data_idx; ++i)
340 ext4_bmap_bit_set(block_bitmap.data, i);
343 block_bitmap.dirty = true;
346 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
349 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
352 uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
353 bg_ref->block_group, &bg_ref->fs->sb);
355 struct ext4_block block_bitmap;
356 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
361 /* Initialize all bitmap bits to zero */
362 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
363 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
365 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
367 uint32_t start_bit = inodes_per_group;
368 uint32_t end_bit = block_size * 8;
371 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
372 ext4_bmap_bit_set(block_bitmap.data, i);
375 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
377 block_bitmap.dirty = true;
380 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
383 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
385 struct ext4_sblock *sb = &bg_ref->fs->sb;
387 uint32_t inode_size = ext4_get32(sb, inode_size);
388 uint32_t block_size = ext4_sb_get_block_size(sb);
389 uint32_t inodes_per_block = block_size / inode_size;
390 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
391 uint32_t table_blocks = inodes_in_group / inodes_per_block;
394 if (inodes_in_group % inodes_per_block)
397 /* Compute initialization bounds */
398 uint32_t first_block = ext4_bg_get_inode_table_first_block(
399 bg_ref->block_group, sb);
401 uint32_t last_block = first_block + table_blocks - 1;
403 /* Initialization of all itable blocks */
404 for (fblock = first_block; fblock <= last_block; ++fblock) {
406 struct ext4_block block;
407 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
411 memset(block.data, 0, block_size);
414 ext4_block_set(bg_ref->fs->bdev, &block);
423 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
424 struct ext4_block_group_ref *ref)
426 /* Compute number of descriptors, that fits in one data block */
427 uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) /
428 ext4_sb_get_desc_size(&fs->sb);
430 /* Block group descriptor table starts at the next block after superblock */
431 uint64_t block_id = ext4_get32(&fs->sb, first_data_block) + 1;
433 /* Find the block containing the descriptor we are looking for */
434 block_id += bgid / dsc_per_block;
435 uint32_t offset = (bgid % dsc_per_block) *
436 ext4_sb_get_desc_size(&fs->sb);
439 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
443 ref->block_group = (void *)(ref->block.data + offset);
448 if (ext4_bg_has_flag(ref->block_group,
449 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
450 rc = ext4_fs_init_block_bitmap(ref);
452 ext4_block_set(fs->bdev, &ref->block);
455 ext4_bg_clear_flag(ref->block_group,
456 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
461 if (ext4_bg_has_flag(ref->block_group,
462 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
463 rc = ext4_fs_init_inode_bitmap(ref);
465 ext4_block_set(ref->fs->bdev, &ref->block);
469 ext4_bg_clear_flag(ref->block_group,
470 EXT4_BLOCK_GROUP_INODE_UNINIT);
472 if (!ext4_bg_has_flag(ref->block_group,
473 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
474 rc = ext4_fs_init_inode_table(ref);
476 ext4_block_set(fs->bdev, &ref->block);
480 ext4_bg_set_flag(ref->block_group,
481 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
490 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
491 struct ext4_bgroup *bg)
493 /* If checksum not supported, 0 will be returned */
496 /* Compute the checksum only if the filesystem supports it */
497 if (ext4_sb_check_read_only(sb,
498 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
500 void *checksum = &bg->checksum;
502 uint32_t offset = (uint32_t) (checksum - base);
504 /* Convert block group index to little endian */
505 uint32_t le_group = to_le32(bgid);
508 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
510 /* Include index of block group */
511 crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group));
513 /* Compute crc from the first part (stop before checksum field) */
514 crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset);
517 offset += sizeof(bg->checksum);
519 /* Checksum of the rest of block group descriptor */
520 if ((ext4_sb_check_feature_incompatible(sb,
521 EXT4_FEATURE_INCOMPAT_64BIT)) &&
522 (offset < ext4_sb_get_desc_size(sb)))
524 crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset,
525 ext4_sb_get_desc_size(sb) - offset);
530 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
532 /* Check if reference modified */
534 /* Compute new checksum of block group */
536 ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
539 ref->block_group->checksum = to_le16(checksum);
541 /* Mark block dirty for writing changes to physical device */
542 ref->block.dirty = true;
545 /* Put back block, that contains block group descriptor */
546 return ext4_block_set(ref->fs->bdev, &ref->block);
549 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
550 struct ext4_inode_ref *ref)
552 /* Compute number of i-nodes, that fits in one data block */
553 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
556 * Inode numbers are 1-based, but it is simpler to work with 0-based
557 * when computing indices
560 uint32_t block_group = index / inodes_per_group;
561 uint32_t offset_in_group = index % inodes_per_group;
563 /* Load block group, where i-node is located */
564 struct ext4_block_group_ref bg_ref;
566 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
571 /* Load block address, where i-node table is located */
572 uint32_t inode_table_start =
573 ext4_bg_get_inode_table_first_block(bg_ref.block_group,
576 /* Put back block group reference (not needed more) */
577 rc = ext4_fs_put_block_group_ref(&bg_ref);
582 /* Compute position of i-node in the block group */
583 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
584 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
585 uint32_t byte_offset_in_group = offset_in_group * inode_size;
587 /* Compute block address */
588 uint64_t block_id = inode_table_start +
589 (byte_offset_in_group / block_size);
592 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
597 /* Compute position of i-node in the data block */
598 uint32_t offset_in_block = byte_offset_in_group % block_size;
599 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
601 /* We need to store the original value of index in the reference */
602 ref->index = index + 1;
609 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
611 /* Check if reference modified */
613 /* Mark block dirty for writing changes to physical device */
614 ref->block.dirty = true;
617 /* Put back block, that contains i-node */
618 return ext4_block_set(ref->fs->bdev, &ref->block);
621 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
624 /* Check if newly allocated i-node will be a directory */
628 is_dir = is_directory;
630 /* Allocate inode by allocation algorithm */
632 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
636 /* Load i-node from on-disk i-node table */
637 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
639 ext4_ialloc_free_inode(fs, index, is_dir);
643 /* Initialize i-node */
644 struct ext4_inode *inode = inode_ref->inode;
649 * Default directory permissions to be compatible with other systems
650 * 0777 (octal) == rwxrwxrwx
654 mode |= EXT4_INODE_MODE_DIRECTORY;
655 ext4_inode_set_mode(&fs->sb, inode, mode);
656 ext4_inode_set_links_count(inode, 1); /* '.' entry */
660 * Default file permissions to be compatible with other systems
661 * 0666 (octal) == rw-rw-rw-
665 mode |= EXT4_INODE_MODE_FILE;
666 ext4_inode_set_mode(&fs->sb, inode, mode);
667 ext4_inode_set_links_count(inode, 0);
670 ext4_inode_set_uid(inode, 0);
671 ext4_inode_set_gid(inode, 0);
672 ext4_inode_set_size(inode, 0);
673 ext4_inode_set_access_time(inode, 0);
674 ext4_inode_set_change_inode_time(inode, 0);
675 ext4_inode_set_modification_time(inode, 0);
676 ext4_inode_set_deletion_time(inode, 0);
677 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
678 ext4_inode_set_flags(inode, 0);
679 ext4_inode_set_generation(inode, 0);
681 /* Reset blocks array */
682 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
683 inode->blocks[i] = 0;
685 #if CONFIG_EXTENT_ENABLE
686 /* Initialize extents if needed */
687 if (ext4_sb_check_feature_incompatible(
688 &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
689 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
692 /* Initialize extent root header */
693 struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
694 ext4_extent_header_set_depth(header, 0);
695 ext4_extent_header_set_entries_count(header, 0);
696 ext4_extent_header_set_generation(header, 0);
697 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
699 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
700 sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent);
702 ext4_extent_header_set_max_entries_count(header, max_entries);
706 inode_ref->dirty = true;
711 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
713 struct ext4_fs *fs = inode_ref->fs;
716 #if CONFIG_EXTENT_ENABLE
717 /* For extents must be data block destroyed by other way */
718 if ((ext4_sb_check_feature_incompatible(&fs->sb,
719 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
720 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
721 /* Data structures are released during truncate operation... */
725 /* Release all indirect (no data) blocks */
727 /* 1) Single indirect */
728 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
730 int rc = ext4_balloc_free_block(inode_ref, fblock);
734 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
737 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
738 uint32_t count = block_size / sizeof(uint32_t);
740 struct ext4_block block;
742 /* 2) Double indirect */
743 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
745 int rc = ext4_block_get(fs->bdev, &block, fblock);
750 for (offset = 0; offset < count; ++offset) {
751 ind_block = to_le32(((uint32_t *) block.data)[offset]);
753 if (ind_block != 0) {
754 rc = ext4_balloc_free_block(inode_ref, ind_block);
756 ext4_block_set(fs->bdev, &block);
762 ext4_block_set(fs->bdev, &block);
763 rc = ext4_balloc_free_block(inode_ref, fblock);
767 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
770 /* 3) Tripple indirect */
771 struct ext4_block subblock;
772 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
774 int rc = ext4_block_get(fs->bdev, &block, fblock);
779 for ( offset = 0; offset < count; ++offset) {
780 ind_block = to_le32(((uint32_t *) block.data)[offset]);
782 if (ind_block != 0) {
783 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
785 ext4_block_set(fs->bdev, &block);
789 uint32_t ind_subblock;
790 for (suboffset = 0; suboffset < count;
792 ind_subblock = to_le32(((uint32_t *)
793 subblock.data)[suboffset]);
795 if (ind_subblock != 0) {
796 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
798 ext4_block_set(fs->bdev, &subblock);
799 ext4_block_set(fs->bdev, &block);
805 ext4_block_set(fs->bdev, &subblock);
808 rc = ext4_balloc_free_block(inode_ref, ind_block);
810 ext4_block_set(fs->bdev, &block);
816 ext4_block_set(fs->bdev, &block);
817 rc = ext4_balloc_free_block(inode_ref, fblock);
821 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
825 /* Mark inode dirty for writing to the physical device */
826 inode_ref->dirty = true;
828 /* Free block with extended attributes if present */
829 uint32_t xattr_block = ext4_inode_get_file_acl(
830 inode_ref->inode, &fs->sb);
832 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
836 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
839 /* Free inode by allocator */
841 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
842 EXT4_INODE_MODE_DIRECTORY))
843 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
845 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
850 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
853 struct ext4_sblock *sb = &inode_ref->fs->sb;
856 /* Check flags, if i-node can be truncated */
857 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
860 /* If sizes are equal, nothing has to be done. */
861 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
862 if (old_size == new_size)
865 /* It's not suppported to make the larger file by truncate operation */
866 if (old_size < new_size)
869 /* Compute how many blocks will be released */
870 uint64_t size_diff = old_size - new_size;
871 uint32_t block_size = ext4_sb_get_block_size(sb);
872 uint32_t diff_blocks_count = size_diff / block_size;
873 if (size_diff % block_size != 0)
876 uint32_t old_blocks_count = old_size / block_size;
877 if (old_size % block_size != 0)
879 #if CONFIG_EXTENT_ENABLE
880 if ((ext4_sb_check_feature_incompatible(sb,
881 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
882 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
884 /* Extents require special operation */
885 int rc = ext4_extent_release_blocks_from(inode_ref,
886 old_blocks_count - diff_blocks_count);
893 /* Release data blocks from the end of file */
895 /* Starting from 1 because of logical blocks are numbered from 0 */
896 for (i = 1; i <= diff_blocks_count; ++i) {
897 int rc = ext4_fs_release_inode_block(inode_ref,
898 old_blocks_count - i);
905 ext4_inode_set_size(inode_ref->inode, new_size);
906 inode_ref->dirty = true;
911 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
912 uint64_t iblock, uint32_t *fblock)
914 struct ext4_fs *fs = inode_ref->fs;
916 /* For empty file is situation simple */
917 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
922 uint32_t current_block;
923 #if CONFIG_EXTENT_ENABLE
924 /* Handle i-node using extents */
925 if ((ext4_sb_check_feature_incompatible(&fs->sb,
926 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
927 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
930 int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
934 *fblock = current_block;
939 struct ext4_inode *inode = inode_ref->inode;
941 /* Direct block are read directly from array in i-node structure */
942 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
943 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
944 *fblock = current_block;
948 /* Determine indirection level of the target block */
949 unsigned int level = 0;
951 for (i = 1; i < 4; i++) {
952 if (iblock < fs->inode_block_limits[i]) {
961 /* Compute offsets for the topmost level */
962 uint64_t block_offset_in_level =
963 iblock - fs->inode_block_limits[level - 1];
964 current_block = ext4_inode_get_indirect_block(inode, level - 1);
965 uint32_t offset_in_block =
966 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
969 if (current_block == 0) {
974 struct ext4_block block;
977 * Navigate through other levels, until we find the block number
978 * or find null reference meaning we are dealing with sparse file
981 /* Load indirect block */
982 int rc = ext4_block_get(fs->bdev, &block, current_block);
986 /* Read block address from indirect block */
988 to_le32(((uint32_t *) block.data)[offset_in_block]);
990 /* Put back indirect block untouched */
991 rc = ext4_block_set(fs->bdev, &block);
995 /* Check for sparse file */
996 if (current_block == 0) {
1001 /* Jump to the next level */
1004 /* Termination condition - we have address of data block loaded */
1008 /* Visit the next level */
1009 block_offset_in_level %= fs->inode_blocks_per_level[level];
1011 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1014 *fblock = current_block;
1019 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1020 uint64_t iblock, uint32_t fblock)
1022 struct ext4_fs *fs = inode_ref->fs;
1024 #if CONFIG_EXTENT_ENABLE
1025 /* Handle inode using extents */
1026 if ((ext4_sb_check_feature_incompatible(&fs->sb,
1027 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1028 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1034 /* Handle simple case when we are dealing with direct reference */
1035 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1036 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1038 inode_ref->dirty = true;
1043 /* Determine the indirection level needed to get the desired block */
1044 unsigned int level = 0;
1046 for (i = 1; i < 4; i++) {
1047 if (iblock < fs->inode_block_limits[i]) {
1056 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1058 /* Compute offsets for the topmost level */
1059 uint64_t block_offset_in_level =
1060 iblock - fs->inode_block_limits[level - 1];
1061 uint32_t current_block =
1062 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1063 uint32_t offset_in_block =
1064 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1066 uint32_t new_block_addr;
1068 struct ext4_block block;
1069 struct ext4_block new_block;
1071 /* Is needed to allocate indirect block on the i-node level */
1072 if (current_block == 0) {
1073 /* Allocate new indirect block */
1074 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1079 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1081 inode_ref->dirty = true;
1083 /* Load newly allocated block */
1084 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1086 ext4_balloc_free_block(inode_ref, new_block_addr);
1090 /* Initialize new block */
1091 memset(new_block.data, 0, block_size);
1092 new_block.dirty = true;
1094 /* Put back the allocated block */
1095 rc = ext4_block_set(fs->bdev, &new_block);
1099 current_block = new_block_addr;
1103 * Navigate through other levels, until we find the block number
1104 * or find null reference meaning we are dealing with sparse file
1107 int rc = ext4_block_get(fs->bdev, &block, current_block);
1112 to_le32(((uint32_t *) block.data)[offset_in_block]);
1114 if ((level > 1) && (current_block == 0)) {
1115 /* Allocate new block */
1116 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1118 ext4_block_set(fs->bdev, &block);
1122 /* Load newly allocated block */
1123 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1126 ext4_block_set(fs->bdev, &block);
1130 /* Initialize allocated block */
1131 memset(new_block.data, 0, block_size);
1132 new_block.dirty = true;
1134 rc = ext4_block_set(fs->bdev, &new_block);
1136 ext4_block_set(fs->bdev, &block);
1140 /* Write block address to the parent */
1141 ((uint32_t *) block.data)[offset_in_block] =
1142 to_le32(new_block_addr);
1144 current_block = new_block_addr;
1147 /* Will be finished, write the fblock address */
1149 ((uint32_t *) block.data)[offset_in_block] =
1154 rc = ext4_block_set(fs->bdev, &block);
1161 * If we are on the last level, break here as
1162 * there is no next level to visit
1167 /* Visit the next level */
1168 block_offset_in_level %= fs->inode_blocks_per_level[level];
1170 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1176 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1181 struct ext4_fs *fs = inode_ref->fs;
1183 /* Extents are handled otherwise = there is not support in this function */
1184 ext4_assert(!(ext4_sb_check_feature_incompatible(&fs->sb,
1185 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1186 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1188 struct ext4_inode *inode = inode_ref->inode;
1190 /* Handle simple case when we are dealing with direct reference */
1191 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1192 fblock = ext4_inode_get_direct_block(inode, iblock);
1198 ext4_inode_set_direct_block(inode, iblock, 0);
1199 return ext4_balloc_free_block(inode_ref, fblock);
1202 /* Determine the indirection level needed to get the desired block */
1203 unsigned int level = 0;
1205 for (i = 1; i < 4; i++) {
1206 if (iblock < fs->inode_block_limits[i]) {
1215 /* Compute offsets for the topmost level */
1216 uint64_t block_offset_in_level =
1217 iblock - fs->inode_block_limits[level - 1];
1218 uint32_t current_block =
1219 ext4_inode_get_indirect_block(inode, level - 1);
1220 uint32_t offset_in_block =
1221 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1224 * Navigate through other levels, until we find the block number
1225 * or find null reference meaning we are dealing with sparse file
1227 struct ext4_block block;
1232 if (current_block == 0)
1235 int rc = ext4_block_get(fs->bdev, &block, current_block);
1240 to_le32(((uint32_t *) block.data)[offset_in_block]);
1242 /* Set zero if physical data block address found */
1244 ((uint32_t *) block.data)[offset_in_block] =
1249 rc = ext4_block_set(fs->bdev, &block);
1256 * If we are on the last level, break here as
1257 * there is no next level to visit
1262 /* Visit the next level */
1263 block_offset_in_level %= fs->inode_blocks_per_level[level];
1265 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1268 fblock = current_block;
1272 /* Physical block is not referenced, it can be released */
1273 return ext4_balloc_free_block(inode_ref, fblock);
1277 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1278 uint32_t *fblock, uint32_t *iblock)
1280 #if CONFIG_EXTENT_ENABLE
1281 /* Handle extents separately */
1282 if ((ext4_sb_check_feature_incompatible(&inode_ref->fs->sb,
1283 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1284 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1285 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1288 struct ext4_sblock *sb = &inode_ref->fs->sb;
1290 /* Compute next block index and allocate data block */
1291 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1292 uint32_t block_size = ext4_sb_get_block_size(sb);
1294 /* Align size i-node size */
1295 if ((inode_size % block_size) != 0)
1296 inode_size += block_size - (inode_size % block_size);
1298 /* Logical blocks are numbered from 0 */
1299 uint32_t new_block_idx = inode_size / block_size;
1301 /* Allocate new physical block */
1302 uint32_t phys_block;
1303 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1307 /* Add physical block address to the i-node */
1308 rc = ext4_fs_set_inode_data_block_index(inode_ref,
1309 new_block_idx, phys_block);
1311 ext4_balloc_free_block(inode_ref, phys_block);
1316 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1317 inode_ref->dirty = true;
1319 *fblock = phys_block;
1320 *iblock = new_block_idx;