Quellcode durchsuchen

Kernel: Make device generate their own names

Besides removing the monolithic DevFSDeviceInode::determine_name()
method, being able to determine a device's name inside the /dev
hierarchy outside of DevFS has its uses.
Jean-Baptiste Boric vor 4 Jahren
Ursprung
Commit
f64e287b82

+ 1 - 0
Kernel/Console.h

@@ -53,6 +53,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0666; }
+    virtual String device_name() const override { return "console"; }
 
 private:
     CircularQueue<char, 16384> m_logbuffer;

+ 5 - 0
Kernel/Devices/BXVGADevice.cpp

@@ -192,6 +192,11 @@ KResultOr<Region*> BXVGADevice::mmap(Process& process, FileDescription&, Virtual
         shared);
 }
 
+String BXVGADevice::device_name() const
+{
+    return String::formatted("fb{}", minor());
+}
+
 int BXVGADevice::ioctl(FileDescription&, unsigned request, FlatPtr arg)
 {
     REQUIRE_PROMISE(video);

+ 1 - 0
Kernel/Devices/BXVGADevice.h

@@ -46,6 +46,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0660; }
+    virtual String device_name() const override;
 
 private:
     virtual const char* class_name() const override { return "BXVGA"; }

+ 1 - 0
Kernel/Devices/Device.h

@@ -58,6 +58,7 @@ public:
     uid_t gid() const { return m_gid; }
 
     virtual mode_t required_mode() const = 0;
+    virtual String device_name() const = 0;
 
     virtual bool is_device() const override { return true; }
     virtual bool is_disk_device() const { return false; }

+ 1 - 0
Kernel/Devices/FullDevice.h

@@ -38,6 +38,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0600; }
+    virtual String device_name() const override { return "full"; }
 
 private:
     // ^CharacterDevice

+ 1 - 0
Kernel/Devices/KeyboardDevice.h

@@ -76,6 +76,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0440; }
+    virtual String device_name() const override { return "keyboard"; }
 
 private:
     // ^IRQHandler

+ 5 - 0
Kernel/Devices/MBVGADevice.cpp

@@ -114,4 +114,9 @@ int MBVGADevice::ioctl(FileDescription&, unsigned request, FlatPtr arg)
     };
 }
 
+String MBVGADevice::device_name() const
+{
+    return String::formatted("fb{}", minor());
+}
+
 }

+ 1 - 0
Kernel/Devices/MBVGADevice.h

@@ -45,6 +45,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0660; }
+    virtual String device_name() const override;
 
 private:
     virtual const char* class_name() const override { return "MBVGA"; }

+ 1 - 0
Kernel/Devices/NullDevice.h

@@ -41,6 +41,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0666; }
+    virtual String device_name() const override { return "null"; }
 
 private:
     // ^CharacterDevice

+ 1 - 0
Kernel/Devices/PS2MouseDevice.h

@@ -63,6 +63,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0440; }
+    virtual String device_name() const override { return "mouse"; }
 
 private:
     // ^IRQHandler

+ 1 - 0
Kernel/Devices/RandomDevice.h

@@ -38,6 +38,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0666; }
+    virtual String device_name() const override { return "random"; }
 
 private:
     // ^CharacterDevice

+ 1 - 0
Kernel/Devices/SB16.h

@@ -55,6 +55,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0220; }
+    virtual String device_name() const override { return "audio"; }
 
 private:
     // ^IRQHandler

+ 5 - 0
Kernel/Devices/SerialDevice.cpp

@@ -87,6 +87,11 @@ KResultOr<size_t> SerialDevice::write(FileDescription&, size_t, const UserOrKern
     return (size_t)nread;
 }
 
+String SerialDevice::device_name() const
+{
+    return String::formatted("ttyS{}", minor() - 64);
+}
+
 void SerialDevice::initialize()
 {
     set_interrupts(0);

+ 1 - 0
Kernel/Devices/SerialDevice.h

@@ -125,6 +125,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0620; }
+    virtual String device_name() const override;
 
 private:
     // ^CharacterDevice

