Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp153378pxj; Mon, 17 May 2021 23:42:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOPmLGk5cKOg7VskhAxFD/pvTENvEHZ8oaOljp4cePN3bIJr3ddCA+5w9yIMxy+blDvqjB X-Received: by 2002:a05:6e02:13e1:: with SMTP id w1mr3194417ilj.181.1621320166858; Mon, 17 May 2021 23:42:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621320166; cv=none; d=google.com; s=arc-20160816; b=TtqLzUyHDMMfFH+Bp8063cvf1JUfxRcBZkxJUo/ZiolatPB5IF9ROvyjJAeK4eHQ8X 62E0F+JgZacC/DD0Vv1TCwQFMAgKvm0KMzIJaBaMIWmbW1ZD99h5WRdOplPvsbreyu/T pa0H7gwQBmt2bNV0gsVNjsSnEFNqX2X8v3pckx4WVYzfTUm5vp1PrTHD8738YSQhfy43 aptPqlZdoi+r89Pzze9wQVovVQrfEIMJM8usop3ejLcfl57UWrb4fTDFHFjLSaqgxhkd 4iugKUb2ad6N37C7sZpqfMultRbDqOa4rJ+1mq3aXg/06hTXa0l89VCKNaHq/fIBmzqc 61LA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=W+CnPxJauSUM82R6oN864DZXrMAXTzC0WaLq8goFE8I=; b=F9p0UppPKC85ePOHza7ZgjMQGm4DIU6WJeiWKa1XG2MTm2l6l3Sw/uL5pvV6LbmohQ te/2kSDiPse4ABqo4e3x2GdHPzDt4cONXaDoDF6NI1jEEv72CNZU5IPDw0D7PJg9rEJD 1TGKZ2e92XiIR/OCXEh2IHCDFPnSiisy+UR6x1OGOlOEf331GrXEoOp4DghvzI6uNJWL NB+0zBRve7JkWrspHkXlMDdyBG6KKCeiHTdiZq36qFnb9JNJO6uumUi+odpe7HVTU2G7 HYti11UUIXGqEQ9PrG5GGe47XIQfNG2yLbF44pYCh97puqa4rnQS8d4Iv5Y+2i0J7PQs zo2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ePAPu6xX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o12si21599399ilu.42.2021.05.17.23.42.34; Mon, 17 May 2021 23:42:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ePAPu6xX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241632AbhEQOwG (ORCPT + 99 others); Mon, 17 May 2021 10:52:06 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:37263 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240618AbhEQOoS (ORCPT ); Mon, 17 May 2021 10:44:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621262575; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=W+CnPxJauSUM82R6oN864DZXrMAXTzC0WaLq8goFE8I=; b=ePAPu6xX0XRRkQfeDmjqoPezh3kH9bREt1GM4GZ8GlgIDxIfBrXuqK1tXnApSBfCsE0fBY yHEiGjFuADKnUY1tl4lBkiN4VbYaESMHSV23ij6GHVEASkmJ2XdIp1mATvz4YrTV7DHffY TSSFudGHGSjzJKO2R/ZuuF0Vx0DoZlA= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-580-qQROYA91PcWMNEdKUq7SQA-1; Mon, 17 May 2021 10:42:54 -0400 X-MC-Unique: qQROYA91PcWMNEdKUq7SQA-1 Received: by mail-wm1-f71.google.com with SMTP id 12-20020a1c010c0000b0290176491efde9so825604wmb.4 for ; Mon, 17 May 2021 07:42:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=W+CnPxJauSUM82R6oN864DZXrMAXTzC0WaLq8goFE8I=; b=DIgagki8+qQAHkLtzkniDweAWurKmskREUO49OMpBriwsAvTNEr9gu4d16+Lgp/tOt zO7wCpgvXBqXrI72WKH7JyCI02XgOTkoUEgCX2xVPWcvjAbvZq1bjnnjIKnNTkBHxhSI 3Pj1h8CX6EdPN9xE5qJLu75RAfNa7yY+aAlTcpAiUIDmYEaqS1WNFwUy1MNihCqSXQwX irg4nw8ronA8p7Xi9L5AdLiOwRpzttluZxdVqB4SOzMtnNhwMf0S8V/6iAx5J0QIkkZ4 d6ssle36teCAwxB/7iK45HSq+0H0DjDTIVMzCNZkCHj0p9O21dREw2ofrk3kt9iS44z1 ePeA== X-Gm-Message-State: AOAM531kRuPmJ1AJ9FUvPwJfZDzEZacthNmWHL2HcajPR6g1Ogfg9/3v YKaeag+a9bX7DdtnfVFPJXqsCN9wCQo+bcskXW8QCkgPupHn26Oiu4uMGBORXdJQYNSEgV5p4dd Nea0js3+o+gTqU4pzOq9G4D1otiDqYrYC+/A0z/wP X-Received: by 2002:a7b:c012:: with SMTP id c18mr310790wmb.94.1621262572624; Mon, 17 May 2021 07:42:52 -0700 (PDT) X-Received: by 2002:a7b:c012:: with SMTP id c18mr310766wmb.94.1621262572257; Mon, 17 May 2021 07:42:52 -0700 (PDT) MIME-Version: 1.0 References: <20210429102828.31248-1-prasanna.kalever@redhat.com> In-Reply-To: From: Prasanna Kalever Date: Mon, 17 May 2021 20:12:41 +0530 Message-ID: Subject: Re: [PATCH] nbd: provide a way for userspace processes to identify device backends To: josef@toxicpanda.com, axboe@kernel.dk, Ming Lei Cc: Prasanna Kumar Kalever , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, nbd@other.debian.org, Ilya Dryomov , Xiubo Li , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 30, 2021 at 7:44 AM Xiubo Li wrote: > > On 2021/4/29 18:28, Prasanna Kumar Kalever wrote: > > Problem: > > On reconfigure of device, there is no way to defend if the backend > > storage is matching with the initial backend storage. > > > > Say, if an initial connect request for backend "pool1/image1" got > > mapped to /dev/nbd0 and the userspace process is terminated. A next > > reconfigure request within NBD_ATTR_DEAD_CONN_TIMEOUT is allowed to > > use /dev/nbd0 for a different backend "pool1/image2" > > > > For example, an operation like below could be dangerous: > > > > $ sudo rbd-nbd map --try-netlink rbd-pool/ext4-image > > /dev/nbd0 > > $ sudo blkid /dev/nbd0 > > /dev/nbd0: UUID="bfc444b4-64b1-418f-8b36-6e0d170cfc04" TYPE="ext4" > > $ sudo pkill -9 rbd-nbd > > $ sudo rbd-nbd attach --try-netlink --device /dev/nbd0 rbd-pool/xfs-image > > /dev/nbd0 > > $ sudo blkid /dev/nbd0 > > /dev/nbd0: UUID="d29bf343-6570-4069-a9ea-2fa156ced908" TYPE="xfs" > > > > Solution: > > Provide a way for userspace processes to keep some metadata to identify > > between the device and the backend, so that when a reconfigure request is > > made, we can compare and avoid such dangerous operations. > > > > With this solution, as part of the initial connect request, backend > > path can be stored in the sysfs per device config, so that on a reconfigure > > request it's easy to check if the backend path matches with the initial > > connect backend path. > > > > Please note, ioctl interface to nbd will not have these changes, as there > > won't be any reconfigure. > > > > Signed-off-by: Prasanna Kumar Kalever > > --- > > drivers/block/nbd.c | 60 +++++++++++++++++++++++++++++++- > > include/uapi/linux/nbd-netlink.h | 1 + > > 2 files changed, 60 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c > > index 4ff71b579cfc..b5022187ad9c 100644 > > --- a/drivers/block/nbd.c > > +++ b/drivers/block/nbd.c > > @@ -79,6 +79,7 @@ struct link_dead_args { > > #define NBD_RT_HAS_CONFIG_REF 4 > > #define NBD_RT_BOUND 5 > > #define NBD_RT_DISCONNECT_ON_CLOSE 6 > > +#define NBD_RT_HAS_BACKEND_FILE 7 > > > > #define NBD_DESTROY_ON_DISCONNECT 0 > > #define NBD_DISCONNECT_REQUESTED 1 > > @@ -119,6 +120,8 @@ struct nbd_device { > > > > struct completion *destroy_complete; > > unsigned long flags; > > + > > + char *backend; > > }; > > > > #define NBD_CMD_REQUEUED 1 > > @@ -216,6 +219,20 @@ static const struct device_attribute pid_attr = { > > .show = pid_show, > > }; > > > > +static ssize_t backend_show(struct device *dev, > > + struct device_attribute *attr, char *buf) > > +{ > > + struct gendisk *disk = dev_to_disk(dev); > > + struct nbd_device *nbd = (struct nbd_device *)disk->private_data; > > + > > + return sprintf(buf, "%s\n", nbd->backend ?: ""); > > +} > > + > > +static const struct device_attribute backend_attr = { > > + .attr = { .name = "backend", .mode = 0444}, > > + .show = backend_show, > > +}; > > + > > static void nbd_dev_remove(struct nbd_device *nbd) > > { > > struct gendisk *disk = nbd->disk; > > @@ -1215,6 +1232,12 @@ static void nbd_config_put(struct nbd_device *nbd) > > &config->runtime_flags)) > > device_remove_file(disk_to_dev(nbd->disk), &pid_attr); > > nbd->task_recv = NULL; > > + if (test_and_clear_bit(NBD_RT_HAS_BACKEND_FILE, > > + &config->runtime_flags)) { > > + device_remove_file(disk_to_dev(nbd->disk), &backend_attr); > > + kfree(nbd->backend); > > + nbd->backend = NULL; > > + } > > nbd_clear_sock(nbd); > > if (config->num_connections) { > > int i; > > @@ -1274,7 +1297,7 @@ static int nbd_start_device(struct nbd_device *nbd) > > > > error = device_create_file(disk_to_dev(nbd->disk), &pid_attr); > > if (error) { > > - dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n"); > > + dev_err(disk_to_dev(nbd->disk), "device_create_file failed for pid!\n"); > > return error; > > } > > set_bit(NBD_RT_HAS_PID_FILE, &config->runtime_flags); > > @@ -1681,6 +1704,7 @@ static int nbd_dev_add(int index) > > BLK_MQ_F_BLOCKING; > > nbd->tag_set.driver_data = nbd; > > nbd->destroy_complete = NULL; > > + nbd->backend = NULL; > > > > err = blk_mq_alloc_tag_set(&nbd->tag_set); > > if (err) > > @@ -1754,6 +1778,7 @@ static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = { > > [NBD_ATTR_SOCKETS] = { .type = NLA_NESTED}, > > [NBD_ATTR_DEAD_CONN_TIMEOUT] = { .type = NLA_U64 }, > > [NBD_ATTR_DEVICE_LIST] = { .type = NLA_NESTED}, > > + [NBD_ATTR_BACKEND_IDENTIFIER] = { .type = NLA_STRING}, > > }; > > > > static const struct nla_policy nbd_sock_policy[NBD_SOCK_MAX + 1] = { > > @@ -1956,6 +1981,23 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info) > > } > > } > > ret = nbd_start_device(nbd); > > + if (ret) > > + goto out; > > + if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) { > > + nbd->backend = nla_strdup(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER], > > + GFP_KERNEL); > > + if (!nbd->backend) { > > + ret = -ENOMEM; > > + goto out; > > + } > > + } > > + ret = device_create_file(disk_to_dev(nbd->disk), &backend_attr); > > + if (ret) { > > + dev_err(disk_to_dev(nbd->disk), > > + "device_create_file failed for backend!\n"); > > + goto out; > > + } > > + set_bit(NBD_RT_HAS_BACKEND_FILE, &config->runtime_flags); > > out: > > mutex_unlock(&nbd->config_lock); > > if (!ret) { > > @@ -2048,6 +2090,22 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info) > > index); > > return -EINVAL; > > } > > + if (nbd->backend) { > > + if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) { > > + if (nla_strcmp(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER], > > + nbd->backend)) { > > + mutex_unlock(&nbd_index_mutex); > > + dev_err(nbd_to_dev(nbd), > > + "backend image doesn't match with %s\n", > > + nbd->backend); > > + return -EINVAL; > > + } > > + } else { > > + mutex_unlock(&nbd_index_mutex); > > + dev_err(nbd_to_dev(nbd), "must specify backend\n"); > > + return -EINVAL; > > + } > > + } > > if (!refcount_inc_not_zero(&nbd->refs)) { > > mutex_unlock(&nbd_index_mutex); > > printk(KERN_ERR "nbd: device at index %d is going down\n", > > diff --git a/include/uapi/linux/nbd-netlink.h b/include/uapi/linux/nbd-netlink.h > > index c5d0ef7aa7d5..2d0b90964227 100644 > > --- a/include/uapi/linux/nbd-netlink.h > > +++ b/include/uapi/linux/nbd-netlink.h > > @@ -35,6 +35,7 @@ enum { > > NBD_ATTR_SOCKETS, > > NBD_ATTR_DEAD_CONN_TIMEOUT, > > NBD_ATTR_DEVICE_LIST, > > + NBD_ATTR_BACKEND_IDENTIFIER, > > __NBD_ATTR_MAX, > > }; > > #define NBD_ATTR_MAX (__NBD_ATTR_MAX - 1) > > Reviewed-by: Xiubo Li Thank you Xiubo Li. Assuming this patch got lost, attempting to bring this patch onto the top with a friendly reminder. I sincerely request the maintainers to please take a look. Many Thanks! -- Prasanna