Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 32c00aff authored by Josef Bacik's avatar Josef Bacik Committed by Chris Mason
Browse files

Btrfs: release delalloc reservations on extent item insertion



This patch fixes an issue with the delalloc metadata space reservation
code.  The problem is we used to free the reservation as soon as we
allocated the delalloc region.  The problem with this is if we are not
inserting an inline extent, we don't actually insert the extent item until
after the ordered extent is written out.  This patch does 3 things,

1) It moves the reservation clearing stuff into the ordered code, so when
we remove the ordered extent we remove the reservation.
2) It adds a EXTENT_DO_ACCOUNTING flag that gets passed when we clear
delalloc bits in the cases where we want to clear the metadata reservation
when we clear the delalloc extent, in the case that we do an inline extent
or we invalidate the page.
3) It adds another waitqueue to the space info so that when we start a fs
wide delalloc flush, anybody else who also hits that area will simply wait
for the flush to finish and then try to make their allocation.

This has been tested thoroughly to make sure we did not regress on
performance.

Signed-off-by: default avatarJosef Bacik <jbacik@redhat.com>
Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent a3429ab7
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -128,12 +128,14 @@ struct btrfs_inode {
	u64 last_unlink_trans;

	/*
	 * These two counters are for delalloc metadata reservations.  We keep
	 * track of how many extents we've accounted for vs how many extents we
	 * have.
	 * Counters to keep track of the number of extent item's we may use due
	 * to delalloc and such.  outstanding_extents is the number of extent
	 * items we think we'll end up using, and reserved_extents is the number
	 * of extent items we've reserved metadata for.
	 */
	int delalloc_reserved_extents;
	int delalloc_extents;
	spinlock_t accounting_lock;
	int reserved_extents;
	int outstanding_extents;

	/*
	 * ordered_data_close is set by truncate when a file that used
+3 −0
Original line number Diff line number Diff line
@@ -699,6 +699,9 @@ struct btrfs_space_info {

	int allocating_chunk;
	wait_queue_head_t wait;

	int flushing;
	wait_queue_head_t flush_wait;
};

/*
+43 −11
Original line number Diff line number Diff line
@@ -2823,14 +2823,17 @@ int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
					   num_items);

	spin_lock(&meta_sinfo->lock);
	if (BTRFS_I(inode)->delalloc_reserved_extents <=
	    BTRFS_I(inode)->delalloc_extents) {
	spin_lock(&BTRFS_I(inode)->accounting_lock);
	if (BTRFS_I(inode)->reserved_extents <=
	    BTRFS_I(inode)->outstanding_extents) {
		spin_unlock(&BTRFS_I(inode)->accounting_lock);
		spin_unlock(&meta_sinfo->lock);
		return 0;
	}
	spin_unlock(&BTRFS_I(inode)->accounting_lock);

	BTRFS_I(inode)->delalloc_reserved_extents--;
	BUG_ON(BTRFS_I(inode)->delalloc_reserved_extents < 0);
	BTRFS_I(inode)->reserved_extents--;
	BUG_ON(BTRFS_I(inode)->reserved_extents < 0);

	if (meta_sinfo->bytes_delalloc < num_bytes) {
		bug = true;
@@ -2863,6 +2866,37 @@ static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
		meta_sinfo->force_delalloc = 0;
}

static void flush_delalloc(struct btrfs_root *root,
				 struct btrfs_space_info *info)
{
	bool wait = false;

	spin_lock(&info->lock);

	if (!info->flushing) {
		info->flushing = 1;
		init_waitqueue_head(&info->flush_wait);
	} else {
		wait = true;
	}

	spin_unlock(&info->lock);

	if (wait) {
		wait_event(info->flush_wait,
			   !info->flushing);
		return;
	}

	btrfs_start_delalloc_inodes(root);
	btrfs_wait_ordered_extents(root, 0);

	spin_lock(&info->lock);
	info->flushing = 0;
	spin_unlock(&info->lock);
	wake_up(&info->flush_wait);
}

static int maybe_allocate_chunk(struct btrfs_root *root,
				 struct btrfs_space_info *info)
{
@@ -2980,21 +3014,20 @@ int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
			filemap_flush(inode->i_mapping);
			goto again;
		} else if (flushed == 3) {
			btrfs_start_delalloc_inodes(root);
			btrfs_wait_ordered_extents(root, 0);
			flush_delalloc(root, meta_sinfo);
			goto again;
		}
		spin_lock(&meta_sinfo->lock);
		meta_sinfo->bytes_delalloc -= num_bytes;
		spin_unlock(&meta_sinfo->lock);
		printk(KERN_ERR "enospc, has %d, reserved %d\n",
		       BTRFS_I(inode)->delalloc_extents,
		       BTRFS_I(inode)->delalloc_reserved_extents);
		       BTRFS_I(inode)->outstanding_extents,
		       BTRFS_I(inode)->reserved_extents);
		dump_space_info(meta_sinfo, 0, 0);
		return -ENOSPC;
	}

	BTRFS_I(inode)->delalloc_reserved_extents++;
	BTRFS_I(inode)->reserved_extents++;
	check_force_delalloc(meta_sinfo);
	spin_unlock(&meta_sinfo->lock);

@@ -3093,8 +3126,7 @@ int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
		}

		if (retries == 2) {
			btrfs_start_delalloc_inodes(root);
			btrfs_wait_ordered_extents(root, 0);
			flush_delalloc(root, meta_sinfo);
			goto again;
		}
		spin_lock(&meta_sinfo->lock);
+13 −6
Original line number Diff line number Diff line
@@ -460,7 +460,8 @@ static int clear_state_bit(struct extent_io_tree *tree,
			    struct extent_state *state, int bits, int wake,
			    int delete)
{
	int ret = state->state & bits;
	int bits_to_clear = bits & ~EXTENT_DO_ACCOUNTING;
	int ret = state->state & bits_to_clear;

	if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
		u64 range = state->end - state->start + 1;
@@ -468,7 +469,7 @@ static int clear_state_bit(struct extent_io_tree *tree,
		tree->dirty_bytes -= range;
	}
	clear_state_cb(tree, state, bits);
	state->state &= ~bits;
	state->state &= ~bits_to_clear;
	if (wake)
		wake_up(&state->wq);
	if (delete || state->state == 0) {
@@ -956,7 +957,8 @@ int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
		       gfp_t mask)
{
	return clear_extent_bit(tree, start, end,
				EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
				EXTENT_DIRTY | EXTENT_DELALLOC |
				EXTENT_DO_ACCOUNTING, 0, 0,
				NULL, mask);
}

@@ -1419,9 +1421,13 @@ int extent_clear_unlock_delalloc(struct inode *inode,
	if (op & EXTENT_CLEAR_DELALLOC)
		clear_bits |= EXTENT_DELALLOC;

	if (op & EXTENT_CLEAR_ACCOUNTING)
		clear_bits |= EXTENT_DO_ACCOUNTING;

	clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
	if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK |
		    EXTENT_END_WRITEBACK | EXTENT_SET_PRIVATE2)))
	if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
		    EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
		    EXTENT_SET_PRIVATE2)))
		return 0;

	while (nr_pages > 0) {
@@ -2709,7 +2715,8 @@ int extent_invalidatepage(struct extent_io_tree *tree,
	lock_extent(tree, start, end, GFP_NOFS);
	wait_on_page_writeback(page);
	clear_extent_bit(tree, start, end,
			 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
			 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
			 EXTENT_DO_ACCOUNTING,
			 1, 1, NULL, GFP_NOFS);
	return 0;
}
+2 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#define EXTENT_BUFFER_FILLED (1 << 8)
#define EXTENT_BOUNDARY (1 << 9)
#define EXTENT_NODATASUM (1 << 10)
#define EXTENT_DO_ACCOUNTING (1 << 11)
#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)

/* flags for bio submission */
@@ -33,6 +34,7 @@
#define EXTENT_SET_WRITEBACK	 0x10
#define EXTENT_END_WRITEBACK	 0x20
#define EXTENT_SET_PRIVATE2	 0x40
#define EXTENT_CLEAR_ACCOUNTING  0x80

/*
 * page->private values.  Every page that is controlled by the extent
Loading