secondary.c revision 218049
1/*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 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 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/sbin/hastd/secondary.c 218049 2011-01-28 22:35:46Z pjd $"); 33 34#include <sys/param.h> 35#include <sys/time.h> 36#include <sys/bio.h> 37#include <sys/disk.h> 38#include <sys/stat.h> 39 40#include <assert.h> 41#include <err.h> 42#include <errno.h> 43#include <fcntl.h> 44#include <libgeom.h> 45#include <pthread.h> 46#include <signal.h> 47#include <stdint.h> 48#include <stdio.h> 49#include <string.h> 50#include <sysexits.h> 51#include <unistd.h> 52 53#include <activemap.h> 54#include <nv.h> 55#include <pjdlog.h> 56 57#include "control.h" 58#include "event.h" 59#include "hast.h" 60#include "hast_proto.h" 61#include "hastd.h" 62#include "hooks.h" 63#include "metadata.h" 64#include "proto.h" 65#include "subr.h" 66#include "synch.h" 67 68struct hio { 69 uint64_t hio_seq; 70 int hio_error; 71 struct nv *hio_nv; 72 void *hio_data; 73 uint8_t hio_cmd; 74 uint64_t hio_offset; 75 uint64_t hio_length; 76 TAILQ_ENTRY(hio) hio_next; 77}; 78 79static struct hast_resource *gres; 80 81/* 82 * Free list holds unused structures. When free list is empty, we have to wait 83 * until some in-progress requests are freed. 84 */ 85static TAILQ_HEAD(, hio) hio_free_list; 86static pthread_mutex_t hio_free_list_lock; 87static pthread_cond_t hio_free_list_cond; 88/* 89 * Disk thread (the one that do I/O requests) takes requests from this list. 90 */ 91static TAILQ_HEAD(, hio) hio_disk_list; 92static pthread_mutex_t hio_disk_list_lock; 93static pthread_cond_t hio_disk_list_cond; 94/* 95 * There is one recv list for every component, although local components don't 96 * use recv lists as local requests are done synchronously. 97 */ 98static TAILQ_HEAD(, hio) hio_send_list; 99static pthread_mutex_t hio_send_list_lock; 100static pthread_cond_t hio_send_list_cond; 101 102/* 103 * Maximum number of outstanding I/O requests. 104 */ 105#define HAST_HIO_MAX 256 106 107static void *recv_thread(void *arg); 108static void *disk_thread(void *arg); 109static void *send_thread(void *arg); 110 111#define QUEUE_INSERT(name, hio) do { \ 112 bool _wakeup; \ 113 \ 114 mtx_lock(&hio_##name##_list_lock); \ 115 _wakeup = TAILQ_EMPTY(&hio_##name##_list); \ 116 TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next); \ 117 mtx_unlock(&hio_##name##_list_lock); \ 118 if (_wakeup) \ 119 cv_signal(&hio_##name##_list_cond); \ 120} while (0) 121#define QUEUE_TAKE(name, hio) do { \ 122 mtx_lock(&hio_##name##_list_lock); \ 123 while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \ 124 cv_wait(&hio_##name##_list_cond, \ 125 &hio_##name##_list_lock); \ 126 } \ 127 TAILQ_REMOVE(&hio_##name##_list, (hio), hio_next); \ 128 mtx_unlock(&hio_##name##_list_lock); \ 129} while (0) 130 131static void 132init_environment(void) 133{ 134 struct hio *hio; 135 unsigned int ii; 136 137 /* 138 * Initialize lists, their locks and theirs condition variables. 139 */ 140 TAILQ_INIT(&hio_free_list); 141 mtx_init(&hio_free_list_lock); 142 cv_init(&hio_free_list_cond); 143 TAILQ_INIT(&hio_disk_list); 144 mtx_init(&hio_disk_list_lock); 145 cv_init(&hio_disk_list_cond); 146 TAILQ_INIT(&hio_send_list); 147 mtx_init(&hio_send_list_lock); 148 cv_init(&hio_send_list_cond); 149 150 /* 151 * Allocate requests pool and initialize requests. 152 */ 153 for (ii = 0; ii < HAST_HIO_MAX; ii++) { 154 hio = malloc(sizeof(*hio)); 155 if (hio == NULL) { 156 pjdlog_exitx(EX_TEMPFAIL, 157 "Unable to allocate memory (%zu bytes) for hio request.", 158 sizeof(*hio)); 159 } 160 hio->hio_error = 0; 161 hio->hio_data = malloc(MAXPHYS); 162 if (hio->hio_data == NULL) { 163 pjdlog_exitx(EX_TEMPFAIL, 164 "Unable to allocate memory (%zu bytes) for gctl_data.", 165 (size_t)MAXPHYS); 166 } 167 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next); 168 } 169} 170 171static void 172init_local(struct hast_resource *res) 173{ 174 175 if (metadata_read(res, true) < 0) 176 exit(EX_NOINPUT); 177} 178 179static void 180init_remote(struct hast_resource *res, struct nv *nvin) 181{ 182 uint64_t resuid; 183 struct nv *nvout; 184 unsigned char *map; 185 size_t mapsize; 186 187 map = NULL; 188 mapsize = 0; 189 nvout = nv_alloc(); 190 nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize"); 191 nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize"); 192 resuid = nv_get_uint64(nvin, "resuid"); 193 res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt"); 194 res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt"); 195 nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt"); 196 nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt"); 197 mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize - 198 METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize); 199 map = malloc(mapsize); 200 if (map == NULL) { 201 pjdlog_exitx(EX_TEMPFAIL, 202 "Unable to allocate memory (%zu bytes) for activemap.", 203 mapsize); 204 } 205 nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize"); 206 /* 207 * When we work as primary and secondary is missing we will increase 208 * localcnt in our metadata. When secondary is connected and synced 209 * we make localcnt be equal to remotecnt, which means nodes are more 210 * or less in sync. 211 * Split-brain condition is when both nodes are not able to communicate 212 * and are both configured as primary nodes. In turn, they can both 213 * make incompatible changes to the data and we have to detect that. 214 * Under split-brain condition we will increase our localcnt on first 215 * write and remote node will increase its localcnt on first write. 216 * When we connect we can see that primary's localcnt is greater than 217 * our remotecnt (primary was modified while we weren't watching) and 218 * our localcnt is greater than primary's remotecnt (we were modified 219 * while primary wasn't watching). 220 * There are many possible combinations which are all gathered below. 221 * Don't pay too much attention to exact numbers, the more important 222 * is to compare them. We compare secondary's local with primary's 223 * remote and secondary's remote with primary's local. 224 * Note that every case where primary's localcnt is smaller than 225 * secondary's remotecnt and where secondary's localcnt is smaller than 226 * primary's remotecnt should be impossible in practise. We will perform 227 * full synchronization then. Those cases are marked with an asterisk. 228 * Regular synchronization means that only extents marked as dirty are 229 * synchronized (regular synchronization). 230 * 231 * SECONDARY METADATA PRIMARY METADATA 232 * local=3 remote=3 local=2 remote=2* ?! Full sync from secondary. 233 * local=3 remote=3 local=2 remote=3* ?! Full sync from primary. 234 * local=3 remote=3 local=2 remote=4* ?! Full sync from primary. 235 * local=3 remote=3 local=3 remote=2 Primary is out-of-date, 236 * regular sync from secondary. 237 * local=3 remote=3 local=3 remote=3 Regular sync just in case. 238 * local=3 remote=3 local=3 remote=4* ?! Full sync from primary. 239 * local=3 remote=3 local=4 remote=2 Split-brain condition. 240 * local=3 remote=3 local=4 remote=3 Secondary out-of-date, 241 * regular sync from primary. 242 * local=3 remote=3 local=4 remote=4* ?! Full sync from primary. 243 */ 244 if (res->hr_resuid == 0) { 245 /* 246 * Provider is used for the first time. If primary node done no 247 * writes yet as well (we will find "virgin" argument) then 248 * there is no need to synchronize anything. If primary node 249 * done any writes already we have to synchronize everything. 250 */ 251 assert(res->hr_secondary_localcnt == 0); 252 res->hr_resuid = resuid; 253 if (metadata_write(res) < 0) 254 exit(EX_NOINPUT); 255 if (nv_exists(nvin, "virgin")) { 256 free(map); 257 map = NULL; 258 mapsize = 0; 259 } else { 260 memset(map, 0xff, mapsize); 261 } 262 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 263 } else if ( 264 /* Is primary is out-of-date? */ 265 (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 266 res->hr_secondary_remotecnt == res->hr_primary_localcnt) || 267 /* Node are more or less in sync? */ 268 (res->hr_secondary_localcnt == res->hr_primary_remotecnt && 269 res->hr_secondary_remotecnt == res->hr_primary_localcnt) || 270 /* Is secondary is out-of-date? */ 271 (res->hr_secondary_localcnt == res->hr_primary_remotecnt && 272 res->hr_secondary_remotecnt < res->hr_primary_localcnt)) { 273 /* 274 * Nodes are more or less in sync or one of the nodes is 275 * out-of-date. 276 * It doesn't matter at this point which one, we just have to 277 * send out local bitmap to the remote node. 278 */ 279 if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) != 280 (ssize_t)mapsize) { 281 pjdlog_exit(LOG_ERR, "Unable to read activemap"); 282 } 283 if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 284 res->hr_secondary_remotecnt == res->hr_primary_localcnt) { 285 /* Primary is out-of-date, sync from secondary. */ 286 nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc"); 287 } else { 288 /* 289 * Secondary is out-of-date or counts match. 290 * Sync from primary. 291 */ 292 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 293 } 294 } else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 295 res->hr_primary_localcnt > res->hr_secondary_remotecnt) { 296 /* 297 * Not good, we have split-brain condition. 298 */ 299 pjdlog_error("Split-brain detected, exiting."); 300 nv_add_string(nvout, "Split-brain condition!", "errmsg"); 301 free(map); 302 map = NULL; 303 mapsize = 0; 304 } else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt || 305 res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ { 306 /* 307 * This should never happen in practise, but we will perform 308 * full synchronization. 309 */ 310 assert(res->hr_secondary_localcnt < res->hr_primary_remotecnt || 311 res->hr_primary_localcnt < res->hr_secondary_remotecnt); 312 mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize - 313 METADATA_SIZE, res->hr_extentsize, 314 res->hr_local_sectorsize); 315 memset(map, 0xff, mapsize); 316 if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) { 317 /* In this one of five cases sync from secondary. */ 318 nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc"); 319 } else { 320 /* For the rest four cases sync from primary. */ 321 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc"); 322 } 323 pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).", 324 (uintmax_t)res->hr_primary_localcnt, 325 (uintmax_t)res->hr_primary_remotecnt, 326 (uintmax_t)res->hr_secondary_localcnt, 327 (uintmax_t)res->hr_secondary_remotecnt); 328 } 329 if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) < 0) { 330 pjdlog_exit(EX_TEMPFAIL, "Unable to send activemap to %s", 331 res->hr_remoteaddr); 332 } 333 if (map != NULL) 334 free(map); 335 nv_free(nvout); 336 if (res->hr_secondary_localcnt > res->hr_primary_remotecnt && 337 res->hr_primary_localcnt > res->hr_secondary_remotecnt) { 338 /* Exit on split-brain. */ 339 event_send(res, EVENT_SPLITBRAIN); 340 exit(EX_CONFIG); 341 } 342} 343 344void 345hastd_secondary(struct hast_resource *res, struct nv *nvin) 346{ 347 sigset_t mask; 348 pthread_t td; 349 pid_t pid; 350 int error, mode; 351 352 /* 353 * Create communication channel between parent and child. 354 */ 355 if (proto_client("socketpair://", &res->hr_ctrl) < 0) { 356 KEEP_ERRNO((void)pidfile_remove(pfh)); 357 pjdlog_exit(EX_OSERR, 358 "Unable to create control sockets between parent and child"); 359 } 360 /* 361 * Create communication channel between child and parent. 362 */ 363 if (proto_client("socketpair://", &res->hr_event) < 0) { 364 KEEP_ERRNO((void)pidfile_remove(pfh)); 365 pjdlog_exit(EX_OSERR, 366 "Unable to create event sockets between child and parent"); 367 } 368 369 pid = fork(); 370 if (pid < 0) { 371 KEEP_ERRNO((void)pidfile_remove(pfh)); 372 pjdlog_exit(EX_OSERR, "Unable to fork"); 373 } 374 375 if (pid > 0) { 376 /* This is parent. */ 377 proto_close(res->hr_remotein); 378 res->hr_remotein = NULL; 379 proto_close(res->hr_remoteout); 380 res->hr_remoteout = NULL; 381 /* Declare that we are receiver. */ 382 proto_recv(res->hr_event, NULL, 0); 383 /* Declare that we are sender. */ 384 proto_send(res->hr_ctrl, NULL, 0); 385 res->hr_workerpid = pid; 386 return; 387 } 388 389 gres = res; 390 mode = pjdlog_mode_get(); 391 392 /* Declare that we are sender. */ 393 proto_send(res->hr_event, NULL, 0); 394 /* Declare that we are receiver. */ 395 proto_recv(res->hr_ctrl, NULL, 0); 396 descriptors_cleanup(res); 397 398 descriptors_assert(res, mode); 399 400 pjdlog_init(mode); 401 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 402 setproctitle("%s (secondary)", res->hr_name); 403 404 PJDLOG_VERIFY(sigemptyset(&mask) == 0); 405 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); 406 407 /* Error in setting timeout is not critical, but why should it fail? */ 408 if (proto_timeout(res->hr_remotein, 0) < 0) 409 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 410 if (proto_timeout(res->hr_remoteout, res->hr_timeout) < 0) 411 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 412 413 init_local(res); 414 init_environment(); 415 416 if (drop_privs() != 0) 417 exit(EX_CONFIG); 418 419 /* 420 * Create the control thread before sending any event to the parent, 421 * as we can deadlock when parent sends control request to worker, 422 * but worker has no control thread started yet, so parent waits. 423 * In the meantime worker sends an event to the parent, but parent 424 * is unable to handle the event, because it waits for control 425 * request response. 426 */ 427 error = pthread_create(&td, NULL, ctrl_thread, res); 428 assert(error == 0); 429 430 init_remote(res, nvin); 431 event_send(res, EVENT_CONNECT); 432 433 error = pthread_create(&td, NULL, recv_thread, res); 434 assert(error == 0); 435 error = pthread_create(&td, NULL, disk_thread, res); 436 assert(error == 0); 437 (void)send_thread(res); 438} 439 440static void 441reqlog(int loglevel, int debuglevel, int error, struct hio *hio, const char *fmt, ...) 442{ 443 char msg[1024]; 444 va_list ap; 445 int len; 446 447 va_start(ap, fmt); 448 len = vsnprintf(msg, sizeof(msg), fmt, ap); 449 va_end(ap); 450 if ((size_t)len < sizeof(msg)) { 451 switch (hio->hio_cmd) { 452 case HIO_READ: 453 (void)snprintf(msg + len, sizeof(msg) - len, 454 "READ(%ju, %ju).", (uintmax_t)hio->hio_offset, 455 (uintmax_t)hio->hio_length); 456 break; 457 case HIO_DELETE: 458 (void)snprintf(msg + len, sizeof(msg) - len, 459 "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset, 460 (uintmax_t)hio->hio_length); 461 break; 462 case HIO_FLUSH: 463 (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH."); 464 break; 465 case HIO_WRITE: 466 (void)snprintf(msg + len, sizeof(msg) - len, 467 "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset, 468 (uintmax_t)hio->hio_length); 469 break; 470 case HIO_KEEPALIVE: 471 (void)snprintf(msg + len, sizeof(msg) - len, "KEEPALIVE."); 472 break; 473 default: 474 (void)snprintf(msg + len, sizeof(msg) - len, 475 "UNKNOWN(%u).", (unsigned int)hio->hio_cmd); 476 break; 477 } 478 } 479 pjdlog_common(loglevel, debuglevel, error, "%s", msg); 480} 481 482static int 483requnpack(struct hast_resource *res, struct hio *hio) 484{ 485 486 hio->hio_cmd = nv_get_uint8(hio->hio_nv, "cmd"); 487 if (hio->hio_cmd == 0) { 488 pjdlog_error("Header contains no 'cmd' field."); 489 hio->hio_error = EINVAL; 490 goto end; 491 } 492 switch (hio->hio_cmd) { 493 case HIO_KEEPALIVE: 494 break; 495 case HIO_READ: 496 case HIO_WRITE: 497 case HIO_DELETE: 498 hio->hio_offset = nv_get_uint64(hio->hio_nv, "offset"); 499 if (nv_error(hio->hio_nv) != 0) { 500 pjdlog_error("Header is missing 'offset' field."); 501 hio->hio_error = EINVAL; 502 goto end; 503 } 504 hio->hio_length = nv_get_uint64(hio->hio_nv, "length"); 505 if (nv_error(hio->hio_nv) != 0) { 506 pjdlog_error("Header is missing 'length' field."); 507 hio->hio_error = EINVAL; 508 goto end; 509 } 510 if (hio->hio_length == 0) { 511 pjdlog_error("Data length is zero."); 512 hio->hio_error = EINVAL; 513 goto end; 514 } 515 if (hio->hio_length > MAXPHYS) { 516 pjdlog_error("Data length is too large (%ju > %ju).", 517 (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS); 518 hio->hio_error = EINVAL; 519 goto end; 520 } 521 if ((hio->hio_offset % res->hr_local_sectorsize) != 0) { 522 pjdlog_error("Offset %ju is not multiple of sector size.", 523 (uintmax_t)hio->hio_offset); 524 hio->hio_error = EINVAL; 525 goto end; 526 } 527 if ((hio->hio_length % res->hr_local_sectorsize) != 0) { 528 pjdlog_error("Length %ju is not multiple of sector size.", 529 (uintmax_t)hio->hio_length); 530 hio->hio_error = EINVAL; 531 goto end; 532 } 533 if (hio->hio_offset + hio->hio_length > 534 (uint64_t)res->hr_datasize) { 535 pjdlog_error("Data offset is too large (%ju > %ju).", 536 (uintmax_t)(hio->hio_offset + hio->hio_length), 537 (uintmax_t)res->hr_datasize); 538 hio->hio_error = EINVAL; 539 goto end; 540 } 541 break; 542 default: 543 pjdlog_error("Header contains invalid 'cmd' (%hhu).", 544 hio->hio_cmd); 545 hio->hio_error = EINVAL; 546 goto end; 547 } 548 hio->hio_error = 0; 549end: 550 return (hio->hio_error); 551} 552 553static __dead2 void 554secondary_exit(int exitcode, const char *fmt, ...) 555{ 556 va_list ap; 557 558 assert(exitcode != EX_OK); 559 va_start(ap, fmt); 560 pjdlogv_errno(LOG_ERR, fmt, ap); 561 va_end(ap); 562 event_send(gres, EVENT_DISCONNECT); 563 exit(exitcode); 564} 565 566/* 567 * Thread receives requests from the primary node. 568 */ 569static void * 570recv_thread(void *arg) 571{ 572 struct hast_resource *res = arg; 573 struct hio *hio; 574 575 for (;;) { 576 pjdlog_debug(2, "recv: Taking free request."); 577 QUEUE_TAKE(free, hio); 578 pjdlog_debug(2, "recv: (%p) Got request.", hio); 579 if (hast_proto_recv_hdr(res->hr_remotein, &hio->hio_nv) < 0) { 580 secondary_exit(EX_TEMPFAIL, 581 "Unable to receive request header"); 582 } 583 if (requnpack(res, hio) != 0) { 584 pjdlog_debug(2, 585 "recv: (%p) Moving request to the send queue.", 586 hio); 587 QUEUE_INSERT(send, hio); 588 continue; 589 } 590 reqlog(LOG_DEBUG, 2, -1, hio, 591 "recv: (%p) Got request header: ", hio); 592 if (hio->hio_cmd == HIO_KEEPALIVE) { 593 pjdlog_debug(2, 594 "recv: (%p) Moving request to the free queue.", 595 hio); 596 nv_free(hio->hio_nv); 597 QUEUE_INSERT(free, hio); 598 continue; 599 } else if (hio->hio_cmd == HIO_WRITE) { 600 if (hast_proto_recv_data(res, res->hr_remotein, 601 hio->hio_nv, hio->hio_data, MAXPHYS) < 0) { 602 secondary_exit(EX_TEMPFAIL, 603 "Unable to receive request data"); 604 } 605 } 606 pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.", 607 hio); 608 QUEUE_INSERT(disk, hio); 609 } 610 /* NOTREACHED */ 611 return (NULL); 612} 613 614/* 615 * Thread reads from or writes to local component and also handles DELETE and 616 * FLUSH requests. 617 */ 618static void * 619disk_thread(void *arg) 620{ 621 struct hast_resource *res = arg; 622 struct hio *hio; 623 ssize_t ret; 624 bool clear_activemap; 625 626 clear_activemap = true; 627 628 for (;;) { 629 pjdlog_debug(2, "disk: Taking request."); 630 QUEUE_TAKE(disk, hio); 631 while (clear_activemap) { 632 unsigned char *map; 633 size_t mapsize; 634 635 /* 636 * When first request is received, it means that primary 637 * already received our activemap, merged it and stored 638 * locally. We can now safely clear our activemap. 639 */ 640 mapsize = 641 activemap_calc_ondisk_size(res->hr_local_mediasize - 642 METADATA_SIZE, res->hr_extentsize, 643 res->hr_local_sectorsize); 644 map = calloc(1, mapsize); 645 if (map == NULL) { 646 pjdlog_warning("Unable to allocate memory to clear local activemap."); 647 break; 648 } 649 if (pwrite(res->hr_localfd, map, mapsize, 650 METADATA_SIZE) != (ssize_t)mapsize) { 651 pjdlog_errno(LOG_WARNING, 652 "Unable to store cleared activemap"); 653 free(map); 654 break; 655 } 656 free(map); 657 clear_activemap = false; 658 pjdlog_debug(1, "Local activemap cleared."); 659 } 660 reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio); 661 /* Handle the actual request. */ 662 switch (hio->hio_cmd) { 663 case HIO_READ: 664 ret = pread(res->hr_localfd, hio->hio_data, 665 hio->hio_length, 666 hio->hio_offset + res->hr_localoff); 667 if (ret < 0) 668 hio->hio_error = errno; 669 else if (ret != (int64_t)hio->hio_length) 670 hio->hio_error = EIO; 671 else 672 hio->hio_error = 0; 673 break; 674 case HIO_WRITE: 675 ret = pwrite(res->hr_localfd, hio->hio_data, 676 hio->hio_length, 677 hio->hio_offset + res->hr_localoff); 678 if (ret < 0) 679 hio->hio_error = errno; 680 else if (ret != (int64_t)hio->hio_length) 681 hio->hio_error = EIO; 682 else 683 hio->hio_error = 0; 684 break; 685 case HIO_DELETE: 686 ret = g_delete(res->hr_localfd, 687 hio->hio_offset + res->hr_localoff, 688 hio->hio_length); 689 if (ret < 0) 690 hio->hio_error = errno; 691 else 692 hio->hio_error = 0; 693 break; 694 case HIO_FLUSH: 695 ret = g_flush(res->hr_localfd); 696 if (ret < 0) 697 hio->hio_error = errno; 698 else 699 hio->hio_error = 0; 700 break; 701 } 702 if (hio->hio_error != 0) { 703 reqlog(LOG_ERR, 0, hio->hio_error, hio, 704 "Request failed: "); 705 } 706 pjdlog_debug(2, "disk: (%p) Moving request to the send queue.", 707 hio); 708 QUEUE_INSERT(send, hio); 709 } 710 /* NOTREACHED */ 711 return (NULL); 712} 713 714/* 715 * Thread sends requests back to primary node. 716 */ 717static void * 718send_thread(void *arg) 719{ 720 struct hast_resource *res = arg; 721 struct nv *nvout; 722 struct hio *hio; 723 void *data; 724 size_t length; 725 726 for (;;) { 727 pjdlog_debug(2, "send: Taking request."); 728 QUEUE_TAKE(send, hio); 729 reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio); 730 nvout = nv_alloc(); 731 /* Copy sequence number. */ 732 nv_add_uint64(nvout, nv_get_uint64(hio->hio_nv, "seq"), "seq"); 733 switch (hio->hio_cmd) { 734 case HIO_READ: 735 if (hio->hio_error == 0) { 736 data = hio->hio_data; 737 length = hio->hio_length; 738 break; 739 } 740 /* 741 * We send no data in case of an error. 742 */ 743 /* FALLTHROUGH */ 744 case HIO_DELETE: 745 case HIO_FLUSH: 746 case HIO_WRITE: 747 data = NULL; 748 length = 0; 749 break; 750 default: 751 abort(); 752 break; 753 } 754 if (hio->hio_error != 0) 755 nv_add_int16(nvout, hio->hio_error, "error"); 756 if (hast_proto_send(res, res->hr_remoteout, nvout, data, 757 length) < 0) { 758 secondary_exit(EX_TEMPFAIL, "Unable to send reply."); 759 } 760 nv_free(nvout); 761 pjdlog_debug(2, "send: (%p) Moving request to the free queue.", 762 hio); 763 nv_free(hio->hio_nv); 764 hio->hio_error = 0; 765 QUEUE_INSERT(free, hio); 766 } 767 /* NOTREACHED */ 768 return (NULL); 769} 770