From 874bffc729ab89a5384ffe4b38bac8b37cc810be Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 30 May 2019 21:29:26 +0200 Subject: [PATCH] FileSystem: Get rid of VFS::absolute_path() and teach Mount about custodies. --- Kernel/FileSystem/FileDescriptor.cpp | 2 +- Kernel/FileSystem/FileDescriptor.h | 2 +- Kernel/FileSystem/InodeFile.cpp | 9 ++- Kernel/FileSystem/ProcFS.cpp | 32 ++--------- Kernel/FileSystem/VirtualFileSystem.cpp | 76 +++++++------------------ Kernel/FileSystem/VirtualFileSystem.h | 16 +++--- 6 files changed, 40 insertions(+), 97 deletions(-) diff --git a/Kernel/FileSystem/FileDescriptor.cpp b/Kernel/FileSystem/FileDescriptor.cpp index 54e3c7d57cc..49862fee250 100644 --- a/Kernel/FileSystem/FileDescriptor.cpp +++ b/Kernel/FileSystem/FileDescriptor.cpp @@ -261,7 +261,7 @@ int FileDescriptor::close() return 0; } -KResultOr FileDescriptor::absolute_path() +String FileDescriptor::absolute_path() { if (m_custody) return m_custody->absolute_path(); diff --git a/Kernel/FileSystem/FileDescriptor.h b/Kernel/FileSystem/FileDescriptor.h index e9844f2a59a..cf5f9891eee 100644 --- a/Kernel/FileSystem/FileDescriptor.h +++ b/Kernel/FileSystem/FileDescriptor.h @@ -43,7 +43,7 @@ public: ByteBuffer read_entire_file(); - KResultOr absolute_path(); + String absolute_path(); bool is_directory() const; diff --git a/Kernel/FileSystem/InodeFile.cpp b/Kernel/FileSystem/InodeFile.cpp index 620e90ff663..80a25e53012 100644 --- a/Kernel/FileSystem/InodeFile.cpp +++ b/Kernel/FileSystem/InodeFile.cpp @@ -41,12 +41,11 @@ KResultOr InodeFile::mmap(Process& process, LinearAddress preferred_lad return region; } -String InodeFile::absolute_path(FileDescriptor&) const +String InodeFile::absolute_path(FileDescriptor& descriptor) const { - auto path_or_error = VFS::the().absolute_path(const_cast(inode())); - if (path_or_error.is_error()) - return { }; - return path_or_error.value(); + ASSERT_NOT_REACHED(); + ASSERT(descriptor.custody()); + return descriptor.absolute_path(); } KResult InodeFile::truncate(off_t size) diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index e0bc81631e3..3f71e7b206d 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -192,10 +192,7 @@ ByteBuffer procfs$pid_fds(InodeIdentifier identifier) auto* descriptor = process.file_descriptor(i); if (!descriptor) continue; - auto result = descriptor->absolute_path(); - if (result.is_error()) - continue; - builder.appendf("% 3u %s\n", i, result.value().characters()); + builder.appendf("% 3u %s\n", i, descriptor->absolute_path().characters()); } return builder.to_byte_buffer(); } @@ -210,10 +207,7 @@ ByteBuffer procfs$pid_fd_entry(InodeIdentifier identifier) auto* descriptor = process.file_descriptor(fd); if (!descriptor) return { }; - auto result = descriptor->absolute_path(); - if (result.is_error()) - return { }; - return result.value().to_byte_buffer(); + return descriptor->absolute_path().to_byte_buffer(); } ByteBuffer procfs$pid_vm(InodeIdentifier identifier) @@ -420,11 +414,7 @@ ByteBuffer procfs$mounts(InodeIdentifier) else { builder.appendf("%u:%u", mount.host().fsid(), mount.host().index()); builder.append(' '); - auto result = VFS::the().absolute_path(mount.host()); - if (result.is_error()) - builder.append("[error]"); - else - builder.append(result.value()); + builder.append(mount.absolute_path()); } builder.append('\n'); }); @@ -442,15 +432,7 @@ ByteBuffer procfs$df(InodeIdentifier) builder.appendf("%u,", fs.free_block_count()); builder.appendf("%u,", fs.total_inode_count()); builder.appendf("%u,", fs.free_inode_count()); - if (!mount.host().is_valid()) - builder.append("/"); - else { - auto result = VFS::the().absolute_path(mount.host()); - if (result.is_error()) - builder.append("[Error]"); - else - builder.append(result.value()); - } + builder.append(mount.absolute_path()); builder.append('\n'); }); return builder.to_byte_buffer(); @@ -613,11 +595,7 @@ ByteBuffer procfs$inodes(InodeIdentifier) StringBuilder builder; for (auto it : all_inodes()) { RetainPtr inode = *it; - auto result = VFS::the().absolute_path(*inode); - if (result.is_error()) - continue; - auto path = result.value(); - builder.appendf("Inode{K%x} %02u:%08u (%u) %s\n", inode.ptr(), inode->fsid(), inode->index(), inode->retain_count(), path.characters()); + builder.appendf("Inode{K%x} %02u:%08u (%u)\n", inode.ptr(), inode->fsid(), inode->index(), inode->retain_count()); } return builder.to_byte_buffer(); } diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index adde3534048..6ee9d652dd7 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -36,9 +36,8 @@ InodeIdentifier VFS::root_inode_id() const return m_root_inode->identifier(); } -bool VFS::mount(RetainPtr&& file_system, StringView path) +bool VFS::mount(Retained&& file_system, StringView path) { - ASSERT(file_system); auto result = resolve_path_to_custody(path, root_custody()); if (result.is_error()) { kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters()); @@ -47,19 +46,19 @@ bool VFS::mount(RetainPtr&& file_system, StringView path) auto& inode = result.value()->inode(); kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", file_system->class_name(), file_system.ptr(), path.characters(), inode.index()); // FIXME: check that this is not already a mount point - auto mount = make(inode.identifier(), move(file_system)); + auto mount = make(*result.value(), move(file_system)); m_mounts.append(move(mount)); return true; } -bool VFS::mount_root(RetainPtr&& file_system) +bool VFS::mount_root(Retained&& file_system) { if (m_root_inode) { kprintf("VFS: mount_root can't mount another root\n"); return false; } - auto mount = make(InodeIdentifier(), move(file_system)); + auto mount = make(nullptr, move(file_system)); auto root_inode_id = mount->guest().fs()->root_inode(); auto root_inode = mount->guest().fs()->get_inode(root_inode_id); @@ -565,53 +564,6 @@ RetainPtr VFS::get_inode(InodeIdentifier inode_id) return inode_id.fs()->get_inode(inode_id); } -KResultOr VFS::absolute_path(InodeIdentifier inode_id) -{ - auto inode = get_inode(inode_id); - if (!inode) - return KResult(-EIO); - return absolute_path(*inode); -} - -KResultOr VFS::absolute_path(Inode& core_inode) -{ - Vector lineage; - RetainPtr inode = &core_inode; - while (inode->identifier() != root_inode_id()) { - if (auto* mount = find_mount_for_guest(inode->identifier())) - lineage.append(mount->host()); - else - lineage.append(inode->identifier()); - - InodeIdentifier parent_id; - if (inode->is_directory()) { - auto result = resolve_path("..", inode->identifier()); - if (result.is_error()) - return result.error(); - parent_id = result.value(); - } else { - parent_id = inode->parent()->identifier(); - } - if (!parent_id.is_valid()) - return KResult(-EIO); - inode = get_inode(parent_id); - } - if (lineage.is_empty()) - return "/"; - lineage.append(root_inode_id()); - StringBuilder builder; - for (size_t i = lineage.size() - 1; i >= 1; --i) { - auto& child = lineage[i - 1]; - auto parent = lineage[i]; - if (auto* mount = find_mount_for_host(parent)) - parent = mount->guest(); - builder.append('/'); - auto parent_inode = get_inode(parent); - builder.append(parent_inode->reverse_lookup(child)); - } - return builder.to_string(); -} - KResultOr VFS::resolve_path(StringView path, InodeIdentifier base, int options, InodeIdentifier* parent_id) { if (path.is_empty()) @@ -701,13 +653,27 @@ KResultOr VFS::resolve_path(StringView path, InodeIdentifier ba return crumb_id; } -VFS::Mount::Mount(InodeIdentifier host, RetainPtr&& guest_fs) - : m_host(host) - , m_guest(guest_fs->root_inode()) +VFS::Mount::Mount(RetainPtr&& host_custody, Retained&& guest_fs) + : m_guest(guest_fs->root_inode()) , m_guest_fs(move(guest_fs)) + , m_host_custody(move(host_custody)) { } +String VFS::Mount::absolute_path() const +{ + if (!m_host_custody) + return "/"; + return m_host_custody->absolute_path(); +} + +InodeIdentifier VFS::Mount::host() const +{ + if (!m_host_custody) + return { }; + return m_host_custody->inode().identifier(); +} + void VFS::register_device(Device& device) { m_devices.set(encoded_device(device.major(), device.minor()), &device); diff --git a/Kernel/FileSystem/VirtualFileSystem.h b/Kernel/FileSystem/VirtualFileSystem.h index cfe4daed083..375d11947a6 100644 --- a/Kernel/FileSystem/VirtualFileSystem.h +++ b/Kernel/FileSystem/VirtualFileSystem.h @@ -41,17 +41,20 @@ class VFS { public: class Mount { public: - Mount(InodeIdentifier host, RetainPtr&&); + Mount(RetainPtr&&, Retained&&); - InodeIdentifier host() const { return m_host; } + InodeIdentifier host() const; InodeIdentifier guest() const { return m_guest; } const FS& guest_fs() const { return *m_guest_fs; } + String absolute_path() const; + private: InodeIdentifier m_host; InodeIdentifier m_guest; - RetainPtr m_guest_fs; + Retained m_guest_fs; + RetainPtr m_host_custody; }; [[gnu::pure]] static VFS& the(); @@ -59,8 +62,8 @@ public: VFS(); ~VFS(); - bool mount_root(RetainPtr&&); - bool mount(RetainPtr&&, StringView path); + bool mount_root(Retained&&); + bool mount(Retained&&, StringView path); KResultOr> open(RetainPtr&&, int options); KResultOr> open(StringView path, int options, mode_t mode, Custody& base); @@ -86,9 +89,6 @@ public: size_t mount_count() const { return m_mounts.size(); } void for_each_mount(Function) const; - KResultOr absolute_path(Inode&); - KResultOr absolute_path(InodeIdentifier); - InodeIdentifier root_inode_id() const; Inode* root_inode() { return m_root_inode.ptr(); } const Inode* root_inode() const { return m_root_inode.ptr(); }