From patchwork Thu Apr 6 21:01:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 9668415 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 C92C960364 for ; Thu, 6 Apr 2017 21:02:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D01B0285D5 for ; Thu, 6 Apr 2017 21:02:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C4D57285DD; Thu, 6 Apr 2017 21:02:18 +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.4 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 345C3285D5 for ; Thu, 6 Apr 2017 21:02:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753112AbdDFVCR (ORCPT ); Thu, 6 Apr 2017 17:02:17 -0400 Received: from mail-qt0-f195.google.com ([209.85.216.195]:36182 "EHLO mail-qt0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753391AbdDFVCQ (ORCPT ); Thu, 6 Apr 2017 17:02:16 -0400 Received: by mail-qt0-f195.google.com with SMTP id n37so7381119qtb.3 for ; Thu, 06 Apr 2017 14:02:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=zUCB++0ataR22p8VaCSR3bRoln0478gTck1Bxsyf5KA=; b=kn8eAL2uao7CL+iKrIR7jfZReUuqlWBLOi7FZABcz/JnB//El46ytg6yiybX5WaGnw QVZbOIy7COdGuAnVqyQobgG9yQiCwN+NylBK4VYQCXgAD45bhc1NTs97IyK4HjoG9jMw a5efxsh87K9R+Mf0Je8acVn13Ji3Fbo/WU0YGDNVdC6X62xu0waK6UvnsN912YKNSAN2 pBbKvHgyEVeQgmucFnHyu3Vqcwn6Ny3qTU2aB549RrCjVvfmvc6ElIqSebLyWtSqS+Qz mtsOv+vJMZiL08CBudz61PyB92lBzwAoTfyB4ZCC9D8nVHH3wX5a7ADcOq4De6f0IVlB 7g3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=zUCB++0ataR22p8VaCSR3bRoln0478gTck1Bxsyf5KA=; b=lca1K/6h3O3qv1oS9yyXd6CjW2IoiFYLk9BDdPf4d+9TKHnJlafiaxGaoTY4cKoXrv BpVVCYfEbDLodAvCxBbkbzaiPh5n+9hUEIxzTJNnuK3ln9Nxaf2hfeHVupBKeWr7n+Sb 4Wj3ubTs5fGnXnk+3pML4it47165Uu8kfl8fp7In+8m+5U8mHaXpnmNRpR0wfCD2vKyI LGTOYPGVHjju/aJBl8UqvjhTN3hwSlq8ZD4QaTdLYgowOXDw0kha4Q49vJXZrX2MIG0W 3v/Qh6kkVqjchCyYu2VLsm9YfqBstQRHuQWDfSxPQveFCneLE26u8OKalra3HjUn5kbJ C/XQ== X-Gm-Message-State: AFeK/H0UoOx4DkbBtJtMd7MOrtOUnVqsp3dd/tQcIabVXgoRhbge0BiRdulo1sUx7Rrg2Q== X-Received: by 10.237.36.151 with SMTP id t23mr40385324qtc.8.1491512535571; Thu, 06 Apr 2017 14:02:15 -0700 (PDT) Received: from localhost (cpe-2606-A000-4381-1201-225-22FF-FEB3-E51A.dyn6.twc.com. [2606:a000:4381:1201:225:22ff:feb3:e51a]) by smtp.gmail.com with ESMTPSA id x139sm1724157qkb.21.2017.04.06.14.02.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Apr 2017 14:02:15 -0700 (PDT) From: Josef Bacik X-Google-Original-From: Josef Bacik To: axboe@kernel.dk, nbd-general@lists.sourceforge.net, linux-block@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 04/12] nbd: stop using the bdev everywhere Date: Thu, 6 Apr 2017 17:01:59 -0400 Message-Id: <1491512527-4286-5-git-send-email-jbacik@fb.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1491512527-4286-1-git-send-email-jbacik@fb.com> References: <1491512527-4286-1-git-send-email-jbacik@fb.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 In preparation for the upcoming netlink interface we need to not rely on already having the bdev for the NBD device we are doing operations on. Instead of passing the bdev around, just use it in places where we know we already have the bdev. Signed-off-by: Josef Bacik --- drivers/block/nbd.c | 92 +++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 55 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index c2d9923..8891889 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -120,11 +120,6 @@ static inline struct device *nbd_to_dev(struct nbd_device *nbd) return disk_to_dev(nbd->disk); } -static bool nbd_is_connected(struct nbd_device *nbd) -{ - return !!nbd->task_recv; -} - static const char *nbdcmd_to_ascii(int cmd) { switch (cmd) { @@ -160,36 +155,30 @@ static void nbd_mark_nsock_dead(struct nbd_sock *nsock) nsock->sent = 0; } -static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev) +static void nbd_size_clear(struct nbd_device *nbd) { if (nbd->config->bytesize) { - if (bdev->bd_openers <= 1) - bd_set_size(bdev, 0); set_capacity(nbd->disk, 0); kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); } - - return 0; } -static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev) +static void nbd_size_update(struct nbd_device *nbd) { struct nbd_config *config = nbd->config; blk_queue_logical_block_size(nbd->disk->queue, config->blksize); blk_queue_physical_block_size(nbd->disk->queue, config->blksize); - bd_set_size(bdev, config->bytesize); set_capacity(nbd->disk, config->bytesize >> 9); kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE); } -static void nbd_size_set(struct nbd_device *nbd, struct block_device *bdev, - loff_t blocksize, loff_t nr_blocks) +static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize, + loff_t nr_blocks) { struct nbd_config *config = nbd->config; config->blksize = blocksize; config->bytesize = blocksize * nr_blocks; - if (nbd_is_connected(nbd)) - nbd_size_update(nbd, bdev); + nbd_size_update(nbd); } static void nbd_end_request(struct nbd_cmd *cmd) @@ -739,8 +728,7 @@ static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx, return ret; } -static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev, - unsigned long arg) +static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg) { struct nbd_config *config = nbd->config; struct socket *sock; @@ -783,8 +771,6 @@ static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev, nsock->sent = 0; socks[config->num_connections++] = nsock; - if (max_part) - bdev->bd_invalidated = 1; return 0; } @@ -800,19 +786,20 @@ static void nbd_bdev_reset(struct block_device *bdev) { if (bdev->bd_openers > 1) return; - set_device_ro(bdev, false); - bdev->bd_inode->i_size = 0; + bd_set_size(bdev, 0); if (max_part > 0) { blkdev_reread_part(bdev); bdev->bd_invalidated = 1; } } -static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev) +static void nbd_parse_flags(struct nbd_device *nbd) { struct nbd_config *config = nbd->config; if (config->flags & NBD_FLAG_READ_ONLY) - set_device_ro(bdev, true); + set_disk_ro(nbd->disk, true); + else + set_disk_ro(nbd->disk, false); if (config->flags & NBD_FLAG_SEND_TRIM) queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); if (config->flags & NBD_FLAG_SEND_FLUSH) @@ -841,52 +828,36 @@ static void send_disconnects(struct nbd_device *nbd) } } -static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev) +static int nbd_disconnect(struct nbd_device *nbd) { struct nbd_config *config = nbd->config; dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n"); - mutex_unlock(&nbd->config_lock); - fsync_bdev(bdev); - mutex_lock(&nbd->config_lock); - if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED, &config->runtime_flags)) send_disconnects(nbd); return 0; } -static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev) +static void nbd_clear_sock(struct nbd_device *nbd) { sock_shutdown(nbd); nbd_clear_que(nbd); - - __invalidate_device(bdev, true); - nbd_bdev_reset(bdev); nbd->task_setup = NULL; - return 0; } static void nbd_config_put(struct nbd_device *nbd) { if (refcount_dec_and_mutex_lock(&nbd->config_refs, &nbd->config_lock)) { - struct block_device *bdev; struct nbd_config *config = nbd->config; - - bdev = bdget_disk(nbd->disk, 0); - if (!bdev) { - mutex_unlock(&nbd->config_lock); - return; - } - nbd_dev_dbg_close(nbd); - nbd_size_clear(nbd, bdev); + nbd_size_clear(nbd); if (test_and_clear_bit(NBD_HAS_PID_FILE, &config->runtime_flags)) device_remove_file(disk_to_dev(nbd->disk), &pid_attr); nbd->task_recv = NULL; - nbd_clear_sock(nbd, bdev); + nbd_clear_sock(nbd); if (config->num_connections) { int i; for (i = 0; i < config->num_connections; i++) { @@ -897,7 +868,6 @@ static void nbd_config_put(struct nbd_device *nbd) } nbd_reset(nbd); mutex_unlock(&nbd->config_lock); - bdput(bdev); module_put(THIS_MODULE); } } @@ -912,27 +882,30 @@ static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev) return -EBUSY; if (!config->socks) return -EINVAL; - if (num_connections > 1 && !(config->flags & NBD_FLAG_CAN_MULTI_CONN)) { dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n"); return -EINVAL; } + if (max_part) + bdev->bd_invalidated = 1; blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections); nbd->task_recv = current; mutex_unlock(&nbd->config_lock); - nbd_parse_flags(nbd, bdev); + nbd_parse_flags(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"); return error; } - set_bit(NBD_HAS_PID_FILE, &config->runtime_flags); - nbd_size_update(nbd, bdev); + set_bit(NBD_HAS_PID_FILE, &config->runtime_flags); + if (max_part) + bdev->bd_invalidated = 1; + bd_set_size(bdev, config->bytesize); nbd_dev_dbg_init(nbd); for (i = 0; i < num_connections; i++) { @@ -965,6 +938,14 @@ static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev) return error; } +static void nbd_clear_sock_ioctl(struct nbd_device *nbd, + struct block_device *bdev) +{ + nbd_clear_sock(nbd); + kill_bdev(bdev); + nbd_bdev_reset(bdev); +} + /* Must be called with config_lock held */ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, unsigned int cmd, unsigned long arg) @@ -973,21 +954,22 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, switch (cmd) { case NBD_DISCONNECT: - return nbd_disconnect(nbd, bdev); + return nbd_disconnect(nbd); case NBD_CLEAR_SOCK: - return nbd_clear_sock(nbd, bdev); + nbd_clear_sock_ioctl(nbd, bdev); + return 0; case NBD_SET_SOCK: - return nbd_add_socket(nbd, bdev, arg); + return nbd_add_socket(nbd, arg); case NBD_SET_BLKSIZE: - nbd_size_set(nbd, bdev, arg, + nbd_size_set(nbd, arg, div_s64(config->bytesize, arg)); return 0; case NBD_SET_SIZE: - nbd_size_set(nbd, bdev, config->blksize, + nbd_size_set(nbd, config->blksize, div_s64(arg, config->blksize)); return 0; case NBD_SET_SIZE_BLOCKS: - nbd_size_set(nbd, bdev, config->blksize, arg); + nbd_size_set(nbd, config->blksize, arg); return 0; case NBD_SET_TIMEOUT: if (arg) {