Blame SOURCES/0005-python-Implement-nbdkit-API-version-2.patch

538efe
From 49ef7e7d7c3602cc8e53d2052fce9d3a12840ea2 Mon Sep 17 00:00:00 2001
538efe
From: "Richard W.M. Jones" <rjones@redhat.com>
538efe
Date: Thu, 21 Nov 2019 15:44:39 +0000
538efe
Subject: [PATCH 05/19] python: Implement nbdkit API version 2.
538efe
538efe
To avoid breaking existing plugins, Python plugins wishing to use
538efe
version 2 of the API must opt in by declaring:
538efe
538efe
  API_VERSION = 2
538efe
538efe
(Plugins which do not do this are assumed to want API version 1).
538efe
538efe
For v2 API, we also avoid a copy by passing a buffer into pread.
538efe
538efe
It's more efficient if we pass the C buffer directly to Python code.
538efe
In some cases the Python code will be able to write directly into the
538efe
C buffer using functions like file.readinto and socket.recv_into.
538efe
This avoids an extra copy.
538efe
538efe
Thanks: Nir Soffer
538efe
https://www.redhat.com/archives/libguestfs/2019-November/thread.html#00220
538efe
(cherry picked from commit a9b2637cf4f00fb8a25ffaf31ee83be5fe019ae2)
538efe
---
538efe
 plugins/python/example.py               |  20 +++-
538efe
 plugins/python/nbdkit-python-plugin.pod |  69 +++++++-----
538efe
 plugins/python/python.c                 | 139 +++++++++++++++++++-----
538efe
 tests/python-exception.py               |   4 +-
538efe
 tests/shebang.py                        |   5 +-
538efe
 tests/test.py                           |  28 +++--
538efe
 6 files changed, 190 insertions(+), 75 deletions(-)
538efe
538efe
diff --git a/plugins/python/example.py b/plugins/python/example.py
538efe
index 60f9d7f0..c04b7e29 100644
538efe
--- a/plugins/python/example.py
538efe
+++ b/plugins/python/example.py
538efe
@@ -34,6 +34,12 @@ import errno
538efe
 disk = bytearray(1024 * 1024)
538efe
 
538efe
 
538efe
+# There are several variants of the API.  nbdkit will call this
538efe
+# function first to determine which one you want to use.  This is the
538efe
+# latest version at the time this example was written.
538efe
+API_VERSION = 2
538efe
+
538efe
+
538efe
 # This just prints the extra command line parameters, but real plugins
538efe
 # should parse them and reject any unknown parameters.
538efe
 def config(key, value):
538efe
@@ -54,20 +60,22 @@ def get_size(h):
538efe
     return len(disk)
538efe
 
538efe
 
538efe
-def pread(h, count, offset):
538efe
+def pread(h, buf, offset, flags):
538efe
     global disk
538efe
-    return disk[offset:offset+count]
538efe
+    end = offset + len(buf)
538efe
+    buf[:] = disk[offset:end]
538efe
+    # or if reading from a file you can use:
538efe
+    #f.readinto(buf)
538efe
 
538efe
-
538efe
-def pwrite(h, buf, offset):
538efe
+def pwrite(h, buf, offset, flags):
538efe
     global disk
538efe
     end = offset + len(buf)
538efe
     disk[offset:end] = buf
538efe
 
538efe
 
538efe
-def zero(h, count, offset, may_trim):
538efe
+def zero(h, count, offset, flags):
538efe
     global disk
538efe
-    if may_trim:
538efe
+    if flags & nbdkit.FLAG_MAY_TRIM:
538efe
         disk[offset:offset+count] = bytearray(count)
538efe
     else:
538efe
         nbdkit.set_error(errno.EOPNOTSUPP)
538efe
diff --git a/plugins/python/nbdkit-python-plugin.pod b/plugins/python/nbdkit-python-plugin.pod
538efe
index 3680fd65..4923d9da 100644
538efe
--- a/plugins/python/nbdkit-python-plugin.pod
538efe
+++ b/plugins/python/nbdkit-python-plugin.pod
538efe
@@ -33,11 +33,12 @@ To write a Python nbdkit plugin, you create a Python file which
538efe
 contains at least the following required functions (in the top level
538efe
 C<__main__> module):
