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