2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
|
#include <AK/HashMap.h>
|
2020-08-24 22:35:19 -03:00
|
|
|
#include <AK/Singleton.h>
|
2019-01-28 19:55:55 -02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-23 09:45:10 -03:00
|
|
|
#include <AK/StringView.h>
|
2021-06-21 12:34:09 -03:00
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
2019-05-15 22:02:37 -03:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2019-04-06 15:29:48 -03:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2019-06-07 06:43:58 -03:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
static u32 s_lastFileSystemID;
|
2020-08-24 22:35:19 -03:00
|
|
|
static AK::Singleton<HashMap<u32, FS*>> s_fs_map;
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
static HashMap<u32, FS*>& all_fses()
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2018-12-19 21:39:29 -02:00
|
|
|
return *s_fs_map;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 14:13:10 -02:00
|
|
|
FS::FS()
|
2019-05-01 22:28:20 -03:00
|
|
|
: m_fsid(++s_lastFileSystemID)
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2018-12-19 21:39:29 -02:00
|
|
|
all_fses().set(m_fsid, this);
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 14:13:10 -02:00
|
|
|
FS::~FS()
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2018-12-19 21:39:29 -02:00
|
|
|
all_fses().remove(m_fsid);
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
FS* FS::from_fsid(u32 id)
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2018-12-19 21:39:29 -02:00
|
|
|
auto it = all_fses().find(id);
|
|
|
|
|
if (it != all_fses().end())
|
2018-10-10 06:53:07 -03:00
|
|
|
return (*it).value;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 07:41:27 -03:00
|
|
|
FS::DirectoryEntryView::DirectoryEntryView(const StringView& n, InodeIdentifier i, u8 ft)
|
|
|
|
|
: name(n)
|
|
|
|
|
, inode(i)
|
|
|
|
|
, file_type(ft)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 21:39:29 -02:00
|
|
|
void FS::sync()
|
|
|
|
|
{
|
2019-05-15 22:02:37 -03:00
|
|
|
Inode::sync();
|
2019-04-25 17:05:53 -03:00
|
|
|
|
2019-06-27 08:44:26 -03:00
|
|
|
NonnullRefPtrVector<FS, 32> fses;
|
2019-04-25 17:05:53 -03:00
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
|
for (auto& it : all_fses())
|
|
|
|
|
fses.append(*it.value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 08:44:26 -03:00
|
|
|
for (auto& fs : fses)
|
|
|
|
|
fs.flush_writes();
|
2018-12-19 21:39:29 -02:00
|
|
|
}
|
2019-06-16 06:49:39 -03:00
|
|
|
|
|
|
|
|
void FS::lock_all()
|
|
|
|
|
{
|
|
|
|
|
for (auto& it : all_fses()) {
|
|
|
|
|
it.value->m_lock.lock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 15:46:47 -03:00
|
|
|
void FS::set_block_size(size_t block_size)
|
2019-08-11 05:09:36 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(block_size > 0);
|
2019-08-11 05:09:36 -03:00
|
|
|
if (block_size == m_block_size)
|
|
|
|
|
return;
|
|
|
|
|
m_block_size = block_size;
|
|
|
|
|
}
|
2020-02-15 21:27:42 -03:00
|
|
|
|
2021-05-18 16:04:52 -03:00
|
|
|
void FS::set_fragment_size(size_t fragment_size)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(fragment_size > 0);
|
|
|
|
|
if (fragment_size == m_fragment_size)
|
|
|
|
|
return;
|
|
|
|
|
m_fragment_size = fragment_size;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
}
|