538efe
 
538efe
+ API_VERSION = 2
538efe
  def open(readonly):
538efe
    # see below
538efe
  def get_size(h):
538efe
    # see below
538efe
- def pread(h, count, offset):
538efe
+ def pread(h, buf, offset, flags):
538efe
    # see below
538efe
 
538efe
 Note that the subroutines must have those literal names (like C<open>),
538efe
@@ -82,6 +83,18 @@ I<--dump-plugin> option, eg:
538efe
  python_version=3.7.0
538efe
  python_pep_384_abi_version=3
538efe
 
538efe
+=head2 API versions
538efe
+
538efe
+The nbdkit API has evolved and new versions are released periodically.
538efe
+To ensure backwards compatibility plugins have to opt in to the new
538efe
+version.  From Python you do this by declaring a constant in your
538efe
+module:
538efe
+
538efe
+ API_VERSION = 2
538efe
+
538efe
+(where 2 is the latest version at the time this documentation was
538efe
+written).  All newly written Python modules must have this constant.
538efe
+
538efe
 =head2 Executable script
538efe
 
538efe
 If you want you can make the script executable and include a "shebang"
538efe
@@ -199,16 +212,12 @@ contents will be garbage collected.
538efe
 
538efe
 (Required)
538efe
 
538efe
- def pread(h, count, offset):
538efe
-   # construct a buffer of length count bytes and return it
538efe
+ def pread(h, buf, offset, flags):
538efe
+   # read into the buffer
538efe
 
