Source file mirage_block_mem.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
(*
 * Copyright (C) 2011-2013 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.
 *)

open Mirage_block

module Int64Map = Map.Make(Int64)

type t = {
  mutable map: Cstruct.t Int64Map.t;
  info: info;
  id: string;
}

type error = Mirage_block.error
let pp_error = Mirage_block.pp_error
type write_error = Mirage_block.write_error
let pp_write_error = Mirage_block.pp_write_error
let devices = Hashtbl.create 1
let get_info { info; _ } = Lwt.return info

let connect name =
  if Hashtbl.mem devices name
  then Lwt.return (Hashtbl.find devices name)
  else
    let map = Int64Map.empty in
    let info = {
      read_write = true;
      sector_size = 512;
      size_sectors = 32768L; (* 16 MiB *)
    } in
    let device = { map; info; id = name } in
    Hashtbl.replace devices name device;
    Lwt.return device

let disconnect t =
  t.map <- Int64Map.empty;
  Lwt.return ()

let rec read x sector_start buffers = match buffers with
  | [] -> Lwt.return (Ok ())
  | b :: bs ->
    if Int64Map.mem sector_start x.map
    then Cstruct.blit (Int64Map.find sector_start x.map) 0 b 0 512
    else begin
      for i = 0 to 511 do
        Cstruct.set_uint8 b i 0
      done
    end;
    read x (Int64.succ sector_start)
      (if Cstruct.length b > 512
       then (Cstruct.shift b 512) :: bs
       else bs)

let rec write x sector_start buffers = match buffers with
  | [] -> Lwt.return (Ok ())
  | b :: bs ->
    if Cstruct.length b = 512 then begin
      x.map <- Int64Map.add sector_start b x.map;
      write x (Int64.succ sector_start) bs
    end else begin
      x.map <- Int64Map.add sector_start (Cstruct.sub b 0 512) x.map;
      write x (Int64.succ sector_start) (Cstruct.shift b 512 :: bs)
    end