2018-10-10 09:53:07 +00:00
|
|
|
#include "VirtualFileSystem.h"
|
2018-11-07 10:37:54 +00:00
|
|
|
#include "FileDescriptor.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
#include "FileSystem.h"
|
2018-11-18 13:57:41 +00:00
|
|
|
#include <AK/FileSystemPath.h>
|
2018-10-28 11:20:25 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2018-10-12 22:44:54 +00:00
|
|
|
#include <AK/kmalloc.h>
|
2018-10-17 08:55:43 +00:00
|
|
|
#include <AK/kstdio.h>
|
|
|
|
#include <AK/ktime.h>
|
2018-10-30 12:59:29 +00:00
|
|
|
#include "CharacterDevice.h"
|
2018-11-07 20:19:47 +00:00
|
|
|
#include <LibC/errno_numbers.h>
|
2019-02-21 14:45:31 +00:00
|
|
|
#include <Kernel/Process.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
//#define VFS_DEBUG
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
static VFS* s_the;
|
2018-10-18 08:27:07 +00:00
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS& VFS::the()
|
2018-10-18 08:27:07 +00:00
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS::VFS()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-22 09:15:16 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: Constructing VFS\n");
|
2018-10-22 09:15:16 +00:00
|
|
|
#endif
|
2018-10-18 08:27:07 +00:00
|
|
|
s_the = this;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
VFS::~VFS()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-30 14:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 22:28:43 +00:00
|
|
|
InodeIdentifier VFS::root_inode_id() const
|
|
|
|
{
|
2019-01-16 11:57:07 +00:00
|
|
|
ASSERT(m_root_inode);
|
|
|
|
return m_root_inode->identifier();
|
2018-11-18 22:28:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
bool VFS::mount(RetainPtr<FS>&& file_system, const String& path)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-01-31 16:31:23 +00:00
|
|
|
ASSERT(file_system);
|
2018-10-28 13:11:51 +00:00
|
|
|
int error;
|
2019-02-25 19:47:56 +00:00
|
|
|
auto inode = old_resolve_path(path, root_inode_id(), error);
|
2018-12-02 23:20:00 +00:00
|
|
|
if (!inode.is_valid()) {
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", file_system->class_name(), file_system.ptr(), path.characters(), inode.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
// FIXME: check that this is not already a mount point
|
2019-01-31 16:31:23 +00:00
|
|
|
auto mount = make<Mount>(inode, move(file_system));
|
2018-10-17 09:40:58 +00:00
|
|
|
m_mounts.append(move(mount));
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
bool VFS::mount_root(RetainPtr<FS>&& file_system)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-01-16 11:57:07 +00:00
|
|
|
if (m_root_inode) {
|
2018-11-15 14:10:12 +00:00
|
|
|
kprintf("VFS: mount_root can't mount another root\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
auto mount = make<Mount>(InodeIdentifier(), move(file_system));
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-01-16 11:57:07 +00:00
|
|
|
auto root_inode_id = mount->guest().fs()->root_inode();
|
|
|
|
auto root_inode = mount->guest().fs()->get_inode(root_inode_id);
|
|
|
|
if (!root_inode->is_directory()) {
|
|
|
|
kprintf("VFS: root inode (%02u:%08u) for / is not a directory :(\n", root_inode_id.fsid(), root_inode_id.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-16 11:57:07 +00:00
|
|
|
m_root_inode = move(root_inode);
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-31 19:10:39 +00:00
|
|
|
kprintf("VFS: mounted root on %s{%p}\n",
|
2019-01-16 11:57:07 +00:00
|
|
|
m_root_inode->fs().class_name(),
|
|
|
|
&m_root_inode->fs());
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-17 09:40:58 +00:00
|
|
|
m_mounts.append(move(mount));
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:10:12 +00:00
|
|
|
auto VFS::find_mount_for_host(InodeIdentifier inode) -> Mount*
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
if (mount->host() == inode)
|
|
|
|
return mount.ptr();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:10:12 +00:00
|
|
|
auto VFS::find_mount_for_guest(InodeIdentifier inode) -> Mount*
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
if (mount->guest() == inode)
|
|
|
|
return mount.ptr();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:43:10 +00:00
|
|
|
bool VFS::is_vfs_root(InodeIdentifier inode) const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-01-16 11:57:07 +00:00
|
|
|
return inode == root_inode_id();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:18:28 +00:00
|
|
|
void VFS::traverse_directory_inode(Inode& dir_inode, Function<bool(const FS::DirectoryEntry&)> callback)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-11-15 16:13:10 +00:00
|
|
|
dir_inode.traverse_as_directory([&] (const FS::DirectoryEntry& entry) {
|
2019-01-31 16:31:23 +00:00
|
|
|
InodeIdentifier resolved_inode;
|
2018-11-15 14:10:12 +00:00
|
|
|
if (auto mount = find_mount_for_host(entry.inode))
|
2019-01-31 16:31:23 +00:00
|
|
|
resolved_inode = mount->guest();
|
2018-10-10 09:53:07 +00:00
|
|
|
else
|
2019-01-31 16:31:23 +00:00
|
|
|
resolved_inode = entry.inode;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-02 23:20:00 +00:00
|
|
|
if (dir_inode.identifier().is_root_inode() && !is_vfs_root(dir_inode.identifier()) && !strcmp(entry.name, "..")) {
|
2018-11-15 14:10:12 +00:00
|
|
|
auto mount = find_mount_for_guest(entry.inode);
|
2018-10-10 09:53:07 +00:00
|
|
|
ASSERT(mount);
|
2019-01-31 16:31:23 +00:00
|
|
|
resolved_inode = mount->host();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
callback(FS::DirectoryEntry(entry.name, entry.name_length, resolved_inode, entry.file_type));
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-06 21:14:31 +00:00
|
|
|
KResultOr<Retained<FileDescriptor>> VFS::open(RetainPtr<Device>&& device, int options)
|
2018-10-30 14:33:37 +00:00
|
|
|
{
|
2018-11-09 09:03:21 +00:00
|
|
|
// FIXME: Respect options.
|
2019-03-06 21:14:31 +00:00
|
|
|
(void)options;
|
2019-01-16 12:36:10 +00:00
|
|
|
return FileDescriptor::create(move(device));
|
2018-10-30 14:33:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
KResult VFS::utime(const String& path, Inode& base, time_t atime, time_t mtime)
|
2019-02-21 15:37:41 +00:00
|
|
|
{
|
2019-03-06 21:14:31 +00:00
|
|
|
auto descriptor_or_error = VFS::the().open(move(path), 0, 0, base);
|
|
|
|
if (descriptor_or_error.is_error())
|
|
|
|
return descriptor_or_error.error();
|
|
|
|
auto& inode = *descriptor_or_error.value()->inode();
|
2019-02-25 19:47:56 +00:00
|
|
|
if (inode.fs().is_readonly())
|
|
|
|
return KResult(-EROFS);
|
|
|
|
if (inode.metadata().uid != current->euid())
|
|
|
|
return KResult(-EACCES);
|
2019-03-06 21:14:31 +00:00
|
|
|
|
|
|
|
int error = inode.set_atime(atime);
|
2019-02-21 15:37:41 +00:00
|
|
|
if (error)
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(error);
|
2019-02-21 15:37:41 +00:00
|
|
|
error = inode.set_mtime(mtime);
|
|
|
|
if (error)
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(error);
|
|
|
|
return KSuccess;
|
2019-02-21 15:37:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:11:08 +00:00
|
|
|
KResult VFS::stat(const String& path, int options, Inode& base, struct stat& statbuf)
|
2019-02-21 15:09:12 +00:00
|
|
|
{
|
2019-03-01 23:11:08 +00:00
|
|
|
auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options);
|
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return inode_or_error.error();
|
|
|
|
return FileDescriptor::create(inode_or_error.value().ptr())->fstat(statbuf);
|
2019-02-21 15:09:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:14:31 +00:00
|
|
|
KResultOr<Retained<FileDescriptor>> VFS::open(const String& path, int options, mode_t mode, Inode& base)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-03-06 21:14:31 +00:00
|
|
|
auto inode_or_error = resolve_path_to_inode(path, base, nullptr, options);
|
2019-02-14 13:38:30 +00:00
|
|
|
if (options & O_CREAT) {
|
2019-03-06 21:14:31 +00:00
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return create(path, options, mode, base);
|
|
|
|
if (options & O_EXCL)
|
|
|
|
return KResult(-EEXIST);
|
2019-01-21 23:58:13 +00:00
|
|
|
}
|
2019-03-06 21:14:31 +00:00
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return inode_or_error.error();
|
2019-02-21 15:09:12 +00:00
|
|
|
|
2019-03-06 21:14:31 +00:00
|
|
|
auto metadata = inode_or_error.value()->metadata();
|
2019-02-21 15:09:12 +00:00
|
|
|
|
2019-02-21 14:45:31 +00:00
|
|
|
// NOTE: Read permission is a bit weird, since O_RDONLY == 0,
|
|
|
|
// so we check if (NOT write_only OR read_and_write)
|
|
|
|
if (!(options & O_WRONLY) || (options & O_RDWR)) {
|
2019-03-06 21:14:31 +00:00
|
|
|
if (!metadata.may_read(*current))
|
|
|
|
return KResult(-EACCES);
|
2019-02-21 14:45:31 +00:00
|
|
|
}
|
|
|
|
if ((options & O_WRONLY) || (options & O_RDWR)) {
|
2019-03-06 21:14:31 +00:00
|
|
|
if (!metadata.may_write(*current))
|
|
|
|
return KResult(-EACCES);
|
|
|
|
if (metadata.is_directory())
|
|
|
|
return KResult(-EISDIR);
|
2019-02-21 14:45:31 +00:00
|
|
|
}
|
2019-02-21 15:09:12 +00:00
|
|
|
|
|
|
|
if (metadata.is_device()) {
|
2019-02-15 23:47:20 +00:00
|
|
|
auto it = m_devices.find(encoded_device(metadata.major_device, metadata.minor_device));
|
|
|
|
if (it == m_devices.end()) {
|
|
|
|
kprintf("VFS::open: no such device %u,%u\n", metadata.major_device, metadata.minor_device);
|
2019-03-06 21:14:31 +00:00
|
|
|
return KResult(-ENODEV);
|
2019-01-16 11:57:07 +00:00
|
|
|
}
|
2019-03-20 01:55:12 +00:00
|
|
|
auto descriptor_or_error = (*it).value->open(options);
|
|
|
|
if (descriptor_or_error.is_error())
|
|
|
|
return descriptor_or_error.error();
|
|
|
|
descriptor_or_error.value()->set_original_inode(Badge<VFS>(), *inode_or_error.value());
|
|
|
|
return descriptor_or_error;
|
2019-01-16 11:57:07 +00:00
|
|
|
}
|
2019-03-06 21:14:31 +00:00
|
|
|
return FileDescriptor::create(*inode_or_error.value());
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:14:31 +00:00
|
|
|
KResultOr<Retained<FileDescriptor>> VFS::create(const String& path, int options, mode_t mode, Inode& base)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-03-06 21:14:31 +00:00
|
|
|
(void)options;
|
2019-01-23 03:29:56 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
|
2019-01-23 03:29:56 +00:00
|
|
|
// Turn it into a regular file. (This feels rather hackish.)
|
|
|
|
mode |= 0100000;
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:26:40 +00:00
|
|
|
RetainPtr<Inode> parent_inode;
|
2019-03-06 21:14:31 +00:00
|
|
|
auto existing_file_or_error = resolve_path_to_inode(path, base, &parent_inode);
|
|
|
|
if (!existing_file_or_error.is_error())
|
|
|
|
return KResult(-EEXIST);
|
|
|
|
if (!parent_inode)
|
|
|
|
return KResult(-ENOENT);
|
|
|
|
if (existing_file_or_error.error() != -ENOENT)
|
|
|
|
return existing_file_or_error.error();
|
|
|
|
if (!parent_inode->metadata().may_write(*current))
|
|
|
|
return KResult(-EACCES);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-02-21 12:26:40 +00:00
|
|
|
FileSystemPath p(path);
|
|
|
|
dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index());
|
2019-03-06 21:14:31 +00:00
|
|
|
int error;
|
2019-02-21 12:26:40 +00:00
|
|
|
auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), mode, 0, error);
|
2019-01-21 23:58:13 +00:00
|
|
|
if (!new_file)
|
2019-03-06 21:14:31 +00:00
|
|
|
return KResult(error);
|
2019-01-21 23:58:13 +00:00
|
|
|
|
|
|
|
return FileDescriptor::create(move(new_file));
|
2018-10-15 22:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
KResult VFS::mkdir(const String& path, mode_t mode, Inode& base)
|
2018-10-15 22:35:03 +00:00
|
|
|
{
|
2019-02-21 12:26:40 +00:00
|
|
|
RetainPtr<Inode> parent_inode;
|
2019-02-25 19:47:56 +00:00
|
|
|
auto result = resolve_path_to_inode(path, base, &parent_inode);
|
|
|
|
if (!result.is_error())
|
|
|
|
return KResult(-EEXIST);
|
|
|
|
if (!parent_inode)
|
|
|
|
return KResult(-ENOENT);
|
|
|
|
if (result.error() != -ENOENT)
|
|
|
|
return result.error();
|
|
|
|
|
|
|
|
if (!parent_inode->metadata().may_write(*current))
|
|
|
|
return KResult(-EACCES);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-02-21 12:26:40 +00:00
|
|
|
FileSystemPath p(path);
|
|
|
|
dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_inode->fsid(), parent_inode->index());
|
2019-02-25 19:47:56 +00:00
|
|
|
int error;
|
2019-02-21 12:26:40 +00:00
|
|
|
auto new_dir = parent_inode->fs().create_directory(parent_inode->identifier(), p.basename(), mode, error);
|
2019-02-25 19:47:56 +00:00
|
|
|
if (new_dir)
|
|
|
|
return KSuccess;
|
|
|
|
return KResult(error);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 14:57:59 +00:00
|
|
|
KResult VFS::access(const String& path, int mode, Inode& base)
|
|
|
|
{
|
|
|
|
auto inode_or_error = resolve_path_to_inode(path, base);
|
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return inode_or_error.error();
|
|
|
|
auto inode = inode_or_error.value();
|
|
|
|
auto metadata = inode->metadata();
|
|
|
|
if (mode & R_OK) {
|
|
|
|
if (!metadata.may_read(*current))
|
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
if (mode & W_OK) {
|
|
|
|
if (!metadata.may_write(*current))
|
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
if (mode & X_OK) {
|
|
|
|
if (!metadata.may_execute(*current))
|
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:54:07 +00:00
|
|
|
KResultOr<Retained<Inode>> VFS::open_directory(const String& path, Inode& base)
|
|
|
|
{
|
|
|
|
auto inode_or_error = resolve_path_to_inode(path, base);
|
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return inode_or_error.error();
|
|
|
|
auto inode = inode_or_error.value();
|
|
|
|
if (!inode->is_directory())
|
|
|
|
return KResult(-ENOTDIR);
|
2019-03-03 11:34:54 +00:00
|
|
|
if (!inode->metadata().may_execute(*current))
|
2019-03-01 22:54:07 +00:00
|
|
|
return KResult(-EACCES);
|
|
|
|
return Retained<Inode>(*inode);
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:39:19 +00:00
|
|
|
KResult VFS::chmod(Inode& inode, mode_t mode)
|
2019-01-29 03:55:08 +00:00
|
|
|
{
|
2019-03-01 09:39:19 +00:00
|
|
|
if (inode.fs().is_readonly())
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-EROFS);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-03-01 09:39:19 +00:00
|
|
|
if (current->euid() != inode.metadata().uid && !current->is_superuser())
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-EPERM);
|
2019-01-29 03:55:08 +00:00
|
|
|
|
|
|
|
// Only change the permission bits.
|
2019-03-01 09:39:19 +00:00
|
|
|
mode = (inode.mode() & ~04777u) | (mode & 04777u);
|
|
|
|
return inode.chmod(mode);
|
|
|
|
}
|
2019-01-29 03:55:08 +00:00
|
|
|
|
2019-03-01 09:39:19 +00:00
|
|
|
KResult VFS::chmod(const String& path, mode_t mode, Inode& base)
|
|
|
|
{
|
|
|
|
auto inode_or_error = resolve_path_to_inode(path, base);
|
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return inode_or_error.error();
|
|
|
|
auto inode = inode_or_error.value();
|
|
|
|
return chmod(*inode, mode);
|
2019-02-25 19:47:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:32:53 +00:00
|
|
|
KResult VFS::chown(const String& path, uid_t a_uid, gid_t a_gid, Inode& base)
|
|
|
|
{
|
|
|
|
auto inode_or_error = resolve_path_to_inode(path, base);
|
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return inode_or_error.error();
|
|
|
|
auto inode = inode_or_error.value();
|
|
|
|
|
|
|
|
if (inode->fs().is_readonly())
|
|
|
|
return KResult(-EROFS);
|
|
|
|
|
|
|
|
if (current->euid() != inode->metadata().uid && !current->is_superuser())
|
|
|
|
return KResult(-EPERM);
|
|
|
|
|
|
|
|
uid_t new_uid = inode->metadata().uid;
|
|
|
|
gid_t new_gid = inode->metadata().gid;
|
|
|
|
|
|
|
|
if (a_uid != (uid_t)-1) {
|
|
|
|
if (current->euid() != a_uid && !current->is_superuser())
|
|
|
|
return KResult(-EPERM);
|
|
|
|
new_uid = a_uid;
|
|
|
|
}
|
|
|
|
if (a_gid != (gid_t)-1) {
|
|
|
|
if (!current->in_group(a_gid) && !current->is_superuser())
|
|
|
|
return KResult(-EPERM);
|
|
|
|
new_gid = a_gid;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgprintf("VFS::chown(): inode %u:%u <- uid:%d, gid:%d\n", inode->fsid(), inode->index(), new_uid, new_gid);
|
|
|
|
return inode->chown(new_uid, new_gid);
|
|
|
|
}
|
|
|
|
|
2019-03-01 23:11:08 +00:00
|
|
|
KResultOr<Retained<Inode>> VFS::resolve_path_to_inode(const String& path, Inode& base, RetainPtr<Inode>* parent_inode, int options)
|
2019-02-25 19:47:56 +00:00
|
|
|
{
|
|
|
|
// FIXME: This won't work nicely across mount boundaries.
|
|
|
|
FileSystemPath p(path);
|
|
|
|
if (!p.is_valid())
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
InodeIdentifier parent_id;
|
2019-03-01 23:11:08 +00:00
|
|
|
auto result = resolve_path(path, base.identifier(), options, &parent_id);
|
2019-02-25 19:47:56 +00:00
|
|
|
if (parent_inode && parent_id.is_valid())
|
|
|
|
*parent_inode = get_inode(parent_id);
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
2019-03-01 23:11:08 +00:00
|
|
|
return Retained<Inode>(*get_inode(result.value()));
|
2019-01-29 03:55:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 14:31:26 +00:00
|
|
|
KResult VFS::link(const String& old_path, const String& new_path, Inode& base)
|
2019-02-21 12:26:40 +00:00
|
|
|
{
|
2019-02-27 14:31:26 +00:00
|
|
|
auto old_inode_or_error = resolve_path_to_inode(old_path, base);
|
|
|
|
if (old_inode_or_error.is_error())
|
|
|
|
return old_inode_or_error.error();
|
|
|
|
auto old_inode = old_inode_or_error.value();
|
2019-02-21 12:26:40 +00:00
|
|
|
|
|
|
|
RetainPtr<Inode> parent_inode;
|
2019-02-27 14:31:26 +00:00
|
|
|
auto new_inode_or_error = resolve_path_to_inode(new_path, base, &parent_inode);
|
|
|
|
if (!new_inode_or_error.is_error())
|
|
|
|
return KResult(-EEXIST);
|
2019-01-22 06:03:44 +00:00
|
|
|
|
2019-02-27 14:31:26 +00:00
|
|
|
if (!parent_inode)
|
|
|
|
return KResult(-ENOENT);
|
|
|
|
|
|
|
|
if (parent_inode->fsid() != old_inode->fsid())
|
|
|
|
return KResult(-EXDEV);
|
|
|
|
|
|
|
|
if (parent_inode->fs().is_readonly())
|
|
|
|
return KResult(-EROFS);
|
|
|
|
|
|
|
|
if (!parent_inode->metadata().may_write(*current))
|
|
|
|
return KResult(-EACCES);
|
|
|
|
|
|
|
|
return parent_inode->add_child(old_inode->identifier(), FileSystemPath(new_path).basename(), 0);
|
2019-02-21 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
KResult VFS::unlink(const String& path, Inode& base)
|
2019-02-21 12:26:40 +00:00
|
|
|
{
|
|
|
|
RetainPtr<Inode> parent_inode;
|
2019-02-27 13:11:25 +00:00
|
|
|
auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode);
|
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return inode_or_error.error();
|
|
|
|
auto inode = inode_or_error.value();
|
2019-01-23 04:35:42 +00:00
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
if (inode->is_directory())
|
|
|
|
return KResult(-EISDIR);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
if (!parent_inode->metadata().may_write(*current))
|
|
|
|
return KResult(-EACCES);
|
2019-01-22 06:03:44 +00:00
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
return parent_inode->remove_child(FileSystemPath(path).basename());
|
2019-01-22 06:03:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 00:50:34 +00:00
|
|
|
KResult VFS::symlink(const String& target, const String& linkpath, Inode& base)
|
|
|
|
{
|
|
|
|
RetainPtr<Inode> parent_inode;
|
|
|
|
auto existing_file_or_error = resolve_path_to_inode(linkpath, base, &parent_inode);
|
|
|
|
if (!existing_file_or_error.is_error())
|
|
|
|
return KResult(-EEXIST);
|
|
|
|
if (!parent_inode)
|
|
|
|
return KResult(-ENOENT);
|
|
|
|
if (existing_file_or_error.error() != -ENOENT)
|
|
|
|
return existing_file_or_error.error();
|
|
|
|
if (!parent_inode->metadata().may_write(*current))
|
|
|
|
return KResult(-EACCES);
|
|
|
|
|
|
|
|
FileSystemPath p(linkpath);
|
|
|
|
dbgprintf("VFS::symlink: '%s' (-> '%s') in %u:%u\n", p.basename().characters(), target.characters(), parent_inode->fsid(), parent_inode->index());
|
|
|
|
int error;
|
|
|
|
auto new_file = parent_inode->fs().create_inode(parent_inode->identifier(), p.basename(), 0120644, 0, error);
|
|
|
|
if (!new_file)
|
|
|
|
return KResult(error);
|
|
|
|
ssize_t nwritten = new_file->write_bytes(0, target.length(), (const byte*)target.characters(), nullptr);
|
|
|
|
if (nwritten < 0)
|
|
|
|
return KResult(nwritten);
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
KResult VFS::rmdir(const String& path, Inode& base)
|
2019-01-28 03:16:01 +00:00
|
|
|
{
|
2019-02-21 12:26:40 +00:00
|
|
|
RetainPtr<Inode> parent_inode;
|
2019-02-27 13:11:25 +00:00
|
|
|
auto inode_or_error = resolve_path_to_inode(path, base, &parent_inode);
|
|
|
|
if (inode_or_error.is_error())
|
|
|
|
return KResult(inode_or_error.error());
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
auto inode = inode_or_error.value();
|
|
|
|
if (inode->fs().is_readonly())
|
|
|
|
return KResult(-EROFS);
|
2019-01-28 03:16:01 +00:00
|
|
|
|
|
|
|
// FIXME: We should return EINVAL if the last component of the path is "."
|
|
|
|
// FIXME: We should return ENOTEMPTY if the last component of the path is ".."
|
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
if (!inode->is_directory())
|
|
|
|
return KResult(-ENOTDIR);
|
2019-02-21 14:45:31 +00:00
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
if (!parent_inode->metadata().may_write(*current))
|
|
|
|
return KResult(-EACCES);
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
if (inode->directory_entry_count() != 2)
|
|
|
|
return KResult(-ENOTEMPTY);
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
auto result = inode->remove_child(".");
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-01-28 03:16:01 +00:00
|
|
|
|
2019-02-27 13:11:25 +00:00
|
|
|
result = inode->remove_child("..");
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-01-28 03:16:01 +00:00
|
|
|
|
|
|
|
// FIXME: The reverse_lookup here can definitely be avoided.
|
2019-02-27 13:11:25 +00:00
|
|
|
return parent_inode->remove_child(parent_inode->reverse_lookup(inode->identifier()));
|
2019-01-28 03:16:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
KResultOr<InodeIdentifier> VFS::resolve_symbolic_link(InodeIdentifier base, Inode& symlink_inode)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-12-21 16:45:42 +00:00
|
|
|
auto symlink_contents = symlink_inode.read_entire();
|
|
|
|
if (!symlink_contents)
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-ENOENT);
|
2018-12-21 16:45:42 +00:00
|
|
|
auto linkee = String((const char*)symlink_contents.pointer(), symlink_contents.size());
|
2018-10-29 20:54:11 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-11-15 14:10:12 +00:00
|
|
|
kprintf("linkee (%s)(%u) from %u:%u\n", linkee.characters(), linkee.length(), base.fsid(), base.index());
|
2018-10-29 20:54:11 +00:00
|
|
|
#endif
|
2019-02-25 19:47:56 +00:00
|
|
|
return resolve_path(linkee, base);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:18:28 +00:00
|
|
|
RetainPtr<Inode> VFS::get_inode(InodeIdentifier inode_id)
|
2018-10-28 11:20:25 +00:00
|
|
|
{
|
2018-12-02 23:20:00 +00:00
|
|
|
if (!inode_id.is_valid())
|
2018-11-13 22:44:54 +00:00
|
|
|
return nullptr;
|
2018-12-02 23:20:00 +00:00
|
|
|
return inode_id.fs()->get_inode(inode_id);
|
2018-11-13 22:44:54 +00:00
|
|
|
}
|
2019-01-31 05:13:55 +00:00
|
|
|
|
2019-03-06 21:30:13 +00:00
|
|
|
KResultOr<String> VFS::absolute_path(InodeIdentifier inode_id)
|
2019-01-31 05:13:55 +00:00
|
|
|
{
|
|
|
|
auto inode = get_inode(inode_id);
|
|
|
|
if (!inode)
|
2019-03-06 21:30:13 +00:00
|
|
|
return KResult(-EIO);
|
2019-01-31 05:13:55 +00:00
|
|
|
return absolute_path(*inode);
|
|
|
|
}
|
2018-10-28 11:20:25 +00:00
|
|
|
|
2019-03-06 21:30:13 +00:00
|
|
|
KResultOr<String> VFS::absolute_path(Inode& core_inode)
|
2018-11-13 22:44:54 +00:00
|
|
|
{
|
2018-10-28 11:20:25 +00:00
|
|
|
Vector<InodeIdentifier> lineage;
|
2018-12-19 20:18:28 +00:00
|
|
|
RetainPtr<Inode> inode = &core_inode;
|
2019-01-16 11:57:07 +00:00
|
|
|
while (inode->identifier() != root_inode_id()) {
|
2018-11-15 14:10:12 +00:00
|
|
|
if (auto* mount = find_mount_for_guest(inode->identifier()))
|
2018-10-28 11:20:25 +00:00
|
|
|
lineage.append(mount->host());
|
|
|
|
else
|
2018-11-13 22:44:54 +00:00
|
|
|
lineage.append(inode->identifier());
|
|
|
|
|
|
|
|
InodeIdentifier parent_id;
|
|
|
|
if (inode->is_directory()) {
|
2019-03-06 21:30:13 +00:00
|
|
|
auto result = resolve_path("..", inode->identifier());
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
parent_id = result.value();
|
2018-11-13 22:44:54 +00:00
|
|
|
} else {
|
2019-01-04 17:37:58 +00:00
|
|
|
parent_id = inode->parent()->identifier();
|
2018-11-13 22:44:54 +00:00
|
|
|
}
|
2019-03-06 21:30:13 +00:00
|
|
|
if (!parent_id.is_valid())
|
|
|
|
return KResult(-EIO);
|
2018-11-13 22:44:54 +00:00
|
|
|
inode = get_inode(parent_id);
|
2018-10-28 11:20:25 +00:00
|
|
|
}
|
2018-12-21 01:10:45 +00:00
|
|
|
if (lineage.is_empty())
|
2018-10-28 11:20:25 +00:00
|
|
|
return "/";
|
2019-01-16 11:57:07 +00:00
|
|
|
lineage.append(root_inode_id());
|
2018-10-28 11:20:25 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
for (size_t i = lineage.size() - 1; i >= 1; --i) {
|
|
|
|
auto& child = lineage[i - 1];
|
|
|
|
auto parent = lineage[i];
|
2018-11-15 14:10:12 +00:00
|
|
|
if (auto* mount = find_mount_for_host(parent))
|
2018-10-28 11:20:25 +00:00
|
|
|
parent = mount->guest();
|
|
|
|
builder.append('/');
|
2018-11-15 16:04:55 +00:00
|
|
|
auto parent_inode = get_inode(parent);
|
|
|
|
builder.append(parent_inode->reverse_lookup(child));
|
2018-10-28 11:20:25 +00:00
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
return builder.to_string();
|
2018-10-28 11:20:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
KResultOr<InodeIdentifier> VFS::resolve_path(const String& path, InodeIdentifier base, int options, InodeIdentifier* parent_id)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-02-25 19:47:56 +00:00
|
|
|
if (path.is_empty())
|
|
|
|
return KResult(-EINVAL);
|
2018-10-26 12:24:11 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
auto parts = path.split('/');
|
2018-11-15 14:36:35 +00:00
|
|
|
InodeIdentifier crumb_id;
|
2018-10-26 12:24:11 +00:00
|
|
|
|
|
|
|
if (path[0] == '/')
|
2019-01-16 11:57:07 +00:00
|
|
|
crumb_id = root_inode_id();
|
2018-10-26 12:24:11 +00:00
|
|
|
else
|
2019-01-16 11:57:07 +00:00
|
|
|
crumb_id = base.is_valid() ? base : root_inode_id();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-01-28 03:16:01 +00:00
|
|
|
if (parent_id)
|
|
|
|
*parent_id = crumb_id;
|
2018-11-18 22:28:43 +00:00
|
|
|
|
2019-02-25 21:06:55 +00:00
|
|
|
for (int i = 0; i < parts.size(); ++i) {
|
2018-12-02 23:20:00 +00:00
|
|
|
bool inode_was_root_at_head_of_loop = crumb_id.is_root_inode();
|
2018-10-10 09:53:07 +00:00
|
|
|
auto& part = parts[i];
|
2018-12-21 01:10:45 +00:00
|
|
|
if (part.is_empty())
|
2018-11-10 14:40:24 +00:00
|
|
|
break;
|
2018-12-21 16:16:53 +00:00
|
|
|
auto crumb_inode = get_inode(crumb_id);
|
2018-12-21 16:28:16 +00:00
|
|
|
if (!crumb_inode) {
|
2018-10-10 09:53:07 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("invalid metadata\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-EIO);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2018-12-21 16:28:16 +00:00
|
|
|
auto metadata = crumb_inode->metadata();
|
2019-01-31 16:31:23 +00:00
|
|
|
if (!metadata.is_directory()) {
|
2018-10-10 09:53:07 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2019-02-06 16:27:51 +00:00
|
|
|
kprintf("parent of <%s> not directory, it's inode %u:%u / %u:%u, mode: %u, size: %u\n", part.characters(), crumb_id.fsid(), crumb_id.index(), metadata.inode.fsid(), metadata.inode.index(), metadata.mode, metadata.size);
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-ENOTDIR);
|
2019-02-21 14:45:31 +00:00
|
|
|
}
|
2019-03-03 11:34:54 +00:00
|
|
|
if (!metadata.may_execute(*current))
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-EACCES);
|
2018-11-15 14:36:35 +00:00
|
|
|
auto parent = crumb_id;
|
2018-12-21 16:16:53 +00:00
|
|
|
crumb_id = crumb_inode->lookup(part);
|
2018-12-02 23:20:00 +00:00
|
|
|
if (!crumb_id.is_valid()) {
|
2018-10-10 09:53:07 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-11-15 14:10:12 +00:00
|
|
|
kprintf("child <%s>(%u) not found in directory, %02u:%08u\n", part.characters(), part.length(), parent.fsid(), parent.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2019-02-25 19:47:56 +00:00
|
|
|
return KResult(-ENOENT);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
#ifdef VFS_DEBUG
|
2019-02-06 16:27:51 +00:00
|
|
|
kprintf("<%s> %u:%u\n", part.characters(), crumb_id.fsid(), crumb_id.index());
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2018-11-15 14:36:35 +00:00
|
|
|
if (auto mount = find_mount_for_host(crumb_id)) {
|
2018-10-10 09:53:07 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf(" -- is host\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2018-11-15 14:36:35 +00:00
|
|
|
crumb_id = mount->guest();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2018-12-02 23:20:00 +00:00
|
|
|
if (inode_was_root_at_head_of_loop && crumb_id.is_root_inode() && !is_vfs_root(crumb_id) && part == "..") {
|
2018-10-10 09:53:07 +00:00
|
|
|
#ifdef VFS_DEBUG
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf(" -- is guest\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
#endif
|
2018-11-15 14:36:35 +00:00
|
|
|
auto mount = find_mount_for_guest(crumb_id);
|
2018-11-15 15:34:36 +00:00
|
|
|
auto dir_inode = get_inode(mount->host());
|
2019-02-15 08:07:29 +00:00
|
|
|
ASSERT(dir_inode);
|
2018-11-15 15:34:36 +00:00
|
|
|
crumb_id = dir_inode->lookup("..");
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2018-12-21 16:16:53 +00:00
|
|
|
crumb_inode = get_inode(crumb_id);
|
2019-02-15 08:07:29 +00:00
|
|
|
ASSERT(crumb_inode);
|
2018-12-21 16:16:53 +00:00
|
|
|
metadata = crumb_inode->metadata();
|
2019-01-31 16:31:23 +00:00
|
|
|
if (metadata.is_directory()) {
|
2019-01-28 03:16:01 +00:00
|
|
|
if (i != parts.size() - 1) {
|
|
|
|
if (parent_id)
|
|
|
|
*parent_id = crumb_id;
|
|
|
|
}
|
2018-11-18 22:28:43 +00:00
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
if (metadata.is_symlink()) {
|
2018-10-28 13:11:51 +00:00
|
|
|
if (i == parts.size() - 1) {
|
2019-02-25 19:47:56 +00:00
|
|
|
if (options & O_NOFOLLOW)
|
|
|
|
return KResult(-ELOOP);
|
2018-10-28 13:11:51 +00:00
|
|
|
if (options & O_NOFOLLOW_NOERROR)
|
2018-11-15 14:36:35 +00:00
|
|
|
return crumb_id;
|
2018-10-28 13:11:51 +00:00
|
|
|
}
|
2019-02-25 19:47:56 +00:00
|
|
|
auto result = resolve_symbolic_link(parent, *crumb_inode);
|
|
|
|
if (result.is_error())
|
|
|
|
return KResult(-ENOENT);
|
|
|
|
crumb_id = result.value();
|
|
|
|
ASSERT(crumb_id.is_valid());
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:36:35 +00:00
|
|
|
return crumb_id;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
InodeIdentifier VFS::old_resolve_path(const String& path, InodeIdentifier base, int& error, int options, InodeIdentifier* parent_id)
|
|
|
|
{
|
|
|
|
auto result = resolve_path(path, base, options, parent_id);
|
|
|
|
if (result.is_error()) {
|
|
|
|
error = result.error();
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
return result.value();
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:13:10 +00:00
|
|
|
VFS::Mount::Mount(InodeIdentifier host, RetainPtr<FS>&& guest_fs)
|
2018-10-10 09:53:07 +00:00
|
|
|
: m_host(host)
|
2018-12-02 23:20:00 +00:00
|
|
|
, m_guest(guest_fs->root_inode())
|
2018-11-15 14:36:35 +00:00
|
|
|
, m_guest_fs(move(guest_fs))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-15 23:47:20 +00:00
|
|
|
void VFS::register_device(Device& device)
|
2018-10-14 00:59:22 +00:00
|
|
|
{
|
2019-02-15 23:47:20 +00:00
|
|
|
m_devices.set(encoded_device(device.major(), device.minor()), &device);
|
2018-10-14 00:59:22 +00:00
|
|
|
}
|
2018-10-26 16:43:25 +00:00
|
|
|
|
2019-02-15 23:47:20 +00:00
|
|
|
void VFS::unregister_device(Device& device)
|
2019-01-30 17:26:19 +00:00
|
|
|
{
|
2019-02-15 23:47:20 +00:00
|
|
|
m_devices.remove(encoded_device(device.major(), device.minor()));
|
2019-01-30 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 23:47:20 +00:00
|
|
|
Device* VFS::get_device(unsigned major, unsigned minor)
|
2019-01-31 04:55:30 +00:00
|
|
|
{
|
2019-02-15 23:47:20 +00:00
|
|
|
auto it = m_devices.find(encoded_device(major, minor));
|
|
|
|
if (it == m_devices.end())
|
2019-01-31 04:55:30 +00:00
|
|
|
return nullptr;
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2018-11-15 14:10:12 +00:00
|
|
|
void VFS::for_each_mount(Function<void(const Mount&)> callback) const
|
2018-10-26 16:43:25 +00:00
|
|
|
{
|
|
|
|
for (auto& mount : m_mounts) {
|
|
|
|
callback(*mount);
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 23:39:29 +00:00
|
|
|
|
|
|
|
void VFS::sync()
|
|
|
|
{
|
|
|
|
FS::sync();
|
|
|
|
}
|