538efe
-The body of your C<pread> function should construct a buffer of length
538efe
-(at least) C<count> bytes.  You should read C<count> bytes from the
538efe
-disk starting at C<offset>.
538efe
-
538efe
-The returned buffer can be any type compatible with the Python 3
538efe
-buffer protocol, such as bytearray, bytes or memoryview
538efe
-(L<https://docs.python.org/3/c-api/buffer.html>)
538efe
+The body of your C<pread> function should read exactly C<len(buf)>
538efe
+bytes of data starting at disk C<offset> and write it into the buffer
538efe
+C<buf>.  C<flags> is always 0.
538efe
 
538efe
 NBD only supports whole reads, so your function should try to read
538efe
 the whole region (perhaps requiring a loop).  If the read fails or
538efe
@@ -219,13 +228,13 @@ C<nbdkit.set_error> first.
538efe
 
538efe
 (Optional)
538efe
 
538efe
- def pwrite(h, buf, offset):
538efe
+ def pwrite(h, buf, offset, flags):
538efe
    length = len (buf)
538efe
    # no return value
538efe
 
538efe
 The body of your C<pwrite> function should write the buffer C<buf> to
538efe
 the disk.  You should write C<count> bytes to the disk starting at
538efe
-C<offset>.
538efe
+C<offset>.  C<flags> may contain C<nbdkit.FLAG_FUA>.
538efe
 
538efe
 NBD only supports whole writes, so your function should try to
538efe
 write the whole region (perhaps requiring a loop).  If the write
538efe
@@ -236,11 +245,12 @@ fails or is partial, your function should throw an exception,
538efe
 
538efe
 (Optional)
538efe
 
538efe
- def flush(h):
538efe
+ def flush(h, flags):
538efe
    # no return value
538efe
 
538efe
 The body of your C<flush> function should do a L<sync(2)> or
538efe
 L<fdatasync(2)> or equivalent on the backing store.
538efe
+C<flags> is always 0.
538efe
 
538efe
 If the flush fails, your function should throw an exception, optionally
538efe
 using C<nbdkit.set_error> first.
538efe
@@ -249,32 +259,35 @@ using C<nbdkit.set_error> first.
538efe
 
538efe
 (Optional)
538efe
 
538efe
- def trim(h, count, offset):
538efe
+ def trim(h, count, offset, flags):
538efe
    # no return value
538efe
 
538efe
-The body of your C<trim> function should "punch a hole" in the
538efe
-backing store.  If the trim fails, your function should throw an
538efe
-exception, optionally using C<nbdkit.set_error> first.
538efe
+The body of your C<trim> function should "punch a hole" in the backing
538efe
+store.  C<flags> may contain C<nbdkit.FLAG_FUA>.  If the trim fails,
538efe
+your function should throw an exception, optionally using
538efe
+C<nbdkit.set_error> first.
538efe
 
538efe
 =item C<zero>
538efe
 
538efe
 (Optional)
538efe
 
538efe
- def zero(h, count, offset, may_trim):
538efe
+ def zero(h, count, offset, flags):
538efe
    # no return value
538efe
 
538efe
-The body of your C<zero> function should ensure that C<count> bytes
538efe
-of the disk, starting at C<offset>, will read back as zero.  If
538efe
-C<may_trim> is true, the operation may be optimized as a trim as long
538efe
-as subsequent reads see zeroes.
538efe
+The body of your C<zero> function should ensure that C<count> bytes of
538efe
+the disk, starting at C<offset>, will read back as zero.  C<flags> is
538efe
+a bitmask which may include C<nbdkit.FLAG_MAY_TRIM>,
538efe
+C<nbdkit.FLAG_FUA>, C<nbdkit.FLAG_FAST_ZERO>.
538efe
 
538efe
 NBD only supports whole writes, so your function should try to
538efe
-write the whole region (perhaps requiring a loop).  If the write
538efe
-fails or is partial, your function should throw an exception,
538efe
-optionally using C<nbdkit.set_error> first.  In particular, if
538efe
-you would like to automatically fall back to C<pwrite> (perhaps
538efe
-because there is nothing to optimize if C<may_trim> is false),
538efe
-use C<nbdkit.set_error(errno.EOPNOTSUPP)>.
538efe
+write the whole region (perhaps requiring a loop).
538efe
+
538efe
+If the write fails or is partial, your function should throw an
538efe
+exception, optionally using C<nbdkit.set_error> first.  In particular,
538efe
+if you would like to automatically fall back to C<pwrite> (perhaps
538efe
+because there is nothing to optimize if
538efe
+S<C<flags & nbdkit.FLAG_MAY_TRIM>> is false), use
538efe
+S<C<nbdkit.set_error (errno.EOPNOTSUPP)>>.
538efe
 
538efe
 =back
538efe
 
538efe
diff --git a/plugins/python/python.c b/plugins/python/python.c
538efe
index 47da0838..0f28595f 100644
538efe
--- a/plugins/python/python.c
538efe
+++ b/plugins/python/python.c
538efe
@@ -46,6 +46,8 @@
538efe
 #define PY_SSIZE_T_CLEAN 1
538efe
 #include <Python.h>
538efe
 
538efe
+#define NBDKIT_API_VERSION 2
538efe
+
538efe
 #include <nbdkit-plugin.h>
538efe
 
538efe
 #include "cleanup.h"
538efe
@@ -60,6 +62,7 @@
538efe
  */
538efe
 static const char *script;
538efe
 static PyObject *module;
538efe
+static int py_api_version = 1;
538efe
 
538efe
 static int last_error;
538efe
 
538efe
@@ -285,9 +288,14 @@ py_dump_plugin (void)
538efe
   PyObject *fn;
538efe
   PyObject *r;
538efe
 
538efe
+  /* Python version and ABI. */
538efe
   printf ("python_version=%s\n", PY_VERSION);
538efe
   printf ("python_pep_384_abi_version=%d\n", PYTHON_ABI_VERSION);
538efe
 
538efe
+  /* Maximum nbdkit API version supported. */
538efe
+  printf ("nbdkit_python_maximum_api_version=%d\n", NBDKIT_API_VERSION);
538efe
+
538efe
+  /* If the script has a dump_plugin function, call it. */
538efe
   if (script && callback_defined ("dump_plugin", &fn)) {
538efe
     PyErr_Clear ();
538efe
 
538efe
@@ -297,6 +305,30 @@ py_dump_plugin (void)
538efe
   }
538efe
 }
538efe
 
