From f494b6265d7aa777c86fc77d314dd2f560c23c74 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 30 Apr 2025 11:20:04 -0400 Subject: update fs headers 2.0 --- kernel/drivers/ata.c | 8 +-- kernel/fs/fs.c | 60 ++++++++++++++------- kernel/include/comus/fs.h | 131 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 150 insertions(+), 49 deletions(-) (limited to 'kernel') diff --git a/kernel/drivers/ata.c b/kernel/drivers/ata.c index 6c8de86..6f53d04 100644 --- a/kernel/drivers/ata.c +++ b/kernel/drivers/ata.c @@ -646,7 +646,8 @@ enum ide_error ide_device_read_sectors(ide_device_t dev_identifier, } else if (dev->type == IDE_ATAPI) { // for (i = 0; i < numsects; i++) // err = ide_atapi_read(drive, lba + i, 1, es, edi + (i*2048)); - panic("atapi unimplemented- todo"); + //panic("atapi unimplemented- todo"); + return 1; } if (err) { @@ -676,8 +677,9 @@ enum ide_error ide_device_write_sectors(ide_device_t device_identifier, if (dev->type == IDE_ATA) { err = ide_device_ata_access(dev, ATA_WRITE, lba, numsects, buf); } else if (dev->type == IDE_ATAPI) { - panic("atapi unimplemented- todo"); - err = IDE_ERROR_POLL_WRITE_PROTECTED; + //panic("atapi unimplemented- todo"); + //err = IDE_ERROR_POLL_WRITE_PROTECTED; + return 1; } if (err) diff --git a/kernel/fs/fs.c b/kernel/fs/fs.c index ea47e38..4c7ea29 100644 --- a/kernel/fs/fs.c +++ b/kernel/fs/fs.c @@ -1,3 +1,4 @@ +#include "comus/memory.h" #include #include #include @@ -6,23 +7,19 @@ struct disk fs_disks[N_DISKS]; struct file_system fs_loaded_file_systems[N_DISKS]; -void fs_init(void) +static void load_disks(void) { size_t idx = 0; - // zero structures - memsetv(fs_disks, 0, sizeof(fs_disks)); - memsetv(fs_loaded_file_systems, 0, sizeof(fs_loaded_file_systems)); - // check for initrd size_t rd_len; void *rd = mboot_get_initrd(&rd_len); if (rd != NULL) { assert(idx < N_DISKS, "Too many disks, limit is: %d\n", N_DISKS); fs_disks[idx] = (struct disk){ - .present = 1, - .id = idx, - .type = DISK_TYPE_RAMDISK, + .d_present = 1, + .d_id = idx, + .d_type = DISK_TYPE_RAMDISK, .rd.start = rd, .rd.len = rd_len, }; @@ -34,17 +31,44 @@ void fs_init(void) for (size_t i = 0; i < ide_list.num_devices; i++) { assert(idx < N_DISKS, "Too many disks, limit is: %d\n", N_DISKS); fs_disks[idx] = (struct disk){ - .present = 1, - .id = idx, - .type = DISK_TYPE_ATA, + .d_present = 1, + .d_id = idx, + .d_type = DISK_TYPE_ATA, .ide = ide_list.devices[i], }; idx++; } - INFO("loaded %zu disks\n", idx); + INFO("loaded %zu disks", idx); + +} + +static void load_fs(struct disk *disk) +{ + struct file_system *fs = &fs_loaded_file_systems[disk->d_id]; + fs->fs_disk = disk; + fs->fs_id = disk->d_id; + + (void)fs; + // TODO: load fs on disk + + WARN("failed to load fs on disk %u", disk->d_id); +} + +void fs_init(void) +{ + + // zero structures + memsetv(fs_disks, 0, sizeof(fs_disks)); + memsetv(fs_loaded_file_systems, 0, sizeof(fs_loaded_file_systems)); + + // load disks + load_disks(); - // TODO: load filesystems on disks + // load fs's on disks + for (size_t i = 0; i < N_DISKS; i++) + if (fs_disks[i].d_present) + load_fs(&fs_disks[i]); } struct disk *fs_get_root_disk(void) @@ -54,7 +78,7 @@ struct disk *fs_get_root_disk(void) for (int i = 0; i < N_DISKS; i++) { struct disk *disk = &fs_disks[i]; - if (disk->present) + if (disk->d_present) return disk; } @@ -136,7 +160,7 @@ int disk_read(struct disk *disk, size_t offset, size_t len, void *buffer) { int ret = 0; - switch (disk->type) { + switch (disk->d_type) { case DISK_TYPE_RAMDISK: ret = disk_read_rd(disk, offset, len, buffer); break; @@ -145,7 +169,7 @@ int disk_read(struct disk *disk, size_t offset, size_t len, void *buffer) break; default: ERROR("attempted to read from disk with invalid type: %d\n", - disk->type); + disk->d_type); ret = -E_BAD_PARAM; } @@ -200,7 +224,7 @@ int disk_write(struct disk *disk, size_t offset, size_t len, void *buffer) { int ret = 0; - switch (disk->type) { + switch (disk->d_type) { case DISK_TYPE_RAMDISK: ret = disk_write_rd(disk, offset, len, buffer); break; @@ -208,7 +232,7 @@ int disk_write(struct disk *disk, size_t offset, size_t len, void *buffer) ret = disk_write_ata(disk, offset, len, buffer); break; default: - ERROR("attempted to write to disk with invalid type: %d\n", disk->type); + ERROR("attempted to write to disk with invalid type: %d\n", disk->d_type); ret = -E_BAD_PARAM; } diff --git a/kernel/include/comus/fs.h b/kernel/include/comus/fs.h index c48e3e1..77d065e 100644 --- a/kernel/include/comus/fs.h +++ b/kernel/include/comus/fs.h @@ -20,13 +20,11 @@ enum disk_type { struct disk { /// set to 1 in array to state that fs is defined - /// system use only - int present; - /// index into disks array - /// system use only - int id; + int d_present; + /// disk id + int d_id; /// disk type - enum disk_type type; + enum disk_type d_type; /// internal disk device union { struct { @@ -59,60 +57,137 @@ int disk_read(struct disk *disk, size_t offset, size_t len, void *buffer); */ int disk_write(struct disk *disk, size_t offset, size_t len, void *buffer); +/// file type enum file_type { - // regular file + /// regular file F_REG = 0, - // directory + /// directory F_DIR = 1, }; +/// directory entry struct dirent { - int d_id; + /// file offset into directory unsigned long d_offset; + /// file name len unsigned short d_namelen; + /// file name char d_name[N_FILE_NAME]; }; +/// file statistics struct stat { - /// file id - int s_id; /// file type enum file_type s_type; /// file length unsigned long s_length; }; +/// generic file pointer type. file system open function +/// must extend this struct, allocate it on the heap, +/// and return it as a generic file pointer. +/// +/// # Functions +/// +/// read - read bytes from a opened file +/// write - write bytes to a opened file +/// seek - seek the open file +/// close - close an opened file (free pointer and other structures) +/// +/// # Example FS Open +/// +/// ``` +/// struct example_file { +/// struct file file; +/// size_t offset; // custom +/// size_t sector; // data +/// }; +/// +/// int example_open(const char *fullpath, struct file **out) +/// { +/// struct example_file *file; +/// +/// // do some checks here to get file info at full path +/// +/// file = kalloc(sizeof(struct example_file)); +/// file->read = example_read; +/// file->write = example_write; +/// file->seek = example_seek; +/// file->ents = example_ents; +/// file->close = example_close; // frees pointer from kalloc +/// *out = (struct file *) *file; +/// return 0; // success +/// } +/// ``` +/// struct file { - /// file id - int f_id; /// read from the file - int (*read)(struct file *, char *, size_t); + int (*read)(struct file *file, char *buffer, size_t nbytes); /// write into the file - int (*write)(struct file *, char *, size_t); + int (*write)(struct file *file, char *buffer, size_t nbytes); /// seeks the file - int (*seek)(struct file *, long); + int (*seek)(struct file *file, long int offset, int whence); /// get directory entry at index - int (*ents)(struct file *, struct dirent *, size_t); + int (*ents)(struct file *file, struct dirent *dirent, size_t dir_index); + /// closes a file + void (*close)(struct file *file); }; +/// file system vtable, used for opening +/// and stating files. filesystem mount functions must +/// set fs_name, fs_disk, open, and stat. +/// +/// # Variables +/// +/// fs_present - if this filesystem is present in `fs_loaded_file_systems`. +/// - this is set in fs.c. do not touch. +/// +/// fs_id - the unique if for this filesystem. +/// - this is set in fs.c. do not touch. +/// +/// fs_disk - the disk of this filesyste. +/// - this is set in fs.c. do not touch. +/// +/// fs_name - the custom name for this filesystem. mount function is to set +/// - this. +/// +/// # Functions +/// +/// open - open a file at a given fullpath, returning the file pointer +/// - in out on success. see `struct file` +/// +/// stat - get statistics on a file at fillpath +/// +/// # Example FS Mount +/// +/// // mount fs on disk in fs, present, id, & disk are already set +/// int example_mount(struct file_system *fs) +/// { +/// // do some checks to make sure fs on disk is valid +/// if (failure) { +/// return 1; +/// } +/// +/// fs->fs_name = "examplefs"; +/// fs->open = example_open; // see `struct file` +/// fs->stat = example_stat; +/// INFO("loaded examplefs on disk %u", fs->fs_disk->d_id); +/// return 0; +/// } +/// struct file_system { /// set to 1 in fs array to state that fs is defined - /// system use only int fs_present; - /// index into the loaded filesystems array - /// system use only + /// filesystem id int fs_id; - /// mount point of this filesystem - /// system use only - char fs_mount[N_FILE_NAME]; /// the disk this filesystem is hooked up to - struct disk disk; + struct disk *fs_disk; + /// filesystem name + const char *fs_name; /// opens a file - int (*open)(const char *, struct file **); - /// closes a file - void (*close)(struct file *); + int (*open)(const char *fullpath, struct file **out); /// stats a file - int (*stat)(const char *, struct stat *); + int (*stat)(const char *fullpath, struct stat *file); }; // list of all disks on the system -- cgit v1.2.3-freya