2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-02-16 02:52:58 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-03 13:36:40 +03:00
|
|
|
#include <Kernel/Devices/Device.h>
|
2019-02-16 02:52:58 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-11-02 21:16:01 +03:00
|
|
|
class BlockDevice;
|
|
|
|
|
|
|
|
class AsyncBlockDeviceRequest : public AsyncDeviceRequest {
|
|
|
|
public:
|
|
|
|
enum RequestType {
|
|
|
|
Read,
|
|
|
|
Write
|
|
|
|
};
|
|
|
|
AsyncBlockDeviceRequest(Device& block_device, RequestType request_type,
|
|
|
|
u32 block_index, u32 block_count, const UserOrKernelBuffer& buffer, size_t buffer_size);
|
|
|
|
|
|
|
|
RequestType request_type() const { return m_request_type; }
|
|
|
|
u32 block_index() const { return m_block_index; }
|
|
|
|
u32 block_count() const { return m_block_count; }
|
|
|
|
UserOrKernelBuffer& buffer() { return m_buffer; }
|
|
|
|
const UserOrKernelBuffer& buffer() const { return m_buffer; }
|
|
|
|
size_t buffer_size() const { return m_buffer_size; }
|
|
|
|
|
|
|
|
virtual void start() override;
|
|
|
|
virtual const char* name() const override
|
|
|
|
{
|
|
|
|
switch (m_request_type) {
|
|
|
|
case Read:
|
|
|
|
return "BlockDeviceRequest (read)";
|
|
|
|
case Write:
|
2020-12-17 19:31:36 +03:00
|
|
|
return "BlockDeviceRequest (write)";
|
2020-11-02 21:16:01 +03:00
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-11-02 21:16:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BlockDevice& m_block_device;
|
|
|
|
const RequestType m_request_type;
|
|
|
|
const u32 m_block_index;
|
|
|
|
const u32 m_block_count;
|
|
|
|
UserOrKernelBuffer m_buffer;
|
|
|
|
const size_t m_buffer_size;
|
|
|
|
};
|
|
|
|
|
2019-02-16 02:52:58 +03:00
|
|
|
class BlockDevice : public Device {
|
|
|
|
public:
|
|
|
|
virtual ~BlockDevice() override;
|
|
|
|
|
2019-08-21 17:45:52 +03:00
|
|
|
size_t block_size() const { return m_block_size; }
|
2019-04-28 16:02:55 +03:00
|
|
|
virtual bool is_seekable() const override { return true; }
|
2020-01-23 00:23:50 +03:00
|
|
|
|
2020-11-02 21:16:01 +03:00
|
|
|
bool read_block(unsigned index, UserOrKernelBuffer&);
|
2020-09-12 06:11:07 +03:00
|
|
|
bool write_block(unsigned index, const UserOrKernelBuffer&);
|
2020-02-08 04:17:26 +03:00
|
|
|
|
2020-11-02 21:16:01 +03:00
|
|
|
virtual void start_request(AsyncBlockDeviceRequest&) = 0;
|
2020-02-08 04:17:26 +03:00
|
|
|
|
2019-02-16 02:52:58 +03:00
|
|
|
protected:
|
2019-08-21 17:45:52 +03:00
|
|
|
BlockDevice(unsigned major, unsigned minor, size_t block_size = PAGE_SIZE)
|
2020-01-23 00:23:50 +03:00
|
|
|
: Device(major, minor)
|
2019-08-21 17:45:52 +03:00
|
|
|
, m_block_size(block_size)
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
}
|
2019-02-16 11:57:42 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool is_block_device() const final { return true; }
|
2019-08-21 17:45:52 +03:00
|
|
|
|
|
|
|
size_t m_block_size { 0 };
|
2019-02-16 02:52:58 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|