132115b10SPawel Jakub Dawidek /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
432115b10SPawel Jakub Dawidek * Copyright (c) 2009 The FreeBSD Foundation
51fee97b0SPawel Jakub Dawidek * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
632115b10SPawel Jakub Dawidek * All rights reserved.
732115b10SPawel Jakub Dawidek *
832115b10SPawel Jakub Dawidek * This software was developed by Pawel Jakub Dawidek under sponsorship from
932115b10SPawel Jakub Dawidek * the FreeBSD Foundation.
1032115b10SPawel Jakub Dawidek *
1132115b10SPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without
1232115b10SPawel Jakub Dawidek * modification, are permitted provided that the following conditions
1332115b10SPawel Jakub Dawidek * are met:
1432115b10SPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright
1532115b10SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer.
1632115b10SPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright
1732115b10SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the
1832115b10SPawel Jakub Dawidek * documentation and/or other materials provided with the distribution.
1932115b10SPawel Jakub Dawidek *
2032115b10SPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
2132115b10SPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2232115b10SPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2332115b10SPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2432115b10SPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2532115b10SPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2632115b10SPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2732115b10SPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2832115b10SPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2932115b10SPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3032115b10SPawel Jakub Dawidek * SUCH DAMAGE.
3132115b10SPawel Jakub Dawidek */
3232115b10SPawel Jakub Dawidek
3332115b10SPawel Jakub Dawidek #include <sys/types.h>
3432115b10SPawel Jakub Dawidek #include <sys/time.h>
3532115b10SPawel Jakub Dawidek #include <sys/bio.h>
3632115b10SPawel Jakub Dawidek #include <sys/disk.h>
3732115b10SPawel Jakub Dawidek #include <sys/stat.h>
3832115b10SPawel Jakub Dawidek
3932115b10SPawel Jakub Dawidek #include <geom/gate/g_gate.h>
4032115b10SPawel Jakub Dawidek
4132115b10SPawel Jakub Dawidek #include <err.h>
4232115b10SPawel Jakub Dawidek #include <errno.h>
4332115b10SPawel Jakub Dawidek #include <fcntl.h>
4432115b10SPawel Jakub Dawidek #include <libgeom.h>
4532115b10SPawel Jakub Dawidek #include <pthread.h>
466d0c801eSPawel Jakub Dawidek #include <signal.h>
4732115b10SPawel Jakub Dawidek #include <stdint.h>
4832115b10SPawel Jakub Dawidek #include <stdio.h>
4932115b10SPawel Jakub Dawidek #include <string.h>
5032115b10SPawel Jakub Dawidek #include <sysexits.h>
5132115b10SPawel Jakub Dawidek #include <unistd.h>
5232115b10SPawel Jakub Dawidek
5332115b10SPawel Jakub Dawidek #include <activemap.h>
5432115b10SPawel Jakub Dawidek #include <nv.h>
5532115b10SPawel Jakub Dawidek #include <rangelock.h>
5632115b10SPawel Jakub Dawidek
5732115b10SPawel Jakub Dawidek #include "control.h"
585bdff860SPawel Jakub Dawidek #include "event.h"
5932115b10SPawel Jakub Dawidek #include "hast.h"
6032115b10SPawel Jakub Dawidek #include "hast_proto.h"
6132115b10SPawel Jakub Dawidek #include "hastd.h"
620becad39SPawel Jakub Dawidek #include "hooks.h"
6332115b10SPawel Jakub Dawidek #include "metadata.h"
6432115b10SPawel Jakub Dawidek #include "proto.h"
6532115b10SPawel Jakub Dawidek #include "pjdlog.h"
66d6e636c9SPawel Jakub Dawidek #include "refcnt.h"
6732115b10SPawel Jakub Dawidek #include "subr.h"
6832115b10SPawel Jakub Dawidek #include "synch.h"
6932115b10SPawel Jakub Dawidek
700989854dSPawel Jakub Dawidek /* The is only one remote component for now. */
710989854dSPawel Jakub Dawidek #define ISREMOTE(no) ((no) == 1)
720989854dSPawel Jakub Dawidek
7332115b10SPawel Jakub Dawidek struct hio {
7432115b10SPawel Jakub Dawidek /*
7532115b10SPawel Jakub Dawidek * Number of components we are still waiting for.
7632115b10SPawel Jakub Dawidek * When this field goes to 0, we can send the request back to the
7732115b10SPawel Jakub Dawidek * kernel. Each component has to decrease this counter by one
7832115b10SPawel Jakub Dawidek * even on failure.
7932115b10SPawel Jakub Dawidek */
806e87c151SEd Schouten refcnt_t hio_countdown;
8132115b10SPawel Jakub Dawidek /*
8232115b10SPawel Jakub Dawidek * Each component has a place to store its own error.
8332115b10SPawel Jakub Dawidek * Once the request is handled by all components we can decide if the
8432115b10SPawel Jakub Dawidek * request overall is successful or not.
8532115b10SPawel Jakub Dawidek */
8632115b10SPawel Jakub Dawidek int *hio_errors;
8732115b10SPawel Jakub Dawidek /*
880b626a28SPawel Jakub Dawidek * Structure used to communicate with GEOM Gate class.
8932115b10SPawel Jakub Dawidek */
9032115b10SPawel Jakub Dawidek struct g_gate_ctl_io hio_ggio;
9107ebc362SPawel Jakub Dawidek /*
9207ebc362SPawel Jakub Dawidek * Request was already confirmed to GEOM Gate.
9307ebc362SPawel Jakub Dawidek */
9407ebc362SPawel Jakub Dawidek bool hio_done;
9507ebc362SPawel Jakub Dawidek /*
965d69ed53SMikolaj Golub * Number of components we are still waiting before sending write
975d69ed53SMikolaj Golub * completion ack to GEOM Gate. Used for memsync.
985d69ed53SMikolaj Golub */
995d69ed53SMikolaj Golub refcnt_t hio_writecount;
1005d69ed53SMikolaj Golub /*
101*136a9bb4SElyes Haouas * Memsync request was acknowledged by remote.
1025d69ed53SMikolaj Golub */
1035d69ed53SMikolaj Golub bool hio_memsyncacked;
1045d69ed53SMikolaj Golub /*
10507ebc362SPawel Jakub Dawidek * Remember replication from the time the request was initiated,
10607ebc362SPawel Jakub Dawidek * so we won't get confused when replication changes on reload.
10707ebc362SPawel Jakub Dawidek */
10807ebc362SPawel Jakub Dawidek int hio_replication;
10932115b10SPawel Jakub Dawidek TAILQ_ENTRY(hio) *hio_next;
11032115b10SPawel Jakub Dawidek };
11132115b10SPawel Jakub Dawidek #define hio_free_next hio_next[0]
11232115b10SPawel Jakub Dawidek #define hio_done_next hio_next[0]
11332115b10SPawel Jakub Dawidek
11432115b10SPawel Jakub Dawidek /*
11532115b10SPawel Jakub Dawidek * Free list holds unused structures. When free list is empty, we have to wait
11632115b10SPawel Jakub Dawidek * until some in-progress requests are freed.
11732115b10SPawel Jakub Dawidek */
11832115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_free_list;
1196b66c350SMikolaj Golub static size_t hio_free_list_size;
12032115b10SPawel Jakub Dawidek static pthread_mutex_t hio_free_list_lock;
12132115b10SPawel Jakub Dawidek static pthread_cond_t hio_free_list_cond;
12232115b10SPawel Jakub Dawidek /*
12332115b10SPawel Jakub Dawidek * There is one send list for every component. One requests is placed on all
12432115b10SPawel Jakub Dawidek * send lists - each component gets the same request, but each component is
12532115b10SPawel Jakub Dawidek * responsible for managing his own send list.
12632115b10SPawel Jakub Dawidek */
12732115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_send_list;
1286b66c350SMikolaj Golub static size_t *hio_send_list_size;
12932115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_send_list_lock;
13032115b10SPawel Jakub Dawidek static pthread_cond_t *hio_send_list_cond;
1316b66c350SMikolaj Golub #define hio_send_local_list_size hio_send_list_size[0]
1326b66c350SMikolaj Golub #define hio_send_remote_list_size hio_send_list_size[1]
13332115b10SPawel Jakub Dawidek /*
13432115b10SPawel Jakub Dawidek * There is one recv list for every component, although local components don't
13532115b10SPawel Jakub Dawidek * use recv lists as local requests are done synchronously.
13632115b10SPawel Jakub Dawidek */
13732115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) *hio_recv_list;
1386b66c350SMikolaj Golub static size_t *hio_recv_list_size;
13932115b10SPawel Jakub Dawidek static pthread_mutex_t *hio_recv_list_lock;
14032115b10SPawel Jakub Dawidek static pthread_cond_t *hio_recv_list_cond;
1416b66c350SMikolaj Golub #define hio_recv_remote_list_size hio_recv_list_size[1]
14232115b10SPawel Jakub Dawidek /*
14332115b10SPawel Jakub Dawidek * Request is placed on done list by the slowest component (the one that
14432115b10SPawel Jakub Dawidek * decreased hio_countdown from 1 to 0).
14532115b10SPawel Jakub Dawidek */
14632115b10SPawel Jakub Dawidek static TAILQ_HEAD(, hio) hio_done_list;
1476b66c350SMikolaj Golub static size_t hio_done_list_size;
14832115b10SPawel Jakub Dawidek static pthread_mutex_t hio_done_list_lock;
14932115b10SPawel Jakub Dawidek static pthread_cond_t hio_done_list_cond;
15032115b10SPawel Jakub Dawidek /*
15132115b10SPawel Jakub Dawidek * Structure below are for interaction with sync thread.
15232115b10SPawel Jakub Dawidek */
15332115b10SPawel Jakub Dawidek static bool sync_inprogress;
15432115b10SPawel Jakub Dawidek static pthread_mutex_t sync_lock;
15532115b10SPawel Jakub Dawidek static pthread_cond_t sync_cond;
15632115b10SPawel Jakub Dawidek /*
15732115b10SPawel Jakub Dawidek * The lock below allows to synchornize access to remote connections.
15832115b10SPawel Jakub Dawidek */
15932115b10SPawel Jakub Dawidek static pthread_rwlock_t *hio_remote_lock;
16032115b10SPawel Jakub Dawidek
16132115b10SPawel Jakub Dawidek /*
16232115b10SPawel Jakub Dawidek * Lock to synchronize metadata updates. Also synchronize access to
16332115b10SPawel Jakub Dawidek * hr_primary_localcnt and hr_primary_remotecnt fields.
16432115b10SPawel Jakub Dawidek */
16532115b10SPawel Jakub Dawidek static pthread_mutex_t metadata_lock;
16632115b10SPawel Jakub Dawidek
16732115b10SPawel Jakub Dawidek /*
16832115b10SPawel Jakub Dawidek * Maximum number of outstanding I/O requests.
16932115b10SPawel Jakub Dawidek */
17032115b10SPawel Jakub Dawidek #define HAST_HIO_MAX 256
17132115b10SPawel Jakub Dawidek /*
17232115b10SPawel Jakub Dawidek * Number of components. At this point there are only two components: local
17332115b10SPawel Jakub Dawidek * and remote, but in the future it might be possible to use multiple local
17432115b10SPawel Jakub Dawidek * and remote components.
17532115b10SPawel Jakub Dawidek */
17632115b10SPawel Jakub Dawidek #define HAST_NCOMPONENTS 2
17732115b10SPawel Jakub Dawidek
17832115b10SPawel Jakub Dawidek #define ISCONNECTED(res, no) \
17932115b10SPawel Jakub Dawidek ((res)->hr_remotein != NULL && (res)->hr_remoteout != NULL)
18032115b10SPawel Jakub Dawidek
18132115b10SPawel Jakub Dawidek #define QUEUE_INSERT1(hio, name, ncomp) do { \
18232115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
1839c539971SMikolaj Golub if (TAILQ_EMPTY(&hio_##name##_list[(ncomp)])) \
1849c539971SMikolaj Golub cv_broadcast(&hio_##name##_list_cond[(ncomp)]); \
18532115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_##name##_list[(ncomp)], (hio), \
18632115b10SPawel Jakub Dawidek hio_next[(ncomp)]); \
1876b66c350SMikolaj Golub hio_##name##_list_size[(ncomp)]++; \
1889c539971SMikolaj Golub mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \
18932115b10SPawel Jakub Dawidek } while (0)
19032115b10SPawel Jakub Dawidek #define QUEUE_INSERT2(hio, name) do { \
19132115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock); \
1929c539971SMikolaj Golub if (TAILQ_EMPTY(&hio_##name##_list)) \
1939c539971SMikolaj Golub cv_broadcast(&hio_##name##_list_cond); \
19432115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\
1956b66c350SMikolaj Golub hio_##name##_list_size++; \
19632115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock); \
19732115b10SPawel Jakub Dawidek } while (0)
198448efa94SPawel Jakub Dawidek #define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \
199448efa94SPawel Jakub Dawidek bool _last; \
200448efa94SPawel Jakub Dawidek \
20132115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock[(ncomp)]); \
202448efa94SPawel Jakub Dawidek _last = false; \
203448efa94SPawel Jakub Dawidek while (((hio) = TAILQ_FIRST(&hio_##name##_list[(ncomp)])) == NULL && !_last) { \
204448efa94SPawel Jakub Dawidek cv_timedwait(&hio_##name##_list_cond[(ncomp)], \
205448efa94SPawel Jakub Dawidek &hio_##name##_list_lock[(ncomp)], (timeout)); \
206448efa94SPawel Jakub Dawidek if ((timeout) != 0) \
207448efa94SPawel Jakub Dawidek _last = true; \
20832115b10SPawel Jakub Dawidek } \
209448efa94SPawel Jakub Dawidek if (hio != NULL) { \
2106b66c350SMikolaj Golub PJDLOG_ASSERT(hio_##name##_list_size[(ncomp)] != 0); \
2116b66c350SMikolaj Golub hio_##name##_list_size[(ncomp)]--; \
21232115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_##name##_list[(ncomp)], (hio), \
21332115b10SPawel Jakub Dawidek hio_next[(ncomp)]); \
214448efa94SPawel Jakub Dawidek } \
21532115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock[(ncomp)]); \
21632115b10SPawel Jakub Dawidek } while (0)
21732115b10SPawel Jakub Dawidek #define QUEUE_TAKE2(hio, name) do { \
21832115b10SPawel Jakub Dawidek mtx_lock(&hio_##name##_list_lock); \
21932115b10SPawel Jakub Dawidek while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \
22032115b10SPawel Jakub Dawidek cv_wait(&hio_##name##_list_cond, \
22132115b10SPawel Jakub Dawidek &hio_##name##_list_lock); \
22232115b10SPawel Jakub Dawidek } \
2236b66c350SMikolaj Golub PJDLOG_ASSERT(hio_##name##_list_size != 0); \
2246b66c350SMikolaj Golub hio_##name##_list_size--; \
22532115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_##name##_list, (hio), hio_##name##_next); \
22632115b10SPawel Jakub Dawidek mtx_unlock(&hio_##name##_list_lock); \
22732115b10SPawel Jakub Dawidek } while (0)
22832115b10SPawel Jakub Dawidek
2298f04423fSMikolaj Golub #define ISFULLSYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_FULLSYNC)
2308f04423fSMikolaj Golub #define ISMEMSYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_MEMSYNC)
2318f04423fSMikolaj Golub #define ISASYNC(hio) ((hio)->hio_replication == HAST_REPLICATION_ASYNC)
2328f04423fSMikolaj Golub
233328e0f4bSPawel Jakub Dawidek #define SYNCREQ(hio) do { \
234328e0f4bSPawel Jakub Dawidek (hio)->hio_ggio.gctl_unit = -1; \
235328e0f4bSPawel Jakub Dawidek (hio)->hio_ggio.gctl_seq = 1; \
236328e0f4bSPawel Jakub Dawidek } while (0)
23732115b10SPawel Jakub Dawidek #define ISSYNCREQ(hio) ((hio)->hio_ggio.gctl_unit == -1)
23832115b10SPawel Jakub Dawidek #define SYNCREQDONE(hio) do { (hio)->hio_ggio.gctl_unit = -2; } while (0)
23932115b10SPawel Jakub Dawidek #define ISSYNCREQDONE(hio) ((hio)->hio_ggio.gctl_unit == -2)
2408f04423fSMikolaj Golub
2418f04423fSMikolaj Golub #define ISMEMSYNCWRITE(hio) (ISMEMSYNC(hio) && \
2428f04423fSMikolaj Golub (hio)->hio_ggio.gctl_cmd == BIO_WRITE && !ISSYNCREQ(hio))
24332115b10SPawel Jakub Dawidek
24432115b10SPawel Jakub Dawidek static struct hast_resource *gres;
24532115b10SPawel Jakub Dawidek
24632115b10SPawel Jakub Dawidek static pthread_mutex_t range_lock;
24732115b10SPawel Jakub Dawidek static struct rangelocks *range_regular;
24832115b10SPawel Jakub Dawidek static bool range_regular_wait;
24932115b10SPawel Jakub Dawidek static pthread_cond_t range_regular_cond;
25032115b10SPawel Jakub Dawidek static struct rangelocks *range_sync;
25132115b10SPawel Jakub Dawidek static bool range_sync_wait;
25232115b10SPawel Jakub Dawidek static pthread_cond_t range_sync_cond;
253ac0401e3SPawel Jakub Dawidek static bool fullystarted;
25432115b10SPawel Jakub Dawidek
25532115b10SPawel Jakub Dawidek static void *ggate_recv_thread(void *arg);
25632115b10SPawel Jakub Dawidek static void *local_send_thread(void *arg);
25732115b10SPawel Jakub Dawidek static void *remote_send_thread(void *arg);
25832115b10SPawel Jakub Dawidek static void *remote_recv_thread(void *arg);
25932115b10SPawel Jakub Dawidek static void *ggate_send_thread(void *arg);
26032115b10SPawel Jakub Dawidek static void *sync_thread(void *arg);
26132115b10SPawel Jakub Dawidek static void *guard_thread(void *arg);
26232115b10SPawel Jakub Dawidek
2636d0c801eSPawel Jakub Dawidek static void
output_status_aux(struct nv * nvout)2646b66c350SMikolaj Golub output_status_aux(struct nv *nvout)
2656b66c350SMikolaj Golub {
2666b66c350SMikolaj Golub
2676b66c350SMikolaj Golub nv_add_uint64(nvout, (uint64_t)hio_free_list_size,
2686b66c350SMikolaj Golub "idle_queue_size");
2696b66c350SMikolaj Golub nv_add_uint64(nvout, (uint64_t)hio_send_local_list_size,
2706b66c350SMikolaj Golub "local_queue_size");
2716b66c350SMikolaj Golub nv_add_uint64(nvout, (uint64_t)hio_send_remote_list_size,
2726b66c350SMikolaj Golub "send_queue_size");
2736b66c350SMikolaj Golub nv_add_uint64(nvout, (uint64_t)hio_recv_remote_list_size,
2746b66c350SMikolaj Golub "recv_queue_size");
2756b66c350SMikolaj Golub nv_add_uint64(nvout, (uint64_t)hio_done_list_size,
2766b66c350SMikolaj Golub "done_queue_size");
2776b66c350SMikolaj Golub }
2786b66c350SMikolaj Golub
2796b66c350SMikolaj Golub static void
cleanup(struct hast_resource * res)28032115b10SPawel Jakub Dawidek cleanup(struct hast_resource *res)
28132115b10SPawel Jakub Dawidek {
28232115b10SPawel Jakub Dawidek int rerrno;
28332115b10SPawel Jakub Dawidek
28432115b10SPawel Jakub Dawidek /* Remember errno. */
28532115b10SPawel Jakub Dawidek rerrno = errno;
28632115b10SPawel Jakub Dawidek
28732115b10SPawel Jakub Dawidek /* Destroy ggate provider if we created one. */
28832115b10SPawel Jakub Dawidek if (res->hr_ggateunit >= 0) {
28932115b10SPawel Jakub Dawidek struct g_gate_ctl_destroy ggiod;
29032115b10SPawel Jakub Dawidek
2914e47b646SPawel Jakub Dawidek bzero(&ggiod, sizeof(ggiod));
29232115b10SPawel Jakub Dawidek ggiod.gctl_version = G_GATE_VERSION;
29332115b10SPawel Jakub Dawidek ggiod.gctl_unit = res->hr_ggateunit;
29432115b10SPawel Jakub Dawidek ggiod.gctl_force = 1;
2952b1b224dSPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) == -1) {
296783ee753SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING,
297783ee753SPawel Jakub Dawidek "Unable to destroy hast/%s device",
29832115b10SPawel Jakub Dawidek res->hr_provname);
29932115b10SPawel Jakub Dawidek }
30032115b10SPawel Jakub Dawidek res->hr_ggateunit = -1;
30132115b10SPawel Jakub Dawidek }
30232115b10SPawel Jakub Dawidek
30332115b10SPawel Jakub Dawidek /* Restore errno. */
30432115b10SPawel Jakub Dawidek errno = rerrno;
30532115b10SPawel Jakub Dawidek }
30632115b10SPawel Jakub Dawidek
307e43e02f1SPawel Jakub Dawidek static __dead2 void
primary_exit(int exitcode,const char * fmt,...)30832115b10SPawel Jakub Dawidek primary_exit(int exitcode, const char *fmt, ...)
30932115b10SPawel Jakub Dawidek {
31032115b10SPawel Jakub Dawidek va_list ap;
31132115b10SPawel Jakub Dawidek
3122ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(exitcode != EX_OK);
31332115b10SPawel Jakub Dawidek va_start(ap, fmt);
31432115b10SPawel Jakub Dawidek pjdlogv_errno(LOG_ERR, fmt, ap);
31532115b10SPawel Jakub Dawidek va_end(ap);
31632115b10SPawel Jakub Dawidek cleanup(gres);
31732115b10SPawel Jakub Dawidek exit(exitcode);
31832115b10SPawel Jakub Dawidek }
31932115b10SPawel Jakub Dawidek
320e43e02f1SPawel Jakub Dawidek static __dead2 void
primary_exitx(int exitcode,const char * fmt,...)32132115b10SPawel Jakub Dawidek primary_exitx(int exitcode, const char *fmt, ...)
32232115b10SPawel Jakub Dawidek {
32332115b10SPawel Jakub Dawidek va_list ap;
32432115b10SPawel Jakub Dawidek
32532115b10SPawel Jakub Dawidek va_start(ap, fmt);
32632115b10SPawel Jakub Dawidek pjdlogv(exitcode == EX_OK ? LOG_INFO : LOG_ERR, fmt, ap);
32732115b10SPawel Jakub Dawidek va_end(ap);
32832115b10SPawel Jakub Dawidek cleanup(gres);
32932115b10SPawel Jakub Dawidek exit(exitcode);
33032115b10SPawel Jakub Dawidek }
33132115b10SPawel Jakub Dawidek
33232115b10SPawel Jakub Dawidek static int
hast_activemap_flush(struct hast_resource * res)333974a1085SEd Schouten hast_activemap_flush(struct hast_resource *res) __unlocks(res->hr_amp_lock)
33432115b10SPawel Jakub Dawidek {
33532115b10SPawel Jakub Dawidek const unsigned char *buf;
33632115b10SPawel Jakub Dawidek size_t size;
337a818a4ffSMikolaj Golub int ret;
33832115b10SPawel Jakub Dawidek
339a818a4ffSMikolaj Golub mtx_lock(&res->hr_amp_diskmap_lock);
34032115b10SPawel Jakub Dawidek buf = activemap_bitmap(res->hr_amp, &size);
341a818a4ffSMikolaj Golub mtx_unlock(&res->hr_amp_lock);
3422ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(buf != NULL);
3432ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
344a818a4ffSMikolaj Golub ret = 0;
34532115b10SPawel Jakub Dawidek if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
34632115b10SPawel Jakub Dawidek (ssize_t)size) {
347be1143efSPawel Jakub Dawidek pjdlog_errno(LOG_ERR, "Unable to flush activemap to disk");
3482adbba66SMikolaj Golub res->hr_stat_activemap_write_error++;
349a818a4ffSMikolaj Golub ret = -1;
35032115b10SPawel Jakub Dawidek }
351a818a4ffSMikolaj Golub if (ret == 0 && res->hr_metaflush == 1 &&
352a818a4ffSMikolaj Golub g_flush(res->hr_localfd) == -1) {
353518dd4c0SPawel Jakub Dawidek if (errno == EOPNOTSUPP) {
354518dd4c0SPawel Jakub Dawidek pjdlog_warning("The %s provider doesn't support flushing write cache. Disabling it.",
355518dd4c0SPawel Jakub Dawidek res->hr_localpath);
356518dd4c0SPawel Jakub Dawidek res->hr_metaflush = 0;
357518dd4c0SPawel Jakub Dawidek } else {
358518dd4c0SPawel Jakub Dawidek pjdlog_errno(LOG_ERR,
359518dd4c0SPawel Jakub Dawidek "Unable to flush disk cache on activemap update");
3602adbba66SMikolaj Golub res->hr_stat_activemap_flush_error++;
361a818a4ffSMikolaj Golub ret = -1;
362518dd4c0SPawel Jakub Dawidek }
363518dd4c0SPawel Jakub Dawidek }
364a818a4ffSMikolaj Golub mtx_unlock(&res->hr_amp_diskmap_lock);
365a818a4ffSMikolaj Golub return (ret);
36632115b10SPawel Jakub Dawidek }
36732115b10SPawel Jakub Dawidek
368f377917cSPawel Jakub Dawidek static bool
real_remote(const struct hast_resource * res)369f377917cSPawel Jakub Dawidek real_remote(const struct hast_resource *res)
370f377917cSPawel Jakub Dawidek {
371f377917cSPawel Jakub Dawidek
372f377917cSPawel Jakub Dawidek return (strcmp(res->hr_remoteaddr, "none") != 0);
373f377917cSPawel Jakub Dawidek }
374f377917cSPawel Jakub Dawidek
37532115b10SPawel Jakub Dawidek static void
init_environment(struct hast_resource * res __unused)37632115b10SPawel Jakub Dawidek init_environment(struct hast_resource *res __unused)
37732115b10SPawel Jakub Dawidek {
37832115b10SPawel Jakub Dawidek struct hio *hio;
37932115b10SPawel Jakub Dawidek unsigned int ii, ncomps;
38032115b10SPawel Jakub Dawidek
38132115b10SPawel Jakub Dawidek /*
38232115b10SPawel Jakub Dawidek * In the future it might be per-resource value.
38332115b10SPawel Jakub Dawidek */
38432115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS;
38532115b10SPawel Jakub Dawidek
38632115b10SPawel Jakub Dawidek /*
38732115b10SPawel Jakub Dawidek * Allocate memory needed by lists.
38832115b10SPawel Jakub Dawidek */
38932115b10SPawel Jakub Dawidek hio_send_list = malloc(sizeof(hio_send_list[0]) * ncomps);
39032115b10SPawel Jakub Dawidek if (hio_send_list == NULL) {
39132115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
39232115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send lists.",
39332115b10SPawel Jakub Dawidek sizeof(hio_send_list[0]) * ncomps);
39432115b10SPawel Jakub Dawidek }
3956b66c350SMikolaj Golub hio_send_list_size = malloc(sizeof(hio_send_list_size[0]) * ncomps);
3966b66c350SMikolaj Golub if (hio_send_list_size == NULL) {
3976b66c350SMikolaj Golub primary_exitx(EX_TEMPFAIL,
3986b66c350SMikolaj Golub "Unable to allocate %zu bytes of memory for send list counters.",
3996b66c350SMikolaj Golub sizeof(hio_send_list_size[0]) * ncomps);
4006b66c350SMikolaj Golub }
40132115b10SPawel Jakub Dawidek hio_send_list_lock = malloc(sizeof(hio_send_list_lock[0]) * ncomps);
40232115b10SPawel Jakub Dawidek if (hio_send_list_lock == NULL) {
40332115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
40432115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send list locks.",
40532115b10SPawel Jakub Dawidek sizeof(hio_send_list_lock[0]) * ncomps);
40632115b10SPawel Jakub Dawidek }
40732115b10SPawel Jakub Dawidek hio_send_list_cond = malloc(sizeof(hio_send_list_cond[0]) * ncomps);
40832115b10SPawel Jakub Dawidek if (hio_send_list_cond == NULL) {
40932115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
41032115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for send list condition variables.",
41132115b10SPawel Jakub Dawidek sizeof(hio_send_list_cond[0]) * ncomps);
41232115b10SPawel Jakub Dawidek }
41332115b10SPawel Jakub Dawidek hio_recv_list = malloc(sizeof(hio_recv_list[0]) * ncomps);
41432115b10SPawel Jakub Dawidek if (hio_recv_list == NULL) {
41532115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
41632115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv lists.",
41732115b10SPawel Jakub Dawidek sizeof(hio_recv_list[0]) * ncomps);
41832115b10SPawel Jakub Dawidek }
4196b66c350SMikolaj Golub hio_recv_list_size = malloc(sizeof(hio_recv_list_size[0]) * ncomps);
4206b66c350SMikolaj Golub if (hio_recv_list_size == NULL) {
4216b66c350SMikolaj Golub primary_exitx(EX_TEMPFAIL,
4226b66c350SMikolaj Golub "Unable to allocate %zu bytes of memory for recv list counters.",
4236b66c350SMikolaj Golub sizeof(hio_recv_list_size[0]) * ncomps);
4246b66c350SMikolaj Golub }
42532115b10SPawel Jakub Dawidek hio_recv_list_lock = malloc(sizeof(hio_recv_list_lock[0]) * ncomps);
42632115b10SPawel Jakub Dawidek if (hio_recv_list_lock == NULL) {
42732115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
42832115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv list locks.",
42932115b10SPawel Jakub Dawidek sizeof(hio_recv_list_lock[0]) * ncomps);
43032115b10SPawel Jakub Dawidek }
43132115b10SPawel Jakub Dawidek hio_recv_list_cond = malloc(sizeof(hio_recv_list_cond[0]) * ncomps);
43232115b10SPawel Jakub Dawidek if (hio_recv_list_cond == NULL) {
43332115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
43432115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for recv list condition variables.",
43532115b10SPawel Jakub Dawidek sizeof(hio_recv_list_cond[0]) * ncomps);
43632115b10SPawel Jakub Dawidek }
43732115b10SPawel Jakub Dawidek hio_remote_lock = malloc(sizeof(hio_remote_lock[0]) * ncomps);
43832115b10SPawel Jakub Dawidek if (hio_remote_lock == NULL) {
43932115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
44032115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for remote connections locks.",
44132115b10SPawel Jakub Dawidek sizeof(hio_remote_lock[0]) * ncomps);
44232115b10SPawel Jakub Dawidek }
44332115b10SPawel Jakub Dawidek
44432115b10SPawel Jakub Dawidek /*
4456b66c350SMikolaj Golub * Initialize lists, their counters, locks and condition variables.
44632115b10SPawel Jakub Dawidek */
44732115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_free_list);
44832115b10SPawel Jakub Dawidek mtx_init(&hio_free_list_lock);
44932115b10SPawel Jakub Dawidek cv_init(&hio_free_list_cond);
45032115b10SPawel Jakub Dawidek for (ii = 0; ii < HAST_NCOMPONENTS; ii++) {
45132115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_send_list[ii]);
4526b66c350SMikolaj Golub hio_send_list_size[ii] = 0;
45332115b10SPawel Jakub Dawidek mtx_init(&hio_send_list_lock[ii]);
45432115b10SPawel Jakub Dawidek cv_init(&hio_send_list_cond[ii]);
45532115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_recv_list[ii]);
4566b66c350SMikolaj Golub hio_recv_list_size[ii] = 0;
45732115b10SPawel Jakub Dawidek mtx_init(&hio_recv_list_lock[ii]);
45832115b10SPawel Jakub Dawidek cv_init(&hio_recv_list_cond[ii]);
45932115b10SPawel Jakub Dawidek rw_init(&hio_remote_lock[ii]);
46032115b10SPawel Jakub Dawidek }
46132115b10SPawel Jakub Dawidek TAILQ_INIT(&hio_done_list);
46232115b10SPawel Jakub Dawidek mtx_init(&hio_done_list_lock);
46332115b10SPawel Jakub Dawidek cv_init(&hio_done_list_cond);
46432115b10SPawel Jakub Dawidek mtx_init(&metadata_lock);
46532115b10SPawel Jakub Dawidek
46632115b10SPawel Jakub Dawidek /*
46732115b10SPawel Jakub Dawidek * Allocate requests pool and initialize requests.
46832115b10SPawel Jakub Dawidek */
46932115b10SPawel Jakub Dawidek for (ii = 0; ii < HAST_HIO_MAX; ii++) {
47032115b10SPawel Jakub Dawidek hio = malloc(sizeof(*hio));
47132115b10SPawel Jakub Dawidek if (hio == NULL) {
47232115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
47332115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for hio request.",
47432115b10SPawel Jakub Dawidek sizeof(*hio));
47532115b10SPawel Jakub Dawidek }
4766e87c151SEd Schouten refcnt_init(&hio->hio_countdown, 0);
47732115b10SPawel Jakub Dawidek hio->hio_errors = malloc(sizeof(hio->hio_errors[0]) * ncomps);
47832115b10SPawel Jakub Dawidek if (hio->hio_errors == NULL) {
47932115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
48032115b10SPawel Jakub Dawidek "Unable allocate %zu bytes of memory for hio errors.",
48132115b10SPawel Jakub Dawidek sizeof(hio->hio_errors[0]) * ncomps);
48232115b10SPawel Jakub Dawidek }
48332115b10SPawel Jakub Dawidek hio->hio_next = malloc(sizeof(hio->hio_next[0]) * ncomps);
48432115b10SPawel Jakub Dawidek if (hio->hio_next == NULL) {
48532115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
48632115b10SPawel Jakub Dawidek "Unable allocate %zu bytes of memory for hio_next field.",
48732115b10SPawel Jakub Dawidek sizeof(hio->hio_next[0]) * ncomps);
48832115b10SPawel Jakub Dawidek }
48932115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_version = G_GATE_VERSION;
49032115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_data = malloc(MAXPHYS);
49132115b10SPawel Jakub Dawidek if (hio->hio_ggio.gctl_data == NULL) {
49232115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
49332115b10SPawel Jakub Dawidek "Unable to allocate %zu bytes of memory for gctl_data.",
49432115b10SPawel Jakub Dawidek MAXPHYS);
49532115b10SPawel Jakub Dawidek }
49632115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_length = MAXPHYS;
49732115b10SPawel Jakub Dawidek hio->hio_ggio.gctl_error = 0;
49832115b10SPawel Jakub Dawidek TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_free_next);
4996b66c350SMikolaj Golub hio_free_list_size++;
50032115b10SPawel Jakub Dawidek }
50132115b10SPawel Jakub Dawidek }
50232115b10SPawel Jakub Dawidek
503ce837469SPawel Jakub Dawidek static bool
init_resuid(struct hast_resource * res)504ce837469SPawel Jakub Dawidek init_resuid(struct hast_resource *res)
505ce837469SPawel Jakub Dawidek {
506ce837469SPawel Jakub Dawidek
507ce837469SPawel Jakub Dawidek mtx_lock(&metadata_lock);
508ce837469SPawel Jakub Dawidek if (res->hr_resuid != 0) {
509ce837469SPawel Jakub Dawidek mtx_unlock(&metadata_lock);
510ce837469SPawel Jakub Dawidek return (false);
511ce837469SPawel Jakub Dawidek } else {
512ce837469SPawel Jakub Dawidek /* Initialize unique resource identifier. */
513ce837469SPawel Jakub Dawidek arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
514ce837469SPawel Jakub Dawidek mtx_unlock(&metadata_lock);
5152b1b224dSPawel Jakub Dawidek if (metadata_write(res) == -1)
516ce837469SPawel Jakub Dawidek exit(EX_NOINPUT);
517ce837469SPawel Jakub Dawidek return (true);
518ce837469SPawel Jakub Dawidek }
519ce837469SPawel Jakub Dawidek }
520ce837469SPawel Jakub Dawidek
52132115b10SPawel Jakub Dawidek static void
init_local(struct hast_resource * res)52232115b10SPawel Jakub Dawidek init_local(struct hast_resource *res)
52332115b10SPawel Jakub Dawidek {
52432115b10SPawel Jakub Dawidek unsigned char *buf;
52532115b10SPawel Jakub Dawidek size_t mapsize;
52632115b10SPawel Jakub Dawidek
5272b1b224dSPawel Jakub Dawidek if (metadata_read(res, true) == -1)
52832115b10SPawel Jakub Dawidek exit(EX_NOINPUT);
52932115b10SPawel Jakub Dawidek mtx_init(&res->hr_amp_lock);
53032115b10SPawel Jakub Dawidek if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
5312b1b224dSPawel Jakub Dawidek res->hr_local_sectorsize, res->hr_keepdirty) == -1) {
53232115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create activemap");
53332115b10SPawel Jakub Dawidek }
53432115b10SPawel Jakub Dawidek mtx_init(&range_lock);
53532115b10SPawel Jakub Dawidek cv_init(&range_regular_cond);
5362b1b224dSPawel Jakub Dawidek if (rangelock_init(&range_regular) == -1)
53732115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
53832115b10SPawel Jakub Dawidek cv_init(&range_sync_cond);
5392b1b224dSPawel Jakub Dawidek if (rangelock_init(&range_sync) == -1)
54032115b10SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
54132115b10SPawel Jakub Dawidek mapsize = activemap_ondisk_size(res->hr_amp);
54232115b10SPawel Jakub Dawidek buf = calloc(1, mapsize);
54332115b10SPawel Jakub Dawidek if (buf == NULL) {
54432115b10SPawel Jakub Dawidek primary_exitx(EX_TEMPFAIL,
54532115b10SPawel Jakub Dawidek "Unable to allocate buffer for activemap.");
54632115b10SPawel Jakub Dawidek }
54732115b10SPawel Jakub Dawidek if (pread(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
54832115b10SPawel Jakub Dawidek (ssize_t)mapsize) {
54932115b10SPawel Jakub Dawidek primary_exit(EX_NOINPUT, "Unable to read activemap");
55032115b10SPawel Jakub Dawidek }
55132115b10SPawel Jakub Dawidek activemap_copyin(res->hr_amp, buf, mapsize);
552b0dfbe5bSPawel Jakub Dawidek free(buf);
55332115b10SPawel Jakub Dawidek if (res->hr_resuid != 0)
55432115b10SPawel Jakub Dawidek return;
55532115b10SPawel Jakub Dawidek /*
556ce837469SPawel Jakub Dawidek * We're using provider for the first time. Initialize local and remote
557ce837469SPawel Jakub Dawidek * counters. We don't initialize resuid here, as we want to do it just
558ce837469SPawel Jakub Dawidek * in time. The reason for this is that we want to inform secondary
559ce837469SPawel Jakub Dawidek * that there were no writes yet, so there is no need to synchronize
560ce837469SPawel Jakub Dawidek * anything.
56132115b10SPawel Jakub Dawidek */
5629446b453SPawel Jakub Dawidek res->hr_primary_localcnt = 0;
56332115b10SPawel Jakub Dawidek res->hr_primary_remotecnt = 0;
5642b1b224dSPawel Jakub Dawidek if (metadata_write(res) == -1)
56532115b10SPawel Jakub Dawidek exit(EX_NOINPUT);
56632115b10SPawel Jakub Dawidek }
56732115b10SPawel Jakub Dawidek
56832ecf620SPawel Jakub Dawidek static int
primary_connect(struct hast_resource * res,struct proto_conn ** connp)56932ecf620SPawel Jakub Dawidek primary_connect(struct hast_resource *res, struct proto_conn **connp)
57032ecf620SPawel Jakub Dawidek {
57132ecf620SPawel Jakub Dawidek struct proto_conn *conn;
57232ecf620SPawel Jakub Dawidek int16_t val;
57332ecf620SPawel Jakub Dawidek
57432ecf620SPawel Jakub Dawidek val = 1;
5752b1b224dSPawel Jakub Dawidek if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
57632ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL,
57732ecf620SPawel Jakub Dawidek "Unable to send connection request to parent");
57832ecf620SPawel Jakub Dawidek }
5792b1b224dSPawel Jakub Dawidek if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
58032ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL,
58132ecf620SPawel Jakub Dawidek "Unable to receive reply to connection request from parent");
58232ecf620SPawel Jakub Dawidek }
58332ecf620SPawel Jakub Dawidek if (val != 0) {
58432ecf620SPawel Jakub Dawidek errno = val;
58532ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
58632ecf620SPawel Jakub Dawidek res->hr_remoteaddr);
58732ecf620SPawel Jakub Dawidek return (-1);
58832ecf620SPawel Jakub Dawidek }
5892b1b224dSPawel Jakub Dawidek if (proto_connection_recv(res->hr_conn, true, &conn) == -1) {
59032ecf620SPawel Jakub Dawidek primary_exit(EX_TEMPFAIL,
59132ecf620SPawel Jakub Dawidek "Unable to receive connection from parent");
59232ecf620SPawel Jakub Dawidek }
5932b1b224dSPawel Jakub Dawidek if (proto_connect_wait(conn, res->hr_timeout) == -1) {
59432ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
59532ecf620SPawel Jakub Dawidek res->hr_remoteaddr);
59632ecf620SPawel Jakub Dawidek proto_close(conn);
59732ecf620SPawel Jakub Dawidek return (-1);
59832ecf620SPawel Jakub Dawidek }
59932ecf620SPawel Jakub Dawidek /* Error in setting timeout is not critical, but why should it fail? */
6002b1b224dSPawel Jakub Dawidek if (proto_timeout(conn, res->hr_timeout) == -1)
60132ecf620SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
60232ecf620SPawel Jakub Dawidek
60332ecf620SPawel Jakub Dawidek *connp = conn;
60432ecf620SPawel Jakub Dawidek
60532ecf620SPawel Jakub Dawidek return (0);
60632ecf620SPawel Jakub Dawidek }
60732ecf620SPawel Jakub Dawidek
608c66ee1b3SPawel Jakub Dawidek /*
609c66ee1b3SPawel Jakub Dawidek * Function instructs GEOM_GATE to handle reads directly from within the kernel.
610c66ee1b3SPawel Jakub Dawidek */
611c66ee1b3SPawel Jakub Dawidek static void
enable_direct_reads(struct hast_resource * res)612c66ee1b3SPawel Jakub Dawidek enable_direct_reads(struct hast_resource *res)
613c66ee1b3SPawel Jakub Dawidek {
614c66ee1b3SPawel Jakub Dawidek struct g_gate_ctl_modify ggiomodify;
615c66ee1b3SPawel Jakub Dawidek
616c66ee1b3SPawel Jakub Dawidek bzero(&ggiomodify, sizeof(ggiomodify));
617c66ee1b3SPawel Jakub Dawidek ggiomodify.gctl_version = G_GATE_VERSION;
618c66ee1b3SPawel Jakub Dawidek ggiomodify.gctl_unit = res->hr_ggateunit;
619c66ee1b3SPawel Jakub Dawidek ggiomodify.gctl_modify = GG_MODIFY_READPROV | GG_MODIFY_READOFFSET;
620c66ee1b3SPawel Jakub Dawidek strlcpy(ggiomodify.gctl_readprov, res->hr_localpath,
621c66ee1b3SPawel Jakub Dawidek sizeof(ggiomodify.gctl_readprov));
622c66ee1b3SPawel Jakub Dawidek ggiomodify.gctl_readoffset = res->hr_localoff;
623c66ee1b3SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_MODIFY, &ggiomodify) == 0)
624c66ee1b3SPawel Jakub Dawidek pjdlog_debug(1, "Direct reads enabled.");
625c66ee1b3SPawel Jakub Dawidek else
626c66ee1b3SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Failed to enable direct reads");
627c66ee1b3SPawel Jakub Dawidek }
628c66ee1b3SPawel Jakub Dawidek
629ac0401e3SPawel Jakub Dawidek static int
init_remote(struct hast_resource * res,struct proto_conn ** inp,struct proto_conn ** outp)6300d9014f3SPawel Jakub Dawidek init_remote(struct hast_resource *res, struct proto_conn **inp,
6310d9014f3SPawel Jakub Dawidek struct proto_conn **outp)
63232115b10SPawel Jakub Dawidek {
6330d9014f3SPawel Jakub Dawidek struct proto_conn *in, *out;
63432115b10SPawel Jakub Dawidek struct nv *nvout, *nvin;
63532115b10SPawel Jakub Dawidek const unsigned char *token;
63632115b10SPawel Jakub Dawidek unsigned char *map;
63732115b10SPawel Jakub Dawidek const char *errmsg;
63832115b10SPawel Jakub Dawidek int32_t extentsize;
63932115b10SPawel Jakub Dawidek int64_t datasize;
64032115b10SPawel Jakub Dawidek uint32_t mapsize;
641d6e636c9SPawel Jakub Dawidek uint8_t version;
64232115b10SPawel Jakub Dawidek size_t size;
643ac0401e3SPawel Jakub Dawidek int error;
64432115b10SPawel Jakub Dawidek
6452ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
6462ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(real_remote(res));
6470d9014f3SPawel Jakub Dawidek
6480d9014f3SPawel Jakub Dawidek in = out = NULL;
6492be8fd75SPawel Jakub Dawidek errmsg = NULL;
6500d9014f3SPawel Jakub Dawidek
65132ecf620SPawel Jakub Dawidek if (primary_connect(res, &out) == -1)
652ac0401e3SPawel Jakub Dawidek return (ECONNREFUSED);
653ac0401e3SPawel Jakub Dawidek
654ac0401e3SPawel Jakub Dawidek error = ECONNABORTED;
65532ecf620SPawel Jakub Dawidek
65632115b10SPawel Jakub Dawidek /*
65732115b10SPawel Jakub Dawidek * First handshake step.
65832115b10SPawel Jakub Dawidek * Setup outgoing connection with remote node.
65932115b10SPawel Jakub Dawidek */
66032115b10SPawel Jakub Dawidek nvout = nv_alloc();
66132115b10SPawel Jakub Dawidek nv_add_string(nvout, res->hr_name, "resource");
662d6e636c9SPawel Jakub Dawidek nv_add_uint8(nvout, HAST_PROTO_VERSION, "version");
66332115b10SPawel Jakub Dawidek if (nv_error(nvout) != 0) {
66432115b10SPawel Jakub Dawidek pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
66532115b10SPawel Jakub Dawidek "Unable to allocate header for connection with %s",
66632115b10SPawel Jakub Dawidek res->hr_remoteaddr);
66732115b10SPawel Jakub Dawidek nv_free(nvout);
66832115b10SPawel Jakub Dawidek goto close;
66932115b10SPawel Jakub Dawidek }
6702b1b224dSPawel Jakub Dawidek if (hast_proto_send(res, out, nvout, NULL, 0) == -1) {
67132115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING,
67232115b10SPawel Jakub Dawidek "Unable to send handshake header to %s",
67332115b10SPawel Jakub Dawidek res->hr_remoteaddr);
67432115b10SPawel Jakub Dawidek nv_free(nvout);
67532115b10SPawel Jakub Dawidek goto close;
67632115b10SPawel Jakub Dawidek }
67732115b10SPawel Jakub Dawidek nv_free(nvout);
6782b1b224dSPawel Jakub Dawidek if (hast_proto_recv_hdr(out, &nvin) == -1) {
67932115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING,
68032115b10SPawel Jakub Dawidek "Unable to receive handshake header from %s",
68132115b10SPawel Jakub Dawidek res->hr_remoteaddr);
68232115b10SPawel Jakub Dawidek goto close;
68332115b10SPawel Jakub Dawidek }
68432115b10SPawel Jakub Dawidek errmsg = nv_get_string(nvin, "errmsg");
68532115b10SPawel Jakub Dawidek if (errmsg != NULL) {
68632115b10SPawel Jakub Dawidek pjdlog_warning("%s", errmsg);
687ac0401e3SPawel Jakub Dawidek if (nv_exists(nvin, "wait"))
688ac0401e3SPawel Jakub Dawidek error = EBUSY;
68932115b10SPawel Jakub Dawidek nv_free(nvin);
69032115b10SPawel Jakub Dawidek goto close;
69132115b10SPawel Jakub Dawidek }
692d6e636c9SPawel Jakub Dawidek version = nv_get_uint8(nvin, "version");
693d6e636c9SPawel Jakub Dawidek if (version == 0) {
694d6e636c9SPawel Jakub Dawidek /*
695d6e636c9SPawel Jakub Dawidek * If no version is sent, it means this is protocol version 1.
696d6e636c9SPawel Jakub Dawidek */
697d6e636c9SPawel Jakub Dawidek version = 1;
698d6e636c9SPawel Jakub Dawidek }
699d6e636c9SPawel Jakub Dawidek if (version > HAST_PROTO_VERSION) {
700d6e636c9SPawel Jakub Dawidek pjdlog_warning("Invalid version received (%hhu).", version);
701d6e636c9SPawel Jakub Dawidek nv_free(nvin);
702d6e636c9SPawel Jakub Dawidek goto close;
703d6e636c9SPawel Jakub Dawidek }
704d6e636c9SPawel Jakub Dawidek res->hr_version = version;
705d6e636c9SPawel Jakub Dawidek pjdlog_debug(1, "Negotiated protocol version %d.", res->hr_version);
70632115b10SPawel Jakub Dawidek token = nv_get_uint8_array(nvin, &size, "token");
70732115b10SPawel Jakub Dawidek if (token == NULL) {
70832115b10SPawel Jakub Dawidek pjdlog_warning("Handshake header from %s has no 'token' field.",
70932115b10SPawel Jakub Dawidek res->hr_remoteaddr);
71032115b10SPawel Jakub Dawidek nv_free(nvin);
71132115b10SPawel Jakub Dawidek goto close;
71232115b10SPawel Jakub Dawidek }
71332115b10SPawel Jakub Dawidek if (size != sizeof(res->hr_token)) {
71432115b10SPawel Jakub Dawidek pjdlog_warning("Handshake header from %s contains 'token' of wrong size (got %zu, expected %zu).",
71532115b10SPawel Jakub Dawidek res->hr_remoteaddr, size, sizeof(res->hr_token));
71632115b10SPawel Jakub Dawidek nv_free(nvin);
71732115b10SPawel Jakub Dawidek goto close;
71832115b10SPawel Jakub Dawidek }
71932115b10SPawel Jakub Dawidek bcopy(token, res->hr_token, sizeof(res->hr_token));
72032115b10SPawel Jakub Dawidek nv_free(nvin);
72132115b10SPawel Jakub Dawidek
72232115b10SPawel Jakub Dawidek /*
72332115b10SPawel Jakub Dawidek * Second handshake step.
72432115b10SPawel Jakub Dawidek * Setup incoming connection with remote node.
72532115b10SPawel Jakub Dawidek */
72632ecf620SPawel Jakub Dawidek if (primary_connect(res, &in) == -1)
72732115b10SPawel Jakub Dawidek goto close;
72832ecf620SPawel Jakub Dawidek
72932115b10SPawel Jakub Dawidek nvout = nv_alloc();
73032115b10SPawel Jakub Dawidek nv_add_string(nvout, res->hr_name, "resource");
73132115b10SPawel Jakub Dawidek nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
73232115b10SPawel Jakub Dawidek "token");
733ce837469SPawel Jakub Dawidek if (res->hr_resuid == 0) {
734ce837469SPawel Jakub Dawidek /*
735ce837469SPawel Jakub Dawidek * The resuid field was not yet initialized.
736ce837469SPawel Jakub Dawidek * Because we do synchronization inside init_resuid(), it is
737ce837469SPawel Jakub Dawidek * possible that someone already initialized it, the function
738ce837469SPawel Jakub Dawidek * will return false then, but if we successfully initialized
739ce837469SPawel Jakub Dawidek * it, we will get true. True means that there were no writes
740ce837469SPawel Jakub Dawidek * to this resource yet and we want to inform secondary that
741ce837469SPawel Jakub Dawidek * synchronization is not needed by sending "virgin" argument.
742ce837469SPawel Jakub Dawidek */
743ce837469SPawel Jakub Dawidek if (init_resuid(res))
744ce837469SPawel Jakub Dawidek nv_add_int8(nvout, 1, "virgin");
745ce837469SPawel Jakub Dawidek }
74632115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_resuid, "resuid");
74732115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_primary_localcnt, "localcnt");
74832115b10SPawel Jakub Dawidek nv_add_uint64(nvout, res->hr_primary_remotecnt, "remotecnt");
74932115b10SPawel Jakub Dawidek if (nv_error(nvout) != 0) {
75032115b10SPawel Jakub Dawidek pjdlog_common(LOG_WARNING, 0, nv_error(nvout),
75132115b10SPawel Jakub Dawidek "Unable to allocate header for connection with %s",
75232115b10SPawel Jakub Dawidek res->hr_remoteaddr);
75332115b10SPawel Jakub Dawidek nv_free(nvout);
75432115b10SPawel Jakub Dawidek goto close;
75532115b10SPawel Jakub Dawidek }
7562b1b224dSPawel Jakub Dawidek if (hast_proto_send(res, in, nvout, NULL, 0) == -1) {
75732115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING,
75832115b10SPawel Jakub Dawidek "Unable to send handshake header to %s",
75932115b10SPawel Jakub Dawidek res->hr_remoteaddr);
76032115b10SPawel Jakub Dawidek nv_free(nvout);
76132115b10SPawel Jakub Dawidek goto close;
76232115b10SPawel Jakub Dawidek }
76332115b10SPawel Jakub Dawidek nv_free(nvout);
7642b1b224dSPawel Jakub Dawidek if (hast_proto_recv_hdr(out, &nvin) == -1) {
76532115b10SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING,
76632115b10SPawel Jakub Dawidek "Unable to receive handshake header from %s",
76732115b10SPawel Jakub Dawidek res->hr_remoteaddr);
76832115b10SPawel Jakub Dawidek goto close;
76932115b10SPawel Jakub Dawidek }
77032115b10SPawel Jakub Dawidek errmsg = nv_get_string(nvin, "errmsg");
77132115b10SPawel Jakub Dawidek if (errmsg != NULL) {
77232115b10SPawel Jakub Dawidek pjdlog_warning("%s", errmsg);
77332115b10SPawel Jakub Dawidek nv_free(nvin);
77432115b10SPawel Jakub Dawidek goto close;
77532115b10SPawel Jakub Dawidek }
77632115b10SPawel Jakub Dawidek datasize = nv_get_int64(nvin, "datasize");
77732115b10SPawel Jakub Dawidek if (datasize != res->hr_datasize) {
77832115b10SPawel Jakub Dawidek pjdlog_warning("Data size differs between nodes (local=%jd, remote=%jd).",
77932115b10SPawel Jakub Dawidek (intmax_t)res->hr_datasize, (intmax_t)datasize);
78032115b10SPawel Jakub Dawidek nv_free(nvin);
78132115b10SPawel Jakub Dawidek goto close;
78232115b10SPawel Jakub Dawidek }
78332115b10SPawel Jakub Dawidek extentsize = nv_get_int32(nvin, "extentsize");
78432115b10SPawel Jakub Dawidek if (extentsize != res->hr_extentsize) {
78532115b10SPawel Jakub Dawidek pjdlog_warning("Extent size differs between nodes (local=%zd, remote=%zd).",
78632115b10SPawel Jakub Dawidek (ssize_t)res->hr_extentsize, (ssize_t)extentsize);
78732115b10SPawel Jakub Dawidek nv_free(nvin);
78832115b10SPawel Jakub Dawidek goto close;
78932115b10SPawel Jakub Dawidek }
79032115b10SPawel Jakub Dawidek res->hr_secondary_localcnt = nv_get_uint64(nvin, "localcnt");
79132115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt = nv_get_uint64(nvin, "remotecnt");
79232115b10SPawel Jakub Dawidek res->hr_syncsrc = nv_get_uint8(nvin, "syncsrc");
793c66ee1b3SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY)
794c66ee1b3SPawel Jakub Dawidek enable_direct_reads(res);
79506cbf549SPawel Jakub Dawidek if (nv_exists(nvin, "virgin")) {
79606cbf549SPawel Jakub Dawidek /*
79706cbf549SPawel Jakub Dawidek * Secondary was reinitialized, bump localcnt if it is 0 as
79806cbf549SPawel Jakub Dawidek * only we have the data.
79906cbf549SPawel Jakub Dawidek */
80006cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_PRIMARY);
80106cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
80206cbf549SPawel Jakub Dawidek
80306cbf549SPawel Jakub Dawidek if (res->hr_primary_localcnt == 0) {
80406cbf549SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_secondary_remotecnt == 0);
80506cbf549SPawel Jakub Dawidek
80606cbf549SPawel Jakub Dawidek mtx_lock(&metadata_lock);
80706cbf549SPawel Jakub Dawidek res->hr_primary_localcnt++;
80806cbf549SPawel Jakub Dawidek pjdlog_debug(1, "Increasing localcnt to %ju.",
80906cbf549SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt);
81006cbf549SPawel Jakub Dawidek (void)metadata_write(res);
81106cbf549SPawel Jakub Dawidek mtx_unlock(&metadata_lock);
81206cbf549SPawel Jakub Dawidek }
81306cbf549SPawel Jakub Dawidek }
81432115b10SPawel Jakub Dawidek map = NULL;
81532115b10SPawel Jakub Dawidek mapsize = nv_get_uint32(nvin, "mapsize");
81632115b10SPawel Jakub Dawidek if (mapsize > 0) {
81732115b10SPawel Jakub Dawidek map = malloc(mapsize);
81832115b10SPawel Jakub Dawidek if (map == NULL) {
81932115b10SPawel Jakub Dawidek pjdlog_error("Unable to allocate memory for remote activemap (mapsize=%ju).",
82032115b10SPawel Jakub Dawidek (uintmax_t)mapsize);
82132115b10SPawel Jakub Dawidek nv_free(nvin);
82232115b10SPawel Jakub Dawidek goto close;
82332115b10SPawel Jakub Dawidek }
82432115b10SPawel Jakub Dawidek /*
82532115b10SPawel Jakub Dawidek * Remote node have some dirty extents on its own, lets
82632115b10SPawel Jakub Dawidek * download its activemap.
82732115b10SPawel Jakub Dawidek */
8280d9014f3SPawel Jakub Dawidek if (hast_proto_recv_data(res, out, nvin, map,
8292b1b224dSPawel Jakub Dawidek mapsize) == -1) {
83032115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR,
83132115b10SPawel Jakub Dawidek "Unable to receive remote activemap");
83232115b10SPawel Jakub Dawidek nv_free(nvin);
83332115b10SPawel Jakub Dawidek free(map);
83432115b10SPawel Jakub Dawidek goto close;
83532115b10SPawel Jakub Dawidek }
836d03a08e5SMikolaj Golub mtx_lock(&res->hr_amp_lock);
83732115b10SPawel Jakub Dawidek /*
83832115b10SPawel Jakub Dawidek * Merge local and remote bitmaps.
83932115b10SPawel Jakub Dawidek */
84032115b10SPawel Jakub Dawidek activemap_merge(res->hr_amp, map, mapsize);
84132115b10SPawel Jakub Dawidek free(map);
84232115b10SPawel Jakub Dawidek /*
84332115b10SPawel Jakub Dawidek * Now that we merged bitmaps from both nodes, flush it to the
84432115b10SPawel Jakub Dawidek * disk before we start to synchronize.
84532115b10SPawel Jakub Dawidek */
84632115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res);
84732115b10SPawel Jakub Dawidek }
848584a9bc3SPawel Jakub Dawidek nv_free(nvin);
849ba2a8224SMikolaj Golub #ifdef notyet
85002dfe972SPawel Jakub Dawidek /* Setup directions. */
85102dfe972SPawel Jakub Dawidek if (proto_send(out, NULL, 0) == -1)
85202dfe972SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
85302dfe972SPawel Jakub Dawidek if (proto_recv(in, NULL, 0) == -1)
85402dfe972SPawel Jakub Dawidek pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
855ba2a8224SMikolaj Golub #endif
85632115b10SPawel Jakub Dawidek pjdlog_info("Connected to %s.", res->hr_remoteaddr);
857d6e636c9SPawel Jakub Dawidek if (res->hr_original_replication == HAST_REPLICATION_MEMSYNC &&
858d6e636c9SPawel Jakub Dawidek res->hr_version < 2) {
859d6e636c9SPawel Jakub Dawidek pjdlog_warning("The 'memsync' replication mode is not supported by the remote node, falling back to 'fullsync' mode.");
860d6e636c9SPawel Jakub Dawidek res->hr_replication = HAST_REPLICATION_FULLSYNC;
861d6e636c9SPawel Jakub Dawidek } else if (res->hr_replication != res->hr_original_replication) {
862d6e636c9SPawel Jakub Dawidek /*
863d6e636c9SPawel Jakub Dawidek * This is in case hastd disconnected and was upgraded.
864d6e636c9SPawel Jakub Dawidek */
865d6e636c9SPawel Jakub Dawidek res->hr_replication = res->hr_original_replication;
866d6e636c9SPawel Jakub Dawidek }
8670d9014f3SPawel Jakub Dawidek if (inp != NULL && outp != NULL) {
8680d9014f3SPawel Jakub Dawidek *inp = in;
8690d9014f3SPawel Jakub Dawidek *outp = out;
8700d9014f3SPawel Jakub Dawidek } else {
8710d9014f3SPawel Jakub Dawidek res->hr_remotein = in;
8720d9014f3SPawel Jakub Dawidek res->hr_remoteout = out;
8730d9014f3SPawel Jakub Dawidek }
8745bdff860SPawel Jakub Dawidek event_send(res, EVENT_CONNECT);
875ac0401e3SPawel Jakub Dawidek return (0);
8760d9014f3SPawel Jakub Dawidek close:
8772be8fd75SPawel Jakub Dawidek if (errmsg != NULL && strcmp(errmsg, "Split-brain condition!") == 0)
8785bdff860SPawel Jakub Dawidek event_send(res, EVENT_SPLITBRAIN);
8790d9014f3SPawel Jakub Dawidek proto_close(out);
8800d9014f3SPawel Jakub Dawidek if (in != NULL)
8810d9014f3SPawel Jakub Dawidek proto_close(in);
882ac0401e3SPawel Jakub Dawidek return (error);
8830d9014f3SPawel Jakub Dawidek }
8840d9014f3SPawel Jakub Dawidek
8850d9014f3SPawel Jakub Dawidek static void
sync_start(void)8860d9014f3SPawel Jakub Dawidek sync_start(void)
8870d9014f3SPawel Jakub Dawidek {
8880d9014f3SPawel Jakub Dawidek
88932115b10SPawel Jakub Dawidek mtx_lock(&sync_lock);
89032115b10SPawel Jakub Dawidek sync_inprogress = true;
89132115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock);
89232115b10SPawel Jakub Dawidek cv_signal(&sync_cond);
89332115b10SPawel Jakub Dawidek }
89432115b10SPawel Jakub Dawidek
89532115b10SPawel Jakub Dawidek static void
sync_stop(void)89655ce1e7cSPawel Jakub Dawidek sync_stop(void)
89755ce1e7cSPawel Jakub Dawidek {
89855ce1e7cSPawel Jakub Dawidek
89955ce1e7cSPawel Jakub Dawidek mtx_lock(&sync_lock);
90055ce1e7cSPawel Jakub Dawidek if (sync_inprogress)
90155ce1e7cSPawel Jakub Dawidek sync_inprogress = false;
90255ce1e7cSPawel Jakub Dawidek mtx_unlock(&sync_lock);
90355ce1e7cSPawel Jakub Dawidek }
90455ce1e7cSPawel Jakub Dawidek
90555ce1e7cSPawel Jakub Dawidek static void
init_ggate(struct hast_resource * res)90632115b10SPawel Jakub Dawidek init_ggate(struct hast_resource *res)
90732115b10SPawel Jakub Dawidek {
90832115b10SPawel Jakub Dawidek struct g_gate_ctl_create ggiocreate;
90932115b10SPawel Jakub Dawidek struct g_gate_ctl_cancel ggiocancel;
91032115b10SPawel Jakub Dawidek
91132115b10SPawel Jakub Dawidek /*
91232115b10SPawel Jakub Dawidek * We communicate with ggate via /dev/ggctl. Open it.
91332115b10SPawel Jakub Dawidek */
91432115b10SPawel Jakub Dawidek res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
9152b1b224dSPawel Jakub Dawidek if (res->hr_ggatefd == -1)
91632115b10SPawel Jakub Dawidek primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
91732115b10SPawel Jakub Dawidek /*
91832115b10SPawel Jakub Dawidek * Create provider before trying to connect, as connection failure
91932115b10SPawel Jakub Dawidek * is not critical, but may take some time.
92032115b10SPawel Jakub Dawidek */
9214e47b646SPawel Jakub Dawidek bzero(&ggiocreate, sizeof(ggiocreate));
92232115b10SPawel Jakub Dawidek ggiocreate.gctl_version = G_GATE_VERSION;
92332115b10SPawel Jakub Dawidek ggiocreate.gctl_mediasize = res->hr_datasize;
92432115b10SPawel Jakub Dawidek ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
92532115b10SPawel Jakub Dawidek ggiocreate.gctl_flags = 0;
9262a49afacSPawel Jakub Dawidek ggiocreate.gctl_maxcount = 0;
92732115b10SPawel Jakub Dawidek ggiocreate.gctl_timeout = 0;
92832115b10SPawel Jakub Dawidek ggiocreate.gctl_unit = G_GATE_NAME_GIVEN;
92932115b10SPawel Jakub Dawidek snprintf(ggiocreate.gctl_name, sizeof(ggiocreate.gctl_name), "hast/%s",
93032115b10SPawel Jakub Dawidek res->hr_provname);
93132115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_CREATE, &ggiocreate) == 0) {
93232115b10SPawel Jakub Dawidek pjdlog_info("Device hast/%s created.", res->hr_provname);
93332115b10SPawel Jakub Dawidek res->hr_ggateunit = ggiocreate.gctl_unit;
93432115b10SPawel Jakub Dawidek return;
93532115b10SPawel Jakub Dawidek }
93632115b10SPawel Jakub Dawidek if (errno != EEXIST) {
93732115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "Unable to create hast/%s device",
93832115b10SPawel Jakub Dawidek res->hr_provname);
93932115b10SPawel Jakub Dawidek }
94032115b10SPawel Jakub Dawidek pjdlog_debug(1,
94132115b10SPawel Jakub Dawidek "Device hast/%s already exists, we will try to take it over.",
94232115b10SPawel Jakub Dawidek res->hr_provname);
94332115b10SPawel Jakub Dawidek /*
94432115b10SPawel Jakub Dawidek * If we received EEXIST, we assume that the process who created the
94532115b10SPawel Jakub Dawidek * provider died and didn't clean up. In that case we will start from
94632115b10SPawel Jakub Dawidek * where he left of.
94732115b10SPawel Jakub Dawidek */
9484e47b646SPawel Jakub Dawidek bzero(&ggiocancel, sizeof(ggiocancel));
94932115b10SPawel Jakub Dawidek ggiocancel.gctl_version = G_GATE_VERSION;
95032115b10SPawel Jakub Dawidek ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
95132115b10SPawel Jakub Dawidek snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
95232115b10SPawel Jakub Dawidek res->hr_provname);
95332115b10SPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_CANCEL, &ggiocancel) == 0) {
95432115b10SPawel Jakub Dawidek pjdlog_info("Device hast/%s recovered.", res->hr_provname);
95532115b10SPawel Jakub Dawidek res->hr_ggateunit = ggiocancel.gctl_unit;
95632115b10SPawel Jakub Dawidek return;
95732115b10SPawel Jakub Dawidek }
95832115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "Unable to take over hast/%s device",
95932115b10SPawel Jakub Dawidek res->hr_provname);
96032115b10SPawel Jakub Dawidek }
96132115b10SPawel Jakub Dawidek
96232115b10SPawel Jakub Dawidek void
hastd_primary(struct hast_resource * res)96332115b10SPawel Jakub Dawidek hastd_primary(struct hast_resource *res)
96432115b10SPawel Jakub Dawidek {
96532115b10SPawel Jakub Dawidek pthread_t td;
96632115b10SPawel Jakub Dawidek pid_t pid;
967bc7a916aSMikolaj Golub int error, mode, debuglevel;
96832115b10SPawel Jakub Dawidek
96932115b10SPawel Jakub Dawidek /*
97032ecf620SPawel Jakub Dawidek * Create communication channel for sending control commands from
97132ecf620SPawel Jakub Dawidek * parent to child.
97232115b10SPawel Jakub Dawidek */
9732b1b224dSPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
974d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */
97532115b10SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh));
9766be3a25cSPawel Jakub Dawidek pjdlog_exit(EX_OSERR,
97732115b10SPawel Jakub Dawidek "Unable to create control sockets between parent and child");
97832115b10SPawel Jakub Dawidek }
9795bdff860SPawel Jakub Dawidek /*
98032ecf620SPawel Jakub Dawidek * Create communication channel for sending events from child to parent.
9815bdff860SPawel Jakub Dawidek */
9822b1b224dSPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
983d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */
9845bdff860SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh));
9855bdff860SPawel Jakub Dawidek pjdlog_exit(EX_OSERR,
9865bdff860SPawel Jakub Dawidek "Unable to create event sockets between child and parent");
9875bdff860SPawel Jakub Dawidek }
98832ecf620SPawel Jakub Dawidek /*
98932ecf620SPawel Jakub Dawidek * Create communication channel for sending connection requests from
99032ecf620SPawel Jakub Dawidek * child to parent.
99132ecf620SPawel Jakub Dawidek */
9922b1b224dSPawel Jakub Dawidek if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) {
99332ecf620SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */
99432ecf620SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh));
99532ecf620SPawel Jakub Dawidek pjdlog_exit(EX_OSERR,
99632ecf620SPawel Jakub Dawidek "Unable to create connection sockets between child and parent");
99732ecf620SPawel Jakub Dawidek }
99832115b10SPawel Jakub Dawidek
99932115b10SPawel Jakub Dawidek pid = fork();
1000dfb1aeceSPawel Jakub Dawidek if (pid == -1) {
1001d64c0992SPawel Jakub Dawidek /* TODO: There's no need for this to be fatal error. */
100232115b10SPawel Jakub Dawidek KEEP_ERRNO((void)pidfile_remove(pfh));
10036be3a25cSPawel Jakub Dawidek pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
100432115b10SPawel Jakub Dawidek }
100532115b10SPawel Jakub Dawidek
100632115b10SPawel Jakub Dawidek if (pid > 0) {
100732115b10SPawel Jakub Dawidek /* This is parent. */
10085bdff860SPawel Jakub Dawidek /* Declare that we are receiver. */
10095bdff860SPawel Jakub Dawidek proto_recv(res->hr_event, NULL, 0);
101032ecf620SPawel Jakub Dawidek proto_recv(res->hr_conn, NULL, 0);
1011da1783eaSPawel Jakub Dawidek /* Declare that we are sender. */
1012da1783eaSPawel Jakub Dawidek proto_send(res->hr_ctrl, NULL, 0);
101332115b10SPawel Jakub Dawidek res->hr_workerpid = pid;
101432115b10SPawel Jakub Dawidek return;
101532115b10SPawel Jakub Dawidek }
1016ecc99c89SPawel Jakub Dawidek
10175b41e644SPawel Jakub Dawidek gres = res;
10186b66c350SMikolaj Golub res->output_status_aux = output_status_aux;
1019da1783eaSPawel Jakub Dawidek mode = pjdlog_mode_get();
1020bc7a916aSMikolaj Golub debuglevel = pjdlog_debug_get();
102132115b10SPawel Jakub Dawidek
10225bdff860SPawel Jakub Dawidek /* Declare that we are sender. */
10235bdff860SPawel Jakub Dawidek proto_send(res->hr_event, NULL, 0);
102432ecf620SPawel Jakub Dawidek proto_send(res->hr_conn, NULL, 0);
1025da1783eaSPawel Jakub Dawidek /* Declare that we are receiver. */
1026da1783eaSPawel Jakub Dawidek proto_recv(res->hr_ctrl, NULL, 0);
1027da1783eaSPawel Jakub Dawidek descriptors_cleanup(res);
1028da1783eaSPawel Jakub Dawidek
1029f463896eSPawel Jakub Dawidek descriptors_assert(res, mode);
1030f463896eSPawel Jakub Dawidek
1031da1783eaSPawel Jakub Dawidek pjdlog_init(mode);
1032bc7a916aSMikolaj Golub pjdlog_debug_set(debuglevel);
1033da1783eaSPawel Jakub Dawidek pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
1034643080b7SPawel Jakub Dawidek setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
10355bdff860SPawel Jakub Dawidek
103632115b10SPawel Jakub Dawidek init_local(res);
103732115b10SPawel Jakub Dawidek init_ggate(res);
103832115b10SPawel Jakub Dawidek init_environment(res);
1039115f4e5cSPawel Jakub Dawidek
10400cddb12fSPawel Jakub Dawidek if (drop_privs(res) != 0) {
10416d7967deSPawel Jakub Dawidek cleanup(res);
10426d7967deSPawel Jakub Dawidek exit(EX_CONFIG);
10436d7967deSPawel Jakub Dawidek }
1044f4c96f94SPawel Jakub Dawidek pjdlog_info("Privileges successfully dropped.");
10456d7967deSPawel Jakub Dawidek
10468b70e6aeSPawel Jakub Dawidek /*
10474a88128bSPawel Jakub Dawidek * Create the guard thread first, so we can handle signals from the
10484b85a12fSUlrich Spörlein * very beginning.
10494a88128bSPawel Jakub Dawidek */
10504a88128bSPawel Jakub Dawidek error = pthread_create(&td, NULL, guard_thread, res);
10512ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0);
10524a88128bSPawel Jakub Dawidek /*
10538b70e6aeSPawel Jakub Dawidek * Create the control thread before sending any event to the parent,
10548b70e6aeSPawel Jakub Dawidek * as we can deadlock when parent sends control request to worker,
10558b70e6aeSPawel Jakub Dawidek * but worker has no control thread started yet, so parent waits.
10568b70e6aeSPawel Jakub Dawidek * In the meantime worker sends an event to the parent, but parent
10578b70e6aeSPawel Jakub Dawidek * is unable to handle the event, because it waits for control
10588b70e6aeSPawel Jakub Dawidek * request response.
10598b70e6aeSPawel Jakub Dawidek */
10608b70e6aeSPawel Jakub Dawidek error = pthread_create(&td, NULL, ctrl_thread, res);
10612ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0);
1062ac0401e3SPawel Jakub Dawidek if (real_remote(res)) {
1063ac0401e3SPawel Jakub Dawidek error = init_remote(res, NULL, NULL);
1064ac0401e3SPawel Jakub Dawidek if (error == 0) {
10658b70e6aeSPawel Jakub Dawidek sync_start();
1066ac0401e3SPawel Jakub Dawidek } else if (error == EBUSY) {
1067ac0401e3SPawel Jakub Dawidek time_t start = time(NULL);
1068ac0401e3SPawel Jakub Dawidek
1069ac0401e3SPawel Jakub Dawidek pjdlog_warning("Waiting for remote node to become %s for %ds.",
1070ac0401e3SPawel Jakub Dawidek role2str(HAST_ROLE_SECONDARY),
1071ac0401e3SPawel Jakub Dawidek res->hr_timeout);
1072ac0401e3SPawel Jakub Dawidek for (;;) {
1073ac0401e3SPawel Jakub Dawidek sleep(1);
1074ac0401e3SPawel Jakub Dawidek error = init_remote(res, NULL, NULL);
1075ac0401e3SPawel Jakub Dawidek if (error != EBUSY)
1076ac0401e3SPawel Jakub Dawidek break;
1077ac0401e3SPawel Jakub Dawidek if (time(NULL) > start + res->hr_timeout)
1078ac0401e3SPawel Jakub Dawidek break;
1079ac0401e3SPawel Jakub Dawidek }
1080ac0401e3SPawel Jakub Dawidek if (error == EBUSY) {
1081ac0401e3SPawel Jakub Dawidek pjdlog_warning("Remote node is still %s, starting anyway.",
1082ac0401e3SPawel Jakub Dawidek role2str(HAST_ROLE_PRIMARY));
1083ac0401e3SPawel Jakub Dawidek }
1084ac0401e3SPawel Jakub Dawidek }
1085ac0401e3SPawel Jakub Dawidek }
108632115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, ggate_recv_thread, res);
10872ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0);
108832115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, local_send_thread, res);
10892ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0);
109032115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, remote_send_thread, res);
10912ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0);
109232115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, remote_recv_thread, res);
10932ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0);
109432115b10SPawel Jakub Dawidek error = pthread_create(&td, NULL, ggate_send_thread, res);
10952ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(error == 0);
1096ac0401e3SPawel Jakub Dawidek fullystarted = true;
10974a88128bSPawel Jakub Dawidek (void)sync_thread(res);
109832115b10SPawel Jakub Dawidek }
109932115b10SPawel Jakub Dawidek
110032115b10SPawel Jakub Dawidek static void
reqlog(int loglevel,int debuglevel,struct g_gate_ctl_io * ggio,const char * fmt,...)1101d6e636c9SPawel Jakub Dawidek reqlog(int loglevel, int debuglevel, struct g_gate_ctl_io *ggio,
1102d6e636c9SPawel Jakub Dawidek const char *fmt, ...)
110332115b10SPawel Jakub Dawidek {
110432115b10SPawel Jakub Dawidek char msg[1024];
110532115b10SPawel Jakub Dawidek va_list ap;
110632115b10SPawel Jakub Dawidek
110732115b10SPawel Jakub Dawidek va_start(ap, fmt);
11084c71d263SPawel Jakub Dawidek (void)vsnprintf(msg, sizeof(msg), fmt, ap);
110932115b10SPawel Jakub Dawidek va_end(ap);
111032115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) {
111132115b10SPawel Jakub Dawidek case BIO_READ:
11124c71d263SPawel Jakub Dawidek (void)snprlcat(msg, sizeof(msg), "READ(%ju, %ju).",
1113d6e636c9SPawel Jakub Dawidek (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
111432115b10SPawel Jakub Dawidek break;
111532115b10SPawel Jakub Dawidek case BIO_DELETE:
11164c71d263SPawel Jakub Dawidek (void)snprlcat(msg, sizeof(msg), "DELETE(%ju, %ju).",
1117d6e636c9SPawel Jakub Dawidek (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
111832115b10SPawel Jakub Dawidek break;
111932115b10SPawel Jakub Dawidek case BIO_FLUSH:
11204c71d263SPawel Jakub Dawidek (void)snprlcat(msg, sizeof(msg), "FLUSH.");
112132115b10SPawel Jakub Dawidek break;
112232115b10SPawel Jakub Dawidek case BIO_WRITE:
11234c71d263SPawel Jakub Dawidek (void)snprlcat(msg, sizeof(msg), "WRITE(%ju, %ju).",
1124d6e636c9SPawel Jakub Dawidek (uintmax_t)ggio->gctl_offset, (uintmax_t)ggio->gctl_length);
112532115b10SPawel Jakub Dawidek break;
112632115b10SPawel Jakub Dawidek default:
11274c71d263SPawel Jakub Dawidek (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%u).",
11284c71d263SPawel Jakub Dawidek (unsigned int)ggio->gctl_cmd);
112932115b10SPawel Jakub Dawidek break;
113032115b10SPawel Jakub Dawidek }
113132115b10SPawel Jakub Dawidek pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
113232115b10SPawel Jakub Dawidek }
113332115b10SPawel Jakub Dawidek
113432115b10SPawel Jakub Dawidek static void
remote_close(struct hast_resource * res,int ncomp)113532115b10SPawel Jakub Dawidek remote_close(struct hast_resource *res, int ncomp)
113632115b10SPawel Jakub Dawidek {
113732115b10SPawel Jakub Dawidek
113832115b10SPawel Jakub Dawidek rw_wlock(&hio_remote_lock[ncomp]);
113932115b10SPawel Jakub Dawidek /*
11405a58d22aSPawel Jakub Dawidek * Check for a race between dropping rlock and acquiring wlock -
114132115b10SPawel Jakub Dawidek * another thread can close connection in-between.
114232115b10SPawel Jakub Dawidek */
114332115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) {
11442ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL);
11452ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL);
114632115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
114732115b10SPawel Jakub Dawidek return;
114832115b10SPawel Jakub Dawidek }
114932115b10SPawel Jakub Dawidek
11502ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL);
11512ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL);
115232115b10SPawel Jakub Dawidek
11538f8c798cSPawel Jakub Dawidek pjdlog_debug(2, "Closing incoming connection to %s.",
115432115b10SPawel Jakub Dawidek res->hr_remoteaddr);
115532115b10SPawel Jakub Dawidek proto_close(res->hr_remotein);
115632115b10SPawel Jakub Dawidek res->hr_remotein = NULL;
11578f8c798cSPawel Jakub Dawidek pjdlog_debug(2, "Closing outgoing connection to %s.",
115832115b10SPawel Jakub Dawidek res->hr_remoteaddr);
115932115b10SPawel Jakub Dawidek proto_close(res->hr_remoteout);
116032115b10SPawel Jakub Dawidek res->hr_remoteout = NULL;
116132115b10SPawel Jakub Dawidek
116232115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
116332115b10SPawel Jakub Dawidek
11648f8c798cSPawel Jakub Dawidek pjdlog_warning("Disconnected from %s.", res->hr_remoteaddr);
11658f8c798cSPawel Jakub Dawidek
116632115b10SPawel Jakub Dawidek /*
116732115b10SPawel Jakub Dawidek * Stop synchronization if in-progress.
116832115b10SPawel Jakub Dawidek */
116955ce1e7cSPawel Jakub Dawidek sync_stop();
11705b41e644SPawel Jakub Dawidek
11715bdff860SPawel Jakub Dawidek event_send(res, EVENT_DISCONNECT);
1172f7fe83f9SPawel Jakub Dawidek }
117332115b10SPawel Jakub Dawidek
117432115b10SPawel Jakub Dawidek /*
117507ebc362SPawel Jakub Dawidek * Acknowledge write completion to the kernel, but don't update activemap yet.
117607ebc362SPawel Jakub Dawidek */
117707ebc362SPawel Jakub Dawidek static void
write_complete(struct hast_resource * res,struct hio * hio)117807ebc362SPawel Jakub Dawidek write_complete(struct hast_resource *res, struct hio *hio)
117907ebc362SPawel Jakub Dawidek {
118007ebc362SPawel Jakub Dawidek struct g_gate_ctl_io *ggio;
118107ebc362SPawel Jakub Dawidek unsigned int ncomp;
118207ebc362SPawel Jakub Dawidek
118307ebc362SPawel Jakub Dawidek PJDLOG_ASSERT(!hio->hio_done);
118407ebc362SPawel Jakub Dawidek
118507ebc362SPawel Jakub Dawidek ggio = &hio->hio_ggio;
118607ebc362SPawel Jakub Dawidek PJDLOG_ASSERT(ggio->gctl_cmd == BIO_WRITE);
118707ebc362SPawel Jakub Dawidek
118807ebc362SPawel Jakub Dawidek /*
118907ebc362SPawel Jakub Dawidek * Bump local count if this is first write after
119007ebc362SPawel Jakub Dawidek * connection failure with remote node.
119107ebc362SPawel Jakub Dawidek */
119207ebc362SPawel Jakub Dawidek ncomp = 1;
119307ebc362SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]);
119407ebc362SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) {
119507ebc362SPawel Jakub Dawidek mtx_lock(&metadata_lock);
119607ebc362SPawel Jakub Dawidek if (res->hr_primary_localcnt == res->hr_secondary_remotecnt) {
119707ebc362SPawel Jakub Dawidek res->hr_primary_localcnt++;
119807ebc362SPawel Jakub Dawidek pjdlog_debug(1, "Increasing localcnt to %ju.",
119907ebc362SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt);
120007ebc362SPawel Jakub Dawidek (void)metadata_write(res);
120107ebc362SPawel Jakub Dawidek }
120207ebc362SPawel Jakub Dawidek mtx_unlock(&metadata_lock);
120307ebc362SPawel Jakub Dawidek }
120407ebc362SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
12052b1b224dSPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1)
120607ebc362SPawel Jakub Dawidek primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
120707ebc362SPawel Jakub Dawidek hio->hio_done = true;
120807ebc362SPawel Jakub Dawidek }
120907ebc362SPawel Jakub Dawidek
121007ebc362SPawel Jakub Dawidek /*
121132115b10SPawel Jakub Dawidek * Thread receives ggate I/O requests from the kernel and passes them to
121232115b10SPawel Jakub Dawidek * appropriate threads:
121332115b10SPawel Jakub Dawidek * WRITE - always goes to both local_send and remote_send threads
121432115b10SPawel Jakub Dawidek * READ (when the block is up-to-date on local component) -
121532115b10SPawel Jakub Dawidek * only local_send thread
121632115b10SPawel Jakub Dawidek * READ (when the block isn't up-to-date on local component) -
121732115b10SPawel Jakub Dawidek * only remote_send thread
121832115b10SPawel Jakub Dawidek * DELETE - always goes to both local_send and remote_send threads
121932115b10SPawel Jakub Dawidek * FLUSH - always goes to both local_send and remote_send threads
122032115b10SPawel Jakub Dawidek */
122132115b10SPawel Jakub Dawidek static void *
ggate_recv_thread(void * arg)122232115b10SPawel Jakub Dawidek ggate_recv_thread(void *arg)
122332115b10SPawel Jakub Dawidek {
122432115b10SPawel Jakub Dawidek struct hast_resource *res = arg;
122532115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio;
122632115b10SPawel Jakub Dawidek struct hio *hio;
122732115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps;
122832115b10SPawel Jakub Dawidek int error;
122932115b10SPawel Jakub Dawidek
123032115b10SPawel Jakub Dawidek for (;;) {
123132115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_recv: Taking free request.");
123232115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, free);
123332115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_recv: (%p) Got free request.", hio);
123432115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio;
123532115b10SPawel Jakub Dawidek ggio->gctl_unit = res->hr_ggateunit;
123632115b10SPawel Jakub Dawidek ggio->gctl_length = MAXPHYS;
123732115b10SPawel Jakub Dawidek ggio->gctl_error = 0;
123807ebc362SPawel Jakub Dawidek hio->hio_done = false;
123907ebc362SPawel Jakub Dawidek hio->hio_replication = res->hr_replication;
124032115b10SPawel Jakub Dawidek pjdlog_debug(2,
124132115b10SPawel Jakub Dawidek "ggate_recv: (%p) Waiting for request from the kernel.",
124232115b10SPawel Jakub Dawidek hio);
12432b1b224dSPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) {
124432115b10SPawel Jakub Dawidek if (sigexit_received)
124532115b10SPawel Jakub Dawidek pthread_exit(NULL);
124632115b10SPawel Jakub Dawidek primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
124732115b10SPawel Jakub Dawidek }
124832115b10SPawel Jakub Dawidek error = ggio->gctl_error;
124932115b10SPawel Jakub Dawidek switch (error) {
125032115b10SPawel Jakub Dawidek case 0:
125132115b10SPawel Jakub Dawidek break;
125232115b10SPawel Jakub Dawidek case ECANCELED:
125332115b10SPawel Jakub Dawidek /* Exit gracefully. */
125432115b10SPawel Jakub Dawidek if (!sigexit_received) {
125532115b10SPawel Jakub Dawidek pjdlog_debug(2,
125632115b10SPawel Jakub Dawidek "ggate_recv: (%p) Received cancel from the kernel.",
125732115b10SPawel Jakub Dawidek hio);
125832115b10SPawel Jakub Dawidek pjdlog_info("Received cancel from the kernel, exiting.");
125932115b10SPawel Jakub Dawidek }
126032115b10SPawel Jakub Dawidek pthread_exit(NULL);
126132115b10SPawel Jakub Dawidek case ENOMEM:
126232115b10SPawel Jakub Dawidek /*
126332115b10SPawel Jakub Dawidek * Buffer too small? Impossible, we allocate MAXPHYS
126432115b10SPawel Jakub Dawidek * bytes - request can't be bigger than that.
126532115b10SPawel Jakub Dawidek */
126632115b10SPawel Jakub Dawidek /* FALLTHROUGH */
126732115b10SPawel Jakub Dawidek case ENXIO:
126832115b10SPawel Jakub Dawidek default:
126932115b10SPawel Jakub Dawidek primary_exitx(EX_OSERR, "G_GATE_CMD_START failed: %s.",
127032115b10SPawel Jakub Dawidek strerror(error));
127132115b10SPawel Jakub Dawidek }
127207ebc362SPawel Jakub Dawidek
127307ebc362SPawel Jakub Dawidek ncomp = 0;
127407ebc362SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS;
127507ebc362SPawel Jakub Dawidek
127632115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++)
127732115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL;
127832115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio,
127932115b10SPawel Jakub Dawidek "ggate_recv: (%p) Request received from the kernel: ",
128032115b10SPawel Jakub Dawidek hio);
128107ebc362SPawel Jakub Dawidek
128232115b10SPawel Jakub Dawidek /*
128332115b10SPawel Jakub Dawidek * Inform all components about new write request.
128432115b10SPawel Jakub Dawidek * For read request prefer local component unless the given
128532115b10SPawel Jakub Dawidek * range is out-of-date, then use remote component.
128632115b10SPawel Jakub Dawidek */
128732115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) {
128832115b10SPawel Jakub Dawidek case BIO_READ:
12893db86c39SPawel Jakub Dawidek res->hr_stat_read++;
129007ebc362SPawel Jakub Dawidek ncomps = 1;
129132115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock);
129232115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_UNDEF ||
129332115b10SPawel Jakub Dawidek res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
129432115b10SPawel Jakub Dawidek /*
129532115b10SPawel Jakub Dawidek * This range is up-to-date on local component,
129632115b10SPawel Jakub Dawidek * so handle request locally.
129732115b10SPawel Jakub Dawidek */
129832115b10SPawel Jakub Dawidek /* Local component is 0 for now. */
129932115b10SPawel Jakub Dawidek ncomp = 0;
130032115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc ==
130132115b10SPawel Jakub Dawidek HAST_SYNCSRC_SECONDARY) */ {
13022ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc ==
130332115b10SPawel Jakub Dawidek HAST_SYNCSRC_SECONDARY);
130432115b10SPawel Jakub Dawidek /*
130532115b10SPawel Jakub Dawidek * This range is out-of-date on local component,
130632115b10SPawel Jakub Dawidek * so send request to the remote node.
130732115b10SPawel Jakub Dawidek */
130832115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */
130932115b10SPawel Jakub Dawidek ncomp = 1;
131032115b10SPawel Jakub Dawidek }
131132115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock);
131232115b10SPawel Jakub Dawidek break;
131332115b10SPawel Jakub Dawidek case BIO_WRITE:
13143db86c39SPawel Jakub Dawidek res->hr_stat_write++;
13158a34134aSPawel Jakub Dawidek if (res->hr_resuid == 0 &&
13168a34134aSPawel Jakub Dawidek res->hr_primary_localcnt == 0) {
13178a34134aSPawel Jakub Dawidek /* This is first write. */
13189446b453SPawel Jakub Dawidek res->hr_primary_localcnt = 1;
1319ce837469SPawel Jakub Dawidek }
132032115b10SPawel Jakub Dawidek for (;;) {
132132115b10SPawel Jakub Dawidek mtx_lock(&range_lock);
132232115b10SPawel Jakub Dawidek if (rangelock_islocked(range_sync,
132332115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length)) {
132432115b10SPawel Jakub Dawidek pjdlog_debug(2,
132532115b10SPawel Jakub Dawidek "regular: Range offset=%jd length=%zu locked.",
132632115b10SPawel Jakub Dawidek (intmax_t)ggio->gctl_offset,
132732115b10SPawel Jakub Dawidek (size_t)ggio->gctl_length);
132832115b10SPawel Jakub Dawidek range_regular_wait = true;
132932115b10SPawel Jakub Dawidek cv_wait(&range_regular_cond, &range_lock);
133032115b10SPawel Jakub Dawidek range_regular_wait = false;
133132115b10SPawel Jakub Dawidek mtx_unlock(&range_lock);
133232115b10SPawel Jakub Dawidek continue;
133332115b10SPawel Jakub Dawidek }
133432115b10SPawel Jakub Dawidek if (rangelock_add(range_regular,
13352b1b224dSPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length) == -1) {
133632115b10SPawel Jakub Dawidek mtx_unlock(&range_lock);
133732115b10SPawel Jakub Dawidek pjdlog_debug(2,
133832115b10SPawel Jakub Dawidek "regular: Range offset=%jd length=%zu is already locked, waiting.",
133932115b10SPawel Jakub Dawidek (intmax_t)ggio->gctl_offset,
134032115b10SPawel Jakub Dawidek (size_t)ggio->gctl_length);
134132115b10SPawel Jakub Dawidek sleep(1);
134232115b10SPawel Jakub Dawidek continue;
134332115b10SPawel Jakub Dawidek }
134432115b10SPawel Jakub Dawidek mtx_unlock(&range_lock);
134532115b10SPawel Jakub Dawidek break;
134632115b10SPawel Jakub Dawidek }
134732115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock);
134832115b10SPawel Jakub Dawidek if (activemap_write_start(res->hr_amp,
134932115b10SPawel Jakub Dawidek ggio->gctl_offset, ggio->gctl_length)) {
13503db86c39SPawel Jakub Dawidek res->hr_stat_activemap_update++;
135132115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res);
1352a818a4ffSMikolaj Golub } else {
135332115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock);
1354a818a4ffSMikolaj Golub }
13558f04423fSMikolaj Golub if (ISMEMSYNC(hio)) {
13565d69ed53SMikolaj Golub hio->hio_memsyncacked = false;
13575d69ed53SMikolaj Golub refcnt_init(&hio->hio_writecount, ncomps);
13585d69ed53SMikolaj Golub }
135907ebc362SPawel Jakub Dawidek break;
13603db86c39SPawel Jakub Dawidek case BIO_DELETE:
13613db86c39SPawel Jakub Dawidek res->hr_stat_delete++;
13623db86c39SPawel Jakub Dawidek break;
13633db86c39SPawel Jakub Dawidek case BIO_FLUSH:
13643db86c39SPawel Jakub Dawidek res->hr_stat_flush++;
13653db86c39SPawel Jakub Dawidek break;
13663db86c39SPawel Jakub Dawidek }
136732115b10SPawel Jakub Dawidek pjdlog_debug(2,
136807ebc362SPawel Jakub Dawidek "ggate_recv: (%p) Moving request to the send queues.", hio);
13696e87c151SEd Schouten refcnt_init(&hio->hio_countdown, ncomps);
1370d6e636c9SPawel Jakub Dawidek for (ii = ncomp; ii < ncomps; ii++)
137132115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ii);
137232115b10SPawel Jakub Dawidek }
137332115b10SPawel Jakub Dawidek /* NOTREACHED */
137432115b10SPawel Jakub Dawidek return (NULL);
137532115b10SPawel Jakub Dawidek }
137632115b10SPawel Jakub Dawidek
137732115b10SPawel Jakub Dawidek /*
137832115b10SPawel Jakub Dawidek * Thread reads from or writes to local component.
137932115b10SPawel Jakub Dawidek * If local read fails, it redirects it to remote_send thread.
138032115b10SPawel Jakub Dawidek */
138132115b10SPawel Jakub Dawidek static void *
local_send_thread(void * arg)138232115b10SPawel Jakub Dawidek local_send_thread(void *arg)
138332115b10SPawel Jakub Dawidek {
138432115b10SPawel Jakub Dawidek struct hast_resource *res = arg;
138532115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio;
138632115b10SPawel Jakub Dawidek struct hio *hio;
138732115b10SPawel Jakub Dawidek unsigned int ncomp, rncomp;
138832115b10SPawel Jakub Dawidek ssize_t ret;
138932115b10SPawel Jakub Dawidek
139032115b10SPawel Jakub Dawidek /* Local component is 0 for now. */
139132115b10SPawel Jakub Dawidek ncomp = 0;
139232115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */
139332115b10SPawel Jakub Dawidek rncomp = 1;
139432115b10SPawel Jakub Dawidek
139532115b10SPawel Jakub Dawidek for (;;) {
139632115b10SPawel Jakub Dawidek pjdlog_debug(2, "local_send: Taking request.");
1397448efa94SPawel Jakub Dawidek QUEUE_TAKE1(hio, send, ncomp, 0);
139832115b10SPawel Jakub Dawidek pjdlog_debug(2, "local_send: (%p) Got request.", hio);
139932115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio;
140032115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) {
140132115b10SPawel Jakub Dawidek case BIO_READ:
140232115b10SPawel Jakub Dawidek ret = pread(res->hr_localfd, ggio->gctl_data,
140332115b10SPawel Jakub Dawidek ggio->gctl_length,
140432115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff);
140532115b10SPawel Jakub Dawidek if (ret == ggio->gctl_length)
140632115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0;
1407a01a750fSMikolaj Golub else if (!ISSYNCREQ(hio)) {
140832115b10SPawel Jakub Dawidek /*
140932115b10SPawel Jakub Dawidek * If READ failed, try to read from remote node.
141032115b10SPawel Jakub Dawidek */
14112b1b224dSPawel Jakub Dawidek if (ret == -1) {
1412cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio,
1413cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s), trying remote node. ",
1414cd7b7ee5SPawel Jakub Dawidek strerror(errno));
1415cd7b7ee5SPawel Jakub Dawidek } else if (ret != ggio->gctl_length) {
1416cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio,
1417cd7b7ee5SPawel Jakub Dawidek "Local request failed (%zd != %jd), trying remote node. ",
1418fba1bf5aSPawel Jakub Dawidek ret, (intmax_t)ggio->gctl_length);
1419cd7b7ee5SPawel Jakub Dawidek }
142032115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, rncomp);
142132115b10SPawel Jakub Dawidek continue;
142232115b10SPawel Jakub Dawidek }
142332115b10SPawel Jakub Dawidek break;
142432115b10SPawel Jakub Dawidek case BIO_WRITE:
142532115b10SPawel Jakub Dawidek ret = pwrite(res->hr_localfd, ggio->gctl_data,
142632115b10SPawel Jakub Dawidek ggio->gctl_length,
142732115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff);
14282b1b224dSPawel Jakub Dawidek if (ret == -1) {
142932115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno;
1430cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio,
1431cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ",
1432cd7b7ee5SPawel Jakub Dawidek strerror(errno));
1433cd7b7ee5SPawel Jakub Dawidek } else if (ret != ggio->gctl_length) {
143432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = EIO;
1435cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio,
1436cd7b7ee5SPawel Jakub Dawidek "Local request failed (%zd != %jd): ",
1437fba1bf5aSPawel Jakub Dawidek ret, (intmax_t)ggio->gctl_length);
1438cd7b7ee5SPawel Jakub Dawidek } else {
143932115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0;
14408f04423fSMikolaj Golub if (ISASYNC(hio)) {
144107ebc362SPawel Jakub Dawidek ggio->gctl_error = 0;
144207ebc362SPawel Jakub Dawidek write_complete(res, hio);
144307ebc362SPawel Jakub Dawidek }
1444cd7b7ee5SPawel Jakub Dawidek }
144532115b10SPawel Jakub Dawidek break;
144632115b10SPawel Jakub Dawidek case BIO_DELETE:
144732115b10SPawel Jakub Dawidek ret = g_delete(res->hr_localfd,
144832115b10SPawel Jakub Dawidek ggio->gctl_offset + res->hr_localoff,
144932115b10SPawel Jakub Dawidek ggio->gctl_length);
14502b1b224dSPawel Jakub Dawidek if (ret == -1) {
145132115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno;
1452cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio,
1453cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ",
1454cd7b7ee5SPawel Jakub Dawidek strerror(errno));
1455cd7b7ee5SPawel Jakub Dawidek } else {
145632115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0;
1457cd7b7ee5SPawel Jakub Dawidek }
145832115b10SPawel Jakub Dawidek break;
145932115b10SPawel Jakub Dawidek case BIO_FLUSH:
146012daf727SPawel Jakub Dawidek if (!res->hr_localflush) {
146112daf727SPawel Jakub Dawidek ret = -1;
146212daf727SPawel Jakub Dawidek errno = EOPNOTSUPP;
146312daf727SPawel Jakub Dawidek break;
146412daf727SPawel Jakub Dawidek }
146532115b10SPawel Jakub Dawidek ret = g_flush(res->hr_localfd);
14662b1b224dSPawel Jakub Dawidek if (ret == -1) {
146712daf727SPawel Jakub Dawidek if (errno == EOPNOTSUPP)
146812daf727SPawel Jakub Dawidek res->hr_localflush = false;
146932115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno;
1470cd7b7ee5SPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio,
1471cd7b7ee5SPawel Jakub Dawidek "Local request failed (%s): ",
1472cd7b7ee5SPawel Jakub Dawidek strerror(errno));
1473cd7b7ee5SPawel Jakub Dawidek } else {
147432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0;
1475cd7b7ee5SPawel Jakub Dawidek }
147632115b10SPawel Jakub Dawidek break;
147732115b10SPawel Jakub Dawidek }
14785d69ed53SMikolaj Golub if (ISMEMSYNCWRITE(hio)) {
14795d69ed53SMikolaj Golub if (refcnt_release(&hio->hio_writecount) == 0) {
14805d69ed53SMikolaj Golub write_complete(res, hio);
14815d69ed53SMikolaj Golub }
14825d69ed53SMikolaj Golub }
1483d6e636c9SPawel Jakub Dawidek if (refcnt_release(&hio->hio_countdown) > 0)
148443b8675bSPawel Jakub Dawidek continue;
148532115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) {
148632115b10SPawel Jakub Dawidek mtx_lock(&sync_lock);
148732115b10SPawel Jakub Dawidek SYNCREQDONE(hio);
148832115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock);
148932115b10SPawel Jakub Dawidek cv_signal(&sync_cond);
149032115b10SPawel Jakub Dawidek } else {
149132115b10SPawel Jakub Dawidek pjdlog_debug(2,
149232115b10SPawel Jakub Dawidek "local_send: (%p) Moving request to the done queue.",
149332115b10SPawel Jakub Dawidek hio);
149432115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done);
149532115b10SPawel Jakub Dawidek }
149632115b10SPawel Jakub Dawidek }
149732115b10SPawel Jakub Dawidek /* NOTREACHED */
149832115b10SPawel Jakub Dawidek return (NULL);
149932115b10SPawel Jakub Dawidek }
150032115b10SPawel Jakub Dawidek
1501448efa94SPawel Jakub Dawidek static void
keepalive_send(struct hast_resource * res,unsigned int ncomp)1502448efa94SPawel Jakub Dawidek keepalive_send(struct hast_resource *res, unsigned int ncomp)
1503448efa94SPawel Jakub Dawidek {
1504448efa94SPawel Jakub Dawidek struct nv *nv;
1505448efa94SPawel Jakub Dawidek
150621e7bc5eSPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]);
150721e7bc5eSPawel Jakub Dawidek
150821e7bc5eSPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) {
150921e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
1510448efa94SPawel Jakub Dawidek return;
151121e7bc5eSPawel Jakub Dawidek }
1512448efa94SPawel Jakub Dawidek
15132ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL);
15142ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL);
1515448efa94SPawel Jakub Dawidek
1516448efa94SPawel Jakub Dawidek nv = nv_alloc();
1517448efa94SPawel Jakub Dawidek nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
1518448efa94SPawel Jakub Dawidek if (nv_error(nv) != 0) {
151921e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
1520448efa94SPawel Jakub Dawidek nv_free(nv);
1521448efa94SPawel Jakub Dawidek pjdlog_debug(1,
1522448efa94SPawel Jakub Dawidek "keepalive_send: Unable to prepare header to send.");
1523448efa94SPawel Jakub Dawidek return;
1524448efa94SPawel Jakub Dawidek }
15252b1b224dSPawel Jakub Dawidek if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
152621e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
1527448efa94SPawel Jakub Dawidek pjdlog_common(LOG_DEBUG, 1, errno,
1528448efa94SPawel Jakub Dawidek "keepalive_send: Unable to send request");
1529448efa94SPawel Jakub Dawidek nv_free(nv);
1530448efa94SPawel Jakub Dawidek remote_close(res, ncomp);
1531448efa94SPawel Jakub Dawidek return;
1532448efa94SPawel Jakub Dawidek }
153321e7bc5eSPawel Jakub Dawidek
153421e7bc5eSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
1535448efa94SPawel Jakub Dawidek nv_free(nv);
1536448efa94SPawel Jakub Dawidek pjdlog_debug(2, "keepalive_send: Request sent.");
1537448efa94SPawel Jakub Dawidek }
1538448efa94SPawel Jakub Dawidek
153932115b10SPawel Jakub Dawidek /*
154032115b10SPawel Jakub Dawidek * Thread sends request to secondary node.
154132115b10SPawel Jakub Dawidek */
154232115b10SPawel Jakub Dawidek static void *
remote_send_thread(void * arg)154332115b10SPawel Jakub Dawidek remote_send_thread(void *arg)
154432115b10SPawel Jakub Dawidek {
154532115b10SPawel Jakub Dawidek struct hast_resource *res = arg;
154632115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio;
1547448efa94SPawel Jakub Dawidek time_t lastcheck, now;
154832115b10SPawel Jakub Dawidek struct hio *hio;
154932115b10SPawel Jakub Dawidek struct nv *nv;
155032115b10SPawel Jakub Dawidek unsigned int ncomp;
155132115b10SPawel Jakub Dawidek bool wakeup;
155232115b10SPawel Jakub Dawidek uint64_t offset, length;
155332115b10SPawel Jakub Dawidek uint8_t cmd;
155432115b10SPawel Jakub Dawidek void *data;
155532115b10SPawel Jakub Dawidek
155632115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */
155732115b10SPawel Jakub Dawidek ncomp = 1;
1558448efa94SPawel Jakub Dawidek lastcheck = time(NULL);
155932115b10SPawel Jakub Dawidek
156032115b10SPawel Jakub Dawidek for (;;) {
156132115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_send: Taking request.");
15628d7dcf14SMikolaj Golub QUEUE_TAKE1(hio, send, ncomp, HAST_KEEPALIVE);
1563448efa94SPawel Jakub Dawidek if (hio == NULL) {
1564448efa94SPawel Jakub Dawidek now = time(NULL);
15658d7dcf14SMikolaj Golub if (lastcheck + HAST_KEEPALIVE <= now) {
1566448efa94SPawel Jakub Dawidek keepalive_send(res, ncomp);
1567448efa94SPawel Jakub Dawidek lastcheck = now;
1568448efa94SPawel Jakub Dawidek }
1569448efa94SPawel Jakub Dawidek continue;
1570448efa94SPawel Jakub Dawidek }
157132115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_send: (%p) Got request.", hio);
157232115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio;
157332115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) {
157432115b10SPawel Jakub Dawidek case BIO_READ:
157532115b10SPawel Jakub Dawidek cmd = HIO_READ;
157632115b10SPawel Jakub Dawidek data = NULL;
157732115b10SPawel Jakub Dawidek offset = ggio->gctl_offset;
157832115b10SPawel Jakub Dawidek length = ggio->gctl_length;
157932115b10SPawel Jakub Dawidek break;
158032115b10SPawel Jakub Dawidek case BIO_WRITE:
158132115b10SPawel Jakub Dawidek cmd = HIO_WRITE;
158232115b10SPawel Jakub Dawidek data = ggio->gctl_data;
158332115b10SPawel Jakub Dawidek offset = ggio->gctl_offset;
158432115b10SPawel Jakub Dawidek length = ggio->gctl_length;
158532115b10SPawel Jakub Dawidek break;
158632115b10SPawel Jakub Dawidek case BIO_DELETE:
158732115b10SPawel Jakub Dawidek cmd = HIO_DELETE;
158832115b10SPawel Jakub Dawidek data = NULL;
158932115b10SPawel Jakub Dawidek offset = ggio->gctl_offset;
159032115b10SPawel Jakub Dawidek length = ggio->gctl_length;
159132115b10SPawel Jakub Dawidek break;
159232115b10SPawel Jakub Dawidek case BIO_FLUSH:
159332115b10SPawel Jakub Dawidek cmd = HIO_FLUSH;
159432115b10SPawel Jakub Dawidek data = NULL;
159532115b10SPawel Jakub Dawidek offset = 0;
159632115b10SPawel Jakub Dawidek length = 0;
159732115b10SPawel Jakub Dawidek break;
159832115b10SPawel Jakub Dawidek default:
159909c2e843SPawel Jakub Dawidek PJDLOG_ABORT("invalid condition");
160032115b10SPawel Jakub Dawidek }
160132115b10SPawel Jakub Dawidek nv = nv_alloc();
160232115b10SPawel Jakub Dawidek nv_add_uint8(nv, cmd, "cmd");
160332115b10SPawel Jakub Dawidek nv_add_uint64(nv, (uint64_t)ggio->gctl_seq, "seq");
160432115b10SPawel Jakub Dawidek nv_add_uint64(nv, offset, "offset");
160532115b10SPawel Jakub Dawidek nv_add_uint64(nv, length, "length");
16065d69ed53SMikolaj Golub if (ISMEMSYNCWRITE(hio))
1607d6e636c9SPawel Jakub Dawidek nv_add_uint8(nv, 1, "memsync");
160832115b10SPawel Jakub Dawidek if (nv_error(nv) != 0) {
160932115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = nv_error(nv);
161032115b10SPawel Jakub Dawidek pjdlog_debug(2,
161132115b10SPawel Jakub Dawidek "remote_send: (%p) Unable to prepare header to send.",
161232115b10SPawel Jakub Dawidek hio);
161332115b10SPawel Jakub Dawidek reqlog(LOG_ERR, 0, ggio,
161432115b10SPawel Jakub Dawidek "Unable to prepare header to send (%s): ",
161532115b10SPawel Jakub Dawidek strerror(nv_error(nv)));
161632115b10SPawel Jakub Dawidek /* Move failed request immediately to the done queue. */
161732115b10SPawel Jakub Dawidek goto done_queue;
161832115b10SPawel Jakub Dawidek }
161932115b10SPawel Jakub Dawidek /*
162032115b10SPawel Jakub Dawidek * Protect connection from disappearing.
162132115b10SPawel Jakub Dawidek */
162232115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]);
162332115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) {
162432115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
162532115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = ENOTCONN;
162632115b10SPawel Jakub Dawidek goto done_queue;
162732115b10SPawel Jakub Dawidek }
162832115b10SPawel Jakub Dawidek /*
162932115b10SPawel Jakub Dawidek * Move the request to recv queue before sending it, because
163032115b10SPawel Jakub Dawidek * in different order we can get reply before we move request
163132115b10SPawel Jakub Dawidek * to recv queue.
163232115b10SPawel Jakub Dawidek */
16331212a85cSPawel Jakub Dawidek pjdlog_debug(2,
16341212a85cSPawel Jakub Dawidek "remote_send: (%p) Moving request to the recv queue.",
16351212a85cSPawel Jakub Dawidek hio);
163632115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]);
163732115b10SPawel Jakub Dawidek wakeup = TAILQ_EMPTY(&hio_recv_list[ncomp]);
163832115b10SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
16396b66c350SMikolaj Golub hio_recv_list_size[ncomp]++;
164032115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]);
164132115b10SPawel Jakub Dawidek if (hast_proto_send(res, res->hr_remoteout, nv, data,
16422b1b224dSPawel Jakub Dawidek data != NULL ? length : 0) == -1) {
164332115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno;
164432115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
164532115b10SPawel Jakub Dawidek pjdlog_debug(2,
164632115b10SPawel Jakub Dawidek "remote_send: (%p) Unable to send request.", hio);
164732115b10SPawel Jakub Dawidek reqlog(LOG_ERR, 0, ggio,
164832115b10SPawel Jakub Dawidek "Unable to send request (%s): ",
164932115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp]));
1650ee087cdfSPawel Jakub Dawidek remote_close(res, ncomp);
1651d685f88bSMikolaj Golub } else {
165232115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
1653d685f88bSMikolaj Golub }
165432115b10SPawel Jakub Dawidek nv_free(nv);
165532115b10SPawel Jakub Dawidek if (wakeup)
165632115b10SPawel Jakub Dawidek cv_signal(&hio_recv_list_cond[ncomp]);
165732115b10SPawel Jakub Dawidek continue;
165832115b10SPawel Jakub Dawidek done_queue:
165932115b10SPawel Jakub Dawidek nv_free(nv);
166032115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) {
1661d6e636c9SPawel Jakub Dawidek if (refcnt_release(&hio->hio_countdown) > 0)
166232115b10SPawel Jakub Dawidek continue;
166332115b10SPawel Jakub Dawidek mtx_lock(&sync_lock);
166432115b10SPawel Jakub Dawidek SYNCREQDONE(hio);
166532115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock);
166632115b10SPawel Jakub Dawidek cv_signal(&sync_cond);
166732115b10SPawel Jakub Dawidek continue;
166832115b10SPawel Jakub Dawidek }
166932115b10SPawel Jakub Dawidek if (ggio->gctl_cmd == BIO_WRITE) {
167032115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock);
167132115b10SPawel Jakub Dawidek if (activemap_need_sync(res->hr_amp, ggio->gctl_offset,
167232115b10SPawel Jakub Dawidek ggio->gctl_length)) {
167332115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res);
1674a818a4ffSMikolaj Golub } else {
167532115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock);
1676a818a4ffSMikolaj Golub }
16775d69ed53SMikolaj Golub if (ISMEMSYNCWRITE(hio)) {
16785d69ed53SMikolaj Golub if (refcnt_release(&hio->hio_writecount) == 0) {
16795d69ed53SMikolaj Golub if (hio->hio_errors[0] == 0)
16805d69ed53SMikolaj Golub write_complete(res, hio);
16815d69ed53SMikolaj Golub }
16825d69ed53SMikolaj Golub }
168332115b10SPawel Jakub Dawidek }
1684d6e636c9SPawel Jakub Dawidek if (refcnt_release(&hio->hio_countdown) > 0)
168532115b10SPawel Jakub Dawidek continue;
168632115b10SPawel Jakub Dawidek pjdlog_debug(2,
168732115b10SPawel Jakub Dawidek "remote_send: (%p) Moving request to the done queue.",
168832115b10SPawel Jakub Dawidek hio);
168932115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done);
169032115b10SPawel Jakub Dawidek }
169132115b10SPawel Jakub Dawidek /* NOTREACHED */
169232115b10SPawel Jakub Dawidek return (NULL);
169332115b10SPawel Jakub Dawidek }
169432115b10SPawel Jakub Dawidek
169532115b10SPawel Jakub Dawidek /*
169632115b10SPawel Jakub Dawidek * Thread receives answer from secondary node and passes it to ggate_send
169732115b10SPawel Jakub Dawidek * thread.
169832115b10SPawel Jakub Dawidek */
169932115b10SPawel Jakub Dawidek static void *
remote_recv_thread(void * arg)170032115b10SPawel Jakub Dawidek remote_recv_thread(void *arg)
170132115b10SPawel Jakub Dawidek {
170232115b10SPawel Jakub Dawidek struct hast_resource *res = arg;
170332115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio;
170432115b10SPawel Jakub Dawidek struct hio *hio;
170532115b10SPawel Jakub Dawidek struct nv *nv;
170632115b10SPawel Jakub Dawidek unsigned int ncomp;
170732115b10SPawel Jakub Dawidek uint64_t seq;
1708d6e636c9SPawel Jakub Dawidek bool memsyncack;
170932115b10SPawel Jakub Dawidek int error;
171032115b10SPawel Jakub Dawidek
171132115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */
171232115b10SPawel Jakub Dawidek ncomp = 1;
171332115b10SPawel Jakub Dawidek
171432115b10SPawel Jakub Dawidek for (;;) {
171532115b10SPawel Jakub Dawidek /* Wait until there is anything to receive. */
171632115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]);
171732115b10SPawel Jakub Dawidek while (TAILQ_EMPTY(&hio_recv_list[ncomp])) {
171832115b10SPawel Jakub Dawidek pjdlog_debug(2, "remote_recv: No requests, waiting.");
171932115b10SPawel Jakub Dawidek cv_wait(&hio_recv_list_cond[ncomp],
172032115b10SPawel Jakub Dawidek &hio_recv_list_lock[ncomp]);
172132115b10SPawel Jakub Dawidek }
172232115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]);
17233f5bce18SPawel Jakub Dawidek
1724d6e636c9SPawel Jakub Dawidek memsyncack = false;
1725d6e636c9SPawel Jakub Dawidek
172632115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]);
172732115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) {
172832115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
172932115b10SPawel Jakub Dawidek /*
173032115b10SPawel Jakub Dawidek * Connection is dead, so move all pending requests to
173132115b10SPawel Jakub Dawidek * the done queue (one-by-one).
173232115b10SPawel Jakub Dawidek */
173332115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]);
173432115b10SPawel Jakub Dawidek hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
17352ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(hio != NULL);
173632115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
173732115b10SPawel Jakub Dawidek hio_next[ncomp]);
17386b66c350SMikolaj Golub hio_recv_list_size[ncomp]--;
173932115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]);
17405d69ed53SMikolaj Golub hio->hio_errors[ncomp] = ENOTCONN;
174132115b10SPawel Jakub Dawidek goto done_queue;
174232115b10SPawel Jakub Dawidek }
17432b1b224dSPawel Jakub Dawidek if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
174432115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR,
174532115b10SPawel Jakub Dawidek "Unable to receive reply header");
174632115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
174732115b10SPawel Jakub Dawidek remote_close(res, ncomp);
174832115b10SPawel Jakub Dawidek continue;
174932115b10SPawel Jakub Dawidek }
175032115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
175132115b10SPawel Jakub Dawidek seq = nv_get_uint64(nv, "seq");
175232115b10SPawel Jakub Dawidek if (seq == 0) {
175332115b10SPawel Jakub Dawidek pjdlog_error("Header contains no 'seq' field.");
175432115b10SPawel Jakub Dawidek nv_free(nv);
175532115b10SPawel Jakub Dawidek continue;
175632115b10SPawel Jakub Dawidek }
1757d6e636c9SPawel Jakub Dawidek memsyncack = nv_exists(nv, "received");
175832115b10SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]);
175932115b10SPawel Jakub Dawidek TAILQ_FOREACH(hio, &hio_recv_list[ncomp], hio_next[ncomp]) {
176032115b10SPawel Jakub Dawidek if (hio->hio_ggio.gctl_seq == seq) {
176132115b10SPawel Jakub Dawidek TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
176232115b10SPawel Jakub Dawidek hio_next[ncomp]);
17636b66c350SMikolaj Golub hio_recv_list_size[ncomp]--;
176432115b10SPawel Jakub Dawidek break;
176532115b10SPawel Jakub Dawidek }
176632115b10SPawel Jakub Dawidek }
176732115b10SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]);
176832115b10SPawel Jakub Dawidek if (hio == NULL) {
176932115b10SPawel Jakub Dawidek pjdlog_error("Found no request matching received 'seq' field (%ju).",
177032115b10SPawel Jakub Dawidek (uintmax_t)seq);
177132115b10SPawel Jakub Dawidek nv_free(nv);
177232115b10SPawel Jakub Dawidek continue;
177332115b10SPawel Jakub Dawidek }
17741212a85cSPawel Jakub Dawidek ggio = &hio->hio_ggio;
177532115b10SPawel Jakub Dawidek error = nv_get_int16(nv, "error");
177632115b10SPawel Jakub Dawidek if (error != 0) {
177732115b10SPawel Jakub Dawidek /* Request failed on remote side. */
177872089204SPawel Jakub Dawidek hio->hio_errors[ncomp] = error;
17791212a85cSPawel Jakub Dawidek reqlog(LOG_WARNING, 0, ggio,
1780cd7b7ee5SPawel Jakub Dawidek "Remote request failed (%s): ", strerror(error));
178132115b10SPawel Jakub Dawidek nv_free(nv);
178232115b10SPawel Jakub Dawidek goto done_queue;
178332115b10SPawel Jakub Dawidek }
178432115b10SPawel Jakub Dawidek switch (ggio->gctl_cmd) {
178532115b10SPawel Jakub Dawidek case BIO_READ:
178632115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]);
178732115b10SPawel Jakub Dawidek if (!ISCONNECTED(res, ncomp)) {
178832115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
178932115b10SPawel Jakub Dawidek nv_free(nv);
179032115b10SPawel Jakub Dawidek goto done_queue;
179132115b10SPawel Jakub Dawidek }
179232115b10SPawel Jakub Dawidek if (hast_proto_recv_data(res, res->hr_remotein, nv,
17932b1b224dSPawel Jakub Dawidek ggio->gctl_data, ggio->gctl_length) == -1) {
179432115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = errno;
179532115b10SPawel Jakub Dawidek pjdlog_errno(LOG_ERR,
179632115b10SPawel Jakub Dawidek "Unable to receive reply data");
179732115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
179832115b10SPawel Jakub Dawidek nv_free(nv);
179932115b10SPawel Jakub Dawidek remote_close(res, ncomp);
180032115b10SPawel Jakub Dawidek goto done_queue;
180132115b10SPawel Jakub Dawidek }
180232115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
180332115b10SPawel Jakub Dawidek break;
180432115b10SPawel Jakub Dawidek case BIO_WRITE:
180532115b10SPawel Jakub Dawidek case BIO_DELETE:
180632115b10SPawel Jakub Dawidek case BIO_FLUSH:
180732115b10SPawel Jakub Dawidek break;
180832115b10SPawel Jakub Dawidek default:
180909c2e843SPawel Jakub Dawidek PJDLOG_ABORT("invalid condition");
181032115b10SPawel Jakub Dawidek }
181132115b10SPawel Jakub Dawidek hio->hio_errors[ncomp] = 0;
181232115b10SPawel Jakub Dawidek nv_free(nv);
181332115b10SPawel Jakub Dawidek done_queue:
18145d69ed53SMikolaj Golub if (ISMEMSYNCWRITE(hio)) {
18155d69ed53SMikolaj Golub if (!hio->hio_memsyncacked) {
18165d69ed53SMikolaj Golub PJDLOG_ASSERT(memsyncack ||
18175d69ed53SMikolaj Golub hio->hio_errors[ncomp] != 0);
18185d69ed53SMikolaj Golub /* Remote ack arrived. */
18195d69ed53SMikolaj Golub if (refcnt_release(&hio->hio_writecount) == 0) {
1820d6e636c9SPawel Jakub Dawidek if (hio->hio_errors[0] == 0)
1821d6e636c9SPawel Jakub Dawidek write_complete(res, hio);
18225d69ed53SMikolaj Golub }
18235d69ed53SMikolaj Golub hio->hio_memsyncacked = true;
18245d69ed53SMikolaj Golub if (hio->hio_errors[ncomp] == 0) {
1825d6e636c9SPawel Jakub Dawidek pjdlog_debug(2,
18265d69ed53SMikolaj Golub "remote_recv: (%p) Moving request "
18275d69ed53SMikolaj Golub "back to the recv queue.", hio);
1828d6e636c9SPawel Jakub Dawidek mtx_lock(&hio_recv_list_lock[ncomp]);
1829d6e636c9SPawel Jakub Dawidek TAILQ_INSERT_TAIL(&hio_recv_list[ncomp],
1830d6e636c9SPawel Jakub Dawidek hio, hio_next[ncomp]);
18316b66c350SMikolaj Golub hio_recv_list_size[ncomp]++;
1832d6e636c9SPawel Jakub Dawidek mtx_unlock(&hio_recv_list_lock[ncomp]);
18335d69ed53SMikolaj Golub continue;
18345d69ed53SMikolaj Golub }
1835d6e636c9SPawel Jakub Dawidek } else {
18365d69ed53SMikolaj Golub PJDLOG_ASSERT(!memsyncack);
18375d69ed53SMikolaj Golub /* Remote final reply arrived. */
1838d6e636c9SPawel Jakub Dawidek }
18395d69ed53SMikolaj Golub }
18405d69ed53SMikolaj Golub if (refcnt_release(&hio->hio_countdown) > 0)
1841d6e636c9SPawel Jakub Dawidek continue;
184232115b10SPawel Jakub Dawidek if (ISSYNCREQ(hio)) {
184332115b10SPawel Jakub Dawidek mtx_lock(&sync_lock);
184432115b10SPawel Jakub Dawidek SYNCREQDONE(hio);
184532115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock);
184632115b10SPawel Jakub Dawidek cv_signal(&sync_cond);
184732115b10SPawel Jakub Dawidek } else {
184832115b10SPawel Jakub Dawidek pjdlog_debug(2,
184932115b10SPawel Jakub Dawidek "remote_recv: (%p) Moving request to the done queue.",
185032115b10SPawel Jakub Dawidek hio);
185132115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, done);
185232115b10SPawel Jakub Dawidek }
185332115b10SPawel Jakub Dawidek }
185432115b10SPawel Jakub Dawidek /* NOTREACHED */
185532115b10SPawel Jakub Dawidek return (NULL);
185632115b10SPawel Jakub Dawidek }
185732115b10SPawel Jakub Dawidek
185832115b10SPawel Jakub Dawidek /*
185932115b10SPawel Jakub Dawidek * Thread sends answer to the kernel.
186032115b10SPawel Jakub Dawidek */
186132115b10SPawel Jakub Dawidek static void *
ggate_send_thread(void * arg)186232115b10SPawel Jakub Dawidek ggate_send_thread(void *arg)
186332115b10SPawel Jakub Dawidek {
186432115b10SPawel Jakub Dawidek struct hast_resource *res = arg;
186532115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio;
186632115b10SPawel Jakub Dawidek struct hio *hio;
186707ebc362SPawel Jakub Dawidek unsigned int ii, ncomps;
186832115b10SPawel Jakub Dawidek
186932115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS;
187032115b10SPawel Jakub Dawidek
187132115b10SPawel Jakub Dawidek for (;;) {
187232115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_send: Taking request.");
187332115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, done);
187432115b10SPawel Jakub Dawidek pjdlog_debug(2, "ggate_send: (%p) Got request.", hio);
187532115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio;
187632115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) {
187732115b10SPawel Jakub Dawidek if (hio->hio_errors[ii] == 0) {
187832115b10SPawel Jakub Dawidek /*
187932115b10SPawel Jakub Dawidek * One successful request is enough to declare
188032115b10SPawel Jakub Dawidek * success.
188132115b10SPawel Jakub Dawidek */
188232115b10SPawel Jakub Dawidek ggio->gctl_error = 0;
188332115b10SPawel Jakub Dawidek break;
188432115b10SPawel Jakub Dawidek }
188532115b10SPawel Jakub Dawidek }
188632115b10SPawel Jakub Dawidek if (ii == ncomps) {
188732115b10SPawel Jakub Dawidek /*
188832115b10SPawel Jakub Dawidek * None of the requests were successful.
1889b068d5aaSMikolaj Golub * Use the error from local component except the
1890b068d5aaSMikolaj Golub * case when we did only remote request.
189132115b10SPawel Jakub Dawidek */
1892b068d5aaSMikolaj Golub if (ggio->gctl_cmd == BIO_READ &&
1893b068d5aaSMikolaj Golub res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
1894b068d5aaSMikolaj Golub ggio->gctl_error = hio->hio_errors[1];
1895b068d5aaSMikolaj Golub else
189632115b10SPawel Jakub Dawidek ggio->gctl_error = hio->hio_errors[0];
189732115b10SPawel Jakub Dawidek }
189832115b10SPawel Jakub Dawidek if (ggio->gctl_error == 0 && ggio->gctl_cmd == BIO_WRITE) {
189932115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock);
1900d9f039e0SMikolaj Golub if (activemap_write_complete(res->hr_amp,
1901d9f039e0SMikolaj Golub ggio->gctl_offset, ggio->gctl_length)) {
1902d9f039e0SMikolaj Golub res->hr_stat_activemap_update++;
1903d9f039e0SMikolaj Golub (void)hast_activemap_flush(res);
1904a818a4ffSMikolaj Golub } else {
190532115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock);
190632115b10SPawel Jakub Dawidek }
1907a818a4ffSMikolaj Golub }
190832115b10SPawel Jakub Dawidek if (ggio->gctl_cmd == BIO_WRITE) {
190932115b10SPawel Jakub Dawidek /*
191032115b10SPawel Jakub Dawidek * Unlock range we locked.
191132115b10SPawel Jakub Dawidek */
191232115b10SPawel Jakub Dawidek mtx_lock(&range_lock);
191332115b10SPawel Jakub Dawidek rangelock_del(range_regular, ggio->gctl_offset,
191432115b10SPawel Jakub Dawidek ggio->gctl_length);
191532115b10SPawel Jakub Dawidek if (range_sync_wait)
191632115b10SPawel Jakub Dawidek cv_signal(&range_sync_cond);
191732115b10SPawel Jakub Dawidek mtx_unlock(&range_lock);
191807ebc362SPawel Jakub Dawidek if (!hio->hio_done)
191907ebc362SPawel Jakub Dawidek write_complete(res, hio);
192007ebc362SPawel Jakub Dawidek } else {
19212b1b224dSPawel Jakub Dawidek if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
192207ebc362SPawel Jakub Dawidek primary_exit(EX_OSERR,
192307ebc362SPawel Jakub Dawidek "G_GATE_CMD_DONE failed");
192432115b10SPawel Jakub Dawidek }
192532115b10SPawel Jakub Dawidek }
19262adbba66SMikolaj Golub if (hio->hio_errors[0]) {
19272adbba66SMikolaj Golub switch (ggio->gctl_cmd) {
19282adbba66SMikolaj Golub case BIO_READ:
19292adbba66SMikolaj Golub res->hr_stat_read_error++;
19302adbba66SMikolaj Golub break;
19312adbba66SMikolaj Golub case BIO_WRITE:
19322adbba66SMikolaj Golub res->hr_stat_write_error++;
19332adbba66SMikolaj Golub break;
19342adbba66SMikolaj Golub case BIO_DELETE:
19352adbba66SMikolaj Golub res->hr_stat_delete_error++;
19362adbba66SMikolaj Golub break;
19372adbba66SMikolaj Golub case BIO_FLUSH:
19382adbba66SMikolaj Golub res->hr_stat_flush_error++;
19392adbba66SMikolaj Golub break;
19402adbba66SMikolaj Golub }
19412adbba66SMikolaj Golub }
194232115b10SPawel Jakub Dawidek pjdlog_debug(2,
194332115b10SPawel Jakub Dawidek "ggate_send: (%p) Moving request to the free queue.", hio);
194432115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, free);
194532115b10SPawel Jakub Dawidek }
194632115b10SPawel Jakub Dawidek /* NOTREACHED */
194732115b10SPawel Jakub Dawidek return (NULL);
194832115b10SPawel Jakub Dawidek }
194932115b10SPawel Jakub Dawidek
195032115b10SPawel Jakub Dawidek /*
195132115b10SPawel Jakub Dawidek * Thread synchronize local and remote components.
195232115b10SPawel Jakub Dawidek */
195332115b10SPawel Jakub Dawidek static void *
sync_thread(void * arg __unused)195432115b10SPawel Jakub Dawidek sync_thread(void *arg __unused)
195532115b10SPawel Jakub Dawidek {
195632115b10SPawel Jakub Dawidek struct hast_resource *res = arg;
195732115b10SPawel Jakub Dawidek struct hio *hio;
195832115b10SPawel Jakub Dawidek struct g_gate_ctl_io *ggio;
1959fa356f6cSPawel Jakub Dawidek struct timeval tstart, tend, tdiff;
196032115b10SPawel Jakub Dawidek unsigned int ii, ncomp, ncomps;
196132115b10SPawel Jakub Dawidek off_t offset, length, synced;
1962c66ee1b3SPawel Jakub Dawidek bool dorewind, directreads;
196332115b10SPawel Jakub Dawidek int syncext;
196432115b10SPawel Jakub Dawidek
196532115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS;
196632115b10SPawel Jakub Dawidek dorewind = true;
1967b9cf0cf5SPawel Jakub Dawidek synced = 0;
1968b9cf0cf5SPawel Jakub Dawidek offset = -1;
1969c66ee1b3SPawel Jakub Dawidek directreads = false;
197032115b10SPawel Jakub Dawidek
197132115b10SPawel Jakub Dawidek for (;;) {
197232115b10SPawel Jakub Dawidek mtx_lock(&sync_lock);
1973b9cf0cf5SPawel Jakub Dawidek if (offset >= 0 && !sync_inprogress) {
1974fa356f6cSPawel Jakub Dawidek gettimeofday(&tend, NULL);
1975fa356f6cSPawel Jakub Dawidek timersub(&tend, &tstart, &tdiff);
1976fa356f6cSPawel Jakub Dawidek pjdlog_info("Synchronization interrupted after %#.0T. "
1977fa356f6cSPawel Jakub Dawidek "%NB synchronized so far.", &tdiff,
197853d9b386SPawel Jakub Dawidek (intmax_t)synced);
19795bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCINTR);
198053d9b386SPawel Jakub Dawidek }
198132115b10SPawel Jakub Dawidek while (!sync_inprogress) {
198232115b10SPawel Jakub Dawidek dorewind = true;
198332115b10SPawel Jakub Dawidek synced = 0;
198432115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock);
198532115b10SPawel Jakub Dawidek }
198632115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock);
198732115b10SPawel Jakub Dawidek /*
198832115b10SPawel Jakub Dawidek * Obtain offset at which we should synchronize.
198932115b10SPawel Jakub Dawidek * Rewind synchronization if needed.
199032115b10SPawel Jakub Dawidek */
199132115b10SPawel Jakub Dawidek mtx_lock(&res->hr_amp_lock);
199232115b10SPawel Jakub Dawidek if (dorewind)
199332115b10SPawel Jakub Dawidek activemap_sync_rewind(res->hr_amp);
199432115b10SPawel Jakub Dawidek offset = activemap_sync_offset(res->hr_amp, &length, &syncext);
199532115b10SPawel Jakub Dawidek if (syncext != -1) {
199632115b10SPawel Jakub Dawidek /*
199732115b10SPawel Jakub Dawidek * We synchronized entire syncext extent, we can mark
199832115b10SPawel Jakub Dawidek * it as clean now.
199932115b10SPawel Jakub Dawidek */
200032115b10SPawel Jakub Dawidek if (activemap_extent_complete(res->hr_amp, syncext))
200132115b10SPawel Jakub Dawidek (void)hast_activemap_flush(res);
2002a818a4ffSMikolaj Golub else
200332115b10SPawel Jakub Dawidek mtx_unlock(&res->hr_amp_lock);
2004a818a4ffSMikolaj Golub } else {
2005a818a4ffSMikolaj Golub mtx_unlock(&res->hr_amp_lock);
2006a818a4ffSMikolaj Golub }
200732115b10SPawel Jakub Dawidek if (dorewind) {
200832115b10SPawel Jakub Dawidek dorewind = false;
20092b1b224dSPawel Jakub Dawidek if (offset == -1)
201032115b10SPawel Jakub Dawidek pjdlog_info("Nodes are in sync.");
201132115b10SPawel Jakub Dawidek else {
2012fa356f6cSPawel Jakub Dawidek pjdlog_info("Synchronization started. %NB to go.",
2013fa356f6cSPawel Jakub Dawidek (intmax_t)(res->hr_extentsize *
201432115b10SPawel Jakub Dawidek activemap_ndirty(res->hr_amp)));
20155bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCSTART);
2016fa356f6cSPawel Jakub Dawidek gettimeofday(&tstart, NULL);
201732115b10SPawel Jakub Dawidek }
201832115b10SPawel Jakub Dawidek }
20192b1b224dSPawel Jakub Dawidek if (offset == -1) {
202055ce1e7cSPawel Jakub Dawidek sync_stop();
202132115b10SPawel Jakub Dawidek pjdlog_debug(1, "Nothing to synchronize.");
202232115b10SPawel Jakub Dawidek /*
202332115b10SPawel Jakub Dawidek * Synchronization complete, make both localcnt and
202432115b10SPawel Jakub Dawidek * remotecnt equal.
202532115b10SPawel Jakub Dawidek */
202632115b10SPawel Jakub Dawidek ncomp = 1;
202732115b10SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]);
202832115b10SPawel Jakub Dawidek if (ISCONNECTED(res, ncomp)) {
202932115b10SPawel Jakub Dawidek if (synced > 0) {
2030fa356f6cSPawel Jakub Dawidek int64_t bps;
2031fa356f6cSPawel Jakub Dawidek
2032fa356f6cSPawel Jakub Dawidek gettimeofday(&tend, NULL);
2033fa356f6cSPawel Jakub Dawidek timersub(&tend, &tstart, &tdiff);
2034fa356f6cSPawel Jakub Dawidek bps = (int64_t)((double)synced /
2035fa356f6cSPawel Jakub Dawidek ((double)tdiff.tv_sec +
2036fa356f6cSPawel Jakub Dawidek (double)tdiff.tv_usec / 1000000));
203732115b10SPawel Jakub Dawidek pjdlog_info("Synchronization complete. "
2038fa356f6cSPawel Jakub Dawidek "%NB synchronized in %#.0lT (%NB/sec).",
2039fa356f6cSPawel Jakub Dawidek (intmax_t)synced, &tdiff,
2040fa356f6cSPawel Jakub Dawidek (intmax_t)bps);
20415bdff860SPawel Jakub Dawidek event_send(res, EVENT_SYNCDONE);
204232115b10SPawel Jakub Dawidek }
204332115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock);
2044c66ee1b3SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY)
2045c66ee1b3SPawel Jakub Dawidek directreads = true;
204632115b10SPawel Jakub Dawidek res->hr_syncsrc = HAST_SYNCSRC_UNDEF;
204732115b10SPawel Jakub Dawidek res->hr_primary_localcnt =
204832115b10SPawel Jakub Dawidek res->hr_secondary_remotecnt;
20499237aa3fSMikolaj Golub res->hr_primary_remotecnt =
20509237aa3fSMikolaj Golub res->hr_secondary_localcnt;
205132115b10SPawel Jakub Dawidek pjdlog_debug(1,
205232115b10SPawel Jakub Dawidek "Setting localcnt to %ju and remotecnt to %ju.",
205332115b10SPawel Jakub Dawidek (uintmax_t)res->hr_primary_localcnt,
20549237aa3fSMikolaj Golub (uintmax_t)res->hr_primary_remotecnt);
205532115b10SPawel Jakub Dawidek (void)metadata_write(res);
205632115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock);
205732115b10SPawel Jakub Dawidek }
205832115b10SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
2059c66ee1b3SPawel Jakub Dawidek if (directreads) {
2060c66ee1b3SPawel Jakub Dawidek directreads = false;
2061c66ee1b3SPawel Jakub Dawidek enable_direct_reads(res);
2062c66ee1b3SPawel Jakub Dawidek }
206332115b10SPawel Jakub Dawidek continue;
206432115b10SPawel Jakub Dawidek }
206532115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: Taking free request.");
206632115b10SPawel Jakub Dawidek QUEUE_TAKE2(hio, free);
206732115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Got free request.", hio);
206832115b10SPawel Jakub Dawidek /*
206932115b10SPawel Jakub Dawidek * Lock the range we are going to synchronize. We don't want
207032115b10SPawel Jakub Dawidek * race where someone writes between our read and write.
207132115b10SPawel Jakub Dawidek */
207232115b10SPawel Jakub Dawidek for (;;) {
207332115b10SPawel Jakub Dawidek mtx_lock(&range_lock);
207432115b10SPawel Jakub Dawidek if (rangelock_islocked(range_regular, offset, length)) {
207532115b10SPawel Jakub Dawidek pjdlog_debug(2,
207632115b10SPawel Jakub Dawidek "sync: Range offset=%jd length=%jd locked.",
207732115b10SPawel Jakub Dawidek (intmax_t)offset, (intmax_t)length);
207832115b10SPawel Jakub Dawidek range_sync_wait = true;
207932115b10SPawel Jakub Dawidek cv_wait(&range_sync_cond, &range_lock);
208032115b10SPawel Jakub Dawidek range_sync_wait = false;
208132115b10SPawel Jakub Dawidek mtx_unlock(&range_lock);
208232115b10SPawel Jakub Dawidek continue;
208332115b10SPawel Jakub Dawidek }
20842b1b224dSPawel Jakub Dawidek if (rangelock_add(range_sync, offset, length) == -1) {
208532115b10SPawel Jakub Dawidek mtx_unlock(&range_lock);
208632115b10SPawel Jakub Dawidek pjdlog_debug(2,
208732115b10SPawel Jakub Dawidek "sync: Range offset=%jd length=%jd is already locked, waiting.",
208832115b10SPawel Jakub Dawidek (intmax_t)offset, (intmax_t)length);
208932115b10SPawel Jakub Dawidek sleep(1);
209032115b10SPawel Jakub Dawidek continue;
209132115b10SPawel Jakub Dawidek }
209232115b10SPawel Jakub Dawidek mtx_unlock(&range_lock);
209332115b10SPawel Jakub Dawidek break;
209432115b10SPawel Jakub Dawidek }
209532115b10SPawel Jakub Dawidek /*
209632115b10SPawel Jakub Dawidek * First read the data from synchronization source.
209732115b10SPawel Jakub Dawidek */
209832115b10SPawel Jakub Dawidek SYNCREQ(hio);
209932115b10SPawel Jakub Dawidek ggio = &hio->hio_ggio;
210032115b10SPawel Jakub Dawidek ggio->gctl_cmd = BIO_READ;
210132115b10SPawel Jakub Dawidek ggio->gctl_offset = offset;
210232115b10SPawel Jakub Dawidek ggio->gctl_length = length;
210332115b10SPawel Jakub Dawidek ggio->gctl_error = 0;
210407ebc362SPawel Jakub Dawidek hio->hio_done = false;
210507ebc362SPawel Jakub Dawidek hio->hio_replication = res->hr_replication;
210632115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++)
210732115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL;
210832115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
210932115b10SPawel Jakub Dawidek hio);
211032115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
211132115b10SPawel Jakub Dawidek hio);
211232115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock);
211332115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
211432115b10SPawel Jakub Dawidek /*
211532115b10SPawel Jakub Dawidek * This range is up-to-date on local component,
211632115b10SPawel Jakub Dawidek * so handle request locally.
211732115b10SPawel Jakub Dawidek */
211832115b10SPawel Jakub Dawidek /* Local component is 0 for now. */
211932115b10SPawel Jakub Dawidek ncomp = 0;
212032115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
21212ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
212232115b10SPawel Jakub Dawidek /*
212332115b10SPawel Jakub Dawidek * This range is out-of-date on local component,
212432115b10SPawel Jakub Dawidek * so send request to the remote node.
212532115b10SPawel Jakub Dawidek */
212632115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */
212732115b10SPawel Jakub Dawidek ncomp = 1;
212832115b10SPawel Jakub Dawidek }
212932115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock);
21306e87c151SEd Schouten refcnt_init(&hio->hio_countdown, 1);
213132115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp);
213232115b10SPawel Jakub Dawidek
213332115b10SPawel Jakub Dawidek /*
213432115b10SPawel Jakub Dawidek * Let's wait for READ to finish.
213532115b10SPawel Jakub Dawidek */
213632115b10SPawel Jakub Dawidek mtx_lock(&sync_lock);
213732115b10SPawel Jakub Dawidek while (!ISSYNCREQDONE(hio))
213832115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock);
213932115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock);
214032115b10SPawel Jakub Dawidek
214132115b10SPawel Jakub Dawidek if (hio->hio_errors[ncomp] != 0) {
214232115b10SPawel Jakub Dawidek pjdlog_error("Unable to read synchronization data: %s.",
214332115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp]));
214432115b10SPawel Jakub Dawidek goto free_queue;
214532115b10SPawel Jakub Dawidek }
214632115b10SPawel Jakub Dawidek
214732115b10SPawel Jakub Dawidek /*
214832115b10SPawel Jakub Dawidek * We read the data from synchronization source, now write it
214932115b10SPawel Jakub Dawidek * to synchronization target.
215032115b10SPawel Jakub Dawidek */
215132115b10SPawel Jakub Dawidek SYNCREQ(hio);
215232115b10SPawel Jakub Dawidek ggio->gctl_cmd = BIO_WRITE;
215332115b10SPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++)
215432115b10SPawel Jakub Dawidek hio->hio_errors[ii] = EINVAL;
215532115b10SPawel Jakub Dawidek reqlog(LOG_DEBUG, 2, ggio, "sync: (%p) Sending sync request: ",
215632115b10SPawel Jakub Dawidek hio);
215732115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
215832115b10SPawel Jakub Dawidek hio);
215932115b10SPawel Jakub Dawidek mtx_lock(&metadata_lock);
216032115b10SPawel Jakub Dawidek if (res->hr_syncsrc == HAST_SYNCSRC_PRIMARY) {
216132115b10SPawel Jakub Dawidek /*
216232115b10SPawel Jakub Dawidek * This range is up-to-date on local component,
216332115b10SPawel Jakub Dawidek * so we update remote component.
216432115b10SPawel Jakub Dawidek */
216532115b10SPawel Jakub Dawidek /* Remote component is 1 for now. */
216632115b10SPawel Jakub Dawidek ncomp = 1;
216732115b10SPawel Jakub Dawidek } else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
21682ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
216932115b10SPawel Jakub Dawidek /*
217032115b10SPawel Jakub Dawidek * This range is out-of-date on local component,
217132115b10SPawel Jakub Dawidek * so we update it.
217232115b10SPawel Jakub Dawidek */
217332115b10SPawel Jakub Dawidek /* Local component is 0 for now. */
217432115b10SPawel Jakub Dawidek ncomp = 0;
217532115b10SPawel Jakub Dawidek }
217632115b10SPawel Jakub Dawidek mtx_unlock(&metadata_lock);
217732115b10SPawel Jakub Dawidek
21783f5bce18SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the send queue.",
217932115b10SPawel Jakub Dawidek hio);
21806e87c151SEd Schouten refcnt_init(&hio->hio_countdown, 1);
218132115b10SPawel Jakub Dawidek QUEUE_INSERT1(hio, send, ncomp);
218232115b10SPawel Jakub Dawidek
218332115b10SPawel Jakub Dawidek /*
218432115b10SPawel Jakub Dawidek * Let's wait for WRITE to finish.
218532115b10SPawel Jakub Dawidek */
218632115b10SPawel Jakub Dawidek mtx_lock(&sync_lock);
218732115b10SPawel Jakub Dawidek while (!ISSYNCREQDONE(hio))
218832115b10SPawel Jakub Dawidek cv_wait(&sync_cond, &sync_lock);
218932115b10SPawel Jakub Dawidek mtx_unlock(&sync_lock);
219032115b10SPawel Jakub Dawidek
219132115b10SPawel Jakub Dawidek if (hio->hio_errors[ncomp] != 0) {
219232115b10SPawel Jakub Dawidek pjdlog_error("Unable to write synchronization data: %s.",
219332115b10SPawel Jakub Dawidek strerror(hio->hio_errors[ncomp]));
219432115b10SPawel Jakub Dawidek goto free_queue;
219532115b10SPawel Jakub Dawidek }
2196e23d2d01SPawel Jakub Dawidek
2197e23d2d01SPawel Jakub Dawidek synced += length;
219832115b10SPawel Jakub Dawidek free_queue:
219932115b10SPawel Jakub Dawidek mtx_lock(&range_lock);
220032115b10SPawel Jakub Dawidek rangelock_del(range_sync, offset, length);
220132115b10SPawel Jakub Dawidek if (range_regular_wait)
220232115b10SPawel Jakub Dawidek cv_signal(&range_regular_cond);
220332115b10SPawel Jakub Dawidek mtx_unlock(&range_lock);
220432115b10SPawel Jakub Dawidek pjdlog_debug(2, "sync: (%p) Moving request to the free queue.",
220532115b10SPawel Jakub Dawidek hio);
220632115b10SPawel Jakub Dawidek QUEUE_INSERT2(hio, free);
220732115b10SPawel Jakub Dawidek }
220832115b10SPawel Jakub Dawidek /* NOTREACHED */
220932115b10SPawel Jakub Dawidek return (NULL);
221032115b10SPawel Jakub Dawidek }
221132115b10SPawel Jakub Dawidek
2212115f4e5cSPawel Jakub Dawidek void
primary_config_reload(struct hast_resource * res,struct nv * nv)2213115f4e5cSPawel Jakub Dawidek primary_config_reload(struct hast_resource *res, struct nv *nv)
22140989854dSPawel Jakub Dawidek {
22150989854dSPawel Jakub Dawidek unsigned int ii, ncomps;
2216115f4e5cSPawel Jakub Dawidek int modified, vint;
2217115f4e5cSPawel Jakub Dawidek const char *vstr;
22180989854dSPawel Jakub Dawidek
22190989854dSPawel Jakub Dawidek pjdlog_info("Reloading configuration...");
22200989854dSPawel Jakub Dawidek
22212ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
22222ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(gres == res);
2223115f4e5cSPawel Jakub Dawidek nv_assert(nv, "remoteaddr");
22240b626a28SPawel Jakub Dawidek nv_assert(nv, "sourceaddr");
2225115f4e5cSPawel Jakub Dawidek nv_assert(nv, "replication");
22261fee97b0SPawel Jakub Dawidek nv_assert(nv, "checksum");
22278cd3d45aSPawel Jakub Dawidek nv_assert(nv, "compression");
2228115f4e5cSPawel Jakub Dawidek nv_assert(nv, "timeout");
2229115f4e5cSPawel Jakub Dawidek nv_assert(nv, "exec");
2230518dd4c0SPawel Jakub Dawidek nv_assert(nv, "metaflush");
2231115f4e5cSPawel Jakub Dawidek
22320989854dSPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS;
22330989854dSPawel Jakub Dawidek
22341fee97b0SPawel Jakub Dawidek #define MODIFIED_REMOTEADDR 0x01
22350b626a28SPawel Jakub Dawidek #define MODIFIED_SOURCEADDR 0x02
22360b626a28SPawel Jakub Dawidek #define MODIFIED_REPLICATION 0x04
22370b626a28SPawel Jakub Dawidek #define MODIFIED_CHECKSUM 0x08
22380b626a28SPawel Jakub Dawidek #define MODIFIED_COMPRESSION 0x10
22390b626a28SPawel Jakub Dawidek #define MODIFIED_TIMEOUT 0x20
22400b626a28SPawel Jakub Dawidek #define MODIFIED_EXEC 0x40
2241518dd4c0SPawel Jakub Dawidek #define MODIFIED_METAFLUSH 0x80
22420989854dSPawel Jakub Dawidek modified = 0;
2243115f4e5cSPawel Jakub Dawidek
2244115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "remoteaddr");
2245115f4e5cSPawel Jakub Dawidek if (strcmp(gres->hr_remoteaddr, vstr) != 0) {
22460989854dSPawel Jakub Dawidek /*
22470989854dSPawel Jakub Dawidek * Don't copy res->hr_remoteaddr to gres just yet.
22480989854dSPawel Jakub Dawidek * We want remote_close() to log disconnect from the old
22490989854dSPawel Jakub Dawidek * addresses, not from the new ones.
22500989854dSPawel Jakub Dawidek */
22510989854dSPawel Jakub Dawidek modified |= MODIFIED_REMOTEADDR;
22520989854dSPawel Jakub Dawidek }
22530b626a28SPawel Jakub Dawidek vstr = nv_get_string(nv, "sourceaddr");
22540b626a28SPawel Jakub Dawidek if (strcmp(gres->hr_sourceaddr, vstr) != 0) {
22550b626a28SPawel Jakub Dawidek strlcpy(gres->hr_sourceaddr, vstr, sizeof(gres->hr_sourceaddr));
22560b626a28SPawel Jakub Dawidek modified |= MODIFIED_SOURCEADDR;
22570b626a28SPawel Jakub Dawidek }
2258115f4e5cSPawel Jakub Dawidek vint = nv_get_int32(nv, "replication");
2259115f4e5cSPawel Jakub Dawidek if (gres->hr_replication != vint) {
2260115f4e5cSPawel Jakub Dawidek gres->hr_replication = vint;
22610989854dSPawel Jakub Dawidek modified |= MODIFIED_REPLICATION;
22620989854dSPawel Jakub Dawidek }
22631fee97b0SPawel Jakub Dawidek vint = nv_get_int32(nv, "checksum");
22641fee97b0SPawel Jakub Dawidek if (gres->hr_checksum != vint) {
22651fee97b0SPawel Jakub Dawidek gres->hr_checksum = vint;
22661fee97b0SPawel Jakub Dawidek modified |= MODIFIED_CHECKSUM;
22671fee97b0SPawel Jakub Dawidek }
22688cd3d45aSPawel Jakub Dawidek vint = nv_get_int32(nv, "compression");
22698cd3d45aSPawel Jakub Dawidek if (gres->hr_compression != vint) {
22708cd3d45aSPawel Jakub Dawidek gres->hr_compression = vint;
22718cd3d45aSPawel Jakub Dawidek modified |= MODIFIED_COMPRESSION;
22728cd3d45aSPawel Jakub Dawidek }
2273115f4e5cSPawel Jakub Dawidek vint = nv_get_int32(nv, "timeout");
2274115f4e5cSPawel Jakub Dawidek if (gres->hr_timeout != vint) {
2275115f4e5cSPawel Jakub Dawidek gres->hr_timeout = vint;
22760989854dSPawel Jakub Dawidek modified |= MODIFIED_TIMEOUT;
22770989854dSPawel Jakub Dawidek }
2278115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "exec");
2279115f4e5cSPawel Jakub Dawidek if (strcmp(gres->hr_exec, vstr) != 0) {
2280115f4e5cSPawel Jakub Dawidek strlcpy(gres->hr_exec, vstr, sizeof(gres->hr_exec));
22810becad39SPawel Jakub Dawidek modified |= MODIFIED_EXEC;
22820becad39SPawel Jakub Dawidek }
2283518dd4c0SPawel Jakub Dawidek vint = nv_get_int32(nv, "metaflush");
2284518dd4c0SPawel Jakub Dawidek if (gres->hr_metaflush != vint) {
2285518dd4c0SPawel Jakub Dawidek gres->hr_metaflush = vint;
2286518dd4c0SPawel Jakub Dawidek modified |= MODIFIED_METAFLUSH;
2287518dd4c0SPawel Jakub Dawidek }
2288115f4e5cSPawel Jakub Dawidek
22890989854dSPawel Jakub Dawidek /*
22901fee97b0SPawel Jakub Dawidek * Change timeout for connected sockets.
22911fee97b0SPawel Jakub Dawidek * Don't bother if we need to reconnect.
22920989854dSPawel Jakub Dawidek */
22931fee97b0SPawel Jakub Dawidek if ((modified & MODIFIED_TIMEOUT) != 0 &&
229407ebc362SPawel Jakub Dawidek (modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) == 0) {
22950989854dSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) {
22960989854dSPawel Jakub Dawidek if (!ISREMOTE(ii))
22970989854dSPawel Jakub Dawidek continue;
22980989854dSPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ii]);
22990989854dSPawel Jakub Dawidek if (!ISCONNECTED(gres, ii)) {
23000989854dSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ii]);
23010989854dSPawel Jakub Dawidek continue;
23020989854dSPawel Jakub Dawidek }
23030989854dSPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ii]);
23040989854dSPawel Jakub Dawidek if (proto_timeout(gres->hr_remotein,
23052b1b224dSPawel Jakub Dawidek gres->hr_timeout) == -1) {
23060989854dSPawel Jakub Dawidek pjdlog_errno(LOG_WARNING,
23070989854dSPawel Jakub Dawidek "Unable to set connection timeout");
23080989854dSPawel Jakub Dawidek }
23090989854dSPawel Jakub Dawidek if (proto_timeout(gres->hr_remoteout,
23102b1b224dSPawel Jakub Dawidek gres->hr_timeout) == -1) {
23110989854dSPawel Jakub Dawidek pjdlog_errno(LOG_WARNING,
23120989854dSPawel Jakub Dawidek "Unable to set connection timeout");
23130989854dSPawel Jakub Dawidek }
23140989854dSPawel Jakub Dawidek }
23151fee97b0SPawel Jakub Dawidek }
231607ebc362SPawel Jakub Dawidek if ((modified & (MODIFIED_REMOTEADDR | MODIFIED_SOURCEADDR)) != 0) {
23170989854dSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++) {
23180989854dSPawel Jakub Dawidek if (!ISREMOTE(ii))
23190989854dSPawel Jakub Dawidek continue;
23200989854dSPawel Jakub Dawidek remote_close(gres, ii);
23210989854dSPawel Jakub Dawidek }
23220989854dSPawel Jakub Dawidek if (modified & MODIFIED_REMOTEADDR) {
2323115f4e5cSPawel Jakub Dawidek vstr = nv_get_string(nv, "remoteaddr");
2324115f4e5cSPawel Jakub Dawidek strlcpy(gres->hr_remoteaddr, vstr,
23250989854dSPawel Jakub Dawidek sizeof(gres->hr_remoteaddr));
23260989854dSPawel Jakub Dawidek }
23270989854dSPawel Jakub Dawidek }
23280989854dSPawel Jakub Dawidek #undef MODIFIED_REMOTEADDR
23290b626a28SPawel Jakub Dawidek #undef MODIFIED_SOURCEADDR
23300989854dSPawel Jakub Dawidek #undef MODIFIED_REPLICATION
23311fee97b0SPawel Jakub Dawidek #undef MODIFIED_CHECKSUM
23328cd3d45aSPawel Jakub Dawidek #undef MODIFIED_COMPRESSION
23330989854dSPawel Jakub Dawidek #undef MODIFIED_TIMEOUT
23340becad39SPawel Jakub Dawidek #undef MODIFIED_EXEC
2335518dd4c0SPawel Jakub Dawidek #undef MODIFIED_METAFLUSH
23360989854dSPawel Jakub Dawidek
23370989854dSPawel Jakub Dawidek pjdlog_info("Configuration reloaded successfully.");
23380989854dSPawel Jakub Dawidek }
23390989854dSPawel Jakub Dawidek
2340f7fe83f9SPawel Jakub Dawidek static void
guard_one(struct hast_resource * res,unsigned int ncomp)2341ff6bb1f8SPawel Jakub Dawidek guard_one(struct hast_resource *res, unsigned int ncomp)
2342ff6bb1f8SPawel Jakub Dawidek {
2343ff6bb1f8SPawel Jakub Dawidek struct proto_conn *in, *out;
2344ff6bb1f8SPawel Jakub Dawidek
2345ff6bb1f8SPawel Jakub Dawidek if (!ISREMOTE(ncomp))
2346ff6bb1f8SPawel Jakub Dawidek return;
2347ff6bb1f8SPawel Jakub Dawidek
2348ff6bb1f8SPawel Jakub Dawidek rw_rlock(&hio_remote_lock[ncomp]);
2349ff6bb1f8SPawel Jakub Dawidek
2350ff6bb1f8SPawel Jakub Dawidek if (!real_remote(res)) {
2351ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
2352ff6bb1f8SPawel Jakub Dawidek return;
2353ff6bb1f8SPawel Jakub Dawidek }
2354ff6bb1f8SPawel Jakub Dawidek
2355ff6bb1f8SPawel Jakub Dawidek if (ISCONNECTED(res, ncomp)) {
23562ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein != NULL);
23572ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout != NULL);
2358ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
2359ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
2360ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr);
2361ff6bb1f8SPawel Jakub Dawidek return;
2362ff6bb1f8SPawel Jakub Dawidek }
2363ff6bb1f8SPawel Jakub Dawidek
23642ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL);
23652ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL);
2366ff6bb1f8SPawel Jakub Dawidek /*
2367ff6bb1f8SPawel Jakub Dawidek * Upgrade the lock. It doesn't have to be atomic as no other thread
2368ff6bb1f8SPawel Jakub Dawidek * can change connection status from disconnected to connected.
2369ff6bb1f8SPawel Jakub Dawidek */
2370ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
2371ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Reconnecting to %s.",
2372ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr);
2373ff6bb1f8SPawel Jakub Dawidek in = out = NULL;
2374ac0401e3SPawel Jakub Dawidek if (init_remote(res, &in, &out) == 0) {
2375ff6bb1f8SPawel Jakub Dawidek rw_wlock(&hio_remote_lock[ncomp]);
23762ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL);
23772ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL);
23782ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(in != NULL && out != NULL);
2379ff6bb1f8SPawel Jakub Dawidek res->hr_remotein = in;
2380ff6bb1f8SPawel Jakub Dawidek res->hr_remoteout = out;
2381ff6bb1f8SPawel Jakub Dawidek rw_unlock(&hio_remote_lock[ncomp]);
2382ff6bb1f8SPawel Jakub Dawidek pjdlog_info("Successfully reconnected to %s.",
2383ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr);
2384ff6bb1f8SPawel Jakub Dawidek sync_start();
2385ff6bb1f8SPawel Jakub Dawidek } else {
2386ff6bb1f8SPawel Jakub Dawidek /* Both connections should be NULL. */
23872ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remotein == NULL);
23882ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(res->hr_remoteout == NULL);
23892ec483c5SPawel Jakub Dawidek PJDLOG_ASSERT(in == NULL && out == NULL);
2390ff6bb1f8SPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
2391ff6bb1f8SPawel Jakub Dawidek res->hr_remoteaddr);
2392ff6bb1f8SPawel Jakub Dawidek }
2393ff6bb1f8SPawel Jakub Dawidek }
2394ff6bb1f8SPawel Jakub Dawidek
239532115b10SPawel Jakub Dawidek /*
239632115b10SPawel Jakub Dawidek * Thread guards remote connections and reconnects when needed, handles
239732115b10SPawel Jakub Dawidek * signals, etc.
239832115b10SPawel Jakub Dawidek */
239932115b10SPawel Jakub Dawidek static void *
guard_thread(void * arg)240032115b10SPawel Jakub Dawidek guard_thread(void *arg)
240132115b10SPawel Jakub Dawidek {
240232115b10SPawel Jakub Dawidek struct hast_resource *res = arg;
240332115b10SPawel Jakub Dawidek unsigned int ii, ncomps;
24046d0c801eSPawel Jakub Dawidek struct timespec timeout;
2405ff6bb1f8SPawel Jakub Dawidek time_t lastcheck, now;
24066d0c801eSPawel Jakub Dawidek sigset_t mask;
24076d0c801eSPawel Jakub Dawidek int signo;
240832115b10SPawel Jakub Dawidek
240932115b10SPawel Jakub Dawidek ncomps = HAST_NCOMPONENTS;
2410ff6bb1f8SPawel Jakub Dawidek lastcheck = time(NULL);
241132115b10SPawel Jakub Dawidek
24126d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigemptyset(&mask) == 0);
24136d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
24146d0c801eSPawel Jakub Dawidek PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
24156d0c801eSPawel Jakub Dawidek
24168d7dcf14SMikolaj Golub timeout.tv_sec = HAST_KEEPALIVE;
24176d0c801eSPawel Jakub Dawidek timeout.tv_nsec = 0;
24186d0c801eSPawel Jakub Dawidek signo = -1;
24196d0c801eSPawel Jakub Dawidek
242032115b10SPawel Jakub Dawidek for (;;) {
24216d0c801eSPawel Jakub Dawidek switch (signo) {
24226d0c801eSPawel Jakub Dawidek case SIGINT:
24236d0c801eSPawel Jakub Dawidek case SIGTERM:
24246d0c801eSPawel Jakub Dawidek sigexit_received = true;
242532115b10SPawel Jakub Dawidek primary_exitx(EX_OK,
242632115b10SPawel Jakub Dawidek "Termination signal received, exiting.");
24276d0c801eSPawel Jakub Dawidek break;
24286d0c801eSPawel Jakub Dawidek default:
2429ff6bb1f8SPawel Jakub Dawidek break;
2430f7fe83f9SPawel Jakub Dawidek }
24316d0c801eSPawel Jakub Dawidek
2432ac0401e3SPawel Jakub Dawidek /*
2433ac0401e3SPawel Jakub Dawidek * Don't check connections until we fully started,
2434ac0401e3SPawel Jakub Dawidek * as we may still be looping, waiting for remote node
2435ac0401e3SPawel Jakub Dawidek * to switch from primary to secondary.
2436ac0401e3SPawel Jakub Dawidek */
2437ac0401e3SPawel Jakub Dawidek if (fullystarted) {
24386d0c801eSPawel Jakub Dawidek pjdlog_debug(2, "remote_guard: Checking connections.");
2439ff6bb1f8SPawel Jakub Dawidek now = time(NULL);
24408d7dcf14SMikolaj Golub if (lastcheck + HAST_KEEPALIVE <= now) {
24416d0c801eSPawel Jakub Dawidek for (ii = 0; ii < ncomps; ii++)
2442ff6bb1f8SPawel Jakub Dawidek guard_one(res, ii);
2443ff6bb1f8SPawel Jakub Dawidek lastcheck = now;
244432115b10SPawel Jakub Dawidek }
2445ac0401e3SPawel Jakub Dawidek }
24466d0c801eSPawel Jakub Dawidek signo = sigtimedwait(&mask, NULL, &timeout);
244732115b10SPawel Jakub Dawidek }
244832115b10SPawel Jakub Dawidek /* NOTREACHED */
244932115b10SPawel Jakub Dawidek return (NULL);
245032115b10SPawel Jakub Dawidek }
2451