From patchwork Tue Nov 12 12:24:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 11239191 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 3857213BD for ; Tue, 12 Nov 2019 12:24:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1806820818 for ; Tue, 12 Nov 2019 12:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727077AbfKLMYY (ORCPT ); Tue, 12 Nov 2019 07:24:24 -0500 Received: from mx2.suse.de ([195.135.220.15]:56776 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725874AbfKLMYY (ORCPT ); Tue, 12 Nov 2019 07:24:24 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 9E04EB194; Tue, 12 Nov 2019 12:24:22 +0000 (UTC) From: Johannes Thumshirn To: David Sterba Cc: Linux BTRFS Mailinglist , Johannes Thumshirn Subject: [PATCH 1/5] btrfs: decrement number of open devices after closing the device not before Date: Tue, 12 Nov 2019 13:24:12 +0100 Message-Id: <20191112122416.30672-2-jthumshirn@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20191112122416.30672-1-jthumshirn@suse.de> References: <20191112122416.30672-1-jthumshirn@suse.de> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org In btrfs_close_one_device we're decrementing the number of open devices before we're calling btrfs_close_bdev(). As there is no intermediate exit between these points in this function it is technically OK to do so, but it a) is a bit harder to understand and b) further refactoring to implement proper error handling in btrfs_close_one_device() will change the layout of the function. Move both operations closer together and move the decrement step after btrfs_close_bdev(). Signed-off-by: Johannes Thumshirn --- fs/btrfs/volumes.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 22a5bd991e47..5ee26e7fca32 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -1067,9 +1067,6 @@ static void btrfs_close_one_device(struct btrfs_device *device) struct btrfs_device *new_device; struct rcu_string *name; - if (device->bdev) - fs_devices->open_devices--; - if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && device->devid != BTRFS_DEV_REPLACE_DEVID) { list_del_init(&device->dev_alloc_list); @@ -1080,6 +1077,8 @@ static void btrfs_close_one_device(struct btrfs_device *device) fs_devices->missing_devices--; btrfs_close_bdev(device); + if (device->bdev) + fs_devices->open_devices--; new_device = btrfs_alloc_device(NULL, &device->devid, device->uuid); From patchwork Tue Nov 12 12:24:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 11239195 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 0FC5217E6 for ; Tue, 12 Nov 2019 12:24:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E345E2084E for ; Tue, 12 Nov 2019 12:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727101AbfKLMYZ (ORCPT ); Tue, 12 Nov 2019 07:24:25 -0500 Received: from mx2.suse.de ([195.135.220.15]:56790 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727028AbfKLMYY (ORCPT ); Tue, 12 Nov 2019 07:24:24 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id B48B4B23B; Tue, 12 Nov 2019 12:24:22 +0000 (UTC) From: Johannes Thumshirn To: David Sterba Cc: Linux BTRFS Mailinglist , Johannes Thumshirn Subject: [PATCH 2/5] btrfs: handle device allocation failure in btrfs_close_one_device() Date: Tue, 12 Nov 2019 13:24:13 +0100 Message-Id: <20191112122416.30672-3-jthumshirn@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20191112122416.30672-1-jthumshirn@suse.de> References: <20191112122416.30672-1-jthumshirn@suse.de> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org In btrfs_close_one_device() we're allocating a new device and if this fails we BUG(). Move the allocation to the top of the function and return an error in case it failed. The BUG_ON() is temporarily moved to close_fs_devices(), the caller of btrfs_close_one_device() as further work is pending to untangle this. Signed-off-by: Johannes Thumshirn --- fs/btrfs/volumes.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 5ee26e7fca32..0a2a73907563 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -1061,12 +1061,17 @@ static void btrfs_close_bdev(struct btrfs_device *device) blkdev_put(device->bdev, device->mode); } -static void btrfs_close_one_device(struct btrfs_device *device) +static int btrfs_close_one_device(struct btrfs_device *device) { struct btrfs_fs_devices *fs_devices = device->fs_devices; struct btrfs_device *new_device; struct rcu_string *name; + new_device = btrfs_alloc_device(NULL, &device->devid, + device->uuid); + if (IS_ERR(new_device)) + goto err_close_device; + if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && device->devid != BTRFS_DEV_REPLACE_DEVID) { list_del_init(&device->dev_alloc_list); @@ -1080,10 +1085,6 @@ static void btrfs_close_one_device(struct btrfs_device *device) if (device->bdev) fs_devices->open_devices--; - new_device = btrfs_alloc_device(NULL, &device->devid, - device->uuid); - BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ - /* Safe because we are under uuid_mutex */ if (device->name) { name = rcu_string_strdup(device->name->str, GFP_NOFS); @@ -1096,18 +1097,32 @@ static void btrfs_close_one_device(struct btrfs_device *device) synchronize_rcu(); btrfs_free_device(device); + + return 0; + +err_close_device: + btrfs_close_bdev(device); + if (device->bdev) { + fs_devices->open_devices--; + btrfs_sysfs_rm_device_link(fs_devices, device); + device->bdev = NULL; + } + + return -ENOMEM; } static int close_fs_devices(struct btrfs_fs_devices *fs_devices) { struct btrfs_device *device, *tmp; + int ret; if (--fs_devices->opened > 0) return 0; mutex_lock(&fs_devices->device_list_mutex); list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { - btrfs_close_one_device(device); + ret = btrfs_close_one_device(device); + BUG_ON(ret); /* -ENOMEM */ } mutex_unlock(&fs_devices->device_list_mutex); From patchwork Tue Nov 12 12:24:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 11239199 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 0286A13BD for ; Tue, 12 Nov 2019 12:24:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E16842084E for ; Tue, 12 Nov 2019 12:24:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727116AbfKLMY0 (ORCPT ); Tue, 12 Nov 2019 07:24:26 -0500 Received: from mx2.suse.de ([195.135.220.15]:56782 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727027AbfKLMYY (ORCPT ); Tue, 12 Nov 2019 07:24:24 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id AF284B1AB; Tue, 12 Nov 2019 12:24:22 +0000 (UTC) From: Johannes Thumshirn To: David Sterba Cc: Linux BTRFS Mailinglist , Johannes Thumshirn Subject: [PATCH 3/5] btrfs: handle allocation failure in strdup Date: Tue, 12 Nov 2019 13:24:14 +0100 Message-Id: <20191112122416.30672-4-jthumshirn@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20191112122416.30672-1-jthumshirn@suse.de> References: <20191112122416.30672-1-jthumshirn@suse.de> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Gracefully handle allocation failures in btrfs_close_one_device()'s rcu_string_strdup() instead of crashing the machine. Signed-off-by: Johannes Thumshirn --- fs/btrfs/volumes.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 0a2a73907563..e5864ca3bb3b 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -1064,7 +1064,7 @@ static void btrfs_close_bdev(struct btrfs_device *device) static int btrfs_close_one_device(struct btrfs_device *device) { struct btrfs_fs_devices *fs_devices = device->fs_devices; - struct btrfs_device *new_device; + struct btrfs_device *new_device = NULL; struct rcu_string *name; new_device = btrfs_alloc_device(NULL, &device->devid, @@ -1072,6 +1072,15 @@ static int btrfs_close_one_device(struct btrfs_device *device) if (IS_ERR(new_device)) goto err_close_device; + /* Safe because we are under uuid_mutex */ + if (device->name) { + name = rcu_string_strdup(device->name->str, GFP_NOFS); + if (!name) + goto err_free_device; + + rcu_assign_pointer(new_device->name, name); + } + if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && device->devid != BTRFS_DEV_REPLACE_DEVID) { list_del_init(&device->dev_alloc_list); @@ -1085,13 +1094,6 @@ static int btrfs_close_one_device(struct btrfs_device *device) if (device->bdev) fs_devices->open_devices--; - /* Safe because we are under uuid_mutex */ - if (device->name) { - name = rcu_string_strdup(device->name->str, GFP_NOFS); - BUG_ON(!name); /* -ENOMEM */ - rcu_assign_pointer(new_device->name, name); - } - list_replace_rcu(&device->dev_list, &new_device->dev_list); new_device->fs_devices = device->fs_devices; @@ -1100,6 +1102,10 @@ static int btrfs_close_one_device(struct btrfs_device *device) return 0; +err_free_device: + if (new_device) + btrfs_free_device(new_device); + err_close_device: btrfs_close_bdev(device); if (device->bdev) { From patchwork Tue Nov 12 12:24:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 11239193 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 92304139A for ; Tue, 12 Nov 2019 12:24:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7D2552084E for ; Tue, 12 Nov 2019 12:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725997AbfKLMYZ (ORCPT ); Tue, 12 Nov 2019 07:24:25 -0500 Received: from mx2.suse.de ([195.135.220.15]:56804 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727047AbfKLMYY (ORCPT ); Tue, 12 Nov 2019 07:24:24 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id C6543B287; Tue, 12 Nov 2019 12:24:22 +0000 (UTC) From: Johannes Thumshirn To: David Sterba Cc: Linux BTRFS Mailinglist , Johannes Thumshirn Subject: [PATCH 4/5] btrfs: handle error return of close_fs_devices() Date: Tue, 12 Nov 2019 13:24:15 +0100 Message-Id: <20191112122416.30672-5-jthumshirn@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20191112122416.30672-1-jthumshirn@suse.de> References: <20191112122416.30672-1-jthumshirn@suse.de> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org close_fs_devices() will be able to return an error instead of crashing after the following patch. Prepare btrfs_close_devices() for this. Signed-off-by: Johannes Thumshirn --- fs/btrfs/volumes.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index e5864ca3bb3b..be1fd935edf7 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -1143,10 +1143,10 @@ static int close_fs_devices(struct btrfs_fs_devices *fs_devices) int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) { struct btrfs_fs_devices *seed_devices = NULL; - int ret; + int err, err2 = 0; mutex_lock(&uuid_mutex); - ret = close_fs_devices(fs_devices); + err = close_fs_devices(fs_devices); if (!fs_devices->opened) { seed_devices = fs_devices->seed; fs_devices->seed = NULL; @@ -1156,10 +1156,13 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) while (seed_devices) { fs_devices = seed_devices; seed_devices = fs_devices->seed; - close_fs_devices(fs_devices); + err2 = close_fs_devices(fs_devices); free_fs_devices(fs_devices); } - return ret; + + if (err2) + return err2; + return err; } static int open_fs_devices(struct btrfs_fs_devices *fs_devices, From patchwork Tue Nov 12 12:24:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 11239197 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 AA2C0139A for ; Tue, 12 Nov 2019 12:24:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 91F642084E for ; Tue, 12 Nov 2019 12:24:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727103AbfKLMYZ (ORCPT ); Tue, 12 Nov 2019 07:24:25 -0500 Received: from mx2.suse.de ([195.135.220.15]:56802 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727044AbfKLMYY (ORCPT ); Tue, 12 Nov 2019 07:24:24 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id CE2E0B2CE; Tue, 12 Nov 2019 12:24:22 +0000 (UTC) From: Johannes Thumshirn To: David Sterba Cc: Linux BTRFS Mailinglist , Johannes Thumshirn Subject: [PATCH 5/5] btrfs: remove final BUG_ON() in close_fs_devices() Date: Tue, 12 Nov 2019 13:24:16 +0100 Message-Id: <20191112122416.30672-6-jthumshirn@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20191112122416.30672-1-jthumshirn@suse.de> References: <20191112122416.30672-1-jthumshirn@suse.de> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Now that the preparation work is done, remove the temporary BUG_ON() in close_fs_devices() and return an error instead. Signed-off-by: Johannes Thumshirn --- fs/btrfs/volumes.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index be1fd935edf7..844333b96075 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -1128,7 +1128,12 @@ static int close_fs_devices(struct btrfs_fs_devices *fs_devices) mutex_lock(&fs_devices->device_list_mutex); list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { ret = btrfs_close_one_device(device); - BUG_ON(ret); /* -ENOMEM */ + if (ret) { + mutex_unlock(&fs_devices->device_list_mutex); + return ret; + } + fs_devices->opened--; + fs_devices->seeding--; } mutex_unlock(&fs_devices->device_list_mutex);