112 lines
2.8 KiB
C
112 lines
2.8 KiB
C
#include <fs/ramdiskfs.h>
|
|
#include <fs/vfs.h>
|
|
#include <libk/fieldsizeof.h>
|
|
#include <libk/hash.h>
|
|
#include <libk/lengthof.h>
|
|
#include <libk/std.h>
|
|
#include <libk/string.h>
|
|
#include <mm/liballoc.h>
|
|
#include <sync/spin_lock.h>
|
|
#include <sys/debug.h>
|
|
|
|
static struct vfs_mount_table mount_table;
|
|
|
|
static struct vfs_mountpoint* vfs_find_mountpoint (const char* mountpoint) {
|
|
struct hash_node_link* found_link = NULL;
|
|
size_t mountpoint_len = strlen (mountpoint);
|
|
uint32_t hash = hash_fnv32 (mountpoint, strlen (mountpoint));
|
|
|
|
spin_lock (&mount_table.lock);
|
|
hash_find (&mount_table, mountpoint, mountpoint_len, hash,
|
|
lengthof (mount_table.mountpoint_buckets), mountpoint_buckets, struct vfs_mountpoint,
|
|
mount_table_link, key, found_link);
|
|
spin_unlock (&mount_table.lock);
|
|
|
|
if (found_link == NULL)
|
|
return NULL;
|
|
|
|
return hash_entry (found_link, struct vfs_mountpoint, mount_table_link);
|
|
}
|
|
|
|
struct vfs_mountpoint* vfs_create_mountpoint (const char* key, int fs_type) {
|
|
if (strlen_null (key) > fieldsizeof (struct vfs_mountpoint, key))
|
|
return NULL;
|
|
|
|
struct vfs_mountpoint* mountpoint = malloc (sizeof (*mountpoint));
|
|
|
|
if (mountpoint == NULL)
|
|
return NULL;
|
|
|
|
memset (mountpoint, 0, sizeof (*mountpoint));
|
|
|
|
memcpy (mountpoint->key, key, strlen_null (key));
|
|
mountpoint->fs_type = fs_type;
|
|
mountpoint->lock = SPIN_LOCK_INIT;
|
|
|
|
switch (mountpoint->fs_type) {
|
|
case VFS_RAMDISKFS: {
|
|
mountpoint->driver_ops.mount = &ramdiskfs_mount;
|
|
mountpoint->driver_ops.describe = &ramdiskfs_describe;
|
|
mountpoint->driver_ops.read = &ramdiskfs_read;
|
|
} break;
|
|
default: {
|
|
free (mountpoint);
|
|
return NULL;
|
|
} break;
|
|
}
|
|
|
|
bool ok = mountpoint->driver_ops.mount (mountpoint);
|
|
|
|
if (!ok) {
|
|
free (mountpoint);
|
|
return NULL;
|
|
}
|
|
|
|
uint32_t mp_hash = hash_fnv32 (mountpoint->key, strlen (mountpoint->key));
|
|
|
|
spin_lock (&mount_table.lock);
|
|
|
|
hash_insert (&mount_table, &mountpoint->mount_table_link, mp_hash,
|
|
lengthof (mount_table.mountpoint_buckets), mountpoint_buckets);
|
|
|
|
spin_unlock (&mount_table.lock);
|
|
|
|
return mountpoint;
|
|
}
|
|
|
|
int vfs_describe (const char* mountpoint, const char* path, struct fs_desc_buffer* desc) {
|
|
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
|
|
|
|
if (vmp == NULL)
|
|
return -VFS_NOT_FOUND;
|
|
|
|
spin_lock (&vmp->lock);
|
|
|
|
int ret = vmp->driver_ops.describe (vmp, path, desc);
|
|
|
|
spin_unlock (&vmp->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int vfs_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 -VFS_NOT_FOUND;
|
|
|
|
spin_lock (&vmp->lock);
|
|
|
|
int ret = vmp->driver_ops.read (vmp, path, buffer, off, size);
|
|
|
|
spin_unlock (&vmp->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void vfs_init (void) {
|
|
memset (&mount_table, 0, sizeof (mount_table));
|
|
|
|
mount_table.lock = SPIN_LOCK_INIT;
|
|
}
|