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