[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libnbd PATCH v2 17/23] ocaml: Add example for 64-bit extents
From: |
Eric Blake |
Subject: |
[libnbd PATCH v2 17/23] ocaml: Add example for 64-bit extents |
Date: |
Mon, 14 Nov 2022 16:51:52 -0600 |
Since our example program for 32-bit extents is inherently limited to
32-bit lengths, it is also worth demonstrating the 64-bit extent API,
including the difference in the array indexing being saner.
---
ocaml/examples/Makefile.am | 3 ++-
ocaml/examples/extents64.ml | 42 +++++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 1 deletion(-)
create mode 100644 ocaml/examples/extents64.ml
diff --git a/ocaml/examples/Makefile.am b/ocaml/examples/Makefile.am
index 5ee6dd63..c6f4989d 100644
--- a/ocaml/examples/Makefile.am
+++ b/ocaml/examples/Makefile.am
@@ -1,5 +1,5 @@
# nbd client library in userspace
-# Copyright (C) 2013-2019 Red Hat Inc.
+# Copyright (C) 2013-2022 Red Hat Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -20,6 +20,7 @@ include $(top_srcdir)/subdir-rules.mk
ml_examples = \
asynch_copy.ml \
extents.ml \
+ extents64.ml \
get_size.ml \
open_qcow2.ml \
server_flags.ml \
diff --git a/ocaml/examples/extents64.ml b/ocaml/examples/extents64.ml
new file mode 100644
index 00000000..8ee7e218
--- /dev/null
+++ b/ocaml/examples/extents64.ml
@@ -0,0 +1,42 @@
+open Printf
+
+let () =
+ NBD.with_handle (
+ fun nbd ->
+ NBD.add_meta_context nbd "base:allocation";
+ NBD.connect_command nbd
+ ["nbdkit"; "-s"; "--exit-with-parent"; "-r";
+ "sparse-random"; "8G"];
+
+ (* Read the extents and print them. *)
+ let size = NBD.get_size nbd in
+ let cap =
+ match NBD.get_extended_headers_negotiated nbd with
+ | true -> size
+ | false -> 0x8000_0000_L in
+ let fetch_offset = ref 0_L in
+ while !fetch_offset < size do
+ let remaining = Int64.sub size !fetch_offset in
+ let fetch_size = min remaining cap in
+ NBD.block_status_64 nbd fetch_size !fetch_offset (
+ fun meta _ entries err ->
+ printf "nbd_block_status callback: meta=%s err=%d\n" meta !err;
+ if meta = "base:allocation" then (
+ printf "index\t%16s %16s %s\n" "offset" "length" "flags";
+ for i = 0 to Array.length entries - 1 do
+ let len = fst entries.(i)
+ and flags =
+ match snd entries.(i) with
+ | 0_L -> "data"
+ | 1_L -> "hole"
+ | 2_L -> "zero"
+ | 3_L -> "hole+zero"
+ | unknown -> sprintf "unknown (%Ld)" unknown in
+ printf "%d:\t%16Ld %16Ld %s\n" i !fetch_offset len flags;
+ fetch_offset := Int64.add !fetch_offset len
+ done;
+ );
+ 0
+ ) (* NBD.block_status *)
+ done
+ )
--
2.38.1
- [cross-project PATCH v2] NBD 64-bit extensions, Eric Blake, 2022/11/14
- [libnbd PATCH v2 00/23] libnbd 64-bit NBD extensions, Eric Blake, 2022/11/14
- [libnbd PATCH v2 15/23] info: Update nbdinfo --map to use 64-bit block status, Eric Blake, 2022/11/14
- [libnbd PATCH v2 12/23] copy: Update nbdcopy to use 64-bit block status, Eric Blake, 2022/11/14
- [libnbd PATCH v2 23/23] RFC: pread: Accept 64-bit holes, Eric Blake, 2022/11/14
- [libnbd PATCH v2 21/23] api: Add nbd_can_block_status_payload(), Eric Blake, 2022/11/14
- [libnbd PATCH v2 16/23] examples: Update copy-libev to use 64-bit block status, Eric Blake, 2022/11/14
- [libnbd PATCH v2 19/23] api: Add nbd_[aio_]opt_extended_headers(), Eric Blake, 2022/11/14
- [libnbd PATCH v2 17/23] ocaml: Add example for 64-bit extents,
Eric Blake <=
- [libnbd PATCH v2 22/23] api: Add nbd_[aio_]block_status_filter(), Eric Blake, 2022/11/14
- [libnbd PATCH v2 08/23] block_status: Track 64-bit extents internally, Eric Blake, 2022/11/14
- [libnbd PATCH v2 13/23] dump: Update nbddump to use 64-bit block status, Eric Blake, 2022/11/14
- [libnbd PATCH v2 14/23] info: Expose extended-headers support through nbdinfo, Eric Blake, 2022/11/14
- [libnbd PATCH v2 06/23] states: Break deadlock if server goofs on extended replies, Eric Blake, 2022/11/14
- [libnbd PATCH v2 01/23] block_status: Refactor array storage, Eric Blake, 2022/11/14
- [libnbd PATCH v2 03/23] protocol: Add definitions for extended headers, Eric Blake, 2022/11/14
- [libnbd PATCH v2 02/23] internal: Refactor layout of replies in sbuf, Eric Blake, 2022/11/14
- [libnbd PATCH v2 18/23] generator: Actually request extended headers, Eric Blake, 2022/11/14
- [libnbd PATCH v2 20/23] interop: Add test of 64-bit block status, Eric Blake, 2022/11/14