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;
62 const char *RestrictTarget;
65 main(int ac, char **av)
69 char *restrictcmd = NULL;
74 while ((ch = getopt(ac, av,
75 "b:c:de:hf:i:m:p:qrt:v2yABC:FR:S:T:X")) != -1) {
84 BandwidthOpt = strtoull(optarg, &ptr, 0);
99 /* bytes per second if no suffix */
106 SplitupOptStr = strdup(optarg);
107 SplitupOpt = strtoull(optarg, &ptr, 0);
122 /* bytes per second if no suffix */
135 ScoreBoardFile = optarg;
141 DelayOpt = strtol(optarg, NULL, 0);
144 MemoryLimit = strtouq(optarg, &ptr, 0);
167 if (MemoryLimit < 1024 * 1024)
168 MemoryLimit = 1024 * 1024;
180 TimeoutOpt = strtol(optarg, NULL, 0);
201 cacheSize = strtol(optarg, &ptr, 0);
214 /* bytes if no suffix */
220 UseReadAhead = strtol(ptr + 1, NULL, 0);
221 UseReadBehind = -UseReadAhead;
223 if (cacheSize < 1024 * 1024)
224 cacheSize = 1024 * 1024;
225 if (UseReadAhead < 0)
227 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
228 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
229 UseReadBehind = -UseReadAhead;
231 hammer_cache_set(cacheSize);
237 if (restrictcmd == NULL)
238 restrictcmd = optarg;
241 if (RestrictTarget == NULL)
242 RestrictTarget = optarg;
259 signal(SIGALRM, sigalrm);
260 signal(SIGINT, sigintr);
263 * Check command restriction (used by hammer ssh-remote). Several
264 * commands may be iterated with a comma.
269 ptr = strdup(restrictcmd);
270 while ((elm = strsep(&ptr, ",")) != NULL) {
271 if (strcmp(av[0], elm) == 0)
275 fprintf(stderr, "hammer-remote: request does not match "
276 "restricted command\n");
285 if (strcmp(av[0], "synctid") == 0) {
286 hammer_cmd_synctid(av + 1, ac - 1);
289 if (strcmp(av[0], "namekey2") == 0) {
293 const char *aname = av[1];
298 key = (uint32_t)crc32(aname, len) & 0xFFFFFFFEU;
302 crcx = crc32(aname + 3, len - 5);
303 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
304 key |= (int64_t)(crcx & 0x3F) << 42;
310 key |= ((int64_t)(aname[2] & 0x1F) << 48);
313 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
314 ((int64_t)(aname[len-2] & 0x1F) << 37);
317 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
318 ((int64_t)(aname[len-1] & 0x1F) << 32);
324 key |= 0x100000000LL;
325 printf("0x%016jx\n", (uintmax_t)key);
328 if (strcmp(av[0], "namekey1") == 0) {
333 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
335 key |= 0x100000000LL;
336 printf("0x%016jx\n", (uintmax_t)key);
339 if (strcmp(av[0], "namekey32") == 0) {
344 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
347 printf("0x%08x\n", key);
350 if (strcmp(av[0], "pfs-status") == 0) {
351 hammer_cmd_pseudofs_status(av + 1, ac - 1);
354 if (strcmp(av[0], "pfs-master") == 0) {
355 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
358 if (strcmp(av[0], "pfs-slave") == 0) {
359 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
362 if (strcmp(av[0], "pfs-update") == 0) {
363 hammer_cmd_pseudofs_update(av + 1, ac - 1);
366 if (strcmp(av[0], "pfs-upgrade") == 0) {
367 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
370 if (strcmp(av[0], "pfs-downgrade") == 0) {
371 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
374 if (strcmp(av[0], "pfs-destroy") == 0) {
375 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
378 if (strcmp(av[0], "prune") == 0) {
379 hammer_cmd_softprune(av + 1, ac - 1, 0);
382 if (strcmp(av[0], "config") == 0) {
383 hammer_cmd_config(av + 1, ac - 1);
386 if (strcmp(av[0], "viconfig") == 0) {
387 hammer_cmd_viconfig(av + 1, ac - 1);
390 if (strcmp(av[0], "cleanup") == 0) {
391 hammer_cmd_cleanup(av + 1, ac - 1);
394 if (strcmp(av[0], "abort-cleanup") == 0) {
395 hammer_cmd_abort_cleanup(av + 1, ac - 1);
398 if (strcmp(av[0], "info") == 0) {
399 hammer_cmd_info(av + 1, ac - 1);
402 if (strcmp(av[0], "prune-everything") == 0) {
403 hammer_cmd_softprune(av + 1, ac - 1, 1);
406 if (strcmp(av[0], "ssh-remote") == 0) {
409 hammer_cmd_sshremote(av[1], av[2]);
412 if (strcmp(av[0], "snap") == 0) {
413 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
416 if (strcmp(av[0], "snaplo") == 0) {
417 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
420 if (strcmp(av[0], "snapq") == 0) {
421 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
424 if (strcmp(av[0], "snapls") == 0) {
425 hammer_cmd_snapls(av + 1, ac - 1);
428 if (strcmp(av[0], "snaprm") == 0) {
429 hammer_cmd_snaprm(av + 1, ac - 1);
432 if (strcmp(av[0], "snapshot") == 0) {
433 hammer_cmd_snapshot(av + 1, ac - 1);
436 if (strcmp(av[0], "bstats") == 0) {
437 hammer_cmd_bstats(av + 1, ac - 1);
440 if (strcmp(av[0], "iostats") == 0) {
441 hammer_cmd_iostats(av + 1, ac - 1);
444 if (strcmp(av[0], "stats") == 0) {
445 hammer_cmd_stats(av + 1, ac - 1);
449 if (strncmp(av[0], "history", 7) == 0) {
450 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
453 if (strcmp(av[0], "rebalance") == 0) {
454 signal(SIGINT, sigalrm);
455 hammer_cmd_rebalance(av + 1, ac - 1);
458 if (strncmp(av[0], "reblock", 7) == 0) {
459 signal(SIGINT, sigalrm);
460 if (strcmp(av[0], "reblock") == 0)
461 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_FLAGS);
462 else if (strcmp(av[0], "reblock-btree") == 0)
463 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
464 else if (strcmp(av[0], "reblock-inodes") == 0)
465 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
466 else if (strcmp(av[0], "reblock-dirs") == 0)
467 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
468 else if (strcmp(av[0], "reblock-data") == 0)
469 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
474 if (strncmp(av[0], "mirror", 6) == 0) {
475 if (strcmp(av[0], "mirror-read") == 0)
476 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
477 else if (strcmp(av[0], "mirror-read-stream") == 0)
478 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
479 else if (strcmp(av[0], "mirror-write") == 0)
480 hammer_cmd_mirror_write(av + 1, ac - 1);
481 else if (strcmp(av[0], "mirror-copy") == 0)
482 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
483 else if (strcmp(av[0], "mirror-stream") == 0)
484 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
485 else if (strcmp(av[0], "mirror-dump") == 0)
486 hammer_cmd_mirror_dump(av + 1, ac - 1);
491 if (strcmp(av[0], "dedup-simulate") == 0) {
492 hammer_cmd_dedup_simulate(av + 1, ac - 1);
495 if (strcmp(av[0], "dedup") == 0) {
496 hammer_cmd_dedup(av + 1, ac - 1);
499 if (strcmp(av[0], "version") == 0) {
500 hammer_cmd_get_version(av + 1, ac - 1);
503 if (strcmp(av[0], "version-upgrade") == 0) {
504 hammer_cmd_set_version(av + 1, ac - 1);
507 if (strcmp(av[0], "volume-add") == 0) {
508 hammer_cmd_volume_add(av + 1, ac - 1);
511 if (strcmp(av[0], "volume-del") == 0) {
512 hammer_cmd_volume_del(av + 1, ac - 1);
515 if (strcmp(av[0], "volume-list") == 0) {
516 hammer_cmd_volume_list(av + 1, ac - 1, av[0]);
519 if (strcmp(av[0], "volume-blkdevs") == 0) {
520 hammer_cmd_volume_blkdevs(av + 1, ac - 1, av[0]);
524 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
525 if (status != uuid_s_ok) {
526 errx(1, "uuids file does not have the DragonFly "
527 "HAMMER filesystem type");
530 if (strcmp(av[0], "show") == 0) {
531 const char *arg = NULL;
534 hammer_parsedevs(blkdevs);
538 if (strcmp(av[2], "filter") == 0)
540 else if (strcmp(av[2], "nofilter") == 0)
543 hammer_cmd_show(-1, arg, filter, 0, NULL, NULL);
546 if (strcmp(av[0], "show-undo") == 0) {
547 hammer_parsedevs(blkdevs);
548 hammer_cmd_show_undo();
551 if (strcmp(av[0], "recover") == 0) {
552 hammer_parsedevs(blkdevs);
554 errx(1, "hammer recover required target directory");
555 hammer_cmd_recover(av[1]);
558 if (strcmp(av[0], "blockmap") == 0) {
559 hammer_parsedevs(blkdevs);
560 hammer_cmd_blockmap();
563 if (strcmp(av[0], "checkmap") == 0) {
564 hammer_parsedevs(blkdevs);
565 hammer_cmd_checkmap();
574 * Parse the device specification.
576 * Multi-volume hammer devices are colon-separated. Each element
577 * may be further expanded via /etc/devtab. One may also specify
578 * a single element which is expanded into multiple elements via
583 hammer_parsedevs(const char *blkdevs)
585 struct volume_info *vol = NULL;
590 if (blkdevs == NULL) {
591 errx(1, "A -f blkdevs specification is required "
595 copy = strdup(blkdevs);
596 while ((volname = copy) != NULL) {
597 if ((copy = strchr(copy, ':')) != NULL)
599 volname = getdevpath(volname, 0);
600 if (strchr(volname, ':'))
601 hammer_parsedevs(volname);
603 vol = setup_volume(-1, volname, 0, O_RDONLY);
612 * All volumes have the same vol_count.
615 if (volnum != vol->ondisk->vol_count)
616 errx(1, "Volume header says %d volumes, but %d specified.",
617 vol->ondisk->vol_count, volnum);
619 assert(NumVolumes == 0);
625 sigalrm(int signo __unused)
627 /* do nothing (interrupts HAMMER ioctl) */
632 sigintr(int signo __unused)
634 if (RunningIoctl == 0)
637 /* do nothing (interrupts HAMMER ioctl) */
646 "hammer [-2ABFqrvXy] [-b bandwidth] [-C cachesize[:readahead]] \n"
647 " [-R restrictcmd] [-T restrictpath] [-c cyclefile]\n"
648 " [-e scoreboardfile] [-f blkdevs] [-i delay] [-p ssh-port]\n"
649 " [-S splitsize] [-t seconds] [-m memlimit] command [argument ...]\n"
650 "hammer synctid <filesystem> [quick]\n"
651 "hammer bstats [interval]\n"
652 "hammer iostats [interval]\n"
653 "hammer stats [interval]\n"
654 "hammer history[@offset[,len]] <file> ...\n"
655 "hammer namekey1 <path>\n"
656 "hammer namekey2 <path>\n"
657 "hammer namekey32 <path>\n"
658 "hammer cleanup [<filesystem> ...]\n"
659 "hammer abort-cleanup\n"
660 "hammer info [<dirpath> ...]\n"
661 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
662 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
663 "hammer prune <softlink-dir>\n"
664 "hammer prune-everything <filesystem>\n"
665 "hammer rebalance <filesystem> [saturation_percentage]\n"
666 "hammer reblock[-btree|-inodes|-dirs|-data] "
667 "<filesystem> [fill_percentage]\n"
668 "hammer pfs-status <dirpath> ...\n"
669 "hammer pfs-master <dirpath> [options]\n"
670 "hammer pfs-slave <dirpath> [options]\n"
671 "hammer pfs-update <dirpath> [options]\n"
672 "hammer pfs-upgrade <dirpath>\n"
673 "hammer pfs-downgrade <dirpath>\n"
674 "hammer pfs-destroy <dirpath>\n"
675 "hammer mirror-read <filesystem> [begin-tid]\n"
676 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
677 "hammer mirror-write <filesystem>\n"
678 "hammer mirror-dump [header]\n"
679 "hammer mirror-copy [[user@]host:]<filesystem>"
680 " [[user@]host:]<filesystem>\n"
681 "hammer mirror-stream [[user@]host:]<filesystem>"
682 " [[user@]host:]<filesystem>\n"
683 "hammer ssh-remote command filesystem\n"
684 "hammer version <filesystem>\n"
685 "hammer version-upgrade <filesystem> <version> [force]\n"
686 "hammer volume-add <device> <filesystem>\n"
687 "hammer volume-del <device> <filesystem>\n"
688 "hammer volume-list <filesystem>\n"
689 "hammer volume-blkdevs <filesystem>\n"
692 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
695 "hammer config [<filesystem> [<configfile>]]\n"
696 "hammer viconfig [<filesystem>]\n"
697 "hammer snap <path> [<note>]\n"
698 "hammer snaplo <path> [<note>]\n"
699 "hammer snapq <dir> [<note>]\n"
700 "hammer snaprm <path> ...\n"
701 "hammer snaprm <transid> ...\n"
702 "hammer snaprm <filesystem> <transid> ...\n"
703 "hammer snapls [<path> ...]\n"
706 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
709 "hammer -f blkdevs blockmap\n"
710 "hammer -f blkdevs checkmap\n"
711 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
712 "hammer -f blkdevs show-undo\n"
713 "hammer -f blkdevs recover <target_dir>\n"
716 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
719 "hammer dedup-simulate <filesystem>\n"
720 "hammer dedup <filesystem>\n"