From patchwork Wed Jun 17 10:20:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lin Ma X-Patchwork-Id: 11609589 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2268613A0 for ; Wed, 17 Jun 2020 10:28:20 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 03BAD208B3 for ; Wed, 17 Jun 2020 10:28:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 03BAD208B3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:60708 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlVIp-00079O-81 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 17 Jun 2020 06:28:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54406) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jlVGq-0004GS-77; Wed, 17 Jun 2020 06:26:16 -0400 Received: from [114.255.249.163] (port=49827 helo=localhost) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlVGo-0007Ok-EB; Wed, 17 Jun 2020 06:26:15 -0400 Received: by localhost (Postfix, from userid 0) id 16553140E50; Wed, 17 Jun 2020 18:20:24 +0800 (CST) From: Lin Ma To: qemu-block@nongnu.org Subject: [PATCH v2 1/3] block: Add bdrv_co_get_lba_status Date: Wed, 17 Jun 2020 18:20:17 +0800 Message-Id: <20200617102019.29652-2-lma@suse.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200617102019.29652-1-lma@suse.com> References: <20200617102019.29652-1-lma@suse.com> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 114.255.249.163 (failed) Received-SPF: none client-ip=114.255.249.163; envelope-from=root@localhost; helo=localhost X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/17 06:20:25 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: 68 X-Spam_score: 6.8 X-Spam_bar: ++++++ X-Spam_report: (6.8 / 5.0 requ) BAYES_00=-1.9, FSL_HELO_NON_FQDN_1=0.001, HEADER_FROM_DIFFERENT_DOMAINS=1, HELO_LOCALHOST=3.828, RCVD_IN_MSPIKE_BL=0.01, RCVD_IN_MSPIKE_ZBI=2.7, RCVD_IN_XBL=0.375, RDNS_NONE=0.793, SPF_NONE=0.001 autolearn=_AUTOLEARN X-Spam_action: reject X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, Lin Ma , pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" The get lba status wrapper based on the bdrv_block_status. The following patches will add GET LBA STATUS 16 support for scsi emulation layer. Signed-off-by: Lin Ma --- block/io.c | 43 +++++++++++++++++++++++++++++++++++++++ include/block/block_int.h | 5 +++++ 2 files changed, 48 insertions(+) diff --git a/block/io.c b/block/io.c index df8f2a98d4..2064016b19 100644 --- a/block/io.c +++ b/block/io.c @@ -2208,6 +2208,49 @@ int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild *child, int64_t offset, BDRV_REQ_ZERO_WRITE | flags); } +int coroutine_fn +bdrv_co_get_lba_status(BdrvChild *child, int64_t offset, int64_t bytes, + uint32_t *num_blocks, uint32_t *is_deallocated) +{ + BlockDriverState *bs = child->bs; + int ret = 0; + int64_t target_size, count = 0; + bool first = true; + uint8_t wanted_bit1 = 0; + + target_size = bdrv_getlength(bs); + if (target_size < 0) { + return -EIO; + } + + if (offset < 0 || bytes < 0) { + return -EIO; + } + + for ( ; offset <= target_size - bytes; offset += count) { + ret = bdrv_block_status(bs, offset, bytes, &count, NULL, NULL); + if (ret < 0) { + goto out; + } + if (first) { + if (ret & BDRV_BLOCK_ZERO) { + wanted_bit1 = BDRV_BLOCK_ZERO >> 1; + *is_deallocated = 1; + } else { + wanted_bit1 = 0; + } + first = false; + } + if ((ret & BDRV_BLOCK_ZERO) >> 1 == wanted_bit1) { + (*num_blocks)++; + } else { + break; + } + } +out: + return ret; +} + /* * Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not. */ diff --git a/include/block/block_int.h b/include/block/block_int.h index 791de6a59c..43f90591b9 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -1296,6 +1296,11 @@ int coroutine_fn bdrv_co_block_status_from_backing(BlockDriverState *bs, int64_t *pnum, int64_t *map, BlockDriverState **file); +int coroutine_fn bdrv_co_get_lba_status(BdrvChild *child, + int64_t offset, + int64_t bytes, + uint32_t *num_blocks, + uint32_t *is_deallocated); const char *bdrv_get_parent_name(const BlockDriverState *bs); void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp); bool blk_dev_has_removable_media(BlockBackend *blk); From patchwork Wed Jun 17 10:20:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lin Ma X-Patchwork-Id: 11609585 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4741613A0 for ; Wed, 17 Jun 2020 10:27:53 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 27A8B208B3 for ; Wed, 17 Jun 2020 10:27:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 27A8B208B3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:58458 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlVI5-0006BO-GX for patchwork-qemu-devel@patchwork.kernel.org; Wed, 17 Jun 2020 06:27:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54434) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jlVGr-0004Hz-Ah; Wed, 17 Jun 2020 06:26:17 -0400 Received: from [114.255.249.163] (port=49820 helo=localhost) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlVGo-0007Oh-G5; Wed, 17 Jun 2020 06:26:17 -0400 Received: by localhost (Postfix, from userid 0) id 1939F1415E5; Wed, 17 Jun 2020 18:20:24 +0800 (CST) From: Lin Ma To: qemu-block@nongnu.org Subject: [PATCH v2 2/3] block: Add GET LBA STATUS support Date: Wed, 17 Jun 2020 18:20:18 +0800 Message-Id: <20200617102019.29652-3-lma@suse.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200617102019.29652-1-lma@suse.com> References: <20200617102019.29652-1-lma@suse.com> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 114.255.249.163 (failed) Received-SPF: none client-ip=114.255.249.163; envelope-from=root@localhost; helo=localhost X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/17 06:20:25 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: 68 X-Spam_score: 6.8 X-Spam_bar: ++++++ X-Spam_report: (6.8 / 5.0 requ) BAYES_00=-1.9, FSL_HELO_NON_FQDN_1=0.001, HEADER_FROM_DIFFERENT_DOMAINS=1, HELO_LOCALHOST=3.828, RCVD_IN_MSPIKE_BL=0.01, RCVD_IN_MSPIKE_ZBI=2.7, RCVD_IN_XBL=0.375, RDNS_NONE=0.793, SPF_NONE=0.001 autolearn=_AUTOLEARN X-Spam_action: reject X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, Lin Ma , pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Lin Ma --- block/block-backend.c | 38 ++++++++++++++++++++++++++++++++++ include/sysemu/block-backend.h | 2 ++ 2 files changed, 40 insertions(+) diff --git a/block/block-backend.c b/block/block-backend.c index 6936b25c83..6d08dd5e0d 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1650,6 +1650,44 @@ int blk_pdiscard(BlockBackend *blk, int64_t offset, int bytes) return blk_prw(blk, offset, NULL, bytes, blk_pdiscard_entry, 0); } +static int coroutine_fn +blk_do_get_lba_status(BlockBackend *blk, int64_t offset, int bytes, + uint32_t *num_blocks, uint32_t *is_deallocated) +{ + int ret; + + blk_wait_while_drained(blk); + + ret = blk_check_byte_request(blk, offset, bytes); + if (ret < 0) { + return ret; + } + + return bdrv_co_get_lba_status(blk->root, offset, bytes, num_blocks, + is_deallocated); +} + +static void blk_aio_get_lba_status_entry(void *opaque) +{ + BlkAioEmAIOCB *acb = opaque; + BlkRwCo *rwco = &acb->rwco; + + void *data = acb->common.opaque; + uint32_t *num_blocks = (uint32_t *)data; + uint32_t *is_deallocated = (uint32_t *)(data + sizeof(uint32_t)); + + rwco->ret = blk_do_get_lba_status(rwco->blk, rwco->offset, acb->bytes, + num_blocks, is_deallocated); + blk_aio_complete(acb); +} + +BlockAIOCB *blk_aio_get_lba_status(BlockBackend *blk, int64_t offset, int bytes, + BlockCompletionFunc *cb, void *opaque) +{ + return blk_aio_prwv(blk, offset, bytes, NULL, blk_aio_get_lba_status_entry, + 0, cb, opaque); +} + /* To be called between exactly one pair of blk_inc/dec_in_flight() */ static int coroutine_fn blk_do_flush(BlockBackend *blk) { diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h index 8203d7f6f9..cd527ec0c9 100644 --- a/include/sysemu/block-backend.h +++ b/include/sysemu/block-backend.h @@ -171,6 +171,8 @@ BlockAIOCB *blk_aio_flush(BlockBackend *blk, BlockCompletionFunc *cb, void *opaque); BlockAIOCB *blk_aio_pdiscard(BlockBackend *blk, int64_t offset, int bytes, BlockCompletionFunc *cb, void *opaque); +BlockAIOCB *blk_aio_get_lba_status(BlockBackend *blk, int64_t offset, int bytes, + BlockCompletionFunc *cb, void *opaque); void blk_aio_cancel(BlockAIOCB *acb); void blk_aio_cancel_async(BlockAIOCB *acb); int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf); From patchwork Wed Jun 17 10:20:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lin Ma X-Patchwork-Id: 11609591 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0FEFD13A0 for ; Wed, 17 Jun 2020 10:29:49 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E4422208B3 for ; Wed, 17 Jun 2020 10:29:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E4422208B3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:37080 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlVKG-0000bG-6m for patchwork-qemu-devel@patchwork.kernel.org; Wed, 17 Jun 2020 06:29:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54412) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jlVGq-0004Ge-D8; Wed, 17 Jun 2020 06:26:16 -0400 Received: from [114.255.249.163] (port=23486 helo=localhost) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlVGo-0007Og-Fm; Wed, 17 Jun 2020 06:26:16 -0400 Received: by localhost (Postfix, from userid 0) id 1F7BA1415FB; Wed, 17 Jun 2020 18:20:24 +0800 (CST) From: Lin Ma To: qemu-block@nongnu.org Subject: [PATCH v2 3/3] scsi-disk: Add support for the GET LBA STATUS 16 command Date: Wed, 17 Jun 2020 18:20:19 +0800 Message-Id: <20200617102019.29652-4-lma@suse.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: <20200617102019.29652-1-lma@suse.com> References: <20200617102019.29652-1-lma@suse.com> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 114.255.249.163 (failed) Received-SPF: none client-ip=114.255.249.163; envelope-from=root@localhost; helo=localhost X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/17 06:20:25 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: 68 X-Spam_score: 6.8 X-Spam_bar: ++++++ X-Spam_report: (6.8 / 5.0 requ) BAYES_00=-1.9, FSL_HELO_NON_FQDN_1=0.001, HEADER_FROM_DIFFERENT_DOMAINS=1, HELO_LOCALHOST=3.828, RCVD_IN_MSPIKE_BL=0.01, RCVD_IN_MSPIKE_ZBI=2.7, RCVD_IN_XBL=0.375, RDNS_NONE=0.793, SPF_NONE=0.001 autolearn=_AUTOLEARN X-Spam_action: reject X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, Lin Ma , pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Lin Ma --- hw/scsi/scsi-disk.c | 90 ++++++++++++++++++++++++++++++++++++++ include/block/accounting.h | 1 + include/scsi/constants.h | 1 + 3 files changed, 92 insertions(+) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 387503e11b..9e3002ddaf 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -1866,6 +1866,89 @@ static void scsi_disk_emulate_write_data(SCSIRequest *req) } } +typedef struct GetLbaStatusCBData { + uint32_t num_blocks; + uint32_t is_deallocated; + SCSIDiskReq *r; +} GetLbaStatusCBData; + +static void scsi_get_lba_status_complete(void *opaque, int ret); + +static void scsi_get_lba_status_complete_noio(GetLbaStatusCBData *data, int ret) +{ + SCSIDiskReq *r = data->r; + SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); + + assert(r->req.aiocb == NULL); + + block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, + s->qdev.blocksize, BLOCK_ACCT_GET_LBA_STATUS); + + r->req.aiocb = blk_aio_get_lba_status(s->qdev.conf.blk, + r->req.cmd.lba * s->qdev.blocksize, + s->qdev.blocksize, + scsi_get_lba_status_complete, data); +} + +static void scsi_get_lba_status_complete(void *opaque, int ret) +{ + GetLbaStatusCBData *data = opaque; + SCSIDiskReq *r = data->r; + SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); + + assert(r->req.aiocb != NULL); + r->req.aiocb = NULL; + + aio_context_acquire(blk_get_aio_context(s->qdev.conf.blk)); + if (scsi_disk_req_check_error(r, ret, true)) { + g_free(data); + goto done; + } + + block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); + scsi_req_unref(&r->req); + g_free(data); + +done: + aio_context_release(blk_get_aio_context(s->qdev.conf.blk)); +} + +static void scsi_disk_emulate_get_lba_status(SCSIRequest *req, uint8_t *outbuf) +{ + SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); + GetLbaStatusCBData *data; + uint32_t *num_blocks; + uint32_t *is_deallocated; + + data = g_new0(GetLbaStatusCBData, 1); + data->r = r; + num_blocks = &(data->num_blocks); + is_deallocated = &(data->is_deallocated); + + scsi_req_ref(&r->req); + scsi_get_lba_status_complete_noio(data, 0); + + /* + * 8 + 16 is the length in bytes of response header and + * one LBA status descriptor + */ + memset(outbuf, 0, 8 + 16); + outbuf[3] = 20; + outbuf[8] = (req->cmd.lba >> 56) & 0xff; + outbuf[9] = (req->cmd.lba >> 48) & 0xff; + outbuf[10] = (req->cmd.lba >> 40) & 0xff; + outbuf[11] = (req->cmd.lba >> 32) & 0xff; + outbuf[12] = (req->cmd.lba >> 24) & 0xff; + outbuf[13] = (req->cmd.lba >> 16) & 0xff; + outbuf[14] = (req->cmd.lba >> 8) & 0xff; + outbuf[15] = req->cmd.lba & 0xff; + outbuf[16] = (*num_blocks >> 24) & 0xff; + outbuf[17] = (*num_blocks >> 16) & 0xff; + outbuf[18] = (*num_blocks >> 8) & 0xff; + outbuf[19] = *num_blocks & 0xff; + outbuf[20] = *is_deallocated ? 1 : 0; +} + static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf) { SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); @@ -2076,6 +2159,13 @@ static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf) /* Protection, exponent and lowest lba field left blank. */ break; + } else if ((req->cmd.buf[1] & 31) == SAI_GET_LBA_STATUS) { + if (req->cmd.lba > s->qdev.max_lba) { + goto illegal_lba; + } + scsi_disk_emulate_get_lba_status(req, outbuf); + r->iov.iov_len = req->cmd.xfer; + return r->iov.iov_len; } trace_scsi_disk_emulate_command_SAI_unsupported(); goto illegal_request; diff --git a/include/block/accounting.h b/include/block/accounting.h index 878b4c3581..645014fb0b 100644 --- a/include/block/accounting.h +++ b/include/block/accounting.h @@ -38,6 +38,7 @@ enum BlockAcctType { BLOCK_ACCT_WRITE, BLOCK_ACCT_FLUSH, BLOCK_ACCT_UNMAP, + BLOCK_ACCT_GET_LBA_STATUS, BLOCK_MAX_IOTYPE, }; diff --git a/include/scsi/constants.h b/include/scsi/constants.h index 874176019e..b18377b214 100644 --- a/include/scsi/constants.h +++ b/include/scsi/constants.h @@ -154,6 +154,7 @@ * SERVICE ACTION IN subcodes */ #define SAI_READ_CAPACITY_16 0x10 +#define SAI_GET_LBA_STATUS 0x12 /* * READ POSITION service action codes