File: plugin.c

package info (click to toggle)
nbdkit 1.42.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,696 kB
  • sloc: ansic: 59,224; sh: 16,793; makefile: 6,463; python: 1,837; cpp: 1,116; ml: 504; perl: 502; tcl: 62
file content (1142 lines) | stat: -rw-r--r-- 28,066 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
/* nbdkit
 * Copyright Red Hat
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Red Hat nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>

#include "cleanup.h"

#include "plugin.h"

/* XXX Apparently global state is technically wrong in Python 3, see:
 *
 * https://www.python.org/dev/peps/pep-3121/
 *
 * However it probably doesn't matter for nbdkit since we don't ever
 * have multiple Python interpreters or multiple instances of the
 * plugin in a single process.
 */
const char *script;             /* The Python script name. */
PyObject *module;               /* The imported __main__ module from script. */
int py_api_version = 1;         /* The declared Python API version. */

static PyThreadState *tstate;

static void
py_load (void)
{
  PyImport_AppendInittab ("nbdkit", create_nbdkit_module);
  Py_Initialize ();
  tstate = PyEval_SaveThread ();
}

static void
py_unload (void)
{
  if (tstate) {
    PyEval_RestoreThread (tstate);
    Py_XDECREF (module);
    Py_Finalize ();
  }
}

static void
py_dump_plugin (void)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;

  /* Python version and ABI. */
  printf ("python_version=%s\n", PY_VERSION);
  printf ("python_pep_384_abi_version=%d\n", PYTHON_ABI_VERSION);

  /* Maximum nbdkit API version supported. */
  printf ("nbdkit_python_maximum_api_version=%d\n", NBDKIT_API_VERSION);

  /* If the script has a dump_plugin function, call it. */
  if (script && callback_defined ("dump_plugin", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallObject (fn, NULL);
    Py_DECREF (fn);
    Py_DECREF (r);
  }
}

static int
get_py_api_version (void)
{
  PyObject *obj;
  long value;

  obj = PyObject_GetAttrString (module, "API_VERSION");
  if (obj == NULL)
    return 1;                   /* Default to API version 1. */

  value = PyLong_AsLong (obj);
  Py_DECREF (obj);

  if (value < 1 || value > NBDKIT_API_VERSION) {
    nbdkit_error ("%s: API_VERSION requested unknown version: %ld.  "
                  "This plugin supports API versions between 1 and %d.",
                  script, value, NBDKIT_API_VERSION);
    return -1;
  }

  nbdkit_debug ("module requested API_VERSION %ld", value);
  return (int) value;
}

static int
py_config (const char *key, const char *value)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  int fd;
  FILE *fp;
  PyObject *modname;
  PyObject *fn;
  PyObject *r;

  if (!script) {
    /* The first parameter MUST be "script". */
    if (strcmp (key, "script") != 0) {
      nbdkit_error ("the first parameter must be "
                    "script=/path/to/python/script.py");
      return -1;
    }
    script = value;

    /* Load the Python script. Mark the file CLOEXEC, in case the act
     * of loading the script invokes code that in turn fork()s.
     * However, we can't rely on fopen("re"), so do it by hand.  This
     * does not have to be atomic, because there are no threads during
     * .config before the python interpreter is running, but it's
     * easier to use open/fdopen than fopen/fcntl(fileno).
     */
    fd = open (script, O_CLOEXEC | O_RDONLY);
    if (fd == -1) {
      nbdkit_error ("%s: cannot open file: %m", script);
      return -1;
    }
    fp = fdopen (fd, "r");
    if (!fp) {
      nbdkit_error ("%s: cannot open file: %m", script);
      close (fd);
      return -1;
    }

    if (PyRun_SimpleFileEx (fp, script, 1) == -1) {
      nbdkit_error ("%s: error running this script", script);
      return -1;
    }
    /* Note that because closeit flag == 1, fp is now closed. */

    /* The script should define a module called __main__. */
    modname = PyUnicode_FromString ("__main__");
    module = PyImport_Import (modname);
    Py_DECREF (modname);
    if (!module) {
      nbdkit_error ("%s: cannot find __main__ module", script);
      return -1;
    }

    /* Minimal set of callbacks which are required (by nbdkit itself). */
    if (!callback_defined ("open", NULL) ||
        !callback_defined ("get_size", NULL) ||
        !callback_defined ("pread", NULL)) {
      nbdkit_error ("%s: one of the required callbacks "
                    "'open', 'get_size' or 'pread' "
                    "is not defined by this Python script.  "
                    "nbdkit requires these callbacks.", script);
      return -1;
    }

    /* Get the API version. */
    py_api_version = get_py_api_version ();
    if (py_api_version == -1)
      return -1;
  }
  else if (callback_defined ("config", &fn)) {
    /* Other parameters are passed to the Python .config callback. */
    PyErr_Clear ();

    r = PyObject_CallFunction (fn, "ss", key, value);
    Py_DECREF (fn);
    if (check_python_failure ("config") == -1)
      return -1;
    Py_DECREF (r);
  }
  else {
    /* Emulate what core nbdkit does if a config callback is NULL. */
    nbdkit_error ("%s: this plugin does not need command line configuration",
                  script);
    return -1;
  }

  return 0;
}

