ext2: Remove magic number relevant to inode's 'block' field

BUG=b:329359333
TEST=cargo test

Change-Id: I70012ed8303d01ee4cde328dcbbf40869d6587b9
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5635974
Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
Reviewed-by: Junichi Uekawa <uekawa@chromium.org>
This commit is contained in:
Keiichi Watanabe 2024-06-17 16:45:16 +09:00 committed by crosvm LUCI
parent 79429edb3f
commit ac825c410a
2 changed files with 21 additions and 9 deletions

View file

@ -31,6 +31,7 @@ use crate::blockgroup::GroupMetaData;
use crate::blockgroup::BLOCK_SIZE;
use crate::inode::Inode;
use crate::inode::InodeBlock;
use crate::inode::InodeBlocksCount;
use crate::inode::InodeNum;
use crate::inode::InodeType;
use crate::superblock::Config;
@ -277,7 +278,7 @@ impl<'a> Ext2<'a> {
let block_id = self.allocate_block()?;
let inode = self.get_inode_mut(parent)?;
inode.block.set_direct_blocks(&[block_id])?;
inode.blocks = block_size as u32 / 512;
inode.blocks = InodeBlocksCount::from_bytes_len(block_size as u32);
self.dentries.insert(
parent,
DirEntryBlock {
@ -382,7 +383,7 @@ impl<'a> Ext2<'a> {
&std::fs::metadata(path)?,
block_size as u32,
0,
0,
InodeBlocksCount::from_bytes_len(0),
InodeBlock::default(),
)?;
@ -525,10 +526,7 @@ impl<'a> Ext2<'a> {
unimplemented!("Triple-indirect block is not supported");
}
// The spec says that the `blocks` field is a "32-bit value representing the total number
// of 512-bytes blocks". This `512` is a fixed number regardless of the actual block size,
// which is usuaully 4KB.
let blocks = used_blocks as u32 * (block_size as u32 / 512);
let blocks = InodeBlocksCount::from_bytes_len((used_blocks * block_size) as u32);
let size = file_size as u32;
let inode = Inode::from_metadata(
arena,
@ -575,7 +573,7 @@ impl<'a> Ext2<'a> {
&std::fs::symlink_metadata(&link)?,
dst.len() as u32,
1, //links_count,
0, //blocks,
InodeBlocksCount::from_bytes_len(0),
block,
)?;
self.add_inode(inode_num, inode)?;

View file

@ -173,7 +173,7 @@ pub(crate) struct Inode {
_dtime: u32,
_gid: u16,
pub links_count: u16,
pub blocks: u32,
pub blocks: InodeBlocksCount,
_flags: u32,
_osd1: u32,
pub block: InodeBlock,
@ -189,6 +189,20 @@ pub(crate) struct Inode {
_reserved2: u32,
}
/// Used in `Inode` to represent how many 512-byte blocks are used by a file.
///
/// The block size '512' byte is fixed and not related to the actual block size of the file system.
/// For more details, see notes for `i_blocks_lo` in the specification.
#[repr(C)]
#[derive(Default, Debug, Copy, Clone, FromZeroes, FromBytes, AsBytes)]
pub struct InodeBlocksCount(u32);
impl InodeBlocksCount {
pub fn from_bytes_len(len: u32) -> Self {
Self(len / 512)
}
}
impl Inode {
/// Size of the inode record in bytes.
/// Its return value must be stored in `Superblock` and used to calculate the size of
@ -266,7 +280,7 @@ impl Inode {
m: &std::fs::Metadata,
size: u32,
links_count: u16,
blocks: u32,
blocks: InodeBlocksCount,
block: InodeBlock,
) -> Result<&'a mut Self> {
// (inode_num - 1) because inode is 1-indexed.