sbin/hammer: Drop non inode case from get_leaf_crc()
[dragonfly.git] / sbin / hammer / cmd_cleanup.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 /*
35  * Clean up specific HAMMER filesystems or all HAMMER filesystems.
36  *
37  * If no filesystems are specified any HAMMER- or null-mounted hammer PFS's
38  * are cleaned.
39  *
40  * Each HAMMER filesystem may contain a configuration file.  If no
41  * configuration file is present one will be created with the following
42  * defaults:
43  *
44  *      snapshots 1d 60d        (0d 0d for /tmp, /var/tmp, /usr/obj)
45  *      prune     1d 5m
46  *      rebalance 1d 5m
47  *      #dedup    1d 5m         (not enabled by default)
48  *      reblock   1d 5m
49  *      recopy    30d 10m
50  *
51  * All hammer commands create and maintain cycle files in the snapshots
52  * directory.
53  *
54  * For HAMMER version 2- the configuration file is a named 'config' in
55  * the snapshots directory, which defaults to <pfs>/snapshots.
56  * For HAMMER version 3+ the configuration file is saved in filesystem
57  * meta-data. The snapshots directory defaults to /var/hammer/<pfs>
58  * (/var/hammer/root for root mount).
59  */
60
61 #include <libutil.h>
62
63 #include "hammer.h"
64
65 struct didpfs {
66         struct didpfs *next;
67         uuid_t          uuid;
68 };
69
70 static void do_cleanup(const char *path);
71 static void config_init(const char *path, struct hammer_ioc_config *config);
72 static void migrate_config(FILE *fp, struct hammer_ioc_config *config);
73 static void migrate_snapshots(int fd, const char *snapshots_path);
74 static void migrate_one_snapshot(int fd, const char *fpath,
75                         struct hammer_ioc_snapshot *snapshot);
76 static int strtosecs(char *ptr);
77 static const char *dividing_slash(const char *path);
78 static int check_period(const char *snapshots_path, const char *cmd, int arg1,
79                         time_t *savep);
80 static void save_period(const char *snapshots_path, const char *cmd,
81                         time_t savet);
82 static int check_softlinks(int fd, int new_config, const char *snapshots_path);
83 static void cleanup_softlinks(int fd, int new_config,
84                         const char *snapshots_path, int arg2, char *arg3);
85 static void delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot);
86 static int check_expired(const char *fpath, int arg2);
87
88 static int create_snapshot(const char *path, const char *snapshots_path);
89 static int cleanup_rebalance(const char *path, const char *snapshots_path,
90                         int arg1, int arg2);
91 static int cleanup_prune(const char *path, const char *snapshots_path,
92                         int arg1, int arg2, int snapshots_disabled);
93 static int cleanup_reblock(const char *path, const char *snapshots_path,
94                         int arg1, int arg2);
95 static int cleanup_recopy(const char *path, const char *snapshots_path,
96                         int arg1, int arg2);
97 static int cleanup_dedup(const char *path, const char *snapshots_path,
98                         int arg1, int arg2);
99
100 static void runcmd(int *resp, const char *ctl, ...) __printflike(2, 3);
101
102 #define WS      " \t\r\n"
103
104 struct didpfs *FirstPFS;
105
106 void
107 hammer_cmd_cleanup(char **av, int ac)
108 {
109         char *fstype, *fs, *path;
110         struct statfs *stfsbuf;
111         int mntsize, i;
112
113         tzset();
114         if (ac == 0) {
115                 mntsize = getmntinfo(&stfsbuf, MNT_NOWAIT);
116                 if (mntsize > 0) {
117                         for (i=0; i < mntsize; i++) {
118                                 /*
119                                  * We will cleanup in the case fstype is hammer.
120                                  * If we have null-mounted PFS, we check the
121                                  * mount source. If it looks like a PFS, we
122                                  * proceed to cleanup also.
123                                  */
124                                 fstype = stfsbuf[i].f_fstypename;
125                                 fs = stfsbuf[i].f_mntfromname;
126                                 if ((strcmp(fstype, "hammer") == 0) ||
127                                     ((strcmp(fstype, "null") == 0) &&
128                                      (strstr(fs, "/@@0x") != NULL ||
129                                       strstr(fs, "/@@-1") != NULL))) {
130                                         path = stfsbuf[i].f_mntonname;
131                                         do_cleanup(path);
132                                 }
133                         }
134                 }
135         } else {
136                 while (ac) {
137                         do_cleanup(*av);
138                         --ac;
139                         ++av;
140                 }
141         }
142 }
143
144 static
145 void
146 do_cleanup(const char *path)
147 {
148         struct hammer_ioc_pseudofs_rw pfs;
149         struct hammer_ioc_config config;
150         struct hammer_ioc_version version;
151         union hammer_ioc_mrecord_any mrec_tmp;
152         char *snapshots_path = NULL;
153         char *config_path;
154         struct stat st;
155         char *cmd;
156         char *ptr;
157         int arg1;
158         int arg2;
159         char *arg3;
160         time_t savet;
161         char buf[256];
162         char *cbase;
163         char *cptr;
164         FILE *fp = NULL;
165         struct didpfs *didpfs;
166         int snapshots_disabled = 0;
167         int prune_warning = 0;
168         int new_config = 0;
169         int snapshots_from_pfs = 0;
170         int fd;
171         int r;
172         int found_rebal = 0;
173
174         bzero(&mrec_tmp, sizeof(mrec_tmp));
175         clrpfs(&pfs, &mrec_tmp.pfs.pfsd, -1);
176
177         printf("cleanup %-20s -", path);
178         fd = open(path, O_RDONLY);
179         if (fd < 0) {
180                 printf(" unable to access directory: %s\n", strerror(errno));
181                 return;
182         }
183         if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
184                 printf(" not a HAMMER filesystem: %s\n", strerror(errno));
185                 close(fd);
186                 return;
187         }
188         if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
189                 printf(" unrecognized HAMMER version\n");
190                 close(fd);
191                 return;
192         }
193         bzero(&version, sizeof(version));
194         if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) {
195                 printf(" HAMMER filesystem but couldn't retrieve version!\n");
196                 close(fd);
197                 return;
198         }
199         HammerVersion = version.cur_version;
200
201         bzero(&config, sizeof(config));
202         if (version.cur_version >= 3) {
203                 if (ioctl(fd, HAMMERIOC_GET_CONFIG, &config) == 0 &&
204                     config.head.error == 0) {
205                         new_config = 1;
206                 }
207         }
208
209         /*
210          * Make sure we have not already handled this PFS.  Several nullfs
211          * mounts might alias the same PFS.
212          */
213         for (didpfs = FirstPFS; didpfs; didpfs = didpfs->next) {
214                 if (bcmp(&didpfs->uuid, &mrec_tmp.pfs.pfsd.unique_uuid, sizeof(uuid_t)) == 0) {
215                         printf(" PFS#%d already handled\n", pfs.pfs_id);
216                         close(fd);
217                         return;
218                 }
219         }
220         didpfs = malloc(sizeof(*didpfs));
221         didpfs->next = FirstPFS;
222         FirstPFS = didpfs;
223         didpfs->uuid = mrec_tmp.pfs.pfsd.unique_uuid;
224
225         /*
226          * Calculate the old snapshots directory for HAMMER VERSION < 3
227          *
228          * If the directory is explicitly specified in the PFS config
229          * we flag it and will not migrate it later.
230          */
231         if (mrec_tmp.pfs.pfsd.snapshots[0] == '/') {
232                 asprintf(&snapshots_path, "%s", mrec_tmp.pfs.pfsd.snapshots);
233                 snapshots_from_pfs = 1;
234         } else if (mrec_tmp.pfs.pfsd.snapshots[0]) {
235                 printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
236                 close(fd);
237                 return;
238         } else if (hammer_is_pfs_slave(&mrec_tmp.pfs.pfsd)) {
239                 if (version.cur_version < 3) {
240                         printf(" WARNING: must configure snapshot dir for PFS slave\n");
241                         printf("\tWe suggest <fs>/var/slaves/<name> where "
242                                "<fs> is the base HAMMER fs\n");
243                         printf("\tcontaining the slave\n");
244                         close(fd);
245                         return;
246                 }
247         } else {
248                 asprintf(&snapshots_path,
249                          "%s%ssnapshots", path, dividing_slash(path));
250         }
251
252         /*
253          * Check for old-style config file
254          */
255         if (snapshots_path) {
256                 asprintf(&config_path, "%s/config", snapshots_path);
257                 fp = fopen(config_path, "r");
258         }
259
260         /*
261          * Handle upgrades to hammer version 3, move the config
262          * file into meta-data.
263          *
264          * For the old config read the file into the config structure,
265          * we will parse it out of the config structure regardless.
266          */
267         if (version.cur_version >= 3) {
268                 if (fp) {
269                         printf("(migrating) ");
270                         fflush(stdout);
271                         migrate_config(fp, &config);
272                         migrate_snapshots(fd, snapshots_path);
273                         fclose(fp);
274                         if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) {
275                                 printf(" cannot init meta-data config!\n");
276                                 close(fd);
277                                 return;
278                         }
279                         remove(config_path);
280                 } else if (new_config == 0) {
281                         config_init(path, &config);
282                         if (ioctl(fd, HAMMERIOC_SET_CONFIG, &config) < 0) {
283                                 printf(" cannot init meta-data config!\n");
284                                 close(fd);
285                                 return;
286                         }
287                 }
288                 new_config = 1;
289         } else {
290                 /*
291                  * Create missing snapshots directory for HAMMER VERSION < 3
292                  */
293                 if (stat(snapshots_path, &st) < 0) {
294                         if (mkdir(snapshots_path, 0755) != 0) {
295                                 free(snapshots_path);
296                                 printf(" unable to create snapshot dir \"%s\": %s\n",
297                                         snapshots_path, strerror(errno));
298                                 close(fd);
299                                 return;
300                         }
301                 }
302
303                 /*
304                  *  Create missing config file for HAMMER VERSION < 3
305                  */
306                 if (fp == NULL) {
307                         config_init(path, &config);
308                         fp = fopen(config_path, "w");
309                         if (fp) {
310                                 fwrite(config.config.text, 1,
311                                         strlen(config.config.text), fp);
312                                 fclose(fp);
313                         }
314                 } else {
315                         migrate_config(fp, &config);
316                         fclose(fp);
317                 }
318         }
319
320         /*
321          * If snapshots_from_pfs is not set we calculate the new snapshots
322          * directory default (in /var) for HAMMER VERSION >= 3 and migrate
323          * the old snapshots directory over.
324          *
325          * People who have set an explicit snapshots directory will have
326          * to migrate the data manually into /var/hammer, or not bother at
327          * all.  People running slaves may wish to migrate it and then
328          * clear the snapshots specification in the PFS config for the
329          * slave.
330          */
331         if (new_config && snapshots_from_pfs == 0) {
332                 char *npath;
333
334                 if (path[0] != '/') {
335                         printf(" path must start with '/'\n");
336                         return;
337                 }
338                 if (strcmp(path, "/") == 0)
339                         asprintf(&npath, "%s/root", SNAPSHOTS_BASE);
340                 else
341                         asprintf(&npath, "%s/%s", SNAPSHOTS_BASE, path + 1);
342                 if (snapshots_path) {
343                         if (stat(npath, &st) < 0 && errno == ENOENT) {
344                                 if (stat(snapshots_path, &st) < 0 && errno == ENOENT) {
345                                         printf(" HAMMER UPGRADE: Creating snapshots\n"
346                                                "\tCreating snapshots in %s\n",
347                                                npath);
348                                         runcmd(&r, "mkdir -p %s", npath);
349                                 } else {
350                                         printf(" HAMMER UPGRADE: Moving snapshots\n"
351                                                "\tMoving snapshots from %s to %s\n",
352                                                snapshots_path, npath);
353                                         runcmd(&r, "mkdir -p %s", npath);
354                                         runcmd(&r, "cpdup %s %s", snapshots_path, npath);
355                                         if (r != 0) {
356                                                 printf("Unable to move snapshots directory!\n");
357                                                 printf("Please fix this critical error.\n");
358                                                 printf("Aborting cleanup of %s\n", path);
359                                                 close(fd);
360                                                 return;
361                                         }
362                                         runcmd(&r, "rm -rf %s", snapshots_path);
363                                 }
364                         }
365                         free(snapshots_path);
366                 } else if (stat(npath, &st) < 0 && errno == ENOENT) {
367                         runcmd(&r, "mkdir -p %s", npath);
368                 }
369                 snapshots_path = npath;
370         }
371
372         /*
373          * Lock the PFS.  fd is the base directory of the mounted PFS.
374          */
375         if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
376                 if (errno == EWOULDBLOCK)
377                         printf(" PFS#%d locked by other process\n", pfs.pfs_id);
378                 else
379                         printf(" can not lock %s: %s\n", config_path, strerror(errno));
380                 close(fd);
381                 return;
382         }
383
384         printf(" handle PFS#%d using %s\n", pfs.pfs_id, snapshots_path);
385
386         struct pidfh    *pfh = NULL;
387         static char     pidfile[PIDFILE_BUFSIZE];
388
389         snprintf (pidfile, PIDFILE_BUFSIZE, "%s/hammer.cleanup.%d",
390                 pidfile_loc, getpid());
391         pfh = pidfile_open(pidfile, 0644, NULL);
392         if (pfh == NULL)
393                 warn ("Unable to open or create %s", pidfile);
394         pidfile_write(pfh);
395
396         /*
397          * Process the config file
398          */
399         cbase = config.config.text;
400
401         while ((cptr = strchr(cbase, '\n')) != NULL) {
402                 bcopy(cbase, buf, cptr - cbase);
403                 buf[cptr - cbase] = 0;
404                 cbase = cptr + 1;
405
406                 cmd = strtok(buf, WS);
407                 if (cmd == NULL || cmd[0] == '#')
408                         continue;
409
410                 arg1 = 0;
411                 arg2 = 0;
412                 arg3 = NULL;
413                 if ((ptr = strtok(NULL, WS)) != NULL) {
414                         arg1 = strtosecs(ptr);
415                         if ((ptr = strtok(NULL, WS)) != NULL) {
416                                 arg2 = strtosecs(ptr);
417                                 arg3 = strtok(NULL, WS);
418                         }
419                 }
420
421                 printf("%20s - ", cmd);
422                 fflush(stdout);
423
424                 r = 1;
425                 if (strcmp(cmd, "snapshots") == 0) {
426                         if (arg1 == 0) {
427                                 if (arg2 &&
428                                     check_softlinks(fd, new_config,
429                                                     snapshots_path)) {
430                                         printf("only removing old snapshots\n");
431                                         prune_warning = 1;
432                                         cleanup_softlinks(fd, new_config,
433                                                           snapshots_path,
434                                                           arg2, arg3);
435                                 } else {
436                                         printf("disabled\n");
437                                         snapshots_disabled = 1;
438                                 }
439                         } else
440                         if (check_period(snapshots_path, cmd, arg1, &savet)) {
441                                 printf("run\n");
442                                 cleanup_softlinks(fd, new_config,
443                                                   snapshots_path,
444                                                   arg2, arg3);
445                                 r = create_snapshot(path, snapshots_path);
446                         } else {
447                                 printf("skip\n");
448                         }
449                 } else if (arg1 == 0) {
450                         /*
451                          * The commands following this check can't handle
452                          * a period of 0, so call the feature disabled and
453                          * ignore the directive.
454                          */
455                         printf("disabled\n");
456                 } else if (strcmp(cmd, "prune") == 0) {
457                         if (check_period(snapshots_path, cmd, arg1, &savet)) {
458                                 if (prune_warning) {
459                                         printf("run - WARNING snapshot "
460                                                "softlinks present "
461                                                "but snapshots disabled\n");
462                                 } else {
463                                         printf("run\n");
464                                 }
465                                 r = cleanup_prune(path, snapshots_path,
466                                               arg1, arg2, snapshots_disabled);
467                         } else {
468                                 printf("skip\n");
469                         }
470                 } else if (strcmp(cmd, "rebalance") == 0) {
471                         found_rebal = 1;
472                         if (check_period(snapshots_path, cmd, arg1, &savet)) {
473                                 printf("run");
474                                 fflush(stdout);
475                                 if (VerboseOpt)
476                                         printf("\n");
477                                 r = cleanup_rebalance(path, snapshots_path,
478                                                 arg1, arg2);
479                         } else {
480                                 printf("skip\n");
481                         }
482                 } else if (strcmp(cmd, "reblock") == 0) {
483                         if (check_period(snapshots_path, cmd, arg1, &savet)) {
484                                 printf("run");
485                                 fflush(stdout);
486                                 if (VerboseOpt)
487                                         printf("\n");
488                                 r = cleanup_reblock(path, snapshots_path,
489                                                 arg1, arg2);
490                         } else {
491                                 printf("skip\n");
492                         }
493                 } else if (strcmp(cmd, "recopy") == 0) {
494                         if (check_period(snapshots_path, cmd, arg1, &savet)) {
495                                 printf("run");
496                                 fflush(stdout);
497                                 if (VerboseOpt)
498                                         printf("\n");
499                                 r = cleanup_recopy(path, snapshots_path,
500                                                arg1, arg2);
501                         } else {
502                                 printf("skip\n");
503                         }
504                 } else if (strcmp(cmd, "dedup") == 0) {
505                         if (check_period(snapshots_path, cmd, arg1, &savet)) {
506                                 printf("run");
507                                 fflush(stdout);
508                                 if (VerboseOpt)
509                                         printf("\n");
510                                 r = cleanup_dedup(path, snapshots_path,
511                                                 arg1, arg2);
512                         } else {
513                                 printf("skip\n");
514                         }
515                 } else {
516                         printf("unknown directive\n");
517                         r = 1;
518                 }
519                 if (r == 0)
520                         save_period(snapshots_path, cmd, savet);
521         }
522
523         /*
524          * Add new rebalance feature if the config doesn't have it.
525          * (old style config only).
526          */
527         if (new_config == 0 && found_rebal == 0) {
528                 if ((fp = fopen(config_path, "r+")) != NULL) {
529                         fseek(fp, 0L, 2);
530                         fprintf(fp, "rebalance 1d 5m\n");
531                         fclose(fp);
532                 }
533         }
534
535         /*
536          * Cleanup, and delay a little
537          */
538         close(fd);
539         usleep(1000);
540         pidfile_close(pfh);
541         pidfile_remove(pfh);
542 }
543
544 /*
545  * Initialize new config data (new or old style)
546  */
547 static
548 void
549 config_init(const char *path, struct hammer_ioc_config *config)
550 {
551         const char *snapshots;
552
553         if (strcmp(path, "/tmp") == 0 ||
554             strcmp(path, "/var/tmp") == 0 ||
555             strcmp(path, "/usr/obj") == 0) {
556                 snapshots = "snapshots 0d 0d\n";
557         } else {
558                 snapshots = "snapshots 1d 60d\n";
559         }
560         bzero(config->config.text, sizeof(config->config.text));
561         snprintf(config->config.text, sizeof(config->config.text) - 1, "%s%s",
562                 snapshots,
563                 "prune     1d 5m\n"
564                 "rebalance 1d 5m\n"
565                 "#dedup    1d 5m\n"
566                 "reblock   1d 5m\n"
567                 "recopy    30d 10m\n");
568 }
569
570 /*
571  * Migrate configuration data from the old snapshots/config
572  * file to the new meta-data format.
573  */
574 static
575 void
576 migrate_config(FILE *fp, struct hammer_ioc_config *config)
577 {
578         int n;
579
580         n = fread(config->config.text, 1, sizeof(config->config.text) - 1, fp);
581         if (n >= 0)
582                 bzero(config->config.text + n, sizeof(config->config.text) - n);
583 }
584
585 /*
586  * Migrate snapshot softlinks in the snapshots directory to the
587  * new meta-data format.  The softlinks are left intact, but
588  * this way the pruning code won't lose track of them if you
589  * happen to blow away the snapshots directory.
590  */
591 static
592 void
593 migrate_snapshots(int fd, const char *snapshots_path)
594 {
595         struct hammer_ioc_snapshot snapshot;
596         struct dirent *den;
597         struct stat st;
598         DIR *dir;
599         char *fpath;
600
601         bzero(&snapshot, sizeof(snapshot));
602
603         if ((dir = opendir(snapshots_path)) != NULL) {
604                 while ((den = readdir(dir)) != NULL) {
605                         if (den->d_name[0] == '.')
606                                 continue;
607                         asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
608                         if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
609                                 migrate_one_snapshot(fd, fpath, &snapshot);
610                         free(fpath);
611                 }
612                 closedir(dir);
613         }
614         migrate_one_snapshot(fd, NULL, &snapshot);
615
616 }
617
618 /*
619  * Migrate a single snapshot.  If fpath is NULL the ioctl is flushed,
620  * otherwise it is flushed when it fills up.
621  */
622 static
623 void
624 migrate_one_snapshot(int fd, const char *fpath,
625                      struct hammer_ioc_snapshot *snapshot)
626 {
627         if (fpath) {
628                 hammer_snapshot_data_t snap;
629                 struct tm tm;
630                 time_t t;
631                 int year;
632                 int month;
633                 int day = 0;
634                 int hour = 0;
635                 int minute = 0;
636                 int r;
637                 char linkbuf[1024];
638                 const char *ptr;
639                 hammer_tid_t tid;
640
641                 t = (time_t)-1;
642                 tid = (hammer_tid_t)(int64_t)-1;
643
644                 /* fpath may contain directory components */
645                 if ((ptr = strrchr(fpath, '/')) != NULL)
646                         ++ptr;
647                 else
648                         ptr = fpath;
649                 while (*ptr && *ptr != '-' && *ptr != '.')
650                         ++ptr;
651                 if (*ptr)
652                         ++ptr;
653                 r = sscanf(ptr, "%4d%2d%2d-%2d%2d",
654                            &year, &month, &day, &hour, &minute);
655
656                 if (r >= 3) {
657                         bzero(&tm, sizeof(tm));
658                         tm.tm_isdst = -1;
659                         tm.tm_min = minute;
660                         tm.tm_hour = hour;
661                         tm.tm_mday = day;
662                         tm.tm_mon = month - 1;
663                         tm.tm_year = year - 1900;
664                         t = mktime(&tm);
665                 }
666                 bzero(linkbuf, sizeof(linkbuf));
667                 if (readlink(fpath, linkbuf, sizeof(linkbuf) - 1) > 0 &&
668                     (ptr = strrchr(linkbuf, '@')) != NULL &&
669                     ptr > linkbuf && ptr[-1] == '@') {
670                         tid = strtoull(ptr + 1, NULL, 16);
671                 }
672                 if (t != (time_t)-1 && tid != (hammer_tid_t)(int64_t)-1) {
673                         snap = &snapshot->snaps[snapshot->count];
674                         bzero(snap, sizeof(*snap));
675                         snap->tid = tid;
676                         snap->ts = (uint64_t)t * 1000000ULL;
677                         snprintf(snap->label, sizeof(snap->label),
678                                  "migrated");
679                         ++snapshot->count;
680                 } else {
681                         printf("    non-canonical snapshot softlink: %s->%s\n",
682                                fpath, linkbuf);
683                 }
684         }
685
686         if ((fpath == NULL && snapshot->count) ||
687             snapshot->count == HAMMER_SNAPS_PER_IOCTL) {
688                 printf(" (%d snapshots)", snapshot->count);
689 again:
690                 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, snapshot) < 0) {
691                         printf("    Ioctl to migrate snapshots failed: %s\n",
692                                strerror(errno));
693                 } else if (snapshot->head.error == EALREADY) {
694                         ++snapshot->index;
695                         goto again;
696                 } else if (snapshot->head.error) {
697                         printf("    Ioctl to migrate snapshots failed: %s\n",
698                                strerror(snapshot->head.error));
699                 }
700                 printf("index %d\n", snapshot->index);
701                 snapshot->index = 0;
702                 snapshot->count = 0;
703                 snapshot->head.error = 0;
704         }
705 }
706
707 static
708 int
709 strtosecs(char *ptr)
710 {
711         int val;
712
713         val = strtol(ptr, &ptr, 0);
714         switch(*ptr) {
715         case 'd':
716                 val *= 24;
717                 /* fall through */
718         case 'h':
719                 val *= 60;
720                 /* fall through */
721         case 'm':
722                 val *= 60;
723                 /* fall through */
724         case 's':
725                 break;
726         default:
727                 errx(1, "illegal suffix converting %s", ptr);
728                 /* not reached */
729                 break;
730         }
731         return(val);
732 }
733
734 static
735 const char *
736 dividing_slash(const char *path)
737 {
738         int len = strlen(path);
739         if (len && path[len-1] == '/')
740                 return("");
741         else
742                 return("/");
743 }
744
745 /*
746  * Check whether the desired period has elapsed since the last successful
747  * run.  The run may take a while and cross a boundary so we remember the
748  * current time_t so we can save it later on.
749  *
750  * Periods in minutes, hours, or days are assumed to have been crossed
751  * if the local time crosses a minute, hour, or day boundary regardless
752  * of how close the last operation actually was.
753  *
754  * If ForceOpt is set always return true.
755  */
756 static
757 int
758 check_period(const char *snapshots_path, const char *cmd, int arg1,
759         time_t *savep)
760 {
761         char *check_path;
762         struct tm tp1;
763         struct tm tp2;
764         FILE *fp;
765         time_t baset, lastt;
766         char buf[256];
767
768         time(savep);
769         localtime_r(savep, &tp1);
770
771         /*
772          * Force run if -F
773          */
774         if (ForceOpt)
775                 return(1);
776
777         /*
778          * Retrieve the start time of the last successful operation.
779          */
780         asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd);
781         fp = fopen(check_path, "r");
782         free(check_path);
783         if (fp == NULL)
784                 return(1);
785         if (fgets(buf, sizeof(buf), fp) == NULL) {
786                 fclose(fp);
787                 return(1);
788         }
789         fclose(fp);
790
791         lastt = strtol(buf, NULL, 0);
792         localtime_r(&lastt, &tp2);
793
794         /*
795          * Normalize the times.  e.g. if asked to do something on a 1-day
796          * interval the operation will be performed as soon as the day
797          * turns over relative to the previous operation, even if the previous
798          * operation ran a few seconds ago just before midnight.
799          */
800         if (arg1 % 60 == 0) {
801                 tp1.tm_sec = 0;
802                 tp2.tm_sec = 0;
803         }
804         if (arg1 % (60 * 60) == 0) {
805                 tp1.tm_min = 0;
806                 tp2.tm_min = 0;
807         }
808         if (arg1 % (24 * 60 * 60) == 0) {
809                 tp1.tm_hour = 0;
810                 tp2.tm_hour = 0;
811         }
812
813         baset = mktime(&tp1);
814         lastt = mktime(&tp2);
815
816 #if 0
817         printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1);
818 #endif
819
820         if ((int)(baset - lastt) >= arg1)
821                 return(1);
822         return(0);
823 }
824
825 /*
826  * Store the start time of the last successful operation.
827  */
828 static
829 void
830 save_period(const char *snapshots_path, const char *cmd,
831                         time_t savet)
832 {
833         char *ocheck_path;
834         char *ncheck_path;
835         FILE *fp;
836
837         asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd);
838         asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd);
839         fp = fopen(ncheck_path, "w");
840         if (fp) {
841                 fprintf(fp, "0x%08llx\n", (long long)savet);
842                 if (fclose(fp) == 0)
843                         rename(ncheck_path, ocheck_path);
844                 remove(ncheck_path);
845         } else {
846                 fprintf(stderr, "hammer: Unable to create period-file %s: %s\n",
847                         ncheck_path, strerror(errno));
848         }
849 }
850
851 /*
852  * Simply count the number of softlinks in the snapshots dir
853  */
854 static
855 int
856 check_softlinks(int fd, int new_config, const char *snapshots_path)
857 {
858         struct dirent *den;
859         struct stat st;
860         DIR *dir;
861         char *fpath;
862         int res = 0;
863
864         /*
865          * Old-style softlink-based snapshots
866          */
867         if ((dir = opendir(snapshots_path)) != NULL) {
868                 while ((den = readdir(dir)) != NULL) {
869                         if (den->d_name[0] == '.')
870                                 continue;
871                         asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
872                         if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
873                                 ++res;
874                         free(fpath);
875                 }
876                 closedir(dir);
877         }
878
879         /*
880          * New-style snapshots are stored as filesystem meta-data,
881          * count those too.
882          */
883         if (new_config) {
884                 struct hammer_ioc_snapshot snapshot;
885
886                 bzero(&snapshot, sizeof(snapshot));
887                 do {
888                         if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
889                                 err(2, "hammer cleanup: check_softlink "
890                                         "snapshot error");
891                                 /* not reached */
892                         }
893                         res += snapshot.count;
894                 } while (snapshot.head.error == 0 && snapshot.count);
895         }
896         return (res);
897 }
898
899 /*
900  * Clean up expired softlinks in the snapshots dir
901  */
902 static
903 void
904 cleanup_softlinks(int fd, int new_config,
905                   const char *snapshots_path, int arg2, char *arg3)
906 {
907         struct dirent *den;
908         struct stat st;
909         DIR *dir;
910         char *fpath;
911         int anylink = 0;
912
913         if (arg3 != NULL && strstr(arg3, "any") != NULL)
914                 anylink = 1;
915
916         if ((dir = opendir(snapshots_path)) != NULL) {
917                 while ((den = readdir(dir)) != NULL) {
918                         if (den->d_name[0] == '.')
919                                 continue;
920                         asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
921                         if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) &&
922                             (anylink || strncmp(den->d_name, "snap-", 5) == 0)) {
923                                 if (check_expired(den->d_name, arg2)) {
924                                         if (VerboseOpt) {
925                                                 printf("    expire %s\n",
926                                                         fpath);
927                                         }
928                                         remove(fpath);
929                                 }
930                         }
931                         free(fpath);
932                 }
933                 closedir(dir);
934         }
935
936         /*
937          * New-style snapshots are stored as filesystem meta-data,
938          * count those too.
939          */
940         if (new_config) {
941                 struct hammer_ioc_snapshot snapshot;
942                 struct hammer_ioc_snapshot dsnapshot;
943                 hammer_snapshot_data_t snap;
944                 struct tm *tp;
945                 time_t t;
946                 time_t dt;
947                 char snapts[32];
948                 uint32_t i;
949
950                 bzero(&snapshot, sizeof(snapshot));
951                 bzero(&dsnapshot, sizeof(dsnapshot));
952                 do {
953                         if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
954                                 err(2, "hammer cleanup: check_softlink "
955                                         "snapshot error");
956                                 /* not reached */
957                         }
958                         for (i = 0; i < snapshot.count; ++i) {
959                                 snap = &snapshot.snaps[i];
960                                 t = snap->ts / 1000000ULL;
961                                 dt = time(NULL) - t;
962                                 if ((int)dt > arg2 || snap->tid == 0) {
963                                         dsnapshot.snaps[dsnapshot.count++] =
964                                                 *snap;
965                                 }
966                                 if ((int)dt > arg2 && VerboseOpt) {
967                                         tp = localtime(&t);
968                                         strftime(snapts, sizeof(snapts),
969                                                  "%Y-%m-%d %H:%M:%S %Z", tp);
970                                         printf("    expire 0x%016jx %s %s\n",
971                                                (uintmax_t)snap->tid,
972                                                snapts,
973                                                snap->label);
974                                 }
975                                 if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
976                                         delete_snapshots(fd, &dsnapshot);
977                         }
978                 } while (snapshot.head.error == 0 && snapshot.count);
979
980                 if (dsnapshot.count)
981                         delete_snapshots(fd, &dsnapshot);
982         }
983 }
984
985 static
986 void
987 delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot)
988 {
989         for (;;) {
990                 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnapshot) < 0) {
991                         printf("    Ioctl to delete snapshots failed: %s\n",
992                                strerror(errno));
993                         break;
994                 }
995                 if (dsnapshot->head.error) {
996                         printf("    Ioctl to delete snapshots failed at "
997                                "snap=%016jx: %s\n",
998                                dsnapshot->snaps[dsnapshot->index].tid,
999                                strerror(dsnapshot->head.error));
1000                         if (++dsnapshot->index < dsnapshot->count)
1001                                 continue;
1002                 }
1003                 break;
1004         }
1005         dsnapshot->index = 0;
1006         dsnapshot->count = 0;
1007         dsnapshot->head.error = 0;
1008 }
1009
1010 /*
1011  * Take a softlink path in the form snap-yyyymmdd-hhmm and the
1012  * expiration in seconds (arg2) and return non-zero if the softlink
1013  * has expired.
1014  */
1015 static
1016 int
1017 check_expired(const char *fpath, int arg2)
1018 {
1019         struct tm tm;
1020         time_t t;
1021         int year;
1022         int month;
1023         int day = 0;
1024         int hour = 0;
1025         int minute = 0;
1026         int r;
1027
1028         while (*fpath && *fpath != '-' && *fpath != '.')
1029                 ++fpath;
1030         if (*fpath)
1031                 ++fpath;
1032
1033         r = sscanf(fpath, "%4d%2d%2d-%2d%2d",
1034                    &year, &month, &day, &hour, &minute);
1035
1036         if (r >= 3) {
1037                 bzero(&tm, sizeof(tm));
1038                 tm.tm_isdst = -1;
1039                 tm.tm_min = minute;
1040                 tm.tm_hour = hour;
1041                 tm.tm_mday = day;
1042                 tm.tm_mon = month - 1;
1043                 tm.tm_year = year - 1900;
1044                 t = mktime(&tm);
1045                 if (t == (time_t)-1)
1046                         return(0);
1047                 t = time(NULL) - t;
1048                 if ((int)t > arg2)
1049                         return(1);
1050         }
1051         return(0);
1052 }
1053
1054 /*
1055  * Issue a snapshot.
1056  */
1057 static
1058 int
1059 create_snapshot(const char *path, const char *snapshots_path)
1060 {
1061         int r;
1062
1063         runcmd(&r, "hammer snapshot %s %s", path, snapshots_path);
1064         return(r);
1065 }
1066
1067 static
1068 int
1069 cleanup_prune(const char *path, const char *snapshots_path,
1070                   int arg1 __unused, int arg2, int snapshots_disabled)
1071 {
1072         const char *path_or_snapshots_path;
1073
1074         /*
1075          * If the snapshots_path (e.g. /var/hammer/...) has no snapshots
1076          * in it then prune will get confused and prune the filesystem
1077          * containing the snapshots_path instead of the requested
1078          * filesystem.  De-confuse prune.  We need a better way.
1079          */
1080         if (hammer_softprune_testdir(snapshots_path))
1081                 path_or_snapshots_path = snapshots_path;
1082         else
1083                 path_or_snapshots_path = path;
1084
1085         /*
1086          * If snapshots have been disabled run prune-everything instead
1087          * of prune.
1088          */
1089         if (snapshots_disabled && arg2) {
1090                 runcmd(NULL,
1091                        "hammer -c %s/.prune.cycle -t %d prune-everything %s",
1092                        snapshots_path, arg2, path);
1093         } else if (snapshots_disabled) {
1094                 runcmd(NULL, "hammer prune-everything %s", path);
1095         } else if (arg2) {
1096                 runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s",
1097                         snapshots_path, arg2, path_or_snapshots_path);
1098         } else {
1099                 runcmd(NULL, "hammer prune %s", path_or_snapshots_path);
1100         }
1101         return(0);
1102 }
1103
1104 static
1105 int
1106 cleanup_rebalance(const char *path, const char *snapshots_path,
1107                   int arg1 __unused, int arg2)
1108 {
1109         if (VerboseOpt == 0) {
1110                 printf(".");
1111                 fflush(stdout);
1112         }
1113
1114         runcmd(NULL,
1115                "hammer -c %s/.rebalance.cycle -t %d rebalance %s",
1116                snapshots_path, arg2, path);
1117         if (VerboseOpt == 0) {
1118                 printf(".");
1119                 fflush(stdout);
1120         }
1121         if (VerboseOpt == 0)
1122                 printf("\n");
1123         return(0);
1124 }
1125
1126 static
1127 int
1128 cleanup_reblock(const char *path, const char *snapshots_path,
1129                   int arg1 __unused, int arg2)
1130 {
1131         if (VerboseOpt == 0) {
1132                 printf(".");
1133                 fflush(stdout);
1134         }
1135
1136         /*
1137          * When reblocking the B-Tree always reblock everything in normal
1138          * mode.
1139          */
1140         runcmd(NULL,
1141                "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s",
1142                snapshots_path, arg2, path);
1143         if (VerboseOpt == 0) {
1144                 printf(".");
1145                 fflush(stdout);
1146         }
1147
1148         /*
1149          * When reblocking the inodes always reblock everything in normal
1150          * mode.
1151          */
1152         runcmd(NULL,
1153                "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s",
1154                snapshots_path, arg2, path);
1155         if (VerboseOpt == 0) {
1156                 printf(".");
1157                 fflush(stdout);
1158         }
1159
1160         /*
1161          * When reblocking the directories always reblock everything in normal
1162          * mode.
1163          */
1164         runcmd(NULL,
1165                "hammer -c %s/.reblock-4.cycle -t %d reblock-dirs %s",
1166                snapshots_path, arg2, path);
1167         if (VerboseOpt == 0) {
1168                 printf(".");
1169                 fflush(stdout);
1170         }
1171
1172         /*
1173          * Do not reblock all the data in normal mode.
1174          */
1175         runcmd(NULL,
1176                "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
1177                snapshots_path, arg2, path);
1178         if (VerboseOpt == 0)
1179                 printf("\n");
1180         return(0);
1181 }
1182
1183 static
1184 int
1185 cleanup_recopy(const char *path, const char *snapshots_path,
1186                   int arg1 __unused, int arg2)
1187 {
1188         if (VerboseOpt == 0) {
1189                 printf(".");
1190                 fflush(stdout);
1191         }
1192         runcmd(NULL,
1193                "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
1194                snapshots_path, arg2, path);
1195         if (VerboseOpt == 0) {
1196                 printf(".");
1197                 fflush(stdout);
1198         }
1199         runcmd(NULL,
1200                "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
1201                snapshots_path, arg2, path);
1202         if (VerboseOpt == 0) {
1203                 printf(".");
1204                 fflush(stdout);
1205         }
1206         runcmd(NULL,
1207                "hammer -c %s/.recopy-4.cycle -t %d reblock-dirs %s",
1208                snapshots_path, arg2, path);
1209         if (VerboseOpt == 0) {
1210                 printf(".");
1211                 fflush(stdout);
1212         }
1213         runcmd(NULL,
1214                "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
1215                snapshots_path, arg2, path);
1216         if (VerboseOpt == 0)
1217                 printf("\n");
1218         return(0);
1219 }
1220
1221 static
1222 int
1223 cleanup_dedup(const char *path, const char *snapshots_path __unused,
1224                   int arg1 __unused, int arg2)
1225 {
1226         if (VerboseOpt == 0) {
1227                 printf(".");
1228                 fflush(stdout);
1229         }
1230
1231         runcmd(NULL, "hammer -t %d dedup %s", arg2, path);
1232         if (VerboseOpt == 0) {
1233                 printf(".");
1234                 fflush(stdout);
1235         }
1236         if (VerboseOpt == 0)
1237                 printf("\n");
1238         return(0);
1239 }
1240
1241 static
1242 void
1243 runcmd(int *resp, const char *ctl, ...)
1244 {
1245         va_list va;
1246         char *cmd;
1247         char *arg;
1248         char **av;
1249         int n;
1250         int nmax;
1251         int res;
1252         pid_t pid;
1253
1254         /*
1255          * Generate the command
1256          */
1257         va_start(va, ctl);
1258         vasprintf(&cmd, ctl, va);
1259         va_end(va);
1260         if (VerboseOpt)
1261                 printf("    %s\n", cmd);
1262
1263         /*
1264          * Break us down into arguments.  We do not just use system() here
1265          * because it blocks SIGINT and friends.
1266          */
1267         n = 0;
1268         nmax = 16;
1269         av = malloc(sizeof(char *) * nmax);
1270
1271         for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) {
1272                 if (n == nmax - 1) {
1273                         nmax += 16;
1274                         av = realloc(av, sizeof(char *) * nmax);
1275                 }
1276                 av[n++] = arg;
1277         }
1278         av[n++] = NULL;
1279
1280         /*
1281          * Run the command.
1282          */
1283         RunningIoctl = 1;
1284         if ((pid = fork()) == 0) {
1285                 if (VerboseOpt < 2) {
1286                         int fd = open("/dev/null", O_RDWR);
1287                         dup2(fd, 1);
1288                         close(fd);
1289                 }
1290                 execvp(av[0], av);
1291                 _exit(127);
1292         } else if (pid < 0) {
1293                 res = 127;
1294         } else {
1295                 int status;
1296
1297                 while (waitpid(pid, &status, 0) != pid)
1298                         ;
1299                 res = WEXITSTATUS(status);
1300         }
1301         RunningIoctl = 0;
1302         if (DidInterrupt)
1303                 _exit(1);
1304
1305         free(cmd);
1306         free(av);
1307         if (resp)
1308                 *resp = res;
1309 }