kernel/netgraph7: Fix panic
[dragonfly.git] / sbin / hammer2 / main.c
CommitLineData
2910a90c
MD
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "hammer2.h"
37
5ba65e34 38int DebugOpt;
bd878fc9 39int VerboseOpt;
9b8b748f 40int QuietOpt;
5ba65e34 41int NormalExit = 1; /* if set to 0 main() has to pthread_exit() */
f481450f
MD
42int RecurseOpt;
43int ForceOpt;
44
45static void usage(int code);
5ba65e34 46
2910a90c
MD
47int
48main(int ac, char **av)
49{
5ba65e34 50 const char *sel_path = NULL;
2910a90c 51 const char *uuid_str = NULL;
62efe6ec 52 const char *arg;
7750fd72 53 int pfs_type = HAMMER2_PFSTYPE_NONE;
2910a90c
MD
54 int all_opt = 0;
55 int ecode = 0;
56 int ch;
57
62efe6ec 58 srandomdev();
7dc0f844 59 signal(SIGPIPE, SIG_IGN);
0c3a8cd0 60 dmsg_crypto_setup();
62efe6ec 61
2910a90c
MD
62 /*
63 * Core options
64 */
f481450f 65 while ((ch = getopt(ac, av, "adfrqs:t:u:v")) != -1) {
2910a90c
MD
66 switch(ch) {
67 case 'a':
68 all_opt = 1;
69 break;
bd878fc9 70 case 'd':
78b00acd 71 if (DebugOpt)
d157d4dc 72 ++DMsgDebugOpt;
bd878fc9
MD
73 DebugOpt = 1;
74 break;
f481450f
MD
75 case 'f':
76 ForceOpt = 1;
77 break;
78 case 'r':
79 RecurseOpt = 1;
80 break;
2910a90c
MD
81 case 's':
82 sel_path = optarg;
83 break;
84 case 't':
85 /*
86 * set node type for mkpfs
87 */
afa78c43 88 if (strcasecmp(optarg, "CACHE") == 0) {
2910a90c 89 pfs_type = HAMMER2_PFSTYPE_CACHE;
b93cc2e0
MD
90 } else if (strcasecmp(optarg, "DUMMY") == 0) {
91 pfs_type = HAMMER2_PFSTYPE_DUMMY;
2910a90c
MD
92 } else if (strcasecmp(optarg, "SLAVE") == 0) {
93 pfs_type = HAMMER2_PFSTYPE_SLAVE;
94 } else if (strcasecmp(optarg, "SOFT_SLAVE") == 0) {
95 pfs_type = HAMMER2_PFSTYPE_SOFT_SLAVE;
96 } else if (strcasecmp(optarg, "SOFT_MASTER") == 0) {
97 pfs_type = HAMMER2_PFSTYPE_SOFT_MASTER;
98 } else if (strcasecmp(optarg, "MASTER") == 0) {
99 pfs_type = HAMMER2_PFSTYPE_MASTER;
100 } else {
101 fprintf(stderr, "-t: Unrecognized node type\n");
102 usage(1);
103 }
104 break;
105 case 'u':
106 /*
107 * set uuid for mkpfs, else one will be generated
108 * (required for all except the MASTER node_type)
109 */
110 uuid_str = optarg;
111 break;
bd878fc9 112 case 'v':
9b8b748f
MD
113 if (QuietOpt)
114 --QuietOpt;
115 else
116 ++VerboseOpt;
117 break;
118 case 'q':
119 if (VerboseOpt)
120 --VerboseOpt;
121 else
122 ++QuietOpt;
5ba65e34 123 break;
2910a90c
MD
124 default:
125 fprintf(stderr, "Unknown option: %c\n", ch);
126 usage(1);
127 /* not reached */
128 break;
129 }
130 }
131
132 /*
133 * Adjust, then process the command
134 */
135 ac -= optind;
136 av += optind;
137 if (ac < 1) {
138 fprintf(stderr, "Missing command\n");
139 usage(1);
140 /* not reached */
141 }
142
143 if (strcmp(av[0], "connect") == 0) {
144 /*
145 * Add cluster connection
146 */
147 if (ac < 2) {
148 fprintf(stderr, "connect: missing argument\n");
149 usage(1);
150 }
151 ecode = cmd_remote_connect(sel_path, av[1]);
8f579e46
MD
152 } else if (strcmp(av[0], "chaindump") == 0) {
153 if (ac < 2)
154 ecode = cmd_chaindump(".");
155 else
156 ecode = cmd_chaindump(av[1]);
02454b3e
MD
157 } else if (strcmp(av[0], "debugspan") == 0) {
158 /*
159 * Debug connection to the target hammer2 service and run
160 * the CONN/SPAN protocol.
161 */
162 if (ac < 2) {
163 fprintf(stderr, "debugspan: requires hostname\n");
164 usage(1);
165 }
166 ecode = cmd_debugspan(av[1]);
2910a90c
MD
167 } else if (strcmp(av[0], "disconnect") == 0) {
168 /*
169 * Remove cluster connection
170 */
171 if (ac < 2) {
172 fprintf(stderr, "disconnect: missing argument\n");
173 usage(1);
174 }
175 ecode = cmd_remote_disconnect(sel_path, av[1]);
f6aebb44
MD
176 } else if (strcmp(av[0], "destroy") == 0) {
177 if (ac < 2) {
178 fprintf(stderr,
179 "Specify one or more paths to destroy\n");
180 }
181 ecode = cmd_destroy_path(ac - 1, (const char **)(void *)&av[1]);
775153c2
MD
182 } else if (strcmp(av[0], "hash") == 0) {
183 ecode = cmd_hash(ac - 1, (const char **)(void *)&av[1]);
b92bfd39
MD
184 } else if (strcmp(av[0], "info") == 0) {
185 ecode = cmd_info(ac - 1, (const char **)(void *)&av[1]);
186 } else if (strcmp(av[0], "mountall") == 0) {
187 ecode = cmd_mountall(ac - 1, (const char **)(void *)&av[1]);
2910a90c
MD
188 } else if (strcmp(av[0], "status") == 0) {
189 /*
190 * Get status of PFS and its connections (-a for all PFSs)
191 */
3c198419
MD
192 if (ac < 2) {
193 ecode = cmd_remote_status(sel_path, all_opt);
194 } else {
195 int i;
196
197 for (i = 1; i < ac; ++i)
198 ecode = cmd_remote_status(av[i], all_opt);
199 }
7dc0f844
MD
200 } else if (strcmp(av[0], "pfs-clid") == 0) {
201 /*
202 * Print cluster id (uuid) for specific PFS
203 */
204 if (ac < 2) {
205 fprintf(stderr, "pfs-clid: requires name\n");
206 usage(1);
207 }
208 ecode = cmd_pfs_getid(sel_path, av[1], 0);
209 } else if (strcmp(av[0], "pfs-fsid") == 0) {
210 /*
211 * Print private id (uuid) for specific PFS
212 */
213 if (ac < 2) {
214 fprintf(stderr, "pfs-fsid: requires name\n");
215 usage(1);
216 }
217 ecode = cmd_pfs_getid(sel_path, av[1], 1);
bd878fc9 218 } else if (strcmp(av[0], "pfs-list") == 0) {
ae183399
MD
219 /*
220 * List all PFSs
221 */
b92bfd39
MD
222 if (ac >= 2) {
223 ecode = cmd_pfs_list(ac - 1,
224 (const char **)(void *)&av[1]);
225 } else {
226 ecode = cmd_pfs_list(1, &sel_path);
d0ceb671 227 }
bd878fc9 228 } else if (strcmp(av[0], "pfs-create") == 0) {
2910a90c
MD
229 /*
230 * Create new PFS using pfs_type
231 */
ae183399 232 if (ac < 2) {
bd878fc9 233 fprintf(stderr, "pfs-create: requires name\n");
ae183399
MD
234 usage(1);
235 }
236 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str);
bd878fc9 237 } else if (strcmp(av[0], "pfs-delete") == 0) {
ae183399
MD
238 /*
239 * Delete a PFS by name
240 */
241 if (ac < 2) {
bd878fc9 242 fprintf(stderr, "pfs-delete: requires name\n");
ae183399
MD
243 usage(1);
244 }
245 ecode = cmd_pfs_delete(sel_path, av[1]);
2910a90c
MD
246 } else if (strcmp(av[0], "snapshot") == 0) {
247 /*
bd878fc9 248 * Create snapshot with optional pfs-type and optional
2910a90c
MD
249 * label override.
250 */
d0ceb671 251 if (ac > 3) {
87b1094e
MD
252 fprintf(stderr, "pfs-snapshot: too many arguments\n");
253 usage(1);
254 }
d0ceb671
MD
255 switch(ac) {
256 case 1:
257 ecode = cmd_pfs_snapshot(sel_path, NULL, NULL);
258 break;
259 case 2:
260 ecode = cmd_pfs_snapshot(sel_path, av[1], NULL);
261 break;
262 case 3:
263 ecode = cmd_pfs_snapshot(sel_path, av[1], av[2]);
264 break;
265 }
62efe6ec 266 } else if (strcmp(av[0], "service") == 0) {
2910a90c 267 /*
62efe6ec
MD
268 * Start the service daemon. This daemon accepts
269 * connections from local and remote clients, handles
270 * the security handshake, and manages the core messaging
271 * protocol.
2910a90c 272 */
62efe6ec 273 ecode = cmd_service();
ad7cf8ea 274 } else if (strcmp(av[0], "stat") == 0) {
a1b07447 275 ecode = cmd_stat(ac - 1, (const char **)(void *)&av[1]);
9ab15106
MD
276 } else if (strcmp(av[0], "leaf") == 0) {
277 /*
278 * Start the management daemon for a specific PFS.
279 *
280 * This will typically connect to the local master node
281 * daemon, register the PFS, and then pass its side of
282 * the socket descriptor to the kernel HAMMER2 VFS via an
283 * ioctl(). The process and/or thread context remains in the
284 * kernel until the PFS is unmounted or the connection is
285 * lost, then returns from the ioctl.
286 *
287 * It is possible to connect directly to a remote master node
288 * instead of the local master node in situations where
289 * encryption is not desired or no local master node is
290 * desired. This is not recommended because it represents
291 * a single point of failure for the PFS's communications.
292 *
293 * Direct kernel<->kernel communication between HAMMER2 VFSs
294 * is theoretically possible for directly-connected
295 * registrations (i.e. where the spanning tree is degenerate),
296 * but not recommended. We specifically try to reduce the
297 * complexity of the HAMMER2 VFS kernel code.
298 */
299 ecode = cmd_leaf(sel_path);
bd878fc9 300 } else if (strcmp(av[0], "shell") == 0) {
9ab15106
MD
301 /*
302 * Connect to the command line monitor in the hammer2 master
303 * node for the machine using HAMMER2_DBG_SHELL messages.
304 */
bd878fc9 305 ecode = cmd_shell((ac < 2) ? NULL : av[1]);
62efe6ec
MD
306 } else if (strcmp(av[0], "rsainit") == 0) {
307 /*
308 * Initialize a RSA keypair. If no target directory is
309 * specified we default to "/etc/hammer2".
310 */
311 arg = (ac < 2) ? HAMMER2_DEFAULT_DIR : av[1];
312 ecode = cmd_rsainit(arg);
313 } else if (strcmp(av[0], "rsaenc") == 0) {
314 /*
315 * Encrypt the input symmetrically by running it through
316 * the specified public and/or private key files.
317 *
318 * If no key files are specified data is encoded using
319 * "/etc/hammer2/rsa.pub".
320 *
321 * WARNING: no padding is added, data stream must contain
322 * random padding for this to be secure.
323 *
324 * Used for debugging only
325 */
326 if (ac == 1) {
327 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.pub";
328 ecode = cmd_rsaenc(&rsapath, 1);
329 } else {
a1b07447
MD
330 ecode = cmd_rsaenc((const char **)(void *)&av[1],
331 ac - 1);
62efe6ec
MD
332 }
333 } else if (strcmp(av[0], "rsadec") == 0) {
334 /*
335 * Decrypt the input symmetrically by running it through
336 * the specified public and/or private key files.
337 *
338 * If no key files are specified data is decoded using
339 * "/etc/hammer2/rsa.prv".
340 *
341 * WARNING: no padding is added, data stream must contain
342 * random padding for this to be secure.
343 *
344 * Used for debugging only
345 */
346 if (ac == 1) {
347 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.prv";
348 ecode = cmd_rsadec(&rsapath, 1);
349 } else {
a1b07447
MD
350 ecode = cmd_rsadec((const char **)(void *)&av[1],
351 ac - 1);
62efe6ec 352 }
bd878fc9
MD
353 } else if (strcmp(av[0], "show") == 0) {
354 /*
355 * Raw dump of filesystem. Use -v to check all crc's, and
356 * -vv to dump bulk file data.
357 */
358 if (ac != 2) {
359 fprintf(stderr, "show: requires device path\n");
360 usage(1);
361 } else {
78ac5385
MD
362 cmd_show(av[1], 0);
363 }
364 } else if (strcmp(av[0], "freemap") == 0) {
365 /*
366 * Raw dump of freemap. Use -v to check all crc's, and
367 * -vv to dump bulk file data.
368 */
369 if (ac != 2) {
370 fprintf(stderr, "freemap: requires device path\n");
371 usage(1);
372 } else {
373 cmd_show(av[1], 1);
bd878fc9 374 }
355d67fc 375 } else if (strcmp(av[0], "setcomp") == 0) {
f481450f
MD
376 if (ac < 3) {
377 /*
378 * Missing compression method and at least one
379 * path.
380 */
381 fprintf(stderr,
382 "setcomp: requires compression method and"
355d67fc
MD
383 "directory/file path\n");
384 usage(1);
385 } else {
f481450f
MD
386 /*
387 * Multiple paths may be specified
388 */
389 ecode = cmd_setcomp(av[1], &av[2]);
355d67fc 390 }
8adee7de
MD
391 } else if (strcmp(av[0], "setcheck") == 0) {
392 if (ac < 3) {
393 /*
394 * Missing compression method and at least one
395 * path.
396 */
397 fprintf(stderr,
398 "setcheck: requires check code method and"
399 "directory/file path\n");
400 usage(1);
401 } else {
402 /*
403 * Multiple paths may be specified
404 */
405 ecode = cmd_setcheck(av[1], &av[2]);
406 }
407 } else if (strcmp(av[0], "clrcheck") == 0) {
408 ecode = cmd_setcheck("none", &av[1]);
409 } else if (strcmp(av[0], "setcrc32") == 0) {
410 ecode = cmd_setcheck("crc32", &av[1]);
b83c55fc
MD
411 } else if (strcmp(av[0], "setxxhash64") == 0) {
412 ecode = cmd_setcheck("xxhash64", &av[1]);
8adee7de
MD
413 } else if (strcmp(av[0], "setsha192") == 0) {
414 ecode = cmd_setcheck("sha192", &av[1]);
355d67fc
MD
415 } else if (strcmp(av[0], "printinode") == 0) {
416 if (ac != 2) {
f481450f
MD
417 fprintf(stderr,
418 "printinode: requires directory/file path\n");
355d67fc 419 usage(1);
00261fe0 420 } else {
355d67fc 421 print_inode(av[1]);
00261fe0
MD
422 }
423 } else if (strcmp(av[0], "bulkfree") == 0) {
424 if (ac != 2) {
425 fprintf(stderr,
426 "bulkfree: requires path to mount\n");
427 usage(1);
428 } else {
429 ecode = cmd_bulkfree(av[1]);
430 }
b83c55fc 431#if 0
9dca9515
MD
432 } else if (strcmp(av[0], "bulkfree-async") == 0) {
433 if (ac != 2) {
434 fprintf(stderr,
435 "bulkfree-async: requires path to mount\n");
436 usage(1);
437 } else {
438 ecode = cmd_bulkfree_async(av[1]);
439 }
b83c55fc 440#endif
30ce27d4
MD
441 } else if (strcmp(av[0], "cleanup") == 0) {
442 ecode = cmd_cleanup(av[1]); /* can be NULL */
2910a90c
MD
443 } else {
444 fprintf(stderr, "Unrecognized command: %s\n", av[0]);
445 usage(1);
446 }
5ba65e34
MD
447
448 /*
449 * In DebugMode we may wind up starting several pthreads in the
450 * original process, in which case we have to let them run and
451 * not actually exit.
452 */
453 if (NormalExit) {
454 return (ecode);
455 } else {
456 pthread_exit(NULL);
457 _exit(2); /* NOT REACHED */
458 }
2910a90c
MD
459}
460
461static
462void
463usage(int code)
464{
465 fprintf(stderr,
d0ceb671 466 "hammer2 [options] command...\n"
2910a90c 467 " -s path Select filesystem\n"
bd878fc9
MD
468 " -t type PFS type for pfs-create\n"
469 " -u uuid uuid for pfs-create\n"
470 "\n"
4aedc17b 471 " cleanup [<path>...] "
472 "Run cleanup passes\n"
f481450f
MD
473 " connect <target> "
474 "Add cluster link\n"
f8a108fb
MD
475 " debugspan <target> "
476 "Debug spanning tree\n"
477 " chaindump <path> "
478 "Dump in-memory chain topo at\n"
4aedc17b 479 " destroy <path>* "
f6aebb44 480 "Destroy a directory entry (only use if inode bad)\n"
f481450f
MD
481 " disconnect <target> "
482 "Del cluster link\n"
483 " hash filename* "
484 "Print directory hash\n"
b92bfd39
MD
485 " info [devpath...] "
486 "Info on all offline or online H2 partitions\n"
487 " mountall [devpath...] "
488 "Mount @LOCAL for all H2 partitions\n"
3c198419 489 " status [<path>...] "
b92bfd39
MD
490 "Report active cluster status\n"
491 " pfs-list [<path>...] "
f481450f
MD
492 "List PFSs\n"
493 " pfs-clid <label> "
494 "Print cluster id for specific PFS\n"
495 " pfs-fsid <label> "
496 "Print private id for specific PFS\n"
497 " pfs-create <label> "
498 "Create a PFS\n"
499 " pfs-delete <label> "
500 "Destroy a PFS\n"
4aedc17b 501 " snapshot <path> [<label>] "
d0ceb671 502 "Snapshot a PFS or directory\n"
f481450f
MD
503 " service "
504 "Start service daemon\n"
4aedc17b 505 " stat [<path>] "
f481450f
MD
506 "Return inode quota & config\n"
507 " leaf "
508 "Start pfs leaf daemon\n"
509 " shell [<host>] "
510 "Connect to debug shell\n"
511 " debugspan <target> "
512 "Connect to target, run CONN/SPAN\n"
513 " rsainit "
514 "Initialize rsa fields\n"
515 " show devpath "
516 "Raw hammer2 media dump\n"
517 " freemap devpath "
518 "Raw hammer2 media dump\n"
8adee7de
MD
519 " setcomp comp[:level] path... "
520 "Set comp algo {none, autozero, lz4, zlib} & level\n"
521 " setcheck check path... "
b83c55fc 522 "Set check algo {none, crc32, xxhash64, sha192}\n"
3c198419
MD
523 " clrcheck path... "
524 "Clear check code override\n"
8adee7de
MD
525 " setcrc32 path... "
526 "Set check algo to crc32\n"
b83c55fc
MD
527 " setxxhash64 path... "
528 "Set check algo to xxhash64\n"
8adee7de
MD
529 " setsha192 path... "
530 "Set check algo to sha192\n"
00261fe0
MD
531 " bulkfree path... "
532 "Run bulkfree pass\n"
b83c55fc 533#if 0
4aedc17b 534 " bulkfree-async path... "
9dca9515 535 "Run bulkfree pass asynchronously\n"
b83c55fc 536#endif
2910a90c
MD
537 );
538 exit(code);
539}