package PVE::AbstractConfig;

use strict;
use warnings;

use PVE::Tools qw(lock_file lock_file_full);
use PVE::INotify;
use PVE::Cluster;
use PVE::Storage;

use PVE::GuestHelpers qw(typesafe_ne);
use PVE::ReplicationConfig;
use PVE::Replication;

my $nodename = PVE::INotify::nodename();

# Printable string, currently either "VM" or "CT"
sub guest_type {
    my ($class) = @_;
    die "abstract method - implement me ";
}

sub __config_max_unused_disks {
    my ($class) = @_;

    die "implement me - abstract method\n";
}

# Path to the flock file for this VM/CT
sub config_file_lock {
    my ($class, $vmid) = @_;
    die "abstract method - implement me";
}

# Relative config file path for this VM/CT in CFS
sub cfs_config_path {
    my ($class, $vmid, $node) = @_;
    die "abstract method - implement me";
}

# Absolute config file path for this VM/CT
sub config_file {
    my ($class, $vmid, $node) = @_;

    my $cfspath = $class->cfs_config_path($vmid, $node);
    return "/etc/pve/$cfspath";
}

# Read and parse config file for this VM/CT
sub load_config {
    my ($class, $vmid, $node) = @_;

    $node = $nodename if !$node;
    my $cfspath = $class->cfs_config_path($vmid, $node);

    my $conf = PVE::Cluster::cfs_read_file($cfspath);
    die "Configuration file '$cfspath' does not exist\n"
	if !defined($conf);

    return $conf;
}

# Generate and write config file for this VM/CT
sub write_config {
    my ($class, $vmid, $conf) = @_;

    my $cfspath = $class->cfs_config_path($vmid);

    PVE::Cluster::cfs_write_file($cfspath, $conf);
}

# Pending changes related

sub parse_pending_delete {
    my ($class, $data) = @_;

    return {} if !$data;

    $data =~ s/[,;]/ /g;
    $data =~ s/^\s+//;

    my $pending_deletions = {};
    for my $entry (split(/\s+/, $data)) {
	my ($force, $key) = $entry =~ /^(!?)(.*)$/;

	$pending_deletions->{$key} = {
	    force => $force ? 1 : 0,
	};
    }

    return $pending_deletions;
}

sub print_pending_delete {
    my ($class, $delete_hash) = @_;

    my $render_key = sub {
	my $key = shift;
	$key = "!$key" if $delete_hash->{$key}->{force};
	return $key;
    };

    join (',', map { $render_key->($_) } sort keys %$delete_hash);
}

sub add_to_pending_delete {
    my ($class, $conf, $key, $force) = @_;

    $conf->{pending} //= {};
    my $pending = $conf->{pending};
    my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});

    $pending_delete_hash->{$key} = { force => $force };

    $pending->{delete} = $class->print_pending_delete($pending_delete_hash);

    return $conf;
}

sub remove_from_pending_delete {
    my ($class, $conf, $key) = @_;

    my $pending = $conf->{pending};
    my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});

    return $conf if ! exists $pending_delete_hash->{$key};

    delete $pending_delete_hash->{$key};

    if (%$pending_delete_hash) {
	$pending->{delete} = $class->print_pending_delete($pending_delete_hash);
    } else {
	delete $pending->{delete};
    }

    return $conf;
}

sub cleanup_pending {
    my ($class, $conf) = @_;

    my $pending = $conf->{pending};
    # remove pending changes when nothing changed
    my $changes;
    foreach my $opt (keys %{$conf->{pending}}) {
	next if $opt eq 'delete'; # just to be sure
	if (defined($conf->{$opt}) && ($pending->{$opt} eq $conf->{$opt})) {
	    $changes = 1;
	    delete $pending->{$opt};
	}
    }

    my $current_delete_hash = $class->parse_pending_delete($pending->{delete});
    my $pending_delete_hash = {};
    for my $opt (keys %$current_delete_hash) {
	if (defined($conf->{$opt})) {
	    $pending_delete_hash->{$opt} = $current_delete_hash->{$opt};
	} else {
	    $changes = 1;
	}
    }

    if (%$pending_delete_hash) {
	$pending->{delete} = $class->print_pending_delete($pending_delete_hash);
    } else {
	delete $pending->{delete};
    }

    return $changes;
}

