/*
- * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2008-2009 Andre Noll <maan@systemlinux.org>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
static struct timeval next_removal_check;
/** Creation time of the snapshot currently being created. */
static int64_t current_snapshot_creation_time;
-/* The snapshot currently being removed. */
+/** The snapshot currently being removed. */
struct snapshot *snapshot_currently_being_removed;
/** Needed by the post-create hook. */
static char *path_to_last_complete_snapshot;
+static char *name_of_reference_snapshot;
/** \sa \ref snap.h for details. */
enum hook_status snapshot_creation_status;
/** \sa \ref snap.h for details. */
return ret;
}
-static int pre_remove_hook(struct snapshot *s, char *why)
+static int pre_remove_hook(struct snapshot *s, const char *why)
{
int ret, fds[3] = {0, 0, 0};
char *cmd;
+ if (!s)
+ return 0;
DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
assert(snapshot_removal_status == HS_READY);
assert(remove_pid == 0);
return s->creation_time == current_snapshot_creation_time;
}
+static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
+{
+ struct snapshot *s;
+ int i;
+
+ DSS_DEBUG_LOG("looking for orphaned snapshots\n");
+ FOR_EACH_SNAPSHOT(s, i, sl) {
+ if (snapshot_is_being_created(s))
+ continue;
+ /*
+ * We know that no rm is currently running, so if s is marked
+ * as being deleted, a previously started rm must have failed.
+ */
+ if (s->flags & SS_BEING_DELETED)
+ return s;
+
+ if (s->flags & SS_COMPLETE) /* good snapshot */
+ continue;
+ /*
+ * This snapshot is incomplete and it is not the snapshot
+ * currently being created. However, we must not remove it if
+ * rsync is about to be restarted. As only the newest snapshot
+ * can be restarted, this snapshot is orphaned if it is not the
+ * newest snapshot or if we are not about to restart rsync.
+ */
+ if (get_newest_snapshot(sl) != s)
+ return s;
+ if (snapshot_creation_status != HS_NEEDS_RESTART)
+ return s;
+ }
+ /* no orphaned snapshots */
+ return NULL;
+}
+
+static int is_reference_snapshot(struct snapshot *s)
+{
+ if (!name_of_reference_snapshot)
+ return 0;
+ return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
+}
+
/*
* return: 0: no redundant snapshots, 1: rm process started, negative: error
*/
-static int remove_redundant_snapshot(struct snapshot_list *sl)
+static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
{
- int ret, i, interval;
+ int i, interval;
struct snapshot *s;
unsigned missing = 0;
if (snapshot_is_being_created(s))
continue;
+ if (is_reference_snapshot(s))
+ continue;
//DSS_DEBUG_LOG("checking %s\n", s->name);
if (s->interval > interval) {
prev = s;
prev = s;
}
assert(victim);
- if (conf.dry_run_given) {
- dss_msg("%s would be removed (interval = %i)\n",
- victim->name, victim->interval);
- continue;
- }
- ret = pre_remove_hook(victim, "redundant");
- return ret < 0? ret : 1;
+ return victim;
}
- return 0;
+ return NULL;
}
-static int remove_outdated_snapshot(struct snapshot_list *sl)
+static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
{
- int i, ret;
+ int i;
struct snapshot *s;
DSS_DEBUG_LOG("looking for snapshots belonging to intervals greater than %d\n",
FOR_EACH_SNAPSHOT(s, i, sl) {
if (snapshot_is_being_created(s))
continue;
- if (s->interval <= conf.num_intervals_arg)
+ if (is_reference_snapshot(s))
continue;
- if (conf.dry_run_given) {
- dss_msg("%s would be removed (interval = %i)\n",
- s->name, s->interval);
+ if (s->interval <= conf.num_intervals_arg)
continue;
- }
- ret = pre_remove_hook(s, "outdated");
- if (ret < 0)
- return ret;
- return 1;
+ return s;
}
- return 0;
+ return NULL;
}
-static int remove_oldest_snapshot(struct snapshot_list *sl)
+struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
{
- int ret;
- 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;
- ret = pre_remove_hook(s, "oldest");
- if (ret < 0)
- return ret;
- return 1;
+ int i;
+ struct snapshot *s;
+ FOR_EACH_SNAPSHOT(s, i, sl) {
+ if (snapshot_is_being_created(s))
+ continue;
+ if (is_reference_snapshot(s))
+ continue;
+ DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
+ return s;
+ }
+ return NULL;
}
static int rename_incomplete_snapshot(int64_t start)
return ret;
}
-static int try_to_free_disk_space(int low_disk_space)
+static int try_to_free_disk_space(void)
{
int ret;
struct snapshot_list sl;
+ struct snapshot *victim;
struct timeval now;
+ const char *why;
+ int low_disk_space;
+ ret = disk_space_low();
+ if (ret < 0)
+ return ret;
+ low_disk_space = ret;
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 && sl.num_snapshots <= 1)
+ goto out;
+ why = "outdated";
+ victim = find_outdated_snapshot(&sl);
+ if (victim)
+ goto remove;
+ why = "redundant";
+ victim = find_redundant_snapshot(&sl);
+ if (victim)
+ goto remove;
+ /* try harder only if disk space is low */
if (!low_disk_space)
goto out;
+ why = "orphaned";
+ victim = find_orphaned_snapshot(&sl);
+ if (victim)
+ goto remove;
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");
+ victim = find_oldest_removable_snapshot(&sl);
+ if (victim)
+ goto remove;
+ DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
+ goto out;
+remove:
+ ret = pre_remove_hook(victim, why);
out:
free_snapshot_list(&sl);
return ret;
if (ret < 0)
goto out;
snapshot_creation_status = HS_SUCCESS;
+ free(name_of_reference_snapshot);
+ name_of_reference_snapshot = NULL;
out:
create_pid = 0;
create_process_stopped = 0;
DSS_NOTICE_LOG("deferring snapshot creation...\n");
warn_count = 60; /* warn only once per hour */
}
+ gettimeofday(&next_snapshot_time, NULL);
+ next_snapshot_time.tv_sec += 60;
snapshot_creation_status = HS_READY;
ret = 0;
goto out;
static void create_rsync_argv(char ***argv, int64_t *num)
{
- char *logname, *newest;
+ char *logname;
int i = 0, j;
struct snapshot_list sl;
dss_get_snapshot_list(&sl);
- newest = name_of_newest_complete_snapshot(&sl);
+ assert(!name_of_reference_snapshot);
+ name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
free_snapshot_list(&sl);
*argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
(*argv)[i++] = dss_strdup("--delete");
for (j = 0; j < conf.rsync_option_given; j++)
(*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
- if (newest) {
- DSS_INFO_LOG("using %s as reference snapshot\n", newest);
- (*argv)[i++] = make_message("--link-dest=../%s", newest);
- free(newest);
+ if (name_of_reference_snapshot) {
+ DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
+ (*argv)[i++] = make_message("--link-dest=../%s",
+ name_of_reference_snapshot);
} else
- DSS_INFO_LOG("no previous snapshot found\n");
+ DSS_INFO_LOG("no suitable reference snapshot found\n");
logname = dss_logname();
if (use_rsync_locally(logname))
(*argv)[i++] = dss_strdup(conf.source_dir_arg);
for (;;) {
fd_set rfds;
- int low_disk_space;
struct timeval *tvp;
if (remove_pid)
goto out;
continue;
}
- ret = disk_space_low();
- if (ret < 0)
- goto out;
- low_disk_space = ret;
- ret = try_to_free_disk_space(low_disk_space);
+ ret = try_to_free_disk_space();
if (ret < 0)
goto out;
if (snapshot_removal_status != HS_READY) {
{
int ret;
struct snapshot_list sl;
+ struct snapshot *victim;
struct disk_space ds;
+ const char *why;
ret = get_disk_space(".", &ds);
if (ret < 0)
return ret;
log_disk_space(&ds);
dss_get_snapshot_list(&sl);
- ret = remove_outdated_snapshot(&sl);
- if (ret < 0)
- goto out;
- if (ret > 0)
+ why = "outdated";
+ victim = find_outdated_snapshot(&sl);
+ if (victim)
goto rm;
- ret = remove_redundant_snapshot(&sl);
- if (ret < 0)
- goto out;
- if (ret > 0)
+ why = "redundant";
+ victim = find_redundant_snapshot(&sl);
+ if (victim)
goto rm;
ret = 0;
goto out;
rm:
+ if (conf.dry_run_given) {
+ dss_msg("%s snapshot %s (interval = %i)\n",
+ why, victim->name, victim->interval);
+ ret = 0;
+ goto out;
+ }
+ ret = pre_remove_hook(victim, why);
+ if (ret < 0)
+ goto out;
if (snapshot_removal_status == HS_PRE_RUNNING) {
ret = wait_for_remove_process();
if (ret < 0)