From patchwork Fri Mar 24 10:45:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinpu Wang X-Patchwork-Id: 9642609 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 E6C1260328 for ; Fri, 24 Mar 2017 10:48:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 08FB126E81 for ; Fri, 24 Mar 2017 10:48:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F167428236; Fri, 24 Mar 2017 10:48:00 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 3872426E81 for ; Fri, 24 Mar 2017 10:48:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935736AbdCXKr6 (ORCPT ); Fri, 24 Mar 2017 06:47:58 -0400 Received: from mail-wm0-f44.google.com ([74.125.82.44]:33311 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935684AbdCXKrr (ORCPT ); Fri, 24 Mar 2017 06:47:47 -0400 Received: by mail-wm0-f44.google.com with SMTP id v203so6737258wmg.0 for ; Fri, 24 Mar 2017 03:47:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=profitbricks-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=NoIZef1/jCRTsaf0p5NJFt2ex2B0+HyMyqtkX/PjU88=; b=UyTGTj5yqAH6LZx/6cRU0BXMrxWB20BBN8Cb59LYnju1cIbCg9Ge4MlDw3CUInSXcc z8JvSLyfOcLnX7nAtqpJou0o/exlf6dZc1CKmq+EejZthmm+kId+fbWZ9Gt5IteSg3vE V3cDQ5TE3F7Jps1dF7kYhGvM8xOk6w5n0lXMokZ+jJWSE9cQU7yeVNm745Cvje7Q5I4N 2VkMiUJzfLfyIU3+bfg7Li9M1i1VagRpt53ycZqGbOvmHnmfzQkU/zn9bHjsGyhOlO+S mlMBFMmwXkPYsAzVYdyIRze4bg4YyCbwmiQLtzBbI/ypKZXDFrYqHklYzVJTm+O9x5/X U+Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NoIZef1/jCRTsaf0p5NJFt2ex2B0+HyMyqtkX/PjU88=; b=adIKV4MDoZvZI1zPpXZRCOBU3J/aP3iWLoLpFq9ZRd5C+xEnlvEYTflmp7cu2psNnj GyoOrQMX0IWIjK+ir6fzaAp2srsuscpEXr0H+HIdW2Hub+43LMzmgU7xFdRqTAU6bnp/ L8Zwh3ltwvoH9LjJGZYp56aKcRerNqAcW51gHMMb1Vr98Gc6QA6Gw0VWO2zybidpioGt xVRhcCtbrkbBEt3m2LOqAVPHldg5XGZAckIeBbGu28RyK6dD5xfDfWnovxEIejhh7Tj1 Fc6mhNJHj63453rVa54mlF7V0eJAfbw3FdwQKy4xSTpij5KzrygPNl2R/4yRai17rQOj B2rA== X-Gm-Message-State: AFeK/H3PVnyTXTnHPTEi65nQzJJY8XXeY+WjmnSz94AkkDHfU5ho/FvBCEydS/SxAo1OiZDD X-Received: by 10.28.175.11 with SMTP id y11mr2190916wme.2.1490352465015; Fri, 24 Mar 2017 03:47:45 -0700 (PDT) Received: from jinpu-GA-870A-USB3.pb.local ([62.217.45.26]) by smtp.gmail.com with ESMTPSA id s17sm2404458wrc.25.2017.03.24.03.47.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 24 Mar 2017 03:47:44 -0700 (PDT) From: Jack Wang X-Google-Original-From: Jack Wang To: linux-block@vger.kernel.org, linux-rdma@vger.kernel.org Cc: dledford@redhat.com, axboe@kernel.dk, hch@lst.de, mail@fholler.de, Milind.dumbare@gmail.com, yun.wang@profitbricks.com, Jack Wang , Kleber Souza , Danil Kipnis , Roman Pen Subject: [PATCH 15/28] ibnbd: add shared library functions Date: Fri, 24 Mar 2017 11:45:30 +0100 Message-Id: <1490352343-20075-16-git-send-email-jinpu.wangl@profitbricks.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490352343-20075-1-git-send-email-jinpu.wangl@profitbricks.com> References: <1490352343-20075-1-git-send-email-jinpu.wangl@profitbricks.com> Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jack Wang Signed-off-by: Jack Wang Signed-off-by: Kleber Souza Signed-off-by: Danil Kipnis Signed-off-by: Roman Pen --- drivers/block/ibnbd_lib/ibnbd-proto.c | 244 ++++++++++++++++++++++++++++++++++ drivers/block/ibnbd_lib/ibnbd.c | 108 +++++++++++++++ 2 files changed, 352 insertions(+) create mode 100644 drivers/block/ibnbd_lib/ibnbd-proto.c create mode 100644 drivers/block/ibnbd_lib/ibnbd.c diff --git a/drivers/block/ibnbd_lib/ibnbd-proto.c b/drivers/block/ibnbd_lib/ibnbd-proto.c new file mode 100644 index 0000000..c6d83f2 --- /dev/null +++ b/drivers/block/ibnbd_lib/ibnbd-proto.c @@ -0,0 +1,244 @@ +/* + * InfiniBand Network Block Driver + * + * Copyright (c) 2014 - 2017 ProfitBricks GmbH. All rights reserved. + * Authors: Fabian Holler < mail@fholler.de> + * Jack Wang + * Kleber Souza + * Danil Kipnis + * Roman Pen + * Milind Dumbare + * + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * substantially similar to the "NO WARRANTY" disclaimer below + * ("Disclaimer") and any redistribution must be conditioned upon + * including a substantially similar Disclaimer requirement for further + * binary redistribution. + * 3. Neither the names of the above-listed copyright holders nor the names + * of any contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License ("GPL") version 2 as published by the Free + * Software Foundation. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + */ + +#include "../ibnbd_inc/ibnbd-proto.h" +#include "../ibnbd_inc/log.h" + +static int ibnbd_validate_msg_sess_info(const struct ibnbd_msg_sess_info *msg, + size_t len) +{ + if (unlikely(len != sizeof(*msg))) { + ERR_NP("Sess info message with unexpected length received" + " %lu instead of %lu\n", len, sizeof(*msg)); + return -EINVAL; + } + + return 0; +} + +static int +ibnbd_validate_msg_sess_info_rsp(const struct ibnbd_msg_sess_info_rsp *msg, + size_t len) +{ + if (unlikely(len != sizeof(*msg))) { + ERR_NP("Sess info message with unexpected length received" + " %lu instead of %lu\n", len, sizeof(*msg)); + return -EINVAL; + } + + return 0; +} + +static int ibnbd_validate_msg_open_resp(const struct ibnbd_msg_open_rsp *msg, + size_t len) +{ + if (unlikely(msg->result)) + return 0; + + if (unlikely(len != sizeof(*msg))) { + ERR_NP("Open Response msg received with unexpected length" + " %zuB instead of %luB\n", len, sizeof(*msg)); + return -EINVAL; + } + + if (unlikely(!msg->logical_block_size)) { + ERR_NP("Open Resp msg received with unexpected with" + " invalid logical_block_size value %d\n", + msg->logical_block_size); + return -EINVAL; + } + + if (unlikely(!msg->physical_block_size)) { + ERR_NP("Open Resp msg received with invalid" + " physical_block_size value %d\n", + msg->physical_block_size); + return -EINVAL; + } + + if (unlikely(!msg->max_hw_sectors)) { + ERR_NP("Open Resp msg received with invalid" + " max_hw_sectors value %d\n", msg->max_hw_sectors); + return -EINVAL; + } + + return 0; +} + +static int ibnbd_validate_msg_revalidate(const struct ibnbd_msg_revalidate *msg, + size_t len) +{ + if (unlikely(len != sizeof(*msg))) { + ERR_NP("Device resize message with unexpected length received" + " %lu instead of %lu\n", len, sizeof(*msg)); + return -EINVAL; + } + + return 0; +} + +static int ibnbd_validate_msg_open(const struct ibnbd_msg_open *msg, + size_t len) +{ + if (len != sizeof(*msg)) { + ERR_NP("Open msg received with unexpected length" + " %zuB instead of %luB\n", len, sizeof(*msg)); + return -EINVAL; + } + if (msg->dev_name[strnlen(msg->dev_name, NAME_MAX)] != '\0') { + ERR_NP("Open msg received with invalid dev_name value," + " null terminator missing\n"); + return -EINVAL; + } + + if (unlikely(msg->access_mode != IBNBD_ACCESS_RO && + msg->access_mode != IBNBD_ACCESS_RW && + msg->access_mode != IBNBD_ACCESS_MIGRATION)) { + ERR_NP("Open msg received with invalid access_mode value %d\n", + msg->access_mode); + return -EINVAL; + } + + return 0; +} + +static int ibnbd_validate_msg_close(const struct ibnbd_msg_close *msg, size_t + len) +{ + if (unlikely(len != sizeof(*msg))) { + ERR_NP("Close msg received with unexpected length %lu instead" + " of %lu\n", len, sizeof(*msg)); + return -EINVAL; + } + + return 0; +} + +static int ibnbd_validate_msg_close_rsp(const struct ibnbd_msg_close_rsp *msg, + size_t len) +{ + if (unlikely(len != sizeof(*msg))) { + ERR_NP("Close_rsp msg received with unexpected length %lu" + " instead of %lu\n", len, sizeof(*msg)); + return -EINVAL; + } + + return 0; +} + +int ibnbd_validate_message(const void *data, size_t len) +{ + const struct ibnbd_msg_hdr *hdr = data; + + switch (hdr->type) { + case IBNBD_MSG_SESS_INFO: { + const struct ibnbd_msg_sess_info *msg = data; + + return ibnbd_validate_msg_sess_info(msg, len); + } + case IBNBD_MSG_SESS_INFO_RSP: { + const struct ibnbd_msg_sess_info_rsp *msg = data; + + return ibnbd_validate_msg_sess_info_rsp(msg, len); + } + case IBNBD_MSG_OPEN_RSP: { + const struct ibnbd_msg_open_rsp *msg = data; + + return ibnbd_validate_msg_open_resp(msg, len); + } + case IBNBD_MSG_REVAL: { + const struct ibnbd_msg_revalidate *msg = data; + + return ibnbd_validate_msg_revalidate(msg, len); + } + case IBNBD_MSG_OPEN: { + const struct ibnbd_msg_open *msg = data; + + return ibnbd_validate_msg_open(msg, len); + } + case IBNBD_MSG_CLOSE: { + const struct ibnbd_msg_close *msg = data; + + return ibnbd_validate_msg_close(msg, len); + } + case IBNBD_MSG_CLOSE_RSP: { + const struct ibnbd_msg_close_rsp *msg = data; + + return ibnbd_validate_msg_close_rsp(msg, len); + } + default: + ERR_NP("Ignoring received message with unknown type %d\n", + hdr->type); + return -EINVAL; + } +} + +const char *ibnbd_io_mode_str(enum ibnbd_io_mode mode) +{ + switch (mode) { + case IBNBD_FILEIO: + return "fileio"; + case IBNBD_BLOCKIO: + return "blockio"; + case IBNBD_AUTOIO: + return "autoio"; + default: + return "unknown"; + } +} + +const char *ibnbd_access_mode_str(enum ibnbd_access_mode mode) +{ + switch (mode) { + case IBNBD_ACCESS_RO: + return "ro"; + case IBNBD_ACCESS_RW: + return "rw"; + case IBNBD_ACCESS_MIGRATION: + return "migration"; + default: + return "unknown"; + } +} diff --git a/drivers/block/ibnbd_lib/ibnbd.c b/drivers/block/ibnbd_lib/ibnbd.c new file mode 100644 index 0000000..1ba4777 --- /dev/null +++ b/drivers/block/ibnbd_lib/ibnbd.c @@ -0,0 +1,108 @@ +/* + * InfiniBand Network Block Driver + * + * Copyright (c) 2014 - 2017 ProfitBricks GmbH. All rights reserved. + * Authors: Fabian Holler < mail@fholler.de> + * Jack Wang + * Kleber Souza + * Danil Kipnis + * Roman Pen + * Milind Dumbare + * + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * substantially similar to the "NO WARRANTY" disclaimer below + * ("Disclaimer") and any redistribution must be conditioned upon + * including a substantially similar Disclaimer requirement for further + * binary redistribution. + * 3. Neither the names of the above-listed copyright holders nor the names + * of any contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License ("GPL") version 2 as published by the Free + * Software Foundation. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + */ + +#include +#include "../ibnbd_inc/ibnbd.h" +#include "../ibnbd_inc/ibnbd-proto.h" + +u32 ibnbd_io_flags_to_bi_rw(u32 flags) +{ + u32 result = 0; + + if (flags == 0) + return result; + + if (flags & IBNBD_RW_REQ_WRITE) + result |= WRITE; + + if (flags & IBNBD_RW_REQ_SYNC) + result |= REQ_SYNC; + + if (flags & IBNBD_RW_REQ_DISCARD) + result |= REQ_OP_DISCARD; + + if (flags & IBNBD_RW_REQ_SECURE) + result |= REQ_OP_SECURE_ERASE; + + if (flags & IBNBD_RW_REQ_WRITE_SAME) + result |= REQ_OP_WRITE_SAME; + + if (flags & IBNBD_RW_REQ_FUA) + result |= REQ_FUA; + + if (flags & IBNBD_RW_REQ_FLUSH) + result |= REQ_OP_FLUSH | REQ_PREFLUSH; + + return result; +} + +u32 rq_cmd_to_ibnbd_io_flags(struct request *rq) +{ + u32 result = 0; + + if (req_op(rq) == REQ_OP_WRITE) + result |= IBNBD_RW_REQ_WRITE; + + if (rq_is_sync(rq)) + result |= IBNBD_RW_REQ_SYNC; + + if (req_op(rq) == REQ_OP_DISCARD) + result |= IBNBD_RW_REQ_DISCARD; + + if (req_op(rq) == REQ_OP_SECURE_ERASE) + result |= IBNBD_RW_REQ_SECURE; + + if (req_op(rq) == REQ_OP_WRITE_SAME) + result |= IBNBD_RW_REQ_WRITE_SAME; + + if (rq->cmd_flags & REQ_FUA) + result |= IBNBD_RW_REQ_FUA; + + if (req_op(rq) == REQ_OP_FLUSH) + result |= IBNBD_RW_REQ_FLUSH; + + return result; +}