HAMMER Utilities: Add an 'everything' directive to the prune command. This
[dragonfly.git] / sbin / hammer / cmd_prune.c
blob1c4d7d57ed5b24adbd06296daec28d1c02f49fc9
1 /*
2 * Copyright (c) 2008 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/Attic/cmd_prune.c,v 1.3 2008/02/06 09:00:28 dillon Exp $
37 #include "hammer.h"
39 static void hammer_prune_load_file(hammer_tid_t now_tid,
40 struct hammer_ioc_prune *prune,
41 const char *filesystem, const char *filename);
42 static int hammer_prune_parse_line(hammer_tid_t now_tid,
43 struct hammer_ioc_prune *prune,
44 const char *filesystem, char **av, int ac);
45 static int parse_modulo_time(const char *str, u_int64_t *delta);
46 static char *tid_to_stamp_str(hammer_tid_t tid);
47 static void prune_usage(int code);
50 * prune <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
51 * prune <filesystem> [using <filename>]
53 void
54 hammer_cmd_prune(char **av, int ac)
56 struct hammer_ioc_prune prune;
57 const char *filesystem;
58 int fd;
59 hammer_tid_t now_tid = (hammer_tid_t)time(NULL) * 1000000000LL;
61 bzero(&prune, sizeof(prune));
62 prune.nelms = 0;
63 prune.beg_obj_id = HAMMER_MIN_OBJID;
64 prune.end_obj_id = HAMMER_MAX_OBJID;
65 prune.cur_obj_id = prune.end_obj_id; /* reverse scan */
66 prune.cur_key = HAMMER_MAX_KEY;
68 if (ac == 0)
69 prune_usage(1);
70 filesystem = av[0];
71 if (ac == 1) {
72 hammer_prune_load_file(now_tid, &prune, filesystem,
73 "/etc/hammer.conf");
74 } else if (strcmp(av[1], "using") == 0) {
75 if (ac == 2)
76 prune_usage(1);
77 hammer_prune_load_file(now_tid, &prune, filesystem, av[2]);
78 } else if (strcmp(av[1], "everything") == 0) {
79 prune.flags |= HAMMER_IOC_PRUNE_ALL;
80 if (ac > 2)
81 prune_usage(1);
82 } else {
83 if (hammer_prune_parse_line(now_tid, &prune, filesystem,
84 av, ac) < 0) {
85 prune_usage(1);
88 fd = open(filesystem, O_RDONLY);
89 if (fd < 0)
90 err(1, "Unable to open %s", filesystem);
91 if (ioctl(fd, HAMMERIOC_PRUNE, &prune) < 0)
92 printf("Prune %s failed: %s\n", filesystem, strerror(errno));
93 else
94 printf("Prune %s succeeded\n", filesystem);
95 close(fd);
96 printf("Pruned %lld records (%lld directory entries) and %lld bytes\n",
97 prune.stat_rawrecords,
98 prune.stat_dirrecords,
99 prune.stat_bytes
103 static void
104 hammer_prune_load_file(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
105 const char *filesystem, const char *filename)
107 char buf[256];
108 FILE *fp;
109 char *av[16];
110 int ac;
111 int lineno;
113 if ((fp = fopen(filename, "r")) == NULL)
114 err(1, "Unable to read %s", filename);
115 lineno = 0;
116 while (fgets(buf, sizeof(buf), fp) != NULL) {
117 ++lineno;
118 if (strncmp(buf, "prune", 5) != 0)
119 continue;
120 ac = 0;
121 av[ac] = strtok(buf, " \t\r\n");
122 while (av[ac] != NULL) {
123 ++ac;
124 if (ac == 16) {
125 fclose(fp);
126 errx(1, "Malformed prune directive in %s "
127 "line %d\n", filename, lineno);
129 av[ac] = strtok(NULL, " \t\r\n");
131 if (ac == 0)
132 continue;
133 if (strcmp(av[0], "prune") != 0)
134 continue;
135 if (hammer_prune_parse_line(now_tid, prune, filesystem,
136 av + 1, ac - 1) < 0) {
137 errx(1, "Malformed prune directive in %s line %d\n",
138 filename, lineno);
141 fclose(fp);
144 static __inline
145 const char *
146 plural(int notplural)
148 return(notplural ? "" : "s");
152 * Parse the following parameters:
154 * <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
156 static int
157 hammer_prune_parse_line(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
158 const char *filesystem, char **av, int ac)
160 struct hammer_ioc_prune_elm *elm;
161 u_int64_t from_time;
162 u_int64_t to_time;
163 u_int64_t every_time;
164 char *from_stamp_str;
165 char *to_stamp_str;
167 if (ac != 7)
168 return(-1);
169 if (strcmp(av[0], filesystem) != 0)
170 return(0);
171 if (strcmp(av[1], "from") != 0)
172 return(-1);
173 if (strcmp(av[3], "to") != 0)
174 return(-1);
175 if (strcmp(av[5], "every") != 0)
176 return(-1);
177 if (parse_modulo_time(av[2], &from_time) < 0)
178 return(-1);
179 if (parse_modulo_time(av[4], &to_time) < 0)
180 return(-1);
181 if (parse_modulo_time(av[6], &every_time) < 0)
182 return(-1);
183 if (from_time > to_time)
184 return(-1);
185 if (from_time == 0 || to_time == 0) {
186 fprintf(stderr, "Bad from or to time specification.\n");
187 return(-1);
189 if (to_time % from_time != 0) {
190 fprintf(stderr, "Bad TO time specification.\n"
191 "It must be an integral multiple of FROM time\n");
192 return(-1);
194 if (every_time == 0 ||
195 from_time % every_time != 0 ||
196 to_time % every_time != 0) {
197 fprintf(stderr, "Bad 'every <modulo_time>' specification.\n"
198 "It must be an integral subdivision of FROM and TO\n");
199 return(-1);
201 if (prune->nelms == HAMMER_MAX_PRUNE_ELMS) {
202 fprintf(stderr, "Too many prune specifications in file! "
203 "Max is %d\n", HAMMER_MAX_PRUNE_ELMS);
204 return(-1);
208 * Example: from 1m to 60m every 5m
210 elm = &prune->elms[prune->nelms++];
211 elm->beg_tid = now_tid - now_tid % to_time;
212 if (now_tid - elm->beg_tid < to_time)
213 elm->beg_tid -= to_time;
215 elm->end_tid = now_tid - now_tid % from_time;
216 if (now_tid - elm->end_tid < from_time)
217 elm->end_tid -= from_time;
219 elm->mod_tid = every_time;
220 assert(elm->beg_tid < elm->end_tid);
223 * Convert back to local time for pretty printing
225 from_stamp_str = tid_to_stamp_str(elm->beg_tid);
226 to_stamp_str = tid_to_stamp_str(elm->end_tid);
227 printf("Prune %s to %s every ", from_stamp_str, to_stamp_str);
229 every_time /= 1000000000;
230 if (every_time < 60)
231 printf("%lld second%s\n", every_time, plural(every_time == 1));
232 every_time /= 60;
233 if (every_time && every_time < 60)
234 printf("%lld minute%s\n", every_time, plural(every_time == 1));
235 every_time /= 60;
236 if (every_time && every_time < 24)
237 printf("%lld hour%s\n", every_time, plural(every_time == 1));
238 every_time /= 24;
239 if (every_time)
240 printf("%lld day%s\n", every_time, plural(every_time == 1));
242 free(from_stamp_str);
243 free(to_stamp_str);
244 return(0);
247 static
249 parse_modulo_time(const char *str, u_int64_t *delta)
251 char *term;
253 *delta = strtoull(str, &term, 10);
255 switch(*term) {
256 case 'y':
257 *delta *= 12;
258 /* fall through */
259 case 'M':
260 *delta *= 30;
261 /* fall through */
262 case 'd':
263 *delta *= 24;
264 /* fall through */
265 case 'h':
266 *delta *= 60;
267 /* fall through */
268 case 'm':
269 *delta *= 60;
270 /* fall through */
271 case 's':
272 break;
273 default:
274 return(-1);
276 *delta *= 1000000000LL; /* TID's are in nanoseconds */
277 return(0);
280 static char *
281 tid_to_stamp_str(hammer_tid_t tid)
283 struct tm *tp;
284 char *buf = malloc(256);
285 time_t t;
287 t = (time_t)(tid / 1000000000);
288 tp = localtime(&t);
289 strftime(buf, 256, "%e-%b-%Y %H:%M:%S %Z", tp);
290 return(buf);
293 static void
294 prune_usage(int code)
296 fprintf(stderr, "Bad prune directive, specify one of:\n"
297 "prune filesystem [using filename]\n"
298 "prune filesystem from <modulo_time> to <modulo_time> every <modulo_time>\n");
299 exit(code);