1 /*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net> 4 * All rights reserved. 5 * 6 * This software was developed by Pawel Jakub Dawidek under sponsorship from 7 * the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 #ifndef _HAST_H_ 34 #define _HAST_H_ 35 36 #include <sys/queue.h> 37 #include <sys/socket.h> 38 39 #include <arpa/inet.h> 40 41 #include <netinet/in.h> 42 43 #include <limits.h> 44 #include <pthread.h> 45 #include <stdbool.h> 46 #include <stdint.h> 47 48 #include <activemap.h> 49 50 #include "proto.h" 51 52 /* 53 * Version history: 54 * 0 - initial version 55 * 1 - HIO_KEEPALIVE added 56 */ 57 #define HAST_PROTO_VERSION 1 58 59 #define EHAST_OK 0 60 #define EHAST_NOENTRY 1 61 #define EHAST_INVALID 2 62 #define EHAST_NOMEMORY 3 63 #define EHAST_UNIMPLEMENTED 4 64 65 #define HASTCTL_CMD_UNKNOWN 0 66 #define HASTCTL_CMD_SETROLE 1 67 #define HASTCTL_CMD_STATUS 2 68 69 #define HAST_ROLE_UNDEF 0 70 #define HAST_ROLE_INIT 1 71 #define HAST_ROLE_PRIMARY 2 72 #define HAST_ROLE_SECONDARY 3 73 74 #define HAST_SYNCSRC_UNDEF 0 75 #define HAST_SYNCSRC_PRIMARY 1 76 #define HAST_SYNCSRC_SECONDARY 2 77 78 #define HIO_UNDEF 0 79 #define HIO_READ 1 80 #define HIO_WRITE 2 81 #define HIO_DELETE 3 82 #define HIO_FLUSH 4 83 #define HIO_KEEPALIVE 5 84 85 #define HAST_USER "hast" 86 #define HAST_TIMEOUT 20 87 #define HAST_CONFIG "/etc/hast.conf" 88 #define HAST_CONTROL "/var/run/hastctl" 89 #define HASTD_LISTEN_TCP4 "tcp4://0.0.0.0:8457" 90 #define HASTD_LISTEN_TCP6 "tcp6://[::]:8457" 91 #define HASTD_PIDFILE "/var/run/hastd.pid" 92 93 /* Default extent size. */ 94 #define HAST_EXTENTSIZE 2097152 95 /* Default maximum number of extents that are kept dirty. */ 96 #define HAST_KEEPDIRTY 64 97 98 #define HAST_ADDRSIZE 1024 99 #define HAST_TOKEN_SIZE 16 100 101 /* Number of seconds to sleep between reconnect retries or keepalive packets. */ 102 #define HAST_KEEPALIVE 10 103 104 struct hastd_listen { 105 /* Address to listen on. */ 106 char hl_addr[HAST_ADDRSIZE]; 107 /* Protocol-specific data. */ 108 struct proto_conn *hl_conn; 109 TAILQ_ENTRY(hastd_listen) hl_next; 110 }; 111 112 struct hastd_config { 113 /* Address to communicate with hastctl(8). */ 114 char hc_controladdr[HAST_ADDRSIZE]; 115 /* Protocol-specific data. */ 116 struct proto_conn *hc_controlconn; 117 /* Incoming control connection. */ 118 struct proto_conn *hc_controlin; 119 /* PID file path. */ 120 char hc_pidfile[PATH_MAX]; 121 /* List of addresses to listen on. */ 122 TAILQ_HEAD(, hastd_listen) hc_listen; 123 /* List of resources. */ 124 TAILQ_HEAD(, hast_resource) hc_resources; 125 }; 126 127 #define HAST_REPLICATION_FULLSYNC 0 128 #define HAST_REPLICATION_MEMSYNC 1 129 #define HAST_REPLICATION_ASYNC 2 130 131 #define HAST_COMPRESSION_NONE 0 132 #define HAST_COMPRESSION_HOLE 1 133 #define HAST_COMPRESSION_LZF 2 134 135 #define HAST_CHECKSUM_NONE 0 136 #define HAST_CHECKSUM_CRC32 1 137 #define HAST_CHECKSUM_SHA256 2 138 139 /* 140 * Structure that describes single resource. 141 */ 142 struct hast_resource { 143 /* Resource name. */ 144 char hr_name[NAME_MAX]; 145 /* Replication mode (HAST_REPLICATION_*). */ 146 int hr_replication; 147 /* Provider name that will appear in /dev/hast/. */ 148 char hr_provname[NAME_MAX]; 149 /* Synchronization extent size. */ 150 int hr_extentsize; 151 /* Maximum number of extents that are kept dirty. */ 152 int hr_keepdirty; 153 /* Path to a program to execute on various events. */ 154 char hr_exec[PATH_MAX]; 155 /* Compression algorithm. */ 156 int hr_compression; 157 /* Checksum algorithm. */ 158 int hr_checksum; 159 160 /* Path to local component. */ 161 char hr_localpath[PATH_MAX]; 162 /* Descriptor to access local component. */ 163 int hr_localfd; 164 /* Offset into local component. */ 165 off_t hr_localoff; 166 /* Size of usable space. */ 167 off_t hr_datasize; 168 /* Size of entire local provider. */ 169 off_t hr_local_mediasize; 170 /* Sector size of local provider. */ 171 unsigned int hr_local_sectorsize; 172 /* Is flushing write cache supported by the local provider? */ 173 bool hr_localflush; 174 /* Flush write cache on metadata updates? */ 175 int hr_metaflush; 176 177 /* Descriptor for /dev/ggctl communication. */ 178 int hr_ggatefd; 179 /* Unit number for ggate communication. */ 180 int hr_ggateunit; 181 182 /* Address of the remote component. */ 183 char hr_remoteaddr[HAST_ADDRSIZE]; 184 /* Local address to bind to for outgoing connections. */ 185 char hr_sourceaddr[HAST_ADDRSIZE]; 186 /* Connection for incoming data. */ 187 struct proto_conn *hr_remotein; 188 /* Connection for outgoing data. */ 189 struct proto_conn *hr_remoteout; 190 /* Token to verify both in and out connection are coming from 191 the same node (not necessarily from the same address). */ 192 unsigned char hr_token[HAST_TOKEN_SIZE]; 193 /* Connection timeout. */ 194 int hr_timeout; 195 196 /* Resource unique identifier. */ 197 uint64_t hr_resuid; 198 /* Primary's local modification count. */ 199 uint64_t hr_primary_localcnt; 200 /* Primary's remote modification count. */ 201 uint64_t hr_primary_remotecnt; 202 /* Secondary's local modification count. */ 203 uint64_t hr_secondary_localcnt; 204 /* Secondary's remote modification count. */ 205 uint64_t hr_secondary_remotecnt; 206 /* Synchronization source. */ 207 uint8_t hr_syncsrc; 208 209 /* Resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */ 210 int hr_role; 211 /* Previous resource role: HAST_ROLE_{INIT,PRIMARY,SECONDARY}. */ 212 int hr_previous_role; 213 /* PID of child worker process. 0 - no child. */ 214 pid_t hr_workerpid; 215 /* Control commands from parent to child. */ 216 struct proto_conn *hr_ctrl; 217 /* Events from child to parent. */ 218 struct proto_conn *hr_event; 219 /* Connection requests from child to parent. */ 220 struct proto_conn *hr_conn; 221 222 /* Activemap structure. */ 223 struct activemap *hr_amp; 224 /* Locked used to synchronize access to hr_amp. */ 225 pthread_mutex_t hr_amp_lock; 226 227 /* Number of BIO_READ requests. */ 228 uint64_t hr_stat_read; 229 /* Number of BIO_WRITE requests. */ 230 uint64_t hr_stat_write; 231 /* Number of BIO_DELETE requests. */ 232 uint64_t hr_stat_delete; 233 /* Number of BIO_FLUSH requests. */ 234 uint64_t hr_stat_flush; 235 /* Number of activemap updates. */ 236 uint64_t hr_stat_activemap_update; 237 238 /* Next resource. */ 239 TAILQ_ENTRY(hast_resource) hr_next; 240 }; 241 242 struct hastd_config *yy_config_parse(const char *config, bool exitonerror); 243 void yy_config_free(struct hastd_config *config); 244 245 void yyerror(const char *); 246 int yylex(void); 247 248 #endif /* !_HAST_H_ */ 249