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_crc32c.h"
48 #include "ext4_debug.h"
49 #include "ext4_block_group.h"
50 #include "ext4_balloc.h"
51 #include "ext4_bitmap.h"
52 #include "ext4_inode.h"
53 #include "ext4_ialloc.h"
54 #include "ext4_extent.h"
58 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev)
63 bool read_only = false;
65 ext4_assert(fs && bdev);
69 r = ext4_sb_read(fs->bdev, &fs->sb);
73 if (!ext4_sb_check(&fs->sb))
76 bsize = ext4_sb_get_block_size(&fs->sb);
77 if (bsize > EXT4_MAX_BLOCK_SIZE)
80 r = ext4_fs_check_features(fs, &read_only);
87 /* Compute limits for indirect block levels */
88 uint32_t blocks_id = bsize / sizeof(uint32_t);
90 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
91 fs->inode_blocks_per_level[0] = 1;
93 for (i = 1; i < 4; i++) {
94 fs->inode_blocks_per_level[i] =
95 fs->inode_blocks_per_level[i - 1] * blocks_id;
96 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
97 fs->inode_blocks_per_level[i];
101 tmp = ext4_get16(&fs->sb, state);
102 if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS)
103 ext4_dbg(DEBUG_FS, DBG_WARN
104 "last umount error: superblock fs_error flag\n");
107 /* Mark system as mounted */
108 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
109 r = ext4_sb_write(fs->bdev, &fs->sb);
113 /*Update mount count*/
114 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_inc(uint32_t features_incompatible)
131 if (features_incompatible & EXT4_FINCOM_COMPRESSION)
132 ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
133 if (features_incompatible & EXT4_FINCOM_FILETYPE)
134 ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
135 if (features_incompatible & EXT4_FINCOM_RECOVER)
136 ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
137 if (features_incompatible & EXT4_FINCOM_JOURNAL_DEV)
138 ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
139 if (features_incompatible & EXT4_FINCOM_META_BG)
140 ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
141 if (features_incompatible & EXT4_FINCOM_EXTENTS)
142 ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
143 if (features_incompatible & EXT4_FINCOM_64BIT)
144 ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
145 if (features_incompatible & EXT4_FINCOM_MMP)
146 ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
147 if (features_incompatible & EXT4_FINCOM_FLEX_BG)
148 ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
149 if (features_incompatible & EXT4_FINCOM_EA_INODE)
150 ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
151 if (features_incompatible & EXT4_FINCOM_DIRDATA)
152 ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
153 if (features_incompatible & EXT4_FINCOM_BG_USE_META_CSUM)
154 ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
155 if (features_incompatible & EXT4_FINCOM_LARGEDIR)
156 ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
157 if (features_incompatible & EXT4_FINCOM_INLINE_DATA)
158 ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
160 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
162 if (features_compatible & EXT4_FCOM_DIR_PREALLOC)
163 ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
164 if (features_compatible & EXT4_FCOM_IMAGIC_INODES)
165 ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
166 if (features_compatible & EXT4_FCOM_HAS_JOURNAL)
167 ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
168 if (features_compatible & EXT4_FCOM_EXT_ATTR)
169 ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
170 if (features_compatible & EXT4_FCOM_RESIZE_INODE)
171 ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
172 if (features_compatible & EXT4_FCOM_DIR_INDEX)
173 ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
176 static void ext4_fs_debug_features_ro(uint32_t features_ro)
178 if (features_ro & EXT4_FRO_COM_SPARSE_SUPER)
179 ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
180 if (features_ro & EXT4_FRO_COM_LARGE_FILE)
181 ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
182 if (features_ro & EXT4_FRO_COM_BTREE_DIR)
183 ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
184 if (features_ro & EXT4_FRO_COM_HUGE_FILE)
185 ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
186 if (features_ro & EXT4_FRO_COM_GDT_CSUM)
187 ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
188 if (features_ro & EXT4_FRO_COM_DIR_NLINK)
189 ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
190 if (features_ro & EXT4_FRO_COM_EXTRA_ISIZE)
191 ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
192 if (features_ro & EXT4_FRO_COM_QUOTA)
193 ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
194 if (features_ro & EXT4_FRO_COM_BIGALLOC)
195 ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
196 if (features_ro & EXT4_FRO_COM_METADATA_CSUM)
197 ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
200 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
202 ext4_assert(fs && read_only);
204 if (ext4_get32(&fs->sb, rev_level) == 0) {
209 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
210 ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
212 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
213 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
215 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
216 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
218 /*Check features_incompatible*/
219 v = (ext4_get32(&fs->sb, features_incompatible) &
220 (~CONFIG_SUPPORTED_FINCOM));
222 ext4_dbg(DEBUG_FS, DBG_ERROR
223 "sblock has unsupported features incompatible:\n");
224 ext4_fs_debug_features_inc(v);
228 /*Check features_read_only*/
229 v = (ext4_get32(&fs->sb, features_read_only) &
230 (~CONFIG_SUPPORTED_FRO_COM));
232 ext4_dbg(DEBUG_FS, DBG_WARN
233 "sblock has unsupported features read only:\n");
234 ext4_fs_debug_features_ro(v);
243 /**@brief Determine whether the block is inside the group.
244 * @param baddr block address
245 * @param bgid block group id
248 static int ext4_block_in_group(struct ext4_sblock *s,
252 uint32_t actual_bgid;
253 actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
254 if (actual_bgid == bgid)
259 /**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only
260 * need to use it within a single byte (to ensure we get endianness right).
261 * We can use memset for the rest of the bitmap as there are no other users.
263 static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
267 if (start_bit >= end_bit)
270 for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
271 ext4_bmap_bit_set(bitmap, i);
274 memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
277 /**@brief Initialize block bitmap in block group.
278 * @param bg_ref Reference to block group
281 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
283 uint32_t i, bit, bit_max;
284 uint32_t group_blocks;
285 uint16_t inode_size = ext4_get16(&bg_ref->fs->sb, inode_size);
286 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
287 uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
288 ext4_fsblk_t bitmap_block_addr =
289 ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
290 ext4_fsblk_t bitmap_inode_addr =
291 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
292 ext4_fsblk_t inode_table_addr =
293 ext4_bg_get_inode_table_first_block(bg_ref->block_group,
295 ext4_fsblk_t first_group_addr =
296 ext4_balloc_get_block_of_bgid(&bg_ref->fs->sb, bg_ref->index);
298 uint32_t dsc_per_block =
299 ext4_sb_get_block_size(&bg_ref->fs->sb) /
300 ext4_sb_get_desc_size(&bg_ref->fs->sb);
303 ext4_sb_feature_incom(&bg_ref->fs->sb, EXT4_FINCOM_FLEX_BG);
305 uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
307 struct ext4_block block_bitmap;
309 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
313 memset(block_bitmap.data, 0, block_size);
315 bit_max = ext4_sb_is_super_in_bg(&bg_ref->fs->sb, bg_ref->index);
316 if (!ext4_sb_feature_incom(&bg_ref->fs->sb, EXT4_FINCOM_META_BG) ||
317 bg_ref->index < ext4_sb_first_meta_bg(&bg_ref->fs->sb) *
320 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
323 ext4_get16(&bg_ref->fs->sb,
324 s_reserved_gdt_blocks);
326 } else { /* For META_BG_BLOCK_GROUPS */
327 bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
330 for (bit = 0; bit < bit_max; bit++)
331 ext4_bmap_bit_set(block_bitmap.data, bit);
333 if (bg_ref->index == ext4_block_group_cnt(&bg_ref->fs->sb) - 1) {
335 * Even though mke2fs always initialize first and last group
336 * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
337 * to make sure we calculate the right free blocks
339 group_blocks = (ext4_sb_get_blocks_cnt(&bg_ref->fs->sb) -
340 ext4_get32(&bg_ref->fs->sb, first_data_block) -
341 (ext4_get32(&bg_ref->fs->sb, blocks_per_group) *
342 (ext4_block_group_cnt(&bg_ref->fs->sb) - 1)));
344 group_blocks = ext4_get32(&bg_ref->fs->sb, blocks_per_group);
347 ext4_block_in_group(&bg_ref->fs->sb,
348 bitmap_block_addr, bg_ref->index))
349 ext4_bmap_bit_set(block_bitmap.data,
350 bitmap_block_addr - first_group_addr);
353 ext4_block_in_group(&bg_ref->fs->sb,
354 bitmap_inode_addr, bg_ref->index))
355 ext4_bmap_bit_set(block_bitmap.data,
356 bitmap_inode_addr - first_group_addr);
358 for (i = inode_table_addr;
359 i < inode_table_addr + inode_table_bcnt; i++) {
361 ext4_block_in_group(&bg_ref->fs->sb,
364 ext4_bmap_bit_set(block_bitmap.data,
365 i - first_group_addr);
368 * Also if the number of blocks within the group is
369 * less than the blocksize * 8 ( which is the size
370 * of bitmap ), set rest of the block bitmap to 1
372 ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
373 block_bitmap.dirty = true;
375 ext4_balloc_set_bitmap_csum(&bg_ref->fs->sb,
378 bg_ref->dirty = true;
381 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
384 /**@brief Initialize i-node bitmap in block group.
385 * @param bg_ref Reference to block group
388 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
391 ext4_fsblk_t bitmap_block_addr =
392 ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
394 struct ext4_block block_bitmap;
396 ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
400 /* Initialize all bitmap bits to zero */
401 uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
402 uint32_t inodes_per_group =
403 ext4_get32(&bg_ref->fs->sb, inodes_per_group);
405 memset(block_bitmap.data, 0, (inodes_per_group + 7) / 8);
407 uint32_t start_bit = inodes_per_group;
408 uint32_t end_bit = block_size * 8;
411 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
412 ext4_bmap_bit_set(block_bitmap.data, i);
415 memset(block_bitmap.data + (i >> 3), 0xff, (end_bit - i) >> 3);
417 block_bitmap.dirty = true;
419 ext4_ialloc_set_bitmap_csum(&bg_ref->fs->sb,
422 bg_ref->dirty = true;
425 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
428 /**@brief Initialize i-node table in block group.
429 * @param bg_ref Reference to block group
432 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
434 struct ext4_sblock *sb = &bg_ref->fs->sb;
436 uint32_t inode_size = ext4_get32(sb, inode_size);
437 uint32_t block_size = ext4_sb_get_block_size(sb);
438 uint32_t inodes_per_block = block_size / inode_size;
439 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
440 uint32_t table_blocks = inodes_in_group / inodes_per_block;
443 if (inodes_in_group % inodes_per_block)
446 /* Compute initialization bounds */
447 ext4_fsblk_t first_block =
448 ext4_bg_get_inode_table_first_block(bg_ref->block_group, sb);
450 ext4_fsblk_t last_block = first_block + table_blocks - 1;
452 /* Initialization of all itable blocks */
453 for (fblock = first_block; fblock <= last_block; ++fblock) {
455 struct ext4_block block;
456 int rc = ext4_block_get(bg_ref->fs->bdev, &block, fblock);
460 memset(block.data, 0, block_size);
463 ext4_block_set(bg_ref->fs->bdev, &block);
471 static ext4_fsblk_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
473 uint32_t dsc_per_block)
475 uint32_t first_meta_bg, dsc_id;
479 dsc_id = bgid / dsc_per_block;
480 first_meta_bg = ext4_sb_first_meta_bg(s);
482 if (!ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG) ||
483 dsc_id < first_meta_bg)
484 return ext4_get32(s, first_data_block) + dsc_id + 1;
486 if (ext4_sb_is_super_in_bg(s, bgid))
489 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
492 /**@brief Compute checksum of block group descriptor.
493 * @param sb Superblock
494 * @param bgid Index of block group in the filesystem
495 * @param bg Block group to compute checksum for
496 * @return Checksum value
498 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
499 struct ext4_bgroup *bg)
501 /* If checksum not supported, 0 will be returned */
503 #if CONFIG_META_CSUM_ENABLE
504 /* Compute the checksum only if the filesystem supports it */
505 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
506 /* Use metadata_csum algorithm instead */
507 uint32_t le32_bgid = to_le32(bgid);
508 uint32_t orig_checksum, checksum;
510 /* Preparation: temporarily set bg checksum to 0 */
511 orig_checksum = bg->checksum;
514 /* First calculate crc32 checksum against fs uuid */
515 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
517 /* Then calculate crc32 checksum against bgid */
518 checksum = ext4_crc32c(checksum, &le32_bgid,
520 /* Finally calculate crc32 checksum against block_group_desc */
521 checksum = ext4_crc32c(checksum, bg,
522 ext4_sb_get_desc_size(sb));
523 bg->checksum = orig_checksum;
525 crc = checksum & 0xFFFF;
529 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_GDT_CSUM)) {
530 uint8_t *base = (uint8_t *)bg;
531 uint8_t *checksum = (uint8_t *)&bg->checksum;
533 uint32_t offset = (uint32_t)(checksum - base);
535 /* Convert block group index to little endian */
536 uint32_t le_group = to_le32(bgid);
539 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
541 /* Include index of block group */
543 ext4_bg_crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
545 /* Compute crc from the first part (stop before checksum field)
547 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
550 offset += sizeof(bg->checksum);
552 /* Checksum of the rest of block group descriptor */
553 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_64BIT)) &&
554 (offset < ext4_sb_get_desc_size(sb)))
556 crc = ext4_bg_crc16(crc, ((uint8_t *)bg) + offset,
557 ext4_sb_get_desc_size(sb) - offset);
562 #if CONFIG_META_CSUM_ENABLE
563 static bool ext4_fs_verify_bg_csum(struct ext4_sblock *sb,
565 struct ext4_bgroup *bg)
567 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
570 return ext4_fs_bg_checksum(sb,
573 to_le16(bg->checksum);
576 #define ext4_fs_verify_bg_csum(...) true
579 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
580 struct ext4_block_group_ref *ref)
582 /* Compute number of descriptors, that fits in one data block */
583 uint32_t dsc_per_block =
584 ext4_sb_get_block_size(&fs->sb) / ext4_sb_get_desc_size(&fs->sb);
586 /* Block group descriptor table starts at the next block after
589 ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_per_block);
592 (bgid % dsc_per_block) * ext4_sb_get_desc_size(&fs->sb);
594 int rc = ext4_block_get(fs->bdev, &ref->block, block_id);
598 ref->block_group = (void *)(ref->block.data + offset);
603 if (!ext4_fs_verify_bg_csum(&fs->sb,
607 DBG_WARN "Block group descriptor checksum failed."
608 "Block group index: %" PRIu32"\n",
612 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
613 rc = ext4_fs_init_block_bitmap(ref);
615 ext4_block_set(fs->bdev, &ref->block);
618 ext4_bg_clear_flag(ref->block_group,
619 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
624 if (ext4_bg_has_flag(ref->block_group, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
625 rc = ext4_fs_init_inode_bitmap(ref);
627 ext4_block_set(ref->fs->bdev, &ref->block);
631 ext4_bg_clear_flag(ref->block_group,
632 EXT4_BLOCK_GROUP_INODE_UNINIT);
634 if (!ext4_bg_has_flag(ref->block_group,
635 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
636 rc = ext4_fs_init_inode_table(ref);
638 ext4_block_set(fs->bdev, &ref->block);
642 ext4_bg_set_flag(ref->block_group,
643 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
652 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
654 /* Check if reference modified */
656 /* Compute new checksum of block group */
657 uint16_t checksum = ext4_fs_bg_checksum(
658 &ref->fs->sb, ref->index, ref->block_group);
660 ref->block_group->checksum = to_le16(checksum);
662 /* Mark block dirty for writing changes to physical device */
663 ref->block.dirty = true;
666 /* Put back block, that contains block group descriptor */
667 return ext4_block_set(ref->fs->bdev, &ref->block);
670 #if CONFIG_META_CSUM_ENABLE
671 static uint32_t ext4_fs_inode_checksum(struct ext4_inode_ref *inode_ref)
673 uint32_t checksum = 0;
674 struct ext4_sblock *sb = &inode_ref->fs->sb;
675 uint16_t inode_size = ext4_get16(sb, inode_size);
677 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
678 uint32_t orig_checksum;
680 uint32_t ino_index = to_le32(inode_ref->index);
682 to_le32(ext4_inode_get_generation(inode_ref->inode));
684 /* Preparation: temporarily set bg checksum to 0 */
685 orig_checksum = ext4_inode_get_checksum(sb, inode_ref->inode);
686 ext4_inode_set_checksum(sb, inode_ref->inode, 0);
688 /* First calculate crc32 checksum against fs uuid */
689 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
691 /* Then calculate crc32 checksum against inode number
692 * and inode generation */
693 checksum = ext4_crc32c(checksum, &ino_index,
695 checksum = ext4_crc32c(checksum, &ino_gen,
697 /* Finally calculate crc32 checksum against
698 * the entire inode */
699 checksum = ext4_crc32c(checksum, inode_ref->inode,
701 ext4_inode_set_checksum(sb, inode_ref->inode,
704 /* If inode size is not large enough to hold the
705 * upper 16bit of the checksum */
706 if (inode_size == EXT4_GOOD_OLD_INODE_SIZE)
713 #define ext4_fs_inode_checksum(...) 0
716 static void ext4_fs_set_inode_checksum(struct ext4_inode_ref *inode_ref)
718 struct ext4_sblock *sb = &inode_ref->fs->sb;
719 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
722 ext4_inode_set_checksum(sb, inode_ref->inode,
723 ext4_fs_inode_checksum(inode_ref));
726 #if CONFIG_META_CSUM_ENABLE
727 static bool ext4_fs_verify_inode_csum(struct ext4_inode_ref *inode_ref)
729 struct ext4_sblock *sb = &inode_ref->fs->sb;
730 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
733 return ext4_inode_get_checksum(sb, inode_ref->inode) ==
734 ext4_fs_inode_checksum(inode_ref);
737 #define ext4_fs_verify_inode_csum(...) true
741 __ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
742 struct ext4_inode_ref *ref,
745 /* Compute number of i-nodes, that fits in one data block */
746 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
749 * Inode numbers are 1-based, but it is simpler to work with 0-based
750 * when computing indices
753 uint32_t block_group = index / inodes_per_group;
754 uint32_t offset_in_group = index % inodes_per_group;
756 /* Load block group, where i-node is located */
757 struct ext4_block_group_ref bg_ref;
759 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
764 /* Load block address, where i-node table is located */
765 uint32_t inode_table_start =
766 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
768 /* Put back block group reference (not needed more) */
769 rc = ext4_fs_put_block_group_ref(&bg_ref);
774 /* Compute position of i-node in the block group */
775 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
776 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
777 uint32_t byte_offset_in_group = offset_in_group * inode_size;
779 /* Compute block address */
780 ext4_fsblk_t block_id =
781 inode_table_start + (byte_offset_in_group / block_size);
783 rc = ext4_block_get(fs->bdev, &ref->block, block_id);
788 /* Compute position of i-node in the data block */
789 uint32_t offset_in_block = byte_offset_in_group % block_size;
790 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
792 /* We need to store the original value of index in the reference */
793 ref->index = index + 1;
797 if (initialized && !ext4_fs_verify_inode_csum(ref)) {
799 DBG_WARN "Inode checksum failed."
800 "Inode: %" PRIu32"\n",
807 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
808 struct ext4_inode_ref *ref)
810 return __ext4_fs_get_inode_ref(fs, index, ref, true);
813 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
815 /* Check if reference modified */
817 /* Mark block dirty for writing changes to physical device */
818 ext4_fs_set_inode_checksum(ref);
819 ref->block.dirty = true;
822 /* Put back block, that contains i-node */
823 return ext4_block_set(ref->fs->bdev, &ref->block);
826 void ext4_fs_inode_blocks_init(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref)
829 struct ext4_inode *inode = inode_ref->inode;
831 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
832 inode->blocks[i] = 0;
835 #if CONFIG_EXTENT_ENABLE
836 /* Initialize extents if needed */
837 if (ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) {
838 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
840 /* Initialize extent root header */
841 ext4_extent_tree_init(inode_ref);
846 uint32_t ext4_fs_correspond_inode_mode(int filetype)
849 case EXT4_DIRENTRY_DIR:
850 return EXT4_INODE_MODE_DIRECTORY;
851 case EXT4_DIRENTRY_REG_FILE:
852 return EXT4_INODE_MODE_FILE;
853 case EXT4_DIRENTRY_SYMLINK:
854 return EXT4_INODE_MODE_SOFTLINK;
856 /* FIXME: right now we only support 3 file type. */
862 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
865 /* Check if newly allocated i-node will be a directory */
867 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
869 is_dir = (filetype == EXT4_DIRENTRY_DIR);
871 /* Allocate inode by allocation algorithm */
873 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
877 /* Load i-node from on-disk i-node table */
878 rc = __ext4_fs_get_inode_ref(fs, index, inode_ref, false);
880 ext4_ialloc_free_inode(fs, index, is_dir);
884 /* Initialize i-node */
885 struct ext4_inode *inode = inode_ref->inode;
890 * Default directory permissions to be compatible with other
892 * 0777 (octal) == rwxrwxrwx
896 mode |= EXT4_INODE_MODE_DIRECTORY;
899 * Default file permissions to be compatible with other systems
900 * 0666 (octal) == rw-rw-rw-
904 mode |= ext4_fs_correspond_inode_mode(filetype);
906 ext4_inode_set_mode(&fs->sb, inode, mode);
908 ext4_inode_set_links_count(inode, 0);
909 ext4_inode_set_uid(inode, 0);
910 ext4_inode_set_gid(inode, 0);
911 ext4_inode_set_size(inode, 0);
912 ext4_inode_set_access_time(inode, 0);
913 ext4_inode_set_change_inode_time(inode, 0);
914 ext4_inode_set_modification_time(inode, 0);
915 ext4_inode_set_deletion_time(inode, 0);
916 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
917 ext4_inode_set_flags(inode, 0);
918 ext4_inode_set_generation(inode, 0);
919 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE)
920 ext4_inode_set_extra_isize(inode,
921 sizeof(struct ext4_inode) -
922 offsetof(struct ext4_inode,
925 /* Reset blocks array. For symbolic link inode, just
926 * fill in blocks with 0 */
927 if (ext4_inode_is_type(&fs->sb, inode, EXT4_INODE_MODE_SOFTLINK)) {
928 for (int i = 0; i < EXT4_INODE_BLOCKS; i++)
929 inode->blocks[i] = 0;
932 ext4_fs_inode_blocks_init(fs, inode_ref);
934 inode_ref->dirty = true;
939 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
941 struct ext4_fs *fs = inode_ref->fs;
945 #if CONFIG_EXTENT_ENABLE
946 /* For extents must be data block destroyed by other way */
947 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
948 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
949 /* Data structures are released during truncate operation... */
953 /* Release all indirect (no data) blocks */
955 /* 1) Single indirect */
956 ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
958 int rc = ext4_balloc_free_block(inode_ref, fblock);
962 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
965 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
966 uint32_t count = block_size / sizeof(uint32_t);
968 struct ext4_block block;
970 /* 2) Double indirect */
971 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
973 int rc = ext4_block_get(fs->bdev, &block, fblock);
977 ext4_fsblk_t ind_block;
978 for (offset = 0; offset < count; ++offset) {
979 ind_block = to_le32(((uint32_t *)block.data)[offset]);
983 rc = ext4_balloc_free_block(inode_ref, ind_block);
985 ext4_block_set(fs->bdev, &block);
991 ext4_block_set(fs->bdev, &block);
992 rc = ext4_balloc_free_block(inode_ref, fblock);
996 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
999 /* 3) Tripple indirect */
1000 struct ext4_block subblock;
1001 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
1004 rc = ext4_block_get(fs->bdev, &block, fblock);
1008 ext4_fsblk_t ind_block;
1009 for (offset = 0; offset < count; ++offset) {
1010 ind_block = to_le32(((uint32_t *)block.data)[offset]);
1014 rc = ext4_block_get(fs->bdev, &subblock,
1017 ext4_block_set(fs->bdev, &block);
1021 ext4_fsblk_t ind_subblk;
1022 for (suboff = 0; suboff < count; ++suboff) {
1023 ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
1025 if (ind_subblk == 0)
1027 rc = ext4_balloc_free_block(inode_ref, ind_subblk);
1029 ext4_block_set(fs->bdev, &subblock);
1030 ext4_block_set(fs->bdev, &block);
1036 ext4_block_set(fs->bdev, &subblock);
1038 rc = ext4_balloc_free_block(inode_ref,
1041 ext4_block_set(fs->bdev, &block);
1047 ext4_block_set(fs->bdev, &block);
1048 rc = ext4_balloc_free_block(inode_ref, fblock);
1052 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
1054 /* Mark inode dirty for writing to the physical device */
1055 inode_ref->dirty = true;
1057 /* Free block with extended attributes if present */
1058 ext4_fsblk_t xattr_block =
1059 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
1061 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
1065 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
1068 /* Free inode by allocator */
1069 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
1070 EXT4_INODE_MODE_DIRECTORY))
1071 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
1073 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
1079 /**@brief Release data block from i-node
1080 * @param inode_ref I-node to release block from
1081 * @param iblock Logical block to be released
1082 * @return Error code
1084 static int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1087 ext4_fsblk_t fblock;
1089 struct ext4_fs *fs = inode_ref->fs;
1091 /* Extents are handled otherwise = there is not support in this function
1094 ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS) &&
1095 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1097 struct ext4_inode *inode = inode_ref->inode;
1099 /* Handle simple case when we are dealing with direct reference */
1100 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1101 fblock = ext4_inode_get_direct_block(inode, iblock);
1107 ext4_inode_set_direct_block(inode, iblock, 0);
1108 return ext4_balloc_free_block(inode_ref, fblock);
1111 /* Determine the indirection level needed to get the desired block */
1112 unsigned int level = 0;
1114 for (i = 1; i < 4; i++) {
1115 if (iblock < fs->inode_block_limits[i]) {
1124 /* Compute offsets for the topmost level */
1125 uint64_t block_offset_in_level =
1126 iblock - fs->inode_block_limits[level - 1];
1127 ext4_fsblk_t current_block =
1128 ext4_inode_get_indirect_block(inode, level - 1);
1129 uint32_t offset_in_block =
1130 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1133 * Navigate through other levels, until we find the block number
1134 * or find null reference meaning we are dealing with sparse file
1136 struct ext4_block block;
1141 if (current_block == 0)
1144 int rc = ext4_block_get(fs->bdev, &block, current_block);
1149 to_le32(((uint32_t *)block.data)[offset_in_block]);
1151 /* Set zero if physical data block address found */
1153 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1157 rc = ext4_block_set(fs->bdev, &block);
1164 * If we are on the last level, break here as
1165 * there is no next level to visit
1170 /* Visit the next level */
1171 block_offset_in_level %= fs->inode_blocks_per_level[level];
1172 offset_in_block = block_offset_in_level /
1173 fs->inode_blocks_per_level[level - 1];
1176 fblock = current_block;
1180 /* Physical block is not referenced, it can be released */
1181 return ext4_balloc_free_block(inode_ref, fblock);
1184 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
1186 struct ext4_sblock *sb = &inode_ref->fs->sb;
1189 /* Check flags, if i-node can be truncated */
1190 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
1193 /* If sizes are equal, nothing has to be done. */
1194 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
1195 if (old_size == new_size)
1198 /* It's not supported to make the larger file by truncate operation */
1199 if (old_size < new_size)
1202 if (ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK)
1203 && old_size < sizeof(inode_ref->inode->blocks)
1204 && !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
1205 char *content = (char *)inode_ref->inode->blocks;
1206 memset(content + new_size, 0,
1207 sizeof(inode_ref->inode->blocks) - new_size);
1208 ext4_inode_set_size(inode_ref->inode, new_size);
1209 inode_ref->dirty = true;
1214 /* Compute how many blocks will be released */
1215 uint32_t block_size = ext4_sb_get_block_size(sb);
1216 uint32_t new_blocks_count = (new_size + block_size - 1) /
1218 uint32_t old_blocks_count = (old_size + block_size - 1) /
1220 uint32_t diff_blocks_count = old_blocks_count - new_blocks_count;
1221 #if CONFIG_EXTENT_ENABLE
1222 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_EXTENTS)) &&
1223 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1225 /* Extents require special operation */
1226 if (diff_blocks_count) {
1227 int rc = ext4_extent_remove_space(inode_ref,
1228 new_blocks_count, EXT_MAX_BLOCKS);
1236 /* Release data blocks from the end of file */
1238 /* Starting from 1 because of logical blocks are numbered from 0
1240 for (i = 0; i < diff_blocks_count; ++i) {
1241 int rc = ext4_fs_release_inode_block(
1242 inode_ref, new_blocks_count + i);
1249 ext4_inode_set_size(inode_ref->inode, new_size);
1250 inode_ref->dirty = true;
1255 /**@brief Compute 'goal' for inode index
1256 * @param inode_ref Reference to inode, to allocate block for
1259 ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref)
1261 uint32_t group_inodes =
1262 ext4_get32(&inode_ref->fs->sb, inodes_per_group);
1263 return (inode_ref->index - 1) / group_inodes;
1266 /**@brief Compute 'goal' for allocation algorithm (For blockmap).
1267 * @param inode_ref Reference to inode, to allocate block for
1269 * @return error code
1271 int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
1274 struct ext4_sblock *sb = &inode_ref->fs->sb;
1277 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1278 uint32_t block_size = ext4_sb_get_block_size(sb);
1279 uint32_t inode_block_count = inode_size / block_size;
1281 if (inode_size % block_size != 0)
1282 inode_block_count++;
1284 /* If inode has some blocks, get last block address + 1 */
1285 if (inode_block_count > 0) {
1286 int rc = ext4_fs_get_inode_data_block_index(
1287 inode_ref, inode_block_count - 1, goal, false);
1296 /* If goal == 0, sparse file -> continue */
1299 /* Identify block group of inode */
1301 uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
1302 uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
1303 block_size = ext4_sb_get_block_size(sb);
1305 /* Load block group reference */
1306 struct ext4_block_group_ref bg_ref;
1308 ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
1312 /* Compute indexes */
1313 uint32_t block_group_count = ext4_block_group_cnt(sb);
1314 ext4_fsblk_t inode_table_first_block =
1315 ext4_bg_get_inode_table_first_block(bg_ref.block_group, sb);
1316 uint16_t inode_table_item_size = ext4_get16(sb, inode_size);
1317 uint32_t inode_table_bytes;
1319 /* Check for last block group */
1320 if (block_group < block_group_count - 1) {
1321 inode_table_bytes = inodes_per_group * inode_table_item_size;
1323 /* Last block group could be smaller */
1324 uint32_t inodes_count_total = ext4_get32(sb, inodes_count);
1327 (inodes_count_total -
1328 ((block_group_count - 1) * inodes_per_group)) *
1329 inode_table_item_size;
1332 ext4_fsblk_t inode_table_blocks = inode_table_bytes / block_size;
1334 if (inode_table_bytes % block_size)
1335 inode_table_blocks++;
1337 *goal = inode_table_first_block + inode_table_blocks;
1339 return ext4_fs_put_block_group_ref(&bg_ref);
1342 static int ext4_fs_get_inode_data_block_idx(struct ext4_inode_ref *inode_ref,
1343 uint64_t iblock, ext4_fsblk_t *fblock,
1345 bool support_unwritten __unused)
1347 struct ext4_fs *fs = inode_ref->fs;
1349 /* For empty file is situation simple */
1350 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1355 ext4_fsblk_t current_block;
1357 (void)extent_create;
1358 #if CONFIG_EXTENT_ENABLE
1359 /* Handle i-node using extents */
1360 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1361 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1363 ext4_fsblk_t current_fsblk;
1364 int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
1365 ¤t_fsblk, extent_create, NULL);
1369 current_block = current_fsblk;
1370 *fblock = current_block;
1372 ext4_assert(*fblock || support_unwritten);
1377 struct ext4_inode *inode = inode_ref->inode;
1379 /* Direct block are read directly from array in i-node structure */
1380 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1382 ext4_inode_get_direct_block(inode, (uint32_t)iblock);
1383 *fblock = current_block;
1387 /* Determine indirection level of the target block */
1388 unsigned int level = 0;
1390 for (i = 1; i < 4; i++) {
1391 if (iblock < fs->inode_block_limits[i]) {
1400 /* Compute offsets for the topmost level */
1401 uint64_t block_offset_in_level =
1402 iblock - fs->inode_block_limits[level - 1];
1403 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1404 uint32_t offset_in_block =
1405 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1408 if (current_block == 0) {
1413 struct ext4_block block;
1416 * Navigate through other levels, until we find the block number
1417 * or find null reference meaning we are dealing with sparse file
1420 /* Load indirect block */
1421 int rc = ext4_block_get(fs->bdev, &block, current_block);
1425 /* Read block address from indirect block */
1427 to_le32(((uint32_t *)block.data)[offset_in_block]);
1429 /* Put back indirect block untouched */
1430 rc = ext4_block_set(fs->bdev, &block);
1434 /* Check for sparse file */
1435 if (current_block == 0) {
1440 /* Jump to the next level */
1443 /* Termination condition - we have address of data block loaded
1448 /* Visit the next level */
1449 block_offset_in_level %= fs->inode_blocks_per_level[level];
1450 offset_in_block = block_offset_in_level /
1451 fs->inode_blocks_per_level[level - 1];
1454 *fblock = current_block;
1460 int ext4_fs_get_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1461 uint64_t iblock, ext4_fsblk_t *fblock,
1462 bool support_unwritten)
1464 return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
1465 false, support_unwritten);
1468 int ext4_fs_init_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1469 uint64_t iblock, ext4_fsblk_t *fblock)
1471 return ext4_fs_get_inode_data_block_idx(inode_ref, iblock, fblock,
1475 static int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1476 uint64_t iblock, ext4_fsblk_t fblock)
1478 struct ext4_fs *fs = inode_ref->fs;
1480 #if CONFIG_EXTENT_ENABLE
1481 /* Handle inode using extents */
1482 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1483 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1489 /* Handle simple case when we are dealing with direct reference */
1490 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1491 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1493 inode_ref->dirty = true;
1498 /* Determine the indirection level needed to get the desired block */
1499 unsigned int level = 0;
1501 for (i = 1; i < 4; i++) {
1502 if (iblock < fs->inode_block_limits[i]) {
1511 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1513 /* Compute offsets for the topmost level */
1514 uint64_t block_offset_in_level =
1515 iblock - fs->inode_block_limits[level - 1];
1516 ext4_fsblk_t current_block =
1517 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1518 uint32_t offset_in_block =
1519 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1521 ext4_fsblk_t new_block_addr;
1523 struct ext4_block block;
1524 struct ext4_block new_block;
1526 /* Is needed to allocate indirect block on the i-node level */
1527 if (current_block == 0) {
1528 /* Allocate new indirect block */
1530 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1534 rc = ext4_balloc_alloc_block(inode_ref,
1541 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1542 (uint32_t)new_block_addr);
1543 inode_ref->dirty = true;
1545 /* Load newly allocated block */
1546 rc = ext4_block_get(fs->bdev, &new_block, new_block_addr);
1548 ext4_balloc_free_block(inode_ref, new_block_addr);
1552 /* Initialize new block */
1553 memset(new_block.data, 0, block_size);
1554 new_block.dirty = true;
1556 /* Put back the allocated block */
1557 rc = ext4_block_set(fs->bdev, &new_block);
1561 current_block = new_block_addr;
1565 * Navigate through other levels, until we find the block number
1566 * or find null reference meaning we are dealing with sparse file
1569 int rc = ext4_block_get(fs->bdev, &block, current_block);
1574 to_le32(((uint32_t *)block.data)[offset_in_block]);
1576 if ((level > 1) && (current_block == 0)) {
1578 rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1580 ext4_block_set(fs->bdev, &block);
1584 /* Allocate new block */
1586 ext4_balloc_alloc_block(inode_ref, goal, &new_block_addr);
1588 ext4_block_set(fs->bdev, &block);
1592 /* Load newly allocated block */
1593 rc = ext4_block_get(fs->bdev, &new_block,
1597 ext4_block_set(fs->bdev, &block);
1601 /* Initialize allocated block */
1602 memset(new_block.data, 0, block_size);
1603 new_block.dirty = true;
1605 rc = ext4_block_set(fs->bdev, &new_block);
1607 ext4_block_set(fs->bdev, &block);
1611 /* Write block address to the parent */
1612 ((uint32_t *)block.data)[offset_in_block] =
1613 to_le32((uint32_t)new_block_addr);
1615 current_block = new_block_addr;
1618 /* Will be finished, write the fblock address */
1620 ((uint32_t *)block.data)[offset_in_block] =
1621 to_le32((uint32_t)fblock);
1625 rc = ext4_block_set(fs->bdev, &block);
1632 * If we are on the last level, break here as
1633 * there is no next level to visit
1638 /* Visit the next level */
1639 block_offset_in_level %= fs->inode_blocks_per_level[level];
1640 offset_in_block = block_offset_in_level /
1641 fs->inode_blocks_per_level[level - 1];
1648 int ext4_fs_append_inode_block(struct ext4_inode_ref *inode_ref,
1649 ext4_fsblk_t *fblock, uint32_t *iblock)
1651 #if CONFIG_EXTENT_ENABLE
1652 /* Handle extents separately */
1653 if ((ext4_sb_feature_incom(&inode_ref->fs->sb, EXT4_FINCOM_EXTENTS)) &&
1654 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1656 ext4_fsblk_t current_fsblk;
1657 struct ext4_sblock *sb = &inode_ref->fs->sb;
1658 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1659 uint32_t block_size = ext4_sb_get_block_size(sb);
1660 *iblock = (inode_size + block_size - 1) /
1663 rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
1664 ¤t_fsblk, true, NULL);
1667 *fblock = current_fsblk;
1668 ext4_assert(*fblock);
1670 ext4_inode_set_size(inode_ref->inode,
1671 inode_size + block_size);
1672 inode_ref->dirty = true;
1678 struct ext4_sblock *sb = &inode_ref->fs->sb;
1680 /* Compute next block index and allocate data block */
1681 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1682 uint32_t block_size = ext4_sb_get_block_size(sb);
1684 /* Align size i-node size */
1685 if ((inode_size % block_size) != 0)
1686 inode_size += block_size - (inode_size % block_size);
1688 /* Logical blocks are numbered from 0 */
1689 uint32_t new_block_idx = inode_size / block_size;
1691 /* Allocate new physical block */
1692 ext4_fsblk_t goal, phys_block;
1693 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1697 rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block);
1701 /* Add physical block address to the i-node */
1702 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1705 ext4_balloc_free_block(inode_ref, phys_block);
1710 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1711 inode_ref->dirty = true;
1713 *fblock = phys_block;
1714 *iblock = new_block_idx;
1719 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1723 link = ext4_inode_get_links_count(inode_ref->inode);
1725 ext4_inode_set_links_count(inode_ref->inode, link);
1728 ext4_sb_feature_com(&inode_ref->fs->sb, EXT4_FCOM_DIR_INDEX) &&
1729 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1731 if (is_dx && link > 1) {
1732 if (link >= EXT4_LINK_MAX || link == 2) {
1733 ext4_inode_set_links_count(inode_ref->inode, 1);
1736 ext4_get32(&inode_ref->fs->sb, features_read_only);
1737 v |= EXT4_FRO_COM_DIR_NLINK;
1738 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1743 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1745 uint16_t links = ext4_inode_get_links_count(inode_ref->inode);
1746 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1747 EXT4_INODE_MODE_DIRECTORY)) {
1749 ext4_inode_set_links_count(inode_ref->inode, links - 1);
1754 ext4_inode_set_links_count(inode_ref->inode, links - 1);