@@ -563,22 +563,32 @@ impl VirtioBlock {
563563 }
564564 }
565565
566- fn drain_and_flush ( & mut self , discard : bool ) {
567- if let Err ( err) = self . disk . file_engine . drain_and_flush ( discard) {
568- error ! ( "Failed to drain ops and flush block data: {:?}" , err) ;
569- }
566+ fn drain_and_flush ( & mut self , discard : bool ) -> Result < ( ) , VirtioBlockError > {
567+ self . disk
568+ . file_engine
569+ . drain_and_flush ( discard)
570+ . map_err ( VirtioBlockError :: FileEngine )
570571 }
571572
572573 /// Prepare device for being snapshotted.
573- pub fn prepare_save ( & mut self ) {
574+ pub fn prepare_save ( & mut self ) -> Result < ( ) , VirtioBlockError > {
574575 if !self . is_activated ( ) {
575- return ;
576+ return Ok ( ( ) ) ;
576577 }
577578
578- self . drain_and_flush ( false ) ;
579- if let FileEngine :: Async ( ref _engine) = self . disk . file_engine {
579+ self . drain_and_flush ( false ) ?;
580+ let is_async = matches ! ( self . disk. file_engine, FileEngine :: Async ( _) ) ;
581+ if is_async {
580582 self . process_async_completion_queue ( ) ;
583+ if let FileEngine :: Async ( ref engine) = self . disk . file_engine {
584+ let pending = engine. pending_ops ( ) ;
585+ if pending != 0 {
586+ return Err ( VirtioBlockError :: PendingAsyncOperations ( pending) ) ;
587+ }
588+ }
581589 }
590+
591+ Ok ( ( ) )
582592 }
583593}
584594
@@ -682,7 +692,9 @@ impl Drop for VirtioBlock {
682692 }
683693 }
684694 CacheType :: Writeback => {
685- self . drain_and_flush ( true ) ;
695+ if let Err ( err) = self . drain_and_flush ( true ) {
696+ error ! ( "Failed to drain ops and flush block data: {:?}" , err) ;
697+ }
686698 }
687699 } ;
688700 }
@@ -1673,7 +1685,7 @@ mod tests {
16731685 // Add a batch of flush requests.
16741686 add_flush_requests_batch ( & mut block, & vq, 5 ) ;
16751687 simulate_queue_event ( & mut block, None ) ;
1676- block. prepare_save ( ) ;
1688+ block. prepare_save ( ) . unwrap ( ) ;
16771689
16781690 // Check that all the pending flush requests were processed during `prepare_save()`.
16791691 check_flush_requests_batch ( 5 , & vq) ;
0 commit comments