Skip to content

Commit ce62847

Browse files
author
maxing.lan
committed
fix: builder: fix wrong use of blob meta info of optimize subcommand
Blob meta info is only for fs-version 6, so we move the corresponding code in one place for better judgement branch. Besides, fix two potential bugs: 1. for fs-version 5, we don't align the chunk offset up to 4k, wrong rounding up will cause coredump in runtime. 2. use BlobChunkInfoV2Ondisk instead of BlobChunkInfoV1Ondisk to keep consistent with generate_chunk_info() which returns BlobChunkInfoV2Ondisk. Theoretical risk, not fully verified. Signed-off-by: maxing.lan <[email protected]>
1 parent fc811b5 commit ce62847

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

builder/src/optimize_prefetch.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use nydus_rafs::metadata::RafsSuper;
2222
use nydus_rafs::metadata::RafsVersion;
2323
use nydus_storage::device::BlobInfo;
2424
use nydus_storage::meta::BatchContextGenerator;
25-
use nydus_storage::meta::BlobChunkInfoV1Ondisk;
25+
use nydus_storage::meta::BlobChunkInfoV2Ondisk;
2626
use nydus_utils::compress;
2727
use sha2::Digest;
2828
use std::cmp::{max, min};
@@ -239,9 +239,11 @@ impl OptimizePrefetch {
239239
blob_mgr.add_blob(blob_state.blob_ctx.clone());
240240
blob_mgr.set_current_blob_index(0);
241241
Blob::finalize_blob_data(&ctx, &mut blob_mgr, blob_state.blob_writer.as_mut())?;
242-
if let Some((_, blob_ctx)) = blob_mgr.get_current_blob() {
243-
Blob::dump_meta_data(&ctx, blob_ctx, blob_state.blob_writer.as_mut()).unwrap();
244-
};
242+
if let RafsBlobTable::V6(_) = blob_table {
243+
if let Some((_, blob_ctx)) = blob_mgr.get_current_blob() {
244+
Blob::dump_meta_data(&ctx, blob_ctx, blob_state.blob_writer.as_mut()).unwrap();
245+
};
246+
}
245247
ctx.blob_id = String::from("");
246248
blob_mgr.get_current_blob().unwrap().1.blob_id = String::from("");
247249
finalize_blob(ctx, &mut blob_mgr, blob_state.blob_writer.as_mut())?;
@@ -335,12 +337,6 @@ impl OptimizePrefetch {
335337
blob_file.seek(std::io::SeekFrom::Start(inner.compressed_offset()))?;
336338
blob_file.read_exact(&mut buf)?;
337339
prefetch_state.blob_writer.write_all(&buf)?;
338-
let info = batch.generate_chunk_info(
339-
blob_ctx.current_compressed_offset,
340-
blob_ctx.current_uncompressed_offset,
341-
inner.uncompressed_size(),
342-
encrypted,
343-
)?;
344340
inner.set_blob_index(blob_info.blob_index());
345341
if blob_ctx.chunk_count == u32::MAX {
346342
blob_ctx.chunk_count = 0;
@@ -349,27 +345,36 @@ impl OptimizePrefetch {
349345
blob_ctx.chunk_count += 1;
350346
inner.set_compressed_offset(blob_ctx.current_compressed_offset);
351347
inner.set_uncompressed_offset(blob_ctx.current_uncompressed_offset);
352-
let aligned_d_size: u64 = nydus_utils::try_round_up_4k(inner.uncompressed_size())
353-
.ok_or_else(|| anyhow!("invalid size"))?;
348+
let mut aligned_d_size: u64 = inner.uncompressed_size() as u64;
349+
if let RafsBlobTable::V6(_) = blob_table {
350+
aligned_d_size = nydus_utils::try_round_up_4k(inner.uncompressed_size())
351+
.ok_or_else(|| anyhow!("invalid size"))?;
352+
let info = batch.generate_chunk_info(
353+
blob_ctx.current_compressed_offset,
354+
blob_ctx.current_uncompressed_offset,
355+
inner.uncompressed_size(),
356+
encrypted,
357+
)?;
358+
blob_info.set_meta_ci_compressed_size(
359+
(blob_info.meta_ci_compressed_size()
360+
+ size_of::<BlobChunkInfoV2Ondisk>() as u64) as usize,
361+
);
362+
363+
blob_info.set_meta_ci_uncompressed_size(
364+
(blob_info.meta_ci_uncompressed_size()
365+
+ size_of::<BlobChunkInfoV2Ondisk>() as u64) as usize,
366+
);
367+
blob_ctx.add_chunk_meta_info(&inner, Some(info))?;
368+
}
354369
blob_ctx.compressed_blob_size += inner.compressed_size() as u64;
355370
blob_ctx.uncompressed_blob_size += aligned_d_size;
356371
blob_ctx.current_compressed_offset += inner.compressed_size() as u64;
357372
blob_ctx.current_uncompressed_offset += aligned_d_size;
358-
blob_ctx.add_chunk_meta_info(&inner, Some(info))?;
359373
blob_ctx.blob_hash.update(&buf);
360374

361375
blob_info.set_compressed_size(blob_ctx.compressed_blob_size as usize);
362376
blob_info.set_uncompressed_size(blob_ctx.uncompressed_blob_size as usize);
363377
blob_info.set_chunk_count(blob_ctx.chunk_count as usize);
364-
blob_info.set_meta_ci_compressed_size(
365-
(blob_info.meta_ci_compressed_size() + size_of::<BlobChunkInfoV1Ondisk>() as u64)
366-
as usize,
367-
);
368-
369-
blob_info.set_meta_ci_uncompressed_size(
370-
(blob_info.meta_ci_uncompressed_size() + size_of::<BlobChunkInfoV1Ondisk>() as u64)
371-
as usize,
372-
);
373378
}
374379

375380
Ok(())

0 commit comments

Comments
 (0)