Version 1.15.4.
[nbdkit/ericb.git] / server / internal.h
blob97e417f98b7ff42e57fea4f4bbf7af457d419985
1 /* nbdkit
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
6 * met:
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
30 * SUCH DAMAGE.
33 #ifndef NBDKIT_INTERNAL_H
34 #define NBDKIT_INTERNAL_H
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdarg.h>
39 #include <sys/socket.h>
40 #include <pthread.h>
42 #define NBDKIT_API_VERSION 2
43 #include "nbdkit-plugin.h"
44 #include "nbdkit-filter.h"
45 #include "cleanup.h"
46 #include "nbd-protocol.h"
48 #ifdef __APPLE__
49 #define UNIX_PATH_MAX 104
50 #else
51 #define UNIX_PATH_MAX 108
52 #endif
54 #if HAVE_VALGRIND
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__)
59 # define DO_DLCLOSE 0
60 #else
61 # define DO_DLCLOSE 1
62 #endif
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)
72 /* main.c */
73 struct debug_flag {
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 */
81 enum log_to {
82 LOG_TO_DEFAULT, /* --log not specified: log to stderr, unless
83 we forked into the background in which
84 case log to syslog */
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;
95 extern bool newstyle;
96 extern bool no_sr;
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;
103 extern int tls;
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;
109 extern bool verbose;
111 extern struct backend *backend;
112 #define for_each_backend(b) for (b = backend; b != NULL; b = b->next)
114 /* quit.c */
115 extern volatile int quit;
116 extern int quit_fd;
117 extern void set_up_quit_pipe (void);
118 extern void handle_quit (int sig);
119 extern void set_quit (void);
121 /* signals.c */
122 extern void set_up_signals (void);
124 /* background.c */
125 extern bool forked_into_background;
126 extern void fork_into_background (void);
128 /* captive.c */
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);
135 /* usergroup.c */
136 extern void change_user (void);
138 /* connections.c */
139 struct connection;
141 /* Flags for connection_send_function */
142 enum {
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,
151 int flags)
152 __attribute__((__nonnull__ (1, 2)));
153 typedef void (*connection_close_function) (struct connection *)
154 __attribute__((__nonnull__ (1)));
156 struct b_conn_handle {
157 void *handle;
159 uint64_t exportsize;
160 int can_write;
161 int can_flush;
162 int is_rotational;
163 int can_trim;
164 int can_zero;
165 int can_fast_zero;
166 int can_fua;
167 int can_multi_conn;
168 int can_extents;
169 int can_cache;
172 static inline void
173 reset_b_conn_handle (struct b_conn_handle *h)
175 h->handle = NULL;
176 h->exportsize = -1;
177 h->can_write = -1;
178 h->can_flush = -1;
179 h->is_rotational = -1;
180 h->can_trim = -1;
181 h->can_zero = -1;
182 h->can_fast_zero = -1;
183 h->can_fua = -1;
184 h->can_multi_conn = -1;
185 h->can_extents = -1;
186 h->can_cache = -1;
189 struct connection {
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;
197 int nworkers;
199 struct b_conn_handle *handles;
200 size_t nr_handles;
202 char exportname[NBD_MAX_STRING + 1];
203 uint32_t exportnamelen;
204 uint32_t cflags;
205 uint16_t eflags;
206 bool using_tls;
207 bool structured_replies;
208 bool meta_context_base_allocation;
210 int sockin, sockout;
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)));
239 /* protocol.c */
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
245 * number.
247 #define base_allocation_id 1
249 /* crypto.c */
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)));
257 /* debug.c */
258 #define debug nbdkit_debug
260 /* log-*.c */
261 #if !HAVE_VFPRINTF_PERCENT_M
262 #include <stdio.h>
263 #define vfprintf nbdkit_vfprintf
264 extern int nbdkit_vfprintf (FILE *f, const char *fmt, va_list args)
265 __attribute__((__format__ (printf, 2, 0)));
266 #endif
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)));
272 /* backend.c */
273 struct backend {
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
282 * the plugin.
284 size_t i;
286 /* The type of backend: filter or plugin. */
287 const char *type;
289 /* A copy of the backend name that survives a dlclose. */
290 char *name;
292 /* The file the backend was loaded from. */
293 char *filename;
295 /* The dlopen handle for the backend. */
296 void *dl;
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,
330 int *err);
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,
346 void (*load) (void))
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,
352 int readonly)
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,
359 void *handle)
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,
366 int readonly)
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,
404 int *err)
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,
408 int *err)
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)));
419 /* plugins.c */
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)));
424 /* filters.c */
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)));
430 /* locks.c */
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);
440 /* sockets.c */
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)));
450 /* threadlocal.c */
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
466 #include <errno.h>
467 #define program_name program_invocation_short_name
468 #else
469 #define program_name "nbdkit"
470 #endif
472 #endif /* NBDKIT_INTERNAL_H */