2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * 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
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static void hammer_parsedevs(const char *blkdevs);
38 static void sigalrm(int signo);
39 static void sigintr(int signo);
40 static void usage(int exit_code);
57 uint64_t BandwidthOpt;
58 uint64_t SplitupOpt = 4ULL * 1024ULL * 1024ULL * 1024ULL;
59 uint64_t MemoryLimit = 1024LLU * 1024 * 1024;
60 const char *SplitupOptStr;
61 const char *CyclePath;
64 main(int ac, char **av)
68 char *restrictcmd = NULL;
72 while ((ch = getopt(ac, av,
73 "b:c:de:hf:i:m:p:qrt:v2yABC:FR:S:T:X")) != -1) {
82 BandwidthOpt = strtoull(optarg, &ptr, 0);
97 /* bytes per second if no suffix */
104 SplitupOptStr = strdup(optarg);
105 SplitupOpt = strtoull(optarg, &ptr, 0);
120 /* bytes per second if no suffix */
133 ScoreBoardFile = optarg;
139 DelayOpt = strtol(optarg, NULL, 0);
142 MemoryLimit = strtouq(optarg, &ptr, 0);
165 if (MemoryLimit < 1024 * 1024)
166 MemoryLimit = 1024 * 1024;
178 TimeoutOpt = strtol(optarg, NULL, 0);
199 if (hammer_parse_cache_size(optarg) == -1)
206 if (restrictcmd == NULL)
207 restrictcmd = optarg;
210 if (RestrictTarget == NULL)
211 RestrictTarget = optarg;
228 signal(SIGALRM, sigalrm);
229 signal(SIGINT, sigintr);
232 * Check command restriction (used by hammer ssh-remote). Several
233 * commands may be iterated with a comma.
238 dup = ptr = strdup(restrictcmd);
239 while ((elm = strsep(&ptr, ",")) != NULL) {
240 if (strcmp(av[0], elm) == 0)
244 fprintf(stderr, "hammer-remote: request does not match "
245 "restricted command\n");
251 uuid_name_lookup(&Hammer_FSType, HAMMER_FSTYPE_STRING, &status);
252 if (status != uuid_s_ok) {
253 errx(1, "uuids file does not have the DragonFly "
254 "HAMMER filesystem type");
260 if (strcmp(av[0], "synctid") == 0) {
261 hammer_cmd_synctid(av + 1, ac - 1);
264 if (strcmp(av[0], "namekey2") == 0) {
268 const char *aname = av[1];
273 key = (uint32_t)crc32(aname, len) & 0xFFFFFFFEU;
277 crcx = crc32(aname + 3, len - 5);
278 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
279 key |= (int64_t)(crcx & 0x3F) << 42;
285 key |= ((int64_t)(aname[2] & 0x1F) << 48);
288 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
289 ((int64_t)(aname[len-2] & 0x1F) << 37);
292 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
293 ((int64_t)(aname[len-1] & 0x1F) << 32);
299 key |= 0x100000000LL;
300 printf("0x%016jx\n", (uintmax_t)key);
303 if (strcmp(av[0], "namekey1") == 0) {
308 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
310 key |= 0x100000000LL;
311 printf("0x%016jx\n", (uintmax_t)key);
314 if (strcmp(av[0], "namekey32") == 0) {
319 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
322 printf("0x%08x\n", key);
325 if (strcmp(av[0], "pfs-status") == 0) {
326 hammer_cmd_pseudofs_status(av + 1, ac - 1);
329 if (strcmp(av[0], "pfs-master") == 0) {
330 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
333 if (strcmp(av[0], "pfs-slave") == 0) {
334 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
337 if (strcmp(av[0], "pfs-update") == 0) {
338 hammer_cmd_pseudofs_update(av + 1, ac - 1);
341 if (strcmp(av[0], "pfs-upgrade") == 0) {
342 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
345 if (strcmp(av[0], "pfs-downgrade") == 0) {
346 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
349 if (strcmp(av[0], "pfs-destroy") == 0) {
350 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
353 if (strcmp(av[0], "prune") == 0) {
354 hammer_cmd_softprune(av + 1, ac - 1, 0);
357 if (strcmp(av[0], "config") == 0) {
358 hammer_cmd_config(av + 1, ac - 1);
361 if (strcmp(av[0], "viconfig") == 0) {
362 hammer_cmd_viconfig(av + 1, ac - 1);
365 if (strcmp(av[0], "cleanup") == 0) {
366 hammer_cmd_cleanup(av + 1, ac - 1);
369 if (strcmp(av[0], "abort-cleanup") == 0) {
370 hammer_cmd_abort_cleanup(av + 1, ac - 1);
373 if (strcmp(av[0], "info") == 0) {
374 hammer_cmd_info(av + 1, ac - 1);
377 if (strcmp(av[0], "prune-everything") == 0) {
378 hammer_cmd_softprune(av + 1, ac - 1, 1);
381 if (strcmp(av[0], "ssh-remote") == 0) {
384 hammer_cmd_sshremote(av[1], av[2]);
387 if (strcmp(av[0], "snap") == 0) {
388 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
391 if (strcmp(av[0], "snaplo") == 0) {
392 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
395 if (strcmp(av[0], "snapq") == 0) {
396 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
399 if (strcmp(av[0], "snapls") == 0) {
400 hammer_cmd_snapls(av + 1, ac - 1);
403 if (strcmp(av[0], "snaprm") == 0) {
404 hammer_cmd_snaprm(av + 1, ac - 1);
407 if (strcmp(av[0], "snapshot") == 0) {
408 hammer_cmd_snapshot(av + 1, ac - 1);
411 if (strcmp(av[0], "bstats") == 0) {
412 hammer_cmd_bstats(av + 1, ac - 1);
415 if (strcmp(av[0], "iostats") == 0) {
416 hammer_cmd_iostats(av + 1, ac - 1);
419 if (strcmp(av[0], "stats") == 0) {
420 hammer_cmd_stats(av + 1, ac - 1);
424 if (strncmp(av[0], "history", 7) == 0) {
425 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
428 if (strcmp(av[0], "rebalance") == 0) {
429 signal(SIGINT, sigalrm);
430 hammer_cmd_rebalance(av + 1, ac - 1);
433 if (strncmp(av[0], "reblock", 7) == 0) {
434 signal(SIGINT, sigalrm);
435 if (strcmp(av[0], "reblock") == 0)
436 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_FLAGS);
437 else if (strcmp(av[0], "reblock-btree") == 0)
438 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
439 else if (strcmp(av[0], "reblock-inodes") == 0)
440 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
441 else if (strcmp(av[0], "reblock-dirs") == 0)
442 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
443 else if (strcmp(av[0], "reblock-data") == 0)
444 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
449 if (strncmp(av[0], "mirror", 6) == 0) {
450 if (strcmp(av[0], "mirror-read") == 0)
451 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
452 else if (strcmp(av[0], "mirror-read-stream") == 0)
453 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
454 else if (strcmp(av[0], "mirror-write") == 0)
455 hammer_cmd_mirror_write(av + 1, ac - 1);
456 else if (strcmp(av[0], "mirror-copy") == 0)
457 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
458 else if (strcmp(av[0], "mirror-stream") == 0)
459 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
460 else if (strcmp(av[0], "mirror-dump") == 0)
461 hammer_cmd_mirror_dump(av + 1, ac - 1);
466 if (strcmp(av[0], "dedup-simulate") == 0) {
467 hammer_cmd_dedup_simulate(av + 1, ac - 1);
470 if (strcmp(av[0], "dedup") == 0) {
471 hammer_cmd_dedup(av + 1, ac - 1);
474 if (strcmp(av[0], "version") == 0) {
475 hammer_cmd_get_version(av + 1, ac - 1);
478 if (strcmp(av[0], "version-upgrade") == 0) {
479 hammer_cmd_set_version(av + 1, ac - 1);
482 if (strcmp(av[0], "volume-add") == 0) {
483 hammer_cmd_volume_add(av + 1, ac - 1);
486 if (strcmp(av[0], "volume-del") == 0) {
487 hammer_cmd_volume_del(av + 1, ac - 1);
490 if (strcmp(av[0], "volume-list") == 0) {
491 hammer_cmd_volume_list(av + 1, ac - 1);
494 if (strcmp(av[0], "volume-blkdevs") == 0) {
495 hammer_cmd_volume_blkdevs(av + 1, ac - 1);
499 if (strcmp(av[0], "show") == 0) {
500 const char *arg = NULL;
506 hammer_parsedevs(blkdevs);
508 errx(1, "Too many options specified");
512 dup = ptr = strdup(av[2]);
513 while ((p = strsep(&ptr, ",")) != NULL) {
514 if (strcmp(p, "filter") == 0) {
516 } else if (strcmp(p, "nofilter") == 0) {
518 } else if (strcmp(p, "obfuscate") == 0) {
520 } else if (strcmp(p, "indent") == 0) {
526 hammer_cmd_show(arg, filter, obfuscate, indent);
529 if (strcmp(av[0], "show-undo") == 0) {
530 hammer_parsedevs(blkdevs);
531 hammer_cmd_show_undo();
534 if (strcmp(av[0], "recover") == 0) {
535 hammer_parsedevs(blkdevs);
537 errx(1, "hammer recover required target directory");
538 hammer_cmd_recover(av[1]);
541 if (strcmp(av[0], "blockmap") == 0) {
542 hammer_parsedevs(blkdevs);
543 hammer_cmd_blockmap();
546 if (strcmp(av[0], "checkmap") == 0) {
547 hammer_parsedevs(blkdevs);
548 hammer_cmd_checkmap();
557 * Parse the device specification.
559 * Multi-volume hammer devices are colon-separated. Each element
560 * may be further expanded via /etc/devtab. One may also specify
561 * a single element which is expanded into multiple elements via
566 hammer_parsedevs(const char *blkdevs)
568 struct volume_info *vol = NULL;
573 if (blkdevs == NULL) {
574 errx(1, "A -f blkdevs specification is required "
578 copy = strdup(blkdevs);
579 while ((volname = copy) != NULL) {
580 if ((copy = strchr(copy, ':')) != NULL)
582 volname = getdevpath(volname, 0);
583 if (strchr(volname, ':'))
584 hammer_parsedevs(volname);
586 vol = load_volume(volname, O_RDONLY);
595 * All volumes have the same vol_count.
598 if (volnum != vol->ondisk->vol_count)
599 errx(1, "Volume header says %d volumes, but %d specified.",
600 vol->ondisk->vol_count, volnum);
602 if (get_root_volume() == NULL)
603 errx(1, "No root volume found");
608 sigalrm(int signo __unused)
610 /* do nothing (interrupts HAMMER ioctl) */
615 sigintr(int signo __unused)
617 if (RunningIoctl == 0)
620 /* do nothing (interrupts HAMMER ioctl) */
629 "hammer [-2ABFqrvXy] [-b bandwidth] [-C cachesize[:readahead]] \n"
630 " [-R restrictcmd] [-T restrictpath] [-c cyclefile]\n"
631 " [-e scoreboardfile] [-f blkdevs] [-i delay] [-p ssh-port]\n"
632 " [-S splitsize] [-t seconds] [-m memlimit] command [argument ...]\n"
633 "hammer synctid <filesystem> [quick]\n"
634 "hammer bstats [interval]\n"
635 "hammer iostats [interval]\n"
636 "hammer stats [interval]\n"
637 "hammer history[@offset[,len]] <file> ...\n"
638 "hammer namekey1 <path>\n"
639 "hammer namekey2 <path>\n"
640 "hammer namekey32 <path>\n"
641 "hammer cleanup [<filesystem> ...]\n"
642 "hammer abort-cleanup\n"
643 "hammer info [<dirpath> ...]\n"
644 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
645 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
646 "hammer prune <softlink-dir>\n"
647 "hammer prune-everything <filesystem>\n"
648 "hammer rebalance <filesystem> [saturation_percentage]\n"
649 "hammer reblock[-btree|-inodes|-dirs|-data] "
650 "<filesystem> [fill_percentage]\n"
651 "hammer pfs-status <dirpath> ...\n"
652 "hammer pfs-master <dirpath> [options]\n"
653 "hammer pfs-slave <dirpath> [options]\n"
654 "hammer pfs-update <dirpath> [options]\n"
655 "hammer pfs-upgrade <dirpath>\n"
656 "hammer pfs-downgrade <dirpath>\n"
657 "hammer pfs-destroy <dirpath>\n"
658 "hammer mirror-read <filesystem> [begin-tid]\n"
659 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
660 "hammer mirror-write <filesystem>\n"
661 "hammer mirror-dump [header]\n"
662 "hammer mirror-copy [[user@]host:]<filesystem>"
663 " [[user@]host:]<filesystem>\n"
664 "hammer mirror-stream [[user@]host:]<filesystem>"
665 " [[user@]host:]<filesystem>\n"
666 "hammer ssh-remote command filesystem\n"
667 "hammer version <filesystem>\n"
668 "hammer version-upgrade <filesystem> <version> [force]\n"
669 "hammer volume-add <device> <filesystem>\n"
670 "hammer volume-del <device> <filesystem>\n"
671 "hammer volume-list <filesystem>\n"
672 "hammer volume-blkdevs <filesystem>\n"
675 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
678 "hammer config [<filesystem> [<configfile>]]\n"
679 "hammer viconfig [<filesystem>]\n"
680 "hammer snap <path> [<note>]\n"
681 "hammer snaplo <path> [<note>]\n"
682 "hammer snapq <dir> [<note>]\n"
683 "hammer snaprm <path> ...\n"
684 "hammer snaprm <transid> ...\n"
685 "hammer snaprm <filesystem> <transid> ...\n"
686 "hammer snapls [<path> ...]\n"
689 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
692 "hammer -f blkdevs blockmap\n"
693 "hammer -f blkdevs checkmap\n"
694 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
695 "hammer -f blkdevs show-undo\n"
696 "hammer -f blkdevs recover <target_dir>\n"
699 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
702 "hammer dedup-simulate <filesystem>\n"
703 "hammer dedup <filesystem>\n"