Skip to content

Commit

Permalink
storage: rename BlobChunkInfoOndisk to BlobChunkInfoV1Ondisk
Browse files Browse the repository at this point in the history
Rename BlobChunkInfoOndisk to BlobChunkInfoV1Ondisk.

Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
  • Loading branch information
jiangliu committed Oct 17, 2022
1 parent cec5d13 commit 69339ef
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 156 deletions.
6 changes: 3 additions & 3 deletions src/bin/nydus-image/core/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::Write;

use anyhow::{Context, Result};
use nydus_rafs::metadata::RAFS_MAX_CHUNK_SIZE;
use nydus_storage::meta::{BlobChunkInfoOndisk, BlobMetaHeaderOndisk};
use nydus_storage::meta::{BlobChunkInfoV1Ondisk, BlobMetaHeaderOndisk};
use nydus_utils::{compress, try_round_up_4k};
use sha2::Digest;

Expand Down Expand Up @@ -75,13 +75,13 @@ impl Blob {

fn dump_meta_data_raw(
pos: u64,
blob_meta_info: &[BlobChunkInfoOndisk],
blob_meta_info: &[BlobChunkInfoV1Ondisk],
compressor: compress::Algorithm,
) -> Result<(std::borrow::Cow<[u8]>, BlobMetaHeaderOndisk)> {
let data = unsafe {
std::slice::from_raw_parts(
blob_meta_info.as_ptr() as *const u8,
blob_meta_info.len() * std::mem::size_of::<BlobChunkInfoOndisk>(),
blob_meta_info.len() * std::mem::size_of::<BlobChunkInfoV1Ondisk>(),
)
};
let (buf, compressed) = compress::compress(data, compressor)
Expand Down
6 changes: 3 additions & 3 deletions src/bin/nydus-image/core/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use nydus_rafs::metadata::{Inode, RAFS_DEFAULT_CHUNK_SIZE};
use nydus_rafs::metadata::{RafsSuperFlags, RafsVersion};
use nydus_rafs::{RafsIoReader, RafsIoWrite};
use nydus_storage::device::{BlobFeatures, BlobInfo};
use nydus_storage::meta::{BlobChunkInfoOndisk, BlobMetaHeaderOndisk};
use nydus_storage::meta::{BlobChunkInfoV1Ondisk, BlobMetaHeaderOndisk};
use nydus_utils::{compress, digest, div_round_up, round_down_4k};

use super::chunk_dict::{ChunkDict, HashChunkDict};
Expand Down Expand Up @@ -330,7 +330,7 @@ pub struct BlobContext {
pub blob_meta_info_enabled: bool,
/// Data chunks stored in the data blob, for v6.
/// TODO: zran
pub blob_meta_info: Vec<BlobChunkInfoOndisk>,
pub blob_meta_info: Vec<BlobChunkInfoV1Ondisk>,
/// Blob metadata header stored in the data blob, for v6
pub blob_meta_header: BlobMetaHeaderOndisk,

Expand Down Expand Up @@ -453,7 +453,7 @@ impl BlobContext {
}

debug_assert!(chunk.index() as usize == self.blob_meta_info.len());
let mut meta = BlobChunkInfoOndisk::default();
let mut meta = BlobChunkInfoV1Ondisk::default();
meta.set_compressed_offset(chunk.compressed_offset());
meta.set_compressed_size(chunk.compressed_size());
meta.set_uncompressed_offset(chunk.uncompressed_offset());
Expand Down
127 changes: 127 additions & 0 deletions storage/src/meta/chunk_info_v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (C) 2021-2022 Alibaba Cloud. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0

use crate::meta::{BLOB_METADATA_CHUNK_SIZE_MASK, round_up_4k};

const BLOB_METADATA_V1_CHUNK_COMP_OFFSET_MASK: u64 = 0xff_ffff_ffff;
const BLOB_METADATA_V1_CHUNK_UNCOMP_OFFSET_MASK: u64 = 0xfff_ffff_f000;
const BLOB_METADATA_V1_CHUNK_SIZE_LOW_MASK: u64 = 0x0f_ffff;
const BLOB_METADATA_V1_CHUNK_SIZE_HIGH_MASK: u64 = 0xf0_0000;
const BLOB_METADATA_V1_CHUNK_SIZE_LOW_SHIFT: u64 = 44;
const BLOB_METADATA_V1_CHUNK_SIZE_HIGH_COMP_SHIFT: u64 = 20;
const BLOB_METADATA_V1_CHUNK_SIZE_HIGH_UNCOMP_SHIFT: u64 = 12;

/// Blob chunk compression information on disk format.
#[repr(C)]
#[derive(Clone, Copy, Default)]
pub struct BlobChunkInfoV1Ondisk {
// 20bits: size (low), 32bits: offset, 4bits: size (high), 8bits reserved
uncomp_info: u64,
// 20bits: size (low), 4bits: size (high), offset: 40bits
comp_info: u64,
}

impl BlobChunkInfoV1Ondisk {
/// Get compressed offset of the chunk.
#[inline]
pub fn compressed_offset(&self) -> u64 {
self.comp_info & BLOB_METADATA_V1_CHUNK_COMP_OFFSET_MASK
}

/// Set compressed offset of the chunk.
#[inline]
pub fn set_compressed_offset(&mut self, offset: u64) {
assert!(offset & !BLOB_METADATA_V1_CHUNK_COMP_OFFSET_MASK == 0);
self.comp_info &= !BLOB_METADATA_V1_CHUNK_COMP_OFFSET_MASK;
self.comp_info |= offset & BLOB_METADATA_V1_CHUNK_COMP_OFFSET_MASK;
}

/// Get compressed size of the chunk.
#[inline]
pub fn compressed_size(&self) -> u32 {
let bit20 = self.comp_info >> BLOB_METADATA_V1_CHUNK_SIZE_LOW_SHIFT;
let bit4 = (self.comp_info & 0xf0000000000) >> BLOB_METADATA_V1_CHUNK_SIZE_HIGH_COMP_SHIFT;
(bit4 | bit20) as u32 + 1
}

/// Set compressed size of the chunk.
#[inline]
pub fn set_compressed_size(&mut self, size: u32) {
let size = size as u64;
assert!(size > 0 && size <= BLOB_METADATA_CHUNK_SIZE_MASK + 1);

let size_low = ((size - 1) & BLOB_METADATA_V1_CHUNK_SIZE_LOW_MASK)
<< BLOB_METADATA_V1_CHUNK_SIZE_LOW_SHIFT;
let size_high = ((size - 1) & BLOB_METADATA_V1_CHUNK_SIZE_HIGH_MASK)
<< BLOB_METADATA_V1_CHUNK_SIZE_HIGH_COMP_SHIFT;
let offset = self.comp_info & BLOB_METADATA_V1_CHUNK_COMP_OFFSET_MASK;

self.comp_info = size_low | size_high | offset;
}

/// Get compressed end of the chunk.
#[inline]
pub fn compressed_end(&self) -> u64 {
self.compressed_offset() + self.compressed_size() as u64
}

/// Get uncompressed offset of the chunk.
#[inline]
pub fn uncompressed_offset(&self) -> u64 {
self.uncomp_info & BLOB_METADATA_V1_CHUNK_UNCOMP_OFFSET_MASK
}

/// Set uncompressed offset of the chunk.
#[inline]
pub fn set_uncompressed_offset(&mut self, offset: u64) {
assert!(offset & !BLOB_METADATA_V1_CHUNK_UNCOMP_OFFSET_MASK == 0);
self.uncomp_info &= !BLOB_METADATA_V1_CHUNK_UNCOMP_OFFSET_MASK;
self.uncomp_info |= offset & BLOB_METADATA_V1_CHUNK_UNCOMP_OFFSET_MASK;
}

/// Get uncompressed end of the chunk.
#[inline]
pub fn uncompressed_size(&self) -> u32 {
let size_high = (self.uncomp_info & 0xf00) << BLOB_METADATA_V1_CHUNK_SIZE_HIGH_UNCOMP_SHIFT;
let size_low = self.uncomp_info >> BLOB_METADATA_V1_CHUNK_SIZE_LOW_SHIFT;
(size_high | size_low) as u32 + 1
}

/// Set uncompressed end of the chunk.
#[inline]
pub fn set_uncompressed_size(&mut self, size: u32) {
let size = size as u64;
assert!(size != 0 && size <= BLOB_METADATA_CHUNK_SIZE_MASK + 1);

let size_low = ((size - 1) & BLOB_METADATA_V1_CHUNK_SIZE_LOW_MASK)
<< BLOB_METADATA_V1_CHUNK_SIZE_LOW_SHIFT;
let size_high = ((size - 1) & BLOB_METADATA_V1_CHUNK_SIZE_HIGH_MASK)
>> BLOB_METADATA_V1_CHUNK_SIZE_HIGH_UNCOMP_SHIFT;
let offset = self.uncomp_info & BLOB_METADATA_V1_CHUNK_UNCOMP_OFFSET_MASK;

self.uncomp_info = size_low | offset | size_high;
}

/// Get uncompressed size of the chunk.
#[inline]
pub fn uncompressed_end(&self) -> u64 {
self.uncompressed_offset() + self.uncompressed_size() as u64
}

/// Get 4k aligned uncompressed size of the chunk.
#[inline]
pub fn aligned_uncompressed_end(&self) -> u64 {
round_up_4k(self.uncompressed_end())
}

/// Check whether the blob chunk is compressed or not.
///
/// Assume the image builder guarantee that compress_size < uncompress_size if the chunk is
/// compressed.
#[inline]
pub fn is_compressed(&self) -> bool {
self.compressed_size() != self.uncompressed_size()
}
}

Loading

0 comments on commit 69339ef

Please sign in to comment.