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);
60 u_int64_t BandwidthOpt;
61 const char *CyclePath;
65 main(int ac, char **av)
73 while ((ch = getopt(ac, av, "b:c:dhf:i:p:qrs:t:v2yBC:F")) != -1) {
82 BandwidthOpt = strtoull(optarg, &ptr, 0);
97 /* bytes per second if no suffix */
113 DelayOpt = strtol(optarg, NULL, 0);
128 TimeoutOpt = strtol(optarg, NULL, 0);
146 cacheSize = strtol(optarg, &ptr, 0);
159 /* bytes if no suffix */
165 UseReadAhead = strtol(ptr + 1, NULL, 0);
166 UseReadBehind = -UseReadAhead;
168 if (cacheSize < 1024 * 1024)
169 cacheSize = 1024 * 1024;
170 if (UseReadAhead < 0)
172 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
173 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
174 UseReadBehind = -UseReadAhead;
176 hammer_cache_set(cacheSize);
193 signal(SIGALRM, sigalrm);
194 signal(SIGINT, sigintr);
196 if (strcmp(av[0], "synctid") == 0) {
197 hammer_cmd_synctid(av + 1, ac - 1);
200 if (strcmp(av[0], "namekey2") == 0) {
204 const char *aname = av[1];
209 key = (u_int32_t)crc32(aname, len) & 0xFFFFFFFEU;
213 crcx = crc32(aname + 3, len - 5);
214 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
215 key |= (int64_t)(crcx & 0x3F) << 42;
221 key |= ((int64_t)(aname[2] & 0x1F) << 48);
224 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
225 ((int64_t)(aname[len-2] & 0x1F) << 37);
228 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
229 ((int64_t)(aname[len-1] & 0x1F) << 32);
235 key |= 0x100000000LL;
236 printf("0x%016jx\n", (uintmax_t)key);
239 if (strcmp(av[0], "namekey1") == 0) {
244 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
246 key |= 0x100000000LL;
247 printf("0x%016jx\n", (uintmax_t)key);
250 if (strcmp(av[0], "namekey32") == 0) {
255 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
258 printf("0x%08x\n", key);
261 if (strcmp(av[0], "pfs-status") == 0) {
262 hammer_cmd_pseudofs_status(av + 1, ac - 1);
265 if (strcmp(av[0], "pfs-master") == 0) {
266 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
269 if (strcmp(av[0], "pfs-slave") == 0) {
270 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
273 if (strcmp(av[0], "pfs-update") == 0) {
274 hammer_cmd_pseudofs_update(av + 1, ac - 1);
277 if (strcmp(av[0], "pfs-upgrade") == 0) {
278 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
281 if (strcmp(av[0], "pfs-downgrade") == 0) {
282 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
285 if (strcmp(av[0], "pfs-destroy") == 0) {
286 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
289 if (strcmp(av[0], "status") == 0) {
290 hammer_cmd_status(av + 1, ac - 1);
293 if (strcmp(av[0], "prune") == 0) {
294 hammer_cmd_softprune(av + 1, ac - 1, 0);
297 if (strcmp(av[0], "config") == 0) {
298 hammer_cmd_config(av + 1, ac - 1);
301 if (strcmp(av[0], "viconfig") == 0) {
302 hammer_cmd_viconfig(av + 1, ac - 1);
305 if (strcmp(av[0], "cleanup") == 0) {
306 hammer_cmd_cleanup(av + 1, ac - 1);
309 if (strcmp(av[0], "info") == 0) {
313 if (strcmp(av[0], "prune-everything") == 0) {
314 hammer_cmd_softprune(av + 1, ac - 1, 1);
317 if (strcmp(av[0], "snap") == 0) {
318 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
321 if (strcmp(av[0], "snaplo") == 0) {
322 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
325 if (strcmp(av[0], "snapq") == 0) {
326 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
329 if (strcmp(av[0], "snapls") == 0) {
330 hammer_cmd_snapls(av + 1, ac - 1);
333 if (strcmp(av[0], "snaprm") == 0) {
334 hammer_cmd_snaprm(av + 1, ac - 1);
337 if (strcmp(av[0], "snapshot") == 0) {
338 hammer_cmd_snapshot(av + 1, ac - 1);
341 if (strcmp(av[0], "bstats") == 0) {
342 hammer_cmd_bstats(av + 1, ac - 1);
345 if (strcmp(av[0], "iostats") == 0) {
346 hammer_cmd_iostats(av + 1, ac - 1);
350 if (strncmp(av[0], "history", 7) == 0) {
351 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
354 if (strcmp(av[0], "rebalance") == 0) {
355 signal(SIGINT, sigalrm);
356 hammer_cmd_rebalance(av + 1, ac - 1);
359 if (strncmp(av[0], "reblock", 7) == 0) {
360 signal(SIGINT, sigalrm);
361 if (strcmp(av[0], "reblock") == 0)
362 hammer_cmd_reblock(av + 1, ac - 1, -1);
363 else if (strcmp(av[0], "reblock-btree") == 0)
364 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
365 else if (strcmp(av[0], "reblock-inodes") == 0)
366 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
367 else if (strcmp(av[0], "reblock-dirs") == 0)
368 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
369 else if (strcmp(av[0], "reblock-data") == 0)
370 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
375 if (strncmp(av[0], "mirror", 6) == 0) {
376 if (strcmp(av[0], "mirror-read") == 0)
377 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
378 else if (strcmp(av[0], "mirror-read-stream") == 0)
379 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
380 else if (strcmp(av[0], "mirror-write") == 0)
381 hammer_cmd_mirror_write(av + 1, ac - 1);
382 else if (strcmp(av[0], "mirror-copy") == 0)
383 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
384 else if (strcmp(av[0], "mirror-stream") == 0)
385 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
386 else if (strcmp(av[0], "mirror-dump") == 0)
387 hammer_cmd_mirror_dump();
392 if (strcmp(av[0], "version") == 0) {
393 hammer_cmd_get_version(av + 1, ac - 1);
396 if (strcmp(av[0], "version-upgrade") == 0) {
397 hammer_cmd_set_version(av + 1, ac - 1);
400 if (strcmp(av[0], "expand") == 0) {
401 hammer_cmd_expand(av + 1, ac - 1);
405 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
406 if (status != uuid_s_ok) {
407 errx(1, "uuids file does not have the DragonFly "
408 "HAMMER filesystem type");
411 if (strcmp(av[0], "show") == 0) {
413 intmax_t obj_id = (int64_t)HAMMER_MIN_OBJID;
415 hammer_parsedevs(blkdevs);
417 sscanf(av[1], "%08x:%jx", &lo, &obj_id);
418 hammer_cmd_show(-1, lo, (int64_t)obj_id, 0, NULL, NULL);
421 if (strcmp(av[0], "show-undo") == 0) {
422 hammer_parsedevs(blkdevs);
423 hammer_cmd_show_undo();
426 if (strcmp(av[0], "blockmap") == 0) {
427 hammer_parsedevs(blkdevs);
428 hammer_cmd_blockmap();
437 * Parse the device specification.
439 * Multi-volume hammer devices are colon-separated. Each element
440 * may be further expanded via /etc/devtab. One may also specify
441 * a single element which is expanded into multiple elements via
446 hammer_parsedevs(const char *blkdevs)
451 if (blkdevs == NULL) {
452 errx(1, "A -f blkdevs specification is required "
456 copy = strdup(blkdevs);
457 while ((volname = copy) != NULL) {
458 if ((copy = strchr(copy, ':')) != NULL)
460 volname = getdevpath(volname, 0);
461 if (strchr(volname, ':'))
462 hammer_parsedevs(volname);
464 setup_volume(-1, volname, 0, O_RDONLY);
470 sigalrm(int signo __unused)
472 /* do nothing (interrupts HAMMER ioctl) */
477 sigintr(int signo __unused)
479 if (RunningIoctl == 0)
482 /* do nothing (interrupts HAMMER ioctl) */
491 "hammer [-2Bqrvy] [-b bandwidth] [-C cachesize[:readahead]] [-c cyclefile]\n"
492 " [-f blkdevs] [-i delay] [-t seconds] command [argument ...]\n"
493 "hammer synctid <filesystem> [quick]\n"
494 "hammer -f blkdevs blockmap\n"
495 "hammer bstats [interval]\n"
496 "hammer iostats [interval]\n"
497 "hammer history[@offset[,len]] <file> ...\n"
498 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
499 "hammer namekey1 <path>\n"
500 "hammer namekey2 <path>\n"
501 "hammer namekey32 <path>\n"
502 "hammer cleanup [<filesystem> ...]\n"
504 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
505 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
506 "hammer prune <softlink-dir>\n"
507 "hammer prune-everything <filesystem>\n"
508 "hammer rebalance <filesystem> [saturation_percentage]\n"
509 "hammer reblock[-btree|-inodes|-dirs|-data] "
510 "<filesystem> [fill_percentage]\n"
511 "hammer pfs-status <dirpath> ...\n"
512 "hammer pfs-master <dirpath> [options]\n"
513 "hammer pfs-slave <dirpath> [options]\n"
514 "hammer pfs-update <dirpath> [options]\n"
515 "hammer pfs-upgrade <dirpath>\n"
516 "hammer pfs-downgrade <dirpath>\n"
517 "hammer pfs-destroy <dirpath>\n"
518 "hammer mirror-read <filesystem> [begin-tid]\n"
519 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
520 "hammer mirror-write <filesystem>\n"
521 "hammer mirror-dump\n"
522 "hammer mirror-copy [[user@]host:]<filesystem>"
523 " [[user@]host:]<filesystem>\n"
524 "hammer mirror-stream [[user@]host:]<filesystem>"
525 " [[user@]host:]<filesystem>\n"
526 "hammer version <filesystem>\n"
527 "hammer version-upgrade <filesystem> <version> [force]\n"
528 "hammer expand <filesystem> <device>\n"
531 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
534 "hammer config [<filesystem> [<configfile>]]\n"
535 "hammer viconfig [<filesystem>]\n"
536 "hammer snap <path> [<note>]\n"
537 "hammer snaplo <path> [<note>]\n"
538 "hammer snapq <dir> [<note>]\n"
539 "hammer snaprm {<path> | <transid>} ...\n"
540 "hammer snapls [<path> ...]\n"
543 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
546 "hammer -f blkdevs show-undo\n"