HAMMER Utilities: Add the 'hammer softprune' command.
[dragonfly.git] / sbin / hammer / cmd_prune.c
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:
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/Attic/cmd_prune.c,v 1.12 2008/05/31 18:45:04 dillon Exp $
35  */
36
37 #include "hammer.h"
38
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,
54                         hammer_tid_t tid);
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);
58
59 /*
60  * prune <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
61  * prune <filesystem> [using <filename>]
62  */
63 void
64 hammer_cmd_prune(char **av, int ac)
65 {
66         struct hammer_ioc_prune prune;
67         const char *filesystem;
68         int fd;
69         hammer_tid_t now_tid = (hammer_tid_t)time(NULL) * 1000000000LL;
70
71         bzero(&prune, sizeof(prune));
72         prune.elms = malloc(HAMMER_MAX_PRUNE_ELMS * sizeof(*prune.elms));
73         prune.nelms = 0;
74         prune.beg_localization = HAMMER_MIN_LOCALIZATION;
75         prune.beg_obj_id = HAMMER_MIN_OBJID;
76         prune.end_localization = HAMMER_MAX_LOCALIZATION;
77         prune.end_obj_id = HAMMER_MAX_OBJID;
78         hammer_get_cycle(&prune.end_obj_id, &prune.end_localization);
79
80         prune.stat_oldest_tid = HAMMER_MAX_TID;
81
82         if (ac == 0)
83                 prune_usage(1);
84         filesystem = av[0];
85         if (ac == 1) {
86                 hammer_prune_load_file(now_tid, &prune, filesystem, 
87                                       "/etc/hammer.conf");
88         } else if (strcmp(av[1], "using") == 0) {
89                 if (ac == 2)
90                         prune_usage(1);
91                 hammer_prune_load_file(now_tid, &prune, filesystem, av[2]);
92         } else if (strcmp(av[1], "everything") == 0) {
93                 prune.head.flags |= HAMMER_IOC_PRUNE_ALL;
94                 if (ac > 2)
95                         prune_usage(1);
96         } else {
97                 if (hammer_prune_parse_line(now_tid, &prune, filesystem,
98                                             av, ac) < 0) {
99                         prune_usage(1);
100                 }
101         }
102         fd = open(filesystem, O_RDONLY);
103         if (fd < 0)
104                 err(1, "Unable to open %s", filesystem);
105         if (ioctl(fd, HAMMERIOC_PRUNE, &prune) < 0) {
106                 printf("Prune %s failed: %s\n",
107                        filesystem, strerror(errno));
108         } else if (prune.head.flags & HAMMER_IOC_HEAD_INTR) {
109                 printf("Prune %s interrupted by timer at %016llx %04x\n",
110                        filesystem,
111                        prune.cur_obj_id, prune.cur_localization);
112                 if (CyclePath) {
113                         hammer_set_cycle(prune.cur_obj_id,
114                                          prune.cur_localization);
115                 }
116
117         } else {
118                 if (CyclePath)
119                         hammer_reset_cycle();
120                 printf("Prune %s succeeded\n", filesystem);
121         }
122         close(fd);
123         if (LinkPath)
124                 hammer_prune_create_links(filesystem, &prune);
125         printf("Pruned %lld/%lld records (%lld directory entries) "
126                "and %lld bytes\n",
127                 prune.stat_rawrecords,
128                 prune.stat_scanrecords,
129                 prune.stat_dirrecords,
130                 prune.stat_bytes
131         );
132 }
133
134 static void
135 hammer_prune_load_file(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
136                        const char *filesystem, const char *filename)
137 {
138         char buf[256];
139         FILE *fp;
140         char *av[16];
141         int ac;
142         int lineno;
143
144         if ((fp = fopen(filename, "r")) == NULL)
145                 err(1, "Unable to read %s", filename);
146         lineno = 0;
147         while (fgets(buf, sizeof(buf), fp) != NULL) {
148                 ++lineno;
149                 if (strncmp(buf, "prune", 5) != 0)
150                         continue;
151                 ac = 0;
152                 av[ac] = strtok(buf, " \t\r\n");
153                 while (av[ac] != NULL) {
154                         ++ac;
155                         if (ac == 16) {
156                                 fclose(fp);
157                                 errx(1, "Malformed prune directive in %s "
158                                      "line %d\n", filename, lineno);
159                         }
160                         av[ac] = strtok(NULL, " \t\r\n");
161                 }
162                 if (ac == 0)
163                         continue;
164                 if (strcmp(av[0], "prune") != 0)
165                         continue;
166                 printf("prune %s\n", av[2]);
167                 if (hammer_prune_parse_line(now_tid, prune, filesystem,
168                                             av + 1, ac - 1) < 0) {
169                         errx(1, "Malformed prune directive in %s line %d\n",
170                              filename, lineno);
171                 }
172         }
173         fclose(fp);
174 }
175
176 static __inline
177 const char *
178 plural(int notplural)
179 {
180         return(notplural ? "" : "s");
181 }
182
183 /*
184  * Parse the following parameters:
185  *
186  * <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
187  * <filesystem> from <modulo_time> everything
188  */
189 static int
190 hammer_prune_parse_line(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
191                         const char *filesystem, char **av, int ac)
192 {
193         int r;
194
195         switch(ac) {
196         case 4:
197                 r = hammer_prune_parse_line_short(now_tid, prune,
198                                                   filesystem, av);
199                 break;
200         case 7:
201                 r = hammer_prune_parse_line_long(now_tid, prune,
202                                                  filesystem, av);
203                 break;
204         default:
205                 r = -1;
206                 break;
207         }
208         return(r);
209 }
210
211 static int
212 hammer_prune_parse_line_short(hammer_tid_t now_tid,
213                              struct hammer_ioc_prune *prune,
214                              const char *filesystem, char **av)
215 {
216         struct hammer_ioc_prune_elm *elm;
217         u_int64_t from_time;
218         char *from_stamp_str;
219
220         if (strcmp(av[0], filesystem) != 0)
221                 return(0);
222         if (strcmp(av[1], "from") != 0)
223                 return(-1);
224         if (strcmp(av[3], "everything") != 0)
225                 return(-1);
226         if (parse_modulo_time(av[2], &from_time) < 0)
227                 return(-1);
228         if (from_time == 0) {
229                 fprintf(stderr, "Bad from or to time specification.\n");
230                 return(-1);
231         }
232         if (prune->nelms == HAMMER_MAX_PRUNE_ELMS) {
233                 fprintf(stderr, "Too many prune specifications in file! "
234                         "Max is %d\n", HAMMER_MAX_PRUNE_ELMS);
235                 return(-1);
236         }
237
238         /*
239          * Example:  from 1y everything
240          */
241         elm = &prune->elms[prune->nelms++];
242         elm->beg_tid = 1;
243         elm->end_tid = now_tid - now_tid % from_time;
244         if (now_tid - elm->end_tid < from_time)
245                 elm->end_tid -= from_time;
246         assert(elm->beg_tid < elm->end_tid);
247         elm->mod_tid = elm->end_tid - elm->beg_tid;
248
249         /*
250          * Convert back to local time for pretty printing
251          */
252         from_stamp_str = tid_to_stamp_str(elm->end_tid);
253         printf("Prune everything older then %s", from_stamp_str);
254         free(from_stamp_str);
255         return(0);
256 }
257
258 static int
259 hammer_prune_parse_line_long(hammer_tid_t now_tid,
260                              struct hammer_ioc_prune *prune,
261                              const char *filesystem, char **av)
262 {
263         struct hammer_ioc_prune_elm *elm;
264         u_int64_t from_time;
265         u_int64_t to_time;
266         u_int64_t every_time;
267         char *from_stamp_str;
268         char *to_stamp_str;
269
270         if (strcmp(av[0], filesystem) != 0)
271                 return(0);
272         if (strcmp(av[1], "from") != 0)
273                 return(-1);
274         if (strcmp(av[3], "to") != 0)
275                 return(-1);
276         if (strcmp(av[5], "every") != 0)
277                 return(-1);
278         if (parse_modulo_time(av[2], &from_time) < 0)
279                 return(-1);
280         if (parse_modulo_time(av[4], &to_time) < 0)
281                 return(-1);
282         if (parse_modulo_time(av[6], &every_time) < 0)
283                 return(-1);
284         if (from_time > to_time)
285                 return(-1);
286         if (from_time == 0 || to_time == 0) {
287                 fprintf(stderr, "Bad from or to time specification.\n");
288                 return(-1);
289         }
290         if (to_time % from_time != 0) {
291                 fprintf(stderr, "Bad TO time specification.\n"
292                         "It must be an integral multiple of FROM time\n");
293                 return(-1);
294         }
295         if (every_time == 0 ||
296             from_time % every_time != 0 ||
297             to_time % every_time != 0) {
298                 fprintf(stderr, "Bad 'every <modulo_time>' specification.\n"
299                         "It must be an integral subdivision of FROM and TO\n");
300                 return(-1);
301         }
302         if (prune->nelms == HAMMER_MAX_PRUNE_ELMS) {
303                 fprintf(stderr, "Too many prune specifications in file! "
304                         "Max is %d\n", HAMMER_MAX_PRUNE_ELMS);
305                 return(-1);
306         }
307
308         /*
309          * Example:  from 1m to 60m every 5m
310          */
311         elm = &prune->elms[prune->nelms++];
312         elm->beg_tid = now_tid - now_tid % to_time;
313         if (now_tid - elm->beg_tid < to_time)
314                 elm->beg_tid -= to_time;
315
316         elm->end_tid = now_tid - now_tid % from_time;
317         if (now_tid - elm->end_tid < from_time)
318                 elm->end_tid -= from_time;
319
320         elm->mod_tid = every_time;
321         assert(elm->beg_tid < elm->end_tid);
322
323         /*
324          * Convert back to local time for pretty printing
325          */
326         from_stamp_str = tid_to_stamp_str(elm->beg_tid);
327         to_stamp_str = tid_to_stamp_str(elm->end_tid);
328         printf("Prune %s to %s every ", from_stamp_str, to_stamp_str);
329
330         every_time /= 1000000000;
331         if (every_time < 60)
332                 printf("%lld second%s\n", every_time, plural(every_time == 1));
333         every_time /= 60;
334         if (every_time && every_time < 60)
335                 printf("%lld minute%s\n", every_time, plural(every_time == 1));
336         every_time /= 60;
337         if (every_time && every_time < 24)
338                 printf("%lld hour%s\n", every_time, plural(every_time == 1));
339         every_time /= 24;
340         if (every_time)
341                 printf("%lld day%s\n", every_time, plural(every_time == 1));
342
343         free(from_stamp_str);
344         free(to_stamp_str);
345         return(0);
346 }
347
348 /*
349  * Create softlinks in the form $linkpath/snap_ddmmmyyyy[_hhmmss]
350  */
351 static void
352 hammer_prune_create_links(const char *filesystem,
353                           struct hammer_ioc_prune *prune)
354 {
355         struct hammer_ioc_prune_elm *elm;
356         hammer_tid_t tid;
357         struct dirent *den;
358         char *path;
359         DIR *dir;
360
361         if ((dir = opendir(LinkPath)) == NULL) {
362                 fprintf(stderr, "Unable to access linkpath %s\n", LinkPath);
363                 return;
364         }
365         while ((den = readdir(dir)) != NULL) {
366                 if (strncmp(den->d_name, "snap-", 5) == 0) {
367                         asprintf(&path, "%s/%s", LinkPath, den->d_name);
368                         remove(path);
369                         free(path);
370                 }
371         }
372         closedir(dir);
373
374         for (elm = &prune->elms[0]; elm < &prune->elms[prune->nelms]; ++elm) {
375                 for (tid = elm->beg_tid;
376                      tid < elm->end_tid;
377                      tid += elm->mod_tid) {
378                         if (tid < prune->stat_oldest_tid)
379                                 continue;
380                         hammer_prune_make_softlink(filesystem, tid);
381                 }
382         }
383 }
384
385 static void
386 hammer_prune_make_softlink(const char *filesystem, hammer_tid_t tid)
387 {
388         struct tm *tp;
389         char *path;
390         char *target;
391         char buf[64];
392         time_t t;
393
394         t = (time_t)(tid / 1000000000);
395         tp = localtime(&t);
396
397         /*
398          * Construct the contents of the softlink.
399          */
400         asprintf(&target, "%s/@@0x%016llx", filesystem, tid);
401
402         /*
403          * Construct the name of the snap-shot softlink
404          */
405         if (tid % (1000000000ULL * 60 * 60 * 24) == 0) {
406                 strftime(buf, sizeof(buf), "snap-%d%b%Y", tp);
407         } else if (tid % (1000000000ULL * 60 * 60) == 0) {
408                 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H%M", tp);
409         } else if (tid % (1000000000ULL * 60) == 0) {
410                 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H%M", tp);
411         } else {
412                 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H%M%S", tp);
413         }
414
415         asprintf(&path, "%s/%s", LinkPath, buf);
416         symlink(target, path);
417         free(path);
418         free(target);
419 }
420
421 static
422 int
423 parse_modulo_time(const char *str, u_int64_t *delta)
424 {
425         char *term;
426
427         *delta = strtoull(str, &term, 10);
428
429         switch(*term) {
430         case 'y':
431                 *delta *= 12;
432                 /* fall through */
433         case 'M':
434                 *delta *= 30;
435                 /* fall through */
436         case 'd':
437                 *delta *= 24;
438                 /* fall through */
439         case 'h':
440                 *delta *= 60;
441                 /* fall through */
442         case 'm':
443                 *delta *= 60;
444                 /* fall through */
445         case 's':
446                 break;
447         default:
448                 return(-1);
449         }
450         *delta *= 1000000000LL; /* TID's are in nanoseconds */
451         return(0);
452 }
453
454 static char *
455 tid_to_stamp_str(hammer_tid_t tid)
456 {
457         struct tm *tp;
458         char *buf = malloc(256);
459         time_t t;
460
461         t = (time_t)(tid / 1000000000);
462         tp = localtime(&t);
463         strftime(buf, 256, "%e-%b-%Y %H:%M:%S %Z", tp);
464         return(buf);
465 }
466
467 static void
468 prune_usage(int code)
469 {
470         fprintf(stderr, "Bad prune directive, specify one of:\n"
471                         "prune filesystem [using filename]\n"
472                         "prune filesystem from <modulo_time> to <modulo_time> every <modulo_time>\n"
473                         "prune filesystem from <modulo_time> everything\n"
474                         "prune filesystem everything\n");
475         exit(code);
476 }