mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00:00
Kernel/DevFS: Simplify nodes insertion and lookup
Use IntrusiveList instead of a Vector to add inodes to a directory.
This commit is contained in:
parent
f633fb706e
commit
fcc046047f
2 changed files with 20 additions and 68 deletions
|
@ -25,8 +25,7 @@ void DevFS::notify_new_device(Device& device)
|
||||||
auto name = KString::try_create(device.device_name()).release_value();
|
auto name = KString::try_create(device.device_name()).release_value();
|
||||||
MutexLocker locker(m_lock);
|
MutexLocker locker(m_lock);
|
||||||
auto new_device_inode = adopt_ref(*new DevFSDeviceInode(*this, device, move(name)));
|
auto new_device_inode = adopt_ref(*new DevFSDeviceInode(*this, device, move(name)));
|
||||||
m_nodes.append(new_device_inode);
|
m_root_inode->m_nodes.append(new_device_inode);
|
||||||
m_root_inode->m_devices.append(new_device_inode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t DevFS::allocate_inode_index()
|
size_t DevFS::allocate_inode_index()
|
||||||
|
@ -63,18 +62,6 @@ Inode& DevFS::root_inode()
|
||||||
return *m_root_inode;
|
return *m_root_inode;
|
||||||
}
|
}
|
||||||
|
|
||||||
KResultOr<NonnullRefPtr<Inode>> DevFS::get_inode(InodeIdentifier inode_id) const
|
|
||||||
{
|
|
||||||
MutexLocker locker(m_lock);
|
|
||||||
if (inode_id.index() == 1)
|
|
||||||
return *m_root_inode;
|
|
||||||
for (auto& node : m_nodes) {
|
|
||||||
if (inode_id.index() == node.index())
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
return ENOENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
DevFSInode::DevFSInode(DevFS& fs)
|
DevFSInode::DevFSInode(DevFS& fs)
|
||||||
: Inode(fs, fs.allocate_inode_index())
|
: Inode(fs, fs.allocate_inode_index())
|
||||||
{
|
{
|
||||||
|
@ -211,19 +198,9 @@ KResult DevFSRootDirectoryInode::traverse_as_directory(Function<bool(FileSystem:
|
||||||
MutexLocker locker(fs().m_lock);
|
MutexLocker locker(fs().m_lock);
|
||||||
callback({ ".", identifier(), 0 });
|
callback({ ".", identifier(), 0 });
|
||||||
callback({ "..", identifier(), 0 });
|
callback({ "..", identifier(), 0 });
|
||||||
|
for (auto& node : m_nodes) {
|
||||||
for (auto& directory : m_subdirectories) {
|
InodeIdentifier identifier = { fsid(), node.index() };
|
||||||
InodeIdentifier identifier = { fsid(), directory.index() };
|
callback({ node.name(), identifier, 0 });
|
||||||
callback({ directory.name(), identifier, 0 });
|
|
||||||
}
|
|
||||||
for (auto& link : m_links) {
|
|
||||||
InodeIdentifier identifier = { fsid(), link.index() };
|
|
||||||
callback({ link.name(), identifier, 0 });
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& device_node : m_devices) {
|
|
||||||
InodeIdentifier identifier = { fsid(), device_node.index() };
|
|
||||||
callback({ device_node.name(), identifier, 0 });
|
|
||||||
}
|
}
|
||||||
return KSuccess;
|
return KSuccess;
|
||||||
}
|
}
|
||||||
|
@ -231,19 +208,9 @@ KResult DevFSRootDirectoryInode::traverse_as_directory(Function<bool(FileSystem:
|
||||||
KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::lookup(StringView name)
|
KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::lookup(StringView name)
|
||||||
{
|
{
|
||||||
MutexLocker locker(fs().m_lock);
|
MutexLocker locker(fs().m_lock);
|
||||||
for (auto& subdirectory : m_subdirectories) {
|
for (auto& node : m_nodes) {
|
||||||
if (subdirectory.name() == name)
|
if (node.name() == name)
|
||||||
return subdirectory;
|
return node;
|
||||||
}
|
|
||||||
for (auto& link : m_links) {
|
|
||||||
if (link.name() == name)
|
|
||||||
return link;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& device_node : m_devices) {
|
|
||||||
if (device_node.name() == name) {
|
|
||||||
return device_node;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ENOENT;
|
return ENOENT;
|
||||||
}
|
}
|
||||||
|
@ -251,37 +218,24 @@ KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::create_child(StringView
|
||||||
{
|
{
|
||||||
MutexLocker locker(fs().m_lock);
|
MutexLocker locker(fs().m_lock);
|
||||||
|
|
||||||
|
for (auto& node : m_nodes) {
|
||||||
|
if (node.name() == name)
|
||||||
|
return KResult(EEXIST);
|
||||||
|
}
|
||||||
|
|
||||||
InodeMetadata metadata;
|
InodeMetadata metadata;
|
||||||
metadata.mode = mode;
|
metadata.mode = mode;
|
||||||
if (metadata.is_directory()) {
|
if (metadata.is_directory()) {
|
||||||
for (auto& directory : m_subdirectories) {
|
|
||||||
if (directory.name() == name)
|
|
||||||
return EEXIST;
|
|
||||||
}
|
|
||||||
if (name != "pts")
|
if (name != "pts")
|
||||||
return EROFS;
|
return EROFS;
|
||||||
auto new_directory_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevFSPtsDirectoryInode(fs())));
|
auto new_directory_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevFSPtsDirectoryInode(fs())));
|
||||||
if (!m_subdirectories.try_ensure_capacity(m_subdirectories.size() + 1))
|
m_nodes.append(*new_directory_inode);
|
||||||
return ENOMEM;
|
return new_directory_inode;
|
||||||
if (!fs().m_nodes.try_ensure_capacity(fs().m_nodes.size() + 1))
|
|
||||||
return ENOMEM;
|
|
||||||
m_subdirectories.append(*new_directory_inode);
|
|
||||||
fs().m_nodes.append(*new_directory_inode);
|
|
||||||
return KResult(KSuccess);
|
|
||||||
}
|
}
|
||||||
if (metadata.is_symlink()) {
|
if (metadata.is_symlink()) {
|
||||||
for (auto& link : m_links) {
|
|
||||||
if (link.name() == name)
|
|
||||||
return EEXIST;
|
|
||||||
}
|
|
||||||
auto name_kstring = TRY(KString::try_create(name));
|
auto name_kstring = TRY(KString::try_create(name));
|
||||||
auto new_link_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevFSLinkInode(fs(), move(name_kstring))));
|
auto new_link_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevFSLinkInode(fs(), move(name_kstring))));
|
||||||
if (!m_links.try_ensure_capacity(m_links.size() + 1))
|
m_nodes.append(*new_link_inode);
|
||||||
return ENOMEM;
|
|
||||||
if (!fs().m_nodes.try_ensure_capacity(fs().m_nodes.size() + 1))
|
|
||||||
return ENOMEM;
|
|
||||||
m_links.append(*new_link_inode);
|
|
||||||
fs().m_nodes.append(*new_link_inode);
|
|
||||||
return new_link_inode;
|
return new_link_inode;
|
||||||
}
|
}
|
||||||
return EROFS;
|
return EROFS;
|
||||||
|
|
|
@ -32,18 +32,16 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DevFS();
|
DevFS();
|
||||||
KResultOr<NonnullRefPtr<Inode>> get_inode(InodeIdentifier) const;
|
|
||||||
size_t allocate_inode_index();
|
size_t allocate_inode_index();
|
||||||
|
|
||||||
RefPtr<DevFSRootDirectoryInode> m_root_inode;
|
RefPtr<DevFSRootDirectoryInode> m_root_inode;
|
||||||
NonnullRefPtrVector<DevFSInode> m_nodes;
|
|
||||||
|
|
||||||
InodeIndex m_next_inode_index { 0 };
|
InodeIndex m_next_inode_index { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
class DevFSInode : public Inode {
|
class DevFSInode : public Inode {
|
||||||
friend class DevFS;
|
friend class DevFS;
|
||||||
friend class DevFSRootDirectoryInode;
|
friend class DevFSRootDirectoryInode;
|
||||||
|
friend class DevFSDirectoryInode;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual StringView name() const = 0;
|
virtual StringView name() const = 0;
|
||||||
|
@ -64,6 +62,9 @@ protected:
|
||||||
virtual KResult chmod(mode_t) override;
|
virtual KResult chmod(mode_t) override;
|
||||||
virtual KResult chown(UserID, GroupID) override;
|
virtual KResult chown(UserID, GroupID) override;
|
||||||
virtual KResult truncate(u64) override;
|
virtual KResult truncate(u64) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
IntrusiveListNode<DevFSInode, RefPtr<DevFSInode>> m_list_node;
|
||||||
};
|
};
|
||||||
|
|
||||||
class DevFSDeviceInode : public DevFSInode {
|
class DevFSDeviceInode : public DevFSInode {
|
||||||
|
@ -120,7 +121,7 @@ protected:
|
||||||
// ^Inode
|
// ^Inode
|
||||||
virtual InodeMetadata metadata() const override;
|
virtual InodeMetadata metadata() const override;
|
||||||
|
|
||||||
NonnullRefPtrVector<DevFSDeviceInode> m_devices;
|
IntrusiveList<DevFSInode, NonnullRefPtr<DevFSInode>, &DevFSInode::m_list_node> m_nodes;
|
||||||
};
|
};
|
||||||
|
|
||||||
class DevFSPtsDirectoryInode final : public DevFSDirectoryInode {
|
class DevFSPtsDirectoryInode final : public DevFSDirectoryInode {
|
||||||
|
@ -151,9 +152,6 @@ private:
|
||||||
virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
|
virtual KResult traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
|
||||||
virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override;
|
virtual KResultOr<NonnullRefPtr<Inode>> lookup(StringView name) override;
|
||||||
virtual InodeMetadata metadata() const override;
|
virtual InodeMetadata metadata() const override;
|
||||||
|
|
||||||
NonnullRefPtrVector<DevFSDirectoryInode> m_subdirectories;
|
|
||||||
NonnullRefPtrVector<DevFSLinkInode> m_links;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue