@@ -391,41 +391,59 @@ let find_backend_device path =
391391 raise Not_found
392392 with _ -> None
393393
394- (* * [backing_info_of_device] returns Some (driver_type, backing_file) for the
395- leaf backing a particular device [path]. *)
394+ type backing_file_error =
395+ | Driver_mismatch of {expected : string ; actual : string option }
396+ | Driver_unknown of {path : string }
397+ | Not_a_device
398+
399+ let backing_file_error_to_string = function
400+ | Not_a_device ->
401+ " Not a device"
402+ | Driver_mismatch {expected; actual = None } ->
403+ Printf. sprintf " Driver mismatch {expected=%s; actual=None}" expected
404+ | Driver_mismatch {expected; actual = Some actual } ->
405+ Printf. sprintf " Driver mismatch {expected=%s; actual=%s}" expected actual
406+ | Driver_unknown {path} ->
407+ Printf. sprintf " Driver unknown {path=%s}" path
408+
409+ (* * [backing_info_of_device] returns [Ok (Some (driver_type, backing_file))]
410+ for the leaf backing a particular device [path]. *)
396411let backing_info_of_device path =
397412 let tapdisk_of_path path =
398- try
399- let ( let * ) = Option. bind in
400- let * _, _, backing_info = Tapctl. of_device ( Tapctl. create () ) path in
401- backing_info
402- with
403- | Tapctl. Not_a_device ->
413+ match Tapctl. of_device ( Tapctl. create () ) path with
414+ | Some ( _ , _ , backing_info ) ->
415+ Ok backing_info
416+ | None ->
417+ Ok None
418+ | exception Tapctl. Not_a_device ->
404419 debug " %s is not a device" path ;
405- None
406- | Tapctl. Not_blktap -> (
420+ Error Not_a_device
421+ | exception Tapctl. Not_blktap -> (
407422 debug " Device %s is not controlled by blktap" path ;
408423 (* Check if it is a [driver] behind a NBD device *)
409424 get_nbd_device path |> image_behind_nbd_device |> function
410425 | Some (typ , backing_file ) as backing_info ->
411426 debug " %s is a %s behind NBD device %s" backing_file typ path ;
412- backing_info
427+ Ok backing_info
413428 | _ ->
414- None
429+ Ok None
415430 )
416431 in
417432 find_backend_device path |> Option. value ~default: path |> tapdisk_of_path
418433
419- (* * [backing_file_of_device_with_driver path driver] returns Some backing_file
434+ (* * [backing_file_of_device_with_driver path driver] returns [Ok backing_file]
420435 where [backing_file] is the leaf backing a particular device [path]
421- (with a driver of type [driver]) or None .
436+ (with a driver of type [driver]) or [Error backing_file_error] .
422437 [path] may either be a blktap2 device *or* a blkfront device backed by a
423438 blktap2 device. If the latter then the script must be
424439 run in the same domain as blkback. *)
425440let backing_file_of_device_with_driver path ~driver =
426441 match backing_info_of_device path with
427- | Some (typ , backing_file ) when typ = driver ->
428- Some backing_file
429- | _ ->
442+ | Ok ( Some (typ , backing_file ) ) when typ = driver ->
443+ Ok backing_file
444+ | Ok info ->
430445 debug " Device %s has an unknown driver" path ;
431- None
446+ let typ = Option. map fst info in
447+ Error (Driver_mismatch {expected= driver; actual= typ})
448+ | Error _ as err ->
449+ err
0 commit comments