1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_ALLOC_BACKGROUND_H 3 #define _BCACHEFS_ALLOC_BACKGROUND_H 4 5 #include "bcachefs.h" 6 #include "alloc_types.h" 7 #include "buckets.h" 8 #include "debug.h" 9 #include "super.h" 10 11 /* How out of date a pointer gen is allowed to be: */ 12 #define BUCKET_GC_GEN_MAX 96U 13 14 static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos) 15 { 16 rcu_read_lock(); 17 struct bch_dev *ca = bch2_dev_rcu_noerror(c, pos.inode); 18 bool ret = ca && bucket_valid(ca, pos.offset); 19 rcu_read_unlock(); 20 return ret; 21 } 22 23 static inline u64 bucket_to_u64(struct bpos bucket) 24 { 25 return (bucket.inode << 48) | bucket.offset; 26 } 27 28 static inline struct bpos u64_to_bucket(u64 bucket) 29 { 30 return POS(bucket >> 48, bucket & ~(~0ULL << 48)); 31 } 32 33 static inline u8 alloc_gc_gen(struct bch_alloc_v4 a) 34 { 35 return a.gen - a.oldest_gen; 36 } 37 38 static inline void alloc_to_bucket(struct bucket *dst, struct bch_alloc_v4 src) 39 { 40 dst->gen = src.gen; 41 dst->data_type = src.data_type; 42 dst->stripe_sectors = src.stripe_sectors; 43 dst->dirty_sectors = src.dirty_sectors; 44 dst->cached_sectors = src.cached_sectors; 45 dst->stripe = src.stripe; 46 } 47 48 static inline void __bucket_m_to_alloc(struct bch_alloc_v4 *dst, struct bucket src) 49 { 50 dst->gen = src.gen; 51 dst->data_type = src.data_type; 52 dst->stripe_sectors = src.stripe_sectors; 53 dst->dirty_sectors = src.dirty_sectors; 54 dst->cached_sectors = src.cached_sectors; 55 dst->stripe = src.stripe; 56 } 57 58 static inline struct bch_alloc_v4 bucket_m_to_alloc(struct bucket b) 59 { 60 struct bch_alloc_v4 ret = {}; 61 __bucket_m_to_alloc(&ret, b); 62 return ret; 63 } 64 65 static inline enum bch_data_type bucket_data_type(enum bch_data_type data_type) 66 { 67 switch (data_type) { 68 case BCH_DATA_cached: 69 case BCH_DATA_stripe: 70 return BCH_DATA_user; 71 default: 72 return data_type; 73 } 74 } 75 76 static inline bool bucket_data_type_mismatch(enum bch_data_type bucket, 77 enum bch_data_type ptr) 78 { 79 return !data_type_is_empty(bucket) && 80 bucket_data_type(bucket) != bucket_data_type(ptr); 81 } 82 83 /* 84 * It is my general preference to use unsigned types for unsigned quantities - 85 * however, these helpers are used in disk accounting calculations run by 86 * triggers where the output will be negated and added to an s64. unsigned is 87 * right out even though all these quantities will fit in 32 bits, since it 88 * won't be sign extended correctly; u64 will negate "correctly", but s64 is the 89 * simpler option here. 90 */ 91 static inline s64 bch2_bucket_sectors_total(struct bch_alloc_v4 a) 92 { 93 return a.stripe_sectors + a.dirty_sectors + a.cached_sectors; 94 } 95 96 static inline s64 bch2_bucket_sectors_dirty(struct bch_alloc_v4 a) 97 { 98 return a.stripe_sectors + a.dirty_sectors; 99 } 100 101 static inline s64 bch2_bucket_sectors(struct bch_alloc_v4 a) 102 { 103 return a.data_type == BCH_DATA_cached 104 ? a.cached_sectors 105 : bch2_bucket_sectors_dirty(a); 106 } 107 108 static inline s64 bch2_bucket_sectors_fragmented(struct bch_dev *ca, 109 struct bch_alloc_v4 a) 110 { 111 int d = bch2_bucket_sectors(a); 112 113 return d ? max(0, ca->mi.bucket_size - d) : 0; 114 } 115 116 static inline s64 bch2_gc_bucket_sectors_fragmented(struct bch_dev *ca, struct bucket a) 117 { 118 int d = a.stripe_sectors + a.dirty_sectors; 119 120 return d ? max(0, ca->mi.bucket_size - d) : 0; 121 } 122 123 static inline s64 bch2_bucket_sectors_unstriped(struct bch_alloc_v4 a) 124 { 125 return a.data_type == BCH_DATA_stripe ? a.dirty_sectors : 0; 126 } 127 128 static inline enum bch_data_type alloc_data_type(struct bch_alloc_v4 a, 129 enum bch_data_type data_type) 130 { 131 if (a.stripe) 132 return data_type == BCH_DATA_parity ? data_type : BCH_DATA_stripe; 133 if (bch2_bucket_sectors_dirty(a)) 134 return data_type; 135 if (a.cached_sectors) 136 return BCH_DATA_cached; 137 if (BCH_ALLOC_V4_NEED_DISCARD(&a)) 138 return BCH_DATA_need_discard; 139 if (alloc_gc_gen(a) >= BUCKET_GC_GEN_MAX) 140 return BCH_DATA_need_gc_gens; 141 return BCH_DATA_free; 142 } 143 144 static inline void alloc_data_type_set(struct bch_alloc_v4 *a, enum bch_data_type data_type) 145 { 146 a->data_type = alloc_data_type(*a, data_type); 147 } 148 149 static inline u64 alloc_lru_idx_read(struct bch_alloc_v4 a) 150 { 151 return a.data_type == BCH_DATA_cached 152 ? a.io_time[READ] & LRU_TIME_MAX 153 : 0; 154 } 155 156 #define DATA_TYPES_MOVABLE \ 157 ((1U << BCH_DATA_btree)| \ 158 (1U << BCH_DATA_user)| \ 159 (1U << BCH_DATA_stripe)) 160 161 static inline bool data_type_movable(enum bch_data_type type) 162 { 163 return (1U << type) & DATA_TYPES_MOVABLE; 164 } 165 166 static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a, 167 struct bch_dev *ca) 168 { 169 if (a.data_type >= BCH_DATA_NR) 170 return 0; 171 172 if (!data_type_movable(a.data_type) || 173 !bch2_bucket_sectors_fragmented(ca, a)) 174 return 0; 175 176 /* 177 * avoid overflowing LRU_TIME_BITS on a corrupted fs, when 178 * bucket_sectors_dirty is (much) bigger than bucket_size 179 */ 180 u64 d = min_t(s64, bch2_bucket_sectors_dirty(a), 181 ca->mi.bucket_size); 182 183 return div_u64(d * (1ULL << 31), ca->mi.bucket_size); 184 } 185 186 static inline u64 alloc_freespace_genbits(struct bch_alloc_v4 a) 187 { 188 return ((u64) alloc_gc_gen(a) >> 4) << 56; 189 } 190 191 static inline struct bpos alloc_freespace_pos(struct bpos pos, struct bch_alloc_v4 a) 192 { 193 pos.offset |= alloc_freespace_genbits(a); 194 return pos; 195 } 196 197 static inline unsigned alloc_v4_u64s_noerror(const struct bch_alloc_v4 *a) 198 { 199 return (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?: 200 BCH_ALLOC_V4_U64s_V0) + 201 BCH_ALLOC_V4_NR_BACKPOINTERS(a) * 202 (sizeof(struct bch_backpointer) / sizeof(u64)); 203 } 204 205 static inline unsigned alloc_v4_u64s(const struct bch_alloc_v4 *a) 206 { 207 unsigned ret = alloc_v4_u64s_noerror(a); 208 BUG_ON(ret > U8_MAX - BKEY_U64s); 209 return ret; 210 } 211 212 static inline void set_alloc_v4_u64s(struct bkey_i_alloc_v4 *a) 213 { 214 set_bkey_val_u64s(&a->k, alloc_v4_u64s(&a->v)); 215 } 216 217 struct bkey_i_alloc_v4 * 218 bch2_trans_start_alloc_update_noupdate(struct btree_trans *, struct btree_iter *, struct bpos); 219 struct bkey_i_alloc_v4 * 220 bch2_trans_start_alloc_update(struct btree_trans *, struct bpos, 221 enum btree_iter_update_trigger_flags); 222 223 void __bch2_alloc_to_v4(struct bkey_s_c, struct bch_alloc_v4 *); 224 225 static inline const struct bch_alloc_v4 *bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *convert) 226 { 227 const struct bch_alloc_v4 *ret; 228 229 if (unlikely(k.k->type != KEY_TYPE_alloc_v4)) 230 goto slowpath; 231 232 ret = bkey_s_c_to_alloc_v4(k).v; 233 if (BCH_ALLOC_V4_BACKPOINTERS_START(ret) != BCH_ALLOC_V4_U64s) 234 goto slowpath; 235 236 return ret; 237 slowpath: 238 __bch2_alloc_to_v4(k, convert); 239 return convert; 240 } 241 242 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *, struct bkey_s_c); 243 244 int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int); 245 246 int bch2_alloc_v1_validate(struct bch_fs *, struct bkey_s_c, 247 struct bkey_validate_context); 248 int bch2_alloc_v2_validate(struct bch_fs *, struct bkey_s_c, 249 struct bkey_validate_context); 250 int bch2_alloc_v3_validate(struct bch_fs *, struct bkey_s_c, 251 struct bkey_validate_context); 252 int bch2_alloc_v4_validate(struct bch_fs *, struct bkey_s_c, 253 struct bkey_validate_context); 254 void bch2_alloc_v4_swab(struct bkey_s); 255 void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c); 256 257 #define bch2_bkey_ops_alloc ((struct bkey_ops) { \ 258 .key_validate = bch2_alloc_v1_validate, \ 259 .val_to_text = bch2_alloc_to_text, \ 260 .trigger = bch2_trigger_alloc, \ 261 .min_val_size = 8, \ 262 }) 263 264 #define bch2_bkey_ops_alloc_v2 ((struct bkey_ops) { \ 265 .key_validate = bch2_alloc_v2_validate, \ 266 .val_to_text = bch2_alloc_to_text, \ 267 .trigger = bch2_trigger_alloc, \ 268 .min_val_size = 8, \ 269 }) 270 271 #define bch2_bkey_ops_alloc_v3 ((struct bkey_ops) { \ 272 .key_validate = bch2_alloc_v3_validate, \ 273 .val_to_text = bch2_alloc_to_text, \ 274 .trigger = bch2_trigger_alloc, \ 275 .min_val_size = 16, \ 276 }) 277 278 #define bch2_bkey_ops_alloc_v4 ((struct bkey_ops) { \ 279 .key_validate = bch2_alloc_v4_validate, \ 280 .val_to_text = bch2_alloc_to_text, \ 281 .swab = bch2_alloc_v4_swab, \ 282 .trigger = bch2_trigger_alloc, \ 283 .min_val_size = 48, \ 284 }) 285 286 int bch2_bucket_gens_validate(struct bch_fs *, struct bkey_s_c, 287 struct bkey_validate_context); 288 void bch2_bucket_gens_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c); 289 290 #define bch2_bkey_ops_bucket_gens ((struct bkey_ops) { \ 291 .key_validate = bch2_bucket_gens_validate, \ 292 .val_to_text = bch2_bucket_gens_to_text, \ 293 }) 294 295 int bch2_bucket_gens_init(struct bch_fs *); 296 297 static inline bool bkey_is_alloc(const struct bkey *k) 298 { 299 return k->type == KEY_TYPE_alloc || 300 k->type == KEY_TYPE_alloc_v2 || 301 k->type == KEY_TYPE_alloc_v3; 302 } 303 304 int bch2_alloc_read(struct bch_fs *); 305 306 int bch2_alloc_key_to_dev_counters(struct btree_trans *, struct bch_dev *, 307 const struct bch_alloc_v4 *, 308 const struct bch_alloc_v4 *, unsigned); 309 int bch2_trigger_alloc(struct btree_trans *, enum btree_id, unsigned, 310 struct bkey_s_c, struct bkey_s, 311 enum btree_iter_update_trigger_flags); 312 313 int bch2_check_discard_freespace_key(struct btree_trans *, struct btree_iter *, u8 *, bool); 314 int bch2_check_alloc_info(struct bch_fs *); 315 int bch2_check_alloc_to_lru_refs(struct bch_fs *); 316 void bch2_dev_do_discards(struct bch_dev *); 317 void bch2_do_discards(struct bch_fs *); 318 319 static inline u64 should_invalidate_buckets(struct bch_dev *ca, 320 struct bch_dev_usage u) 321 { 322 u64 want_free = ca->mi.nbuckets >> 7; 323 u64 free = max_t(s64, 0, 324 u.d[BCH_DATA_free].buckets 325 + u.d[BCH_DATA_need_discard].buckets 326 - bch2_dev_buckets_reserved(ca, BCH_WATERMARK_stripe)); 327 328 return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets); 329 } 330 331 void bch2_dev_do_invalidates(struct bch_dev *); 332 void bch2_do_invalidates(struct bch_fs *); 333 334 static inline struct bch_backpointer *alloc_v4_backpointers(struct bch_alloc_v4 *a) 335 { 336 return (void *) ((u64 *) &a->v + 337 (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?: 338 BCH_ALLOC_V4_U64s_V0)); 339 } 340 341 static inline const struct bch_backpointer *alloc_v4_backpointers_c(const struct bch_alloc_v4 *a) 342 { 343 return (void *) ((u64 *) &a->v + BCH_ALLOC_V4_BACKPOINTERS_START(a)); 344 } 345 346 int bch2_dev_freespace_init(struct bch_fs *, struct bch_dev *, u64, u64); 347 int bch2_fs_freespace_init(struct bch_fs *); 348 int bch2_dev_remove_alloc(struct bch_fs *, struct bch_dev *); 349 350 void bch2_recalc_capacity(struct bch_fs *); 351 u64 bch2_min_rw_member_capacity(struct bch_fs *); 352 353 void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *); 354 void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *); 355 356 void bch2_dev_allocator_background_exit(struct bch_dev *); 357 void bch2_dev_allocator_background_init(struct bch_dev *); 358 359 void bch2_fs_allocator_background_init(struct bch_fs *); 360 361 #endif /* _BCACHEFS_ALLOC_BACKGROUND_H */ 362