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"
49 #define UNIX_PATH_MAX 104
51 #define UNIX_PATH_MAX 108
55 # include <valgrind.h>
56 /* http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
57 # define DO_DLCLOSE !RUNNING_ON_VALGRIND
58 #elif defined(__SANITIZE_ADDRESS__)
64 #define container_of(ptr, type, member) ({ \
65 const typeof (((type *) 0)->member) *__mptr = (ptr); \
66 (type *) ((char *) __mptr - offsetof(type, member)); \
69 /* Maximum read or write request that we will handle. */
70 #define MAX_REQUEST_SIZE (64 * 1024 * 1024)
74 struct debug_flag
*next
;
75 char *name
; /* plugin or filter name */
76 char *flag
; /* flag name */
77 int value
; /* value of flag */
78 bool used
; /* if flag was successfully set */
82 LOG_TO_DEFAULT
, /* --log not specified: log to stderr, unless
83 we forked into the background in which
85 LOG_TO_STDERR
, /* --log=stderr forced on the command line */
86 LOG_TO_SYSLOG
, /* --log=syslog forced on the command line */
89 extern struct debug_flag
*debug_flags
;
90 extern const char *exportname
;
91 extern bool foreground
;
92 extern const char *ipaddr
;
93 extern enum log_to log_to
;
94 extern unsigned mask_handshake
;
97 extern const char *port
;
98 extern bool read_only
;
99 extern const char *run
;
100 extern bool listen_stdin
;
101 extern const char *selinux_label
;
102 extern unsigned threads
;
104 extern const char *tls_certificates_dir
;
105 extern const char *tls_psk
;
106 extern bool tls_verify_peer
;
107 extern char *unixsocket
;
108 extern const char *user
, *group
;
111 extern struct backend
*backend
;
112 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
115 extern volatile int quit
;
117 extern void set_up_quit_pipe (void);
118 extern void handle_quit (int sig
);
119 extern void set_quit (void);
122 extern void set_up_signals (void);
125 extern bool forked_into_background
;
126 extern void fork_into_background (void);
129 extern void run_command (void);
131 /* socket-activation.c */
132 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
133 extern unsigned int get_socket_activation (void);
136 extern void change_user (void);
141 /* Flags for connection_send_function */
143 SEND_MORE
= 1, /* Hint to use MSG_MORE/corking to group send()s */
146 typedef int (*connection_recv_function
) (struct connection
*,
147 void *buf
, size_t len
)
148 __attribute__((__nonnull__ (1, 2)));
149 typedef int (*connection_send_function
) (struct connection
*,
150 const void *buf
, size_t len
,
152 __attribute__((__nonnull__ (1, 2)));
153 typedef void (*connection_close_function
) (struct connection
*)
154 __attribute__((__nonnull__ (1)));
156 struct b_conn_handle
{
173 reset_b_conn_handle (struct b_conn_handle
*h
)
179 h
->is_rotational
= -1;
182 h
->can_fast_zero
= -1;
184 h
->can_multi_conn
= -1;
190 pthread_mutex_t request_lock
;
191 pthread_mutex_t read_lock
;
192 pthread_mutex_t write_lock
;
193 pthread_mutex_t status_lock
;
194 int status
; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
195 int status_pipe
[2]; /* track status changes via poll when nworkers > 1 */
196 void *crypto_session
;
199 struct b_conn_handle
*handles
;
202 char exportname
[NBD_MAX_STRING
+ 1];
203 uint32_t exportnamelen
;
207 bool structured_replies
;
208 bool meta_context_base_allocation
;
211 connection_recv_function recv
;
212 connection_send_function send
;
213 connection_close_function close
;
216 extern int handle_single_connection (int sockin
, int sockout
);
217 extern void *connection_get_handle (struct connection
*conn
, size_t i
)
218 __attribute__((__nonnull__ (1)));
219 extern int connection_get_status (struct connection
*conn
)
220 __attribute__((__nonnull__ (1)));
221 extern int connection_set_status (struct connection
*conn
, int value
)
222 __attribute__((__nonnull__ (1)));
224 /* protocol-handshake.c */
225 extern int protocol_handshake (struct connection
*conn
)
226 __attribute__((__nonnull__ (1)));
227 extern int protocol_common_open (struct connection
*conn
,
228 uint64_t *exportsize
, uint16_t *flags
)
229 __attribute__((__nonnull__ (1, 2, 3)));
231 /* protocol-handshake-oldstyle.c */
232 extern int protocol_handshake_oldstyle (struct connection
*conn
)
233 __attribute__((__nonnull__ (1)));
235 /* protocol-handshake-newstyle.c */
236 extern int protocol_handshake_newstyle (struct connection
*conn
)
237 __attribute__((__nonnull__ (1)));
240 extern int protocol_recv_request_send_reply (struct connection
*conn
)
241 __attribute__((__nonnull__ (1)));
243 /* The context ID of base:allocation. As far as I can tell it doesn't
244 * matter what this is as long as nbdkit always returns the same
247 #define base_allocation_id 1
250 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
251 extern void crypto_init (bool tls_set_on_cli
);
252 extern void crypto_free (void);
253 extern int crypto_negotiate_tls (struct connection
*conn
,
254 int sockin
, int sockout
)
255 __attribute__((__nonnull__ (1)));
258 #define debug nbdkit_debug
261 #if !HAVE_VFPRINTF_PERCENT_M
263 #define vfprintf nbdkit_vfprintf
264 extern int nbdkit_vfprintf (FILE *f
, const char *fmt
, va_list args
)
265 __attribute__((__format__ (printf
, 2, 0)));
267 extern void log_stderr_verror (const char *fs
, va_list args
)
268 __attribute__((__format__ (printf
, 1, 0)));
269 extern void log_syslog_verror (const char *fs
, va_list args
)
270 __attribute__((__format__ (printf
, 1, 0)));
274 /* Next filter or plugin in the chain. This is always NULL for
275 * plugins and never NULL for filters.
277 struct backend
*next
;
279 /* A unique index used to fetch the handle from the connections
280 * object. The plugin (last in the chain) has index 0, and the
281 * filters have index 1, 2, ... depending how "far" they are from
286 /* The type of backend: filter or plugin. */
289 /* A copy of the backend name that survives a dlclose. */
292 /* The file the backend was loaded from. */
295 /* The dlopen handle for the backend. */
298 /* Backend callbacks. All are required. */
299 void (*free
) (struct backend
*);
300 int (*thread_model
) (struct backend
*);
301 const char *(*plugin_name
) (struct backend
*);
302 void (*usage
) (struct backend
*);
303 const char *(*version
) (struct backend
*);
304 void (*dump_fields
) (struct backend
*);
305 void (*config
) (struct backend
*, const char *key
, const char *value
);
306 void (*config_complete
) (struct backend
*);
307 const char *(*magic_config_key
) (struct backend
*);
308 int (*open
) (struct backend
*, struct connection
*conn
, int readonly
);
309 int (*prepare
) (struct backend
*, struct connection
*conn
, int readonly
);
310 int (*finalize
) (struct backend
*, struct connection
*conn
);
311 void (*close
) (struct backend
*, struct connection
*conn
);
313 int64_t (*get_size
) (struct backend
*, struct connection
*conn
);
314 int (*can_write
) (struct backend
*, struct connection
*conn
);
315 int (*can_flush
) (struct backend
*, struct connection
*conn
);
316 int (*is_rotational
) (struct backend
*, struct connection
*conn
);
317 int (*can_trim
) (struct backend
*, struct connection
*conn
);
318 int (*can_zero
) (struct backend
*, struct connection
*conn
);
319 int (*can_fast_zero
) (struct backend
*, struct connection
*conn
);
320 int (*can_extents
) (struct backend
*, struct connection
*conn
);
321 int (*can_fua
) (struct backend
*, struct connection
*conn
);
322 int (*can_multi_conn
) (struct backend
*, struct connection
*conn
);
323 int (*can_cache
) (struct backend
*, struct connection
*conn
);
325 int (*pread
) (struct backend
*, struct connection
*conn
, void *buf
,
326 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
327 int (*pwrite
) (struct backend
*, struct connection
*conn
, const void *buf
,
328 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
329 int (*flush
) (struct backend
*, struct connection
*conn
, uint32_t flags
,
331 int (*trim
) (struct backend
*, struct connection
*conn
, uint32_t count
,
332 uint64_t offset
, uint32_t flags
, int *err
);
333 int (*zero
) (struct backend
*, struct connection
*conn
, uint32_t count
,
334 uint64_t offset
, uint32_t flags
, int *err
);
335 int (*extents
) (struct backend
*, struct connection
*conn
, uint32_t count
,
336 uint64_t offset
, uint32_t flags
,
337 struct nbdkit_extents
*extents
, int *err
);
338 int (*cache
) (struct backend
*, struct connection
*conn
, uint32_t count
,
339 uint64_t offset
, uint32_t flags
, int *err
);
342 extern void backend_init (struct backend
*b
, struct backend
*next
, size_t index
,
343 const char *filename
, void *dl
, const char *type
)
344 __attribute__((__nonnull__ (1, 4, 5, 6)));
345 extern void backend_load (struct backend
*b
, const char *name
,
347 __attribute__((__nonnull__ (1 /* not 2 */)));
348 extern void backend_unload (struct backend
*b
, void (*unload
) (void))
349 __attribute__((__nonnull__ (1)));
351 extern int backend_open (struct backend
*b
, struct connection
*conn
,
353 __attribute__((__nonnull__ (1, 2)));
354 extern int backend_prepare (struct backend
*b
, struct connection
*conn
)
355 __attribute__((__nonnull__ (1, 2)));
356 extern void backend_close (struct backend
*b
, struct connection
*conn
)
357 __attribute__((__nonnull__ (1, 2)));
358 extern void backend_set_handle (struct backend
*b
, struct connection
*conn
,
360 __attribute__((__nonnull__ (1, 2, 3)));
361 extern bool backend_valid_range (struct backend
*b
, struct connection
*conn
,
362 uint64_t offset
, uint32_t count
)
363 __attribute__((__nonnull__ (1, 2)));
365 extern int backend_reopen (struct backend
*b
, struct connection
*conn
,
367 __attribute__((__nonnull__ (1, 2)));
368 extern int64_t backend_get_size (struct backend
*b
, struct connection
*conn
)
369 __attribute__((__nonnull__ (1, 2)));
370 extern int backend_can_write (struct backend
*b
, struct connection
*conn
)
371 __attribute__((__nonnull__ (1, 2)));
372 extern int backend_can_flush (struct backend
*b
, struct connection
*conn
)
373 __attribute__((__nonnull__ (1, 2)));
374 extern int backend_is_rotational (struct backend
*b
, struct connection
*conn
)
375 __attribute__((__nonnull__ (1, 2)));
376 extern int backend_can_trim (struct backend
*b
, struct connection
*conn
)
377 __attribute__((__nonnull__ (1, 2)));
378 extern int backend_can_zero (struct backend
*b
, struct connection
*conn
)
379 __attribute__((__nonnull__ (1, 2)));
380 extern int backend_can_fast_zero (struct backend
*b
, struct connection
*conn
)
381 __attribute__((__nonnull__ (1, 2)));
382 extern int backend_can_extents (struct backend
*b
, struct connection
*conn
)
383 __attribute__((__nonnull__ (1, 2)));
384 extern int backend_can_fua (struct backend
*b
, struct connection
*conn
)
385 __attribute__((__nonnull__ (1, 2)));
386 extern int backend_can_multi_conn (struct backend
*b
, struct connection
*conn
)
387 __attribute__((__nonnull__ (1, 2)));
388 extern int backend_can_cache (struct backend
*b
, struct connection
*conn
)
389 __attribute__((__nonnull__ (1, 2)));
391 extern int backend_pread (struct backend
*b
, struct connection
*conn
,
392 void *buf
, uint32_t count
, uint64_t offset
,
393 uint32_t flags
, int *err
)
394 __attribute__((__nonnull__ (1, 2, 3, 7)));
395 extern int backend_pwrite (struct backend
*b
, struct connection
*conn
,
396 const void *buf
, uint32_t count
, uint64_t offset
,
397 uint32_t flags
, int *err
)
398 __attribute__((__nonnull__ (1, 2, 3, 7)));
399 extern int backend_flush (struct backend
*b
, struct connection
*conn
,
400 uint32_t flags
, int *err
)
401 __attribute__((__nonnull__ (1, 2, 4)));
402 extern int backend_trim (struct backend
*b
, struct connection
*conn
,
403 uint32_t count
, uint64_t offset
, uint32_t flags
,
405 __attribute__((__nonnull__ (1, 2, 6)));
406 extern int backend_zero (struct backend
*b
, struct connection
*conn
,
407 uint32_t count
, uint64_t offset
, uint32_t flags
,
409 __attribute__((__nonnull__ (1, 2, 6)));
410 extern int backend_extents (struct backend
*b
, struct connection
*conn
,
411 uint32_t count
, uint64_t offset
, uint32_t flags
,
412 struct nbdkit_extents
*extents
, int *err
)
413 __attribute__((__nonnull__ (1, 2, 6, 7)));
414 extern int backend_cache (struct backend
*b
, struct connection
*conn
,
415 uint32_t count
, uint64_t offset
,
416 uint32_t flags
, int *err
)
417 __attribute__((__nonnull__ (1, 2, 6)));
420 extern struct backend
*plugin_register (size_t index
, const char *filename
,
421 void *dl
, struct nbdkit_plugin
*(*plugin_init
) (void))
422 __attribute__((__nonnull__ (2, 3, 4)));
425 extern struct backend
*filter_register (struct backend
*next
, size_t index
,
426 const char *filename
, void *dl
,
427 struct nbdkit_filter
*(*filter_init
) (void))
428 __attribute__((__nonnull__ (1, 3, 4, 5)));
431 extern void lock_init_thread_model (void);
432 extern const char *name_of_thread_model (int model
);
433 extern void lock_connection (void);
434 extern void unlock_connection (void);
435 extern void lock_request (struct connection
*conn
);
436 extern void unlock_request (struct connection
*conn
);
437 extern void lock_unload (void);
438 extern void unlock_unload (void);
441 extern int *bind_unix_socket (size_t *)
442 __attribute__((__nonnull__ (1)));
443 extern int *bind_tcpip_socket (size_t *)
444 __attribute__((__nonnull__ (1)));
445 extern void accept_incoming_connections (int *socks
, size_t nr_socks
)
446 __attribute__((__nonnull__ (1)));
447 extern void free_listening_sockets (int *socks
, size_t nr_socks
)
448 __attribute__((__nonnull__ (1)));
451 extern void threadlocal_init (void);
452 extern void threadlocal_new_server_thread (void);
453 extern void threadlocal_set_name (const char *name
)
454 __attribute__((__nonnull__ (1)));
455 extern const char *threadlocal_get_name (void);
456 extern void threadlocal_set_instance_num (size_t instance_num
);
457 extern size_t threadlocal_get_instance_num (void);
458 extern void threadlocal_set_error (int err
);
459 extern int threadlocal_get_error (void);
460 extern void *threadlocal_buffer (size_t size
);
461 extern void threadlocal_set_conn (struct connection
*conn
);
462 extern struct connection
*threadlocal_get_conn (void);
464 /* Declare program_name. */
465 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
467 #define program_name program_invocation_short_name
469 #define program_name "nbdkit"
472 #endif /* NBDKIT_INTERNAL_H */