Skip to content

Commit 90eb1cb

Browse files
authored
Merge pull request #4922 from robhoes/master
CP-40388: Call VDI.activate_readonly in SMAPIv3 when supported and appropriate
2 parents 344682a + c3f2a1b commit 90eb1cb

File tree

8 files changed

+81
-9
lines changed

8 files changed

+81
-9
lines changed

ocaml/xapi-idl/storage/storage_interface.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,12 @@ module StorageAPI (R : RPC) = struct
883883
declare "VDI.activate3" []
884884
(dbg_p @-> dp_p @-> sr_p @-> vdi_p @-> vm_p @-> returning unit_p err)
885885

886+
(** [activate_readonly task dp sr vdi] signals the desire to immediately use [vdi].
887+
This client must have called [attach] on the [vdi] first. *)
888+
let activate_readonly =
889+
declare "VDI.activate_readonly" []
890+
(dbg_p @-> dp_p @-> sr_p @-> vdi_p @-> vm_p @-> returning unit_p err)
891+
886892
(** [deactivate task dp sr vdi] signals that this client has stopped reading
887893
(and writing) [vdi]. *)
888894
let deactivate =
@@ -1309,6 +1315,9 @@ module type Server_impl = sig
13091315
val activate3 :
13101316
context -> dbg:debug_info -> dp:dp -> sr:sr -> vdi:vdi -> vm:vm -> unit
13111317

1318+
val activate_readonly :
1319+
context -> dbg:debug_info -> dp:dp -> sr:sr -> vdi:vdi -> vm:vm -> unit
1320+
13121321
val deactivate :
13131322
context -> dbg:debug_info -> dp:dp -> sr:sr -> vdi:vdi -> vm:vm -> unit
13141323

@@ -1530,6 +1539,9 @@ module Server (Impl : Server_impl) () = struct
15301539
S.VDI.activate3 (fun dbg dp sr vdi vm ->
15311540
Impl.VDI.activate3 () ~dbg ~dp ~sr ~vdi ~vm
15321541
) ;
1542+
S.VDI.activate_readonly (fun dbg dp sr vdi vm ->
1543+
Impl.VDI.activate_readonly () ~dbg ~dp ~sr ~vdi ~vm
1544+
) ;
15331545
S.VDI.deactivate (fun dbg dp sr vdi vm ->
15341546
Impl.VDI.deactivate () ~dbg ~dp ~sr ~vdi ~vm
15351547
) ;

ocaml/xapi-idl/storage/storage_skeleton.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ module VDI = struct
114114

115115
let activate3 ctx ~dbg ~dp ~sr ~vdi ~vm = u "VDI.activate3"
116116

117+
let activate_readonly ctx ~dbg ~dp ~sr ~vdi ~vm = u "VDI.activate_readonly"
118+
117119
let deactivate ctx ~dbg ~dp ~sr ~vdi ~vm = u "VDI.deactivate"
118120

119121
let detach ctx ~dbg ~dp ~sr ~vdi ~vm = u "VDI.detach"

