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