Adjust `blocks_count` field to `sector_count` in `InodeDesc` and `RawInode`.

In ext2, the granule of the `blocks_count` field in disk-inode is fixed at
512B. In the current implementation of Asterinas, the `blocks_count`
granule of memory-inode is BLOCK_SIZE.
This commit is contained in:
Chaoqun Zheng 2025-12-25 16:38:51 +08:00 committed by Tate, Hongliang Tian
parent 046c9f6a94
commit ebfa7977b1
2 changed files with 22 additions and 11 deletions

View File

@ -1488,7 +1488,7 @@ impl InodeImpl {
self.fs().free_blocks(device_range).unwrap(); self.fs().free_blocks(device_range).unwrap();
return Err(e); return Err(e);
} }
self.desc.blocks_count = range.start + device_range.len() as Ext2Bid; self.desc.sector_count = blocks_to_sectors(range.start + device_range.len() as u32);
self.last_alloc_device_bid = Some(device_range.end - 1); self.last_alloc_device_bid = Some(device_range.end - 1);
return Ok(device_range.len() as Ext2Bid); return Ok(device_range.len() as Ext2Bid);
} }
@ -1534,7 +1534,7 @@ impl InodeImpl {
return Err(e); return Err(e);
} }
self.desc.blocks_count = range.start + device_range.len() as Ext2Bid; self.desc.sector_count = blocks_to_sectors(range.start + device_range.len() as u32);
self.last_alloc_device_bid = Some(device_range.end - 1); self.last_alloc_device_bid = Some(device_range.end - 1);
Ok(device_range.len() as Ext2Bid) Ok(device_range.len() as Ext2Bid)
} }
@ -1696,7 +1696,7 @@ impl InodeImpl {
current_range.end -= free_cnt; current_range.end -= free_cnt;
} }
self.desc.blocks_count = range.start; self.desc.sector_count = blocks_to_sectors(range.start);
self.last_alloc_device_bid = if range.start == 0 { self.last_alloc_device_bid = if range.start == 0 {
None None
} else { } else {
@ -2143,8 +2143,8 @@ pub(super) struct InodeDesc {
dtime: Duration, dtime: Duration,
/// Hard links count. /// Hard links count.
hard_links: u16, hard_links: u16,
/// Number of blocks. /// Number of sectors.
blocks_count: Ext2Bid, sector_count: u32,
/// File flags. /// File flags.
flags: FileFlags, flags: FileFlags,
/// Pointers to blocks. /// Pointers to blocks.
@ -2173,7 +2173,7 @@ impl TryFrom<RawInode> for InodeDesc {
mtime: Duration::from(inode.mtime), mtime: Duration::from(inode.mtime),
dtime: Duration::from(inode.dtime), dtime: Duration::from(inode.dtime),
hard_links: inode.hard_links, hard_links: inode.hard_links,
blocks_count: inode.blocks_count, sector_count: inode.sector_count,
flags: FileFlags::from_bits(inode.flags) flags: FileFlags::from_bits(inode.flags)
.ok_or(Error::with_message(Errno::EINVAL, "invalid file flags"))?, .ok_or(Error::with_message(Errno::EINVAL, "invalid file flags"))?,
block_ptrs: inode.block_ptrs, block_ptrs: inode.block_ptrs,
@ -2201,7 +2201,7 @@ impl InodeDesc {
mtime: now, mtime: now,
dtime: Duration::ZERO, dtime: Duration::ZERO,
hard_links: 1, hard_links: 1,
blocks_count: 0, sector_count: 0,
flags: FileFlags::empty(), flags: FileFlags::empty(),
block_ptrs: BlockPtrs::default(), block_ptrs: BlockPtrs::default(),
acl: match type_ { acl: match type_ {
@ -2220,7 +2220,7 @@ impl InodeDesc {
/// Ext2 allows the `block_count` to exceed the actual number of blocks utilized. /// Ext2 allows the `block_count` to exceed the actual number of blocks utilized.
pub fn blocks_count(&self) -> Ext2Bid { pub fn blocks_count(&self) -> Ext2Bid {
let blocks = self.size_to_blocks(self.size); let blocks = self.size_to_blocks(self.size);
assert!(blocks <= self.blocks_count); debug_assert!(blocks <= sectors_to_blocks(self.sector_count));
blocks blocks
} }
@ -2232,6 +2232,17 @@ impl InodeDesc {
} }
} }
fn sectors_to_blocks(sector_count: u32) -> Ext2Bid {
sector_count.div_ceil((BLOCK_SIZE / SECTOR_SIZE) as u32)
}
fn blocks_to_sectors(block_count: u32) -> u32 {
const SECTORS_PER_BLOCK: u32 = (BLOCK_SIZE / SECTOR_SIZE) as u32;
block_count
.checked_mul(SECTORS_PER_BLOCK)
.expect("sector count overflow in blocks_to_sectors conversion")
}
bitflags! { bitflags! {
pub struct FilePerm: u16 { pub struct FilePerm: u16 {
/// set-user-ID /// set-user-ID
@ -2335,7 +2346,7 @@ pub(super) struct RawInode {
/// Low 16 bits of Group Id. /// Low 16 bits of Group Id.
pub gid: u16, pub gid: u16,
pub hard_links: u16, pub hard_links: u16,
pub blocks_count: u32, pub sector_count: u32,
/// File flags. /// File flags.
pub flags: u32, pub flags: u32,
/// OS dependent Value 1. /// OS dependent Value 1.
@ -2369,7 +2380,7 @@ impl From<&InodeDesc> for RawInode {
dtime: UnixTime::from(inode.dtime), dtime: UnixTime::from(inode.dtime),
gid: inode.gid as u16, gid: inode.gid as u16,
hard_links: inode.hard_links, hard_links: inode.hard_links,
blocks_count: inode.blocks_count, sector_count: inode.sector_count,
flags: inode.flags.bits(), flags: inode.flags.bits(),
block_ptrs: inode.block_ptrs, block_ptrs: inode.block_ptrs,
file_acl: match inode.acl { file_acl: match inode.acl {

View File

@ -7,7 +7,7 @@ pub(super) use core::{
pub(super) use align_ext::AlignExt; pub(super) use align_ext::AlignExt;
pub(super) use aster_block::{ pub(super) use aster_block::{
BLOCK_SIZE, BlockDevice, BLOCK_SIZE, BlockDevice, SECTOR_SIZE,
bio::{BioDirection, BioSegment, BioStatus, BioWaiter}, bio::{BioDirection, BioSegment, BioStatus, BioWaiter},
id::Bid, id::Bid,
}; };