1 /* 2 * Copyright (c) 2023 Antmicro <www.antmicro.com> 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef __EXT2_DISKOPS_H__ 8 #define __EXT2_DISKOPS_H__ 9 10 #include <stdint.h> 11 12 #include "ext2_struct.h" 13 14 /** @brief Fetch superblock into buffer in fs structure. 15 * 16 * @retval 0 on success 17 * @retval <0 error 18 */ 19 int ext2_fetch_superblock(struct ext2_data *fs); 20 21 /** 22 * @brief Fetch inode into given buffer. 23 * 24 * @param fs File system data 25 * @param ino Inode number 26 * @param inode Buffer for fetched inode 27 * 28 * @retval 0 on success 29 * @retval <0 error 30 */ 31 int ext2_fetch_inode(struct ext2_data *fs, uint32_t ino, struct ext2_inode *inode); 32 33 /** 34 * @brief Fetch block into buffer in the inode structure. 35 * 36 * @param inode Inode structure 37 * @param block Number of inode block to fetch (0 - first block in that inode) 38 * 39 * @retval 0 on success 40 * @retval <0 error 41 */ 42 int ext2_fetch_inode_block(struct ext2_inode *inode, uint32_t block); 43 44 /** 45 * @brief Fetch block group into buffer in fs structure. 46 * 47 * If the group was already fetched then this function has no effect. 48 * 49 * @param fs File system data 50 * @param group Block group number 51 * 52 * @retval 0 on success 53 * @retval <0 error 54 */ 55 int ext2_fetch_block_group(struct ext2_data *fs, uint32_t group); 56 57 /* 58 * @brief Fetch one block of inode table into internal buffer 59 * 60 * If the block of inode table was already fetched then this function does nothing and returns 61 * with success. 62 * 63 * @param bg Block group structure 64 * @param block Number of inode table block to fetch (relative to start of the inode table) 65 * 66 * @retval 0 on success 67 * @retval <0 error 68 */ 69 int ext2_fetch_bg_itable(struct ext2_bgroup *bg, uint32_t block); 70 71 /** 72 * @brief Fetch inode bitmap into internal buffer 73 * 74 * If the bitmap was already fetched then this function has no effect. 75 * 76 * @param bg Block group structure 77 * 78 * @retval 0 on success 79 * @retval <0 error 80 */ 81 int ext2_fetch_bg_ibitmap(struct ext2_bgroup *bg); 82 83 /** 84 * @brief Fetch block bitmap into internal buffer 85 * 86 * If the bitmap was already fetched then this function has no effect. 87 * 88 * @param bg Block group structure 89 * 90 * @retval 0 on success 91 * @retval <0 error 92 */ 93 int ext2_fetch_bg_bbitmap(struct ext2_bgroup *bg); 94 95 /** 96 * @brief Clear the entry in the inode table of given inode 97 * 98 * This function triggers fetching of block group and inode table (where the 99 * inode is described). 100 * 101 * @param fs File system data 102 * @param inode Inode number of inode to clear 103 * 104 * @retval 0 on success 105 * @retval <0 error 106 */ 107 int ext2_clear_inode(struct ext2_data *fs, uint32_t ino); 108 109 /** 110 * @brief Commit changes made to inode structure 111 * 112 * The changes are committed only to the cached block. They are truly written to 113 * storage when sync is performed. 114 * 115 * @param inode Inode structure 116 * 117 * @retval 0 on success 118 * @retval <0 error 119 */ 120 int ext2_commit_inode(struct ext2_inode *inode); 121 122 /** 123 * @brief Commit changes made to inode block 124 * 125 * The changes are committed only to the cached block. They are truly written to 126 * storage when sync is performed. 127 * 128 * @param inode Inode structure 129 * 130 * @retval 0 on success 131 * @retval <0 error 132 */ 133 int ext2_commit_inode_block(struct ext2_inode *inode); 134 135 /** 136 * @brief Commit changes made to superblock structure. 137 * 138 * The changes made to program structure are copied to disk representation and written to the 139 * backing storage. 140 * 141 * @param fs File system data struct 142 * 143 * @retval 0 on success 144 * @retval <0 error 145 */ 146 int ext2_commit_superblock(struct ext2_data *fs); 147 148 /** 149 * @brief Commit changes made to block group structure. 150 * 151 * The changes made to program structure are copied to disk representation and written to the 152 * backing storage. 153 * 154 * @param fs File system data struct 155 * 156 * @retval 0 on success 157 * @retval <0 error 158 */ 159 int ext2_commit_bg(struct ext2_data *fs); 160 161 /* Operations that reserve or free the block or inode in the file system. They 162 * mark an inode or block as used in the bitmap and change free inode/block 163 * count in superblock and block group. 164 */ 165 166 /** 167 * @brief Reserve a block for future use. 168 * 169 * Search for a free block. If block is found, proper fields in superblock and 170 * block group are updated and block is marked as used in block bitmap. 171 * 172 * @param fs File system data 173 * 174 * @retval >0 number of allocated block 175 * @retval <0 error 176 */ 177 int64_t ext2_alloc_block(struct ext2_data *fs); 178 179 /** 180 * @brief Reserve an inode for future use. 181 * 182 * Search for free inode. If inode is found, proper fields in superblock and 183 * block group are updated and inode is marked as used in inode bitmap. 184 * 185 * @param fs File system data 186 * 187 * @retval >0 number of allocated inode 188 * @retval <0 error 189 */ 190 int32_t ext2_alloc_inode(struct ext2_data *fs); 191 192 /** 193 * @brief Free the block 194 * 195 * @param fs File system data 196 * 197 * @retval 0 on success 198 * @retval <0 error 199 */ 200 int ext2_free_block(struct ext2_data *fs, uint32_t block); 201 202 /** 203 * @brief Free the inode 204 * 205 * @param fs File system data 206 * @param inode Inode number 207 * @param directory True if freed inode was a directory 208 * 209 * @retval 0 on success 210 * @retval <0 error 211 */ 212 int ext2_free_inode(struct ext2_data *fs, uint32_t ino, bool directory); 213 214 /** 215 * @brief Allocate directory entry filled with data from disk directory entry. 216 * 217 * NOTE: This function never fails. 218 * 219 * Returns structure allocated on direntry_heap. 220 */ 221 struct ext2_direntry *ext2_fetch_direntry(struct ext2_disk_direntry *disk_de); 222 223 /** 224 * @brief Write the data from program directory entry to disk structure. 225 */ 226 void ext2_write_direntry(struct ext2_disk_direntry *disk_de, struct ext2_direntry *de); 227 228 uint32_t ext2_get_disk_direntry_inode(struct ext2_disk_direntry *de); 229 uint32_t ext2_get_disk_direntry_reclen(struct ext2_disk_direntry *de); 230 uint8_t ext2_get_disk_direntry_namelen(struct ext2_disk_direntry *de); 231 uint8_t ext2_get_disk_direntry_type(struct ext2_disk_direntry *de); 232 233 void ext2_set_disk_direntry_inode(struct ext2_disk_direntry *de, uint32_t inode); 234 void ext2_set_disk_direntry_reclen(struct ext2_disk_direntry *de, uint16_t reclen); 235 void ext2_set_disk_direntry_namelen(struct ext2_disk_direntry *de, uint8_t namelen); 236 void ext2_set_disk_direntry_type(struct ext2_disk_direntry *de, uint8_t type); 237 void ext2_set_disk_direntry_name(struct ext2_disk_direntry *de, const char *name, size_t len); 238 239 #endif /* __EXT2_DISKOPS_H__ */ 240