From patchwork Wed Aug 23 15:11:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Borisov X-Patchwork-Id: 9917591 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 E1C92603FF for ; Wed, 23 Aug 2017 15:11:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D4DCF289A0 for ; Wed, 23 Aug 2017 15:11:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9B5828900; Wed, 23 Aug 2017 15:11:32 +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 5FD87289BF for ; Wed, 23 Aug 2017 15:11:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932081AbdHWPL3 (ORCPT ); Wed, 23 Aug 2017 11:11:29 -0400 Received: from mx2.suse.de ([195.135.220.15]:37217 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754131AbdHWPL2 (ORCPT ); Wed, 23 Aug 2017 11:11:28 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id DD63FAD1A; Wed, 23 Aug 2017 15:11:26 +0000 (UTC) From: Nikolay Borisov To: sandeen@sandeen.net Cc: linux-xfs@vger.kernel.org, Nikolay Borisov Subject: [PATCH 3/4] fiemap: Simplify internals of fiemap_f. Date: Wed, 23 Aug 2017 18:11:21 +0300 Message-Id: <1503501082-16983-3-git-send-email-nborisov@suse.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1503501082-16983-1-git-send-email-nborisov@suse.com> References: <5fc15693-7df3-c1bf-9f78-020eed30b567@sandeen.net> <1503501082-16983-1-git-send-email-nborisov@suse.com> Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP So far fiemap used some rather convoluted logic to terminate the iteration and calculate the number of extents to pass to fm_extent_count. Simplify this by: * Get the whole number of extents that this file has and keep iterating until we've printed all extents * Always use a hard-coded batch of 32 extents so we don't have to worry about any extra calculations Signed-off-by: Nikolay Borisov --- io/fiemap.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/io/fiemap.c b/io/fiemap.c index 30c49112e089..ef54b265ab91 100644 --- a/io/fiemap.c +++ b/io/fiemap.c @@ -23,6 +23,8 @@ #include "init.h" #include "io.h" +#define EXTENT_BATCH 32 + static cmdinfo_t fiemap_cmd; static const __u64 blocksize = 512; static int max_extents = 0; @@ -95,7 +97,7 @@ print_verbose( memset(lbuf, 0, sizeof(lbuf)); } - if ((*cur_extent + 1) == max_extents) + if (*cur_extent == max_extents) return; snprintf(lbuf, sizeof(lbuf), "[%llu..%llu]:", lstart, @@ -137,7 +139,7 @@ print_plain( (*cur_extent)++; } - if ((*cur_extent + 1) == max_extents) + if (*cur_extent == max_extents) return; printf("\t%d: [%llu..%llu]: %llu..%llu", *cur_extent, @@ -215,7 +217,7 @@ fiemap_f( char **argv) { struct fiemap *fiemap; - int num_extents = 32; + int num_extents; int last = 0; int lflag = 0; int vflag = 0; @@ -251,10 +253,15 @@ fiemap_f( } } - if (max_extents) - num_extents = min(num_extents, max_extents); + ret = get_extent_count(file->fd, last_logical, -1); + if (ret < 0) { + exitcode = 1; + return 0; + } + num_extents = ret; + map_size = sizeof(struct fiemap) + - (num_extents * sizeof(struct fiemap_extent)); + (EXTENT_BATCH * sizeof(struct fiemap_extent)); fiemap = malloc(map_size); if (!fiemap) { fprintf(stderr, _("%s: malloc of %d bytes failed.\n"), @@ -265,16 +272,14 @@ fiemap_f( printf("%s:\n", file->name); - while (!last && ((cur_extent + 1) != max_extents)) { - if (max_extents) - num_extents = min(num_extents, - max_extents - (cur_extent + 1)); + while (!last && num_extents) { + /* Query a batch worth of extents */ memset(fiemap, 0, map_size); fiemap->fm_flags = fiemap_flags; fiemap->fm_start = last_logical; fiemap->fm_length = -1LL; - fiemap->fm_extent_count = num_extents; + fiemap->fm_extent_count = EXTENT_BATCH; ret = ioctl(file->fd, FS_IOC_FIEMAP, (unsigned long)fiemap); if (ret < 0) { @@ -307,17 +312,17 @@ fiemap_f( print_plain(extent, lflag, blocksize, &cur_extent, &last_logical); - if (extent->fe_flags & FIEMAP_EXTENT_LAST) { + if (extent->fe_flags & FIEMAP_EXTENT_LAST || + cur_extent == max_extents) { last = 1; break; } - - if ((cur_extent + 1) == max_extents) - break; } + + num_extents -= fiemap->fm_mapped_extents; } - if ((cur_extent + 1) == max_extents) + if (cur_extent == max_extents) goto out; memset(&st, 0, sizeof(st));