]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
Btrfs: fix space leak when we fail to make an allocation
authorJosef Bacik <josef@redhat.com>
Fri, 19 Aug 2011 19:45:52 +0000 (15:45 -0400)
committerChris Mason <chris.mason@oracle.com>
Wed, 16 Nov 2011 02:20:18 +0000 (21:20 -0500)
When changing back to using a spin_lock to protect the extent counters I decided
that since we would only be dropping our original extent, it was ok to just drop
the extent and return.  However since somebody else could have come in and done
a reservation, we need to do the normal song and dance to clear the reservation
out properly.  So calculate how much space we need to free, and then subtract
what we just attempted to reserve.  If it's more then we know we need to drop
those bytes from the delalloc block rsv.  Thanks,

Signed-off-by: Josef Bacik <josef@redhat.com>
(cherry picked from commit 7ed49f187c82821e35f8869399bcf90822a74a23)

fs/btrfs/extent-tree.c

index 3d5a8d74ef0f07ae86c3d916de27b1ea03940db8..b5c8f293d56bcb0b3b0c3844be22bf961966a9df 100644 (file)
@@ -4025,16 +4025,24 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
 
        ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
        if (ret) {
+               u64 to_free = 0;
                unsigned dropped;
-               /*
-                * We don't need the return value since our reservation failed,
-                * we just need to clean up our counter.
-                */
+
                spin_lock(&BTRFS_I(inode)->lock);
                dropped = drop_outstanding_extent(inode);
-               WARN_ON(dropped > 1);
-               BTRFS_I(inode)->csum_bytes -= num_bytes;
+               to_free = calc_csum_metadata_size(inode, num_bytes, 0);
                spin_unlock(&BTRFS_I(inode)->lock);
+               to_free += btrfs_calc_trans_metadata_size(root, dropped);
+
+               /*
+                * Somebody could have come in and twiddled with the
+                * reservation, so if we have to free more than we would have
+                * reserved from this reservation go ahead and release those
+                * bytes.
+                */
+               to_free -= to_reserve;
+               if (to_free)
+                       btrfs_block_rsv_release(root, block_rsv, to_free);
                return ret;
        }