Merge branch 'vendor/BINUTILS225'
[dragonfly.git] / sbin / hammer2 / main.c
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
38 int DebugOpt;
39 int VerboseOpt;
40 int QuietOpt;
41 int NormalExit = 1;     /* if set to 0 main() has to pthread_exit() */
42 int RecurseOpt;
43 int ForceOpt;
44
45 static void usage(int code);
46
47 int
48 main(int ac, char **av)
49 {
50         const char *sel_path = NULL;
51         const char *uuid_str = NULL;
52         const char *arg;
53         int pfs_type = HAMMER2_PFSTYPE_NONE;
54         int all_opt = 0;
55         int ecode = 0;
56         int ch;
57
58         srandomdev();
59         signal(SIGPIPE, SIG_IGN);
60         dmsg_crypto_setup();
61
62         /*
63          * Core options
64          */
65         while ((ch = getopt(ac, av, "adfrqs:t:u:v")) != -1) {
66                 switch(ch) {
67                 case 'a':
68                         all_opt = 1;
69                         break;
70                 case 'd':
71                         if (DebugOpt)
72                                 ++DMsgDebugOpt;
73                         DebugOpt = 1;
74                         break;
75                 case 'f':
76                         ForceOpt = 1;
77                         break;
78                 case 'r':
79                         RecurseOpt = 1;
80                         break;
81                 case 's':
82                         sel_path = optarg;
83                         break;
84                 case 't':
85                         /*
86                          * set node type for mkpfs
87                          */
88                         if (strcasecmp(optarg, "CACHE") == 0) {
89                                 pfs_type = HAMMER2_PFSTYPE_CACHE;
90                         } else if (strcasecmp(optarg, "COPY") == 0) {
91                                 pfs_type = HAMMER2_PFSTYPE_COPY;
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;
112                 case 'v':
113                         if (QuietOpt)
114                                 --QuietOpt;
115                         else
116                                 ++VerboseOpt;
117                         break;
118                 case 'q':
119                         if (VerboseOpt)
120                                 --VerboseOpt;
121                         else
122                                 ++QuietOpt;
123                         break;
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]);
152         } else if (strcmp(av[0], "chaindump") == 0) {
153                 if (ac < 2)
154                         ecode = cmd_chaindump(".");
155                 else
156                         ecode = cmd_chaindump(av[1]);
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]);
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]);
176         } else if (strcmp(av[0], "hash") == 0) {
177                 ecode = cmd_hash(ac - 1, (const char **)(void *)&av[1]);
178         } else if (strcmp(av[0], "status") == 0) {
179                 /*
180                  * Get status of PFS and its connections (-a for all PFSs)
181                  */
182                 if (ac < 2) {
183                         ecode = cmd_remote_status(sel_path, all_opt);
184                 } else {
185                         int i;
186
187                         for (i = 1; i < ac; ++i)
188                                 ecode = cmd_remote_status(av[i], all_opt);
189                 }
190         } else if (strcmp(av[0], "pfs-clid") == 0) {
191                 /*
192                  * Print cluster id (uuid) for specific PFS
193                  */
194                 if (ac < 2) {
195                         fprintf(stderr, "pfs-clid: requires name\n");
196                         usage(1);
197                 }
198                 ecode = cmd_pfs_getid(sel_path, av[1], 0);
199         } else if (strcmp(av[0], "pfs-fsid") == 0) {
200                 /*
201                  * Print private id (uuid) for specific PFS
202                  */
203                 if (ac < 2) {
204                         fprintf(stderr, "pfs-fsid: requires name\n");
205                         usage(1);
206                 }
207                 ecode = cmd_pfs_getid(sel_path, av[1], 1);
208         } else if (strcmp(av[0], "pfs-list") == 0) {
209                 /*
210                  * List all PFSs
211                  */
212                 if (ac > 2) {
213                         fprintf(stderr, "pfs-list: too many arguments\n");
214                         usage(1);
215                 }
216                 ecode = cmd_pfs_list((ac == 2) ? av[1] : sel_path);
217         } else if (strcmp(av[0], "pfs-create") == 0) {
218                 /*
219                  * Create new PFS using pfs_type
220                  */
221                 if (ac < 2) {
222                         fprintf(stderr, "pfs-create: requires name\n");
223                         usage(1);
224                 }
225                 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str);
226         } else if (strcmp(av[0], "pfs-delete") == 0) {
227                 /*
228                  * Delete a PFS by name
229                  */
230                 if (ac < 2) {
231                         fprintf(stderr, "pfs-delete: requires name\n");
232                         usage(1);
233                 }
234                 ecode = cmd_pfs_delete(sel_path, av[1]);
235         } else if (strcmp(av[0], "snapshot") == 0) {
236                 /*
237                  * Create snapshot with optional pfs-type and optional
238                  * label override.
239                  */
240                 if (ac > 3) {
241                         fprintf(stderr, "pfs-snapshot: too many arguments\n");
242                         usage(1);
243                 }
244                 switch(ac) {
245                 case 1:
246                         ecode = cmd_pfs_snapshot(sel_path, NULL, NULL);
247                         break;
248                 case 2:
249                         ecode = cmd_pfs_snapshot(sel_path, av[1], NULL);
250                         break;
251                 case 3:
252                         ecode = cmd_pfs_snapshot(sel_path, av[1], av[2]);
253                         break;
254                 }
255         } else if (strcmp(av[0], "service") == 0) {
256                 /*
257                  * Start the service daemon.  This daemon accepts
258                  * connections from local and remote clients, handles
259                  * the security handshake, and manages the core messaging
260                  * protocol.
261                  */
262                 ecode = cmd_service();
263         } else if (strcmp(av[0], "stat") == 0) {
264                 ecode = cmd_stat(ac - 1, (const char **)(void *)&av[1]);
265         } else if (strcmp(av[0], "leaf") == 0) {
266                 /*
267                  * Start the management daemon for a specific PFS.
268                  *
269                  * This will typically connect to the local master node
270                  * daemon, register the PFS, and then pass its side of
271                  * the socket descriptor to the kernel HAMMER2 VFS via an
272                  * ioctl().  The process and/or thread context remains in the
273                  * kernel until the PFS is unmounted or the connection is
274                  * lost, then returns from the ioctl.
275                  *
276                  * It is possible to connect directly to a remote master node
277                  * instead of the local master node in situations where
278                  * encryption is not desired or no local master node is
279                  * desired.  This is not recommended because it represents
280                  * a single point of failure for the PFS's communications.
281                  *
282                  * Direct kernel<->kernel communication between HAMMER2 VFSs
283                  * is theoretically possible for directly-connected
284                  * registrations (i.e. where the spanning tree is degenerate),
285                  * but not recommended.  We specifically try to reduce the
286                  * complexity of the HAMMER2 VFS kernel code.
287                  */
288                 ecode = cmd_leaf(sel_path);
289         } else if (strcmp(av[0], "shell") == 0) {
290                 /*
291                  * Connect to the command line monitor in the hammer2 master
292                  * node for the machine using HAMMER2_DBG_SHELL messages.
293                  */
294                 ecode = cmd_shell((ac < 2) ? NULL : av[1]);
295         } else if (strcmp(av[0], "rsainit") == 0) {
296                 /*
297                  * Initialize a RSA keypair.  If no target directory is
298                  * specified we default to "/etc/hammer2".
299                  */
300                 arg = (ac < 2) ? HAMMER2_DEFAULT_DIR : av[1];
301                 ecode = cmd_rsainit(arg);
302         } else if (strcmp(av[0], "rsaenc") == 0) {
303                 /*
304                  * Encrypt the input symmetrically by running it through
305                  * the specified public and/or private key files.
306                  *
307                  * If no key files are specified data is encoded using
308                  * "/etc/hammer2/rsa.pub".
309                  *
310                  * WARNING: no padding is added, data stream must contain
311                  *          random padding for this to be secure.
312                  *
313                  * Used for debugging only
314                  */
315                 if (ac == 1) {
316                         const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.pub";
317                         ecode = cmd_rsaenc(&rsapath, 1);
318                 } else {
319                         ecode = cmd_rsaenc((const char **)(void *)&av[1],
320                                            ac - 1);
321                 }
322         } else if (strcmp(av[0], "rsadec") == 0) {
323                 /*
324                  * Decrypt the input symmetrically by running it through
325                  * the specified public and/or private key files.
326                  *
327                  * If no key files are specified data is decoded using
328                  * "/etc/hammer2/rsa.prv".
329                  *
330                  * WARNING: no padding is added, data stream must contain
331                  *          random padding for this to be secure.
332                  *
333                  * Used for debugging only
334                  */
335                 if (ac == 1) {
336                         const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.prv";
337                         ecode = cmd_rsadec(&rsapath, 1);
338                 } else {
339                         ecode = cmd_rsadec((const char **)(void *)&av[1],
340                                            ac - 1);
341                 }
342         } else if (strcmp(av[0], "show") == 0) {
343                 /*
344                  * Raw dump of filesystem.  Use -v to check all crc's, and
345                  * -vv to dump bulk file data.
346                  */
347                 if (ac != 2) {
348                         fprintf(stderr, "show: requires device path\n");
349                         usage(1);
350                 } else {
351                         cmd_show(av[1], 0);
352                 }
353         } else if (strcmp(av[0], "freemap") == 0) {
354                 /*
355                  * Raw dump of freemap.  Use -v to check all crc's, and
356                  * -vv to dump bulk file data.
357                  */
358                 if (ac != 2) {
359                         fprintf(stderr, "freemap: requires device path\n");
360                         usage(1);
361                 } else {
362                         cmd_show(av[1], 1);
363                 }
364         } else if (strcmp(av[0], "setcomp") == 0) {
365                 if (ac < 3) {
366                         /*
367                          * Missing compression method and at least one
368                          * path.
369                          */
370                         fprintf(stderr,
371                                 "setcomp: requires compression method and"
372                                 "directory/file path\n");
373                         usage(1);
374                 } else {
375                         /*
376                          * Multiple paths may be specified
377                          */
378                         ecode = cmd_setcomp(av[1], &av[2]);
379                 }
380         } else if (strcmp(av[0], "setcheck") == 0) {
381                 if (ac < 3) {
382                         /*
383                          * Missing compression method and at least one
384                          * path.
385                          */
386                         fprintf(stderr,
387                                 "setcheck: requires check code method and"
388                                 "directory/file path\n");
389                         usage(1);
390                 } else {
391                         /*
392                          * Multiple paths may be specified
393                          */
394                         ecode = cmd_setcheck(av[1], &av[2]);
395                 }
396         } else if (strcmp(av[0], "clrcheck") == 0) {
397                 ecode = cmd_setcheck("none", &av[1]);
398         } else if (strcmp(av[0], "setcrc32") == 0) {
399                 ecode = cmd_setcheck("crc32", &av[1]);
400         } else if (strcmp(av[0], "setcrc64") == 0) {
401                 ecode = cmd_setcheck("crc64", &av[1]);
402         } else if (strcmp(av[0], "setsha192") == 0) {
403                 ecode = cmd_setcheck("sha192", &av[1]);
404         } else if (strcmp(av[0], "printinode") == 0) {
405                 if (ac != 2) {
406                         fprintf(stderr,
407                                 "printinode: requires directory/file path\n");
408                         usage(1);
409                 } else {
410                         print_inode(av[1]);
411                 }
412         } else if (strcmp(av[0], "bulkfree") == 0) {
413                 if (ac != 2) {
414                         fprintf(stderr,
415                                 "bulkfree: requires path to mount\n");
416                         usage(1);
417                 } else {
418                         ecode = cmd_bulkfree(av[1]);
419                 }
420         } else {
421                 fprintf(stderr, "Unrecognized command: %s\n", av[0]);
422                 usage(1);
423         }
424
425         /*
426          * In DebugMode we may wind up starting several pthreads in the
427          * original process, in which case we have to let them run and
428          * not actually exit.
429          */
430         if (NormalExit) {
431                 return (ecode);
432         } else {
433                 pthread_exit(NULL);
434                 _exit(2);       /* NOT REACHED */
435         }
436 }
437
438 static
439 void
440 usage(int code)
441 {
442         fprintf(stderr,
443                 "hammer2 [options] command...\n"
444                 "    -s path            Select filesystem\n"
445                 "    -t type            PFS type for pfs-create\n"
446                 "    -u uuid            uuid for pfs-create\n"
447                 "\n"
448                 "    connect <target>             "
449                         "Add cluster link\n"
450                 "    disconnect <target>          "
451                         "Del cluster link\n"
452                 "    hash filename*               "
453                         "Print directory hash\n"
454                 "    status [<path>...]           "
455                         "Report cluster status\n"
456                 "    pfs-list [<path>]            "
457                         "List PFSs\n"
458                 "    pfs-clid <label>             "
459                         "Print cluster id for specific PFS\n"
460                 "    pfs-fsid <label>             "
461                         "Print private id for specific PFS\n"
462                 "    pfs-create <label>           "
463                         "Create a PFS\n"
464                 "    pfs-delete <label>           "
465                         "Destroy a PFS\n"
466                 "    snapshot <path> [<label>]           "
467                         "Snapshot a PFS or directory\n"
468                 "    service                      "
469                         "Start service daemon\n"
470                 "    stat [<path>]                "
471                         "Return inode quota & config\n"
472                 "    leaf                         "
473                         "Start pfs leaf daemon\n"
474                 "    shell [<host>]               "
475                         "Connect to debug shell\n"
476                 "    debugspan <target>           "
477                         "Connect to target, run CONN/SPAN\n"
478                 "    rsainit                      "
479                         "Initialize rsa fields\n"
480                 "    show devpath                 "
481                         "Raw hammer2 media dump\n"
482                 "    freemap devpath              "
483                         "Raw hammer2 media dump\n"
484                 "    setcomp comp[:level] path... "
485                         "Set comp algo {none, autozero, lz4, zlib} & level\n"
486                 "    setcheck check path...       "
487                         "Set check algo {none, crc32, crc64, sha192}\n"
488                 "    clrcheck path...             "
489                         "Clear check code override\n"
490                 "    setcrc32 path...             "
491                         "Set check algo to crc32\n"
492                 "    setcrc64 path...             "
493                         "Set check algo to crc64\n"
494                 "    setsha192 path...            "
495                         "Set check algo to sha192\n"
496                 "    bulkfree path...             "
497                         "Run bulkfree pass\n"
498         );
499         exit(code);
500 }