Blame SOURCES/0009-python-Implement-can_fua-and-can_cache.patch

efa7a1
From 38124a137974e1433d68732640ca7f88664557da Mon Sep 17 00:00:00 2001
efa7a1
From: "Richard W.M. Jones" <rjones@redhat.com>
efa7a1
Date: Fri, 22 Nov 2019 19:25:53 +0000
efa7a1
Subject: [PATCH] python: Implement can_fua and can_cache.
efa7a1
efa7a1
(cherry picked from commit 97c46f885edec5a61a96ac86eccb9d8c874c602e)
efa7a1
---
efa7a1
 plugins/python/nbdkit-python-plugin.pod | 18 +++++++-
efa7a1
 plugins/python/python.c                 | 58 +++++++++++++++++++++++++
efa7a1
 2 files changed, 74 insertions(+), 2 deletions(-)
efa7a1
efa7a1
diff --git a/plugins/python/nbdkit-python-plugin.pod b/plugins/python/nbdkit-python-plugin.pod
efa7a1
index b92bb56..4065ec7 100644
efa7a1
--- a/plugins/python/nbdkit-python-plugin.pod
efa7a1
+++ b/plugins/python/nbdkit-python-plugin.pod
efa7a1
@@ -229,6 +229,22 @@ contents will be garbage collected.
efa7a1
  def can_fast_zero(h):
efa7a1
    # return a boolean
efa7a1
 
efa7a1
+=item C<can_fua>
efa7a1
+
efa7a1
+(Optional)
efa7a1
+
efa7a1
+ def can_fua(h):
efa7a1
+   # return nbdkit.FUA_NONE or nbdkit.FUA_EMULATE
efa7a1
+   # or nbdkit.FUA_NATIVE
efa7a1
+
efa7a1
+=item C<can_cache>
efa7a1
+
efa7a1
+(Optional)
efa7a1
+
efa7a1
+ def can_cache(h):
efa7a1
+   # return nbdkit.CACHE_NONE or nbdkit.CACHE_EMULATE
efa7a1
+   # or nbdkit.CACHE_NATIVE
efa7a1
+
efa7a1
 =item C<pread>
efa7a1
 
efa7a1
 (Required)
efa7a1
@@ -345,8 +361,6 @@ C<longname>,
efa7a1
 C<description>,
efa7a1
 C<config_help>,
efa7a1
 C<magic_config_key>,
efa7a1
-C<can_fua>,
efa7a1
-C<can_cache>,
efa7a1
 C<can_extents>,
efa7a1
 C<extents>.
efa7a1
 
efa7a1
diff --git a/plugins/python/python.c b/plugins/python/python.c
efa7a1
index b186b99..5e2e526 100644
efa7a1
--- a/plugins/python/python.c
efa7a1
+++ b/plugins/python/python.c
efa7a1
@@ -815,6 +815,62 @@ py_can_fast_zero (void *handle)
efa7a1
   return boolean_callback (handle, "can_fast_zero", NULL);
efa7a1
 }
efa7a1
 
efa7a1
+static int
efa7a1
+py_can_fua (void *handle)
efa7a1
+{
efa7a1
+  PyObject *obj = handle;
efa7a1
+  PyObject *fn;
efa7a1
+  PyObject *r;
efa7a1
+  int ret;
efa7a1
+
efa7a1
+  if (callback_defined ("can_fua", &fn)) {
efa7a1
+    PyErr_Clear ();
efa7a1
+
efa7a1
+    r = PyObject_CallFunctionObjArgs (fn, obj, NULL);
efa7a1
+    Py_DECREF (fn);
efa7a1
+    if (check_python_failure ("can_fua") == -1)
efa7a1
+      return -1;
efa7a1
+    ret = PyLong_AsLong (r);
efa7a1
+    Py_DECREF (r);
efa7a1
+    return ret;
efa7a1
+  }
efa7a1
+  /* No Python ‘can_fua’, but check if there's a Python ‘flush’
efa7a1
+   * callback defined.  (In C modules, nbdkit would do this).
efa7a1
+   */
efa7a1
+  else if (callback_defined ("flush", NULL))
efa7a1
+    return NBDKIT_FUA_EMULATE;
efa7a1
+  else
efa7a1
+    return NBDKIT_FUA_NONE;
efa7a1
+}
efa7a1
+
efa7a1
+static int
efa7a1
+py_can_cache (void *handle)
efa7a1
+{
efa7a1
+  PyObject *obj = handle;
efa7a1
+  PyObject *fn;
efa7a1
+  PyObject *r;
efa7a1
+  int ret;
efa7a1
+
efa7a1
+  if (callback_defined ("can_cache", &fn)) {
efa7a1
+    PyErr_Clear ();
efa7a1
+
efa7a1
+    r = PyObject_CallFunctionObjArgs (fn, obj, NULL);
efa7a1
+    Py_DECREF (fn);
efa7a1
+    if (check_python_failure ("can_cache") == -1)
efa7a1
+      return -1;
efa7a1
+    ret = PyLong_AsLong (r);
efa7a1
+    Py_DECREF (r);
efa7a1
+    return ret;
efa7a1
+  }
efa7a1
+  /* No Python ‘can_cache’, but check if there's a Python ‘cache’
efa7a1
+   * callback defined.  (In C modules, nbdkit would do this).
efa7a1
+   */
efa7a1
+  else if (callback_defined ("cache", NULL))
efa7a1
+    return NBDKIT_CACHE_NATIVE;
efa7a1
+  else
efa7a1
+    return NBDKIT_CACHE_NONE;
efa7a1
+}
efa7a1
+
efa7a1
 #define py_config_help \
efa7a1
   "script=<FILENAME>     (required) The Python plugin to run.\n" \
efa7a1
   "[other arguments may be used by the plugin that you load]"
efa7a1
@@ -844,6 +900,8 @@ static struct nbdkit_plugin plugin = {
efa7a1
   .can_trim          = py_can_trim,
efa7a1
   .can_zero          = py_can_zero,
efa7a1
   .can_fast_zero     = py_can_fast_zero,
efa7a1
+  .can_fua           = py_can_fua,
efa7a1
+  .can_cache         = py_can_cache,
efa7a1
 
efa7a1
   .pread             = py_pread,
efa7a1
   .pwrite            = py_pwrite,
efa7a1
-- 
efa7a1
2.18.2
efa7a1