static int
py_config_complete (void)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;

  if (!script) {
    nbdkit_error ("the first parameter must be "
                  "script=/path/to/python/script.py");
    return -1;
  }
  else if (callback_defined ("config_complete", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallObject (fn, NULL);
    Py_DECREF (fn);
    if (check_python_failure ("config_complete") == -1)
      return -1;
    Py_DECREF (r);
  }

  return 0;
}

static int
py_thread_model (void)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;
  int ret = NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS;

  if (script && callback_defined ("thread_model", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallObject (fn, NULL);
    Py_DECREF (fn);
    if (check_python_failure ("thread_model") == -1)
      return -1;
    ret = PyLong_AsLong (r);
    Py_DECREF (r);
  }

  return ret;
}

static int
py_get_ready (void)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("get_ready", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallObject (fn, NULL);
    Py_DECREF (fn);
    if (check_python_failure ("get_ready") == -1)
      return -1;
    Py_DECREF (r);
  }

  return 0;
}

static int
py_after_fork (void)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("after_fork", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallObject (fn, NULL);
    Py_DECREF (fn);
    if (check_python_failure ("after_fork") == -1)
      return -1;
    Py_DECREF (r);
  }

  return 0;
}

static void
py_cleanup (void)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("cleanup", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallObject (fn, NULL);
    Py_DECREF (fn);
    if (check_python_failure ("cleanup") == -1)
      return;
    Py_DECREF (r);
  }
}

static int
py_list_exports (int readonly, int is_tls, struct nbdkit_exports *exports)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;
  PyObject *iter, *t;

  if (!callback_defined ("list_exports", &fn))
    /* Do the same as the core server */
    return nbdkit_use_default_export (exports);

  PyErr_Clear ();

  r = PyObject_CallFunctionObjArgs (fn,
                                    readonly ? Py_True : Py_False,
                                    is_tls ? Py_True : Py_False,
                                    NULL);
  Py_DECREF (fn);
  if (check_python_failure ("list_exports") == -1)
    return -1;

  iter = PyObject_GetIter (r);
  if (iter == NULL) {
    nbdkit_error ("list_exports method did not return "
                  "something which is iterable");
    Py_DECREF (r);
    return -1;
  }

  while ((t = PyIter_Next (iter)) != NULL) {
    PyObject *py_name, *py_desc;
    CLEANUP_FREE char *name = NULL;
    CLEANUP_FREE char *desc = NULL;

    name = python_to_string (t);
    if (!name) {
      if (!PyTuple_Check (t) || PyTuple_Size (t) != 2) {
        nbdkit_error ("list_exports method did not return an iterable of "
                      "2-tuples");
        Py_DECREF (iter);
        Py_DECREF (r);
        Py_DECREF (t);
        return -1;
      }
      py_name = PyTuple_GetItem (t, 0);
      py_desc = PyTuple_GetItem (t, 1);
      name = python_to_string (py_name);
      desc = python_to_string (py_desc);
      Py_DECREF (t);
      if (name == NULL || desc == NULL) {
        nbdkit_error ("list_exports method did not return an iterable of "
                      "string 2-tuples");
        Py_DECREF (iter);
        Py_DECREF (r);
        return -1;
      }
    }
    if (nbdkit_add_export (exports, name, desc) == -1) {
      Py_DECREF (iter);
      Py_DECREF (r);
      return -1;
    }
  }

  Py_DECREF (iter);
  Py_DECREF (r);
  return 0;
}

