1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009-2010 The FreeBSD Foundation 5 * 6 * This software was developed by Pawel Jakub Dawidek under sponsorship from 7 * the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/param.h> 32 33 #include <err.h> 34 #include <libutil.h> 35 #include <stdio.h> 36 #include <string.h> 37 #include <unistd.h> 38 39 #include <activemap.h> 40 41 #include "hast.h" 42 #include "hast_proto.h" 43 #include "metadata.h" 44 #include "nv.h" 45 #include "pjdlog.h" 46 #include "proto.h" 47 #include "subr.h" 48 49 /* Path to configuration file. */ 50 static const char *cfgpath = HAST_CONFIG; 51 /* Hastd configuration. */ 52 static struct hastd_config *cfg; 53 /* Control connection. */ 54 static struct proto_conn *controlconn; 55 56 enum { 57 CMD_INVALID, 58 CMD_CREATE, 59 CMD_ROLE, 60 CMD_STATUS, 61 CMD_DUMP, 62 CMD_STOP, 63 CMD_LIST 64 }; 65 66 static __dead2 void 67 usage(void) 68 { 69 70 fprintf(stderr, 71 "usage: %s create [-d] [-c config] [-e extentsize] [-k keepdirty]\n" 72 "\t\t[-m mediasize] name ...\n", 73 getprogname()); 74 fprintf(stderr, 75 " %s role [-d] [-c config] <init | primary | secondary> all | name ...\n", 76 getprogname()); 77 fprintf(stderr, 78 " %s list [-d] [-c config] [all | name ...]\n", 79 getprogname()); 80 fprintf(stderr, 81 " %s status [-d] [-c config] [all | name ...]\n", 82 getprogname()); 83 fprintf(stderr, 84 " %s dump [-d] [-c config] [all | name ...]\n", 85 getprogname()); 86 exit(EX_USAGE); 87 } 88 89 static int 90 create_one(struct hast_resource *res, intmax_t mediasize, intmax_t extentsize, 91 intmax_t keepdirty) 92 { 93 unsigned char *buf; 94 size_t mapsize; 95 int ec; 96 97 ec = 0; 98 pjdlog_prefix_set("[%s] ", res->hr_name); 99 100 if (provinfo(res, true) == -1) { 101 ec = EX_NOINPUT; 102 goto end; 103 } 104 if (mediasize == 0) 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.", 108 res->hr_localpath); 109 ec = EX_DATAERR; 110 goto end; 111 } 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); 115 ec = EX_DATAERR; 116 goto end; 117 } 118 if (extentsize == 0) 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); 123 ec = EX_DATAERR; 124 goto end; 125 } 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); 129 ec = EX_DATAERR; 130 goto end; 131 } 132 mapsize = activemap_calc_ondisk_size(mediasize - METADATA_SIZE, 133 extentsize, res->hr_local_sectorsize); 134 if (keepdirty == 0) 135 keepdirty = HAST_KEEPDIRTY; 136 res->hr_datasize = mediasize - METADATA_SIZE - mapsize; 137 res->hr_extentsize = extentsize; 138 res->hr_keepdirty = keepdirty; 139 140 res->hr_localoff = METADATA_SIZE + mapsize; 141 142 if (metadata_write(res) == -1) { 143 ec = EX_IOERR; 144 goto end; 145 } 146 buf = calloc(1, mapsize); 147 if (buf == NULL) { 148 pjdlog_error("Unable to allocate %zu bytes of memory for initial bitmap.", 149 mapsize); 150 ec = EX_TEMPFAIL; 151 goto end; 152 } 153 if (pwrite(res->hr_localfd, buf, mapsize, METADATA_SIZE) != 154 (ssize_t)mapsize) { 155 pjdlog_errno(LOG_ERR, "Unable to store initial bitmap on %s", 156 res->hr_localpath); 157 free(buf); 158 ec = EX_IOERR; 159 goto end; 160 } 161 free(buf); 162 end: 163 if (res->hr_localfd >= 0) 164 close(res->hr_localfd); 165 pjdlog_prefix_set("%s", ""); 166 return (ec); 167 } 168 169 static void 170 control_create(int argc, char *argv[], intmax_t mediasize, intmax_t extentsize, 171 intmax_t keepdirty) 172 { 173 struct hast_resource *res; 174 int ec, ii, ret; 175 176 /* Initialize the given resources. */ 177 if (argc < 1) 178 usage(); 179 ec = 0; 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) 183 break; 184 } 185 if (res == NULL) { 186 pjdlog_error("Unknown resource %s.", argv[ii]); 187 if (ec == 0) 188 ec = EX_DATAERR; 189 continue; 190 } 191 ret = create_one(res, mediasize, extentsize, keepdirty); 192 if (ret != 0 && ec == 0) 193 ec = ret; 194 } 195 exit(ec); 196 } 197 198 static int 199 dump_one(struct hast_resource *res) 200 { 201 int ret; 202 203 ret = metadata_read(res, false); 204 if (ret != 0) 205 return (ret); 206 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)); 218 219 return (0); 220 } 221 222 static void 223 control_dump(int argc, char *argv[]) 224 { 225 struct hast_resource *res; 226 int ec, ret; 227 228 /* Dump metadata of the given resource(s). */ 229 230 ec = 0; 231 if (argc == 0 || (argc == 1 && strcmp(argv[0], "all") == 0)) { 232 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 233 ret = dump_one(res); 234 if (ret != 0 && ec == 0) 235 ec = ret; 236 } 237 } else { 238 int ii; 239 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) 243 break; 244 } 245 if (res == NULL) { 246 pjdlog_error("Unknown resource %s.", argv[ii]); 247 if (ec == 0) 248 ec = EX_DATAERR; 249 continue; 250 } 251 ret = dump_one(res); 252 if (ret != 0 && ec == 0) 253 ec = ret; 254 } 255 } 256 exit(ec); 257 } 258 259 static int 260 control_set_role(struct nv *nv, const char *newrole) 261 { 262 const char *res, *oldrole; 263 unsigned int ii; 264 int error, ret; 265 266 ret = 0; 267 268 for (ii = 0; ; ii++) { 269 res = nv_get_string(nv, "resource%u", ii); 270 if (res == NULL) 271 break; 272 pjdlog_prefix_set("[%s] ", res); 273 error = nv_get_int16(nv, "error%u", ii); 274 if (error != 0) { 275 if (ret == 0) 276 ret = error; 277 pjdlog_warning("Received error %d from hastd.", error); 278 continue; 279 } 280 oldrole = nv_get_string(nv, "role%u", ii); 281 if (strcmp(oldrole, newrole) == 0) 282 pjdlog_debug(2, "Role unchanged (%s).", oldrole); 283 else { 284 pjdlog_debug(1, "Role changed from %s to %s.", oldrole, 285 newrole); 286 } 287 } 288 pjdlog_prefix_set("%s", ""); 289 return (ret); 290 } 291 292 static int 293 control_list(struct nv *nv) 294 { 295 pid_t pid; 296 unsigned int ii; 297 const char *str; 298 int error, ret; 299 300 ret = 0; 301 302 for (ii = 0; ; ii++) { 303 str = nv_get_string(nv, "resource%u", ii); 304 if (str == NULL) 305 break; 306 printf("%s:\n", str); 307 error = nv_get_int16(nv, "error%u", ii); 308 if (error != 0) { 309 if (ret == 0) 310 ret = error; 311 printf(" error: %d\n", error); 312 continue; 313 } 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); 327 if (str != NULL) 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); 332 if (str != NULL) 333 printf(" status: %s\n", str); 334 pid = nv_get_int32(nv, "workerpid%u", ii); 335 if (pid != 0) 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)); 357 printf(" queues: " 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)); 364 } 365 return (ret); 366 } 367 368 static int 369 control_status(struct nv *nv) 370 { 371 unsigned int ii; 372 const char *str; 373 int error, hprinted, ret; 374 375 hprinted = 0; 376 ret = 0; 377 378 for (ii = 0; ; ii++) { 379 str = nv_get_string(nv, "resource%u", ii); 380 if (str == NULL) 381 break; 382 if (!hprinted) { 383 printf("Name\tStatus\t Role\t\tComponents\n"); 384 hprinted = 1; 385 } 386 printf("%s\t", str); 387 error = nv_get_int16(nv, "error%u", ii); 388 if (error != 0) { 389 if (ret == 0) 390 ret = error; 391 printf("ERR%d\n", error); 392 continue; 393 } 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)); 397 printf("%s\t", 398 nv_get_string(nv, "localpath%u", ii)); 399 printf("%s\n", 400 nv_get_string(nv, "remoteaddr%u", ii)); 401 } 402 return (ret); 403 } 404 405 int 406 main(int argc, char *argv[]) 407 { 408 struct nv *nv; 409 int64_t mediasize, extentsize, keepdirty; 410 int cmd, debug, error, ii; 411 const char *optstr; 412 413 debug = 0; 414 mediasize = extentsize = keepdirty = 0; 415 416 if (argc == 1) 417 usage(); 418 419 if (strcmp(argv[1], "create") == 0) { 420 cmd = CMD_CREATE; 421 optstr = "c:de:k:m:h"; 422 } else if (strcmp(argv[1], "role") == 0) { 423 cmd = CMD_ROLE; 424 optstr = "c:dh"; 425 } else if (strcmp(argv[1], "list") == 0) { 426 cmd = CMD_LIST; 427 optstr = "c:dh"; 428 } else if (strcmp(argv[1], "status") == 0) { 429 cmd = CMD_STATUS; 430 optstr = "c:dh"; 431 } else if (strcmp(argv[1], "dump") == 0) { 432 cmd = CMD_DUMP; 433 optstr = "c:dh"; 434 } else if (strcmp(argv[1], "stop") == 0) { 435 cmd = CMD_STOP; 436 optstr = "c:dh"; 437 } else 438 usage(); 439 440 argc--; 441 argv++; 442 443 for (;;) { 444 int ch; 445 446 ch = getopt(argc, argv, optstr); 447 if (ch == -1) 448 break; 449 switch (ch) { 450 case 'c': 451 cfgpath = optarg; 452 break; 453 case 'd': 454 debug++; 455 break; 456 case 'e': 457 if (expand_number(optarg, &extentsize) == -1) 458 errx(EX_USAGE, "Invalid extentsize"); 459 break; 460 case 'k': 461 if (expand_number(optarg, &keepdirty) == -1) 462 errx(EX_USAGE, "Invalid keepdirty"); 463 break; 464 case 'm': 465 if (expand_number(optarg, &mediasize) == -1) 466 errx(EX_USAGE, "Invalid mediasize"); 467 break; 468 case 'h': 469 default: 470 usage(); 471 } 472 } 473 argc -= optind; 474 argv += optind; 475 476 switch (cmd) { 477 case CMD_CREATE: 478 case CMD_ROLE: 479 if (argc == 0) 480 usage(); 481 break; 482 } 483 484 pjdlog_init(PJDLOG_MODE_STD); 485 pjdlog_debug_set(debug); 486 487 cfg = yy_config_parse(cfgpath, true); 488 PJDLOG_ASSERT(cfg != NULL); 489 490 switch (cmd) { 491 case CMD_CREATE: 492 control_create(argc, argv, mediasize, extentsize, keepdirty); 493 /* NOTREACHED */ 494 PJDLOG_ABORT("What are we doing here?!"); 495 break; 496 case CMD_DUMP: 497 /* Dump metadata from local component of the given resource. */ 498 control_dump(argc, argv); 499 /* NOTREACHED */ 500 PJDLOG_ABORT("What are we doing here?!"); 501 break; 502 case CMD_ROLE: 503 /* Change role for the given resources. */ 504 if (argc < 2) 505 usage(); 506 nv = nv_alloc(); 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"); 514 else 515 usage(); 516 for (ii = 0; ii < argc - 1; ii++) 517 nv_add_string(nv, argv[ii + 1], "resource%d", ii); 518 break; 519 case CMD_LIST: 520 case CMD_STATUS: 521 /* Obtain status of the given resources. */ 522 nv = nv_alloc(); 523 nv_add_uint8(nv, HASTCTL_CMD_STATUS, "cmd"); 524 if (argc == 0) 525 nv_add_string(nv, "all", "resource%d", 0); 526 else { 527 for (ii = 0; ii < argc; ii++) 528 nv_add_string(nv, argv[ii], "resource%d", ii); 529 } 530 break; 531 case CMD_STOP: 532 /* Stop hastd. */ 533 if (argc > 0) 534 usage(); 535 nv = nv_alloc(); 536 nv_add_uint8(nv, HASTCTL_CMD_STOP, "cmd"); 537 break; 538 default: 539 PJDLOG_ABORT("Impossible command!"); 540 } 541 542 /* Setup control connection... */ 543 if (proto_client(NULL, cfg->hc_controladdr, &controlconn) == -1) { 544 pjdlog_exit(EX_OSERR, 545 "Unable to setup control connection to %s", 546 cfg->hc_controladdr); 547 } 548 /* ...and connect to hastd. */ 549 if (proto_connect(controlconn, HAST_TIMEOUT) == -1) { 550 pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s", 551 cfg->hc_controladdr); 552 } 553 554 if (drop_privs(NULL) != 0) 555 exit(EX_CONFIG); 556 557 /* Send the command to the server... */ 558 if (hast_proto_send(NULL, controlconn, nv, NULL, 0) == -1) { 559 pjdlog_exit(EX_UNAVAILABLE, 560 "Unable to send command to hastd via %s", 561 cfg->hc_controladdr); 562 } 563 nv_free(nv); 564 /* ...and receive reply. */ 565 if (hast_proto_recv_hdr(controlconn, &nv) == -1) { 566 pjdlog_exit(EX_UNAVAILABLE, 567 "cannot receive reply from hastd via %s", 568 cfg->hc_controladdr); 569 } 570 571 error = nv_get_int16(nv, "error"); 572 if (error != 0) { 573 pjdlog_exitx(EX_SOFTWARE, "Error %d received from hastd.", 574 error); 575 } 576 nv_set_error(nv, 0); 577 578 switch (cmd) { 579 case CMD_ROLE: 580 error = control_set_role(nv, argv[0]); 581 break; 582 case CMD_LIST: 583 error = control_list(nv); 584 break; 585 case CMD_STATUS: 586 error = control_status(nv); 587 break; 588 case CMD_STOP: 589 error = 0; 590 break; 591 default: 592 PJDLOG_ABORT("Impossible command!"); 593 } 594 595 exit(error); 596 } 597