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 fs->read_only = read_only;
71 r = ext4_sb_read(fs->bdev, &fs->sb);
75 if (!ext4_sb_check(&fs->sb))
78 bsize = ext4_sb_get_block_size(&fs->sb);
79 if (bsize > EXT4_MAX_BLOCK_SIZE)
82 r = ext4_fs_check_features(fs, &read_only);
87 fs->read_only = read_only;
89 /* Compute limits for indirect block levels */
90 uint32_t blocks_id = bsize / sizeof(uint32_t);
92 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
93 fs->inode_blocks_per_level[0] = 1;
95 for (i = 1; i < 4; i++) {
96 fs->inode_blocks_per_level[i] =
97 fs->inode_blocks_per_level[i - 1] * blocks_id;
98 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
99 fs->inode_blocks_per_level[i];
103 tmp = ext4_get16(&fs->sb, state);
104 if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS)
105 ext4_dbg(DEBUG_FS, DBG_WARN
106 "last umount error: superblock fs_error flag\n");
109 if (!fs->read_only) {
110 /* Mark system as mounted */
111 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
112 r = ext4_sb_write(fs->bdev, &fs->sb);
116 /*Update mount count*/
117 ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
123 int ext4_fs_fini(struct ext4_fs *fs)
127 /*Set superblock state*/
128 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
131 return ext4_sb_write(fs->bdev, &fs->sb);
136 static void ext4_fs_debug_features_inc(uint32_t features_incompatible)
138 if (features_incompatible & EXT4_FINCOM_COMPRESSION)
139 ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
140 if (features_incompatible & EXT4_FINCOM_FILETYPE)
141 ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
142 if (features_incompatible & EXT4_FINCOM_RECOVER)
143 ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
144 if (features_incompatible & EXT4_FINCOM_JOURNAL_DEV)
145 ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
146 if (features_incompatible & EXT4_FINCOM_META_BG)
147 ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
148 if (features_incompatible & EXT4_FINCOM_EXTENTS)
149 ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
150 if (features_incompatible & EXT4_FINCOM_64BIT)
151 ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
152 if (features_incompatible & EXT4_FINCOM_MMP)
153 ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
154 if (features_incompatible & EXT4_FINCOM_FLEX_BG)
155 ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
156 if (features_incompatible & EXT4_FINCOM_EA_INODE)
157 ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
158 if (features_incompatible & EXT4_FINCOM_DIRDATA)
159 ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
160 if (features_incompatible & EXT4_FINCOM_BG_USE_META_CSUM)
161 ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
162 if (features_incompatible & EXT4_FINCOM_LARGEDIR)
163 ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
164 if (features_incompatible & EXT4_FINCOM_INLINE_DATA)
165 ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
167 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
169 if (features_compatible & EXT4_FCOM_DIR_PREALLOC)
170 ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
171 if (features_compatible & EXT4_FCOM_IMAGIC_INODES)
172 ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
173 if (features_compatible & EXT4_FCOM_HAS_JOURNAL)
174 ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
175 if (features_compatible & EXT4_FCOM_EXT_ATTR)
176 ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
177 if (features_compatible & EXT4_FCOM_RESIZE_INODE)
178 ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
179 if (features_compatible & EXT4_FCOM_DIR_INDEX)
180 ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
183 static void ext4_fs_debug_features_ro(uint32_t features_ro)
185 if (features_ro & EXT4_FRO_COM_SPARSE_SUPER)
186 ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
187 if (features_ro & EXT4_FRO_COM_LARGE_FILE)
188 ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
189 if (features_ro & EXT4_FRO_COM_BTREE_DIR)
190 ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
191 if (features_ro & EXT4_FRO_COM_HUGE_FILE)
192 ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
193 if (features_ro & EXT4_FRO_COM_GDT_CSUM)
194 ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
195 if (features_ro & EXT4_FRO_COM_DIR_NLINK)
196 ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
197 if (features_ro & EXT4_FRO_COM_EXTRA_ISIZE)
198 ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
199 if (features_ro & EXT4_FRO_COM_QUOTA)
200 ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
201 if (features_ro & EXT4_FRO_COM_BIGALLOC)
202 ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
203 if (features_ro & EXT4_FRO_COM_METADATA_CSUM)
204 ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
207 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
209 ext4_assert(fs && read_only);
211 if (ext4_get32(&fs->sb, rev_level) == 0) {
216 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
217 ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
219 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
220 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
222 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
223 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
225 /*Check features_incompatible*/
226 v = (ext4_get32(&fs->sb, features_incompatible) &
227 (~CONFIG_SUPPORTED_FINCOM));
229 ext4_dbg(DEBUG_FS, DBG_ERROR
230 "sblock has unsupported features incompatible:\n");
231 ext4_fs_debug_features_inc(v);
235 /*Check features_read_only*/
236 v = ext4_get32(&fs->sb, features_read_only);
237 v &= ~CONFIG_SUPPORTED_FRO_COM;
239 ext4_dbg(DEBUG_FS, DBG_WARN
240 "sblock has unsupported features read only:\n");
241 ext4_fs_debug_features_ro(v);
250 /**@brief Determine whether the block is inside the group.
251 * @param baddr block address
252 * @param bgid block group id
255 static bool ext4_block_in_group(struct ext4_sblock *s, ext4_fsblk_t baddr,
258 uint32_t actual_bgid;
259 actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
260 if (actual_bgid == bgid)
265 /**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only
266 * need to use it within a single byte (to ensure we get endianness right).
267 * We can use memset for the rest of the bitmap as there are no other users.
269 static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
273 if (start_bit >= end_bit)
276 for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
277 ext4_bmap_bit_set(bitmap, i);
280 memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
283 /**@brief Initialize block bitmap in block group.
284 * @param bg_ref Reference to block group
287 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
289 struct ext4_sblock *sb = &bg_ref->fs->sb;
290 struct ext4_bgroup *bg = bg_ref->block_group;
293 uint32_t i, bit, bit_max;
294 uint32_t group_blocks;
295 uint16_t inode_size = ext4_get16(sb, inode_size);
296 uint32_t block_size = ext4_sb_get_block_size(sb);
297 uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
299 ext4_fsblk_t bmp_blk = ext4_bg_get_block_bitmap(bg, sb);
300 ext4_fsblk_t bmp_inode = ext4_bg_get_inode_bitmap(bg, sb);
301 ext4_fsblk_t inode_table = ext4_bg_get_inode_table_first_block(bg, sb);
302 ext4_fsblk_t first_bg = ext4_balloc_get_block_of_bgid(sb, bg_ref->index);
304 uint32_t dsc_per_block = block_size / ext4_sb_get_desc_size(sb);
306 bool flex_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_FLEX_BG);
307 bool meta_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_META_BG);
309 uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
311 struct ext4_block block_bitmap;
312 rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &block_bitmap, bmp_blk);
316 memset(block_bitmap.data, 0, block_size);
317 bit_max = ext4_sb_is_super_in_bg(sb, bg_ref->index);
319 uint32_t count = ext4_sb_first_meta_bg(sb) * dsc_per_block;
320 if (!meta_bg || bg_ref->index < count) {
322 bit_max += ext4_bg_num_gdb(sb, bg_ref->index);
323 bit_max += ext4_get16(sb, s_reserved_gdt_blocks);
325 } else { /* For META_BG_BLOCK_GROUPS */
326 bit_max += ext4_bg_num_gdb(sb, bg_ref->index);
328 for (bit = 0; bit < bit_max; bit++)
329 ext4_bmap_bit_set(block_bitmap.data, bit);
331 if (bg_ref->index == ext4_block_group_cnt(sb) - 1) {
333 * Even though mke2fs always initialize first and last group
334 * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
335 * to make sure we calculate the right free blocks
338 group_blocks = (uint32_t)(ext4_sb_get_blocks_cnt(sb) -
339 ext4_get32(sb, first_data_block) -
340 ext4_get32(sb, blocks_per_group) *
341 (ext4_block_group_cnt(sb) - 1));
343 group_blocks = ext4_get32(sb, blocks_per_group);
347 in_bg = ext4_block_in_group(sb, bmp_blk, bg_ref->index);
348 if (!flex_bg || in_bg)
349 ext4_bmap_bit_set(block_bitmap.data,
350 (uint32_t)(bmp_blk - first_bg));
352 in_bg = ext4_block_in_group(sb, bmp_inode, bg_ref->index);
353 if (!flex_bg || in_bg)
354 ext4_bmap_bit_set(block_bitmap.data,
355 (uint32_t)(bmp_inode - first_bg));
357 for (i = inode_table; i < inode_table + inode_table_bcnt; i++) {
358 in_bg = ext4_block_in_group(sb, i, bg_ref->index);
359 if (!flex_bg || in_bg)
360 ext4_bmap_bit_set(block_bitmap.data,
361 (uint32_t)(i - first_bg));
364 * Also if the number of blocks within the group is
365 * less than the blocksize * 8 ( which is the size
366 * of bitmap ), set rest of the block bitmap to 1
368 ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
369 ext4_trans_set_block_dirty(block_bitmap.buf);
371 ext4_balloc_set_bitmap_csum(sb, bg_ref->block_group, block_bitmap.data);
372 bg_ref->dirty = true;
375 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
378 /**@brief Initialize i-node bitmap in block group.
379 * @param bg_ref Reference to block group
382 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
385 struct ext4_sblock *sb = &bg_ref->fs->sb;
386 struct ext4_bgroup *bg = bg_ref->block_group;
389 ext4_fsblk_t bitmap_block_addr = ext4_bg_get_inode_bitmap(bg, sb);
392 rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, bitmap_block_addr);
396 /* Initialize all bitmap bits to zero */
397 uint32_t block_size = ext4_sb_get_block_size(sb);
398 uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
400 memset(b.data, 0, (inodes_per_group + 7) / 8);
402 uint32_t start_bit = inodes_per_group;
403 uint32_t end_bit = block_size * 8;
406 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
407 ext4_bmap_bit_set(b.data, i);
410 memset(b.data + (i >> 3), 0xff, (end_bit - i) >> 3);
412 ext4_trans_set_block_dirty(b.buf);
414 ext4_ialloc_set_bitmap_csum(sb, bg, b.data);
415 bg_ref->dirty = true;
418 return ext4_block_set(bg_ref->fs->bdev, &b);
421 /**@brief Initialize i-node table in block group.
422 * @param bg_ref Reference to block group
425 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
427 struct ext4_sblock *sb = &bg_ref->fs->sb;
428 struct ext4_bgroup *bg = bg_ref->block_group;
430 uint32_t inode_size = ext4_get32(sb, inode_size);
431 uint32_t block_size = ext4_sb_get_block_size(sb);
432 uint32_t inodes_per_block = block_size / inode_size;
433 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
434 uint32_t table_blocks = inodes_in_group / inodes_per_block;
437 if (inodes_in_group % inodes_per_block)
440 /* Compute initialization bounds */
441 ext4_fsblk_t first_block = ext4_bg_get_inode_table_first_block(bg, sb);
443 ext4_fsblk_t last_block = first_block + table_blocks - 1;
445 /* Initialization of all itable blocks */
446 for (fblock = first_block; fblock <= last_block; ++fblock) {
448 int rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, fblock);
452 memset(b.data, 0, block_size);
453 ext4_trans_set_block_dirty(b.buf);
455 ext4_block_set(bg_ref->fs->bdev, &b);
463 static ext4_fsblk_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
465 uint32_t dsc_per_block)
467 uint32_t first_meta_bg, dsc_id;
469 dsc_id = bgid / dsc_per_block;
470 first_meta_bg = ext4_sb_first_meta_bg(s);
472 bool meta_bg = ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG);
474 if (!meta_bg || dsc_id < first_meta_bg)
475 return ext4_get32(s, first_data_block) + dsc_id + 1;
477 if (ext4_sb_is_super_in_bg(s, bgid))
480 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
483 /**@brief Compute checksum of block group descriptor.
484 * @param sb Superblock
485 * @param bgid Index of block group in the filesystem
486 * @param bg Block group to compute checksum for
487 * @return Checksum value
489 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
490 struct ext4_bgroup *bg)
492 /* If checksum not supported, 0 will be returned */
494 #if CONFIG_META_CSUM_ENABLE
495 /* Compute the checksum only if the filesystem supports it */
496 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
497 /* Use metadata_csum algorithm instead */
498 uint32_t le32_bgid = to_le32(bgid);
499 uint32_t orig_checksum, checksum;
501 /* Preparation: temporarily set bg checksum to 0 */
502 orig_checksum = bg->checksum;
505 /* First calculate crc32 checksum against fs uuid */
506 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
508 /* Then calculate crc32 checksum against bgid */
509 checksum = ext4_crc32c(checksum, &le32_bgid, sizeof(bgid));
510 /* Finally calculate crc32 checksum against block_group_desc */
511 checksum = ext4_crc32c(checksum, bg, ext4_sb_get_desc_size(sb));
512 bg->checksum = orig_checksum;
514 crc = checksum & 0xFFFF;
518 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_GDT_CSUM)) {
519 uint8_t *base = (uint8_t *)bg;
520 uint8_t *checksum = (uint8_t *)&bg->checksum;
522 uint32_t offset = (uint32_t)(checksum - base);
524 /* Convert block group index to little endian */
525 uint32_t group = to_le32(bgid);
528 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
530 /* Include index of block group */
531 crc = ext4_bg_crc16(crc, (uint8_t *)&group, sizeof(group));
533 /* Compute crc from the first part (stop before checksum field)
535 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
538 offset += sizeof(bg->checksum);
540 /* Checksum of the rest of block group descriptor */
541 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_64BIT)) &&
542 (offset < ext4_sb_get_desc_size(sb))) {
544 const uint8_t *start = ((uint8_t *)bg) + offset;
545 size_t len = ext4_sb_get_desc_size(sb) - offset;
546 crc = ext4_bg_crc16(crc, start, len);
552 #if CONFIG_META_CSUM_ENABLE
553 static bool ext4_fs_verify_bg_csum(struct ext4_sblock *sb,
555 struct ext4_bgroup *bg)
557 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
560 return ext4_fs_bg_checksum(sb, bgid, bg) == to_le16(bg->checksum);
563 #define ext4_fs_verify_bg_csum(...) true
566 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
567 struct ext4_block_group_ref *ref)
569 /* Compute number of descriptors, that fits in one data block */
570 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
571 uint32_t dsc_cnt = block_size / ext4_sb_get_desc_size(&fs->sb);
573 /* Block group descriptor table starts at the next block after
575 uint64_t block_id = ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_cnt);
577 uint32_t offset = (bgid % dsc_cnt) * ext4_sb_get_desc_size(&fs->sb);
579 int rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id);
583 ref->block_group = (void *)(ref->block.data + offset);
587 struct ext4_bgroup *bg = ref->block_group;
589 if (!ext4_fs_verify_bg_csum(&fs->sb, bgid, bg)) {
591 DBG_WARN "Block group descriptor checksum failed."
592 "Block group index: %" PRIu32"\n",
596 if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
597 rc = ext4_fs_init_block_bitmap(ref);
599 ext4_block_set(fs->bdev, &ref->block);
602 ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT);
606 if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
607 rc = ext4_fs_init_inode_bitmap(ref);
609 ext4_block_set(ref->fs->bdev, &ref->block);
613 ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT);
615 if (!ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
616 rc = ext4_fs_init_inode_table(ref);
618 ext4_block_set(fs->bdev, &ref->block);
622 ext4_bg_set_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED);
631 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
633 /* Check if reference modified */
635 /* Compute new checksum of block group */
637 cs = ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
639 ref->block_group->checksum = to_le16(cs);
641 /* Mark block dirty for writing changes to physical device */
642 ext4_trans_set_block_dirty(ref->block.buf);
645 /* Put back block, that contains block group descriptor */
646 return ext4_block_set(ref->fs->bdev, &ref->block);
649 #if CONFIG_META_CSUM_ENABLE
650 static uint32_t ext4_fs_inode_checksum(struct ext4_inode_ref *inode_ref)
652 uint32_t checksum = 0;
653 struct ext4_sblock *sb = &inode_ref->fs->sb;
654 uint16_t inode_size = ext4_get16(sb, inode_size);
656 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
657 uint32_t orig_checksum;
659 uint32_t ino_index = to_le32(inode_ref->index);
661 to_le32(ext4_inode_get_generation(inode_ref->inode));
663 /* Preparation: temporarily set bg checksum to 0 */
664 orig_checksum = ext4_inode_get_csum(sb, inode_ref->inode);
665 ext4_inode_set_csum(sb, inode_ref->inode, 0);
667 /* First calculate crc32 checksum against fs uuid */
668 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
670 /* Then calculate crc32 checksum against inode number
671 * and inode generation */
672 checksum = ext4_crc32c(checksum, &ino_index, sizeof(ino_index));
673 checksum = ext4_crc32c(checksum, &ino_gen, sizeof(ino_gen));
674 /* Finally calculate crc32 checksum against
675 * the entire inode */
676 checksum = ext4_crc32c(checksum, inode_ref->inode, inode_size);
677 ext4_inode_set_csum(sb, inode_ref->inode, orig_checksum);
679 /* If inode size is not large enough to hold the
680 * upper 16bit of the checksum */
681 if (inode_size == EXT4_GOOD_OLD_INODE_SIZE)
688 #define ext4_fs_inode_checksum(...) 0
691 static void ext4_fs_set_inode_checksum(struct ext4_inode_ref *inode_ref)
693 struct ext4_sblock *sb = &inode_ref->fs->sb;
694 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
697 uint32_t csum = ext4_fs_inode_checksum(inode_ref);
698 ext4_inode_set_csum(sb, inode_ref->inode, csum);
701 #if CONFIG_META_CSUM_ENABLE
702 static bool ext4_fs_verify_inode_csum(struct ext4_inode_ref *inode_ref)
704 struct ext4_sblock *sb = &inode_ref->fs->sb;
705 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
708 return ext4_inode_get_csum(sb, inode_ref->inode) ==
709 ext4_fs_inode_checksum(inode_ref);
712 #define ext4_fs_verify_inode_csum(...) true
716 __ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
717 struct ext4_inode_ref *ref,
720 /* Compute number of i-nodes, that fits in one data block */
721 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
724 * Inode numbers are 1-based, but it is simpler to work with 0-based
725 * when computing indices
728 uint32_t block_group = index / inodes_per_group;
729 uint32_t offset_in_group = index % inodes_per_group;
731 /* Load block group, where i-node is located */
732 struct ext4_block_group_ref bg_ref;
734 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
739 /* Load block address, where i-node table is located */
740 ext4_fsblk_t inode_table_start =
741 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
743 /* Put back block group reference (not needed more) */
744 rc = ext4_fs_put_block_group_ref(&bg_ref);
749 /* Compute position of i-node in the block group */
750 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
751 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
752 uint32_t byte_offset_in_group = offset_in_group * inode_size;
754 /* Compute block address */
755 ext4_fsblk_t block_id =
756 inode_table_start + (byte_offset_in_group / block_size);
758 rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id);
763 /* Compute position of i-node in the data block */
764 uint32_t offset_in_block = byte_offset_in_group % block_size;
765 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
767 /* We need to store the original value of index in the reference */
768 ref->index = index + 1;
772 if (initialized && !ext4_fs_verify_inode_csum(ref)) {
774 DBG_WARN "Inode checksum failed."
775 "Inode: %" PRIu32"\n",
782 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
783 struct ext4_inode_ref *ref)
785 return __ext4_fs_get_inode_ref(fs, index, ref, true);
788 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
790 /* Check if reference modified */
792 /* Mark block dirty for writing changes to physical device */
793 ext4_fs_set_inode_checksum(ref);
794 ext4_trans_set_block_dirty(ref->block.buf);
797 /* Put back block, that contains i-node */
798 return ext4_block_set(ref->fs->bdev, &ref->block);
801 void ext4_fs_inode_blocks_init(struct ext4_fs *fs,
802 struct ext4_inode_ref *inode_ref)
805 struct ext4_inode *inode = inode_ref->inode;
807 for (i = 0; i < EXT4_INODE_BLOCKS; i++)
808 inode->blocks[i] = 0;
811 #if CONFIG_EXTENT_ENABLE
812 /* Initialize extents if needed */
813 if (ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) {
814 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
816 /* Initialize extent root header */
817 ext4_extent_tree_init(inode_ref);
822 uint32_t ext4_fs_correspond_inode_mode(int filetype)
826 return EXT4_INODE_MODE_DIRECTORY;
827 case EXT4_DE_REG_FILE:
828 return EXT4_INODE_MODE_FILE;
829 case EXT4_DE_SYMLINK:
830 return EXT4_INODE_MODE_SOFTLINK;
832 /* FIXME: right now we only support 3 file type. */
838 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
841 /* Check if newly allocated i-node will be a directory */
843 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
845 is_dir = (filetype == EXT4_DE_DIR);
847 /* Allocate inode by allocation algorithm */
849 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
853 /* Load i-node from on-disk i-node table */
854 rc = __ext4_fs_get_inode_ref(fs, index, inode_ref, false);
856 ext4_ialloc_free_inode(fs, index, is_dir);
860 /* Initialize i-node */
861 struct ext4_inode *inode = inode_ref->inode;
866 * Default directory permissions to be compatible with other
868 * 0777 (octal) == rwxrwxrwx
872 mode |= EXT4_INODE_MODE_DIRECTORY;
875 * Default file permissions to be compatible with other systems
876 * 0666 (octal) == rw-rw-rw-
880 mode |= ext4_fs_correspond_inode_mode(filetype);
882 ext4_inode_set_mode(&fs->sb, inode, mode);
884 ext4_inode_set_links_cnt(inode, 0);
885 ext4_inode_set_uid(inode, 0);
886 ext4_inode_set_gid(inode, 0);
887 ext4_inode_set_size(inode, 0);
888 ext4_inode_set_access_time(inode, 0);
889 ext4_inode_set_change_inode_time(inode, 0);
890 ext4_inode_set_modif_time(inode, 0);
891 ext4_inode_set_del_time(inode, 0);
892 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
893 ext4_inode_set_flags(inode, 0);
894 ext4_inode_set_generation(inode, 0);
895 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
896 uint16_t off = offsetof(struct ext4_inode, extra_isize);
897 uint16_t size = sizeof(struct ext4_inode) - off;
898 ext4_inode_set_extra_isize(inode, size);
901 /* Reset blocks array. For symbolic link inode, just
902 * fill in blocks with 0 */
903 if (ext4_inode_is_type(&fs->sb, inode, EXT4_INODE_MODE_SOFTLINK)) {
904 for (int i = 0; i < EXT4_INODE_BLOCKS; i++)
905 inode->blocks[i] = 0;
908 ext4_fs_inode_blocks_init(fs, inode_ref);
911 inode_ref->dirty = true;
916 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
918 struct ext4_fs *fs = inode_ref->fs;
922 #if CONFIG_EXTENT_ENABLE
923 /* For extents must be data block destroyed by other way */
924 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
925 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
926 /* Data structures are released during truncate operation... */
930 /* Release all indirect (no data) blocks */
932 /* 1) Single indirect */
933 ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
935 int rc = ext4_balloc_free_block(inode_ref, fblock);
939 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
942 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
943 uint32_t count = block_size / sizeof(uint32_t);
945 struct ext4_block block;
947 /* 2) Double indirect */
948 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
950 int rc = ext4_trans_block_get(fs->bdev, &block, fblock);
954 ext4_fsblk_t ind_block;
955 for (offset = 0; offset < count; ++offset) {
956 ind_block = to_le32(((uint32_t *)block.data)[offset]);
960 rc = ext4_balloc_free_block(inode_ref, ind_block);
962 ext4_block_set(fs->bdev, &block);
968 ext4_block_set(fs->bdev, &block);
969 rc = ext4_balloc_free_block(inode_ref, fblock);
973 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
976 /* 3) Tripple indirect */
977 struct ext4_block subblock;
978 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
981 rc = ext4_trans_block_get(fs->bdev, &block, fblock);
985 ext4_fsblk_t ind_block;
986 for (offset = 0; offset < count; ++offset) {
987 ind_block = to_le32(((uint32_t *)block.data)[offset]);
991 rc = ext4_trans_block_get(fs->bdev, &subblock,
994 ext4_block_set(fs->bdev, &block);
998 ext4_fsblk_t ind_subblk;
999 for (suboff = 0; suboff < count; ++suboff) {
1000 ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
1002 if (ind_subblk == 0)
1004 rc = ext4_balloc_free_block(inode_ref, ind_subblk);
1006 ext4_block_set(fs->bdev, &subblock);
1007 ext4_block_set(fs->bdev, &block);
1013 ext4_block_set(fs->bdev, &subblock);
1015 rc = ext4_balloc_free_block(inode_ref,
1018 ext4_block_set(fs->bdev, &block);
1024 ext4_block_set(fs->bdev, &block);
1025 rc = ext4_balloc_free_block(inode_ref, fblock);
1029 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
1031 /* Mark inode dirty for writing to the physical device */
1032 inode_ref->dirty = true;
1034 /* Free block with extended attributes if present */
1035 ext4_fsblk_t xattr_block =
1036 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
1038 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
1042 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
1045 /* Free inode by allocator */
1046 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
1047 EXT4_INODE_MODE_DIRECTORY))
1048 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
1050 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
1056 /**@brief Release data block from i-node
1057 * @param inode_ref I-node to release block from
1058 * @param iblock Logical block to be released
1059 * @return Error code
1061 static int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1064 ext4_fsblk_t fblock;
1066 struct ext4_fs *fs = inode_ref->fs;
1068 /* Extents are handled otherwise = there is not support in this function
1071 ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS) &&
1072 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1074 struct ext4_inode *inode = inode_ref->inode;
1076 /* Handle simple case when we are dealing with direct reference */
1077 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1078 fblock = ext4_inode_get_direct_block(inode, iblock);
1084 ext4_inode_set_direct_block(inode, iblock, 0);
1085 return ext4_balloc_free_block(inode_ref, fblock);
1088 /* Determine the indirection level needed to get the desired block */
1089 unsigned int level = 0;
1091 for (i = 1; i < 4; i++) {
1092 if (iblock < fs->inode_block_limits[i]) {
1101 /* Compute offsets for the topmost level */
1102 uint32_t block_offset_in_level =
1103 iblock - fs->inode_block_limits[level - 1];
1104 ext4_fsblk_t current_block =
1105 ext4_inode_get_indirect_block(inode, level - 1);
1106 uint32_t offset_in_block =
1107 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1110 * Navigate through other levels, until we find the block number
1111 * or find null reference meaning we are dealing with sparse file
1113 struct ext4_block block;
1118 if (current_block == 0)
1121 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1126 to_le32(((uint32_t *)block.data)[offset_in_block]);
1128 /* Set zero if physical data block address found */
1130 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1131 ext4_trans_set_block_dirty(block.buf);
1134 rc = ext4_block_set(fs->bdev, &block);
1141 * If we are on the last level, break here as
1142 * there is no next level to visit
1147 /* Visit the next level */
1148 block_offset_in_level %= fs->inode_blocks_per_level[level];
1149 offset_in_block = block_offset_in_level /
1150 fs->inode_blocks_per_level[level - 1];
1153 fblock = current_block;
1157 /* Physical block is not referenced, it can be released */
1158 return ext4_balloc_free_block(inode_ref, fblock);
1161 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
1163 struct ext4_sblock *sb = &inode_ref->fs->sb;
1167 /* Check flags, if i-node can be truncated */
1168 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
1171 /* If sizes are equal, nothing has to be done. */
1172 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
1173 if (old_size == new_size)
1176 /* It's not supported to make the larger file by truncate operation */
1177 if (old_size < new_size)
1181 v = ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK);
1182 if (v && old_size < sizeof(inode_ref->inode->blocks) &&
1183 !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
1184 char *content = (char *)inode_ref->inode->blocks + new_size;
1186 sizeof(inode_ref->inode->blocks) - (uint32_t)new_size);
1187 ext4_inode_set_size(inode_ref->inode, new_size);
1188 inode_ref->dirty = true;
1193 /* Compute how many blocks will be released */
1194 uint32_t block_size = ext4_sb_get_block_size(sb);
1195 uint32_t new_blocks_cnt = (uint32_t)((new_size + block_size - 1) / block_size);
1196 uint32_t old_blocks_cnt = (uint32_t)((old_size + block_size - 1) / block_size);
1197 uint32_t diff_blocks_cnt = old_blocks_cnt - new_blocks_cnt;
1198 #if CONFIG_EXTENT_ENABLE
1199 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_EXTENTS)) &&
1200 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1202 /* Extents require special operation */
1203 if (diff_blocks_cnt) {
1204 r = ext4_extent_remove_space(inode_ref, new_blocks_cnt,
1213 /* Release data blocks from the end of file */
1215 /* Starting from 1 because of logical blocks are numbered from 0
1217 for (i = 0; i < diff_blocks_cnt; ++i) {
1218 r = ext4_fs_release_inode_block(inode_ref,
1219 new_blocks_cnt + i);
1226 ext4_inode_set_size(inode_ref->inode, new_size);
1227 inode_ref->dirty = true;
1232 /**@brief Compute 'goal' for inode index
1233 * @param inode_ref Reference to inode, to allocate block for
1236 ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref)
1238 uint32_t grp_inodes = ext4_get32(&inode_ref->fs->sb, inodes_per_group);
1239 return (inode_ref->index - 1) / grp_inodes;
1242 /**@brief Compute 'goal' for allocation algorithm (For blockmap).
1243 * @param inode_ref Reference to inode, to allocate block for
1245 * @return error code
1247 int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
1251 struct ext4_sblock *sb = &inode_ref->fs->sb;
1254 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1255 uint32_t block_size = ext4_sb_get_block_size(sb);
1256 uint32_t iblock_cnt = (uint32_t)(inode_size / block_size);
1258 if (inode_size % block_size != 0)
1261 /* If inode has some blocks, get last block address + 1 */
1262 if (iblock_cnt > 0) {
1263 r = ext4_fs_get_inode_dblk_idx(inode_ref, iblock_cnt - 1,
1273 /* If goal == 0, sparse file -> continue */
1276 /* Identify block group of inode */
1278 uint32_t inodes_per_bg = ext4_get32(sb, inodes_per_group);
1279 uint32_t block_group = (inode_ref->index - 1) / inodes_per_bg;
1280 block_size = ext4_sb_get_block_size(sb);
1282 /* Load block group reference */
1283 struct ext4_block_group_ref bg_ref;
1284 r = ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
1288 struct ext4_bgroup *bg = bg_ref.block_group;
1290 /* Compute indexes */
1291 uint32_t bg_count = ext4_block_group_cnt(sb);
1292 ext4_fsblk_t itab_first_block = ext4_bg_get_inode_table_first_block(bg, sb);
1293 uint16_t itab_item_size = ext4_get16(sb, inode_size);
1294 uint32_t itab_bytes;
1296 /* Check for last block group */
1297 if (block_group < bg_count - 1) {
1298 itab_bytes = inodes_per_bg * itab_item_size;
1300 /* Last block group could be smaller */
1301 uint32_t inodes_cnt = ext4_get32(sb, inodes_count);
1303 itab_bytes = (inodes_cnt - ((bg_count - 1) * inodes_per_bg));
1304 itab_bytes *= itab_item_size;
1307 ext4_fsblk_t inode_table_blocks = itab_bytes / block_size;
1309 if (itab_bytes % block_size)
1310 inode_table_blocks++;
1312 *goal = itab_first_block + inode_table_blocks;
1314 return ext4_fs_put_block_group_ref(&bg_ref);
1317 static int ext4_fs_get_inode_dblk_idx_internal(struct ext4_inode_ref *inode_ref,
1318 uint64_t iblock, ext4_fsblk_t *fblock,
1320 bool support_unwritten __unused)
1322 struct ext4_fs *fs = inode_ref->fs;
1324 /* For empty file is situation simple */
1325 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1330 ext4_fsblk_t current_block;
1332 (void)extent_create;
1333 #if CONFIG_EXTENT_ENABLE
1334 /* Handle i-node using extents */
1335 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1336 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1338 ext4_fsblk_t current_fsblk;
1339 int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
1340 ¤t_fsblk, extent_create, NULL);
1344 current_block = current_fsblk;
1345 *fblock = current_block;
1347 ext4_assert(*fblock || support_unwritten);
1352 struct ext4_inode *inode = inode_ref->inode;
1354 /* Direct block are read directly from array in i-node structure */
1355 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1357 ext4_inode_get_direct_block(inode, (uint32_t)iblock);
1358 *fblock = current_block;
1362 /* Determine indirection level of the target block */
1365 for (i = 1; i < 4; i++) {
1366 if (iblock < fs->inode_block_limits[i]) {
1375 /* Compute offsets for the topmost level */
1376 uint32_t blk_off_in_lvl = iblock - fs->inode_block_limits[l - 1];
1377 current_block = ext4_inode_get_indirect_block(inode, l - 1);
1378 uint32_t off_in_blk = blk_off_in_lvl / fs->inode_blocks_per_level[l - 1];
1381 if (current_block == 0) {
1386 struct ext4_block block;
1389 * Navigate through other levels, until we find the block number
1390 * or find null reference meaning we are dealing with sparse file
1393 /* Load indirect block */
1394 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1398 /* Read block address from indirect block */
1400 to_le32(((uint32_t *)block.data)[off_in_blk]);
1402 /* Put back indirect block untouched */
1403 rc = ext4_block_set(fs->bdev, &block);
1407 /* Check for sparse file */
1408 if (current_block == 0) {
1413 /* Jump to the next level */
1416 /* Termination condition - we have address of data block loaded
1421 /* Visit the next level */
1422 blk_off_in_lvl %= fs->inode_blocks_per_level[l];
1423 off_in_blk = blk_off_in_lvl / fs->inode_blocks_per_level[l - 1];
1426 *fblock = current_block;
1432 int ext4_fs_get_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
1433 uint64_t iblock, ext4_fsblk_t *fblock,
1434 bool support_unwritten)
1436 return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock,
1437 false, support_unwritten);
1440 int ext4_fs_init_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
1441 uint64_t iblock, ext4_fsblk_t *fblock)
1443 return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock,
1447 static int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1448 uint32_t iblock, ext4_fsblk_t fblock)
1450 struct ext4_fs *fs = inode_ref->fs;
1452 #if CONFIG_EXTENT_ENABLE
1453 /* Handle inode using extents */
1454 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1455 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1461 /* Handle simple case when we are dealing with direct reference */
1462 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1463 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1465 inode_ref->dirty = true;
1470 /* Determine the indirection level needed to get the desired block */
1473 for (i = 1; i < 4; i++) {
1474 if (iblock < fs->inode_block_limits[i]) {
1483 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1485 /* Compute offsets for the topmost level */
1486 uint32_t blk_off_in_lvl = iblock - fs->inode_block_limits[l - 1];
1487 ext4_fsblk_t current_block =
1488 ext4_inode_get_indirect_block(inode_ref->inode, l - 1);
1489 uint32_t off_in_blk = blk_off_in_lvl / fs->inode_blocks_per_level[l - 1];
1491 ext4_fsblk_t new_blk;
1493 struct ext4_block block;
1494 struct ext4_block new_block;
1496 /* Is needed to allocate indirect block on the i-node level */
1497 if (current_block == 0) {
1498 /* Allocate new indirect block */
1500 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1504 rc = ext4_balloc_alloc_block(inode_ref, goal, &new_blk);
1509 ext4_inode_set_indirect_block(inode_ref->inode, l - 1,
1511 inode_ref->dirty = true;
1513 /* Load newly allocated block */
1514 rc = ext4_trans_block_get_noread(fs->bdev, &new_block, new_blk);
1516 ext4_balloc_free_block(inode_ref, new_blk);
1520 /* Initialize new block */
1521 memset(new_block.data, 0, block_size);
1522 ext4_trans_set_block_dirty(new_block.buf);
1524 /* Put back the allocated block */
1525 rc = ext4_block_set(fs->bdev, &new_block);
1529 current_block = new_blk;
1533 * Navigate through other levels, until we find the block number
1534 * or find null reference meaning we are dealing with sparse file
1537 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1541 current_block = to_le32(((uint32_t *)block.data)[off_in_blk]);
1542 if ((l > 1) && (current_block == 0)) {
1544 rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1546 ext4_block_set(fs->bdev, &block);
1550 /* Allocate new block */
1552 ext4_balloc_alloc_block(inode_ref, goal, &new_blk);
1554 ext4_block_set(fs->bdev, &block);
1558 /* Load newly allocated block */
1559 rc = ext4_trans_block_get_noread(fs->bdev, &new_block,
1563 ext4_block_set(fs->bdev, &block);
1567 /* Initialize allocated block */
1568 memset(new_block.data, 0, block_size);
1569 ext4_trans_set_block_dirty(new_block.buf);
1571 rc = ext4_block_set(fs->bdev, &new_block);
1573 ext4_block_set(fs->bdev, &block);
1577 /* Write block address to the parent */
1578 uint32_t * p = (uint32_t * )block.data;
1579 p[off_in_blk] = to_le32((uint32_t)new_blk);
1580 ext4_trans_set_block_dirty(block.buf);
1581 current_block = new_blk;
1584 /* Will be finished, write the fblock address */
1586 uint32_t * p = (uint32_t * )block.data;
1587 p[off_in_blk] = to_le32((uint32_t)fblock);
1588 ext4_trans_set_block_dirty(block.buf);
1591 rc = ext4_block_set(fs->bdev, &block);
1598 * If we are on the last level, break here as
1599 * there is no next level to visit
1604 /* Visit the next level */
1605 blk_off_in_lvl %= fs->inode_blocks_per_level[l];
1606 off_in_blk = blk_off_in_lvl / fs->inode_blocks_per_level[l - 1];
1613 int ext4_fs_append_inode_dblk(struct ext4_inode_ref *inode_ref,
1614 ext4_fsblk_t *fblock, uint32_t *iblock)
1616 #if CONFIG_EXTENT_ENABLE
1617 /* Handle extents separately */
1618 if ((ext4_sb_feature_incom(&inode_ref->fs->sb, EXT4_FINCOM_EXTENTS)) &&
1619 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1621 ext4_fsblk_t current_fsblk;
1622 struct ext4_sblock *sb = &inode_ref->fs->sb;
1623 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1624 uint32_t block_size = ext4_sb_get_block_size(sb);
1625 *iblock = (uint32_t)((inode_size + block_size - 1) / block_size);
1627 rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
1628 ¤t_fsblk, true, NULL);
1630 *fblock = current_fsblk;
1631 ext4_assert(*fblock);
1633 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1634 inode_ref->dirty = true;
1640 struct ext4_sblock *sb = &inode_ref->fs->sb;
1642 /* Compute next block index and allocate data block */
1643 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1644 uint32_t block_size = ext4_sb_get_block_size(sb);
1646 /* Align size i-node size */
1647 if ((inode_size % block_size) != 0)
1648 inode_size += block_size - (inode_size % block_size);
1650 /* Logical blocks are numbered from 0 */
1651 uint32_t new_block_idx = (uint32_t)(inode_size / block_size);
1653 /* Allocate new physical block */
1654 ext4_fsblk_t goal, phys_block;
1655 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1659 rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block);
1663 /* Add physical block address to the i-node */
1664 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1667 ext4_balloc_free_block(inode_ref, phys_block);
1672 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1673 inode_ref->dirty = true;
1675 *fblock = phys_block;
1676 *iblock = new_block_idx;
1681 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1685 link = ext4_inode_get_links_cnt(inode_ref->inode);
1687 ext4_inode_set_links_cnt(inode_ref->inode, link);
1689 is_dx = ext4_sb_feature_com(&inode_ref->fs->sb, EXT4_FCOM_DIR_INDEX) &&
1690 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1692 if (is_dx && link > 1) {
1693 if (link >= EXT4_LINK_MAX || link == 2) {
1694 ext4_inode_set_links_cnt(inode_ref->inode, 1);
1697 v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1698 v |= EXT4_FRO_COM_DIR_NLINK;
1699 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1704 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1706 uint16_t links = ext4_inode_get_links_cnt(inode_ref->inode);
1707 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1708 EXT4_INODE_MODE_DIRECTORY)) {
1710 ext4_inode_set_links_cnt(inode_ref->inode, links - 1);
1715 ext4_inode_set_links_cnt(inode_ref->inode, links - 1);