tape: introduce trait BlockWrite

This commit is contained in:
Dietmar Maurer 2021-03-29 12:52:26 +02:00
parent 0db5712493
commit 1336ae8249
5 changed files with 144 additions and 126 deletions

View File

@ -1,7 +1,7 @@
//! Driver for Linux SCSI tapes //! Driver for Linux SCSI tapes
use std::fs::{OpenOptions, File}; use std::fs::{OpenOptions, File};
use std::io::Read; use std::io::{Read, Write};
use std::os::unix::fs::OpenOptionsExt; use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::convert::TryFrom; use std::convert::TryFrom;
@ -9,8 +9,10 @@ use std::convert::TryFrom;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use nix::fcntl::{fcntl, FcntlArg, OFlag}; use nix::fcntl::{fcntl, FcntlArg, OFlag};
use proxmox::sys::error::SysResult; use proxmox::{
use proxmox::tools::Uuid; tools::Uuid,
sys::error::{SysError, SysResult},
};
use crate::{ use crate::{
config, config,
@ -25,6 +27,7 @@ use crate::{
LinuxDriveAndMediaStatus, LinuxDriveAndMediaStatus,
}, },
tape::{ tape::{
BlockWrite,
BlockRead, BlockRead,
BlockReadStatus, BlockReadStatus,
TapeRead, TapeRead,
@ -257,10 +260,9 @@ impl LinuxTapeHandle {
Ok(()) Ok(())
} }
/// Write a single EOF mark /// Write a single EOF mark without flushing buffers
pub fn write_eof_mark(&self) -> Result<(), Error> { pub fn write_eof_mark(&mut self) -> Result<(), std::io::Error> {
tape_write_eof_mark(&self.file)?; tape_write_eof_mark(&mut self.file)
Ok(())
} }
/// Set the drive's block length to the value specified. /// Set the drive's block length to the value specified.
@ -519,10 +521,8 @@ impl TapeDriver for LinuxTapeHandle {
fn write_file<'a>(&'a mut self) -> Result<Box<dyn TapeWrite + 'a>, std::io::Error> { fn write_file<'a>(&'a mut self) -> Result<Box<dyn TapeWrite + 'a>, std::io::Error> {
let handle = TapeWriterHandle { let writer = LinuxTapeWriter::new(&mut self.file);
writer: BlockedWriter::new(&mut self.file), let handle = BlockedWriter::new(writer);
};
Ok(Box::new(handle)) Ok(Box::new(handle))
} }
@ -545,28 +545,28 @@ impl TapeDriver for LinuxTapeHandle {
self.set_encryption(None)?; self.set_encryption(None)?;
let mut handle = TapeWriterHandle { { // limit handle scope
writer: BlockedWriter::new(&mut self.file), let mut handle = self.write_file()?;
};
let mut value = serde_json::to_value(media_set_label)?; let mut value = serde_json::to_value(media_set_label)?;
if media_set_label.encryption_key_fingerprint.is_some() { if media_set_label.encryption_key_fingerprint.is_some() {
match key_config { match key_config {
Some(key_config) => { Some(key_config) => {
value["key-config"] = serde_json::to_value(key_config)?; value["key-config"] = serde_json::to_value(key_config)?;
} }
None => { None => {
bail!("missing encryption key config"); bail!("missing encryption key config");
}
} }
} }
let raw = serde_json::to_string_pretty(&value)?;
let header = MediaContentHeader::new(PROXMOX_BACKUP_MEDIA_SET_LABEL_MAGIC_1_0, raw.len() as u32);
handle.write_header(&header, raw.as_bytes())?;
handle.finish(false)?;
} }
let raw = serde_json::to_string_pretty(&value)?;
let header = MediaContentHeader::new(PROXMOX_BACKUP_MEDIA_SET_LABEL_MAGIC_1_0, raw.len() as u32);
handle.write_header(&header, raw.as_bytes())?;
handle.finish(false)?;
self.sync()?; // sync data to tape self.sync()?; // sync data to tape
Ok(()) Ok(())
@ -655,7 +655,7 @@ impl TapeDriver for LinuxTapeHandle {
} }
/// Write a single EOF mark without flushing buffers /// Write a single EOF mark without flushing buffers
fn tape_write_eof_mark(file: &File) -> Result<(), std::io::Error> { fn tape_write_eof_mark(file: &mut File) -> Result<(), std::io::Error> {
let cmd = mtop { mt_op: MTCmd::MTWEOFI, mt_count: 1 }; let cmd = mtop { mt_op: MTCmd::MTWEOFI, mt_count: 1 };
@ -745,29 +745,67 @@ pub fn read_tapedev_options(file: &File) -> Result<SetDrvBufferOptions, Error> {
} }
/// like BlockedWriter, but writes EOF mark on finish struct LinuxTapeWriter<'a> {
pub struct TapeWriterHandle<'a> { /// Assumes that 'file' is a linux tape device.
writer: BlockedWriter<&'a mut File>, file: &'a mut File,
} }
impl TapeWrite for TapeWriterHandle<'_> { impl <'a> LinuxTapeWriter<'a> {
pub fn new(file: &'a mut File) -> Self {
Self { file }
}
}
fn write_all(&mut self, data: &[u8]) -> Result<bool, std::io::Error> { impl <'a> BlockWrite for LinuxTapeWriter<'a> {
self.writer.write_all(data)
/// Write a single block to a linux tape device
///
/// EOM Behaviour on Linux: When the end of medium early warning is
/// encountered, the current write is finished and the number of bytes
/// is returned. The next write returns -1 and errno is set to
/// ENOSPC. To enable writing a trailer, the next write is allowed to
/// proceed and, if successful, the number of bytes is returned. After
/// this, -1 and the number of bytes are alternately returned until
/// the physical end of medium (or some other error) is encountered.
///
/// See: https://github.com/torvalds/linux/blob/master/Documentation/scsi/st.rst
///
/// On success, this returns if we en countered a EOM condition.
fn write_block(&mut self, data: &[u8]) -> Result<bool, std::io::Error> {
let mut leof = false;
loop {
match self.file.write(data) {
Ok(count) if count == data.len() => return Ok(leof),
Ok(count) if count > 0 => {
proxmox::io_bail!(
"short block write ({} < {}). Tape drive uses wrong block size.",
count, data.len());
}
Ok(_) => { // count is 0 here, assume EOT
return Err(std::io::Error::from_raw_os_error(nix::errno::Errno::ENOSPC as i32));
}
// handle interrupted system call
Err(err) if err.kind() == std::io::ErrorKind::Interrupted => {
continue;
}
// detect and handle LEOM (early warning)
Err(err) if err.is_errno(nix::errno::Errno::ENOSPC) => {
if leof {
return Err(err);
} else {
leof = true;
continue; // next write will succeed
}
}
Err(err) => return Err(err),
}
}
} }
fn bytes_written(&self) -> usize { fn write_filemark(&mut self) -> Result<(), std::io::Error> {
self.writer.bytes_written() tape_write_eof_mark(&mut self.file)
}
fn finish(&mut self, incomplete: bool) -> Result<bool, std::io::Error> {
let leof = self.writer.finish(incomplete)?;
tape_write_eof_mark(self.writer.writer_ref_mut())?;
Ok(leof)
}
fn logical_end_of_media(&self) -> bool {
self.writer.logical_end_of_media()
} }
} }

