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