Lines Matching full:fs

9 #include <zephyr/fs/fs.h>
25 static int get_level_offsets(struct ext2_data *fs, uint32_t block, uint32_t offsets[4]);
26 static inline uint32_t get_ngroups(struct ext2_data *fs);
201 int ext2_fetch_superblock(struct ext2_data *fs) in ext2_fetch_superblock() argument
206 if (fs->block_size == 1024) { in ext2_fetch_superblock()
208 b = ext2_get_block(fs, 1); in ext2_fetch_superblock()
211 b = ext2_get_block(fs, 0); in ext2_fetch_superblock()
220 fill_sblock(&fs->sblock, disk_sb); in ext2_fetch_superblock()
226 static inline uint32_t get_ngroups(struct ext2_data *fs) in get_ngroups() argument
229 fs->sblock.s_blocks_count / fs->sblock.s_blocks_per_group; in get_ngroups()
231 if (fs->sblock.s_blocks_count % fs->sblock.s_blocks_per_group != 0) { in get_ngroups()
238 int ext2_fetch_block_group(struct ext2_data *fs, uint32_t group) in ext2_fetch_block_group() argument
240 struct ext2_bgroup *bg = &fs->bgroup; in ext2_fetch_block_group()
247 uint32_t ngroups = get_ngroups(fs); in ext2_fetch_block_group()
256 uint32_t groups_per_block = fs->block_size / sizeof(struct ext2_disk_bgroup); in ext2_fetch_block_group()
259 uint32_t global_block = fs->sblock.s_first_data_block + 1 + block; in ext2_fetch_block_group()
261 struct ext2_block *b = ext2_get_block(fs, global_block); in ext2_fetch_block_group()
280 bg->fs = fs; in ext2_fetch_block_group()
299 struct ext2_data *fs = bg->fs; in ext2_fetch_bg_itable() local
303 bg->inode_table = ext2_get_block(fs, global_block); in ext2_fetch_bg_itable()
318 struct ext2_data *fs = bg->fs; in ext2_fetch_bg_ibitmap() local
321 bg->inode_bitmap = ext2_get_block(fs, global_block); in ext2_fetch_bg_ibitmap()
334 struct ext2_data *fs = bg->fs; in ext2_fetch_bg_bbitmap() local
337 bg->block_bitmap = ext2_get_block(fs, global_block); in ext2_fetch_bg_bbitmap()
349 static int32_t get_itable_entry(struct ext2_data *fs, uint32_t ino) in get_itable_entry() argument
352 uint32_t ino_group = (ino - 1) / fs->sblock.s_inodes_per_group; in get_itable_entry()
353 uint32_t ino_index = (ino - 1) % fs->sblock.s_inodes_per_group; in get_itable_entry()
357 rc = ext2_fetch_block_group(fs, ino_group); in get_itable_entry()
362 uint32_t inode_size = fs->sblock.s_inode_size; in get_itable_entry()
363 uint32_t inodes_per_block = fs->block_size / inode_size; in get_itable_entry()
370 rc = ext2_fetch_bg_itable(&fs->bgroup, block_index); in get_itable_entry()
377 int ext2_fetch_inode(struct ext2_data *fs, uint32_t ino, struct ext2_inode *inode) in ext2_fetch_inode() argument
380 int32_t itable_offset = get_itable_entry(fs, ino); in ext2_fetch_inode()
388 struct ext2_disk_inode *dino = &BGROUP_INODE_TABLE(&fs->bgroup)[itable_offset]; in ext2_fetch_inode()
393 inode->i_fs = fs; in ext2_fetch_inode()
456 struct ext2_data *fs = inode->i_fs; in ext2_fetch_inode_block() local
461 max_lvl = get_level_offsets(fs, block, offsets); in ext2_fetch_inode_block()
497 static int64_t delete_blocks(struct ext2_data *fs, uint32_t block_num, int lvl, in delete_blocks() argument
519 list_block = ext2_get_block(fs, block_num); in delete_blocks()
549 fs->flags |= EXT2_DATA_FLAGS_ERR; in delete_blocks()
558 rem = delete_blocks(fs, block_num2, lvl - 1, &offsets[1]); in delete_blocks()
567 for (uint32_t i = start_blk; i < fs->block_size / EXT2_BLOCK_NUM_SIZE; ++i) { in delete_blocks()
573 rem = delete_blocks(fs, block_num2, lvl - 1, zero_offsets); in delete_blocks()
592 ret = ext2_free_block(fs, block_num); in delete_blocks()
599 ret = ext2_write_block(fs, list_block); in delete_blocks()
610 static int get_level_offsets(struct ext2_data *fs, uint32_t block, uint32_t offsets[4]) in get_level_offsets() argument
612 const uint32_t B = fs->block_size / EXT2_BLOCK_NUM_SIZE; in get_level_offsets()
668 struct ext2_data *fs = inode->i_fs; in ext2_inode_remove_blocks() local
685 fs->flags |= EXT2_DATA_FLAGS_ERR; in ext2_inode_remove_blocks()
717 struct ext2_data *fs = inode->i_fs; in alloc_level_blocks() local
728 ret = ext2_assign_block_num(fs, inode->blocks[lvl]); in alloc_level_blocks()
736 ret = ext2_write_block(fs, inode->blocks[lvl-1]); in alloc_level_blocks()
753 inode->i_blocks += fs->block_size / 512; in alloc_level_blocks()
759 int ext2_commit_superblock(struct ext2_data *fs) in ext2_commit_superblock() argument
765 if (fs->block_size == 1024) { in ext2_commit_superblock()
767 b = ext2_get_block(fs, 1); in ext2_commit_superblock()
770 b = ext2_get_block(fs, 0); in ext2_commit_superblock()
779 fill_disk_sblock(disk_sb, &fs->sblock); in ext2_commit_superblock()
781 ret = ext2_write_block(fs, b); in ext2_commit_superblock()
789 int ext2_commit_bg(struct ext2_data *fs) in ext2_commit_bg() argument
792 struct ext2_bgroup *bg = &fs->bgroup; in ext2_commit_bg()
794 uint32_t groups_per_block = fs->block_size / sizeof(struct ext2_disk_bgroup); in ext2_commit_bg()
797 uint32_t global_block = fs->sblock.s_first_data_block + 1 + block; in ext2_commit_bg()
799 struct ext2_block *b = ext2_get_block(fs, global_block); in ext2_commit_bg()
809 ret = ext2_write_block(fs, b); in ext2_commit_bg()
819 struct ext2_data *fs = inode->i_fs; in ext2_commit_inode() local
821 int32_t itable_offset = get_itable_entry(fs, inode->i_id); in ext2_commit_inode()
828 struct ext2_disk_inode *dino = &BGROUP_INODE_TABLE(&fs->bgroup)[itable_offset]; in ext2_commit_inode()
833 return ext2_write_block(fs, fs->bgroup.inode_table); in ext2_commit_inode()
854 int ext2_clear_inode(struct ext2_data *fs, uint32_t ino) in ext2_clear_inode() argument
857 int32_t itable_offset = get_itable_entry(fs, ino); in ext2_clear_inode()
863 memset(&BGROUP_INODE_TABLE(&fs->bgroup)[itable_offset], 0, sizeof(struct ext2_disk_inode)); in ext2_clear_inode()
864 ret = ext2_write_block(fs, fs->bgroup.inode_table); in ext2_clear_inode()
868 int64_t ext2_alloc_block(struct ext2_data *fs) in ext2_alloc_block() argument
874 rc = ext2_fetch_block_group(fs, group); in ext2_alloc_block()
879 LOG_DBG("Free blocks: %d", fs->bgroup.bg_free_blocks_count); in ext2_alloc_block()
880 while ((rc >= 0) && (fs->bgroup.bg_free_blocks_count == 0)) { in ext2_alloc_block()
882 rc = ext2_fetch_block_group(fs, group); in ext2_alloc_block()
892 rc = ext2_fetch_bg_bbitmap(&fs->bgroup); in ext2_alloc_block()
897 bitmap_slot = ext2_bitmap_find_free(BGROUP_BLOCK_BITMAP(&fs->bgroup), fs->block_size); in ext2_alloc_block()
904 total = group * fs->sblock.s_blocks_per_group + bitmap_slot + fs->sblock.s_first_data_block; in ext2_alloc_block()
908 rc = ext2_bitmap_set(BGROUP_BLOCK_BITMAP(&fs->bgroup), bitmap_slot, fs->block_size); in ext2_alloc_block()
913 fs->bgroup.bg_free_blocks_count -= 1; in ext2_alloc_block()
914 fs->sblock.s_free_blocks_count -= 1; in ext2_alloc_block()
916 set = ext2_bitmap_count_set(BGROUP_BLOCK_BITMAP(&fs->bgroup), fs->sblock.s_blocks_count); in ext2_alloc_block()
918 if (set != (fs->sblock.s_blocks_count - fs->sblock.s_free_blocks_count)) { in ext2_alloc_block()
919 error_behavior(fs, "Wrong number of used blocks in superblock and bitmap"); in ext2_alloc_block()
923 rc = ext2_commit_superblock(fs); in ext2_alloc_block()
928 rc = ext2_commit_bg(fs); in ext2_alloc_block()
933 rc = ext2_write_block(fs, fs->bgroup.block_bitmap); in ext2_alloc_block()
941 static int check_zero_inode(struct ext2_data *fs, uint32_t ino) in check_zero_inode() argument
943 int32_t itable_offset = get_itable_entry(fs, ino); in check_zero_inode()
949 uint8_t *bytes = (uint8_t *)&BGROUP_INODE_TABLE(&fs->bgroup)[itable_offset]; in check_zero_inode()
959 int32_t ext2_alloc_inode(struct ext2_data *fs) in ext2_alloc_inode() argument
965 rc = ext2_fetch_block_group(fs, group); in ext2_alloc_inode()
967 while (fs->bgroup.bg_free_inodes_count == 0 && rc >= 0) { in ext2_alloc_inode()
969 rc = ext2_fetch_block_group(fs, group); in ext2_alloc_inode()
980 LOG_DBG("Free inodes (bg): %d", fs->bgroup.bg_free_inodes_count); in ext2_alloc_inode()
981 LOG_DBG("Free inodes (sb): %d", fs->sblock.s_free_inodes_count); in ext2_alloc_inode()
983 rc = ext2_fetch_bg_ibitmap(&fs->bgroup); in ext2_alloc_inode()
988 r = ext2_bitmap_find_free(BGROUP_INODE_BITMAP(&fs->bgroup), fs->block_size); in ext2_alloc_inode()
995 global_idx = group * fs->sblock.s_inodes_per_group + r + 1; in ext2_alloc_inode()
998 if (check_zero_inode(fs, global_idx) != 0) { in ext2_alloc_inode()
999 error_behavior(fs, "Inode is not cleared in inode table!"); in ext2_alloc_inode()
1005 rc = ext2_bitmap_set(BGROUP_INODE_BITMAP(&fs->bgroup), r, fs->block_size); in ext2_alloc_inode()
1010 fs->bgroup.bg_free_inodes_count -= 1; in ext2_alloc_inode()
1011 fs->sblock.s_free_inodes_count -= 1; in ext2_alloc_inode()
1013 set = ext2_bitmap_count_set(BGROUP_INODE_BITMAP(&fs->bgroup), fs->sblock.s_inodes_count); in ext2_alloc_inode()
1015 if (set != fs->sblock.s_inodes_count - fs->sblock.s_free_inodes_count) { in ext2_alloc_inode()
1016 error_behavior(fs, "Wrong number of used inodes in superblock and bitmap"); in ext2_alloc_inode()
1020 rc = ext2_commit_superblock(fs); in ext2_alloc_inode()
1025 rc = ext2_commit_bg(fs); in ext2_alloc_inode()
1030 rc = ext2_write_block(fs, fs->bgroup.inode_bitmap); in ext2_alloc_inode()
1036 LOG_DBG("Free inodes (bg): %d", fs->bgroup.bg_free_inodes_count); in ext2_alloc_inode()
1037 LOG_DBG("Free inodes (sb): %d", fs->sblock.s_free_inodes_count); in ext2_alloc_inode()
1042 int ext2_free_block(struct ext2_data *fs, uint32_t block) in ext2_free_block() argument
1047 block -= fs->sblock.s_first_data_block; in ext2_free_block()
1050 uint32_t group = block / fs->sblock.s_blocks_per_group; in ext2_free_block()
1051 uint32_t off = block % fs->sblock.s_blocks_per_group; in ext2_free_block()
1054 rc = ext2_fetch_block_group(fs, group); in ext2_free_block()
1059 rc = ext2_fetch_bg_bbitmap(&fs->bgroup); in ext2_free_block()
1064 rc = ext2_bitmap_unset(BGROUP_BLOCK_BITMAP(&fs->bgroup), off, fs->block_size); in ext2_free_block()
1069 fs->bgroup.bg_free_blocks_count += 1; in ext2_free_block()
1070 fs->sblock.s_free_blocks_count += 1; in ext2_free_block()
1072 set = ext2_bitmap_count_set(BGROUP_BLOCK_BITMAP(&fs->bgroup), fs->sblock.s_blocks_count); in ext2_free_block()
1074 if (set != fs->sblock.s_blocks_count - fs->sblock.s_free_blocks_count) { in ext2_free_block()
1075 error_behavior(fs, "Wrong number of used blocks in superblock and bitmap"); in ext2_free_block()
1079 rc = ext2_commit_superblock(fs); in ext2_free_block()
1084 rc = ext2_commit_bg(fs); in ext2_free_block()
1089 rc = ext2_write_block(fs, fs->bgroup.block_bitmap); in ext2_free_block()
1097 int ext2_free_inode(struct ext2_data *fs, uint32_t ino, bool directory) in ext2_free_inode() argument
1102 uint32_t group = (ino - 1) / fs->sblock.s_inodes_per_group; in ext2_free_inode()
1103 uint32_t bitmap_off = (ino - 1) % fs->sblock.s_inodes_per_group; in ext2_free_inode()
1106 rc = ext2_fetch_block_group(fs, group); in ext2_free_inode()
1111 rc = ext2_fetch_bg_ibitmap(&fs->bgroup); in ext2_free_inode()
1116 rc = ext2_bitmap_unset(BGROUP_INODE_BITMAP(&fs->bgroup), bitmap_off, fs->block_size); in ext2_free_inode()
1121 rc = ext2_clear_inode(fs, ino); in ext2_free_inode()
1126 fs->bgroup.bg_free_inodes_count += 1; in ext2_free_inode()
1127 fs->sblock.s_free_inodes_count += 1; in ext2_free_inode()
1130 fs->bgroup.bg_used_dirs_count -= 1; in ext2_free_inode()
1133 set = ext2_bitmap_count_set(BGROUP_INODE_BITMAP(&fs->bgroup), fs->sblock.s_inodes_count); in ext2_free_inode()
1135 if (set != fs->sblock.s_inodes_count - fs->sblock.s_free_inodes_count) { in ext2_free_inode()
1136 error_behavior(fs, "Wrong number of used inodes in superblock and bitmap"); in ext2_free_inode()
1142 rc = ext2_commit_superblock(fs); in ext2_free_inode()
1147 rc = ext2_commit_bg(fs); in ext2_free_inode()
1152 rc = ext2_write_block(fs, fs->bgroup.inode_bitmap); in ext2_free_inode()
1157 rc = fs->backend_ops->sync(fs); in ext2_free_inode()