hammer(8): Add -X to usage().
[dragonfly.git] / sbin / hammer / hammer.c
blobc63f931a9eed1e158e3516328ba06c5f83c50e2d
1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/sbin/hammer/hammer.c,v 1.44 2008/11/13 02:04:27 dillon Exp $
37 #include "hammer.h"
38 #include <signal.h>
39 #include <math.h>
40 #include <fstab.h>
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);
47 int RecurseOpt;
48 int VerboseOpt;
49 int QuietOpt;
50 int NoSyncOpt;
51 int TwoWayPipeOpt;
52 int TimeoutOpt;
53 int DelayOpt = 5;
54 char *SshPort;
55 int ForceYesOpt = 0;
56 int CompressOpt;
57 int ForceOpt;
58 int RunningIoctl;
59 int DidInterrupt;
60 int BulkOpt;
61 u_int64_t BandwidthOpt;
62 const char *CyclePath;
63 const char *LinkPath;
65 int
66 main(int ac, char **av)
68 char *blkdevs = NULL;
69 char *ptr;
70 u_int32_t status;
71 int ch;
72 int cacheSize = 0;
74 while ((ch = getopt(ac, av, "b:c:dhf:i:p:qrs:t:v2yBC:FX")) != -1) {
75 switch(ch) {
76 case '2':
77 TwoWayPipeOpt = 1;
78 break;
79 case 'y':
80 ForceYesOpt = 1;
81 break;
82 case 'b':
83 BandwidthOpt = strtoull(optarg, &ptr, 0);
84 switch(*ptr) {
85 case 'g':
86 case 'G':
87 BandwidthOpt *= 1024;
88 /* fall through */
89 case 'm':
90 case 'M':
91 BandwidthOpt *= 1024;
92 /* fall through */
93 case 'k':
94 case 'K':
95 BandwidthOpt *= 1024;
96 break;
97 case '\0':
98 /* bytes per second if no suffix */
99 break;
100 default:
101 usage(1);
103 break;
104 case 'c':
105 CyclePath = optarg;
106 break;
107 case 'd':
108 ++DebugOpt;
109 break;
110 case 'h':
111 usage(0);
112 /* not reached */
113 case 'i':
114 DelayOpt = strtol(optarg, NULL, 0);
115 break;
116 case 'p':
117 SshPort = optarg;
118 break;
119 case 'r':
120 RecurseOpt = 1;
121 break;
122 case 'f':
123 blkdevs = optarg;
124 break;
125 case 's':
126 LinkPath = optarg;
127 break;
128 case 't':
129 TimeoutOpt = strtol(optarg, NULL, 0);
130 break;
131 case 'v':
132 if (QuietOpt > 0)
133 --QuietOpt;
134 else
135 ++VerboseOpt;
136 break;
137 case 'q':
138 if (VerboseOpt > 0)
139 --VerboseOpt;
140 else
141 ++QuietOpt;
142 break;
143 case 'B':
144 BulkOpt = 1;
145 break;
146 case 'C':
147 cacheSize = strtol(optarg, &ptr, 0);
148 switch(*ptr) {
149 case 'm':
150 case 'M':
151 cacheSize *= 1024;
152 /* fall through */
153 case 'k':
154 case 'K':
155 cacheSize *= 1024;
156 ++ptr;
157 break;
158 case '\0':
159 case ':':
160 /* bytes if no suffix */
161 break;
162 default:
163 usage(1);
165 if (*ptr == ':') {
166 UseReadAhead = strtol(ptr + 1, NULL, 0);
167 UseReadBehind = -UseReadAhead;
169 if (cacheSize < 1024 * 1024)
170 cacheSize = 1024 * 1024;
171 if (UseReadAhead < 0)
172 usage(1);
173 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
174 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
175 UseReadBehind = -UseReadAhead;
177 hammer_cache_set(cacheSize);
178 break;
179 case 'F':
180 ForceOpt = 1;
181 break;
182 case 'X':
183 CompressOpt = 1;
184 break;
185 default:
186 usage(1);
187 /* not reached */
190 ac -= optind;
191 av += optind;
192 if (ac < 1) {
193 usage(1);
194 /* not reached */
197 signal(SIGALRM, sigalrm);
198 signal(SIGINT, sigintr);
200 if (strcmp(av[0], "synctid") == 0) {
201 hammer_cmd_synctid(av + 1, ac - 1);
202 exit(0);
204 if (strcmp(av[0], "namekey2") == 0) {
205 int64_t key;
206 int32_t crcx;
207 int len;
208 const char *aname = av[1];
210 if (aname == NULL)
211 usage(1);
212 len = strlen(aname);
213 key = (u_int32_t)crc32(aname, len) & 0xFFFFFFFEU;
215 switch(len) {
216 default:
217 crcx = crc32(aname + 3, len - 5);
218 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
219 key |= (int64_t)(crcx & 0x3F) << 42;
220 /* fall through */
221 case 5:
222 case 4:
223 /* fall through */
224 case 3:
225 key |= ((int64_t)(aname[2] & 0x1F) << 48);
226 /* fall through */
227 case 2:
228 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
229 ((int64_t)(aname[len-2] & 0x1F) << 37);
230 /* fall through */
231 case 1:
232 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
233 ((int64_t)(aname[len-1] & 0x1F) << 32);
234 /* fall through */
235 case 0:
236 break;
238 if (key == 0)
239 key |= 0x100000000LL;
240 printf("0x%016jx\n", (uintmax_t)key);
241 exit(0);
243 if (strcmp(av[0], "namekey1") == 0) {
244 int64_t key;
246 if (av[1] == NULL)
247 usage(1);
248 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
249 if (key == 0)
250 key |= 0x100000000LL;
251 printf("0x%016jx\n", (uintmax_t)key);
252 exit(0);
254 if (strcmp(av[0], "namekey32") == 0) {
255 int32_t key;
257 if (av[1] == NULL)
258 usage(1);
259 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
260 if (key == 0)
261 ++key;
262 printf("0x%08x\n", key);
263 exit(0);
265 if (strcmp(av[0], "pfs-status") == 0) {
266 hammer_cmd_pseudofs_status(av + 1, ac - 1);
267 exit(0);
269 if (strcmp(av[0], "pfs-master") == 0) {
270 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
271 exit(0);
273 if (strcmp(av[0], "pfs-slave") == 0) {
274 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
275 exit(0);
277 if (strcmp(av[0], "pfs-update") == 0) {
278 hammer_cmd_pseudofs_update(av + 1, ac - 1);
279 exit(0);
281 if (strcmp(av[0], "pfs-upgrade") == 0) {
282 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
283 exit(0);
285 if (strcmp(av[0], "pfs-downgrade") == 0) {
286 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
287 exit(0);
289 if (strcmp(av[0], "pfs-destroy") == 0) {
290 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
291 exit(0);
293 if (strcmp(av[0], "status") == 0) {
294 hammer_cmd_status(av + 1, ac - 1);
295 exit(0);
297 if (strcmp(av[0], "prune") == 0) {
298 hammer_cmd_softprune(av + 1, ac - 1, 0);
299 exit(0);
301 if (strcmp(av[0], "config") == 0) {
302 hammer_cmd_config(av + 1, ac - 1);
303 exit(0);
305 if (strcmp(av[0], "viconfig") == 0) {
306 hammer_cmd_viconfig(av + 1, ac - 1);
307 exit(0);
309 if (strcmp(av[0], "cleanup") == 0) {
310 hammer_cmd_cleanup(av + 1, ac - 1);
311 exit(0);
313 if (strcmp(av[0], "info") == 0) {
314 hammer_cmd_info();
315 exit(0);
317 if (strcmp(av[0], "prune-everything") == 0) {
318 hammer_cmd_softprune(av + 1, ac - 1, 1);
319 exit(0);
321 if (strcmp(av[0], "snap") == 0) {
322 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
323 exit(0);
325 if (strcmp(av[0], "snaplo") == 0) {
326 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
327 exit(0);
329 if (strcmp(av[0], "snapq") == 0) {
330 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
331 exit(0);
333 if (strcmp(av[0], "snapls") == 0) {
334 hammer_cmd_snapls(av + 1, ac - 1);
335 exit(0);
337 if (strcmp(av[0], "snaprm") == 0) {
338 hammer_cmd_snaprm(av + 1, ac - 1);
339 exit(0);
341 if (strcmp(av[0], "snapshot") == 0) {
342 hammer_cmd_snapshot(av + 1, ac - 1);
343 exit(0);
345 if (strcmp(av[0], "bstats") == 0) {
346 hammer_cmd_bstats(av + 1, ac - 1);
347 exit(0);
349 if (strcmp(av[0], "iostats") == 0) {
350 hammer_cmd_iostats(av + 1, ac - 1);
351 exit(0);
354 if (strncmp(av[0], "history", 7) == 0) {
355 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
356 exit(0);
358 if (strcmp(av[0], "rebalance") == 0) {
359 signal(SIGINT, sigalrm);
360 hammer_cmd_rebalance(av + 1, ac - 1);
361 exit(0);
363 if (strncmp(av[0], "reblock", 7) == 0) {
364 signal(SIGINT, sigalrm);
365 if (strcmp(av[0], "reblock") == 0)
366 hammer_cmd_reblock(av + 1, ac - 1, -1);
367 else if (strcmp(av[0], "reblock-btree") == 0)
368 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
369 else if (strcmp(av[0], "reblock-inodes") == 0)
370 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
371 else if (strcmp(av[0], "reblock-dirs") == 0)
372 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
373 else if (strcmp(av[0], "reblock-data") == 0)
374 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
375 else
376 usage(1);
377 exit(0);
379 if (strncmp(av[0], "mirror", 6) == 0) {
380 if (strcmp(av[0], "mirror-read") == 0)
381 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
382 else if (strcmp(av[0], "mirror-read-stream") == 0)
383 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
384 else if (strcmp(av[0], "mirror-write") == 0)
385 hammer_cmd_mirror_write(av + 1, ac - 1);
386 else if (strcmp(av[0], "mirror-copy") == 0)
387 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
388 else if (strcmp(av[0], "mirror-stream") == 0)
389 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
390 else if (strcmp(av[0], "mirror-dump") == 0)
391 hammer_cmd_mirror_dump();
392 else
393 usage(1);
394 exit(0);
396 if (strcmp(av[0], "version") == 0) {
397 hammer_cmd_get_version(av + 1, ac - 1);
398 exit(0);
400 if (strcmp(av[0], "version-upgrade") == 0) {
401 hammer_cmd_set_version(av + 1, ac - 1);
402 exit(0);
404 if (strcmp(av[0], "volume-add") == 0) {
405 hammer_cmd_volume_add(av + 1, ac - 1);
406 exit(0);
408 if (strcmp(av[0], "volume-del") == 0) {
409 hammer_cmd_volume_del(av + 1, ac - 1);
410 exit(0);
413 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
414 if (status != uuid_s_ok) {
415 errx(1, "uuids file does not have the DragonFly "
416 "HAMMER filesystem type");
419 if (strcmp(av[0], "show") == 0) {
420 u_int32_t lo = 0;
421 intmax_t obj_id = (int64_t)HAMMER_MIN_OBJID;
423 hammer_parsedevs(blkdevs);
424 if (ac > 1)
425 sscanf(av[1], "%08x:%jx", &lo, &obj_id);
426 hammer_cmd_show(-1, lo, (int64_t)obj_id, 0, NULL, NULL);
427 exit(0);
429 if (strcmp(av[0], "show-undo") == 0) {
430 hammer_parsedevs(blkdevs);
431 hammer_cmd_show_undo();
432 exit(0);
434 if (strcmp(av[0], "blockmap") == 0) {
435 hammer_parsedevs(blkdevs);
436 hammer_cmd_blockmap();
437 exit(0);
439 usage(1);
440 /* not reached */
441 return(0);
445 * Parse the device specification.
447 * Multi-volume hammer devices are colon-separated. Each element
448 * may be further expanded via /etc/devtab. One may also specify
449 * a single element which is expanded into multiple elements via
450 * /etc/devtab.
452 static
453 void
454 hammer_parsedevs(const char *blkdevs)
456 char *copy;
457 char *volname;
459 if (blkdevs == NULL) {
460 errx(1, "A -f blkdevs specification is required "
461 "for this command");
464 copy = strdup(blkdevs);
465 while ((volname = copy) != NULL) {
466 if ((copy = strchr(copy, ':')) != NULL)
467 *copy++ = 0;
468 volname = getdevpath(volname, 0);
469 if (strchr(volname, ':'))
470 hammer_parsedevs(volname);
471 else
472 setup_volume(-1, volname, 0, O_RDONLY);
476 static
477 void
478 sigalrm(int signo __unused)
480 /* do nothing (interrupts HAMMER ioctl) */
483 static
484 void
485 sigintr(int signo __unused)
487 if (RunningIoctl == 0)
488 _exit(1);
489 DidInterrupt = 1;
490 /* do nothing (interrupts HAMMER ioctl) */
493 static
494 void
495 usage(int exit_code)
497 fprintf(stderr,
498 "hammer -h\n"
499 "hammer [-2BqrvXy] [-b bandwidth] [-C cachesize[:readahead]] [-c cyclefile]\n"
500 " [-f blkdevs] [-i delay] [-t seconds] command [argument ...]\n"
501 "hammer synctid <filesystem> [quick]\n"
502 "hammer -f blkdevs blockmap\n"
503 "hammer bstats [interval]\n"
504 "hammer iostats [interval]\n"
505 "hammer history[@offset[,len]] <file> ...\n"
506 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
507 "hammer namekey1 <path>\n"
508 "hammer namekey2 <path>\n"
509 "hammer namekey32 <path>\n"
510 "hammer cleanup [<filesystem> ...]\n"
511 "hammer info\n"
512 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
513 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
514 "hammer prune <softlink-dir>\n"
515 "hammer prune-everything <filesystem>\n"
516 "hammer rebalance <filesystem> [saturation_percentage]\n"
517 "hammer reblock[-btree|-inodes|-dirs|-data] "
518 "<filesystem> [fill_percentage]\n"
519 "hammer pfs-status <dirpath> ...\n"
520 "hammer pfs-master <dirpath> [options]\n"
521 "hammer pfs-slave <dirpath> [options]\n"
522 "hammer pfs-update <dirpath> [options]\n"
523 "hammer pfs-upgrade <dirpath>\n"
524 "hammer pfs-downgrade <dirpath>\n"
525 "hammer pfs-destroy <dirpath>\n"
526 "hammer mirror-read <filesystem> [begin-tid]\n"
527 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
528 "hammer mirror-write <filesystem>\n"
529 "hammer mirror-dump\n"
530 "hammer mirror-copy [[user@]host:]<filesystem>"
531 " [[user@]host:]<filesystem>\n"
532 "hammer mirror-stream [[user@]host:]<filesystem>"
533 " [[user@]host:]<filesystem>\n"
534 "hammer version <filesystem>\n"
535 "hammer version-upgrade <filesystem> <version> [force]\n"
536 "hammer volume-add <device> <filesystem>\n"
537 "hammer volume-del <device> <filesystem>\n"
540 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
542 fprintf(stderr,
543 "hammer config [<filesystem> [<configfile>]]\n"
544 "hammer viconfig [<filesystem>]\n"
545 "hammer snap <path> [<note>]\n"
546 "hammer snaplo <path> [<note>]\n"
547 "hammer snapq <dir> [<note>]\n"
548 "hammer snaprm {<path> | <transid>} ...\n"
549 "hammer snapls [<path> ...]\n"
552 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
554 fprintf(stderr,
555 "hammer -f blkdevs show-undo\n"
558 exit(exit_code);