--- /dev/null
+From 17024ad0a0fdfcfe53043afb969b813d3e020c21 Mon Sep 17 00:00:00 2001
+From: Omar Sandoval <osandov@fb.com>
+Date: Thu, 20 Jul 2017 15:10:35 -0700
+Subject: Btrfs: fix early ENOSPC due to delalloc
+
+From: Omar Sandoval <osandov@fb.com>
+
+commit 17024ad0a0fdfcfe53043afb969b813d3e020c21 upstream.
+
+If a lot of metadata is reserved for outstanding delayed allocations, we
+rely on shrink_delalloc() to reclaim metadata space in order to fulfill
+reservation tickets. However, shrink_delalloc() has a shortcut where if
+it determines that space can be overcommitted, it will stop early. This
+made sense before the ticketed enospc system, but now it means that
+shrink_delalloc() will often not reclaim enough space to fulfill any
+tickets, leading to an early ENOSPC. (Reservation tickets don't care
+about being able to overcommit, they need every byte accounted for.)
+
+Fix it by getting rid of the shortcut so that shrink_delalloc() reclaims
+all of the metadata it is supposed to. This fixes early ENOSPCs we were
+seeing when doing a btrfs receive to populate a new filesystem, as well
+as early ENOSPCs Christoph saw when doing a big cp -r onto Btrfs.
+
+Fixes: 957780eb2788 ("Btrfs: introduce ticketed enospc infrastructure")
+Tested-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
+Reviewed-by: Josef Bacik <jbacik@fb.com>
+Signed-off-by: Omar Sandoval <osandov@fb.com>
+Signed-off-by: David Sterba <dsterba@suse.com>
+Signed-off-by: Nikolay Borisov <nborisov@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ fs/btrfs/extent-tree.c | 4 ----
+ 1 file changed, 4 deletions(-)
+
+--- a/fs/btrfs/extent-tree.c
++++ b/fs/btrfs/extent-tree.c
+@@ -4776,10 +4776,6 @@ skip_async:
+ else
+ flush = BTRFS_RESERVE_NO_FLUSH;
+ spin_lock(&space_info->lock);
+- if (can_overcommit(root, space_info, orig, flush)) {
+- spin_unlock(&space_info->lock);
+- break;
+- }
+ if (list_empty(&space_info->tickets) &&
+ list_empty(&space_info->priority_tickets)) {
+ spin_unlock(&space_info->lock);