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
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
33 #ifndef NBDKIT_INTERNAL_H
34 #define NBDKIT_INTERNAL_H
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
45 #define NBDKIT_API_VERSION 2
46 #define NBDKIT_INTERNAL
47 #include "nbdkit-plugin.h"
48 #include "nbdkit-filter.h"
50 #include "nbd-protocol.h"
51 #include "string-vector.h"
52 #include "unix-path-max.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.
60 #define unlikely(x) __builtin_expect (!!(x), 0)
61 #define if_verbose if (unlikely (verbose))
63 #define unlikely(x) (x)
64 #define if_verbose if (verbose)
67 #if defined(__SANITIZE_ADDRESS__)
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
77 # include <valgrind.h>
78 /* http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
79 # define DO_DLCLOSE !RUNNING_ON_VALGRIND
85 /* Declare program_name. */
86 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
88 #define program_name program_invocation_short_name
90 #define program_name "nbdkit"
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)
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
;
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
;
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
;
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)
152 #define debug(fs, ...) \
155 debug_in_server ((fs), ##__VA_ARGS__); \
158 extern void debug_in_server (const char *msg
, ...)
159 ATTRIBUTE_FORMAT_PRINTF (1, 2);
162 extern volatile int quit
;
166 extern HANDLE quit_fd
;
168 extern void set_up_quit_pipe (void);
169 extern void close_quit_pipe (void);
170 extern void handle_quit (int sig
);
173 extern void set_up_signals (void);
176 extern bool forked_into_background
;
177 extern void fork_into_background (void);
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);
187 extern void change_user (void);
191 /* Flags for connection_send_function */
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
,
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
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 */
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 */
224 uint32_t minimum_block_size
;
225 uint32_t preferred_block_size
;
226 uint32_t maximum_block_size
;
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 */
247 pthread_mutex_t request_lock
;
248 pthread_mutex_t read_lock
;
249 pthread_mutex_t write_lock
;
250 pthread_mutex_t status_lock
;
253 int status_pipe
[2]; /* track status changes via poll when nworkers > 1 */
254 void *crypto_session
;
257 struct context
*top_context
; /* The context tied to 'top'. */
258 char **default_exportname
; /* One per plugin and filter. */
262 bool handshake_complete
;
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
;
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);
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
300 #define base_allocation_id 1
303 extern void free_interns (void);
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
);
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);
317 extern void log_verror (const char *fs
, va_list args
);
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);
326 #if !HAVE_VFPRINTF_PERCENT_M
328 #define vfprintf replace_vfprintf
329 extern int replace_vfprintf (FILE *f
, const char *fmt
, va_list args
)
330 __attribute__((__format__ (printf
, 2, 0)));
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
347 /* The type of backend: filter or plugin. */
350 /* A copy of the backend name that survives a dlclose. */
353 /* The file the backend was loaded from. */
356 /* The dlopen handle for the backend. */
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
,
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
,
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
,
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
,
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
,
491 __attribute__((__nonnull__ (1, 5)));
492 extern int backend_zero (struct context
*c
,
493 uint32_t count
, uint64_t offset
, uint32_t flags
,
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)));
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)));
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)));
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);
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)));
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.
563 struct connection *conn = threadlocal_get_conn (); \
564 assert (conn != NULL)
567 extern int exports_resolve_default (struct nbdkit_exports
*exports
,
568 struct backend
*b
, int readonly
);
570 #endif /* NBDKIT_INTERNAL_H */