hammer utility - Add force support to cleanup
[dragonfly.git] / sbin / hammer / cmd_cleanup.c
index 0e0162d..8342684 100644 (file)
@@ -36,7 +36,7 @@
 /*
  * Clean up specific HAMMER filesystems or all HAMMER filesystems.
  *
- * If no filesystems are specified any HAMMER or null-mounted hammer PFS's
+ * If no filesystems are specified any HAMMER- or null-mounted hammer PFS's
  * are cleaned.
  *
  * Each HAMMER filesystem may contain a configuration file.  If no
@@ -47,7 +47,7 @@
  *     prune     1d 5m
  *     rebalance 1d 5m
  *     reblock   1d 5m
- *     recopy    30d 5m
+ *     recopy    30d 10m
  *
  * All hammer commands create and maintain cycle files in the snapshots
  * directory.
@@ -81,6 +81,7 @@ static void save_period(const char *snapshots_path, const char *cmd,
 static int check_softlinks(int fd, int new_config, const char *snapshots_path);
 static void cleanup_softlinks(int fd, int new_config,
                        const char *snapshots_path, int arg2, char *arg3);
+static void delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot);
 static int check_expired(const char *fpath, int arg2);
 
 static int create_snapshot(const char *path, const char *snapshots_path);
@@ -152,7 +153,7 @@ do_cleanup(const char *path)
        struct hammer_ioc_config config;
        struct hammer_ioc_version version;
        union hammer_ioc_mrecord_any mrec_tmp;
-       char *snapshots_path;
+       char *snapshots_path = NULL;
        char *config_path;
        struct stat st;
        char *cmd;
@@ -164,7 +165,7 @@ do_cleanup(const char *path)
        char buf[256];
        char *cbase;
        char *cptr;
-       FILE *fp;
+       FILE *fp = NULL;
        struct didpfs *didpfs;
        int snapshots_disabled = 0;
        int prune_warning = 0;
@@ -241,12 +242,14 @@ do_cleanup(const char *path)
                close(fd);
                return;
        } else if (mrec_tmp.pfs.pfsd.mirror_flags & HAMMER_PFSD_SLAVE) {
-               printf(" WARNING: must configure snapshot dir for PFS slave\n");
-               printf("\tWe suggest <fs>/var/slaves/<name> where "
-                      "<fs> is the base HAMMER fs\n");
-               printf("\tcontaining the slave\n");
-               close(fd);
-               return;
+               if (version.cur_version < 3) {
+                       printf(" WARNING: must configure snapshot dir for PFS slave\n");
+                       printf("\tWe suggest <fs>/var/slaves/<name> where "
+                              "<fs> is the base HAMMER fs\n");
+                       printf("\tcontaining the slave\n");
+                       close(fd);
+                       return;
+               }
        } else {
                asprintf(&snapshots_path,
                         "%s%ssnapshots", path, dividing_slash(path));
@@ -255,8 +258,10 @@ do_cleanup(const char *path)
        /*
         * Check for old-style config file
         */
-       asprintf(&config_path, "%s/config", snapshots_path);
-       fp = fopen(config_path, "r");
+       if (snapshots_path) {
+               asprintf(&config_path, "%s/config", snapshots_path);
+               fp = fopen(config_path, "r");
+       }
 
        /*
         * Handle upgrades to hammer version 3, move the config
@@ -337,29 +342,33 @@ do_cleanup(const char *path)
                        asprintf(&npath, "%s/root", SNAPSHOTS_BASE);
                else
                        asprintf(&npath, "%s/%s", SNAPSHOTS_BASE, path + 1);
-               if (stat(npath, &st) < 0 && errno == ENOENT) {
-                       if (stat(snapshots_path, &st) < 0 && errno == ENOENT) {
-                               printf(" HAMMER UPGRADE: Creating snapshots\n"
-                                      "\tCreating snapshots in %s\n",
-                                      npath);
-                               runcmd(&r, "mkdir -p %s", npath);
-                       } else {
-                               printf(" HAMMER UPGRADE: Moving snapshots\n"
-                                      "\tMoving snapshots from %s to %s\n",
-                                      snapshots_path, npath);
-                               runcmd(&r, "mkdir -p %s", npath);
-                               runcmd(&r, "cpdup %s %s", snapshots_path, npath);
-                               if (r != 0) {
-                           printf("Unable to move snapshots directory!\n");
-                           printf("Please fix this critical error.\n");
-                           printf("Aborting cleanup of %s\n", path);
-                                       close(fd);
-                                       return;
+               if (snapshots_path) {
+                       if (stat(npath, &st) < 0 && errno == ENOENT) {
+                               if (stat(snapshots_path, &st) < 0 && errno == ENOENT) {
+                                       printf(" HAMMER UPGRADE: Creating snapshots\n"
+                                              "\tCreating snapshots in %s\n",
+                                              npath);
+                                       runcmd(&r, "mkdir -p %s", npath);
+                               } else {
+                                       printf(" HAMMER UPGRADE: Moving snapshots\n"
+                                              "\tMoving snapshots from %s to %s\n",
+                                              snapshots_path, npath);
+                                       runcmd(&r, "mkdir -p %s", npath);
+                                       runcmd(&r, "cpdup %s %s", snapshots_path, npath);
+                                       if (r != 0) {
+                                   printf("Unable to move snapshots directory!\n");
+                                   printf("Please fix this critical error.\n");
+                                   printf("Aborting cleanup of %s\n", path);
+                                               close(fd);
+                                               return;
+                                       }
+                                       runcmd(&r, "rm -rf %s", snapshots_path);
                                }
-                               runcmd(&r, "rm -rf %s", snapshots_path);
                        }
+                       free(snapshots_path);
+               } else if (stat(npath, &st) < 0 && errno == ENOENT) {
+                       runcmd(&r, "mkdir -p %s", npath);
                }
-               free(snapshots_path);
                snapshots_path = npath;
        }
 
@@ -388,6 +397,9 @@ do_cleanup(const char *path)
                cbase = cptr + 1;
 
                cmd = strtok(buf, WS);
+               if (cmd == NULL || cmd[0] == '#')
+                       continue;
+
                arg1 = 0;
                arg2 = 0;
                arg3 = NULL;
@@ -530,8 +542,7 @@ config_init(const char *path, struct hammer_ioc_config *config)
                "prune     1d 5m\n"
                "rebalance 1d 5m\n"
                "reblock   1d 5m\n"
-               "recopy    30d 10m\n"
-               "rebalance 1d 5m\n");
+               "recopy    30d 10m\n");
 }
 
 /*
@@ -606,7 +617,11 @@ migrate_one_snapshot(int fd, const char *fpath,
                t = (time_t)-1;
                tid = (hammer_tid_t)(int64_t)-1;
 
-               ptr = fpath;
+               /* fpath may contain directory components */
+               if ((ptr = strrchr(fpath, '/')) != NULL)
+                       ++ptr;
+               else
+                       ptr = fpath;
                while (*ptr && *ptr != '-' && *ptr != '.')
                        ++ptr;
                if (*ptr)
