Redesign VFS around handles

This commit is contained in:
2026-02-22 13:57:41 +01:00
parent b571e2dbd3
commit 85872b856b
10 changed files with 210 additions and 196 deletions

View File

@@ -108,47 +108,73 @@ struct proc* proc_from_file (struct procgroup* procgroup, const char* mountpoint
const char* path) {
struct desc desc;
if (vfs_open (procgroup, mountpoint, path) != ST_OK)
return NULL;
if (procgroup == NULL) {
if (vfs_kernel_describe (mountpoint, path, &desc) < 0)
return NULL;
if (vfs_describe (procgroup, mountpoint, path, &desc) != ST_OK) {
vfs_close (procgroup, mountpoint, path);
return NULL;
}
if (desc.type != FS_FILE)
return NULL;
if (desc.type != FS_FILE) {
vfs_close (procgroup, mountpoint, path);
return NULL;
}
uint8_t* temp_buffer = malloc (desc.size);
uint8_t* temp_buffer = malloc (desc.size);
if (temp_buffer == NULL)
return NULL;
if (temp_buffer == NULL) {
vfs_close (procgroup, mountpoint, path);
return NULL;
}
if (vfs_kernel_read (mountpoint, path, temp_buffer, 0, desc.size) < 0) {
free (temp_buffer);
return NULL;
}
if (!proc_check_elf (temp_buffer)) {
free (temp_buffer);
return NULL;
}
struct proc* proc = proc_from_elf (temp_buffer);
if (vfs_read (procgroup, mountpoint, path, temp_buffer, 0, desc.size) != ST_OK) {
free (temp_buffer);
vfs_close (procgroup, mountpoint, path);
return NULL;
}
return proc;
} else {
int handle = vfs_open (procgroup, mountpoint, path);
vfs_close (procgroup, mountpoint, path);
if (handle < 0)
return NULL;
bool ok = proc_check_elf (temp_buffer);
if (vfs_describe (procgroup, handle, &desc) != ST_OK) {
vfs_close (procgroup, handle);
return NULL;
}
DEBUG ("Spawning %s:%s, elf header %s\n", mountpoint, path, ok ? "ok" : "bad");
if (desc.type != FS_FILE) {
vfs_close (procgroup, handle);
return NULL;
}
uint8_t* temp_buffer = malloc (desc.size);
if (temp_buffer == NULL) {
vfs_close (procgroup, handle);
return NULL;
}
if (vfs_read (procgroup, handle, temp_buffer, 0, desc.size) != ST_OK) {
free (temp_buffer);
vfs_close (procgroup, handle);
return NULL;
}
vfs_close (procgroup, handle);
if (!proc_check_elf (temp_buffer)) {
free (temp_buffer);
return NULL;
}
struct proc* proc = proc_from_elf (temp_buffer);
if (!ok) {
free (temp_buffer);
return NULL;
return proc;
}
struct proc* proc = proc_from_elf (temp_buffer);
free (temp_buffer);
return proc;
}
struct proc* proc_find_pid (int pid) {

View File

@@ -197,6 +197,8 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc,
spin_unlock (&procgroup->lock);
spin_unlock (&procgroup_tree_lock);
struct list_node_link* resource_delete_list = NULL;
/* delete resources */
struct rb_node_link* rnode;
rbtree_first (&procgroup->resource_tree, rnode);
@@ -209,6 +211,16 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc,
rnode = next;
list_append (resource_delete_list, &resource->delete_list_link);
}
struct list_node_link *resource_delete_link, *resource_delete_tmp_link;
list_foreach (resource_delete_list, resource_delete_link, resource_delete_tmp_link) {
struct proc_resource* resource =
list_entry (resource_delete_link, struct proc_resource, delete_list_link);
list_remove (resource_delete_list, &resource->delete_list_link);
proc_delete_resource (resource, rctx);
}

View File

@@ -33,6 +33,8 @@ struct procgroup {
uintptr_t map_base;
struct procgroup_tls tls;
uint64_t capabilities;
struct rb_node_link* vfs_handle_tree;
int sys_vfs_handles;
};
struct procgroup* procgroup_create (void);

View File

@@ -21,6 +21,7 @@ struct proc_resource {
int rid;
spin_lock_t lock;
struct rb_node_link resource_tree_link;
struct list_node_link delete_list_link;
union {
struct proc_mutex mutex;
struct proc_mail mail;