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_broadcast(&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_broadcast(&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);
294 /* Expects res->hr_amp locked, returns unlocked. */
296 hast_activemap_flush(struct hast_resource *res)
298 const unsigned char *buf;
302 mtx_lock(&res->hr_amp_diskmap_lock);
303 buf = activemap_bitmap(res->hr_amp, &size);
304 mtx_unlock(&res->hr_amp_lock);
305 PJDLOG_ASSERT(buf != NULL);
306 PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
308 if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
310 pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk");
311 res->hr_stat_activemap_write_error++;
314 if (ret == 0 && res->hr_metaflush == 1 &&
315 g_flush(res->hr_localfd) == -1) {
316 if (errno == EOPNOTSUPP) {
317 pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.",
319 res->hr_metaflush = 0;
321 pjdlog_errno(LOG_ERR,
322 "Unable to flush disk cache on activemap update");
323 res->hr_stat_activemap_flush_error++;
327 mtx_unlock(&res->hr_amp_diskmap_lock);
332 real_remote(const struct hast_resource *res)
335 return (strcmp(res->hr_remoteaddr, "none") != 0);
339 init_environment(struct hast_resource *res __unused)
342 unsigned int ii, ncomps;
345 * In the future it might be per-resource value.
347 ncomps = HAST_NCOMPONENTS;
350 * Allocate memory needed by lists.
352 hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
353 if (hio_send_list == NULL) {
354 primary_exitx(EX_TEMPFAIL,
355 "Unable to allocate %zu bytes of memory for send lists.",
356 sizeof(hio_send_list[0]) * ncomps);
358 hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
359 if (hio_send_list_lock == NULL) {
360 primary_exitx(EX_TEMPFAIL,
361 "Unable to allocate %zu bytes of memory for send list locks.",
362 sizeof(hio_send_list_lock[0]) * ncomps);
364 hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
365 if (hio_send_list_cond == NULL) {
366 primary_exitx(EX_TEMPFAIL,
367 "Unable to allocate %zu bytes of memory for send list condition variables.",
368 sizeof(hio_send_list_cond[0]) * ncomps);
370 hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
371 if (hio_recv_list == NULL) {
372 primary_exitx(EX_TEMPFAIL,
373 "Unable to allocate %zu bytes of memory for recv lists.",
374 sizeof(hio_recv_list[0]) * ncomps);
376 hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
377 if (hio_recv_list_lock == NULL) {
378 primary_exitx(EX_TEMPFAIL,
379 "Unable to allocate %zu bytes of memory for recv list locks.",
380 sizeof(hio_recv_list_lock[0]) * ncomps);
382 hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
383 if (hio_recv_list_cond == NULL) {
384 primary_exitx(EX_TEMPFAIL,
385 "Unable to allocate %zu bytes of memory for recv list condition variables.",
386 sizeof(hio_recv_list_cond[0]) * ncomps);
388 hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
389 if (hio_remote_lock == NULL) {
390 primary_exitx(EX_TEMPFAIL,
391 "Unable to allocate %zu bytes of memory for remote connections locks.",
392 sizeof(hio_remote_lock[0]) * ncomps);
396 * Initialize lists, their locks and theirs condition variables.
398 TAILQ_INIT(&hio_free_list);
399 mtx_init(&hio_free_list_lock);
400 cv_init(&hio_free_list_cond);
401 for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
402 TAILQ_INIT(&hio_send_list[ii]);
403 mtx_init(&hio_send_list_lock[ii]);
404 cv_init(&hio_send_list_cond[ii]);
405 TAILQ_INIT(&hio_recv_list[ii]);
406 mtx_init(&hio_recv_list_lock[ii]);
407 cv_init(&hio_recv_list_cond[ii]);
408 rw_init(&hio_remote_lock[ii]);
410 TAILQ_INIT(&hio_done_list);
411 mtx_init(&hio_done_list_lock);
412 cv_init(&hio_done_list_cond);
413 mtx_init(&metadata_lock);
416 * Allocate requests pool and initialize requests.
418 for (ii = 0; ii < HAST_HIO_MAX; ii++) {
419 hio = malloc(sizeof(*hio));
421 primary_exitx(EX_TEMPFAIL,
422 "Unable to allocate %zu bytes of memory for hio request.",
425 hio->hio_countdown = 0;
426 hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
427 if (hio->hio_errors == NULL) {
428 primary_exitx(EX_TEMPFAIL,
429 "Unable allocate %zu bytes of memory for hio errors.",
430 sizeof(hio->hio_errors[0]) * ncomps);
432 hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
433 if (hio->hio_next == NULL) {
434 primary_exitx(EX_TEMPFAIL,
435 "Unable allocate %zu bytes of memory for hio_next field.",
436 sizeof(hio->hio_next[0]) * ncomps);
438 hio->hio_ggio.gctl_version = G_GATE_VERSION;
439 hio->hio_ggio.gctl_data = malloc(MAXPHYS);
440 if (hio->hio_ggio.gctl_data == NULL) {
441 primary_exitx(EX_TEMPFAIL,
442 "Unable to allocate %zu bytes of memory for gctl_data.",
445 hio->hio_ggio.gctl_length = MAXPHYS;
446 hio->hio_ggio.gctl_error = 0;
447 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
452 init_resuid(struct hast_resource *res)
455 mtx_lock(&metadata_lock);
456 if (res->hr_resuid != 0) {
457 mtx_unlock(&metadata_lock);
460 /* Initialize unique resource identifier. */
461 arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
462 mtx_unlock(&metadata_lock);
463 if (metadata_write(res) == -1)
470 init_local(struct hast_resource *res)
475 if (metadata_read(res, true) == -1)
477 mtx_init(&res->hr_amp_lock);
478 if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
479 res->hr_local_sectorsize, res->hr_keepdirty) == -1) {
480 primary_exit(EX_TEMPFAIL, "Unable to create activemap");
482 mtx_init(&range_lock);
483 cv_init(&range_regular_cond);
484 if (rangelock_init(&range_regular) == -1)
485 primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
486 cv_init(&range_sync_cond);
487 if (rangelock_init(&range_sync) == -1)
488 primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
489 mapsize = activemap_ondisk_size(res->hr_amp);
490 buf = calloc(1, mapsize);
492 primary_exitx(EX_TEMPFAIL,
493 "Unable to allocate buffer for activemap.");
495 if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
497 primary_exit(EX_NOINPUT, "Unable to read activemap");
499 activemap_copyin(res->hr_amp, buf, mapsize);
501 if (res->hr_resuid != 0)
504 * We're using provider for the first time. Initialize local and remote
505 * counters. We don't initialize resuid here, as we want to do it just
506 * in time. The reason for this is that we want to inform secondary
507 * that there were no writes yet, so there is no need to synchronize
510 res->hr_primary_localcnt = 0;
511 res->hr_primary_remotecnt = 0;
512 if (metadata_write(res) == -1)
517 primary_connect(struct hast_resource *res, struct proto_conn **connp)
519 struct proto_conn *conn;
523 if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
524 primary_exit(EX_TEMPFAIL,
525 "Unable to send connection request to parent");
527 if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
528 primary_exit(EX_TEMPFAIL,
529 "Unable to receive reply to connection request from parent");
533 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
537 if (proto_connection_recv(res->hr_conn, true, &conn) == -1) {
538 primary_exit(EX_TEMPFAIL,
539 "Unable to receive connection from parent");
541 if (proto_connect_wait(conn, res->hr_timeout) == -1) {
542 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
547 /* Error in setting timeout is not critical, but why should it fail? */
548 if (proto_timeout(conn, res->hr_timeout) == -1)
549 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
557 * Function instructs GEOM_GATE to handle reads directly from within the kernel.
560 enable_direct_reads(struct hast_resource *res)
562 struct g_gate_ctl_modify ggiomodify;
564 bzero(&ggiomodify, sizeof(ggiomodify));
565 ggiomodify.gctl_version = G_GATE_VERSION;
566 ggiomodify.gctl_unit = res->hr_ggateunit;
567 ggiomodify.gctl_modify = GG_MODIFY_READPROV | GG_MODIFY_READOFFSET;
568 strlcpy(ggiomodify.gctl_readprov, res->hr_localpath,
569 sizeof(ggiomodify.gctl_readprov));
570 ggiomodify.gctl_readoffset = res->hr_localoff;
571 if (ioctl(res->hr_ggatefd, G_GATE_CMD_MODIFY, &ggiomodify) == 0)
572 pjdlog_debug(1, "Direct reads enabled.");
574 pjdlog_errno(LOG_WARNING, "Failed to enable direct reads");
578 init_remote(struct hast_resource *res, struct proto_conn **inp,
579 struct proto_conn **outp)
581 struct proto_conn *in, *out;
582 struct nv *nvout, *nvin;
583 const unsigned char *token;
593 PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
594 PJDLOG_ASSERT(real_remote(res));
599 if (primary_connect(res, &out) == -1)
600 return (ECONNREFUSED);
602 error = ECONNABORTED;
605 * First handshake step.
606 * Setup outgoing connection with remote node.
609 nv_add_string(nvout, res->hr_name, "resource");
610 nv_add_uint8(nvout, HAST_PROTO_VERSION, "version");
611 if (nv_error(nvout) != 0) {
612 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
613 "Unable to allocate header for connection with %s",
618 if (hast_proto_send(res, out, nvout, NULL, 0) == -1) {
619 pjdlog_errno(LOG_WARNING,
620 "Unable to send handshake header to %s",
626 if (hast_proto_recv_hdr(out, &nvin) == -1) {
627 pjdlog_errno(LOG_WARNING,
628 "Unable to receive handshake header from %s",
632 errmsg = nv_get_string(nvin, "errmsg");
633 if (errmsg != NULL) {
634 pjdlog_warning("%s", errmsg);
635 if (nv_exists(nvin, "wait"))
640 version = nv_get_uint8(nvin, "version");
643 * If no version is sent, it means this is protocol version 1.
647 if (version > HAST_PROTO_VERSION) {
648 pjdlog_warning("Invalid version received (%hhu).", version);
652 res->hr_version = version;
653 pjdlog_debug(1, "Negotiated protocol version %d.", res->hr_version);
654 token = nv_get_uint8_array(nvin, &size, "token");
656 pjdlog_warning("Handshake header from %s has no 'token' field.",
661 if (size != sizeof(res->hr_token)) {
662 pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
663 res->hr_remoteaddr, size, sizeof(res->hr_token));
667 bcopy(token, res->hr_token, sizeof(res->hr_token));
671 * Second handshake step.
672 * Setup incoming connection with remote node.
674 if (primary_connect(res, &in) == -1)
678 nv_add_string(nvout, res->hr_name, "resource");
679 nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
681 if (res->hr_resuid == 0) {
683 * The resuid field was not yet initialized.
684 * Because we do synchronization inside init_resuid(), it is
685 * possible that someone already initialized it, the function
686 * will return false then, but if we successfully initialized
687 * it, we will get true. True means that there were no writes
688 * to this resource yet and we want to inform secondary that
689 * synchronization is not needed by sending "virgin" argument.
691 if (init_resuid(res))
692 nv_add_int8(nvout, 1, "virgin");
694 nv_add_uint64(nvout, res->hr_resuid, "resuid");
695 nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
696 nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
697 if (nv_error(nvout) != 0) {
698 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
699 "Unable to allocate header for connection with %s",
704 if (hast_proto_send(res, in, nvout, NULL, 0) == -1) {
705 pjdlog_errno(LOG_WARNING,
706 "Unable to send handshake header to %s",
712 if (hast_proto_recv_hdr(out, &nvin) == -1) {
713 pjdlog_errno(LOG_WARNING,
714 "Unable to receive handshake header from %s",
718 errmsg = nv_get_string(nvin, "errmsg");
719 if (errmsg != NULL) {
720 pjdlog_warning("%s", errmsg);
724 datasize = nv_get_int64(nvin, "datasize");
725 if (datasize != res->hr_datasize) {
726 pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
727 (intmax_t)res->hr_datasize, (intmax_t)datasize);
731 extentsize = nv_get_int32(nvin, "extentsize");
732 if (extentsize != res->hr_extentsize) {
733 pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
734 (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
738 res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
739 res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
740 res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
741 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY)
742 enable_direct_reads(res);
743 if (nv_exists(nvin, "virgin")) {
745 * Secondary was reinitialized, bump localcnt if it is 0 as
746 * only we have the data.
748 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
749 PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
751 if (res->hr_primary_localcnt == 0) {
752 PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
754 mtx_lock(&metadata_lock);
755 res->hr_primary_localcnt++;
756 pjdlog_debug(1, "Increasing localcnt to %ju.",
757 (uintmax_t)res->hr_primary_localcnt);
758 (void)metadata_write(res);
759 mtx_unlock(&metadata_lock);
763 mapsize = nv_get_uint32(nvin, "mapsize");
765 map = malloc(mapsize);
767 pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
773 * Remote node have some dirty extents on its own, lets
774 * download its activemap.
776 if (hast_proto_recv_data(res, out, nvin, map,
778 pjdlog_errno(LOG_ERR,
779 "Unable to receive remote activemap");
784 mtx_lock(&res->hr_amp_lock);
786 * Merge local and remote bitmaps.
788 activemap_merge(res->hr_amp, map, mapsize);
791 * Now that we merged bitmaps from both nodes, flush it to the
792 * disk before we start to synchronize.
794 (void)hast_activemap_flush(res);
798 /* Setup directions. */
799 if (proto_send(out, NULL, 0) == -1)
800 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
801 if (proto_recv(in, NULL, 0) == -1)
802 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
804 pjdlog_info("Connected to %s.", res->hr_remoteaddr);
805 if (res->hr_original_replication == HAST_REPLICATION_MEMSYNC &&
806 res->hr_version < 2) {
807 pjdlog_warning("The 'memsync' replication mode is not supported by the remote node, falling back to 'fullsync' mode.");
808 res->hr_replication = HAST_REPLICATION_FULLSYNC;
809 } else if (res->hr_replication != res->hr_original_replication) {
811 * This is in case hastd disconnected and was upgraded.
813 res->hr_replication = res->hr_original_replication;
815 if (inp != NULL && outp != NULL) {
819 res->hr_remotein = in;
820 res->hr_remoteout = out;
822 event_send(res, EVENT_CONNECT);
825 if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
826 event_send(res, EVENT_SPLITBRAIN);
837 mtx_lock(&sync_lock);
838 sync_inprogress = true;
839 mtx_unlock(&sync_lock);
840 cv_signal(&sync_cond);
847 mtx_lock(&sync_lock);
849 sync_inprogress = false;
850 mtx_unlock(&sync_lock);
854 init_ggate(struct hast_resource *res)
856 struct g_gate_ctl_create ggiocreate;
857 struct g_gate_ctl_cancel ggiocancel;
860 * We communicate with ggate via /dev/ggctl. Open it.
862 res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
863 if (res->hr_ggatefd == -1)
864 primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
866 * Create provider before trying to connect, as connection failure
867 * is not critical, but may take some time.
869 bzero(&ggiocreate, sizeof(ggiocreate));
870 ggiocreate.gctl_version = G_GATE_VERSION;
871 ggiocreate.gctl_mediasize = res->hr_datasize;
872 ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
873 ggiocreate.gctl_flags = 0;
874 ggiocreate.gctl_maxcount = 0;
875 ggiocreate.gctl_timeout = 0;
876 ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
877 snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
879 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
880 pjdlog_info("Device hast/%s created.", res->hr_provname);
881 res->hr_ggateunit = ggiocreate.gctl_unit;
884 if (errno != EEXIST) {
885 primary_exit(EX_OSERR, "Unable to create hast/%s device",
889 "Device hast/%s already exists, we will try to take it over.",
892 * If we received EEXIST, we assume that the process who created the
893 * provider died and didn't clean up. In that case we will start from
896 bzero(&ggiocancel, sizeof(ggiocancel));
897 ggiocancel.gctl_version = G_GATE_VERSION;
898 ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
899 snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
901 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
902 pjdlog_info("Device hast/%s recovered.", res->hr_provname);
903 res->hr_ggateunit = ggiocancel.gctl_unit;
906 primary_exit(EX_OSERR, "Unable to take over hast/%s device",
911 hastd_primary(struct hast_resource *res)
915 int error, mode, debuglevel;
918 * Create communication channel for sending control commands from
921 if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
922 /* TODO: There's no need for this to be fatal error. */
923 KEEP_ERRNO((void)pidfile_remove(pfh));
924 pjdlog_exit(EX_OSERR,
925 "Unable to create control sockets between parent and child");
928 * Create communication channel for sending events from child to parent.
930 if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
931 /* TODO: There's no need for this to be fatal error. */
932 KEEP_ERRNO((void)pidfile_remove(pfh));
933 pjdlog_exit(EX_OSERR,
934 "Unable to create event sockets between child and parent");
937 * Create communication channel for sending connection requests from
940 if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) {
941 /* TODO: There's no need for this to be fatal error. */
942 KEEP_ERRNO((void)pidfile_remove(pfh));
943 pjdlog_exit(EX_OSERR,
944 "Unable to create connection sockets between child and parent");
949 /* TODO: There's no need for this to be fatal error. */
950 KEEP_ERRNO((void)pidfile_remove(pfh));
951 pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
955 /* This is parent. */
956 /* Declare that we are receiver. */
957 proto_recv(res->hr_event, NULL, 0);
958 proto_recv(res->hr_conn, NULL, 0);
959 /* Declare that we are sender. */
960 proto_send(res->hr_ctrl, NULL, 0);
961 res->hr_workerpid = pid;
966 mode = pjdlog_mode_get();
967 debuglevel = pjdlog_debug_get();
969 /* Declare that we are sender. */
970 proto_send(res->hr_event, NULL, 0);
971 proto_send(res->hr_conn, NULL, 0);
972 /* Declare that we are receiver. */
973 proto_recv(res->hr_ctrl, NULL, 0);
974 descriptors_cleanup(res);
976 descriptors_assert(res, mode);
979 pjdlog_debug_set(debuglevel);
980 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
981 setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
985 init_environment(res);
987 if (drop_privs(res) != 0) {
991 pjdlog_info("Privileges successfully dropped.");
994 * Create the guard thread first, so we can handle signals from the
997 error = pthread_create(&td, NULL, guard_thread, res);
998 PJDLOG_ASSERT(error == 0);
1000 * Create the control thread before sending any event to the parent,
1001 * as we can deadlock when parent sends control request to worker,
1002 * but worker has no control thread started yet, so parent waits.
1003 * In the meantime worker sends an event to the parent, but parent
1004 * is unable to handle the event, because it waits for control
1007 error = pthread_create(&td, NULL, ctrl_thread, res);
1008 PJDLOG_ASSERT(error == 0);
1009 if (real_remote(res)) {
1010 error = init_remote(res, NULL, NULL);
1013 } else if (error == EBUSY) {
1014 time_t start = time(NULL);
1016 pjdlog_warning("Waiting for remote node to become %s for %ds.",
1017 role2str(HAST_ROLE_SECONDARY),
1021 error = init_remote(res, NULL, NULL);
1024 if (time(NULL) > start + res->hr_timeout)
1027 if (error == EBUSY) {
1028 pjdlog_warning("Remote node is still %s, starting anyway.",
1029 role2str(HAST_ROLE_PRIMARY));
1033 error = pthread_create(&td, NULL, ggate_recv_thread, res);
1034 PJDLOG_ASSERT(error == 0);
1035 error = pthread_create(&td, NULL, local_send_thread, res);
1036 PJDLOG_ASSERT(error == 0);
1037 error = pthread_create(&td, NULL, remote_send_thread, res);
1038 PJDLOG_ASSERT(error == 0);
1039 error = pthread_create(&td, NULL, remote_recv_thread, res);
1040 PJDLOG_ASSERT(error == 0);
1041 error = pthread_create(&td, NULL, ggate_send_thread, res);
1042 PJDLOG_ASSERT(error == 0);
1043 fullystarted = true;
1044 (void)sync_thread(res);
1048 reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio,
1049 const char *fmt, ...)
1055 (void)vsnprintf(msg, sizeof(msg), fmt, ap);
1057 switch (ggio->gctl_cmd) {
1059 (void)snprlcat(msg, sizeof(msg), "READ(%ju, %ju).",
1060 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1063 (void)snprlcat(msg, sizeof(msg), "DELETE(%ju, %ju).",
1064 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1067 (void)snprlcat(msg, sizeof(msg), "FLUSH.");
1070 (void)snprlcat(msg, sizeof(msg), "WRITE(%ju, %ju).",
1071 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1074 (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%u).",
1075 (unsigned int)ggio->gctl_cmd);
1078 pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
1082 remote_close(struct hast_resource *res, int ncomp)
1085 rw_wlock(&hio_remote_lock[ncomp]);
1087 * Check for a race between dropping rlock and acquiring wlock -
1088 * another thread can close connection in-between.
1090 if (!ISCONNECTED(res, ncomp)) {
1091 PJDLOG_ASSERT(res->hr_remotein == NULL);
1092 PJDLOG_ASSERT(res->hr_remoteout == NULL);
1093 rw_unlock(&hio_remote_lock[ncomp]);
1097 PJDLOG_ASSERT(res->hr_remotein != NULL);
1098 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1100 pjdlog_debug(2, "Closing incoming connection to %s.",
1101 res->hr_remoteaddr);
1102 proto_close(res->hr_remotein);
1103 res->hr_remotein = NULL;
1104 pjdlog_debug(2, "Closing outgoing connection to %s.",
1105 res->hr_remoteaddr);
1106 proto_close(res->hr_remoteout);
1107 res->hr_remoteout = NULL;
1109 rw_unlock(&hio_remote_lock[ncomp]);
1111 pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
1114 * Stop synchronization if in-progress.
1118 event_send(res, EVENT_DISCONNECT);
1122 * Acknowledge write completion to the kernel, but don't update activemap yet.
1125 write_complete(struct hast_resource *res, struct hio *hio)
1127 struct g_gate_ctl_io *ggio;
1130 PJDLOG_ASSERT(!hio->hio_done);
1132 ggio = &hio->hio_ggio;
1133 PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE);
1136 * Bump local count if this is first write after
1137 * connection failure with remote node.
1140 rw_rlock(&hio_remote_lock[ncomp]);
1141 if (!ISCONNECTED(res, ncomp)) {
1142 mtx_lock(&metadata_lock);
1143 if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) {
1144 res->hr_primary_localcnt++;
1145 pjdlog_debug(1, "Increasing localcnt to %ju.",
1146 (uintmax_t)res->hr_primary_localcnt);
1147 (void)metadata_write(res);
1149 mtx_unlock(&metadata_lock);
1151 rw_unlock(&hio_remote_lock[ncomp]);
1152 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1)
1153 primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
1154 hio->hio_done = true;
1158 * Thread receives ggate I/O requests from the kernel and passes them to
1159 * appropriate threads:
1160 * WRITE - always goes to both local_send and remote_send threads
1161 * READ (when the block is up-to-date on local component) -
1162 * only local_send thread
1163 * READ (when the block isn't up-to-date on local component) -
1164 * only remote_send thread
1165 * DELETE - always goes to both local_send and remote_send threads
1166 * FLUSH - always goes to both local_send and remote_send threads
1169 ggate_recv_thread(void *arg)
1171 struct hast_resource *res = arg;
1172 struct g_gate_ctl_io *ggio;
1174 unsigned int ii, ncomp, ncomps;
1178 pjdlog_debug(2, "ggate_recv: Taking free request.");
1179 QUEUE_TAKE2(hio, free);
1180 pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
1181 ggio = &hio->hio_ggio;
1182 ggio->gctl_unit = res->hr_ggateunit;
1183 ggio->gctl_length = MAXPHYS;
1184 ggio->gctl_error = 0;
1185 hio->hio_done = false;
1186 hio->hio_replication = res->hr_replication;
1188 "ggate_recv: (%p) Waiting for request from the kernel.",
1190 if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) {
1191 if (sigexit_received)
1193 primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
1195 error = ggio->gctl_error;
1200 /* Exit gracefully. */
1201 if (!sigexit_received) {
1203 "ggate_recv: (%p) Received cancel from the kernel.",
1205 pjdlog_info("Received cancel from the kernel, exiting.");
1210 * Buffer too small? Impossible, we allocate MAXPHYS
1211 * bytes - request can't be bigger than that.
1216 primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
1221 ncomps = HAST_NCOMPONENTS;
1223 for (ii = 0; ii < ncomps; ii++)
1224 hio->hio_errors[ii] = EINVAL;
1225 reqlog(LOG_DEBUG, 2, ggio,
1226 "ggate_recv: (%p) Request received from the kernel: ",
1230 * Inform all components about new write request.
1231 * For read request prefer local component unless the given
1232 * range is out-of-date, then use remote component.
1234 switch (ggio->gctl_cmd) {
1236 res->hr_stat_read++;
1238 mtx_lock(&metadata_lock);
1239 if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
1240 res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
1242 * This range is up-to-date on local component,
1243 * so handle request locally.
1245 /* Local component is 0 for now. */
1247 } else /* if (res->hr_syncsrc ==
1248 HAST_SYNCSRC_SECONDARY) */ {
1249 PJDLOG_ASSERT(res->hr_syncsrc ==
1250 HAST_SYNCSRC_SECONDARY);
1252 * This range is out-of-date on local component,
1253 * so send request to the remote node.
1255 /* Remote component is 1 for now. */
1258 mtx_unlock(&metadata_lock);
1261 res->hr_stat_write++;
1262 if (res->hr_resuid == 0 &&
1263 res->hr_primary_localcnt == 0) {
1264 /* This is first write. */
1265 res->hr_primary_localcnt = 1;
1268 mtx_lock(&range_lock);
1269 if (rangelock_islocked(range_sync,
1270 ggio->gctl_offset, ggio->gctl_length)) {
1272 "regular: Range offset=%jd length=%zu locked.",
1273 (intmax_t)ggio->gctl_offset,
1274 (size_t)ggio->gctl_length);
1275 range_regular_wait = true;
1276 cv_wait(&range_regular_cond, &range_lock);
1277 range_regular_wait = false;
1278 mtx_unlock(&range_lock);
1281 if (rangelock_add(range_regular,
1282 ggio->gctl_offset, ggio->gctl_length) == -1) {
1283 mtx_unlock(&range_lock);
1285 "regular: Range offset=%jd length=%zu is already locked, waiting.",
1286 (intmax_t)ggio->gctl_offset,
1287 (size_t)ggio->gctl_length);
1291 mtx_unlock(&range_lock);
1294 mtx_lock(&res->hr_amp_lock);
1295 if (activemap_write_start(res->hr_amp,
1296 ggio->gctl_offset, ggio->gctl_length)) {
1297 res->hr_stat_activemap_update++;
1298 (void)hast_activemap_flush(res);
1300 mtx_unlock(&res->hr_amp_lock);
1304 res->hr_stat_delete++;
1307 res->hr_stat_flush++;
1311 "ggate_recv: (%p) Moving request to the send queues.", hio);
1312 hio->hio_countdown = ncomps;
1313 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC &&
1314 ggio->gctl_cmd == BIO_WRITE) {
1315 /* Each remote request needs two responses in memsync. */
1316 hio->hio_countdown++;
1318 for (ii = ncomp; ii < ncomps; ii++)
1319 QUEUE_INSERT1(hio, send, ii);
1326 * Thread reads from or writes to local component.
1327 * If local read fails, it redirects it to remote_send thread.
1330 local_send_thread(void *arg)
1332 struct hast_resource *res = arg;
1333 struct g_gate_ctl_io *ggio;
1335 unsigned int ncomp, rncomp;
1338 /* Local component is 0 for now. */
1340 /* Remote component is 1 for now. */
1344 pjdlog_debug(2, "local_send: Taking request.");
1345 QUEUE_TAKE1(hio, send, ncomp, 0);
1346 pjdlog_debug(2, "local_send: (%p) Got request.", hio);
1347 ggio = &hio->hio_ggio;
1348 switch (ggio->gctl_cmd) {
1350 ret = pread(res->hr_localfd, ggio->gctl_data,
1352 ggio->gctl_offset + res->hr_localoff);
1353 if (ret == ggio->gctl_length)
1354 hio->hio_errors[ncomp] = 0;
1355 else if (!ISSYNCREQ(hio)) {
1357 * If READ failed, try to read from remote node.
1360 reqlog(LOG_WARNING, 0, ggio,
1361 "Local request failed (%s), trying remote node. ",
1363 } else if (ret != ggio->gctl_length) {
1364 reqlog(LOG_WARNING, 0, ggio,
1365 "Local request failed (%zd != %jd), trying remote node. ",
1366 ret, (intmax_t)ggio->gctl_length);
1368 QUEUE_INSERT1(hio, send, rncomp);
1373 ret = pwrite(res->hr_localfd, ggio->gctl_data,
1375 ggio->gctl_offset + res->hr_localoff);
1377 hio->hio_errors[ncomp] = errno;
1378 reqlog(LOG_WARNING, 0, ggio,
1379 "Local request failed (%s): ",
1381 } else if (ret != ggio->gctl_length) {
1382 hio->hio_errors[ncomp] = EIO;
1383 reqlog(LOG_WARNING, 0, ggio,
1384 "Local request failed (%zd != %jd): ",
1385 ret, (intmax_t)ggio->gctl_length);
1387 hio->hio_errors[ncomp] = 0;
1388 if (hio->hio_replication ==
1389 HAST_REPLICATION_ASYNC) {
1390 ggio->gctl_error = 0;
1391 write_complete(res, hio);
1396 ret = g_delete(res->hr_localfd,
1397 ggio->gctl_offset + res->hr_localoff,
1400 hio->hio_errors[ncomp] = errno;
1401 reqlog(LOG_WARNING, 0, ggio,
1402 "Local request failed (%s): ",
1405 hio->hio_errors[ncomp] = 0;
1409 if (!res->hr_localflush) {
1414 ret = g_flush(res->hr_localfd);
1416 if (errno == EOPNOTSUPP)
1417 res->hr_localflush = false;
1418 hio->hio_errors[ncomp] = errno;
1419 reqlog(LOG_WARNING, 0, ggio,
1420 "Local request failed (%s): ",
1423 hio->hio_errors[ncomp] = 0;
1428 if (hio->hio_replication != HAST_REPLICATION_MEMSYNC ||
1429 ggio->gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) {
1430 if (refcnt_release(&hio->hio_countdown) > 0)
1434 * Depending on hio_countdown value, requests finished
1435 * in the following order:
1436 * 0: remote memsync, remote final, local write
1437 * 1: remote memsync, local write, (remote final)
1438 * 2: local write, (remote memsync), (remote final)
1440 switch (refcnt_release(&hio->hio_countdown)) {
1443 * Local write finished as last.
1448 * Local write finished after remote memsync
1449 * reply arrvied. We can complete the write now.
1451 if (hio->hio_errors[0] == 0)
1452 write_complete(res, hio);
1456 * Local write finished as first.
1460 PJDLOG_ABORT("Invalid hio_countdown.");
1463 if (ISSYNCREQ(hio)) {
1464 mtx_lock(&sync_lock);
1466 mtx_unlock(&sync_lock);
1467 cv_signal(&sync_cond);
1470 "local_send: (%p) Moving request to the done queue.",
1472 QUEUE_INSERT2(hio, done);
1480 keepalive_send(struct hast_resource *res, unsigned int ncomp)
1484 rw_rlock(&hio_remote_lock[ncomp]);
1486 if (!ISCONNECTED(res, ncomp)) {
1487 rw_unlock(&hio_remote_lock[ncomp]);
1491 PJDLOG_ASSERT(res->hr_remotein != NULL);
1492 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1495 nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1496 if (nv_error(nv) != 0) {
1497 rw_unlock(&hio_remote_lock[ncomp]);
1500 "keepalive_send: Unable to prepare header to send.");
1503 if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
1504 rw_unlock(&hio_remote_lock[ncomp]);
1505 pjdlog_common(LOG_DEBUG, 1, errno,
1506 "keepalive_send: Unable to send request");
1508 remote_close(res, ncomp);
1512 rw_unlock(&hio_remote_lock[ncomp]);
1514 pjdlog_debug(2, "keepalive_send: Request sent.");
1518 * Thread sends request to secondary node.
1521 remote_send_thread(void *arg)
1523 struct hast_resource *res = arg;
1524 struct g_gate_ctl_io *ggio;
1525 time_t lastcheck, now;
1530 uint64_t offset, length;
1534 /* Remote component is 1 for now. */
1536 lastcheck = time(NULL);
1539 pjdlog_debug(2, "remote_send: Taking request.");
1540 QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1543 if (lastcheck + HAST_KEEPALIVE <= now) {
1544 keepalive_send(res, ncomp);
1549 pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
1550 ggio = &hio->hio_ggio;
1551 switch (ggio->gctl_cmd) {
1555 offset = ggio->gctl_offset;
1556 length = ggio->gctl_length;
1560 data = ggio->gctl_data;
1561 offset = ggio->gctl_offset;
1562 length = ggio->gctl_length;
1567 offset = ggio->gctl_offset;
1568 length = ggio->gctl_length;
1577 PJDLOG_ABORT("invalid condition");
1580 nv_add_uint8(nv, cmd, "cmd");
1581 nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
1582 nv_add_uint64(nv, offset, "offset");
1583 nv_add_uint64(nv, length, "length");
1584 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC &&
1585 ggio->gctl_cmd == BIO_WRITE && !ISSYNCREQ(hio)) {
1586 nv_add_uint8(nv, 1, "memsync");
1588 if (nv_error(nv) != 0) {
1589 hio->hio_errors[ncomp] = nv_error(nv);
1591 "remote_send: (%p) Unable to prepare header to send.",
1593 reqlog(LOG_ERR, 0, ggio,
1594 "Unable to prepare header to send (%s): ",
1595 strerror(nv_error(nv)));
1596 /* Move failed request immediately to the done queue. */
1600 * Protect connection from disappearing.
1602 rw_rlock(&hio_remote_lock[ncomp]);
1603 if (!ISCONNECTED(res, ncomp)) {
1604 rw_unlock(&hio_remote_lock[ncomp]);
1605 hio->hio_errors[ncomp] = ENOTCONN;
1609 * Move the request to recv queue before sending it, because
1610 * in different order we can get reply before we move request
1614 "remote_send: (%p) Moving request to the recv queue.",
1616 mtx_lock(&hio_recv_list_lock[ncomp]);
1617 wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
1618 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
1619 mtx_unlock(&hio_recv_list_lock[ncomp]);
1620 if (hast_proto_send(res, res->hr_remoteout, nv, data,
1621 data != NULL ? length : 0) == -1) {
1622 hio->hio_errors[ncomp] = errno;
1623 rw_unlock(&hio_remote_lock[ncomp]);
1625 "remote_send: (%p) Unable to send request.", hio);
1626 reqlog(LOG_ERR, 0, ggio,
1627 "Unable to send request (%s): ",
1628 strerror(hio->hio_errors[ncomp]));
1629 remote_close(res, ncomp);
1631 * Take request back from the receive queue and move
1632 * it immediately to the done queue.
1634 mtx_lock(&hio_recv_list_lock[ncomp]);
1635 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1637 mtx_unlock(&hio_recv_list_lock[ncomp]);
1640 rw_unlock(&hio_remote_lock[ncomp]);
1643 cv_signal(&hio_recv_list_cond[ncomp]);
1647 if (ISSYNCREQ(hio)) {
1648 if (refcnt_release(&hio->hio_countdown) > 0)
1650 mtx_lock(&sync_lock);
1652 mtx_unlock(&sync_lock);
1653 cv_signal(&sync_cond);
1656 if (ggio->gctl_cmd == BIO_WRITE) {
1657 mtx_lock(&res->hr_amp_lock);
1658 if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
1659 ggio->gctl_length)) {
1660 (void)hast_activemap_flush(res);
1662 mtx_unlock(&res->hr_amp_lock);
1664 if (hio->hio_replication == HAST_REPLICATION_MEMSYNC)
1665 (void)refcnt_release(&hio->hio_countdown);
1667 if (refcnt_release(&hio->hio_countdown) > 0)
1670 "remote_send: (%p) Moving request to the done queue.",
1672 QUEUE_INSERT2(hio, done);
1679 * Thread receives answer from secondary node and passes it to ggate_send
1683 remote_recv_thread(void *arg)
1685 struct hast_resource *res = arg;
1686 struct g_gate_ctl_io *ggio;
1694 /* Remote component is 1 for now. */
1698 /* Wait until there is anything to receive. */
1699 mtx_lock(&hio_recv_list_lock[ncomp]);
1700 while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
1701 pjdlog_debug(2, "remote_recv: No requests, waiting.");
1702 cv_wait(&hio_recv_list_cond[ncomp],
1703 &hio_recv_list_lock[ncomp]);
1705 mtx_unlock(&hio_recv_list_lock[ncomp]);
1709 rw_rlock(&hio_remote_lock[ncomp]);
1710 if (!ISCONNECTED(res, ncomp)) {
1711 rw_unlock(&hio_remote_lock[ncomp]);
1713 * Connection is dead, so move all pending requests to
1714 * the done queue (one-by-one).
1716 mtx_lock(&hio_recv_list_lock[ncomp]);
1717 hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
1718 PJDLOG_ASSERT(hio != NULL);
1719 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1721 mtx_unlock(&hio_recv_list_lock[ncomp]);
1724 if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
1725 pjdlog_errno(LOG_ERR,
1726 "Unable to receive reply header");
1727 rw_unlock(&hio_remote_lock[ncomp]);
1728 remote_close(res, ncomp);
1731 rw_unlock(&hio_remote_lock[ncomp]);
1732 seq = nv_get_uint64(nv, "seq");
1734 pjdlog_error("Header contains no 'seq' field.");
1738 memsyncack = nv_exists(nv, "received");
1739 mtx_lock(&hio_recv_list_lock[ncomp]);
1740 TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
1741 if (hio->hio_ggio.gctl_seq == seq) {
1742 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1747 mtx_unlock(&hio_recv_list_lock[ncomp]);
1749 pjdlog_error("Found no request matching received 'seq' field (%ju).",
1754 ggio = &hio->hio_ggio;
1755 error = nv_get_int16(nv, "error");
1757 /* Request failed on remote side. */
1758 hio->hio_errors[ncomp] = error;
1759 reqlog(LOG_WARNING, 0, ggio,
1760 "Remote request failed (%s): ", strerror(error));
1764 switch (ggio->gctl_cmd) {
1766 rw_rlock(&hio_remote_lock[ncomp]);
1767 if (!ISCONNECTED(res, ncomp)) {
1768 rw_unlock(&hio_remote_lock[ncomp]);
1772 if (hast_proto_recv_data(res, res->hr_remotein, nv,
1773 ggio->gctl_data, ggio->gctl_length) == -1) {
1774 hio->hio_errors[ncomp] = errno;
1775 pjdlog_errno(LOG_ERR,
1776 "Unable to receive reply data");
1777 rw_unlock(&hio_remote_lock[ncomp]);
1779 remote_close(res, ncomp);
1782 rw_unlock(&hio_remote_lock[ncomp]);
1789 PJDLOG_ABORT("invalid condition");
1791 hio->hio_errors[ncomp] = 0;
1794 if (hio->hio_replication != HAST_REPLICATION_MEMSYNC ||
1795 hio->hio_ggio.gctl_cmd != BIO_WRITE || ISSYNCREQ(hio)) {
1796 if (refcnt_release(&hio->hio_countdown) > 0)
1800 * Depending on hio_countdown value, requests finished
1801 * in the following order:
1803 * 0: local write, remote memsync, remote final
1805 * 0: remote memsync, local write, remote final
1807 * 1: local write, remote memsync, (remote final)
1809 * 1: remote memsync, remote final, (local write)
1811 * 2: remote memsync, (local write), (remote final)
1813 * 2: remote memsync, (remote final), (local write)
1815 switch (refcnt_release(&hio->hio_countdown)) {
1818 * Remote final reply arrived.
1820 PJDLOG_ASSERT(!memsyncack);
1825 * Local request already finished, so we
1826 * can complete the write.
1828 if (hio->hio_errors[0] == 0)
1829 write_complete(res, hio);
1831 * We still need to wait for final
1835 "remote_recv: (%p) Moving request back to the recv queue.",
1837 mtx_lock(&hio_recv_list_lock[ncomp]);
1838 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp],
1839 hio, hio_next[ncomp]);
1840 mtx_unlock(&hio_recv_list_lock[ncomp]);
1843 * Remote final reply arrived before
1844 * local write finished.
1845 * Nothing to do in such case.
1851 * We received remote memsync reply even before
1852 * local write finished.
1854 PJDLOG_ASSERT(memsyncack);
1857 "remote_recv: (%p) Moving request back to the recv queue.",
1859 mtx_lock(&hio_recv_list_lock[ncomp]);
1860 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio,
1862 mtx_unlock(&hio_recv_list_lock[ncomp]);
1865 PJDLOG_ABORT("Invalid hio_countdown.");
1868 if (ISSYNCREQ(hio)) {
1869 mtx_lock(&sync_lock);
1871 mtx_unlock(&sync_lock);
1872 cv_signal(&sync_cond);
1875 "remote_recv: (%p) Moving request to the done queue.",
1877 QUEUE_INSERT2(hio, done);
1885 * Thread sends answer to the kernel.
1888 ggate_send_thread(void *arg)
1890 struct hast_resource *res = arg;
1891 struct g_gate_ctl_io *ggio;
1893 unsigned int ii, ncomps;
1895 ncomps = HAST_NCOMPONENTS;
1898 pjdlog_debug(2, "ggate_send: Taking request.");
1899 QUEUE_TAKE2(hio, done);
1900 pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
1901 ggio = &hio->hio_ggio;
1902 for (ii = 0; ii < ncomps; ii++) {
1903 if (hio->hio_errors[ii] == 0) {
1905 * One successful request is enough to declare
1908 ggio->gctl_error = 0;
1914 * None of the requests were successful.
1915 * Use the error from local component except the
1916 * case when we did only remote request.
1918 if (ggio->gctl_cmd == BIO_READ &&
1919 res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1920 ggio->gctl_error = hio->hio_errors[1];
1922 ggio->gctl_error = hio->hio_errors[0];
1924 if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
1925 mtx_lock(&res->hr_amp_lock);
1926 if (activemap_write_complete(res->hr_amp,
1927 ggio->gctl_offset, ggio->gctl_length)) {
1928 res->hr_stat_activemap_update++;
1929 (void)hast_activemap_flush(res);
1931 mtx_unlock(&res->hr_amp_lock);
1934 if (ggio->gctl_cmd == BIO_WRITE) {
1936 * Unlock range we locked.
1938 mtx_lock(&range_lock);
1939 rangelock_del(range_regular, ggio->gctl_offset,
1941 if (range_sync_wait)
1942 cv_signal(&range_sync_cond);
1943 mtx_unlock(&range_lock);
1945 write_complete(res, hio);
1947 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
1948 primary_exit(EX_OSERR,
1949 "G_GATE_CMD_DONE failed");
1952 if (hio->hio_errors[0]) {
1953 switch (ggio->gctl_cmd) {
1955 res->hr_stat_read_error++;
1958 res->hr_stat_write_error++;
1961 res->hr_stat_delete_error++;
1964 res->hr_stat_flush_error++;
1969 "ggate_send: (%p) Moving request to the free queue.", hio);
1970 QUEUE_INSERT2(hio, free);
1977 * Thread synchronize local and remote components.
1980 sync_thread(void *arg __unused)
1982 struct hast_resource *res = arg;
1984 struct g_gate_ctl_io *ggio;
1985 struct timeval tstart, tend, tdiff;
1986 unsigned int ii, ncomp, ncomps;
1987 off_t offset, length, synced;
1988 bool dorewind, directreads;
1991 ncomps = HAST_NCOMPONENTS;
1995 directreads = false;
1998 mtx_lock(&sync_lock);
1999 if (offset >= 0 && !sync_inprogress) {
2000 gettimeofday(&tend, NULL);
2001 timersub(&tend, &tstart, &tdiff);
2002 pjdlog_info("Synchronization interrupted after %#.0T. "
2003 "%NB synchronized so far.", &tdiff,
2005 event_send(res, EVENT_SYNCINTR);
2007 while (!sync_inprogress) {
2010 cv_wait(&sync_cond, &sync_lock);
2012 mtx_unlock(&sync_lock);
2014 * Obtain offset at which we should synchronize.
2015 * Rewind synchronization if needed.
2017 mtx_lock(&res->hr_amp_lock);
2019 activemap_sync_rewind(res->hr_amp);
2020 offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
2021 if (syncext != -1) {
2023 * We synchronized entire syncext extent, we can mark
2026 if (activemap_extent_complete(res->hr_amp, syncext))
2027 (void)hast_activemap_flush(res);
2029 mtx_unlock(&res->hr_amp_lock);
2031 mtx_unlock(&res->hr_amp_lock);
2036 pjdlog_info("Nodes are in sync.");
2038 pjdlog_info("Synchronization started. %NB to go.",
2039 (intmax_t)(res->hr_extentsize *
2040 activemap_ndirty(res->hr_amp)));
2041 event_send(res, EVENT_SYNCSTART);
2042 gettimeofday(&tstart, NULL);
2047 pjdlog_debug(1, "Nothing to synchronize.");
2049 * Synchronization complete, make both localcnt and
2053 rw_rlock(&hio_remote_lock[ncomp]);
2054 if (ISCONNECTED(res, ncomp)) {
2058 gettimeofday(&tend, NULL);
2059 timersub(&tend, &tstart, &tdiff);
2060 bps = (int64_t)((double)synced /
2061 ((double)tdiff.tv_sec +
2062 (double)tdiff.tv_usec / 1000000));
2063 pjdlog_info("Synchronization complete. "
2064 "%NB synchronized in %#.0lT (%NB/sec).",
2065 (intmax_t)synced, &tdiff,
2067 event_send(res, EVENT_SYNCDONE);
2069 mtx_lock(&metadata_lock);
2070 if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
2072 res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
2073 res->hr_primary_localcnt =
2074 res->hr_secondary_remotecnt;
2075 res->hr_primary_remotecnt =
2076 res->hr_secondary_localcnt;
2078 "Setting localcnt to %ju and remotecnt to %ju.",
2079 (uintmax_t)res->hr_primary_localcnt,
2080 (uintmax_t)res->hr_primary_remotecnt);
2081 (void)metadata_write(res);
2082 mtx_unlock(&metadata_lock);
2084 rw_unlock(&hio_remote_lock[ncomp]);
2086 directreads = false;
2087 enable_direct_reads(res);
2091 pjdlog_debug(2, "sync: Taking free request.");
2092 QUEUE_TAKE2(hio, free);
2093 pjdlog_debug(2, "sync: (%p) Got free request.", hio);
2095 * Lock the range we are going to synchronize. We don't want
2096 * race where someone writes between our read and write.
2099 mtx_lock(&range_lock);
2100 if (rangelock_islocked(range_regular, offset, length)) {
2102 "sync: Range offset=%jd length=%jd locked.",
2103 (intmax_t)offset, (intmax_t)length);
2104 range_sync_wait = true;
2105 cv_wait(&range_sync_cond, &range_lock);
2106 range_sync_wait = false;
2107 mtx_unlock(&range_lock);
2110 if (rangelock_add(range_sync, offset, length) == -1) {
2111 mtx_unlock(&range_lock);
2113 "sync: Range offset=%jd length=%jd is already locked, waiting.",
2114 (intmax_t)offset, (intmax_t)length);
2118 mtx_unlock(&range_lock);
2122 * First read the data from synchronization source.
2125 ggio = &hio->hio_ggio;
2126 ggio->gctl_cmd = BIO_READ;
2127 ggio->gctl_offset = offset;
2128 ggio->gctl_length = length;
2129 ggio->gctl_error = 0;
2130 hio->hio_done = false;
2131 hio->hio_replication = res->hr_replication;
2132 for (ii = 0; ii < ncomps; ii++)
2133 hio->hio_errors[ii] = EINVAL;
2134 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2136 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2138 mtx_lock(&metadata_lock);
2139 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2141 * This range is up-to-date on local component,
2142 * so handle request locally.
2144 /* Local component is 0 for now. */
2146 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2147 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2149 * This range is out-of-date on local component,
2150 * so send request to the remote node.
2152 /* Remote component is 1 for now. */
2155 mtx_unlock(&metadata_lock);
2156 hio->hio_countdown = 1;
2157 QUEUE_INSERT1(hio, send, ncomp);
2160 * Let's wait for READ to finish.
2162 mtx_lock(&sync_lock);
2163 while (!ISSYNCREQDONE(hio))
2164 cv_wait(&sync_cond, &sync_lock);
2165 mtx_unlock(&sync_lock);
2167 if (hio->hio_errors[ncomp] != 0) {
2168 pjdlog_error("Unable to read synchronization data: %s.",
2169 strerror(hio->hio_errors[ncomp]));
2174 * We read the data from synchronization source, now write it
2175 * to synchronization target.
2178 ggio->gctl_cmd = BIO_WRITE;
2179 for (ii = 0; ii < ncomps; ii++)
2180 hio->hio_errors[ii] = EINVAL;
2181 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2183 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2185 mtx_lock(&metadata_lock);
2186 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2188 * This range is up-to-date on local component,
2189 * so we update remote component.
2191 /* Remote component is 1 for now. */
2193 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2194 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2196 * This range is out-of-date on local component,
2199 /* Local component is 0 for now. */
2202 mtx_unlock(&metadata_lock);
2204 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2206 hio->hio_countdown = 1;
2207 QUEUE_INSERT1(hio, send, ncomp);
2210 * Let's wait for WRITE to finish.
2212 mtx_lock(&sync_lock);
2213 while (!ISSYNCREQDONE(hio))
2214 cv_wait(&sync_cond, &sync_lock);
2215 mtx_unlock(&sync_lock);
2217 if (hio->hio_errors[ncomp] != 0) {
2218 pjdlog_error("Unable to write synchronization data: %s.",
2219 strerror(hio->hio_errors[ncomp]));
2225 mtx_lock(&range_lock);
2226 rangelock_del(range_sync, offset, length);
2227 if (range_regular_wait)
2228 cv_signal(&range_regular_cond);
2229 mtx_unlock(&range_lock);
2230 pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
2232 QUEUE_INSERT2(hio, free);
2239 primary_config_reload(struct hast_resource *res, struct nv *nv)
2241 unsigned int ii, ncomps;
2245 pjdlog_info("Reloading configuration...");
2247 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
2248 PJDLOG_ASSERT(gres == res);
2249 nv_assert(nv, "remoteaddr");
2250 nv_assert(nv, "sourceaddr");
2251 nv_assert(nv, "replication");
2252 nv_assert(nv, "checksum");
2253 nv_assert(nv, "compression");
2254 nv_assert(nv, "timeout");
2255 nv_assert(nv, "exec");
2256 nv_assert(nv, "metaflush");
2258 ncomps = HAST_NCOMPONENTS;
2260 #define MODIFIED_REMOTEADDR 0x01
2261 #define MODIFIED_SOURCEADDR 0x02
2262 #define MODIFIED_REPLICATION 0x04
2263 #define MODIFIED_CHECKSUM 0x08
2264 #define MODIFIED_COMPRESSION 0x10
2265 #define MODIFIED_TIMEOUT 0x20
2266 #define MODIFIED_EXEC 0x40
2267 #define MODIFIED_METAFLUSH 0x80
2270 vstr = nv_get_string(nv, "remoteaddr");
2271 if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
2273 * Don't copy res->hr_remoteaddr to gres just yet.
2274 * We want remote_close() to log disconnect from the old
2275 * addresses, not from the new ones.
2277 modified |= MODIFIED_REMOTEADDR;
2279 vstr = nv_get_string(nv, "sourceaddr");
2280 if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
2281 strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
2282 modified |= MODIFIED_SOURCEADDR;
2284 vint = nv_get_int32(nv, "replication");
2285 if (gres->hr_replication != vint) {
2286 gres->hr_replication = vint;
2287 modified |= MODIFIED_REPLICATION;
2289 vint = nv_get_int32(nv, "checksum");
2290 if (gres->hr_checksum != vint) {
2291 gres->hr_checksum = vint;
2292 modified |= MODIFIED_CHECKSUM;
2294 vint = nv_get_int32(nv, "compression");
2295 if (gres->hr_compression != vint) {
2296 gres->hr_compression = vint;
2297 modified |= MODIFIED_COMPRESSION;
2299 vint = nv_get_int32(nv, "timeout");
2300 if (gres->hr_timeout != vint) {
2301 gres->hr_timeout = vint;
2302 modified |= MODIFIED_TIMEOUT;
2304 vstr = nv_get_string(nv, "exec");
2305 if (strcmp(gres->hr_exec, vstr) != 0) {
2306 strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
2307 modified |= MODIFIED_EXEC;
2309 vint = nv_get_int32(nv, "metaflush");
2310 if (gres->hr_metaflush != vint) {
2311 gres->hr_metaflush = vint;
2312 modified |= MODIFIED_METAFLUSH;
2316 * Change timeout for connected sockets.
2317 * Don't bother if we need to reconnect.
2319 if ((modified & MODIFIED_TIMEOUT) != 0 &&
2320 (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
2321 for (ii = 0; ii < ncomps; ii++) {
2324 rw_rlock(&hio_remote_lock[ii]);
2325 if (!ISCONNECTED(gres, ii)) {
2326 rw_unlock(&hio_remote_lock[ii]);
2329 rw_unlock(&hio_remote_lock[ii]);
2330 if (proto_timeout(gres->hr_remotein,
2331 gres->hr_timeout) == -1) {
2332 pjdlog_errno(LOG_WARNING,
2333 "Unable to set connection timeout");
2335 if (proto_timeout(gres->hr_remoteout,
2336 gres->hr_timeout) == -1) {
2337 pjdlog_errno(LOG_WARNING,
2338 "Unable to set connection timeout");
2342 if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
2343 for (ii = 0; ii < ncomps; ii++) {
2346 remote_close(gres, ii);
2348 if (modified & MODIFIED_REMOTEADDR) {
2349 vstr = nv_get_string(nv, "remoteaddr");
2350 strlcpy(gres->hr_remoteaddr, vstr,
2351 sizeof(gres->hr_remoteaddr));
2354 #undef MODIFIED_REMOTEADDR
2355 #undef MODIFIED_SOURCEADDR
2356 #undef MODIFIED_REPLICATION
2357 #undef MODIFIED_CHECKSUM
2358 #undef MODIFIED_COMPRESSION
2359 #undef MODIFIED_TIMEOUT
2360 #undef MODIFIED_EXEC
2361 #undef MODIFIED_METAFLUSH
2363 pjdlog_info("Configuration reloaded successfully.");
2367 guard_one(struct hast_resource *res, unsigned int ncomp)
2369 struct proto_conn *in, *out;
2371 if (!ISREMOTE(ncomp))
2374 rw_rlock(&hio_remote_lock[ncomp]);
2376 if (!real_remote(res)) {
2377 rw_unlock(&hio_remote_lock[ncomp]);
2381 if (ISCONNECTED(res, ncomp)) {
2382 PJDLOG_ASSERT(res->hr_remotein != NULL);
2383 PJDLOG_ASSERT(res->hr_remoteout != NULL);
2384 rw_unlock(&hio_remote_lock[ncomp]);
2385 pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2386 res->hr_remoteaddr);
2390 PJDLOG_ASSERT(res->hr_remotein == NULL);
2391 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2393 * Upgrade the lock. It doesn't have to be atomic as no other thread
2394 * can change connection status from disconnected to connected.
2396 rw_unlock(&hio_remote_lock[ncomp]);
2397 pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2398 res->hr_remoteaddr);
2400 if (init_remote(res, &in, &out) == 0) {
2401 rw_wlock(&hio_remote_lock[ncomp]);
2402 PJDLOG_ASSERT(res->hr_remotein == NULL);
2403 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2404 PJDLOG_ASSERT(in != NULL && out != NULL);
2405 res->hr_remotein = in;
2406 res->hr_remoteout = out;
2407 rw_unlock(&hio_remote_lock[ncomp]);
2408 pjdlog_info("Successfully reconnected to %s.",
2409 res->hr_remoteaddr);
2412 /* Both connections should be NULL. */
2413 PJDLOG_ASSERT(res->hr_remotein == NULL);
2414 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2415 PJDLOG_ASSERT(in == NULL && out == NULL);
2416 pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2417 res->hr_remoteaddr);
2422 * Thread guards remote connections and reconnects when needed, handles
2426 guard_thread(void *arg)
2428 struct hast_resource *res = arg;
2429 unsigned int ii, ncomps;
2430 struct timespec timeout;
2431 time_t lastcheck, now;
2435 ncomps = HAST_NCOMPONENTS;
2436 lastcheck = time(NULL);
2438 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
2439 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
2440 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
2442 timeout.tv_sec = HAST_KEEPALIVE;
2443 timeout.tv_nsec = 0;
2450 sigexit_received = true;
2451 primary_exitx(EX_OK,
2452 "Termination signal received, exiting.");
2459 * Don't check connections until we fully started,
2460 * as we may still be looping, waiting for remote node
2461 * to switch from primary to secondary.
2464 pjdlog_debug(2, "remote_guard: Checking connections.");
2466 if (lastcheck + HAST_KEEPALIVE <= now) {
2467 for (ii = 0; ii < ncomps; ii++)
2472 signo = sigtimedwait(&mask, NULL, &timeout);