From: Ciro Iriarte <cyruspy@gmail.com>
To: pve-devel@lists.proxmox.com
Subject: [RFC PATCH storage v2 3/6] dir: implement copy-offload via reflink (FICLONE)
Date: Thu, 26 Nov 2026 13:57:01 -0300 [thread overview]
Message-ID: <20261126.3.copyoffload-v2@cyruspy.gmail.com> (raw)
In-Reply-To: <20261126.0.copyoffload-v2@cyruspy.gmail.com>
From: Ciro Iriarte <ciro.iriarte+software@gmail.com>
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 <ciro.iriarte+software@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-26 16:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 16:56 [RFC PATCH storage/qemu-server/docs v2 0/6] offload full clone to the storage backend Ciro Iriarte
2026-11-26 16:56 ` [RFC PATCH storage v2 1/6] storage: add asynchronous copy-offload hook for full copies Ciro Iriarte
2026-11-26 16:56 ` [RFC PATCH storage v2 2/6] rbd: implement copy-offload via snapshot + clone + flatten Ciro Iriarte
2026-11-26 16:57 ` Ciro Iriarte [this message]
2026-11-26 16:57 ` [RFC PATCH storage v2 4/6] btrfs, lvmthin: implement copy-offload (atomic class) Ciro Iriarte
2026-11-26 16:57 ` [RFC PATCH qemu-server v2 5/6] use storage copy-offload for full clone Ciro Iriarte
2026-11-26 16:57 ` [RFC PATCH docs v2 6/6] pvesm: document copy-offload and add it to the storage capability table Ciro Iriarte
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126.3.copyoffload-v2@cyruspy.gmail.com \
--to=cyruspy@gmail.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.