Finished with ramfs_create(), rest of the VFS soon to follow

This commit is contained in:
notsomeidiot123 2026-07-05 21:36:23 -04:00
parent 817995394b
commit 9065a5c68a
6 changed files with 84 additions and 26 deletions

View File

@ -89,7 +89,7 @@ void pci_make_file(uint32_t class, uint8_t bus, uint8_t slot, uint8_t func){
// file->write = pci_write_file;
// if(file == 0) break;
if(!file){
mlog(MODULE_NAME, "Failed to make file for PCI device!\n", MLOG_PRINT);
mlog(MODULE_NAME, "Failed to make file for PCI device %s!\n", MLOG_PRINT, cpy);
return;
}
// printf("ASSERT file == null: %d\n", file == 0);

View File

@ -58,10 +58,10 @@ extern void kmain(kernel_info_t *kernel_info){
pic_setmask(0x0, PIC1_DATA);
pic_setmask(0x0, PIC2_DATA);
vfs_init();
fcreate("/dev", FS_FILE_IS_DIR);
fcreate("/dev/disk", FS_FILE_IS_DIR);
fcreate("/tmp", FS_FILE_IS_DIR);
fcreate("/dev", FS_FILE_IS_DIR);
fcreate("/boot", FS_FILE_IS_DIR);
fcreate("/dev/disk", FS_FILE_IS_DIR);
mlog("KERNEL", "Initializing Scheduler & starting PID 1\n", MLOG_PRINT);
boot_info = kernel_info;
scheduler_init();

View File

@ -20,15 +20,19 @@ vfile_t root_dir = {"/", FS_FILE_IS_DIR | FS_FILE_SYSTEM, &ramfs_ops};
void ramfs_init(){
mlog(MODULE_NAME, "Initializing VFS\n", MLOG_PRINT);
root_dir.private = kmalloc(1);
root_dir.size = PAGE_SIZE_BYTES;
root_dir.private = 0;
root_dir.size = 0;
// printf("Sizeof VFILE_T: %d", sizeof(vfile_t));
}
vfile_t *search_dir(char *subname, vfile_t *directory){
if(!directory) return 0;
vfile_t **children = (vfile_t **)directory->private;
if(!children){
return 0;
}
for(int i = 0; i < directory->size / sizeof(vfile_t *); i++){
if(!strcmp(children[i], subname)){
if(children[i] && !strcmp(children[i]->name, subname)){
return children[i];
}
}
@ -69,7 +73,6 @@ vfile_t *resolve_path(char *pathname, vfile_t *start, uint32_t get_last_child){
for(int i = 0; i < pathname_entries - !get_last_child; i++){
entry = search_dir(path_tokens[i], entry);
current_path += strlen(path_tokens[i]) + 1;
if(!entry){
break;
}
@ -78,7 +81,8 @@ vfile_t *resolve_path(char *pathname, vfile_t *start, uint32_t get_last_child){
entry = 0;
break;
}
entry->fileops->rfopen(current_path, entry);
entry = entry->fileops->rfopen(current_path, entry);
break;
}
}
kfree(path);
@ -88,29 +92,77 @@ vfile_t *resolve_path(char *pathname, vfile_t *start, uint32_t get_last_child){
char *get_filename(char *path){
char *filename = path + strlen(path) - 1;//get last character in pathname
while(*filename != '/' || filename == path) filename--;
while(*filename != path[0] && (*filename != '/' || filename == path)) filename--;
filename += filename != path;//if the new filename is at the beginning of the path, don't adjust for '/' character
return filename;
}
void ramfs_resize(vfile_t *file, uint32_t new_size_bytes){
if(new_size_bytes == file->size) return;
int ramfs_resize(vfile_t *file, uint32_t new_size_bytes){
if(new_size_bytes == file->size) return new_size_bytes;
uint32_t new_size_pages = (new_size_bytes + PAGE_SIZE_BYTES - 1)/PAGE_SIZE_BYTES;
printf("New page count: %d from %d\n", new_size_pages, new_size_bytes);
uint32_t old_size_pages = (file->size + PAGE_SIZE_BYTES - 1)/PAGE_SIZE_BYTES;
do{
if(old_size_pages == new_size_pages){
break;
}
void *new_ptr = kmalloc(new_size_pages);
if(!new_ptr){
mlog(MODULE_NAME, "Failed to allocate memory for virtual file!\n", MLOG_PRINT);
return 0;
}
if(file->private){
memcpy((uint32_t *)file->private, (uint32_t*)new_ptr, file->size);
kfree(file->private);
}
file->private = new_ptr;
}while(0);
file->size = new_size_bytes;
return new_size_bytes;
}
vfile_t *ramfs_create(char *path, FS_FILE_FLAGS flags){
vfile_t *ramfs_create(vfile_t *root, char *path, FS_FILE_FLAGS flags){
if(!path){
return 0;
}
vfile_t *parent = resolve_path(path, &root_dir, false);
if(!parent){
if(path[0] == '/') path++;
vfile_t *parent = resolve_path(path, root, false);
if(!parent || !parent->flags & FS_FILE_IS_DIR){
mlog(MODULE_NAME, "Could not locate parent directory for %s\n", MLOG_PRINT, path);
}
get_filename(path);
ramfs_resize(parent, 8191);
path--;
return 0;
}
char *new_name = get_filename(path);
// printf("%d\n", parent->size);
vfile_t **dirents = (vfile_t **)parent->private;
uint32_t insert_index = 0;
for(uint32_t i = 0; i < parent->size/sizeof(vfile_t *); i++){
if(dirents[i]){
continue;
}
//Empty dirent, can insert
vfile_t *new_file = kmalloc(1);
*new_file = (vfile_t){0};
// new_file->name = new_name;
strcpy(new_name, new_file->name);
new_file->flags = flags;
dirents[i] = new_file;
new_file->fileops = &ramfs_ops;
path--;
return new_file;
}
//no empty dirents, must resize
if( !ramfs_resize(parent, parent->size + 4)) return 0;
dirents = parent->private;
vfile_t *new_file = kmalloc(1);
if(!new_file) return 0;
*new_file = (vfile_t){0};
strcpy(new_name, new_file->name);
new_file->flags = flags;
dirents[parent->size/(sizeof(vfile_t*)) - 1] = new_file;
new_file->fileops = &ramfs_ops;
path--;
return new_file;
}
int ramfs_delete(vfile_t *file){
return 0;
@ -130,7 +182,9 @@ void ramfs_close(vfile_t *file){
return;
}
vfile_t *ramfs_rfopen(char *name, vfile_t *parent){
return 0;
vfile_t *returnable = resolve_path(name, parent, true);
// for(;;);
return returnable;
}
vfile_t *get_root_dir(){

View File

@ -2,7 +2,7 @@
#include <stdint.h>
void ramfs_init();
vfile_t *ramfs_create(char *path, FS_FILE_FLAGS flags);
vfile_t *ramfs_create(vfile_t *parent, char *path, FS_FILE_FLAGS flags);
//Will never create a new reference
vfile_t *get_root_dir();

View File

@ -25,10 +25,11 @@ vfile_t *fcreate(char *path, FS_FILE_FLAGS flags){
spinlock_acquire(vfs_lock);
vfile_t *returnable = vfcreate(get_root_dir(), path, flags);
spinlock_release(vfs_lock);
return 0;
return returnable;
}
vfile_t *vfcreate(vfile_t *parent_dir, char *relpath, FS_FILE_FLAGS flags){
// printf("%s\n", relpath);
if(!relpath){
return 0;
}
@ -37,6 +38,7 @@ vfile_t *vfcreate(vfile_t *parent_dir, char *relpath, FS_FILE_FLAGS flags){
strcpy(relpath, name);
if(name[0] == '/') name++;//first slash just indicates that it's an absolute path, we don't want to include that into the filename.
uint32_t name_length = strlen(name);
if(name[name_length-1] == '/') name[name_length - 1] = 0;
uint32_t name_index = 0;
uint32_t subpath_start = 0;
@ -51,21 +53,23 @@ vfile_t *vfcreate(vfile_t *parent_dir, char *relpath, FS_FILE_FLAGS flags){
if(name_index >= name_length){//if there is no other directory
// printf("Creating file; name: %s\n", name + subpath_start);
new_file = parent_dir->fileops->create(name, flags);
new_file = parent_dir->fileops->create(parent_dir, name, flags);
}
else{
vfile_t *new_parent = rfopen(name, parent_dir);
if(!new_parent){
// printf("No new parent found!\n");
printf("No new parent found from name: %s!\n", name);
kfree(name);
return 0;
}
// printf("Recursed\n");
// printf("%s", new_parent->name);
new_file = vfcreate(new_parent, name+name_index+1, flags);
fclose(new_parent);
// fclose(new_parent);
}
kfree(name);
// printf("Returning new file!\n");
return new_file;
// return 0;
}
int fdelete(vfile_t *file_entry){

View File

@ -13,7 +13,7 @@ typedef enum fs_flags{
}FS_FILE_FLAGS;
typedef struct fileops{
struct virtual_file *(*create)(char *path, FS_FILE_FLAGS flags);
struct virtual_file *(*create)(struct virtual_file *parent, char *path, FS_FILE_FLAGS flags);
int (*delete)(struct virtual_file *file_entry);
int (*write)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count);
int (*read)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count);