BlockDevice.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/Devices/BlockDevice.h>
  27. namespace Kernel {
  28. AsyncBlockDeviceRequest::AsyncBlockDeviceRequest(Device& block_device, RequestType request_type, u32 block_index, u32 block_count, const UserOrKernelBuffer& buffer, size_t buffer_size)
  29. : AsyncDeviceRequest(block_device)
  30. , m_block_device(static_cast<BlockDevice&>(block_device))
  31. , m_request_type(request_type)
  32. , m_block_index(block_index)
  33. , m_block_count(block_count)
  34. , m_buffer(buffer)
  35. , m_buffer_size(buffer_size)
  36. {
  37. }
  38. void AsyncBlockDeviceRequest::start()
  39. {
  40. m_block_device.start_request(*this);
  41. }
  42. BlockDevice::~BlockDevice()
  43. {
  44. }
  45. bool BlockDevice::read_block(unsigned index, UserOrKernelBuffer& buffer)
  46. {
  47. auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, 1, buffer, 512);
  48. switch (read_request->wait().request_result()) {
  49. case AsyncDeviceRequest::Success:
  50. return true;
  51. case AsyncDeviceRequest::Failure:
  52. dbgln("BlockDevice::read_block({}) IO error", index);
  53. break;
  54. case AsyncDeviceRequest::MemoryFault:
  55. dbgln("BlockDevice::read_block({}) EFAULT", index);
  56. break;
  57. case AsyncDeviceRequest::Cancelled:
  58. dbgln("BlockDevice::read_block({}) cancelled", index);
  59. break;
  60. default:
  61. VERIFY_NOT_REACHED();
  62. }
  63. return false;
  64. }
  65. bool BlockDevice::write_block(unsigned index, const UserOrKernelBuffer& buffer)
  66. {
  67. auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, 1, buffer, 512);
  68. switch (write_request->wait().request_result()) {
  69. case AsyncDeviceRequest::Success:
  70. return true;
  71. case AsyncDeviceRequest::Failure:
  72. dbgln("BlockDevice::write_block({}) IO error", index);
  73. break;
  74. case AsyncDeviceRequest::MemoryFault:
  75. dbgln("BlockDevice::write_block({}) EFAULT", index);
  76. break;
  77. case AsyncDeviceRequest::Cancelled:
  78. dbgln("BlockDevice::write_block({}) cancelled", index);
  79. break;
  80. default:
  81. VERIFY_NOT_REACHED();
  82. }
  83. return false;
  84. }
  85. }