2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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-06 15:56:10 +01:00
|
|
|
#include <AK/Bitmap.h>
|
2020-02-22 16:37:51 +01:00
|
|
|
#include <AK/HashMap.h>
|
2020-07-02 12:48:08 +03:00
|
|
|
#include <Kernel/FileSystem/BlockBasedFileSystem.h>
|
2022-10-24 10:02:37 +03:00
|
|
|
#include <Kernel/FileSystem/Ext2FS/Definitions.h>
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2020-02-16 01:27:42 +01:00
|
|
|
#include <Kernel/KBuffer.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-10-24 10:02:37 +03:00
|
|
|
class Ext2FSInode;
|
2018-11-13 13:02:39 +01:00
|
|
|
|
2021-07-11 00:34:36 +02:00
|
|
|
class Ext2FS final : public BlockBasedFileSystem {
|
2018-11-15 17:13:10 +01:00
|
|
|
friend class Ext2FSInode;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
public:
|
2021-03-17 18:35:42 +01:00
|
|
|
enum class FeaturesReadOnly : u32 {
|
|
|
|
None = 0,
|
|
|
|
FileSize64bits = 1 << 1,
|
|
|
|
};
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
static ErrorOr<NonnullLockRefPtr<FileSystem>> try_create(OpenFileDescription&);
|
2020-04-06 11:54:21 +03:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
virtual ~Ext2FS() override;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2019-02-21 14:48:00 +01:00
|
|
|
virtual unsigned total_block_count() const override;
|
|
|
|
virtual unsigned free_block_count() const override;
|
|
|
|
virtual unsigned total_inode_count() const override;
|
|
|
|
virtual unsigned free_inode_count() const override;
|
|
|
|
|
2019-12-15 19:33:39 +01:00
|
|
|
virtual bool supports_watchers() const override { return true; }
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual u8 internal_file_type_to_directory_entry_type(DirectoryEntryView const& entry) const override;
|
2020-08-29 21:25:01 +03:00
|
|
|
|
2021-03-17 18:35:42 +01:00
|
|
|
FeaturesReadOnly get_features_readonly() const;
|
|
|
|
|
2022-10-24 10:02:37 +03:00
|
|
|
virtual StringView class_name() const override { return "Ext2FS"sv; }
|
|
|
|
virtual Inode& root_inode() override;
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
private:
|
2022-07-22 19:43:50 +01:00
|
|
|
AK_TYPEDEF_DISTINCT_ORDERED_ID(unsigned, GroupIndex);
|
2021-02-12 13:33:58 +01:00
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
explicit Ext2FS(OpenFileDescription&);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ext2_super_block const& super_block() const { return m_super_block; }
|
|
|
|
ext2_group_desc const& group_descriptor(GroupIndex) const;
|
2020-12-18 13:18:47 +01:00
|
|
|
ext2_group_desc* block_group_descriptors() { return (ext2_group_desc*)m_cached_group_descriptor_table->data(); }
|
2022-04-01 20:58:27 +03:00
|
|
|
ext2_group_desc const* block_group_descriptors() const { return (ext2_group_desc const*)m_cached_group_descriptor_table->data(); }
|
2019-01-28 04:16:01 +01:00
|
|
|
void flush_block_group_descriptor_table();
|
2021-07-05 21:38:17 +02:00
|
|
|
u64 inodes_per_block() const;
|
|
|
|
u64 inodes_per_group() const;
|
|
|
|
u64 blocks_per_group() const;
|
|
|
|
u64 inode_size() const;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
Kernel/FileSystem: Discard safely filesystems when unmounted last time
This commit reached that goal of "safely discarding" a filesystem by
doing the following:
1. Stop using the s_file_system_map HashMap as it was an unsafe measure
to access pointers of FileSystems. Instead, make sure to register all
FileSystems at the VFS layer, with an IntrusiveList, to avoid problems
related to OOM conditions.
2. Make sure to cleanly remove the DiskCache object from a BlockBased
filesystem, so the destructor of such object will not need to do that in
the destruction point.
3. For ext2 filesystems, don't cache the root inode at m_inode_cache
HashMap. The reason for this is that when unmounting an ext2 filesystem,
we lookup at the cache to see if there's a reference to a cached inode
and if that's the case, we fail with EBUSY. If we keep the m_root_inode
also being referenced at the m_inode_cache map, we have 2 references to
that object, which will lead to fail with EBUSY. Also, it's much simpler
to always ask for a root inode and get it immediately from m_root_inode,
instead of looking up the cache for that inode.
2022-08-20 09:28:02 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<Ext2FSInode>> build_root_inode() const;
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> write_ext2_inode(InodeIndex, ext2_inode const&);
|
2021-02-12 09:18:47 +01:00
|
|
|
bool find_block_containing_inode(InodeIndex, BlockIndex& block_index, unsigned& offset) const;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2022-01-22 19:53:02 -05:00
|
|
|
ErrorOr<void> flush_super_block();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2022-08-20 00:03:24 +03:00
|
|
|
virtual ErrorOr<void> initialize_while_locked() override;
|
|
|
|
virtual bool is_initialized_while_locked() override;
|
|
|
|
|
|
|
|
virtual ErrorOr<void> prepare_to_clear_last_mount() override;
|
2022-08-19 20:53:40 +02:00
|
|
|
ErrorOr<NonnullLockRefPtr<Inode>> get_inode(InodeIdentifier) const;
|
|
|
|
ErrorOr<NonnullLockRefPtr<Inode>> create_inode(Ext2FSInode& parent_inode, StringView name, mode_t, dev_t, UserID, GroupID);
|
|
|
|
ErrorOr<NonnullLockRefPtr<Inode>> create_directory(Ext2FSInode& parent_inode, StringView name, mode_t, UserID, GroupID);
|
2019-11-02 11:36:56 +01:00
|
|
|
virtual void flush_writes() override;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2019-09-22 18:34:52 +02:00
|
|
|
BlockIndex first_block_index() const;
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<InodeIndex> allocate_inode(GroupIndex preferred_group = 0);
|
|
|
|
ErrorOr<Vector<BlockIndex>> allocate_blocks(GroupIndex preferred_group_index, size_t count);
|
2019-04-23 13:00:53 +02:00
|
|
|
GroupIndex group_index_from_inode(InodeIndex) const;
|
2019-02-15 23:24:01 +01:00
|
|
|
GroupIndex group_index_from_block_index(BlockIndex) const;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<bool> get_inode_allocation_state(InodeIndex) const;
|
|
|
|
ErrorOr<void> set_inode_allocation_state(InodeIndex, bool);
|
|
|
|
ErrorOr<void> set_block_allocation_state(BlockIndex, bool);
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2019-01-22 07:03:44 +01:00
|
|
|
void uncache_inode(InodeIndex);
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> free_inode(Ext2FSInode&);
|
2019-01-22 07:03:44 +01:00
|
|
|
|
2019-01-23 02:45:25 +01:00
|
|
|
struct BlockListShape {
|
|
|
|
unsigned direct_blocks { 0 };
|
|
|
|
unsigned indirect_blocks { 0 };
|
|
|
|
unsigned doubly_indirect_blocks { 0 };
|
|
|
|
unsigned triply_indirect_blocks { 0 };
|
|
|
|
unsigned meta_blocks { 0 };
|
|
|
|
};
|
|
|
|
|
2020-11-07 16:45:03 +01:00
|
|
|
BlockListShape compute_block_list_shape(unsigned blocks) const;
|
2019-01-23 02:45:25 +01:00
|
|
|
|
2021-07-05 21:38:17 +02:00
|
|
|
u64 m_block_group_count { 0 };
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-12-22 01:27:29 -08:00
|
|
|
mutable ext2_super_block m_super_block {};
|
2020-12-18 13:18:47 +01:00
|
|
|
mutable OwnPtr<KBuffer> m_cached_group_descriptor_table;
|
2018-10-29 23:45:34 +01:00
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
mutable HashMap<InodeIndex, LockRefPtr<Ext2FSInode>> m_inode_cache;
|
2019-11-02 11:36:56 +01:00
|
|
|
|
|
|
|
bool m_super_block_dirty { false };
|
|
|
|
bool m_block_group_descriptors_dirty { false };
|
2019-11-02 12:18:43 +01:00
|
|
|
|
|
|
|
struct CachedBitmap {
|
2021-08-01 02:21:20 -07:00
|
|
|
CachedBitmap(BlockIndex bi, NonnullOwnPtr<KBuffer> buf)
|
2019-11-02 12:18:43 +01:00
|
|
|
: bitmap_block_index(bi)
|
2019-11-03 00:10:24 +01:00
|
|
|
, buffer(move(buf))
|
2020-02-16 01:27:42 +01:00
|
|
|
{
|
|
|
|
}
|
2019-11-02 12:18:43 +01:00
|
|
|
BlockIndex bitmap_block_index { 0 };
|
|
|
|
bool dirty { false };
|
2021-08-01 02:21:20 -07:00
|
|
|
NonnullOwnPtr<KBuffer> buffer;
|
2021-11-06 15:56:10 +01:00
|
|
|
Bitmap bitmap(u32 blocks_per_group) { return Bitmap { buffer->data(), blocks_per_group }; }
|
2019-11-02 12:18:43 +01:00
|
|
|
};
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<CachedBitmap*> get_bitmap_block(BlockIndex);
|
|
|
|
ErrorOr<void> update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, bool new_state, u32& super_block_counter, u16& group_descriptor_counter);
|
2019-11-02 12:18:43 +01:00
|
|
|
|
|
|
|
Vector<OwnPtr<CachedBitmap>> m_cached_bitmaps;
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<Ext2FSInode> m_root_inode;
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|