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