From patchwork Tue Mar 11 10:29:09 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Shilong X-Patchwork-Id: 3810801 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EDDB0BF540 for ; Tue, 11 Mar 2014 10:31:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0FD6B2035D for ; Tue, 11 Mar 2014 10:31:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4F2B4202A1 for ; Tue, 11 Mar 2014 10:31:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753684AbaCKKbP (ORCPT ); Tue, 11 Mar 2014 06:31:15 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:22248 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1753432AbaCKKbN (ORCPT ); Tue, 11 Mar 2014 06:31:13 -0400 X-IronPort-AV: E=Sophos;i="4.97,629,1389715200"; d="scan'208";a="9678363" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 11 Mar 2014 18:27:19 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s2BAV9Qh024881 for ; Tue, 11 Mar 2014 18:31:11 +0800 Received: from wangs.fnst.cn.fujitsu.com ([10.167.226.104]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2014031118282658-554763 ; Tue, 11 Mar 2014 18:28:26 +0800 From: Wang Shilong To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/2] Btrfs-progs: mkfs: make sure we can deal with hard links with -r option Date: Tue, 11 Mar 2014 18:29:09 +0800 Message-Id: <1394533749-3138-2-git-send-email-wangsl.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1394533749-3138-1-git-send-email-wangsl.fnst@cn.fujitsu.com> References: <1394533749-3138-1-git-send-email-wangsl.fnst@cn.fujitsu.com> X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/03/11 18:28:26, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/03/11 18:28:28, Serialize complete at 2014/03/11 18:28:28 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Steps to reproduce: # mkdir -p /tmp/test # touch /tmp/test/file # ln /tmp/test/file /tmp/test/hardlinks # mkfs.btrfs -f /dev/sda13 -r /tmp/test # btrfs check /dev/sda13 To deal with hard link, we must deal with inode with same inode id rather than increase inode id by ourselves. Signed-off-by: Wang Shilong --- mkfs.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/mkfs.c b/mkfs.c index 2f7dfef..b9385bc 100644 --- a/mkfs.c +++ b/mkfs.c @@ -380,7 +380,10 @@ static int add_directory_items(struct btrfs_trans_handle *trans, ret = btrfs_insert_dir_item(trans, root, name, name_len, parent_inum, &location, filetype, index_cnt); - + if (ret) + return ret; + ret = btrfs_insert_inode_ref(trans, root, name, name_len, + objectid, parent_inum, index_cnt); *dir_index_cnt = index_cnt; index_cnt++; @@ -493,9 +496,7 @@ static int add_inode_items(struct btrfs_trans_handle *trans, struct btrfs_inode_item btrfs_inode; u64 objectid; u64 inode_size = 0; - int name_len; - name_len = strlen(name); fill_inode_item(trans, root, &btrfs_inode, st); objectid = self_objectid; @@ -509,16 +510,8 @@ static int add_inode_items(struct btrfs_trans_handle *trans, btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY); ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode); - if (ret) - goto fail; - - ret = btrfs_insert_inode_ref(trans, root, name, name_len, - objectid, parent_inum, dir_index_cnt); - if (ret) - goto fail; *inode_ret = btrfs_inode; -fail: return ret; } @@ -826,7 +819,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans, goto fail; } - cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID; + cur_inum = st.st_ino; ret = add_directory_items(trans, root, cur_inum, parent_inum, cur_file->d_name, @@ -840,6 +833,10 @@ static int traverse_directory(struct btrfs_trans_handle *trans, cur_file->d_name, cur_inum, parent_inum, dir_index_cnt, &cur_inode); + if (ret == -EEXIST) { + BUG_ON(st.st_nlink <= 1); + continue; + } if (ret) { fprintf(stderr, "add_inode_items failed\n"); goto fail;