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
40 static void hammer_parsedevs(const char *blkdevs);
41 static void sigalrm(int signo);
42 static void sigintr(int signo);
43 static void usage(int exit_code);
60 u_int64_t BandwidthOpt;
61 u_int64_t SplitupOpt = 4ULL * 1024ULL * 1024ULL * 1024ULL;
62 u_int64_t MemoryLimit = 1024LLU * 1024 * 1024;
63 const char *SplitupOptStr;
64 const char *CyclePath;
66 const char *RestrictTarget;
69 main(int ac, char **av)
73 char *restrictcmd = NULL;
78 while ((ch = getopt(ac, av,
79 "b:c:de:hf:i:m:p:qrs:t:v2yABC:FR:S:T:X")) != -1) {
88 BandwidthOpt = strtoull(optarg, &ptr, 0);
100 BandwidthOpt *= 1024;
103 /* bytes per second if no suffix */
110 SplitupOptStr = strdup(optarg);
111 SplitupOpt = strtoull(optarg, &ptr, 0);
126 /* bytes per second if no suffix */
139 ScoreBoardFile = optarg;
145 DelayOpt = strtol(optarg, NULL, 0);
148 MemoryLimit = strtouq(optarg, &ptr, 0);
171 if (MemoryLimit < 1024 * 1024)
172 MemoryLimit = 1024 * 1024;
187 TimeoutOpt = strtol(optarg, NULL, 0);
208 cacheSize = strtol(optarg, &ptr, 0);
221 /* bytes if no suffix */
227 UseReadAhead = strtol(ptr + 1, NULL, 0);
228 UseReadBehind = -UseReadAhead;
230 if (cacheSize < 1024 * 1024)
231 cacheSize = 1024 * 1024;
232 if (UseReadAhead < 0)
234 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
235 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
236 UseReadBehind = -UseReadAhead;
238 hammer_cache_set(cacheSize);
244 if (restrictcmd == NULL)
245 restrictcmd = optarg;
248 if (RestrictTarget == NULL)
249 RestrictTarget = optarg;
266 signal(SIGALRM, sigalrm);
267 signal(SIGINT, sigintr);
270 * Check command restriction (used by hammer ssh-remote). Several
271 * commands may be iterated with a comma.
276 ptr = strdup(restrictcmd);
277 while ((elm = strsep(&ptr, ",")) != NULL) {
278 if (strcmp(av[0], elm) == 0)
282 fprintf(stderr, "hammer-remote: request does not match "
283 "restricted command\n");
292 if (strcmp(av[0], "synctid") == 0) {
293 hammer_cmd_synctid(av + 1, ac - 1);
296 if (strcmp(av[0], "namekey2") == 0) {
300 const char *aname = av[1];
305 key = (u_int32_t)crc32(aname, len) & 0xFFFFFFFEU;
309 crcx = crc32(aname + 3, len - 5);
310 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
311 key |= (int64_t)(crcx & 0x3F) << 42;
317 key |= ((int64_t)(aname[2] & 0x1F) << 48);
320 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
321 ((int64_t)(aname[len-2] & 0x1F) << 37);
324 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
325 ((int64_t)(aname[len-1] & 0x1F) << 32);
331 key |= 0x100000000LL;
332 printf("0x%016jx\n", (uintmax_t)key);
335 if (strcmp(av[0], "namekey1") == 0) {
340 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
342 key |= 0x100000000LL;
343 printf("0x%016jx\n", (uintmax_t)key);
346 if (strcmp(av[0], "namekey32") == 0) {
351 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
354 printf("0x%08x\n", key);
357 if (strcmp(av[0], "pfs-status") == 0) {
358 hammer_cmd_pseudofs_status(av + 1, ac - 1);
361 if (strcmp(av[0], "pfs-master") == 0) {
362 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
365 if (strcmp(av[0], "pfs-slave") == 0) {
366 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
369 if (strcmp(av[0], "pfs-update") == 0) {
370 hammer_cmd_pseudofs_update(av + 1, ac - 1);
373 if (strcmp(av[0], "pfs-upgrade") == 0) {
374 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
377 if (strcmp(av[0], "pfs-downgrade") == 0) {
378 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
381 if (strcmp(av[0], "pfs-destroy") == 0) {
382 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
385 if (strcmp(av[0], "prune") == 0) {
386 hammer_cmd_softprune(av + 1, ac - 1, 0);
389 if (strcmp(av[0], "config") == 0) {
390 hammer_cmd_config(av + 1, ac - 1);
393 if (strcmp(av[0], "viconfig") == 0) {
394 hammer_cmd_viconfig(av + 1, ac - 1);
397 if (strcmp(av[0], "cleanup") == 0) {
398 hammer_cmd_cleanup(av + 1, ac - 1);
401 if (strcmp(av[0], "abort-cleanup") == 0) {
402 hammer_cmd_abort_cleanup(av + 1, ac - 1);
405 if (strcmp(av[0], "info") == 0) {
406 hammer_cmd_info(av + 1, ac - 1);
409 if (strcmp(av[0], "prune-everything") == 0) {
410 hammer_cmd_softprune(av + 1, ac - 1, 1);
413 if (strcmp(av[0], "ssh-remote") == 0) {
416 hammer_cmd_sshremote(av[1], av[2]);
419 if (strcmp(av[0], "snap") == 0) {
420 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
423 if (strcmp(av[0], "snaplo") == 0) {
424 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
427 if (strcmp(av[0], "snapq") == 0) {
428 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
431 if (strcmp(av[0], "snapls") == 0) {
432 hammer_cmd_snapls(av + 1, ac - 1);
435 if (strcmp(av[0], "snaprm") == 0) {
436 hammer_cmd_snaprm(av + 1, ac - 1);
439 if (strcmp(av[0], "snapshot") == 0) {
440 hammer_cmd_snapshot(av + 1, ac - 1);
443 if (strcmp(av[0], "bstats") == 0) {
444 hammer_cmd_bstats(av + 1, ac - 1);
447 if (strcmp(av[0], "iostats") == 0) {
448 hammer_cmd_iostats(av + 1, ac - 1);
452 if (strncmp(av[0], "history", 7) == 0) {
453 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
456 if (strcmp(av[0], "rebalance") == 0) {
457 signal(SIGINT, sigalrm);
458 hammer_cmd_rebalance(av + 1, ac - 1);
461 if (strncmp(av[0], "reblock", 7) == 0) {
462 signal(SIGINT, sigalrm);
463 if (strcmp(av[0], "reblock") == 0)
464 hammer_cmd_reblock(av + 1, ac - 1, -1);
465 else if (strcmp(av[0], "reblock-btree") == 0)
466 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
467 else if (strcmp(av[0], "reblock-inodes") == 0)
468 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
469 else if (strcmp(av[0], "reblock-dirs") == 0)
470 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
471 else if (strcmp(av[0], "reblock-data") == 0)
472 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
477 if (strncmp(av[0], "mirror", 6) == 0) {
478 if (strcmp(av[0], "mirror-read") == 0)
479 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
480 else if (strcmp(av[0], "mirror-read-stream") == 0)
481 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
482 else if (strcmp(av[0], "mirror-write") == 0)
483 hammer_cmd_mirror_write(av + 1, ac - 1);
484 else if (strcmp(av[0], "mirror-copy") == 0)
485 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
486 else if (strcmp(av[0], "mirror-stream") == 0)
487 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
488 else if (strcmp(av[0], "mirror-dump") == 0)
489 hammer_cmd_mirror_dump(av + 1, ac - 1);
494 if (strcmp(av[0], "dedup-simulate") == 0) {
495 hammer_cmd_dedup_simulate(av + 1, ac - 1);
498 if (strcmp(av[0], "dedup") == 0) {
499 hammer_cmd_dedup(av + 1, ac - 1);
502 if (strcmp(av[0], "version") == 0) {
503 hammer_cmd_get_version(av + 1, ac - 1);
506 if (strcmp(av[0], "version-upgrade") == 0) {
507 hammer_cmd_set_version(av + 1, ac - 1);
510 if (strcmp(av[0], "volume-add") == 0) {
511 hammer_cmd_volume_add(av + 1, ac - 1);
514 if (strcmp(av[0], "volume-del") == 0) {
515 hammer_cmd_volume_del(av + 1, ac - 1);
518 if (strcmp(av[0], "volume-list") == 0) {
519 hammer_cmd_volume_list(av + 1, ac - 1, av[0]);
522 if (strcmp(av[0], "volume-blkdevs") == 0) {
523 hammer_cmd_volume_blkdevs(av + 1, ac - 1, av[0]);
527 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
528 if (status != uuid_s_ok) {
529 errx(1, "uuids file does not have the DragonFly "
530 "HAMMER filesystem type");
533 if (strcmp(av[0], "show") == 0) {
534 const char *arg = NULL;
537 hammer_parsedevs(blkdevs);
541 if (strcmp(av[2], "filter") == 0)
543 else if (strcmp(av[2], "nofilter") == 0)
546 hammer_cmd_show(-1, arg, filter, 0, NULL, NULL);
549 if (strcmp(av[0], "show-undo") == 0) {
550 hammer_parsedevs(blkdevs);
551 hammer_cmd_show_undo();
554 if (strcmp(av[0], "recover") == 0) {
555 hammer_parsedevs(blkdevs);
557 errx(1, "hammer recover required target directory");
558 hammer_cmd_recover(av[1]);
561 if (strcmp(av[0], "blockmap") == 0) {
562 hammer_parsedevs(blkdevs);
563 hammer_cmd_blockmap();
566 if (strcmp(av[0], "checkmap") == 0) {
567 hammer_parsedevs(blkdevs);
568 hammer_cmd_checkmap();
577 * Parse the device specification.
579 * Multi-volume hammer devices are colon-separated. Each element
580 * may be further expanded via /etc/devtab. One may also specify
581 * a single element which is expanded into multiple elements via
586 hammer_parsedevs(const char *blkdevs)
592 if (blkdevs == NULL) {
593 errx(1, "A -f blkdevs specification is required "
597 copy = strdup(blkdevs);
598 while ((volname = copy) != NULL) {
599 if ((copy = strchr(copy, ':')) != NULL)
601 volname = getdevpath(volname, 0);
602 if (strchr(volname, ':'))
603 hammer_parsedevs(volname);
605 setup_volume(-1, volname, 0, O_RDONLY);
612 assert(NumVolumes == 0);
618 sigalrm(int signo __unused)
620 /* do nothing (interrupts HAMMER ioctl) */
625 sigintr(int signo __unused)
627 if (RunningIoctl == 0)
630 /* do nothing (interrupts HAMMER ioctl) */
639 "hammer [-2ABqrvXy] [-b bandwidth] [-C cachesize[:readahead]] [-c cyclefile]\n"
640 " [-f blkdevs] [-i delay] [-t seconds] [-S splitup]\n"
641 " command [argument ...]\n"
642 "hammer synctid <filesystem> [quick]\n"
643 "hammer bstats [interval]\n"
644 "hammer iostats [interval]\n"
645 "hammer history[@offset[,len]] <file> ...\n"
646 "hammer namekey1 <path>\n"
647 "hammer namekey2 <path>\n"
648 "hammer namekey32 <path>\n"
649 "hammer cleanup [<filesystem> ...]\n"
650 "hammer abort-cleanup\n"
651 "hammer info [<dirpath> ...]\n"
652 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
653 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
654 "hammer prune <softlink-dir>\n"
655 "hammer prune-everything <filesystem>\n"
656 "hammer rebalance <filesystem> [saturation_percentage]\n"
657 "hammer reblock[-btree|-inodes|-dirs|-data] "
658 "<filesystem> [fill_percentage]\n"
659 "hammer pfs-status <dirpath> ...\n"
660 "hammer pfs-master <dirpath> [options]\n"
661 "hammer pfs-slave <dirpath> [options]\n"
662 "hammer pfs-update <dirpath> [options]\n"
663 "hammer pfs-upgrade <dirpath>\n"
664 "hammer pfs-downgrade <dirpath>\n"
665 "hammer pfs-destroy <dirpath>\n"
666 "hammer mirror-read <filesystem> [begin-tid]\n"
667 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
668 "hammer mirror-write <filesystem>\n"
669 "hammer mirror-dump [header]\n"
670 "hammer mirror-copy [[user@]host:]<filesystem>"
671 " [[user@]host:]<filesystem>\n"
672 "hammer mirror-stream [[user@]host:]<filesystem>"
673 " [[user@]host:]<filesystem>\n"
674 "hammer ssh-remote command filesystem\n"
675 "hammer version <filesystem>\n"
676 "hammer version-upgrade <filesystem> <version> [force]\n"
677 "hammer volume-add <device> <filesystem>\n"
678 "hammer volume-del <device> <filesystem>\n"
679 "hammer volume-list <filesystem>\n"
680 "hammer volume-blkdevs <filesystem>\n"
683 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
686 "hammer config [<filesystem> [<configfile>]]\n"
687 "hammer viconfig [<filesystem>]\n"
688 "hammer snap <path> [<note>]\n"
689 "hammer snaplo <path> [<note>]\n"
690 "hammer snapq <dir> [<note>]\n"
691 "hammer snaprm <path> ...\n"
692 "hammer snaprm <transid> ...\n"
693 "hammer snaprm <filesystem> <transid> ...\n"
694 "hammer snapls [<path> ...]\n"
697 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
700 "hammer -f blkdevs blockmap\n"
701 "hammer -f blkdevs checkmap\n"
702 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
703 "hammer -f blkdevs show-undo\n"
704 "hammer -f blkdevs recover <target_dir>\n"
707 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
710 "hammer dedup-simulate <filesystem>\n"
711 "hammer dedup <filesystem>\n"