From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 2587F1FF0AA for ; Fri, 21 Aug 2026 16:02:51 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id EB9E2215BF; Fri, 21 Aug 2026 16:02:50 +0200 (CEST) From: "Max R. Carrara" To: pbs-devel@lists.proxmox.com Subject: [PATCH proxmox-backup v2 03/10] tape: move tape block structs into separate file module Date: Fri, 21 Aug 2026 16:02:27 +0200 Message-ID: <20260821140238.615302-4-m.carrara@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260821140238.615302-1-m.carrara@proxmox.com> References: <20260821140238.615302-1-m.carrara@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1787320937922 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.704 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: SY6U62XDQJ2B7GE72WMTDV5HDTFA7MCU X-Message-ID-Hash: SY6U62XDQJ2B7GE72WMTDV5HDTFA7MCU X-MailFrom: m.carrara@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Move `BlockHeader` and `BlockHeaderFlags` into a separate file for overall better organization and to make future changes a little easier to follow. Also, re-export both types as `pub(crate)`, since there is not really any reason for them to be completely public. Signed-off-by: Max R. Carrara --- pbs-tape/src/lib.rs | 79 ++------------------------------------ pbs-tape/src/tape_block.rs | 79 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 76 deletions(-) create mode 100644 pbs-tape/src/tape_block.rs diff --git a/pbs-tape/src/lib.rs b/pbs-tape/src/lib.rs index 0fe55a749..6cff175dd 100644 --- a/pbs-tape/src/lib.rs +++ b/pbs-tape/src/lib.rs @@ -1,7 +1,6 @@ use std::collections::HashSet; use anyhow::{Error, bail}; -use bitflags::bitflags; use endian_trait::Endian; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -20,6 +19,9 @@ pub use blocked_reader::BlockedReader; mod blocked_writer; pub use blocked_writer::BlockedWriter; +mod tape_block; +pub(crate) use tape_block::{BlockHeader, BlockHeaderFlags}; + mod tape_write; pub use tape_write::*; @@ -49,41 +51,6 @@ pub const PROXMOX_BACKUP_MEDIA_LABEL_MAGIC_1_0: [u8; 8] = [42, 5, 191, 60, 176, // openssl::sha::sha256(b"Proxmox Backup MediaSet Label v1.0") pub const PROXMOX_BACKUP_MEDIA_SET_LABEL_MAGIC_1_0: [u8; 8] = [8, 96, 99, 249, 47, 151, 83, 216]; -/// Tape Block Header with data payload -/// -/// All tape files are written as sequence of blocks. -/// -/// Note: this struct is large, never put this on the stack! -/// so we use an unsized type to avoid that. -/// -/// Tape data block are always read/written with a fixed size -/// (`PROXMOX_TAPE_BLOCK_SIZE`). But they may contain less data, so the -/// header has an additional size field. For streams of blocks, there -/// is a sequence number (`seq_nr`) which may be use for additional -/// error checking. -#[repr(C, packed)] -pub struct BlockHeader { - /// fixed value `PROXMOX_TAPE_BLOCK_HEADER_MAGIC_1_0` - pub magic: [u8; 8], - pub flags: BlockHeaderFlags, - /// size as 3 bytes unsigned, little endian - pub size: [u8; 3], - /// block sequence number - pub seq_nr: u32, - pub payload: [u8], -} - -bitflags! { - /// Header flags (e.g. `END_OF_STREAM` or `INCOMPLETE`) - #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] - pub struct BlockHeaderFlags: u8 { - /// Marks the last block in a stream. - const END_OF_STREAM = 0b00000001; - /// Mark multivolume streams (when set in the last block) - const INCOMPLETE = 0b00000010; - } -} - #[derive(Endian, Copy, Clone, Debug)] #[repr(C, packed)] /// Media Content Header @@ -152,46 +119,6 @@ impl MediaContentHeader { } } -impl BlockHeader { - pub const SIZE: usize = PROXMOX_TAPE_BLOCK_SIZE; - - /// Allocates a new instance on the heap - pub fn new() -> Box { - use std::alloc::{Layout, alloc_zeroed}; - - // align to PAGESIZE, so that we can use it with SG_IO - let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize; - - let mut buffer = unsafe { - let ptr = alloc_zeroed(Layout::from_size_align(Self::SIZE, page_size).unwrap()); - Box::from_raw(core::ptr::slice_from_raw_parts_mut(ptr, Self::SIZE - 16) as *mut Self) - }; - buffer.magic = PROXMOX_TAPE_BLOCK_HEADER_MAGIC_1_0; - buffer - } - - /// Set the `size` field - pub fn set_size(&mut self, size: usize) { - let size = size.to_le_bytes(); - self.size.copy_from_slice(&size[..3]); - } - - /// Returns the `size` field - pub fn size(&self) -> usize { - (self.size[0] as usize) + ((self.size[1] as usize) << 8) + ((self.size[2] as usize) << 16) - } - - /// Set the `seq_nr` field - pub fn set_seq_nr(&mut self, seq_nr: u32) { - self.seq_nr = seq_nr.to_le(); - } - - /// Returns the `seq_nr` field - pub fn seq_nr(&self) -> u32 { - u32::from_le(self.seq_nr) - } -} - /// Changer element status. /// /// Drive and slots may be `Empty`, or contain some media, either diff --git a/pbs-tape/src/tape_block.rs b/pbs-tape/src/tape_block.rs new file mode 100644 index 000000000..4f9c2192f --- /dev/null +++ b/pbs-tape/src/tape_block.rs @@ -0,0 +1,79 @@ +use bitflags::bitflags; + +use crate::PROXMOX_TAPE_BLOCK_HEADER_MAGIC_1_0; +use crate::PROXMOX_TAPE_BLOCK_SIZE; + +/// Tape Block Header with data payload +/// +/// All tape files are written as sequence of blocks. +/// +/// Note: this struct is large, never put this on the stack! +/// so we use an unsized type to avoid that. +/// +/// Tape data block are always read/written with a fixed size +/// (`PROXMOX_TAPE_BLOCK_SIZE`). But they may contain less data, so the +/// header has an additional size field. For streams of blocks, there +/// is a sequence number (`seq_nr`) which may be use for additional +/// error checking. +#[repr(C, packed)] +pub struct BlockHeader { + /// fixed value `PROXMOX_TAPE_BLOCK_HEADER_MAGIC_1_0` + pub magic: [u8; 8], + pub flags: BlockHeaderFlags, + /// size as 3 bytes unsigned, little endian + pub size: [u8; 3], + /// block sequence number + pub seq_nr: u32, + pub payload: [u8], +} + +bitflags! { + /// Header flags (e.g. `END_OF_STREAM` or `INCOMPLETE`) + #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] + pub struct BlockHeaderFlags: u8 { + /// Marks the last block in a stream. + const END_OF_STREAM = 0b00000001; + /// Mark multivolume streams (when set in the last block) + const INCOMPLETE = 0b00000010; + } +} + +impl BlockHeader { + pub const SIZE: usize = PROXMOX_TAPE_BLOCK_SIZE; + + /// Allocates a new instance on the heap + pub fn new() -> Box { + use std::alloc::{Layout, alloc_zeroed}; + + // align to PAGESIZE, so that we can use it with SG_IO + let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize; + + let mut buffer = unsafe { + let ptr = alloc_zeroed(Layout::from_size_align(Self::SIZE, page_size).unwrap()); + Box::from_raw(core::ptr::slice_from_raw_parts_mut(ptr, Self::SIZE - 16) as *mut Self) + }; + buffer.magic = PROXMOX_TAPE_BLOCK_HEADER_MAGIC_1_0; + buffer + } + + /// Set the `size` field + pub fn set_size(&mut self, size: usize) { + let size = size.to_le_bytes(); + self.size.copy_from_slice(&size[..3]); + } + + /// Returns the `size` field + pub fn size(&self) -> usize { + (self.size[0] as usize) + ((self.size[1] as usize) << 8) + ((self.size[2] as usize) << 16) + } + + /// Set the `seq_nr` field + pub fn set_seq_nr(&mut self, seq_nr: u32) { + self.seq_nr = seq_nr.to_le(); + } + + /// Returns the `seq_nr` field + pub fn seq_nr(&self) -> u32 { + u32::from_le(self.seq_nr) + } +} -- 2.47.3