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 refcnt_t 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 refcnt_init(&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 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC &&
1304 ggio->gctl_cmd == BIO_WRITE) {
1305 /* Each remote request needs two responses in memsync. */
1306 refcnt_init(&hio->hio_countdown, ncomps + 1);
1308 refcnt_init(&hio->hio_countdown, ncomps);
1310 for (ii = ncomp; ii < ncomps; ii++)
1311 QUEUE_INSERT1(hio, send, ii);
1318 * Thread reads from or writes to local component.
1319 * If local read fails, it redirects it to remote_send thread.
1322 local_send_thread(void *arg)
1324 struct hast_resource *res = arg;
1325 struct g_gate_ctl_io *ggio;
1327 unsigned int ncomp, rncomp;
1330 /* Local component is 0 for now. */
1332 /* Remote component is 1 for now. */
1336 pjdlog_debug(2, "local_send: Taking request.");
1337 QUEUE_TAKE1(hio, send, ncomp, 0);
1338 pjdlog_debug(2, "local_send: (%p) Got request.", hio);
1339 ggio = &hio->hio_ggio;
1340 switch (ggio->gctl_cmd) {
1342 ret = pread(res->hr_localfd, ggio->gctl_data,
1344 ggio->gctl_offset + res->hr_localoff);
1345 if (ret == ggio->gctl_length)
1346 hio->hio_errors[ncomp] = 0;
1347 else if (!ISSYNCREQ(hio)) {
1349 * If READ failed, try to read from remote node.
1352 reqlog(LOG_WARNING, 0, ggio,
1353 "Local request failed (%s), trying remote node. ",
1355 } else if (ret != ggio->gctl_length) {
1356 reqlog(LOG_WARNING, 0, ggio,
1357 "Local request failed (%zd != %jd), trying remote node. ",
1358 ret, (intmax_t)ggio->gctl_length);
1360 QUEUE_INSERT1(hio, send, rncomp);
1365 ret = pwrite(res->hr_localfd, ggio->gctl_data,
1367 ggio->gctl_offset + res->hr_localoff);
1369 hio->hio_errors[ncomp] = errno;
1370 reqlog(LOG_WARNING, 0, ggio,
1371 "Local request failed (%s): ",
1373 } else if (ret != ggio->gctl_length) {
1374 hio->hio_errors[ncomp] = EIO;
1375 reqlog(LOG_WARNING, 0, ggio,
1376 "Local request failed (%zd != %jd): ",
1377 ret, (intmax_t)ggio->gctl_length);
1379 hio->hio_errors[ncomp] = 0;
1380 if (hio->hio_replication ==
1381 HAST_REPLICATION_ASYNC) {
1382 ggio->gctl_error = 0;
1383 write_complete(res, hio);
1388 ret = g_delete(res->hr_localfd,
1389 ggio->gctl_offset + res->hr_localoff,
1392 hio->hio_errors[ncomp] = errno;
1393 reqlog(LOG_WARNING, 0, ggio,
1394 "Local request failed (%s): ",
1397 hio->hio_errors[ncomp] = 0;
1401 if (!res->hr_localflush) {
1406 ret = g_flush(res->hr_localfd);
1408 if (errno == EOPNOTSUPP)
1409 res->hr_localflush = false;
1410 hio->hio_errors[ncomp] = errno;
1411 reqlog(LOG_WARNING, 0, ggio,
1412 "Local request failed (%s): ",
1415 hio->hio_errors[ncomp] = 0;
1420 if (hio->hio_replication != HAST_REPLICATION_MEMSYNC ||
1421 ggio->gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) {
1422 if (refcnt_release(&hio->hio_countdown) > 0)
1426 * Depending on hio_countdown value, requests finished
1427 * in the following order:
1428 * 0: remote memsync, remote final, local write
1429 * 1: remote memsync, local write, (remote final)
1430 * 2: local write, (remote memsync), (remote final)
1432 switch (refcnt_release(&hio->hio_countdown)) {
1435 * Local write finished as last.
1440 * Local write finished after remote memsync
1441 * reply arrvied. We can complete the write now.
1443 if (hio->hio_errors[0] == 0)
1444 write_complete(res, hio);
1448 * Local write finished as first.
1452 PJDLOG_ABORT("Invalid hio_countdown.");
1455 if (ISSYNCREQ(hio)) {
1456 mtx_lock(&sync_lock);
1458 mtx_unlock(&sync_lock);
1459 cv_signal(&sync_cond);
1462 "local_send: (%p) Moving request to the done queue.",
1464 QUEUE_INSERT2(hio, done);
1472 keepalive_send(struct hast_resource *res, unsigned int ncomp)
1476 rw_rlock(&hio_remote_lock[ncomp]);
1478 if (!ISCONNECTED(res, ncomp)) {
1479 rw_unlock(&hio_remote_lock[ncomp]);
1483 PJDLOG_ASSERT(res->hr_remotein != NULL);
1484 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1487 nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1488 if (nv_error(nv) != 0) {
1489 rw_unlock(&hio_remote_lock[ncomp]);
1492 "keepalive_send: Unable to prepare header to send.");
1495 if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
1496 rw_unlock(&hio_remote_lock[ncomp]);
1497 pjdlog_common(LOG_DEBUG, 1, errno,
1498 "keepalive_send: Unable to send request");
1500 remote_close(res, ncomp);
1504 rw_unlock(&hio_remote_lock[ncomp]);
1506 pjdlog_debug(2, "keepalive_send: Request sent.");
1510 * Thread sends request to secondary node.
1513 remote_send_thread(void *arg)
1515 struct hast_resource *res = arg;
1516 struct g_gate_ctl_io *ggio;
1517 time_t lastcheck, now;
1522 uint64_t offset, length;
1526 /* Remote component is 1 for now. */
1528 lastcheck = time(NULL);
1531 pjdlog_debug(2, "remote_send: Taking request.");
1532 QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1535 if (lastcheck + HAST_KEEPALIVE <= now) {
1536 keepalive_send(res, ncomp);
1541 pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
1542 ggio = &hio->hio_ggio;
1543 switch (ggio->gctl_cmd) {
1547 offset = ggio->gctl_offset;
1548 length = ggio->gctl_length;
1552 data = ggio->gctl_data;
1553 offset = ggio->gctl_offset;
1554 length = ggio->gctl_length;
1559 offset = ggio->gctl_offset;
1560 length = ggio->gctl_length;
1569 PJDLOG_ABORT("invalid condition");
1572 nv_add_uint8(nv, cmd, "cmd");
1573 nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
1574 nv_add_uint64(nv, offset, "offset");
1575 nv_add_uint64(nv, length, "length");
1576 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC &&
1577 ggio->gctl_cmd == BIO_WRITE && !ISSYNCREQ(hio)) {
1578 nv_add_uint8(nv, 1, "memsync");
1580 if (nv_error(nv) != 0) {
1581 hio->hio_errors[ncomp] = nv_error(nv);
1583 "remote_send: (%p) Unable to prepare header to send.",
1585 reqlog(LOG_ERR, 0, ggio,
1586 "Unable to prepare header to send (%s): ",
1587 strerror(nv_error(nv)));
1588 /* Move failed request immediately to the done queue. */
1592 * Protect connection from disappearing.
1594 rw_rlock(&hio_remote_lock[ncomp]);
1595 if (!ISCONNECTED(res, ncomp)) {
1596 rw_unlock(&hio_remote_lock[ncomp]);
1597 hio->hio_errors[ncomp] = ENOTCONN;
1601 * Move the request to recv queue before sending it, because
1602 * in different order we can get reply before we move request
1606 "remote_send: (%p) Moving request to the recv queue.",
1608 mtx_lock(&hio_recv_list_lock[ncomp]);
1609 wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
1610 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
1611 mtx_unlock(&hio_recv_list_lock[ncomp]);
1612 if (hast_proto_send(res, res->hr_remoteout, nv, data,
1613 data != NULL ? length : 0) == -1) {
1614 hio->hio_errors[ncomp] = errno;
1615 rw_unlock(&hio_remote_lock[ncomp]);
1617 "remote_send: (%p) Unable to send request.", hio);
1618 reqlog(LOG_ERR, 0, ggio,
1619 "Unable to send request (%s): ",
1620 strerror(hio->hio_errors[ncomp]));
1621 remote_close(res, ncomp);
1623 * Take request back from the receive queue and move
1624 * it immediately to the done queue.
1626 mtx_lock(&hio_recv_list_lock[ncomp]);
1627 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1629 mtx_unlock(&hio_recv_list_lock[ncomp]);
1632 rw_unlock(&hio_remote_lock[ncomp]);
1635 cv_signal(&hio_recv_list_cond[ncomp]);
1639 if (ISSYNCREQ(hio)) {
1640 if (refcnt_release(&hio->hio_countdown) > 0)
1642 mtx_lock(&sync_lock);
1644 mtx_unlock(&sync_lock);
1645 cv_signal(&sync_cond);
1648 if (ggio->gctl_cmd == BIO_WRITE) {
1649 mtx_lock(&res->hr_amp_lock);
1650 if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
1651 ggio->gctl_length)) {
1652 (void)hast_activemap_flush(res);
1654 mtx_unlock(&res->hr_amp_lock);
1655 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC)
1656 (void)refcnt_release(&hio->hio_countdown);
1658 if (refcnt_release(&hio->hio_countdown) > 0)
1661 "remote_send: (%p) Moving request to the done queue.",
1663 QUEUE_INSERT2(hio, done);
1670 * Thread receives answer from secondary node and passes it to ggate_send
1674 remote_recv_thread(void *arg)
1676 struct hast_resource *res = arg;
1677 struct g_gate_ctl_io *ggio;
1685 /* Remote component is 1 for now. */
1689 /* Wait until there is anything to receive. */
1690 mtx_lock(&hio_recv_list_lock[ncomp]);
1691 while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
1692 pjdlog_debug(2, "remote_recv: No requests, waiting.");
1693 cv_wait(&hio_recv_list_cond[ncomp],
1694 &hio_recv_list_lock[ncomp]);
1696 mtx_unlock(&hio_recv_list_lock[ncomp]);
1700 rw_rlock(&hio_remote_lock[ncomp]);
1701 if (!ISCONNECTED(res, ncomp)) {
1702 rw_unlock(&hio_remote_lock[ncomp]);
1704 * Connection is dead, so move all pending requests to
1705 * the done queue (one-by-one).
1707 mtx_lock(&hio_recv_list_lock[ncomp]);
1708 hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
1709 PJDLOG_ASSERT(hio != NULL);
1710 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1712 mtx_unlock(&hio_recv_list_lock[ncomp]);
1715 if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
1716 pjdlog_errno(LOG_ERR,
1717 "Unable to receive reply header");
1718 rw_unlock(&hio_remote_lock[ncomp]);
1719 remote_close(res, ncomp);
1722 rw_unlock(&hio_remote_lock[ncomp]);
1723 seq = nv_get_uint64(nv, "seq");
1725 pjdlog_error("Header contains no 'seq' field.");
1729 memsyncack = nv_exists(nv, "received");
1730 mtx_lock(&hio_recv_list_lock[ncomp]);
1731 TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
1732 if (hio->hio_ggio.gctl_seq == seq) {
1733 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1738 mtx_unlock(&hio_recv_list_lock[ncomp]);
1740 pjdlog_error("Found no request matching received 'seq' field (%ju).",
1745 ggio = &hio->hio_ggio;
1746 error = nv_get_int16(nv, "error");
1748 /* Request failed on remote side. */
1749 hio->hio_errors[ncomp] = error;
1750 reqlog(LOG_WARNING, 0, ggio,
1751 "Remote request failed (%s): ", strerror(error));
1755 switch (ggio->gctl_cmd) {
1757 rw_rlock(&hio_remote_lock[ncomp]);
1758 if (!ISCONNECTED(res, ncomp)) {
1759 rw_unlock(&hio_remote_lock[ncomp]);
1763 if (hast_proto_recv_data(res, res->hr_remotein, nv,
1764 ggio->gctl_data, ggio->gctl_length) == -1) {
1765 hio->hio_errors[ncomp] = errno;
1766 pjdlog_errno(LOG_ERR,
1767 "Unable to receive reply data");
1768 rw_unlock(&hio_remote_lock[ncomp]);
1770 remote_close(res, ncomp);
1773 rw_unlock(&hio_remote_lock[ncomp]);
1780 PJDLOG_ABORT("invalid condition");
1782 hio->hio_errors[ncomp] = 0;
1785 if (hio->hio_replication != HAST_REPLICATION_MEMSYNC ||
1786 hio->hio_ggio.gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) {
1787 if (refcnt_release(&hio->hio_countdown) > 0)
1791 * Depending on hio_countdown value, requests finished
1792 * in the following order:
1794 * 0: local write, remote memsync, remote final
1796 * 0: remote memsync, local write, remote final
1798 * 1: local write, remote memsync, (remote final)
1800 * 1: remote memsync, remote final, (local write)
1802 * 2: remote memsync, (local write), (remote final)
1804 * 2: remote memsync, (remote final), (local write)
1806 switch (refcnt_release(&hio->hio_countdown)) {
1809 * Remote final reply arrived.
1811 PJDLOG_ASSERT(!memsyncack);
1816 * Local request already finished, so we
1817 * can complete the write.
1819 if (hio->hio_errors[0] == 0)
1820 write_complete(res, hio);
1822 * We still need to wait for final
1826 "remote_recv: (%p) Moving request back to the recv queue.",
1828 mtx_lock(&hio_recv_list_lock[ncomp]);
1829 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp],
1830 hio, hio_next[ncomp]);
1831 mtx_unlock(&hio_recv_list_lock[ncomp]);
1834 * Remote final reply arrived before
1835 * local write finished.
1836 * Nothing to do in such case.
1842 * We received remote memsync reply even before
1843 * local write finished.
1845 PJDLOG_ASSERT(memsyncack);
1848 "remote_recv: (%p) Moving request back to the recv queue.",
1850 mtx_lock(&hio_recv_list_lock[ncomp]);
1851 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio,
1853 mtx_unlock(&hio_recv_list_lock[ncomp]);
1856 PJDLOG_ABORT("Invalid hio_countdown.");
1859 if (ISSYNCREQ(hio)) {
1860 mtx_lock(&sync_lock);
1862 mtx_unlock(&sync_lock);
1863 cv_signal(&sync_cond);
1866 "remote_recv: (%p) Moving request to the done queue.",
1868 QUEUE_INSERT2(hio, done);
1876 * Thread sends answer to the kernel.
1879 ggate_send_thread(void *arg)
1881 struct hast_resource *res = arg;
1882 struct g_gate_ctl_io *ggio;
1884 unsigned int ii, ncomps;
1886 ncomps = HAST_NCOMPONENTS;
1889 pjdlog_debug(2, "ggate_send: Taking request.");
1890 QUEUE_TAKE2(hio, done);
1891 pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
1892 ggio = &hio->hio_ggio;
1893 for (ii = 0; ii < ncomps; ii++) {
1894 if (hio->hio_errors[ii] == 0) {
1896 * One successful request is enough to declare
1899 ggio->gctl_error = 0;
1905 * None of the requests were successful.
1906 * Use the error from local component except the
1907 * case when we did only remote request.
1909 if (ggio->gctl_cmd == BIO_READ &&
1910 res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1911 ggio->gctl_error = hio->hio_errors[1];
1913 ggio->gctl_error = hio->hio_errors[0];
1915 if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
1916 mtx_lock(&res->hr_amp_lock);
1917 if (activemap_write_complete(res->hr_amp,
1918 ggio->gctl_offset, ggio->gctl_length)) {
1919 res->hr_stat_activemap_update++;
1920 (void)hast_activemap_flush(res);
1922 mtx_unlock(&res->hr_amp_lock);
1924 if (ggio->gctl_cmd == BIO_WRITE) {
1926 * Unlock range we locked.
1928 mtx_lock(&range_lock);
1929 rangelock_del(range_regular, ggio->gctl_offset,
1931 if (range_sync_wait)
1932 cv_signal(&range_sync_cond);
1933 mtx_unlock(&range_lock);
1935 write_complete(res, hio);
1937 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
1938 primary_exit(EX_OSERR,
1939 "G_GATE_CMD_DONE failed");
1942 if (hio->hio_errors[0]) {
1943 switch (ggio->gctl_cmd) {
1945 res->hr_stat_read_error++;
1948 res->hr_stat_write_error++;
1951 res->hr_stat_delete_error++;
1954 res->hr_stat_flush_error++;
1959 "ggate_send: (%p) Moving request to the free queue.", hio);
1960 QUEUE_INSERT2(hio, free);
1967 * Thread synchronize local and remote components.
1970 sync_thread(void *arg __unused)
1972 struct hast_resource *res = arg;
1974 struct g_gate_ctl_io *ggio;
1975 struct timeval tstart, tend, tdiff;
1976 unsigned int ii, ncomp, ncomps;
1977 off_t offset, length, synced;
1978 bool dorewind, directreads;
1981 ncomps = HAST_NCOMPONENTS;
1985 directreads = false;
1988 mtx_lock(&sync_lock);
1989 if (offset >= 0 && !sync_inprogress) {
1990 gettimeofday(&tend, NULL);
1991 timersub(&tend, &tstart, &tdiff);
1992 pjdlog_info("Synchronization interrupted after %#.0T. "
1993 "%NB synchronized so far.", &tdiff,
1995 event_send(res, EVENT_SYNCINTR);
1997 while (!sync_inprogress) {
2000 cv_wait(&sync_cond, &sync_lock);
2002 mtx_unlock(&sync_lock);
2004 * Obtain offset at which we should synchronize.
2005 * Rewind synchronization if needed.
2007 mtx_lock(&res->hr_amp_lock);
2009 activemap_sync_rewind(res->hr_amp);
2010 offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
2011 if (syncext != -1) {
2013 * We synchronized entire syncext extent, we can mark
2016 if (activemap_extent_complete(res->hr_amp, syncext))
2017 (void)hast_activemap_flush(res);
2019 mtx_unlock(&res->hr_amp_lock);
2023 pjdlog_info("Nodes are in sync.");
2025 pjdlog_info("Synchronization started. %NB to go.",
2026 (intmax_t)(res->hr_extentsize *
2027 activemap_ndirty(res->hr_amp)));
2028 event_send(res, EVENT_SYNCSTART);
2029 gettimeofday(&tstart, NULL);
2034 pjdlog_debug(1, "Nothing to synchronize.");
2036 * Synchronization complete, make both localcnt and
2040 rw_rlock(&hio_remote_lock[ncomp]);
2041 if (ISCONNECTED(res, ncomp)) {
2045 gettimeofday(&tend, NULL);
2046 timersub(&tend, &tstart, &tdiff);
2047 bps = (int64_t)((double)synced /
2048 ((double)tdiff.tv_sec +
2049 (double)tdiff.tv_usec / 1000000));
2050 pjdlog_info("Synchronization complete. "
2051 "%NB synchronized in %#.0lT (%NB/sec).",
2052 (intmax_t)synced, &tdiff,
2054 event_send(res, EVENT_SYNCDONE);
2056 mtx_lock(&metadata_lock);
2057 if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
2059 res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
2060 res->hr_primary_localcnt =
2061 res->hr_secondary_remotecnt;
2062 res->hr_primary_remotecnt =
2063 res->hr_secondary_localcnt;
2065 "Setting localcnt to %ju and remotecnt to %ju.",
2066 (uintmax_t)res->hr_primary_localcnt,
2067 (uintmax_t)res->hr_primary_remotecnt);
2068 (void)metadata_write(res);
2069 mtx_unlock(&metadata_lock);
2071 rw_unlock(&hio_remote_lock[ncomp]);
2073 directreads = false;
2074 enable_direct_reads(res);
2078 pjdlog_debug(2, "sync: Taking free request.");
2079 QUEUE_TAKE2(hio, free);
2080 pjdlog_debug(2, "sync: (%p) Got free request.", hio);
2082 * Lock the range we are going to synchronize. We don't want
2083 * race where someone writes between our read and write.
2086 mtx_lock(&range_lock);
2087 if (rangelock_islocked(range_regular, offset, length)) {
2089 "sync: Range offset=%jd length=%jd locked.",
2090 (intmax_t)offset, (intmax_t)length);
2091 range_sync_wait = true;
2092 cv_wait(&range_sync_cond, &range_lock);
2093 range_sync_wait = false;
2094 mtx_unlock(&range_lock);
2097 if (rangelock_add(range_sync, offset, length) == -1) {
2098 mtx_unlock(&range_lock);
2100 "sync: Range offset=%jd length=%jd is already locked, waiting.",
2101 (intmax_t)offset, (intmax_t)length);
2105 mtx_unlock(&range_lock);
2109 * First read the data from synchronization source.
2112 ggio = &hio->hio_ggio;
2113 ggio->gctl_cmd = BIO_READ;
2114 ggio->gctl_offset = offset;
2115 ggio->gctl_length = length;
2116 ggio->gctl_error = 0;
2117 hio->hio_done = false;
2118 hio->hio_replication = res->hr_replication;
2119 for (ii = 0; ii < ncomps; ii++)
2120 hio->hio_errors[ii] = EINVAL;
2121 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2123 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2125 mtx_lock(&metadata_lock);
2126 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2128 * This range is up-to-date on local component,
2129 * so handle request locally.
2131 /* Local component is 0 for now. */
2133 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2134 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2136 * This range is out-of-date on local component,
2137 * so send request to the remote node.
2139 /* Remote component is 1 for now. */
2142 mtx_unlock(&metadata_lock);
2143 refcnt_init(&hio->hio_countdown, 1);
2144 QUEUE_INSERT1(hio, send, ncomp);
2147 * Let's wait for READ to finish.
2149 mtx_lock(&sync_lock);
2150 while (!ISSYNCREQDONE(hio))
2151 cv_wait(&sync_cond, &sync_lock);
2152 mtx_unlock(&sync_lock);
2154 if (hio->hio_errors[ncomp] != 0) {
2155 pjdlog_error("Unable to read synchronization data: %s.",
2156 strerror(hio->hio_errors[ncomp]));
2161 * We read the data from synchronization source, now write it
2162 * to synchronization target.
2165 ggio->gctl_cmd = BIO_WRITE;
2166 for (ii = 0; ii < ncomps; ii++)
2167 hio->hio_errors[ii] = EINVAL;
2168 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2170 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2172 mtx_lock(&metadata_lock);
2173 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2175 * This range is up-to-date on local component,
2176 * so we update remote component.
2178 /* Remote component is 1 for now. */
2180 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2181 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2183 * This range is out-of-date on local component,
2186 /* Local component is 0 for now. */
2189 mtx_unlock(&metadata_lock);
2191 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2193 refcnt_init(&hio->hio_countdown, 1);
2194 QUEUE_INSERT1(hio, send, ncomp);
2197 * Let's wait for WRITE to finish.
2199 mtx_lock(&sync_lock);
2200 while (!ISSYNCREQDONE(hio))
2201 cv_wait(&sync_cond, &sync_lock);
2202 mtx_unlock(&sync_lock);
2204 if (hio->hio_errors[ncomp] != 0) {
2205 pjdlog_error("Unable to write synchronization data: %s.",
2206 strerror(hio->hio_errors[ncomp]));
2212 mtx_lock(&range_lock);
2213 rangelock_del(range_sync, offset, length);
2214 if (range_regular_wait)
2215 cv_signal(&range_regular_cond);
2216 mtx_unlock(&range_lock);
2217 pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
2219 QUEUE_INSERT2(hio, free);
2226 primary_config_reload(struct hast_resource *res, struct nv *nv)
2228 unsigned int ii, ncomps;
2232 pjdlog_info("Reloading configuration...");
2234 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
2235 PJDLOG_ASSERT(gres == res);
2236 nv_assert(nv, "remoteaddr");
2237 nv_assert(nv, "sourceaddr");
2238 nv_assert(nv, "replication");
2239 nv_assert(nv, "checksum");
2240 nv_assert(nv, "compression");
2241 nv_assert(nv, "timeout");
2242 nv_assert(nv, "exec");
2243 nv_assert(nv, "metaflush");
2245 ncomps = HAST_NCOMPONENTS;
2247 #define MODIFIED_REMOTEADDR 0x01
2248 #define MODIFIED_SOURCEADDR 0x02
2249 #define MODIFIED_REPLICATION 0x04
2250 #define MODIFIED_CHECKSUM 0x08
2251 #define MODIFIED_COMPRESSION 0x10
2252 #define MODIFIED_TIMEOUT 0x20
2253 #define MODIFIED_EXEC 0x40
2254 #define MODIFIED_METAFLUSH 0x80
2257 vstr = nv_get_string(nv, "remoteaddr");
2258 if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
2260 * Don't copy res->hr_remoteaddr to gres just yet.
2261 * We want remote_close() to log disconnect from the old
2262 * addresses, not from the new ones.
2264 modified |= MODIFIED_REMOTEADDR;
2266 vstr = nv_get_string(nv, "sourceaddr");
2267 if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
2268 strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
2269 modified |= MODIFIED_SOURCEADDR;
2271 vint = nv_get_int32(nv, "replication");
2272 if (gres->hr_replication != vint) {
2273 gres->hr_replication = vint;
2274 modified |= MODIFIED_REPLICATION;
2276 vint = nv_get_int32(nv, "checksum");
2277 if (gres->hr_checksum != vint) {
2278 gres->hr_checksum = vint;
2279 modified |= MODIFIED_CHECKSUM;
2281 vint = nv_get_int32(nv, "compression");
2282 if (gres->hr_compression != vint) {
2283 gres->hr_compression = vint;
2284 modified |= MODIFIED_COMPRESSION;
2286 vint = nv_get_int32(nv, "timeout");
2287 if (gres->hr_timeout != vint) {
2288 gres->hr_timeout = vint;
2289 modified |= MODIFIED_TIMEOUT;
2291 vstr = nv_get_string(nv, "exec");
2292 if (strcmp(gres->hr_exec, vstr) != 0) {
2293 strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
2294 modified |= MODIFIED_EXEC;
2296 vint = nv_get_int32(nv, "metaflush");
2297 if (gres->hr_metaflush != vint) {
2298 gres->hr_metaflush = vint;
2299 modified |= MODIFIED_METAFLUSH;
2303 * Change timeout for connected sockets.
2304 * Don't bother if we need to reconnect.
2306 if ((modified & MODIFIED_TIMEOUT) != 0 &&
2307 (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
2308 for (ii = 0; ii < ncomps; ii++) {
2311 rw_rlock(&hio_remote_lock[ii]);
2312 if (!ISCONNECTED(gres, ii)) {
2313 rw_unlock(&hio_remote_lock[ii]);
2316 rw_unlock(&hio_remote_lock[ii]);
2317 if (proto_timeout(gres->hr_remotein,
2318 gres->hr_timeout) == -1) {
2319 pjdlog_errno(LOG_WARNING,
2320 "Unable to set connection timeout");
2322 if (proto_timeout(gres->hr_remoteout,
2323 gres->hr_timeout) == -1) {
2324 pjdlog_errno(LOG_WARNING,
2325 "Unable to set connection timeout");
2329 if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
2330 for (ii = 0; ii < ncomps; ii++) {
2333 remote_close(gres, ii);
2335 if (modified & MODIFIED_REMOTEADDR) {
2336 vstr = nv_get_string(nv, "remoteaddr");
2337 strlcpy(gres->hr_remoteaddr, vstr,
2338 sizeof(gres->hr_remoteaddr));
2341 #undef MODIFIED_REMOTEADDR
2342 #undef MODIFIED_SOURCEADDR
2343 #undef MODIFIED_REPLICATION
2344 #undef MODIFIED_CHECKSUM
2345 #undef MODIFIED_COMPRESSION
2346 #undef MODIFIED_TIMEOUT
2347 #undef MODIFIED_EXEC
2348 #undef MODIFIED_METAFLUSH
2350 pjdlog_info("Configuration reloaded successfully.");
2354 guard_one(struct hast_resource *res, unsigned int ncomp)
2356 struct proto_conn *in, *out;
2358 if (!ISREMOTE(ncomp))
2361 rw_rlock(&hio_remote_lock[ncomp]);
2363 if (!real_remote(res)) {
2364 rw_unlock(&hio_remote_lock[ncomp]);
2368 if (ISCONNECTED(res, ncomp)) {
2369 PJDLOG_ASSERT(res->hr_remotein != NULL);
2370 PJDLOG_ASSERT(res->hr_remoteout != NULL);
2371 rw_unlock(&hio_remote_lock[ncomp]);
2372 pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2373 res->hr_remoteaddr);
2377 PJDLOG_ASSERT(res->hr_remotein == NULL);
2378 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2380 * Upgrade the lock. It doesn't have to be atomic as no other thread
2381 * can change connection status from disconnected to connected.
2383 rw_unlock(&hio_remote_lock[ncomp]);
2384 pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2385 res->hr_remoteaddr);
2387 if (init_remote(res, &in, &out) == 0) {
2388 rw_wlock(&hio_remote_lock[ncomp]);
2389 PJDLOG_ASSERT(res->hr_remotein == NULL);
2390 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2391 PJDLOG_ASSERT(in != NULL && out != NULL);
2392 res->hr_remotein = in;
2393 res->hr_remoteout = out;
2394 rw_unlock(&hio_remote_lock[ncomp]);
2395 pjdlog_info("Successfully reconnected to %s.",
2396 res->hr_remoteaddr);
2399 /* Both connections should be NULL. */
2400 PJDLOG_ASSERT(res->hr_remotein == NULL);
2401 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2402 PJDLOG_ASSERT(in == NULL && out == NULL);
2403 pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2404 res->hr_remoteaddr);
2409 * Thread guards remote connections and reconnects when needed, handles
2413 guard_thread(void *arg)
2415 struct hast_resource *res = arg;
2416 unsigned int ii, ncomps;
2417 struct timespec timeout;
2418 time_t lastcheck, now;
2422 ncomps = HAST_NCOMPONENTS;
2423 lastcheck = time(NULL);
2425 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
2426 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
2427 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
2429 timeout.tv_sec = HAST_KEEPALIVE;
2430 timeout.tv_nsec = 0;
2437 sigexit_received = true;
2438 primary_exitx(EX_OK,
2439 "Termination signal received, exiting.");
2446 * Don't check connections until we fully started,
2447 * as we may still be looping, waiting for remote node
2448 * to switch from primary to secondary.
2451 pjdlog_debug(2, "remote_guard: Checking connections.");
2453 if (lastcheck + HAST_KEEPALIVE <= now) {
2454 for (ii = 0; ii < ncomps; ii++)
2459 signo = sigtimedwait(&mask, NULL, &timeout);