mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:38:11 +00:00
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:
parent
19912a0b32
commit
5ed3f7c6bf
9 changed files with 39 additions and 50 deletions
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -9,13 +9,13 @@
|
||||||
|
|
||||||
namespace Kernel {
|
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())
|
if (table->is_protective_mbr())
|
||||||
return { PartitionTable::Error::MBRProtective };
|
return Error::from_errno(ENOTSUP);
|
||||||
if (!table->is_valid())
|
if (!table->is_valid())
|
||||||
return { PartitionTable::Error::Invalid };
|
return Error::from_errno(EINVAL);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Error.h>
|
||||||
#include <AK/NonnullOwnPtr.h>
|
#include <AK/NonnullOwnPtr.h>
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/Result.h>
|
#include <AK/Result.h>
|
||||||
|
@ -20,7 +21,7 @@ class EBRPartitionTable : public MBRPartitionTable {
|
||||||
public:
|
public:
|
||||||
~EBRPartitionTable();
|
~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&);
|
explicit EBRPartitionTable(StorageDevice const&);
|
||||||
virtual bool is_valid() const override { return m_valid; };
|
virtual bool is_valid() const override { return m_valid; };
|
||||||
|
|
||||||
|
|
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -47,11 +47,11 @@ struct [[gnu::packed]] GUIDPartitionHeader {
|
||||||
u32 crc32_entries_array;
|
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())
|
if (!table->is_valid())
|
||||||
return { PartitionTable::Error::Invalid };
|
return Error::from_errno(EINVAL);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Error.h>
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/Result.h>
|
#include <AK/Result.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
@ -20,7 +21,7 @@ public:
|
||||||
virtual ~GUIDPartitionTable() = default;
|
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&);
|
explicit GUIDPartitionTable(StorageDevice const&);
|
||||||
|
|
||||||
virtual bool is_valid() const override { return m_valid; };
|
virtual bool is_valid() const override { return m_valid; };
|
||||||
|
|
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -15,15 +15,15 @@ namespace Kernel {
|
||||||
#define EBR_CHS_CONTAINER 0x05
|
#define EBR_CHS_CONTAINER 0x05
|
||||||
#define EBR_LBA_CONTAINER 0x0F
|
#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())
|
if (table->contains_ebr())
|
||||||
return { PartitionTable::Error::ContainsEBR };
|
return Error::from_errno(ENOTSUP);
|
||||||
if (table->is_protective_mbr())
|
if (table->is_protective_mbr())
|
||||||
return { PartitionTable::Error::MBRProtective };
|
return Error::from_errno(ENOTSUP);
|
||||||
if (!table->is_valid())
|
if (!table->is_valid())
|
||||||
return { PartitionTable::Error::Invalid };
|
return Error::from_errno(EINVAL);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -7,6 +7,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/ByteBuffer.h>
|
#include <AK/ByteBuffer.h>
|
||||||
|
#include <AK/Error.h>
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/Result.h>
|
#include <AK/Result.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
@ -41,7 +42,7 @@ public:
|
||||||
public:
|
public:
|
||||||
~MBRPartitionTable();
|
~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);
|
static OwnPtr<MBRPartitionTable> try_to_initialize(StorageDevice const&, u32 start_lba);
|
||||||
explicit MBRPartitionTable(StorageDevice const&);
|
explicit MBRPartitionTable(StorageDevice const&);
|
||||||
MBRPartitionTable(StorageDevice const&, u32 start_lba);
|
MBRPartitionTable(StorageDevice const&, u32 start_lba);
|
||||||
|
|
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -15,13 +15,6 @@
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
class PartitionTable {
|
class PartitionTable {
|
||||||
public:
|
|
||||||
enum class Error {
|
|
||||||
Invalid,
|
|
||||||
MBRProtective,
|
|
||||||
ContainsEBR,
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Optional<DiskPartitionMetadata> partition(unsigned index);
|
Optional<DiskPartitionMetadata> partition(unsigned index);
|
||||||
size_t partitions_count() const { return m_partitions.size(); }
|
size_t partitions_count() const { return m_partitions.size(); }
|
||||||
|
|
|
@ -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.
|
* Copyright (c) 2022, the SerenityOS developers.
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* 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);
|
auto mbr_table_or_error = MBRPartitionTable::try_to_initialize(device);
|
||||||
if (!mbr_table_or_result.is_error())
|
if (!mbr_table_or_error.is_error())
|
||||||
return move(mbr_table_or_result.value());
|
return mbr_table_or_error.release_value();
|
||||||
if (mbr_table_or_result.error() == PartitionTable::Error::MBRProtective) {
|
auto ebr_table_or_error = EBRPartitionTable::try_to_initialize(device);
|
||||||
auto gpt_table_or_result = GUIDPartitionTable::try_to_initialize(device);
|
if (!ebr_table_or_error.is_error()) {
|
||||||
if (gpt_table_or_result.is_error())
|
return ebr_table_or_error.release_value();
|
||||||
return {};
|
|
||||||
return move(gpt_table_or_result.value());
|
|
||||||
}
|
}
|
||||||
if (mbr_table_or_result.error() == PartitionTable::Error::ContainsEBR) {
|
return TRY(GUIDPartitionTable::try_to_initialize(device));
|
||||||
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 {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UNMAP_AFTER_INIT void StorageManagement::enumerate_disk_partitions()
|
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());
|
VERIFY(!m_storage_devices.is_empty());
|
||||||
size_t device_index = 0;
|
size_t device_index = 0;
|
||||||
for (auto& device : m_storage_devices) {
|
for (auto& device : m_storage_devices) {
|
||||||
auto partition_table = try_to_initialize_partition_table(device);
|
auto partition_table_or_error = try_to_initialize_partition_table(device);
|
||||||
if (!partition_table)
|
if (partition_table_or_error.is_error())
|
||||||
continue;
|
continue;
|
||||||
|
auto partition_table = partition_table_or_error.release_value();
|
||||||
for (size_t partition_index = 0; partition_index < partition_table->partitions_count(); partition_index++) {
|
for (size_t partition_index = 0; partition_index < partition_table->partitions_count(); partition_index++) {
|
||||||
auto partition_metadata = partition_table->partition(partition_index);
|
auto partition_metadata = partition_table->partition(partition_index);
|
||||||
if (!partition_metadata.has_value())
|
if (!partition_metadata.has_value())
|
||||||
|
|
|
@ -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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -45,7 +45,7 @@ private:
|
||||||
|
|
||||||
void dump_storage_devices_and_partitions() const;
|
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;
|
RefPtr<BlockDevice> boot_block_device() const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue