at v6.10 322 lines 9.1 kB view raw
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 11enum bch_validate_flags; 12 13/* How out of date a pointer gen is allowed to be: */ 14#define BUCKET_GC_GEN_MAX 96U 15 16static 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 25static inline u64 bucket_to_u64(struct bpos bucket) 26{ 27 return (bucket.inode << 48) | bucket.offset; 28} 29 30static inline struct bpos u64_to_bucket(u64 bucket) 31{ 32 return POS(bucket >> 48, bucket & ~(~0ULL << 48)); 33} 34 35static inline u8 alloc_gc_gen(struct bch_alloc_v4 a) 36{ 37 return a.gen - a.oldest_gen; 38} 39 40static 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->dirty_sectors = src.dirty_sectors; 45 dst->cached_sectors = src.cached_sectors; 46 dst->stripe = src.stripe; 47} 48 49static inline void __bucket_m_to_alloc(struct bch_alloc_v4 *dst, struct bucket src) 50{ 51 dst->gen = src.gen; 52 dst->data_type = src.data_type; 53 dst->dirty_sectors = src.dirty_sectors; 54 dst->cached_sectors = src.cached_sectors; 55 dst->stripe = src.stripe; 56} 57 58static 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 65static 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 76static 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 83static inline unsigned bch2_bucket_sectors_total(struct bch_alloc_v4 a) 84{ 85 return a.dirty_sectors + a.cached_sectors; 86} 87 88static inline unsigned bch2_bucket_sectors_dirty(struct bch_alloc_v4 a) 89{ 90 return a.dirty_sectors; 91} 92 93static inline unsigned bch2_bucket_sectors_fragmented(struct bch_dev *ca, 94 struct bch_alloc_v4 a) 95{ 96 int d = bch2_bucket_sectors_dirty(a); 97 98 return d ? max(0, ca->mi.bucket_size - d) : 0; 99} 100 101static inline enum bch_data_type alloc_data_type(struct bch_alloc_v4 a, 102 enum bch_data_type data_type) 103{ 104 if (a.stripe) 105 return data_type == BCH_DATA_parity ? data_type : BCH_DATA_stripe; 106 if (a.dirty_sectors) 107 return data_type; 108 if (a.cached_sectors) 109 return BCH_DATA_cached; 110 if (BCH_ALLOC_V4_NEED_DISCARD(&a)) 111 return BCH_DATA_need_discard; 112 if (alloc_gc_gen(a) >= BUCKET_GC_GEN_MAX) 113 return BCH_DATA_need_gc_gens; 114 return BCH_DATA_free; 115} 116 117static inline void alloc_data_type_set(struct bch_alloc_v4 *a, enum bch_data_type data_type) 118{ 119 a->data_type = alloc_data_type(*a, data_type); 120} 121 122static inline u64 alloc_lru_idx_read(struct bch_alloc_v4 a) 123{ 124 return a.data_type == BCH_DATA_cached ? a.io_time[READ] : 0; 125} 126 127#define DATA_TYPES_MOVABLE \ 128 ((1U << BCH_DATA_btree)| \ 129 (1U << BCH_DATA_user)| \ 130 (1U << BCH_DATA_stripe)) 131 132static inline bool data_type_movable(enum bch_data_type type) 133{ 134 return (1U << type) & DATA_TYPES_MOVABLE; 135} 136 137static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a, 138 struct bch_dev *ca) 139{ 140 if (!data_type_movable(a.data_type) || 141 !bch2_bucket_sectors_fragmented(ca, a)) 142 return 0; 143 144 /* 145 * avoid overflowing LRU_TIME_BITS on a corrupted fs, when 146 * bucket_sectors_dirty is (much) bigger than bucket_size 147 */ 148 u64 d = min(bch2_bucket_sectors_dirty(a), 149 ca->mi.bucket_size); 150 151 return div_u64(d * (1ULL << 31), ca->mi.bucket_size); 152} 153 154static inline u64 alloc_freespace_genbits(struct bch_alloc_v4 a) 155{ 156 return ((u64) alloc_gc_gen(a) >> 4) << 56; 157} 158 159static inline struct bpos alloc_freespace_pos(struct bpos pos, struct bch_alloc_v4 a) 160{ 161 pos.offset |= alloc_freespace_genbits(a); 162 return pos; 163} 164 165static inline unsigned alloc_v4_u64s_noerror(const struct bch_alloc_v4 *a) 166{ 167 return (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?: 168 BCH_ALLOC_V4_U64s_V0) + 169 BCH_ALLOC_V4_NR_BACKPOINTERS(a) * 170 (sizeof(struct bch_backpointer) / sizeof(u64)); 171} 172 173static inline unsigned alloc_v4_u64s(const struct bch_alloc_v4 *a) 174{ 175 unsigned ret = alloc_v4_u64s_noerror(a); 176 BUG_ON(ret > U8_MAX - BKEY_U64s); 177 return ret; 178} 179 180static inline void set_alloc_v4_u64s(struct bkey_i_alloc_v4 *a) 181{ 182 set_bkey_val_u64s(&a->k, alloc_v4_u64s(&a->v)); 183} 184 185struct bkey_i_alloc_v4 * 186bch2_trans_start_alloc_update_noupdate(struct btree_trans *, struct btree_iter *, struct bpos); 187struct bkey_i_alloc_v4 * 188bch2_trans_start_alloc_update(struct btree_trans *, struct bpos); 189 190void __bch2_alloc_to_v4(struct bkey_s_c, struct bch_alloc_v4 *); 191 192static inline const struct bch_alloc_v4 *bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *convert) 193{ 194 const struct bch_alloc_v4 *ret; 195 196 if (unlikely(k.k->type != KEY_TYPE_alloc_v4)) 197 goto slowpath; 198 199 ret = bkey_s_c_to_alloc_v4(k).v; 200 if (BCH_ALLOC_V4_BACKPOINTERS_START(ret) != BCH_ALLOC_V4_U64s) 201 goto slowpath; 202 203 return ret; 204slowpath: 205 __bch2_alloc_to_v4(k, convert); 206 return convert; 207} 208 209struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *, struct bkey_s_c); 210 211int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int); 212 213int bch2_alloc_v1_invalid(struct bch_fs *, struct bkey_s_c, 214 enum bch_validate_flags, struct printbuf *); 215int bch2_alloc_v2_invalid(struct bch_fs *, struct bkey_s_c, 216 enum bch_validate_flags, struct printbuf *); 217int bch2_alloc_v3_invalid(struct bch_fs *, struct bkey_s_c, 218 enum bch_validate_flags, struct printbuf *); 219int bch2_alloc_v4_invalid(struct bch_fs *, struct bkey_s_c, 220 enum bch_validate_flags, struct printbuf *); 221void bch2_alloc_v4_swab(struct bkey_s); 222void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c); 223 224#define bch2_bkey_ops_alloc ((struct bkey_ops) { \ 225 .key_invalid = bch2_alloc_v1_invalid, \ 226 .val_to_text = bch2_alloc_to_text, \ 227 .trigger = bch2_trigger_alloc, \ 228 .min_val_size = 8, \ 229}) 230 231#define bch2_bkey_ops_alloc_v2 ((struct bkey_ops) { \ 232 .key_invalid = bch2_alloc_v2_invalid, \ 233 .val_to_text = bch2_alloc_to_text, \ 234 .trigger = bch2_trigger_alloc, \ 235 .min_val_size = 8, \ 236}) 237 238#define bch2_bkey_ops_alloc_v3 ((struct bkey_ops) { \ 239 .key_invalid = bch2_alloc_v3_invalid, \ 240 .val_to_text = bch2_alloc_to_text, \ 241 .trigger = bch2_trigger_alloc, \ 242 .min_val_size = 16, \ 243}) 244 245#define bch2_bkey_ops_alloc_v4 ((struct bkey_ops) { \ 246 .key_invalid = bch2_alloc_v4_invalid, \ 247 .val_to_text = bch2_alloc_to_text, \ 248 .swab = bch2_alloc_v4_swab, \ 249 .trigger = bch2_trigger_alloc, \ 250 .min_val_size = 48, \ 251}) 252 253int bch2_bucket_gens_invalid(struct bch_fs *, struct bkey_s_c, 254 enum bch_validate_flags, struct printbuf *); 255void bch2_bucket_gens_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c); 256 257#define bch2_bkey_ops_bucket_gens ((struct bkey_ops) { \ 258 .key_invalid = bch2_bucket_gens_invalid, \ 259 .val_to_text = bch2_bucket_gens_to_text, \ 260}) 261 262int bch2_bucket_gens_init(struct bch_fs *); 263 264static inline bool bkey_is_alloc(const struct bkey *k) 265{ 266 return k->type == KEY_TYPE_alloc || 267 k->type == KEY_TYPE_alloc_v2 || 268 k->type == KEY_TYPE_alloc_v3; 269} 270 271int bch2_alloc_read(struct bch_fs *); 272 273int bch2_trigger_alloc(struct btree_trans *, enum btree_id, unsigned, 274 struct bkey_s_c, struct bkey_s, 275 enum btree_iter_update_trigger_flags); 276int bch2_check_alloc_info(struct bch_fs *); 277int bch2_check_alloc_to_lru_refs(struct bch_fs *); 278void bch2_dev_do_discards(struct bch_dev *); 279void bch2_do_discards(struct bch_fs *); 280 281static inline u64 should_invalidate_buckets(struct bch_dev *ca, 282 struct bch_dev_usage u) 283{ 284 u64 want_free = ca->mi.nbuckets >> 7; 285 u64 free = max_t(s64, 0, 286 u.d[BCH_DATA_free].buckets 287 + u.d[BCH_DATA_need_discard].buckets 288 - bch2_dev_buckets_reserved(ca, BCH_WATERMARK_stripe)); 289 290 return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets); 291} 292 293void bch2_dev_do_invalidates(struct bch_dev *); 294void bch2_do_invalidates(struct bch_fs *); 295 296static inline struct bch_backpointer *alloc_v4_backpointers(struct bch_alloc_v4 *a) 297{ 298 return (void *) ((u64 *) &a->v + 299 (BCH_ALLOC_V4_BACKPOINTERS_START(a) ?: 300 BCH_ALLOC_V4_U64s_V0)); 301} 302 303static inline const struct bch_backpointer *alloc_v4_backpointers_c(const struct bch_alloc_v4 *a) 304{ 305 return (void *) ((u64 *) &a->v + BCH_ALLOC_V4_BACKPOINTERS_START(a)); 306} 307 308int bch2_dev_freespace_init(struct bch_fs *, struct bch_dev *, u64, u64); 309int bch2_fs_freespace_init(struct bch_fs *); 310 311void bch2_recalc_capacity(struct bch_fs *); 312u64 bch2_min_rw_member_capacity(struct bch_fs *); 313 314void bch2_dev_allocator_remove(struct bch_fs *, struct bch_dev *); 315void bch2_dev_allocator_add(struct bch_fs *, struct bch_dev *); 316 317void bch2_dev_allocator_background_exit(struct bch_dev *); 318void bch2_dev_allocator_background_init(struct bch_dev *); 319 320void bch2_fs_allocator_background_init(struct bch_fs *); 321 322#endif /* _BCACHEFS_ALLOC_BACKGROUND_H */