2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
52 #include <activemap.h>
59 #include "hast_proto.h"
75 TAILQ_ENTRY(hio) hio_next;
78 static struct hast_resource *gres;
81 * Free list holds unused structures. When free list is empty, we have to wait
82 * until some in-progress requests are freed.
84 static TAILQ_HEAD(, hio) hio_free_list;
85 static pthread_mutex_t hio_free_list_lock;
86 static pthread_cond_t hio_free_list_cond;
88 * Disk thread (the one that do I/O requests) takes requests from this list.
90 static TAILQ_HEAD(, hio) hio_disk_list;
91 static pthread_mutex_t hio_disk_list_lock;
92 static pthread_cond_t hio_disk_list_cond;
94 * There is one recv list for every component, although local components don't
95 * use recv lists as local requests are done synchronously.
97 static TAILQ_HEAD(, hio) hio_send_list;
98 static pthread_mutex_t hio_send_list_lock;
99 static pthread_cond_t hio_send_list_cond;
102 * Maximum number of outstanding I/O requests.
104 #define HAST_HIO_MAX 256
106 static void *recv_thread(void *arg);
107 static void *disk_thread(void *arg);
108 static void *send_thread(void *arg);
110 #define QUEUE_INSERT(name, hio) do { \
113 mtx_lock(&hio_##name##_list_lock); \
114 _wakeup = TAILQ_EMPTY(&hio_##name##_list); \
115 TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next); \
116 mtx_unlock(&hio_##name##_list_lock); \
118 cv_signal(&hio_##name##_list_cond); \
120 #define QUEUE_TAKE(name, hio) do { \
121 mtx_lock(&hio_##name##_list_lock); \
122 while (((hio) = TAILQ_FIRST(&hio_##name##_list)) == NULL) { \
123 cv_wait(&hio_##name##_list_cond, \
124 &hio_##name##_list_lock); \
126 TAILQ_REMOVE(&hio_##name##_list, (hio), hio_next); \
127 mtx_unlock(&hio_##name##_list_lock); \
131 hio_clear(struct hio *hio)
136 hio->hio_cmd = HIO_UNDEF;
139 hio->hio_memsync = false;
143 hio_copy(const struct hio *srchio, struct hio *dsthio)
147 * We don't copy hio_error, hio_data and hio_next fields.
150 dsthio->hio_seq = srchio->hio_seq;
151 dsthio->hio_cmd = srchio->hio_cmd;
152 dsthio->hio_offset = srchio->hio_offset;
153 dsthio->hio_length = srchio->hio_length;
154 dsthio->hio_memsync = srchio->hio_memsync;
158 init_environment(void)
164 * Initialize lists, their locks and theirs condition variables.
166 TAILQ_INIT(&hio_free_list);
167 mtx_init(&hio_free_list_lock);
168 cv_init(&hio_free_list_cond);
169 TAILQ_INIT(&hio_disk_list);
170 mtx_init(&hio_disk_list_lock);
171 cv_init(&hio_disk_list_cond);
172 TAILQ_INIT(&hio_send_list);
173 mtx_init(&hio_send_list_lock);
174 cv_init(&hio_send_list_cond);
177 * Allocate requests pool and initialize requests.
179 for (ii = 0; ii < HAST_HIO_MAX; ii++) {
180 hio = malloc(sizeof(*hio));
182 pjdlog_exitx(EX_TEMPFAIL,
183 "Unable to allocate memory (%zu bytes) for hio request.",
186 hio->hio_data = malloc(MAXPHYS);
187 if (hio->hio_data == NULL) {
188 pjdlog_exitx(EX_TEMPFAIL,
189 "Unable to allocate memory (%zu bytes) for gctl_data.",
193 TAILQ_INSERT_HEAD(&hio_free_list, hio, hio_next);
198 init_local(struct hast_resource *res)
201 if (metadata_read(res, true) == -1)
206 init_remote(struct hast_resource *res, struct nv *nvin)
214 /* Setup direction. */
215 if (proto_send(res->hr_remoteout, NULL, 0) == -1)
216 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
220 nv_add_int64(nvout, (int64_t)res->hr_datasize, "datasize");
221 nv_add_int32(nvout, (int32_t)res->hr_extentsize, "extentsize");
222 resuid = nv_get_uint64(nvin, "resuid");
223 res->hr_primary_localcnt = nv_get_uint64(nvin, "localcnt");
224 res->hr_primary_remotecnt = nv_get_uint64(nvin, "remotecnt");
225 nv_add_uint64(nvout, res->hr_secondary_localcnt, "localcnt");
226 nv_add_uint64(nvout, res->hr_secondary_remotecnt, "remotecnt");
227 mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
228 METADATA_SIZE, res->hr_extentsize, res->hr_local_sectorsize);
229 map = malloc(mapsize);
231 pjdlog_exitx(EX_TEMPFAIL,
232 "Unable to allocate memory (%zu bytes) for activemap.",
236 * When we work as primary and secondary is missing we will increase
237 * localcnt in our metadata. When secondary is connected and synced
238 * we make localcnt be equal to remotecnt, which means nodes are more
240 * Split-brain condition is when both nodes are not able to communicate
241 * and are both configured as primary nodes. In turn, they can both
242 * make incompatible changes to the data and we have to detect that.
243 * Under split-brain condition we will increase our localcnt on first
244 * write and remote node will increase its localcnt on first write.
245 * When we connect we can see that primary's localcnt is greater than
246 * our remotecnt (primary was modified while we weren't watching) and
247 * our localcnt is greater than primary's remotecnt (we were modified
248 * while primary wasn't watching).
249 * There are many possible combinations which are all gathered below.
250 * Don't pay too much attention to exact numbers, the more important
251 * is to compare them. We compare secondary's local with primary's
252 * remote and secondary's remote with primary's local.
253 * Note that every case where primary's localcnt is smaller than
254 * secondary's remotecnt and where secondary's localcnt is smaller than
255 * primary's remotecnt should be impossible in practise. We will perform
256 * full synchronization then. Those cases are marked with an asterisk.
257 * Regular synchronization means that only extents marked as dirty are
258 * synchronized (regular synchronization).
260 * SECONDARY METADATA PRIMARY METADATA
261 * local=3 remote=3 local=2 remote=2* ?! Full sync from secondary.
262 * local=3 remote=3 local=2 remote=3* ?! Full sync from primary.
263 * local=3 remote=3 local=2 remote=4* ?! Full sync from primary.
264 * local=3 remote=3 local=3 remote=2 Primary is out-of-date,
265 * regular sync from secondary.
266 * local=3 remote=3 local=3 remote=3 Regular sync just in case.
267 * local=3 remote=3 local=3 remote=4* ?! Full sync from primary.
268 * local=3 remote=3 local=4 remote=2 Split-brain condition.
269 * local=3 remote=3 local=4 remote=3 Secondary out-of-date,
270 * regular sync from primary.
271 * local=3 remote=3 local=4 remote=4* ?! Full sync from primary.
273 if (res->hr_resuid == 0) {
275 * Provider is used for the first time. If primary node done no
276 * writes yet as well (we will find "virgin" argument) then
277 * there is no need to synchronize anything. If primary node
278 * done any writes already we have to synchronize everything.
280 PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
281 res->hr_resuid = resuid;
282 if (metadata_write(res) == -1)
284 if (nv_exists(nvin, "virgin")) {
289 memset(map, 0xff, mapsize);
291 nv_add_int8(nvout, 1, "virgin");
292 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
293 } else if (res->hr_resuid != resuid) {
297 (void)snprintf(errmsg, sizeof(errmsg),
298 "Resource unique ID mismatch (primary=%ju, secondary=%ju).",
299 (uintmax_t)resuid, (uintmax_t)res->hr_resuid);
300 pjdlog_error("%s", errmsg);
301 nv_add_string(nvout, errmsg, "errmsg");
302 if (hast_proto_send(res, res->hr_remotein, nvout,
304 pjdlog_exit(EX_TEMPFAIL,
305 "Unable to send response to %s",
311 /* Is primary out-of-date? */
312 (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
313 res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
314 /* Are the nodes more or less in sync? */
315 (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
316 res->hr_secondary_remotecnt == res->hr_primary_localcnt) ||
317 /* Is secondary out-of-date? */
318 (res->hr_secondary_localcnt == res->hr_primary_remotecnt &&
319 res->hr_secondary_remotecnt < res->hr_primary_localcnt)) {
321 * Nodes are more or less in sync or one of the nodes is
323 * It doesn't matter at this point which one, we just have to
324 * send out local bitmap to the remote node.
326 if (pread(res->hr_localfd, map, mapsize, METADATA_SIZE) !=
328 pjdlog_exit(LOG_ERR, "Unable to read activemap");
330 if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
331 res->hr_secondary_remotecnt == res->hr_primary_localcnt) {
332 /* Primary is out-of-date, sync from secondary. */
333 nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
336 * Secondary is out-of-date or counts match.
339 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
341 } else if (res->hr_secondary_localcnt > res->hr_primary_remotecnt &&
342 res->hr_primary_localcnt > res->hr_secondary_remotecnt) {
344 * Not good, we have split-brain condition.
347 pjdlog_error("Split-brain detected, exiting.");
348 nv_add_string(nvout, "Split-brain condition!", "errmsg");
349 if (hast_proto_send(res, res->hr_remotein, nvout,
351 pjdlog_exit(EX_TEMPFAIL,
352 "Unable to send response to %s",
356 /* Exit on split-brain. */
357 event_send(res, EVENT_SPLITBRAIN);
359 } else /* if (res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
360 res->hr_primary_localcnt < res->hr_secondary_remotecnt) */ {
362 * This should never happen in practise, but we will perform
363 * full synchronization.
365 PJDLOG_ASSERT(res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
366 res->hr_primary_localcnt < res->hr_secondary_remotecnt);
367 mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
368 METADATA_SIZE, res->hr_extentsize,
369 res->hr_local_sectorsize);
370 memset(map, 0xff, mapsize);
371 if (res->hr_secondary_localcnt > res->hr_primary_remotecnt) {
372 /* In this one of five cases sync from secondary. */
373 nv_add_uint8(nvout, HAST_SYNCSRC_SECONDARY, "syncsrc");
375 /* For the rest four cases sync from primary. */
376 nv_add_uint8(nvout, HAST_SYNCSRC_PRIMARY, "syncsrc");
378 pjdlog_warning("This should never happen, asking for full synchronization (primary(local=%ju, remote=%ju), secondary(local=%ju, remote=%ju)).",
379 (uintmax_t)res->hr_primary_localcnt,
380 (uintmax_t)res->hr_primary_remotecnt,
381 (uintmax_t)res->hr_secondary_localcnt,
382 (uintmax_t)res->hr_secondary_remotecnt);
384 nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize");
385 if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) == -1) {
386 pjdlog_exit(EX_TEMPFAIL, "Unable to send activemap to %s",
393 /* Setup direction. */
394 if (proto_recv(res->hr_remotein, NULL, 0) == -1)
395 pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
400 hastd_secondary(struct hast_resource *res, struct nv *nvin)
405 int error, mode, debuglevel;
408 * Create communication channel between parent and child.
410 if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
411 KEEP_ERRNO((void)pidfile_remove(pfh));
412 pjdlog_exit(EX_OSERR,
413 "Unable to create control sockets between parent and child");
416 * Create communication channel between child and parent.
418 if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
419 KEEP_ERRNO((void)pidfile_remove(pfh));
420 pjdlog_exit(EX_OSERR,
421 "Unable to create event sockets between child and parent");
426 KEEP_ERRNO((void)pidfile_remove(pfh));
427 pjdlog_exit(EX_OSERR, "Unable to fork");
431 /* This is parent. */
432 proto_close(res->hr_remotein);
433 res->hr_remotein = NULL;
434 proto_close(res->hr_remoteout);
435 res->hr_remoteout = NULL;
436 /* Declare that we are receiver. */
437 proto_recv(res->hr_event, NULL, 0);
438 /* Declare that we are sender. */
439 proto_send(res->hr_ctrl, NULL, 0);
440 res->hr_workerpid = pid;
445 mode = pjdlog_mode_get();
446 debuglevel = pjdlog_debug_get();
448 /* Declare that we are sender. */
449 proto_send(res->hr_event, NULL, 0);
450 /* Declare that we are receiver. */
451 proto_recv(res->hr_ctrl, NULL, 0);
452 descriptors_cleanup(res);
454 descriptors_assert(res, mode);
457 pjdlog_debug_set(debuglevel);
458 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
459 setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));
461 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
462 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
464 /* Error in setting timeout is not critical, but why should it fail? */
465 if (proto_timeout(res->hr_remotein, 2 * HAST_KEEPALIVE) == -1)
466 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
467 if (proto_timeout(res->hr_remoteout, res->hr_timeout) == -1)
468 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
473 if (drop_privs(res) != 0)
475 pjdlog_info("Privileges successfully dropped.");
478 * Create the control thread before sending any event to the parent,
479 * as we can deadlock when parent sends control request to worker,
480 * but worker has no control thread started yet, so parent waits.
481 * In the meantime worker sends an event to the parent, but parent
482 * is unable to handle the event, because it waits for control
485 error = pthread_create(&td, NULL, ctrl_thread, res);
486 PJDLOG_ASSERT(error == 0);
488 init_remote(res, nvin);
489 event_send(res, EVENT_CONNECT);
491 error = pthread_create(&td, NULL, recv_thread, res);
492 PJDLOG_ASSERT(error == 0);
493 error = pthread_create(&td, NULL, disk_thread, res);
494 PJDLOG_ASSERT(error == 0);
495 (void)send_thread(res);
499 reqlog(int loglevel, int debuglevel, int error, struct hio *hio,
500 const char *fmt, ...)
507 len = vsnprintf(msg, sizeof(msg), fmt, ap);
509 if ((size_t)len < sizeof(msg)) {
510 switch (hio->hio_cmd) {
512 (void)snprintf(msg + len, sizeof(msg) - len,
513 "READ(%ju, %ju).", (uintmax_t)hio->hio_offset,
514 (uintmax_t)hio->hio_length);
517 (void)snprintf(msg + len, sizeof(msg) - len,
518 "DELETE(%ju, %ju).", (uintmax_t)hio->hio_offset,
519 (uintmax_t)hio->hio_length);
522 (void)snprintf(msg + len, sizeof(msg) - len, "FLUSH.");
525 (void)snprintf(msg + len, sizeof(msg) - len,
526 "WRITE(%ju, %ju).", (uintmax_t)hio->hio_offset,
527 (uintmax_t)hio->hio_length);
530 (void)snprintf(msg + len, sizeof(msg) - len, "KEEPALIVE.");
533 (void)snprintf(msg + len, sizeof(msg) - len,
534 "UNKNOWN(%u).", (unsigned int)hio->hio_cmd);
538 pjdlog_common(loglevel, debuglevel, error, "%s", msg);
542 requnpack(struct hast_resource *res, struct hio *hio, struct nv *nv)
545 hio->hio_cmd = nv_get_uint8(nv, "cmd");
546 if (hio->hio_cmd == 0) {
547 pjdlog_error("Header contains no 'cmd' field.");
548 hio->hio_error = EINVAL;
551 if (hio->hio_cmd != HIO_KEEPALIVE) {
552 hio->hio_seq = nv_get_uint64(nv, "seq");
553 if (hio->hio_seq == 0) {
554 pjdlog_error("Header contains no 'seq' field.");
555 hio->hio_error = EINVAL;
559 switch (hio->hio_cmd) {
564 hio->hio_memsync = nv_exists(nv, "memsync");
568 hio->hio_offset = nv_get_uint64(nv, "offset");
569 if (nv_error(nv) != 0) {
570 pjdlog_error("Header is missing 'offset' field.");
571 hio->hio_error = EINVAL;
574 hio->hio_length = nv_get_uint64(nv, "length");
575 if (nv_error(nv) != 0) {
576 pjdlog_error("Header is missing 'length' field.");
577 hio->hio_error = EINVAL;
580 if (hio->hio_length == 0) {
581 pjdlog_error("Data length is zero.");
582 hio->hio_error = EINVAL;
585 if (hio->hio_cmd != HIO_DELETE && hio->hio_length > MAXPHYS) {
586 pjdlog_error("Data length is too large (%ju > %ju).",
587 (uintmax_t)hio->hio_length, (uintmax_t)MAXPHYS);
588 hio->hio_error = EINVAL;
591 if ((hio->hio_offset % res->hr_local_sectorsize) != 0) {
592 pjdlog_error("Offset %ju is not multiple of sector size.",
593 (uintmax_t)hio->hio_offset);
594 hio->hio_error = EINVAL;
597 if ((hio->hio_length % res->hr_local_sectorsize) != 0) {
598 pjdlog_error("Length %ju is not multiple of sector size.",
599 (uintmax_t)hio->hio_length);
600 hio->hio_error = EINVAL;
603 if (hio->hio_offset + hio->hio_length >
604 (uint64_t)res->hr_datasize) {
605 pjdlog_error("Data offset is too large (%ju > %ju).",
606 (uintmax_t)(hio->hio_offset + hio->hio_length),
607 (uintmax_t)res->hr_datasize);
608 hio->hio_error = EINVAL;
613 pjdlog_error("Header contains invalid 'cmd' (%hhu).",
615 hio->hio_error = EINVAL;
620 return (hio->hio_error);
624 secondary_exit(int exitcode, const char *fmt, ...)
628 PJDLOG_ASSERT(exitcode != EX_OK);
630 pjdlogv_errno(LOG_ERR, fmt, ap);
632 event_send(gres, EVENT_DISCONNECT);
637 * Thread receives requests from the primary node.
640 recv_thread(void *arg)
642 struct hast_resource *res = arg;
643 struct hio *hio, *mshio;
647 pjdlog_debug(2, "recv: Taking free request.");
648 QUEUE_TAKE(free, hio);
649 pjdlog_debug(2, "recv: (%p) Got request.", hio);
650 if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
651 secondary_exit(EX_TEMPFAIL,
652 "Unable to receive request header");
654 if (requnpack(res, hio, nv) != 0) {
657 "recv: (%p) Moving request to the send queue.",
659 QUEUE_INSERT(send, hio);
662 switch (hio->hio_cmd) {
667 res->hr_stat_write++;
670 res->hr_stat_delete++;
673 res->hr_stat_flush++;
678 PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
681 reqlog(LOG_DEBUG, 2, -1, hio,
682 "recv: (%p) Got request header: ", hio);
683 if (hio->hio_cmd == HIO_KEEPALIVE) {
686 "recv: (%p) Moving request to the free queue.",
689 QUEUE_INSERT(free, hio);
691 } else if (hio->hio_cmd == HIO_WRITE) {
692 if (hast_proto_recv_data(res, res->hr_remotein, nv,
693 hio->hio_data, MAXPHYS) == -1) {
694 secondary_exit(EX_TEMPFAIL,
695 "Unable to receive request data");
697 if (hio->hio_memsync) {
699 * For memsync requests we expect two replies.
700 * Clone the hio so we can handle both of them.
702 pjdlog_debug(2, "recv: Taking free request.");
703 QUEUE_TAKE(free, mshio);
704 pjdlog_debug(2, "recv: (%p) Got request.",
706 hio_copy(hio, mshio);
707 mshio->hio_error = 0;
709 * We want to keep 'memsync' tag only on the
710 * request going onto send queue (mshio).
712 hio->hio_memsync = false;
714 "recv: (%p) Moving memsync request to the send queue.",
716 QUEUE_INSERT(send, mshio);
720 pjdlog_debug(2, "recv: (%p) Moving request to the disk queue.",
722 QUEUE_INSERT(disk, hio);
729 * Thread reads from or writes to local component and also handles DELETE and
733 disk_thread(void *arg)
735 struct hast_resource *res = arg;
738 bool clear_activemap, logerror;
740 clear_activemap = true;
743 pjdlog_debug(2, "disk: Taking request.");
744 QUEUE_TAKE(disk, hio);
745 while (clear_activemap) {
750 * When first request is received, it means that primary
751 * already received our activemap, merged it and stored
752 * locally. We can now safely clear our activemap.
755 activemap_calc_ondisk_size(res->hr_local_mediasize -
756 METADATA_SIZE, res->hr_extentsize,
757 res->hr_local_sectorsize);
758 map = calloc(1, mapsize);
760 pjdlog_warning("Unable to allocate memory to clear local activemap.");
763 if (pwrite(res->hr_localfd, map, mapsize,
764 METADATA_SIZE) != (ssize_t)mapsize) {
765 pjdlog_errno(LOG_WARNING,
766 "Unable to store cleared activemap");
768 res->hr_stat_activemap_write_error++;
772 clear_activemap = false;
773 pjdlog_debug(1, "Local activemap cleared.");
776 reqlog(LOG_DEBUG, 2, -1, hio, "disk: (%p) Got request: ", hio);
778 /* Handle the actual request. */
779 switch (hio->hio_cmd) {
781 ret = pread(res->hr_localfd, hio->hio_data,
783 hio->hio_offset + res->hr_localoff);
785 hio->hio_error = errno;
786 else if (ret != (int64_t)hio->hio_length)
787 hio->hio_error = EIO;
792 ret = pwrite(res->hr_localfd, hio->hio_data,
794 hio->hio_offset + res->hr_localoff);
796 hio->hio_error = errno;
797 else if (ret != (int64_t)hio->hio_length)
798 hio->hio_error = EIO;
803 ret = g_delete(res->hr_localfd,
804 hio->hio_offset + res->hr_localoff,
807 hio->hio_error = errno;
812 if (!res->hr_localflush) {
814 hio->hio_error = EOPNOTSUPP;
818 ret = g_flush(res->hr_localfd);
820 if (errno == EOPNOTSUPP)
821 res->hr_localflush = false;
822 hio->hio_error = errno;
828 PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
831 if (logerror && hio->hio_error != 0) {
832 reqlog(LOG_ERR, 0, hio->hio_error, hio,
835 pjdlog_debug(2, "disk: (%p) Moving request to the send queue.",
837 QUEUE_INSERT(send, hio);
844 * Thread sends requests back to primary node.
847 send_thread(void *arg)
849 struct hast_resource *res = arg;
856 pjdlog_debug(2, "send: Taking request.");
857 QUEUE_TAKE(send, hio);
858 reqlog(LOG_DEBUG, 2, -1, hio, "send: (%p) Got request: ", hio);
860 /* Copy sequence number. */
861 nv_add_uint64(nvout, hio->hio_seq, "seq");
862 if (hio->hio_memsync) {
863 PJDLOG_ASSERT(hio->hio_cmd == HIO_WRITE);
864 nv_add_int8(nvout, 1, "received");
866 switch (hio->hio_cmd) {
868 if (hio->hio_error == 0) {
869 data = hio->hio_data;
870 length = hio->hio_length;
874 * We send no data in case of an error.
884 PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
887 if (hio->hio_error != 0) {
888 switch (hio->hio_cmd) {
890 res->hr_stat_read_error++;
893 res->hr_stat_write_error++;
896 res->hr_stat_delete_error++;
899 res->hr_stat_flush_error++;
902 nv_add_int16(nvout, hio->hio_error, "error");
904 if (hast_proto_send(res, res->hr_remoteout, nvout, data,
906 secondary_exit(EX_TEMPFAIL, "Unable to send reply");
909 pjdlog_debug(2, "send: (%p) Moving request to the free queue.",
912 QUEUE_INSERT(free, hio);