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>
43 #include <ext4_misc.h>
44 #include <ext4_errno.h>
45 #include <ext4_debug.h>
47 #include <ext4_trans.h>
49 #include <ext4_blockdev.h>
50 #include <ext4_super.h>
51 #include <ext4_crc32.h>
52 #include <ext4_block_group.h>
53 #include <ext4_balloc.h>
54 #include <ext4_bitmap.h>
55 #include <ext4_inode.h>
56 #include <ext4_ialloc.h>
57 #include <ext4_extent.h>
61 int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev,
68 ext4_assert(fs && bdev);
72 fs->read_only = read_only;
74 r = ext4_sb_read(fs->bdev, &fs->sb);
78 if (!ext4_sb_check(&fs->sb))
81 bsize = ext4_sb_get_block_size(&fs->sb);
82 if (bsize > EXT4_MAX_BLOCK_SIZE)
85 r = ext4_fs_check_features(fs, &read_only);
90 fs->read_only = read_only;
92 /* Compute limits for indirect block levels */
93 uint32_t blocks_id = bsize / sizeof(uint32_t);
95 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
96 fs->inode_blocks_per_level[0] = 1;
98 for (i = 1; i < 4; i++) {
99 fs->inode_blocks_per_level[i] =
100 fs->inode_blocks_per_level[i - 1] * blocks_id;
101 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
102 fs->inode_blocks_per_level[i];
106 tmp = ext4_get16(&fs->sb, state);
107 if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS)
108 ext4_dbg(DEBUG_FS, DBG_WARN
109 "last umount error: superblock fs_error flag\n");
112 if (!fs->read_only) {
113 /* Mark system as mounted */
114 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS);
115 r = ext4_sb_write(fs->bdev, &fs->sb);
119 /*Update mount count*/
120 ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1);
126 int ext4_fs_fini(struct ext4_fs *fs)
130 /*Set superblock state*/
131 ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS);
134 return ext4_sb_write(fs->bdev, &fs->sb);
139 static void ext4_fs_debug_features_inc(uint32_t features_incompatible)
141 if (features_incompatible & EXT4_FINCOM_COMPRESSION)
142 ext4_dbg(DEBUG_FS, DBG_NONE "compression\n");
143 if (features_incompatible & EXT4_FINCOM_FILETYPE)
144 ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n");
145 if (features_incompatible & EXT4_FINCOM_RECOVER)
146 ext4_dbg(DEBUG_FS, DBG_NONE "recover\n");
147 if (features_incompatible & EXT4_FINCOM_JOURNAL_DEV)
148 ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n");
149 if (features_incompatible & EXT4_FINCOM_META_BG)
150 ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n");
151 if (features_incompatible & EXT4_FINCOM_EXTENTS)
152 ext4_dbg(DEBUG_FS, DBG_NONE "extents\n");
153 if (features_incompatible & EXT4_FINCOM_64BIT)
154 ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n");
155 if (features_incompatible & EXT4_FINCOM_MMP)
156 ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n");
157 if (features_incompatible & EXT4_FINCOM_FLEX_BG)
158 ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n");
159 if (features_incompatible & EXT4_FINCOM_EA_INODE)
160 ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n");
161 if (features_incompatible & EXT4_FINCOM_DIRDATA)
162 ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n");
163 if (features_incompatible & EXT4_FINCOM_BG_USE_META_CSUM)
164 ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n");
165 if (features_incompatible & EXT4_FINCOM_LARGEDIR)
166 ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n");
167 if (features_incompatible & EXT4_FINCOM_INLINE_DATA)
168 ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n");
170 static void ext4_fs_debug_features_comp(uint32_t features_compatible)
172 if (features_compatible & EXT4_FCOM_DIR_PREALLOC)
173 ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n");
174 if (features_compatible & EXT4_FCOM_IMAGIC_INODES)
175 ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n");
176 if (features_compatible & EXT4_FCOM_HAS_JOURNAL)
177 ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n");
178 if (features_compatible & EXT4_FCOM_EXT_ATTR)
179 ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n");
180 if (features_compatible & EXT4_FCOM_RESIZE_INODE)
181 ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n");
182 if (features_compatible & EXT4_FCOM_DIR_INDEX)
183 ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n");
186 static void ext4_fs_debug_features_ro(uint32_t features_ro)
188 if (features_ro & EXT4_FRO_COM_SPARSE_SUPER)
189 ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n");
190 if (features_ro & EXT4_FRO_COM_LARGE_FILE)
191 ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n");
192 if (features_ro & EXT4_FRO_COM_BTREE_DIR)
193 ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n");
194 if (features_ro & EXT4_FRO_COM_HUGE_FILE)
195 ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n");
196 if (features_ro & EXT4_FRO_COM_GDT_CSUM)
197 ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n");
198 if (features_ro & EXT4_FRO_COM_DIR_NLINK)
199 ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n");
200 if (features_ro & EXT4_FRO_COM_EXTRA_ISIZE)
201 ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n");
202 if (features_ro & EXT4_FRO_COM_QUOTA)
203 ext4_dbg(DEBUG_FS, DBG_NONE "quota\n");
204 if (features_ro & EXT4_FRO_COM_BIGALLOC)
205 ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n");
206 if (features_ro & EXT4_FRO_COM_METADATA_CSUM)
207 ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n");
210 int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
212 ext4_assert(fs && read_only);
214 if (ext4_get32(&fs->sb, rev_level) == 0) {
219 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n");
220 ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible));
222 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n");
223 ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible));
225 ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n");
226 ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only));
228 /*Check features_incompatible*/
229 v = (ext4_get32(&fs->sb, features_incompatible) &
230 (~CONFIG_SUPPORTED_FINCOM));
232 ext4_dbg(DEBUG_FS, DBG_ERROR
233 "sblock has unsupported features incompatible:\n");
234 ext4_fs_debug_features_inc(v);
238 /*Check features_read_only*/
239 v = ext4_get32(&fs->sb, features_read_only);
240 v &= ~CONFIG_SUPPORTED_FRO_COM;
242 ext4_dbg(DEBUG_FS, DBG_WARN
243 "sblock has unsupported features read only:\n");
244 ext4_fs_debug_features_ro(v);
253 /**@brief Determine whether the block is inside the group.
254 * @param baddr block address
255 * @param bgid block group id
258 static bool ext4_block_in_group(struct ext4_sblock *s, ext4_fsblk_t baddr,
261 uint32_t actual_bgid;
262 actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
263 if (actual_bgid == bgid)
268 /**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only
269 * need to use it within a single byte (to ensure we get endianness right).
270 * We can use memset for the rest of the bitmap as there are no other users.
272 static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
276 if (start_bit >= end_bit)
279 for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
280 ext4_bmap_bit_set(bitmap, i);
283 memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
286 /**@brief Initialize block bitmap in block group.
287 * @param bg_ref Reference to block group
290 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
292 struct ext4_sblock *sb = &bg_ref->fs->sb;
293 struct ext4_bgroup *bg = bg_ref->block_group;
296 uint32_t bit, bit_max;
297 uint32_t group_blocks;
298 uint16_t inode_size = ext4_get16(sb, inode_size);
299 uint32_t block_size = ext4_sb_get_block_size(sb);
300 uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
303 ext4_fsblk_t bmp_blk = ext4_bg_get_block_bitmap(bg, sb);
304 ext4_fsblk_t bmp_inode = ext4_bg_get_inode_bitmap(bg, sb);
305 ext4_fsblk_t inode_table = ext4_bg_get_inode_table_first_block(bg, sb);
306 ext4_fsblk_t first_bg = ext4_balloc_get_block_of_bgid(sb, bg_ref->index);
308 uint32_t dsc_per_block = block_size / ext4_sb_get_desc_size(sb);
310 bool flex_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_FLEX_BG);
311 bool meta_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_META_BG);
313 uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
315 struct ext4_block block_bitmap;
316 rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &block_bitmap, bmp_blk);
320 memset(block_bitmap.data, 0, block_size);
321 bit_max = ext4_sb_is_super_in_bg(sb, bg_ref->index);
323 uint32_t count = ext4_sb_first_meta_bg(sb) * dsc_per_block;
324 if (!meta_bg || bg_ref->index < count) {
326 bit_max += ext4_bg_num_gdb(sb, bg_ref->index);
327 bit_max += ext4_get16(sb, s_reserved_gdt_blocks);
329 } else { /* For META_BG_BLOCK_GROUPS */
330 bit_max += ext4_bg_num_gdb(sb, bg_ref->index);
332 for (bit = 0; bit < bit_max; bit++)
333 ext4_bmap_bit_set(block_bitmap.data, bit);
335 if (bg_ref->index == ext4_block_group_cnt(sb) - 1) {
337 * Even though mke2fs always initialize first and last group
338 * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
339 * to make sure we calculate the right free blocks
342 group_blocks = (uint32_t)(ext4_sb_get_blocks_cnt(sb) -
343 ext4_get32(sb, first_data_block) -
344 ext4_get32(sb, blocks_per_group) *
345 (ext4_block_group_cnt(sb) - 1));
347 group_blocks = ext4_get32(sb, blocks_per_group);
351 in_bg = ext4_block_in_group(sb, bmp_blk, bg_ref->index);
352 if (!flex_bg || in_bg)
353 ext4_bmap_bit_set(block_bitmap.data,
354 (uint32_t)(bmp_blk - first_bg));
356 in_bg = ext4_block_in_group(sb, bmp_inode, bg_ref->index);
357 if (!flex_bg || in_bg)
358 ext4_bmap_bit_set(block_bitmap.data,
359 (uint32_t)(bmp_inode - first_bg));
361 for (i = inode_table; i < inode_table + inode_table_bcnt; i++) {
362 in_bg = ext4_block_in_group(sb, i, bg_ref->index);
363 if (!flex_bg || in_bg)
364 ext4_bmap_bit_set(block_bitmap.data,
365 (uint32_t)(i - first_bg));
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 ext4_trans_set_block_dirty(block_bitmap.buf);
375 ext4_balloc_set_bitmap_csum(sb, bg_ref->block_group, block_bitmap.data);
376 bg_ref->dirty = true;
379 return ext4_block_set(bg_ref->fs->bdev, &block_bitmap);
382 /**@brief Initialize i-node bitmap in block group.
383 * @param bg_ref Reference to block group
386 static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref)
389 struct ext4_sblock *sb = &bg_ref->fs->sb;
390 struct ext4_bgroup *bg = bg_ref->block_group;
393 ext4_fsblk_t bitmap_block_addr = ext4_bg_get_inode_bitmap(bg, sb);
396 rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, bitmap_block_addr);
400 /* Initialize all bitmap bits to zero */
401 uint32_t block_size = ext4_sb_get_block_size(sb);
402 uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
404 memset(b.data, 0, (inodes_per_group + 7) / 8);
406 uint32_t start_bit = inodes_per_group;
407 uint32_t end_bit = block_size * 8;
410 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
411 ext4_bmap_bit_set(b.data, i);
414 memset(b.data + (i >> 3), 0xff, (end_bit - i) >> 3);
416 ext4_trans_set_block_dirty(b.buf);
418 ext4_ialloc_set_bitmap_csum(sb, bg, b.data);
419 bg_ref->dirty = true;
422 return ext4_block_set(bg_ref->fs->bdev, &b);
425 /**@brief Initialize i-node table in block group.
426 * @param bg_ref Reference to block group
429 static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref)
431 struct ext4_sblock *sb = &bg_ref->fs->sb;
432 struct ext4_bgroup *bg = bg_ref->block_group;
434 uint32_t inode_size = ext4_get16(sb, inode_size);
435 uint32_t block_size = ext4_sb_get_block_size(sb);
436 uint32_t inodes_per_block = block_size / inode_size;
437 uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index);
438 uint32_t table_blocks = inodes_in_group / inodes_per_block;
441 if (inodes_in_group % inodes_per_block)
444 /* Compute initialization bounds */
445 ext4_fsblk_t first_block = ext4_bg_get_inode_table_first_block(bg, sb);
447 ext4_fsblk_t last_block = first_block + table_blocks - 1;
449 /* Initialization of all itable blocks */
450 for (fblock = first_block; fblock <= last_block; ++fblock) {
452 int rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, fblock);
456 memset(b.data, 0, block_size);
457 ext4_trans_set_block_dirty(b.buf);
459 rc = ext4_block_set(bg_ref->fs->bdev, &b);
467 static ext4_fsblk_t ext4_fs_get_descriptor_block(struct ext4_sblock *s,
469 uint32_t dsc_per_block)
471 uint32_t first_meta_bg, dsc_id;
473 dsc_id = bgid / dsc_per_block;
474 first_meta_bg = ext4_sb_first_meta_bg(s);
476 bool meta_bg = ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG);
478 if (!meta_bg || dsc_id < first_meta_bg)
479 return ext4_get32(s, first_data_block) + dsc_id + 1;
481 if (ext4_sb_is_super_in_bg(s, bgid))
484 return (has_super + ext4_fs_first_bg_block_no(s, bgid));
487 /**@brief Compute checksum of block group descriptor.
488 * @param sb Superblock
489 * @param bgid Index of block group in the filesystem
490 * @param bg Block group to compute checksum for
491 * @return Checksum value
493 static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid,
494 struct ext4_bgroup *bg)
496 /* If checksum not supported, 0 will be returned */
498 #if CONFIG_META_CSUM_ENABLE
499 /* Compute the checksum only if the filesystem supports it */
500 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
501 /* Use metadata_csum algorithm instead */
502 uint32_t le32_bgid = to_le32(bgid);
503 uint32_t orig_checksum, checksum;
505 /* Preparation: temporarily set bg checksum to 0 */
506 orig_checksum = bg->checksum;
509 /* First calculate crc32 checksum against fs uuid */
510 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
512 /* Then calculate crc32 checksum against bgid */
513 checksum = ext4_crc32c(checksum, &le32_bgid, sizeof(bgid));
514 /* Finally calculate crc32 checksum against block_group_desc */
515 checksum = ext4_crc32c(checksum, bg, ext4_sb_get_desc_size(sb));
516 bg->checksum = orig_checksum;
518 crc = checksum & 0xFFFF;
522 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_GDT_CSUM)) {
523 uint8_t *base = (uint8_t *)bg;
524 uint8_t *checksum = (uint8_t *)&bg->checksum;
526 uint32_t offset = (uint32_t)(checksum - base);
528 /* Convert block group index to little endian */
529 uint32_t group = to_le32(bgid);
532 crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid));
534 /* Include index of block group */
535 crc = ext4_bg_crc16(crc, (uint8_t *)&group, sizeof(group));
537 /* Compute crc from the first part (stop before checksum field)
539 crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset);
542 offset += sizeof(bg->checksum);
544 /* Checksum of the rest of block group descriptor */
545 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_64BIT)) &&
546 (offset < ext4_sb_get_desc_size(sb))) {
548 const uint8_t *start = ((uint8_t *)bg) + offset;
549 size_t len = ext4_sb_get_desc_size(sb) - offset;
550 crc = ext4_bg_crc16(crc, start, len);
556 #if CONFIG_META_CSUM_ENABLE
557 static bool ext4_fs_verify_bg_csum(struct ext4_sblock *sb,
559 struct ext4_bgroup *bg)
561 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
564 return ext4_fs_bg_checksum(sb, bgid, bg) == to_le16(bg->checksum);
567 #define ext4_fs_verify_bg_csum(...) true
570 int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
571 struct ext4_block_group_ref *ref)
573 /* Compute number of descriptors, that fits in one data block */
574 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
575 uint32_t dsc_cnt = block_size / ext4_sb_get_desc_size(&fs->sb);
577 /* Block group descriptor table starts at the next block after
579 uint64_t block_id = ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_cnt);
581 uint32_t offset = (bgid % dsc_cnt) * ext4_sb_get_desc_size(&fs->sb);
583 int rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id);
587 ref->block_group = (void *)(ref->block.data + offset);
591 struct ext4_bgroup *bg = ref->block_group;
593 if (!ext4_fs_verify_bg_csum(&fs->sb, bgid, bg)) {
595 DBG_WARN "Block group descriptor checksum failed."
596 "Block group index: %" PRIu32"\n",
600 if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
601 rc = ext4_fs_init_block_bitmap(ref);
603 ext4_block_set(fs->bdev, &ref->block);
606 ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT);
610 if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT)) {
611 rc = ext4_fs_init_inode_bitmap(ref);
613 ext4_block_set(ref->fs->bdev, &ref->block);
617 ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT);
619 if (!ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
620 rc = ext4_fs_init_inode_table(ref);
622 ext4_block_set(fs->bdev, &ref->block);
626 ext4_bg_set_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED);
635 int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref)
637 /* Check if reference modified */
639 /* Compute new checksum of block group */
641 cs = ext4_fs_bg_checksum(&ref->fs->sb, ref->index,
643 ref->block_group->checksum = to_le16(cs);
645 /* Mark block dirty for writing changes to physical device */
646 ext4_trans_set_block_dirty(ref->block.buf);
649 /* Put back block, that contains block group descriptor */
650 return ext4_block_set(ref->fs->bdev, &ref->block);
653 #if CONFIG_META_CSUM_ENABLE
654 static uint32_t ext4_fs_inode_checksum(struct ext4_inode_ref *inode_ref)
656 uint32_t checksum = 0;
657 struct ext4_sblock *sb = &inode_ref->fs->sb;
658 uint16_t inode_size = ext4_get16(sb, inode_size);
660 if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
661 uint32_t orig_checksum;
663 uint32_t ino_index = to_le32(inode_ref->index);
665 to_le32(ext4_inode_get_generation(inode_ref->inode));
667 /* Preparation: temporarily set bg checksum to 0 */
668 orig_checksum = ext4_inode_get_csum(sb, inode_ref->inode);
669 ext4_inode_set_csum(sb, inode_ref->inode, 0);
671 /* First calculate crc32 checksum against fs uuid */
672 checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid,
674 /* Then calculate crc32 checksum against inode number
675 * and inode generation */
676 checksum = ext4_crc32c(checksum, &ino_index, sizeof(ino_index));
677 checksum = ext4_crc32c(checksum, &ino_gen, sizeof(ino_gen));
678 /* Finally calculate crc32 checksum against
679 * the entire inode */
680 checksum = ext4_crc32c(checksum, inode_ref->inode, inode_size);
681 ext4_inode_set_csum(sb, inode_ref->inode, orig_checksum);
683 /* If inode size is not large enough to hold the
684 * upper 16bit of the checksum */
685 if (inode_size == EXT4_GOOD_OLD_INODE_SIZE)
692 #define ext4_fs_inode_checksum(...) 0
695 static void ext4_fs_set_inode_checksum(struct ext4_inode_ref *inode_ref)
697 struct ext4_sblock *sb = &inode_ref->fs->sb;
698 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
701 uint32_t csum = ext4_fs_inode_checksum(inode_ref);
702 ext4_inode_set_csum(sb, inode_ref->inode, csum);
705 #if CONFIG_META_CSUM_ENABLE
706 static bool ext4_fs_verify_inode_csum(struct ext4_inode_ref *inode_ref)
708 struct ext4_sblock *sb = &inode_ref->fs->sb;
709 if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
712 return ext4_inode_get_csum(sb, inode_ref->inode) ==
713 ext4_fs_inode_checksum(inode_ref);
716 #define ext4_fs_verify_inode_csum(...) true
720 __ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
721 struct ext4_inode_ref *ref,
724 /* Compute number of i-nodes, that fits in one data block */
725 uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group);
728 * Inode numbers are 1-based, but it is simpler to work with 0-based
729 * when computing indices
732 uint32_t block_group = index / inodes_per_group;
733 uint32_t offset_in_group = index % inodes_per_group;
735 /* Load block group, where i-node is located */
736 struct ext4_block_group_ref bg_ref;
738 int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
743 /* Load block address, where i-node table is located */
744 ext4_fsblk_t inode_table_start =
745 ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb);
747 /* Put back block group reference (not needed more) */
748 rc = ext4_fs_put_block_group_ref(&bg_ref);
753 /* Compute position of i-node in the block group */
754 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
755 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
756 uint32_t byte_offset_in_group = offset_in_group * inode_size;
758 /* Compute block address */
759 ext4_fsblk_t block_id =
760 inode_table_start + (byte_offset_in_group / block_size);
762 rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id);
767 /* Compute position of i-node in the data block */
768 uint32_t offset_in_block = byte_offset_in_group % block_size;
769 ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block);
771 /* We need to store the original value of index in the reference */
772 ref->index = index + 1;
776 if (initialized && !ext4_fs_verify_inode_csum(ref)) {
778 DBG_WARN "Inode checksum failed."
779 "Inode: %" PRIu32"\n",
786 int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
787 struct ext4_inode_ref *ref)
789 return __ext4_fs_get_inode_ref(fs, index, ref, true);
792 int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref)
794 /* Check if reference modified */
796 /* Mark block dirty for writing changes to physical device */
797 ext4_fs_set_inode_checksum(ref);
798 ext4_trans_set_block_dirty(ref->block.buf);
801 /* Put back block, that contains i-node */
802 return ext4_block_set(ref->fs->bdev, &ref->block);
805 void ext4_fs_inode_blocks_init(struct ext4_fs *fs,
806 struct ext4_inode_ref *inode_ref)
808 struct ext4_inode *inode = inode_ref->inode;
810 /* Reset blocks array. For inode which is not directory or file, just
811 * fill in blocks with 0 */
812 switch (ext4_inode_type(&fs->sb, inode_ref->inode)) {
813 case EXT4_INODE_MODE_FILE:
814 case EXT4_INODE_MODE_DIRECTORY:
820 #if CONFIG_EXTENT_ENABLE
821 /* Initialize extents if needed */
822 if (ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) {
823 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
825 /* Initialize extent root header */
826 ext4_extent_tree_init(inode_ref);
829 inode_ref->dirty = true;
833 uint32_t ext4_fs_correspond_inode_mode(int filetype)
837 return EXT4_INODE_MODE_DIRECTORY;
838 case EXT4_DE_REG_FILE:
839 return EXT4_INODE_MODE_FILE;
840 case EXT4_DE_SYMLINK:
841 return EXT4_INODE_MODE_SOFTLINK;
843 return EXT4_INODE_MODE_CHARDEV;
845 return EXT4_INODE_MODE_BLOCKDEV;
847 return EXT4_INODE_MODE_FIFO;
849 return EXT4_INODE_MODE_SOCKET;
851 /* FIXME: unsupported filetype */
852 return EXT4_INODE_MODE_FILE;
855 int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
858 /* Check if newly allocated i-node will be a directory */
860 uint16_t inode_size = ext4_get16(&fs->sb, inode_size);
862 is_dir = (filetype == EXT4_DE_DIR);
864 /* Allocate inode by allocation algorithm */
866 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
870 /* Load i-node from on-disk i-node table */
871 rc = __ext4_fs_get_inode_ref(fs, index, inode_ref, false);
873 ext4_ialloc_free_inode(fs, index, is_dir);
877 /* Initialize i-node */
878 struct ext4_inode *inode = inode_ref->inode;
880 memset(inode, 0, inode_size);
885 * Default directory permissions to be compatible with other
887 * 0777 (octal) == rwxrwxrwx
891 mode |= EXT4_INODE_MODE_DIRECTORY;
892 } else if (filetype == EXT4_DE_SYMLINK) {
894 * Default symbolic link permissions to be compatible with other systems
895 * 0777 (octal) == rwxrwxrwx
899 mode |= EXT4_INODE_MODE_SOFTLINK;
902 * Default file permissions to be compatible with other systems
903 * 0666 (octal) == rw-rw-rw-
907 mode |= ext4_fs_correspond_inode_mode(filetype);
909 ext4_inode_set_mode(&fs->sb, inode, mode);
911 ext4_inode_set_links_cnt(inode, 0);
912 ext4_inode_set_uid(inode, 0);
913 ext4_inode_set_gid(inode, 0);
914 ext4_inode_set_size(inode, 0);
915 ext4_inode_set_access_time(inode, 0);
916 ext4_inode_set_change_inode_time(inode, 0);
917 ext4_inode_set_modif_time(inode, 0);
918 ext4_inode_set_del_time(inode, 0);
919 ext4_inode_set_blocks_count(&fs->sb, inode, 0);
920 ext4_inode_set_flags(inode, 0);
921 ext4_inode_set_generation(inode, 0);
922 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
923 uint16_t size = ext4_get16(&fs->sb, want_extra_isize);
924 ext4_inode_set_extra_isize(&fs->sb, inode, size);
927 memset(inode->blocks, 0, sizeof(inode->blocks));
928 inode_ref->dirty = true;
933 int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref)
935 struct ext4_fs *fs = inode_ref->fs;
939 #if CONFIG_EXTENT_ENABLE
940 /* For extents must be data block destroyed by other way */
941 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
942 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
943 /* Data structures are released during truncate operation... */
947 /* Release all indirect (no data) blocks */
949 /* 1) Single indirect */
950 ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
952 int rc = ext4_balloc_free_block(inode_ref, fblock);
956 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
959 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
960 uint32_t count = block_size / sizeof(uint32_t);
962 struct ext4_block block;
964 /* 2) Double indirect */
965 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
967 int rc = ext4_trans_block_get(fs->bdev, &block, fblock);
971 ext4_fsblk_t ind_block;
972 for (offset = 0; offset < count; ++offset) {
973 ind_block = to_le32(((uint32_t *)block.data)[offset]);
977 rc = ext4_balloc_free_block(inode_ref, ind_block);
979 ext4_block_set(fs->bdev, &block);
985 ext4_block_set(fs->bdev, &block);
986 rc = ext4_balloc_free_block(inode_ref, fblock);
990 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
993 /* 3) Tripple indirect */
994 struct ext4_block subblock;
995 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
998 rc = ext4_trans_block_get(fs->bdev, &block, fblock);
1002 ext4_fsblk_t ind_block;
1003 for (offset = 0; offset < count; ++offset) {
1004 ind_block = to_le32(((uint32_t *)block.data)[offset]);
1008 rc = ext4_trans_block_get(fs->bdev, &subblock,
1011 ext4_block_set(fs->bdev, &block);
1015 ext4_fsblk_t ind_subblk;
1016 for (suboff = 0; suboff < count; ++suboff) {
1017 ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]);
1019 if (ind_subblk == 0)
1021 rc = ext4_balloc_free_block(inode_ref, ind_subblk);
1023 ext4_block_set(fs->bdev, &subblock);
1024 ext4_block_set(fs->bdev, &block);
1030 ext4_block_set(fs->bdev, &subblock);
1032 rc = ext4_balloc_free_block(inode_ref,
1035 ext4_block_set(fs->bdev, &block);
1041 ext4_block_set(fs->bdev, &block);
1042 rc = ext4_balloc_free_block(inode_ref, fblock);
1046 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
1048 /* Mark inode dirty for writing to the physical device */
1049 inode_ref->dirty = true;
1051 /* Free block with extended attributes if present */
1052 ext4_fsblk_t xattr_block =
1053 ext4_inode_get_file_acl(inode_ref->inode, &fs->sb);
1055 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
1059 ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0);
1062 /* Free inode by allocator */
1063 if (ext4_inode_is_type(&fs->sb, inode_ref->inode,
1064 EXT4_INODE_MODE_DIRECTORY))
1065 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
1067 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
1073 /**@brief Release data block from i-node
1074 * @param inode_ref I-node to release block from
1075 * @param iblock Logical block to be released
1076 * @return Error code
1078 static int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref,
1081 ext4_fsblk_t fblock;
1083 struct ext4_fs *fs = inode_ref->fs;
1085 /* Extents are handled otherwise = there is not support in this function
1088 ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS) &&
1089 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1091 struct ext4_inode *inode = inode_ref->inode;
1093 /* Handle simple case when we are dealing with direct reference */
1094 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1095 fblock = ext4_inode_get_direct_block(inode, iblock);
1101 ext4_inode_set_direct_block(inode, iblock, 0);
1102 return ext4_balloc_free_block(inode_ref, fblock);
1105 /* Determine the indirection level needed to get the desired block */
1106 unsigned int level = 0;
1108 for (i = 1; i < 4; i++) {
1109 if (iblock < fs->inode_block_limits[i]) {
1118 /* Compute offsets for the topmost level */
1119 uint32_t block_offset_in_level =
1120 (uint32_t)(iblock - fs->inode_block_limits[level - 1]);
1121 ext4_fsblk_t current_block =
1122 ext4_inode_get_indirect_block(inode, level - 1);
1123 uint32_t offset_in_block =
1124 (uint32_t)(block_offset_in_level / fs->inode_blocks_per_level[level - 1]);
1127 * Navigate through other levels, until we find the block number
1128 * or find null reference meaning we are dealing with sparse file
1130 struct ext4_block block;
1135 if (current_block == 0)
1138 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1143 to_le32(((uint32_t *)block.data)[offset_in_block]);
1145 /* Set zero if physical data block address found */
1147 ((uint32_t *)block.data)[offset_in_block] = to_le32(0);
1148 ext4_trans_set_block_dirty(block.buf);
1151 rc = ext4_block_set(fs->bdev, &block);
1158 * If we are on the last level, break here as
1159 * there is no next level to visit
1164 /* Visit the next level */
1165 block_offset_in_level %= fs->inode_blocks_per_level[level];
1166 offset_in_block = (uint32_t)(block_offset_in_level /
1167 fs->inode_blocks_per_level[level - 1]);
1170 fblock = current_block;
1174 /* Physical block is not referenced, it can be released */
1175 return ext4_balloc_free_block(inode_ref, fblock);
1178 int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size)
1180 struct ext4_sblock *sb = &inode_ref->fs->sb;
1185 /* Check flags, if i-node can be truncated */
1186 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
1189 /* If sizes are equal, nothing has to be done. */
1190 uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
1191 if (old_size == new_size)
1194 /* It's not supported to make the larger file by truncate operation */
1195 if (old_size < new_size)
1198 /* For symbolic link which is small enough */
1199 v = ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK);
1200 if (v && old_size < sizeof(inode_ref->inode->blocks) &&
1201 !ext4_inode_get_blocks_count(sb, inode_ref->inode)) {
1202 char *content = (char *)inode_ref->inode->blocks + new_size;
1204 sizeof(inode_ref->inode->blocks) - (uint32_t)new_size);
1205 ext4_inode_set_size(inode_ref->inode, new_size);
1206 inode_ref->dirty = true;
1211 i = ext4_inode_type(sb, inode_ref->inode);
1212 if (i == EXT4_INODE_MODE_CHARDEV ||
1213 i == EXT4_INODE_MODE_BLOCKDEV ||
1214 i == EXT4_INODE_MODE_SOCKET) {
1215 inode_ref->inode->blocks[0] = 0;
1216 inode_ref->inode->blocks[1] = 0;
1218 inode_ref->dirty = true;
1222 /* Compute how many blocks will be released */
1223 uint32_t block_size = ext4_sb_get_block_size(sb);
1224 uint32_t new_blocks_cnt = (uint32_t)((new_size + block_size - 1) / block_size);
1225 uint32_t old_blocks_cnt = (uint32_t)((old_size + block_size - 1) / block_size);
1226 uint32_t diff_blocks_cnt = old_blocks_cnt - new_blocks_cnt;
1227 #if CONFIG_EXTENT_ENABLE
1228 if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_EXTENTS)) &&
1229 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1231 /* Extents require special operation */
1232 if (diff_blocks_cnt) {
1233 r = ext4_extent_remove_space(inode_ref, new_blocks_cnt,
1242 /* Release data blocks from the end of file */
1244 /* Starting from 1 because of logical blocks are numbered from 0
1246 for (i = 0; i < diff_blocks_cnt; ++i) {
1247 r = ext4_fs_release_inode_block(inode_ref,
1248 new_blocks_cnt + i);
1255 ext4_inode_set_size(inode_ref->inode, new_size);
1256 inode_ref->dirty = true;
1261 /**@brief Compute 'goal' for inode index
1262 * @param inode_ref Reference to inode, to allocate block for
1265 ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref)
1267 uint32_t grp_inodes = ext4_get32(&inode_ref->fs->sb, inodes_per_group);
1268 return (inode_ref->index - 1) / grp_inodes;
1271 /**@brief Compute 'goal' for allocation algorithm (For blockmap).
1272 * @param inode_ref Reference to inode, to allocate block for
1274 * @return error code
1276 int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
1280 struct ext4_sblock *sb = &inode_ref->fs->sb;
1283 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1284 uint32_t block_size = ext4_sb_get_block_size(sb);
1285 uint32_t iblock_cnt = (uint32_t)(inode_size / block_size);
1287 if (inode_size % block_size != 0)
1290 /* If inode has some blocks, get last block address + 1 */
1291 if (iblock_cnt > 0) {
1292 r = ext4_fs_get_inode_dblk_idx(inode_ref, iblock_cnt - 1,
1302 /* If goal == 0, sparse file -> continue */
1305 /* Identify block group of inode */
1307 uint32_t inodes_per_bg = ext4_get32(sb, inodes_per_group);
1308 uint32_t block_group = (inode_ref->index - 1) / inodes_per_bg;
1309 block_size = ext4_sb_get_block_size(sb);
1311 /* Load block group reference */
1312 struct ext4_block_group_ref bg_ref;
1313 r = ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
1317 struct ext4_bgroup *bg = bg_ref.block_group;
1319 /* Compute indexes */
1320 uint32_t bg_count = ext4_block_group_cnt(sb);
1321 ext4_fsblk_t itab_first_block = ext4_bg_get_inode_table_first_block(bg, sb);
1322 uint16_t itab_item_size = ext4_get16(sb, inode_size);
1323 uint32_t itab_bytes;
1325 /* Check for last block group */
1326 if (block_group < bg_count - 1) {
1327 itab_bytes = inodes_per_bg * itab_item_size;
1329 /* Last block group could be smaller */
1330 uint32_t inodes_cnt = ext4_get32(sb, inodes_count);
1332 itab_bytes = (inodes_cnt - ((bg_count - 1) * inodes_per_bg));
1333 itab_bytes *= itab_item_size;
1336 ext4_fsblk_t inode_table_blocks = itab_bytes / block_size;
1338 if (itab_bytes % block_size)
1339 inode_table_blocks++;
1341 *goal = itab_first_block + inode_table_blocks;
1343 return ext4_fs_put_block_group_ref(&bg_ref);
1346 static int ext4_fs_get_inode_dblk_idx_internal(struct ext4_inode_ref *inode_ref,
1347 ext4_lblk_t iblock, ext4_fsblk_t *fblock,
1349 bool support_unwritten __unused)
1351 struct ext4_fs *fs = inode_ref->fs;
1353 /* For empty file is situation simple */
1354 if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) {
1359 ext4_fsblk_t current_block;
1361 (void)extent_create;
1362 #if CONFIG_EXTENT_ENABLE
1363 /* Handle i-node using extents */
1364 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1365 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1367 ext4_fsblk_t current_fsblk;
1368 int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
1369 ¤t_fsblk, extent_create, NULL);
1373 current_block = current_fsblk;
1374 *fblock = current_block;
1376 ext4_assert(*fblock || support_unwritten);
1381 struct ext4_inode *inode = inode_ref->inode;
1383 /* Direct block are read directly from array in i-node structure */
1384 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1386 ext4_inode_get_direct_block(inode, (uint32_t)iblock);
1387 *fblock = current_block;
1391 /* Determine indirection level of the target block */
1394 for (i = 1; i < 4; i++) {
1395 if (iblock < fs->inode_block_limits[i]) {
1404 /* Compute offsets for the topmost level */
1405 uint32_t blk_off_in_lvl = (uint32_t)(iblock - fs->inode_block_limits[l - 1]);
1406 current_block = ext4_inode_get_indirect_block(inode, l - 1);
1407 uint32_t off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]);
1410 if (current_block == 0) {
1415 struct ext4_block block;
1418 * Navigate through other levels, until we find the block number
1419 * or find null reference meaning we are dealing with sparse file
1422 /* Load indirect block */
1423 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1427 /* Read block address from indirect block */
1429 to_le32(((uint32_t *)block.data)[off_in_blk]);
1431 /* Put back indirect block untouched */
1432 rc = ext4_block_set(fs->bdev, &block);
1436 /* Check for sparse file */
1437 if (current_block == 0) {
1442 /* Jump to the next level */
1445 /* Termination condition - we have address of data block loaded
1450 /* Visit the next level */
1451 blk_off_in_lvl %= fs->inode_blocks_per_level[l];
1452 off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]);
1455 *fblock = current_block;
1461 int ext4_fs_get_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
1462 ext4_lblk_t iblock, ext4_fsblk_t *fblock,
1463 bool support_unwritten)
1465 return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock,
1466 false, support_unwritten);
1469 int ext4_fs_init_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
1470 ext4_lblk_t iblock, ext4_fsblk_t *fblock)
1472 return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock,
1476 static int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref,
1477 ext4_lblk_t iblock, ext4_fsblk_t fblock)
1479 struct ext4_fs *fs = inode_ref->fs;
1481 #if CONFIG_EXTENT_ENABLE
1482 /* Handle inode using extents */
1483 if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) &&
1484 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1490 /* Handle simple case when we are dealing with direct reference */
1491 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1492 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock,
1494 inode_ref->dirty = true;
1499 /* Determine the indirection level needed to get the desired block */
1502 for (i = 1; i < 4; i++) {
1503 if (iblock < fs->inode_block_limits[i]) {
1512 uint32_t block_size = ext4_sb_get_block_size(&fs->sb);
1514 /* Compute offsets for the topmost level */
1515 uint32_t blk_off_in_lvl = (uint32_t)(iblock - fs->inode_block_limits[l - 1]);
1516 ext4_fsblk_t current_block =
1517 ext4_inode_get_indirect_block(inode_ref->inode, l - 1);
1518 uint32_t off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]);
1520 ext4_fsblk_t new_blk;
1522 struct ext4_block block;
1523 struct ext4_block new_block;
1525 /* Is needed to allocate indirect block on the i-node level */
1526 if (current_block == 0) {
1527 /* Allocate new indirect block */
1529 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1533 rc = ext4_balloc_alloc_block(inode_ref, goal, &new_blk);
1538 ext4_inode_set_indirect_block(inode_ref->inode, l - 1,
1540 inode_ref->dirty = true;
1542 /* Load newly allocated block */
1543 rc = ext4_trans_block_get_noread(fs->bdev, &new_block, new_blk);
1545 ext4_balloc_free_block(inode_ref, new_blk);
1549 /* Initialize new block */
1550 memset(new_block.data, 0, block_size);
1551 ext4_trans_set_block_dirty(new_block.buf);
1553 /* Put back the allocated block */
1554 rc = ext4_block_set(fs->bdev, &new_block);
1558 current_block = new_blk;
1562 * Navigate through other levels, until we find the block number
1563 * or find null reference meaning we are dealing with sparse file
1566 int rc = ext4_trans_block_get(fs->bdev, &block, current_block);
1570 current_block = to_le32(((uint32_t *)block.data)[off_in_blk]);
1571 if ((l > 1) && (current_block == 0)) {
1573 rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1575 ext4_block_set(fs->bdev, &block);
1579 /* Allocate new block */
1581 ext4_balloc_alloc_block(inode_ref, goal, &new_blk);
1583 ext4_block_set(fs->bdev, &block);
1587 /* Load newly allocated block */
1588 rc = ext4_trans_block_get_noread(fs->bdev, &new_block,
1592 ext4_block_set(fs->bdev, &block);
1596 /* Initialize allocated block */
1597 memset(new_block.data, 0, block_size);
1598 ext4_trans_set_block_dirty(new_block.buf);
1600 rc = ext4_block_set(fs->bdev, &new_block);
1602 ext4_block_set(fs->bdev, &block);
1606 /* Write block address to the parent */
1607 uint32_t * p = (uint32_t * )block.data;
1608 p[off_in_blk] = to_le32((uint32_t)new_blk);
1609 ext4_trans_set_block_dirty(block.buf);
1610 current_block = new_blk;
1613 /* Will be finished, write the fblock address */
1615 uint32_t * p = (uint32_t * )block.data;
1616 p[off_in_blk] = to_le32((uint32_t)fblock);
1617 ext4_trans_set_block_dirty(block.buf);
1620 rc = ext4_block_set(fs->bdev, &block);
1627 * If we are on the last level, break here as
1628 * there is no next level to visit
1633 /* Visit the next level */
1634 blk_off_in_lvl %= fs->inode_blocks_per_level[l];
1635 off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]);
1642 int ext4_fs_append_inode_dblk(struct ext4_inode_ref *inode_ref,
1643 ext4_fsblk_t *fblock, ext4_lblk_t *iblock)
1645 #if CONFIG_EXTENT_ENABLE
1646 /* Handle extents separately */
1647 if ((ext4_sb_feature_incom(&inode_ref->fs->sb, EXT4_FINCOM_EXTENTS)) &&
1648 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1650 ext4_fsblk_t current_fsblk;
1651 struct ext4_sblock *sb = &inode_ref->fs->sb;
1652 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1653 uint32_t block_size = ext4_sb_get_block_size(sb);
1654 *iblock = (uint32_t)((inode_size + block_size - 1) / block_size);
1656 rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
1657 ¤t_fsblk, true, NULL);
1661 *fblock = current_fsblk;
1662 ext4_assert(*fblock);
1664 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1665 inode_ref->dirty = true;
1671 struct ext4_sblock *sb = &inode_ref->fs->sb;
1673 /* Compute next block index and allocate data block */
1674 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1675 uint32_t block_size = ext4_sb_get_block_size(sb);
1677 /* Align size i-node size */
1678 if ((inode_size % block_size) != 0)
1679 inode_size += block_size - (inode_size % block_size);
1681 /* Logical blocks are numbered from 0 */
1682 uint32_t new_block_idx = (uint32_t)(inode_size / block_size);
1684 /* Allocate new physical block */
1685 ext4_fsblk_t goal, phys_block;
1686 int rc = ext4_fs_indirect_find_goal(inode_ref, &goal);
1690 rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block);
1694 /* Add physical block address to the i-node */
1695 rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx,
1698 ext4_balloc_free_block(inode_ref, phys_block);
1703 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1704 inode_ref->dirty = true;
1706 *fblock = phys_block;
1707 *iblock = new_block_idx;
1712 void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref)
1716 link = ext4_inode_get_links_cnt(inode_ref->inode);
1718 ext4_inode_set_links_cnt(inode_ref->inode, link);
1720 is_dx = ext4_sb_feature_com(&inode_ref->fs->sb, EXT4_FCOM_DIR_INDEX) &&
1721 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX);
1723 if (is_dx && link > 1) {
1724 if (link >= EXT4_LINK_MAX || link == 2) {
1725 ext4_inode_set_links_cnt(inode_ref->inode, 1);
1728 v = ext4_get32(&inode_ref->fs->sb, features_read_only);
1729 v |= EXT4_FRO_COM_DIR_NLINK;
1730 ext4_set32(&inode_ref->fs->sb, features_read_only, v);
1735 void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref)
1737 uint16_t links = ext4_inode_get_links_cnt(inode_ref->inode);
1738 if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode,
1739 EXT4_INODE_MODE_DIRECTORY)) {
1741 ext4_inode_set_links_cnt(inode_ref->inode, links - 1);
1746 ext4_inode_set_links_cnt(inode_ref->inode, links - 1);