Skip to content

Commit 266aa94

Browse files
committed
condense block allocation and writing into one loop, which essentially halves the amount of indirect block ptr lookups
1 parent 2b8ade8 commit 266aa94

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

ext2/src/write.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,35 @@ where
3434
data
3535
};
3636

37-
for (i, block) in (start_block..=end_block).enumerate() {
38-
if !self.is_block_allocated(file, block)? {
39-
// TODO: we don't need to allocate if the full content of this block would be zero if the fs allows sparse files
40-
let free_block_address = self.allocate_block()?;
41-
if free_block_address.is_none() {
42-
return Err(Error::NoSpace);
43-
}
44-
let free_block_address = free_block_address.unwrap();
45-
46-
let inode = file.inode_mut();
47-
let free_slot = inode.direct_ptrs()
48-
.enumerate()
49-
.find(|(_, ptr)| ptr.is_none())
50-
.map(|(i, _)| i);
51-
if let Some(free_slot) = free_slot {
52-
inode.set_direct_ptr(free_slot, Some(free_block_address));
53-
self.write_inode(file.inode_address(), file)?;
37+
let mut chunks = data.chunks_exact(block_size as usize);
38+
for ((i, block), chunk) in (start_block..=end_block).enumerate().zip(&mut chunks) {
39+
let block_address =
40+
if let Some(block_address) = self.resolve_block_index(file, block)? {
41+
block_address
5442
} else {
55-
todo!("allocate indirect block");
56-
}
57-
}
58-
}
43+
// TODO: we don't need to allocate if the full content of this block would be zero if the fs allows sparse files
44+
let free_block_address = self.allocate_block()?;
45+
if free_block_address.is_none() {
46+
return Err(Error::NoSpace);
47+
}
48+
let free_block_address = free_block_address.unwrap();
5949

60-
// we can now be certain that all blocks that we want to write into are allocated
50+
let inode = file.inode_mut();
51+
let free_slot = inode.direct_ptrs()
52+
.enumerate()
53+
.find(|(_, ptr)| ptr.is_none())
54+
.map(|(i, _)| i);
55+
if let Some(free_slot) = free_slot {
56+
inode.set_direct_ptr(free_slot, Some(free_block_address));
57+
self.write_inode(file.inode_address(), file)?;
58+
} else {
59+
todo!("allocate indirect block");
60+
}
6161

62-
let mut chunks = data.chunks_exact(block_size as usize);
63-
for (block, data) in (start_block..=end_block).zip(&mut chunks) {
64-
let block_address = self.resolve_block_index(&file, block)?.expect("we should have just allocated this block, it should be present");
65-
self.write_block(block_address, data)?;
62+
free_block_address
63+
};
64+
65+
self.write_block(block_address, chunk)?;
6666
}
6767
debug_assert_eq!(chunks.remainder().len(), 0, "data to write was not block aligned");
6868

0 commit comments

Comments
 (0)