X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=dss.c;h=40eccab2b993ccab5c2f83f85fe6e06c664c40c1;hb=04bcb8e95d4afd843bcbb98cb01eb6c19a990446;hp=cee49d68885d88abc8af991647905fd6c91f354d;hpb=d75434361fceedb4367a706e32442dd3a03a579c;p=dss.git diff --git a/dss.c b/dss.c index cee49d6..40eccab 100644 --- a/dss.c +++ b/dss.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Andre Noll + * Copyright (C) 2008-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -52,10 +52,11 @@ static struct timeval next_snapshot_time; 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. */ @@ -185,8 +186,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 +219,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 +280,53 @@ 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; +} + +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; @@ -305,6 +349,8 @@ static int remove_redundant_snapshot(struct snapshot_list *sl) 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; @@ -329,20 +375,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", @@ -350,31 +390,28 @@ static int remove_outdated_snapshot(struct snapshot_list *sl) 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) { - 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"); + 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) @@ -400,7 +437,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 +447,33 @@ 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; 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; @@ -568,7 +618,10 @@ static int handle_rm_exit(int status) snapshot_removal_status = HS_READY; return -E_BAD_EXIT_CODE; } - snapshot_removal_status = HS_SUCCESS; + if (conf.post_remove_hook_given) + snapshot_removal_status = HS_SUCCESS; + else + snapshot_removal_status = HS_READY; return 1; } @@ -634,7 +687,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; @@ -653,6 +711,8 @@ static int handle_rsync_exit(int status) 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; @@ -677,6 +737,8 @@ static int handle_pre_create_hook_exit(int status) 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; @@ -873,12 +935,13 @@ static int use_rsync_locally(char *logname) 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 *)); @@ -887,12 +950,12 @@ static void create_rsync_argv(char ***argv, int64_t *num) (*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); @@ -1056,26 +1119,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)