HAMMER Utilities: Cleanup
[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 *
7a27dae7 34 * $DragonFly: src/sbin/hammer/hammer.c,v 1.36 2008/07/31 06:21:19 dillon Exp $
0dfeb6c8
MD
35 */
36
61aeeb33 37#include "hammer.h"
0006adae 38#include <signal.h>
d71afe7b 39#include <math.h>
0dfeb6c8 40
d38ab092 41static void hammer_parsedevs(const char *blkdevs);
0006adae 42static void sigalrm(int signo);
0dfeb6c8
MD
43static void usage(int exit_code);
44
d38ab092 45int RecurseOpt;
563b4845 46int VerboseOpt;
d71afe7b 47int NoSyncOpt;
243ca327
MD
48int TwoWayPipeOpt;
49int TimeoutOpt;
48eadef9
MD
50int DelayOpt = 5;
51u_int64_t BandwidthOpt;
d7ae405c 52const char *CyclePath;
da804f11 53const char *LinkPath;
d38ab092 54
0dfeb6c8
MD
55int
56main(int ac, char **av)
57{
d38ab092 58 char *blkdevs = NULL;
48eadef9
MD
59 char *ptr;
60 u_int32_t status;
61 int ch;
0dfeb6c8 62
48eadef9 63 while ((ch = getopt(ac, av, "b:c:dhf:i:rs:t:v2")) != -1) {
0dfeb6c8 64 switch(ch) {
243ca327
MD
65 case '2':
66 TwoWayPipeOpt = 1;
67 break;
48eadef9
MD
68 case 'b':
69 BandwidthOpt = strtoull(optarg, &ptr, 0);
70 switch(*ptr) {
71 case 'g':
72 case 'G':
73 BandwidthOpt *= 1024;
74 /* fall through */
75 case 'm':
76 case 'M':
77 BandwidthOpt *= 1024;
78 /* fall through */
79 case 'k':
80 case 'K':
81 BandwidthOpt *= 1024;
82 break;
83 default:
84 usage(1);
85 }
86 break;
d7ae405c
MD
87 case 'c':
88 CyclePath = optarg;
89 break;
ba7b52c9
MD
90 case 'd':
91 ++DebugOpt;
92 break;
0dfeb6c8
MD
93 case 'h':
94 usage(0);
95 /* not reached */
48eadef9
MD
96 case 'i':
97 DelayOpt = strtol(optarg, NULL, 0);
98 break;
d38ab092
MD
99 case 'r':
100 RecurseOpt = 1;
101 break;
102 case 'f':
103 blkdevs = optarg;
104 break;
da804f11
MD
105 case 's':
106 LinkPath = optarg;
107 break;
0006adae 108 case 't':
243ca327 109 TimeoutOpt = strtol(optarg, NULL, 0);
0006adae 110 break;
563b4845
MD
111 case 'v':
112 ++VerboseOpt;
113 break;
0dfeb6c8
MD
114 default:
115 usage(1);
116 /* not reached */
117 }
118 }
119 ac -= optind;
120 av += optind;
121 if (ac < 1) {
122 usage(1);
123 /* not reached */
124 }
125
243ca327 126 signal(SIGALRM, sigalrm);
0006adae 127
367431cf
MD
128 if (strcmp(av[0], "synctid") == 0) {
129 hammer_cmd_synctid(av + 1, ac - 1);
130 exit(0);
131 }
cbd800c2
MD
132 if (strcmp(av[0], "namekey") == 0) {
133 int64_t key;
134
135 if (av[1] == NULL)
136 usage(1);
137 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
138 if (key == 0)
139 key |= 0x100000000LL;
140 printf("0x%016llx\n", key);
141 exit(0);
142 }
143 if (strcmp(av[0], "namekey32") == 0) {
144 int32_t key;
145
146 if (av[1] == NULL)
147 usage(1);
148 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
149 if (key == 0)
150 ++key;
151 printf("0x%08x\n", key);
152 exit(0);
153 }
34ebae70
MD
154 if (strcmp(av[0], "pfs-status") == 0) {
155 hammer_cmd_pseudofs_status(av + 1, ac - 1);
156 exit(0);
157 }
d4e5b69b
MD
158 if (strcmp(av[0], "pfs-master") == 0) {
159 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
160 exit(0);
161 }
162 if (strcmp(av[0], "pfs-slave") == 0) {
163 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
34ebae70
MD
164 exit(0);
165 }
166 if (strcmp(av[0], "pfs-update") == 0) {
d4e5b69b 167 hammer_cmd_pseudofs_update(av + 1, ac - 1);
66db8054
MD
168 exit(0);
169 }
9c67b4d2
MD
170 if (strcmp(av[0], "pfs-upgrade") == 0) {
171 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
172 exit(0);
173 }
174 if (strcmp(av[0], "pfs-downgrade") == 0) {
175 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
176 exit(0);
177 }
243ca327
MD
178 if (strcmp(av[0], "pfs-destroy") == 0) {
179 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
180 exit(0);
181 }
a7fbbf91
MD
182 if (strcmp(av[0], "status") == 0) {
183 hammer_cmd_status(av + 1, ac - 1);
184 exit(0);
185 }
13ce745d 186 if (strcmp(av[0], "prune") == 0) {
b5aaba7f 187 hammer_cmd_softprune(av + 1, ac - 1, 0);
13ce745d
MD
188 exit(0);
189 }
b5aaba7f
MD
190 if (strcmp(av[0], "prune-everything") == 0) {
191 hammer_cmd_softprune(av + 1, ac - 1, 1);
e8969ef0
MD
192 exit(0);
193 }
6b669ab4
MN
194 if (strcmp(av[0], "snapshot") == 0) {
195 hammer_cmd_snapshot(av + 1, ac - 1);
196 exit(0);
197 }
68e079b8
MD
198 if (strcmp(av[0], "bstats") == 0) {
199 hammer_cmd_bstats(av + 1, ac - 1);
200 exit(0);
201 }
202 if (strcmp(av[0], "iostats") == 0) {
203 hammer_cmd_iostats(av + 1, ac - 1);
204 exit(0);
205 }
13ce745d
MD
206
207 if (strncmp(av[0], "history", 7) == 0) {
208 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
209 exit(0);
210 }
ba7b52c9
MD
211 if (strncmp(av[0], "reblock", 7) == 0) {
212 if (strcmp(av[0], "reblock") == 0)
213 hammer_cmd_reblock(av + 1, ac - 1, -1);
214 else if (strcmp(av[0], "reblock-btree") == 0)
215 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
58c17893
MD
216 else if (strcmp(av[0], "reblock-inodes") == 0)
217 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
9e29c876
MD
218 else if (strcmp(av[0], "reblock-dirs") == 0)
219 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
ba7b52c9
MD
220 else if (strcmp(av[0], "reblock-data") == 0)
221 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
ba7b52c9
MD
222 else
223 usage(1);
3f673d5c
MD
224 exit(0);
225 }
a7fbbf91
MD
226 if (strncmp(av[0], "mirror", 6) == 0) {
227 if (strcmp(av[0], "mirror-read") == 0)
48eadef9 228 hammer_cmd_mirror_read(av + 1, ac - 1, 0);
7a27dae7 229 else if (strcmp(av[0], "mirror-read-stream") == 0)
48eadef9 230 hammer_cmd_mirror_read(av + 1, ac - 1, 1);
a7fbbf91
MD
231 else if (strcmp(av[0], "mirror-write") == 0)
232 hammer_cmd_mirror_write(av + 1, ac - 1);
233 else if (strcmp(av[0], "mirror-copy") == 0)
48eadef9
MD
234 hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
235 else if (strcmp(av[0], "mirror-stream") == 0)
236 hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
243ca327
MD
237 else if (strcmp(av[0], "mirror-dump") == 0)
238 hammer_cmd_mirror_dump();
a7fbbf91
MD
239 else
240 usage(1);
241 exit(0);
242 }
61aeeb33
MD
243
244 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
245 if (status != uuid_s_ok) {
246 errx(1, "uuids file does not have the DragonFly "
247 "HAMMER filesystem type");
248 }
61aeeb33 249
d38ab092 250 if (strcmp(av[0], "show") == 0) {
47197d71 251 hammer_off_t node_offset = (hammer_off_t)-1;
61aeeb33 252
d38ab092
MD
253 hammer_parsedevs(blkdevs);
254 if (ac > 1)
47197d71
MD
255 sscanf(av[1], "%llx", &node_offset);
256 hammer_cmd_show(node_offset, 0, NULL, NULL);
d38ab092
MD
257 exit(0);
258 }
eb3f8f1f
MD
259 if (strcmp(av[0], "blockmap") == 0) {
260 hammer_parsedevs(blkdevs);
261 hammer_cmd_blockmap();
262 exit(0);
263 }
61aeeb33
MD
264 usage(1);
265 /* not reached */
0dfeb6c8
MD
266 return(0);
267}
268
d38ab092
MD
269static
270void
271hammer_parsedevs(const char *blkdevs)
272{
273 char *copy;
274 char *volname;
275
276 if (blkdevs == NULL) {
84082922 277 errx(1, "A -f blkdev[:blkdev]* specification is required "
d38ab092
MD
278 "for this command");
279 }
280
281 copy = strdup(blkdevs);
282 while ((volname = copy) != NULL) {
283 if ((copy = strchr(copy, ':')) != NULL)
284 *copy++ = 0;
285 setup_volume(-1, volname, 0, O_RDONLY);
286 }
287}
288
0006adae
MD
289static
290void
291sigalrm(int signo __unused)
292{
293 /* do nothing (interrupts HAMMER ioctl) */
294}
295
0dfeb6c8
MD
296static
297void
298usage(int exit_code)
299{
300 fprintf(stderr,
301 "hammer -h\n"
84082922
TN
302 "hammer [-v] [-t seconds] [-c cyclefile] command [argument ...]\n"
303 "hammer synctid <filesystem> [quick]\n"
304 "hammer namekey[32] <path>\n"
305 "hammer prune <softlink-dir>\n"
b5aaba7f 306 "hammer prune-everything <filesystem>\n"
84082922
TN
307 "hammer snapshot [filesystem] <snapshot-dir>\n"
308 "hammer bstats [interval]\n"
309 "hammer iostats [interval]\n"
310 "hammer mirror-read <filesystem> [begin-tid]\n"
311 "hammer mirror-write <filesystem> [file ...]\n"
243ca327 312 "hammer mirror-dump\n"
a7fbbf91
MD
313 "hammer mirror-copy [[user@]host:]<filesystem>"
314 " [[user@]host:]<filesystem>\n"
ba1b6c7d 315 "hammer reblock[-btree/inodes/dirs/data] "
84082922 316 "<filesystem> [fill_percentage]\n"
34ebae70 317 "hammer pfs-status <dirpath>\n"
d4e5b69b
MD
318 "hammer pfs-master <dirpath> [options]\n"
319 "hammer pfs-slave <dirpath> [options]\n"
34ebae70 320 "hammer pfs-update <dirpath> [options]\n"
9c67b4d2
MD
321 "hammer pfs-upgrade <dirpath>\n"
322 "hammer pfs-downgrade <dirpath>\n"
323 "hammer pfs-destroy <dirpath>\n"
84082922
TN
324 "hammer history[@offset[,len]] <file> ...\n"
325 "hammer -f blkdev[:blkdev]* [-r] show [offset]\n"
326#if 0
327 "hammer -f blkdev[:blkdev]* blockmap\n"
328#endif
0dfeb6c8 329 );
0dfeb6c8
MD
330 exit(exit_code);
331}
d38ab092 332