Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Assertions.h>
28#include <AK/HashMap.h>
29#include <AK/StringBuilder.h>
30#include <Kernel/FileSystem/FileSystem.h>
31#include <Kernel/FileSystem/Inode.h>
32#include <Kernel/Net/LocalSocket.h>
33#include <Kernel/VM/MemoryManager.h>
34#include <LibC/errno_numbers.h>
35
36namespace Kernel {
37
38static u32 s_lastFileSystemID;
39static HashMap<u32, FS*>* s_fs_map;
40
41static HashMap<u32, FS*>& all_fses()
42{
43 if (!s_fs_map)
44 s_fs_map = new HashMap<u32, FS*>();
45 return *s_fs_map;
46}
47
48FS::FS()
49 : m_fsid(++s_lastFileSystemID)
50{
51 all_fses().set(m_fsid, this);
52}
53
54FS::~FS()
55{
56 all_fses().remove(m_fsid);
57}
58
59FS* FS::from_fsid(u32 id)
60{
61 auto it = all_fses().find(id);
62 if (it != all_fses().end())
63 return (*it).value;
64 return nullptr;
65}
66
67FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, u8 ft)
68 : name_length(strlen(n))
69 , inode(i)
70 , file_type(ft)
71{
72 ASSERT(name_length < (int)sizeof(name));
73 memcpy(name, n, name_length);
74 name[name_length] = '\0';
75}
76
77FS::DirectoryEntry::DirectoryEntry(const char* n, size_t nl, InodeIdentifier i, u8 ft)
78 : name_length(nl)
79 , inode(i)
80 , file_type(ft)
81{
82 ASSERT(name_length < (int)sizeof(name));
83 memcpy(name, n, nl);
84 name[nl] = '\0';
85}
86
87void FS::sync()
88{
89 Inode::sync();
90
91 NonnullRefPtrVector<FS, 32> fses;
92 {
93 InterruptDisabler disabler;
94 for (auto& it : all_fses())
95 fses.append(*it.value);
96 }
97
98 for (auto& fs : fses)
99 fs.flush_writes();
100}
101
102void FS::lock_all()
103{
104 for (auto& it : all_fses()) {
105 it.value->m_lock.lock();
106 }
107}
108
109void FS::set_block_size(int block_size)
110{
111 ASSERT(block_size > 0);
112 if (block_size == m_block_size)
113 return;
114 m_block_size = block_size;
115}
116
117}