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_crc32.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,
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 if (!fs->read_only) {
108 /* Mark system as mounted */
109 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
110 r = ext4_sb_write(fs->bdev, &fs->sb);
114 /*Update mount count*/
115 ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
121 int ext4_fs_fini(struct ext4_fs *fs)
125 /*Set superblock state*/
126 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
129 return ext4_sb_write(fs->bdev, &fs->sb);
134 static void ext4_fs_debug_features_inc(uint32_t features_incompatible)
136 if (features_incompatible & EXT4_FINCOM_COMPRESSION)
137 ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
138 if (features_incompatible & EXT4_FINCOM_FILETYPE)
139 ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
140 if (features_incompatible & EXT4_FINCOM_RECOVER)
141 ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
142 if (features_incompatible & EXT4_FINCOM_JOURNAL_DEV)
143 ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
144 if (features_incompatible & EXT4_FINCOM_META_BG)
145 ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
146 if (features_incompatible & EXT4_FINCOM_EXTENTS)
147 ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
148 if (features_incompatible & EXT4_FINCOM_64BIT)
149 ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
150 if (features_incompatible & EXT4_FINCOM_MMP)
151 ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
152 if (features_incompatible & EXT4_FINCOM_FLEX_BG)
153 ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
154 if (features_incompatible & EXT4_FINCOM_EA_INODE)
155 ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
156 if (features_incompatible & EXT4_FINCOM_DIRDATA)
157 ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
158 if (features_incompatible & EXT4_FINCOM_BG_USE_META_CSUM)
159 ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
160 if (features_incompatible & EXT4_FINCOM_LARGEDIR)
161 ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
162 if (features_incompatible & EXT4_FINCOM_INLINE_DATA)
163 ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
165 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
167 if (features_compatible & EXT4_FCOM_DIR_PREALLOC)
168 ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
169 if (features_compatible & EXT4_FCOM_IMAGIC_INODES)
170 ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
171 if (features_compatible & EXT4_FCOM_HAS_JOURNAL)
172 ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
173 if (features_compatible & EXT4_FCOM_EXT_ATTR)
174 ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
175 if (features_compatible & EXT4_FCOM_RESIZE_INODE)
176 ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
177 if (features_compatible & EXT4_FCOM_DIR_INDEX)
178 ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
181 static void ext4_fs_debug_features_ro(uint32_t features_ro)
183 if (features_ro & EXT4_FRO_COM_SPARSE_SUPER)
184 ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
185 if (features_ro & EXT4_FRO_COM_LARGE_FILE)
186 ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
187 if (features_ro & EXT4_FRO_COM_BTREE_DIR)
188 ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
189 if (features_ro & EXT4_FRO_COM_HUGE_FILE)
190 ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
191 if (features_ro & EXT4_FRO_COM_GDT_CSUM)
192 ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
193 if (features_ro & EXT4_FRO_COM_DIR_NLINK)
194 ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
195 if (features_ro & EXT4_FRO_COM_EXTRA_ISIZE)
196 ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
197 if (features_ro & EXT4_FRO_COM_QUOTA)
198 ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
199 if (features_ro & EXT4_FRO_COM_BIGALLOC)
200 ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
201 if (features_ro & EXT4_FRO_COM_METADATA_CSUM)
202 ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
205 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
207 ext4_assert(fs && read_only);
209 if (ext4_get32(&fs->sb, rev_level) == 0) {
214 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
215 ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
217 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
218 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
220 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
221 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
223 /*Check features_incompatible*/
224 v = (ext4_get32(&fs->sb, features_incompatible) &
225 (~CONFIG_SUPPORTED_FINCOM));
227 ext4_dbg(DEBUG_FS, DBG_ERROR
228 "sblock has unsupported features incompatible:\n");
229 ext4_fs_debug_features_inc(v);
233 /*Check features_read_only*/
234 v = ext4_get32(&fs->sb, features_read_only);
235 v &= ~CONFIG_SUPPORTED_FRO_COM;
237 ext4_dbg(DEBUG_FS, DBG_WARN
238 "sblock has unsupported features read only:\n");
239 ext4_fs_debug_features_ro(v);
248 /**@brief Determine whether the block is inside the group.
249 * @param baddr block address
250 * @param bgid block group id
253 static bool ext4_block_in_group(struct ext4_sblock *s, ext4_fsblk_t baddr,
256 uint32_t actual_bgid;
257 actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
258 if (actual_bgid == bgid)
263 /**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only
264 * need to use it within a single byte (to ensure we get endianness right).
265 * We can use memset for the rest of the bitmap as there are no other users.
267 static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
271 if (start_bit >= end_bit)
274 for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
275 ext4_bmap_bit_set(bitmap, i);
278 memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
281 /**@brief Initialize block bitmap in block group.
282 * @param bg_ref Reference to block group
285 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
287 struct ext4_sblock *sb = &bg_ref->fs->sb;
288 struct ext4_bgroup *bg = bg_ref->block_group;
291 uint32_t i, bit, bit_max;
292 uint32_t group_blocks;
293 uint16_t inode_size = ext4_get16(sb, inode_size);
294 uint32_t block_size = ext4_sb_get_block_size(sb);
295 uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
297 ext4_fsblk_t bmp_blk = ext4_bg_get_block_bitmap(bg, sb);
298 ext4_fsblk_t bmp_inode = ext4_bg_get_inode_bitmap(bg, sb);
299 ext4_fsblk_t inode_table = ext4_bg_get_inode_table_first_block(bg, sb);
300 ext4_fsblk_t first_bg = ext4_balloc_get_block_of_bgid(sb, bg_ref->index);
302 uint32_t dsc_per_block = block_size / ext4_sb_get_desc_size(sb);
304 bool flex_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_FLEX_BG);
305 bool meta_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_META_BG);
307 uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
309 struct ext4_block block_bitmap;
310 rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &block_bitmap, bmp_blk);
314 memset(block_bitmap.data, 0, block_size);
315 bit_max = ext4_sb_is_super_in_bg(sb, bg_ref->index);
317 uint32_t count = ext4_sb_first_meta_bg(sb) * dsc_per_block;
318 if (!meta_bg || bg_ref->index < count) {
320 bit_max += ext4_bg_num_gdb(sb, bg_ref->index);
321 bit_max += ext4_get16(sb, s_reserved_gdt_blocks);
323 } else { /* For META_BG_BLOCK_GROUPS */
324 bit_max += ext4_bg_num_gdb(sb, bg_ref->index);
326 for (bit = 0; bit < bit_max; bit++)
327 ext4_bmap_bit_set(block_bitmap.data, bit);
329 if (bg_ref->index == ext4_block_group_cnt(sb) - 1) {
331 * Even though mke2fs always initialize first and last group
332 * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
333 * to make sure we calculate the right free blocks
336 group_blocks = (uint32_t)(ext4_sb_get_blocks_cnt(sb) -
337 ext4_get32(sb, first_data_block) -
338 ext4_get32(sb, blocks_per_group) *
339 (ext4_block_group_cnt(sb) - 1));
341 group_blocks = ext4_get32(sb, blocks_per_group);
345 in_bg = ext4_block_in_group(sb, bmp_blk, bg_ref->index);
346 if (!flex_bg || in_bg)
347 ext4_bmap_bit_set(block_bitmap.data,
348 (uint32_t)(bmp_blk - first_bg));
350 in_bg = ext4_block_in_group(sb, bmp_inode, bg_ref->index);
351 if (!flex_bg || in_bg)
352 ext4_bmap_bit_set(block_bitmap.data,
353 (uint32_t)(bmp_inode - first_bg));
355 for (i = inode_table; i < inode_table + inode_table_bcnt; i++) {
356 in_bg = ext4_block_in_group(sb, i, bg_ref->index);
357 if (!flex_bg || in_bg)
358 ext4_bmap_bit_set(block_bitmap.data,
359 (uint32_t)(i - first_bg));
362 * Also if the number of blocks within the group is
363 * less than the blocksize * 8 ( which is the size
364 * of bitmap ), set rest of the block bitmap to 1
366 ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
367 ext4_trans_set_block_dirty(block_bitmap.buf);
369 ext4_balloc_set_bitmap_csum(sb, bg_ref->block_group, block_bitmap.data);
370 bg_ref->dirty = true;
373 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
376 /**@brief Initialize i-node bitmap in block group.
377 * @param bg_ref Reference to block group
380 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
383 struct ext4_sblock *sb = &bg_ref->fs->sb;
384 struct ext4_bgroup *bg = bg_ref->block_group;
387 ext4_fsblk_t bitmap_block_addr = ext4_bg_get_inode_bitmap(bg, sb);
390 rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, bitmap_block_addr);
394 /* Initialize all bitmap bits to zero */
395 uint32_t block_size = ext4_sb_get_block_size(sb);
396 uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
398 memset(b.data, 0, (inodes_per_group + 7) / 8);
400 uint32_t start_bit = inodes_per_group;
401 uint32_t end_bit = block_size * 8;
404 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
405 ext4_bmap_bit_set(b.data, i);
408 memset(b.data + (i >> 3), 0xff, (end_bit - i) >> 3);
410 ext4_trans_set_block_dirty(b.buf);
412 ext4_ialloc_set_bitmap_csum(sb, bg, b.data);
413 bg_ref->dirty = true;
416 return ext4_block_set(bg_ref->fs->bdev, &b);
419 /**@brief Initialize i-node table in block group.
420 * @param bg_ref Reference to block group
423 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
425 struct ext4_sblock *sb = &bg_ref->fs->sb;
426 struct ext4_bgroup *bg = bg_ref->block_group;
428 uint32_t inode_size = ext4_get32(sb, inode_size);
429 uint32_t block_size = ext4_sb_get_block_size(sb);
430 uint32_t inodes_per_block = block_size / inode_size;
431 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
432 uint32_t table_blocks = inodes_in_group / inodes_per_block;
435 if (inodes_in_group % inodes_per_block)
438 /* Compute initialization bounds */
439 ext4_fsblk_t first_block = ext4_bg_get_inode_table_first_block(bg, sb);
441 ext4_fsblk_t last_block = first_block + table_blocks - 1;
443 /* Initialization of all itable blocks */
444 for (fblock = first_block; fblock <= last_block; ++fblock) {
446 int rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, fblock);
450 memset(b.data, 0, block_size);
451 ext4_trans_set_block_dirty(b.buf);
453 ext4_block_set(bg_ref->fs->bdev, &b);
461 static ext4_fsblk_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
463 uint32_t dsc_per_block)
465 uint32_t first_meta_bg, dsc_id;
467 dsc_id = bgid / dsc_per_block;
468 first_meta_bg = ext4_sb_first_meta_bg(s);
470 bool meta_bg = ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG);
472 if (!meta_bg || dsc_id < first_meta_bg)
473 return ext4_get32(s, first_data_block) + dsc_id + 1;
475 if (ext4_sb_is_super_in_bg(s, bgid))
478 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
481 /**@brief Compute checksum of block group descriptor.
482 * @param sb Superblock
483 * @param bgid Index of block group in the filesystem
484 * @param bg Block group to compute checksum for
485 * @return Checksum value
487 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
488 struct ext4_bgroup *bg)
490 /* If checksum not supported, 0 will be returned */
492 #if CONFIG_META_CSUM_ENABLE
493 /* Compute the checksum only if the filesystem supports it */
494 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
495 /* Use metadata_csum algorithm instead */
496 uint32_t le32_bgid = to_le32(bgid);
497 uint32_t orig_checksum, checksum;
499 /* Preparation: temporarily set bg checksum to 0 */
500 orig_checksum = bg->checksum;
503 /* First calculate crc32 checksum against fs uuid */
504 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
506 /* Then calculate crc32 checksum against bgid */
507 checksum = ext4_crc32c(checksum, &le32_bgid, sizeof(bgid));
508 /* Finally calculate crc32 checksum against block_group_desc */
509 checksum = ext4_crc32c(checksum, bg, ext4_sb_get_desc_size(sb));
510 bg->checksum = orig_checksum;
512 crc = checksum & 0xFFFF;
516 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_GDT_CSUM)) {
517 uint8_t *base = (uint8_t *)bg;
518 uint8_t *checksum = (uint8_t *)&bg->checksum;
520 uint32_t offset = (uint32_t)(checksum - base);
522 /* Convert block group index to little endian */
523 uint32_t group = to_le32(bgid);
526 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
528 /* Include index of block group */
529 crc = ext4_bg_crc16(crc, (uint8_t *)&group, sizeof(group));
531 /* Compute crc from the first part (stop before checksum field)
533 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
536 offset += sizeof(bg->checksum);
538 /* Checksum of the rest of block group descriptor */
539 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_64BIT)) &&
540 (offset < ext4_sb_get_desc_size(sb))) {
542 const uint8_t *start = ((uint8_t *)bg) + offset;
543 size_t len = ext4_sb_get_desc_size(sb) - offset;
544 crc = ext4_bg_crc16(crc, start, len);
550 #if CONFIG_META_CSUM_ENABLE
551 static bool ext4_fs_verify_bg_csum(struct ext4_sblock *sb,
553 struct ext4_bgroup *bg)
555 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
558 return ext4_fs_bg_checksum(sb, bgid, bg) == to_le16(bg->checksum);
561 #define ext4_fs_verify_bg_csum(...) true
564 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
565 struct ext4_block_group_ref *ref)
567 /* Compute number of descriptors, that fits in one data block */
568 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
569 uint32_t dsc_cnt = block_size / ext4_sb_get_desc_size(&fs->sb);
571 /* Block group descriptor table starts at the next block after
573 uint64_t block_id = ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_cnt);
575 uint32_t offset = (bgid % dsc_cnt) * ext4_sb_get_desc_size(&fs->sb);
577 int rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id);
581 ref->block_group = (void *)(ref->block.data + offset);
585 struct ext4_bgroup *bg = ref->block_group;
587 if (!ext4_fs_verify_bg_csum(&fs->sb, bgid, bg)) {
589 DBG_WARN "Block group descriptor checksum failed."
590 "Block group index: %" PRIu32"\n",
594 if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
595 rc = ext4_fs_init_block_bitmap(ref);
597 ext4_block_set(fs->bdev, &ref->block);
600 ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT);
604 if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
605 rc = ext4_fs_init_inode_bitmap(ref);
607 ext4_block_set(ref->fs->bdev, &ref->block);
611 ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT);
613 if (!ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
614 rc = ext4_fs_init_inode_table(ref);
616 ext4_block_set(fs->bdev, &ref->block);
620 ext4_bg_set_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED);
629 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
631 /* Check if reference modified */
633 /* Compute new checksum of block group */
635 cs = ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
637 ref->block_group->checksum = to_le16(cs);
639 /* Mark block dirty for writing changes to physical device */
640 ext4_trans_set_block_dirty(ref->block.buf);
643 /* Put back block, that contains block group descriptor */
644 return ext4_block_set(ref->fs->bdev, &ref->block);
647 #if CONFIG_META_CSUM_ENABLE
648 static uint32_t ext4_fs_inode_checksum(struct ext4_inode_ref *inode_ref)
650 uint32_t checksum = 0;
651 struct ext4_sblock *sb = &inode_ref->fs->sb;
652 uint16_t inode_size = ext4_get16(sb, inode_size);
654 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
655 uint32_t orig_checksum;
657 uint32_t ino_index = to_le32(inode_ref->index);
659 to_le32(ext4_inode_get_generation(inode_ref->inode));
661 /* Preparation: temporarily set bg checksum to 0 */
662 orig_checksum = ext4_inode_get_csum(sb, inode_ref->inode);
663 ext4_inode_set_csum(sb, inode_ref->inode, 0);
665 /* First calculate crc32 checksum against fs uuid */
666 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
668 /* Then calculate crc32 checksum against inode number
669 * and inode generation */
670 checksum = ext4_crc32c(checksum, &ino_index, sizeof(ino_index));
671 checksum = ext4_crc32c(checksum, &ino_gen, sizeof(ino_gen));
672 /* Finally calculate crc32 checksum against
673 * the entire inode */
674 checksum = ext4_crc32c(checksum, inode_ref->inode, inode_size);
675 ext4_inode_set_csum(sb, inode_ref->inode, orig_checksum);
677 /* If inode size is not large enough to hold the
678 * upper 16bit of the checksum */
679 if (inode_size == EXT4_GOOD_OLD_INODE_SIZE)
686 #define ext4_fs_inode_checksum(...) 0
689 static void ext4_fs_set_inode_checksum(struct ext4_inode_ref *inode_ref)
691 struct ext4_sblock *sb = &inode_ref->fs->sb;
692 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
695 uint32_t csum = ext4_fs_inode_checksum(inode_ref);
696 ext4_inode_set_csum(sb, inode_ref->inode, csum);
699 #if CONFIG_META_CSUM_ENABLE
700 static bool ext4_fs_verify_inode_csum(struct ext4_inode_ref *inode_ref)
702 struct ext4_sblock *sb = &inode_ref->fs->sb;
703 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
706 return ext4_inode_get_csum(sb, inode_ref->inode) ==
707 ext4_fs_inode_checksum(inode_ref);
710 #define ext4_fs_verify_inode_csum(...) true
714 __ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
715 struct ext4_inode_ref *ref,
718 /* Compute number of i-nodes, that fits in one data block */
719 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
722 * Inode numbers are 1-based, but it is simpler to work with 0-based
723 * when computing indices
726 uint32_t block_group = index / inodes_per_group;
727 uint32_t offset_in_group = index % inodes_per_group;
729 /* Load block group, where i-node is located */
730 struct ext4_block_group_ref bg_ref;
732 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
737 /* Load block address, where i-node table is located */
738 ext4_fsblk_t inode_table_start =
739 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
741 /* Put back block group reference (not needed more) */
742 rc = ext4_fs_put_block_group_ref(&bg_ref);
747 /* Compute position of i-node in the block group */
748 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
749 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
750 uint32_t byte_offset_in_group = offset_in_group * inode_size;
752 /* Compute block address */
753 ext4_fsblk_t block_id =
754 inode_table_start + (byte_offset_in_group / block_size);
756 rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id);
761 /* Compute position of i-node in the data block */
762 uint32_t offset_in_block = byte_offset_in_group % block_size;
763 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
765 /* We need to store the original value of index in the reference */
766 ref->index = index + 1;
770 if (initialized && !ext4_fs_verify_inode_csum(ref)) {
772 DBG_WARN "Inode checksum failed."
773 "Inode: %" PRIu32"\n",
780 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
781 struct ext4_inode_ref *ref)
783 return __ext4_fs_get_inode_ref(fs, index, ref, true);
786 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
788 /* Check if reference modified */
790 /* Mark block dirty for writing changes to physical device */
791 ext4_fs_set_inode_checksum(ref);
792 ext4_trans_set_block_dirty(ref->block.buf);
795 /* Put back block, that contains i-node */
796 return ext4_block_set(ref->fs->bdev, &ref->block);
799 void ext4_fs_inode_blocks_init(struct ext4_fs *fs,
800 struct ext4_inode_ref *inode_ref)
803 struct ext4_inode *inode = inode_ref->inode;
805 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
806 inode->blocks[i] = 0;
809 #if CONFIG_EXTENT_ENABLE
810 /* Initialize extents if needed */
811 if (ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) {
812 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
814 /* Initialize extent root header */
815 ext4_extent_tree_init(inode_ref);
820 uint32_t ext4_fs_correspond_inode_mode(int filetype)
824 return EXT4_INODE_MODE_DIRECTORY;
825 case EXT4_DE_REG_FILE:
826 return EXT4_INODE_MODE_FILE;
827 case EXT4_DE_SYMLINK:
828 return EXT4_INODE_MODE_SOFTLINK;
830 /* FIXME: right now we only support 3 file type. */
836 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
839 /* Check if newly allocated i-node will be a directory */
841 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
843 is_dir = (filetype == EXT4_DE_DIR);
845 /* Allocate inode by allocation algorithm */
847 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
851 /* Load i-node from on-disk i-node table */
852 rc = __ext4_fs_get_inode_ref(fs, index, inode_ref, false);
854 ext4_ialloc_free_inode(fs, index, is_dir);
858 /* Initialize i-node */
859 struct ext4_inode *inode = inode_ref->inode;
864 * Default directory permissions to be compatible with other
866 * 0777 (octal) == rwxrwxrwx
870 mode |= EXT4_INODE_MODE_DIRECTORY;
873 * Default file permissions to be compatible with other systems
874 * 0666 (octal) == rw-rw-rw-
878 mode |= ext4_fs_correspond_inode_mode(filetype);
880 ext4_inode_set_mode(&fs->sb, inode, mode);
882 ext4_inode_set_links_cnt(inode, 0);
883 ext4_inode_set_uid(inode, 0);
884 ext4_inode_set_gid(inode, 0);
885 ext4_inode_set_size(inode, 0);
886 ext4_inode_set_access_time(inode, 0);
887 ext4_inode_set_change_inode_time(inode, 0);
888 ext4_inode_set_modif_time(inode, 0);
889 ext4_inode_set_del_time(inode, 0);
890 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
891 ext4_inode_set_flags(inode, 0);
892 ext4_inode_set_generation(inode, 0);
893 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
894 uint16_t off = offsetof(struct ext4_inode, extra_isize);
895 uint16_t size = sizeof(struct ext4_inode) - off;
896 ext4_inode_set_extra_isize(inode, size);
899 /* Reset blocks array. For symbolic link inode, just
900 * fill in blocks with 0 */
901 if (ext4_inode_is_type(&fs->sb, inode, EXT4_INODE_MODE_SOFTLINK)) {
902 for (int i = 0; i < EXT4_INODE_BLOCKS; i++)
903 inode->blocks[i] = 0;
906 ext4_fs_inode_blocks_init(fs, inode_ref);
909 inode_ref->dirty = true;
914 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
916 struct ext4_fs *fs = inode_ref->fs;
920 #if CONFIG_EXTENT_ENABLE
921 /* For extents must be data block destroyed by other way */
922 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
923 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
924 /* Data structures are released during truncate operation... */
928 /* Release all indirect (no data) blocks */
930 /* 1) Single indirect */
931 ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
933 int rc = ext4_balloc_free_block(inode_ref, fblock);
937 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
940 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
941 uint32_t count = block_size / sizeof(uint32_t);
943 struct ext4_block block;
945 /* 2) Double indirect */
946 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
948 int rc = ext4_trans_block_get(fs->bdev, &block, fblock);
952 ext4_fsblk_t ind_block;
953 for (offset = 0; offset < count; ++offset) {
954 ind_block = to_le32(((uint32_t *)block.data)[offset]);
958 rc = ext4_balloc_free_block(inode_ref, ind_block);
960 ext4_block_set(fs->bdev, &block);
966 ext4_block_set(fs->bdev, &block);
967 rc = ext4_balloc_free_block(inode_ref, fblock);
971 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
974 /* 3) Tripple indirect */
975 struct ext4_block subblock;
976 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
979 rc = ext4_trans_block_get(fs->bdev, &block, fblock);
983 ext4_fsblk_t ind_block;
984 for (offset = 0; offset < count; ++offset) {
985 ind_block = to_le32(((uint32_t *)block.data)[offset]);
989 rc = ext4_trans_block_get(fs->bdev, &subblock,
992 ext4_block_set(fs->bdev, &block);
996 ext4_fsblk_t ind_subblk;
997 for (suboff = 0; suboff < count; ++suboff) {
998 ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
1000 if (ind_subblk == 0)
1002 rc = ext4_balloc_free_block(inode_ref, ind_subblk);
1004 ext4_block_set(fs->bdev, &subblock);
1005 ext4_block_set(fs->bdev, &block);
1011 ext4_block_set(fs->bdev, &subblock);
1013 rc = ext4_balloc_free_block(inode_ref,
1016 ext4_block_set(fs->bdev, &block);
1022 ext4_block_set(fs->bdev, &block);
1023 rc = ext4_balloc_free_block(inode_ref, fblock);
1027 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
1029 /* Mark inode dirty for writing to the physical device */
1030 inode_ref->dirty = true;
1032 /* Free block with extended attributes if present */
1033 ext4_fsblk_t xattr_block =
1034 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
1036 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
1040 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
1043 /* Free inode by allocator */
1044 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
1045 EXT4_INODE_MODE_DIRECTORY))
1046 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
1048 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
1054 /**@brief Release data block from i-node
1055 * @param inode_ref I-node to release block from
1056 * @param iblock Logical block to be released
1057 * @return Error code
1059 static int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1062 ext4_fsblk_t fblock;
1064 struct ext4_fs *fs = inode_ref->fs;
1066 /* Extents are handled otherwise = there is not support in this function
1069 ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS) &&
1070 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1072 struct ext4_inode *inode = inode_ref->inode;
1074 /* Handle simple case when we are dealing with direct reference */
1075 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1076 fblock = ext4_inode_get_direct_block(inode, iblock);
1082 ext4_inode_set_direct_block(inode, iblock, 0);
1083 return ext4_balloc_free_block(inode_ref, fblock);
1086 /* Determine the indirection level needed to get the desired block */
1087 unsigned int level = 0;
1089 for (i = 1; i < 4; i++) {
1090 if (iblock < fs->inode_block_limits[i]) {
1099 /* Compute offsets for the topmost level */
1100 uint32_t block_offset_in_level =
1101 iblock - fs->inode_block_limits[level - 1];
1102 ext4_fsblk_t current_block =
1103 ext4_inode_get_indirect_block(inode, level - 1);
1104 uint32_t offset_in_block =
1105 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1108 * Navigate through other levels, until we find the block number
1109 * or find null reference meaning we are dealing with sparse file
1111 struct ext4_block block;
1116 if (current_block == 0)
1119 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1124 to_le32(((uint32_t *)block.data)[offset_in_block]);
1126 /* Set zero if physical data block address found */
1128 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1129 ext4_trans_set_block_dirty(block.buf);
1132 rc = ext4_block_set(fs->bdev, &block);
1139 * If we are on the last level, break here as
1140 * there is no next level to visit
1145 /* Visit the next level */
1146 block_offset_in_level %= fs->inode_blocks_per_level[level];
1147 offset_in_block = block_offset_in_level /
1148 fs->inode_blocks_per_level[level - 1];
1151 fblock = current_block;
1155 /* Physical block is not referenced, it can be released */
1156 return ext4_balloc_free_block(inode_ref, fblock);
1159 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
1161 struct ext4_sblock *sb = &inode_ref->fs->sb;
1165 /* Check flags, if i-node can be truncated */
1166 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
1169 /* If sizes are equal, nothing has to be done. */
1170 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
1171 if (old_size == new_size)
1174 /* It's not supported to make the larger file by truncate operation */
1175 if (old_size < new_size)
1179 v = ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK);
1180 if (v && old_size < sizeof(inode_ref->inode->blocks) &&
1181 !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
1182 char *content = (char *)inode_ref->inode->blocks + new_size;
1184 sizeof(inode_ref->inode->blocks) - (uint32_t)new_size);
1185 ext4_inode_set_size(inode_ref->inode, new_size);
1186 inode_ref->dirty = true;
1191 /* Compute how many blocks will be released */
1192 uint32_t block_size = ext4_sb_get_block_size(sb);
1193 uint32_t new_blocks_cnt = (uint32_t)((new_size + block_size - 1) / block_size);
1194 uint32_t old_blocks_cnt = (uint32_t)((old_size + block_size - 1) / block_size);
1195 uint32_t diff_blocks_cnt = old_blocks_cnt - new_blocks_cnt;
1196 #if CONFIG_EXTENT_ENABLE
1197 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_EXTENTS)) &&
1198 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1200 /* Extents require special operation */
1201 if (diff_blocks_cnt) {
1202 r = ext4_extent_remove_space(inode_ref, new_blocks_cnt,
1211 /* Release data blocks from the end of file */
1213 /* Starting from 1 because of logical blocks are numbered from 0
1215 for (i = 0; i < diff_blocks_cnt; ++i) {
1216 r = ext4_fs_release_inode_block(inode_ref,
1217 new_blocks_cnt + i);
1224 ext4_inode_set_size(inode_ref->inode, new_size);
1225 inode_ref->dirty = true;
1230 /**@brief Compute 'goal' for inode index
1231 * @param inode_ref Reference to inode, to allocate block for
1234 ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref)
1236 uint32_t grp_inodes = ext4_get32(&inode_ref->fs->sb, inodes_per_group);
1237 return (inode_ref->index - 1) / grp_inodes;
1240 /**@brief Compute 'goal' for allocation algorithm (For blockmap).
1241 * @param inode_ref Reference to inode, to allocate block for
1243 * @return error code
1245 int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
1249 struct ext4_sblock *sb = &inode_ref->fs->sb;
1252 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1253 uint32_t block_size = ext4_sb_get_block_size(sb);
1254 uint32_t iblock_cnt = (uint32_t)(inode_size / block_size);
1256 if (inode_size % block_size != 0)
1259 /* If inode has some blocks, get last block address + 1 */
1260 if (iblock_cnt > 0) {
1261 r = ext4_fs_get_inode_dblk_idx(inode_ref, iblock_cnt - 1,
1271 /* If goal == 0, sparse file -> continue */
1274 /* Identify block group of inode */
1276 uint32_t inodes_per_bg = ext4_get32(sb, inodes_per_group);
1277 uint32_t block_group = (inode_ref->index - 1) / inodes_per_bg;
1278 block_size = ext4_sb_get_block_size(sb);
1280 /* Load block group reference */
1281 struct ext4_block_group_ref bg_ref;
1282 r = ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
1286 struct ext4_bgroup *bg = bg_ref.block_group;
1288 /* Compute indexes */
1289 uint32_t bg_count = ext4_block_group_cnt(sb);
1290 ext4_fsblk_t itab_first_block = ext4_bg_get_inode_table_first_block(bg, sb);
1291 uint16_t itab_item_size = ext4_get16(sb, inode_size);
1292 uint32_t itab_bytes;
1294 /* Check for last block group */
1295 if (block_group < bg_count - 1) {
1296 itab_bytes = inodes_per_bg * itab_item_size;
1298 /* Last block group could be smaller */
1299 uint32_t inodes_cnt = ext4_get32(sb, inodes_count);
1301 itab_bytes = (inodes_cnt - ((bg_count - 1) * inodes_per_bg));
1302 itab_bytes *= itab_item_size;
1305 ext4_fsblk_t inode_table_blocks = itab_bytes / block_size;
1307 if (itab_bytes % block_size)
1308 inode_table_blocks++;
1310 *goal = itab_first_block + inode_table_blocks;
1312 return ext4_fs_put_block_group_ref(&bg_ref);
1315 static int ext4_fs_get_inode_dblk_idx_internal(struct ext4_inode_ref *inode_ref,
1316 uint64_t iblock, ext4_fsblk_t *fblock,
1318 bool support_unwritten __unused)
1320 struct ext4_fs *fs = inode_ref->fs;
1322 /* For empty file is situation simple */
1323 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1328 ext4_fsblk_t current_block;
1330 (void)extent_create;
1331 #if CONFIG_EXTENT_ENABLE
1332 /* Handle i-node using extents */
1333 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1334 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1336 ext4_fsblk_t current_fsblk;
1337 int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
1338 ¤t_fsblk, extent_create, NULL);
1342 current_block = current_fsblk;
1343 *fblock = current_block;
1345 ext4_assert(*fblock || support_unwritten);
1350 struct ext4_inode *inode = inode_ref->inode;
1352 /* Direct block are read directly from array in i-node structure */
1353 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1355 ext4_inode_get_direct_block(inode, (uint32_t)iblock);
1356 *fblock = current_block;
1360 /* Determine indirection level of the target block */
1363 for (i = 1; i < 4; i++) {
1364 if (iblock < fs->inode_block_limits[i]) {
1373 /* Compute offsets for the topmost level */
1374 uint32_t blk_off_in_lvl = iblock - fs->inode_block_limits[l - 1];
1375 current_block = ext4_inode_get_indirect_block(inode, l - 1);
1376 uint32_t off_in_blk = blk_off_in_lvl / fs->inode_blocks_per_level[l - 1];
1379 if (current_block == 0) {
1384 struct ext4_block block;
1387 * Navigate through other levels, until we find the block number
1388 * or find null reference meaning we are dealing with sparse file
1391 /* Load indirect block */
1392 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1396 /* Read block address from indirect block */
1398 to_le32(((uint32_t *)block.data)[off_in_blk]);
1400 /* Put back indirect block untouched */
1401 rc = ext4_block_set(fs->bdev, &block);
1405 /* Check for sparse file */
1406 if (current_block == 0) {
1411 /* Jump to the next level */
1414 /* Termination condition - we have address of data block loaded
1419 /* Visit the next level */
1420 blk_off_in_lvl %= fs->inode_blocks_per_level[l];
1421 off_in_blk = blk_off_in_lvl / fs->inode_blocks_per_level[l - 1];
1424 *fblock = current_block;
1430 int ext4_fs_get_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
1431 uint64_t iblock, ext4_fsblk_t *fblock,
1432 bool support_unwritten)
1434 return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock,
1435 false, support_unwritten);
1438 int ext4_fs_init_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
1439 uint64_t iblock, ext4_fsblk_t *fblock)
1441 return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock,
1445 static int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1446 uint32_t iblock, ext4_fsblk_t fblock)
1448 struct ext4_fs *fs = inode_ref->fs;
1450 #if CONFIG_EXTENT_ENABLE
1451 /* Handle inode using extents */
1452 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1453 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1459 /* Handle simple case when we are dealing with direct reference */
1460 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1461 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1463 inode_ref->dirty = true;
1468 /* Determine the indirection level needed to get the desired block */
1471 for (i = 1; i < 4; i++) {
1472 if (iblock < fs->inode_block_limits[i]) {
1481 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1483 /* Compute offsets for the topmost level */
1484 uint32_t blk_off_in_lvl = iblock - fs->inode_block_limits[l - 1];
1485 ext4_fsblk_t current_block =
1486 ext4_inode_get_indirect_block(inode_ref->inode, l - 1);
1487 uint32_t off_in_blk = blk_off_in_lvl / fs->inode_blocks_per_level[l - 1];
1489 ext4_fsblk_t new_blk;
1491 struct ext4_block block;
1492 struct ext4_block new_block;
1494 /* Is needed to allocate indirect block on the i-node level */
1495 if (current_block == 0) {
1496 /* Allocate new indirect block */
1498 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1502 rc = ext4_balloc_alloc_block(inode_ref, goal, &new_blk);
1507 ext4_inode_set_indirect_block(inode_ref->inode, l - 1,
1509 inode_ref->dirty = true;
1511 /* Load newly allocated block */
1512 rc = ext4_trans_block_get_noread(fs->bdev, &new_block, new_blk);
1514 ext4_balloc_free_block(inode_ref, new_blk);
1518 /* Initialize new block */
1519 memset(new_block.data, 0, block_size);
1520 ext4_trans_set_block_dirty(new_block.buf);
1522 /* Put back the allocated block */
1523 rc = ext4_block_set(fs->bdev, &new_block);
1527 current_block = new_blk;
1531 * Navigate through other levels, until we find the block number
1532 * or find null reference meaning we are dealing with sparse file
1535 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1539 current_block = to_le32(((uint32_t *)block.data)[off_in_blk]);
1540 if ((l > 1) && (current_block == 0)) {
1542 rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1544 ext4_block_set(fs->bdev, &block);
1548 /* Allocate new block */
1550 ext4_balloc_alloc_block(inode_ref, goal, &new_blk);
1552 ext4_block_set(fs->bdev, &block);
1556 /* Load newly allocated block */
1557 rc = ext4_trans_block_get_noread(fs->bdev, &new_block,
1561 ext4_block_set(fs->bdev, &block);
1565 /* Initialize allocated block */
1566 memset(new_block.data, 0, block_size);
1567 ext4_trans_set_block_dirty(new_block.buf);
1569 rc = ext4_block_set(fs->bdev, &new_block);
1571 ext4_block_set(fs->bdev, &block);
1575 /* Write block address to the parent */
1576 uint32_t * p = (uint32_t * )block.data;
1577 p[off_in_blk] = to_le32((uint32_t)new_blk);
1578 ext4_trans_set_block_dirty(block.buf);
1579 current_block = new_blk;
1582 /* Will be finished, write the fblock address */
1584 uint32_t * p = (uint32_t * )block.data;
1585 p[off_in_blk] = to_le32((uint32_t)fblock);
1586 ext4_trans_set_block_dirty(block.buf);
1589 rc = ext4_block_set(fs->bdev, &block);
1596 * If we are on the last level, break here as
1597 * there is no next level to visit
1602 /* Visit the next level */
1603 blk_off_in_lvl %= fs->inode_blocks_per_level[l];
1604 off_in_blk = blk_off_in_lvl / fs->inode_blocks_per_level[l - 1];
1611 int ext4_fs_append_inode_dblk(struct ext4_inode_ref *inode_ref,
1612 ext4_fsblk_t *fblock, uint32_t *iblock)
1614 #if CONFIG_EXTENT_ENABLE
1615 /* Handle extents separately */
1616 if ((ext4_sb_feature_incom(&inode_ref->fs->sb, EXT4_FINCOM_EXTENTS)) &&
1617 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1619 ext4_fsblk_t current_fsblk;
1620 struct ext4_sblock *sb = &inode_ref->fs->sb;
1621 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1622 uint32_t block_size = ext4_sb_get_block_size(sb);
1623 *iblock = (uint32_t)((inode_size + block_size - 1) / block_size);
1625 rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
1626 ¤t_fsblk, true, NULL);
1628 *fblock = current_fsblk;
1629 ext4_assert(*fblock);
1631 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1632 inode_ref->dirty = true;
1638 struct ext4_sblock *sb = &inode_ref->fs->sb;
1640 /* Compute next block index and allocate data block */
1641 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1642 uint32_t block_size = ext4_sb_get_block_size(sb);
1644 /* Align size i-node size */
1645 if ((inode_size % block_size) != 0)
1646 inode_size += block_size - (inode_size % block_size);
1648 /* Logical blocks are numbered from 0 */
1649 uint32_t new_block_idx = (uint32_t)(inode_size / block_size);
1651 /* Allocate new physical block */
1652 ext4_fsblk_t goal, phys_block;
1653 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1657 rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block);
1661 /* Add physical block address to the i-node */
1662 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1665 ext4_balloc_free_block(inode_ref, phys_block);
1670 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1671 inode_ref->dirty = true;
1673 *fblock = phys_block;
1674 *iblock = new_block_idx;
1679 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1683 link = ext4_inode_get_links_cnt(inode_ref->inode);
1685 ext4_inode_set_links_cnt(inode_ref->inode, link);
1687 is_dx = ext4_sb_feature_com(&inode_ref->fs->sb, EXT4_FCOM_DIR_INDEX) &&
1688 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1690 if (is_dx && link > 1) {
1691 if (link >= EXT4_LINK_MAX || link == 2) {
1692 ext4_inode_set_links_cnt(inode_ref->inode, 1);
1695 v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1696 v |= EXT4_FRO_COM_DIR_NLINK;
1697 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1702 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1704 uint16_t links = ext4_inode_get_links_cnt(inode_ref->inode);
1705 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1706 EXT4_INODE_MODE_DIRECTORY)) {
1708 ext4_inode_set_links_cnt(inode_ref->inode, links - 1);
1713 ext4_inode_set_links_cnt(inode_ref->inode, links - 1);