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
, int oflags
);
38 static void hammer_parsedevs_noverify(const char *blkdevs
, int oflags
);
39 static void sigalrm(int signo
);
40 static void sigintr(int signo
);
41 static void usage(int exit_code
);
58 uint64_t BandwidthOpt
;
59 uint64_t SplitupOpt
= 4ULL * 1024ULL * 1024ULL * 1024ULL;
60 uint64_t MemoryLimit
= 1024LLU * 1024 * 1024;
61 const char *SplitupOptStr
;
62 const char *CyclePath
;
65 main(int ac
, char **av
)
69 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 if (hammer_parse_cache_size(optarg
) == -1)
207 if (restrictcmd
== NULL
)
208 restrictcmd
= optarg
;
211 if (RestrictTarget
== NULL
)
212 RestrictTarget
= optarg
;
229 signal(SIGALRM
, sigalrm
);
230 signal(SIGINT
, sigintr
);
233 * Check command restriction (used by hammer ssh-remote). Several
234 * commands may be iterated with a comma.
239 dup
= ptr
= strdup(restrictcmd
);
240 while ((elm
= strsep(&ptr
, ",")) != NULL
) {
241 if (strcmp(av
[0], elm
) == 0)
245 errx(1, "hammer-remote: request does not match "
246 "restricted command");
251 * Lookup the filesystem type
253 uuid_name_lookup(&Hammer_FSType
, HAMMER_FSTYPE_STRING
, &status
);
254 if (status
!= uuid_s_ok
) {
255 errx(1, "uuids file does not have the DragonFly "
256 "HAMMER filesystem type");
262 if (strcmp(av
[0], "synctid") == 0) {
263 hammer_cmd_synctid(av
+ 1, ac
- 1);
266 if (strcmp(av
[0], "namekey2") == 0) {
270 const char *aname
= av
[1];
275 key
= (uint32_t)crc32(aname
, len
) & 0xFFFFFFFEU
;
279 crcx
= crc32(aname
+ 3, len
- 5);
280 crcx
= crcx
^ (crcx
>> 6) ^ (crcx
>> 12);
281 key
|= (int64_t)(crcx
& 0x3F) << 42;
287 key
|= ((int64_t)(aname
[2] & 0x1F) << 48);
290 key
|= ((int64_t)(aname
[1] & 0x1F) << 53) |
291 ((int64_t)(aname
[len
-2] & 0x1F) << 37);
294 key
|= ((int64_t)(aname
[0] & 0x1F) << 58) |
295 ((int64_t)(aname
[len
-1] & 0x1F) << 32);
301 key
|= 0x100000000LL
;
302 printf("0x%016jx\n", (uintmax_t)key
);
305 if (strcmp(av
[0], "namekey1") == 0) {
310 key
= (int64_t)(crc32(av
[1], strlen(av
[1])) & 0x7FFFFFFF) << 32;
312 key
|= 0x100000000LL
;
313 printf("0x%016jx\n", (uintmax_t)key
);
316 if (strcmp(av
[0], "namekey32") == 0) {
321 key
= crc32(av
[1], strlen(av
[1])) & 0x7FFFFFFF;
324 printf("0x%08x\n", key
);
327 if (strcmp(av
[0], "pfs-status") == 0) {
328 hammer_cmd_pseudofs_status(av
+ 1, ac
- 1);
331 if (strcmp(av
[0], "pfs-master") == 0) {
332 hammer_cmd_pseudofs_create(av
+ 1, ac
- 1, 0);
335 if (strcmp(av
[0], "pfs-slave") == 0) {
336 hammer_cmd_pseudofs_create(av
+ 1, ac
- 1, 1);
339 if (strcmp(av
[0], "pfs-update") == 0) {
340 hammer_cmd_pseudofs_update(av
+ 1, ac
- 1);
343 if (strcmp(av
[0], "pfs-upgrade") == 0) {
344 hammer_cmd_pseudofs_upgrade(av
+ 1, ac
- 1);
347 if (strcmp(av
[0], "pfs-downgrade") == 0) {
348 hammer_cmd_pseudofs_downgrade(av
+ 1, ac
- 1);
351 if (strcmp(av
[0], "pfs-destroy") == 0) {
352 hammer_cmd_pseudofs_destroy(av
+ 1, ac
- 1);
355 if (strcmp(av
[0], "prune") == 0) {
356 hammer_cmd_softprune(av
+ 1, ac
- 1, 0);
359 if (strcmp(av
[0], "config") == 0) {
360 hammer_cmd_config(av
+ 1, ac
- 1);
363 if (strcmp(av
[0], "viconfig") == 0) {
364 hammer_cmd_viconfig(av
+ 1, ac
- 1);
367 if (strcmp(av
[0], "cleanup") == 0) {
368 hammer_cmd_cleanup(av
+ 1, ac
- 1);
371 if (strcmp(av
[0], "abort-cleanup") == 0) {
372 hammer_cmd_abort_cleanup(av
+ 1, ac
- 1);
375 if (strcmp(av
[0], "info") == 0) {
376 hammer_cmd_info(av
+ 1, ac
- 1);
379 if (strcmp(av
[0], "prune-everything") == 0) {
380 hammer_cmd_softprune(av
+ 1, ac
- 1, 1);
383 if (strcmp(av
[0], "ssh-remote") == 0) {
386 hammer_cmd_sshremote(av
[1], av
[2]);
389 if (strcmp(av
[0], "snap") == 0) {
390 hammer_cmd_snap(av
+ 1, ac
- 1, 0, 1);
393 if (strcmp(av
[0], "snaplo") == 0) {
394 hammer_cmd_snap(av
+ 1, ac
- 1, 0, 0);
397 if (strcmp(av
[0], "snapq") == 0) {
398 hammer_cmd_snap(av
+ 1, ac
- 1, 1, 0);
401 if (strcmp(av
[0], "snapls") == 0) {
402 hammer_cmd_snapls(av
+ 1, ac
- 1);
405 if (strcmp(av
[0], "snaprm") == 0) {
406 hammer_cmd_snaprm(av
+ 1, ac
- 1);
409 if (strcmp(av
[0], "snapshot") == 0) {
410 hammer_cmd_snapshot(av
+ 1, ac
- 1);
413 if (strcmp(av
[0], "bstats") == 0) {
414 hammer_cmd_bstats(av
+ 1, ac
- 1);
417 if (strcmp(av
[0], "iostats") == 0) {
418 hammer_cmd_iostats(av
+ 1, ac
- 1);
421 if (strcmp(av
[0], "stats") == 0) {
422 hammer_cmd_stats(av
+ 1, ac
- 1);
426 if (strncmp(av
[0], "history", 7) == 0) {
427 hammer_cmd_history(av
[0] + 7, av
+ 1, ac
- 1);
430 if (strcmp(av
[0], "rebalance") == 0) {
431 signal(SIGINT
, sigalrm
);
432 hammer_cmd_rebalance(av
+ 1, ac
- 1);
435 if (strncmp(av
[0], "reblock", 7) == 0) {
436 signal(SIGINT
, sigalrm
);
437 if (strcmp(av
[0], "reblock") == 0)
438 hammer_cmd_reblock(av
+ 1, ac
- 1, HAMMER_IOC_DO_FLAGS
);
439 else if (strcmp(av
[0], "reblock-btree") == 0)
440 hammer_cmd_reblock(av
+ 1, ac
- 1, HAMMER_IOC_DO_BTREE
);
441 else if (strcmp(av
[0], "reblock-inodes") == 0)
442 hammer_cmd_reblock(av
+ 1, ac
- 1, HAMMER_IOC_DO_INODES
);
443 else if (strcmp(av
[0], "reblock-dirs") == 0)
444 hammer_cmd_reblock(av
+ 1, ac
- 1, HAMMER_IOC_DO_DIRS
);
445 else if (strcmp(av
[0], "reblock-data") == 0)
446 hammer_cmd_reblock(av
+ 1, ac
- 1, HAMMER_IOC_DO_DATA
);
451 if (strncmp(av
[0], "mirror", 6) == 0) {
452 if (strcmp(av
[0], "mirror-read") == 0)
453 hammer_cmd_mirror_read(av
+ 1, ac
- 1, 0);
454 else if (strcmp(av
[0], "mirror-read-stream") == 0)
455 hammer_cmd_mirror_read(av
+ 1, ac
- 1, 1);
456 else if (strcmp(av
[0], "mirror-write") == 0)
457 hammer_cmd_mirror_write(av
+ 1, ac
- 1);
458 else if (strcmp(av
[0], "mirror-copy") == 0)
459 hammer_cmd_mirror_copy(av
+ 1, ac
- 1, 0);
460 else if (strcmp(av
[0], "mirror-stream") == 0)
461 hammer_cmd_mirror_copy(av
+ 1, ac
- 1, 1);
462 else if (strcmp(av
[0], "mirror-dump") == 0)
463 hammer_cmd_mirror_dump(av
+ 1, ac
- 1);
468 if (strcmp(av
[0], "dedup-simulate") == 0) {
469 hammer_cmd_dedup_simulate(av
+ 1, ac
- 1);
472 if (strcmp(av
[0], "dedup") == 0) {
473 hammer_cmd_dedup(av
+ 1, ac
- 1);
476 if (strcmp(av
[0], "version") == 0) {
477 hammer_cmd_get_version(av
+ 1, ac
- 1);
480 if (strcmp(av
[0], "version-upgrade") == 0) {
481 hammer_cmd_set_version(av
+ 1, ac
- 1);
484 if (strcmp(av
[0], "volume-add") == 0) {
485 hammer_cmd_volume_add(av
+ 1, ac
- 1);
488 if (strcmp(av
[0], "volume-del") == 0) {
489 hammer_cmd_volume_del(av
+ 1, ac
- 1);
492 if (strcmp(av
[0], "volume-list") == 0) {
493 hammer_cmd_volume_list(av
+ 1, ac
- 1);
496 if (strcmp(av
[0], "volume-blkdevs") == 0) {
497 hammer_cmd_volume_blkdevs(av
+ 1, ac
- 1);
501 if (strcmp(av
[0], "show") == 0) {
502 const char *arg
= NULL
;
508 hammer_parsedevs(blkdevs
, O_RDONLY
);
510 errx(1, "Too many options specified");
514 dup
= ptr
= strdup(av
[2]);
515 while ((p
= strsep(&ptr
, ",")) != NULL
) {
516 if (strcmp(p
, "filter") == 0) {
518 } else if (strcmp(p
, "nofilter") == 0) {
520 } else if (strcmp(p
, "obfuscate") == 0) {
522 } else if (strcmp(p
, "indent") == 0) {
528 hammer_cmd_show(arg
, filter
, obfuscate
, indent
);
531 if (strcmp(av
[0], "show-undo") == 0) {
532 hammer_parsedevs(blkdevs
, O_RDONLY
);
533 hammer_cmd_show_undo();
536 if (strcmp(av
[0], "recover") == 0) {
537 hammer_parsedevs_noverify(blkdevs
, O_RDONLY
);
538 hammer_cmd_recover(av
+ 1, ac
- 1);
541 if (strcmp(av
[0], "blockmap") == 0) {
542 hammer_parsedevs(blkdevs
, O_RDONLY
);
543 hammer_cmd_blockmap();
546 if (strcmp(av
[0], "checkmap") == 0) {
547 hammer_parsedevs(blkdevs
, O_RDONLY
);
548 hammer_cmd_checkmap();
551 if (strcmp(av
[0], "strip") == 0) {
552 hammer_parsedevs(blkdevs
, O_RDWR
);
563 * Parse the device specification.
565 * Multi-volume hammer devices are colon-separated. Each element
566 * may be further expanded via /etc/devtab. One may also specify
567 * a single element which is expanded into multiple elements via
572 __hammer_parsedevs(const char *blkdevs
, int oflags
, int verify
)
574 struct volume_info
*vol
= NULL
;
579 if (blkdevs
== NULL
) {
580 errx(1, "A -f blkdevs specification is required "
584 copy
= strdup(blkdevs
);
585 while ((volname
= copy
) != NULL
) {
586 if ((copy
= strchr(copy
, ':')) != NULL
)
588 volname
= getdevpath(volname
, 0);
589 if (strchr(volname
, ':'))
590 __hammer_parsedevs(volname
, oflags
, verify
);
592 vol
= load_volume(volname
, oflags
, verify
);
601 * All volumes have the same vol_count.
604 if (volnum
!= vol
->ondisk
->vol_count
)
605 errx(1, "Volume header says %d volumes, but %d specified.",
606 vol
->ondisk
->vol_count
, volnum
);
608 if (get_root_volume() == NULL
)
609 errx(1, "No root volume found");
614 hammer_parsedevs(const char *blkdevs
, int oflags
)
616 __hammer_parsedevs(blkdevs
, oflags
, 1);
621 hammer_parsedevs_noverify(const char *blkdevs
, int oflags
)
623 __hammer_parsedevs(blkdevs
, oflags
, 0);
628 sigalrm(int signo __unused
)
630 /* do nothing (interrupts HAMMER ioctl) */
635 sigintr(int signo __unused
)
637 if (RunningIoctl
== 0)
640 /* do nothing (interrupts HAMMER ioctl) */
649 "hammer [-2ABFqrvXy] [-b bandwidth] [-C cachesize[:readahead]] \n"
650 " [-R restrictcmd] [-T restrictpath] [-c cyclefile]\n"
651 " [-e scoreboardfile] [-f blkdevs] [-i delay] [-p ssh-port]\n"
652 " [-S splitsize] [-t seconds] [-m memlimit] command [argument ...]\n"
653 "hammer synctid <filesystem> [quick]\n"
654 "hammer bstats [interval]\n"
655 "hammer iostats [interval]\n"
656 "hammer stats [interval]\n"
657 "hammer history[@offset[,len]] <file> ...\n"
658 "hammer namekey1 <path>\n"
659 "hammer namekey2 <path>\n"
660 "hammer namekey32 <path>\n"
661 "hammer cleanup [<filesystem> ...]\n"
662 "hammer abort-cleanup\n"
663 "hammer info [<dirpath> ...]\n"
664 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
665 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
666 "hammer prune <softlink-dir>\n"
667 "hammer prune-everything <filesystem>\n"
668 "hammer rebalance <filesystem> [saturation_percentage]\n"
669 "hammer reblock[-btree|-inodes|-dirs|-data] "
670 "<filesystem> [fill_percentage]\n"
671 "hammer pfs-status <dirpath> ...\n"
672 "hammer pfs-master <dirpath> [options]\n"
673 "hammer pfs-slave <dirpath> [options]\n"
674 "hammer pfs-update <dirpath> [options]\n"
675 "hammer pfs-upgrade <dirpath>\n"
676 "hammer pfs-downgrade <dirpath>\n"
677 "hammer pfs-destroy <dirpath>\n"
678 "hammer mirror-read <filesystem> [begin-tid]\n"
679 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
680 "hammer mirror-write <filesystem>\n"
681 "hammer mirror-dump [header]\n"
682 "hammer mirror-copy [[user@]host:]<filesystem>"
683 " [[user@]host:]<filesystem>\n"
684 "hammer mirror-stream [[user@]host:]<filesystem>"
685 " [[user@]host:]<filesystem>\n"
686 "hammer ssh-remote command filesystem\n"
687 "hammer version <filesystem>\n"
688 "hammer version-upgrade <filesystem> <version> [force]\n"
689 "hammer volume-add <device> <filesystem>\n"
690 "hammer volume-del <device> <filesystem>\n"
691 "hammer volume-list <filesystem>\n"
692 "hammer volume-blkdevs <filesystem>\n"
695 fprintf(stderr
, "\nHAMMER utility version 3+ commands:\n");
698 "hammer config [<filesystem> [<configfile>]]\n"
699 "hammer viconfig [<filesystem>]\n"
700 "hammer snap <path> [<note>]\n"
701 "hammer snaplo <path> [<note>]\n"
702 "hammer snapq <dir> [<note>]\n"
703 "hammer snaprm <path> ...\n"
704 "hammer snaprm <transid> ...\n"
705 "hammer snaprm <filesystem> <transid> ...\n"
706 "hammer snapls [<path> ...]\n"
709 fprintf(stderr
, "\nHAMMER utility version 4+ commands:\n");
712 "hammer -f blkdevs blockmap\n"
713 "hammer -f blkdevs checkmap\n"
714 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
715 "hammer -f blkdevs show-undo\n"
716 "hammer -f blkdevs recover <target_dir> [full|quick]\n"
717 "hammer -f blkdevs strip\n"
720 fprintf(stderr
, "\nHAMMER utility version 5+ commands:\n");
723 "hammer dedup-simulate <filesystem>\n"
724 "hammer dedup <filesystem>\n"