+static int remove_oldest_snapshot(struct snapshot_list *sl)
+{
+ struct snapshot *s = get_oldest_snapshot(sl);
+
+ if (!s) /* no snapshot found */
+ return 0;
+ DSS_INFO_LOG("oldest snapshot: %s\n", s->name);
+ if (snapshot_is_being_created(s))
+ return 0;
+ return pre_remove_hook(s, "oldest");
+}
+
+static int rename_incomplete_snapshot(int64_t start)
+{
+ char *old_name;
+ int ret;
+
+ free(path_to_last_complete_snapshot);
+ ret = complete_name(start, get_current_time(),
+ &path_to_last_complete_snapshot);
+ if (ret < 0)
+ return ret;
+ old_name = incomplete_name(start);
+ ret = dss_rename(old_name, path_to_last_complete_snapshot);
+ if (ret >= 0)
+ DSS_NOTICE_LOG("%s -> %s\n", old_name,
+ path_to_last_complete_snapshot);
+ free(old_name);
+ return ret;
+}
+
+static int try_to_free_disk_space(int low_disk_space)
+{
+ int ret;
+ struct snapshot_list sl;
+ struct timeval now;
+
+ gettimeofday(&now, NULL);
+ if (tv_diff(&next_removal_check, &now, NULL) > 0)
+ return 0;
+ if (!low_disk_space && conf.keep_redundant_given)
+ return 0;
+ dss_get_snapshot_list(&sl);
+ ret = remove_outdated_snapshot(&sl);
+ if (ret) /* error, or we are removing something */
+ goto out;
+ /* no outdated snapshot */
+ ret = remove_redundant_snapshot(&sl);
+ if (ret)
+ goto out;
+ ret = 0;
+ if (!low_disk_space)
+ goto out;
+ DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
+ ret = remove_oldest_snapshot(&sl);
+ if (ret)
+ goto out;
+ DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
+ ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
+out:
+ free_snapshot_list(&sl);
+ return ret;
+}
+
+static int post_create_hook(void)
+{
+ int ret, fds[3] = {0, 0, 0};
+ char *cmd;
+
+ if (!conf.post_create_hook_given) {
+ snapshot_creation_status = HS_READY;
+ return 0;
+ }
+ cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
+ conf.dest_dir_arg, path_to_last_complete_snapshot);
+ DSS_NOTICE_LOG("executing %s\n", cmd);
+ ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
+ free(cmd);
+ if (ret < 0)
+ return ret;
+ snapshot_creation_status = HS_POST_RUNNING;
+ return ret;
+}
+
+static int post_remove_hook(void)