From patchwork Fri Oct 20 01:59:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 10018583 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 9537660234 for ; Fri, 20 Oct 2017 01:59:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8658928710 for ; Fri, 20 Oct 2017 01:59:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B7BA287AC; Fri, 20 Oct 2017 01:59:33 +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 0761828710 for ; Fri, 20 Oct 2017 01:59:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751872AbdJTB70 (ORCPT ); Thu, 19 Oct 2017 21:59:26 -0400 Received: from prv3-mh.provo.novell.com ([137.65.250.26]:59647 "EHLO prv3-mh.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751769AbdJTB7R (ORCPT ); Thu, 19 Oct 2017 21:59:17 -0400 Received: from adam-pc.lan (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (NOT encrypted); Thu, 19 Oct 2017 19:59:14 -0600 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz Subject: [PATCH 4/7] btrfs-progs: mkfs/rootdir: Introduce function to get end position of last device extent Date: Fri, 20 Oct 2017 09:59:04 +0800 Message-Id: <20171020015907.25430-5-wqu@suse.com> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171020015907.25430-1-wqu@suse.com> References: <20171020015907.25430-1-wqu@suse.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 Useful for later 'mkfs.btrfs --rootdir' shrink support. Signed-off-by: Qu Wenruo --- mkfs/rootdir.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/mkfs/rootdir.c b/mkfs/rootdir.c index 99022afaa030..1ca37996a3b3 100644 --- a/mkfs/rootdir.c +++ b/mkfs/rootdir.c @@ -26,6 +26,7 @@ #include #include #include "ctree.h" +#include "volumes.h" #include "internal.h" #include "disk-io.h" #include "messages.h" @@ -778,3 +779,45 @@ u64 btrfs_mkfs_size_dir(const char *dir_name, u32 sectorsize, u64 min_dev_size, total_size = data_chunk_size + meta_chunk_size + min_dev_size; return total_size; } + +/* + * Get the end position of the last device extent for given @devid; + * @size_ret is exclsuive (means it should be aligned to sectorsize) + */ +static int get_device_extent_end(struct btrfs_fs_info *fs_info, + u64 devid, u64 *size_ret) +{ + struct btrfs_root *dev_root = fs_info->dev_root; + struct btrfs_key key; + struct btrfs_path path; + struct btrfs_dev_extent *de; + int ret; + + key.objectid = devid; + key.type = BTRFS_DEV_EXTENT_KEY; + key.offset = (u64)-1; + + btrfs_init_path(&path); + ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0); + /* Not really possible */ + BUG_ON(ret == 0); + + ret = btrfs_previous_item(dev_root, &path, devid, BTRFS_DEV_EXTENT_KEY); + if (ret < 0) + goto out; + + /* No dev_extent at all, not really possible for rootdir case*/ + if (ret > 0) { + *size_ret = 0; + ret = -EUCLEAN; + goto out; + } + + btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); + de = btrfs_item_ptr(path.nodes[0], path.slots[0], + struct btrfs_dev_extent); + *size_ret = key.offset + btrfs_dev_extent_length(path.nodes[0], de); +out: + btrfs_release_path(&path); + return ret; +}