xref: /openbmc/linux/fs/btrfs/delalloc-space.c (revision bcacf5f3)
186736342SJosef Bacik // SPDX-License-Identifier: GPL-2.0
286736342SJosef Bacik 
386736342SJosef Bacik #include "ctree.h"
486736342SJosef Bacik #include "delalloc-space.h"
586736342SJosef Bacik #include "block-rsv.h"
686736342SJosef Bacik #include "btrfs_inode.h"
786736342SJosef Bacik #include "space-info.h"
886736342SJosef Bacik #include "transaction.h"
986736342SJosef Bacik #include "qgroup.h"
1007730d87SJosef Bacik #include "block-group.h"
1186736342SJosef Bacik 
1286736342SJosef Bacik int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
1386736342SJosef Bacik {
1486736342SJosef Bacik 	struct btrfs_root *root = inode->root;
1586736342SJosef Bacik 	struct btrfs_fs_info *fs_info = root->fs_info;
1686736342SJosef Bacik 	struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
1786736342SJosef Bacik 	u64 used;
1886736342SJosef Bacik 	int ret = 0;
1986736342SJosef Bacik 	int need_commit = 2;
2086736342SJosef Bacik 	int have_pinned_space;
2186736342SJosef Bacik 
2286736342SJosef Bacik 	/* Make sure bytes are sectorsize aligned */
2386736342SJosef Bacik 	bytes = ALIGN(bytes, fs_info->sectorsize);
2486736342SJosef Bacik 
2586736342SJosef Bacik 	if (btrfs_is_free_space_inode(inode)) {
2686736342SJosef Bacik 		need_commit = 0;
2786736342SJosef Bacik 		ASSERT(current->journal_info);
2886736342SJosef Bacik 	}
2986736342SJosef Bacik 
3086736342SJosef Bacik again:
3186736342SJosef Bacik 	/* Make sure we have enough space to handle the data first */
3286736342SJosef Bacik 	spin_lock(&data_sinfo->lock);
3386736342SJosef Bacik 	used = btrfs_space_info_used(data_sinfo, true);
3486736342SJosef Bacik 
3586736342SJosef Bacik 	if (used + bytes > data_sinfo->total_bytes) {
3686736342SJosef Bacik 		struct btrfs_trans_handle *trans;
3786736342SJosef Bacik 
3886736342SJosef Bacik 		/*
3986736342SJosef Bacik 		 * If we don't have enough free bytes in this space then we need
4086736342SJosef Bacik 		 * to alloc a new chunk.
4186736342SJosef Bacik 		 */
4286736342SJosef Bacik 		if (!data_sinfo->full) {
4386736342SJosef Bacik 			u64 alloc_target;
4486736342SJosef Bacik 
4586736342SJosef Bacik 			data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
4686736342SJosef Bacik 			spin_unlock(&data_sinfo->lock);
4786736342SJosef Bacik 
4886736342SJosef Bacik 			alloc_target = btrfs_data_alloc_profile(fs_info);
4986736342SJosef Bacik 			/*
5086736342SJosef Bacik 			 * It is ugly that we don't call nolock join
5186736342SJosef Bacik 			 * transaction for the free space inode case here.
5286736342SJosef Bacik 			 * But it is safe because we only do the data space
5386736342SJosef Bacik 			 * reservation for the free space cache in the
5486736342SJosef Bacik 			 * transaction context, the common join transaction
5586736342SJosef Bacik 			 * just increase the counter of the current transaction
5686736342SJosef Bacik 			 * handler, doesn't try to acquire the trans_lock of
5786736342SJosef Bacik 			 * the fs.
5886736342SJosef Bacik 			 */
5986736342SJosef Bacik 			trans = btrfs_join_transaction(root);
6086736342SJosef Bacik 			if (IS_ERR(trans))
6186736342SJosef Bacik 				return PTR_ERR(trans);
6286736342SJosef Bacik 
6386736342SJosef Bacik 			ret = btrfs_chunk_alloc(trans, alloc_target,
6486736342SJosef Bacik 						CHUNK_ALLOC_NO_FORCE);
6586736342SJosef Bacik 			btrfs_end_transaction(trans);
6686736342SJosef Bacik 			if (ret < 0) {
6786736342SJosef Bacik 				if (ret != -ENOSPC)
6886736342SJosef Bacik 					return ret;
6986736342SJosef Bacik 				else {
7086736342SJosef Bacik 					have_pinned_space = 1;
7186736342SJosef Bacik 					goto commit_trans;
7286736342SJosef Bacik 				}
7386736342SJosef Bacik 			}
7486736342SJosef Bacik 
7586736342SJosef Bacik 			goto again;
7686736342SJosef Bacik 		}
7786736342SJosef Bacik 
7886736342SJosef Bacik 		/*
7986736342SJosef Bacik 		 * If we don't have enough pinned space to deal with this
8086736342SJosef Bacik 		 * allocation, and no removed chunk in current transaction,
8186736342SJosef Bacik 		 * don't bother committing the transaction.
8286736342SJosef Bacik 		 */
8386736342SJosef Bacik 		have_pinned_space = __percpu_counter_compare(
8486736342SJosef Bacik 			&data_sinfo->total_bytes_pinned,
8586736342SJosef Bacik 			used + bytes - data_sinfo->total_bytes,
8686736342SJosef Bacik 			BTRFS_TOTAL_BYTES_PINNED_BATCH);
8786736342SJosef Bacik 		spin_unlock(&data_sinfo->lock);
8886736342SJosef Bacik 
8986736342SJosef Bacik 		/* Commit the current transaction and try again */
9086736342SJosef Bacik commit_trans:
9186736342SJosef Bacik 		if (need_commit) {
9286736342SJosef Bacik 			need_commit--;
9386736342SJosef Bacik 
9486736342SJosef Bacik 			if (need_commit > 0) {
9586736342SJosef Bacik 				btrfs_start_delalloc_roots(fs_info, -1);
9686736342SJosef Bacik 				btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
9786736342SJosef Bacik 							 (u64)-1);
9886736342SJosef Bacik 			}
9986736342SJosef Bacik 
10086736342SJosef Bacik 			trans = btrfs_join_transaction(root);
10186736342SJosef Bacik 			if (IS_ERR(trans))
10286736342SJosef Bacik 				return PTR_ERR(trans);
10386736342SJosef Bacik 			if (have_pinned_space >= 0 ||
10486736342SJosef Bacik 			    test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
10586736342SJosef Bacik 				     &trans->transaction->flags) ||
10686736342SJosef Bacik 			    need_commit > 0) {
10786736342SJosef Bacik 				ret = btrfs_commit_transaction(trans);
10886736342SJosef Bacik 				if (ret)
10986736342SJosef Bacik 					return ret;
11086736342SJosef Bacik 				/*
11186736342SJosef Bacik 				 * The cleaner kthread might still be doing iput
11286736342SJosef Bacik 				 * operations. Wait for it to finish so that
11386736342SJosef Bacik 				 * more space is released.  We don't need to
11486736342SJosef Bacik 				 * explicitly run the delayed iputs here because
11586736342SJosef Bacik 				 * the commit_transaction would have woken up
11686736342SJosef Bacik 				 * the cleaner.
11786736342SJosef Bacik 				 */
11886736342SJosef Bacik 				ret = btrfs_wait_on_delayed_iputs(fs_info);
11986736342SJosef Bacik 				if (ret)
12086736342SJosef Bacik 					return ret;
12186736342SJosef Bacik 				goto again;
12286736342SJosef Bacik 			} else {
12386736342SJosef Bacik 				btrfs_end_transaction(trans);
12486736342SJosef Bacik 			}
12586736342SJosef Bacik 		}
12686736342SJosef Bacik 
12786736342SJosef Bacik 		trace_btrfs_space_reservation(fs_info,
12886736342SJosef Bacik 					      "space_info:enospc",
12986736342SJosef Bacik 					      data_sinfo->flags, bytes, 1);
13086736342SJosef Bacik 		return -ENOSPC;
13186736342SJosef Bacik 	}
13286736342SJosef Bacik 	btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, bytes);
13386736342SJosef Bacik 	trace_btrfs_space_reservation(fs_info, "space_info",
13486736342SJosef Bacik 				      data_sinfo->flags, bytes, 1);
13586736342SJosef Bacik 	spin_unlock(&data_sinfo->lock);
13686736342SJosef Bacik 
13786736342SJosef Bacik 	return 0;
13886736342SJosef Bacik }
13986736342SJosef Bacik 
14086736342SJosef Bacik int btrfs_check_data_free_space(struct inode *inode,
14186736342SJosef Bacik 			struct extent_changeset **reserved, u64 start, u64 len)
14286736342SJosef Bacik {
14386736342SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
14486736342SJosef Bacik 	int ret;
14586736342SJosef Bacik 
14686736342SJosef Bacik 	/* align the range */
14786736342SJosef Bacik 	len = round_up(start + len, fs_info->sectorsize) -
14886736342SJosef Bacik 	      round_down(start, fs_info->sectorsize);
14986736342SJosef Bacik 	start = round_down(start, fs_info->sectorsize);
15086736342SJosef Bacik 
15186736342SJosef Bacik 	ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
15286736342SJosef Bacik 	if (ret < 0)
15386736342SJosef Bacik 		return ret;
15486736342SJosef Bacik 
15586736342SJosef Bacik 	/* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
15686736342SJosef Bacik 	ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
15786736342SJosef Bacik 	if (ret < 0)
15886736342SJosef Bacik 		btrfs_free_reserved_data_space_noquota(inode, start, len);
15986736342SJosef Bacik 	else
16086736342SJosef Bacik 		ret = 0;
16186736342SJosef Bacik 	return ret;
16286736342SJosef Bacik }
16386736342SJosef Bacik 
16486736342SJosef Bacik /*
16586736342SJosef Bacik  * Called if we need to clear a data reservation for this inode
16686736342SJosef Bacik  * Normally in a error case.
16786736342SJosef Bacik  *
16886736342SJosef Bacik  * This one will *NOT* use accurate qgroup reserved space API, just for case
16986736342SJosef Bacik  * which we can't sleep and is sure it won't affect qgroup reserved space.
17086736342SJosef Bacik  * Like clear_bit_hook().
17186736342SJosef Bacik  */
17286736342SJosef Bacik void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
17386736342SJosef Bacik 					    u64 len)
17486736342SJosef Bacik {
17586736342SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
17686736342SJosef Bacik 	struct btrfs_space_info *data_sinfo;
17786736342SJosef Bacik 
17886736342SJosef Bacik 	/* Make sure the range is aligned to sectorsize */
17986736342SJosef Bacik 	len = round_up(start + len, fs_info->sectorsize) -
18086736342SJosef Bacik 	      round_down(start, fs_info->sectorsize);
18186736342SJosef Bacik 	start = round_down(start, fs_info->sectorsize);
18286736342SJosef Bacik 
18386736342SJosef Bacik 	data_sinfo = fs_info->data_sinfo;
18486736342SJosef Bacik 	spin_lock(&data_sinfo->lock);
18586736342SJosef Bacik 	btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, -len);
18686736342SJosef Bacik 	trace_btrfs_space_reservation(fs_info, "space_info",
18786736342SJosef Bacik 				      data_sinfo->flags, len, 0);
18886736342SJosef Bacik 	spin_unlock(&data_sinfo->lock);
18986736342SJosef Bacik }
19086736342SJosef Bacik 
19186736342SJosef Bacik /*
19286736342SJosef Bacik  * Called if we need to clear a data reservation for this inode
19386736342SJosef Bacik  * Normally in a error case.
19486736342SJosef Bacik  *
19586736342SJosef Bacik  * This one will handle the per-inode data rsv map for accurate reserved
19686736342SJosef Bacik  * space framework.
19786736342SJosef Bacik  */
19886736342SJosef Bacik void btrfs_free_reserved_data_space(struct inode *inode,
19986736342SJosef Bacik 			struct extent_changeset *reserved, u64 start, u64 len)
20086736342SJosef Bacik {
20186736342SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
20286736342SJosef Bacik 
20386736342SJosef Bacik 	/* Make sure the range is aligned to sectorsize */
20486736342SJosef Bacik 	len = round_up(start + len, root->fs_info->sectorsize) -
20586736342SJosef Bacik 	      round_down(start, root->fs_info->sectorsize);
20686736342SJosef Bacik 	start = round_down(start, root->fs_info->sectorsize);
20786736342SJosef Bacik 
20886736342SJosef Bacik 	btrfs_free_reserved_data_space_noquota(inode, start, len);
20986736342SJosef Bacik 	btrfs_qgroup_free_data(inode, reserved, start, len);
21086736342SJosef Bacik }
21186736342SJosef Bacik 
21286736342SJosef Bacik /**
21386736342SJosef Bacik  * btrfs_inode_rsv_release - release any excessive reservation.
21486736342SJosef Bacik  * @inode - the inode we need to release from.
21586736342SJosef Bacik  * @qgroup_free - free or convert qgroup meta.
21686736342SJosef Bacik  *   Unlike normal operation, qgroup meta reservation needs to know if we are
21786736342SJosef Bacik  *   freeing qgroup reservation or just converting it into per-trans.  Normally
21886736342SJosef Bacik  *   @qgroup_free is true for error handling, and false for normal release.
21986736342SJosef Bacik  *
22086736342SJosef Bacik  * This is the same as btrfs_block_rsv_release, except that it handles the
22186736342SJosef Bacik  * tracepoint for the reservation.
22286736342SJosef Bacik  */
22386736342SJosef Bacik static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
22486736342SJosef Bacik {
22586736342SJosef Bacik 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
22686736342SJosef Bacik 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
22786736342SJosef Bacik 	u64 released = 0;
22886736342SJosef Bacik 	u64 qgroup_to_release = 0;
22986736342SJosef Bacik 
23086736342SJosef Bacik 	/*
23186736342SJosef Bacik 	 * Since we statically set the block_rsv->size we just want to say we
23286736342SJosef Bacik 	 * are releasing 0 bytes, and then we'll just get the reservation over
23386736342SJosef Bacik 	 * the size free'd.
23486736342SJosef Bacik 	 */
23586736342SJosef Bacik 	released = __btrfs_block_rsv_release(fs_info, block_rsv, 0,
23686736342SJosef Bacik 					     &qgroup_to_release);
23786736342SJosef Bacik 	if (released > 0)
23886736342SJosef Bacik 		trace_btrfs_space_reservation(fs_info, "delalloc",
23986736342SJosef Bacik 					      btrfs_ino(inode), released, 0);
24086736342SJosef Bacik 	if (qgroup_free)
24186736342SJosef Bacik 		btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
24286736342SJosef Bacik 	else
24386736342SJosef Bacik 		btrfs_qgroup_convert_reserved_meta(inode->root,
24486736342SJosef Bacik 						   qgroup_to_release);
24586736342SJosef Bacik }
24686736342SJosef Bacik 
24786736342SJosef Bacik static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
24886736342SJosef Bacik 						 struct btrfs_inode *inode)
24986736342SJosef Bacik {
25086736342SJosef Bacik 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
25186736342SJosef Bacik 	u64 reserve_size = 0;
25286736342SJosef Bacik 	u64 qgroup_rsv_size = 0;
25386736342SJosef Bacik 	u64 csum_leaves;
25486736342SJosef Bacik 	unsigned outstanding_extents;
25586736342SJosef Bacik 
25686736342SJosef Bacik 	lockdep_assert_held(&inode->lock);
25786736342SJosef Bacik 	outstanding_extents = inode->outstanding_extents;
258bcacf5f3SJosef Bacik 
259bcacf5f3SJosef Bacik 	/*
260bcacf5f3SJosef Bacik 	 * Insert size for the number of outstanding extents, 1 normal size for
261bcacf5f3SJosef Bacik 	 * updating the inode.
262bcacf5f3SJosef Bacik 	 */
263bcacf5f3SJosef Bacik 	if (outstanding_extents) {
2642bd36e7bSJosef Bacik 		reserve_size = btrfs_calc_insert_metadata_size(fs_info,
265bcacf5f3SJosef Bacik 						outstanding_extents);
266bcacf5f3SJosef Bacik 		reserve_size += btrfs_calc_metadata_size(fs_info, 1);
267bcacf5f3SJosef Bacik 	}
26886736342SJosef Bacik 	csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
26986736342SJosef Bacik 						 inode->csum_bytes);
2702bd36e7bSJosef Bacik 	reserve_size += btrfs_calc_insert_metadata_size(fs_info,
27186736342SJosef Bacik 							csum_leaves);
27286736342SJosef Bacik 	/*
27386736342SJosef Bacik 	 * For qgroup rsv, the calculation is very simple:
27486736342SJosef Bacik 	 * account one nodesize for each outstanding extent
27586736342SJosef Bacik 	 *
27686736342SJosef Bacik 	 * This is overestimating in most cases.
27786736342SJosef Bacik 	 */
27886736342SJosef Bacik 	qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
27986736342SJosef Bacik 
28086736342SJosef Bacik 	spin_lock(&block_rsv->lock);
28186736342SJosef Bacik 	block_rsv->size = reserve_size;
28286736342SJosef Bacik 	block_rsv->qgroup_rsv_size = qgroup_rsv_size;
28386736342SJosef Bacik 	spin_unlock(&block_rsv->lock);
28486736342SJosef Bacik }
28586736342SJosef Bacik 
28686736342SJosef Bacik static void calc_inode_reservations(struct btrfs_fs_info *fs_info,
28786736342SJosef Bacik 				    u64 num_bytes, u64 *meta_reserve,
28886736342SJosef Bacik 				    u64 *qgroup_reserve)
28986736342SJosef Bacik {
29086736342SJosef Bacik 	u64 nr_extents = count_max_extents(num_bytes);
29186736342SJosef Bacik 	u64 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, num_bytes);
292bcacf5f3SJosef Bacik 	u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
29386736342SJosef Bacik 
2942bd36e7bSJosef Bacik 	*meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
295bcacf5f3SJosef Bacik 						nr_extents + csum_leaves);
296bcacf5f3SJosef Bacik 
297bcacf5f3SJosef Bacik 	/*
298bcacf5f3SJosef Bacik 	 * finish_ordered_io has to update the inode, so add the space required
299bcacf5f3SJosef Bacik 	 * for an inode update.
300bcacf5f3SJosef Bacik 	 */
301bcacf5f3SJosef Bacik 	*meta_reserve += inode_update;
30286736342SJosef Bacik 	*qgroup_reserve = nr_extents * fs_info->nodesize;
30386736342SJosef Bacik }
30486736342SJosef Bacik 
30586736342SJosef Bacik int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
30686736342SJosef Bacik {
30786736342SJosef Bacik 	struct btrfs_root *root = inode->root;
30886736342SJosef Bacik 	struct btrfs_fs_info *fs_info = root->fs_info;
30986736342SJosef Bacik 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
31086736342SJosef Bacik 	u64 meta_reserve, qgroup_reserve;
31186736342SJosef Bacik 	unsigned nr_extents;
31286736342SJosef Bacik 	enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
31386736342SJosef Bacik 	int ret = 0;
31486736342SJosef Bacik 	bool delalloc_lock = true;
31586736342SJosef Bacik 
31686736342SJosef Bacik 	/*
31786736342SJosef Bacik 	 * If we are a free space inode we need to not flush since we will be in
31886736342SJosef Bacik 	 * the middle of a transaction commit.  We also don't need the delalloc
31986736342SJosef Bacik 	 * mutex since we won't race with anybody.  We need this mostly to make
32086736342SJosef Bacik 	 * lockdep shut its filthy mouth.
32186736342SJosef Bacik 	 *
32286736342SJosef Bacik 	 * If we have a transaction open (can happen if we call truncate_block
32386736342SJosef Bacik 	 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
32486736342SJosef Bacik 	 */
32586736342SJosef Bacik 	if (btrfs_is_free_space_inode(inode)) {
32686736342SJosef Bacik 		flush = BTRFS_RESERVE_NO_FLUSH;
32786736342SJosef Bacik 		delalloc_lock = false;
32886736342SJosef Bacik 	} else {
32986736342SJosef Bacik 		if (current->journal_info)
33086736342SJosef Bacik 			flush = BTRFS_RESERVE_FLUSH_LIMIT;
33186736342SJosef Bacik 
33286736342SJosef Bacik 		if (btrfs_transaction_in_commit(fs_info))
33386736342SJosef Bacik 			schedule_timeout(1);
33486736342SJosef Bacik 	}
33586736342SJosef Bacik 
33686736342SJosef Bacik 	if (delalloc_lock)
33786736342SJosef Bacik 		mutex_lock(&inode->delalloc_mutex);
33886736342SJosef Bacik 
33986736342SJosef Bacik 	num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
34086736342SJosef Bacik 
34186736342SJosef Bacik 	/*
34286736342SJosef Bacik 	 * We always want to do it this way, every other way is wrong and ends
34386736342SJosef Bacik 	 * in tears.  Pre-reserving the amount we are going to add will always
34486736342SJosef Bacik 	 * be the right way, because otherwise if we have enough parallelism we
34586736342SJosef Bacik 	 * could end up with thousands of inodes all holding little bits of
34686736342SJosef Bacik 	 * reservations they were able to make previously and the only way to
34786736342SJosef Bacik 	 * reclaim that space is to ENOSPC out the operations and clear
34886736342SJosef Bacik 	 * everything out and try again, which is bad.  This way we just
34986736342SJosef Bacik 	 * over-reserve slightly, and clean up the mess when we are done.
35086736342SJosef Bacik 	 */
35186736342SJosef Bacik 	calc_inode_reservations(fs_info, num_bytes, &meta_reserve,
35286736342SJosef Bacik 				&qgroup_reserve);
35386736342SJosef Bacik 	ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true);
35486736342SJosef Bacik 	if (ret)
35586736342SJosef Bacik 		goto out_fail;
35686736342SJosef Bacik 	ret = btrfs_reserve_metadata_bytes(root, block_rsv, meta_reserve, flush);
35786736342SJosef Bacik 	if (ret)
35886736342SJosef Bacik 		goto out_qgroup;
35986736342SJosef Bacik 
36086736342SJosef Bacik 	/*
36186736342SJosef Bacik 	 * Now we need to update our outstanding extents and csum bytes _first_
36286736342SJosef Bacik 	 * and then add the reservation to the block_rsv.  This keeps us from
36386736342SJosef Bacik 	 * racing with an ordered completion or some such that would think it
36486736342SJosef Bacik 	 * needs to free the reservation we just made.
36586736342SJosef Bacik 	 */
36686736342SJosef Bacik 	spin_lock(&inode->lock);
36786736342SJosef Bacik 	nr_extents = count_max_extents(num_bytes);
36886736342SJosef Bacik 	btrfs_mod_outstanding_extents(inode, nr_extents);
36986736342SJosef Bacik 	inode->csum_bytes += num_bytes;
37086736342SJosef Bacik 	btrfs_calculate_inode_block_rsv_size(fs_info, inode);
37186736342SJosef Bacik 	spin_unlock(&inode->lock);
37286736342SJosef Bacik 
37386736342SJosef Bacik 	/* Now we can safely add our space to our block rsv */
37486736342SJosef Bacik 	btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
37586736342SJosef Bacik 	trace_btrfs_space_reservation(root->fs_info, "delalloc",
37686736342SJosef Bacik 				      btrfs_ino(inode), meta_reserve, 1);
37786736342SJosef Bacik 
37886736342SJosef Bacik 	spin_lock(&block_rsv->lock);
37986736342SJosef Bacik 	block_rsv->qgroup_rsv_reserved += qgroup_reserve;
38086736342SJosef Bacik 	spin_unlock(&block_rsv->lock);
38186736342SJosef Bacik 
38286736342SJosef Bacik 	if (delalloc_lock)
38386736342SJosef Bacik 		mutex_unlock(&inode->delalloc_mutex);
38486736342SJosef Bacik 	return 0;
38586736342SJosef Bacik out_qgroup:
38686736342SJosef Bacik 	btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
38786736342SJosef Bacik out_fail:
38886736342SJosef Bacik 	btrfs_inode_rsv_release(inode, true);
38986736342SJosef Bacik 	if (delalloc_lock)
39086736342SJosef Bacik 		mutex_unlock(&inode->delalloc_mutex);
39186736342SJosef Bacik 	return ret;
39286736342SJosef Bacik }
39386736342SJosef Bacik 
39486736342SJosef Bacik /**
39586736342SJosef Bacik  * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
39686736342SJosef Bacik  * @inode: the inode to release the reservation for.
39786736342SJosef Bacik  * @num_bytes: the number of bytes we are releasing.
39886736342SJosef Bacik  * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
39986736342SJosef Bacik  *
40086736342SJosef Bacik  * This will release the metadata reservation for an inode.  This can be called
40186736342SJosef Bacik  * once we complete IO for a given set of bytes to release their metadata
40286736342SJosef Bacik  * reservations, or on error for the same reason.
40386736342SJosef Bacik  */
40486736342SJosef Bacik void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
40586736342SJosef Bacik 				     bool qgroup_free)
40686736342SJosef Bacik {
40786736342SJosef Bacik 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
40886736342SJosef Bacik 
40986736342SJosef Bacik 	num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
41086736342SJosef Bacik 	spin_lock(&inode->lock);
41186736342SJosef Bacik 	inode->csum_bytes -= num_bytes;
41286736342SJosef Bacik 	btrfs_calculate_inode_block_rsv_size(fs_info, inode);
41386736342SJosef Bacik 	spin_unlock(&inode->lock);
41486736342SJosef Bacik 
41586736342SJosef Bacik 	if (btrfs_is_testing(fs_info))
41686736342SJosef Bacik 		return;
41786736342SJosef Bacik 
41886736342SJosef Bacik 	btrfs_inode_rsv_release(inode, qgroup_free);
41986736342SJosef Bacik }
42086736342SJosef Bacik 
42186736342SJosef Bacik /**
42286736342SJosef Bacik  * btrfs_delalloc_release_extents - release our outstanding_extents
42386736342SJosef Bacik  * @inode: the inode to balance the reservation for.
42486736342SJosef Bacik  * @num_bytes: the number of bytes we originally reserved with
42586736342SJosef Bacik  * @qgroup_free: do we need to free qgroup meta reservation or convert them.
42686736342SJosef Bacik  *
42786736342SJosef Bacik  * When we reserve space we increase outstanding_extents for the extents we may
42886736342SJosef Bacik  * add.  Once we've set the range as delalloc or created our ordered extents we
42986736342SJosef Bacik  * have outstanding_extents to track the real usage, so we use this to free our
43086736342SJosef Bacik  * temporarily tracked outstanding_extents.  This _must_ be used in conjunction
43186736342SJosef Bacik  * with btrfs_delalloc_reserve_metadata.
43286736342SJosef Bacik  */
43386736342SJosef Bacik void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
43486736342SJosef Bacik 				    bool qgroup_free)
43586736342SJosef Bacik {
43686736342SJosef Bacik 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
43786736342SJosef Bacik 	unsigned num_extents;
43886736342SJosef Bacik 
43986736342SJosef Bacik 	spin_lock(&inode->lock);
44086736342SJosef Bacik 	num_extents = count_max_extents(num_bytes);
44186736342SJosef Bacik 	btrfs_mod_outstanding_extents(inode, -num_extents);
44286736342SJosef Bacik 	btrfs_calculate_inode_block_rsv_size(fs_info, inode);
44386736342SJosef Bacik 	spin_unlock(&inode->lock);
44486736342SJosef Bacik 
44586736342SJosef Bacik 	if (btrfs_is_testing(fs_info))
44686736342SJosef Bacik 		return;
44786736342SJosef Bacik 
44886736342SJosef Bacik 	btrfs_inode_rsv_release(inode, qgroup_free);
44986736342SJosef Bacik }
45086736342SJosef Bacik 
45186736342SJosef Bacik /**
45286736342SJosef Bacik  * btrfs_delalloc_reserve_space - reserve data and metadata space for
45386736342SJosef Bacik  * delalloc
45486736342SJosef Bacik  * @inode: inode we're writing to
45586736342SJosef Bacik  * @start: start range we are writing to
45686736342SJosef Bacik  * @len: how long the range we are writing to
45786736342SJosef Bacik  * @reserved: mandatory parameter, record actually reserved qgroup ranges of
45886736342SJosef Bacik  * 	      current reservation.
45986736342SJosef Bacik  *
46086736342SJosef Bacik  * This will do the following things
46186736342SJosef Bacik  *
46286736342SJosef Bacik  * - reserve space in data space info for num bytes
46386736342SJosef Bacik  *   and reserve precious corresponding qgroup space
46486736342SJosef Bacik  *   (Done in check_data_free_space)
46586736342SJosef Bacik  *
46686736342SJosef Bacik  * - reserve space for metadata space, based on the number of outstanding
46786736342SJosef Bacik  *   extents and how much csums will be needed
46886736342SJosef Bacik  *   also reserve metadata space in a per root over-reserve method.
46986736342SJosef Bacik  * - add to the inodes->delalloc_bytes
47086736342SJosef Bacik  * - add it to the fs_info's delalloc inodes list.
47186736342SJosef Bacik  *   (Above 3 all done in delalloc_reserve_metadata)
47286736342SJosef Bacik  *
47386736342SJosef Bacik  * Return 0 for success
47486736342SJosef Bacik  * Return <0 for error(-ENOSPC or -EQUOT)
47586736342SJosef Bacik  */
47686736342SJosef Bacik int btrfs_delalloc_reserve_space(struct inode *inode,
47786736342SJosef Bacik 			struct extent_changeset **reserved, u64 start, u64 len)
47886736342SJosef Bacik {
47986736342SJosef Bacik 	int ret;
48086736342SJosef Bacik 
48186736342SJosef Bacik 	ret = btrfs_check_data_free_space(inode, reserved, start, len);
48286736342SJosef Bacik 	if (ret < 0)
48386736342SJosef Bacik 		return ret;
48486736342SJosef Bacik 	ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
48586736342SJosef Bacik 	if (ret < 0)
48686736342SJosef Bacik 		btrfs_free_reserved_data_space(inode, *reserved, start, len);
48786736342SJosef Bacik 	return ret;
48886736342SJosef Bacik }
48986736342SJosef Bacik 
49086736342SJosef Bacik /**
49186736342SJosef Bacik  * btrfs_delalloc_release_space - release data and metadata space for delalloc
49286736342SJosef Bacik  * @inode: inode we're releasing space for
49386736342SJosef Bacik  * @start: start position of the space already reserved
49486736342SJosef Bacik  * @len: the len of the space already reserved
49586736342SJosef Bacik  * @release_bytes: the len of the space we consumed or didn't use
49686736342SJosef Bacik  *
49786736342SJosef Bacik  * This function will release the metadata space that was not used and will
49886736342SJosef Bacik  * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
49986736342SJosef Bacik  * list if there are no delalloc bytes left.
50086736342SJosef Bacik  * Also it will handle the qgroup reserved space.
50186736342SJosef Bacik  */
50286736342SJosef Bacik void btrfs_delalloc_release_space(struct inode *inode,
50386736342SJosef Bacik 				  struct extent_changeset *reserved,
50486736342SJosef Bacik 				  u64 start, u64 len, bool qgroup_free)
50586736342SJosef Bacik {
50686736342SJosef Bacik 	btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
50786736342SJosef Bacik 	btrfs_free_reserved_data_space(inode, reserved, start, len);
50886736342SJosef Bacik }
509