X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=dss.c;h=bb71bc4cef5a86033d94422e0d6cbf32043bb9df;hb=360bcc95d588232d8c81e028464d9e23b356a193;hp=c0597f3af32e4586d6fbb98f78aea9b59f338aff;hpb=723146898de138324dd6e421af05fd61c6cda848;p=dss.git diff --git a/dss.c b/dss.c index c0597f3..bb71bc4 100644 --- a/dss.c +++ b/dss.c @@ -185,8 +185,8 @@ static int next_snapshot_is_due(void) goto out; } - tv_divide(wanted, &diff, &tmp); /* sleep time betweeen two snapshots */ - diff.tv_sec = s->completion_time; /* completion time of the the latest snaphot */ + tv_divide(wanted, &diff, &tmp); /* sleep time between two snapshots */ + diff.tv_sec = s->completion_time; /* completion time of the latest snapshot */ diff.tv_usec = 0; tv_add(&diff, &tmp, &next_snapshot_time); ret = (tv_diff(&now, &next_snapshot_time, &diff) < 0)? 0 : 1; @@ -218,11 +218,13 @@ static int pre_create_hook(void) 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); @@ -277,12 +279,46 @@ static int snapshot_is_being_created(struct snapshot *s) 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; +} + /* * 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; @@ -329,20 +365,14 @@ static int remove_redundant_snapshot(struct snapshot_list *sl) 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", @@ -352,29 +382,21 @@ static int remove_outdated_snapshot(struct snapshot_list *sl) continue; if (s->interval <= conf.num_intervals_arg) continue; - if (conf.dry_run_given) { - dss_msg("%s would be removed (interval = %i)\n", - s->name, s->interval); - 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) { struct snapshot *s = get_oldest_snapshot(sl); if (!s) /* no snapshot found */ - return 0; + return NULL; DSS_INFO_LOG("oldest snapshot: %s\n", s->name); if (snapshot_is_being_created(s)) - return 0; - return pre_remove_hook(s, "oldest"); + return NULL; + return s; } static int rename_incomplete_snapshot(int64_t start) @@ -400,7 +422,9 @@ static int try_to_free_disk_space(int low_disk_space) { int ret; struct snapshot_list sl; + struct snapshot *victim; struct timeval now; + const char *why; gettimeofday(&now, NULL); if (tv_diff(&next_removal_check, &now, NULL) > 0) @@ -408,22 +432,31 @@ static int try_to_free_disk_space(int low_disk_space) 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; + 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 */ 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) + why = "orphaned"; + victim = find_orphaned_snapshot(&sl); + if (victim) goto out; + DSS_WARNING_LOG("disk space low and nothing obvious to remove\n"); + victim = find_oldest_removable_snapshot(&sl); + if (victim) + goto remove; DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n"); ret = -ERRNO_TO_DSS_ERROR(ENOSPC); + goto out; +remove: + ret = pre_remove_hook(victim, why); out: free_snapshot_list(&sl); return ret; @@ -637,7 +670,12 @@ static int handle_rsync_exit(int status) goto out; } es = WEXITSTATUS(status); - if (es == 13) { /* Errors with program diagnostics */ + /* + * Restart rsync on non-fatal errors: + * 12: Error in rsync protocol data stream + * 13: Errors with program diagnostics + */ + if (es == 12 || es == 13) { DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n", (int)create_pid, es); snapshot_creation_status = HS_NEEDS_RESTART; @@ -1059,26 +1097,35 @@ static int com_prune(void) { 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)