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");
306 res->hr_stat_activemap_write_error++;
309 if (res->hr_metaflush == 1 && g_flush(res->hr_localfd) == -1) {
310 if (errno == EOPNOTSUPP) {
311 pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.",
313 res->hr_metaflush = 0;
315 pjdlog_errno(LOG_ERR,
316 "Unable to flush disk cache on activemap update");
317 res->hr_stat_activemap_flush_error++;
325 real_remote(const struct hast_resource *res)
328 return (strcmp(res->hr_remoteaddr, "none") != 0);
332 init_environment(struct hast_resource *res __unused)
335 unsigned int ii, ncomps;
338 * In the future it might be per-resource value.
340 ncomps = HAST_NCOMPONENTS;
343 * Allocate memory needed by lists.
345 hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
346 if (hio_send_list == NULL) {
347 primary_exitx(EX_TEMPFAIL,
348 "Unable to allocate %zu bytes of memory for send lists.",
349 sizeof(hio_send_list[0]) * ncomps);
351 hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
352 if (hio_send_list_lock == NULL) {
353 primary_exitx(EX_TEMPFAIL,
354 "Unable to allocate %zu bytes of memory for send list locks.",
355 sizeof(hio_send_list_lock[0]) * ncomps);
357 hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
358 if (hio_send_list_cond == NULL) {
359 primary_exitx(EX_TEMPFAIL,
360 "Unable to allocate %zu bytes of memory for send list condition variables.",
361 sizeof(hio_send_list_cond[0]) * ncomps);
363 hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
364 if (hio_recv_list == NULL) {
365 primary_exitx(EX_TEMPFAIL,
366 "Unable to allocate %zu bytes of memory for recv lists.",
367 sizeof(hio_recv_list[0]) * ncomps);
369 hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
370 if (hio_recv_list_lock == NULL) {
371 primary_exitx(EX_TEMPFAIL,
372 "Unable to allocate %zu bytes of memory for recv list locks.",
373 sizeof(hio_recv_list_lock[0]) * ncomps);
375 hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
376 if (hio_recv_list_cond == NULL) {
377 primary_exitx(EX_TEMPFAIL,
378 "Unable to allocate %zu bytes of memory for recv list condition variables.",
379 sizeof(hio_recv_list_cond[0]) * ncomps);
381 hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
382 if (hio_remote_lock == NULL) {
383 primary_exitx(EX_TEMPFAIL,
384 "Unable to allocate %zu bytes of memory for remote connections locks.",
385 sizeof(hio_remote_lock[0]) * ncomps);
389 * Initialize lists, their locks and theirs condition variables.
391 TAILQ_INIT(&hio_free_list);
392 mtx_init(&hio_free_list_lock);
393 cv_init(&hio_free_list_cond);
394 for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
395 TAILQ_INIT(&hio_send_list[ii]);
396 mtx_init(&hio_send_list_lock[ii]);
397 cv_init(&hio_send_list_cond[ii]);
398 TAILQ_INIT(&hio_recv_list[ii]);
399 mtx_init(&hio_recv_list_lock[ii]);
400 cv_init(&hio_recv_list_cond[ii]);
401 rw_init(&hio_remote_lock[ii]);
403 TAILQ_INIT(&hio_done_list);
404 mtx_init(&hio_done_list_lock);
405 cv_init(&hio_done_list_cond);
406 mtx_init(&metadata_lock);
409 * Allocate requests pool and initialize requests.
411 for (ii = 0; ii < HAST_HIO_MAX; ii++) {
412 hio = malloc(sizeof(*hio));
414 primary_exitx(EX_TEMPFAIL,
415 "Unable to allocate %zu bytes of memory for hio request.",
418 hio->hio_countdown = 0;
419 hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
420 if (hio->hio_errors == NULL) {
421 primary_exitx(EX_TEMPFAIL,
422 "Unable allocate %zu bytes of memory for hio errors.",
423 sizeof(hio->hio_errors[0]) * ncomps);
425 hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
426 if (hio->hio_next == NULL) {
427 primary_exitx(EX_TEMPFAIL,
428 "Unable allocate %zu bytes of memory for hio_next field.",
429 sizeof(hio->hio_next[0]) * ncomps);
431 hio->hio_ggio.gctl_version = G_GATE_VERSION;
432 hio->hio_ggio.gctl_data = malloc(MAXPHYS);
433 if (hio->hio_ggio.gctl_data == NULL) {
434 primary_exitx(EX_TEMPFAIL,
435 "Unable to allocate %zu bytes of memory for gctl_data.",
438 hio->hio_ggio.gctl_length = MAXPHYS;
439 hio->hio_ggio.gctl_error = 0;
440 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
445 init_resuid(struct hast_resource *res)
448 mtx_lock(&metadata_lock);
449 if (res->hr_resuid != 0) {
450 mtx_unlock(&metadata_lock);
453 /* Initialize unique resource identifier. */
454 arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
455 mtx_unlock(&metadata_lock);
456 if (metadata_write(res) == -1)
463 init_local(struct hast_resource *res)
468 if (metadata_read(res, true) == -1)
470 mtx_init(&res->hr_amp_lock);
471 if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
472 res->hr_local_sectorsize, res->hr_keepdirty) == -1) {
473 primary_exit(EX_TEMPFAIL, "Unable to create activemap");
475 mtx_init(&range_lock);
476 cv_init(&range_regular_cond);
477 if (rangelock_init(&range_regular) == -1)
478 primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
479 cv_init(&range_sync_cond);
480 if (rangelock_init(&range_sync) == -1)
481 primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
482 mapsize = activemap_ondisk_size(res->hr_amp);
483 buf = calloc(1, mapsize);
485 primary_exitx(EX_TEMPFAIL,
486 "Unable to allocate buffer for activemap.");
488 if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
490 primary_exit(EX_NOINPUT, "Unable to read activemap");
492 activemap_copyin(res->hr_amp, buf, mapsize);
494 if (res->hr_resuid != 0)
497 * We're using provider for the first time. Initialize local and remote
498 * counters. We don't initialize resuid here, as we want to do it just
499 * in time. The reason for this is that we want to inform secondary
500 * that there were no writes yet, so there is no need to synchronize
503 res->hr_primary_localcnt = 0;
504 res->hr_primary_remotecnt = 0;
505 if (metadata_write(res) == -1)
510 primary_connect(struct hast_resource *res, struct proto_conn **connp)
512 struct proto_conn *conn;
516 if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
517 primary_exit(EX_TEMPFAIL,
518 "Unable to send connection request to parent");
520 if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
521 primary_exit(EX_TEMPFAIL,
522 "Unable to receive reply to connection request from parent");
526 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
530 if (proto_connection_recv(res->hr_conn, true, &conn) == -1) {
531 primary_exit(EX_TEMPFAIL,
532 "Unable to receive connection from parent");
534 if (proto_connect_wait(conn, res->hr_timeout) == -1) {
535 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
540 /* Error in setting timeout is not critical, but why should it fail? */
541 if (proto_timeout(conn, res->hr_timeout) == -1)
542 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
550 * Function instructs GEOM_GATE to handle reads directly from within the kernel.
553 enable_direct_reads(struct hast_resource *res)
555 struct g_gate_ctl_modify ggiomodify;
557 bzero(&ggiomodify, sizeof(ggiomodify));
558 ggiomodify.gctl_version = G_GATE_VERSION;
559 ggiomodify.gctl_unit = res->hr_ggateunit;
560 ggiomodify.gctl_modify = GG_MODIFY_READPROV | GG_MODIFY_READOFFSET;
561 strlcpy(ggiomodify.gctl_readprov, res->hr_localpath,
562 sizeof(ggiomodify.gctl_readprov));
563 ggiomodify.gctl_readoffset = res->hr_localoff;
564 if (ioctl(res->hr_ggatefd, G_GATE_CMD_MODIFY, &ggiomodify) == 0)
565 pjdlog_debug(1, "Direct reads enabled.");
567 pjdlog_errno(LOG_WARNING, "Failed to enable direct reads");
571 init_remote(struct hast_resource *res, struct proto_conn **inp,
572 struct proto_conn **outp)
574 struct proto_conn *in, *out;
575 struct nv *nvout, *nvin;
576 const unsigned char *token;
586 PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
587 PJDLOG_ASSERT(real_remote(res));
592 if (primary_connect(res, &out) == -1)
593 return (ECONNREFUSED);
595 error = ECONNABORTED;
598 * First handshake step.
599 * Setup outgoing connection with remote node.
602 nv_add_string(nvout, res->hr_name, "resource");
603 nv_add_uint8(nvout, HAST_PROTO_VERSION, "version");
604 if (nv_error(nvout) != 0) {
605 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
606 "Unable to allocate header for connection with %s",
611 if (hast_proto_send(res, out, nvout, NULL, 0) == -1) {
612 pjdlog_errno(LOG_WARNING,
613 "Unable to send handshake header to %s",
619 if (hast_proto_recv_hdr(out, &nvin) == -1) {
620 pjdlog_errno(LOG_WARNING,
621 "Unable to receive handshake header from %s",
625 errmsg = nv_get_string(nvin, "errmsg");
626 if (errmsg != NULL) {
627 pjdlog_warning("%s", errmsg);
628 if (nv_exists(nvin, "wait"))
633 version = nv_get_uint8(nvin, "version");
636 * If no version is sent, it means this is protocol version 1.
640 if (version > HAST_PROTO_VERSION) {
641 pjdlog_warning("Invalid version received (%hhu).", version);
645 res->hr_version = version;
646 pjdlog_debug(1, "Negotiated protocol version %d.", res->hr_version);
647 token = nv_get_uint8_array(nvin, &size, "token");
649 pjdlog_warning("Handshake header from %s has no 'token' field.",
654 if (size != sizeof(res->hr_token)) {
655 pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
656 res->hr_remoteaddr, size, sizeof(res->hr_token));
660 bcopy(token, res->hr_token, sizeof(res->hr_token));
664 * Second handshake step.
665 * Setup incoming connection with remote node.
667 if (primary_connect(res, &in) == -1)
671 nv_add_string(nvout, res->hr_name, "resource");
672 nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
674 if (res->hr_resuid == 0) {
676 * The resuid field was not yet initialized.
677 * Because we do synchronization inside init_resuid(), it is
678 * possible that someone already initialized it, the function
679 * will return false then, but if we successfully initialized
680 * it, we will get true. True means that there were no writes
681 * to this resource yet and we want to inform secondary that
682 * synchronization is not needed by sending "virgin" argument.
684 if (init_resuid(res))
685 nv_add_int8(nvout, 1, "virgin");
687 nv_add_uint64(nvout, res->hr_resuid, "resuid");
688 nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
689 nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
690 if (nv_error(nvout) != 0) {
691 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
692 "Unable to allocate header for connection with %s",
697 if (hast_proto_send(res, in, nvout, NULL, 0) == -1) {
698 pjdlog_errno(LOG_WARNING,
699 "Unable to send handshake header to %s",
705 if (hast_proto_recv_hdr(out, &nvin) == -1) {
706 pjdlog_errno(LOG_WARNING,
707 "Unable to receive handshake header from %s",
711 errmsg = nv_get_string(nvin, "errmsg");
712 if (errmsg != NULL) {
713 pjdlog_warning("%s", errmsg);
717 datasize = nv_get_int64(nvin, "datasize");
718 if (datasize != res->hr_datasize) {
719 pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
720 (intmax_t)res->hr_datasize, (intmax_t)datasize);
724 extentsize = nv_get_int32(nvin, "extentsize");
725 if (extentsize != res->hr_extentsize) {
726 pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
727 (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
731 res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
732 res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
733 res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
734 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY)
735 enable_direct_reads(res);
736 if (nv_exists(nvin, "virgin")) {
738 * Secondary was reinitialized, bump localcnt if it is 0 as
739 * only we have the data.
741 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
742 PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
744 if (res->hr_primary_localcnt == 0) {
745 PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
747 mtx_lock(&metadata_lock);
748 res->hr_primary_localcnt++;
749 pjdlog_debug(1, "Increasing localcnt to %ju.",
750 (uintmax_t)res->hr_primary_localcnt);
751 (void)metadata_write(res);
752 mtx_unlock(&metadata_lock);
756 mapsize = nv_get_uint32(nvin, "mapsize");
758 map = malloc(mapsize);
760 pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
766 * Remote node have some dirty extents on its own, lets
767 * download its activemap.
769 if (hast_proto_recv_data(res, out, nvin, map,
771 pjdlog_errno(LOG_ERR,
772 "Unable to receive remote activemap");
778 * Merge local and remote bitmaps.
780 activemap_merge(res->hr_amp, map, mapsize);
783 * Now that we merged bitmaps from both nodes, flush it to the
784 * disk before we start to synchronize.
786 (void)hast_activemap_flush(res);
790 /* Setup directions. */
791 if (proto_send(out, NULL, 0) == -1)
792 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
793 if (proto_recv(in, NULL, 0) == -1)
794 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
796 pjdlog_info("Connected to %s.", res->hr_remoteaddr);
797 if (res->hr_original_replication == HAST_REPLICATION_MEMSYNC &&
798 res->hr_version < 2) {
799 pjdlog_warning("The 'memsync' replication mode is not supported by the remote node, falling back to 'fullsync' mode.");
800 res->hr_replication = HAST_REPLICATION_FULLSYNC;
801 } else if (res->hr_replication != res->hr_original_replication) {
803 * This is in case hastd disconnected and was upgraded.
805 res->hr_replication = res->hr_original_replication;
807 if (inp != NULL && outp != NULL) {
811 res->hr_remotein = in;
812 res->hr_remoteout = out;
814 event_send(res, EVENT_CONNECT);
817 if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
818 event_send(res, EVENT_SPLITBRAIN);
829 mtx_lock(&sync_lock);
830 sync_inprogress = true;
831 mtx_unlock(&sync_lock);
832 cv_signal(&sync_cond);
839 mtx_lock(&sync_lock);
841 sync_inprogress = false;
842 mtx_unlock(&sync_lock);
846 init_ggate(struct hast_resource *res)
848 struct g_gate_ctl_create ggiocreate;
849 struct g_gate_ctl_cancel ggiocancel;
852 * We communicate with ggate via /dev/ggctl. Open it.
854 res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
855 if (res->hr_ggatefd == -1)
856 primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
858 * Create provider before trying to connect, as connection failure
859 * is not critical, but may take some time.
861 bzero(&ggiocreate, sizeof(ggiocreate));
862 ggiocreate.gctl_version = G_GATE_VERSION;
863 ggiocreate.gctl_mediasize = res->hr_datasize;
864 ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
865 ggiocreate.gctl_flags = 0;
866 ggiocreate.gctl_maxcount = 0;
867 ggiocreate.gctl_timeout = 0;
868 ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
869 snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
871 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
872 pjdlog_info("Device hast/%s created.", res->hr_provname);
873 res->hr_ggateunit = ggiocreate.gctl_unit;
876 if (errno != EEXIST) {
877 primary_exit(EX_OSERR, "Unable to create hast/%s device",
881 "Device hast/%s already exists, we will try to take it over.",
884 * If we received EEXIST, we assume that the process who created the
885 * provider died and didn't clean up. In that case we will start from
888 bzero(&ggiocancel, sizeof(ggiocancel));
889 ggiocancel.gctl_version = G_GATE_VERSION;
890 ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
891 snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
893 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
894 pjdlog_info("Device hast/%s recovered.", res->hr_provname);
895 res->hr_ggateunit = ggiocancel.gctl_unit;
898 primary_exit(EX_OSERR, "Unable to take over hast/%s device",
903 hastd_primary(struct hast_resource *res)
907 int error, mode, debuglevel;
910 * Create communication channel for sending control commands from
913 if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
914 /* TODO: There's no need for this to be fatal error. */
915 KEEP_ERRNO((void)pidfile_remove(pfh));
916 pjdlog_exit(EX_OSERR,
917 "Unable to create control sockets between parent and child");
920 * Create communication channel for sending events from child to parent.
922 if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
923 /* TODO: There's no need for this to be fatal error. */
924 KEEP_ERRNO((void)pidfile_remove(pfh));
925 pjdlog_exit(EX_OSERR,
926 "Unable to create event sockets between child and parent");
929 * Create communication channel for sending connection requests from
932 if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) {
933 /* TODO: There's no need for this to be fatal error. */
934 KEEP_ERRNO((void)pidfile_remove(pfh));
935 pjdlog_exit(EX_OSERR,
936 "Unable to create connection sockets between child and parent");
941 /* TODO: There's no need for this to be fatal error. */
942 KEEP_ERRNO((void)pidfile_remove(pfh));
943 pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
947 /* This is parent. */
948 /* Declare that we are receiver. */
949 proto_recv(res->hr_event, NULL, 0);
950 proto_recv(res->hr_conn, NULL, 0);
951 /* Declare that we are sender. */
952 proto_send(res->hr_ctrl, NULL, 0);
953 res->hr_workerpid = pid;
958 mode = pjdlog_mode_get();
959 debuglevel = pjdlog_debug_get();
961 /* Declare that we are sender. */
962 proto_send(res->hr_event, NULL, 0);
963 proto_send(res->hr_conn, NULL, 0);
964 /* Declare that we are receiver. */
965 proto_recv(res->hr_ctrl, NULL, 0);
966 descriptors_cleanup(res);
968 descriptors_assert(res, mode);
971 pjdlog_debug_set(debuglevel);
972 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
973 setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
977 init_environment(res);
979 if (drop_privs(res) != 0) {
983 pjdlog_info("Privileges successfully dropped.");
986 * Create the guard thread first, so we can handle signals from the
989 error = pthread_create(&td, NULL, guard_thread, res);
990 PJDLOG_ASSERT(error == 0);
992 * Create the control thread before sending any event to the parent,
993 * as we can deadlock when parent sends control request to worker,
994 * but worker has no control thread started yet, so parent waits.
995 * In the meantime worker sends an event to the parent, but parent
996 * is unable to handle the event, because it waits for control
999 error = pthread_create(&td, NULL, ctrl_thread, res);
1000 PJDLOG_ASSERT(error == 0);
1001 if (real_remote(res)) {
1002 error = init_remote(res, NULL, NULL);
1005 } else if (error == EBUSY) {
1006 time_t start = time(NULL);
1008 pjdlog_warning("Waiting for remote node to become %s for %ds.",
1009 role2str(HAST_ROLE_SECONDARY),
1013 error = init_remote(res, NULL, NULL);
1016 if (time(NULL) > start + res->hr_timeout)
1019 if (error == EBUSY) {
1020 pjdlog_warning("Remote node is still %s, starting anyway.",
1021 role2str(HAST_ROLE_PRIMARY));
1025 error = pthread_create(&td, NULL, ggate_recv_thread, res);
1026 PJDLOG_ASSERT(error == 0);
1027 error = pthread_create(&td, NULL, local_send_thread, res);
1028 PJDLOG_ASSERT(error == 0);
1029 error = pthread_create(&td, NULL, remote_send_thread, res);
1030 PJDLOG_ASSERT(error == 0);
1031 error = pthread_create(&td, NULL, remote_recv_thread, res);
1032 PJDLOG_ASSERT(error == 0);
1033 error = pthread_create(&td, NULL, ggate_send_thread, res);
1034 PJDLOG_ASSERT(error == 0);
1035 fullystarted = true;
1036 (void)sync_thread(res);
1040 reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio,
1041 const char *fmt, ...)
1047 (void)vsnprintf(msg, sizeof(msg), fmt, ap);
1049 switch (ggio->gctl_cmd) {
1051 (void)snprlcat(msg, sizeof(msg), "READ(%ju, %ju).",
1052 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1055 (void)snprlcat(msg, sizeof(msg), "DELETE(%ju, %ju).",
1056 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1059 (void)snprlcat(msg, sizeof(msg), "FLUSH.");
1062 (void)snprlcat(msg, sizeof(msg), "WRITE(%ju, %ju).",
1063 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1066 (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%u).",
1067 (unsigned int)ggio->gctl_cmd);
1070 pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
1074 remote_close(struct hast_resource *res, int ncomp)
1077 rw_wlock(&hio_remote_lock[ncomp]);
1079 * Check for a race between dropping rlock and acquiring wlock -
1080 * another thread can close connection in-between.
1082 if (!ISCONNECTED(res, ncomp)) {
1083 PJDLOG_ASSERT(res->hr_remotein == NULL);
1084 PJDLOG_ASSERT(res->hr_remoteout == NULL);
1085 rw_unlock(&hio_remote_lock[ncomp]);
1089 PJDLOG_ASSERT(res->hr_remotein != NULL);
1090 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1092 pjdlog_debug(2, "Closing incoming connection to %s.",
1093 res->hr_remoteaddr);
1094 proto_close(res->hr_remotein);
1095 res->hr_remotein = NULL;
1096 pjdlog_debug(2, "Closing outgoing connection to %s.",
1097 res->hr_remoteaddr);
1098 proto_close(res->hr_remoteout);
1099 res->hr_remoteout = NULL;
1101 rw_unlock(&hio_remote_lock[ncomp]);
1103 pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
1106 * Stop synchronization if in-progress.
1110 event_send(res, EVENT_DISCONNECT);
1114 * Acknowledge write completion to the kernel, but don't update activemap yet.
1117 write_complete(struct hast_resource *res, struct hio *hio)
1119 struct g_gate_ctl_io *ggio;
1122 PJDLOG_ASSERT(!hio->hio_done);
1124 ggio = &hio->hio_ggio;
1125 PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE);
1128 * Bump local count if this is first write after
1129 * connection failure with remote node.
1132 rw_rlock(&hio_remote_lock[ncomp]);
1133 if (!ISCONNECTED(res, ncomp)) {
1134 mtx_lock(&metadata_lock);
1135 if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) {
1136 res->hr_primary_localcnt++;
1137 pjdlog_debug(1, "Increasing localcnt to %ju.",
1138 (uintmax_t)res->hr_primary_localcnt);
1139 (void)metadata_write(res);
1141 mtx_unlock(&metadata_lock);
1143 rw_unlock(&hio_remote_lock[ncomp]);
1144 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1)
1145 primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
1146 hio->hio_done = true;
1150 * Thread receives ggate I/O requests from the kernel and passes them to
1151 * appropriate threads:
1152 * WRITE - always goes to both local_send and remote_send threads
1153 * READ (when the block is up-to-date on local component) -
1154 * only local_send thread
1155 * READ (when the block isn't up-to-date on local component) -
1156 * only remote_send thread
1157 * DELETE - always goes to both local_send and remote_send threads
1158 * FLUSH - always goes to both local_send and remote_send threads
1161 ggate_recv_thread(void *arg)
1163 struct hast_resource *res = arg;
1164 struct g_gate_ctl_io *ggio;
1166 unsigned int ii, ncomp, ncomps;
1170 pjdlog_debug(2, "ggate_recv: Taking free request.");
1171 QUEUE_TAKE2(hio, free);
1172 pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
1173 ggio = &hio->hio_ggio;
1174 ggio->gctl_unit = res->hr_ggateunit;
1175 ggio->gctl_length = MAXPHYS;
1176 ggio->gctl_error = 0;
1177 hio->hio_done = false;
1178 hio->hio_replication = res->hr_replication;
1180 "ggate_recv: (%p) Waiting for request from the kernel.",
1182 if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) {
1183 if (sigexit_received)
1185 primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
1187 error = ggio->gctl_error;
1192 /* Exit gracefully. */
1193 if (!sigexit_received) {
1195 "ggate_recv: (%p) Received cancel from the kernel.",
1197 pjdlog_info("Received cancel from the kernel, exiting.");
1202 * Buffer too small? Impossible, we allocate MAXPHYS
1203 * bytes - request can't be bigger than that.
1208 primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
1213 ncomps = HAST_NCOMPONENTS;
1215 for (ii = 0; ii < ncomps; ii++)
1216 hio->hio_errors[ii] = EINVAL;
1217 reqlog(LOG_DEBUG, 2, ggio,
1218 "ggate_recv: (%p) Request received from the kernel: ",
1222 * Inform all components about new write request.
1223 * For read request prefer local component unless the given
1224 * range is out-of-date, then use remote component.
1226 switch (ggio->gctl_cmd) {
1228 res->hr_stat_read++;
1230 mtx_lock(&metadata_lock);
1231 if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
1232 res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
1234 * This range is up-to-date on local component,
1235 * so handle request locally.
1237 /* Local component is 0 for now. */
1239 } else /* if (res->hr_syncsrc ==
1240 HAST_SYNCSRC_SECONDARY) */ {
1241 PJDLOG_ASSERT(res->hr_syncsrc ==
1242 HAST_SYNCSRC_SECONDARY);
1244 * This range is out-of-date on local component,
1245 * so send request to the remote node.
1247 /* Remote component is 1 for now. */
1250 mtx_unlock(&metadata_lock);
1253 res->hr_stat_write++;
1254 if (res->hr_resuid == 0 &&
1255 res->hr_primary_localcnt == 0) {
1256 /* This is first write. */
1257 res->hr_primary_localcnt = 1;
1260 mtx_lock(&range_lock);
1261 if (rangelock_islocked(range_sync,
1262 ggio->gctl_offset, ggio->gctl_length)) {
1264 "regular: Range offset=%jd length=%zu locked.",
1265 (intmax_t)ggio->gctl_offset,
1266 (size_t)ggio->gctl_length);
1267 range_regular_wait = true;
1268 cv_wait(&range_regular_cond, &range_lock);
1269 range_regular_wait = false;
1270 mtx_unlock(&range_lock);
1273 if (rangelock_add(range_regular,
1274 ggio->gctl_offset, ggio->gctl_length) == -1) {
1275 mtx_unlock(&range_lock);
1277 "regular: Range offset=%jd length=%zu is already locked, waiting.",
1278 (intmax_t)ggio->gctl_offset,
1279 (size_t)ggio->gctl_length);
1283 mtx_unlock(&range_lock);
1286 mtx_lock(&res->hr_amp_lock);
1287 if (activemap_write_start(res->hr_amp,
1288 ggio->gctl_offset, ggio->gctl_length)) {
1289 res->hr_stat_activemap_update++;
1290 (void)hast_activemap_flush(res);
1292 mtx_unlock(&res->hr_amp_lock);
1295 res->hr_stat_delete++;
1298 res->hr_stat_flush++;
1302 "ggate_recv: (%p) Moving request to the send queues.", hio);
1303 hio->hio_countdown = ncomps;
1304 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC &&
1305 ggio->gctl_cmd == BIO_WRITE) {
1306 /* Each remote request needs two responses in memsync. */
1307 hio->hio_countdown++;
1309 for (ii = ncomp; ii < ncomps; ii++)
1310 QUEUE_INSERT1(hio, send, ii);
1317 * Thread reads from or writes to local component.
1318 * If local read fails, it redirects it to remote_send thread.
1321 local_send_thread(void *arg)
1323 struct hast_resource *res = arg;
1324 struct g_gate_ctl_io *ggio;
1326 unsigned int ncomp, rncomp;
1329 /* Local component is 0 for now. */
1331 /* Remote component is 1 for now. */
1335 pjdlog_debug(2, "local_send: Taking request.");
1336 QUEUE_TAKE1(hio, send, ncomp, 0);
1337 pjdlog_debug(2, "local_send: (%p) Got request.", hio);
1338 ggio = &hio->hio_ggio;
1339 switch (ggio->gctl_cmd) {
1341 ret = pread(res->hr_localfd, ggio->gctl_data,
1343 ggio->gctl_offset + res->hr_localoff);
1344 if (ret == ggio->gctl_length)
1345 hio->hio_errors[ncomp] = 0;
1346 else if (!ISSYNCREQ(hio)) {
1348 * If READ failed, try to read from remote node.
1351 reqlog(LOG_WARNING, 0, ggio,
1352 "Local request failed (%s), trying remote node. ",
1354 } else if (ret != ggio->gctl_length) {
1355 reqlog(LOG_WARNING, 0, ggio,
1356 "Local request failed (%zd != %jd), trying remote node. ",
1357 ret, (intmax_t)ggio->gctl_length);
1359 QUEUE_INSERT1(hio, send, rncomp);
1364 ret = pwrite(res->hr_localfd, ggio->gctl_data,
1366 ggio->gctl_offset + res->hr_localoff);
1368 hio->hio_errors[ncomp] = errno;
1369 reqlog(LOG_WARNING, 0, ggio,
1370 "Local request failed (%s): ",
1372 } else if (ret != ggio->gctl_length) {
1373 hio->hio_errors[ncomp] = EIO;
1374 reqlog(LOG_WARNING, 0, ggio,
1375 "Local request failed (%zd != %jd): ",
1376 ret, (intmax_t)ggio->gctl_length);
1378 hio->hio_errors[ncomp] = 0;
1379 if (hio->hio_replication ==
1380 HAST_REPLICATION_ASYNC) {
1381 ggio->gctl_error = 0;
1382 write_complete(res, hio);
1387 ret = g_delete(res->hr_localfd,
1388 ggio->gctl_offset + res->hr_localoff,
1391 hio->hio_errors[ncomp] = errno;
1392 reqlog(LOG_WARNING, 0, ggio,
1393 "Local request failed (%s): ",
1396 hio->hio_errors[ncomp] = 0;
1400 if (!res->hr_localflush) {
1405 ret = g_flush(res->hr_localfd);
1407 if (errno == EOPNOTSUPP)
1408 res->hr_localflush = false;
1409 hio->hio_errors[ncomp] = errno;
1410 reqlog(LOG_WARNING, 0, ggio,
1411 "Local request failed (%s): ",
1414 hio->hio_errors[ncomp] = 0;
1419 if (hio->hio_replication != HAST_REPLICATION_MEMSYNC ||
1420 ggio->gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) {
1421 if (refcnt_release(&hio->hio_countdown) > 0)
1425 * Depending on hio_countdown value, requests finished
1426 * in the following order:
1427 * 0: remote memsync, remote final, local write
1428 * 1: remote memsync, local write, (remote final)
1429 * 2: local write, (remote memsync), (remote final)
1431 switch (refcnt_release(&hio->hio_countdown)) {
1434 * Local write finished as last.
1439 * Local write finished after remote memsync
1440 * reply arrvied. We can complete the write now.
1442 if (hio->hio_errors[0] == 0)
1443 write_complete(res, hio);
1447 * Local write finished as first.
1451 PJDLOG_ABORT("Invalid hio_countdown.");
1454 if (ISSYNCREQ(hio)) {
1455 mtx_lock(&sync_lock);
1457 mtx_unlock(&sync_lock);
1458 cv_signal(&sync_cond);
1461 "local_send: (%p) Moving request to the done queue.",
1463 QUEUE_INSERT2(hio, done);
1471 keepalive_send(struct hast_resource *res, unsigned int ncomp)
1475 rw_rlock(&hio_remote_lock[ncomp]);
1477 if (!ISCONNECTED(res, ncomp)) {
1478 rw_unlock(&hio_remote_lock[ncomp]);
1482 PJDLOG_ASSERT(res->hr_remotein != NULL);
1483 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1486 nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1487 if (nv_error(nv) != 0) {
1488 rw_unlock(&hio_remote_lock[ncomp]);
1491 "keepalive_send: Unable to prepare header to send.");
1494 if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
1495 rw_unlock(&hio_remote_lock[ncomp]);
1496 pjdlog_common(LOG_DEBUG, 1, errno,
1497 "keepalive_send: Unable to send request");
1499 remote_close(res, ncomp);
1503 rw_unlock(&hio_remote_lock[ncomp]);
1505 pjdlog_debug(2, "keepalive_send: Request sent.");
1509 * Thread sends request to secondary node.
1512 remote_send_thread(void *arg)
1514 struct hast_resource *res = arg;
1515 struct g_gate_ctl_io *ggio;
1516 time_t lastcheck, now;
1521 uint64_t offset, length;
1525 /* Remote component is 1 for now. */
1527 lastcheck = time(NULL);
1530 pjdlog_debug(2, "remote_send: Taking request.");
1531 QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1534 if (lastcheck + HAST_KEEPALIVE <= now) {
1535 keepalive_send(res, ncomp);
1540 pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
1541 ggio = &hio->hio_ggio;
1542 switch (ggio->gctl_cmd) {
1546 offset = ggio->gctl_offset;
1547 length = ggio->gctl_length;
1551 data = ggio->gctl_data;
1552 offset = ggio->gctl_offset;
1553 length = ggio->gctl_length;
1558 offset = ggio->gctl_offset;
1559 length = ggio->gctl_length;
1568 PJDLOG_ABORT("invalid condition");
1571 nv_add_uint8(nv, cmd, "cmd");
1572 nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
1573 nv_add_uint64(nv, offset, "offset");
1574 nv_add_uint64(nv, length, "length");
1575 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC &&
1576 ggio->gctl_cmd == BIO_WRITE && !ISSYNCREQ(hio)) {
1577 nv_add_uint8(nv, 1, "memsync");
1579 if (nv_error(nv) != 0) {
1580 hio->hio_errors[ncomp] = nv_error(nv);
1582 "remote_send: (%p) Unable to prepare header to send.",
1584 reqlog(LOG_ERR, 0, ggio,
1585 "Unable to prepare header to send (%s): ",
1586 strerror(nv_error(nv)));
1587 /* Move failed request immediately to the done queue. */
1591 * Protect connection from disappearing.
1593 rw_rlock(&hio_remote_lock[ncomp]);
1594 if (!ISCONNECTED(res, ncomp)) {
1595 rw_unlock(&hio_remote_lock[ncomp]);
1596 hio->hio_errors[ncomp] = ENOTCONN;
1600 * Move the request to recv queue before sending it, because
1601 * in different order we can get reply before we move request
1605 "remote_send: (%p) Moving request to the recv queue.",
1607 mtx_lock(&hio_recv_list_lock[ncomp]);
1608 wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
1609 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
1610 mtx_unlock(&hio_recv_list_lock[ncomp]);
1611 if (hast_proto_send(res, res->hr_remoteout, nv, data,
1612 data != NULL ? length : 0) == -1) {
1613 hio->hio_errors[ncomp] = errno;
1614 rw_unlock(&hio_remote_lock[ncomp]);
1616 "remote_send: (%p) Unable to send request.", hio);
1617 reqlog(LOG_ERR, 0, ggio,
1618 "Unable to send request (%s): ",
1619 strerror(hio->hio_errors[ncomp]));
1620 remote_close(res, ncomp);
1622 * Take request back from the receive queue and move
1623 * it immediately to the done queue.
1625 mtx_lock(&hio_recv_list_lock[ncomp]);
1626 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1628 mtx_unlock(&hio_recv_list_lock[ncomp]);
1631 rw_unlock(&hio_remote_lock[ncomp]);
1634 cv_signal(&hio_recv_list_cond[ncomp]);
1638 if (ISSYNCREQ(hio)) {
1639 if (refcnt_release(&hio->hio_countdown) > 0)
1641 mtx_lock(&sync_lock);
1643 mtx_unlock(&sync_lock);
1644 cv_signal(&sync_cond);
1647 if (ggio->gctl_cmd == BIO_WRITE) {
1648 mtx_lock(&res->hr_amp_lock);
1649 if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
1650 ggio->gctl_length)) {
1651 (void)hast_activemap_flush(res);
1653 mtx_unlock(&res->hr_amp_lock);
1654 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC)
1655 (void)refcnt_release(&hio->hio_countdown);
1657 if (refcnt_release(&hio->hio_countdown) > 0)
1660 "remote_send: (%p) Moving request to the done queue.",
1662 QUEUE_INSERT2(hio, done);
1669 * Thread receives answer from secondary node and passes it to ggate_send
1673 remote_recv_thread(void *arg)
1675 struct hast_resource *res = arg;
1676 struct g_gate_ctl_io *ggio;
1684 /* Remote component is 1 for now. */
1688 /* Wait until there is anything to receive. */
1689 mtx_lock(&hio_recv_list_lock[ncomp]);
1690 while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
1691 pjdlog_debug(2, "remote_recv: No requests, waiting.");
1692 cv_wait(&hio_recv_list_cond[ncomp],
1693 &hio_recv_list_lock[ncomp]);
1695 mtx_unlock(&hio_recv_list_lock[ncomp]);
1699 rw_rlock(&hio_remote_lock[ncomp]);
1700 if (!ISCONNECTED(res, ncomp)) {
1701 rw_unlock(&hio_remote_lock[ncomp]);
1703 * Connection is dead, so move all pending requests to
1704 * the done queue (one-by-one).
1706 mtx_lock(&hio_recv_list_lock[ncomp]);
1707 hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
1708 PJDLOG_ASSERT(hio != NULL);
1709 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1711 mtx_unlock(&hio_recv_list_lock[ncomp]);
1714 if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
1715 pjdlog_errno(LOG_ERR,
1716 "Unable to receive reply header");
1717 rw_unlock(&hio_remote_lock[ncomp]);
1718 remote_close(res, ncomp);
1721 rw_unlock(&hio_remote_lock[ncomp]);
1722 seq = nv_get_uint64(nv, "seq");
1724 pjdlog_error("Header contains no 'seq' field.");
1728 memsyncack = nv_exists(nv, "received");
1729 mtx_lock(&hio_recv_list_lock[ncomp]);
1730 TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
1731 if (hio->hio_ggio.gctl_seq == seq) {
1732 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1737 mtx_unlock(&hio_recv_list_lock[ncomp]);
1739 pjdlog_error("Found no request matching received 'seq' field (%ju).",
1744 ggio = &hio->hio_ggio;
1745 error = nv_get_int16(nv, "error");
1747 /* Request failed on remote side. */
1748 hio->hio_errors[ncomp] = error;
1749 reqlog(LOG_WARNING, 0, ggio,
1750 "Remote request failed (%s): ", strerror(error));
1754 switch (ggio->gctl_cmd) {
1756 rw_rlock(&hio_remote_lock[ncomp]);
1757 if (!ISCONNECTED(res, ncomp)) {
1758 rw_unlock(&hio_remote_lock[ncomp]);
1762 if (hast_proto_recv_data(res, res->hr_remotein, nv,
1763 ggio->gctl_data, ggio->gctl_length) == -1) {
1764 hio->hio_errors[ncomp] = errno;
1765 pjdlog_errno(LOG_ERR,
1766 "Unable to receive reply data");
1767 rw_unlock(&hio_remote_lock[ncomp]);
1769 remote_close(res, ncomp);
1772 rw_unlock(&hio_remote_lock[ncomp]);
1779 PJDLOG_ABORT("invalid condition");
1781 hio->hio_errors[ncomp] = 0;
1784 if (hio->hio_replication != HAST_REPLICATION_MEMSYNC ||
1785 hio->hio_ggio.gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) {
1786 if (refcnt_release(&hio->hio_countdown) > 0)
1790 * Depending on hio_countdown value, requests finished
1791 * in the following order:
1793 * 0: local write, remote memsync, remote final
1795 * 0: remote memsync, local write, remote final
1797 * 1: local write, remote memsync, (remote final)
1799 * 1: remote memsync, remote final, (local write)
1801 * 2: remote memsync, (local write), (remote final)
1803 * 2: remote memsync, (remote final), (local write)
1805 switch (refcnt_release(&hio->hio_countdown)) {
1808 * Remote final reply arrived.
1810 PJDLOG_ASSERT(!memsyncack);
1815 * Local request already finished, so we
1816 * can complete the write.
1818 if (hio->hio_errors[0] == 0)
1819 write_complete(res, hio);
1821 * We still need to wait for final
1825 "remote_recv: (%p) Moving request back to the recv queue.",
1827 mtx_lock(&hio_recv_list_lock[ncomp]);
1828 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp],
1829 hio, hio_next[ncomp]);
1830 mtx_unlock(&hio_recv_list_lock[ncomp]);
1833 * Remote final reply arrived before
1834 * local write finished.
1835 * Nothing to do in such case.
1841 * We received remote memsync reply even before
1842 * local write finished.
1844 PJDLOG_ASSERT(memsyncack);
1847 "remote_recv: (%p) Moving request back to the recv queue.",
1849 mtx_lock(&hio_recv_list_lock[ncomp]);
1850 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio,
1852 mtx_unlock(&hio_recv_list_lock[ncomp]);
1855 PJDLOG_ABORT("Invalid hio_countdown.");
1858 if (ISSYNCREQ(hio)) {
1859 mtx_lock(&sync_lock);
1861 mtx_unlock(&sync_lock);
1862 cv_signal(&sync_cond);
1865 "remote_recv: (%p) Moving request to the done queue.",
1867 QUEUE_INSERT2(hio, done);
1875 * Thread sends answer to the kernel.
1878 ggate_send_thread(void *arg)
1880 struct hast_resource *res = arg;
1881 struct g_gate_ctl_io *ggio;
1883 unsigned int ii, ncomps;
1885 ncomps = HAST_NCOMPONENTS;
1888 pjdlog_debug(2, "ggate_send: Taking request.");
1889 QUEUE_TAKE2(hio, done);
1890 pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
1891 ggio = &hio->hio_ggio;
1892 for (ii = 0; ii < ncomps; ii++) {
1893 if (hio->hio_errors[ii] == 0) {
1895 * One successful request is enough to declare
1898 ggio->gctl_error = 0;
1904 * None of the requests were successful.
1905 * Use the error from local component except the
1906 * case when we did only remote request.
1908 if (ggio->gctl_cmd == BIO_READ &&
1909 res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1910 ggio->gctl_error = hio->hio_errors[1];
1912 ggio->gctl_error = hio->hio_errors[0];
1914 if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
1915 mtx_lock(&res->hr_amp_lock);
1916 if (activemap_write_complete(res->hr_amp,
1917 ggio->gctl_offset, ggio->gctl_length)) {
1918 res->hr_stat_activemap_update++;
1919 (void)hast_activemap_flush(res);
1921 mtx_unlock(&res->hr_amp_lock);
1923 if (ggio->gctl_cmd == BIO_WRITE) {
1925 * Unlock range we locked.
1927 mtx_lock(&range_lock);
1928 rangelock_del(range_regular, ggio->gctl_offset,
1930 if (range_sync_wait)
1931 cv_signal(&range_sync_cond);
1932 mtx_unlock(&range_lock);
1934 write_complete(res, hio);
1936 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
1937 primary_exit(EX_OSERR,
1938 "G_GATE_CMD_DONE failed");
1941 if (hio->hio_errors[0]) {
1942 switch (ggio->gctl_cmd) {
1944 res->hr_stat_read_error++;
1947 res->hr_stat_write_error++;
1950 res->hr_stat_delete_error++;
1953 res->hr_stat_flush_error++;
1958 "ggate_send: (%p) Moving request to the free queue.", hio);
1959 QUEUE_INSERT2(hio, free);
1966 * Thread synchronize local and remote components.
1969 sync_thread(void *arg __unused)
1971 struct hast_resource *res = arg;
1973 struct g_gate_ctl_io *ggio;
1974 struct timeval tstart, tend, tdiff;
1975 unsigned int ii, ncomp, ncomps;
1976 off_t offset, length, synced;
1977 bool dorewind, directreads;
1980 ncomps = HAST_NCOMPONENTS;
1984 directreads = false;
1987 mtx_lock(&sync_lock);
1988 if (offset >= 0 && !sync_inprogress) {
1989 gettimeofday(&tend, NULL);
1990 timersub(&tend, &tstart, &tdiff);
1991 pjdlog_info("Synchronization interrupted after %#.0T. "
1992 "%NB synchronized so far.", &tdiff,
1994 event_send(res, EVENT_SYNCINTR);
1996 while (!sync_inprogress) {
1999 cv_wait(&sync_cond, &sync_lock);
2001 mtx_unlock(&sync_lock);
2003 * Obtain offset at which we should synchronize.
2004 * Rewind synchronization if needed.
2006 mtx_lock(&res->hr_amp_lock);
2008 activemap_sync_rewind(res->hr_amp);
2009 offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
2010 if (syncext != -1) {
2012 * We synchronized entire syncext extent, we can mark
2015 if (activemap_extent_complete(res->hr_amp, syncext))
2016 (void)hast_activemap_flush(res);
2018 mtx_unlock(&res->hr_amp_lock);
2022 pjdlog_info("Nodes are in sync.");
2024 pjdlog_info("Synchronization started. %NB to go.",
2025 (intmax_t)(res->hr_extentsize *
2026 activemap_ndirty(res->hr_amp)));
2027 event_send(res, EVENT_SYNCSTART);
2028 gettimeofday(&tstart, NULL);
2033 pjdlog_debug(1, "Nothing to synchronize.");
2035 * Synchronization complete, make both localcnt and
2039 rw_rlock(&hio_remote_lock[ncomp]);
2040 if (ISCONNECTED(res, ncomp)) {
2044 gettimeofday(&tend, NULL);
2045 timersub(&tend, &tstart, &tdiff);
2046 bps = (int64_t)((double)synced /
2047 ((double)tdiff.tv_sec +
2048 (double)tdiff.tv_usec / 1000000));
2049 pjdlog_info("Synchronization complete. "
2050 "%NB synchronized in %#.0lT (%NB/sec).",
2051 (intmax_t)synced, &tdiff,
2053 event_send(res, EVENT_SYNCDONE);
2055 mtx_lock(&metadata_lock);
2056 if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
2058 res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
2059 res->hr_primary_localcnt =
2060 res->hr_secondary_remotecnt;
2061 res->hr_primary_remotecnt =
2062 res->hr_secondary_localcnt;
2064 "Setting localcnt to %ju and remotecnt to %ju.",
2065 (uintmax_t)res->hr_primary_localcnt,
2066 (uintmax_t)res->hr_primary_remotecnt);
2067 (void)metadata_write(res);
2068 mtx_unlock(&metadata_lock);
2070 rw_unlock(&hio_remote_lock[ncomp]);
2072 directreads = false;
2073 enable_direct_reads(res);
2077 pjdlog_debug(2, "sync: Taking free request.");
2078 QUEUE_TAKE2(hio, free);
2079 pjdlog_debug(2, "sync: (%p) Got free request.", hio);
2081 * Lock the range we are going to synchronize. We don't want
2082 * race where someone writes between our read and write.
2085 mtx_lock(&range_lock);
2086 if (rangelock_islocked(range_regular, offset, length)) {
2088 "sync: Range offset=%jd length=%jd locked.",
2089 (intmax_t)offset, (intmax_t)length);
2090 range_sync_wait = true;
2091 cv_wait(&range_sync_cond, &range_lock);
2092 range_sync_wait = false;
2093 mtx_unlock(&range_lock);
2096 if (rangelock_add(range_sync, offset, length) == -1) {
2097 mtx_unlock(&range_lock);
2099 "sync: Range offset=%jd length=%jd is already locked, waiting.",
2100 (intmax_t)offset, (intmax_t)length);
2104 mtx_unlock(&range_lock);
2108 * First read the data from synchronization source.
2111 ggio = &hio->hio_ggio;
2112 ggio->gctl_cmd = BIO_READ;
2113 ggio->gctl_offset = offset;
2114 ggio->gctl_length = length;
2115 ggio->gctl_error = 0;
2116 hio->hio_done = false;
2117 hio->hio_replication = res->hr_replication;
2118 for (ii = 0; ii < ncomps; ii++)
2119 hio->hio_errors[ii] = EINVAL;
2120 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2122 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2124 mtx_lock(&metadata_lock);
2125 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2127 * This range is up-to-date on local component,
2128 * so handle request locally.
2130 /* Local component is 0 for now. */
2132 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2133 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2135 * This range is out-of-date on local component,
2136 * so send request to the remote node.
2138 /* Remote component is 1 for now. */
2141 mtx_unlock(&metadata_lock);
2142 hio->hio_countdown = 1;
2143 QUEUE_INSERT1(hio, send, ncomp);
2146 * Let's wait for READ to finish.
2148 mtx_lock(&sync_lock);
2149 while (!ISSYNCREQDONE(hio))
2150 cv_wait(&sync_cond, &sync_lock);
2151 mtx_unlock(&sync_lock);
2153 if (hio->hio_errors[ncomp] != 0) {
2154 pjdlog_error("Unable to read synchronization data: %s.",
2155 strerror(hio->hio_errors[ncomp]));
2160 * We read the data from synchronization source, now write it
2161 * to synchronization target.
2164 ggio->gctl_cmd = BIO_WRITE;
2165 for (ii = 0; ii < ncomps; ii++)
2166 hio->hio_errors[ii] = EINVAL;
2167 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2169 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2171 mtx_lock(&metadata_lock);
2172 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2174 * This range is up-to-date on local component,
2175 * so we update remote component.
2177 /* Remote component is 1 for now. */
2179 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2180 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2182 * This range is out-of-date on local component,
2185 /* Local component is 0 for now. */
2188 mtx_unlock(&metadata_lock);
2190 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2192 hio->hio_countdown = 1;
2193 QUEUE_INSERT1(hio, send, ncomp);
2196 * Let's wait for WRITE to finish.
2198 mtx_lock(&sync_lock);
2199 while (!ISSYNCREQDONE(hio))
2200 cv_wait(&sync_cond, &sync_lock);
2201 mtx_unlock(&sync_lock);
2203 if (hio->hio_errors[ncomp] != 0) {
2204 pjdlog_error("Unable to write synchronization data: %s.",
2205 strerror(hio->hio_errors[ncomp]));
2211 mtx_lock(&range_lock);
2212 rangelock_del(range_sync, offset, length);
2213 if (range_regular_wait)
2214 cv_signal(&range_regular_cond);
2215 mtx_unlock(&range_lock);
2216 pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
2218 QUEUE_INSERT2(hio, free);
2225 primary_config_reload(struct hast_resource *res, struct nv *nv)
2227 unsigned int ii, ncomps;
2231 pjdlog_info("Reloading configuration...");
2233 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
2234 PJDLOG_ASSERT(gres == res);
2235 nv_assert(nv, "remoteaddr");
2236 nv_assert(nv, "sourceaddr");
2237 nv_assert(nv, "replication");
2238 nv_assert(nv, "checksum");
2239 nv_assert(nv, "compression");
2240 nv_assert(nv, "timeout");
2241 nv_assert(nv, "exec");
2242 nv_assert(nv, "metaflush");
2244 ncomps = HAST_NCOMPONENTS;
2246 #define MODIFIED_REMOTEADDR 0x01
2247 #define MODIFIED_SOURCEADDR 0x02
2248 #define MODIFIED_REPLICATION 0x04
2249 #define MODIFIED_CHECKSUM 0x08
2250 #define MODIFIED_COMPRESSION 0x10
2251 #define MODIFIED_TIMEOUT 0x20
2252 #define MODIFIED_EXEC 0x40
2253 #define MODIFIED_METAFLUSH 0x80
2256 vstr = nv_get_string(nv, "remoteaddr");
2257 if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
2259 * Don't copy res->hr_remoteaddr to gres just yet.
2260 * We want remote_close() to log disconnect from the old
2261 * addresses, not from the new ones.
2263 modified |= MODIFIED_REMOTEADDR;
2265 vstr = nv_get_string(nv, "sourceaddr");
2266 if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
2267 strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
2268 modified |= MODIFIED_SOURCEADDR;
2270 vint = nv_get_int32(nv, "replication");
2271 if (gres->hr_replication != vint) {
2272 gres->hr_replication = vint;
2273 modified |= MODIFIED_REPLICATION;
2275 vint = nv_get_int32(nv, "checksum");
2276 if (gres->hr_checksum != vint) {
2277 gres->hr_checksum = vint;
2278 modified |= MODIFIED_CHECKSUM;
2280 vint = nv_get_int32(nv, "compression");
2281 if (gres->hr_compression != vint) {
2282 gres->hr_compression = vint;
2283 modified |= MODIFIED_COMPRESSION;
2285 vint = nv_get_int32(nv, "timeout");
2286 if (gres->hr_timeout != vint) {
2287 gres->hr_timeout = vint;
2288 modified |= MODIFIED_TIMEOUT;
2290 vstr = nv_get_string(nv, "exec");
2291 if (strcmp(gres->hr_exec, vstr) != 0) {
2292 strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
2293 modified |= MODIFIED_EXEC;
2295 vint = nv_get_int32(nv, "metaflush");
2296 if (gres->hr_metaflush != vint) {
2297 gres->hr_metaflush = vint;
2298 modified |= MODIFIED_METAFLUSH;
2302 * Change timeout for connected sockets.
2303 * Don't bother if we need to reconnect.
2305 if ((modified & MODIFIED_TIMEOUT) != 0 &&
2306 (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
2307 for (ii = 0; ii < ncomps; ii++) {
2310 rw_rlock(&hio_remote_lock[ii]);
2311 if (!ISCONNECTED(gres, ii)) {
2312 rw_unlock(&hio_remote_lock[ii]);
2315 rw_unlock(&hio_remote_lock[ii]);
2316 if (proto_timeout(gres->hr_remotein,
2317 gres->hr_timeout) == -1) {
2318 pjdlog_errno(LOG_WARNING,
2319 "Unable to set connection timeout");
2321 if (proto_timeout(gres->hr_remoteout,
2322 gres->hr_timeout) == -1) {
2323 pjdlog_errno(LOG_WARNING,
2324 "Unable to set connection timeout");
2328 if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
2329 for (ii = 0; ii < ncomps; ii++) {
2332 remote_close(gres, ii);
2334 if (modified & MODIFIED_REMOTEADDR) {
2335 vstr = nv_get_string(nv, "remoteaddr");
2336 strlcpy(gres->hr_remoteaddr, vstr,
2337 sizeof(gres->hr_remoteaddr));
2340 #undef MODIFIED_REMOTEADDR
2341 #undef MODIFIED_SOURCEADDR
2342 #undef MODIFIED_REPLICATION
2343 #undef MODIFIED_CHECKSUM
2344 #undef MODIFIED_COMPRESSION
2345 #undef MODIFIED_TIMEOUT
2346 #undef MODIFIED_EXEC
2347 #undef MODIFIED_METAFLUSH
2349 pjdlog_info("Configuration reloaded successfully.");
2353 guard_one(struct hast_resource *res, unsigned int ncomp)
2355 struct proto_conn *in, *out;
2357 if (!ISREMOTE(ncomp))
2360 rw_rlock(&hio_remote_lock[ncomp]);
2362 if (!real_remote(res)) {
2363 rw_unlock(&hio_remote_lock[ncomp]);
2367 if (ISCONNECTED(res, ncomp)) {
2368 PJDLOG_ASSERT(res->hr_remotein != NULL);
2369 PJDLOG_ASSERT(res->hr_remoteout != NULL);
2370 rw_unlock(&hio_remote_lock[ncomp]);
2371 pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2372 res->hr_remoteaddr);
2376 PJDLOG_ASSERT(res->hr_remotein == NULL);
2377 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2379 * Upgrade the lock. It doesn't have to be atomic as no other thread
2380 * can change connection status from disconnected to connected.
2382 rw_unlock(&hio_remote_lock[ncomp]);
2383 pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2384 res->hr_remoteaddr);
2386 if (init_remote(res, &in, &out) == 0) {
2387 rw_wlock(&hio_remote_lock[ncomp]);
2388 PJDLOG_ASSERT(res->hr_remotein == NULL);
2389 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2390 PJDLOG_ASSERT(in != NULL && out != NULL);
2391 res->hr_remotein = in;
2392 res->hr_remoteout = out;
2393 rw_unlock(&hio_remote_lock[ncomp]);
2394 pjdlog_info("Successfully reconnected to %s.",
2395 res->hr_remoteaddr);
2398 /* Both connections should be NULL. */
2399 PJDLOG_ASSERT(res->hr_remotein == NULL);
2400 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2401 PJDLOG_ASSERT(in == NULL && out == NULL);
2402 pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2403 res->hr_remoteaddr);
2408 * Thread guards remote connections and reconnects when needed, handles
2412 guard_thread(void *arg)
2414 struct hast_resource *res = arg;
2415 unsigned int ii, ncomps;
2416 struct timespec timeout;
2417 time_t lastcheck, now;
2421 ncomps = HAST_NCOMPONENTS;
2422 lastcheck = time(NULL);
2424 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
2425 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
2426 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
2428 timeout.tv_sec = HAST_KEEPALIVE;
2429 timeout.tv_nsec = 0;
2436 sigexit_received = true;
2437 primary_exitx(EX_OK,
2438 "Termination signal received, exiting.");
2445 * Don't check connections until we fully started,
2446 * as we may still be looping, waiting for remote node
2447 * to switch from primary to secondary.
2450 pjdlog_debug(2, "remote_guard: Checking connections.");
2452 if (lastcheck + HAST_KEEPALIVE <= now) {
2453 for (ii = 0; ii < ncomps; ii++)
2458 signo = sigtimedwait(&mask, NULL, &timeout);