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] =
93 fs->inode_blocks_per_level[i - 1] * blocks_id;
94 fs->inode_block_limits[i] =
95 fs->inode_block_limits[i - 1] + 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, "last umount error\n");
104 /* Mark system as mounted */
105 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
106 r = ext4_sb_write(fs->bdev, &fs->sb);
110 /*Update mount count*/
111 ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
116 int ext4_fs_fini(struct ext4_fs *fs)
120 /*Set superblock state*/
121 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
123 return ext4_sb_write(fs->bdev, &fs->sb);
126 static void ext4_fs_debug_features_incomp(uint32_t features_incompatible)
129 if (features_incompatible & EXT4_FEATURE_INCOMPAT_COMPRESSION) {
130 ext4_dprintf(EXT4_DEBUG_FS, "compression\n");
132 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FILETYPE) {
133 ext4_dprintf(EXT4_DEBUG_FS, "filetype\n");
135 if (features_incompatible & EXT4_FEATURE_INCOMPAT_RECOVER) {
136 ext4_dprintf(EXT4_DEBUG_FS, "recover\n");
138 if (features_incompatible & EXT4_FEATURE_INCOMPAT_JOURNAL_DEV) {
139 ext4_dprintf(EXT4_DEBUG_FS, "journal_dev\n");
141 if (features_incompatible & EXT4_FEATURE_INCOMPAT_META_BG) {
142 ext4_dprintf(EXT4_DEBUG_FS, "meta_bg\n");
144 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EXTENTS) {
145 ext4_dprintf(EXT4_DEBUG_FS, "extents\n");
147 if (features_incompatible & EXT4_FEATURE_INCOMPAT_64BIT) {
148 ext4_dprintf(EXT4_DEBUG_FS, "64bit\n");
150 if (features_incompatible & EXT4_FEATURE_INCOMPAT_MMP) {
151 ext4_dprintf(EXT4_DEBUG_FS, "mnp\n");
153 if (features_incompatible & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
154 ext4_dprintf(EXT4_DEBUG_FS, "flex_bg\n");
156 if (features_incompatible & EXT4_FEATURE_INCOMPAT_EA_INODE) {
157 ext4_dprintf(EXT4_DEBUG_FS, "ea_inode\n");
159 if (features_incompatible & EXT4_FEATURE_INCOMPAT_DIRDATA) {
160 ext4_dprintf(EXT4_DEBUG_FS, "dirdata\n");
162 if (features_incompatible & EXT4_FEATURE_INCOMPAT_BG_USE_META_CSUM) {
163 ext4_dprintf(EXT4_DEBUG_FS, "meta_csum\n");
165 if (features_incompatible & EXT4_FEATURE_INCOMPAT_LARGEDIR) {
166 ext4_dprintf(EXT4_DEBUG_FS, "largedir\n");
168 if (features_incompatible & EXT4_FEATURE_INCOMPAT_INLINE_DATA) {
169 ext4_dprintf(EXT4_DEBUG_FS, "inline_data\n");
172 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
174 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_PREALLOC) {
175 ext4_dprintf(EXT4_DEBUG_FS, " dir_prealloc\n");
177 if (features_compatible & EXT4_FEATURE_COMPAT_IMAGIC_INODES) {
178 ext4_dprintf(EXT4_DEBUG_FS, "imagic_inodes\n");
180 if (features_compatible & EXT4_FEATURE_COMPAT_HAS_JOURNAL) {
181 ext4_dprintf(EXT4_DEBUG_FS, "has_journal\n");
183 if (features_compatible & EXT4_FEATURE_COMPAT_EXT_ATTR) {
184 ext4_dprintf(EXT4_DEBUG_FS, "ext_attr\n");
186 if (features_compatible & EXT4_FEATURE_COMPAT_RESIZE_INODE) {
187 ext4_dprintf(EXT4_DEBUG_FS, "resize_inode\n");
189 if (features_compatible & EXT4_FEATURE_COMPAT_DIR_INDEX) {
190 ext4_dprintf(EXT4_DEBUG_FS, "dir_index\n");
194 static void ext4_fs_debug_features_ro(uint32_t features_ro)
196 if (features_ro & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) {
197 ext4_dprintf(EXT4_DEBUG_FS, "sparse_super\n");
199 if (features_ro & EXT4_FEATURE_RO_COMPAT_LARGE_FILE) {
200 ext4_dprintf(EXT4_DEBUG_FS, "large_file\n");
202 if (features_ro & EXT4_FEATURE_RO_COMPAT_BTREE_DIR) {
203 ext4_dprintf(EXT4_DEBUG_FS, "btree_dir\n");
205 if (features_ro & EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
206 ext4_dprintf(EXT4_DEBUG_FS, "huge_file\n");
208 if (features_ro & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
209 ext4_dprintf(EXT4_DEBUG_FS, "gtd_csum\n");
211 if (features_ro & EXT4_FEATURE_RO_COMPAT_DIR_NLINK) {
212 ext4_dprintf(EXT4_DEBUG_FS, "dir_nlink\n");
214 if (features_ro & EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE) {
215 ext4_dprintf(EXT4_DEBUG_FS, "extra_isize\n");
217 if (features_ro & EXT4_FEATURE_RO_COMPAT_QUOTA) {
218 ext4_dprintf(EXT4_DEBUG_FS, "quota\n");
220 if (features_ro & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
221 ext4_dprintf(EXT4_DEBUG_FS, "bigalloc\n");
223 if (features_ro & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
224 ext4_dprintf(EXT4_DEBUG_FS, "metadata_csum\n");
228 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
230 ext4_assert(fs && read_only);
232 if (ext4_get32(&fs->sb, rev_level) == 0) {
237 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_incompatible:\n");
238 ext4_fs_debug_features_incomp(ext4_get32(&fs->sb, features_incompatible));
240 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_compatible:\n");
241 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
243 ext4_dprintf(EXT4_DEBUG_FS, "\nSB features_read_only:\n");
244 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
246 /*Check features_incompatible*/
247 v = (ext4_get32(&fs->sb, features_incompatible) &
248 (~CONFIG_FEATURE_INCOMPAT_SUPP));
250 ext4_dprintf(EXT4_DEBUG_FS, "SB features_incompatible: fail\n");
251 ext4_fs_debug_features_incomp(v);
255 /*Check features_read_only*/
256 v = (ext4_get32(&fs->sb, features_read_only) &
257 (~CONFIG_FEATURE_RO_COMPAT_SUPP));
259 ext4_dprintf(EXT4_DEBUG_FS,
260 "\nERROR sblock features_read_only . Unsupported:\n");
261 ext4_fs_debug_features_incomp(v);
271 /**@brief Initialize block bitmap in block group.
272 * @param bg_ref Reference to block group
275 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
278 uint32_t bitmap_block_addr =
279 ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
281 struct ext4_block block_bitmap;
282 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
286 memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
288 /* Determine first block and first data block in group */
289 uint32_t first_idx = 0;
291 uint32_t first_data =
292 ext4_balloc_get_first_data_block_in_group(&bg_ref->fs->sb, bg_ref);
293 uint32_t first_data_idx =
294 ext4_fs_baddr2_index_in_group(&bg_ref->fs->sb, first_data);
296 /*Set bits from to first block to first data block - 1 to one (allocated)*/
297 /*TODO: Optimize it*/
298 for (i = first_idx; i < first_data_idx; ++i)
299 ext4_bmap_bit_set(block_bitmap.data, i);
301 block_bitmap.dirty = true;
304 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
307 /**@brief Initialize i-node bitmap in block group.
308 * @param bg_ref Reference to block group
311 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
314 uint32_t bitmap_block_addr =
315 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
317 struct ext4_block block_bitmap;
318 int rc = ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
322 /* Initialize all bitmap bits to zero */
323 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
324 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
326 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
328 uint32_t start_bit = inodes_per_group;
329 uint32_t end_bit = block_size * 8;
332 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
333 ext4_bmap_bit_set(block_bitmap.data, i);
336 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
338 block_bitmap.dirty = true;
341 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
344 /**@brief Initialize i-node table in block group.
345 * @param bg_ref Reference to block group
348 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
350 struct ext4_sblock *sb = &bg_ref->fs->sb;
352 uint32_t inode_size = ext4_get32(sb, inode_size);
353 uint32_t block_size = ext4_sb_get_block_size(sb);
354 uint32_t inodes_per_block = block_size / inode_size;
355 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
356 uint32_t table_blocks = inodes_in_group / inodes_per_block;
359 if (inodes_in_group % inodes_per_block)
362 /* Compute initialization bounds */
363 uint32_t first_block =
364 ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
366 uint32_t last_block = first_block + table_blocks - 1;
368 /* Initialization of all itable blocks */
369 for (fblock = first_block; fblock <= last_block; ++fblock) {
371 struct ext4_block block;
372 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
376 memset(block.data, 0, block_size);
379 ext4_block_set(bg_ref->fs->bdev, &block);
387 static uint64_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
389 uint32_t dsc_per_block)
391 uint32_t first_meta_bg, dsc_id;
395 dsc_id = bgid / dsc_per_block;
396 first_meta_bg = ext4_sb_first_meta_bg(s);
398 if (!ext4_sb_has_feature_incompatible(s, EXT4_FEATURE_INCOMPAT_META_BG) ||
399 dsc_id < first_meta_bg)
400 return ext4_get32(s, first_data_block) + dsc_id + 1;
402 if (ext4_sb_is_super_in_bg(s, bgid))
405 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
408 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
409 struct ext4_block_group_ref *ref)
411 /* Compute number of descriptors, that fits in one data block */
412 uint32_t dsc_per_block =
413 ext4_sb_get_block_size(&fs->sb) / ext4_sb_get_desc_size(&fs->sb);
415 /* Block group descriptor table starts at the next block after superblock */
417 ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_per_block);
419 uint32_t offset = (bgid % dsc_per_block) * ext4_sb_get_desc_size(&fs->sb);
421 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
425 ref->block_group = (void *)(ref->block.data + offset);
430 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
431 rc = ext4_fs_init_block_bitmap(ref);
433 ext4_block_set(fs->bdev, &ref->block);
436 ext4_bg_clear_flag(ref->block_group, EXT4_BLOCK_GROUP_BLOCK_UNINIT);
441 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
442 rc = ext4_fs_init_inode_bitmap(ref);
444 ext4_block_set(ref->fs->bdev, &ref->block);
448 ext4_bg_clear_flag(ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT);
450 if (!ext4_bg_has_flag(ref->block_group,
451 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
452 rc = ext4_fs_init_inode_table(ref);
454 ext4_block_set(fs->bdev, &ref->block);
458 ext4_bg_set_flag(ref->block_group, EXT4_BLOCK_GROUP_ITABLE_ZEROED);
467 /**@brief Compute checksum of block group descriptor.
468 * @param sb Superblock
469 * @param bgid Index of block group in the filesystem
470 * @param bg Block group to compute checksum for
471 * @return Checksum value
473 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
474 struct ext4_bgroup *bg)
476 /* If checksum not supported, 0 will be returned */
479 /* Compute the checksum only if the filesystem supports it */
480 if (ext4_sb_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
481 uint8_t *base = (uint8_t *)bg;
482 uint8_t *checksum = (uint8_t *)&bg->checksum;
484 uint32_t offset = (uint32_t)(checksum - base);
486 /* Convert block group index to little endian */
487 uint32_t le_group = to_le32(bgid);
490 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
492 /* Include index of block group */
493 crc = ext4_bg_crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
495 /* Compute crc from the first part (stop before checksum field) */
496 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
499 offset += sizeof(bg->checksum);
501 /* Checksum of the rest of block group descriptor */
502 if ((ext4_sb_has_feature_incompatible(sb,
503 EXT4_FEATURE_INCOMPAT_64BIT)) &&
504 (offset < ext4_sb_get_desc_size(sb)))
506 crc = ext4_bg_crc16(crc, ((uint8_t *)bg) + offset,
507 ext4_sb_get_desc_size(sb) - offset);
512 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
514 /* Check if reference modified */
516 /* Compute new checksum of block group */
518 ext4_fs_bg_checksum(&ref->fs->sb, ref->index, ref->block_group);
520 ref->block_group->checksum = to_le16(checksum);
522 /* Mark block dirty for writing changes to physical device */
523 ref->block.dirty = true;
526 /* Put back block, that contains block group descriptor */
527 return ext4_block_set(ref->fs->bdev, &ref->block);
530 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
531 struct ext4_inode_ref *ref)
533 /* Compute number of i-nodes, that fits in one data block */
534 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
537 * Inode numbers are 1-based, but it is simpler to work with 0-based
538 * when computing indices
541 uint32_t block_group = index / inodes_per_group;
542 uint32_t offset_in_group = index % inodes_per_group;
544 /* Load block group, where i-node is located */
545 struct ext4_block_group_ref bg_ref;
547 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
552 /* Load block address, where i-node table is located */
553 uint32_t inode_table_start =
554 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
556 /* Put back block group reference (not needed more) */
557 rc = ext4_fs_put_block_group_ref(&bg_ref);
562 /* Compute position of i-node in the block group */
563 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
564 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
565 uint32_t byte_offset_in_group = offset_in_group * inode_size;
567 /* Compute block address */
568 uint64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
570 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
575 /* Compute position of i-node in the data block */
576 uint32_t offset_in_block = byte_offset_in_group % block_size;
577 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
579 /* We need to store the original value of index in the reference */
580 ref->index = index + 1;
587 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
589 /* Check if reference modified */
591 /* Mark block dirty for writing changes to physical device */
592 ref->block.dirty = true;
595 /* Put back block, that contains i-node */
596 return ext4_block_set(ref->fs->bdev, &ref->block);
599 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
602 /* Check if newly allocated i-node will be a directory */
606 is_dir = is_directory;
608 /* Allocate inode by allocation algorithm */
610 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
614 /* Load i-node from on-disk i-node table */
615 rc = ext4_fs_get_inode_ref(fs, index, inode_ref);
617 ext4_ialloc_free_inode(fs, index, is_dir);
621 /* Initialize i-node */
622 struct ext4_inode *inode = inode_ref->inode;
627 * Default directory permissions to be compatible with other systems
628 * 0777 (octal) == rwxrwxrwx
632 mode |= EXT4_INODE_MODE_DIRECTORY;
633 ext4_inode_set_mode(&fs->sb, inode, mode);
634 ext4_inode_set_links_count(inode, 0);
638 * Default file permissions to be compatible with other systems
639 * 0666 (octal) == rw-rw-rw-
643 mode |= EXT4_INODE_MODE_FILE;
644 ext4_inode_set_mode(&fs->sb, inode, mode);
645 ext4_inode_set_links_count(inode, 1);
648 ext4_inode_set_uid(inode, 0);
649 ext4_inode_set_gid(inode, 0);
650 ext4_inode_set_size(inode, 0);
651 ext4_inode_set_access_time(inode, 0);
652 ext4_inode_set_change_inode_time(inode, 0);
653 ext4_inode_set_modification_time(inode, 0);
654 ext4_inode_set_deletion_time(inode, 0);
655 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
656 ext4_inode_set_flags(inode, 0);
657 ext4_inode_set_generation(inode, 0);
659 /* Reset blocks array */
660 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
661 inode->blocks[i] = 0;
663 #if CONFIG_EXTENT_ENABLE
664 /* Initialize extents if needed */
665 if (ext4_sb_has_feature_incompatible(&fs->sb,
666 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
667 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
669 /* Initialize extent root header */
670 struct ext4_extent_header *header = ext4_inode_get_extent_header(inode);
671 ext4_extent_header_set_depth(header, 0);
672 ext4_extent_header_set_entries_count(header, 0);
673 ext4_extent_header_set_generation(header, 0);
674 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
676 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
677 sizeof(struct ext4_extent_header)) /
678 sizeof(struct ext4_extent);
680 ext4_extent_header_set_max_entries_count(header, max_entries);
684 inode_ref->dirty = true;
689 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
691 struct ext4_fs *fs = inode_ref->fs;
694 #if CONFIG_EXTENT_ENABLE
695 /* For extents must be data block destroyed by other way */
696 if ((ext4_sb_has_feature_incompatible(&fs->sb,
697 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
698 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
699 /* Data structures are released during truncate operation... */
703 /* Release all indirect (no data) blocks */
705 /* 1) Single indirect */
706 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
708 int rc = ext4_balloc_free_block(inode_ref, fblock);
712 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
715 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
716 uint32_t count = block_size / sizeof(uint32_t);
718 struct ext4_block block;
720 /* 2) Double indirect */
721 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
723 int rc = ext4_block_get(fs->bdev, &block, fblock);
728 for (offset = 0; offset < count; ++offset) {
729 ind_block = to_le32(((uint32_t *)block.data)[offset]);
731 if (ind_block != 0) {
732 rc = ext4_balloc_free_block(inode_ref, ind_block);
734 ext4_block_set(fs->bdev, &block);
740 ext4_block_set(fs->bdev, &block);
741 rc = ext4_balloc_free_block(inode_ref, fblock);
745 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
748 /* 3) Tripple indirect */
749 struct ext4_block subblock;
750 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
752 int rc = ext4_block_get(fs->bdev, &block, fblock);
757 for (offset = 0; offset < count; ++offset) {
758 ind_block = to_le32(((uint32_t *)block.data)[offset]);
760 if (ind_block != 0) {
761 rc = ext4_block_get(fs->bdev, &subblock, ind_block);
763 ext4_block_set(fs->bdev, &block);
767 uint32_t ind_subblock;
768 for (suboffset = 0; suboffset < count; ++suboffset) {
770 to_le32(((uint32_t *)subblock.data)[suboffset]);
772 if (ind_subblock != 0) {
773 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
775 ext4_block_set(fs->bdev, &subblock);
776 ext4_block_set(fs->bdev, &block);
782 ext4_block_set(fs->bdev, &subblock);
784 rc = ext4_balloc_free_block(inode_ref, ind_block);
786 ext4_block_set(fs->bdev, &block);
792 ext4_block_set(fs->bdev, &block);
793 rc = ext4_balloc_free_block(inode_ref, fblock);
797 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
799 #if CONFIG_EXTENT_ENABLE
802 /* Mark inode dirty for writing to the physical device */
803 inode_ref->dirty = true;
805 /* Free block with extended attributes if present */
806 uint32_t xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
808 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
812 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
815 /* Free inode by allocator */
817 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
818 EXT4_INODE_MODE_DIRECTORY))
819 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
821 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
826 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
828 struct ext4_sblock *sb = &inode_ref->fs->sb;
831 /* Check flags, if i-node can be truncated */
832 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
835 /* If sizes are equal, nothing has to be done. */
836 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
837 if (old_size == new_size)
840 /* It's not suppported to make the larger file by truncate operation */
841 if (old_size < new_size)
844 /* Compute how many blocks will be released */
845 uint64_t size_diff = old_size - new_size;
846 uint32_t block_size = ext4_sb_get_block_size(sb);
847 uint32_t diff_blocks_count = size_diff / block_size;
848 if (size_diff % block_size != 0)
851 uint32_t old_blocks_count = old_size / block_size;
852 if (old_size % block_size != 0)
854 #if CONFIG_EXTENT_ENABLE
855 if ((ext4_sb_has_feature_incompatible(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
856 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
858 /* Extents require special operation */
859 int rc = ext4_extent_release_blocks_from(
860 inode_ref, old_blocks_count - diff_blocks_count);
867 /* Release data blocks from the end of file */
869 /* Starting from 1 because of logical blocks are numbered from 0 */
870 for (i = 1; i <= diff_blocks_count; ++i) {
872 ext4_fs_release_inode_block(inode_ref, old_blocks_count - i);
879 ext4_inode_set_size(inode_ref->inode, new_size);
880 inode_ref->dirty = true;
885 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
886 uint64_t iblock, uint32_t *fblock)
888 struct ext4_fs *fs = inode_ref->fs;
890 /* For empty file is situation simple */
891 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
896 uint32_t current_block;
897 #if CONFIG_EXTENT_ENABLE
898 /* Handle i-node using extents */
899 if ((ext4_sb_has_feature_incompatible(&fs->sb,
900 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
901 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
903 int rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
907 *fblock = current_block;
912 struct ext4_inode *inode = inode_ref->inode;
914 /* Direct block are read directly from array in i-node structure */
915 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
916 current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
917 *fblock = current_block;
921 /* Determine indirection level of the target block */
922 unsigned int level = 0;
924 for (i = 1; i < 4; i++) {
925 if (iblock < fs->inode_block_limits[i]) {
934 /* Compute offsets for the topmost level */
935 uint64_t block_offset_in_level = iblock - fs->inode_block_limits[level - 1];
936 current_block = ext4_inode_get_indirect_block(inode, level - 1);
937 uint32_t offset_in_block =
938 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
941 if (current_block == 0) {
946 struct ext4_block block;
949 * Navigate through other levels, until we find the block number
950 * or find null reference meaning we are dealing with sparse file
953 /* Load indirect block */
954 int rc = ext4_block_get(fs->bdev, &block, current_block);
958 /* Read block address from indirect block */
959 current_block = to_le32(((uint32_t *)block.data)[offset_in_block]);
961 /* Put back indirect block untouched */
962 rc = ext4_block_set(fs->bdev, &block);
966 /* Check for sparse file */
967 if (current_block == 0) {
972 /* Jump to the next level */
975 /* Termination condition - we have address of data block loaded */
979 /* Visit the next level */
980 block_offset_in_level %= fs->inode_blocks_per_level[level];
982 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
985 *fblock = current_block;
990 int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
991 uint64_t iblock, uint32_t fblock)
993 struct ext4_fs *fs = inode_ref->fs;
995 #if CONFIG_EXTENT_ENABLE
996 /* Handle inode using extents */
997 if ((ext4_sb_has_feature_incompatible(&fs->sb,
998 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
999 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1005 /* Handle simple case when we are dealing with direct reference */
1006 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1007 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
1008 inode_ref->dirty = true;
1013 /* Determine the indirection level needed to get the desired block */
1014 unsigned int level = 0;
1016 for (i = 1; i < 4; i++) {
1017 if (iblock < fs->inode_block_limits[i]) {
1026 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1028 /* Compute offsets for the topmost level */
1029 uint64_t block_offset_in_level = iblock - fs->inode_block_limits[level - 1];
1030 uint32_t current_block =
1031 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1032 uint32_t offset_in_block =
1033 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1035 uint32_t new_block_addr;
1037 struct ext4_block block;
1038 struct ext4_block new_block;
1040 /* Is needed to allocate indirect block on the i-node level */
1041 if (current_block == 0) {
1042 /* Allocate new indirect block */
1043 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1048 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1050 inode_ref->dirty = true;
1052 /* Load newly allocated block */
1053 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1055 ext4_balloc_free_block(inode_ref, new_block_addr);
1059 /* Initialize new block */
1060 memset(new_block.data, 0, block_size);
1061 new_block.dirty = true;
1063 /* Put back the allocated block */
1064 rc = ext4_block_set(fs->bdev, &new_block);
1068 current_block = new_block_addr;
1072 * Navigate through other levels, until we find the block number
1073 * or find null reference meaning we are dealing with sparse file
1076 int rc = ext4_block_get(fs->bdev, &block, current_block);
1080 current_block = to_le32(((uint32_t *)block.data)[offset_in_block]);
1082 if ((level > 1) && (current_block == 0)) {
1083 /* Allocate new block */
1084 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1086 ext4_block_set(fs->bdev, &block);
1090 /* Load newly allocated block */
1091 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1094 ext4_block_set(fs->bdev, &block);
1098 /* Initialize allocated block */
1099 memset(new_block.data, 0, block_size);
1100 new_block.dirty = true;
1102 rc = ext4_block_set(fs->bdev, &new_block);
1104 ext4_block_set(fs->bdev, &block);
1108 /* Write block address to the parent */
1109 ((uint32_t *)block.data)[offset_in_block] = to_le32(new_block_addr);
1111 current_block = new_block_addr;
1114 /* Will be finished, write the fblock address */
1116 ((uint32_t *)block.data)[offset_in_block] = to_le32(fblock);
1120 rc = ext4_block_set(fs->bdev, &block);
1127 * If we are on the last level, break here as
1128 * there is no next level to visit
1133 /* Visit the next level */
1134 block_offset_in_level %= fs->inode_blocks_per_level[level];
1136 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1142 int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1147 struct ext4_fs *fs = inode_ref->fs;
1149 /* Extents are handled otherwise = there is not support in this function */
1151 !(ext4_sb_has_feature_incompatible(&fs->sb,
1152 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1153 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1155 struct ext4_inode *inode = inode_ref->inode;
1157 /* Handle simple case when we are dealing with direct reference */
1158 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1159 fblock = ext4_inode_get_direct_block(inode, iblock);
1165 ext4_inode_set_direct_block(inode, iblock, 0);
1166 return ext4_balloc_free_block(inode_ref, fblock);
1169 /* Determine the indirection level needed to get the desired block */
1170 unsigned int level = 0;
1172 for (i = 1; i < 4; i++) {
1173 if (iblock < fs->inode_block_limits[i]) {
1182 /* Compute offsets for the topmost level */
1183 uint64_t block_offset_in_level = iblock - fs->inode_block_limits[level - 1];
1184 uint32_t current_block = ext4_inode_get_indirect_block(inode, level - 1);
1185 uint32_t offset_in_block =
1186 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1189 * Navigate through other levels, until we find the block number
1190 * or find null reference meaning we are dealing with sparse file
1192 struct ext4_block block;
1197 if (current_block == 0)
1200 int rc = ext4_block_get(fs->bdev, &block, current_block);
1204 current_block = to_le32(((uint32_t *)block.data)[offset_in_block]);
1206 /* Set zero if physical data block address found */
1208 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1212 rc = ext4_block_set(fs->bdev, &block);
1219 * If we are on the last level, break here as
1220 * there is no next level to visit
1225 /* Visit the next level */
1226 block_offset_in_level %= fs->inode_blocks_per_level[level];
1228 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1231 fblock = current_block;
1235 /* Physical block is not referenced, it can be released */
1236 return ext4_balloc_free_block(inode_ref, fblock);
1239 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1240 uint32_t *fblock, uint32_t *iblock)
1242 #if CONFIG_EXTENT_ENABLE
1243 /* Handle extents separately */
1244 if ((ext4_sb_has_feature_incompatible(&inode_ref->fs->sb,
1245 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1246 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1247 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1250 struct ext4_sblock *sb = &inode_ref->fs->sb;
1252 /* Compute next block index and allocate data block */
1253 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1254 uint32_t block_size = ext4_sb_get_block_size(sb);
1256 /* Align size i-node size */
1257 if ((inode_size % block_size) != 0)
1258 inode_size += block_size - (inode_size % block_size);
1260 /* Logical blocks are numbered from 0 */
1261 uint32_t new_block_idx = inode_size / block_size;
1263 /* Allocate new physical block */
1264 uint32_t phys_block;
1265 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1269 /* Add physical block address to the i-node */
1270 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1273 ext4_balloc_free_block(inode_ref, phys_block);
1278 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1279 inode_ref->dirty = true;
1281 *fblock = phys_block;
1282 *iblock = new_block_idx;
1287 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1290 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1291 EXT4_INODE_MODE_DIRECTORY)) {
1292 ext4_inode_set_links_count(inode_ref->inode, 0);
1296 link = ext4_inode_get_links_count(inode_ref->inode);
1298 ext4_inode_set_links_count(inode_ref->inode, link);
1300 bool is_dx = ext4_sb_has_feature_compatible(
1301 &inode_ref->fs->sb, EXT4_FEATURE_COMPAT_DIR_INDEX) &&
1302 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1304 if (is_dx && link > 1) {
1305 if (link >= EXT4_LINK_MAX || link == 2) {
1306 ext4_inode_set_links_count(inode_ref->inode, 1);
1308 uint32_t v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1309 v |= EXT4_FEATURE_RO_COMPAT_DIR_NLINK;
1310 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1315 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1317 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1318 EXT4_INODE_MODE_DIRECTORY)) {
1319 ext4_inode_set_links_count(inode_ref->inode, 0);
1323 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1326 ext4_inode_set_links_count(inode_ref->inode, links - 1);