Blame view

fs/btrfs/block-group.h 9.85 KB
aac0023c2   Josef Bacik   btrfs: move basic...
1
2
3
4
  /* SPDX-License-Identifier: GPL-2.0 */
  
  #ifndef BTRFS_BLOCK_GROUP_H
  #define BTRFS_BLOCK_GROUP_H
67b61aefc   David Sterba   btrfs: move struc...
5
  #include "free-space-cache.h"
aac0023c2   Josef Bacik   btrfs: move basic...
6
7
8
9
10
11
  enum btrfs_disk_cache_state {
  	BTRFS_DC_WRITTEN,
  	BTRFS_DC_ERROR,
  	BTRFS_DC_CLEAR,
  	BTRFS_DC_SETUP,
  };
07730d87a   Josef Bacik   btrfs: migrate th...
12
  /*
2bee7eb8b   Dennis Zhou   btrfs: discard on...
13
14
15
16
17
18
19
20
21
22
23
24
25
   * This describes the state of the block_group for async discard.  This is due
   * to the two pass nature of it where extent discarding is prioritized over
   * bitmap discarding.  BTRFS_DISCARD_RESET_CURSOR is set when we are resetting
   * between lists to prevent contention for discard state variables
   * (eg. discard_cursor).
   */
  enum btrfs_discard_state {
  	BTRFS_DISCARD_EXTENTS,
  	BTRFS_DISCARD_BITMAPS,
  	BTRFS_DISCARD_RESET_CURSOR,
  };
  
  /*
07730d87a   Josef Bacik   btrfs: migrate th...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
   * Control flags for do_chunk_alloc's force field CHUNK_ALLOC_NO_FORCE means to
   * only allocate a chunk if we really need one.
   *
   * CHUNK_ALLOC_LIMITED means to only try and allocate one if we have very few
   * chunks already allocated.  This is used as part of the clustering code to
   * help make sure we have a good pool of storage to cluster in, without filling
   * the FS with empty chunks
   *
   * CHUNK_ALLOC_FORCE means it must try to allocate one
   */
  enum btrfs_chunk_alloc_enum {
  	CHUNK_ALLOC_NO_FORCE,
  	CHUNK_ALLOC_LIMITED,
  	CHUNK_ALLOC_FORCE,
  };
aac0023c2   Josef Bacik   btrfs: move basic...
41
42
43
44
45
  struct btrfs_caching_control {
  	struct list_head list;
  	struct mutex mutex;
  	wait_queue_head_t wait;
  	struct btrfs_work work;
32da5386d   David Sterba   btrfs: rename btr...
46
  	struct btrfs_block_group *block_group;
aac0023c2   Josef Bacik   btrfs: move basic...
47
48
49
50
51
52
  	u64 progress;
  	refcount_t count;
  };
  
  /* Once caching_thread() finds this much free space, it will wake up waiters. */
  #define CACHING_CTL_WAKE_UP SZ_2M
