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,
135 "EXT4_FEATURE_INCOMPAT_COMPRESSION\n");
137 if(features_incompatible &
138 EXT4_FEATURE_INCOMPAT_FILETYPE){
139 ext4_dprintf(EXT4_DEBUG_FS,
140 "EXT4_FEATURE_INCOMPAT_FILETYPE\n");
142 if(features_incompatible &
143 EXT4_FEATURE_INCOMPAT_RECOVER){
144 ext4_dprintf(EXT4_DEBUG_FS,
145 "EXT4_FEATURE_INCOMPAT_RECOVER\n");
147 if(features_incompatible &
148 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV){
149 ext4_dprintf(EXT4_DEBUG_FS,
150 "EXT4_FEATURE_INCOMPAT_JOURNAL_DEV\n");
152 if(features_incompatible &
153 EXT4_FEATURE_INCOMPAT_META_BG){
154 ext4_dprintf(EXT4_DEBUG_FS,
155 "EXT4_FEATURE_INCOMPAT_META_BG\n");
157 if(features_incompatible &
158 EXT4_FEATURE_INCOMPAT_EXTENTS){
159 ext4_dprintf(EXT4_DEBUG_FS,
160 "EXT4_FEATURE_INCOMPAT_EXTENTS\n");
162 if(features_incompatible &
163 EXT4_FEATURE_INCOMPAT_64BIT){
164 ext4_dprintf(EXT4_DEBUG_FS,
165 "EXT4_FEATURE_INCOMPAT_64BIT\n");
167 if(features_incompatible &
168 EXT4_FEATURE_INCOMPAT_MMP){
169 ext4_dprintf(EXT4_DEBUG_FS,
170 "EXT4_FEATURE_INCOMPAT_MMP\n");
172 if(features_incompatible &
173 EXT4_FEATURE_INCOMPAT_FLEX_BG){
174 ext4_dprintf(EXT4_DEBUG_FS,
175 "EXT4_FEATURE_INCOMPAT_FLEX_BG\n");
177 if(features_incompatible &
178 EXT4_FEATURE_INCOMPAT_EA_INODE){
179 ext4_dprintf(EXT4_DEBUG_FS,
180 "EXT4_FEATURE_INCOMPAT_EA_INODE\n");
182 if(features_incompatible &
183 EXT4_FEATURE_INCOMPAT_DIRDATA){
184 ext4_dprintf(EXT4_DEBUG_FS,
185 "EXT4_FEATURE_INCOMPAT_DIRDATA\n");
188 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
190 if(features_compatible &
191 EXT4_FEATURE_COMPAT_DIR_PREALLOC){
192 ext4_dprintf(EXT4_DEBUG_FS,
193 "EXT4_FEATURE_COMPAT_DIR_PREALLOC\n");
195 if(features_compatible &
196 EXT4_FEATURE_COMPAT_IMAGIC_INODES){
197 ext4_dprintf(EXT4_DEBUG_FS,
198 "EXT4_FEATURE_COMPAT_IMAGIC_INODES\n");
200 if(features_compatible &
201 EXT4_FEATURE_COMPAT_HAS_JOURNAL){
202 ext4_dprintf(EXT4_DEBUG_FS,
203 "EXT4_FEATURE_COMPAT_HAS_JOURNAL\n");
205 if(features_compatible &
206 EXT4_FEATURE_COMPAT_EXT_ATTR){
207 ext4_dprintf(EXT4_DEBUG_FS,
208 "EXT4_FEATURE_COMPAT_EXT_ATTR\n");
210 if(features_compatible &
211 EXT4_FEATURE_COMPAT_RESIZE_INODE){
212 ext4_dprintf(EXT4_DEBUG_FS,
213 "EXT4_FEATURE_COMPAT_RESIZE_INODE\n");
215 if(features_compatible &
216 EXT4_FEATURE_COMPAT_DIR_INDEX){
217 ext4_dprintf(EXT4_DEBUG_FS,
218 "EXT4_FEATURE_COMPAT_DIR_INDEX\n");
222 static void ext4_fs_debug_features_ro(uint32_t features_ro)
225 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER){
226 ext4_dprintf(EXT4_DEBUG_FS,
227 "EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER\n");
230 EXT4_FEATURE_RO_COMPAT_LARGE_FILE){
231 ext4_dprintf(EXT4_DEBUG_FS,
232 "EXT4_FEATURE_RO_COMPAT_LARGE_FILE\n");
235 EXT4_FEATURE_RO_COMPAT_BTREE_DIR){
236 ext4_dprintf(EXT4_DEBUG_FS,
237 "EXT4_FEATURE_RO_COMPAT_BTREE_DIR\n");
240 EXT4_FEATURE_RO_COMPAT_HUGE_FILE){
241 ext4_dprintf(EXT4_DEBUG_FS,
242 "EXT4_FEATURE_RO_COMPAT_HUGE_FILE\n");
245 EXT4_FEATURE_RO_COMPAT_GDT_CSUM){
246 ext4_dprintf(EXT4_DEBUG_FS,
247 "EXT4_FEATURE_RO_COMPAT_GDT_CSUM\n");
250 EXT4_FEATURE_RO_COMPAT_DIR_NLINK){
251 ext4_dprintf(EXT4_DEBUG_FS,
252 "EXT4_FEATURE_RO_COMPAT_DIR_NLINK\n");
255 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE){
256 ext4_dprintf(EXT4_DEBUG_FS,
257 "EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE\n");
261 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
263 ext4_assert(fs && read_only);
265 if(ext4_get32(&fs->sb, rev_level) == 0){
269 ext4_dprintf(EXT4_DEBUG_FS,
270 "\nSblock rev_level: \n%d\n", (int)ext4_get32(&fs->sb, rev_level));
272 ext4_dprintf(EXT4_DEBUG_FS,
273 "\nSblock minor_rev_level: \n%d\n",
274 ext4_get32(&fs->sb, minor_rev_level));
276 ext4_dprintf(EXT4_DEBUG_FS,
277 "\nSblock features_incompatible:\n");
278 ext4_fs_debug_features_incomp(ext4_get32(&fs->sb, features_incompatible));
280 ext4_dprintf(EXT4_DEBUG_FS,
281 "\nSblock features_compatible:\n");
282 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
284 ext4_dprintf(EXT4_DEBUG_FS,
285 "\nSblock features_read_only:\n");
286 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
288 /*Check features_incompatible*/
289 v = (ext4_get32(&fs->sb, features_incompatible) &
290 (~EXT4_FEATURE_INCOMPAT_SUPP));
292 ext4_dprintf(EXT4_DEBUG_FS,
293 "\nERROR sblock features_incompatible. Unsupported:\n");
294 ext4_fs_debug_features_incomp(v);
299 /*Check features_read_only*/
300 v = (ext4_get32(&fs->sb, features_read_only) &
301 (~EXT4_FEATURE_RO_COMPAT_SUPP));
303 ext4_dprintf(EXT4_DEBUG_FS,
304 "\nERROR sblock features_read_only . Unsupported:\n");
305 ext4_fs_debug_features_incomp(v);
316 uint32_t ext4_fs_baddr2_index_in_group(struct ext4_sblock *s, uint32_t baddr)
319 if(ext4_get32(s, first_data_block))
322 return baddr % ext4_get32(s, blocks_per_group);
327 uint32_t ext4_fs_index_in_group2_baddr(struct ext4_sblock *s, uint32_t index,
330 if(ext4_get32(s, first_data_block))
333 return ext4_get32(s, blocks_per_group) * bgid + index;
339 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
342 uint32_t bitmap_block_addr = ext4_bg_get_block_bitmap(
343 bg_ref->block_group, &bg_ref->fs->sb);
345 struct ext4_block block_bitmap;
346 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
352 memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
354 /* Determine first block and first data block in group */
355 uint32_t first_idx = 0;
357 uint32_t first_data = ext4_balloc_get_first_data_block_in_group(
358 &bg_ref->fs->sb, bg_ref);
359 uint32_t first_data_idx = ext4_fs_baddr2_index_in_group(
360 &bg_ref->fs->sb, first_data);
362 /*Set bits from to first block to first data block - 1 to one (allocated)*/
363 /*TODO: Optimize it*/
364 for (i = first_idx; i < first_data_idx; ++i)
365 ext4_bmap_bit_set(block_bitmap.data, i);
368 block_bitmap.dirty = true;
371 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
374 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
377 uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap(
378 bg_ref->block_group, &bg_ref->fs->sb);
380 struct ext4_block block_bitmap;
381 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap,
386 /* Initialize all bitmap bits to zero */
387 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
388 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
390 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
392 uint32_t start_bit = inodes_per_group;
393 uint32_t end_bit = block_size * 8;
396 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
397 ext4_bmap_bit_set(block_bitmap.data, i);
400 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
402 block_bitmap.dirty = true;
405 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
408 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
410 struct ext4_sblock *sb = &bg_ref->fs->sb;
412 uint32_t inode_size = ext4_get32(sb, inode_size);
413 uint32_t block_size = ext4_sb_get_block_size(sb);
414 uint32_t inodes_per_block = block_size / inode_size;
415 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
416 uint32_t table_blocks = inodes_in_group / inodes_per_block;
419 if (inodes_in_group % inodes_per_block)
422 /* Compute initialization bounds */
423 uint32_t first_block = ext4_bg_get_inode_table_first_block(
424 bg_ref->block_group, sb);
426 uint32_t last_block = first_block + table_blocks - 1;
428 /* Initialization of all itable blocks */
429 for (fblock = first_block; fblock <= last_block; ++fblock) {
431 struct ext4_block block;
432 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
436 memset(block.data, 0, block_size);
439 ext4_block_set(bg_ref->fs->bdev, &block);
448 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
449 struct ext4_block_group_ref *ref)
451 /* Compute number of descriptors, that fits in one data block */
452 uint32_t dsc_per_block = ext4_sb_get_block_size(&fs->sb) /
453 ext4_sb_get_desc_size(&fs->sb);
455 /* Block group descriptor table starts at the next block after superblock */
456 uint64_t block_id = ext4_get32(&fs->sb, first_data_block) + 1;
458 /* Find the block containing the descriptor we are looking for */
459 block_id += bgid / dsc_per_block;
460 uint32_t offset = (bgid % dsc_per_block) *
461 ext4_sb_get_desc_size(&fs->sb);
464 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
468 ref->block_group = (void *)(ref->block.data + offset);
473 if (ext4_bg_has_flag(ref->block_group,
474 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
475 rc = ext4_fs_init_block_bitmap(ref);
477 ext4_block_set(fs->bdev, &ref->block);
480 ext4_bg_clear_flag(ref->block_group,
481 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
486 if (ext4_bg_has_flag(ref->block_group,
487 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
488 rc = ext4_fs_init_inode_bitmap(ref);
490 ext4_block_set(ref->fs->bdev, &ref->block);
494 ext4_bg_clear_flag(ref->block_group,
495 EXT4_BLOCK_GROUP_INODE_UNINIT);
497 if (!ext4_bg_has_flag(ref->block_group,
498 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
499 rc = ext4_fs_init_inode_table(ref);
501 ext4_block_set(fs->bdev, &ref->block);
505 ext4_bg_set_flag(ref->block_group,
506 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
515 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
516 struct ext4_bgroup *bg)
518 /* If checksum not supported, 0 will be returned */
521 /* Compute the checksum only if the filesystem supports it */
522 if (ext4_sb_check_read_only(sb,
523 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
525 void *checksum = &bg->checksum;
527 uint32_t offset = (uint32_t) (checksum - base);
529 /* Convert block group index to little endian */
530 uint32_t le_group = to_le32(bgid);
533 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
535 /* Include index of block group */
536 crc = ext4_bg_crc16(crc, (uint8_t *) &le_group, sizeof(le_group));
538 /* Compute crc from the first part (stop before checksum field) */
539 crc = ext4_bg_crc16(crc, (uint8_t *) bg, offset);
542 offset += sizeof(bg->checksum);
544 /* Checksum of the rest of block group descriptor */
545 if ((ext4_sb_check_feature_incompatible(sb,
546 EXT4_FEATURE_INCOMPAT_64BIT)) &&
547 (offset < ext4_sb_get_desc_size(sb)))
549 crc = ext4_bg_crc16(crc, ((uint8_t *) bg) + offset,
550 ext4_sb_get_desc_size(sb) - offset);
555 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
557 /* Check if reference modified */
559 /* Compute new checksum of block group */
561 ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
564 ref->block_group->checksum = to_le16(checksum);
566 /* Mark block dirty for writing changes to physical device */
567 ref->block.dirty = true;
570 /* Put back block, that contains block group descriptor */
571 return ext4_block_set(ref->fs->bdev, &ref->block);
574 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
575 struct ext4_inode_ref *ref)
577 /* Compute number of i-nodes, that fits in one data block */
578 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
581 * Inode numbers are 1-based, but it is simpler to work with 0-based
582 * when computing indices
585 uint32_t block_group = index / inodes_per_group;
586 uint32_t offset_in_group = index % inodes_per_group;
588 /* Load block group, where i-node is located */
589 struct ext4_block_group_ref bg_ref;
591 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
596 /* Load block address, where i-node table is located */
597 uint32_t inode_table_start =
598 ext4_bg_get_inode_table_first_block(bg_ref.block_group,
601 /* Put back block group reference (not needed more) */
602 rc = ext4_fs_put_block_group_ref(&bg_ref);
607 /* Compute position of i-node in the block group */
608 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
609 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
610 uint32_t byte_offset_in_group = offset_in_group * inode_size;
612 /* Compute block address */
613 uint64_t block_id = inode_table_start +
614 (byte_offset_in_group / block_size);
617 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
622 /* Compute position of i-node in the data block */
623 uint32_t offset_in_block = byte_offset_in_group % block_size;
624 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
626 /* We need to store the original value of index in the reference */
627 ref->index = index + 1;
634 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
636 /* Check if reference modified */
638 /* Mark block dirty for writing changes to physical device */
639 ref->block.dirty = true;
642 /* Put back block, that contains i-node */
643 return ext4_block_set(ref->fs->bdev, &ref->block);
646 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
649 /* Check if newly allocated i-node will be a directory */
653 is_dir = is_directory;
655 /* Allocate inode by allocation algorithm */
657 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
661 /* Load i-node from on-disk i-node table */
662 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
664 ext4_ialloc_free_inode(fs, index, is_dir);
668 /* Initialize i-node */
669 struct ext4_inode *inode = inode_ref->inode;
674 * Default directory permissions to be compatible with other systems
675 * 0777 (octal) == rwxrwxrwx
679 mode |= EXT4_INODE_MODE_DIRECTORY;
680 ext4_inode_set_mode(&fs->sb, inode, mode);
681 ext4_inode_set_links_count(inode, 1); /* '.' entry */
685 * Default file permissions to be compatible with other systems
686 * 0666 (octal) == rw-rw-rw-
690 mode |= EXT4_INODE_MODE_FILE;
691 ext4_inode_set_mode(&fs->sb, inode, mode);
692 ext4_inode_set_links_count(inode, 0);
695 ext4_inode_set_uid(inode, 0);
696 ext4_inode_set_gid(inode, 0);
697 ext4_inode_set_size(inode, 0);
698 ext4_inode_set_access_time(inode, 0);
699 ext4_inode_set_change_inode_time(inode, 0);
700 ext4_inode_set_modification_time(inode, 0);
701 ext4_inode_set_deletion_time(inode, 0);
702 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
703 ext4_inode_set_flags(inode, 0);
704 ext4_inode_set_generation(inode, 0);
706 /* Reset blocks array */
707 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
708 inode->blocks[i] = 0;
710 #if CONFIG_EXTENT_ENABLE
711 /* Initialize extents if needed */
712 if (ext4_sb_check_feature_incompatible(
713 &fs->sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
714 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
717 /* Initialize extent root header */
718 struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
719 ext4_extent_header_set_depth(header, 0);
720 ext4_extent_header_set_entries_count(header, 0);
721 ext4_extent_header_set_generation(header, 0);
722 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
724 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
725 sizeof(struct ext4_extent_header)) / sizeof(struct ext4_extent);
727 ext4_extent_header_set_max_entries_count(header, max_entries);
731 inode_ref->dirty = true;
736 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
738 struct ext4_fs *fs = inode_ref->fs;
741 #if CONFIG_EXTENT_ENABLE
742 /* For extents must be data block destroyed by other way */
743 if ((ext4_sb_check_feature_incompatible(&fs->sb,
744 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
745 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
746 /* Data structures are released during truncate operation... */
750 /* Release all indirect (no data) blocks */
752 /* 1) Single indirect */
753 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
755 int rc = ext4_balloc_free_block(inode_ref, fblock);
759 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
762 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
763 uint32_t count = block_size / sizeof(uint32_t);
765 struct ext4_block block;
767 /* 2) Double indirect */
768 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
770 int rc = ext4_block_get(fs->bdev, &block, fblock);
775 for (offset = 0; offset < count; ++offset) {
776 ind_block = to_le32(((uint32_t *) block.data)[offset]);
778 if (ind_block != 0) {
779 rc = ext4_balloc_free_block(inode_ref, ind_block);
781 ext4_block_set(fs->bdev, &block);
787 ext4_block_set(fs->bdev, &block);
788 rc = ext4_balloc_free_block(inode_ref, fblock);
792 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
795 /* 3) Tripple indirect */
796 struct ext4_block subblock;
797 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
799 int rc = ext4_block_get(fs->bdev, &block, fblock);
804 for ( offset = 0; offset < count; ++offset) {
805 ind_block = to_le32(((uint32_t *) block.data)[offset]);
807 if (ind_block != 0) {
808 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
810 ext4_block_set(fs->bdev, &block);
814 uint32_t ind_subblock;
815 for (suboffset = 0; suboffset < count;
817 ind_subblock = to_le32(((uint32_t *)
818 subblock.data)[suboffset]);
820 if (ind_subblock != 0) {
821 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
823 ext4_block_set(fs->bdev, &subblock);
824 ext4_block_set(fs->bdev, &block);
830 ext4_block_set(fs->bdev, &subblock);
833 rc = ext4_balloc_free_block(inode_ref, ind_block);
835 ext4_block_set(fs->bdev, &block);
841 ext4_block_set(fs->bdev, &block);
842 rc = ext4_balloc_free_block(inode_ref, fblock);
846 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
850 /* Mark inode dirty for writing to the physical device */
851 inode_ref->dirty = true;
853 /* Free block with extended attributes if present */
854 uint32_t xattr_block = ext4_inode_get_file_acl(
855 inode_ref->inode, &fs->sb);
857 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
861 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
864 /* Free inode by allocator */
866 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
867 EXT4_INODE_MODE_DIRECTORY))
868 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
870 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
875 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref,
878 struct ext4_sblock *sb = &inode_ref->fs->sb;
881 /* Check flags, if i-node can be truncated */
882 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
885 /* If sizes are equal, nothing has to be done. */
886 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
887 if (old_size == new_size)
890 /* It's not suppported to make the larger file by truncate operation */
891 if (old_size < new_size)
894 /* Compute how many blocks will be released */
895 uint64_t size_diff = old_size - new_size;
896 uint32_t block_size = ext4_sb_get_block_size(sb);
897 uint32_t diff_blocks_count = size_diff / block_size;
898 if (size_diff % block_size != 0)
901 uint32_t old_blocks_count = old_size / block_size;
902 if (old_size % block_size != 0)
904 #if CONFIG_EXTENT_ENABLE
905 if ((ext4_sb_check_feature_incompatible(sb,
906 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
907 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
909 /* Extents require special operation */
910 int rc = ext4_extent_release_blocks_from(inode_ref,
911 old_blocks_count - diff_blocks_count);
918 /* Release data blocks from the end of file */
920 /* Starting from 1 because of logical blocks are numbered from 0 */
921 for (i = 1; i <= diff_blocks_count; ++i) {
922 int rc = ext4_fs_release_inode_block(inode_ref,
923 old_blocks_count - i);
930 ext4_inode_set_size(inode_ref->inode, new_size);
931 inode_ref->dirty = true;
936 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
937 uint64_t iblock, uint32_t *fblock)
939 struct ext4_fs *fs = inode_ref->fs;
941 /* For empty file is situation simple */
942 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
947 uint32_t current_block;
948 #if CONFIG_EXTENT_ENABLE
949 /* Handle i-node using extents */
950 if ((ext4_sb_check_feature_incompatible(&fs->sb,
951 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
952 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
955 int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
959 *fblock = current_block;
964 struct ext4_inode *inode = inode_ref->inode;
966 /* Direct block are read directly from array in i-node structure */
967 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
968 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
969 *fblock = current_block;
973 /* Determine indirection level of the target block */
974 unsigned int level = 0;
976 for (i = 1; i < 4; i++) {
977 if (iblock < fs->inode_block_limits[i]) {
986 /* Compute offsets for the topmost level */
987 uint64_t block_offset_in_level =
988 iblock - fs->inode_block_limits[level - 1];
989 current_block = ext4_inode_get_indirect_block(inode, level - 1);
990 uint32_t offset_in_block =
991 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
994 if (current_block == 0) {
999 struct ext4_block block;
1002 * Navigate through other levels, until we find the block number
1003 * or find null reference meaning we are dealing with sparse file
1006 /* Load indirect block */
1007 int rc = ext4_block_get(fs->bdev, &block, current_block);
1011 /* Read block address from indirect block */
1013 to_le32(((uint32_t *) block.data)[offset_in_block]);
1015 /* Put back indirect block untouched */
1016 rc = ext4_block_set(fs->bdev, &block);
1020 /* Check for sparse file */
1021 if (current_block == 0) {
1026 /* Jump to the next level */
1029 /* Termination condition - we have address of data block loaded */
1033 /* Visit the next level */
1034 block_offset_in_level %= fs->inode_blocks_per_level[level];
1036 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1039 *fblock = current_block;
1044 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1045 uint64_t iblock, uint32_t fblock)
1047 struct ext4_fs *fs = inode_ref->fs;
1049 #if CONFIG_EXTENT_ENABLE
1050 /* Handle inode using extents */
1051 if ((ext4_sb_check_feature_incompatible(&fs->sb,
1052 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1053 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1059 /* Handle simple case when we are dealing with direct reference */
1060 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1061 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock,
1063 inode_ref->dirty = true;
1068 /* Determine the indirection level needed to get the desired block */
1069 unsigned int level = 0;
1071 for (i = 1; i < 4; i++) {
1072 if (iblock < fs->inode_block_limits[i]) {
1081 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1083 /* Compute offsets for the topmost level */
1084 uint64_t block_offset_in_level =
1085 iblock - fs->inode_block_limits[level - 1];
1086 uint32_t current_block =
1087 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1088 uint32_t offset_in_block =
1089 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1091 uint32_t new_block_addr;
1093 struct ext4_block block;
1094 struct ext4_block new_block;
1096 /* Is needed to allocate indirect block on the i-node level */
1097 if (current_block == 0) {
1098 /* Allocate new indirect block */
1099 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1104 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1106 inode_ref->dirty = true;
1108 /* Load newly allocated block */
1109 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1111 ext4_balloc_free_block(inode_ref, new_block_addr);
1115 /* Initialize new block */
1116 memset(new_block.data, 0, block_size);
1117 new_block.dirty = true;
1119 /* Put back the allocated block */
1120 rc = ext4_block_set(fs->bdev, &new_block);
1124 current_block = new_block_addr;
1128 * Navigate through other levels, until we find the block number
1129 * or find null reference meaning we are dealing with sparse file
1132 int rc = ext4_block_get(fs->bdev, &block, current_block);
1137 to_le32(((uint32_t *) block.data)[offset_in_block]);
1139 if ((level > 1) && (current_block == 0)) {
1140 /* Allocate new block */
1141 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1143 ext4_block_set(fs->bdev, &block);
1147 /* Load newly allocated block */
1148 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1151 ext4_block_set(fs->bdev, &block);
1155 /* Initialize allocated block */
1156 memset(new_block.data, 0, block_size);
1157 new_block.dirty = true;
1159 rc = ext4_block_set(fs->bdev, &new_block);
1161 ext4_block_set(fs->bdev, &block);
1165 /* Write block address to the parent */
1166 ((uint32_t *) block.data)[offset_in_block] =
1167 to_le32(new_block_addr);
1169 current_block = new_block_addr;
1172 /* Will be finished, write the fblock address */
1174 ((uint32_t *) block.data)[offset_in_block] =
1179 rc = ext4_block_set(fs->bdev, &block);
1186 * If we are on the last level, break here as
1187 * there is no next level to visit
1192 /* Visit the next level */
1193 block_offset_in_level %= fs->inode_blocks_per_level[level];
1195 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1201 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1206 struct ext4_fs *fs = inode_ref->fs;
1208 /* Extents are handled otherwise = there is not support in this function */
1209 ext4_assert(!(ext4_sb_check_feature_incompatible(&fs->sb,
1210 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1211 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1213 struct ext4_inode *inode = inode_ref->inode;
1215 /* Handle simple case when we are dealing with direct reference */
1216 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1217 fblock = ext4_inode_get_direct_block(inode, iblock);
1223 ext4_inode_set_direct_block(inode, iblock, 0);
1224 return ext4_balloc_free_block(inode_ref, fblock);
1227 /* Determine the indirection level needed to get the desired block */
1228 unsigned int level = 0;
1230 for (i = 1; i < 4; i++) {
1231 if (iblock < fs->inode_block_limits[i]) {
1240 /* Compute offsets for the topmost level */
1241 uint64_t block_offset_in_level =
1242 iblock - fs->inode_block_limits[level - 1];
1243 uint32_t current_block =
1244 ext4_inode_get_indirect_block(inode, level - 1);
1245 uint32_t offset_in_block =
1246 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1249 * Navigate through other levels, until we find the block number
1250 * or find null reference meaning we are dealing with sparse file
1252 struct ext4_block block;
1257 if (current_block == 0)
1260 int rc = ext4_block_get(fs->bdev, &block, current_block);
1265 to_le32(((uint32_t *) block.data)[offset_in_block]);
1267 /* Set zero if physical data block address found */
1269 ((uint32_t *) block.data)[offset_in_block] =
1274 rc = ext4_block_set(fs->bdev, &block);
1281 * If we are on the last level, break here as
1282 * there is no next level to visit
1287 /* Visit the next level */
1288 block_offset_in_level %= fs->inode_blocks_per_level[level];
1290 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1293 fblock = current_block;
1297 /* Physical block is not referenced, it can be released */
1298 return ext4_balloc_free_block(inode_ref, fblock);
1302 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1303 uint32_t *fblock, uint32_t *iblock)
1305 #if CONFIG_EXTENT_ENABLE
1306 /* Handle extents separately */
1307 if ((ext4_sb_check_feature_incompatible(&inode_ref->fs->sb,
1308 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1309 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))){
1310 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1313 struct ext4_sblock *sb = &inode_ref->fs->sb;
1315 /* Compute next block index and allocate data block */
1316 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1317 uint32_t block_size = ext4_sb_get_block_size(sb);
1319 /* Align size i-node size */
1320 if ((inode_size % block_size) != 0)
1321 inode_size += block_size - (inode_size % block_size);
1323 /* Logical blocks are numbered from 0 */
1324 uint32_t new_block_idx = inode_size / block_size;
1326 /* Allocate new physical block */
1327 uint32_t phys_block;
1328 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1332 /* Add physical block address to the i-node */
1333 rc = ext4_fs_set_inode_data_block_index(inode_ref,
1334 new_block_idx, phys_block);
1336 ext4_balloc_free_block(inode_ref, phys_block);
1341 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1342 inode_ref->dirty = true;
1344 *fblock = phys_block;
1345 *iblock = new_block_idx;