From patchwork Fri Jun 15 18:23:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhu Lingshan X-Patchwork-Id: 10467313 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1172560384 for ; Fri, 15 Jun 2018 18:25:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0286728E3B for ; Fri, 15 Jun 2018 18:25:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB5BF28E41; Fri, 15 Jun 2018 18:25:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E27328E3B for ; Fri, 15 Jun 2018 18:25:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756524AbeFOSZX (ORCPT ); Fri, 15 Jun 2018 14:25:23 -0400 Received: from smtp2.provo.novell.com ([137.65.250.81]:53094 "EHLO smtp2.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756508AbeFOSZT (ORCPT ); Fri, 15 Jun 2018 14:25:19 -0400 Received: from vstart.localdomain (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Fri, 15 Jun 2018 12:25:13 -0600 From: Zhu Lingshan To: target-devel@vger.kernel.org, linux-scsi@vger.kernel.org, nab@linux-iscsi.org, mchristi@redhat.com, martin.petersen@oracle.com Cc: hare@suse.de, lduncan@suse.com, ddiss@suse.de, lszhu@suse.de, Zhu Lingshan Subject: [PATCH 24/33] TCMU PR: handle PR read keys for TCMU devs Date: Sat, 16 Jun 2018 02:23:33 +0800 Message-Id: <20180615182342.6239-24-lszhu@suse.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180615182342.6239-1-lszhu@suse.com> References: <20180615182342.6239-1-lszhu@suse.com> Sender: target-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: target-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch added a function tcmu_execute_pr_read_keys() which can help handle Persistent Reservation Read Keys. Signed-off-by: Zhu Lingshan --- drivers/target/target_core_user.c | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/drivers/target/target_core_user.c b/drivers/target/target_core_user.c index 2fcdfc97ca75..a89c866a755d 100644 --- a/drivers/target/target_core_user.c +++ b/drivers/target/target_core_user.c @@ -2846,6 +2846,80 @@ static int tcmu_pr_info_mock_empty(struct tcmu_pr_info **_pr_info) return 0; } +static sense_reason_t +tcmu_execute_pr_read_keys(struct se_cmd *cmd, unsigned char *buf, u32 buf_len) +{ + struct se_device *dev = cmd->se_dev; + struct tcmu_pr_info *pr_info = NULL; + struct tcmu_pr_reg *reg; + u32 add_len = 0, off = 8; + int rc; + struct tcmu_dev *udev = TCMU_DEV(dev); + + udev->pr_info.pr_info_buf = kzalloc(TCMU_PR_INFO_XATTR_MAX_SIZE, + GFP_KERNEL); + if (!udev->pr_info.pr_info_buf) + return TCM_OUT_OF_RESOURCES; + + if (buf_len < 8) { + WARN_ON(1); + kfree(udev->pr_info.pr_info_buf); + return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; + } + + pr_debug("getting pr_info for buf: %p, %u\n", buf, buf_len); + + rc = tcmu_pr_info_get(udev, &pr_info, NULL, NULL); + if (rc == -ENODATA) { + pr_debug("PR info not present for read, mocking empty\n"); + rc = tcmu_pr_info_mock_empty(&pr_info); + } + if (rc < 0) { + kfree(udev->pr_info.pr_info_buf); + return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; + } + pr_debug("packing read_keys response buf: %p, %u\n", buf, buf_len); + + buf[0] = ((pr_info->gen >> 24) & 0xff); + buf[1] = ((pr_info->gen >> 16) & 0xff); + buf[2] = ((pr_info->gen >> 8) & 0xff); + buf[3] = (pr_info->gen & 0xff); + + pr_debug("packed gen %u in read_keys response\n", pr_info->gen); + + list_for_each_entry(reg, &pr_info->regs, regs_node) { + /* + * Check for overflow of 8byte PRI READ_KEYS payload and + * next reservation key list descriptor. + */ + if ((add_len + 8) > (buf_len - 8)) + break; + + buf[off++] = ((reg->key >> 56) & 0xff); + buf[off++] = ((reg->key >> 48) & 0xff); + buf[off++] = ((reg->key >> 40) & 0xff); + buf[off++] = ((reg->key >> 32) & 0xff); + buf[off++] = ((reg->key >> 24) & 0xff); + buf[off++] = ((reg->key >> 16) & 0xff); + buf[off++] = ((reg->key >> 8) & 0xff); + buf[off++] = (reg->key & 0xff); + pr_debug("packed key 0x%llx in read_keys response\n", reg->key); + + add_len += 8; + } + + buf[4] = ((add_len >> 24) & 0xff); + buf[5] = ((add_len >> 16) & 0xff); + buf[6] = ((add_len >> 8) & 0xff); + buf[7] = (add_len & 0xff); + + pr_debug("packed len %u in read_keys response\n", add_len); + tcmu_pr_info_free(pr_info); + kfree(udev->pr_info.pr_info_buf); + + return TCM_NO_SENSE; +} + static int tcmu_configure_device(struct se_device *dev) { struct tcmu_dev *udev = TCMU_DEV(dev);