2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-07-11 00:20:38 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
#include <AK/Error.h>
|
2019-06-21 18:58:45 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2019-08-11 23:56:39 +10:00
|
|
|
#include <AK/RefPtr.h>
|
2020-08-18 12:41:27 +02:00
|
|
|
#include <AK/StringView.h>
|
2020-01-28 20:42:27 +01:00
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
2021-07-11 11:49:16 +02:00
|
|
|
#include <Kernel/Forward.h>
|
2021-07-18 09:10:27 +02:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2020-01-28 20:42:27 +01:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2020-09-11 21:11:07 -06:00
|
|
|
#include <Kernel/UserOrKernelBuffer.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-05-19 08:35:09 -06:00
|
|
|
static constexpr u32 mepoch = 476763780;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-07-11 00:20:38 +02:00
|
|
|
class FileSystem : public RefCounted<FileSystem> {
|
2019-02-28 21:51:59 +01:00
|
|
|
friend class Inode;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
public:
|
2021-07-11 00:20:38 +02:00
|
|
|
virtual ~FileSystem();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2019-01-23 05:38:54 +01:00
|
|
|
unsigned fsid() const { return m_fsid; }
|
2021-07-11 00:20:38 +02:00
|
|
|
static FileSystem* from_fsid(u32);
|
2018-12-20 00:39:29 +01:00
|
|
|
static void sync();
|
2019-06-16 11:49:39 +02:00
|
|
|
static void lock_all();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> initialize() = 0;
|
2021-07-17 20:59:06 +02:00
|
|
|
virtual StringView class_name() const = 0;
|
2021-07-18 01:50:47 +02:00
|
|
|
virtual Inode& root_inode() = 0;
|
2019-12-15 19:33:39 +01:00
|
|
|
virtual bool supports_watchers() const { return false; }
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-12-19 21:56:45 +01:00
|
|
|
bool is_readonly() const { return m_readonly; }
|
|
|
|
|
2019-02-21 14:48:00 +01:00
|
|
|
virtual unsigned total_block_count() const { return 0; }
|
|
|
|
virtual unsigned free_block_count() const { return 0; }
|
|
|
|
virtual unsigned total_inode_count() const { return 0; }
|
|
|
|
virtual unsigned free_inode_count() const { return 0; }
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> prepare_to_unmount() { return {}; }
|
2019-08-11 23:56:39 +10:00
|
|
|
|
2020-08-18 12:41:27 +02:00
|
|
|
struct DirectoryEntryView {
|
|
|
|
DirectoryEntryView(const StringView& name, InodeIdentifier, u8 file_type);
|
|
|
|
|
|
|
|
StringView name;
|
|
|
|
InodeIdentifier inode;
|
|
|
|
u8 file_type { 0 };
|
|
|
|
};
|
|
|
|
|
2020-09-18 09:49:51 +02:00
|
|
|
virtual void flush_writes() { }
|
2019-04-25 22:05:53 +02:00
|
|
|
|
2021-07-05 21:38:17 +02:00
|
|
|
u64 block_size() const { return m_block_size; }
|
2021-05-18 21:04:52 +02:00
|
|
|
size_t fragment_size() const { return m_fragment_size; }
|
2019-08-11 10:09:36 +02:00
|
|
|
|
2020-04-06 11:54:21 +03:00
|
|
|
virtual bool is_file_backed() const { return false; }
|
2019-08-17 12:17:36 +03:00
|
|
|
|
2020-08-29 21:25:01 +03:00
|
|
|
// Converts file types that are used internally by the filesystem to DT_* types
|
|
|
|
virtual u8 internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const { return entry.file_type; }
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
protected:
|
2021-07-11 00:20:38 +02:00
|
|
|
FileSystem();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-07-05 21:38:17 +02:00
|
|
|
void set_block_size(u64 size) { m_block_size = size; }
|
|
|
|
void set_fragment_size(size_t size) { m_fragment_size = size; }
|
2019-08-11 10:09:36 +02:00
|
|
|
|
2021-07-17 21:09:51 +02:00
|
|
|
mutable Mutex m_lock { "FS" };
|
2019-02-28 21:51:59 +01:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
private:
|
2019-01-23 05:38:54 +01:00
|
|
|
unsigned m_fsid { 0 };
|
2021-07-05 21:38:17 +02:00
|
|
|
u64 m_block_size { 0 };
|
2021-05-18 21:04:52 +02:00
|
|
|
size_t m_fragment_size { 0 };
|
2018-12-19 21:56:45 +01:00
|
|
|
bool m_readonly { false };
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|
2021-07-11 00:20:38 +02:00
|
|
|
inline FileSystem* InodeIdentifier::fs()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2021-07-11 00:20:38 +02:00
|
|
|
return FileSystem::from_fsid(m_fsid);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 00:20:38 +02:00
|
|
|
inline const FileSystem* InodeIdentifier::fs() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2021-07-11 00:20:38 +02:00
|
|
|
return FileSystem::from_fsid(m_fsid);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
2020-02-16 01:27:42 +01:00
|
|
|
struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> {
|
2021-02-12 09:18:47 +01:00
|
|
|
static unsigned hash(const Kernel::InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index().value()); }
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|