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/param.h>
43 #include <activemap.h>
46 #include "hast_proto.h"
53 /* Path to configuration file. */
54 static const char *cfgpath = HAST_CONFIG;
55 /* Hastd configuration. */
56 static struct hastd_config *cfg;
57 /* Control connection. */
58 static struct proto_conn *controlconn;
74 "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n"
75 "\t\t[-m mediasize] name ...\n",
78 " %s role [-d] [-c config] <init | primary | secondary> all | name ...\n",
81 " %s list [-d] [-c config] [all | name ...]\n",
84 " %s status [-d] [-c config] [all | name ...]\n",
87 " %s dump [-d] [-c config] [all | name ...]\n",
93 create_one(struct hast_resource *res, intmax_t mediasize, intmax_t extentsize,
101 pjdlog_prefix_set("[%s] ", res->hr_name);
103 if (provinfo(res, true) == -1) {
108 mediasize = res->hr_local_mediasize;
109 else if (mediasize > res->hr_local_mediasize) {
110 pjdlog_error("Provided mediasize is larger than provider %s size.",
115 if (!powerof2(res->hr_local_sectorsize)) {
116 pjdlog_error("Sector size of provider %s is not power of 2 (%u).",
117 res->hr_localpath, res->hr_local_sectorsize);
122 extentsize = HAST_EXTENTSIZE;
123 if (extentsize < res->hr_local_sectorsize) {
124 pjdlog_error("Extent size (%jd) is less than sector size (%u).",
125 (intmax_t)extentsize, res->hr_local_sectorsize);
129 if ((extentsize % res->hr_local_sectorsize) != 0) {
130 pjdlog_error("Extent size (%jd) is not multiple of sector size (%u).",
131 (intmax_t)extentsize, res->hr_local_sectorsize);
135 mapsize = activemap_calc_ondisk_size(mediasize - METADATA_SIZE,
136 extentsize, res->hr_local_sectorsize);
138 keepdirty = HAST_KEEPDIRTY;
139 res->hr_datasize = mediasize - METADATA_SIZE - mapsize;
140 res->hr_extentsize = extentsize;
141 res->hr_keepdirty = keepdirty;
143 res->hr_localoff = METADATA_SIZE + mapsize;
145 if (metadata_write(res) == -1) {
149 buf = calloc(1, mapsize);
151 pjdlog_error("Unable to allocate %zu bytes of memory for initial bitmap.",
156 if (pwrite(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
158 pjdlog_errno(LOG_ERR, "Unable to store initial bitmap on %s",
166 if (res->hr_localfd >= 0)
167 close(res->hr_localfd);
168 pjdlog_prefix_set("%s", "");
173 control_create(int argc, char *argv[], intmax_t mediasize, intmax_t extentsize,
176 struct hast_resource *res;
179 /* Initialize the given resources. */
183 for (ii = 0; ii < argc; ii++) {
184 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
185 if (strcmp(argv[ii], res->hr_name) == 0)
189 pjdlog_error("Unknown resource %s.", argv[ii]);
194 ret = create_one(res, mediasize, extentsize, keepdirty);
195 if (ret != 0 && ec == 0)
202 dump_one(struct hast_resource *res)
206 ret = metadata_read(res, false);
210 printf("resource: %s\n", res->hr_name);
211 printf(" datasize: %ju (%NB)\n", (uintmax_t)res->hr_datasize,
212 (intmax_t)res->hr_datasize);
213 printf(" extentsize: %d (%NB)\n", res->hr_extentsize,
214 (intmax_t)res->hr_extentsize);
215 printf(" keepdirty: %d\n", res->hr_keepdirty);
216 printf(" localoff: %ju\n", (uintmax_t)res->hr_localoff);
217 printf(" resuid: %ju\n", (uintmax_t)res->hr_resuid);
218 printf(" localcnt: %ju\n", (uintmax_t)res->hr_primary_localcnt);
219 printf(" remotecnt: %ju\n", (uintmax_t)res->hr_primary_remotecnt);
220 printf(" prevrole: %s\n", role2str(res->hr_previous_role));
226 control_dump(int argc, char *argv[])
228 struct hast_resource *res;
231 /* Dump metadata of the given resource(s). */
234 if (argc == 0 || (argc == 1 && strcmp(argv[0], "all") == 0)) {
235 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
237 if (ret != 0 && ec == 0)
243 for (ii = 0; ii < argc; ii++) {
244 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
245 if (strcmp(argv[ii], res->hr_name) == 0)
249 pjdlog_error("Unknown resource %s.", argv[ii]);
255 if (ret != 0 && ec == 0)
263 control_set_role(struct nv *nv, const char *newrole)
265 const char *res, *oldrole;
271 for (ii = 0; ; ii++) {
272 res = nv_get_string(nv, "resource%u", ii);
275 pjdlog_prefix_set("[%s] ", res);
276 error = nv_get_int16(nv, "error%u", ii);
280 pjdlog_warning("Received error %d from hastd.", error);
283 oldrole = nv_get_string(nv, "role%u", ii);
284 if (strcmp(oldrole, newrole) == 0)
285 pjdlog_debug(2, "Role unchanged (%s).", oldrole);
287 pjdlog_debug(1, "Role changed from %s to %s.", oldrole,
291 pjdlog_prefix_set("%s", "");
296 control_list(struct nv *nv)
305 for (ii = 0; ; ii++) {
306 str = nv_get_string(nv, "resource%u", ii);
309 printf("%s:\n", str);
310 error = nv_get_int16(nv, "error%u", ii);
314 printf(" error: %d\n", error);
317 printf(" role: %s\n", nv_get_string(nv, "role%u", ii));
318 printf(" provname: %s\n",
319 nv_get_string(nv, "provname%u", ii));
320 printf(" localpath: %s\n",
321 nv_get_string(nv, "localpath%u", ii));
322 printf(" extentsize: %u (%NB)\n",
323 (unsigned int)nv_get_uint32(nv, "extentsize%u", ii),
324 (intmax_t)nv_get_uint32(nv, "extentsize%u", ii));
325 printf(" keepdirty: %u\n",
326 (unsigned int)nv_get_uint32(nv, "keepdirty%u", ii));
327 printf(" remoteaddr: %s\n",
328 nv_get_string(nv, "remoteaddr%u", ii));
329 str = nv_get_string(nv, "sourceaddr%u", ii);
331 printf(" sourceaddr: %s\n", str);
332 printf(" replication: %s\n",
333 nv_get_string(nv, "replication%u", ii));
334 str = nv_get_string(nv, "status%u", ii);
336 printf(" status: %s\n", str);
337 pid = nv_get_int32(nv, "workerpid%u", ii);
339 printf(" workerpid: %d\n", pid);
340 printf(" dirty: %ju (%NB)\n",
341 (uintmax_t)nv_get_uint64(nv, "dirty%u", ii),
342 (intmax_t)nv_get_uint64(nv, "dirty%u", ii));
343 printf(" statistics:\n");
344 printf(" reads: %ju\n",
345 (uintmax_t)nv_get_uint64(nv, "stat_read%u", ii));
346 printf(" writes: %ju\n",
347 (uintmax_t)nv_get_uint64(nv, "stat_write%u", ii));
348 printf(" deletes: %ju\n",
349 (uintmax_t)nv_get_uint64(nv, "stat_delete%u", ii));
350 printf(" flushes: %ju\n",
351 (uintmax_t)nv_get_uint64(nv, "stat_flush%u", ii));
352 printf(" activemap updates: %ju\n",
353 (uintmax_t)nv_get_uint64(nv, "stat_activemap_update%u", ii));
354 printf(" local errors: "
355 "read: %ju, write: %ju, delete: %ju, flush: %ju\n",
356 (uintmax_t)nv_get_uint64(nv, "stat_read_error%u", ii),
357 (uintmax_t)nv_get_uint64(nv, "stat_write_error%u", ii),
358 (uintmax_t)nv_get_uint64(nv, "stat_delete_error%u", ii),
359 (uintmax_t)nv_get_uint64(nv, "stat_flush_error%u", ii));
361 "local: %ju, send: %ju, recv: %ju, done: %ju, idle: %ju\n",
362 (uintmax_t)nv_get_uint64(nv, "local_queue_size%u", ii),
363 (uintmax_t)nv_get_uint64(nv, "send_queue_size%u", ii),
364 (uintmax_t)nv_get_uint64(nv, "recv_queue_size%u", ii),
365 (uintmax_t)nv_get_uint64(nv, "done_queue_size%u", ii),
366 (uintmax_t)nv_get_uint64(nv, "idle_queue_size%u", ii));
372 control_status(struct nv *nv)
376 int error, hprinted, ret;
381 for (ii = 0; ; ii++) {
382 str = nv_get_string(nv, "resource%u", ii);
386 printf("Name\tStatus\t Role\t\tComponents\n");
390 error = nv_get_int16(nv, "error%u", ii);
394 printf("ERR%d\n", error);
397 str = nv_get_string(nv, "status%u", ii);
398 printf("%-9s", (str != NULL) ? str : "-");
399 printf("%-15s", nv_get_string(nv, "role%u", ii));
401 nv_get_string(nv, "localpath%u", ii));
403 nv_get_string(nv, "remoteaddr%u", ii));
409 main(int argc, char *argv[])
412 int64_t mediasize, extentsize, keepdirty;
413 int cmd, debug, error, ii;
417 mediasize = extentsize = keepdirty = 0;
422 if (strcmp(argv[1], "create") == 0) {
424 optstr = "c:de:k:m:h";
425 } else if (strcmp(argv[1], "role") == 0) {
428 } else if (strcmp(argv[1], "list") == 0) {
431 } else if (strcmp(argv[1], "status") == 0) {
434 } else if (strcmp(argv[1], "dump") == 0) {
446 ch = getopt(argc, argv, optstr);
457 if (expand_number(optarg, &extentsize) == -1)
458 errx(EX_USAGE, "Invalid extentsize");
461 if (expand_number(optarg, &keepdirty) == -1)
462 errx(EX_USAGE, "Invalid keepdirty");
465 if (expand_number(optarg, &mediasize) == -1)
466 errx(EX_USAGE, "Invalid mediasize");
484 pjdlog_init(PJDLOG_MODE_STD);
485 pjdlog_debug_set(debug);
487 cfg = yy_config_parse(cfgpath, true);
488 PJDLOG_ASSERT(cfg != NULL);
492 control_create(argc, argv, mediasize, extentsize, keepdirty);
494 PJDLOG_ABORT("What are we doing here?!");
497 /* Dump metadata from local component of the given resource. */
498 control_dump(argc, argv);
500 PJDLOG_ABORT("What are we doing here?!");
503 /* Change role for the given resources. */
507 nv_add_uint8(nv, HASTCTL_CMD_SETROLE, "cmd");
508 if (strcmp(argv[0], "init") == 0)
509 nv_add_uint8(nv, HAST_ROLE_INIT, "role");
510 else if (strcmp(argv[0], "primary") == 0)
511 nv_add_uint8(nv, HAST_ROLE_PRIMARY, "role");
512 else if (strcmp(argv[0], "secondary") == 0)
513 nv_add_uint8(nv, HAST_ROLE_SECONDARY, "role");
516 for (ii = 0; ii < argc - 1; ii++)
517 nv_add_string(nv, argv[ii + 1], "resource%d", ii);
521 /* Obtain status of the given resources. */
523 nv_add_uint8(nv, HASTCTL_CMD_STATUS, "cmd");
525 nv_add_string(nv, "all", "resource%d", 0);
527 for (ii = 0; ii < argc; ii++)
528 nv_add_string(nv, argv[ii], "resource%d", ii);
532 PJDLOG_ABORT("Impossible command!");
535 /* Setup control connection... */
536 if (proto_client(NULL, cfg->hc_controladdr, &controlconn) == -1) {
537 pjdlog_exit(EX_OSERR,
538 "Unable to setup control connection to %s",
539 cfg->hc_controladdr);
541 /* ...and connect to hastd. */
542 if (proto_connect(controlconn, HAST_TIMEOUT) == -1) {
543 pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s",
544 cfg->hc_controladdr);
547 if (drop_privs(NULL) != 0)
550 /* Send the command to the server... */
551 if (hast_proto_send(NULL, controlconn, nv, NULL, 0) == -1) {
552 pjdlog_exit(EX_UNAVAILABLE,
553 "Unable to send command to hastd via %s",
554 cfg->hc_controladdr);
557 /* ...and receive reply. */
558 if (hast_proto_recv_hdr(controlconn, &nv) == -1) {
559 pjdlog_exit(EX_UNAVAILABLE,
560 "cannot receive reply from hastd via %s",
561 cfg->hc_controladdr);
564 error = nv_get_int16(nv, "error");
566 pjdlog_exitx(EX_SOFTWARE, "Error %d received from hastd.",
573 error = control_set_role(nv, argv[0]);
576 error = control_list(nv);
579 error = control_status(nv);
582 PJDLOG_ABORT("Impossible command!");