sub get_partial_fast_plug_option {
    my ($class) = @_;

    die "abstract method - implement me ";
}

sub partial_fast_plug {
    my ($class, $conf, $opt) = @_;

    my $partial_fast_plug_option = $class->get_partial_fast_plug_option();
    return 0 if !$partial_fast_plug_option->{$opt};

    my $format = $partial_fast_plug_option->{$opt}->{fmt};
    my $fast_pluggable = $partial_fast_plug_option->{$opt}->{properties};

    my $configured = {};
    if (exists($conf->{$opt})) {
	$configured = PVE::JSONSchema::parse_property_string($format, $conf->{$opt});
    }
    my $pending = PVE::JSONSchema::parse_property_string($format, $conf->{pending}->{$opt});

    my $changes = 0;

    # merge configured and pending opts to iterate
    my @all_keys = keys %{{ %$pending, %$configured }};

    foreach my $subopt (@all_keys) {
	my $type = $format->{$subopt}->{type};
	if (typesafe_ne($configured->{$subopt}, $pending->{$subopt}, $type)) {
	    if ($fast_pluggable->{$subopt}) {
		$configured->{$subopt} = $pending->{$subopt};
		$changes = 1
	    }
	}
    }

    # if there're no keys in $configured (after merge) there shouldn't be anything to change
    if (keys %$configured) {
	$conf->{$opt} = PVE::JSONSchema::print_property_string($configured, $format);
    }

    return $changes;
}


sub load_snapshot_config {
    my ($class, $vmid, $snapname) = @_;

    my $conf = $class->load_config($vmid);

    my $snapshot = $conf->{snapshots}->{$snapname};
    die "snapshot '$snapname' does not exist\n" if !defined($snapshot);

    $snapshot->{digest} = $conf->{digest};

    return $snapshot;

}

sub load_current_config {
    my ($class, $vmid, $current) = @_;

    my $conf = $class->load_config($vmid);

    # take pending changes in
    if (!$current) {
	foreach my $opt (keys %{$conf->{pending}}) {
	    next if $opt eq 'delete';
	    my $value = $conf->{pending}->{$opt};
	    next if ref($value); # just to be sure
	    $conf->{$opt} = $value;
	}
	my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
	foreach my $opt (keys %$pending_delete_hash) {
	    delete $conf->{$opt} if $conf->{$opt};
	}
    }

    delete $conf->{snapshots};
    delete $conf->{pending};

    return $conf;
}


# Lock config file using flock, run $code with @param, unlock config file.
# $timeout is the maximum time to acquire the flock
sub lock_config_full {
    my ($class, $vmid, $timeout, $code, @param) = @_;

    my $filename = $class->config_file_lock($vmid);

    my $res = lock_file($filename, $timeout, $code, @param);

    die $@ if $@;

    return $res;
}

sub create_and_lock_config {
    my ($class, $vmid, $allow_existing, $lock) = @_;

    $class->lock_config_full($vmid, 5, sub {
	PVE::Cluster::check_vmid_unused($vmid, $allow_existing);

	my $conf = eval { $class->load_config($vmid) } || {};
	$class->check_lock($conf);
	$conf->{lock} = $lock // 'create';
	$class->write_config($vmid, $conf);
    });
}

# destroys configuration, only applicable for configs owned by the callers node.
# dies if removal fails, e.g., when inquorate.
sub destroy_config {
    my ($class, $vmid) = @_;

    my $config_fn = $class->config_file($vmid, $nodename);
    unlink $config_fn or die "failed to remove config file: $!\n";
}

