From patchwork Tue Oct 10 07:51:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9995229 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 D80FC603B5 for ; Tue, 10 Oct 2017 07:51:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C8CE028066 for ; Tue, 10 Oct 2017 07:51:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BD6CC280CF; Tue, 10 Oct 2017 07:51:33 +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.9 required=2.0 tests=BAYES_00,FREEMAIL_FROM, 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 48A8328066 for ; Tue, 10 Oct 2017 07:51:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755515AbdJJHva (ORCPT ); Tue, 10 Oct 2017 03:51:30 -0400 Received: from mout.gmx.net ([212.227.15.18]:64777 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755157AbdJJHv2 (ORCPT ); Tue, 10 Oct 2017 03:51:28 -0400 Received: from localhost.localdomain ([104.207.157.105]) by mail.gmx.com (mrgmx001 [212.227.17.184]) with ESMTPSA (Nemesis) id 0LlDb4-1dSDaS0jO2-00b47I; Tue, 10 Oct 2017 09:51:26 +0200 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz, yoasif@gmail.com, rrauenza@gmail.com Subject: [PATCH 3/4] btrfs-progs: check: Also check unalignment/mismatch device and super size Date: Tue, 10 Oct 2017 07:51:12 +0000 Message-Id: <20171010075113.10718-4-quwenruo.btrfs@gmx.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171010075113.10718-1-quwenruo.btrfs@gmx.com> References: <20171010075113.10718-1-quwenruo.btrfs@gmx.com> X-Provags-ID: V03:K0:kWRVnO8W2BIrs+J/j7vHoK/BY6NceC69ZZc6UKNmtsoYd0kpYjs dS+LQYxHppiAEU3KHM3bTc3Wyy0EjMHW3k4IH0kBdaNilDcVKA8txVQTiLjJN8aJer4nEnW bMEWBekkmtSd1Al7S2SXGGa4LjzOiWNBkqjIcXG1U7s6cETTGzMtKmUVpVNNOPnHs8oMqR2 yfK0G3mCgiKi/YqdL/Z/A== X-UI-Out-Filterresults: notjunk:1; V01:K0:D0N0i1jQvFw=:OpbnKqnxid3STlWj+I2EX1 d8c+KetAt7Nc5EoWLYTHHzVpRRibZDI9EUH+uuN7eBtdat3QPx8gGaUmOMEQr0w8Qn6o0+7bL /Jrk5qISLYp1fPo0G5lLV1rjTOD45vAY8Qd5JlL1EWJllW9gA0Dr5rUTy90+UG+oBPrTRaElz U6STWpHkKBhUHcRXlyXc/HPLLMJ3RniNm3EsRfsrIA5UDkiIWdBTmr+TygSx1NBFxkDp8Dn6c Z37jhZw8K7U2eqb4zGFuDq3vY5yNK0EIbzdY2PCFGekuqo7E8408g2ji9gyF1ZIPpDmN28gIX t+xO5pFFbfJMwdPUwAqxEv2Dcy9KS5F+e3cyzLllmA3zKroKf92AcbNJdpRozwyvdTqKLC0xA KGV5AzCiVyALwuAPdi+lQtkMM42mw9tFxyf/0ON5hUk9qLIdQK5e+PDH/Q377DGEPwcN6efy9 4BBlfiOx8VX/h9RyUw1hW4wo/OHC+6RA1KmAXPVwCIjBBYMhzcntqQQLO1dow6TupvGRzqhPP 0Pf7an0Dn6vvvbyCfWgWkd0iemW3kF2Efzn1d8KFXKZXcp3BidkXu/m+/EpeYioUvBwxqBE6N ERlA7iKClaDzOASmwWFV3bbFYiaS9vEU2ViFkukTkRKrgsy10O/J7/4lHu4HKpLeek0OBqPOk EJGvjf5rLyBXFbNREpCJ5FnoIqedDD9Jaa74s3LKl7b8ZgbMegC2Uit0CC53P8X8ntA0w5VFx ZdHhYtwOnRTz0DMbUij1WZxs/t0LeyPC8bMES5plS13+1iEVwTi1OTVSHfmWHkPrVNlN2duse uDnkVfJ239MnVp1m7mvdjF4gps5zg== Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Along with the fix introduced, also introduce check for them. Unlike normal check funtions, some of the check is optional, and even if the image failed to pass optional check, kernel can still run fine. (But may cause noisy kernel warning) So some check, mainly for alignment, will not cause btrfs check to fail, but only to output warning and how to fix it. Signed-off-by: Qu Wenruo --- cmds-check.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/cmds-check.c b/cmds-check.c index fdb6d832eee1..23dd3b51102b 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -9879,6 +9879,67 @@ static int check_device_used(struct device_record *dev_rec, } } +/* + * Extra (optional) check for dev_item size + * + * To avoid possible kernel warning for v4.14 kernel. + * It's not a deadly problem, just to info v4.14 kernel user. + * So it won't change the return value. + */ +static void check_dev_size_alignment(u64 devid, u64 total_bytes, + u32 sectorsize) +{ + if (!IS_ALIGNED(total_bytes, sectorsize)) { + warning("unaligned total_bytes detected for devid %llu, have %llu should be aligned to %u", + devid, total_bytes, sectorsize); + warning("this is OK for older kernel (<4.14), but may cause kernel warning for newer kernel (>=4.14)"); + warning("this can be fixed by 'btrfs check --fix-dev-size'"); + } +} + +/* + * Unlike device size alignment check above, some super total_bytes check + * failure can lead to unmountable fs for kernel >=v4.6. + * + * So this function will return the error for fatal super total_bytes problem. + */ +static int check_super_size(struct btrfs_fs_info *fs_info) +{ + struct btrfs_device *dev; + struct list_head *dev_list = &fs_info->fs_devices->devices; + u64 total_bytes = 0; + u64 super_bytes = btrfs_super_total_bytes(fs_info->super_copy); + + list_for_each_entry(dev, dev_list, dev_list) + total_bytes += dev->total_bytes; + + /* Important check, which can cause unmountable fs */ + if (super_bytes < total_bytes) { + error("super total bytes %llu smaller than real devices size %llu", + super_bytes, total_bytes); + error("this fs will not be mountable for newer kernels (>=v4.6)"); + error("this can be fixed by 'btrfs check --fix-dev-size'"); + return 1; + } + + /* + * Optional check, just to make everything aligned and match with + * each other. + * + * For btrfs-image restored fs, we don't need to check it anyway. + */ + if (btrfs_super_flags(fs_info->super_copy) & + (BTRFS_SUPER_FLAG_METADUMP | BTRFS_SUPER_FLAG_METADUMP_V2)) + return 0; + if (!IS_ALIGNED(super_bytes, fs_info->sectorsize) || + !IS_ALIGNED(total_bytes, fs_info->sectorsize) || + super_bytes != total_bytes) { + warning("minor unaligned/mismatch device size detected"); + warning("recommended to use 'btrfs check --fix-dev-size' to fix it"); + } + return 0; +} + /* check btrfs_dev_item -> btrfs_dev_extent */ static int check_devices(struct rb_root *dev_cache, struct device_extent_tree *dev_extent_cache) @@ -9896,6 +9957,11 @@ static int check_devices(struct rb_root *dev_cache, if (err) ret = err; + if (!IS_ALIGNED(dev_rec->total_byte, global_info->sectorsize)) { + } + + check_dev_size_alignment(dev_rec->devid, dev_rec->total_byte, + global_info->sectorsize); dev_node = rb_next(dev_node); } list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans, @@ -11141,6 +11207,7 @@ static int check_dev_item(struct btrfs_fs_info *fs_info, struct btrfs_path path; struct btrfs_key key; struct btrfs_dev_extent *ptr; + u64 total_bytes; u64 dev_id; u64 used; u64 total = 0; @@ -11149,6 +11216,7 @@ static int check_dev_item(struct btrfs_fs_info *fs_info, dev_item = btrfs_item_ptr(eb, slot, struct btrfs_dev_item); dev_id = btrfs_device_id(eb, dev_item); used = btrfs_device_bytes_used(eb, dev_item); + total_bytes = btrfs_device_total_bytes(eb, dev_item); key.objectid = dev_id; key.type = BTRFS_DEV_EXTENT_KEY; @@ -11193,6 +11261,8 @@ next: BTRFS_DEV_EXTENT_KEY, dev_id); return ACCOUNTING_MISMATCH; } + check_dev_size_alignment(dev_id, total_bytes, fs_info->sectorsize); + return 0; } @@ -13471,6 +13541,9 @@ int cmd_check(int argc, char **argv) error( "errors found in extent allocation tree or chunk allocation"); + /* Only re-check super size after we checked and repaired the fs */ + err |= check_super_size(info); + ret = repair_root_items(info); err |= !!ret; if (ret < 0) {