tape: update changer status inside ScsiMediaChange implementation

This commit is contained in:
Dietmar Maurer 2021-02-20 09:56:27 +01:00
parent cbd9899389
commit 3f16f1b006
3 changed files with 39 additions and 31 deletions

View File

@ -163,7 +163,6 @@ pub async fn transfer(
tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
changer_config.transfer(from, to)?; changer_config.transfer(from, to)?;
let _ = changer_config.status(false); // update status cache
Ok(()) Ok(())
}).await? }).await?
} }

View File

@ -154,11 +154,7 @@ pub fn load_media(
move |worker, config| { move |worker, config| {
task_log!(worker, "loading media '{}' into drive '{}'", label_text, drive); task_log!(worker, "loading media '{}' into drive '{}'", label_text, drive);
let (mut changer, _) = required_media_changer(&config, &drive)?; let (mut changer, _) = required_media_changer(&config, &drive)?;
changer.load_media(&label_text)?; changer.load_media(&label_text)
let _ = changer.status(); // update status cache
Ok(())
}, },
)?; )?;
@ -262,11 +258,7 @@ pub fn unload(
task_log!(worker, "unloading media from drive '{}'", drive); task_log!(worker, "unloading media from drive '{}'", drive);
let (mut changer, _) = required_media_changer(&config, &drive)?; let (mut changer, _) = required_media_changer(&config, &drive)?;
changer.unload_media(target_slot)?; changer.unload_media(target_slot)
let _ = changer.status(); // update status cache
Ok(())
}, },
)?; )?;
@ -424,7 +416,6 @@ pub fn eject_media(
move |_worker, config| { move |_worker, config| {
if let Some((mut changer, _)) = media_changer(&config, &drive)? { if let Some((mut changer, _)) = media_changer(&config, &drive)? {
changer.unload_media(None)?; changer.unload_media(None)?;
let _ = changer.status(); // update status cache
} else { } else {
let mut drive = open_drive(&config, &drive)?; let mut drive = open_drive(&config, &drive)?;
drive.eject_media()?; drive.eject_media()?;
@ -867,8 +858,6 @@ pub fn update_inventory(
continue; continue;
} }
let _ = changer.status(); // update status cache
let mut drive = open_drive(&config, &drive)?; let mut drive = open_drive(&config, &drive)?;
match drive.read_label() { match drive.read_label() {
Err(err) => { Err(err) => {
@ -887,7 +876,6 @@ pub fn update_inventory(
} }
} }
changer.unload_media(None)?; changer.unload_media(None)?;
let _ = changer.status(); // update status cache
} }
Ok(()) Ok(())
}, },

View File

@ -168,11 +168,11 @@ pub trait ScsiMediaChange {
fn status(&mut self, use_cache: bool) -> Result<MtxStatus, Error>; fn status(&mut self, use_cache: bool) -> Result<MtxStatus, Error>;
fn load_slot(&mut self, from_slot: u64, drivenum: u64) -> Result<(), Error>; fn load_slot(&mut self, from_slot: u64, drivenum: u64) -> Result<MtxStatus, Error>;
fn unload(&mut self, to_slot: u64, drivenum: u64) -> Result<(), Error>; fn unload(&mut self, to_slot: u64, drivenum: u64) -> Result<MtxStatus, Error>;
fn transfer(&mut self, from_slot: u64, to_slot: u64) -> Result<(), Error>; fn transfer(&mut self, from_slot: u64, to_slot: u64) -> Result<MtxStatus, Error>;
} }
/// Interface to the media changer device for a single drive /// Interface to the media changer device for a single drive
@ -431,31 +431,49 @@ impl ScsiMediaChange for ScsiTapeChanger {
status status
} }
fn load_slot(&mut self, from_slot: u64, drivenum: u64) -> Result<(), Error> { fn load_slot(&mut self, from_slot: u64, drivenum: u64) -> Result<MtxStatus, Error> {
if USE_MTX { let result = if USE_MTX {
mtx::mtx_load(&self.path, from_slot, drivenum) mtx::mtx_load(&self.path, from_slot, drivenum)
} else { } else {
let mut file = sg_pt_changer::open(&self.path)?; let mut file = sg_pt_changer::open(&self.path)?;
sg_pt_changer::load_slot(&mut file, from_slot, drivenum) sg_pt_changer::load_slot(&mut file, from_slot, drivenum)
} };
let status = self.status(false)?; // always update status
result?; // check load result
Ok(status)
} }
fn unload(&mut self, to_slot: u64, drivenum: u64) -> Result<(), Error> { fn unload(&mut self, to_slot: u64, drivenum: u64) -> Result<MtxStatus, Error> {
if USE_MTX { let result = if USE_MTX {
mtx::mtx_unload(&self.path, to_slot, drivenum) mtx::mtx_unload(&self.path, to_slot, drivenum)
} else { } else {
let mut file = sg_pt_changer::open(&self.path)?; let mut file = sg_pt_changer::open(&self.path)?;
sg_pt_changer::unload(&mut file, to_slot, drivenum) sg_pt_changer::unload(&mut file, to_slot, drivenum)
} };
let status = self.status(false)?; // always update status
result?; // check unload result
Ok(status)
} }
fn transfer(&mut self, from_slot: u64, to_slot: u64) -> Result<(), Error> { fn transfer(&mut self, from_slot: u64, to_slot: u64) -> Result<MtxStatus, Error> {
if USE_MTX { let result = if USE_MTX {
mtx::mtx_transfer(&self.path, from_slot, to_slot) mtx::mtx_transfer(&self.path, from_slot, to_slot)
} else { } else {
let mut file = sg_pt_changer::open(&self.path)?; let mut file = sg_pt_changer::open(&self.path)?;
sg_pt_changer::transfer_medium(&mut file, from_slot, to_slot) sg_pt_changer::transfer_medium(&mut file, from_slot, to_slot)
} };
let status = self.status(false)?; // always update status
result?; // check unload result
Ok(status)
} }
} }
@ -539,19 +557,22 @@ impl MediaChange for MtxMediaChanger {
} }
fn transfer_media(&mut self, from: u64, to: u64) -> Result<(), Error> { fn transfer_media(&mut self, from: u64, to: u64) -> Result<(), Error> {
self.config.transfer(from, to) self.config.transfer(from, to)?;
Ok(())
} }
fn load_media_from_slot(&mut self, slot: u64) -> Result<(), Error> { fn load_media_from_slot(&mut self, slot: u64) -> Result<(), Error> {
self.config.load_slot(slot, self.drive_number) self.config.load_slot(slot, self.drive_number)?;
Ok(())
} }
fn unload_media(&mut self, target_slot: Option<u64>) -> Result<(), Error> { fn unload_media(&mut self, target_slot: Option<u64>) -> Result<(), Error> {
if let Some(target_slot) = target_slot { if let Some(target_slot) = target_slot {
self.config.unload(target_slot, self.drive_number) self.config.unload(target_slot, self.drive_number)?;
} else { } else {
let status = self.status()?; let status = self.status()?;
self.unload_to_free_slot(status) self.unload_to_free_slot(status)?;
} }
Ok(())
} }
} }