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
34 * $DragonFly: src/sbin/hammer/hammer.c,v 1.44 2008/11/13 02:04:27 dillon Exp $
42 static void hammer_parsedevs(const char *blkdevs);
43 static void sigalrm(int signo);
44 static void sigintr(int signo);
45 static void usage(int exit_code);
61 u_int64_t BandwidthOpt;
62 u_int64_t SplitupOpt = 4ULL * 1024ULL * 1024ULL * 1024ULL;
63 u_int64_t MemoryLimit = 1024LLU * 1024 * 1024;
64 const char *SplitupOptStr;
65 const char *CyclePath;
67 const char *RestrictTarget;
70 main(int ac, char **av)
74 char *restrictcmd = NULL;
79 while ((ch = getopt(ac, av,
80 "b:c:de:hf:i:m:p:qrs:t:v2yBC:FR:S:T:X")) != -1) {
89 BandwidthOpt = strtoull(optarg, &ptr, 0);
101 BandwidthOpt *= 1024;
104 /* bytes per second if no suffix */
111 SplitupOptStr = strdup(optarg);
112 SplitupOpt = strtoull(optarg, &ptr, 0);
127 /* bytes per second if no suffix */
140 ScoreBoardFile = optarg;
146 DelayOpt = strtol(optarg, NULL, 0);
149 MemoryLimit = strtouq(optarg, &ptr, 0);
172 if (MemoryLimit < 1024 * 1024)
173 MemoryLimit = 1024 * 1024;
188 TimeoutOpt = strtol(optarg, NULL, 0);
206 cacheSize = strtol(optarg, &ptr, 0);
219 /* bytes if no suffix */
225 UseReadAhead = strtol(ptr + 1, NULL, 0);
226 UseReadBehind = -UseReadAhead;
228 if (cacheSize < 1024 * 1024)
229 cacheSize = 1024 * 1024;
230 if (UseReadAhead < 0)
232 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
233 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
234 UseReadBehind = -UseReadAhead;
236 hammer_cache_set(cacheSize);
242 if (restrictcmd == NULL)
243 restrictcmd = optarg;
246 if (RestrictTarget == NULL)
247 RestrictTarget = optarg;
264 signal(SIGALRM, sigalrm);
265 signal(SIGINT, sigintr);
268 * Check command restriction (used by hammer ssh-remote). Several
269 * commands may be iterated with a comma.
274 ptr = strdup(restrictcmd);
275 while ((elm = strsep(&ptr, ",")) != NULL) {
276 if (strcmp(av[0], elm) == 0)
280 fprintf(stderr, "hammer-remote: request does not match "
281 "restricted command\n");
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 = (u_int32_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], "info") == 0) {
400 hammer_cmd_info(av + 1, ac - 1);
403 if (strcmp(av[0], "prune-everything") == 0) {
404 hammer_cmd_softprune(av + 1, ac - 1, 1);
407 if (strcmp(av[0], "ssh-remote") == 0) {
410 hammer_cmd_sshremote(av[1], av[2]);
413 if (strcmp(av[0], "snap") == 0) {
414 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
417 if (strcmp(av[0], "snaplo") == 0) {
418 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
421 if (strcmp(av[0], "snapq") == 0) {
422 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
425 if (strcmp(av[0], "snapls") == 0) {
426 hammer_cmd_snapls(av + 1, ac - 1);
429 if (strcmp(av[0], "snaprm") == 0) {
430 hammer_cmd_snaprm(av + 1, ac - 1);
433 if (strcmp(av[0], "snapshot") == 0) {
434 hammer_cmd_snapshot(av + 1, ac - 1);
437 if (strcmp(av[0], "bstats") == 0) {
438 hammer_cmd_bstats(av + 1, ac - 1);
441 if (strcmp(av[0], "iostats") == 0) {
442 hammer_cmd_iostats(av + 1, ac - 1);
446 if (strncmp(av[0], "history", 7) == 0) {
447 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
450 if (strcmp(av[0], "rebalance") == 0) {
451 signal(SIGINT, sigalrm);
452 hammer_cmd_rebalance(av + 1, ac - 1);
455 if (strncmp(av[0], "reblock", 7) == 0) {
456 signal(SIGINT, sigalrm);
457 if (strcmp(av[0], "reblock") == 0)
458 hammer_cmd_reblock(av + 1, ac - 1, -1);
459 else if (strcmp(av[0], "reblock-btree") == 0)
460 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
461 else if (strcmp(av[0], "reblock-inodes") == 0)
462 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
463 else if (strcmp(av[0], "reblock-dirs") == 0)
464 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
465 else if (strcmp(av[0], "reblock-data") == 0)
466 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
471 if (strncmp(av[0], "mirror", 6) == 0) {
472 if (strcmp(av[0], "mirror-read") == 0)
473 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
474 else if (strcmp(av[0], "mirror-read-stream") == 0)
475 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
476 else if (strcmp(av[0], "mirror-write") == 0)
477 hammer_cmd_mirror_write(av + 1, ac - 1);
478 else if (strcmp(av[0], "mirror-copy") == 0)
479 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
480 else if (strcmp(av[0], "mirror-stream") == 0)
481 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
482 else if (strcmp(av[0], "mirror-dump") == 0)
483 hammer_cmd_mirror_dump();
488 if (strcmp(av[0], "dedup-simulate") == 0) {
489 hammer_cmd_dedup_simulate(av + 1, ac - 1);
492 if (strcmp(av[0], "dedup") == 0) {
493 hammer_cmd_dedup(av + 1, ac - 1);
496 if (strcmp(av[0], "version") == 0) {
497 hammer_cmd_get_version(av + 1, ac - 1);
500 if (strcmp(av[0], "version-upgrade") == 0) {
501 hammer_cmd_set_version(av + 1, ac - 1);
504 if (strcmp(av[0], "volume-add") == 0) {
505 hammer_cmd_volume_add(av + 1, ac - 1);
508 if (strcmp(av[0], "volume-del") == 0) {
509 hammer_cmd_volume_del(av + 1, ac - 1);
512 if (strcmp(av[0], "volume-list") == 0) {
513 hammer_cmd_volume_list(av + 1, ac - 1);
517 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
518 if (status != uuid_s_ok) {
519 errx(1, "uuids file does not have the DragonFly "
520 "HAMMER filesystem type");
523 if (strcmp(av[0], "show") == 0) {
525 intmax_t obj_id = (int64_t)HAMMER_MIN_OBJID;
527 hammer_parsedevs(blkdevs);
529 sscanf(av[1], "%08x:%jx", &lo, &obj_id);
530 hammer_cmd_show(-1, lo, (int64_t)obj_id, 0, NULL, NULL);
533 if (strcmp(av[0], "show-undo") == 0) {
534 hammer_parsedevs(blkdevs);
535 hammer_cmd_show_undo();
538 if (strcmp(av[0], "recover") == 0) {
539 hammer_parsedevs(blkdevs);
541 errx(1, "hammer recover required target directory");
542 hammer_cmd_recover(av[1]);
545 if (strcmp(av[0], "blockmap") == 0) {
546 hammer_parsedevs(blkdevs);
547 hammer_cmd_blockmap();
550 if (strcmp(av[0], "checkmap") == 0) {
551 hammer_parsedevs(blkdevs);
552 hammer_cmd_checkmap();
561 * Parse the device specification.
563 * Multi-volume hammer devices are colon-separated. Each element
564 * may be further expanded via /etc/devtab. One may also specify
565 * a single element which is expanded into multiple elements via
570 hammer_parsedevs(const char *blkdevs)
575 if (blkdevs == NULL) {
576 errx(1, "A -f blkdevs specification is required "
580 copy = strdup(blkdevs);
581 while ((volname = copy) != NULL) {
582 if ((copy = strchr(copy, ':')) != NULL)
584 volname = getdevpath(volname, 0);
585 if (strchr(volname, ':'))
586 hammer_parsedevs(volname);
588 setup_volume(-1, volname, 0, O_RDONLY);
594 sigalrm(int signo __unused)
596 /* do nothing (interrupts HAMMER ioctl) */
601 sigintr(int signo __unused)
603 if (RunningIoctl == 0)
606 /* do nothing (interrupts HAMMER ioctl) */
615 "hammer [-2BqrvXy] [-b bandwidth] [-C cachesize[:readahead]] [-c cyclefile]\n"
616 " [-f blkdevs] [-i delay] [-t seconds] [-S splitup]\n"
617 " command [argument ...]\n"
618 "hammer synctid <filesystem> [quick]\n"
619 "hammer bstats [interval]\n"
620 "hammer iostats [interval]\n"
621 "hammer history[@offset[,len]] <file> ...\n"
622 "hammer namekey1 <path>\n"
623 "hammer namekey2 <path>\n"
624 "hammer namekey32 <path>\n"
625 "hammer cleanup [<filesystem> ...]\n"
626 "hammer info [<dirpath> ...]\n"
627 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
628 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
629 "hammer prune <softlink-dir>\n"
630 "hammer prune-everything <filesystem>\n"
631 "hammer rebalance <filesystem> [saturation_percentage]\n"
632 "hammer reblock[-btree|-inodes|-dirs|-data] "
633 "<filesystem> [fill_percentage]\n"
634 "hammer pfs-status <dirpath> ...\n"
635 "hammer pfs-master <dirpath> [options]\n"
636 "hammer pfs-slave <dirpath> [options]\n"
637 "hammer pfs-update <dirpath> [options]\n"
638 "hammer pfs-upgrade <dirpath>\n"
639 "hammer pfs-downgrade <dirpath>\n"
640 "hammer pfs-destroy <dirpath>\n"
641 "hammer mirror-read <filesystem> [begin-tid]\n"
642 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
643 "hammer mirror-write <filesystem>\n"
644 "hammer mirror-dump\n"
645 "hammer mirror-copy [[user@]host:]<filesystem>"
646 " [[user@]host:]<filesystem>\n"
647 "hammer mirror-stream [[user@]host:]<filesystem>"
648 " [[user@]host:]<filesystem>\n"
649 "hammer ssh-remote command filesystem\n"
650 "hammer version <filesystem>\n"
651 "hammer version-upgrade <filesystem> <version> [force]\n"
652 "hammer volume-add <device> <filesystem>\n"
653 "hammer volume-del <device> <filesystem>\n"
654 "hammer volume-list <filesystem>\n"
657 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
660 "hammer config [<filesystem> [<configfile>]]\n"
661 "hammer viconfig [<filesystem>]\n"
662 "hammer snap <path> [<note>]\n"
663 "hammer snaplo <path> [<note>]\n"
664 "hammer snapq <dir> [<note>]\n"
665 "hammer snaprm <path> ...\n"
666 "hammer snaprm <transid> ...\n"
667 "hammer snaprm <filesystem> <transid> ...\n"
668 "hammer snapls [<path> ...]\n"
671 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
674 "hammer -f blkdevs blockmap\n"
675 "hammer -f blkdevs checkmap\n"
676 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
677 "hammer -f blkdevs show-undo\n"
678 "hammer -f blkdevs recover <target_dir>\n"
681 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
684 "hammer dedup-simulate <filesystem>\n"
685 "hammer dedup <filesystem>\n"