2023-02-03 21:18:52 +00:00
|
|
|
#include "fs/VFS.h"
|
2023-03-10 21:18:48 +00:00
|
|
|
#include "Log.h"
|
2023-05-17 17:40:37 +00:00
|
|
|
#include "fs/Mount.h"
|
2023-04-08 11:12:49 +00:00
|
|
|
#include "thread/Thread.h"
|
|
|
|
#include <bits/modes.h>
|
2023-03-10 21:18:48 +00:00
|
|
|
#include <luna/PathParser.h>
|
2023-04-16 09:25:32 +00:00
|
|
|
#include <luna/Utf8.h>
|
2023-02-03 21:18:52 +00:00
|
|
|
|
|
|
|
namespace VFS
|
|
|
|
{
|
2023-06-17 15:27:22 +00:00
|
|
|
SharedPtr<VFS::Inode> g_root_inode = {};
|
2023-02-25 17:05:25 +00:00
|
|
|
|
|
|
|
Inode& root_inode()
|
|
|
|
{
|
2023-06-17 15:27:22 +00:00
|
|
|
return *g_root_inode;
|
2023-03-10 21:18:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 19:46:31 +00:00
|
|
|
static constexpr int MAX_SYMLINKS = 8;
|
|
|
|
|
|
|
|
Result<SharedPtr<Inode>> resolve_path_impl(const char* path, Credentials auth, SharedPtr<Inode> current_inode,
|
|
|
|
bool follow_last_symlink, int& symlinks_followed)
|
2023-03-10 21:18:48 +00:00
|
|
|
{
|
2023-05-20 19:46:31 +00:00
|
|
|
if (symlinks_followed >= MAX_SYMLINKS) return err(ELOOP);
|
|
|
|
|
2023-04-15 18:26:15 +00:00
|
|
|
if (*path == '\0') return err(ENOENT);
|
|
|
|
|
2023-03-10 21:18:48 +00:00
|
|
|
auto parser = TRY(PathParser::create(path));
|
|
|
|
|
2023-05-20 19:46:31 +00:00
|
|
|
SharedPtr<Inode> parent_inode = current_inode;
|
2023-03-10 21:18:48 +00:00
|
|
|
|
|
|
|
const char* section;
|
2023-04-08 11:12:49 +00:00
|
|
|
while (parser.next().try_set_value(section))
|
|
|
|
{
|
|
|
|
if (!can_execute(current_inode, auth)) return err(EACCES);
|
|
|
|
current_inode = TRY(current_inode->find(section));
|
2023-05-20 19:46:31 +00:00
|
|
|
|
|
|
|
if (current_inode->type() == VFS::InodeType::Symlink && (follow_last_symlink || parser.has_next()))
|
|
|
|
{
|
|
|
|
auto link = TRY(current_inode->readlink());
|
|
|
|
|
|
|
|
SharedPtr<VFS::Inode> symlink_root;
|
|
|
|
|
2023-06-17 15:27:22 +00:00
|
|
|
if (PathParser::is_absolute(link.chars())) symlink_root = g_root_inode;
|
2023-05-20 19:46:31 +00:00
|
|
|
else
|
|
|
|
symlink_root = parent_inode;
|
|
|
|
|
|
|
|
symlinks_followed++;
|
|
|
|
current_inode = TRY(resolve_path_impl(link.chars(), auth, symlink_root, true, symlinks_followed));
|
|
|
|
symlinks_followed--;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_inode = current_inode;
|
2023-04-08 11:12:49 +00:00
|
|
|
}
|
2023-03-10 21:18:48 +00:00
|
|
|
|
|
|
|
return current_inode;
|
2023-02-25 17:05:25 +00:00
|
|
|
}
|
2023-03-11 00:13:44 +00:00
|
|
|
|
2023-05-20 19:46:31 +00:00
|
|
|
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth, SharedPtr<VFS::Inode> working_directory,
|
|
|
|
bool follow_last_symlink)
|
|
|
|
{
|
|
|
|
SharedPtr<Inode> current_inode;
|
|
|
|
|
2023-06-17 15:27:22 +00:00
|
|
|
if (PathParser::is_absolute(path) || !working_directory) current_inode = g_root_inode;
|
2023-05-20 19:46:31 +00:00
|
|
|
else
|
|
|
|
current_inode = working_directory;
|
|
|
|
|
|
|
|
int symlinks_followed = 0;
|
|
|
|
|
|
|
|
return resolve_path_impl(path, auth, current_inode, follow_last_symlink, symlinks_followed);
|
|
|
|
}
|
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
|
2023-03-11 00:13:44 +00:00
|
|
|
{
|
2023-06-19 09:21:58 +00:00
|
|
|
auto parent_path = TRY(PathParser::dirname(path));
|
2023-03-11 00:13:44 +00:00
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
|
2023-04-08 11:12:49 +00:00
|
|
|
|
|
|
|
if (!can_write(parent_inode, auth)) return err(EACCES);
|
2023-03-11 00:13:44 +00:00
|
|
|
|
2023-06-19 09:21:58 +00:00
|
|
|
auto child_name = TRY(PathParser::basename(path));
|
2023-03-11 00:13:44 +00:00
|
|
|
|
2023-04-16 09:25:32 +00:00
|
|
|
TRY(validate_filename(child_name.view()));
|
|
|
|
|
2023-03-11 00:13:44 +00:00
|
|
|
return parent_inode->create_subdirectory(child_name.chars());
|
|
|
|
}
|
2023-03-11 09:23:46 +00:00
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
|
2023-03-11 09:23:46 +00:00
|
|
|
{
|
2023-06-19 09:21:58 +00:00
|
|
|
auto parent_path = TRY(PathParser::dirname(path));
|
2023-03-11 09:23:46 +00:00
|
|
|
|
2023-04-11 20:14:57 +00:00
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
|
2023-04-08 11:12:49 +00:00
|
|
|
|
|
|
|
if (!can_write(parent_inode, auth)) return err(EACCES);
|
2023-03-11 09:23:46 +00:00
|
|
|
|
2023-06-19 09:21:58 +00:00
|
|
|
auto child_name = TRY(PathParser::basename(path));
|
2023-03-11 09:23:46 +00:00
|
|
|
|
2023-04-16 09:25:32 +00:00
|
|
|
TRY(validate_filename(child_name.view()));
|
|
|
|
|
2023-03-11 09:23:46 +00:00
|
|
|
return parent_inode->create_file(child_name.chars());
|
|
|
|
}
|
2023-04-08 11:12:49 +00:00
|
|
|
|
2023-04-16 09:25:32 +00:00
|
|
|
Result<void> validate_filename(StringView name)
|
|
|
|
{
|
2023-06-22 18:22:43 +00:00
|
|
|
#ifdef MOON_DISABLE_FILENAME_RESTRICTIONS
|
2023-06-22 18:24:33 +00:00
|
|
|
(void)name;
|
2023-06-22 18:22:43 +00:00
|
|
|
return {};
|
|
|
|
#endif
|
|
|
|
|
2023-04-16 09:25:32 +00:00
|
|
|
// Forbid problematic characters that could cause trouble in shell scripts and the like.
|
|
|
|
if (strpbrk(name.chars(), "*?:[]\"<>\\")) return err(EINVAL);
|
|
|
|
|
|
|
|
// Forbid filenames with leading spaces.
|
|
|
|
if (!name.is_empty() && name[0] == ' ') return err(EINVAL);
|
|
|
|
|
|
|
|
// Forbid filenames with trailing spaces.
|
|
|
|
if (!name.is_empty() && name[name.length() - 1] == ' ') return err(EINVAL);
|
|
|
|
|
|
|
|
for (const auto& c : name)
|
|
|
|
{
|
|
|
|
// Forbid filenames with control characters.
|
|
|
|
if (c < 32 || c == 127) return err(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forbid filenames that are not valid UTF-8.
|
|
|
|
|
|
|
|
Utf8StringDecoder decoder(name.chars());
|
|
|
|
|
|
|
|
// This will fail if the filename is invalid UTF-8.
|
|
|
|
TRY(decoder.code_points());
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
bool can_execute(SharedPtr<Inode> inode, Credentials auth)
|
|
|
|
{
|
2023-05-17 17:40:37 +00:00
|
|
|
if (auth.euid == 0) return true;
|
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
if (inode->uid() == auth.euid) { return inode->mode() & S_IXUSR; }
|
|
|
|
if (inode->gid() == auth.egid) { return inode->mode() & S_IXGRP; }
|
|
|
|
|
|
|
|
return inode->mode() & S_IXOTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool can_write(SharedPtr<Inode> inode, Credentials auth)
|
|
|
|
{
|
2023-05-17 17:40:37 +00:00
|
|
|
if (auth.euid == 0) return true;
|
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
if (inode->uid() == auth.euid) { return inode->mode() & S_IWUSR; }
|
|
|
|
if (inode->gid() == auth.egid) { return inode->mode() & S_IWGRP; }
|
|
|
|
|
|
|
|
return inode->mode() & S_IWOTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool can_read(SharedPtr<Inode> inode, Credentials auth)
|
|
|
|
{
|
2023-05-17 17:40:37 +00:00
|
|
|
if (auth.euid == 0) return true;
|
|
|
|
|
2023-04-08 11:12:49 +00:00
|
|
|
if (inode->uid() == auth.euid) { return inode->mode() & S_IRUSR; }
|
|
|
|
if (inode->gid() == auth.egid) { return inode->mode() & S_IRGRP; }
|
|
|
|
|
|
|
|
return inode->mode() & S_IROTH;
|
|
|
|
}
|
2023-04-08 14:32:56 +00:00
|
|
|
|
|
|
|
bool is_setuid(SharedPtr<Inode> inode)
|
|
|
|
{
|
|
|
|
return inode->mode() & S_ISUID;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_setgid(SharedPtr<Inode> inode)
|
|
|
|
{
|
|
|
|
return inode->mode() & S_ISGID;
|
|
|
|
}
|
2023-05-17 17:40:37 +00:00
|
|
|
|
2023-06-03 09:55:10 +00:00
|
|
|
bool is_sticky(SharedPtr<Inode> inode)
|
|
|
|
{
|
|
|
|
return inode->mode() & S_ISVTX;
|
|
|
|
}
|
|
|
|
|
2023-05-26 18:27:47 +00:00
|
|
|
bool is_seekable(SharedPtr<Inode> inode)
|
|
|
|
{
|
|
|
|
return inode->type() != InodeType::FIFO && inode->type() != InodeType::CharacterDevice;
|
|
|
|
}
|
|
|
|
|
2023-05-17 17:40:37 +00:00
|
|
|
Result<void> mount_root(SharedPtr<VFS::FileSystem> fs)
|
|
|
|
{
|
2023-06-17 15:27:22 +00:00
|
|
|
check(!g_root_inode);
|
|
|
|
|
|
|
|
g_root_inode = TRY(MountInode::create({}, fs));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> pivot_root(const char* new_root, const char* put_old, SharedPtr<VFS::Inode> working_directory)
|
|
|
|
{
|
2023-06-19 09:21:58 +00:00
|
|
|
auto new_root_parent = TRY(PathParser::dirname(new_root));
|
|
|
|
auto new_root_path = TRY(PathParser::basename(new_root));
|
2023-06-17 15:27:22 +00:00
|
|
|
|
|
|
|
auto new_root_parent_inode = TRY(VFS::resolve_path(new_root_parent.chars(), Credentials {}, working_directory));
|
|
|
|
auto new_root_inode = TRY(new_root_parent_inode->find(new_root_path.chars()));
|
|
|
|
|
|
|
|
if (new_root_inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
|
|
|
|
if (!new_root_inode->is_mountpoint()) return err(EINVAL);
|
|
|
|
if (new_root_inode->fs() == g_root_inode->fs()) return err(EBUSY);
|
|
|
|
|
2023-06-19 09:21:58 +00:00
|
|
|
auto parent_path = TRY(PathParser::dirname(put_old));
|
|
|
|
auto child = TRY(PathParser::basename(put_old));
|
2023-06-17 15:27:22 +00:00
|
|
|
|
|
|
|
kdbgln("vfs: Pivoting root from / to %s, using %s as new root", put_old, new_root);
|
|
|
|
|
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars(), Credentials {}, working_directory));
|
|
|
|
|
|
|
|
auto inode = TRY(parent_inode->find(child.chars()));
|
|
|
|
if (inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
|
|
|
|
if (inode->is_mountpoint()) return err(EBUSY);
|
|
|
|
if (inode->fs() != new_root_inode->fs()) return err(EINVAL);
|
|
|
|
|
|
|
|
auto mount = g_root_inode;
|
|
|
|
|
|
|
|
TRY(parent_inode->replace_entry(mount, child.chars()));
|
|
|
|
((MountInode*)mount.ptr())->set_source(inode);
|
|
|
|
|
|
|
|
g_root_inode = new_root_inode;
|
|
|
|
TRY(new_root_parent_inode->replace_entry(((MountInode*)g_root_inode.ptr())->source(), new_root_path.chars()));
|
|
|
|
((MountInode*)g_root_inode.ptr())->set_source({});
|
2023-06-22 17:57:12 +00:00
|
|
|
g_root_inode->fs()->reset_mount_dir();
|
2023-05-17 17:40:37 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> mount(const char* path, SharedPtr<VFS::FileSystem> fs, Credentials auth,
|
|
|
|
SharedPtr<VFS::Inode> working_directory)
|
|
|
|
{
|
2023-06-19 09:21:58 +00:00
|
|
|
auto parent_path = TRY(PathParser::dirname(path));
|
|
|
|
auto child = TRY(PathParser::basename(path));
|
2023-05-17 17:40:37 +00:00
|
|
|
|
2023-07-10 11:04:34 +00:00
|
|
|
#ifdef MOUNT_DEBUG
|
|
|
|
kdbgln("vfs: Mounting filesystem on target %s", path);
|
|
|
|
#endif
|
2023-05-17 17:40:37 +00:00
|
|
|
|
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
|
|
|
|
|
|
|
|
auto inode = TRY(parent_inode->find(child.chars()));
|
|
|
|
if (inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
|
|
|
|
if (inode->is_mountpoint()) return err(EBUSY);
|
|
|
|
|
|
|
|
auto mount = TRY(MountInode::create(inode, fs));
|
|
|
|
|
|
|
|
TRY(parent_inode->replace_entry(mount, child.chars()));
|
|
|
|
|
2023-07-10 11:04:34 +00:00
|
|
|
kinfoln("vfs: Successfully mounted filesystem on target %s", path);
|
|
|
|
|
2023-05-17 17:40:37 +00:00
|
|
|
return {};
|
|
|
|
}
|
2023-05-17 17:52:26 +00:00
|
|
|
|
|
|
|
Result<void> umount(const char* path, Credentials auth, SharedPtr<VFS::Inode> working_directory)
|
|
|
|
{
|
2023-06-19 09:21:58 +00:00
|
|
|
auto parent_path = TRY(PathParser::dirname(path));
|
|
|
|
auto child = TRY(PathParser::basename(path));
|
2023-05-17 17:52:26 +00:00
|
|
|
|
2023-06-17 15:27:22 +00:00
|
|
|
if (child.view() == "/") return err(EBUSY);
|
|
|
|
|
2023-06-18 18:18:00 +00:00
|
|
|
kinfoln("vfs: Unmounting filesystem on target %s", path);
|
2023-05-17 17:52:26 +00:00
|
|
|
|
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
|
|
|
|
|
|
|
|
auto inode = TRY(parent_inode->find(child.chars()));
|
|
|
|
if (!inode->is_mountpoint()) return err(EINVAL);
|
|
|
|
|
|
|
|
// There are still open file descriptors referencing files within this file system.
|
|
|
|
if (inode->fs()->handles() != 0) return err(EBUSY);
|
|
|
|
|
|
|
|
auto mount = (MountInode*)inode.ptr();
|
|
|
|
TRY(parent_inode->replace_entry(mount->source(), child.chars()));
|
2023-05-18 14:18:09 +00:00
|
|
|
g_mounts.remove(mount);
|
2023-05-17 17:52:26 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2023-02-03 21:18:52 +00:00
|
|
|
}
|