Source file device_number.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(*
 * Copyright (c) 2012 Citrix Systems Inc
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

type bus_type =
	| Xen 
	| Scsi
	| Ide

type spec = bus_type * int * int

type t = spec

let to_debug_string = function
	| (Xen, disk, partition)  -> Printf.sprintf "Xen(%d, %d)"  disk partition
	| (Scsi, disk, partition) -> Printf.sprintf "Scsi(%d, %d)" disk partition
	| (Ide, disk, partition)  -> Printf.sprintf "Ide(%d, %d)"  disk partition

(* ocamlp4-friendly operators *)
let (<|) = (lsl)
let (>|) = (lsr)

let int_of_string x = try int_of_string x with _ -> failwith (Printf.sprintf "int_of_string [%s]" x)

(* If this is true then we will use the deprecated (linux-specific) IDE encodings for disks > 3 *)
let use_deprecated_ide_encoding = true

let make (x: spec) : t = 
	let max_xen = ((1 <| 20) - 1), 15 in
	let max_scsi = 15, 15 in
	let max_ide = if use_deprecated_ide_encoding then 19, 63 else 3, 63 in
	let assert_in_range description (disk_limit, partition_limit) (disk, partition) = 
		if disk < 0 || (disk > disk_limit) 
		then failwith (Printf.sprintf "%s disk number out of range 0 <= %d <= %d" description disk disk_limit);
		if partition < 0 || partition > partition_limit 
		then failwith (Printf.sprintf "%s partition number out of range 0 <= %d <= %d" description partition partition_limit) in
	begin match x with
		| Xen, disk, partition -> assert_in_range "xen" max_xen (disk, partition)
		| Scsi, disk, partition -> assert_in_range "scsi" max_scsi (disk, partition)
		| Ide, disk, partition -> assert_in_range "ide" max_ide (disk, partition)
	end;
	x

let spec (x: t) : spec = x

let (||) = (lor)

let standard_ide_table = [ 3; 22 ]
let deprecated_ide_table = standard_ide_table @ [ 33; 34; 56; 57; 88; 89; 90; 91 ]

let to_xenstore_int = function
	| Xen, disk, partition when disk < 16 -> (202 <| 8) || (disk <| 4)       || partition
	| Xen, disk, partition                -> (1 <| 28)  || (disk <| 8)       || partition
	| Scsi, disk, partition               -> (8 <| 8)   || (disk <| 4)       || partition
	| Ide, disk, partition                ->
		let m = List.nth deprecated_ide_table (disk / 2) in
		let n = disk - (disk / 2) * 2 in (* NB integers behave differently to reals *)
		(m <| 8) || (n <| 6) || partition

let of_xenstore_int x =
	let (&&) = (land) in

	if (x && (1 <| 28)) <> 0
	then Xen, (x >| 8) && ((1 <| 20) - 1), x && ((1 <| 8) - 1)
	else match x >| 8 with
		| 202 -> Xen, (x >| 4) && ((1 <| 4) - 1), x && ((1 <| 4) - 1)
		| 8   -> Scsi, (x >| 4) && ((1 <| 4) - 1), x && ((1 <| 4) - 1)
		| n   ->
			let idx = snd(List.fold_left (fun (i, res) e -> i+1, if e = n then i else res) (0, -1) deprecated_ide_table) in
			if idx < 0
			then failwith (Printf.sprintf "Unknown device number: %d" x);
			Ide, ((x >| 6) && ((1 <| 2) - 1)) + idx * 2,  x && ((1 <| 6) - 1)

type xenstore_key = int

let to_xenstore_key x = to_xenstore_int x
let of_xenstore_key x = of_xenstore_int x

(* NB the device encoding is base 26 starting from 1 rather than 0 eg
   0  -> a
   25 -> z
   26 -> aa
*)

(** Return an integer encoded as a linux device suffix *)
let rec string_of_int26 x = 
	let high, low = x / 26 - 1, x mod 26 + 1 in
	let high' = if high = -1 then "" else string_of_int26 high in
	let low' = String.make 1 (char_of_int (low + (int_of_char 'a') - 1)) in
	high' ^ low'

module String = struct
	include String
	let fold_right f string accu =
		let accu = ref accu in
		for i = length string - 1 downto 0 do
			accu := f string.[i] !accu
		done;
		!accu

	let explode string =
		fold_right (fun h t -> h :: t) string []

	let implode list =
		concat "" (List.map (String.make 1) list)
end

(** Convert a linux device string back into an integer *)
let int26_of_string x = 
	let ints = List.map (fun c -> int_of_char c - (int_of_char 'a') + 1) (String.explode x) in
	List.fold_left (fun acc x -> acc * 26 + x) 0 ints - 1

let to_linux_device = 
	let p x = if x = 0 then "" else string_of_int x in 
	function
		| Xen,  disk, part -> Printf.sprintf "xvd%s%s" (string_of_int26 disk) (p part)
		| Scsi, disk, part -> Printf.sprintf "sd%s%s"  (string_of_int26 disk) (p part)
		| Ide,  disk, part -> Printf.sprintf "hd%s%s"  (string_of_int26 disk) (p part)

let of_linux_device x =
	let letter c = 'a' <= c && (c <= 'z') in
	let digit c = '0' <= c && (c <= '9') in
	let take f x = 
		let rec inner f acc = function
			| x :: xs -> 
				if f x then inner f (x :: acc) xs else List.rev acc, x :: xs
			| [] -> List.rev acc, [] in
		inner f [] x in
	(* Parse a string "abc123" into x, y where x is "abc" interpreted as base-26
	   and y is 123 *)
	let parse_b26_int x = 
		let d, p = take letter x in
		let d' = int26_of_string (String.implode d) in
		let p' = if p = [] then 0 else int_of_string (String.implode p) in
		d', p' in
	(* Parse a string "123p456" into x, y where x = 123 and y = 456 *)
	let parse_int_p_int x = 
		let d, rest = take digit x in
		match rest with
			| 'p' :: rest ->
				let p, _ = take digit rest in
				int_of_string (String.implode d), int_of_string (String.implode p)
			| [] ->
				int_of_string (String.implode d), 0
			| _ -> 
				failwith 
					(Printf.sprintf "expected digit+ p digit+ got: %s" (String.implode x)) in
	match String.explode x with
		| 'x' :: 'v' :: 'd' :: rest ->
			let disk, partition = parse_b26_int rest in
			Xen, disk, partition
		| 's' :: 'd' :: rest ->
			let disk, partition = parse_b26_int rest in
			Scsi, disk, partition
		| 'h' :: 'd' :: rest ->
			let disk, partition = parse_b26_int rest in
			Ide, disk, partition
		| 'd' :: rest ->
			let disk, partition = parse_int_p_int rest in
			Xen, disk, partition
		| _ -> failwith (Printf.sprintf "Failed to parse device name: %s" x)
	
type disk_number = int

let to_disk_number = function
	| Xen, disk, _ -> disk
	| Scsi, disk, _ -> disk
	| Ide, disk, _ -> disk

let of_disk_number hvm n = 
	if hvm && (n < 16)
	then Ide, n, 0
	else Xen, n, 0

let of_string hvm name = 
	try
		of_disk_number hvm (int_of_string name)
	with _ ->
		of_linux_device name