# Lock config file using flock, run $code with @param, unlock config file.
# $timeout is the maximum time to acquire the flock
# $shared eq 1 creates a non-exclusive ("read") flock
sub lock_config_mode {
    my ($class, $vmid, $timeout, $shared, $code, @param) = @_;

    my $filename = $class->config_file_lock($vmid);

    my $res = lock_file_full($filename, $timeout, $shared, $code, @param);

    die $@ if $@;

    return $res;
}

# Lock config file using flock, run $code with @param, unlock config file.
sub lock_config {
    my ($class, $vmid, $code, @param) = @_;

    return $class->lock_config_full($vmid, 10, $code, @param);
}

# Checks whether the config is locked with the lock parameter
sub check_lock {
    my ($class, $conf) = @_;

    die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
}

# Returns whether the config is locked with the lock parameter, also checks
# whether the lock value is correct if the optional $lock is set.
sub has_lock {
    my ($class, $conf, $lock) = @_;

    return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
}

# Sets the lock parameter for this VM/CT's config to $lock.
sub set_lock {
    my ($class, $vmid, $lock) = @_;

    my $conf;
    $class->lock_config($vmid, sub {
	$conf = $class->load_config($vmid);
	$class->check_lock($conf);
	$conf->{lock} = $lock;
	$class->write_config($vmid, $conf);
    });
    return $conf;
}

# Removes the lock parameter for this VM/CT's config, also checks whether
# the lock value is correct if the optional $lock is set.
sub remove_lock {
    my ($class, $vmid, $lock) = @_;

    $class->lock_config($vmid, sub {
	my $conf = $class->load_config($vmid);
	if (!$conf->{lock}) {
	    my $lockstring = defined($lock) ? "'$lock' " : "any";
	    die "no lock found trying to remove $lockstring lock\n";
	} elsif (defined($lock) && $conf->{lock} ne $lock) {
	    die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
	}
	delete $conf->{lock};
	$class->write_config($vmid, $conf);
    });
}

# Checks whether protection mode is enabled for this VM/CT.
sub check_protection {
    my ($class, $conf, $err_msg) = @_;

    if ($conf->{protection}) {
	die "$err_msg - protection mode enabled\n";
    }
}

# Adds an unused volume to $config, if possible.
sub add_unused_volume {
    my ($class, $config, $volid) = @_;

    my $key;
    for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
	my $test = "unused$ind";
	if (my $vid = $config->{$test}) {
	    return if $vid eq $volid; # do not add duplicates
	} else {
	    $key = $test;
	}
    }

    die "Too many unused volumes - please delete them first.\n" if !$key;

    $config->{$key} = $volid;

    return $key;
}

# Returns whether the template parameter is set in $conf.
sub is_template {
    my ($class, $conf) = @_;

    return 1 if defined $conf->{template} && $conf->{template} == 1;
}

# Checks whether $feature is available for the referenced volumes in $conf.
# Note: depending on the parameters, some volumes may be skipped!
sub has_feature {
    my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
    die "implement me - abstract method\n";
}

# get all replicatable volume (hash $res->{$volid} = 1)
# $cleanup: for cleanup - simply ignores volumes without replicate feature
# $norerr: never raise exceptions - return undef instead
sub get_replicatable_volumes {
    my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;

    die "implement me - abstract method\n";
}

# Returns all the guests volumes which would be included in a vzdump job
# Return Format (array-ref with hash-refs as elements):
# [
#   {
#     key,        key in the config, e.g. mp0, scsi0,...
#     included,   boolean
#     reason,     string
#     data        volume object as returned from foreach_drive/foreach_mountpoint
#   },
# ]
sub get_backup_volumes {
    my ($class, $conf) = @_;

    die "implement me - abstract method\n";
}

# Internal snapshots

# NOTE: Snapshot create/delete involves several non-atomic
# actions, and can take a long time.
# So we try to avoid locking the file and use the 'lock' variable
# inside the config file instead.

# Save the vmstate (RAM).
sub __snapshot_save_vmstate {
    my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
    die "implement me - abstract method\n";
}

# Check whether the VM/CT is running.
sub __snapshot_check_running {
    my ($class, $vmid) = @_;
    die "implement me - abstract method\n";
}

