Skip to content

Commit ed18c06

Browse files
committed
move a lot of logic into this lib
1 parent 8eb084d commit ed18c06

File tree

5 files changed

+183
-59
lines changed

5 files changed

+183
-59
lines changed

ext2/src/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub enum Error {
1212
DeviceRead,
1313
DeviceWrite,
1414
InvalidInodeAddress(u32),
15+
NoSpace,
1516
}
1617

1718
impl Display for Error {

ext2/src/file.rs

-57
This file was deleted.

ext2/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![no_std]
22
#![feature(const_option)]
3+
#![feature(iter_array_chunks)]
34

45
extern crate alloc;
56

@@ -8,7 +9,6 @@ use alloc::vec;
89
pub use address::*;
910
pub use dir::*;
1011
pub use error::*;
11-
pub use file::*;
1212
use filesystem::BlockDevice;
1313
pub use inode::*;
1414
pub use superblock::*;
@@ -20,9 +20,10 @@ mod block_group;
2020
mod bytefield;
2121
mod dir;
2222
mod error;
23-
mod file;
23+
mod read;
2424
mod inode;
2525
mod superblock;
26+
mod write;
2627

2728
const ROOT_DIR_INODE_ADDRESS: InodeAddress = InodeAddress::new(2).unwrap();
2829

ext2/src/read.rs

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
use alloc::vec;
2+
use alloc::vec::Vec;
3+
4+
use filesystem::BlockDevice;
5+
6+
use crate::{BlockAddress, Error, Ext2Fs, Inode, RegularFile};
7+
8+
const SZ: usize = size_of::<BlockAddress>();
9+
10+
impl<T> Ext2Fs<T>
11+
where
12+
T: BlockDevice,
13+
{
14+
pub fn read_from_file(
15+
&self,
16+
file: &RegularFile,
17+
offset: usize,
18+
buf: &mut [u8],
19+
) -> Result<usize, Error> {
20+
let file_size = file.len();
21+
if offset >= file_size {
22+
return Ok(0);
23+
}
24+
25+
let block_size = self.superblock.block_size();
26+
let offset = offset as u32;
27+
28+
let start_block = offset / block_size;
29+
let end_block = (offset + buf.len() as u32 - 1) / block_size;
30+
let relative_offset = (offset % block_size) as usize;
31+
let block_count = (end_block - start_block + 1) as usize;
32+
33+
// read blocks
34+
let mut data: Vec<u8> = vec![0_u8; block_count * block_size as usize]; // TODO: avoid allocation - maybe try to only allocate the first and last block if the read is not aligned, but read the rest directly into the buffer
35+
let res = self.read_blocks_from_inode(file, start_block as usize, end_block as usize, &mut data)?;
36+
// copy the data into buf, but only the requested part and only up to the file size
37+
let total_read = res.min(file_size - offset as usize).min(buf.len());
38+
buf[..total_read].copy_from_slice(&data[relative_offset..relative_offset + total_read]);
39+
40+
41+
Ok(total_read)
42+
}
43+
44+
fn read_blocks_from_inode(&self, inode: &Inode, start_block: usize, end_block: usize, buf: &mut [u8]) -> Result<usize, Error> {
45+
let block_size = self.superblock.block_size() as usize;
46+
assert_eq!(buf.len(), (end_block - start_block + 1) * block_size, "buf.len() must be equal to the number of blocks you want to read");
47+
48+
let (direct_limit, indirect_limit, double_indirect_limit) = self.indirect_pointer_limits();
49+
50+
let mut total_read = 0;
51+
52+
for (i, block) in (start_block..=end_block).enumerate() {
53+
let block_data = &mut buf[i * block_size..(i + 1) * block_size];
54+
let block_pointer = if block < direct_limit as usize {
55+
inode.direct_ptr(block)
56+
} else if block < indirect_limit as usize {
57+
self.resolve_indirect_ptr(inode.single_indirect_ptr(), block as u32 - direct_limit)?
58+
} else if block < double_indirect_limit as usize {
59+
self.resolve_double_indirect_ptr(inode.double_indirect_ptr(), block as u32 - indirect_limit)?
60+
} else {
61+
self.resolve_triple_indirect_ptr(inode.triple_indirect_ptr(), block as u32 - double_indirect_limit)?
62+
};
63+
if let Some(block_pointer) = block_pointer {
64+
total_read += self.read_block(block_pointer, block_data)?;
65+
} else {
66+
block_data.fill(0);
67+
total_read += block_size; // FIXME: what if the last block is sparse?
68+
}
69+
}
70+
71+
Ok(total_read)
72+
}
73+
74+
pub fn indirect_pointer_limits(&self) -> (u32, u32, u32) {
75+
let direct_limit = 12;
76+
let indirect_limit = direct_limit + self.superblock.block_size() / 4;
77+
let double_indirect_limit = indirect_limit + indirect_limit * indirect_limit;
78+
(direct_limit, indirect_limit, double_indirect_limit)
79+
}
80+
81+
pub fn is_block_allocated(&self, inode: &Inode, block_index: u32) -> Result<bool, Error> {
82+
let (direct_limit, indirect_limit, double_indirect_limit) = self.indirect_pointer_limits();
83+
84+
Ok(
85+
if block_index < direct_limit {
86+
inode.direct_ptr(block_index as usize)
87+
} else if block_index < indirect_limit {
88+
self.resolve_indirect_ptr(inode.single_indirect_ptr(), block_index - direct_limit)?
89+
} else if block_index < double_indirect_limit {
90+
self.resolve_double_indirect_ptr(inode.double_indirect_ptr(), block_index - indirect_limit)?
91+
} else {
92+
self.resolve_triple_indirect_ptr(inode.triple_indirect_ptr(), block_index - double_indirect_limit)?
93+
}
94+
.is_some()
95+
)
96+
}
97+
98+
pub fn resolve_indirect_ptr(&self, indirect_ptr: Option<BlockAddress>, block_index: u32) -> Result<Option<BlockAddress>, Error> {
99+
if indirect_ptr.is_none() {
100+
return Ok(None);
101+
}
102+
let indirect_ptr = indirect_ptr.unwrap();
103+
104+
let mut indirect_block_data = vec![0_u8; self.superblock.block_size() as usize];
105+
self.read_block(indirect_ptr, &mut indirect_block_data)?;
106+
Ok(
107+
indirect_block_data
108+
.iter()
109+
.copied()
110+
.array_chunks::<SZ>()
111+
.map(u32::from_le_bytes)
112+
.map(BlockAddress::new)
113+
.nth(block_index as usize)
114+
.unwrap() // the amount of pointers is fixed, so this is fine
115+
)
116+
}
117+
118+
pub fn resolve_double_indirect_ptr(&self, double_indirect_block: Option<BlockAddress>, block_index: u32) -> Result<Option<BlockAddress>, Error> {
119+
let block_size = self.superblock.block_size();
120+
121+
let single_indirect_block_size = block_size / 4;
122+
let single_indirect_index = block_index / single_indirect_block_size;
123+
124+
self.resolve_indirect_ptr(double_indirect_block, single_indirect_index)
125+
.and_then(|single_indirect_block_ptr| self.resolve_indirect_ptr(single_indirect_block_ptr, block_index % single_indirect_block_size))
126+
}
127+
128+
pub fn resolve_triple_indirect_ptr(&self, triple_indirect_block: Option<BlockAddress>, block_index: u32) -> Result<Option<BlockAddress>, Error> {
129+
let block_size = self.superblock.block_size();
130+
131+
let double_indirect_block_size = block_size / 4;
132+
let double_indirect_index = block_index / double_indirect_block_size;
133+
134+
self.resolve_indirect_ptr(triple_indirect_block, double_indirect_index)
135+
.and_then(|double_indirect_block_ptr| self.resolve_double_indirect_ptr(double_indirect_block_ptr, block_index % double_indirect_block_size))
136+
}
137+
}

ext2/src/write.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use filesystem::BlockDevice;
2+
3+
use crate::{Error, Ext2Fs, RegularFile};
4+
5+
impl<T> Ext2Fs<T>
6+
where
7+
T: BlockDevice,
8+
{
9+
#[allow(unused_variables)]
10+
pub fn write_to_file(
11+
&mut self,
12+
file: &RegularFile,
13+
offset: usize,
14+
buf: &[u8],
15+
) -> Result<usize, Error> {
16+
let block_size = self.superblock.block_size();
17+
let offset = offset as u32;
18+
19+
let start_block = offset / block_size;
20+
let end_block = (offset + buf.len() as u32 - 1) / block_size;
21+
let relative_offset = (offset % block_size) as usize;
22+
let block_count = (end_block - start_block + 1) as usize;
23+
24+
assert_eq!(buf.len() % block_size as usize, 0, "buf.len() must be a multiple of block_size for now"); // TODO: make this more flexible
25+
26+
for (i, block) in (start_block..=end_block).enumerate() {
27+
if !self.is_block_allocated(file, block)? {
28+
// TODO: we don't need to allocate if the full content of this block would be zero if the fs allows sparse files
29+
let free_block_address = self.allocate_block()?;
30+
if free_block_address.is_none() {
31+
return Err(Error::NoSpace);
32+
}
33+
let free_block_address = free_block_address.unwrap();
34+
// TODO: write the block address to the inode
35+
}
36+
}
37+
38+
// we can now be certain that all blocks that we want to write into are allocated
39+
40+
todo!()
41+
}
42+
}

0 commit comments

Comments
 (0)