xref: /openbmc/linux/fs/btrfs/space-info.c (revision 82220b18)
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;
3159d4b0a12SJosef Bacik 	found->bytes_zone_unusable += 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 -
527e50b122bSFilipe Manana 			cache->reserved - cache->delalloc_bytes -
528e50b122bSFilipe Manana 			cache->bytes_super - cache->zone_unusable;
5295da6afebSJosef Bacik 		btrfs_info(fs_info,
530e50b122bSFilipe Manana "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %llu delalloc %llu super %llu zone_unusable (%llu bytes available) %s",
531b3470b5dSDavid Sterba 			   cache->start, cache->length, cache->used, cache->pinned,
532b92e8f54SFilipe Manana 			   cache->reserved, cache->delalloc_bytes,
533b92e8f54SFilipe Manana 			   cache->bytes_super, cache->zone_unusable,
534e50b122bSFilipe Manana 			   avail, cache->ro ? "[readonly]" : "");
5355da6afebSJosef Bacik 		spin_unlock(&cache->lock);
536ab0db043SJosef Bacik 		btrfs_dump_free_space(cache, bytes);
5371ff9fee3SFilipe Manana 		total_avail += avail;
5385da6afebSJosef Bacik 	}
5395da6afebSJosef Bacik 	if (++index < BTRFS_NR_RAID_TYPES)
5405da6afebSJosef Bacik 		goto again;
5415da6afebSJosef Bacik 	up_read(&info->groups_sem);
5421ff9fee3SFilipe Manana 
5431ff9fee3SFilipe Manana 	btrfs_info(fs_info, "%llu bytes available across all block groups", total_avail);
5445da6afebSJosef Bacik }
5450d9764f6SJosef Bacik 
calc_reclaim_items_nr(const struct btrfs_fs_info * fs_info,u64 to_reclaim)546f4160ee8SFilipe Manana static inline u64 calc_reclaim_items_nr(const struct btrfs_fs_info *fs_info,
5470d9764f6SJosef Bacik 					u64 to_reclaim)
5480d9764f6SJosef Bacik {
5490d9764f6SJosef Bacik 	u64 bytes;
5500d9764f6SJosef Bacik 	u64 nr;
5510d9764f6SJosef Bacik 
5522bd36e7bSJosef Bacik 	bytes = btrfs_calc_insert_metadata_size(fs_info, 1);
5530d9764f6SJosef Bacik 	nr = div64_u64(to_reclaim, bytes);
5540d9764f6SJosef Bacik 	if (!nr)
5550d9764f6SJosef Bacik 		nr = 1;
5560d9764f6SJosef Bacik 	return nr;
5570d9764f6SJosef Bacik }
5580d9764f6SJosef Bacik 
calc_delayed_refs_nr(const struct btrfs_fs_info * fs_info,u64 to_reclaim)559f4160ee8SFilipe Manana static inline u64 calc_delayed_refs_nr(const struct btrfs_fs_info *fs_info,
560007145ffSFilipe Manana 				       u64 to_reclaim)
561007145ffSFilipe Manana {
5620e55a545SFilipe Manana 	const u64 bytes = btrfs_calc_delayed_ref_bytes(fs_info, 1);
563007145ffSFilipe Manana 	u64 nr;
564007145ffSFilipe Manana 
565007145ffSFilipe Manana 	nr = div64_u64(to_reclaim, bytes);
566007145ffSFilipe Manana 	if (!nr)
567007145ffSFilipe Manana 		nr = 1;
568007145ffSFilipe Manana 	return nr;
569007145ffSFilipe Manana }
570007145ffSFilipe Manana 
5710d9764f6SJosef Bacik #define EXTENT_SIZE_PER_ITEM	SZ_256K
5720d9764f6SJosef Bacik 
5730d9764f6SJosef Bacik /*
5740d9764f6SJosef Bacik  * shrink metadata reservation for delalloc
5750d9764f6SJosef Bacik  */
shrink_delalloc(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,u64 to_reclaim,bool wait_ordered,bool for_preempt)576920a9958SJosef Bacik static void shrink_delalloc(struct btrfs_fs_info *fs_info,
577920a9958SJosef Bacik 			    struct btrfs_space_info *space_info,
578385f421fSJosef Bacik 			    u64 to_reclaim, bool wait_ordered,
579385f421fSJosef Bacik 			    bool for_preempt)
5800d9764f6SJosef Bacik {
5810d9764f6SJosef Bacik 	struct btrfs_trans_handle *trans;
5820d9764f6SJosef Bacik 	u64 delalloc_bytes;
5835deb17e1SJosef Bacik 	u64 ordered_bytes;
5840d9764f6SJosef Bacik 	u64 items;
5850d9764f6SJosef Bacik 	long time_left;
5860d9764f6SJosef Bacik 	int loops;
5870d9764f6SJosef Bacik 
58803fe78ccSJosef Bacik 	delalloc_bytes = percpu_counter_sum_positive(&fs_info->delalloc_bytes);
58903fe78ccSJosef Bacik 	ordered_bytes = percpu_counter_sum_positive(&fs_info->ordered_bytes);
59003fe78ccSJosef Bacik 	if (delalloc_bytes == 0 && ordered_bytes == 0)
59103fe78ccSJosef Bacik 		return;
59203fe78ccSJosef Bacik 
5930d9764f6SJosef Bacik 	/* Calc the number of the pages we need flush for space reservation */
594d7f81facSJosef Bacik 	if (to_reclaim == U64_MAX) {
595d7f81facSJosef Bacik 		items = U64_MAX;
596d7f81facSJosef Bacik 	} else {
597d7f81facSJosef Bacik 		/*
598d7f81facSJosef Bacik 		 * to_reclaim is set to however much metadata we need to
599d7f81facSJosef Bacik 		 * reclaim, but reclaiming that much data doesn't really track
60003fe78ccSJosef Bacik 		 * exactly.  What we really want to do is reclaim full inode's
60103fe78ccSJosef Bacik 		 * worth of reservations, however that's not available to us
60203fe78ccSJosef Bacik 		 * here.  We will take a fraction of the delalloc bytes for our
60303fe78ccSJosef Bacik 		 * flushing loops and hope for the best.  Delalloc will expand
60403fe78ccSJosef Bacik 		 * the amount we write to cover an entire dirty extent, which
60503fe78ccSJosef Bacik 		 * will reclaim the metadata reservation for that range.  If
60603fe78ccSJosef Bacik 		 * it's not enough subsequent flush stages will be more
60703fe78ccSJosef Bacik 		 * aggressive.
608d7f81facSJosef Bacik 		 */
60903fe78ccSJosef Bacik 		to_reclaim = max(to_reclaim, delalloc_bytes >> 3);
610d7f81facSJosef Bacik 		items = calc_reclaim_items_nr(fs_info, to_reclaim) * 2;
611d7f81facSJosef Bacik 	}
6120d9764f6SJosef Bacik 
6130d031dc4SYu Zhe 	trans = current->journal_info;
6140d9764f6SJosef Bacik 
6150d9764f6SJosef Bacik 	/*
6160d9764f6SJosef Bacik 	 * If we are doing more ordered than delalloc we need to just wait on
6170d9764f6SJosef Bacik 	 * ordered extents, otherwise we'll waste time trying to flush delalloc
6180d9764f6SJosef Bacik 	 * that likely won't give us the space back we need.
6190d9764f6SJosef Bacik 	 */
620385f421fSJosef Bacik 	if (ordered_bytes > delalloc_bytes && !for_preempt)
6210d9764f6SJosef Bacik 		wait_ordered = true;
6220d9764f6SJosef Bacik 
6230d9764f6SJosef Bacik 	loops = 0;
6245deb17e1SJosef Bacik 	while ((delalloc_bytes || ordered_bytes) && loops < 3) {
6259db4dc24SNikolay Borisov 		u64 temp = min(delalloc_bytes, to_reclaim) >> PAGE_SHIFT;
6269db4dc24SNikolay Borisov 		long nr_pages = min_t(u64, temp, LONG_MAX);
627e1646070SJosef Bacik 		int async_pages;
628e076ab2aSJosef Bacik 
629e076ab2aSJosef Bacik 		btrfs_start_delalloc_roots(fs_info, nr_pages, true);
6300d9764f6SJosef Bacik 
631e1646070SJosef Bacik 		/*
632e1646070SJosef Bacik 		 * We need to make sure any outstanding async pages are now
633e1646070SJosef Bacik 		 * processed before we continue.  This is because things like
634e1646070SJosef Bacik 		 * sync_inode() try to be smart and skip writing if the inode is
635e1646070SJosef Bacik 		 * marked clean.  We don't use filemap_fwrite for flushing
636e1646070SJosef Bacik 		 * because we want to control how many pages we write out at a
637e1646070SJosef Bacik 		 * time, thus this is the only safe way to make sure we've
638e1646070SJosef Bacik 		 * waited for outstanding compressed workers to have started
639e1646070SJosef Bacik 		 * their jobs and thus have ordered extents set up properly.
640e1646070SJosef Bacik 		 *
641e1646070SJosef Bacik 		 * This exists because we do not want to wait for each
642e1646070SJosef Bacik 		 * individual inode to finish its async work, we simply want to
643e1646070SJosef Bacik 		 * start the IO on everybody, and then come back here and wait
644e1646070SJosef Bacik 		 * for all of the async work to catch up.  Once we're done with
645e1646070SJosef Bacik 		 * that we know we'll have ordered extents for everything and we
646e1646070SJosef Bacik 		 * can decide if we wait for that or not.
647e1646070SJosef Bacik 		 *
648e1646070SJosef Bacik 		 * If we choose to replace this in the future, make absolutely
649e1646070SJosef Bacik 		 * sure that the proper waiting is being done in the async case,
650e1646070SJosef Bacik 		 * as there have been bugs in that area before.
651e1646070SJosef Bacik 		 */
652e1646070SJosef Bacik 		async_pages = atomic_read(&fs_info->async_delalloc_pages);
653e1646070SJosef Bacik 		if (!async_pages)
654e1646070SJosef Bacik 			goto skip_async;
655e1646070SJosef Bacik 
656e1646070SJosef Bacik 		/*
657e1646070SJosef Bacik 		 * We don't want to wait forever, if we wrote less pages in this
658e1646070SJosef Bacik 		 * loop than we have outstanding, only wait for that number of
659e1646070SJosef Bacik 		 * pages, otherwise we can wait for all async pages to finish
660e1646070SJosef Bacik 		 * before continuing.
661e1646070SJosef Bacik 		 */
662e1646070SJosef Bacik 		if (async_pages > nr_pages)
663e1646070SJosef Bacik 			async_pages -= nr_pages;
664e1646070SJosef Bacik 		else
665e1646070SJosef Bacik 			async_pages = 0;
666e1646070SJosef Bacik 		wait_event(fs_info->async_submit_wait,
667e1646070SJosef Bacik 			   atomic_read(&fs_info->async_delalloc_pages) <=
668e1646070SJosef Bacik 			   async_pages);
669e1646070SJosef Bacik skip_async:
6700d9764f6SJosef Bacik 		loops++;
6710d9764f6SJosef Bacik 		if (wait_ordered && !trans) {
6720d9764f6SJosef Bacik 			btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
6730d9764f6SJosef Bacik 		} else {
6740d9764f6SJosef Bacik 			time_left = schedule_timeout_killable(1);
6750d9764f6SJosef Bacik 			if (time_left)
6760d9764f6SJosef Bacik 				break;
6770d9764f6SJosef Bacik 		}
678448b966bSJosef Bacik 
679385f421fSJosef Bacik 		/*
680385f421fSJosef Bacik 		 * If we are for preemption we just want a one-shot of delalloc
681385f421fSJosef Bacik 		 * flushing so we can stop flushing if we decide we don't need
682385f421fSJosef Bacik 		 * to anymore.
683385f421fSJosef Bacik 		 */
684385f421fSJosef Bacik 		if (for_preempt)
685385f421fSJosef Bacik 			break;
686385f421fSJosef Bacik 
687448b966bSJosef Bacik 		spin_lock(&space_info->lock);
688448b966bSJosef Bacik 		if (list_empty(&space_info->tickets) &&
689448b966bSJosef Bacik 		    list_empty(&space_info->priority_tickets)) {
690448b966bSJosef Bacik 			spin_unlock(&space_info->lock);
691448b966bSJosef Bacik 			break;
692448b966bSJosef Bacik 		}
693448b966bSJosef Bacik 		spin_unlock(&space_info->lock);
694448b966bSJosef Bacik 
6950d9764f6SJosef Bacik 		delalloc_bytes = percpu_counter_sum_positive(
6960d9764f6SJosef Bacik 						&fs_info->delalloc_bytes);
6975deb17e1SJosef Bacik 		ordered_bytes = percpu_counter_sum_positive(
6985deb17e1SJosef Bacik 						&fs_info->ordered_bytes);
6990d9764f6SJosef Bacik 	}
7000d9764f6SJosef Bacik }
7010d9764f6SJosef Bacik 
7020d9764f6SJosef Bacik /*
7030d9764f6SJosef Bacik  * Try to flush some data based on policy set by @state. This is only advisory
7040d9764f6SJosef Bacik  * and may fail for various reasons. The caller is supposed to examine the
7050d9764f6SJosef Bacik  * state of @space_info to detect the outcome.
7060d9764f6SJosef 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)7070d9764f6SJosef Bacik static void flush_space(struct btrfs_fs_info *fs_info,
7080d9764f6SJosef Bacik 		       struct btrfs_space_info *space_info, u64 num_bytes,
7094b02b00fSJosef Bacik 		       enum btrfs_flush_state state, bool for_preempt)
7100d9764f6SJosef Bacik {
711ce5603d0SJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
7120d9764f6SJosef Bacik 	struct btrfs_trans_handle *trans;
7130d9764f6SJosef Bacik 	int nr;
7140d9764f6SJosef Bacik 	int ret = 0;
7150d9764f6SJosef Bacik 
7160d9764f6SJosef Bacik 	switch (state) {
7170d9764f6SJosef Bacik 	case FLUSH_DELAYED_ITEMS_NR:
7180d9764f6SJosef Bacik 	case FLUSH_DELAYED_ITEMS:
7190d9764f6SJosef Bacik 		if (state == FLUSH_DELAYED_ITEMS_NR)
7200d9764f6SJosef Bacik 			nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
7210d9764f6SJosef Bacik 		else
7220d9764f6SJosef Bacik 			nr = -1;
7230d9764f6SJosef Bacik 
7242391245aSFilipe Manana 		trans = btrfs_join_transaction_nostart(root);
7250d9764f6SJosef Bacik 		if (IS_ERR(trans)) {
7260d9764f6SJosef Bacik 			ret = PTR_ERR(trans);
7272391245aSFilipe Manana 			if (ret == -ENOENT)
7282391245aSFilipe Manana 				ret = 0;
7290d9764f6SJosef Bacik 			break;
7300d9764f6SJosef Bacik 		}
7310d9764f6SJosef Bacik 		ret = btrfs_run_delayed_items_nr(trans, nr);
7320d9764f6SJosef Bacik 		btrfs_end_transaction(trans);
7330d9764f6SJosef Bacik 		break;
7340d9764f6SJosef Bacik 	case FLUSH_DELALLOC:
7350d9764f6SJosef Bacik 	case FLUSH_DELALLOC_WAIT:
73603fe78ccSJosef Bacik 	case FLUSH_DELALLOC_FULL:
73703fe78ccSJosef Bacik 		if (state == FLUSH_DELALLOC_FULL)
73803fe78ccSJosef Bacik 			num_bytes = U64_MAX;
739920a9958SJosef Bacik 		shrink_delalloc(fs_info, space_info, num_bytes,
74003fe78ccSJosef Bacik 				state != FLUSH_DELALLOC, for_preempt);
7410d9764f6SJosef Bacik 		break;
7420d9764f6SJosef Bacik 	case FLUSH_DELAYED_REFS_NR:
7430d9764f6SJosef Bacik 	case FLUSH_DELAYED_REFS:
7442391245aSFilipe Manana 		trans = btrfs_join_transaction_nostart(root);
7450d9764f6SJosef Bacik 		if (IS_ERR(trans)) {
7460d9764f6SJosef Bacik 			ret = PTR_ERR(trans);
7472391245aSFilipe Manana 			if (ret == -ENOENT)
7482391245aSFilipe Manana 				ret = 0;
7490d9764f6SJosef Bacik 			break;
7500d9764f6SJosef Bacik 		}
7510d9764f6SJosef Bacik 		if (state == FLUSH_DELAYED_REFS_NR)
752007145ffSFilipe Manana 			nr = calc_delayed_refs_nr(fs_info, num_bytes);
7530d9764f6SJosef Bacik 		else
7540d9764f6SJosef Bacik 			nr = 0;
7550d9764f6SJosef Bacik 		btrfs_run_delayed_refs(trans, nr);
7560d9764f6SJosef Bacik 		btrfs_end_transaction(trans);
7570d9764f6SJosef Bacik 		break;
7580d9764f6SJosef Bacik 	case ALLOC_CHUNK:
7590d9764f6SJosef Bacik 	case ALLOC_CHUNK_FORCE:
7600d9764f6SJosef Bacik 		trans = btrfs_join_transaction(root);
7610d9764f6SJosef Bacik 		if (IS_ERR(trans)) {
7620d9764f6SJosef Bacik 			ret = PTR_ERR(trans);
7630d9764f6SJosef Bacik 			break;
7640d9764f6SJosef Bacik 		}
7650d9764f6SJosef Bacik 		ret = btrfs_chunk_alloc(trans,
766c6c45303SJosef Bacik 				btrfs_get_alloc_profile(fs_info, space_info->flags),
7670d9764f6SJosef Bacik 				(state == ALLOC_CHUNK) ? CHUNK_ALLOC_NO_FORCE :
7680d9764f6SJosef Bacik 					CHUNK_ALLOC_FORCE);
7690d9764f6SJosef Bacik 		btrfs_end_transaction(trans);
770b0931513SNaohiro Aota 
7710d9764f6SJosef Bacik 		if (ret > 0 || ret == -ENOSPC)
7720d9764f6SJosef Bacik 			ret = 0;
7730d9764f6SJosef Bacik 		break;
774844245b4SJosef Bacik 	case RUN_DELAYED_IPUTS:
7750d9764f6SJosef Bacik 		/*
7760d9764f6SJosef Bacik 		 * If we have pending delayed iputs then we could free up a
7770d9764f6SJosef Bacik 		 * bunch of pinned space, so make sure we run the iputs before
7780d9764f6SJosef Bacik 		 * we do our pinned bytes check below.
7790d9764f6SJosef Bacik 		 */
7800d9764f6SJosef Bacik 		btrfs_run_delayed_iputs(fs_info);
7810d9764f6SJosef Bacik 		btrfs_wait_on_delayed_iputs(fs_info);
782844245b4SJosef Bacik 		break;
783844245b4SJosef Bacik 	case COMMIT_TRANS:
784c416a30cSJosef Bacik 		ASSERT(current->journal_info == NULL);
7852ee70ed1SFilipe Manana 		/*
7862ee70ed1SFilipe Manana 		 * We don't want to start a new transaction, just attach to the
7872ee70ed1SFilipe Manana 		 * current one or wait it fully commits in case its commit is
7882ee70ed1SFilipe Manana 		 * happening at the moment. Note: we don't use a nostart join
7892ee70ed1SFilipe Manana 		 * because that does not wait for a transaction to fully commit
7902ee70ed1SFilipe Manana 		 * (only for it to be unblocked, state TRANS_STATE_UNBLOCKED).
7912ee70ed1SFilipe Manana 		 */
7922ee70ed1SFilipe Manana 		trans = btrfs_attach_transaction_barrier(root);
793f00c42ddSJosef Bacik 		if (IS_ERR(trans)) {
794f00c42ddSJosef Bacik 			ret = PTR_ERR(trans);
7952ee70ed1SFilipe Manana 			if (ret == -ENOENT)
7962ee70ed1SFilipe Manana 				ret = 0;
797f00c42ddSJosef Bacik 			break;
798f00c42ddSJosef Bacik 		}
799f00c42ddSJosef Bacik 		ret = btrfs_commit_transaction(trans);
800f00c42ddSJosef Bacik 		break;
8010d9764f6SJosef Bacik 	default:
8020d9764f6SJosef Bacik 		ret = -ENOSPC;
8030d9764f6SJosef Bacik 		break;
8040d9764f6SJosef Bacik 	}
8050d9764f6SJosef Bacik 
8060d9764f6SJosef Bacik 	trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
8074b02b00fSJosef Bacik 				ret, for_preempt);
8080d9764f6SJosef Bacik 	return;
8090d9764f6SJosef Bacik }
8100d9764f6SJosef Bacik 
8110d9764f6SJosef Bacik static inline u64
btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)8120d9764f6SJosef Bacik btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
8139f246926SJosef Bacik 				 struct btrfs_space_info *space_info)
8140d9764f6SJosef Bacik {
8150d9764f6SJosef Bacik 	u64 used;
816fa121a26SJosef Bacik 	u64 avail;
817db161806SNikolay Borisov 	u64 to_reclaim = space_info->reclaim_size;
8180d9764f6SJosef Bacik 
819db161806SNikolay Borisov 	lockdep_assert_held(&space_info->lock);
820fa121a26SJosef Bacik 
821fa121a26SJosef Bacik 	avail = calc_available_free_space(fs_info, space_info,
822fa121a26SJosef Bacik 					  BTRFS_RESERVE_FLUSH_ALL);
823fa121a26SJosef Bacik 	used = btrfs_space_info_used(space_info, true);
824fa121a26SJosef Bacik 
825fa121a26SJosef Bacik 	/*
826fa121a26SJosef Bacik 	 * We may be flushing because suddenly we have less space than we had
827fa121a26SJosef Bacik 	 * before, and now we're well over-committed based on our current free
828fa121a26SJosef Bacik 	 * space.  If that's the case add in our overage so we make sure to put
829fa121a26SJosef Bacik 	 * appropriate pressure on the flushing state machine.
830fa121a26SJosef Bacik 	 */
831e15acc25SNaohiro Aota 	if (space_info->total_bytes + avail < used)
832e15acc25SNaohiro Aota 		to_reclaim += used - (space_info->total_bytes + avail);
833fa121a26SJosef Bacik 
8340d9764f6SJosef Bacik 	return to_reclaim;
8350d9764f6SJosef Bacik }
8360d9764f6SJosef Bacik 
need_preemptive_reclaim(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)837ae7913baSJosef Bacik static bool need_preemptive_reclaim(struct btrfs_fs_info *fs_info,
8382e294c60SJosef Bacik 				    struct btrfs_space_info *space_info)
8390d9764f6SJosef Bacik {
840*82220b18SFilipe Manana 	const u64 global_rsv_size = btrfs_block_rsv_reserved(&fs_info->global_block_rsv);
8412e294c60SJosef Bacik 	u64 ordered, delalloc;
8426a921de5SNaohiro Aota 	u64 thresh;
8432e294c60SJosef Bacik 	u64 used;
8440d9764f6SJosef Bacik 
845e15acc25SNaohiro Aota 	thresh = mult_perc(space_info->total_bytes, 90);
8466a921de5SNaohiro Aota 
847bf7bd725SNiels Dossche 	lockdep_assert_held(&space_info->lock);
848bf7bd725SNiels Dossche 
8490d9764f6SJosef Bacik 	/* If we're just plain full then async reclaim just slows us down. */
850610a6ef4SJosef Bacik 	if ((space_info->bytes_used + space_info->bytes_reserved +
851610a6ef4SJosef Bacik 	     global_rsv_size) >= thresh)
852ae7913baSJosef Bacik 		return false;
8530d9764f6SJosef Bacik 
85411462397SJosef Bacik 	used = space_info->bytes_may_use + space_info->bytes_pinned;
85511462397SJosef Bacik 
85611462397SJosef Bacik 	/* The total flushable belongs to the global rsv, don't flush. */
85711462397SJosef Bacik 	if (global_rsv_size >= used)
85811462397SJosef Bacik 		return false;
85911462397SJosef Bacik 
86011462397SJosef Bacik 	/*
86111462397SJosef Bacik 	 * 128MiB is 1/4 of the maximum global rsv size.  If we have less than
86211462397SJosef Bacik 	 * that devoted to other reservations then there's no sense in flushing,
86311462397SJosef Bacik 	 * we don't have a lot of things that need flushing.
86411462397SJosef Bacik 	 */
86511462397SJosef Bacik 	if (used - global_rsv_size <= SZ_128M)
86611462397SJosef Bacik 		return false;
86711462397SJosef Bacik 
868f205edf7SJosef Bacik 	/*
869f205edf7SJosef Bacik 	 * We have tickets queued, bail so we don't compete with the async
870f205edf7SJosef Bacik 	 * flushers.
871f205edf7SJosef Bacik 	 */
872f205edf7SJosef Bacik 	if (space_info->reclaim_size)
873f205edf7SJosef Bacik 		return false;
874f205edf7SJosef Bacik 
8752e294c60SJosef Bacik 	/*
8762e294c60SJosef Bacik 	 * If we have over half of the free space occupied by reservations or
8772e294c60SJosef Bacik 	 * pinned then we want to start flushing.
8782e294c60SJosef Bacik 	 *
8792e294c60SJosef Bacik 	 * We do not do the traditional thing here, which is to say
8802e294c60SJosef Bacik 	 *
8812e294c60SJosef Bacik 	 *   if (used >= ((total_bytes + avail) / 2))
8822e294c60SJosef Bacik 	 *     return 1;
8832e294c60SJosef Bacik 	 *
8842e294c60SJosef Bacik 	 * because this doesn't quite work how we want.  If we had more than 50%
8852e294c60SJosef Bacik 	 * of the space_info used by bytes_used and we had 0 available we'd just
8862e294c60SJosef Bacik 	 * constantly run the background flusher.  Instead we want it to kick in
88788a777a6SJosef Bacik 	 * if our reclaimable space exceeds our clamped free space.
88888a777a6SJosef Bacik 	 *
88988a777a6SJosef Bacik 	 * Our clamping range is 2^1 -> 2^8.  Practically speaking that means
89088a777a6SJosef Bacik 	 * the following:
89188a777a6SJosef Bacik 	 *
89288a777a6SJosef Bacik 	 * Amount of RAM        Minimum threshold       Maximum threshold
89388a777a6SJosef Bacik 	 *
89488a777a6SJosef Bacik 	 *        256GiB                     1GiB                  128GiB
89588a777a6SJosef Bacik 	 *        128GiB                   512MiB                   64GiB
89688a777a6SJosef Bacik 	 *         64GiB                   256MiB                   32GiB
89788a777a6SJosef Bacik 	 *         32GiB                   128MiB                   16GiB
89888a777a6SJosef Bacik 	 *         16GiB                    64MiB                    8GiB
89988a777a6SJosef Bacik 	 *
90088a777a6SJosef Bacik 	 * These are the range our thresholds will fall in, corresponding to how
90188a777a6SJosef Bacik 	 * much delalloc we need for the background flusher to kick in.
9022e294c60SJosef Bacik 	 */
90388a777a6SJosef Bacik 
9042e294c60SJosef Bacik 	thresh = calc_available_free_space(fs_info, space_info,
9052e294c60SJosef Bacik 					   BTRFS_RESERVE_FLUSH_ALL);
9061239e2daSJosef Bacik 	used = space_info->bytes_used + space_info->bytes_reserved +
9071239e2daSJosef Bacik 	       space_info->bytes_readonly + global_rsv_size;
908e15acc25SNaohiro Aota 	if (used < space_info->total_bytes)
909e15acc25SNaohiro Aota 		thresh += space_info->total_bytes - used;
91088a777a6SJosef Bacik 	thresh >>= space_info->clamp;
9119f42d377SJosef Bacik 
9122e294c60SJosef Bacik 	used = space_info->bytes_pinned;
9139f42d377SJosef Bacik 
9142e294c60SJosef Bacik 	/*
9152e294c60SJosef Bacik 	 * If we have more ordered bytes than delalloc bytes then we're either
9162e294c60SJosef Bacik 	 * doing a lot of DIO, or we simply don't have a lot of delalloc waiting
9172e294c60SJosef Bacik 	 * around.  Preemptive flushing is only useful in that it can free up
9182e294c60SJosef Bacik 	 * space before tickets need to wait for things to finish.  In the case
9192e294c60SJosef Bacik 	 * of ordered extents, preemptively waiting on ordered extents gets us
9202e294c60SJosef Bacik 	 * nothing, if our reservations are tied up in ordered extents we'll
9212e294c60SJosef Bacik 	 * simply have to slow down writers by forcing them to wait on ordered
9222e294c60SJosef Bacik 	 * extents.
9232e294c60SJosef Bacik 	 *
9242e294c60SJosef Bacik 	 * In the case that ordered is larger than delalloc, only include the
9252e294c60SJosef Bacik 	 * block reserves that we would actually be able to directly reclaim
9262e294c60SJosef Bacik 	 * from.  In this case if we're heavy on metadata operations this will
9272e294c60SJosef Bacik 	 * clearly be heavy enough to warrant preemptive flushing.  In the case
9282e294c60SJosef Bacik 	 * of heavy DIO or ordered reservations, preemptive flushing will just
9292e294c60SJosef Bacik 	 * waste time and cause us to slow down.
9303e101569SJosef Bacik 	 *
9313e101569SJosef Bacik 	 * We want to make sure we truly are maxed out on ordered however, so
9323e101569SJosef Bacik 	 * cut ordered in half, and if it's still higher than delalloc then we
9333e101569SJosef Bacik 	 * can keep flushing.  This is to avoid the case where we start
9343e101569SJosef Bacik 	 * flushing, and now delalloc == ordered and we stop preemptively
9353e101569SJosef Bacik 	 * flushing when we could still have several gigs of delalloc to flush.
9362e294c60SJosef Bacik 	 */
9373e101569SJosef Bacik 	ordered = percpu_counter_read_positive(&fs_info->ordered_bytes) >> 1;
9382cdb3909SJosef Bacik 	delalloc = percpu_counter_read_positive(&fs_info->delalloc_bytes);
9392e294c60SJosef Bacik 	if (ordered >= delalloc)
940*82220b18SFilipe Manana 		used += btrfs_block_rsv_reserved(&fs_info->delayed_refs_rsv) +
941*82220b18SFilipe Manana 			btrfs_block_rsv_reserved(&fs_info->delayed_block_rsv);
9429f42d377SJosef Bacik 	else
94330acce4eSJosef Bacik 		used += space_info->bytes_may_use - global_rsv_size;
9440d9764f6SJosef Bacik 
9450d9764f6SJosef Bacik 	return (used >= thresh && !btrfs_fs_closing(fs_info) &&
9460d9764f6SJosef Bacik 		!test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
9470d9764f6SJosef Bacik }
9480d9764f6SJosef Bacik 
steal_from_global_rsv(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,struct reserve_ticket * ticket)9497f9fe614SJosef Bacik static bool steal_from_global_rsv(struct btrfs_fs_info *fs_info,
9507f9fe614SJosef Bacik 				  struct btrfs_space_info *space_info,
9517f9fe614SJosef Bacik 				  struct reserve_ticket *ticket)
9527f9fe614SJosef Bacik {
9537f9fe614SJosef Bacik 	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
9547f9fe614SJosef Bacik 	u64 min_bytes;
9557f9fe614SJosef Bacik 
9561b0309eaSJosef Bacik 	if (!ticket->steal)
9571b0309eaSJosef Bacik 		return false;
9581b0309eaSJosef Bacik 
9597f9fe614SJosef Bacik 	if (global_rsv->space_info != space_info)
9607f9fe614SJosef Bacik 		return false;
9617f9fe614SJosef Bacik 
9627f9fe614SJosef Bacik 	spin_lock(&global_rsv->lock);
963428c8e03SDavid Sterba 	min_bytes = mult_perc(global_rsv->size, 10);
9647f9fe614SJosef Bacik 	if (global_rsv->reserved < min_bytes + ticket->bytes) {
9657f9fe614SJosef Bacik 		spin_unlock(&global_rsv->lock);
9667f9fe614SJosef Bacik 		return false;
9677f9fe614SJosef Bacik 	}
9687f9fe614SJosef Bacik 	global_rsv->reserved -= ticket->bytes;
9696d548b9eSFilipe Manana 	remove_ticket(space_info, ticket);
9707f9fe614SJosef Bacik 	ticket->bytes = 0;
9717f9fe614SJosef Bacik 	wake_up(&ticket->wait);
9727f9fe614SJosef Bacik 	space_info->tickets_id++;
9737f9fe614SJosef Bacik 	if (global_rsv->reserved < global_rsv->size)
9747f9fe614SJosef Bacik 		global_rsv->full = 0;
9757f9fe614SJosef Bacik 	spin_unlock(&global_rsv->lock);
9767f9fe614SJosef Bacik 
9777f9fe614SJosef Bacik 	return true;
9787f9fe614SJosef Bacik }
9797f9fe614SJosef Bacik 
9802341ccd1SJosef Bacik /*
9812341ccd1SJosef Bacik  * maybe_fail_all_tickets - we've exhausted our flushing, start failing tickets
9822341ccd1SJosef Bacik  * @fs_info - fs_info for this fs
9832341ccd1SJosef Bacik  * @space_info - the space info we were flushing
9842341ccd1SJosef Bacik  *
9852341ccd1SJosef Bacik  * We call this when we've exhausted our flushing ability and haven't made
9862341ccd1SJosef Bacik  * progress in satisfying tickets.  The reservation code handles tickets in
9872341ccd1SJosef Bacik  * order, so if there is a large ticket first and then smaller ones we could
9882341ccd1SJosef Bacik  * very well satisfy the smaller tickets.  This will attempt to wake up any
9892341ccd1SJosef Bacik  * tickets in the list to catch this case.
9902341ccd1SJosef Bacik  *
9912341ccd1SJosef Bacik  * This function returns true if it was able to make progress by clearing out
9922341ccd1SJosef Bacik  * other tickets, or if it stumbles across a ticket that was smaller than the
9932341ccd1SJosef Bacik  * first ticket.
9942341ccd1SJosef Bacik  */
maybe_fail_all_tickets(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)9952341ccd1SJosef Bacik static bool maybe_fail_all_tickets(struct btrfs_fs_info *fs_info,
9962341ccd1SJosef Bacik 				   struct btrfs_space_info *space_info)
9970d9764f6SJosef Bacik {
9980d9764f6SJosef Bacik 	struct reserve_ticket *ticket;
9992341ccd1SJosef Bacik 	u64 tickets_id = space_info->tickets_id;
10000e24f6d8SJosef Bacik 	const bool aborted = BTRFS_FS_ERROR(fs_info);
10010d9764f6SJosef Bacik 
1002fcdef39cSJosef Bacik 	trace_btrfs_fail_all_tickets(fs_info, space_info);
1003fcdef39cSJosef Bacik 
100484fe47a4SJosef Bacik 	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
100584fe47a4SJosef Bacik 		btrfs_info(fs_info, "cannot satisfy tickets, dumping space info");
100684fe47a4SJosef Bacik 		__btrfs_dump_space_info(fs_info, space_info);
100784fe47a4SJosef Bacik 	}
100884fe47a4SJosef Bacik 
10092341ccd1SJosef Bacik 	while (!list_empty(&space_info->tickets) &&
10102341ccd1SJosef Bacik 	       tickets_id == space_info->tickets_id) {
10112341ccd1SJosef Bacik 		ticket = list_first_entry(&space_info->tickets,
10122341ccd1SJosef Bacik 					  struct reserve_ticket, list);
10132341ccd1SJosef Bacik 
10141b0309eaSJosef Bacik 		if (!aborted && steal_from_global_rsv(fs_info, space_info, ticket))
10157f9fe614SJosef Bacik 			return true;
10167f9fe614SJosef Bacik 
10170e24f6d8SJosef Bacik 		if (!aborted && btrfs_test_opt(fs_info, ENOSPC_DEBUG))
101884fe47a4SJosef Bacik 			btrfs_info(fs_info, "failing ticket with %llu bytes",
101984fe47a4SJosef Bacik 				   ticket->bytes);
102084fe47a4SJosef Bacik 
1021d611add4SFilipe Manana 		remove_ticket(space_info, ticket);
10220e24f6d8SJosef Bacik 		if (aborted)
10230e24f6d8SJosef Bacik 			ticket->error = -EIO;
10240e24f6d8SJosef Bacik 		else
10250d9764f6SJosef Bacik 			ticket->error = -ENOSPC;
10260d9764f6SJosef Bacik 		wake_up(&ticket->wait);
10272341ccd1SJosef Bacik 
10282341ccd1SJosef Bacik 		/*
10292341ccd1SJosef Bacik 		 * We're just throwing tickets away, so more flushing may not
10302341ccd1SJosef Bacik 		 * trip over btrfs_try_granting_tickets, so we need to call it
10312341ccd1SJosef Bacik 		 * here to see if we can make progress with the next ticket in
10322341ccd1SJosef Bacik 		 * the list.
10332341ccd1SJosef Bacik 		 */
10340e24f6d8SJosef Bacik 		if (!aborted)
10352341ccd1SJosef Bacik 			btrfs_try_granting_tickets(fs_info, space_info);
10360d9764f6SJosef Bacik 	}
10372341ccd1SJosef Bacik 	return (tickets_id != space_info->tickets_id);
10380d9764f6SJosef Bacik }
10390d9764f6SJosef Bacik 
10400d9764f6SJosef Bacik /*
10410d9764f6SJosef Bacik  * This is for normal flushers, we can wait all goddamned day if we want to.  We
10420d9764f6SJosef Bacik  * will loop and continuously try to flush as long as we are making progress.
10430d9764f6SJosef Bacik  * We count progress as clearing off tickets each time we have to loop.
10440d9764f6SJosef Bacik  */
btrfs_async_reclaim_metadata_space(struct work_struct * work)10450d9764f6SJosef Bacik static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
10460d9764f6SJosef Bacik {
10470d9764f6SJosef Bacik 	struct btrfs_fs_info *fs_info;
10480d9764f6SJosef Bacik 	struct btrfs_space_info *space_info;
10490d9764f6SJosef Bacik 	u64 to_reclaim;
105091e79a83SJosef Bacik 	enum btrfs_flush_state flush_state;
10510d9764f6SJosef Bacik 	int commit_cycles = 0;
10520d9764f6SJosef Bacik 	u64 last_tickets_id;
10530d9764f6SJosef Bacik 
10540d9764f6SJosef Bacik 	fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
10550d9764f6SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
10560d9764f6SJosef Bacik 
10570d9764f6SJosef Bacik 	spin_lock(&space_info->lock);
10589f246926SJosef Bacik 	to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info);
10590d9764f6SJosef Bacik 	if (!to_reclaim) {
10600d9764f6SJosef Bacik 		space_info->flush = 0;
10610d9764f6SJosef Bacik 		spin_unlock(&space_info->lock);
10620d9764f6SJosef Bacik 		return;
10630d9764f6SJosef Bacik 	}
10640d9764f6SJosef Bacik 	last_tickets_id = space_info->tickets_id;
10650d9764f6SJosef Bacik 	spin_unlock(&space_info->lock);
10660d9764f6SJosef Bacik 
10670d9764f6SJosef Bacik 	flush_state = FLUSH_DELAYED_ITEMS_NR;
10680d9764f6SJosef Bacik 	do {
10694b02b00fSJosef Bacik 		flush_space(fs_info, space_info, to_reclaim, flush_state, false);
10700d9764f6SJosef Bacik 		spin_lock(&space_info->lock);
10710d9764f6SJosef Bacik 		if (list_empty(&space_info->tickets)) {
10720d9764f6SJosef Bacik 			space_info->flush = 0;
10730d9764f6SJosef Bacik 			spin_unlock(&space_info->lock);
10740d9764f6SJosef Bacik 			return;
10750d9764f6SJosef Bacik 		}
10760d9764f6SJosef Bacik 		to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
10779f246926SJosef Bacik 							      space_info);
10780d9764f6SJosef Bacik 		if (last_tickets_id == space_info->tickets_id) {
10790d9764f6SJosef Bacik 			flush_state++;
10800d9764f6SJosef Bacik 		} else {
10810d9764f6SJosef Bacik 			last_tickets_id = space_info->tickets_id;
10820d9764f6SJosef Bacik 			flush_state = FLUSH_DELAYED_ITEMS_NR;
10830d9764f6SJosef Bacik 			if (commit_cycles)
10840d9764f6SJosef Bacik 				commit_cycles--;
10850d9764f6SJosef Bacik 		}
10860d9764f6SJosef Bacik 
10870d9764f6SJosef Bacik 		/*
108803fe78ccSJosef Bacik 		 * We do not want to empty the system of delalloc unless we're
108903fe78ccSJosef Bacik 		 * under heavy pressure, so allow one trip through the flushing
109003fe78ccSJosef Bacik 		 * logic before we start doing a FLUSH_DELALLOC_FULL.
109103fe78ccSJosef Bacik 		 */
109203fe78ccSJosef Bacik 		if (flush_state == FLUSH_DELALLOC_FULL && !commit_cycles)
109303fe78ccSJosef Bacik 			flush_state++;
109403fe78ccSJosef Bacik 
109503fe78ccSJosef Bacik 		/*
10960d9764f6SJosef Bacik 		 * We don't want to force a chunk allocation until we've tried
10970d9764f6SJosef Bacik 		 * pretty hard to reclaim space.  Think of the case where we
10980d9764f6SJosef Bacik 		 * freed up a bunch of space and so have a lot of pinned space
10990d9764f6SJosef Bacik 		 * to reclaim.  We would rather use that than possibly create a
11000d9764f6SJosef Bacik 		 * underutilized metadata chunk.  So if this is our first run
11010d9764f6SJosef Bacik 		 * through the flushing state machine skip ALLOC_CHUNK_FORCE and
11020d9764f6SJosef Bacik 		 * commit the transaction.  If nothing has changed the next go
11030d9764f6SJosef Bacik 		 * around then we can force a chunk allocation.
11040d9764f6SJosef Bacik 		 */
11050d9764f6SJosef Bacik 		if (flush_state == ALLOC_CHUNK_FORCE && !commit_cycles)
11060d9764f6SJosef Bacik 			flush_state++;
11070d9764f6SJosef Bacik 
11080d9764f6SJosef Bacik 		if (flush_state > COMMIT_TRANS) {
11090d9764f6SJosef Bacik 			commit_cycles++;
11100d9764f6SJosef Bacik 			if (commit_cycles > 2) {
11112341ccd1SJosef Bacik 				if (maybe_fail_all_tickets(fs_info, space_info)) {
11120d9764f6SJosef Bacik 					flush_state = FLUSH_DELAYED_ITEMS_NR;
11130d9764f6SJosef Bacik 					commit_cycles--;
11140d9764f6SJosef Bacik 				} else {
11150d9764f6SJosef Bacik 					space_info->flush = 0;
11160d9764f6SJosef Bacik 				}
11170d9764f6SJosef Bacik 			} else {
11180d9764f6SJosef Bacik 				flush_state = FLUSH_DELAYED_ITEMS_NR;
11190d9764f6SJosef Bacik 			}
11200d9764f6SJosef Bacik 		}
11210d9764f6SJosef Bacik 		spin_unlock(&space_info->lock);
11220d9764f6SJosef Bacik 	} while (flush_state <= COMMIT_TRANS);
11230d9764f6SJosef Bacik }
11240d9764f6SJosef Bacik 
11251a7a92c8SJosef Bacik /*
1126576fa348SJosef Bacik  * This handles pre-flushing of metadata space before we get to the point that
1127576fa348SJosef Bacik  * we need to start blocking threads on tickets.  The logic here is different
1128576fa348SJosef Bacik  * from the other flush paths because it doesn't rely on tickets to tell us how
1129576fa348SJosef Bacik  * much we need to flush, instead it attempts to keep us below the 80% full
1130576fa348SJosef Bacik  * watermark of space by flushing whichever reservation pool is currently the
1131576fa348SJosef Bacik  * largest.
1132576fa348SJosef Bacik  */
btrfs_preempt_reclaim_metadata_space(struct work_struct * work)1133576fa348SJosef Bacik static void btrfs_preempt_reclaim_metadata_space(struct work_struct *work)
1134576fa348SJosef Bacik {
1135576fa348SJosef Bacik 	struct btrfs_fs_info *fs_info;
1136576fa348SJosef Bacik 	struct btrfs_space_info *space_info;
1137576fa348SJosef Bacik 	struct btrfs_block_rsv *delayed_block_rsv;
1138576fa348SJosef Bacik 	struct btrfs_block_rsv *delayed_refs_rsv;
1139576fa348SJosef Bacik 	struct btrfs_block_rsv *global_rsv;
1140576fa348SJosef Bacik 	struct btrfs_block_rsv *trans_rsv;
114188a777a6SJosef Bacik 	int loops = 0;
1142576fa348SJosef Bacik 
1143576fa348SJosef Bacik 	fs_info = container_of(work, struct btrfs_fs_info,
1144576fa348SJosef Bacik 			       preempt_reclaim_work);
1145576fa348SJosef Bacik 	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
1146576fa348SJosef Bacik 	delayed_block_rsv = &fs_info->delayed_block_rsv;
1147576fa348SJosef Bacik 	delayed_refs_rsv = &fs_info->delayed_refs_rsv;
1148576fa348SJosef Bacik 	global_rsv = &fs_info->global_block_rsv;
1149576fa348SJosef Bacik 	trans_rsv = &fs_info->trans_block_rsv;
1150576fa348SJosef Bacik 
1151576fa348SJosef Bacik 	spin_lock(&space_info->lock);
11522e294c60SJosef Bacik 	while (need_preemptive_reclaim(fs_info, space_info)) {
1153576fa348SJosef Bacik 		enum btrfs_flush_state flush;
1154576fa348SJosef Bacik 		u64 delalloc_size = 0;
1155576fa348SJosef Bacik 		u64 to_reclaim, block_rsv_size;
1156*82220b18SFilipe Manana 		const u64 global_rsv_size = btrfs_block_rsv_reserved(global_rsv);
1157576fa348SJosef Bacik 
115888a777a6SJosef Bacik 		loops++;
115988a777a6SJosef Bacik 
1160576fa348SJosef Bacik 		/*
1161576fa348SJosef Bacik 		 * We don't have a precise counter for the metadata being
1162576fa348SJosef Bacik 		 * reserved for delalloc, so we'll approximate it by subtracting
1163576fa348SJosef Bacik 		 * out the block rsv's space from the bytes_may_use.  If that
1164576fa348SJosef Bacik 		 * amount is higher than the individual reserves, then we can
1165576fa348SJosef Bacik 		 * assume it's tied up in delalloc reservations.
1166576fa348SJosef Bacik 		 */
1167576fa348SJosef Bacik 		block_rsv_size = global_rsv_size +
1168*82220b18SFilipe Manana 			btrfs_block_rsv_reserved(delayed_block_rsv) +
1169*82220b18SFilipe Manana 			btrfs_block_rsv_reserved(delayed_refs_rsv) +
1170*82220b18SFilipe Manana 			btrfs_block_rsv_reserved(trans_rsv);
1171576fa348SJosef Bacik 		if (block_rsv_size < space_info->bytes_may_use)
1172576fa348SJosef Bacik 			delalloc_size = space_info->bytes_may_use - block_rsv_size;
1173576fa348SJosef Bacik 
1174576fa348SJosef Bacik 		/*
1175576fa348SJosef Bacik 		 * We don't want to include the global_rsv in our calculation,
1176576fa348SJosef Bacik 		 * because that's space we can't touch.  Subtract it from the
1177576fa348SJosef Bacik 		 * block_rsv_size for the next checks.
1178576fa348SJosef Bacik 		 */
1179576fa348SJosef Bacik 		block_rsv_size -= global_rsv_size;
1180576fa348SJosef Bacik 
1181576fa348SJosef Bacik 		/*
1182576fa348SJosef Bacik 		 * We really want to avoid flushing delalloc too much, as it
1183576fa348SJosef Bacik 		 * could result in poor allocation patterns, so only flush it if
1184576fa348SJosef Bacik 		 * it's larger than the rest of the pools combined.
1185576fa348SJosef Bacik 		 */
1186576fa348SJosef Bacik 		if (delalloc_size > block_rsv_size) {
1187576fa348SJosef Bacik 			to_reclaim = delalloc_size;
1188576fa348SJosef Bacik 			flush = FLUSH_DELALLOC;
1189576fa348SJosef Bacik 		} else if (space_info->bytes_pinned >
1190*82220b18SFilipe Manana 			   (btrfs_block_rsv_reserved(delayed_block_rsv) +
1191*82220b18SFilipe Manana 			    btrfs_block_rsv_reserved(delayed_refs_rsv))) {
1192576fa348SJosef Bacik 			to_reclaim = space_info->bytes_pinned;
1193c416a30cSJosef Bacik 			flush = COMMIT_TRANS;
1194*82220b18SFilipe Manana 		} else if (btrfs_block_rsv_reserved(delayed_block_rsv) >
1195*82220b18SFilipe Manana 			   btrfs_block_rsv_reserved(delayed_refs_rsv)) {
1196*82220b18SFilipe Manana 			to_reclaim = btrfs_block_rsv_reserved(delayed_block_rsv);
1197576fa348SJosef Bacik 			flush = FLUSH_DELAYED_ITEMS_NR;
1198576fa348SJosef Bacik 		} else {
1199*82220b18SFilipe Manana 			to_reclaim = btrfs_block_rsv_reserved(delayed_refs_rsv);
1200576fa348SJosef Bacik 			flush = FLUSH_DELAYED_REFS_NR;
1201576fa348SJosef Bacik 		}
1202576fa348SJosef Bacik 
120306bae876SNiels Dossche 		spin_unlock(&space_info->lock);
120406bae876SNiels Dossche 
1205576fa348SJosef Bacik 		/*
1206576fa348SJosef Bacik 		 * We don't want to reclaim everything, just a portion, so scale
1207576fa348SJosef Bacik 		 * down the to_reclaim by 1/4.  If it takes us down to 0,
1208576fa348SJosef Bacik 		 * reclaim 1 items worth.
1209576fa348SJosef Bacik 		 */
1210576fa348SJosef Bacik 		to_reclaim >>= 2;
1211576fa348SJosef Bacik 		if (!to_reclaim)
1212576fa348SJosef Bacik 			to_reclaim = btrfs_calc_insert_metadata_size(fs_info, 1);
12134b02b00fSJosef Bacik 		flush_space(fs_info, space_info, to_reclaim, flush, true);
1214576fa348SJosef Bacik 		cond_resched();
1215576fa348SJosef Bacik 		spin_lock(&space_info->lock);
1216576fa348SJosef Bacik 	}
121788a777a6SJosef Bacik 
121888a777a6SJosef Bacik 	/* We only went through once, back off our clamping. */
121988a777a6SJosef Bacik 	if (loops == 1 && !space_info->reclaim_size)
122088a777a6SJosef Bacik 		space_info->clamp = max(1, space_info->clamp - 1);
1221e5ad49e2SJosef Bacik 	trace_btrfs_done_preemptive_reclaim(fs_info, space_info);
1222576fa348SJosef Bacik 	spin_unlock(&space_info->lock);
1223576fa348SJosef Bacik }
1224576fa348SJosef Bacik 
1225576fa348SJosef Bacik /*
12261a7a92c8SJosef Bacik  * FLUSH_DELALLOC_WAIT:
12271a7a92c8SJosef Bacik  *   Space is freed from flushing delalloc in one of two ways.
12281a7a92c8SJosef Bacik  *
12291a7a92c8SJosef Bacik  *   1) compression is on and we allocate less space than we reserved
12301a7a92c8SJosef Bacik  *   2) we are overwriting existing space
12311a7a92c8SJosef Bacik  *
12321a7a92c8SJosef Bacik  *   For #1 that extra space is reclaimed as soon as the delalloc pages are
12331a7a92c8SJosef Bacik  *   COWed, by way of btrfs_add_reserved_bytes() which adds the actual extent
12341a7a92c8SJosef Bacik  *   length to ->bytes_reserved, and subtracts the reserved space from
12351a7a92c8SJosef Bacik  *   ->bytes_may_use.
12361a7a92c8SJosef Bacik  *
12371a7a92c8SJosef Bacik  *   For #2 this is trickier.  Once the ordered extent runs we will drop the
12381a7a92c8SJosef Bacik  *   extent in the range we are overwriting, which creates a delayed ref for
12391a7a92c8SJosef Bacik  *   that freed extent.  This however is not reclaimed until the transaction
12401a7a92c8SJosef Bacik  *   commits, thus the next stages.
12411a7a92c8SJosef Bacik  *
12421a7a92c8SJosef Bacik  * RUN_DELAYED_IPUTS
12431a7a92c8SJosef Bacik  *   If we are freeing inodes, we want to make sure all delayed iputs have
12441a7a92c8SJosef Bacik  *   completed, because they could have been on an inode with i_nlink == 0, and
12451a7a92c8SJosef Bacik  *   thus have been truncated and freed up space.  But again this space is not
12461a7a92c8SJosef Bacik  *   immediately re-usable, it comes in the form of a delayed ref, which must be
12471a7a92c8SJosef Bacik  *   run and then the transaction must be committed.
12481a7a92c8SJosef Bacik  *
12491a7a92c8SJosef Bacik  * COMMIT_TRANS
1250c416a30cSJosef Bacik  *   This is where we reclaim all of the pinned space generated by running the
1251c416a30cSJosef Bacik  *   iputs
1252c4923027SJosef Bacik  *
1253c4923027SJosef Bacik  * ALLOC_CHUNK_FORCE
1254c4923027SJosef Bacik  *   For data we start with alloc chunk force, however we could have been full
1255c4923027SJosef Bacik  *   before, and then the transaction commit could have freed new block groups,
1256c4923027SJosef Bacik  *   so if we now have space to allocate do the force chunk allocation.
12571a7a92c8SJosef Bacik  */
125857056740SJosef Bacik static const enum btrfs_flush_state data_flush_states[] = {
125903fe78ccSJosef Bacik 	FLUSH_DELALLOC_FULL,
126057056740SJosef Bacik 	RUN_DELAYED_IPUTS,
126157056740SJosef Bacik 	COMMIT_TRANS,
1262c4923027SJosef Bacik 	ALLOC_CHUNK_FORCE,
126357056740SJosef Bacik };
126457056740SJosef Bacik 
btrfs_async_reclaim_data_space(struct work_struct * work)126557056740SJosef Bacik static void btrfs_async_reclaim_data_space(struct work_struct *work)
12660d9764f6SJosef Bacik {
126757056740SJosef Bacik 	struct btrfs_fs_info *fs_info;
126857056740SJosef Bacik 	struct btrfs_space_info *space_info;
126957056740SJosef Bacik 	u64 last_tickets_id;
127091e79a83SJosef Bacik 	enum btrfs_flush_state flush_state = 0;
127157056740SJosef Bacik 
127257056740SJosef Bacik 	fs_info = container_of(work, struct btrfs_fs_info, async_data_reclaim_work);
127357056740SJosef Bacik 	space_info = fs_info->data_sinfo;
127457056740SJosef Bacik 
127557056740SJosef Bacik 	spin_lock(&space_info->lock);
127657056740SJosef Bacik 	if (list_empty(&space_info->tickets)) {
127757056740SJosef Bacik 		space_info->flush = 0;
127857056740SJosef Bacik 		spin_unlock(&space_info->lock);
127957056740SJosef Bacik 		return;
128057056740SJosef Bacik 	}
128157056740SJosef Bacik 	last_tickets_id = space_info->tickets_id;
128257056740SJosef Bacik 	spin_unlock(&space_info->lock);
128357056740SJosef Bacik 
128457056740SJosef Bacik 	while (!space_info->full) {
12854b02b00fSJosef Bacik 		flush_space(fs_info, space_info, U64_MAX, ALLOC_CHUNK_FORCE, false);
128657056740SJosef Bacik 		spin_lock(&space_info->lock);
128757056740SJosef Bacik 		if (list_empty(&space_info->tickets)) {
128857056740SJosef Bacik 			space_info->flush = 0;
128957056740SJosef Bacik 			spin_unlock(&space_info->lock);
129057056740SJosef Bacik 			return;
129157056740SJosef Bacik 		}
12920e24f6d8SJosef Bacik 
12930e24f6d8SJosef Bacik 		/* Something happened, fail everything and bail. */
12940e24f6d8SJosef Bacik 		if (BTRFS_FS_ERROR(fs_info))
12950e24f6d8SJosef Bacik 			goto aborted_fs;
129657056740SJosef Bacik 		last_tickets_id = space_info->tickets_id;
129757056740SJosef Bacik 		spin_unlock(&space_info->lock);
129857056740SJosef Bacik 	}
129957056740SJosef Bacik 
130057056740SJosef Bacik 	while (flush_state < ARRAY_SIZE(data_flush_states)) {
130157056740SJosef Bacik 		flush_space(fs_info, space_info, U64_MAX,
13024b02b00fSJosef Bacik 			    data_flush_states[flush_state], false);
130357056740SJosef Bacik 		spin_lock(&space_info->lock);
130457056740SJosef Bacik 		if (list_empty(&space_info->tickets)) {
130557056740SJosef Bacik 			space_info->flush = 0;
130657056740SJosef Bacik 			spin_unlock(&space_info->lock);
130757056740SJosef Bacik 			return;
130857056740SJosef Bacik 		}
130957056740SJosef Bacik 
131057056740SJosef Bacik 		if (last_tickets_id == space_info->tickets_id) {
131157056740SJosef Bacik 			flush_state++;
131257056740SJosef Bacik 		} else {
131357056740SJosef Bacik 			last_tickets_id = space_info->tickets_id;
131457056740SJosef Bacik 			flush_state = 0;
131557056740SJosef Bacik 		}
131657056740SJosef Bacik 
131757056740SJosef Bacik 		if (flush_state >= ARRAY_SIZE(data_flush_states)) {
131857056740SJosef Bacik 			if (space_info->full) {
131957056740SJosef Bacik 				if (maybe_fail_all_tickets(fs_info, space_info))
132057056740SJosef Bacik 					flush_state = 0;
132157056740SJosef Bacik 				else
132257056740SJosef Bacik 					space_info->flush = 0;
132357056740SJosef Bacik 			} else {
132457056740SJosef Bacik 				flush_state = 0;
132557056740SJosef Bacik 			}
13260e24f6d8SJosef Bacik 
13270e24f6d8SJosef Bacik 			/* Something happened, fail everything and bail. */
13280e24f6d8SJosef Bacik 			if (BTRFS_FS_ERROR(fs_info))
13290e24f6d8SJosef Bacik 				goto aborted_fs;
13300e24f6d8SJosef Bacik 
133157056740SJosef Bacik 		}
133257056740SJosef Bacik 		spin_unlock(&space_info->lock);
133357056740SJosef Bacik 	}
13340e24f6d8SJosef Bacik 	return;
13350e24f6d8SJosef Bacik 
13360e24f6d8SJosef Bacik aborted_fs:
13370e24f6d8SJosef Bacik 	maybe_fail_all_tickets(fs_info, space_info);
13380e24f6d8SJosef Bacik 	space_info->flush = 0;
13390e24f6d8SJosef Bacik 	spin_unlock(&space_info->lock);
134057056740SJosef Bacik }
134157056740SJosef Bacik 
btrfs_init_async_reclaim_work(struct btrfs_fs_info * fs_info)134257056740SJosef Bacik void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info)
134357056740SJosef Bacik {
134457056740SJosef Bacik 	INIT_WORK(&fs_info->async_reclaim_work, btrfs_async_reclaim_metadata_space);
134557056740SJosef Bacik 	INIT_WORK(&fs_info->async_data_reclaim_work, btrfs_async_reclaim_data_space);
1346576fa348SJosef Bacik 	INIT_WORK(&fs_info->preempt_reclaim_work,
1347576fa348SJosef Bacik 		  btrfs_preempt_reclaim_metadata_space);
13480d9764f6SJosef Bacik }
13490d9764f6SJosef Bacik 
13500d9764f6SJosef Bacik static const enum btrfs_flush_state priority_flush_states[] = {
13510d9764f6SJosef Bacik 	FLUSH_DELAYED_ITEMS_NR,
13520d9764f6SJosef Bacik 	FLUSH_DELAYED_ITEMS,
13530d9764f6SJosef Bacik 	ALLOC_CHUNK,
13540d9764f6SJosef Bacik };
13550d9764f6SJosef Bacik 
1356d3984c90SJosef Bacik static const enum btrfs_flush_state evict_flush_states[] = {
1357d3984c90SJosef Bacik 	FLUSH_DELAYED_ITEMS_NR,
1358d3984c90SJosef Bacik 	FLUSH_DELAYED_ITEMS,
1359d3984c90SJosef Bacik 	FLUSH_DELAYED_REFS_NR,
1360d3984c90SJosef Bacik 	FLUSH_DELAYED_REFS,
1361d3984c90SJosef Bacik 	FLUSH_DELALLOC,
1362d3984c90SJosef Bacik 	FLUSH_DELALLOC_WAIT,
136303fe78ccSJosef Bacik 	FLUSH_DELALLOC_FULL,
1364d3984c90SJosef Bacik 	ALLOC_CHUNK,
1365d3984c90SJosef Bacik 	COMMIT_TRANS,
1366d3984c90SJosef Bacik };
1367d3984c90SJosef 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)13680d9764f6SJosef Bacik static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
13690d9764f6SJosef Bacik 				struct btrfs_space_info *space_info,
13709ce2f423SJosef Bacik 				struct reserve_ticket *ticket,
13719ce2f423SJosef Bacik 				const enum btrfs_flush_state *states,
13729ce2f423SJosef Bacik 				int states_nr)
13730d9764f6SJosef Bacik {
13740d9764f6SJosef Bacik 	u64 to_reclaim;
13759f35f76dSJosef Bacik 	int flush_state = 0;
13760d9764f6SJosef Bacik 
13770d9764f6SJosef Bacik 	spin_lock(&space_info->lock);
13789f246926SJosef Bacik 	to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info);
13799cd8dcdcSJosef Bacik 	/*
13809cd8dcdcSJosef Bacik 	 * This is the priority reclaim path, so to_reclaim could be >0 still
1381143823cfSDavid Sterba 	 * because we may have only satisfied the priority tickets and still
13829cd8dcdcSJosef Bacik 	 * left non priority tickets on the list.  We would then have
13839cd8dcdcSJosef Bacik 	 * to_reclaim but ->bytes == 0.
13849cd8dcdcSJosef Bacik 	 */
13859cd8dcdcSJosef Bacik 	if (ticket->bytes == 0) {
13860d9764f6SJosef Bacik 		spin_unlock(&space_info->lock);
13870d9764f6SJosef Bacik 		return;
13880d9764f6SJosef Bacik 	}
13890d9764f6SJosef Bacik 
13909f35f76dSJosef Bacik 	while (flush_state < states_nr) {
13919f35f76dSJosef Bacik 		spin_unlock(&space_info->lock);
13924b02b00fSJosef Bacik 		flush_space(fs_info, space_info, to_reclaim, states[flush_state],
13934b02b00fSJosef Bacik 			    false);
13940d9764f6SJosef Bacik 		flush_state++;
13950d9764f6SJosef Bacik 		spin_lock(&space_info->lock);
13960d9764f6SJosef Bacik 		if (ticket->bytes == 0) {
13970d9764f6SJosef Bacik 			spin_unlock(&space_info->lock);
13980d9764f6SJosef Bacik 			return;
13990d9764f6SJosef Bacik 		}
14009f35f76dSJosef Bacik 	}
14019f35f76dSJosef Bacik 
14021b6948acSFilipe Manana 	/*
14031b6948acSFilipe Manana 	 * Attempt to steal from the global rsv if we can, except if the fs was
14041b6948acSFilipe Manana 	 * turned into error mode due to a transaction abort when flushing space
14057e3bfd14SFilipe Manana 	 * above, in that case fail with the abort error instead of returning
14067e3bfd14SFilipe Manana 	 * success to the caller if we can steal from the global rsv - this is
14077e3bfd14SFilipe Manana 	 * just to have caller fail immeditelly instead of later when trying to
14087e3bfd14SFilipe Manana 	 * modify the fs, making it easier to debug -ENOSPC problems.
14091b6948acSFilipe Manana 	 */
14101b6948acSFilipe Manana 	if (BTRFS_FS_ERROR(fs_info)) {
14117e3bfd14SFilipe Manana 		ticket->error = BTRFS_FS_ERROR(fs_info);
14121b6948acSFilipe Manana 		remove_ticket(space_info, ticket);
14131b6948acSFilipe Manana 	} else if (!steal_from_global_rsv(fs_info, space_info, ticket)) {
1414ee6adbfdSJosef Bacik 		ticket->error = -ENOSPC;
1415ee6adbfdSJosef Bacik 		remove_ticket(space_info, ticket);
1416ee6adbfdSJosef Bacik 	}
1417ee6adbfdSJosef Bacik 
14189f35f76dSJosef Bacik 	/*
14199f35f76dSJosef Bacik 	 * We must run try_granting_tickets here because we could be a large
14209f35f76dSJosef Bacik 	 * ticket in front of a smaller ticket that can now be satisfied with
14219f35f76dSJosef Bacik 	 * the available space.
14229f35f76dSJosef Bacik 	 */
14239f35f76dSJosef Bacik 	btrfs_try_granting_tickets(fs_info, space_info);
14240d9764f6SJosef Bacik 	spin_unlock(&space_info->lock);
14250d9764f6SJosef Bacik }
14260d9764f6SJosef Bacik 
priority_reclaim_data_space(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,struct reserve_ticket * ticket)14271004f686SJosef Bacik static void priority_reclaim_data_space(struct btrfs_fs_info *fs_info,
14281004f686SJosef Bacik 					struct btrfs_space_info *space_info,
142957056740SJosef Bacik 					struct reserve_ticket *ticket)
14301004f686SJosef Bacik {
14319f35f76dSJosef Bacik 	spin_lock(&space_info->lock);
14329cd8dcdcSJosef Bacik 
14339cd8dcdcSJosef Bacik 	/* We could have been granted before we got here. */
14349cd8dcdcSJosef Bacik 	if (ticket->bytes == 0) {
14359cd8dcdcSJosef Bacik 		spin_unlock(&space_info->lock);
14369cd8dcdcSJosef Bacik 		return;
14379cd8dcdcSJosef Bacik 	}
14389cd8dcdcSJosef Bacik 
14391004f686SJosef Bacik 	while (!space_info->full) {
14409f35f76dSJosef Bacik 		spin_unlock(&space_info->lock);
14414b02b00fSJosef Bacik 		flush_space(fs_info, space_info, U64_MAX, ALLOC_CHUNK_FORCE, false);
14421004f686SJosef Bacik 		spin_lock(&space_info->lock);
14431004f686SJosef Bacik 		if (ticket->bytes == 0) {
14441004f686SJosef Bacik 			spin_unlock(&space_info->lock);
14451004f686SJosef Bacik 			return;
14461004f686SJosef Bacik 		}
14471004f686SJosef Bacik 	}
14489f35f76dSJosef Bacik 
14499f35f76dSJosef Bacik 	ticket->error = -ENOSPC;
14509f35f76dSJosef Bacik 	remove_ticket(space_info, ticket);
14519f35f76dSJosef Bacik 	btrfs_try_granting_tickets(fs_info, space_info);
14529f35f76dSJosef Bacik 	spin_unlock(&space_info->lock);
14531004f686SJosef Bacik }
14541004f686SJosef Bacik 
wait_reserve_ticket(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,struct reserve_ticket * ticket)1455374bf9c5SJosef Bacik static void wait_reserve_ticket(struct btrfs_fs_info *fs_info,
14560d9764f6SJosef Bacik 				struct btrfs_space_info *space_info,
14570d9764f6SJosef Bacik 				struct reserve_ticket *ticket)
14580d9764f6SJosef Bacik 
14590d9764f6SJosef Bacik {
14600d9764f6SJosef Bacik 	DEFINE_WAIT(wait);
14610d9764f6SJosef Bacik 	int ret = 0;
14620d9764f6SJosef Bacik 
14630d9764f6SJosef Bacik 	spin_lock(&space_info->lock);
14640d9764f6SJosef Bacik 	while (ticket->bytes > 0 && ticket->error == 0) {
14650d9764f6SJosef Bacik 		ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
14660d9764f6SJosef Bacik 		if (ret) {
14670cab7accSFilipe Manana 			/*
14680cab7accSFilipe Manana 			 * Delete us from the list. After we unlock the space
14690cab7accSFilipe Manana 			 * info, we don't want the async reclaim job to reserve
14700cab7accSFilipe Manana 			 * space for this ticket. If that would happen, then the
14710cab7accSFilipe Manana 			 * ticket's task would not known that space was reserved
14720cab7accSFilipe Manana 			 * despite getting an error, resulting in a space leak
14730cab7accSFilipe Manana 			 * (bytes_may_use counter of our space_info).
14740cab7accSFilipe Manana 			 */
1475d611add4SFilipe Manana 			remove_ticket(space_info, ticket);
1476374bf9c5SJosef Bacik 			ticket->error = -EINTR;
14770d9764f6SJosef Bacik 			break;
14780d9764f6SJosef Bacik 		}
14790d9764f6SJosef Bacik 		spin_unlock(&space_info->lock);
14800d9764f6SJosef Bacik 
14810d9764f6SJosef Bacik 		schedule();
14820d9764f6SJosef Bacik 
14830d9764f6SJosef Bacik 		finish_wait(&ticket->wait, &wait);
14840d9764f6SJosef Bacik 		spin_lock(&space_info->lock);
14850d9764f6SJosef Bacik 	}
14860d9764f6SJosef Bacik 	spin_unlock(&space_info->lock);
14870d9764f6SJosef Bacik }
14880d9764f6SJosef Bacik 
148943dd529aSDavid Sterba /*
149043dd529aSDavid Sterba  * Do the appropriate flushing and waiting for a ticket.
1491d98b188eSNikolay Borisov  *
1492d98b188eSNikolay Borisov  * @fs_info:    the filesystem
1493d98b188eSNikolay Borisov  * @space_info: space info for the reservation
1494d98b188eSNikolay Borisov  * @ticket:     ticket for the reservation
1495ac1ea10eSJosef Bacik  * @start_ns:   timestamp when the reservation started
1496ac1ea10eSJosef Bacik  * @orig_bytes: amount of bytes originally reserved
1497d98b188eSNikolay Borisov  * @flush:      how much we can flush
149803235279SJosef Bacik  *
149903235279SJosef Bacik  * This does the work of figuring out how to flush for the ticket, waiting for
150003235279SJosef Bacik  * the reservation, and returning the appropriate error if there is one.
150103235279SJosef 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)150203235279SJosef Bacik static int handle_reserve_ticket(struct btrfs_fs_info *fs_info,
150303235279SJosef Bacik 				 struct btrfs_space_info *space_info,
150403235279SJosef Bacik 				 struct reserve_ticket *ticket,
1505ac1ea10eSJosef Bacik 				 u64 start_ns, u64 orig_bytes,
150603235279SJosef Bacik 				 enum btrfs_reserve_flush_enum flush)
150703235279SJosef Bacik {
150803235279SJosef Bacik 	int ret;
150903235279SJosef Bacik 
1510d3984c90SJosef Bacik 	switch (flush) {
151157056740SJosef Bacik 	case BTRFS_RESERVE_FLUSH_DATA:
1512d3984c90SJosef Bacik 	case BTRFS_RESERVE_FLUSH_ALL:
15137f9fe614SJosef Bacik 	case BTRFS_RESERVE_FLUSH_ALL_STEAL:
151403235279SJosef Bacik 		wait_reserve_ticket(fs_info, space_info, ticket);
1515d3984c90SJosef Bacik 		break;
1516d3984c90SJosef Bacik 	case BTRFS_RESERVE_FLUSH_LIMIT:
15179ce2f423SJosef Bacik 		priority_reclaim_metadata_space(fs_info, space_info, ticket,
15189ce2f423SJosef Bacik 						priority_flush_states,
15199ce2f423SJosef Bacik 						ARRAY_SIZE(priority_flush_states));
1520d3984c90SJosef Bacik 		break;
1521d3984c90SJosef Bacik 	case BTRFS_RESERVE_FLUSH_EVICT:
1522d3984c90SJosef Bacik 		priority_reclaim_metadata_space(fs_info, space_info, ticket,
1523d3984c90SJosef Bacik 						evict_flush_states,
1524d3984c90SJosef Bacik 						ARRAY_SIZE(evict_flush_states));
1525d3984c90SJosef Bacik 		break;
15261004f686SJosef Bacik 	case BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE:
152757056740SJosef Bacik 		priority_reclaim_data_space(fs_info, space_info, ticket);
15281004f686SJosef Bacik 		break;
1529d3984c90SJosef Bacik 	default:
1530d3984c90SJosef Bacik 		ASSERT(0);
1531d3984c90SJosef Bacik 		break;
1532d3984c90SJosef Bacik 	}
153303235279SJosef Bacik 
153403235279SJosef Bacik 	ret = ticket->error;
153503235279SJosef Bacik 	ASSERT(list_empty(&ticket->list));
15360cab7accSFilipe Manana 	/*
15370cab7accSFilipe Manana 	 * Check that we can't have an error set if the reservation succeeded,
15380cab7accSFilipe Manana 	 * as that would confuse tasks and lead them to error out without
15390cab7accSFilipe Manana 	 * releasing reserved space (if an error happens the expectation is that
15400cab7accSFilipe Manana 	 * space wasn't reserved at all).
15410cab7accSFilipe Manana 	 */
15420cab7accSFilipe Manana 	ASSERT(!(ticket->bytes == 0 && ticket->error));
1543ac1ea10eSJosef Bacik 	trace_btrfs_reserve_ticket(fs_info, space_info->flags, orig_bytes,
1544ac1ea10eSJosef Bacik 				   start_ns, flush, ticket->error);
154503235279SJosef Bacik 	return ret;
154603235279SJosef Bacik }
154703235279SJosef Bacik 
1548666daa9fSJosef Bacik /*
1549666daa9fSJosef Bacik  * This returns true if this flush state will go through the ordinary flushing
1550666daa9fSJosef Bacik  * code.
1551666daa9fSJosef Bacik  */
is_normal_flushing(enum btrfs_reserve_flush_enum flush)1552666daa9fSJosef Bacik static inline bool is_normal_flushing(enum btrfs_reserve_flush_enum flush)
1553666daa9fSJosef Bacik {
1554666daa9fSJosef Bacik 	return	(flush == BTRFS_RESERVE_FLUSH_ALL) ||
1555666daa9fSJosef Bacik 		(flush == BTRFS_RESERVE_FLUSH_ALL_STEAL);
1556666daa9fSJosef Bacik }
1557666daa9fSJosef Bacik 
maybe_clamp_preempt(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)155888a777a6SJosef Bacik static inline void maybe_clamp_preempt(struct btrfs_fs_info *fs_info,
155988a777a6SJosef Bacik 				       struct btrfs_space_info *space_info)
156088a777a6SJosef Bacik {
156188a777a6SJosef Bacik 	u64 ordered = percpu_counter_sum_positive(&fs_info->ordered_bytes);
156288a777a6SJosef Bacik 	u64 delalloc = percpu_counter_sum_positive(&fs_info->delalloc_bytes);
156388a777a6SJosef Bacik 
156488a777a6SJosef Bacik 	/*
156588a777a6SJosef Bacik 	 * If we're heavy on ordered operations then clamping won't help us.  We
156688a777a6SJosef Bacik 	 * need to clamp specifically to keep up with dirty'ing buffered
156788a777a6SJosef Bacik 	 * writers, because there's not a 1:1 correlation of writing delalloc
156888a777a6SJosef Bacik 	 * and freeing space, like there is with flushing delayed refs or
156988a777a6SJosef Bacik 	 * delayed nodes.  If we're already more ordered than delalloc then
157088a777a6SJosef Bacik 	 * we're keeping up, otherwise we aren't and should probably clamp.
157188a777a6SJosef Bacik 	 */
157288a777a6SJosef Bacik 	if (ordered < delalloc)
157388a777a6SJosef Bacik 		space_info->clamp = min(space_info->clamp + 1, 8);
157488a777a6SJosef Bacik }
157588a777a6SJosef Bacik 
can_steal(enum btrfs_reserve_flush_enum flush)1576ee6adbfdSJosef Bacik static inline bool can_steal(enum btrfs_reserve_flush_enum flush)
1577ee6adbfdSJosef Bacik {
1578ee6adbfdSJosef Bacik 	return (flush == BTRFS_RESERVE_FLUSH_ALL_STEAL ||
1579ee6adbfdSJosef Bacik 		flush == BTRFS_RESERVE_FLUSH_EVICT);
1580ee6adbfdSJosef Bacik }
1581ee6adbfdSJosef Bacik 
1582765c3fe9SJosef Bacik /*
1583765c3fe9SJosef Bacik  * NO_FLUSH and FLUSH_EMERGENCY don't want to create a ticket, they just want to
1584765c3fe9SJosef Bacik  * fail as quickly as possible.
1585765c3fe9SJosef Bacik  */
can_ticket(enum btrfs_reserve_flush_enum flush)1586765c3fe9SJosef Bacik static inline bool can_ticket(enum btrfs_reserve_flush_enum flush)
1587765c3fe9SJosef Bacik {
1588765c3fe9SJosef Bacik 	return (flush != BTRFS_RESERVE_NO_FLUSH &&
1589765c3fe9SJosef Bacik 		flush != BTRFS_RESERVE_FLUSH_EMERGENCY);
1590765c3fe9SJosef Bacik }
1591765c3fe9SJosef Bacik 
159243dd529aSDavid Sterba /*
159343dd529aSDavid Sterba  * Try to reserve bytes from the block_rsv's space.
1594d98b188eSNikolay Borisov  *
1595d98b188eSNikolay Borisov  * @fs_info:    the filesystem
1596d98b188eSNikolay Borisov  * @space_info: space info we want to allocate from
1597d98b188eSNikolay Borisov  * @orig_bytes: number of bytes we want
1598d98b188eSNikolay Borisov  * @flush:      whether or not we can flush to make our reservation
15990d9764f6SJosef Bacik  *
16000d9764f6SJosef Bacik  * This will reserve orig_bytes number of bytes from the space info associated
16010d9764f6SJosef Bacik  * with the block_rsv.  If there is not enough space it will make an attempt to
16020d9764f6SJosef Bacik  * flush out space to make room.  It will do this by flushing delalloc if
16030d9764f6SJosef Bacik  * possible or committing the transaction.  If flush is 0 then no attempts to
16040d9764f6SJosef Bacik  * regain reservations will be made and this will fail if there is not enough
16050d9764f6SJosef Bacik  * space already.
16060d9764f6SJosef Bacik  */
__reserve_bytes(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,u64 orig_bytes,enum btrfs_reserve_flush_enum flush)1607f3bda421SJosef Bacik static int __reserve_bytes(struct btrfs_fs_info *fs_info,
1608f3bda421SJosef Bacik 			   struct btrfs_space_info *space_info, u64 orig_bytes,
16099f246926SJosef Bacik 			   enum btrfs_reserve_flush_enum flush)
16100d9764f6SJosef Bacik {
161157056740SJosef Bacik 	struct work_struct *async_work;
16120d9764f6SJosef Bacik 	struct reserve_ticket ticket;
1613ac1ea10eSJosef Bacik 	u64 start_ns = 0;
16140d9764f6SJosef Bacik 	u64 used;
16153a49a548SFilipe Manana 	int ret = -ENOSPC;
1616ef1317a1SJosef Bacik 	bool pending_tickets;
16170d9764f6SJosef Bacik 
16180d9764f6SJosef Bacik 	ASSERT(orig_bytes);
16199d0d47d5SFilipe Manana 	/*
16209d0d47d5SFilipe Manana 	 * If have a transaction handle (current->journal_info != NULL), then
16219d0d47d5SFilipe Manana 	 * the flush method can not be neither BTRFS_RESERVE_FLUSH_ALL* nor
16229d0d47d5SFilipe Manana 	 * BTRFS_RESERVE_FLUSH_EVICT, as we could deadlock because those
16239d0d47d5SFilipe Manana 	 * flushing methods can trigger transaction commits.
16249d0d47d5SFilipe Manana 	 */
16259d0d47d5SFilipe Manana 	if (current->journal_info) {
16269d0d47d5SFilipe Manana 		/* One assert per line for easier debugging. */
16279d0d47d5SFilipe Manana 		ASSERT(flush != BTRFS_RESERVE_FLUSH_ALL);
16289d0d47d5SFilipe Manana 		ASSERT(flush != BTRFS_RESERVE_FLUSH_ALL_STEAL);
16299d0d47d5SFilipe Manana 		ASSERT(flush != BTRFS_RESERVE_FLUSH_EVICT);
16309d0d47d5SFilipe Manana 	}
16310d9764f6SJosef Bacik 
163257056740SJosef Bacik 	if (flush == BTRFS_RESERVE_FLUSH_DATA)
163357056740SJosef Bacik 		async_work = &fs_info->async_data_reclaim_work;
163457056740SJosef Bacik 	else
163557056740SJosef Bacik 		async_work = &fs_info->async_reclaim_work;
163657056740SJosef Bacik 
16370d9764f6SJosef Bacik 	spin_lock(&space_info->lock);
16380d9764f6SJosef Bacik 	used = btrfs_space_info_used(space_info, true);
1639666daa9fSJosef Bacik 
1640666daa9fSJosef Bacik 	/*
1641666daa9fSJosef Bacik 	 * We don't want NO_FLUSH allocations to jump everybody, they can
1642666daa9fSJosef Bacik 	 * generally handle ENOSPC in a different way, so treat them the same as
1643666daa9fSJosef Bacik 	 * normal flushers when it comes to skipping pending tickets.
1644666daa9fSJosef Bacik 	 */
1645666daa9fSJosef Bacik 	if (is_normal_flushing(flush) || (flush == BTRFS_RESERVE_NO_FLUSH))
1646ef1317a1SJosef Bacik 		pending_tickets = !list_empty(&space_info->tickets) ||
1647ef1317a1SJosef Bacik 			!list_empty(&space_info->priority_tickets);
1648666daa9fSJosef Bacik 	else
1649666daa9fSJosef Bacik 		pending_tickets = !list_empty(&space_info->priority_tickets);
16500d9764f6SJosef Bacik 
16510d9764f6SJosef Bacik 	/*
16529b4851bcSGoldwyn Rodrigues 	 * Carry on if we have enough space (short-circuit) OR call
16539b4851bcSGoldwyn Rodrigues 	 * can_overcommit() to ensure we can overcommit to continue.
16540d9764f6SJosef Bacik 	 */
1655ef1317a1SJosef Bacik 	if (!pending_tickets &&
1656e15acc25SNaohiro Aota 	    ((used + orig_bytes <= space_info->total_bytes) ||
1657a30a3d20SJosef Bacik 	     btrfs_can_overcommit(fs_info, space_info, orig_bytes, flush))) {
16580d9764f6SJosef Bacik 		btrfs_space_info_update_bytes_may_use(fs_info, space_info,
16590d9764f6SJosef Bacik 						      orig_bytes);
16600d9764f6SJosef Bacik 		ret = 0;
16610d9764f6SJosef Bacik 	}
16620d9764f6SJosef Bacik 
16630d9764f6SJosef Bacik 	/*
1664765c3fe9SJosef Bacik 	 * Things are dire, we need to make a reservation so we don't abort.  We
1665765c3fe9SJosef Bacik 	 * will let this reservation go through as long as we have actual space
1666765c3fe9SJosef Bacik 	 * left to allocate for the block.
1667765c3fe9SJosef Bacik 	 */
1668765c3fe9SJosef Bacik 	if (ret && unlikely(flush == BTRFS_RESERVE_FLUSH_EMERGENCY)) {
1669765c3fe9SJosef Bacik 		used = btrfs_space_info_used(space_info, false);
1670e15acc25SNaohiro Aota 		if (used + orig_bytes <= space_info->total_bytes) {
1671765c3fe9SJosef Bacik 			btrfs_space_info_update_bytes_may_use(fs_info, space_info,
1672765c3fe9SJosef Bacik 							      orig_bytes);
1673765c3fe9SJosef Bacik 			ret = 0;
1674765c3fe9SJosef Bacik 		}
1675765c3fe9SJosef Bacik 	}
1676765c3fe9SJosef Bacik 
1677765c3fe9SJosef Bacik 	/*
16780d9764f6SJosef Bacik 	 * If we couldn't make a reservation then setup our reservation ticket
16790d9764f6SJosef Bacik 	 * and kick the async worker if it's not already running.
16800d9764f6SJosef Bacik 	 *
16810d9764f6SJosef Bacik 	 * If we are a priority flusher then we just need to add our ticket to
16820d9764f6SJosef Bacik 	 * the list and we will do our own flushing further down.
16830d9764f6SJosef Bacik 	 */
1684765c3fe9SJosef Bacik 	if (ret && can_ticket(flush)) {
16850d9764f6SJosef Bacik 		ticket.bytes = orig_bytes;
16860d9764f6SJosef Bacik 		ticket.error = 0;
1687db161806SNikolay Borisov 		space_info->reclaim_size += ticket.bytes;
16880d9764f6SJosef Bacik 		init_waitqueue_head(&ticket.wait);
1689ee6adbfdSJosef Bacik 		ticket.steal = can_steal(flush);
1690ac1ea10eSJosef Bacik 		if (trace_btrfs_reserve_ticket_enabled())
1691ac1ea10eSJosef Bacik 			start_ns = ktime_get_ns();
1692ac1ea10eSJosef Bacik 
16937f9fe614SJosef Bacik 		if (flush == BTRFS_RESERVE_FLUSH_ALL ||
169457056740SJosef Bacik 		    flush == BTRFS_RESERVE_FLUSH_ALL_STEAL ||
169557056740SJosef Bacik 		    flush == BTRFS_RESERVE_FLUSH_DATA) {
16960d9764f6SJosef Bacik 			list_add_tail(&ticket.list, &space_info->tickets);
16970d9764f6SJosef Bacik 			if (!space_info->flush) {
16980aae4ca9SJosef Bacik 				/*
16990aae4ca9SJosef Bacik 				 * We were forced to add a reserve ticket, so
17000aae4ca9SJosef Bacik 				 * our preemptive flushing is unable to keep
17010aae4ca9SJosef Bacik 				 * up.  Clamp down on the threshold for the
17020aae4ca9SJosef Bacik 				 * preemptive flushing in order to keep up with
17030aae4ca9SJosef Bacik 				 * the workload.
17040aae4ca9SJosef Bacik 				 */
17050aae4ca9SJosef Bacik 				maybe_clamp_preempt(fs_info, space_info);
17060aae4ca9SJosef Bacik 
17070d9764f6SJosef Bacik 				space_info->flush = 1;
17080d9764f6SJosef Bacik 				trace_btrfs_trigger_flush(fs_info,
17090d9764f6SJosef Bacik 							  space_info->flags,
17100d9764f6SJosef Bacik 							  orig_bytes, flush,
17110d9764f6SJosef Bacik 							  "enospc");
171257056740SJosef Bacik 				queue_work(system_unbound_wq, async_work);
17130d9764f6SJosef Bacik 			}
17140d9764f6SJosef Bacik 		} else {
17150d9764f6SJosef Bacik 			list_add_tail(&ticket.list,
17160d9764f6SJosef Bacik 				      &space_info->priority_tickets);
17170d9764f6SJosef Bacik 		}
17180d9764f6SJosef Bacik 	} else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
17190d9764f6SJosef Bacik 		/*
17200d9764f6SJosef Bacik 		 * We will do the space reservation dance during log replay,
17210d9764f6SJosef Bacik 		 * which means we won't have fs_info->fs_root set, so don't do
17220d9764f6SJosef Bacik 		 * the async reclaim as we will panic.
17230d9764f6SJosef Bacik 		 */
17240d9764f6SJosef Bacik 		if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
1725ed738ba7SJosef Bacik 		    !work_busy(&fs_info->preempt_reclaim_work) &&
1726ed738ba7SJosef Bacik 		    need_preemptive_reclaim(fs_info, space_info)) {
17270d9764f6SJosef Bacik 			trace_btrfs_trigger_flush(fs_info, space_info->flags,
17280d9764f6SJosef Bacik 						  orig_bytes, flush, "preempt");
17290d9764f6SJosef Bacik 			queue_work(system_unbound_wq,
1730576fa348SJosef Bacik 				   &fs_info->preempt_reclaim_work);
17310d9764f6SJosef Bacik 		}
17320d9764f6SJosef Bacik 	}
17330d9764f6SJosef Bacik 	spin_unlock(&space_info->lock);
1734765c3fe9SJosef Bacik 	if (!ret || !can_ticket(flush))
17350d9764f6SJosef Bacik 		return ret;
17360d9764f6SJosef Bacik 
1737ac1ea10eSJosef Bacik 	return handle_reserve_ticket(fs_info, space_info, &ticket, start_ns,
1738ac1ea10eSJosef Bacik 				     orig_bytes, flush);
17390d9764f6SJosef Bacik }
17400d9764f6SJosef Bacik 
174143dd529aSDavid Sterba /*
174243dd529aSDavid Sterba  * Try to reserve metadata bytes from the block_rsv's space.
1743d98b188eSNikolay Borisov  *
1744be8d1a2aSYang Li  * @fs_info:    the filesystem
1745d98b188eSNikolay Borisov  * @block_rsv:  block_rsv we're allocating for
1746d98b188eSNikolay Borisov  * @orig_bytes: number of bytes we want
1747d98b188eSNikolay Borisov  * @flush:      whether or not we can flush to make our reservation
17480d9764f6SJosef Bacik  *
17490d9764f6SJosef Bacik  * This will reserve orig_bytes number of bytes from the space info associated
17500d9764f6SJosef Bacik  * with the block_rsv.  If there is not enough space it will make an attempt to
17510d9764f6SJosef Bacik  * flush out space to make room.  It will do this by flushing delalloc if
17520d9764f6SJosef Bacik  * possible or committing the transaction.  If flush is 0 then no attempts to
17530d9764f6SJosef Bacik  * regain reservations will be made and this will fail if there is not enough
17540d9764f6SJosef Bacik  * space already.
17550d9764f6SJosef 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)17569270501cSJosef Bacik int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
17570d9764f6SJosef Bacik 				 struct btrfs_block_rsv *block_rsv,
17580d9764f6SJosef Bacik 				 u64 orig_bytes,
17590d9764f6SJosef Bacik 				 enum btrfs_reserve_flush_enum flush)
17600d9764f6SJosef Bacik {
17610d9764f6SJosef Bacik 	int ret;
17620d9764f6SJosef Bacik 
1763f3bda421SJosef Bacik 	ret = __reserve_bytes(fs_info, block_rsv->space_info, orig_bytes, flush);
17640d9764f6SJosef Bacik 	if (ret == -ENOSPC) {
17650d9764f6SJosef Bacik 		trace_btrfs_space_reservation(fs_info, "space_info:enospc",
17660d9764f6SJosef Bacik 					      block_rsv->space_info->flags,
17670d9764f6SJosef Bacik 					      orig_bytes, 1);
17680d9764f6SJosef Bacik 
17690d9764f6SJosef Bacik 		if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
17700d9764f6SJosef Bacik 			btrfs_dump_space_info(fs_info, block_rsv->space_info,
17710d9764f6SJosef Bacik 					      orig_bytes, 0);
17720d9764f6SJosef Bacik 	}
17730d9764f6SJosef Bacik 	return ret;
17740d9764f6SJosef Bacik }
17758698fc4eSJosef Bacik 
177643dd529aSDavid Sterba /*
177743dd529aSDavid Sterba  * Try to reserve data bytes for an allocation.
1778d98b188eSNikolay Borisov  *
1779d98b188eSNikolay Borisov  * @fs_info: the filesystem
1780d98b188eSNikolay Borisov  * @bytes:   number of bytes we need
1781d98b188eSNikolay Borisov  * @flush:   how we are allowed to flush
17828698fc4eSJosef Bacik  *
17838698fc4eSJosef Bacik  * This will reserve bytes from the data space info.  If there is not enough
17848698fc4eSJosef Bacik  * space then we will attempt to flush space as specified by flush.
17858698fc4eSJosef Bacik  */
btrfs_reserve_data_bytes(struct btrfs_fs_info * fs_info,u64 bytes,enum btrfs_reserve_flush_enum flush)17868698fc4eSJosef Bacik int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
17878698fc4eSJosef Bacik 			     enum btrfs_reserve_flush_enum flush)
17888698fc4eSJosef Bacik {
17898698fc4eSJosef Bacik 	struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
1790f3bda421SJosef Bacik 	int ret;
17918698fc4eSJosef Bacik 
1792f3bda421SJosef Bacik 	ASSERT(flush == BTRFS_RESERVE_FLUSH_DATA ||
17931daedb1dSJosef Bacik 	       flush == BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE ||
17941daedb1dSJosef Bacik 	       flush == BTRFS_RESERVE_NO_FLUSH);
17958698fc4eSJosef Bacik 	ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_DATA);
17968698fc4eSJosef Bacik 
1797f3bda421SJosef Bacik 	ret = __reserve_bytes(fs_info, data_sinfo, bytes, flush);
1798f3bda421SJosef Bacik 	if (ret == -ENOSPC) {
1799f3bda421SJosef Bacik 		trace_btrfs_space_reservation(fs_info, "space_info:enospc",
18008698fc4eSJosef Bacik 					      data_sinfo->flags, bytes, 1);
1801f3bda421SJosef Bacik 		if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
1802f3bda421SJosef Bacik 			btrfs_dump_space_info(fs_info, data_sinfo, bytes, 0);
1803f3bda421SJosef Bacik 	}
18048698fc4eSJosef Bacik 	return ret;
18058698fc4eSJosef Bacik }
18068e327b9cSQu Wenruo 
18078e327b9cSQu 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)18088e327b9cSQu Wenruo __cold void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info)
18098e327b9cSQu Wenruo {
18108e327b9cSQu Wenruo 	struct btrfs_space_info *space_info;
18118e327b9cSQu Wenruo 
18128e327b9cSQu Wenruo 	btrfs_info(fs_info, "dumping space info:");
18138e327b9cSQu Wenruo 	list_for_each_entry(space_info, &fs_info->space_info, list) {
18148e327b9cSQu Wenruo 		spin_lock(&space_info->lock);
18158e327b9cSQu Wenruo 		__btrfs_dump_space_info(fs_info, space_info);
18168e327b9cSQu Wenruo 		spin_unlock(&space_info->lock);
18178e327b9cSQu Wenruo 	}
18188e327b9cSQu Wenruo 	dump_global_block_rsv(fs_info);
18198e327b9cSQu Wenruo }
1820e2f13b34SJosef Bacik 
1821e2f13b34SJosef Bacik /*
1822e2f13b34SJosef Bacik  * Account the unused space of all the readonly block group in the space_info.
1823e2f13b34SJosef Bacik  * takes mirrors into account.
1824e2f13b34SJosef Bacik  */
btrfs_account_ro_block_groups_free_space(struct btrfs_space_info * sinfo)1825e2f13b34SJosef Bacik u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
1826e2f13b34SJosef Bacik {
1827e2f13b34SJosef Bacik 	struct btrfs_block_group *block_group;
1828e2f13b34SJosef Bacik 	u64 free_bytes = 0;
1829e2f13b34SJosef Bacik 	int factor;
1830e2f13b34SJosef Bacik 
1831e2f13b34SJosef Bacik 	/* It's df, we don't care if it's racy */
1832e2f13b34SJosef Bacik 	if (list_empty(&sinfo->ro_bgs))
1833e2f13b34SJosef Bacik 		return 0;
1834e2f13b34SJosef Bacik 
1835e2f13b34SJosef Bacik 	spin_lock(&sinfo->lock);
1836e2f13b34SJosef Bacik 	list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
1837e2f13b34SJosef Bacik 		spin_lock(&block_group->lock);
1838e2f13b34SJosef Bacik 
1839e2f13b34SJosef Bacik 		if (!block_group->ro) {
1840e2f13b34SJosef Bacik 			spin_unlock(&block_group->lock);
1841e2f13b34SJosef Bacik 			continue;
1842e2f13b34SJosef Bacik 		}
1843e2f13b34SJosef Bacik 
1844e2f13b34SJosef Bacik 		factor = btrfs_bg_type_to_factor(block_group->flags);
1845e2f13b34SJosef Bacik 		free_bytes += (block_group->length -
1846e2f13b34SJosef Bacik 			       block_group->used) * factor;
1847e2f13b34SJosef Bacik 
1848e2f13b34SJosef Bacik 		spin_unlock(&block_group->lock);
1849e2f13b34SJosef Bacik 	}
1850e2f13b34SJosef Bacik 	spin_unlock(&sinfo->lock);
1851e2f13b34SJosef Bacik 
1852e2f13b34SJosef Bacik 	return free_bytes;
1853e2f13b34SJosef Bacik }
1854