Skip to content

Commit db91ed7

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 <[email protected]>
1 parent cec62e8 commit db91ed7

File tree

5 files changed

+12
-13
lines changed

5 files changed

+12
-13
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/xapi_vdi_helpers.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,11 @@ let read_raw ~__context ~vdi =
304304
type nbd_connect_info = {path: string; exportname: string} [@@deriving rpc]
305305

306306
let get_device_numbers path =
307-
let rdev = (Unix.LargeFile.stat path).Unix.LargeFile.st_rdev in
308-
let major = rdev / 256 and minor = rdev mod 256 in
309-
(major, minor)
307+
Unix.LargeFile.((stat path).st_rdev) |> Unixext.Stat.decode_st_dev
310308

311309
let is_nbd_device path =
312310
let nbd_device_num = 43 in
313-
let major, _ = get_device_numbers path in
311+
let Unixext.Stat.{major; _} = get_device_numbers path in
314312
major = nbd_device_num
315313

316314
let get_nbd_device path =
@@ -365,8 +363,7 @@ let find_backend_device path =
365363
let open Ezxenstore_core.Xenstore in
366364
(* If we're looking at a xen frontend device, see if the backend
367365
is in the same domain. If so check if it looks like a .vhd *)
368-
let rdev = (Unix.stat path).Unix.st_rdev in
369-
let major = rdev / 256 and minor = rdev mod 256 in
366+
let Unixext.Stat.{major; minor} = get_device_numbers path in
370367
let link =
371368
Unix.readlink (Printf.sprintf "/sys/dev/block/%d:%d/device" major minor)
372369
in

0 commit comments

Comments
 (0)