mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:24:57 +00:00
Kernel: Return EIO if attached device is gone in StorageDevicePartition
The previous code will just do a nullptr-dereference if the underlying device is gone, so instead gracefully return EIO error.
This commit is contained in:
parent
67e028e4f4
commit
f08f339e9a
1 changed files with 8 additions and 2 deletions
|
@ -47,24 +47,30 @@ void StorageDevicePartition::start_request(AsyncBlockDeviceRequest& request)
|
|||
|
||||
ErrorOr<size_t> StorageDevicePartition::read(OpenFileDescription& fd, u64 offset, UserOrKernelBuffer& outbuf, size_t len)
|
||||
{
|
||||
auto device = m_device.strong_ref();
|
||||
if (!device)
|
||||
return Error::from_errno(EIO);
|
||||
// NOTE: The last available offset is actually just after the last addressable block.
|
||||
if (offset >= (m_metadata.end_block() - m_metadata.start_block() + 1) * block_size())
|
||||
return 0;
|
||||
size_t nread = min(static_cast<size_t>((m_metadata.end_block() - m_metadata.start_block() + 1) - offset), len);
|
||||
u64 adjust = m_metadata.start_block() * block_size();
|
||||
dbgln_if(OFFD_DEBUG, "StorageDevicePartition::read offset={}, adjust={}, len={}", fd.offset(), adjust, nread);
|
||||
return m_device.strong_ref()->read(fd, offset + adjust, outbuf, nread);
|
||||
return device->read(fd, offset + adjust, outbuf, nread);
|
||||
}
|
||||
|
||||
ErrorOr<size_t> StorageDevicePartition::write(OpenFileDescription& fd, u64 offset, UserOrKernelBuffer const& inbuf, size_t len)
|
||||
{
|
||||
auto device = m_device.strong_ref();
|
||||
if (!device)
|
||||
return Error::from_errno(EIO);
|
||||
// NOTE: The last available offset is actually just after the last addressable block.
|
||||
if (offset >= (m_metadata.end_block() - m_metadata.start_block() + 1) * block_size())
|
||||
return Error::from_errno(ENOSPC);
|
||||
size_t nwrite = min(static_cast<size_t>((m_metadata.end_block() - m_metadata.start_block() + 1) - offset), len);
|
||||
u64 adjust = m_metadata.start_block() * block_size();
|
||||
dbgln_if(OFFD_DEBUG, "StorageDevicePartition::write offset={}, adjust={}, len={}", offset, adjust, nwrite);
|
||||
return m_device.strong_ref()->write(fd, offset + adjust, inbuf, nwrite);
|
||||
return device->write(fd, offset + adjust, inbuf, nwrite);
|
||||
}
|
||||
|
||||
StringView StorageDevicePartition::class_name() const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue