Kernel: Implement SYS_SYNC and add sync executable to userspace
You can (and have to) manually sync disk after writes to it.
This commit is contained in:
parent
b0c22b61ec
commit
5fae3cec2a
|
@ -13,6 +13,7 @@ namespace Kernel
|
|||
|
||||
virtual bool is_device() const override { return true; }
|
||||
virtual bool is_partition() const { return false; }
|
||||
virtual bool is_storage_device() const { return false; }
|
||||
|
||||
virtual dev_t rdev() const override = 0;
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ namespace Kernel
|
|||
void initialize_device_updater();
|
||||
|
||||
void add_device(BAN::StringView path, BAN::RefPtr<RamInode>);
|
||||
void for_each_device(const BAN::Function<BAN::Iteration(Device*)>& callback);
|
||||
|
||||
dev_t get_next_dev();
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <BAN/HashMap.h>
|
||||
#include <BAN/Iteration.h>
|
||||
#include <kernel/FS/FileSystem.h>
|
||||
#include <kernel/SpinLock.h>
|
||||
|
||||
|
@ -25,7 +26,7 @@ namespace Kernel
|
|||
blksize_t blksize() const { return m_blksize; }
|
||||
ino_t next_ino() { return m_next_ino++; }
|
||||
|
||||
void for_each_inode(void (*callback)(BAN::RefPtr<RamInode>));
|
||||
void for_each_inode(const BAN::Function<BAN::Iteration(BAN::RefPtr<RamInode>)>& callback);
|
||||
|
||||
protected:
|
||||
RamFileSystem(size_t size)
|
||||
|
|
|
@ -109,6 +109,8 @@ namespace Kernel
|
|||
BAN::ErrorOr<long> sys_fstatat(int fd, const char* path, struct stat* buf, int flag);
|
||||
BAN::ErrorOr<long> sys_stat(const char* path, struct stat* buf, int flag);
|
||||
|
||||
BAN::ErrorOr<long> sys_sync();
|
||||
|
||||
BAN::ErrorOr<void> mount(BAN::StringView source, BAN::StringView target);
|
||||
|
||||
BAN::ErrorOr<long> sys_read_dir_entries(int fd, DirectoryEntryList* buffer, size_t buffer_size);
|
||||
|
|
|
@ -77,6 +77,7 @@ namespace Kernel
|
|||
const BAN::Vector<Partition*>& partitions() const { return m_partitions; }
|
||||
|
||||
BAN::ErrorOr<void> sync_disk_cache();
|
||||
virtual bool is_storage_device() const override { return true; }
|
||||
|
||||
protected:
|
||||
virtual BAN::ErrorOr<void> read_sectors_impl(uint64_t lba, uint8_t sector_count, uint8_t* buffer) = 0;
|
||||
|
|
|
@ -44,6 +44,7 @@ namespace Kernel
|
|||
{
|
||||
if (inode->is_device())
|
||||
((Device*)inode.ptr())->update();
|
||||
return BAN::Iteration::Continue;
|
||||
}
|
||||
);
|
||||
s_instance->m_device_lock.unlock();
|
||||
|
@ -60,8 +61,22 @@ namespace Kernel
|
|||
MUST(reinterpret_cast<RamDirectoryInode*>(root_inode().ptr())->add_inode(path, device));
|
||||
}
|
||||
|
||||
void DevFileSystem::for_each_device(const BAN::Function<BAN::Iteration(Device*)>& callback)
|
||||
{
|
||||
LockGuard _(m_device_lock);
|
||||
for_each_inode(
|
||||
[&](BAN::RefPtr<Kernel::RamInode> inode)
|
||||
{
|
||||
if (!inode->is_device())
|
||||
return BAN::Iteration::Continue;
|
||||
return callback((Device*)inode.ptr());
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
dev_t DevFileSystem::get_next_dev()
|
||||
{
|
||||
LockGuard _(m_device_lock);
|
||||
static dev_t next_dev = 1;
|
||||
return next_dev++;
|
||||
}
|
||||
|
|
|
@ -48,11 +48,18 @@ namespace Kernel
|
|||
return m_inodes[ino];
|
||||
}
|
||||
|
||||
void RamFileSystem::for_each_inode(void (*callback)(BAN::RefPtr<RamInode>))
|
||||
void RamFileSystem::for_each_inode(const BAN::Function<BAN::Iteration(BAN::RefPtr<RamInode>)>& callback)
|
||||
{
|
||||
LockGuard _(m_lock);
|
||||
for (auto& [_, inode] : m_inodes)
|
||||
callback(inode);
|
||||
{
|
||||
auto decision = callback(inode);
|
||||
if (decision == BAN::Iteration::Break)
|
||||
break;
|
||||
if (decision == BAN::Iteration::Continue)
|
||||
continue;
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#include <BAN/ScopeGuard.h>
|
||||
#include <BAN/StringView.h>
|
||||
#include <kernel/CriticalScope.h>
|
||||
#include <kernel/FS/DevFS/FileSystem.h>
|
||||
#include <kernel/FS/VirtualFileSystem.h>
|
||||
#include <kernel/InterruptController.h>
|
||||
#include <kernel/LockGuard.h>
|
||||
|
@ -8,6 +9,7 @@
|
|||
#include <kernel/Memory/PageTableScope.h>
|
||||
#include <kernel/Process.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
#include <kernel/Storage/StorageDevice.h>
|
||||
#include <kernel/Timer/Timer.h>
|
||||
#include <LibELF/ELF.h>
|
||||
#include <LibELF/Values.h>
|
||||
|
@ -752,6 +754,24 @@ namespace Kernel
|
|||
return 0;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_sync()
|
||||
{
|
||||
BAN::ErrorOr<long> ret = 0;
|
||||
DevFileSystem::get().for_each_device(
|
||||
[&](Device* device)
|
||||
{
|
||||
if (device->is_storage_device())
|
||||
{
|
||||
auto success = ((StorageDevice*)device)->sync_disk_cache();
|
||||
if (success.is_error())
|
||||
ret = success.release_error();
|
||||
}
|
||||
return BAN::Iteration::Continue;
|
||||
}
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_read_dir_entries(int fd, DirectoryEntryList* list, size_t list_size)
|
||||
{
|
||||
LockGuard _(m_lock);
|
||||
|
|
|
@ -191,6 +191,9 @@ namespace Kernel
|
|||
case SYS_STAT:
|
||||
ret = Process::current().sys_stat((const char*)arg1, (struct stat*)arg2, (int)arg3);
|
||||
break;
|
||||
case SYS_SYNC:
|
||||
ret = Process::current().sys_sync();
|
||||
break;
|
||||
default:
|
||||
dwarnln("Unknown syscall {}", syscall);
|
||||
break;
|
||||
|
|
|
@ -54,6 +54,7 @@ __BEGIN_DECLS
|
|||
#define SYS_NANOSLEEP 47
|
||||
#define SYS_FSTATAT 48
|
||||
#define SYS_STAT 49 // stat/lstat
|
||||
#define SYS_SYNC 50
|
||||
|
||||
__END_DECLS
|
||||
|
||||
|
|
|
@ -183,6 +183,11 @@ int chdir(const char* path)
|
|||
return syscall(SYS_SET_PWD, path);
|
||||
}
|
||||
|
||||
void sync(void)
|
||||
{
|
||||
syscall(SYS_SYNC);
|
||||
}
|
||||
|
||||
pid_t getpid(void)
|
||||
{
|
||||
return syscall(SYS_GET_PID);
|
||||
|
|
|
@ -12,6 +12,7 @@ set(USERSPACE_PROJECTS
|
|||
Shell
|
||||
snake
|
||||
stat
|
||||
sync
|
||||
tee
|
||||
test
|
||||
touch
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
cmake_minimum_required(VERSION 3.26)
|
||||
|
||||
project(sync CXX)
|
||||
|
||||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(sync ${SOURCES})
|
||||
target_compile_options(sync PUBLIC -O2 -g)
|
||||
target_link_libraries(sync PUBLIC libc)
|
||||
|
||||
add_custom_target(sync-install
|
||||
COMMAND sudo cp ${CMAKE_CURRENT_BINARY_DIR}/sync ${BANAN_BIN}/
|
||||
DEPENDS sync
|
||||
USES_TERMINAL
|
||||
)
|
|
@ -0,0 +1,6 @@
|
|||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
sync();
|
||||
}
|
Loading…
Reference in New Issue