From patchwork Tue Jun 3 10:04:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 894675 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7626B2AD02; Tue, 3 Jun 2025 10:06:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748945166; cv=none; b=RfdsdcxbUfPvyzdBVSylLfCr8MZcVgao7T72/NdJ+kavoKvyOyGwzFvql3VeRhoWMJCWmGg8MQa4F98uCN7nGDPhYc7xU0skGao+UznfQwaPM3l4WMFxZpHkug8fkMwu6c+/dHW2cL71MHqUQzH0S7qkUalMn46HSSK8iAaJO0o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748945166; c=relaxed/simple; bh=qbqxz7fvNqfbQzcGB5ZbzcH6HWNnj8SopuCcypubu9Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ikrjK/ocFUAaOoOoJ9Yr/MltBE/PSPUaatWpIJ2oqzitu7sz2JRTU0xA+fXIkORrwtw4lJY5yE+ix4uC9cufAcyaxBXjLfVR16bupqnQldQlTFN5fnlidzsM6aD0mIhtkG4ne6d0dVdBgDGhwGfEee5TLN1GL5qwRq9PQ9XkmHE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SJCF9hx0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SJCF9hx0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BF99C4CEEF; Tue, 3 Jun 2025 10:06:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1748945165; bh=qbqxz7fvNqfbQzcGB5ZbzcH6HWNnj8SopuCcypubu9Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SJCF9hx0d11D4koD/G9qAZPVNAUvmWZwS4NHAlQDjxsxuuatW6Vg5OkJZFppW6YLO SHCsTOnEAxKTs8Go/yxnj9pEt1ZolOrrzUSrV+znDiB4Mpc4RIneVYZZt1Pgum+bf1 m0jnFl2iSrn2sbB/G2e9kAfUDiycsvV4FofvuKGGUeH/uZyt94X1+spPTwoqoni0e5 58G3eSku5Uskv16/e8cLWKsZMgiaAJ1mbm1lkAJ59+bNZfEP5eMMfP/NFH/x34smi0 BUZFauzxuYezaPU4gFnbpUeITR0Mm6ENAekfRAAtczNZBeB1YSRcqfrWQytvzgnXpq SLzwOb+TgCl0w== From: Hannes Reinecke To: Jens Axboe Cc: Christoph Hellwig , "Martin K. Petersen" , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, James Bottomley , Hannes Reinecke Subject: [PATCH 1/3] block: add 'read_keys' persistent reservation ioctl Date: Tue, 3 Jun 2025 12:04:14 +0200 Message-Id: <20250603100416.131490-2-hare@kernel.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20250603100416.131490-1-hare@kernel.org> References: <20250603100416.131490-1-hare@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The persistent reservation operations already have a callback for 'read_keys', but this is not accessible via an ioctl (unlike the other callbacks). So add a new persistent reservation ioctl 'READ_KEYS' to allow userspace to use this functionality. Signed-off-by: Hannes Reinecke --- block/ioctl.c | 39 +++++++++++++++++++++++++++++++++++++++ include/linux/pr.h | 6 ------ include/uapi/linux/pr.h | 7 +++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index e472cc1030c6..71f87974e044 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -422,6 +422,43 @@ static int blkdev_pr_clear(struct block_device *bdev, blk_mode_t mode, return ops->pr_clear(bdev, c.key); } +static int blkdev_pr_read_keys(struct block_device *bdev, blk_mode_t mode, + struct pr_keys __user *arg) +{ + const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; + struct pr_keys k_in, *k_out; + size_t k_len; + int ret, num_keys; + + if (!blkdev_pr_allowed(bdev, mode)) + return -EPERM; + if (!ops || !ops->pr_read_keys) + return -EOPNOTSUPP; + + if (copy_from_user(&k_in, arg, sizeof(k_in))) + return -EFAULT; + + num_keys = k_in.num_keys; + k_out = kzalloc(sizeof(struct pr_keys) + num_keys * sizeof(u64), + GFP_KERNEL); + if (!k_out) + return -ENOMEM; + + k_out->num_keys = num_keys; + ret = ops->pr_read_keys(bdev, k_out); + if (ret) { + kfree(k_out); + return ret; + } + + k_len = sizeof(k_in) + num_keys * sizeof(u64); + if (copy_to_user(arg, k_out, k_len)) + ret = -EFAULT; + + kfree(k_out); + return ret; +} + static int blkdev_flushbuf(struct block_device *bdev, unsigned cmd, unsigned long arg) { @@ -643,6 +680,8 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, return blkdev_pr_preempt(bdev, mode, argp, true); case IOC_PR_CLEAR: return blkdev_pr_clear(bdev, mode, argp); + case IOC_PR_READ_KEYS: + return blkdev_pr_read_keys(bdev, mode, argp); default: return -ENOIOCTLCMD; } diff --git a/include/linux/pr.h b/include/linux/pr.h index 3003daec28a5..6f293c3e27a1 100644 --- a/include/linux/pr.h +++ b/include/linux/pr.h @@ -4,12 +4,6 @@ #include -struct pr_keys { - u32 generation; - u32 num_keys; - u64 keys[]; -}; - struct pr_held_reservation { u64 key; u32 generation; diff --git a/include/uapi/linux/pr.h b/include/uapi/linux/pr.h index d8126415966f..6e0d91d43c54 100644 --- a/include/uapi/linux/pr.h +++ b/include/uapi/linux/pr.h @@ -56,6 +56,12 @@ struct pr_clear { __u32 __pad; }; +struct pr_keys { + __u32 generation; + __u32 num_keys; + __u64 keys[]; +}; + #define PR_FL_IGNORE_KEY (1 << 0) /* ignore existing key */ #define IOC_PR_REGISTER _IOW('p', 200, struct pr_registration) @@ -64,5 +70,6 @@ struct pr_clear { #define IOC_PR_PREEMPT _IOW('p', 203, struct pr_preempt) #define IOC_PR_PREEMPT_ABORT _IOW('p', 204, struct pr_preempt) #define IOC_PR_CLEAR _IOW('p', 205, struct pr_clear) +#define IOC_PR_READ_KEYS _IOWR('p', 206, struct pr_keys) #endif /* _UAPI_PR_H */ From patchwork Tue Jun 3 10:04:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 893984 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 997992AD02; Tue, 3 Jun 2025 10:06:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748945167; cv=none; b=niUDNCIZcnSiBg3G+meClaS6zisKYdNByPCJysNJzVUVvJQASgHDLqUzPwoP9TXqcewMB0S+nPjyLFnlQTfHLqGE/fU1ryro6lCtQMFXeFRbPxsNlLb7MKiQKeOMfG9k+z8RPM9sa8I9+WnigLRE1H1Vwgflvq8m0v1VOwe+DPc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748945167; c=relaxed/simple; bh=GLHpXmqM1IZutIkryodASrssO2tP1+XsYj3UZKfpBX0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=j5ZcacLC9tJ+wX0djNY6nfhuSbtZD75bllx3TDxxJdYkNIJ+hO5FMLXAPRZS98RGGj6Omw6paZsXp+n7doXqRoP1uDjYOsxAAFYgHoFrA9BrSF10BEFJxi9NIHP7cUY6nAY0Pri3c+c5xu9NKmcUahn2hhs1J8kR6D+6AMNyCgA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=U02SJs3R; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="U02SJs3R" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6F506C4CEF0; Tue, 3 Jun 2025 10:06:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1748945167; bh=GLHpXmqM1IZutIkryodASrssO2tP1+XsYj3UZKfpBX0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U02SJs3RLq44jfJsVitxNDnQCsPRxBtfrid8fsMU624GyNO7/NDbUAhl5QIPbfWjf NbO30vCnuQSjGp2FCJjMn6zB/xQhePc9QtmDWWf28amavqNNlMOCHBVFefuSaTwH16 NV1Ddj82i8U5yUZtO2HU6Ir8uG82osNqBaq5GkHDjmAasXSz8lJsIKwDq+4C44DuLy oWTnpELcyILuuDh+XYCJbI646uJJuYjkE+cghfSzEaoM6XwXXzXUvilUQH0vMCWx3B mvy7HRhdlyu9MulxruommbfQWMyteJTBR8+LnOS6y/GGN8uztOJPNmxJX2z5GXOPs/ KSbO1LU8Hn/5g== From: Hannes Reinecke To: Jens Axboe Cc: Christoph Hellwig , "Martin K. Petersen" , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, James Bottomley , Hannes Reinecke Subject: [PATCH 2/3] block: add 'read_reservation' persistent reservation ioctl Date: Tue, 3 Jun 2025 12:04:15 +0200 Message-Id: <20250603100416.131490-3-hare@kernel.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20250603100416.131490-1-hare@kernel.org> References: <20250603100416.131490-1-hare@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The persistent reservation operations already have a callback for 'read_reservation', but this is not accessible via an ioctl (unlike the other callbacks). So add a new persistent reservation ioctl 'READ_RESV' to allow userspace to use this functionality. Signed-off-by: Hannes Reinecke --- block/ioctl.c | 22 ++++++++++++++++++++++ include/linux/pr.h | 6 ------ include/uapi/linux/pr.h | 7 +++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index 71f87974e044..3612352a1d83 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -459,6 +459,26 @@ static int blkdev_pr_read_keys(struct block_device *bdev, blk_mode_t mode, return ret; } +static int blkdev_pr_read_reservation(struct block_device *bdev, + blk_mode_t mode, struct pr_held_reservation __user *arg) +{ + const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops; + struct pr_held_reservation r; + int ret; + + if (!blkdev_pr_allowed(bdev, mode)) + return -EPERM; + if (!ops || !ops->pr_read_reservation) + return -EOPNOTSUPP; + + ret = ops->pr_read_reservation(bdev, &r); + if (ret) + return ret; + if (copy_to_user(arg, &r, sizeof(r))) + return -EFAULT; + return 0; +} + static int blkdev_flushbuf(struct block_device *bdev, unsigned cmd, unsigned long arg) { @@ -682,6 +702,8 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, return blkdev_pr_clear(bdev, mode, argp); case IOC_PR_READ_KEYS: return blkdev_pr_read_keys(bdev, mode, argp); + case IOC_PR_READ_RESV: + return blkdev_pr_read_reservation(bdev, mode, argp); default: return -ENOIOCTLCMD; } diff --git a/include/linux/pr.h b/include/linux/pr.h index 6f293c3e27a1..e3ccfbd9dfba 100644 --- a/include/linux/pr.h +++ b/include/linux/pr.h @@ -4,12 +4,6 @@ #include -struct pr_held_reservation { - u64 key; - u32 generation; - enum pr_type type; -}; - struct pr_ops { int (*pr_register)(struct block_device *bdev, u64 old_key, u64 new_key, u32 flags); diff --git a/include/uapi/linux/pr.h b/include/uapi/linux/pr.h index 6e0d91d43c54..20198c7e68e8 100644 --- a/include/uapi/linux/pr.h +++ b/include/uapi/linux/pr.h @@ -62,6 +62,12 @@ struct pr_keys { __u64 keys[]; }; +struct pr_held_reservation { + __u64 key; + __u32 generation; + __u32 type; +}; + #define PR_FL_IGNORE_KEY (1 << 0) /* ignore existing key */ #define IOC_PR_REGISTER _IOW('p', 200, struct pr_registration) @@ -71,5 +77,6 @@ struct pr_keys { #define IOC_PR_PREEMPT_ABORT _IOW('p', 204, struct pr_preempt) #define IOC_PR_CLEAR _IOW('p', 205, struct pr_clear) #define IOC_PR_READ_KEYS _IOWR('p', 206, struct pr_keys) +#define IOC_PR_READ_RESV _IOWR('p', 207, struct pr_held_reservation) #endif /* _UAPI_PR_H */ From patchwork Tue Jun 3 10:04:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 894674 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2DB22AD02; Tue, 3 Jun 2025 10:06:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748945169; cv=none; b=hvJJ5Sd3wjNHY1eDgTiJA94BUEC+Ezk+8IDs9Iy6eASTXmwsXoCP4J7zf4BT4FZzaKV1X+FrWk8/Q/Awx515RPPm74Pjcnq2m4ngU5on/pAvupT+Aia5dxuw3X0CctQ+M9DpIQ0pNYrYx95ffqQJuIUV71KMDfCK9aYkVOqRi7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748945169; c=relaxed/simple; bh=Fxuz0OajvxuzpLg2IerPHvE8qh8nzI9KPaajqSoERQQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QUJlnBfRGj0uUT/yaBpJdmmLQOO9+QtwBBlHkN4Kc+8m8QaiF3wrfh0JsG9Os2YSUmFarqFUHcxzDHZArEciD7XVM8T7gKfhcEk3kX35yYSNOgMVcInkBO8zaNRITxJAJbADKxToRCrQg04VFeUSwaIZlyRDKBsoUW6LelxHMtU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ouz+gu00; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ouz+gu00" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8240FC4CEEF; Tue, 3 Jun 2025 10:06:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1748945169; bh=Fxuz0OajvxuzpLg2IerPHvE8qh8nzI9KPaajqSoERQQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ouz+gu00j8EmYRCoB0wZRLuGqX4oFCm905Z7iNNKKWmfl1DxwXILvyZaTK36BEVqc 8bDawuKsPyL8RappnT2sR/h+p1qHhxlVe9CqjdarmfiVbE9hqXhXx3jy+mTqCtO4Ga AajBJ3BaV/6wwn2BaX+GoRylij/SO7VHu+XlPRBv0tHql+LXP8bf7Fd5Nmf7ZzWIN+ jw5suWauAHLA7094p/C+bZ2ESGvf/Ol3EFk8qkXgD9frOE/TQTOuqmEtsApPIno+uH r7pN1tdvzFP2gZzTIs5xP7/JF1Mq0k78wXqpukoq3TzfQUYASY/bTz3KeIX52eOsN9 lvLRwOYcU0C6A== From: Hannes Reinecke To: Jens Axboe Cc: Christoph Hellwig , "Martin K. Petersen" , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, James Bottomley , Hannes Reinecke Subject: [PATCH 3/3] scsi: return PR generation if no reservation is held Date: Tue, 3 Jun 2025 12:04:16 +0200 Message-Id: <20250603100416.131490-4-hare@kernel.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20250603100416.131490-1-hare@kernel.org> References: <20250603100416.131490-1-hare@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For READ RESERVATION the PRgeneration value is always present, even if no reservations are held. Signed-off-by: Hannes Reinecke --- drivers/scsi/sd.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 950d8c9fb884..daa61feaf441 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -2008,9 +2008,13 @@ static int sd_pr_read_reservation(struct block_device *bdev, if (result) return result; + rsv->generation = get_unaligned_be32(&data[0]); + rsv->type = 0; len = get_unaligned_be32(&data[4]); - if (!len) + if (!len) { + rsv->key = 0; return 0; + } /* Make sure we have at least the key and type */ if (len < 14) { @@ -2020,9 +2024,9 @@ static int sd_pr_read_reservation(struct block_device *bdev, return -EINVAL; } - rsv->generation = get_unaligned_be32(&data[0]); rsv->key = get_unaligned_be64(&data[8]); - rsv->type = scsi_pr_type_to_block(data[21] & 0x0f); + if (len == 16) + rsv->type = scsi_pr_type_to_block(data[21] & 0x0f); return 0; }