Kernel/Storage: Migrate the partition code to use the ErrorOr container

That code used the old AK::Result container, which leads to overly
complicated initialization flow when trying to figure out the correct
partition table type. Instead, when using the ErrorOr container the code
is much simpler and more understandable.
This commit is contained in:
Liav A 2022-04-25 19:54:06 +03:00 committed by Linus Groh
parent 19912a0b32
commit 5ed3f7c6bf
Notes: sideshowbarker 2024-07-17 11:29:39 +09:00
9 changed files with 39 additions and 50 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -9,13 +9,13 @@
namespace Kernel {
Result<NonnullOwnPtr<EBRPartitionTable>, PartitionTable::Error> EBRPartitionTable::try_to_initialize(StorageDevice const& device)
ErrorOr<NonnullOwnPtr<EBRPartitionTable>> EBRPartitionTable::try_to_initialize(StorageDevice const& device)
{
auto table = adopt_nonnull_own_or_enomem(new (nothrow) EBRPartitionTable(device)).release_value_but_fixme_should_propagate_errors();
auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) EBRPartitionTable(device)));
if (table->is_protective_mbr())
return { PartitionTable::Error::MBRProtective };
return Error::from_errno(ENOTSUP);
if (!table->is_valid())
return { PartitionTable::Error::Invalid };
return Error::from_errno(EINVAL);
return table;
}

View file

@ -1,11 +1,12 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/RefPtr.h>
#include <AK/Result.h>
@ -20,7 +21,7 @@ class EBRPartitionTable : public MBRPartitionTable {
public:
~EBRPartitionTable();
static Result<NonnullOwnPtr<EBRPartitionTable>, PartitionTable::Error> try_to_initialize(StorageDevice const&);
static ErrorOr<NonnullOwnPtr<EBRPartitionTable>> try_to_initialize(StorageDevice const&);
explicit EBRPartitionTable(StorageDevice const&);
virtual bool is_valid() const override { return m_valid; };

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -47,11 +47,11 @@ struct [[gnu::packed]] GUIDPartitionHeader {
u32 crc32_entries_array;
};
Result<NonnullOwnPtr<GUIDPartitionTable>, PartitionTable::Error> GUIDPartitionTable::try_to_initialize(StorageDevice const& device)
ErrorOr<NonnullOwnPtr<GUIDPartitionTable>> GUIDPartitionTable::try_to_initialize(StorageDevice const& device)
{
auto table = adopt_nonnull_own_or_enomem(new (nothrow) GUIDPartitionTable(device)).release_value_but_fixme_should_propagate_errors();
auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GUIDPartitionTable(device)));
if (!table->is_valid())
return { PartitionTable::Error::Invalid };
return Error::from_errno(EINVAL);
return table;
}

View file

