]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
blkpr: add read-keys command
authorStefan Hajnoczi <stefanha@redhat.com>
Wed, 17 Dec 2025 18:26:06 +0000 (13:26 -0500)
committerKarel Zak <kzak@redhat.com>
Mon, 12 Jan 2026 14:30:13 +0000 (15:30 +0100)
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>
sys-utils/blkpr.8.adoc
sys-utils/blkpr.c

index 98983b7796b458a50d5a45cef64d546f008cf83d..3a157af38749eb8a5b3815333e3cd1fab558c6ff 100644 (file)
@@ -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.
index c6b030def38f2ab82bcf10e5cc80ccc10063a742..84e736e9f1756c3047ab4ce2ded2ed3c2797093f 100644 (file)
@@ -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"},
+
+#ifdef 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)
 
+#ifdef 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 /* 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;
+#ifdef 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"));