From patchwork Thu May 18 02:10:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13246045 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 4C179C77B75 for ; Thu, 18 May 2023 02:11:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229854AbjERCLT (ORCPT ); Wed, 17 May 2023 22:11:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229824AbjERCLM (ORCPT ); Wed, 17 May 2023 22:11:12 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1B4F3AAF for ; Wed, 17 May 2023 19:11:10 -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 8A5B21F88D for ; Thu, 18 May 2023 02:11:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1684375869; 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=khM5rjNMvZXQdcEyD6GtQ5FUuN2k5B41GbOI6BglZCs=; b=tXOzQXw3IXTrVCCNrBz46I1avs5LijAPKp4CaWIl3ZHQ57pBP73lNsT7/qTvK9GM/CQiBG y+UCxCcGcdM9ZRKXSRW/NgQL2w+yPfKo9uXr3fzjR1i3Z5GH7zAjUx0ywCOmErbtZpJRmZ 53zxUf+q4X9pe8CadYOtKIbjN4PF93Y= 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 DA5A61332D for ; Thu, 18 May 2023 02:11:08 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id YMJkKDyJZWRVJAAAMHmgww (envelope-from ) for ; Thu, 18 May 2023 02:11:08 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH v2 5/7] btrfs-progs: tune: add the ability to delete old data csums Date: Thu, 18 May 2023 10:10:43 +0800 Message-Id: <8a6585cd2ca07e9a85d91e14b431fb20002ed4b8.1684375729.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 The new helper function, delete_old_data_csums(), would delete the old data csums while keep the new one untouched. Since the new data csums have a key objectid (-13) smaller than the old data csums (-10), we can safely delete from the tail of the btree. Signed-off-by: Qu Wenruo --- tune/change-csum.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tune/change-csum.c b/tune/change-csum.c index a30d142c1600..61368ddf34b9 100644 --- a/tune/change-csum.c +++ b/tune/change-csum.c @@ -326,6 +326,68 @@ out: return ret; } +static int delete_old_data_csums(struct btrfs_fs_info *fs_info) +{ + struct btrfs_root *csum_root = btrfs_csum_root(fs_info, 0); + struct btrfs_trans_handle *trans; + struct btrfs_path path = { 0 }; + struct btrfs_key last_key; + int ret; + + last_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; + last_key.type = BTRFS_EXTENT_CSUM_KEY; + last_key.offset = (u64)-1; + + trans = btrfs_start_transaction(csum_root, 1); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + errno = -ret; + error("failed to start transaction to delete old data csums: %m"); + return ret; + } + while (true) { + int start_slot; + int nr; + + ret = btrfs_search_slot(trans, csum_root, &last_key, &path, -1, 1); + + nr = btrfs_header_nritems(path.nodes[0]); + /* No item left (empty csum tree), exit. */ + if (!nr) + break; + for (start_slot = 0; start_slot < nr; start_slot++) { + struct btrfs_key found_key; + + btrfs_item_key_to_cpu(path.nodes[0], &found_key, start_slot); + /* Break from the for loop, we found the first old csum. */ + if (found_key.objectid == BTRFS_EXTENT_CSUM_OBJECTID) + break; + } + /* No more old csum item detected, exit. */ + if (start_slot == nr) + break; + + /* Delete items starting from @start_slot to the end. */ + ret = btrfs_del_items(trans, csum_root, &path, start_slot, + nr - start_slot); + if (ret < 0) { + errno = -ret; + error("failed to delete items: %m"); + break; + } + btrfs_release_path(&path); + } + btrfs_release_path(&path); + if (ret < 0) + btrfs_abort_transaction(trans, ret); + ret = btrfs_commit_transaction(trans, csum_root); + if (ret < 0) { + errno = -ret; + error("failed to commit transaction after deleting the old data csums: %m"); + } + return ret; +} + int btrfs_change_csum_type(struct btrfs_fs_info *fs_info, u16 new_csum_type) { int ret; @@ -350,6 +412,9 @@ int btrfs_change_csum_type(struct btrfs_fs_info *fs_info, u16 new_csum_type) } /* Phase 2, delete the old data csums. */ + ret = delete_old_data_csums(fs_info); + if (ret < 0) + return ret; /* Phase 3, change the new csum key objectid */