From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493296; cv=none; d=zohomail.com; s=zohoarc; b=RbkMrUbIPXhZ3/8SOZfZL1MocK/aIQCbQL+jbfyc2fUG+iY0QKUiIZ63HGoAiAnY11B+Goc/LJ1Psj/TkoxV3Rge4HmzBDGXmutcZ1gChsNjxz/rmdIG2md4RiHhmIqZVy6toxi0EG2s7mvXeHc9c+LKSGIzQ3oNIvag9And1Mo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493296; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kjAdu6ZTaJ2H/T12j7/Y4iOTA7PHN1pPSyvKcs9msGY=; b=MXiaMjL/yp3eInY4vEILDgENKsEkbCxT8sRC6HWghpCTUBEnPxLR7FNvluMgnrmdK7LgNQfrutMivOSc5B7cgYVUKQ3Rdc/z+0uvvxz7Kj8NQV24RETgAUo5QF4+LQ2rxx05uG2lKKMa8EVYysmcxsn01QT5CGKvVgPbJw8aG5k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720493296542603.2823873640805; Mon, 8 Jul 2024 19:48:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0tB-000616-9n; Mon, 08 Jul 2024 22:47:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0t9-000600-TE for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:31 -0400 Received: from mail-oo1-xc34.google.com ([2607:f8b0:4864:20::c34]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0t7-0003oP-1x for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:31 -0400 Received: by mail-oo1-xc34.google.com with SMTP id 006d021491bc7-5c670e5469aso1031476eaf.1 for ; Mon, 08 Jul 2024 19:47:28 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.47.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:47:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493247; x=1721098047; darn=nongnu.org; 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; bh=kjAdu6ZTaJ2H/T12j7/Y4iOTA7PHN1pPSyvKcs9msGY=; b=HD77NH+VelMJrOVwet+FUrFhNaIyIn4IqCppJO1H1miAB/7eYz2EPrpdBKYMndMzx+ ZvOTzDv4GTyr20ZCrLU2PbssmYLKKdvQp4xqrVcqjfSegA7AAoPUxiGPgWSX1SA61z6v 09mx5XHjj+pZvBDKkitQ13qnyDIiT8sc3AUZCEH/62/H4du+ARA9DU26a1HXRo6LTYEc RB3zzH92uqq1LXt/uOWR4ihW8iUbWh+Oae2vpEBukTwX+bbta7TXTuHSBYdQ11kuu7UB WW5VOKcWp7Io9YQfG/9UehRD6GtoR1efJqq7rUvITvwhTmCXSEh84MEl4uBxvdxJtUWs bwUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493247; x=1721098047; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kjAdu6ZTaJ2H/T12j7/Y4iOTA7PHN1pPSyvKcs9msGY=; b=uzMHpQnY1vuIpIwQpR2DMgzLebaOlMETKwkvXTv4Siox9oAMNDy+6HSPhsJuW/arIR tMnfCxX7fWfbH5R0AaSc5PAy+4KAZHYI41U+T5QUV1MyiENNjErkmXE/cXCI86Dx+zOt rvFRP+x0m2PWGV6T5dyY3r4iivtDsCK7AidDOxC1TmlStdoX5ik29phJT+68b3HYbxYI T7gmpPJxyX60RMEcz59xCvwoJAYFnX/fZW/4IJMLIFJg88BRJTwBNynfVjje0y3IYc8Z TcYhchCUp4rUVfI5ECHwl5axrjHaWB1cX7eWV0NvqI+gp4peECR8iX9r8kkfhEqFCd5N H31g== X-Forwarded-Encrypted: i=1; AJvYcCV3t4HRyXEznY4++NknePD8wcNYWkZPLYjV9ceu0E2vb54mLmnA4yYWaUBUzVIs4ZcdoV9T4xhNu88jL86O5vUZ0isDpWM= X-Gm-Message-State: AOJu0YzDWsaPlRuDd5miwUgTNrAfbAR3NpU+tRnu+fhDmcrlzKRm71k8 ahM0eP7nsQzHUesxHjI+PeKca9pxQrLXwSJhhsdprHsdlmTgUtR9KdHQtD7TN2s= X-Google-Smtp-Source: AGHT+IGuxIlv2n+wPUC1s6ApdEvC4a/+okNoNzAo/vR9QUCKxRAQM8IqHLbTUVsa9yRbUw6LRhrlfg== X-Received: by 2002:a05:6358:5920:b0:1aa:c73d:5a94 with SMTP id e5c5f4694b2df-1aade299e3emr124337055d.16.1720493247351; Mon, 08 Jul 2024 19:47:27 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu Subject: [PATCH v8 01/10] block: add persistent reservation in/out api Date: Tue, 9 Jul 2024 10:46:57 +0800 Message-Id: <20240709024706.4108-2-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::c34; envelope-from=luchangqi.123@bytedance.com; helo=mail-oo1-xc34.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493297955100003 Content-Type: text/plain; charset="utf-8" Add persistent reservation in/out operations at the block level. The following operations are included: - read_keys: retrieves the list of registered keys. - read_reservation: retrieves the current reservation status. - register: registers a new reservation key. - reserve: initiates a reservation for a specific key. - release: releases a reservation for a specific key. - clear: clears all existing reservations. - preempt: preempts a reservation held by another key. Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi Reviewed-by: Stefan Hajnoczi --- block/block-backend.c | 403 ++++++++++++++++++++++++++++++ block/io.c | 164 ++++++++++++ include/block/block-common.h | 40 +++ include/block/block-io.h | 20 ++ include/block/block_int-common.h | 84 +++++++ include/sysemu/block-backend-io.h | 24 ++ 6 files changed, 735 insertions(+) diff --git a/block/block-backend.c b/block/block-backend.c index db6f9b92a3..b74aaba23f 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1770,6 +1770,409 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsign= ed long int req, void *buf, return blk_aio_prwv(blk, req, 0, buf, blk_aio_ioctl_entry, 0, cb, opaq= ue); } =20 +typedef struct BlkPrInCo { + BlockBackend *blk; + uint32_t *generation; + uint32_t num_keys; + BlockPrType *type; + uint64_t *keys; + int ret; +} BlkPrInCo; + +typedef struct BlkPrInCB { + BlockAIOCB common; + BlkPrInCo prco; + bool has_returned; +} BlkPrInCB; + +static const AIOCBInfo blk_pr_in_aiocb_info =3D { + .aiocb_size =3D sizeof(BlkPrInCB), +}; + +static void blk_pr_in_complete(BlkPrInCB *acb) +{ + if (acb->has_returned) { + acb->common.cb(acb->common.opaque, acb->prco.ret); + + /* This is paired with blk_inc_in_flight() in blk_aio_pr_in(). */ + blk_dec_in_flight(acb->prco.blk); + qemu_aio_unref(acb); + } +} + +static void blk_pr_in_complete_bh(void *opaque) +{ + BlkPrInCB *acb =3D opaque; + assert(acb->has_returned); + blk_pr_in_complete(acb); +} + +static BlockAIOCB *blk_aio_pr_in(BlockBackend *blk, uint32_t *generation, + uint32_t num_keys, BlockPrType *type, + uint64_t *keys, CoroutineEntry co_entry, + BlockCompletionFunc *cb, void *opaque) +{ + BlkPrInCB *acb; + Coroutine *co; + + /* This is paired with blk_dec_in_flight() in blk_pr_in_complete(). */ + blk_inc_in_flight(blk); + acb =3D blk_aio_get(&blk_pr_in_aiocb_info, blk, cb, opaque); + acb->prco =3D (BlkPrInCo) { + .blk =3D blk, + .generation =3D generation, + .num_keys =3D num_keys, + .type =3D type, + .ret =3D NOT_DONE, + .keys =3D keys, + }; + acb->has_returned =3D false; + + co =3D qemu_coroutine_create(co_entry, acb); + aio_co_enter(qemu_get_current_aio_context(), co); + + acb->has_returned =3D true; + if (acb->prco.ret !=3D NOT_DONE) { + replay_bh_schedule_oneshot_event(qemu_get_current_aio_context(), + blk_pr_in_complete_bh, acb); + } + + return &acb->common; +} + +/* To be called between exactly one pair of blk_inc/dec_in_flight() */ +static int coroutine_fn +blk_aio_pr_do_read_keys(BlockBackend *blk, uint32_t *generation, + uint32_t num_keys, uint64_t *keys) +{ + IO_CODE(); + + blk_wait_while_drained(blk); + GRAPH_RDLOCK_GUARD(); + + if (!blk_co_is_available(blk)) { + return -ENOMEDIUM; + } + + return bdrv_co_pr_read_keys(blk_bs(blk), generation, num_keys, keys); +} + +static void coroutine_fn blk_aio_pr_read_keys_entry(void *opaque) +{ + BlkPrInCB *acb =3D opaque; + BlkPrInCo *prco =3D &acb->prco; + + prco->ret =3D blk_aio_pr_do_read_keys(prco->blk, prco->generation, + prco->num_keys, prco->keys); + blk_pr_in_complete(acb); +} + +BlockAIOCB *blk_aio_pr_read_keys(BlockBackend *blk, uint32_t *generation, + uint32_t num_keys, uint64_t *keys, + BlockCompletionFunc *cb, void *opaque) +{ + IO_CODE(); + return blk_aio_pr_in(blk, generation, num_keys, NULL, keys, + blk_aio_pr_read_keys_entry, cb, opaque); +} + +/* To be called between exactly one pair of blk_inc/dec_in_flight() */ +static int coroutine_fn +blk_aio_pr_do_read_reservation(BlockBackend *blk, uint32_t *generation, + uint64_t *key, BlockPrType *type) +{ + IO_CODE(); + + blk_wait_while_drained(blk); + GRAPH_RDLOCK_GUARD(); + + if (!blk_co_is_available(blk)) { + return -ENOMEDIUM; + } + + return bdrv_co_pr_read_reservation(blk_bs(blk), generation, key, type); +} + +static void coroutine_fn blk_aio_pr_read_reservation_entry(void *opaque) +{ + BlkPrInCB *acb =3D opaque; + BlkPrInCo *prco =3D &acb->prco; + + prco->ret =3D blk_aio_pr_do_read_reservation(prco->blk, prco->generati= on, + prco->keys, prco->type); + blk_pr_in_complete(acb); +} + +BlockAIOCB *blk_aio_pr_read_reservation(BlockBackend *blk, uint32_t *gener= ation, + uint64_t *key, BlockPrType *type, + BlockCompletionFunc *cb, void *opa= que) +{ + IO_CODE(); + return blk_aio_pr_in(blk, generation, 0, type, key, + blk_aio_pr_read_reservation_entry, cb, opaque); +} + +typedef struct BlkPrOutCo { + BlockBackend *blk; + uint64_t old_key; + uint64_t new_key; + bool ptpl; + BlockPrType type; + bool ignore_key; + bool abort; + int ret; +} BlkPrOutCo; + +typedef struct BlkPrOutCB { + BlockAIOCB common; + BlkPrOutCo prco; + bool has_returned; +} BlkPrOutCB; + +static const AIOCBInfo blk_pr_out_aiocb_info =3D { + .aiocb_size =3D sizeof(BlkPrOutCB), +}; + +static void blk_pr_out_complete(BlkPrOutCB *acb) +{ + if (acb->has_returned) { + acb->common.cb(acb->common.opaque, acb->prco.ret); + + /* This is paired with blk_inc_in_flight() in blk_aio_pr_out(). */ + blk_dec_in_flight(acb->prco.blk); + qemu_aio_unref(acb); + } +} + +static void blk_pr_out_complete_bh(void *opaque) +{ + BlkPrOutCB *acb =3D opaque; + assert(acb->has_returned); + blk_pr_out_complete(acb); +} + +static BlockAIOCB *blk_aio_pr_out(BlockBackend *blk, uint64_t old_key, + uint64_t new_key, bool ptpl, + BlockPrType type, bool ignore_key, + bool abort, CoroutineEntry co_entry, + BlockCompletionFunc *cb, void *opaque) +{ + BlkPrOutCB *acb; + Coroutine *co; + + /* This is paired with blk_dec_in_flight() in blk_pr_out_complete(). */ + blk_inc_in_flight(blk); + acb =3D blk_aio_get(&blk_pr_out_aiocb_info, blk, cb, opaque); + acb->prco =3D (BlkPrOutCo) { + .blk =3D blk, + .old_key =3D old_key, + .new_key =3D new_key, + .ptpl =3D ptpl, + .type =3D type, + .ignore_key =3D ignore_key, + .abort =3D abort, + .ret =3D NOT_DONE, + }; + acb->has_returned =3D false; + + co =3D qemu_coroutine_create(co_entry, acb); + aio_co_enter(qemu_get_current_aio_context(), co); + + acb->has_returned =3D true; + if (acb->prco.ret !=3D NOT_DONE) { + replay_bh_schedule_oneshot_event(qemu_get_current_aio_context(), + blk_pr_out_complete_bh, acb); + } + + return &acb->common; +} + +/* To be called between exactly one pair of blk_inc/dec_in_flight() */ +static int coroutine_fn +blk_aio_pr_do_register(BlockBackend *blk, uint64_t old_key, + uint64_t new_key, BlockPrType type, + bool ptpl, bool ignore_key) +{ + IO_CODE(); + + blk_wait_while_drained(blk); + GRAPH_RDLOCK_GUARD(); + + if (!blk_co_is_available(blk)) { + return -ENOMEDIUM; + } + + return bdrv_co_pr_register(blk_bs(blk), old_key, new_key, type, + ptpl, ignore_key); +} + +static void coroutine_fn blk_aio_pr_register_entry(void *opaque) +{ + BlkPrOutCB *acb =3D opaque; + BlkPrOutCo *prco =3D &acb->prco; + + prco->ret =3D blk_aio_pr_do_register(prco->blk, prco->old_key, prco->n= ew_key, + prco->type, prco->ptpl, + prco->ignore_key); + blk_pr_out_complete(acb); +} + +BlockAIOCB *blk_aio_pr_register(BlockBackend *blk, uint64_t old_key, + uint64_t new_key, BlockPrType type, + bool ptpl, bool ignore_key, + BlockCompletionFunc *cb, + void *opaque) +{ + IO_CODE(); + return blk_aio_pr_out(blk, old_key, new_key, ptpl, type, ignore_key, f= alse, + blk_aio_pr_register_entry, cb, opaque); +} + +/* To be called between exactly one pair of blk_inc/dec_in_flight() */ +static int coroutine_fn +blk_aio_pr_do_reserve(BlockBackend *blk, uint64_t key, BlockPrType type) +{ + IO_CODE(); + + blk_wait_while_drained(blk); + GRAPH_RDLOCK_GUARD(); + + if (!blk_co_is_available(blk)) { + return -ENOMEDIUM; + } + + return bdrv_co_pr_reserve(blk_bs(blk), key, type); +} + +static void coroutine_fn blk_aio_pr_reserve_entry(void *opaque) +{ + BlkPrOutCB *acb =3D opaque; + BlkPrOutCo *prco =3D &acb->prco; + + prco->ret =3D blk_aio_pr_do_reserve(prco->blk, prco->old_key, + prco->type); + blk_pr_out_complete(acb); +} + + +BlockAIOCB *blk_aio_pr_reserve(BlockBackend *blk, uint64_t key, + BlockPrType type, + BlockCompletionFunc *cb, + void *opaque) +{ + IO_CODE(); + return blk_aio_pr_out(blk, key, 0, false, type, false, false, + blk_aio_pr_reserve_entry, cb, opaque); +} + +/* To be called between exactly one pair of blk_inc/dec_in_flight() */ +static int coroutine_fn +blk_aio_pr_do_release(BlockBackend *blk, uint64_t key, BlockPrType type) +{ + IO_CODE(); + + blk_wait_while_drained(blk); + GRAPH_RDLOCK_GUARD(); + + if (!blk_co_is_available(blk)) { + return -ENOMEDIUM; + } + + return bdrv_co_pr_release(blk_bs(blk), key, type); +} + +static void coroutine_fn blk_aio_pr_release_entry(void *opaque) +{ + BlkPrOutCB *acb =3D opaque; + BlkPrOutCo *prco =3D &acb->prco; + + prco->ret =3D blk_aio_pr_do_release(prco->blk, prco->old_key, prco->ty= pe); + blk_pr_out_complete(acb); +} + + +BlockAIOCB *blk_aio_pr_release(BlockBackend *blk, uint64_t key, + BlockPrType type, BlockCompletionFunc *cb, + void *opaque) +{ + IO_CODE(); + return blk_aio_pr_out(blk, key, 0, false, type, false, false, + blk_aio_pr_release_entry, cb, opaque); +} + +/* To be called between exactly one pair of blk_inc/dec_in_flight() */ +static int coroutine_fn +blk_aio_pr_do_clear(BlockBackend *blk, uint64_t key) +{ + IO_CODE(); + + blk_wait_while_drained(blk); + GRAPH_RDLOCK_GUARD(); + + if (!blk_co_is_available(blk)) { + return -ENOMEDIUM; + } + + return bdrv_co_pr_clear(blk_bs(blk), key); +} + +static void coroutine_fn blk_aio_pr_clear_entry(void *opaque) +{ + BlkPrOutCB *acb =3D opaque; + BlkPrOutCo *prco =3D &acb->prco; + + prco->ret =3D blk_aio_pr_do_clear(prco->blk, prco->old_key); + blk_pr_out_complete(acb); +} + + +BlockAIOCB *blk_aio_pr_clear(BlockBackend *blk, uint64_t key, + BlockCompletionFunc *cb, void *opaque) +{ + IO_CODE(); + return blk_aio_pr_out(blk, key, 0, false, 0, false, false, + blk_aio_pr_clear_entry, cb, opaque); +} + +/* To be called between exactly one pair of blk_inc/dec_in_flight() */ +static int coroutine_fn +blk_aio_pr_do_preempt(BlockBackend *blk, uint64_t cr_key, + uint64_t pr_key, BlockPrType type, bool abort) +{ + IO_CODE(); + + blk_wait_while_drained(blk); + GRAPH_RDLOCK_GUARD(); + + if (!blk_co_is_available(blk)) { + return -ENOMEDIUM; + } + + return bdrv_co_pr_preempt(blk_bs(blk), cr_key, pr_key, type, abort); +} + +static void coroutine_fn blk_aio_pr_preempt_entry(void *opaque) +{ + BlkPrOutCB *acb =3D opaque; + BlkPrOutCo *prco =3D &acb->prco; + + prco->ret =3D blk_aio_pr_do_preempt(prco->blk, prco->old_key, + prco->new_key, prco->type, + prco->abort); + blk_pr_out_complete(acb); +} + + +BlockAIOCB *blk_aio_pr_preempt(BlockBackend *blk, uint64_t cr_key, + uint64_t pr_key, BlockPrType type, + bool abort, BlockCompletionFunc *cb, + void *opaque) +{ + IO_CODE(); + return blk_aio_pr_out(blk, cr_key, pr_key, false, type, false, abort, + blk_aio_pr_preempt_entry, cb, opaque); +} + /* To be called between exactly one pair of blk_inc/dec_in_flight() */ static int coroutine_fn blk_co_do_pdiscard(BlockBackend *blk, int64_t offset, int64_t bytes) diff --git a/block/io.c b/block/io.c index 7217cf811b..105161930e 100644 --- a/block/io.c +++ b/block/io.c @@ -146,6 +146,7 @@ static void bdrv_merge_limits(BlockLimits *dst, const B= lockLimits *src) src->min_mem_alignment); dst->max_iov =3D MIN_NON_ZERO(dst->max_iov, src->max_iov); dst->max_hw_iov =3D MIN_NON_ZERO(dst->max_hw_iov, src->max_hw_iov); + dst->pr_cap |=3D src->pr_cap; } =20 typedef struct BdrvRefreshLimitsState { @@ -3220,6 +3221,169 @@ out: return co.ret; } =20 +int coroutine_fn bdrv_co_pr_read_keys(BlockDriverState *bs, + uint32_t *generation, uint32_t num_keys, + uint64_t *keys) +{ + BlockDriver *drv =3D bs->drv; + CoroutineIOCompletion co =3D { + .coroutine =3D qemu_coroutine_self(), + }; + + IO_CODE(); + assert_bdrv_graph_readable(); + + bdrv_inc_in_flight(bs); + if (!drv || !drv->bdrv_co_pr_read_keys) { + co.ret =3D -ENOTSUP; + goto out; + } + + co.ret =3D drv->bdrv_co_pr_read_keys(bs, generation, num_keys, keys); +out: + bdrv_dec_in_flight(bs); + return co.ret; +} + +int coroutine_fn bdrv_co_pr_read_reservation(BlockDriverState *bs, + uint32_t *generation, uint64_t *key, BlockPrType *type) +{ + BlockDriver *drv =3D bs->drv; + CoroutineIOCompletion co =3D { + .coroutine =3D qemu_coroutine_self(), + }; + + IO_CODE(); + assert_bdrv_graph_readable(); + + bdrv_inc_in_flight(bs); + if (!drv || !drv->bdrv_co_pr_read_reservation) { + co.ret =3D -ENOTSUP; + goto out; + } + + co.ret =3D drv->bdrv_co_pr_read_reservation(bs, generation, key, type); +out: + bdrv_dec_in_flight(bs); + return co.ret; +} + +int coroutine_fn bdrv_co_pr_register(BlockDriverState *bs, uint64_t old_ke= y, + uint64_t new_key, BlockPrType type, bool ptpl, + bool ignore_key) +{ + BlockDriver *drv =3D bs->drv; + CoroutineIOCompletion co =3D { + .coroutine =3D qemu_coroutine_self(), + }; + + IO_CODE(); + assert_bdrv_graph_readable(); + + bdrv_inc_in_flight(bs); + if (!drv || !drv->bdrv_co_pr_register) { + co.ret =3D -ENOTSUP; + goto out; + } + + co.ret =3D drv->bdrv_co_pr_register(bs, old_key, new_key, type, + ptpl, ignore_key); +out: + bdrv_dec_in_flight(bs); + return co.ret; +} + +int coroutine_fn bdrv_co_pr_reserve(BlockDriverState *bs, uint64_t key, + BlockPrType type) +{ + BlockDriver *drv =3D bs->drv; + CoroutineIOCompletion co =3D { + .coroutine =3D qemu_coroutine_self(), + }; + + IO_CODE(); + assert_bdrv_graph_readable(); + + bdrv_inc_in_flight(bs); + if (!drv || !drv->bdrv_co_pr_reserve) { + co.ret =3D -ENOTSUP; + goto out; + } + + co.ret =3D drv->bdrv_co_pr_reserve(bs, key, type); +out: + bdrv_dec_in_flight(bs); + return co.ret; +} + +int coroutine_fn bdrv_co_pr_release(BlockDriverState *bs, uint64_t key, + BlockPrType type) +{ + BlockDriver *drv =3D bs->drv; + CoroutineIOCompletion co =3D { + .coroutine =3D qemu_coroutine_self(), + }; + + IO_CODE(); + assert_bdrv_graph_readable(); + + bdrv_inc_in_flight(bs); + if (!drv || !drv->bdrv_co_pr_release) { + co.ret =3D -ENOTSUP; + goto out; + } + + co.ret =3D drv->bdrv_co_pr_release(bs, key, type); +out: + bdrv_dec_in_flight(bs); + return co.ret; +} + +int coroutine_fn bdrv_co_pr_clear(BlockDriverState *bs, uint64_t key) +{ + BlockDriver *drv =3D bs->drv; + CoroutineIOCompletion co =3D { + .coroutine =3D qemu_coroutine_self(), + }; + + IO_CODE(); + assert_bdrv_graph_readable(); + + bdrv_inc_in_flight(bs); + if (!drv || !drv->bdrv_co_pr_clear) { + co.ret =3D -ENOTSUP; + goto out; + } + + co.ret =3D drv->bdrv_co_pr_clear(bs, key); +out: + bdrv_dec_in_flight(bs); + return co.ret; +} + +int coroutine_fn bdrv_co_pr_preempt(BlockDriverState *bs, uint64_t cr_key, + uint64_t pr_key, BlockPrType type, bool abort) +{ + BlockDriver *drv =3D bs->drv; + CoroutineIOCompletion co =3D { + .coroutine =3D qemu_coroutine_self(), + }; + + IO_CODE(); + assert_bdrv_graph_readable(); + + bdrv_inc_in_flight(bs); + if (!drv || !drv->bdrv_co_pr_preempt) { + co.ret =3D -ENOTSUP; + goto out; + } + + co.ret =3D drv->bdrv_co_pr_preempt(bs, cr_key, pr_key, type, abort); +out: + bdrv_dec_in_flight(bs); + return co.ret; +} + int coroutine_fn bdrv_co_zone_report(BlockDriverState *bs, int64_t offset, unsigned int *nr_zones, BlockZoneDescriptor *zones) diff --git a/include/block/block-common.h b/include/block/block-common.h index a846023a09..7ca4e2328f 100644 --- a/include/block/block-common.h +++ b/include/block/block-common.h @@ -524,6 +524,46 @@ typedef enum { BDRV_FIX_ERRORS =3D 2, } BdrvCheckMode; =20 +/** + * According SCSI protocol(chapter 5.9 of SCSI Primary Commands - 4) + * and NVMe protocol(chapter 7.2 of NVMe Base Specification 2.0), + * the persistent reservation types and persistent capabilities of + * the public layer block are abstracted. + */ +typedef enum { + BLK_PR_WRITE_EXCLUSIVE =3D 0x1, + BLK_PR_EXCLUSIVE_ACCESS =3D 0x2, + BLK_PR_WRITE_EXCLUSIVE_REGS_ONLY =3D 0x3, + BLK_PR_EXCLUSIVE_ACCESS_REGS_ONLY =3D 0x4, + BLK_PR_WRITE_EXCLUSIVE_ALL_REGS =3D 0x5, + BLK_PR_EXCLUSIVE_ACCESS_ALL_REGS =3D 0x6, +} BlockPrType; + +typedef enum BLKPrCap { + /* Persist Through Power Loss */ + BLK_PR_CAP_PTPL =3D 1 << 0, + /* Write Exclusive reservation type */ + BLK_PR_CAP_WR_EX =3D 1 << 1, + /* Exclusive Access reservation type */ + BLK_PR_CAP_EX_AC =3D 1 << 2, + /* Write Exclusive Registrants Only reservation type */ + BLK_PR_CAP_WR_EX_RO =3D 1 << 3, + /* Exclusive Access Registrants Only reservation type */ + BLK_PR_CAP_EX_AC_RO =3D 1 << 4, + /* Write Exclusive All Registrants reservation type */ + BLK_PR_CAP_WR_EX_AR =3D 1 << 5, + /* Exclusive Access All Registrants reservation type */ + BLK_PR_CAP_EX_AC_AR =3D 1 << 6, + + BLK_PR_CAP_ALL =3D (BLK_PR_CAP_PTPL | + BLK_PR_CAP_WR_EX | + BLK_PR_CAP_EX_AC | + BLK_PR_CAP_WR_EX_RO | + BLK_PR_CAP_EX_AC_RO | + BLK_PR_CAP_WR_EX_AR | + BLK_PR_CAP_EX_AC_AR), +} BLKPrCap; + typedef struct BlockSizes { uint32_t phys; uint32_t log; diff --git a/include/block/block-io.h b/include/block/block-io.h index b49e0537dd..908361862b 100644 --- a/include/block/block-io.h +++ b/include/block/block-io.h @@ -106,6 +106,26 @@ void bdrv_aio_cancel_async(BlockAIOCB *acb); int coroutine_fn GRAPH_RDLOCK bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf); =20 +int coroutine_fn GRAPH_RDLOCK +bdrv_co_pr_read_keys(BlockDriverState *bs, uint32_t *generation, + uint32_t num_keys, uint64_t *keys); +int coroutine_fn GRAPH_RDLOCK +bdrv_co_pr_read_reservation(BlockDriverState *bs, uint32_t *generation, + uint64_t *key, BlockPrType *type); +int coroutine_fn GRAPH_RDLOCK +bdrv_co_pr_register(BlockDriverState *bs, uint64_t old_key, + uint64_t new_key, BlockPrType type, + bool ptpl, bool ignore_key); +int coroutine_fn GRAPH_RDLOCK +bdrv_co_pr_reserve(BlockDriverState *bs, uint64_t key, BlockPrType type); +int coroutine_fn GRAPH_RDLOCK +bdrv_co_pr_release(BlockDriverState *bs, uint64_t key, BlockPrType type); +int coroutine_fn GRAPH_RDLOCK +bdrv_co_pr_clear(BlockDriverState *bs, uint64_t key); +int coroutine_fn GRAPH_RDLOCK +bdrv_co_pr_preempt(BlockDriverState *bs, uint64_t cr_key, uint64_t pr_key, + BlockPrType type, bool abort); + /* Ensure contents are flushed to disk. */ int coroutine_fn GRAPH_RDLOCK bdrv_co_flush(BlockDriverState *bs); =20 diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 761276127e..6e628069e9 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -766,6 +766,87 @@ struct BlockDriver { int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_ioctl)( BlockDriverState *bs, unsigned long int req, void *buf); =20 + /* + * Persistent reservation series api. + * Please refer to chapter 5.9 of SCSI Primary Commands - 4 or + * chapter 7 of NVMe Base Specification 2.0. + * + * The block layer driver should implement all the following APIs + * or none at all, including: bdrv_co_pr_read_keys, + * bdrv_co_pr_read_reservation, bdrv_co_pr_register, + * bdrv_co_pr_reserve, bdrv_co_pr_release, + * bdrv_co_pr_clear and bdrv_co_pr_preempt. + * + * Read the registered keys and return them in the @keys. + * @generation: The generation of the reservation key. + * @num_keys: The maximum number of keys that can be transmitted. + * @keys: Registered keys array. + * + * On success, store generation in @generation and store keys @keys + * and return the number of @keys. + * On failure return -errno. + */ + int coroutine_fn GRAPH_RDLOCK_PTR(*bdrv_co_pr_read_keys)( + BlockDriverState *bs, uint32_t *generation, + uint32_t num_keys, uint64_t *keys); + /* + * Read the reservation key and store it in the @key. + * @generation: The generation of the reservation key. + * @key: The reservation key. + * @type: Type of the reservation key. + * + * On success, store generation in @generation, store the + * reservation key in @key and return the number of @key + * which used to determine whether the reservation key exists. + * On failure return -errno. + */ + int coroutine_fn GRAPH_RDLOCK_PTR(*bdrv_co_pr_read_reservation)( + BlockDriverState *bs, uint32_t *generation, + uint64_t *key, BlockPrType *type); + /* + * Register, unregister, or replace a reservation key. + * @old_key: The current reservation key associated with the host. + * @new_key: The new reservation Key. + * @type: Type of the reservation key. + * @ignore_key: Ignore or not @old_key. + * @ptpl: Whether to support Persist Through Power Loss(PTPL). + */ + int coroutine_fn GRAPH_RDLOCK_PTR(*bdrv_co_pr_register)( + BlockDriverState *bs, uint64_t old_key, + uint64_t new_key, BlockPrType type, + bool ptpl, bool ignore_key); + /* + * Acquire a reservation on a host. + * @key: The current reservation key associated with the host. + * @type: Type of the reservation key. + */ + int coroutine_fn GRAPH_RDLOCK_PTR(*bdrv_co_pr_reserve)( + BlockDriverState *bs, uint64_t key, BlockPrType type); + /* + * Release a reservation on a host. + * @key: The current reservation key associated with the host. + * @type: Type of the reservation key. + */ + int coroutine_fn GRAPH_RDLOCK_PTR(*bdrv_co_pr_release)( + BlockDriverState *bs, uint64_t key, BlockPrType type); + /** + * Clear reservations on a host. + * @key: The current reservation key associated with the host. + */ + int coroutine_fn GRAPH_RDLOCK_PTR(*bdrv_co_pr_clear)( + BlockDriverState *bs, uint64_t key); + /* + * Preempt a reservation held on a host. + * @cr_key: The current reservation key associated with the host. + * @pr_key: The preempt reservation Key which to be + * unregistered from the namespace. + * @type: Type of the reservation key. + * @abort: Whether to abort a reservation held on a host. + */ + int coroutine_fn GRAPH_RDLOCK_PTR(*bdrv_co_pr_preempt)( + BlockDriverState *bs, uint64_t cr_key, + uint64_t pr_key, BlockPrType type, bool abort); + /* * Returns 0 for completed check, -errno for internal errors. * The check results are stored in result. @@ -899,6 +980,9 @@ typedef struct BlockLimits { uint32_t max_active_zones; =20 uint32_t write_granularity; + + /* Persistent reservation capacities. */ + uint8_t pr_cap; } BlockLimits; =20 typedef struct BdrvOpBlocker BdrvOpBlocker; diff --git a/include/sysemu/block-backend-io.h b/include/sysemu/block-backe= nd-io.h index d174275a5c..b3d49a3c6f 100644 --- a/include/sysemu/block-backend-io.h +++ b/include/sysemu/block-backend-io.h @@ -62,6 +62,30 @@ void blk_aio_cancel_async(BlockAIOCB *acb); BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *= buf, BlockCompletionFunc *cb, void *opaque); =20 +BlockAIOCB *blk_aio_pr_read_keys(BlockBackend *blk, uint32_t *generation, + uint32_t num_keys, uint64_t *keys, + BlockCompletionFunc *cb, void *opaque); +BlockAIOCB *blk_aio_pr_read_reservation(BlockBackend *blk, uint32_t *gener= ation, + uint64_t *key, BlockPrType *type, + BlockCompletionFunc *cb, void *opa= que); +BlockAIOCB *blk_aio_pr_register(BlockBackend *blk, uint64_t old_key, + uint64_t new_key, BlockPrType type, + bool ptpl, bool ignore_key, + BlockCompletionFunc *cb, + void *opaque); +BlockAIOCB *blk_aio_pr_reserve(BlockBackend *blk, uint64_t key, + BlockPrType type, + BlockCompletionFunc *cb, + void *opaque); +BlockAIOCB *blk_aio_pr_release(BlockBackend *blk, uint64_t key, + BlockPrType type, BlockCompletionFunc *cb, + void *opaque); +BlockAIOCB *blk_aio_pr_clear(BlockBackend *blk, uint64_t key, + BlockCompletionFunc *cb, void *opaque); +BlockAIOCB *blk_aio_pr_preempt(BlockBackend *blk, uint64_t cr_key, + uint64_t pr_key, BlockPrType type, bool abo= rt, + BlockCompletionFunc *cb, void *opaque); + void blk_inc_in_flight(BlockBackend *blk); void blk_dec_in_flight(BlockBackend *blk); =20 --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493369; cv=none; d=zohomail.com; s=zohoarc; b=HuHCJ5pfHrj0FdvJ89HhSe+rlqPjPZirlXH7ES/Xi/ogirxaJNLkyovgesnu+Nr/QfAjyUibb3rKoplQJFfyqh1kToIJVAasF2CbhSc+7epqZJS8gJusU8pBAOFlGFFrNN3niG3UHE0Dk80NIKqE1wiEjjwGG6BdkL6U/YGXmss= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493369; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ROim7xG4VeV7okIeoWk1lldyf4YzP1rLfO+fx4YPjhw=; b=Zfjh7eALmmp0rZz8Hgp4r18iBRbBHHzA9jc2aICVL4DCXuE+VFhHfLArfJkzTAGbCKHZocfw8j0Dasdk0E1U5pmVyFKPndrr/fwEdJ3MgQVvbwoYxNo5aO3xr3r/qIRNyjlTnFFWFZGdRlvD2A0pctt4S+2OH3mH2otlj6PBvIo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720493369434960.8958027052314; Mon, 8 Jul 2024 19:49:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0tI-0006Go-Cs; Mon, 08 Jul 2024 22:47:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0tG-0006DR-HS for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:38 -0400 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0tE-0003pU-UC for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:38 -0400 Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-2c95ca60719so2721882a91.3 for ; Mon, 08 Jul 2024 19:47:36 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.47.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:47:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493255; x=1721098055; darn=nongnu.org; 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; bh=ROim7xG4VeV7okIeoWk1lldyf4YzP1rLfO+fx4YPjhw=; b=AhZm9ZZgGfIyJkuFqAwnSBYX66taznC7VmrYsG4uDJKu6nhpct4gyapD7i6rQtiuuV CThv0M7/WmIYCL1z53ivswizF18PAzyM4uaFviH6jK6tyrv7VHtK2LR+9fVsZrqmqgbw NGUF/YCq+oWVShP0HuyJ+ZvnTRU8rsB/yI0p+yELApUUWglNnh4Vf5x8LLbVqwWqGI9s 5wMVs+6elOVBtM6mtQFBh9Bk7MdIkhY5dfuvZWOjjXCZCItN8H8hV10wZeItA5xz7yJE SN+u6Sv0h6s80UFH3FSzxBpf8vfhYb5ljo2JIs2IFyLBNVvQY1tnX+jAXmiSAhcyqsnG OZgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493255; x=1721098055; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ROim7xG4VeV7okIeoWk1lldyf4YzP1rLfO+fx4YPjhw=; b=Eujl8/jO8ZK445VqWJCK7zHJzV2eWvYiSLe1GjMT0YpEJ+iMjRQu5Ch2ttfZBT4WkC XW4Pk23ESyrc+fFjoxeIDwaDJamyIPbiG0/epY7e6oycmq0XvSfnvIXK9B1XBVnwSdUw 11IX5oT9Mz57AHRKCB42qcFbRGMKgq/aaJ2N0JGMCepVMfmi5JwXd1Gahx7aSic5/T7x LFPYZuWAaLd97IGsRlQlduDoIJlL1BQLWb4fosuuYNUD9T2vWhkzti9Mesa8OPE/VGN5 Sl/TYGMUd3KiMHxDdXfhsAQwbpaBDkn3Ka7v8PAiSDzh3jxxnOKO9m4iib2QZcmTLdG+ tzgQ== X-Forwarded-Encrypted: i=1; AJvYcCWVqWIPnLCJJFKkfHoxp8vDjD2QrEbbhwiXb/iiu++SPtvEaC+K4I+ht3J6Y3PQFZcXzAlLV1Ql6XHxOX0c7QIGSr/XL4o= X-Gm-Message-State: AOJu0YyR85h/+Eg9PM/xy+CtJ9zq2qiD+j3jAYCMzzsYwzmufEdnkU5K 0/kSvoT7YEswAOl0J7eAFF8KfvC5ZNvvQFlaU8iuEhzllYHTtK7C/FSuRO4Uqws= X-Google-Smtp-Source: AGHT+IGLx7Kudcu6eYHjzoDDW32UULQB2c0hm4P9miPhIpUEEh/+JHRA1IiyzYADjnO/TzLlVMucUw== X-Received: by 2002:a17:90a:10c9:b0:2c8:da73:af82 with SMTP id 98e67ed59e1d1-2ca35be7ec4mr1148479a91.10.1720493255483; Mon, 08 Jul 2024 19:47:35 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu Subject: [PATCH v8 02/10] block/raw: add persistent reservation in/out driver Date: Tue, 9 Jul 2024 10:46:58 +0800 Message-Id: <20240709024706.4108-3-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102c; envelope-from=luchangqi.123@bytedance.com; helo=mail-pj1-x102c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493370246100007 Content-Type: text/plain; charset="utf-8" Add persistent reservation in/out operations for raw driver. The following methods are implemented: bdrv_co_pr_read_keys, bdrv_co_pr_read_reservation, bdrv_co_pr_register, bdrv_co_pr_reserve, bdrv_co_pr_release, bdrv_co_pr_clear and bdrv_co_pr_preempt. Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi Reviewed-by: Stefan Hajnoczi --- block/raw-format.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/block/raw-format.c b/block/raw-format.c index ac7e8495f6..3746bc1bd3 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -454,6 +454,55 @@ raw_co_ioctl(BlockDriverState *bs, unsigned long int r= eq, void *buf) return bdrv_co_ioctl(bs->file->bs, req, buf); } =20 +static int coroutine_fn GRAPH_RDLOCK +raw_co_pr_read_keys(BlockDriverState *bs, uint32_t *generation, + uint32_t num_keys, uint64_t *keys) +{ + + return bdrv_co_pr_read_keys(bs->file->bs, generation, num_keys, keys); +} + +static int coroutine_fn GRAPH_RDLOCK +raw_co_pr_read_reservation(BlockDriverState *bs, uint32_t *generation, + uint64_t *key, BlockPrType *type) +{ + return bdrv_co_pr_read_reservation(bs->file->bs, generation, key, type= ); +} + +static int coroutine_fn GRAPH_RDLOCK +raw_co_pr_register(BlockDriverState *bs, uint64_t old_key, + uint64_t new_key, BlockPrType type, + bool ptpl, bool ignore_key) +{ + return bdrv_co_pr_register(bs->file->bs, old_key, new_key, + type, ptpl, ignore_key); +} + +static int coroutine_fn GRAPH_RDLOCK +raw_co_pr_reserve(BlockDriverState *bs, uint64_t key, BlockPrType type) +{ + return bdrv_co_pr_reserve(bs->file->bs, key, type); +} + +static int coroutine_fn GRAPH_RDLOCK +raw_co_pr_release(BlockDriverState *bs, uint64_t key, BlockPrType type) +{ + return bdrv_co_pr_release(bs->file->bs, key, type); +} + +static int coroutine_fn GRAPH_RDLOCK +raw_co_pr_clear(BlockDriverState *bs, uint64_t key) +{ + return bdrv_co_pr_clear(bs->file->bs, key); +} + +static int coroutine_fn GRAPH_RDLOCK +raw_co_pr_preempt(BlockDriverState *bs, uint64_t old_key, + uint64_t new_key, BlockPrType type, bool abort) +{ + return bdrv_co_pr_preempt(bs->file->bs, old_key, new_key, type, abort); +} + static int GRAPH_RDLOCK raw_has_zero_init(BlockDriverState *bs) { return bdrv_has_zero_init(bs->file->bs); @@ -672,6 +721,13 @@ BlockDriver bdrv_raw =3D { .strong_runtime_opts =3D raw_strong_runtime_opts, .mutable_opts =3D mutable_opts, .bdrv_cancel_in_flight =3D raw_cancel_in_flight, + .bdrv_co_pr_read_keys =3D raw_co_pr_read_keys, + .bdrv_co_pr_read_reservation =3D raw_co_pr_read_reservation, + .bdrv_co_pr_register =3D raw_co_pr_register, + .bdrv_co_pr_reserve =3D raw_co_pr_reserve, + .bdrv_co_pr_release =3D raw_co_pr_release, + .bdrv_co_pr_clear =3D raw_co_pr_clear, + .bdrv_co_pr_preempt =3D raw_co_pr_preempt, }; =20 static void bdrv_raw_init(void) --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493282; cv=none; d=zohomail.com; s=zohoarc; b=FykFPTw2/kWz2T7XIDZeWjit15dfh2SrlhVh0iPZwXBzDYCy06TzYIDFnHgRgbUMJxJTS4XgB3jpne8g+iZZIraLpLciUXc9GoxUhGNXoMvnpKpry4NE/G/qCGEBZUZcAaYYKORTCcBffnJSGUw/YkuY1Bm/fLFTQKUThAML1V8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493282; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=USEpHR6NMIBW3vxaZJ60aZgKZIewwnVEOQkR5GTNazo=; b=jWIqTBwTD1jSv8HgviguOzdZ+0luOFmX/0w/95LdG8z1V0owKaK2V9MHWXX0r+JtOIgPEGX62F6uSmuHrAcBgoLVHrfZBnwc6lUSTt0medNQLlUaVIHmdWEom6e2XVbQ2yL//cWoNfbrHaTsKw5UhKiVrBC12PC4n5S503D5JnE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720493282797655.2383072335912; Mon, 8 Jul 2024 19:48:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0tQ-0006db-TO; Mon, 08 Jul 2024 22:47:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0tO-0006Yu-Pw for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:46 -0400 Received: from mail-pf1-x429.google.com ([2607:f8b0:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0tK-0003sC-NQ for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:46 -0400 Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-706a1711ee5so2694236b3a.0 for ; Mon, 08 Jul 2024 19:47:42 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.47.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:47:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493261; x=1721098061; darn=nongnu.org; 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; bh=USEpHR6NMIBW3vxaZJ60aZgKZIewwnVEOQkR5GTNazo=; b=WQly5TchE1wpi/u2ee3Lm682RvJh1vMw8ZgoKU+KNyF+SFQR52wgMzmqSzSuL4JKAn bzj+dSCMI1sX5ZHG7ueQR1AsPOZuSoWsgLK/lfQTPPX40ewA8TesJh3KssIJkumFGfTh T9rBURKNGP7TGydndWHGRtyEoigoqlqocCpUhT4r2z+d/7GDHU9yGoY5+fi3nuNsNpCZ RQ7Wts/6Jwcrrt8oJcpZ/iJbWZvOIhdbSxt2u6Tr7hgtBgbVqbxsHP6u/wn6pn0FZsWk p2eeQ3bRfLqQmF23/98rVEGRfXWf/kN47geE61+DZxsBUyVUU7tKE1hmshykqrX/pwpc Su+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493261; x=1721098061; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=USEpHR6NMIBW3vxaZJ60aZgKZIewwnVEOQkR5GTNazo=; b=n+AmK9Ni5gMbCyZLvGab2Y8VkOnSeXd31wKr8h1izFmqkoabwAfempM/oFXY7F+DJ8 lFViO6c4OkrtUHMJrehx4Dhki224jJ/GJ3xdiF14VaiQp/p5cotxkTa+NlHwEGm2bYfM zrEi9siE0JPvYYybI7mgxdtDkE/i6gwUBN+gU6tHb6tVJB84SBM0MpW5qNG1Jhritahm 8oQTAtKhMRc3IOMxw/DusslPPqwKzAhVK7V07gdO/devAbfHBYv5t972QmAIFq4EpPw7 Dmkl4seWoa6e0zMyqJJn5HAnftCOCdIPHhnSR2tHi2Lo9UOmyx5cfVNR1lvfqU31tpZS 3Yaw== X-Forwarded-Encrypted: i=1; AJvYcCWTGYwLzj+mmiS6n0MvQKBdAcSeWr8lUvu8DAEp6ONwJcqhpj4HYfVNUyhJEZUUZzAiHzHwhqxXUafCP66M1vMGgf3h8oY= X-Gm-Message-State: AOJu0Yy17CwCVpHaklVp/5mG91TrnXfsCWfOuDga+qho5LwXI29wW80f SNioOnE3wqNe/dY+bCj8S/iYq3D86nSr6t3dX/2fH20NpqUQCKExlSIofZyyXvg= X-Google-Smtp-Source: AGHT+IElpzsrQB8iAD86BTIv6CGQEy/CaIRCfzFWUCpmjuSyUKQfdIy3Xn+IJZNybfMedGwOkdxp/w== X-Received: by 2002:a05:6a20:7f99:b0:1c0:c95d:af4e with SMTP id adf61e73a8af0-1c2984e6277mr1321528637.57.1720493261035; Mon, 08 Jul 2024 19:47:41 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu Subject: [PATCH v8 03/10] scsi/constant: add persistent reservation in/out protocol constants Date: Tue, 9 Jul 2024 10:46:59 +0800 Message-Id: <20240709024706.4108-4-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::429; envelope-from=luchangqi.123@bytedance.com; helo=mail-pf1-x429.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493284036100003 Content-Type: text/plain; charset="utf-8" Add constants for the persistent reservation in/out protocol in the scsi/constant module. The constants include the persistent reservation command, type, and scope values defined in sections 6.13 and 6.14 of the SCSI Primary Commands-4 (SPC-4) specification. Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi Reviewed-by: Stefan Hajnoczi --- include/scsi/constants.h | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/include/scsi/constants.h b/include/scsi/constants.h index 9b98451912..922a314535 100644 --- a/include/scsi/constants.h +++ b/include/scsi/constants.h @@ -319,4 +319,56 @@ #define IDENT_DESCR_TGT_DESCR_SIZE 32 #define XCOPY_BLK2BLK_SEG_DESC_SIZE 28 =20 +typedef enum { + SCSI_PR_WRITE_EXCLUSIVE =3D 0x01, + SCSI_PR_EXCLUSIVE_ACCESS =3D 0x03, + SCSI_PR_WRITE_EXCLUSIVE_REGS_ONLY =3D 0x05, + SCSI_PR_EXCLUSIVE_ACCESS_REGS_ONLY =3D 0x06, + SCSI_PR_WRITE_EXCLUSIVE_ALL_REGS =3D 0x07, + SCSI_PR_EXCLUSIVE_ACCESS_ALL_REGS =3D 0x08, +} SCSIPrType; + +typedef enum { + SCSI_PR_LU_SCOPE =3D 0x00, +} SCSIPrScope; + +typedef enum { + SCSI_PR_OUT_REGISTER =3D 0x0, + SCSI_PR_OUT_RESERVE =3D 0x1, + SCSI_PR_OUT_RELEASE =3D 0x2, + SCSI_PR_OUT_CLEAR =3D 0x3, + SCSI_PR_OUT_PREEMPT =3D 0x4, + SCSI_PR_OUT_PREEMPT_AND_ABORT =3D 0x5, + SCSI_PR_OUT_REG_AND_IGNORE_KEY =3D 0x6, + SCSI_PR_OUT_REG_AND_MOVE =3D 0x7, +} SCSIPrOutAction; + +typedef enum { + SCSI_PR_IN_READ_KEYS =3D 0x0, + SCSI_PR_IN_READ_RESERVATION =3D 0x1, + SCSI_PR_IN_REPORT_CAPABILITIES =3D 0x2, +} SCSIPrInAction; + +typedef enum { + /* Exclusive Access All Registrants reservation type */ + SCSI_PR_CAP_EX_AC_AR =3D 1 << 0, + /* Write Exclusive reservation type */ + SCSI_PR_CAP_WR_EX =3D 1 << 9, + /* Exclusive Access reservation type */ + SCSI_PR_CAP_EX_AC =3D 1 << 11, + /* Write Exclusive Registrants Only reservation type */ + SCSI_PR_CAP_WR_EX_RO =3D 1 << 13, + /* Exclusive Access Registrants Only reservation type */ + SCSI_PR_CAP_EX_AC_RO =3D 1 << 14, + /* Write Exclusive All Registrants reservation type */ + SCSI_PR_CAP_WR_EX_AR =3D 1 << 15, + + SCSI_PR_CAP_ALL =3D (SCSI_PR_CAP_EX_AC_AR | + SCSI_PR_CAP_WR_EX | + SCSI_PR_CAP_EX_AC | + SCSI_PR_CAP_WR_EX_RO | + SCSI_PR_CAP_EX_AC_RO | + SCSI_PR_CAP_WR_EX_AR), +} SCSIPrCap; + #endif --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493357; cv=none; d=zohomail.com; s=zohoarc; b=B0DMiyPwTOMT1k8AhXWpeneNmSMqWvKERV+4LFr6ovcaIAmtmC0iG4f8MsKSe3roJQ38937hu9MzLDNC9TaoNjqduIGk1deadrr+fY4oC/KTHAlU+/KVjG/eBPNVkE7VP5PrMO4oUgVijrn3q0cXnGeztn4dm5IyVO/tQ2D93Xk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493357; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=CUQGygF7ax3lHkGrG99vLN6/xen2jiCuqCocfv+MI40=; b=bkNtI4HM+xjeM0qoqKx9kTYWzLQvKzloSW1cIQJlY7mpAzRRNpGmnH8vbITCgaZwkFP/KPjkkD5XyNmJIe9wHgxTNtDfHYdlFR4njJdzOESEW9uqTJxO1fknhZeebU5Th4FRCPiPMbvmlnI1uKTc8kAO+O0HY9ROko2hvH2VtFI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 172049335772259.235595673164084; Mon, 8 Jul 2024 19:49:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0tl-00075V-Rc; Mon, 08 Jul 2024 22:48:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0tS-0006iy-AW for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:51 -0400 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0tQ-0003vG-JU for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:50 -0400 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2c9a1ea8cc3so2911852a91.0 for ; Mon, 08 Jul 2024 19:47:48 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.47.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:47:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493267; x=1721098067; darn=nongnu.org; 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; bh=CUQGygF7ax3lHkGrG99vLN6/xen2jiCuqCocfv+MI40=; b=eqOC0i5hkWsBvBiM3o2w0nPVZ+dbrTgSxkpnUg6lCCY7d8AHb4Kf1mFuHke+DOBPKY c5oXx6gxeReKHSeefGe6AY3u+0QhI5OSdHNZgl9WuxnbScXWc3P6+e2Ioi5ET3diGHh0 pgeHBFhUHLrJqGsef9SvdauPrjWwWxW9wZ8QIetSjOfhBb6GFyJtyeM7gRN8zGOA3Xud Tfnl0EsUJpdyxZdl2c5+jzQg/8O0/CW8Hxy+EN0N9CzYm1FuBtks3qoESvlQlrA4vc7N /sp+GRBui4sG59MH3eqkUXhbEO5HlbTJtNpEOBHHOUfdZKc99APG8AJcIBFovUeGMIj6 +ruQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493267; x=1721098067; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CUQGygF7ax3lHkGrG99vLN6/xen2jiCuqCocfv+MI40=; b=FPpbeIAF6jOKHw57mu1rhY7QxQeHLw05Sn7SeVNzuYjX46/dSWefv1AM0eiDgbTpAj JPfIoqzjvqI2In3ic/+bBPHqZFzUkGt7A5HHBzv47F+pxHdNE/tCRny3hjEvAgoc0Z5C PsfisiOlhaj4/HPbb9ic6U1ktHTHx3FHc3On/XPmlLWJiL/rvoNWwHqc5bXukmBC1ob7 VNk09kWcZ5cCCCwxxuPLos8hSt4xg117Sy7lRaYp7/lsTifJf135ReZ/iFtkHQrzrlye pDpYURdwMOwSehOCZGrPFqEWX1iqcIsk09H4mcvL5byn8F4AqCDt1rogSIT86Y8xf4zW CrzQ== X-Forwarded-Encrypted: i=1; AJvYcCUu8ncC+y2pp9l7pBx4xXeAbKU1pJunKE02KgO8xKV44V6FjbVvTkc2uA/AbiRFJnrI+n+9O1bDmCVNl1+ODCMq2sOZ5vc= X-Gm-Message-State: AOJu0Yz/vIRq1wcj0yU3FDdEAYPdIbuJxQX32lYvZM5k/j9KumQB4IkF MWC1QuIttaf7l1suwizcuyyRe0Tj8ygYCYRxuyy2onRDypnojDtL984xeBkHwm4= X-Google-Smtp-Source: AGHT+IFOU4Neoj7hWnBDufij+xm3oWBKDxSvv4N/swxxU3oViMSAZ8mPoMo0jTRWPYFeNIXAJh6AmA== X-Received: by 2002:a17:90b:1904:b0:2c8:f5c2:ad0c with SMTP id 98e67ed59e1d1-2ca3a7ac2bbmr1410107a91.2.1720493266872; Mon, 08 Jul 2024 19:47:46 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu Subject: [PATCH v8 04/10] scsi/util: add helper functions for persistent reservation types conversion Date: Tue, 9 Jul 2024 10:47:00 +0800 Message-Id: <20240709024706.4108-5-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=luchangqi.123@bytedance.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493358145100003 Content-Type: text/plain; charset="utf-8" This commit introduces two helper functions that facilitate the conversion between the persistent reservation types used in the SCSI protocol and those used in the block layer. Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi Reviewed-by: Stefan Hajnoczi --- include/scsi/utils.h | 8 +++++ scsi/utils.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/include/scsi/utils.h b/include/scsi/utils.h index d5c8efa16e..89a0b082fb 100644 --- a/include/scsi/utils.h +++ b/include/scsi/utils.h @@ -1,6 +1,8 @@ #ifndef SCSI_UTILS_H #define SCSI_UTILS_H =20 +#include "block/block-common.h" +#include "scsi/constants.h" #ifdef CONFIG_LINUX #include #endif @@ -135,6 +137,12 @@ uint32_t scsi_data_cdb_xfer(uint8_t *buf); uint32_t scsi_cdb_xfer(uint8_t *buf); int scsi_cdb_length(uint8_t *buf); =20 +BlockPrType scsi_pr_type_to_block(SCSIPrType type); +SCSIPrType block_pr_type_to_scsi(BlockPrType type); + +uint8_t scsi_pr_cap_to_block(uint16_t scsi_pr_cap); +uint16_t block_pr_cap_to_scsi(uint8_t block_pr_cap); + /* Linux SG_IO interface. */ #ifdef CONFIG_LINUX #define SG_ERR_DRIVER_TIMEOUT 0x06 diff --git a/scsi/utils.c b/scsi/utils.c index 357b036671..0dfdeb499d 100644 --- a/scsi/utils.c +++ b/scsi/utils.c @@ -658,3 +658,84 @@ int scsi_sense_from_host_status(uint8_t host_status, } return GOOD; } + +BlockPrType scsi_pr_type_to_block(SCSIPrType type) +{ + switch (type) { + case SCSI_PR_WRITE_EXCLUSIVE: + return BLK_PR_WRITE_EXCLUSIVE; + case SCSI_PR_EXCLUSIVE_ACCESS: + return BLK_PR_EXCLUSIVE_ACCESS; + case SCSI_PR_WRITE_EXCLUSIVE_REGS_ONLY: + return BLK_PR_WRITE_EXCLUSIVE_REGS_ONLY; + case SCSI_PR_EXCLUSIVE_ACCESS_REGS_ONLY: + return BLK_PR_EXCLUSIVE_ACCESS_REGS_ONLY; + case SCSI_PR_WRITE_EXCLUSIVE_ALL_REGS: + return BLK_PR_WRITE_EXCLUSIVE_ALL_REGS; + case SCSI_PR_EXCLUSIVE_ACCESS_ALL_REGS: + return BLK_PR_EXCLUSIVE_ACCESS_ALL_REGS; + } + + return 0; +} + +SCSIPrType block_pr_type_to_scsi(BlockPrType type) +{ + switch (type) { + case BLK_PR_WRITE_EXCLUSIVE: + return SCSI_PR_WRITE_EXCLUSIVE; + case BLK_PR_EXCLUSIVE_ACCESS: + return SCSI_PR_EXCLUSIVE_ACCESS; + case BLK_PR_WRITE_EXCLUSIVE_REGS_ONLY: + return SCSI_PR_WRITE_EXCLUSIVE_REGS_ONLY; + case BLK_PR_EXCLUSIVE_ACCESS_REGS_ONLY: + return SCSI_PR_EXCLUSIVE_ACCESS_REGS_ONLY; + case BLK_PR_WRITE_EXCLUSIVE_ALL_REGS: + return SCSI_PR_WRITE_EXCLUSIVE_ALL_REGS; + case BLK_PR_EXCLUSIVE_ACCESS_ALL_REGS: + return SCSI_PR_EXCLUSIVE_ACCESS_ALL_REGS; + } + + return 0; +} + + +uint8_t scsi_pr_cap_to_block(uint16_t scsi_pr_cap) +{ + uint8_t res =3D 0; + + res |=3D (scsi_pr_cap & SCSI_PR_CAP_WR_EX) ? + BLK_PR_CAP_WR_EX : 0; + res |=3D (scsi_pr_cap & SCSI_PR_CAP_EX_AC) ? + BLK_PR_CAP_EX_AC : 0; + res |=3D (scsi_pr_cap & SCSI_PR_CAP_WR_EX_RO) ? + BLK_PR_CAP_WR_EX_RO : 0; + res |=3D (scsi_pr_cap & SCSI_PR_CAP_EX_AC_RO) ? + BLK_PR_CAP_EX_AC_RO : 0; + res |=3D (scsi_pr_cap & SCSI_PR_CAP_WR_EX_AR) ? + BLK_PR_CAP_WR_EX_AR : 0; + res |=3D (scsi_pr_cap & SCSI_PR_CAP_EX_AC_AR) ? + BLK_PR_CAP_EX_AC_AR : 0; + + return res; +} + +uint16_t block_pr_cap_to_scsi(uint8_t block_pr_cap) +{ + uint16_t res =3D 0; + + res |=3D (block_pr_cap & BLK_PR_CAP_WR_EX) ? + SCSI_PR_CAP_WR_EX : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_EX_AC) ? + SCSI_PR_CAP_EX_AC : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_WR_EX_RO) ? + SCSI_PR_CAP_WR_EX_RO : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_EX_AC_RO) ? + SCSI_PR_CAP_EX_AC_RO : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_WR_EX_AR) ? + SCSI_PR_CAP_WR_EX_AR : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_EX_AC_AR) ? + SCSI_PR_CAP_EX_AC_AR : 0; + + return res; +} --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493357; cv=none; d=zohomail.com; s=zohoarc; b=YYDTVSnYKQz6/WlGxcmllzl0ISkZS6y8hoZ1GZGQowVXx/AbJ1DwRo8r8/XiDBbiDOvbHhTZPCp9aWCZB0W+i1G7CHGWWJuHRfQzCiNuQxMBjCELPxrEPZ1OS3vWtnjxL58VKVXzlRwuj0HNYdewaqaRz5rfiiDwdPtQmbKOVFs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493357; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=k/nkJpsENqr9B0X5L0j+OPvH0SDv1gpy1ACWG+Ghrts=; b=jf8DKsoL1qfnjfIoJ/loatkA4ur5R5iErJIt+LviwTNAjojH7/WIuTzgZutwVvesDWlhSbVQkA1VMjwwQ36SrGbZjLlQfloH8am0t6t818y8jJJhs9p6yd2Tim+4TyQwFm2BTgu7PrtZTVhya0/GK8ModadwNsoiWhvaOVrjy9g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720493357206617.5103761614935; Mon, 8 Jul 2024 19:49:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0tp-0007co-CA; Mon, 08 Jul 2024 22:48:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0tZ-0006yd-Th for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:04 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0tW-0003y1-0P for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:47:56 -0400 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-2c9df3eb0edso1808216a91.3 for ; Mon, 08 Jul 2024 19:47:53 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.47.48 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:47:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493273; x=1721098073; darn=nongnu.org; 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; bh=k/nkJpsENqr9B0X5L0j+OPvH0SDv1gpy1ACWG+Ghrts=; b=BN2/gQqsA6SM6CxAgzZRg/Eahd4UsTQI4nIZ2zRtQ+qBlWqUiuqRq8eyxQDSAFKT8k bO1vOh1SWseieqBP5KDrxxwxSA51YiKYrx9BShkJ3j62LJVq6ORPdA9GjY5IlGxHNONL l21r+YGRT+ml6sVIt2eQfQZPDCzvsFtJOMZYWWBGuZAKy5G9UYs8weJp++F5HGUvw7/G UAtpjy5tn40yt6DR1/Pijx+yzKs4oa+yLCOmVWxpyH61ZnqhQCJAElZLO3fzLyOkuROV XJN8YhwChRdeOnHrdwKvM/AHcPvUwZdNwXA6nJeacvmZb5y/X7+0hvxFyyRiHc2O6gqf b/Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493273; x=1721098073; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=k/nkJpsENqr9B0X5L0j+OPvH0SDv1gpy1ACWG+Ghrts=; b=pdJpy0FhsJERm/lhtTX0dWj67DZgpyXvFox64C6dzS9FPj4N970fxRezq3+y8QB2n9 l7V6qYUm5q/2E1gWG74p9ZF62rXn+xYAOdw2/LsH6gW9xV7o3+ly9CbP6AN6cE+f83gK 3EtCDgTbYfJhcAl1KUNuvK/ctfCXMy7q36u9rGfnTsUZL0SY2F830BoYW4pEZk3o6hTR lHig8NHypoIfUiZjE9HchVrpYBBRCI341PKBILOuMUmIMxl38Bi/TJqY8W+t3NnOFB4o VXfrSVZLfXJVG2hwuwFHm2E5SWZvI8pr8k+ARlFPmkUAtlqETvR8F9z5NIb6ksMxs8ru vuRw== X-Forwarded-Encrypted: i=1; AJvYcCU09mA45LhNZ74ARTKvexECEGEMVvpm+9cN2DpyzIvhvbg6ED8xwqdDQiF7Ri9130zaBzLcpwH2WHRchYvKWyUcrjJTu8k= X-Gm-Message-State: AOJu0YwHMYnjoMdrWl7oMs6vmUxWwF2723QpPMXApI2tjMznoQlhhDo2 9C7ZjhqsSSlHlVY6LsHcrn7CKMD/swZD2MOpE9TbDkHrbkWfByGooZgKAgWvroc= X-Google-Smtp-Source: AGHT+IHrDBX+KGY6zxVzeG5IifRCwIT5BKD+WYV3y84hC/3LmxlldhhVpQFNWmOayfh5WFE5iD1xFg== X-Received: by 2002:a17:90a:4ce1:b0:2bf:de8b:bef5 with SMTP id 98e67ed59e1d1-2ca35c30c1bmr1070660a91.12.1720493272665; Mon, 08 Jul 2024 19:47:52 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu Subject: [PATCH v8 05/10] hw/scsi: add persistent reservation in/out api for scsi device Date: Tue, 9 Jul 2024 10:47:01 +0800 Message-Id: <20240709024706.4108-6-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=luchangqi.123@bytedance.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493358157100004 Content-Type: text/plain; charset="utf-8" Add persistent reservation in/out operations in the SCSI device layer. By introducing the persistent reservation in/out api, this enables the SCSI device to perform reservation-related tasks, including querying keys, querying reservation status, registering reservation keys, initiating and releasing reservations, as well as clearing and preempting reservations held by other keys. These operations are crucial for management and control of shared storage resources in a persistent manner. Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi Reviewed-by: Stefan Hajnoczi --- hw/scsi/scsi-disk.c | 368 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 368 insertions(+) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 4bd7af9d0c..09c12fafe3 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -32,6 +32,7 @@ #include "migration/vmstate.h" #include "hw/scsi/emulation.h" #include "scsi/constants.h" +#include "scsi/utils.h" #include "sysemu/block-backend.h" #include "sysemu/blockdev.h" #include "hw/block/block.h" @@ -42,6 +43,7 @@ #include "qemu/cutils.h" #include "trace.h" #include "qom/object.h" +#include "block/block_int.h" =20 #ifdef __linux #include @@ -1474,6 +1476,362 @@ static void scsi_disk_emulate_read_data(SCSIRequest= *req) scsi_req_complete(&r->req, GOOD); } =20 +typedef struct SCSIPrReadKeys { + uint32_t generation; + uint32_t num_keys; + uint64_t *keys; + SCSIDiskReq *req; +} SCSIPrReadKeys; + +typedef struct SCSIPrReadReservation { + uint32_t generation; + uint64_t key; + BlockPrType type; + SCSIDiskReq *req; +} SCSIPrReadReservation; + +static void scsi_pr_read_keys_complete(void *opaque, int ret) +{ + int num_keys; + uint8_t *buf; + SCSIPrReadKeys *blk_keys =3D (SCSIPrReadKeys *)opaque; + SCSIDiskReq *r =3D blk_keys->req; + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, r->req.dev); + + assert(blk_get_aio_context(s->qdev.conf.blk) =3D=3D + qemu_get_current_aio_context()); + + assert(r->req.aiocb !=3D NULL); + r->req.aiocb =3D NULL; + + if (scsi_disk_req_check_error(r, ret, true)) { + goto done; + } + + buf =3D scsi_req_get_buf(&r->req); + num_keys =3D MIN(blk_keys->num_keys, ret > 0 ? ret : 0); + blk_keys->generation =3D cpu_to_be32(blk_keys->generation); + memcpy(&buf[0], &blk_keys->generation, 4); + for (int i =3D 0; i < num_keys; i++) { + blk_keys->keys[i] =3D cpu_to_be64(blk_keys->keys[i]); + memcpy(&buf[8 + i * 8], &blk_keys->keys[i], 8); + } + num_keys =3D cpu_to_be32(num_keys * 8); + memcpy(&buf[4], &num_keys, 4); + + scsi_req_data(&r->req, r->buflen); +done: + scsi_req_unref(&r->req); + g_free(blk_keys->keys); + g_free(blk_keys); +} + +static void scsi_disk_emulate_pr_read_keys(SCSIRequest *req) +{ + SCSIPrReadKeys *blk_keys; + SCSIDiskReq *r =3D DO_UPCAST(SCSIDiskReq, req, req); + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); + int buflen =3D MIN(r->req.cmd.xfer, r->buflen); + int num_keys =3D (buflen - sizeof(uint32_t) * 2) / sizeof(uint64_t); + + if (num_keys <=3D 0) { + scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); + return; + } + + blk_keys =3D g_new0(SCSIPrReadKeys, 1); + blk_keys->generation =3D 0; + /* num_keys is the maximum number of keys that can be transmitted */ + blk_keys->num_keys =3D num_keys; + blk_keys->keys =3D g_malloc(sizeof(uint64_t) * num_keys); + blk_keys->req =3D r; + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + r->req.aiocb =3D blk_aio_pr_read_keys(s->qdev.conf.blk, &blk_keys->gen= eration, + blk_keys->num_keys, blk_keys->keys, + scsi_pr_read_keys_complete, blk_ke= ys); + return; +} + +static void scsi_pr_read_reservation_complete(void *opaque, int ret) +{ + uint8_t *buf; + uint32_t additional_len =3D 0; + SCSIPrReadReservation *blk_rsv =3D (SCSIPrReadReservation *)opaque; + SCSIDiskReq *r =3D blk_rsv->req; + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, r->req.dev); + + assert(blk_get_aio_context(s->qdev.conf.blk) =3D=3D + qemu_get_current_aio_context()); + + assert(r->req.aiocb !=3D NULL); + r->req.aiocb =3D NULL; + + if (scsi_disk_req_check_error(r, ret, true)) { + goto done; + } + + buf =3D scsi_req_get_buf(&r->req); + blk_rsv->generation =3D cpu_to_be32(blk_rsv->generation); + memcpy(&buf[0], &blk_rsv->generation, 4); + if (ret) { + additional_len =3D cpu_to_be32(16); + blk_rsv->key =3D cpu_to_be64(blk_rsv->key); + memcpy(&buf[8], &blk_rsv->key, 8); + buf[21] =3D block_pr_type_to_scsi(blk_rsv->type) & 0xf; + } else { + additional_len =3D cpu_to_be32(0); + } + + memcpy(&buf[4], &additional_len, 4); + scsi_req_data(&r->req, r->buflen); + +done: + scsi_req_unref(&r->req); + g_free(blk_rsv); +} + +static void scsi_disk_emulate_pr_read_reservation(SCSIRequest *req) +{ + SCSIPrReadReservation *blk_rsv; + SCSIDiskReq *r =3D DO_UPCAST(SCSIDiskReq, req, req); + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); + int buflen =3D MIN(r->req.cmd.xfer, r->buflen); + int count =3D sizeof(uint32_t) * 2 + sizeof(uint64_t) + + sizeof(uint32_t) + sizeof(uint8_t) * 2; + + if (buflen < count) { + scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); + return; + } + + blk_rsv =3D g_malloc(sizeof(*blk_rsv)); + blk_rsv->generation =3D 0; + blk_rsv->key =3D 0; + blk_rsv->type =3D 0; + blk_rsv->req =3D r; + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + r->req.aiocb =3D blk_aio_pr_read_reservation(s->qdev.conf.blk, + &blk_rsv->generation, &blk_rsv->key, &blk_rsv->type, + scsi_pr_read_reservation_complete, blk_rsv); + return; +} + +static void scsi_disk_emulate_pr_report_capabilities(SCSIRequest *req) +{ + SCSIDiskReq *r =3D DO_UPCAST(SCSIDiskReq, req, req); + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); + BlockDriverState *bs =3D blk_bs(s->qdev.conf.blk); + uint8_t blk_pr_cap =3D bs->bl.pr_cap; + uint8_t *buf =3D scsi_req_get_buf(req); + uint16_t pr_cap; + /* + * according to response of report_capabilities + * command of chapter 6.13 of spc4 + */ + int len =3D sizeof(uint16_t) * 2 + sizeof(uint8_t) * 4; + + if (len > r->buflen) { + scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); + return; + } + + len =3D cpu_to_be16(len); + memcpy(&buf[0], &len, 2); + /* enable PTPL_C */ + buf[2] =3D 1; + buf[3] =3D (blk_pr_cap & BLK_PR_CAP_PTPL) ? 1 : 0; + /* enable TMV */ + buf[3] |=3D 1 << 7; + + /* enable persistent reservation type */ + pr_cap =3D block_pr_cap_to_scsi(blk_pr_cap); + buf[4] =3D pr_cap >> 8 & 0xff; + buf[5] =3D pr_cap & 0xff; + + scsi_req_data(&r->req, len); + return; +} + +static void scsi_aio_pr_complete(void *opaque, int ret) +{ + SCSIDiskReq *r =3D (SCSIDiskReq *)opaque; + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, r->req.dev); + + /* The request must only run in the BlockBackend's AioContext */ + assert(blk_get_aio_context(s->qdev.conf.blk) =3D=3D + qemu_get_current_aio_context()); + + assert(r->req.aiocb !=3D NULL); + r->req.aiocb =3D NULL; + + if (scsi_disk_req_check_error(r, ret, true)) { + goto done; + } + + scsi_req_complete(&r->req, GOOD); + +done: + scsi_req_unref(&r->req); +} + +static void scsi_disk_emulate_pr_register(SCSIDiskReq *r, uint64_t r_key, + uint64_t sa_key, SCSIPrType type, + uint8_t aptpl, bool ignore_key) +{ + SCSIRequest *req =3D &r->req; + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + r->req.aiocb =3D blk_aio_pr_register(s->qdev.conf.blk, r_key, sa_key, + scsi_pr_type_to_block(type), + aptpl ? true : false, + ignore_key, scsi_aio_pr_complete, r= ); + + return; +} + +static void scsi_disk_emulate_pr_reserve(SCSIDiskReq *r, uint64_t r_key, + SCSIPrType type) +{ + SCSIRequest *req =3D &r->req; + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + r->req.aiocb =3D blk_aio_pr_reserve(s->qdev.conf.blk, r_key, + scsi_pr_type_to_block(type), + scsi_aio_pr_complete, r); + + return; +} + +static void scsi_disk_emulate_pr_release(SCSIDiskReq *r, uint64_t r_key, + SCSIPrType type) +{ + SCSIRequest *req =3D &r->req; + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + r->req.aiocb =3D blk_aio_pr_release(s->qdev.conf.blk, r_key, + scsi_pr_type_to_block(type), + scsi_aio_pr_complete, r); + + return; +} + +static void scsi_disk_emulate_pr_clear(SCSIDiskReq *r, uint64_t r_key) +{ + SCSIRequest *req =3D &r->req; + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + r->req.aiocb =3D blk_aio_pr_clear(s->qdev.conf.blk, r_key, + scsi_aio_pr_complete, r); + + return; +} + +static void scsi_disk_emulate_pr_preempt(SCSIDiskReq *r, uint64_t r_key, + uint64_t sa_key, SCSIPrType type, + bool abort) +{ + SCSIRequest *req =3D &r->req; + SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, req->dev); + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + r->req.aiocb =3D blk_aio_pr_preempt(s->qdev.conf.blk, r_key, sa_key, + scsi_pr_type_to_block(type), abort, + scsi_aio_pr_complete, r); + + return; +} + +static void scsi_disk_emulate_pr_in(SCSIRequest *req) +{ + SCSIPrInAction action =3D req->cmd.buf[1] & 0x1f; + SCSIDiskReq *r =3D DO_UPCAST(SCSIDiskReq, req, req); + + switch (action) { + case SCSI_PR_IN_READ_KEYS: + scsi_disk_emulate_pr_read_keys(req); + break; + case SCSI_PR_IN_READ_RESERVATION: + scsi_disk_emulate_pr_read_reservation(req); + break; + case SCSI_PR_IN_REPORT_CAPABILITIES: + scsi_disk_emulate_pr_report_capabilities(req); + break; + default: + scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE)); + return; + } + + return; +} + +static void scsi_disk_emulate_pr_out(SCSIDiskReq *r, uint8_t *inbuf) +{ + uint8_t aptpl; + uint64_t r_key, sa_key; + SCSIPrOutAction action; + SCSIPrScope scope; + SCSIPrType type; + SCSIRequest *req =3D &r->req; + + memcpy(&r_key, &inbuf[0], 8); + r_key =3D be64_to_cpu(r_key); + memcpy(&sa_key, &inbuf[8], 8); + sa_key =3D be64_to_cpu(sa_key); + memcpy(&aptpl, &inbuf[20], 1); + aptpl =3D aptpl & 0x01; + action =3D req->cmd.buf[1] & 0x1f; + scope =3D (req->cmd.buf[2] >> 4) & 0x0f; + type =3D req->cmd.buf[2] & 0x0f; + + if (scope !=3D SCSI_PR_LU_SCOPE) { + scsi_req_complete(&r->req, TASK_ABORTED); + return; + } + + switch (action) { + case SCSI_PR_OUT_REGISTER: + scsi_disk_emulate_pr_register(r, r_key, sa_key, type, + aptpl, false); + break; + case SCSI_PR_OUT_REG_AND_IGNORE_KEY: + scsi_disk_emulate_pr_register(r, r_key, sa_key, type, aptpl, true); + break; + case SCSI_PR_OUT_RESERVE: + scsi_disk_emulate_pr_reserve(r, r_key, type); + break; + case SCSI_PR_OUT_RELEASE: + scsi_disk_emulate_pr_release(r, r_key, type); + break; + case SCSI_PR_OUT_CLEAR: + scsi_disk_emulate_pr_clear(r, r_key); + break; + case SCSI_PR_OUT_PREEMPT: + scsi_disk_emulate_pr_preempt(r, r_key, sa_key, type, false); + break; + case SCSI_PR_OUT_PREEMPT_AND_ABORT: + scsi_disk_emulate_pr_preempt(r, r_key, sa_key, type, true); + break; + default: + scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE)); + return; + } + + return; +} + static int scsi_disk_check_mode_select(SCSIDiskState *s, int page, uint8_t *inbuf, int inlen) { @@ -1957,6 +2315,9 @@ static void scsi_disk_emulate_write_data(SCSIRequest = *req) scsi_req_complete(&r->req, GOOD); break; =20 + case PERSISTENT_RESERVE_OUT: + scsi_disk_emulate_pr_out(r, r->iov.iov_base); + break; default: abort(); } @@ -2213,6 +2574,11 @@ static int32_t scsi_disk_emulate_command(SCSIRequest= *req, uint8_t *buf) case FORMAT_UNIT: trace_scsi_disk_emulate_command_FORMAT_UNIT(r->req.cmd.xfer); break; + case PERSISTENT_RESERVE_OUT: + break; + case PERSISTENT_RESERVE_IN: + scsi_disk_emulate_pr_in(req); + return 0; default: trace_scsi_disk_emulate_command_UNKNOWN(buf[0], scsi_command_name(buf[0])); @@ -2632,6 +2998,8 @@ static const SCSIReqOps *const scsi_disk_reqops_dispa= tch[256] =3D { [VERIFY_12] =3D &scsi_disk_emulate_reqops, [VERIFY_16] =3D &scsi_disk_emulate_reqops, [FORMAT_UNIT] =3D &scsi_disk_emulate_reqops, + [PERSISTENT_RESERVE_IN] =3D &scsi_disk_emulate_reqops, + [PERSISTENT_RESERVE_OUT] =3D &scsi_disk_emulate_reqops, =20 [READ_6] =3D &scsi_disk_dma_reqops, [READ_10] =3D &scsi_disk_dma_reqops, --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493357; cv=none; d=zohomail.com; s=zohoarc; b=X0548TTrGchDCyXrXG9KkEZkqYNygKDq3Bcz7SKq+aHJlwiq+fqaiZBy2D0wqLwgYgtON8EWJQB9/Ir6rePhnu/ku6Dw9iXy+hx+C/sqABXs615OaYooICyNqCAJYourjj4y6KJZdfG311I9D/DSHXKho3pYniEzs4d3FM9bFgI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493357; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=LNfE2FvcIsikcgtI6Krtxtj2etktds/RmL9xP+AY0Zg=; b=ay+MDGjySxVxNLhzcdzpUOnyORCuoXwkzgDE509ERLeFHKTx4ol2o3LxI7AWx4w3wCt20ppFjcdBCbrAT3hbeN9bWHjPfRncx8+ZAojDtS3+0jpMu6DstpBPXSaEyQ03/n5fp2yszXOQF4Rp28VRPXyp2Jl8/pvfDwiMM7AbVvI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 172049335792220.27089458967089; Mon, 8 Jul 2024 19:49:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0tr-0007j5-35; Mon, 08 Jul 2024 22:48:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0te-00071m-9g for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:06 -0400 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0tc-00040j-Hp for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:02 -0400 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-70b09cb7776so2421959b3a.1 for ; Mon, 08 Jul 2024 19:47:59 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.47.54 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:47:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493278; x=1721098078; darn=nongnu.org; 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; bh=LNfE2FvcIsikcgtI6Krtxtj2etktds/RmL9xP+AY0Zg=; b=alu7z1pDFtT3YWJhvebf/e+AUXYMcz/Wdp2ZyU/UdoHKoCAhwrdT+llQviyVEdKoIr pDb5lDn8/dgBSbizZJgzbWcQdZgFoDDmukD3CrL0wpvgckxTRbLEaOCmCRw4iCob54qh FMAwtU7DTHmwfsgO+s4OK2Dcmt9bOXWJ1daYA958AcbqCQY1rei7t2mFuksivoo9+jb8 9RJnS0u+jJKlCx1gVvdDiCS8I4ERID9cqKLBBIJxTl9O2gTC1CnSI2LbMhgJALSvtsPj 9Uf8BGoJimdyUhQL4DsaxB5NeuwYP9PNT8VTcMMOpRt+CmS/B6da/410VD1qvUIuAgXa +4wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493278; x=1721098078; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LNfE2FvcIsikcgtI6Krtxtj2etktds/RmL9xP+AY0Zg=; b=YQCq8Iya82elHoQNH9XlsfT/up43RHVqsIuOLym92yx8Wl334bEgfnIgO30okFdSQD GhP2NrNHCHc8w8Q2Reu35ErIEz/ioGHNxv/GB5BUTnrlBrzf19rin5vGvOH6JWMqQu5/ VnYlTI1xjFEH8DhJqBxM+GHHq//CQUKsFwZw7FPAQ4T3HPf2yK+BNo4/LMcEShjNZGU4 f2ibihkpJT11xa0d8EhhHkzgElJwALKGOAsTkzepNiH6wbEMRLQCL5YpqTSLXAC0ZHFA daHV5z9Qqwmn0l83+16MBsqWNB9W4FnYJrQEuvvFC5WQzRL6nOA+22sgU1PBQWZ+DHTQ KzmA== X-Forwarded-Encrypted: i=1; AJvYcCUlupSRo68NIkqYKj96IYhv8JYnwPcacvZErPn1Q1YgYeYKl8YASMDEyTypFDbmy5qXzUpg4RKbyxdkxlXYcB/DUWxFbxM= X-Gm-Message-State: AOJu0Ywt5tCfpLtj7muiFQPQvSsUotma/qPC1wVAZn2ltTbCX4znSFBC RmBKqyyoF45NClVFV6jLRmPxiKYf4gxYOBlFpYWMN+VjhZpqpa57Hdy5tV+1A0E= X-Google-Smtp-Source: AGHT+IHO3nCz6uW/4QOBKdyAXahz/tcloJqrja0IDFyg68VePKeV0WHTO3HyOt7ZIpuJs0oy8hjSKg== X-Received: by 2002:a05:6a20:7485:b0:1c2:97cd:94d8 with SMTP id adf61e73a8af0-1c298223d92mr1254415637.20.1720493278547; Mon, 08 Jul 2024 19:47:58 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu Subject: [PATCH v8 06/10] block/nvme: add reservation command protocol constants Date: Tue, 9 Jul 2024 10:47:02 +0800 Message-Id: <20240709024706.4108-7-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=luchangqi.123@bytedance.com; helo=mail-pf1-x42a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493358164100005 Content-Type: text/plain; charset="utf-8" Add constants for the NVMe persistent command protocol. The constants include the reservation command opcode and reservation type values defined in section 7 of the NVMe 2.0 specification. Reviewed-by: Stefan Hajnoczi Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi --- include/block/nvme.h | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/include/block/nvme.h b/include/block/nvme.h index bb231d0b9a..8b125f7769 100644 --- a/include/block/nvme.h +++ b/include/block/nvme.h @@ -633,7 +633,11 @@ enum NvmeIoCommands { NVME_CMD_WRITE_ZEROES =3D 0x08, NVME_CMD_DSM =3D 0x09, NVME_CMD_VERIFY =3D 0x0c, + NVME_CMD_RESV_REGISTER =3D 0x0d, + NVME_CMD_RESV_REPORT =3D 0x0e, + NVME_CMD_RESV_ACQUIRE =3D 0x11, NVME_CMD_IO_MGMT_RECV =3D 0x12, + NVME_CMD_RESV_RELEASE =3D 0x15, NVME_CMD_COPY =3D 0x19, NVME_CMD_IO_MGMT_SEND =3D 0x1d, NVME_CMD_ZONE_MGMT_SEND =3D 0x79, @@ -641,6 +645,63 @@ enum NvmeIoCommands { NVME_CMD_ZONE_APPEND =3D 0x7d, }; =20 +typedef enum { + NVME_RESV_REGISTER_ACTION_REGISTER =3D 0x00, + NVME_RESV_REGISTER_ACTION_UNREGISTER =3D 0x01, + NVME_RESV_REGISTER_ACTION_REPLACE =3D 0x02, +} NvmeReservationRegisterAction; + +typedef enum { + NVME_RESV_RELEASE_ACTION_RELEASE =3D 0x00, + NVME_RESV_RELEASE_ACTION_CLEAR =3D 0x01, +} NvmeReservationReleaseAction; + +typedef enum { + NVME_RESV_ACQUIRE_ACTION_ACQUIRE =3D 0x00, + NVME_RESV_ACQUIRE_ACTION_PREEMPT =3D 0x01, + NVME_RESV_ACQUIRE_ACTION_PREEMPT_AND_ABORT =3D 0x02, +} NvmeReservationAcquireAction; + +typedef enum { + NVME_RESV_WRITE_EXCLUSIVE =3D 0x01, + NVME_RESV_EXCLUSIVE_ACCESS =3D 0x02, + NVME_RESV_WRITE_EXCLUSIVE_REGS_ONLY =3D 0x03, + NVME_RESV_EXCLUSIVE_ACCESS_REGS_ONLY =3D 0x04, + NVME_RESV_WRITE_EXCLUSIVE_ALL_REGS =3D 0x05, + NVME_RESV_EXCLUSIVE_ACCESS_ALL_REGS =3D 0x06, +} NvmeResvType; + +typedef enum { + NVME_RESV_PTPL_NO_CHANGE =3D 0x00, + NVME_RESV_PTPL_DISABLE =3D 0x02, + NVME_RESV_PTPL_ENABLE =3D 0x03, +} NvmeResvPTPL; + +typedef enum NVMEPrCap { + /* Persist Through Power Loss */ + NVME_PR_CAP_PTPL =3D 1 << 0, + /* Write Exclusive reservation type */ + NVME_PR_CAP_WR_EX =3D 1 << 1, + /* Exclusive Access reservation type */ + NVME_PR_CAP_EX_AC =3D 1 << 2, + /* Write Exclusive Registrants Only reservation type */ + NVME_PR_CAP_WR_EX_RO =3D 1 << 3, + /* Exclusive Access Registrants Only reservation type */ + NVME_PR_CAP_EX_AC_RO =3D 1 << 4, + /* Write Exclusive All Registrants reservation type */ + NVME_PR_CAP_WR_EX_AR =3D 1 << 5, + /* Exclusive Access All Registrants reservation type */ + NVME_PR_CAP_EX_AC_AR =3D 1 << 6, + + NVME_PR_CAP_ALL =3D (NVME_PR_CAP_PTPL | + NVME_PR_CAP_WR_EX | + NVME_PR_CAP_EX_AC | + NVME_PR_CAP_WR_EX_RO | + NVME_PR_CAP_EX_AC_RO | + NVME_PR_CAP_WR_EX_AR | + NVME_PR_CAP_EX_AC_AR), +} NvmePrCap; + typedef struct QEMU_PACKED NvmeDeleteQ { uint8_t opcode; uint8_t flags; --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493303; cv=none; d=zohomail.com; s=zohoarc; b=G/f3Kk/fKUe9TFW4IeTQYvmzyKG+L24ZTDn+pwwmWn4ma3Hmc4cwRXlEpJr9O+1nQZZzYWj/UEXHPYo0D1/SCyEqvT7kcG3R3OgJDDBHqyj8jJiCspnzJDgP33eMuso7mrQFdAOPb1VSMf9bd8Elhg/Bpjw5vTEY3tuSeTv+FhA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493303; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Tjqh17JUE1WkBpchsBFPlHMHCUXRZeQxBDgeEciSrXM=; b=hUM3QT3sNk0AgwSLe4tOhOUFp2kGJUxZH5u9QmVmlPmOwsN4ZeoGr5GFjnGFQfK4jNtn/YaXmvBy20dYbhzxHD2aV5q/2iCCaZA61a1SOn4e33lIKBumEudShR/tKy5eG3AKeJU9MMgXq93kS72GAvAEU8ENq0H/I/eCwCKqr1A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720493303671952.7302055471572; Mon, 8 Jul 2024 19:48:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0to-0007US-9t; Mon, 08 Jul 2024 22:48:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0tj-00076z-Uz for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:09 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0th-00043y-Q2 for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:07 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1fb4a807708so36825265ad.2 for ; Mon, 08 Jul 2024 19:48:05 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.47.59 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:48:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493284; x=1721098084; darn=nongnu.org; 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; bh=Tjqh17JUE1WkBpchsBFPlHMHCUXRZeQxBDgeEciSrXM=; b=hp/RDHiwq+UO5YCMc9zrhcZDmPuS+42U/weMxkoQL/TBzs8w276RigRgg7qqmT4I9T wMlWe9lrmr55LiUClJWbjvIfpcFOgqAfjwOPtDmFs4zMwybkWN5pl4XZPt02WVCE2Qph ltjwio0pyY8Of7e0Ld1TfYggG4/0grSiP7ObtvB4pbcIxaKlStGwyF8asEjgWNm0RB/5 ndgou2vMWHjUaLoHJgl++wol28NN45r2P7ICIpb0E6J0erw/8I5dorLGMpApFYk420M2 g9eQuD8NRKzOMUoQzN1od19h3oD4JSDdrw+E4OIIaSRPXnKFMp7PCMsYlla3PVR9oHRD wq/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493284; x=1721098084; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Tjqh17JUE1WkBpchsBFPlHMHCUXRZeQxBDgeEciSrXM=; b=eTJkNmxAkg46/7OgPEyYBh4vVPf2jXqp2os9kPCVrSMMthLWhETsrLn6rC9xx4ctsn /qiZP0USDVl6I2HD6W4lq1FBLaL+B5EevTQtiIuFZgL3UfQvxDBuGqupa3rXNO9dvJ9v UcAHpfcGqUnbUjQVxJB4Z8z7tSSTT0E/9mvkFVlN6U1vd9vJxelPap1eCKbCR0oiYco3 fdfQQeUUO3Nwwz2HGZzpowLcnE99YA3c1xswN5numdKeve+PfXw4mthMDjssnIZb0dF3 KTbJy9eRxRouWnkEmx3ik0Re9L7S9qpsnaVTVs1EecZKA5mBO3C1PW8AYQKzo21JyjyT Sq5Q== X-Forwarded-Encrypted: i=1; AJvYcCVaraG1+57CnCW/iEBE6eOV37lNG4LPairaOEl0qUW0oh1t3K7bPWQNjogcmsSRRge3LlIKVvfB5QeFZqmntxBK6cwDSAU= X-Gm-Message-State: AOJu0YyR8oPz3XMHJUc6jA932uC7s5v0xjZrU+MiotudCseDlvaAmztg QcJzyW/7CV31JByl11+ODM44GmKwZP34tTmYEOAzf57LigrhL2PqZF/zHT6UyYA= X-Google-Smtp-Source: AGHT+IHDIJtkjnvUylyk4h6TBeNrWqMSyfq5WI2Oe6iBatlu1yPH622OT9sPllfAgcRWsxDCUYFNFQ== X-Received: by 2002:a17:90a:e795:b0:2c9:98c2:f6d7 with SMTP id 98e67ed59e1d1-2ca35bca26cmr1266227a91.5.1720493284459; Mon, 08 Jul 2024 19:48:04 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu , Klaus Jensen Subject: [PATCH v8 07/10] hw/nvme: add helper functions for converting reservation types Date: Tue, 9 Jul 2024 10:47:03 +0800 Message-Id: <20240709024706.4108-8-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=luchangqi.123@bytedance.com; helo=mail-pl1-x636.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493303936100001 Content-Type: text/plain; charset="utf-8" This commit introduces two helper functions that facilitate the conversion between the reservation types used in the NVME protocol and those used in the block layer. Reviewed-by: Klaus Jensen Reviewed-by: Stefan Hajnoczi Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi --- hw/nvme/nvme.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/hw/nvme/nvme.h b/hw/nvme/nvme.h index bed8191bd5..6d0e456348 100644 --- a/hw/nvme/nvme.h +++ b/hw/nvme/nvme.h @@ -474,6 +474,90 @@ static inline const char *nvme_io_opc_str(uint8_t opc) } } =20 +static inline NvmeResvType block_pr_type_to_nvme(BlockPrType type) +{ + switch (type) { + case BLK_PR_WRITE_EXCLUSIVE: + return NVME_RESV_WRITE_EXCLUSIVE; + case BLK_PR_EXCLUSIVE_ACCESS: + return NVME_RESV_EXCLUSIVE_ACCESS; + case BLK_PR_WRITE_EXCLUSIVE_REGS_ONLY: + return NVME_RESV_WRITE_EXCLUSIVE_REGS_ONLY; + case BLK_PR_EXCLUSIVE_ACCESS_REGS_ONLY: + return NVME_RESV_EXCLUSIVE_ACCESS_REGS_ONLY; + case BLK_PR_WRITE_EXCLUSIVE_ALL_REGS: + return NVME_RESV_WRITE_EXCLUSIVE_ALL_REGS; + case BLK_PR_EXCLUSIVE_ACCESS_ALL_REGS: + return NVME_RESV_EXCLUSIVE_ACCESS_ALL_REGS; + } + + return 0; +} + +static inline BlockPrType nvme_pr_type_to_block(NvmeResvType type) +{ + switch (type) { + case NVME_RESV_WRITE_EXCLUSIVE: + return BLK_PR_WRITE_EXCLUSIVE; + case NVME_RESV_EXCLUSIVE_ACCESS: + return BLK_PR_EXCLUSIVE_ACCESS; + case NVME_RESV_WRITE_EXCLUSIVE_REGS_ONLY: + return BLK_PR_WRITE_EXCLUSIVE_REGS_ONLY; + case NVME_RESV_EXCLUSIVE_ACCESS_REGS_ONLY: + return BLK_PR_EXCLUSIVE_ACCESS_REGS_ONLY; + case NVME_RESV_WRITE_EXCLUSIVE_ALL_REGS: + return BLK_PR_WRITE_EXCLUSIVE_ALL_REGS; + case NVME_RESV_EXCLUSIVE_ACCESS_ALL_REGS: + return BLK_PR_EXCLUSIVE_ACCESS_ALL_REGS; + } + + return 0; +} + +static inline uint8_t nvme_pr_cap_to_block(uint16_t nvme_pr_cap) +{ + uint8_t res =3D 0; + + res |=3D (nvme_pr_cap & NVME_PR_CAP_PTPL) ? + NVME_PR_CAP_PTPL : 0; + res |=3D (nvme_pr_cap & NVME_PR_CAP_WR_EX) ? + BLK_PR_CAP_WR_EX : 0; + res |=3D (nvme_pr_cap & NVME_PR_CAP_EX_AC) ? + BLK_PR_CAP_EX_AC : 0; + res |=3D (nvme_pr_cap & NVME_PR_CAP_WR_EX_RO) ? + BLK_PR_CAP_WR_EX_RO : 0; + res |=3D (nvme_pr_cap & NVME_PR_CAP_EX_AC_RO) ? + BLK_PR_CAP_EX_AC_RO : 0; + res |=3D (nvme_pr_cap & NVME_PR_CAP_WR_EX_AR) ? + BLK_PR_CAP_WR_EX_AR : 0; + res |=3D (nvme_pr_cap & NVME_PR_CAP_EX_AC_AR) ? + BLK_PR_CAP_EX_AC_AR : 0; + + return res; +} + +static inline uint8_t block_pr_cap_to_nvme(uint8_t block_pr_cap) +{ + uint16_t res =3D 0; + + res |=3D (block_pr_cap & BLK_PR_CAP_PTPL) ? + NVME_PR_CAP_PTPL : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_WR_EX) ? + NVME_PR_CAP_WR_EX : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_EX_AC) ? + NVME_PR_CAP_EX_AC : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_WR_EX_RO) ? + NVME_PR_CAP_WR_EX_RO : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_EX_AC_RO) ? + NVME_PR_CAP_EX_AC_RO : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_WR_EX_AR) ? + NVME_PR_CAP_WR_EX_AR : 0; + res |=3D (block_pr_cap & BLK_PR_CAP_EX_AC_AR) ? + NVME_PR_CAP_EX_AC_AR : 0; + + return res; +} + typedef struct NvmeSQueue { struct NvmeCtrl *ctrl; uint16_t sqid; --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493321; cv=none; d=zohomail.com; s=zohoarc; b=kceMQAFPubkFwtGU8bVh/DeNtq34cs4oZIGaSYNEmyR8VUX1glBCJTtTns3JOXeOuULLdS3hlDlRYDhZePXViagMjfGFGU86/NNDhNwQQ7jXL10ROE34Aj6mTh3DOg5UxwIK/ZF89htxllCwL8cPf9/bI8SD7Jrw/UjgNSxDZ5U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493321; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=FrkIIgQX6TLIKPjYDCoZVTqMBbiuqD+0ICFBlvY+Bo0=; b=CpkVX8l4BcSWw9cEIyjQSXLO4Dv3+dnph9xwEoMzs0iqKUVgtr3cmszyInagbCu34qRW5e3fW7lTJD+XYPF1i/tja1V8kibBXF+GT9FatuEVAF/Xizaa3xUVdqGrwI8jy/n9+kAEWcbPRBmUXtwerj4fOnHTeUW0S2i2Ryiqf3g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720493321126307.90585427820713; Mon, 8 Jul 2024 19:48:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0tw-0008E7-Qb; Mon, 08 Jul 2024 22:48:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0tp-0007gV-QA for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:13 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0tn-00048m-Tm for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:13 -0400 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-75c3afd7a50so2323379a12.2 for ; Mon, 08 Jul 2024 19:48:11 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.48.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:48:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493290; x=1721098090; darn=nongnu.org; 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; bh=FrkIIgQX6TLIKPjYDCoZVTqMBbiuqD+0ICFBlvY+Bo0=; b=lYmX58PnwTWdvy7IaL/UDm1Lp3g/dMSen6Dfsw3ZPdfdKMwdBpOChY6DHJ1I8l+rYL OKbOQjOSUxlnoZtlipbSssQ33Rw3co5et65TrvZ4iK0P35o59kMkO1ZpIorzk7abDKCm x8LxrqGn1sfmEMdOITCv0Mo+puHU3qots/q03q9NM9f1s5Q2fU5BylrHwAvrnU/isP1y tOGZRJJxTd6gc53ecN9s5Gd0Ozq/AtxKQMJtdtx4rDiuCerY8RAYFtWnLJNPcPXNgZLa sAoLU+Rqr+tqAz3jaMmW2kVSq6bm+DTfl4QqbvhXhehaYu9KNhGGS1rTSVEmDjRVrywX k5/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493290; x=1721098090; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FrkIIgQX6TLIKPjYDCoZVTqMBbiuqD+0ICFBlvY+Bo0=; b=TERc3FhdPfQ5QKQ0UQbluoUN+PH1QrAgX6T1Fw8N2CAOvana9K3pbyCdWgKvfx7bgF Gzxo6GCPakAGmqzj2erbItbXk8Ib8VHuWKD4mtr/2iUuQq8mT9gL0jELWeUbWU5LzsRI pCNE8pW1HakmWQkv96GCKYxAgRLqCkbqypuIx2LGXocjYHV6R6e3jsyHHILn9Zoovk+E DDIWgUj8t3NCGQWaBQe8KWL9pQFqw35bA02ZGhs/vG3qqryj27or3gPkzSR29BAMzqLa rkCXkgn3h2ZN05SZhgRSiWCJi+Cns+t0Bm6Oo70i/sMTv4IKYK967459sCz1nseAo7K7 /pPA== X-Forwarded-Encrypted: i=1; AJvYcCWX6D3o1qevdt8zG25TZOOlmzmz9HMf4RX1CJlC+XXJiyujR1an9VOpi9BE0HzF10cPwHr4w9Lu+99oWU+vjNDFIKNCBCo= X-Gm-Message-State: AOJu0YwEl0Ca3ZHuYQWQAl38nUEiOfDA3x30adpuz2FEwQp4J3AP9hNe azDW4lbyNJQoKM5ytzC/m900II7Kj4K1PLbEgmU7P7koKvnvWbr1YvBkfB9+2H8= X-Google-Smtp-Source: AGHT+IH4+UhEgX1vBHl1RyMAwyBv7NU0C2eEuu2qE9QAx1fgQ4ilwN5S5f6VLKdSayU3Yl7GbM7+iA== X-Received: by 2002:a05:6a20:258f:b0:1c0:d9c9:64f9 with SMTP id adf61e73a8af0-1c298243af3mr1415907637.36.1720493290387; Mon, 08 Jul 2024 19:48:10 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu , Klaus Jensen Subject: [PATCH v8 08/10] hw/nvme: enable ONCS and rescap function Date: Tue, 9 Jul 2024 10:47:04 +0800 Message-Id: <20240709024706.4108-9-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52b; envelope-from=luchangqi.123@bytedance.com; helo=mail-pg1-x52b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493322086100003 Content-Type: text/plain; charset="utf-8" This commit enables ONCS to support the reservation function at the controller level. Also enables rescap function in the namespace by detecting the supported reservation function in the backend driver. Reviewed-by: Klaus Jensen Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi Reviewed-by: Stefan Hajnoczi --- hw/nvme/ctrl.c | 3 ++- hw/nvme/ns.c | 5 +++++ include/block/nvme.h | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index 127c3d2383..ad212de723 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -8248,7 +8248,8 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pc= i_dev) id->nn =3D cpu_to_le32(NVME_MAX_NAMESPACES); id->oncs =3D cpu_to_le16(NVME_ONCS_WRITE_ZEROES | NVME_ONCS_TIMESTAMP | NVME_ONCS_FEATURES | NVME_ONCS_DSM | - NVME_ONCS_COMPARE | NVME_ONCS_COPY); + NVME_ONCS_COMPARE | NVME_ONCS_COPY | + NVME_ONCS_RESERVATIONS); =20 /* * NOTE: If this device ever supports a command set that does NOT use = 0x0 diff --git a/hw/nvme/ns.c b/hw/nvme/ns.c index ea8db175db..a5c903d727 100644 --- a/hw/nvme/ns.c +++ b/hw/nvme/ns.c @@ -20,6 +20,7 @@ #include "qemu/bitops.h" #include "sysemu/sysemu.h" #include "sysemu/block-backend.h" +#include "block/block_int.h" =20 #include "nvme.h" #include "trace.h" @@ -33,6 +34,7 @@ void nvme_ns_init_format(NvmeNamespace *ns) BlockDriverInfo bdi; int npdg, ret; int64_t nlbas; + uint8_t blk_pr_cap; =20 ns->lbaf =3D id_ns->lbaf[NVME_ID_NS_FLBAS_INDEX(id_ns->flbas)]; ns->lbasz =3D 1 << ns->lbaf.ds; @@ -55,6 +57,9 @@ void nvme_ns_init_format(NvmeNamespace *ns) } =20 id_ns->npda =3D id_ns->npdg =3D npdg - 1; + + blk_pr_cap =3D blk_bs(ns->blkconf.blk)->bl.pr_cap; + id_ns->rescap =3D block_pr_cap_to_nvme(blk_pr_cap); } =20 static int nvme_ns_init(NvmeNamespace *ns, Error **errp) diff --git a/include/block/nvme.h b/include/block/nvme.h index 8b125f7769..9b9eaeb3a7 100644 --- a/include/block/nvme.h +++ b/include/block/nvme.h @@ -1251,7 +1251,7 @@ enum NvmeIdCtrlOncs { NVME_ONCS_DSM =3D 1 << 2, NVME_ONCS_WRITE_ZEROES =3D 1 << 3, NVME_ONCS_FEATURES =3D 1 << 4, - NVME_ONCS_RESRVATIONS =3D 1 << 5, + NVME_ONCS_RESERVATIONS =3D 1 << 5, NVME_ONCS_TIMESTAMP =3D 1 << 6, NVME_ONCS_VERIFY =3D 1 << 7, NVME_ONCS_COPY =3D 1 << 8, --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493366; cv=none; d=zohomail.com; s=zohoarc; b=dILBeHvwrewcW3Ai4SBjam69eJVrQbtdx8IGRg9rL3uWnO1xmB2mUDXt9faLUIukgMCbEeIzAx7pQ7vCdbvNjpgQx73PhA4i53aFEVSZIXXd29SfzkZ/Wqk5xH4s/+jp8OrYtfO/OC5aUIDi8Ylpa6kgOz3fnnSCPzQ8+UftReE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493366; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vnnlaAsHMDMy1W0jqBEpl+WRDzxCFDRuHFC4XpL5tjY=; b=P25xf0lADE0/FSVMfLOI5p4EvmNLT2vUWHQE7Afp/RGTf16kyqb8gxHjlpukPSES9QNKKmExbJG8exPuHbY3yxPTh7t5zprFx+nK8YwxxwlRMaF85HclFkjXVZFrscUvcIur3hEGILE35n19JeIVilLslG0hdTPfM7V/xzSUHoM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720493366820541.1859969845663; Mon, 8 Jul 2024 19:49:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0u3-0000KQ-Co; Mon, 08 Jul 2024 22:48:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0tw-0008Iq-Ot for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:20 -0400 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0tu-0004BI-96 for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:20 -0400 Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-6bce380eb96so2487016a12.0 for ; Mon, 08 Jul 2024 19:48:17 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.48.12 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:48:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493297; x=1721098097; darn=nongnu.org; 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; bh=vnnlaAsHMDMy1W0jqBEpl+WRDzxCFDRuHFC4XpL5tjY=; b=N+9XLgsTcymeJCh5LAY9kc29q81rwMcK9t0zJpL1TzBZ9cI3izWmAeooYN/MSit4lw x4f4BY37utaXVCTssMw5McniNgfC0wtGWUW07+BQoDhZSNhFevFJzVlCIIL7TZdz7wPf rIRdG2ebb43AbFfnn4a9kE6q4kZO34K52gBXkuY84vqPpTQ4rlEokx7ue08eR2D0KRJ5 msYiRZx+duVgA70lLou7MP+kExQifwtGDZ5dkxE5SnaiteaSJiohrZs/knLpTVP7WgLJ tQgCsQsMEFnU/KPOXA8zozUW9Y0fhigd0649+APvbampz3IWbPsAv468tOziZy7xV1/Y ID0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493297; x=1721098097; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vnnlaAsHMDMy1W0jqBEpl+WRDzxCFDRuHFC4XpL5tjY=; b=KrD6KEO+StfjuvT6Hp1W/lCnC3GVAeg5XPMNRS7QG2ESvych/X4UjHsbLDlMhRX+/S Ci/7ILeq6g67Gs5TLEkNYLqJaf6fXz9JeniSfVqzFxlZBmhyhOji7HtnupHahD5fbqkZ ccaOOOiFR8+b0RdeHpq86zTAafIuXG4N5/IkrI0PgHmvUqT/rVMDWELghB1m3PqK+2f9 NfxIrfB95laMQ3kXcTwRqCbQCoETvjHB65vc3bmYdDHnez1ArUXlbtMUQjoPf33gH34Y NrKeShlSUvIFEWDDUUR6vrLq1j32m07ZM4GM+0YK/aft5Ucxp8DjsNZb3sA8Sbs1nYSn WnWg== X-Forwarded-Encrypted: i=1; AJvYcCUm34novPCeyGCfBKXcX99IpAfeqFy/6OoE0xL53lrsnHhTvJ+jsn1foSTwCEkcxU8A0zuiuh+LsnvSyNVWU4PKWPwq7q0= X-Gm-Message-State: AOJu0YzZa60b+m+C0HDT1zKRdQe49QMAKeBfGgL7Az4Phl4vsvmQvUMT 7dnh01BZ4yPAF+WC0A/+k93oNPKr6TrpnKaVos8FjxYHrePaipv+rpx2vzkrfHU= X-Google-Smtp-Source: AGHT+IEkpKwcZsl+ge7lGQPC4xY6yxacLqZdOodb8yyG8zapymsMf7HT6V8uS7sYp/vm/hgBfYlRWA== X-Received: by 2002:a05:6a20:734c:b0:1be:c262:9c0d with SMTP id adf61e73a8af0-1c2984cf07emr1385548637.44.1720493296617; Mon, 08 Jul 2024 19:48:16 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu , Klaus Jensen Subject: [PATCH v8 09/10] hw/nvme: add reservation protocal command Date: Tue, 9 Jul 2024 10:47:05 +0800 Message-Id: <20240709024706.4108-10-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52c; envelope-from=luchangqi.123@bytedance.com; helo=mail-pg1-x52c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493368358100003 Content-Type: text/plain; charset="utf-8" Add reservation acquire, reservation register, reservation release and reservation report commands in the nvme device layer. By introducing these commands, this enables the nvme device to perform reservation-related tasks, including querying keys, querying reservation status, registering reservation keys, initiating and releasing reservations, as well as clearing and preempting reservations held by other keys. These commands are crucial for management and control of shared storage resources in a persistent manner. Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi Acked-by: Klaus Jensen --- hw/nvme/ctrl.c | 323 ++++++++++++++++++++++++++++++++++++++++++- hw/nvme/nvme.h | 4 + include/block/nvme.h | 37 +++++ 3 files changed, 363 insertions(+), 1 deletion(-) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index ad212de723..a69a499078 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -294,6 +294,10 @@ static const uint32_t nvme_cse_iocs_nvm[256] =3D { [NVME_CMD_COMPARE] =3D NVME_CMD_EFF_CSUPP, [NVME_CMD_IO_MGMT_RECV] =3D NVME_CMD_EFF_CSUPP, [NVME_CMD_IO_MGMT_SEND] =3D NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_= LBCC, + [NVME_CMD_RESV_REGISTER] =3D NVME_CMD_EFF_CSUPP, + [NVME_CMD_RESV_REPORT] =3D NVME_CMD_EFF_CSUPP, + [NVME_CMD_RESV_ACQUIRE] =3D NVME_CMD_EFF_CSUPP, + [NVME_CMD_RESV_RELEASE] =3D NVME_CMD_EFF_CSUPP, }; =20 static const uint32_t nvme_cse_iocs_zoned[256] =3D { @@ -308,6 +312,10 @@ static const uint32_t nvme_cse_iocs_zoned[256] =3D { [NVME_CMD_ZONE_APPEND] =3D NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_= LBCC, [NVME_CMD_ZONE_MGMT_SEND] =3D NVME_CMD_EFF_CSUPP | NVME_CMD_EFF_= LBCC, [NVME_CMD_ZONE_MGMT_RECV] =3D NVME_CMD_EFF_CSUPP, + [NVME_CMD_RESV_REGISTER] =3D NVME_CMD_EFF_CSUPP, + [NVME_CMD_RESV_REPORT] =3D NVME_CMD_EFF_CSUPP, + [NVME_CMD_RESV_ACQUIRE] =3D NVME_CMD_EFF_CSUPP, + [NVME_CMD_RESV_RELEASE] =3D NVME_CMD_EFF_CSUPP, }; =20 static void nvme_process_sq(void *opaque); @@ -1745,6 +1753,7 @@ static void nvme_aio_err(NvmeRequest *req, int ret) =20 switch (req->cmd.opcode) { case NVME_CMD_READ: + case NVME_CMD_RESV_REPORT: status =3D NVME_UNRECOVERED_READ; break; case NVME_CMD_FLUSH: @@ -1752,6 +1761,9 @@ static void nvme_aio_err(NvmeRequest *req, int ret) case NVME_CMD_WRITE_ZEROES: case NVME_CMD_ZONE_APPEND: case NVME_CMD_COPY: + case NVME_CMD_RESV_REGISTER: + case NVME_CMD_RESV_ACQUIRE: + case NVME_CMD_RESV_RELEASE: status =3D NVME_WRITE_FAULT; break; default: @@ -2127,7 +2139,10 @@ static inline bool nvme_is_write(NvmeRequest *req) =20 return rw->opcode =3D=3D NVME_CMD_WRITE || rw->opcode =3D=3D NVME_CMD_ZONE_APPEND || - rw->opcode =3D=3D NVME_CMD_WRITE_ZEROES; + rw->opcode =3D=3D NVME_CMD_WRITE_ZEROES || + rw->opcode =3D=3D NVME_CMD_RESV_REGISTER || + rw->opcode =3D=3D NVME_CMD_RESV_ACQUIRE || + rw->opcode =3D=3D NVME_CMD_RESV_RELEASE; } =20 static void nvme_misc_cb(void *opaque, int ret) @@ -2692,6 +2707,304 @@ static uint16_t nvme_verify(NvmeCtrl *n, NvmeReques= t *req) return NVME_NO_COMPLETE; } =20 +typedef struct NvmeKeyInfo { + uint64_t cr_key; + uint64_t nr_key; +} NvmeKeyInfo; + +static uint16_t nvme_resv_register(NvmeCtrl *n, NvmeRequest *req) +{ + int ret; + NvmeKeyInfo key_info; + NvmeNamespace *ns =3D req->ns; + uint32_t cdw10 =3D le32_to_cpu(req->cmd.cdw10); + bool ignore_key =3D cdw10 >> 3 & 0x1; + uint8_t action =3D cdw10 & 0x7; + uint8_t ptpl =3D cdw10 >> 30 & 0x3; + bool aptpl; + + switch (ptpl) { + case NVME_RESV_PTPL_NO_CHANGE: + aptpl =3D (ns->id_ns.rescap & NVME_PR_CAP_PTPL) ? true : false; + break; + case NVME_RESV_PTPL_DISABLE: + aptpl =3D false; + break; + case NVME_RESV_PTPL_ENABLE: + aptpl =3D true; + break; + default: + return NVME_INVALID_FIELD; + } + + ret =3D nvme_h2c(n, (uint8_t *)&key_info, sizeof(NvmeKeyInfo), req); + if (ret) { + return ret; + } + + switch (action) { + case NVME_RESV_REGISTER_ACTION_REGISTER: + req->aiocb =3D blk_aio_pr_register(ns->blkconf.blk, 0, + key_info.nr_key, 0, aptpl, + ignore_key, nvme_misc_cb, + req); + break; + case NVME_RESV_REGISTER_ACTION_UNREGISTER: + req->aiocb =3D blk_aio_pr_register(ns->blkconf.blk, key_info.cr_ke= y, 0, + 0, aptpl, ignore_key, + nvme_misc_cb, req); + break; + case NVME_RESV_REGISTER_ACTION_REPLACE: + req->aiocb =3D blk_aio_pr_register(ns->blkconf.blk, key_info.cr_ke= y, + key_info.nr_key, 0, aptpl, ignore= _key, + nvme_misc_cb, req); + break; + default: + return NVME_INVALID_FIELD; + } + + return NVME_NO_COMPLETE; +} + +static uint16_t nvme_resv_release(NvmeCtrl *n, NvmeRequest *req) +{ + int ret; + uint64_t cr_key; + NvmeNamespace *ns =3D req->ns; + uint32_t cdw10 =3D le32_to_cpu(req->cmd.cdw10); + uint8_t action =3D cdw10 & 0x7; + NvmeResvType type =3D cdw10 >> 8 & 0xff; + + ret =3D nvme_h2c(n, (uint8_t *)&cr_key, sizeof(cr_key), req); + if (ret) { + return ret; + } + + switch (action) { + case NVME_RESV_RELEASE_ACTION_RELEASE: + req->aiocb =3D blk_aio_pr_release(ns->blkconf.blk, cr_key, + nvme_pr_type_to_block(type), + nvme_misc_cb, req); + break; + case NVME_RESV_RELEASE_ACTION_CLEAR: + req->aiocb =3D blk_aio_pr_clear(ns->blkconf.blk, cr_key, + nvme_misc_cb, req); + break; + default: + return NVME_INVALID_FIELD; + } + + return NVME_NO_COMPLETE; +} + +static uint16_t nvme_resv_acquire(NvmeCtrl *n, NvmeRequest *req) +{ + int ret; + NvmeKeyInfo key_info; + NvmeNamespace *ns =3D req->ns; + uint32_t cdw10 =3D le32_to_cpu(req->cmd.cdw10); + uint8_t action =3D cdw10 & 0x7; + NvmeResvType type =3D cdw10 >> 8 & 0xff; + + ret =3D nvme_h2c(n, (uint8_t *)&key_info, sizeof(NvmeKeyInfo), req); + if (ret) { + return ret; + } + + switch (action) { + case NVME_RESV_ACQUIRE_ACTION_ACQUIRE: + req->aiocb =3D blk_aio_pr_reserve(ns->blkconf.blk, key_info.cr_key, + nvme_pr_type_to_block(type), + nvme_misc_cb, req); + break; + case NVME_RESV_ACQUIRE_ACTION_PREEMPT: + req->aiocb =3D blk_aio_pr_preempt(ns->blkconf.blk, + key_info.cr_key, key_info.nr_key, + nvme_pr_type_to_block(type), + false, nvme_misc_cb, req); + break; + case NVME_RESV_ACQUIRE_ACTION_PREEMPT_AND_ABORT: + req->aiocb =3D blk_aio_pr_preempt(ns->blkconf.blk, key_info.cr_key, + key_info.nr_key, type, true, + nvme_misc_cb, req); + break; + default: + return NVME_INVALID_FIELD; + } + + return NVME_NO_COMPLETE; +} + +typedef struct NvmeResvKeys { + uint32_t generation; + uint32_t num_keys; + uint64_t *keys; + NvmeRequest *req; +} NvmeResvKeys; + +typedef struct NvmeReadReservation { + uint32_t generation; + uint64_t key; + BlockPrType type; + NvmeRequest *req; + NvmeResvKeys *keys_info; +} NvmeReadReservation; + +static int nvme_read_reservation_cb(NvmeReadReservation *reservation) +{ + int rc; + NvmeReservationStatus *nvme_status; + NvmeRequest *req =3D reservation->req; + NvmeCtrl *n =3D req->sq->ctrl; + NvmeResvKeys *keys_info =3D reservation->keys_info; + int len =3D sizeof(NvmeReservationStatusHeader) + + sizeof(NvmeRegisteredCtrl) * keys_info->num_keys; + + nvme_status =3D g_malloc(len); + nvme_status->header.gen =3D reservation->generation; + nvme_status->header.rtype =3D block_pr_type_to_nvme(reservation->type); + nvme_status->header.regctl =3D keys_info->num_keys; + for (int i =3D 0; i < keys_info->num_keys; i++) { + nvme_status->regctl_ds[i].cntlid =3D nvme_ctrl(req)->cntlid; + nvme_status->regctl_ds[i].rkey =3D keys_info->keys[i]; + nvme_status->regctl_ds[i].rcsts =3D keys_info->keys[i] =3D=3D + reservation->key ? 1 : 0; + /* hostid is not supported currently */ + memset(&nvme_status->regctl_ds[i].hostid, 0, 8); + } + + rc =3D nvme_c2h(n, (uint8_t *)nvme_status, len, req); + g_free(nvme_status); + return rc; +} + +static int nvme_read_reservation_ext_cb(NvmeReadReservation *reservation) +{ + int rc; + NvmeReservationStatusExt *nvme_status_ext; + NvmeRequest *req =3D reservation->req; + NvmeCtrl *n =3D req->sq->ctrl; + NvmeResvKeys *keys_info =3D reservation->keys_info; + int len =3D sizeof(NvmeReservationStatusHeader) + + sizeof(uint8_t) * 40 + + sizeof(NvmeRegisteredCtrlExt) * keys_info->num_keys; + + nvme_status_ext =3D g_malloc(len); + nvme_status_ext->header.gen =3D cpu_to_be32(reservation->generation); + nvme_status_ext->header.rtype =3D block_pr_type_to_nvme(reservation->t= ype); + nvme_status_ext->header.regctl =3D cpu_to_be16(keys_info->num_keys); + + for (int i =3D 0; i < keys_info->num_keys; i++) { + uint16_t ctnlid =3D nvme_ctrl(req)->cntlid; + nvme_status_ext->regctl_eds[i].cntlid =3D cpu_to_be16(ctnlid); + nvme_status_ext->regctl_eds[i].rkey =3D cpu_to_be64(keys_info->key= s[i]); + nvme_status_ext->regctl_eds[i].rcsts =3D keys_info->keys[i] =3D=3D + reservation->key ? 1 : 0; + /* hostid is not supported currently */ + memset(&nvme_status_ext->regctl_eds[i].hostid, 0, 16); + } + + rc =3D nvme_c2h(n, (uint8_t *)nvme_status_ext, len, req); + g_free(nvme_status_ext); + return rc; +} + +static void nvme_resv_read_reservation_cb(void *opaque, int ret) +{ + NvmeReadReservation *reservation =3D opaque; + NvmeRequest *req =3D reservation->req; + bool eds =3D req->cmd.cdw11 & 0x1; + NvmeResvKeys *keys_info =3D reservation->keys_info; + + if (ret < 0) { + goto out; + } + + if (eds) { + ret =3D nvme_read_reservation_ext_cb(reservation); + } else { + ret =3D nvme_read_reservation_cb(reservation); + } + +out: + g_free(keys_info->keys); + g_free(keys_info); + g_free(reservation); + nvme_misc_cb(req, ret); +} + +static void nvme_resv_read_keys_cb(void *opaque, int ret) +{ + NvmeResvKeys *keys_info =3D opaque; + NvmeRequest *req =3D keys_info->req; + NvmeNamespace *ns =3D req->ns; + NvmeReadReservation *reservation; + + if (ret < 0) { + goto out; + } + + keys_info->num_keys =3D MIN(ret, keys_info->num_keys); + reservation =3D g_new0(NvmeReadReservation, 1); + memset(reservation, 0, sizeof(*reservation)); + reservation->req =3D req; + reservation->keys_info =3D keys_info; + + req->aiocb =3D blk_aio_pr_read_reservation(ns->blkconf.blk, + &reservation->generation, &reservation->key, + &reservation->type, nvme_resv_read_reservation_cb, + reservation); + return; + +out: + g_free(keys_info->keys); + g_free(keys_info); + nvme_misc_cb(req, ret); +} + + +static uint16_t nvme_resv_report(NvmeCtrl *n, NvmeRequest *req) +{ + int num_keys; + uint32_t cdw10 =3D req->cmd.cdw10; + uint32_t cdw11 =3D req->cmd.cdw11; + int buflen =3D (cdw10 + 1) * sizeof(uint32_t); + bool eds =3D cdw11 & 0x1; + NvmeNamespace *ns =3D req->ns; + NvmeResvKeys *keys_info; + + if (eds) { + if (buflen < sizeof(NvmeReservationStatusHeader) + + sizeof(uint8_t) * 40) { + return NVME_INVALID_FIELD; + } + + num_keys =3D (buflen - sizeof(NvmeReservationStatusHeader) - + sizeof(uint8_t) * 40) / + sizeof(struct NvmeRegisteredCtrlExt); + } else { + if (buflen < sizeof(NvmeReservationStatusHeader)) { + return NVME_INVALID_FIELD; + } + + num_keys =3D (buflen - sizeof(NvmeReservationStatusHeader)) / + sizeof(struct NvmeRegisteredCtrl); + } + + keys_info =3D g_new0(NvmeResvKeys, 1); + keys_info->generation =3D 0; + /* num_keys is the maximum number of keys that can be transmitted */ + keys_info->num_keys =3D num_keys; + keys_info->keys =3D g_malloc(sizeof(uint64_t) * num_keys); + keys_info->req =3D req; + + req->aiocb =3D blk_aio_pr_read_keys(ns->blkconf.blk, &keys_info->gener= ation, + keys_info->num_keys, keys_info->keys, + nvme_resv_read_keys_cb, keys_info); + + return NVME_NO_COMPLETE; +} + typedef struct NvmeCopyAIOCB { BlockAIOCB common; BlockAIOCB *aiocb; @@ -4469,6 +4782,14 @@ static uint16_t nvme_io_cmd(NvmeCtrl *n, NvmeRequest= *req) return nvme_dsm(n, req); case NVME_CMD_VERIFY: return nvme_verify(n, req); + case NVME_CMD_RESV_REGISTER: + return nvme_resv_register(n, req); + case NVME_CMD_RESV_REPORT: + return nvme_resv_report(n, req); + case NVME_CMD_RESV_ACQUIRE: + return nvme_resv_acquire(n, req); + case NVME_CMD_RESV_RELEASE: + return nvme_resv_release(n, req); case NVME_CMD_COPY: return nvme_copy(n, req); case NVME_CMD_ZONE_MGMT_SEND: diff --git a/hw/nvme/nvme.h b/hw/nvme/nvme.h index 6d0e456348..fdd919c2b1 100644 --- a/hw/nvme/nvme.h +++ b/hw/nvme/nvme.h @@ -470,6 +470,10 @@ static inline const char *nvme_io_opc_str(uint8_t opc) case NVME_CMD_ZONE_MGMT_SEND: return "NVME_ZONED_CMD_MGMT_SEND"; case NVME_CMD_ZONE_MGMT_RECV: return "NVME_ZONED_CMD_MGMT_RECV"; case NVME_CMD_ZONE_APPEND: return "NVME_ZONED_CMD_ZONE_APPEND"; + case NVME_CMD_RESV_REGISTER: return "NVME_CMD_RESV_REGISTER"; + case NVME_CMD_RESV_REPORT: return "NVME_CMD_RESV_REPORT"; + case NVME_CMD_RESV_ACQUIRE: return "NVME_CMD_RESV_ACQUIRE"; + case NVME_CMD_RESV_RELEASE: return "NVME_CMD_RESV_RELEASE"; default: return "NVME_NVM_CMD_UNKNOWN"; } } diff --git a/include/block/nvme.h b/include/block/nvme.h index 9b9eaeb3a7..2f24570d4a 100644 --- a/include/block/nvme.h +++ b/include/block/nvme.h @@ -702,6 +702,43 @@ typedef enum NVMEPrCap { NVME_PR_CAP_EX_AC_AR), } NvmePrCap; =20 +typedef struct QEMU_PACKED NvmeRegisteredCtrl { + uint16_t cntlid; + uint8_t rcsts; + uint8_t rsvd3[5]; + uint8_t hostid[8]; + uint64_t rkey; +} NvmeRegisteredCtrl; + +typedef struct QEMU_PACKED NvmeRegisteredCtrlExt { + uint16_t cntlid; + uint8_t rcsts; + uint8_t rsvd3[5]; + uint64_t rkey; + uint8_t hostid[16]; + uint8_t rsvd32[32]; +} NvmeRegisteredCtrlExt; + +typedef struct QEMU_PACKED NvmeReservationStatusHeader { + uint32_t gen; + uint8_t rtype; + uint16_t regctl; + uint16_t resv5; + uint8_t ptpls; + uint8_t resv10[14]; +} NvmeReservationStatusHeader; + +typedef struct QEMU_PACKED NvmeReservationStatus { + struct NvmeReservationStatusHeader header; + struct NvmeRegisteredCtrl regctl_ds[]; +} NvmeReservationStatus; + +typedef struct QEMU_PACKED NvmeReservationStatusExt { + struct NvmeReservationStatusHeader header; + uint8_t rsvd24[40]; + struct NvmeRegisteredCtrlExt regctl_eds[]; +} NvmeReservationStatusExt; + typedef struct QEMU_PACKED NvmeDeleteQ { uint8_t opcode; uint8_t flags; --=20 2.20.1 From nobody Sun Nov 24 18:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1720493337; cv=none; d=zohomail.com; s=zohoarc; b=JJCXoUM/Hphos0PdjMGhdFI4TrKrK22cgAyEf46TfyAG/Do4RpFrK4/l3VPGbAc3n5LEsDtYsaVxyJRNtdPUnE4HEPiltDvKkn98w/0nCkyAT9LBmZGnWRW/QiC0/d5nruS/sasaPxqdexdqf+0y+ESTop3MuXWZd9pZvw0CmVU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720493337; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=wf7JaRE116YRJ47jpXrVccE2M8602dkABnf39B3fRt4=; b=KJIfiZR4+wDOAhtVbN7qYMZwv6iz72br6YZ6L8rvjO1/4jAqCIifiD/LgqplU8zoBdpJ2LrdWPeWMb9iT2oVYveMMorB8WnH7i6HEjStSOrDllr7XA9M3ZCHdUuYwnsj+Fzevmi5qigIxhplZWm52SRLZmVBeCKQ5h9Y5dadxNs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720493337066208.30000534291162; Mon, 8 Jul 2024 19:48:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sR0uC-0001KD-Bm; Mon, 08 Jul 2024 22:48:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sR0u4-0000eW-RA for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:30 -0400 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sR0u1-0004D8-95 for qemu-devel@nongnu.org; Mon, 08 Jul 2024 22:48:28 -0400 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-2c96187b3d1so2627321a91.3 for ; Mon, 08 Jul 2024 19:48:24 -0700 (PDT) Received: from TF4D9JK212.bytedance.net ([61.213.176.9]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a97e8a5sm8964661a91.27.2024.07.08.19.48.18 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 08 Jul 2024 19:48:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1720493303; x=1721098103; darn=nongnu.org; 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; bh=wf7JaRE116YRJ47jpXrVccE2M8602dkABnf39B3fRt4=; b=WxQaQHsbi1dBvPbYF5fI69HtDjQb+IHTo7GyC2QRqQtqTuUFQ9WEJpww6MY7dDG8Jb 0OZ4/nMihtlBZMKOT/D5hYytxhT5ut27ZZv9VfiqX9mUBpA6QHrr5mvsw4vtw7JwUJZK lKcZf/ru+cQ3I1HbSAXLemRKGJ8tMi2SSfv48NL9uZUtUcoAHo15p3eqTEb0ZoZmcNhQ /qLg1Ahliqu+TS4kANs0UTcoaqF13hG3L+sy+X2PZZ5niYwi97jkCHnleJGdO1wXJTn5 gLyRdky6HQCyOltwfGRwZagRcY4MTItjMMPdtSrHd2h4hSZX/aRnpnQXctiFzAyHw0x+ KBQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720493303; x=1721098103; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wf7JaRE116YRJ47jpXrVccE2M8602dkABnf39B3fRt4=; b=BXT4Sg/mU260Rd0VW9bT/diFpaOxg1by8SwXEc6kVaJSFclw1Y56jy9xZioEaV8PH6 TEWsQOn3EwACStUW3iNjGj+FdBpPyTsS+Cqlbg2GkN55YbjnO+MPc9Y79NfSK+Us8j0I gv/Ya8ObZk1rk+Fl+P77PitB661pTAwDP/2J9jgFf/3WN4IFQzig3tPpQi7JlvcPYAcA wtaWIqMOp8GhIk4tEvI4nWA2RMdNvmbPOhe/iVKVeiRyRnPsjqolA9ycOZgw+4+IAC1h rz/9Cn0zFos5o4r+OPfCJwiqMAmQFgUzjrnTc1CkTXgutFOkQF5KvIUofjql4qAu7GKV r6fA== X-Forwarded-Encrypted: i=1; AJvYcCX2PdFbQmjzYNuEVGazY5XEtJbX/nUKBoTotKX0wucF2NvGwqSouM/QrWvNTo5xZN3ulW1oVyWPH81aT1Mt5B0gspzwSS0= X-Gm-Message-State: AOJu0YzK7XOUtNpuhr1tEFIG7AuvipVOMoavUIQMxzo7i6CgxtYckxUr Umf/WeWqdt4ivMlzeXmKMD+ZzacSkv2uhTrvajLksQcuomvURiJp6DXzfJM3r88= X-Google-Smtp-Source: AGHT+IGa45nztKedRfLrV6ZUBisf1fzhspNzw25dCnjTCTA13VUACsC7TaEXCrrl9h51UwXUUwSHiw== X-Received: by 2002:a17:90a:fe13:b0:2c9:90f5:cfca with SMTP id 98e67ed59e1d1-2ca35d5bbdbmr1145438a91.42.1720493303351; Mon, 08 Jul 2024 19:48:23 -0700 (PDT) From: Changqi Lu To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, stefanha@redhat.com, fam@euphon.net, ronniesahlberg@gmail.com, pbonzini@redhat.com, pl@dlhnet.de, kbusch@kernel.org, its@irrelevant.dk, foss@defmacro.it, philmd@linaro.org, pizhenwei@bytedance.com, Changqi Lu Subject: [PATCH v8 10/10] block/iscsi: add persistent reservation in/out driver Date: Tue, 9 Jul 2024 10:47:06 +0800 Message-Id: <20240709024706.4108-11-luchangqi.123@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240709024706.4108-1-luchangqi.123@bytedance.com> References: <20240709024706.4108-1-luchangqi.123@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=luchangqi.123@bytedance.com; helo=mail-pj1-x1033.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1720493338143100003 Content-Type: text/plain; charset="utf-8" Add persistent reservation in/out operations for iscsi driver. The following methods are implemented: bdrv_co_pr_read_keys, bdrv_co_pr_read_reservation, bdrv_co_pr_register, bdrv_co_pr_reserve, bdrv_co_pr_release, bdrv_co_pr_clear and bdrv_co_pr_preempt. Signed-off-by: Changqi Lu Signed-off-by: zhenwei pi Reviewed-by: Stefan Hajnoczi --- block/iscsi.c | 425 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 425 insertions(+) diff --git a/block/iscsi.c b/block/iscsi.c index 2ff14b7472..ba51f6d016 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -96,6 +96,7 @@ typedef struct IscsiLun { unsigned long *allocmap_valid; long allocmap_size; int cluster_size; + uint8_t pr_cap; bool use_16_for_rw; bool write_protected; bool lbpme; @@ -280,6 +281,10 @@ iscsi_co_generic_cb(struct iscsi_context *iscsi, int s= tatus, iTask->err_code =3D -error; iTask->err_str =3D g_strdup(iscsi_get_error(iscsi)); } + } else if (status =3D=3D SCSI_STATUS_RESERVATION_CONFLICT) { + iTask->err_code =3D -EBADE; + error_report("iSCSI Persistent Reservation Conflict: %s", + iscsi_get_error(iscsi)); } } } @@ -1792,6 +1797,50 @@ static void iscsi_save_designator(IscsiLun *lun, } } =20 +/* + * Ensure iscsi_open() must succeed, weather or not the target + * implement SCSI_PR_IN_REPORT_CAPABILITIES. + */ +static void iscsi_get_pr_cap_sync(IscsiLun *iscsilun) +{ + struct scsi_task *task =3D NULL; + struct scsi_persistent_reserve_in_report_capabilities *rc =3D NULL; + int retries =3D ISCSI_CMD_RETRIES; + int xferlen =3D sizeof(struct scsi_persistent_reserve_in_report_capabi= lities); + + do { + if (task !=3D NULL) { + scsi_free_scsi_task(task); + task =3D NULL; + } + + task =3D iscsi_persistent_reserve_in_sync(iscsilun->iscsi, + iscsilun->lun, SCSI_PR_IN_REPORT_CAPABILITIES, xferlen); + if (task !=3D NULL && task->status =3D=3D SCSI_STATUS_GOOD) { + rc =3D scsi_datain_unmarshall(task); + if (rc =3D=3D NULL) { + error_report("iSCSI: Failed to unmarshall " + "report capabilities data."); + } else { + iscsilun->pr_cap =3D + scsi_pr_cap_to_block(rc->persistent_reservation_type_m= ask); + iscsilun->pr_cap |=3D (rc->ptpl_a) ? BLK_PR_CAP_PTPL := 0; + } + break; + } + } while (task !=3D NULL && task->status =3D=3D SCSI_STATUS_CHECK_CONDI= TION + && task->sense.key =3D=3D SCSI_SENSE_UNIT_ATTENTION + && retries-- > 0); + + if (task =3D=3D NULL || task->status !=3D SCSI_STATUS_GOOD) { + error_report("iSCSI: failed to send report capabilities command."); + } + + if (task) { + scsi_free_scsi_task(task); + } +} + static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) { @@ -2024,6 +2073,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *op= tions, int flags, bs->supported_zero_flags =3D BDRV_REQ_MAY_UNMAP; } =20 + iscsi_get_pr_cap_sync(iscsilun); out: qemu_opts_del(opts); g_free(initiator_name); @@ -2110,6 +2160,8 @@ static void iscsi_refresh_limits(BlockDriverState *bs= , Error **errp) bs->bl.opt_transfer =3D pow2floor(iscsilun->bl.opt_xfer_len * iscsilun->block_size); } + + bs->bl.pr_cap =3D iscsilun->pr_cap; } =20 /* Note that this will not re-establish a connection with an iSCSI target = - it @@ -2408,6 +2460,371 @@ out_unlock: return r; } =20 +static int coroutine_fn +iscsi_co_pr_read_keys(BlockDriverState *bs, uint32_t *generation, + uint32_t num_keys, uint64_t *keys) +{ + IscsiLun *iscsilun =3D bs->opaque; + QEMUIOVector qiov; + struct IscsiTask iTask; + int xferlen =3D sizeof(struct scsi_persistent_reserve_in_read_keys) + + sizeof(uint64_t) * num_keys; + g_autofree uint8_t *buf =3D g_malloc0(xferlen); + int32_t num_collect_keys =3D 0; + int r =3D 0; + + qemu_iovec_init_buf(&qiov, buf, xferlen); + iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); +retry: + iTask.task =3D iscsi_persistent_reserve_in_task(iscsilun->iscsi, + iscsilun->lun, SCSI_PR_IN_READ_KEYS, xferlen, + iscsi_co_generic_cb, &iTask); + + if (iTask.task =3D=3D NULL) { + qemu_mutex_unlock(&iscsilun->mutex); + return -ENOMEM; + } + + scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *)qiov.iov, qiov.n= iov); + iscsi_co_wait_for_task(&iTask, iscsilun); + + if (iTask.task !=3D NULL) { + scsi_free_scsi_task(iTask.task); + iTask.task =3D NULL; + } + + if (iTask.do_retry) { + iTask.complete =3D 0; + goto retry; + } + + if (iTask.status !=3D SCSI_STATUS_GOOD) { + error_report("iSCSI PERSISTENT_RESERVE_IN failed: %s", iTask.err_s= tr); + r =3D iTask.err_code; + goto out; + } + + memcpy(generation, &buf[0], 4); + *generation =3D be32_to_cpu(*generation); + memcpy(&num_collect_keys, &buf[4], 4); + num_collect_keys =3D be32_to_cpu(num_collect_keys) / sizeof(uint64_t); + if (num_collect_keys > num_keys) { + r =3D -EINVAL; + goto out; + } + + for (int i =3D 0; i < num_collect_keys; i++) { + memcpy(&keys[i], &buf[8 + i * 8], 8); + keys[i] =3D be64_to_cpu(keys[i]); + } + r =3D num_collect_keys; + +out: + qemu_mutex_unlock(&iscsilun->mutex); + g_free(iTask.err_str); + g_free(buf); + return r; +} + +static int coroutine_fn +iscsi_co_pr_read_reservation(BlockDriverState *bs, uint32_t *generation, + uint64_t *key, BlockPrType *type) +{ + IscsiLun *iscsilun =3D bs->opaque; + QEMUIOVector qiov; + struct IscsiTask iTask; + int xferlen =3D sizeof(struct scsi_persistent_reserve_in_read_reservat= ion); + g_autofree uint8_t *buf =3D g_malloc0(xferlen); + uint8_t scope_type =3D 0; + int32_t num_collect_keys =3D 0; + int r =3D 0; + + qemu_iovec_init_buf(&qiov, buf, xferlen); + iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); +retry: + iTask.task =3D iscsi_persistent_reserve_in_task(iscsilun->iscsi, + iscsilun->lun, SCSI_PR_IN_READ_RESERVATION, + xferlen, iscsi_co_generic_cb, &iTask); + + if (iTask.task =3D=3D NULL) { + qemu_mutex_unlock(&iscsilun->mutex); + return -ENOMEM; + } + + scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *)qiov.iov, qiov.n= iov); + iscsi_co_wait_for_task(&iTask, iscsilun); + + if (iTask.task !=3D NULL) { + scsi_free_scsi_task(iTask.task); + iTask.task =3D NULL; + } + + if (iTask.do_retry) { + iTask.complete =3D 0; + goto retry; + } + + if (iTask.status !=3D SCSI_STATUS_GOOD) { + error_report("iSCSI PERSISTENT_RESERVE_IN failed: %s", iTask.err_s= tr); + r =3D iTask.err_code; + goto out; + } + + memcpy(generation, &buf[0], 4); + *generation =3D be32_to_cpu(*generation); + memcpy(key, &buf[8], 8); + *key =3D be64_to_cpu(*key); + memcpy(&scope_type, &buf[21], 1); + *type =3D scsi_pr_type_to_block(scope_type & 0xf); + memcpy(&num_collect_keys, &buf[4], 4); + r =3D be32_to_cpu(num_collect_keys) / sizeof(uint64_t); +out: + qemu_mutex_unlock(&iscsilun->mutex); + g_free(iTask.err_str); + g_free(buf); + return r; +} + +static int coroutine_fn +iscsi_co_pr_register(BlockDriverState *bs, uint64_t old_key, + uint64_t new_key, BlockPrType type, + bool ptpl, bool ignore_key) +{ + IscsiLun *iscsilun =3D bs->opaque; + struct IscsiTask iTask; + struct scsi_persistent_reserve_out_basic basic; + SCSIPrOutAction action =3D ignore_key ? SCSI_PR_OUT_REG_AND_IGNORE_KEY= : + SCSI_PR_OUT_REGISTER; + int r =3D 0; + + basic.reservation_key =3D old_key; + basic.service_action_reservation_key =3D new_key; + basic.aptpl =3D ptpl ? 1 : 0; + + iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); +retry: + iTask.task =3D iscsi_persistent_reserve_out_task(iscsilun->iscsi, + iscsilun->lun, action, 0, block_pr_type_to_scsi(type), + &basic, iscsi_co_generic_cb, &iTask); + + if (iTask.task =3D=3D NULL) { + qemu_mutex_unlock(&iscsilun->mutex); + return -ENOMEM; + } + + iscsi_co_wait_for_task(&iTask, iscsilun); + + if (iTask.task !=3D NULL) { + scsi_free_scsi_task(iTask.task); + iTask.task =3D NULL; + } + + if (iTask.do_retry) { + iTask.complete =3D 0; + goto retry; + } + + if (iTask.status !=3D SCSI_STATUS_GOOD) { + error_report("iSCSI PERSISTENT_RESERVE_OUT failed: %s", iTask.err_= str); + r =3D iTask.err_code; + } + + qemu_mutex_unlock(&iscsilun->mutex); + + g_free(iTask.err_str); + return r; +} + +static int coroutine_fn +iscsi_co_pr_reserve(BlockDriverState *bs, uint64_t key, BlockPrType type) +{ + IscsiLun *iscsilun =3D bs->opaque; + struct IscsiTask iTask; + struct scsi_persistent_reserve_out_basic basic; + int r =3D 0; + + basic.reservation_key =3D key; + iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); +retry: + iTask.task =3D iscsi_persistent_reserve_out_task(iscsilun->iscsi, + iscsilun->lun, SCSI_PR_OUT_RESERVE, 0, + block_pr_type_to_scsi(type), &basic, + iscsi_co_generic_cb, &iTask); + + if (iTask.task =3D=3D NULL) { + qemu_mutex_unlock(&iscsilun->mutex); + return -ENOMEM; + } + + + iscsi_co_wait_for_task(&iTask, iscsilun); + + if (iTask.task !=3D NULL) { + scsi_free_scsi_task(iTask.task); + iTask.task =3D NULL; + } + + if (iTask.do_retry) { + iTask.complete =3D 0; + goto retry; + } + + if (iTask.status !=3D SCSI_STATUS_GOOD) { + error_report("iSCSI PERSISTENT_RESERVE_OUT failed: %s", iTask.err_= str); + r =3D iTask.err_code; + } + + qemu_mutex_unlock(&iscsilun->mutex); + + g_free(iTask.err_str); + return r; +} + +static int coroutine_fn +iscsi_co_pr_release(BlockDriverState *bs, uint64_t key, BlockPrType type) +{ + IscsiLun *iscsilun =3D bs->opaque; + struct IscsiTask iTask; + struct scsi_persistent_reserve_out_basic basic; + int r =3D 0; + + basic.reservation_key =3D key; + iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); +retry: + iTask.task =3D iscsi_persistent_reserve_out_task(iscsilun->iscsi, + iscsilun->lun, SCSI_PR_OUT_RELEASE, 0, + block_pr_type_to_scsi(type), &basic, + iscsi_co_generic_cb, &iTask); + + if (iTask.task =3D=3D NULL) { + qemu_mutex_unlock(&iscsilun->mutex); + return -ENOMEM; + } + + + iscsi_co_wait_for_task(&iTask, iscsilun); + + if (iTask.task !=3D NULL) { + scsi_free_scsi_task(iTask.task); + iTask.task =3D NULL; + } + + if (iTask.do_retry) { + iTask.complete =3D 0; + goto retry; + } + + if (iTask.status !=3D SCSI_STATUS_GOOD) { + error_report("iSCSI PERSISTENT_RESERVE_OUT failed: %s", iTask.err_= str); + r =3D iTask.err_code; + } + + qemu_mutex_unlock(&iscsilun->mutex); + + g_free(iTask.err_str); + return r; +} + +static int coroutine_fn +iscsi_co_pr_clear(BlockDriverState *bs, uint64_t key) +{ + IscsiLun *iscsilun =3D bs->opaque; + struct IscsiTask iTask; + struct scsi_persistent_reserve_out_basic basic; + int r =3D 0; + + basic.reservation_key =3D key; + iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); +retry: + iTask.task =3D iscsi_persistent_reserve_out_task(iscsilun->iscsi, + iscsilun->lun, SCSI_PR_OUT_CLEAR, 0, 0, &basic, + iscsi_co_generic_cb, &iTask); + + if (iTask.task =3D=3D NULL) { + qemu_mutex_unlock(&iscsilun->mutex); + return -ENOMEM; + } + + + iscsi_co_wait_for_task(&iTask, iscsilun); + + if (iTask.task !=3D NULL) { + scsi_free_scsi_task(iTask.task); + iTask.task =3D NULL; + } + + if (iTask.do_retry) { + iTask.complete =3D 0; + goto retry; + } + + if (iTask.status !=3D SCSI_STATUS_GOOD) { + error_report("iSCSI PERSISTENT_RESERVE_OUT failed: %s", iTask.err_= str); + r =3D iTask.err_code; + } + + qemu_mutex_unlock(&iscsilun->mutex); + + g_free(iTask.err_str); + return r; +} + +static int coroutine_fn +iscsi_co_pr_preempt(BlockDriverState *bs, uint64_t old_key, + uint64_t new_key, BlockPrType type, bool abort) +{ + IscsiLun *iscsilun =3D bs->opaque; + struct IscsiTask iTask; + struct scsi_persistent_reserve_out_basic basic; + SCSIPrOutAction action =3D abort ? SCSI_PR_OUT_PREEMPT_AND_ABORT : + SCSI_PR_OUT_PREEMPT; + int r =3D 0; + + basic.reservation_key =3D old_key; + basic.service_action_reservation_key =3D new_key; + + iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); +retry: + iTask.task =3D iscsi_persistent_reserve_out_task(iscsilun->iscsi, + iscsilun->lun, action, 0, block_pr_type_to_scsi(type), + &basic, iscsi_co_generic_cb, &iTask); + + if (iTask.task =3D=3D NULL) { + qemu_mutex_unlock(&iscsilun->mutex); + return -ENOMEM; + } + + + iscsi_co_wait_for_task(&iTask, iscsilun); + + if (iTask.task !=3D NULL) { + scsi_free_scsi_task(iTask.task); + iTask.task =3D NULL; + } + + if (iTask.do_retry) { + iTask.complete =3D 0; + goto retry; + } + + if (iTask.status !=3D SCSI_STATUS_GOOD) { + error_report("iSCSI PERSISTENT_RESERVE_OUT failed: %s", iTask.err_= str); + r =3D iTask.err_code; + } + + qemu_mutex_unlock(&iscsilun->mutex); + + g_free(iTask.err_str); + return r; +} + =20 static const char *const iscsi_strong_runtime_opts[] =3D { "transport", @@ -2451,6 +2868,14 @@ static BlockDriver bdrv_iscsi =3D { .bdrv_co_writev =3D iscsi_co_writev, .bdrv_co_flush_to_disk =3D iscsi_co_flush, =20 + .bdrv_co_pr_read_keys =3D iscsi_co_pr_read_keys, + .bdrv_co_pr_read_reservation =3D iscsi_co_pr_read_reservation, + .bdrv_co_pr_register =3D iscsi_co_pr_register, + .bdrv_co_pr_reserve =3D iscsi_co_pr_reserve, + .bdrv_co_pr_release =3D iscsi_co_pr_release, + .bdrv_co_pr_clear =3D iscsi_co_pr_clear, + .bdrv_co_pr_preempt =3D iscsi_co_pr_preempt, + #ifdef __linux__ .bdrv_aio_ioctl =3D iscsi_aio_ioctl, #endif --=20 2.20.1