]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPlugin.pm
zfs: make zfs_request a virtual method
[pve-storage.git] / PVE / Storage / ZFSPlugin.pm
index a50f7cb703d3ab91fd90053387bad06ab9eca210..afa54153295a813ba5c5710300a58fb828f355f6 100644 (file)
@@ -5,286 +5,158 @@ use warnings;
 use IO::File;
 use POSIX;
 use PVE::Tools qw(run_command);
-use PVE::Storage::Plugin;
-use Digest::MD5 qw(md5_hex);
+use PVE::Storage::ZFSDirPlugin;
 
-use base qw(PVE::Storage::Plugin);
+use base qw(PVE::Storage::ZFSDirPlugin);
+use PVE::Storage::LunCmd::Comstar;
+use PVE::Storage::LunCmd::Istgt;
+use PVE::Storage::LunCmd::Iet;
 
 my @ssh_opts = ('-o', 'BatchMode=yes');
 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
+my $id_rsa_path = '/etc/pve/priv/zfs';
+
+my $lun_cmds = {
+    create_lu   => 1,
+    delete_lu   => 1,
+    import_lu   => 1,
+    modify_lu   => 1,
+    add_view    => 1,
+    list_view   => 1,
+    list_lu     => 1,
+};
 
-sub zfs_request {
-    my ($scfg, $timeout, $method, @params) = @_;
+my $zfs_unknown_scsi_provider = sub {
+    my ($provider) = @_;
 
-    my $cmdmap;
-    my $zfscmd;
-    my $target;
+    die "$provider: unknown iscsi provider. Available [comstar, istgt, iet]";
+};
 
-    $timeout = 5 if !$timeout;
+my $zfs_get_base = sub {
+    my ($scfg) = @_;
 
     if ($scfg->{iscsiprovider} eq 'comstar') {
-       my $stmfadmcmd = "/usr/sbin/stmfadm";
-       my $sbdadmcmd = "/usr/sbin/sbdadm";
-
-       $cmdmap = {
-           create_lu   => { cmd => $stmfadmcmd, method => 'create-lu' },
-           delete_lu   => { cmd => $stmfadmcmd, method => 'delete-lu' },
-           import_lu   => { cmd => $stmfadmcmd, method => 'import-lu' },
-           modify_lu   => { cmd => $stmfadmcmd, method => 'modify-lu' },
-           add_view    => { cmd => $stmfadmcmd, method => 'add-view' },
-           list_view   => { cmd => $stmfadmcmd, method => 'list-view' },
-           list_lu     => { cmd => $sbdadmcmd, method => 'list-lu' },
-           zpool_list  => { cmd => 'zpool', method => 'list' },
-       };
-    } else {
-       die 'unknown iscsi provider. Available [comstar]';
-    }
-
-    if ($cmdmap->{$method}) {
-       $zfscmd = $cmdmap->{$method}->{cmd};
-       $method = $cmdmap->{$method}->{method};
+        return PVE::Storage::LunCmd::Comstar::get_base;
+    } elsif ($scfg->{iscsiprovider} eq 'istgt') {
+        return PVE::Storage::LunCmd::Istgt::get_base;
+    } elsif ($scfg->{iscsiprovider} eq 'iet') {
+        return PVE::Storage::LunCmd::Iet::get_base;
     } else {
-       $zfscmd = 'zfs';
+        $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
     }
+};
 
-    $target = 'root@' . $scfg->{portal};
-
-    my $cmd = [@ssh_cmd, $target, $zfscmd, $method, @params];
-
-    my $msg = '';
-
-    my $output = sub {
-       my $line = shift;
-       $msg .= "$line\n";
-    };
+sub zfs_request {
+    my ($class, $scfg, $timeout, $method, @params) = @_;
 
-    run_command($cmd, outfunc => $output, timeout => $timeout);
+    my $cmdmap;
+    my $zfscmd;
+    my $target;
+    my $msg;
 
-    return $msg;
-}
+    $timeout = 5 if !$timeout;
 
-sub zfs_parse_size {
-    my ($text) = @_;
-
-    return 0 if !$text;
-
-    if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
-       my ($size, $reminder, $unit) = ($1, $2, $3);
-       return $size if !$unit;
-       if ($unit eq 'K') {
-           $size *= 1024;
-       } elsif ($unit eq 'M') {
-           $size *= 1024*1024;
-       } elsif ($unit eq 'G') {
-           $size *= 1024*1024*1024;
-       } elsif ($unit eq 'T') {
-           $size *= 1024*1024*1024*1024;
-       }
-
-       if ($reminder) {
-           $size = ceil($size);
-       }
-       return $size;
+    if ($lun_cmds->{$method}) {
+        if ($scfg->{iscsiprovider} eq 'comstar') {
+            $msg = PVE::Storage::LunCmd::Comstar::run_lun_command($scfg, $timeout, $method, @params);
+        } elsif ($scfg->{iscsiprovider} eq 'istgt') {
+            $msg = PVE::Storage::LunCmd::Istgt::run_lun_command($scfg, $timeout, $method, @params);
+        } elsif ($scfg->{iscsiprovider} eq 'iet') {
+            $msg = PVE::Storage::LunCmd::Iet::run_lun_command($scfg, $timeout, $method, @params);
+        } else {
+            $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
+        }
     } else {
-       return 0;
-    }
-}
-
-sub zfs_get_pool_stats {
-    my ($scfg) = @_;
-
-    my $available = 0;
-    my $used = 0;
-
-    my $text = zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
-                          'available,used', $scfg->{pool});
+        if ($method eq 'zpool_list') {
+            $zfscmd = 'zpool';
+            $method = 'list',
+        } else {
+            $zfscmd = 'zfs';
+        }
 
-    my @lines = split /\n/, $text;
+        $target = 'root@' . $scfg->{portal};
 
-    if($lines[0] =~ /^(\d+)$/) {
-       $available = $1;
-    }
+        my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, $zfscmd, $method, @params];
 
