2012-06-06 10:55:50 +01:00
|
|
|
#ifndef SERVE_H
|
|
|
|
#define SERVE_H
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-06-08 10:32:33 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2012-10-07 21:55:01 +01:00
|
|
|
#include <signal.h> /* for sig_atomic_t */
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
#include "flexnbd.h"
|
2012-05-19 12:48:03 +01:00
|
|
|
#include "parse.h"
|
2012-06-07 17:47:43 +01:00
|
|
|
#include "acl.h"
|
2012-05-17 20:14:22 +01:00
|
|
|
|
|
|
|
|
2012-06-06 11:27:52 +01:00
|
|
|
static const int block_allocation_resolution = 4096;//128<<10;
|
|
|
|
|
2012-05-21 04:03:17 +01:00
|
|
|
|
2012-06-07 11:44:19 +01:00
|
|
|
struct client_tbl_entry {
|
|
|
|
pthread_t thread;
|
|
|
|
union mysockaddr address;
|
|
|
|
struct client * client;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-05-27 14:40:16 +01:00
|
|
|
#define MAX_NBD_CLIENTS 16
|
2012-06-06 10:35:50 +01:00
|
|
|
struct server {
|
2012-06-27 15:45:33 +01:00
|
|
|
/* The flexnbd wrapper this server is attached to */
|
|
|
|
struct flexnbd * flexnbd;
|
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** address/port to bind to */
|
2012-05-17 20:14:22 +01:00
|
|
|
union mysockaddr bind_to;
|
2012-05-29 11:24:24 +01:00
|
|
|
/** (static) file name to serve */
|
2012-05-17 20:14:22 +01:00
|
|
|
char* filename;
|
2012-05-29 11:24:24 +01:00
|
|
|
/** file name of INCOMPLETE flag */
|
|
|
|
char* filename_incomplete;
|
2012-05-29 04:03:28 +01:00
|
|
|
/** TCP backlog for listen() */
|
2012-05-17 20:14:22 +01:00
|
|
|
int tcp_backlog;
|
2012-05-29 11:24:24 +01:00
|
|
|
/** (static) file name of UNIX control socket (or NULL if none) */
|
2012-05-18 18:44:34 +01:00
|
|
|
char* control_socket_name;
|
2012-05-29 04:03:28 +01:00
|
|
|
/** size of file */
|
2012-06-11 13:57:03 +01:00
|
|
|
uint64_t size;
|
2012-05-29 00:59:12 +01:00
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** Claims around any I/O to this file */
|
2012-07-11 09:43:16 +01:00
|
|
|
struct flexthread_mutex * l_io;
|
2012-10-09 17:20:39 +01:00
|
|
|
|
2012-05-29 04:03:28 +01:00
|
|
|
/** to interrupt accept loop and clients, write() to close_signal[1] */
|
2012-06-06 12:41:03 +01:00
|
|
|
struct self_pipe * close_signal;
|
2012-05-21 04:03:17 +01:00
|
|
|
|
2012-06-08 11:02:40 +01:00
|
|
|
/** access control list */
|
|
|
|
struct acl * acl;
|
2012-06-08 10:32:33 +01:00
|
|
|
/** acl_updated_signal will be signalled after the acl struct
|
|
|
|
* has been replaced
|
|
|
|
*/
|
|
|
|
struct self_pipe * acl_updated_signal;
|
2012-07-11 09:43:16 +01:00
|
|
|
|
|
|
|
/* Claimed around any updates to the ACL. */
|
|
|
|
struct flexthread_mutex * l_acl;
|
2012-06-08 10:32:33 +01:00
|
|
|
|
2012-10-04 14:41:55 +01:00
|
|
|
/* Claimed around starting a mirror so that it doesn't race with
|
|
|
|
* shutting down on a SIGTERM. */
|
|
|
|
struct flexthread_mutex * l_start_mirror;
|
|
|
|
|
2012-07-12 14:54:48 +01:00
|
|
|
struct mirror* mirror;
|
2012-10-09 17:20:39 +01:00
|
|
|
struct mirror_super * mirror_super;
|
2012-10-04 14:41:55 +01:00
|
|
|
/* This is used to stop the mirror from starting after we
|
|
|
|
* receive a SIGTERM */
|
|
|
|
int mirror_can_start;
|
|
|
|
|
2012-06-06 10:19:45 +01:00
|
|
|
int server_fd;
|
2012-06-06 10:35:50 +01:00
|
|
|
int control_fd;
|
2012-10-09 17:20:39 +01:00
|
|
|
|
|
|
|
/* the allocation_map keeps track of which blocks in the backing file
|
2012-10-07 21:55:01 +01:00
|
|
|
* have been allocated, or part-allocated on disc, with unallocated
|
|
|
|
* blocks presumed to contain zeroes (i.e. represented as sparse files
|
|
|
|
* by the filesystem). We can use this information when receiving
|
|
|
|
* incoming writes, and avoid writing zeroes to unallocated sections
|
2012-10-09 17:20:39 +01:00
|
|
|
* of the file which would needlessly increase disc usage. This
|
2012-10-07 21:55:01 +01:00
|
|
|
* bitmap will start at all-zeroes for an empty file, and tend towards
|
|
|
|
* all-ones as the file is written to (i.e. we assume that allocated
|
|
|
|
* blocks can never become unallocated again, as is the case with ext3
|
|
|
|
* at least).
|
|
|
|
*/
|
2012-06-07 11:17:02 +01:00
|
|
|
struct bitset_mapping* allocation_map;
|
2012-10-07 21:55:01 +01:00
|
|
|
/* when starting up, this thread builds the allocation_map */
|
|
|
|
pthread_t allocation_map_builder_thread;
|
|
|
|
/* when the thread has finished, it sets this to 1 */
|
|
|
|
volatile sig_atomic_t allocation_map_built;
|
2012-10-09 17:20:39 +01:00
|
|
|
|
2012-06-21 17:22:34 +01:00
|
|
|
int max_nbd_clients;
|
|
|
|
struct client_tbl_entry *nbd_client;
|
2012-06-21 18:01:50 +01:00
|
|
|
|
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
/* Marker for whether this server has control over the data in
|
|
|
|
* the file, or if we're waiting to receive it from an inbound
|
|
|
|
* migration which hasn't yet finished.
|
2012-10-09 17:20:39 +01:00
|
|
|
*
|
|
|
|
* It's the value which controls the exit status of a serve or
|
|
|
|
* listen process.
|
2012-06-21 18:01:50 +01:00
|
|
|
*/
|
2012-10-09 17:20:39 +01:00
|
|
|
int success;
|
2012-05-17 20:14:22 +01:00
|
|
|
};
|
|
|
|
|
2012-10-09 17:20:39 +01:00
|
|
|
struct server * server_create(
|
2012-06-27 15:45:33 +01:00
|
|
|
struct flexnbd * flexnbd,
|
2012-10-09 17:20:39 +01:00
|
|
|
char* s_ip_address,
|
|
|
|
char* s_port,
|
2012-06-21 17:22:34 +01:00
|
|
|
char* s_file,
|
|
|
|
int default_deny,
|
2012-10-09 17:20:39 +01:00
|
|
|
int acl_entries,
|
2012-06-21 17:22:34 +01:00
|
|
|
char** s_acl_entries,
|
2012-06-21 18:01:50 +01:00
|
|
|
int max_nbd_clients,
|
2012-10-09 17:20:39 +01:00
|
|
|
int success );
|
2012-06-08 18:03:41 +01:00
|
|
|
void server_destroy( struct server * );
|
2012-06-06 14:25:35 +01:00
|
|
|
int server_is_closed(struct server* serve);
|
2012-06-06 11:27:52 +01:00
|
|
|
void server_dirty(struct server *serve, off64_t from, int len);
|
2012-06-08 11:02:40 +01:00
|
|
|
void server_lock_io( struct server * serve);
|
2012-06-06 13:29:13 +01:00
|
|
|
void server_unlock_io( struct server* serve );
|
2012-06-06 12:41:03 +01:00
|
|
|
void serve_signal_close( struct server *serve );
|
2012-06-13 13:44:21 +01:00
|
|
|
void serve_wait_for_close( struct server * serve );
|
2012-06-08 18:03:41 +01:00
|
|
|
void server_replace_acl( struct server *serve, struct acl * acl);
|
2012-06-21 18:01:50 +01:00
|
|
|
void server_control_arrived( struct server *serve );
|
2012-06-22 10:05:41 +01:00
|
|
|
int server_is_in_control( struct server *serve );
|
2012-06-27 15:45:33 +01:00
|
|
|
int server_default_deny( struct server * serve );
|
2012-07-11 09:43:16 +01:00
|
|
|
int server_io_locked( struct server * serve );
|
|
|
|
int server_acl_locked( struct server * serve );
|
|
|
|
void server_lock_acl( struct server *serve );
|
|
|
|
void server_unlock_acl( struct server *serve );
|
2012-10-04 14:41:55 +01:00
|
|
|
void server_lock_start_mirror( struct server *serve );
|
|
|
|
void server_unlock_start_mirror( struct server *serve );
|
2012-07-17 16:30:49 +01:00
|
|
|
int server_is_mirroring( struct server * serve );
|
|
|
|
void server_abandon_mirror( struct server * serve );
|
2012-10-04 14:41:55 +01:00
|
|
|
void server_prevent_mirror_start( struct server *serve );
|
|
|
|
void server_allow_mirror_start( struct server *serve );
|
|
|
|
int server_mirror_can_start( struct server *serve );
|
2012-07-11 09:43:16 +01:00
|
|
|
|
2012-07-23 10:22:25 +01:00
|
|
|
void server_unlink( struct server * serve );
|
2012-06-06 11:27:52 +01:00
|
|
|
|
2012-06-21 18:01:50 +01:00
|
|
|
int do_serve( struct server * );
|
2012-06-06 11:27:52 +01:00
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
struct mode_readwrite_params {
|
|
|
|
union mysockaddr connect_to;
|
2012-06-06 09:55:08 +01:00
|
|
|
union mysockaddr connect_from;
|
2012-05-17 20:14:22 +01:00
|
|
|
off64_t from;
|
|
|
|
off64_t len;
|
|
|
|
int data_fd;
|
|
|
|
int client;
|
|
|
|
};
|
|
|
|
|
2012-05-19 12:48:03 +01:00
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
#endif
|
|
|
|
|