BlockDevice.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Devices/BlockDevice.h>
  7. #include <Kernel/FileSystem/SysFS/Subsystems/DeviceIdentifiers/BlockDevicesDirectory.h>
  8. namespace Kernel {
  9. AsyncBlockDeviceRequest::AsyncBlockDeviceRequest(Device& block_device, RequestType request_type, u64 block_index, u32 block_count, UserOrKernelBuffer const& buffer, size_t buffer_size)
  10. : AsyncDeviceRequest(block_device)
  11. , m_block_device(static_cast<BlockDevice&>(block_device))
  12. , m_request_type(request_type)
  13. , m_block_index(block_index)
  14. , m_block_count(block_count)
  15. , m_buffer(buffer)
  16. , m_buffer_size(buffer_size)
  17. {
  18. }
  19. void AsyncBlockDeviceRequest::start()
  20. {
  21. m_block_device.start_request(*this);
  22. }
  23. BlockDevice::~BlockDevice() = default;
  24. void BlockDevice::after_inserting_add_symlink_to_device_identifier_directory()
  25. {
  26. VERIFY(m_symlink_sysfs_component);
  27. SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
  28. list.append(*m_symlink_sysfs_component);
  29. });
  30. }
  31. void BlockDevice::before_will_be_destroyed_remove_symlink_from_device_identifier_directory()
  32. {
  33. VERIFY(m_symlink_sysfs_component);
  34. SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
  35. list.remove(*m_symlink_sysfs_component);
  36. });
  37. }
  38. // FIXME: This method will be eventually removed after all nodes in /sys/dev/block/ are symlinks
  39. void BlockDevice::after_inserting_add_to_device_identifier_directory()
  40. {
  41. VERIFY(m_sysfs_component);
  42. SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
  43. list.append(*m_sysfs_component);
  44. });
  45. }
  46. // FIXME: This method will be eventually removed after all nodes in /sys/dev/block/ are symlinks
  47. void BlockDevice::before_will_be_destroyed_remove_from_device_identifier_directory()
  48. {
  49. VERIFY(m_sysfs_component);
  50. SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
  51. list.remove(*m_sysfs_component);
  52. });
  53. }
  54. bool BlockDevice::read_block(u64 index, UserOrKernelBuffer& buffer)
  55. {
  56. auto read_request_or_error = try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, 1, buffer, m_block_size);
  57. if (read_request_or_error.is_error()) {
  58. dbgln("BlockDevice::read_block({}): try_make_request failed", index);
  59. return false;
  60. }
  61. auto read_request = read_request_or_error.release_value();
  62. switch (read_request->wait().request_result()) {
  63. case AsyncDeviceRequest::Success:
  64. return true;
  65. case AsyncDeviceRequest::Failure:
  66. dbgln("BlockDevice::read_block({}) IO error", index);
  67. break;
  68. case AsyncDeviceRequest::MemoryFault:
  69. dbgln("BlockDevice::read_block({}) EFAULT", index);
  70. break;
  71. case AsyncDeviceRequest::Cancelled:
  72. dbgln("BlockDevice::read_block({}) cancelled", index);
  73. break;
  74. default:
  75. VERIFY_NOT_REACHED();
  76. }
  77. return false;
  78. }
  79. bool BlockDevice::write_block(u64 index, UserOrKernelBuffer const& buffer)
  80. {
  81. auto write_request_or_error = try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, 1, buffer, m_block_size);
  82. if (write_request_or_error.is_error()) {
  83. dbgln("BlockDevice::write_block({}): try_make_request failed", index);
  84. return false;
  85. }
  86. auto write_request = write_request_or_error.release_value();
  87. switch (write_request->wait().request_result()) {
  88. case AsyncDeviceRequest::Success:
  89. return true;
  90. case AsyncDeviceRequest::Failure:
  91. dbgln("BlockDevice::write_block({}) IO error", index);
  92. break;
  93. case AsyncDeviceRequest::MemoryFault:
  94. dbgln("BlockDevice::write_block({}) EFAULT", index);
  95. break;
  96. case AsyncDeviceRequest::Cancelled:
  97. dbgln("BlockDevice::write_block({}) cancelled", index);
  98. break;
  99. default:
  100. VERIFY_NOT_REACHED();
  101. }
  102. return false;
  103. }
  104. }