-    if($lines[1] =~ /^(\d+)$/) {
-       $used = $1;
-    }
+        $msg = '';
 
-    return ($available, $used);
-}
+        my $output = sub {
+        my $line = shift;
+        $msg .= "$line\n";
+        };
 
-sub zfs_parse_zvol_list {
-    my ($text) = @_;
-
-    my $list = ();
-
-    return $list if !$text;
-
-    my @lines = split /\n/, $text;
-    foreach my $line (@lines) {
-       if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
-           my $zvol = {};
-           my $name;
-           my $disk;
-           my @zvols = split /\//, $1;
-           my $pool = $zvols[0];
-
-           if (scalar(@zvols) == 2 && $zvols[0] !~ /^rpool$/) {
-               $disk = $zvols[1];
-               next unless $disk =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
-               $name = $pool . '/' . $disk;
-           } else {
-               next;
-           }
-
-           $zvol->{name} = $name;
-           $zvol->{size} = zfs_parse_size($2);
-           if ($3 !~ /^-$/) {
-               $zvol->{origin} = $3;
-           }
-           push @$list, $zvol;
-       }
+        run_command($cmd, outfunc => $output, timeout => $timeout);
     }
 
-    return $list;
+    return $msg;
 }
 
 sub zfs_get_lu_name {
-    my ($scfg, $zvol) = @_;
+    my ($class, $scfg, $zvol) = @_;
     my $object;
 
+    my $base = $zfs_get_base->($scfg);
     if ($zvol =~ /^.+\/.+/) {
-        $object = "/dev/zvol/rdsk/$zvol";
+        $object = "$base/$zvol";
     } else {
-        $object = "/dev/zvol/rdsk/$scfg->{pool}/$zvol";
+        $object = "$base/$scfg->{pool}/$zvol";
     }
 
-    my $text = zfs_request($scfg, undef, 'list_lu');
-    my @lines = split /\n/, $text;
-    foreach my $line (@lines) {
-       return $1 if ($line =~ /(\w+)\s+\d+\s+$object$/);
-    }
-    die "Could not find lu_name for zvol $zvol";
-}
+    my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object);
 
