swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <vfs.h> |
| 4 | |
| 5 | struct ext2_superblock |
| 6 | { |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 7 | /// Total number of inodes |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 8 | uint total_inodes; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 9 | |
| 10 | /// Total number of blocks |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 11 | uint total_blocks; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 12 | |
| 13 | /// Number of blocks reserved for superuser |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 14 | uint blocks_reserved_for_superuser; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 15 | |
| 16 | /// Number of unallocated blocks |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 17 | uint unallocated_blocks; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 18 | |
| 19 | /// Number of unallocated inodes |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 20 | uint unallocated_inodes; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 21 | |
| 22 | /// Block number containing the superblock |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 23 | uint superblock_block_number; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 24 | |
| 25 | /// the number to shift 1024 to the left by to get the block size |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 26 | uint block_size_shift; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 27 | |
| 28 | /// the number to shift 1024 to the left by to get the fragment size |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 29 | uint fragment_size_shift; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 30 | |
| 31 | /// Number of blocks in one block group |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 32 | uint blocks_per_block_group; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 33 | |
| 34 | /// Number of fragments in one block group |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 35 | uint fragments_per_block_group; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 36 | |
| 37 | /// Number of inodse in one block group |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 38 | uint inodes_per_block_group; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 39 | |
| 40 | /// UNIX time of last mount |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 41 | uint last_mount_time; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 42 | |
| 43 | /// UNIX time of last write |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 44 | uint last_write_time; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 45 | |
| 46 | /// Number of mounts since last consistency check |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 47 | ushort mounts_since_last_fsck; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 48 | |
| 49 | /// Number of mounts allowed between consistency checks |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 50 | ushort mounts_allowed_before_fsck; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 51 | |
| 52 | /// EXT2 signature, should be 0xef53 |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 53 | ushort signature; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 54 | |
| 55 | /// File system state, see enum ext2_fs_state |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 56 | ushort state; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 57 | |
| 58 | /// What to do in case of an error, see enum ext2_error_case |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 59 | ushort error_case; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 60 | |
| 61 | /// Minor portion of version |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 62 | ushort version_minor; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 63 | |
| 64 | /// UNIX time of last consistency check |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 65 | uint last_fsck_time; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 66 | |
| 67 | /// Max time between consistency checks |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 68 | uint fsck_time_interval; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 69 | |
| 70 | /// Operating system ID of creator |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 71 | uint creator_os_id; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 72 | |
| 73 | /// Major portion of version |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 74 | uint version_major; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 75 | |
| 76 | /// User ID that can use reserved blocks |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 77 | ushort reserved_blocks_uid; |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 78 | |
| 79 | /// Group ID that can use reserved blocks |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 80 | ushort reserved_blocks_gid; |
| 81 | }; |
| 82 | |
| 83 | enum ext2_fs_state |
| 84 | { |
| 85 | EXT2_STATE_CLEAN = 1, |
| 86 | EXT2_STATE_ERRORS = 2, |
| 87 | }; |
| 88 | |
| 89 | enum ext2_error_case |
| 90 | { |
| 91 | EXT2_ERROR_IGNORE = 1, |
| 92 | EXT2_ERROR_REMOUNT = 2, |
| 93 | EXT2_ERROR_KPANIC = 3, |
| 94 | }; |
| 95 | |
| 96 | enum ext2_os_id |
| 97 | { |
| 98 | EXT2_OS_LINUX = 0, |
| 99 | EXT2_OS_HURD, |
| 100 | EXT2_OS_MASIX, |
| 101 | EXT2_OS_FREEBSD, |
| 102 | EXT2_OS_OTHER_BSD, |
| 103 | }; |
| 104 | |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 105 | #define EXT2_SIGNATURE 0xef53 |
| 106 | |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 107 | struct ext2_block_group_descriptor |
| 108 | { |
| 109 | /// Address of block usage bitmap |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 110 | uint block_bitmap; // 4 |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 111 | |
| 112 | /// Address of inode usage bitmap |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 113 | uint inode_bitmap; // 8 |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 114 | |
| 115 | /// Starting block address of inode table |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 116 | uint inode_table_start_block; // 12 |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 117 | |
| 118 | /// Number of unallocated blocks in this group |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 119 | ushort unallocated_blocks; // 14 |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 120 | |
| 121 | /// Number of unallocated inodes in this group |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 122 | ushort unallocated_inodes; // 16 |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 123 | |
| 124 | /// Number of directories in this group |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 125 | ushort num_dirs; // 18 |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 126 | |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 127 | ushort padding; // 20 |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 128 | |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 129 | uchar reserved[12]; // 32 |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | struct ext2_inode |
| 133 | { |
| 134 | /// The format and permissions of the file, see EXT2_S_* |
| 135 | ushort mode; |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 136 | |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 137 | /// The user id of the owner |
| 138 | ushort uid; |
| 139 | |
| 140 | /// The lower 32 bits of the file size |
| 141 | uint size; |
| 142 | |
| 143 | /// Last time this was accessed in UNIX time |
| 144 | uint access_time; |
| 145 | |
| 146 | /// Time this was created in UNIX time |
| 147 | uint created_time; |
| 148 | |
| 149 | /// Time this was last modified in UNIX time |
| 150 | uint modified_time; |
| 151 | |
| 152 | /// Time this was deleted in UNIX time |
| 153 | uint deleted_time; |
| 154 | |
| 155 | /// Owner group id |
| 156 | ushort gid; |
| 157 | |
| 158 | /// Reference count for hard links |
| 159 | ushort links_count; |
| 160 | |
| 161 | /// Number of 512 byte blocks storing this files data, NOT EXT2 blocks! |
| 162 | uint num_blocks; |
| 163 | |
| 164 | /// Flags describing how this file can be accessed |
| 165 | uint flags; |
| 166 | |
| 167 | /// First field reserved for the operating system |
| 168 | uint os_reserved_1; |
| 169 | |
| 170 | union |
| 171 | { |
| 172 | /// The blocks of this file |
| 173 | uint blocks[15]; |
| 174 | |
| 175 | struct |
| 176 | { |
| 177 | /// The direct blocks |
| 178 | uint direct_blocks[12]; |
| 179 | |
| 180 | /// The single indirect block |
| 181 | uint ind_block; |
| 182 | |
| 183 | /// The double indirect block |
| 184 | uint ind2_block; |
| 185 | |
| 186 | /// The triple indirect block |
| 187 | uint ind3_block; |
| 188 | }; |
| 189 | }; |
| 190 | |
| 191 | /// Used by NFS to version files |
| 192 | uint generation; |
| 193 | |
| 194 | /// File attributes, unused |
| 195 | uint file_acl; |
| 196 | |
| 197 | /// Directory attributes, unused |
| 198 | uint dir_acl; |
| 199 | |
| 200 | /// Location of file fragment, unused |
| 201 | uint faddr; |
| 202 | |
| 203 | /// Second field reserved for the operating system |
| 204 | uchar os_reserved_2[12]; |
| 205 | }; |
| 206 | |
| 207 | enum |
| 208 | { |
| 209 | // File types |
| 210 | EXT2_S_IFSOCK = 0xc000, |
| 211 | EXT2_S_IFLINK = 0xa000, |
| 212 | EXT2_S_IFREG = 0x8000, |
| 213 | EXT2_S_IFBLK = 0x6000, |
| 214 | EXT2_S_IFDIR = 0x4000, |
| 215 | EXT2_S_IFCHR = 0x2000, |
| 216 | EXT2_S_IFIFO = 0x1000, |
| 217 | |
| 218 | /// Set UID |
| 219 | EXT2_S_ISUID = 0x0800, |
| 220 | /// Set GUID |
| 221 | EXT2_S_ISGID = 0x0400, |
| 222 | /// Sticky |
| 223 | EXT2_S_ISVTX = 0x0200, |
| 224 | |
| 225 | // Normal UNIX file permissions |
| 226 | EXT2_S_IRUSR = 0x0100, |
| 227 | EXT2_S_IWUSR = 0x0080, |
| 228 | EXT2_S_IXUSR = 0x0040, |
| 229 | EXT2_S_IRGRP = 0x0020, |
| 230 | EXT2_S_IWGRP = 0x0010, |
| 231 | EXT2_S_IXGRP = 0x0008, |
| 232 | EXT2_S_IROTH = 0x0004, |
| 233 | EXT2_S_IWOTH = 0x0002, |
| 234 | EXT2_S_IXOTH = 0x0001, |
| 235 | }; |
| 236 | |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 237 | /// File type flag (used on inode->mode) |
| 238 | #define EXT2_F_TYPE 0xf000 |
| 239 | |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 240 | struct ext2_dirent |
| 241 | { |
| 242 | uint inode; |
| 243 | ushort rec_len; |
| 244 | uchar name_len; |
| 245 | uchar file_type; |
| 246 | char name[]; |
| 247 | }; |
| 248 | |
| 249 | /// Read a file system block (0-indexed), if necessary multiple disk blocks |
| 250 | /// will be read automatically |
| 251 | void ext2_read_block(struct ext2_superblock *sb, void *buffer, uint block); |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 252 | void ext2_write_block(struct ext2_superblock *sb, void *buffer, uint block); |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 253 | |
swissChili | 4418ca5 | 2021-06-14 17:36:00 -0700 | [diff] [blame] | 254 | struct ext2_superblock ext2_read_superblock(); |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 255 | |
| 256 | void ext2_write_superblock(struct ext2_superblock *sb); |
| 257 | |
| 258 | // Just for fun, corrupt the superblock (obviously don't actually do this) |
| 259 | void ext2_corrupt_superblock_for_fun(); |
| 260 | |
swissChili | ef829f3 | 2021-06-13 20:00:54 -0700 | [diff] [blame] | 261 | void ext2_mount(struct fs_node *where); |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 262 | |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 263 | bool ext2_valid_filesystem(); |
swissChili | b7ef65d | 2021-07-17 12:51:52 -0700 | [diff] [blame] | 264 | |
| 265 | bool ext2_find_inode(struct ext2_superblock *sb, uint number, struct ext2_inode *inode); |
| 266 | |
| 267 | /// Load a block group descriptor for a certain block group |
| 268 | struct ext2_block_group_descriptor ext2_load_block_group_descriptor( |
| 269 | struct ext2_superblock *sb, uint block_group); |
| 270 | |
| 271 | /// List the contents of a directory dir. Calls `cb` for each item. If `dir` is |
| 272 | /// not a directory, returns false. Otherwise returns true. |
| 273 | bool ext2_dir_ls(struct ext2_superblock *sb, |
| 274 | struct ext2_inode *dir, |
| 275 | void (*cb)(uint inode, |
| 276 | const char *name, |
| 277 | void *data), |
| 278 | void *data); |
| 279 | |
| 280 | /// Read a data block for the inode, 0 being the first block of the file. |
| 281 | bool ext2_read_inode_block(struct ext2_superblock *sb, |
| 282 | struct ext2_inode *inode, |
| 283 | void *buffer, |
| 284 | uint block); |
swissChili | cbd4363 | 2021-07-17 16:19:44 -0700 | [diff] [blame] | 285 | |
swissChili | 4749d02 | 2021-07-19 12:33:06 -0700 | [diff] [blame] | 286 | ssize_t ext2_read_inode(struct ext2_superblock *sb, struct ext2_inode *inode, void *buffer, ssize_t size); |
| 287 | |
| 288 | /** |
| 289 | * @brief Set a block in a bitmap |
| 290 | * @param bitmap_block The first block of the bitmap, not necessarily the block |
| 291 | * that this specific bit is in. |
| 292 | * @param index The bit to check, starting at 0 |
| 293 | * @returns The value of the bit |
| 294 | */ |
| 295 | bool ext2_check_in_bitmap(struct ext2_superblock *sb, uint bitmap_block, |
| 296 | uint index); |
| 297 | |
| 298 | /** |
| 299 | * @brief Set a block in a bitmap |
| 300 | * @param bitmap_block The first block of the bitmap, not necessarily the block |
| 301 | * that this specific bit is in. |
| 302 | * @param index The bit to set, starting at 0 |
| 303 | * @param value The value of the bit, 0 for 0, non-0 for 1 |
| 304 | */ |
| 305 | void ext2_set_in_bitmap(struct ext2_superblock *sb, uint bitmap_block, |
| 306 | uint index, bool value); |
| 307 | |
| 308 | /** |
| 309 | * @brief Find the first zero bit in a bitset |
| 310 | * @param bitmap_block The first block of the bitmap |
| 311 | * @param num_blocks The number of blocks in the bitmap, or ~0 for no limit |
| 312 | * (UNSAFE). |
| 313 | * @param start_at The first bit to search from, useful if you want to ignore |
| 314 | * the first reserved inodes. |
| 315 | * @returns The index of the first zero bit, or -1 if there is none |
| 316 | */ |
| 317 | uint ext2_first_zero_bit(struct ext2_superblock *sb, uint bitmap_block, |
| 318 | uint num_blocks, uint start_at); |
| 319 | |
| 320 | uint ext2_first_free_inode(struct ext2_superblock *sb); |