Kernel: Add distinct InodeIndex type

Use the DistinctNumeric mechanism to make InodeIndex a strongly typed
integer type.
This commit is contained in:
Andreas Kling 2021-02-12 09:18:47 +01:00
parent c8a90a31b6
commit e44c1792a7
Notes: sideshowbarker 2024-07-18 22:24:41 +09:00
17 changed files with 63 additions and 56 deletions

View File

@ -61,13 +61,13 @@ bool DevPtsFS::initialize()
return true;
}
static unsigned inode_index_to_pty_index(unsigned inode_index)
static unsigned inode_index_to_pty_index(InodeIndex inode_index)
{
ASSERT(inode_index > 1);
return inode_index - 2;
return inode_index.value() - 2;
}
static unsigned pty_index_to_inode_index(unsigned pty_index)
static InodeIndex pty_index_to_inode_index(unsigned pty_index)
{
return pty_index + 2;
}
@ -109,7 +109,7 @@ void DevPtsFS::unregister_slave_pty(SlavePTY& slave_pty)
s_ptys->remove(slave_pty.index());
}
DevPtsFSInode::DevPtsFSInode(DevPtsFS& fs, unsigned index, SlavePTY* pty)
DevPtsFSInode::DevPtsFSInode(DevPtsFS& fs, InodeIndex index, SlavePTY* pty)
: Inode(fs, index)
{
if (pty)

View File

@ -64,7 +64,7 @@ public:
virtual ~DevPtsFSInode() override;
private:
DevPtsFSInode(DevPtsFS&, unsigned index, SlavePTY*);
DevPtsFSInode(DevPtsFS&, InodeIndex, SlavePTY*);
// ^Inode
virtual ssize_t read_bytes(off_t, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const override;

View File

@ -46,7 +46,7 @@ static const ssize_t max_inline_symlink_length = 60;
struct Ext2FSDirectoryEntry {
String name;
Ext2FS::InodeIndex inode_index { 0 };
InodeIndex inode_index { 0 };
u8 file_type { 0 };
};
@ -172,7 +172,7 @@ NonnullRefPtr<Inode> Ext2FS::root_inode() const
return *get_inode({ fsid(), EXT2_ROOT_INO });
}
bool Ext2FS::find_block_containing_inode(unsigned inode, unsigned& block_index, unsigned& offset) const
bool Ext2FS::find_block_containing_inode(InodeIndex inode, unsigned& block_index, unsigned& offset) const
{
LOCKER(m_lock);
auto& super_block = this->super_block();
@ -185,7 +185,7 @@ bool Ext2FS::find_block_containing_inode(unsigned inode, unsigned& block_index,
auto& bgd = group_descriptor(group_index_from_inode(inode));
offset = ((inode - 1) % inodes_per_group()) * inode_size();
offset = ((inode.value() - 1) % inodes_per_group()) * inode_size();
block_index = bgd.bg_inode_table + (offset >> EXT2_BLOCK_SIZE_BITS(&super_block));
offset &= block_size() - 1;
@ -620,7 +620,7 @@ void Ext2FS::flush_writes()
uncache_inode(index);
}
Ext2FSInode::Ext2FSInode(Ext2FS& fs, unsigned index)
Ext2FSInode::Ext2FSInode(Ext2FS& fs, InodeIndex index)
: Inode(fs, index)
{
}
@ -728,7 +728,7 @@ ssize_t Ext2FSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer&
m_block_list = fs().block_list_for_inode(m_raw_inode);
if (m_block_list.is_empty()) {
klog() << "ext2fs: read_bytes: empty block list for inode " << index();
dmesgln("Ext2FS: read_bytes: empty block list for inode {}", index());
return -EIO;
}
@ -990,7 +990,7 @@ KResult Ext2FSInode::write_directory(const Vector<Ext2FSDirectoryEntry>& entries
dbgln_if(EXT2_DEBUG, "* Inode: {}, name_len: {}, rec_len: {}, file_type: {}, name: {}", entry.inode_index, u16(entry.name.length()), u16(record_length), u8(entry.file_type), entry.name);
stream << u32(entry.inode_index);
stream << u32(entry.inode_index.value());
stream << u16(record_length);
stream << u8(entry.name.length());
stream << u8(entry.file_type);
@ -1121,7 +1121,7 @@ unsigned Ext2FS::blocks_per_group() const
return EXT2_BLOCKS_PER_GROUP(&super_block());
}
bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
bool Ext2FS::write_ext2_inode(InodeIndex inode, const ext2_inode& e2inode)
{
LOCKER(m_lock);
unsigned block_index;
@ -1189,7 +1189,7 @@ Vector<Ext2FS::BlockIndex> Ext2FS::allocate_blocks(GroupIndex preferred_group_in
return blocks;
}
unsigned Ext2FS::find_a_free_inode(GroupIndex preferred_group)
InodeIndex Ext2FS::find_a_free_inode(GroupIndex preferred_group)
{
LOCKER(m_lock);
dbgln_if(EXT2_DEBUG, "Ext2FS: find_a_free_inode(preferred_group: {})", preferred_group);
@ -1221,16 +1221,16 @@ unsigned Ext2FS::find_a_free_inode(GroupIndex preferred_group)
auto& bgd = group_descriptor(group_index);
unsigned inodes_in_group = min(inodes_per_group(), super_block().s_inodes_count);
unsigned first_free_inode_in_group = 0;
InodeIndex first_free_inode_in_group = 0;
unsigned first_inode_in_group = (group_index - 1) * inodes_per_group() + 1;
InodeIndex first_inode_in_group = (group_index - 1) * inodes_per_group() + 1;
auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
auto inode_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), inodes_in_group);
for (size_t i = 0; i < inode_bitmap.size(); ++i) {
if (inode_bitmap.get(i))
continue;
first_free_inode_in_group = first_inode_in_group + i;
first_free_inode_in_group = InodeIndex(first_inode_in_group.value() + i);
break;
}
@ -1239,7 +1239,7 @@ unsigned Ext2FS::find_a_free_inode(GroupIndex preferred_group)
return 0;
}
unsigned inode = first_free_inode_in_group;
InodeIndex inode = first_free_inode_in_group;
dbgln_if(EXT2_DEBUG, "Ext2FS: found suitable inode {}", inode);
ASSERT(get_inode_allocation_state(inode) == false);
@ -1253,11 +1253,11 @@ Ext2FS::GroupIndex Ext2FS::group_index_from_block_index(BlockIndex block_index)
return (block_index - 1) / blocks_per_group() + 1;
}
unsigned Ext2FS::group_index_from_inode(unsigned inode) const
unsigned Ext2FS::group_index_from_inode(InodeIndex inode) const
{
if (!inode)
return 0;
return (inode - 1) / inodes_per_group() + 1;
return (inode.value() - 1) / inodes_per_group() + 1;
}
bool Ext2FS::get_inode_allocation_state(InodeIndex index) const
@ -1267,7 +1267,7 @@ bool Ext2FS::get_inode_allocation_state(InodeIndex index) const
return true;
unsigned group_index = group_index_from_inode(index);
auto& bgd = group_descriptor(group_index);
unsigned index_in_group = index - ((group_index - 1) * inodes_per_group());
unsigned index_in_group = index.value() - ((group_index - 1) * inodes_per_group());
unsigned bit_index = (index_in_group - 1) % inodes_per_group();
auto& cached_bitmap = const_cast<Ext2FS&>(*this).get_bitmap_block(bgd.bg_inode_bitmap);
@ -1279,7 +1279,7 @@ bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
LOCKER(m_lock);
unsigned group_index = group_index_from_inode(inode_index);
auto& bgd = group_descriptor(group_index);
unsigned index_in_group = inode_index - ((group_index - 1) * inodes_per_group());
unsigned index_in_group = inode_index.value() - ((group_index - 1) * inodes_per_group());
unsigned bit_index = (index_in_group - 1) % inodes_per_group();
auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
@ -1473,7 +1473,7 @@ bool Ext2FSInode::populate_lookup_cache() const
LOCKER(m_lock);
if (!m_lookup_cache.is_empty())
return true;
HashMap<String, unsigned> children;
HashMap<String, InodeIndex> children;
KResult result = traverse_as_directory([&children](auto& entry) {
children.set(entry.name, entry.inode.index());

View File

@ -83,14 +83,12 @@ private:
bool populate_lookup_cache() const;
KResult resize(u64);
static u8 file_type_for_directory_entry(const ext2_dir_entry_2&);
Ext2FS& fs();
const Ext2FS& fs() const;
Ext2FSInode(Ext2FS&, unsigned index);
Ext2FSInode(Ext2FS&, InodeIndex);
mutable Vector<unsigned> m_block_list;
mutable HashMap<String, unsigned> m_lookup_cache;
mutable HashMap<String, InodeIndex> m_lookup_cache;
ext2_inode m_raw_inode;
};
@ -98,8 +96,6 @@ class Ext2FS final : public BlockBasedFS {
friend class Ext2FSInode;
public:
using InodeIndex = u32;
static NonnullRefPtr<Ext2FS> create(FileDescription&);
virtual ~Ext2FS() override;
@ -132,7 +128,7 @@ private:
unsigned inode_size() const;
bool write_ext2_inode(InodeIndex, const ext2_inode&);
bool find_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset) const;
bool find_block_containing_inode(InodeIndex, BlockIndex& block_index, unsigned& offset) const;
bool flush_super_block();

View File

@ -227,7 +227,7 @@ ssize_t FileDescription::get_dir_entries(UserOrKernelBuffer& buffer, ssize_t siz
OutputMemoryStream stream { temp_buffer };
KResult result = VFS::the().traverse_directory_inode(*m_inode, [&stream, this](auto& entry) {
stream << (u32)entry.inode.index();
stream << (u32)entry.inode.index().value();
stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry);
stream << (u32)entry.name.length();
stream << entry.name.bytes();

View File

@ -116,7 +116,7 @@ namespace AK {
template<>
struct Traits<Kernel::InodeIdentifier> : public GenericTraits<Kernel::InodeIdentifier> {
static unsigned hash(const Kernel::InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index()); }
static unsigned hash(const Kernel::InodeIdentifier& inode) { return pair_int_hash(inode.fsid(), inode.index().value()); }
};
}

View File

@ -116,7 +116,7 @@ KResultOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Custody& base, RefPtr<C
return VFS::the().resolve_path(path, base, out_parent, options, symlink_recursion_level);
}
Inode::Inode(FS& fs, unsigned index)
Inode::Inode(FS& fs, InodeIndex index)
: m_fs(fs)
, m_index(index)
{

View File

@ -56,7 +56,7 @@ public:
FS& fs() { return m_fs; }
const FS& fs() const { return m_fs; }
unsigned fsid() const { return m_fs.fsid(); }
unsigned index() const { return m_index; }
InodeIndex index() const { return m_index; }
size_t size() const { return metadata().size; }
bool is_symlink() const { return metadata().is_symlink(); }
@ -127,7 +127,7 @@ public:
static SpinLock<u32>& all_inodes_lock();
protected:
Inode(FS& fs, unsigned index);
Inode(FS& fs, InodeIndex);
void set_metadata_dirty(bool);
void inode_contents_changed(off_t, ssize_t, const UserOrKernelBuffer&);
void inode_size_changed(size_t old_size, size_t new_size);
@ -140,7 +140,7 @@ protected:
private:
FS& m_fs;
unsigned m_index { 0 };
InodeIndex m_index { 0 };
WeakPtr<SharedInodeVMObject> m_shared_vmobject;
RefPtr<LocalSocket> m_socket;
HashTable<InodeWatcher*> m_watchers;

View File

@ -27,6 +27,7 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/DistinctNumeric.h>
#include <AK/String.h>
#include <AK/Types.h>
@ -35,10 +36,12 @@ namespace Kernel {
class FS;
struct InodeMetadata;
TYPEDEF_DISTINCT_ORDERED_ID(unsigned, InodeIndex);
class InodeIdentifier {
public:
InodeIdentifier() { }
InodeIdentifier(u32 fsid, u32 inode)
InodeIdentifier(u32 fsid, InodeIndex inode)
: m_fsid(fsid)
, m_index(inode)
{
@ -47,7 +50,7 @@ public:
bool is_valid() const { return m_fsid != 0 && m_index != 0; }
u32 fsid() const { return m_fsid; }
u32 index() const { return m_index; }
InodeIndex index() const { return m_index; }
FS* fs();
const FS* fs() const;
@ -66,12 +69,12 @@ public:
private:
u32 m_fsid { 0 };
u32 m_index { 0 };
InodeIndex m_index { 0 };
};
inline const LogStream& operator<<(const LogStream& stream, const InodeIdentifier& value)
{
stream << value.fsid() << ':' << value.index();
stream << value.fsid() << ':' << value.index().value();
return stream;
}
@ -84,3 +87,11 @@ struct AK::Formatter<Kernel::InodeIdentifier> : AK::Formatter<FormatString> {
return AK::Formatter<FormatString>::format(builder, "{}:{}", value.fsid(), value.index());
}
};
template<>
struct AK::Formatter<Kernel::InodeIndex> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::InodeIndex value)
{
return AK::Formatter<FormatString>::format(builder, "{}", value.value());
}
};

View File

@ -108,7 +108,7 @@ struct InodeMetadata {
if (!is_valid())
return EIO;
buffer.st_rdev = encoded_device(major_device, minor_device);
buffer.st_ino = inode.index();
buffer.st_ino = inode.index().value();
buffer.st_mode = mode;
buffer.st_nlink = link_count;
buffer.st_uid = uid;

View File

@ -104,14 +104,14 @@ void InodeWatcher::notify_inode_event(Badge<Inode>, InodeWatcherEvent::Type even
void InodeWatcher::notify_child_added(Badge<Inode>, const InodeIdentifier& child_id)
{
LOCKER(m_lock);
m_queue.enqueue({ InodeWatcherEvent::Type::ChildAdded, child_id.index() });
m_queue.enqueue({ InodeWatcherEvent::Type::ChildAdded, child_id.index().value() });
evaluate_block_conditions();
}
void InodeWatcher::notify_child_removed(Badge<Inode>, const InodeIdentifier& child_id)
{
LOCKER(m_lock);
m_queue.enqueue({ InodeWatcherEvent::Type::ChildRemoved, child_id.index() });
m_queue.enqueue({ InodeWatcherEvent::Type::ChildRemoved, child_id.index().value() });
evaluate_block_conditions();
}

View File

@ -171,7 +171,7 @@ class Plan9FSInode final : public Inode {
public:
virtual ~Plan9FSInode() override;
u32 fid() const { return index(); }
u32 fid() const { return index().value(); }
// ^Inode
virtual InodeMetadata metadata() const override;

View File

@ -128,7 +128,7 @@ enum ProcFileType {
static inline ProcessID to_pid(const InodeIdentifier& identifier)
{
return identifier.index() >> 16u;
return identifier.index().value() >> 16u;
}
static inline ThreadID to_tid(const InodeIdentifier& identifier)
@ -139,25 +139,25 @@ static inline ThreadID to_tid(const InodeIdentifier& identifier)
static inline ProcParentDirectory to_proc_parent_directory(const InodeIdentifier& identifier)
{
return (ProcParentDirectory)((identifier.index() >> 12) & 0xf);
return (ProcParentDirectory)((identifier.index().value() >> 12) & 0xf);
}
static inline ProcFileType to_proc_file_type(const InodeIdentifier& identifier)
{
return (ProcFileType)(identifier.index() & 0xff);
return (ProcFileType)(identifier.index().value() & 0xff);
}
static inline int to_fd(const InodeIdentifier& identifier)
{
ASSERT(to_proc_parent_directory(identifier) == PDI_PID_fd);
return (identifier.index() & 0xff) - FI_MaxStaticFileIndex;
return (identifier.index().value() & 0xff) - FI_MaxStaticFileIndex;
}
static inline size_t to_sys_index(const InodeIdentifier& identifier)
{
ASSERT(to_proc_parent_directory(identifier) == PDI_Root_sys);
ASSERT(to_proc_file_type(identifier) == FI_Root_sys_variable);
return identifier.index() >> 16u;
return identifier.index().value() >> 16u;
}
static inline InodeIdentifier to_identifier(unsigned fsid, ProcParentDirectory parent, ProcessID pid, ProcFileType proc_file_type)
@ -1020,7 +1020,7 @@ RefPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
return m_root_inode;
LOCKER(m_inodes_lock);
auto it = m_inodes.find(inode_id.index());
auto it = m_inodes.find(inode_id.index().value());
if (it != m_inodes.end()) {
// It's possible that the ProcFSInode ref count was dropped to 0 or
// the ~ProcFSInode destructor is even running already, but blocked
@ -1032,12 +1032,12 @@ RefPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
// We couldn't ref it, so just create a new one and replace the entry
}
auto inode = adopt(*new ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
auto result = m_inodes.set(inode_id.index(), inode.ptr());
auto result = m_inodes.set(inode_id.index().value(), inode.ptr());
ASSERT(result == ((it == m_inodes.end()) ? AK::HashSetResult::InsertedNewEntry : AK::HashSetResult::ReplacedExistingEntry));
return inode;
}
ProcFSInode::ProcFSInode(ProcFS& fs, unsigned index)
ProcFSInode::ProcFSInode(ProcFS& fs, InodeIndex index)
: Inode(fs, index)
{
}
@ -1045,7 +1045,7 @@ ProcFSInode::ProcFSInode(ProcFS& fs, unsigned index)
ProcFSInode::~ProcFSInode()
{
LOCKER(fs().m_inodes_lock);
auto it = fs().m_inodes.find(index());
auto it = fs().m_inodes.find(index().value());
if (it != fs().m_inodes.end() && it->value == this)
fs().m_inodes.remove(it);
}

View File

@ -116,7 +116,7 @@ private:
ProcFS& fs() { return static_cast<ProcFS&>(Inode::fs()); }
const ProcFS& fs() const { return static_cast<const ProcFS&>(Inode::fs()); }
ProcFSInode(ProcFS&, unsigned index);
ProcFSInode(ProcFS&, InodeIndex);
};
class ProcFSProxyInode final : public Inode {

View File

@ -62,7 +62,7 @@ void TmpFS::register_inode(TmpFSInode& inode)
LOCKER(m_lock);
ASSERT(inode.identifier().fsid() == fsid());
unsigned index = inode.identifier().index();
auto index = inode.identifier().index();
m_inodes.set(index, inode);
}

View File

@ -55,7 +55,7 @@ private:
RefPtr<TmpFSInode> m_root_inode;
HashMap<unsigned, NonnullRefPtr<TmpFSInode>> m_inodes;
HashMap<InodeIndex, NonnullRefPtr<TmpFSInode>> m_inodes;
RefPtr<Inode> get_inode(InodeIdentifier identifier) const;
void register_inode(TmpFSInode&);
void unregister_inode(InodeIdentifier);

View File

@ -146,7 +146,7 @@ bool VFS::mount_root(FS& file_system)
auto root_inode = file_system.root_inode();
if (!root_inode->is_directory()) {
klog() << "VFS: root inode (" << String::format("%02u", file_system.fsid()) << ":" << String::format("%08u", root_inode->index()) << ") for / is not a directory :(";
klog() << "VFS: root inode (" << String::format("%02u", file_system.fsid()) << ":" << String::format("%08u", root_inode->index().value()) << ") for / is not a directory :(";
return false;
}