2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/ioctl.h>
37 #include <sys/sysctl.h>
51 #include <activemap.h>
54 #include "hast_proto.h"
61 /* Path to configuration file. */
62 static const char *cfgpath = HAST_CONFIG;
63 /* Hastd configuration. */
64 static struct hastd_config *cfg;
65 /* Control connection. */
66 static struct proto_conn *controlconn;
81 "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n"
82 "\t\t[-m mediasize] name ...\n",
85 " %s role [-d] [-c config] <init | primary | secondary> all | name ...\n",
88 " %s status [-d] [-c config] [all | name ...]\n",
91 " %s dump [-d] [-c config] [all | name ...]\n",
97 create_one(struct hast_resource *res, intmax_t mediasize, intmax_t extentsize,
105 pjdlog_prefix_set("[%s] ", res->hr_name);
107 if (provinfo(res, true) == -1) {
112 mediasize = res->hr_local_mediasize;
113 else if (mediasize > res->hr_local_mediasize) {
114 pjdlog_error("Provided mediasize is larger than provider %s size.",
119 if (!powerof2(res->hr_local_sectorsize)) {
120 pjdlog_error("Sector size of provider %s is not power of 2 (%u).",
121 res->hr_localpath, res->hr_local_sectorsize);
126 extentsize = HAST_EXTENTSIZE;
127 if (extentsize < res->hr_local_sectorsize) {
128 pjdlog_error("Extent size (%jd) is less than sector size (%u).",
129 (intmax_t)extentsize, res->hr_local_sectorsize);
133 if ((extentsize % res->hr_local_sectorsize) != 0) {
134 pjdlog_error("Extent size (%jd) is not multiple of sector size (%u).",
135 (intmax_t)extentsize, res->hr_local_sectorsize);
139 mapsize = activemap_calc_ondisk_size(mediasize - METADATA_SIZE,
140 extentsize, res->hr_local_sectorsize);
142 keepdirty = HAST_KEEPDIRTY;
143 res->hr_datasize = mediasize - METADATA_SIZE - mapsize;
144 res->hr_extentsize = extentsize;
145 res->hr_keepdirty = keepdirty;
147 res->hr_localoff = METADATA_SIZE + mapsize;
149 if (metadata_write(res) == -1) {
153 buf = calloc(1, mapsize);
155 pjdlog_error("Unable to allocate %zu bytes of memory for initial bitmap.",
160 if (pwrite(res->hr_localfd, buf, mapsize, METADATA_SIZE) !=
162 pjdlog_errno(LOG_ERR, "Unable to store initial bitmap on %s",
170 if (res->hr_localfd >= 0)
171 close(res->hr_localfd);
172 pjdlog_prefix_set("%s", "");
177 control_create(int argc, char *argv[], intmax_t mediasize, intmax_t extentsize,
180 struct hast_resource *res;
183 /* Initialize the given resources. */
187 for (ii = 0; ii < argc; ii++) {
188 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
189 if (strcmp(argv[ii], res->hr_name) == 0)
193 pjdlog_error("Unknown resource %s.", argv[ii]);
198 ret = create_one(res, mediasize, extentsize, keepdirty);
199 if (ret != 0 && ec == 0)
206 dump_one(struct hast_resource *res)
210 ret = metadata_read(res, false);
214 printf("resource: %s\n", res->hr_name);
215 printf(" datasize: %ju (%NB)\n", (uintmax_t)res->hr_datasize,
216 (intmax_t)res->hr_datasize);
217 printf(" extentsize: %d (%NB)\n", res->hr_extentsize,
218 (intmax_t)res->hr_extentsize);
219 printf(" keepdirty: %d\n", res->hr_keepdirty);
220 printf(" localoff: %ju\n", (uintmax_t)res->hr_localoff);
221 printf(" resuid: %ju\n", (uintmax_t)res->hr_resuid);
222 printf(" localcnt: %ju\n", (uintmax_t)res->hr_primary_localcnt);
223 printf(" remotecnt: %ju\n", (uintmax_t)res->hr_primary_remotecnt);
224 printf(" prevrole: %s\n", role2str(res->hr_previous_role));
230 control_dump(int argc, char *argv[])
232 struct hast_resource *res;
235 /* Dump metadata of the given resource(s). */
238 if (argc == 0 || (argc == 1 && strcmp(argv[0], "all") == 0)) {
239 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
241 if (ret != 0 && ec == 0)
247 for (ii = 0; ii < argc; ii++) {
248 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
249 if (strcmp(argv[ii], res->hr_name) == 0)
253 pjdlog_error("Unknown resource %s.", argv[ii]);
259 if (ret != 0 && ec == 0)
267 control_set_role(struct nv *nv, const char *newrole)
269 const char *res, *oldrole;
275 for (ii = 0; ; ii++) {
276 res = nv_get_string(nv, "resource%u", ii);
279 pjdlog_prefix_set("[%s] ", res);
280 error = nv_get_int16(nv, "error%u", ii);
284 pjdlog_warning("Received error %d from hastd.", error);
287 oldrole = nv_get_string(nv, "role%u", ii);
288 if (strcmp(oldrole, newrole) == 0)
289 pjdlog_debug(2, "Role unchanged (%s).", oldrole);
291 pjdlog_debug(1, "Role changed from %s to %s.", oldrole,
295 pjdlog_prefix_set("%s", "");
300 control_status(struct nv *nv)
308 for (ii = 0; ; ii++) {
309 str = nv_get_string(nv, "resource%u", ii);
312 printf("%s:\n", str);
313 error = nv_get_int16(nv, "error%u", ii);
317 printf(" error: %d\n", error);
320 printf(" role: %s\n", nv_get_string(nv, "role%u", ii));
321 printf(" provname: %s\n",
322 nv_get_string(nv, "provname%u", ii));
323 printf(" localpath: %s\n",
324 nv_get_string(nv, "localpath%u", ii));
325 printf(" extentsize: %u (%NB)\n",
326 (unsigned int)nv_get_uint32(nv, "extentsize%u", ii),
327 (intmax_t)nv_get_uint32(nv, "extentsize%u", ii));
328 printf(" keepdirty: %u\n",
329 (unsigned int)nv_get_uint32(nv, "keepdirty%u", ii));
330 printf(" remoteaddr: %s\n",
331 nv_get_string(nv, "remoteaddr%u", ii));
332 str = nv_get_string(nv, "sourceaddr%u", ii);
334 printf(" sourceaddr: %s\n", str);
335 printf(" replication: %s\n",
336 nv_get_string(nv, "replication%u", ii));
337 str = nv_get_string(nv, "status%u", ii);
339 printf(" status: %s\n", str);
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 (uint64_t)nv_get_uint64(nv, "stat_read%u", ii));
346 printf(" writes: %ju\n",
347 (uint64_t)nv_get_uint64(nv, "stat_write%u", ii));
348 printf(" deletes: %ju\n",
349 (uint64_t)nv_get_uint64(nv, "stat_delete%u", ii));
350 printf(" flushes: %ju\n",
351 (uint64_t)nv_get_uint64(nv, "stat_flush%u", ii));
352 printf(" activemap updates: %ju\n",
353 (uint64_t)nv_get_uint64(nv, "stat_activemap_update%u", ii));
359 main(int argc, char *argv[])
362 int64_t mediasize, extentsize, keepdirty;
363 int cmd, debug, error, ii;
367 mediasize = extentsize = keepdirty = 0;
372 if (strcmp(argv[1], "create") == 0) {
374 optstr = "c:de:k:m:h";
375 } else if (strcmp(argv[1], "role") == 0) {
378 } else if (strcmp(argv[1], "status") == 0) {
381 } else if (strcmp(argv[1], "dump") == 0) {
393 ch = getopt(argc, argv, optstr);
404 if (expand_number(optarg, &extentsize) == -1)
405 errx(EX_USAGE, "Invalid extentsize");
408 if (expand_number(optarg, &keepdirty) == -1)
409 errx(EX_USAGE, "Invalid keepdirty");
412 if (expand_number(optarg, &mediasize) == -1)
413 errx(EX_USAGE, "Invalid mediasize");
431 pjdlog_init(PJDLOG_MODE_STD);
432 pjdlog_debug_set(debug);
434 cfg = yy_config_parse(cfgpath, true);
435 PJDLOG_ASSERT(cfg != NULL);
439 control_create(argc, argv, mediasize, extentsize, keepdirty);
441 PJDLOG_ABORT("What are we doing here?!");
444 /* Dump metadata from local component of the given resource. */
445 control_dump(argc, argv);
447 PJDLOG_ABORT("What are we doing here?!");
450 /* Change role for the given resources. */
454 nv_add_uint8(nv, HASTCTL_CMD_SETROLE, "cmd");
455 if (strcmp(argv[0], "init") == 0)
456 nv_add_uint8(nv, HAST_ROLE_INIT, "role");
457 else if (strcmp(argv[0], "primary") == 0)
458 nv_add_uint8(nv, HAST_ROLE_PRIMARY, "role");
459 else if (strcmp(argv[0], "secondary") == 0)
460 nv_add_uint8(nv, HAST_ROLE_SECONDARY, "role");
463 for (ii = 0; ii < argc - 1; ii++)
464 nv_add_string(nv, argv[ii + 1], "resource%d", ii);
467 /* Obtain status of the given resources. */
469 nv_add_uint8(nv, HASTCTL_CMD_STATUS, "cmd");
471 nv_add_string(nv, "all", "resource%d", 0);
473 for (ii = 0; ii < argc; ii++)
474 nv_add_string(nv, argv[ii], "resource%d", ii);
478 PJDLOG_ABORT("Impossible command!");
481 /* Setup control connection... */
482 if (proto_client(NULL, cfg->hc_controladdr, &controlconn) == -1) {
483 pjdlog_exit(EX_OSERR,
484 "Unable to setup control connection to %s",
485 cfg->hc_controladdr);
487 /* ...and connect to hastd. */
488 if (proto_connect(controlconn, HAST_TIMEOUT) == -1) {
489 pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s",
490 cfg->hc_controladdr);
493 if (drop_privs(NULL) != 0)
496 /* Send the command to the server... */
497 if (hast_proto_send(NULL, controlconn, nv, NULL, 0) == -1) {
498 pjdlog_exit(EX_UNAVAILABLE,
499 "Unable to send command to hastd via %s",
500 cfg->hc_controladdr);
503 /* ...and receive reply. */
504 if (hast_proto_recv_hdr(controlconn, &nv) == -1) {
505 pjdlog_exit(EX_UNAVAILABLE,
506 "cannot receive reply from hastd via %s",
507 cfg->hc_controladdr);
510 error = nv_get_int16(nv, "error");
512 pjdlog_exitx(EX_SOFTWARE, "Error %d received from hastd.",
519 error = control_set_role(nv, argv[0]);
522 error = control_status(nv);
525 PJDLOG_ABORT("Impossible command!");