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
39 static __inline void hammer_parse_blkdevs(const char *blkdevs, int oflags);
40 static void __hammer_parse_blkdevs(const char *blkdevs, int oflags,
41 int verify_volume, int verify_count);
42 static void sigalrm(int signo);
43 static void sigintr(int signo);
44 static void usage(int exit_code) __dead2;
60 uint64_t BandwidthOpt;
61 uint64_t SplitupOpt = 4ULL * 1024ULL * 1024ULL * 1024ULL;
62 uint64_t MemoryLimit = 1024LLU * 1024 * 1024;
63 const char *SplitupOptStr;
64 const char *CyclePath;
67 main(int ac, char **av)
71 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 */
107 SplitupOptStr = strdup(optarg);
108 SplitupOpt = strtoull(optarg, &ptr, 0);
123 /* bytes per second if no suffix */
137 ScoreBoardFile = optarg;
143 DelayOpt = strtol(optarg, NULL, 0);
146 MemoryLimit = strtouq(optarg, &ptr, 0);
169 if (MemoryLimit < 1024 * 1024)
170 MemoryLimit = 1024 * 1024;
182 TimeoutOpt = strtol(optarg, NULL, 0);
203 if (hammer_parse_cache_size(optarg) == -1) {
212 if (restrictcmd == NULL)
213 restrictcmd = optarg;
216 if (RestrictTarget == NULL)
217 RestrictTarget = optarg;
234 signal(SIGALRM, sigalrm);
235 signal(SIGINT, sigintr);
238 * Check command restriction (used by hammer ssh-remote). Several
239 * commands may be iterated with a comma.
244 dup = ptr = strdup(restrictcmd);
245 while ((elm = strsep(&ptr, ",")) != NULL) {
246 if (strcmp(av[0], elm) == 0)
250 errx(1, "hammer-remote: request does not match "
251 "restricted command");
258 * Lookup the filesystem type
260 if (hammer_uuid_name_lookup(&Hammer_FSType, HAMMER_FSTYPE_STRING)) {
261 errx(1, "uuids file does not have the DragonFly "
262 "HAMMER filesystem type");
269 if (strcmp(av[0], "synctid") == 0) {
270 hammer_cmd_synctid(av + 1, ac - 1);
273 if (strcmp(av[0], "namekey2") == 0) {
277 const char *aname = av[1];
284 key = (uint32_t)crc32(aname, len) & 0xFFFFFFFEU;
288 crcx = crc32(aname + 3, len - 5);
289 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
290 key |= (int64_t)(crcx & 0x3F) << 42;
296 key |= ((int64_t)(aname[2] & 0x1F) << 48);
299 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
300 ((int64_t)(aname[len-2] & 0x1F) << 37);
303 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
304 ((int64_t)(aname[len-1] & 0x1F) << 32);
310 key |= 0x100000000LL;
311 printf("0x%016jx\n", (uintmax_t)key);
314 if (strcmp(av[0], "namekey1") == 0) {
321 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
323 key |= 0x100000000LL;
324 printf("0x%016jx\n", (uintmax_t)key);
327 if (strcmp(av[0], "namekey32") == 0) {
334 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
337 printf("0x%08x\n", key);
340 if (strcmp(av[0], "pfs-status") == 0) {
341 hammer_cmd_pseudofs_status(av + 1, ac - 1);
344 if (strcmp(av[0], "pfs-master") == 0) {
345 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
348 if (strcmp(av[0], "pfs-slave") == 0) {
349 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
352 if (strcmp(av[0], "pfs-update") == 0) {
353 hammer_cmd_pseudofs_update(av + 1, ac - 1);
356 if (strcmp(av[0], "pfs-upgrade") == 0) {
357 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
360 if (strcmp(av[0], "pfs-downgrade") == 0) {
361 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
364 if (strcmp(av[0], "pfs-destroy") == 0) {
365 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
368 if (strcmp(av[0], "prune") == 0) {
369 hammer_cmd_softprune(av + 1, ac - 1, 0);
372 if (strcmp(av[0], "prune-everything") == 0) {
373 hammer_cmd_softprune(av + 1, ac - 1, 1);
376 if (strcmp(av[0], "config") == 0) {
377 hammer_cmd_config(av + 1, ac - 1);
380 if (strcmp(av[0], "viconfig") == 0) {
381 hammer_cmd_viconfig(av + 1, ac - 1);
384 if (strcmp(av[0], "cleanup") == 0) {
385 hammer_cmd_cleanup(av + 1, ac - 1);
388 if (strcmp(av[0], "abort-cleanup") == 0) {
389 hammer_cmd_abort_cleanup(av + 1, ac - 1);
392 if (strcmp(av[0], "info") == 0) {
393 hammer_cmd_info(av + 1, ac - 1);
396 if (strcmp(av[0], "ssh-remote") == 0) {
401 hammer_cmd_sshremote(av[1], av[2]);
404 if (strcmp(av[0], "snap") == 0) {
405 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
408 if (strcmp(av[0], "snaplo") == 0) {
409 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
412 if (strcmp(av[0], "snapq") == 0) {
413 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
416 if (strcmp(av[0], "snapls") == 0) {
417 hammer_cmd_snapls(av + 1, ac - 1);
420 if (strcmp(av[0], "snaprm") == 0) {
421 hammer_cmd_snaprm(av + 1, ac - 1);
424 if (strcmp(av[0], "snapshot") == 0) {
425 hammer_cmd_snapshot(av + 1, ac - 1);
428 if (strcmp(av[0], "bstats") == 0) {
429 hammer_cmd_bstats(av + 1, ac - 1);
432 if (strcmp(av[0], "iostats") == 0) {
433 hammer_cmd_iostats(av + 1, ac - 1);
436 if (strcmp(av[0], "stats") == 0) {
437 hammer_cmd_stats(av + 1, ac - 1);
441 if (strncmp(av[0], "history", 7) == 0) {
442 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
445 if (strcmp(av[0], "rebalance") == 0) {
446 signal(SIGINT, sigalrm);
447 hammer_cmd_rebalance(av + 1, ac - 1);
450 if (strncmp(av[0], "reblock", 7) == 0) {
451 signal(SIGINT, sigalrm);
452 if (strcmp(av[0], "reblock") == 0)
453 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_FLAGS);
454 else if (strcmp(av[0], "reblock-btree") == 0)
455 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
456 else if (strcmp(av[0], "reblock-inodes") == 0)
457 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
458 else if (strcmp(av[0], "reblock-dirs") == 0)
459 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
460 else if (strcmp(av[0], "reblock-data") == 0)
461 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
468 if (strncmp(av[0], "mirror", 6) == 0) {
469 if (strcmp(av[0], "mirror-read") == 0)
470 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
471 else if (strcmp(av[0], "mirror-read-stream") == 0)
472 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
473 else if (strcmp(av[0], "mirror-write") == 0)
474 hammer_cmd_mirror_write(av + 1, ac - 1);
475 else if (strcmp(av[0], "mirror-copy") == 0)
476 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
477 else if (strcmp(av[0], "mirror-stream") == 0)
478 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
479 else if (strcmp(av[0], "mirror-dump") == 0)
480 hammer_cmd_mirror_dump(av + 1, ac - 1);
487 if (strcmp(av[0], "dedup-simulate") == 0) {
488 hammer_cmd_dedup_simulate(av + 1, ac - 1);
491 if (strcmp(av[0], "dedup") == 0) {
492 hammer_cmd_dedup(av + 1, ac - 1);
495 if (strcmp(av[0], "version") == 0) {
496 hammer_cmd_get_version(av + 1, ac - 1);
499 if (strcmp(av[0], "version-upgrade") == 0) {
500 hammer_cmd_set_version(av + 1, ac - 1);
503 if (strcmp(av[0], "volume-add") == 0) {
504 hammer_cmd_volume_add(av + 1, ac - 1);
507 if (strcmp(av[0], "volume-del") == 0) {
508 hammer_cmd_volume_del(av + 1, ac - 1);
511 if (strcmp(av[0], "volume-list") == 0) {
512 hammer_cmd_volume_list(av + 1, ac - 1);
515 if (strcmp(av[0], "volume-blkdevs") == 0) {
516 hammer_cmd_volume_blkdevs(av + 1, ac - 1);
520 if (strcmp(av[0], "show") == 0) {
521 const char *arg = NULL;
527 hammer_parse_blkdevs(blkdevs, O_RDONLY);
529 errx(1, "Too many options specified");
535 dup = ptr = strdup(av[2]);
536 while ((p = strsep(&ptr, ",")) != NULL) {
537 if (strcmp(p, "filter") == 0)
539 else if (strcmp(p, "nofilter") == 0)
541 else if (strcmp(p, "obfuscate") == 0)
543 else if (strcmp(p, "indent") == 0)
548 hammer_cmd_show(arg, filter, obfuscate, indent);
551 if (strcmp(av[0], "show-undo") == 0) {
552 hammer_parse_blkdevs(blkdevs, O_RDONLY);
553 hammer_cmd_show_undo();
556 if (strcmp(av[0], "recover") == 0) {
557 __hammer_parse_blkdevs(blkdevs, O_RDONLY, 0, 1);
558 hammer_cmd_recover(av + 1, ac - 1);
561 if (strcmp(av[0], "blockmap") == 0) {
562 hammer_parse_blkdevs(blkdevs, O_RDONLY);
563 hammer_cmd_blockmap();
566 if (strcmp(av[0], "checkmap") == 0) {
567 hammer_parse_blkdevs(blkdevs, O_RDONLY);
568 hammer_cmd_checkmap();
571 if (strcmp(av[0], "strip") == 0) {
572 __hammer_parse_blkdevs(blkdevs, O_RDWR, 0, 0);
583 * Parse the device specification.
585 * Multi-volume hammer devices are colon-separated. Each element
586 * may be further expanded via /etc/devtab. One may also specify
587 * a single element which is expanded into multiple elements via
592 __hammer_parse_blkdevs(const char *blkdevs, int oflags, int verify_volume,
595 volume_info_t volume = NULL;
600 if (blkdevs == NULL) {
601 errx(1, "A -f blkdevs specification is required "
606 copy = strdup(blkdevs);
607 while ((volname = copy) != NULL) {
608 if ((copy = strchr(copy, ':')) != NULL)
610 volname = getdevpath(volname, 0);
611 if (strchr(volname, ':')) {
612 __hammer_parse_blkdevs(volname, oflags, verify_volume,
615 volume = load_volume(volname, oflags, verify_volume);
625 if (vol_count != volume->ondisk->vol_count) {
626 errx(1, "Volume header says %d volumes, but %d specified.",
627 volume->ondisk->vol_count, vol_count);
630 if (get_root_volume() == NULL) {
631 errx(1, "No root volume found");
639 hammer_parse_blkdevs(const char *blkdevs, int oflags)
641 __hammer_parse_blkdevs(blkdevs, oflags, 1, 1);
646 sigalrm(int signo __unused)
648 /* do nothing (interrupts HAMMER ioctl) */
653 sigintr(int signo __unused)
655 if (RunningIoctl == 0)
658 /* do nothing (interrupts HAMMER ioctl) */
667 "hammer [-2ABFqrvXy] [-b bandwidth] [-C cachesize[:readahead]] \n"
668 " [-R restrictcmd] [-T restrictpath] [-c cyclefile]\n"
669 " [-e scoreboardfile] [-f blkdevs] [-i delay] [-p ssh-port]\n"
670 " [-S splitsize] [-t seconds] [-m memlimit] command [argument ...]\n"
671 "hammer synctid <filesystem> [quick]\n"
672 "hammer bstats [interval]\n"
673 "hammer iostats [interval]\n"
674 "hammer stats [interval]\n"
675 "hammer history[@offset[,len]] <file> ...\n"
676 "hammer namekey1 <path>\n"
677 "hammer namekey2 <path>\n"
678 "hammer namekey32 <path>\n"
679 "hammer cleanup [<filesystem> ...]\n"
680 "hammer abort-cleanup\n"
681 "hammer info [<dirpath> ...]\n"
682 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
683 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
684 "hammer prune <softlink-dir>\n"
685 "hammer prune-everything <filesystem>\n"
686 "hammer rebalance <filesystem> [saturation_percentage]\n"
687 "hammer reblock[-btree|-inodes|-dirs|-data] "
688 "<filesystem> [fill_percentage]\n"
689 "hammer pfs-status <dirpath> ...\n"
690 "hammer pfs-master <dirpath> [options]\n"
691 "hammer pfs-slave <dirpath> [options]\n"
692 "hammer pfs-update <dirpath> [options]\n"
693 "hammer pfs-upgrade <dirpath>\n"
694 "hammer pfs-downgrade <dirpath>\n"
695 "hammer pfs-destroy <dirpath>\n"
696 "hammer mirror-read <filesystem> [begin-tid]\n"
697 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
698 "hammer mirror-write <filesystem>\n"
699 "hammer mirror-dump [header]\n"
700 "hammer mirror-copy [[user@]host:]<filesystem>"
701 " [[user@]host:]<filesystem>\n"
702 "hammer mirror-stream [[user@]host:]<filesystem>"
703 " [[user@]host:]<filesystem>\n"
704 "hammer ssh-remote command filesystem\n"
705 "hammer version <filesystem>\n"
706 "hammer version-upgrade <filesystem> <version> [force]\n"
707 "hammer volume-add <device> <filesystem>\n"
708 "hammer volume-del <device> <filesystem>\n"
709 "hammer volume-list <filesystem>\n"
710 "hammer volume-blkdevs <filesystem>\n"
713 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
716 "hammer config [<filesystem> [<configfile>]]\n"
717 "hammer viconfig [<filesystem>]\n"
718 "hammer snap <path> [<note>]\n"
719 "hammer snaplo <path> [<note>]\n"
720 "hammer snapq <dir> [<note>]\n"
721 "hammer snaprm <path> ...\n"
722 "hammer snaprm <transid> ...\n"
723 "hammer snaprm <filesystem> <transid> ...\n"
724 "hammer snapls [<path> ...]\n"
727 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
730 "hammer -f blkdevs blockmap\n"
731 "hammer -f blkdevs checkmap\n"
732 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
733 "hammer -f blkdevs show-undo\n"
734 "hammer -f blkdevs recover <target_dir> [full|quick]\n"
735 "hammer -f blkdevs strip\n"
738 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
741 "hammer dedup-simulate <filesystem>\n"
742 "hammer dedup <filesystem>\n"