public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: jeffm@suse.com
To: linux-btrfs@vger.kernel.org
Cc: Jeff Mahoney <jeffm@suse.com>
Subject: [PATCH 2/7] btrfs: btrfs_issue_discard ensure offset/length are aligned to sector boundaries
Date: Mon, 15 Jun 2015 09:41:15 -0400	[thread overview]
Message-ID: <1434375680-4115-2-git-send-email-jeffm@suse.com> (raw)
In-Reply-To: <557ED6DC.6070900@suse.com>

From: Jeff Mahoney <jeffm@suse.com>

It's possible, though unexpected, to pass unaligned offsets and lengths
to btrfs_issue_discard.  We then shift the offset/length values to sector
units.  If an unaligned offset has been passed, it will result in the
entire sector being discarded, possibly losing data.  An unaligned
length is safe but we'll end up returning an inaccurate number of
discarded bytes.

This patch aligns the offset to the 512B boundary, adjusts the length,
and warns, since we shouldn't be discarding on an offset that isn't
aligned with our sector size.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
---
 fs/btrfs/extent-tree.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index da1145d..cf9cefd 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1888,12 +1888,21 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
 			       u64 *discarded_bytes)
 {
 	int ret = 0;
+	u64 aligned_start = ALIGN(start, 1 << 9);
 
-	*discarded_bytes = 0;
-	ret = blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
-	if (!ret)
-		*discarded_bytes = len;
+	if (WARN_ON(start != aligned_start)) {
+		len -= aligned_start - start;
+		len = round_down(len, 1 << 9);
+		start = aligned_start;
+	}
 
+	*discarded_bytes = 0;
+	if (len) {
+		ret = blkdev_issue_discard(bdev, start >> 9, len >> 9,
+					   GFP_NOFS, 0);
+		if (!ret)
+			*discarded_bytes = len;
+	}
 	return ret;
 }
 
-- 
2.4.3


  parent reply	other threads:[~2015-06-15 13:41 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-15 13:45 [PATCH v5] btrfs: fix automatic blockgroup remove + discard Jeff Mahoney
2015-06-15 13:41 ` [PATCH 1/7] btrfs: make btrfs_issue_discard return bytes discarded jeffm
2015-06-17  9:53   ` Filipe David Manana
2015-06-15 13:41 ` jeffm [this message]
2015-06-17  9:55   ` [PATCH 2/7] btrfs: btrfs_issue_discard ensure offset/length are aligned to sector boundaries Filipe David Manana
2015-06-15 13:41 ` [PATCH 3/7] btrfs: skip superblocks during discard jeffm
2015-06-17  9:57   ` Filipe David Manana
2015-06-15 13:41 ` [PATCH 4/7] btrfs: iterate over unused chunk space in FITRIM jeffm
2015-06-17 10:03   ` Filipe David Manana
2015-06-15 13:41 ` [PATCH 5/7] btrfs: explictly delete unused block groups in close_ctree and ro-remount jeffm
2015-06-17 10:04   ` Filipe David Manana
2015-06-17 13:24     ` Filipe David Manana
2015-06-17 14:32       ` Jeff Mahoney
2015-06-17 14:36         ` Jeff Mahoney
2015-06-17 14:37           ` Filipe David Manana
2015-06-15 13:41 ` [PATCH 6/7] btrfs: add missing discards when unpinning extents with -o discard jeffm
2015-06-17 10:07   ` Filipe David Manana
2015-06-15 13:41 ` [PATCH 7/7] btrfs: cleanup, stop casting for extent_map->lookup everywhere jeffm
2015-06-17 10:08   ` Filipe David Manana

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=1434375680-4115-2-git-send-email-jeffm@suse.com \
    --to=jeffm@suse.com \
    --cc=linux-btrfs@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