HAMMER utilities: feature add.
[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.4 2008/03/20 04:03:03 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 void hammer_prune_create_links(const char *filesystem,
46                         struct hammer_ioc_prune *prune);
47 static void hammer_prune_make_softlink(const char *filesystem,
48                         hammer_tid_t tid);
49 static int parse_modulo_time(const char *str, u_int64_t *delta);
50 static char *tid_to_stamp_str(hammer_tid_t tid);
51 static void prune_usage(int code);
52
53 /*
54  * prune <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
55  * prune <filesystem> [using <filename>]
56  */
57 void
58 hammer_cmd_prune(char **av, int ac)
59 {
60         struct hammer_ioc_prune prune;
61         const char *filesystem;
62         int fd;
63         hammer_tid_t now_tid = (hammer_tid_t)time(NULL) * 1000000000LL;
64
65         bzero(&prune, sizeof(prune));
66         prune.nelms = 0;
67         prune.beg_obj_id = HAMMER_MIN_OBJID;
68         prune.end_obj_id = HAMMER_MAX_OBJID;
69         prune.cur_obj_id = prune.end_obj_id;    /* reverse scan */
70         prune.cur_key = HAMMER_MAX_KEY;
71         prune.stat_oldest_tid = HAMMER_MAX_TID;
72
73         if (ac == 0)
74                 prune_usage(1);
75         filesystem = av[0];
76         if (ac == 1) {
77                 hammer_prune_load_file(now_tid, &prune, filesystem, 
78                                       "/etc/hammer.conf");
79         } else if (strcmp(av[1], "using") == 0) {
80                 if (ac == 2)
81                         prune_usage(1);
82                 hammer_prune_load_file(now_tid, &prune, filesystem, av[2]);
83         } else if (strcmp(av[1], "everything") == 0) {
84                 prune.flags |= HAMMER_IOC_PRUNE_ALL;
85                 if (ac > 2)
86                         prune_usage(1);
87         } else {
88                 if (hammer_prune_parse_line(now_tid, &prune, filesystem,
89                                             av, ac) < 0) {
90                         prune_usage(1);
91                 }
92         }
93         fd = open(filesystem, O_RDONLY);
94         if (fd < 0)
95                 err(1, "Unable to open %s", filesystem);
96         if (ioctl(fd, HAMMERIOC_PRUNE, &prune) < 0)
97                 printf("Prune %s failed: %s\n", filesystem, strerror(errno));
98         else
99                 printf("Prune %s succeeded\n", filesystem);
100         close(fd);
101         if (LinkPath)
102                 hammer_prune_create_links(filesystem, &prune);
103         printf("Pruned %lld records (%lld directory entries) and %lld bytes\n",
104                 prune.stat_rawrecords,
105                 prune.stat_dirrecords,
106                 prune.stat_bytes
107         );
108 }
109
110 static void
111 hammer_prune_load_file(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
112                        const char *filesystem, const char *filename)
113 {
114         char buf[256];
115         FILE *fp;
116         char *av[16];
117         int ac;
118         int lineno;
119
120         if ((fp = fopen(filename, "r")) == NULL)
121                 err(1, "Unable to read %s", filename);
122         lineno = 0;
123         while (fgets(buf, sizeof(buf), fp) != NULL) {
124                 ++lineno;
125                 if (strncmp(buf, "prune", 5) != 0)
126                         continue;
127                 ac = 0;
128                 av[ac] = strtok(buf, " \t\r\n");
129                 while (av[ac] != NULL) {
130                         ++ac;
131                         if (ac == 16) {
132                                 fclose(fp);
133                                 errx(1, "Malformed prune directive in %s "
134                                      "line %d\n", filename, lineno);
135                         }
136                         av[ac] = strtok(NULL, " \t\r\n");
137                 }
138                 if (ac == 0)
139                         continue;
140                 if (strcmp(av[0], "prune") != 0)
141                         continue;
142                 if (hammer_prune_parse_line(now_tid, prune, filesystem,
143                                             av + 1, ac - 1) < 0) {
144                         errx(1, "Malformed prune directive in %s line %d\n",
145                              filename, lineno);
146                 }
147         }
148         fclose(fp);
149 }
150
151 static __inline
152 const char *
153 plural(int notplural)
154 {
155         return(notplural ? "" : "s");
156 }
157
158 /*
159  * Parse the following parameters:
160  *
161  * <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
162  */
163 static int
164 hammer_prune_parse_line(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
165                         const char *filesystem, char **av, int ac)
166 {
167         struct hammer_ioc_prune_elm *elm;
168         u_int64_t from_time;
169         u_int64_t to_time;
170         u_int64_t every_time;
171         char *from_stamp_str;
172         char *to_stamp_str;
173
174         if (ac != 7)
175                 return(-1);
176         if (strcmp(av[0], filesystem) != 0)
177                 return(0);
178         if (strcmp(av[1], "from") != 0)
179                 return(-1);
180         if (strcmp(av[3], "to") != 0)
181                 return(-1);
182         if (strcmp(av[5], "every") != 0)
183                 return(-1);
184         if (parse_modulo_time(av[2], &from_time) < 0)
185                 return(-1);
186         if (parse_modulo_time(av[4], &to_time) < 0)
187                 return(-1);
188         if (parse_modulo_time(av[6], &every_time) < 0)
189                 return(-1);
190         if (from_time > to_time)
191                 return(-1);
192         if (from_time == 0 || to_time == 0) {
193                 fprintf(stderr, "Bad from or to time specification.\n");
194                 return(-1);
195         }
196         if (to_time % from_time != 0) {
197                 fprintf(stderr, "Bad TO time specification.\n"
198                         "It must be an integral multiple of FROM time\n");
199                 return(-1);
200         }
201         if (every_time == 0 ||
202             from_time % every_time != 0 ||
203             to_time % every_time != 0) {
204                 fprintf(stderr, "Bad 'every <modulo_time>' specification.\n"
205                         "It must be an integral subdivision of FROM and TO\n");
206                 return(-1);
207         }
208         if (prune->nelms == HAMMER_MAX_PRUNE_ELMS) {
209                 fprintf(stderr, "Too many prune specifications in file! "
210                         "Max is %d\n", HAMMER_MAX_PRUNE_ELMS);
211                 return(-1);
212         }
213
214         /*
215          * Example:  from 1m to 60m every 5m
216          */
217         elm = &prune->elms[prune->nelms++];
218         elm->beg_tid = now_tid - now_tid % to_time;
219         if (now_tid - elm->beg_tid < to_time)
220                 elm->beg_tid -= to_time;
221
222         elm->end_tid = now_tid - now_tid % from_time;
223         if (now_tid - elm->end_tid < from_time)
224                 elm->end_tid -= from_time;
225
226         elm->mod_tid = every_time;
227         assert(elm->beg_tid < elm->end_tid);
228
229         /*
230          * Convert back to local time for pretty printing
231          */
232         from_stamp_str = tid_to_stamp_str(elm->beg_tid);
233         to_stamp_str = tid_to_stamp_str(elm->end_tid);
234         printf("Prune %s to %s every ", from_stamp_str, to_stamp_str);
235
236         every_time /= 1000000000;
237         if (every_time < 60)
238                 printf("%lld second%s\n", every_time, plural(every_time == 1));
239         every_time /= 60;
240         if (every_time && every_time < 60)
241                 printf("%lld minute%s\n", every_time, plural(every_time == 1));
242         every_time /= 60;
243         if (every_time && every_time < 24)
244                 printf("%lld hour%s\n", every_time, plural(every_time == 1));
245         every_time /= 24;
246         if (every_time)
247                 printf("%lld day%s\n", every_time, plural(every_time == 1));
248
249         free(from_stamp_str);
250         free(to_stamp_str);
251         return(0);
252 }
253
254 /*
255  * Create softlinks in the form $linkpath/snap_ddmmmyyyy[_hhmmss]
256  */
257 static void
258 hammer_prune_create_links(const char *filesystem,
259                           struct hammer_ioc_prune *prune)
260 {
261         struct hammer_ioc_prune_elm *elm;
262         hammer_tid_t tid;
263         struct dirent *den;
264         char *path;
265         DIR *dir;
266
267         if ((dir = opendir(LinkPath)) == NULL) {
268                 fprintf(stderr, "Unable to access linkpath %s\n", LinkPath);
269                 return;
270         }
271         while ((den = readdir(dir)) != NULL) {
272                 if (strncmp(den->d_name, "snap-", 5) == 0) {
273                         asprintf(&path, "%s/%s", LinkPath, den->d_name);
274                         remove(path);
275                         free(path);
276                 }
277         }
278         closedir(dir);
279
280         for (elm = &prune->elms[0]; elm < &prune->elms[prune->nelms]; ++elm) {
281                 for (tid = elm->beg_tid;
282                      tid < elm->end_tid;
283                      tid += elm->mod_tid) {
284                         if (tid < prune->stat_oldest_tid)
285                                 continue;
286                         hammer_prune_make_softlink(filesystem, tid);
287                 }
288         }
289 }
290
291 static void
292 hammer_prune_make_softlink(const char *filesystem, hammer_tid_t tid)
293 {
294         struct tm *tp;
295         char *path;
296         char *target;
297         char buf[64];
298         time_t t;
299
300         t = (time_t)(tid / 1000000000);
301         tp = localtime(&t);
302
303         /*
304          * Construct the contents of the softlink.
305          */
306         asprintf(&target, "%s/@@0x%016llx", filesystem, tid);
307
308         /*
309          * Construct the name of the snap-shot softlink
310          */
311         if (tid % (1000000000ULL * 60 * 60 * 24) == 0) {
312                 strftime(buf, sizeof(buf), "snap-%d%b%Y", tp);
313         } else if (tid % (1000000000ULL * 60 * 60) == 0) {
314                 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H", tp);
315         } else if (tid % (1000000000ULL * 60) == 0) {
316                 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H%M", tp);
317         } else {
318                 strftime(buf, sizeof(buf), "snap-%d%b%Y_%H%M%S", tp);
319         }
320
321         asprintf(&path, "%s/%s", LinkPath, buf);
322         symlink(target, path);
323         free(path);
324         free(target);
325 }
326
327 static
328 int
329 parse_modulo_time(const char *str, u_int64_t *delta)
330 {
331         char *term;
332
333         *delta = strtoull(str, &term, 10);
334
335         switch(*term) {
336         case 'y':
337                 *delta *= 12;
338                 /* fall through */
339         case 'M':
340                 *delta *= 30;
341                 /* fall through */
342         case 'd':
343                 *delta *= 24;
344                 /* fall through */
345         case 'h':
346                 *delta *= 60;
347                 /* fall through */
348         case 'm':
349                 *delta *= 60;
350                 /* fall through */
351         case 's':
352                 break;
353         default:
354                 return(-1);
355         }
356         *delta *= 1000000000LL; /* TID's are in nanoseconds */
357         return(0);
358 }
359
360 static char *
361 tid_to_stamp_str(hammer_tid_t tid)
362 {
363         struct tm *tp;
364         char *buf = malloc(256);
365         time_t t;
366
367         t = (time_t)(tid / 1000000000);
368         tp = localtime(&t);
369         strftime(buf, 256, "%e-%b-%Y %H:%M:%S %Z", tp);
370         return(buf);
371 }
372
373 static void
374 prune_usage(int code)
375 {
376         fprintf(stderr, "Bad prune directive, specify one of:\n"
377                         "prune filesystem [using filename]\n"
378                         "prune filesystem from <modulo_time> to <modulo_time> every <modulo_time>\n");
379         exit(code);
380 }