static const char *
py_default_export (int readonly, int is_tls)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;
  CLEANUP_FREE char *name = NULL;

  if (!callback_defined ("default_export", &fn))
    return "";

  PyErr_Clear ();

  r = PyObject_CallFunctionObjArgs (fn,
                                    readonly ? Py_True : Py_False,
                                    is_tls ? Py_True : Py_False,
                                    NULL);
  Py_DECREF (fn);
  if (check_python_failure ("default_export") == -1)
    return NULL;

  name = python_to_string (r);
  Py_DECREF (r);
  if (!name) {
    nbdkit_error ("default_export method did not return a string");
    return NULL;
  }

  return nbdkit_strdup_intern (name);
}

static int
py_preconnect (int readonly)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("preconnect", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallFunctionObjArgs (fn, readonly ? Py_True : Py_False,
                                      NULL);
    Py_DECREF (fn);
    if (check_python_failure ("preconnect") == -1)
      return -1;
    Py_DECREF (r);
  }

  return 0;
}

struct handle {
  int can_zero;
  PyObject *py_h;
};

static void *
py_open (int readonly)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  PyObject *fn;
  struct handle *h;

  if (!callback_defined ("open", &fn)) {
    nbdkit_error ("%s: missing callback: %s", script, "open");
    return NULL;
  }

  h = malloc (sizeof *h);
  if (h == NULL) {
    nbdkit_error ("malloc: %m");
    return NULL;
  }
  h->can_zero = -1;

  PyErr_Clear ();

  h->py_h = PyObject_CallFunctionObjArgs (fn, readonly ? Py_True : Py_False,
                                          NULL);
  Py_DECREF (fn);
  if (check_python_failure ("open") == -1) {
    free (h);
    return NULL;
  }

  assert (h->py_h);
  return h;
}

static void
py_close (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("close", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallFunctionObjArgs (fn, h->py_h, NULL);
    Py_DECREF (fn);
    check_python_failure ("close");
    Py_XDECREF (r);
  }

  Py_DECREF (h->py_h);
  free (h);
}

static const char *
py_export_description (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;
  CLEANUP_FREE char *desc = NULL;

  if (!callback_defined ("export_description", &fn))
    return NULL;

  PyErr_Clear ();

  r = PyObject_CallFunctionObjArgs (fn, h->py_h, NULL);
  Py_DECREF (fn);
  if (check_python_failure ("export_description") == -1)
    return NULL;

  desc = python_to_string (r);
  Py_DECREF (r);
  if (!desc) {
    nbdkit_error ("export_description method did not return a string");
    return NULL;
  }

  return nbdkit_strdup_intern (desc);
}

static int64_t
py_get_size (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;
  int64_t ret;

  if (!callback_defined ("get_size", &fn)) {
    nbdkit_error ("%s: missing callback: %s", script, "get_size");
    return -1;
  }

  PyErr_Clear ();

  r = PyObject_CallFunctionObjArgs (fn, h->py_h, NULL);
  Py_DECREF (fn);
  if (check_python_failure ("get_size") == -1)
    return -1;

  ret = PyLong_AsLongLong (r);
  Py_DECREF (r);
  if (check_python_failure ("PyLong_AsLongLong") == -1)
    return -1;

  return ret;
}

static int
py_block_size (void *handle,
               uint32_t *minimum, uint32_t *preferred, uint32_t *maximum)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;
  unsigned u_minimum, u_preferred, u_maximum;

  if (callback_defined ("block_size", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallFunctionObjArgs (fn, h->py_h, NULL);
    Py_DECREF (fn);
    if (check_python_failure ("block_size") == -1)
      return -1;

    PyArg_ParseTuple (r, "III",
                      &u_minimum, &u_preferred, &u_maximum);
    Py_DECREF (r);
    if (check_python_failure ("block_size: PyArg_ParseTuple") == -1)
      return -1;

    *minimum = u_minimum;
    *preferred = u_preferred;
    *maximum = u_maximum;
    return 0;
  }
  else {
    *minimum = *preferred = *maximum = 0;
    return 0;
  }
}

