18719aaaeSJosef Bacik /* SPDX-License-Identifier: GPL-2.0 */
28719aaaeSJosef Bacik
38719aaaeSJosef Bacik #ifndef BTRFS_SPACE_INFO_H
48719aaaeSJosef Bacik #define BTRFS_SPACE_INFO_H
58719aaaeSJosef Bacik
6f04fbcc6SQu Wenruo #include "volumes.h"
7f04fbcc6SQu Wenruo
8f1e5c618SJosef Bacik /*
9f1e5c618SJosef Bacik * Different levels for to flush space when doing space reservations.
10f1e5c618SJosef Bacik *
11f1e5c618SJosef Bacik * The higher the level, the more methods we try to reclaim space.
12f1e5c618SJosef Bacik */
13f1e5c618SJosef Bacik enum btrfs_reserve_flush_enum {
14f1e5c618SJosef Bacik /* If we are in the transaction, we can't flush anything.*/
15f1e5c618SJosef Bacik BTRFS_RESERVE_NO_FLUSH,
16f1e5c618SJosef Bacik
17f1e5c618SJosef Bacik /*
18f1e5c618SJosef Bacik * Flush space by:
19f1e5c618SJosef Bacik * - Running delayed inode items
20f1e5c618SJosef Bacik * - Allocating a new chunk
21f1e5c618SJosef Bacik */
22f1e5c618SJosef Bacik BTRFS_RESERVE_FLUSH_LIMIT,
23f1e5c618SJosef Bacik
24f1e5c618SJosef Bacik /*
25f1e5c618SJosef Bacik * Flush space by:
26f1e5c618SJosef Bacik * - Running delayed inode items
27f1e5c618SJosef Bacik * - Running delayed refs
28f1e5c618SJosef Bacik * - Running delalloc and waiting for ordered extents
29f1e5c618SJosef Bacik * - Allocating a new chunk
301a332502SFilipe Manana * - Committing transaction
31f1e5c618SJosef Bacik */
32f1e5c618SJosef Bacik BTRFS_RESERVE_FLUSH_EVICT,
33f1e5c618SJosef Bacik
34f1e5c618SJosef Bacik /*
35f1e5c618SJosef Bacik * Flush space by above mentioned methods and by:
36f1e5c618SJosef Bacik * - Running delayed iputs
37f1e5c618SJosef Bacik * - Committing transaction
38f1e5c618SJosef Bacik *
39f1e5c618SJosef Bacik * Can be interrupted by a fatal signal.
40f1e5c618SJosef Bacik */
41f1e5c618SJosef Bacik BTRFS_RESERVE_FLUSH_DATA,
42f1e5c618SJosef Bacik BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
43f1e5c618SJosef Bacik BTRFS_RESERVE_FLUSH_ALL,
44f1e5c618SJosef Bacik
45f1e5c618SJosef Bacik /*
46f1e5c618SJosef Bacik * Pretty much the same as FLUSH_ALL, but can also steal space from
47f1e5c618SJosef Bacik * global rsv.
48f1e5c618SJosef Bacik *
49f1e5c618SJosef Bacik * Can be interrupted by a fatal signal.
50f1e5c618SJosef Bacik */
51f1e5c618SJosef Bacik BTRFS_RESERVE_FLUSH_ALL_STEAL,
52765c3fe9SJosef Bacik
53765c3fe9SJosef Bacik /*
54765c3fe9SJosef Bacik * This is for btrfs_use_block_rsv only. We have exhausted our block
55765c3fe9SJosef Bacik * rsv and our global block rsv. This can happen for things like
56765c3fe9SJosef Bacik * delalloc where we are overwriting a lot of extents with a single
57765c3fe9SJosef Bacik * extent and didn't reserve enough space. Alternatively it can happen
58765c3fe9SJosef Bacik * with delalloc where we reserve 1 extents worth for a large extent but
59765c3fe9SJosef Bacik * fragmentation leads to multiple extents being created. This will
60765c3fe9SJosef Bacik * give us the reservation in the case of
61765c3fe9SJosef Bacik *
62765c3fe9SJosef Bacik * if (num_bytes < (space_info->total_bytes -
63765c3fe9SJosef Bacik * btrfs_space_info_used(space_info, false))
64765c3fe9SJosef Bacik *
65765c3fe9SJosef Bacik * Which ignores bytes_may_use. This is potentially dangerous, but our
66765c3fe9SJosef Bacik * reservation system is generally pessimistic so is able to absorb this
67765c3fe9SJosef Bacik * style of mistake.
68765c3fe9SJosef Bacik */
69765c3fe9SJosef Bacik BTRFS_RESERVE_FLUSH_EMERGENCY,
70f1e5c618SJosef Bacik };
71f1e5c618SJosef Bacik
72f1e5c618SJosef Bacik enum btrfs_flush_state {
73f1e5c618SJosef Bacik FLUSH_DELAYED_ITEMS_NR = 1,
74f1e5c618SJosef Bacik FLUSH_DELAYED_ITEMS = 2,
75f1e5c618SJosef Bacik FLUSH_DELAYED_REFS_NR = 3,
76f1e5c618SJosef Bacik FLUSH_DELAYED_REFS = 4,
77f1e5c618SJosef Bacik FLUSH_DELALLOC = 5,
78f1e5c618SJosef Bacik FLUSH_DELALLOC_WAIT = 6,
79f1e5c618SJosef Bacik FLUSH_DELALLOC_FULL = 7,
80f1e5c618SJosef Bacik ALLOC_CHUNK = 8,
81f1e5c618SJosef Bacik ALLOC_CHUNK_FORCE = 9,
82f1e5c618SJosef Bacik RUN_DELAYED_IPUTS = 10,
83f1e5c618SJosef Bacik COMMIT_TRANS = 11,
84f1e5c618SJosef Bacik };
85f1e5c618SJosef Bacik
868719aaaeSJosef Bacik struct btrfs_space_info {
878719aaaeSJosef Bacik spinlock_t lock;
888719aaaeSJosef Bacik
898719aaaeSJosef Bacik u64 total_bytes; /* total bytes in the space,
908719aaaeSJosef Bacik this doesn't take mirrors into account */
918719aaaeSJosef Bacik u64 bytes_used; /* total bytes used,
928719aaaeSJosef Bacik this doesn't take mirrors into account */
938719aaaeSJosef Bacik u64 bytes_pinned; /* total bytes pinned, will be freed when the
948719aaaeSJosef Bacik transaction finishes */
958719aaaeSJosef Bacik u64 bytes_reserved; /* total bytes the allocator has reserved for
968719aaaeSJosef Bacik current allocations */
978719aaaeSJosef Bacik u64 bytes_may_use; /* number of bytes that may be used for
988719aaaeSJosef Bacik delalloc/allocations */
998719aaaeSJosef Bacik u64 bytes_readonly; /* total bytes that are read only */
100169e0da9SNaohiro Aota u64 bytes_zone_unusable; /* total bytes that are unusable until
101169e0da9SNaohiro Aota resetting the device zone */
1028719aaaeSJosef Bacik
1038719aaaeSJosef Bacik u64 max_extent_size; /* This will hold the maximum extent size of
1048719aaaeSJosef Bacik the space info if we had an ENOSPC in the
1058719aaaeSJosef Bacik allocator. */
106f6fca391SStefan Roesch /* Chunk size in bytes */
107f6fca391SStefan Roesch u64 chunk_size;
1088719aaaeSJosef Bacik
109bb5a098dSJosef Bacik /*
110bb5a098dSJosef Bacik * Once a block group drops below this threshold (percents) we'll
111bb5a098dSJosef Bacik * schedule it for reclaim.
112bb5a098dSJosef Bacik */
113bb5a098dSJosef Bacik int bg_reclaim_threshold;
114bb5a098dSJosef Bacik
11588a777a6SJosef Bacik int clamp; /* Used to scale our threshold for preemptive
11688a777a6SJosef Bacik flushing. The value is >> clamp, so turns
11788a777a6SJosef Bacik out to be a 2^clamp divisor. */
11888a777a6SJosef Bacik
1198719aaaeSJosef Bacik unsigned int full:1; /* indicates that we cannot allocate any more
1208719aaaeSJosef Bacik chunks for this space */
1218719aaaeSJosef Bacik unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
1228719aaaeSJosef Bacik
1238719aaaeSJosef Bacik unsigned int flush:1; /* set if we are trying to make space */
1248719aaaeSJosef Bacik
1258719aaaeSJosef Bacik unsigned int force_alloc; /* set if we need to force a chunk
1268719aaaeSJosef Bacik alloc for this space */
1278719aaaeSJosef Bacik
1288719aaaeSJosef Bacik u64 disk_used; /* total bytes used on disk */
1298719aaaeSJosef Bacik u64 disk_total; /* total bytes on disk, takes mirrors into
1308719aaaeSJosef Bacik account */
1318719aaaeSJosef Bacik
1328719aaaeSJosef Bacik u64 flags;
1338719aaaeSJosef Bacik
1348719aaaeSJosef Bacik struct list_head list;
1358719aaaeSJosef Bacik /* Protected by the spinlock 'lock'. */
1368719aaaeSJosef Bacik struct list_head ro_bgs;
1378719aaaeSJosef Bacik struct list_head priority_tickets;
1388719aaaeSJosef Bacik struct list_head tickets;
139db161806SNikolay Borisov
140db161806SNikolay Borisov /*
141db161806SNikolay Borisov * Size of space that needs to be reclaimed in order to satisfy pending
142db161806SNikolay Borisov * tickets
143db161806SNikolay Borisov */
144db161806SNikolay Borisov u64 reclaim_size;
145db161806SNikolay Borisov
1468719aaaeSJosef Bacik /*
1478719aaaeSJosef Bacik * tickets_id just indicates the next ticket will be handled, so note
1488719aaaeSJosef Bacik * it's not stored per ticket.
1498719aaaeSJosef Bacik */
1508719aaaeSJosef Bacik u64 tickets_id;
1518719aaaeSJosef Bacik
1528719aaaeSJosef Bacik struct rw_semaphore groups_sem;
1538719aaaeSJosef Bacik /* for block groups in our same type */
1548719aaaeSJosef Bacik struct list_head block_groups[BTRFS_NR_RAID_TYPES];
1558719aaaeSJosef Bacik
1568719aaaeSJosef Bacik struct kobject kobj;
1578719aaaeSJosef Bacik struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
1588719aaaeSJosef Bacik };
1598719aaaeSJosef Bacik
160b338b013SJosef Bacik struct reserve_ticket {
161b338b013SJosef Bacik u64 bytes;
162b338b013SJosef Bacik int error;
1637f9fe614SJosef Bacik bool steal;
164b338b013SJosef Bacik struct list_head list;
165b338b013SJosef Bacik wait_queue_head_t wait;
166b338b013SJosef Bacik };
167b338b013SJosef Bacik
btrfs_mixed_space_info(struct btrfs_space_info * space_info)1688719aaaeSJosef Bacik static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
1698719aaaeSJosef Bacik {
1708719aaaeSJosef Bacik return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
1718719aaaeSJosef Bacik (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
1728719aaaeSJosef Bacik }
1738719aaaeSJosef Bacik
174bb96c4e5SJosef Bacik /*
175bb96c4e5SJosef Bacik *
176bb96c4e5SJosef Bacik * Declare a helper function to detect underflow of various space info members
177bb96c4e5SJosef Bacik */
178f3e75e38SJosef Bacik #define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \
179bb96c4e5SJosef Bacik static inline void \
180bb96c4e5SJosef Bacik btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info, \
181bb96c4e5SJosef Bacik struct btrfs_space_info *sinfo, \
182bb96c4e5SJosef Bacik s64 bytes) \
183bb96c4e5SJosef Bacik { \
184f3e75e38SJosef Bacik const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \
185bb96c4e5SJosef Bacik lockdep_assert_held(&sinfo->lock); \
186bb96c4e5SJosef Bacik trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \
187f3e75e38SJosef Bacik trace_btrfs_space_reservation(fs_info, trace_name, \
188f3e75e38SJosef Bacik sinfo->flags, abs_bytes, \
189f3e75e38SJosef Bacik bytes > 0); \
190bb96c4e5SJosef Bacik if (bytes < 0 && sinfo->name < -bytes) { \
191bb96c4e5SJosef Bacik WARN_ON(1); \
192bb96c4e5SJosef Bacik sinfo->name = 0; \
193bb96c4e5SJosef Bacik return; \
194bb96c4e5SJosef Bacik } \
195bb96c4e5SJosef Bacik sinfo->name += bytes; \
196bb96c4e5SJosef Bacik }
197bb96c4e5SJosef Bacik
198f3e75e38SJosef Bacik DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
199f3e75e38SJosef Bacik DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
200*ae29e6f7SNaohiro Aota DECLARE_SPACE_INFO_UPDATE(bytes_zone_unusable, "zone_unusable");
201bb96c4e5SJosef Bacik
202280c2908SJosef Bacik int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
2039d4b0a12SJosef Bacik void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
204723de71dSJosef Bacik struct btrfs_block_group *block_group);
205f6fca391SStefan Roesch void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
206f6fca391SStefan Roesch u64 chunk_size);
207280c2908SJosef Bacik struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
208280c2908SJosef Bacik u64 flags);
209e1f60a65SDavid Sterba u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
210280c2908SJosef Bacik bool may_use_included);
211280c2908SJosef Bacik void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
2125da6afebSJosef Bacik void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
2135da6afebSJosef Bacik struct btrfs_space_info *info, u64 bytes,
2145da6afebSJosef Bacik int dump_block_groups);
2159270501cSJosef Bacik int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
2160d9764f6SJosef Bacik struct btrfs_block_rsv *block_rsv,
2170d9764f6SJosef Bacik u64 orig_bytes,
2180d9764f6SJosef Bacik enum btrfs_reserve_flush_enum flush);
21918fa2284SJosef Bacik void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
22018fa2284SJosef Bacik struct btrfs_space_info *space_info);
221a30a3d20SJosef Bacik int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
222a30a3d20SJosef Bacik struct btrfs_space_info *space_info, u64 bytes,
223a30a3d20SJosef Bacik enum btrfs_reserve_flush_enum flush);
22418fa2284SJosef Bacik
btrfs_space_info_free_bytes_may_use(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info,u64 num_bytes)225d05e4649SJosef Bacik static inline void btrfs_space_info_free_bytes_may_use(
22618fa2284SJosef Bacik struct btrfs_fs_info *fs_info,
22718fa2284SJosef Bacik struct btrfs_space_info *space_info,
22818fa2284SJosef Bacik u64 num_bytes)
22918fa2284SJosef Bacik {
23018fa2284SJosef Bacik spin_lock(&space_info->lock);
23118fa2284SJosef Bacik btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
23218fa2284SJosef Bacik btrfs_try_granting_tickets(fs_info, space_info);
23318fa2284SJosef Bacik spin_unlock(&space_info->lock);
23418fa2284SJosef Bacik }
2358698fc4eSJosef Bacik int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
2368698fc4eSJosef Bacik enum btrfs_reserve_flush_enum flush);
2378e327b9cSQu Wenruo void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info);
23843712116SJosef Bacik void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info);
239e2f13b34SJosef Bacik u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
2408e327b9cSQu Wenruo
2418719aaaeSJosef Bacik #endif /* BTRFS_SPACE_INFO_H */
242