Serenity Operating System
1/*
2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/AtomicRefCounted.h>
10#include <AK/Error.h>
11#include <AK/StringView.h>
12#include <Kernel/FileSystem/InodeIdentifier.h>
13#include <Kernel/Forward.h>
14#include <Kernel/Library/LockRefPtr.h>
15#include <Kernel/Locking/Mutex.h>
16#include <Kernel/UnixTypes.h>
17#include <Kernel/UserOrKernelBuffer.h>
18
19namespace Kernel {
20
21class FileSystem : public AtomicRefCounted<FileSystem> {
22 friend class Inode;
23 friend class VirtualFileSystem;
24
25public:
26 virtual ~FileSystem();
27
28 FileSystemID fsid() const { return m_fsid; }
29 static void sync();
30 static void lock_all();
31
32 virtual ErrorOr<void> initialize() = 0;
33 virtual StringView class_name() const = 0;
34 virtual Inode& root_inode() = 0;
35 virtual bool supports_watchers() const { return false; }
36
37 bool is_readonly() const { return m_readonly; }
38
39 virtual unsigned total_block_count() const { return 0; }
40 virtual unsigned free_block_count() const { return 0; }
41 virtual unsigned total_inode_count() const { return 0; }
42 virtual unsigned free_inode_count() const { return 0; }
43
44 ErrorOr<void> prepare_to_unmount();
45
46 struct DirectoryEntryView {
47 DirectoryEntryView(StringView name, InodeIdentifier, u8 file_type);
48
49 StringView name;
50 InodeIdentifier inode;
51 u8 file_type { 0 };
52 };
53
54 virtual void flush_writes() { }
55
56 u64 block_size() const { return m_block_size; }
57 size_t fragment_size() const { return m_fragment_size; }
58
59 virtual bool is_file_backed() const { return false; }
60
61 // Converts file types that are used internally by the filesystem to DT_* types
62 virtual u8 internal_file_type_to_directory_entry_type(DirectoryEntryView const& entry) const { return entry.file_type; }
63
64 SpinlockProtected<size_t, LockRank::FileSystem>& mounted_count(Badge<VirtualFileSystem>) { return m_attach_count; }
65
66protected:
67 FileSystem();
68
69 void set_block_size(u64 size) { m_block_size = size; }
70 void set_fragment_size(size_t size) { m_fragment_size = size; }
71
72 virtual ErrorOr<void> prepare_to_clear_last_mount() { return {}; }
73
74 mutable Mutex m_lock { "FS"sv };
75
76private:
77 FileSystemID m_fsid;
78 u64 m_block_size { 0 };
79 size_t m_fragment_size { 0 };
80 bool m_readonly { false };
81
82 SpinlockProtected<size_t, LockRank::FileSystem> m_attach_count { 0 };
83 IntrusiveListNode<FileSystem> m_file_system_node;
84};
85
86}
87
88namespace AK {
89
90template<>
91struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> {
92 static unsigned hash(Kernel::InodeIdentifier const& inode) { return pair_int_hash(inode.fsid().value(), inode.index().value()); }
93};
94
95}