538efe
+static int
538efe
+get_py_api_version (void)
538efe
+{
538efe
+  PyObject *obj;
538efe
+  long value;
538efe
+
538efe
+  obj = PyObject_GetAttrString (module, "API_VERSION");
538efe
+  if (obj == NULL)
538efe
+    return 1;                   /* Default to API version 1. */
538efe
+
538efe
+  value = PyLong_AsLong (obj);
538efe
+  Py_DECREF (obj);
538efe
+
538efe
+  if (value < 1 || value > NBDKIT_API_VERSION) {
538efe
+    nbdkit_error ("%s: API_VERSION requested unknown version: %ld.  "
538efe
+                  "This plugin supports API versions between 1 and %d.",
538efe
+                  script, value, NBDKIT_API_VERSION);
538efe
+    return -1;
538efe
+  }
538efe
+
538efe
+  nbdkit_debug ("module requested API_VERSION %ld", value);
538efe
+  return (int) value;
538efe
+}
538efe
+
538efe
 static int
538efe
 py_config (const char *key, const char *value)
538efe
 {
538efe
@@ -359,6 +391,11 @@ py_config (const char *key, const char *value)
538efe
                     "nbdkit requires these callbacks.", script);
538efe
       return -1;
538efe
     }
538efe
+
538efe
+    /* Get the API version. */
538efe
+    py_api_version = get_py_api_version ();
538efe
+    if (py_api_version == -1)
538efe
+      return -1;
538efe
   }
538efe
   else if (callback_defined ("config", &fn)) {
538efe
     /* Other parameters are passed to the Python .config callback. */
538efe
@@ -469,8 +506,8 @@ py_get_size (void *handle)
538efe
 }
538efe
 
538efe
 static int
