summaryrefslogtreecommitdiff
path: root/Kernel/FileSystem/FileSystem.cpp
blob: b98840fd0a8eda882985e09321e2ba95b7eb1774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/HashMap.h>
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <Kernel/Arch/x86/InterruptDisabler.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Net/LocalSocket.h>

namespace Kernel {

static u32 s_lastFileSystemID;
static Singleton<HashMap<u32, FileSystem*>> s_file_system_map;

static HashMap<u32, FileSystem*>& all_file_systems()
{
    return *s_file_system_map;
}

FileSystem::FileSystem()
    : m_fsid(++s_lastFileSystemID)
{
    s_file_system_map->set(m_fsid, this);
}

FileSystem::~FileSystem()
{
    s_file_system_map->remove(m_fsid);
}

FileSystem* FileSystem::from_fsid(u32 id)
{
    auto it = all_file_systems().find(id);
    if (it != all_file_systems().end())
        return (*it).value;
    return nullptr;
}

FileSystem::DirectoryEntryView::DirectoryEntryView(const StringView& n, InodeIdentifier i, u8 ft)
    : name(n)
    , inode(i)
    , file_type(ft)
{
}

void FileSystem::sync()
{
    Inode::sync_all();

    NonnullRefPtrVector<FileSystem, 32> file_systems;
    {
        InterruptDisabler disabler;
        for (auto& it : all_file_systems())
            file_systems.append(*it.value);
    }

    for (auto& fs : file_systems)
        fs.flush_writes();
}

void FileSystem::lock_all()
{
    for (auto& it : all_file_systems()) {
        it.value->m_lock.lock();
    }
}

}