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 struct debug_flag
*next
;
92 char *name
; /* plugin or filter name */
93 char *flag
; /* flag name */
94 int value
; /* value of flag */
95 bool used
; /* if flag was successfully set */
99 LOG_TO_DEFAULT
, /* --log not specified: log to stderr, unless
100 we forked into the background in which
101 case log to syslog */
102 LOG_TO_STDERR
, /* --log=stderr forced on the command line */
103 LOG_TO_SYSLOG
, /* --log=syslog forced on the command line */
104 LOG_TO_NULL
, /* --log=null forced on the command line */
107 extern struct debug_flag
*debug_flags
;
108 extern const char *exportname
;
109 extern bool foreground
;
110 extern const char *ipaddr
;
111 extern enum log_to log_to
;
112 extern unsigned mask_handshake
;
113 extern bool newstyle
;
115 extern const char *port
;
116 extern bool read_only
;
117 extern const char *run
;
118 extern bool listen_stdin
;
119 extern const char *selinux_label
;
120 extern unsigned threads
;
122 extern const char *tls_certificates_dir
;
123 extern const char *tls_psk
;
124 extern bool tls_verify_peer
;
125 extern char *unixsocket
;
126 extern const char *user
, *group
;
129 extern struct backend
*backend
;
130 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
133 extern volatile int quit
;
135 extern void set_up_quit_pipe (void);
136 extern void close_quit_pipe (void);
137 extern void handle_quit (int sig
);
138 extern void set_quit (void);
141 extern void set_up_signals (void);
144 extern bool forked_into_background
;
145 extern void fork_into_background (void);
148 extern void run_command (void);
150 /* socket-activation.c */
151 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
152 extern unsigned int get_socket_activation (void);
155 extern void change_user (void);
160 /* Flags for connection_send_function */
162 SEND_MORE
= 1, /* Hint to use MSG_MORE/corking to group send()s */
165 typedef int (*connection_recv_function
) (struct connection
*,
166 void *buf
, size_t len
)
167 __attribute__((__nonnull__ (1, 2)));
168 typedef int (*connection_send_function
) (struct connection
*,
169 const void *buf
, size_t len
,
171 __attribute__((__nonnull__ (1, 2)));
172 typedef void (*connection_close_function
) (struct connection
*)
173 __attribute__((__nonnull__ (1)));
176 HANDLE_OPEN
= 1, /* Set if .open passed, so .close is needed */
177 HANDLE_CONNECTED
= 2, /* Set if .prepare passed, so .finalize is needed */
178 HANDLE_FAILED
= 4, /* Set if .finalize failed */
181 struct b_conn_handle
{
184 unsigned char state
; /* Bitmask of HANDLE_* values */
200 reset_b_conn_handle (struct b_conn_handle
*h
)
207 h
->is_rotational
= -1;
210 h
->can_fast_zero
= -1;
212 h
->can_multi_conn
= -1;
218 pthread_mutex_t request_lock
;
219 pthread_mutex_t read_lock
;
220 pthread_mutex_t write_lock
;
221 pthread_mutex_t status_lock
;
222 int status
; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
223 int status_pipe
[2]; /* track status changes via poll when nworkers > 1 */
224 void *crypto_session
;
227 struct b_conn_handle
*handles
;
230 char exportname
[NBD_MAX_STRING
+ 1];
231 uint32_t exportnamelen
;
235 bool structured_replies
;
236 bool meta_context_base_allocation
;
239 connection_recv_function recv
;
240 connection_send_function send
;
241 connection_close_function close
;
244 extern void handle_single_connection (int sockin
, int sockout
);
245 extern int connection_get_status (struct connection
*conn
)
246 __attribute__((__nonnull__ (1)));
247 extern int connection_set_status (struct connection
*conn
, int value
)
248 __attribute__((__nonnull__ (1)));
250 /* protocol-handshake.c */
251 extern int protocol_handshake (struct connection
*conn
)
252 __attribute__((__nonnull__ (1)));
253 extern int protocol_common_open (struct connection
*conn
,
254 uint64_t *exportsize
, uint16_t *flags
)
255 __attribute__((__nonnull__ (1, 2, 3)));
257 /* protocol-handshake-oldstyle.c */
258 extern int protocol_handshake_oldstyle (struct connection
*conn
)
259 __attribute__((__nonnull__ (1)));
261 /* protocol-handshake-newstyle.c */
262 extern int protocol_handshake_newstyle (struct connection
*conn
)
263 __attribute__((__nonnull__ (1)));
266 extern int protocol_recv_request_send_reply (struct connection
*conn
)
267 __attribute__((__nonnull__ (1)));
269 /* The context ID of base:allocation. As far as I can tell it doesn't
270 * matter what this is as long as nbdkit always returns the same
273 #define base_allocation_id 1
276 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
277 extern void crypto_init (bool tls_set_on_cli
);
278 extern void crypto_free (void);
279 extern int crypto_negotiate_tls (struct connection
*conn
,
280 int sockin
, int sockout
)
281 __attribute__((__nonnull__ (1)));
284 #define debug(fs, ...) \
287 nbdkit_debug ((fs), ##__VA_ARGS__); \
291 #if !HAVE_VFPRINTF_PERCENT_M
293 #define vfprintf nbdkit_vfprintf
294 extern int nbdkit_vfprintf (FILE *f
, const char *fmt
, va_list args
)
295 __attribute__((__format__ (printf
, 2, 0)));
297 extern void log_stderr_verror (const char *fs
, va_list args
)
298 __attribute__((__format__ (printf
, 1, 0)));
299 extern void log_syslog_verror (const char *fs
, va_list args
)
300 __attribute__((__format__ (printf
, 1, 0)));
304 /* Next filter or plugin in the chain. This is always NULL for
305 * plugins and never NULL for filters.
307 struct backend
*next
;
309 /* A unique index used to fetch the handle from the connections
310 * object. The plugin (last in the chain) has index 0, and the
311 * filters have index 1, 2, ... depending how "far" they are from
316 /* The type of backend: filter or plugin. */
319 /* A copy of the backend name that survives a dlclose. */
322 /* The file the backend was loaded from. */
325 /* The dlopen handle for the backend. */
328 /* Backend callbacks. All are required. */
329 void (*free
) (struct backend
*);
330 int (*thread_model
) (struct backend
*);
331 const char *(*plugin_name
) (struct backend
*);
332 void (*usage
) (struct backend
*);
333 const char *(*version
) (struct backend
*);
334 void (*dump_fields
) (struct backend
*);
335 void (*config
) (struct backend
*, const char *key
, const char *value
);
336 void (*config_complete
) (struct backend
*);
337 const char *(*magic_config_key
) (struct backend
*);
338 void *(*open
) (struct backend
*, struct connection
*conn
, int readonly
);
339 int (*prepare
) (struct backend
*, struct connection
*conn
, void *handle
,
341 int (*finalize
) (struct backend
*, struct connection
*conn
, void *handle
);
342 void (*close
) (struct backend
*, struct connection
*conn
, void *handle
);
344 int64_t (*get_size
) (struct backend
*, struct connection
*conn
, void *handle
);
345 int (*can_write
) (struct backend
*, struct connection
*conn
, void *handle
);
346 int (*can_flush
) (struct backend
*, struct connection
*conn
, void *handle
);
347 int (*is_rotational
) (struct backend
*, struct connection
*conn
,
349 int (*can_trim
) (struct backend
*, struct connection
*conn
, void *handle
);
350 int (*can_zero
) (struct backend
*, struct connection
*conn
, void *handle
);
351 int (*can_fast_zero
) (struct backend
*, struct connection
*conn
,
353 int (*can_extents
) (struct backend
*, struct connection
*conn
, void *handle
);
354 int (*can_fua
) (struct backend
*, struct connection
*conn
, void *handle
);
355 int (*can_multi_conn
) (struct backend
*, struct connection
*conn
,
357 int (*can_cache
) (struct backend
*, struct connection
*conn
, void *handle
);
359 int (*pread
) (struct backend
*, struct connection
*conn
, void *handle
,
360 void *buf
, uint32_t count
, uint64_t offset
,
361 uint32_t flags
, int *err
);
362 int (*pwrite
) (struct backend
*, struct connection
*conn
, void *handle
,
363 const void *buf
, uint32_t count
, uint64_t offset
,
364 uint32_t flags
, int *err
);
365 int (*flush
) (struct backend
*, struct connection
*conn
, void *handle
,
366 uint32_t flags
, int *err
);
367 int (*trim
) (struct backend
*, struct connection
*conn
, void *handle
,
368 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
369 int (*zero
) (struct backend
*, struct connection
*conn
, void *handle
,
370 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
371 int (*extents
) (struct backend
*, struct connection
*conn
, void *handle
,
372 uint32_t count
, uint64_t offset
, uint32_t flags
,
373 struct nbdkit_extents
*extents
, int *err
);
374 int (*cache
) (struct backend
*, struct connection
*conn
, void *handle
,
375 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
378 extern void backend_init (struct backend
*b
, struct backend
*next
, size_t index
,
379 const char *filename
, void *dl
, const char *type
)
380 __attribute__((__nonnull__ (1, 4, 5, 6)));
381 extern void backend_load (struct backend
*b
, const char *name
,
383 __attribute__((__nonnull__ (1 /* not 2 */)));
384 extern void backend_unload (struct backend
*b
, void (*unload
) (void))
385 __attribute__((__nonnull__ (1)));
387 extern int backend_open (struct backend
*b
, struct connection
*conn
,
389 __attribute__((__nonnull__ (1, 2)));
390 extern int backend_prepare (struct backend
*b
, struct connection
*conn
)
391 __attribute__((__nonnull__ (1, 2)));
392 extern int backend_finalize (struct backend
*b
, struct connection
*conn
)
393 __attribute__((__nonnull__ (1, 2)));
394 extern void backend_close (struct backend
*b
, struct connection
*conn
)
395 __attribute__((__nonnull__ (1, 2)));
396 extern bool backend_valid_range (struct backend
*b
, struct connection
*conn
,
397 uint64_t offset
, uint32_t count
)
398 __attribute__((__nonnull__ (1, 2)));
400 extern int backend_reopen (struct backend
*b
, struct connection
*conn
,
402 __attribute__((__nonnull__ (1, 2)));
403 extern int64_t backend_get_size (struct backend
*b
, struct connection
*conn
)
404 __attribute__((__nonnull__ (1, 2)));
405 extern int backend_can_write (struct backend
*b
, struct connection
*conn
)
406 __attribute__((__nonnull__ (1, 2)));
407 extern int backend_can_flush (struct backend
*b
, struct connection
*conn
)
408 __attribute__((__nonnull__ (1, 2)));
409 extern int backend_is_rotational (struct backend
*b
, struct connection
*conn
)
410 __attribute__((__nonnull__ (1, 2)));
411 extern int backend_can_trim (struct backend
*b
, struct connection
*conn
)
412 __attribute__((__nonnull__ (1, 2)));
413 extern int backend_can_zero (struct backend
*b
, struct connection
*conn
)
414 __attribute__((__nonnull__ (1, 2)));
415 extern int backend_can_fast_zero (struct backend
*b
, struct connection
*conn
)
416 __attribute__((__nonnull__ (1, 2)));
417 extern int backend_can_extents (struct backend
*b
, struct connection
*conn
)
418 __attribute__((__nonnull__ (1, 2)));
419 extern int backend_can_fua (struct backend
*b
, struct connection
*conn
)
420 __attribute__((__nonnull__ (1, 2)));
421 extern int backend_can_multi_conn (struct backend
*b
, struct connection
*conn
)
422 __attribute__((__nonnull__ (1, 2)));
423 extern int backend_can_cache (struct backend
*b
, struct connection
*conn
)
424 __attribute__((__nonnull__ (1, 2)));
426 extern int backend_pread (struct backend
*b
, struct connection
*conn
,
427 void *buf
, uint32_t count
, uint64_t offset
,
428 uint32_t flags
, int *err
)
429 __attribute__((__nonnull__ (1, 2, 3, 7)));
430 extern int backend_pwrite (struct backend
*b
, struct connection
*conn
,
431 const void *buf
, uint32_t count
, uint64_t offset
,
432 uint32_t flags
, int *err
)
433 __attribute__((__nonnull__ (1, 2, 3, 7)));
434 extern int backend_flush (struct backend
*b
, struct connection
*conn
,
435 uint32_t flags
, int *err
)
436 __attribute__((__nonnull__ (1, 2, 4)));
437 extern int backend_trim (struct backend
*b
, struct connection
*conn
,
438 uint32_t count
, uint64_t offset
, uint32_t flags
,
440 __attribute__((__nonnull__ (1, 2, 6)));
441 extern int backend_zero (struct backend
*b
, struct connection
*conn
,
442 uint32_t count
, uint64_t offset
, uint32_t flags
,
444 __attribute__((__nonnull__ (1, 2, 6)));
445 extern int backend_extents (struct backend
*b
, struct connection
*conn
,
446 uint32_t count
, uint64_t offset
, uint32_t flags
,
447 struct nbdkit_extents
*extents
, int *err
)
448 __attribute__((__nonnull__ (1, 2, 6, 7)));
449 extern int backend_cache (struct backend
*b
, struct connection
*conn
,
450 uint32_t count
, uint64_t offset
,
451 uint32_t flags
, int *err
)
452 __attribute__((__nonnull__ (1, 2, 6)));
455 extern struct backend
*plugin_register (size_t index
, const char *filename
,
456 void *dl
, struct nbdkit_plugin
*(*plugin_init
) (void))
457 __attribute__((__nonnull__ (2, 3, 4)));
460 extern struct backend
*filter_register (struct backend
*next
, size_t index
,
461 const char *filename
, void *dl
,
462 struct nbdkit_filter
*(*filter_init
) (void))
463 __attribute__((__nonnull__ (1, 3, 4, 5)));
466 extern void lock_init_thread_model (void);
467 extern const char *name_of_thread_model (int model
);
468 extern void lock_connection (void);
469 extern void unlock_connection (void);
470 extern void lock_request (struct connection
*conn
);
471 extern void unlock_request (struct connection
*conn
);
472 extern void lock_unload (void);
473 extern void unlock_unload (void);
476 extern int *bind_unix_socket (size_t *)
477 __attribute__((__nonnull__ (1)));
478 extern int *bind_tcpip_socket (size_t *)
479 __attribute__((__nonnull__ (1)));
480 extern int *bind_vsock (size_t *)
481 __attribute__((__nonnull__ (1)));
482 extern void accept_incoming_connections (int *socks
, size_t nr_socks
)
483 __attribute__((__nonnull__ (1)));
486 extern void threadlocal_init (void);
487 extern void threadlocal_new_server_thread (void);
488 extern void threadlocal_set_name (const char *name
)
489 __attribute__((__nonnull__ (1)));
490 extern const char *threadlocal_get_name (void);
491 extern void threadlocal_set_instance_num (size_t instance_num
);
492 extern size_t threadlocal_get_instance_num (void);
493 extern void threadlocal_set_error (int err
);
494 extern int threadlocal_get_error (void);
495 extern void *threadlocal_buffer (size_t size
);
496 extern void threadlocal_set_conn (struct connection
*conn
);
497 extern struct connection
*threadlocal_get_conn (void);
499 /* Declare program_name. */
500 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
502 #define program_name program_invocation_short_name
504 #define program_name "nbdkit"
507 #endif /* NBDKIT_INTERNAL_H */