hammer2 - spanning tree and messaging work
[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 static void usage(int code);
39
40 int DebugOpt;
41 int VerboseOpt;
42 int QuietOpt;
43 int NormalExit = 1;     /* if set to 0 main() has to pthread_exit() */
44
45 int
46 main(int ac, char **av)
47 {
48         const char *sel_path = NULL;
49         const char *uuid_str = NULL;
50         const char *arg;
51         int pfs_type = HAMMER2_PFSTYPE_NONE;
52         int all_opt = 0;
53         int ecode = 0;
54         int ch;
55
56         srandomdev();
57         signal(SIGPIPE, SIG_IGN);
58
59         /*
60          * Core options
61          */
62         while ((ch = getopt(ac, av, "adqs:t:u:v")) != -1) {
63                 switch(ch) {
64                 case 'a':
65                         all_opt = 1;
66                         break;
67                 case 'd':
68                         DebugOpt = 1;
69                         break;
70                 case 's':
71                         sel_path = optarg;
72                         break;
73                 case 't':
74                         /*
75                          * set node type for mkpfs
76                          */
77                         if (strcasecmp(optarg, "ADMIN") == 0) {
78                                 pfs_type = HAMMER2_PFSTYPE_ADMIN;
79                         } else if (strcasecmp(optarg, "CACHE") == 0) {
80                                 pfs_type = HAMMER2_PFSTYPE_CACHE;
81                         } else if (strcasecmp(optarg, "COPY") == 0) {
82                                 pfs_type = HAMMER2_PFSTYPE_COPY;
83                         } else if (strcasecmp(optarg, "SLAVE") == 0) {
84                                 pfs_type = HAMMER2_PFSTYPE_SLAVE;
85                         } else if (strcasecmp(optarg, "SOFT_SLAVE") == 0) {
86                                 pfs_type = HAMMER2_PFSTYPE_SOFT_SLAVE;
87                         } else if (strcasecmp(optarg, "SOFT_MASTER") == 0) {
88                                 pfs_type = HAMMER2_PFSTYPE_SOFT_MASTER;
89                         } else if (strcasecmp(optarg, "MASTER") == 0) {
90                                 pfs_type = HAMMER2_PFSTYPE_MASTER;
91                         } else {
92                                 fprintf(stderr, "-t: Unrecognized node type\n");
93                                 usage(1);
94                         }
95                         break;
96                 case 'u':
97                         /*
98                          * set uuid for mkpfs, else one will be generated
99                          * (required for all except the MASTER node_type)
100                          */
101                         uuid_str = optarg;
102                         break;
103                 case 'v':
104                         if (QuietOpt)
105                                 --QuietOpt;
106                         else
107                                 ++VerboseOpt;
108                         break;
109                 case 'q':
110                         if (VerboseOpt)
111                                 --VerboseOpt;
112                         else
113                                 ++QuietOpt;
114                         break;
115                 default:
116                         fprintf(stderr, "Unknown option: %c\n", ch);
117                         usage(1);
118                         /* not reached */
119                         break;
120                 }
121         }
122
123         /*
124          * Adjust, then process the command
125          */
126         ac -= optind;
127         av += optind;
128         if (ac < 1) {
129                 fprintf(stderr, "Missing command\n");
130                 usage(1);
131                 /* not reached */
132         }
133
134         if (strcmp(av[0], "connect") == 0) {
135                 /*
136                  * Add cluster connection
137                  */
138                 if (ac < 2) {
139                         fprintf(stderr, "connect: missing argument\n");
140                         usage(1);
141                 }
142                 ecode = cmd_remote_connect(sel_path, av[1]);
143         } else if (strcmp(av[0], "disconnect") == 0) {
144                 /*
145                  * Remove cluster connection
146                  */
147                 if (ac < 2) {
148                         fprintf(stderr, "disconnect: missing argument\n");
149                         usage(1);
150                 }
151                 ecode = cmd_remote_disconnect(sel_path, av[1]);
152         } else if (strcmp(av[0], "status") == 0) {
153                 /*
154                  * Get status of PFS and its connections (-a for all PFSs)
155                  */
156                 ecode = cmd_remote_status(sel_path, all_opt);
157         } else if (strcmp(av[0], "pfs-clid") == 0) {
158                 /*
159                  * Print cluster id (uuid) for specific PFS
160                  */
161                 if (ac < 2) {
162                         fprintf(stderr, "pfs-clid: requires name\n");
163                         usage(1);
164                 }
165                 ecode = cmd_pfs_getid(sel_path, av[1], 0);
166         } else if (strcmp(av[0], "pfs-fsid") == 0) {
167                 /*
168                  * Print private id (uuid) for specific PFS
169                  */
170                 if (ac < 2) {
171                         fprintf(stderr, "pfs-fsid: requires name\n");
172                         usage(1);
173                 }
174                 ecode = cmd_pfs_getid(sel_path, av[1], 1);
175         } else if (strcmp(av[0], "pfs-list") == 0) {
176                 /*
177                  * List all PFSs
178                  */
179                 ecode = cmd_pfs_list(sel_path);
180         } else if (strcmp(av[0], "pfs-create") == 0) {
181                 /*
182                  * Create new PFS using pfs_type
183                  */
184                 if (ac < 2) {
185                         fprintf(stderr, "pfs-create: requires name\n");
186                         usage(1);
187                 }
188                 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str);
189         } else if (strcmp(av[0], "pfs-delete") == 0) {
190                 /*
191                  * Delete a PFS by name
192                  */
193                 if (ac < 2) {
194                         fprintf(stderr, "pfs-delete: requires name\n");
195                         usage(1);
196                 }
197                 ecode = cmd_pfs_delete(sel_path, av[1]);
198         } else if (strcmp(av[0], "snapshot") == 0) {
199                 /*
200                  * Create snapshot with optional pfs-type and optional
201                  * label override.
202                  */
203         } else if (strcmp(av[0], "service") == 0) {
204                 /*
205                  * Start the service daemon.  This daemon accepts
206                  * connections from local and remote clients, handles
207                  * the security handshake, and manages the core messaging
208                  * protocol.
209                  */
210                 ecode = cmd_service();
211         } else if (strcmp(av[0], "stat") == 0) {
212                 ecode = cmd_stat(ac - 1, (const char **)&av[1]);
213         } else if (strcmp(av[0], "leaf") == 0) {
214                 /*
215                  * Start the management daemon for a specific PFS.
216                  *
217                  * This will typically connect to the local master node
218                  * daemon, register the PFS, and then pass its side of
219                  * the socket descriptor to the kernel HAMMER2 VFS via an
220                  * ioctl().  The process and/or thread context remains in the
221                  * kernel until the PFS is unmounted or the connection is
222                  * lost, then returns from the ioctl.
223                  *
224                  * It is possible to connect directly to a remote master node
225                  * instead of the local master node in situations where
226                  * encryption is not desired or no local master node is
227                  * desired.  This is not recommended because it represents
228                  * a single point of failure for the PFS's communications.
229                  *
230                  * Direct kernel<->kernel communication between HAMMER2 VFSs
231                  * is theoretically possible for directly-connected
232                  * registrations (i.e. where the spanning tree is degenerate),
233                  * but not recommended.  We specifically try to reduce the
234                  * complexity of the HAMMER2 VFS kernel code.
235                  */
236                 ecode = cmd_leaf(sel_path);
237         } else if (strcmp(av[0], "shell") == 0) {
238                 /*
239                  * Connect to the command line monitor in the hammer2 master
240                  * node for the machine using HAMMER2_DBG_SHELL messages.
241                  */
242                 ecode = cmd_shell((ac < 2) ? NULL : av[1]);
243         } else if (strcmp(av[0], "rsainit") == 0) {
244                 /*
245                  * Initialize a RSA keypair.  If no target directory is
246                  * specified we default to "/etc/hammer2".
247                  */
248                 arg = (ac < 2) ? HAMMER2_DEFAULT_DIR : av[1];
249                 ecode = cmd_rsainit(arg);
250         } else if (strcmp(av[0], "rsaenc") == 0) {
251                 /*
252                  * Encrypt the input symmetrically by running it through
253                  * the specified public and/or private key files.
254                  *
255                  * If no key files are specified data is encoded using
256                  * "/etc/hammer2/rsa.pub".
257                  *
258                  * WARNING: no padding is added, data stream must contain
259                  *          random padding for this to be secure.
260                  *
261                  * Used for debugging only
262                  */
263                 if (ac == 1) {
264                         const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.pub";
265                         ecode = cmd_rsaenc(&rsapath, 1);
266                 } else {
267                         ecode = cmd_rsaenc((const char **)&av[1], ac - 1);
268                 }
269         } else if (strcmp(av[0], "rsadec") == 0) {
270                 /*
271                  * Decrypt the input symmetrically by running it through
272                  * the specified public and/or private key files.
273                  *
274                  * If no key files are specified data is decoded using
275                  * "/etc/hammer2/rsa.prv".
276                  *
277                  * WARNING: no padding is added, data stream must contain
278                  *          random padding for this to be secure.
279                  *
280                  * Used for debugging only
281                  */
282                 if (ac == 1) {
283                         const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.prv";
284                         ecode = cmd_rsadec(&rsapath, 1);
285                 } else {
286                         ecode = cmd_rsadec((const char **)&av[1], ac - 1);
287                 }
288         } else if (strcmp(av[0], "show") == 0) {
289                 /*
290                  * Raw dump of filesystem.  Use -v to check all crc's, and
291                  * -vv to dump bulk file data.
292                  */
293                 if (ac != 2) {
294                         fprintf(stderr, "show: requires device path\n");
295                         usage(1);
296                 } else {
297                         cmd_show(av[1]);
298                 }
299         } else {
300                 fprintf(stderr, "Unrecognized command: %s\n", av[0]);
301                 usage(1);
302         }
303
304         /*
305          * In DebugMode we may wind up starting several pthreads in the
306          * original process, in which case we have to let them run and
307          * not actually exit.
308          */
309         if (NormalExit) {
310                 return (ecode);
311         } else {
312                 pthread_exit(NULL);
313                 _exit(2);       /* NOT REACHED */
314         }
315 }
316
317 static
318 void
319 usage(int code)
320 {
321         fprintf(stderr,
322                 "hammer2 [-s path] command...\n"
323                 "    -s path            Select filesystem\n"
324                 "    -t type            PFS type for pfs-create\n"
325                 "    -u uuid            uuid for pfs-create\n"
326                 "\n"
327                 "    connect <target>   Add cluster link\n"
328                 "    disconnect <target> Del cluster link\n"
329                 "    status             Report cluster status\n"
330                 "    pfs-list           List PFSs\n"
331                 "    pfs-clid <label>   Print cluster id for specific PFS\n"
332                 "    pfs-fsid <label>   Print private id for specific PFS\n"
333                 "    pfs-create <label> Create a PFS\n"
334                 "    pfs-delete <label> Destroy a PFS\n"
335                 "    snapshot           Snapshot a PFS\n"
336                 "    service            Start service daemon\n"
337                 "    stat [<path>]      Return inode quota & config\n"
338                 "    leaf               Start pfs leaf daemon\n"
339                 "    shell [<host>]     Connect to debug shell\n"
340                 "    rsainit            Initialize rsa fields\n"
341                 "    show devpath       Raw hammer2 media dump\n"
342         );
343         exit(code);
344 }