Skip to content

Commit

Permalink
rafs: rename EROFS_BLOCK_SIZE to EROFS_BLOCK_SIZE_4096
Browse files Browse the repository at this point in the history
Rename EROFS_BLOCK_SIZE to EROFS_BLOCK_SIZE_4096, we are going to
support EROFS_BLOCK_SIZE_512.

Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
  • Loading branch information
jiangliu committed Mar 9, 2023
1 parent c2e08c4 commit d0b07e1
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 87 deletions.
16 changes: 8 additions & 8 deletions rafs/src/builder/core/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::builder::{
};
use crate::metadata::chunk::ChunkWrapper;
use crate::metadata::layout::v5::RafsV5BlobTable;
use crate::metadata::layout::v6::{RafsV6BlobTable, EROFS_BLOCK_SIZE, EROFS_INODE_SLOT_SIZE};
use crate::metadata::layout::v6::{RafsV6BlobTable, EROFS_BLOCK_SIZE_4096, EROFS_INODE_SLOT_SIZE};
use crate::metadata::layout::RafsBlobTable;
use crate::metadata::{Inode, RAFS_DEFAULT_CHUNK_SIZE};
use crate::metadata::{RafsSuperFlags, RafsVersion};
Expand Down Expand Up @@ -970,11 +970,11 @@ impl BootstrapContext {
layered,
inode_map: HashMap::new(),
nodes: Vec::new(),
offset: EROFS_BLOCK_SIZE,
offset: EROFS_BLOCK_SIZE_4096,
writer,
v6_available_blocks: vec![
VecDeque::new();
EROFS_BLOCK_SIZE as usize / EROFS_INODE_SLOT_SIZE
EROFS_BLOCK_SIZE_4096 as usize / EROFS_INODE_SLOT_SIZE
],
})
}
Expand All @@ -991,17 +991,17 @@ impl BootstrapContext {
// If found it, return the offset where we can store data.
// If not, return 0.
pub(crate) fn allocate_available_block(&mut self, size: u64) -> u64 {
if size >= EROFS_BLOCK_SIZE {
if size >= EROFS_BLOCK_SIZE_4096 {
return 0;
}

let min_idx = div_round_up(size, EROFS_INODE_SLOT_SIZE as u64) as usize;
let max_idx = div_round_up(EROFS_BLOCK_SIZE, EROFS_INODE_SLOT_SIZE as u64) as usize;
let max_idx = div_round_up(EROFS_BLOCK_SIZE_4096, EROFS_INODE_SLOT_SIZE as u64) as usize;

for idx in min_idx..max_idx {
let blocks = &mut self.v6_available_blocks[idx];
if let Some(mut offset) = blocks.pop_front() {
offset += EROFS_BLOCK_SIZE - (idx * EROFS_INODE_SLOT_SIZE) as u64;
offset += EROFS_BLOCK_SIZE_4096 - (idx * EROFS_INODE_SLOT_SIZE) as u64;
self.append_available_block(offset + (min_idx * EROFS_INODE_SLOT_SIZE) as u64);
return offset;
}
Expand All @@ -1012,8 +1012,8 @@ impl BootstrapContext {

// Append the block that `offset` belongs to corresponding deque.
pub(crate) fn append_available_block(&mut self, offset: u64) {
if offset % EROFS_BLOCK_SIZE != 0 {
let avail = EROFS_BLOCK_SIZE - offset % EROFS_BLOCK_SIZE;
if offset % EROFS_BLOCK_SIZE_4096 != 0 {
let avail = EROFS_BLOCK_SIZE_4096 - offset % EROFS_BLOCK_SIZE_4096;
let idx = avail as usize / EROFS_INODE_SLOT_SIZE;
self.v6_available_blocks[idx].push_back(round_down_4k(offset));
}
Expand Down
37 changes: 19 additions & 18 deletions rafs/src/builder/core/v6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::metadata::inode::new_v6_inode;
use crate::metadata::layout::v6::{
align_offset, calculate_nid, RafsV6BlobTable, RafsV6Device, RafsV6Dirent, RafsV6InodeChunkAddr,
RafsV6InodeChunkHeader, RafsV6OndiskInode, RafsV6SuperBlock, RafsV6SuperBlockExt,
EROFS_BLOCK_SIZE, EROFS_DEVTABLE_OFFSET, EROFS_INODE_CHUNK_BASED, EROFS_INODE_FLAT_INLINE,
EROFS_BLOCK_SIZE_4096, EROFS_DEVTABLE_OFFSET, EROFS_INODE_CHUNK_BASED, EROFS_INODE_FLAT_INLINE,
EROFS_INODE_FLAT_PLAIN, EROFS_INODE_SLOT_SIZE, EROFS_SUPER_BLOCK_SIZE, EROFS_SUPER_OFFSET,
};
use crate::metadata::RafsStore;
Expand Down Expand Up @@ -162,8 +162,8 @@ impl Node {
for child in tree.children.iter() {
let len = child.node.name().as_bytes().len() + size_of::<RafsV6Dirent>();
// erofs disk format requires dirent to be aligned with 4096.
if (d_size % EROFS_BLOCK_SIZE) + len as u64 > EROFS_BLOCK_SIZE {
d_size = div_round_up(d_size as u64, EROFS_BLOCK_SIZE) * EROFS_BLOCK_SIZE;
if (d_size % EROFS_BLOCK_SIZE_4096) + len as u64 > EROFS_BLOCK_SIZE_4096 {
d_size = div_round_up(d_size as u64, EROFS_BLOCK_SIZE_4096) * EROFS_BLOCK_SIZE_4096;
}
d_size += len as u64;
}
Expand Down Expand Up @@ -217,7 +217,7 @@ impl Node {
//
//
let inode_size = self.v6_size_with_xattr();
let tail: u64 = d_size % EROFS_BLOCK_SIZE;
let tail: u64 = d_size % EROFS_BLOCK_SIZE_4096;

// We use a simple inline strategy here:
// If the inode size with xattr + tail data size <= EROFS_BLOCK_SIZE,
Expand All @@ -229,7 +229,7 @@ impl Node {
// since it contain only single blocks with some unused space, the available space can only
// be smaller than EROFS_BLOCK_SIZE, therefore we can't use our used blocks to store the
// inode plus the tail data bigger than EROFS_BLOCK_SIZE.
let should_inline = tail != 0 && (inode_size + tail) <= EROFS_BLOCK_SIZE;
let should_inline = tail != 0 && (inode_size + tail) <= EROFS_BLOCK_SIZE_4096;

// If should inline, we first try to allocate space for the inode together with tail data
// using used blocks.
Expand All @@ -240,10 +240,11 @@ impl Node {
self.v6_datalayout = if should_inline {
self.v6_offset = bootstrap_ctx.allocate_available_block(inode_size + tail);
if self.v6_offset == 0 {
let available = EROFS_BLOCK_SIZE - bootstrap_ctx.offset % EROFS_BLOCK_SIZE;
let available =
EROFS_BLOCK_SIZE_4096 - bootstrap_ctx.offset % EROFS_BLOCK_SIZE_4096;
if available < inode_size + tail {
bootstrap_ctx.append_available_block(bootstrap_ctx.offset);
bootstrap_ctx.align_offset(EROFS_BLOCK_SIZE);
bootstrap_ctx.align_offset(EROFS_BLOCK_SIZE_4096);
}

self.v6_offset = bootstrap_ctx.offset;
Expand All @@ -252,7 +253,7 @@ impl Node {

if d_size != tail {
bootstrap_ctx.append_available_block(bootstrap_ctx.offset);
bootstrap_ctx.align_offset(EROFS_BLOCK_SIZE);
bootstrap_ctx.align_offset(EROFS_BLOCK_SIZE_4096);
}
self.v6_dirents_offset = bootstrap_ctx.offset;
bootstrap_ctx.offset += round_down_4k(d_size);
Expand All @@ -269,10 +270,10 @@ impl Node {
}

bootstrap_ctx.append_available_block(bootstrap_ctx.offset);
bootstrap_ctx.align_offset(EROFS_BLOCK_SIZE);
bootstrap_ctx.align_offset(EROFS_BLOCK_SIZE_4096);
self.v6_dirents_offset = bootstrap_ctx.offset;
bootstrap_ctx.offset += d_size;
bootstrap_ctx.align_offset(EROFS_BLOCK_SIZE);
bootstrap_ctx.align_offset(EROFS_BLOCK_SIZE_4096);

EROFS_INODE_FLAT_PLAIN
};
Expand Down Expand Up @@ -336,7 +337,7 @@ impl Node {
for (offset, name, file_type) in self.v6_dirents.iter() {
let len = name.len() + size_of::<RafsV6Dirent>();
// write to bootstrap when it will exceed EROFS_BLOCK_SIZE
if used + len as u64 > EROFS_BLOCK_SIZE {
if used + len as u64 > EROFS_BLOCK_SIZE_4096 {
for (entry, name) in dirents.iter_mut() {
trace!("{:?} nameoff {}", name, nameoff);
entry.set_name_offset(nameoff as u16);
Expand All @@ -363,7 +364,7 @@ impl Node {
nameoff = 0;
used = 0;
// track where we're going to write.
dirent_off += EROFS_BLOCK_SIZE;
dirent_off += EROFS_BLOCK_SIZE_4096;
}

trace!(
Expand Down Expand Up @@ -527,7 +528,7 @@ impl Node {

impl BuildContext {
pub fn v6_block_size(&self) -> u64 {
EROFS_BLOCK_SIZE
EROFS_BLOCK_SIZE_4096
}

pub fn v6_block_addr(&self, offset: u64) -> Result<u32> {
Expand Down Expand Up @@ -609,7 +610,7 @@ impl Bootstrap {
let blob_table_size = blob_table.size() as u64;
let blob_table_offset = align_offset(
(EROFS_DEVTABLE_OFFSET as u64) + devtable_len as u64,
EROFS_BLOCK_SIZE as u64,
EROFS_BLOCK_SIZE_4096 as u64,
);
let blob_table_entries = blobs.len();
assert!(blob_table_entries < u8::MAX as usize);
Expand Down Expand Up @@ -639,11 +640,11 @@ impl Bootstrap {
// When using nid 0 as root nid,
// the root directory will not be shown by glibc's getdents/readdir.
// Because in some OS, ino == 0 represents corresponding file is deleted.
let orig_meta_addr = bootstrap_ctx.nodes[0].v6_offset - EROFS_BLOCK_SIZE;
let orig_meta_addr = bootstrap_ctx.nodes[0].v6_offset - EROFS_BLOCK_SIZE_4096;
let meta_addr = if blob_table_size > 0 {
align_offset(
blob_table_offset + blob_table_size + prefetch_table_size as u64,
EROFS_BLOCK_SIZE as u64,
EROFS_BLOCK_SIZE_4096 as u64,
)
} else {
orig_meta_addr
Expand Down Expand Up @@ -733,7 +734,7 @@ impl Bootstrap {
.writer
.seek_to_end()
.context("failed to seek to bootstrap's end for chunk table")?;
assert_eq!(pos % EROFS_BLOCK_SIZE, 0);
assert_eq!(pos % EROFS_BLOCK_SIZE_4096, 0);
let mut devtable: Vec<RafsV6Device> = Vec::new();
let mut block_count = 0u32;
let mut inlined_chunk_digest = true;
Expand Down Expand Up @@ -834,7 +835,7 @@ impl Bootstrap {
.writer
.seek_to_end()
.context("failed to seek to bootstrap's end for chunk table")?;
let padding = align_offset(pos, EROFS_BLOCK_SIZE as u64) - pos;
let padding = align_offset(pos, EROFS_BLOCK_SIZE_4096 as u64) - pos;
bootstrap_ctx
.writer
.write_all(&WRITE_PADDING_DATA[0..padding as usize])
Expand Down
41 changes: 23 additions & 18 deletions rafs/src/metadata/direct_v6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::metadata::layout::v5::RafsV5ChunkInfo;
use crate::metadata::layout::v6::{
rafsv6_load_blob_extra_info, recover_namespace, RafsV6BlobTable, RafsV6Dirent,
RafsV6InodeChunkAddr, RafsV6InodeCompact, RafsV6InodeExtended, RafsV6OndiskInode,
RafsV6XattrEntry, RafsV6XattrIbodyHeader, EROFS_BLOCK_SIZE, EROFS_INODE_CHUNK_BASED,
RafsV6XattrEntry, RafsV6XattrIbodyHeader, EROFS_BLOCK_SIZE_4096, EROFS_INODE_CHUNK_BASED,
EROFS_INODE_FLAT_INLINE, EROFS_INODE_FLAT_PLAIN, EROFS_INODE_SLOT_SIZE,
EROFS_I_DATALAYOUT_BITS, EROFS_I_VERSION_BIT, EROFS_I_VERSION_BITS,
};
Expand Down Expand Up @@ -100,7 +100,7 @@ impl DirectSuperBlockV6 {
/// Create a new instance of `DirectSuperBlockV6`.
pub fn new(meta: &RafsSuperMeta) -> Self {
let state = DirectMappingState::new(meta);
let meta_offset = meta.meta_blkaddr as usize * EROFS_BLOCK_SIZE as usize;
let meta_offset = meta.meta_blkaddr as usize * EROFS_BLOCK_SIZE_4096 as usize;
let info = DirectCachedInfo {
meta_offset,
root_ino: meta.root_nid as Inode,
Expand Down Expand Up @@ -170,8 +170,11 @@ impl DirectSuperBlockV6 {
let file = clone_file(r.as_raw_fd())?;
let md = file.metadata()?;
let len = md.len();
let md_range =
MetaRange::new(EROFS_BLOCK_SIZE as u64, len - EROFS_BLOCK_SIZE as u64, true)?;
let md_range = MetaRange::new(
EROFS_BLOCK_SIZE_4096 as u64,
len - EROFS_BLOCK_SIZE_4096 as u64,
true,
)?;

// Validate blob table layout as blob_table_start and blob_table_offset is read from bootstrap.
let old_state = self.state.load();
Expand Down Expand Up @@ -227,7 +230,7 @@ impl DirectSuperBlockV6 {
let mut v6_chunk = RafsV6InodeChunkAddr::new();
v6_chunk.set_blob_index(chunk.blob_index());
v6_chunk.set_blob_ci_index(chunk.id());
v6_chunk.set_block_addr((chunk.uncompressed_offset() / EROFS_BLOCK_SIZE) as u32);
v6_chunk.set_block_addr((chunk.uncompressed_offset() / EROFS_BLOCK_SIZE_4096) as u32);
chunk_map.insert(v6_chunk, idx);
}

Expand All @@ -238,7 +241,7 @@ impl DirectSuperBlockV6 {
impl RafsSuperInodes for DirectSuperBlockV6 {
fn get_max_ino(&self) -> Inode {
// The maximum inode number supported by RAFSv6 is smaller than limit of fuse-backend-rs.
(0xffff_ffffu64) * EROFS_BLOCK_SIZE / EROFS_INODE_SLOT_SIZE as u64
(0xffff_ffffu64) * EROFS_BLOCK_SIZE_4096 / EROFS_INODE_SLOT_SIZE as u64
}

/// Find inode offset by ino from inode table and mmap to OndiskInode.
Expand Down Expand Up @@ -323,7 +326,7 @@ impl OndiskInodeWrapper {
offset: usize,
) -> Result<Self> {
let inode = DirectSuperBlockV6::disk_inode(state, offset)?;
let blocks_count = div_round_up(inode.size(), EROFS_BLOCK_SIZE);
let blocks_count = div_round_up(inode.size(), EROFS_BLOCK_SIZE_4096);

Ok(OndiskInodeWrapper {
mapping,
Expand Down Expand Up @@ -358,7 +361,7 @@ impl OndiskInodeWrapper {
index: usize,
) -> RafsResult<&'a RafsV6Dirent> {
let offset = self.data_block_offset(inode, block_index)?;
if size_of::<RafsV6Dirent>() * (index + 1) >= EROFS_BLOCK_SIZE as usize {
if size_of::<RafsV6Dirent>() * (index + 1) >= EROFS_BLOCK_SIZE_4096 as usize {
Err(RafsError::InvalidImageData)
} else if let Some(offset) = offset.checked_add(size_of::<RafsV6Dirent>() * index) {
state
Expand Down Expand Up @@ -386,7 +389,7 @@ impl OndiskInodeWrapper {
let buf: &[u8] = match index.cmp(&(max_entries - 1)) {
Ordering::Less => {
let next_de = self.get_entry(state, inode, block_index, index + 1)?;
if next_de.e_nameoff as u64 >= EROFS_BLOCK_SIZE {
if next_de.e_nameoff as u64 >= EROFS_BLOCK_SIZE_4096 {
return Err(RafsError::InvalidImageData);
}
let len = next_de.e_nameoff.checked_sub(de.e_nameoff).ok_or_else(|| {
Expand All @@ -407,7 +410,7 @@ impl OndiskInodeWrapper {
}
Ordering::Equal => {
let base = de.e_nameoff as u64;
if base >= EROFS_BLOCK_SIZE {
if base >= EROFS_BLOCK_SIZE_4096 {
return Err(RafsError::InvalidImageData);
}

Expand All @@ -416,12 +419,12 @@ impl OndiskInodeWrapper {
// Because the other blocks should be fully used, while the last may not.
let block_count = self.blocks_count() as usize;
let len = match block_count.cmp(&(block_index + 1)) {
Ordering::Greater => (EROFS_BLOCK_SIZE - base) as usize,
Ordering::Greater => (EROFS_BLOCK_SIZE_4096 - base) as usize,
Ordering::Equal => {
if self.size() % EROFS_BLOCK_SIZE == 0 {
EROFS_BLOCK_SIZE as usize
if self.size() % EROFS_BLOCK_SIZE_4096 == 0 {
EROFS_BLOCK_SIZE_4096 as usize
} else {
(self.size() % EROFS_BLOCK_SIZE - base) as usize
(self.size() % EROFS_BLOCK_SIZE_4096 - base) as usize
}
}
Ordering::Less => return Err(RafsError::InvalidImageData),
Expand Down Expand Up @@ -486,7 +489,7 @@ impl OndiskInodeWrapper {
if base.checked_add(index).is_none() || base + index > u32::MAX as usize {
Err(RafsError::InvalidImageData)
} else {
Ok((base + index) * EROFS_BLOCK_SIZE as usize)
Ok((base + index) * EROFS_BLOCK_SIZE_4096 as usize)
}
}

Expand Down Expand Up @@ -671,7 +674,9 @@ impl OndiskInodeWrapper {
.get_entry(&state, inode, block_index, 0)
.map_err(err_invalidate_data)?;
let name_offset = head_entry.e_nameoff as usize;
if name_offset as u64 >= EROFS_BLOCK_SIZE || name_offset % size_of::<RafsV6Dirent>() != 0 {
if name_offset as u64 >= EROFS_BLOCK_SIZE_4096
|| name_offset % size_of::<RafsV6Dirent>() != 0
{
Err(enoent!(format!(
"v6: invalid e_nameoff {} from directory entry",
name_offset
Expand All @@ -689,7 +694,7 @@ impl RafsInode for OndiskInodeWrapper {
let max_inode = self.mapping.get_max_ino();

if self.ino() > max_inode
|| self.offset > (u32::MAX as usize) * EROFS_BLOCK_SIZE as usize
|| self.offset > (u32::MAX as usize) * EROFS_BLOCK_SIZE_4096 as usize
|| inode.nlink() == 0
|| self.get_name_size() as usize > (RAFS_MAX_NAME + 1)
{
Expand Down Expand Up @@ -989,7 +994,7 @@ impl RafsInode for OndiskInodeWrapper {
fn get_symlink(&self) -> Result<OsString> {
let state = self.state();
let inode = self.disk_inode(&state);
if inode.size() > EROFS_BLOCK_SIZE {
if inode.size() > EROFS_BLOCK_SIZE_4096 {
return Err(einval!(format!(
"v6: invalid symlink size {}",
inode.size()
Expand Down
Loading

0 comments on commit d0b07e1

Please sign in to comment.