-sub zfs_get_zvol_size {
-    my ($scfg, $zvol) = @_;
+    return $lu_name if $lu_name;
 
-    my $text = zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
-
-    if($text =~ /volsize\s(\d+)/){
-       return $1;
-    }
-
-    die "Could not get zvol size";
+    die "Could not find lu_name for zvol $zvol";
 }
 
 sub zfs_add_lun_mapping_entry {
-    my ($scfg, $zvol, $guid) = @_;
+    my ($class, $scfg, $zvol, $guid) = @_;
 
     if (! defined($guid)) {
-       $guid = zfs_get_lu_name($scfg, $zvol);
+    $guid = $class->zfs_get_lu_name($scfg, $zvol);
     }
 
-    zfs_request($scfg, undef, 'add_view', $guid);
+    $class->zfs_request($scfg, undef, 'add_view', $guid);
 }
 
 sub zfs_delete_lu {
-    my ($scfg, $zvol) = @_;
+    my ($class, $scfg, $zvol) = @_;
 
-    my $guid = zfs_get_lu_name($scfg, $zvol);
+    my $guid = $class->zfs_get_lu_name($scfg, $zvol);
 
-    zfs_request($scfg, undef, 'delete_lu', $guid);
+    $class->zfs_request($scfg, undef, 'delete_lu', $guid);
 }
 
 sub zfs_create_lu {
-    my ($scfg, $zvol) = @_;
-
-    my $prefix = '600144f';
-    my $digest = md5_hex($zvol);
-    $digest =~ /(\w{7}(.*))/;
-    my $guid = "$prefix$2";
+    my ($class, $scfg, $zvol) = @_;
 
-    zfs_request($scfg, undef, 'create_lu', '-p', 'wcd=false', '-p', "guid=$guid", "/dev/zvol/rdsk/$scfg->{pool}/$zvol");
+    my $base = $zfs_get_base->($scfg);
+    my $guid = $class->zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol");
 
     return $guid;
 }
 
 sub zfs_import_lu {
-    my ($scfg, $zvol) = @_;
+    my ($class, $scfg, $zvol) = @_;
 
-    zfs_request($scfg, undef, 'import_lu', "/dev/zvol/rdsk/$scfg->{pool}/$zvol");
+    my $base = $zfs_get_base->($scfg);
+    $class->zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol");
 }
 
 sub zfs_resize_lu {
-    my ($scfg, $zvol, $size) = @_;
+    my ($class, $scfg, $zvol, $size) = @_;
 
-    my $guid = zfs_get_lu_name($scfg, $zvol);
+    my $guid = $class->zfs_get_lu_name($scfg, $zvol);
 
-    zfs_request($scfg, undef, 'modify_lu', '-s', "${size}K", $guid);
-}
-
-sub zfs_create_zvol {
-    my ($scfg, $zvol, $size) = @_;
-
-    zfs_request($scfg, undef, 'create', '-b', $scfg->{blocksize}, '-V', "${size}k", "$scfg->{pool}/$zvol");
-}
-
-sub zfs_delete_zvol {
-    my ($scfg, $zvol) = @_;
-
-    zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
+    $class->zfs_request($scfg, undef, 'modify_lu', "${size}K", $guid);
 }
 
 sub zfs_get_lun_number {
-    my ($scfg, $guid) = @_;
-    my $lunnum = undef;
+    my ($class, $scfg, $guid) = @_;
 
     die "could not find lun_number for guid $guid" if !$guid;
 
-    my $text = zfs_request($scfg, undef, 'list_view', '-l', $guid);
-    my @lines = split /\n/, $text;
-    foreach my $line (@lines) {
-       if ($line =~ /^\s*LUN\s*:\s*(\d+)$/) {
-           $lunnum = $1;
-           last;
-       }
-    }
-
-    return $lunnum;
-}
-
-sub zfs_list_zvol {
-    my ($scfg) = @_;
-
-    my $text = zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-Hr');
-    my $zvols = zfs_parse_zvol_list($text);
-    return undef if !$zvols;
-
-    my $list = ();
-    foreach my $zvol (@$zvols) {
-       my @values = split('/', $zvol->{name});
-
-       my $pool = $values[0];
-       my $image = $values[1];
-
-       next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
-       my $owner = $3;
-
-       my $parent = $zvol->{origin};
-       if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
-           $parent = $1;
-       }
-
-       $list->{$pool}->{$image} = {
-           name => $image,
-           size => $zvol->{size},
-           parent => $parent,
-           format => 'raw',
-           vmid => $owner
-       };
-    }
-
-    return $list;
+    return $class->zfs_request($scfg, undef, 'list_view', $guid);
 }
 
 # Configuration
