| 1 | #include <fs/ext2.h> |
| 2 | #include <mem/alloc.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | // TODO: flags - maybe? |
| 8 | |
| 9 | uint64_t PTRS_PER_BLOCK; |
| 10 | |
| 11 | bool ext2_read_superblock(struct partition *p, struct ext2_sblock *sblock) { |
| 12 | struct block_device *d = p->disk; |
| 13 | uint8_t *buffer = kmalloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 14 | if (!buffer) |
| 15 | return false; |
| 16 | |
| 17 | memset(buffer, 0, PAGE_SIZE); |
| 18 | uint32_t superblock_lba = (EXT2_SUPERBLOCK_OFFSET / d->sector_size); |
| 19 | uint32_t superblock_offset = EXT2_SUPERBLOCK_OFFSET % d->sector_size; |
| 20 | |
| 21 | if (!d->read_sector(d, superblock_lba + p->start_lba, buffer, 1)) { |
| 22 | kfree_aligned(buffer); |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | memcpy(sblock, buffer + superblock_offset, sizeof(struct ext2_sblock)); |
| 27 | |
| 28 | kfree_aligned(buffer); |
| 29 | return (sblock->magic == 0xEF53); |
| 30 | } |
| 31 | |
| 32 | bool ext2_write_superblock(struct ext2_fs *fs) { |
| 33 | return ext2_block_write(fs, ent: fs->sbcache_ent, EXT2_PRIO_SBLOCK); |
| 34 | } |
| 35 | |
| 36 | bool ext2_write_group_desc(struct ext2_fs *fs) { |
| 37 | return ext2_block_write(fs, ent: fs->gdesc_cache_ent, EXT2_PRIO_SBLOCK); |
| 38 | } |
| 39 | |
| 40 | struct vfs_node *ext2_g_mount(struct partition *p) { |
| 41 | if (!p) |
| 42 | return NULL; |
| 43 | |
| 44 | p->fs_data = kmalloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 45 | struct ext2_fs *fs = p->fs_data; |
| 46 | fs->sblock = kmalloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 47 | |
| 48 | memset(fs->sblock, 0, PAGE_SIZE); |
| 49 | |
| 50 | struct vfs_node *n = kmalloc(sizeof(struct vfs_node), ALLOC_FLAGS_ZERO); |
| 51 | |
| 52 | if (!p->fs_data || !fs->sblock | !n) |
| 53 | return NULL; |
| 54 | |
| 55 | if (!ext2_read_superblock(p, sblock: fs->sblock)) |
| 56 | return NULL; |
| 57 | |
| 58 | ext2_mount(p, fs, sblock: fs->sblock, out_node: n); |
| 59 | return n; |
| 60 | } |
| 61 | |
| 62 | void ext2_g_print(struct partition *p) { |
| 63 | if (!p) |
| 64 | return; |
| 65 | } |
| 66 | |