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 *CyclePath;
68 main(int ac, char **av)
76 while ((ch = getopt(ac, av, "b:c:dhf:i:m:p:qrs:t:v2yBC:FS:X")) != -1) {
85 BandwidthOpt = strtoull(optarg, &ptr, 0);
100 /* bytes per second if no suffix */
107 SplitupOpt = strtoull(optarg, &ptr, 0);
122 /* bytes per second if no suffix */
138 DelayOpt = strtol(optarg, NULL, 0);
141 MemoryLimit = strtouq(optarg, &ptr, 0);
164 if (MemoryLimit < 1024 * 1024)
165 MemoryLimit = 1024 * 1024;
180 TimeoutOpt = strtol(optarg, NULL, 0);
198 cacheSize = strtol(optarg, &ptr, 0);
211 /* bytes if no suffix */
217 UseReadAhead = strtol(ptr + 1, NULL, 0);
218 UseReadBehind = -UseReadAhead;
220 if (cacheSize < 1024 * 1024)
221 cacheSize = 1024 * 1024;
222 if (UseReadAhead < 0)
224 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
225 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
226 UseReadBehind = -UseReadAhead;
228 hammer_cache_set(cacheSize);
248 signal(SIGALRM, sigalrm);
249 signal(SIGINT, sigintr);
251 if (strcmp(av[0], "synctid") == 0) {
252 hammer_cmd_synctid(av + 1, ac - 1);
255 if (strcmp(av[0], "namekey2") == 0) {
259 const char *aname = av[1];
264 key = (u_int32_t)crc32(aname, len) & 0xFFFFFFFEU;
268 crcx = crc32(aname + 3, len - 5);
269 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
270 key |= (int64_t)(crcx & 0x3F) << 42;
276 key |= ((int64_t)(aname[2] & 0x1F) << 48);
279 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
280 ((int64_t)(aname[len-2] & 0x1F) << 37);
283 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
284 ((int64_t)(aname[len-1] & 0x1F) << 32);
290 key |= 0x100000000LL;
291 printf("0x%016jx\n", (uintmax_t)key);
294 if (strcmp(av[0], "namekey1") == 0) {
299 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
301 key |= 0x100000000LL;
302 printf("0x%016jx\n", (uintmax_t)key);
305 if (strcmp(av[0], "namekey32") == 0) {
310 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
313 printf("0x%08x\n", key);
316 if (strcmp(av[0], "pfs-status") == 0) {
317 hammer_cmd_pseudofs_status(av + 1, ac - 1);
320 if (strcmp(av[0], "pfs-master") == 0) {
321 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
324 if (strcmp(av[0], "pfs-slave") == 0) {
325 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
328 if (strcmp(av[0], "pfs-update") == 0) {
329 hammer_cmd_pseudofs_update(av + 1, ac - 1);
332 if (strcmp(av[0], "pfs-upgrade") == 0) {
333 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
336 if (strcmp(av[0], "pfs-downgrade") == 0) {
337 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
340 if (strcmp(av[0], "pfs-destroy") == 0) {
341 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
344 if (strcmp(av[0], "status") == 0) {
345 hammer_cmd_status(av + 1, ac - 1);
348 if (strcmp(av[0], "prune") == 0) {
349 hammer_cmd_softprune(av + 1, ac - 1, 0);
352 if (strcmp(av[0], "config") == 0) {
353 hammer_cmd_config(av + 1, ac - 1);
356 if (strcmp(av[0], "viconfig") == 0) {
357 hammer_cmd_viconfig(av + 1, ac - 1);
360 if (strcmp(av[0], "cleanup") == 0) {
361 hammer_cmd_cleanup(av + 1, ac - 1);
364 if (strcmp(av[0], "info") == 0) {
368 if (strcmp(av[0], "prune-everything") == 0) {
369 hammer_cmd_softprune(av + 1, ac - 1, 1);
372 if (strcmp(av[0], "snap") == 0) {
373 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
376 if (strcmp(av[0], "snaplo") == 0) {
377 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
380 if (strcmp(av[0], "snapq") == 0) {
381 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
384 if (strcmp(av[0], "snapls") == 0) {
385 hammer_cmd_snapls(av + 1, ac - 1);
388 if (strcmp(av[0], "snaprm") == 0) {
389 hammer_cmd_snaprm(av + 1, ac - 1);
392 if (strcmp(av[0], "snapshot") == 0) {
393 hammer_cmd_snapshot(av + 1, ac - 1);
396 if (strcmp(av[0], "bstats") == 0) {
397 hammer_cmd_bstats(av + 1, ac - 1);
400 if (strcmp(av[0], "iostats") == 0) {
401 hammer_cmd_iostats(av + 1, ac - 1);
405 if (strncmp(av[0], "history", 7) == 0) {
406 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
409 if (strcmp(av[0], "rebalance") == 0) {
410 signal(SIGINT, sigalrm);
411 hammer_cmd_rebalance(av + 1, ac - 1);
414 if (strncmp(av[0], "reblock", 7) == 0) {
415 signal(SIGINT, sigalrm);
416 if (strcmp(av[0], "reblock") == 0)
417 hammer_cmd_reblock(av + 1, ac - 1, -1);
418 else if (strcmp(av[0], "reblock-btree") == 0)
419 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
420 else if (strcmp(av[0], "reblock-inodes") == 0)
421 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
422 else if (strcmp(av[0], "reblock-dirs") == 0)
423 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
424 else if (strcmp(av[0], "reblock-data") == 0)
425 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
430 if (strncmp(av[0], "mirror", 6) == 0) {
431 if (strcmp(av[0], "mirror-read") == 0)
432 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
433 else if (strcmp(av[0], "mirror-read-stream") == 0)
434 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
435 else if (strcmp(av[0], "mirror-write") == 0)
436 hammer_cmd_mirror_write(av + 1, ac - 1);
437 else if (strcmp(av[0], "mirror-copy") == 0)
438 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
439 else if (strcmp(av[0], "mirror-stream") == 0)
440 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
441 else if (strcmp(av[0], "mirror-dump") == 0)
442 hammer_cmd_mirror_dump();
447 if (strcmp(av[0], "dedup-simulate") == 0) {
448 hammer_cmd_dedup_simulate(av + 1, ac - 1);
451 if (strcmp(av[0], "dedup") == 0) {
452 hammer_cmd_dedup(av + 1, ac - 1);
455 if (strcmp(av[0], "version") == 0) {
456 hammer_cmd_get_version(av + 1, ac - 1);
459 if (strcmp(av[0], "version-upgrade") == 0) {
460 hammer_cmd_set_version(av + 1, ac - 1);
463 if (strcmp(av[0], "volume-add") == 0) {
464 hammer_cmd_volume_add(av + 1, ac - 1);
467 if (strcmp(av[0], "volume-del") == 0) {
468 hammer_cmd_volume_del(av + 1, ac - 1);
471 if (strcmp(av[0], "volume-list") == 0) {
472 hammer_cmd_volume_list(av + 1, ac - 1);
476 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
477 if (status != uuid_s_ok) {
478 errx(1, "uuids file does not have the DragonFly "
479 "HAMMER filesystem type");
482 if (strcmp(av[0], "show") == 0) {
484 intmax_t obj_id = (int64_t)HAMMER_MIN_OBJID;
486 hammer_parsedevs(blkdevs);
488 sscanf(av[1], "%08x:%jx", &lo, &obj_id);
489 hammer_cmd_show(-1, lo, (int64_t)obj_id, 0, NULL, NULL);
492 if (strcmp(av[0], "show-undo") == 0) {
493 hammer_parsedevs(blkdevs);
494 hammer_cmd_show_undo();
497 if (strcmp(av[0], "recover") == 0) {
498 hammer_parsedevs(blkdevs);
500 errx(1, "hammer recover required target directory");
501 hammer_cmd_recover(av[1]);
504 if (strcmp(av[0], "blockmap") == 0) {
505 hammer_parsedevs(blkdevs);
506 hammer_cmd_blockmap();
509 if (strcmp(av[0], "checkmap") == 0) {
510 hammer_parsedevs(blkdevs);
511 hammer_cmd_checkmap();
520 * Parse the device specification.
522 * Multi-volume hammer devices are colon-separated. Each element
523 * may be further expanded via /etc/devtab. One may also specify
524 * a single element which is expanded into multiple elements via
529 hammer_parsedevs(const char *blkdevs)
534 if (blkdevs == NULL) {
535 errx(1, "A -f blkdevs specification is required "
539 copy = strdup(blkdevs);
540 while ((volname = copy) != NULL) {
541 if ((copy = strchr(copy, ':')) != NULL)
543 volname = getdevpath(volname, 0);
544 if (strchr(volname, ':'))
545 hammer_parsedevs(volname);
547 setup_volume(-1, volname, 0, O_RDONLY);
553 sigalrm(int signo __unused)
555 /* do nothing (interrupts HAMMER ioctl) */
560 sigintr(int signo __unused)
562 if (RunningIoctl == 0)
565 /* do nothing (interrupts HAMMER ioctl) */
574 "hammer [-2BqrvXy] [-b bandwidth] [-C cachesize[:readahead]] [-c cyclefile]\n"
575 " [-f blkdevs] [-i delay] [-t seconds] [-S splitup]\n"
576 " command [argument ...]\n"
577 "hammer synctid <filesystem> [quick]\n"
578 "hammer bstats [interval]\n"
579 "hammer iostats [interval]\n"
580 "hammer history[@offset[,len]] <file> ...\n"
581 "hammer namekey1 <path>\n"
582 "hammer namekey2 <path>\n"
583 "hammer namekey32 <path>\n"
584 "hammer cleanup [<filesystem> ...]\n"
586 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
587 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
588 "hammer prune <softlink-dir>\n"
589 "hammer prune-everything <filesystem>\n"
590 "hammer rebalance <filesystem> [saturation_percentage]\n"
591 "hammer reblock[-btree|-inodes|-dirs|-data] "
592 "<filesystem> [fill_percentage]\n"
593 "hammer pfs-status <dirpath> ...\n"
594 "hammer pfs-master <dirpath> [options]\n"
595 "hammer pfs-slave <dirpath> [options]\n"
596 "hammer pfs-update <dirpath> [options]\n"
597 "hammer pfs-upgrade <dirpath>\n"
598 "hammer pfs-downgrade <dirpath>\n"
599 "hammer pfs-destroy <dirpath>\n"
600 "hammer mirror-read <filesystem> [begin-tid]\n"
601 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
602 "hammer mirror-write <filesystem>\n"
603 "hammer mirror-dump\n"
604 "hammer mirror-copy [[user@]host:]<filesystem>"
605 " [[user@]host:]<filesystem>\n"
606 "hammer mirror-stream [[user@]host:]<filesystem>"
607 " [[user@]host:]<filesystem>\n"
608 "hammer version <filesystem>\n"
609 "hammer version-upgrade <filesystem> <version> [force]\n"
610 "hammer volume-add <device> <filesystem>\n"
611 "hammer volume-del <device> <filesystem>\n"
612 "hammer volume-list <filesystem>\n"
615 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
618 "hammer config [<filesystem> [<configfile>]]\n"
619 "hammer viconfig [<filesystem>]\n"
620 "hammer snap <path> [<note>]\n"
621 "hammer snaplo <path> [<note>]\n"
622 "hammer snapq <dir> [<note>]\n"
623 "hammer snaprm <path> ...\n"
624 "hammer snaprm <transid> ...\n"
625 "hammer snaprm <filesystem> <transid> ...\n"
626 "hammer snapls [<path> ...]\n"
629 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
632 "hammer -f blkdevs blockmap\n"
633 "hammer -f blkdevs checkmap\n"
634 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
635 "hammer -f blkdevs show-undo\n"
636 "hammer -f blkdevs recover <target_dir>\n"
639 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
642 "hammer dedup-simulate <filesystem>\n"
643 "hammer dedup <filesystem>\n"