From patchwork Wed Jun 1 08:22:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9146341 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 A6ADF60751 for ; Wed, 1 Jun 2016 08:22:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 953CD2040D for ; Wed, 1 Jun 2016 08:22:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 89FC52654B; Wed, 1 Jun 2016 08:22:46 +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,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 1952D2040D for ; Wed, 1 Jun 2016 08:22:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751718AbcFAIWl (ORCPT ); Wed, 1 Jun 2016 04:22:41 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:1901 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1750998AbcFAIWj (ORCPT ); Wed, 1 Jun 2016 04:22:39 -0400 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="579661" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 01 Jun 2016 16:22:33 +0800 Received: from adam-work.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id 372CB4056403; Wed, 1 Jun 2016 16:22:28 +0800 (CST) From: Qu Wenruo To: dsterba@suse.com, linux-btrfs@vger.kernel.org Subject: [PATCH] btrfs-progs: convert: Fix bugs in backup superblock migration Date: Wed, 1 Jun 2016 16:22:26 +0800 Message-Id: <20160601082226.24166-1-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.8.3 MIME-Version: 1.0 X-yoursite-MailScanner-ID: 372CB4056403.AB7BF X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com 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 New convert has several bugs with backup superblock migration 1) Backup superblocks are not migrated due to bad judgement Two wrong judgement cause backup superblocks are not migrated at all 2) Converted ext* image doesn't keep hole for backup superblocks Since we are creating file extents according to tmp_used, which has wiped out backup superblock ranges. In that case, later superblock migration will fail, since migration will insert file extent range into ext* image. Fix above bugs will make convert on ext2 image filled about 100M data successful. Reported-by: Satoru Takeuchi Signed-off-by: Qu Wenruo --- v2: Fix a bug that range judgement misses one possible branch. Which leads to overflap file extent for converted btrfs. Special test cases are under way --- btrfs-convert.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index 30dd6e8..42d646c 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -1263,12 +1263,46 @@ static int create_image_file_range(struct btrfs_trans_handle *trans, struct btrfs_block_group_cache *bg_cache; u64 len = *ret_len; u64 disk_bytenr; + int i; int ret; BUG_ON(bytenr != round_down(bytenr, root->sectorsize)); BUG_ON(len != round_down(len, root->sectorsize)); len = min_t(u64, len, BTRFS_MAX_EXTENT_SIZE); + /* + * Skip sb ranges first + * [0, 1M), [sb_offset(1), +64K), [sb_offset(2), +64K]. + * + * Or we will insert a hole into current image file, and later + * migrate block will fail as there is already a file extent. + */ + if (bytenr < 1024 * 1024) { + *ret_len = 1024 * 1024 - bytenr; + return 0; + } + for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { + u64 cur = btrfs_sb_offset(i); + /* + * |--reserved--| + * |----range-------| + * May still need to go through file extent inserts + */ + if (bytenr < cur && bytenr + len >= cur) { + len = min_t(u64, len, cur - bytenr); + break; + } + /* + * |--reserved--| + * |---range---| + * Drop out, no need to insert anything + */ + if (bytenr >= cur && bytenr < cur + BTRFS_STRIPE_LEN) { + *ret_len = cur + BTRFS_STRIPE_LEN - bytenr; + return 0; + } + } + cache = search_cache_extent(used, bytenr); if (cache) { if (cache->start <= bytenr) { @@ -1425,7 +1459,7 @@ static int migrate_reserved_ranges(struct btrfs_trans_handle *trans, /* second sb(fisrt sb is included in 0~1M) */ cur_off = btrfs_sb_offset(1); cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off; - if (cur_off < total_bytes) + if (cur_off > total_bytes) return ret; ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino, cur_off, cur_len, datacsum); @@ -1435,7 +1469,7 @@ static int migrate_reserved_ranges(struct btrfs_trans_handle *trans, /* Last sb */ cur_off = btrfs_sb_offset(2); cur_len = min(total_bytes, cur_off + BTRFS_STRIPE_LEN) - cur_off; - if (cur_off < total_bytes) + if (cur_off > total_bytes) return ret; ret = migrate_one_reserved_range(trans, root, used, inode, fd, ino, cur_off, cur_len, datacsum);