@@ -295,33 +167,48 @@ sub type {
 
 sub plugindata {
     return {
-       content => [ {images => 1}, { images => 1 }],
+    content => [ {images => 1}, { images => 1 }],
     };
 }
 
 sub properties {
     return {
-       iscsiprovider => {
-           description => "iscsi provider",
-           type => 'string',
-       },
-        blocksize => {
-            description => "block size",
-            type => 'string',
-        }
+    iscsiprovider => {
+        description => "iscsi provider",
+        type => 'string',
+    },
+    # this will disable write caching on comstar and istgt.
+    # it is not implemented for iet. iet blockio always operates with 
+    # writethrough caching when not in readonly mode
+    nowritecache => {
+        description => "disable write caching on the target",
+        type => 'boolean',
+    },
+    comstar_tg => {
+        description => "target group for comstar views",
+        type => 'string',
+    },
+    comstar_hg => {
+        description => "host group for comstar views",
+        type => 'string',
+    },
     };
 }
 
 sub options {
     return {
-        nodes => { optional => 1 },
-        disable => { optional => 1 },
-        portal => { fixed => 1 },
-       target => { fixed => 1 },
-        pool => { fixed => 1 },
-       blocksize => { fixed => 1 },
-       iscsiprovider => { fixed => 1 },
-       content => { optional => 1 },
+    nodes => { optional => 1 },
+    disable => { optional => 1 },
+    portal => { fixed => 1 },
+    target => { fixed => 1 },
+    pool => { fixed => 1 },
+    blocksize => { fixed => 1 },
+    iscsiprovider => { fixed => 1 },
+    nowritecache => { optional => 1 },
+    sparse => { optional => 1 },
+    comstar_hg => { optional => 1 },
+    comstar_tg => { optional => 1 },
+    content => { optional => 1 },
     };
 }
 
@@ -331,7 +218,7 @@ sub parse_volname {
     my ($class, $volname) = @_;
 
     if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
-       return ('images', $5, $8, $2, $4, $6);
+    return ('images', $5, $8, $2, $4, $6);
     }
 
     die "unable to parse zfs volume name '$volname'\n";
@@ -345,39 +232,14 @@ sub path {
     my $target = $scfg->{target};
     my $portal = $scfg->{portal};
 
-    my $guid = zfs_get_lu_name($scfg, $name);
-    my $lun = zfs_get_lun_number($scfg, $guid);
+    my $guid = $class->zfs_get_lu_name($scfg, $name);
+    my $lun = $class->zfs_get_lun_number($scfg, $guid);
 
     my $path = "iscsi://$portal/$target/$lun";
 
     return ($path, $vmid, $vtype);
 }
 
-my $find_free_diskname = sub {
-    my ($storeid, $scfg, $vmid) = @_;
-
-    my $name = undef;
-    my $volumes = zfs_list_zvol($scfg);
-
-    my $disk_ids = {};
-    my $dat = $volumes->{$scfg->{pool}};
-
-    foreach my $image (keys %$dat) {
-        my $volname = $dat->{$image}->{name};
-        if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
-            $disk_ids->{$2} = 1;
-        }
-    }
-
-    for (my $i = 1; $i < 100; $i++) {
-        if (!$disk_ids->{$i}) {
-            return "vm-$vmid-disk-$i";
-        }
-    }
-
-    die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
-};
-
 sub create_base {
     my ($class, $storeid, $scfg, $volname) = @_;
 
@@ -393,11 +255,11 @@ sub create_base {
 
     my $newvolname = $basename ? "$basename/$newname" : "$newname";
 
-    zfs_delete_lu($scfg, $name);
-    zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
+    $class->zfs_delete_lu($scfg, $name);
+    $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
 
-    my $guid = zfs_create_lu($scfg, $newname);
-    zfs_add_lun_mapping_entry($scfg, $newname, $guid);
+    my $guid = $class->zfs_create_lu($scfg, $newname);
+    $class->zfs_add_lun_mapping_entry($scfg, $newname, $guid);
 
     my $running  = undef; #fixme : is create_base always offline ?
 
@@ -407,23 +269,23 @@ sub create_base {
 }
 
 sub clone_image {
-    my ($class, $scfg, $storeid, $volname, $vmid) = @_;
+    my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
 
-    my $snap = '__base__';
+    $snap ||= '__base__';
 
     my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
         $class->parse_volname($volname);
 
     die "clone_image only works on base images\n" if !$isBase;
 
-    my $name = &$find_free_diskname($storeid, $scfg, $vmid);
+    my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
 
     warn "clone $volname: $basename to $name\n";
 
-    zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
+    $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
 
-    my $guid = zfs_create_lu($scfg, $name);
-    zfs_add_lun_mapping_entry($scfg, $name, $guid);
+    my $guid = $class->zfs_create_lu($scfg, $name);
+    $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
 
     return $name;
 }
@@ -434,13 +296,13 @@ sub alloc_image {
     die "unsupported format '$fmt'" if $fmt ne 'raw';
 
     die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
-       if $name && $name !~ m/^vm-$vmid-/;
+    if $name && $name !~ m/^vm-$vmid-/;
 
-    $name = &$find_free_diskname($storeid, $scfg, $vmid);
+    $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) if !$name;
 
-    zfs_create_zvol($scfg, $name, $size);
-    my $guid = zfs_create_lu($scfg, $name);
-    zfs_add_lun_mapping_entry($scfg, $name, $guid);
+    $class->zfs_create_zvol($scfg, $name, $size);
+    my $guid = $class->zfs_create_lu($scfg, $name);
+    $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
 
     return $name;
 }
@@ -450,14 +312,14 @@ sub free_image {
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
-    zfs_delete_lu($scfg, $name);
+    $class->zfs_delete_lu($scfg, $name);
     eval {
-        zfs_delete_zvol($scfg, $name);
+        $class->zfs_delete_zvol($scfg, $name);
     };
     do {
         my $err = $@;
-        my $guid = zfs_create_lu($scfg, $name);
-        zfs_add_lun_mapping_entry($scfg, $name, $guid);
+        my $guid = $class->zfs_create_lu($scfg, $name);
+        $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
         die $err;
     } if $@;
 
@@ -467,37 +329,37 @@ sub free_image {
 sub list_images {
     my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
 
-    $cache->{zfs} = zfs_list_zvol($scfg) if !$cache->{zfs};
+    $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
     my $zfspool = $scfg->{pool};
     my $res = [];
 
     if (my $dat = $cache->{zfs}->{$zfspool}) {
 
-       foreach my $image (keys %$dat) {
+    foreach my $image (keys %$dat) {
 
-           my $volname = $dat->{$image}->{name};
-           my $parent = $dat->{$image}->{parent};
+        my $volname = $dat->{$image}->{name};
+        my $parent = $dat->{$image}->{parent};
 
-           my $volid = undef;
+        my $volid = undef;
             if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
-               my ($basename) = ($1);
-               $volid = "$storeid:$basename/$volname";
-           } else {
-               $volid = "$storeid:$volname";
-           }
-
-           my $owner = $dat->{$volname}->{vmid};
-           if ($vollist) {
-               my $found = grep { $_ eq $volid } @$vollist;
-               next if !$found;
-           } else {
-               next if defined ($vmid) && ($owner ne $vmid);
-           }
-
-           my $info = $dat->{$volname};
-           $info->{volid} = $volid;
-           push @$res, $info;
-       }
+        my ($basename) = ($1);
+        $volid = "$storeid:$basename/$volname";
+        } else {
+        $volid = "$storeid:$volname";
+        }
+
+        my $owner = $dat->{$volname}->{vmid};
+        if ($vollist) {
+        my $found = grep { $_ eq $volid } @$vollist;
+        next if !$found;
+        } else {
+        next if defined ($vmid) && ($owner ne $vmid);
+        }
+
+        my $info = $dat->{$volname};
+        $info->{volid} = $volid;
+        push @$res, $info;
+    }
     }
 
     return $res;
@@ -512,9 +374,9 @@ sub status {
     my $active = 0;
 
     eval {
-       ($free, $used) = zfs_get_pool_stats($scfg);
-       $active = 1;
-       $total = $free + $used;
+    ($free, $used) = $class->zfs_get_pool_stats($scfg);
+    $active = 1;
+    $total = $free + $used;
     };
     warn $@ if $@;
 
@@ -544,7 +406,7 @@ sub deactivate_volume {
 sub volume_size_info {
     my ($class, $scfg, $storeid, $volname, $timeout) = @_;
 
-    return zfs_get_zvol_size($scfg, $volname);
+    return $class->zfs_get_zvol_size($scfg, $volname);
 }
 
 sub volume_resize {
@@ -552,53 +414,68 @@ sub volume_resize {
 
     my $new_size = ($size/1024);
 
-    zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
-    zfs_resize_lu($scfg, $volname, $new_size);
+    $class->zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
+    $class->zfs_resize_lu($scfg, $volname, $new_size);
 }
 
 sub volume_snapshot {
     my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
 
-    zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
+    $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
 }
 
 sub volume_snapshot_rollback {
     my ($class, $scfg, $storeid, $volname, $snap) = @_;
 
-    zfs_delete_lu($scfg, $volname);
+    # abort rollback if snapshot is not the latest
+    my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
+    my $text = $class->zfs_request($scfg, undef, 'list', @params);
+    my @snapshots = split(/\n/, $text);
+    my $recentsnap = undef;
+    foreach (@snapshots) {
+        if (/$scfg->{pool}\/$volname/) {
+            s/^.*@//;
+            $recentsnap = $_;
+        } 
+    }
+    if ($snap ne $recentsnap) {
+        die "cannot rollback, more recent snapshots exist\n";
+    }
+
+    $class->zfs_delete_lu($scfg, $volname);
 
-    zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
+    $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
 
-    zfs_import_lu($scfg, $volname);
+    $class->zfs_import_lu($scfg, $volname);
 
-    zfs_add_lun_mapping_entry($scfg, $volname);
+    $class->zfs_add_lun_mapping_entry($scfg, $volname);
 }
 
 sub volume_snapshot_delete {
     my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
 
-    zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
+    $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
 }
 
 sub volume_has_feature {
     my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
 
     my $features = {
-       snapshot => { current => 1, snap => 1},
-       clone => { base => 1},
-       template => { current => 1},
-       copy => { base => 1, current => 1},
+    snapshot => { current => 1, snap => 1},
+    clone => { base => 1},
+    template => { current => 1},
+    copy => { base => 1, current => 1},
     };
 
     my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
-       $class->parse_volname($volname);
+    $class->parse_volname($volname);
 
     my $key = undef;
 
     if ($snapname) {
-       $key = 'snap';
+    $key = 'snap';
     } else {
-       $key = $isBase ? 'base' : 'current';
+    $key = $isBase ? 'base' : 'current';
     }
 
     return 1 if $features->{$feature}->{$key};