linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nikolay Borisov <nborisov@suse.com>
To: linux-xfs@vger.kernel.org
Cc: fstests@vger.kernel.org, darrick.wong@oracle.com,
	Nikolay Borisov <nborisov@suse.com>
Subject: [PATCH v2 2/2] fiemap: Implement ranged query
Date: Tue, 31 Oct 2017 16:10:11 +0200	[thread overview]
Message-ID: <1509459011-7398-2-git-send-email-nborisov@suse.com> (raw)
In-Reply-To: <1509459011-7398-1-git-send-email-nborisov@suse.com>

Currently the fiemap implementation of xfs_io doesn't support making ranged
queries. This patch implements two optional arguments which take the starting
offset and the length of the region to be queried. This also requires changing
of the final hole range is calculated. Namely, it's now being done as
[last_logical, logical addres of next extent] rather than being statically
determined by [last_logical, filesize].

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
 io/fiemap.c       | 72 +++++++++++++++++++++++++++++++++++++++++++++++--------
 man/man8/xfs_io.8 |  5 ++--
 2 files changed, 65 insertions(+), 12 deletions(-)

diff --git a/io/fiemap.c b/io/fiemap.c
index 08391f69d9bd..668f8fe4e732 100644
--- a/io/fiemap.c
+++ b/io/fiemap.c
@@ -27,6 +27,9 @@
 
 static cmdinfo_t fiemap_cmd;
 static int max_extents = -1;
+static __u64 covered_length = 0;
+static __u64 len = -1LL;
+static bool range_limit = false;
 
 static void
 fiemap_help(void)
@@ -79,7 +82,7 @@ print_hole(
 		       boff_w, _("hole"), tot_w, lstart - llast);
 	   }
 
-
+	   covered_length += BBTOB(lstart - llast);
 }
 
 static int
