2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2009-2010 The FreeBSD Foundation
7 * This software was developed by Pawel Jakub Dawidek under sponsorship from
8 * the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
44 #include "hast_checksum.h"
45 #include "hast_compression.h"
46 #include "hast_proto.h"
56 child_cleanup(struct hast_resource *res)
59 proto_close(res->hr_ctrl);
61 if (res->hr_event != NULL) {
62 proto_close(res->hr_event);
65 if (res->hr_conn != NULL) {
66 proto_close(res->hr_conn);
69 res->hr_workerpid = 0;
73 control_set_role_common(struct hastd_config *cfg, struct nv *nvout,
74 uint8_t role, struct hast_resource *res, const char *name, unsigned int no)
78 /* Name is always needed. */
80 nv_add_string(nvout, name, "resource%u", no);
83 PJDLOG_ASSERT(cfg != NULL);
84 PJDLOG_ASSERT(name != NULL);
86 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
87 if (strcmp(res->hr_name, name) == 0)
91 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
95 PJDLOG_ASSERT(res != NULL);
97 /* Send previous role back. */
98 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
100 /* Nothing changed, return here. */
101 if (role == res->hr_role)
104 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
105 pjdlog_info("Role changed to %s.", role2str(role));
107 /* Change role to the new one. */
108 oldrole = res->hr_role;
110 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
113 * If previous role was primary or secondary we have to kill process
116 if (res->hr_workerpid != 0) {
117 if (kill(res->hr_workerpid, SIGTERM) == -1) {
118 pjdlog_errno(LOG_WARNING,
119 "Unable to kill worker process %u",
120 (unsigned int)res->hr_workerpid);
121 } else if (waitpid(res->hr_workerpid, NULL, 0) !=
123 pjdlog_errno(LOG_WARNING,
124 "Error while waiting for worker process %u",
125 (unsigned int)res->hr_workerpid);
127 pjdlog_debug(1, "Worker process %u stopped.",
128 (unsigned int)res->hr_workerpid);
133 /* Start worker process if we are changing to primary. */
134 if (role == HAST_ROLE_PRIMARY)
136 pjdlog_prefix_set("%s", "");
137 hook_exec(res->hr_exec, "role", res->hr_name, role2str(oldrole),
138 role2str(res->hr_role), NULL);
142 control_set_role(struct hast_resource *res, uint8_t role)
145 control_set_role_common(NULL, NULL, role, res, NULL, 0);
149 control_status_worker(struct hast_resource *res, struct nv *nvout,
152 struct nv *cnvin, *cnvout;
159 * Prepare and send command to worker process.
162 nv_add_uint8(cnvout, CONTROL_STATUS, "cmd");
163 error = nv_error(cnvout);
165 pjdlog_common(LOG_ERR, 0, error,
166 "Unable to prepare control header");
169 if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) == -1) {
171 pjdlog_errno(LOG_ERR, "Unable to send control header");
178 if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) == -1) {
180 pjdlog_errno(LOG_ERR, "Unable to receive control header");
184 error = nv_get_int16(cnvin, "error");
188 if ((str = nv_get_string(cnvin, "status")) == NULL) {
190 pjdlog_errno(LOG_ERR, "Field 'status' is missing.");
193 nv_add_string(nvout, str, "status%u", no);
194 nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no);
195 nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"),
197 nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"),
199 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_read"),
201 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_write"),
203 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_delete"),
204 "stat_delete%u", no);
205 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_flush"),
207 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_activemap_update"),
208 "stat_activemap_update%u", no);
209 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_read_error"),
210 "stat_read_error%u", no);
211 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_write_error"),
212 "stat_write_error%u", no);
213 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_delete_error"),
214 "stat_delete_error%u", no);
215 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_flush_error"),
216 "stat_flush_error%u", no);
217 nv_add_uint64(nvout, nv_get_uint64(cnvin, "idle_queue_size"),
218 "idle_queue_size%u", no);
219 nv_add_uint64(nvout, nv_get_uint64(cnvin, "local_queue_size"),
220 "local_queue_size%u", no);
221 nv_add_uint64(nvout, nv_get_uint64(cnvin, "send_queue_size"),
222 "send_queue_size%u", no);
223 nv_add_uint64(nvout, nv_get_uint64(cnvin, "recv_queue_size"),
224 "recv_queue_size%u", no);
225 nv_add_uint64(nvout, nv_get_uint64(cnvin, "done_queue_size"),
226 "done_queue_size%u", no);
233 nv_add_int16(nvout, error, "error");
237 control_status(struct hastd_config *cfg, struct nv *nvout,
238 struct hast_resource *res, const char *name, unsigned int no)
241 PJDLOG_ASSERT(cfg != NULL);
242 PJDLOG_ASSERT(nvout != NULL);
243 PJDLOG_ASSERT(name != NULL);
245 /* Name is always needed. */
246 nv_add_string(nvout, name, "resource%u", no);
249 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
250 if (strcmp(res->hr_name, name) == 0)
254 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
258 PJDLOG_ASSERT(res != NULL);
259 nv_add_string(nvout, res->hr_provname, "provname%u", no);
260 nv_add_string(nvout, res->hr_localpath, "localpath%u", no);
261 nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no);
262 if (res->hr_sourceaddr[0] != '\0')
263 nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr%u", no);
264 switch (res->hr_replication) {
265 case HAST_REPLICATION_FULLSYNC:
266 nv_add_string(nvout, "fullsync", "replication%u", no);
268 case HAST_REPLICATION_MEMSYNC:
269 nv_add_string(nvout, "memsync", "replication%u", no);
271 case HAST_REPLICATION_ASYNC:
272 nv_add_string(nvout, "async", "replication%u", no);
275 nv_add_string(nvout, "unknown", "replication%u", no);
278 nv_add_string(nvout, checksum_name(res->hr_checksum),
280 nv_add_string(nvout, compression_name(res->hr_compression),
281 "compression%u", no);
282 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
283 nv_add_int32(nvout, res->hr_workerpid, "workerpid%u", no);
285 switch (res->hr_role) {
286 case HAST_ROLE_PRIMARY:
287 PJDLOG_ASSERT(res->hr_workerpid != 0);
289 case HAST_ROLE_SECONDARY:
290 if (res->hr_workerpid != 0)
298 * If we are here, it means that we have a worker process, which we
299 * want to ask some questions.
301 control_status_worker(res, nvout, no);
305 control_handle(struct hastd_config *cfg)
307 struct proto_conn *conn;
308 struct nv *nvin, *nvout;
314 if (proto_accept(cfg->hc_controlconn, &conn) == -1) {
315 pjdlog_errno(LOG_ERR, "Unable to accept control connection");
319 cfg->hc_controlin = conn;
321 role = HAST_ROLE_UNDEF;
323 if (hast_proto_recv_hdr(conn, &nvin) == -1) {
324 pjdlog_errno(LOG_ERR, "Unable to receive control header");
329 /* Obtain command code. 0 means that nv_get_uint8() failed. */
330 cmd = nv_get_uint8(nvin, "cmd");
332 pjdlog_error("Control header is missing 'cmd' field.");
336 /* Allocate outgoing nv structure. */
339 pjdlog_error("Unable to allocate header for control response.");
345 str = nv_get_string(nvin, "resource0");
347 pjdlog_error("Control header is missing 'resource0' field.");
348 error = EHAST_INVALID;
351 if (cmd == HASTCTL_CMD_SETROLE) {
352 role = nv_get_uint8(nvin, "role");
355 case HAST_ROLE_PRIMARY:
356 case HAST_ROLE_SECONDARY:
359 pjdlog_error("Invalid role received (%hhu).", role);
360 error = EHAST_INVALID;
364 if (strcmp(str, "all") == 0) {
365 struct hast_resource *res;
367 /* All configured resources. */
370 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
372 case HASTCTL_CMD_SETROLE:
373 control_set_role_common(cfg, nvout, role, res,
376 case HASTCTL_CMD_STATUS:
377 control_status(cfg, nvout, res, res->hr_name,
381 pjdlog_error("Invalid command received (%hhu).",
383 error = EHAST_UNIMPLEMENTED;
388 /* Only selected resources. */
390 for (ii = 0; ; ii++) {
391 str = nv_get_string(nvin, "resource%u", ii);
395 case HASTCTL_CMD_SETROLE:
396 control_set_role_common(cfg, nvout, role, NULL,
399 case HASTCTL_CMD_STATUS:
400 control_status(cfg, nvout, NULL, str, ii);
403 pjdlog_error("Invalid command received (%hhu).",
405 error = EHAST_UNIMPLEMENTED;
410 if (nv_error(nvout) != 0)
414 nv_add_int16(nvout, error, "error");
416 if (hast_proto_send(NULL, conn, nvout, NULL, 0) == -1)
417 pjdlog_errno(LOG_ERR, "Unable to send control response");
424 cfg->hc_controlin = NULL;
428 * Thread handles control requests from the parent.
431 ctrl_thread(void *arg)
433 struct hast_resource *res = arg;
434 struct nv *nvin, *nvout;
438 if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) == -1) {
439 if (sigexit_received)
441 pjdlog_errno(LOG_ERR,
442 "Unable to receive control message");
443 kill(getpid(), SIGTERM);
446 cmd = nv_get_uint8(nvin, "cmd");
448 pjdlog_error("Control message is missing 'cmd' field.");
455 if (res->hr_remotein != NULL &&
456 res->hr_remoteout != NULL) {
457 nv_add_string(nvout, "complete", "status");
459 nv_add_string(nvout, "degraded", "status");
461 nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
463 if (res->hr_role == HAST_ROLE_PRIMARY) {
465 (uint32_t)res->hr_keepdirty, "keepdirty");
467 (uint64_t)(activemap_ndirty(res->hr_amp) *
468 res->hr_extentsize), "dirty");
470 nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
471 nv_add_uint64(nvout, (uint64_t)0, "dirty");
473 nv_add_uint64(nvout, res->hr_stat_read, "stat_read");
474 nv_add_uint64(nvout, res->hr_stat_write, "stat_write");
475 nv_add_uint64(nvout, res->hr_stat_delete,
477 nv_add_uint64(nvout, res->hr_stat_flush, "stat_flush");
478 nv_add_uint64(nvout, res->hr_stat_activemap_update,
479 "stat_activemap_update");
480 nv_add_uint64(nvout, res->hr_stat_read_error,
482 nv_add_uint64(nvout, res->hr_stat_write_error +
483 res->hr_stat_activemap_write_error,
485 nv_add_uint64(nvout, res->hr_stat_delete_error,
486 "stat_delete_error");
487 nv_add_uint64(nvout, res->hr_stat_flush_error +
488 res->hr_stat_activemap_flush_error,
490 res->output_status_aux(nvout);
491 nv_add_int16(nvout, 0, "error");
495 * When parent receives SIGHUP and discovers that
496 * something related to us has changes, it sends reload
499 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
500 primary_config_reload(res, nvin);
501 nv_add_int16(nvout, 0, "error");
504 nv_add_int16(nvout, EINVAL, "error");
508 if (nv_error(nvout) != 0) {
509 pjdlog_error("Unable to create answer on control message.");
513 if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) == -1) {
514 pjdlog_errno(LOG_ERR,
515 "Unable to send reply to control message");