53c1a5636cff1f35f73df9b3d219083f6bf8cb95
[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.7 2008/02/04 08:34:22 dillon Exp $
35  */
36
37 #include "hammer.h"
38
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);
42
43 int RecurseOpt;
44 int VerboseOpt;
45
46 int
47 main(int ac, char **av)
48 {
49         u_int64_t tid;
50         int ch;
51         int status;
52         char *blkdevs = NULL;
53
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 */
71                 }
72         }
73         ac -= optind;
74         av += optind;
75         if (ac < 1) {
76                 usage(1);
77                 /* not reached */
78         }
79
80         if (strcmp(av[0], "now") == 0) {
81                 hammer_parsetime(&tid, "0s");
82                 printf("0x%08x\n", (int)(tid / 1000000000LL));
83                 exit(0);
84         }
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);
91         }
92         if (strcmp(av[0], "namekey") == 0) {
93                 int64_t key;
94
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);
102         }
103         if (strcmp(av[0], "namekey32") == 0) {
104                 int32_t key;
105
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);
113         }
114         if (strcmp(av[0], "prune") == 0) {
115                 hammer_cmd_prune(av + 1, ac - 1);
116                 exit(0);
117         }
118
119         if (strncmp(av[0], "history", 7) == 0) {
120                 hammer_cmd_history(av[0] + 7, av + 1, ac - 1);
121                 exit(0);
122         }
123
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");
128         }
129
130         init_alist_templates();
131         if (strcmp(av[0], "show") == 0) {
132                 int32_t vol_no = -1;
133                 int32_t clu_no = -1;
134
135                 hammer_parsedevs(blkdevs);
136                 if (ac > 1)
137                         sscanf(av[1], "%d:%d", &vol_no, &clu_no);
138                 hammer_cmd_show(vol_no, clu_no, 0);
139                 exit(0);
140         }
141         usage(1);
142         /* not reached */
143         return(0);
144 }
145
146 /*
147  * Parse a timestamp for the mount point
148  *
149  * yyyymmddhhmmss
150  * -N[s/h/d/m/y]
151  */
152 static
153 void
154 hammer_parsetime(u_int64_t *tidp, const char *timestr)
155 {
156         struct tm tm;
157         time_t t;
158         int32_t n;
159         char c;
160         double seconds = 0;
161
162         t = time(NULL);
163
164         if (*timestr == 0)
165                 usage(1);
166
167         if (isalpha(timestr[strlen(timestr)-1])) {
168                 if (sscanf(timestr, "%d%c", &n, &c) != 2)
169                         usage(1);
170                 switch(c) {
171                 case 'Y':
172                         n *= 365;
173                         goto days;
174                 case 'M':
175                         n *= 30;
176                         /* fall through */
177                 case 'D':
178                 days:
179                         n *= 24;
180                         /* fall through */
181                 case 'h':
182                         n *= 60;
183                         /* fall through */
184                 case 'm':
185                         n *= 60;
186                         /* fall through */
187                 case 's':
188                         t -= n;
189                         break;
190                 default:
191                         usage(1);
192                 }
193         } else {
194                 localtime_r(&t, &tm);
195                 seconds = (double)tm.tm_sec;
196                 tm.tm_year += 1900;
197                 tm.tm_mon += 1;
198                 n = sscanf(timestr, "%4d%2d%2d:%2d%2d%lf",
199                            &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
200                            &tm.tm_hour, &tm.tm_min, &seconds);
201                 tm.tm_mon -= 1;
202                 tm.tm_year -= 1900;
203                 /* if [:hhmmss] is omitted, assume :000000.0 */
204                 if (n < 4)
205                         tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
206                 else
207                         tm.tm_sec = (int)seconds;
208                 t = mktime(&tm);
209         }
210         *tidp = (u_int64_t)t * 1000000000 + 
211                 (seconds - (int)seconds) * 1000000000;
212 }
213
214 static
215 void
216 hammer_parsedevs(const char *blkdevs)
217 {
218         char *copy;
219         char *volname;
220
221         if (blkdevs == NULL) {
222                 errx(1, "A -f blkdevs specification is required "
223                         "for this command");
224         }
225
226         copy = strdup(blkdevs);
227         while ((volname = copy) != NULL) {
228                 if ((copy = strchr(copy, ':')) != NULL)
229                         *copy++ = 0;
230                 setup_volume(-1, volname, 0, O_RDONLY);
231         }
232 }
233
234 static
235 void
236 usage(int exit_code)
237 {
238         fprintf(stderr, 
239                 "hammer -h\n"
240                 "hammer now\n"
241                 "hammer stamp <time>\n"
242                 "hammer prune <filesystem> [using <configfile>]\n"
243                 "hammer prune <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>\n"
244                 "hammer history[@offset[,len]] <file-1>...<file-N>\n"
245                 "hammer -f blkdevs [-r] show [vol_no[:clu_no]]\n"
246         );
247         fprintf(stderr, "time: +n[s/m/h/D/M/Y]\n"
248                         "time: yyyymmdd[:hhmmss]\n"
249                         "modulo_time: n{s,m,h,d,M,y}\n");
250         exit(exit_code);
251 }
252