Version 1.33.7.
[nbdkit.git] / server / internal.h
blob77fdc21d0ed5ece2421823a998c798bc8a2ad97d
1 /* nbdkit
2 * Copyright (C) 2013-2022 Red Hat Inc.
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 pthread_mutex_t request_lock;
248 pthread_mutex_t read_lock;
249 pthread_mutex_t write_lock;
250 pthread_mutex_t status_lock;
252 conn_status status;
253 int status_pipe[2]; /* track status changes via poll when nworkers > 1 */
254 void *crypto_session;
255 int nworkers;
257 struct context *top_context; /* The context tied to 'top'. */
258 char **default_exportname; /* One per plugin and filter. */
260 uint32_t cflags;
261 uint16_t eflags;
262 bool handshake_complete;
263 bool using_tls;
264 bool structured_replies;
265 bool meta_context_base_allocation;
267 string_vector interns;
268 char *exportname_from_set_meta_context;
269 const char *exportname;
271 int sockin, sockout;
272 connection_recv_function recv;
273 connection_send_function send;
274 connection_close_function close;
277 extern void handle_single_connection (int sockin, int sockout);
278 extern conn_status connection_get_status (void);
279 extern void connection_set_status (conn_status value);
281 /* protocol-handshake.c */
282 extern int protocol_handshake (void);
283 extern int protocol_common_open (uint64_t *exportsize, uint16_t *flags,
284 const char *exportname)
285 __attribute__((__nonnull__ (1, 2, 3)));
287 /* protocol-handshake-oldstyle.c */
288 extern int protocol_handshake_oldstyle (void);
290 /* protocol-handshake-newstyle.c */
291 extern int protocol_handshake_newstyle (void);
293 /* protocol.c */
294 extern void protocol_recv_request_send_reply (void);
296 /* The context ID of base:allocation. As far as I can tell it doesn't
297 * matter what this is as long as nbdkit always returns the same
298 * number.
300 #define base_allocation_id 1
302 /* public.c */
303 extern void free_interns (void);
305 /* crypto.c */
306 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
307 extern void crypto_init (bool tls_set_on_cli);
308 extern void crypto_free (void);
309 extern int crypto_negotiate_tls (int sockin, int sockout);
311 /* debug-flags.c */
312 extern void add_debug_flag (const char *arg);
313 extern void apply_debug_flags (void *dl, const char *name);
314 extern void free_debug_flags (void);
316 /* log.c */
317 extern void log_verror (const char *fs, va_list args);
319 /* log-*.c */
320 extern void log_stderr_verror (const char *fs, va_list args)
321 ATTRIBUTE_FORMAT_PRINTF(1, 0);
322 extern void log_syslog_verror (const char *fs, va_list args)
323 ATTRIBUTE_FORMAT_PRINTF(1, 0);
325 /* vfprintf.c */
326 #if !HAVE_VFPRINTF_PERCENT_M
327 #include <stdio.h>
328 #define vfprintf replace_vfprintf
329 extern int replace_vfprintf (FILE *f, const char *fmt, va_list args)
330 __attribute__((__format__ (printf, 2, 0)));
331 #endif
333 /* backend.c */
334 struct backend {
335 /* Next filter or plugin in the chain. This is always NULL for
336 * plugins and never NULL for filters.
338 struct backend *next;
340 /* A unique index used to fetch the handle from the connections
341 * object. The plugin (last in the chain) has index 0, and the
342 * filters have index 1, 2, ... depending how "far" they are from
343 * the plugin.
345 size_t i;
347 /* The type of backend: filter or plugin. */
348 const char *type;
350 /* A copy of the backend name that survives a dlclose. */
351 char *name;
353 /* The file the backend was loaded from. */
354 char *filename;
356 /* The dlopen handle for the backend. */
357 void *dl;
359 /* Backend callbacks. All are required. */
360 void (*free) (struct backend *);
361 int (*thread_model) (struct backend *);
362 const char *(*plugin_name) (struct backend *);
363 void (*usage) (struct backend *);
364 const char *(*version) (struct backend *);
365 void (*dump_fields) (struct backend *);
366 void (*config) (struct backend *, const char *key, const char *value);
367 void (*config_complete) (struct backend *);
368 const char *(*magic_config_key) (struct backend *);
369 void (*get_ready) (struct backend *);
370 void (*after_fork) (struct backend *);
371 void (*cleanup) (struct backend *);
373 int (*preconnect) (struct backend *, int readonly);
374 int (*list_exports) (struct backend *, int readonly, int is_tls,
375 struct nbdkit_exports *exports);
376 const char *(*default_export) (struct backend *, int readonly, int is_tls);
377 void *(*open) (struct context *, int readonly, const char *exportname,
378 int is_tls);
379 int (*prepare) (struct context *, int readonly);
380 int (*finalize) (struct context *);
381 void (*close) (struct context *);
383 const char *(*export_description) (struct context *);
384 int64_t (*get_size) (struct context *);
385 int (*block_size) (struct context *,
386 uint32_t *minimum, uint32_t *preferred, uint32_t *maximum);
387 int (*can_write) (struct context *);
388 int (*can_flush) (struct context *);
389 int (*is_rotational) (struct context *);
390 int (*can_trim) (struct context *);
391 int (*can_zero) (struct context *);
392 int (*can_fast_zero) (struct context *);
393 int (*can_extents) (struct context *);
394 int (*can_fua) (struct context *);
395 int (*can_multi_conn) (struct context *);
396 int (*can_cache) (struct context *);
398 int (*pread) (struct context *,
399 void *buf, uint32_t count, uint64_t offset,
400 uint32_t flags, int *err);
401 int (*pwrite) (struct context *,
402 const void *buf, uint32_t count, uint64_t offset,
403 uint32_t flags, int *err);
404 int (*flush) (struct context *, uint32_t flags, int *err);
405 int (*trim) (struct context *,
406 uint32_t count, uint64_t offset, uint32_t flags, int *err);
407 int (*zero) (struct context *,
408 uint32_t count, uint64_t offset, uint32_t flags, int *err);
409 int (*extents) (struct context *,
410 uint32_t count, uint64_t offset, uint32_t flags,
411 struct nbdkit_extents *extents, int *err);
412 int (*cache) (struct context *,
413 uint32_t count, uint64_t offset, uint32_t flags, int *err);
416 extern void backend_init (struct backend *b, struct backend *next, size_t index,
417 const char *filename, void *dl, const char *type)
418 __attribute__((__nonnull__ (1, 4, 5, 6)));
419 extern void backend_load (struct backend *b, const char *name,
420 void (*load) (void))
421 __attribute__((__nonnull__ (1 /* not 2 */)));
422 extern void backend_unload (struct backend *b, void (*unload) (void))
423 __attribute__((__nonnull__ (1)));
425 extern int backend_list_exports (struct backend *b, int readonly,
426 struct nbdkit_exports *exports)
427 __attribute__((__nonnull__ (1, 3)));
428 extern const char *backend_default_export (struct backend *b, int readonly)
429 __attribute__((__nonnull__ (1)));
430 /* exportname is only valid for this call and almost certainly will be
431 * freed on return of this function, so backends must save the
432 * exportname if they need to refer to it later.
434 extern struct context *backend_open (struct backend *b,
435 int readonly, const char *exportname,
436 int shared)
437 __attribute__((__nonnull__ (1, 3)));
438 extern int backend_prepare (struct context *c)
439 __attribute__((__nonnull__ (1)));
440 extern int backend_finalize (struct context *c)
441 __attribute__((__nonnull__ (1)));
442 extern void backend_close (struct context *c)
443 __attribute__((__nonnull__ (1)));
444 extern bool backend_valid_range (struct context *c,
445 uint64_t offset, uint32_t count)
446 __attribute__((__nonnull__ (1)));
448 extern const char *backend_export_description (struct context *c)
449 __attribute__((__nonnull__ (1)));
450 extern int64_t backend_get_size (struct context *c)
451 __attribute__((__nonnull__ (1)));
452 extern int backend_block_size (struct context *c,
453 uint32_t *minimum, uint32_t *preferred,
454 uint32_t *maximum)
455 __attribute__((__nonnull__ (1, 2, 3, 4)));
456 extern int backend_can_write (struct context *c)
457 __attribute__((__nonnull__ (1)));
458 extern int backend_can_flush (struct context *c)
459 __attribute__((__nonnull__ (1)));
460 extern int backend_is_rotational (struct context *c)
461 __attribute__((__nonnull__ (1)));
462 extern int backend_can_trim (struct context *c)
463 __attribute__((__nonnull__ (1)));
464 extern int backend_can_zero (struct context *c)
465 __attribute__((__nonnull__ (1)));
466 extern int backend_can_fast_zero (struct context *c)
467 __attribute__((__nonnull__ (1)));
468 extern int backend_can_extents (struct context *c)
469 __attribute__((__nonnull__ (1)));
470 extern int backend_can_fua (struct context *c)
471 __attribute__((__nonnull__ (1)));
472 extern int backend_can_multi_conn (struct context *c)
473 __attribute__((__nonnull__ (1)));
474 extern int backend_can_cache (struct context *c)
475 __attribute__((__nonnull__ (1)));
477 extern int backend_pread (struct context *c,
478 void *buf, uint32_t count, uint64_t offset,
479 uint32_t flags, int *err)
480 __attribute__((__nonnull__ (1, 2, 6)));
481 extern int backend_pwrite (struct context *c,
482 const void *buf, uint32_t count, uint64_t offset,
483 uint32_t flags, int *err)
484 __attribute__((__nonnull__ (1, 2, 6)));
485 extern int backend_flush (struct context *c,
486 uint32_t flags, int *err)
487 __attribute__((__nonnull__ (1, 3)));
488 extern int backend_trim (struct context *c,
489 uint32_t count, uint64_t offset, uint32_t flags,
490 int *err)
491 __attribute__((__nonnull__ (1, 5)));
492 extern int backend_zero (struct context *c,
493 uint32_t count, uint64_t offset, uint32_t flags,
494 int *err)
495 __attribute__((__nonnull__ (1, 5)));
496 extern int backend_extents (struct context *c,
497 uint32_t count, uint64_t offset, uint32_t flags,
498 struct nbdkit_extents *extents, int *err)
499 __attribute__((__nonnull__ (1, 5, 6)));
500 extern int backend_cache (struct context *c,
501 uint32_t count, uint64_t offset,
502 uint32_t flags, int *err)
503 __attribute__((__nonnull__ (1, 5)));
505 /* plugins.c */
506 extern struct backend *plugin_register (size_t index, const char *filename,
507 void *dl, struct nbdkit_plugin *(*plugin_init) (void))
508 __attribute__((__nonnull__ (2, 3, 4)));
510 /* filters.c */
511 extern struct backend *filter_register (struct backend *next, size_t index,
512 const char *filename, void *dl,
513 struct nbdkit_filter *(*filter_init) (void))
514 __attribute__((__nonnull__ (1, 3, 4, 5)));
516 /* locks.c */
517 extern unsigned thread_model;
518 extern void lock_init_thread_model (void);
519 extern const char *name_of_thread_model (int model);
520 extern void lock_connection (void);
521 extern void unlock_connection (void);
522 extern void lock_request (void);
523 extern void unlock_request (void);
524 extern void lock_unload (void);
525 extern void unlock_unload (void);
527 /* sockets.c */
528 DEFINE_VECTOR_TYPE(sockets, int);
529 extern void bind_unix_socket (sockets *) __attribute__((__nonnull__ (1)));
530 extern void bind_tcpip_socket (sockets *) __attribute__((__nonnull__ (1)));
531 extern void bind_vsock (sockets *) __attribute__((__nonnull__ (1)));
532 extern void accept_incoming_connections (const sockets *socks)
533 __attribute__((__nonnull__ (1)));
535 /* threadlocal.c */
536 extern void threadlocal_init (void);
537 extern void threadlocal_new_server_thread (void);
538 extern void threadlocal_set_name (const char *name)
539 __attribute__((__nonnull__ (1)));
540 extern const char *threadlocal_get_name (void);
541 extern void threadlocal_set_instance_num (size_t instance_num);
542 extern size_t threadlocal_get_instance_num (void);
543 extern void threadlocal_set_error (int err);
544 extern int threadlocal_get_error (void);
545 extern void *threadlocal_buffer (size_t size);
546 extern void threadlocal_set_conn (struct connection *conn);
547 extern struct connection *threadlocal_get_conn (void);
548 extern struct context *threadlocal_get_context (void);
550 extern struct context *threadlocal_push_context (struct context *ctx);
551 extern void threadlocal_pop_context (struct context **ctx);
552 #define CLEANUP_CONTEXT_POP __attribute__((cleanup (threadlocal_pop_context)))
553 #define PUSH_CONTEXT_FOR_SCOPE(ctx) \
554 CLEANUP_CONTEXT_POP CLANG_UNUSED_VARIABLE_WORKAROUND \
555 struct context *NBDKIT_UNIQUE_NAME(_ctx) = threadlocal_push_context (ctx)
557 /* Macro which sets local variable struct connection *conn from
558 * thread-local storage, asserting that it is non-NULL. If you want
559 * to check if conn could be NULL (eg. outside a connection context)
560 * then call threadlocal_get_conn instead.
562 #define GET_CONN \
563 struct connection *conn = threadlocal_get_conn (); \
564 assert (conn != NULL)
566 /* exports.c */
567 extern int exports_resolve_default (struct nbdkit_exports *exports,
568 struct backend *b, int readonly);
570 #endif /* NBDKIT_INTERNAL_H */