HAMMER 27/many: Major surgery - change allocation model
[dragonfly.git] / sbin / hammer / hammer.c
blobc3dc2ecb939b931725f632336dccbd52ccfac0fa
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.8 2008/02/08 08:30:56 dillon Exp $
37 #include "hammer.h"
39 static void hammer_parsetime(u_int64_t *tidp, const char *timestr);
40 static void hammer_parsedevs(const char *blkdevs);
41 static void usage(int exit_code);
43 int RecurseOpt;
44 int VerboseOpt;
46 int
47 main(int ac, char **av)
49 u_int64_t tid;
50 int ch;
51 u_int32_t status;
52 char *blkdevs = NULL;
54 while ((ch = getopt(ac, av, "hf:rv")) != -1) {
55 switch(ch) {
56 case 'h':
57 usage(0);
58 /* not reached */
59 case 'r':
60 RecurseOpt = 1;
61 break;
62 case 'f':
63 blkdevs = optarg;
64 break;
65 case 'v':
66 ++VerboseOpt;
67 break;
68 default:
69 usage(1);
70 /* not reached */
73 ac -= optind;
74 av += optind;
75 if (ac < 1) {
76 usage(1);
77 /* not reached */
80 if (strcmp(av[0], "now") == 0) {
81 hammer_parsetime(&tid, "0s");
82 printf("0x%08x\n", (int)(tid / 1000000000LL));
83 exit(0);
85 if (strcmp(av[0], "stamp") == 0) {
86 if (av[1] == NULL)
87 usage(1);
88 hammer_parsetime(&tid, av[1]);
89 printf("0x%08x\n", (int)(tid / 1000000000LL));
90 exit(0);
92 if (strcmp(av[0], "namekey") == 0) {
93 int64_t key;
95 if (av[1] == NULL)
96 usage(1);
97 key = (int64_t)(crc32(av[1], strlen(av[1])) & 0x7FFFFFFF) << 32;
98 if (key == 0)
99 key |= 0x100000000LL;
100 printf("0x%016llx\n", key);
101 exit(0);
103 if (strcmp(av[0], "namekey32") == 0) {
104 int32_t key;
106 if (av[1] == NULL)
107 usage(1);
108 key = crc32(av[1], strlen(av[1])) & 0x7FFFFFFF;
109 if (key == 0)
110 ++key;
111 printf("0x%08x\n", key);
112 exit(0);
114 if (strcmp(av[0], "prune") == 0) {
115 hammer_cmd_prune(av + 1, ac - 1);
116 exit(0);
119 if (strncmp(av[0], "history", 7) == 0) {
120 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
121 exit(0);
124 uuid_name_lookup(&Hammer_FSType, "DragonFly HAMMER", &status);
125 if (status != uuid_s_ok) {
126 errx(1, "uuids file does not have the DragonFly "
127 "HAMMER filesystem type");
130 if (strcmp(av[0], "show") == 0) {
131 hammer_off_t node_offset = (hammer_off_t)-1;
133 hammer_parsedevs(blkdevs);
134 if (ac > 1)
135 sscanf(av[1], "%llx", &node_offset);
136 hammer_cmd_show(node_offset, 0, NULL, NULL);
137 exit(0);
139 usage(1);
140 /* not reached */
141 return(0);
145 * Parse a timestamp for the mount point
147 * yyyymmddhhmmss
148 * -N[s/h/d/m/y]
150 static
151 void
152 hammer_parsetime(u_int64_t *tidp, const char *timestr)
154 struct tm tm;
155 time_t t;
156 int32_t n;
157 char c;
158 double seconds = 0;
160 t = time(NULL);
162 if (*timestr == 0)
163 usage(1);
165 if (isalpha(timestr[strlen(timestr)-1])) {
166 if (sscanf(timestr, "%d%c", &n, &c) != 2)
167 usage(1);
168 switch(c) {
169 case 'Y':
170 n *= 365;
171 goto days;
172 case 'M':
173 n *= 30;
174 /* fall through */
175 case 'D':
176 days:
177 n *= 24;
178 /* fall through */
179 case 'h':
180 n *= 60;
181 /* fall through */
182 case 'm':
183 n *= 60;
184 /* fall through */
185 case 's':
186 t -= n;
187 break;
188 default:
189 usage(1);
191 } else {
192 localtime_r(&t, &tm);
193 seconds = (double)tm.tm_sec;
194 tm.tm_year += 1900;
195 tm.tm_mon += 1;
196 n = sscanf(timestr, "%4d%2d%2d:%2d%2d%lf",
197 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
198 &tm.tm_hour, &tm.tm_min, &seconds);
199 tm.tm_mon -= 1;
200 tm.tm_year -= 1900;
201 /* if [:hhmmss] is omitted, assume :000000.0 */
202 if (n < 4)
203 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
204 else
205 tm.tm_sec = (int)seconds;
206 t = mktime(&tm);
208 *tidp = (u_int64_t)t * 1000000000 +
209 (seconds - (int)seconds) * 1000000000;
212 static
213 void
214 hammer_parsedevs(const char *blkdevs)
216 char *copy;
217 char *volname;
219 if (blkdevs == NULL) {
220 errx(1, "A -f blkdevs specification is required "
221 "for this command");
224 copy = strdup(blkdevs);
225 while ((volname = copy) != NULL) {
226 if ((copy = strchr(copy, ':')) != NULL)
227 *copy++ = 0;
228 setup_volume(-1, volname, 0, O_RDONLY);
232 static
233 void
234 usage(int exit_code)
236 fprintf(stderr,
237 "hammer -h\n"
238 "hammer now\n"
239 "hammer stamp <time>\n"
240 "hammer prune <filesystem> [using <configfile>]\n"
241 "hammer prune <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>\n"
242 "hammer history[@offset[,len]] <file-1>...<file-N>\n"
243 "hammer -f blkdevs [-r] show [vol_no[:clu_no]]\n"
245 fprintf(stderr, "time: +n[s/m/h/D/M/Y]\n"
246 "time: yyyymmdd[:hhmmss]\n"
247 "modulo_time: n{s,m,h,d,M,y}\n");
248 exit(exit_code);