@ -1,11 +1,12 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/RefPtr.h>
#include <AK/Result.h>
#include <AK/Types.h>
@ -20,7 +21,7 @@ public:
virtual ~GUIDPartitionTable() = default;
;
static Result<NonnullOwnPtr<GUIDPartitionTable>, PartitionTable::Error> try_to_initialize(StorageDevice const&);
static ErrorOr<NonnullOwnPtr<GUIDPartitionTable>> try_to_initialize(StorageDevice const&);
explicit GUIDPartitionTable(StorageDevice const&);
virtual bool is_valid() const override { return m_valid; };

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -15,15 +15,15 @@ namespace Kernel {
#define EBR_CHS_CONTAINER 0x05
#define EBR_LBA_CONTAINER 0x0F
Result<NonnullOwnPtr<MBRPartitionTable>, PartitionTable::Error> MBRPartitionTable::try_to_initialize(StorageDevice const& device)
ErrorOr<NonnullOwnPtr<MBRPartitionTable>> MBRPartitionTable::try_to_initialize(StorageDevice const& device)
{
auto table = adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(device)).release_value_but_fixme_should_propagate_errors();
auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(device)));
if (table->contains_ebr())
return { PartitionTable::Error::ContainsEBR };
return Error::from_errno(ENOTSUP);
if (table->is_protective_mbr())
return { PartitionTable::Error::MBRProtective };
return Error::from_errno(ENOTSUP);
if (!table->is_valid())
return { PartitionTable::Error::Invalid };
return Error::from_errno(EINVAL);
return table;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,6 +7,7 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Error.h>
#include <AK/RefPtr.h>
#include <AK/Result.h>
#include <AK/Vector.h>
@ -41,7 +42,7 @@ public:
public:
~MBRPartitionTable();
static Result<NonnullOwnPtr<MBRPartitionTable>, PartitionTable::Error> try_to_initialize(StorageDevice const&);
static ErrorOr<NonnullOwnPtr<MBRPartitionTable>> try_to_initialize(StorageDevice const&);
static OwnPtr<MBRPartitionTable> try_to_initialize(StorageDevice const&, u32 start_lba);
explicit MBRPartitionTable(StorageDevice const&);
MBRPartitionTable(StorageDevice const&, u32 start_lba);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -15,13 +15,6 @@
namespace Kernel {
class PartitionTable {
public:
enum class Error {
Invalid,
MBRProtective,
ContainsEBR,
};
public:
Optional<DiskPartitionMetadata> partition(unsigned index);
size_t partitions_count() const { return m_partitions.size(); }

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
@ -129,24 +129,16 @@ UNMAP_AFTER_INIT void StorageManagement::dump_storage_devices_and_partitions() c
}
}
UNMAP_AFTER_INIT OwnPtr<PartitionTable> StorageManagement::try_to_initialize_partition_table(StorageDevice const& device) const
UNMAP_AFTER_INIT ErrorOr<NonnullOwnPtr<PartitionTable>> StorageManagement::try_to_initialize_partition_table(StorageDevice const& device) const
{
auto mbr_table_or_result = MBRPartitionTable::try_to_initialize(device);
if (!mbr_table_or_result.is_error())
return move(mbr_table_or_result.value());
if (mbr_table_or_result.error() == PartitionTable::Error::MBRProtective) {
auto gpt_table_or_result = GUIDPartitionTable::try_to_initialize(device);
if (gpt_table_or_result.is_error())
return {};
return move(gpt_table_or_result.value());
auto mbr_table_or_error = MBRPartitionTable::try_to_initialize(device);
if (!mbr_table_or_error.is_error())
return mbr_table_or_error.release_value();
auto ebr_table_or_error = EBRPartitionTable::try_to_initialize(device);
if (!ebr_table_or_error.is_error()) {
return ebr_table_or_error.release_value();
}
if (mbr_table_or_result.error() == PartitionTable::Error::ContainsEBR) {
auto ebr_table_or_result = EBRPartitionTable::try_to_initialize(device);
if (ebr_table_or_result.is_error())
return {};
return move(ebr_table_or_result.value());
}
return {};
return TRY(GUIDPartitionTable::try_to_initialize(device));
}
UNMAP_AFTER_INIT void StorageManagement::enumerate_disk_partitions()
@ -154,9 +146,10 @@ UNMAP_AFTER_INIT void StorageManagement::enumerate_disk_partitions()
VERIFY(!m_storage_devices.is_empty());
size_t device_index = 0;
for (auto& device : m_storage_devices) {
auto partition_table = try_to_initialize_partition_table(device);
if (!partition_table)
auto partition_table_or_error = try_to_initialize_partition_table(device);
if (partition_table_or_error.is_error())
continue;
auto partition_table = partition_table_or_error.release_value();
for (size_t partition_index = 0; partition_index < partition_table->partitions_count(); partition_index++) {
auto partition_metadata = partition_table->partition(partition_index);
if (!partition_metadata.has_value())

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -45,7 +45,7 @@ private:
void dump_storage_devices_and_partitions() const;
OwnPtr<PartitionTable> try_to_initialize_partition_table(StorageDevice const&) const;
ErrorOr<NonnullOwnPtr<PartitionTable>> try_to_initialize_partition_table(StorageDevice const&) const;
RefPtr<BlockDevice> boot_block_device() const;