2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
47 #include "hast_checksum.h"
48 #include "hast_compression.h"
49 #include "hast_proto.h"
59 child_cleanup(struct hast_resource *res)
62 proto_close(res->hr_ctrl);
64 if (res->hr_event != NULL) {
65 proto_close(res->hr_event);
68 if (res->hr_conn != NULL) {
69 proto_close(res->hr_conn);
72 res->hr_workerpid = 0;
76 control_set_role_common(struct hastd_config *cfg, struct nv *nvout,
77 uint8_t role, struct hast_resource *res, const char *name, unsigned int no)
81 /* Name is always needed. */
83 nv_add_string(nvout, name, "resource%u", no);
86 PJDLOG_ASSERT(cfg != NULL);
87 PJDLOG_ASSERT(name != NULL);
89 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
90 if (strcmp(res->hr_name, name) == 0)
94 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
98 PJDLOG_ASSERT(res != NULL);
100 /* Send previous role back. */
101 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
103 /* Nothing changed, return here. */
104 if (role == res->hr_role)
107 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
108 pjdlog_info("Role changed to %s.", role2str(role));
110 /* Change role to the new one. */
111 oldrole = res->hr_role;
113 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
116 * If previous role was primary or secondary we have to kill process
119 if (res->hr_workerpid != 0) {
120 if (kill(res->hr_workerpid, SIGTERM) == -1) {
121 pjdlog_errno(LOG_WARNING,
122 "Unable to kill worker process %u",
123 (unsigned int)res->hr_workerpid);
124 } else if (waitpid(res->hr_workerpid, NULL, 0) !=
126 pjdlog_errno(LOG_WARNING,
127 "Error while waiting for worker process %u",
128 (unsigned int)res->hr_workerpid);
130 pjdlog_debug(1, "Worker process %u stopped.",
131 (unsigned int)res->hr_workerpid);
136 /* Start worker process if we are changing to primary. */
137 if (role == HAST_ROLE_PRIMARY)
139 pjdlog_prefix_set("%s", "");
140 hook_exec(res->hr_exec, "role", res->hr_name, role2str(oldrole),
141 role2str(res->hr_role), NULL);
145 control_set_role(struct hast_resource *res, uint8_t role)
148 control_set_role_common(NULL, NULL, role, res, NULL, 0);
152 control_status_worker(struct hast_resource *res, struct nv *nvout,
155 struct nv *cnvin, *cnvout;
162 * Prepare and send command to worker process.
165 nv_add_uint8(cnvout, CONTROL_STATUS, "cmd");
166 error = nv_error(cnvout);
168 pjdlog_common(LOG_ERR, 0, error,
169 "Unable to prepare control header");
172 if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) == -1) {
174 pjdlog_errno(LOG_ERR, "Unable to send control header");
181 if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) == -1) {
183 pjdlog_errno(LOG_ERR, "Unable to receive control header");
187 error = nv_get_int16(cnvin, "error");
191 if ((str = nv_get_string(cnvin, "status")) == NULL) {
193 pjdlog_errno(LOG_ERR, "Field 'status' is missing.");
196 nv_add_string(nvout, str, "status%u", no);
197 nv_add_uint64(nvout, nv_get_uint64(cnvin, "dirty"), "dirty%u", no);
198 nv_add_uint32(nvout, nv_get_uint32(cnvin, "extentsize"),
200 nv_add_uint32(nvout, nv_get_uint32(cnvin, "keepdirty"),
202 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_read"),
204 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_write"),
206 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_delete"),
207 "stat_delete%u", no);
208 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_flush"),
210 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_activemap_update"),
211 "stat_activemap_update%u", no);
212 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_read_error"),
213 "stat_read_error%u", no);
214 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_write_error"),
215 "stat_write_error%u", no);
216 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_delete_error"),
217 "stat_delete_error%u", no);
218 nv_add_uint64(nvout, nv_get_uint64(cnvin, "stat_flush_error"),
219 "stat_flush_error%u", no);
220 nv_add_uint64(nvout, nv_get_uint64(cnvin, "idle_queue_size"),
221 "idle_queue_size%u", no);
222 nv_add_uint64(nvout, nv_get_uint64(cnvin, "local_queue_size"),
223 "local_queue_size%u", no);
224 nv_add_uint64(nvout, nv_get_uint64(cnvin, "send_queue_size"),
225 "send_queue_size%u", no);
226 nv_add_uint64(nvout, nv_get_uint64(cnvin, "recv_queue_size"),
227 "recv_queue_size%u", no);
228 nv_add_uint64(nvout, nv_get_uint64(cnvin, "done_queue_size"),
229 "done_queue_size%u", no);
236 nv_add_int16(nvout, error, "error");
240 control_status(struct hastd_config *cfg, struct nv *nvout,
241 struct hast_resource *res, const char *name, unsigned int no)
244 PJDLOG_ASSERT(cfg != NULL);
245 PJDLOG_ASSERT(nvout != NULL);
246 PJDLOG_ASSERT(name != NULL);
248 /* Name is always needed. */
249 nv_add_string(nvout, name, "resource%u", no);
252 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
253 if (strcmp(res->hr_name, name) == 0)
257 nv_add_int16(nvout, EHAST_NOENTRY, "error%u", no);
261 PJDLOG_ASSERT(res != NULL);
262 nv_add_string(nvout, res->hr_provname, "provname%u", no);
263 nv_add_string(nvout, res->hr_localpath, "localpath%u", no);
264 nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr%u", no);
265 if (res->hr_sourceaddr[0] != '\0')
266 nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr%u", no);
267 switch (res->hr_replication) {
268 case HAST_REPLICATION_FULLSYNC:
269 nv_add_string(nvout, "fullsync", "replication%u", no);
271 case HAST_REPLICATION_MEMSYNC:
272 nv_add_string(nvout, "memsync", "replication%u", no);
274 case HAST_REPLICATION_ASYNC:
275 nv_add_string(nvout, "async", "replication%u", no);
278 nv_add_string(nvout, "unknown", "replication%u", no);
281 nv_add_string(nvout, checksum_name(res->hr_checksum),
283 nv_add_string(nvout, compression_name(res->hr_compression),
284 "compression%u", no);
285 nv_add_string(nvout, role2str(res->hr_role), "role%u", no);
286 nv_add_int32(nvout, res->hr_workerpid, "workerpid%u", no);
288 switch (res->hr_role) {
289 case HAST_ROLE_PRIMARY:
290 PJDLOG_ASSERT(res->hr_workerpid != 0);
292 case HAST_ROLE_SECONDARY:
293 if (res->hr_workerpid != 0)
301 * If we are here, it means that we have a worker process, which we
302 * want to ask some questions.
304 control_status_worker(res, nvout, no);
308 control_handle(struct hastd_config *cfg)
310 struct proto_conn *conn;
311 struct nv *nvin, *nvout;
317 if (proto_accept(cfg->hc_controlconn, &conn) == -1) {
318 pjdlog_errno(LOG_ERR, "Unable to accept control connection");
322 cfg->hc_controlin = conn;
324 role = HAST_ROLE_UNDEF;
326 if (hast_proto_recv_hdr(conn, &nvin) == -1) {
327 pjdlog_errno(LOG_ERR, "Unable to receive control header");
332 /* Obtain command code. 0 means that nv_get_uint8() failed. */
333 cmd = nv_get_uint8(nvin, "cmd");
335 pjdlog_error("Control header is missing 'cmd' field.");
339 /* Allocate outgoing nv structure. */
342 pjdlog_error("Unable to allocate header for control response.");
348 str = nv_get_string(nvin, "resource0");
350 pjdlog_error("Control header is missing 'resource0' field.");
351 error = EHAST_INVALID;
354 if (cmd == HASTCTL_CMD_SETROLE) {
355 role = nv_get_uint8(nvin, "role");
358 case HAST_ROLE_PRIMARY:
359 case HAST_ROLE_SECONDARY:
362 pjdlog_error("Invalid role received (%hhu).", role);
363 error = EHAST_INVALID;
367 if (strcmp(str, "all") == 0) {
368 struct hast_resource *res;
370 /* All configured resources. */
373 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
375 case HASTCTL_CMD_SETROLE:
376 control_set_role_common(cfg, nvout, role, res,
379 case HASTCTL_CMD_STATUS:
380 control_status(cfg, nvout, res, res->hr_name,
384 pjdlog_error("Invalid command received (%hhu).",
386 error = EHAST_UNIMPLEMENTED;
391 /* Only selected resources. */
393 for (ii = 0; ; ii++) {
394 str = nv_get_string(nvin, "resource%u", ii);
398 case HASTCTL_CMD_SETROLE:
399 control_set_role_common(cfg, nvout, role, NULL,
402 case HASTCTL_CMD_STATUS:
403 control_status(cfg, nvout, NULL, str, ii);
406 pjdlog_error("Invalid command received (%hhu).",
408 error = EHAST_UNIMPLEMENTED;
413 if (nv_error(nvout) != 0)
417 nv_add_int16(nvout, error, "error");
419 if (hast_proto_send(NULL, conn, nvout, NULL, 0) == -1)
420 pjdlog_errno(LOG_ERR, "Unable to send control response");
427 cfg->hc_controlin = NULL;
431 * Thread handles control requests from the parent.
434 ctrl_thread(void *arg)
436 struct hast_resource *res = arg;
437 struct nv *nvin, *nvout;
441 if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) == -1) {
442 if (sigexit_received)
444 pjdlog_errno(LOG_ERR,
445 "Unable to receive control message");
446 kill(getpid(), SIGTERM);
449 cmd = nv_get_uint8(nvin, "cmd");
451 pjdlog_error("Control message is missing 'cmd' field.");
458 if (res->hr_remotein != NULL &&
459 res->hr_remoteout != NULL) {
460 nv_add_string(nvout, "complete", "status");
462 nv_add_string(nvout, "degraded", "status");
464 nv_add_uint32(nvout, (uint32_t)res->hr_extentsize,
466 if (res->hr_role == HAST_ROLE_PRIMARY) {
468 (uint32_t)res->hr_keepdirty, "keepdirty");
470 (uint64_t)(activemap_ndirty(res->hr_amp) *
471 res->hr_extentsize), "dirty");
473 nv_add_uint32(nvout, (uint32_t)0, "keepdirty");
474 nv_add_uint64(nvout, (uint64_t)0, "dirty");
476 nv_add_uint64(nvout, res->hr_stat_read, "stat_read");
477 nv_add_uint64(nvout, res->hr_stat_write, "stat_write");
478 nv_add_uint64(nvout, res->hr_stat_delete,
480 nv_add_uint64(nvout, res->hr_stat_flush, "stat_flush");
481 nv_add_uint64(nvout, res->hr_stat_activemap_update,
482 "stat_activemap_update");
483 nv_add_uint64(nvout, res->hr_stat_read_error,
485 nv_add_uint64(nvout, res->hr_stat_write_error +
486 res->hr_stat_activemap_write_error,
488 nv_add_uint64(nvout, res->hr_stat_delete_error,
489 "stat_delete_error");
490 nv_add_uint64(nvout, res->hr_stat_flush_error +
491 res->hr_stat_activemap_flush_error,
493 res->output_status_aux(nvout);
494 nv_add_int16(nvout, 0, "error");
498 * When parent receives SIGHUP and discovers that
499 * something related to us has changes, it sends reload
502 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
503 primary_config_reload(res, nvin);
504 nv_add_int16(nvout, 0, "error");
507 nv_add_int16(nvout, EINVAL, "error");
511 if (nv_error(nvout) != 0) {
512 pjdlog_error("Unable to create answer on control message.");
516 if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) == -1) {
517 pjdlog_errno(LOG_ERR,
518 "Unable to send reply to control message");