All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hans van Kranenburg <hans.van.kranenburg@mendix.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 3/6] btrfs: alloc_chunk: fix more DUP stripe size handling
Date: Thu,  4 Oct 2018 23:24:40 +0200	[thread overview]
Message-ID: <20181004212443.26519-4-hans.van.kranenburg@mendix.com> (raw)
In-Reply-To: <20181004212443.26519-1-hans.van.kranenburg@mendix.com>

Commit 92e222df7b "btrfs: alloc_chunk: fix DUP stripe size handling"
fixed calculating the stripe_size for a new DUP chunk.

However, the same calculation reappears a bit later, and that one was
not changed yet. The resulting bug that is exposed is that the newly
allocated device extents ('stripes') can have a few MiB overlap with the
next thing stored after them, which is another device extent or the end
of the disk.

The scenario in which this can happen is:
* The block device for the filesystem is less than 10GiB in size.
* The amount of contiguous free unallocated disk space chosen to use for
  chunk allocation is 20% of the total device size, or a few MiB more or
  less.

An example:
- The filesystem device is 7880MiB (max_chunk_size gets set to 788MiB)
- There's 1578MiB unallocated raw disk space left in one contiguous
  piece.

In this case stripe_size is first calculated as 789MiB, (half of
1578MiB).

Since 789MiB (stripe_size * data_stripes) > 788MiB (max_chunk_size), we
enter the if block. Now stripe_size value is immediately overwritten
while calculating an adjusted value based on max_chunk_size, which ends
up as 788MiB.

Next, the value is rounded up to a 16MiB boundary, 800MiB, which is
actually more than the value we had before. However, the last comparison
fails to detect this, because it's comparing the value with the total
amount of free space, which is about twice the size of stripe_size.

In the example above, this means that the resulting raw disk space being
allocated is 1600MiB, while only a gap of 1578MiB has been found. The
second device extent object for this DUP chunk will overlap for 22MiB
with whatever comes next.

The underlying problem here is that the stripe_size is reused all the
time for different things. So, when entering the code in the if block,
stripe_size is immediately overwritten with something else. If later we
decide we want to have the previous value back, then the logic to
compute it was copy pasted in again.

With this change, the value in stripe_size is not unnecessarily
destroyed, so the duplicated calculation is not needed any more.

Signed-off-by: Hans van Kranenburg <hans.van.kranenburg@mendix.com>
---
 fs/btrfs/volumes.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 40fa85e68b1f..7045814fc98d 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -4763,19 +4763,16 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 	/*
 	 * Use the number of data stripes to figure out how big this chunk
 	 * is really going to be in terms of logical address space,
-	 * and compare that answer with the max chunk size
+	 * and compare that answer with the max chunk size. If it's higher,
+	 * we try to reduce stripe_size.
 	 */
 	if (stripe_size * data_stripes > max_chunk_size) {
-		stripe_size = div_u64(max_chunk_size, data_stripes);
-
-		/* bump the answer up to a 16MB boundary */
-		stripe_size = round_up(stripe_size, SZ_16M);
-
-		/*
-		 * But don't go higher than the limits we found while searching
-		 * for free extents
+		/* Reduce stripe_size, round it up to a 16MB boundary
+		 * again and then use it, unless it ends up being even
+		 * bigger than the previous value we had already.
 		 */
-		stripe_size = min(devices_info[ndevs - 1].max_avail,
+		stripe_size = min(round_up(div_u64(max_chunk_size,
+						   data_stripes), SZ_16M),
 				  stripe_size);
 	}
 
-- 
2.19.0.329.g76f2f5c1e3


  parent reply	other threads:[~2018-10-04 21:30 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-04 21:24 [PATCH 0/6] Chunk allocator DUP fix and cleanups Hans van Kranenburg
2018-10-04 21:24 ` [PATCH 1/6] btrfs: alloc_chunk: do not refurbish num_bytes Hans van Kranenburg
2018-10-05  8:59   ` Nikolay Borisov
2018-10-05  9:00     ` Nikolay Borisov
2018-10-04 21:24 ` [PATCH 2/6] btrfs: alloc_chunk: improve chunk size variable name Hans van Kranenburg
2018-10-04 21:36   ` Hans van Kranenburg
2018-10-05  9:00   ` Nikolay Borisov
2018-10-04 21:24 ` Hans van Kranenburg [this message]
2018-10-04 21:24 ` [PATCH 4/6] btrfs: fix ncopies raid_attr for RAID56 Hans van Kranenburg
2018-10-05  9:10   ` Nikolay Borisov
2018-10-04 21:24 ` [PATCH 5/6] btrfs: introduce nparity raid_attr Hans van Kranenburg
2018-10-04 22:21   ` Hans van Kranenburg
2018-10-05 14:42   ` Nikolay Borisov
2018-10-05 22:45     ` Hans van Kranenburg
2018-10-04 21:24 ` [PATCH 6/6] btrfs: alloc_chunk: rework chunk/stripe calculations Hans van Kranenburg
2018-10-05  7:51 ` [PATCH 0/6] Chunk allocator DUP fix and cleanups Qu Wenruo
2018-10-05 10:58   ` Hans van Kranenburg
2018-10-08  6:43     ` Qu Wenruo
2018-10-08 13:19       ` Hans van Kranenburg
2018-10-08 23:51         ` Hans van Kranenburg
2018-10-05  7:51 ` Qu Wenruo
2018-10-11 15:13 ` David Sterba
2018-10-11 19:40   ` Hans van Kranenburg
2018-10-11 20:34     ` Hans van Kranenburg
2018-11-13 15:03     ` David Sterba
2018-11-13 16:45       ` Hans van Kranenburg

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=20181004212443.26519-4-hans.van.kranenburg@mendix.com \
    --to=hans.van.kranenburg@mendix.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.