From patchwork Wed Mar 11 16:12:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinpu Wang X-Patchwork-Id: 11432159 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 DA7951392 for ; Wed, 11 Mar 2020 16:13:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A2893206E9 for ; Wed, 11 Mar 2020 16:13:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=cloud.ionos.com header.i=@cloud.ionos.com header.b="dSFOfoFZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730249AbgCKQNV (ORCPT ); Wed, 11 Mar 2020 12:13:21 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:37187 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730245AbgCKQNV (ORCPT ); Wed, 11 Mar 2020 12:13:21 -0400 Received: by mail-wr1-f67.google.com with SMTP id 6so3394427wre.4 for ; Wed, 11 Mar 2020 09:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.ionos.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Febvpk8H0CfldfHZhzVT2mSC65UCbc+4UoAb79NFUIs=; b=dSFOfoFZNn+WQn4pA2tJfrkq4HLr7q5fqwZsdUfC+3PMdhDGgDogS+2Xa7lGMUqiUq ZcYOp9RC06OCaKUgVNv9/o5OmzEmpOC11jsXFoe9m124ynqfw3POgOqra8CCMxIv+/77 wi28ArIDpNPnDm0Vtc/7ogONt+uKVNGJs60JpGEWNefxaNKYm3SMpiamoLiHFbfLvpXF z58rvmYlbirFBgUAWK68gLwlSlNRkjmeWzd3o1ZaJnoPCcwAxEIVdqJNmMcvV9Pyekre 1F4K6t9PdQEMNUoo5Vm37dOOvDafpayO/kNPC2h4ku4x9wEuwQGWR/kmqun4+wAr3rDL FWGA== 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=Febvpk8H0CfldfHZhzVT2mSC65UCbc+4UoAb79NFUIs=; b=CGepBBAJPKNJ8wI9qoxaASucsmDEM9bmrxCbBF6+rvEUTVGk/ReZiUZzfM+gTXMCw2 cjF2mwFSs+jWUVEIun9D5biulallD0OO+vQJHWaUD2sqYaKw5F4UJU1oUS3PUuy2eLR6 jUQb38lX0O+yryJ+OLDNeOje8Jx/qGm0X8HSTSYfczDvUVcVOBkcpwT9VlCwSESvaSKs ApeNkY9UbA3M4q/iphDCI97dHMsPf+ZKMH3CIrhUtMZjdUynhEuQxF0jmJbtd+1ibiQp +2GRp1w4b+qHFrhzItu7atqtxMy+4H1nxJDNJUYPvr6CNO0RrxzArZOE0yL9kzc/9tBw P51g== X-Gm-Message-State: ANhLgQ140k0vXTB6/cW8vaHf5FQx02yuIWz/ch0Ayt8TD4e+qYf2IoHZ QSjYB/uN8G28fxEEOnvYtFWdVTvtqPM= X-Google-Smtp-Source: ADFU+vvoQZ5ibK/2Pixba4ulnTfwxz8PM7INCv5dy4VYTAn8aCJS4+TFe2DmZWNxlWfBEUHjHn39kw== X-Received: by 2002:adf:eacf:: with SMTP id o15mr5155582wrn.319.1583943196481; Wed, 11 Mar 2020 09:13:16 -0700 (PDT) Received: from jwang-Latitude-5491.fritz.box ([2001:16b8:4963:f600:4938:8f65:9543:5ec9]) by smtp.gmail.com with ESMTPSA id v13sm2739332wru.47.2020.03.11.09.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2020 09:13:15 -0700 (PDT) From: Jack Wang To: linux-block@vger.kernel.org, linux-rdma@vger.kernel.org Cc: axboe@kernel.dk, hch@infradead.org, sagi@grimberg.me, bvanassche@acm.org, leon@kernel.org, dledford@redhat.com, jgg@ziepe.ca, danil.kipnis@cloud.ionos.com, jinpu.wang@cloud.ionos.com, rpenyaev@suse.de, pankaj.gupta@cloud.ionos.com Subject: [PATCH v10 22/26] block/rnbd: server: functionality for IO submission to file or block dev Date: Wed, 11 Mar 2020 17:12:36 +0100 Message-Id: <20200311161240.30190-23-jinpu.wang@cloud.ionos.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200311161240.30190-1-jinpu.wang@cloud.ionos.com> References: <20200311161240.30190-1-jinpu.wang@cloud.ionos.com> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This provides helper functions for IO submission to file or block dev. Signed-off-by: Danil Kipnis Signed-off-by: Jack Wang --- drivers/block/rnbd/rnbd-srv-dev.c | 94 +++++++++++++++++++++++++++++++ drivers/block/rnbd/rnbd-srv-dev.h | 88 +++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 drivers/block/rnbd/rnbd-srv-dev.c create mode 100644 drivers/block/rnbd/rnbd-srv-dev.h diff --git a/drivers/block/rnbd/rnbd-srv-dev.c b/drivers/block/rnbd/rnbd-srv-dev.c new file mode 100644 index 000000000000..370f4a43e10c --- /dev/null +++ b/drivers/block/rnbd/rnbd-srv-dev.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * RDMA Network Block Driver + * + * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved. + * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved. + * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved. + */ +#undef pr_fmt +#define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt + +#include "rnbd-srv-dev.h" +#include "rnbd-log.h" + +struct rnbd_dev *rnbd_dev_open(const char *path, fmode_t flags, + void (*io_cb)(void *priv, int error)) +{ + struct rnbd_dev *dev; + int ret; + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) + return ERR_PTR(-ENOMEM); + + dev->blk_open_flags = flags; + dev->bdev = blkdev_get_by_path(path, flags, THIS_MODULE); + ret = PTR_ERR_OR_ZERO(dev->bdev); + if (ret) + goto err; + + dev->blk_open_flags = flags; + dev->io_cb = io_cb; + bdevname(dev->bdev, dev->name); + + return dev; + +err: + kfree(dev); + return ERR_PTR(ret); +} + +void rnbd_dev_close(struct rnbd_dev *dev) +{ + blkdev_put(dev->bdev, dev->blk_open_flags); + kfree(dev); +} + +static void rnbd_dev_bi_end_io(struct bio *bio) +{ + struct rnbd_dev_blk_io *io = bio->bi_private; + + io->dev->io_cb(io->priv, blk_status_to_errno(bio->bi_status)); + bio_put(bio); + kfree(io); +} + +int rnbd_dev_submit_io(struct rnbd_dev *dev, sector_t sector, void *data, + size_t len, u32 bi_size, enum rnbd_io_flags flags, + short prio, void *priv) +{ + struct request_queue *q = bdev_get_queue(dev->bdev); + struct rnbd_dev_blk_io *io; + struct bio *bio; + + /* check if the buffer is suitable for bdev */ + if (WARN_ON(!blk_rq_aligned(q, (unsigned long)data, len))) + return -EINVAL; + + /* Generate bio with pages pointing to the rdma buffer */ + bio = bio_map_kern(q, data, len, GFP_KERNEL); + if (IS_ERR(bio)) + return PTR_ERR(bio); + + io = kmalloc(sizeof(*io), GFP_KERNEL); + if (unlikely(!io)) { + bio_put(bio); + return -ENOMEM; + } + + io->dev = dev; + io->priv = priv; + + bio->bi_end_io = rnbd_dev_bi_end_io; + bio->bi_private = io; + bio->bi_opf = rnbd_to_bio_flags(flags); + bio->bi_iter.bi_sector = sector; + bio->bi_iter.bi_size = bi_size; + bio_set_prio(bio, prio); + bio_set_dev(bio, dev->bdev); + + submit_bio(bio); + + return 0; +} diff --git a/drivers/block/rnbd/rnbd-srv-dev.h b/drivers/block/rnbd/rnbd-srv-dev.h new file mode 100644 index 000000000000..6d07b9323f84 --- /dev/null +++ b/drivers/block/rnbd/rnbd-srv-dev.h @@ -0,0 +1,88 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * RDMA Network Block Driver + * + * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved. + * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved. + * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved. + */ +#ifndef RNBD_SRV_DEV_H +#define RNBD_SRV_DEV_H + +#include +#include "rnbd-proto.h" + +struct rnbd_dev { + struct block_device *bdev; + fmode_t blk_open_flags; + char name[BDEVNAME_SIZE]; + void (*io_cb)(void *priv, int error); +}; + +struct rnbd_dev_blk_io { + struct rnbd_dev *dev; + void *priv; +}; + +/** + * rnbd_dev_open() - Open a device + * @flags: open flags + * @io_cb: is called when I/O finished + */ +struct rnbd_dev *rnbd_dev_open(const char *path, fmode_t flags, + void (*io_cb)(void *priv, int error)); + +/** + * rnbd_dev_close() - Close a device + */ +void rnbd_dev_close(struct rnbd_dev *dev); + +static inline int rnbd_dev_get_max_segs(const struct rnbd_dev *dev) +{ + return queue_max_segments(bdev_get_queue(dev->bdev)); +} + +static inline int rnbd_dev_get_max_hw_sects(const struct rnbd_dev *dev) +{ + return queue_max_hw_sectors(bdev_get_queue(dev->bdev)); +} + +static inline int rnbd_dev_get_secure_discard(const struct rnbd_dev *dev) +{ + return blk_queue_secure_erase(bdev_get_queue(dev->bdev)); +} + +static inline int rnbd_dev_get_max_discard_sects(const struct rnbd_dev *dev) +{ + if (!blk_queue_discard(bdev_get_queue(dev->bdev))) + return 0; + + return blk_queue_get_max_sectors(bdev_get_queue(dev->bdev), + REQ_OP_DISCARD); +} + +static inline int rnbd_dev_get_discard_granularity(const struct rnbd_dev *dev) +{ + return bdev_get_queue(dev->bdev)->limits.discard_granularity; +} + +static inline int rnbd_dev_get_discard_alignment(const struct rnbd_dev *dev) +{ + return bdev_get_queue(dev->bdev)->limits.discard_alignment; +} + +/** + * rnbd_dev_submit_io() - Submit an I/O to the disk + * @dev: device to that the I/O is submitted + * @sector: address to read/write data to + * @data: I/O data to write or buffer to read I/O date into + * @len: length of @data + * @bi_size: Amount of data that will be read/written + * @prio: IO priority + * @priv: private data passed to @io_fn + */ +int rnbd_dev_submit_io(struct rnbd_dev *dev, sector_t sector, void *data, + size_t len, u32 bi_size, enum rnbd_io_flags flags, + short prio, void *priv); + +#endif /* RNBD_SRV_DEV_H */