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"
48 #define UNIX_PATH_MAX 104
50 #define UNIX_PATH_MAX 108
54 # include <valgrind.h>
55 /* http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
56 # define DO_DLCLOSE !RUNNING_ON_VALGRIND
57 #elif defined(__SANITIZE_ADDRESS__)
63 #define container_of(ptr, type, member) ({ \
64 const typeof (((type *) 0)->member) *__mptr = (ptr); \
65 (type *) ((char *) __mptr - offsetof(type, member)); \
68 /* Maximum read or write request that we will handle. */
69 #define MAX_REQUEST_SIZE (64 * 1024 * 1024)
73 struct debug_flag
*next
;
74 char *name
; /* plugin or filter name */
75 char *flag
; /* flag name */
76 int value
; /* value of flag */
77 bool used
; /* if flag was successfully set */
81 LOG_TO_DEFAULT
, /* --log not specified: log to stderr, unless
82 we forked into the background in which
84 LOG_TO_STDERR
, /* --log=stderr forced on the command line */
85 LOG_TO_SYSLOG
, /* --log=syslog forced on the command line */
88 extern struct debug_flag
*debug_flags
;
89 extern const char *exportname
;
90 extern bool foreground
;
91 extern const char *ipaddr
;
92 extern enum log_to log_to
;
93 extern unsigned mask_handshake
;
96 extern const char *port
;
97 extern bool read_only
;
98 extern const char *run
;
99 extern bool listen_stdin
;
100 extern const char *selinux_label
;
103 extern const char *tls_certificates_dir
;
104 extern const char *tls_psk
;
105 extern bool tls_verify_peer
;
106 extern char *unixsocket
;
107 extern const char *user
, *group
;
110 extern struct backend
*backend
;
111 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
114 extern volatile int quit
;
116 extern void set_up_quit_pipe (void);
117 extern void handle_quit (int sig
);
118 extern void set_quit (void);
121 extern void set_up_signals (void);
124 extern bool forked_into_background
;
125 extern void fork_into_background (void);
128 extern void run_command (void);
130 /* socket-activation.c */
131 #define FIRST_SOCKET_ACTIVATION_FD 3 /* defined by systemd ABI */
132 extern unsigned int get_socket_activation (void);
135 extern void change_user (void);
140 /* Flags for connection_send_function */
142 SEND_MORE
= 1, /* Hint to use MSG_MORE/corking to group send()s */
145 typedef int (*connection_recv_function
) (struct connection
*,
146 void *buf
, size_t len
)
147 __attribute__((__nonnull__ (1, 2)));
148 typedef int (*connection_send_function
) (struct connection
*,
149 const void *buf
, size_t len
,
151 __attribute__((__nonnull__ (1, 2)));
152 typedef void (*connection_close_function
) (struct connection
*)
153 __attribute__((__nonnull__ (1)));
155 struct b_conn_handle
{
172 reset_b_conn_handle (struct b_conn_handle
*h
)
178 h
->is_rotational
= -1;
181 h
->can_fast_zero
= -1;
183 h
->can_multi_conn
= -1;
189 pthread_mutex_t request_lock
;
190 pthread_mutex_t read_lock
;
191 pthread_mutex_t write_lock
;
192 pthread_mutex_t status_lock
;
193 int status
; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
194 int status_pipe
[2]; /* track status changes via poll when nworkers > 1 */
195 void *crypto_session
;
198 struct b_conn_handle
*handles
;
205 bool structured_replies
;
206 bool meta_context_base_allocation
;
209 connection_recv_function recv
;
210 connection_send_function send
;
211 connection_close_function close
;
214 extern int handle_single_connection (int sockin
, int sockout
);
215 extern void *connection_get_handle (struct connection
*conn
, size_t i
)
216 __attribute__((__nonnull__ (1)));
217 extern int connection_get_status (struct connection
*conn
)
218 __attribute__((__nonnull__ (1)));
219 extern int connection_set_status (struct connection
*conn
, int value
)
220 __attribute__((__nonnull__ (1)));
222 /* protocol-handshake.c */
223 extern int protocol_handshake (struct connection
*conn
)
224 __attribute__((__nonnull__ (1)));
225 extern int protocol_common_open (struct connection
*conn
,
226 uint64_t *exportsize
, uint16_t *flags
)
227 __attribute__((__nonnull__ (1, 2, 3)));
229 /* protocol-handshake-oldstyle.c */
230 extern int protocol_handshake_oldstyle (struct connection
*conn
)
231 __attribute__((__nonnull__ (1)));
233 /* protocol-handshake-newstyle.c */
234 extern int protocol_handshake_newstyle (struct connection
*conn
)
235 __attribute__((__nonnull__ (1)));
238 extern int protocol_recv_request_send_reply (struct connection
*conn
)
239 __attribute__((__nonnull__ (1)));
241 /* The context ID of base:allocation. As far as I can tell it doesn't
242 * matter what this is as long as nbdkit always returns the same
245 #define base_allocation_id 1
248 #define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
249 extern void crypto_init (bool tls_set_on_cli
);
250 extern void crypto_free (void);
251 extern int crypto_negotiate_tls (struct connection
*conn
,
252 int sockin
, int sockout
)
253 __attribute__((__nonnull__ (1)));
256 #define debug nbdkit_debug
259 #if !HAVE_VFPRINTF_PERCENT_M
261 #define vfprintf nbdkit_vfprintf
262 extern int nbdkit_vfprintf (FILE *f
, const char *fmt
, va_list args
)
263 __attribute__((__format__ (printf
, 2, 0)));
265 extern void log_stderr_verror (const char *fs
, va_list args
)
266 __attribute__((__format__ (printf
, 1, 0)));
267 extern void log_syslog_verror (const char *fs
, va_list args
)
268 __attribute__((__format__ (printf
, 1, 0)));
272 /* Next filter or plugin in the chain. This is always NULL for
273 * plugins and never NULL for filters.
275 struct backend
*next
;
277 /* A unique index used to fetch the handle from the connections
278 * object. The plugin (last in the chain) has index 0, and the
279 * filters have index 1, 2, ... depending how "far" they are from
284 /* The type of backend: filter or plugin. */
287 /* A copy of the backend name that survives a dlclose. */
290 /* The file the backend was loaded from. */
293 /* The dlopen handle for the backend. */
296 /* Backend callbacks. All are required. */
297 void (*free
) (struct backend
*);
298 int (*thread_model
) (struct backend
*);
299 const char *(*plugin_name
) (struct backend
*);
300 void (*usage
) (struct backend
*);
301 const char *(*version
) (struct backend
*);
302 void (*dump_fields
) (struct backend
*);
303 void (*config
) (struct backend
*, const char *key
, const char *value
);
304 void (*config_complete
) (struct backend
*);
305 const char *(*magic_config_key
) (struct backend
*);
306 int (*open
) (struct backend
*, struct connection
*conn
, int readonly
);
307 int (*prepare
) (struct backend
*, struct connection
*conn
, int readonly
);
308 int (*finalize
) (struct backend
*, struct connection
*conn
);
309 void (*close
) (struct backend
*, struct connection
*conn
);
311 int64_t (*get_size
) (struct backend
*, struct connection
*conn
);
312 int (*can_write
) (struct backend
*, struct connection
*conn
);
313 int (*can_flush
) (struct backend
*, struct connection
*conn
);
314 int (*is_rotational
) (struct backend
*, struct connection
*conn
);
315 int (*can_trim
) (struct backend
*, struct connection
*conn
);
316 int (*can_zero
) (struct backend
*, struct connection
*conn
);
317 int (*can_fast_zero
) (struct backend
*, struct connection
*conn
);
318 int (*can_extents
) (struct backend
*, struct connection
*conn
);
319 int (*can_fua
) (struct backend
*, struct connection
*conn
);
320 int (*can_multi_conn
) (struct backend
*, struct connection
*conn
);
321 int (*can_cache
) (struct backend
*, struct connection
*conn
);
323 int (*pread
) (struct backend
*, struct connection
*conn
, void *buf
,
324 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
325 int (*pwrite
) (struct backend
*, struct connection
*conn
, const void *buf
,
326 uint32_t count
, uint64_t offset
, uint32_t flags
, int *err
);
327 int (*flush
) (struct backend
*, struct connection
*conn
, uint32_t flags
,
329 int (*trim
) (struct backend
*, struct connection
*conn
, uint32_t count
,
330 uint64_t offset
, uint32_t flags
, int *err
);
331 int (*zero
) (struct backend
*, struct connection
*conn
, uint32_t count
,
332 uint64_t offset
, uint32_t flags
, int *err
);
333 int (*extents
) (struct backend
*, struct connection
*conn
, uint32_t count
,
334 uint64_t offset
, uint32_t flags
,
335 struct nbdkit_extents
*extents
, int *err
);
336 int (*cache
) (struct backend
*, struct connection
*conn
, uint32_t count
,
337 uint64_t offset
, uint32_t flags
, int *err
);
340 extern void backend_init (struct backend
*b
, struct backend
*next
, size_t index
,
341 const char *filename
, void *dl
, const char *type
)
342 __attribute__((__nonnull__ (1, 4, 5, 6)));
343 extern void backend_load (struct backend
*b
, const char *name
,
345 __attribute__((__nonnull__ (1 /* not 2 */)));
346 extern void backend_unload (struct backend
*b
, void (*unload
) (void))
347 __attribute__((__nonnull__ (1)));
349 extern int backend_open (struct backend
*b
, struct connection
*conn
,
351 __attribute__((__nonnull__ (1, 2)));
352 extern int backend_prepare (struct backend
*b
, struct connection
*conn
)
353 __attribute__((__nonnull__ (1, 2)));
354 extern void backend_close (struct backend
*b
, struct connection
*conn
)
355 __attribute__((__nonnull__ (1, 2)));
356 extern void backend_set_handle (struct backend
*b
, struct connection
*conn
,
358 __attribute__((__nonnull__ (1, 2, 3)));
359 extern bool backend_valid_range (struct backend
*b
, struct connection
*conn
,
360 uint64_t offset
, uint32_t count
)
361 __attribute__((__nonnull__ (1, 2)));
363 extern int64_t backend_get_size (struct backend
*b
, struct connection
*conn
)
364 __attribute__((__nonnull__ (1, 2)));
365 extern int backend_can_write (struct backend
*b
, struct connection
*conn
)
366 __attribute__((__nonnull__ (1, 2)));
367 extern int backend_can_flush (struct backend
*b
, struct connection
*conn
)
368 __attribute__((__nonnull__ (1, 2)));
369 extern int backend_is_rotational (struct backend
*b
, struct connection
*conn
)
370 __attribute__((__nonnull__ (1, 2)));
371 extern int backend_can_trim (struct backend
*b
, struct connection
*conn
)
372 __attribute__((__nonnull__ (1, 2)));
373 extern int backend_can_zero (struct backend
*b
, struct connection
*conn
)
374 __attribute__((__nonnull__ (1, 2)));
375 extern int backend_can_fast_zero (struct backend
*b
, struct connection
*conn
)
376 __attribute__((__nonnull__ (1, 2)));
377 extern int backend_can_extents (struct backend
*b
, struct connection
*conn
)
378 __attribute__((__nonnull__ (1, 2)));
379 extern int backend_can_fua (struct backend
*b
, struct connection
*conn
)
380 __attribute__((__nonnull__ (1, 2)));
381 extern int backend_can_multi_conn (struct backend
*b
, struct connection
*conn
)
382 __attribute__((__nonnull__ (1, 2)));
383 extern int backend_can_cache (struct backend
*b
, struct connection
*conn
)
384 __attribute__((__nonnull__ (1, 2)));
386 extern int backend_pread (struct backend
*b
, struct connection
*conn
,
387 void *buf
, uint32_t count
, uint64_t offset
,
388 uint32_t flags
, int *err
)
389 __attribute__((__nonnull__ (1, 2, 3, 7)));
390 extern int backend_pwrite (struct backend
*b
, struct connection
*conn
,
391 const void *buf
, uint32_t count
, uint64_t offset
,
392 uint32_t flags
, int *err
)
393 __attribute__((__nonnull__ (1, 2, 3, 7)));
394 extern int backend_flush (struct backend
*b
, struct connection
*conn
,
395 uint32_t flags
, int *err
)
396 __attribute__((__nonnull__ (1, 2, 4)));
397 extern int backend_trim (struct backend
*b
, struct connection
*conn
,
398 uint32_t count
, uint64_t offset
, uint32_t flags
,
400 __attribute__((__nonnull__ (1, 2, 6)));
401 extern int backend_zero (struct backend
*b
, struct connection
*conn
,
402 uint32_t count
, uint64_t offset
, uint32_t flags
,
404 __attribute__((__nonnull__ (1, 2, 6)));
405 extern int backend_extents (struct backend
*b
, struct connection
*conn
,
406 uint32_t count
, uint64_t offset
, uint32_t flags
,
407 struct nbdkit_extents
*extents
, int *err
)
408 __attribute__((__nonnull__ (1, 2, 6, 7)));
409 extern int backend_cache (struct backend
*b
, struct connection
*conn
,
410 uint32_t count
, uint64_t offset
,
411 uint32_t flags
, int *err
)
412 __attribute__((__nonnull__ (1, 2, 6)));
415 extern struct backend
*plugin_register (size_t index
, const char *filename
,
416 void *dl
, struct nbdkit_plugin
*(*plugin_init
) (void))
417 __attribute__((__nonnull__ (2, 3, 4)));
420 extern struct backend
*filter_register (struct backend
*next
, size_t index
,
421 const char *filename
, void *dl
,
422 struct nbdkit_filter
*(*filter_init
) (void))
423 __attribute__((__nonnull__ (1, 3, 4, 5)));
426 extern void lock_init_thread_model (void);
427 extern const char *name_of_thread_model (int model
);
428 extern void lock_connection (void);
429 extern void unlock_connection (void);
430 extern void lock_request (struct connection
*conn
);
431 extern void unlock_request (struct connection
*conn
);
432 extern void lock_unload (void);
433 extern void unlock_unload (void);
436 extern int *bind_unix_socket (size_t *)
437 __attribute__((__nonnull__ (1)));
438 extern int *bind_tcpip_socket (size_t *)
439 __attribute__((__nonnull__ (1)));
440 extern void accept_incoming_connections (int *socks
, size_t nr_socks
)
441 __attribute__((__nonnull__ (1)));
442 extern void free_listening_sockets (int *socks
, size_t nr_socks
)
443 __attribute__((__nonnull__ (1)));
446 extern void threadlocal_init (void);
447 extern void threadlocal_new_server_thread (void);
448 extern void threadlocal_set_name (const char *name
)
449 __attribute__((__nonnull__ (1)));
450 extern const char *threadlocal_get_name (void);
451 extern void threadlocal_set_instance_num (size_t instance_num
);
452 extern size_t threadlocal_get_instance_num (void);
453 extern void threadlocal_set_error (int err
);
454 extern int threadlocal_get_error (void);
455 extern void *threadlocal_buffer (size_t size
);
456 extern void threadlocal_set_conn (struct connection
*conn
);
457 extern struct connection
*threadlocal_get_conn (void);
459 /* Declare program_name. */
460 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
462 #define program_name program_invocation_short_name
464 #define program_name "nbdkit"
467 #endif /* NBDKIT_INTERNAL_H */