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/param.h>
40 #include <activemap.h>
43 #include "hast_proto.h"
50 /* Path to configuration file. */
51 static const char *cfgpath = HAST_CONFIG;
52 /* Hastd configuration. */
53 static struct hastd_config *cfg;
54 /* Control connection. */
55 static struct proto_conn *controlconn;
71 "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n"
72 "\t\t[-m mediasize] name ...\n",
75 " %s role [-d] [-c config] <init | primary | secondary> all | name ...\n",
78 " %s list [-d] [-c config] [all | name ...]\n",
81 " %s status [-d] [-c config] [all | name ...]\n",
84 " %s dump [-d] [-c config] [all | name ...]\n",
90 create_one(struct hast_resource *res, intmax_t mediasize, intmax_t extentsize,
98 pjdlog_prefix_set("[%s] ", res->hr_name);
100 if (provinfo(res, true) == -1) {
105 mediasize = res->hr_local_mediasize;
106 else if (mediasize > res->hr_local_mediasize) {
107 pjdlog_error("Provided mediasize is larger than provider %s size.",
112 if (!powerof2(res->hr_local_sectorsize)) {
113 pjdlog_error("Sector size of provider %s is not power of 2 (%u).",
114 res->hr_localpath, res->hr_local_sectorsize);
119 extentsize = HAST_EXTENTSIZE;
120 if (extentsize < res->hr_local_sectorsize) {
121 pjdlog_error("Extent size (%jd) is less than sector size (%u).",
122 (intmax_t)extentsize, res->hr_local_sectorsize);
126 if ((extentsize % res->hr_local_sectorsize) != 0) {
127 pjdlog_error("Extent size (%jd) is not multiple of sector size (%u).",
128 (intmax_t)extentsize, res->hr_local_sectorsize);
132 mapsize = activemap_calc_ondisk_size(mediasize - METADATA_SIZE,
133 extentsize, res->hr_local_sectorsize);
135 keepdirty = HAST_KEEPDIRTY;
136 res->hr_datasize = mediasize - METADATA_SIZE - mapsize;
137 res->hr_extentsize = extentsize;
138 res->hr_keepdirty = keepdirty;
140 res->hr_localoff = METADATA_SIZE + mapsize;
142 if (metadata_write(res) == -1) {
146 buf = calloc(1, mapsize);
148 pjdlog_error("Unable to allocate %zu bytes of memory for initial bitmap.",
153 if (pwrite(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
155 pjdlog_errno(LOG_ERR, "Unable to store initial bitmap on %s",
163 if (res->hr_localfd >= 0)
164 close(res->hr_localfd);
165 pjdlog_prefix_set("%s", "");
170 control_create(int argc, char *argv[], intmax_t mediasize, intmax_t extentsize,
173 struct hast_resource *res;
176 /* Initialize the given resources. */
180 for (ii = 0; ii < argc; ii++) {
181 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
182 if (strcmp(argv[ii], res->hr_name) == 0)
186 pjdlog_error("Unknown resource %s.", argv[ii]);
191 ret = create_one(res, mediasize, extentsize, keepdirty);
192 if (ret != 0 && ec == 0)
199 dump_one(struct hast_resource *res)
203 ret = metadata_read(res, false);
207 printf("resource: %s\n", res->hr_name);
208 printf(" datasize: %ju (%NB)\n", (uintmax_t)res->hr_datasize,
209 (intmax_t)res->hr_datasize);
210 printf(" extentsize: %d (%NB)\n", res->hr_extentsize,
211 (intmax_t)res->hr_extentsize);
212 printf(" keepdirty: %d\n", res->hr_keepdirty);
213 printf(" localoff: %ju\n", (uintmax_t)res->hr_localoff);
214 printf(" resuid: %ju\n", (uintmax_t)res->hr_resuid);
215 printf(" localcnt: %ju\n", (uintmax_t)res->hr_primary_localcnt);
216 printf(" remotecnt: %ju\n", (uintmax_t)res->hr_primary_remotecnt);
217 printf(" prevrole: %s\n", role2str(res->hr_previous_role));
223 control_dump(int argc, char *argv[])
225 struct hast_resource *res;
228 /* Dump metadata of the given resource(s). */
231 if (argc == 0 || (argc == 1 && strcmp(argv[0], "all") == 0)) {
232 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
234 if (ret != 0 && ec == 0)
240 for (ii = 0; ii < argc; ii++) {
241 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
242 if (strcmp(argv[ii], res->hr_name) == 0)
246 pjdlog_error("Unknown resource %s.", argv[ii]);
252 if (ret != 0 && ec == 0)
260 control_set_role(struct nv *nv, const char *newrole)
262 const char *res, *oldrole;
268 for (ii = 0; ; ii++) {
269 res = nv_get_string(nv, "resource%u", ii);
272 pjdlog_prefix_set("[%s] ", res);
273 error = nv_get_int16(nv, "error%u", ii);
277 pjdlog_warning("Received error %d from hastd.", error);
280 oldrole = nv_get_string(nv, "role%u", ii);
281 if (strcmp(oldrole, newrole) == 0)
282 pjdlog_debug(2, "Role unchanged (%s).", oldrole);
284 pjdlog_debug(1, "Role changed from %s to %s.", oldrole,
288 pjdlog_prefix_set("%s", "");
293 control_list(struct nv *nv)
302 for (ii = 0; ; ii++) {
303 str = nv_get_string(nv, "resource%u", ii);
306 printf("%s:\n", str);
307 error = nv_get_int16(nv, "error%u", ii);
311 printf(" error: %d\n", error);
314 printf(" role: %s\n", nv_get_string(nv, "role%u", ii));
315 printf(" provname: %s\n",
316 nv_get_string(nv, "provname%u", ii));
317 printf(" localpath: %s\n",
318 nv_get_string(nv, "localpath%u", ii));
319 printf(" extentsize: %u (%NB)\n",
320 (unsigned int)nv_get_uint32(nv, "extentsize%u", ii),
321 (intmax_t)nv_get_uint32(nv, "extentsize%u", ii));
322 printf(" keepdirty: %u\n",
323 (unsigned int)nv_get_uint32(nv, "keepdirty%u", ii));
324 printf(" remoteaddr: %s\n",
325 nv_get_string(nv, "remoteaddr%u", ii));
326 str = nv_get_string(nv, "sourceaddr%u", ii);
328 printf(" sourceaddr: %s\n", str);
329 printf(" replication: %s\n",
330 nv_get_string(nv, "replication%u", ii));
331 str = nv_get_string(nv, "status%u", ii);
333 printf(" status: %s\n", str);
334 pid = nv_get_int32(nv, "workerpid%u", ii);
336 printf(" workerpid: %d\n", pid);
337 printf(" dirty: %ju (%NB)\n",
338 (uintmax_t)nv_get_uint64(nv, "dirty%u", ii),
339 (intmax_t)nv_get_uint64(nv, "dirty%u", ii));
340 printf(" statistics:\n");
341 printf(" reads: %ju\n",
342 (uintmax_t)nv_get_uint64(nv, "stat_read%u", ii));
343 printf(" writes: %ju\n",
344 (uintmax_t)nv_get_uint64(nv, "stat_write%u", ii));
345 printf(" deletes: %ju\n",
346 (uintmax_t)nv_get_uint64(nv, "stat_delete%u", ii));
347 printf(" flushes: %ju\n",
348 (uintmax_t)nv_get_uint64(nv, "stat_flush%u", ii));
349 printf(" activemap updates: %ju\n",
350 (uintmax_t)nv_get_uint64(nv, "stat_activemap_update%u", ii));
351 printf(" local errors: "
352 "read: %ju, write: %ju, delete: %ju, flush: %ju\n",
353 (uintmax_t)nv_get_uint64(nv, "stat_read_error%u", ii),
354 (uintmax_t)nv_get_uint64(nv, "stat_write_error%u", ii),
355 (uintmax_t)nv_get_uint64(nv, "stat_delete_error%u", ii),
356 (uintmax_t)nv_get_uint64(nv, "stat_flush_error%u", ii));
358 "local: %ju, send: %ju, recv: %ju, done: %ju, idle: %ju\n",
359 (uintmax_t)nv_get_uint64(nv, "local_queue_size%u", ii),
360 (uintmax_t)nv_get_uint64(nv, "send_queue_size%u", ii),
361 (uintmax_t)nv_get_uint64(nv, "recv_queue_size%u", ii),
362 (uintmax_t)nv_get_uint64(nv, "done_queue_size%u", ii),
363 (uintmax_t)nv_get_uint64(nv, "idle_queue_size%u", ii));
369 control_status(struct nv *nv)
373 int error, hprinted, ret;
378 for (ii = 0; ; ii++) {
379 str = nv_get_string(nv, "resource%u", ii);
383 printf("Name\tStatus\t Role\t\tComponents\n");
387 error = nv_get_int16(nv, "error%u", ii);
391 printf("ERR%d\n", error);
394 str = nv_get_string(nv, "status%u", ii);
395 printf("%-9s", (str != NULL) ? str : "-");
396 printf("%-15s", nv_get_string(nv, "role%u", ii));
398 nv_get_string(nv, "localpath%u", ii));
400 nv_get_string(nv, "remoteaddr%u", ii));
406 main(int argc, char *argv[])
409 int64_t mediasize, extentsize, keepdirty;
410 int cmd, debug, error, ii;
414 mediasize = extentsize = keepdirty = 0;
419 if (strcmp(argv[1], "create") == 0) {
421 optstr = "c:de:k:m:h";
422 } else if (strcmp(argv[1], "role") == 0) {
425 } else if (strcmp(argv[1], "list") == 0) {
428 } else if (strcmp(argv[1], "status") == 0) {
431 } else if (strcmp(argv[1], "dump") == 0) {
443 ch = getopt(argc, argv, optstr);
454 if (expand_number(optarg, &extentsize) == -1)
455 errx(EX_USAGE, "Invalid extentsize");
458 if (expand_number(optarg, &keepdirty) == -1)
459 errx(EX_USAGE, "Invalid keepdirty");
462 if (expand_number(optarg, &mediasize) == -1)
463 errx(EX_USAGE, "Invalid mediasize");
481 pjdlog_init(PJDLOG_MODE_STD);
482 pjdlog_debug_set(debug);
484 cfg = yy_config_parse(cfgpath, true);
485 PJDLOG_ASSERT(cfg != NULL);
489 control_create(argc, argv, mediasize, extentsize, keepdirty);
491 PJDLOG_ABORT("What are we doing here?!");
494 /* Dump metadata from local component of the given resource. */
495 control_dump(argc, argv);
497 PJDLOG_ABORT("What are we doing here?!");
500 /* Change role for the given resources. */
504 nv_add_uint8(nv, HASTCTL_CMD_SETROLE, "cmd");
505 if (strcmp(argv[0], "init") == 0)
506 nv_add_uint8(nv, HAST_ROLE_INIT, "role");
507 else if (strcmp(argv[0], "primary") == 0)
508 nv_add_uint8(nv, HAST_ROLE_PRIMARY, "role");
509 else if (strcmp(argv[0], "secondary") == 0)
510 nv_add_uint8(nv, HAST_ROLE_SECONDARY, "role");
513 for (ii = 0; ii < argc - 1; ii++)
514 nv_add_string(nv, argv[ii + 1], "resource%d", ii);
518 /* Obtain status of the given resources. */
520 nv_add_uint8(nv, HASTCTL_CMD_STATUS, "cmd");
522 nv_add_string(nv, "all", "resource%d", 0);
524 for (ii = 0; ii < argc; ii++)
525 nv_add_string(nv, argv[ii], "resource%d", ii);
529 PJDLOG_ABORT("Impossible command!");
532 /* Setup control connection... */
533 if (proto_client(NULL, cfg->hc_controladdr, &controlconn) == -1) {
534 pjdlog_exit(EX_OSERR,
535 "Unable to setup control connection to %s",
536 cfg->hc_controladdr);
538 /* ...and connect to hastd. */
539 if (proto_connect(controlconn, HAST_TIMEOUT) == -1) {
540 pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s",
541 cfg->hc_controladdr);
544 if (drop_privs(NULL) != 0)
547 /* Send the command to the server... */
548 if (hast_proto_send(NULL, controlconn, nv, NULL, 0) == -1) {
549 pjdlog_exit(EX_UNAVAILABLE,
550 "Unable to send command to hastd via %s",
551 cfg->hc_controladdr);
554 /* ...and receive reply. */
555 if (hast_proto_recv_hdr(controlconn, &nv) == -1) {
556 pjdlog_exit(EX_UNAVAILABLE,
557 "cannot receive reply from hastd via %s",
558 cfg->hc_controladdr);
561 error = nv_get_int16(nv, "error");
563 pjdlog_exitx(EX_SOFTWARE, "Error %d received from hastd.",
570 error = control_set_role(nv, argv[0]);
573 error = control_list(nv);
576 error = control_status(nv);
579 PJDLOG_ABORT("Impossible command!");