blob: cb5056472e798dc590f7c8c3e3f0e2b78100548a [file] [log] [blame]
Josef Bacik8719aaa2019-06-18 16:09:16 -04001/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef BTRFS_SPACE_INFO_H
4#define BTRFS_SPACE_INFO_H
5
6struct btrfs_space_info {
7 spinlock_t lock;
8
9 u64 total_bytes; /* total bytes in the space,
10 this doesn't take mirrors into account */
11 u64 bytes_used; /* total bytes used,
12 this doesn't take mirrors into account */
13 u64 bytes_pinned; /* total bytes pinned, will be freed when the
14 transaction finishes */
15 u64 bytes_reserved; /* total bytes the allocator has reserved for
16 current allocations */
17 u64 bytes_may_use; /* number of bytes that may be used for
18 delalloc/allocations */
19 u64 bytes_readonly; /* total bytes that are read only */
Naohiro Aota169e0da2021-02-04 19:21:52 +090020 u64 bytes_zone_unusable; /* total bytes that are unusable until
21 resetting the device zone */
Josef Bacik8719aaa2019-06-18 16:09:16 -040022
23 u64 max_extent_size; /* This will hold the maximum extent size of
24 the space info if we had an ENOSPC in the
25 allocator. */
26
Josef Bacik88a777a2020-10-09 09:28:27 -040027 int clamp; /* Used to scale our threshold for preemptive
28 flushing. The value is >> clamp, so turns
29 out to be a 2^clamp divisor. */
30
Josef Bacik8719aaa2019-06-18 16:09:16 -040031 unsigned int full:1; /* indicates that we cannot allocate any more
32 chunks for this space */
33 unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
34
35 unsigned int flush:1; /* set if we are trying to make space */
36
37 unsigned int force_alloc; /* set if we need to force a chunk
38 alloc for this space */
39
40 u64 disk_used; /* total bytes used on disk */
41 u64 disk_total; /* total bytes on disk, takes mirrors into
42 account */
43
44 u64 flags;
45
Josef Bacik8719aaa2019-06-18 16:09:16 -040046 struct list_head list;
47 /* Protected by the spinlock 'lock'. */
48 struct list_head ro_bgs;
49 struct list_head priority_tickets;
50 struct list_head tickets;
Nikolay Borisovdb161802020-03-10 11:00:35 +020051
52 /*
53 * Size of space that needs to be reclaimed in order to satisfy pending
54 * tickets
55 */
56 u64 reclaim_size;
57
Josef Bacik8719aaa2019-06-18 16:09:16 -040058 /*
59 * tickets_id just indicates the next ticket will be handled, so note
60 * it's not stored per ticket.
61 */
62 u64 tickets_id;
63
64 struct rw_semaphore groups_sem;
65 /* for block groups in our same type */
66 struct list_head block_groups[BTRFS_NR_RAID_TYPES];
Josef Bacik8719aaa2019-06-18 16:09:16 -040067
68 struct kobject kobj;
69 struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
70};
71
Josef Bacikb338b012019-06-18 16:09:22 -040072struct reserve_ticket {
Josef Bacikb338b012019-06-18 16:09:22 -040073 u64 bytes;
74 int error;
Josef Bacik7f9fe612020-03-13 15:58:05 -040075 bool steal;
Josef Bacikb338b012019-06-18 16:09:22 -040076 struct list_head list;
77 wait_queue_head_t wait;
78};
79
Josef Bacik8719aaa2019-06-18 16:09:16 -040080static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
81{
82 return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
83 (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
84}
85
Josef Bacikbb96c4e2019-06-18 16:09:21 -040086/*
87 *
88 * Declare a helper function to detect underflow of various space info members
89 */
Josef Bacikf3e75e32019-08-22 15:10:55 -040090#define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \
Josef Bacikbb96c4e2019-06-18 16:09:21 -040091static inline void \
92btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info, \
93 struct btrfs_space_info *sinfo, \
94 s64 bytes) \
95{ \
Josef Bacikf3e75e32019-08-22 15:10:55 -040096 const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \
Josef Bacikbb96c4e2019-06-18 16:09:21 -040097 lockdep_assert_held(&sinfo->lock); \
98 trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \
Josef Bacikf3e75e32019-08-22 15:10:55 -040099 trace_btrfs_space_reservation(fs_info, trace_name, \
100 sinfo->flags, abs_bytes, \
101 bytes > 0); \
Josef Bacikbb96c4e2019-06-18 16:09:21 -0400102 if (bytes < 0 && sinfo->name < -bytes) { \
103 WARN_ON(1); \
104 sinfo->name = 0; \
105 return; \
106 } \
107 sinfo->name += bytes; \
108}
109
Josef Bacikf3e75e32019-08-22 15:10:55 -0400110DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
111DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
Josef Bacikbb96c4e2019-06-18 16:09:21 -0400112
Josef Bacik280c29082019-06-18 16:09:19 -0400113int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
114void btrfs_update_space_info(struct btrfs_fs_info *info, u64 flags,
115 u64 total_bytes, u64 bytes_used,
Naohiro Aota169e0da2021-02-04 19:21:52 +0900116 u64 bytes_readonly, u64 bytes_zone_unusable,
Josef Bacik280c29082019-06-18 16:09:19 -0400117 struct btrfs_space_info **space_info);
118struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
119 u64 flags);
David Sterbae1f60a62019-10-01 19:57:39 +0200120u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
Josef Bacik280c29082019-06-18 16:09:19 -0400121 bool may_use_included);
122void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
Josef Bacik5da6afe2019-06-18 16:09:24 -0400123void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
124 struct btrfs_space_info *info, u64 bytes,
125 int dump_block_groups);
Josef Bacik0d9764f2019-06-18 16:09:25 -0400126int btrfs_reserve_metadata_bytes(struct btrfs_root *root,
127 struct btrfs_block_rsv *block_rsv,
128 u64 orig_bytes,
129 enum btrfs_reserve_flush_enum flush);
Josef Bacik18fa2282019-08-22 15:10:58 -0400130void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
131 struct btrfs_space_info *space_info);
Josef Bacika30a3d22020-01-17 09:07:39 -0500132int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
133 struct btrfs_space_info *space_info, u64 bytes,
134 enum btrfs_reserve_flush_enum flush);
Josef Bacik18fa2282019-08-22 15:10:58 -0400135
Josef Bacikd05e4642019-08-22 15:11:02 -0400136static inline void btrfs_space_info_free_bytes_may_use(
Josef Bacik18fa2282019-08-22 15:10:58 -0400137 struct btrfs_fs_info *fs_info,
138 struct btrfs_space_info *space_info,
139 u64 num_bytes)
140{
141 spin_lock(&space_info->lock);
142 btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
143 btrfs_try_granting_tickets(fs_info, space_info);
144 spin_unlock(&space_info->lock);
145}
Josef Bacik8698fc42020-07-21 10:22:25 -0400146int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
147 enum btrfs_reserve_flush_enum flush);
Josef Bacik8719aaa2019-06-18 16:09:16 -0400148#endif /* BTRFS_SPACE_INFO_H */