static int
py_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
          uint32_t flags)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;
  Py_buffer view = {0};
  int ret = -1;

  if (!callback_defined ("pread", &fn)) {
    nbdkit_error ("%s: missing callback: %s", script, "pread");
    return ret;
  }

  PyErr_Clear ();

  switch (py_api_version) {
  case 1:
    r = PyObject_CallFunction (fn, "OIL", h->py_h, count, offset);
    break;
  case 2:
    r = PyObject_CallFunction (fn, "ONLI", h->py_h,
          PyMemoryView_FromMemory ((char *)buf, count, PyBUF_WRITE),
          offset, flags);
    break;
  default: abort ();
  }
  Py_DECREF (fn);
  if (check_python_failure ("pread") == -1)
    return ret;

  if (py_api_version == 1) {
    /* In API v1 the Python pread function had to return a buffer
     * protocol compatible function.  In API v2+ it writes directly to
     * the C buffer so this code is not used.
     */
    if (PyObject_GetBuffer (r, &view, PyBUF_SIMPLE) == -1) {
      nbdkit_error ("%s: value returned from pread does not support the "
                    "buffer protocol",
                    script);
      goto out;
    }

    if (view.len < count) {
      nbdkit_error ("%s: buffer returned from pread is too small", script);
      goto out;
    }

    memcpy (buf, view.buf, count);
  }
  ret = 0;

out:
  if (view.obj)
    PyBuffer_Release (&view);

  Py_DECREF (r);

  return ret;
}

static int
py_pwrite (void *handle, const void *buf, uint32_t count, uint64_t offset,
           uint32_t flags)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("pwrite", &fn)) {
    PyErr_Clear ();

    switch (py_api_version) {
    case 1:
      r = PyObject_CallFunction (fn, "ONL", h->py_h,
            PyMemoryView_FromMemory ((char *)buf, count, PyBUF_READ),
            offset);
      break;
    case 2:
      r = PyObject_CallFunction (fn, "ONLI", h->py_h,
            PyMemoryView_FromMemory ((char *)buf, count, PyBUF_READ),
            offset, flags);
      break;
    default: abort ();
    }
    Py_DECREF (fn);
    if (check_python_failure ("pwrite") == -1)
      return -1;
    Py_DECREF (r);
  }
  else {
    nbdkit_error ("%s not implemented", "pwrite");
    return -1;
  }

  return 0;
}

static int
py_flush (void *handle, uint32_t flags)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("flush", &fn)) {
    PyErr_Clear ();

    switch (py_api_version) {
    case 1:
      r = PyObject_CallFunctionObjArgs (fn, h->py_h, NULL);
      break;
    case 2:
      r = PyObject_CallFunction (fn, "OI", h->py_h, flags);
      break;
    default: abort ();
    }
    Py_DECREF (fn);
    if (check_python_failure ("flush") == -1)
      return -1;
    Py_DECREF (r);
  }
  else {
    nbdkit_error ("%s not implemented", "flush");
    return -1;
  }

  return 0;
}

static int
py_trim (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("trim", &fn)) {
    PyErr_Clear ();

    switch (py_api_version) {
    case 1:
      r = PyObject_CallFunction (fn, "OIL", h->py_h, count, offset);
      break;
    case 2:
      r = PyObject_CallFunction (fn, "OILI", h->py_h, count, offset, flags);
      break;
    default: abort ();
    }
    Py_DECREF (fn);
    if (check_python_failure ("trim") == -1)
      return -1;
    Py_DECREF (r);
  }
  else {
    nbdkit_error ("%s not implemented", "trim");
    return -1;
  }

  return 0;
}

static int
py_zero (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("zero", &fn)) {
    PyErr_Clear ();

    last_error = 0;
    switch (py_api_version) {
    case 1: {
      int may_trim = flags & NBDKIT_FLAG_MAY_TRIM;
      r = PyObject_CallFunction (fn, "OILO",
                                 h->py_h, count, offset,
                                 may_trim ? Py_True : Py_False);
      break;
    }
    case 2:
      r = PyObject_CallFunction (fn, "OILI", h->py_h, count, offset, flags);
      break;
    default: abort ();
    }
    Py_DECREF (fn);
    if (last_error == EOPNOTSUPP || last_error == ENOTSUP) {
      /* When user requests this particular error, we want to
       * gracefully fall back, and to accommodate both a normal return
       * and an exception.
       */
      nbdkit_debug ("zero requested falling back to pwrite");
      Py_XDECREF (r);
      PyErr_Clear ();
      return -1;
    }
    if (check_python_failure ("zero") == -1)
      return -1;
    Py_DECREF (r);
    return 0;
  }

  nbdkit_debug ("zero missing, falling back to pwrite");
  nbdkit_set_error (EOPNOTSUPP);
  return -1;
}

