1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:38:12 +00:00

Add sync() syscall and a /bin/sync.

It walks all the live Inode objects and flushes pending metadata changes
wherever needed.

This could be optimized by keeping a separate list of dirty Inodes,
but let's not get ahead of ourselves.
This commit is contained in:
Andreas Kling 2018-12-20 00:39:29 +01:00
parent d0f06e5f3f
commit ed7ae6c02c
14 changed files with 78 additions and 24 deletions

View file

@ -306,6 +306,7 @@ void Ext2FSInode::populate_metadata() const
void Ext2FSInode::flush_metadata()
{
dbgprintf("Ext2FSInode: flush_metadata for inode %u\n", index());
m_raw_inode.i_size = m_metadata.size;
m_raw_inode.i_mode = m_metadata.mode;
m_raw_inode.i_uid = m_metadata.uid;

View file

@ -4,36 +4,45 @@
#include "FileSystem.h"
static dword s_lastFileSystemID;
static HashMap<dword, FS*>* map;
static HashMap<dword, FS*>* s_fs_map;
static HashTable<Inode*>* s_inode_set;
static HashMap<dword, FS*>& fileSystems()
static HashMap<dword, FS*>& all_fses()
{
if (!map)
map = new HashMap<dword, FS*>();
return *map;
if (!s_fs_map)
s_fs_map = new HashMap<dword, FS*>();
return *s_fs_map;
}
static HashTable<Inode*>& all_inodes()
{
if (!s_inode_set)
s_inode_set = new HashTable<Inode*>();
return *s_inode_set;
}
void FS::initializeGlobals()
{
s_lastFileSystemID = 0;
map = 0;
s_fs_map = nullptr;
s_inode_set = nullptr;
}
FS::FS()
: m_fsid(++s_lastFileSystemID)
{
fileSystems().set(m_fsid, this);
all_fses().set(m_fsid, this);
}
FS::~FS()
{
fileSystems().remove(m_fsid);
all_fses().remove(m_fsid);
}
FS* FS::from_fsid(dword id)
{
auto it = fileSystems().find(id);
if (it != fileSystems().end())
auto it = all_fses().find(id);
if (it != all_fses().end())
return (*it).value;
return nullptr;
}
@ -125,8 +134,16 @@ FS::DirectoryEntry::DirectoryEntry(const char* n, size_t nl, InodeIdentifier i,
name[nl] = '\0';
}
Inode::Inode(FS& fs, unsigned index)
: m_fs(fs)
, m_index(index)
{
all_inodes().set(this);
}
Inode::~Inode()
{
all_inodes().remove(this);
}
void Inode::will_be_destroyed()
@ -167,3 +184,11 @@ int Inode::set_mtime(Unix::time_t ts)
m_metadata_dirty = true;
return 0;
}
void FS::sync()
{
for (auto* inode : all_inodes()) {
if (inode->is_metadata_dirty())
inode->flush_metadata();
}
}

View file

@ -26,6 +26,7 @@ public:
dword id() const { return m_fsid; }
static FS* from_fsid(dword);
static void sync();
virtual bool initialize() = 0;
virtual const char* class_name() const = 0;
@ -98,11 +99,7 @@ public:
void will_be_destroyed();
protected:
Inode(FS& fs, unsigned index)
: m_fs(fs)
, m_index(index)
{
}
Inode(FS& fs, unsigned index);
virtual void populate_metadata() const = 0;
void set_metadata_dirty(bool b) { m_metadata_dirty = b; }

View file

@ -47,13 +47,11 @@ VFS::~VFS()
auto VFS::makeNode(InodeIdentifier inode) -> RetainPtr<Vnode>
{
auto metadata = inode.metadata();
if (!metadata.isValid())
auto core_inode = inode.fs()->get_inode(inode);
if (!core_inode)
return nullptr;
auto core_inode = inode.fs()->get_inode(inode);
if (core_inode)
core_inode->m_metadata = metadata;
auto& metadata = core_inode->metadata();
InterruptDisabler disabler;
@ -501,3 +499,8 @@ void VFS::for_each_mount(Function<void(const Mount&)> callback) const
callback(*mount);
}
}
void VFS::sync()
{
FS::sync();
}

View file

@ -123,6 +123,8 @@ public:
InodeIdentifier root_inode_id() const;
void sync();
private:
friend class FileDescriptor;
friend class Vnode;