xref: /openbmc/linux/fs/btrfs/delalloc-space.c (revision 9aa06c76)
186736342SJosef Bacik // SPDX-License-Identifier: GPL-2.0
286736342SJosef Bacik 
39b569ea0SJosef Bacik #include "messages.h"
486736342SJosef Bacik #include "ctree.h"
586736342SJosef Bacik #include "delalloc-space.h"
686736342SJosef Bacik #include "block-rsv.h"
786736342SJosef Bacik #include "btrfs_inode.h"
886736342SJosef Bacik #include "space-info.h"
986736342SJosef Bacik #include "transaction.h"
1086736342SJosef Bacik #include "qgroup.h"
1107730d87SJosef Bacik #include "block-group.h"
12ec8eb376SJosef Bacik #include "fs.h"
1386736342SJosef Bacik 
146f4ad559SJosef Bacik /*
156f4ad559SJosef Bacik  * HOW DOES THIS WORK
166f4ad559SJosef Bacik  *
176f4ad559SJosef Bacik  * There are two stages to data reservations, one for data and one for metadata
186f4ad559SJosef Bacik  * to handle the new extents and checksums generated by writing data.
196f4ad559SJosef Bacik  *
206f4ad559SJosef Bacik  *
216f4ad559SJosef Bacik  * DATA RESERVATION
226f4ad559SJosef Bacik  *   The general flow of the data reservation is as follows
236f4ad559SJosef Bacik  *
246f4ad559SJosef Bacik  *   -> Reserve
256f4ad559SJosef Bacik  *     We call into btrfs_reserve_data_bytes() for the user request bytes that
266f4ad559SJosef Bacik  *     they wish to write.  We make this reservation and add it to
276f4ad559SJosef Bacik  *     space_info->bytes_may_use.  We set EXTENT_DELALLOC on the inode io_tree
286f4ad559SJosef Bacik  *     for the range and carry on if this is buffered, or follow up trying to
296f4ad559SJosef Bacik  *     make a real allocation if we are pre-allocating or doing O_DIRECT.
306f4ad559SJosef Bacik  *
316f4ad559SJosef Bacik  *   -> Use
326f4ad559SJosef Bacik  *     At writepages()/prealloc/O_DIRECT time we will call into
336f4ad559SJosef Bacik  *     btrfs_reserve_extent() for some part or all of this range of bytes.  We
346f4ad559SJosef Bacik  *     will make the allocation and subtract space_info->bytes_may_use by the
356f4ad559SJosef Bacik  *     original requested length and increase the space_info->bytes_reserved by
366f4ad559SJosef Bacik  *     the allocated length.  This distinction is important because compression
376f4ad559SJosef Bacik  *     may allocate a smaller on disk extent than we previously reserved.
386f4ad559SJosef Bacik  *
396f4ad559SJosef Bacik  *   -> Allocation
406f4ad559SJosef Bacik  *     finish_ordered_io() will insert the new file extent item for this range,
416f4ad559SJosef Bacik  *     and then add a delayed ref update for the extent tree.  Once that delayed
426f4ad559SJosef Bacik  *     ref is written the extent size is subtracted from
436f4ad559SJosef Bacik  *     space_info->bytes_reserved and added to space_info->bytes_used.
446f4ad559SJosef Bacik  *
456f4ad559SJosef Bacik  *   Error handling
466f4ad559SJosef Bacik  *
476f4ad559SJosef Bacik  *   -> By the reservation maker
486f4ad559SJosef Bacik  *     This is the simplest case, we haven't completed our operation and we know
496f4ad559SJosef Bacik  *     how much we reserved, we can simply call
506f4ad559SJosef Bacik  *     btrfs_free_reserved_data_space*() and it will be removed from
516f4ad559SJosef Bacik  *     space_info->bytes_may_use.
526f4ad559SJosef Bacik  *
536f4ad559SJosef Bacik  *   -> After the reservation has been made, but before cow_file_range()
546f4ad559SJosef Bacik  *     This is specifically for the delalloc case.  You must clear
556f4ad559SJosef Bacik  *     EXTENT_DELALLOC with the EXTENT_CLEAR_DATA_RESV bit, and the range will
566f4ad559SJosef Bacik  *     be subtracted from space_info->bytes_may_use.
576f4ad559SJosef Bacik  *
586f4ad559SJosef Bacik  * METADATA RESERVATION
596f4ad559SJosef Bacik  *   The general metadata reservation lifetimes are discussed elsewhere, this
606f4ad559SJosef Bacik  *   will just focus on how it is used for delalloc space.
616f4ad559SJosef Bacik  *
626f4ad559SJosef Bacik  *   We keep track of two things on a per inode bases
636f4ad559SJosef Bacik  *
646f4ad559SJosef Bacik  *   ->outstanding_extents
656f4ad559SJosef Bacik  *     This is the number of file extent items we'll need to handle all of the
666f4ad559SJosef Bacik  *     outstanding DELALLOC space we have in this inode.  We limit the maximum
676f4ad559SJosef Bacik  *     size of an extent, so a large contiguous dirty area may require more than
686f4ad559SJosef Bacik  *     one outstanding_extent, which is why count_max_extents() is used to
696f4ad559SJosef Bacik  *     determine how many outstanding_extents get added.
706f4ad559SJosef Bacik  *
716f4ad559SJosef Bacik  *   ->csum_bytes
726f4ad559SJosef Bacik  *     This is essentially how many dirty bytes we have for this inode, so we
736f4ad559SJosef Bacik  *     can calculate the number of checksum items we would have to add in order
746f4ad559SJosef Bacik  *     to checksum our outstanding data.
756f4ad559SJosef Bacik  *
766f4ad559SJosef Bacik  *   We keep a per-inode block_rsv in order to make it easier to keep track of
776f4ad559SJosef Bacik  *   our reservation.  We use btrfs_calculate_inode_block_rsv_size() to
786f4ad559SJosef Bacik  *   calculate the current theoretical maximum reservation we would need for the
796f4ad559SJosef Bacik  *   metadata for this inode.  We call this and then adjust our reservation as
806f4ad559SJosef Bacik  *   necessary, either by attempting to reserve more space, or freeing up excess
816f4ad559SJosef Bacik  *   space.
826f4ad559SJosef Bacik  *
836f4ad559SJosef Bacik  * OUTSTANDING_EXTENTS HANDLING
846f4ad559SJosef Bacik  *
856f4ad559SJosef Bacik  *  ->outstanding_extents is used for keeping track of how many extents we will
866f4ad559SJosef Bacik  *  need to use for this inode, and it will fluctuate depending on where you are
876f4ad559SJosef Bacik  *  in the life cycle of the dirty data.  Consider the following normal case for
886f4ad559SJosef Bacik  *  a completely clean inode, with a num_bytes < our maximum allowed extent size
896f4ad559SJosef Bacik  *
906f4ad559SJosef Bacik  *  -> reserve
916f4ad559SJosef Bacik  *    ->outstanding_extents += 1 (current value is 1)
926f4ad559SJosef Bacik  *
936f4ad559SJosef Bacik  *  -> set_delalloc
941a9fd417SDavid Sterba  *    ->outstanding_extents += 1 (current value is 2)
956f4ad559SJosef Bacik  *
966f4ad559SJosef Bacik  *  -> btrfs_delalloc_release_extents()
976f4ad559SJosef Bacik  *    ->outstanding_extents -= 1 (current value is 1)
986f4ad559SJosef Bacik  *
996f4ad559SJosef Bacik  *    We must call this once we are done, as we hold our reservation for the
1006f4ad559SJosef Bacik  *    duration of our operation, and then assume set_delalloc will update the
1016f4ad559SJosef Bacik  *    counter appropriately.
1026f4ad559SJosef Bacik  *
1036f4ad559SJosef Bacik  *  -> add ordered extent
1046f4ad559SJosef Bacik  *    ->outstanding_extents += 1 (current value is 2)
1056f4ad559SJosef Bacik  *
1066f4ad559SJosef Bacik  *  -> btrfs_clear_delalloc_extent
1076f4ad559SJosef Bacik  *    ->outstanding_extents -= 1 (current value is 1)
1086f4ad559SJosef Bacik  *
1096f4ad559SJosef Bacik  *  -> finish_ordered_io/btrfs_remove_ordered_extent
1106f4ad559SJosef Bacik  *    ->outstanding_extents -= 1 (current value is 0)
1116f4ad559SJosef Bacik  *
1126f4ad559SJosef Bacik  *  Each stage is responsible for their own accounting of the extent, thus
1136f4ad559SJosef Bacik  *  making error handling and cleanup easier.
1146f4ad559SJosef Bacik  */
1156f4ad559SJosef Bacik 
11686736342SJosef Bacik int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
11786736342SJosef Bacik {
11886736342SJosef Bacik 	struct btrfs_root *root = inode->root;
11986736342SJosef Bacik 	struct btrfs_fs_info *fs_info = root->fs_info;
1208698fc4eSJosef Bacik 	enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
12186736342SJosef Bacik 
12286736342SJosef Bacik 	/* Make sure bytes are sectorsize aligned */
12386736342SJosef Bacik 	bytes = ALIGN(bytes, fs_info->sectorsize);
12486736342SJosef Bacik 
1258698fc4eSJosef Bacik 	if (btrfs_is_free_space_inode(inode))
1268698fc4eSJosef Bacik 		flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
12786736342SJosef Bacik 
1288698fc4eSJosef Bacik 	return btrfs_reserve_data_bytes(fs_info, bytes, flush);
12986736342SJosef Bacik }
13086736342SJosef Bacik 
13136ea6f3eSNikolay Borisov int btrfs_check_data_free_space(struct btrfs_inode *inode,
1321daedb1dSJosef Bacik 				struct extent_changeset **reserved, u64 start,
1331daedb1dSJosef Bacik 				u64 len, bool noflush)
13486736342SJosef Bacik {
13536ea6f3eSNikolay Borisov 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
1361daedb1dSJosef Bacik 	enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
13786736342SJosef Bacik 	int ret;
13886736342SJosef Bacik 
13986736342SJosef Bacik 	/* align the range */
14086736342SJosef Bacik 	len = round_up(start + len, fs_info->sectorsize) -
14186736342SJosef Bacik 	      round_down(start, fs_info->sectorsize);
14286736342SJosef Bacik 	start = round_down(start, fs_info->sectorsize);
14386736342SJosef Bacik 
1441daedb1dSJosef Bacik 	if (noflush)
1451daedb1dSJosef Bacik 		flush = BTRFS_RESERVE_NO_FLUSH;
1461daedb1dSJosef Bacik 	else if (btrfs_is_free_space_inode(inode))
1471daedb1dSJosef Bacik 		flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
1481daedb1dSJosef Bacik 
1491daedb1dSJosef Bacik 	ret = btrfs_reserve_data_bytes(fs_info, len, flush);
15086736342SJosef Bacik 	if (ret < 0)
15186736342SJosef Bacik 		return ret;
15286736342SJosef Bacik 
15386736342SJosef Bacik 	/* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
15436ea6f3eSNikolay Borisov 	ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
155da5e817dSJohannes Thumshirn 	if (ret < 0) {
1569db5d510SNikolay Borisov 		btrfs_free_reserved_data_space_noquota(fs_info, len);
157da5e817dSJohannes Thumshirn 		extent_changeset_free(*reserved);
158da5e817dSJohannes Thumshirn 		*reserved = NULL;
159da5e817dSJohannes Thumshirn 	} else {
16086736342SJosef Bacik 		ret = 0;
161da5e817dSJohannes Thumshirn 	}
16286736342SJosef Bacik 	return ret;
16386736342SJosef Bacik }
16486736342SJosef Bacik 
16586736342SJosef Bacik /*
16686736342SJosef Bacik  * Called if we need to clear a data reservation for this inode
16786736342SJosef Bacik  * Normally in a error case.
16886736342SJosef Bacik  *
16986736342SJosef Bacik  * This one will *NOT* use accurate qgroup reserved space API, just for case
17086736342SJosef Bacik  * which we can't sleep and is sure it won't affect qgroup reserved space.
17186736342SJosef Bacik  * Like clear_bit_hook().
17286736342SJosef Bacik  */
1739db5d510SNikolay Borisov void btrfs_free_reserved_data_space_noquota(struct btrfs_fs_info *fs_info,
17486736342SJosef Bacik 					    u64 len)
17586736342SJosef Bacik {
17686736342SJosef Bacik 	struct btrfs_space_info *data_sinfo;
17786736342SJosef Bacik 
17846d4dac8SFilipe Manana 	ASSERT(IS_ALIGNED(len, fs_info->sectorsize));
17986736342SJosef Bacik 
18086736342SJosef Bacik 	data_sinfo = fs_info->data_sinfo;
18139753e4aSJosef Bacik 	btrfs_space_info_free_bytes_may_use(fs_info, data_sinfo, len);
18286736342SJosef Bacik }
18386736342SJosef Bacik 
18486736342SJosef Bacik /*
18586736342SJosef Bacik  * Called if we need to clear a data reservation for this inode
18686736342SJosef Bacik  * Normally in a error case.
18786736342SJosef Bacik  *
18886736342SJosef Bacik  * This one will handle the per-inode data rsv map for accurate reserved
18986736342SJosef Bacik  * space framework.
19086736342SJosef Bacik  */
19125ce28caSNikolay Borisov void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
19286736342SJosef Bacik 			struct extent_changeset *reserved, u64 start, u64 len)
19386736342SJosef Bacik {
19425ce28caSNikolay Borisov 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
19586736342SJosef Bacik 
19686736342SJosef Bacik 	/* Make sure the range is aligned to sectorsize */
19725ce28caSNikolay Borisov 	len = round_up(start + len, fs_info->sectorsize) -
19825ce28caSNikolay Borisov 	      round_down(start, fs_info->sectorsize);
19925ce28caSNikolay Borisov 	start = round_down(start, fs_info->sectorsize);
20086736342SJosef Bacik 
20125ce28caSNikolay Borisov 	btrfs_free_reserved_data_space_noquota(fs_info, len);
20225ce28caSNikolay Borisov 	btrfs_qgroup_free_data(inode, reserved, start, len);
20386736342SJosef Bacik }
20486736342SJosef Bacik 
20543dd529aSDavid Sterba /*
20643dd529aSDavid Sterba  * Release any excessive reservations for an inode.
207b762d1d0SNikolay Borisov  *
208b762d1d0SNikolay Borisov  * @inode:       the inode we need to release from
209b762d1d0SNikolay Borisov  * @qgroup_free: free or convert qgroup meta. Unlike normal operation, qgroup
210b762d1d0SNikolay Borisov  *               meta reservation needs to know if we are freeing qgroup
211b762d1d0SNikolay Borisov  *               reservation or just converting it into per-trans.  Normally
212b762d1d0SNikolay Borisov  *               @qgroup_free is true for error handling, and false for normal
213b762d1d0SNikolay Borisov  *               release.
21486736342SJosef Bacik  *
21586736342SJosef Bacik  * This is the same as btrfs_block_rsv_release, except that it handles the
21686736342SJosef Bacik  * tracepoint for the reservation.
21786736342SJosef Bacik  */
21886736342SJosef Bacik static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
21986736342SJosef Bacik {
22086736342SJosef Bacik 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
22186736342SJosef Bacik 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
22286736342SJosef Bacik 	u64 released = 0;
22386736342SJosef Bacik 	u64 qgroup_to_release = 0;
22486736342SJosef Bacik 
22586736342SJosef Bacik 	/*
22686736342SJosef Bacik 	 * Since we statically set the block_rsv->size we just want to say we
22786736342SJosef Bacik 	 * are releasing 0 bytes, and then we'll just get the reservation over
22886736342SJosef Bacik 	 * the size free'd.
22986736342SJosef Bacik 	 */
23063f018beSNikolay Borisov 	released = btrfs_block_rsv_release(fs_info, block_rsv, 0,
23186736342SJosef Bacik 					   &qgroup_to_release);
23286736342SJosef Bacik 	if (released > 0)
23386736342SJosef Bacik 		trace_btrfs_space_reservation(fs_info, "delalloc",
23486736342SJosef Bacik 					      btrfs_ino(inode), released, 0);
23586736342SJosef Bacik 	if (qgroup_free)
23686736342SJosef Bacik 		btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
23786736342SJosef Bacik 	else
23886736342SJosef Bacik 		btrfs_qgroup_convert_reserved_meta(inode->root,
23986736342SJosef Bacik 						   qgroup_to_release);
24086736342SJosef Bacik }
24186736342SJosef Bacik 
24286736342SJosef Bacik static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
24386736342SJosef Bacik 						 struct btrfs_inode *inode)
24486736342SJosef Bacik {
24586736342SJosef Bacik 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
24686736342SJosef Bacik 	u64 reserve_size = 0;
24786736342SJosef Bacik 	u64 qgroup_rsv_size = 0;
24886736342SJosef Bacik 	u64 csum_leaves;
24986736342SJosef Bacik 	unsigned outstanding_extents;
25086736342SJosef Bacik 
25186736342SJosef Bacik 	lockdep_assert_held(&inode->lock);
25286736342SJosef Bacik 	outstanding_extents = inode->outstanding_extents;
253bcacf5f3SJosef Bacik 
254bcacf5f3SJosef Bacik 	/*
255bcacf5f3SJosef Bacik 	 * Insert size for the number of outstanding extents, 1 normal size for
256bcacf5f3SJosef Bacik 	 * updating the inode.
257bcacf5f3SJosef Bacik 	 */
258bcacf5f3SJosef Bacik 	if (outstanding_extents) {
2592bd36e7bSJosef Bacik 		reserve_size = btrfs_calc_insert_metadata_size(fs_info,
260bcacf5f3SJosef Bacik 						outstanding_extents);
261bcacf5f3SJosef Bacik 		reserve_size += btrfs_calc_metadata_size(fs_info, 1);
262bcacf5f3SJosef Bacik 	}
26386736342SJosef Bacik 	csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
26486736342SJosef Bacik 						 inode->csum_bytes);
2652bd36e7bSJosef Bacik 	reserve_size += btrfs_calc_insert_metadata_size(fs_info,
26686736342SJosef Bacik 							csum_leaves);
26786736342SJosef Bacik 	/*
26886736342SJosef Bacik 	 * For qgroup rsv, the calculation is very simple:
26986736342SJosef Bacik 	 * account one nodesize for each outstanding extent
27086736342SJosef Bacik 	 *
27186736342SJosef Bacik 	 * This is overestimating in most cases.
27286736342SJosef Bacik 	 */
27386736342SJosef Bacik 	qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
27486736342SJosef Bacik 
27586736342SJosef Bacik 	spin_lock(&block_rsv->lock);
27686736342SJosef Bacik 	block_rsv->size = reserve_size;
27786736342SJosef Bacik 	block_rsv->qgroup_rsv_size = qgroup_rsv_size;
27886736342SJosef Bacik 	spin_unlock(&block_rsv->lock);
27986736342SJosef Bacik }
28086736342SJosef Bacik 
28186736342SJosef Bacik static void calc_inode_reservations(struct btrfs_fs_info *fs_info,
28228c9b1e7SOmar Sandoval 				    u64 num_bytes, u64 disk_num_bytes,
28328c9b1e7SOmar Sandoval 				    u64 *meta_reserve, u64 *qgroup_reserve)
28486736342SJosef Bacik {
2857d7672bcSNaohiro Aota 	u64 nr_extents = count_max_extents(fs_info, num_bytes);
28628c9b1e7SOmar Sandoval 	u64 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, disk_num_bytes);
287bcacf5f3SJosef Bacik 	u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
28886736342SJosef Bacik 
2892bd36e7bSJosef Bacik 	*meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
290bcacf5f3SJosef Bacik 						nr_extents + csum_leaves);
291bcacf5f3SJosef Bacik 
292bcacf5f3SJosef Bacik 	/*
293bcacf5f3SJosef Bacik 	 * finish_ordered_io has to update the inode, so add the space required
294bcacf5f3SJosef Bacik 	 * for an inode update.
295bcacf5f3SJosef Bacik 	 */
296bcacf5f3SJosef Bacik 	*meta_reserve += inode_update;
29786736342SJosef Bacik 	*qgroup_reserve = nr_extents * fs_info->nodesize;
29886736342SJosef Bacik }
29986736342SJosef Bacik 
30028c9b1e7SOmar Sandoval int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
301d4135134SFilipe Manana 				    u64 disk_num_bytes, bool noflush)
30286736342SJosef Bacik {
30386736342SJosef Bacik 	struct btrfs_root *root = inode->root;
30486736342SJosef Bacik 	struct btrfs_fs_info *fs_info = root->fs_info;
30586736342SJosef Bacik 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
30686736342SJosef Bacik 	u64 meta_reserve, qgroup_reserve;
30786736342SJosef Bacik 	unsigned nr_extents;
30886736342SJosef Bacik 	enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
30986736342SJosef Bacik 	int ret = 0;
31086736342SJosef Bacik 
31186736342SJosef Bacik 	/*
31286736342SJosef Bacik 	 * If we are a free space inode we need to not flush since we will be in
31386736342SJosef Bacik 	 * the middle of a transaction commit.  We also don't need the delalloc
31486736342SJosef Bacik 	 * mutex since we won't race with anybody.  We need this mostly to make
31586736342SJosef Bacik 	 * lockdep shut its filthy mouth.
31686736342SJosef Bacik 	 *
31786736342SJosef Bacik 	 * If we have a transaction open (can happen if we call truncate_block
31886736342SJosef Bacik 	 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
31986736342SJosef Bacik 	 */
320d4135134SFilipe Manana 	if (noflush || btrfs_is_free_space_inode(inode)) {
32186736342SJosef Bacik 		flush = BTRFS_RESERVE_NO_FLUSH;
32286736342SJosef Bacik 	} else {
32386736342SJosef Bacik 		if (current->journal_info)
32486736342SJosef Bacik 			flush = BTRFS_RESERVE_FLUSH_LIMIT;
32586736342SJosef Bacik 
32686736342SJosef Bacik 		if (btrfs_transaction_in_commit(fs_info))
32786736342SJosef Bacik 			schedule_timeout(1);
32886736342SJosef Bacik 	}
32986736342SJosef Bacik 
33086736342SJosef Bacik 	num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
33128c9b1e7SOmar Sandoval 	disk_num_bytes = ALIGN(disk_num_bytes, fs_info->sectorsize);
33286736342SJosef Bacik 
33386736342SJosef Bacik 	/*
33486736342SJosef Bacik 	 * We always want to do it this way, every other way is wrong and ends
33586736342SJosef Bacik 	 * in tears.  Pre-reserving the amount we are going to add will always
33686736342SJosef Bacik 	 * be the right way, because otherwise if we have enough parallelism we
33786736342SJosef Bacik 	 * could end up with thousands of inodes all holding little bits of
33886736342SJosef Bacik 	 * reservations they were able to make previously and the only way to
33986736342SJosef Bacik 	 * reclaim that space is to ENOSPC out the operations and clear
34086736342SJosef Bacik 	 * everything out and try again, which is bad.  This way we just
34186736342SJosef Bacik 	 * over-reserve slightly, and clean up the mess when we are done.
34286736342SJosef Bacik 	 */
34328c9b1e7SOmar Sandoval 	calc_inode_reservations(fs_info, num_bytes, disk_num_bytes,
34428c9b1e7SOmar Sandoval 				&meta_reserve, &qgroup_reserve);
345d4135134SFilipe Manana 	ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true,
346d4135134SFilipe Manana 						 noflush);
34786736342SJosef Bacik 	if (ret)
34816ad3be1SFilipe Manana 		return ret;
3499270501cSJosef Bacik 	ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, meta_reserve, flush);
35016ad3be1SFilipe Manana 	if (ret) {
35116ad3be1SFilipe Manana 		btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
35216ad3be1SFilipe Manana 		return ret;
35316ad3be1SFilipe Manana 	}
35486736342SJosef Bacik 
35586736342SJosef Bacik 	/*
35686736342SJosef Bacik 	 * Now we need to update our outstanding extents and csum bytes _first_
35786736342SJosef Bacik 	 * and then add the reservation to the block_rsv.  This keeps us from
35886736342SJosef Bacik 	 * racing with an ordered completion or some such that would think it
35986736342SJosef Bacik 	 * needs to free the reservation we just made.
36086736342SJosef Bacik 	 */
3617d7672bcSNaohiro Aota 	nr_extents = count_max_extents(fs_info, num_bytes);
362*9aa06c76SFilipe Manana 	spin_lock(&inode->lock);
36386736342SJosef Bacik 	btrfs_mod_outstanding_extents(inode, nr_extents);
36428c9b1e7SOmar Sandoval 	inode->csum_bytes += disk_num_bytes;
36586736342SJosef Bacik 	btrfs_calculate_inode_block_rsv_size(fs_info, inode);
36686736342SJosef Bacik 	spin_unlock(&inode->lock);
36786736342SJosef Bacik 
36886736342SJosef Bacik 	/* Now we can safely add our space to our block rsv */
36986736342SJosef Bacik 	btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
37086736342SJosef Bacik 	trace_btrfs_space_reservation(root->fs_info, "delalloc",
37186736342SJosef Bacik 				      btrfs_ino(inode), meta_reserve, 1);
37286736342SJosef Bacik 
37386736342SJosef Bacik 	spin_lock(&block_rsv->lock);
37486736342SJosef Bacik 	block_rsv->qgroup_rsv_reserved += qgroup_reserve;
37586736342SJosef Bacik 	spin_unlock(&block_rsv->lock);
37686736342SJosef Bacik 
37786736342SJosef Bacik 	return 0;
37886736342SJosef Bacik }
37986736342SJosef Bacik 
38043dd529aSDavid Sterba /*
38143dd529aSDavid Sterba  * Release a metadata reservation for an inode.
382b762d1d0SNikolay Borisov  *
38386736342SJosef Bacik  * @inode:        the inode to release the reservation for.
38486736342SJosef Bacik  * @num_bytes:    the number of bytes we are releasing.
38586736342SJosef Bacik  * @qgroup_free:  free qgroup reservation or convert it to per-trans reservation
38686736342SJosef Bacik  *
38786736342SJosef Bacik  * This will release the metadata reservation for an inode.  This can be called
38886736342SJosef Bacik  * once we complete IO for a given set of bytes to release their metadata
38986736342SJosef Bacik  * reservations, or on error for the same reason.
39086736342SJosef Bacik  */
39186736342SJosef Bacik void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
39286736342SJosef Bacik 				     bool qgroup_free)
39386736342SJosef Bacik {
39486736342SJosef Bacik 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
39586736342SJosef Bacik 
39686736342SJosef Bacik 	num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
39786736342SJosef Bacik 	spin_lock(&inode->lock);
39886736342SJosef Bacik 	inode->csum_bytes -= num_bytes;
39986736342SJosef Bacik 	btrfs_calculate_inode_block_rsv_size(fs_info, inode);
40086736342SJosef Bacik 	spin_unlock(&inode->lock);
40186736342SJosef Bacik 
40286736342SJosef Bacik 	if (btrfs_is_testing(fs_info))
40386736342SJosef Bacik 		return;
40486736342SJosef Bacik 
40586736342SJosef Bacik 	btrfs_inode_rsv_release(inode, qgroup_free);
40686736342SJosef Bacik }
40786736342SJosef Bacik 
40843dd529aSDavid Sterba /*
40943dd529aSDavid Sterba  * Release our outstanding_extents for an inode.
41043dd529aSDavid Sterba  *
41186736342SJosef Bacik  * @inode:      the inode to balance the reservation for.
41286736342SJosef Bacik  * @num_bytes:  the number of bytes we originally reserved with
41386736342SJosef Bacik  *
41486736342SJosef Bacik  * When we reserve space we increase outstanding_extents for the extents we may
41586736342SJosef Bacik  * add.  Once we've set the range as delalloc or created our ordered extents we
41686736342SJosef Bacik  * have outstanding_extents to track the real usage, so we use this to free our
41786736342SJosef Bacik  * temporarily tracked outstanding_extents.  This _must_ be used in conjunction
41886736342SJosef Bacik  * with btrfs_delalloc_reserve_metadata.
41986736342SJosef Bacik  */
4208702ba93SQu Wenruo void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
42186736342SJosef Bacik {
42286736342SJosef Bacik 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
42386736342SJosef Bacik 	unsigned num_extents;
42486736342SJosef Bacik 
42586736342SJosef Bacik 	spin_lock(&inode->lock);
4267d7672bcSNaohiro Aota 	num_extents = count_max_extents(fs_info, num_bytes);
42786736342SJosef Bacik 	btrfs_mod_outstanding_extents(inode, -num_extents);
42886736342SJosef Bacik 	btrfs_calculate_inode_block_rsv_size(fs_info, inode);
42986736342SJosef Bacik 	spin_unlock(&inode->lock);
43086736342SJosef Bacik 
43186736342SJosef Bacik 	if (btrfs_is_testing(fs_info))
43286736342SJosef Bacik 		return;
43386736342SJosef Bacik 
4348702ba93SQu Wenruo 	btrfs_inode_rsv_release(inode, true);
43586736342SJosef Bacik }
43686736342SJosef Bacik 
43743dd529aSDavid Sterba /*
43843dd529aSDavid Sterba  * Reserve data and metadata space for delalloc
43943dd529aSDavid Sterba  *
44086736342SJosef Bacik  * @inode:     inode we're writing to
44186736342SJosef Bacik  * @start:     start range we are writing to
44286736342SJosef Bacik  * @len:       how long the range we are writing to
44386736342SJosef Bacik  * @reserved:  mandatory parameter, record actually reserved qgroup ranges of
44486736342SJosef Bacik  * 	       current reservation.
44586736342SJosef Bacik  *
44686736342SJosef Bacik  * This will do the following things
44786736342SJosef Bacik  *
44843dd529aSDavid Sterba  * - reserve space in data space info for num bytes and reserve precious
44943dd529aSDavid Sterba  *   corresponding qgroup space
45086736342SJosef Bacik  *   (Done in check_data_free_space)
45186736342SJosef Bacik  *
45286736342SJosef Bacik  * - reserve space for metadata space, based on the number of outstanding
45343dd529aSDavid Sterba  *   extents and how much csums will be needed also reserve metadata space in a
45443dd529aSDavid Sterba  *   per root over-reserve method.
45586736342SJosef Bacik  * - add to the inodes->delalloc_bytes
45686736342SJosef Bacik  * - add it to the fs_info's delalloc inodes list.
45786736342SJosef Bacik  *   (Above 3 all done in delalloc_reserve_metadata)
45886736342SJosef Bacik  *
45986736342SJosef Bacik  * Return 0 for success
46043dd529aSDavid Sterba  * Return <0 for error(-ENOSPC or -EDQUOT)
46186736342SJosef Bacik  */
462e5b7231eSNikolay Borisov int btrfs_delalloc_reserve_space(struct btrfs_inode *inode,
46386736342SJosef Bacik 			struct extent_changeset **reserved, u64 start, u64 len)
46486736342SJosef Bacik {
46586736342SJosef Bacik 	int ret;
46686736342SJosef Bacik 
4671daedb1dSJosef Bacik 	ret = btrfs_check_data_free_space(inode, reserved, start, len, false);
46886736342SJosef Bacik 	if (ret < 0)
46986736342SJosef Bacik 		return ret;
470d4135134SFilipe Manana 	ret = btrfs_delalloc_reserve_metadata(inode, len, len, false);
471da5e817dSJohannes Thumshirn 	if (ret < 0) {
472e5b7231eSNikolay Borisov 		btrfs_free_reserved_data_space(inode, *reserved, start, len);
473da5e817dSJohannes Thumshirn 		extent_changeset_free(*reserved);
474da5e817dSJohannes Thumshirn 		*reserved = NULL;
475da5e817dSJohannes Thumshirn 	}
47686736342SJosef Bacik 	return ret;
47786736342SJosef Bacik }
47886736342SJosef Bacik 
47943dd529aSDavid Sterba /*
480b762d1d0SNikolay Borisov  * Release data and metadata space for delalloc
481b762d1d0SNikolay Borisov  *
48286736342SJosef Bacik  * @inode:       inode we're releasing space for
483b762d1d0SNikolay Borisov  * @reserved:    list of changed/reserved ranges
48486736342SJosef Bacik  * @start:       start position of the space already reserved
485b762d1d0SNikolay Borisov  * @len:         length of the space already reserved
486b762d1d0SNikolay Borisov  * @qgroup_free: should qgroup reserved-space also be freed
48786736342SJosef Bacik  *
48843dd529aSDavid Sterba  * Release the metadata space that was not used and will decrement
48943dd529aSDavid Sterba  * ->delalloc_bytes and remove it from the fs_info->delalloc_inodes list if
49043dd529aSDavid Sterba  * there are no delalloc bytes left.  Also it will handle the qgroup reserved
49143dd529aSDavid Sterba  * space.
49286736342SJosef Bacik  */
49386d52921SNikolay Borisov void btrfs_delalloc_release_space(struct btrfs_inode *inode,
49486736342SJosef Bacik 				  struct extent_changeset *reserved,
49586736342SJosef Bacik 				  u64 start, u64 len, bool qgroup_free)
49686736342SJosef Bacik {
49786d52921SNikolay Borisov 	btrfs_delalloc_release_metadata(inode, len, qgroup_free);
49886d52921SNikolay Borisov 	btrfs_free_reserved_data_space(inode, reserved, start, len);
49986736342SJosef Bacik }
500