32da5386d   David Sterba   btrfs: rename btr...
53
  struct btrfs_block_group {
aac0023c2   Josef Bacik   btrfs: move basic...
54
55
56
  	struct btrfs_fs_info *fs_info;
  	struct inode *inode;
  	spinlock_t lock;
b3470b5db   David Sterba   btrfs: add dedica...
57
58
  	u64 start;
  	u64 length;
aac0023c2   Josef Bacik   btrfs: move basic...
59
60
  	u64 pinned;
  	u64 reserved;
bf38be65f   David Sterba   btrfs: move block...
61
  	u64 used;
aac0023c2   Josef Bacik   btrfs: move basic...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  	u64 delalloc_bytes;
  	u64 bytes_super;
  	u64 flags;
  	u64 cache_generation;
  
  	/*
  	 * If the free space extent count exceeds this number, convert the block
  	 * group to bitmaps.
  	 */
  	u32 bitmap_high_thresh;
  
  	/*
  	 * If the free space extent count drops below this number, convert the
  	 * block group back to extents.
  	 */
  	u32 bitmap_low_thresh;
  
  	/*
  	 * It is just used for the delayed data space allocation because
  	 * only the data space allocation and the relative metadata update
  	 * can be done cross the transaction.
  	 */
  	struct rw_semaphore data_rwsem;
  
  	/* For raid56, this is a full stripe, without parity */
  	unsigned long full_stripe_len;
  
  	unsigned int ro;
  	unsigned int iref:1;
  	unsigned int has_caching_ctl:1;
  	unsigned int removed:1;
  
  	int disk_cache_state;
  
  	/* Cache tracking stuff */
  	int cached;
  	struct btrfs_caching_control *caching_ctl;
  	u64 last_byte_to_unpin;
  
  	struct btrfs_space_info *space_info;
  
  	/* Free space cache stuff */
  	struct btrfs_free_space_ctl *free_space_ctl;
  
  	/* Block group cache stuff */
  	struct rb_node cache_node;
  
  	/* For block groups in the same raid type */
  	struct list_head list;
48aaeebe4   Josef Bacik   btrfs: convert bl...
111
  	refcount_t refs;
aac0023c2   Josef Bacik   btrfs: move basic...
112
113
114
115
116
117
118
119
120
121
122
123
  
  	/*
  	 * List of struct btrfs_free_clusters for this block group.
  	 * Today it will only have one thing on it, but that may change
  	 */
  	struct list_head cluster_list;
  
  	/* For delayed block group creation or deletion of empty block groups */
  	struct list_head bg_list;
  
  	/* For read-only block groups */
  	struct list_head ro_list;
6b7304af6   Filipe Manana   btrfs: rename mem...
124
125
126
127
128
129
130
131
132
  	/*
  	 * When non-zero it means the block group's logical address and its
  	 * device extents can not be reused for future block group allocations
  	 * until the counter goes down to 0. This is to prevent them from being
  	 * reused while some task is still using the block group after it was
  	 * deleted - we want to make sure they can only be reused for new block
  	 * groups after that task is done with the deleted block group.
  	 */
  	atomic_t frozen;
b0643e59c   Dennis Zhou   btrfs: add the be...
133
  	/* For discard operations */
b0643e59c   Dennis Zhou   btrfs: add the be...
134
135
136
  	struct list_head discard_list;
  	int discard_index;
  	u64 discard_eligible_time;
2bee7eb8b   Dennis Zhou   btrfs: discard on...
137
138
  	u64 discard_cursor;
  	enum btrfs_discard_state discard_state;
aac0023c2   Josef Bacik   btrfs: move basic...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  
  	/* For dirty block groups */
  	struct list_head dirty_list;
  	struct list_head io_list;
  
  	struct btrfs_io_ctl io_ctl;
  
  	/*
  	 * Incremented when doing extent allocations and holding a read lock
  	 * on the space_info's groups_sem semaphore.
  	 * Decremented when an ordered extent that represents an IO against this
  	 * block group's range is created (after it's added to its inode's
  	 * root's list of ordered extents) or immediately after the allocation
  	 * if it's a metadata extent or fallocate extent (for these cases we
  	 * don't create ordered extents).
  	 */
  	atomic_t reservations;
  
  	/*
  	 * Incremented while holding the spinlock *lock* by a task checking if
  	 * it can perform a nocow write (incremented if the value for the *ro*
  	 * field is 0). Decremented by such tasks once they create an ordered
  	 * extent or before that if some error happens before reaching that step.
  	 * This is to prevent races between block group relocation and nocow
  	 * writes through direct IO.
  	 */
  	atomic_t nocow_writers;
  
  	/* Lock for free space tree operations. */
  	struct mutex free_space_lock;
  
  	/*
  	 * Does the block group need to be added to the free space tree?
  	 * Protected by free_space_lock.
  	 */
  	int needs_free_space;
  
  	/* Record locked full stripes for RAID5/6 block group */
  	struct btrfs_full_stripe_locks_tree full_stripe_locks_root;
  };
b0643e59c   Dennis Zhou   btrfs: add the be...
179
180
181
182
  static inline u64 btrfs_block_group_end(struct btrfs_block_group *block_group)
  {
  	return (block_group->start + block_group->length);
  }
5cb0724e1   Dennis Zhou   btrfs: only keep ...
183
184
185
186
187
188
189
190
191
192
  static inline bool btrfs_is_block_group_data_only(
  					struct btrfs_block_group *block_group)
  {
  	/*
  	 * In mixed mode the fragmentation is expected to be high, lowering the
  	 * efficiency, so only proper data block groups are considered.
  	 */
  	return (block_group->flags & BTRFS_BLOCK_GROUP_DATA) &&
  	       !(block_group->flags & BTRFS_BLOCK_GROUP_METADATA);
  }
aac0023c2   Josef Bacik   btrfs: move basic...
193
194
  #ifdef CONFIG_BTRFS_DEBUG
  static inline int btrfs_should_fragment_free_space(
32da5386d   David Sterba   btrfs: rename btr...
195
  		struct btrfs_block_group *block_group)
aac0023c2   Josef Bacik   btrfs: move basic...
196
197
198
199
200
201
202
203
204
  {
  	struct btrfs_fs_info *fs_info = block_group->fs_info;
  
  	return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
  		block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
  	       (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
  		block_group->flags &  BTRFS_BLOCK_GROUP_DATA);
  }
  #endif
32da5386d   David Sterba   btrfs: rename btr...
205
  struct btrfs_block_group *btrfs_lookup_first_block_group(
2e405ad84   Josef Bacik   btrfs: migrate th...
206
  		struct btrfs_fs_info *info, u64 bytenr);
32da5386d   David Sterba   btrfs: rename btr...
207
  struct btrfs_block_group *btrfs_lookup_block_group(
2e405ad84   Josef Bacik   btrfs: migrate th...
208
  		struct btrfs_fs_info *info, u64 bytenr);
32da5386d   David Sterba   btrfs: rename btr...
209
210
211
212
  struct btrfs_block_group *btrfs_next_block_group(
  		struct btrfs_block_group *cache);
  void btrfs_get_block_group(struct btrfs_block_group *cache);
  void btrfs_put_block_group(struct btrfs_block_group *cache);
