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