HAMMER Utilities: Mirroring and pseudo-fs directives
[dragonfly.git] / sbin / hammer / hammer.c
blob76620103285cf7a7110afdf2ce28f5f1ba855be5
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:
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.
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.
34 * $DragonFly: src/sbin/hammer/hammer.c,v 1.29 2008/07/02 22:05:59 dillon Exp $
37 #include "hammer.h"
38 #include <signal.h>
39 #include <math.h>
41 static void hammer_parsedevs(const char *blkdevs);
42 static void sigalrm(int signo);
43 static void usage(int exit_code);
45 int RecurseOpt;
46 int VerboseOpt;
47 int NoSyncOpt;
48 const char *CyclePath;
49 const char *LinkPath;
51 int
52 main(int ac, char **av)
54 int ch;
55 int timeout = 0;
56 u_int32_t status;
57 char *blkdevs = NULL;
59 while ((ch = getopt(ac, av, "c:dhf:rs:t:v")) != -1) {
60 switch(ch) {
61 case 'c':
62 CyclePath = optarg;
63 break;
64 case 'd':
65 ++DebugOpt;
66 break;
67 case 'h':
68 usage(0);
69 /* not reached */
70 case 'r':
71 RecurseOpt = 1;
72 break;
73 case 'f':
74 blkdevs = optarg;
75 break;
76 case 's':
77 LinkPath = optarg;
78 break;
79 case 't':
80 timeout = strtol(optarg, NULL, 0);
81 break;
82 case 'v':
83 ++VerboseOpt;
84 break;
85 default:
86 usage(1);
87 /* not reached */
90 ac -= optind;
91 av += optind;
92 if (ac < 1) {
93 usage(1);
94 /* not reached */
97 if (timeout > 0) {
98 signal(SIGALRM, sigalrm);
99 alarm(timeout);
102 if (strcmp(av[0], "synctid") == 0) {
103 hammer_cmd_synctid(av + 1, ac - 1);
104 exit(0);
106 if (strcmp(av[0], "namekey") == 0) {
107 int64_t key;
109 if (av[1] == NULL)
110 usage(1);
111 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
112 if (key == 0)
113 key |= 0x100000000LL;
114 printf("0x%016llx\n", key);
115 exit(0);
117 if (strcmp(av[0], "namekey32") == 0) {
118 int32_t key;
120 if (av[1] == NULL)
121 usage(1);
122 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
123 if (key == 0)
124 ++key;
125 printf("0x%08x\n", key);
126 exit(0);
128 if (strcmp(av[0], "pfs-status") == 0) {
129 hammer_cmd_pseudofs_status(av + 1, ac - 1);
130 exit(0);
132 if (strcmp(av[0], "pfs-create") == 0) {
133 hammer_cmd_pseudofs_create(av + 1, ac - 1);
134 exit(0);
136 if (strcmp(av[0], "pfs-update") == 0) {
137 hammer_cmd_pseudofs_update(av + 1, ac - 1, 0);
138 exit(0);
140 if (strcmp(av[0], "status") == 0) {
141 hammer_cmd_status(av + 1, ac - 1);
142 exit(0);
144 if (strcmp(av[0], "prune") == 0) {
145 hammer_cmd_softprune(av + 1, ac - 1, 0);
146 exit(0);
148 if (strcmp(av[0], "prune-everything") == 0) {
149 hammer_cmd_softprune(av + 1, ac - 1, 1);
150 exit(0);
152 if (strcmp(av[0], "snapshot") == 0) {
153 hammer_cmd_snapshot(av + 1, ac - 1);
154 exit(0);
156 if (strcmp(av[0], "bstats") == 0) {
157 hammer_cmd_bstats(av + 1, ac - 1);
158 exit(0);
160 if (strcmp(av[0], "iostats") == 0) {
161 hammer_cmd_iostats(av + 1, ac - 1);
162 exit(0);
165 if (strncmp(av[0], "history", 7) == 0) {
166 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
167 exit(0);
169 if (strncmp(av[0], "reblock", 7) == 0) {
170 if (strcmp(av[0], "reblock") == 0)
171 hammer_cmd_reblock(av + 1, ac - 1, -1);
172 else if (strcmp(av[0], "reblock-btree") == 0)
173 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_BTREE);
174 else if (strcmp(av[0], "reblock-inodes") == 0)
175 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_INODES);
176 else if (strcmp(av[0], "reblock-dirs") == 0)
177 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DIRS);
178 else if (strcmp(av[0], "reblock-data") == 0)
179 hammer_cmd_reblock(av + 1, ac - 1, HAMMER_IOC_DO_DATA);
180 else
181 usage(1);
182 exit(0);
184 if (strncmp(av[0], "mirror", 6) == 0) {
185 if (strcmp(av[0], "mirror-read") == 0)
186 hammer_cmd_mirror_read(av + 1, ac - 1);
187 else if (strcmp(av[0], "mirror-write") == 0)
188 hammer_cmd_mirror_write(av + 1, ac - 1);
189 else if (strcmp(av[0], "mirror-copy") == 0)
190 hammer_cmd_mirror_copy(av + 1, ac - 1);
191 else
192 usage(1);
193 exit(0);
196 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
197 if (status != uuid_s_ok) {
198 errx(1, "uuids file does not have the DragonFly "
199 "HAMMER filesystem type");
202 if (strcmp(av[0], "show") == 0) {
203 hammer_off_t node_offset = (hammer_off_t)-1;
205 hammer_parsedevs(blkdevs);
206 if (ac > 1)
207 sscanf(av[1], "%llx", &node_offset);
208 hammer_cmd_show(node_offset, 0, NULL, NULL);
209 exit(0);
211 if (strcmp(av[0], "blockmap") == 0) {
212 hammer_parsedevs(blkdevs);
213 hammer_cmd_blockmap();
214 exit(0);
216 usage(1);
217 /* not reached */
218 return(0);
221 static
222 void
223 hammer_parsedevs(const char *blkdevs)
225 char *copy;
226 char *volname;
228 if (blkdevs == NULL) {
229 errx(1, "A -f blkdevs specification is required "
230 "for this command");
233 copy = strdup(blkdevs);
234 while ((volname = copy) != NULL) {
235 if ((copy = strchr(copy, ':')) != NULL)
236 *copy++ = 0;
237 setup_volume(-1, volname, 0, O_RDONLY);
241 static
242 void
243 sigalrm(int signo __unused)
245 /* do nothing (interrupts HAMMER ioctl) */
248 static
249 void
250 usage(int exit_code)
252 fprintf(stderr,
253 "hammer -h\n"
254 "hammer [-t timeout] [-c cyclefile] ....\n"
255 "hammer prune <dir-holding-softlinks>\n"
256 "hammer prune-everything <filesystem>\n"
257 "hammer snapshot <softlink-dir> [<filesystem>]\n"
258 "hammer bstats <interval>\n"
259 "hammer iostats <interval>\n"
260 "hammer mirror-read <filesystem>\n"
261 "hammer mirror-write <filesystem>\n"
262 "hammer mirror-copy [[user@]host:]<filesystem>"
263 " [[user@]host:]<filesystem>\n"
264 "hammer reblock[-btree/inodes/dirs/data] "
265 "<filesystem> [pack%%]\n"
266 "hammer pfs-status <dirpath>\n"
267 "hammer pfs-create <dirpath> [options]\n"
268 "hammer pfs-update <dirpath> [options]\n"
269 "hammer history[@offset[,len]] <file-1>...<file-N>\n"
270 "hammer -f blkdevs [-r] show\n"
271 "hammer -f blkdevs blockmap\n"
273 exit(exit_code);