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