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"
57 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
62 bool read_only = false;
64 ext4_assert(fs && bdev);
68 r = ext4_sb_read(fs->bdev, &fs->sb);
72 if (!ext4_sb_check(&fs->sb))
75 bsize = ext4_sb_get_block_size(&fs->sb);
76 if (bsize > EXT4_MAX_BLOCK_SIZE)
79 r = ext4_fs_check_features(fs, &read_only);
86 /* Compute limits for indirect block levels */
87 uint32_t blocks_id = bsize / sizeof(uint32_t);
89 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
90 fs->inode_blocks_per_level[0] = 1;
92 for (i = 1; i < 4; i++) {
93 fs->inode_blocks_per_level[i] =
94 fs->inode_blocks_per_level[i - 1] * blocks_id;
95 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
96 fs->inode_blocks_per_level[i];
100 tmp = ext4_get16(&fs->sb, state);
101 if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) {
102 ext4_dprintf(EXT4_DEBUG_FS, "last umount error\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);
111 /*Update mount count*/
112 ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
117 int ext4_fs_fini(struct ext4_fs *fs)
121 /*Set superblock state*/
122 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
124 return ext4_sb_write(fs->bdev, &fs->sb);
127 static void ext4_fs_debug_features_incomp(uint32_t features_incompatible)
130 if (features_incompatible & EXT4_FEATURE_INCOMPAT_COMPRESSION) {
131 ext4_dprintf(EXT4_DEBUG_FS, "compression\n");
133 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FILETYPE) {
134 ext4_dprintf(EXT4_DEBUG_FS, "filetype\n");
136 if (features_incompatible & EXT4_FEATURE_INCOMPAT_RECOVER) {
137 ext4_dprintf(EXT4_DEBUG_FS, "recover\n");
139 if (features_incompatible & EXT4_FEATURE_INCOMPAT_JOURNAL_DEV) {
140 ext4_dprintf(EXT4_DEBUG_FS, "journal_dev\n");
142 if (features_incompatible & EXT4_FEATURE_INCOMPAT_META_BG) {
143 ext4_dprintf(EXT4_DEBUG_FS, "meta_bg\n");
145 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EXTENTS) {
146 ext4_dprintf(EXT4_DEBUG_FS, "extents\n");
148 if (features_incompatible & EXT4_FEATURE_INCOMPAT_64BIT) {
149 ext4_dprintf(EXT4_DEBUG_FS, "64bit\n");
151 if (features_incompatible & EXT4_FEATURE_INCOMPAT_MMP) {
152 ext4_dprintf(EXT4_DEBUG_FS, "mnp\n");
154 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
155 ext4_dprintf(EXT4_DEBUG_FS, "flex_bg\n");
157 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EA_INODE) {
158 ext4_dprintf(EXT4_DEBUG_FS, "ea_inode\n");
160 if (features_incompatible & EXT4_FEATURE_INCOMPAT_DIRDATA) {
161 ext4_dprintf(EXT4_DEBUG_FS, "dirdata\n");
163 if (features_incompatible & EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM) {
164 ext4_dprintf(EXT4_DEBUG_FS, "meta_csum\n");
166 if (features_incompatible & EXT4_FEATURE_INCOMPAT_LARGEDIR) {
167 ext4_dprintf(EXT4_DEBUG_FS, "largedir\n");
169 if (features_incompatible & EXT4_FEATURE_INCOMPAT_INLINE_DATA) {
170 ext4_dprintf(EXT4_DEBUG_FS, "inline_data\n");
173 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
175 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_PREALLOC) {
176 ext4_dprintf(EXT4_DEBUG_FS, " dir_prealloc\n");
178 if (features_compatible & EXT4_FEATURE_COMPAT_IMAGIC_INODES) {
179 ext4_dprintf(EXT4_DEBUG_FS, "imagic_inodes\n");
181 if (features_compatible & EXT4_FEATURE_COMPAT_HAS_JOURNAL) {
182 ext4_dprintf(EXT4_DEBUG_FS, "has_journal\n");
184 if (features_compatible & EXT4_FEATURE_COMPAT_EXT_ATTR) {
185 ext4_dprintf(EXT4_DEBUG_FS, "ext_attr\n");
187 if (features_compatible & EXT4_FEATURE_COMPAT_RESIZE_INODE) {
188 ext4_dprintf(EXT4_DEBUG_FS, "resize_inode\n");
190 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_INDEX) {
191 ext4_dprintf(EXT4_DEBUG_FS, "dir_index\n");
195 static void ext4_fs_debug_features_ro(uint32_t features_ro)
197 if (features_ro & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) {
198 ext4_dprintf(EXT4_DEBUG_FS, "sparse_super\n");
200 if (features_ro & EXT4_FEATURE_RO_COMPAT_LARGE_FILE) {
201 ext4_dprintf(EXT4_DEBUG_FS, "large_file\n");
203 if (features_ro & EXT4_FEATURE_RO_COMPAT_BTREE_DIR) {
204 ext4_dprintf(EXT4_DEBUG_FS, "btree_dir\n");
206 if (features_ro & EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
207 ext4_dprintf(EXT4_DEBUG_FS, "huge_file\n");
209 if (features_ro & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
210 ext4_dprintf(EXT4_DEBUG_FS, "gtd_csum\n");
212 if (features_ro & EXT4_FEATURE_RO_COMPAT_DIR_NLINK) {
213 ext4_dprintf(EXT4_DEBUG_FS, "dir_nlink\n");
215 if (features_ro & EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE) {
216 ext4_dprintf(EXT4_DEBUG_FS, "extra_isize\n");
218 if (features_ro & EXT4_FEATURE_RO_COMPAT_QUOTA) {
219 ext4_dprintf(EXT4_DEBUG_FS, "quota\n");
221 if (features_ro & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
222 ext4_dprintf(EXT4_DEBUG_FS, "bigalloc\n");
224 if (features_ro & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
225 ext4_dprintf(EXT4_DEBUG_FS, "metadata_csum\n");
229 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
231 ext4_assert(fs && read_only);
233 if (ext4_get32(&fs->sb, rev_level) == 0) {
238 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_incompatible:\n");
239 ext4_fs_debug_features_incomp(
240 ext4_get32(&fs->sb, features_incompatible));
242 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_compatible:\n");
243 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
245 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_read_only:\n");
246 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
248 /*Check features_incompatible*/
249 v = (ext4_get32(&fs->sb, features_incompatible) &
250 (~CONFIG_FEATURE_INCOMPAT_SUPP));
252 ext4_dprintf(EXT4_DEBUG_FS, "SB features_incompatible: fail\n");
253 ext4_fs_debug_features_incomp(v);
257 /*Check features_read_only*/
258 v = (ext4_get32(&fs->sb, features_read_only) &
259 (~CONFIG_FEATURE_RO_COMPAT_SUPP));
263 "\nERROR sblock features_read_only . Unsupported:\n");
264 ext4_fs_debug_features_incomp(v);
274 /**@brief Initialize block bitmap in block group.
275 * @param bg_ref Reference to block group
278 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
281 uint32_t bitmap_block_addr =
282 ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
284 struct ext4_block block_bitmap;
286 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
290 memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
292 /* Determine first block and first data block in group */
293 uint32_t first_idx = 0;
295 uint32_t first_data =
296 ext4_balloc_get_first_data_block_in_group(&bg_ref->fs->sb, bg_ref);
297 uint32_t first_data_idx =
298 ext4_fs_baddr2_index_in_group(&bg_ref->fs->sb, first_data);
300 /*Set bits from to first block to first data block - 1 to one
302 /*TODO: Optimize it*/
303 for (i = first_idx; i < first_data_idx; ++i)
304 ext4_bmap_bit_set(block_bitmap.data, i);
306 block_bitmap.dirty = true;
309 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
312 /**@brief Initialize i-node bitmap in block group.
313 * @param bg_ref Reference to block group
316 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
319 uint32_t bitmap_block_addr =
320 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
322 struct ext4_block block_bitmap;
324 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
328 /* Initialize all bitmap bits to zero */
329 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
330 uint32_t inodes_per_group =
331 ext4_get32(&bg_ref->fs->sb, inodes_per_group);
333 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
335 uint32_t start_bit = inodes_per_group;
336 uint32_t end_bit = block_size * 8;
339 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
340 ext4_bmap_bit_set(block_bitmap.data, i);
343 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
345 block_bitmap.dirty = true;
348 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
351 /**@brief Initialize i-node table in block group.
352 * @param bg_ref Reference to block group
355 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
357 struct ext4_sblock *sb = &bg_ref->fs->sb;
359 uint32_t inode_size = ext4_get32(sb, inode_size);
360 uint32_t block_size = ext4_sb_get_block_size(sb);
361 uint32_t inodes_per_block = block_size / inode_size;
362 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
363 uint32_t table_blocks = inodes_in_group / inodes_per_block;
366 if (inodes_in_group % inodes_per_block)
369 /* Compute initialization bounds */
370 uint32_t first_block =
371 ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
373 uint32_t last_block = first_block + table_blocks - 1;
375 /* Initialization of all itable blocks */
376 for (fblock = first_block; fblock <= last_block; ++fblock) {
378 struct ext4_block block;
379 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
383 memset(block.data, 0, block_size);
386 ext4_block_set(bg_ref->fs->bdev, &block);
394 static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
396 uint32_t dsc_per_block)
398 uint32_t first_meta_bg, dsc_id;
402 dsc_id = bgid / dsc_per_block;
403 first_meta_bg = ext4_sb_first_meta_bg(s);
405 if (!ext4_sb_has_feature_incompatible(s,
406 EXT4_FEATURE_INCOMPAT_META_BG) ||
407 dsc_id < first_meta_bg)
408 return ext4_get32(s, first_data_block) + dsc_id + 1;
410 if (ext4_sb_is_super_in_bg(s, bgid))
413 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
416 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
417 struct ext4_block_group_ref *ref)
419 /* Compute number of descriptors, that fits in one data block */
420 uint32_t dsc_per_block =
421 ext4_sb_get_block_size(&fs->sb) / ext4_sb_get_desc_size(&fs->sb);
423 /* Block group descriptor table starts at the next block after
426 ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_per_block);
429 (bgid % dsc_per_block) * ext4_sb_get_desc_size(&fs->sb);
431 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
435 ref->block_group = (void *)(ref->block.data + offset);
440 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
441 rc = ext4_fs_init_block_bitmap(ref);
443 ext4_block_set(fs->bdev, &ref->block);
446 ext4_bg_clear_flag(ref->block_group,
447 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
452 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
453 rc = ext4_fs_init_inode_bitmap(ref);
455 ext4_block_set(ref->fs->bdev, &ref->block);
459 ext4_bg_clear_flag(ref->block_group,
460 EXT4_BLOCK_GROUP_INODE_UNINIT);
462 if (!ext4_bg_has_flag(ref->block_group,
463 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
464 rc = ext4_fs_init_inode_table(ref);
466 ext4_block_set(fs->bdev, &ref->block);
470 ext4_bg_set_flag(ref->block_group,
471 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
480 /**@brief Compute checksum of block group descriptor.
481 * @param sb Superblock
482 * @param bgid Index of block group in the filesystem
483 * @param bg Block group to compute checksum for
484 * @return Checksum value
486 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
487 struct ext4_bgroup *bg)
489 /* If checksum not supported, 0 will be returned */
492 /* Compute the checksum only if the filesystem supports it */
493 if (ext4_sb_has_feature_read_only(sb,
494 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
495 uint8_t *base = (uint8_t *)bg;
496 uint8_t *checksum = (uint8_t *)&bg->checksum;
498 uint32_t offset = (uint32_t)(checksum - base);
500 /* Convert block group index to little endian */
501 uint32_t le_group = to_le32(bgid);
504 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
506 /* Include index of block group */
508 ext4_bg_crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
510 /* Compute crc from the first part (stop before checksum field)
512 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
515 offset += sizeof(bg->checksum);
517 /* Checksum of the rest of block group descriptor */
518 if ((ext4_sb_has_feature_incompatible(
519 sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
520 (offset < ext4_sb_get_desc_size(sb)))
522 crc = ext4_bg_crc16(crc, ((uint8_t *)bg) + offset,
523 ext4_sb_get_desc_size(sb) - offset);
528 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
530 /* Check if reference modified */
532 /* Compute new checksum of block group */
533 uint16_t checksum = ext4_fs_bg_checksum(
534 &ref->fs->sb, ref->index, ref->block_group);
536 ref->block_group->checksum = to_le16(checksum);
538 /* Mark block dirty for writing changes to physical device */
539 ref->block.dirty = true;
542 /* Put back block, that contains block group descriptor */
543 return ext4_block_set(ref->fs->bdev, &ref->block);
546 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
547 struct ext4_inode_ref *ref)
549 /* Compute number of i-nodes, that fits in one data block */
550 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
553 * Inode numbers are 1-based, but it is simpler to work with 0-based
554 * when computing indices
557 uint32_t block_group = index / inodes_per_group;
558 uint32_t offset_in_group = index % inodes_per_group;
560 /* Load block group, where i-node is located */
561 struct ext4_block_group_ref bg_ref;
563 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
568 /* Load block address, where i-node table is located */
569 uint32_t inode_table_start =
570 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
572 /* Put back block group reference (not needed more) */
573 rc = ext4_fs_put_block_group_ref(&bg_ref);
578 /* Compute position of i-node in the block group */
579 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
580 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
581 uint32_t byte_offset_in_group = offset_in_group * inode_size;
583 /* Compute block address */
585 inode_table_start + (byte_offset_in_group / block_size);
587 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
592 /* Compute position of i-node in the data block */
593 uint32_t offset_in_block = byte_offset_in_group % block_size;
594 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
596 /* We need to store the original value of index in the reference */
597 ref->index = index + 1;
604 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
606 /* Check if reference modified */
608 /* Mark block dirty for writing changes to physical device */
609 ref->block.dirty = true;
612 /* Put back block, that contains i-node */
613 return ext4_block_set(ref->fs->bdev, &ref->block);
616 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
619 /* Check if newly allocated i-node will be a directory */
623 is_dir = is_directory;
625 /* Allocate inode by allocation algorithm */
627 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
631 /* Load i-node from on-disk i-node table */
632 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
634 ext4_ialloc_free_inode(fs, index, is_dir);
638 /* Initialize i-node */
639 struct ext4_inode *inode = inode_ref->inode;
644 * Default directory permissions to be compatible with other
646 * 0777 (octal) == rwxrwxrwx
650 mode |= EXT4_INODE_MODE_DIRECTORY;
651 ext4_inode_set_mode(&fs->sb, inode, mode);
654 * Default file permissions to be compatible with other systems
655 * 0666 (octal) == rw-rw-rw-
659 mode |= EXT4_INODE_MODE_FILE;
660 ext4_inode_set_mode(&fs->sb, inode, mode);
663 ext4_inode_set_links_count(inode, 0);
664 ext4_inode_set_uid(inode, 0);
665 ext4_inode_set_gid(inode, 0);
666 ext4_inode_set_size(inode, 0);
667 ext4_inode_set_access_time(inode, 0);
668 ext4_inode_set_change_inode_time(inode, 0);
669 ext4_inode_set_modification_time(inode, 0);
670 ext4_inode_set_deletion_time(inode, 0);
671 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
672 ext4_inode_set_flags(inode, 0);
673 ext4_inode_set_generation(inode, 0);
675 /* Reset blocks array */
676 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
677 inode->blocks[i] = 0;
679 #if CONFIG_EXTENT_ENABLE
680 /* Initialize extents if needed */
681 if (ext4_sb_has_feature_incompatible(&fs->sb,
682 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
683 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
685 /* Initialize extent root header */
686 struct ext4_extent_header *header =
687 ext4_inode_get_extent_header(inode);
688 ext4_extent_header_set_depth(header, 0);
689 ext4_extent_header_set_entries_count(header, 0);
690 ext4_extent_header_set_generation(header, 0);
691 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
693 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
694 sizeof(struct ext4_extent_header)) /
695 sizeof(struct ext4_extent);
697 ext4_extent_header_set_max_entries_count(header, max_entries);
701 inode_ref->dirty = true;
706 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
708 struct ext4_fs *fs = inode_ref->fs;
711 #if CONFIG_EXTENT_ENABLE
712 /* For extents must be data block destroyed by other way */
713 if ((ext4_sb_has_feature_incompatible(&fs->sb,
714 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
715 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
716 /* Data structures are released during truncate operation... */
720 /* Release all indirect (no data) blocks */
722 /* 1) Single indirect */
723 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
725 int rc = ext4_balloc_free_block(inode_ref, fblock);
729 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
732 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
733 uint32_t count = block_size / sizeof(uint32_t);
735 struct ext4_block block;
737 /* 2) Double indirect */
738 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
740 int rc = ext4_block_get(fs->bdev, &block, fblock);
745 for (offset = 0; offset < count; ++offset) {
746 ind_block = to_le32(((uint32_t *)block.data)[offset]);
748 if (ind_block != 0) {
749 rc = ext4_balloc_free_block(inode_ref,
752 ext4_block_set(fs->bdev, &block);
758 ext4_block_set(fs->bdev, &block);
759 rc = ext4_balloc_free_block(inode_ref, fblock);
763 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
766 /* 3) Tripple indirect */
767 struct ext4_block subblock;
768 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
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_block_get(fs->bdev, &subblock,
782 ext4_block_set(fs->bdev, &block);
786 uint32_t ind_subblock;
787 for (suboffset = 0; suboffset < count;
789 ind_subblock = to_le32(
791 subblock.data)[suboffset]);
793 if (ind_subblock != 0) {
794 rc = ext4_balloc_free_block(
795 inode_ref, ind_subblock);
800 ext4_block_set(fs->bdev,
807 ext4_block_set(fs->bdev, &subblock);
809 rc = ext4_balloc_free_block(inode_ref,
812 ext4_block_set(fs->bdev, &block);
818 ext4_block_set(fs->bdev, &block);
819 rc = ext4_balloc_free_block(inode_ref, fblock);
823 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
825 #if CONFIG_EXTENT_ENABLE
828 /* Mark inode dirty for writing to the physical device */
829 inode_ref->dirty = true;
831 /* Free block with extended attributes if present */
832 uint32_t xattr_block =
833 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
835 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
839 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
842 /* Free inode by allocator */
844 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
845 EXT4_INODE_MODE_DIRECTORY))
846 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
848 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
853 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
855 struct ext4_sblock *sb = &inode_ref->fs->sb;
858 /* Check flags, if i-node can be truncated */
859 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
862 /* If sizes are equal, nothing has to be done. */
863 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
864 if (old_size == new_size)
867 /* It's not supported to make the larger file by truncate operation */
868 if (old_size < new_size)
871 /* Compute how many blocks will be released */
872 uint64_t size_diff = old_size - new_size;
873 uint32_t block_size = ext4_sb_get_block_size(sb);
874 uint32_t diff_blocks_count = size_diff / block_size;
875 if (size_diff % block_size != 0)
878 uint32_t old_blocks_count = old_size / block_size;
879 if (old_size % block_size != 0)
881 #if CONFIG_EXTENT_ENABLE
882 if ((ext4_sb_has_feature_incompatible(sb,
883 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
884 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
886 /* Extents require special operation */
887 int rc = ext4_extent_release_blocks_from(
888 inode_ref, old_blocks_count - diff_blocks_count);
894 /* Release data blocks from the end of file */
896 /* Starting from 1 because of logical blocks are numbered from 0
898 for (i = 1; i <= diff_blocks_count; ++i) {
899 int rc = ext4_fs_release_inode_block(
900 inode_ref, old_blocks_count - i);
907 ext4_inode_set_size(inode_ref->inode, new_size);
908 inode_ref->dirty = true;
913 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
914 uint64_t iblock, uint32_t *fblock)
916 struct ext4_fs *fs = inode_ref->fs;
918 /* For empty file is situation simple */
919 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
924 uint32_t current_block;
925 #if CONFIG_EXTENT_ENABLE
926 /* Handle i-node using extents */
927 if ((ext4_sb_has_feature_incompatible(&fs->sb,
928 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
929 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
932 ext4_extent_find_block(inode_ref, iblock, ¤t_block);
936 *fblock = current_block;
941 struct ext4_inode *inode = inode_ref->inode;
943 /* Direct block are read directly from array in i-node structure */
944 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
946 ext4_inode_get_direct_block(inode, (uint32_t)iblock);
947 *fblock = current_block;
951 /* Determine indirection level of the target block */
952 unsigned int level = 0;
954 for (i = 1; i < 4; i++) {
955 if (iblock < fs->inode_block_limits[i]) {
964 /* Compute offsets for the topmost level */
965 uint64_t block_offset_in_level =
966 iblock - fs->inode_block_limits[level - 1];
967 current_block = ext4_inode_get_indirect_block(inode, level - 1);
968 uint32_t offset_in_block =
969 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
972 if (current_block == 0) {
977 struct ext4_block block;
980 * Navigate through other levels, until we find the block number
981 * or find null reference meaning we are dealing with sparse file
984 /* Load indirect block */
985 int rc = ext4_block_get(fs->bdev, &block, current_block);
989 /* Read block address from indirect block */
991 to_le32(((uint32_t *)block.data)[offset_in_block]);
993 /* Put back indirect block untouched */
994 rc = ext4_block_set(fs->bdev, &block);
998 /* Check for sparse file */
999 if (current_block == 0) {
1004 /* Jump to the next level */
1007 /* Termination condition - we have address of data block loaded
1012 /* Visit the next level */
1013 block_offset_in_level %= fs->inode_blocks_per_level[level];
1014 offset_in_block = block_offset_in_level /
1015 fs->inode_blocks_per_level[level - 1];
1018 *fblock = current_block;
1023 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1024 uint64_t iblock, uint32_t fblock)
1026 struct ext4_fs *fs = inode_ref->fs;
1028 #if CONFIG_EXTENT_ENABLE
1029 /* Handle inode using extents */
1030 if ((ext4_sb_has_feature_incompatible(&fs->sb,
1031 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1032 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1038 /* Handle simple case when we are dealing with direct reference */
1039 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1040 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1042 inode_ref->dirty = true;
1047 /* Determine the indirection level needed to get the desired block */
1048 unsigned int level = 0;
1050 for (i = 1; i < 4; i++) {
1051 if (iblock < fs->inode_block_limits[i]) {
1060 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1062 /* Compute offsets for the topmost level */
1063 uint64_t block_offset_in_level =
1064 iblock - fs->inode_block_limits[level - 1];
1065 uint32_t current_block =
1066 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1067 uint32_t offset_in_block =
1068 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1070 uint32_t new_block_addr;
1072 struct ext4_block block;
1073 struct ext4_block new_block;
1075 /* Is needed to allocate indirect block on the i-node level */
1076 if (current_block == 0) {
1077 /* Allocate new indirect block */
1078 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1083 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1085 inode_ref->dirty = true;
1087 /* Load newly allocated block */
1088 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1090 ext4_balloc_free_block(inode_ref, new_block_addr);
1094 /* Initialize new block */
1095 memset(new_block.data, 0, block_size);
1096 new_block.dirty = true;
1098 /* Put back the allocated block */
1099 rc = ext4_block_set(fs->bdev, &new_block);
1103 current_block = new_block_addr;
1107 * Navigate through other levels, until we find the block number
1108 * or find null reference meaning we are dealing with sparse file
1111 int rc = ext4_block_get(fs->bdev, &block, current_block);
1116 to_le32(((uint32_t *)block.data)[offset_in_block]);
1118 if ((level > 1) && (current_block == 0)) {
1119 /* Allocate new block */
1121 ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1123 ext4_block_set(fs->bdev, &block);
1127 /* Load newly allocated block */
1128 rc = ext4_block_get(fs->bdev, &new_block,
1132 ext4_block_set(fs->bdev, &block);
1136 /* Initialize allocated block */
1137 memset(new_block.data, 0, block_size);
1138 new_block.dirty = true;
1140 rc = ext4_block_set(fs->bdev, &new_block);
1142 ext4_block_set(fs->bdev, &block);
1146 /* Write block address to the parent */
1147 ((uint32_t *)block.data)[offset_in_block] =
1148 to_le32(new_block_addr);
1150 current_block = new_block_addr;
1153 /* Will be finished, write the fblock address */
1155 ((uint32_t *)block.data)[offset_in_block] =
1160 rc = ext4_block_set(fs->bdev, &block);
1167 * If we are on the last level, break here as
1168 * there is no next level to visit
1173 /* Visit the next level */
1174 block_offset_in_level %= fs->inode_blocks_per_level[level];
1175 offset_in_block = block_offset_in_level /
1176 fs->inode_blocks_per_level[level - 1];
1182 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1187 struct ext4_fs *fs = inode_ref->fs;
1189 /* Extents are handled otherwise = there is not support in this function
1192 ext4_sb_has_feature_incompatible(&fs->sb,
1193 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1194 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1196 struct ext4_inode *inode = inode_ref->inode;
1198 /* Handle simple case when we are dealing with direct reference */
1199 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1200 fblock = ext4_inode_get_direct_block(inode, iblock);
1206 ext4_inode_set_direct_block(inode, iblock, 0);
1207 return ext4_balloc_free_block(inode_ref, fblock);
1210 /* Determine the indirection level needed to get the desired block */
1211 unsigned int level = 0;
1213 for (i = 1; i < 4; i++) {
1214 if (iblock < fs->inode_block_limits[i]) {
1223 /* Compute offsets for the topmost level */
1224 uint64_t block_offset_in_level =
1225 iblock - fs->inode_block_limits[level - 1];
1226 uint32_t current_block =
1227 ext4_inode_get_indirect_block(inode, level - 1);
1228 uint32_t offset_in_block =
1229 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1232 * Navigate through other levels, until we find the block number
1233 * or find null reference meaning we are dealing with sparse file
1235 struct ext4_block block;
1240 if (current_block == 0)
1243 int rc = ext4_block_get(fs->bdev, &block, current_block);
1248 to_le32(((uint32_t *)block.data)[offset_in_block]);
1250 /* Set zero if physical data block address found */
1252 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1256 rc = ext4_block_set(fs->bdev, &block);
1263 * If we are on the last level, break here as
1264 * there is no next level to visit
1269 /* Visit the next level */
1270 block_offset_in_level %= fs->inode_blocks_per_level[level];
1271 offset_in_block = block_offset_in_level /
1272 fs->inode_blocks_per_level[level - 1];
1275 fblock = current_block;
1279 /* Physical block is not referenced, it can be released */
1280 return ext4_balloc_free_block(inode_ref, fblock);
1283 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1284 uint32_t *fblock, uint32_t *iblock)
1286 #if CONFIG_EXTENT_ENABLE
1287 /* Handle extents separately */
1288 if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1289 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1290 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1291 return ext4_extent_append_block(inode_ref, iblock, fblock,
1295 struct ext4_sblock *sb = &inode_ref->fs->sb;
1297 /* Compute next block index and allocate data block */
1298 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1299 uint32_t block_size = ext4_sb_get_block_size(sb);
1301 /* Align size i-node size */
1302 if ((inode_size % block_size) != 0)
1303 inode_size += block_size - (inode_size % block_size);
1305 /* Logical blocks are numbered from 0 */
1306 uint32_t new_block_idx = inode_size / block_size;
1308 /* Allocate new physical block */
1309 uint32_t phys_block;
1310 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1314 /* Add physical block address to the i-node */
1315 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1318 ext4_balloc_free_block(inode_ref, phys_block);
1323 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1324 inode_ref->dirty = true;
1326 *fblock = phys_block;
1327 *iblock = new_block_idx;
1332 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1336 link = ext4_inode_get_links_count(inode_ref->inode);
1338 ext4_inode_set_links_count(inode_ref->inode, link);
1341 ext4_sb_has_feature_compatible(&inode_ref->fs->sb,
1342 EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1343 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1345 if (is_dx && link > 1) {
1346 if (link >= EXT4_LINK_MAX || link == 2) {
1347 ext4_inode_set_links_count(inode_ref->inode, 1);
1350 ext4_get32(&inode_ref->fs->sb, features_read_only);
1351 v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1352 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1357 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1359 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1360 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1361 EXT4_INODE_MODE_DIRECTORY)) {
1363 ext4_inode_set_links_count(inode_ref->inode, links - 1);
1368 ext4_inode_set_links_count(inode_ref->inode, links - 1);