2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
54 #include <activemap.h>
61 #include "hast_proto.h"
77 TAILQ_ENTRY(hio) hio_next;
80 static struct hast_resource *gres;
83 * Free list holds unused structures. When free list is empty, we have to wait
84 * until some in-progress requests are freed.
86 static TAILQ_HEAD(, hio) hio_free_list;
87 static size_t hio_free_list_size;
88 static pthread_mutex_t hio_free_list_lock;
89 static pthread_cond_t hio_free_list_cond;
91 * Disk thread (the one that does I/O requests) takes requests from this list.
93 static TAILQ_HEAD(, hio) hio_disk_list;
94 static size_t hio_disk_list_size;
95 static pthread_mutex_t hio_disk_list_lock;
96 static pthread_cond_t hio_disk_list_cond;
98 * Thread that sends requests back to primary takes requests from this list.
100 static TAILQ_HEAD(, hio) hio_send_list;
101 static size_t hio_send_list_size;
102 static pthread_mutex_t hio_send_list_lock;
103 static pthread_cond_t hio_send_list_cond;
106 * Maximum number of outstanding I/O requests.
108 #define HAST_HIO_MAX 256
110 static void *recv_thread(void *arg);
111 static void *disk_thread(void *arg);
112 static void *send_thread(void *arg);
114 #define QUEUE_INSERT(name, hio) do { \
115 mtx_lock(&hio_##name##_list_lock); \
116 if (TAILQ_EMPTY(&hio_##name##_list)) \
117 cv_broadcast(&hio_##name##_list_cond); \
118 TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next); \
119 hio_##name##_list_size++; \
120 mtx_unlock(&hio_##name##_list_lock); \
122 #define QUEUE_TAKE(name, hio) do { \
123 mtx_lock(&hio_##name##_list_lock); \
124 while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \
125 cv_wait(&hio_##name##_list_cond, \
126 &hio_##name##_list_lock); \
128 PJDLOG_ASSERT(hio_##name##_list_size != 0); \
129 hio_##name##_list_size--; \
130 TAILQ_REMOVE(&hio_##name##_list, (hio), hio_next); \
131 mtx_unlock(&hio_##name##_list_lock); \
135 output_status_aux(struct nv *nvout)
138 nv_add_uint64(nvout, (uint64_t)hio_free_list_size, "idle_queue_size");
139 nv_add_uint64(nvout, (uint64_t)hio_disk_list_size, "local_queue_size");
140 nv_add_uint64(nvout, (uint64_t)hio_send_list_size, "send_queue_size");
144 hio_clear(struct hio *hio)
149 hio->hio_cmd = HIO_UNDEF;
152 hio->hio_memsync = false;
156 hio_copy(const struct hio *srchio, struct hio *dsthio)
160 * We don't copy hio_error, hio_data and hio_next fields.
163 dsthio->hio_seq = srchio->hio_seq;
164 dsthio->hio_cmd = srchio->hio_cmd;
165 dsthio->hio_offset = srchio->hio_offset;
166 dsthio->hio_length = srchio->hio_length;
167 dsthio->hio_memsync = srchio->hio_memsync;
171 init_environment(void)
177 * Initialize lists, their locks and theirs condition variables.
179 TAILQ_INIT(&hio_free_list);
180 mtx_init(&hio_free_list_lock);
181 cv_init(&hio_free_list_cond);
182 TAILQ_INIT(&hio_disk_list);
183 mtx_init(&hio_disk_list_lock);
184 cv_init(&hio_disk_list_cond);
185 TAILQ_INIT(&hio_send_list);
186 mtx_init(&hio_send_list_lock);
187 cv_init(&hio_send_list_cond);
190 * Allocate requests pool and initialize requests.
192 for (ii = 0; ii < HAST_HIO_MAX; ii++) {
193 hio = malloc(sizeof(*hio));
195 pjdlog_exitx(EX_TEMPFAIL,
196 "Unable to allocate memory (%zu bytes) for hio request.",
199 hio->hio_data = malloc(MAXPHYS);
200 if (hio->hio_data == NULL) {
201 pjdlog_exitx(EX_TEMPFAIL,
202 "Unable to allocate memory (%zu bytes) for gctl_data.",
206 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next);
207 hio_free_list_size++;
212 init_local(struct hast_resource *res)
215 if (metadata_read(res, true) == -1)
220 init_remote(struct hast_resource *res, struct nv *nvin)
228 /* Setup direction. */
229 if (proto_send(res->hr_remoteout, NULL, 0) == -1)
230 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
234 nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize");
235 nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize");
236 resuid = nv_get_uint64(nvin, "resuid");
237 res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt");
238 res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt");
239 nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt");
240 nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt");
241 mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
242 METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize);
243 map = malloc(mapsize);
245 pjdlog_exitx(EX_TEMPFAIL,
246 "Unable to allocate memory (%zu bytes) for activemap.",
250 * When we work as primary and secondary is missing we will increase
251 * localcnt in our metadata. When secondary is connected and synced
252 * we make localcnt be equal to remotecnt, which means nodes are more
254 * Split-brain condition is when both nodes are not able to communicate
255 * and are both configured as primary nodes. In turn, they can both
256 * make incompatible changes to the data and we have to detect that.
257 * Under split-brain condition we will increase our localcnt on first
258 * write and remote node will increase its localcnt on first write.
259 * When we connect we can see that primary's localcnt is greater than
260 * our remotecnt (primary was modified while we weren't watching) and
261 * our localcnt is greater than primary's remotecnt (we were modified
262 * while primary wasn't watching).
263 * There are many possible combinations which are all gathered below.
264 * Don't pay too much attention to exact numbers, the more important
265 * is to compare them. We compare secondary's local with primary's
266 * remote and secondary's remote with primary's local.
267 * Note that every case where primary's localcnt is smaller than
268 * secondary's remotecnt and where secondary's localcnt is smaller than
269 * primary's remotecnt should be impossible in practise. We will perform
270 * full synchronization then. Those cases are marked with an asterisk.
271 * Regular synchronization means that only extents marked as dirty are
272 * synchronized (regular synchronization).
274 * SECONDARY METADATA PRIMARY METADATA
275 * local=3 remote=3 local=2 remote=2* ?! Full sync from secondary.
276 * local=3 remote=3 local=2 remote=3* ?! Full sync from primary.
277 * local=3 remote=3 local=2 remote=4* ?! Full sync from primary.
278 * local=3 remote=3 local=3 remote=2 Primary is out-of-date,
279 * regular sync from secondary.
280 * local=3 remote=3 local=3 remote=3 Regular sync just in case.
281 * local=3 remote=3 local=3 remote=4* ?! Full sync from primary.
282 * local=3 remote=3 local=4 remote=2 Split-brain condition.
283 * local=3 remote=3 local=4 remote=3 Secondary out-of-date,
284 * regular sync from primary.
285 * local=3 remote=3 local=4 remote=4* ?! Full sync from primary.
287 if (res->hr_resuid == 0) {
289 * Provider is used for the first time. If primary node done no
290 * writes yet as well (we will find "virgin" argument) then
291 * there is no need to synchronize anything. If primary node
292 * done any writes already we have to synchronize everything.
294 PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
295 res->hr_resuid = resuid;
296 if (metadata_write(res) == -1)
298 if (nv_exists(nvin, "virgin")) {
303 memset(map, 0xff, mapsize);
305 nv_add_int8(nvout, 1, "virgin");
306 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
307 } else if (res->hr_resuid != resuid) {
311 (void)snprintf(errmsg, sizeof(errmsg),
312 "Resource unique ID mismatch (primary=%ju, secondary=%ju).",
313 (uintmax_t)resuid, (uintmax_t)res->hr_resuid);
314 pjdlog_error("%s", errmsg);
315 nv_add_string(nvout, errmsg, "errmsg");
316 if (hast_proto_send(res, res->hr_remotein, nvout,
318 pjdlog_exit(EX_TEMPFAIL,
319 "Unable to send response to %s",
325 /* Is primary out-of-date? */
326 (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
327 res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
328 /* Are the nodes more or less in sync? */
329 (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
330 res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
331 /* Is secondary out-of-date? */
332 (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
333 res->hr_secondary_remotecnt < res->hr_primary_localcnt)) {
335 * Nodes are more or less in sync or one of the nodes is
337 * It doesn't matter at this point which one, we just have to
338 * send out local bitmap to the remote node.
340 if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) !=
342 pjdlog_exit(LOG_ERR, "Unable to read activemap");
344 if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
345 res->hr_secondary_remotecnt == res->hr_primary_localcnt) {
346 /* Primary is out-of-date, sync from secondary. */
347 nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
350 * Secondary is out-of-date or counts match.
353 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
355 } else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
356 res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
358 * Not good, we have split-brain condition.
361 pjdlog_error("Split-brain detected, exiting.");
362 nv_add_string(nvout, "Split-brain condition!", "errmsg");
363 if (hast_proto_send(res, res->hr_remotein, nvout,
365 pjdlog_exit(EX_TEMPFAIL,
366 "Unable to send response to %s",
370 /* Exit on split-brain. */
371 event_send(res, EVENT_SPLITBRAIN);
373 } else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
374 res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ {
376 * This should never happen in practise, but we will perform
377 * full synchronization.
379 PJDLOG_ASSERT(res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
380 res->hr_primary_localcnt < res->hr_secondary_remotecnt);
381 mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
382 METADATA_SIZE, res->hr_extentsize,
383 res->hr_local_sectorsize);
384 memset(map, 0xff, mapsize);
385 if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) {
386 /* In this one of five cases sync from secondary. */
387 nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
389 /* For the rest four cases sync from primary. */
390 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
392 pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).",
393 (uintmax_t)res->hr_primary_localcnt,
394 (uintmax_t)res->hr_primary_remotecnt,
395 (uintmax_t)res->hr_secondary_localcnt,
396 (uintmax_t)res->hr_secondary_remotecnt);
398 nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize");
399 if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) == -1) {
400 pjdlog_exit(EX_TEMPFAIL, "Unable to send activemap to %s",
407 /* Setup direction. */
408 if (proto_recv(res->hr_remotein, NULL, 0) == -1)
409 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
414 hastd_secondary(struct hast_resource *res, struct nv *nvin)
419 int error, mode, debuglevel;
422 * Create communication channel between parent and child.
424 if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
425 KEEP_ERRNO((void)pidfile_remove(pfh));
426 pjdlog_exit(EX_OSERR,
427 "Unable to create control sockets between parent and child");
430 * Create communication channel between child and parent.
432 if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
433 KEEP_ERRNO((void)pidfile_remove(pfh));
434 pjdlog_exit(EX_OSERR,
435 "Unable to create event sockets between child and parent");
440 KEEP_ERRNO((void)pidfile_remove(pfh));
441 pjdlog_exit(EX_OSERR, "Unable to fork");
445 /* This is parent. */
446 proto_close(res->hr_remotein);
447 res->hr_remotein = NULL;
448 proto_close(res->hr_remoteout);
449 res->hr_remoteout = NULL;
450 /* Declare that we are receiver. */
451 proto_recv(res->hr_event, NULL, 0);
452 /* Declare that we are sender. */
453 proto_send(res->hr_ctrl, NULL, 0);
454 res->hr_workerpid = pid;
459 res->output_status_aux = output_status_aux;
460 mode = pjdlog_mode_get();
461 debuglevel = pjdlog_debug_get();
463 /* Declare that we are sender. */
464 proto_send(res->hr_event, NULL, 0);
465 /* Declare that we are receiver. */
466 proto_recv(res->hr_ctrl, NULL, 0);
467 descriptors_cleanup(res);
469 descriptors_assert(res, mode);
472 pjdlog_debug_set(debuglevel);
473 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
474 setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
476 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
477 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
479 /* Error in setting timeout is not critical, but why should it fail? */
480 if (proto_timeout(res->hr_remotein, 2 * HAST_KEEPALIVE) == -1)
481 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
482 if (proto_timeout(res->hr_remoteout, res->hr_timeout) == -1)
483 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
488 if (drop_privs(res) != 0)
490 pjdlog_info("Privileges successfully dropped.");
493 * Create the control thread before sending any event to the parent,
494 * as we can deadlock when parent sends control request to worker,
495 * but worker has no control thread started yet, so parent waits.
496 * In the meantime worker sends an event to the parent, but parent
497 * is unable to handle the event, because it waits for control
500 error = pthread_create(&td, NULL, ctrl_thread, res);
501 PJDLOG_ASSERT(error == 0);
503 init_remote(res, nvin);
504 event_send(res, EVENT_CONNECT);
506 error = pthread_create(&td, NULL, recv_thread, res);
507 PJDLOG_ASSERT(error == 0);
508 error = pthread_create(&td, NULL, disk_thread, res);
509 PJDLOG_ASSERT(error == 0);
510 (void)send_thread(res);
514 reqlog(int loglevel, int debuglevel, int error, struct hio *hio,
515 const char *fmt, ...)
522 len = vsnprintf(msg, sizeof(msg), fmt, ap);
524 if ((size_t)len < sizeof(msg)) {
525 switch (hio->hio_cmd) {
527 (void)snprintf(msg + len, sizeof(msg) - len,
528 "READ(%ju, %ju).", (uintmax_t)hio->hio_offset,
529 (uintmax_t)hio->hio_length);
532 (void)snprintf(msg + len, sizeof(msg) - len,
533 "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset,
534 (uintmax_t)hio->hio_length);
537 (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
540 (void)snprintf(msg + len, sizeof(msg) - len,
541 "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset,
542 (uintmax_t)hio->hio_length);
545 (void)snprintf(msg + len, sizeof(msg) - len, "KEEPALIVE.");
548 (void)snprintf(msg + len, sizeof(msg) - len,
549 "UNKNOWN(%u).", (unsigned int)hio->hio_cmd);
553 pjdlog_common(loglevel, debuglevel, error, "%s", msg);
557 requnpack(struct hast_resource *res, struct hio *hio, struct nv *nv)
560 hio->hio_cmd = nv_get_uint8(nv, "cmd");
561 if (hio->hio_cmd == 0) {
562 pjdlog_error("Header contains no 'cmd' field.");
563 hio->hio_error = EINVAL;
566 if (hio->hio_cmd != HIO_KEEPALIVE) {
567 hio->hio_seq = nv_get_uint64(nv, "seq");
568 if (hio->hio_seq == 0) {
569 pjdlog_error("Header contains no 'seq' field.");
570 hio->hio_error = EINVAL;
574 switch (hio->hio_cmd) {
579 hio->hio_memsync = nv_exists(nv, "memsync");
583 hio->hio_offset = nv_get_uint64(nv, "offset");
584 if (nv_error(nv) != 0) {
585 pjdlog_error("Header is missing 'offset' field.");
586 hio->hio_error = EINVAL;
589 hio->hio_length = nv_get_uint64(nv, "length");
590 if (nv_error(nv) != 0) {
591 pjdlog_error("Header is missing 'length' field.");
592 hio->hio_error = EINVAL;
595 if (hio->hio_length == 0) {
596 pjdlog_error("Data length is zero.");
597 hio->hio_error = EINVAL;
600 if (hio->hio_cmd != HIO_DELETE && hio->hio_length > MAXPHYS) {
601 pjdlog_error("Data length is too large (%ju > %ju).",
602 (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS);
603 hio->hio_error = EINVAL;
606 if ((hio->hio_offset % res->hr_local_sectorsize) != 0) {
607 pjdlog_error("Offset %ju is not multiple of sector size.",
608 (uintmax_t)hio->hio_offset);
609 hio->hio_error = EINVAL;
612 if ((hio->hio_length % res->hr_local_sectorsize) != 0) {
613 pjdlog_error("Length %ju is not multiple of sector size.",
614 (uintmax_t)hio->hio_length);
615 hio->hio_error = EINVAL;
618 if (hio->hio_offset + hio->hio_length >
619 (uint64_t)res->hr_datasize) {
620 pjdlog_error("Data offset is too large (%ju > %ju).",
621 (uintmax_t)(hio->hio_offset + hio->hio_length),
622 (uintmax_t)res->hr_datasize);
623 hio->hio_error = EINVAL;
628 pjdlog_error("Header contains invalid 'cmd' (%hhu).",
630 hio->hio_error = EINVAL;
635 return (hio->hio_error);
639 secondary_exit(int exitcode, const char *fmt, ...)
643 PJDLOG_ASSERT(exitcode != EX_OK);
645 pjdlogv_errno(LOG_ERR, fmt, ap);
647 event_send(gres, EVENT_DISCONNECT);
652 * Thread receives requests from the primary node.
655 recv_thread(void *arg)
657 struct hast_resource *res = arg;
658 struct hio *hio, *mshio;
662 pjdlog_debug(2, "recv: Taking free request.");
663 QUEUE_TAKE(free, hio);
664 pjdlog_debug(2, "recv: (%p) Got request.", hio);
665 if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
666 secondary_exit(EX_TEMPFAIL,
667 "Unable to receive request header");
669 if (requnpack(res, hio, nv) != 0) {
672 "recv: (%p) Moving request to the send queue.",
674 QUEUE_INSERT(send, hio);
677 switch (hio->hio_cmd) {
682 res->hr_stat_write++;
685 res->hr_stat_delete++;
688 res->hr_stat_flush++;
693 PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
696 reqlog(LOG_DEBUG, 2, -1, hio,
697 "recv: (%p) Got request header: ", hio);
698 if (hio->hio_cmd == HIO_KEEPALIVE) {
701 "recv: (%p) Moving request to the free queue.",
704 QUEUE_INSERT(free, hio);
706 } else if (hio->hio_cmd == HIO_WRITE) {
707 if (hast_proto_recv_data(res, res->hr_remotein, nv,
708 hio->hio_data, MAXPHYS) == -1) {
709 secondary_exit(EX_TEMPFAIL,
710 "Unable to receive request data");
712 if (hio->hio_memsync) {
714 * For memsync requests we expect two replies.
715 * Clone the hio so we can handle both of them.
717 pjdlog_debug(2, "recv: Taking free request.");
718 QUEUE_TAKE(free, mshio);
719 pjdlog_debug(2, "recv: (%p) Got request.",
721 hio_copy(hio, mshio);
722 mshio->hio_error = 0;
724 * We want to keep 'memsync' tag only on the
725 * request going onto send queue (mshio).
727 hio->hio_memsync = false;
729 "recv: (%p) Moving memsync request to the send queue.",
731 QUEUE_INSERT(send, mshio);
735 pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.",
737 QUEUE_INSERT(disk, hio);
744 * Thread reads from or writes to local component and also handles DELETE and
748 disk_thread(void *arg)
750 struct hast_resource *res = arg;
753 bool clear_activemap, logerror;
755 clear_activemap = true;
758 pjdlog_debug(2, "disk: Taking request.");
759 QUEUE_TAKE(disk, hio);
760 while (clear_activemap) {
765 * When first request is received, it means that primary
766 * already received our activemap, merged it and stored
767 * locally. We can now safely clear our activemap.
770 activemap_calc_ondisk_size(res->hr_local_mediasize -
771 METADATA_SIZE, res->hr_extentsize,
772 res->hr_local_sectorsize);
773 map = calloc(1, mapsize);
775 pjdlog_warning("Unable to allocate memory to clear local activemap.");
778 if (pwrite(res->hr_localfd, map, mapsize,
779 METADATA_SIZE) != (ssize_t)mapsize) {
780 pjdlog_errno(LOG_WARNING,
781 "Unable to store cleared activemap");
783 res->hr_stat_activemap_write_error++;
787 clear_activemap = false;
788 pjdlog_debug(1, "Local activemap cleared.");
791 reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio);
793 /* Handle the actual request. */
794 switch (hio->hio_cmd) {
796 ret = pread(res->hr_localfd, hio->hio_data,
798 hio->hio_offset + res->hr_localoff);
800 hio->hio_error = errno;
801 else if (ret != (int64_t)hio->hio_length)
802 hio->hio_error = EIO;
807 ret = pwrite(res->hr_localfd, hio->hio_data,
809 hio->hio_offset + res->hr_localoff);
811 hio->hio_error = errno;
812 else if (ret != (int64_t)hio->hio_length)
813 hio->hio_error = EIO;
818 ret = g_delete(res->hr_localfd,
819 hio->hio_offset + res->hr_localoff,
822 hio->hio_error = errno;
827 if (!res->hr_localflush) {
829 hio->hio_error = EOPNOTSUPP;
833 ret = g_flush(res->hr_localfd);
835 if (errno == EOPNOTSUPP)
836 res->hr_localflush = false;
837 hio->hio_error = errno;
843 PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
846 if (logerror && hio->hio_error != 0) {
847 reqlog(LOG_ERR, 0, hio->hio_error, hio,
850 pjdlog_debug(2, "disk: (%p) Moving request to the send queue.",
852 QUEUE_INSERT(send, hio);
859 * Thread sends requests back to primary node.
862 send_thread(void *arg)
864 struct hast_resource *res = arg;
871 pjdlog_debug(2, "send: Taking request.");
872 QUEUE_TAKE(send, hio);
873 reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio);
875 /* Copy sequence number. */
876 nv_add_uint64(nvout, hio->hio_seq, "seq");
877 if (hio->hio_memsync) {
878 PJDLOG_ASSERT(hio->hio_cmd == HIO_WRITE);
879 nv_add_int8(nvout, 1, "received");
881 switch (hio->hio_cmd) {
883 if (hio->hio_error == 0) {
884 data = hio->hio_data;
885 length = hio->hio_length;
889 * We send no data in case of an error.
899 PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
902 if (hio->hio_error != 0) {
903 switch (hio->hio_cmd) {
905 res->hr_stat_read_error++;
908 res->hr_stat_write_error++;
911 res->hr_stat_delete_error++;
914 res->hr_stat_flush_error++;
917 nv_add_int16(nvout, hio->hio_error, "error");
919 if (hast_proto_send(res, res->hr_remoteout, nvout, data,
921 secondary_exit(EX_TEMPFAIL, "Unable to send reply");
924 pjdlog_debug(2, "send: (%p) Moving request to the free queue.",
927 QUEUE_INSERT(free, hio);