# Check whether we need to freeze the VM/CT
sub __snapshot_check_freeze_needed {
    my ($sself, $vmid, $config, $save_vmstate) = @_;
    die "implement me - abstract method\n";
}

# Freeze or unfreeze the VM/CT.
sub __snapshot_freeze {
    my ($class, $vmid, $unfreeze) = @_;

    die "abstract method - implement me\n";
}

# Code run before and after creating all the volume snapshots
# base: noop
sub __snapshot_create_vol_snapshots_hook {
    my ($class, $vmid, $snap, $running, $hook) = @_;

    return;
}

# Create the volume snapshots for the VM/CT.
sub __snapshot_create_vol_snapshot {
    my ($class, $vmid, $vs, $volume, $snapname) = @_;

    die "abstract method - implement me\n";
}

# Remove a drive from the snapshot config.
sub __snapshot_delete_remove_drive {
    my ($class, $snap, $drive) = @_;

    die "abstract method - implement me\n";
}

# Delete the vmstate file/drive
sub __snapshot_delete_vmstate_file {
    my ($class, $snap, $force) = @_;

    die "abstract method - implement me\n";
}

# Delete a volume snapshot
sub __snapshot_delete_vol_snapshot {
    my ($class, $vmid, $vs, $volume, $snapname) = @_;

    die "abstract method - implement me\n";
}

# called during rollback prepare, and between config rollback and starting guest
# can change config, e.g. for vmgenid
# $data is shared across calls and passed to vm_start
sub __snapshot_rollback_hook {
    my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;

    return;
}

# Checks whether a volume snapshot is possible for this volume.
sub __snapshot_rollback_vol_possible {
    my ($class, $volume, $snapname) = @_;

    die "abstract method - implement me\n";
}

# Rolls back this volume.
sub __snapshot_rollback_vol_rollback {
    my ($class, $volume, $snapname) = @_;

    die "abstract method - implement me\n";
}

# Stops the VM/CT for a rollback.
sub __snapshot_rollback_vm_stop {
    my ($class, $vmid) = @_;

    die "abstract method - implement me\n";
}

# Start the VM/CT after a rollback with restored vmstate.
sub __snapshot_rollback_vm_start {
    my ($class, $vmid, $vmstate, $data);

    die "abstract method - implement me\n";
}

# Get list of volume IDs which are referenced in $conf, but not in $snap.
sub __snapshot_rollback_get_unused {
    my ($class, $conf, $snap) = @_;

    die "abstract method - implement me\n";
}

# Iterate over all configured volumes, calling $func for each key/value pair.
sub __snapshot_foreach_volume {
    my ($class, $conf, $func) = @_;

    die "abstract method - implement me\n";
}

# Copy the current config $source to the snapshot config $dest
sub __snapshot_copy_config {
    my ($class, $source, $dest) = @_;

    foreach my $k (keys %$source) {
	next if $k eq 'snapshots';
	next if $k eq 'snapstate';
	next if $k eq 'snaptime';
	next if $k eq 'vmstate';
	next if $k eq 'lock';
	next if $k eq 'digest';
	next if $k eq 'description';
	next if $k =~ m/^unused\d+$/;

	$dest->{$k} = $source->{$k};
    }
};

# Apply the snapshot config $snap to the config $conf (rollback)
sub __snapshot_apply_config {
    my ($class, $conf, $snap) = @_;

    # copy snapshot list
    my $newconf = {
	snapshots => $conf->{snapshots},
    };

    # keep description and list of unused disks
    foreach my $k (keys %$conf) {
	next if !($k =~ m/^unused\d+$/ || $k eq 'description');

	$newconf->{$k} = $conf->{$k};
    }

    $class->__snapshot_copy_config($snap, $newconf);

    return $newconf;
}

