Update Red Hat Copyright Notices
[nbdkit.git] / server / internal.h
blob4b6e29154e10520c270161a0c42ffb77d0be8bef
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef NBDKIT_INTERNAL_H
34 #define NBDKIT_INTERNAL_H
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdarg.h>
39 #include <pthread.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
45 #define NBDKIT_API_VERSION 2
46 #define NBDKIT_INTERNAL
47 #include "nbdkit-plugin.h"
48 #include "nbdkit-filter.h"
49 #include "cleanup.h"
50 #include "nbd-protocol.h"
51 #include "string-vector.h"
52 #include "unix-path-max.h"
53 #include "vector.h"
54 #include "windows-compat.h"
56 /* Define unlikely macro, but only for GCC. These are used to move
57 * debug and error handling code out of hot paths.
59 #if defined (__GNUC__)
60 #define unlikely(x) __builtin_expect (!!(x), 0)
61 #define if_verbose if (unlikely (verbose))
62 #else
63 #define unlikely(x) (x)
64 #define if_verbose if (verbose)
65 #endif
67 #if defined (__SANITIZE_ADDRESS__)
68 # define DO_DLCLOSE 0
69 #elif ENABLE_LIBFUZZER
70 /* XXX This causes dlopen in the server to leak during fuzzing.
71 * However it is necessary because of
72 * https://bugs.llvm.org/show_bug.cgi?id=43917
74 # define DO_DLCLOSE 0
75 #else
76 # if HAVE_VALGRIND
77 # include <valgrind.h>
78 /* http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
79 # define DO_DLCLOSE !RUNNING_ON_VALGRIND
80 # else
81 # define DO_DLCLOSE 1
82 # endif
83 #endif
85 /* Declare program_name. */
86 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
87 #include <errno.h>
88 #define program_name program_invocation_short_name
89 #else
90 #define program_name "nbdkit"
91 #endif
93 #define container_of(ptr, type, member) ({ \
94 const typeof (((type *) 0)->member) *__mptr = (ptr); \
95 (type *) ((char *) __mptr - offsetof (type, member)); \
98 /* Maximum read or write request that we will handle. */
99 #define MAX_REQUEST_SIZE (64 * 1024 * 1024)
101 /* main.c */
102 enum log_to {
103 LOG_TO_DEFAULT, /* --log not specified: log to stderr, unless
104 we forked into the background in which
105 case log to syslog */
106 LOG_TO_STDERR, /* --log=stderr forced on the command line */
107 LOG_TO_SYSLOG, /* --log=syslog forced on the command line */
108 LOG_TO_NULL, /* --log=null forced on the command line */
111 extern int tcpip_sock_af;
112 extern struct debug_flag *debug_flags;
113 extern const char *export_name;
114 extern bool foreground;
115 extern const char *ipaddr;
116 extern enum log_to log_to;
117 extern unsigned mask_handshake;
118 extern bool newstyle;
119 extern bool no_sr;
120 extern const char *port;
121 extern bool read_only;
122 extern const char *run;
123 extern bool listen_stdin;
124 extern bool configured;
125 extern const char *selinux_label;
126 extern unsigned threads;
127 extern int tls;
128 extern const char *tls_certificates_dir;
129 extern const char *tls_psk;
130 extern bool tls_verify_peer;
131 extern char *unixsocket;
132 extern const char *user, *group;
133 extern bool verbose;
134 extern bool vsock;
135 extern int saved_stdin;
136 extern int saved_stdout;
138 /* Linked list of backends. Each backend struct is followed by either
139 * a filter or plugin struct. "top" points to the first one. They
140 * are linked through the backend->next field.
142 * ┌──────────┐ ┌──────────┐ ┌──────────┐
143 * top ───▶│ backend │───▶│ backend │───▶│ backend │
144 * │ b->i = 2 │ │ b->i = 1 │ │ b->i = 0 │
145 * │ filter │ │ filter │ │ plugin │
146 * └──────────┘ └──────────┘ └──────────┘
148 extern struct backend *top;
149 #define for_each_backend(b) for (b = top; b != NULL; b = b->next)
151 /* debug.c */
152 #define debug(fs, ...) \
153 do { \
154 if_verbose \
155 debug_in_server ((fs), ##__VA_ARGS__); \
156 } while (0)
158 extern void debug_in_server (const char *msg, ...)
159 ATTRIBUTE_FORMAT_PRINTF (1, 2);
161 /* quit.c */
162 extern volatile int quit;
163 #ifndef WIN32
164 extern int quit_fd;
165 #else
166 extern HANDLE quit_fd;
167 #endif
168 extern void set_up_quit_pipe (void);
169 extern void close_quit_pipe (void);
170 extern void handle_quit (int sig);
172 /* signals.c */
173 extern void set_up_signals (void);
175 /* background.c */
176 extern bool forked_into_background;
177 extern void fork_into_background (void);
179 /* captive.c */
180 extern void run_command (void);
182 /* socket-activation.c */
183 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
184 extern unsigned int get_socket_activation (void);
186 /* usergroup.c */
187 extern void change_user (void);
189 /* connections.c */
191 /* Flags for connection_send_function */
192 enum {
193 SEND_MORE = 1, /* Hint to use MSG_MORE/corking to group send()s */
196 typedef int (*connection_recv_function) (void *buf, size_t len)
197 __attribute__ ((__nonnull__ (1)));
198 typedef int (*connection_send_function) (const void *buf, size_t len,
199 int flags)
200 __attribute__ ((__nonnull__ (1)));
201 typedef void (*connection_close_function) (int how);
203 /* struct context stores data per connection and backend. Primarily
204 * this is the filter or plugin handle, but other state is also stored
205 * here.
207 enum {
208 HANDLE_OPEN = 1, /* Set if .open passed, so .close is needed */
209 HANDLE_CONNECTED = 2, /* Set if .prepare passed, so .finalize is needed */
210 HANDLE_FAILED = 4, /* Set if .finalize failed */
213 struct context {
214 struct nbdkit_next_ops next; /* Must be first member, for ABI reasons */
216 void *handle; /* Plugin or filter handle. */
217 struct backend *b; /* Backend that provided handle. */
218 struct context *c_next; /* Underlying context, only when b->next != NULL. */
219 struct connection *conn; /* Active connection at context creation, if any. */
221 unsigned char state; /* Bitmask of HANDLE_* values */
223 uint64_t exportsize;
224 uint32_t minimum_block_size;
225 uint32_t preferred_block_size;
226 uint32_t maximum_block_size;
227 int can_write;
228 int can_flush;
229 int is_rotational;
230 int can_trim;
231 int can_zero;
232 int can_fast_zero;
233 int can_fua;
234 int can_multi_conn;
235 int can_extents;
236 int can_cache;
239 typedef enum {
240 STATUS_DEAD, /* Connection is closed */
241 STATUS_CLIENT_DONE, /* Client has sent NBD_CMD_DISC */
242 STATUS_SHUTDOWN, /* Server wants soft shutdown */
243 STATUS_ACTIVE, /* Client can make requests */
244 } conn_status;
246 struct connection {
247 /* Listed in precedence order: do not grab earlier locks in this list
248 * while holding a later lock.
250 pthread_mutex_t request_lock; /* Forces serialization of requests */
251 pthread_mutex_t read_lock; /* Read entire client payload off wire */
252 pthread_mutex_t write_lock; /* Protect sockout, write response to wire */
253 pthread_mutex_t status_lock; /* Track current status of client */
255 conn_status status;
256 int status_pipe[2]; /* track status changes via poll when nworkers > 1 */
257 void *crypto_session;
258 int nworkers;
260 struct context *top_context; /* The context tied to 'top'. */
261 char **default_exportname; /* One per plugin and filter. */
263 uint32_t cflags;
264 uint16_t eflags;
265 bool handshake_complete;
266 bool using_tls;
267 bool structured_replies;
268 bool meta_context_base_allocation;
270 string_vector interns;
271 char *exportname_from_set_meta_context;
272 const char *exportname;
274 int sockin, sockout;
275 /* If nworkers > 1, only call this while read_lock is held */
276 connection_recv_function recv;
277 /* If nworkers > 1, only call these while write_lock is held */
278 connection_send_function send;
279 connection_close_function close;
282 extern void handle_single_connection (int sockin, int sockout);
283 extern conn_status connection_get_status (void);
284 extern bool connection_set_status (conn_status value);
286 /* protocol-handshake.c */
287 extern int protocol_handshake (void);
288 extern int protocol_common_open (uint64_t *exportsize, uint16_t *flags,
289 const char *exportname)
290 __attribute__ ((__nonnull__ (1, 2, 3)));
292 /* protocol-handshake-oldstyle.c */
293 extern int protocol_handshake_oldstyle (void);
295 /* protocol-handshake-newstyle.c */
296 extern int protocol_handshake_newstyle (void);
298 /* protocol.c */
299 extern bool protocol_recv_request_send_reply (void);
301 /* The context ID of base:allocation. As far as I can tell it doesn't
302 * matter what this is as long as nbdkit always returns the same
303 * number.
305 #define base_allocation_id 1
307 /* public.c */
308 extern void free_interns (void);
310 /* crypto.c */
311 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
312 extern void crypto_init (bool tls_set_on_cli);
313 extern void crypto_free (void);
314 extern int crypto_negotiate_tls (int sockin, int sockout);
316 /* debug-flags.c */
317 extern void add_debug_flag (const char *arg);
318 extern void apply_debug_flags (void *dl, const char *name);
319 extern void free_debug_flags (void);
321 /* log.c */
322 extern void log_verror (const char *fs, va_list args);
324 /* log-*.c */
325 extern void log_stderr_verror (const char *fs, va_list args)
326 ATTRIBUTE_FORMAT_PRINTF (1, 0);
327 extern void log_syslog_verror (const char *fs, va_list args)
328 ATTRIBUTE_FORMAT_PRINTF (1, 0);
330 /* vfprintf.c */
331 #if !HAVE_VFPRINTF_PERCENT_M
332 #include <stdio.h>
333 #define vfprintf replace_vfprintf
334 extern int replace_vfprintf (FILE *f, const char *fmt, va_list args)
335 __attribute__ ((__format__ (printf, 2, 0)));
336 #endif
338 /* backend.c */
339 struct backend {
340 /* Next filter or plugin in the chain. This is always NULL for
341 * plugins and never NULL for filters.
343 struct backend *next;
345 /* A unique index used to fetch the handle from the connections
346 * object. The plugin (last in the chain) has index 0, and the
347 * filters have index 1, 2, ... depending how "far" they are from
348 * the plugin.
350 size_t i;
352 /* The type of backend: filter or plugin. */
353 const char *type;
355 /* A copy of the backend name that survives a dlclose. */
356 char *name;
358 /* The file the backend was loaded from. */
359 char *filename;
361 /* The dlopen handle for the backend. */
362 void *dl;
364 /* Backend callbacks. All are required. */
365 void (*free) (struct backend *);
366 int (*thread_model) (struct backend *);
367 const char *(*plugin_name) (struct backend *);
368 void (*usage) (struct backend *);
369 const char *(*version) (struct backend *);
370 void (*dump_fields) (struct backend *);
371 void (*config) (struct backend *, const char *key, const char *value);
372 void (*config_complete) (struct backend *);
373 const char *(*magic_config_key) (struct backend *);
374 void (*get_ready) (struct backend *);
375 void (*after_fork) (struct backend *);
376 void (*cleanup) (struct backend *);
378 int (*preconnect) (struct backend *, int readonly);
379 int (*list_exports) (struct backend *, int readonly, int is_tls,
380 struct nbdkit_exports *exports);
381 const char *(*default_export) (struct backend *, int readonly, int is_tls);
382 void *(*open) (struct context *, int readonly, const char *exportname,
383 int is_tls);
384 int (*prepare) (struct context *, int readonly);
385 int (*finalize) (struct context *);
386 void (*close) (struct context *);
388 const char *(*export_description) (struct context *);
389 int64_t (*get_size) (struct context *);
390 int (*block_size) (struct context *,
391 uint32_t *minimum, uint32_t *preferred, uint32_t *maximum);
392 int (*can_write) (struct context *);
393 int (*can_flush) (struct context *);
394 int (*is_rotational) (struct context *);
395 int (*can_trim) (struct context *);
396 int (*can_zero) (struct context *);
397 int (*can_fast_zero) (struct context *);
398 int (*can_extents) (struct context *);
399 int (*can_fua) (struct context *);
400 int (*can_multi_conn) (struct context *);
401 int (*can_cache) (struct context *);
403 int (*pread) (struct context *,
404 void *buf, uint32_t count, uint64_t offset,
405 uint32_t flags, int *err);
406 int (*pwrite) (struct context *,
407 const void *buf, uint32_t count, uint64_t offset,
408 uint32_t flags, int *err);
409 int (*flush) (struct context *, uint32_t flags, int *err);
410 int (*trim) (struct context *,
411 uint32_t count, uint64_t offset, uint32_t flags, int *err);
412 int (*zero) (struct context *,
413 uint32_t count, uint64_t offset, uint32_t flags, int *err);
414 int (*extents) (struct context *,
415 uint32_t count, uint64_t offset, uint32_t flags,
416 struct nbdkit_extents *extents, int *err);
417 int (*cache) (struct context *,
418 uint32_t count, uint64_t offset, uint32_t flags, int *err);
421 extern void backend_init (struct backend *b, struct backend *next, size_t index,
422 const char *filename, void *dl, const char *type)
423 __attribute__ ((__nonnull__ (1, 4, 5, 6)));
424 extern void backend_load (struct backend *b, const char *name,
425 void (*load) (void))
426 __attribute__ ((__nonnull__ (1 /* not 2 */)));
427 extern void backend_unload (struct backend *b, void (*unload) (void))
428 __attribute__ ((__nonnull__ (1)));
430 extern int backend_list_exports (struct backend *b, int readonly,
431 struct nbdkit_exports *exports)
432 __attribute__ ((__nonnull__ (1, 3)));
433 extern const char *backend_default_export (struct backend *b, int readonly)
434 __attribute__ ((__nonnull__ (1)));
435 /* exportname is only valid for this call and almost certainly will be
436 * freed on return of this function, so backends must save the
437 * exportname if they need to refer to it later.
439 extern struct context *backend_open (struct backend *b,
440 int readonly, const char *exportname,
441 int shared)
442 __attribute__ ((__nonnull__ (1, 3)));
443 extern int backend_prepare (struct context *c)
444 __attribute__ ((__nonnull__ (1)));
445 extern int backend_finalize (struct context *c)
446 __attribute__ ((__nonnull__ (1)));
447 extern void backend_close (struct context *c)
448 __attribute__ ((__nonnull__ (1)));
449 extern bool backend_valid_range (struct context *c,
450 uint64_t offset, uint32_t count)
451 __attribute__ ((__nonnull__ (1)));
453 extern const char *backend_export_description (struct context *c)
454 __attribute__ ((__nonnull__ (1)));
455 extern int64_t backend_get_size (struct context *c)
456 __attribute__ ((__nonnull__ (1)));
457 extern int backend_block_size (struct context *c,
458 uint32_t *minimum, uint32_t *preferred,
459 uint32_t *maximum)
460 __attribute__ ((__nonnull__ (1, 2, 3, 4)));
461 extern int backend_can_write (struct context *c)
462 __attribute__ ((__nonnull__ (1)));
463 extern int backend_can_flush (struct context *c)
464 __attribute__ ((__nonnull__ (1)));
465 extern int backend_is_rotational (struct context *c)
466 __attribute__ ((__nonnull__ (1)));
467 extern int backend_can_trim (struct context *c)
468 __attribute__ ((__nonnull__ (1)));
469 extern int backend_can_zero (struct context *c)
470 __attribute__ ((__nonnull__ (1)));
471 extern int backend_can_fast_zero (struct context *c)
472 __attribute__ ((__nonnull__ (1)));
473 extern int backend_can_extents (struct context *c)
474 __attribute__ ((__nonnull__ (1)));
475 extern int backend_can_fua (struct context *c)
476 __attribute__ ((__nonnull__ (1)));
477 extern int backend_can_multi_conn (struct context *c)
478 __attribute__ ((__nonnull__ (1)));
479 extern int backend_can_cache (struct context *c)
480 __attribute__ ((__nonnull__ (1)));
482 extern int backend_pread (struct context *c,
483 void *buf, uint32_t count, uint64_t offset,
484 uint32_t flags, int *err)
485 __attribute__ ((__nonnull__ (1, 2, 6)));
486 extern int backend_pwrite (struct context *c,
487 const void *buf, uint32_t count, uint64_t offset,
488 uint32_t flags, int *err)
489 __attribute__ ((__nonnull__ (1, 2, 6)));
490 extern int backend_flush (struct context *c,
491 uint32_t flags, int *err)
492 __attribute__ ((__nonnull__ (1, 3)));
493 extern int backend_trim (struct context *c,
494 uint32_t count, uint64_t offset, uint32_t flags,
495 int *err)
496 __attribute__ ((__nonnull__ (1, 5)));
497 extern int backend_zero (struct context *c,
498 uint32_t count, uint64_t offset, uint32_t flags,
499 int *err)
500 __attribute__ ((__nonnull__ (1, 5)));
501 extern int backend_extents (struct context *c,
502 uint32_t count, uint64_t offset, uint32_t flags,
503 struct nbdkit_extents *extents, int *err)
504 __attribute__ ((__nonnull__ (1, 5, 6)));
505 extern int backend_cache (struct context *c,
506 uint32_t count, uint64_t offset,
507 uint32_t flags, int *err)
508 __attribute__ ((__nonnull__ (1, 5)));
510 /* plugins.c */
511 extern struct backend *plugin_register (size_t index, const char *filename,
512 void *dl, struct nbdkit_plugin *(*plugin_init) (void))
513 __attribute__ ((__nonnull__ (2, 3, 4)));
515 /* filters.c */
516 extern struct backend *filter_register (struct backend *next, size_t index,
517 const char *filename, void *dl,
518 struct nbdkit_filter *(*filter_init) (void))
519 __attribute__ ((__nonnull__ (1, 3, 4, 5)));
521 /* locks.c */
522 extern unsigned thread_model;
523 extern void lock_init_thread_model (void);
524 extern const char *name_of_thread_model (int model);
525 extern void lock_connection (void);
526 extern void unlock_connection (void);
527 extern void lock_request (void);
528 extern void unlock_request (void);
529 extern void lock_unload (void);
530 extern void unlock_unload (void);
532 /* sockets.c */
533 DEFINE_VECTOR_TYPE (sockets, int);
534 extern void bind_unix_socket (sockets *) __attribute__ ((__nonnull__ (1)));
535 extern void bind_tcpip_socket (sockets *) __attribute__ ((__nonnull__ (1)));
536 extern void bind_vsock (sockets *) __attribute__ ((__nonnull__ (1)));
537 extern void accept_incoming_connections (const sockets *socks)
538 __attribute__ ((__nonnull__ (1)));
540 /* threadlocal.c */
541 extern void threadlocal_init (void);
542 extern void threadlocal_new_server_thread (void);
543 extern void threadlocal_set_name (const char *name)
544 __attribute__ ((__nonnull__ (1)));
545 extern const char *threadlocal_get_name (void);
546 extern void threadlocal_set_instance_num (size_t instance_num);
547 extern size_t threadlocal_get_instance_num (void);
548 extern void threadlocal_set_error (int err);
549 extern int threadlocal_get_error (void);
550 extern void *threadlocal_buffer (size_t size);
551 extern void threadlocal_set_conn (struct connection *conn);
552 extern struct connection *threadlocal_get_conn (void);
553 extern struct context *threadlocal_get_context (void);
555 extern struct context *threadlocal_push_context (struct context *ctx);
556 extern void threadlocal_pop_context (struct context **ctx);
557 #define CLEANUP_CONTEXT_POP __attribute__ ((cleanup (threadlocal_pop_context)))
558 #define PUSH_CONTEXT_FOR_SCOPE(ctx) \
559 CLEANUP_CONTEXT_POP CLANG_UNUSED_VARIABLE_WORKAROUND \
560 struct context *NBDKIT_UNIQUE_NAME (_ctx) = threadlocal_push_context (ctx)
562 /* Macro which sets local variable struct connection *conn from
563 * thread-local storage, asserting that it is non-NULL. If you want
564 * to check if conn could be NULL (eg. outside a connection context)
565 * then call threadlocal_get_conn instead.
567 #define GET_CONN \
568 struct connection *conn = threadlocal_get_conn (); \
569 assert (conn != NULL)
571 /* exports.c */
572 extern int exports_resolve_default (struct nbdkit_exports *exports,
573 struct backend *b, int readonly);
575 #endif /* NBDKIT_INTERNAL_H */