HAMMER - Implement experimental volume removal
[dragonfly.git] / sbin / hammer / hammer.c
CommitLineData
0dfeb6c8
MD
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:
10 *
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.
20 *
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.
33 *
de1c0b31 34 * $DragonFly: src/sbin/hammer/hammer.c,v 1.44 2008/11/13 02:04:27 dillon Exp $
0dfeb6c8
MD
35 */
36
61aeeb33 37#include "hammer.h"
0006adae 38#include <signal.h>
d71afe7b 39#include <math.h>
9c9ac2f1 40#include <fstab.h>
0dfeb6c8 41
d38ab092 42static void hammer_parsedevs(const char *blkdevs);
0006adae 43static void sigalrm(int signo);
445faa69 44static void sigintr(int signo);
0dfeb6c8
MD
45static void usage(int exit_code);
46
d38ab092 47int RecurseOpt;
563b4845 48int VerboseOpt;
e95314de 49int QuietOpt;
d71afe7b 50int NoSyncOpt;
243ca327
MD
51int TwoWayPipeOpt;
52int TimeoutOpt;
48eadef9 53int DelayOpt = 5;
6c45ca3e 54char *SshPort;
07485271 55int ForceYesOpt = 0;
e7f926a5 56int ForceOpt;
445faa69
MD
57int RunningIoctl;
58int DidInterrupt;
0bd7a37c 59int BulkOpt;
48eadef9 60u_int64_t BandwidthOpt;
d7ae405c 61const char *CyclePath;
da804f11 62const char *LinkPath;
d38ab092 63
0dfeb6c8
MD
64int
65main(int ac, char **av)
66{
d38ab092 67 char *blkdevs = NULL;
48eadef9
MD
68 char *ptr;
69 u_int32_t status;
70 int ch;
0faa08a1 71 int cacheSize = 0;
0dfeb6c8 72
6c45ca3e 73 while ((ch = getopt(ac, av, "b:c:dhf:i:p:qrs:t:v2yBC:F")) != -1) {
0dfeb6c8 74 switch(ch) {
243ca327
MD
75 case '2':
76 TwoWayPipeOpt = 1;
77 break;
07485271
MN
78 case 'y':
79 ForceYesOpt = 1;
80 break;
48eadef9
MD
81 case 'b':
82 BandwidthOpt = strtoull(optarg, &ptr, 0);
83 switch(*ptr) {
84 case 'g':
85 case 'G':
86 BandwidthOpt *= 1024;
87 /* fall through */
88 case 'm':
89 case 'M':
90 BandwidthOpt *= 1024;
91 /* fall through */
92 case 'k':
93 case 'K':
94 BandwidthOpt *= 1024;
95 break;
224ac2f2
MD
96 case '\0':
97 /* bytes per second if no suffix */
98 break;
48eadef9
MD
99 default:
100 usage(1);
101 }
102 break;
d7ae405c
MD
103 case 'c':
104 CyclePath = optarg;
105 break;
ba7b52c9
MD
106 case 'd':
107 ++DebugOpt;
108 break;
0dfeb6c8
MD
109 case 'h':
110 usage(0);
111 /* not reached */
48eadef9
MD
112 case 'i':
113 DelayOpt = strtol(optarg, NULL, 0);
114 break;
6c45ca3e
MD
115 case 'p':
116 SshPort = optarg;
117 break;
d38ab092
MD
118 case 'r':
119 RecurseOpt = 1;
120 break;
121 case 'f':
122 blkdevs = optarg;
123 break;
da804f11
MD
124 case 's':
125 LinkPath = optarg;
126 break;
0006adae 127 case 't':
243ca327 128 TimeoutOpt = strtol(optarg, NULL, 0);
0006adae 129 break;
563b4845 130 case 'v':
e95314de
MD
131 if (QuietOpt > 0)
132 --QuietOpt;
133 else
134 ++VerboseOpt;
135 break;
136 case 'q':
137 if (VerboseOpt > 0)
138 --VerboseOpt;
139 else
140 ++QuietOpt;
563b4845 141 break;
0bd7a37c
MD
142 case 'B':
143 BulkOpt = 1;
144 break;
0faa08a1
MD
145 case 'C':
146 cacheSize = strtol(optarg, &ptr, 0);
147 switch(*ptr) {
148 case 'm':
149 case 'M':
150 cacheSize *= 1024;
151 /* fall through */
152 case 'k':
153 case 'K':
154 cacheSize *= 1024;
b46b99bf 155 ++ptr;
0faa08a1
MD
156 break;
157 case '\0':
b46b99bf 158 case ':':
0faa08a1
MD
159 /* bytes if no suffix */
160 break;
161 default:
162 usage(1);
163 }
b46b99bf
MD
164 if (*ptr == ':') {
165 UseReadAhead = strtol(ptr + 1, NULL, 0);
166 UseReadBehind = -UseReadAhead;
167 }
0faa08a1
MD
168 if (cacheSize < 1024 * 1024)
169 cacheSize = 1024 * 1024;
b46b99bf
MD
170 if (UseReadAhead < 0)
171 usage(1);
172 if (UseReadAhead * HAMMER_BUFSIZE / cacheSize / 16) {
173 UseReadAhead = cacheSize / 16 / HAMMER_BUFSIZE;
174 UseReadBehind = -UseReadAhead;
175 }
0faa08a1
MD
176 hammer_cache_set(cacheSize);
177 break;
e7f926a5
MD
178 case 'F':
179 ForceOpt = 1;
180 break;
0dfeb6c8
MD
181 default:
182 usage(1);
183 /* not reached */
184 }
185 }
186 ac -= optind;
187 av += optind;
188 if (ac < 1) {
189 usage(1);
190 /* not reached */
191 }
192
243ca327 193 signal(SIGALRM, sigalrm);
445faa69 194 signal(SIGINT, sigintr);
0006adae 195
367431cf
MD
196 if (strcmp(av[0], "synctid") == 0) {
197 hammer_cmd_synctid(av + 1, ac - 1);
198 exit(0);
199 }
5e435c92
MD
200 if (strcmp(av[0], "namekey2") == 0) {
201 int64_t key;
202 int32_t crcx;
203 int len;
204 const char *aname = av[1];
205
206 if (aname == NULL)
207 usage(1);
208 len = strlen(aname);
209 key = (u_int32_t)crc32(aname, len) & 0xFFFFFFFEU;
210
211 switch(len) {
212 default:
213 crcx = crc32(aname + 3, len - 5);
214 crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
215 key |= (int64_t)(crcx & 0x3F) << 42;
216 /* fall through */
217 case 5:
218 case 4:
219 /* fall through */
220 case 3:
221 key |= ((int64_t)(aname[2] & 0x1F) << 48);
222 /* fall through */
223 case 2:
224 key |= ((int64_t)(aname[1] & 0x1F) << 53) |
225 ((int64_t)(aname[len-2] & 0x1F) << 37);
226 /* fall through */
227 case 1:
228 key |= ((int64_t)(aname[0] & 0x1F) << 58) |
229 ((int64_t)(aname[len-1] & 0x1F) << 32);
230 /* fall through */
231 case 0:
232 break;
233 }
234 if (key == 0)
235 key |= 0x100000000LL;
a276dc6b 236 printf("0x%016jx\n", (uintmax_t)key);
5e435c92
MD
237 exit(0);
238 }
239 if (strcmp(av[0], "namekey1") == 0) {
cbd800c2
MD
240 int64_t key;
241
242 if (av[1] == NULL)
243 usage(1);
244 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
245 if (key == 0)
246 key |= 0x100000000LL;
a276dc6b 247 printf("0x%016jx\n", (uintmax_t)key);
cbd800c2
MD
248 exit(0);
249 }
250 if (strcmp(av[0], "namekey32") == 0) {
251 int32_t key;
252
253 if (av[1] == NULL)
254 usage(1);
255 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
256 if (key == 0)
257 ++key;
258 printf("0x%08x\n", key);
259 exit(0);
260 }
34ebae70
MD
261 if (strcmp(av[0], "pfs-status") == 0) {
262 hammer_cmd_pseudofs_status(av + 1, ac - 1);
263 exit(0);
264 }
d4e5b69b
MD
265 if (strcmp(av[0], "pfs-master") == 0) {
266 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
267 exit(0);
268 }
269 if (strcmp(av[0], "pfs-slave") == 0) {
270 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
34ebae70
MD
271 exit(0);
272 }
273 if (strcmp(av[0], "pfs-update") == 0) {
d4e5b69b 274 hammer_cmd_pseudofs_update(av + 1, ac - 1);
66db8054
MD
275 exit(0);
276 }
9c67b4d2
MD
277 if (strcmp(av[0], "pfs-upgrade") == 0) {
278 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
279 exit(0);
280 }
281 if (strcmp(av[0], "pfs-downgrade") == 0) {
282 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
283 exit(0);
284 }
243ca327
MD
285 if (strcmp(av[0], "pfs-destroy") == 0) {
286 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
287 exit(0);
288 }
a7fbbf91
MD
289 if (strcmp(av[0], "status") == 0) {
290 hammer_cmd_status(av + 1, ac - 1);
291 exit(0);
292 }
13ce745d 293 if (strcmp(av[0], "prune") == 0) {
b5aaba7f 294 hammer_cmd_softprune(av + 1, ac - 1, 0);
13ce745d
MD
295 exit(0);
296 }
83f2a3aa
MD
297 if (strcmp(av[0], "config") == 0) {
298 hammer_cmd_config(av + 1, ac - 1);
299 exit(0);
300 }
301 if (strcmp(av[0], "viconfig") == 0) {
302 hammer_cmd_viconfig(av + 1, ac - 1);
303 exit(0);
304 }
6a6e350f
MD
305 if (strcmp(av[0], "cleanup") == 0) {
306 hammer_cmd_cleanup(av + 1, ac - 1);
307 exit(0);
308 }
b66b9421 309 if (strcmp(av[0], "info") == 0) {
eac446c5 310 hammer_cmd_info();
b66b9421
AH
311 exit(0);
312 }
b5aaba7f
MD
313 if (strcmp(av[0], "prune-everything") == 0) {
314 hammer_cmd_softprune(av + 1, ac - 1, 1);
e8969ef0
MD
315 exit(0);
316 }
83f2a3aa
MD
317 if (strcmp(av[0], "snap") == 0) {
318 hammer_cmd_snap(av + 1, ac - 1, 0, 1);
319 exit(0);
320 }
321 if (strcmp(av[0], "snaplo") == 0) {
322 hammer_cmd_snap(av + 1, ac - 1, 0, 0);
323 exit(0);
324 }
325 if (strcmp(av[0], "snapq") == 0) {
326 hammer_cmd_snap(av + 1, ac - 1, 1, 0);
327 exit(0);
328 }
329 if (strcmp(av[0], "snapls") == 0) {
330 hammer_cmd_snapls(av + 1, ac - 1);
331 exit(0);
332 }
333 if (strcmp(av[0], "snaprm") == 0) {
334 hammer_cmd_snaprm(av + 1, ac - 1);
335 exit(0);
336 }
6b669ab4
MN
337 if (strcmp(av[0], "snapshot") == 0) {
338 hammer_cmd_snapshot(av + 1, ac - 1);
339 exit(0);
340 }
68e079b8
MD
341 if (strcmp(av[0], "bstats") == 0) {
342 hammer_cmd_bstats(av + 1, ac - 1);
343 exit(0);
344 }
345 if (strcmp(av[0], "iostats") == 0) {
346 hammer_cmd_iostats(av + 1, ac - 1);
347 exit(0);
348 }
13ce745d
MD
349
350 if (strncmp(av[0], "history", 7) == 0) {
351 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
352 exit(0);
353 }
797a0b63
MD
354 if (strcmp(av[0], "rebalance") == 0) {
355 signal(SIGINT, sigalrm);
356 hammer_cmd_rebalance(av + 1, ac - 1);
357 exit(0);
358 }
ba7b52c9 359 if (strncmp(av[0], "reblock", 7) == 0) {
6a6e350f 360 signal(SIGINT, sigalrm);
ba7b52c9
MD
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);
58c17893
MD
365 else if (strcmp(av[0], "reblock-inodes") == 0)
366 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
9e29c876
MD
367 else if (strcmp(av[0], "reblock-dirs") == 0)
368 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
ba7b52c9
MD
369 else if (strcmp(av[0], "reblock-data") == 0)
370 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
ba7b52c9
MD
371 else
372 usage(1);
3f673d5c
MD
373 exit(0);
374 }
a7fbbf91
MD
375 if (strncmp(av[0], "mirror", 6) == 0) {
376 if (strcmp(av[0], "mirror-read") == 0)
48eadef9 377 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
7a27dae7 378 else if (strcmp(av[0], "mirror-read-stream") == 0)
48eadef9 379 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
a7fbbf91
MD
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)
48eadef9
MD
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);
243ca327
MD
386 else if (strcmp(av[0], "mirror-dump") == 0)
387 hammer_cmd_mirror_dump();
a7fbbf91
MD
388 else
389 usage(1);
390 exit(0);
391 }
de1c0b31
MD
392 if (strcmp(av[0], "version") == 0) {
393 hammer_cmd_get_version(av + 1, ac - 1);
394 exit(0);
395 }
396 if (strcmp(av[0], "version-upgrade") == 0) {
397 hammer_cmd_set_version(av + 1, ac - 1);
398 exit(0);
399 }
d121f61c
MN
400 if (strcmp(av[0], "volume-add") == 0) {
401 hammer_cmd_volume_add(av + 1, ac - 1);
e27700cf
MN
402 exit(0);
403 }
865c9609
MN
404 if (strcmp(av[0], "volume-del") == 0) {
405 hammer_cmd_volume_del(av + 1, ac - 1);
406 exit(0);
407 }
61aeeb33
MD
408
409 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
410 if (status != uuid_s_ok) {
411 errx(1, "uuids file does not have the DragonFly "
412 "HAMMER filesystem type");
413 }
61aeeb33 414
d38ab092 415 if (strcmp(av[0], "show") == 0) {
e7f926a5 416 u_int32_t lo = 0;
a276dc6b 417 intmax_t obj_id = (int64_t)HAMMER_MIN_OBJID;
61aeeb33 418
d38ab092
MD
419 hammer_parsedevs(blkdevs);
420 if (ac > 1)
a276dc6b
MD
421 sscanf(av[1], "%08x:%jx", &lo, &obj_id);
422 hammer_cmd_show(-1, lo, (int64_t)obj_id, 0, NULL, NULL);
d38ab092
MD
423 exit(0);
424 }
6aec797f
MD
425 if (strcmp(av[0], "show-undo") == 0) {
426 hammer_parsedevs(blkdevs);
427 hammer_cmd_show_undo();
428 exit(0);
429 }
eb3f8f1f
MD
430 if (strcmp(av[0], "blockmap") == 0) {
431 hammer_parsedevs(blkdevs);
432 hammer_cmd_blockmap();
433 exit(0);
434 }
61aeeb33
MD
435 usage(1);
436 /* not reached */
0dfeb6c8
MD
437 return(0);
438}
439
9c9ac2f1
MD
440/*
441 * Parse the device specification.
442 *
443 * Multi-volume hammer devices are colon-separated. Each element
444 * may be further expanded via /etc/devtab. One may also specify
445 * a single element which is expanded into multiple elements via
446 * /etc/devtab.
447 */
0dfeb6c8
MD
448static
449void
d38ab092
MD
450hammer_parsedevs(const char *blkdevs)
451{
452 char *copy;
453 char *volname;
454
455 if (blkdevs == NULL) {
f6532f03 456 errx(1, "A -f blkdevs specification is required "
d38ab092
MD
457 "for this command");
458 }
459
460 copy = strdup(blkdevs);
461 while ((volname = copy) != NULL) {
462 if ((copy = strchr(copy, ':')) != NULL)
463 *copy++ = 0;
9c9ac2f1
MD
464 volname = getdevpath(volname, 0);
465 if (strchr(volname, ':'))
466 hammer_parsedevs(volname);
467 else
468 setup_volume(-1, volname, 0, O_RDONLY);
d38ab092
MD
469 }
470}
471
472static
473void
0006adae
MD
474sigalrm(int signo __unused)
475{
476 /* do nothing (interrupts HAMMER ioctl) */
477}
478
479static
480void
445faa69
MD
481sigintr(int signo __unused)
482{
483 if (RunningIoctl == 0)
484 _exit(1);
485 DidInterrupt = 1;
486 /* do nothing (interrupts HAMMER ioctl) */
487}
488
489static
490void
0dfeb6c8
MD
491usage(int exit_code)
492{
493 fprintf(stderr,
494 "hammer -h\n"
4567021b 495 "hammer [-2Bqrvy] [-b bandwidth] [-C cachesize[:readahead]] [-c cyclefile]\n"
f6532f03 496 " [-f blkdevs] [-i delay] [-t seconds] command [argument ...]\n"
84082922 497 "hammer synctid <filesystem> [quick]\n"
f6532f03 498 "hammer -f blkdevs blockmap\n"
bb8e52c0
TN
499 "hammer bstats [interval]\n"
500 "hammer iostats [interval]\n"
501 "hammer history[@offset[,len]] <file> ...\n"
f6532f03 502 "hammer -f blkdevs [-qqq] show [lo:objid]\n"
5e435c92
MD
503 "hammer namekey1 <path>\n"
504 "hammer namekey2 <path>\n"
4567021b 505 "hammer namekey32 <path>\n"
bb8e52c0 506 "hammer cleanup [<filesystem> ...]\n"
b66b9421 507 "hammer info\n"
bb8e52c0 508 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
16265794 509 "hammer snapshot <filesystem> <snapshot-dir> [<note>]\n"
84082922 510 "hammer prune <softlink-dir>\n"
b5aaba7f 511 "hammer prune-everything <filesystem>\n"
797a0b63 512 "hammer rebalance <filesystem> [saturation_percentage]\n"
4567021b 513 "hammer reblock[-btree|-inodes|-dirs|-data] "
84082922 514 "<filesystem> [fill_percentage]\n"
34bb69d8 515 "hammer pfs-status <dirpath> ...\n"
d4e5b69b
MD
516 "hammer pfs-master <dirpath> [options]\n"
517 "hammer pfs-slave <dirpath> [options]\n"
34ebae70 518 "hammer pfs-update <dirpath> [options]\n"
9c67b4d2
MD
519 "hammer pfs-upgrade <dirpath>\n"
520 "hammer pfs-downgrade <dirpath>\n"
521 "hammer pfs-destroy <dirpath>\n"
bb8e52c0
TN
522 "hammer mirror-read <filesystem> [begin-tid]\n"
523 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
524 "hammer mirror-write <filesystem>\n"
525 "hammer mirror-dump\n"
526 "hammer mirror-copy [[user@]host:]<filesystem>"
527 " [[user@]host:]<filesystem>\n"
528 "hammer mirror-stream [[user@]host:]<filesystem>"
529 " [[user@]host:]<filesystem>\n"
5e435c92 530 "hammer version <filesystem>\n"
4567021b 531 "hammer version-upgrade <filesystem> <version> [force]\n"
d121f61c 532 "hammer volume-add <device> <filesystem>\n"
865c9609 533 "hammer volume-del <device> <filesystem>\n"
0dfeb6c8 534 );
83f2a3aa 535
f6532f03 536 fprintf(stderr, "\nHAMMER utility version 3+ commands:\n");
83f2a3aa
MD
537
538 fprintf(stderr,
539 "hammer config [<filesystem> [<configfile>]]\n"
540 "hammer viconfig [<filesystem>]\n"
f6532f03
TN
541 "hammer snap <path> [<note>]\n"
542 "hammer snaplo <path> [<note>]\n"
543 "hammer snapq <dir> [<note>]\n"
544 "hammer snaprm {<path> | <transid>} ...\n"
545 "hammer snapls [<path> ...]\n"
546 );
547
548 fprintf(stderr, "\nHAMMER utility version 4+ commands:\n");
549
550 fprintf(stderr,
551 "hammer -f blkdevs show-undo\n"
83f2a3aa
MD
552 );
553
0dfeb6c8
MD
554 exit(exit_code);
555}
d38ab092 556