xref: /openbmc/linux/fs/btrfs/space-info.h (revision 90cbee20)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef BTRFS_SPACE_INFO_H
4 #define BTRFS_SPACE_INFO_H
5 
6 #include "volumes.h"
7 
8 /*
9  * Different levels for to flush space when doing space reservations.
10  *
11  * The higher the level, the more methods we try to reclaim space.
12  */
13 enum btrfs_reserve_flush_enum {
14 	/* If we are in the transaction, we can't flush anything.*/
15 	BTRFS_RESERVE_NO_FLUSH,
16 
17 	/*
18 	 * Flush space by:
19 	 * - Running delayed inode items
20 	 * - Allocating a new chunk
21 	 */
22 	BTRFS_RESERVE_FLUSH_LIMIT,
23 
24 	/*
25 	 * Flush space by:
26 	 * - Running delayed inode items
27 	 * - Running delayed refs
28 	 * - Running delalloc and waiting for ordered extents
29 	 * - Allocating a new chunk
30 	 */
31 	BTRFS_RESERVE_FLUSH_EVICT,
32 
33 	/*
34 	 * Flush space by above mentioned methods and by:
35 	 * - Running delayed iputs
36 	 * - Committing transaction
37 	 *
38 	 * Can be interrupted by a fatal signal.
39 	 */
40 	BTRFS_RESERVE_FLUSH_DATA,
41 	BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
42 	BTRFS_RESERVE_FLUSH_ALL,
43 
44 	/*
45 	 * Pretty much the same as FLUSH_ALL, but can also steal space from
46 	 * global rsv.
47 	 *
48 	 * Can be interrupted by a fatal signal.
49 	 */
50 	BTRFS_RESERVE_FLUSH_ALL_STEAL,
51 
52 	/*
53 	 * This is for btrfs_use_block_rsv only.  We have exhausted our block
54 	 * rsv and our global block rsv.  This can happen for things like
55 	 * delalloc where we are overwriting a lot of extents with a single
56 	 * extent and didn't reserve enough space.  Alternatively it can happen
57 	 * with delalloc where we reserve 1 extents worth for a large extent but
58 	 * fragmentation leads to multiple extents being created.  This will
59 	 * give us the reservation in the case of
60 	 *
61 	 * if (num_bytes < (space_info->total_bytes -
62 	 *		    btrfs_space_info_used(space_info, false))
63 	 *
64 	 * Which ignores bytes_may_use.  This is potentially dangerous, but our
65 	 * reservation system is generally pessimistic so is able to absorb this
66 	 * style of mistake.
67 	 */
68 	BTRFS_RESERVE_FLUSH_EMERGENCY,
69 };
70 
71 enum btrfs_flush_state {
72 	FLUSH_DELAYED_ITEMS_NR	= 1,
73 	FLUSH_DELAYED_ITEMS	= 2,
74 	FLUSH_DELAYED_REFS_NR	= 3,
75 	FLUSH_DELAYED_REFS	= 4,
76 	FLUSH_DELALLOC		= 5,
77 	FLUSH_DELALLOC_WAIT	= 6,
78 	FLUSH_DELALLOC_FULL	= 7,
79 	ALLOC_CHUNK		= 8,
80 	ALLOC_CHUNK_FORCE	= 9,
81 	RUN_DELAYED_IPUTS	= 10,
82 	COMMIT_TRANS		= 11,
83 };
84 
85 struct btrfs_space_info {
86 	spinlock_t lock;
87 
88 	u64 total_bytes;	/* total bytes in the space,
89 				   this doesn't take mirrors into account */
90 	u64 bytes_used;		/* total bytes used,
91 				   this doesn't take mirrors into account */
92 	u64 bytes_pinned;	/* total bytes pinned, will be freed when the
93 				   transaction finishes */
94 	u64 bytes_reserved;	/* total bytes the allocator has reserved for
95 				   current allocations */
96 	u64 bytes_may_use;	/* number of bytes that may be used for
97 				   delalloc/allocations */
98 	u64 bytes_readonly;	/* total bytes that are read only */
99 	u64 bytes_zone_unusable;	/* total bytes that are unusable until
100 					   resetting the device zone */
101 
102 	u64 max_extent_size;	/* This will hold the maximum extent size of
103 				   the space info if we had an ENOSPC in the
104 				   allocator. */
105 	/* Chunk size in bytes */
106 	u64 chunk_size;
107 
108 	/*
109 	 * Once a block group drops below this threshold (percents) we'll
110 	 * schedule it for reclaim.
111 	 */
112 	int bg_reclaim_threshold;
113 
114 	int clamp;		/* Used to scale our threshold for preemptive
115 				   flushing. The value is >> clamp, so turns
116 				   out to be a 2^clamp divisor. */
117 
118 	unsigned int full:1;	/* indicates that we cannot allocate any more
119 				   chunks for this space */
120 	unsigned int chunk_alloc:1;	/* set if we are allocating a chunk */
121 
122 	unsigned int flush:1;		/* set if we are trying to make space */
123 
124 	unsigned int force_alloc;	/* set if we need to force a chunk
125 					   alloc for this space */
126 
127 	u64 disk_used;		/* total bytes used on disk */
128 	u64 disk_total;		/* total bytes on disk, takes mirrors into
129 				   account */
130 
131 	u64 flags;
132 
133 	struct list_head list;
134 	/* Protected by the spinlock 'lock'. */
135 	struct list_head ro_bgs;
136 	struct list_head priority_tickets;
137 	struct list_head tickets;
138 
139 	/*
140 	 * Size of space that needs to be reclaimed in order to satisfy pending
141 	 * tickets
142 	 */
143 	u64 reclaim_size;
144 
145 	/*
146 	 * tickets_id just indicates the next ticket will be handled, so note
147 	 * it's not stored per ticket.
148 	 */
149 	u64 tickets_id;
150 
151 	struct rw_semaphore groups_sem;
152 	/* for block groups in our same type */
153 	struct list_head block_groups[BTRFS_NR_RAID_TYPES];
154 
155 	struct kobject kobj;
156 	struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
157 };
158 
159 struct reserve_ticket {
160 	u64 bytes;
161 	int error;
162 	bool steal;
163 	struct list_head list;
164 	wait_queue_head_t wait;
165 };
166 
167 static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
168 {
169 	return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
170 		(space_info->flags & BTRFS_BLOCK_GROUP_DATA));
171 }
172 
173 /*
174  *
175  * Declare a helper function to detect underflow of various space info members
176  */
177 #define DECLARE_SPACE_INFO_UPDATE(name, trace_name)			\
178 static inline void							\
179 btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info,		\
180 			       struct btrfs_space_info *sinfo,		\
181 			       s64 bytes)				\
182 {									\
183 	const u64 abs_bytes = (bytes < 0) ? -bytes : bytes;		\
184 	lockdep_assert_held(&sinfo->lock);				\
185 	trace_update_##name(fs_info, sinfo, sinfo->name, bytes);	\
186 	trace_btrfs_space_reservation(fs_info, trace_name,		\
187 				      sinfo->flags, abs_bytes,		\
188 				      bytes > 0);			\
189 	if (bytes < 0 && sinfo->name < -bytes) {			\
190 		WARN_ON(1);						\
191 		sinfo->name = 0;					\
192 		return;							\
193 	}								\
194 	sinfo->name += bytes;						\
195 }
196 
197 DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
198 DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
199 
200 int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
201 void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
202 				struct btrfs_block_group *block_group);
203 void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
204 					u64 chunk_size);
205 struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
206 					       u64 flags);
207 u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
208 			  bool may_use_included);
209 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
210 void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
211 			   struct btrfs_space_info *info, u64 bytes,
212 			   int dump_block_groups);
213 int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
214 				 struct btrfs_block_rsv *block_rsv,
215 				 u64 orig_bytes,
216 				 enum btrfs_reserve_flush_enum flush);
217 void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
218 				struct btrfs_space_info *space_info);
219 int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
220 			 struct btrfs_space_info *space_info, u64 bytes,
221 			 enum btrfs_reserve_flush_enum flush);
222 
223 static inline void btrfs_space_info_free_bytes_may_use(
224 				struct btrfs_fs_info *fs_info,
225 				struct btrfs_space_info *space_info,
226 				u64 num_bytes)
227 {
228 	spin_lock(&space_info->lock);
229 	btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
230 	btrfs_try_granting_tickets(fs_info, space_info);
231 	spin_unlock(&space_info->lock);
232 }
233 int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
234 			     enum btrfs_reserve_flush_enum flush);
235 void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info);
236 void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info);
237 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
238 
239 #endif /* BTRFS_SPACE_INFO_H */
240