static int
py_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;

  if (callback_defined ("cache", &fn)) {
    PyErr_Clear ();

    switch (py_api_version) {
    case 1:
    case 2:
      r = PyObject_CallFunction (fn, "OILI",
                                 h->py_h, count, offset, flags, NULL);
      break;
    default: abort ();
    }
    Py_DECREF (fn);
    if (check_python_failure ("cache") == -1)
      return -1;
    Py_DECREF (r);
  }
  else {
    nbdkit_error ("%s not implemented", "cache");
    return -1;
  }

  return 0;
}

static int
boolean_callback (void *handle, const char *can_fn, const char *plain_fn)
{
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;
  int ret;

  if (callback_defined (can_fn, &fn)) {
    PyErr_Clear ();

    r = PyObject_CallFunctionObjArgs (fn, h->py_h, NULL);
    Py_DECREF (fn);
    if (check_python_failure (can_fn) == -1)
      return -1;
    ret = r == Py_True;
    Py_DECREF (r);
    return ret;
  }
  /* No Python ‘can_fn’ (eg. ‘can_write’), but if there's a Python
   * ‘plain_fn’ (eg. ‘pwrite’) callback defined, return 1.  (In C
   * modules, nbdkit would do this).
   */
  else if (plain_fn && callback_defined (plain_fn, NULL))
    return 1;
  else
    return 0;
}

static int
py_is_rotational (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  return boolean_callback (handle, "is_rotational", NULL);
}

static int
py_can_multi_conn (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  return boolean_callback (handle, "can_multi_conn", NULL);
}

static int
py_can_write (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  return boolean_callback (handle, "can_write", "pwrite");
}

static int
py_can_flush (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  return boolean_callback (handle, "can_flush", "flush");
}

static int
py_can_trim (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  return boolean_callback (handle, "can_trim", "trim");
}

static int
py_can_zero (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;

  if (h->can_zero >= 0)
    return h->can_zero;
  return h->can_zero = boolean_callback (handle, "can_zero", "zero");
}

static int
py_can_fast_zero (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  int r;

  if (callback_defined ("can_fast_zero", NULL))
    return boolean_callback (handle, "can_fast_zero", NULL);

  /* No Python ‘can_fast_zero’, but we advertise fast fail support when
   * 'can_zero' is false.  (In C modules, nbdkit would do this).
   */
  r = py_can_zero (handle);
  if (r == -1)
    return -1;
  return !r;
}

