kernel: Make the root inode be a mountpoint as well + add pivot_root()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a6330eaffc
commit
e79d4297ea
@ -31,3 +31,4 @@ luna_app(time.cpp time)
|
||||
luna_app(ln.cpp ln)
|
||||
luna_app(mktemp.cpp mktemp)
|
||||
luna_app(sysfuzz.cpp sysfuzz)
|
||||
luna_app(pivot_root.cpp pivot_root)
|
||||
|
19
apps/pivot_root.cpp
Normal file
19
apps/pivot_root.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <os/ArgumentParser.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
Result<int> luna_main(int argc, char** argv)
|
||||
{
|
||||
StringView new_root;
|
||||
StringView put_old;
|
||||
|
||||
os::ArgumentParser parser;
|
||||
parser.add_description("Move the current root directory to another directory and replace it with another mount.");
|
||||
parser.add_system_program_info("pivot_root"_sv);
|
||||
parser.add_positional_argument(new_root, "new_root", true);
|
||||
parser.add_positional_argument(put_old, "put_old", true);
|
||||
parser.parse(argc, argv);
|
||||
|
||||
long rc = syscall(SYS_pivot_root, new_root.chars(), put_old.chars());
|
||||
return Result<int>::from_syscall(rc);
|
||||
}
|
@ -6,14 +6,18 @@ Result<SharedPtr<VFS::Inode>> MountInode::create(SharedPtr<VFS::Inode> source, S
|
||||
{
|
||||
auto inode = TRY(adopt_shared_if_nonnull(new (std::nothrow) MountInode()));
|
||||
|
||||
inode->m_source = source;
|
||||
inode->m_mountee = fs;
|
||||
inode->m_mount_root_inode = fs->root_inode();
|
||||
|
||||
auto parent = TRY(source->find(".."));
|
||||
TRY(fs->set_mount_dir(parent));
|
||||
if (source)
|
||||
{
|
||||
inode->m_source = source;
|
||||
|
||||
source->add_handle();
|
||||
auto parent = TRY(source->find(".."));
|
||||
TRY(fs->set_mount_dir(parent));
|
||||
|
||||
source->add_handle();
|
||||
}
|
||||
|
||||
g_mounts.append(inode.ptr());
|
||||
|
||||
@ -22,5 +26,5 @@ Result<SharedPtr<VFS::Inode>> MountInode::create(SharedPtr<VFS::Inode> source, S
|
||||
|
||||
MountInode::~MountInode()
|
||||
{
|
||||
m_source->remove_handle();
|
||||
if (m_source) m_source->remove_handle();
|
||||
}
|
||||
|
@ -142,10 +142,27 @@ class MountInode : public VFS::Inode, public LinkedListNode<MountInode>
|
||||
return m_mount_root_inode->replace_entry(inode, name);
|
||||
}
|
||||
|
||||
Result<void> set_source(SharedPtr<VFS::Inode> source)
|
||||
{
|
||||
if (m_source) m_source->remove_handle();
|
||||
|
||||
m_source = source;
|
||||
|
||||
if (source)
|
||||
{
|
||||
auto parent = TRY(source->find(".."));
|
||||
TRY(m_mountee->set_mount_dir(parent));
|
||||
|
||||
source->add_handle();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual ~MountInode();
|
||||
|
||||
private:
|
||||
SharedPtr<VFS::Inode> m_source;
|
||||
SharedPtr<VFS::Inode> m_source {};
|
||||
SharedPtr<VFS::FileSystem> m_mountee;
|
||||
SharedPtr<VFS::Inode> m_mount_root_inode;
|
||||
|
||||
|
@ -8,11 +8,11 @@
|
||||
|
||||
namespace VFS
|
||||
{
|
||||
SharedPtr<FileSystem> root_fs;
|
||||
SharedPtr<VFS::Inode> g_root_inode = {};
|
||||
|
||||
Inode& root_inode()
|
||||
{
|
||||
return *root_fs->root_inode();
|
||||
return *g_root_inode;
|
||||
}
|
||||
|
||||
static constexpr int MAX_SYMLINKS = 8;
|
||||
@ -40,7 +40,7 @@ namespace VFS
|
||||
|
||||
SharedPtr<VFS::Inode> symlink_root;
|
||||
|
||||
if (PathParser::is_absolute(link.chars())) symlink_root = root_fs->root_inode();
|
||||
if (PathParser::is_absolute(link.chars())) symlink_root = g_root_inode;
|
||||
else
|
||||
symlink_root = parent_inode;
|
||||
|
||||
@ -60,7 +60,7 @@ namespace VFS
|
||||
{
|
||||
SharedPtr<Inode> current_inode;
|
||||
|
||||
if (PathParser::is_absolute(path) || !working_directory) current_inode = root_fs->root_inode();
|
||||
if (PathParser::is_absolute(path) || !working_directory) current_inode = g_root_inode;
|
||||
else
|
||||
current_inode = working_directory;
|
||||
|
||||
@ -180,7 +180,47 @@ namespace VFS
|
||||
|
||||
Result<void> mount_root(SharedPtr<VFS::FileSystem> fs)
|
||||
{
|
||||
root_fs = fs;
|
||||
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)
|
||||
{
|
||||
auto root_parser = TRY(PathParser::create(new_root));
|
||||
auto new_root_parent = TRY(root_parser.dirname());
|
||||
auto new_root_path = TRY(root_parser.basename());
|
||||
|
||||
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);
|
||||
|
||||
auto parser = TRY(PathParser::create(put_old));
|
||||
auto parent_path = TRY(parser.dirname());
|
||||
auto child = TRY(parser.basename());
|
||||
|
||||
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({});
|
||||
|
||||
return {};
|
||||
}
|
||||
@ -213,6 +253,8 @@ namespace VFS
|
||||
auto parent_path = TRY(parser.dirname());
|
||||
auto child = TRY(parser.basename());
|
||||
|
||||
if (child.view() == "/") return err(EBUSY);
|
||||
|
||||
kdbgln("vfs: Unmounting filesystem on target %s", path);
|
||||
|
||||
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
|
||||
|
@ -297,6 +297,7 @@ namespace VFS
|
||||
Inode& root_inode();
|
||||
|
||||
Result<void> mount_root(SharedPtr<VFS::FileSystem> fs);
|
||||
Result<void> pivot_root(const char* new_root, const char* put_old, SharedPtr<VFS::Inode> working_directory);
|
||||
Result<void> mount(const char* path, SharedPtr<VFS::FileSystem> fs, Credentials auth,
|
||||
SharedPtr<Inode> working_directory = {});
|
||||
|
||||
|
@ -36,3 +36,16 @@ Result<u64> sys_umount(Registers*, SyscallArgs args)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result<u64> sys_pivot_root(Registers*, SyscallArgs args)
|
||||
{
|
||||
auto new_root = TRY(MemoryManager::strdup_from_user(args[0]));
|
||||
auto put_old = TRY(MemoryManager::strdup_from_user(args[1]));
|
||||
|
||||
auto* current = Scheduler::current();
|
||||
if (current->auth.euid != 0) return err(EPERM);
|
||||
|
||||
TRY(VFS::pivot_root(new_root.chars(), put_old.chars(), current->current_directory));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -5,7 +5,8 @@
|
||||
_e(lseek) _e(mkdir) _e(execve) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) _e(geteuid) \
|
||||
_e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \
|
||||
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
|
||||
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat)
|
||||
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat) \
|
||||
_e(pivot_root)
|
||||
|
||||
enum Syscalls
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user