+ 1 - 0
Kernel/Devices/ZeroDevice.h

@@ -38,6 +38,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0666; }
+    virtual String device_name() const override { return "zero"; }
 
 private:
     // ^CharacterDevice

+ 2 - 69
Kernel/FileSystem/DevFS.cpp

@@ -350,79 +350,12 @@ KResult DevFSDeviceInode::chown(uid_t uid, gid_t gid)
 
 String DevFSDeviceInode::name() const
 {
+    LOCKER(m_lock);
     if (m_cached_name.is_null() || m_cached_name.is_empty())
-        const_cast<DevFSDeviceInode&>(*this).m_cached_name = determine_name();
+        const_cast<DevFSDeviceInode&>(*this).m_cached_name = m_attached_device->device_name();
     return m_cached_name;
 }
 
-String DevFSDeviceInode::determine_name() const
-{
-    LOCKER(m_lock);
-    if (m_attached_device->is_character_device()) {
-        switch (m_attached_device->major()) {
-        case 85:
-            if (m_attached_device->minor() == 1)
-                return "keyboard";
-            ASSERT_NOT_REACHED();
-        case 10:
-            if (m_attached_device->minor() == 1)
-                return "mouse";
-            ASSERT_NOT_REACHED();
-        case 42:
-            if (m_attached_device->minor() == 42)
-                return "audio";
-            ASSERT_NOT_REACHED();
-        case 1:
-            switch (m_attached_device->minor()) {
-            case 8:
-                return "random";
-            case 3:
-                return "null";
-            case 5:
-                return "zero";
-            case 7:
-                return "full";
-            default:
-                ASSERT_NOT_REACHED();
-            }
-        case 5:
-            if (m_attached_device->minor() == 1)
-                return "console";
-            if (m_attached_device->minor() == 2)
-                return "ptmx";
-            ASSERT_NOT_REACHED();
-
-        case 4:
-            if (m_attached_device->minor() >= 64)
-                return String::formatted("ttyS{}", m_attached_device->minor() - 64);
-            return String::formatted("tty{}", m_attached_device->minor());
-
-        default:
-            ASSERT_NOT_REACHED();
-        }
-    } else {
-        switch (m_attached_device->major()) {
-        case 29:
-            return String::formatted("fb{}", m_attached_device->minor());
-        case 3: {
-            size_t drive_index = (u8)'a' + m_attached_device->minor();
-            char drive_letter = (u8)drive_index;
-            return String::format("hd%c", drive_letter);
-        }
-        case 6: {
-            return String::formatted("ramdisk{}", m_attached_device->minor());
-        }
-
-        case 100:
-            // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
-            size_t drive_index = (u8)'a' + (m_attached_device->minor() / 16);
-            char drive_letter = (u8)drive_index;
-            return String::formatted("hd{:c}{}", drive_letter, m_attached_device->minor() + 1);
-        }
-    }
-
-    ASSERT_NOT_REACHED();
-}
 ssize_t DevFSDeviceInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
 {
     LOCKER(m_lock);

+ 7 - 0
Kernel/Storage/PATADiskDevice.cpp

@@ -65,6 +65,13 @@ void PATADiskDevice::start_request(AsyncBlockDeviceRequest& request)
     m_channel.start_request(request, use_dma, is_slave());
 }
 
+String PATADiskDevice::device_name() const
+{
+    // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
+    size_t drive_index = minor() / 16;
+    return String::formatted("hd{:c}{}", 'a' + drive_index, minor() + 1);
+}
+
 size_t PATADiskDevice::max_addressable_block() const
 {
     return m_cylinders * m_heads * m_sectors_per_track;

+ 1 - 0
Kernel/Storage/PATADiskDevice.h

@@ -61,6 +61,7 @@ public:
 
     // ^BlockDevice
     virtual void start_request(AsyncBlockDeviceRequest&) override;
+    virtual String device_name() const override;
 
 private:
     PATADiskDevice(const IDEController&, IDEChannel&, DriveType, u8, u8, u8, int major, int minor);

+ 7 - 0
Kernel/Storage/Partition/DiskPartition.cpp

@@ -102,6 +102,13 @@ bool DiskPartition::can_write(const FileDescription& fd, size_t offset) const
     return m_device->can_write(fd, offset + adjust);
 }
 
