2 * Copyright (C) 2013-2020 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
39 #include <sys/socket.h>
42 #define NBDKIT_API_VERSION 2
43 #include "nbdkit-plugin.h"
44 #include "nbdkit-filter.h"
46 #include "nbd-protocol.h"
48 /* Define unlikely macro, but only for GCC. These are used to move
49 * debug and error handling code out of hot paths.
52 #define unlikely(x) __builtin_expect (!!(x), 0)
53 #define if_verbose if (unlikely (verbose))
55 #define unlikely(x) (x)
56 #define if_verbose if (verbose)
60 #define UNIX_PATH_MAX 104
62 #define UNIX_PATH_MAX 108
66 # include <valgrind.h>
67 /* http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
68 # define DO_DLCLOSE !RUNNING_ON_VALGRIND
69 #elif defined(__SANITIZE_ADDRESS__)
71 #elif ENABLE_LIBFUZZER
72 /* XXX This causes dlopen in the server to leak during fuzzing.
73 * However it is necessary because of
74 * https://bugs.llvm.org/show_bug.cgi?id=43917
81 #define container_of(ptr, type, member) ({ \
82 const typeof (((type *) 0)->member) *__mptr = (ptr); \
83 (type *) ((char *) __mptr - offsetof(type, member)); \
86 /* Maximum read or write request that we will handle. */
87 #define MAX_REQUEST_SIZE (64 * 1024 * 1024)
91 LOG_TO_DEFAULT
, /* --log not specified: log to stderr, unless
92 we forked into the background in which
94 LOG_TO_STDERR
, /* --log=stderr forced on the command line */
95 LOG_TO_SYSLOG
, /* --log=syslog forced on the command line */
96 LOG_TO_NULL
, /* --log=null forced on the command line */
99 extern struct debug_flag
*debug_flags
;
100 extern const char *exportname
;
101 extern bool foreground
;
102 extern const char *ipaddr
;
103 extern enum log_to log_to
;
104 extern unsigned mask_handshake
;
105 extern bool newstyle
;
107 extern const char *port
;
108 extern bool read_only
;
109 extern const char *run
;
110 extern bool listen_stdin
;
111 extern const char *selinux_label
;
112 extern unsigned threads
;
114 extern const char *tls_certificates_dir
;
115 extern const char *tls_psk
;
116 extern bool tls_verify_peer
;
117 extern char *unixsocket
;
118 extern const char *user
, *group
;
121 extern struct backend
*backend
;
122 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
125 extern volatile int quit
;
127 extern void set_up_quit_pipe (void);
128 extern void close_quit_pipe (void);
129 extern void handle_quit (int sig
);
130 extern void set_quit (void);
133 extern void set_up_signals (void);
136 extern bool forked_into_background
;
137 extern void fork_into_background (void);
140 extern void run_command (void);
142 /* socket-activation.c */
143 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
144 extern unsigned int get_socket_activation (void);
147 extern void change_user (void);
152 /* Flags for connection_send_function */
154 SEND_MORE
= 1, /* Hint to use MSG_MORE/corking to group send()s */
157 typedef int (*connection_recv_function
) (struct connection
*,
158 void *buf
, size_t len
)
159 __attribute__((__nonnull__ (1, 2)));
160 typedef int (*connection_send_function
) (struct connection
*,
161 const void *buf
, size_t len
,
163 __attribute__((__nonnull__ (1, 2)));
164 typedef void (*connection_close_function
) (struct connection
*)
165 __attribute__((__nonnull__ (1)));
168 HANDLE_OPEN
= 1, /* Set if .open passed, so .close is needed */
169 HANDLE_CONNECTED
= 2, /* Set if .prepare passed, so .finalize is needed */
170 HANDLE_FAILED
= 4, /* Set if .finalize failed */
173 struct b_conn_handle
{
176 unsigned char state
; /* Bitmask of HANDLE_* values */
194 reset_b_conn_handle (struct b_conn_handle
*h
)
201 h
->is_rotational
= -1;
204 h
->can_fast_zero
= -1;
206 h
->can_multi_conn
= -1;
214 pthread_mutex_t request_lock
;
215 pthread_mutex_t read_lock
;
216 pthread_mutex_t write_lock
;
217 pthread_mutex_t status_lock
;
218 int status
; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
219 int status_pipe
[2]; /* track status changes via poll when nworkers > 1 */
220 void *crypto_session
;
223 struct b_conn_handle
*handles
;
226 char exportname
[NBD_MAX_STRING
+ 1];
227 uint32_t exportnamelen
;
231 bool structured_replies
;
232 bool meta_context_base_allocation
;
235 connection_recv_function recv
;
236 connection_send_function send
;
237 connection_close_function close
;
240 extern void handle_single_connection (int sockin
, int sockout
);
241 extern int connection_get_status (struct connection
*conn
)
242 __attribute__((__nonnull__ (1)));
243 extern int connection_set_status (struct connection
*conn
, int value
)
244 __attribute__((__nonnull__ (1)));
246 /* protocol-handshake.c */
247 extern int protocol_handshake (struct connection
*conn
)
248 __attribute__((__nonnull__ (1)));
249 extern int protocol_common_open (struct connection
*conn
,
250 uint64_t *exportsize
, uint16_t *flags
)
251 __attribute__((__nonnull__ (1, 2, 3)));
253 /* protocol-handshake-oldstyle.c */
254 extern int protocol_handshake_oldstyle (struct connection
*conn
)
255 __attribute__((__nonnull__ (1)));
257 /* protocol-handshake-newstyle.c */
258 extern int protocol_handshake_newstyle (struct connection
*conn
)
259 __attribute__((__nonnull__ (1)));
262 extern int protocol_recv_request_send_reply (struct connection
*conn
)
263 __attribute__((__nonnull__ (1)));
265 /* The context ID of base:allocation. As far as I can tell it doesn't
266 * matter what this is as long as nbdkit always returns the same
269 #define base_allocation_id 1
272 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
273 extern void crypto_init (bool tls_set_on_cli
);
274 extern void crypto_free (void);
275 extern int crypto_negotiate_tls (struct connection
*conn
,
276 int sockin
, int sockout
)
277 __attribute__((__nonnull__ (1)));
280 #define debug(fs, ...) \
283 nbdkit_debug ((fs), ##__VA_ARGS__); \
287 extern void add_debug_flag (const char *arg
);
288 extern void apply_debug_flags (void *dl
, const char *name
);
289 extern void free_debug_flags (void);
292 #if !HAVE_VFPRINTF_PERCENT_M
294 #define vfprintf nbdkit_vfprintf
295 extern int nbdkit_vfprintf (FILE *f
, const char *fmt
, va_list args
)
296 __attribute__((__format__ (printf
, 2, 0)));
298 extern void log_stderr_verror (const char *fs
, va_list args
)
299 __attribute__((__format__ (printf
, 1, 0)));
300 extern void log_syslog_verror (const char *fs
, va_list args
)
301 __attribute__((__format__ (printf
, 1, 0)));
305 /* Next filter or plugin in the chain. This is always NULL for
306 * plugins and never NULL for filters.
308 struct backend
*next
;
310 /* A unique index used to fetch the handle from the connections
311 * object. The plugin (last in the chain) has index 0, and the
312 * filters have index 1, 2, ... depending how "far" they are from
317 /* The type of backend: filter or plugin. */
320 /* A copy of the backend name that survives a dlclose. */
323 /* The file the backend was loaded from. */
326 /* The dlopen handle for the backend. */
329 /* Backend callbacks. All are required. */
330 void (*free
) (struct backend
*);
331 int (*thread_model
) (struct backend
*);
332 const char *(*plugin_name
) (struct backend
*);
333 void (*usage
) (struct backend
*);
334 const char *(*version
) (struct backend
*);
335 void (*dump_fields
) (struct backend
*);
336 void (*config
) (struct backend
*, const char *key
, const char *value
);
337 void (*config_complete
) (struct backend
*);
338 const char *(*magic_config_key
) (struct backend
*);
339 int (*preconnect
) (struct backend
*, struct connection
*conn
, int readonly
);
340 void *(*open
) (struct backend
*, struct connection
*conn
, int readonly
);
341 int (*prepare
) (struct backend
*, struct connection
*conn
, void *handle
,
343 int (*finalize
) (struct backend
*, struct connection
*conn
, void *handle
);
344 void (*close
) (struct backend
*, struct connection
*conn
, void *handle
);
346 int64_t (*get_size
) (struct backend
*, struct connection
*conn
, void *handle
);
347 int (*can_write
) (struct backend
*, struct connection
*conn
, void *handle
);
348 int (*can_flush
) (struct backend
*, struct connection
*conn
, void *handle
);
349 int (*is_rotational
) (struct backend
*, struct connection
*conn
,
351 int (*can_trim
) (struct backend
*, struct connection
*conn
, void *handle
);
352 int (*can_zero
) (struct backend
*, struct connection
*conn
, void *handle
);
353 int (*can_fast_zero
) (struct backend
*, struct connection
*conn
,
355 int (*can_extents
) (struct backend
*, struct connection
*conn
, void *handle
);
356 int (*can_fua
) (struct backend
*, struct connection
*conn
, void *handle
);
357 int (*can_multi_conn
) (struct backend
*, struct connection
*conn
,
359 int (*can_cache
) (struct backend
*, struct connection
*conn
, void *handle
);
360 int (*init_sparse
) (struct backend
*, struct connection
*conn
, void *handle
);
361 int (*init_zero
) (struct backend
*, struct connection
*conn
, void *handle
);
363 int (*pread
) (struct backend
*, struct connection
*conn
, void *handle
,
364 void *buf
, uint32_t count
, uint64_t offset
,
365 uint32_t flags
, int *err
);
366 int (*pwrite
) (struct backend
*, struct connection
*conn
, void *handle
,
367 const void *buf
, uint32_t count
, uint64_t offset
,
368 uint32_t flags
, int *err
);
369 int (*flush
) (struct backend
*, struct connection
*conn
, void *handle
,
370 uint32_t flags
, int *err
);
371 int (*trim
) (struct backend
*, struct connection
*conn
, void *handle
,
372 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
373 int (*zero
) (struct backend
*, struct connection
*conn
, void *handle
,
374 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
375 int (*extents
) (struct backend
*, struct connection
*conn
, void *handle
,
376 uint32_t count
, uint64_t offset
, uint32_t flags
,
377 struct nbdkit_extents
*extents
, int *err
);
378 int (*cache
) (struct backend
*, struct connection
*conn
, void *handle
,
379 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
382 extern void backend_init (struct backend
*b
, struct backend
*next
, size_t index
,
383 const char *filename
, void *dl
, const char *type
)
384 __attribute__((__nonnull__ (1, 4, 5, 6)));
385 extern void backend_load (struct backend
*b
, const char *name
,
387 __attribute__((__nonnull__ (1 /* not 2 */)));
388 extern void backend_unload (struct backend
*b
, void (*unload
) (void))
389 __attribute__((__nonnull__ (1)));
391 extern int backend_open (struct backend
*b
, struct connection
*conn
,
393 __attribute__((__nonnull__ (1, 2)));
394 extern int backend_prepare (struct backend
*b
, struct connection
*conn
)
395 __attribute__((__nonnull__ (1, 2)));
396 extern int backend_finalize (struct backend
*b
, struct connection
*conn
)
397 __attribute__((__nonnull__ (1, 2)));
398 extern void backend_close (struct backend
*b
, struct connection
*conn
)
399 __attribute__((__nonnull__ (1, 2)));
400 extern bool backend_valid_range (struct backend
*b
, struct connection
*conn
,
401 uint64_t offset
, uint32_t count
)
402 __attribute__((__nonnull__ (1, 2)));
404 extern int backend_reopen (struct backend
*b
, struct connection
*conn
,
406 __attribute__((__nonnull__ (1, 2)));
407 extern int64_t backend_get_size (struct backend
*b
, struct connection
*conn
)
408 __attribute__((__nonnull__ (1, 2)));
409 extern int backend_can_write (struct backend
*b
, struct connection
*conn
)
410 __attribute__((__nonnull__ (1, 2)));
411 extern int backend_can_flush (struct backend
*b
, struct connection
*conn
)
412 __attribute__((__nonnull__ (1, 2)));
413 extern int backend_is_rotational (struct backend
*b
, struct connection
*conn
)
414 __attribute__((__nonnull__ (1, 2)));
415 extern int backend_can_trim (struct backend
*b
, struct connection
*conn
)
416 __attribute__((__nonnull__ (1, 2)));
417 extern int backend_can_zero (struct backend
*b
, struct connection
*conn
)
418 __attribute__((__nonnull__ (1, 2)));
419 extern int backend_can_fast_zero (struct backend
*b
, struct connection
*conn
)
420 __attribute__((__nonnull__ (1, 2)));
421 extern int backend_can_extents (struct backend
*b
, struct connection
*conn
)
422 __attribute__((__nonnull__ (1, 2)));
423 extern int backend_can_fua (struct backend
*b
, struct connection
*conn
)
424 __attribute__((__nonnull__ (1, 2)));
425 extern int backend_can_multi_conn (struct backend
*b
, struct connection
*conn
)
426 __attribute__((__nonnull__ (1, 2)));
427 extern int backend_can_cache (struct backend
*b
, struct connection
*conn
)
428 __attribute__((__nonnull__ (1, 2)));
429 extern int backend_init_sparse (struct backend
*b
, struct connection
*conn
)
430 __attribute__((__nonnull__ (1, 2)));
431 extern int backend_init_zero (struct backend
*b
, struct connection
*conn
)
432 __attribute__((__nonnull__ (1, 2)));
434 extern int backend_pread (struct backend
*b
, struct connection
*conn
,
435 void *buf
, uint32_t count
, uint64_t offset
,
436 uint32_t flags
, int *err
)
437 __attribute__((__nonnull__ (1, 2, 3, 7)));
438 extern int backend_pwrite (struct backend
*b
, struct connection
*conn
,
439 const void *buf
, uint32_t count
, uint64_t offset
,
440 uint32_t flags
, int *err
)
441 __attribute__((__nonnull__ (1, 2, 3, 7)));
442 extern int backend_flush (struct backend
*b
, struct connection
*conn
,
443 uint32_t flags
, int *err
)
444 __attribute__((__nonnull__ (1, 2, 4)));
445 extern int backend_trim (struct backend
*b
, struct connection
*conn
,
446 uint32_t count
, uint64_t offset
, uint32_t flags
,
448 __attribute__((__nonnull__ (1, 2, 6)));
449 extern int backend_zero (struct backend
*b
, struct connection
*conn
,
450 uint32_t count
, uint64_t offset
, uint32_t flags
,
452 __attribute__((__nonnull__ (1, 2, 6)));
453 extern int backend_extents (struct backend
*b
, struct connection
*conn
,
454 uint32_t count
, uint64_t offset
, uint32_t flags
,
455 struct nbdkit_extents
*extents
, int *err
)
456 __attribute__((__nonnull__ (1, 2, 6, 7)));
457 extern int backend_cache (struct backend
*b
, struct connection
*conn
,
458 uint32_t count
, uint64_t offset
,
459 uint32_t flags
, int *err
)
460 __attribute__((__nonnull__ (1, 2, 6)));
463 extern struct backend
*plugin_register (size_t index
, const char *filename
,
464 void *dl
, struct nbdkit_plugin
*(*plugin_init
) (void))
465 __attribute__((__nonnull__ (2, 3, 4)));
468 extern struct backend
*filter_register (struct backend
*next
, size_t index
,
469 const char *filename
, void *dl
,
470 struct nbdkit_filter
*(*filter_init
) (void))
471 __attribute__((__nonnull__ (1, 3, 4, 5)));
474 extern void lock_init_thread_model (void);
475 extern const char *name_of_thread_model (int model
);
476 extern void lock_connection (void);
477 extern void unlock_connection (void);
478 extern void lock_request (struct connection
*conn
);
479 extern void unlock_request (struct connection
*conn
);
480 extern void lock_unload (void);
481 extern void unlock_unload (void);
484 extern int *bind_unix_socket (size_t *)
485 __attribute__((__nonnull__ (1)));
486 extern int *bind_tcpip_socket (size_t *)
487 __attribute__((__nonnull__ (1)));
488 extern int *bind_vsock (size_t *)
489 __attribute__((__nonnull__ (1)));
490 extern void accept_incoming_connections (int *socks
, size_t nr_socks
)
491 __attribute__((__nonnull__ (1)));
494 extern void threadlocal_init (void);
495 extern void threadlocal_new_server_thread (void);
496 extern void threadlocal_set_name (const char *name
)
497 __attribute__((__nonnull__ (1)));
498 extern const char *threadlocal_get_name (void);
499 extern void threadlocal_set_instance_num (size_t instance_num
);
500 extern size_t threadlocal_get_instance_num (void);
501 extern void threadlocal_set_error (int err
);
502 extern int threadlocal_get_error (void);
503 extern void *threadlocal_buffer (size_t size
);
504 extern void threadlocal_set_conn (struct connection
*conn
);
505 extern struct connection
*threadlocal_get_conn (void);
507 /* Declare program_name. */
508 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
510 #define program_name program_invocation_short_name
512 #define program_name "nbdkit"
515 #endif /* NBDKIT_INTERNAL_H */