From f13f756c206b8c6457bb2ff018e33cd1a73bf691 Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Thu, 2 Jan 2025 17:20:24 +0200 Subject: [PATCH] ext2: dedup block/inode free code --- kernel/driver/fs/ext2/src/allocation.rs | 94 +++++++++++++------------ 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/kernel/driver/fs/ext2/src/allocation.rs b/kernel/driver/fs/ext2/src/allocation.rs index eb949228..d1c4846b 100644 --- a/kernel/driver/fs/ext2/src/allocation.rs +++ b/kernel/driver/fs/ext2/src/allocation.rs @@ -4,9 +4,38 @@ use yggdrasil_abi::io::FileType; use crate::{BlockGroupDescriptor, Ext2Fs, ExtendedSuperblock, Inode}; impl Ext2Fs { + async fn free< + F: FnOnce(&mut BlockGroupDescriptor) -> u32, + G: FnOnce(&mut ExtendedSuperblock), + >( + &self, + element_index: u32, + group_index: u32, + descriptor_mapper: F, + superblock_mapper: G, + ) -> Result<(), Error> { + let bitmap = self + .with_bgdt_entry_mut(group_index, |descriptor| Ok(descriptor_mapper(descriptor))) + .await?; + self.with_block_mut(bitmap, size_of::(), |bitmap| { + let index = (element_index / 8) as usize; + let bit = 1u8 << (element_index % 8); + + if bitmap[index] & bit == 0 { + log::warn!("Trying to free an unallocated element #{element_index}"); + } + bitmap[index] &= !bit; + Ok(()) + }) + .await?; + self.with_superblock_mut(|sb| Ok(superblock_mapper(sb))) + .await?; + Ok(()) + } + async fn allocate< F: Fn(&mut BlockGroupDescriptor) -> Option<(u32, u32)>, - G: Fn(&mut ExtendedSuperblock), + G: FnOnce(&mut ExtendedSuperblock), >( &self, descriptor_mapper: F, @@ -95,38 +124,26 @@ impl Ext2Fs { // Free inode blocks inode.resize(self, 0).await?; - inode.dtime = real_time().seconds as u32; self.write_inode(ino, inode).await?; let inode_index = ino - 1; let group_index = inode_index / self.block_group_inode_count; - - let bitmap = self - .with_bgdt_entry_mut(group_index, |descriptor| { + self.free( + inode_index, + group_index, + |descriptor| { if is_directory { descriptor.directories = descriptor.directories.saturating_sub(1); } descriptor.unallocated_inodes += 1; - Ok(descriptor.inode_usage_bitmap) - }) - .await?; - self.with_block_mut(bitmap, size_of::(), |bitmap| { - let index = (inode_index / 8) as usize; - let bit = 1u8 << (inode_index % 8); - - if bitmap[index] & bit == 0 { - log::warn!("Trying to free an unallocated inode #{ino}"); - } - bitmap[index] &= !bit; - Ok(()) - }) - .await?; - self.with_superblock_mut(|sb| { - sb.total_unallocated_inodes += 1; - Ok(()) - }) + descriptor.inode_usage_bitmap + }, + |superblock| { + superblock.total_unallocated_inodes += 1; + }, + ) .await?; Ok(()) @@ -161,28 +178,17 @@ impl Ext2Fs { } let group_index = block_index / self.block_group_block_count; - let bitmap = self - .with_bgdt_entry_mut(group_index, |descriptor| { + self.free( + block_index, + group_index, + |descriptor| { descriptor.unallocated_blocks += 1; - Ok(descriptor.block_usage_bitmap) - }) - .await?; - self.with_block_mut(bitmap, size_of::(), |bitmap| { - let index = (block_index / 8) as usize; - let bit = 1u8 << (block_index % 8); - - if bitmap[index] & bit == 0 { - log::warn!("Freeing a block #{block_index}, but bitmap says it's not allocated"); - } - bitmap[index] &= !bit; - - Ok(()) - }) - .await?; - self.with_superblock_mut(|sb| { - sb.total_unallocated_blocks += 1; - Ok(()) - }) + descriptor.block_usage_bitmap + }, + |superblock| { + superblock.total_unallocated_blocks += 1; + }, + ) .await?; Ok(())