DiskPartition.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/DiskPartition.h>
  27. #include <Kernel/FileSystem/FileDescription.h>
  28. // #define OFFD_DEBUG
  29. namespace Kernel {
  30. NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned block_offset, unsigned block_limit)
  31. {
  32. return adopt(*new DiskPartition(device, block_offset, block_limit));
  33. }
  34. DiskPartition::DiskPartition(BlockDevice& device, unsigned block_offset, unsigned block_limit)
  35. : BlockDevice(100, 0, device.block_size())
  36. , m_device(device)
  37. , m_block_offset(block_offset)
  38. , m_block_limit(block_limit)
  39. {
  40. }
  41. DiskPartition::~DiskPartition()
  42. {
  43. }
  44. KResultOr<size_t> DiskPartition::read(FileDescription& fd, size_t offset, UserOrKernelBuffer& outbuf, size_t len)
  45. {
  46. unsigned adjust = m_block_offset * block_size();
  47. #ifdef OFFD_DEBUG
  48. klog() << "DiskPartition::read offset=" << fd.offset() << " adjust=" << adjust << " len=" << len;
  49. #endif
  50. return m_device->read(fd, offset + adjust, outbuf, len);
  51. }
  52. bool DiskPartition::can_read(const FileDescription& fd, size_t offset) const
  53. {
  54. unsigned adjust = m_block_offset * block_size();
  55. #ifdef OFFD_DEBUG
  56. klog() << "DiskPartition::can_read offset=" << offset << " adjust=" << adjust;
  57. #endif
  58. return m_device->can_read(fd, offset + adjust);
  59. }
  60. KResultOr<size_t> DiskPartition::write(FileDescription& fd, size_t offset, const UserOrKernelBuffer& inbuf, size_t len)
  61. {
  62. unsigned adjust = m_block_offset * block_size();
  63. #ifdef OFFD_DEBUG
  64. klog() << "DiskPartition::write offset=" << offset << " adjust=" << adjust << " len=" << len;
  65. #endif
  66. return m_device->write(fd, offset + adjust, inbuf, len);
  67. }
  68. bool DiskPartition::can_write(const FileDescription& fd, size_t offset) const
  69. {
  70. unsigned adjust = m_block_offset * block_size();
  71. #ifdef OFFD_DEBUG
  72. klog() << "DiskPartition::can_write offset=" << offset << " adjust=" << adjust;
  73. #endif
  74. return m_device->can_write(fd, offset + adjust);
  75. }
  76. bool DiskPartition::read_blocks(unsigned index, u16 count, UserOrKernelBuffer& out)
  77. {
  78. #ifdef OFFD_DEBUG
  79. klog() << "DiskPartition::read_blocks " << index << " (really: " << (m_block_offset + index) << ") count=" << count;
  80. #endif
  81. return m_device->read_blocks(m_block_offset + index, count, out);
  82. }
  83. bool DiskPartition::write_blocks(unsigned index, u16 count, const UserOrKernelBuffer& data)
  84. {
  85. #ifdef OFFD_DEBUG
  86. klog() << "DiskPartition::write_blocks " << index << " (really: " << (m_block_offset + index) << ") count=" << count;
  87. #endif
  88. return m_device->write_blocks(m_block_offset + index, count, data);
  89. }
  90. const char* DiskPartition::class_name() const
  91. {
  92. return "DiskPartition";
  93. }
  94. }