@@ -90,7 +93,8 @@ print_verbose(
 	int			tot_w,
 	int			flg_w,
 	int			cur_extent,
-	__u64			last_logical)
+	__u64			last_logical,
+	__u64			limit)
 {
 	__u64			lstart;
 	__u64			llast;
@@ -122,7 +126,7 @@ print_verbose(
 		cur_extent++;
 	}
 
-	if (cur_extent == max_extents)
+	if (cur_extent == max_extents || (range_limit && covered_length >= limit))
 		return 1;
 
 	snprintf(lbuf, sizeof(lbuf), "[%llu..%llu]:", lstart,
@@ -140,7 +144,8 @@ print_plain(
 	struct fiemap_extent	*extent,
 	int			lflag,
 	int			cur_extent,
-	__u64			last_logical)
+	__u64			last_logical,
+	__u64			limit)
 {
 	__u64			lstart;
 	__u64			llast;
@@ -157,7 +162,7 @@ print_plain(
 		cur_extent++;
 	}
 
-	if (cur_extent == max_extents)
+	if (cur_extent == max_extents || (range_limit && covered_length >= limit))
 		return 1;
 
 	printf("\t%d: [%llu..%llu]: %llu..%llu", cur_extent,
@@ -256,8 +261,11 @@ fiemap_f(
 	int		tot_w = 5;	/* 5 since its just one number */
 	int		flg_w = 5;
 	__u64		last_logical = 0;
+	size_t		fsblocksize, fssectsize;
 	struct stat	st;
 
+	init_cvtnum(&fsblocksize, &fssectsize);
+
 	while ((c = getopt(argc, argv, "aln:v")) != EOF) {
 		switch (c) {
 		case 'a':
@@ -277,6 +285,26 @@ fiemap_f(
 		}
 	}
 
+	if (optind < argc) {
+		off64_t start_offset = cvtnum(fsblocksize, fssectsize, argv[optind]);
+		if (start_offset < 0) {
+			printf("non-numeric offset argument -- %s\n", argv[optind]);
+			return 0;
+		}
+		last_logical = start_offset;
+		optind++;
+	}
+
+	if (optind < argc) {
+		off64_t length = cvtnum(fsblocksize, fssectsize, argv[optind]);
+		if (length < 0) {
+			printf("non-numeric len argument -- %s\n", argv[optind]);
+			return 0;
+		}
+		len = length;
+		range_limit = true;
+	}
+
 	map_size = sizeof(struct fiemap) +
 		(EXTENT_BATCH * sizeof(struct fiemap_extent));
 	fiemap = malloc(map_size);
@@ -317,14 +345,18 @@ fiemap_f(
 				num_printed = print_verbose(extent, foff_w,
 							    boff_w, tot_w,
 							    flg_w, cur_extent,
-							    last_logical);
+							    last_logical,
+							    len);
 			} else
 				num_printed = print_plain(extent, lflag,
 							  cur_extent,
-							  last_logical);
+							  last_logical,
+							  len);
 
 			cur_extent += num_printed;
 			last_logical = extent->fe_logical + extent->fe_length;
+			if (num_printed == 2)
+				covered_length += extent->fe_length;
 
 			if (extent->fe_flags & FIEMAP_EXTENT_LAST) {
 				last = 1;
@@ -333,6 +365,9 @@ fiemap_f(
 
 			if (cur_extent == max_extents)
 				break;
+
+			if (range_limit && covered_length >= len)
+				goto out;
 		}
 	}
 
@@ -348,9 +383,26 @@ fiemap_f(
 		return 0;
 	}
 
-	if (cur_extent && last_logical < st.st_size)
+	if (last_logical < st.st_size &&
+	    (!range_limit || covered_length < len)) {
+		int end;
+
+		ret = __fiemap(fiemap, map_size, fiemap_flags, last_logical,
+			       st.st_size);
+		if (ret < 0) {
+			exitcode = 1;
+			goto out;
+		}
+
+		if (!fiemap->fm_mapped_extents)
+			end = BTOBBT(st.st_size);
+		else
+			end = BTOBBT(fiemap->fm_extents[0].fe_logical);
+
+
 		print_hole(foff_w, boff_w, tot_w, cur_extent, lflag, !vflag,
-			   BTOBBT(last_logical), BTOBBT(st.st_size));
+			   BTOBBT(last_logical), end);
+	}
 
 out:
 	free(fiemap);
@@ -365,7 +417,7 @@ fiemap_init(void)
 	fiemap_cmd.argmin = 0;
 	fiemap_cmd.argmax = -1;
 	fiemap_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
-	fiemap_cmd.args = _("[-alv] [-n nx]");
+	fiemap_cmd.args = _("[-alv] [-n nx] [offset [len]]");
 	fiemap_cmd.oneline = _("print block mapping for a file");
 	fiemap_cmd.help = fiemap_help;
 
diff --git a/man/man8/xfs_io.8 b/man/man8/xfs_io.8
index 0fd9b951199c..27f1ae163913 100644
--- a/man/man8/xfs_io.8
+++ b/man/man8/xfs_io.8
@@ -295,11 +295,12 @@ Prints the block mapping for the current open file. Refer to the
 .BR xfs_bmap (8)
 manual page for complete documentation.
 .TP
-.BI "fiemap [ \-alv ] [ \-n " nx " ]"
+.BI "fiemap [ \-alv ] [ \-n " nx " ] [ " offset " [ " len " ]]"
 Prints the block mapping for the current open file using the fiemap
 ioctl.  Options behave as described in the
 .BR xfs_bmap (8)
-manual page.
+manual page. Optionally, this command also supports passing the start offset
+from where to begin the fiemap and the length of that region.
 .TP
 .BI "fsmap [ \-d | \-l | \-r ] [ \-m | \-v ] [ \-n " nx " ] [ " start " ] [ " end " ]
 Prints the mapping of disk blocks used by the filesystem hosting the current
-- 
2.7.4


  reply	other threads:[~2017-10-31 14:10 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-27 12:37 [PATCH v2 0/2] Fiemap's range query Nikolay Borisov
2017-10-27 12:37 ` [PATCH 1/2] fiemap: Factor out actual fiemap call code Nikolay Borisov
2017-10-27 16:33   ` Darrick J. Wong
2017-10-27 16:37     ` Darrick J. Wong
2017-10-27 12:37 ` [PATCH 1/2] initial fiemap test Nikolay Borisov
2017-10-27 12:44   ` Eryu Guan
2017-10-31  9:22     ` Nikolay Borisov
2017-10-31  9:32       ` Eryu Guan
2017-10-31 12:27         ` Nikolay Borisov
2017-10-27 12:37 ` [PATCH 2/2] fiemap: Implement ranged query Nikolay Borisov
2017-10-27 12:37 ` [PATCH 2/2] generic: Adjust generic test ouputs for new fiemap implementation Nikolay Borisov
2017-10-27 12:46   ` Eryu Guan
2017-10-27 12:48     ` Nikolay Borisov
2017-10-31 14:10 ` [PATCH v2 1/2] fiemap: Factor out actual fiemap call code Nikolay Borisov
2017-10-31 14:10   ` Nikolay Borisov [this message]
2017-10-31 14:11   ` [PATCH v2 1/4] common: Check for fiemap range argument support Nikolay Borisov
2017-10-31 14:11     ` [PATCH v2 2/4] punch: Implement fixup for fiemap range queries Nikolay Borisov
2017-11-07  5:18       ` Eryu Guan
2017-10-31 14:11     ` [PATCH v2 3/4] generic: Adjust generic test ouputs for new fiemap implementation Nikolay Borisov
2017-11-07  5:15       ` Eryu Guan
2017-10-31 14:11     ` [PATCH v2 4/4] xfs: initial fiemap range query test Nikolay Borisov
2017-11-02  3:16     ` [PATCH v2 1/4] common: Check for fiemap range argument support Eryu Guan
2017-11-02  6:59       ` Nikolay Borisov
2017-11-02  8:13       ` [PATCH v3] " Nikolay Borisov
2017-11-02 21:12         ` Dave Chinner
2017-11-03 14:05           ` Nikolay Borisov
2017-11-03 16:28             ` Darrick J. Wong
2017-11-14 15:09         ` Eric Sandeen
2017-11-15  7:41           ` Eryu Guan
2017-10-31 20:29   ` [PATCH v2 1/2] fiemap: Factor out actual fiemap call code Darrick J. Wong
2017-10-31 20:32     ` Nikolay Borisov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1509459011-7398-2-git-send-email-nborisov@suse.com \
    --to=nborisov@suse.com \
    --cc=darrick.wong@oracle.com \
    --cc=fstests@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).