2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sbin/hammer/Attic/cmd_prune.c,v 1.9 2008/05/11 20:44:44 dillon Exp $
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 hammer_prune_parse_line_short(hammer_tid_t now_tid,
46 struct hammer_ioc_prune *prune,
47 const char *filesystem, char **av);
48 static int hammer_prune_parse_line_long(hammer_tid_t now_tid,
49 struct hammer_ioc_prune *prune,
50 const char *filesystem, char **av);
51 static void hammer_prune_create_links(const char *filesystem,
52 struct hammer_ioc_prune *prune);
53 static void hammer_prune_make_softlink(const char *filesystem,
55 static int parse_modulo_time(const char *str, u_int64_t *delta);
56 static char *tid_to_stamp_str(hammer_tid_t tid);
57 static void prune_usage(int code);
60 * prune <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
61 * prune <filesystem> [using <filename>]
64 hammer_cmd_prune(char **av, int ac)
66 struct hammer_ioc_prune prune;
67 const char *filesystem;
69 hammer_tid_t now_tid = (hammer_tid_t)time(NULL) * 1000000000LL;
71 bzero(&prune, sizeof(prune));
73 prune.beg_obj_id = HAMMER_MIN_OBJID;
74 prune.end_obj_id = hammer_get_cycle(HAMMER_MAX_OBJID);
76 prune.cur_obj_id = prune.end_obj_id; /* remove me */
77 prune.cur_key = HAMMER_MAX_KEY; /* remove me */
78 prune.stat_oldest_tid = HAMMER_MAX_TID;
84 hammer_prune_load_file(now_tid, &prune, filesystem,
86 } else if (strcmp(av[1], "using") == 0) {
89 hammer_prune_load_file(now_tid, &prune, filesystem, av[2]);
90 } else if (strcmp(av[1], "everything") == 0) {
91 prune.head.flags |= HAMMER_IOC_PRUNE_ALL;
95 if (hammer_prune_parse_line(now_tid, &prune, filesystem,
100 fd = open(filesystem, O_RDONLY);
102 err(1, "Unable to open %s", filesystem);
103 if (ioctl(fd, HAMMERIOC_PRUNE, &prune) < 0) {
104 printf("Prune %s failed: %s\n",
105 filesystem, strerror(errno));
106 } else if (prune.head.flags & HAMMER_IOC_HEAD_INTR) {
107 printf("Prune %s interrupted by timer at %016llx\n",
108 filesystem, prune.cur_obj_id);
110 hammer_set_cycle(prune.cur_obj_id);
114 hammer_reset_cycle();
115 printf("Prune %s succeeded\n", filesystem);
119 hammer_prune_create_links(filesystem, &prune);
120 printf("Pruned %lld/%lld records (%lld directory entries) "
122 prune.stat_rawrecords,
123 prune.stat_scanrecords,
124 prune.stat_dirrecords,
130 hammer_prune_load_file(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
131 const char *filesystem, const char *filename)
139 if ((fp = fopen(filename, "r")) == NULL)
140 err(1, "Unable to read %s", filename);
142 while (fgets(buf, sizeof(buf), fp) != NULL) {
144 if (strncmp(buf, "prune", 5) != 0)
147 av[ac] = strtok(buf, " \t\r\n");
148 while (av[ac] != NULL) {
152 errx(1, "Malformed prune directive in %s "
153 "line %d\n", filename, lineno);
155 av[ac] = strtok(NULL, " \t\r\n");
159 if (strcmp(av[0], "prune") != 0)
161 if (hammer_prune_parse_line(now_tid, prune, filesystem,
162 av + 1, ac - 1) < 0) {
163 errx(1, "Malformed prune directive in %s line %d\n",
172 plural(int notplural)
174 return(notplural ? "" : "s");
178 * Parse the following parameters:
180 * <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
181 * <filesystem> from <modulo_time> everything
184 hammer_prune_parse_line(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
185 const char *filesystem, char **av, int ac)
191 r = hammer_prune_parse_line_short(now_tid, prune,
195 r = hammer_prune_parse_line_long(now_tid, prune,
206 hammer_prune_parse_line_short(hammer_tid_t now_tid,
207 struct hammer_ioc_prune *prune,
208 const char *filesystem, char **av)
210 struct hammer_ioc_prune_elm *elm;
212 char *from_stamp_str;
214 if (strcmp(av[0], filesystem) != 0)
216 if (strcmp(av[1], "from") != 0)
218 if (strcmp(av[3], "everything") != 0)
220 if (parse_modulo_time(av[2], &from_time) < 0)
222 if (from_time == 0) {
223 fprintf(stderr, "Bad from or to time specification.\n");
226 if (prune->nelms == HAMMER_MAX_PRUNE_ELMS) {
227 fprintf(stderr, "Too many prune specifications in file! "
228 "Max is %d\n", HAMMER_MAX_PRUNE_ELMS);
233 * Example: from 1y everything
235 elm = &prune->elms[prune->nelms++];
237 elm->end_tid = now_tid - now_tid % from_time;
238 if (now_tid - elm->end_tid < from_time)
239 elm->end_tid -= from_time;
240 assert(elm->beg_tid < elm->end_tid);
241 elm->mod_tid = elm->end_tid - elm->beg_tid;
244 * Convert back to local time for pretty printing
246 from_stamp_str = tid_to_stamp_str(elm->end_tid);
247 printf("Prune everything older then %s", from_stamp_str);
248 free(from_stamp_str);
253 hammer_prune_parse_line_long(hammer_tid_t now_tid,
254 struct hammer_ioc_prune *prune,
255 const char *filesystem, char **av)
257 struct hammer_ioc_prune_elm *elm;
260 u_int64_t every_time;
261 char *from_stamp_str;
264 if (strcmp(av[0], filesystem) != 0)
266 if (strcmp(av[1], "from") != 0)
268 if (strcmp(av[3], "to") != 0)
270 if (strcmp(av[5], "every") != 0)
272 if (parse_modulo_time(av[2], &from_time) < 0)
274 if (parse_modulo_time(av[4], &to_time) < 0)
276 if (parse_modulo_time(av[6], &every_time) < 0)
278 if (from_time > to_time)
280 if (from_time == 0 || to_time == 0) {
281 fprintf(stderr, "Bad from or to time specification.\n");
284 if (to_time % from_time != 0) {
285 fprintf(stderr, "Bad TO time specification.\n"
286 "It must be an integral multiple of FROM time\n");
289 if (every_time == 0 ||
290 from_time % every_time != 0 ||
291 to_time % every_time != 0) {
292 fprintf(stderr, "Bad 'every <modulo_time>' specification.\n"
293 "It must be an integral subdivision of FROM and TO\n");
296 if (prune->nelms == HAMMER_MAX_PRUNE_ELMS) {
297 fprintf(stderr, "Too many prune specifications in file! "
298 "Max is %d\n", HAMMER_MAX_PRUNE_ELMS);
303 * Example: from 1m to 60m every 5m
305 elm = &prune->elms[prune->nelms++];
306 elm->beg_tid = now_tid - now_tid % to_time;
307 if (now_tid - elm->beg_tid < to_time)
308 elm->beg_tid -= to_time;
310 elm->end_tid = now_tid - now_tid % from_time;
311 if (now_tid - elm->end_tid < from_time)
312 elm->end_tid -= from_time;
314 elm->mod_tid = every_time;
315 assert(elm->beg_tid < elm->end_tid);
318 * Convert back to local time for pretty printing
320 from_stamp_str = tid_to_stamp_str(elm->beg_tid);
321 to_stamp_str = tid_to_stamp_str(elm->end_tid);
322 printf("Prune %s to %s every ", from_stamp_str, to_stamp_str);
324 every_time /= 1000000000;
326 printf("%lld second%s\n", every_time, plural(every_time == 1));
328 if (every_time && every_time < 60)
329 printf("%lld minute%s\n", every_time, plural(every_time == 1));
331 if (every_time && every_time < 24)
332 printf("%lld hour%s\n", every_time, plural(every_time == 1));
335 printf("%lld day%s\n", every_time, plural(every_time == 1));
337 free(from_stamp_str);
343 * Create softlinks in the form $linkpath/snap_ddmmmyyyy[_hhmmss]
346 hammer_prune_create_links(const char *filesystem,
347 struct hammer_ioc_prune *prune)
349 struct hammer_ioc_prune_elm *elm;
355 if ((dir = opendir(LinkPath)) == NULL) {
356 fprintf(stderr, "Unable to access linkpath %s\n", LinkPath);
359 while ((den = readdir(dir)) != NULL) {
360 if (strncmp(den->d_name, "snap-", 5) == 0) {
361 asprintf(&path, "%s/%s", LinkPath, den->d_name);
368 for (elm = &prune->elms[0]; elm < &prune->elms[prune->nelms]; ++elm) {
369 for (tid = elm->beg_tid;
371 tid += elm->mod_tid) {
372 if (tid < prune->stat_oldest_tid)
374 hammer_prune_make_softlink(filesystem, tid);
380 hammer_prune_make_softlink(const char *filesystem, hammer_tid_t tid)
388 t = (time_t)(tid / 1000000000);
392 * Construct the contents of the softlink.
394 asprintf(&target, "%s/@@0x%016llx", filesystem, tid);
397 * Construct the name of the snap-shot softlink
399 if (tid % (1000000000ULL * 60 * 60 * 24) == 0) {
400 strftime(buf, sizeof(buf), "snap-%d%b%Y", tp);
401 } else if (tid % (1000000000ULL * 60 * 60) == 0) {
402 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H%M", tp);
403 } else if (tid % (1000000000ULL * 60) == 0) {
404 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H%M", tp);
406 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H%M%S", tp);
409 asprintf(&path, "%s/%s", LinkPath, buf);
410 symlink(target, path);
417 parse_modulo_time(const char *str, u_int64_t *delta)
421 *delta = strtoull(str, &term, 10);
444 *delta *= 1000000000LL; /* TID's are in nanoseconds */
449 tid_to_stamp_str(hammer_tid_t tid)
452 char *buf = malloc(256);
455 t = (time_t)(tid / 1000000000);
457 strftime(buf, 256, "%e-%b-%Y %H:%M:%S %Z", tp);
462 prune_usage(int code)
464 fprintf(stderr, "Bad prune directive, specify one of:\n"
465 "prune filesystem [using filename]\n"
466 "prune filesystem from <modulo_time> to <modulo_time> every <modulo_time>\n"
467 "prune filesystem from <modulo_time> everything\n"
468 "prune filesystem everything\n");