# Prepares the configuration for snapshotting.
sub __snapshot_prepare {
    my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;

    my $snap;

    my $updatefn =  sub {

	my $conf = $class->load_config($vmid);

	die "you can't take a snapshot if it's a template\n"
	    if $class->is_template($conf);

	$class->check_lock($conf);

	$conf->{lock} = 'snapshot';

	die "snapshot name '$snapname' already used\n"
	    if defined($conf->{snapshots}->{$snapname});

	my $storecfg = PVE::Storage::config();
	die "snapshot feature is not available\n"
	    if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');

	$snap = $conf->{snapshots}->{$snapname} = {};

	if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
	    $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
	}

	$class->__snapshot_copy_config($conf, $snap);

	$snap->{snapstate} = "prepare";
	$snap->{snaptime} = time();
	$snap->{description} = $comment if $comment;

	$class->write_config($vmid, $conf);
    };

    $class->lock_config($vmid, $updatefn);

    return $snap;
}

# Commits the configuration after snapshotting.
sub __snapshot_commit {
    my ($class, $vmid, $snapname) = @_;

    my $updatefn = sub {

	my $conf = $class->load_config($vmid);

	die "missing snapshot lock\n"
	    if !($conf->{lock} && $conf->{lock} eq 'snapshot');

	my $snap = $conf->{snapshots}->{$snapname};
	die "snapshot '$snapname' does not exist\n" if !defined($snap);

	die "wrong snapshot state\n"
	    if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");

	delete $snap->{snapstate};
	delete $conf->{lock};

	$conf->{parent} = $snapname;

	$class->write_config($vmid, $conf);
    };

    $class->lock_config($vmid, $updatefn);
}

# Creates a snapshot for the VM/CT.
sub snapshot_create {
    my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;

    my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);

    $save_vmstate = 0 if !$snap->{vmstate};

    my $conf = $class->load_config($vmid);

    my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});

    my $drivehash = {};

    eval {
	if ($freezefs) {
	    $class->__snapshot_freeze($vmid, 0);
	}

	$class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");

	$class->__snapshot_foreach_volume($snap, sub {
	    my ($vs, $volume) = @_;

	    $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
	    $drivehash->{$vs} = 1;
	});
    };
    my $err = $@;

    if ($running) {
	$class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
	if ($freezefs) {
	    $class->__snapshot_freeze($vmid, 1);
	}
	$class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
    }

    if ($err) {
	warn "snapshot create failed: starting cleanup\n";
	eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
	warn "$@" if $@;
	die "$err\n";
    }

    $class->__snapshot_commit($vmid, $snapname);
}

# Deletes a snapshot.
# Note: $drivehash is only set when called from snapshot_create.
sub snapshot_delete {
    my ($class, $vmid, $snapname, $force, $drivehash) = @_;

    my $prepare = 1;

    my $unused = [];

    my $conf = $class->load_config($vmid);
    my $snap = $conf->{snapshots}->{$snapname};

    die "snapshot '$snapname' does not exist\n" if !defined($snap);

    $class->set_lock($vmid, 'snapshot-delete')
	if (!$drivehash); # doesn't already have a 'snapshot' lock

    my $unlink_parent = sub {
	my ($confref, $new_parent) = @_;

	if ($confref->{parent} && $confref->{parent} eq $snapname) {
	    if ($new_parent) {
		$confref->{parent} = $new_parent;
	    } else {
		delete $confref->{parent};
	    }
	}
    };

    my $remove_drive = sub {
	my ($drive) = @_;

	my $conf = $class->load_config($vmid);
	$snap = $conf->{snapshots}->{$snapname};
	die "snapshot '$snapname' does not exist\n" if !defined($snap);

	$class->__snapshot_delete_remove_drive($snap, $drive);

	$class->write_config($vmid, $conf);
    };

    #prepare
    $class->lock_config($vmid, sub {
	my $conf = $class->load_config($vmid);

	die "you can't delete a snapshot if vm is a template\n"
	    if $class->is_template($conf);

	$snap = $conf->{snapshots}->{$snapname};
	die "snapshot '$snapname' does not exist\n" if !defined($snap);

	$snap->{snapstate} = 'delete';

	$class->write_config($vmid, $conf);
    });

    # now remove vmstate file
    if ($snap->{vmstate}) {
	$class->__snapshot_delete_vmstate_file($snap, $force);

	# save changes (remove vmstate from snapshot)
	$class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
    };

    # now remove all volume snapshots
    $class->__snapshot_foreach_volume($snap, sub {
	my ($vs, $volume) = @_;

	return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
	if (!$drivehash || $drivehash->{$vs}) {
	    eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
	    if (my $err = $@) {
		die $err if !$force;
		warn $err;
	    }
	}

	# save changes (remove drive from snapshot)
	$class->lock_config($vmid, $remove_drive, $vs) if !$force;
    });

    # now cleanup config
    $class->lock_config($vmid, sub {
	my $conf = $class->load_config($vmid);
	$snap = $conf->{snapshots}->{$snapname};
	die "snapshot '$snapname' does not exist\n" if !defined($snap);

	# remove parent refs
	&$unlink_parent($conf, $snap->{parent});
	foreach my $sn (keys %{$conf->{snapshots}}) {
	    next if $sn eq $snapname;
	    &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
	}


	delete $conf->{snapshots}->{$snapname};
	delete $conf->{lock};
	foreach my $volid (@$unused) {
	    $class->add_unused_volume($conf, $volid);
	}

	$class->write_config($vmid, $conf);
    });
}

