From patchwork Tue Nov 27 05:24:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Misono Tomohiro X-Patchwork-Id: 10699731 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2A0041869 for ; Tue, 27 Nov 2018 05:31:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 167B42A23B for ; Tue, 27 Nov 2018 05:31:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0AAA62A3D6; Tue, 27 Nov 2018 05:31:39 +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 A5AED2A2D2 for ; Tue, 27 Nov 2018 05:31:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728642AbeK0Q2P (ORCPT ); Tue, 27 Nov 2018 11:28:15 -0500 Received: from mgwkm02.jp.fujitsu.com ([202.219.69.169]:14838 "EHLO mgwkm02.jp.fujitsu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728624AbeK0Q2P (ORCPT ); Tue, 27 Nov 2018 11:28:15 -0500 X-Greylist: delayed 666 seconds by postgrey-1.27 at vger.kernel.org; Tue, 27 Nov 2018 11:28:13 EST Received: from kw-mxoi2.gw.nic.fujitsu.com (unknown [192.168.231.133]) by mgwkm02.jp.fujitsu.com with smtp id 7647_8b25_e2f3972e_611d_4d56_9986_bc484b0beca1; Tue, 27 Nov 2018 14:20:23 +0900 Received: from g01jpfmpwyt03.exch.g01.fujitsu.local (g01jpfmpwyt03.exch.g01.fujitsu.local [10.128.193.57]) by kw-mxoi2.gw.nic.fujitsu.com (Postfix) with ESMTP id A8F6FAC00DD for ; Tue, 27 Nov 2018 14:20:23 +0900 (JST) Received: from g01jpexchyt37.g01.fujitsu.local (unknown [10.128.193.4]) by g01jpfmpwyt03.exch.g01.fujitsu.local (Postfix) with ESMTP id BB7F546E6C3; Tue, 27 Nov 2018 14:20:22 +0900 (JST) Received: from luna3.soft.fujitsu.com (10.124.196.199) by g01jpexchyt37.g01.fujitsu.local (10.128.193.67) with Microsoft SMTP Server id 14.3.352.0; Tue, 27 Nov 2018 14:20:22 +0900 From: Misono Tomohiro To: CC: David Sterba Subject: [PATCH 2/8] btrfs-progs: sub list: factor out main part of btrfs_list_subvols Date: Tue, 27 Nov 2018 14:24:43 +0900 Message-ID: <6b60008a654c82495140a928747c117d27663243.1543294426.git.misono.tomohiro@jp.fujitsu.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 X-SecurityPolicyCheck-GC: OK by FENCE-Mail X-TM-AS-MML: disable 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 No functional changes. This is a preparation work for reworking "subvolume list". Signed-off-by: Misono Tomohiro Signed-off-by: David Sterba --- cmds-subvolume.c | 50 +++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/cmds-subvolume.c b/cmds-subvolume.c index 84a03fd8..40cc2687 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -1128,22 +1128,15 @@ out: return subvols; } -static struct subvol_list *btrfs_list_subvols(int fd, - struct btrfs_list_filter_set_v2 *filter_set) +static void get_subvols_info(struct subvol_list **subvols, + struct btrfs_list_filter_set_v2 *filter_set, + int fd, + size_t *capacity) { - struct subvol_list *subvols; - size_t capacity = 0; struct btrfs_util_subvolume_iterator *iter; enum btrfs_util_error err; int ret = -1; - subvols = malloc(sizeof(*subvols)); - if (!subvols) { - error("out of memory"); - return NULL; - } - subvols->num = 0; - err = btrfs_util_create_subvolume_iterator_fd(fd, BTRFS_FS_TREE_OBJECTID, 0, &iter); @@ -1171,11 +1164,11 @@ static struct subvol_list *btrfs_list_subvols(int fd, continue; } - if (subvols->num >= capacity) { + if ((*subvols)->num >= *capacity) { struct subvol_list *new_subvols; - size_t new_capacity = max_t(size_t, 1, capacity * 2); + size_t new_capacity = max_t(size_t, 1, *capacity * 2); - new_subvols = realloc(subvols, + new_subvols = realloc(*subvols, sizeof(*new_subvols) + new_capacity * sizeof(new_subvols->subvols[0])); @@ -1184,12 +1177,12 @@ static struct subvol_list *btrfs_list_subvols(int fd, goto out; } - subvols = new_subvols; - capacity = new_capacity; + *subvols = new_subvols; + *capacity = new_capacity; } - subvols->subvols[subvols->num] = subvol; - subvols->num++; + (*subvols)->subvols[(*subvols)->num] = subvol; + (*subvols)->num++; } ret = 0; @@ -1197,9 +1190,26 @@ out: if (iter) btrfs_util_destroy_subvolume_iterator(iter); if (ret) { - free_subvol_list(subvols); - subvols = NULL; + free_subvol_list(*subvols); + *subvols = NULL; + } +} + +static struct subvol_list *btrfs_list_subvols(int fd, + struct btrfs_list_filter_set_v2 *filter_set) +{ + struct subvol_list *subvols; + size_t capacity = 0; + + subvols = malloc(sizeof(*subvols)); + if (!subvols) { + error("out of memory"); + return NULL; } + subvols->num = 0; + + get_subvols_info(&subvols, filter_set, fd, &capacity); + return subvols; }