ocaml/xapi-storage-script/main.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ let bind ~volume_script_dir =
13111311
|> wrap
13121312
in
13131313
S.VDI.attach3 vdi_attach3_impl ;
1314-
let vdi_activate3_impl dbg _dp sr vdi' vm' =
1314+
let vdi_activate_common dbg sr vdi' vm' readonly =
13151315
(let vdi = Storage_interface.Vdi.string_of vdi' in
13161316
let domain = Storage_interface.Vm.string_of vm' in
13171317
Attached_SRs.find sr >>>= fun sr ->
@@ -1329,11 +1329,23 @@ let bind ~volume_script_dir =
13291329
)
13301330
>>>= fun response ->
13311331
choose_datapath domain response >>>= fun (rpc, _datapath, uri, domain) ->
1332-
return_data_rpc (fun () -> Datapath_client.activate rpc dbg uri domain)
1332+
return_data_rpc (fun () ->
1333+
if readonly then
1334+
Datapath_client.activate_readonly rpc dbg uri domain
1335+
else
1336+
Datapath_client.activate rpc dbg uri domain
1337+
)
13331338
)
13341339
|> wrap
13351340
in
1341+
let vdi_activate3_impl dbg _dp sr vdi' vm' =
1342+
vdi_activate_common dbg sr vdi' vm' false
1343+
in
13361344
S.VDI.activate3 vdi_activate3_impl ;
1345+
let vdi_activate_readonly_impl dbg _dp sr vdi' vm' =
1346+
vdi_activate_common dbg sr vdi' vm' true
1347+
in
1348+
S.VDI.activate_readonly vdi_activate_readonly_impl ;
13371349
let vdi_deactivate_impl dbg _dp sr vdi' vm' =
13381350
(let vdi = Storage_interface.Vdi.string_of vdi' in
13391351
let domain = Storage_interface.Vm.string_of vm' in

ocaml/xapi-storage/generator/lib/data.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ module Datapath (R : RPC) = struct
124124
; "activated readonly multiple times, including on multiple independent "
125125
; "hosts. It is not permitted for a volume to be activated both readonly "
126126
; "and read-write concurrently. Implementations shall declare the "
127-
; "VDI_ATTACH_READONLY feature for this method to be supported. Once a "
127+
; "VDI_ACTIVATE_READONLY feature for this method to be supported. Once a "
128128
; "volume is activated readonly it is required that all readonly "
129129
; "activations are deactivated before any read-write activation is "
130130
; "attempted. This function is idempotent and in all other respects "

ocaml/xapi/smint.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type capability =
4444
| Vdi_snapshot
4545
| Vdi_resize
4646
| Vdi_activate
47+
| Vdi_activate_readonly
4748
| Vdi_deactivate
4849
| Vdi_update
4950
| Vdi_introduce
@@ -82,6 +83,7 @@ let string_to_capability_table =
8283
; ("VDI_CLONE", Vdi_clone)
8384
; ("VDI_SNAPSHOT", Vdi_snapshot)
8485
; ("VDI_ACTIVATE", Vdi_activate)
86+
; ("VDI_ACTIVATE_READONLY", Vdi_activate_readonly)
8587
; ("VDI_DEACTIVATE", Vdi_deactivate)
8688
; ("VDI_UPDATE", Vdi_update)
8789
; ("VDI_INTRODUCE", Vdi_introduce)

ocaml/xapi/storage_mux.ml

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type plugin = {
3434
processor: processor
3535
; backend_domain: string
3636
; query_result: query_result
37+
; features: Smint.feature list
3738
}
3839

3940
let plugins : (sr, plugin) Hashtbl.t = Hashtbl.create 10
@@ -48,8 +49,16 @@ let debug_printer rpc call =
4849

4950
let register sr rpc d info =
5051
with_lock m (fun () ->
52+
let features =
53+
Smint.parse_capability_int64_features info.Storage_interface.features
54+
in
5155
Hashtbl.replace plugins sr
52-
{processor= debug_printer rpc; backend_domain= d; query_result= info} ;
56+
{
57+
processor= debug_printer rpc
58+
; backend_domain= d
59+
; query_result= info
60+
; features
61+
} ;
5362
debug "register SR %s (currently-registered = [ %s ])" (s_of_sr sr)
5463
(String.concat ", "
5564
(Hashtbl.fold (fun sr _ acc -> s_of_sr sr :: acc) plugins [])
@@ -69,6 +78,13 @@ let query_result_of_sr sr =
6978
try with_lock m (fun () -> Some (Hashtbl.find plugins sr).query_result)
7079
with _ -> None
7180

81+
let sr_has_capability sr capability =
82+
try
83+
with_lock m (fun () ->
84+
Smint.has_capability capability (Hashtbl.find plugins sr).features
85+
)
86+
with _ -> false
87+
7288
(* This is the policy: *)
7389
let of_sr sr =
7490
with_lock m (fun () ->
@@ -156,7 +172,8 @@ module Mux = struct
156172
end
157173

158174
module DP_info = struct
159-
type t = {sr: Sr.t; vdi: Vdi.t; vm: Vm.t} [@@deriving rpcty]
175+
type t = {sr: Sr.t; vdi: Vdi.t; vm: Vm.t; read_write: bool [@default true]}
176+
[@@deriving rpcty]
160177

161178
let storage_dp_path = "/var/run/nonpersistent/xapi/storage-dps"
162179

@@ -496,7 +513,7 @@ module Mux = struct
496513
let rpc = of_sr sr
497514
end)) in
498515
let vm = Vm.of_string "0" in
499-
DP_info.write dp DP_info.{sr; vdi; vm} ;
516+
DP_info.write dp DP_info.{sr; vdi; vm; read_write} ;
500517
let backend = C.VDI.attach3 dbg dp sr vdi vm read_write in
501518
(* VDI.attach2 should be used instead, VDI.attach is only kept for
502519
backwards-compatibility, because older xapis call Remote.VDI.attach during SXM.
@@ -543,7 +560,7 @@ module Mux = struct
543560
let rpc = of_sr sr
544561
end)) in
545562
let vm = Vm.of_string "0" in
546-
DP_info.write dp DP_info.{sr; vdi; vm} ;
563+
DP_info.write dp DP_info.{sr; vdi; vm; read_write} ;
547564
C.VDI.attach3 dbg dp sr vdi vm read_write
548565

549566
let attach3 () ~dbg ~dp ~sr ~vdi ~vm ~read_write =
@@ -552,7 +569,7 @@ module Mux = struct
552569
let module C = StorageAPI (Idl.Exn.GenClient (struct
553570
let rpc = of_sr sr
554571
end)) in
555-
DP_info.write dp DP_info.{sr; vdi; vm} ;
572+
DP_info.write dp DP_info.{sr; vdi; vm; read_write} ;
556573
C.VDI.attach3 dbg dp sr vdi vm read_write
557574

558575
let activate () ~dbg ~dp ~sr ~vdi =
@@ -569,7 +586,30 @@ module Mux = struct
569586
let module C = StorageAPI (Idl.Exn.GenClient (struct
570587
let rpc = of_sr sr
571588
end)) in
572-
C.VDI.activate3 dbg dp sr vdi vm
589+
let read_write =
590+
let open DP_info in
591+
match read dp with
592+
| Some x ->
593+
x.read_write
594+
| None ->
595+
failwith "DP not found"
596+
in
597+
if (not read_write) && sr_has_capability sr Smint.Vdi_activate_readonly
598+
then (
599+
info "The VDI was attached read-only: calling activate_readonly" ;
600+
C.VDI.activate_readonly dbg dp sr vdi vm
601+
) else (
602+
info "The VDI was attached read/write: calling activate3" ;
603+
C.VDI.activate3 dbg dp sr vdi vm
604+
)
605+
606+
let activate_readonly () ~dbg ~dp ~sr ~vdi ~vm =
607+
info "VDI.activate_readonly dbg:%s dp:%s sr:%s vdi:%s vm:%s" dbg dp
608+
(s_of_sr sr) (s_of_vdi vdi) (s_of_vm vm) ;
609+
let module C = StorageAPI (Idl.Exn.GenClient (struct
610+
let rpc = of_sr sr
611+
end)) in
612+
C.VDI.activate_readonly dbg dp sr vdi vm
573613

574614
let deactivate () ~dbg ~dp ~sr ~vdi ~vm =
575615
info "VDI.deactivate dbg:%s dp:%s sr:%s vdi:%s vm:%s" dbg dp (s_of_sr sr)

ocaml/xapi/storage_smapiv1.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,8 @@ module SMAPIv1 : Server_impl = struct
587587
let activate3 context ~dbg ~dp ~sr ~vdi ~vm:_ =
588588
activate context ~dbg ~dp ~sr ~vdi
589589

590+
let activate_readonly = activate3
591+
590592
let deactivate _context ~dbg ~dp ~sr ~vdi ~vm:_ =
591593
try
592594
for_vdi ~dbg ~sr ~vdi "VDI.deactivate"

ocaml/xapi/storage_smapiv1_wrapper.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ functor
674674
)
675675
)
676676

677+
let activate_readonly = activate3
678+
677679
let activate context ~dbg ~dp ~sr ~vdi =
678680
info "VDI.activate dbg:%s dp:%s sr:%s vdi:%s " dbg dp (s_of_sr sr)
679681
(s_of_vdi vdi) ;

0 commit comments

Comments
 (0)