static int
py_can_fua (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;
  int ret;

  if (callback_defined ("can_fua", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallFunctionObjArgs (fn, h->py_h, NULL);
    Py_DECREF (fn);
    if (check_python_failure ("can_fua") == -1)
      return -1;
    ret = PyLong_AsLong (r);
    Py_DECREF (r);
    return ret;
  }
  /* No Python ‘can_fua’, but check if there's a Python ‘flush’
   * callback defined.  (In C modules, nbdkit would do this).
   */
  else if (callback_defined ("flush", NULL))
    return NBDKIT_FUA_EMULATE;
  else
    return NBDKIT_FUA_NONE;
}

static int
py_can_cache (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;
  int ret;

  if (callback_defined ("can_cache", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallFunctionObjArgs (fn, h->py_h, NULL);
    Py_DECREF (fn);
    if (check_python_failure ("can_cache") == -1)
      return -1;
    ret = PyLong_AsLong (r);
    Py_DECREF (r);
    return ret;
  }
  /* No Python ‘can_cache’, but check if there's a Python ‘cache’
   * callback defined.  (In C modules, nbdkit would do this).
   */
  else if (callback_defined ("cache", NULL))
    return NBDKIT_CACHE_NATIVE;
  else
    return NBDKIT_CACHE_NONE;
}

static int
py_can_extents (void *handle)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  return boolean_callback (handle, "can_extents", "extents");
}

static int
py_extents (void *handle, uint32_t count, uint64_t offset,
            uint32_t flags, struct nbdkit_extents *extents)
{
  ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
  struct handle *h = handle;
  PyObject *fn;
  PyObject *r;
  PyObject *iter, *t;
  size_t size;

  if (callback_defined ("extents", &fn)) {
    PyErr_Clear ();

    r = PyObject_CallFunction (fn, "OILI", h->py_h, count, offset, flags);
    Py_DECREF (fn);
    if (check_python_failure ("extents") == -1)
      return -1;

    iter = PyObject_GetIter (r);
    if (iter == NULL) {
      nbdkit_error ("extents method did not return "
                    "something which is iterable");
      Py_DECREF (r);
      return -1;
    }

    size = 0;
    while ((t = PyIter_Next (iter)) != NULL) {
      PyObject *py_offset, *py_length, *py_type;
      uint64_t extent_offset, extent_length;
      uint32_t extent_type;

      size++;

      if (!PyTuple_Check (t) || PyTuple_Size (t) != 3) {
        nbdkit_error ("extents method did not return an iterable of 3-tuples");
        Py_DECREF (iter);
        Py_DECREF (r);
        Py_DECREF (t);
        return -1;
      }
      py_offset = PyTuple_GetItem (t, 0);
      py_length = PyTuple_GetItem (t, 1);
      py_type = PyTuple_GetItem (t, 2);
      extent_offset = PyLong_AsUnsignedLongLong (py_offset);
      extent_length = PyLong_AsUnsignedLongLong (py_length);
      extent_type = PyLong_AsUnsignedLong (py_type);
      Py_DECREF (t);
      if (check_python_failure ("PyLong") == -1) {
        Py_DECREF (iter);
        Py_DECREF (r);
        return -1;
      }
      if (nbdkit_add_extent (extents,
                             extent_offset, extent_length, extent_type) == -1) {
        Py_DECREF (iter);
        Py_DECREF (r);
        return -1;
      }
    }

    if (size < 1) {
      nbdkit_error ("extents method cannot return an empty list");
      Py_DECREF (iter);
      Py_DECREF (r);
      return -1;
    }

    Py_DECREF (iter);
    Py_DECREF (r);
  }
  else {
    /* Do the same as the core server: synthesize a fully allocated
     * extent covering the whole range.
     */
    if (nbdkit_add_extent (extents,
                           offset, count, 0 /* allocated data */) == -1)
      return -1;
  }

  return 0;
}

#define py_config_help \
  "script=<FILENAME>     (required) The Python plugin to run.\n" \
  "[other arguments may be used by the plugin that you load]"

/* This is the maximum possible, but the default for plugins is
 * NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS.  Plugins can override
 * that by providing a thread_model() function.
 */
#define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL

static struct nbdkit_plugin plugin = {
  .name               = "python",
  .version            = PACKAGE_VERSION,

  .load               = py_load,
  .unload             = py_unload,
  .dump_plugin        = py_dump_plugin,

  .config             = py_config,
  .config_complete    = py_config_complete,
  .config_help        = py_config_help,

  .thread_model       = py_thread_model,
  .get_ready          = py_get_ready,
  .after_fork         = py_after_fork,
  .cleanup            = py_cleanup,
  .list_exports       = py_list_exports,
  .default_export     = py_default_export,

  .preconnect         = py_preconnect,
  .open               = py_open,
  .close              = py_close,

  .export_description = py_export_description,
  .get_size           = py_get_size,
  .block_size         = py_block_size,
  .is_rotational      = py_is_rotational,
  .can_multi_conn     = py_can_multi_conn,
  .can_write          = py_can_write,
  .can_flush          = py_can_flush,
  .can_trim           = py_can_trim,
  .can_zero           = py_can_zero,
  .can_fast_zero      = py_can_fast_zero,
  .can_fua            = py_can_fua,
  .can_cache          = py_can_cache,
  .can_extents        = py_can_extents,

  .pread              = py_pread,
  .pwrite             = py_pwrite,
  .flush              = py_flush,
  .trim               = py_trim,
  .zero               = py_zero,
  .cache              = py_cache,
  .extents            = py_extents,
};

NBDKIT_REGISTER_PLUGIN (plugin)