xref: /openbmc/linux/fs/btrfs/space-info.c (revision 0db00e5d86dc793aab9722ad3728d99166eb7d96)
1280c2908SJosef Bacik // SPDX-License-Identifier: GPL-2.0
2280c2908SJosef Bacik 
3784352feSDavid Sterba #include "misc.h"
4280c2908SJosef Bacik #include "ctree.h"
5280c2908SJosef Bacik #include "space-info.h"
6280c2908SJosef Bacik #include "sysfs.h"
7280c2908SJosef Bacik #include "volumes.h"
85da6afebSJosef Bacik #include "free-space-cache.h"
90d9764f6SJosef Bacik #include "ordered-data.h"
100d9764f6SJosef Bacik #include "transaction.h"
11aac0023cSJosef Bacik #include "block-group.h"
12b0931513SNaohiro Aota #include "zoned.h"
13c7f13d42SJosef Bacik #include "fs.h"
1407e81dc9SJosef Bacik #include "accessors.h"
15a0231804SJosef Bacik #include "extent-tree.h"
16280c2908SJosef Bacik 
174b8b0528SJosef Bacik /*
184b8b0528SJosef Bacik  * HOW DOES SPACE RESERVATION WORK
194b8b0528SJosef Bacik  *
204b8b0528SJosef Bacik  * If you want to know about delalloc specifically, there is a separate comment
214b8b0528SJosef Bacik  * for that with the delalloc code.  This comment is about how the whole system
224b8b0528SJosef Bacik  * works generally.
234b8b0528SJosef Bacik  *
244b8b0528SJosef Bacik  * BASIC CONCEPTS
254b8b0528SJosef Bacik  *
264b8b0528SJosef Bacik  *   1) space_info.  This is the ultimate arbiter of how much space we can use.
274b8b0528SJosef Bacik  *   There's a description of the bytes_ fields with the struct declaration,
284b8b0528SJosef Bacik  *   refer to that for specifics on each field.  Suffice it to say that for
294b8b0528SJosef Bacik  *   reservations we care about total_bytes - SUM(space_info->bytes_) when
304b8b0528SJosef Bacik  *   determining if there is space to make an allocation.  There is a space_info
314b8b0528SJosef Bacik  *   for METADATA, SYSTEM, and DATA areas.
324b8b0528SJosef Bacik  *
334b8b0528SJosef Bacik  *   2) block_rsv's.  These are basically buckets for every different type of
344b8b0528SJosef Bacik  *   metadata reservation we have.  You can see the comment in the block_rsv
354b8b0528SJosef Bacik  *   code on the rules for each type, but generally block_rsv->reserved is how
364b8b0528SJosef Bacik  *   much space is accounted for in space_info->bytes_may_use.
374b8b0528SJosef Bacik  *
384b8b0528SJosef Bacik  *   3) btrfs_calc*_size.  These are the worst case calculations we used based
394b8b0528SJosef Bacik  *   on the number of items we will want to modify.  We have one for changing
404b8b0528SJosef Bacik  *   items, and one for inserting new items.  Generally we use these helpers to
414b8b0528SJosef Bacik  *   determine the size of the block reserves, and then use the actual bytes
424b8b0528SJosef Bacik  *   values to adjust the space_info counters.
434b8b0528SJosef Bacik  *
444b8b0528SJosef Bacik  * MAKING RESERVATIONS, THE NORMAL CASE
454b8b0528SJosef Bacik  *
464b8b0528SJosef Bacik  *   We call into either btrfs_reserve_data_bytes() or
474b8b0528SJosef Bacik  *   btrfs_reserve_metadata_bytes(), depending on which we're looking for, with
484b8b0528SJosef Bacik  *   num_bytes we want to reserve.
494b8b0528SJosef Bacik  *
504b8b0528SJosef Bacik  *   ->reserve
514b8b0528SJosef Bacik  *     space_info->bytes_may_reserve += num_bytes
524b8b0528SJosef Bacik  *
534b8b0528SJosef Bacik  *   ->extent allocation
544b8b0528SJosef Bacik  *     Call btrfs_add_reserved_bytes() which does
554b8b0528SJosef Bacik  *     space_info->bytes_may_reserve -= num_bytes
564b8b0528SJosef Bacik  *     space_info->bytes_reserved += extent_bytes
574b8b0528SJosef Bacik  *
584b8b0528SJosef Bacik  *   ->insert reference
594b8b0528SJosef Bacik  *     Call btrfs_update_block_group() which does
604b8b0528SJosef Bacik  *     space_info->bytes_reserved -= extent_bytes
614b8b0528SJosef Bacik  *     space_info->bytes_used += extent_bytes
624b8b0528SJosef Bacik  *
634b8b0528SJosef Bacik  * MAKING RESERVATIONS, FLUSHING NORMALLY (non-priority)
644b8b0528SJosef Bacik  *
654b8b0528SJosef Bacik  *   Assume we are unable to simply make the reservation because we do not have
664b8b0528SJosef Bacik  *   enough space
674b8b0528SJosef Bacik  *
684b8b0528SJosef Bacik  *   -> __reserve_bytes
694b8b0528SJosef Bacik  *     create a reserve_ticket with ->bytes set to our reservation, add it to
704b8b0528SJosef Bacik  *     the tail of space_info->tickets, kick async flush thread
714b8b0528SJosef Bacik  *
724b8b0528SJosef Bacik  *   ->handle_reserve_ticket
734b8b0528SJosef Bacik  *     wait on ticket->wait for ->bytes to be reduced to 0, or ->error to be set
744b8b0528SJosef Bacik  *     on the ticket.
754b8b0528SJosef Bacik  *
764b8b0528SJosef Bacik  *   -> btrfs_async_reclaim_metadata_space/btrfs_async_reclaim_data_space
774b8b0528SJosef Bacik  *     Flushes various things attempting to free up space.
784b8b0528SJosef Bacik  *
794b8b0528SJosef Bacik  *   -> btrfs_try_granting_tickets()
804b8b0528SJosef Bacik  *     This is called by anything that either subtracts space from
814b8b0528SJosef Bacik  *     space_info->bytes_may_use, ->bytes_pinned, etc, or adds to the
824b8b0528SJosef Bacik  *     space_info->total_bytes.  This loops through the ->priority_tickets and
834b8b0528SJosef Bacik  *     then the ->tickets list checking to see if the reservation can be
844b8b0528SJosef Bacik  *     completed.  If it can the space is added to space_info->bytes_may_use and
854b8b0528SJosef Bacik  *     the ticket is woken up.
864b8b0528SJosef Bacik  *
874b8b0528SJosef Bacik  *   -> ticket wakeup
884b8b0528SJosef Bacik  *     Check if ->bytes == 0, if it does we got our reservation and we can carry
894b8b0528SJosef Bacik  *     on, if not return the appropriate error (ENOSPC, but can be EINTR if we
904b8b0528SJosef Bacik  *     were interrupted.)
914b8b0528SJosef Bacik  *
924b8b0528SJosef Bacik  * MAKING RESERVATIONS, FLUSHING HIGH PRIORITY
934b8b0528SJosef Bacik  *
944b8b0528SJosef Bacik  *   Same as the above, except we add ourselves to the
954b8b0528SJosef Bacik  *   space_info->priority_tickets, and we do not use ticket->wait, we simply
964b8b0528SJosef Bacik  *   call flush_space() ourselves for the states that are safe for us to call
974b8b0528SJosef Bacik  *   without deadlocking and hope for the best.
984b8b0528SJosef Bacik  *
994b8b0528SJosef Bacik  * THE FLUSHING STATES
1004b8b0528SJosef Bacik  *
1014b8b0528SJosef Bacik  *   Generally speaking we will have two cases for each state, a "nice" state
1024b8b0528SJosef Bacik  *   and a "ALL THE THINGS" state.  In btrfs we delay a lot of work in order to
1034b8b0528SJosef Bacik  *   reduce the locking over head on the various trees, and even to keep from
1044b8b0528SJosef Bacik  *   doing any work at all in the case of delayed refs.  Each of these delayed
1054b8b0528SJosef Bacik  *   things however hold reservations, and so letting them run allows us to
1064b8b0528SJosef Bacik  *   reclaim space so we can make new reservations.
1074b8b0528SJosef Bacik  *
1084b8b0528SJosef Bacik  *   FLUSH_DELAYED_ITEMS
1094b8b0528SJosef Bacik  *     Every inode has a delayed item to update the inode.  Take a simple write
1104b8b0528SJosef Bacik  *     for example, we would update the inode item at write time to update the
1114b8b0528SJosef Bacik  *     mtime, and then again at finish_ordered_io() time in order to update the
1124b8b0528SJosef Bacik  *     isize or bytes.  We keep these delayed items to coalesce these operations
1134b8b0528SJosef Bacik  *     into a single operation done on demand.  These are an easy way to reclaim
1144b8b0528SJosef Bacik  *     metadata space.
1154b8b0528SJosef Bacik  *
1164b8b0528SJosef Bacik  *   FLUSH_DELALLOC
1174b8b0528SJosef Bacik  *     Look at the delalloc comment to get an idea of how much space is reserved
1184b8b0528SJosef Bacik  *     for delayed allocation.  We can reclaim some of this space simply by
1194b8b0528SJosef Bacik  *     running delalloc, but usually we need to wait for ordered extents to
1204b8b0528SJosef Bacik  *     reclaim the bulk of this space.
1214b8b0528SJosef Bacik  *
1224b8b0528SJosef Bacik  *   FLUSH_DELAYED_REFS
1234b8b0528SJosef Bacik  *     We have a block reserve for the outstanding delayed refs space, and every
1244b8b0528SJosef Bacik  *     delayed ref operation holds a reservation.  Running these is a quick way
1254b8b0528SJosef Bacik  *     to reclaim space, but we want to hold this until the end because COW can
1264b8b0528SJosef Bacik  *     churn a lot and we can avoid making some extent tree modifications if we
1274b8b0528SJosef Bacik  *     are able to delay for as long as possible.
1284b8b0528SJosef Bacik  *
1294b8b0528SJosef Bacik  *   ALLOC_CHUNK
1304b8b0528SJosef Bacik  *     We will skip this the first time through space reservation, because of
1314b8b0528SJosef Bacik  *     overcommit and we don't want to have a lot of useless metadata space when
1324b8b0528SJosef Bacik  *     our worst case reservations will likely never come true.
1334b8b0528SJosef Bacik  *
1344b8b0528SJosef Bacik  *   RUN_DELAYED_IPUTS
1354b8b0528SJosef Bacik  *     If we're freeing inodes we're likely freeing checksums, file extent
1364b8b0528SJosef Bacik  *     items, and extent tree items.  Loads of space could be freed up by these
1374b8b0528SJosef Bacik  *     operations, however they won't be usable until the transaction commits.
1384b8b0528SJosef Bacik  *
1394b8b0528SJosef Bacik  *   COMMIT_TRANS
140c416a30cSJosef Bacik  *     This will commit the transaction.  Historically we had a lot of logic
141c416a30cSJosef Bacik  *     surrounding whether or not we'd commit the transaction, but this waits born
142c416a30cSJosef Bacik  *     out of a pre-tickets era where we could end up committing the transaction
143c416a30cSJosef Bacik  *     thousands of times in a row without making progress.  Now thanks to our
144c416a30cSJosef Bacik  *     ticketing system we know if we're not making progress and can error
145c416a30cSJosef Bacik  *     everybody out after a few commits rather than burning the disk hoping for
146c416a30cSJosef Bacik  *     a different answer.
147f00c42ddSJosef Bacik  *
1484b8b0528SJosef Bacik  * OVERCOMMIT
1494b8b0528SJosef Bacik  *
1504b8b0528SJosef Bacik  *   Because we hold so many reservations for metadata we will allow you to
1514b8b0528SJosef Bacik  *   reserve more space than is currently free in the currently allocate
1524b8b0528SJosef Bacik  *   metadata space.  This only happens with metadata, data does not allow
1534b8b0528SJosef Bacik  *   overcommitting.
1544b8b0528SJosef Bacik  *
1554b8b0528SJosef Bacik  *   You can see the current logic for when we allow overcommit in
1564b8b0528SJosef Bacik  *   btrfs_can_overcommit(), but it only applies to unallocated space.  If there
1574b8b0528SJosef Bacik  *   is no unallocated space to be had, all reservations are kept within the
1584b8b0528SJosef Bacik  *   free space in the allocated metadata chunks.
1594b8b0528SJosef Bacik  *
1604b8b0528SJosef Bacik  *   Because of overcommitting, you generally want to use the
1614b8b0528SJosef Bacik  *   btrfs_can_overcommit() logic for metadata allocations, as it does the right
1624b8b0528SJosef Bacik  *   thing with or without extra unallocated space.
1634b8b0528SJosef Bacik  */
1644b8b0528SJosef Bacik 
btrfs_space_info_used(struct btrfs_space_info * s_info,bool may_use_included)165e1f60a65SDavid Sterba u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
166280c2908SJosef Bacik 			  bool may_use_included)
167280c2908SJosef Bacik {
168280c2908SJosef Bacik 	ASSERT(s_info);
169280c2908SJosef Bacik 	return s_info->bytes_used + s_info->bytes_reserved +
170280c2908SJosef Bacik 		s_info->bytes_pinned + s_info->bytes_readonly +
171169e0da9SNaohiro Aota 		s_info->bytes_zone_unusable +
172280c2908SJosef Bacik 		(may_use_included ? s_info->bytes_may_use : 0);
173280c2908SJosef Bacik }
174280c2908SJosef Bacik 
175280c2908SJosef Bacik /*
176280c2908SJosef Bacik  * after adding space to the filesystem, we need to clear the full flags
177280c2908SJosef Bacik  * on all the space infos.
178280c2908SJosef Bacik  */
btrfs_clear_space_info_full(struct btrfs_fs_info * info)179280c2908SJosef Bacik void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
180280c2908SJosef Bacik {
181280c2908SJosef Bacik 	struct list_head *head = &info->space_info;
182280c2908SJosef Bacik 	struct btrfs_space_info *found;
183280c2908SJosef Bacik 
18472804905SJosef Bacik 	list_for_each_entry(found, head, list)
185280c2908SJosef Bacik 		found->full = 0;
186280c2908SJosef Bacik }
187280c2908SJosef Bacik 
188bb5a098dSJosef Bacik /*
189bb5a098dSJosef Bacik  * Block groups with more than this value (percents) of unusable space will be
190bb5a098dSJosef Bacik  * scheduled for background reclaim.
191bb5a098dSJosef Bacik  */
192bb5a098dSJosef Bacik #define BTRFS_DEFAULT_ZONED_RECLAIM_THRESH			(75)
193bb5a098dSJosef Bacik 
194f6fca391SStefan Roesch /*
195f6fca391SStefan Roesch  * Calculate chunk size depending on volume type (regular or zoned).
196f6fca391SStefan Roesch  */
calc_chunk_size(const struct btrfs_fs_info * fs_info,u64 flags)197f6fca391SStefan Roesch static u64 calc_chunk_size(const struct btrfs_fs_info *fs_info, u64 flags)
198f6fca391SStefan Roesch {
199f6fca391SStefan Roesch 	if (btrfs_is_zoned(fs_info))
200f6fca391SStefan Roesch 		return fs_info->zone_size;
201f6fca391SStefan Roesch 
202f6fca391SStefan Roesch 	ASSERT(flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
203f6fca391SStefan Roesch 
204f6fca391SStefan Roesch 	if (flags & BTRFS_BLOCK_GROUP_DATA)
2055da431b7SQu Wenruo 		return BTRFS_MAX_DATA_CHUNK_SIZE;
206f6fca391SStefan Roesch 	else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
207f6fca391SStefan Roesch 		return SZ_32M;
208f6fca391SStefan Roesch 
209f6fca391SStefan Roesch 	/* Handle BTRFS_BLOCK_GROUP_METADATA */
210f6fca391SStefan Roesch 	if (fs_info->fs_devices->total_rw_bytes > 50ULL * SZ_1G)
211f6fca391SStefan Roesch 		return SZ_1G;
212f6fca391SStefan Roesch 
213f6fca391SStefan Roesch 	return SZ_256M;
214f6fca391SStefan Roesch }
215f6fca391SStefan Roesch 
216f6fca391SStefan Roesch /*
217f6fca391SStefan Roesch  * Update default chunk size.
218f6fca391SStefan Roesch  */
btrfs_update_space_info_chunk_size(struct btrfs_space_info * space_info,u64 chunk_size)219f6fca391SStefan Roesch void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
220f6fca391SStefan Roesch 					u64 chunk_size)
221f6fca391SStefan Roesch {
222f6fca391SStefan Roesch 	WRITE_ONCE(space_info->chunk_size, chunk_size);
223f6fca391SStefan Roesch }
224f6fca391SStefan Roesch 
create_space_info(struct btrfs_fs_info * info,u64 flags)225280c2908SJosef Bacik static int create_space_info(struct btrfs_fs_info *info, u64 flags)
226280c2908SJosef Bacik {
227280c2908SJosef Bacik 
228280c2908SJosef Bacik 	struct btrfs_space_info *space_info;
229280c2908SJosef Bacik 	int i;
230280c2908SJosef Bacik 	int ret;
231280c2908SJosef Bacik 
232280c2908SJosef Bacik 	space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
233280c2908SJosef Bacik 	if (!space_info)
234280c2908SJosef Bacik 		return -ENOMEM;
235280c2908SJosef Bacik 
236280c2908SJosef Bacik 	for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
237280c2908SJosef Bacik 		INIT_LIST_HEAD(&space_info->block_groups[i]);
238280c2908SJosef Bacik 	init_rwsem(&space_info->groups_sem);
239280c2908SJosef Bacik 	spin_lock_init(&space_info->lock);
240280c2908SJosef Bacik 	space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
241280c2908SJosef Bacik 	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
242280c2908SJosef Bacik 	INIT_LIST_HEAD(&space_info->ro_bgs);
243280c2908SJosef Bacik 	INIT_LIST_HEAD(&space_info->tickets);
244280c2908SJosef Bacik 	INIT_LIST_HEAD(&space_info->priority_tickets);
24588a777a6SJosef Bacik 	space_info->clamp = 1;
246f6fca391SStefan Roesch 	btrfs_update_space_info_chunk_size(space_info, calc_chunk_size(info, flags));
247280c2908SJosef Bacik 
248bb5a098dSJosef Bacik 	if (btrfs_is_zoned(info))
249bb5a098dSJosef Bacik 		space_info->bg_reclaim_threshold = BTRFS_DEFAULT_ZONED_RECLAIM_THRESH;
250bb5a098dSJosef Bacik 
251b882327aSDavid Sterba 	ret = btrfs_sysfs_add_space_info_type(info, space_info);
252b882327aSDavid Sterba 	if (ret)
253280c2908SJosef Bacik 		return ret;
254280c2908SJosef Bacik 
25572804905SJosef Bacik 	list_add(&space_info->list, &info->space_info);
256280c2908SJosef Bacik 	if (flags & BTRFS_BLOCK_GROUP_DATA)
257280c2908SJosef Bacik 		info->data_sinfo = space_info;
258280c2908SJosef Bacik 
259280c2908SJosef Bacik 	return ret;
260280c2908SJosef Bacik }
261280c2908SJosef Bacik 
btrfs_init_space_info(struct btrfs_fs_info * fs_info)262280c2908SJosef Bacik int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
263280c2908SJosef Bacik {
264280c2908SJosef Bacik 	struct btrfs_super_block *disk_super;
265280c2908SJosef Bacik 	u64 features;
266280c2908SJosef Bacik 	u64 flags;
267280c2908SJosef Bacik 	int mixed = 0;
268280c2908SJosef Bacik 	int ret;
269280c2908SJosef Bacik 
270280c2908SJosef Bacik 	disk_super = fs_info->super_copy;
271280c2908SJosef Bacik 	if (!btrfs_super_root(disk_super))
272280c2908SJosef Bacik 		return -EINVAL;
273280c2908SJosef Bacik 
274280c2908SJosef Bacik 	features = btrfs_super_incompat_flags(disk_super);
275280c2908SJosef Bacik 	if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
276280c2908SJosef Bacik 		mixed = 1;
277280c2908SJosef Bacik 
278280c2908SJosef Bacik 	flags = BTRFS_BLOCK_GROUP_SYSTEM;
279280c2908SJosef Bacik 	ret = create_space_info(fs_info, flags);
280280c2908SJosef Bacik 	if (ret)
281280c2908SJosef Bacik 		goto out;
282280c2908SJosef Bacik 
283280c2908SJosef Bacik 	if (mixed) {
284280c2908SJosef Bacik 		flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
285280c2908SJosef Bacik 		ret = create_space_info(fs_info, flags);
286280c2908SJosef Bacik 	} else {
287280c2908SJosef Bacik 		flags = BTRFS_BLOCK_GROUP_METADATA;
288280c2908SJosef Bacik 		ret = create_space_info(fs_info, flags);
289280c2908SJosef Bacik 		if (ret)
290280c2908SJosef Bacik 			goto out;
291280c2908SJosef Bacik 
292280c2908SJosef Bacik 		flags = BTRFS_BLOCK_GROUP_DATA;
293280c2908SJosef Bacik 		ret = create_space_info(fs_info, flags);
294280c2908SJosef Bacik 	}
295280c2908SJosef Bacik out:
296280c2908SJosef Bacik 	return ret;
297280c2908SJosef Bacik }
298280c2908SJosef Bacik 
btrfs_add_bg_to_space_info(struct btrfs_fs_info * info,struct btrfs_block_group * block_group)2999d4b0a12SJosef Bacik void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
300723de71dSJosef Bacik 				struct btrfs_block_group *block_group)
301280c2908SJosef Bacik {
302280c2908SJosef Bacik 	struct btrfs_space_info *found;
303723de71dSJosef Bacik 	int factor, index;
304280c2908SJosef Bacik 
3059d4b0a12SJosef Bacik 	factor = btrfs_bg_type_to_factor(block_group->flags);
306280c2908SJosef Bacik 
3079d4b0a12SJosef Bacik 	found = btrfs_find_space_info(info, block_group->flags);
308280c2908SJosef Bacik 	ASSERT(found);
309280c2908SJosef Bacik 	spin_lock(&found->lock);
3109d4b0a12SJosef Bacik 	found->total_bytes += block_group->length;
3119d4b0a12SJosef Bacik 	found->disk_total += block_group->length * factor;
3129d4b0a12SJosef Bacik 	found->bytes_used += block_group->used;
3139d4b0a12SJosef Bacik 	found->disk_used += block_group->used * factor;
3149d4b0a12SJosef Bacik 	found->bytes_readonly += block_group->bytes_super;
315ae29e6f7SNaohiro Aota 	btrfs_space_info_update_bytes_zone_unusable(info, found, block_group->zone_unusable);
3169d4b0a12SJosef Bacik 	if (block_group->length > 0)
317280c2908SJosef Bacik 		found->full = 0;
31818fa2284SJosef Bacik 	btrfs_try_granting_tickets(info, found);
319280c2908SJosef Bacik 	spin_unlock(&found->lock);
320723de71dSJosef Bacik 
321723de71dSJosef Bacik 	block_group->space_info = found;
322723de71dSJosef Bacik 
323723de71dSJosef Bacik 	index = btrfs_bg_flags_to_raid_index(block_group->flags);
324723de71dSJosef Bacik 	down_write(&found->groups_sem);
325723de71dSJosef Bacik 	list_add_tail(&block_group->list, &found->block_groups[index]);
326723de71dSJosef Bacik 	up_write(&found->groups_sem);
327280c2908SJosef Bacik }
328280c2908SJosef Bacik 
btrfs_find_space_info(struct btrfs_fs_info * info,u64 flags)329280c2908SJosef Bacik struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
330280c2908SJosef Bacik 					       u64 flags)
331280c2908SJosef Bacik {
332280c2908SJosef Bacik 	struct list_head *head = &info->space_info;
333280c2908SJosef Bacik 	struct btrfs_space_info *found;
334280c2908SJosef Bacik 
335280c2908SJosef Bacik 	flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
336280c2908SJosef Bacik 
33772804905SJosef Bacik 	list_for_each_entry(found, head, list) {
33872804905SJosef Bacik 		if (found->flags & flags)
339280c2908SJosef Bacik 			return found;
340280c2908SJosef Bacik 	}
341280c2908SJosef Bacik 	return NULL;
342280c2908SJosef Bacik }
34341783ef2SJosef Bacik 
calc_available_free_space(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,enum btrfs_reserve_flush_enum flush)344fa121a26SJosef Bacik static u64 calc_available_free_space(struct btrfs_fs_info *fs_info,
345fa121a26SJosef Bacik 			  struct btrfs_space_info *space_info,
3469f246926SJosef Bacik 			  enum btrfs_reserve_flush_enum flush)
34741783ef2SJosef Bacik {
34841783ef2SJosef Bacik 	u64 profile;
34941783ef2SJosef Bacik 	u64 avail;
35041783ef2SJosef Bacik 	int factor;
35141783ef2SJosef Bacik 
3529f246926SJosef Bacik 	if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
35341783ef2SJosef Bacik 		profile = btrfs_system_alloc_profile(fs_info);
35441783ef2SJosef Bacik 	else
35541783ef2SJosef Bacik 		profile = btrfs_metadata_alloc_profile(fs_info);
35641783ef2SJosef Bacik 
35741783ef2SJosef Bacik 	avail = atomic64_read(&fs_info->free_chunk_space);
35841783ef2SJosef Bacik 
35941783ef2SJosef Bacik 	/*
36041783ef2SJosef Bacik 	 * If we have dup, raid1 or raid10 then only half of the free
36141783ef2SJosef Bacik 	 * space is actually usable.  For raid56, the space info used
36241783ef2SJosef Bacik 	 * doesn't include the parity drive, so we don't have to
36341783ef2SJosef Bacik 	 * change the math
36441783ef2SJosef Bacik 	 */
36541783ef2SJosef Bacik 	factor = btrfs_bg_type_to_factor(profile);
36641783ef2SJosef Bacik 	avail = div_u64(avail, factor);
36741783ef2SJosef Bacik 
36841783ef2SJosef Bacik 	/*
36941783ef2SJosef Bacik 	 * If we aren't flushing all things, let us overcommit up to
37041783ef2SJosef Bacik 	 * 1/2th of the space. If we can flush, don't let us overcommit
37141783ef2SJosef Bacik 	 * too much, let it overcommit up to 1/8 of the space.
37241783ef2SJosef Bacik 	 */
37341783ef2SJosef Bacik 	if (flush == BTRFS_RESERVE_FLUSH_ALL)
37441783ef2SJosef Bacik 		avail >>= 3;
37541783ef2SJosef Bacik 	else
37641783ef2SJosef Bacik 		avail >>= 1;
377fa121a26SJosef Bacik 	return avail;
378fa121a26SJosef Bacik }
379fa121a26SJosef Bacik 
btrfs_can_overcommit(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,u64 bytes,enum btrfs_reserve_flush_enum flush)380fa121a26SJosef Bacik int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
381fa121a26SJosef Bacik 			 struct btrfs_space_info *space_info, u64 bytes,
382fa121a26SJosef Bacik 			 enum btrfs_reserve_flush_enum flush)
383fa121a26SJosef Bacik {
384fa121a26SJosef Bacik 	u64 avail;
385fa121a26SJosef Bacik 	u64 used;
386fa121a26SJosef Bacik 
387fa121a26SJosef Bacik 	/* Don't overcommit when in mixed mode */
388fa121a26SJosef Bacik 	if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
389fa121a26SJosef Bacik 		return 0;
390fa121a26SJosef Bacik 
391fa121a26SJosef Bacik 	used = btrfs_space_info_used(space_info, true);
392fa121a26SJosef Bacik 	avail = calc_available_free_space(fs_info, space_info, flush);
39341783ef2SJosef Bacik 
394e15acc25SNaohiro Aota 	if (used + bytes < space_info->total_bytes + avail)
39541783ef2SJosef Bacik 		return 1;
39641783ef2SJosef Bacik 	return 0;
39741783ef2SJosef Bacik }
398b338b013SJosef Bacik 
remove_ticket(struct btrfs_space_info * space_info,struct reserve_ticket * ticket)399d611add4SFilipe Manana static void remove_ticket(struct btrfs_space_info *space_info,
400d611add4SFilipe Manana 			  struct reserve_ticket *ticket)
401d611add4SFilipe Manana {
402d611add4SFilipe Manana 	if (!list_empty(&ticket->list)) {
403d611add4SFilipe Manana 		list_del_init(&ticket->list);
404d611add4SFilipe Manana 		ASSERT(space_info->reclaim_size >= ticket->bytes);
405d611add4SFilipe Manana 		space_info->reclaim_size -= ticket->bytes;
406d611add4SFilipe Manana 	}
407d611add4SFilipe Manana }
408d611add4SFilipe Manana 
409b338b013SJosef Bacik /*
410b338b013SJosef Bacik  * This is for space we already have accounted in space_info->bytes_may_use, so
411b338b013SJosef Bacik  * basically when we're returning space from block_rsv's.
412b338b013SJosef Bacik  */
btrfs_try_granting_tickets(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)41318fa2284SJosef Bacik void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
41418fa2284SJosef Bacik 				struct btrfs_space_info *space_info)
415b338b013SJosef Bacik {
416b338b013SJosef Bacik 	struct list_head *head;
417b338b013SJosef Bacik 	enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
418b338b013SJosef Bacik 
41918fa2284SJosef Bacik 	lockdep_assert_held(&space_info->lock);
420b338b013SJosef Bacik 
42118fa2284SJosef Bacik 	head = &space_info->priority_tickets;
422b338b013SJosef Bacik again:
42391182645SJosef Bacik 	while (!list_empty(head)) {
42491182645SJosef Bacik 		struct reserve_ticket *ticket;
42591182645SJosef Bacik 		u64 used = btrfs_space_info_used(space_info, true);
42691182645SJosef Bacik 
42791182645SJosef Bacik 		ticket = list_first_entry(head, struct reserve_ticket, list);
42891182645SJosef Bacik 
4291a9fd417SDavid Sterba 		/* Check and see if our ticket can be satisfied now. */
430e15acc25SNaohiro Aota 		if ((used + ticket->bytes <= space_info->total_bytes) ||
431a30a3d20SJosef Bacik 		    btrfs_can_overcommit(fs_info, space_info, ticket->bytes,
432a30a3d20SJosef Bacik 					 flush)) {
43391182645SJosef Bacik 			btrfs_space_info_update_bytes_may_use(fs_info,
43491182645SJosef Bacik 							      space_info,
43591182645SJosef Bacik 							      ticket->bytes);
436d611add4SFilipe Manana 			remove_ticket(space_info, ticket);
437b338b013SJosef Bacik 			ticket->bytes = 0;
438b338b013SJosef Bacik 			space_info->tickets_id++;
439b338b013SJosef Bacik 			wake_up(&ticket->wait);
440b338b013SJosef Bacik 		} else {
44191182645SJosef Bacik 			break;
442b338b013SJosef Bacik 		}
443b338b013SJosef Bacik 	}
444b338b013SJosef Bacik 
44591182645SJosef Bacik 	if (head == &space_info->priority_tickets) {
446b338b013SJosef Bacik 		head = &space_info->tickets;
447b338b013SJosef Bacik 		flush = BTRFS_RESERVE_FLUSH_ALL;
448b338b013SJosef Bacik 		goto again;
449b338b013SJosef Bacik 	}
450b338b013SJosef Bacik }
4515da6afebSJosef Bacik 
4525da6afebSJosef Bacik #define DUMP_BLOCK_RSV(fs_info, rsv_name)				\
4535da6afebSJosef Bacik do {									\
4545da6afebSJosef Bacik 	struct btrfs_block_rsv *__rsv = &(fs_info)->rsv_name;		\
4555da6afebSJosef Bacik 	spin_lock(&__rsv->lock);					\
4565da6afebSJosef Bacik 	btrfs_info(fs_info, #rsv_name ": size %llu reserved %llu",	\
4575da6afebSJosef Bacik 		   __rsv->size, __rsv->reserved);			\
4585da6afebSJosef Bacik 	spin_unlock(&__rsv->lock);					\
4595da6afebSJosef Bacik } while (0)
4605da6afebSJosef Bacik 
space_info_flag_to_str(const struct btrfs_space_info * space_info)46125a860c4SQu Wenruo static const char *space_info_flag_to_str(const struct btrfs_space_info *space_info)
46225a860c4SQu Wenruo {
46325a860c4SQu Wenruo 	switch (space_info->flags) {
46425a860c4SQu Wenruo 	case BTRFS_BLOCK_GROUP_SYSTEM:
46525a860c4SQu Wenruo 		return "SYSTEM";
46625a860c4SQu Wenruo 	case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
46725a860c4SQu Wenruo 		return "DATA+METADATA";
46825a860c4SQu Wenruo 	case BTRFS_BLOCK_GROUP_DATA:
46925a860c4SQu Wenruo 		return "DATA";
47025a860c4SQu Wenruo 	case BTRFS_BLOCK_GROUP_METADATA:
47125a860c4SQu Wenruo 		return "METADATA";
47225a860c4SQu Wenruo 	default:
47325a860c4SQu Wenruo 		return "UNKNOWN";
47425a860c4SQu Wenruo 	}
47525a860c4SQu Wenruo }
47625a860c4SQu Wenruo 
dump_global_block_rsv(struct btrfs_fs_info * fs_info)4778e327b9cSQu Wenruo static void dump_global_block_rsv(struct btrfs_fs_info *fs_info)
4788e327b9cSQu Wenruo {
4798e327b9cSQu Wenruo 	DUMP_BLOCK_RSV(fs_info, global_block_rsv);
4808e327b9cSQu Wenruo 	DUMP_BLOCK_RSV(fs_info, trans_block_rsv);
4818e327b9cSQu Wenruo 	DUMP_BLOCK_RSV(fs_info, chunk_block_rsv);
4828e327b9cSQu Wenruo 	DUMP_BLOCK_RSV(fs_info, delayed_block_rsv);
4838e327b9cSQu Wenruo 	DUMP_BLOCK_RSV(fs_info, delayed_refs_rsv);
4848e327b9cSQu Wenruo }
4858e327b9cSQu Wenruo 
__btrfs_dump_space_info(struct btrfs_fs_info * fs_info,struct btrfs_space_info * info)48684fe47a4SJosef Bacik static void __btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
48784fe47a4SJosef Bacik 				    struct btrfs_space_info *info)
4885da6afebSJosef Bacik {
48925a860c4SQu Wenruo 	const char *flag_str = space_info_flag_to_str(info);
49084fe47a4SJosef Bacik 	lockdep_assert_held(&info->lock);
4915da6afebSJosef Bacik 
4920619b790SQu Wenruo 	/* The free space could be negative in case of overcommit */
49325a860c4SQu Wenruo 	btrfs_info(fs_info, "space_info %s has %lld free, is %sfull",
49425a860c4SQu Wenruo 		   flag_str,
4950619b790SQu Wenruo 		   (s64)(info->total_bytes - btrfs_space_info_used(info, true)),
4965da6afebSJosef Bacik 		   info->full ? "" : "not ");
4975da6afebSJosef Bacik 	btrfs_info(fs_info,
498169e0da9SNaohiro Aota "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu zone_unusable=%llu",
4995da6afebSJosef Bacik 		info->total_bytes, info->bytes_used, info->bytes_pinned,
5005da6afebSJosef Bacik 		info->bytes_reserved, info->bytes_may_use,
501169e0da9SNaohiro Aota 		info->bytes_readonly, info->bytes_zone_unusable);
50284fe47a4SJosef Bacik }
50384fe47a4SJosef Bacik 
btrfs_dump_space_info(struct btrfs_fs_info * fs_info,struct btrfs_space_info * info,u64 bytes,int dump_block_groups)50484fe47a4SJosef Bacik void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
50584fe47a4SJosef Bacik 			   struct btrfs_space_info *info, u64 bytes,
50684fe47a4SJosef Bacik 			   int dump_block_groups)
50784fe47a4SJosef Bacik {
50832da5386SDavid Sterba 	struct btrfs_block_group *cache;
5091ff9fee3SFilipe Manana 	u64 total_avail = 0;
51084fe47a4SJosef Bacik 	int index = 0;
51184fe47a4SJosef Bacik 
51284fe47a4SJosef Bacik 	spin_lock(&info->lock);
51384fe47a4SJosef Bacik 	__btrfs_dump_space_info(fs_info, info);
5148e327b9cSQu Wenruo 	dump_global_block_rsv(fs_info);
51584fe47a4SJosef Bacik 	spin_unlock(&info->lock);
51684fe47a4SJosef Bacik 
5175da6afebSJosef Bacik 	if (!dump_block_groups)
5185da6afebSJosef Bacik 		return;
5195da6afebSJosef Bacik 
5205da6afebSJosef Bacik 	down_read(&info->groups_sem);
5215da6afebSJosef Bacik again:
5225da6afebSJosef Bacik 	list_for_each_entry(cache, &info->block_groups[index], list) {
523e50b122bSFilipe Manana 		u64 avail;
524e50b122bSFilipe Manana 
5255da6afebSJosef Bacik 		spin_lock(&cache->lock);
526e50b122bSFilipe Manana 		avail = cache->length - cache->used - cache->pinned -
527*0caf15beSNaohiro Aota 			cache->reserved - cache->bytes_super - cache->zone_unusable;
5285da6afebSJosef Bacik 		btrfs_info(fs_info,
529e50b122bSFilipe Manana "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %llu delalloc %llu super %llu zone_unusable (%llu bytes available) %s",
530b3470b5dSDavid Sterba 			   cache->start, cache->length, cache->used, cache->pinned,
531b92e8f54SFilipe Manana 			   cache->reserved, cache->delalloc_bytes,
532b92e8f54SFilipe Manana 			   cache->bytes_super, cache->zone_unusable,
533e50b122bSFilipe Manana 			   avail, cache->ro ? "[readonly]" : "");
5345da6afebSJosef Bacik 		spin_unlock(&cache->lock);
535ab0db043SJosef Bacik 		btrfs_dump_free_space(cache, bytes);
5361ff9fee3SFilipe Manana 		total_avail += avail;
5375da6afebSJosef Bacik 	}
5385da6afebSJosef Bacik 	if (++index < BTRFS_NR_RAID_TYPES)
5395da6afebSJosef Bacik 		goto again;
5405da6afebSJosef Bacik 	up_read(&info->groups_sem);
5411ff9fee3SFilipe Manana 
5421ff9fee3SFilipe Manana 	btrfs_info(fs_info, "%llu bytes available across all block groups", total_avail);
5435da6afebSJosef Bacik }
5440d9764f6SJosef Bacik 
calc_reclaim_items_nr(const struct btrfs_fs_info * fs_info,u64 to_reclaim)545f4160ee8SFilipe Manana static inline u64 calc_reclaim_items_nr(const struct btrfs_fs_info *fs_info,
5460d9764f6SJosef Bacik 					u64 to_reclaim)
5470d9764f6SJosef Bacik {
5480d9764f6SJosef Bacik 	u64 bytes;
5490d9764f6SJosef Bacik 	u64 nr;
5500d9764f6SJosef Bacik 
5512bd36e7bSJosef Bacik 	bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
5520d9764f6SJosef Bacik 	nr = div64_u64(to_reclaim, bytes);
5530d9764f6SJosef Bacik 	if (!nr)
5540d9764f6SJosef Bacik 		nr = 1;
5550d9764f6SJosef Bacik 	return nr;
5560d9764f6SJosef Bacik }
5570d9764f6SJosef Bacik 
calc_delayed_refs_nr(const struct btrfs_fs_info * fs_info,u64 to_reclaim)558f4160ee8SFilipe Manana static inline u64 calc_delayed_refs_nr(const struct btrfs_fs_info *fs_info,
559007145ffSFilipe Manana 				       u64 to_reclaim)
560007145ffSFilipe Manana {
5610e55a545SFilipe Manana 	const u64 bytes = btrfs_calc_delayed_ref_bytes(fs_info, 1);
562007145ffSFilipe Manana 	u64 nr;
563007145ffSFilipe Manana 
564007145ffSFilipe Manana 	nr = div64_u64(to_reclaim, bytes);
565007145ffSFilipe Manana 	if (!nr)
566007145ffSFilipe Manana 		nr = 1;
567007145ffSFilipe Manana 	return nr;
568007145ffSFilipe Manana }
569007145ffSFilipe Manana 
5700d9764f6SJosef Bacik #define EXTENT_SIZE_PER_ITEM	SZ_256K
5710d9764f6SJosef Bacik 
5720d9764f6SJosef Bacik /*
5730d9764f6SJosef Bacik  * shrink metadata reservation for delalloc
5740d9764f6SJosef Bacik  */
shrink_delalloc(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,u64 to_reclaim,bool wait_ordered,bool for_preempt)575920a9958SJosef Bacik static void shrink_delalloc(struct btrfs_fs_info *fs_info,
576920a9958SJosef Bacik 			    struct btrfs_space_info *space_info,
577385f421fSJosef Bacik 			    u64 to_reclaim, bool wait_ordered,
578385f421fSJosef Bacik 			    bool for_preempt)
5790d9764f6SJosef Bacik {
5800d9764f6SJosef Bacik 	struct btrfs_trans_handle *trans;
5810d9764f6SJosef Bacik 	u64 delalloc_bytes;
5825deb17e1SJosef Bacik 	u64 ordered_bytes;
5830d9764f6SJosef Bacik 	u64 items;
5840d9764f6SJosef Bacik 	long time_left;
5850d9764f6SJosef Bacik 	int loops;
5860d9764f6SJosef Bacik 
58703fe78ccSJosef Bacik 	delalloc_bytes = percpu_counter_sum_positive(&fs_info->delalloc_bytes);
58803fe78ccSJosef Bacik 	ordered_bytes = percpu_counter_sum_positive(&fs_info->ordered_bytes);
58903fe78ccSJosef Bacik 	if (delalloc_bytes == 0 && ordered_bytes == 0)
59003fe78ccSJosef Bacik 		return;
59103fe78ccSJosef Bacik 
5920d9764f6SJosef Bacik 	/* Calc the number of the pages we need flush for space reservation */
593d7f81facSJosef Bacik 	if (to_reclaim == U64_MAX) {
594d7f81facSJosef Bacik 		items = U64_MAX;
595d7f81facSJosef Bacik 	} else {
596d7f81facSJosef Bacik 		/*
597d7f81facSJosef Bacik 		 * to_reclaim is set to however much metadata we need to
598d7f81facSJosef Bacik 		 * reclaim, but reclaiming that much data doesn't really track
59903fe78ccSJosef Bacik 		 * exactly.  What we really want to do is reclaim full inode's
60003fe78ccSJosef Bacik 		 * worth of reservations, however that's not available to us
60103fe78ccSJosef Bacik 		 * here.  We will take a fraction of the delalloc bytes for our
60203fe78ccSJosef Bacik 		 * flushing loops and hope for the best.  Delalloc will expand
60303fe78ccSJosef Bacik 		 * the amount we write to cover an entire dirty extent, which
60403fe78ccSJosef Bacik 		 * will reclaim the metadata reservation for that range.  If
60503fe78ccSJosef Bacik 		 * it's not enough subsequent flush stages will be more
60603fe78ccSJosef Bacik 		 * aggressive.
607d7f81facSJosef Bacik 		 */
60803fe78ccSJosef Bacik 		to_reclaim = max(to_reclaim, delalloc_bytes >> 3);
609d7f81facSJosef Bacik 		items = calc_reclaim_items_nr(fs_info, to_reclaim) * 2;
610d7f81facSJosef Bacik 	}
6110d9764f6SJosef Bacik 
6120d031dc4SYu Zhe 	trans = current->journal_info;
6130d9764f6SJosef Bacik 
6140d9764f6SJosef Bacik 	/*
6150d9764f6SJosef Bacik 	 * If we are doing more ordered than delalloc we need to just wait on
6160d9764f6SJosef Bacik 	 * ordered extents, otherwise we'll waste time trying to flush delalloc
6170d9764f6SJosef Bacik 	 * that likely won't give us the space back we need.
6180d9764f6SJosef Bacik 	 */
619385f421fSJosef Bacik 	if (ordered_bytes > delalloc_bytes && !for_preempt)
6200d9764f6SJosef Bacik 		wait_ordered = true;
6210d9764f6SJosef Bacik 
6220d9764f6SJosef Bacik 	loops = 0;
6235deb17e1SJosef Bacik 	while ((delalloc_bytes || ordered_bytes) && loops < 3) {
6249db4dc24SNikolay Borisov 		u64 temp = min(delalloc_bytes, to_reclaim) >> PAGE_SHIFT;
6259db4dc24SNikolay Borisov 		long nr_pages = min_t(u64, temp, LONG_MAX);
626e1646070SJosef Bacik 		int async_pages;
627e076ab2aSJosef Bacik 
628e076ab2aSJosef Bacik 		btrfs_start_delalloc_roots(fs_info, nr_pages, true);
6290d9764f6SJosef Bacik 
630e1646070SJosef Bacik 		/*
631e1646070SJosef Bacik 		 * We need to make sure any outstanding async pages are now
632e1646070SJosef Bacik 		 * processed before we continue.  This is because things like
633e1646070SJosef Bacik 		 * sync_inode() try to be smart and skip writing if the inode is
634e1646070SJosef Bacik 		 * marked clean.  We don't use filemap_fwrite for flushing
635e1646070SJosef Bacik 		 * because we want to control how many pages we write out at a
636e1646070SJosef Bacik 		 * time, thus this is the only safe way to make sure we've
637e1646070SJosef Bacik 		 * waited for outstanding compressed workers to have started
638e1646070SJosef Bacik 		 * their jobs and thus have ordered extents set up properly.
639e1646070SJosef Bacik 		 *
640e1646070SJosef Bacik 		 * This exists because we do not want to wait for each
641e1646070SJosef Bacik 		 * individual inode to finish its async work, we simply want to
642e1646070SJosef Bacik 		 * start the IO on everybody, and then come back here and wait
643e1646070SJosef Bacik 		 * for all of the async work to catch up.  Once we're done with
644e1646070SJosef Bacik 		 * that we know we'll have ordered extents for everything and we
645e1646070SJosef Bacik 		 * can decide if we wait for that or not.
646e1646070SJosef Bacik 		 *
647e1646070SJosef Bacik 		 * If we choose to replace this in the future, make absolutely
648e1646070SJosef Bacik 		 * sure that the proper waiting is being done in the async case,
649e1646070SJosef Bacik 		 * as there have been bugs in that area before.
650e1646070SJosef Bacik 		 */
651e1646070SJosef Bacik 		async_pages = atomic_read(&fs_info->async_delalloc_pages);
652e1646070SJosef Bacik 		if (!async_pages)
653e1646070SJosef Bacik 			goto skip_async;
654e1646070SJosef Bacik 
655e1646070SJosef Bacik 		/*
656e1646070SJosef Bacik 		 * We don't want to wait forever, if we wrote less pages in this
657e1646070SJosef Bacik 		 * loop than we have outstanding, only wait for that number of
658e1646070SJosef Bacik 		 * pages, otherwise we can wait for all async pages to finish
659e1646070SJosef Bacik 		 * before continuing.
660e1646070SJosef Bacik 		 */
661e1646070SJosef Bacik 		if (async_pages > nr_pages)
662e1646070SJosef Bacik 			async_pages -= nr_pages;
663e1646070SJosef Bacik 		else
664e1646070SJosef Bacik 			async_pages = 0;
665e1646070SJosef Bacik 		wait_event(fs_info->async_submit_wait,
666e1646070SJosef Bacik 			   atomic_read(&fs_info->async_delalloc_pages) <=
667e1646070SJosef Bacik 			   async_pages);
668e1646070SJosef Bacik skip_async:
6690d9764f6SJosef Bacik 		loops++;
6700d9764f6SJosef Bacik 		if (wait_ordered && !trans) {
6710d9764f6SJosef Bacik 			btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
6720d9764f6SJosef Bacik 		} else {
6730d9764f6SJosef Bacik 			time_left = schedule_timeout_killable(1);
6740d9764f6SJosef Bacik 			if (time_left)
6750d9764f6SJosef Bacik 				break;
6760d9764f6SJosef Bacik 		}
677448b966bSJosef Bacik 
678385f421fSJosef Bacik 		/*
679385f421fSJosef Bacik 		 * If we are for preemption we just want a one-shot of delalloc
680385f421fSJosef Bacik 		 * flushing so we can stop flushing if we decide we don't need
681385f421fSJosef Bacik 		 * to anymore.
682385f421fSJosef Bacik 		 */
683385f421fSJosef Bacik 		if (for_preempt)
684385f421fSJosef Bacik 			break;
685385f421fSJosef Bacik 
686448b966bSJosef Bacik 		spin_lock(&space_info->lock);
687448b966bSJosef Bacik 		if (list_empty(&space_info->tickets) &&
688448b966bSJosef Bacik 		    list_empty(&space_info->priority_tickets)) {
689448b966bSJosef Bacik 			spin_unlock(&space_info->lock);
690448b966bSJosef Bacik 			break;
691448b966bSJosef Bacik 		}
692448b966bSJosef Bacik 		spin_unlock(&space_info->lock);
693448b966bSJosef Bacik 
6940d9764f6SJosef Bacik 		delalloc_bytes = percpu_counter_sum_positive(
6950d9764f6SJosef Bacik 						&fs_info->delalloc_bytes);
6965deb17e1SJosef Bacik 		ordered_bytes = percpu_counter_sum_positive(
6975deb17e1SJosef Bacik 						&fs_info->ordered_bytes);
6980d9764f6SJosef Bacik 	}
6990d9764f6SJosef Bacik }
7000d9764f6SJosef Bacik 
7010d9764f6SJosef Bacik /*
7020d9764f6SJosef Bacik  * Try to flush some data based on policy set by @state. This is only advisory
7030d9764f6SJosef Bacik  * and may fail for various reasons. The caller is supposed to examine the
7040d9764f6SJosef Bacik  * state of @space_info to detect the outcome.
7050d9764f6SJosef Bacik  */
flush_space(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,u64 num_bytes,enum btrfs_flush_state state,bool for_preempt)7060d9764f6SJosef Bacik static void flush_space(struct btrfs_fs_info *fs_info,
7070d9764f6SJosef Bacik 		       struct btrfs_space_info *space_info, u64 num_bytes,
7084b02b00fSJosef Bacik 		       enum btrfs_flush_state state, bool for_preempt)
7090d9764f6SJosef Bacik {
710ce5603d0SJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
7110d9764f6SJosef Bacik 	struct btrfs_trans_handle *trans;
7120d9764f6SJosef Bacik 	int nr;
7130d9764f6SJosef Bacik 	int ret = 0;
7140d9764f6SJosef Bacik 
7150d9764f6SJosef Bacik 	switch (state) {
7160d9764f6SJosef Bacik 	case FLUSH_DELAYED_ITEMS_NR:
7170d9764f6SJosef Bacik 	case FLUSH_DELAYED_ITEMS:
7180d9764f6SJosef Bacik 		if (state == FLUSH_DELAYED_ITEMS_NR)
7190d9764f6SJosef Bacik 			nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
7200d9764f6SJosef Bacik 		else
7210d9764f6SJosef Bacik 			nr = -1;
7220d9764f6SJosef Bacik 
7232391245aSFilipe Manana 		trans = btrfs_join_transaction_nostart(root);
7240d9764f6SJosef Bacik 		if (IS_ERR(trans)) {
7250d9764f6SJosef Bacik 			ret = PTR_ERR(trans);
7262391245aSFilipe Manana 			if (ret == -ENOENT)
7272391245aSFilipe Manana 				ret = 0;
7280d9764f6SJosef Bacik 			break;
7290d9764f6SJosef Bacik 		}
7300d9764f6SJosef Bacik 		ret = btrfs_run_delayed_items_nr(trans, nr);
7310d9764f6SJosef Bacik 		btrfs_end_transaction(trans);
7320d9764f6SJosef Bacik 		break;
7330d9764f6SJosef Bacik 	case FLUSH_DELALLOC:
7340d9764f6SJosef Bacik 	case FLUSH_DELALLOC_WAIT:
73503fe78ccSJosef Bacik 	case FLUSH_DELALLOC_FULL:
73603fe78ccSJosef Bacik 		if (state == FLUSH_DELALLOC_FULL)
73703fe78ccSJosef Bacik 			num_bytes = U64_MAX;
738920a9958SJosef Bacik 		shrink_delalloc(fs_info, space_info, num_bytes,
73903fe78ccSJosef Bacik 				state != FLUSH_DELALLOC, for_preempt);
7400d9764f6SJosef Bacik 		break;
7410d9764f6SJosef Bacik 	case FLUSH_DELAYED_REFS_NR:
7420d9764f6SJosef Bacik 	case FLUSH_DELAYED_REFS:
7432391245aSFilipe Manana 		trans = btrfs_join_transaction_nostart(root);
7440d9764f6SJosef Bacik 		if (IS_ERR(trans)) {
7450d9764f6SJosef Bacik 			ret = PTR_ERR(trans);
7462391245aSFilipe Manana 			if (ret == -ENOENT)
7472391245aSFilipe Manana 				ret = 0;
7480d9764f6SJosef Bacik 			break;
7490d9764f6SJosef Bacik 		}
7500d9764f6SJosef Bacik 		if (state == FLUSH_DELAYED_REFS_NR)
751007145ffSFilipe Manana 			nr = calc_delayed_refs_nr(fs_info, num_bytes);
7520d9764f6SJosef Bacik 		else
7530d9764f6SJosef Bacik 			nr = 0;
7540d9764f6SJosef Bacik 		btrfs_run_delayed_refs(trans, nr);
7550d9764f6SJosef Bacik 		btrfs_end_transaction(trans);
7560d9764f6SJosef Bacik 		break;
7570d9764f6SJosef Bacik 	case ALLOC_CHUNK:
7580d9764f6SJosef Bacik 	case ALLOC_CHUNK_FORCE:
7590d9764f6SJosef Bacik 		trans = btrfs_join_transaction(root);
7600d9764f6SJosef Bacik 		if (IS_ERR(trans)) {
7610d9764f6SJosef Bacik 			ret = PTR_ERR(trans);
7620d9764f6SJosef Bacik 			break;
7630d9764f6SJosef Bacik 		}
7640d9764f6SJosef Bacik 		ret = btrfs_chunk_alloc(trans,
765c6c45303SJosef Bacik 				btrfs_get_alloc_profile(fs_info, space_info->flags),
7660d9764f6SJosef Bacik 				(state == ALLOC_CHUNK) ? CHUNK_ALLOC_NO_FORCE :
7670d9764f6SJosef Bacik 					CHUNK_ALLOC_FORCE);
7680d9764f6SJosef Bacik 		btrfs_end_transaction(trans);
769b0931513SNaohiro Aota 
7700d9764f6SJosef Bacik 		if (ret > 0 || ret == -ENOSPC)
7710d9764f6SJosef Bacik 			ret = 0;
7720d9764f6SJosef Bacik 		break;
773844245b4SJosef Bacik 	case RUN_DELAYED_IPUTS:
7740d9764f6SJosef Bacik 		/*
7750d9764f6SJosef Bacik 		 * If we have pending delayed iputs then we could free up a
7760d9764f6SJosef Bacik 		 * bunch of pinned space, so make sure we run the iputs before
7770d9764f6SJosef Bacik 		 * we do our pinned bytes check below.
7780d9764f6SJosef Bacik 		 */
7790d9764f6SJosef Bacik 		btrfs_run_delayed_iputs(fs_info);
7800d9764f6SJosef Bacik 		btrfs_wait_on_delayed_iputs(fs_info);
781844245b4SJosef Bacik 		break;
782844245b4SJosef Bacik 	case COMMIT_TRANS:
783c416a30cSJosef Bacik 		ASSERT(current->journal_info == NULL);
7842ee70ed1SFilipe Manana 		/*
7852ee70ed1SFilipe Manana 		 * We don't want to start a new transaction, just attach to the
7862ee70ed1SFilipe Manana 		 * current one or wait it fully commits in case its commit is
7872ee70ed1SFilipe Manana 		 * happening at the moment. Note: we don't use a nostart join
7882ee70ed1SFilipe Manana 		 * because that does not wait for a transaction to fully commit
7892ee70ed1SFilipe Manana 		 * (only for it to be unblocked, state TRANS_STATE_UNBLOCKED).
7902ee70ed1SFilipe Manana 		 */
7912ee70ed1SFilipe Manana 		trans = btrfs_attach_transaction_barrier(root);
792f00c42ddSJosef Bacik 		if (IS_ERR(trans)) {
793f00c42ddSJosef Bacik 			ret = PTR_ERR(trans);
7942ee70ed1SFilipe Manana 			if (ret == -ENOENT)
7952ee70ed1SFilipe Manana 				ret = 0;
796f00c42ddSJosef Bacik 			break;
797f00c42ddSJosef Bacik 		}
798f00c42ddSJosef Bacik 		ret = btrfs_commit_transaction(trans);
799f00c42ddSJosef Bacik 		break;
8000d9764f6SJosef Bacik 	default:
8010d9764f6SJosef Bacik 		ret = -ENOSPC;
8020d9764f6SJosef Bacik 		break;
8030d9764f6SJosef Bacik 	}
8040d9764f6SJosef Bacik 
8050d9764f6SJosef Bacik 	trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
8064b02b00fSJosef Bacik 				ret, for_preempt);
8070d9764f6SJosef Bacik 	return;
8080d9764f6SJosef Bacik }
8090d9764f6SJosef Bacik 
8100d9764f6SJosef Bacik static inline u64
btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)8110d9764f6SJosef Bacik btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
8129f246926SJosef Bacik 				 struct btrfs_space_info *space_info)
8130d9764f6SJosef Bacik {
8140d9764f6SJosef Bacik 	u64 used;
815fa121a26SJosef Bacik 	u64 avail;
816db161806SNikolay Borisov 	u64 to_reclaim = space_info->reclaim_size;
8170d9764f6SJosef Bacik 
818db161806SNikolay Borisov 	lockdep_assert_held(&space_info->lock);
819fa121a26SJosef Bacik 
820fa121a26SJosef Bacik 	avail = calc_available_free_space(fs_info, space_info,
821fa121a26SJosef Bacik 					  BTRFS_RESERVE_FLUSH_ALL);
822fa121a26SJosef Bacik 	used = btrfs_space_info_used(space_info, true);
823fa121a26SJosef Bacik 
824fa121a26SJosef Bacik 	/*
825fa121a26SJosef Bacik 	 * We may be flushing because suddenly we have less space than we had
826fa121a26SJosef Bacik 	 * before, and now we're well over-committed based on our current free
827fa121a26SJosef Bacik 	 * space.  If that's the case add in our overage so we make sure to put
828fa121a26SJosef Bacik 	 * appropriate pressure on the flushing state machine.
829fa121a26SJosef Bacik 	 */
830e15acc25SNaohiro Aota 	if (space_info->total_bytes + avail < used)
831e15acc25SNaohiro Aota 		to_reclaim += used - (space_info->total_bytes + avail);
832fa121a26SJosef Bacik 
8330d9764f6SJosef Bacik 	return to_reclaim;
8340d9764f6SJosef Bacik }
8350d9764f6SJosef Bacik 
need_preemptive_reclaim(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)836ae7913baSJosef Bacik static bool need_preemptive_reclaim(struct btrfs_fs_info *fs_info,
8372e294c60SJosef Bacik 				    struct btrfs_space_info *space_info)
8380d9764f6SJosef Bacik {
83982220b18SFilipe Manana 	const u64 global_rsv_size = btrfs_block_rsv_reserved(&fs_info->global_block_rsv);
8402e294c60SJosef Bacik 	u64 ordered, delalloc;
8416a921de5SNaohiro Aota 	u64 thresh;
8422e294c60SJosef Bacik 	u64 used;
8430d9764f6SJosef Bacik 
844e15acc25SNaohiro Aota 	thresh = mult_perc(space_info->total_bytes, 90);
8456a921de5SNaohiro Aota 
846bf7bd725SNiels Dossche 	lockdep_assert_held(&space_info->lock);
847bf7bd725SNiels Dossche 
8480d9764f6SJosef Bacik 	/* If we're just plain full then async reclaim just slows us down. */
849610a6ef4SJosef Bacik 	if ((space_info->bytes_used + space_info->bytes_reserved +
850610a6ef4SJosef Bacik 	     global_rsv_size) >= thresh)
851ae7913baSJosef Bacik 		return false;
8520d9764f6SJosef Bacik 
85311462397SJosef Bacik 	used = space_info->bytes_may_use + space_info->bytes_pinned;
85411462397SJosef Bacik 
85511462397SJosef Bacik 	/* The total flushable belongs to the global rsv, don't flush. */
85611462397SJosef Bacik 	if (global_rsv_size >= used)
85711462397SJosef Bacik 		return false;
85811462397SJosef Bacik 
85911462397SJosef Bacik 	/*
86011462397SJosef Bacik 	 * 128MiB is 1/4 of the maximum global rsv size.  If we have less than
86111462397SJosef Bacik 	 * that devoted to other reservations then there's no sense in flushing,
86211462397SJosef Bacik 	 * we don't have a lot of things that need flushing.
86311462397SJosef Bacik 	 */
86411462397SJosef Bacik 	if (used - global_rsv_size <= SZ_128M)
86511462397SJosef Bacik 		return false;
86611462397SJosef Bacik 
867f205edf7SJosef Bacik 	/*
868f205edf7SJosef Bacik 	 * We have tickets queued, bail so we don't compete with the async
869f205edf7SJosef Bacik 	 * flushers.
870f205edf7SJosef Bacik 	 */
871f205edf7SJosef Bacik 	if (space_info->reclaim_size)
872f205edf7SJosef Bacik 		return false;
873f205edf7SJosef Bacik 
8742e294c60SJosef Bacik 	/*
8752e294c60SJosef Bacik 	 * If we have over half of the free space occupied by reservations or
8762e294c60SJosef Bacik 	 * pinned then we want to start flushing.
8772e294c60SJosef Bacik 	 *
8782e294c60SJosef Bacik 	 * We do not do the traditional thing here, which is to say
8792e294c60SJosef Bacik 	 *
8802e294c60SJosef Bacik 	 *   if (used >= ((total_bytes + avail) / 2))
8812e294c60SJosef Bacik 	 *     return 1;
8822e294c60SJosef Bacik 	 *
8832e294c60SJosef Bacik 	 * because this doesn't quite work how we want.  If we had more than 50%
8842e294c60SJosef Bacik 	 * of the space_info used by bytes_used and we had 0 available we'd just
8852e294c60SJosef Bacik 	 * constantly run the background flusher.  Instead we want it to kick in
88688a777a6SJosef Bacik 	 * if our reclaimable space exceeds our clamped free space.
88788a777a6SJosef Bacik 	 *
88888a777a6SJosef Bacik 	 * Our clamping range is 2^1 -> 2^8.  Practically speaking that means
88988a777a6SJosef Bacik 	 * the following:
89088a777a6SJosef Bacik 	 *
89188a777a6SJosef Bacik 	 * Amount of RAM        Minimum threshold       Maximum threshold
89288a777a6SJosef Bacik 	 *
89388a777a6SJosef Bacik 	 *        256GiB                     1GiB                  128GiB
89488a777a6SJosef Bacik 	 *        128GiB                   512MiB                   64GiB
89588a777a6SJosef Bacik 	 *         64GiB                   256MiB                   32GiB
89688a777a6SJosef Bacik 	 *         32GiB                   128MiB                   16GiB
89788a777a6SJosef Bacik 	 *         16GiB                    64MiB                    8GiB
89888a777a6SJosef Bacik 	 *
89988a777a6SJosef Bacik 	 * These are the range our thresholds will fall in, corresponding to how
90088a777a6SJosef Bacik 	 * much delalloc we need for the background flusher to kick in.
9012e294c60SJosef Bacik 	 */
90288a777a6SJosef Bacik 
9032e294c60SJosef Bacik 	thresh = calc_available_free_space(fs_info, space_info,
9042e294c60SJosef Bacik 					   BTRFS_RESERVE_FLUSH_ALL);
9051239e2daSJosef Bacik 	used = space_info->bytes_used + space_info->bytes_reserved +
9061239e2daSJosef Bacik 	       space_info->bytes_readonly + global_rsv_size;
907e15acc25SNaohiro Aota 	if (used < space_info->total_bytes)
908e15acc25SNaohiro Aota 		thresh += space_info->total_bytes - used;
90988a777a6SJosef Bacik 	thresh >>= space_info->clamp;
9109f42d377SJosef Bacik 
9112e294c60SJosef Bacik 	used = space_info->bytes_pinned;
9129f42d377SJosef Bacik 
9132e294c60SJosef Bacik 	/*
9142e294c60SJosef Bacik 	 * If we have more ordered bytes than delalloc bytes then we're either
9152e294c60SJosef Bacik 	 * doing a lot of DIO, or we simply don't have a lot of delalloc waiting
9162e294c60SJosef Bacik 	 * around.  Preemptive flushing is only useful in that it can free up
9172e294c60SJosef Bacik 	 * space before tickets need to wait for things to finish.  In the case
9182e294c60SJosef Bacik 	 * of ordered extents, preemptively waiting on ordered extents gets us
9192e294c60SJosef Bacik 	 * nothing, if our reservations are tied up in ordered extents we'll
9202e294c60SJosef Bacik 	 * simply have to slow down writers by forcing them to wait on ordered
9212e294c60SJosef Bacik 	 * extents.
9222e294c60SJosef Bacik 	 *
9232e294c60SJosef Bacik 	 * In the case that ordered is larger than delalloc, only include the
9242e294c60SJosef Bacik 	 * block reserves that we would actually be able to directly reclaim
9252e294c60SJosef Bacik 	 * from.  In this case if we're heavy on metadata operations this will
9262e294c60SJosef Bacik 	 * clearly be heavy enough to warrant preemptive flushing.  In the case
9272e294c60SJosef Bacik 	 * of heavy DIO or ordered reservations, preemptive flushing will just
9282e294c60SJosef Bacik 	 * waste time and cause us to slow down.
9293e101569SJosef Bacik 	 *
9303e101569SJosef Bacik 	 * We want to make sure we truly are maxed out on ordered however, so
9313e101569SJosef Bacik 	 * cut ordered in half, and if it's still higher than delalloc then we
9323e101569SJosef Bacik 	 * can keep flushing.  This is to avoid the case where we start
9333e101569SJosef Bacik 	 * flushing, and now delalloc == ordered and we stop preemptively
9343e101569SJosef Bacik 	 * flushing when we could still have several gigs of delalloc to flush.
9352e294c60SJosef Bacik 	 */
9363e101569SJosef Bacik 	ordered = percpu_counter_read_positive(&fs_info->ordered_bytes) >> 1;
9372cdb3909SJosef Bacik 	delalloc = percpu_counter_read_positive(&fs_info->delalloc_bytes);
9382e294c60SJosef Bacik 	if (ordered >= delalloc)
93982220b18SFilipe Manana 		used += btrfs_block_rsv_reserved(&fs_info->delayed_refs_rsv) +
94082220b18SFilipe Manana 			btrfs_block_rsv_reserved(&fs_info->delayed_block_rsv);
9419f42d377SJosef Bacik 	else
94230acce4eSJosef Bacik 		used += space_info->bytes_may_use - global_rsv_size;
9430d9764f6SJosef Bacik 
9440d9764f6SJosef Bacik 	return (used >= thresh && !btrfs_fs_closing(fs_info) &&
9450d9764f6SJosef Bacik 		!test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
9460d9764f6SJosef Bacik }
9470d9764f6SJosef Bacik 
steal_from_global_rsv(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,struct reserve_ticket * ticket)9487f9fe614SJosef Bacik static bool steal_from_global_rsv(struct btrfs_fs_info *fs_info,
9497f9fe614SJosef Bacik 				  struct btrfs_space_info *space_info,
9507f9fe614SJosef Bacik 				  struct reserve_ticket *ticket)
9517f9fe614SJosef Bacik {
9527f9fe614SJosef Bacik 	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
9537f9fe614SJosef Bacik 	u64 min_bytes;
9547f9fe614SJosef Bacik 
9551b0309eaSJosef Bacik 	if (!ticket->steal)
9561b0309eaSJosef Bacik 		return false;
9571b0309eaSJosef Bacik 
9587f9fe614SJosef Bacik 	if (global_rsv->space_info != space_info)
9597f9fe614SJosef Bacik 		return false;
9607f9fe614SJosef Bacik 
9617f9fe614SJosef Bacik 	spin_lock(&global_rsv->lock);
962428c8e03SDavid Sterba 	min_bytes = mult_perc(global_rsv->size, 10);
9637f9fe614SJosef Bacik 	if (global_rsv->reserved < min_bytes + ticket->bytes) {
9647f9fe614SJosef Bacik 		spin_unlock(&global_rsv->lock);
9657f9fe614SJosef Bacik 		return false;
9667f9fe614SJosef Bacik 	}
9677f9fe614SJosef Bacik 	global_rsv->reserved -= ticket->bytes;
9686d548b9eSFilipe Manana 	remove_ticket(space_info, ticket);
9697f9fe614SJosef Bacik 	ticket->bytes = 0;
9707f9fe614SJosef Bacik 	wake_up(&ticket->wait);
9717f9fe614SJosef Bacik 	space_info->tickets_id++;
9727f9fe614SJosef Bacik 	if (global_rsv->reserved < global_rsv->size)
9737f9fe614SJosef Bacik 		global_rsv->full = 0;
9747f9fe614SJosef Bacik 	spin_unlock(&global_rsv->lock);
9757f9fe614SJosef Bacik 
9767f9fe614SJosef Bacik 	return true;
9777f9fe614SJosef Bacik }
9787f9fe614SJosef Bacik 
9792341ccd1SJosef Bacik /*
9802341ccd1SJosef Bacik  * maybe_fail_all_tickets - we've exhausted our flushing, start failing tickets
9812341ccd1SJosef Bacik  * @fs_info - fs_info for this fs
9822341ccd1SJosef Bacik  * @space_info - the space info we were flushing
9832341ccd1SJosef Bacik  *
9842341ccd1SJosef Bacik  * We call this when we've exhausted our flushing ability and haven't made
9852341ccd1SJosef Bacik  * progress in satisfying tickets.  The reservation code handles tickets in
9862341ccd1SJosef Bacik  * order, so if there is a large ticket first and then smaller ones we could
9872341ccd1SJosef Bacik  * very well satisfy the smaller tickets.  This will attempt to wake up any
9882341ccd1SJosef Bacik  * tickets in the list to catch this case.
9892341ccd1SJosef Bacik  *
9902341ccd1SJosef Bacik  * This function returns true if it was able to make progress by clearing out
9912341ccd1SJosef Bacik  * other tickets, or if it stumbles across a ticket that was smaller than the
9922341ccd1SJosef Bacik  * first ticket.
9932341ccd1SJosef Bacik  */
maybe_fail_all_tickets(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)9942341ccd1SJosef Bacik static bool maybe_fail_all_tickets(struct btrfs_fs_info *fs_info,
9952341ccd1SJosef Bacik 				   struct btrfs_space_info *space_info)
9960d9764f6SJosef Bacik {
9970d9764f6SJosef Bacik 	struct reserve_ticket *ticket;
9982341ccd1SJosef Bacik 	u64 tickets_id = space_info->tickets_id;
9990e24f6d8SJosef Bacik 	const bool aborted = BTRFS_FS_ERROR(fs_info);
10000d9764f6SJosef Bacik 
1001fcdef39cSJosef Bacik 	trace_btrfs_fail_all_tickets(fs_info, space_info);
1002fcdef39cSJosef Bacik 
100384fe47a4SJosef Bacik 	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
100484fe47a4SJosef Bacik 		btrfs_info(fs_info, "cannot satisfy tickets, dumping space info");
100584fe47a4SJosef Bacik 		__btrfs_dump_space_info(fs_info, space_info);
100684fe47a4SJosef Bacik 	}
100784fe47a4SJosef Bacik 
10082341ccd1SJosef Bacik 	while (!list_empty(&space_info->tickets) &&
10092341ccd1SJosef Bacik 	       tickets_id == space_info->tickets_id) {
10102341ccd1SJosef Bacik 		ticket = list_first_entry(&space_info->tickets,
10112341ccd1SJosef Bacik 					  struct reserve_ticket, list);
10122341ccd1SJosef Bacik 
10131b0309eaSJosef Bacik 		if (!aborted && steal_from_global_rsv(fs_info, space_info, ticket))
10147f9fe614SJosef Bacik 			return true;
10157f9fe614SJosef Bacik 
10160e24f6d8SJosef Bacik 		if (!aborted && btrfs_test_opt(fs_info, ENOSPC_DEBUG))
101784fe47a4SJosef Bacik 			btrfs_info(fs_info, "failing ticket with %llu bytes",
101884fe47a4SJosef Bacik 				   ticket->bytes);
101984fe47a4SJosef Bacik 
1020d611add4SFilipe Manana 		remove_ticket(space_info, ticket);
10210e24f6d8SJosef Bacik 		if (aborted)
10220e24f6d8SJosef Bacik 			ticket->error = -EIO;
10230e24f6d8SJosef Bacik 		else
10240d9764f6SJosef Bacik 			ticket->error = -ENOSPC;
10250d9764f6SJosef Bacik 		wake_up(&ticket->wait);
10262341ccd1SJosef Bacik 
10272341ccd1SJosef Bacik 		/*
10282341ccd1SJosef Bacik 		 * We're just throwing tickets away, so more flushing may not
10292341ccd1SJosef Bacik 		 * trip over btrfs_try_granting_tickets, so we need to call it
10302341ccd1SJosef Bacik 		 * here to see if we can make progress with the next ticket in
10312341ccd1SJosef Bacik 		 * the list.
10322341ccd1SJosef Bacik 		 */
10330e24f6d8SJosef Bacik 		if (!aborted)
10342341ccd1SJosef Bacik 			btrfs_try_granting_tickets(fs_info, space_info);
10350d9764f6SJosef Bacik 	}
10362341ccd1SJosef Bacik 	return (tickets_id != space_info->tickets_id);
10370d9764f6SJosef Bacik }
10380d9764f6SJosef Bacik 
10390d9764f6SJosef Bacik /*
10400d9764f6SJosef Bacik  * This is for normal flushers, we can wait all goddamned day if we want to.  We
10410d9764f6SJosef Bacik  * will loop and continuously try to flush as long as we are making progress.
10420d9764f6SJosef Bacik  * We count progress as clearing off tickets each time we have to loop.
10430d9764f6SJosef Bacik  */
btrfs_async_reclaim_metadata_space(struct work_struct * work)10440d9764f6SJosef Bacik static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
10450d9764f6SJosef Bacik {
10460d9764f6SJosef Bacik 	struct btrfs_fs_info *fs_info;
10470d9764f6SJosef Bacik 	struct btrfs_space_info *space_info;
10480d9764f6SJosef Bacik 	u64 to_reclaim;
104991e79a83SJosef Bacik 	enum btrfs_flush_state flush_state;
10500d9764f6SJosef Bacik 	int commit_cycles = 0;
10510d9764f6SJosef Bacik 	u64 last_tickets_id;
10520d9764f6SJosef Bacik 
10530d9764f6SJosef Bacik 	fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
10540d9764f6SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
10550d9764f6SJosef Bacik 
10560d9764f6SJosef Bacik 	spin_lock(&space_info->lock);
10579f246926SJosef Bacik 	to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info);
10580d9764f6SJosef Bacik 	if (!to_reclaim) {
10590d9764f6SJosef Bacik 		space_info->flush = 0;
10600d9764f6SJosef Bacik 		spin_unlock(&space_info->lock);
10610d9764f6SJosef Bacik 		return;
10620d9764f6SJosef Bacik 	}
10630d9764f6SJosef Bacik 	last_tickets_id = space_info->tickets_id;
10640d9764f6SJosef Bacik 	spin_unlock(&space_info->lock);
10650d9764f6SJosef Bacik 
10660d9764f6SJosef Bacik 	flush_state = FLUSH_DELAYED_ITEMS_NR;
10670d9764f6SJosef Bacik 	do {
10684b02b00fSJosef Bacik 		flush_space(fs_info, space_info, to_reclaim, flush_state, false);
10690d9764f6SJosef Bacik 		spin_lock(&space_info->lock);
10700d9764f6SJosef Bacik 		if (list_empty(&space_info->tickets)) {
10710d9764f6SJosef Bacik 			space_info->flush = 0;
10720d9764f6SJosef Bacik 			spin_unlock(&space_info->lock);
10730d9764f6SJosef Bacik 			return;
10740d9764f6SJosef Bacik 		}
10750d9764f6SJosef Bacik 		to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
10769f246926SJosef Bacik 							      space_info);
10770d9764f6SJosef Bacik 		if (last_tickets_id == space_info->tickets_id) {
10780d9764f6SJosef Bacik 			flush_state++;
10790d9764f6SJosef Bacik 		} else {
10800d9764f6SJosef Bacik 			last_tickets_id = space_info->tickets_id;
10810d9764f6SJosef Bacik 			flush_state = FLUSH_DELAYED_ITEMS_NR;
10820d9764f6SJosef Bacik 			if (commit_cycles)
10830d9764f6SJosef Bacik 				commit_cycles--;
10840d9764f6SJosef Bacik 		}
10850d9764f6SJosef Bacik 
10860d9764f6SJosef Bacik 		/*
108703fe78ccSJosef Bacik 		 * We do not want to empty the system of delalloc unless we're
108803fe78ccSJosef Bacik 		 * under heavy pressure, so allow one trip through the flushing
108903fe78ccSJosef Bacik 		 * logic before we start doing a FLUSH_DELALLOC_FULL.
109003fe78ccSJosef Bacik 		 */
109103fe78ccSJosef Bacik 		if (flush_state == FLUSH_DELALLOC_FULL && !commit_cycles)
109203fe78ccSJosef Bacik 			flush_state++;
109303fe78ccSJosef Bacik 
109403fe78ccSJosef Bacik 		/*
10950d9764f6SJosef Bacik 		 * We don't want to force a chunk allocation until we've tried
10960d9764f6SJosef Bacik 		 * pretty hard to reclaim space.  Think of the case where we
10970d9764f6SJosef Bacik 		 * freed up a bunch of space and so have a lot of pinned space
10980d9764f6SJosef Bacik 		 * to reclaim.  We would rather use that than possibly create a
10990d9764f6SJosef Bacik 		 * underutilized metadata chunk.  So if this is our first run
11000d9764f6SJosef Bacik 		 * through the flushing state machine skip ALLOC_CHUNK_FORCE and
11010d9764f6SJosef Bacik 		 * commit the transaction.  If nothing has changed the next go
11020d9764f6SJosef Bacik 		 * around then we can force a chunk allocation.
11030d9764f6SJosef Bacik 		 */
11040d9764f6SJosef Bacik 		if (flush_state == ALLOC_CHUNK_FORCE && !commit_cycles)
11050d9764f6SJosef Bacik 			flush_state++;
11060d9764f6SJosef Bacik 
11070d9764f6SJosef Bacik 		if (flush_state > COMMIT_TRANS) {
11080d9764f6SJosef Bacik 			commit_cycles++;
11090d9764f6SJosef Bacik 			if (commit_cycles > 2) {
11102341ccd1SJosef Bacik 				if (maybe_fail_all_tickets(fs_info, space_info)) {
11110d9764f6SJosef Bacik 					flush_state = FLUSH_DELAYED_ITEMS_NR;
11120d9764f6SJosef Bacik 					commit_cycles--;
11130d9764f6SJosef Bacik 				} else {
11140d9764f6SJosef Bacik 					space_info->flush = 0;
11150d9764f6SJosef Bacik 				}
11160d9764f6SJosef Bacik 			} else {
11170d9764f6SJosef Bacik 				flush_state = FLUSH_DELAYED_ITEMS_NR;
11180d9764f6SJosef Bacik 			}
11190d9764f6SJosef Bacik 		}
11200d9764f6SJosef Bacik 		spin_unlock(&space_info->lock);
11210d9764f6SJosef Bacik 	} while (flush_state <= COMMIT_TRANS);
11220d9764f6SJosef Bacik }
11230d9764f6SJosef Bacik 
11241a7a92c8SJosef Bacik /*
1125576fa348SJosef Bacik  * This handles pre-flushing of metadata space before we get to the point that
1126576fa348SJosef Bacik  * we need to start blocking threads on tickets.  The logic here is different
1127576fa348SJosef Bacik  * from the other flush paths because it doesn't rely on tickets to tell us how
1128576fa348SJosef Bacik  * much we need to flush, instead it attempts to keep us below the 80% full
1129576fa348SJosef Bacik  * watermark of space by flushing whichever reservation pool is currently the
1130576fa348SJosef Bacik  * largest.
1131576fa348SJosef Bacik  */
btrfs_preempt_reclaim_metadata_space(struct work_struct * work)1132576fa348SJosef Bacik static void btrfs_preempt_reclaim_metadata_space(struct work_struct *work)
1133576fa348SJosef Bacik {
1134576fa348SJosef Bacik 	struct btrfs_fs_info *fs_info;
1135576fa348SJosef Bacik 	struct btrfs_space_info *space_info;
1136576fa348SJosef Bacik 	struct btrfs_block_rsv *delayed_block_rsv;
1137576fa348SJosef Bacik 	struct btrfs_block_rsv *delayed_refs_rsv;
1138576fa348SJosef Bacik 	struct btrfs_block_rsv *global_rsv;
1139576fa348SJosef Bacik 	struct btrfs_block_rsv *trans_rsv;
114088a777a6SJosef Bacik 	int loops = 0;
1141576fa348SJosef Bacik 
1142576fa348SJosef Bacik 	fs_info = container_of(work, struct btrfs_fs_info,
1143576fa348SJosef Bacik 			       preempt_reclaim_work);
1144576fa348SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
1145576fa348SJosef Bacik 	delayed_block_rsv = &fs_info->delayed_block_rsv;
1146576fa348SJosef Bacik 	delayed_refs_rsv = &fs_info->delayed_refs_rsv;
1147576fa348SJosef Bacik 	global_rsv = &fs_info->global_block_rsv;
1148576fa348SJosef Bacik 	trans_rsv = &fs_info->trans_block_rsv;
1149576fa348SJosef Bacik 
1150576fa348SJosef Bacik 	spin_lock(&space_info->lock);
11512e294c60SJosef Bacik 	while (need_preemptive_reclaim(fs_info, space_info)) {
1152576fa348SJosef Bacik 		enum btrfs_flush_state flush;
1153576fa348SJosef Bacik 		u64 delalloc_size = 0;
1154576fa348SJosef Bacik 		u64 to_reclaim, block_rsv_size;
115582220b18SFilipe Manana 		const u64 global_rsv_size = btrfs_block_rsv_reserved(global_rsv);
1156576fa348SJosef Bacik 
115788a777a6SJosef Bacik 		loops++;
115888a777a6SJosef Bacik 
1159576fa348SJosef Bacik 		/*
1160576fa348SJosef Bacik 		 * We don't have a precise counter for the metadata being
1161576fa348SJosef Bacik 		 * reserved for delalloc, so we'll approximate it by subtracting
1162576fa348SJosef Bacik 		 * out the block rsv's space from the bytes_may_use.  If that
1163576fa348SJosef Bacik 		 * amount is higher than the individual reserves, then we can
1164576fa348SJosef Bacik 		 * assume it's tied up in delalloc reservations.
1165576fa348SJosef Bacik 		 */
1166576fa348SJosef Bacik 		block_rsv_size = global_rsv_size +
116782220b18SFilipe Manana 			btrfs_block_rsv_reserved(delayed_block_rsv) +
116882220b18SFilipe Manana 			btrfs_block_rsv_reserved(delayed_refs_rsv) +
116982220b18SFilipe Manana 			btrfs_block_rsv_reserved(trans_rsv);
1170576fa348SJosef Bacik 		if (block_rsv_size < space_info->bytes_may_use)
1171576fa348SJosef Bacik 			delalloc_size = space_info->bytes_may_use - block_rsv_size;
1172576fa348SJosef Bacik 
1173576fa348SJosef Bacik 		/*
1174576fa348SJosef Bacik 		 * We don't want to include the global_rsv in our calculation,
1175576fa348SJosef Bacik 		 * because that's space we can't touch.  Subtract it from the
1176576fa348SJosef Bacik 		 * block_rsv_size for the next checks.
1177576fa348SJosef Bacik 		 */
1178576fa348SJosef Bacik 		block_rsv_size -= global_rsv_size;
1179576fa348SJosef Bacik 
1180576fa348SJosef Bacik 		/*
1181576fa348SJosef Bacik 		 * We really want to avoid flushing delalloc too much, as it
1182576fa348SJosef Bacik 		 * could result in poor allocation patterns, so only flush it if
1183576fa348SJosef Bacik 		 * it's larger than the rest of the pools combined.
1184576fa348SJosef Bacik 		 */
1185576fa348SJosef Bacik 		if (delalloc_size > block_rsv_size) {
1186576fa348SJosef Bacik 			to_reclaim = delalloc_size;
1187576fa348SJosef Bacik 			flush = FLUSH_DELALLOC;
1188576fa348SJosef Bacik 		} else if (space_info->bytes_pinned >
118982220b18SFilipe Manana 			   (btrfs_block_rsv_reserved(delayed_block_rsv) +
119082220b18SFilipe Manana 			    btrfs_block_rsv_reserved(delayed_refs_rsv))) {
1191576fa348SJosef Bacik 			to_reclaim = space_info->bytes_pinned;
1192c416a30cSJosef Bacik 			flush = COMMIT_TRANS;
119382220b18SFilipe Manana 		} else if (btrfs_block_rsv_reserved(delayed_block_rsv) >
119482220b18SFilipe Manana 			   btrfs_block_rsv_reserved(delayed_refs_rsv)) {
119582220b18SFilipe Manana 			to_reclaim = btrfs_block_rsv_reserved(delayed_block_rsv);
1196576fa348SJosef Bacik 			flush = FLUSH_DELAYED_ITEMS_NR;
1197576fa348SJosef Bacik 		} else {
119882220b18SFilipe Manana 			to_reclaim = btrfs_block_rsv_reserved(delayed_refs_rsv);
1199576fa348SJosef Bacik 			flush = FLUSH_DELAYED_REFS_NR;
1200576fa348SJosef Bacik 		}
1201576fa348SJosef Bacik 
120206bae876SNiels Dossche 		spin_unlock(&space_info->lock);
120306bae876SNiels Dossche 
1204576fa348SJosef Bacik 		/*
1205576fa348SJosef Bacik 		 * We don't want to reclaim everything, just a portion, so scale
1206576fa348SJosef Bacik 		 * down the to_reclaim by 1/4.  If it takes us down to 0,
1207576fa348SJosef Bacik 		 * reclaim 1 items worth.
1208576fa348SJosef Bacik 		 */
1209576fa348SJosef Bacik 		to_reclaim >>= 2;
1210576fa348SJosef Bacik 		if (!to_reclaim)
1211576fa348SJosef Bacik 			to_reclaim = btrfs_calc_insert_metadata_size(fs_info, 1);
12124b02b00fSJosef Bacik 		flush_space(fs_info, space_info, to_reclaim, flush, true);
1213576fa348SJosef Bacik 		cond_resched();
1214576fa348SJosef Bacik 		spin_lock(&space_info->lock);
1215576fa348SJosef Bacik 	}
121688a777a6SJosef Bacik 
121788a777a6SJosef Bacik 	/* We only went through once, back off our clamping. */
121888a777a6SJosef Bacik 	if (loops == 1 && !space_info->reclaim_size)
121988a777a6SJosef Bacik 		space_info->clamp = max(1, space_info->clamp - 1);
1220e5ad49e2SJosef Bacik 	trace_btrfs_done_preemptive_reclaim(fs_info, space_info);
1221576fa348SJosef Bacik 	spin_unlock(&space_info->lock);
1222576fa348SJosef Bacik }
1223576fa348SJosef Bacik 
1224576fa348SJosef Bacik /*
12251a7a92c8SJosef Bacik  * FLUSH_DELALLOC_WAIT:
12261a7a92c8SJosef Bacik  *   Space is freed from flushing delalloc in one of two ways.
12271a7a92c8SJosef Bacik  *
12281a7a92c8SJosef Bacik  *   1) compression is on and we allocate less space than we reserved
12291a7a92c8SJosef Bacik  *   2) we are overwriting existing space
12301a7a92c8SJosef Bacik  *
12311a7a92c8SJosef Bacik  *   For #1 that extra space is reclaimed as soon as the delalloc pages are
12321a7a92c8SJosef Bacik  *   COWed, by way of btrfs_add_reserved_bytes() which adds the actual extent
12331a7a92c8SJosef Bacik  *   length to ->bytes_reserved, and subtracts the reserved space from
12341a7a92c8SJosef Bacik  *   ->bytes_may_use.
12351a7a92c8SJosef Bacik  *
12361a7a92c8SJosef Bacik  *   For #2 this is trickier.  Once the ordered extent runs we will drop the
12371a7a92c8SJosef Bacik  *   extent in the range we are overwriting, which creates a delayed ref for
12381a7a92c8SJosef Bacik  *   that freed extent.  This however is not reclaimed until the transaction
12391a7a92c8SJosef Bacik  *   commits, thus the next stages.
12401a7a92c8SJosef Bacik  *
12411a7a92c8SJosef Bacik  * RUN_DELAYED_IPUTS
12421a7a92c8SJosef Bacik  *   If we are freeing inodes, we want to make sure all delayed iputs have
12431a7a92c8SJosef Bacik  *   completed, because they could have been on an inode with i_nlink == 0, and
12441a7a92c8SJosef Bacik  *   thus have been truncated and freed up space.  But again this space is not
12451a7a92c8SJosef Bacik  *   immediately re-usable, it comes in the form of a delayed ref, which must be
12461a7a92c8SJosef Bacik  *   run and then the transaction must be committed.
12471a7a92c8SJosef Bacik  *
12481a7a92c8SJosef Bacik  * COMMIT_TRANS
1249c416a30cSJosef Bacik  *   This is where we reclaim all of the pinned space generated by running the
1250c416a30cSJosef Bacik  *   iputs
1251c4923027SJosef Bacik  *
1252c4923027SJosef Bacik  * ALLOC_CHUNK_FORCE
1253c4923027SJosef Bacik  *   For data we start with alloc chunk force, however we could have been full
1254c4923027SJosef Bacik  *   before, and then the transaction commit could have freed new block groups,
1255c4923027SJosef Bacik  *   so if we now have space to allocate do the force chunk allocation.
12561a7a92c8SJosef Bacik  */
125757056740SJosef Bacik static const enum btrfs_flush_state data_flush_states[] = {
125803fe78ccSJosef Bacik 	FLUSH_DELALLOC_FULL,
125957056740SJosef Bacik 	RUN_DELAYED_IPUTS,
126057056740SJosef Bacik 	COMMIT_TRANS,
1261c4923027SJosef Bacik 	ALLOC_CHUNK_FORCE,
126257056740SJosef Bacik };
126357056740SJosef Bacik 
btrfs_async_reclaim_data_space(struct work_struct * work)126457056740SJosef Bacik static void btrfs_async_reclaim_data_space(struct work_struct *work)
12650d9764f6SJosef Bacik {
126657056740SJosef Bacik 	struct btrfs_fs_info *fs_info;
126757056740SJosef Bacik 	struct btrfs_space_info *space_info;
126857056740SJosef Bacik 	u64 last_tickets_id;
126991e79a83SJosef Bacik 	enum btrfs_flush_state flush_state = 0;
127057056740SJosef Bacik 
127157056740SJosef Bacik 	fs_info = container_of(work, struct btrfs_fs_info, async_data_reclaim_work);
127257056740SJosef Bacik 	space_info = fs_info->data_sinfo;
127357056740SJosef Bacik 
127457056740SJosef Bacik 	spin_lock(&space_info->lock);
127557056740SJosef Bacik 	if (list_empty(&space_info->tickets)) {
127657056740SJosef Bacik 		space_info->flush = 0;
127757056740SJosef Bacik 		spin_unlock(&space_info->lock);
127857056740SJosef Bacik 		return;
127957056740SJosef Bacik 	}
128057056740SJosef Bacik 	last_tickets_id = space_info->tickets_id;
128157056740SJosef Bacik 	spin_unlock(&space_info->lock);
128257056740SJosef Bacik 
128357056740SJosef Bacik 	while (!space_info->full) {
12844b02b00fSJosef Bacik 		flush_space(fs_info, space_info, U64_MAX, ALLOC_CHUNK_FORCE, false);
128557056740SJosef Bacik 		spin_lock(&space_info->lock);
128657056740SJosef Bacik 		if (list_empty(&space_info->tickets)) {
128757056740SJosef Bacik 			space_info->flush = 0;
128857056740SJosef Bacik 			spin_unlock(&space_info->lock);
128957056740SJosef Bacik 			return;
129057056740SJosef Bacik 		}
12910e24f6d8SJosef Bacik 
12920e24f6d8SJosef Bacik 		/* Something happened, fail everything and bail. */
12930e24f6d8SJosef Bacik 		if (BTRFS_FS_ERROR(fs_info))
12940e24f6d8SJosef Bacik 			goto aborted_fs;
129557056740SJosef Bacik 		last_tickets_id = space_info->tickets_id;
129657056740SJosef Bacik 		spin_unlock(&space_info->lock);
129757056740SJosef Bacik 	}
129857056740SJosef Bacik 
129957056740SJosef Bacik 	while (flush_state < ARRAY_SIZE(data_flush_states)) {
130057056740SJosef Bacik 		flush_space(fs_info, space_info, U64_MAX,
13014b02b00fSJosef Bacik 			    data_flush_states[flush_state], false);
130257056740SJosef Bacik 		spin_lock(&space_info->lock);
130357056740SJosef Bacik 		if (list_empty(&space_info->tickets)) {
130457056740SJosef Bacik 			space_info->flush = 0;
130557056740SJosef Bacik 			spin_unlock(&space_info->lock);
130657056740SJosef Bacik 			return;
130757056740SJosef Bacik 		}
130857056740SJosef Bacik 
130957056740SJosef Bacik 		if (last_tickets_id == space_info->tickets_id) {
131057056740SJosef Bacik 			flush_state++;
131157056740SJosef Bacik 		} else {
131257056740SJosef Bacik 			last_tickets_id = space_info->tickets_id;
131357056740SJosef Bacik 			flush_state = 0;
131457056740SJosef Bacik 		}
131557056740SJosef Bacik 
131657056740SJosef Bacik 		if (flush_state >= ARRAY_SIZE(data_flush_states)) {
131757056740SJosef Bacik 			if (space_info->full) {
131857056740SJosef Bacik 				if (maybe_fail_all_tickets(fs_info, space_info))
131957056740SJosef Bacik 					flush_state = 0;
132057056740SJosef Bacik 				else
132157056740SJosef Bacik 					space_info->flush = 0;
132257056740SJosef Bacik 			} else {
132357056740SJosef Bacik 				flush_state = 0;
132457056740SJosef Bacik 			}
13250e24f6d8SJosef Bacik 
13260e24f6d8SJosef Bacik 			/* Something happened, fail everything and bail. */
13270e24f6d8SJosef Bacik 			if (BTRFS_FS_ERROR(fs_info))
13280e24f6d8SJosef Bacik 				goto aborted_fs;
13290e24f6d8SJosef Bacik 
133057056740SJosef Bacik 		}
133157056740SJosef Bacik 		spin_unlock(&space_info->lock);
133257056740SJosef Bacik 	}
13330e24f6d8SJosef Bacik 	return;
13340e24f6d8SJosef Bacik 
13350e24f6d8SJosef Bacik aborted_fs:
13360e24f6d8SJosef Bacik 	maybe_fail_all_tickets(fs_info, space_info);
13370e24f6d8SJosef Bacik 	space_info->flush = 0;
13380e24f6d8SJosef Bacik 	spin_unlock(&space_info->lock);
133957056740SJosef Bacik }
134057056740SJosef Bacik 
btrfs_init_async_reclaim_work(struct btrfs_fs_info * fs_info)134157056740SJosef Bacik void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info)
134257056740SJosef Bacik {
134357056740SJosef Bacik 	INIT_WORK(&fs_info->async_reclaim_work, btrfs_async_reclaim_metadata_space);
134457056740SJosef Bacik 	INIT_WORK(&fs_info->async_data_reclaim_work, btrfs_async_reclaim_data_space);
1345576fa348SJosef Bacik 	INIT_WORK(&fs_info->preempt_reclaim_work,
1346576fa348SJosef Bacik 		  btrfs_preempt_reclaim_metadata_space);
13470d9764f6SJosef Bacik }
13480d9764f6SJosef Bacik 
13490d9764f6SJosef Bacik static const enum btrfs_flush_state priority_flush_states[] = {
13500d9764f6SJosef Bacik 	FLUSH_DELAYED_ITEMS_NR,
13510d9764f6SJosef Bacik 	FLUSH_DELAYED_ITEMS,
13520d9764f6SJosef Bacik 	ALLOC_CHUNK,
13530d9764f6SJosef Bacik };
13540d9764f6SJosef Bacik 
1355d3984c90SJosef Bacik static const enum btrfs_flush_state evict_flush_states[] = {
1356d3984c90SJosef Bacik 	FLUSH_DELAYED_ITEMS_NR,
1357d3984c90SJosef Bacik 	FLUSH_DELAYED_ITEMS,
1358d3984c90SJosef Bacik 	FLUSH_DELAYED_REFS_NR,
1359d3984c90SJosef Bacik 	FLUSH_DELAYED_REFS,
1360d3984c90SJosef Bacik 	FLUSH_DELALLOC,
1361d3984c90SJosef Bacik 	FLUSH_DELALLOC_WAIT,
136203fe78ccSJosef Bacik 	FLUSH_DELALLOC_FULL,
1363d3984c90SJosef Bacik 	ALLOC_CHUNK,
1364d3984c90SJosef Bacik 	COMMIT_TRANS,
1365d3984c90SJosef Bacik };
1366d3984c90SJosef Bacik 
priority_reclaim_metadata_space(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,struct reserve_ticket * ticket,const enum btrfs_flush_state * states,int states_nr)13670d9764f6SJosef Bacik static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
13680d9764f6SJosef Bacik 				struct btrfs_space_info *space_info,
13699ce2f423SJosef Bacik 				struct reserve_ticket *ticket,
13709ce2f423SJosef Bacik 				const enum btrfs_flush_state *states,
13719ce2f423SJosef Bacik 				int states_nr)
13720d9764f6SJosef Bacik {
13730d9764f6SJosef Bacik 	u64 to_reclaim;
13749f35f76dSJosef Bacik 	int flush_state = 0;
13750d9764f6SJosef Bacik 
13760d9764f6SJosef Bacik 	spin_lock(&space_info->lock);
13779f246926SJosef Bacik 	to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info);
13789cd8dcdcSJosef Bacik 	/*
13799cd8dcdcSJosef Bacik 	 * This is the priority reclaim path, so to_reclaim could be >0 still
1380143823cfSDavid Sterba 	 * because we may have only satisfied the priority tickets and still
13819cd8dcdcSJosef Bacik 	 * left non priority tickets on the list.  We would then have
13829cd8dcdcSJosef Bacik 	 * to_reclaim but ->bytes == 0.
13839cd8dcdcSJosef Bacik 	 */
13849cd8dcdcSJosef Bacik 	if (ticket->bytes == 0) {
13850d9764f6SJosef Bacik 		spin_unlock(&space_info->lock);
13860d9764f6SJosef Bacik 		return;
13870d9764f6SJosef Bacik 	}
13880d9764f6SJosef Bacik 
13899f35f76dSJosef Bacik 	while (flush_state < states_nr) {
13909f35f76dSJosef Bacik 		spin_unlock(&space_info->lock);
13914b02b00fSJosef Bacik 		flush_space(fs_info, space_info, to_reclaim, states[flush_state],
13924b02b00fSJosef Bacik 			    false);
13930d9764f6SJosef Bacik 		flush_state++;
13940d9764f6SJosef Bacik 		spin_lock(&space_info->lock);
13950d9764f6SJosef Bacik 		if (ticket->bytes == 0) {
13960d9764f6SJosef Bacik 			spin_unlock(&space_info->lock);
13970d9764f6SJosef Bacik 			return;
13980d9764f6SJosef Bacik 		}
13999f35f76dSJosef Bacik 	}
14009f35f76dSJosef Bacik 
14011b6948acSFilipe Manana 	/*
14021b6948acSFilipe Manana 	 * Attempt to steal from the global rsv if we can, except if the fs was
14031b6948acSFilipe Manana 	 * turned into error mode due to a transaction abort when flushing space
14047e3bfd14SFilipe Manana 	 * above, in that case fail with the abort error instead of returning
14057e3bfd14SFilipe Manana 	 * success to the caller if we can steal from the global rsv - this is
14067e3bfd14SFilipe Manana 	 * just to have caller fail immeditelly instead of later when trying to
14077e3bfd14SFilipe Manana 	 * modify the fs, making it easier to debug -ENOSPC problems.
14081b6948acSFilipe Manana 	 */
14091b6948acSFilipe Manana 	if (BTRFS_FS_ERROR(fs_info)) {
14107e3bfd14SFilipe Manana 		ticket->error = BTRFS_FS_ERROR(fs_info);
14111b6948acSFilipe Manana 		remove_ticket(space_info, ticket);
14121b6948acSFilipe Manana 	} else if (!steal_from_global_rsv(fs_info, space_info, ticket)) {
1413ee6adbfdSJosef Bacik 		ticket->error = -ENOSPC;
1414ee6adbfdSJosef Bacik 		remove_ticket(space_info, ticket);
1415ee6adbfdSJosef Bacik 	}
1416ee6adbfdSJosef Bacik 
14179f35f76dSJosef Bacik 	/*
14189f35f76dSJosef Bacik 	 * We must run try_granting_tickets here because we could be a large
14199f35f76dSJosef Bacik 	 * ticket in front of a smaller ticket that can now be satisfied with
14209f35f76dSJosef Bacik 	 * the available space.
14219f35f76dSJosef Bacik 	 */
14229f35f76dSJosef Bacik 	btrfs_try_granting_tickets(fs_info, space_info);
14230d9764f6SJosef Bacik 	spin_unlock(&space_info->lock);
14240d9764f6SJosef Bacik }
14250d9764f6SJosef Bacik 
priority_reclaim_data_space(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,struct reserve_ticket * ticket)14261004f686SJosef Bacik static void priority_reclaim_data_space(struct btrfs_fs_info *fs_info,
14271004f686SJosef Bacik 					struct btrfs_space_info *space_info,
142857056740SJosef Bacik 					struct reserve_ticket *ticket)
14291004f686SJosef Bacik {
14309f35f76dSJosef Bacik 	spin_lock(&space_info->lock);
14319cd8dcdcSJosef Bacik 
14329cd8dcdcSJosef Bacik 	/* We could have been granted before we got here. */
14339cd8dcdcSJosef Bacik 	if (ticket->bytes == 0) {
14349cd8dcdcSJosef Bacik 		spin_unlock(&space_info->lock);
14359cd8dcdcSJosef Bacik 		return;
14369cd8dcdcSJosef Bacik 	}
14379cd8dcdcSJosef Bacik 
14381004f686SJosef Bacik 	while (!space_info->full) {
14399f35f76dSJosef Bacik 		spin_unlock(&space_info->lock);
14404b02b00fSJosef Bacik 		flush_space(fs_info, space_info, U64_MAX, ALLOC_CHUNK_FORCE, false);
14411004f686SJosef Bacik 		spin_lock(&space_info->lock);
14421004f686SJosef Bacik 		if (ticket->bytes == 0) {
14431004f686SJosef Bacik 			spin_unlock(&space_info->lock);
14441004f686SJosef Bacik 			return;
14451004f686SJosef Bacik 		}
14461004f686SJosef Bacik 	}
14479f35f76dSJosef Bacik 
14489f35f76dSJosef Bacik 	ticket->error = -ENOSPC;
14499f35f76dSJosef Bacik 	remove_ticket(space_info, ticket);
14509f35f76dSJosef Bacik 	btrfs_try_granting_tickets(fs_info, space_info);
14519f35f76dSJosef Bacik 	spin_unlock(&space_info->lock);
14521004f686SJosef Bacik }
14531004f686SJosef Bacik 
wait_reserve_ticket(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,struct reserve_ticket * ticket)1454374bf9c5SJosef Bacik static void wait_reserve_ticket(struct btrfs_fs_info *fs_info,
14550d9764f6SJosef Bacik 				struct btrfs_space_info *space_info,
14560d9764f6SJosef Bacik 				struct reserve_ticket *ticket)
14570d9764f6SJosef Bacik 
14580d9764f6SJosef Bacik {
14590d9764f6SJosef Bacik 	DEFINE_WAIT(wait);
14600d9764f6SJosef Bacik 	int ret = 0;
14610d9764f6SJosef Bacik 
14620d9764f6SJosef Bacik 	spin_lock(&space_info->lock);
14630d9764f6SJosef Bacik 	while (ticket->bytes > 0 && ticket->error == 0) {
14640d9764f6SJosef Bacik 		ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
14650d9764f6SJosef Bacik 		if (ret) {
14660cab7accSFilipe Manana 			/*
14670cab7accSFilipe Manana 			 * Delete us from the list. After we unlock the space
14680cab7accSFilipe Manana 			 * info, we don't want the async reclaim job to reserve
14690cab7accSFilipe Manana 			 * space for this ticket. If that would happen, then the
14700cab7accSFilipe Manana 			 * ticket's task would not known that space was reserved
14710cab7accSFilipe Manana 			 * despite getting an error, resulting in a space leak
14720cab7accSFilipe Manana 			 * (bytes_may_use counter of our space_info).
14730cab7accSFilipe Manana 			 */
1474d611add4SFilipe Manana 			remove_ticket(space_info, ticket);
1475374bf9c5SJosef Bacik 			ticket->error = -EINTR;
14760d9764f6SJosef Bacik 			break;
14770d9764f6SJosef Bacik 		}
14780d9764f6SJosef Bacik 		spin_unlock(&space_info->lock);
14790d9764f6SJosef Bacik 
14800d9764f6SJosef Bacik 		schedule();
14810d9764f6SJosef Bacik 
14820d9764f6SJosef Bacik 		finish_wait(&ticket->wait, &wait);
14830d9764f6SJosef Bacik 		spin_lock(&space_info->lock);
14840d9764f6SJosef Bacik 	}
14850d9764f6SJosef Bacik 	spin_unlock(&space_info->lock);
14860d9764f6SJosef Bacik }
14870d9764f6SJosef Bacik 
148843dd529aSDavid Sterba /*
148943dd529aSDavid Sterba  * Do the appropriate flushing and waiting for a ticket.
1490d98b188eSNikolay Borisov  *
1491d98b188eSNikolay Borisov  * @fs_info:    the filesystem
1492d98b188eSNikolay Borisov  * @space_info: space info for the reservation
1493d98b188eSNikolay Borisov  * @ticket:     ticket for the reservation
1494ac1ea10eSJosef Bacik  * @start_ns:   timestamp when the reservation started
1495ac1ea10eSJosef Bacik  * @orig_bytes: amount of bytes originally reserved
1496d98b188eSNikolay Borisov  * @flush:      how much we can flush
149703235279SJosef Bacik  *
149803235279SJosef Bacik  * This does the work of figuring out how to flush for the ticket, waiting for
149903235279SJosef Bacik  * the reservation, and returning the appropriate error if there is one.
150003235279SJosef Bacik  */
handle_reserve_ticket(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,struct reserve_ticket * ticket,u64 start_ns,u64 orig_bytes,enum btrfs_reserve_flush_enum flush)150103235279SJosef Bacik static int handle_reserve_ticket(struct btrfs_fs_info *fs_info,
150203235279SJosef Bacik 				 struct btrfs_space_info *space_info,
150303235279SJosef Bacik 				 struct reserve_ticket *ticket,
1504ac1ea10eSJosef Bacik 				 u64 start_ns, u64 orig_bytes,
150503235279SJosef Bacik 				 enum btrfs_reserve_flush_enum flush)
150603235279SJosef Bacik {
150703235279SJosef Bacik 	int ret;
150803235279SJosef Bacik 
1509d3984c90SJosef Bacik 	switch (flush) {
151057056740SJosef Bacik 	case BTRFS_RESERVE_FLUSH_DATA:
1511d3984c90SJosef Bacik 	case BTRFS_RESERVE_FLUSH_ALL:
15127f9fe614SJosef Bacik 	case BTRFS_RESERVE_FLUSH_ALL_STEAL:
151303235279SJosef Bacik 		wait_reserve_ticket(fs_info, space_info, ticket);
1514d3984c90SJosef Bacik 		break;
1515d3984c90SJosef Bacik 	case BTRFS_RESERVE_FLUSH_LIMIT:
15169ce2f423SJosef Bacik 		priority_reclaim_metadata_space(fs_info, space_info, ticket,
15179ce2f423SJosef Bacik 						priority_flush_states,
15189ce2f423SJosef Bacik 						ARRAY_SIZE(priority_flush_states));
1519d3984c90SJosef Bacik 		break;
1520d3984c90SJosef Bacik 	case BTRFS_RESERVE_FLUSH_EVICT:
1521d3984c90SJosef Bacik 		priority_reclaim_metadata_space(fs_info, space_info, ticket,
1522d3984c90SJosef Bacik 						evict_flush_states,
1523d3984c90SJosef Bacik 						ARRAY_SIZE(evict_flush_states));
1524d3984c90SJosef Bacik 		break;
15251004f686SJosef Bacik 	case BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE:
152657056740SJosef Bacik 		priority_reclaim_data_space(fs_info, space_info, ticket);
15271004f686SJosef Bacik 		break;
1528d3984c90SJosef Bacik 	default:
1529d3984c90SJosef Bacik 		ASSERT(0);
1530d3984c90SJosef Bacik 		break;
1531d3984c90SJosef Bacik 	}
153203235279SJosef Bacik 
153303235279SJosef Bacik 	ret = ticket->error;
153403235279SJosef Bacik 	ASSERT(list_empty(&ticket->list));
15350cab7accSFilipe Manana 	/*
15360cab7accSFilipe Manana 	 * Check that we can't have an error set if the reservation succeeded,
15370cab7accSFilipe Manana 	 * as that would confuse tasks and lead them to error out without
15380cab7accSFilipe Manana 	 * releasing reserved space (if an error happens the expectation is that
15390cab7accSFilipe Manana 	 * space wasn't reserved at all).
15400cab7accSFilipe Manana 	 */
15410cab7accSFilipe Manana 	ASSERT(!(ticket->bytes == 0 && ticket->error));
1542ac1ea10eSJosef Bacik 	trace_btrfs_reserve_ticket(fs_info, space_info->flags, orig_bytes,
1543ac1ea10eSJosef Bacik 				   start_ns, flush, ticket->error);
154403235279SJosef Bacik 	return ret;
154503235279SJosef Bacik }
154603235279SJosef Bacik 
1547666daa9fSJosef Bacik /*
1548666daa9fSJosef Bacik  * This returns true if this flush state will go through the ordinary flushing
1549666daa9fSJosef Bacik  * code.
1550666daa9fSJosef Bacik  */
is_normal_flushing(enum btrfs_reserve_flush_enum flush)1551666daa9fSJosef Bacik static inline bool is_normal_flushing(enum btrfs_reserve_flush_enum flush)
1552666daa9fSJosef Bacik {
1553666daa9fSJosef Bacik 	return	(flush == BTRFS_RESERVE_FLUSH_ALL) ||
1554666daa9fSJosef Bacik 		(flush == BTRFS_RESERVE_FLUSH_ALL_STEAL);
1555666daa9fSJosef Bacik }
1556666daa9fSJosef Bacik 
maybe_clamp_preempt(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)155788a777a6SJosef Bacik static inline void maybe_clamp_preempt(struct btrfs_fs_info *fs_info,
155888a777a6SJosef Bacik 				       struct btrfs_space_info *space_info)
155988a777a6SJosef Bacik {
156088a777a6SJosef Bacik 	u64 ordered = percpu_counter_sum_positive(&fs_info->ordered_bytes);
156188a777a6SJosef Bacik 	u64 delalloc = percpu_counter_sum_positive(&fs_info->delalloc_bytes);
156288a777a6SJosef Bacik 
156388a777a6SJosef Bacik 	/*
156488a777a6SJosef Bacik 	 * If we're heavy on ordered operations then clamping won't help us.  We
156588a777a6SJosef Bacik 	 * need to clamp specifically to keep up with dirty'ing buffered
156688a777a6SJosef Bacik 	 * writers, because there's not a 1:1 correlation of writing delalloc
156788a777a6SJosef Bacik 	 * and freeing space, like there is with flushing delayed refs or
156888a777a6SJosef Bacik 	 * delayed nodes.  If we're already more ordered than delalloc then
156988a777a6SJosef Bacik 	 * we're keeping up, otherwise we aren't and should probably clamp.
157088a777a6SJosef Bacik 	 */
157188a777a6SJosef Bacik 	if (ordered < delalloc)
157288a777a6SJosef Bacik 		space_info->clamp = min(space_info->clamp + 1, 8);
157388a777a6SJosef Bacik }
157488a777a6SJosef Bacik 
can_steal(enum btrfs_reserve_flush_enum flush)1575ee6adbfdSJosef Bacik static inline bool can_steal(enum btrfs_reserve_flush_enum flush)
1576ee6adbfdSJosef Bacik {
1577ee6adbfdSJosef Bacik 	return (flush == BTRFS_RESERVE_FLUSH_ALL_STEAL ||
1578ee6adbfdSJosef Bacik 		flush == BTRFS_RESERVE_FLUSH_EVICT);
1579ee6adbfdSJosef Bacik }
1580ee6adbfdSJosef Bacik 
1581765c3fe9SJosef Bacik /*
1582765c3fe9SJosef Bacik  * NO_FLUSH and FLUSH_EMERGENCY don't want to create a ticket, they just want to
1583765c3fe9SJosef Bacik  * fail as quickly as possible.
1584765c3fe9SJosef Bacik  */
can_ticket(enum btrfs_reserve_flush_enum flush)1585765c3fe9SJosef Bacik static inline bool can_ticket(enum btrfs_reserve_flush_enum flush)
1586765c3fe9SJosef Bacik {
1587765c3fe9SJosef Bacik 	return (flush != BTRFS_RESERVE_NO_FLUSH &&
1588765c3fe9SJosef Bacik 		flush != BTRFS_RESERVE_FLUSH_EMERGENCY);
1589765c3fe9SJosef Bacik }
1590765c3fe9SJosef Bacik 
159143dd529aSDavid Sterba /*
159243dd529aSDavid Sterba  * Try to reserve bytes from the block_rsv's space.
1593d98b188eSNikolay Borisov  *
1594d98b188eSNikolay Borisov  * @fs_info:    the filesystem
1595d98b188eSNikolay Borisov  * @space_info: space info we want to allocate from
1596d98b188eSNikolay Borisov  * @orig_bytes: number of bytes we want
1597d98b188eSNikolay Borisov  * @flush:      whether or not we can flush to make our reservation
15980d9764f6SJosef Bacik  *
15990d9764f6SJosef Bacik  * This will reserve orig_bytes number of bytes from the space info associated
16000d9764f6SJosef Bacik  * with the block_rsv.  If there is not enough space it will make an attempt to
16010d9764f6SJosef Bacik  * flush out space to make room.  It will do this by flushing delalloc if
16020d9764f6SJosef Bacik  * possible or committing the transaction.  If flush is 0 then no attempts to
16030d9764f6SJosef Bacik  * regain reservations will be made and this will fail if there is not enough
16040d9764f6SJosef Bacik  * space already.
16050d9764f6SJosef Bacik  */
__reserve_bytes(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,u64 orig_bytes,enum btrfs_reserve_flush_enum flush)1606f3bda421SJosef Bacik static int __reserve_bytes(struct btrfs_fs_info *fs_info,
1607f3bda421SJosef Bacik 			   struct btrfs_space_info *space_info, u64 orig_bytes,
16089f246926SJosef Bacik 			   enum btrfs_reserve_flush_enum flush)
16090d9764f6SJosef Bacik {
161057056740SJosef Bacik 	struct work_struct *async_work;
16110d9764f6SJosef Bacik 	struct reserve_ticket ticket;
1612ac1ea10eSJosef Bacik 	u64 start_ns = 0;
16130d9764f6SJosef Bacik 	u64 used;
16143a49a548SFilipe Manana 	int ret = -ENOSPC;
1615ef1317a1SJosef Bacik 	bool pending_tickets;
16160d9764f6SJosef Bacik 
16170d9764f6SJosef Bacik 	ASSERT(orig_bytes);
16189d0d47d5SFilipe Manana 	/*
16199d0d47d5SFilipe Manana 	 * If have a transaction handle (current->journal_info != NULL), then
16209d0d47d5SFilipe Manana 	 * the flush method can not be neither BTRFS_RESERVE_FLUSH_ALL* nor
16219d0d47d5SFilipe Manana 	 * BTRFS_RESERVE_FLUSH_EVICT, as we could deadlock because those
16229d0d47d5SFilipe Manana 	 * flushing methods can trigger transaction commits.
16239d0d47d5SFilipe Manana 	 */
16249d0d47d5SFilipe Manana 	if (current->journal_info) {
16259d0d47d5SFilipe Manana 		/* One assert per line for easier debugging. */
16269d0d47d5SFilipe Manana 		ASSERT(flush != BTRFS_RESERVE_FLUSH_ALL);
16279d0d47d5SFilipe Manana 		ASSERT(flush != BTRFS_RESERVE_FLUSH_ALL_STEAL);
16289d0d47d5SFilipe Manana 		ASSERT(flush != BTRFS_RESERVE_FLUSH_EVICT);
16299d0d47d5SFilipe Manana 	}
16300d9764f6SJosef Bacik 
163157056740SJosef Bacik 	if (flush == BTRFS_RESERVE_FLUSH_DATA)
163257056740SJosef Bacik 		async_work = &fs_info->async_data_reclaim_work;
163357056740SJosef Bacik 	else
163457056740SJosef Bacik 		async_work = &fs_info->async_reclaim_work;
163557056740SJosef Bacik 
16360d9764f6SJosef Bacik 	spin_lock(&space_info->lock);
16370d9764f6SJosef Bacik 	used = btrfs_space_info_used(space_info, true);
1638666daa9fSJosef Bacik 
1639666daa9fSJosef Bacik 	/*
1640666daa9fSJosef Bacik 	 * We don't want NO_FLUSH allocations to jump everybody, they can
1641666daa9fSJosef Bacik 	 * generally handle ENOSPC in a different way, so treat them the same as
1642666daa9fSJosef Bacik 	 * normal flushers when it comes to skipping pending tickets.
1643666daa9fSJosef Bacik 	 */
1644666daa9fSJosef Bacik 	if (is_normal_flushing(flush) || (flush == BTRFS_RESERVE_NO_FLUSH))
1645ef1317a1SJosef Bacik 		pending_tickets = !list_empty(&space_info->tickets) ||
1646ef1317a1SJosef Bacik 			!list_empty(&space_info->priority_tickets);
1647666daa9fSJosef Bacik 	else
1648666daa9fSJosef Bacik 		pending_tickets = !list_empty(&space_info->priority_tickets);
16490d9764f6SJosef Bacik 
16500d9764f6SJosef Bacik 	/*
16519b4851bcSGoldwyn Rodrigues 	 * Carry on if we have enough space (short-circuit) OR call
16529b4851bcSGoldwyn Rodrigues 	 * can_overcommit() to ensure we can overcommit to continue.
16530d9764f6SJosef Bacik 	 */
1654ef1317a1SJosef Bacik 	if (!pending_tickets &&
1655e15acc25SNaohiro Aota 	    ((used + orig_bytes <= space_info->total_bytes) ||
1656a30a3d20SJosef Bacik 	     btrfs_can_overcommit(fs_info, space_info, orig_bytes, flush))) {
16570d9764f6SJosef Bacik 		btrfs_space_info_update_bytes_may_use(fs_info, space_info,
16580d9764f6SJosef Bacik 						      orig_bytes);
16590d9764f6SJosef Bacik 		ret = 0;
16600d9764f6SJosef Bacik 	}
16610d9764f6SJosef Bacik 
16620d9764f6SJosef Bacik 	/*
1663765c3fe9SJosef Bacik 	 * Things are dire, we need to make a reservation so we don't abort.  We
1664765c3fe9SJosef Bacik 	 * will let this reservation go through as long as we have actual space
1665765c3fe9SJosef Bacik 	 * left to allocate for the block.
1666765c3fe9SJosef Bacik 	 */
1667765c3fe9SJosef Bacik 	if (ret && unlikely(flush == BTRFS_RESERVE_FLUSH_EMERGENCY)) {
1668765c3fe9SJosef Bacik 		used = btrfs_space_info_used(space_info, false);
1669e15acc25SNaohiro Aota 		if (used + orig_bytes <= space_info->total_bytes) {
1670765c3fe9SJosef Bacik 			btrfs_space_info_update_bytes_may_use(fs_info, space_info,
1671765c3fe9SJosef Bacik 							      orig_bytes);
1672765c3fe9SJosef Bacik 			ret = 0;
1673765c3fe9SJosef Bacik 		}
1674765c3fe9SJosef Bacik 	}
1675765c3fe9SJosef Bacik 
1676765c3fe9SJosef Bacik 	/*
16770d9764f6SJosef Bacik 	 * If we couldn't make a reservation then setup our reservation ticket
16780d9764f6SJosef Bacik 	 * and kick the async worker if it's not already running.
16790d9764f6SJosef Bacik 	 *
16800d9764f6SJosef Bacik 	 * If we are a priority flusher then we just need to add our ticket to
16810d9764f6SJosef Bacik 	 * the list and we will do our own flushing further down.
16820d9764f6SJosef Bacik 	 */
1683765c3fe9SJosef Bacik 	if (ret && can_ticket(flush)) {
16840d9764f6SJosef Bacik 		ticket.bytes = orig_bytes;
16850d9764f6SJosef Bacik 		ticket.error = 0;
1686db161806SNikolay Borisov 		space_info->reclaim_size += ticket.bytes;
16870d9764f6SJosef Bacik 		init_waitqueue_head(&ticket.wait);
1688ee6adbfdSJosef Bacik 		ticket.steal = can_steal(flush);
1689ac1ea10eSJosef Bacik 		if (trace_btrfs_reserve_ticket_enabled())
1690ac1ea10eSJosef Bacik 			start_ns = ktime_get_ns();
1691ac1ea10eSJosef Bacik 
16927f9fe614SJosef Bacik 		if (flush == BTRFS_RESERVE_FLUSH_ALL ||
169357056740SJosef Bacik 		    flush == BTRFS_RESERVE_FLUSH_ALL_STEAL ||
169457056740SJosef Bacik 		    flush == BTRFS_RESERVE_FLUSH_DATA) {
16950d9764f6SJosef Bacik 			list_add_tail(&ticket.list, &space_info->tickets);
16960d9764f6SJosef Bacik 			if (!space_info->flush) {
16970aae4ca9SJosef Bacik 				/*
16980aae4ca9SJosef Bacik 				 * We were forced to add a reserve ticket, so
16990aae4ca9SJosef Bacik 				 * our preemptive flushing is unable to keep
17000aae4ca9SJosef Bacik 				 * up.  Clamp down on the threshold for the
17010aae4ca9SJosef Bacik 				 * preemptive flushing in order to keep up with
17020aae4ca9SJosef Bacik 				 * the workload.
17030aae4ca9SJosef Bacik 				 */
17040aae4ca9SJosef Bacik 				maybe_clamp_preempt(fs_info, space_info);
17050aae4ca9SJosef Bacik 
17060d9764f6SJosef Bacik 				space_info->flush = 1;
17070d9764f6SJosef Bacik 				trace_btrfs_trigger_flush(fs_info,
17080d9764f6SJosef Bacik 							  space_info->flags,
17090d9764f6SJosef Bacik 							  orig_bytes, flush,
17100d9764f6SJosef Bacik 							  "enospc");
171157056740SJosef Bacik 				queue_work(system_unbound_wq, async_work);
17120d9764f6SJosef Bacik 			}
17130d9764f6SJosef Bacik 		} else {
17140d9764f6SJosef Bacik 			list_add_tail(&ticket.list,
17150d9764f6SJosef Bacik 				      &space_info->priority_tickets);
17160d9764f6SJosef Bacik 		}
17170d9764f6SJosef Bacik 	} else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
17180d9764f6SJosef Bacik 		/*
17190d9764f6SJosef Bacik 		 * We will do the space reservation dance during log replay,
17200d9764f6SJosef Bacik 		 * which means we won't have fs_info->fs_root set, so don't do
17210d9764f6SJosef Bacik 		 * the async reclaim as we will panic.
17220d9764f6SJosef Bacik 		 */
17230d9764f6SJosef Bacik 		if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
1724ed738ba7SJosef Bacik 		    !work_busy(&fs_info->preempt_reclaim_work) &&
1725ed738ba7SJosef Bacik 		    need_preemptive_reclaim(fs_info, space_info)) {
17260d9764f6SJosef Bacik 			trace_btrfs_trigger_flush(fs_info, space_info->flags,
17270d9764f6SJosef Bacik 						  orig_bytes, flush, "preempt");
17280d9764f6SJosef Bacik 			queue_work(system_unbound_wq,
1729576fa348SJosef Bacik 				   &fs_info->preempt_reclaim_work);
17300d9764f6SJosef Bacik 		}
17310d9764f6SJosef Bacik 	}
17320d9764f6SJosef Bacik 	spin_unlock(&space_info->lock);
1733765c3fe9SJosef Bacik 	if (!ret || !can_ticket(flush))
17340d9764f6SJosef Bacik 		return ret;
17350d9764f6SJosef Bacik 
1736ac1ea10eSJosef Bacik 	return handle_reserve_ticket(fs_info, space_info, &ticket, start_ns,
1737ac1ea10eSJosef Bacik 				     orig_bytes, flush);
17380d9764f6SJosef Bacik }
17390d9764f6SJosef Bacik 
174043dd529aSDavid Sterba /*
174143dd529aSDavid Sterba  * Try to reserve metadata bytes from the block_rsv's space.
1742d98b188eSNikolay Borisov  *
1743be8d1a2aSYang Li  * @fs_info:    the filesystem
1744d98b188eSNikolay Borisov  * @block_rsv:  block_rsv we're allocating for
1745d98b188eSNikolay Borisov  * @orig_bytes: number of bytes we want
1746d98b188eSNikolay Borisov  * @flush:      whether or not we can flush to make our reservation
17470d9764f6SJosef Bacik  *
17480d9764f6SJosef Bacik  * This will reserve orig_bytes number of bytes from the space info associated
17490d9764f6SJosef Bacik  * with the block_rsv.  If there is not enough space it will make an attempt to
17500d9764f6SJosef Bacik  * flush out space to make room.  It will do this by flushing delalloc if
17510d9764f6SJosef Bacik  * possible or committing the transaction.  If flush is 0 then no attempts to
17520d9764f6SJosef Bacik  * regain reservations will be made and this will fail if there is not enough
17530d9764f6SJosef Bacik  * space already.
17540d9764f6SJosef Bacik  */
btrfs_reserve_metadata_bytes(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * block_rsv,u64 orig_bytes,enum btrfs_reserve_flush_enum flush)17559270501cSJosef Bacik int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
17560d9764f6SJosef Bacik 				 struct btrfs_block_rsv *block_rsv,
17570d9764f6SJosef Bacik 				 u64 orig_bytes,
17580d9764f6SJosef Bacik 				 enum btrfs_reserve_flush_enum flush)
17590d9764f6SJosef Bacik {
17600d9764f6SJosef Bacik 	int ret;
17610d9764f6SJosef Bacik 
1762f3bda421SJosef Bacik 	ret = __reserve_bytes(fs_info, block_rsv->space_info, orig_bytes, flush);
17630d9764f6SJosef Bacik 	if (ret == -ENOSPC) {
17640d9764f6SJosef Bacik 		trace_btrfs_space_reservation(fs_info, "space_info:enospc",
17650d9764f6SJosef Bacik 					      block_rsv->space_info->flags,
17660d9764f6SJosef Bacik 					      orig_bytes, 1);
17670d9764f6SJosef Bacik 
17680d9764f6SJosef Bacik 		if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
17690d9764f6SJosef Bacik 			btrfs_dump_space_info(fs_info, block_rsv->space_info,
17700d9764f6SJosef Bacik 					      orig_bytes, 0);
17710d9764f6SJosef Bacik 	}
17720d9764f6SJosef Bacik 	return ret;
17730d9764f6SJosef Bacik }
17748698fc4eSJosef Bacik 
177543dd529aSDavid Sterba /*
177643dd529aSDavid Sterba  * Try to reserve data bytes for an allocation.
1777d98b188eSNikolay Borisov  *
1778d98b188eSNikolay Borisov  * @fs_info: the filesystem
1779d98b188eSNikolay Borisov  * @bytes:   number of bytes we need
1780d98b188eSNikolay Borisov  * @flush:   how we are allowed to flush
17818698fc4eSJosef Bacik  *
17828698fc4eSJosef Bacik  * This will reserve bytes from the data space info.  If there is not enough
17838698fc4eSJosef Bacik  * space then we will attempt to flush space as specified by flush.
17848698fc4eSJosef Bacik  */
btrfs_reserve_data_bytes(struct btrfs_fs_info * fs_info,u64 bytes,enum btrfs_reserve_flush_enum flush)17858698fc4eSJosef Bacik int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
17868698fc4eSJosef Bacik 			     enum btrfs_reserve_flush_enum flush)
17878698fc4eSJosef Bacik {
17888698fc4eSJosef Bacik 	struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
1789f3bda421SJosef Bacik 	int ret;
17908698fc4eSJosef Bacik 
1791f3bda421SJosef Bacik 	ASSERT(flush == BTRFS_RESERVE_FLUSH_DATA ||
17921daedb1dSJosef Bacik 	       flush == BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE ||
17931daedb1dSJosef Bacik 	       flush == BTRFS_RESERVE_NO_FLUSH);
17948698fc4eSJosef Bacik 	ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_DATA);
17958698fc4eSJosef Bacik 
1796f3bda421SJosef Bacik 	ret = __reserve_bytes(fs_info, data_sinfo, bytes, flush);
1797f3bda421SJosef Bacik 	if (ret == -ENOSPC) {
1798f3bda421SJosef Bacik 		trace_btrfs_space_reservation(fs_info, "space_info:enospc",
17998698fc4eSJosef Bacik 					      data_sinfo->flags, bytes, 1);
1800f3bda421SJosef Bacik 		if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
1801f3bda421SJosef Bacik 			btrfs_dump_space_info(fs_info, data_sinfo, bytes, 0);
1802f3bda421SJosef Bacik 	}
18038698fc4eSJosef Bacik 	return ret;
18048698fc4eSJosef Bacik }
18058e327b9cSQu Wenruo 
18068e327b9cSQu Wenruo /* Dump all the space infos when we abort a transaction due to ENOSPC. */
btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info * fs_info)18078e327b9cSQu Wenruo __cold void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info)
18088e327b9cSQu Wenruo {
18098e327b9cSQu Wenruo 	struct btrfs_space_info *space_info;
18108e327b9cSQu Wenruo 
18118e327b9cSQu Wenruo 	btrfs_info(fs_info, "dumping space info:");
18128e327b9cSQu Wenruo 	list_for_each_entry(space_info, &fs_info->space_info, list) {
18138e327b9cSQu Wenruo 		spin_lock(&space_info->lock);
18148e327b9cSQu Wenruo 		__btrfs_dump_space_info(fs_info, space_info);
18158e327b9cSQu Wenruo 		spin_unlock(&space_info->lock);
18168e327b9cSQu Wenruo 	}
18178e327b9cSQu Wenruo 	dump_global_block_rsv(fs_info);
18188e327b9cSQu Wenruo }
1819e2f13b34SJosef Bacik 
1820e2f13b34SJosef Bacik /*
1821e2f13b34SJosef Bacik  * Account the unused space of all the readonly block group in the space_info.
1822e2f13b34SJosef Bacik  * takes mirrors into account.
1823e2f13b34SJosef Bacik  */
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info * sinfo)1824e2f13b34SJosef Bacik u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
1825e2f13b34SJosef Bacik {
1826e2f13b34SJosef Bacik 	struct btrfs_block_group *block_group;
1827e2f13b34SJosef Bacik 	u64 free_bytes = 0;
1828e2f13b34SJosef Bacik 	int factor;
1829e2f13b34SJosef Bacik 
1830e2f13b34SJosef Bacik 	/* It's df, we don't care if it's racy */
1831e2f13b34SJosef Bacik 	if (list_empty(&sinfo->ro_bgs))
1832e2f13b34SJosef Bacik 		return 0;
1833e2f13b34SJosef Bacik 
1834e2f13b34SJosef Bacik 	spin_lock(&sinfo->lock);
1835e2f13b34SJosef Bacik 	list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
1836e2f13b34SJosef Bacik 		spin_lock(&block_group->lock);
1837e2f13b34SJosef Bacik 
1838e2f13b34SJosef Bacik 		if (!block_group->ro) {
1839e2f13b34SJosef Bacik 			spin_unlock(&block_group->lock);
1840e2f13b34SJosef Bacik 			continue;
1841e2f13b34SJosef Bacik 		}
1842e2f13b34SJosef Bacik 
1843e2f13b34SJosef Bacik 		factor = btrfs_bg_type_to_factor(block_group->flags);
1844e2f13b34SJosef Bacik 		free_bytes += (block_group->length -
1845e2f13b34SJosef Bacik 			       block_group->used) * factor;
1846e2f13b34SJosef Bacik 
1847e2f13b34SJosef Bacik 		spin_unlock(&block_group->lock);
1848e2f13b34SJosef Bacik 	}
1849e2f13b34SJosef Bacik 	spin_unlock(&sinfo->lock);
1850e2f13b34SJosef Bacik 
1851e2f13b34SJosef Bacik 	return free_bytes;
1852e2f13b34SJosef Bacik }
1853