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