DiskPartition.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/Debug.h>
  27. #include <Kernel/FileSystem/FileDescription.h>
  28. #include <Kernel/Storage/Partition/DiskPartition.h>
  29. namespace Kernel {
  30. NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata)
  31. {
  32. return adopt(*new DiskPartition(device, minor_number, metadata));
  33. }
  34. DiskPartition::DiskPartition(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata)
  35. : BlockDevice(100, minor_number, device.block_size())
  36. , m_device(device)
  37. , m_metadata(metadata)
  38. {
  39. }
  40. DiskPartition::~DiskPartition()
  41. {
  42. }
  43. const DiskPartitionMetadata& DiskPartition::metadata() const
  44. {
  45. return m_metadata;
  46. }
  47. void DiskPartition::start_request(AsyncBlockDeviceRequest& request)
  48. {
  49. request.add_sub_request(m_device->make_request<AsyncBlockDeviceRequest>(request.request_type(),
  50. request.block_index() + m_metadata.start_block(), request.block_count(), request.buffer(), request.buffer_size()));
  51. }
  52. KResultOr<size_t> DiskPartition::read(FileDescription& fd, size_t offset, UserOrKernelBuffer& outbuf, size_t len)
  53. {
  54. unsigned adjust = m_metadata.start_block() * block_size();
  55. #if OFFD_DEBUG
  56. klog() << "DiskPartition::read offset=" << fd.offset() << " adjust=" << adjust << " len=" << len;
  57. #endif
  58. return m_device->read(fd, offset + adjust, outbuf, len);
  59. }
  60. bool DiskPartition::can_read(const FileDescription& fd, size_t offset) const
  61. {
  62. unsigned adjust = m_metadata.start_block() * block_size();
  63. #if OFFD_DEBUG
  64. klog() << "DiskPartition::can_read offset=" << offset << " adjust=" << adjust;
  65. #endif
  66. return m_device->can_read(fd, offset + adjust);
  67. }
  68. KResultOr<size_t> DiskPartition::write(FileDescription& fd, size_t offset, const UserOrKernelBuffer& inbuf, size_t len)
  69. {
  70. unsigned adjust = m_metadata.start_block() * block_size();
  71. #if OFFD_DEBUG
  72. klog() << "DiskPartition::write offset=" << offset << " adjust=" << adjust << " len=" << len;
  73. #endif
  74. return m_device->write(fd, offset + adjust, inbuf, len);
  75. }
  76. bool DiskPartition::can_write(const FileDescription& fd, size_t offset) const
  77. {
  78. unsigned adjust = m_metadata.start_block() * block_size();
  79. #if OFFD_DEBUG
  80. klog() << "DiskPartition::can_write offset=" << offset << " adjust=" << adjust;
  81. #endif
  82. return m_device->can_write(fd, offset + adjust);
  83. }
  84. String DiskPartition::device_name() const
  85. {
  86. // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
  87. size_t partition_index = minor() % 16;
  88. return String::formatted("{}{}", m_device->device_name(), partition_index + 1);
  89. }
  90. const char* DiskPartition::class_name() const
  91. {
  92. return "DiskPartition";
  93. }
  94. }