From patchwork Wed May 24 07:41:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13253407 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14D7CC77B7C for ; Wed, 24 May 2023 07:41:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239981AbjEXHlz (ORCPT ); Wed, 24 May 2023 03:41:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239941AbjEXHlw (ORCPT ); Wed, 24 May 2023 03:41:52 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6F7C91 for ; Wed, 24 May 2023 00:41:50 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 830EF1F750 for ; Wed, 24 May 2023 07:41:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1684914109; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KeMxxFOCh3GOlfHbl4lBa5S05J+OJFUUhMywAuDYrI8=; b=stqvbENROCyT2k7o3EhHhXP1SFCGd02SUWp60XW/rOl1WuABoBc/AkTzXUw+egLTxj37tS 6d/0C/LMs9qYCvfD8114iW6EYwuRTWbeNpj+2OcdishnoNlhm6waHMAgH9d8ZvaLfQlPkW zxfTPWN6YUHUDjKorlpQ0SUM+7TL+7A= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id DADDF13425 for ; Wed, 24 May 2023 07:41:48 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id kNQwKby/bWSiRQAAMHmgww (envelope-from ) for ; Wed, 24 May 2023 07:41:48 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/7] btrfs-progs: tune: implement resume support for metadata checksum Date: Wed, 24 May 2023 15:41:24 +0800 Message-Id: <592baf7bd93e0fce6a30faa7a216f894c66aab09.1684913599.git.wqu@suse.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org For interrupted metadata checksum change, we only need to call the same change_meta_csums(). Since we don't have any record on the last converted metadata, thus we have to go through all metadata anyway. And the existing change_meta_csums() has already implemented the needed checks to skip converted metadata. Since we're here, also implement all the surrounding checks, like making sure the new target csum type matches the interrupted one. Signed-off-by: Qu Wenruo --- tune/change-csum.c | 88 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 7 deletions(-) diff --git a/tune/change-csum.c b/tune/change-csum.c index b5efc3a8807f..7ae618a433cb 100644 --- a/tune/change-csum.c +++ b/tune/change-csum.c @@ -45,12 +45,7 @@ static int check_csum_change_requreiment(struct btrfs_fs_info *fs_info) error("no csum change support for extent-tree-v2 feature yet."); return -EOPNOTSUPP; } - if (btrfs_super_flags(fs_info->super_copy) & - (BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM | - BTRFS_SUPER_FLAG_CHANGING_META_CSUM)) { - error("resume from half converted status is not yet supported"); - return -EOPNOTSUPP; - } + key.objectid = BTRFS_BALANCE_OBJECTID; key.type = BTRFS_TEMPORARY_ITEM_KEY; key.offset = 0; @@ -522,7 +517,7 @@ out: return ret; } -static int change_meta_csums(struct btrfs_fs_info *fs_info, u32 new_csum_type) +static int change_meta_csums(struct btrfs_fs_info *fs_info, u16 new_csum_type) { struct btrfs_root *extent_root = btrfs_extent_root(fs_info, 0); struct btrfs_path path = { 0 }; @@ -640,6 +635,71 @@ out: return ret; } +static int resume_csum_change(struct btrfs_fs_info *fs_info, u16 new_csum_type) +{ + const u64 super_flags = btrfs_super_flags(fs_info->super_copy); + struct btrfs_root *tree_root = fs_info->tree_root; + struct btrfs_path path = { 0 }; + struct btrfs_key key; + int ret; + + if ((super_flags & (BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM | + BTRFS_SUPER_FLAG_CHANGING_META_CSUM)) == + (BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM | + BTRFS_SUPER_FLAG_CHANGING_META_CSUM)) { + error("Invalid super flags, only one bit of CHANGING_DATA_CSUM or CHANGING_META_CSUM can be set"); + return -EUCLEAN; + } + + key.objectid = BTRFS_CSUM_CHANGE_OBJECTID; + key.type = BTRFS_TEMPORARY_ITEM_KEY; + key.offset = (u64)-1; + ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0); + if (ret < 0) { + errno = -ret; + error("failed to locate the csum change item: %m"); + return ret; + } + assert(ret > 0); + ret = btrfs_previous_item(tree_root, &path, BTRFS_CSUM_CHANGE_OBJECTID, + BTRFS_TEMPORARY_ITEM_KEY); + if (ret > 0) + ret = -ENOENT; + if (ret < 0) { + errno = -ret; + error("failed to locate the csum change item: %m"); + btrfs_release_path(&path); + return ret; + } + btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); + btrfs_release_path(&path); + + if (new_csum_type != key.offset) { + ret = -EINVAL; + error("target csum type mismatch with interrupted csum type, has %s (%u) expect %s (%llu)", + btrfs_super_csum_name(new_csum_type), new_csum_type, + btrfs_super_csum_name(key.offset), key.offset); + return ret; + } + + if (super_flags & BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM) { + error("resume on data checksum changing is not yet supported"); + return -EOPNOTSUPP; + } + + /* + * For metadata resume, just call the same change_meta_csums(), as we + * have no record on previous converted metadata, thus have to go + * through all metadata anyway. + */ + ret = change_meta_csums(fs_info, new_csum_type); + if (ret < 0) { + errno = -ret; + error("failed to resume metadata csum change: %m"); + } + return ret; +} + int btrfs_change_csum_type(struct btrfs_fs_info *fs_info, u16 new_csum_type) { u16 old_csum_type = fs_info->csum_type; @@ -650,6 +710,20 @@ int btrfs_change_csum_type(struct btrfs_fs_info *fs_info, u16 new_csum_type) if (ret < 0) return ret; + if (btrfs_super_flags(fs_info->super_copy) & + (BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM | + BTRFS_SUPER_FLAG_CHANGING_META_CSUM)) { + ret = resume_csum_change(fs_info, new_csum_type); + if (ret < 0) { + errno = -ret; + error("failed to resume unfinished csum change: %m"); + return ret; + } + printf("converted csum type from %s (%u) to %s (%u)\n", + btrfs_super_csum_name(old_csum_type), old_csum_type, + btrfs_super_csum_name(new_csum_type), new_csum_type); + return ret; + } /* * Phase 1, generate new data csums. * From patchwork Wed May 24 07:41:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13253408 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 285FCC77B7A for ; Wed, 24 May 2023 07:41:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239993AbjEXHl4 (ORCPT ); Wed, 24 May 2023 03:41:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239978AbjEXHlx (ORCPT ); Wed, 24 May 2023 03:41:53 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF5358F for ; Wed, 24 May 2023 00:41:51 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 8B08C1F8AF for ; Wed, 24 May 2023 07:41:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1684914110; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=h+MsLyQ9EtzC19HFwPmjq9vM57S1dbnZRsoyGE957bA=; b=O/trRCAo1ZgqsImBVENpYth35ynPLoskHgX/UtEoT0LqlSAirmpsjz6jguiLYEqFDx3eWO e7C/UDZKXkguQnEFBh98HIhUAv3K5i6AJ9aIM2kY0ZD3QzmRzWy1QQh+qodsgJuaG4tZXT YtAWmkVHV745iPfjvqAqVFyUFtbvi2o= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E2DAC13425 for ; Wed, 24 May 2023 07:41:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id +BwkK72/bWSiRQAAMHmgww (envelope-from ) for ; Wed, 24 May 2023 07:41:49 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/7] btrfs-progs: tune: implement resume support for generating new data csum Date: Wed, 24 May 2023 15:41:25 +0800 Message-Id: <2e78225fdd088219c01b45865a112204536f21f4.1684913599.git.wqu@suse.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If a csum change is interrupted at new data csum generation stage, we can detect such situation by checking the old and new csum items. At the new data csum generation stage, old csums are untouched, and only new csums items (with different objectid) are inserted into the csum tree. Thus the old csum items should cover a larger range, while the new csum items should be a subset of the old csums. The resume part would start by re-generating the remaining part, then go through the conversion stages. Signed-off-by: Qu Wenruo --- tune/change-csum.c | 226 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 193 insertions(+), 33 deletions(-) diff --git a/tune/change-csum.c b/tune/change-csum.c index 7ae618a433cb..b95a4117b59b 100644 --- a/tune/change-csum.c +++ b/tune/change-csum.c @@ -197,9 +197,9 @@ out: * item. */ #define CSUM_CHANGE_BYTES_THRESHOLD (SZ_2M) -static int generate_new_data_csums(struct btrfs_fs_info *fs_info, u16 new_csum_type) +static int generate_new_data_csums_range(struct btrfs_fs_info *fs_info, u64 start, + u16 new_csum_type) { - struct btrfs_root *tree_root = fs_info->tree_root; struct btrfs_root *csum_root = btrfs_csum_root(fs_info, 0); struct btrfs_trans_handle *trans; struct btrfs_path path = { 0 }; @@ -208,7 +208,7 @@ static int generate_new_data_csums(struct btrfs_fs_info *fs_info, u16 new_csum_t void *csum_buffer; u64 converted_bytes = 0; u64 last_csum; - u64 cur = 0; + u64 cur = start; int ret; ret = get_last_csum_bytenr(fs_info, &last_csum); @@ -221,34 +221,6 @@ static int generate_new_data_csums(struct btrfs_fs_info *fs_info, u16 new_csum_t if (!csum_buffer) return -ENOMEM; - trans = btrfs_start_transaction(tree_root, 1); - if (IS_ERR(trans)) { - ret = PTR_ERR(trans); - errno = -ret; - error("failed to start transaction: %m"); - goto out; - } - key.objectid = BTRFS_CSUM_CHANGE_OBJECTID; - key.type = BTRFS_TEMPORARY_ITEM_KEY; - key.offset = new_csum_type; - ret = btrfs_insert_empty_item(trans, tree_root, &path, &key, 0); - btrfs_release_path(&path); - if (ret < 0) { - errno = -ret; - error("failed to insert csum change item: %m"); - btrfs_abort_transaction(trans, ret); - goto out; - } - btrfs_set_super_flags(fs_info->super_copy, - btrfs_super_flags(fs_info->super_copy) | - BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM); - ret = btrfs_commit_transaction(trans, tree_root); - if (ret < 0) { - errno = -ret; - error("failed to commit the initial transaction: %m"); - goto out; - } - trans = btrfs_start_transaction(csum_root, CSUM_CHANGE_BYTES_THRESHOLD / fs_info->sectorsize * new_csum_size); @@ -321,6 +293,44 @@ out: return ret; } +static int generate_new_data_csums(struct btrfs_fs_info *fs_info, u16 new_csum_type) +{ + struct btrfs_root *tree_root = fs_info->tree_root; + struct btrfs_trans_handle *trans; + struct btrfs_path path = { 0 }; + struct btrfs_key key; + int ret; + + trans = btrfs_start_transaction(tree_root, 1); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + errno = -ret; + error("failed to start transaction: %m"); + return ret; + } + key.objectid = BTRFS_CSUM_CHANGE_OBJECTID; + key.type = BTRFS_TEMPORARY_ITEM_KEY; + key.offset = new_csum_type; + ret = btrfs_insert_empty_item(trans, tree_root, &path, &key, 0); + btrfs_release_path(&path); + if (ret < 0) { + errno = -ret; + error("failed to insert csum change item: %m"); + btrfs_abort_transaction(trans, ret); + return ret; + } + btrfs_set_super_flags(fs_info->super_copy, + btrfs_super_flags(fs_info->super_copy) | + BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM); + ret = btrfs_commit_transaction(trans, tree_root); + if (ret < 0) { + errno = -ret; + error("failed to commit the initial transaction: %m"); + return ret; + } + return generate_new_data_csums_range(fs_info, 0, new_csum_type); +} + static int delete_old_data_csums(struct btrfs_fs_info *fs_info) { struct btrfs_root *csum_root = btrfs_csum_root(fs_info, 0); @@ -635,6 +645,152 @@ out: return ret; } +/* + * Get the first and last csum items which has @objectid as their objectid. + * + * This would be called to handle data csum resume, which may have both old + * and new csums co-exist in the same csum tree. + * + * Return >0 if there is no such EXTENT_CSUM with given @objectid. + * Return 0 if there is such EXTENT_CSUM and populate @first_ret and @last_ret. + * Return <0 for errors. + */ +static int get_csum_items_range(struct btrfs_fs_info *fs_info, + u64 objectid, u64 *first_ret, u64 *last_ret, + u32 *last_item_size) +{ + struct btrfs_root *csum_root = btrfs_csum_root(fs_info, 0); + struct btrfs_path path = { 0 }; + struct btrfs_key key; + int ret; + + key.objectid = objectid; + key.type = BTRFS_EXTENT_CSUM_KEY; + key.offset = 0; + + ret = btrfs_search_slot(NULL, csum_root, &key, &path, 0, 0); + if (ret < 0) { + errno = -ret; + error("failed to search csum tree: %m"); + return ret; + } + if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) { + ret = btrfs_next_leaf(csum_root, &path); + if (ret < 0) { + errno = -ret; + error("failed to search csum tree: %m"); + btrfs_release_path(&path); + return ret; + } + /* + * There is no next leaf, meaning we didn't find any + * csum item with given objectid. + */ + if (ret > 0) { + btrfs_release_path(&path); + return ret; + } + } + + btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); + btrfs_release_path(&path); + if (key.objectid != objectid) + return 1; + *first_ret = key.offset; + + key.objectid = objectid; + key.type = BTRFS_EXTENT_CSUM_KEY; + key.offset = (u64)-1; + + ret = btrfs_search_slot(NULL, csum_root, &key, &path, 0, 0); + if (ret < 0) { + errno = -ret; + error("failed to search csum tree: %m"); + return ret; + } + assert(ret > 0); + ret = btrfs_previous_item(csum_root, &path, objectid, + BTRFS_EXTENT_CSUM_KEY); + if (ret < 0) { + errno = -ret; + error("failed to search csum tree: %m"); + btrfs_release_path(&path); + return ret; + } + if (ret > 0) { + btrfs_release_path(&path); + return 1; + } + btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); + *last_item_size = btrfs_item_size(path.nodes[0], path.slots[0]); + btrfs_release_path(&path); + *last_ret = key.offset; + return 0; +} + +static int resume_data_csum_change(struct btrfs_fs_info *fs_info, + u16 new_csum_type) +{ + u64 old_csum_first; + u64 old_csum_last; + u64 new_csum_first; + u64 new_csum_last; + bool old_csum_found = false; + bool new_csum_found = false; + u32 old_last_size; + u32 new_last_size; + u64 resume_start; + int ret; + + ret = get_csum_items_range(fs_info, BTRFS_EXTENT_CSUM_OBJECTID, + &old_csum_first, &old_csum_last, + &old_last_size); + if (ret < 0) + return ret; + if (ret == 0) + old_csum_found = true; + ret = get_csum_items_range(fs_info, BTRFS_CSUM_CHANGE_OBJECTID, + &new_csum_first, &new_csum_last, + &new_last_size); + if (ret < 0) + return ret; + if (ret == 0) + new_csum_found = true; + + /* + * Both old and new csum exist, and new csum is only a subset of the + * old ones. + * + * This means we're still generating new data csums. + */ + if (old_csum_found && new_csum_found && old_csum_first <= new_csum_first && + old_csum_last >= new_csum_last) { + resume_start = new_csum_last + new_last_size / + btrfs_csum_type_size(new_csum_type) * + fs_info->sectorsize; + goto new_data_csums; + } + + /* Other cases are not yet supported. */ + return -EOPNOTSUPP; + +new_data_csums: + ret = generate_new_data_csums_range(fs_info, resume_start, new_csum_type); + if (ret < 0) { + errno = -ret; + error("failed to generate new data csums: %m"); + return ret; + } + ret = delete_old_data_csums(fs_info); + if (ret < 0) + return ret; + ret = change_csum_objectids(fs_info); + if (ret < 0) + return ret; + ret = change_meta_csums(fs_info, new_csum_type); + return ret; +} + static int resume_csum_change(struct btrfs_fs_info *fs_info, u16 new_csum_type) { const u64 super_flags = btrfs_super_flags(fs_info->super_copy); @@ -683,8 +839,12 @@ static int resume_csum_change(struct btrfs_fs_info *fs_info, u16 new_csum_type) } if (super_flags & BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM) { - error("resume on data checksum changing is not yet supported"); - return -EOPNOTSUPP; + ret = resume_data_csum_change(fs_info, new_csum_type); + if (ret < 0) { + errno = -ret; + error("failed to resume data checksum change: %m"); + } + return ret; } /* From patchwork Wed May 24 07:41:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13253409 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D481CC7EE2C for ; Wed, 24 May 2023 07:41:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239997AbjEXHl4 (ORCPT ); Wed, 24 May 2023 03:41:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239977AbjEXHlz (ORCPT ); Wed, 24 May 2023 03:41:55 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB2B49D for ; Wed, 24 May 2023 00:41:52 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 95C351F8B3 for ; Wed, 24 May 2023 07:41:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1684914111; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KESw63R7fH6sI9QpjFhSs5hImiXiJGJh+S1Z4Brh5uc=; b=cHYuZ6EAZZk3RzR3Es8Z824Mrw4ThwZSF+lXGxg5YNVkLYg44jZxpuoG/oMnfackd1pjtV RHjXCjaWWYskbQlyYswA30xWFTVmzTmHdUUXVY4njg/cQSwU2pA66/IZLIfN0ua/9eEWvU DkT35q5GLs5rPK+r6wizxzvpr7W6UGA= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id EBEDB13425 for ; Wed, 24 May 2023 07:41:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id +LApLb6/bWSiRQAAMHmgww (envelope-from ) for ; Wed, 24 May 2023 07:41:50 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 3/7] btrfs-progs: tune: implement resume support for csum tree without any new csum item Date: Wed, 24 May 2023 15:41:26 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org There are two possible situations where there is no new csum items at all: - We just inserted csum change item This means all csums are really old csum type, and we can start the conversion from the beginning, only need to skip the csum change item insert. - We finished data csum conversion but not yet started metadata conversion This means all csums are already new csum type, and we can resume by starting changing metadata csums. To distinguish the two cases, we need to read the first sector, and verify the data content against both csum types. If the csum matches with old csum type, we resume from generating new data csum. If the csum matches with new csum type, we resume from rewriting metadata csum. If the csum doesn't match either csum type, we have some big problems then. Signed-off-by: Qu Wenruo --- tune/change-csum.c | 105 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 3 deletions(-) diff --git a/tune/change-csum.c b/tune/change-csum.c index b95a4117b59b..05bddaa48d27 100644 --- a/tune/change-csum.c +++ b/tune/change-csum.c @@ -115,7 +115,8 @@ static int get_last_csum_bytenr(struct btrfs_fs_info *fs_info, u64 *result) static int read_verify_one_data_sector(struct btrfs_fs_info *fs_info, u64 logical, void *data_buf, - const void *old_csums) + const void *old_csums, u16 old_csum_type, + bool output_error) { const u32 sectorsize = fs_info->sectorsize; int num_copies = btrfs_num_copies(fs_info, logical, sectorsize); @@ -138,7 +139,7 @@ static int read_verify_one_data_sector(struct btrfs_fs_info *fs_info, if (memcmp(csum_has, old_csums, fs_info->csum_size) == 0) { found_good = true; break; - } else { + } else if (output_error){ char found[BTRFS_CSUM_STRING_LEN]; char want[BTRFS_CSUM_STRING_LEN]; @@ -168,7 +169,8 @@ static int generate_new_csum_range(struct btrfs_trans_handle *trans, for (u64 cur = logical; cur < logical + length; cur += sectorsize) { ret = read_verify_one_data_sector(fs_info, cur, buf, old_csums + - (cur - logical) / sectorsize * fs_info->csum_size); + (cur - logical) / sectorsize * fs_info->csum_size, + fs_info->csum_type, true); if (ret < 0) { error("failed to recover a good copy for data at logical %llu", @@ -532,8 +534,20 @@ static int change_meta_csums(struct btrfs_fs_info *fs_info, u16 new_csum_type) struct btrfs_root *extent_root = btrfs_extent_root(fs_info, 0); struct btrfs_path path = { 0 }; struct btrfs_key key; + u64 super_flags; int ret; + /* Re-set the super flags, this is for resume cases. */ + super_flags = btrfs_super_flags(fs_info->super_copy); + super_flags &= ~BTRFS_SUPER_FLAG_CHANGING_DATA_CSUM; + super_flags |= BTRFS_SUPER_FLAG_CHANGING_META_CSUM; + btrfs_set_super_flags(fs_info->super_copy, super_flags); + ret = write_all_supers(fs_info); + if (ret < 0) { + errno = -ret; + error("failed to update super flags: %m"); + } + /* * Disable metadata csum checks first, as we may hit tree blocks with * either old or new csums. @@ -728,6 +742,63 @@ static int get_csum_items_range(struct btrfs_fs_info *fs_info, return 0; } +/* + * Verify one data sector to determine which csum type matches the csum. + * + * Return >0 if the current csum type doesn't pass the check (including csum + * item too small compared to csum type). + * Return 0 if the current csum type passes the check. + * Return <0 for other errors. + */ +static int determine_csum_type(struct btrfs_fs_info *fs_info, u64 logical, + u16 csum_type) +{ + struct btrfs_root *csum_root = btrfs_csum_root(fs_info, logical); + struct btrfs_path path = { 0 }; + struct btrfs_key key; + u16 csum_size = btrfs_csum_type_size(csum_type); + u8 csum_expected[BTRFS_CSUM_SIZE]; + void *buf; + int ret; + + key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; + key.type = BTRFS_EXTENT_CSUM_KEY; + key.offset = logical; + + ret = btrfs_search_slot(NULL, csum_root, &key, &path, 0, 0); + if (ret > 0) + ret = -ENOENT; + if (ret < 0) { + errno = -ret; + error("failed to search csum tree: %m"); + btrfs_release_path(&path); + return ret; + } + + /* + * The csum item size is smaller than expected csum size, no + * more need to check. + */ + if (btrfs_item_size(path.nodes[0], path.slots[0]) < csum_size) { + btrfs_release_path(&path); + return 1; + } + read_extent_buffer(path.nodes[0], csum_expected, + btrfs_item_ptr_offset(path.nodes[0], path.slots[0]), + csum_size); + btrfs_release_path(&path); + + buf = malloc(fs_info->sectorsize); + if (!buf) + return -ENOMEM; + ret = read_verify_one_data_sector(fs_info, logical, buf, csum_expected, + csum_type, false); + if (ret < 0) + ret = 1; + free(buf); + return ret; +} + static int resume_data_csum_change(struct btrfs_fs_info *fs_info, u16 new_csum_type) { @@ -757,6 +828,33 @@ static int resume_data_csum_change(struct btrfs_fs_info *fs_info, if (ret == 0) new_csum_found = true; + /* + * Only old csums exists. This can be one of the two cases: + * - Only the csum change item inserted, no new csum generated. + * - All data csum is converted to the new type. + * + * Here we need to check if the csum item is in old or new type. + */ + if (old_csum_found && !new_csum_found) { + ret = determine_csum_type(fs_info, old_csum_first, fs_info->csum_type); + if (ret == 0) { + /* All old data csums, restart generation. */ + resume_start = 0; + goto new_data_csums; + } + ret = determine_csum_type(fs_info, old_csum_first, new_csum_type); + if (ret == 0) { + /* + * All new data csums, just go metadata csum change, which + * would drop the CHANGING_DATA_CSUM flag for us. + */ + goto new_meta_csum; + } + error("The data checksum for logical %llu doesn't match either old or new csum type, unable to resume", + old_csum_first); + return -EUCLEAN; + } + /* * Both old and new csum exist, and new csum is only a subset of the * old ones. @@ -787,6 +885,7 @@ new_data_csums: ret = change_csum_objectids(fs_info); if (ret < 0) return ret; +new_meta_csum: ret = change_meta_csums(fs_info, new_csum_type); return ret; } From patchwork Wed May 24 07:41:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13253410 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A964EC7EE23 for ; Wed, 24 May 2023 07:41:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239999AbjEXHl5 (ORCPT ); Wed, 24 May 2023 03:41:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239982AbjEXHlz (ORCPT ); Wed, 24 May 2023 03:41:55 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F31AD8F for ; Wed, 24 May 2023 00:41:53 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id ACEB522435 for ; Wed, 24 May 2023 07:41:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1684914112; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vIAw/2Qb+ZXZkszKOZh52aoZ9E4wSHooqysN4RC4IBs=; b=jcfIS0LzIxvjkh+3AfIfReNLU6ANRThYKEBcEFaa+CtU6uyw54j4vucbq9B9m8DR3fUwE+ i+FwA1KuUYGWPdgvhgJb2kUrvwFlLzAmRg/bfFU6ThRI6U5xG/YjmqvRLiM2c8IKswXlcs kdVXHlZSBoetcw9I3CzY31CwnCPigeA= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0374C13425 for ; Wed, 24 May 2023 07:41:51 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 6OkHML+/bWSiRQAAMHmgww (envelope-from ) for ; Wed, 24 May 2023 07:41:51 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 4/7] btrfs-progs: tune: implement resume support for empty csum tree Date: Wed, 24 May 2023 15:41:27 +0800 Message-Id: <6a45bc14a406c86552e6446f9e78dae3489f26b5.1684913599.git.wqu@suse.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org We have a very rare chance to hit a fs with empty csum tree but still has CHANGING_DATA_CSUM flag. The window is very small, but it's still possible, so handle it by jumping directly to metadata csum change. Signed-off-by: Qu Wenruo --- tune/change-csum.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tune/change-csum.c b/tune/change-csum.c index 05bddaa48d27..aef3b05a0d9d 100644 --- a/tune/change-csum.c +++ b/tune/change-csum.c @@ -828,6 +828,13 @@ static int resume_data_csum_change(struct btrfs_fs_info *fs_info, if (ret == 0) new_csum_found = true; + /* + * No csum item found at all, this fs has empty csum tree. + * Just go metadata change. + */ + if (!old_csum_found && !new_csum_found) + goto new_meta_csum; + /* * Only old csums exists. This can be one of the two cases: * - Only the csum change item inserted, no new csum generated. From patchwork Wed May 24 07:41:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13253411 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D193BC77B7A for ; Wed, 24 May 2023 07:41:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240003AbjEXHl6 (ORCPT ); Wed, 24 May 2023 03:41:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239996AbjEXHl4 (ORCPT ); Wed, 24 May 2023 03:41:56 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2C8990 for ; Wed, 24 May 2023 00:41:54 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id AC7221F750 for ; Wed, 24 May 2023 07:41:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1684914113; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TwCuV9K7Q5EmcHwKTeEzeULyQ/1jtaO4uobNHQ/IGA4=; b=iqe2oG8+D4bug0OI9oIlXrR5OoNJgXMhvHBQkOKf3GD3dQIPvaHg/N1KliyeSSY32P6872 WU4JDBdiECveInO6e6+V86YK7EJ6NzbALzbStfYZrOQSo1RPuLhUvUPelbVm3uObi/8JY2 McBCI40O18qYSvGqpyyVMVcoRUZcnHQ= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0E4FA13425 for ; Wed, 24 May 2023 07:41:52 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id MJSuMsC/bWSiRQAAMHmgww (envelope-from ) for ; Wed, 24 May 2023 07:41:52 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 5/7] btrfs-progs: tune: implement resume support for half deleted old csums Date: Wed, 24 May 2023 15:41:28 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If the csum conversion is interrupted when old csums are being deleted, we should resume by continue deleting the old csums. The function delete_old_data_csums() can handle half deleted cases already. Signed-off-by: Qu Wenruo --- tune/change-csum.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tune/change-csum.c b/tune/change-csum.c index aef3b05a0d9d..2ec2d6cc5271 100644 --- a/tune/change-csum.c +++ b/tune/change-csum.c @@ -876,6 +876,15 @@ static int resume_data_csum_change(struct btrfs_fs_info *fs_info, goto new_data_csums; } + /* + * Both old and new csum exist, and old csum is a subset of the new ones. + * + * This means we're deleting the old csums. + */ + if (old_csum_found && new_csum_found && new_csum_first <= old_csum_first && + new_csum_last >= old_csum_last) + goto delete_old; + /* Other cases are not yet supported. */ return -EOPNOTSUPP; @@ -886,6 +895,7 @@ new_data_csums: error("failed to generate new data csums: %m"); return ret; } +delete_old: ret = delete_old_data_csums(fs_info); if (ret < 0) return ret; From patchwork Wed May 24 07:41:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13253412 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD82AC77B7C for ; Wed, 24 May 2023 07:42:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240004AbjEXHl7 (ORCPT ); Wed, 24 May 2023 03:41:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240002AbjEXHl5 (ORCPT ); Wed, 24 May 2023 03:41:57 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 231628F for ; Wed, 24 May 2023 00:41:56 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id B6BD51F8AF for ; Wed, 24 May 2023 07:41:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1684914114; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0vWXCQxtBz9MeG5eAvn5ZK7HbiFICHkqVFgk/FisX2s=; b=DLCk2zNnz75x90do85JsdkiD9WZifpSzYo1fbiOPYN8h/GmkJHsgMGF7drMzZdaWYItxqF /RvPXHIY6AfgXl7wZKDURJTnVK6ocROv8gQZMqwa2Cub43Mn5OjowRLlp7h6H6KaS9Z7x0 aCZSxNJzECZaJWSgns9bNcuFXsOSL9s= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 18B9013425 for ; Wed, 24 May 2023 07:41:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id AAw3NcG/bWSiRQAAMHmgww (envelope-from ) for ; Wed, 24 May 2023 07:41:53 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 6/7] btrfs-progs: tune: implement resume support for data csum objectid change Date: Wed, 24 May 2023 15:41:29 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org When the csum conversion is interrupted when changing data csum objectid, we should just resume the objectid conversion. This situation can be detected by comparing the old and new csum items. They should both exist but doesn't intersect (interrupted halfway), or only new csum items exist (interrupted after we have deleted old csums). Signed-off-by: Qu Wenruo --- tune/change-csum.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tune/change-csum.c b/tune/change-csum.c index 2ec2d6cc5271..dad39c3ec854 100644 --- a/tune/change-csum.c +++ b/tune/change-csum.c @@ -885,8 +885,26 @@ static int resume_data_csum_change(struct btrfs_fs_info *fs_info, new_csum_last >= old_csum_last) goto delete_old; - /* Other cases are not yet supported. */ - return -EOPNOTSUPP; + /* + * Both csums exist but not covering each other, or only new csum exists. + * + * This means we have already deleted all the old csums, is going to or + * have already started objectid change. + */ + if ((old_csum_found && new_csum_found && old_csum_last <= new_csum_first) && + (!old_csum_found && new_csum_found)) + goto change; + + /* The remaining cases should not be possible. */ + error("unexpected resume condition:"); + error("old csum found=%d start=%llu last=%llu new csum found=%d start=%llu last=%llu", + old_csum_found, + old_csum_found ? old_csum_first : 0, + old_csum_found ? old_csum_last : 0, + new_csum_found, + new_csum_found ? new_csum_first : 0, + new_csum_found ? new_csum_last : 0); + return -EUCLEAN; new_data_csums: ret = generate_new_data_csums_range(fs_info, resume_start, new_csum_type); @@ -899,6 +917,7 @@ delete_old: ret = delete_old_data_csums(fs_info); if (ret < 0) return ret; +change: ret = change_csum_objectids(fs_info); if (ret < 0) return ret; From patchwork Wed May 24 07:41:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13253413 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6BB49C7EE2C for ; Wed, 24 May 2023 07:42:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240005AbjEXHmA (ORCPT ); Wed, 24 May 2023 03:42:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239982AbjEXHl6 (ORCPT ); Wed, 24 May 2023 03:41:58 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1758890 for ; Wed, 24 May 2023 00:41:57 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id C002222435 for ; Wed, 24 May 2023 07:41:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1684914115; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IhdbGigzGey0rT99VFdIppWATdfpzYKqxl+k/tEYVXQ=; b=jyWvdH73zDzbgWeMDZmwt66U9wh07gC7S/CZaoPMlHoodtz0Gc1tYCDqE93w815zCkqG1A ma3AOWFJR4jFIQOdLQomxoY4sv3PY04oPoJMDqBws0puR+nk1YFfa93SjinID0W3Q7ouXN EkBuEiJKbj32P49xCIIewUvNuTjRbtI= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 2351D13425 for ; Wed, 24 May 2023 07:41:54 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id +OfJN8K/bWSiRQAAMHmgww (envelope-from ) for ; Wed, 24 May 2023 07:41:54 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 7/7] btrfs-progs: tune: reject csum change if the fs is already using the target csum type Date: Wed, 24 May 2023 15:41:30 +0800 Message-Id: <9fbd94b5285d0d71fd6c092bdd06167f3482b98b.1684913599.git.wqu@suse.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Currently "btrfstune --csum" allows us to change the csum to the same one, this is good for testing but not good for end users, as if the end user interrupts it, they have to resume the change (even it's to the same csum type) until it finished, or kernel would reject such fs. Furthermore, we never change the super block csum type until we completely changed the csum type, thus for resume cases, the fs would still show up as using the old csum type thus won't cause any problem resuming. So here we just reject the csum conversion if the target csum type is the same as the existing csum type. Signed-off-by: Qu Wenruo --- tune/change-csum.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tune/change-csum.c b/tune/change-csum.c index dad39c3ec854..ef3d663e038e 100644 --- a/tune/change-csum.c +++ b/tune/change-csum.c @@ -29,7 +29,7 @@ #include "common/utils.h" #include "tune/tune.h" -static int check_csum_change_requreiment(struct btrfs_fs_info *fs_info) +static int check_csum_change_requreiment(struct btrfs_fs_info *fs_info, u16 new_csum_type) { struct btrfs_root *tree_root = fs_info->tree_root; struct btrfs_root *dev_root = fs_info->dev_root; @@ -75,6 +75,12 @@ static int check_csum_change_requreiment(struct btrfs_fs_info *fs_info) error("running dev-replace detected, please finish or cancel it."); return -EINVAL; } + + if (fs_info->csum_type == new_csum_type) { + error("the fs is already using csum type %s (%u)", + btrfs_super_csum_name(new_csum_type), new_csum_type); + return -EINVAL; + } return 0; } @@ -1001,7 +1007,7 @@ int btrfs_change_csum_type(struct btrfs_fs_info *fs_info, u16 new_csum_type) int ret; /* Phase 0, check conflicting features. */ - ret = check_csum_change_requreiment(fs_info); + ret = check_csum_change_requreiment(fs_info, new_csum_type); if (ret < 0) return ret;