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;
69 main(int ac, char **av)
77 while ((ch = getopt(ac, av, "b:c:dhf:i:m:p:qrs:t:v2yBC:FS:X")) != -1) {
86 BandwidthOpt = strtoull(optarg, &ptr, 0);
101 /* bytes per second if no suffix */
108 SplitupOptStr = strdup(optarg);
109 SplitupOpt = strtoull(optarg, &ptr, 0);
124 /* bytes per second if no suffix */
140 DelayOpt = strtol(optarg, NULL, 0);
143 MemoryLimit = strtouq(optarg, &ptr, 0);
166 if (MemoryLimit < 1024 * 1024)
167 MemoryLimit = 1024 * 1024;
182 TimeoutOpt = strtol(optarg, NULL, 0);
200 cacheSize = strtol(optarg, &ptr, 0);
213 /* bytes if no suffix */
219 UseReadAhead = strtol(ptr + 1, NULL, 0);
220 UseReadBehind = -UseReadAhead;
222 if (cacheSize < 1024 * 1024)
223 cacheSize = 1024 * 1024;
224 if (UseReadAhead < 0)
226 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
227 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
228 UseReadBehind = -UseReadAhead;
230 hammer_cache_set(cacheSize);
250 signal(SIGALRM, sigalrm);
251 signal(SIGINT, sigintr);
253 if (strcmp(av[0], "synctid") == 0) {
254 hammer_cmd_synctid(av + 1, ac - 1);
257 if (strcmp(av[0], "namekey2") == 0) {
261 const char *aname = av[1];
266 key = (u_int32_t)crc32(aname, len) & 0xFFFFFFFEU;
270 crcx = crc32(aname + 3, len - 5);
271 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
272 key |= (int64_t)(crcx & 0x3F) << 42;
278 key |= ((int64_t)(aname[2] & 0x1F) << 48);
281 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
282 ((int64_t)(aname[len-2] & 0x1F) << 37);
285 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
286 ((int64_t)(aname[len-1] & 0x1F) << 32);
292 key |= 0x100000000LL;
293 printf("0x%016jx\n", (uintmax_t)key);
296 if (strcmp(av[0], "namekey1") == 0) {
301 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
303 key |= 0x100000000LL;
304 printf("0x%016jx\n", (uintmax_t)key);
307 if (strcmp(av[0], "namekey32") == 0) {
312 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
315 printf("0x%08x\n", key);
318 if (strcmp(av[0], "pfs-status") == 0) {
319 hammer_cmd_pseudofs_status(av + 1, ac - 1);
322 if (strcmp(av[0], "pfs-master") == 0) {
323 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
326 if (strcmp(av[0], "pfs-slave") == 0) {
327 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
330 if (strcmp(av[0], "pfs-update") == 0) {
331 hammer_cmd_pseudofs_update(av + 1, ac - 1);
334 if (strcmp(av[0], "pfs-upgrade") == 0) {
335 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
338 if (strcmp(av[0], "pfs-downgrade") == 0) {
339 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
342 if (strcmp(av[0], "pfs-destroy") == 0) {
343 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
346 if (strcmp(av[0], "status") == 0) {
347 hammer_cmd_status(av + 1, ac - 1);
350 if (strcmp(av[0], "prune") == 0) {
351 hammer_cmd_softprune(av + 1, ac - 1, 0);
354 if (strcmp(av[0], "config") == 0) {
355 hammer_cmd_config(av + 1, ac - 1);
358 if (strcmp(av[0], "viconfig") == 0) {
359 hammer_cmd_viconfig(av + 1, ac - 1);
362 if (strcmp(av[0], "cleanup") == 0) {
363 hammer_cmd_cleanup(av + 1, ac - 1);
366 if (strcmp(av[0], "info") == 0) {
370 if (strcmp(av[0], "prune-everything") == 0) {
371 hammer_cmd_softprune(av + 1, ac - 1, 1);
374 if (strcmp(av[0], "snap") == 0) {
375 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
378 if (strcmp(av[0], "snaplo") == 0) {
379 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
382 if (strcmp(av[0], "snapq") == 0) {
383 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
386 if (strcmp(av[0], "snapls") == 0) {
387 hammer_cmd_snapls(av + 1, ac - 1);
390 if (strcmp(av[0], "snaprm") == 0) {
391 hammer_cmd_snaprm(av + 1, ac - 1);
394 if (strcmp(av[0], "snapshot") == 0) {
395 hammer_cmd_snapshot(av + 1, ac - 1);
398 if (strcmp(av[0], "bstats") == 0) {
399 hammer_cmd_bstats(av + 1, ac - 1);
402 if (strcmp(av[0], "iostats") == 0) {
403 hammer_cmd_iostats(av + 1, ac - 1);
407 if (strncmp(av[0], "history", 7) == 0) {
408 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
411 if (strcmp(av[0], "rebalance") == 0) {
412 signal(SIGINT, sigalrm);
413 hammer_cmd_rebalance(av + 1, ac - 1);
416 if (strncmp(av[0], "reblock", 7) == 0) {
417 signal(SIGINT, sigalrm);
418 if (strcmp(av[0], "reblock") == 0)
419 hammer_cmd_reblock(av + 1, ac - 1, -1);
420 else if (strcmp(av[0], "reblock-btree") == 0)
421 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
422 else if (strcmp(av[0], "reblock-inodes") == 0)
423 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
424 else if (strcmp(av[0], "reblock-dirs") == 0)
425 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
426 else if (strcmp(av[0], "reblock-data") == 0)
427 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
432 if (strncmp(av[0], "mirror", 6) == 0) {
433 if (strcmp(av[0], "mirror-read") == 0)
434 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
435 else if (strcmp(av[0], "mirror-read-stream") == 0)
436 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
437 else if (strcmp(av[0], "mirror-write") == 0)
438 hammer_cmd_mirror_write(av + 1, ac - 1);
439 else if (strcmp(av[0], "mirror-copy") == 0)
440 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
441 else if (strcmp(av[0], "mirror-stream") == 0)
442 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
443 else if (strcmp(av[0], "mirror-dump") == 0)
444 hammer_cmd_mirror_dump();
449 if (strcmp(av[0], "dedup-simulate") == 0) {
450 hammer_cmd_dedup_simulate(av + 1, ac - 1);
453 if (strcmp(av[0], "dedup") == 0) {
454 hammer_cmd_dedup(av + 1, ac - 1);
457 if (strcmp(av[0], "version") == 0) {
458 hammer_cmd_get_version(av + 1, ac - 1);
461 if (strcmp(av[0], "version-upgrade") == 0) {
462 hammer_cmd_set_version(av + 1, ac - 1);
465 if (strcmp(av[0], "volume-add") == 0) {
466 hammer_cmd_volume_add(av + 1, ac - 1);
469 if (strcmp(av[0], "volume-del") == 0) {
470 hammer_cmd_volume_del(av + 1, ac - 1);
473 if (strcmp(av[0], "volume-list") == 0) {
474 hammer_cmd_volume_list(av + 1, ac - 1);
478 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
479 if (status != uuid_s_ok) {
480 errx(1, "uuids file does not have the DragonFly "
481 "HAMMER filesystem type");
484 if (strcmp(av[0], "show") == 0) {
486 intmax_t obj_id = (int64_t)HAMMER_MIN_OBJID;
488 hammer_parsedevs(blkdevs);
490 sscanf(av[1], "%08x:%jx", &lo, &obj_id);
491 hammer_cmd_show(-1, lo, (int64_t)obj_id, 0, NULL, NULL);
494 if (strcmp(av[0], "show-undo") == 0) {
495 hammer_parsedevs(blkdevs);
496 hammer_cmd_show_undo();
499 if (strcmp(av[0], "recover") == 0) {
500 hammer_parsedevs(blkdevs);
502 errx(1, "hammer recover required target directory");
503 hammer_cmd_recover(av[1]);
506 if (strcmp(av[0], "blockmap") == 0) {
507 hammer_parsedevs(blkdevs);
508 hammer_cmd_blockmap();
511 if (strcmp(av[0], "checkmap") == 0) {
512 hammer_parsedevs(blkdevs);
513 hammer_cmd_checkmap();
522 * Parse the device specification.
524 * Multi-volume hammer devices are colon-separated. Each element
525 * may be further expanded via /etc/devtab. One may also specify
526 * a single element which is expanded into multiple elements via
531 hammer_parsedevs(const char *blkdevs)
536 if (blkdevs == NULL) {
537 errx(1, "A -f blkdevs specification is required "
541 copy = strdup(blkdevs);
542 while ((volname = copy) != NULL) {
543 if ((copy = strchr(copy, ':')) != NULL)
545 volname = getdevpath(volname, 0);
546 if (strchr(volname, ':'))
547 hammer_parsedevs(volname);
549 setup_volume(-1, volname, 0, O_RDONLY);
555 sigalrm(int signo __unused)
557 /* do nothing (interrupts HAMMER ioctl) */
562 sigintr(int signo __unused)
564 if (RunningIoctl == 0)
567 /* do nothing (interrupts HAMMER ioctl) */
576 "hammer [-2BqrvXy] [-b bandwidth] [-C cachesize[:readahead]] [-c cyclefile]\n"
577 " [-f blkdevs] [-i delay] [-t seconds] [-S splitup]\n"
578 " command [argument ...]\n"
579 "hammer synctid <filesystem> [quick]\n"
580 "hammer bstats [interval]\n"
581 "hammer iostats [interval]\n"
582 "hammer history[@offset[,len]] <file> ...\n"
583 "hammer namekey1 <path>\n"
584 "hammer namekey2 <path>\n"
585 "hammer namekey32 <path>\n"
586 "hammer cleanup [<filesystem> ...]\n"
588 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
589 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
590 "hammer prune <softlink-dir>\n"
591 "hammer prune-everything <filesystem>\n"
592 "hammer rebalance <filesystem> [saturation_percentage]\n"
593 "hammer reblock[-btree|-inodes|-dirs|-data] "
594 "<filesystem> [fill_percentage]\n"
595 "hammer pfs-status <dirpath> ...\n"
596 "hammer pfs-master <dirpath> [options]\n"
597 "hammer pfs-slave <dirpath> [options]\n"
598 "hammer pfs-update <dirpath> [options]\n"
599 "hammer pfs-upgrade <dirpath>\n"
600 "hammer pfs-downgrade <dirpath>\n"
601 "hammer pfs-destroy <dirpath>\n"
602 "hammer mirror-read <filesystem> [begin-tid]\n"
603 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
604 "hammer mirror-write <filesystem>\n"
605 "hammer mirror-dump\n"
606 "hammer mirror-copy [[user@]host:]<filesystem>"
607 " [[user@]host:]<filesystem>\n"
608 "hammer mirror-stream [[user@]host:]<filesystem>"
609 " [[user@]host:]<filesystem>\n"
610 "hammer version <filesystem>\n"
611 "hammer version-upgrade <filesystem> <version> [force]\n"
612 "hammer volume-add <device> <filesystem>\n"
613 "hammer volume-del <device> <filesystem>\n"
614 "hammer volume-list <filesystem>\n"
617 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
620 "hammer config [<filesystem> [<configfile>]]\n"
621 "hammer viconfig [<filesystem>]\n"
622 "hammer snap <path> [<note>]\n"
623 "hammer snaplo <path> [<note>]\n"
624 "hammer snapq <dir> [<note>]\n"
625 "hammer snaprm <path> ...\n"
626 "hammer snaprm <transid> ...\n"
627 "hammer snaprm <filesystem> <transid> ...\n"
628 "hammer snapls [<path> ...]\n"
631 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
634 "hammer -f blkdevs blockmap\n"
635 "hammer -f blkdevs checkmap\n"
636 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
637 "hammer -f blkdevs show-undo\n"
638 "hammer -f blkdevs recover <target_dir>\n"
641 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
644 "hammer dedup-simulate <filesystem>\n"
645 "hammer dedup <filesystem>\n"