Commit 8b62f87b authored by Josef Bacik's avatar Josef Bacik Committed by David Sterba

Btrfs: rework outstanding_extents

Right now we do a lot of weird hoops around outstanding_extents in order
to keep the extent count consistent.  This is because we logically
transfer the outstanding_extent count from the initial reservation
through the set_delalloc_bits.  This makes it pretty difficult to get a
handle on how and when we need to mess with outstanding_extents.

Fix this by revamping the rules of how we deal with outstanding_extents.
Now instead everybody that is holding on to a delalloc extent is
required to increase the outstanding extents count for itself.  This
means we'll have something like this

btrfs_delalloc_reserve_metadata	- outstanding_extents = 1
 btrfs_set_extent_delalloc	- outstanding_extents = 2
btrfs_release_delalloc_extents	- outstanding_extents = 1

for an initial file write.  Now take the append write where we extend an
existing delalloc range but still under the maximum extent size

btrfs_delalloc_reserve_metadata - outstanding_extents = 2
  btrfs_set_extent_delalloc
    btrfs_set_bit_hook		- outstanding_extents = 3
    btrfs_merge_extent_hook	- outstanding_extents = 2
btrfs_delalloc_release_extents	- outstanding_extnets = 1

In order to make the ordered extent transition we of course must now
make ordered extents carry their own outstanding_extent reservation, so
for cow_file_range we end up with

btrfs_add_ordered_extent	- outstanding_extents = 2
clear_extent_bit		- outstanding_extents = 1
btrfs_remove_ordered_extent	- outstanding_extents = 0

This makes all manipulations of outstanding_extents much more explicit.
Every successful call to btrfs_delalloc_reserve_metadata _must_ now be
combined with btrfs_release_delalloc_extents, even in the error case, as
that is the only function that actually modifies the
outstanding_extents counter.

The drawback to this is now we are much more likely to have transient
cases where outstanding_extents is much larger than it actually should
be.  This could happen before as we manipulated the delalloc bits, but
now it happens basically at every write.  This may put more pressure on
the ENOSPC flushing code, but I think making this code simpler is worth
the cost.  I have another change coming to mitigate this side-effect
somewhat.

