From nobody Sun Nov 24 20:39:09 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