StorageDevice.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  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 <AK/Memory.h>
  27. #include <AK/StringView.h>
  28. #include <Kernel/Debug.h>
  29. #include <Kernel/FileSystem/FileDescription.h>
  30. #include <Kernel/Storage/StorageDevice.h>
  31. namespace Kernel {
  32. StorageDevice::StorageDevice(const StorageController& controller, int major, int minor, size_t sector_size, size_t max_addressable_block)
  33. : BlockDevice(major, minor, sector_size)
  34. , m_storage_controller(controller)
  35. , m_max_addressable_block(max_addressable_block)
  36. {
  37. }
  38. const char* StorageDevice::class_name() const
  39. {
  40. return "StorageDevice";
  41. }
  42. NonnullRefPtr<StorageController> StorageDevice::controller() const
  43. {
  44. return m_storage_controller;
  45. }
  46. KResultOr<size_t> StorageDevice::read(FileDescription&, size_t offset, UserOrKernelBuffer& outbuf, size_t len)
  47. {
  48. unsigned index = offset / block_size();
  49. u16 whole_blocks = len / block_size();
  50. ssize_t remaining = len % block_size();
  51. unsigned blocks_per_page = PAGE_SIZE / block_size();
  52. // PATAChannel will chuck a wobbly if we try to read more than PAGE_SIZE
  53. // at a time, because it uses a single page for its DMA buffer.
  54. if (whole_blocks >= blocks_per_page) {
  55. whole_blocks = blocks_per_page;
  56. remaining = 0;
  57. }
  58. #if STORAGE_DEVICE_DEBUG
  59. klog() << "StorageDevice::read() index=" << index << " whole_blocks=" << whole_blocks << " remaining=" << remaining;
  60. #endif
  61. if (whole_blocks > 0) {
  62. auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, whole_blocks, outbuf, whole_blocks * block_size());
  63. auto result = read_request->wait();
  64. if (result.wait_result().was_interrupted())
  65. return EINTR;
  66. switch (result.request_result()) {
  67. case AsyncDeviceRequest::Failure:
  68. case AsyncDeviceRequest::Cancelled:
  69. return EIO;
  70. case AsyncDeviceRequest::MemoryFault:
  71. return EFAULT;
  72. default:
  73. break;
  74. }
  75. }
  76. off_t pos = whole_blocks * block_size();
  77. if (remaining > 0) {
  78. auto data = ByteBuffer::create_uninitialized(block_size());
  79. auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
  80. auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size());
  81. auto result = read_request->wait();
  82. if (result.wait_result().was_interrupted())
  83. return EINTR;
  84. switch (result.request_result()) {
  85. case AsyncDeviceRequest::Failure:
  86. return pos;
  87. case AsyncDeviceRequest::Cancelled:
  88. return EIO;
  89. case AsyncDeviceRequest::MemoryFault:
  90. // This should never happen, we're writing to a kernel buffer!
  91. ASSERT_NOT_REACHED();
  92. default:
  93. break;
  94. }
  95. if (!outbuf.write(data.data(), pos, remaining))
  96. return EFAULT;
  97. }
  98. return pos + remaining;
  99. }
  100. bool StorageDevice::can_read(const FileDescription&, size_t offset) const
  101. {
  102. return offset < (max_addressable_block() * block_size());
  103. }
  104. KResultOr<size_t> StorageDevice::write(FileDescription&, size_t offset, const UserOrKernelBuffer& inbuf, size_t len)
  105. {
  106. unsigned index = offset / block_size();
  107. u16 whole_blocks = len / block_size();
  108. ssize_t remaining = len % block_size();
  109. unsigned blocks_per_page = PAGE_SIZE / block_size();
  110. // PATAChannel will chuck a wobbly if we try to write more than PAGE_SIZE
  111. // at a time, because it uses a single page for its DMA buffer.
  112. if (whole_blocks >= blocks_per_page) {
  113. whole_blocks = blocks_per_page;
  114. remaining = 0;
  115. }
  116. #if STORAGE_DEVICE_DEBUG
  117. klog() << "StorageDevice::write() index=" << index << " whole_blocks=" << whole_blocks << " remaining=" << remaining;
  118. #endif
  119. if (whole_blocks > 0) {
  120. auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, whole_blocks, inbuf, whole_blocks * block_size());
  121. auto result = write_request->wait();
  122. if (result.wait_result().was_interrupted())
  123. return EINTR;
  124. switch (result.request_result()) {
  125. case AsyncDeviceRequest::Failure:
  126. case AsyncDeviceRequest::Cancelled:
  127. return EIO;
  128. case AsyncDeviceRequest::MemoryFault:
  129. return EFAULT;
  130. default:
  131. break;
  132. }
  133. }
  134. off_t pos = whole_blocks * block_size();
  135. // since we can only write in block_size() increments, if we want to do a
  136. // partial write, we have to read the block's content first, modify it,
  137. // then write the whole block back to the disk.
  138. if (remaining > 0) {
  139. auto data = ByteBuffer::create_zeroed(block_size());
  140. auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
  141. {
  142. auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size());
  143. auto result = read_request->wait();
  144. if (result.wait_result().was_interrupted())
  145. return EINTR;
  146. switch (result.request_result()) {
  147. case AsyncDeviceRequest::Failure:
  148. return pos;
  149. case AsyncDeviceRequest::Cancelled:
  150. return EIO;
  151. case AsyncDeviceRequest::MemoryFault:
  152. // This should never happen, we're writing to a kernel buffer!
  153. ASSERT_NOT_REACHED();
  154. default:
  155. break;
  156. }
  157. }
  158. if (!inbuf.read(data.data(), pos, remaining))
  159. return EFAULT;
  160. {
  161. auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index + whole_blocks, 1, data_buffer, block_size());
  162. auto result = write_request->wait();
  163. if (result.wait_result().was_interrupted())
  164. return EINTR;
  165. switch (result.request_result()) {
  166. case AsyncDeviceRequest::Failure:
  167. return pos;
  168. case AsyncDeviceRequest::Cancelled:
  169. return EIO;
  170. case AsyncDeviceRequest::MemoryFault:
  171. // This should never happen, we're writing to a kernel buffer!
  172. ASSERT_NOT_REACHED();
  173. default:
  174. break;
  175. }
  176. }
  177. }
  178. return pos + remaining;
  179. }
  180. bool StorageDevice::can_write(const FileDescription&, size_t offset) const
  181. {
  182. return offset < (max_addressable_block() * block_size());
  183. }
  184. }