From patchwork Mon May 7 03:10:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Fengqi X-Patchwork-Id: 10383169 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 1F4C96053A for ; Mon, 7 May 2018 03:11:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 103AD28478 for ; Mon, 7 May 2018 03:11:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0509228B21; Mon, 7 May 2018 03:11:03 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, 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 7803328478 for ; Mon, 7 May 2018 03:11:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751981AbeEGDKu (ORCPT ); Sun, 6 May 2018 23:10:50 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:45920 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751918AbeEGDKp (ORCPT ); Sun, 6 May 2018 23:10:45 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="39622265" Received: from bogon (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 07 May 2018 11:10:42 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 835044B34D53 for ; Mon, 7 May 2018 11:10:43 +0800 (CST) Received: from fnst.localdomain (10.167.226.155) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.361.1; Mon, 7 May 2018 11:10:44 +0800 From: Lu Fengqi To: Subject: [PATCH v3 03/10] btrfs-progs: use btrfs_find_free_dir_index to find free inode index Date: Mon, 7 May 2018 11:10:26 +0800 Message-ID: <20180507031033.4125-4-lufq.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180507031033.4125-1-lufq.fnst@cn.fujitsu.com> References: <20180507031033.4125-1-lufq.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.155] X-yoursite-MailScanner-ID: 835044B34D53.A9FAA X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: lufq.fnst@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 Since we have an existing function to find free inode index, we can reuse it here. Signed-off-by: Lu Fengqi Reviewed-by: Qu Wenruo --- V3: add Reviewed-by from Qu Wenruo. inode.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/inode.c b/inode.c index f9b72533f3a9..03b8be63eb0a 100644 --- a/inode.c +++ b/inode.c @@ -629,7 +629,7 @@ int btrfs_link_subvol(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_root_item root_item; struct extent_buffer *leaf; struct btrfs_key key; - u64 index = 2; + u64 index; u64 offset; char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */ int len; @@ -640,28 +640,15 @@ int btrfs_link_subvol(struct btrfs_trans_handle *trans, struct btrfs_root *root, if (len == 0 || len > BTRFS_NAME_LEN) return -EINVAL; - /* find the free dir_index */ - btrfs_init_path(&path); - key.objectid = dirid; - key.type = BTRFS_DIR_INDEX_KEY; - key.offset = (u64)-1; - - ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); - if (ret <= 0) { - error("search for DIR_INDEX dirid %llu failed: %d", - (unsigned long long)dirid, ret); + /* add the dir_item/dir_index */ + ret = btrfs_find_free_dir_index(root, dirid, &index); + if (ret < 0) { + error("unable to find free dir index dirid %llu failed: %d", + (unsigned long long)dirid, ret); goto fail; } - if (path.slots[0] > 0) { - path.slots[0]--; - btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); - if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY) - index = key.offset + 1; - } - btrfs_release_path(&path); - - /* add the dir_item/dir_index */ + btrfs_init_path(&path); key.objectid = dirid; key.offset = 0; key.type = BTRFS_INODE_ITEM_KEY;