docs: For .open callback, clarify TLS and purpose of readonly flag.
[nbdkit/ericb.git] / docs / nbdkit-filter.pod
blob07ede47a98c2acc47caf3181335540a8cab625fe
1 =head1 NAME
3 nbdkit-filter - how to write nbdkit filters
5 =head1 SYNOPSIS
7  #include <nbdkit-filter.h>
8  
9  static int
10  myfilter_config (nbdkit_next_config *next, void *nxdata,
11                   const char *key, const char *value)
12  {
13    if (strcmp (key, "myparameter") == 0) {
14      // ...
15      return 0;
16    }
17    else {
18      // pass through to next filter or plugin
19      return next (nxdata, key, value);
20    }
21  }
23  static struct nbdkit_filter filter = {
24    .name              = "filter",
25    .config            = myfilter_config,
26    /* etc */
27  };
29  NBDKIT_REGISTER_FILTER(filter)
31 When this has been compiled to a shared library, do:
33  nbdkit [--args ...] --filter=./myfilter.so plugin [key=value ...]
35 When debugging, use the I<-fv> options:
37  nbdkit -fv --filter=./myfilter.so plugin [key=value ...]
39 =head1 DESCRIPTION
41 One or more nbdkit filters can be placed in front of an nbdkit plugin
42 to modify the behaviour of the plugin.  This manual page describes how
43 to create an nbdkit filter.
45 Filters can be used for example to limit requests to an offset/limit,
46 add copy-on-write support, or inject delays or errors (for testing).
48 Different filters can be stacked:
50      NBD     ┌─────────┐    ┌─────────┐          ┌────────┐
51   client ───▶│ filter1 │───▶│ filter2 │── ─ ─ ──▶│ plugin │
52  request     └─────────┘    └─────────┘          └────────┘
54 Each filter intercepts plugin functions (see L<nbdkit-plugin(3)>) and
55 can call the next filter or plugin in the chain, modifying parameters,
56 calling before the filter function, in the middle or after.  Filters
57 may even short-cut the chain.  As an example, to process its own
58 parameters the filter can intercept the C<.config> method:
60  static int
61  myfilter_config (nbdkit_next_config *next, void *nxdata,
62                   const char *key, const char *value)
63  {
64    if (strcmp (key, "myparameter") == 0) {
65      // ...
66      // here you would handle this key, value
67      // ...
68      return 0;
69    }
70    else {
71      // pass through to next filter or plugin
72      return next (nxdata, key, value);
73    }
74  }
76  static struct nbdkit_filter filter = {
77    // ...
78    .config            = myfilter_config,
79    // ...
80  };
82 The call to C<next (nxdata, ...)> calls the C<.config> method of the
83 next filter or plugin in the chain.  In the example above any
84 instances of C<myparameter=...> on the command line would not be seen
85 by the plugin.
87 To see example filters:
88 L<https://github.com/libguestfs/nbdkit/tree/master/filters>
90 Filters must be written in C.
92 Unlike plugins, where we provide a stable ABI guarantee that permits
93 operation across version differences, filters can only be run with the
94 same version of nbdkit that they were compiled with.  The reason for
95 this is two-fold: the filter API includes access to struct
96 nbdkit_next_ops that is likely to change if new callbacks are added
97 (old nbdkit cannot safely run new filters that access new methods);
98 and if we added new methods then an old filter would not see them and
99 so they would be passed unmodified through the filter, and in some
100 cases that leads to data corruption (new nbdkit cannot safely run old
101 filters unaware of new methods).  Therefore, unlike plugins, you
102 should not expect to distribute filters separately from nbdkit.
104 =head1 C<#include E<lt>nbdkit-filter.hE<gt>>
106 All filters should start by including this header file.
108 =head1 C<struct nbdkit_filter>
110 All filters must define and register one C<struct nbdkit_filter>,
111 which contains the name of the filter and pointers to plugin methods
112 that the filter wants to intercept.
114  static struct nbdkit_filter filter = {
115    .name              = "filter",
116    .longname          = "My Filter",
117    .description       = "This is my great filter for nbdkit",
118    .config            = myfilter_config,
119    /* etc */
120  };
122  NBDKIT_REGISTER_FILTER(filter)
124 The C<.name> field is the name of the filter.  This is the only field
125 which is required.
127 =head1 NEXT PLUGIN
129 F<nbdkit-filter.h> defines three function types
130 (C<nbdkit_next_config>, C<nbdkit_next_config_complete>,
131 C<nbdkit_next_open>) and a structure called C<struct nbdkit_next_ops>.
132 These abstract the next plugin or filter in the chain.  There is also
133 an opaque pointer C<nxdata> which must be passed along when calling
134 these functions.
136 =head2 Next config, open and close
138 The filter’s C<.config>, C<.config_complete> and C<.open> methods may
139 only call the next C<.config>, C<.config_complete> and C<.open> method
140 in the chain (optionally for C<.config>).
142 The filter’s C<.close> method is called when an old connection closed,
143 and this has no C<next> parameter because it cannot be
144 short-circuited.
146 =head2 C<next_ops>
148 The filter’s other methods like C<.prepare>, C<.get_size>, C<.pread>
149 etc ― always called in the context of a connection ― are passed a
150 pointer to C<struct nbdkit_next_ops> which contains a comparable set
151 of accessors to plugin methods that can be called during a connection.
153 It is possible for a filter to issue (for example) extra
154 C<next_ops-E<gt>pread> calls in response to a single C<.pwrite> call.
156 Note that the semantics of the functions in C<struct nbdkit_next_ops>
157 are slightly different from what a plugin implements: for example,
158 when a plugin's C<.pread> returns -1 on error, the error value to
159 advertise to the client is implicit (via the plugin calling
160 C<nbdkit_set_error> or setting C<errno>), whereas
161 C<next_ops-E<gt>pread> exposes this via an explicit parameter,
162 allowing a filter to learn or modify this error if desired.
164 There is also a C<next_ops-E<gt>reopen> function which is used by
165 L<nbdkit-retry-filter(3)> to close and reopen the underlying plugin.
166 It should be used with caution because it is difficult to use safely.
168 =head2 Other considerations
170 You can modify parameters when you call the C<next> function.  However
171 be careful when modifying strings because for some methods
172 (eg. C<.config>) the plugin may save the string pointer that you pass
173 along.  So you may have to ensure that the string is not freed for the
174 lifetime of the server.
176 Note that if your filter registers a callback but in that callback it
177 doesn't call the C<next> function then the corresponding method in the
178 plugin will never be called.  In particular, your C<.open> method, if
179 you have one, B<must> call the C<.next> method.
181 =head1 CALLBACKS
183 C<struct nbdkit_filter> has some static fields describing the filter
184 and optional callback functions which can be used to intercept plugin
185 methods.
187 =head2 C<.name>
189  const char *name;
191 This field (a string) is required, and B<must> contain only ASCII
192 alphanumeric characters and be unique amongst all filters.
194 =head2 C<.longname>
196  const char *longname;
198 An optional free text name of the filter.  This field is used in error
199 messages.
201 =head2 C<.description>
203  const char *description;
205 An optional multi-line description of the filter.
207 =head2 C<.load>
209  void load (void);
211 This is called once just after the filter is loaded into memory.  You
212 can use this to perform any global initialization needed by the
213 filter.
215 =head2 C<.unload>
217  void unload (void);
219 This may be called once just before the filter is unloaded from
220 memory.  Note that it's not guaranteed that C<.unload> will always be
221 called (eg. the server might be killed or segfault), so you should try
222 to make the filter as robust as possible by not requiring cleanup.
223 See also L<nbdkit-plugin(3)/SHUTDOWN>.
225 =head2 C<.config>
227  int (*config) (nbdkit_next_config *next, void *nxdata,
228                 const char *key, const char *value);
230 This intercepts the plugin C<.config> method and can be used by the
231 filter to parse its own command line parameters.  You should try to
232 make sure that command line parameter keys that the filter uses do not
233 conflict with ones that could be used by a plugin.
235 If there is an error, C<.config> should call C<nbdkit_error> with an
236 error message and return C<-1>.
238 =head2 C<.config_complete>
240  int (*config_complete) (nbdkit_next_config_complete *next, void *nxdata);
242 This intercepts the plugin C<.config_complete> method and can be used
243 to ensure that all parameters needed by the filter were supplied on
244 the command line.
246 If there is an error, C<.config_complete> should call C<nbdkit_error>
247 with an error message and return C<-1>.
249 =head2 C<.config_help>
251  const char *config_help;
253 This optional multi-line help message should summarize any
254 C<key=value> parameters that it takes.  It does I<not> need to repeat
255 what already appears in C<.description>.
257 If the filter doesn't take any config parameters you should probably
258 omit this.
260 =head2 C<.thread_model>
262  int (*thread_model) (void);
264 Filters may tighten (but not relax) the thread model of the plugin, by
265 defining this callback.  Note that while plugins use a compile-time
266 definition of C<THREAD_MODEL>, filters do not need to declare a model
267 at compile time; instead, this callback is called after
268 C<.config_complete> and before any connections are created.  See
269 L<nbdkit-plugin(3)/THREADS> for a discussion of thread models.
271 The final thread model used by nbdkit is the smallest (ie. most
272 serialized) out of all the filters and the plugin, and applies for all
273 connections.  Requests for a model larger than permitted by the plugin
274 are silently ignored. It is acceptable for decisions made during
275 C<.config> and C<.config_complete> to determine which model to
276 request.
278 This callback is optional; if it is not present, the filter must be
279 written to handle fully parallel requests, including when multiple
280 requests are issued in parallel on the same connection, similar to a
281 plugin requesting C<NBDKIT_THREAD_MODEL_PARALLEL>.  This ensures the
282 filter doesn't slow down other filters or plugins.
284 If there is an error, C<.thread_model> should call C<nbdkit_error>
285 with an error message and return C<-1>.
287 =head2 C<.open>
289  void * (*open) (nbdkit_next_open *next, void *nxdata,
290                  int readonly);
292 This is called when a new client connection is opened and can be used
293 to allocate any per-connection data structures needed by the filter.
294 The handle (which is not the same as the plugin handle) is passed back
295 to other filter callbacks and could be freed in the C<.close>
296 callback.
298 Note that the handle is completely opaque to nbdkit, but it must not
299 be NULL.  If you don't need to use a handle, return
300 C<NBDKIT_HANDLE_NOT_NEEDED> which is a static non-NULL pointer.
302 If there is an error, C<.open> should call C<nbdkit_error> with an
303 error message and return C<NULL>.
305 This callback is optional, but if provided, it must call C<next>,
306 passing a value for C<readonly> according to how the filter plans to
307 use the plugin.  Typically, the filter passes the same value as it
308 received, or passes true to provide a writable layer on top of a
309 read-only backend.  However, it is also acceptable to attempt write
310 access to the plugin even if this filter is readonly, such as when a
311 file system mounted read-only still requires write access to the
312 underlying device in case a journal needs to be replayed for
313 consistency as part of the mounting process.  The filter should
314 generally call C<next> as its first step, to allocate from the plugin
315 outwards, so that C<.close> running from the outer filter to the
316 plugin will be in reverse.
318 =head2 C<.close>
320  void (*close) (void *handle);
322 This is called when the client closes the connection.  It should clean
323 up any per-connection resources used by the filter.  It is called
324 beginning with the outermost filter and ending with the plugin (the
325 opposite order of C<.open> if all filters call C<next> first),
326 although this order technically does not matter since the callback
327 cannot report failures or access the underlying plugin.
329 =head2 C<.prepare>
331 =head2 C<.finalize>
333   int (*prepare) (struct nbdkit_next_ops *next_ops, void *nxdata,
334                   void *handle, int readonly);
335   int (*finalize) (struct nbdkit_next_ops *next_ops, void *nxdata,
336                    void *handle);
338 These two methods can be used to perform any necessary operations just
339 after opening the connection (C<.prepare>) or just before closing the
340 connection (C<.finalize>).
342 For example if you need to scan the underlying disk to check for a
343 partition table, you could do it in your C<.prepare> method (calling
344 the plugin's C<.pread> method via C<next_ops>).  Or if you need to
345 cleanly update superblock data in the image on close you can do it in
346 your C<.finalize> method (calling the plugin's C<.pwrite> method).
347 Doing these things in the filter's C<.open> or C<.close> method is not
348 possible.
350 For C<.prepare>, the value of C<readonly> is the same as was passed to
351 C<.open>, declaring how this filter will be used.
353 There is no C<next_ops-E<gt>prepare> or C<next_ops-E<gt>finalize>.
354 Unlike other filter methods, prepare and finalize are not chained
355 through the C<next_ops> structure.  Instead the core nbdkit server
356 calls the prepare and finalize methods of all filters.  Prepare
357 methods are called starting with the filter closest to the plugin and
358 proceeding outwards (matching the order of C<.open> if all filters
359 call C<next> before doing anything locally).  Finalize methods are
360 called in the reverse order of prepare methods, with the outermost
361 filter first (and matching the order of C<.close>), and only if the
362 prepare method succeeded.
364 If there is an error, both callbacks should call C<nbdkit_error> with
365 an error message and return C<-1>.  An error in C<.prepare> is
366 reported to the client, but leaves the connection open (a client may
367 try again with a different export name, for example); while an error
368 in C<.finalize> forces the client to disconnect.
370 =head2 C<.get_size>
372  int64_t (*get_size) (struct nbdkit_next_ops *next_ops, void *nxdata,
373                       void *handle);
375 This intercepts the plugin C<.get_size> method and can be used to read
376 or modify the apparent size of the block device that the NBD client
377 will see.
379 The returned size must be E<ge> 0.  If there is an error, C<.get_size>
380 should call C<nbdkit_error> with an error message and return C<-1>.
381 This function is only called once per connection and cached by nbdkit.
382 Similarly, repeated calls to C<next_ops-E<gt>get_size> will return a
383 cached value.
385 =head2 C<.can_write>
387 =head2 C<.can_flush>
389 =head2 C<.is_rotational>
391 =head2 C<.can_trim>
393 =head2 C<.can_zero>
395 =head2 C<.can_fast_zero>
397 =head2 C<.can_extents>
399 =head2 C<.can_fua>
401 =head2 C<.can_multi_conn>
403 =head2 C<.can_cache>
405  int (*can_write) (struct nbdkit_next_ops *next_ops, void *nxdata,
406                    void *handle);
407  int (*can_flush) (struct nbdkit_next_ops *next_ops, void *nxdata,
408                    void *handle);
409  int (*is_rotational) (struct nbdkit_next_ops *next_ops,
410                        void *nxdata,
411                        void *handle);
412  int (*can_trim) (struct nbdkit_next_ops *next_ops, void *nxdata,
413                   void *handle);
414  int (*can_zero) (struct nbdkit_next_ops *next_ops, void *nxdata,
415                   void *handle);
416  int (*can_fast_zero) (struct nbdkit_next_ops *next_ops, void *nxdata,
417                        void *handle);
418  int (*can_extents) (struct nbdkit_next_ops *next_ops, void *nxdata,
419                      void *handle);
420  int (*can_fua) (struct nbdkit_next_ops *next_ops, void *nxdata,
421                  void *handle);
422  int (*can_multi_conn) (struct nbdkit_next_ops *next_ops, void *nxdata,
423                         void *handle);
424  int (*can_cache) (struct nbdkit_next_ops *next_ops, void *nxdata,
425                    void *handle);
427 These intercept the corresponding plugin methods, and control feature
428 bits advertised to the client.
430 Of note, the semantics of C<.can_zero> callback in the filter are
431 slightly different from the plugin, and must be one of three success
432 values visible only to filters:
434 =over 4
436 =item C<NBDKIT_ZERO_NONE>
438 Completely suppress advertisement of write zero support (this can only
439 be done from filters, not plugins).
441 =item C<NBDKIT_ZERO_EMULATE>
443 Inform nbdkit that write zeroes should immediately fall back to
444 C<.pwrite> emulation without trying C<.zero> (this value is returned
445 by C<next_ops-E<gt>can_zero> if the plugin returned false in its
446 C<.can_zero>).
448 =item C<NBDKIT_ZERO_NATIVE>
450 Inform nbdkit that write zeroes should attempt to use C<.zero>,
451 although it may still fall back to C<.pwrite> emulation for C<ENOTSUP>
452 or C<EOPNOTSUPP> failures (this value is returned by
453 C<next_ops-E<gt>can_zero> if the plugin returned true in its
454 C<.can_zero>).
456 =back
458 Remember that most of the feature check functions return merely a
459 boolean success value, while C<.can_zero>, C<.can_fua> and
460 C<.can_cache> have three success values.
462 The difference between C<.can_fua> values may affect choices made in
463 the filter: when splitting a write request that requested FUA from the
464 client, if C<next_ops-E<gt>can_fua> returns C<NBDKIT_FUA_NATIVE>, then
465 the filter should pass the FUA flag on to each sub-request; while if
466 it is known that FUA is emulated by a flush because of a return of
467 C<NBDKIT_FUA_EMULATE>, it is more efficient to only flush once after
468 all sub-requests have completed (often by passing C<NBDKIT_FLAG_FUA>
469 on to only the final sub-request, or by dropping the flag and ending
470 with a direct call to C<next_ops-E<gt>flush>).
472 If there is an error, the callback should call C<nbdkit_error> with an
473 error message and return C<-1>.  These functions are called at most
474 once per connection and cached by nbdkit. Similarly, repeated calls to
475 any of the C<next_ops> counterparts will return a cached value; by
476 calling into the plugin during C<.prepare>, you can ensure that later
477 use of the cached values during data commands like <.pwrite> will not
478 fail.
480 =head2 C<.pread>
482  int (*pread) (struct nbdkit_next_ops *next_ops, void *nxdata,
483                void *handle, void *buf, uint32_t count, uint64_t offset,
484                uint32_t flags, int *err);
486 This intercepts the plugin C<.pread> method and can be used to read or
487 modify data read by the plugin.
489 The parameter C<flags> exists in case of future NBD protocol
490 extensions; at this time, it will be 0 on input, and the filter should
491 not pass any flags to C<next_ops-E<gt>pread>.
493 If there is an error (including a short read which couldn't be
494 recovered from), C<.pread> should call C<nbdkit_error> with an error
495 message B<and> return -1 with C<err> set to the positive errno value
496 to return to the client.
498 =head2 C<.pwrite>
500  int (*pwrite) (struct nbdkit_next_ops *next_ops, void *nxdata,
501                 void *handle,
502                 const void *buf, uint32_t count, uint64_t offset,
503                 uint32_t flags, int *err);
505 This intercepts the plugin C<.pwrite> method and can be used to modify
506 data written by the plugin.
508 This function will not be called if C<.can_write> returned false; in
509 turn, the filter should not call C<next_ops-E<gt>pwrite> if
510 C<next_ops-E<gt>can_write> did not return true.
512 The parameter C<flags> may include C<NBDKIT_FLAG_FUA> on input based
513 on the result of C<.can_fua>.  In turn, the filter should only pass
514 C<NBDKIT_FLAG_FUA> on to C<next_ops-E<gt>pwrite> if
515 C<next_ops-E<gt>can_fua> returned a positive value.
517 If there is an error (including a short write which couldn't be
518 recovered from), C<.pwrite> should call C<nbdkit_error> with an error
519 message B<and> return -1 with C<err> set to the positive errno value
520 to return to the client.
522 =head2 C<.flush>
524  int (*flush) (struct nbdkit_next_ops *next_ops, void *nxdata,
525                void *handle, uint32_t flags, int *err);
527 This intercepts the plugin C<.flush> method and can be used to modify
528 flush requests.
530 This function will not be called if C<.can_flush> returned false; in
531 turn, the filter should not call C<next_ops-E<gt>flush> if
532 C<next_ops-E<gt>can_flush> did not return true.
534 The parameter C<flags> exists in case of future NBD protocol
535 extensions; at this time, it will be 0 on input, and the filter should
536 not pass any flags to C<next_ops-E<gt>flush>.
538 If there is an error, C<.flush> should call C<nbdkit_error> with an
539 error message B<and> return -1 with C<err> set to the positive errno
540 value to return to the client.
542 =head2 C<.trim>
544  int (*trim) (struct nbdkit_next_ops *next_ops, void *nxdata,
545               void *handle, uint32_t count, uint64_t offset,
546               uint32_t flags, int *err);
548 This intercepts the plugin C<.trim> method and can be used to modify
549 trim requests.
551 This function will not be called if C<.can_trim> returned false; in
552 turn, the filter should not call C<next_ops-E<gt>trim> if
553 C<next_ops-E<gt>can_trim> did not return true.
555 The parameter C<flags> may include C<NBDKIT_FLAG_FUA> on input based
556 on the result of C<.can_fua>.  In turn, the filter should only pass
557 C<NBDKIT_FLAG_FUA> on to C<next_ops-E<gt>trim> if
558 C<next_ops-E<gt>can_fua> returned a positive value.
560 If there is an error, C<.trim> should call C<nbdkit_error> with an
561 error message B<and> return -1 with C<err> set to the positive errno
562 value to return to the client.
564 =head2 C<.zero>
566  int (*zero) (struct nbdkit_next_ops *next_ops, void *nxdata,
567               void *handle, uint32_t count, uint64_t offset, uint32_t flags,
568               int *err);
570 This intercepts the plugin C<.zero> method and can be used to modify
571 zero requests.
573 This function will not be called if C<.can_zero> returned
574 C<NBDKIT_ZERO_NONE>; in turn, the filter should not call
575 C<next_ops-E<gt>zero> if C<next_ops-E<gt>can_zero> returned
576 C<NBDKIT_ZERO_NONE>.
578 On input, the parameter C<flags> may include C<NBDKIT_FLAG_MAY_TRIM>
579 unconditionally, C<NBDKIT_FLAG_FUA> based on the result of
580 C<.can_fua>, and C<NBDKIT_FLAG_FAST_ZERO> based on the result of
581 C<.can_fast_zero>.  In turn, the filter may pass
582 C<NBDKIT_FLAG_MAY_TRIM> unconditionally, but should only pass
583 C<NBDKIT_FLAG_FUA> or C<NBDKIT_FLAG_FAST_ZERO> on to
584 C<next_ops-E<gt>zero> if the corresponding C<next_ops-E<gt>can_fua> or
585 C<next_ops-E<gt>can_fast_zero> returned a positive value.
587 Note that unlike the plugin C<.zero> which is permitted to fail with
588 C<ENOTSUP> or C<EOPNOTSUPP> to force a fallback to C<.pwrite>, the
589 function C<next_ops-E<gt>zero> will not fail with C<err> set to
590 C<ENOTSUP> or C<EOPNOTSUPP> unless C<NBDKIT_FLAG_FAST_ZERO> was used,
591 because otherwise the fallback has already taken place.
593 If there is an error, C<.zero> should call C<nbdkit_error> with an
594 error message B<and> return -1 with C<err> set to the positive errno
595 value to return to the client.  The filter should not fail with
596 C<ENOTSUP> or C<EOPNOTSUPP> unless C<flags> includes
597 C<NBDKIT_FLAG_FAST_ZERO> (while plugins have automatic fallback to
598 C<.pwrite>, filters do not).
600 =head2 C<.extents>
602  int (*extents) (struct nbdkit_next_ops *next_ops, void *nxdata,
603                  void *handle, uint32_t count, uint64_t offset, uint32_t flags,
604                  struct nbdkit_extents *extents,
605                  int *err);
607 This intercepts the plugin C<.extents> method and can be used to
608 modify extent requests.
610 This function will not be called if C<.can_extents> returned false; in
611 turn, the filter should not call C<next_ops-E<gt>extents> if
612 C<next_ops-E<gt>can_extents> did not return true.
614 It is possible for filters to transform the extents list received back
615 from the layer below.  Without error checking it would look like this:
617  myfilter_extents (..., uint32_t count, uint64_t offset, ...)
619    size_t i;
620    struct nbdkit_extents *extents2;
621    struct nbdkit_extent e;
622    int64_t size;
624    size = next_ops->get_size (nxdata);
625    extents2 = nbdkit_extents_new (offset + shift, size);
626    next_ops->extents (nxdata, count, offset + shift, flags, extents2, err);
627    for (i = 0; i < nbdkit_extents_count (extents2); ++i) {
628      e = nbdkit_get_extent (extents2, i);
629      e.offset -= shift;
630      nbdkit_add_extent (extents, e.offset, e.length, e.type);
631    }
632    nbdkit_extents_free (extents2);
635 If there is an error, C<.extents> should call C<nbdkit_error> with an
636 error message B<and> return -1 with C<err> set to the positive errno
637 value to return to the client.
639 =head3 Allocating and freeing nbdkit_extents list
641 Two functions are provided to filters only for allocating and freeing
642 the map:
644  struct nbdkit_extents *nbdkit_extents_new (uint64_t start, uint64_t end);
646 Allocates and returns a new, empty extents list.  The C<start>
647 parameter is the start of the range described in the list, and the
648 C<end> parameter is the offset of the byte beyond the end.  Normally
649 you would pass in C<offset> as the start and the size of the plugin as
650 the end, but for filters which adjust offsets, they should pass in the
651 adjusted offset.
653 On error this function can return C<NULL>.  In this case it calls
654 C<nbdkit_error> and/or C<nbdkit_set_error> as required.  C<errno> will
655 be set to a suitable value.
657  void nbdkit_extents_free (struct nbdkit_extents *);
659 Frees an existing extents list.
661 =head3 Iterating over nbdkit_extents list
663 Two functions are provided to filters only to iterate over the extents
664 in order:
666  size_t nbdkit_extents_count (const struct nbdkit_extents *);
668 Returns the number of extents in the list.
670  struct nbdkit_extent {
671    uint64_t offset;
672    uint64_t length;
673    uint32_t type;
674  };
675  struct nbdkit_extent nbdkit_get_extent (const struct nbdkit_extents *,
676                                          size_t i);
678 Returns a copy of the C<i>'th extent.
680 =head2 C<.cache>
682  int (*cache) (struct nbdkit_next_ops *next_ops, void *nxdata,
683                void *handle, uint32_t count, uint64_t offset,
684                uint32_t flags, int *err);
686 This intercepts the plugin C<.cache> method and can be used to modify
687 cache requests.
689 This function will not be called if C<.can_cache> returned
690 C<NBDKIT_CACHE_NONE> or C<NBDKIT_CACHE_EMULATE>; in turn, the filter
691 should not call C<next_ops-E<gt>cache> unless
692 C<next_ops-E<gt>can_cache> returned C<NBDKIT_CACHE_NATIVE>.
694 The parameter C<flags> exists in case of future NBD protocol
695 extensions; at this time, it will be 0 on input, and the filter should
696 not pass any flags to C<next_ops-E<gt>cache>.
698 If there is an error, C<.cache> should call C<nbdkit_error> with an
699 error message B<and> return -1 with C<err> set to the positive errno
700 value to return to the client.
702 =head1 ERROR HANDLING
704 If there is an error in the filter itself, the filter should call
705 C<nbdkit_error> to report an error message.  If the callback is
706 involved in serving data, the explicit C<err> parameter determines the
707 error code that will be sent to the client; other callbacks should
708 return the appropriate error indication, eg. C<NULL> or C<-1>.
710 C<nbdkit_error> has the following prototype and works like
711 L<printf(3)>:
713  void nbdkit_error (const char *fs, ...);
714  void nbdkit_verror (const char *fs, va_list args);
716 For convenience, C<nbdkit_error> preserves the value of C<errno>, and
717 also supports the glibc extension of a single C<%m> in a format string
718 expanding to C<strerror(errno)>, even on platforms that don't support
719 that natively.
721 =head1 DEBUGGING
723 Run the server with I<-f> and I<-v> options so it doesn't fork and you
724 can see debugging information:
726  nbdkit -fv --filter=./myfilter.so plugin [key=value [key=value [...]]]
728 To print debugging information from within the filter, call
729 C<nbdkit_debug>, which has the following prototype and works like
730 L<printf(3)>:
732  void nbdkit_debug (const char *fs, ...);
733  void nbdkit_vdebug (const char *fs, va_list args);
735 For convenience, C<nbdkit_debug> preserves the value of C<errno>, and
736 also supports the glibc extension of a single C<%m> in a format string
737 expanding to C<strerror(errno)>, even on platforms that don't support
738 that natively.  Note that C<nbdkit_debug> only prints things when the
739 server is in verbose mode (I<-v> option).
741 =head2 Debug Flags
743 Debug Flags in filters work exactly the same way as plugins.  See
744 L<nbdkit-plugin(3)/Debug Flags>.
746 =head1 INSTALLING THE FILTER
748 The filter is a C<*.so> file and possibly a manual page.  You can of
749 course install the filter C<*.so> file wherever you want, and users
750 will be able to use it by running:
752  nbdkit --filter=/path/to/filter.so plugin [args]
754 However B<if> the shared library has a name of the form
755 C<nbdkit-I<name>-filter.so> B<and if> the library is installed in the
756 C<$filterdir> directory, then users can be run it by only typing:
758  nbdkit --filter=name plugin [args]
760 The location of the C<$filterdir> directory is set when nbdkit is
761 compiled and can be found by doing:
763  nbdkit --dump-config
765 If using the pkg-config/pkgconf system then you can also find the
766 filter directory at compile time by doing:
768  pkgconf nbdkit --variable=filterdir
770 =head1 PKG-CONFIG/PKGCONF
772 nbdkit provides a pkg-config/pkgconf file called C<nbdkit.pc> which
773 should be installed on the correct path when the nbdkit development
774 environment is installed.  You can use this in autoconf
775 F<configure.ac> scripts to test for the development environment:
777  PKG_CHECK_MODULES([NBDKIT], [nbdkit >= 1.2.3])
779 The above will fail unless nbdkit E<ge> 1.2.3 and the header file is
780 installed, and will set C<NBDKIT_CFLAGS> and C<NBDKIT_LIBS>
781 appropriately for compiling filters.
783 You can also run pkg-config/pkgconf directly, for example:
785  if ! pkgconf nbdkit --exists; then
786    echo "you must install the nbdkit development environment"
787    exit 1
788  fi
790 You can also substitute the filterdir variable by doing:
792  PKG_CHECK_VAR([NBDKIT_FILTERDIR], [nbdkit], [filterdir])
794 which defines C<$(NBDKIT_FILTERDIR)> in automake-generated Makefiles.
796 =head1 SEE ALSO
798 L<nbdkit(1)>,
799 L<nbdkit-plugin(3)>.
801 Standard filters provided by nbdkit:
803 __FILTER_LINKS__.
805 =head1 AUTHORS
807 Eric Blake
809 Richard W.M. Jones
811 =head1 COPYRIGHT
813 Copyright (C) 2013-2019 Red Hat Inc.