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;
73 while ((ch = getopt(ac, av,
74 "b:c:de:hf:i:m:p:qrt:v2yABC:FR:S:T:X")) != -1) {
83 BandwidthOpt = strtoull(optarg, &ptr, 0);
98 /* bytes per second if no suffix */
105 SplitupOptStr = strdup(optarg);
106 SplitupOpt = strtoull(optarg, &ptr, 0);
121 /* bytes per second if no suffix */
134 ScoreBoardFile = optarg;
140 DelayOpt = strtol(optarg, NULL, 0);
143 MemoryLimit = strtouq(optarg, &ptr, 0);
166 if (MemoryLimit < 1024 * 1024)
167 MemoryLimit = 1024 * 1024;
179 TimeoutOpt = strtol(optarg, NULL, 0);
200 cacheSize = strtol(optarg, &ptr, 0);
213 /* bytes if no suffix */
219 UseReadAhead = strtol(ptr + 1, NULL, 0);
220 UseReadBehind = -UseReadAhead;
222 if (cacheSize < 1024 * 1024)
223 cacheSize = 1024 * 1024;
224 if (UseReadAhead < 0)
226 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
227 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
228 UseReadBehind = -UseReadAhead;
230 hammer_cache_set(cacheSize);
236 if (restrictcmd == NULL)
237 restrictcmd = optarg;
240 if (RestrictTarget == NULL)
241 RestrictTarget = optarg;
258 signal(SIGALRM, sigalrm);
259 signal(SIGINT, sigintr);
262 * Check command restriction (used by hammer ssh-remote). Several
263 * commands may be iterated with a comma.
268 ptr = strdup(restrictcmd);
269 while ((elm = strsep(&ptr, ",")) != NULL) {
270 if (strcmp(av[0], elm) == 0)
274 fprintf(stderr, "hammer-remote: request does not match "
275 "restricted command\n");
281 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
282 if (status != uuid_s_ok) {
283 errx(1, "uuids file does not have the DragonFly "
284 "HAMMER filesystem type");
290 if (strcmp(av[0], "synctid") == 0) {
291 hammer_cmd_synctid(av + 1, ac - 1);
294 if (strcmp(av[0], "namekey2") == 0) {
298 const char *aname = av[1];
303 key = (uint32_t)crc32(aname, len) & 0xFFFFFFFEU;
307 crcx = crc32(aname + 3, len - 5);
308 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
309 key |= (int64_t)(crcx & 0x3F) << 42;
315 key |= ((int64_t)(aname[2] & 0x1F) << 48);
318 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
319 ((int64_t)(aname[len-2] & 0x1F) << 37);
322 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
323 ((int64_t)(aname[len-1] & 0x1F) << 32);
329 key |= 0x100000000LL;
330 printf("0x%016jx\n", (uintmax_t)key);
333 if (strcmp(av[0], "namekey1") == 0) {
338 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
340 key |= 0x100000000LL;
341 printf("0x%016jx\n", (uintmax_t)key);
344 if (strcmp(av[0], "namekey32") == 0) {
349 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
352 printf("0x%08x\n", key);
355 if (strcmp(av[0], "pfs-status") == 0) {
356 hammer_cmd_pseudofs_status(av + 1, ac - 1);
359 if (strcmp(av[0], "pfs-master") == 0) {
360 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
363 if (strcmp(av[0], "pfs-slave") == 0) {
364 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
367 if (strcmp(av[0], "pfs-update") == 0) {
368 hammer_cmd_pseudofs_update(av + 1, ac - 1);
371 if (strcmp(av[0], "pfs-upgrade") == 0) {
372 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
375 if (strcmp(av[0], "pfs-downgrade") == 0) {
376 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
379 if (strcmp(av[0], "pfs-destroy") == 0) {
380 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
383 if (strcmp(av[0], "prune") == 0) {
384 hammer_cmd_softprune(av + 1, ac - 1, 0);
387 if (strcmp(av[0], "config") == 0) {
388 hammer_cmd_config(av + 1, ac - 1);
391 if (strcmp(av[0], "viconfig") == 0) {
392 hammer_cmd_viconfig(av + 1, ac - 1);
395 if (strcmp(av[0], "cleanup") == 0) {
396 hammer_cmd_cleanup(av + 1, ac - 1);
399 if (strcmp(av[0], "abort-cleanup") == 0) {
400 hammer_cmd_abort_cleanup(av + 1, ac - 1);
403 if (strcmp(av[0], "info") == 0) {
404 hammer_cmd_info(av + 1, ac - 1);
407 if (strcmp(av[0], "prune-everything") == 0) {
408 hammer_cmd_softprune(av + 1, ac - 1, 1);
411 if (strcmp(av[0], "ssh-remote") == 0) {
414 hammer_cmd_sshremote(av[1], av[2]);
417 if (strcmp(av[0], "snap") == 0) {
418 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
421 if (strcmp(av[0], "snaplo") == 0) {
422 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
425 if (strcmp(av[0], "snapq") == 0) {
426 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
429 if (strcmp(av[0], "snapls") == 0) {
430 hammer_cmd_snapls(av + 1, ac - 1);
433 if (strcmp(av[0], "snaprm") == 0) {
434 hammer_cmd_snaprm(av + 1, ac - 1);
437 if (strcmp(av[0], "snapshot") == 0) {
438 hammer_cmd_snapshot(av + 1, ac - 1);
441 if (strcmp(av[0], "bstats") == 0) {
442 hammer_cmd_bstats(av + 1, ac - 1);
445 if (strcmp(av[0], "iostats") == 0) {
446 hammer_cmd_iostats(av + 1, ac - 1);
449 if (strcmp(av[0], "stats") == 0) {
450 hammer_cmd_stats(av + 1, ac - 1);
454 if (strncmp(av[0], "history", 7) == 0) {
455 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
458 if (strcmp(av[0], "rebalance") == 0) {
459 signal(SIGINT, sigalrm);
460 hammer_cmd_rebalance(av + 1, ac - 1);
463 if (strncmp(av[0], "reblock", 7) == 0) {
464 signal(SIGINT, sigalrm);
465 if (strcmp(av[0], "reblock") == 0)
466 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_FLAGS);
467 else if (strcmp(av[0], "reblock-btree") == 0)
468 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
469 else if (strcmp(av[0], "reblock-inodes") == 0)
470 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
471 else if (strcmp(av[0], "reblock-dirs") == 0)
472 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
473 else if (strcmp(av[0], "reblock-data") == 0)
474 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
479 if (strncmp(av[0], "mirror", 6) == 0) {
480 if (strcmp(av[0], "mirror-read") == 0)
481 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
482 else if (strcmp(av[0], "mirror-read-stream") == 0)
483 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
484 else if (strcmp(av[0], "mirror-write") == 0)
485 hammer_cmd_mirror_write(av + 1, ac - 1);
486 else if (strcmp(av[0], "mirror-copy") == 0)
487 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
488 else if (strcmp(av[0], "mirror-stream") == 0)
489 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
490 else if (strcmp(av[0], "mirror-dump") == 0)
491 hammer_cmd_mirror_dump(av + 1, ac - 1);
496 if (strcmp(av[0], "dedup-simulate") == 0) {
497 hammer_cmd_dedup_simulate(av + 1, ac - 1);
500 if (strcmp(av[0], "dedup") == 0) {
501 hammer_cmd_dedup(av + 1, ac - 1);
504 if (strcmp(av[0], "version") == 0) {
505 hammer_cmd_get_version(av + 1, ac - 1);
508 if (strcmp(av[0], "version-upgrade") == 0) {
509 hammer_cmd_set_version(av + 1, ac - 1);
512 if (strcmp(av[0], "volume-add") == 0) {
513 hammer_cmd_volume_add(av + 1, ac - 1);
516 if (strcmp(av[0], "volume-del") == 0) {
517 hammer_cmd_volume_del(av + 1, ac - 1);
520 if (strcmp(av[0], "volume-list") == 0) {
521 hammer_cmd_volume_list(av + 1, ac - 1);
524 if (strcmp(av[0], "volume-blkdevs") == 0) {
525 hammer_cmd_volume_blkdevs(av + 1, ac - 1);
529 if (strcmp(av[0], "show") == 0) {
530 const char *arg = NULL;
535 hammer_parsedevs(blkdevs);
537 errx(1, "Too many options specified");
541 char *p, *buf = strdup(av[2]);
542 while((p = strsep(&buf, ",")) != NULL) {
543 if (strcmp(p, "filter") == 0) {
545 } else if (strcmp(p, "nofilter") == 0) {
547 } else if (strcmp(p, "obfuscate") == 0) {
549 } else if (strcmp(p, "indent") == 0) {
555 hammer_cmd_show(arg, filter, obfuscate, indent);
558 if (strcmp(av[0], "show-undo") == 0) {
559 hammer_parsedevs(blkdevs);
560 hammer_cmd_show_undo();
563 if (strcmp(av[0], "recover") == 0) {
564 hammer_parsedevs(blkdevs);
566 errx(1, "hammer recover required target directory");
567 hammer_cmd_recover(av[1]);
570 if (strcmp(av[0], "blockmap") == 0) {
571 hammer_parsedevs(blkdevs);
572 hammer_cmd_blockmap();
575 if (strcmp(av[0], "checkmap") == 0) {
576 hammer_parsedevs(blkdevs);
577 hammer_cmd_checkmap();
586 * Parse the device specification.
588 * Multi-volume hammer devices are colon-separated. Each element
589 * may be further expanded via /etc/devtab. One may also specify
590 * a single element which is expanded into multiple elements via
595 hammer_parsedevs(const char *blkdevs)
597 struct volume_info *vol = NULL;
602 if (blkdevs == NULL) {
603 errx(1, "A -f blkdevs specification is required "
607 copy = strdup(blkdevs);
608 while ((volname = copy) != NULL) {
609 if ((copy = strchr(copy, ':')) != NULL)
611 volname = getdevpath(volname, 0);
612 if (strchr(volname, ':'))
613 hammer_parsedevs(volname);
615 vol = load_volume(volname, O_RDONLY);
624 * All volumes have the same vol_count.
627 if (volnum != vol->ondisk->vol_count)
628 errx(1, "Volume header says %d volumes, but %d specified.",
629 vol->ondisk->vol_count, volnum);
631 if (get_root_volume() == NULL)
632 errx(1, "No root volume found");
637 sigalrm(int signo __unused)
639 /* do nothing (interrupts HAMMER ioctl) */
644 sigintr(int signo __unused)
646 if (RunningIoctl == 0)
649 /* do nothing (interrupts HAMMER ioctl) */
658 "hammer [-2ABFqrvXy] [-b bandwidth] [-C cachesize[:readahead]] \n"
659 " [-R restrictcmd] [-T restrictpath] [-c cyclefile]\n"
660 " [-e scoreboardfile] [-f blkdevs] [-i delay] [-p ssh-port]\n"
661 " [-S splitsize] [-t seconds] [-m memlimit] command [argument ...]\n"
662 "hammer synctid <filesystem> [quick]\n"
663 "hammer bstats [interval]\n"
664 "hammer iostats [interval]\n"
665 "hammer stats [interval]\n"
666 "hammer history[@offset[,len]] <file> ...\n"
667 "hammer namekey1 <path>\n"
668 "hammer namekey2 <path>\n"
669 "hammer namekey32 <path>\n"
670 "hammer cleanup [<filesystem> ...]\n"
671 "hammer abort-cleanup\n"
672 "hammer info [<dirpath> ...]\n"
673 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
674 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
675 "hammer prune <softlink-dir>\n"
676 "hammer prune-everything <filesystem>\n"
677 "hammer rebalance <filesystem> [saturation_percentage]\n"
678 "hammer reblock[-btree|-inodes|-dirs|-data] "
679 "<filesystem> [fill_percentage]\n"
680 "hammer pfs-status <dirpath> ...\n"
681 "hammer pfs-master <dirpath> [options]\n"
682 "hammer pfs-slave <dirpath> [options]\n"
683 "hammer pfs-update <dirpath> [options]\n"
684 "hammer pfs-upgrade <dirpath>\n"
685 "hammer pfs-downgrade <dirpath>\n"
686 "hammer pfs-destroy <dirpath>\n"
687 "hammer mirror-read <filesystem> [begin-tid]\n"
688 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
689 "hammer mirror-write <filesystem>\n"
690 "hammer mirror-dump [header]\n"
691 "hammer mirror-copy [[user@]host:]<filesystem>"
692 " [[user@]host:]<filesystem>\n"
693 "hammer mirror-stream [[user@]host:]<filesystem>"
694 " [[user@]host:]<filesystem>\n"
695 "hammer ssh-remote command filesystem\n"
696 "hammer version <filesystem>\n"
697 "hammer version-upgrade <filesystem> <version> [force]\n"
698 "hammer volume-add <device> <filesystem>\n"
699 "hammer volume-del <device> <filesystem>\n"
700 "hammer volume-list <filesystem>\n"
701 "hammer volume-blkdevs <filesystem>\n"
704 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
707 "hammer config [<filesystem> [<configfile>]]\n"
708 "hammer viconfig [<filesystem>]\n"
709 "hammer snap <path> [<note>]\n"
710 "hammer snaplo <path> [<note>]\n"
711 "hammer snapq <dir> [<note>]\n"
712 "hammer snaprm <path> ...\n"
713 "hammer snaprm <transid> ...\n"
714 "hammer snaprm <filesystem> <transid> ...\n"
715 "hammer snapls [<path> ...]\n"
718 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
721 "hammer -f blkdevs blockmap\n"
722 "hammer -f blkdevs checkmap\n"
723 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
724 "hammer -f blkdevs show-undo\n"
725 "hammer -f blkdevs recover <target_dir>\n"
728 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
731 "hammer dedup-simulate <filesystem>\n"
732 "hammer dedup <filesystem>\n"