I also added trace points for the counter manipulation.  These were used
by a bpf script I wrote to help track down leak issues.
Signed-off-by: default avatarJosef Bacik <jbacik@fb.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent b115e3bc
...@@ -267,6 +267,23 @@ static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode) ...@@ -267,6 +267,23 @@ static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)
return false; return false;
} }
static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode,
int mod)
{
lockdep_assert_held(&inode->lock);
inode->outstanding_extents += mod;
if (btrfs_is_free_space_inode(inode))
return;
}
static inline void btrfs_mod_reserved_extents(struct btrfs_inode *inode, int mod)
{
lockdep_assert_held(&inode->lock);
inode->reserved_extents += mod;
if (btrfs_is_free_space_inode(inode))
return;
}
static inline int btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation) static inline int btrfs_inode_in_log(struct btrfs_inode *inode, u64 generation)
{ {
int ret = 0; int ret = 0;
......
...@@ -2748,6 +2748,8 @@ int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, ...@@ -2748,6 +2748,8 @@ int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
u64 *qgroup_reserved, bool use_global_rsv); u64 *qgroup_reserved, bool use_global_rsv);
void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info, void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
struct btrfs_block_rsv *rsv); struct btrfs_block_rsv *rsv);
void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes);
int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes); int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes);
void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes); void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes);
int btrfs_delalloc_reserve_space(struct inode *inode, int btrfs_delalloc_reserve_space(struct inode *inode,
......
...@@ -5954,42 +5954,31 @@ void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info, ...@@ -5954,42 +5954,31 @@ void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
} }
/** /**
* drop_outstanding_extent - drop an outstanding extent * drop_over_reserved_extents - drop our extra extent reservations
* @inode: the inode we're dropping the extent for * @inode: the inode we're dropping the extent for
* @num_bytes: the number of bytes we're releasing.
* *
* This is called when we are freeing up an outstanding extent, either called * We reserve extents we may use, but they may have been merged with other
* after an error or after an extent is written. This will return the number of * extents and we may not need the extra reservation.
* reserved extents that need to be freed. This must be called with *
* BTRFS_I(inode)->lock held. * We also call this when we've completed io to an extent or had an error and
* cleared the outstanding extent, in either case we no longer need our
* reservation and can drop the excess.
*/ */
static unsigned drop_outstanding_extent(struct btrfs_inode *inode, static unsigned drop_over_reserved_extents(struct btrfs_inode *inode)
u64 num_bytes)
{ {
unsigned drop_inode_space = 0; unsigned num_extents = 0;
unsigned dropped_extents = 0;
unsigned num_extents;
num_extents = count_max_extents(num_bytes); if (inode->reserved_extents > inode->outstanding_extents) {
ASSERT(num_extents); num_extents = inode->reserved_extents -
ASSERT(inode->outstanding_extents >= num_extents); inode->outstanding_extents;
inode->outstanding_extents -= num_extents; btrfs_mod_reserved_extents(inode, -num_extents);
}
if (inode->outstanding_extents == 0 && if (inode->outstanding_extents == 0 &&
test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
&inode->runtime_flags)) &inode->runtime_flags))
drop_inode_space = 1; num_extents++;
return num_extents;
/*
* If we have more or the same amount of outstanding extents than we have
* reserved then we need to leave the reserved extents count alone.
*/
if (inode->outstanding_extents >= inode->reserved_extents)
return drop_inode_space;
dropped_extents = inode->reserved_extents - inode->outstanding_extents;
inode->reserved_extents -= dropped_extents;
return dropped_extents + drop_inode_space;
} }
/** /**
...@@ -6044,13 +6033,15 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) ...@@ -6044,13 +6033,15 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv; struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
u64 to_reserve = 0; u64 to_reserve = 0;
u64 csum_bytes; u64 csum_bytes;
unsigned nr_extents; unsigned nr_extents, reserve_extents;
enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
int ret = 0; int ret = 0;
bool delalloc_lock = true; bool delalloc_lock = true;
u64 to_free = 0; u64 to_free = 0;
unsigned dropped; unsigned dropped;
bool release_extra = false; bool release_extra = false;
bool underflow = false;
bool did_retry = false;
/* If we are a free space inode we need to not flush since we will be in /* If we are a free space inode we need to not flush since we will be in
* the middle of a transaction commit. We also don't need the delalloc * the middle of a transaction commit. We also don't need the delalloc
...@@ -6075,18 +6066,31 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) ...@@ -6075,18 +6066,31 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
mutex_lock(&inode->delalloc_mutex); mutex_lock(&inode->delalloc_mutex);
num_bytes = ALIGN(num_bytes, fs_info->sectorsize); num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
retry:
spin_lock(&inode->lock); spin_lock(&inode->lock);
nr_extents = count_max_extents(num_bytes); reserve_extents = nr_extents = count_max_extents(num_bytes);
inode->outstanding_extents += nr_extents; btrfs_mod_outstanding_extents(inode, nr_extents);
nr_extents = 0; /*
if (inode->outstanding_extents > inode->reserved_extents) * Because we add an outstanding extent for ordered before we clear
nr_extents += inode->outstanding_extents - * delalloc we will double count our outstanding extents slightly. This
* could mean that we transiently over-reserve, which could result in an
* early ENOSPC if our timing is unlucky. Keep track of the case that
* we had a reservation underflow so we can retry if we fail.
*
* Keep in mind we can legitimately have more outstanding extents than
* reserved because of fragmentation, so only allow a retry once.
*/
if (inode->outstanding_extents >
inode->reserved_extents + nr_extents) {
reserve_extents = inode->outstanding_extents -
inode->reserved_extents; inode->reserved_extents;
underflow = true;
}
/* We always want to reserve a slot for updating the inode. */ /* We always want to reserve a slot for updating the inode. */
to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1); to_reserve = btrfs_calc_trans_metadata_size(fs_info,
reserve_extents + 1);
to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
csum_bytes = inode->csum_bytes; csum_bytes = inode->csum_bytes;
spin_unlock(&inode->lock); spin_unlock(&inode->lock);
...@@ -6111,7 +6115,7 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) ...@@ -6111,7 +6115,7 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1); to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1);
release_extra = true; release_extra = true;
} }
inode->reserved_extents += nr_extents; btrfs_mod_reserved_extents(inode, reserve_extents);
spin_unlock(&inode->lock); spin_unlock(&inode->lock);
if (delalloc_lock) if (delalloc_lock)
...@@ -6127,7 +6131,10 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) ...@@ -6127,7 +6131,10 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
out_fail: out_fail:
spin_lock(&inode->lock); spin_lock(&inode->lock);
dropped = drop_outstanding_extent(inode, num_bytes); nr_extents = count_max_extents(num_bytes);
btrfs_mod_outstanding_extents(inode, -nr_extents);
dropped = drop_over_reserved_extents(inode);
/* /*
* If the inodes csum_bytes is the same as the original * If the inodes csum_bytes is the same as the original
* csum_bytes then we know we haven't raced with any free()ers * csum_bytes then we know we haven't raced with any free()ers
...@@ -6184,6 +6191,11 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) ...@@ -6184,6 +6191,11 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
trace_btrfs_space_reservation(fs_info, "delalloc", trace_btrfs_space_reservation(fs_info, "delalloc",
btrfs_ino(inode), to_free, 0); btrfs_ino(inode), to_free, 0);
} }
if (underflow && !did_retry) {
did_retry = true;
underflow = false;
goto retry;
}
if (delalloc_lock) if (delalloc_lock)
mutex_unlock(&inode->delalloc_mutex); mutex_unlock(&inode->delalloc_mutex);
return ret; return ret;
...@@ -6191,12 +6203,12 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) ...@@ -6191,12 +6203,12 @@ int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
/** /**
* btrfs_delalloc_release_metadata - release a metadata reservation for an inode * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
* @inode: the inode to release the reservation for * @inode: the inode to release the reservation for.
* @num_bytes: the number of bytes we're releasing * @num_bytes: the number of bytes we are releasing.
* *
* This will release the metadata reservation for an inode. This can be called * This will release the metadata reservation for an inode. This can be called
* once we complete IO for a given set of bytes to release their metadata * once we complete IO for a given set of bytes to release their metadata
* reservations. * reservations, or on error for the same reason.
*/ */
void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes) void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes)
{ {
...@@ -6206,8 +6218,7 @@ void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes) ...@@ -6206,8 +6218,7 @@ void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes)
num_bytes = ALIGN(num_bytes, fs_info->sectorsize); num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
spin_lock(&inode->lock); spin_lock(&inode->lock);
dropped = drop_outstanding_extent(inode, num_bytes); dropped = drop_over_reserved_extents(inode);
if (num_bytes) if (num_bytes)
to_free = calc_csum_metadata_size(inode, num_bytes, 0); to_free = calc_csum_metadata_size(inode, num_bytes, 0);
spin_unlock(&inode->lock); spin_unlock(&inode->lock);
...@@ -6223,6 +6234,42 @@ void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes) ...@@ -6223,6 +6234,42 @@ void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes)
btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free); btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free);
} }
/**
* btrfs_delalloc_release_extents - release our outstanding_extents
* @inode: the inode to balance the reservation for.
* @num_bytes: the number of bytes we originally reserved with
*
* When we reserve space we increase outstanding_extents for the extents we may
* add. Once we've set the range as delalloc or created our ordered extents we
* have outstanding_extents to track the real usage, so we use this to free our
* temporarily tracked outstanding_extents. This _must_ be used in conjunction
* with btrfs_delalloc_reserve_metadata.
*/
void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
{
struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
unsigned num_extents;
u64 to_free;
unsigned dropped;
spin_lock(&inode->lock);
num_extents = count_max_extents(num_bytes);
btrfs_mod_outstanding_extents(inode, -num_extents);
dropped = drop_over_reserved_extents(inode);
spin_unlock(&inode->lock);
if (!dropped)
return;
if (btrfs_is_testing(fs_info))
return;
to_free = btrfs_calc_trans_metadata_size(fs_info, dropped);
trace_btrfs_space_reservation(fs_info, "delalloc", btrfs_ino(inode),
to_free, 0);
btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free);
}
/** /**
* btrfs_delalloc_reserve_space - reserve data and metadata space for * btrfs_delalloc_reserve_space - reserve data and metadata space for
* delalloc * delalloc
...@@ -6267,10 +6314,7 @@ int btrfs_delalloc_reserve_space(struct inode *inode, ...@@ -6267,10 +6314,7 @@ int btrfs_delalloc_reserve_space(struct inode *inode,
* @inode: inode we're releasing space for * @inode: inode we're releasing space for
* @start: start position of the space already reserved * @start: start position of the space already reserved
* @len: the len of the space already reserved * @len: the len of the space already reserved
* * @release_bytes: the len of the space we consumed or didn't use
* This must be matched with a call to btrfs_delalloc_reserve_space. This is
* called in the case that we don't need the metadata AND data reservations
* anymore. So if there is an error or we insert an inline extent.
* *
* This function will release the metadata space that was not used and will * This function will release the metadata space that was not used and will
* decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
...@@ -6278,7 +6322,8 @@ int btrfs_delalloc_reserve_space(struct inode *inode, ...@@ -6278,7 +6322,8 @@ int btrfs_delalloc_reserve_space(struct inode *inode,
* Also it will handle the qgroup reserved space. * Also it will handle the qgroup reserved space.
*/ */
void btrfs_delalloc_release_space(struct inode *inode, void btrfs_delalloc_release_space(struct inode *inode,
struct extent_changeset *reserved, u64 start, u64 len) struct extent_changeset *reserved,
u64 start, u64 len)
{ {
btrfs_delalloc_release_metadata(BTRFS_I(inode), len); btrfs_delalloc_release_metadata(BTRFS_I(inode), len);
btrfs_free_reserved_data_space(inode, reserved, start, len); btrfs_free_reserved_data_space(inode, reserved, start, len);
......
...@@ -1656,6 +1656,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file, ...@@ -1656,6 +1656,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
} }
} }
WARN_ON(reserve_bytes == 0);
ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
reserve_bytes); reserve_bytes);
if (ret) { if (ret) {
...@@ -1678,8 +1679,11 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file, ...@@ -1678,8 +1679,11 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
ret = prepare_pages(inode, pages, num_pages, ret = prepare_pages(inode, pages, num_pages,
pos, write_bytes, pos, write_bytes,
force_page_uptodate); force_page_uptodate);
if (ret) if (ret) {
btrfs_delalloc_release_extents(BTRFS_I(inode),
reserve_bytes);
break; break;
}
extents_locked = lock_and_cleanup_extent_if_need( extents_locked = lock_and_cleanup_extent_if_need(
BTRFS_I(inode), pages, BTRFS_I(inode), pages,
...@@ -1688,6 +1692,8 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file, ...@@ -1688,6 +1692,8 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
if (extents_locked < 0) { if (extents_locked < 0) {
if (extents_locked == -EAGAIN) if (extents_locked == -EAGAIN)
goto again; goto again;
btrfs_delalloc_release_extents(BTRFS_I(inode),
reserve_bytes);
ret = extents_locked; ret = extents_locked;
break; break;
} }
...@@ -1716,23 +1722,10 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file, ...@@ -1716,23 +1722,10 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
PAGE_SIZE); PAGE_SIZE);
} }
/*
* If we had a short copy we need to release the excess delaloc
* bytes we reserved. We need to increment outstanding_extents
* because btrfs_delalloc_release_space and
* btrfs_delalloc_release_metadata will decrement it, but
* we still have an outstanding extent for the chunk we actually
* managed to copy.
*/
if (num_sectors > dirty_sectors) { if (num_sectors > dirty_sectors) {
/* release everything except the sectors we dirtied */ /* release everything except the sectors we dirtied */
release_bytes -= dirty_sectors << release_bytes -= dirty_sectors <<
fs_info->sb->s_blocksize_bits; fs_info->sb->s_blocksize_bits;
if (copied > 0) {
spin_lock(&BTRFS_I(inode)->lock);
BTRFS_I(inode)->outstanding_extents++;
spin_unlock(&BTRFS_I(inode)->lock);
}
if (only_release_metadata) { if (only_release_metadata) {
btrfs_delalloc_release_metadata(BTRFS_I(inode), btrfs_delalloc_release_metadata(BTRFS_I(inode),
release_bytes); release_bytes);
...@@ -1758,6 +1751,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file, ...@@ -1758,6 +1751,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
unlock_extent_cached(&BTRFS_I(inode)->io_tree, unlock_extent_cached(&BTRFS_I(inode)->io_tree,
lockstart, lockend, &cached_state, lockstart, lockend, &cached_state,
GFP_NOFS); GFP_NOFS);
btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
if (ret) { if (ret) {
btrfs_drop_pages(pages, num_pages); btrfs_drop_pages(pages, num_pages);
break; break;
......
...@@ -500,11 +500,12 @@ int btrfs_save_ino_cache(struct btrfs_root *root, ...@@ -500,11 +500,12 @@ int btrfs_save_ino_cache(struct btrfs_root *root,
ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc, ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
prealloc, prealloc, &alloc_hint); prealloc, prealloc, &alloc_hint);
if (ret) { if (ret) {
btrfs_delalloc_release_metadata(BTRFS_I(inode), prealloc); btrfs_delalloc_release_extents(BTRFS_I(inode), prealloc);
goto out_put; goto out_put;
} }
ret = btrfs_write_out_ino_cache(root, trans, path, inode); ret = btrfs_write_out_ino_cache(root, trans, path, inode);
btrfs_delalloc_release_extents(BTRFS_I(inode), prealloc);
out_put: out_put:
iput(inode); iput(inode);
out_release: out_release:
......
This diff is collapsed.
...@@ -1217,6 +1217,7 @@ static int cluster_pages_for_defrag(struct inode *inode, ...@@ -1217,6 +1217,7 @@ static int cluster_pages_for_defrag(struct inode *inode,
unlock_page(pages[i]); unlock_page(pages[i]);
put_page(pages[i]); put_page(pages[i]);
} }
btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
extent_changeset_free(data_reserved); extent_changeset_free(data_reserved);
return i_done; return i_done;
out: out:
...@@ -1227,6 +1228,7 @@ static int cluster_pages_for_defrag(struct inode *inode, ...@@ -1227,6 +1228,7 @@ static int cluster_pages_for_defrag(struct inode *inode,
btrfs_delalloc_release_space(inode, data_reserved, btrfs_delalloc_release_space(inode, data_reserved,
start_index << PAGE_SHIFT, start_index << PAGE_SHIFT,
page_cnt << PAGE_SHIFT); page_cnt << PAGE_SHIFT);
btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
extent_changeset_free(data_reserved); extent_changeset_free(data_reserved);
return ret; return ret;
......
...@@ -242,6 +242,15 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, ...@@ -242,6 +242,15 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
} }
spin_unlock(&root->ordered_extent_lock); spin_unlock(&root->ordered_extent_lock);
/*
* We don't need the count_max_extents here, we can assume that all of
* that work has been done at higher layers, so this is truly the
* smallest the extent is going to get.
*/
spin_lock(&BTRFS_I(inode)->lock);
btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
spin_unlock(&BTRFS_I(inode)->lock);
return 0; return 0;
} }
...@@ -591,11 +600,19 @@ void btrfs_remove_ordered_extent(struct inode *inode, ...@@ -591,11 +600,19 @@ void btrfs_remove_ordered_extent(struct inode *inode,
{ {
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
struct btrfs_ordered_inode_tree *tree; struct btrfs_ordered_inode_tree *tree;
struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
struct btrfs_root *root = btrfs_inode->root;
struct rb_node *node; struct rb_node *node;
bool dec_pending_ordered = false; bool dec_pending_ordered = false;
tree = &BTRFS_I(inode)->ordered_tree; /* This is paired with btrfs_add_ordered_extent. */
spin_lock(&btrfs_inode->lock);
btrfs_mod_outstanding_extents(btrfs_inode, -1);
spin_unlock(&btrfs_inode->lock);
if (root != fs_info->tree_root)
btrfs_delalloc_release_metadata(btrfs_inode, entry->len);
tree = &btrfs_inode->ordered_tree;
spin_lock_irq(&tree->lock); spin_lock_irq(&tree->lock);
node = &entry->rb_node; node = &entry->rb_node;
rb_erase(node, &tree->tree); rb_erase(node, &tree->tree);
......
...@@ -3246,6 +3246,8 @@ static int relocate_file_extent_cluster(struct inode *inode, ...@@ -3246,6 +3246,8 @@ static int relocate_file_extent_cluster(struct inode *inode,
put_page(page); put_page(page);
btrfs_delalloc_release_metadata(BTRFS_I(inode), btrfs_delalloc_release_metadata(BTRFS_I(inode),
PAGE_SIZE); PAGE_SIZE);
btrfs_delalloc_release_extents(BTRFS_I(inode),
PAGE_SIZE);
ret = -EIO; ret = -EIO;
goto out; goto out;
} }
...@@ -3275,6 +3277,7 @@ static int relocate_file_extent_cluster(struct inode *inode, ...@@ -3275,6 +3277,7 @@ static int relocate_file_extent_cluster(struct inode *inode,
put_page(page); put_page(page);
index++; index++;
btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
balance_dirty_pages_ratelimited(inode->i_mapping); balance_dirty_pages_ratelimited(inode->i_mapping);
btrfs_throttle(fs_info); btrfs_throttle(fs_info);
} }
......
...@@ -968,7 +968,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -968,7 +968,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
btrfs_test_inode_set_ops(inode); btrfs_test_inode_set_ops(inode);
/* [BTRFS_MAX_EXTENT_SIZE] */ /* [BTRFS_MAX_EXTENT_SIZE] */
BTRFS_I(inode)->outstanding_extents++;
ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1, ret = btrfs_set_extent_delalloc(inode, 0, BTRFS_MAX_EXTENT_SIZE - 1,
NULL, 0); NULL, 0);
if (ret) { if (ret) {
...@@ -983,7 +982,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -983,7 +982,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
} }
/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
BTRFS_I(inode)->outstanding_extents++;
ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE, ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE,
BTRFS_MAX_EXTENT_SIZE + sectorsize - 1, BTRFS_MAX_EXTENT_SIZE + sectorsize - 1,
NULL, 0); NULL, 0);
...@@ -1003,7 +1001,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -1003,7 +1001,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
BTRFS_MAX_EXTENT_SIZE >> 1, BTRFS_MAX_EXTENT_SIZE >> 1,
(BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1, (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DIRTY |
EXTENT_UPTODATE | EXTENT_DO_ACCOUNTING, 0, 0, EXTENT_UPTODATE, 0, 0,
NULL, GFP_KERNEL); NULL, GFP_KERNEL);
if (ret) { if (ret) {
test_msg("clear_extent_bit returned %d\n", ret); test_msg("clear_extent_bit returned %d\n", ret);
...@@ -1017,7 +1015,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -1017,7 +1015,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
} }
/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ /* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */
BTRFS_I(inode)->outstanding_extents++;
ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1, ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1,
(BTRFS_MAX_EXTENT_SIZE >> 1) (BTRFS_MAX_EXTENT_SIZE >> 1)
+ sectorsize - 1, + sectorsize - 1,
...@@ -1035,12 +1032,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -1035,12 +1032,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
/* /*
* [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize] * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize]
*
* I'm artificially adding 2 to outstanding_extents because in the
* buffered IO case we'd add things up as we go, but I don't feel like
* doing that here, this isn't the interesting case we want to test.
*/ */
BTRFS_I(inode)->outstanding_extents += 2;
ret = btrfs_set_extent_delalloc(inode, ret = btrfs_set_extent_delalloc(inode,
BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize, BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize,
(BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1, (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
...@@ -1059,7 +1051,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -1059,7 +1051,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
/* /*
* [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize] * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize]
*/ */
BTRFS_I(inode)->outstanding_extents++;
ret = btrfs_set_extent_delalloc(inode, ret = btrfs_set_extent_delalloc(inode,
BTRFS_MAX_EXTENT_SIZE + sectorsize, BTRFS_MAX_EXTENT_SIZE + sectorsize,
BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL, 0); BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL, 0);
...@@ -1079,7 +1070,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -1079,7 +1070,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
BTRFS_MAX_EXTENT_SIZE + sectorsize, BTRFS_MAX_EXTENT_SIZE + sectorsize,
BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1,
EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_DELALLOC |
EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0, EXTENT_UPTODATE, 0, 0,
NULL, GFP_KERNEL); NULL, GFP_KERNEL);
if (ret) { if (ret) {
test_msg("clear_extent_bit returned %d\n", ret); test_msg("clear_extent_bit returned %d\n", ret);
...@@ -1096,7 +1087,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -1096,7 +1087,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
* Refill the hole again just for good measure, because I thought it * Refill the hole again just for good measure, because I thought it
* might fail and I'd rather satisfy my paranoia at this point. * might fail and I'd rather satisfy my paranoia at this point.
*/ */
BTRFS_I(inode)->outstanding_extents++;
ret = btrfs_set_extent_delalloc(inode, ret = btrfs_set_extent_delalloc(inode,
BTRFS_MAX_EXTENT_SIZE + sectorsize, BTRFS_MAX_EXTENT_SIZE + sectorsize,
BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL, 0); BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize - 1, NULL, 0);
...@@ -1114,7 +1104,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -1114,7 +1104,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
/* Empty */ /* Empty */
ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1, ret = clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_DELALLOC |
EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0, EXTENT_UPTODATE, 0, 0,
NULL, GFP_KERNEL); NULL, GFP_KERNEL);
if (ret) { if (ret) {
test_msg("clear_extent_bit returned %d\n", ret); test_msg("clear_extent_bit returned %d\n", ret);
...@@ -1131,7 +1121,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) ...@@ -1131,7 +1121,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
if (ret) if (ret)
clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1, clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_DELALLOC |
EXTENT_DO_ACCOUNTING | EXTENT_UPTODATE, 0, 0, EXTENT_UPTODATE, 0, 0,
NULL, GFP_KERNEL); NULL, GFP_KERNEL);
iput(inode); iput(inode);
btrfs_free_dummy_root(root); btrfs_free_dummy_root(root);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment