]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/BTRFSPlugin.pm
fix #3555: BTRFS: call DirPlugin's free_image correctly
[pve-storage.git] / PVE / Storage / BTRFSPlugin.pm
index 133edc68d51d18c649023590ad9807813dec1c55..411cab9530ead752e792c35eec85eea409b7288b 100644 (file)
@@ -20,6 +20,7 @@ use constant {
     FS_NOCOW_FL => 0x00800000,
     FS_IOC_GETFLAGS => 0x40086602,
     FS_IOC_SETFLAGS => 0x80086601,
+    BTRFS_MAGIC => 0x9123683e,
 };
 
 # Configuration (similar to DirPlugin)
@@ -66,10 +67,12 @@ sub options {
        shared => { optional => 1 },
        disable => { optional => 1 },
        maxfiles => { optional => 1 },
+       'prune-backups'=> { optional => 1 },
        content => { optional => 1 },
        format => { optional => 1 },
        is_mountpoint => { optional => 1 },
        nocow => { optional => 1 },
+       mkdir => { optional => 1 },
        # TODO: The new variant of mkdir with  `populate` vs `create`...
     };
 }
@@ -89,9 +92,43 @@ sub check_config {
     return PVE::Storage::DirPlugin::check_config($self, $sectionId, $config, $create, $skipSchemaCheck);
 }
 
+my sub getfsmagic($) {
+    my ($path) = @_;
+    # The field type sizes in `struct statfs` are defined in a rather annoying way, and we only
+    # need the first field, which is a `long` for our supported platforms.
+    # Should be moved to pve-rs, so this can be the problem of the `libc` crate ;-)
+    # Just round up and extract what we need:
+    my $buf = pack('x160');
+    if (0 != syscall(&PVE::Syscall::SYS_statfs, $path, $buf)) {
+       die "statfs on '$path' failed - $!\n";
+    }
+
+    return unpack('L!', $buf);
+}
+
+my sub assert_btrfs($) {
+    my ($path) = @_;
+    die "'$path' is not a btrfs file system\n"
+       if getfsmagic($path) != BTRFS_MAGIC;
+}
+
 sub activate_storage {
     my ($class, $storeid, $scfg, $cache) = @_;
-    return PVE::Storage::DirPlugin::activate_storage($class, $storeid, $scfg, $cache);
+
+    my $path = $scfg->{path};
+    if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) {
+       mkpath $path;
+    }
+
+    my $mp = PVE::Storage::DirPlugin::parse_is_mountpoint($scfg);
+    if (defined($mp) && !PVE::Storage::DirPlugin::path_is_mounted($mp, $cache->{mountdata})) {
+       die "unable to activate storage '$storeid' - directory is expected to be a mount point but"
+       ." is not mounted: '$mp'\n";
+    }
+
+    assert_btrfs($path); # only assert this stuff now, ensures $path is there and better UX
+
+    $class->SUPER::activate_storage($storeid, $scfg, $cache);
 }
 
 sub status {
@@ -373,7 +410,7 @@ sub free_image {
        $class->parse_volname($volname);
 
     if ($format ne 'subvol' && $format ne 'raw') {
-       return PVE::Storage::DirPlugin::free_image(@_);
+       return PVE::Storage::DirPlugin->free_image($storeid, $scfg, $volname, $isBase, $_format);
     }
 
     my $path = $class->filesystem_path($scfg, $volname);