| 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 | ERR_FACILITY_DEFINE(ext2, .desc = "Ext2 Filesystem" ); |
| 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 = valloc_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 | vfree_aligned(buffer); |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | memcpy(sblock, buffer + superblock_offset, sizeof(struct ext2_sblock)); |
| 27 | |
| 28 | vfree_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 | struct ext2_fs *fs = valloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 45 | struct ext2_sblock *sblock = valloc_aligned(PAGE_SIZE, PAGE_SIZE); |
| 46 | struct vfs_node *n = vnew(n, ALLOC_ZERO); |
| 47 | if (!fs || !sblock || !n) |
| 48 | goto err; |
| 49 | |
| 50 | memset(sblock, 0, PAGE_SIZE); |
| 51 | p->fs_data = fs; |
| 52 | fs->sblock = sblock; |
| 53 | |
| 54 | if (!ext2_read_superblock(p, sblock)) |
| 55 | goto err; |
| 56 | |
| 57 | ext2_mount(p, fs, sblock, out_node: n); |
| 58 | return n; |
| 59 | |
| 60 | err: |
| 61 | p->fs_data = NULL; |
| 62 | vfree(n); |
| 63 | vfree_aligned(sblock); |
| 64 | vfree_aligned(fs); |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | void ext2_g_print(struct partition *p) { |
| 69 | if (!p) |
| 70 | return; |
| 71 | } |
| 72 | |