Merge branch 'vendor/OPENSSL'
[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                         DebugOpt = 1;
72                         break;
73                 case 'f':
74                         ForceOpt = 1;
75                         break;
76                 case 'r':
77                         RecurseOpt = 1;
78                         break;
79                 case 's':
80                         sel_path = optarg;
81                         break;
82                 case 't':
83                         /*
84                          * set node type for mkpfs
85                          */
86                         if (strcasecmp(optarg, "ADMIN") == 0) {
87                                 pfs_type = HAMMER2_PFSTYPE_ADMIN;
88                         } else 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                 ecode = cmd_remote_status(sel_path, all_opt);
183         } else if (strcmp(av[0], "pfs-clid") == 0) {
184                 /*
185                  * Print cluster id (uuid) for specific PFS
186                  */
187                 if (ac < 2) {
188                         fprintf(stderr, "pfs-clid: requires name\n");
189                         usage(1);
190                 }
191                 ecode = cmd_pfs_getid(sel_path, av[1], 0);
192         } else if (strcmp(av[0], "pfs-fsid") == 0) {
193                 /*
194                  * Print private id (uuid) for specific PFS
195                  */
196                 if (ac < 2) {
197                         fprintf(stderr, "pfs-fsid: requires name\n");
198                         usage(1);
199                 }
200                 ecode = cmd_pfs_getid(sel_path, av[1], 1);
201         } else if (strcmp(av[0], "pfs-list") == 0) {
202                 /*
203                  * List all PFSs
204                  */
205                 ecode = cmd_pfs_list(sel_path);
206         } else if (strcmp(av[0], "pfs-create") == 0) {
207                 /*
208                  * Create new PFS using pfs_type
209                  */
210                 if (ac < 2) {
211                         fprintf(stderr, "pfs-create: requires name\n");
212                         usage(1);
213                 }
214                 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str);
215         } else if (strcmp(av[0], "pfs-delete") == 0) {
216                 /*
217                  * Delete a PFS by name
218                  */
219                 if (ac < 2) {
220                         fprintf(stderr, "pfs-delete: requires name\n");
221                         usage(1);
222                 }
223                 ecode = cmd_pfs_delete(sel_path, av[1]);
224         } else if (strcmp(av[0], "snapshot") == 0) {
225                 /*
226                  * Create snapshot with optional pfs-type and optional
227                  * label override.
228                  */
229                 if (ac > 2) {
230                         fprintf(stderr, "pfs-snapshot: too many arguments\n");
231                         usage(1);
232                 }
233                 if (ac != 2)
234                         ecode = cmd_pfs_snapshot(sel_path, NULL);
235                 else
236                         ecode = cmd_pfs_snapshot(sel_path, av[1]);
237         } else if (strcmp(av[0], "service") == 0) {
238                 /*
239                  * Start the service daemon.  This daemon accepts
240                  * connections from local and remote clients, handles
241                  * the security handshake, and manages the core messaging
242                  * protocol.
243                  */
244                 ecode = cmd_service();
245         } else if (strcmp(av[0], "stat") == 0) {
246                 ecode = cmd_stat(ac - 1, (const char **)(void *)&av[1]);
247         } else if (strcmp(av[0], "leaf") == 0) {
248                 /*
249                  * Start the management daemon for a specific PFS.
250                  *
251                  * This will typically connect to the local master node
252                  * daemon, register the PFS, and then pass its side of
253                  * the socket descriptor to the kernel HAMMER2 VFS via an
254                  * ioctl().  The process and/or thread context remains in the
255                  * kernel until the PFS is unmounted or the connection is
256                  * lost, then returns from the ioctl.
257                  *
258                  * It is possible to connect directly to a remote master node
259                  * instead of the local master node in situations where
260                  * encryption is not desired or no local master node is
261                  * desired.  This is not recommended because it represents
262                  * a single point of failure for the PFS's communications.
263                  *
264                  * Direct kernel<->kernel communication between HAMMER2 VFSs
265                  * is theoretically possible for directly-connected
266                  * registrations (i.e. where the spanning tree is degenerate),
267                  * but not recommended.  We specifically try to reduce the
268                  * complexity of the HAMMER2 VFS kernel code.
269                  */
270                 ecode = cmd_leaf(sel_path);
271         } else if (strcmp(av[0], "shell") == 0) {
272                 /*
273                  * Connect to the command line monitor in the hammer2 master
274                  * node for the machine using HAMMER2_DBG_SHELL messages.
275                  */
276                 ecode = cmd_shell((ac < 2) ? NULL : av[1]);
277         } else if (strcmp(av[0], "rsainit") == 0) {
278                 /*
279                  * Initialize a RSA keypair.  If no target directory is
280                  * specified we default to "/etc/hammer2".
281                  */
282                 arg = (ac < 2) ? HAMMER2_DEFAULT_DIR : av[1];
283                 ecode = cmd_rsainit(arg);
284         } else if (strcmp(av[0], "rsaenc") == 0) {
285                 /*
286                  * Encrypt the input symmetrically by running it through
287                  * the specified public and/or private key files.
288                  *
289                  * If no key files are specified data is encoded using
290                  * "/etc/hammer2/rsa.pub".
291                  *
292                  * WARNING: no padding is added, data stream must contain
293                  *          random padding for this to be secure.
294                  *
295                  * Used for debugging only
296                  */
297                 if (ac == 1) {
298                         const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.pub";
299                         ecode = cmd_rsaenc(&rsapath, 1);
300                 } else {
301                         ecode = cmd_rsaenc((const char **)(void *)&av[1],
302                                            ac - 1);
303                 }
304         } else if (strcmp(av[0], "rsadec") == 0) {
305                 /*
306                  * Decrypt the input symmetrically by running it through
307                  * the specified public and/or private key files.
308                  *
309                  * If no key files are specified data is decoded using
310                  * "/etc/hammer2/rsa.prv".
311                  *
312                  * WARNING: no padding is added, data stream must contain
313                  *          random padding for this to be secure.
314                  *
315                  * Used for debugging only
316                  */
317                 if (ac == 1) {
318                         const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.prv";
319                         ecode = cmd_rsadec(&rsapath, 1);
320                 } else {
321                         ecode = cmd_rsadec((const char **)(void *)&av[1],
322                                            ac - 1);
323                 }
324         } else if (strcmp(av[0], "show") == 0) {
325                 /*
326                  * Raw dump of filesystem.  Use -v to check all crc's, and
327                  * -vv to dump bulk file data.
328                  */
329                 if (ac != 2) {
330                         fprintf(stderr, "show: requires device path\n");
331                         usage(1);
332                 } else {
333                         cmd_show(av[1], 0);
334                 }
335         } else if (strcmp(av[0], "freemap") == 0) {
336                 /*
337                  * Raw dump of freemap.  Use -v to check all crc's, and
338                  * -vv to dump bulk file data.
339                  */
340                 if (ac != 2) {
341                         fprintf(stderr, "freemap: requires device path\n");
342                         usage(1);
343                 } else {
344                         cmd_show(av[1], 1);
345                 }
346         } else if (strcmp(av[0], "setcomp") == 0) {
347                 if (ac < 3) {
348                         /*
349                          * Missing compression method and at least one
350                          * path.
351                          */
352                         fprintf(stderr,
353                                 "setcomp: requires compression method and"
354                                 "directory/file path\n");
355                         usage(1);
356                 } else {
357                         /*
358                          * Multiple paths may be specified
359                          */
360                         ecode = cmd_setcomp(av[1], &av[2]);
361                 }
362         } else if (strcmp(av[0], "printinode") == 0) {
363                 if (ac != 2) {
364                         fprintf(stderr,
365                                 "printinode: requires directory/file path\n");
366                         usage(1);
367                 }
368                 else
369                         print_inode(av[1]);
370         } else {
371                 fprintf(stderr, "Unrecognized command: %s\n", av[0]);
372                 usage(1);
373         }
374
375         /*
376          * In DebugMode we may wind up starting several pthreads in the
377          * original process, in which case we have to let them run and
378          * not actually exit.
379          */
380         if (NormalExit) {
381                 return (ecode);
382         } else {
383                 pthread_exit(NULL);
384                 _exit(2);       /* NOT REACHED */
385         }
386 }
387
388 static
389 void
390 usage(int code)
391 {
392         fprintf(stderr,
393                 "hammer2 [-s path] command...\n"
394                 "    -s path            Select filesystem\n"
395                 "    -t type            PFS type for pfs-create\n"
396                 "    -u uuid            uuid for pfs-create\n"
397                 "\n"
398                 "    connect <target>             "
399                         "Add cluster link\n"
400                 "    disconnect <target>          "
401                         "Del cluster link\n"
402                 "    hash filename*               "
403                         "Print directory hash\n"
404                 "    status                       "
405                         "Report cluster status\n"
406                 "    pfs-list                     "
407                         "List PFSs\n"
408                 "    pfs-clid <label>             "
409                         "Print cluster id for specific PFS\n"
410                 "    pfs-fsid <label>             "
411                         "Print private id for specific PFS\n"
412                 "    pfs-create <label>           "
413                         "Create a PFS\n"
414                 "    pfs-delete <label>           "
415                         "Destroy a PFS\n"
416                 "    snapshot [<label>]           "
417                         "Snapshot a PFS\n"
418                 "    service                      "
419                         "Start service daemon\n"
420                 "    stat [<path>]                "
421                         "Return inode quota & config\n"
422                 "    leaf                         "
423                         "Start pfs leaf daemon\n"
424                 "    shell [<host>]               "
425                         "Connect to debug shell\n"
426                 "    debugspan <target>           "
427                         "Connect to target, run CONN/SPAN\n"
428                 "    rsainit                      "
429                         "Initialize rsa fields\n"
430                 "    show devpath                 "
431                         "Raw hammer2 media dump\n"
432                 "    freemap devpath              "
433                         "Raw hammer2 media dump\n"
434                 "    setcomp comp[:level] path    "
435                         "Set compression {none, autozero, lz4, zlib} & level\n"
436         );
437         exit(code);
438 }