@@ -638,6 +653,9 @@ migrate_one_snapshot(int fd, const char *fpath,
                        snprintf(snap->label, sizeof(snap->label),
                                 "migrated");
                        ++snapshot->count;
+               } else {
+                       printf("    non-canonical snapshot softlink: %s->%s\n",
+                              fpath, linkbuf);
                }
        }
 
@@ -706,6 +724,8 @@ dividing_slash(const char *path)
  * Periods in minutes, hours, or days are assumed to have been crossed
  * if the local time crosses a minute, hour, or day boundary regardless
  * of how close the last operation actually was.
+ *
+ * If ForceOpt is set always return true.
  */
 static int
 check_period(const char *snapshots_path, const char *cmd, int arg1,
@@ -721,6 +741,12 @@ check_period(const char *snapshots_path, const char *cmd, int arg1,
        time(savep);
        localtime_r(savep, &tp1);
 
+       /*
+        * Force run if -F
+        */
+       if (ForceOpt)
+               return(1);
+
        /*
         * Retrieve the start time of the last successful operation.
         */
@@ -888,6 +914,7 @@ cleanup_softlinks(int fd, int new_config,
                struct hammer_snapshot_data *snap;
                struct tm *tp;
                time_t t;
+               time_t dt;
                char snapts[32];
                u_int32_t i;
 
@@ -901,12 +928,13 @@ cleanup_softlinks(int fd, int new_config,
                        }
                        for (i = 0; i < snapshot.count; ++i) {
                                snap = &snapshot.snaps[i];
-                               t = time(NULL) - snap->ts / 1000000ULL;
-                               if ((int)t > arg2 && snap->tid != 0) {
+                               t = snap->ts / 1000000ULL;
+                               dt = time(NULL) - t;
+                               if ((int)dt > arg2 || snap->tid == 0) {
                                        dsnapshot.snaps[dsnapshot.count++] =
                                                *snap;
                                }
-                               if ((int)t > arg2 && VerboseOpt) {
+                               if ((int)dt > arg2 && VerboseOpt) {
                                        tp = localtime(&t);
                                        strftime(snapts, sizeof(snapts),
                                                 "%Y-%m-%d %H:%M:%S %Z", tp);
@@ -915,31 +943,38 @@ cleanup_softlinks(int fd, int new_config,
                                               snapts,
                                               snap->label);
                                }
-                               if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL) {
-                                       if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, &dsnapshot) < 0) {
-                                               printf("    Ioctl to delete snapshots failed: %s index %d\n", strerror(errno), dsnapshot.index);
-                                       } else if (dsnapshot.head.error) {
-                                               printf("    Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot.head.error));
-                                               exit(1);
-                                       }
-                                       dsnapshot.index = 0;
-                                       dsnapshot.count = 0;
-                                       dsnapshot.head.error = 0;
-                               }
+                               if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
+                                       delete_snapshots(fd, &dsnapshot);
                        }
                } while (snapshot.head.error == 0 && snapshot.count);
 
-               if (dsnapshot.count) {
-                       if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, &dsnapshot) < 0) {
-                               printf("    Ioctl to delete snapshots failed: %s\n", strerror(errno));
-                       } else if (dsnapshot.head.error) {
-                               printf("    Ioctl to delete snapshots failed: %s\n", strerror(dsnapshot.head.error));
-                       }
-                       dsnapshot.count = 0;
-                       dsnapshot.index = 0;
-                       dsnapshot.head.error = 0;
+               if (dsnapshot.count)
+                       delete_snapshots(fd, &dsnapshot);
+       }
+}
+
+static void
+delete_snapshots(int fd, struct hammer_ioc_snapshot *dsnapshot)
+{
+       for (;;) {
+               if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnapshot) < 0) {
+                       printf("    Ioctl to delete snapshots failed: %s\n",
+                              strerror(errno));
+                       break;
                }
+               if (dsnapshot->head.error) {
+                       printf("    Ioctl to delete snapshots failed at "
+                              "snap=%016jx: %s\n",
+                              dsnapshot->snaps[dsnapshot->index].tid,
+                              strerror(dsnapshot->head.error));
+                       if (++dsnapshot->index < dsnapshot->count)
+                               continue;
+               }
+               break;
        }
+       dsnapshot->index = 0;
+       dsnapshot->count = 0;
+       dsnapshot->head.error = 0;
 }
 
 /*