From patchwork Sat May 18 16:37:45 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Shilong X-Patchwork-Id: 2589071 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 7059FDF2E5 for ; Sat, 18 May 2013 16:38:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752494Ab3ERQiO (ORCPT ); Sat, 18 May 2013 12:38:14 -0400 Received: from mail-pa0-f50.google.com ([209.85.220.50]:57893 "EHLO mail-pa0-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752399Ab3ERQiN (ORCPT ); Sat, 18 May 2013 12:38:13 -0400 Received: by mail-pa0-f50.google.com with SMTP id fb10so4425502pad.23 for ; Sat, 18 May 2013 09:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:subject:date:message-id:x-mailer; bh=8Nv5x0GcVRTIKdH00PByvcoxKaYtzHXevH3Qq4FanfU=; b=kfM1Bz3toGur7gcaUaXbY1brEg3/3WzwlgS5tL3eOz6CXeKUm25tnnE7jKpoPOXAuX DRH7LIrbiKW+r74/dTXY67k0l3Y02pDP4JP4fDiuZaoGkRYVlaWlyxa0hy1+1tnBI/kT wwV4oTc2JmULu6xtq6D/ZJPogo2L/H1oRvIn+2+moj6vzpHD3lbOGmT+iy934f9aaGRW nhgx3tZ02hAPlAPqK1SpPfYPA46qAOed71UqfFThTXDhghUuC6xYffbMoKb+92m7NbHs i42xk0S/M4zn8dlYaUbq3yZdCSEjrzG8Y0UlRqDVKcWVyVmR3Y/Vyo8XSO+6rNVWxGWf +MRQ== X-Received: by 10.68.237.106 with SMTP id vb10mr47073443pbc.131.1368895093046; Sat, 18 May 2013 09:38:13 -0700 (PDT) Received: from localhost.localdomain.localdomain ([112.22.172.46]) by mx.google.com with ESMTPSA id v7sm16223527pbq.32.2013.05.18.09.38.11 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Sat, 18 May 2013 09:38:12 -0700 (PDT) From: Wang Shilong To: linux-btrfs@vger.kernel.org Subject: [PATCH V2] Btrfs-progs: fix missing recow roots when making btrfs filesystem Date: Sun, 19 May 2013 00:37:45 +0800 Message-Id: <1368895065-1590-1-git-send-email-wangshilong1991@gmail.com> X-Mailer: git-send-email 1.7.11.7 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Wang Shilong When making btrfs filesystem. we firstly write root leaf to specified filed, and then we recow the root. If we don't recow, some trees are not in the correct block group. Steps to reproduce: dd if=/dev/zero of=test.img bs=1M count=100 mkfs.btrfs -f test.img btrfs-debug-tree test.img extent tree key (EXTENT_TREE ROOT_ITEM 0) leaf 4210688 items 10 free space 3349 generation 4 owner 2 fs uuid 2e08fd93-f24d-4f44-a226-e2116fcd544f chunk uuid dc482988-6246-46ce-9329-68bcf6d3683c item 0 key (0 BLOCK_GROUP_ITEM 4194304) itemoff 3971 itemsize 24 block group used 12288 chunk_objectid 256 flags 2 [..snip..] item 3 key (1138688 EXTENT_ITEM 4096) itemoff 3827 itemsize 42 extent refs 1 gen 1 flags 2 tree block key (0 UNKNOWN.0 0) level 0 item 4 key (1138688 TREE_BLOCK_REF 7) itemoff 3827 itemsize 0 tree block backref [..snip..] checksum tree key (CSUM_TREE ROOT_ITEM 0) leaf 1138688 items 0 free space 3995 generation 1 owner 7 fs uuid 2e08fd93-f24d-4f44-a226-e2116fcd544f chunk uuid dc482988-6246-46ce-9329-68bcf6d3683c For the above example, csum root leaf comes into system block group which is wrong,csum root leaf should be in metadata block group. Signed-off-by: Wang Shilong Reviewed-by: Miao Xie --- v1->v2: wrap the repeated code that addressed by David --- mkfs.c | 56 +++++++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/mkfs.c b/mkfs.c index 7ff60e5..b412b7e 100644 --- a/mkfs.c +++ b/mkfs.c @@ -145,45 +145,31 @@ err: return ret; } -static int recow_roots(struct btrfs_trans_handle *trans, - struct btrfs_root *root) +static void __recow_root(struct btrfs_trans_handle *trans, + struct btrfs_root *root) { int ret; struct extent_buffer *tmp; - struct btrfs_fs_info *info = root->fs_info; - - ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node, - NULL, 0, &tmp, 0, 0); - BUG_ON(ret); - free_extent_buffer(tmp); - - ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node, - NULL, 0, &tmp, 0, 0); - BUG_ON(ret); - free_extent_buffer(tmp); - - ret = __btrfs_cow_block(trans, info->extent_root, - info->extent_root->node, NULL, 0, &tmp, 0, 0); - BUG_ON(ret); - free_extent_buffer(tmp); - - ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node, - NULL, 0, &tmp, 0, 0); - BUG_ON(ret); - free_extent_buffer(tmp); + if (trans->transid != btrfs_root_generation(&root->root_item)) { + ret = __btrfs_cow_block(trans, root, root->node, + NULL, 0, &tmp, 0, 0); + BUG_ON(ret); + free_extent_buffer(tmp); + } +} - ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node, - NULL, 0, &tmp, 0, 0); - BUG_ON(ret); - free_extent_buffer(tmp); - - ret = __btrfs_cow_block(trans, info->csum_root, info->csum_root->node, - NULL, 0, &tmp, 0, 0); - BUG_ON(ret); - free_extent_buffer(tmp); +static void recow_roots(struct btrfs_trans_handle *trans, + struct btrfs_root *root) +{ + struct btrfs_fs_info *info = root->fs_info; - return 0; + __recow_root(trans, info->fs_root); + __recow_root(trans, info->tree_root); + __recow_root(trans, info->extent_root); + __recow_root(trans, info->chunk_root); + __recow_root(trans, info->dev_root); + __recow_root(trans, info->csum_root); } static int create_one_raid_group(struct btrfs_trans_handle *trans, @@ -281,8 +267,6 @@ static int create_raid_groups(struct btrfs_trans_handle *trans, (allowed & metadata_profile)); BUG_ON(ret); - ret = recow_roots(trans, root); - BUG_ON(ret); } if (!mixed && num_devices > 1 && (allowed & data_profile)) { ret = create_one_raid_group(trans, root, @@ -290,6 +274,8 @@ static int create_raid_groups(struct btrfs_trans_handle *trans, (allowed & data_profile)); BUG_ON(ret); } + recow_roots(trans, root); + return 0; }