View File

@ -276,8 +276,7 @@ impl TapeDriver for VirtualTapeHandle {
free_space = self.max_size - used_space; free_space = self.max_size - used_space;
} }
let writer = Box::new(file); let writer = EmulateTapeWriter::new(file, free_space);
let writer = Box::new(EmulateTapeWriter::new(writer, free_space));
let writer = Box::new(BlockedWriter::new(writer)); let writer = Box::new(BlockedWriter::new(writer));
Ok(writer) Ok(writer)

View File

@ -1,10 +1,8 @@
use std::io::Write;
use proxmox::tools::vec; use proxmox::tools::vec;
use crate::tape::{ use crate::tape::{
TapeWrite, TapeWrite,
tape_device_write_block, BlockWrite,
file_formats::{ file_formats::{
BlockHeader, BlockHeader,
BlockHeaderFlags, BlockHeaderFlags,
@ -16,16 +14,27 @@ use crate::tape::{
/// This type implement 'TapeWrite'. Data written is assembled to /// This type implement 'TapeWrite'. Data written is assembled to
/// equally sized blocks (see 'BlockHeader'), which are then written /// equally sized blocks (see 'BlockHeader'), which are then written
/// to the underlying writer. /// to the underlying writer.
pub struct BlockedWriter<W> { pub struct BlockedWriter<W: BlockWrite> {
writer: W, writer: W,
buffer: Box<BlockHeader>, buffer: Box<BlockHeader>,
buffer_pos: usize, buffer_pos: usize,
seq_nr: u32, seq_nr: u32,
logical_end_of_media: bool, logical_end_of_media: bool,
bytes_written: usize, bytes_written: usize,
wrote_eof: bool,
} }
impl <W: Write> BlockedWriter<W> { impl <W: BlockWrite> Drop for BlockedWriter<W> {
// Try to make sure to end the file with a filemark
fn drop(&mut self) {
if !self.wrote_eof {
let _ = self.writer.write_filemark();
}
}
}
impl <W: BlockWrite> BlockedWriter<W> {
/// Allow access to underlying writer /// Allow access to underlying writer
pub fn writer_ref_mut(&mut self) -> &mut W { pub fn writer_ref_mut(&mut self) -> &mut W {
@ -41,6 +50,7 @@ impl <W: Write> BlockedWriter<W> {
seq_nr: 0, seq_nr: 0,
logical_end_of_media: false, logical_end_of_media: false,
bytes_written: 0, bytes_written: 0,
wrote_eof: false,
} }
} }
@ -52,7 +62,16 @@ impl <W: Write> BlockedWriter<W> {
BlockHeader::SIZE, BlockHeader::SIZE,
) )
}; };
tape_device_write_block(writer, data) writer.write_block(data)
}
fn write_eof(&mut self) -> Result<(), std::io::Error> {
if self.wrote_eof {
proxmox::io_bail!("BlockedWriter: detected multiple EOF writes");
}
self.wrote_eof = true;
self.writer.write_filemark()
} }
fn write(&mut self, data: &[u8]) -> Result<usize, std::io::Error> { fn write(&mut self, data: &[u8]) -> Result<usize, std::io::Error> {
@ -85,7 +104,7 @@ impl <W: Write> BlockedWriter<W> {
} }
impl <W: Write> TapeWrite for BlockedWriter<W> { impl <W: BlockWrite> TapeWrite for BlockedWriter<W> {
fn write_all(&mut self, mut data: &[u8]) -> Result<bool, std::io::Error> { fn write_all(&mut self, mut data: &[u8]) -> Result<bool, std::io::Error> {
while !data.is_empty() { while !data.is_empty() {
@ -113,7 +132,9 @@ impl <W: Write> TapeWrite for BlockedWriter<W> {
self.buffer.set_seq_nr(self.seq_nr); self.buffer.set_seq_nr(self.seq_nr);
self.seq_nr += 1; self.seq_nr += 1;
self.bytes_written += BlockHeader::SIZE; self.bytes_written += BlockHeader::SIZE;
Self::write_block(&self.buffer, &mut self.writer) let leom = Self::write_block(&self.buffer, &mut self.writer)?;
self.write_eof()?;
Ok(leom)
} }
/// Returns if the writer already detected the logical end of media /// Returns if the writer already detected the logical end of media

View File

@ -1,6 +1,9 @@
use std::io::{self, Write}; use std::io::{self, Write};
use crate::tape::file_formats::PROXMOX_TAPE_BLOCK_SIZE; use crate::tape::{
BlockWrite,
file_formats::PROXMOX_TAPE_BLOCK_SIZE,
};
/// Emulate tape write behavior on a normal Writer /// Emulate tape write behavior on a normal Writer
/// ///
@ -11,7 +14,7 @@ pub struct EmulateTapeWriter<W> {
block_nr: usize, block_nr: usize,
max_blocks: usize, max_blocks: usize,
writer: W, writer: W,
leom_sent: bool, wrote_eof: bool,
} }
impl <W: Write> EmulateTapeWriter<W> { impl <W: Write> EmulateTapeWriter<W> {
@ -27,16 +30,16 @@ impl <W: Write> EmulateTapeWriter<W> {
Self { Self {
block_nr: 0, block_nr: 0,
leom_sent: false, wrote_eof: false,
writer, writer,
max_blocks, max_blocks,
} }
} }
} }
impl <W: Write> Write for EmulateTapeWriter<W> { impl <W: Write> BlockWrite for EmulateTapeWriter<W> {
fn write(&mut self, buffer: &[u8]) -> Result<usize, io::Error> { fn write_block(&mut self, buffer: &[u8]) -> Result<bool, io::Error> {
if buffer.len() != PROXMOX_TAPE_BLOCK_SIZE { if buffer.len() != PROXMOX_TAPE_BLOCK_SIZE {
proxmox::io_bail!("EmulateTapeWriter: got write with wrong block size ({} != {}", proxmox::io_bail!("EmulateTapeWriter: got write with wrong block size ({} != {}",
@ -47,22 +50,22 @@ impl <W: Write> Write for EmulateTapeWriter<W> {
return Err(io::Error::from_raw_os_error(nix::errno::Errno::ENOSPC as i32)); return Err(io::Error::from_raw_os_error(nix::errno::Errno::ENOSPC as i32));
} }
if self.block_nr >= self.max_blocks {
if !self.leom_sent {
self.leom_sent = true;
return Err(io::Error::from_raw_os_error(nix::errno::Errno::ENOSPC as i32));
} else {
self.leom_sent = false;
}
}
self.writer.write_all(buffer)?; self.writer.write_all(buffer)?;
self.block_nr += 1; self.block_nr += 1;
Ok(buffer.len()) if self.block_nr > self.max_blocks {
Ok(true)
} else {
Ok(false)
}
} }
fn flush(&mut self) -> Result<(), io::Error> { fn write_filemark(&mut self) -> Result<(), std::io::Error> {
proxmox::io_bail!("EmulateTapeWriter does not support flush"); if self.wrote_eof {
proxmox::io_bail!("EmulateTapeWriter: detected multiple EOF writes");
}
// do nothing, just record the call
self.wrote_eof = true;
Ok(())
} }
} }

View File

@ -1,9 +1,5 @@
use std::io::Write;
use endian_trait::Endian; use endian_trait::Endian;
use proxmox::sys::error::SysError;
use crate::tape::file_formats::MediaContentHeader; use crate::tape::file_formats::MediaContentHeader;
/// Write trait for tape devices /// Write trait for tape devices
@ -53,53 +49,14 @@ pub trait TapeWrite {
} }
} }
/// Write a single block to a tape device /// Write streams of blocks
/// pub trait BlockWrite {
/// Assumes that 'writer' is a linux tape device. /// Write a data block
/// ///
/// EOM Behaviour on Linux: When the end of medium early warning is /// Returns true if the drive reached the Logical End Of Media
/// encountered, the current write is finished and the number of bytes /// (early warning)
/// is returned. The next write returns -1 and errno is set to fn write_block(&mut self, buffer: &[u8]) -> Result<bool, std::io::Error>;
/// ENOSPC. To enable writing a trailer, the next write is allowed to
/// proceed and, if successful, the number of bytes is returned. After
/// this, -1 and the number of bytes are alternately returned until
/// the physical end of medium (or some other error) is encountered.
///
/// See: https://github.com/torvalds/linux/blob/master/Documentation/scsi/st.rst
///
/// On success, this returns if we en countered a EOM condition.
pub fn tape_device_write_block<W: Write>(
writer: &mut W,
data: &[u8],
) -> Result<bool, std::io::Error> {
let mut leof = false; /// Write a filemark
fn write_filemark(&mut self) -> Result<(), std::io::Error>;
loop {
match writer.write(data) {
Ok(count) if count == data.len() => return Ok(leof),
Ok(count) if count > 0 => {
proxmox::io_bail!(
"short block write ({} < {}). Tape drive uses wrong block size.",
count, data.len());
}
Ok(_) => { // count is 0 here, assume EOT
return Err(std::io::Error::from_raw_os_error(nix::errno::Errno::ENOSPC as i32));
}
// handle interrupted system call
Err(err) if err.kind() == std::io::ErrorKind::Interrupted => {
continue;
}
// detect and handle LEOM (early warning)
Err(err) if err.is_errno(nix::errno::Errno::ENOSPC) => {
if leof {
return Err(err);
} else {
leof = true;
continue; // next write will succeed
}
}
Err(err) => return Err(err),
}
}
} }