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 789521FF0B3 for ; Thu, 26 Nov 2026 16:10:43 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 26F4521777; Thu, 26 Nov 2026 16:10:15 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neatech-ar.20260707.gappssmtp.com; s=20260707; t=1795705804; x=1796310604; darn=lists.proxmox.com; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=lHMVzZqcn9swwr3UrjC+5kRdyYU/hCXOplOTjNmmKRg=; b=YLe7vYCeZCYbZRsJR36O0JTLMKfztYiZN9eJuCHDCdHi7RHrec0rtQxu55y6EMO7tJ toRnAO4bTHfUiGcBFXonbg3LRw5LMamTYlNUrRyglICZs/+fZ55WKLs2U3s8vWgNNVP8 MC458dS+rV6gZ1CKthuQn4OXkEvupchmRzDXYOP4xZNduJMNAHhOjIqT9VqEVur98lfX eQwGbaq4W+Qh9sfe/AB1fEM9KbGlkHerHz0JuN6GfhQC3fqIR3fD/Ams4rnvUG5AJVlr ed0oIMZO7n/SYIj4p5M8+2uIbHkQTGK028wlPMrKYD2TCm7o4QJ0AjzVs/jmREEoEsPY tndg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795705804; x=1796310604; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=lHMVzZqcn9swwr3UrjC+5kRdyYU/hCXOplOTjNmmKRg=; b=yzlzNKTKTwDwEkQrTFx6YEOqzeWA4S097VzNzvOOvtsnaQF1OSCne/sY29u3z+on7W SsopOxgRFWOaMF4G5bYohHIgHi+8WrbCZOOBHFWQZzT8SWFecmlKJ1xjEKdp/yaAORjL Z6BDI9Rai1R0haSfBbtCJThaVwtAAqpizNWTRZY+DY3o9M9JYMOABzjvwHpBdDyQiuGq Qhe5Zrp6hCp5/b/lwJoy02BhaWRh2VYzRKwwK6eb+5mClz/hIqmfoYmM72eWlZ7Q53D3 S5IR7UBpVmh5uslaYv9A6VptQ83VUVR7cOGMq1BJ8+qAAMN2jAEb7ok/TAvqbZLgouT7 lrMA== X-Gm-Message-State: APuU4Z+qsDtks8i/JieA5i3WTE9y65qIQUU7OzJUXqhE0FfhDP5gWoTF x50+7iBFVQYEanRicKsn3P3FM1/rIdkOAcdU3Yk+pgNtreykE2UEsaGtVvR2pPtVuWpL2OzEuL+ diHzhozc= X-Gm-Gg: AZFdTZfM0rvaMsh+QVxuSlHBW4ljQB4dVufTp+NDGbwuarIJxCP0J+PYVI0z7efZVwK ec8EP+0Si3BMlfYBO0lG2xuBXHudUifldpENX4K4F2j+CF2VB/DS0uedkFw+xVgSxRghIRt44nE m3PEm3tctmzeV6zscSc4T5f4q2am8JPt0bxegjIPrQ+S/ze8cL2HNMSvybBqQNQZA/CaIeCxg9t qpwrHjdGh0gkA2qGKNR7kFvvzcZZdeUu/zvCd/oS/mn0KFdPcgXuVtinc1OYz/84fJsg6iJFlCx UYILigpRhD2fLY6ysvbOJBjg42I3jMk4r8Y8j7lA0vcHBUpxOiYU0FNsWqCPXBXk5IInptzqwh5 X/WklXEGVcP3eIy1B/ROHVPH8czAM4mt4UrS96SjIoS5DhNpcidLLNBvy13kmObK7poHPOFFL3e obN0v795aJxHqQR6ocAI5bH3hUuPNRwircYsWJA9nix8dAYf4PGuJnoHevEMLxB06tYFBMQe21/ u+IY6j17pTQI8SUutGSIsjZ0KNhq3K4wzrs7Zd07qoe2Bf8JUNXtz7jyT7Y4lyN1rYTRdXgRpho PKLE8HjyhRKBaAgTEFP/g1wJ6Y7Q/mtSQyKFc5i0866fBAs/is4gqByAesyMrOfX/ZLDg9jMeEh 9PEprsHIHXsUErAmmcmS1x3R0vmMIww== X-Received: by 2002:a05:6102:325b:10b0:7d2:9573:8c1e with SMTP id ada2fe7eead31-7d739460f8bmr13963190137.3.1795705803795; Thu, 26 Nov 2026 07:10:03 -0800 (PST) From: Joaquin Varela To: pve-devel@lists.proxmox.com Subject: [PATCH storage v5 03/13] zfsnvme: add storage configuration and volume methods Date: Thu, 26 Nov 2026 12:09:44 -0300 Message-ID: <20261126150956.310593-4-joaquinvarela@neatech.ar> X-Mailer: git-send-email 2.54.0.windows.1 In-Reply-To: <20261126150956.310593-1-joaquinvarela@neatech.ar> References: <20261126150956.310593-1-joaquinvarela@neatech.ar> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.273 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 DMARC_PASS -0.1 DMARC pass policy KAM_SHORT 0.001 Use of a URL Shortener for very short URL 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: B52CGKLBFYCVSOJSJAQZCBZJVXLGBBZK X-Message-ID-Hash: B52CGKLBFYCVSOJSJAQZCBZJVXLGBBZK X-MailFrom: joaquinvarela@neatech.ar 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: Add the storage schema and config checks, the ZFS helpers and the volume methods of the storage API. Volume names are checked and follow the other ZFS plugins, raw carries no extension, and portals go through PVE::NVMeOF::parse_portal as for nvmeof. The plugin isolates its properties. A new storage cannot take the subsystem NQN of an nvmeof storage. Each storage needs its own NVMe/TCP listener, as nvmet answers a connect to a listener that does not publish the subsystem yet with DNR and a shared one could lose its paths after a restart. The DH-HMAC-CHAP key is stored in /etc/pve/priv/storage and reaches the target on stdin. Signed-off-by: Joaquin Varela --- src/PVE/Storage/ZFSNVMePlugin.pm | 798 ++++++++++++++++++++++++++++++- 1 file changed, 797 insertions(+), 1 deletion(-) diff --git a/src/PVE/Storage/ZFSNVMePlugin.pm b/src/PVE/Storage/ZFSNVMePlugin.pm index 1892bd21..1f95f8c5 100644 --- a/src/PVE/Storage/ZFSNVMePlugin.pm +++ b/src/PVE/Storage/ZFSNVMePlugin.pm @@ -17,6 +17,7 @@ use PVE::Cluster; use PVE::File; use PVE::JSONSchema; use PVE::Network; +use PVE::NVMeOF; use PVE::RESTEnvironment qw(log_warn); use PVE::RPCEnvironment; use PVE::SysFSTools; @@ -105,7 +106,10 @@ my $RE_TRSVCID = qr{(?: \A | ,) trsvcid=(?[^,]+)}nxx; my $RE_HOST_IFACE_ADDRESS = qr{(?: \A | ,) host_iface=(?[^,]+)}nxx; # no ':' like ZFSPlugin, and anchor with \z as $ also accepts a trailing newline my $RE_ZVOL_SUFFIX = qr{[a-zA-Z0-9_.-]+}nxx; -my $RE_ZVOL_OWNER = qr{\A (?:vm|base|subvol|basevol)- (?\d+) - $RE_ZVOL_SUFFIX \z}nxx; +my $RE_ZVOL_OWNER = qr{ + \A (? (?:vm|base|subvol|basevol)- (?\d+) - $RE_ZVOL_SUFFIX) \z +}nxx; +my $RE_ZVOL_ORIGIN = qr{\A (? [A-Za-z0-9] [A-Za-z0-9_.:/-]* \@ [A-Za-z0-9_.:-]+) \z}nxx; my $RE_VOLNAME = qr{ \A (?: (? (?:base|basevol)- (?\d+) - $RE_ZVOL_SUFFIX) /)? @@ -1567,4 +1571,796 @@ my sub nvmet_resize_volume($scfg, $name, $size) { return; } +sub parse_nvme_portals($value, $noerr = undef) { + my $result = []; + my $seen = {}; + + for my $entry (split(/,/, $value // '')) { + $entry = trim($entry); + # parse_portal refuses the same addresses as the nvmeof storage and maps an IPv4-mapped + # IPv6 address to IPv4. It takes an IPv4 address in brackets, this storage does not. + my $portal = PVE::NVMeOF::parse_portal($entry); + if (!$portal || $entry =~ m/\A\[[^\]:]*\]/) { + return undef if $noerr; + die "invalid NVMe/TCP portal '$entry'\n"; + } + my ($address, $port) = $portal->@{qw(traddr trsvcid)}; + if ($seen->{"$address\0$port"}++) { + return undef if $noerr; + die "duplicate NVMe/TCP portal '$entry'\n"; + } + push $result->@*, + { + address => $address, + port => $port, + family => index($address, ':') < 0 ? 'ipv4' : 'ipv6', + }; + if (scalar($result->@*) > $max_paths) { + return undef if $noerr; + die "at most $max_paths NVMe/TCP portals are supported\n"; + } + } + + if (!$result->@*) { + return undef if $noerr; + die "at least one NVMe/TCP portal is required\n"; + } + + return $result; +} + +my sub verify_nvme_portals($value, $noerr = undef) { + return undef if !parse_nvme_portals($value, $noerr); + return $value; +} + +sub parse_nvme_host_ifaces($value, $noerr = undef) { + my $result = []; + + for my $iface (split(/,/, $value // '')) { + $iface = trim($iface); + # the kernel never names an interface '.' or '..' + if ( + length($iface) < 1 + || length($iface) > 15 + || $iface !~ $RE_HOST_IFACE + || $iface eq '.' + || $iface eq '..' + ) { + return undef if $noerr; + die "invalid NVMe/TCP host interface '$iface'\n"; + } + push $result->@*, $iface; + } + + if (!$result->@*) { + return undef if $noerr; + die "at least one NVMe/TCP host interface is required\n"; + } + + return $result; +} + +my sub verify_nvme_host_ifaces($value, $noerr = undef) { + return undef if !parse_nvme_host_ifaces($value, $noerr); + return $value; +} + +my sub verify_nvme_host_nqns($value, $noerr = undef) { + return undef if !parse_nvme_host_nqns($value, $noerr); + return $value; +} + +sub _configured_portals($scfg) { + my $portals = parse_nvme_portals($scfg->{'nvme-portals'}); + my $ifaces = parse_nvme_host_ifaces($scfg->{'nvme-host-ifaces'}); + die "nvme-host-ifaces must contain one interface for each nvme-portals entry\n" + if scalar($ifaces->@*) != scalar($portals->@*); + + for (my $i = 0; $i < scalar($portals->@*); $i++) { + $portals->[$i]->{host_iface} = $ifaces->[$i]; + } + return $portals; +} + +sub _local_iface_exists($iface) { + return -d "/sys/class/net/$iface"; +} + +sub _validate_local_ifaces($portals) { + for my $portal ($portals->@*) { + my $iface = $portal->{host_iface}; + die "NVMe/TCP host interface '$iface' does not exist on this node\n" + if !_local_iface_exists($iface); + } +} + +PVE::JSONSchema::register_format('pve-storage-nvme-nqn', \&verify_nvme_nqn); +PVE::JSONSchema::register_format('pve-storage-nvme-portals', \&verify_nvme_portals); +PVE::JSONSchema::register_format('pve-storage-nvme-host-ifaces', \&verify_nvme_host_ifaces); +PVE::JSONSchema::register_format('pve-storage-nvme-host-nqns', \&verify_nvme_host_nqns); + +sub type($class) { + return 'zfsnvme'; +} + +sub plugindata($class) { + return { + content => [{ images => 1, 'vm-vol' => 1, 'vm-state' => 1 }, { images => 1 }], + 'isolate-properties' => 1, + 'advanced-properties' => { + 'nvme-iopolicy' => 1, + 'nvme-keep-alive-tmo' => 1, + 'nvme-reconnect-delay' => 1, + 'nvme-ctrl-loss-tmo' => 1, + 'nvme-fast-io-fail-tmo' => 1, + 'nvme-nr-io-queues' => 1, + }, + 'hidden-properties' => { + bwlimit => 1, + content => 1, + }, + 'sensitive-properties' => { 'dhchap-key' => 1 }, + }; +} + +# The plugin isolates its properties, so it declares the ones it shares with other plugins itself. +sub properties($class) { + return { + server => { + title => "Server", + description => "IP address or hostname of the ZFS target.", + type => 'string', + format => 'pve-storage-server', + }, + pool => { + title => "Pool", + description => "ZFS pool on the target.", + type => 'string', + }, + blocksize => { + title => "Block Size", + description => "ZFS block size.", + type => 'string', + format => 'pve-storage-zfs-blocksize', + format_description => 'a power of 2 with optional k or m suffix', + }, + sparse => { + title => "Thin Provision", + description => "Use sparse (thin-provisioned) volumes.", + type => 'boolean', + }, + subsysnqn => { + title => "Subsystem NQN", + description => "NVMe subsystem qualified name.", + type => 'string', + format => 'pve-storage-nvme-nqn', + }, + 'nvme-portals' => { + title => "Portals", + description => + "Comma-separated NVMe/TCP target IP addresses, at most 16. Put IPv6 addresses in" + . " brackets. The default TCP port is 4420.", + type => 'string', + format => 'pve-storage-nvme-portals', + maxLength => 2048, + }, + 'nvme-host-ifaces' => { + title => "Host Interfaces", + description => + "Comma-separated local interfaces, in portal order. Interface names must be" + . " identical on every cluster node.", + type => 'string', + format => 'pve-storage-nvme-host-ifaces', + maxLength => 512, + }, + 'nvme-host-nqns' => { + title => "Host NQNs", + description => + "Comma-separated /etc/nvme/hostnqn values for every cluster node allowed to use" + . " this storage, at most 64. Host NQNs can only be added.", + type => 'string', + format => 'pve-storage-nvme-host-nqns', + maxLength => 8192, + }, + 'dhchap-key' => { + title => "DH-HMAC-CHAP Key", + description => + "NVMe DH-HMAC-CHAP key in secret representation format (DHHC-1). Required when the" + . " storage is created. It cannot be changed.", + type => 'string', + maxLength => 256, + sensitive => 1, + }, + 'nvme-iopolicy' => { + title => "I/O Policy", + description => "Native NVMe multipath I/O policy.", + type => 'string', + enum => ['numa', 'round-robin', 'queue-depth'], + default => 'round-robin', + }, + 'nvme-keep-alive-tmo' => { + title => "Keep-Alive Timeout (s)", + description => "NVMe keep-alive timeout in seconds.", + type => 'integer', + minimum => 1, + maximum => 120, + default => 5, + }, + 'nvme-reconnect-delay' => { + title => "Reconnect Delay (s)", + description => "Delay between NVMe reconnect attempts in seconds.", + type => 'integer', + minimum => 1, + maximum => 120, + default => 2, + }, + 'nvme-ctrl-loss-tmo' => { + title => "Controller Loss Timeout (s)", + description => "Time to keep retrying a lost NVMe controller in seconds.", + type => 'integer', + minimum => -1, + maximum => 86400, + default => 600, + }, + 'nvme-fast-io-fail-tmo' => { + title => "Fast I/O Fail Timeout (s)", + description => + "Optional time before failing I/O on a reconnecting NVMe controller. Unset queues" + . " I/O until controller loss timeout.", + type => 'integer', + minimum => 0, + maximum => 86400, + optional => 1, + }, + 'nvme-nr-io-queues' => { + title => "I/O Queues", + description => "Number of NVMe/TCP I/O queues per controller.", + type => 'integer', + minimum => 1, + maximum => 1024, + optional => 1, + }, + }; +} + +sub options($class) { + return { + server => { fixed => 1 }, + subsysnqn => { fixed => 1 }, + 'nvme-portals' => { fixed => 1 }, + 'nvme-host-ifaces' => {}, + 'nvme-host-nqns' => {}, + pool => { fixed => 1 }, + blocksize => { fixed => 1 }, + sparse => { optional => 1 }, + 'dhchap-key' => { optional => 1 }, + 'nvme-iopolicy' => { optional => 1 }, + 'nvme-keep-alive-tmo' => { optional => 1 }, + 'nvme-reconnect-delay' => { optional => 1 }, + 'nvme-ctrl-loss-tmo' => { optional => 1 }, + 'nvme-fast-io-fail-tmo' => { optional => 1 }, + 'nvme-nr-io-queues' => { optional => 1 }, + nodes => { optional => 1 }, + disable => { optional => 1 }, + content => { optional => 1 }, + bwlimit => { optional => 1 }, + }; +} + +sub _validate_fail_fast_timeout($config, $default_ctrl_loss_tmo = undef) { + my $fast = $config->{'nvme-fast-io-fail-tmo'}; + return if !defined($fast); + + my $ctrl = $config->{'nvme-ctrl-loss-tmo'}; + $ctrl = $default_ctrl_loss_tmo if !defined($ctrl); + return if !defined($ctrl) || $ctrl < 0; + + die "nvme-fast-io-fail-tmo must not exceed nvme-ctrl-loss-tmo\n" + if $fast > $ctrl; +} + +# no defaults are injected here, as this also runs when storage.cfg is parsed +sub check_config($class, $section_id, $config, $create, $skip_schema_check = undef) { + nvmet_pool($config) if defined($config->{pool}); + verify_nvme_nqn($config->{subsysnqn}) if defined($config->{subsysnqn}); + parse_nvme_portals($config->{'nvme-portals'}) if defined($config->{'nvme-portals'}); + if (defined($config->{'nvme-host-ifaces'}) && defined($config->{'nvme-portals'})) { + _configured_portals($config); + } elsif (defined($config->{'nvme-host-ifaces'})) { + parse_nvme_host_ifaces($config->{'nvme-host-ifaces'}); + } + parse_nvme_host_nqns($config->{'nvme-host-nqns'}) + if defined($config->{'nvme-host-nqns'}); + _validate_fail_fast_timeout($config, $create ? 600 : undef); + return $class->SUPER::check_config($section_id, $config, $create, $skip_schema_check); +} + +my sub zfs_request($scfg, $timeout, $method, @args) { + $timeout = PVE::RPCEnvironment->is_worker() ? 60 * 60 : 10 if !$timeout; + my $change = $method ne 'get' && $method ne 'list'; + my $res = nvmet_exec( + $scfg, [['zfs', $method, @args]], + op => "zfs $method", + timeout => $timeout, + change => $change, + ); + die "zfs error on '" . nvmet_server($scfg) . "': $res->{err}\n" if $res->{rc}; + return nvmet_output($res); +} + +# the plugin creates and owns zvols only, a filesystem child still holds its name +my sub zfs_parse_zvol_list($text, $pool) { + my $list = []; + for my $line (split /\n/, $text // '') { + my ($dataset, $size, $origin, $type) = split(/\s+/, $line); + next if !defined($type) || ($type ne 'volume' && $type ne 'filesystem'); + my @parts = split /\//, $dataset; + next if @parts < 2; + my $name = pop @parts; + next if join('/', @parts) ne $pool; + next if $name !~ $RE_ZVOL_OWNER; + my ($zvol, $owner) = @+{qw(name owner)}; + my $volume = $type eq 'volume'; + # A size or an origin of another form stays as the target sent it. The sum has a statement + # of its own, as a statement that reads a tainted value taints the sums in it. + $size = (nvmet_uint($size) // $size) + 0 if $volume; + $origin = $+{origin} if $origin =~ $RE_ZVOL_ORIGIN; + push $list->@*, + { + name => $zvol, + owner => $owner, + type => $type, + ($volume ? (size => $size) : ()), + ($origin ne '-' ? (origin => $origin) : ()), + }; + } + return $list; +} + +my sub zfs_list_zvol($scfg, $owned_only) { + my $pool = nvmet_pool($scfg); + my $text = zfs_request( + $scfg, + 10, + 'list', + '-o', + 'name,volsize,origin,type', + '-t', + 'volume,filesystem', + '-d1', + '-Hp', + $pool, + ); + my $list = {}; + my $prefix = "$pool/"; + for my $zvol (zfs_parse_zvol_list($text, $pool)->@*) { + next if $owned_only && $zvol->{type} ne 'volume'; + my $parent = $zvol->{origin}; + $parent = substr($parent, length($prefix)) if $parent && index($parent, $prefix) == 0; + $list->{ $zvol->{name} } = { + name => $zvol->{name}, + size => $zvol->{size}, + parent => $parent, + format => 'raw', + vmid => $zvol->{owner}, + }; + } + return $list if !$owned_only; + + my $properties = zfs_request( + $scfg, + 10, + 'get', + '-H', + '-d', + '1', + '-o', + 'name,value,source', + 'proxmox:nvme-subsys', + $pool, + ); + my $owned = {}; + for my $line (split(/\n/, $properties)) { + my ($dataset, $nqn, $source) = split(/\t/, $line, 3); + next if !defined($source) || ($source ne 'local' && $source ne 'received'); + next if !defined($nqn) || $nqn ne $scfg->{subsysnqn}; + next if index($dataset, $prefix) != 0; + $owned->{ substr($dataset, length($prefix)) } = 1; + } + for my $name (keys $list->%*) { + delete $list->{$name} if !$owned->{$name}; + } + return $list; +} + +my sub zfs_get_properties($scfg, $properties, $dataset, $timeout = undef) { + my $text = zfs_request($scfg, $timeout, 'get', '-o', 'value', '-Hp', $properties, $dataset); + my @values = split /\n/, $text; + return wantarray ? @values : $values[0]; +} + +my sub zfs_get_sorted_snapshot_list($scfg, $name, $sort_params) { + # a name of another form stays as the target sent it + return [ + map { + my $snap = s/^.*\@//r; + $snap =~ $RE_NVMET_SNAPSHOT ? $+{snap} : $snap + } split /\n/, + zfs_request( + $scfg, + undef, + 'list', + '-H', + '-r', + '-t', + 'snapshot', + '-o', + 'name', + $sort_params->@*, + nvmet_dataset($scfg, $name), + ), + ]; +} + +sub parse_volname($class, $volname) { + if ($volname =~ $RE_VOLNAME) { + my $format = ($+{type} eq 'subvol' || $+{type} eq 'basevol') ? 'subvol' : 'raw'; + my $is_base = $+{type} eq 'base' || $+{type} eq 'basevol'; + my $vtype = $class->guest_volume_vtype($format, $+{name}); + return ($vtype, $+{name}, $+{vmid}, $+{base}, $+{base_vmid}, $is_base, $format); + } + die "unable to parse zfs volume name '$volname'\n"; +} + +use constant FORMAT_EXTENSION => { raw => '' }; + +sub get_parsed_format($class, $name) { + return ($class->parse_volname($name))[6]; +} + +sub list_images($class, $storeid, $scfg, $vmid = undef, $vollist = undef, $cache = undef) { + my $res = []; + for my $info (values zfs_list_zvol($scfg, 1)->%*) { + my $volname = + $info->{parent} && $info->{parent} =~ $RE_BASE_SNAPSHOT + ? "$storeid:$+{base}/$info->{name}" + : "$storeid:$info->{name}"; + next + if $vollist + ? !grep { $_ eq $volname } $vollist->@* + : defined($vmid) && $info->{vmid} ne $vmid; + $info->{volid} = $volname; + push $res->@*, $info; + } + return $res; +} + +# names are allocated against every direct child of the pool, also leftovers that list_images hides +sub find_free_diskname($class, $storeid, $scfg, $vmid, $fmt = undef, $add_fmt_suffix = undef) { + my $disk_list = [map { "$storeid:$_" } sort keys zfs_list_zvol($scfg, 0)->%*]; + return PVE::Storage::Plugin::get_next_vm_diskname( + $disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix, + ); +} + +sub status($class, $storeid, $scfg, $cache = undef) { + my ($available, $used) = + eval { zfs_get_properties($scfg, 'available,used', nvmet_pool($scfg)) }; + if (my $err = $@) { + warn "storage '$storeid': $err"; + return (0, 0, 0, 0); + } + ($available, $used) = (nvmet_uint($available), nvmet_uint($used)); + if (!defined($available) || !defined($used)) { + warn "unexpected ZFS pool usage for storage '$storeid'\n"; + return (0, 0, 0, 0); + } + return ($available + $used, $available, $used, 1); +} + +sub volume_size_info($class, $scfg, $storeid, $volname, $timeout = undef) { + my (undef, $name, undef, $parent, undef, undef, $format) = $class->parse_volname($volname); + die "volume_size_info requires a ZFS volume\n" if $format ne 'raw'; + my ($size, $used) = zfs_get_properties( + $scfg, 'volsize,usedbydataset', nvmet_dataset($scfg, $name), $timeout, + ); + $size = nvmet_uint($size); + die "Could not get zfs volume size\n" if !defined($size); + $used = (nvmet_uint($used) // 0) + 0; + return wantarray ? ($size + 0, 'raw', $used, $parent) : $size + 0; +} + +sub volume_snapshot($class, $scfg, $storeid, $volname, $snap) { + my (undef, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname); + die "volume_snapshot requires a ZFS volume\n" if $format ne 'raw'; + my $snapshot = nvmet_snapshot($scfg, $name, $snap); + nvmet_locked($scfg, sub { zfs_request($scfg, undef, 'snapshot', $snapshot) }); + return; +} + +sub volume_snapshot_delete($class, $scfg, $storeid, $volname, $snap, $running = undef) { + my $name = ($class->parse_volname($volname))[1]; + my $snapshot = nvmet_snapshot($scfg, $name, $snap); + nvmet_locked($scfg, sub { zfs_request($scfg, undef, 'destroy', $snapshot) }); + return; +} + +sub volume_rollback_is_possible($class, $scfg, $storeid, $volname, $snap, $blockers = undef) { + my $name = ($class->parse_volname($volname))[1]; + nvmet_snapshot($scfg, $name, $snap); # validation only + my $found; + $blockers //= []; + for my $snapshot (zfs_get_sorted_snapshot_list($scfg, $name, ['-s', 'creation'])->@*) { + $found = 1 if $snapshot eq $snap; + push $blockers->@*, $snapshot if $found && $snapshot ne $snap; + } + die "can't rollback, snapshot '$snap' does not exist on '${storeid}:${volname}'\n" if !$found; + die "can't rollback, '$snap' is not most recent snapshot on '${storeid}:${volname}'\n" + if $blockers->@*; + return 1; +} + +sub volume_snapshot_rollback($class, $scfg, $storeid, $volname, $snap) { + my (undef, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname); + die "snapshot rollback requires a ZFS volume\n" if $format ne 'raw'; + nvmet_rollback_volume($scfg, $name, $snap); +} + +sub volume_snapshot_info($class, $scfg, $storeid, $volname) { + my $name = ($class->parse_volname($volname))[1]; + my $info = {}; + my $text = zfs_request( + $scfg, + undef, + 'list', + '-Hp', + '-r', + '-t', + 'snapshot', + '-o', + 'name,guid,creation', + nvmet_dataset($scfg, $name), + ); + for my $line (split /\n/, $text) { + my ($snapshot, $guid, $creation) = split /\s+/, $line; + $snapshot =~ s/^.*\@//; + $info->{$snapshot} = + { id => nvmet_uint($guid) // $guid, timestamp => nvmet_uint($creation) // $creation }; + } + return $info; +} + +sub free_image($class, $storeid, $scfg, $volname, $is_base = undef, $format = undef) { + my (undef, $name, undef, undef, undef, undef, $volformat) = $class->parse_volname($volname); + die "free_image requires a ZFS volume\n" if $volformat ne 'raw'; + nvmet_destroy_volume($scfg, $name); + return undef; +} + +sub create_base($class, $storeid, $scfg, $volname) { + my (undef, $name, undef, $basename, undef, $is_base, $format) = $class->parse_volname($volname); + die "create_base not possible with base image\n" if $is_base; + die "create_base requires a ZFS volume\n" if $format ne 'raw'; + my $newname = $name =~ s/^vm-/base-/r; + nvmet_template_volume($scfg, $name); + return $basename ? "$basename/$newname" : $newname; +} + +sub clone_image($class, $scfg, $storeid, $volname, $vmid, $snap = undef) { + $snap ||= '__base__'; + my (undef, $basename, undef, undef, undef, $is_base, $format) = $class->parse_volname($volname); + die "clone_image only works on base images\n" if !$is_base; + die "clone_image requires a ZFS volume\n" if $format ne 'raw'; + my $origin = nvmet_snapshot($scfg, $basename, $snap); + my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format); + nvmet_create_volume($scfg, $name, origin => $origin); + return "$basename/$name"; +} + +sub alloc_image($class, $storeid, $scfg, $vmid, $fmt, $name, $size) { + die "unsupported format '$fmt'" if $fmt ne 'raw'; + die "illegal name '$name' - should be 'vm-$vmid-*'\n" + if $name && index($name, "vm-$vmid-") != 0; + my $volname = $name || $class->find_free_diskname($storeid, $scfg, $vmid, $fmt); + $volname = $class->volname_for_format($volname, $fmt, 0); + $size += (1024 - $size % 1024) % 1024; + nvmet_create_volume($scfg, $volname, size => $size); + return $volname; +} + +sub volume_resize($class, $scfg, $storeid, $volname, $size, $running = undef, $snapname = undef) { + # The plugin cannot yet wait for the local namespace to report the new size, so refuse online + # resize before changing the zvol, as a running VM must not see a different size. + die "online resize is not supported for NVMe/TCP block devices; stop the VM first\n" + if $running; + die "resizing a snapshot is not supported for $class\n" if $snapname; + my $new_size = int($size / 1024); + $new_size += (1024 - $new_size % 1024) % 1024; + my $name = ($class->parse_volname($volname))[1]; + nvmet_resize_volume($scfg, $name, $new_size); + return $new_size; +} + +sub volume_has_feature( + $class, $scfg, $feature, $storeid, $volname, + $snapname = undef, + $running = undef, + $opts = undef, +) { + my $features = { + snapshot => { current => 1, snap => 1 }, + clone => { base => 1 }, + template => { current => 1 }, + copy => { base => 1, current => 1 }, + }; + my $is_base = ($class->parse_volname($volname))[5]; + my $key = $snapname ? 'snap' : $is_base ? 'base' : 'current'; + return $features->{$feature}->{$key}; +} + +# the storage has no path, so the base class offers no export or import format either +sub volume_export($class, @args) { + die "ZFS stream export is not supported for NVMe/TCP storage\n"; +} + +sub volume_import($class, @args) { + die "ZFS stream import is not supported for NVMe/TCP storage\n"; +} + +sub rename_volume($class, @args) { + die "renaming volumes is not supported for NVMe/TCP storage\n"; +} + +sub rename_snapshot($class, @args) { + die "rename_snapshot is not supported for $class"; +} + +# Each storage needs its own listener, also a disabled one: nvmet answers a connect to a listener +# that does not publish the subsystem yet with DNR, and the host then deletes the controller. After +# a target restart the storage published second would lose its paths. Storages that reach the same +# data address must spell the server alike, as it names the target lock. +sub _assert_unique_target($storeid, $scfg, $cfg = undef) { + $cfg //= PVE::Storage::config(); + my (%listeners, %addresses); + for my $portal (parse_nvme_portals($scfg->{'nvme-portals'})->@*) { + $listeners{"$portal->{address}\0$portal->{port}"} = 1; + $addresses{ $portal->{address} } = 1; + } + my $ids = $cfg->{ids} // {}; + for my $other_id (sort keys $ids->%*) { + next if $other_id eq $storeid; + my $other = $ids->{$other_id}; + next if ($other->{type} // '') ne 'zfsnvme'; + + die "NVMe subsystem NQN is already used by storage '$other_id'\n" + if ($other->{subsysnqn} // '') eq $scfg->{subsysnqn}; + my $other_server = $other->{server} // ''; + # a storage whose portals do not parse cannot be activated + for my $portal ((parse_nvme_portals($other->{'nvme-portals'}, 1) // [])->@*) { + die "NVMe/TCP portal '$portal->{address}' port $portal->{port} is already used by" + . " storage '$other_id'; give each storage its own address or port\n" + if $listeners{"$portal->{address}\0$portal->{port}"}; + die "storage '$other_id' reaches target address '$portal->{address}' through server" + . " '$other_server'; use the same server value for both storages\n" + if $addresses{ $portal->{address} } && $other_server ne $scfg->{server}; + } + next if $other_server ne $scfg->{server}; + my ($pool, $other_pool) = ($scfg->{pool}, $other->{pool} // ''); + die "ZFS pool '$pool' on '$scfg->{server}' is already used by storage '$other_id'\n" + if $other_pool eq $pool; + die "ZFS pool '$pool' on '$scfg->{server}' overlaps the pool of storage '$other_id'\n" + if index("$other_pool/", "$pool/") == 0 || index("$pool/", "$other_pool/") == 0; + } +} + +# nvmet keeps one key per host NQN, so storages of a target that share a host NQN need the same key +sub _assert_shared_host_key($storeid, $scfg, $key, $cfg = undef) { + $cfg //= PVE::Storage::config(); + my %hosts = map { $_ => 1 } (parse_nvme_host_nqns($scfg->{'nvme-host-nqns'}, 1) // [])->@*; + my $ids = $cfg->{ids} // {}; + for my $other_id (sort keys $ids->%*) { + next if $other_id eq $storeid; + my $other = $ids->{$other_id}; + next if ($other->{type} // '') ne 'zfsnvme'; + next if ($other->{server} // '') ne ($scfg->{server} // ''); + my $other_hosts = parse_nvme_host_nqns($other->{'nvme-host-nqns'}, 1) // []; + next if !grep { $hosts{$_} } $other_hosts->@*; + my $other_key = file_read_firstline(secret_path($other_id)); + die "storage '$other_id' uses a different DH-HMAC-CHAP key for the same NVMe host NQNs" + . " on '$scfg->{server}'\n" + if defined($other_key) && $other_key ne $key; + } +} + +# DHHC-1::: as generated by `nvme gen-dhchap-key`. +# Messages never contain the key. +sub _validate_secret($key) { + die "missing NVMe DH-HMAC-CHAP key\n" if !defined($key) || $key eq ''; + my $invalid = "invalid NVMe DH-HMAC-CHAP key representation\n"; + die $invalid if $key !~ $RE_DHCHAP_KEY; + my ($valid, $hash, $encoded) = @+{qw(key hash secret)}; + die $invalid if length($encoded) % 4; + my $decoded = decode_base64($encoded); + my $length = length($decoded) - 4; + my %lengths = ('00' => [32, 48, 64], '01' => [32], '02' => [48], '03' => [64]); + die $invalid if !grep { $_ == $length } $lengths{$hash}->@*; + die $invalid if pack('V', crc32(substr($decoded, 0, $length))) ne substr($decoded, $length); + return $valid; +} + +my sub set_secret($storeid, $key) { + _validate_secret($key); + make_path($secret_dir, { mode => 0700 }); + file_set_contents(secret_path($storeid), "$key\n", 0600); +} + +my sub get_secret($storeid) { + my $key = file_read_firstline(secret_path($storeid)); + return _validate_secret($key); +} + +sub _unlink_file($path) { + return unlink($path); +} + +my sub delete_secret($storeid) { + _unlink_file(secret_path($storeid)); +} + +# An nvmeof storage lists the namespaces of its subsystem as its own, like a new storage of this +# type would. Only a new storage checks it, so a storage that exists keeps working. +my sub assert_not_used_by_nvmeof($storeid, $scfg) { + my $ids = PVE::Storage::config()->{ids} // {}; + for my $other_id (sort keys $ids->%*) { + next if $other_id eq $storeid || ($ids->{$other_id}->{type} // '') ne 'nvmeof'; + die "NVMe subsystem NQN is already used by storage '$other_id'\n" + if ($ids->{$other_id}->{subsysnqn} // '') eq $scfg->{subsysnqn}; + } +} + +sub on_add_hook($class, $storeid, $scfg, %sensitive) { + _configured_portals($scfg); + parse_nvme_host_nqns($scfg->{'nvme-host-nqns'}); + _assert_unique_target($storeid, $scfg); + assert_not_used_by_nvmeof($storeid, $scfg); + my $key = _validate_secret($sensitive{'dhchap-key'}); + _assert_shared_host_key($storeid, $scfg, $key); + set_secret($storeid, $key); + return; +} + +sub on_update_hook_full($class, $storeid, $scfg, $update, $delete = undef, $sensitive = undef) { + $sensitive //= {}; + my %prospective = ($scfg->%*, $update->%*); + delete @prospective{ $delete->@* } if $delete; + verify_nvme_nqn($prospective{subsysnqn}); + _configured_portals(\%prospective); + my $new_hostnqns = parse_nvme_host_nqns($prospective{'nvme-host-nqns'}); + my %new_hosts = map { $_ => 1 } $new_hostnqns->@*; + for my $old_hostnqn (parse_nvme_host_nqns($scfg->{'nvme-host-nqns'})->@*) { + die "removing NVMe host NQN '$old_hostnqn' is not supported; move every volume off" + . " the storage and recreate it with a new subsystem NQN and a new DH-HMAC-CHAP" + . " key\n" + if !$new_hosts{$old_hostnqn}; + } + _validate_fail_fast_timeout(\%prospective, 600); + _assert_unique_target($storeid, \%prospective); + + my $old_key = file_read_firstline(secret_path($storeid)); + my $key = exists($sensitive->{'dhchap-key'}) ? $sensitive->{'dhchap-key'} : $old_key; + _validate_secret($key); + + # nvmet stores authentication on the global host NQN object, which several subsystems can share. + # Replacing an active key in place can make unrelated storages unrecoverable on their next + # reconnect, so fail until PVE can coordinate a rolling rotation on every node. + die "NVMe DH-HMAC-CHAP key rotation is not supported; create a new storage with a new" + . " subsystem NQN\n" + if defined($old_key) && $key ne $old_key; + _assert_shared_host_key($storeid, \%prospective, $key); + + set_secret($storeid, $key) if exists($sensitive->{'dhchap-key'}); + return; +} + 1; -- 2.54.0.windows.1