Skip to content

Commit f5c5b4f

Browse files
committed
ocaml: Unify handling of major and minor device IDs
There were 5 ad-hoc implementations, they were all getting wrong results for the minor because only the lower 8 bits were accounted for. Use the unixext one which handles up to 32-bit-wide minors correctly. Signed-off-by: Pau Ruiz Safont <pau.safont@vates.tech>
1 parent 711f37d commit f5c5b4f

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

ocaml/tapctl/tapctl.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,9 @@ exception Not_a_device
529529

530530
let of_device ctx path =
531531
let stat = Unix.stat path in
532+
let module Stat = Unixext.Stat in
532533
if stat.Unix.st_kind <> Unix.S_BLK then raise Not_a_device ;
533-
let major = stat.Unix.st_rdev / 256 in
534-
let minor = stat.Unix.st_rdev mod 256 in
534+
let Stat.{major; minor} = Stat.decode_st_dev stat.Unix.st_rdev in
535535
if driver_of_major major <> "tapdev" then raise Not_blktap ;
536536
match List.filter (fun (tapdev, _, _) -> tapdev.minor = minor) (list ctx) with
537537
| [t] ->

ocaml/vhd-tool/cli/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
vhd-format-lwt
2020
xapi-idl
2121
xapi-log
22+
xapi-stdext-unix
2223
xenstore_transport.unix
2324
ezxenstore
2425
)

ocaml/vhd-tool/cli/sparse_dd.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ let after f g =
213213
let find_backend_device path =
214214
try
215215
let open Ezxenstore_core.Xenstore in
216+
let module Stat = Xapi_stdext_unix.Unixext.Stat in
216217
(* If we're looking at a xen frontend device, see if the backend
217218
is in the same domain. If so check if it looks like a .vhd *)
218219
let rdev = (Unix.LargeFile.stat path).Unix.LargeFile.st_rdev in
219-
let major = rdev / 256 and minor = rdev mod 256 in
220+
let Stat.{major; minor} = Stat.decode_st_dev rdev in
220221
let link =
221222
Unix.readlink (Printf.sprintf "/sys/dev/block/%d:%d/device" major minor)
222223
in

ocaml/vhd-tool/src/image.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
module Stat = Xapi_stdext_unix.Unixext.Stat
2+
13
let get_device_numbers path =
2-
let rdev = (Unix.LargeFile.stat path).Unix.LargeFile.st_rdev in
3-
let major = rdev / 256 and minor = rdev mod 256 in
4-
(major, minor)
4+
Unix.LargeFile.((stat path).st_rdev) |> Stat.decode_st_dev
55

66
let is_nbd_device path =
77
let nbd_device_num = 43 in
8-
let major, _ = get_device_numbers path in
8+
let Stat.{major; _} = get_device_numbers path in
99
major = nbd_device_num
1010

1111
type t = [`Vhd of string | `Raw of string | `Nbd of string * string]

ocaml/xapi/stream_vdi.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ let write_block ~__context filename buffer ofd len =
132132
raise e
133133

134134
let get_device_numbers path =
135-
let rdev = (Unix.LargeFile.stat path).Unix.LargeFile.st_rdev in
136-
let major = rdev / 256 and minor = rdev mod 256 in
137-
(major, minor)
135+
Unix.LargeFile.((stat path).st_rdev) |> Unixext.Stat.decode_st_dev
138136

139137
let is_nbd_device path =
140138
let nbd_device_num = 43 in
141-
let major, _ = get_device_numbers path in
139+
let Unixext.Stat.{major; _} = get_device_numbers path in
142140
major = nbd_device_num
143141

144142
type nbd_connect_info = {path: string; exportname: string} [@@deriving rpc]

ocaml/xapi/vhd_tool_wrapper.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ let receive progress_cb format protocol (s : Unix.file_descr)
118118
let find_backend_device path =
119119
try
120120
let open Ezxenstore_core.Xenstore in
121+
let module Stat = Xapi_stdext_unix.Unixext.Stat in
121122
(* If we're looking at a xen frontend device, see if the backend
122123
is in the same domain. If so check if it looks like a .vhd *)
123124
let rdev = (Unix.stat path).Unix.st_rdev in
124-
let major = rdev / 256 and minor = rdev mod 256 in
125+
let Stat.{major; minor} = Stat.decode_st_dev rdev in
125126
let link =
126127
Unix.readlink (Printf.sprintf "/sys/dev/block/%d:%d/device" major minor)
127128
in

0 commit comments

Comments
 (0)