538efe
-py_pread (void *handle, void *buf,
538efe
-          uint32_t count, uint64_t offset)
538efe
+py_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
538efe
+          uint32_t flags)
538efe
 {
538efe
   PyObject *obj = handle;
538efe
   PyObject *fn;
538efe
@@ -485,24 +522,40 @@ py_pread (void *handle, void *buf,
538efe
 
538efe
   PyErr_Clear ();
538efe
 
538efe
-  r = PyObject_CallFunction (fn, "OiL", obj, count, offset);
538efe
+  switch (py_api_version) {
538efe
+  case 1:
538efe
+    r = PyObject_CallFunction (fn, "OiL", obj, count, offset);
538efe
+    break;
538efe
+  case 2:
538efe
+    r = PyObject_CallFunction (fn, "ONLI", obj,
538efe
+          PyMemoryView_FromMemory ((char *)buf, count, PyBUF_WRITE),
538efe
+          offset, flags);
538efe
+    break;
538efe
+  default: abort ();
538efe
+  }
538efe
   Py_DECREF (fn);
538efe
   if (check_python_failure ("pread") == -1)
538efe
     return ret;
538efe
 
538efe
-  if (PyObject_GetBuffer (r, &view, PyBUF_SIMPLE) == -1) {
538efe
-    nbdkit_error ("%s: value returned from pread does not support the "
538efe
-                  "buffer protocol",
538efe
-                  script);
538efe
-    goto out;
538efe
-  }
538efe
+  if (py_api_version == 1) {
538efe
+    /* In API v1 the Python pread function had to return a buffer
538efe
+     * protocol compatible function.  In API v2+ it writes directly to
538efe
+     * the C buffer so this code is not used.
538efe
+     */
538efe
+    if (PyObject_GetBuffer (r, &view, PyBUF_SIMPLE) == -1) {
538efe
+      nbdkit_error ("%s: value returned from pread does not support the "
538efe
+                    "buffer protocol",
538efe
+                    script);
538efe
+      goto out;
538efe
+    }
538efe
 
538efe
-  if (view.len < count) {
538efe
-    nbdkit_error ("%s: buffer returned from pread is too small", script);
538efe
-    goto out;
538efe
-  }
538efe
+    if (view.len < count) {
538efe
+      nbdkit_error ("%s: buffer returned from pread is too small", script);
538efe
+      goto out;
538efe
+    }
538efe
 
538efe
-  memcpy (buf, view.buf, count);
538efe
+    memcpy (buf, view.buf, count);
538efe
+  }
538efe
   ret = 0;
538efe
 
538efe
 out:
538efe
@@ -515,8 +568,8 @@ out:
538efe
 }
538efe
 
538efe
 static int
538efe
-py_pwrite (void *handle, const void *buf,
538efe
-           uint32_t count, uint64_t offset)
538efe
+py_pwrite (void *handle, const void *buf, uint32_t count, uint64_t offset,
538efe
+           uint32_t flags)
538efe
 {
538efe
   PyObject *obj = handle;
538efe
   PyObject *fn;
538efe
@@ -525,9 +578,19 @@ py_pwrite (void *handle, const void *buf,
538efe
   if (callback_defined ("pwrite", &fn)) {
538efe
     PyErr_Clear ();
538efe
 
538efe
-    r = PyObject_CallFunction (fn, "ONL", obj,
538efe
+    switch (py_api_version) {
538efe
+    case 1:
538efe
+      r = PyObject_CallFunction (fn, "ONL", obj,
538efe
             PyMemoryView_FromMemory ((char *)buf, count, PyBUF_READ),
538efe
             offset);
538efe
+      break;
538efe
+    case 2:
538efe
+      r = PyObject_CallFunction (fn, "ONLI", obj,
538efe
+            PyMemoryView_FromMemory ((char *)buf, count, PyBUF_READ),
538efe
+            offset, flags);
538efe
+      break;
538efe
+    default: abort ();
538efe
+    }
538efe
     Py_DECREF (fn);
538efe
     if (check_python_failure ("pwrite") == -1)
538efe
       return -1;
538efe
@@ -542,7 +605,7 @@ py_pwrite (void *handle, const void *buf,
538efe
 }
538efe
 
538efe
 static int
538efe
-py_flush (void *handle)
538efe
+py_flush (void *handle, uint32_t flags)
538efe
 {
538efe
   PyObject *obj = handle;
538efe
   PyObject *fn;
538efe
@@ -551,7 +614,15 @@ py_flush (void *handle)
538efe
   if (callback_defined ("flush", &fn)) {
538efe
     PyErr_Clear ();
538efe
 
538efe
-    r = PyObject_CallFunctionObjArgs (fn, obj, NULL);
538efe
+    switch (py_api_version) {
538efe
+    case 1:
538efe
+      r = PyObject_CallFunctionObjArgs (fn, obj, NULL);
538efe
+      break;
538efe
+    case 2:
538efe
+      r = PyObject_CallFunction (fn, "OI", obj, flags);
538efe
+      break;
538efe
+    default: abort ();
538efe
+    }
538efe
     Py_DECREF (fn);
538efe
     if (check_python_failure ("flush") == -1)
538efe
       return -1;
538efe
@@ -566,7 +637,7 @@ py_flush (void *handle)
538efe
 }
538efe
 
538efe
 static int
538efe
-py_trim (void *handle, uint32_t count, uint64_t offset)
538efe
+py_trim (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
538efe
 {
538efe
   PyObject *obj = handle;
538efe
   PyObject *fn;
538efe
@@ -575,7 +646,15 @@ py_trim (void *handle, uint32_t count, uint64_t offset)
538efe
   if (callback_defined ("trim", &fn)) {
538efe
     PyErr_Clear ();
538efe
 
538efe
-    r = PyObject_CallFunction (fn, "OiL", obj, count, offset);
538efe
+    switch (py_api_version) {
538efe
+    case 1:
538efe
+      r = PyObject_CallFunction (fn, "OiL", obj, count, offset);
538efe
+      break;
538efe
+    case 2:
538efe
+      r = PyObject_CallFunction (fn, "OiLI", obj, count, offset, flags);
538efe
+      break;
538efe
+    default: abort ();
538efe
+    }
538efe
     Py_DECREF (fn);
538efe
     if (check_python_failure ("trim") == -1)
538efe
       return -1;
538efe
@@ -590,7 +669,7 @@ py_trim (void *handle, uint32_t count, uint64_t offset)
538efe
 }
538efe
 
538efe
 static int
538efe
-py_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
538efe
+py_zero (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
538efe
 {
538efe
   PyObject *obj = handle;
538efe
   PyObject *fn;
538efe
@@ -600,9 +679,19 @@ py_zero (void *handle, uint32_t count, uint64_t offset, int may_trim)
538efe
     PyErr_Clear ();
538efe
 
538efe
     last_error = 0;
538efe
-    r = PyObject_CallFunction (fn, "OiLO",
538efe
-                               obj, count, offset,
538efe
-                               may_trim ? Py_True : Py_False);
538efe
+    switch (py_api_version) {
538efe
+    case 1: {
538efe
+      int may_trim = flags & NBDKIT_FLAG_MAY_TRIM;
538efe
+      r = PyObject_CallFunction (fn, "OiLO",
538efe
+                                 obj, count, offset,
538efe
+                                 may_trim ? Py_True : Py_False);
538efe
+      break;
538efe
+    }
538efe
+    case 2:
538efe
+      r = PyObject_CallFunction (fn, "OiLI", obj, count, offset, flags);
538efe
+      break;
538efe
+    default: abort ();
538efe
+    }
538efe
     Py_DECREF (fn);
538efe
     if (last_error == EOPNOTSUPP || last_error == ENOTSUP) {
538efe
       /* When user requests this particular error, we want to
538efe
diff --git a/tests/python-exception.py b/tests/python-exception.py
538efe
index d0c79bb0..ee4a3f3a 100644
538efe
--- a/tests/python-exception.py
538efe
+++ b/tests/python-exception.py
538efe
@@ -62,5 +62,5 @@ def get_size(h):
538efe
     return 0
538efe
 
538efe
 
538efe
-def pread(h, count, offset):
538efe
-    return ""
538efe
+def pread(h, buf, offset):
538efe
+    buf[:] = bytearray(len(buf))
538efe
diff --git a/tests/shebang.py b/tests/shebang.py
538efe
index 6f336230..0634589a 100755
538efe
--- a/tests/shebang.py
538efe
+++ b/tests/shebang.py
538efe
@@ -13,6 +13,7 @@ def get_size(h):
538efe
     return len(disk)
538efe
 
538efe
 
538efe
-def pread(h, count, offset):
538efe
+def pread(h, buf, offset):
538efe
     global disk
538efe
-    return disk[offset:offset+count]
538efe
+    end = offset + len(buf)
538efe
+    buf[:] = disk[offset:end]
538efe
diff --git a/tests/test.py b/tests/test.py
538efe
index 9a2e947d..4db56623 100644
538efe
--- a/tests/test.py
538efe
+++ b/tests/test.py
538efe
@@ -3,6 +3,9 @@ import nbdkit
538efe
 disk = bytearray(1024*1024)
538efe
 
538efe
 
538efe
+API_VERSION = 2
538efe
+
538efe
+
538efe
 def config_complete():
538efe
     print ("set_error = %r" % nbdkit.set_error)
538efe
 
538efe
@@ -32,25 +35,26 @@ def can_trim(h):
538efe
     return True
538efe
 
538efe
 
538efe
-def pread(h, count, offset):
538efe
+def pread(h, buf, offset, flags):
538efe
     global disk
538efe
-    return disk[offset:offset+count]
538efe
+    end = offset + len(buf)
538efe
+    buf[:] = disk[offset:end]
538efe
 
538efe
 
538efe
-def pwrite(h, buf, offset):
538efe
+def pwrite(h, buf, offset, flags):
538efe
     global disk
538efe
     end = offset + len(buf)
538efe
     disk[offset:end] = buf
538efe
 
538efe
 
538efe
-def zero(h, count, offset, may_trim=False):
538efe
+def flush(h, flags):
538efe
+    pass
538efe
+
538efe
+
538efe
+def trim(h, count, offset, flags):
538efe
+    pass
538efe
+
538efe
+
538efe
+def zero(h, count, offset, flags):
538efe
     global disk
538efe
     disk[offset:offset+count] = bytearray(count)
538efe
-
538efe
-
538efe
-def flush(h):
538efe
-    pass
538efe
-
538efe
-
538efe
-def trim(h, count, offset):
538efe
-    pass
538efe
-- 
538efe
2.18.2
538efe