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>
38 #include <sys/refcount.h>
41 #include <geom/gate/g_gate.h>
55 #include <activemap.h>
57 #include <rangelock.h>
62 #include "hast_proto.h"
71 /* The is only one remote component for now. */
72 #define ISREMOTE(no) ((no) == 1)
76 * Number of components we are still waiting for.
77 * When this field goes to 0, we can send the request back to the
78 * kernel. Each component has to decrease this counter by one
81 unsigned int hio_countdown;
83 * Each component has a place to store its own error.
84 * Once the request is handled by all components we can decide if the
85 * request overall is successful or not.
89 * Structure used to communicate with GEOM Gate class.
91 struct g_gate_ctl_io hio_ggio;
93 * Request was already confirmed to GEOM Gate.
97 * Remember replication from the time the request was initiated,
98 * so we won't get confused when replication changes on reload.
101 TAILQ_ENTRY(hio) *hio_next;
103 #define hio_free_next hio_next[0]
104 #define hio_done_next hio_next[0]
107 * Free list holds unused structures. When free list is empty, we have to wait
108 * until some in-progress requests are freed.
110 static TAILQ_HEAD(, hio) hio_free_list;
111 static pthread_mutex_t hio_free_list_lock;
112 static pthread_cond_t hio_free_list_cond;
114 * There is one send list for every component. One requests is placed on all
115 * send lists - each component gets the same request, but each component is
116 * responsible for managing his own send list.
118 static TAILQ_HEAD(, hio) *hio_send_list;
119 static pthread_mutex_t *hio_send_list_lock;
120 static pthread_cond_t *hio_send_list_cond;
122 * There is one recv list for every component, although local components don't
123 * use recv lists as local requests are done synchronously.
125 static TAILQ_HEAD(, hio) *hio_recv_list;
126 static pthread_mutex_t *hio_recv_list_lock;
127 static pthread_cond_t *hio_recv_list_cond;
129 * Request is placed on done list by the slowest component (the one that
130 * decreased hio_countdown from 1 to 0).
132 static TAILQ_HEAD(, hio) hio_done_list;
133 static pthread_mutex_t hio_done_list_lock;
134 static pthread_cond_t hio_done_list_cond;
136 * Structure below are for interaction with sync thread.
138 static bool sync_inprogress;
139 static pthread_mutex_t sync_lock;
140 static pthread_cond_t sync_cond;
142 * The lock below allows to synchornize access to remote connections.
144 static pthread_rwlock_t *hio_remote_lock;
147 * Lock to synchronize metadata updates. Also synchronize access to
148 * hr_primary_localcnt and hr_primary_remotecnt fields.
150 static pthread_mutex_t metadata_lock;
153 * Maximum number of outstanding I/O requests.
155 #define HAST_HIO_MAX 256
157 * Number of components. At this point there are only two components: local
158 * and remote, but in the future it might be possible to use multiple local
159 * and remote components.
161 #define HAST_NCOMPONENTS 2
163 #define ISCONNECTED(res, no) \
164 ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
166 #define QUEUE_INSERT1(hio, name, ncomp) do { \
169 mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
170 _wakeup = TAILQ_EMPTY(&hio_##name##_list[(ncomp)]); \
171 TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \
172 hio_next[(ncomp)]); \
173 mtx_unlock(&hio_##name##_list_lock[ncomp]); \
175 cv_signal(&hio_##name##_list_cond[(ncomp)]); \
177 #define QUEUE_INSERT2(hio, name) do { \
180 mtx_lock(&hio_##name##_list_lock); \
181 _wakeup = TAILQ_EMPTY(&hio_##name##_list); \
182 TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\
183 mtx_unlock(&hio_##name##_list_lock); \
185 cv_signal(&hio_##name##_list_cond); \
187 #define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \
190 mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
192 while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \
193 cv_timedwait(&hio_##name##_list_cond[(ncomp)], \
194 &hio_##name##_list_lock[(ncomp)], (timeout)); \
195 if ((timeout) != 0) \
199 TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \
200 hio_next[(ncomp)]); \
202 mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \
204 #define QUEUE_TAKE2(hio, name) do { \
205 mtx_lock(&hio_##name##_list_lock); \
206 while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \
207 cv_wait(&hio_##name##_list_cond, \
208 &hio_##name##_list_lock); \
210 TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \
211 mtx_unlock(&hio_##name##_list_lock); \
214 #define SYNCREQ(hio) do { \
215 (hio)->hio_ggio.gctl_unit = -1; \
216 (hio)->hio_ggio.gctl_seq = 1; \
218 #define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1)
219 #define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0)
220 #define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2)
222 static struct hast_resource *gres;
224 static pthread_mutex_t range_lock;
225 static struct rangelocks *range_regular;
226 static bool range_regular_wait;
227 static pthread_cond_t range_regular_cond;
228 static struct rangelocks *range_sync;
229 static bool range_sync_wait;
230 static pthread_cond_t range_sync_cond;
231 static bool fullystarted;
233 static void *ggate_recv_thread(void *arg);
234 static void *local_send_thread(void *arg);
235 static void *remote_send_thread(void *arg);
236 static void *remote_recv_thread(void *arg);
237 static void *ggate_send_thread(void *arg);
238 static void *sync_thread(void *arg);
239 static void *guard_thread(void *arg);
242 cleanup(struct hast_resource *res)
246 /* Remember errno. */
249 /* Destroy ggate provider if we created one. */
250 if (res->hr_ggateunit >= 0) {
251 struct g_gate_ctl_destroy ggiod;
253 bzero(&ggiod, sizeof(ggiod));
254 ggiod.gctl_version = G_GATE_VERSION;
255 ggiod.gctl_unit = res->hr_ggateunit;
256 ggiod.gctl_force = 1;
257 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) == -1) {
258 pjdlog_errno(LOG_WARNING,
259 "Unable to destroy hast/%s device",
262 res->hr_ggateunit = -1;
270 primary_exit(int exitcode, const char *fmt, ...)
274 PJDLOG_ASSERT(exitcode != EX_OK);
276 pjdlogv_errno(LOG_ERR, fmt, ap);
283 primary_exitx(int exitcode, const char *fmt, ...)
288 pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap);
295 hast_activemap_flush(struct hast_resource *res)
297 const unsigned char *buf;
300 buf = activemap_bitmap(res->hr_amp, &size);
301 PJDLOG_ASSERT(buf != NULL);
302 PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
303 if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
305 pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk");
308 if (res->hr_metaflush == 1 && g_flush(res->hr_localfd) == -1) {
309 if (errno == EOPNOTSUPP) {
310 pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.",
312 res->hr_metaflush = 0;
314 pjdlog_errno(LOG_ERR,
315 "Unable to flush disk cache on activemap update");
323 real_remote(const struct hast_resource *res)
326 return (strcmp(res->hr_remoteaddr, "none") != 0);
330 init_environment(struct hast_resource *res __unused)
333 unsigned int ii, ncomps;
336 * In the future it might be per-resource value.
338 ncomps = HAST_NCOMPONENTS;
341 * Allocate memory needed by lists.
343 hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
344 if (hio_send_list == NULL) {
345 primary_exitx(EX_TEMPFAIL,
346 "Unable to allocate %zu bytes of memory for send lists.",
347 sizeof(hio_send_list[0]) * ncomps);
349 hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
350 if (hio_send_list_lock == NULL) {
351 primary_exitx(EX_TEMPFAIL,
352 "Unable to allocate %zu bytes of memory for send list locks.",
353 sizeof(hio_send_list_lock[0]) * ncomps);
355 hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
356 if (hio_send_list_cond == NULL) {
357 primary_exitx(EX_TEMPFAIL,
358 "Unable to allocate %zu bytes of memory for send list condition variables.",
359 sizeof(hio_send_list_cond[0]) * ncomps);
361 hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
362 if (hio_recv_list == NULL) {
363 primary_exitx(EX_TEMPFAIL,
364 "Unable to allocate %zu bytes of memory for recv lists.",
365 sizeof(hio_recv_list[0]) * ncomps);
367 hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
368 if (hio_recv_list_lock == NULL) {
369 primary_exitx(EX_TEMPFAIL,
370 "Unable to allocate %zu bytes of memory for recv list locks.",
371 sizeof(hio_recv_list_lock[0]) * ncomps);
373 hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
374 if (hio_recv_list_cond == NULL) {
375 primary_exitx(EX_TEMPFAIL,
376 "Unable to allocate %zu bytes of memory for recv list condition variables.",
377 sizeof(hio_recv_list_cond[0]) * ncomps);
379 hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
380 if (hio_remote_lock == NULL) {
381 primary_exitx(EX_TEMPFAIL,
382 "Unable to allocate %zu bytes of memory for remote connections locks.",
383 sizeof(hio_remote_lock[0]) * ncomps);
387 * Initialize lists, their locks and theirs condition variables.
389 TAILQ_INIT(&hio_free_list);
390 mtx_init(&hio_free_list_lock);
391 cv_init(&hio_free_list_cond);
392 for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
393 TAILQ_INIT(&hio_send_list[ii]);
394 mtx_init(&hio_send_list_lock[ii]);
395 cv_init(&hio_send_list_cond[ii]);
396 TAILQ_INIT(&hio_recv_list[ii]);
397 mtx_init(&hio_recv_list_lock[ii]);
398 cv_init(&hio_recv_list_cond[ii]);
399 rw_init(&hio_remote_lock[ii]);
401 TAILQ_INIT(&hio_done_list);
402 mtx_init(&hio_done_list_lock);
403 cv_init(&hio_done_list_cond);
404 mtx_init(&metadata_lock);
407 * Allocate requests pool and initialize requests.
409 for (ii = 0; ii < HAST_HIO_MAX; ii++) {
410 hio = malloc(sizeof(*hio));
412 primary_exitx(EX_TEMPFAIL,
413 "Unable to allocate %zu bytes of memory for hio request.",
416 hio->hio_countdown = 0;
417 hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
418 if (hio->hio_errors == NULL) {
419 primary_exitx(EX_TEMPFAIL,
420 "Unable allocate %zu bytes of memory for hio errors.",
421 sizeof(hio->hio_errors[0]) * ncomps);
423 hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
424 if (hio->hio_next == NULL) {
425 primary_exitx(EX_TEMPFAIL,
426 "Unable allocate %zu bytes of memory for hio_next field.",
427 sizeof(hio->hio_next[0]) * ncomps);
429 hio->hio_ggio.gctl_version = G_GATE_VERSION;
430 hio->hio_ggio.gctl_data = malloc(MAXPHYS);
431 if (hio->hio_ggio.gctl_data == NULL) {
432 primary_exitx(EX_TEMPFAIL,
433 "Unable to allocate %zu bytes of memory for gctl_data.",
436 hio->hio_ggio.gctl_length = MAXPHYS;
437 hio->hio_ggio.gctl_error = 0;
438 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
443 init_resuid(struct hast_resource *res)
446 mtx_lock(&metadata_lock);
447 if (res->hr_resuid != 0) {
448 mtx_unlock(&metadata_lock);
451 /* Initialize unique resource identifier. */
452 arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
453 mtx_unlock(&metadata_lock);
454 if (metadata_write(res) == -1)
461 init_local(struct hast_resource *res)
466 if (metadata_read(res, true) == -1)
468 mtx_init(&res->hr_amp_lock);
469 if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
470 res->hr_local_sectorsize, res->hr_keepdirty) == -1) {
471 primary_exit(EX_TEMPFAIL, "Unable to create activemap");
473 mtx_init(&range_lock);
474 cv_init(&range_regular_cond);
475 if (rangelock_init(&range_regular) == -1)
476 primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
477 cv_init(&range_sync_cond);
478 if (rangelock_init(&range_sync) == -1)
479 primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
480 mapsize = activemap_ondisk_size(res->hr_amp);
481 buf = calloc(1, mapsize);
483 primary_exitx(EX_TEMPFAIL,
484 "Unable to allocate buffer for activemap.");
486 if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
488 primary_exit(EX_NOINPUT, "Unable to read activemap");
490 activemap_copyin(res->hr_amp, buf, mapsize);
492 if (res->hr_resuid != 0)
495 * We're using provider for the first time. Initialize local and remote
496 * counters. We don't initialize resuid here, as we want to do it just
497 * in time. The reason for this is that we want to inform secondary
498 * that there were no writes yet, so there is no need to synchronize
501 res->hr_primary_localcnt = 0;
502 res->hr_primary_remotecnt = 0;
503 if (metadata_write(res) == -1)
508 primary_connect(struct hast_resource *res, struct proto_conn **connp)
510 struct proto_conn *conn;
514 if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
515 primary_exit(EX_TEMPFAIL,
516 "Unable to send connection request to parent");
518 if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
519 primary_exit(EX_TEMPFAIL,
520 "Unable to receive reply to connection request from parent");
524 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
528 if (proto_connection_recv(res->hr_conn, true, &conn) == -1) {
529 primary_exit(EX_TEMPFAIL,
530 "Unable to receive connection from parent");
532 if (proto_connect_wait(conn, res->hr_timeout) == -1) {
533 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
538 /* Error in setting timeout is not critical, but why should it fail? */
539 if (proto_timeout(conn, res->hr_timeout) == -1)
540 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
548 init_remote(struct hast_resource *res, struct proto_conn **inp,
549 struct proto_conn **outp)
551 struct proto_conn *in, *out;
552 struct nv *nvout, *nvin;
553 const unsigned char *token;
562 PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
563 PJDLOG_ASSERT(real_remote(res));
568 if (primary_connect(res, &out) == -1)
569 return (ECONNREFUSED);
571 error = ECONNABORTED;
574 * First handshake step.
575 * Setup outgoing connection with remote node.
578 nv_add_string(nvout, res->hr_name, "resource");
579 if (nv_error(nvout) != 0) {
580 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
581 "Unable to allocate header for connection with %s",
586 if (hast_proto_send(res, out, nvout, NULL, 0) == -1) {
587 pjdlog_errno(LOG_WARNING,
588 "Unable to send handshake header to %s",
594 if (hast_proto_recv_hdr(out, &nvin) == -1) {
595 pjdlog_errno(LOG_WARNING,
596 "Unable to receive handshake header from %s",
600 errmsg = nv_get_string(nvin, "errmsg");
601 if (errmsg != NULL) {
602 pjdlog_warning("%s", errmsg);
603 if (nv_exists(nvin, "wait"))
608 token = nv_get_uint8_array(nvin, &size, "token");
610 pjdlog_warning("Handshake header from %s has no 'token' field.",
615 if (size != sizeof(res->hr_token)) {
616 pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
617 res->hr_remoteaddr, size, sizeof(res->hr_token));
621 bcopy(token, res->hr_token, sizeof(res->hr_token));
625 * Second handshake step.
626 * Setup incoming connection with remote node.
628 if (primary_connect(res, &in) == -1)
632 nv_add_string(nvout, res->hr_name, "resource");
633 nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
635 if (res->hr_resuid == 0) {
637 * The resuid field was not yet initialized.
638 * Because we do synchronization inside init_resuid(), it is
639 * possible that someone already initialized it, the function
640 * will return false then, but if we successfully initialized
641 * it, we will get true. True means that there were no writes
642 * to this resource yet and we want to inform secondary that
643 * synchronization is not needed by sending "virgin" argument.
645 if (init_resuid(res))
646 nv_add_int8(nvout, 1, "virgin");
648 nv_add_uint64(nvout, res->hr_resuid, "resuid");
649 nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
650 nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
651 if (nv_error(nvout) != 0) {
652 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
653 "Unable to allocate header for connection with %s",
658 if (hast_proto_send(res, in, nvout, NULL, 0) == -1) {
659 pjdlog_errno(LOG_WARNING,
660 "Unable to send handshake header to %s",
666 if (hast_proto_recv_hdr(out, &nvin) == -1) {
667 pjdlog_errno(LOG_WARNING,
668 "Unable to receive handshake header from %s",
672 errmsg = nv_get_string(nvin, "errmsg");
673 if (errmsg != NULL) {
674 pjdlog_warning("%s", errmsg);
678 datasize = nv_get_int64(nvin, "datasize");
679 if (datasize != res->hr_datasize) {
680 pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
681 (intmax_t)res->hr_datasize, (intmax_t)datasize);
685 extentsize = nv_get_int32(nvin, "extentsize");
686 if (extentsize != res->hr_extentsize) {
687 pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
688 (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
692 res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
693 res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
694 res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
695 if (nv_exists(nvin, "virgin")) {
697 * Secondary was reinitialized, bump localcnt if it is 0 as
698 * only we have the data.
700 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
701 PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
703 if (res->hr_primary_localcnt == 0) {
704 PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
706 mtx_lock(&metadata_lock);
707 res->hr_primary_localcnt++;
708 pjdlog_debug(1, "Increasing localcnt to %ju.",
709 (uintmax_t)res->hr_primary_localcnt);
710 (void)metadata_write(res);
711 mtx_unlock(&metadata_lock);
715 mapsize = nv_get_uint32(nvin, "mapsize");
717 map = malloc(mapsize);
719 pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
725 * Remote node have some dirty extents on its own, lets
726 * download its activemap.
728 if (hast_proto_recv_data(res, out, nvin, map,
730 pjdlog_errno(LOG_ERR,
731 "Unable to receive remote activemap");
737 * Merge local and remote bitmaps.
739 activemap_merge(res->hr_amp, map, mapsize);
742 * Now that we merged bitmaps from both nodes, flush it to the
743 * disk before we start to synchronize.
745 (void)hast_activemap_flush(res);
749 /* Setup directions. */
750 if (proto_send(out, NULL, 0) == -1)
751 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
752 if (proto_recv(in, NULL, 0) == -1)
753 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
755 pjdlog_info("Connected to %s.", res->hr_remoteaddr);
756 if (inp != NULL && outp != NULL) {
760 res->hr_remotein = in;
761 res->hr_remoteout = out;
763 event_send(res, EVENT_CONNECT);
766 if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
767 event_send(res, EVENT_SPLITBRAIN);
778 mtx_lock(&sync_lock);
779 sync_inprogress = true;
780 mtx_unlock(&sync_lock);
781 cv_signal(&sync_cond);
788 mtx_lock(&sync_lock);
790 sync_inprogress = false;
791 mtx_unlock(&sync_lock);
795 init_ggate(struct hast_resource *res)
797 struct g_gate_ctl_create ggiocreate;
798 struct g_gate_ctl_cancel ggiocancel;
801 * We communicate with ggate via /dev/ggctl. Open it.
803 res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
804 if (res->hr_ggatefd == -1)
805 primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
807 * Create provider before trying to connect, as connection failure
808 * is not critical, but may take some time.
810 bzero(&ggiocreate, sizeof(ggiocreate));
811 ggiocreate.gctl_version = G_GATE_VERSION;
812 ggiocreate.gctl_mediasize = res->hr_datasize;
813 ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
814 ggiocreate.gctl_flags = 0;
815 ggiocreate.gctl_maxcount = 0;
816 ggiocreate.gctl_timeout = 0;
817 ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
818 snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
820 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
821 pjdlog_info("Device hast/%s created.", res->hr_provname);
822 res->hr_ggateunit = ggiocreate.gctl_unit;
825 if (errno != EEXIST) {
826 primary_exit(EX_OSERR, "Unable to create hast/%s device",
830 "Device hast/%s already exists, we will try to take it over.",
833 * If we received EEXIST, we assume that the process who created the
834 * provider died and didn't clean up. In that case we will start from
837 bzero(&ggiocancel, sizeof(ggiocancel));
838 ggiocancel.gctl_version = G_GATE_VERSION;
839 ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
840 snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
842 if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
843 pjdlog_info("Device hast/%s recovered.", res->hr_provname);
844 res->hr_ggateunit = ggiocancel.gctl_unit;
847 primary_exit(EX_OSERR, "Unable to take over hast/%s device",
852 hastd_primary(struct hast_resource *res)
856 int error, mode, debuglevel;
859 * Create communication channel for sending control commands from
862 if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
863 /* TODO: There's no need for this to be fatal error. */
864 KEEP_ERRNO((void)pidfile_remove(pfh));
865 pjdlog_exit(EX_OSERR,
866 "Unable to create control sockets between parent and child");
869 * Create communication channel for sending events from child to parent.
871 if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
872 /* TODO: There's no need for this to be fatal error. */
873 KEEP_ERRNO((void)pidfile_remove(pfh));
874 pjdlog_exit(EX_OSERR,
875 "Unable to create event sockets between child and parent");
878 * Create communication channel for sending connection requests from
881 if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) {
882 /* TODO: There's no need for this to be fatal error. */
883 KEEP_ERRNO((void)pidfile_remove(pfh));
884 pjdlog_exit(EX_OSERR,
885 "Unable to create connection sockets between child and parent");
890 /* TODO: There's no need for this to be fatal error. */
891 KEEP_ERRNO((void)pidfile_remove(pfh));
892 pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
896 /* This is parent. */
897 /* Declare that we are receiver. */
898 proto_recv(res->hr_event, NULL, 0);
899 proto_recv(res->hr_conn, NULL, 0);
900 /* Declare that we are sender. */
901 proto_send(res->hr_ctrl, NULL, 0);
902 res->hr_workerpid = pid;
907 mode = pjdlog_mode_get();
908 debuglevel = pjdlog_debug_get();
910 /* Declare that we are sender. */
911 proto_send(res->hr_event, NULL, 0);
912 proto_send(res->hr_conn, NULL, 0);
913 /* Declare that we are receiver. */
914 proto_recv(res->hr_ctrl, NULL, 0);
915 descriptors_cleanup(res);
917 descriptors_assert(res, mode);
920 pjdlog_debug_set(debuglevel);
921 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
922 setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
926 init_environment(res);
928 if (drop_privs(res) != 0) {
932 pjdlog_info("Privileges successfully dropped.");
935 * Create the guard thread first, so we can handle signals from the
938 error = pthread_create(&td, NULL, guard_thread, res);
939 PJDLOG_ASSERT(error == 0);
941 * Create the control thread before sending any event to the parent,
942 * as we can deadlock when parent sends control request to worker,
943 * but worker has no control thread started yet, so parent waits.
944 * In the meantime worker sends an event to the parent, but parent
945 * is unable to handle the event, because it waits for control
948 error = pthread_create(&td, NULL, ctrl_thread, res);
949 PJDLOG_ASSERT(error == 0);
950 if (real_remote(res)) {
951 error = init_remote(res, NULL, NULL);
954 } else if (error == EBUSY) {
955 time_t start = time(NULL);
957 pjdlog_warning("Waiting for remote node to become %s for %ds.",
958 role2str(HAST_ROLE_SECONDARY),
962 error = init_remote(res, NULL, NULL);
965 if (time(NULL) > start + res->hr_timeout)
968 if (error == EBUSY) {
969 pjdlog_warning("Remote node is still %s, starting anyway.",
970 role2str(HAST_ROLE_PRIMARY));
974 error = pthread_create(&td, NULL, ggate_recv_thread, res);
975 PJDLOG_ASSERT(error == 0);
976 error = pthread_create(&td, NULL, local_send_thread, res);
977 PJDLOG_ASSERT(error == 0);
978 error = pthread_create(&td, NULL, remote_send_thread, res);
979 PJDLOG_ASSERT(error == 0);
980 error = pthread_create(&td, NULL, remote_recv_thread, res);
981 PJDLOG_ASSERT(error == 0);
982 error = pthread_create(&td, NULL, ggate_send_thread, res);
983 PJDLOG_ASSERT(error == 0);
985 (void)sync_thread(res);
989 reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio, const char *fmt, ...)
995 (void)vsnprintf(msg, sizeof(msg), fmt, ap);
997 switch (ggio->gctl_cmd) {
999 (void)snprlcat(msg, sizeof(msg), "READ(%ju, %ju).",
1000 (uintmax_t)ggio->gctl_offset,
1001 (uintmax_t)ggio->gctl_length);
1004 (void)snprlcat(msg, sizeof(msg), "DELETE(%ju, %ju).",
1005 (uintmax_t)ggio->gctl_offset,
1006 (uintmax_t)ggio->gctl_length);
1009 (void)snprlcat(msg, sizeof(msg), "FLUSH.");
1012 (void)snprlcat(msg, sizeof(msg), "WRITE(%ju, %ju).",
1013 (uintmax_t)ggio->gctl_offset,
1014 (uintmax_t)ggio->gctl_length);
1017 (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%u).",
1018 (unsigned int)ggio->gctl_cmd);
1021 pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
1025 remote_close(struct hast_resource *res, int ncomp)
1028 rw_wlock(&hio_remote_lock[ncomp]);
1030 * Check for a race between dropping rlock and acquiring wlock -
1031 * another thread can close connection in-between.
1033 if (!ISCONNECTED(res, ncomp)) {
1034 PJDLOG_ASSERT(res->hr_remotein == NULL);
1035 PJDLOG_ASSERT(res->hr_remoteout == NULL);
1036 rw_unlock(&hio_remote_lock[ncomp]);
1040 PJDLOG_ASSERT(res->hr_remotein != NULL);
1041 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1043 pjdlog_debug(2, "Closing incoming connection to %s.",
1044 res->hr_remoteaddr);
1045 proto_close(res->hr_remotein);
1046 res->hr_remotein = NULL;
1047 pjdlog_debug(2, "Closing outgoing connection to %s.",
1048 res->hr_remoteaddr);
1049 proto_close(res->hr_remoteout);
1050 res->hr_remoteout = NULL;
1052 rw_unlock(&hio_remote_lock[ncomp]);
1054 pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
1057 * Stop synchronization if in-progress.
1061 event_send(res, EVENT_DISCONNECT);
1065 * Acknowledge write completion to the kernel, but don't update activemap yet.
1068 write_complete(struct hast_resource *res, struct hio *hio)
1070 struct g_gate_ctl_io *ggio;
1073 PJDLOG_ASSERT(!hio->hio_done);
1075 ggio = &hio->hio_ggio;
1076 PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE);
1079 * Bump local count if this is first write after
1080 * connection failure with remote node.
1083 rw_rlock(&hio_remote_lock[ncomp]);
1084 if (!ISCONNECTED(res, ncomp)) {
1085 mtx_lock(&metadata_lock);
1086 if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) {
1087 res->hr_primary_localcnt++;
1088 pjdlog_debug(1, "Increasing localcnt to %ju.",
1089 (uintmax_t)res->hr_primary_localcnt);
1090 (void)metadata_write(res);
1092 mtx_unlock(&metadata_lock);
1094 rw_unlock(&hio_remote_lock[ncomp]);
1095 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1)
1096 primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
1097 hio->hio_done = true;
1101 * Thread receives ggate I/O requests from the kernel and passes them to
1102 * appropriate threads:
1103 * WRITE - always goes to both local_send and remote_send threads
1104 * READ (when the block is up-to-date on local component) -
1105 * only local_send thread
1106 * READ (when the block isn't up-to-date on local component) -
1107 * only remote_send thread
1108 * DELETE - always goes to both local_send and remote_send threads
1109 * FLUSH - always goes to both local_send and remote_send threads
1112 ggate_recv_thread(void *arg)
1114 struct hast_resource *res = arg;
1115 struct g_gate_ctl_io *ggio;
1117 unsigned int ii, ncomp, ncomps;
1121 pjdlog_debug(2, "ggate_recv: Taking free request.");
1122 QUEUE_TAKE2(hio, free);
1123 pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
1124 ggio = &hio->hio_ggio;
1125 ggio->gctl_unit = res->hr_ggateunit;
1126 ggio->gctl_length = MAXPHYS;
1127 ggio->gctl_error = 0;
1128 hio->hio_done = false;
1129 hio->hio_replication = res->hr_replication;
1131 "ggate_recv: (%p) Waiting for request from the kernel.",
1133 if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) {
1134 if (sigexit_received)
1136 primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
1138 error = ggio->gctl_error;
1143 /* Exit gracefully. */
1144 if (!sigexit_received) {
1146 "ggate_recv: (%p) Received cancel from the kernel.",
1148 pjdlog_info("Received cancel from the kernel, exiting.");
1153 * Buffer too small? Impossible, we allocate MAXPHYS
1154 * bytes - request can't be bigger than that.
1159 primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
1164 ncomps = HAST_NCOMPONENTS;
1166 for (ii = 0; ii < ncomps; ii++)
1167 hio->hio_errors[ii] = EINVAL;
1168 reqlog(LOG_DEBUG, 2, ggio,
1169 "ggate_recv: (%p) Request received from the kernel: ",
1173 * Inform all components about new write request.
1174 * For read request prefer local component unless the given
1175 * range is out-of-date, then use remote component.
1177 switch (ggio->gctl_cmd) {
1179 res->hr_stat_read++;
1181 mtx_lock(&metadata_lock);
1182 if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
1183 res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
1185 * This range is up-to-date on local component,
1186 * so handle request locally.
1188 /* Local component is 0 for now. */
1190 } else /* if (res->hr_syncsrc ==
1191 HAST_SYNCSRC_SECONDARY) */ {
1192 PJDLOG_ASSERT(res->hr_syncsrc ==
1193 HAST_SYNCSRC_SECONDARY);
1195 * This range is out-of-date on local component,
1196 * so send request to the remote node.
1198 /* Remote component is 1 for now. */
1201 mtx_unlock(&metadata_lock);
1204 res->hr_stat_write++;
1205 if (res->hr_resuid == 0 &&
1206 res->hr_primary_localcnt == 0) {
1207 /* This is first write. */
1208 res->hr_primary_localcnt = 1;
1211 mtx_lock(&range_lock);
1212 if (rangelock_islocked(range_sync,
1213 ggio->gctl_offset, ggio->gctl_length)) {
1215 "regular: Range offset=%jd length=%zu locked.",
1216 (intmax_t)ggio->gctl_offset,
1217 (size_t)ggio->gctl_length);
1218 range_regular_wait = true;
1219 cv_wait(&range_regular_cond, &range_lock);
1220 range_regular_wait = false;
1221 mtx_unlock(&range_lock);
1224 if (rangelock_add(range_regular,
1225 ggio->gctl_offset, ggio->gctl_length) == -1) {
1226 mtx_unlock(&range_lock);
1228 "regular: Range offset=%jd length=%zu is already locked, waiting.",
1229 (intmax_t)ggio->gctl_offset,
1230 (size_t)ggio->gctl_length);
1234 mtx_unlock(&range_lock);
1237 mtx_lock(&res->hr_amp_lock);
1238 if (activemap_write_start(res->hr_amp,
1239 ggio->gctl_offset, ggio->gctl_length)) {
1240 res->hr_stat_activemap_update++;
1241 (void)hast_activemap_flush(res);
1243 mtx_unlock(&res->hr_amp_lock);
1246 res->hr_stat_delete++;
1249 res->hr_stat_flush++;
1253 "ggate_recv: (%p) Moving request to the send queues.", hio);
1254 refcount_init(&hio->hio_countdown, ncomps);
1255 for (ii = ncomp; ii < ncomp + ncomps; ii++)
1256 QUEUE_INSERT1(hio, send, ii);
1263 * Thread reads from or writes to local component.
1264 * If local read fails, it redirects it to remote_send thread.
1267 local_send_thread(void *arg)
1269 struct hast_resource *res = arg;
1270 struct g_gate_ctl_io *ggio;
1272 unsigned int ncomp, rncomp;
1275 /* Local component is 0 for now. */
1277 /* Remote component is 1 for now. */
1281 pjdlog_debug(2, "local_send: Taking request.");
1282 QUEUE_TAKE1(hio, send, ncomp, 0);
1283 pjdlog_debug(2, "local_send: (%p) Got request.", hio);
1284 ggio = &hio->hio_ggio;
1285 switch (ggio->gctl_cmd) {
1287 ret = pread(res->hr_localfd, ggio->gctl_data,
1289 ggio->gctl_offset + res->hr_localoff);
1290 if (ret == ggio->gctl_length)
1291 hio->hio_errors[ncomp] = 0;
1292 else if (!ISSYNCREQ(hio)) {
1294 * If READ failed, try to read from remote node.
1297 reqlog(LOG_WARNING, 0, ggio,
1298 "Local request failed (%s), trying remote node. ",
1300 } else if (ret != ggio->gctl_length) {
1301 reqlog(LOG_WARNING, 0, ggio,
1302 "Local request failed (%zd != %jd), trying remote node. ",
1303 ret, (intmax_t)ggio->gctl_length);
1305 QUEUE_INSERT1(hio, send, rncomp);
1310 ret = pwrite(res->hr_localfd, ggio->gctl_data,
1312 ggio->gctl_offset + res->hr_localoff);
1314 hio->hio_errors[ncomp] = errno;
1315 reqlog(LOG_WARNING, 0, ggio,
1316 "Local request failed (%s): ",
1318 } else if (ret != ggio->gctl_length) {
1319 hio->hio_errors[ncomp] = EIO;
1320 reqlog(LOG_WARNING, 0, ggio,
1321 "Local request failed (%zd != %jd): ",
1322 ret, (intmax_t)ggio->gctl_length);
1324 hio->hio_errors[ncomp] = 0;
1325 if (hio->hio_replication ==
1326 HAST_REPLICATION_ASYNC &&
1328 ggio->gctl_error = 0;
1329 write_complete(res, hio);
1334 ret = g_delete(res->hr_localfd,
1335 ggio->gctl_offset + res->hr_localoff,
1338 hio->hio_errors[ncomp] = errno;
1339 reqlog(LOG_WARNING, 0, ggio,
1340 "Local request failed (%s): ",
1343 hio->hio_errors[ncomp] = 0;
1347 if (!res->hr_localflush) {
1352 ret = g_flush(res->hr_localfd);
1354 if (errno == EOPNOTSUPP)
1355 res->hr_localflush = false;
1356 hio->hio_errors[ncomp] = errno;
1357 reqlog(LOG_WARNING, 0, ggio,
1358 "Local request failed (%s): ",
1361 hio->hio_errors[ncomp] = 0;
1365 if (!refcount_release(&hio->hio_countdown))
1367 if (ISSYNCREQ(hio)) {
1368 mtx_lock(&sync_lock);
1370 mtx_unlock(&sync_lock);
1371 cv_signal(&sync_cond);
1374 "local_send: (%p) Moving request to the done queue.",
1376 QUEUE_INSERT2(hio, done);
1384 keepalive_send(struct hast_resource *res, unsigned int ncomp)
1388 rw_rlock(&hio_remote_lock[ncomp]);
1390 if (!ISCONNECTED(res, ncomp)) {
1391 rw_unlock(&hio_remote_lock[ncomp]);
1395 PJDLOG_ASSERT(res->hr_remotein != NULL);
1396 PJDLOG_ASSERT(res->hr_remoteout != NULL);
1399 nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1400 if (nv_error(nv) != 0) {
1401 rw_unlock(&hio_remote_lock[ncomp]);
1404 "keepalive_send: Unable to prepare header to send.");
1407 if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
1408 rw_unlock(&hio_remote_lock[ncomp]);
1409 pjdlog_common(LOG_DEBUG, 1, errno,
1410 "keepalive_send: Unable to send request");
1412 remote_close(res, ncomp);
1416 rw_unlock(&hio_remote_lock[ncomp]);
1418 pjdlog_debug(2, "keepalive_send: Request sent.");
1422 * Thread sends request to secondary node.
1425 remote_send_thread(void *arg)
1427 struct hast_resource *res = arg;
1428 struct g_gate_ctl_io *ggio;
1429 time_t lastcheck, now;
1434 uint64_t offset, length;
1438 /* Remote component is 1 for now. */
1440 lastcheck = time(NULL);
1443 pjdlog_debug(2, "remote_send: Taking request.");
1444 QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1447 if (lastcheck + HAST_KEEPALIVE <= now) {
1448 keepalive_send(res, ncomp);
1453 pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
1454 ggio = &hio->hio_ggio;
1455 switch (ggio->gctl_cmd) {
1459 offset = ggio->gctl_offset;
1460 length = ggio->gctl_length;
1464 data = ggio->gctl_data;
1465 offset = ggio->gctl_offset;
1466 length = ggio->gctl_length;
1471 offset = ggio->gctl_offset;
1472 length = ggio->gctl_length;
1481 PJDLOG_ABORT("invalid condition");
1484 nv_add_uint8(nv, cmd, "cmd");
1485 nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
1486 nv_add_uint64(nv, offset, "offset");
1487 nv_add_uint64(nv, length, "length");
1488 if (nv_error(nv) != 0) {
1489 hio->hio_errors[ncomp] = nv_error(nv);
1491 "remote_send: (%p) Unable to prepare header to send.",
1493 reqlog(LOG_ERR, 0, ggio,
1494 "Unable to prepare header to send (%s): ",
1495 strerror(nv_error(nv)));
1496 /* Move failed request immediately to the done queue. */
1500 * Protect connection from disappearing.
1502 rw_rlock(&hio_remote_lock[ncomp]);
1503 if (!ISCONNECTED(res, ncomp)) {
1504 rw_unlock(&hio_remote_lock[ncomp]);
1505 hio->hio_errors[ncomp] = ENOTCONN;
1509 * Move the request to recv queue before sending it, because
1510 * in different order we can get reply before we move request
1514 "remote_send: (%p) Moving request to the recv queue.",
1516 mtx_lock(&hio_recv_list_lock[ncomp]);
1517 wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
1518 TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
1519 mtx_unlock(&hio_recv_list_lock[ncomp]);
1520 if (hast_proto_send(res, res->hr_remoteout, nv, data,
1521 data != NULL ? length : 0) == -1) {
1522 hio->hio_errors[ncomp] = errno;
1523 rw_unlock(&hio_remote_lock[ncomp]);
1525 "remote_send: (%p) Unable to send request.", hio);
1526 reqlog(LOG_ERR, 0, ggio,
1527 "Unable to send request (%s): ",
1528 strerror(hio->hio_errors[ncomp]));
1529 remote_close(res, ncomp);
1531 * Take request back from the receive queue and move
1532 * it immediately to the done queue.
1534 mtx_lock(&hio_recv_list_lock[ncomp]);
1535 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1537 mtx_unlock(&hio_recv_list_lock[ncomp]);
1540 rw_unlock(&hio_remote_lock[ncomp]);
1543 cv_signal(&hio_recv_list_cond[ncomp]);
1547 if (ISSYNCREQ(hio)) {
1548 if (!refcount_release(&hio->hio_countdown))
1550 mtx_lock(&sync_lock);
1552 mtx_unlock(&sync_lock);
1553 cv_signal(&sync_cond);
1556 if (ggio->gctl_cmd == BIO_WRITE) {
1557 mtx_lock(&res->hr_amp_lock);
1558 if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
1559 ggio->gctl_length)) {
1560 (void)hast_activemap_flush(res);
1562 mtx_unlock(&res->hr_amp_lock);
1564 if (!refcount_release(&hio->hio_countdown))
1567 "remote_send: (%p) Moving request to the done queue.",
1569 QUEUE_INSERT2(hio, done);
1576 * Thread receives answer from secondary node and passes it to ggate_send
1580 remote_recv_thread(void *arg)
1582 struct hast_resource *res = arg;
1583 struct g_gate_ctl_io *ggio;
1590 /* Remote component is 1 for now. */
1594 /* Wait until there is anything to receive. */
1595 mtx_lock(&hio_recv_list_lock[ncomp]);
1596 while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
1597 pjdlog_debug(2, "remote_recv: No requests, waiting.");
1598 cv_wait(&hio_recv_list_cond[ncomp],
1599 &hio_recv_list_lock[ncomp]);
1601 mtx_unlock(&hio_recv_list_lock[ncomp]);
1603 rw_rlock(&hio_remote_lock[ncomp]);
1604 if (!ISCONNECTED(res, ncomp)) {
1605 rw_unlock(&hio_remote_lock[ncomp]);
1607 * Connection is dead, so move all pending requests to
1608 * the done queue (one-by-one).
1610 mtx_lock(&hio_recv_list_lock[ncomp]);
1611 hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
1612 PJDLOG_ASSERT(hio != NULL);
1613 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1615 mtx_unlock(&hio_recv_list_lock[ncomp]);
1618 if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
1619 pjdlog_errno(LOG_ERR,
1620 "Unable to receive reply header");
1621 rw_unlock(&hio_remote_lock[ncomp]);
1622 remote_close(res, ncomp);
1625 rw_unlock(&hio_remote_lock[ncomp]);
1626 seq = nv_get_uint64(nv, "seq");
1628 pjdlog_error("Header contains no 'seq' field.");
1632 mtx_lock(&hio_recv_list_lock[ncomp]);
1633 TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
1634 if (hio->hio_ggio.gctl_seq == seq) {
1635 TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
1640 mtx_unlock(&hio_recv_list_lock[ncomp]);
1642 pjdlog_error("Found no request matching received 'seq' field (%ju).",
1647 ggio = &hio->hio_ggio;
1648 error = nv_get_int16(nv, "error");
1650 /* Request failed on remote side. */
1651 hio->hio_errors[ncomp] = error;
1652 reqlog(LOG_WARNING, 0, ggio,
1653 "Remote request failed (%s): ", strerror(error));
1657 switch (ggio->gctl_cmd) {
1659 rw_rlock(&hio_remote_lock[ncomp]);
1660 if (!ISCONNECTED(res, ncomp)) {
1661 rw_unlock(&hio_remote_lock[ncomp]);
1665 if (hast_proto_recv_data(res, res->hr_remotein, nv,
1666 ggio->gctl_data, ggio->gctl_length) == -1) {
1667 hio->hio_errors[ncomp] = errno;
1668 pjdlog_errno(LOG_ERR,
1669 "Unable to receive reply data");
1670 rw_unlock(&hio_remote_lock[ncomp]);
1672 remote_close(res, ncomp);
1675 rw_unlock(&hio_remote_lock[ncomp]);
1682 PJDLOG_ABORT("invalid condition");
1684 hio->hio_errors[ncomp] = 0;
1687 if (!refcount_release(&hio->hio_countdown))
1689 if (ISSYNCREQ(hio)) {
1690 mtx_lock(&sync_lock);
1692 mtx_unlock(&sync_lock);
1693 cv_signal(&sync_cond);
1696 "remote_recv: (%p) Moving request to the done queue.",
1698 QUEUE_INSERT2(hio, done);
1706 * Thread sends answer to the kernel.
1709 ggate_send_thread(void *arg)
1711 struct hast_resource *res = arg;
1712 struct g_gate_ctl_io *ggio;
1714 unsigned int ii, ncomps;
1716 ncomps = HAST_NCOMPONENTS;
1719 pjdlog_debug(2, "ggate_send: Taking request.");
1720 QUEUE_TAKE2(hio, done);
1721 pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
1722 ggio = &hio->hio_ggio;
1723 for (ii = 0; ii < ncomps; ii++) {
1724 if (hio->hio_errors[ii] == 0) {
1726 * One successful request is enough to declare
1729 ggio->gctl_error = 0;
1735 * None of the requests were successful.
1736 * Use the error from local component except the
1737 * case when we did only remote request.
1739 if (ggio->gctl_cmd == BIO_READ &&
1740 res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1741 ggio->gctl_error = hio->hio_errors[1];
1743 ggio->gctl_error = hio->hio_errors[0];
1745 if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
1746 mtx_lock(&res->hr_amp_lock);
1747 if (activemap_write_complete(res->hr_amp,
1748 ggio->gctl_offset, ggio->gctl_length)) {
1749 res->hr_stat_activemap_update++;
1750 (void)hast_activemap_flush(res);
1752 mtx_unlock(&res->hr_amp_lock);
1754 if (ggio->gctl_cmd == BIO_WRITE) {
1756 * Unlock range we locked.
1758 mtx_lock(&range_lock);
1759 rangelock_del(range_regular, ggio->gctl_offset,
1761 if (range_sync_wait)
1762 cv_signal(&range_sync_cond);
1763 mtx_unlock(&range_lock);
1765 write_complete(res, hio);
1767 if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
1768 primary_exit(EX_OSERR,
1769 "G_GATE_CMD_DONE failed");
1773 "ggate_send: (%p) Moving request to the free queue.", hio);
1774 QUEUE_INSERT2(hio, free);
1781 * Thread synchronize local and remote components.
1784 sync_thread(void *arg __unused)
1786 struct hast_resource *res = arg;
1788 struct g_gate_ctl_io *ggio;
1789 struct timeval tstart, tend, tdiff;
1790 unsigned int ii, ncomp, ncomps;
1791 off_t offset, length, synced;
1795 ncomps = HAST_NCOMPONENTS;
1801 mtx_lock(&sync_lock);
1802 if (offset >= 0 && !sync_inprogress) {
1803 gettimeofday(&tend, NULL);
1804 timersub(&tend, &tstart, &tdiff);
1805 pjdlog_info("Synchronization interrupted after %#.0T. "
1806 "%NB synchronized so far.", &tdiff,
1808 event_send(res, EVENT_SYNCINTR);
1810 while (!sync_inprogress) {
1813 cv_wait(&sync_cond, &sync_lock);
1815 mtx_unlock(&sync_lock);
1817 * Obtain offset at which we should synchronize.
1818 * Rewind synchronization if needed.
1820 mtx_lock(&res->hr_amp_lock);
1822 activemap_sync_rewind(res->hr_amp);
1823 offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
1824 if (syncext != -1) {
1826 * We synchronized entire syncext extent, we can mark
1829 if (activemap_extent_complete(res->hr_amp, syncext))
1830 (void)hast_activemap_flush(res);
1832 mtx_unlock(&res->hr_amp_lock);
1836 pjdlog_info("Nodes are in sync.");
1838 pjdlog_info("Synchronization started. %NB to go.",
1839 (intmax_t)(res->hr_extentsize *
1840 activemap_ndirty(res->hr_amp)));
1841 event_send(res, EVENT_SYNCSTART);
1842 gettimeofday(&tstart, NULL);
1847 pjdlog_debug(1, "Nothing to synchronize.");
1849 * Synchronization complete, make both localcnt and
1853 rw_rlock(&hio_remote_lock[ncomp]);
1854 if (ISCONNECTED(res, ncomp)) {
1858 gettimeofday(&tend, NULL);
1859 timersub(&tend, &tstart, &tdiff);
1860 bps = (int64_t)((double)synced /
1861 ((double)tdiff.tv_sec +
1862 (double)tdiff.tv_usec / 1000000));
1863 pjdlog_info("Synchronization complete. "
1864 "%NB synchronized in %#.0lT (%NB/sec).",
1865 (intmax_t)synced, &tdiff,
1867 event_send(res, EVENT_SYNCDONE);
1869 mtx_lock(&metadata_lock);
1870 res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
1871 res->hr_primary_localcnt =
1872 res->hr_secondary_remotecnt;
1873 res->hr_primary_remotecnt =
1874 res->hr_secondary_localcnt;
1876 "Setting localcnt to %ju and remotecnt to %ju.",
1877 (uintmax_t)res->hr_primary_localcnt,
1878 (uintmax_t)res->hr_primary_remotecnt);
1879 (void)metadata_write(res);
1880 mtx_unlock(&metadata_lock);
1882 rw_unlock(&hio_remote_lock[ncomp]);
1885 pjdlog_debug(2, "sync: Taking free request.");
1886 QUEUE_TAKE2(hio, free);
1887 pjdlog_debug(2, "sync: (%p) Got free request.", hio);
1889 * Lock the range we are going to synchronize. We don't want
1890 * race where someone writes between our read and write.
1893 mtx_lock(&range_lock);
1894 if (rangelock_islocked(range_regular, offset, length)) {
1896 "sync: Range offset=%jd length=%jd locked.",
1897 (intmax_t)offset, (intmax_t)length);
1898 range_sync_wait = true;
1899 cv_wait(&range_sync_cond, &range_lock);
1900 range_sync_wait = false;
1901 mtx_unlock(&range_lock);
1904 if (rangelock_add(range_sync, offset, length) == -1) {
1905 mtx_unlock(&range_lock);
1907 "sync: Range offset=%jd length=%jd is already locked, waiting.",
1908 (intmax_t)offset, (intmax_t)length);
1912 mtx_unlock(&range_lock);
1916 * First read the data from synchronization source.
1919 ggio = &hio->hio_ggio;
1920 ggio->gctl_cmd = BIO_READ;
1921 ggio->gctl_offset = offset;
1922 ggio->gctl_length = length;
1923 ggio->gctl_error = 0;
1924 hio->hio_done = false;
1925 hio->hio_replication = res->hr_replication;
1926 for (ii = 0; ii < ncomps; ii++)
1927 hio->hio_errors[ii] = EINVAL;
1928 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
1930 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
1932 mtx_lock(&metadata_lock);
1933 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
1935 * This range is up-to-date on local component,
1936 * so handle request locally.
1938 /* Local component is 0 for now. */
1940 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
1941 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
1943 * This range is out-of-date on local component,
1944 * so send request to the remote node.
1946 /* Remote component is 1 for now. */
1949 mtx_unlock(&metadata_lock);
1950 refcount_init(&hio->hio_countdown, 1);
1951 QUEUE_INSERT1(hio, send, ncomp);
1954 * Let's wait for READ to finish.
1956 mtx_lock(&sync_lock);
1957 while (!ISSYNCREQDONE(hio))
1958 cv_wait(&sync_cond, &sync_lock);
1959 mtx_unlock(&sync_lock);
1961 if (hio->hio_errors[ncomp] != 0) {
1962 pjdlog_error("Unable to read synchronization data: %s.",
1963 strerror(hio->hio_errors[ncomp]));
1968 * We read the data from synchronization source, now write it
1969 * to synchronization target.
1972 ggio->gctl_cmd = BIO_WRITE;
1973 for (ii = 0; ii < ncomps; ii++)
1974 hio->hio_errors[ii] = EINVAL;
1975 reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
1977 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
1979 mtx_lock(&metadata_lock);
1980 if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
1982 * This range is up-to-date on local component,
1983 * so we update remote component.
1985 /* Remote component is 1 for now. */
1987 } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
1988 PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
1990 * This range is out-of-date on local component,
1993 /* Local component is 0 for now. */
1996 mtx_unlock(&metadata_lock);
1998 pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
2000 refcount_init(&hio->hio_countdown, 1);
2001 QUEUE_INSERT1(hio, send, ncomp);
2004 * Let's wait for WRITE to finish.
2006 mtx_lock(&sync_lock);
2007 while (!ISSYNCREQDONE(hio))
2008 cv_wait(&sync_cond, &sync_lock);
2009 mtx_unlock(&sync_lock);
2011 if (hio->hio_errors[ncomp] != 0) {
2012 pjdlog_error("Unable to write synchronization data: %s.",
2013 strerror(hio->hio_errors[ncomp]));
2019 mtx_lock(&range_lock);
2020 rangelock_del(range_sync, offset, length);
2021 if (range_regular_wait)
2022 cv_signal(&range_regular_cond);
2023 mtx_unlock(&range_lock);
2024 pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
2026 QUEUE_INSERT2(hio, free);
2033 primary_config_reload(struct hast_resource *res, struct nv *nv)
2035 unsigned int ii, ncomps;
2039 pjdlog_info("Reloading configuration...");
2041 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
2042 PJDLOG_ASSERT(gres == res);
2043 nv_assert(nv, "remoteaddr");
2044 nv_assert(nv, "sourceaddr");
2045 nv_assert(nv, "replication");
2046 nv_assert(nv, "checksum");
2047 nv_assert(nv, "compression");
2048 nv_assert(nv, "timeout");
2049 nv_assert(nv, "exec");
2050 nv_assert(nv, "metaflush");
2052 ncomps = HAST_NCOMPONENTS;
2054 #define MODIFIED_REMOTEADDR 0x01
2055 #define MODIFIED_SOURCEADDR 0x02
2056 #define MODIFIED_REPLICATION 0x04
2057 #define MODIFIED_CHECKSUM 0x08
2058 #define MODIFIED_COMPRESSION 0x10
2059 #define MODIFIED_TIMEOUT 0x20
2060 #define MODIFIED_EXEC 0x40
2061 #define MODIFIED_METAFLUSH 0x80
2064 vstr = nv_get_string(nv, "remoteaddr");
2065 if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
2067 * Don't copy res->hr_remoteaddr to gres just yet.
2068 * We want remote_close() to log disconnect from the old
2069 * addresses, not from the new ones.
2071 modified |= MODIFIED_REMOTEADDR;
2073 vstr = nv_get_string(nv, "sourceaddr");
2074 if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
2075 strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
2076 modified |= MODIFIED_SOURCEADDR;
2078 vint = nv_get_int32(nv, "replication");
2079 if (gres->hr_replication != vint) {
2080 gres->hr_replication = vint;
2081 modified |= MODIFIED_REPLICATION;
2083 vint = nv_get_int32(nv, "checksum");
2084 if (gres->hr_checksum != vint) {
2085 gres->hr_checksum = vint;
2086 modified |= MODIFIED_CHECKSUM;
2088 vint = nv_get_int32(nv, "compression");
2089 if (gres->hr_compression != vint) {
2090 gres->hr_compression = vint;
2091 modified |= MODIFIED_COMPRESSION;
2093 vint = nv_get_int32(nv, "timeout");
2094 if (gres->hr_timeout != vint) {
2095 gres->hr_timeout = vint;
2096 modified |= MODIFIED_TIMEOUT;
2098 vstr = nv_get_string(nv, "exec");
2099 if (strcmp(gres->hr_exec, vstr) != 0) {
2100 strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
2101 modified |= MODIFIED_EXEC;
2103 vint = nv_get_int32(nv, "metaflush");
2104 if (gres->hr_metaflush != vint) {
2105 gres->hr_metaflush = vint;
2106 modified |= MODIFIED_METAFLUSH;
2110 * Change timeout for connected sockets.
2111 * Don't bother if we need to reconnect.
2113 if ((modified & MODIFIED_TIMEOUT) != 0 &&
2114 (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
2115 for (ii = 0; ii < ncomps; ii++) {
2118 rw_rlock(&hio_remote_lock[ii]);
2119 if (!ISCONNECTED(gres, ii)) {
2120 rw_unlock(&hio_remote_lock[ii]);
2123 rw_unlock(&hio_remote_lock[ii]);
2124 if (proto_timeout(gres->hr_remotein,
2125 gres->hr_timeout) == -1) {
2126 pjdlog_errno(LOG_WARNING,
2127 "Unable to set connection timeout");
2129 if (proto_timeout(gres->hr_remoteout,
2130 gres->hr_timeout) == -1) {
2131 pjdlog_errno(LOG_WARNING,
2132 "Unable to set connection timeout");
2136 if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
2137 for (ii = 0; ii < ncomps; ii++) {
2140 remote_close(gres, ii);
2142 if (modified & MODIFIED_REMOTEADDR) {
2143 vstr = nv_get_string(nv, "remoteaddr");
2144 strlcpy(gres->hr_remoteaddr, vstr,
2145 sizeof(gres->hr_remoteaddr));
2148 #undef MODIFIED_REMOTEADDR
2149 #undef MODIFIED_SOURCEADDR
2150 #undef MODIFIED_REPLICATION
2151 #undef MODIFIED_CHECKSUM
2152 #undef MODIFIED_COMPRESSION
2153 #undef MODIFIED_TIMEOUT
2154 #undef MODIFIED_EXEC
2155 #undef MODIFIED_METAFLUSH
2157 pjdlog_info("Configuration reloaded successfully.");
2161 guard_one(struct hast_resource *res, unsigned int ncomp)
2163 struct proto_conn *in, *out;
2165 if (!ISREMOTE(ncomp))
2168 rw_rlock(&hio_remote_lock[ncomp]);
2170 if (!real_remote(res)) {
2171 rw_unlock(&hio_remote_lock[ncomp]);
2175 if (ISCONNECTED(res, ncomp)) {
2176 PJDLOG_ASSERT(res->hr_remotein != NULL);
2177 PJDLOG_ASSERT(res->hr_remoteout != NULL);
2178 rw_unlock(&hio_remote_lock[ncomp]);
2179 pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2180 res->hr_remoteaddr);
2184 PJDLOG_ASSERT(res->hr_remotein == NULL);
2185 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2187 * Upgrade the lock. It doesn't have to be atomic as no other thread
2188 * can change connection status from disconnected to connected.
2190 rw_unlock(&hio_remote_lock[ncomp]);
2191 pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2192 res->hr_remoteaddr);
2194 if (init_remote(res, &in, &out) == 0) {
2195 rw_wlock(&hio_remote_lock[ncomp]);
2196 PJDLOG_ASSERT(res->hr_remotein == NULL);
2197 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2198 PJDLOG_ASSERT(in != NULL && out != NULL);
2199 res->hr_remotein = in;
2200 res->hr_remoteout = out;
2201 rw_unlock(&hio_remote_lock[ncomp]);
2202 pjdlog_info("Successfully reconnected to %s.",
2203 res->hr_remoteaddr);
2206 /* Both connections should be NULL. */
2207 PJDLOG_ASSERT(res->hr_remotein == NULL);
2208 PJDLOG_ASSERT(res->hr_remoteout == NULL);
2209 PJDLOG_ASSERT(in == NULL && out == NULL);
2210 pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2211 res->hr_remoteaddr);
2216 * Thread guards remote connections and reconnects when needed, handles
2220 guard_thread(void *arg)
2222 struct hast_resource *res = arg;
2223 unsigned int ii, ncomps;
2224 struct timespec timeout;
2225 time_t lastcheck, now;
2229 ncomps = HAST_NCOMPONENTS;
2230 lastcheck = time(NULL);
2232 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
2233 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
2234 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
2236 timeout.tv_sec = HAST_KEEPALIVE;
2237 timeout.tv_nsec = 0;
2244 sigexit_received = true;
2245 primary_exitx(EX_OK,
2246 "Termination signal received, exiting.");
2253 * Don't check connections until we fully started,
2254 * as we may still be looping, waiting for remote node
2255 * to switch from primary to secondary.
2258 pjdlog_debug(2, "remote_guard: Checking connections.");
2260 if (lastcheck + HAST_KEEPALIVE <= now) {
2261 for (ii = 0; ii < ncomps; ii++)
2266 signo = sigtimedwait(&mask, NULL, &timeout);