316 lines
9.9 KiB
Diff
316 lines
9.9 KiB
Diff
|
From 1610c0865534819eccefec55fd2d751843bb6d64 Mon Sep 17 00:00:00 2001
|
||
|
From: "Richard W.M. Jones" <rjones@redhat.com>
|
||
|
Date: Tue, 13 Jul 2021 16:15:45 +0100
|
||
|
Subject: [PATCH 4/7] ocaml: Rearrange the callbacks
|
||
|
|
||
|
Just refactoring.
|
||
|
|
||
|
(cherry picked from commit e54e16e81c51dcbb16d70d83c5b0403babdf5f99)
|
||
|
---
|
||
|
plugins/ocaml/NBDKit.mli | 31 +++++++++--------
|
||
|
plugins/ocaml/callbacks.h | 34 +++++++++----------
|
||
|
plugins/ocaml/NBDKit.ml | 68 ++++++++++++++++++--------------------
|
||
|
tests/test_ocaml_plugin.ml | 9 ++---
|
||
|
4 files changed, 72 insertions(+), 70 deletions(-)
|
||
|
|
||
|
diff --git a/plugins/ocaml/NBDKit.mli b/plugins/ocaml/NBDKit.mli
|
||
|
index ac4b0cbc..cda09f44 100644
|
||
|
--- a/plugins/ocaml/NBDKit.mli
|
||
|
+++ b/plugins/ocaml/NBDKit.mli
|
||
|
@@ -69,33 +69,31 @@ type thread_model =
|
||
|
The ['a] parameter is the handle type returned by your
|
||
|
[open_connection] method and passed back to all connected calls. *)
|
||
|
type 'a plugin = {
|
||
|
- name : string; (* required *)
|
||
|
+ (* Plugin description. *)
|
||
|
+ name : string; (** required field *)
|
||
|
longname : string;
|
||
|
version : string;
|
||
|
description : string;
|
||
|
|
||
|
+ (* Plugin lifecycle. *)
|
||
|
load : (unit -> unit) option;
|
||
|
+ get_ready : (unit -> unit) option;
|
||
|
+ after_fork : (unit -> unit) option;
|
||
|
unload : (unit -> unit) option;
|
||
|
|
||
|
- dump_plugin : (unit -> unit) option;
|
||
|
-
|
||
|
+ (* Plugin configuration. *)
|
||
|
config : (string -> string -> unit) option;
|
||
|
config_complete : (unit -> unit) option;
|
||
|
config_help : string;
|
||
|
thread_model : (unit -> thread_model) option;
|
||
|
|
||
|
- get_ready : (unit -> unit) option;
|
||
|
- after_fork : (unit -> unit) option;
|
||
|
-
|
||
|
+ (* Connection lifecycle. *)
|
||
|
preconnect : (bool -> unit) option;
|
||
|
- list_exports : (bool -> bool -> export list) option;
|
||
|
- default_export : (bool -> bool -> string) option;
|
||
|
- open_connection : (bool -> 'a) option; (* required *)
|
||
|
+ open_connection : (bool -> 'a) option; (** required field *)
|
||
|
close : ('a -> unit) option;
|
||
|
|
||
|
- get_size : ('a -> int64) option; (* required *)
|
||
|
- export_description : ('a -> string) option;
|
||
|
-
|
||
|
+ (* NBD negotiation. *)
|
||
|
+ get_size : ('a -> int64) option; (** required field *)
|
||
|
can_cache : ('a -> cache_flag) option;
|
||
|
can_extents : ('a -> bool) option;
|
||
|
can_fast_zero : ('a -> bool) option;
|
||
|
@@ -107,13 +105,20 @@ type 'a plugin = {
|
||
|
can_zero : ('a -> bool) option;
|
||
|
is_rotational : ('a -> bool) option;
|
||
|
|
||
|
- pread : ('a -> int32 -> int64 -> flags -> string) option; (* required *)
|
||
|
+ (* Serving data. *)
|
||
|
+ pread : ('a -> int32 -> int64 -> flags -> string) option; (* required field *)
|
||
|
pwrite : ('a -> string -> int64 -> flags -> unit) option;
|
||
|
flush : ('a -> flags -> unit) option;
|
||
|
trim : ('a -> int32 -> int64 -> flags -> unit) option;
|
||
|
zero : ('a -> int32 -> int64 -> flags -> unit) option;
|
||
|
extents : ('a -> int32 -> int64 -> flags -> extent list) option;
|
||
|
cache : ('a -> int32 -> int64 -> flags -> unit) option;
|
||
|
+
|
||
|
+ (* Miscellaneous. *)
|
||
|
+ dump_plugin : (unit -> unit) option;
|
||
|
+ list_exports : (bool -> bool -> export list) option;
|
||
|
+ default_export : (bool -> bool -> string) option;
|
||
|
+ export_description : ('a -> string) option;
|
||
|
}
|
||
|
|
||
|
(** The plugin with all fields set to [None], so you can write
|
||
|
diff --git a/plugins/ocaml/callbacks.h b/plugins/ocaml/callbacks.h
|
||
|
index 7171ef21..4d29fb73 100644
|
||
|
--- a/plugins/ocaml/callbacks.h
|
||
|
+++ b/plugins/ocaml/callbacks.h
|
||
|
@@ -33,21 +33,8 @@
|
||
|
/* This is not a header file. It is included at various places in
|
||
|
* plugin.c as a convenient way to define per-callback things.
|
||
|
*/
|
||
|
-CB(load)
|
||
|
-CB(unload)
|
||
|
-CB(dump_plugin)
|
||
|
-CB(config)
|
||
|
-CB(config_complete)
|
||
|
-CB(thread_model)
|
||
|
-CB(get_ready)
|
||
|
CB(after_fork)
|
||
|
-CB(preconnect)
|
||
|
-CB(list_exports)
|
||
|
-CB(default_export)
|
||
|
-CB(open)
|
||
|
-CB(close)
|
||
|
-CB(get_size)
|
||
|
-CB(export_description)
|
||
|
+CB(cache)
|
||
|
CB(can_cache)
|
||
|
CB(can_extents)
|
||
|
CB(can_fast_zero)
|
||
|
@@ -57,11 +44,24 @@ CB(can_multi_conn)
|
||
|
CB(can_trim)
|
||
|
CB(can_write)
|
||
|
CB(can_zero)
|
||
|
+CB(close)
|
||
|
+CB(config)
|
||
|
+CB(config_complete)
|
||
|
+CB(default_export)
|
||
|
+CB(dump_plugin)
|
||
|
+CB(export_description)
|
||
|
+CB(extents)
|
||
|
+CB(flush)
|
||
|
+CB(get_ready)
|
||
|
+CB(get_size)
|
||
|
CB(is_rotational)
|
||
|
+CB(list_exports)
|
||
|
+CB(load)
|
||
|
+CB(open)
|
||
|
CB(pread)
|
||
|
+CB(preconnect)
|
||
|
CB(pwrite)
|
||
|
-CB(flush)
|
||
|
+CB(thread_model)
|
||
|
CB(trim)
|
||
|
+CB(unload)
|
||
|
CB(zero)
|
||
|
-CB(extents)
|
||
|
-CB(cache)
|
||
|
diff --git a/plugins/ocaml/NBDKit.ml b/plugins/ocaml/NBDKit.ml
|
||
|
index cdc3bc58..529618d2 100644
|
||
|
--- a/plugins/ocaml/NBDKit.ml
|
||
|
+++ b/plugins/ocaml/NBDKit.ml
|
||
|
@@ -65,27 +65,20 @@ type 'a plugin = {
|
||
|
description : string;
|
||
|
|
||
|
load : (unit -> unit) option;
|
||
|
+ get_ready : (unit -> unit) option;
|
||
|
+ after_fork : (unit -> unit) option;
|
||
|
unload : (unit -> unit) option;
|
||
|
|
||
|
- dump_plugin : (unit -> unit) option;
|
||
|
-
|
||
|
config : (string -> string -> unit) option;
|
||
|
config_complete : (unit -> unit) option;
|
||
|
config_help : string;
|
||
|
thread_model : (unit -> thread_model) option;
|
||
|
|
||
|
- get_ready : (unit -> unit) option;
|
||
|
- after_fork : (unit -> unit) option;
|
||
|
-
|
||
|
preconnect : (bool -> unit) option;
|
||
|
- list_exports : (bool -> bool -> export list) option;
|
||
|
- default_export : (bool -> bool -> string) option;
|
||
|
open_connection : (bool -> 'a) option;
|
||
|
close : ('a -> unit) option;
|
||
|
|
||
|
get_size : ('a -> int64) option;
|
||
|
- export_description : ('a -> string) option;
|
||
|
-
|
||
|
can_cache : ('a -> cache_flag) option;
|
||
|
can_extents : ('a -> bool) option;
|
||
|
can_fast_zero : ('a -> bool) option;
|
||
|
@@ -104,6 +97,11 @@ type 'a plugin = {
|
||
|
zero : ('a -> int32 -> int64 -> flags -> unit) option;
|
||
|
extents : ('a -> int32 -> int64 -> flags -> extent list) option;
|
||
|
cache : ('a -> int32 -> int64 -> flags -> unit) option;
|
||
|
+
|
||
|
+ dump_plugin : (unit -> unit) option;
|
||
|
+ list_exports : (bool -> bool -> export list) option;
|
||
|
+ default_export : (bool -> bool -> string) option;
|
||
|
+ export_description : ('a -> string) option;
|
||
|
}
|
||
|
|
||
|
let default_callbacks = {
|
||
|
@@ -113,27 +111,20 @@ let default_callbacks = {
|
||
|
description = "";
|
||
|
|
||
|
load = None;
|
||
|
+ get_ready = None;
|
||
|
+ after_fork = None;
|
||
|
unload = None;
|
||
|
|
||
|
- dump_plugin = None;
|
||
|
-
|
||
|
config = None;
|
||
|
config_complete = None;
|
||
|
config_help = "";
|
||
|
thread_model = None;
|
||
|
|
||
|
- get_ready = None;
|
||
|
- after_fork = None;
|
||
|
-
|
||
|
preconnect = None;
|
||
|
- list_exports = None;
|
||
|
- default_export = None;
|
||
|
open_connection = None;
|
||
|
close = None;
|
||
|
|
||
|
get_size = None;
|
||
|
- export_description = None;
|
||
|
-
|
||
|
can_cache = None;
|
||
|
can_extents = None;
|
||
|
can_fast_zero = None;
|
||
|
@@ -152,6 +143,11 @@ let default_callbacks = {
|
||
|
zero = None;
|
||
|
extents = None;
|
||
|
cache = None;
|
||
|
+
|
||
|
+ dump_plugin = None;
|
||
|
+ list_exports = None;
|
||
|
+ default_export = None;
|
||
|
+ export_description = None;
|
||
|
}
|
||
|
|
||
|
external set_name : string -> unit = "ocaml_nbdkit_set_name" "noalloc"
|
||
|
@@ -186,21 +182,8 @@ let register_plugin plugin =
|
||
|
if plugin.config_help <> "" then set_config_help plugin.config_help;
|
||
|
|
||
|
let may f = function None -> () | Some a -> f a in
|
||
|
- may (set_field "load") plugin.load;
|
||
|
- may (set_field "unload") plugin.unload;
|
||
|
- may (set_field "dump_plugin") plugin.dump_plugin;
|
||
|
- may (set_field "config") plugin.config;
|
||
|
- may (set_field "config_complete") plugin.config_complete;
|
||
|
- may (set_field "thread_model") plugin.thread_model;
|
||
|
- may (set_field "get_ready") plugin.get_ready;
|
||
|
may (set_field "after_fork") plugin.after_fork;
|
||
|
- may (set_field "preconnect") plugin.preconnect;
|
||
|
- may (set_field "list_exports") plugin.list_exports;
|
||
|
- may (set_field "default_export") plugin.default_export;
|
||
|
- may (set_field "open") plugin.open_connection;
|
||
|
- may (set_field "close") plugin.close;
|
||
|
- may (set_field "get_size") plugin.get_size;
|
||
|
- may (set_field "export_description") plugin.export_description;
|
||
|
+ may (set_field "cache") plugin.cache;
|
||
|
may (set_field "can_cache") plugin.can_cache;
|
||
|
may (set_field "can_extents") plugin.can_extents;
|
||
|
may (set_field "can_fast_zero") plugin.can_fast_zero;
|
||
|
@@ -210,14 +193,27 @@ let register_plugin plugin =
|
||
|
may (set_field "can_trim") plugin.can_trim;
|
||
|
may (set_field "can_write") plugin.can_write;
|
||
|
may (set_field "can_zero") plugin.can_zero;
|
||
|
+ may (set_field "close") plugin.close;
|
||
|
+ may (set_field "config") plugin.config;
|
||
|
+ may (set_field "config_complete") plugin.config_complete;
|
||
|
+ may (set_field "default_export") plugin.default_export;
|
||
|
+ may (set_field "dump_plugin") plugin.dump_plugin;
|
||
|
+ may (set_field "export_description") plugin.export_description;
|
||
|
+ may (set_field "extents") plugin.extents;
|
||
|
+ may (set_field "flush") plugin.flush;
|
||
|
+ may (set_field "get_ready") plugin.get_ready;
|
||
|
+ may (set_field "get_size") plugin.get_size;
|
||
|
may (set_field "is_rotational") plugin.is_rotational;
|
||
|
+ may (set_field "list_exports") plugin.list_exports;
|
||
|
+ may (set_field "load") plugin.load;
|
||
|
+ may (set_field "open") plugin.open_connection;
|
||
|
may (set_field "pread") plugin.pread;
|
||
|
+ may (set_field "preconnect") plugin.preconnect;
|
||
|
may (set_field "pwrite") plugin.pwrite;
|
||
|
- may (set_field "flush") plugin.flush;
|
||
|
+ may (set_field "thread_model") plugin.thread_model;
|
||
|
may (set_field "trim") plugin.trim;
|
||
|
- may (set_field "zero") plugin.zero;
|
||
|
- may (set_field "extents") plugin.extents;
|
||
|
- may (set_field "cache") plugin.cache
|
||
|
+ may (set_field "unload") plugin.unload;
|
||
|
+ may (set_field "zero") plugin.zero
|
||
|
|
||
|
(* Bindings to nbdkit server functions. *)
|
||
|
|
||
|
diff --git a/tests/test_ocaml_plugin.ml b/tests/test_ocaml_plugin.ml
|
||
|
index fee8528e..00a65a75 100644
|
||
|
--- a/tests/test_ocaml_plugin.ml
|
||
|
+++ b/tests/test_ocaml_plugin.ml
|
||
|
@@ -101,19 +101,20 @@ let plugin = {
|
||
|
load = Some load;
|
||
|
get_ready = Some get_ready;
|
||
|
unload = Some unload;
|
||
|
+
|
||
|
config = Some config;
|
||
|
config_complete = Some config_complete;
|
||
|
+ thread_model = Some thread_model;
|
||
|
|
||
|
open_connection = Some open_connection;
|
||
|
close = Some close;
|
||
|
- list_exports = Some list_exports;
|
||
|
- default_export = Some default_export;
|
||
|
get_size = Some get_size;
|
||
|
pread = Some pread;
|
||
|
pwrite = Some pwrite;
|
||
|
-
|
||
|
extents = Some extents;
|
||
|
- thread_model = Some thread_model;
|
||
|
+
|
||
|
+ list_exports = Some list_exports;
|
||
|
+ default_export = Some default_export;
|
||
|
}
|
||
|
|
||
|
let () = NBDKit.register_plugin plugin
|
||
|
--
|
||
|
2.32.0
|
||
|
|