3eeb3226a   Josef Bacik   btrfs: migrate no...
213
214
  void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
  					const u64 start);
32da5386d   David Sterba   btrfs: rename btr...
215
  void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg);
3eeb3226a   Josef Bacik   btrfs: migrate no...
216
217
  bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
  void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
32da5386d   David Sterba   btrfs: rename btr...
218
219
  void btrfs_wait_nocow_writers(struct btrfs_block_group *bg);
  void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
676f1f759   Josef Bacik   btrfs: export the...
220
  				           u64 num_bytes);
32da5386d   David Sterba   btrfs: rename btr...
221
222
  int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache);
  int btrfs_cache_block_group(struct btrfs_block_group *cache,
676f1f759   Josef Bacik   btrfs: export the...
223
  			    int load_cache_only);
e3cb339fa   Josef Bacik   btrfs: export the...
224
225
  void btrfs_put_caching_control(struct btrfs_caching_control *ctl);
  struct btrfs_caching_control *btrfs_get_caching_control(
32da5386d   David Sterba   btrfs: rename btr...
226
227
  		struct btrfs_block_group *cache);
  u64 add_new_free_space(struct btrfs_block_group *block_group,
9f21246d8   Josef Bacik   btrfs: migrate th...
228
  		       u64 start, u64 end);
e3e0520b3   Josef Bacik   btrfs: migrate th...
229
230
231
232
233
234
  struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
  				struct btrfs_fs_info *fs_info,
  				const u64 chunk_offset);
  int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  			     u64 group_start, struct extent_map *em);
  void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
32da5386d   David Sterba   btrfs: rename btr...
235
  void btrfs_mark_bg_unused(struct btrfs_block_group *bg);
4358d9635   Josef Bacik   btrfs: migrate th...
236
237
238
239
  int btrfs_read_block_groups(struct btrfs_fs_info *info);
  int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
  			   u64 type, u64 chunk_offset, u64 size);
  void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans);
b12de5289   Qu Wenruo   btrfs: scrub: Don...
240
241
  int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
  			     bool do_chunk_alloc);
32da5386d   David Sterba   btrfs: rename btr...
242
  void btrfs_dec_block_group_ro(struct btrfs_block_group *cache);
77745c051   Josef Bacik   btrfs: migrate th...
243
244
245
  int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans);
  int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans);
  int btrfs_setup_space_cache(struct btrfs_trans_handle *trans);
ade4b5169   Josef Bacik   btrfs: export blo...
246
247
  int btrfs_update_block_group(struct btrfs_trans_handle *trans,
  			     u64 bytenr, u64 num_bytes, int alloc);
32da5386d   David Sterba   btrfs: rename btr...
248
  int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
ade4b5169   Josef Bacik   btrfs: export blo...
249
  			     u64 ram_bytes, u64 num_bytes, int delalloc);
32da5386d   David Sterba   btrfs: rename btr...
250
  void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
ade4b5169   Josef Bacik   btrfs: export blo...
251
  			       u64 num_bytes, int delalloc);
07730d87a   Josef Bacik   btrfs: migrate th...
252
253
254
255
  int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
  		      enum btrfs_chunk_alloc_enum force);
  int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type);
  void check_system_chunk(struct btrfs_trans_handle *trans, const u64 type);
878d7b679   Josef Bacik   btrfs: migrate th...
256
  u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags);
3e43c279e   Josef Bacik   btrfs: migrate th...
257
258
  void btrfs_put_block_group_cache(struct btrfs_fs_info *info);
  int btrfs_free_block_groups(struct btrfs_fs_info *info);
878d7b679   Josef Bacik   btrfs: migrate th...
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  
  static inline u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
  {
  	return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
  }
  
  static inline u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
  {
  	return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  }
  
  static inline u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
  {
  	return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  }
676f1f759   Josef Bacik   btrfs: export the...
274

32da5386d   David Sterba   btrfs: rename btr...
275
  static inline int btrfs_block_group_done(struct btrfs_block_group *cache)
676f1f759   Josef Bacik   btrfs: export the...
276
277
278
279
280
  {
  	smp_mb();
  	return cache->cached == BTRFS_CACHE_FINISHED ||
  		cache->cached == BTRFS_CACHE_ERROR;
  }
2e405ad84   Josef Bacik   btrfs: migrate th...
281

684b752b0   Filipe Manana   btrfs: move the b...
282
283
  void btrfs_freeze_block_group(struct btrfs_block_group *cache);
  void btrfs_unfreeze_block_group(struct btrfs_block_group *cache);
96a14336b   Nikolay Borisov   btrfs: Move and u...
284
285
286
287
  #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
  		     u64 physical, u64 **logical, int *naddrs, int *stripe_len);
  #endif
aac0023c2   Josef Bacik   btrfs: move basic...
288
  #endif /* BTRFS_BLOCK_GROUP_H */