ros_fs::layout::disk_inode

Struct DiskInode

Source
#[repr(C)]
pub struct DiskInode { type: InodeType, pub size: u32, pub direct: [u32; 28], pub indirect: u32, pub double_indirect: u32, }
Expand description

磁盘 Inode 结构

一个 Inode 包含了文件的元数据信息,包括文件类型、大小、数据块索引等。

Inode 结构的大小为 64 字节,包含了文件大小、直接索引、一级索引、二级索引等信息。

  • 文件大小:文件的大小,单位为字节。
  • 直接索引:直接索引表,包含了 28 个直接索引块。
  • 一级索引:一级索引表,包含了 128 个间接索引块。
  • 二级索引:二级索引表,包含了 128 个一级索引块。

Fields§

§type: InodeType§size: u32§direct: [u32; 28]

直接索引

§indirect: u32

一级索引

§double_indirect: u32

二级索引

Implementations§

Source§

impl DiskInode

Source

pub fn init(&mut self, type: InodeType)

初始化 Inode 结构

Source

pub fn get_type(&self) -> InodeType

获取 Inode 类型

Source

fn get_direct_block(&self, offset: usize) -> u32

获取直接索引块

不检查索引是否越界

Source

fn get_indirect_block(&self, offset: usize, dev: &Arc<dyn BlockDevice>) -> u32

获取一级索引块

不检查索引是否越界

Source

fn extract_double_indirect_block(offset: usize) -> Option<(usize, usize)>

计算偏移在二级索引中的位置

§参数
§返回

在二级索引中的位置二元组,分别偏移在一级索引表的偏移和二级索引表的偏移, 如果偏移不在二级索引范围内,返回 None

Source

fn get_double_indirect_block( &self, offset: usize, dev: &Arc<dyn BlockDevice>, ) -> u32

获取二级索引块

不检查索引是否越界

Source

pub fn translate(&self, offset: u32, dev: &Arc<dyn BlockDevice>) -> u32

将文件内偏移转换为其对应的数据块索引

通过文件内偏移计算数据块索引,如果偏移在直接索引范围内,返回直接索引; 如果偏移在一级索引范围内,返回一级索引;否则返回二级索引。

§参数
  • offset:文件内偏移
  • dev:块设备
§返回

数据块索引

Source

fn calc_required_block_for(size: u32) -> u32

计算文件大小所需的数据块数

通过文件大小计算所需的数据块数,不包括索引块。

由于一个数据块只能存储一个文件块,因此文件大小除以数据块大小向上取整即可得到所需的数据块数。

Source

pub fn required_data_blocks_for(&self, size: u32) -> u32

计算文件大小所需的数据块数

Source

pub fn required_blocks_for(&self, size: u32) -> u32

计算文件大小所需的总块数

通过文件大小计算所需的总块数,包括数据块和索引块。

在计算出数据块数后,需要考虑索引块的情况:

  • 如果数据块数小于等于直接索引块数,不需要额外的索引块;
  • 如果数据块数大于直接索引块数,需要一级索引块;
  • 如果数据块数大于一级索引块数,需要二级索引块,二级索引块中的每个索引指向一个一级索引块,共需要 (数据块数 - 一级索引块数) / 一级索引块数 个索引块。
Source

pub fn required_delta_blocks_for(&self, new_size: u32) -> u32

计算增加文件大小还需的总块数

Source

pub fn extend_size( &mut self, new_size: u32, new_blocks: Vec<u32>, dev: &Arc<dyn BlockDevice>, )

扩展文件大小

通过新的文件大小和新分配的数据块列表扩展文件大小。

调用该方法前需要调用 Self::required_delta_blocks_for 方法计算所需的总块数, 并申请足够的数据块。

在扩展文件大小时,会将新分配的数据块依次填充到直接索引、一级索引和二级索引中。

Source

pub fn clear_size(&mut self, dev: &Arc<dyn BlockDevice>) -> Vec<u32>

清空 Inode 的数据块

清空 Inode 的数据块,返回被回收的数据块列表。

Source

pub fn read_at( &self, offset: usize, dev: &Arc<dyn BlockDevice>, buf: &mut [u8], ) -> usize

读取 Inode 的数据

从 Inode 中读取数据到缓冲区,返回实际读取的字节数。

§参数
  • offset:读取偏移
  • dev:块设备
  • buf:缓冲区
Source

pub fn write_at( &mut self, offset: usize, dev: &Arc<dyn BlockDevice>, buf: &[u8], ) -> usize

写入 Inode 的数据

将缓冲区中的数据写入 Inode,返回实际写入的字节数。

§参数
  • offset:写入偏移
  • dev:块设备
  • buf:缓冲区
§返回

实际写入的字节数

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.