3 nbdkit-plugin - how to write nbdkit plugins
7 #define NBDKIT_API_VERSION 2
9 #include <nbdkit-plugin.h>
11 #define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
16 /* create a handle ... */
20 static struct nbdkit_plugin plugin = {
22 .open = myplugin_open,
23 .get_size = myplugin_get_size,
24 .pread = myplugin_pread,
25 .pwrite = myplugin_pwrite,
29 NBDKIT_REGISTER_PLUGIN(plugin)
31 When this has been compiled to a shared library, do:
33 nbdkit [--args ...] ./myplugin.so [key=value ...]
35 When debugging, use the I<-fv> options:
37 nbdkit -fv ./myplugin.so [key=value ...]
41 An nbdkit plugin is a new source device which can be served using the
42 Network Block Device (NBD) protocol. This manual page describes how
43 to create an nbdkit plugin in C.
45 To see example plugins:
46 L<https://github.com/libguestfs/nbdkit/tree/master/plugins>
48 Plugins written in C have an ABI guarantee: a plugin compiled against
49 an older version of nbdkit will still work correctly when loaded with
50 a newer nbdkit. We also try (but cannot guarantee) to support plugins
51 compiled against a newer version of nbdkit when loaded with an older
52 nbdkit, although the plugin may have reduced functionality if it
53 depends on features only provided by newer nbdkit.
55 For plugins written in C, we also provide an API guarantee: a plugin
56 written against an older header will still compile unmodified with a
59 The API guarantee does not always apply to plugins written in other
60 (non-C) languages which may have to adapt to changes when recompiled
61 against a newer nbdkit.
63 To write plugins in other languages, see:
64 __LANG_PLUGIN_LINKS__.
66 =head1 C<#define NBDKIT_API_VERSION 2>
68 Plugins must choose which API version they want to use, by defining
69 NBDKIT_API_VERSION to a positive integer prior to including
70 C<nbdkit-plugin.h> (or any other nbdkit header). The default version
71 is 1 for backwards-compatibility with nbdkit v1.1.26 and earlier;
72 however, it is recommended that new plugins be written to the maximum
73 version (currently 2) as it enables more features and better
74 interaction with nbdkit filters. Therefore, the rest of this document
75 only covers the version 2 interface. A newer nbdkit will always
76 support plugins written in C which were compiled against any prior API
79 =head1 C<#include E<lt>nbdkit-plugin.hE<gt>>
81 All plugins should start by including this header file, after
82 optionally choosing an API version.
84 =head1 C<#define THREAD_MODEL>
86 All plugins must define a thread model. See L</THREADS> below for
87 details. It is generally safe to use:
89 #define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
91 =head1 C<struct nbdkit_plugin>
93 All plugins must define and register one C<struct nbdkit_plugin>,
94 which contains the name of the plugin and pointers to callback
97 static struct nbdkit_plugin plugin = {
99 .longname = "My Plugin",
100 .description = "This is my great plugin for nbdkit",
101 .open = myplugin_open,
102 .get_size = myplugin_get_size,
103 .pread = myplugin_pread,
104 .pwrite = myplugin_pwrite,
108 NBDKIT_REGISTER_PLUGIN(plugin)
110 The C<.name> field is the name of the plugin.
112 The callbacks are described below (see L</CALLBACKS>). Only C<.name>,
113 C<.open>, C<.get_size> and C<.pread> are required. All other
114 callbacks can be omitted. However almost all plugins should have a
115 C<.close> callback. Most real-world plugins will also want to declare
116 some of the other callbacks.
118 The nbdkit server calls the callbacks in the following order over the
119 lifetime of the plugin:
125 is called once just after the plugin is loaded into memory.
127 =item C<.config> and C<.config_complete>
129 C<.config> is called zero or more times during command line parsing.
130 C<.config_complete> is called once after all configuration information
131 has been passed to the plugin (but not during C<nbdkit --dump-plugin>).
133 Both are called after loading the plugin but before any connections
136 =item C<.thread_model>
138 In normal operation, C<.thread_model> is called once after
139 C<.config_complete> has validated all configuration information, and
140 before any connections are accepted. However, during C<nbdkit
141 --dump-plugin>, it is called after any C<.config> calls but without
142 C<.config_complete> (so a plugin which determines the results from a
143 script must be prepared for a missing script).
147 Called when a TCP connection has been made to the server. This
148 happens early, before NBD or TLS negotiation.
152 A new client has connected and finished the NBD handshake. TLS
153 negotiation (if required) has been completed successfully.
155 =item C<.can_write>, C<.get_size> and other option negotiation callbacks
157 These are called during option negotiation with the client, but before
158 any data is served. These callbacks may return different values
159 across different C<.open> calls, but within a single connection, they
160 are called at most once and cached by nbdkit for that connection.
162 =item C<.pread>, C<.pwrite> and other data serving callbacks
164 After option negotiation has finished, these may be called to serve
165 data. Depending on the thread model chosen, they might be called in
166 parallel from multiple threads. The data serving callbacks include a
167 flags argument; the results of the negotiation callbacks influence
168 whether particular flags will ever be passed to a data callback.
172 The client has disconnected.
174 =item C<.preconnect>, C<.open> ... C<.close>
176 The sequence C<.preconnect>, C<.open> ... C<.close> can be called
177 repeatedly over the lifetime of the plugin, and can be called in
178 parallel (depending on the thread model).
182 is called once just before the plugin is unloaded from memory.
188 The following flags are defined by nbdkit, and used in various data
189 serving callbacks as follows:
193 =item C<NBDKIT_FLAG_MAY_TRIM>
195 This flag is used by the C<.zero> callback; there is no way to disable
196 this flag, although a plugin that does not support trims as a way to
197 write zeroes may ignore the flag without violating expected semantics.
199 =item C<NBDKIT_FLAG_FUA>
201 This flag represents Forced Unit Access semantics. It is used by the
202 C<.pwrite>, C<.zero>, and C<.trim> callbacks to indicate that the
203 plugin must not return a result until the action has landed in
204 persistent storage. This flag will not be sent to the plugin unless
205 C<.can_fua> is provided and returns C<NBDKIT_FUA_NATIVE>.
209 The following defines are valid as successful return values for
214 =item C<NBDKIT_FUA_NONE>
216 Forced Unit Access is not supported; the client must manually request
217 a flush after writes have completed. The C<NBDKIT_FLAG_FUA> flag will
218 not be passed to the plugin's write callbacks.
220 =item C<NBDKIT_FUA_EMULATE>
222 The client may request Forced Unit Access, but it is implemented by
223 emulation, where nbdkit calls C<.flush> after a write operation; this
224 is semantically correct, but may hurt performance as it tends to flush
225 more data than just what the client requested. The C<NBDKIT_FLAG_FUA>
226 flag will not be passed to the plugin's write callbacks.
228 =item C<NBDKIT_FUA_NATIVE>
230 The client may request Forced Unit Access, which results in the
231 C<NBDKIT_FLAG_FUA> flag being passed to the plugin's write callbacks
232 (C<.pwrite>, C<.trim>, and C<.zero>). When the flag is set, these
233 callbacks must not return success until the client's request has
234 landed in persistent storage.
238 The following defines are valid as successful return values for
243 =item C<NBDKIT_CACHE_NONE>
245 The server does not advertise caching support, and rejects any
246 client-requested caching. Any C<.cache> callback is ignored.
248 =item C<NBDKIT_CACHE_EMULATE>
250 The nbdkit server advertises cache support to the client, where the
251 client may request that the server cache a region of the export to
252 potentially speed up future read and/or write operations on that
253 region. The nbdkit server implements the caching by calling C<.pread>
254 and ignoring the results. This option exists to ease the
255 implementation of a common form of caching; any C<.cache> callback is
258 =item C<NBDKIT_CACHE_NATIVE>
260 The nbdkit server advertises cache support to the client, where the
261 client may request that the server cache a region of the export to
262 potentially speed up future read and/or write operations on that
263 region. The nbdkit server calls the C<.cache> callback to perform the
264 caching; if that callback is missing, the client's cache request
265 succeeds without doing anything.
269 =head1 ERROR HANDLING
271 If there is an error in the plugin, the plugin should call
272 C<nbdkit_error> to report an error message; additionally, if the
273 callback is involved in serving data, the plugin should call
274 C<nbdkit_set_error> to influence the error code that will be sent to
275 the client. These two functions can be called in either order. Then,
276 the callback should return the appropriate error indication,
277 eg. C<NULL> or C<-1>.
279 If the call to C<nbdkit_set_error> is omitted while serving data, then
280 the global variable C<errno> may be used. For plugins which have
281 C<.errno_is_preserved != 0> the core code will use C<errno>. In
282 plugins written in non-C languages, we usually cannot trust that
283 C<errno> will not be overwritten when returning from that language to
284 C. In that case, either the plugin must call C<nbdkit_set_error> or
285 hard-coded C<EIO> is used.
287 C<nbdkit_error> has the following prototype and works like
290 void nbdkit_error (const char *fs, ...);
291 void nbdkit_verror (const char *fs, va_list args);
293 For convenience, C<nbdkit_error> preserves the value of C<errno>, and
294 also supports the glibc extension of a single C<%m> in a format string
295 expanding to C<strerror(errno)>, even on platforms that don't support
298 C<nbdkit_set_error> can be called at any time, but only has an impact
299 during callbacks for serving data, and only when the callback returns
300 an indication of failure. It has the following prototype:
302 void nbdkit_set_error (int err);
310 This field (a string) is required, and B<must> contain only ASCII
311 alphanumeric characters and be unique amongst all plugins.
317 Plugins may optionally set a version string which is displayed in help
318 and debugging output.
322 const char *longname;
324 An optional free text name of the plugin. This field is used in error
327 =head2 C<.description>
329 const char *description;
331 An optional multi-line description of the plugin.
337 This is called once just after the plugin is loaded into memory. You
338 can use this to perform any global initialization needed by the
345 This may be called once just before the plugin is unloaded from
346 memory. Note that it's not guaranteed that C<.unload> will always be
347 called (eg. the server might be killed or segfault), so you should try
348 to make the plugin as robust as possible by not requiring cleanup.
349 See also L</SHUTDOWN> below.
351 =head2 C<.dump_plugin>
353 void dump_plugin (void);
355 This optional callback is called when the
356 S<C<nbdkit plugin --dump-plugin>> command is used. It should print
357 any additional informative C<key=value> fields to stdout as needed.
358 Prefixing the keys with the name of the plugin will avoid conflicts.
362 int config (const char *key, const char *value);
364 On the nbdkit command line, after the plugin filename, come an
365 optional list of C<key=value> arguments. These are passed to the
366 plugin through this callback when the plugin is first loaded and
367 before any connections are accepted.
369 This callback may be called zero or more times.
371 Both C<key> and C<value> parameters will be non-NULL. The strings are
372 owned by nbdkit but will remain valid for the lifetime of the plugin,
373 so the plugin does not need to copy them.
375 The key will be a non-empty string beginning with an ASCII alphabetic
376 character (C<A-Z> C<a-z>). The rest of the key must contain only
377 ASCII alphanumeric plus period, underscore or dash characters (C<A-Z>
378 C<a-z> C<0-9> C<.> C<_> C<->). The value may be an arbitrary string,
379 including an empty string.
381 The names of C<key>s accepted by plugins is up to the plugin, but you
382 should probably look at other plugins and follow the same conventions.
384 If the value is a relative path, then note that the server changes
385 directory when it starts up. See L</FILENAMES AND PATHS> above.
387 If the C<.config> callback is not provided by the plugin, and the user
388 tries to specify any C<key=value> arguments, then nbdkit will exit
391 If there is an error, C<.config> should call C<nbdkit_error> with an
392 error message and return C<-1>.
394 =head2 C<.magic_config_key>
396 const char *magic_config_key;
398 This optional string can be used to set a "magic" key used when
399 parsing plugin parameters. It affects how "bare parameters" (those
400 which do not contain an C<=> character) are parsed on the command
403 If C<magic_config_key != NULL> then any bare parameters are passed to
404 the C<.config> method as: S<C<config (magic_config_key, argv[i]);>>.
406 If C<magic_config_key> is not set then we behave as in nbdkit E<lt>
407 1.7: If the first parameter on the command line is bare then it is
408 passed to the C<.config> method as: S<C<config ("script", value);>>.
409 Any other bare parameters give errors.
411 =head2 C<.config_complete>
413 int config_complete (void);
415 This optional callback is called after all the configuration has been
416 passed to the plugin. It is a good place to do checks, for example
417 that the user has passed the required parameters to the plugin.
419 If there is an error, C<.config_complete> should call C<nbdkit_error>
420 with an error message and return C<-1>.
422 =head2 C<.config_help>
424 const char *config_help;
426 This optional multi-line help message should summarize any
427 C<key=value> parameters that it takes. It does I<not> need to repeat
428 what already appears in C<.description>.
430 If the plugin doesn't take any config parameters you should probably
433 =head2 C<.thread_model>
435 int thread_model (void)
437 This optional callback is called after all the configuration has been
438 passed to the plugin. It can be used to force a stricter thread model
439 based on configuration, compared to C<THREAD_MODEL>. See L</THREADS>
440 below for details. Attempts to request a looser (more parallel) model
441 are silently ignored.
443 If there is an error, C<.thread_model> should call C<nbdkit_error>
444 with an error message and return C<-1>.
446 =head2 C<.preconnect>
448 int preconnect (int readonly);
450 This optional callback is called when a TCP connection has been made
451 to the server. This happens early, before NBD or TLS negotiation. If
452 TLS authentication is required to access the server, then it has
453 B<not> been negotiated at this point.
455 For security reasons (to avoid denial of service attacks) this
456 callback should be written to be as fast and take as few resources as
457 possible. If you use this callback, only use it to do basic access
458 control, such as checking C<nbdkit_peer_name> against a whitelist (see
459 L</PEER NAME> and L<nbdkit-ip-filter(1)>). It may be better to do
460 access control outside the server, for example using TCP wrappers or a
463 The C<readonly> flag informs the plugin that the server was started
464 with the I<-r> flag on the command line.
466 Returning C<0> will allow the connection to continue. If there is an
467 error or you want to deny the connection, call C<nbdkit_error> with an
468 error message and return C<-1>.
472 void *open (int readonly);
474 This is called when a new client connects to the nbdkit server. The
475 callback should allocate a handle and return it. This handle
476 is passed back to other callbacks and could be freed in the C<.close>
479 Note that the handle is completely opaque to nbdkit, but it must not
480 be NULL. If you don't need to use a handle, return
481 C<NBDKIT_HANDLE_NOT_NEEDED> which is a static non-NULL pointer.
483 The C<readonly> flag informs the plugin that the server was started
484 with the I<-r> flag on the command line which forces connections to be
485 read-only. Note that the plugin may I<additionally> force the
486 connection to be readonly (even if this flag is false) by returning
487 false from the C<.can_write> callback. So if your plugin can only
488 serve read-only, you can ignore this parameter.
490 This callback is called after the NBD handshake has completed, which
491 includes TLS authentication (if required). If the plugin defines a
492 C<.preconnect> callback, then it must be called and return with
493 success before C<.open> is called.
495 If there is an error, C<.open> should call C<nbdkit_error> with an
496 error message and return C<NULL>.
500 void close (void *handle);
502 This is called when the client closes the connection. It should clean
503 up any per-connection resources.
505 Note there is no way in the NBD protocol to communicate close errors
506 back to the client, for example if your plugin calls L<close(2)> and
507 you are checking for errors (as you should do). Therefore the best
508 you can do is to log the error on the server. Well-behaved NBD
509 clients I<should> try to flush the connection before it is closed and
510 check for errors, but obviously this is outside the scope of nbdkit.
514 int64_t get_size (void *handle);
516 This is called during the option negotiation phase of the protocol
517 to get the size (in bytes) of the block device being exported.
519 The returned size must be E<ge> 0. If there is an error, C<.get_size>
520 should call C<nbdkit_error> with an error message and return C<-1>.
524 int can_write (void *handle);
526 This is called during the option negotiation phase to find out if the
527 handle supports writes.
529 If there is an error, C<.can_write> should call C<nbdkit_error> with
530 an error message and return C<-1>.
532 This callback is not required. If omitted, then we return true iff a
533 C<.pwrite> callback has been defined.
537 int can_flush (void *handle);
539 This is called during the option negotiation phase to find out if the
540 handle supports the flush-to-disk operation.
542 If there is an error, C<.can_flush> should call C<nbdkit_error> with
543 an error message and return C<-1>.
545 This callback is not required. If omitted, then we return true iff a
546 C<.flush> callback has been defined.
548 =head2 C<.is_rotational>
550 int is_rotational (void *handle);
552 This is called during the option negotiation phase to find out if the
553 backing disk is a rotational medium (like a traditional hard disk) or
554 not (like an SSD). If true, this may cause the client to reorder
555 requests to make them more efficient for a slow rotating disk.
557 If there is an error, C<.is_rotational> should call C<nbdkit_error>
558 with an error message and return C<-1>.
560 This callback is not required. If omitted, then we return false.
564 int can_trim (void *handle);
566 This is called during the option negotiation phase to find out if the
567 plugin supports the trim/discard operation for punching holes in the
570 If there is an error, C<.can_trim> should call C<nbdkit_error> with an
571 error message and return C<-1>.
573 This callback is not required. If omitted, then we return true iff a
574 C<.trim> callback has been defined.
578 int can_zero (void *handle);
580 This is called during the option negotiation phase to find out if the
581 plugin wants the C<.zero> callback to be utilized. Support for
582 writing zeroes is still advertised to the client (unless the
583 L<nbdkit-nozero-filter(1)> is also used), so returning false merely
584 serves as a way to avoid complicating the C<.zero> callback to have to
585 fail with C<ENOTSUP> or C<EOPNOTSUPP> on the connections where it will
586 never be more efficient than using C<.pwrite> up front.
588 If there is an error, C<.can_zero> should call C<nbdkit_error> with an
589 error message and return C<-1>.
591 This callback is not required. If omitted, then for a normal zero
592 request, nbdkit always tries C<.zero> first if it is present, and
593 gracefully falls back to C<.pwrite> if C<.zero> was absent or failed
594 with C<ENOTSUP> or C<EOPNOTSUPP>.
596 =head2 C<.can_fast_zero>
598 int can_fast_zero (void *handle);
600 This is called during the option negotiation phase to find out if the
601 plugin wants to advertise support for fast zero requests. If this
602 support is not advertised, a client cannot attempt fast zero requests,
603 and has no way to tell if writing zeroes offers any speedups compared
604 to using C<.pwrite> (other than compressed network traffic). If
605 support is advertised, then C<.zero> will have
606 C<NBDKIT_FLAG_FAST_ZERO> set when the client has requested a fast
607 zero, in which case the plugin must fail with C<ENOTSUP> or
608 C<EOPNOTSUPP> up front if the request would not offer any benefits
609 over C<.pwrite>. Advertising support for fast zero requests does not
610 require that writing zeroes be fast, only that the result (whether
611 success or failure) is fast, so this should be advertised when
614 If there is an error, C<.can_fast_zero> should call C<nbdkit_error>
615 with an error message and return C<-1>.
617 This callback is not required. If omitted, then nbdkit returns true
618 if C<.zero> is absent or C<.can_zero> returns false (in those cases,
619 nbdkit fails all fast zero requests, as its fallback to C<.pwrite> is
620 not inherently faster), otherwise false (since it cannot be determined
621 in advance if the plugin's C<.zero> will properly honor the semantics
622 of C<NBDKIT_FLAG_FAST_ZERO>).
624 =head2 C<.can_extents>
626 int can_extents (void *handle);
628 This is called during the option negotiation phase to find out if the
629 plugin supports detecting allocated (non-sparse) regions of the disk
630 with the C<.extents> callback.
632 If there is an error, C<.can_extents> should call C<nbdkit_error> with
633 an error message and return C<-1>.
635 This callback is not required. If omitted, then we return true iff a
636 C<.extents> callback has been defined.
640 int can_fua (void *handle);
642 This is called during the option negotiation phase to find out if the
643 plugin supports the Forced Unit Access (FUA) flag on write, zero, and
644 trim requests. If this returns C<NBDKIT_FUA_NONE>, FUA support is not
645 advertised to the client; if this returns C<NBDKIT_FUA_EMULATE>, the
646 C<.flush> callback must work (even if C<.can_flush> returns false),
647 and FUA support is emulated by calling C<.flush> after any write
648 operation; if this returns C<NBDKIT_FUA_NATIVE>, then the C<.pwrite>,
649 C<.zero>, and C<.trim> callbacks (if implemented) must handle the flag
650 C<NBDKIT_FLAG_FUA>, by not returning until that action has landed in
653 If there is an error, C<.can_fua> should call C<nbdkit_error> with an
654 error message and return C<-1>.
656 This callback is not required unless a plugin wants to specifically
657 handle FUA requests. If omitted, nbdkit checks whether C<.flush>
658 exists, and behaves as if this function returns C<NBDKIT_FUA_NONE> or
659 C<NBDKIT_FUA_EMULATE> as appropriate.
661 =head2 C<.can_multi_conn>
663 int can_multi_conn (void *handle);
665 This is called during the option negotiation phase to find out if the
666 plugin is prepared to handle multiple connections from a single
667 client. If the plugin sets this to true then a client may try to open
668 multiple connections to the nbdkit server and spread requests across
669 all connections to maximize parallelism. If the plugin sets it to
670 false (which is the default) then well-behaved clients should only
671 open a single connection, although we cannot control what clients do
674 Specifically it means that either the plugin does not cache requests
675 at all. Or if it does cache them then the effects of a C<.flush>
676 request or setting C<NBDKIT_FLAG_FUA> on a request must be visible
677 across all connections to the plugin before the plugin replies to that
680 Properly working clients should send the same export name for each of
683 If you use Linux L<nbd-client(8)> option S<I<-C num>> with
684 S<num E<gt> 1> then Linux checks this flag and will refuse to connect
685 if C<.can_multi_conn> is false.
687 If there is an error, C<.can_multi_conn> should call C<nbdkit_error>
688 with an error message and return C<-1>.
690 This callback is not required. If omitted, then we return false.
694 int can_cache (void *handle);
696 This is called during the option negotiation phase to find out if the
697 plugin supports a cache operation. The nature of the caching is
698 unspecified (including whether there are limits on how much can be
699 cached at once, and whether writes to a cached region have
700 write-through or write-back semantics), but the command exists to let
701 clients issue a hint to the server that they will be accessing that
702 region of the export.
704 If this returns C<NBDKIT_CACHE_NONE>, cache support is not advertised
705 to the client; if this returns C<NBDKIT_CACHE_EMULATE>, caching is
706 emulated by the server calling C<.pread> and ignoring the results; if
707 this returns C<NBDKIT_CACHE_NATIVE>, then the C<.cache> callback will
708 be used. If there is an error, C<.can_cache> should call
709 C<nbdkit_error> with an error message and return C<-1>.
711 This callback is not required. If omitted, then we return
712 C<NBDKIT_CACHE_NONE> if the C<.cache> callback is missing, or
713 C<NBDKIT_CACHE_NATIVE> if it is defined.
715 =head2 C<.init_sparse>
717 int init_sparse (void *handle);
719 This is called during the option negotiation phase to find out if the
720 plugin knows whether the image starts out as sparse (that is, at least
721 one unallocated hole, regardless of what might be read from that hole).
723 This callback is not required. If omitted, then we return C<0>.
727 int init_zero (void *handle);
729 This is called during the option negotiation phase to find out if the
730 plugin knows whether the image starts out reading entirely as zero
731 (regardless of whether the image is sparse). This bit of information
732 is most useful to a client that plans to copy an image from elsewhere
733 over to a just-created file exposed by the server, because the client
734 can decide if it can bypass a potentially lengthy pre-zeroing pass.
736 This callback is not required. If omitted, then we return C<0>.
740 int pread (void *handle, void *buf, uint32_t count, uint64_t offset,
743 During the data serving phase, nbdkit calls this callback to read data
744 from the backing store. C<count> bytes starting at C<offset> in the
745 backing store should be read and copied into C<buf>. nbdkit takes
746 care of all bounds- and sanity-checking, so the plugin does not need
749 The parameter C<flags> exists in case of future NBD protocol
750 extensions; at this time, it will be 0 on input.
752 The callback must read the whole C<count> bytes if it can. The NBD
753 protocol doesn't allow partial reads (instead, these would be errors).
754 If the whole C<count> bytes was read, the callback should return C<0>
755 to indicate there was I<no> error.
757 If there is an error (including a short read which couldn't be
758 recovered from), C<.pread> should call C<nbdkit_error> with an error
759 message, and C<nbdkit_set_error> to record an appropriate error
760 (unless C<errno> is sufficient), then return C<-1>.
764 int pwrite (void *handle, const void *buf, uint32_t count, uint64_t offset,
767 During the data serving phase, nbdkit calls this callback to write
768 data to the backing store. C<count> bytes starting at C<offset> in
769 the backing store should be written using the data in C<buf>. nbdkit
770 takes care of all bounds- and sanity-checking, so the plugin does not
771 need to worry about that.
773 This function will not be called if C<.can_write> returned false. The
774 parameter C<flags> may include C<NBDKIT_FLAG_FUA> on input based on
775 the result of C<.can_fua>.
777 The callback must write the whole C<count> bytes if it can. The NBD
778 protocol doesn't allow partial writes (instead, these would be
779 errors). If the whole C<count> bytes was written successfully, the
780 callback should return C<0> to indicate there was I<no> error.
782 If there is an error (including a short write which couldn't be
783 recovered from), C<.pwrite> should call C<nbdkit_error> with an error
784 message, and C<nbdkit_set_error> to record an appropriate error
785 (unless C<errno> is sufficient), then return C<-1>.
789 int flush (void *handle, uint32_t flags);
791 During the data serving phase, this callback is used to
792 L<fdatasync(2)> the backing store, ie. to ensure it has been
793 completely written to a permanent medium. If that is not possible
794 then you can omit this callback.
796 This function will not be called directly by the client if
797 C<.can_flush> returned false; however, it may still be called by
798 nbdkit if C<.can_fua> returned C<NBDKIT_FUA_EMULATE>. The parameter
799 C<flags> exists in case of future NBD protocol extensions; at this
800 time, it will be 0 on input.
802 If there is an error, C<.flush> should call C<nbdkit_error> with an
803 error message, and C<nbdkit_set_error> to record an appropriate error
804 (unless C<errno> is sufficient), then return C<-1>.
808 int trim (void *handle, uint32_t count, uint64_t offset, uint32_t flags);
810 During the data serving phase, this callback is used to "punch holes"
811 in the backing store. If that is not possible then you can omit this
814 This function will not be called if C<.can_trim> returned false. The
815 parameter C<flags> may include C<NBDKIT_FLAG_FUA> on input based on
816 the result of C<.can_fua>.
818 If there is an error, C<.trim> should call C<nbdkit_error> with an
819 error message, and C<nbdkit_set_error> to record an appropriate error
820 (unless C<errno> is sufficient), then return C<-1>.
824 int zero (void *handle, uint32_t count, uint64_t offset, uint32_t flags);
826 During the data serving phase, this callback is used to write C<count>
827 bytes of zeroes at C<offset> in the backing store.
829 This function will not be called if C<.can_zero> returned false. On
830 input, the parameter C<flags> may include C<NBDKIT_FLAG_MAY_TRIM>
831 unconditionally, C<NBDKIT_FLAG_FUA> based on the result of
832 C<.can_fua>, and C<NBDKIT_FLAG_FAST_ZERO> based on the result of
835 If C<NBDKIT_FLAG_MAY_TRIM> is requested, the operation can punch a
836 hole instead of writing actual zero bytes, but only if subsequent
837 reads from the hole read as zeroes.
839 If C<NBDKIT_FLAG_FAST_ZERO> is requested, the plugin must decide up
840 front if the implementation is likely to be faster than a
841 corresponding C<.pwrite>; if not, then it must immediately fail with
842 C<ENOTSUP> or C<EOPNOTSUPP> (whether by C<nbdkit_set_error> or
843 C<errno>) and preferably without modifying the exported image. It is
844 acceptable to always fail a fast zero request (as a fast failure is
845 better than attempting the write only to find out after the fact that
846 it was not fast after all). Note that on Linux, support for
847 C<ioctl(BLKZEROOUT)> is insufficient for determining whether a zero
848 request to a block device will be fast (because the kernel will
849 perform a slow fallback when needed).
851 The callback must write the whole C<count> bytes if it can. The NBD
852 protocol doesn't allow partial writes (instead, these would be
853 errors). If the whole C<count> bytes was written successfully, the
854 callback should return C<0> to indicate there was I<no> error.
856 If there is an error, C<.zero> should call C<nbdkit_error> with an
857 error message, and C<nbdkit_set_error> to record an appropriate error
858 (unless C<errno> is sufficient), then return C<-1>.
860 If this callback is omitted, or if it fails with C<ENOTSUP> or
861 C<EOPNOTSUPP> (whether by C<nbdkit_set_error> or C<errno>), then
862 C<.pwrite> will be used as an automatic fallback except when the
863 client requested a fast zero.
867 int extents (void *handle, uint32_t count, uint64_t offset,
868 uint32_t flags, struct nbdkit_extents *extents);
870 During the data serving phase, this callback is used to detect
871 allocated, sparse and zeroed regions of the disk.
873 This function will not be called if C<.can_extents> returned false.
874 nbdkit's default behaviour in this case is to treat the whole virtual
875 disk as if it was allocated. Also, this function will not be called
876 by a client that does not request structured replies (the I<--no-sr>
877 option of nbdkit can be used to test behavior when C<.extents> is
878 unavailable to the client).
880 The callback should detect and return the list of extents overlapping
881 the range C<[offset...offset+count-1]>. The C<extents> parameter
882 points to an opaque object which the callback should fill in by
883 calling C<nbdkit_add_extent>. See L</Extents list> below.
885 If there is an error, C<.extents> should call C<nbdkit_error> with an
886 error message, and C<nbdkit_set_error> to record an appropriate error
887 (unless C<errno> is sufficient), then return C<-1>.
891 The plugin C<extents> callback is passed an opaque pointer C<struct
892 nbdkit_extents *extents>. This structure represents a list of
893 L<filesystem extents|https://en.wikipedia.org/wiki/Extent_(file_systems)>
894 describing which areas of the disk are allocated, which are sparse
895 (“holes”), and, if supported, which are zeroes.
897 The C<extents> callback should scan the disk starting at C<offset> and
898 call C<nbdkit_add_extent> for each extent found.
900 Extents overlapping the range C<[offset...offset+count-1]> should be
901 returned if possible. However nbdkit ignores extents E<lt> offset so
902 the plugin may, if it is easier to implement, return all extent
903 information for the whole disk. The plugin may return extents beyond
904 the end of the range. It may also return extent information for less
905 than the whole range, but it must return at least one extent
906 overlapping C<offset>.
908 The extents B<must> be added in ascending order, and B<must> be
911 The C<flags> parameter of the C<.extents> callback may contain the
912 flag C<NBDKIT_FLAG_REQ_ONE>. This means that the client is only
913 requesting information about the extent overlapping C<offset>. The
914 plugin may ignore this flag, or as an optimization it may return just
915 a single extent for C<offset>.
917 int nbdkit_add_extent (struct nbdkit_extents *extents,
918 uint64_t offset, uint64_t length, uint32_t type);
920 Add an extent covering C<[offset...offset+length-1]> of one of
921 the following four types:
927 A normal, allocated data extent.
929 =item C<type = NBDKIT_EXTENT_HOLE|NBDKIT_EXTENT_ZERO>
931 An unallocated extent, a.k.a. a “hole”, which reads back as zeroes.
932 This is the normal type of hole applicable to most disks.
934 =item C<type = NBDKIT_EXTENT_ZERO>
936 An allocated extent which is known to contain only zeroes.
938 =item C<type = NBDKIT_EXTENT_HOLE>
940 An unallocated extent (hole) which does not read back as zeroes. Note
941 this should only be used in specialized circumstances such as when
942 writing a plugin for (or to emulate) certain SCSI drives which do not
943 guarantee that trimmed blocks read back as zeroes.
947 C<nbdkit_add_extent> returns C<0> on success or C<-1> on failure. On
948 failure C<nbdkit_error> and/or C<nbdkit_set_error> has already been
949 called. C<errno> will be set to a suitable value.
953 int cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags);
955 During the data serving phase, this callback is used to give the
956 plugin a hint that the client intends to make further accesses to the
957 given region of the export. The nature of caching is not specified
958 further by the NBD specification (for example, a server may place
959 limits on how much may be cached at once, and there is no way to
960 control if writes to a cached area have write-through or write-back
961 semantics). In fact, the cache command can always fail and still be
962 compliant, and success might not guarantee a performance gain. If
963 this callback is omitted, then the results of C<.can_cache> determine
964 whether nbdkit will reject cache requests, treat them as instant
965 success, or emulate caching by calling C<.pread> over the same region
966 and ignoring the results.
968 This function will not be called if C<.can_cache> did not return
969 C<NBDKIT_CACHE_NATIVE>. The parameter C<flags> exists in case of
970 future NBD protocol extensions; at this time, it will be 0 on input. A
971 plugin must fail this function if C<flags> includes an unrecognized
972 flag, as that may indicate a requirement that the plugin comply must
973 with a specific caching semantic.
975 If there is an error, C<.cache> should call C<nbdkit_error> with an
976 error message, and C<nbdkit_set_error> to record an appropriate error
977 (unless C<errno> is sufficient), then return C<-1>.
981 The plugin struct also contains an integer field used as a
982 boolean in C code, but unlikely to be exposed in other language
987 =item C<.errno_is_preserved>
989 This defaults to 0; if non-zero, nbdkit can reliably use the value of
990 C<errno> when a callback reports failure, rather than the plugin
991 having to call C<nbdkit_set_error>.
997 Each nbdkit plugin must declare its maximum thread safety model by
998 defining the C<THREAD_MODEL> macro. (This macro is used by
999 C<NBDKIT_REGISTER_PLUGIN>). Additionally, a plugin may implement the
1000 C<.thread_model> callback, called right after C<.config_complete> to
1001 make a runtime decision on which thread model to use. The nbdkit
1002 server chooses the most restrictive model between the plugin's
1003 C<THREAD_MODEL>, the C<.thread_model> if present, any restrictions
1004 requested by filters, and any limitations imposed by the system (for
1005 example, a system without atomic C<FD_CLOEXEC> will serialize all
1006 requests, so as to avoid nbdkit leaking a new file descriptor from one
1007 thread into a child process created by another thread).
1009 In C<nbdkit --dump-plugin PLUGIN> output, the C<max_thread_model> line
1010 matches the C<THREAD_MODEL> macro, and the C<thread_model> line
1011 matches what the system finally settled on after applying all
1014 The possible settings for C<THREAD_MODEL> are defined below.
1018 =item C<#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS>
1020 Only a single handle can be open at any time, and all requests happen
1023 Note this means only one client can connect to the server at any time.
1024 If a second client tries to connect it will block waiting for the
1025 first client to close the connection.
1027 =item C<#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>
1029 I<This is a safe default for most plugins>.
1031 Multiple handles can be open at the same time, but requests are
1032 serialized so that for the plugin as a whole only one
1033 open/read/write/close (etc) request will be in progress at any time.
1035 This is a useful setting if the library you are using is not
1036 thread-safe. However performance may not be good.
1038 =item C<#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS>
1040 Multiple handles can be open and multiple data requests can happen in
1041 parallel. However only one request will happen per handle at a time
1042 (but requests on different handles might happen concurrently).
1044 =item C<#define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL>
1046 Multiple handles can be open and multiple data requests can happen in
1047 parallel (even on the same handle). The server may reorder replies,
1048 answering a later request before an earlier one.
1050 All the libraries you use must be thread-safe and reentrant, and any
1051 code that creates a file descriptor should atomically set
1052 C<FD_CLOEXEC> if you do not want it accidentally leaked to another
1053 thread's child process. You may also need to provide mutexes for
1054 fields in your connection handle.
1058 If none of the above thread models are suitable, then use
1059 C<NBDKIT_THREAD_MODEL_PARALLEL> and implement your own locking using
1060 C<pthread_mutex_t> etc.
1064 When nbdkit receives certain signals it will shut down (see
1065 L<nbdkit(1)/SIGNALS>). The server will wait for any currently running
1066 plugin callbacks to finish and also call the C<.unload> callback
1067 before unloading the plugin.
1069 Note that it's not guaranteed this can always happen (eg. the server
1070 might be killed by C<SIGKILL> or segfault).
1072 =head1 PARSING COMMAND LINE PARAMETERS
1074 =head2 Parsing numbers
1076 There are several functions for parsing numbers. These all deal
1077 correctly with overflow, out of range and parse errors, and you should
1078 use them instead of unsafe functions like L<sscanf(3)>, L<atoi(3)> and
1081 int nbdkit_parse_int (const char *what, const char *str, int *r);
1082 int nbdkit_parse_unsigned (const char *what,
1083 const char *str, unsigned *r);
1084 int nbdkit_parse_int8_t (const char *what,
1085 const char *str, int8_t *r);
1086 int nbdkit_parse_uint8_t (const char *what,
1087 const char *str, uint8_t *r);
1088 int nbdkit_parse_int16_t (const char *what,
1089 const char *str, int16_t *r);
1090 int nbdkit_parse_uint16_t (const char *what,
1091 const char *str, uint16_t *r);
1092 int nbdkit_parse_int32_t (const char *what,
1093 const char *str, int32_t *r);
1094 int nbdkit_parse_uint32_t (const char *what,
1095 const char *str, uint32_t *r);
1096 int nbdkit_parse_int64_t (const char *what,
1097 const char *str, int64_t *r);
1098 int nbdkit_parse_uint64_t (const char *what,
1099 const char *str, uint64_t *r);
1101 Parse string C<str> into an integer of various types. These functions
1102 parse a decimal, hexadecimal (C<"0x...">) or octal (C<"0...">) number.
1104 On success the functions return C<0> and set C<*r> to the parsed value
1105 (unless C<*r == NULL> in which case the result is discarded). On
1106 error, C<nbdkit_error> is called and the functions return C<-1>. On
1107 error C<*r> is always unchanged.
1109 The C<what> parameter is printed in error messages to provide context.
1110 It should usually be a short descriptive string of what you are trying
1113 if (nbdkit_parse_int ("random seed", argv[1], &seed) == -1)
1116 might print an error:
1118 random seed: could not parse number: "lalala"
1120 =head2 Parsing sizes
1122 Use the C<nbdkit_parse_size> utility function to parse human-readable
1123 size strings such as "100M" into the size in bytes.
1125 int64_t nbdkit_parse_size (const char *str);
1127 C<str> can be a string in a number of common formats. The function
1128 returns the size in bytes. If there was an error, it returns C<-1>.
1130 =head2 Parsing booleans
1132 Use the C<nbdkit_parse_bool> utility function to parse human-readable
1133 strings such as "on" into a boolean value.
1135 int nbdkit_parse_bool (const char *str);
1137 C<str> can be a string containing a case-insensitive form of various
1138 common toggle values. The function returns 0 or 1 if the parse was
1139 successful. If there was an error, it returns C<-1>.
1141 =head2 Reading passwords
1143 The C<nbdkit_read_password> utility function can be used to read
1144 passwords from config parameters:
1146 int nbdkit_read_password (const char *value, char **password);
1150 char *password = NULL;
1153 myplugin_config (const char *key, const char *value)
1156 if (strcmp (key, "password") == 0) {
1158 if (nbdkit_read_password (value, &password) == -1)
1164 The C<password> result string is allocated by malloc, and so you may
1167 This function recognizes several password formats. A password may be
1168 used directly on the command line, eg:
1170 nbdkit myplugin password=mostsecret
1172 But more securely this function can also read a password
1175 nbdkit myplugin password=-
1179 nbdkit myplugin password=+/tmp/secret
1181 or from a file descriptor inherited by nbdkit:
1183 nbdkit myplugin password=-99
1185 (If the password begins with a C<-> or C<+> character then it must be
1188 =head1 FILENAMES AND PATHS
1190 The server usually (not always) changes directory to C</> before it
1191 starts serving connections. This means that any relative paths passed
1192 during configuration will not work when the server is running
1193 (example: S<C<nbdkit plugin.so disk.img>>).
1195 To avoid problems, prepend relative paths with the current directory
1196 before storing them in the handle. Or open files and store the file
1199 =head2 C<nbdkit_absolute_path>
1201 char *nbdkit_absolute_path (const char *filename);
1203 The utility function C<nbdkit_absolute_path> converts any path to an
1204 absolute path: if it is relative, then all this function does is
1205 prepend the current working directory to the path, with no extra
1208 Note that this function works I<only> when used in the C<.config>, and
1209 C<.config_complete> callbacks.
1211 If conversion was not possible, this calls C<nbdkit_error> and returns
1212 C<NULL>. Note that this function does not check that the file exists.
1214 The returned string must be freed by the caller.
1216 =head2 C<nbdkit_realpath>
1218 char *nbdkit_realpath (const char *filename);
1220 The utility function C<nbdkit_realpath> converts any path to an
1221 absolute path, resolving symlinks. Under the hood it uses the
1222 C<realpath> function, and thus it fails if the path does not exist,
1223 or it is not possible to access to any of the components of the path.
1225 Note that this function works I<only> when used in the C<.config>, and
1226 C<.config_complete> callbacks.
1228 If the path resolution was not possible, this calls C<nbdkit_error>
1229 and returns C<NULL>.
1231 The returned string must be freed by the caller.
1235 All plugins will see a L<umask(2)> of C<0022>.
1239 A plugin that needs to sleep may call L<sleep(2)>, L<nanosleep(2)> and
1240 similar. However that can cause nbdkit to delay excessively when
1241 shutting down (since it must wait for any plugin or filter which is
1242 sleeping). To avoid this there is a special wrapper around nanosleep
1243 which plugins and filters should use instead.
1245 =head2 C<nbdkit_nanosleep>
1247 int nbdkit_nanosleep (unsigned sec, unsigned nsec);
1249 The utility function C<nbdkit_nanosleep> suspends the current thread,
1250 and returns 0 if it slept at least as many seconds and nanoseconds as
1251 requested, or -1 after calling C<nbdkit_error> if there is no point in
1252 continuing the current command. Attempts to sleep more than
1253 C<INT_MAX> seconds are treated as an error.
1257 If the client negotiated an NBD export name with nbdkit then plugins
1258 may read this from any connected callbacks. Nbdkit's normal behaviour
1259 is to accept any export name passed by the client, log it in debug
1260 output, but otherwise ignore it. By using C<nbdkit_export_name>
1261 plugins may choose to filter by export name or serve different
1264 =head2 C<nbdkit_export_name>
1266 const char *nbdkit_export_name (void);
1268 Return the optional NBD export name if one was negotiated with the
1269 current client (this uses thread-local magic so no parameter is
1270 required). The returned string is only valid while the client is
1271 connected, so if you need to store it in the plugin you must copy it.
1273 The export name is a free-form text string, it is not necessarily a
1274 path or filename and it does not need to begin with a C<'/'>
1275 character. The NBD protocol describes the empty string (C<"">) as a
1276 representing a "default export" or to be used in cases where the
1277 export name does not make sense. B<The export name is untrusted
1278 client data, be cautious when parsing it.>
1280 On error, C<nbdkit_error> is called and the call returns C<NULL>.
1284 It is possible to get the address of the client when you are running
1285 in any connected callback.
1287 =head2 C<nbdkit_peer_name>
1289 int nbdkit_peer_name (struct sockaddr *addr, socklen_t *addrlen);
1291 Return the peer (client) address, if available. The C<addr> and
1292 C<addrlen> parameters behave like L<getpeername(2)>. In particular
1293 you must initialize C<addrlen> with the size of the buffer pointed to
1294 by C<addr>, and if C<addr> is not large enough then the address will
1297 In some cases this is not available or the address returned will be
1298 meaningless (eg. if there is a proxy between the client and nbdkit).
1299 This call uses thread-local magic so no parameter is required to
1300 specify the current connection.
1302 On success this returns C<0>. On error, C<nbdkit_error> is called and
1303 this call returns C<-1>.
1307 Run the server with I<-f> and I<-v> options so it doesn't fork and you
1308 can see debugging information:
1310 nbdkit -fv ./myplugin.so [key=value [key=value [...]]]
1312 To print debugging information from within the plugin, call
1313 C<nbdkit_debug>, which has the following prototype and works like
1316 void nbdkit_debug (const char *fs, ...);
1317 void nbdkit_vdebug (const char *fs, va_list args);
1319 For convenience, C<nbdkit_debug> preserves the value of C<errno>, and
1320 also supports the glibc extension of a single C<%m> in a format string
1321 expanding to C<strerror(errno)>, even on platforms that don't support
1322 that natively. Note that C<nbdkit_debug> only prints things when the
1323 server is in verbose mode (I<-v> option).
1327 The I<-v> option switches general debugging on or off, and this
1328 debugging should be used for messages which are useful for all users
1331 In cases where you want to enable specific extra debugging to track
1332 down bugs in plugins or filters — mainly for use by the plugin/filter
1333 developers themselves — you can define Debug Flags. These are global
1334 ints called C<myplugin_debug_*>:
1336 int myplugin_debug_foo;
1337 int myplugin_debug_bar;
1339 if (myplugin_debug_foo) {
1340 nbdkit_debug ("lots of extra debugging about foo: ...");
1343 Debug Flags can be controlled on the command line using the I<-D> (or
1346 nbdkit -f -v -D myplugin.foo=1 -D myplugin.bar=2 myplugin [...]
1348 Note C<myplugin> is the name passed to C<.name> in the C<struct
1351 You should only use this feature for debug settings. For general
1352 settings use ordinary plugin parameters. Debug Flags can only be C
1353 ints. They are not supported by non-C language plugins.
1355 For convenience C<'.'> characters are replaced with C<'_'> characters
1356 in the variable name, so both of these parameters:
1358 -D myplugin.foo_bar=1
1359 -D myplugin.foo.bar=1
1361 correspond to the plugin variable C<myplugin_debug_foo_bar>.
1363 =head1 INSTALLING THE PLUGIN
1365 The plugin is a C<*.so> file and possibly a manual page. You can of
1366 course install the plugin C<*.so> file wherever you want, and users
1367 will be able to use it by running:
1369 nbdkit /path/to/plugin.so [args]
1371 However B<if> the shared library has a name of the form
1372 C<nbdkit-I<name>-plugin.so> B<and if> the library is installed in the
1373 C<$plugindir> directory, then users can be run it by only typing:
1377 The location of the C<$plugindir> directory is set when nbdkit is
1378 compiled and can be found by doing:
1380 nbdkit --dump-config
1382 If using the pkg-config/pkgconf system then you can also find the
1383 plugin directory at compile time by doing:
1385 pkgconf nbdkit --variable=plugindir
1387 =head1 PKG-CONFIG/PKGCONF
1389 nbdkit provides a pkg-config/pkgconf file called C<nbdkit.pc> which
1390 should be installed on the correct path when the nbdkit plugin
1391 development environment is installed. You can use this in autoconf
1392 F<configure.ac> scripts to test for the development environment:
1394 PKG_CHECK_MODULES([NBDKIT], [nbdkit >= 1.2.3])
1396 The above will fail unless nbdkit E<ge> 1.2.3 and the header file is
1397 installed, and will set C<NBDKIT_CFLAGS> and C<NBDKIT_LIBS>
1398 appropriately for compiling plugins.
1400 You can also run pkg-config/pkgconf directly, for example:
1402 if ! pkgconf nbdkit --exists; then
1403 echo "you must install the nbdkit plugin development environment"
1407 You can also substitute the plugindir variable by doing:
1409 PKG_CHECK_VAR([NBDKIT_PLUGINDIR], [nbdkit], [plugindir])
1411 which defines C<$(NBDKIT_PLUGINDIR)> in automake-generated Makefiles.
1413 =head1 WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES
1415 You can also write nbdkit plugins in Lua, OCaml, Perl, Python, Ruby,
1416 Rust, shell script or Tcl. Other programming languages may be offered
1419 For more information see:
1420 __LANG_PLUGIN_LINKS__.
1422 Plugins written in scripting languages may also be installed in
1423 C<$plugindir>. These must be called C<nbdkit-I<name>-plugin> without
1424 any extension. They must be executable, and they must use the shebang
1425 header (see L<nbdkit(1)/Shebang scripts>). For example a plugin
1426 written in Perl called C<foo.pl> might be installed like this:
1429 #!/usr/sbin/nbdkit perl
1431 $ sudo install -m 0755 foo.pl $plugindir/nbdkit-foo-plugin
1433 and then users will be able to run it like this:
1435 $ nbdkit foo [args ...]
1440 L<nbdkit-nozero-filter(3)>,
1441 L<nbdkit-filter(3)>.
1443 Standard plugins provided by nbdkit:
1457 Copyright (C) 2013-2018 Red Hat Inc.