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         int pfs_type = HAMMER2_PFSTYPE_NONE;
49         int quick_opt = 0;
50         int all_opt = 0;
51         int ecode = 0;
52         int ch;
53
54         /*
55          * Core options
56          */
57         while ((ch = getopt(ac, av, "adqs:t:u:")) != -1) {
58                 switch(ch) {
59                 case 'a':
60                         all_opt = 1;
61                         break;
62                 case 'q':
63                         /*
64                          * Quick mode - do not block verifying certain
65                          * operations such as (connect).
66                          */
67                         quick_opt = 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 'd':
103                         DebugOpt = 1;
104                         break;
105                 default:
106                         fprintf(stderr, "Unknown option: %c\n", ch);
107                         usage(1);
108                         /* not reached */
109                         break;
110                 }
111         }
112
113         /*
114          * Adjust, then process the command
115          */
116         ac -= optind;
117         av += optind;
118         if (ac < 1) {
119                 fprintf(stderr, "Missing command\n");
120                 usage(1);
121                 /* not reached */
122         }
123
124         if (strcmp(av[0], "connect") == 0) {
125                 /*
126                  * Add cluster connection
127                  */
128                 if (ac < 2) {
129                         fprintf(stderr, "connect: missing argument\n");
130                         usage(1);
131                 }
132                 ecode = cmd_remote_connect(sel_path, av[1]);
133         } else if (strcmp(av[0], "disconnect") == 0) {
134                 /*
135                  * Remove cluster connection
136                  */
137                 if (ac < 2) {
138                         fprintf(stderr, "disconnect: missing argument\n");
139                         usage(1);
140                 }
141                 ecode = cmd_remote_disconnect(sel_path, av[1]);
142         } else if (strcmp(av[0], "status") == 0) {
143                 /*
144                  * Get status of PFS and its connections (-a for all PFSs)
145                  */
146                 ecode = cmd_remote_status(sel_path, all_opt);
147         } else if (strcmp(av[0], "pfs_list") == 0) {
148                 /*
149                  * List all PFSs
150                  */
151                 ecode = cmd_pfs_list(sel_path);
152         } else if (strcmp(av[0], "pfs_create") == 0) {
153                 /*
154                  * Create new PFS using pfs_type
155                  */
156                 if (ac < 2) {
157                         fprintf(stderr, "pfs_create: requires name\n");
158                         usage(1);
159                 }
160                 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str);
161         } else if (strcmp(av[0], "pfs_delete") == 0) {
162                 /*
163                  * Delete a PFS by name
164                  */
165                 if (ac < 2) {
166                         fprintf(stderr, "pfs_delete: requires name\n");
167                         usage(1);
168                 }
169                 ecode = cmd_pfs_delete(sel_path, av[1]);
170         } else if (strcmp(av[0], "snapshot") == 0) {
171                 /*
172                  * Create snapshot with optional pfs_type and optional
173                  * label override.
174                  */
175         } else if (strcmp(av[0], "node") == 0) {
176                 /*
177                  * Start the master node daemon.  This daemon accepts
178                  * connections from local and remote clients, implements
179                  * and maintains the spanning tree protocol, and manages
180                  * the core messaging protocol.
181                  */
182                 ecode = cmd_node();
183         } else if (strcmp(av[0], "leaf") == 0) {
184                 /*
185                  * Start the management daemon for a specific PFS.
186                  *
187                  * This will typically connect to the local master node
188                  * daemon, register the PFS, and then pass its side of
189                  * the socket descriptor to the kernel HAMMER2 VFS via an
190                  * ioctl().  The process and/or thread context remains in the
191                  * kernel until the PFS is unmounted or the connection is
192                  * lost, then returns from the ioctl.
193                  *
194                  * It is possible to connect directly to a remote master node
195                  * instead of the local master node in situations where
196                  * encryption is not desired or no local master node is
197                  * desired.  This is not recommended because it represents
198                  * a single point of failure for the PFS's communications.
199                  *
200                  * Direct kernel<->kernel communication between HAMMER2 VFSs
201                  * is theoretically possible for directly-connected
202                  * registrations (i.e. where the spanning tree is degenerate),
203                  * but not recommended.  We specifically try to reduce the
204                  * complexity of the HAMMER2 VFS kernel code.
205                  */
206                 ecode = cmd_leaf(sel_path);
207         } else if (strcmp(av[0], "debug") == 0) {
208                 /*
209                  * Connect to the command line monitor in the hammer2 master
210                  * node for the machine using HAMMER2_DBG_SHELL messages.
211                  */
212                 ecode = cmd_debug();
213         } else {
214                 fprintf(stderr, "Unrecognized command: %s\n", av[0]);
215                 usage(1);
216         }
217
218         /*
219          * In DebugMode we may wind up starting several pthreads in the
220          * original process, in which case we have to let them run and
221          * not actually exit.
222          */
223         if (NormalExit) {
224                 return (ecode);
225         } else {
226                 pthread_exit(NULL);
227                 _exit(2);       /* NOT REACHED */
228         }
229 }
230
231 static
232 void
233 usage(int code)
234 {
235         fprintf(stderr,
236                 "hammer2 [-s path] command...\n"
237                 "    -s path            Select filesystem\n"
238         );
239         exit(code);
240 }