@@ -3,7 +3,7 @@ extern crate log;
33
44use crossbeam_channel:: unbounded;
55#[ cfg( feature = "blk" ) ]
6- use devices:: virtio:: block:: ImageType ;
6+ use devices:: virtio:: block:: { CacheMode , ImageType , SyncMode } ;
77#[ cfg( feature = "gpu" ) ]
88use devices:: virtio:: gpu:: display:: DisplayInfo ;
99#[ cfg( feature = "net" ) ]
@@ -671,6 +671,8 @@ pub unsafe extern "C" fn krun_add_disk(
671671 disk_image_path : disk_path. to_string ( ) ,
672672 disk_image_format : ImageType :: Raw ,
673673 is_disk_read_only : read_only,
674+ cache_mode : CacheMode :: Cached ,
675+ sync_mode : SyncMode :: Full ,
674676 } ;
675677 cfg. add_block_cfg ( block_device_config) ;
676678 }
@@ -718,6 +720,78 @@ pub unsafe extern "C" fn krun_add_disk2(
718720 disk_image_path : disk_path. to_string ( ) ,
719721 disk_image_format : format,
720722 is_disk_read_only : read_only,
723+ cache_mode : CacheMode :: Cached ,
724+ sync_mode : SyncMode :: Full ,
725+ } ;
726+ cfg. add_block_cfg ( block_device_config) ;
727+ }
728+ Entry :: Vacant ( _) => return -libc:: ENOENT ,
729+ }
730+
731+ KRUN_SUCCESS
732+ }
733+
734+ #[ allow( clippy:: missing_safety_doc) ]
735+ #[ no_mangle]
736+ #[ cfg( feature = "blk" ) ]
737+ pub unsafe extern "C" fn krun_add_disk3 (
738+ ctx_id : u32 ,
739+ c_block_id : * const c_char ,
740+ c_disk_path : * const c_char ,
741+ disk_format : u32 ,
742+ read_only : bool ,
743+ cache_mode : u32 ,
744+ sync_mode : u32 ,
745+ ) -> i32 {
746+ let disk_path = match CStr :: from_ptr ( c_disk_path) . to_str ( ) {
747+ Ok ( disk) => disk,
748+ Err ( _) => return -libc:: EINVAL ,
749+ } ;
750+
751+ let block_id = match CStr :: from_ptr ( c_block_id) . to_str ( ) {
752+ Ok ( block_id) => block_id,
753+ Err ( _) => return -libc:: EINVAL ,
754+ } ;
755+
756+ let format = match disk_format {
757+ 0 => ImageType :: Raw ,
758+ 1 => ImageType :: Qcow2 ,
759+ _ => {
760+ // Do not continue if the user cannot specify a valid disk format
761+ return -libc:: EINVAL ;
762+ }
763+ } ;
764+
765+ let cache_mode = match cache_mode {
766+ 0 => CacheMode :: Uncached ,
767+ 1 => CacheMode :: Cached ,
768+ _ => {
769+ // Do not continue if the user cannot specify a valid cache mode
770+ return -libc:: EINVAL ;
771+ }
772+ } ;
773+
774+ let sync_mode = match sync_mode {
775+ 0 => SyncMode :: None ,
776+ 1 => SyncMode :: Relaxed ,
777+ 2 => SyncMode :: Full ,
778+ _ => {
779+ // Do not continue if the user cannot specify a valid sync mode
780+ return -libc:: EINVAL ;
781+ }
782+ } ;
783+
784+ match CTX_MAP . lock ( ) . unwrap ( ) . entry ( ctx_id) {
785+ Entry :: Occupied ( mut ctx_cfg) => {
786+ let cfg = ctx_cfg. get_mut ( ) ;
787+ let block_device_config = BlockDeviceConfig {
788+ block_id : block_id. to_string ( ) ,
789+ cache_type : CacheType :: auto ( disk_path) ,
790+ disk_image_path : disk_path. to_string ( ) ,
791+ disk_image_format : format,
792+ is_disk_read_only : read_only,
793+ cache_mode,
794+ sync_mode,
721795 } ;
722796 cfg. add_block_cfg ( block_device_config) ;
723797 }
@@ -745,6 +819,8 @@ pub unsafe extern "C" fn krun_set_root_disk(ctx_id: u32, c_disk_path: *const c_c
745819 disk_image_path : disk_path. to_string ( ) ,
746820 disk_image_format : ImageType :: Raw ,
747821 is_disk_read_only : false ,
822+ cache_mode : CacheMode :: Cached ,
823+ sync_mode : SyncMode :: Full ,
748824 } ;
749825 cfg. set_root_block_cfg ( block_device_config) ;
750826 }
@@ -772,6 +848,8 @@ pub unsafe extern "C" fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_c
772848 disk_image_path : disk_path. to_string ( ) ,
773849 disk_image_format : ImageType :: Raw ,
774850 is_disk_read_only : false ,
851+ cache_mode : CacheMode :: Cached ,
852+ sync_mode : SyncMode :: Full ,
775853 } ;
776854 cfg. set_data_block_cfg ( block_device_config) ;
777855 }
0 commit comments