Block device abstraction with Eio
README.md

block #

Block devices and the storage platform for OCaml 5, with Bytesrw integration.

Pagers, logs and file system images all read and write storage in fixed-size units. The same code should run on a file in production and on memory in a test, and a unikernel gives it a device instead of a file system. It also needs to know what a write survives when the power goes, since that decides which journals and barriers a layer above can skip. block gives that code its I/O through two interfaces, and does no I/O of its own: the block.eio sublibrary implements them over Eio.

Installation #

$ opam install block

If opam cannot find the package, add the overlay repository first:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install block

Place in the stack #

block is the bottom of a pure-OCaml storage stack. The SQLite engine btree and sqlite take their files from Block.Platform. The disk images of nox-fat and nox-tar, and the nox-notafs file system, sit on a Block.t device.

Which API to use #

  • Block.t is a device addressed by sector number, modelled on MirageOS's mirage-block without Lwt. Use it for a disk image or a partition, where every read and write is a whole sector.
  • Block.Platform is what a storage engine takes from its host. It has one open file addressed by byte offset (FILE), a namespace of files (NAMES), a clock and a source of randomness, and a caps record of what the device promises. Use it for a pager or a log that works on files, and write the layer as a functor over the atoms it needs, so a pager that takes only FILE can never rename a file.

Each interface has an in-memory implementation and an Eio one. A device comes from Block.of_memory in memory or Block_eio.of_file on a file, while Block.Sim and Block_eio.Platform are the two platforms. Block.Sim is deterministic, with a seeded source of randomness and a clock the test sets, so a crash test replays the same bytes on every run.

A device #

# let blk = Block.of_memory ~sector_size:512 ~sectors:16L;;
val blk : Block.t = <abstr>
# Block.write blk 3L (String.make 512 'A');;
- : (unit, Block.write_error) result = Ok ()
# Block.read blk 3L |> Result.map (fun s -> String.sub s 0 4);;
- : (string, Block.error) result = Ok "AAAA"
# (Block.info (Block.with_crc32c blk)).sector_size;;
- : int = 508

Block.with_crc32c keeps the last 4 bytes of each sector for the CRC32C of the rest, little-endian, and a read whose checksum is wrong returns `Read_error. Block.sub is a view of a range of sectors, such as a partition, and Block.read_only a view that refuses writes. Block.to_reader and Block.to_writer stream a device as Bytesrw bytes.

A platform #

A layer written over Block.Platform.FILE runs unchanged over Block.Sim and over Block_eio.Platform:

module Magic (F : Block.Platform.FILE) = struct
  let read f =
    let buf = Bytes.create 4 in
    F.pread f ~off:0 buf ~pos:0 ~len:4;
    Bytes.to_string buf
end

module Sim_magic = Magic (Block.Sim.File)
# let file = Block.Sim.File.of_string "SQLite format 3\000";;
val file : Block.Sim.Names.file = <abstr>
# Sim_magic.read file;;
- : string = "SQLi"
# (Block.Sim.caps file).powersafe_overwrite;;
- : bool = true

On a real file, the same functor applies to Block_eio.Platform.File, whose files come from Eio.Path.open_out or from Block_eio.Platform.Names.

Standards #

The fields of Block.Platform.caps are the device characteristics of SQLite's VFS, which a layer reads to decide what it may skip:

  • sector_size is xSectorSize, the unit a write rewrites;
  • atomic_write is the largest write that is all-or-nothing across a power cut (SQLITE_IOCAP_ATOMIC*), which lets a commit of one such write skip its journal, as atomic commit describes;
  • powersafe_overwrite is SQLITE_IOCAP_POWERSAFE_OVERWRITE: a write leaves the rest of its sector as it was;
  • sequential and safe_append are SQLITE_IOCAP_SEQUENTIAL and SQLITE_IOCAP_SAFE_APPEND, writes in issue order and appended bytes durable before the new size;
  • name_barrier says whether syncing a directory entry is a real barrier.

The device characteristics page lists the SQLite flags. A platform that cannot be sure of a promise leaves its field at the safe value. CRC32C is the Castagnoli polynomial, as iSCSI uses it (RFC 3720, appendix B.4).

Papers #

  • Thanumalayan Sankaranarayana Pillai, Vijay Chidambaram, Ramnatthan Alagappan, Samer Al-Kiswany, Andrea Arpaci-Dusseau and Remzi Arpaci-Dusseau, "All File Systems Are Not Created Equal: On the Complexity of Crafting Crash-Consistent Applications", OSDI 2014. What a file system keeps of a write across a crash differs from one to the next. caps therefore states each promise and defaults to the weakest.
  • Guy Castagnoli, Stefan Bräuer and Martin Herrmann, "Optimization of Cyclic Redundancy-Check Codes with 24 and 32 Parity Bits", IEEE Transactions on Communications 41(6), 1993. The CRC32C polynomial of Block.with_crc32c.

mirage-block is the MirageOS block interface, built on Lwt, and mirage-block-unix its Unix backend.

Licence #

ISC. See LICENSE.md.