2 * Copyright (C) 2013-2019 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 */
192 reset_b_conn_handle (struct b_conn_handle
*h
)
199 h
->is_rotational
= -1;
202 h
->can_fast_zero
= -1;
204 h
->can_multi_conn
= -1;
210 pthread_mutex_t request_lock
;
211 pthread_mutex_t read_lock
;
212 pthread_mutex_t write_lock
;
213 pthread_mutex_t status_lock
;
214 int status
; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
215 int status_pipe
[2]; /* track status changes via poll when nworkers > 1 */
216 void *crypto_session
;
219 struct b_conn_handle
*handles
;
222 char exportname
[NBD_MAX_STRING
+ 1];
223 uint32_t exportnamelen
;
227 bool structured_replies
;
228 bool meta_context_base_allocation
;
231 connection_recv_function recv
;
232 connection_send_function send
;
233 connection_close_function close
;
236 extern void handle_single_connection (int sockin
, int sockout
);
237 extern int connection_get_status (struct connection
*conn
)
238 __attribute__((__nonnull__ (1)));
239 extern int connection_set_status (struct connection
*conn
, int value
)
240 __attribute__((__nonnull__ (1)));
242 /* protocol-handshake.c */
243 extern int protocol_handshake (struct connection
*conn
)
244 __attribute__((__nonnull__ (1)));
245 extern int protocol_common_open (struct connection
*conn
,
246 uint64_t *exportsize
, uint16_t *flags
)
247 __attribute__((__nonnull__ (1, 2, 3)));
249 /* protocol-handshake-oldstyle.c */
250 extern int protocol_handshake_oldstyle (struct connection
*conn
)
251 __attribute__((__nonnull__ (1)));
253 /* protocol-handshake-newstyle.c */
254 extern int protocol_handshake_newstyle (struct connection
*conn
)
255 __attribute__((__nonnull__ (1)));
258 extern int protocol_recv_request_send_reply (struct connection
*conn
)
259 __attribute__((__nonnull__ (1)));
261 /* The context ID of base:allocation. As far as I can tell it doesn't
262 * matter what this is as long as nbdkit always returns the same
265 #define base_allocation_id 1
268 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
269 extern void crypto_init (bool tls_set_on_cli
);
270 extern void crypto_free (void);
271 extern int crypto_negotiate_tls (struct connection
*conn
,
272 int sockin
, int sockout
)
273 __attribute__((__nonnull__ (1)));
276 #define debug(fs, ...) \
279 nbdkit_debug ((fs), ##__VA_ARGS__); \
283 extern void add_debug_flag (const char *arg
);
284 extern void apply_debug_flags (void *dl
, const char *name
);
285 extern void free_debug_flags (void);
288 #if !HAVE_VFPRINTF_PERCENT_M
290 #define vfprintf nbdkit_vfprintf
291 extern int nbdkit_vfprintf (FILE *f
, const char *fmt
, va_list args
)
292 __attribute__((__format__ (printf
, 2, 0)));
294 extern void log_stderr_verror (const char *fs
, va_list args
)
295 __attribute__((__format__ (printf
, 1, 0)));
296 extern void log_syslog_verror (const char *fs
, va_list args
)
297 __attribute__((__format__ (printf
, 1, 0)));
301 /* Next filter or plugin in the chain. This is always NULL for
302 * plugins and never NULL for filters.
304 struct backend
*next
;
306 /* A unique index used to fetch the handle from the connections
307 * object. The plugin (last in the chain) has index 0, and the
308 * filters have index 1, 2, ... depending how "far" they are from
313 /* The type of backend: filter or plugin. */
316 /* A copy of the backend name that survives a dlclose. */
319 /* The file the backend was loaded from. */
322 /* The dlopen handle for the backend. */
325 /* Backend callbacks. All are required. */
326 void (*free
) (struct backend
*);
327 int (*thread_model
) (struct backend
*);
328 const char *(*plugin_name
) (struct backend
*);
329 void (*usage
) (struct backend
*);
330 const char *(*version
) (struct backend
*);
331 void (*dump_fields
) (struct backend
*);
332 void (*config
) (struct backend
*, const char *key
, const char *value
);
333 void (*config_complete
) (struct backend
*);
334 const char *(*magic_config_key
) (struct backend
*);
335 int (*preconnect
) (struct backend
*, struct connection
*conn
, int readonly
);
336 void *(*open
) (struct backend
*, struct connection
*conn
, int readonly
);
337 int (*prepare
) (struct backend
*, struct connection
*conn
, void *handle
,
339 int (*finalize
) (struct backend
*, struct connection
*conn
, void *handle
);
340 void (*close
) (struct backend
*, struct connection
*conn
, void *handle
);
342 int64_t (*get_size
) (struct backend
*, struct connection
*conn
, void *handle
);
343 int (*can_write
) (struct backend
*, struct connection
*conn
, void *handle
);
344 int (*can_flush
) (struct backend
*, struct connection
*conn
, void *handle
);
345 int (*is_rotational
) (struct backend
*, struct connection
*conn
,
347 int (*can_trim
) (struct backend
*, struct connection
*conn
, void *handle
);
348 int (*can_zero
) (struct backend
*, struct connection
*conn
, void *handle
);
349 int (*can_fast_zero
) (struct backend
*, struct connection
*conn
,
351 int (*can_extents
) (struct backend
*, struct connection
*conn
, void *handle
);
352 int (*can_fua
) (struct backend
*, struct connection
*conn
, void *handle
);
353 int (*can_multi_conn
) (struct backend
*, struct connection
*conn
,
355 int (*can_cache
) (struct backend
*, struct connection
*conn
, void *handle
);
357 int (*pread
) (struct backend
*, struct connection
*conn
, void *handle
,
358 void *buf
, uint32_t count
, uint64_t offset
,
359 uint32_t flags
, int *err
);
360 int (*pwrite
) (struct backend
*, struct connection
*conn
, void *handle
,
361 const void *buf
, uint32_t count
, uint64_t offset
,
362 uint32_t flags
, int *err
);
363 int (*flush
) (struct backend
*, struct connection
*conn
, void *handle
,
364 uint32_t flags
, int *err
);
365 int (*trim
) (struct backend
*, struct connection
*conn
, void *handle
,
366 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
367 int (*zero
) (struct backend
*, struct connection
*conn
, void *handle
,
368 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
369 int (*extents
) (struct backend
*, struct connection
*conn
, void *handle
,
370 uint32_t count
, uint64_t offset
, uint32_t flags
,
371 struct nbdkit_extents
*extents
, int *err
);
372 int (*cache
) (struct backend
*, struct connection
*conn
, void *handle
,
373 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
376 extern void backend_init (struct backend
*b
, struct backend
*next
, size_t index
,
377 const char *filename
, void *dl
, const char *type
)
378 __attribute__((__nonnull__ (1, 4, 5, 6)));
379 extern void backend_load (struct backend
*b
, const char *name
,
381 __attribute__((__nonnull__ (1 /* not 2 */)));
382 extern void backend_unload (struct backend
*b
, void (*unload
) (void))
383 __attribute__((__nonnull__ (1)));
385 extern int backend_open (struct backend
*b
, struct connection
*conn
,
387 __attribute__((__nonnull__ (1, 2)));
388 extern int backend_prepare (struct backend
*b
, struct connection
*conn
)
389 __attribute__((__nonnull__ (1, 2)));
390 extern int backend_finalize (struct backend
*b
, struct connection
*conn
)
391 __attribute__((__nonnull__ (1, 2)));
392 extern void backend_close (struct backend
*b
, struct connection
*conn
)
393 __attribute__((__nonnull__ (1, 2)));
394 extern bool backend_valid_range (struct backend
*b
, struct connection
*conn
,
395 uint64_t offset
, uint32_t count
)
396 __attribute__((__nonnull__ (1, 2)));
398 extern int backend_reopen (struct backend
*b
, struct connection
*conn
,
400 __attribute__((__nonnull__ (1, 2)));
401 extern int64_t backend_get_size (struct backend
*b
, struct connection
*conn
)
402 __attribute__((__nonnull__ (1, 2)));
403 extern int backend_can_write (struct backend
*b
, struct connection
*conn
)
404 __attribute__((__nonnull__ (1, 2)));
405 extern int backend_can_flush (struct backend
*b
, struct connection
*conn
)
406 __attribute__((__nonnull__ (1, 2)));
407 extern int backend_is_rotational (struct backend
*b
, struct connection
*conn
)
408 __attribute__((__nonnull__ (1, 2)));
409 extern int backend_can_trim (struct backend
*b
, struct connection
*conn
)
410 __attribute__((__nonnull__ (1, 2)));
411 extern int backend_can_zero (struct backend
*b
, struct connection
*conn
)
412 __attribute__((__nonnull__ (1, 2)));
413 extern int backend_can_fast_zero (struct backend
*b
, struct connection
*conn
)
414 __attribute__((__nonnull__ (1, 2)));
415 extern int backend_can_extents (struct backend
*b
, struct connection
*conn
)
416 __attribute__((__nonnull__ (1, 2)));
417 extern int backend_can_fua (struct backend
*b
, struct connection
*conn
)
418 __attribute__((__nonnull__ (1, 2)));
419 extern int backend_can_multi_conn (struct backend
*b
, struct connection
*conn
)
420 __attribute__((__nonnull__ (1, 2)));
421 extern int backend_can_cache (struct backend
*b
, struct connection
*conn
)
422 __attribute__((__nonnull__ (1, 2)));
424 extern int backend_pread (struct backend
*b
, struct connection
*conn
,
425 void *buf
, uint32_t count
, uint64_t offset
,
426 uint32_t flags
, int *err
)
427 __attribute__((__nonnull__ (1, 2, 3, 7)));
428 extern int backend_pwrite (struct backend
*b
, struct connection
*conn
,
429 const void *buf
, uint32_t count
, uint64_t offset
,
430 uint32_t flags
, int *err
)
431 __attribute__((__nonnull__ (1, 2, 3, 7)));
432 extern int backend_flush (struct backend
*b
, struct connection
*conn
,
433 uint32_t flags
, int *err
)
434 __attribute__((__nonnull__ (1, 2, 4)));
435 extern int backend_trim (struct backend
*b
, struct connection
*conn
,
436 uint32_t count
, uint64_t offset
, uint32_t flags
,
438 __attribute__((__nonnull__ (1, 2, 6)));
439 extern int backend_zero (struct backend
*b
, struct connection
*conn
,
440 uint32_t count
, uint64_t offset
, uint32_t flags
,
442 __attribute__((__nonnull__ (1, 2, 6)));
443 extern int backend_extents (struct backend
*b
, struct connection
*conn
,
444 uint32_t count
, uint64_t offset
, uint32_t flags
,
445 struct nbdkit_extents
*extents
, int *err
)
446 __attribute__((__nonnull__ (1, 2, 6, 7)));
447 extern int backend_cache (struct backend
*b
, struct connection
*conn
,
448 uint32_t count
, uint64_t offset
,
449 uint32_t flags
, int *err
)
450 __attribute__((__nonnull__ (1, 2, 6)));
453 extern struct backend
*plugin_register (size_t index
, const char *filename
,
454 void *dl
, struct nbdkit_plugin
*(*plugin_init
) (void))
455 __attribute__((__nonnull__ (2, 3, 4)));
458 extern struct backend
*filter_register (struct backend
*next
, size_t index
,
459 const char *filename
, void *dl
,
460 struct nbdkit_filter
*(*filter_init
) (void))
461 __attribute__((__nonnull__ (1, 3, 4, 5)));
464 extern void lock_init_thread_model (void);
465 extern const char *name_of_thread_model (int model
);
466 extern void lock_connection (void);
467 extern void unlock_connection (void);
468 extern void lock_request (struct connection
*conn
);
469 extern void unlock_request (struct connection
*conn
);
470 extern void lock_unload (void);
471 extern void unlock_unload (void);
474 extern int *bind_unix_socket (size_t *)
475 __attribute__((__nonnull__ (1)));
476 extern int *bind_tcpip_socket (size_t *)
477 __attribute__((__nonnull__ (1)));
478 extern int *bind_vsock (size_t *)
479 __attribute__((__nonnull__ (1)));
480 extern void accept_incoming_connections (int *socks
, size_t nr_socks
)
481 __attribute__((__nonnull__ (1)));
484 extern void threadlocal_init (void);
485 extern void threadlocal_new_server_thread (void);
486 extern void threadlocal_set_name (const char *name
)
487 __attribute__((__nonnull__ (1)));
488 extern const char *threadlocal_get_name (void);
489 extern void threadlocal_set_instance_num (size_t instance_num
);
490 extern size_t threadlocal_get_instance_num (void);
491 extern void threadlocal_set_error (int err
);
492 extern int threadlocal_get_error (void);
493 extern void *threadlocal_buffer (size_t size
);
494 extern void threadlocal_set_conn (struct connection
*conn
);
495 extern struct connection
*threadlocal_get_conn (void);
497 /* Declare program_name. */
498 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
500 #define program_name program_invocation_short_name
502 #define program_name "nbdkit"
505 #endif /* NBDKIT_INTERNAL_H */