VFS: vnode cache

This commit is contained in:
2024-05-04 20:03:34 +02:00
parent a5029c5974
commit 72de2c4586
12 changed files with 63 additions and 22 deletions

View File

@@ -56,11 +56,13 @@ void ktask_main() {
(new Task(Task::TaskMode::TASKMODE_KERN, templates_tester, "templates_tester2"))->start();
// (new Task(Task::TaskMode::TASKMODE_KERN, vfs_tester, "vfs_tester"))->start();
VFSGlobals::root = SharedPtr<RootNode>(new RootNode());
for (int i = 0; i < saved_modules_size; i++) {
auto &mod = saved_modules[i];
if (strcmp(saved_modules_names[i], "/sysroot.tar") == 0) {
VFSGlobals::mounts.add_mount(new TarFs((char *) mod.address, mod.size, &VFSGlobals::root));
VFSGlobals::mounts.add_mount(new TarFs((char *) mod.address, mod.size))->set_root(static_ptr_cast<NodeDir>(VFSGlobals::root));
}
}
GlobalTtyManager.all_tty_putstr("Setup finished \n");

View File

@@ -14,7 +14,7 @@
#include <sys/fcntl.h>
FDT::FD FDT::open(const Path &p, int opts) {
if (auto n = VFSGlobals::root.traverse(p); n.get() != nullptr) {
if (auto n = VFSGlobals::root->traverse(p); n.get() != nullptr) {
LockGuard l(_mtx);
_files.emplace(_cur_fd++, UniquePtr<File>(new File(n, opts)));
return _cur_fd - 1;

View File

@@ -4,8 +4,33 @@
#include "Filesystem.hpp"
Filesystem::~Filesystem() = default;
Filesystem::Filesystem(NodeDir *mounted_on) : _mounted_on(mounted_on) {
Filesystem::~Filesystem() {
if (_mounted_on.get()) {
_mounted_on->set_mounted(nullptr);
}
}
void Filesystem::set_root(SharedPtr<NodeDir> node) {
_mounted_on = std::move(node);
assert(_mounted_on->type() == Node::DIR);
_mounted_on->set_mounted(this);
}
SharedPtr<Node> Filesystem::get_node(ino_t inode) {
{
LockGuard l(_vnode_cache_lock);
if (auto p = _vnode_cache.find(inode); p != _vnode_cache.end())
if (auto l = p->second.lock()) return *l;
}
auto found = get_node_impl(inode);
{
LockGuard l(_vnode_cache_lock);
if (auto p = _vnode_cache.find(inode); p != _vnode_cache.end())
if (auto l = p->second.lock()) return *l;
_vnode_cache.emplace(inode, found);
return found;
}
}

View File

@@ -8,16 +8,29 @@
#include "Node.hpp"
#include "PointersCollection.hpp"
#include "SkipList.hpp"
class Filesystem {
public:
Filesystem(NodeDir *mounted_on);
Filesystem() = default;
virtual ~Filesystem() = 0;
virtual SharedPtr<NodeDir> root() = 0;
virtual SharedPtr<Node> get_node(ino_t inode) = 0;
Filesystem(Filesystem const &other) = delete;
Filesystem &operator=(Filesystem const &other) = delete;
NodeDir *_mounted_on;
virtual SharedPtr<NodeDir> root() = 0;
virtual SharedPtr<Node> get_node(ino_t inode);
void set_root(SharedPtr<NodeDir> node_dir);
protected:
virtual SharedPtr<Node> get_node_impl(ino_t inode) = 0;
SharedPtr<NodeDir> _mounted_on;
private:
SkipListMap<ino_t, WeakPtr<Node>> _vnode_cache;
Mutex _vnode_cache_lock;
};

View File

@@ -84,7 +84,7 @@ size_t MemFs::MemFsNodeFile::size() {
LockGuard l2(_fs_node->lock);
return _fs_node->data.size();
}
MemFs::MemFs(NodeDir *mounted_on) : Filesystem(mounted_on) {
MemFs::MemFs() {
_files.emplace(1, new DirInode{1});
_top_inode = 2;
}
@@ -100,7 +100,7 @@ SharedPtr<NodeDir> MemFs::root() {
return static_ptr_cast<NodeDir>(MemFsNodeDir::create(this, static_ptr_cast<DirInode>(root)));
}
}
SharedPtr<Node> MemFs::get_node(ino_t inode) {
SharedPtr<Node> MemFs::get_node_impl(ino_t inode) {
LockGuard l(_files_lock);
auto found = _files.find(inode);
if (found == _files.end()) return nullptr;

View File

@@ -91,10 +91,10 @@ private:
};
public:
MemFs(NodeDir *mounted_on);
MemFs();
SharedPtr<NodeDir> root() override;
SharedPtr<Node> get_node(ino_t inode) override;
SharedPtr<Node> get_node_impl(ino_t inode) override;
private:
ino_t _top_inode;

View File

@@ -3,6 +3,7 @@
//
#include "MountTable.hpp"
void MountTable::add_mount(Filesystem *fs) {
Filesystem *MountTable::add_mount(Filesystem *fs) {
_mounts.emplace_front(fs);
return fs;
}

View File

@@ -13,7 +13,7 @@
class MountTable {
public:
void add_mount(Filesystem *fs);
Filesystem *add_mount(Filesystem *fs);
private:
List<Filesystem *> _mounts;

View File

@@ -7,7 +7,7 @@
#include <TtyManager.hpp>
TarFs::TarFs(char *backing, size_t backing_size, NodeDir *mounted_on) : Filesystem(mounted_on), _backing((tar_header *) backing), _backing_size(backing_size) {
TarFs::TarFs(char *backing, size_t backing_size) : _backing((tar_header *) backing), _backing_size(backing_size) {
int i = 2;
const tar_header *header = _backing;
@@ -89,7 +89,7 @@ SharedPtr<NodeDir> TarFs::root() {
return static_ptr_cast<NodeDir>(get_node(1));
}
SharedPtr<Node> TarFs::get_node(ino_t inode) {
SharedPtr<Node> TarFs::get_node_impl(ino_t inode) {
if (_dir_map.find(inode) != _dir_map.end()) {
return static_ptr_cast<Node>(TarFsNodeDir::create(this, inode));
} else {

View File

@@ -9,11 +9,11 @@
class TarFs : public Filesystem {
public:
TarFs(char *backing, size_t backing_size, NodeDir *mounted_on);
TarFs(char *backing, size_t backing_size);
~TarFs() override = default;
SharedPtr<NodeDir> root() override;
SharedPtr<Node> get_node(ino_t inode) override;
SharedPtr<Node> get_node_impl(ino_t inode) override;
struct TarFsNodeDir : public ::NodeDir {

View File

@@ -17,5 +17,5 @@ ino_t RootNode::mkfile(const String &name) {
}
RootNode VFSGlobals::root;
MountTable VFSGlobals::mounts;
SharedPtr<RootNode> VFSGlobals::root;
MountTable VFSGlobals::mounts;

View File

@@ -19,8 +19,8 @@ public:
};
namespace VFSGlobals {
extern RootNode root;
extern MountTable mounts;
extern SharedPtr<RootNode> root;
extern MountTable mounts;
} // namespace VFSGlobals