DiskPartition.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ssize_t DiskPartition::read(FileDescription& file_description, u8* buffer, ssize_t length)
  45. {
  46. // FIXME: This is a hacky solution, but works fine for now...
  47. off_t current_offset = file_description.seek(0, SEEK_CUR);
  48. auto new_offset = current_offset + (m_block_offset * m_device->block_size());
  49. if ((new_offset / m_device->block_size()) > m_block_limit || ((new_offset + length) / m_device->block_size()) > m_block_limit) {
  50. return 0;
  51. }
  52. file_description.seek(new_offset, SEEK_SET);
  53. auto result = m_device->read(file_description, buffer, length);
  54. file_description.seek(current_offset, SEEK_SET);
  55. return result;
  56. }
  57. ssize_t DiskPartition::write(FileDescription& file_description, const u8* buffer, ssize_t length)
  58. {
  59. // FIXME: This is a hacky solution, but works fine for now...
  60. off_t current_offset = file_description.seek(0, SEEK_CUR);
  61. auto new_offset = current_offset + (m_block_offset * m_device->block_size());
  62. if ((new_offset / m_device->block_size()) > m_block_limit || ((new_offset + length) / m_device->block_size()) > m_block_limit) {
  63. return 0;
  64. }
  65. file_description.seek(new_offset, SEEK_SET);
  66. auto result = m_device->write(file_description, buffer, length);
  67. file_description.seek(current_offset, SEEK_SET);
  68. return result;
  69. }
  70. bool DiskPartition::read_blocks(unsigned index, u16 count, u8* out)
  71. {
  72. #ifdef OFFD_DEBUG
  73. klog() << "DiskPartition::read_blocks " << index << " (really: " << (m_block_offset + index) << ") count=" << count;
  74. #endif
  75. return m_device->read_blocks(m_block_offset + index, count, out);
  76. }
  77. bool DiskPartition::write_blocks(unsigned index, u16 count, const u8* data)
  78. {
  79. #ifdef OFFD_DEBUG
  80. klog() << "DiskPartition::write_blocks " << index << " (really: " << (m_block_offset + index) << ") count=" << count;
  81. #endif
  82. return m_device->write_blocks(m_block_offset + index, count, data);
  83. }
  84. const char* DiskPartition::class_name() const
  85. {
  86. return "DiskPartition";
  87. }
  88. }