From patchwork Wed Jun 6 15:25:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Pen X-Patchwork-Id: 10450489 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 DDD0A60146 for ; Wed, 6 Jun 2018 15:26:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CC6EF29776 for ; Wed, 6 Jun 2018 15:26:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C0BC6297B4; Wed, 6 Jun 2018 15:26:21 +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,DKIM_SIGNED, DKIM_VALID, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham 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 7E93929776 for ; Wed, 6 Jun 2018 15:26:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932126AbeFFP0T (ORCPT ); Wed, 6 Jun 2018 11:26:19 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:34991 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752400AbeFFPZ5 (ORCPT ); Wed, 6 Jun 2018 11:25:57 -0400 Received: by mail-wr0-f196.google.com with SMTP id l10-v6so6762923wrn.2 for ; Wed, 06 Jun 2018 08:25:57 -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=ypoicCGne8XLfwg17ePXa0JeUCVriifgqnSyuhSGI/Y=; b=oCaLrgbBqCDNzK9g8o2q5uapAKyvYsAsw3thYRlrPNSoBhGtyN4/XeeFV7YJCBRCNe 5aO6W9ApY5sqmlQlKx2ZoEhQCN8G+qIhCvb+1Lj53nWwVCaXRwEXiXTWuSGZdmWLSL3O E3umlpvkJ8lFIsOvSuNfNJH/8fOyYgzugFYYMmBnWp5sUAmmVlMxjfxrVM+7FfOrfZjY pVwIIBoyOGSYg9Ko0WruiHF/mPc254zYm2p4P+CT4b+XQhtXsX59ueisXeJVCQt8okDi CP2Npad85ta4FmlCSTK0tP7MY3jn/hhP3cGHHPxocN31vCYpUCltA9xUeocuJe66A5hj 9UWg== 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=ypoicCGne8XLfwg17ePXa0JeUCVriifgqnSyuhSGI/Y=; b=LTr+ESkVfP10NSH0pdRKIJkV1e57FZo6f8gW69yL057uwYAaSvxwPw35JPbjGFyNIt johOoqHOwUrrHVONA7vAPCtRTOL4uN50ky6sq/xG4EHGAl8eNx0KTsoWov2BXT+fleOB pYUH+sgh6y02VnBrYoA4Z3tApxYRsftDU7yR4dZL0G2yAnZCTnW3HiYtUfZvhRD2ASaQ ZqETYIKZE27F7n7CQ7sTl+X8DzX6ZWfi7QzAqJ+6+M9OaCoKmR6Vrltf0LIm7V0lPIHt wNG9V2E5751pcCTWKS8tf+lskxv3cMvuIfv/de23TJ/ha+KvKT5SoE0pNC0PePxB89zZ CpmA== X-Gm-Message-State: APt69E1dNl+NM1hfmtas4F+k/Zpt3vw0qImWZCEmUI4/NxM1/OH2925R KDgTiu6bE+XZ1l6x7ocIBlqk3Vh+h3o= X-Google-Smtp-Source: ADUXVKIZ/uXSOMvmR6so6vmIn5Ta9AAUN3hGbLiS1CbAc8ucEZBMc69jqcnqlpwxWHcN5VBK5NRixw== X-Received: by 2002:adf:be81:: with SMTP id i1-v6mr2719703wrh.86.1528298756151; Wed, 06 Jun 2018 08:25:56 -0700 (PDT) Received: from pb.pb.local ([62.217.45.26]) by smtp.gmail.com with ESMTPSA id n11-v6sm18645834wro.13.2018.06.06.08.25.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Jun 2018 08:25:55 -0700 (PDT) From: Roman Pen To: linux-block@vger.kernel.org, linux-rdma@vger.kernel.org Cc: Jens Axboe , Christoph Hellwig , Sagi Grimberg , Bart Van Assche , Or Gerlitz , Doug Ledford , Danil Kipnis , Jack Wang , Roman Pen Subject: [PATCH v3 22/25] ibnbd: server: sysfs interface functions Date: Wed, 6 Jun 2018 17:25:12 +0200 Message-Id: <20180606152515.25807-23-roman.penyaev@profitbricks.com> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20180606152515.25807-1-roman.penyaev@profitbricks.com> References: <20180606152515.25807-1-roman.penyaev@profitbricks.com> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is the sysfs interface to IBNBD mapped devices on server side: /sys/devices/virtual/ibnbd-server/ctl/devices// |- block_dev | *** link pointing to the corresponding block device sysfs entry | |- sessions// | *** sessions directory | |- read_only | *** is devices mapped as read only | |- mapping_path *** relative device path provided by the client during mapping Signed-off-by: Roman Pen Signed-off-by: Danil Kipnis Cc: Jack Wang --- drivers/block/ibnbd/ibnbd-srv-sysfs.c | 242 ++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 drivers/block/ibnbd/ibnbd-srv-sysfs.c diff --git a/drivers/block/ibnbd/ibnbd-srv-sysfs.c b/drivers/block/ibnbd/ibnbd-srv-sysfs.c new file mode 100644 index 000000000000..5bf77cdb09c8 --- /dev/null +++ b/drivers/block/ibnbd/ibnbd-srv-sysfs.c @@ -0,0 +1,242 @@ +/* + * InfiniBand Network Block Driver + * + * Copyright (c) 2014 - 2017 ProfitBricks GmbH. All rights reserved. + * Authors: Fabian Holler + * Jack Wang + * Kleber Souza + * Danil Kipnis + * Roman Penyaev + * Milind Dumbare + * + * Copyright (c) 2017 - 2018 ProfitBricks GmbH. All rights reserved. + * Authors: Danil Kipnis + * Roman Penyaev + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#undef pr_fmt +#define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ibnbd-srv.h" + +static struct device *ibnbd_dev; +static struct class *ibnbd_dev_class; +static struct kobject *ibnbd_devs_kobj; + +static struct attribute *ibnbd_srv_default_dev_attrs[] = { + NULL, +}; + +static struct attribute_group ibnbd_srv_default_dev_attr_group = { + .attrs = ibnbd_srv_default_dev_attrs, +}; + +static struct kobj_type ktype = { + .sysfs_ops = &kobj_sysfs_ops, +}; + +int ibnbd_srv_create_dev_sysfs(struct ibnbd_srv_dev *dev, + struct block_device *bdev, + const char *dir_name) +{ + struct kobject *bdev_kobj; + int ret; + + ret = kobject_init_and_add(&dev->dev_kobj, &ktype, + ibnbd_devs_kobj, dir_name); + if (ret) + return ret; + + ret = kobject_init_and_add(&dev->dev_sessions_kobj, + &ktype, + &dev->dev_kobj, "sessions"); + if (ret) + goto err; + + ret = sysfs_create_group(&dev->dev_kobj, + &ibnbd_srv_default_dev_attr_group); + if (ret) + goto err2; + + bdev_kobj = &disk_to_dev(bdev->bd_disk)->kobj; + ret = sysfs_create_link(&dev->dev_kobj, bdev_kobj, "block_dev"); + if (ret) + goto err3; + + return 0; + +err3: + sysfs_remove_group(&dev->dev_kobj, + &ibnbd_srv_default_dev_attr_group); +err2: + kobject_del(&dev->dev_sessions_kobj); + kobject_put(&dev->dev_sessions_kobj); +err: + kobject_del(&dev->dev_kobj); + kobject_put(&dev->dev_kobj); + return ret; +} + +void ibnbd_srv_destroy_dev_sysfs(struct ibnbd_srv_dev *dev) +{ + sysfs_remove_link(&dev->dev_kobj, "block_dev"); + sysfs_remove_group(&dev->dev_kobj, &ibnbd_srv_default_dev_attr_group); + kobject_del(&dev->dev_sessions_kobj); + kobject_put(&dev->dev_sessions_kobj); + kobject_del(&dev->dev_kobj); + kobject_put(&dev->dev_kobj); +} + +static ssize_t ibnbd_srv_dev_session_ro_show(struct kobject *kobj, + struct kobj_attribute *attr, + char *page) +{ + struct ibnbd_srv_sess_dev *sess_dev; + + sess_dev = container_of(kobj, struct ibnbd_srv_sess_dev, kobj); + + return scnprintf(page, PAGE_SIZE, "%s\n", + (sess_dev->open_flags & FMODE_WRITE) ? "0" : "1"); +} + +static struct kobj_attribute ibnbd_srv_dev_session_ro_attr = + __ATTR(read_only, 0444, + ibnbd_srv_dev_session_ro_show, + NULL); + +static ssize_t +ibnbd_srv_dev_session_mapping_path_show(struct kobject *kobj, + struct kobj_attribute *attr, char *page) +{ + struct ibnbd_srv_sess_dev *sess_dev; + + sess_dev = container_of(kobj, struct ibnbd_srv_sess_dev, kobj); + + return scnprintf(page, PAGE_SIZE, "%s\n", sess_dev->pathname); +} + +static struct kobj_attribute ibnbd_srv_dev_session_mapping_path_attr = + __ATTR(mapping_path, 0444, + ibnbd_srv_dev_session_mapping_path_show, + NULL); + +static struct attribute *ibnbd_srv_default_dev_sessions_attrs[] = { + &ibnbd_srv_dev_session_ro_attr.attr, + &ibnbd_srv_dev_session_mapping_path_attr.attr, + NULL, +}; + +static struct attribute_group ibnbd_srv_default_dev_session_attr_group = { + .attrs = ibnbd_srv_default_dev_sessions_attrs, +}; + +void ibnbd_srv_destroy_dev_session_sysfs(struct ibnbd_srv_sess_dev *sess_dev) +{ + DECLARE_COMPLETION_ONSTACK(sysfs_compl); + + sysfs_remove_group(&sess_dev->kobj, + &ibnbd_srv_default_dev_session_attr_group); + + sess_dev->sysfs_release_compl = &sysfs_compl; + kobject_del(&sess_dev->kobj); + kobject_put(&sess_dev->kobj); + wait_for_completion(&sysfs_compl); +} + +static void ibnbd_srv_sess_dev_release(struct kobject *kobj) +{ + struct ibnbd_srv_sess_dev *sess_dev; + + sess_dev = container_of(kobj, struct ibnbd_srv_sess_dev, kobj); + if (sess_dev->sysfs_release_compl) + complete_all(sess_dev->sysfs_release_compl); +} + +static struct kobj_type ibnbd_srv_sess_dev_ktype = { + .sysfs_ops = &kobj_sysfs_ops, + .release = ibnbd_srv_sess_dev_release, +}; + +int ibnbd_srv_create_dev_session_sysfs(struct ibnbd_srv_sess_dev *sess_dev) +{ + int ret; + + ret = kobject_init_and_add(&sess_dev->kobj, &ibnbd_srv_sess_dev_ktype, + &sess_dev->dev->dev_sessions_kobj, "%s", + sess_dev->sess->sessname); + if (ret) + return ret; + + ret = sysfs_create_group(&sess_dev->kobj, + &ibnbd_srv_default_dev_session_attr_group); + if (ret) + goto err; + + return 0; + +err: + kobject_del(&sess_dev->kobj); + kobject_put(&sess_dev->kobj); + + return ret; +} + +int ibnbd_srv_create_sysfs_files(void) +{ + int err; + + ibnbd_dev_class = class_create(THIS_MODULE, "ibnbd-server"); + if (unlikely(IS_ERR(ibnbd_dev_class))) + return PTR_ERR(ibnbd_dev_class); + + ibnbd_dev = device_create(ibnbd_dev_class, NULL, + MKDEV(0, 0), NULL, "ctl"); + if (unlikely(IS_ERR(ibnbd_dev))) { + err = PTR_ERR(ibnbd_dev); + goto cls_destroy; + } + ibnbd_devs_kobj = kobject_create_and_add("devices", &ibnbd_dev->kobj); + if (unlikely(!ibnbd_devs_kobj)) { + err = -ENOMEM; + goto dev_destroy; + } + + return 0; + +dev_destroy: + device_destroy(ibnbd_dev_class, MKDEV(0, 0)); +cls_destroy: + class_destroy(ibnbd_dev_class); + + return err; +} + +void ibnbd_srv_destroy_sysfs_files(void) +{ + kobject_del(ibnbd_devs_kobj); + kobject_put(ibnbd_devs_kobj); + device_destroy(ibnbd_dev_class, MKDEV(0, 0)); + class_destroy(ibnbd_dev_class); +}