From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id C4DD21FF0B3 for ; Thu, 26 Nov 2026 17:57:39 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 9309721797; Thu, 26 Nov 2026 17:57:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795712245; x=1796317045; darn=lists.proxmox.com; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=BUOSYZ5v/5T5XPHTynxyWWxfEUumiB4I1GDo2W3hG1s=; b=KcTC5ecmbR2E5YO1K5wnoib1AO2AmB0dROG+8qr2EkBaHuQkkgTuq9lEJ5Nkp+ZtHt GbXF68uGVxY3tZsEyqjLl5/G4Q9q1XkGIugiliUimiiv6Ru8WwA7w8Mtqaxme8CSvt4w SIj2x4PMK6V03pRjvCr41TBcFlROSen0xrOvDkN5wJywMDlKOKCPJvbtP6K2ti7NCWbU 3FJTARCW6Pw/YBk1gChBQtbT0UNjLq5n82QkMkSx1RfLosljQNnj5j2Jtr69IQXjX0cc BETY7nllJAtcMHQ2C1UF83JETMGPSzRrfbymqGygc06UwVz0m5lwEii9IyVULQbvJdwh NNVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795712245; x=1796317045; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=BUOSYZ5v/5T5XPHTynxyWWxfEUumiB4I1GDo2W3hG1s=; b=2BRTwRMwiopk1bB64TfwJCWDioRRO4g9iDjw/gRjKxVUBKOsPUQyQ6aG4UyPVENR22 z/xNPdS7y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7d7393d3fa9mr12332569137.15.1795712244477; Thu, 26 Nov 2026 08:57:24 -0800 (PST) Date: Thu, 26 Nov 2026 13:57:01 -0300 From: Ciro Iriarte To: pve-devel@lists.proxmox.com Subject: [RFC PATCH storage v2 3/6] dir: implement copy-offload via reflink (FICLONE) Message-ID: <20261126.3.copyoffload-v2@cyruspy.gmail.com> In-Reply-To: <20261126.0.copyoffload-v2@cyruspy.gmail.com> References: <20261126.0.copyoffload-v2@cyruspy.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.092 Adjusted score from AWL reputation of From: address DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain DMARC_PASS -0.1 DMARC pass policy FREEMAIL_FROM 0.001 Sender email is commonly abused enduser mail provider RCVD_IN_DNSWL_NONE -0.0001 Sender listed at https://www.dnswl.org/, no trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 277625MJCJSPLQBLOMWR3FVX554YTH5I X-Message-ID-Hash: 277625MJCJSPLQBLOMWR3FVX554YTH5I X-MailFrom: cyruspy@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Ciro Iriarte A filesystem with FICLONE support (XFS with reflink=1, btrfs, ZFS with block cloning) can make a full copy by sharing extents copy-on-write. The copy is independent immediately, since the extents are reference counted, so copy_image_status() reports complete on the first poll. Today a full clone on such a directory storage copies every byte through qemu-img convert. copy_image_check() refuses the following, so the clone takes the host-side path: - a snapshot source or a format conversion, neither of which a byte copy can do - a qcow2 with a backing file: a byte copy inherits the dependency and breaks once the base is removed - a qcow2 with internal snapshots: they would carry over into a clone whose configuration has none - a target on another filesystem, or a filesystem without reflink support, detected by reflinking a one-byte scratch file: for XFS it depends on how the filesystem was created prepare creates the target file rather than only choosing a name, as the caller releases the storage lock before start. A failed start truncates it instead of removing it, so the name stays reserved until the caller's rollback frees it. start uses 'cp --reflink=always', so a filesystem without reflink fails instead of turning into a full copy inside a guest freeze. Signed-off-by: Ciro Iriarte --- Changes in v2: * rebased onto current master * rebase: VM disks are matched with vtype_matches(); under API 17 parse_volname() returns 'vm-vol', so the 'images' comparison carried over from v1 would never advertise the feature * new copy_image_check(), also called by prepare(). It probes reflink support, so a filesystem without it falls back to the host copy * refuses a qcow2 with internal snapshots * a failed start truncates the target instead of removing it, keeping the name reserved for the caller's rollback src/PVE/Storage/DirPlugin.pm | 193 +++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) diff --git a/src/PVE/Storage/DirPlugin.pm b/src/PVE/Storage/DirPlugin.pm index 397d4fe..f5f227f 100644 --- a/src/PVE/Storage/DirPlugin.pm +++ b/src/PVE/Storage/DirPlugin.pm @@ -8,6 +8,7 @@ use Encode qw(decode encode); use File::Path; use File::Spec; use IO::File; +use JSON; use POSIX; use PVE::Storage::Plugin; @@ -44,6 +45,7 @@ sub plugindata { 'advanced-properties' => { preallocation => 1, 'snapshot-as-volume-chain' => 1, + 'copy-offload-timeout' => 1, }, 'hidden-properties' => { bwlimit => 1, @@ -89,6 +91,8 @@ sub options { bwlimit => { optional => 1 }, preallocation => { optional => 1 }, 'snapshot-as-volume-chain' => { optional => 1, fixed => 1 }, + 'copy-offload' => { optional => 1 }, + 'copy-offload-timeout' => { optional => 1 }, }; } @@ -317,4 +321,193 @@ sub volume_qemu_snapshot_method { return $scfg->{'snapshot-as-volume-chain'} ? 'mixed' : 'qemu'; } +# qemu_img_info() returns raw JSON text, so decode before use. Returns the decoded +# info, or undef when the image cannot be inspected. +my sub qcow2_info { + my ($path) = @_; + my $json = eval { PVE::Storage::Common::qemu_img_info($path, undef, 10) }; + return undef if $@ || !$json; + my $info = eval { decode_json($json) }; + return undef if $@ || ref($info) ne 'HASH'; + return $info; +} + +sub volume_has_feature { + my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_; + + if ($feature eq 'copy-offload-atomic') { + # reflink clones a whole file; there is no way to pick a snapshot out of one + return 0 if $snapname; + + my ($vtype, undef, undef, undef, undef, undef, $format) = + eval { $class->parse_volname($volname) }; + return 0 if $@ || !defined($vtype); + return 0 if !PVE::Storage::Common::vtype_matches($vtype, 'vm-vol'); + return 0 if $format ne 'raw' && $format ne 'qcow2'; + + # whether this file can be copied to a given target is copy_image_check()'s call + return 1; + } + + return $class->SUPER::volume_has_feature( + $scfg, $feature, $storeid, $volname, $snapname, $running, $opts, + ); +} + +# ---- storage-offloaded full copy via reflink ------------------------------------- +# +# A filesystem that supports FICLONE (XFS with reflink=1, btrfs, ZFS with block +# cloning) can produce a full copy by sharing extents copy-on-write. Unlike an RBD +# clone or a ZFS clone-from-snapshot, the result is independent straight away: the +# extents are reference counted, so deleting the source does not affect the copy. +# +# There is no background work, so copy_image_status() reports 'complete' on the +# first poll. + +# Same filesystem? FICLONE cannot cross one, and the caller may be copying between +# two different storages that happen to be directories. +my sub same_filesystem { + my ($a, $b) = @_; + my $da = (stat($a))[0]; + my $db = (stat($b))[0]; + return defined($da) && defined($db) && $da == $db; +} + +# The nearest existing directory at or above $dir: the target's per-VM directory may +# not exist yet when the pair is checked. +my sub existing_dir { + my ($dir) = @_; + $dir =~ s|/[^/]+$|| while length($dir) > 1 && !-d $dir; + return $dir; +} + +# Whether the filesystem holding $dir can reflink, found by trying it on a one-byte +# scratch file: there is no generic way to ask, and for XFS it depends on how the +# filesystem was created. Returns undef or the reason it cannot. +sub reflink_unsupported { + my ($class, $dir) = @_; + + my ($d) = $dir =~ m|^(/.*)$| or return "invalid directory '$dir'"; + my $src = "$d/.pve-reflink-probe-$$"; + my $dst = "$src.clone"; + + my $fh = IO::File->new($src, O_WRONLY | O_CREAT | O_EXCL, 0600) + or return "cannot create a scratch file in '$d' - $!"; + print $fh "x"; + $fh->close(); + + my $ok = eval { + PVE::Tools::run_command( + ['/bin/cp', '--reflink=always', '--', $src, $dst], + outfunc => sub { }, + errfunc => sub { }, + ); + 1; + }; + unlink($src, $dst); + + return $ok ? undef : "the filesystem of '$d' does not support reflinks"; +} + +sub copy_image_check { + my ($class, $scfg, $storeid, $volname, $target_scfg, $target_storeid, $snap, $opts) = @_; + + return "reflink cannot copy from a snapshot" if defined($snap); + + my ($vtype, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname); + return "only VM disks can be offloaded, not '$vtype'" + if !PVE::Storage::Common::vtype_matches($vtype, 'vm-vol'); + + # FICLONE copies bytes; it cannot convert between formats. + my $target_format = $opts->{format} // $format; + return "cannot convert '$format' to '$target_format'" if $target_format ne $format; + + my $path = $class->filesystem_path($scfg, $volname); + + if ($format eq 'qcow2') { + my $info = qcow2_info($path) // return "cannot inspect '$volname'"; + # A qcow2 with a backing file is not independent, and a byte-identical copy of + # it inherits that dependency: it would pass qemu-img check and then break + # when the base is removed. Only a real convert can flatten it. + return "'$volname' has a backing file" if $info->{'backing-filename'}; + # Internal snapshots would travel with a byte copy: hidden space and old data in + # a clone whose config has no snapshots. qemu-img convert drops them. + return "'$volname' has internal snapshots" + if ref($info->{snapshots}) eq 'ARRAY' && scalar($info->{snapshots}->@*); + } + + my $target_dir = existing_dir($class->get_subdir($target_scfg, 'images')); + return "source and target are on different filesystems" + if !same_filesystem($path, $target_dir); + + return $class->reflink_unsupported($target_dir); +} + +sub copy_image_prepare { + my ( + $class, $scfg, $storeid, $volname, + $target_scfg, $target_storeid, $target_vmid, $snap, $opts, + ) = @_; + + my $reason = $class->copy_image_check( + $scfg, $storeid, $volname, $target_scfg, $target_storeid, $snap, $opts, + ); + die "cannot offload copy of '$volname': $reason\n" if defined($reason); + + my $format = ($class->parse_volname($volname))[6]; + my $path = $class->filesystem_path($scfg, $volname); + my $target_dir = $class->get_subdir($target_scfg, 'images') . "/$target_vmid"; + mkpath $target_dir; + + # mkpath may have created it on another mount than its parent (an automounted or + # bind-mounted VM directory), so check the directory the file lands in. + die "copy offload requires source and target on the same filesystem\n" + if !same_filesystem($path, $target_dir); + + # the trailing 1 adds the format suffix; without it the volname does not parse + my $name = + $class->find_free_diskname($target_storeid, $target_scfg, $target_vmid, $format, 1); + + # Reserve the name by creating the file. The caller drops the storage lock between + # prepare and start, so a name that was only chosen could be taken by a concurrent + # allocation, and the caller's rollback would then delete somebody else's volume. + # An empty file costs nothing and makes the target freeable. + my $target_path = "$target_dir/$name"; + my $fh = IO::File->new($target_path, O_WRONLY | O_CREAT | O_EXCL, 0640) + or die "unable to reserve '$target_path' - $!\n"; + close($fh); + + return "$target_vmid/$name"; +} + +sub copy_image_start { + my ( + $class, $scfg, $storeid, $volname, + $target_scfg, $target_storeid, $target_volname, $snap, + ) = @_; + + my $src = $class->filesystem_path($scfg, $volname); + my $dst = $class->filesystem_path($target_scfg, $target_volname); + + # --reflink=always so an unsupported filesystem fails instead of turning this into + # a full byte copy that blocks the caller, which may be holding a guest frozen. + # On failure the target is truncated, not removed: it is the caller's reservation of + # the name until its rollback frees it. + eval { PVE::Tools::run_command(['/bin/cp', '--reflink=always', '--', $src, $dst]) }; + if (my $err = $@) { + truncate($dst, 0); + die "reflink copy of '$volname' failed - $err"; + } + + return; +} + +sub copy_image_status { + my ($class, $scfg, $storeid, $volname, $source) = @_; + + # FICLONE is synchronous and the extents are reference counted, so the copy is + # already independent of its source. Nothing to poll and nothing to clean up. + return { state => 'complete' }; +} + 1; -- 2.55.0