2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 The FreeBSD Foundation
5 * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
42 #include <geom/gate/g_gate.h>
56 #include <activemap.h>
58 #include <rangelock.h>
63 #include "hast_proto.h"
73 /* The is only one remote component for now. */
74 #define ISREMOTE(no) ((no) == 1)
78 * Number of components we are still waiting for.
79 * When this field goes to 0, we can send the request back to the
80 * kernel. Each component has to decrease this counter by one
83 refcnt_t hio_countdown;
85 * Each component has a place to store its own error.
86 * Once the request is handled by all components we can decide if the
87 * request overall is successful or not.
91 * Structure used to communicate with GEOM Gate class.
93 struct g_gate_ctl_io hio_ggio;
95 * Request was already confirmed to GEOM Gate.
99 * Number of components we are still waiting before sending write
100 * completion ack to GEOM Gate. Used for memsync.
102 refcnt_t hio_writecount;
104 * Memsync request was acknowleged by remote.
106 bool hio_memsyncacked;
108 * Remember replication from the time the request was initiated,
109 * so we won't get confused when replication changes on reload.
112 TAILQ_ENTRY(hio) *hio_next;
114 #define hio_free_next hio_next[0]
115 #define hio_done_next hio_next[0]
118 * Free list holds unused structures. When free list is empty, we have to wait
119 * until some in-progress requests are freed.
121 static TAILQ_HEAD(, hio) hio_free_list;
122 static size_t hio_free_list_size;
123 static pthread_mutex_t hio_free_list_lock;
124 static pthread_cond_t hio_free_list_cond;
126 * There is one send list for every component. One requests is placed on all
127 * send lists - each component gets the same request, but each component is
128 * responsible for managing his own send list.
130 static TAILQ_HEAD(, hio) *hio_send_list;
131 static size_t *hio_send_list_size;
132 static pthread_mutex_t *hio_send_list_lock;
133 static pthread_cond_t *hio_send_list_cond;
134 #define hio_send_local_list_size hio_send_list_size[0]
135 #define hio_send_remote_list_size hio_send_list_size[1]
137 * There is one recv list for every component, although local components don't
138 * use recv lists as local requests are done synchronously.
140 static TAILQ_HEAD(, hio) *hio_recv_list;
141 static size_t *hio_recv_list_size;
142 static pthread_mutex_t *hio_recv_list_lock;
143 static pthread_cond_t *hio_recv_list_cond;
144 #define hio_recv_remote_list_size hio_recv_list_size[1]
146 * Request is placed on done list by the slowest component (the one that
147 * decreased hio_countdown from 1 to 0).
149 static TAILQ_HEAD(, hio) hio_done_list;
150 static size_t hio_done_list_size;
151 static pthread_mutex_t hio_done_list_lock;
152 static pthread_cond_t hio_done_list_cond;
154 * Structure below are for interaction with sync thread.
156 static bool sync_inprogress;
157 static pthread_mutex_t sync_lock;
158 static pthread_cond_t sync_cond;
160 * The lock below allows to synchornize access to remote connections.
162 static pthread_rwlock_t *hio_remote_lock;
165 * Lock to synchronize metadata updates. Also synchronize access to
166 * hr_primary_localcnt and hr_primary_remotecnt fields.
168 static pthread_mutex_t metadata_lock;
171 * Maximum number of outstanding I/O requests.
173 #define HAST_HIO_MAX 256
175 * Number of components. At this point there are only two components: local
176 * and remote, but in the future it might be possible to use multiple local
177 * and remote components.
179 #define HAST_NCOMPONENTS 2
181 #define ISCONNECTED(res, no) \
182 ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
184 #define QUEUE_INSERT1(hio, name, ncomp) do { \
185 mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
186 if (TAILQ_EMPTY(&hio_##name##_list[(ncomp)])) \
187 cv_broadcast(&hio_##name##_list_cond[(ncomp)]); \
188 TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \
189 hio_next[(ncomp)]); \
190 hio_##name##_list_size[(ncomp)]++; \
191 mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \
193 #define QUEUE_INSERT2(hio, name) do { \
194 mtx_lock(&hio_##name##_list_lock); \
195 if (TAILQ_EMPTY(&hio_##name##_list)) \
196 cv_broadcast(&hio_##name##_list_cond); \
197 TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\
198 hio_##name##_list_size++; \
199 mtx_unlock(&hio_##name##_list_lock); \
201 #define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \
204 mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
206 while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \
207 cv_timedwait(&hio_##name##_list_cond[(ncomp)], \
208 &hio_##name##_list_lock[(ncomp)], (timeout)); \
209 if ((timeout) != 0) \
213 PJDLOG_ASSERT(hio_##name##_list_size[(ncomp)] != 0); \
214 hio_##name##_list_size[(ncomp)]--; \
215 TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \
216 hio_next[(ncomp)]); \
218 mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \
220 #define QUEUE_TAKE2(hio, name) do { \
221 mtx_lock(&hio_##name##_list_lock); \
222 while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \
223 cv_wait(&hio_##name##_list_cond, \
224 &hio_##name##_list_lock); \
226 PJDLOG_ASSERT(hio_##name##_list_size != 0); \
227 hio_##name##_list_size--; \
228 TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \
229 mtx_unlock(&hio_##name##_list_lock); \
232 #define ISFULLSYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_FULLSYNC)
233 #define ISMEMSYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_MEMSYNC)
234 #define ISASYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_ASYNC)
236 #define SYNCREQ(hio) do { \
237 (hio)->hio_ggio.gctl_unit = -1; \
238 (hio)->hio_ggio.gctl_seq = 1; \
240 #define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1)
241 #define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0)
242 #define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2)
244 #define ISMEMSYNCWRITE(hio) (ISMEMSYNC(hio) && \
245 (hio)->hio_ggio.gctl_cmd == BIO_WRITE && !ISSYNCREQ(hio))
247 static struct hast_resource *gres;
249 static pthread_mutex_t range_lock;
250 static struct rangelocks *range_regular;
251 static bool range_regular_wait;
252 static pthread_cond_t range_regular_cond;
253 static struct rangelocks *range_sync;
254 static bool range_sync_wait;
255 static pthread_cond_t range_sync_cond;
256 static bool fullystarted;
258 static void *ggate_recv_thread(void *arg);
259 static void *local_send_thread(void *arg);
260 static void *remote_send_thread(void *arg);
261 static void *remote_recv_thread(void *arg);
262 static void *ggate_send_thread(void *arg);
263 static void *sync_thread(void *arg);
264 static void *guard_thread(void *arg);
267 output_status_aux(struct nv *nvout)
270 nv_add_uint64(nvout, (uint64_t)hio_free_list_size,
272 nv_add_uint64(nvout, (uint64_t)hio_send_local_list_size,
274 nv_add_uint64(nvout, (uint64_t)hio_send_remote_list_size,
276 nv_add_uint64(nvout, (uint64_t)hio_recv_remote_list_size,
278 nv_add_uint64(nvout, (uint64_t)hio_done_list_size,
283 cleanup(struct hast_resource *res)
287 /* Remember errno. */
290 /* Destroy ggate provider if we created one. */
291 if (res->hr_ggateunit >= 0) {
292 struct g_gate_ctl_destroy ggiod;
294 bzero(&ggiod, sizeof(ggiod));
295 ggiod.gctl_version = G_GATE_VERSION;
296 ggiod.gctl_unit = res->hr_ggateunit;
297 ggiod.gctl_force = 1;
298 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) == -1) {
299 pjdlog_errno(LOG_WARNING,
300 "Unable to destroy hast/%s device",
303 res->hr_ggateunit = -1;
311 primary_exit(int exitcode, const char *fmt, ...)
315 PJDLOG_ASSERT(exitcode != EX_OK);
317 pjdlogv_errno(LOG_ERR, fmt, ap);
324 primary_exitx(int exitcode, const char *fmt, ...)
329 pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap);
336 hast_activemap_flush(struct hast_resource *res) __unlocks(res->hr_amp_lock)
338 const unsigned char *buf;
342 mtx_lock(&res->hr_amp_diskmap_lock);
343 buf = activemap_bitmap(res->hr_amp, &size);
344 mtx_unlock(&res->hr_amp_lock);
345 PJDLOG_ASSERT(buf != NULL);
346 PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
348 if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
350 pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk");
351 res->hr_stat_activemap_write_error++;
354 if (ret == 0 && res->hr_metaflush == 1 &&
355 g_flush(res->hr_localfd) == -1) {
356 if (errno == EOPNOTSUPP) {
357 pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.",
359 res->hr_metaflush = 0;
361 pjdlog_errno(LOG_ERR,
362 "Unable to flush disk cache on activemap update");
363 res->hr_stat_activemap_flush_error++;
367 mtx_unlock(&res->hr_amp_diskmap_lock);
372 real_remote(const struct hast_resource *res)
375 return (strcmp(res->hr_remoteaddr, "none") != 0);
379 init_environment(struct hast_resource *res __unused)
382 unsigned int ii, ncomps;
385 * In the future it might be per-resource value.
387 ncomps = HAST_NCOMPONENTS;
390 * Allocate memory needed by lists.
392 hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
393 if (hio_send_list == NULL) {
394 primary_exitx(EX_TEMPFAIL,
395 "Unable to allocate %zu bytes of memory for send lists.",
396 sizeof(hio_send_list[0]) * ncomps);
398 hio_send_list_size = malloc(sizeof(hio_send_list_size[0]) * ncomps);
399 if (hio_send_list_size == NULL) {
400 primary_exitx(EX_TEMPFAIL,
401 "Unable to allocate %zu bytes of memory for send list counters.",
402 sizeof(hio_send_list_size[0]) * ncomps);
404 hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
405 if (hio_send_list_lock == NULL) {
406 primary_exitx(EX_TEMPFAIL,
407 "Unable to allocate %zu bytes of memory for send list locks.",
408 sizeof(hio_send_list_lock[0]) * ncomps);
410 hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
411 if (hio_send_list_cond == NULL) {
412 primary_exitx(EX_TEMPFAIL,
413 "Unable to allocate %zu bytes of memory for send list condition variables.",
414 sizeof(hio_send_list_cond[0]) * ncomps);
416 hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
417 if (hio_recv_list == NULL) {
418 primary_exitx(EX_TEMPFAIL,
419 "Unable to allocate %zu bytes of memory for recv lists.",
420 sizeof(hio_recv_list[0]) * ncomps);
422 hio_recv_list_size = malloc(sizeof(hio_recv_list_size[0]) * ncomps);
423 if (hio_recv_list_size == NULL) {
424 primary_exitx(EX_TEMPFAIL,
425 "Unable to allocate %zu bytes of memory for recv list counters.",
426 sizeof(hio_recv_list_size[0]) * ncomps);
428 hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
429 if (hio_recv_list_lock == NULL) {
430 primary_exitx(EX_TEMPFAIL,
431 "Unable to allocate %zu bytes of memory for recv list locks.",
432 sizeof(hio_recv_list_lock[0]) * ncomps);
434 hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
435 if (hio_recv_list_cond == NULL) {
436 primary_exitx(EX_TEMPFAIL,
437 "Unable to allocate %zu bytes of memory for recv list condition variables.",
438 sizeof(hio_recv_list_cond[0]) * ncomps);
440 hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
441 if (hio_remote_lock == NULL) {
442 primary_exitx(EX_TEMPFAIL,
443 "Unable to allocate %zu bytes of memory for remote connections locks.",
444 sizeof(hio_remote_lock[0]) * ncomps);
448 * Initialize lists, their counters, locks and condition variables.
450 TAILQ_INIT(&hio_free_list);
451 mtx_init(&hio_free_list_lock);
452 cv_init(&hio_free_list_cond);
453 for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
454 TAILQ_INIT(&hio_send_list[ii]);
455 hio_send_list_size[ii] = 0;
456 mtx_init(&hio_send_list_lock[ii]);
457 cv_init(&hio_send_list_cond[ii]);
458 TAILQ_INIT(&hio_recv_list[ii]);
459 hio_recv_list_size[ii] = 0;
460 mtx_init(&hio_recv_list_lock[ii]);
461 cv_init(&hio_recv_list_cond[ii]);
462 rw_init(&hio_remote_lock[ii]);
464 TAILQ_INIT(&hio_done_list);
465 mtx_init(&hio_done_list_lock);
466 cv_init(&hio_done_list_cond);
467 mtx_init(&metadata_lock);
470 * Allocate requests pool and initialize requests.
472 for (ii = 0; ii < HAST_HIO_MAX; ii++) {
473 hio = malloc(sizeof(*hio));
475 primary_exitx(EX_TEMPFAIL,
476 "Unable to allocate %zu bytes of memory for hio request.",
479 refcnt_init(&hio->hio_countdown, 0);
480 hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
481 if (hio->hio_errors == NULL) {
482 primary_exitx(EX_TEMPFAIL,
483 "Unable allocate %zu bytes of memory for hio errors.",
484 sizeof(hio->hio_errors[0]) * ncomps);
486 hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
487 if (hio->hio_next == NULL) {
488 primary_exitx(EX_TEMPFAIL,
489 "Unable allocate %zu bytes of memory for hio_next field.",
490 sizeof(hio->hio_next[0]) * ncomps);
492 hio->hio_ggio.gctl_version = G_GATE_VERSION;
493 hio->hio_ggio.gctl_data = malloc(MAXPHYS);
494 if (hio->hio_ggio.gctl_data == NULL) {
495 primary_exitx(EX_TEMPFAIL,
496 "Unable to allocate %zu bytes of memory for gctl_data.",
499 hio->hio_ggio.gctl_length = MAXPHYS;
500 hio->hio_ggio.gctl_error = 0;
501 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
502 hio_free_list_size++;
507 init_resuid(struct hast_resource *res)
510 mtx_lock(&metadata_lock);
511 if (res->hr_resuid != 0) {
512 mtx_unlock(&metadata_lock);
515 /* Initialize unique resource identifier. */
516 arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
517 mtx_unlock(&metadata_lock);
518 if (metadata_write(res) == -1)
525 init_local(struct hast_resource *res)
530 if (metadata_read(res, true) == -1)
532 mtx_init(&res->hr_amp_lock);
533 if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
534 res->hr_local_sectorsize, res->hr_keepdirty) == -1) {
535 primary_exit(EX_TEMPFAIL, "Unable to create activemap");
537 mtx_init(&range_lock);
538 cv_init(&range_regular_cond);
539 if (rangelock_init(&range_regular) == -1)
540 primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
541 cv_init(&range_sync_cond);
542 if (rangelock_init(&range_sync) == -1)
543 primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
544 mapsize = activemap_ondisk_size(res->hr_amp);
545 buf = calloc(1, mapsize);
547 primary_exitx(EX_TEMPFAIL,
548 "Unable to allocate buffer for activemap.");
550 if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
552 primary_exit(EX_NOINPUT, "Unable to read activemap");
554 activemap_copyin(res->hr_amp, buf, mapsize);
556 if (res->hr_resuid != 0)
559 * We're using provider for the first time. Initialize local and remote
560 * counters. We don't initialize resuid here, as we want to do it just
561 * in time. The reason for this is that we want to inform secondary
562 * that there were no writes yet, so there is no need to synchronize
565 res->hr_primary_localcnt = 0;
566 res->hr_primary_remotecnt = 0;
567 if (metadata_write(res) == -1)
572 primary_connect(struct hast_resource *res, struct proto_conn **connp)
574 struct proto_conn *conn;
578 if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
579 primary_exit(EX_TEMPFAIL,
580 "Unable to send connection request to parent");
582 if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
583 primary_exit(EX_TEMPFAIL,
584 "Unable to receive reply to connection request from parent");
588 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
592 if (proto_connection_recv(res->hr_conn, true, &conn) == -1) {
593 primary_exit(EX_TEMPFAIL,
594 "Unable to receive connection from parent");
596 if (proto_connect_wait(conn, res->hr_timeout) == -1) {
597 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
602 /* Error in setting timeout is not critical, but why should it fail? */
603 if (proto_timeout(conn, res->hr_timeout) == -1)
604 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
612 * Function instructs GEOM_GATE to handle reads directly from within the kernel.
615 enable_direct_reads(struct hast_resource *res)
617 struct g_gate_ctl_modify ggiomodify;
619 bzero(&ggiomodify, sizeof(ggiomodify));
620 ggiomodify.gctl_version = G_GATE_VERSION;
621 ggiomodify.gctl_unit = res->hr_ggateunit;
622 ggiomodify.gctl_modify = GG_MODIFY_READPROV | GG_MODIFY_READOFFSET;
623 strlcpy(ggiomodify.gctl_readprov, res->hr_localpath,
624 sizeof(ggiomodify.gctl_readprov));
625 ggiomodify.gctl_readoffset = res->hr_localoff;
626 if (ioctl(res->hr_ggatefd, G_GATE_CMD_MODIFY, &ggiomodify) == 0)
627 pjdlog_debug(1, "Direct reads enabled.");
629 pjdlog_errno(LOG_WARNING, "Failed to enable direct reads");
633 init_remote(struct hast_resource *res, struct proto_conn **inp,
634 struct proto_conn **outp)
636 struct proto_conn *in, *out;
637 struct nv *nvout, *nvin;
638 const unsigned char *token;
648 PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
649 PJDLOG_ASSERT(real_remote(res));
654 if (primary_connect(res, &out) == -1)
655 return (ECONNREFUSED);
657 error = ECONNABORTED;
660 * First handshake step.
661 * Setup outgoing connection with remote node.
664 nv_add_string(nvout, res->hr_name, "resource");
665 nv_add_uint8(nvout, HAST_PROTO_VERSION, "version");
666 if (nv_error(nvout) != 0) {
667 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
668 "Unable to allocate header for connection with %s",
673 if (hast_proto_send(res, out, nvout, NULL, 0) == -1) {
674 pjdlog_errno(LOG_WARNING,
675 "Unable to send handshake header to %s",
681 if (hast_proto_recv_hdr(out, &nvin) == -1) {
682 pjdlog_errno(LOG_WARNING,
683 "Unable to receive handshake header from %s",
687 errmsg = nv_get_string(nvin, "errmsg");
688 if (errmsg != NULL) {
689 pjdlog_warning("%s", errmsg);
690 if (nv_exists(nvin, "wait"))
695 version = nv_get_uint8(nvin, "version");
698 * If no version is sent, it means this is protocol version 1.
702 if (version > HAST_PROTO_VERSION) {
703 pjdlog_warning("Invalid version received (%hhu).", version);
707 res->hr_version = version;
708 pjdlog_debug(1, "Negotiated protocol version %d.", res->hr_version);
709 token = nv_get_uint8_array(nvin, &size, "token");
711 pjdlog_warning("Handshake header from %s has no 'token' field.",
716 if (size != sizeof(res->hr_token)) {
717 pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
718 res->hr_remoteaddr, size, sizeof(res->hr_token));
722 bcopy(token, res->hr_token, sizeof(res->hr_token));
726 * Second handshake step.
727 * Setup incoming connection with remote node.
729 if (primary_connect(res, &in) == -1)
733 nv_add_string(nvout, res->hr_name, "resource");
734 nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
736 if (res->hr_resuid == 0) {
738 * The resuid field was not yet initialized.
739 * Because we do synchronization inside init_resuid(), it is
740 * possible that someone already initialized it, the function
741 * will return false then, but if we successfully initialized
742 * it, we will get true. True means that there were no writes
743 * to this resource yet and we want to inform secondary that
744 * synchronization is not needed by sending "virgin" argument.
746 if (init_resuid(res))
747 nv_add_int8(nvout, 1, "virgin");
749 nv_add_uint64(nvout, res->hr_resuid, "resuid");
750 nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
751 nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
752 if (nv_error(nvout) != 0) {
753 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
754 "Unable to allocate header for connection with %s",
759 if (hast_proto_send(res, in, nvout, NULL, 0) == -1) {
760 pjdlog_errno(LOG_WARNING,
761 "Unable to send handshake header to %s",
767 if (hast_proto_recv_hdr(out, &nvin) == -1) {
768 pjdlog_errno(LOG_WARNING,
769 "Unable to receive handshake header from %s",
773 errmsg = nv_get_string(nvin, "errmsg");
774 if (errmsg != NULL) {
775 pjdlog_warning("%s", errmsg);
779 datasize = nv_get_int64(nvin, "datasize");
780 if (datasize != res->hr_datasize) {
781 pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
782 (intmax_t)res->hr_datasize, (intmax_t)datasize);
786 extentsize = nv_get_int32(nvin, "extentsize");
787 if (extentsize != res->hr_extentsize) {
788 pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
789 (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
793 res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
794 res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
795 res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
796 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY)
797 enable_direct_reads(res);
798 if (nv_exists(nvin, "virgin")) {
800 * Secondary was reinitialized, bump localcnt if it is 0 as
801 * only we have the data.
803 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
804 PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
806 if (res->hr_primary_localcnt == 0) {
807 PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
809 mtx_lock(&metadata_lock);
810 res->hr_primary_localcnt++;
811 pjdlog_debug(1, "Increasing localcnt to %ju.",
812 (uintmax_t)res->hr_primary_localcnt);
813 (void)metadata_write(res);
814 mtx_unlock(&metadata_lock);
818 mapsize = nv_get_uint32(nvin, "mapsize");
820 map = malloc(mapsize);
822 pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
828 * Remote node have some dirty extents on its own, lets
829 * download its activemap.
831 if (hast_proto_recv_data(res, out, nvin, map,
833 pjdlog_errno(LOG_ERR,
834 "Unable to receive remote activemap");
839 mtx_lock(&res->hr_amp_lock);
841 * Merge local and remote bitmaps.
843 activemap_merge(res->hr_amp, map, mapsize);
846 * Now that we merged bitmaps from both nodes, flush it to the
847 * disk before we start to synchronize.
849 (void)hast_activemap_flush(res);
853 /* Setup directions. */
854 if (proto_send(out, NULL, 0) == -1)
855 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
856 if (proto_recv(in, NULL, 0) == -1)
857 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
859 pjdlog_info("Connected to %s.", res->hr_remoteaddr);
860 if (res->hr_original_replication == HAST_REPLICATION_MEMSYNC &&
861 res->hr_version < 2) {
862 pjdlog_warning("The 'memsync' replication mode is not supported by the remote node, falling back to 'fullsync' mode.");
863 res->hr_replication = HAST_REPLICATION_FULLSYNC;
864 } else if (res->hr_replication != res->hr_original_replication) {
866 * This is in case hastd disconnected and was upgraded.
868 res->hr_replication = res->hr_original_replication;
870 if (inp != NULL && outp != NULL) {
874 res->hr_remotein = in;
875 res->hr_remoteout = out;
877 event_send(res, EVENT_CONNECT);
880 if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
881 event_send(res, EVENT_SPLITBRAIN);
892 mtx_lock(&sync_lock);
893 sync_inprogress = true;
894 mtx_unlock(&sync_lock);
895 cv_signal(&sync_cond);
902 mtx_lock(&sync_lock);
904 sync_inprogress = false;
905 mtx_unlock(&sync_lock);
909 init_ggate(struct hast_resource *res)
911 struct g_gate_ctl_create ggiocreate;
912 struct g_gate_ctl_cancel ggiocancel;
915 * We communicate with ggate via /dev/ggctl. Open it.
917 res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
918 if (res->hr_ggatefd == -1)
919 primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
921 * Create provider before trying to connect, as connection failure
922 * is not critical, but may take some time.
924 bzero(&ggiocreate, sizeof(ggiocreate));
925 ggiocreate.gctl_version = G_GATE_VERSION;
926 ggiocreate.gctl_mediasize = res->hr_datasize;
927 ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
928 ggiocreate.gctl_flags = 0;
929 ggiocreate.gctl_maxcount = 0;
930 ggiocreate.gctl_timeout = 0;
931 ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
932 snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
934 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
935 pjdlog_info("Device hast/%s created.", res->hr_provname);
936 res->hr_ggateunit = ggiocreate.gctl_unit;
939 if (errno != EEXIST) {
940 primary_exit(EX_OSERR, "Unable to create hast/%s device",
944 "Device hast/%s already exists, we will try to take it over.",
947 * If we received EEXIST, we assume that the process who created the
948 * provider died and didn't clean up. In that case we will start from
951 bzero(&ggiocancel, sizeof(ggiocancel));
952 ggiocancel.gctl_version = G_GATE_VERSION;
953 ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
954 snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
956 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
957 pjdlog_info("Device hast/%s recovered.", res->hr_provname);
958 res->hr_ggateunit = ggiocancel.gctl_unit;
961 primary_exit(EX_OSERR, "Unable to take over hast/%s device",
966 hastd_primary(struct hast_resource *res)
970 int error, mode, debuglevel;
973 * Create communication channel for sending control commands from
976 if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
977 /* TODO: There's no need for this to be fatal error. */
978 KEEP_ERRNO((void)pidfile_remove(pfh));
979 pjdlog_exit(EX_OSERR,
980 "Unable to create control sockets between parent and child");
983 * Create communication channel for sending events from child to parent.
985 if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
986 /* TODO: There's no need for this to be fatal error. */
987 KEEP_ERRNO((void)pidfile_remove(pfh));
988 pjdlog_exit(EX_OSERR,
989 "Unable to create event sockets between child and parent");
992 * Create communication channel for sending connection requests from
995 if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) {
996 /* TODO: There's no need for this to be fatal error. */
997 KEEP_ERRNO((void)pidfile_remove(pfh));
998 pjdlog_exit(EX_OSERR,
999 "Unable to create connection sockets between child and parent");
1004 /* TODO: There's no need for this to be fatal error. */
1005 KEEP_ERRNO((void)pidfile_remove(pfh));
1006 pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
1010 /* This is parent. */
1011 /* Declare that we are receiver. */
1012 proto_recv(res->hr_event, NULL, 0);
1013 proto_recv(res->hr_conn, NULL, 0);
1014 /* Declare that we are sender. */
1015 proto_send(res->hr_ctrl, NULL, 0);
1016 res->hr_workerpid = pid;
1021 res->output_status_aux = output_status_aux;
1022 mode = pjdlog_mode_get();
1023 debuglevel = pjdlog_debug_get();
1025 /* Declare that we are sender. */
1026 proto_send(res->hr_event, NULL, 0);
1027 proto_send(res->hr_conn, NULL, 0);
1028 /* Declare that we are receiver. */
1029 proto_recv(res->hr_ctrl, NULL, 0);
1030 descriptors_cleanup(res);
1032 descriptors_assert(res, mode);
1035 pjdlog_debug_set(debuglevel);
1036 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
1037 setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
1041 init_environment(res);
1043 if (drop_privs(res) != 0) {
1047 pjdlog_info("Privileges successfully dropped.");
1050 * Create the guard thread first, so we can handle signals from the
1053 error = pthread_create(&td, NULL, guard_thread, res);
1054 PJDLOG_ASSERT(error == 0);
1056 * Create the control thread before sending any event to the parent,
1057 * as we can deadlock when parent sends control request to worker,
1058 * but worker has no control thread started yet, so parent waits.
1059 * In the meantime worker sends an event to the parent, but parent
1060 * is unable to handle the event, because it waits for control
1063 error = pthread_create(&td, NULL, ctrl_thread, res);
1064 PJDLOG_ASSERT(error == 0);
1065 if (real_remote(res)) {
1066 error = init_remote(res, NULL, NULL);
1069 } else if (error == EBUSY) {
1070 time_t start = time(NULL);
1072 pjdlog_warning("Waiting for remote node to become %s for %ds.",
1073 role2str(HAST_ROLE_SECONDARY),
1077 error = init_remote(res, NULL, NULL);
1080 if (time(NULL) > start + res->hr_timeout)
1083 if (error == EBUSY) {
1084 pjdlog_warning("Remote node is still %s, starting anyway.",
1085 role2str(HAST_ROLE_PRIMARY));
1089 error = pthread_create(&td, NULL, ggate_recv_thread, res);
1090 PJDLOG_ASSERT(error == 0);
1091 error = pthread_create(&td, NULL, local_send_thread, res);
1092 PJDLOG_ASSERT(error == 0);
1093 error = pthread_create(&td, NULL, remote_send_thread, res);
1094 PJDLOG_ASSERT(error == 0);
1095 error = pthread_create(&td, NULL, remote_recv_thread, res);
1096 PJDLOG_ASSERT(error == 0);
1097 error = pthread_create(&td, NULL, ggate_send_thread, res);
1098 PJDLOG_ASSERT(error == 0);
1099 fullystarted = true;
1100 (void)sync_thread(res);
1104 reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio,
1105 const char *fmt, ...)
1111 (void)vsnprintf(msg, sizeof(msg), fmt, ap);
1113 switch (ggio->gctl_cmd) {
1115 (void)snprlcat(msg, sizeof(msg), "READ(%ju, %ju).",
1116 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1119 (void)snprlcat(msg, sizeof(msg), "DELETE(%ju, %ju).",
1120 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1123 (void)snprlcat(msg, sizeof(msg), "FLUSH.");
1126 (void)snprlcat(msg, sizeof(msg), "WRITE(%ju, %ju).",
1127 (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
1130 (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%u).",
1131 (unsigned int)ggio->gctl_cmd);
1134 pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
1138 remote_close(struct hast_resource *res, int ncomp)
1141 rw_wlock(&hio_remote_lock[ncomp]);
1143 * Check for a race between dropping rlock and acquiring wlock -
1144 * another thread can close connection in-between.
1146 if (!ISCONNECTED(res, ncomp)) {
1147 PJDLOG_ASSERT(res->hr_remotein == NULL);
1148 PJDLOG_ASSERT(res->hr_remoteout == NULL);
1149 rw_unlock(&hio_remote_lock[ncomp]);
1153 PJDLOG_ASSERT(res->hr_remotein != NULL);
1154 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1156 pjdlog_debug(2, "Closing incoming connection to %s.",
1157 res->hr_remoteaddr);
1158 proto_close(res->hr_remotein);
1159 res->hr_remotein = NULL;
1160 pjdlog_debug(2, "Closing outgoing connection to %s.",
1161 res->hr_remoteaddr);
1162 proto_close(res->hr_remoteout);
1163 res->hr_remoteout = NULL;
1165 rw_unlock(&hio_remote_lock[ncomp]);
1167 pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
1170 * Stop synchronization if in-progress.
1174 event_send(res, EVENT_DISCONNECT);
1178 * Acknowledge write completion to the kernel, but don't update activemap yet.
1181 write_complete(struct hast_resource *res, struct hio *hio)
1183 struct g_gate_ctl_io *ggio;
1186 PJDLOG_ASSERT(!hio->hio_done);
1188 ggio = &hio->hio_ggio;
1189 PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE);
1192 * Bump local count if this is first write after
1193 * connection failure with remote node.
1196 rw_rlock(&hio_remote_lock[ncomp]);
1197 if (!ISCONNECTED(res, ncomp)) {
1198 mtx_lock(&metadata_lock);
1199 if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) {
1200 res->hr_primary_localcnt++;
1201 pjdlog_debug(1, "Increasing localcnt to %ju.",
1202 (uintmax_t)res->hr_primary_localcnt);
1203 (void)metadata_write(res);
1205 mtx_unlock(&metadata_lock);
1207 rw_unlock(&hio_remote_lock[ncomp]);
1208 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1)
1209 primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
1210 hio->hio_done = true;
1214 * Thread receives ggate I/O requests from the kernel and passes them to
1215 * appropriate threads:
1216 * WRITE - always goes to both local_send and remote_send threads
1217 * READ (when the block is up-to-date on local component) -
1218 * only local_send thread
1219 * READ (when the block isn't up-to-date on local component) -
1220 * only remote_send thread
1221 * DELETE - always goes to both local_send and remote_send threads
1222 * FLUSH - always goes to both local_send and remote_send threads
1225 ggate_recv_thread(void *arg)
1227 struct hast_resource *res = arg;
1228 struct g_gate_ctl_io *ggio;
1230 unsigned int ii, ncomp, ncomps;
1234 pjdlog_debug(2, "ggate_recv: Taking free request.");
1235 QUEUE_TAKE2(hio, free);
1236 pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
1237 ggio = &hio->hio_ggio;
1238 ggio->gctl_unit = res->hr_ggateunit;
1239 ggio->gctl_length = MAXPHYS;
1240 ggio->gctl_error = 0;
1241 hio->hio_done = false;
1242 hio->hio_replication = res->hr_replication;
1244 "ggate_recv: (%p) Waiting for request from the kernel.",
1246 if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) {
1247 if (sigexit_received)
1249 primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
1251 error = ggio->gctl_error;
1256 /* Exit gracefully. */
1257 if (!sigexit_received) {
1259 "ggate_recv: (%p) Received cancel from the kernel.",
1261 pjdlog_info("Received cancel from the kernel, exiting.");
1266 * Buffer too small? Impossible, we allocate MAXPHYS
1267 * bytes - request can't be bigger than that.
1272 primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
1277 ncomps = HAST_NCOMPONENTS;
1279 for (ii = 0; ii < ncomps; ii++)
1280 hio->hio_errors[ii] = EINVAL;
1281 reqlog(LOG_DEBUG, 2, ggio,
1282 "ggate_recv: (%p) Request received from the kernel: ",
1286 * Inform all components about new write request.
1287 * For read request prefer local component unless the given
1288 * range is out-of-date, then use remote component.
1290 switch (ggio->gctl_cmd) {
1292 res->hr_stat_read++;
1294 mtx_lock(&metadata_lock);
1295 if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
1296 res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
1298 * This range is up-to-date on local component,
1299 * so handle request locally.
1301 /* Local component is 0 for now. */
1303 } else /* if (res->hr_syncsrc ==
1304 HAST_SYNCSRC_SECONDARY) */ {
1305 PJDLOG_ASSERT(res->hr_syncsrc ==
1306 HAST_SYNCSRC_SECONDARY);
1308 * This range is out-of-date on local component,
1309 * so send request to the remote node.
1311 /* Remote component is 1 for now. */
1314 mtx_unlock(&metadata_lock);
1317 res->hr_stat_write++;
1318 if (res->hr_resuid == 0 &&
1319 res->hr_primary_localcnt == 0) {
1320 /* This is first write. */
1321 res->hr_primary_localcnt = 1;
1324 mtx_lock(&range_lock);
1325 if (rangelock_islocked(range_sync,
1326 ggio->gctl_offset, ggio->gctl_length)) {
1328 "regular: Range offset=%jd length=%zu locked.",
1329 (intmax_t)ggio->gctl_offset,
1330 (size_t)ggio->gctl_length);
1331 range_regular_wait = true;
1332 cv_wait(&range_regular_cond, &range_lock);
1333 range_regular_wait = false;
1334 mtx_unlock(&range_lock);
1337 if (rangelock_add(range_regular,
1338 ggio->gctl_offset, ggio->gctl_length) == -1) {
1339 mtx_unlock(&range_lock);
1341 "regular: Range offset=%jd length=%zu is already locked, waiting.",
1342 (intmax_t)ggio->gctl_offset,
1343 (size_t)ggio->gctl_length);
1347 mtx_unlock(&range_lock);
1350 mtx_lock(&res->hr_amp_lock);
1351 if (activemap_write_start(res->hr_amp,
1352 ggio->gctl_offset, ggio->gctl_length)) {
1353 res->hr_stat_activemap_update++;
1354 (void)hast_activemap_flush(res);
1356 mtx_unlock(&res->hr_amp_lock);
1358 if (ISMEMSYNC(hio)) {
1359 hio->hio_memsyncacked = false;
1360 refcnt_init(&hio->hio_writecount, ncomps);
1364 res->hr_stat_delete++;
1367 res->hr_stat_flush++;
1371 "ggate_recv: (%p) Moving request to the send queues.", hio);
1372 refcnt_init(&hio->hio_countdown, ncomps);
1373 for (ii = ncomp; ii < ncomps; ii++)
1374 QUEUE_INSERT1(hio, send, ii);
1381 * Thread reads from or writes to local component.
1382 * If local read fails, it redirects it to remote_send thread.
1385 local_send_thread(void *arg)
1387 struct hast_resource *res = arg;
1388 struct g_gate_ctl_io *ggio;
1390 unsigned int ncomp, rncomp;
1393 /* Local component is 0 for now. */
1395 /* Remote component is 1 for now. */
1399 pjdlog_debug(2, "local_send: Taking request.");
1400 QUEUE_TAKE1(hio, send, ncomp, 0);
1401 pjdlog_debug(2, "local_send: (%p) Got request.", hio);
1402 ggio = &hio->hio_ggio;
1403 switch (ggio->gctl_cmd) {
1405 ret = pread(res->hr_localfd, ggio->gctl_data,
1407 ggio->gctl_offset + res->hr_localoff);
1408 if (ret == ggio->gctl_length)
1409 hio->hio_errors[ncomp] = 0;
1410 else if (!ISSYNCREQ(hio)) {
1412 * If READ failed, try to read from remote node.
1415 reqlog(LOG_WARNING, 0, ggio,
1416 "Local request failed (%s), trying remote node. ",
1418 } else if (ret != ggio->gctl_length) {
1419 reqlog(LOG_WARNING, 0, ggio,
1420 "Local request failed (%zd != %jd), trying remote node. ",
1421 ret, (intmax_t)ggio->gctl_length);
1423 QUEUE_INSERT1(hio, send, rncomp);
1428 ret = pwrite(res->hr_localfd, ggio->gctl_data,
1430 ggio->gctl_offset + res->hr_localoff);
1432 hio->hio_errors[ncomp] = errno;
1433 reqlog(LOG_WARNING, 0, ggio,
1434 "Local request failed (%s): ",
1436 } else if (ret != ggio->gctl_length) {
1437 hio->hio_errors[ncomp] = EIO;
1438 reqlog(LOG_WARNING, 0, ggio,
1439 "Local request failed (%zd != %jd): ",
1440 ret, (intmax_t)ggio->gctl_length);
1442 hio->hio_errors[ncomp] = 0;
1444 ggio->gctl_error = 0;
1445 write_complete(res, hio);
1450 ret = g_delete(res->hr_localfd,
1451 ggio->gctl_offset + res->hr_localoff,
1454 hio->hio_errors[ncomp] = errno;
1455 reqlog(LOG_WARNING, 0, ggio,
1456 "Local request failed (%s): ",
1459 hio->hio_errors[ncomp] = 0;
1463 if (!res->hr_localflush) {
1468 ret = g_flush(res->hr_localfd);
1470 if (errno == EOPNOTSUPP)
1471 res->hr_localflush = false;
1472 hio->hio_errors[ncomp] = errno;
1473 reqlog(LOG_WARNING, 0, ggio,
1474 "Local request failed (%s): ",
1477 hio->hio_errors[ncomp] = 0;
1481 if (ISMEMSYNCWRITE(hio)) {
1482 if (refcnt_release(&hio->hio_writecount) == 0) {
1483 write_complete(res, hio);
1486 if (refcnt_release(&hio->hio_countdown) > 0)
1488 if (ISSYNCREQ(hio)) {
1489 mtx_lock(&sync_lock);
1491 mtx_unlock(&sync_lock);
1492 cv_signal(&sync_cond);
1495 "local_send: (%p) Moving request to the done queue.",
1497 QUEUE_INSERT2(hio, done);
1505 keepalive_send(struct hast_resource *res, unsigned int ncomp)
1509 rw_rlock(&hio_remote_lock[ncomp]);
1511 if (!ISCONNECTED(res, ncomp)) {
1512 rw_unlock(&hio_remote_lock[ncomp]);
1516 PJDLOG_ASSERT(res->hr_remotein != NULL);
1517 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1520 nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1521 if (nv_error(nv) != 0) {
1522 rw_unlock(&hio_remote_lock[ncomp]);
1525 "keepalive_send: Unable to prepare header to send.");
1528 if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
1529 rw_unlock(&hio_remote_lock[ncomp]);
1530 pjdlog_common(LOG_DEBUG, 1, errno,
1531 "keepalive_send: Unable to send request");
1533 remote_close(res, ncomp);
1537 rw_unlock(&hio_remote_lock[ncomp]);
1539 pjdlog_debug(2, "keepalive_send: Request sent.");
1543 * Thread sends request to secondary node.
1546 remote_send_thread(void *arg)
1548 struct hast_resource *res = arg;
1549 struct g_gate_ctl_io *ggio;
1550 time_t lastcheck, now;
1555 uint64_t offset, length;
1559 /* Remote component is 1 for now. */
1561 lastcheck = time(NULL);
1564 pjdlog_debug(2, "remote_send: Taking request.");
1565 QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1568 if (lastcheck + HAST_KEEPALIVE <= now) {
1569 keepalive_send(res, ncomp);
1574 pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
1575 ggio = &hio->hio_ggio;
1576 switch (ggio->gctl_cmd) {
1580 offset = ggio->gctl_offset;
1581 length = ggio->gctl_length;
1585 data = ggio->gctl_data;
1586 offset = ggio->gctl_offset;
1587 length = ggio->gctl_length;
1592 offset = ggio->gctl_offset;
1593 length = ggio->gctl_length;
1602 PJDLOG_ABORT("invalid condition");
1605 nv_add_uint8(nv, cmd, "cmd");
1606 nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
1607 nv_add_uint64(nv, offset, "offset");
1608 nv_add_uint64(nv, length, "length");
1609 if (ISMEMSYNCWRITE(hio))
1610 nv_add_uint8(nv, 1, "memsync");
1611 if (nv_error(nv) != 0) {
1612 hio->hio_errors[ncomp] = nv_error(nv);
1614 "remote_send: (%p) Unable to prepare header to send.",
1616 reqlog(LOG_ERR, 0, ggio,
1617 "Unable to prepare header to send (%s): ",
1618 strerror(nv_error(nv)));
1619 /* Move failed request immediately to the done queue. */
1623 * Protect connection from disappearing.
1625 rw_rlock(&hio_remote_lock[ncomp]);
1626 if (!ISCONNECTED(res, ncomp)) {
1627 rw_unlock(&hio_remote_lock[ncomp]);
1628 hio->hio_errors[ncomp] = ENOTCONN;
1632 * Move the request to recv queue before sending it, because
1633 * in different order we can get reply before we move request
1637 "remote_send: (%p) Moving request to the recv queue.",
1639 mtx_lock(&hio_recv_list_lock[ncomp]);
1640 wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
1641 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
1642 hio_recv_list_size[ncomp]++;
1643 mtx_unlock(&hio_recv_list_lock[ncomp]);
1644 if (hast_proto_send(res, res->hr_remoteout, nv, data,
1645 data != NULL ? length : 0) == -1) {
1646 hio->hio_errors[ncomp] = errno;
1647 rw_unlock(&hio_remote_lock[ncomp]);
1649 "remote_send: (%p) Unable to send request.", hio);
1650 reqlog(LOG_ERR, 0, ggio,
1651 "Unable to send request (%s): ",
1652 strerror(hio->hio_errors[ncomp]));
1653 remote_close(res, ncomp);
1655 rw_unlock(&hio_remote_lock[ncomp]);
1659 cv_signal(&hio_recv_list_cond[ncomp]);
1663 if (ISSYNCREQ(hio)) {
1664 if (refcnt_release(&hio->hio_countdown) > 0)
1666 mtx_lock(&sync_lock);
1668 mtx_unlock(&sync_lock);
1669 cv_signal(&sync_cond);
1672 if (ggio->gctl_cmd == BIO_WRITE) {
1673 mtx_lock(&res->hr_amp_lock);
1674 if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
1675 ggio->gctl_length)) {
1676 (void)hast_activemap_flush(res);
1678 mtx_unlock(&res->hr_amp_lock);
1680 if (ISMEMSYNCWRITE(hio)) {
1681 if (refcnt_release(&hio->hio_writecount) == 0) {
1682 if (hio->hio_errors[0] == 0)
1683 write_complete(res, hio);
1687 if (refcnt_release(&hio->hio_countdown) > 0)
1690 "remote_send: (%p) Moving request to the done queue.",
1692 QUEUE_INSERT2(hio, done);
1699 * Thread receives answer from secondary node and passes it to ggate_send
1703 remote_recv_thread(void *arg)
1705 struct hast_resource *res = arg;
1706 struct g_gate_ctl_io *ggio;
1714 /* Remote component is 1 for now. */
1718 /* Wait until there is anything to receive. */
1719 mtx_lock(&hio_recv_list_lock[ncomp]);
1720 while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
1721 pjdlog_debug(2, "remote_recv: No requests, waiting.");
1722 cv_wait(&hio_recv_list_cond[ncomp],
1723 &hio_recv_list_lock[ncomp]);
1725 mtx_unlock(&hio_recv_list_lock[ncomp]);
1729 rw_rlock(&hio_remote_lock[ncomp]);
1730 if (!ISCONNECTED(res, ncomp)) {
1731 rw_unlock(&hio_remote_lock[ncomp]);
1733 * Connection is dead, so move all pending requests to
1734 * the done queue (one-by-one).
1736 mtx_lock(&hio_recv_list_lock[ncomp]);
1737 hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
1738 PJDLOG_ASSERT(hio != NULL);
1739 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1741 hio_recv_list_size[ncomp]--;
1742 mtx_unlock(&hio_recv_list_lock[ncomp]);
1743 hio->hio_errors[ncomp] = ENOTCONN;
1746 if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
1747 pjdlog_errno(LOG_ERR,
1748 "Unable to receive reply header");
1749 rw_unlock(&hio_remote_lock[ncomp]);
1750 remote_close(res, ncomp);
1753 rw_unlock(&hio_remote_lock[ncomp]);
1754 seq = nv_get_uint64(nv, "seq");
1756 pjdlog_error("Header contains no 'seq' field.");
1760 memsyncack = nv_exists(nv, "received");
1761 mtx_lock(&hio_recv_list_lock[ncomp]);
1762 TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
1763 if (hio->hio_ggio.gctl_seq == seq) {
1764 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1766 hio_recv_list_size[ncomp]--;
1770 mtx_unlock(&hio_recv_list_lock[ncomp]);
1772 pjdlog_error("Found no request matching received 'seq' field (%ju).",
1777 ggio = &hio->hio_ggio;
1778 error = nv_get_int16(nv, "error");
1780 /* Request failed on remote side. */
1781 hio->hio_errors[ncomp] = error;
1782 reqlog(LOG_WARNING, 0, ggio,
1783 "Remote request failed (%s): ", strerror(error));
1787 switch (ggio->gctl_cmd) {
1789 rw_rlock(&hio_remote_lock[ncomp]);
1790 if (!ISCONNECTED(res, ncomp)) {
1791 rw_unlock(&hio_remote_lock[ncomp]);
1795 if (hast_proto_recv_data(res, res->hr_remotein, nv,
1796 ggio->gctl_data, ggio->gctl_length) == -1) {
1797 hio->hio_errors[ncomp] = errno;
1798 pjdlog_errno(LOG_ERR,
1799 "Unable to receive reply data");
1800 rw_unlock(&hio_remote_lock[ncomp]);
1802 remote_close(res, ncomp);
1805 rw_unlock(&hio_remote_lock[ncomp]);
1812 PJDLOG_ABORT("invalid condition");
1814 hio->hio_errors[ncomp] = 0;
1817 if (ISMEMSYNCWRITE(hio)) {
1818 if (!hio->hio_memsyncacked) {
1819 PJDLOG_ASSERT(memsyncack ||
1820 hio->hio_errors[ncomp] != 0);
1821 /* Remote ack arrived. */
1822 if (refcnt_release(&hio->hio_writecount) == 0) {
1823 if (hio->hio_errors[0] == 0)
1824 write_complete(res, hio);
1826 hio->hio_memsyncacked = true;
1827 if (hio->hio_errors[ncomp] == 0) {
1829 "remote_recv: (%p) Moving request "
1830 "back to the recv queue.", hio);
1831 mtx_lock(&hio_recv_list_lock[ncomp]);
1832 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp],
1833 hio, hio_next[ncomp]);
1834 hio_recv_list_size[ncomp]++;
1835 mtx_unlock(&hio_recv_list_lock[ncomp]);
1839 PJDLOG_ASSERT(!memsyncack);
1840 /* Remote final reply arrived. */
1843 if (refcnt_release(&hio->hio_countdown) > 0)
1845 if (ISSYNCREQ(hio)) {
1846 mtx_lock(&sync_lock);
1848 mtx_unlock(&sync_lock);
1849 cv_signal(&sync_cond);
1852 "remote_recv: (%p) Moving request to the done queue.",
1854 QUEUE_INSERT2(hio, done);
1862 * Thread sends answer to the kernel.
1865 ggate_send_thread(void *arg)
1867 struct hast_resource *res = arg;
1868 struct g_gate_ctl_io *ggio;
1870 unsigned int ii, ncomps;
1872 ncomps = HAST_NCOMPONENTS;
1875 pjdlog_debug(2, "ggate_send: Taking request.");
1876 QUEUE_TAKE2(hio, done);
1877 pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
1878 ggio = &hio->hio_ggio;
1879 for (ii = 0; ii < ncomps; ii++) {
1880 if (hio->hio_errors[ii] == 0) {
1882 * One successful request is enough to declare
1885 ggio->gctl_error = 0;
1891 * None of the requests were successful.
1892 * Use the error from local component except the
1893 * case when we did only remote request.
1895 if (ggio->gctl_cmd == BIO_READ &&
1896 res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1897 ggio->gctl_error = hio->hio_errors[1];
1899 ggio->gctl_error = hio->hio_errors[0];
1901 if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
1902 mtx_lock(&res->hr_amp_lock);
1903 if (activemap_write_complete(res->hr_amp,
1904 ggio->gctl_offset, ggio->gctl_length)) {
1905 res->hr_stat_activemap_update++;
1906 (void)hast_activemap_flush(res);
1908 mtx_unlock(&res->hr_amp_lock);
1911 if (ggio->gctl_cmd == BIO_WRITE) {
1913 * Unlock range we locked.
1915 mtx_lock(&range_lock);
1916 rangelock_del(range_regular, ggio->gctl_offset,
1918 if (range_sync_wait)
1919 cv_signal(&range_sync_cond);
1920 mtx_unlock(&range_lock);
1922 write_complete(res, hio);
1924 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
1925 primary_exit(EX_OSERR,
1926 "G_GATE_CMD_DONE failed");
1929 if (hio->hio_errors[0]) {
1930 switch (ggio->gctl_cmd) {
1932 res->hr_stat_read_error++;
1935 res->hr_stat_write_error++;
1938 res->hr_stat_delete_error++;
1941 res->hr_stat_flush_error++;
1946 "ggate_send: (%p) Moving request to the free queue.", hio);
1947 QUEUE_INSERT2(hio, free);
1954 * Thread synchronize local and remote components.
1957 sync_thread(void *arg __unused)
1959 struct hast_resource *res = arg;
1961 struct g_gate_ctl_io *ggio;
1962 struct timeval tstart, tend, tdiff;
1963 unsigned int ii, ncomp, ncomps;
1964 off_t offset, length, synced;
1965 bool dorewind, directreads;
1968 ncomps = HAST_NCOMPONENTS;
1972 directreads = false;
1975 mtx_lock(&sync_lock);
1976 if (offset >= 0 && !sync_inprogress) {
1977 gettimeofday(&tend, NULL);
1978 timersub(&tend, &tstart, &tdiff);
1979 pjdlog_info("Synchronization interrupted after %#.0T. "
1980 "%NB synchronized so far.", &tdiff,
1982 event_send(res, EVENT_SYNCINTR);
1984 while (!sync_inprogress) {
1987 cv_wait(&sync_cond, &sync_lock);
1989 mtx_unlock(&sync_lock);
1991 * Obtain offset at which we should synchronize.
1992 * Rewind synchronization if needed.
1994 mtx_lock(&res->hr_amp_lock);
1996 activemap_sync_rewind(res->hr_amp);
1997 offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
1998 if (syncext != -1) {
2000 * We synchronized entire syncext extent, we can mark
2003 if (activemap_extent_complete(res->hr_amp, syncext))
2004 (void)hast_activemap_flush(res);
2006 mtx_unlock(&res->hr_amp_lock);
2008 mtx_unlock(&res->hr_amp_lock);
2013 pjdlog_info("Nodes are in sync.");
2015 pjdlog_info("Synchronization started. %NB to go.",
2016 (intmax_t)(res->hr_extentsize *
2017 activemap_ndirty(res->hr_amp)));
2018 event_send(res, EVENT_SYNCSTART);
2019 gettimeofday(&tstart, NULL);
2024 pjdlog_debug(1, "Nothing to synchronize.");
2026 * Synchronization complete, make both localcnt and
2030 rw_rlock(&hio_remote_lock[ncomp]);
2031 if (ISCONNECTED(res, ncomp)) {
2035 gettimeofday(&tend, NULL);
2036 timersub(&tend, &tstart, &tdiff);
2037 bps = (int64_t)((double)synced /
2038 ((double)tdiff.tv_sec +
2039 (double)tdiff.tv_usec / 1000000));
2040 pjdlog_info("Synchronization complete. "
2041 "%NB synchronized in %#.0lT (%NB/sec).",
2042 (intmax_t)synced, &tdiff,
2044 event_send(res, EVENT_SYNCDONE);
2046 mtx_lock(&metadata_lock);
2047 if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
2049 res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
2050 res->hr_primary_localcnt =
2051 res->hr_secondary_remotecnt;
2052 res->hr_primary_remotecnt =
2053 res->hr_secondary_localcnt;
2055 "Setting localcnt to %ju and remotecnt to %ju.",
2056 (uintmax_t)res->hr_primary_localcnt,
2057 (uintmax_t)res->hr_primary_remotecnt);
2058 (void)metadata_write(res);
2059 mtx_unlock(&metadata_lock);
2061 rw_unlock(&hio_remote_lock[ncomp]);
2063 directreads = false;
2064 enable_direct_reads(res);
2068 pjdlog_debug(2, "sync: Taking free request.");
2069 QUEUE_TAKE2(hio, free);
2070 pjdlog_debug(2, "sync: (%p) Got free request.", hio);
2072 * Lock the range we are going to synchronize. We don't want
2073 * race where someone writes between our read and write.
2076 mtx_lock(&range_lock);
2077 if (rangelock_islocked(range_regular, offset, length)) {
2079 "sync: Range offset=%jd length=%jd locked.",
2080 (intmax_t)offset, (intmax_t)length);
2081 range_sync_wait = true;
2082 cv_wait(&range_sync_cond, &range_lock);
2083 range_sync_wait = false;
2084 mtx_unlock(&range_lock);
2087 if (rangelock_add(range_sync, offset, length) == -1) {
2088 mtx_unlock(&range_lock);
2090 "sync: Range offset=%jd length=%jd is already locked, waiting.",
2091 (intmax_t)offset, (intmax_t)length);
2095 mtx_unlock(&range_lock);
2099 * First read the data from synchronization source.
2102 ggio = &hio->hio_ggio;
2103 ggio->gctl_cmd = BIO_READ;
2104 ggio->gctl_offset = offset;
2105 ggio->gctl_length = length;
2106 ggio->gctl_error = 0;
2107 hio->hio_done = false;
2108 hio->hio_replication = res->hr_replication;
2109 for (ii = 0; ii < ncomps; ii++)
2110 hio->hio_errors[ii] = EINVAL;
2111 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2113 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2115 mtx_lock(&metadata_lock);
2116 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2118 * This range is up-to-date on local component,
2119 * so handle request locally.
2121 /* Local component is 0 for now. */
2123 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2124 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2126 * This range is out-of-date on local component,
2127 * so send request to the remote node.
2129 /* Remote component is 1 for now. */
2132 mtx_unlock(&metadata_lock);
2133 refcnt_init(&hio->hio_countdown, 1);
2134 QUEUE_INSERT1(hio, send, ncomp);
2137 * Let's wait for READ to finish.
2139 mtx_lock(&sync_lock);
2140 while (!ISSYNCREQDONE(hio))
2141 cv_wait(&sync_cond, &sync_lock);
2142 mtx_unlock(&sync_lock);
2144 if (hio->hio_errors[ncomp] != 0) {
2145 pjdlog_error("Unable to read synchronization data: %s.",
2146 strerror(hio->hio_errors[ncomp]));
2151 * We read the data from synchronization source, now write it
2152 * to synchronization target.
2155 ggio->gctl_cmd = BIO_WRITE;
2156 for (ii = 0; ii < ncomps; ii++)
2157 hio->hio_errors[ii] = EINVAL;
2158 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
2160 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2162 mtx_lock(&metadata_lock);
2163 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
2165 * This range is up-to-date on local component,
2166 * so we update remote component.
2168 /* Remote component is 1 for now. */
2170 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
2171 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
2173 * This range is out-of-date on local component,
2176 /* Local component is 0 for now. */
2179 mtx_unlock(&metadata_lock);
2181 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2183 refcnt_init(&hio->hio_countdown, 1);
2184 QUEUE_INSERT1(hio, send, ncomp);
2187 * Let's wait for WRITE to finish.
2189 mtx_lock(&sync_lock);
2190 while (!ISSYNCREQDONE(hio))
2191 cv_wait(&sync_cond, &sync_lock);
2192 mtx_unlock(&sync_lock);
2194 if (hio->hio_errors[ncomp] != 0) {
2195 pjdlog_error("Unable to write synchronization data: %s.",
2196 strerror(hio->hio_errors[ncomp]));
2202 mtx_lock(&range_lock);
2203 rangelock_del(range_sync, offset, length);
2204 if (range_regular_wait)
2205 cv_signal(&range_regular_cond);
2206 mtx_unlock(&range_lock);
2207 pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
2209 QUEUE_INSERT2(hio, free);
2216 primary_config_reload(struct hast_resource *res, struct nv *nv)
2218 unsigned int ii, ncomps;
2222 pjdlog_info("Reloading configuration...");
2224 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
2225 PJDLOG_ASSERT(gres == res);
2226 nv_assert(nv, "remoteaddr");
2227 nv_assert(nv, "sourceaddr");
2228 nv_assert(nv, "replication");
2229 nv_assert(nv, "checksum");
2230 nv_assert(nv, "compression");
2231 nv_assert(nv, "timeout");
2232 nv_assert(nv, "exec");
2233 nv_assert(nv, "metaflush");
2235 ncomps = HAST_NCOMPONENTS;
2237 #define MODIFIED_REMOTEADDR 0x01
2238 #define MODIFIED_SOURCEADDR 0x02
2239 #define MODIFIED_REPLICATION 0x04
2240 #define MODIFIED_CHECKSUM 0x08
2241 #define MODIFIED_COMPRESSION 0x10
2242 #define MODIFIED_TIMEOUT 0x20
2243 #define MODIFIED_EXEC 0x40
2244 #define MODIFIED_METAFLUSH 0x80
2247 vstr = nv_get_string(nv, "remoteaddr");
2248 if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
2250 * Don't copy res->hr_remoteaddr to gres just yet.
2251 * We want remote_close() to log disconnect from the old
2252 * addresses, not from the new ones.
2254 modified |= MODIFIED_REMOTEADDR;
2256 vstr = nv_get_string(nv, "sourceaddr");
2257 if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
2258 strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
2259 modified |= MODIFIED_SOURCEADDR;
2261 vint = nv_get_int32(nv, "replication");
2262 if (gres->hr_replication != vint) {
2263 gres->hr_replication = vint;
2264 modified |= MODIFIED_REPLICATION;
2266 vint = nv_get_int32(nv, "checksum");
2267 if (gres->hr_checksum != vint) {
2268 gres->hr_checksum = vint;
2269 modified |= MODIFIED_CHECKSUM;
2271 vint = nv_get_int32(nv, "compression");
2272 if (gres->hr_compression != vint) {
2273 gres->hr_compression = vint;
2274 modified |= MODIFIED_COMPRESSION;
2276 vint = nv_get_int32(nv, "timeout");
2277 if (gres->hr_timeout != vint) {
2278 gres->hr_timeout = vint;
2279 modified |= MODIFIED_TIMEOUT;
2281 vstr = nv_get_string(nv, "exec");
2282 if (strcmp(gres->hr_exec, vstr) != 0) {
2283 strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
2284 modified |= MODIFIED_EXEC;
2286 vint = nv_get_int32(nv, "metaflush");
2287 if (gres->hr_metaflush != vint) {
2288 gres->hr_metaflush = vint;
2289 modified |= MODIFIED_METAFLUSH;
2293 * Change timeout for connected sockets.
2294 * Don't bother if we need to reconnect.
2296 if ((modified & MODIFIED_TIMEOUT) != 0 &&
2297 (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
2298 for (ii = 0; ii < ncomps; ii++) {
2301 rw_rlock(&hio_remote_lock[ii]);
2302 if (!ISCONNECTED(gres, ii)) {
2303 rw_unlock(&hio_remote_lock[ii]);
2306 rw_unlock(&hio_remote_lock[ii]);
2307 if (proto_timeout(gres->hr_remotein,
2308 gres->hr_timeout) == -1) {
2309 pjdlog_errno(LOG_WARNING,
2310 "Unable to set connection timeout");
2312 if (proto_timeout(gres->hr_remoteout,
2313 gres->hr_timeout) == -1) {
2314 pjdlog_errno(LOG_WARNING,
2315 "Unable to set connection timeout");
2319 if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
2320 for (ii = 0; ii < ncomps; ii++) {
2323 remote_close(gres, ii);
2325 if (modified & MODIFIED_REMOTEADDR) {
2326 vstr = nv_get_string(nv, "remoteaddr");
2327 strlcpy(gres->hr_remoteaddr, vstr,
2328 sizeof(gres->hr_remoteaddr));
2331 #undef MODIFIED_REMOTEADDR
2332 #undef MODIFIED_SOURCEADDR
2333 #undef MODIFIED_REPLICATION
2334 #undef MODIFIED_CHECKSUM
2335 #undef MODIFIED_COMPRESSION
2336 #undef MODIFIED_TIMEOUT
2337 #undef MODIFIED_EXEC
2338 #undef MODIFIED_METAFLUSH
2340 pjdlog_info("Configuration reloaded successfully.");
2344 guard_one(struct hast_resource *res, unsigned int ncomp)
2346 struct proto_conn *in, *out;
2348 if (!ISREMOTE(ncomp))
2351 rw_rlock(&hio_remote_lock[ncomp]);
2353 if (!real_remote(res)) {
2354 rw_unlock(&hio_remote_lock[ncomp]);
2358 if (ISCONNECTED(res, ncomp)) {
2359 PJDLOG_ASSERT(res->hr_remotein != NULL);
2360 PJDLOG_ASSERT(res->hr_remoteout != NULL);
2361 rw_unlock(&hio_remote_lock[ncomp]);
2362 pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2363 res->hr_remoteaddr);
2367 PJDLOG_ASSERT(res->hr_remotein == NULL);
2368 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2370 * Upgrade the lock. It doesn't have to be atomic as no other thread
2371 * can change connection status from disconnected to connected.
2373 rw_unlock(&hio_remote_lock[ncomp]);
2374 pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2375 res->hr_remoteaddr);
2377 if (init_remote(res, &in, &out) == 0) {
2378 rw_wlock(&hio_remote_lock[ncomp]);
2379 PJDLOG_ASSERT(res->hr_remotein == NULL);
2380 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2381 PJDLOG_ASSERT(in != NULL && out != NULL);
2382 res->hr_remotein = in;
2383 res->hr_remoteout = out;
2384 rw_unlock(&hio_remote_lock[ncomp]);
2385 pjdlog_info("Successfully reconnected to %s.",
2386 res->hr_remoteaddr);
2389 /* Both connections should be NULL. */
2390 PJDLOG_ASSERT(res->hr_remotein == NULL);
2391 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2392 PJDLOG_ASSERT(in == NULL && out == NULL);
2393 pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2394 res->hr_remoteaddr);
2399 * Thread guards remote connections and reconnects when needed, handles
2403 guard_thread(void *arg)
2405 struct hast_resource *res = arg;
2406 unsigned int ii, ncomps;
2407 struct timespec timeout;
2408 time_t lastcheck, now;
2412 ncomps = HAST_NCOMPONENTS;
2413 lastcheck = time(NULL);
2415 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
2416 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
2417 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
2419 timeout.tv_sec = HAST_KEEPALIVE;
2420 timeout.tv_nsec = 0;
2427 sigexit_received = true;
2428 primary_exitx(EX_OK,
2429 "Termination signal received, exiting.");
2436 * Don't check connections until we fully started,
2437 * as we may still be looping, waiting for remote node
2438 * to switch from primary to secondary.
2441 pjdlog_debug(2, "remote_guard: Checking connections.");
2443 if (lastcheck + HAST_KEEPALIVE <= now) {
2444 for (ii = 0; ii < ncomps; ii++)
2449 signo = sigtimedwait(&mask, NULL, &timeout);