linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andreas Gruenbacher <agruenba@redhat.com>
To: linux-xfs@vger.kernel.org
Cc: Andreas Gruenbacher <agruenba@redhat.com>,
	fstests@vger.kernel.org, Jan Kara <jack@suse.cz>
Subject: [PATCH] xfs_io: add seek consistency checks
Date: Thu, 22 Jun 2017 19:03:48 +0200	[thread overview]
Message-ID: <1498151028-18433-1-git-send-email-agruenba@redhat.com> (raw)

When seeking for data and holes, the lseek result must be greater than
or equal to the start offset.  Furthermore, assuming that the file
doesn't change under us, when switching between SEEK_HOLE and SEEK_DATA,
the seek position must increase monotonically.  Warn and abort if this
is not the case.

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
---
 io/seek.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/io/seek.c b/io/seek.c
index d06375d..871b472 100644
--- a/io/seek.c
+++ b/io/seek.c
@@ -147,7 +147,10 @@ seek_f(
 	 * decide if we want to display that type of entry.
 	 */
 	if (flag & SEEK_HFLAG) {
+		current = HOLE;
 		offset = lseek(file->fd, start, SEEK_HOLE);
+		if (offset != -1 && offset < start)
+			goto bad_result;
 		if ((start == offset) || !(flag & SEEK_DFLAG)) {
 			/*
 			 * this offset is a hole or are only displaying holes.
@@ -155,7 +158,6 @@ seek_f(
 			 * data, then we will fall through below to
 			 * initialize the data search.
 			 */
-			current = HOLE;
 			goto found_hole;
 		}
 	}
@@ -163,6 +165,8 @@ seek_f(
 	/* The offset is not a hole, or we are looking just for data */
 	current = DATA;
 	offset = lseek(file->fd, start, SEEK_DATA);
+	if (offset != -1 && offset < start)
+		goto bad_result;
 
 found_hole:
 	/*
@@ -203,8 +207,15 @@ found_hole:
 		current ^= 1;		/* alternate between data and hole */
 		start = offset;
 		offset = lseek(file->fd, start, seekinfo[current].seektype);
+		if (offset != -1 && offset <= start)
+			goto bad_result;
 	}
 	return 0;
+
+bad_result:
+	fprintf(stderr, "Invalid seek result: lseek(<fd>, %lld, SEEK_%s) = %lld\n",
+		(long long)start, seekinfo[current].name, (long long)offset);
+	return 0;
 }
 
 void
-- 
2.7.5


             reply	other threads:[~2017-06-22 17:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-22 17:03 Andreas Gruenbacher [this message]
2017-06-28 16:43 ` [PATCH] xfs_io: add seek consistency checks Christoph Hellwig

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=1498151028-18433-1-git-send-email-agruenba@redhat.com \
    --to=agruenba@redhat.com \
    --cc=fstests@vger.kernel.org \
    --cc=jack@suse.cz \
    --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).