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