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