#define MODULE "vfs" #include "fs/VFS.h" #include "errno.h" #include "log/Log.h" #include "std/stdlib.h" #include "std/string.h" static VFS::Node* vfs_root; ssize_t VFS::read(Node* node, size_t offset, size_t length, char* buffer) { if (!node) { kwarnln("read() failed: trying to read from nullptr"); return -1; } if (node->type == VFS_DIRECTORY) { kwarnln("read() failed: is a directory"); return -EISDIR; } if (!node->read_func) { kwarnln("read() failed: the chosen node doesn't support reading"); return -1; } kdbgln("read(): node %s (inode %ld), offset %zd, %zd bytes, into %p", node->name, node->inode, offset, length, (void*)buffer); return node->read_func(node, offset, length, buffer); } void VFS::mount_root(Node* root) { if (!root) { kwarnln("mount_root() failed: attempted to mount nullptr"); return; } if (vfs_root) { kwarnln("mount_root() failed: root filesystem already mounted"); return; } kinfoln("mounting node '%s' as vfs root", root->name); vfs_root = root; } VFS::Node* VFS::root() { return vfs_root; } VFS::Node* VFS::resolve_path(const char* filename, Node* root) { if (!root) root = vfs_root; if (strlen(filename) == 0) return 0; if (*filename == '/') // Absolute path. { filename++; root = vfs_root; } Node* current_node = root; while (true) { while (*filename == '/') { filename++; } if (*filename == 0) { return current_node; } size_t path_section_size = 0; while (filename[path_section_size] && filename[path_section_size] != '/') { path_section_size++; } if (strncmp(filename, ".", path_section_size) != 0) // The current path section is not '.' { char* buffer = (char*)kmalloc(path_section_size + 1); memcpy(buffer, filename, path_section_size); buffer[path_section_size] = 0; if (!current_node->find_func) { kwarnln("Current node has no way to find child nodes"); return 0; } Node* child = current_node->find_func(current_node, buffer); if (!child) { kwarnln("Current node did not find our target node"); return 0; } if (child->flags & VFS_MOUNTPOINT) { kdbgln("Current node (%s, inode=%ld) is a mountpoint, resolving actual node", child->name, child->inode); if (!child->link) { kwarnln("Current node's link is null"); return 0; } child = child->link; kdbgln("Resolved to %s (inode %ld)", child->name, child->inode); } current_node = child; kfree(buffer); } filename += path_section_size; } } int VFS::mkdir(const char* path, const char* name) { Node* node = resolve_path(path, vfs_root); if (!node) { kwarnln("Attempting to mkdir in %s, which does not exist", path); return -1; } if (!node->mkdir_func) { kwarnln("Chosen node does not support mkdir()"); return -1; } return node->mkdir_func(node, name); } void VFS::mount(Node* mountpoint, Node* mounted) { if (!mountpoint || !mounted) return; if (mountpoint->flags & VFS_MOUNTPOINT || mounted->flags & VFS_MOUNTPOINT) return; mountpoint->link = mounted; mountpoint->flags |= VFS_MOUNTPOINT; } void VFS::unmount(Node* mountpoint) { if (!mountpoint) return; if (!(mountpoint->flags & VFS_MOUNTPOINT)) return; mountpoint->flags &= ~VFS_MOUNTPOINT; }