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