fc5eb5ba9a06b77e049d8f5a1264c263ad161851
[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.44 2008/11/13 02:04:27 dillon 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 sigintr(int signo);
44 static void usage(int exit_code);
45
46 int RecurseOpt;
47 int VerboseOpt;
48 int QuietOpt;
49 int NoSyncOpt;
50 int TwoWayPipeOpt;
51 int TimeoutOpt;
52 int DelayOpt = 5;
53 int RunningIoctl;
54 int DidInterrupt;
55 u_int64_t BandwidthOpt;
56 const char *CyclePath;
57 const char *LinkPath;
58
59 int
60 main(int ac, char **av)
61 {
62         char *blkdevs = NULL;
63         char *ptr;
64         u_int32_t status;
65         int ch;
66
67         while ((ch = getopt(ac, av, "b:c:dhf:i:qrs:t:v2")) != -1) {
68                 switch(ch) {
69                 case '2':
70                         TwoWayPipeOpt = 1;
71                         break;
72                 case 'b':
73                         BandwidthOpt = strtoull(optarg, &ptr, 0);
74                         switch(*ptr) {
75                         case 'g':
76                         case 'G':
77                                 BandwidthOpt *= 1024;
78                                 /* fall through */
79                         case 'm':
80                         case 'M':
81                                 BandwidthOpt *= 1024;
82                                 /* fall through */
83                         case 'k':
84                         case 'K':
85                                 BandwidthOpt *= 1024;
86                                 break;
87                         default:
88                                 usage(1);
89                         }
90                         break;
91                 case 'c':
92                         CyclePath = optarg;
93                         break;
94                 case 'd':
95                         ++DebugOpt;
96                         break;
97                 case 'h':
98                         usage(0);
99                         /* not reached */
100                 case 'i':
101                         DelayOpt = strtol(optarg, NULL, 0);
102                         break;
103                 case 'r':
104                         RecurseOpt = 1;
105                         break;
106                 case 'f':
107                         blkdevs = optarg;
108                         break;
109                 case 's':
110                         LinkPath = optarg;
111                         break;
112                 case 't':
113                         TimeoutOpt = strtol(optarg, NULL, 0);
114                         break;
115                 case 'v':
116                         if (QuietOpt > 0)
117                                 --QuietOpt;
118                         else
119                                 ++VerboseOpt;
120                         break;
121                 case 'q':
122                         if (VerboseOpt > 0)
123                                 --VerboseOpt;
124                         else
125                                 ++QuietOpt;
126                         break;
127                 default:
128                         usage(1);
129                         /* not reached */
130                 }
131         }
132         ac -= optind;
133         av += optind;
134         if (ac < 1) {
135                 usage(1);
136                 /* not reached */
137         }
138
139         signal(SIGALRM, sigalrm);
140         signal(SIGINT, sigintr);
141
142         if (strcmp(av[0], "synctid") == 0) {
143                 hammer_cmd_synctid(av + 1, ac - 1);
144                 exit(0);
145         }
146         if (strcmp(av[0], "namekey2") == 0) {
147                 int64_t key;
148                 int32_t crcx;
149                 int len;
150                 const char *aname = av[1];
151
152                 if (aname == NULL)
153                         usage(1);
154                 len = strlen(aname);
155                 key = (u_int32_t)crc32(aname, len) & 0xFFFFFFFEU;
156
157                 switch(len) {
158                 default:
159                         crcx = crc32(aname + 3, len - 5);
160                         crcx = crcx ^ (crcx >> 6) ^ (crcx >> 12);
161                         key |= (int64_t)(crcx & 0x3F) << 42;
162                         /* fall through */
163                 case 5:
164                 case 4:
165                         /* fall through */
166                 case 3:
167                         key |= ((int64_t)(aname[2] & 0x1F) << 48);
168                         /* fall through */
169                 case 2:
170                         key |= ((int64_t)(aname[1] & 0x1F) << 53) |
171                                ((int64_t)(aname[len-2] & 0x1F) << 37);
172                         /* fall through */
173                 case 1:
174                         key |= ((int64_t)(aname[0] & 0x1F) << 58) |
175                                ((int64_t)(aname[len-1] & 0x1F) << 32);
176                         /* fall through */
177                 case 0:
178                         break;
179                 }
180                 if (key == 0)
181                         key |= 0x100000000LL;
182                 printf("0x%016llx\n", key);
183                 exit(0);
184         }
185         if (strcmp(av[0], "namekey1") == 0) {
186                 int64_t key;
187
188                 if (av[1] == NULL)
189                         usage(1);
190                 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
191                 if (key == 0)
192                         key |= 0x100000000LL;
193                 printf("0x%016llx\n", key);
194                 exit(0);
195         }
196         if (strcmp(av[0], "namekey32") == 0) {
197                 int32_t key;
198
199                 if (av[1] == NULL)
200                         usage(1);
201                 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
202                 if (key == 0)
203                         ++key;
204                 printf("0x%08x\n", key);
205                 exit(0);
206         }
207         if (strcmp(av[0], "pfs-status") == 0) {
208                 hammer_cmd_pseudofs_status(av + 1, ac - 1);
209                 exit(0);
210         }
211         if (strcmp(av[0], "pfs-master") == 0) {
212                 hammer_cmd_pseudofs_create(av + 1, ac - 1, 0);
213                 exit(0);
214         }
215         if (strcmp(av[0], "pfs-slave") == 0) {
216                 hammer_cmd_pseudofs_create(av + 1, ac - 1, 1);
217                 exit(0);
218         }
219         if (strcmp(av[0], "pfs-update") == 0) {
220                 hammer_cmd_pseudofs_update(av + 1, ac - 1);
221                 exit(0);
222         }
223         if (strcmp(av[0], "pfs-upgrade") == 0) {
224                 hammer_cmd_pseudofs_upgrade(av + 1, ac - 1);
225                 exit(0);
226         }
227         if (strcmp(av[0], "pfs-downgrade") == 0) {
228                 hammer_cmd_pseudofs_downgrade(av + 1, ac - 1);
229                 exit(0);
230         }
231         if (strcmp(av[0], "pfs-destroy") == 0) {
232                 hammer_cmd_pseudofs_destroy(av + 1, ac - 1);
233                 exit(0);
234         }
235         if (strcmp(av[0], "status") == 0) {
236                 hammer_cmd_status(av + 1, ac - 1);
237                 exit(0);
238         }
239         if (strcmp(av[0], "prune") == 0) {
240                 hammer_cmd_softprune(av + 1, ac - 1, 0);
241                 exit(0);
242         }
243         if (strcmp(av[0], "cleanup") == 0) {
244                 hammer_cmd_cleanup(av + 1, ac - 1);
245                 exit(0);
246         }
247         if (strcmp(av[0], "prune-everything") == 0) {
248                 hammer_cmd_softprune(av + 1, ac - 1, 1);
249                 exit(0);
250         }
251         if (strcmp(av[0], "snapshot") == 0) {
252                 hammer_cmd_snapshot(av + 1, ac - 1);
253                 exit(0);
254         }
255         if (strcmp(av[0], "bstats") == 0) {
256                 hammer_cmd_bstats(av + 1, ac - 1);
257                 exit(0);
258         }
259         if (strcmp(av[0], "iostats") == 0) {
260                 hammer_cmd_iostats(av + 1, ac - 1);
261                 exit(0);
262         }
263
264         if (strncmp(av[0], "history", 7) == 0) {
265                 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
266                 exit(0);
267         }
268         if (strncmp(av[0], "reblock", 7) == 0) {
269                 signal(SIGINT, sigalrm);
270                 if (strcmp(av[0], "reblock") == 0)
271                         hammer_cmd_reblock(av + 1, ac - 1, -1);
272                 else if (strcmp(av[0], "reblock-btree") == 0)
273                         hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
274                 else if (strcmp(av[0], "reblock-inodes") == 0)
275                         hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
276                 else if (strcmp(av[0], "reblock-dirs") == 0)
277                         hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
278                 else if (strcmp(av[0], "reblock-data") == 0)
279                         hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
280                 else
281                         usage(1);
282                 exit(0);
283         }
284         if (strncmp(av[0], "mirror", 6) == 0) {
285                 if (strcmp(av[0], "mirror-read") == 0)
286                         hammer_cmd_mirror_read(av + 1, ac - 1, 0);
287                 else if (strcmp(av[0], "mirror-read-stream") == 0)
288                         hammer_cmd_mirror_read(av + 1, ac - 1, 1);
289                 else if (strcmp(av[0], "mirror-write") == 0)
290                         hammer_cmd_mirror_write(av + 1, ac - 1);
291                 else if (strcmp(av[0], "mirror-copy") == 0)
292                         hammer_cmd_mirror_copy(av + 1, ac - 1, 0);
293                 else if (strcmp(av[0], "mirror-stream") == 0)
294                         hammer_cmd_mirror_copy(av + 1, ac - 1, 1);
295                 else if (strcmp(av[0], "mirror-dump") == 0)
296                         hammer_cmd_mirror_dump();
297                 else
298                         usage(1);
299                 exit(0);
300         }
301         if (strcmp(av[0], "version") == 0) {
302                 hammer_cmd_get_version(av + 1, ac - 1);
303                 exit(0);
304         }
305         if (strcmp(av[0], "version-upgrade") == 0) {
306                 hammer_cmd_set_version(av + 1, ac - 1);
307                 exit(0);
308         }
309
310         uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
311         if (status != uuid_s_ok) {
312                 errx(1, "uuids file does not have the DragonFly "
313                         "HAMMER filesystem type");
314         }
315
316         if (strcmp(av[0], "show") == 0) {
317                 hammer_off_t node_offset = (hammer_off_t)-1;
318
319                 hammer_parsedevs(blkdevs);
320                 if (ac > 1)
321                         sscanf(av[1], "%llx", &node_offset);
322                 hammer_cmd_show(node_offset, 0, NULL, NULL);
323                 exit(0);
324         }
325         if (strcmp(av[0], "blockmap") == 0) {
326                 hammer_parsedevs(blkdevs);
327                 hammer_cmd_blockmap();
328                 exit(0);
329         }
330         usage(1);
331         /* not reached */
332         return(0);
333 }
334
335 static
336 void
337 hammer_parsedevs(const char *blkdevs)
338 {
339         char *copy;
340         char *volname;
341
342         if (blkdevs == NULL) {
343                 errx(1, "A -f blkdev[:blkdev]* specification is required "
344                         "for this command");
345         }
346
347         copy = strdup(blkdevs);
348         while ((volname = copy) != NULL) {
349                 if ((copy = strchr(copy, ':')) != NULL)
350                         *copy++ = 0;
351                 setup_volume(-1, volname, 0, O_RDONLY);
352         }
353 }
354
355 static
356 void
357 sigalrm(int signo __unused)
358 {
359         /* do nothing (interrupts HAMMER ioctl) */
360 }
361
362 static
363 void
364 sigintr(int signo __unused)
365 {
366         if (RunningIoctl == 0)
367                 _exit(1);
368         DidInterrupt = 1;
369         /* do nothing (interrupts HAMMER ioctl) */
370 }
371
372 static
373 void
374 usage(int exit_code)
375 {
376         fprintf(stderr, 
377                 "hammer -h\n"
378                 "hammer [-2qrv] [-b bandwidth] [-c cyclefile] [-f blkdev[:blkdev]*]\n"
379                 "       [-i delay ] [-t seconds] command [argument ...]\n"
380                 "hammer synctid <filesystem> [quick]\n"
381                 "hammer -f blkdev[:blkdev]* blockmap\n"
382                 "hammer bstats [interval]\n"
383                 "hammer iostats [interval]\n"
384                 "hammer history[@offset[,len]] <file> ...\n"
385                 "hammer -f blkdev[:blkdev]* [-r] show [offset]\n"
386 #if 0
387                 "hammer -f blkdev[:blkdev]* blockmap\n"
388 #endif
389                 "hammer namekey1 <path>\n"
390                 "hammer namekey2 <path>\n"
391                 "hammer cleanup [<filesystem> ...]\n"
392                 "hammer snapshot [<filesystem>] <snapshot-dir>\n"
393                 "hammer prune <softlink-dir>\n"
394                 "hammer prune-everything <filesystem>\n"
395                 "hammer reblock[-btree/inodes/dirs/data] "
396                         "<filesystem> [fill_percentage]\n"
397                 "hammer pfs-status <dirpath> ...\n"
398                 "hammer pfs-master <dirpath> [options]\n"
399                 "hammer pfs-slave <dirpath> [options]\n"
400                 "hammer pfs-update <dirpath> [options]\n"
401                 "hammer pfs-upgrade <dirpath>\n"
402                 "hammer pfs-downgrade <dirpath>\n"
403                 "hammer pfs-destroy <dirpath>\n"
404                 "hammer mirror-read <filesystem> [begin-tid]\n"
405                 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
406                 "hammer mirror-write <filesystem>\n"
407                 "hammer mirror-dump\n"
408                 "hammer mirror-copy [[user@]host:]<filesystem>"
409                                   " [[user@]host:]<filesystem>\n"
410                 "hammer mirror-stream [[user@]host:]<filesystem>"
411                                     " [[user@]host:]<filesystem>\n"
412                 "hammer version <filesystem>\n"
413                 "hammer version-upgrade <filesystem> version# [force]\n"
414         );
415         exit(exit_code);
416 }
417