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 <AK/StringView.h>
31#include <Kernel/FileSystem/FileSystem.h>
32#include <Kernel/FileSystem/Inode.h>
33#include <Kernel/Net/LocalSocket.h>
34#include <Kernel/VM/MemoryManager.h>
35#include <LibC/errno_numbers.h>
36
37namespace Kernel {
38
39static u32 s_lastFileSystemID;
40static HashMap<u32, FS*>* s_fs_map;
41
42static HashMap<u32, FS*>& all_fses()
43{
44 if (!s_fs_map)
45 s_fs_map = new HashMap<u32, FS*>();
46 return *s_fs_map;
47}
48
49FS::FS()
50 : m_fsid(++s_lastFileSystemID)
51{
52 all_fses().set(m_fsid, this);
53}
54
55FS::~FS()
56{
57 all_fses().remove(m_fsid);
58}
59
60FS* FS::from_fsid(u32 id)
61{
62 auto it = all_fses().find(id);
63 if (it != all_fses().end())
64 return (*it).value;
65 return nullptr;
66}
67
68FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, u8 ft)
69 : name_length(strlen(n))
70 , inode(i)
71 , file_type(ft)
72{
73 ASSERT(name_length < (int)sizeof(name));
74 memcpy(name, n, name_length);
75 name[name_length] = '\0';
76}
77
78FS::DirectoryEntry::DirectoryEntry(const char* n, size_t nl, InodeIdentifier i, u8 ft)
79 : name_length(nl)
80 , inode(i)
81 , file_type(ft)
82{
83 ASSERT(name_length < (int)sizeof(name));
84 memcpy(name, n, nl);
85 name[nl] = '\0';
86}
87
88void FS::sync()
89{
90 Inode::sync();
91
92 NonnullRefPtrVector<FS, 32> fses;
93 {
94 InterruptDisabler disabler;
95 for (auto& it : all_fses())
96 fses.append(*it.value);
97 }
98
99 for (auto& fs : fses)
100 fs.flush_writes();
101}
102
103void FS::lock_all()
104{
105 for (auto& it : all_fses()) {
106 it.value->m_lock.lock();
107 }
108}
109
110void FS::set_block_size(int block_size)
111{
112 ASSERT(block_size > 0);
113 if (block_size == m_block_size)
114 return;
115 m_block_size = block_size;
116}
117
118}