summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2021-09-10 14:44:46 +0300
committerAndreas Kling <kling@serenityos.org>2021-09-11 11:41:14 +0200
commitf5de4f24b260515f703a3a6ea183046f45cd4ce8 (patch)
treedadab2487c0fd610f188151ff1bfdcc6bb7c84b8 /Kernel
parentc545d4ffcb93e95d8226dff996fcb38f92c48d14 (diff)
downloadserenity-f5de4f24b260515f703a3a6ea183046f45cd4ce8.zip
Kernel/Devices: Defer creation of SysFS component after the constructor
Instead of doing so in the constructor, let's do immediately after the constructor, so we can safely pass a reference of a Device, so the SysFSDeviceComponent constructor can use that object to identify whether it's a block device or a character device. This allows to us to not hold a device in SysFSDeviceComponent with a RefPtr. Also, we also call the before_removing method in both SlavePTY::unref and File::unref, so because Device has that method being overrided, it can ensure the device is removed always cleanly.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/ConsoleDevice.cpp1
-rw-r--r--Kernel/Devices/Device.cpp23
-rw-r--r--Kernel/Devices/Device.h14
-rw-r--r--Kernel/Devices/FullDevice.cpp5
-rw-r--r--Kernel/Devices/FullDevice.h3
-rw-r--r--Kernel/Devices/HID/HIDManagement.cpp1
-rw-r--r--Kernel/Devices/HID/PS2KeyboardDevice.cpp8
-rw-r--r--Kernel/Devices/HID/PS2KeyboardDevice.h3
-rw-r--r--Kernel/Devices/HID/PS2MouseDevice.cpp8
-rw-r--r--Kernel/Devices/HID/PS2MouseDevice.h4
-rw-r--r--Kernel/Devices/HID/VMWareMouseDevice.cpp8
-rw-r--r--Kernel/Devices/HID/VMWareMouseDevice.h2
-rw-r--r--Kernel/Devices/KCOVDevice.cpp5
-rw-r--r--Kernel/Devices/KCOVDevice.h6
-rw-r--r--Kernel/Devices/MemoryDevice.cpp5
-rw-r--r--Kernel/Devices/MemoryDevice.h4
-rw-r--r--Kernel/Devices/NullDevice.cpp1
-rw-r--r--Kernel/Devices/RandomDevice.cpp5
-rw-r--r--Kernel/Devices/RandomDevice.h3
-rw-r--r--Kernel/Devices/SB16.cpp1
-rw-r--r--Kernel/Devices/ZeroDevice.cpp5
-rw-r--r--Kernel/Devices/ZeroDevice.h4
-rw-r--r--Kernel/FileSystem/File.cpp1
-rw-r--r--Kernel/FileSystem/File.h1
-rw-r--r--Kernel/FileSystem/SysFS.h4
-rw-r--r--Kernel/Graphics/FramebufferDevice.cpp5
-rw-r--r--Kernel/Graphics/FramebufferDevice.h5
-rw-r--r--Kernel/Graphics/VirtIOGPU/GPU.cpp1
-rw-r--r--Kernel/Storage/PATADiskDevice.cpp5
-rw-r--r--Kernel/Storage/PATADiskDevice.h3
-rw-r--r--Kernel/Storage/Partition/DiskPartition.cpp5
-rw-r--r--Kernel/Storage/Partition/DiskPartition.h5
-rw-r--r--Kernel/Storage/RamdiskDevice.cpp5
-rw-r--r--Kernel/Storage/SATADiskDevice.cpp5
-rw-r--r--Kernel/Storage/SATADiskDevice.h3
-rw-r--r--Kernel/TTY/MasterPTY.cpp2
-rw-r--r--Kernel/TTY/PTYMultiplexer.cpp5
-rw-r--r--Kernel/TTY/PTYMultiplexer.h5
-rw-r--r--Kernel/TTY/SlavePTY.cpp4
-rw-r--r--Kernel/TTY/VirtualConsole.cpp10
-rw-r--r--Kernel/TTY/VirtualConsole.h4
41 files changed, 141 insertions, 56 deletions
diff --git a/Kernel/ConsoleDevice.cpp b/Kernel/ConsoleDevice.cpp
index 69d1ceff84..2bb5569a0c 100644
--- a/Kernel/ConsoleDevice.cpp
+++ b/Kernel/ConsoleDevice.cpp
@@ -20,6 +20,7 @@ static Kernel::Spinlock g_console_lock;
UNMAP_AFTER_INIT void ConsoleDevice::initialize()
{
s_the.ensure_instance();
+ s_the->after_inserting();
}
ConsoleDevice& ConsoleDevice::the()
diff --git a/Kernel/Devices/Device.cpp b/Kernel/Devices/Device.cpp
index e74365e2d0..e3b809cb4e 100644
--- a/Kernel/Devices/Device.cpp
+++ b/Kernel/Devices/Device.cpp
@@ -26,8 +26,9 @@ NonnullRefPtr<SysFSDeviceComponent> SysFSDeviceComponent::must_create(Device con
}
SysFSDeviceComponent::SysFSDeviceComponent(Device const& device)
: SysFSComponent(String::formatted("{}:{}", device.major(), device.minor()))
- , m_associated_device(device)
+ , m_block_device(device.is_block_device())
{
+ VERIFY(device.is_block_device() || device.is_character_device());
}
UNMAP_AFTER_INIT NonnullRefPtr<SysFSDevicesDirectory> SysFSDevicesDirectory::must_create(SysFSRootDirectory const& root_directory)
@@ -58,7 +59,7 @@ KResult SysFSBlockDevicesDirectory::traverse_as_directory(unsigned fsid, Functio
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
for (auto& exposed_device : list) {
- if (!exposed_device.device().is_block_device())
+ if (!exposed_device.is_block_device())
continue;
callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 });
}
@@ -69,7 +70,7 @@ RefPtr<SysFSComponent> SysFSBlockDevicesDirectory::lookup(StringView name)
{
return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> RefPtr<SysFSComponent> {
for (auto& exposed_device : list) {
- if (!exposed_device.device().is_block_device())
+ if (!exposed_device.is_block_device())
continue;
if (exposed_device.name() == name)
return exposed_device;
@@ -94,7 +95,7 @@ KResult SysFSCharacterDevicesDirectory::traverse_as_directory(unsigned fsid, Fun
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
for (auto& exposed_device : list) {
- if (!exposed_device.device().is_character_device())
+ if (exposed_device.is_block_device())
continue;
callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 });
}
@@ -105,7 +106,7 @@ RefPtr<SysFSComponent> SysFSCharacterDevicesDirectory::lookup(StringView name)
{
return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> RefPtr<SysFSComponent> {
for (auto& exposed_device : list) {
- if (!exposed_device.device().is_character_device())
+ if (exposed_device.is_block_device())
continue;
if (exposed_device.name() == name)
return exposed_device;
@@ -145,6 +146,11 @@ Device::Device(unsigned major, unsigned minor)
VERIFY(!map.contains(device_id));
map.set(device_id, this);
});
+}
+
+void Device::after_inserting()
+{
+ VERIFY(!m_sysfs_component);
auto sys_fs_component = SysFSDeviceComponent::must_create(*this);
m_sysfs_component = sys_fs_component;
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
@@ -154,12 +160,11 @@ Device::Device(unsigned major, unsigned minor)
void Device::before_removing()
{
- auto sys_fs_component = m_sysfs_component.strong_ref();
- VERIFY(sys_fs_component);
+ m_state = State::BeingRemoved;
+ VERIFY(m_sysfs_component);
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
- list.remove(*sys_fs_component);
+ list.remove(*m_sysfs_component);
});
- m_state = State::BeingRemoved;
}
Device::~Device()
diff --git a/Kernel/Devices/Device.h b/Kernel/Devices/Device.h
index cc1fc51827..e981d76d73 100644
--- a/Kernel/Devices/Device.h
+++ b/Kernel/Devices/Device.h
@@ -18,6 +18,7 @@
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/RefPtr.h>
+#include <Kernel/API/KResult.h>
#include <Kernel/Devices/AsyncDeviceRequest.h>
#include <Kernel/FileSystem/File.h>
#include <Kernel/FileSystem/SysFS.h>
@@ -26,6 +27,14 @@
namespace Kernel {
+template<typename DeviceType, typename... Args>
+inline KResultOr<NonnullRefPtr<DeviceType>> try_create_device(Args&&... args)
+{
+ auto device = TRY(adopt_nonnull_ref_or_enomem(new DeviceType(forward<Args>(args)...)));
+ device->after_inserting();
+ return device;
+}
+
class Device : public File {
protected:
enum class State {
@@ -46,7 +55,8 @@ public:
GroupID gid() const { return m_gid; }
virtual bool is_device() const override { return true; }
- virtual void before_removing();
+ virtual void before_removing() override;
+ virtual void after_inserting();
static void for_each(Function<void(Device&)>);
static Device* get_device(unsigned major, unsigned minor);
@@ -82,7 +92,7 @@ private:
Spinlock m_requests_lock;
DoublyLinkedList<RefPtr<AsyncDeviceRequest>> m_requests;
- WeakPtr<SysFSDeviceComponent> m_sysfs_component;
+ RefPtr<SysFSDeviceComponent> m_sysfs_component;
};
}
diff --git a/Kernel/Devices/FullDevice.cpp b/Kernel/Devices/FullDevice.cpp
index b8b0488722..df425cfcea 100644
--- a/Kernel/Devices/FullDevice.cpp
+++ b/Kernel/Devices/FullDevice.cpp
@@ -13,7 +13,10 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<FullDevice> FullDevice::must_create()
{
- return adopt_ref(*new FullDevice);
+ auto full_device_or_error = try_create_device<FullDevice>();
+ // FIXME: Find a way to propagate errors
+ VERIFY(!full_device_or_error.is_error());
+ return full_device_or_error.release_value();
}
UNMAP_AFTER_INIT FullDevice::FullDevice()
diff --git a/Kernel/Devices/FullDevice.h b/Kernel/Devices/FullDevice.h
index ded36209de..3a3f7e09b9 100644
--- a/Kernel/Devices/FullDevice.h
+++ b/Kernel/Devices/FullDevice.h
@@ -16,9 +16,10 @@ public:
static NonnullRefPtr<FullDevice> must_create();
virtual ~FullDevice() override;
-private:
+ // FIXME: We expose this constructor to make try_create_device helper to work
FullDevice();
+private:
// ^CharacterDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
diff --git a/Kernel/Devices/HID/HIDManagement.cpp b/Kernel/Devices/HID/HIDManagement.cpp
index 3461e3f7d6..0fdc78839b 100644
--- a/Kernel/Devices/HID/HIDManagement.cpp
+++ b/Kernel/Devices/HID/HIDManagement.cpp
@@ -112,6 +112,7 @@ UNMAP_AFTER_INIT void HIDManagement::enumerate()
m_i8042_controller->detect_devices();
if (m_i8042_controller->mouse())
m_hid_devices.append(m_i8042_controller->mouse().release_nonnull());
+
if (m_i8042_controller->keyboard())
m_hid_devices.append(m_i8042_controller->keyboard().release_nonnull());
}
diff --git a/Kernel/Devices/HID/PS2KeyboardDevice.cpp b/Kernel/Devices/HID/PS2KeyboardDevice.cpp
index e81ea7fa0c..0b569b632f 100644
--- a/Kernel/Devices/HID/PS2KeyboardDevice.cpp
+++ b/Kernel/Devices/HID/PS2KeyboardDevice.cpp
@@ -83,9 +83,11 @@ bool PS2KeyboardDevice::handle_irq(const RegisterState&)
UNMAP_AFTER_INIT RefPtr<PS2KeyboardDevice> PS2KeyboardDevice::try_to_initialize(const I8042Controller& ps2_controller)
{
- auto device = adopt_ref(*new PS2KeyboardDevice(ps2_controller));
- if (device->initialize())
- return device;
+ auto keyboard_device_or_error = try_create_device<PS2KeyboardDevice>(ps2_controller);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!keyboard_device_or_error.is_error());
+ if (keyboard_device_or_error.value()->initialize())
+ return keyboard_device_or_error.release_value();
return nullptr;
}
diff --git a/Kernel/Devices/HID/PS2KeyboardDevice.h b/Kernel/Devices/HID/PS2KeyboardDevice.h
index 8fb9c285cd..cd8feca328 100644
--- a/Kernel/Devices/HID/PS2KeyboardDevice.h
+++ b/Kernel/Devices/HID/PS2KeyboardDevice.h
@@ -35,9 +35,10 @@ public:
enable_irq();
}
-private:
+ // FIXME: We expose this constructor to make try_create_device helper to work
explicit PS2KeyboardDevice(const I8042Controller&);
+private:
// ^IRQHandler
virtual bool handle_irq(const RegisterState&) override;
diff --git a/Kernel/Devices/HID/PS2MouseDevice.cpp b/Kernel/Devices/HID/PS2MouseDevice.cpp
index 15dec13ae1..fce0609e2c 100644
--- a/Kernel/Devices/HID/PS2MouseDevice.cpp
+++ b/Kernel/Devices/HID/PS2MouseDevice.cpp
@@ -174,9 +174,11 @@ void PS2MouseDevice::set_sample_rate(u8 rate)
UNMAP_AFTER_INIT RefPtr<PS2MouseDevice> PS2MouseDevice::try_to_initialize(const I8042Controller& ps2_controller)
{
- auto device = adopt_ref(*new PS2MouseDevice(ps2_controller));
- if (device->initialize())
- return device;
+ auto mouse_device_or_error = try_create_device<PS2MouseDevice>(ps2_controller);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!mouse_device_or_error.is_error());
+ if (mouse_device_or_error.value()->initialize())
+ return mouse_device_or_error.release_value();
return nullptr;
}
diff --git a/Kernel/Devices/HID/PS2MouseDevice.h b/Kernel/Devices/HID/PS2MouseDevice.h
index 1182ce2946..fc3b84cd00 100644
--- a/Kernel/Devices/HID/PS2MouseDevice.h
+++ b/Kernel/Devices/HID/PS2MouseDevice.h
@@ -32,8 +32,10 @@ public:
enable_irq();
}
-protected:
+ // FIXME: We expose this constructor to make try_create_device helper to work
explicit PS2MouseDevice(const I8042Controller&);
+
+protected:
// ^IRQHandler
virtual bool handle_irq(const RegisterState&) override;
diff --git a/Kernel/Devices/HID/VMWareMouseDevice.cpp b/Kernel/Devices/HID/VMWareMouseDevice.cpp
index 46ca4b3984..291cde6a78 100644
--- a/Kernel/Devices/HID/VMWareMouseDevice.cpp
+++ b/Kernel/Devices/HID/VMWareMouseDevice.cpp
@@ -16,9 +16,11 @@ UNMAP_AFTER_INIT RefPtr<VMWareMouseDevice> VMWareMouseDevice::try_to_initialize(
return {};
if (!VMWareBackdoor::the()->vmmouse_is_absolute())
return {};
- auto device = adopt_ref(*new VMWareMouseDevice(ps2_controller));
- if (device->initialize())
- return device;
+ auto mouse_device_or_error = try_create_device<VMWareMouseDevice>(ps2_controller);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!mouse_device_or_error.is_error());
+ if (mouse_device_or_error.value()->initialize())
+ return mouse_device_or_error.release_value();
return {};
}
diff --git a/Kernel/Devices/HID/VMWareMouseDevice.h b/Kernel/Devices/HID/VMWareMouseDevice.h
index 60749639bb..7f17e27392 100644
--- a/Kernel/Devices/HID/VMWareMouseDevice.h
+++ b/Kernel/Devices/HID/VMWareMouseDevice.h
@@ -23,7 +23,7 @@ public:
// ^I8042Device
virtual void irq_handle_byte_read(u8 byte) override;
-private:
+ // FIXME: We expose this constructor to make try_create_device helper to work
explicit VMWareMouseDevice(const I8042Controller&);
};
diff --git a/Kernel/Devices/KCOVDevice.cpp b/Kernel/Devices/KCOVDevice.cpp
index 712d5df7b9..0d3dc59674 100644
--- a/Kernel/Devices/KCOVDevice.cpp
+++ b/Kernel/Devices/KCOVDevice.cpp
@@ -20,7 +20,10 @@ HashMap<ThreadID, KCOVInstance*>* KCOVDevice::thread_instance;
UNMAP_AFTER_INIT NonnullRefPtr<KCOVDevice> KCOVDevice::must_create()
{
- return adopt_ref(*new KCOVDevice);
+ auto kcov_device_or_error = try_create_device<KCOVDevice>();
+ // FIXME: Find a way to propagate errors
+ VERIFY(!kcov_device_or_error.is_error());
+ return kcov_device_or_error.release_value();
}
UNMAP_AFTER_INIT KCOVDevice::KCOVDevice()
diff --git a/Kernel/Devices/KCOVDevice.h b/Kernel/Devices/KCOVDevice.h
index 56002ac545..0b7fcc8799 100644
--- a/Kernel/Devices/KCOVDevice.h
+++ b/Kernel/Devices/KCOVDevice.h
@@ -25,6 +25,9 @@ public:
KResultOr<Memory::Region*> mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override;
KResultOr<NonnullRefPtr<OpenFileDescription>> open(int options) override;
+ // FIXME: We expose this constructor to make try_create_device helper to work
+ KCOVDevice();
+
protected:
virtual StringView class_name() const override { return "KCOVDevice"; }
@@ -34,9 +37,6 @@ protected:
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override { return EINVAL; }
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; }
virtual KResult ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override;
-
-private:
- KCOVDevice();
};
}
diff --git a/Kernel/Devices/MemoryDevice.cpp b/Kernel/Devices/MemoryDevice.cpp
index eb5df7ea02..80020f22ab 100644
--- a/Kernel/Devices/MemoryDevice.cpp
+++ b/Kernel/Devices/MemoryDevice.cpp
@@ -15,7 +15,10 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<MemoryDevice> MemoryDevice::must_create()
{
- return adopt_ref(*new MemoryDevice);
+ auto memory_device_or_error = try_create_device<MemoryDevice>();
+ // FIXME: Find a way to propagate errors
+ VERIFY(!memory_device_or_error.is_error());
+ return memory_device_or_error.release_value();
}
UNMAP_AFTER_INIT MemoryDevice::MemoryDevice()
diff --git a/Kernel/Devices/MemoryDevice.h b/Kernel/Devices/MemoryDevice.h
index 875ff434de..0f40d26a4f 100644
--- a/Kernel/Devices/MemoryDevice.h
+++ b/Kernel/Devices/MemoryDevice.h
@@ -21,8 +21,10 @@ public:
virtual KResultOr<Memory::Region*> mmap(Process&, OpenFileDescription&, Memory::VirtualRange const&, u64 offset, int prot, bool shared) override;
-private:
+ // FIXME: We expose this constructor to make try_create_device helper to work
MemoryDevice();
+
+private:
virtual StringView class_name() const override { return "MemoryDevice"; }
virtual bool can_read(const OpenFileDescription&, size_t) const override { return true; }
virtual bool can_write(const OpenFileDescription&, size_t) const override { return false; }
diff --git a/Kernel/Devices/NullDevice.cpp b/Kernel/Devices/NullDevice.cpp
index 240f90d3f0..ae20bcfeba 100644
--- a/Kernel/Devices/NullDevice.cpp
+++ b/Kernel/Devices/NullDevice.cpp
@@ -15,6 +15,7 @@ static Singleton<NullDevice> s_the;
UNMAP_AFTER_INIT void NullDevice::initialize()
{
s_the.ensure_instance();
+ s_the->after_inserting();
}
NullDevice& NullDevice::the()
diff --git a/Kernel/Devices/RandomDevice.cpp b/Kernel/Devices/RandomDevice.cpp
index d8116ca0dc..1d2ab995e4 100644
--- a/Kernel/Devices/RandomDevice.cpp
+++ b/Kernel/Devices/RandomDevice.cpp
@@ -12,7 +12,10 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<RandomDevice> RandomDevice::must_create()
{
- return adopt_ref(*new RandomDevice);
+ auto random_device_or_error = try_create_device<RandomDevice>();
+ // FIXME: Find a way to propagate errors
+ VERIFY(!random_device_or_error.is_error());
+ return random_device_or_error.release_value();
}
UNMAP_AFTER_INIT RandomDevice::RandomDevice()
diff --git a/Kernel/Devices/RandomDevice.h b/Kernel/Devices/RandomDevice.h
index 52dfcce0bd..8e915cd1c5 100644
--- a/Kernel/Devices/RandomDevice.h
+++ b/Kernel/Devices/RandomDevice.h
@@ -16,9 +16,10 @@ public:
static NonnullRefPtr<RandomDevice> must_create();
virtual ~RandomDevice() override;
-private:
+ // FIXME: We expose this constructor to make try_create_device helper to work
RandomDevice();
+private:
// ^CharacterDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
diff --git a/Kernel/Devices/SB16.cpp b/Kernel/Devices/SB16.cpp
index b0ef199a13..219d77f7bb 100644
--- a/Kernel/Devices/SB16.cpp
+++ b/Kernel/Devices/SB16.cpp
@@ -89,6 +89,7 @@ UNMAP_AFTER_INIT void SB16::detect()
UNMAP_AFTER_INIT void SB16::create()
{
s_the.ensure_instance();
+ s_the->after_inserting();
}
SB16& SB16::the()
diff --git a/Kernel/Devices/ZeroDevice.cpp b/Kernel/Devices/ZeroDevice.cpp
index 880db84502..fad3113f1a 100644
--- a/Kernel/Devices/ZeroDevice.cpp
+++ b/Kernel/Devices/ZeroDevice.cpp
@@ -12,7 +12,10 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<ZeroDevice> ZeroDevice::must_create()
{
- return adopt_ref(*new ZeroDevice);
+ auto zero_device_or_error = try_create_device<ZeroDevice>();
+ // FIXME: Find a way to propagate errors
+ VERIFY(!zero_device_or_error.is_error());
+ return zero_device_or_error.release_value();
}
UNMAP_AFTER_INIT ZeroDevice::ZeroDevice()
diff --git a/Kernel/Devices/ZeroDevice.h b/Kernel/Devices/ZeroDevice.h
index cf68501324..e5a2b6dea2 100644
--- a/Kernel/Devices/ZeroDevice.h
+++ b/Kernel/Devices/ZeroDevice.h
@@ -16,8 +16,10 @@ public:
static NonnullRefPtr<ZeroDevice> must_create();
virtual ~ZeroDevice() override;
-private:
+ // FIXME: We expose this constructor to make try_create_device helper to work
ZeroDevice();
+
+private:
// ^CharacterDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
diff --git a/Kernel/FileSystem/File.cpp b/Kernel/FileSystem/File.cpp
index ac03665b9c..fd98a3a629 100644
--- a/Kernel/FileSystem/File.cpp
+++ b/Kernel/FileSystem/File.cpp
@@ -24,6 +24,7 @@ bool File::unref() const
{
if (deref_base())
return false;
+ const_cast<File&>(*this).before_removing();
delete this;
return true;
}
diff --git a/Kernel/FileSystem/File.h b/Kernel/FileSystem/File.h
index 52e495411d..2740516000 100644
--- a/Kernel/FileSystem/File.h
+++ b/Kernel/FileSystem/File.h
@@ -75,6 +75,7 @@ class File
, public Weakable<File> {
public:
virtual bool unref() const;
+ virtual void before_removing() { }
virtual ~File();
virtual KResultOr<NonnullRefPtr<OpenFileDescription>> open(int options);
diff --git a/Kernel/FileSystem/SysFS.h b/Kernel/FileSystem/SysFS.h
index 08a5c82135..886c31eeb9 100644
--- a/Kernel/FileSystem/SysFS.h
+++ b/Kernel/FileSystem/SysFS.h
@@ -34,12 +34,12 @@ class SysFSDeviceComponent final
public:
static NonnullRefPtr<SysFSDeviceComponent> must_create(Device const&);
- Device const& device() const { return *m_associated_device; }
+ bool is_block_device() { return m_block_device; }
private:
explicit SysFSDeviceComponent(Device const&);
IntrusiveListNode<SysFSDeviceComponent, NonnullRefPtr<SysFSDeviceComponent>> m_list_node;
- RefPtr<Device> m_associated_device;
+ bool m_block_device;
};
class SysFSDevicesDirectory final : public SysFSDirectory {
diff --git a/Kernel/Graphics/FramebufferDevice.cpp b/Kernel/Graphics/FramebufferDevice.cpp
index 03b8b760eb..0d36a3400e 100644
--- a/Kernel/Graphics/FramebufferDevice.cpp
+++ b/Kernel/Graphics/FramebufferDevice.cpp
@@ -22,7 +22,10 @@ namespace Kernel {
NonnullRefPtr<FramebufferDevice> FramebufferDevice::create(const GraphicsDevice& adapter, size_t output_port_index, PhysicalAddress paddr, size_t width, size_t height, size_t pitch)
{
- return adopt_ref(*new FramebufferDevice(adapter, output_port_index, paddr, width, height, pitch));
+ auto framebuffer_device_or_error = try_create_device<FramebufferDevice>(adapter, output_port_index, paddr, width, height, pitch);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!framebuffer_device_or_error.is_error());
+ return framebuffer_device_or_error.release_value();
}
KResultOr<Memory::Region*> FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
diff --git a/Kernel/Graphics/FramebufferDevice.h b/Kernel/Graphics/FramebufferDevice.h
index 00b57226c6..2b4e958bbf 100644
--- a/Kernel/Graphics/FramebufferDevice.h
+++ b/Kernel/Graphics/FramebufferDevice.h
@@ -32,6 +32,9 @@ public:
virtual ~FramebufferDevice() {};
KResult initialize();
+ // FIXME: We expose this constructor to make try_create_device helper to work
+ FramebufferDevice(const GraphicsDevice&, size_t, PhysicalAddress, size_t, size_t, size_t);
+
private:
// ^File
virtual StringView class_name() const override { return "FramebufferDevice"; }
@@ -42,8 +45,6 @@ private:
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override { return EINVAL; }
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override { return EINVAL; }
- FramebufferDevice(const GraphicsDevice&, size_t, PhysicalAddress, size_t, size_t, size_t);
-
PhysicalAddress m_framebuffer_address;
size_t m_framebuffer_pitch { 0 };
size_t m_framebuffer_width { 0 };
diff --git a/Kernel/Graphics/VirtIOGPU/GPU.cpp b/Kernel/Graphics/VirtIOGPU/GPU.cpp
index 36b50f217f..e5b2140bd8 100644
--- a/Kernel/Graphics/VirtIOGPU/GPU.cpp
+++ b/Kernel/Graphics/VirtIOGPU/GPU.cpp
@@ -64,6 +64,7 @@ void GPU::create_framebuffer_devices()
for (size_t i = 0; i < min(m_num_scanouts, VIRTIO_GPU_MAX_SCANOUTS); i++) {
auto& scanout = m_scanouts[i];
scanout.framebuffer = adopt_ref(*new VirtIOGPU::FrameBufferDevice(*this, i));
+ scanout.framebuffer->after_inserting();
scanout.console = Kernel::Graphics::VirtIOGPU::Console::initialize(scanout.framebuffer);
}
}
diff --git a/Kernel/Storage/PATADiskDevice.cpp b/Kernel/Storage/PATADiskDevice.cpp
index 015ff71d9c..d4aab035b0 100644
--- a/Kernel/Storage/PATADiskDevice.cpp
+++ b/Kernel/Storage/PATADiskDevice.cpp
@@ -15,7 +15,10 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<PATADiskDevice> PATADiskDevice::create(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 capabilities, u64 max_addressable_block)
{
- return adopt_ref(*new PATADiskDevice(controller, channel, type, interface_type, capabilities, max_addressable_block));
+ auto device_or_error = try_create_device<PATADiskDevice>(controller, channel, type, interface_type, capabilities, max_addressable_block);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!device_or_error.is_error());
+ return device_or_error.release_value();
}
UNMAP_AFTER_INIT PATADiskDevice::PATADiskDevice(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 capabilities, u64 max_addressable_block)
diff --git a/Kernel/Storage/PATADiskDevice.h b/Kernel/Storage/PATADiskDevice.h
index 4aadb24893..9de2561b36 100644
--- a/Kernel/Storage/PATADiskDevice.h
+++ b/Kernel/Storage/PATADiskDevice.h
@@ -44,9 +44,10 @@ public:
virtual void start_request(AsyncBlockDeviceRequest&) override;
virtual String storage_name() const override;
-private:
+ // FIXME: We expose this constructor to make try_create_device helper to work
PATADiskDevice(const IDEController&, IDEChannel&, DriveType, InterfaceType, u16, u64);
+private:
// ^DiskDevice
virtual StringView class_name() const override;
diff --git a/Kernel/Storage/Partition/DiskPartition.cpp b/Kernel/Storage/Partition/DiskPartition.cpp
index 17d474f011..5352528773 100644
--- a/Kernel/Storage/Partition/DiskPartition.cpp
+++ b/Kernel/Storage/Partition/DiskPartition.cpp
@@ -12,7 +12,10 @@ namespace Kernel {
NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata)
{
- return adopt_ref(*new DiskPartition(device, minor_number, metadata));
+ auto partition_or_error = try_create_device<DiskPartition>(device, minor_number, metadata);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!partition_or_error.is_error());
+ return partition_or_error.release_value();
}
DiskPartition::DiskPartition(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata)
diff --git a/Kernel/Storage/Partition/DiskPartition.h b/Kernel/Storage/Partition/DiskPartition.h
index 46f99a1fcd..1d4bae1c75 100644
--- a/Kernel/Storage/Partition/DiskPartition.h
+++ b/Kernel/Storage/Partition/DiskPartition.h
@@ -28,11 +28,12 @@ public:
const DiskPartitionMetadata& metadata() const;
+ // FIXME: We expose this constructor to make try_create_device helper to work
+ DiskPartition(BlockDevice&, unsigned, DiskPartitionMetadata);
+
private:
virtual StringView class_name() const override;
- DiskPartition(BlockDevice&, unsigned, DiskPartitionMetadata);
-
WeakPtr<BlockDevice> m_device;
DiskPartitionMetadata m_metadata;
};
diff --git a/Kernel/Storage/RamdiskDevice.cpp b/Kernel/Storage/RamdiskDevice.cpp
index 283767b148..5d831edae2 100644
--- a/Kernel/Storage/RamdiskDevice.cpp
+++ b/Kernel/Storage/RamdiskDevice.cpp
@@ -14,7 +14,10 @@ namespace Kernel {
NonnullRefPtr<RamdiskDevice> RamdiskDevice::create(const RamdiskController& controller, NonnullOwnPtr<Memory::Region>&& region, int major, int minor)
{
- return adopt_ref(*new RamdiskDevice(controller, move(region), major, minor));
+ auto device_or_error = try_create_device<RamdiskDevice>(controller, move(region), major, minor);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!device_or_error.is_error());
+ return device_or_error.release_value();
}
RamdiskDevice::RamdiskDevice(const RamdiskController& controller, NonnullOwnPtr<Memory::Region>&& region, int major, int minor)
diff --git a/Kernel/Storage/SATADiskDevice.cpp b/Kernel/Storage/SATADiskDevice.cpp
index 87effd650e..0e7c7bd8d4 100644
--- a/Kernel/Storage/SATADiskDevice.cpp
+++ b/Kernel/Storage/SATADiskDevice.cpp
@@ -14,7 +14,10 @@ namespace Kernel {
NonnullRefPtr<SATADiskDevice> SATADiskDevice::create(const AHCIController& controller, const AHCIPort& port, size_t sector_size, u64 max_addressable_block)
{
- return adopt_ref(*new SATADiskDevice(controller, port, sector_size, max_addressable_block));
+ auto device_or_error = try_create_device<SATADiskDevice>(controller, port, sector_size, max_addressable_block);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!device_or_error.is_error());
+ return device_or_error.release_value();
}
SATADiskDevice::SATADiskDevice(const AHCIController& controller, const AHCIPort& port, size_t sector_size, u64 max_addressable_block)
diff --git a/Kernel/Storage/SATADiskDevice.h b/Kernel/Storage/SATADiskDevice.h
index 8c1138b121..97776b4500 100644
--- a/Kernel/Storage/SATADiskDevice.h
+++ b/Kernel/Storage/SATADiskDevice.h
@@ -32,9 +32,10 @@ public:
virtual void start_request(AsyncBlockDeviceRequest&) override;
virtual String storage_name() const override;
-private:
+ // FIXME: We expose this constructor to make try_create_device helper to work
SATADiskDevice(const AHCIController&, const AHCIPort&, size_t sector_size, u64 max_addressable_block);
+private:
// ^DiskDevice
virtual StringView class_name() const override;
WeakPtr<AHCIPort> m_port;
diff --git a/Kernel/TTY/MasterPTY.cpp b/Kernel/TTY/MasterPTY.cpp
index bb6ad80228..d669ea60b7 100644
--- a/Kernel/TTY/MasterPTY.cpp
+++ b/Kernel/TTY/MasterPTY.cpp
@@ -22,6 +22,8 @@ KResultOr<NonnullRefPtr<MasterPTY>> MasterPTY::try_create(unsigned int index)
auto master_pty = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MasterPTY(index, move(buffer))));
auto slave_pty = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SlavePTY(*master_pty, index)));
master_pty->m_slave = slave_pty;
+ master_pty->after_inserting();
+ slave_pty->after_inserting();
return master_pty;
}
diff --git a/Kernel/TTY/PTYMultiplexer.cpp b/Kernel/TTY/PTYMultiplexer.cpp
index 4cb2e2c10c..22ae1312c0 100644
--- a/Kernel/TTY/PTYMultiplexer.cpp
+++ b/Kernel/TTY/PTYMultiplexer.cpp
@@ -35,6 +35,11 @@ UNMAP_AFTER_INIT PTYMultiplexer::~PTYMultiplexer()
{
}
+void PTYMultiplexer::initialize()
+{
+ the().after_inserting();
+}
+
KResultOr<NonnullRefPtr<OpenFileDescription>> PTYMultiplexer::open(int options)
{
return m_freelist.with_exclusive([&](auto& freelist) -> KResultOr<NonnullRefPtr<OpenFileDescription>> {
diff --git a/Kernel/TTY/PTYMultiplexer.h b/Kernel/TTY/PTYMultiplexer.h
index 9401f3c7e3..8db52a121b 100644
--- a/Kernel/TTY/PTYMultiplexer.h
+++ b/Kernel/TTY/PTYMultiplexer.h
@@ -20,10 +20,7 @@ public:
PTYMultiplexer();
virtual ~PTYMultiplexer() override;
- static void initialize()
- {
- the();
- }
+ static void initialize();
static PTYMultiplexer& the();
// ^CharacterDevice
diff --git a/Kernel/TTY/SlavePTY.cpp b/Kernel/TTY/SlavePTY.cpp
index 5a4e63ef96..e06ff6949c 100644
--- a/Kernel/TTY/SlavePTY.cpp
+++ b/Kernel/TTY/SlavePTY.cpp
@@ -28,8 +28,10 @@ bool SlavePTY::unref() const
m_list_node.remove();
return true;
});
- if (did_hit_zero)
+ if (did_hit_zero) {
+ const_cast<SlavePTY&>(*this).before_removing();
delete this;
+ }
return did_hit_zero;
}
diff --git a/Kernel/TTY/VirtualConsole.cpp b/Kernel/TTY/VirtualConsole.cpp
index 69935ad69a..58361a3a79 100644
--- a/Kernel/TTY/VirtualConsole.cpp
+++ b/Kernel/TTY/VirtualConsole.cpp
@@ -103,12 +103,18 @@ void VirtualConsole::set_graphical(bool graphical)
UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create(size_t index)
{
- return adopt_ref(*new VirtualConsole(index));
+ auto virtual_console_or_error = try_create_device<VirtualConsole>(index);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!virtual_console_or_error.is_error());
+ return virtual_console_or_error.release_value();
}
UNMAP_AFTER_INIT NonnullRefPtr<VirtualConsole> VirtualConsole::create_with_preset_log(size_t index, const CircularQueue<char, 16384>& log)
{
- return adopt_ref(*new VirtualConsole(index, log));
+ auto virtual_console_or_error = try_create_device<VirtualConsole>(index, log);
+ // FIXME: Find a way to propagate errors
+ VERIFY(!virtual_console_or_error.is_error());
+ return virtual_console_or_error.release_value();
}
UNMAP_AFTER_INIT void VirtualConsole::initialize()
diff --git a/Kernel/TTY/VirtualConsole.h b/Kernel/TTY/VirtualConsole.h
index 7f1e17db96..d90e718cbe 100644
--- a/Kernel/TTY/VirtualConsole.h
+++ b/Kernel/TTY/VirtualConsole.h
@@ -84,9 +84,11 @@ public:
void emit_char(char);
-private:
+ // FIXME: We expose these constructors to make try_create_device helper to work
explicit VirtualConsole(const unsigned index);
VirtualConsole(const unsigned index, const CircularQueue<char, 16384>&);
+
+private:
// ^KeyboardClient
virtual void on_key_pressed(KeyEvent) override;