# Rolls back to a given snapshot.
sub snapshot_rollback {
    my ($class, $vmid, $snapname) = @_;

    my $prepare = 1;

    my $storecfg = PVE::Storage::config();

    my $conf = $class->load_config($vmid);

    my $get_snapshot_config = sub {

	die "you can't rollback if vm is a template\n" if $class->is_template($conf);

	my $res = $conf->{snapshots}->{$snapname};

	die "snapshot '$snapname' does not exist\n" if !defined($res);

	return $res;
    };

    my $repl_conf = PVE::ReplicationConfig->new();
    if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
	# remove all replication snapshots
	my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
	my $sorted_volids = [ sort keys %$volumes ];

	# remove all local replication snapshots (jobid => undef)
	my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
	PVE::Replication::prepare($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
    }
    
    my $snap = &$get_snapshot_config();

    $class->__snapshot_foreach_volume($snap, sub {
	my ($vs, $volume) = @_;

	$class->__snapshot_rollback_vol_possible($volume, $snapname);
    });

    my $data = {};

    my $updatefn = sub {

	$conf = $class->load_config($vmid);

	$snap = &$get_snapshot_config();

	die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
	    if $snap->{snapstate};

	if ($prepare) {
	    $class->check_lock($conf);
	    $class->__snapshot_rollback_vm_stop($vmid);
	}

	die "unable to rollback vm $vmid: vm is running\n"
	    if $class->__snapshot_check_running($vmid);

	if ($prepare) {
	    $conf->{lock} = 'rollback';
	} else {
	    die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
	    delete $conf->{lock};

	    my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);

	    foreach my $volid (@$unused) {
		$class->add_unused_volume($conf, $volid);
	    }

	    # copy snapshot config to current config
	    $conf = $class->__snapshot_apply_config($conf, $snap);
	    $conf->{parent} = $snapname;
	}

	$class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);

	$class->write_config($vmid, $conf);

	if (!$prepare && $snap->{vmstate}) {
	    $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data);
	}
    };

    $class->lock_config($vmid, $updatefn);

    $class->__snapshot_foreach_volume($snap, sub {
	my ($vs, $volume) = @_;

	$class->__snapshot_rollback_vol_rollback($volume, $snapname);
    });

    $prepare = 0;
    $class->lock_config($vmid, $updatefn);
}

# bash completion helper

sub snapshot_list {
    my ($class, $vmid) = @_;

    my $snapshot = eval {
	my $conf = $class->load_config($vmid);
	my $snapshots = $conf->{snapshots} || [];
	[ sort keys %$snapshots ]
    } || [];

    return $snapshot;
}

1;