+String DiskPartition::device_name() const
+{
+    // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
+    size_t partition_index = minor() % 16;
+    return String::formatted("{}{}", m_device->device_name(), partition_index + 1);
+}
+
 const char* DiskPartition::class_name() const
 {
     return "DiskPartition";

+ 1 - 0
Kernel/Storage/Partition/DiskPartition.h

@@ -47,6 +47,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0600; }
+    virtual String device_name() const override;
 
     const DiskPartitionMetadata& metadata() const;
 

+ 7 - 0
Kernel/Storage/RamdiskDevice.cpp

@@ -82,4 +82,11 @@ void RamdiskDevice::start_request(AsyncBlockDeviceRequest& request)
     }
 }
 
+String RamdiskDevice::device_name() const
+{
+    // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
+    size_t drive_index = minor() / 16;
+    return String::formatted("ramdisk{}", drive_index);
+}
+
 }

+ 1 - 0
Kernel/Storage/RamdiskDevice.h

@@ -50,6 +50,7 @@ public:
 
     // ^DiskDevice
     virtual const char* class_name() const override;
+    virtual String device_name() const override;
 
     bool is_slave() const;
 

+ 5 - 0
Kernel/TTY/MasterPTY.cpp

@@ -141,4 +141,9 @@ String MasterPTY::absolute_path(const FileDescription&) const
     return String::formatted("ptm:{}", m_pts_name);
 }
 
+String MasterPTY::device_name() const
+{
+    return String::formatted("{}", minor());
+}
+
 }

+ 1 - 0
Kernel/TTY/MasterPTY.h

@@ -50,6 +50,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0640; }
+    virtual String device_name() const override;
 
 private:
     // ^CharacterDevice

+ 1 - 0
Kernel/TTY/PTYMultiplexer.h

@@ -57,6 +57,7 @@ public:
 
     // ^Device
     virtual mode_t required_mode() const override { return 0666; }
+    virtual String device_name() const override { return "ptmx"; }
 
 private:
     // ^CharacterDevice

+ 5 - 0
Kernel/TTY/SlavePTY.cpp

@@ -106,6 +106,11 @@ KResult SlavePTY::close()
     return KSuccess;
 }
 
+String SlavePTY::device_name() const
+{
+    return String::formatted("{}", minor());
+}
+
 FileBlockCondition& SlavePTY::block_condition()
 {
     return m_master->block_condition();

+ 3 - 0
Kernel/TTY/SlavePTY.h

@@ -57,6 +57,9 @@ private:
     virtual const char* class_name() const override { return "SlavePTY"; }
     virtual KResult close() override;
 
+    // ^Device
+    virtual String device_name() const override;
+
     friend class MasterPTY;
     SlavePTY(MasterPTY&, unsigned index);
 

+ 5 - 0
Kernel/TTY/VirtualConsole.cpp

@@ -338,6 +338,11 @@ void VirtualConsole::emit(const u8* data, size_t size)
         TTY::emit(data[i]);
 }
 
+String VirtualConsole::device_name() const
+{
+    return String::formatted("tty{}", minor());
+}
+
 void VirtualConsole::echo(u8 ch)
 {
     if (should_echo_input()) {

+ 3 - 0
Kernel/TTY/VirtualConsole.h

@@ -69,6 +69,9 @@ private:
     // ^CharacterDevice
     virtual const char* class_name() const override { return "VirtualConsole"; }
 
+    // ^Device
+    virtual String device_name() const override;
+
     void set_active(bool);
 
     void flush_vga_cursor();