Update hammer doc:
[dragonfly.git] / sbin / hammer / hammer.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sbin/hammer/hammer.c,v 1.34 2008/07/16 00:53:48 thomas Exp $
35  */
36
37 #include "hammer.h"
38 #include <signal.h>
39 #include <math.h>
40
41 static void hammer_parsedevs(const char *blkdevs);
42 static void sigalrm(int signo);
43 static void usage(int exit_code);
44
45 int RecurseOpt;
46 int VerboseOpt;
47 int NoSyncOpt;
48 int TwoWayPipeOpt;
49 int TimeoutOpt;
50 const char *CyclePath;
51 const char *LinkPath;
52
53 int
54 main(int ac, char **av)
55 {
56         int ch;
57         u_int32_t status;
58         char *blkdevs = NULL;
59
60         while ((ch = getopt(ac, av, "c:dhf:rs:t:v2")) != -1) {
61                 switch(ch) {
62                 case '2':
63                         TwoWayPipeOpt = 1;
64                         break;
65                 case 'c':
66                         CyclePath = optarg;
67                         break;
68                 case 'd':
69                         ++DebugOpt;
70                         break;
71                 case 'h':
72                         usage(0);
73                         /* not reached */
74                 case 'r':
75                         RecurseOpt = 1;
76                         break;
77                 case 'f':
78                         blkdevs = optarg;
79                         break;
80                 case 's':
81                         LinkPath = optarg;
82                         break;
83                 case 't':
84                         TimeoutOpt = strtol(optarg, NULL, 0);
85                         break;
86                 case 'v':
87                         ++VerboseOpt;
88                         break;
89                 default:
90                         usage(1);
91                         /* not reached */
92                 }
93         }
94         ac -= optind;
95         av += optind;
96         if (ac < 1) {
97                 usage(1);
98                 /* not reached */
99         }
100
101         signal(SIGALRM, sigalrm);
102
103         if (strcmp(av[0], "synctid") == 0) {
104                 hammer_cmd_synctid(av + 1, ac - 1);
105                 exit(0);
106         }
107         if (strcmp(av[0], "namekey") == 0) {
108                 int64_t key;
109
110                 if (av[1] == NULL)
111                         usage(1);
112                 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
113                 if (key == 0)
114                         key |= 0x100000000LL;
115                 printf("0x%016llx\n", key);
116                 exit(0);
117         }
118         if (strcmp(av[0], "namekey32") == 0) {
119                 int32_t key;
120
121                 if (av[1] == NULL)
122                         usage(1);
123                 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
124                 if (key == 0)
125                         ++key;
126                 printf("0x%08x\n", key);
127                 exit(0);
128         }
129         if (strcmp(av[0], "pfs-status") == 0) {
130                 hammer_cmd_pseudofs_status(av + 1, ac - 1);
131                 exit(0);
132         }
133         if (strcmp(av[0], "pfs-master") == 0) {
134                 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
135                 exit(0);
136         }
137         if (strcmp(av[0], "pfs-slave") == 0) {
138                 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
139                 exit(0);
140         }
141         if (strcmp(av[0], "pfs-update") == 0) {
142                 hammer_cmd_pseudofs_update(av + 1, ac - 1);
143                 exit(0);
144         }
145         if (strcmp(av[0], "pfs-upgrade") == 0) {
146                 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
147                 exit(0);
148         }
149         if (strcmp(av[0], "pfs-downgrade") == 0) {
150                 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
151                 exit(0);
152         }
153         if (strcmp(av[0], "pfs-destroy") == 0) {
154                 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
155                 exit(0);
156         }
157         if (strcmp(av[0], "status") == 0) {
158                 hammer_cmd_status(av + 1, ac - 1);
159                 exit(0);
160         }
161         if (strcmp(av[0], "prune") == 0) {
162                 hammer_cmd_softprune(av + 1, ac - 1, 0);
163                 exit(0);
164         }
165         if (strcmp(av[0], "prune-everything") == 0) {
166                 hammer_cmd_softprune(av + 1, ac - 1, 1);
167                 exit(0);
168         }
169         if (strcmp(av[0], "snapshot") == 0) {
170                 hammer_cmd_snapshot(av + 1, ac - 1);
171                 exit(0);
172         }
173         if (strcmp(av[0], "bstats") == 0) {
174                 hammer_cmd_bstats(av + 1, ac - 1);
175                 exit(0);
176         }
177         if (strcmp(av[0], "iostats") == 0) {
178                 hammer_cmd_iostats(av + 1, ac - 1);
179                 exit(0);
180         }
181
182         if (strncmp(av[0], "history", 7) == 0) {
183                 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
184                 exit(0);
185         }
186         if (strncmp(av[0], "reblock", 7) == 0) {
187                 if (strcmp(av[0], "reblock") == 0)
188                         hammer_cmd_reblock(av + 1, ac - 1, -1);
189                 else if (strcmp(av[0], "reblock-btree") == 0)
190                         hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
191                 else if (strcmp(av[0], "reblock-inodes") == 0)
192                         hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
193                 else if (strcmp(av[0], "reblock-dirs") == 0)
194                         hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
195                 else if (strcmp(av[0], "reblock-data") == 0)
196                         hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
197                 else
198                         usage(1);
199                 exit(0);
200         }
201         if (strncmp(av[0], "mirror", 6) == 0) {
202                 if (strcmp(av[0], "mirror-read") == 0)
203                         hammer_cmd_mirror_read(av + 1, ac - 1);
204                 else if (strcmp(av[0], "mirror-write") == 0)
205                         hammer_cmd_mirror_write(av + 1, ac - 1);
206                 else if (strcmp(av[0], "mirror-copy") == 0)
207                         hammer_cmd_mirror_copy(av + 1, ac - 1);
208                 else if (strcmp(av[0], "mirror-dump") == 0)
209                         hammer_cmd_mirror_dump();
210                 else
211                         usage(1);
212                 exit(0);
213         }
214
215         uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
216         if (status != uuid_s_ok) {
217                 errx(1, "uuids file does not have the DragonFly "
218                         "HAMMER filesystem type");
219         }
220
221         if (strcmp(av[0], "show") == 0) {
222                 hammer_off_t node_offset = (hammer_off_t)-1;
223
224                 hammer_parsedevs(blkdevs);
225                 if (ac > 1)
226                         sscanf(av[1], "%llx", &node_offset);
227                 hammer_cmd_show(node_offset, 0, NULL, NULL);
228                 exit(0);
229         }
230         if (strcmp(av[0], "blockmap") == 0) {
231                 hammer_parsedevs(blkdevs);
232                 hammer_cmd_blockmap();
233                 exit(0);
234         }
235         usage(1);
236         /* not reached */
237         return(0);
238 }
239
240 static
241 void
242 hammer_parsedevs(const char *blkdevs)
243 {
244         char *copy;
245         char *volname;
246
247         if (blkdevs == NULL) {
248                 errx(1, "A -f blkdev[:blkdev]* specification is required "
249                         "for this command");
250         }
251
252         copy = strdup(blkdevs);
253         while ((volname = copy) != NULL) {
254                 if ((copy = strchr(copy, ':')) != NULL)
255                         *copy++ = 0;
256                 setup_volume(-1, volname, 0, O_RDONLY);
257         }
258 }
259
260 static
261 void
262 sigalrm(int signo __unused)
263 {
264         /* do nothing (interrupts HAMMER ioctl) */
265 }
266
267 static
268 void
269 usage(int exit_code)
270 {
271         fprintf(stderr, 
272                 "hammer -h\n"
273                 "hammer [-v] [-t seconds] [-c cyclefile] command [argument ...]\n"
274                 "hammer synctid <filesystem> [quick]\n"
275                 "hammer namekey[32] <path>\n"
276                 "hammer prune <softlink-dir>\n"
277                 "hammer prune-everything <filesystem>\n"
278                 "hammer snapshot [filesystem] <snapshot-dir>\n"
279                 "hammer bstats [interval]\n"
280                 "hammer iostats [interval]\n"
281                 "hammer mirror-read <filesystem> [begin-tid]\n"
282                 "hammer mirror-write <filesystem> [file ...]\n"
283                 "hammer mirror-dump\n"
284                 "hammer mirror-copy [[user@]host:]<filesystem>"
285                                   " [[user@]host:]<filesystem>\n"
286                 "hammer reblock[-btree/inodes/dirs/data] "
287                         "<filesystem> [fill_percentage]\n"
288                 "hammer pfs-status <dirpath>\n"
289                 "hammer pfs-master <dirpath> [options]\n"
290                 "hammer pfs-slave <dirpath> [options]\n"
291                 "hammer pfs-update <dirpath> [options]\n"
292                 "hammer pfs-upgrade <dirpath>\n"
293                 "hammer pfs-downgrade <dirpath>\n"
294                 "hammer pfs-destroy <dirpath>\n"
295                 "hammer history[@offset[,len]] <file> ...\n"
296                 "hammer -f blkdev[:blkdev]* [-r] show [offset]\n"
297 #if 0
298                 "hammer -f blkdev[:blkdev]* blockmap\n"
299 #endif
300         );
301         exit(exit_code);
302 }
303