Redesign VFS around handles
This commit is contained in:
165
kernel/fs/vfs.c
165
kernel/fs/vfs.c
@@ -46,7 +46,6 @@ int vfs_create_mountpoint (const char* key, int fs_type, struct device* back_dev
|
||||
|
||||
memcpy (mountpoint->key, key, strlen_null (key));
|
||||
mountpoint->fs_type = fs_type;
|
||||
mountpoint->lock = SPIN_LOCK_INIT;
|
||||
mountpoint->back_device = back_device;
|
||||
|
||||
switch (mountpoint->fs_type) {
|
||||
@@ -81,111 +80,137 @@ int vfs_create_mountpoint (const char* key, int fs_type, struct device* back_dev
|
||||
}
|
||||
|
||||
int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* path) {
|
||||
(void)path;
|
||||
|
||||
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
|
||||
|
||||
if (vmp == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
spin_lock (&vmp->lock);
|
||||
struct vfs_handle* handle = malloc (sizeof (*handle));
|
||||
|
||||
vmp->ownerpg = procgroup;
|
||||
vmp->locked = true;
|
||||
if (handle == NULL)
|
||||
return -ST_OOM_ERROR;
|
||||
|
||||
spin_unlock (&vmp->lock);
|
||||
memset (handle, 0, sizeof (*handle));
|
||||
handle->mountpoint = vmp;
|
||||
handle->ownerpg = procgroup;
|
||||
strncpy (handle->path, path, sizeof (handle->path));
|
||||
|
||||
spin_lock (&procgroup->lock);
|
||||
|
||||
int id = handle->id = procgroup->sys_vfs_handles++;
|
||||
|
||||
rbtree_insert (struct vfs_handle, &procgroup->vfs_handle_tree, &handle->handle_tree_link,
|
||||
handle_tree_link, id);
|
||||
|
||||
spin_unlock (&procgroup->lock);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
int vfs_close (struct procgroup* procgroup, int id) {
|
||||
struct vfs_handle* handle = NULL;
|
||||
|
||||
spin_lock (&procgroup->lock);
|
||||
|
||||
rbtree_find (struct vfs_handle, &procgroup->vfs_handle_tree, id, handle, handle_tree_link, id);
|
||||
|
||||
if (handle == NULL) {
|
||||
spin_unlock (&procgroup->lock);
|
||||
return -ST_NOT_FOUND;
|
||||
}
|
||||
|
||||
rbtree_delete (&procgroup->vfs_handle_tree, &handle->handle_tree_link);
|
||||
|
||||
spin_unlock (&procgroup->lock);
|
||||
|
||||
free (handle);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int vfs_close (struct procgroup* procgroup, const char* mountpoint, const char* path) {
|
||||
(void)path;
|
||||
int vfs_describe (struct procgroup* procgroup, int id, struct desc* desc) {
|
||||
struct vfs_handle* handle = NULL;
|
||||
|
||||
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
|
||||
spin_lock (&procgroup->lock);
|
||||
|
||||
if (vmp == NULL)
|
||||
rbtree_find (struct vfs_handle, &procgroup->vfs_handle_tree, id, handle, handle_tree_link, id);
|
||||
|
||||
if (handle == NULL) {
|
||||
spin_unlock (&procgroup->lock);
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
spin_lock (&vmp->lock);
|
||||
|
||||
if (procgroup != NULL && vmp->ownerpg != procgroup) {
|
||||
spin_unlock (&vmp->lock);
|
||||
return -ST_PERMISSION_ERROR;
|
||||
}
|
||||
|
||||
vmp->locked = false;
|
||||
vmp->ownerpg = NULL;
|
||||
spin_unlock (&procgroup->lock);
|
||||
|
||||
spin_unlock (&vmp->lock);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int vfs_describe (struct procgroup* procgroup, const char* mountpoint, const char* path,
|
||||
struct desc* desc) {
|
||||
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
|
||||
|
||||
if (vmp == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
spin_lock (&vmp->lock);
|
||||
|
||||
if ((procgroup != NULL && vmp->ownerpg != procgroup) && vmp->locked) {
|
||||
spin_unlock (&vmp->lock);
|
||||
return -ST_PERMISSION_ERROR;
|
||||
}
|
||||
|
||||
int ret = vmp->driver_ops.describe (vmp, path, desc);
|
||||
|
||||
spin_unlock (&vmp->lock);
|
||||
spin_lock (&handle->lock);
|
||||
int ret = handle->mountpoint->driver_ops.describe (handle->mountpoint, handle->path, desc);
|
||||
spin_unlock (&handle->lock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int vfs_read (struct procgroup* procgroup, const char* mountpoint, const char* path,
|
||||
uint8_t* buffer, size_t off, size_t size) {
|
||||
int vfs_read (struct procgroup* procgroup, int id, uint8_t* buffer, size_t off, size_t size) {
|
||||
struct vfs_handle* handle = NULL;
|
||||
|
||||
spin_lock (&procgroup->lock);
|
||||
|
||||
rbtree_find (struct vfs_handle, &procgroup->vfs_handle_tree, id, handle, handle_tree_link, id);
|
||||
|
||||
if (handle == NULL) {
|
||||
spin_unlock (&procgroup->lock);
|
||||
return -ST_NOT_FOUND;
|
||||
}
|
||||
|
||||
spin_unlock (&procgroup->lock);
|
||||
|
||||
spin_lock (&handle->lock);
|
||||
int ret =
|
||||
handle->mountpoint->driver_ops.read (handle->mountpoint, handle->path, buffer, off, size);
|
||||
spin_unlock (&handle->lock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int vfs_kernel_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t off,
|
||||
size_t size) {
|
||||
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
|
||||
|
||||
if (vmp == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
spin_lock (&vmp->lock);
|
||||
return vmp->driver_ops.read (vmp, path, buffer, off, size);
|
||||
}
|
||||
|
||||
if ((procgroup != NULL && vmp->ownerpg != procgroup) && vmp->locked) {
|
||||
spin_unlock (&vmp->lock);
|
||||
return -ST_PERMISSION_ERROR;
|
||||
}
|
||||
int vfs_kernel_describe (const char* mountpoint, const char* path, struct desc* desc) {
|
||||
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
|
||||
|
||||
int ret = vmp->driver_ops.read (vmp, path, buffer, off, size);
|
||||
if (vmp == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
spin_unlock (&vmp->lock);
|
||||
|
||||
return ret;
|
||||
return vmp->driver_ops.describe (vmp, path, desc);
|
||||
}
|
||||
|
||||
void vfs_procgroup_cleanup (struct procgroup* procgroup) {
|
||||
spin_lock (&mount_table.lock);
|
||||
struct list_node_link* handle_cleanup_list = NULL;
|
||||
struct vfs_handle* handle;
|
||||
|
||||
for (size_t i = 0; i < lengthof (mount_table.mountpoint_buckets); i++) {
|
||||
struct hash_node_link* link = mount_table.mountpoint_buckets[i];
|
||||
struct rb_node_link* node;
|
||||
rbtree_first (&procgroup->vfs_handle_tree, node);
|
||||
|
||||
while (link != NULL) {
|
||||
struct vfs_mountpoint* vmp = hash_entry (link, struct vfs_mountpoint, mount_table_link);
|
||||
|
||||
spin_lock (&vmp->lock);
|
||||
|
||||
if (vmp->ownerpg == procgroup) {
|
||||
vmp->locked = false;
|
||||
vmp->ownerpg = NULL;
|
||||
}
|
||||
|
||||
spin_unlock (&vmp->lock);
|
||||
|
||||
link = link->next;
|
||||
}
|
||||
while (node != NULL) {
|
||||
struct rb_node_link* next;
|
||||
rbtree_next (node, next);
|
||||
handle = rbtree_entry (node, struct vfs_handle, handle_tree_link);
|
||||
node = next;
|
||||
list_append (handle_cleanup_list, &handle->handle_cleanup_link);
|
||||
}
|
||||
|
||||
spin_unlock (&mount_table.lock);
|
||||
struct list_node_link *cleanup_link, *cleanup_tmp_link;
|
||||
list_foreach (handle_cleanup_list, cleanup_link, cleanup_tmp_link) {
|
||||
handle = list_entry (cleanup_link, struct vfs_handle, handle_cleanup_link);
|
||||
list_remove (handle_cleanup_list, &handle->handle_cleanup_link);
|
||||
free (handle);
|
||||
}
|
||||
}
|
||||
|
||||
void vfs_init (void) {
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
#include <device/device.h>
|
||||
#include <libk/hash.h>
|
||||
#include <libk/list.h>
|
||||
#include <libk/rbtree.h>
|
||||
#include <libk/std.h>
|
||||
#include <path.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/procgroup.h>
|
||||
#include <proc/reschedule.h>
|
||||
@@ -13,13 +15,22 @@
|
||||
|
||||
#define VFS_TARFS 0
|
||||
|
||||
struct vfs_mountpoint;
|
||||
|
||||
struct vfs_handle {
|
||||
int id;
|
||||
struct vfs_mountpoint* mountpoint;
|
||||
char path[MAX_PATH];
|
||||
struct procgroup* ownerpg;
|
||||
struct rb_node_link handle_tree_link;
|
||||
struct list_node_link handle_cleanup_link;
|
||||
spin_lock_t lock;
|
||||
};
|
||||
|
||||
struct vfs_mountpoint {
|
||||
char key[0x100];
|
||||
struct hash_node_link mount_table_link;
|
||||
int fs_type;
|
||||
spin_lock_t lock;
|
||||
bool locked;
|
||||
struct procgroup* ownerpg;
|
||||
struct {
|
||||
int (*mount) (struct vfs_mountpoint* mountpoint, struct proc* proc,
|
||||
struct reschedule_ctx* rctx);
|
||||
@@ -41,16 +52,19 @@ struct vfs_mount_table {
|
||||
int vfs_create_mountpoint (const char* key, int fs_type, struct device* back_device,
|
||||
struct proc* proc, struct reschedule_ctx* rctx);
|
||||
|
||||
int vfs_describe (struct procgroup* procgroup, const char* mountpoint, const char* path,
|
||||
struct desc* desc);
|
||||
int vfs_describe (struct procgroup* procgroup, int id, struct desc* desc);
|
||||
|
||||
int vfs_read (struct procgroup* procgroup, const char* mountpoint, const char* path,
|
||||
uint8_t* buffer, size_t off, size_t size);
|
||||
int vfs_read (struct procgroup* procgroup, int id, uint8_t* buffer, size_t off, size_t size);
|
||||
|
||||
int vfs_close (struct procgroup* procgroup, const char* mountpoint, const char* path);
|
||||
int vfs_close (struct procgroup* procgroup, int id);
|
||||
|
||||
int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* path);
|
||||
|
||||
int vfs_kernel_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t off,
|
||||
size_t size);
|
||||
|
||||
int vfs_kernel_describe (const char* mountpoint, const char* path, struct desc* desc);
|
||||
|
||||
void vfs_procgroup_cleanup (struct procgroup* procgroup);
|
||||
|
||||
void vfs_init (void);
|
||||
|
||||
Reference in New Issue
Block a user