public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: util-linux@vger.kernel.org
Cc: pizhenwei@bytedance.com, hare@suse.de, kwolf@redhat.com,
	Stefan Hajnoczi <stefanha@redhat.com>
Subject: [PATCH 2/3] blkpr: add read-keys command
Date: Thu, 11 Dec 2025 11:09:55 -0500	[thread overview]
Message-ID: <20251211160956.1540114-3-stefanha@redhat.com> (raw)
In-Reply-To: <20251211160956.1540114-1-stefanha@redhat.com>

The new IOC_PR_READ_KEYS ioctl lists registered keys on a device. Add a
command so that users can inspect keys. This is useful both for
troubleshooting and for recovery scenarios.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 meson.build            |  3 +++
 sys-utils/blkpr.c      | 46 ++++++++++++++++++++++++++++++++++++++++++
 configure.ac           |  4 ++++
 sys-utils/blkpr.8.adoc |  3 ++-
 4 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index 8b7880e46..47b43c2af 100644
--- a/meson.build
+++ b/meson.build
@@ -765,6 +765,9 @@ conf.set('HAVE_DECL_BLK_ZONE_REP_CAPACITY', have ? 1 : false)
 have = cc.has_header_symbol('linux/pr.h', 'PR_REP_CAPACITY')
 conf.set('HAVE_DECL_PR_REP_CAPACITY', have ? 1 : false)
 
+have = cc.has_header_symbol('linux/pr.h', 'IOC_PR_READ_KEYS')
+conf.set('HAVE_DECL_IOC_PR_READ_KEYS', have ? 1 : false)
+
 code = '''
 #include <time.h>
 #if !@0@
diff --git a/sys-utils/blkpr.c b/sys-utils/blkpr.c
index c6b030def..843ef7da1 100644
--- a/sys-utils/blkpr.c
+++ b/sys-utils/blkpr.c
@@ -103,6 +103,12 @@ static const struct type_string pr_command[] = {
 	{IOC_PR_CLEAR,         "clear",
 	"  * clear: This command unregisters both key and any other reservation\n"
 	"    key registered with the device and drops any existing reservation.\n"},
+
+#if HAVE_DECL_IOC_PR_READ_KEYS
+	{IOC_PR_READ_KEYS,     "read-keys",
+	"  * read-keys: This command lists reservation keys currently registered\n"
+	"    with the device.\n"},
+#endif
 };
 
 static const struct type_string pr_flag[] = {
@@ -151,6 +157,41 @@ PARSE(pr_type)
 PARSE(pr_command)
 PARSE(pr_flag)
 
+#if HAVE_DECL_IOC_PR_READ_KEYS
+static int do_pr_read_keys(int fd)
+{
+	struct pr_read_keys pr_rk;
+	uint32_t num_keys = 8;
+	uint64_t *keys = NULL;
+	int ret;
+
+	/* Loop to grow keys[] until it is large enough */
+	do {
+		num_keys *= 2;
+		keys = xreallocarray(keys, num_keys, sizeof(keys[0]));
+
+		pr_rk.keys_ptr = (uintptr_t)keys;
+		pr_rk.num_keys = num_keys;
+
+		ret = ioctl(fd, IOC_PR_READ_KEYS, &pr_rk);
+		if (ret)
+			goto out;
+	} while (pr_rk.num_keys > num_keys);
+
+	if (pr_rk.num_keys) {
+		for (uint32_t i = 0; i < pr_rk.num_keys; i++) {
+			printf(_("%#" PRIx64 "\n"), (uint64_t)keys[i]);
+		}
+	} else {
+		printf(_("No registered keys\n"));
+	}
+
+out:
+	free(keys);
+	return ret;
+}
+#endif /* HAVE_DECL_IOC_PR_READ_KEYS */
+
 static int do_pr(char *path, uint64_t key, uint64_t oldkey, int op, int type, int flag)
 {
 	struct pr_registration pr_reg;
@@ -190,6 +231,11 @@ static int do_pr(char *path, uint64_t key, uint64_t oldkey, int op, int type, in
 		pr_clr.flags = flag;
 		ret = ioctl(fd, op, &pr_clr);
 		break;
+#if HAVE_DECL_IOC_PR_READ_KEYS
+	case IOC_PR_READ_KEYS:
+		ret = do_pr_read_keys(fd);
+		break;
+#endif
 	default:
 		errno = EINVAL;
 		err(EXIT_FAILURE, _("unknown command"));
diff --git a/configure.ac b/configure.ac
index a424e0947..bbd2156d7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -445,6 +445,10 @@ AC_CHECK_DECLS([PR_REP_CAPACITY], [], [], [
 	#include <linux/pr.h>
 ])
 
+AC_CHECK_DECLS([IOC_PR_READ_KEYS], [], [], [
+	#include <linux/pr.h>
+])
+
 AC_CHECK_HEADERS([security/openpam.h], [], [], [
 #ifdef HAVE_SECURITY_PAM_APPL_H
 #include <security/pam_appl.h>
diff --git a/sys-utils/blkpr.8.adoc b/sys-utils/blkpr.8.adoc
index 98983b779..3a157af38 100644
--- a/sys-utils/blkpr.8.adoc
+++ b/sys-utils/blkpr.8.adoc
@@ -25,7 +25,8 @@ The _device_ argument is the pathname of the block device.
 
 *-c*, *--command* _command_::
 The command for managing persistent reservations. Supported commands are:
-*register*, *reserve*, *release*, *preempt*, *preempt-abort*, and *clear*.
+*register*, *reserve*, *release*, *preempt*, *preempt-abort*, *clear*, and
+*read-keys*.
 
 *-k*, *--key* _key_::
 The key the command should operate on.
-- 
2.52.0


  parent reply	other threads:[~2025-12-11 16:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-11 16:09 [PATCH 0/3] blkpr: add read-keys and read-reservations commands Stefan Hajnoczi
2025-12-11 16:09 ` [PATCH 1/3] blkpr: prepare for _IOR() ioctls Stefan Hajnoczi
2025-12-11 16:09 ` Stefan Hajnoczi [this message]
2025-12-11 16:09 ` [PATCH 3/3] blkpr: add read-reservation command Stefan Hajnoczi
2025-12-17 10:53   ` Karel Zak
2025-12-17 18:26     ` Stefan Hajnoczi

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=20251211160956.1540114-3-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=hare@suse.de \
    --cc=kwolf@redhat.com \
    --cc=pizhenwei@bytedance.com \
    --cc=util-linux@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox