1 /* 2 * Copyright (c) 2011-2019 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@dragonflybsd.org> 6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 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 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include "hammer2.h" 37 38 int DebugOpt; 39 int VerboseOpt; 40 int QuietOpt; 41 int NormalExit = 1; /* if set to 0 main() has to pthread_exit() */ 42 int RecurseOpt; 43 int ForceOpt; 44 size_t MemOpt; 45 46 static void usage(int code); 47 48 int 49 main(int ac, char **av) 50 { 51 const char *sel_path = NULL; 52 const char *uuid_str = NULL; 53 const char *arg; 54 char *opt; 55 int pfs_type = HAMMER2_PFSTYPE_NONE; 56 int all_opt = 0; 57 int ecode = 0; 58 int ch; 59 60 srandomdev(); 61 signal(SIGPIPE, SIG_IGN); 62 dmsg_crypto_setup(); 63 64 /* 65 * Core options 66 */ 67 while ((ch = getopt(ac, av, "adfm:rqs:t:u:v")) != -1) { 68 switch(ch) { 69 case 'a': 70 all_opt = 1; 71 break; 72 case 'd': 73 if (DebugOpt) 74 ++DMsgDebugOpt; 75 DebugOpt = 1; 76 break; 77 case 'f': 78 ForceOpt = 1; 79 break; 80 case 'm': 81 MemOpt = strtoul(optarg, &opt, 0); 82 switch(*opt) { 83 case 'g': 84 case 'G': 85 MemOpt *= 1024; 86 /* FALLTHROUGH */ 87 case 'm': 88 case 'M': 89 MemOpt *= 1024; 90 /* FALLTHROUGH */ 91 case 'k': 92 case 'K': 93 MemOpt *= 1024; 94 break; 95 case 0: 96 break; 97 default: 98 fprintf(stderr, "-m: unrecognized suffix\n"); 99 usage(1); 100 break; 101 } 102 break; 103 case 'r': 104 RecurseOpt = 1; 105 break; 106 case 's': 107 sel_path = optarg; 108 break; 109 case 't': 110 /* 111 * set node type for mkpfs 112 */ 113 if (strcasecmp(optarg, "CACHE") == 0) { 114 pfs_type = HAMMER2_PFSTYPE_CACHE; 115 } else if (strcasecmp(optarg, "DUMMY") == 0) { 116 pfs_type = HAMMER2_PFSTYPE_DUMMY; 117 } else if (strcasecmp(optarg, "SLAVE") == 0) { 118 pfs_type = HAMMER2_PFSTYPE_SLAVE; 119 } else if (strcasecmp(optarg, "SOFT_SLAVE") == 0) { 120 pfs_type = HAMMER2_PFSTYPE_SOFT_SLAVE; 121 } else if (strcasecmp(optarg, "SOFT_MASTER") == 0) { 122 pfs_type = HAMMER2_PFSTYPE_SOFT_MASTER; 123 } else if (strcasecmp(optarg, "MASTER") == 0) { 124 pfs_type = HAMMER2_PFSTYPE_MASTER; 125 } else { 126 fprintf(stderr, "-t: Unrecognized node type\n"); 127 usage(1); 128 } 129 break; 130 case 'u': 131 /* 132 * set uuid for mkpfs, else one will be generated 133 * (required for all except the MASTER node_type) 134 */ 135 uuid_str = optarg; 136 break; 137 case 'v': 138 if (QuietOpt) 139 --QuietOpt; 140 else 141 ++VerboseOpt; 142 break; 143 case 'q': 144 if (VerboseOpt) 145 --VerboseOpt; 146 else 147 ++QuietOpt; 148 break; 149 default: 150 fprintf(stderr, "Unknown option: %c\n", ch); 151 usage(1); 152 /* not reached */ 153 break; 154 } 155 } 156 157 /* 158 * Adjust, then process the command 159 */ 160 ac -= optind; 161 av += optind; 162 if (ac < 1) { 163 fprintf(stderr, "Missing command\n"); 164 usage(1); 165 /* not reached */ 166 } 167 168 if (strcmp(av[0], "connect") == 0) { 169 /* 170 * Add cluster connection 171 */ 172 if (ac < 2) { 173 fprintf(stderr, "connect: missing argument\n"); 174 usage(1); 175 } 176 ecode = cmd_remote_connect(sel_path, av[1]); 177 } else if (strcmp(av[0], "dumpchain") == 0) { 178 if (ac < 2) 179 ecode = cmd_dumpchain(".", (u_int)-1); 180 else if (ac < 3) 181 ecode = cmd_dumpchain(av[1], (u_int)-1); 182 else 183 ecode = cmd_dumpchain(av[1], 184 (u_int)strtoul(av[2], NULL, 0)); 185 } else if (strcmp(av[0], "debugspan") == 0) { 186 /* 187 * Debug connection to the target hammer2 service and run 188 * the CONN/SPAN protocol. 189 */ 190 if (ac < 2) { 191 fprintf(stderr, "debugspan: requires hostname\n"); 192 usage(1); 193 } 194 ecode = cmd_debugspan(av[1]); 195 } else if (strcmp(av[0], "disconnect") == 0) { 196 /* 197 * Remove cluster connection 198 */ 199 if (ac < 2) { 200 fprintf(stderr, "disconnect: missing argument\n"); 201 usage(1); 202 } 203 ecode = cmd_remote_disconnect(sel_path, av[1]); 204 } else if (strcmp(av[0], "destroy") == 0) { 205 if (ac < 2) { 206 fprintf(stderr, 207 "Specify one or more paths to destroy\n"); 208 usage(1); 209 } 210 ecode = cmd_destroy_path(ac - 1, (const char **)(void *)&av[1]); 211 } else if (strcmp(av[0], "destroy-inum") == 0) { 212 if (ac < 2) { 213 fprintf(stderr, 214 "Specify one or more inode numbers to destroy\n"); 215 usage(1); 216 } 217 ecode = cmd_destroy_inum(sel_path, ac - 1, 218 (const char **)(void *)&av[1]); 219 } else if (strcmp(av[0], "hash") == 0) { 220 ecode = cmd_hash(ac - 1, (const char **)(void *)&av[1]); 221 } else if (strcmp(av[0], "dhash") == 0) { 222 ecode = cmd_dhash(ac - 1, (const char **)(void *)&av[1]); 223 } else if (strcmp(av[0], "info") == 0) { 224 ecode = cmd_info(ac - 1, (const char **)(void *)&av[1]); 225 } else if (strcmp(av[0], "mountall") == 0) { 226 ecode = cmd_mountall(ac - 1, (const char **)(void *)&av[1]); 227 } else if (strcmp(av[0], "status") == 0) { 228 /* 229 * Get status of PFS and its connections (-a for all PFSs) 230 */ 231 if (ac < 2) { 232 ecode = cmd_remote_status(sel_path, all_opt); 233 } else { 234 int i; 235 236 for (i = 1; i < ac; ++i) 237 ecode = cmd_remote_status(av[i], all_opt); 238 } 239 } else if (strcmp(av[0], "pfs-clid") == 0) { 240 /* 241 * Print cluster id (uuid) for specific PFS 242 */ 243 if (ac < 2) { 244 fprintf(stderr, "pfs-clid: requires name\n"); 245 usage(1); 246 } 247 ecode = cmd_pfs_getid(sel_path, av[1], 0); 248 } else if (strcmp(av[0], "pfs-fsid") == 0) { 249 /* 250 * Print private id (uuid) for specific PFS 251 */ 252 if (ac < 2) { 253 fprintf(stderr, "pfs-fsid: requires name\n"); 254 usage(1); 255 } 256 ecode = cmd_pfs_getid(sel_path, av[1], 1); 257 } else if (strcmp(av[0], "pfs-list") == 0) { 258 /* 259 * List all PFSs 260 */ 261 if (ac >= 2) { 262 ecode = cmd_pfs_list(ac - 1, 263 (const char **)(void *)&av[1]); 264 } else { 265 ecode = cmd_pfs_list(1, &sel_path); 266 } 267 } else if (strcmp(av[0], "pfs-create") == 0) { 268 /* 269 * Create new PFS using pfs_type 270 */ 271 if (ac < 2) { 272 fprintf(stderr, "pfs-create: requires name\n"); 273 usage(1); 274 } 275 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str); 276 } else if (strcmp(av[0], "pfs-delete") == 0) { 277 /* 278 * Delete a PFS by name 279 */ 280 if (ac < 2) { 281 fprintf(stderr, "pfs-delete: requires name\n"); 282 usage(1); 283 } 284 ecode = cmd_pfs_delete(sel_path, av[1]); 285 } else if (strcmp(av[0], "snapshot") == 0 || 286 strcmp(av[0], "snapshot-debug") == 0) { 287 /* 288 * Create snapshot with optional pfs-type and optional 289 * label override. 290 */ 291 uint32_t flags = 0; 292 293 if (strcmp(av[0], "snapshot-debug") == 0) 294 flags = HAMMER2_PFSFLAGS_NOSYNC; 295 296 if (ac > 3) { 297 fprintf(stderr, "pfs-snapshot: too many arguments\n"); 298 usage(1); 299 } 300 switch(ac) { 301 case 1: 302 ecode = cmd_pfs_snapshot(sel_path, NULL, NULL, flags); 303 break; 304 case 2: 305 ecode = cmd_pfs_snapshot(sel_path, av[1], NULL, flags); 306 break; 307 case 3: 308 ecode = cmd_pfs_snapshot(sel_path, av[1], av[2], flags); 309 break; 310 } 311 } else if (strcmp(av[0], "service") == 0) { 312 /* 313 * Start the service daemon. This daemon accepts 314 * connections from local and remote clients, handles 315 * the security handshake, and manages the core messaging 316 * protocol. 317 */ 318 ecode = cmd_service(); 319 } else if (strcmp(av[0], "stat") == 0) { 320 ecode = cmd_stat(ac - 1, (const char **)(void *)&av[1]); 321 } else if (strcmp(av[0], "leaf") == 0) { 322 /* 323 * Start the management daemon for a specific PFS. 324 * 325 * This will typically connect to the local master node 326 * daemon, register the PFS, and then pass its side of 327 * the socket descriptor to the kernel HAMMER2 VFS via an 328 * ioctl(). The process and/or thread context remains in the 329 * kernel until the PFS is unmounted or the connection is 330 * lost, then returns from the ioctl. 331 * 332 * It is possible to connect directly to a remote master node 333 * instead of the local master node in situations where 334 * encryption is not desired or no local master node is 335 * desired. This is not recommended because it represents 336 * a single point of failure for the PFS's communications. 337 * 338 * Direct kernel<->kernel communication between HAMMER2 VFSs 339 * is theoretically possible for directly-connected 340 * registrations (i.e. where the spanning tree is degenerate), 341 * but not recommended. We specifically try to reduce the 342 * complexity of the HAMMER2 VFS kernel code. 343 */ 344 ecode = cmd_leaf(sel_path); 345 } else if (strcmp(av[0], "shell") == 0) { 346 /* 347 * Connect to the command line monitor in the hammer2 master 348 * node for the machine using HAMMER2_DBG_SHELL messages. 349 */ 350 ecode = cmd_shell((ac < 2) ? NULL : av[1]); 351 } else if (strcmp(av[0], "rsainit") == 0) { 352 /* 353 * Initialize a RSA keypair. If no target directory is 354 * specified we default to "/etc/hammer2". 355 */ 356 arg = (ac < 2) ? HAMMER2_DEFAULT_DIR : av[1]; 357 ecode = cmd_rsainit(arg); 358 } else if (strcmp(av[0], "rsaenc") == 0) { 359 /* 360 * Encrypt the input symmetrically by running it through 361 * the specified public and/or private key files. 362 * 363 * If no key files are specified data is encoded using 364 * "/etc/hammer2/rsa.pub". 365 * 366 * WARNING: no padding is added, data stream must contain 367 * random padding for this to be secure. 368 * 369 * Used for debugging only 370 */ 371 if (ac == 1) { 372 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.pub"; 373 ecode = cmd_rsaenc(&rsapath, 1); 374 } else { 375 ecode = cmd_rsaenc((const char **)(void *)&av[1], 376 ac - 1); 377 } 378 } else if (strcmp(av[0], "rsadec") == 0) { 379 /* 380 * Decrypt the input symmetrically by running it through 381 * the specified public and/or private key files. 382 * 383 * If no key files are specified data is decoded using 384 * "/etc/hammer2/rsa.prv". 385 * 386 * WARNING: no padding is added, data stream must contain 387 * random padding for this to be secure. 388 * 389 * Used for debugging only 390 */ 391 if (ac == 1) { 392 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.prv"; 393 ecode = cmd_rsadec(&rsapath, 1); 394 } else { 395 ecode = cmd_rsadec((const char **)(void *)&av[1], 396 ac - 1); 397 } 398 } else if (strcmp(av[0], "show") == 0) { 399 /* 400 * Raw dump of filesystem. Use -v to check all crc's, and 401 * -vv to dump bulk file data. 402 */ 403 if (ac != 2) { 404 fprintf(stderr, "show: requires device path\n"); 405 usage(1); 406 } else { 407 cmd_show(av[1], 0); 408 } 409 } else if (strcmp(av[0], "freemap") == 0) { 410 /* 411 * Raw dump of freemap. Use -v to check all crc's, and 412 * -vv to dump bulk file data. 413 */ 414 if (ac != 2) { 415 fprintf(stderr, "freemap: requires device path\n"); 416 usage(1); 417 } else { 418 cmd_show(av[1], 1); 419 } 420 } else if (strcmp(av[0], "setcomp") == 0) { 421 if (ac < 3) { 422 /* 423 * Missing compression method and at least one 424 * path. 425 */ 426 fprintf(stderr, 427 "setcomp: requires compression method and" 428 "directory/file path\n"); 429 usage(1); 430 } else { 431 /* 432 * Multiple paths may be specified 433 */ 434 ecode = cmd_setcomp(av[1], &av[2]); 435 } 436 } else if (strcmp(av[0], "setcheck") == 0) { 437 if (ac < 3) { 438 /* 439 * Missing compression method and at least one 440 * path. 441 */ 442 fprintf(stderr, 443 "setcheck: requires check code method and" 444 "directory/file path\n"); 445 usage(1); 446 } else { 447 /* 448 * Multiple paths may be specified 449 */ 450 ecode = cmd_setcheck(av[1], &av[2]); 451 } 452 } else if (strcmp(av[0], "clrcheck") == 0) { 453 ecode = cmd_setcheck("none", &av[1]); 454 } else if (strcmp(av[0], "setcrc32") == 0) { 455 ecode = cmd_setcheck("crc32", &av[1]); 456 } else if (strcmp(av[0], "setxxhash64") == 0) { 457 ecode = cmd_setcheck("xxhash64", &av[1]); 458 } else if (strcmp(av[0], "setsha192") == 0) { 459 ecode = cmd_setcheck("sha192", &av[1]); 460 } else if (strcmp(av[0], "printinode") == 0) { 461 if (ac != 2) { 462 fprintf(stderr, 463 "printinode: requires directory/file path\n"); 464 usage(1); 465 } else { 466 print_inode(av[1]); 467 } 468 } else if (strcmp(av[0], "bulkfree") == 0) { 469 if (ac != 2) { 470 fprintf(stderr, 471 "bulkfree: requires path to mount\n"); 472 usage(1); 473 } else { 474 ecode = cmd_bulkfree(av[1]); 475 } 476 #if 0 477 } else if (strcmp(av[0], "bulkfree-async") == 0) { 478 if (ac != 2) { 479 fprintf(stderr, 480 "bulkfree-async: requires path to mount\n"); 481 usage(1); 482 } else { 483 ecode = cmd_bulkfree_async(av[1]); 484 } 485 #endif 486 } else if (strcmp(av[0], "cleanup") == 0) { 487 ecode = cmd_cleanup(av[1]); /* can be NULL */ 488 } else { 489 fprintf(stderr, "Unrecognized command: %s\n", av[0]); 490 usage(1); 491 } 492 493 /* 494 * In DebugMode we may wind up starting several pthreads in the 495 * original process, in which case we have to let them run and 496 * not actually exit. 497 */ 498 if (NormalExit) { 499 return (ecode); 500 } else { 501 pthread_exit(NULL); 502 _exit(2); /* NOT REACHED */ 503 } 504 } 505 506 static 507 void 508 usage(int code) 509 { 510 fprintf(stderr, 511 "hammer2 [options] command [argument ...]\n" 512 " -s path Select filesystem\n" 513 " -t type PFS type for pfs-create\n" 514 " -u uuid uuid for pfs-create\n" 515 " -m mem[k,m,g] buffer memory (bulkfree)\n" 516 "\n" 517 " cleanup [<path>] " 518 "Run cleanup passes\n" 519 " connect <target> " 520 "Add cluster link\n" 521 " destroy <path>... " 522 "Destroy directory entries (only use if inode bad)\n" 523 " destroy-inum <inum>... " 524 "Destroy inodes (only use if inode bad)\n" 525 " disconnect <target> " 526 "Del cluster link\n" 527 " info [<devpath>...] " 528 "Info on all offline or online H2 partitions\n" 529 " mountall [<devpath>...] " 530 "Mount @LOCAL for all H2 partitions\n" 531 " status [<path>...] " 532 "Report active cluster status\n" 533 " hash [<filename>...] " 534 "Print directory hash (key) for name\n" 535 " dhash [<filename>...] " 536 "Print data hash for long directory entry\n" 537 " pfs-list [<path>...] " 538 "List PFSs\n" 539 " pfs-clid <label> " 540 "Print cluster id for specific PFS\n" 541 " pfs-fsid <label> " 542 "Print private id for specific PFS\n" 543 " pfs-create <label> " 544 "Create a PFS\n" 545 " pfs-delete <label> " 546 "Destroy a PFS\n" 547 " snapshot <path> [<label>] " 548 "Snapshot a PFS or directory\n" 549 " snapshot-debug <path> [<label>] " 550 "Snapshot without filesystem sync\n" 551 " service " 552 "Start service daemon\n" 553 " stat [<path>...] " 554 "Return inode quota & config\n" 555 " leaf " 556 "Start pfs leaf daemon\n" 557 " shell [<host>] " 558 "Connect to debug shell\n" 559 " debugspan <target> " 560 "Connect to target, run CONN/SPAN\n" 561 " rsainit [<path>] " 562 "Initialize rsa fields\n" 563 " show <devpath> " 564 "Raw hammer2 media dump for topology\n" 565 " freemap <devpath> " 566 "Raw hammer2 media dump for freemap\n" 567 " setcomp <comp[:level]> <path>... " 568 "Set comp algo {none, autozero, lz4, zlib} & level\n" 569 " setcheck <check> <path>... " 570 "Set check algo {none, crc32, xxhash64, sha192}\n" 571 " clrcheck [<path>...] " 572 "Clear check code override\n" 573 " setcrc32 [<path>...] " 574 "Set check algo to crc32\n" 575 " setxxhash64 [<path>...] " 576 "Set check algo to xxhash64\n" 577 " setsha192 [<path>...] " 578 "Set check algo to sha192\n" 579 " bulkfree <path> " 580 "Run bulkfree pass\n" 581 " printinode <path> " 582 "Dump inode\n" 583 " dumpchain [<path> [<chnflags>]] " 584 "Dump in-memory chain topology (ONFLUSH flag is 0x200)\n" 585 #if 0 586 " bulkfree-async path " 587 "Run bulkfree pass asynchronously\n" 588 #endif 589 ); 590 exit(code); 591 } 592