2 * Copyright (c) 2009 The FreeBSD Foundation
3 * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
40 #include <geom/gate/g_gate.h>
54 #include <activemap.h>
56 #include <rangelock.h>
61 #include "hast_proto.h"
71 /* The is only one remote component for now. */
72 #define ISREMOTE(no) ((no) == 1)
76 * Number of components we are still waiting for.
77 * When this field goes to 0, we can send the request back to the
78 * kernel. Each component has to decrease this counter by one
81 unsigned int hio_countdown;
83 * Each component has a place to store its own error.
84 * Once the request is handled by all components we can decide if the
85 * request overall is successful or not.
89 * Structure used to communicate with GEOM Gate class.
91 struct g_gate_ctl_io hio_ggio;
93 * Request was already confirmed to GEOM Gate.
97 * Remember replication from the time the request was initiated,
98 * so we won't get confused when replication changes on reload.
101 TAILQ_ENTRY(hio) *hio_next;
103 #define hio_free_next hio_next[0]
104 #define hio_done_next hio_next[0]
107 * Free list holds unused structures. When free list is empty, we have to wait
108 * until some in-progress requests are freed.
110 static TAILQ_HEAD(, hio) hio_free_list;
111 static pthread_mutex_t hio_free_list_lock;
112 static pthread_cond_t hio_free_list_cond;
114 * There is one send list for every component. One requests is placed on all
115 * send lists - each component gets the same request, but each component is
116 * responsible for managing his own send list.
118 static TAILQ_HEAD(, hio) *hio_send_list;
119 static pthread_mutex_t *hio_send_list_lock;
120 static pthread_cond_t *hio_send_list_cond;
122 * There is one recv list for every component, although local components don't
123 * use recv lists as local requests are done synchronously.
125 static TAILQ_HEAD(, hio) *hio_recv_list;
126 static pthread_mutex_t *hio_recv_list_lock;
127 static pthread_cond_t *hio_recv_list_cond;
129 * Request is placed on done list by the slowest component (the one that
130 * decreased hio_countdown from 1 to 0).
132 static TAILQ_HEAD(, hio) hio_done_list;
133 static pthread_mutex_t hio_done_list_lock;
134 static pthread_cond_t hio_done_list_cond;
136 * Structure below are for interaction with sync thread.
138 static bool sync_inprogress;
139 static pthread_mutex_t sync_lock;
140 static pthread_cond_t sync_cond;
142 * The lock below allows to synchornize access to remote connections.
144 static pthread_rwlock_t *hio_remote_lock;
147 * Lock to synchronize metadata updates. Also synchronize access to
148 * hr_primary_localcnt and hr_primary_remotecnt fields.
150 static pthread_mutex_t metadata_lock;
153 * Maximum number of outstanding I/O requests.
155 #define HAST_HIO_MAX 256
157 * Number of components. At this point there are only two components: local
158 * and remote, but in the future it might be possible to use multiple local
159 * and remote components.
161 #define HAST_NCOMPONENTS 2
163 #define ISCONNECTED(res, no) \
164 ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
166 #define QUEUE_INSERT1(hio, name, ncomp) do { \
169 mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
170 _wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]); \
171 TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \
172 hio_next[(ncomp)]); \
173 mtx_unlock(&hio_##name##_list_lock[ncomp]); \
175 cv_signal(&hio_##name##_list_cond[(ncomp)]); \
177 #define QUEUE_INSERT2(hio, name) do { \
180 mtx_lock(&hio_##name##_list_lock); \
181 _wakeup = TAILQ_EMPTY(&hio_##name##_list); \
182 TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\
183 mtx_unlock(&hio_##name##_list_lock); \
185 cv_signal(&hio_##name##_list_cond); \
187 #define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \
190 mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
192 while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \
193 cv_timedwait(&hio_##name##_list_cond[(ncomp)], \
194 &hio_##name##_list_lock[(ncomp)], (timeout)); \
195 if ((timeout) != 0) \
199 TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \
200 hio_next[(ncomp)]); \
202 mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \
204 #define QUEUE_TAKE2(hio, name) do { \
205 mtx_lock(&hio_##name##_list_lock); \
206 while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \
207 cv_wait(&hio_##name##_list_cond, \
208 &hio_##name##_list_lock); \
210 TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \
211 mtx_unlock(&hio_##name##_list_lock); \
214 #define SYNCREQ(hio) do { \
215 (hio)->hio_ggio.gctl_unit = -1; \
216 (hio)->hio_ggio.gctl_seq = 1; \
218 #define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1)
219 #define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0)
220 #define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2)
222 static struct hast_resource *gres;
224 static pthread_mutex_t range_lock;
225 static struct rangelocks *range_regular;
226 static bool range_regular_wait;
227 static pthread_cond_t range_regular_cond;
228 static struct rangelocks *range_sync;
229 static bool range_sync_wait;
230 static pthread_cond_t range_sync_cond;
231 static bool fullystarted;
233 static void *ggate_recv_thread(void *arg);
234 static void *local_send_thread(void *arg);
235 static void *remote_send_thread(void *arg);
236 static void *remote_recv_thread(void *arg);
237 static void *ggate_send_thread(void *arg);
238 static void *sync_thread(void *arg);
239 static void *guard_thread(void *arg);
242 cleanup(struct hast_resource *res)
246 /* Remember errno. */
249 /* Destroy ggate provider if we created one. */
250 if (res->hr_ggateunit >= 0) {
251 struct g_gate_ctl_destroy ggiod;
253 bzero(&ggiod, sizeof(ggiod));
254 ggiod.gctl_version = G_GATE_VERSION;
255 ggiod.gctl_unit = res->hr_ggateunit;
256 ggiod.gctl_force = 1;
257 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) == -1) {
258 pjdlog_errno(LOG_WARNING,
259 "Unable to destroy hast/%s device",
262 res->hr_ggateunit = -1;
270 primary_exit(int exitcode, const char *fmt, ...)
274 PJDLOG_ASSERT(exitcode != EX_OK);
276 pjdlogv_errno(LOG_ERR, fmt, ap);
283 primary_exitx(int exitcode, const char *fmt, ...)
288 pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap);
295 hast_activemap_flush(struct hast_resource *res)
297 const unsigned char *buf;
300 buf = activemap_bitmap(res->hr_amp, &size);
301 PJDLOG_ASSERT(buf != NULL);
302 PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
303 if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
305 pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk");
308 if (res->hr_metaflush == 1 && g_flush(res->hr_localfd) == -1) {
309 if (errno == EOPNOTSUPP) {
310 pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.",
312 res->hr_metaflush = 0;
314 pjdlog_errno(LOG_ERR,
315 "Unable to flush disk cache on activemap update");
323 real_remote(const struct hast_resource *res)
326 return (strcmp(res->hr_remoteaddr, "none") != 0);
330 init_environment(struct hast_resource *res __unused)
333 unsigned int ii, ncomps;
336 * In the future it might be per-resource value.
338 ncomps = HAST_NCOMPONENTS;
341 * Allocate memory needed by lists.
343 hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
344 if (hio_send_list == NULL) {
345 primary_exitx(EX_TEMPFAIL,
346 "Unable to allocate %zu bytes of memory for send lists.",
347 sizeof(hio_send_list[0]) * ncomps);
349 hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
350 if (hio_send_list_lock == NULL) {
351 primary_exitx(EX_TEMPFAIL,
352 "Unable to allocate %zu bytes of memory for send list locks.",
353 sizeof(hio_send_list_lock[0]) * ncomps);
355 hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
356 if (hio_send_list_cond == NULL) {
357 primary_exitx(EX_TEMPFAIL,
358 "Unable to allocate %zu bytes of memory for send list condition variables.",
359 sizeof(hio_send_list_cond[0]) * ncomps);
361 hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
362 if (hio_recv_list == NULL) {
363 primary_exitx(EX_TEMPFAIL,
364 "Unable to allocate %zu bytes of memory for recv lists.",
365 sizeof(hio_recv_list[0]) * ncomps);
367 hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
368 if (hio_recv_list_lock == NULL) {
369 primary_exitx(EX_TEMPFAIL,
370 "Unable to allocate %zu bytes of memory for recv list locks.",
371 sizeof(hio_recv_list_lock[0]) * ncomps);
373 hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
374 if (hio_recv_list_cond == NULL) {
375 primary_exitx(EX_TEMPFAIL,
376 "Unable to allocate %zu bytes of memory for recv list condition variables.",
377 sizeof(hio_recv_list_cond[0]) * ncomps);
379 hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
380 if (hio_remote_lock == NULL) {
381 primary_exitx(EX_TEMPFAIL,
382 "Unable to allocate %zu bytes of memory for remote connections locks.",
383 sizeof(hio_remote_lock[0]) * ncomps);
387 * Initialize lists, their locks and theirs condition variables.
389 TAILQ_INIT(&hio_free_list);
390 mtx_init(&hio_free_list_lock);
391 cv_init(&hio_free_list_cond);
392 for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
393 TAILQ_INIT(&hio_send_list[ii]);
394 mtx_init(&hio_send_list_lock[ii]);
395 cv_init(&hio_send_list_cond[ii]);
396 TAILQ_INIT(&hio_recv_list[ii]);
397 mtx_init(&hio_recv_list_lock[ii]);
398 cv_init(&hio_recv_list_cond[ii]);
399 rw_init(&hio_remote_lock[ii]);
401 TAILQ_INIT(&hio_done_list);
402 mtx_init(&hio_done_list_lock);
403 cv_init(&hio_done_list_cond);
404 mtx_init(&metadata_lock);
407 * Allocate requests pool and initialize requests.
409 for (ii = 0; ii < HAST_HIO_MAX; ii++) {
410 hio = malloc(sizeof(*hio));
412 primary_exitx(EX_TEMPFAIL,
413 "Unable to allocate %zu bytes of memory for hio request.",
416 hio->hio_countdown = 0;
417 hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
418 if (hio->hio_errors == NULL) {
419 primary_exitx(EX_TEMPFAIL,
420 "Unable allocate %zu bytes of memory for hio errors.",
421 sizeof(hio->hio_errors[0]) * ncomps);
423 hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
424 if (hio->hio_next == NULL) {
425 primary_exitx(EX_TEMPFAIL,
426 "Unable allocate %zu bytes of memory for hio_next field.",
427 sizeof(hio->hio_next[0]) * ncomps);
429 hio->hio_ggio.gctl_version = G_GATE_VERSION;
430 hio->hio_ggio.gctl_data = malloc(MAXPHYS);
431 if (hio->hio_ggio.gctl_data == NULL) {
432 primary_exitx(EX_TEMPFAIL,
433 "Unable to allocate %zu bytes of memory for gctl_data.",
436 hio->hio_ggio.gctl_length = MAXPHYS;
437 hio->hio_ggio.gctl_error = 0;
438 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
443 init_resuid(struct hast_resource *res)
446 mtx_lock(&metadata_lock);
447 if (res->hr_resuid != 0) {
448 mtx_unlock(&metadata_lock);
451 /* Initialize unique resource identifier. */
452 arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
453 mtx_unlock(&metadata_lock);
454 if (metadata_write(res) == -1)
461 init_local(struct hast_resource *res)
466 if (metadata_read(res, true) == -1)
468 mtx_init(&res->hr_amp_lock);
469 if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
470 res->hr_local_sectorsize, res->hr_keepdirty) == -1) {
471 primary_exit(EX_TEMPFAIL, "Unable to create activemap");
473 mtx_init(&range_lock);
474 cv_init(&range_regular_cond);
475 if (rangelock_init(&range_regular) == -1)
476 primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
477 cv_init(&range_sync_cond);
478 if (rangelock_init(&range_sync) == -1)
479 primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
480 mapsize = activemap_ondisk_size(res->hr_amp);
481 buf = calloc(1, mapsize);
483 primary_exitx(EX_TEMPFAIL,
484 "Unable to allocate buffer for activemap.");
486 if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
488 primary_exit(EX_NOINPUT, "Unable to read activemap");
490 activemap_copyin(res->hr_amp, buf, mapsize);
492 if (res->hr_resuid != 0)
495 * We're using provider for the first time. Initialize local and remote
496 * counters. We don't initialize resuid here, as we want to do it just
497 * in time. The reason for this is that we want to inform secondary
498 * that there were no writes yet, so there is no need to synchronize
501 res->hr_primary_localcnt = 0;
502 res->hr_primary_remotecnt = 0;
503 if (metadata_write(res) == -1)
508 primary_connect(struct hast_resource *res, struct proto_conn **connp)
510 struct proto_conn *conn;
514 if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
515 primary_exit(EX_TEMPFAIL,
516 "Unable to send connection request to parent");
518 if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
519 primary_exit(EX_TEMPFAIL,
520 "Unable to receive reply to connection request from parent");
524 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
528 if (proto_connection_recv(res->hr_conn, true, &conn) == -1) {
529 primary_exit(EX_TEMPFAIL,
530 "Unable to receive connection from parent");
532 if (proto_connect_wait(conn, res->hr_timeout) == -1) {
533 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
538 /* Error in setting timeout is not critical, but why should it fail? */
539 if (proto_timeout(conn, res->hr_timeout) == -1)
540 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
548 * Function instructs GEOM_GATE to handle reads directly from within the kernel.
551 enable_direct_reads(struct hast_resource *res)
553 struct g_gate_ctl_modify ggiomodify;
555 bzero(&ggiomodify, sizeof(ggiomodify));
556 ggiomodify.gctl_version = G_GATE_VERSION;
557 ggiomodify.gctl_unit = res->hr_ggateunit;
558 ggiomodify.gctl_modify = GG_MODIFY_READPROV | GG_MODIFY_READOFFSET;
559 strlcpy(ggiomodify.gctl_readprov, res->hr_localpath,
560 sizeof(ggiomodify.gctl_readprov));
561 ggiomodify.gctl_readoffset = res->hr_localoff;
562 if (ioctl(res->hr_ggatefd, G_GATE_CMD_MODIFY, &ggiomodify) == 0)
563 pjdlog_debug(1, "Direct reads enabled.");
565 pjdlog_errno(LOG_WARNING, "Failed to enable direct reads");
569 init_remote(struct hast_resource *res, struct proto_conn **inp,
570 struct proto_conn **outp)
572 struct proto_conn *in, *out;
573 struct nv *nvout, *nvin;
574 const unsigned char *token;
584 PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
585 PJDLOG_ASSERT(real_remote(res));
590 if (primary_connect(res, &out) == -1)
591 return (ECONNREFUSED);
593 error = ECONNABORTED;
596 * First handshake step.
597 * Setup outgoing connection with remote node.
600 nv_add_string(nvout, res->hr_name, "resource");
601 nv_add_uint8(nvout, HAST_PROTO_VERSION, "version");
602 if (nv_error(nvout) != 0) {
603 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
604 "Unable to allocate header for connection with %s",
609 if (hast_proto_send(res, out, nvout, NULL, 0) == -1) {
610 pjdlog_errno(LOG_WARNING,
611 "Unable to send handshake header to %s",
617 if (hast_proto_recv_hdr(out, &nvin) == -1) {
618 pjdlog_errno(LOG_WARNING,
619 "Unable to receive handshake header from %s",
623 errmsg = nv_get_string(nvin, "errmsg");
624 if (errmsg != NULL) {
625 pjdlog_warning("%s", errmsg);
626 if (nv_exists(nvin, "wait"))
631 version = nv_get_uint8(nvin, "version");
634 * If no version is sent, it means this is protocol version 1.
638 if (version > HAST_PROTO_VERSION) {
639 pjdlog_warning("Invalid version received (%hhu).", version);
643 res->hr_version = version;
644 pjdlog_debug(1, "Negotiated protocol version %d.", res->hr_version);
645 token = nv_get_uint8_array(nvin, &size, "token");
647 pjdlog_warning("Handshake header from %s has no 'token' field.",
652 if (size != sizeof(res->hr_token)) {
653 pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
654 res->hr_remoteaddr, size, sizeof(res->hr_token));
658 bcopy(token, res->hr_token, sizeof(res->hr_token));
662 * Second handshake step.
663 * Setup incoming connection with remote node.
665 if (primary_connect(res, &in) == -1)
669 nv_add_string(nvout, res->hr_name, "resource");
670 nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
672 if (res->hr_resuid == 0) {
674 * The resuid field was not yet initialized.
675 * Because we do synchronization inside init_resuid(), it is
676 * possible that someone already initialized it, the function
677 * will return false then, but if we successfully initialized
678 * it, we will get true. True means that there were no writes
679 * to this resource yet and we want to inform secondary that
680 * synchronization is not needed by sending "virgin" argument.
682 if (init_resuid(res))
683 nv_add_int8(nvout, 1, "virgin");
685 nv_add_uint64(nvout, res->hr_resuid, "resuid");
686 nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
687 nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
688 if (nv_error(nvout) != 0) {
689 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
690 "Unable to allocate header for connection with %s",
695 if (hast_proto_send(res, in, nvout, NULL, 0) == -1) {
696 pjdlog_errno(LOG_WARNING,
697 "Unable to send handshake header to %s",
703 if (hast_proto_recv_hdr(out, &nvin) == -1) {
704 pjdlog_errno(LOG_WARNING,
705 "Unable to receive handshake header from %s",
709 errmsg = nv_get_string(nvin, "errmsg");
710 if (errmsg != NULL) {
711 pjdlog_warning("%s", errmsg);
715 datasize = nv_get_int64(nvin, "datasize");
716 if (datasize != res->hr_datasize) {
717 pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
718 (intmax_t)res->hr_datasize, (intmax_t)datasize);
722 extentsize = nv_get_int32(nvin, "extentsize");
723 if (extentsize != res->hr_extentsize) {
724 pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
725 (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
729 res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
730 res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
731 res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
732 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY)
733 enable_direct_reads(res);
734 if (nv_exists(nvin, "virgin")) {
736 * Secondary was reinitialized, bump localcnt if it is 0 as
737 * only we have the data.
739 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
740 PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
742 if (res->hr_primary_localcnt == 0) {
743 PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
745 mtx_lock(&metadata_lock);
746 res->hr_primary_localcnt++;
747 pjdlog_debug(1, "Increasing localcnt to %ju.",
748 (uintmax_t)res->hr_primary_localcnt);
749 (void)metadata_write(res);
750 mtx_unlock(&metadata_lock);
754 mapsize = nv_get_uint32(nvin, "mapsize");
756 map = malloc(mapsize);
758 pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
764 * Remote node have some dirty extents on its own, lets
765 * download its activemap.
767 if (hast_proto_recv_data(res, out, nvin, map,
769 pjdlog_errno(LOG_ERR,
770 "Unable to receive remote activemap");
776 * Merge local and remote bitmaps.
778 activemap_merge(res->hr_amp, map, mapsize);
781 * Now that we merged bitmaps from both nodes, flush it to the
782 * disk before we start to synchronize.
784 (void)hast_activemap_flush(res);
788 /* Setup directions. */
789 if (proto_send(out, NULL, 0) == -1)
790 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
791 if (proto_recv(in, NULL, 0) == -1)
792 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
794 pjdlog_info("Connected to %s.", res->hr_remoteaddr);
795 if (res->hr_original_replication == HAST_REPLICATION_MEMSYNC &&
796 res->hr_version < 2) {
797 pjdlog_warning("The 'memsync' replication mode is not supported by the remote node, falling back to 'fullsync' mode.");
798 res->hr_replication = HAST_REPLICATION_FULLSYNC;
799 } else if (res->hr_replication != res->hr_original_replication) {
801 * This is in case hastd disconnected and was upgraded.
803 res->hr_replication = res->hr_original_replication;
805 if (inp != NULL && outp != NULL) {
809 res->hr_remotein = in;
810 res->hr_remoteout = out;
812 event_send(res, EVENT_CONNECT);
815 if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
816 event_send(res, EVENT_SPLITBRAIN);
827 mtx_lock(&sync_lock);
828 sync_inprogress = true;
829 mtx_unlock(&sync_lock);
830 cv_signal(&sync_cond);
837 mtx_lock(&sync_lock);
839 sync_inprogress = false;
840 mtx_unlock(&sync_lock);
844 init_ggate(struct hast_resource *res)
846 struct g_gate_ctl_create ggiocreate;
847 struct g_gate_ctl_cancel ggiocancel;
850 * We communicate with ggate via /dev/ggctl. Open it.
852 res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
853 if (res->hr_ggatefd == -1)
854 primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
856 * Create provider before trying to connect, as connection failure
857 * is not critical, but may take some time.
859 bzero(&ggiocreate, sizeof(ggiocreate));
860 ggiocreate.gctl_version = G_GATE_VERSION;
861 ggiocreate.gctl_mediasize = res->hr_datasize;
862 ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
863 ggiocreate.gctl_flags = 0;
864 ggiocreate.gctl_maxcount = 0;
865 ggiocreate.gctl_timeout = 0;
866 ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
867 snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
869 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
870 pjdlog_info("Device hast/%s created.", res->hr_provname);
871 res->hr_ggateunit = ggiocreate.gctl_unit;
874 if (errno != EEXIST) {
875 primary_exit(EX_OSERR, "Unable to create hast/%s device",
879 "Device hast/%s already exists, we will try to take it over.",
882 * If we received EEXIST, we assume that the process who created the
883 * provider died and didn't clean up. In that case we will start from
886 bzero(&ggiocancel, sizeof(ggiocancel));
887 ggiocancel.gctl_version = G_GATE_VERSION;
888 ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
889 snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
891 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
892 pjdlog_info("Device hast/%s recovered.", res->hr_provname);
893 res->hr_ggateunit = ggiocancel.gctl_unit;
896 primary_exit(EX_OSERR, "Unable to take over hast/%s device",
901 hastd_primary(struct hast_resource *res)
905 int error, mode, debuglevel;
908 * Create communication channel for sending control commands from
911 if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
912 /* TODO: There's no need for this to be fatal error. */
913 KEEP_ERRNO((void)pidfile_remove(pfh));
914 pjdlog_exit(EX_OSERR,
915 "Unable to create control sockets between parent and child");
918 * Create communication channel for sending events from child to parent.
920 if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
921 /* TODO: There's no need for this to be fatal error. */
922 KEEP_ERRNO((void)pidfile_remove(pfh));
923 pjdlog_exit(EX_OSERR,
924 "Unable to create event sockets between child and parent");
927 * Create communication channel for sending connection requests from
930 if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) {
931 /* TODO: There's no need for this to be fatal error. */
932 KEEP_ERRNO((void)pidfile_remove(pfh));
933 pjdlog_exit(EX_OSERR,
934 "Unable to create connection sockets between child and parent");
939 /* TODO: There's no need for this to be fatal error. */
940 KEEP_ERRNO((void)pidfile_remove(pfh));
941 pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
945 /* This is parent. */
946 /* Declare that we are receiver. */
947 proto_recv(res->hr_event, NULL, 0);
948 proto_recv(res->hr_conn, NULL, 0);
949 /* Declare that we are sender. */
950 proto_send(res->hr_ctrl, NULL, 0);
951 res->hr_workerpid = pid;
956 mode = pjdlog_mode_get();
957 debuglevel = pjdlog_debug_get();
959 /* Declare that we are sender. */
960 proto_send(res->hr_event, NULL, 0);
961 proto_send(res->hr_conn, NULL, 0);
962 /* Declare that we are receiver. */
963 proto_recv(res->hr_ctrl, NULL, 0);
964 descriptors_cleanup(res);
966 descriptors_assert(res, mode);
969 pjdlog_debug_set(debuglevel);
970 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
971 setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
975 init_environment(res);
977 if (drop_privs(res) != 0) {
981 pjdlog_info("Privileges successfully dropped.");
984 * Create the guard thread first, so we can handle signals from the
987 error = pthread_create(&td, NULL, guard_thread, res);
988 PJDLOG_ASSERT(error == 0);
990 * Create the control thread before sending any event to the parent,
991 * as we can deadlock when parent sends control request to worker,
992 * but worker has no control thread started yet, so parent waits.
993 * In the meantime worker sends an event to the parent, but parent
994 * is unable to handle the event, because it waits for control
997 error = pthread_create(&td, NULL, ctrl_thread, res);
998 PJDLOG_ASSERT(error == 0);
999 if (real_remote(res)) {
1000 error = init_remote(res, NULL, NULL);
1003 } else if (error == EBUSY) {
1004 time_t start = time(NULL);
1006 pjdlog_warning("Waiting for remote node to become %s for %ds.",
1007 role2str(HAST_ROLE_SECONDARY),
1011 error = init_remote(res, NULL, NULL);
1014 if (time(NULL) > start + res->hr_timeout)
1017 if (error == EBUSY) {
1018 pjdlog_warning("Remote node is still %s, starting anyway.",
1019 role2str(HAST_ROLE_PRIMARY));
1023 error = pthread_create(&td, NULL, ggate_recv_thread, res);
1024 PJDLOG_ASSERT(error == 0);
1025 error = pthread_create(&td, NULL, local_send_thread, res);
1026 PJDLOG_ASSERT(error == 0);
1027 error = pthread_create(&td, NULL, remote_send_thread, res);
1028 PJDLOG_ASSERT(error == 0);
1029 error = pthread_create(&td, NULL, remote_recv_thread, res);
1030 PJDLOG_ASSERT(error == 0);
1031 error = pthread_create(&td, NULL, ggate_send_thread, res);
1032 PJDLOG_ASSERT(error == 0);
1033 fullystarted = true;
1034 (void)sync_thread(res);
1038 reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio,
1039 const char *fmt, ...)
1045 (void)vsnprintf(msg, sizeof(msg), fmt, ap);
1047 switch (ggio->gctl_cmd) {
1049 (void)snprlcat(msg, sizeof(msg), "READ(%ju, %ju).",
1050 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1053 (void)snprlcat(msg, sizeof(msg), "DELETE(%ju, %ju).",
1054 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1057 (void)snprlcat(msg, sizeof(msg), "FLUSH.");
1060 (void)snprlcat(msg, sizeof(msg), "WRITE(%ju, %ju).",
1061 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1064 (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%u).",
1065 (unsigned int)ggio->gctl_cmd);
1068 pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
1072 remote_close(struct hast_resource *res, int ncomp)
1075 rw_wlock(&hio_remote_lock[ncomp]);
1077 * Check for a race between dropping rlock and acquiring wlock -
1078 * another thread can close connection in-between.
1080 if (!ISCONNECTED(res, ncomp)) {
1081 PJDLOG_ASSERT(res->hr_remotein == NULL);
1082 PJDLOG_ASSERT(res->hr_remoteout == NULL);
1083 rw_unlock(&hio_remote_lock[ncomp]);
1087 PJDLOG_ASSERT(res->hr_remotein != NULL);
1088 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1090 pjdlog_debug(2, "Closing incoming connection to %s.",
1091 res->hr_remoteaddr);
1092 proto_close(res->hr_remotein);
1093 res->hr_remotein = NULL;
1094 pjdlog_debug(2, "Closing outgoing connection to %s.",
1095 res->hr_remoteaddr);
1096 proto_close(res->hr_remoteout);
1097 res->hr_remoteout = NULL;
1099 rw_unlock(&hio_remote_lock[ncomp]);
1101 pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
1104 * Stop synchronization if in-progress.
1108 event_send(res, EVENT_DISCONNECT);
1112 * Acknowledge write completion to the kernel, but don't update activemap yet.
1115 write_complete(struct hast_resource *res, struct hio *hio)
1117 struct g_gate_ctl_io *ggio;
1120 PJDLOG_ASSERT(!hio->hio_done);
1122 ggio = &hio->hio_ggio;
1123 PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE);
1126 * Bump local count if this is first write after
1127 * connection failure with remote node.
1130 rw_rlock(&hio_remote_lock[ncomp]);
1131 if (!ISCONNECTED(res, ncomp)) {
1132 mtx_lock(&metadata_lock);
1133 if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) {
1134 res->hr_primary_localcnt++;
1135 pjdlog_debug(1, "Increasing localcnt to %ju.",
1136 (uintmax_t)res->hr_primary_localcnt);
1137 (void)metadata_write(res);
1139 mtx_unlock(&metadata_lock);
1141 rw_unlock(&hio_remote_lock[ncomp]);
1142 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1)
1143 primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
1144 hio->hio_done = true;
1148 * Thread receives ggate I/O requests from the kernel and passes them to
1149 * appropriate threads:
1150 * WRITE - always goes to both local_send and remote_send threads
1151 * READ (when the block is up-to-date on local component) -
1152 * only local_send thread
1153 * READ (when the block isn't up-to-date on local component) -
1154 * only remote_send thread
1155 * DELETE - always goes to both local_send and remote_send threads
1156 * FLUSH - always goes to both local_send and remote_send threads
1159 ggate_recv_thread(void *arg)
1161 struct hast_resource *res = arg;
1162 struct g_gate_ctl_io *ggio;
1164 unsigned int ii, ncomp, ncomps;
1168 pjdlog_debug(2, "ggate_recv: Taking free request.");
1169 QUEUE_TAKE2(hio, free);
1170 pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
1171 ggio = &hio->hio_ggio;
1172 ggio->gctl_unit = res->hr_ggateunit;
1173 ggio->gctl_length = MAXPHYS;
1174 ggio->gctl_error = 0;
1175 hio->hio_done = false;
1176 hio->hio_replication = res->hr_replication;
1178 "ggate_recv: (%p) Waiting for request from the kernel.",
1180 if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) {
1181 if (sigexit_received)
1183 primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
1185 error = ggio->gctl_error;
1190 /* Exit gracefully. */
1191 if (!sigexit_received) {
1193 "ggate_recv: (%p) Received cancel from the kernel.",
1195 pjdlog_info("Received cancel from the kernel, exiting.");
1200 * Buffer too small? Impossible, we allocate MAXPHYS
1201 * bytes - request can't be bigger than that.
1206 primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
1211 ncomps = HAST_NCOMPONENTS;
1213 for (ii = 0; ii < ncomps; ii++)
1214 hio->hio_errors[ii] = EINVAL;
1215 reqlog(LOG_DEBUG, 2, ggio,
1216 "ggate_recv: (%p) Request received from the kernel: ",
1220 * Inform all components about new write request.
1221 * For read request prefer local component unless the given
1222 * range is out-of-date, then use remote component.
1224 switch (ggio->gctl_cmd) {
1226 res->hr_stat_read++;
1228 mtx_lock(&metadata_lock);
1229 if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
1230 res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
1232 * This range is up-to-date on local component,
1233 * so handle request locally.
1235 /* Local component is 0 for now. */
1237 } else /* if (res->hr_syncsrc ==
1238 HAST_SYNCSRC_SECONDARY) */ {
1239 PJDLOG_ASSERT(res->hr_syncsrc ==
1240 HAST_SYNCSRC_SECONDARY);
1242 * This range is out-of-date on local component,
1243 * so send request to the remote node.
1245 /* Remote component is 1 for now. */
1248 mtx_unlock(&metadata_lock);
1251 res->hr_stat_write++;
1252 if (res->hr_resuid == 0 &&
1253 res->hr_primary_localcnt == 0) {
1254 /* This is first write. */
1255 res->hr_primary_localcnt = 1;
1258 mtx_lock(&range_lock);
1259 if (rangelock_islocked(range_sync,
1260 ggio->gctl_offset, ggio->gctl_length)) {
1262 "regular: Range offset=%jd length=%zu locked.",
1263 (intmax_t)ggio->gctl_offset,
1264 (size_t)ggio->gctl_length);
1265 range_regular_wait = true;
1266 cv_wait(&range_regular_cond, &range_lock);
1267 range_regular_wait = false;
1268 mtx_unlock(&range_lock);
1271 if (rangelock_add(range_regular,
1272 ggio->gctl_offset, ggio->gctl_length) == -1) {
1273 mtx_unlock(&range_lock);
1275 "regular: Range offset=%jd length=%zu is already locked, waiting.",
1276 (intmax_t)ggio->gctl_offset,
1277 (size_t)ggio->gctl_length);
1281 mtx_unlock(&range_lock);
1284 mtx_lock(&res->hr_amp_lock);
1285 if (activemap_write_start(res->hr_amp,
1286 ggio->gctl_offset, ggio->gctl_length)) {
1287 res->hr_stat_activemap_update++;
1288 (void)hast_activemap_flush(res);
1290 mtx_unlock(&res->hr_amp_lock);
1293 res->hr_stat_delete++;
1296 res->hr_stat_flush++;
1300 "ggate_recv: (%p) Moving request to the send queues.", hio);
1301 hio->hio_countdown = ncomps;
1302 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC &&
1303 ggio->gctl_cmd == BIO_WRITE) {
1304 /* Each remote request needs two responses in memsync. */
1305 hio->hio_countdown++;
1307 for (ii = ncomp; ii < ncomps; ii++)
1308 QUEUE_INSERT1(hio, send, ii);
1315 * Thread reads from or writes to local component.
1316 * If local read fails, it redirects it to remote_send thread.
1319 local_send_thread(void *arg)
1321 struct hast_resource *res = arg;
1322 struct g_gate_ctl_io *ggio;
1324 unsigned int ncomp, rncomp;
1327 /* Local component is 0 for now. */
1329 /* Remote component is 1 for now. */
1333 pjdlog_debug(2, "local_send: Taking request.");
1334 QUEUE_TAKE1(hio, send, ncomp, 0);
1335 pjdlog_debug(2, "local_send: (%p) Got request.", hio);
1336 ggio = &hio->hio_ggio;
1337 switch (ggio->gctl_cmd) {
1339 ret = pread(res->hr_localfd, ggio->gctl_data,
1341 ggio->gctl_offset + res->hr_localoff);
1342 if (ret == ggio->gctl_length)
1343 hio->hio_errors[ncomp] = 0;
1344 else if (!ISSYNCREQ(hio)) {
1346 * If READ failed, try to read from remote node.
1349 reqlog(LOG_WARNING, 0, ggio,
1350 "Local request failed (%s), trying remote node. ",
1352 } else if (ret != ggio->gctl_length) {
1353 reqlog(LOG_WARNING, 0, ggio,
1354 "Local request failed (%zd != %jd), trying remote node. ",
1355 ret, (intmax_t)ggio->gctl_length);
1357 QUEUE_INSERT1(hio, send, rncomp);
1362 ret = pwrite(res->hr_localfd, ggio->gctl_data,
1364 ggio->gctl_offset + res->hr_localoff);
1366 hio->hio_errors[ncomp] = errno;
1367 reqlog(LOG_WARNING, 0, ggio,
1368 "Local request failed (%s): ",
1370 } else if (ret != ggio->gctl_length) {
1371 hio->hio_errors[ncomp] = EIO;
1372 reqlog(LOG_WARNING, 0, ggio,
1373 "Local request failed (%zd != %jd): ",
1374 ret, (intmax_t)ggio->gctl_length);
1376 hio->hio_errors[ncomp] = 0;
1377 if (hio->hio_replication ==
1378 HAST_REPLICATION_ASYNC) {
1379 ggio->gctl_error = 0;
1380 write_complete(res, hio);
1385 ret = g_delete(res->hr_localfd,
1386 ggio->gctl_offset + res->hr_localoff,
1389 hio->hio_errors[ncomp] = errno;
1390 reqlog(LOG_WARNING, 0, ggio,
1391 "Local request failed (%s): ",
1394 hio->hio_errors[ncomp] = 0;
1398 if (!res->hr_localflush) {
1403 ret = g_flush(res->hr_localfd);
1405 if (errno == EOPNOTSUPP)
1406 res->hr_localflush = false;
1407 hio->hio_errors[ncomp] = errno;
1408 reqlog(LOG_WARNING, 0, ggio,
1409 "Local request failed (%s): ",
1412 hio->hio_errors[ncomp] = 0;
1417 if (hio->hio_replication != HAST_REPLICATION_MEMSYNC ||
1418 ggio->gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) {
1419 if (refcnt_release(&hio->hio_countdown) > 0)
1423 * Depending on hio_countdown value, requests finished
1424 * in the following order:
1425 * 0: remote memsync, remote final, local write
1426 * 1: remote memsync, local write, (remote final)
1427 * 2: local write, (remote memsync), (remote final)
1429 switch (refcnt_release(&hio->hio_countdown)) {
1432 * Local write finished as last.
1437 * Local write finished after remote memsync
1438 * reply arrvied. We can complete the write now.
1440 if (hio->hio_errors[0] == 0)
1441 write_complete(res, hio);
1445 * Local write finished as first.
1449 PJDLOG_ABORT("Invalid hio_countdown.");
1452 if (ISSYNCREQ(hio)) {
1453 mtx_lock(&sync_lock);
1455 mtx_unlock(&sync_lock);
1456 cv_signal(&sync_cond);
1459 "local_send: (%p) Moving request to the done queue.",
1461 QUEUE_INSERT2(hio, done);
1469 keepalive_send(struct hast_resource *res, unsigned int ncomp)
1473 rw_rlock(&hio_remote_lock[ncomp]);
1475 if (!ISCONNECTED(res, ncomp)) {
1476 rw_unlock(&hio_remote_lock[ncomp]);
1480 PJDLOG_ASSERT(res->hr_remotein != NULL);
1481 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1484 nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1485 if (nv_error(nv) != 0) {
1486 rw_unlock(&hio_remote_lock[ncomp]);
1489 "keepalive_send: Unable to prepare header to send.");
1492 if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
1493 rw_unlock(&hio_remote_lock[ncomp]);
1494 pjdlog_common(LOG_DEBUG, 1, errno,
1495 "keepalive_send: Unable to send request");
1497 remote_close(res, ncomp);
1501 rw_unlock(&hio_remote_lock[ncomp]);
1503 pjdlog_debug(2, "keepalive_send: Request sent.");
1507 * Thread sends request to secondary node.
1510 remote_send_thread(void *arg)
1512 struct hast_resource *res = arg;
1513 struct g_gate_ctl_io *ggio;
1514 time_t lastcheck, now;
1519 uint64_t offset, length;
1523 /* Remote component is 1 for now. */
1525 lastcheck = time(NULL);
1528 pjdlog_debug(2, "remote_send: Taking request.");
1529 QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1532 if (lastcheck + HAST_KEEPALIVE <= now) {
1533 keepalive_send(res, ncomp);
1538 pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
1539 ggio = &hio->hio_ggio;
1540 switch (ggio->gctl_cmd) {
1544 offset = ggio->gctl_offset;
1545 length = ggio->gctl_length;
1549 data = ggio->gctl_data;
1550 offset = ggio->gctl_offset;
1551 length = ggio->gctl_length;
1556 offset = ggio->gctl_offset;
1557 length = ggio->gctl_length;
1566 PJDLOG_ABORT("invalid condition");
1569 nv_add_uint8(nv, cmd, "cmd");
1570 nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
1571 nv_add_uint64(nv, offset, "offset");
1572 nv_add_uint64(nv, length, "length");
1573 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC &&
1574 ggio->gctl_cmd == BIO_WRITE && !ISSYNCREQ(hio)) {
1575 nv_add_uint8(nv, 1, "memsync");
1577 if (nv_error(nv) != 0) {
1578 hio->hio_errors[ncomp] = nv_error(nv);
1580 "remote_send: (%p) Unable to prepare header to send.",
1582 reqlog(LOG_ERR, 0, ggio,
1583 "Unable to prepare header to send (%s): ",
1584 strerror(nv_error(nv)));
1585 /* Move failed request immediately to the done queue. */
1589 * Protect connection from disappearing.
1591 rw_rlock(&hio_remote_lock[ncomp]);
1592 if (!ISCONNECTED(res, ncomp)) {
1593 rw_unlock(&hio_remote_lock[ncomp]);
1594 hio->hio_errors[ncomp] = ENOTCONN;
1598 * Move the request to recv queue before sending it, because
1599 * in different order we can get reply before we move request
1603 "remote_send: (%p) Moving request to the recv queue.",
1605 mtx_lock(&hio_recv_list_lock[ncomp]);
1606 wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
1607 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
1608 mtx_unlock(&hio_recv_list_lock[ncomp]);
1609 if (hast_proto_send(res, res->hr_remoteout, nv, data,
1610 data != NULL ? length : 0) == -1) {
1611 hio->hio_errors[ncomp] = errno;
1612 rw_unlock(&hio_remote_lock[ncomp]);
1614 "remote_send: (%p) Unable to send request.", hio);
1615 reqlog(LOG_ERR, 0, ggio,
1616 "Unable to send request (%s): ",
1617 strerror(hio->hio_errors[ncomp]));
1618 remote_close(res, ncomp);
1620 * Take request back from the receive queue and move
1621 * it immediately to the done queue.
1623 mtx_lock(&hio_recv_list_lock[ncomp]);
1624 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1626 mtx_unlock(&hio_recv_list_lock[ncomp]);
1629 rw_unlock(&hio_remote_lock[ncomp]);
1632 cv_signal(&hio_recv_list_cond[ncomp]);
1636 if (ISSYNCREQ(hio)) {
1637 if (refcnt_release(&hio->hio_countdown) > 0)
1639 mtx_lock(&sync_lock);
1641 mtx_unlock(&sync_lock);
1642 cv_signal(&sync_cond);
1645 if (ggio->gctl_cmd == BIO_WRITE) {
1646 mtx_lock(&res->hr_amp_lock);
1647 if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
1648 ggio->gctl_length)) {
1649 (void)hast_activemap_flush(res);
1651 mtx_unlock(&res->hr_amp_lock);
1652 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC)
1653 (void)refcnt_release(&hio->hio_countdown);
1655 if (refcnt_release(&hio->hio_countdown) > 0)
1658 "remote_send: (%p) Moving request to the done queue.",
1660 QUEUE_INSERT2(hio, done);
1667 * Thread receives answer from secondary node and passes it to ggate_send
1671 remote_recv_thread(void *arg)
1673 struct hast_resource *res = arg;
1674 struct g_gate_ctl_io *ggio;
1682 /* Remote component is 1 for now. */
1686 /* Wait until there is anything to receive. */
1687 mtx_lock(&hio_recv_list_lock[ncomp]);
1688 while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
1689 pjdlog_debug(2, "remote_recv: No requests, waiting.");
1690 cv_wait(&hio_recv_list_cond[ncomp],
1691 &hio_recv_list_lock[ncomp]);
1693 mtx_unlock(&hio_recv_list_lock[ncomp]);
1697 rw_rlock(&hio_remote_lock[ncomp]);
1698 if (!ISCONNECTED(res, ncomp)) {
1699 rw_unlock(&hio_remote_lock[ncomp]);
1701 * Connection is dead, so move all pending requests to
1702 * the done queue (one-by-one).
1704 mtx_lock(&hio_recv_list_lock[ncomp]);
1705 hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
1706 PJDLOG_ASSERT(hio != NULL);
1707 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1709 mtx_unlock(&hio_recv_list_lock[ncomp]);
1712 if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
1713 pjdlog_errno(LOG_ERR,
1714 "Unable to receive reply header");
1715 rw_unlock(&hio_remote_lock[ncomp]);
1716 remote_close(res, ncomp);
1719 rw_unlock(&hio_remote_lock[ncomp]);
1720 seq = nv_get_uint64(nv, "seq");
1722 pjdlog_error("Header contains no 'seq' field.");
1726 memsyncack = nv_exists(nv, "received");
1727 mtx_lock(&hio_recv_list_lock[ncomp]);
1728 TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
1729 if (hio->hio_ggio.gctl_seq == seq) {
1730 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1735 mtx_unlock(&hio_recv_list_lock[ncomp]);
1737 pjdlog_error("Found no request matching received 'seq' field (%ju).",
1742 ggio = &hio->hio_ggio;
1743 error = nv_get_int16(nv, "error");
1745 /* Request failed on remote side. */
1746 hio->hio_errors[ncomp] = error;
1747 reqlog(LOG_WARNING, 0, ggio,
1748 "Remote request failed (%s): ", strerror(error));
1752 switch (ggio->gctl_cmd) {
1754 rw_rlock(&hio_remote_lock[ncomp]);
1755 if (!ISCONNECTED(res, ncomp)) {
1756 rw_unlock(&hio_remote_lock[ncomp]);
1760 if (hast_proto_recv_data(res, res->hr_remotein, nv,
1761 ggio->gctl_data, ggio->gctl_length) == -1) {
1762 hio->hio_errors[ncomp] = errno;
1763 pjdlog_errno(LOG_ERR,
1764 "Unable to receive reply data");
1765 rw_unlock(&hio_remote_lock[ncomp]);
1767 remote_close(res, ncomp);
1770 rw_unlock(&hio_remote_lock[ncomp]);
1777 PJDLOG_ABORT("invalid condition");
1779 hio->hio_errors[ncomp] = 0;
1782 if (hio->hio_replication != HAST_REPLICATION_MEMSYNC ||
1783 hio->hio_ggio.gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) {
1784 if (refcnt_release(&hio->hio_countdown) > 0)
1788 * Depending on hio_countdown value, requests finished
1789 * in the following order:
1791 * 0: local write, remote memsync, remote final
1793 * 0: remote memsync, local write, remote final
1795 * 1: local write, remote memsync, (remote final)
1797 * 1: remote memsync, remote final, (local write)
1799 * 2: remote memsync, (local write), (remote final)
1801 * 2: remote memsync, (remote final), (local write)
1803 switch (refcnt_release(&hio->hio_countdown)) {
1806 * Remote final reply arrived.
1808 PJDLOG_ASSERT(!memsyncack);
1813 * Local request already finished, so we
1814 * can complete the write.
1816 if (hio->hio_errors[0] == 0)
1817 write_complete(res, hio);
1819 * We still need to wait for final
1823 "remote_recv: (%p) Moving request back to the recv queue.",
1825 mtx_lock(&hio_recv_list_lock[ncomp]);
1826 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp],
1827 hio, hio_next[ncomp]);
1828 mtx_unlock(&hio_recv_list_lock[ncomp]);
1831 * Remote final reply arrived before
1832 * local write finished.
1833 * Nothing to do in such case.
1839 * We received remote memsync reply even before
1840 * local write finished.
1842 PJDLOG_ASSERT(memsyncack);
1845 "remote_recv: (%p) Moving request back to the recv queue.",
1847 mtx_lock(&hio_recv_list_lock[ncomp]);
1848 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio,
1850 mtx_unlock(&hio_recv_list_lock[ncomp]);
1853 PJDLOG_ABORT("Invalid hio_countdown.");
1856 if (ISSYNCREQ(hio)) {
1857 mtx_lock(&sync_lock);
1859 mtx_unlock(&sync_lock);
1860 cv_signal(&sync_cond);
1863 "remote_recv: (%p) Moving request to the done queue.",
1865 QUEUE_INSERT2(hio, done);
1873 * Thread sends answer to the kernel.
1876 ggate_send_thread(void *arg)
1878 struct hast_resource *res = arg;
1879 struct g_gate_ctl_io *ggio;
1881 unsigned int ii, ncomps;
1883 ncomps = HAST_NCOMPONENTS;
1886 pjdlog_debug(2, "ggate_send: Taking request.");
1887 QUEUE_TAKE2(hio, done);
1888 pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
1889 ggio = &hio->hio_ggio;
1890 for (ii = 0; ii < ncomps; ii++) {
1891 if (hio->hio_errors[ii] == 0) {
1893 * One successful request is enough to declare
1896 ggio->gctl_error = 0;
1902 * None of the requests were successful.
1903 * Use the error from local component except the
1904 * case when we did only remote request.
1906 if (ggio->gctl_cmd == BIO_READ &&
1907 res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1908 ggio->gctl_error = hio->hio_errors[1];
1910 ggio->gctl_error = hio->hio_errors[0];
1912 if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
1913 mtx_lock(&res->hr_amp_lock);
1914 if (activemap_write_complete(res->hr_amp,
1915 ggio->gctl_offset, ggio->gctl_length)) {
1916 res->hr_stat_activemap_update++;
1917 (void)hast_activemap_flush(res);
1919 mtx_unlock(&res->hr_amp_lock);
1921 if (ggio->gctl_cmd == BIO_WRITE) {
1923 * Unlock range we locked.
1925 mtx_lock(&range_lock);
1926 rangelock_del(range_regular, ggio->gctl_offset,
1928 if (range_sync_wait)
1929 cv_signal(&range_sync_cond);
1930 mtx_unlock(&range_lock);
1932 write_complete(res, hio);
1934 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
1935 primary_exit(EX_OSERR,
1936 "G_GATE_CMD_DONE failed");
1940 "ggate_send: (%p) Moving request to the free queue.", hio);
1941 QUEUE_INSERT2(hio, free);
1948 * Thread synchronize local and remote components.
1951 sync_thread(void *arg __unused)
1953 struct hast_resource *res = arg;
1955 struct g_gate_ctl_io *ggio;
1956 struct timeval tstart, tend, tdiff;
1957 unsigned int ii, ncomp, ncomps;
1958 off_t offset, length, synced;
1959 bool dorewind, directreads;
1962 ncomps = HAST_NCOMPONENTS;
1966 directreads = false;
1969 mtx_lock(&sync_lock);
1970 if (offset >= 0 && !sync_inprogress) {
1971 gettimeofday(&tend, NULL);
1972 timersub(&tend, &tstart, &tdiff);
1973 pjdlog_info("Synchronization interrupted after %#.0T. "
1974 "%NB synchronized so far.", &tdiff,
1976 event_send(res, EVENT_SYNCINTR);
1978 while (!sync_inprogress) {
1981 cv_wait(&sync_cond, &sync_lock);
1983 mtx_unlock(&sync_lock);
1985 * Obtain offset at which we should synchronize.
1986 * Rewind synchronization if needed.
1988 mtx_lock(&res->hr_amp_lock);
1990 activemap_sync_rewind(res->hr_amp);
1991 offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
1992 if (syncext != -1) {
1994 * We synchronized entire syncext extent, we can mark
1997 if (activemap_extent_complete(res->hr_amp, syncext))
1998 (void)hast_activemap_flush(res);
2000 mtx_unlock(&res->hr_amp_lock);
2004 pjdlog_info("Nodes are in sync.");
2006 pjdlog_info("Synchronization started. %NB to go.",
2007 (intmax_t)(res->hr_extentsize *
2008 activemap_ndirty(res->hr_amp)));
2009 event_send(res, EVENT_SYNCSTART);
2010 gettimeofday(&tstart, NULL);
2015 pjdlog_debug(1, "Nothing to synchronize.");
2017 * Synchronization complete, make both localcnt and
2021 rw_rlock(&hio_remote_lock[ncomp]);
2022 if (ISCONNECTED(res, ncomp)) {
2026 gettimeofday(&tend, NULL);
2027 timersub(&tend, &tstart, &tdiff);
2028 bps = (int64_t)((double)synced /
2029 ((double)tdiff.tv_sec +
2030 (double)tdiff.tv_usec / 1000000));
2031 pjdlog_info("Synchronization complete. "
2032 "%NB synchronized in %#.0lT (%NB/sec).",
2033 (intmax_t)synced, &tdiff,
2035 event_send(res, EVENT_SYNCDONE);
2037 mtx_lock(&metadata_lock);
2038 if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
2040 res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
2041 res->hr_primary_localcnt =
2042 res->hr_secondary_remotecnt;
2043 res->hr_primary_remotecnt =
2044 res->hr_secondary_localcnt;
2046 "Setting localcnt to %ju and remotecnt to %ju.",
2047 (uintmax_t)res->hr_primary_localcnt,
2048 (uintmax_t)res->hr_primary_remotecnt);
2049 (void)metadata_write(res);
2050 mtx_unlock(&metadata_lock);
2052 rw_unlock(&hio_remote_lock[ncomp]);
2054 directreads = false;
2055 enable_direct_reads(res);
2059 pjdlog_debug(2, "sync: Taking free request.");
2060 QUEUE_TAKE2(hio, free);
2061 pjdlog_debug(2, "sync: (%p) Got free request.", hio);
2063 * Lock the range we are going to synchronize. We don't want
2064 * race where someone writes between our read and write.
2067 mtx_lock(&range_lock);
2068 if (rangelock_islocked(range_regular, offset, length)) {
2070 "sync: Range offset=%jd length=%jd locked.",
2071 (intmax_t)offset, (intmax_t)length);
2072 range_sync_wait = true;
2073 cv_wait(&range_sync_cond, &range_lock);
2074 range_sync_wait = false;
2075 mtx_unlock(&range_lock);
2078 if (rangelock_add(range_sync, offset, length) == -1) {
2079 mtx_unlock(&range_lock);
2081 "sync: Range offset=%jd length=%jd is already locked, waiting.",
2082 (intmax_t)offset, (intmax_t)length);
2086 mtx_unlock(&range_lock);
2090 * First read the data from synchronization source.
2093 ggio = &hio->hio_ggio;
2094 ggio->gctl_cmd = BIO_READ;
2095 ggio->gctl_offset = offset;
2096 ggio->gctl_length = length;
2097 ggio->gctl_error = 0;
2098 hio->hio_done = false;
2099 hio->hio_replication = res->hr_replication;
2100 for (ii = 0; ii < ncomps; ii++)
2101 hio->hio_errors[ii] = EINVAL;
2102 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2104 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2106 mtx_lock(&metadata_lock);
2107 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2109 * This range is up-to-date on local component,
2110 * so handle request locally.
2112 /* Local component is 0 for now. */
2114 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2115 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2117 * This range is out-of-date on local component,
2118 * so send request to the remote node.
2120 /* Remote component is 1 for now. */
2123 mtx_unlock(&metadata_lock);
2124 hio->hio_countdown = 1;
2125 QUEUE_INSERT1(hio, send, ncomp);
2128 * Let's wait for READ to finish.
2130 mtx_lock(&sync_lock);
2131 while (!ISSYNCREQDONE(hio))
2132 cv_wait(&sync_cond, &sync_lock);
2133 mtx_unlock(&sync_lock);
2135 if (hio->hio_errors[ncomp] != 0) {
2136 pjdlog_error("Unable to read synchronization data: %s.",
2137 strerror(hio->hio_errors[ncomp]));
2142 * We read the data from synchronization source, now write it
2143 * to synchronization target.
2146 ggio->gctl_cmd = BIO_WRITE;
2147 for (ii = 0; ii < ncomps; ii++)
2148 hio->hio_errors[ii] = EINVAL;
2149 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2151 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2153 mtx_lock(&metadata_lock);
2154 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2156 * This range is up-to-date on local component,
2157 * so we update remote component.
2159 /* Remote component is 1 for now. */
2161 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2162 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2164 * This range is out-of-date on local component,
2167 /* Local component is 0 for now. */
2170 mtx_unlock(&metadata_lock);
2172 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2174 hio->hio_countdown = 1;
2175 QUEUE_INSERT1(hio, send, ncomp);
2178 * Let's wait for WRITE to finish.
2180 mtx_lock(&sync_lock);
2181 while (!ISSYNCREQDONE(hio))
2182 cv_wait(&sync_cond, &sync_lock);
2183 mtx_unlock(&sync_lock);
2185 if (hio->hio_errors[ncomp] != 0) {
2186 pjdlog_error("Unable to write synchronization data: %s.",
2187 strerror(hio->hio_errors[ncomp]));
2193 mtx_lock(&range_lock);
2194 rangelock_del(range_sync, offset, length);
2195 if (range_regular_wait)
2196 cv_signal(&range_regular_cond);
2197 mtx_unlock(&range_lock);
2198 pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
2200 QUEUE_INSERT2(hio, free);
2207 primary_config_reload(struct hast_resource *res, struct nv *nv)
2209 unsigned int ii, ncomps;
2213 pjdlog_info("Reloading configuration...");
2215 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
2216 PJDLOG_ASSERT(gres == res);
2217 nv_assert(nv, "remoteaddr");
2218 nv_assert(nv, "sourceaddr");
2219 nv_assert(nv, "replication");
2220 nv_assert(nv, "checksum");
2221 nv_assert(nv, "compression");
2222 nv_assert(nv, "timeout");
2223 nv_assert(nv, "exec");
2224 nv_assert(nv, "metaflush");
2226 ncomps = HAST_NCOMPONENTS;
2228 #define MODIFIED_REMOTEADDR 0x01
2229 #define MODIFIED_SOURCEADDR 0x02
2230 #define MODIFIED_REPLICATION 0x04
2231 #define MODIFIED_CHECKSUM 0x08
2232 #define MODIFIED_COMPRESSION 0x10
2233 #define MODIFIED_TIMEOUT 0x20
2234 #define MODIFIED_EXEC 0x40
2235 #define MODIFIED_METAFLUSH 0x80
2238 vstr = nv_get_string(nv, "remoteaddr");
2239 if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
2241 * Don't copy res->hr_remoteaddr to gres just yet.
2242 * We want remote_close() to log disconnect from the old
2243 * addresses, not from the new ones.
2245 modified |= MODIFIED_REMOTEADDR;
2247 vstr = nv_get_string(nv, "sourceaddr");
2248 if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
2249 strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
2250 modified |= MODIFIED_SOURCEADDR;
2252 vint = nv_get_int32(nv, "replication");
2253 if (gres->hr_replication != vint) {
2254 gres->hr_replication = vint;
2255 modified |= MODIFIED_REPLICATION;
2257 vint = nv_get_int32(nv, "checksum");
2258 if (gres->hr_checksum != vint) {
2259 gres->hr_checksum = vint;
2260 modified |= MODIFIED_CHECKSUM;
2262 vint = nv_get_int32(nv, "compression");
2263 if (gres->hr_compression != vint) {
2264 gres->hr_compression = vint;
2265 modified |= MODIFIED_COMPRESSION;
2267 vint = nv_get_int32(nv, "timeout");
2268 if (gres->hr_timeout != vint) {
2269 gres->hr_timeout = vint;
2270 modified |= MODIFIED_TIMEOUT;
2272 vstr = nv_get_string(nv, "exec");
2273 if (strcmp(gres->hr_exec, vstr) != 0) {
2274 strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
2275 modified |= MODIFIED_EXEC;
2277 vint = nv_get_int32(nv, "metaflush");
2278 if (gres->hr_metaflush != vint) {
2279 gres->hr_metaflush = vint;
2280 modified |= MODIFIED_METAFLUSH;
2284 * Change timeout for connected sockets.
2285 * Don't bother if we need to reconnect.
2287 if ((modified & MODIFIED_TIMEOUT) != 0 &&
2288 (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
2289 for (ii = 0; ii < ncomps; ii++) {
2292 rw_rlock(&hio_remote_lock[ii]);
2293 if (!ISCONNECTED(gres, ii)) {
2294 rw_unlock(&hio_remote_lock[ii]);
2297 rw_unlock(&hio_remote_lock[ii]);
2298 if (proto_timeout(gres->hr_remotein,
2299 gres->hr_timeout) == -1) {
2300 pjdlog_errno(LOG_WARNING,
2301 "Unable to set connection timeout");
2303 if (proto_timeout(gres->hr_remoteout,
2304 gres->hr_timeout) == -1) {
2305 pjdlog_errno(LOG_WARNING,
2306 "Unable to set connection timeout");
2310 if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
2311 for (ii = 0; ii < ncomps; ii++) {
2314 remote_close(gres, ii);
2316 if (modified & MODIFIED_REMOTEADDR) {
2317 vstr = nv_get_string(nv, "remoteaddr");
2318 strlcpy(gres->hr_remoteaddr, vstr,
2319 sizeof(gres->hr_remoteaddr));
2322 #undef MODIFIED_REMOTEADDR
2323 #undef MODIFIED_SOURCEADDR
2324 #undef MODIFIED_REPLICATION
2325 #undef MODIFIED_CHECKSUM
2326 #undef MODIFIED_COMPRESSION
2327 #undef MODIFIED_TIMEOUT
2328 #undef MODIFIED_EXEC
2329 #undef MODIFIED_METAFLUSH
2331 pjdlog_info("Configuration reloaded successfully.");
2335 guard_one(struct hast_resource *res, unsigned int ncomp)
2337 struct proto_conn *in, *out;
2339 if (!ISREMOTE(ncomp))
2342 rw_rlock(&hio_remote_lock[ncomp]);
2344 if (!real_remote(res)) {
2345 rw_unlock(&hio_remote_lock[ncomp]);
2349 if (ISCONNECTED(res, ncomp)) {
2350 PJDLOG_ASSERT(res->hr_remotein != NULL);
2351 PJDLOG_ASSERT(res->hr_remoteout != NULL);
2352 rw_unlock(&hio_remote_lock[ncomp]);
2353 pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2354 res->hr_remoteaddr);
2358 PJDLOG_ASSERT(res->hr_remotein == NULL);
2359 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2361 * Upgrade the lock. It doesn't have to be atomic as no other thread
2362 * can change connection status from disconnected to connected.
2364 rw_unlock(&hio_remote_lock[ncomp]);
2365 pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2366 res->hr_remoteaddr);
2368 if (init_remote(res, &in, &out) == 0) {
2369 rw_wlock(&hio_remote_lock[ncomp]);
2370 PJDLOG_ASSERT(res->hr_remotein == NULL);
2371 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2372 PJDLOG_ASSERT(in != NULL && out != NULL);
2373 res->hr_remotein = in;
2374 res->hr_remoteout = out;
2375 rw_unlock(&hio_remote_lock[ncomp]);
2376 pjdlog_info("Successfully reconnected to %s.",
2377 res->hr_remoteaddr);
2380 /* Both connections should be NULL. */
2381 PJDLOG_ASSERT(res->hr_remotein == NULL);
2382 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2383 PJDLOG_ASSERT(in == NULL && out == NULL);
2384 pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2385 res->hr_remoteaddr);
2390 * Thread guards remote connections and reconnects when needed, handles
2394 guard_thread(void *arg)
2396 struct hast_resource *res = arg;
2397 unsigned int ii, ncomps;
2398 struct timespec timeout;
2399 time_t lastcheck, now;
2403 ncomps = HAST_NCOMPONENTS;
2404 lastcheck = time(NULL);
2406 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
2407 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
2408 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
2410 timeout.tv_sec = HAST_KEEPALIVE;
2411 timeout.tv_nsec = 0;
2418 sigexit_received = true;
2419 primary_exitx(EX_OK,
2420 "Termination signal received, exiting.");
2427 * Don't check connections until we fully started,
2428 * as we may still be looping, waiting for remote node
2429 * to switch from primary to secondary.
2432 pjdlog_debug(2, "remote_guard: Checking connections.");
2434 if (lastcheck + HAST_KEEPALIVE <= now) {
2435 for (ii = 0; ii < ncomps; ii++)
2440 signo = sigtimedwait(&mask, NULL, &timeout);