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 const char *CyclePath;
67 main(int ac, char **av)
75 while ((ch = getopt(ac, av, "b:c:dhf:i:p:qrs:t:v2yBC:FS:X")) != -1) {
84 BandwidthOpt = strtoull(optarg, &ptr, 0);
99 /* bytes per second if no suffix */
106 SplitupOpt = strtoull(optarg, &ptr, 0);
121 /* bytes per second if no suffix */
137 DelayOpt = strtol(optarg, NULL, 0);
152 TimeoutOpt = strtol(optarg, NULL, 0);
170 cacheSize = strtol(optarg, &ptr, 0);
183 /* bytes if no suffix */
189 UseReadAhead = strtol(ptr + 1, NULL, 0);
190 UseReadBehind = -UseReadAhead;
192 if (cacheSize < 1024 * 1024)
193 cacheSize = 1024 * 1024;
194 if (UseReadAhead < 0)
196 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
197 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
198 UseReadBehind = -UseReadAhead;
200 hammer_cache_set(cacheSize);
220 signal(SIGALRM, sigalrm);
221 signal(SIGINT, sigintr);
223 if (strcmp(av[0], "synctid") == 0) {
224 hammer_cmd_synctid(av + 1, ac - 1);
227 if (strcmp(av[0], "namekey2") == 0) {
231 const char *aname = av[1];
236 key = (u_int32_t)crc32(aname, len) & 0xFFFFFFFEU;
240 crcx = crc32(aname + 3, len - 5);
241 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
242 key |= (int64_t)(crcx & 0x3F) << 42;
248 key |= ((int64_t)(aname[2] & 0x1F) << 48);
251 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
252 ((int64_t)(aname[len-2] & 0x1F) << 37);
255 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
256 ((int64_t)(aname[len-1] & 0x1F) << 32);
262 key |= 0x100000000LL;
263 printf("0x%016jx\n", (uintmax_t)key);
266 if (strcmp(av[0], "namekey1") == 0) {
271 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
273 key |= 0x100000000LL;
274 printf("0x%016jx\n", (uintmax_t)key);
277 if (strcmp(av[0], "namekey32") == 0) {
282 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
285 printf("0x%08x\n", key);
288 if (strcmp(av[0], "pfs-status") == 0) {
289 hammer_cmd_pseudofs_status(av + 1, ac - 1);
292 if (strcmp(av[0], "pfs-master") == 0) {
293 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
296 if (strcmp(av[0], "pfs-slave") == 0) {
297 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
300 if (strcmp(av[0], "pfs-update") == 0) {
301 hammer_cmd_pseudofs_update(av + 1, ac - 1);
304 if (strcmp(av[0], "pfs-upgrade") == 0) {
305 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
308 if (strcmp(av[0], "pfs-downgrade") == 0) {
309 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
312 if (strcmp(av[0], "pfs-destroy") == 0) {
313 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
316 if (strcmp(av[0], "status") == 0) {
317 hammer_cmd_status(av + 1, ac - 1);
320 if (strcmp(av[0], "prune") == 0) {
321 hammer_cmd_softprune(av + 1, ac - 1, 0);
324 if (strcmp(av[0], "config") == 0) {
325 hammer_cmd_config(av + 1, ac - 1);
328 if (strcmp(av[0], "viconfig") == 0) {
329 hammer_cmd_viconfig(av + 1, ac - 1);
332 if (strcmp(av[0], "cleanup") == 0) {
333 hammer_cmd_cleanup(av + 1, ac - 1);
336 if (strcmp(av[0], "info") == 0) {
340 if (strcmp(av[0], "prune-everything") == 0) {
341 hammer_cmd_softprune(av + 1, ac - 1, 1);
344 if (strcmp(av[0], "snap") == 0) {
345 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
348 if (strcmp(av[0], "snaplo") == 0) {
349 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
352 if (strcmp(av[0], "snapq") == 0) {
353 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
356 if (strcmp(av[0], "snapls") == 0) {
357 hammer_cmd_snapls(av + 1, ac - 1);
360 if (strcmp(av[0], "snaprm") == 0) {
361 hammer_cmd_snaprm(av + 1, ac - 1);
364 if (strcmp(av[0], "snapshot") == 0) {
365 hammer_cmd_snapshot(av + 1, ac - 1);
368 if (strcmp(av[0], "bstats") == 0) {
369 hammer_cmd_bstats(av + 1, ac - 1);
372 if (strcmp(av[0], "iostats") == 0) {
373 hammer_cmd_iostats(av + 1, ac - 1);
377 if (strncmp(av[0], "history", 7) == 0) {
378 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
381 if (strcmp(av[0], "rebalance") == 0) {
382 signal(SIGINT, sigalrm);
383 hammer_cmd_rebalance(av + 1, ac - 1);
386 if (strncmp(av[0], "reblock", 7) == 0) {
387 signal(SIGINT, sigalrm);
388 if (strcmp(av[0], "reblock") == 0)
389 hammer_cmd_reblock(av + 1, ac - 1, -1);
390 else if (strcmp(av[0], "reblock-btree") == 0)
391 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
392 else if (strcmp(av[0], "reblock-inodes") == 0)
393 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
394 else if (strcmp(av[0], "reblock-dirs") == 0)
395 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
396 else if (strcmp(av[0], "reblock-data") == 0)
397 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
402 if (strncmp(av[0], "mirror", 6) == 0) {
403 if (strcmp(av[0], "mirror-read") == 0)
404 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
405 else if (strcmp(av[0], "mirror-read-stream") == 0)
406 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
407 else if (strcmp(av[0], "mirror-write") == 0)
408 hammer_cmd_mirror_write(av + 1, ac - 1);
409 else if (strcmp(av[0], "mirror-copy") == 0)
410 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
411 else if (strcmp(av[0], "mirror-stream") == 0)
412 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
413 else if (strcmp(av[0], "mirror-dump") == 0)
414 hammer_cmd_mirror_dump();
419 if (strcmp(av[0], "dedup-simulate") == 0) {
420 hammer_cmd_dedup_simulate(av + 1, ac - 1);
423 if (strcmp(av[0], "dedup") == 0) {
424 hammer_cmd_dedup(av + 1, ac - 1);
427 if (strcmp(av[0], "version") == 0) {
428 hammer_cmd_get_version(av + 1, ac - 1);
431 if (strcmp(av[0], "version-upgrade") == 0) {
432 hammer_cmd_set_version(av + 1, ac - 1);
435 if (strcmp(av[0], "volume-add") == 0) {
436 hammer_cmd_volume_add(av + 1, ac - 1);
439 if (strcmp(av[0], "volume-del") == 0) {
440 hammer_cmd_volume_del(av + 1, ac - 1);
443 if (strcmp(av[0], "volume-list") == 0) {
444 hammer_cmd_volume_list(av + 1, ac - 1);
448 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
449 if (status != uuid_s_ok) {
450 errx(1, "uuids file does not have the DragonFly "
451 "HAMMER filesystem type");
454 if (strcmp(av[0], "show") == 0) {
456 intmax_t obj_id = (int64_t)HAMMER_MIN_OBJID;
458 hammer_parsedevs(blkdevs);
460 sscanf(av[1], "%08x:%jx", &lo, &obj_id);
461 hammer_cmd_show(-1, lo, (int64_t)obj_id, 0, NULL, NULL);
464 if (strcmp(av[0], "show-undo") == 0) {
465 hammer_parsedevs(blkdevs);
466 hammer_cmd_show_undo();
469 if (strcmp(av[0], "recover") == 0) {
470 hammer_parsedevs(blkdevs);
472 errx(1, "hammer recover required target directory");
473 hammer_cmd_recover(av[1]);
476 if (strcmp(av[0], "blockmap") == 0) {
477 hammer_parsedevs(blkdevs);
478 hammer_cmd_blockmap();
481 if (strcmp(av[0], "checkmap") == 0) {
482 hammer_parsedevs(blkdevs);
483 hammer_cmd_checkmap();
492 * Parse the device specification.
494 * Multi-volume hammer devices are colon-separated. Each element
495 * may be further expanded via /etc/devtab. One may also specify
496 * a single element which is expanded into multiple elements via
501 hammer_parsedevs(const char *blkdevs)
506 if (blkdevs == NULL) {
507 errx(1, "A -f blkdevs specification is required "
511 copy = strdup(blkdevs);
512 while ((volname = copy) != NULL) {
513 if ((copy = strchr(copy, ':')) != NULL)
515 volname = getdevpath(volname, 0);
516 if (strchr(volname, ':'))
517 hammer_parsedevs(volname);
519 setup_volume(-1, volname, 0, O_RDONLY);
525 sigalrm(int signo __unused)
527 /* do nothing (interrupts HAMMER ioctl) */
532 sigintr(int signo __unused)
534 if (RunningIoctl == 0)
537 /* do nothing (interrupts HAMMER ioctl) */
546 "hammer [-2BqrvXy] [-b bandwidth] [-C cachesize[:readahead]] [-c cyclefile]\n"
547 " [-f blkdevs] [-i delay] [-t seconds] [-S splitup]\n"
548 " command [argument ...]\n"
549 "hammer synctid <filesystem> [quick]\n"
550 "hammer bstats [interval]\n"
551 "hammer iostats [interval]\n"
552 "hammer history[@offset[,len]] <file> ...\n"
553 "hammer namekey1 <path>\n"
554 "hammer namekey2 <path>\n"
555 "hammer namekey32 <path>\n"
556 "hammer cleanup [<filesystem> ...]\n"
558 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
559 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
560 "hammer prune <softlink-dir>\n"
561 "hammer prune-everything <filesystem>\n"
562 "hammer rebalance <filesystem> [saturation_percentage]\n"
563 "hammer reblock[-btree|-inodes|-dirs|-data] "
564 "<filesystem> [fill_percentage]\n"
565 "hammer pfs-status <dirpath> ...\n"
566 "hammer pfs-master <dirpath> [options]\n"
567 "hammer pfs-slave <dirpath> [options]\n"
568 "hammer pfs-update <dirpath> [options]\n"
569 "hammer pfs-upgrade <dirpath>\n"
570 "hammer pfs-downgrade <dirpath>\n"
571 "hammer pfs-destroy <dirpath>\n"
572 "hammer mirror-read <filesystem> [begin-tid]\n"
573 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
574 "hammer mirror-write <filesystem>\n"
575 "hammer mirror-dump\n"
576 "hammer mirror-copy [[user@]host:]<filesystem>"
577 " [[user@]host:]<filesystem>\n"
578 "hammer mirror-stream [[user@]host:]<filesystem>"
579 " [[user@]host:]<filesystem>\n"
580 "hammer version <filesystem>\n"
581 "hammer version-upgrade <filesystem> <version> [force]\n"
582 "hammer volume-add <device> <filesystem>\n"
583 "hammer volume-del <device> <filesystem>\n"
584 "hammer volume-list <filesystem>\n"
587 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
590 "hammer config [<filesystem> [<configfile>]]\n"
591 "hammer viconfig [<filesystem>]\n"
592 "hammer snap <path> [<note>]\n"
593 "hammer snaplo <path> [<note>]\n"
594 "hammer snapq <dir> [<note>]\n"
595 "hammer snaprm <path> ...\n"
596 "hammer snaprm <transid> ...\n"
597 "hammer snaprm <filesystem> <transid> ...\n"
598 "hammer snapls [<path> ...]\n"
601 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
604 "hammer -f blkdevs blockmap\n"
605 "hammer -f blkdevs checkmap\n"
606 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
607 "hammer -f blkdevs show-undo\n"
608 "hammer -f blkdevs recover <target_dir>\n"
611 fprintf(stderr, "\nHAMMER utility version 5+ commands:\n");
614 "hammer dedup-simulate <filesystem>\n"
615 "hammer dedup <filesystem>\n"