From 15cd5d324c9b8803fd6d7b4e9581da1375963bbe Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Sun, 1 Aug 2021 02:21:20 -0700 Subject: Kernel: Handle OOM from KBuffer usage in Ext2FS::get_bitmap_block() Fixes up error handling on an OOM-able path, and removes one more usage of KBuffer::create_with_size. --- Kernel/FileSystem/Ext2FileSystem.cpp | 10 ++++++---- Kernel/FileSystem/Ext2FileSystem.h | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'Kernel') diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index c199cfe611..579f5e2f29 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -701,7 +701,7 @@ void Ext2FS::flush_writes() } for (auto& cached_bitmap : m_cached_bitmaps) { if (cached_bitmap->dirty) { - auto buffer = UserOrKernelBuffer::for_kernel_buffer(cached_bitmap->buffer.data()); + auto buffer = UserOrKernelBuffer::for_kernel_buffer(cached_bitmap->buffer->data()); if (auto result = write_block(cached_bitmap->bitmap_block_index, buffer, block_size()); result.is_error()) { dbgln("Ext2FS[{}]::flush_writes(): Failed to write blocks: {}", fsid(), result.error()); } @@ -1499,13 +1499,15 @@ KResultOr Ext2FS::get_bitmap_block(BlockIndex bitmap_bloc return cached_bitmap; } - auto block = KBuffer::create_with_size(block_size(), Region::Access::Read | Region::Access::Write, "Ext2FS: Cached bitmap block"); - auto buffer = UserOrKernelBuffer::for_kernel_buffer(block.data()); + auto block = KBuffer::try_create_with_size(block_size(), Region::Access::Read | Region::Access::Write, "Ext2FS: Cached bitmap block"); + if (!block) + return ENOMEM; + auto buffer = UserOrKernelBuffer::for_kernel_buffer(block->data()); if (auto result = read_block(bitmap_block_index, &buffer, block_size()); result.is_error()) { dbgln("Ext2FS: Failed to load bitmap block {}", bitmap_block_index); return result; } - auto new_bitmap = adopt_own_if_nonnull(new (nothrow) CachedBitmap(bitmap_block_index, move(block))); + auto new_bitmap = adopt_own_if_nonnull(new (nothrow) CachedBitmap(bitmap_block_index, block.release_nonnull())); if (!new_bitmap) return ENOMEM; if (!m_cached_bitmaps.try_append(move(new_bitmap))) diff --git a/Kernel/FileSystem/Ext2FileSystem.h b/Kernel/FileSystem/Ext2FileSystem.h index d3318ab2e4..d3f1161087 100644 --- a/Kernel/FileSystem/Ext2FileSystem.h +++ b/Kernel/FileSystem/Ext2FileSystem.h @@ -168,15 +168,15 @@ private: bool m_block_group_descriptors_dirty { false }; struct CachedBitmap { - CachedBitmap(BlockIndex bi, KBuffer&& buf) + CachedBitmap(BlockIndex bi, NonnullOwnPtr buf) : bitmap_block_index(bi) , buffer(move(buf)) { } BlockIndex bitmap_block_index { 0 }; bool dirty { false }; - KBuffer buffer; - BitmapView bitmap(u32 blocks_per_group) { return BitmapView { buffer.data(), blocks_per_group }; } + NonnullOwnPtr buffer; + BitmapView bitmap(u32 blocks_per_group) { return BitmapView { buffer->data(), blocks_per_group }; } }; KResultOr get_bitmap_block(BlockIndex); -- cgit v1.2.3