1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Code for manipulating bucket marks for garbage collection. 4 * 5 * Copyright 2014 Datera, Inc. 6 */ 7 8 #ifndef _BUCKETS_H 9 #define _BUCKETS_H 10 11 #include "buckets_types.h" 12 #include "extents.h" 13 #include "sb-members.h" 14 15 static inline u64 sector_to_bucket(const struct bch_dev *ca, sector_t s) 16 { 17 return div_u64(s, ca->mi.bucket_size); 18 } 19 20 static inline sector_t bucket_to_sector(const struct bch_dev *ca, size_t b) 21 { 22 return ((sector_t) b) * ca->mi.bucket_size; 23 } 24 25 static inline sector_t bucket_remainder(const struct bch_dev *ca, sector_t s) 26 { 27 u32 remainder; 28 29 div_u64_rem(s, ca->mi.bucket_size, &remainder); 30 return remainder; 31 } 32 33 static inline u64 sector_to_bucket_and_offset(const struct bch_dev *ca, sector_t s, u32 *offset) 34 { 35 return div_u64_rem(s, ca->mi.bucket_size, offset); 36 } 37 38 #define for_each_bucket(_b, _buckets) \ 39 for (_b = (_buckets)->b + (_buckets)->first_bucket; \ 40 _b < (_buckets)->b + (_buckets)->nbuckets; _b++) 41 42 /* 43 * Ugly hack alert: 44 * 45 * We need to cram a spinlock in a single byte, because that's what we have left 46 * in struct bucket, and we care about the size of these - during fsck, we need 47 * in memory state for every single bucket on every device. 48 * 49 * We used to do 50 * while (xchg(&b->lock, 1) cpu_relax(); 51 * but, it turns out not all architectures support xchg on a single byte. 52 * 53 * So now we use bit_spin_lock(), with fun games since we can't burn a whole 54 * ulong for this - we just need to make sure the lock bit always ends up in the 55 * first byte. 56 */ 57 58 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 59 #define BUCKET_LOCK_BITNR 0 60 #else 61 #define BUCKET_LOCK_BITNR (BITS_PER_LONG - 1) 62 #endif 63 64 union ulong_byte_assert { 65 ulong ulong; 66 u8 byte; 67 }; 68 69 static inline void bucket_unlock(struct bucket *b) 70 { 71 BUILD_BUG_ON(!((union ulong_byte_assert) { .ulong = 1UL << BUCKET_LOCK_BITNR }).byte); 72 73 clear_bit_unlock(BUCKET_LOCK_BITNR, (void *) &b->lock); 74 wake_up_bit((void *) &b->lock, BUCKET_LOCK_BITNR); 75 } 76 77 static inline void bucket_lock(struct bucket *b) 78 { 79 wait_on_bit_lock((void *) &b->lock, BUCKET_LOCK_BITNR, 80 TASK_UNINTERRUPTIBLE); 81 } 82 83 static inline struct bucket_array *gc_bucket_array(struct bch_dev *ca) 84 { 85 return rcu_dereference_check(ca->buckets_gc, 86 !ca->fs || 87 percpu_rwsem_is_held(&ca->fs->mark_lock) || 88 lockdep_is_held(&ca->fs->gc_lock) || 89 lockdep_is_held(&ca->bucket_lock)); 90 } 91 92 static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b) 93 { 94 struct bucket_array *buckets = gc_bucket_array(ca); 95 96 if (b - buckets->first_bucket >= buckets->nbuckets_minus_first) 97 return NULL; 98 return buckets->b + b; 99 } 100 101 static inline struct bucket_gens *bucket_gens(struct bch_dev *ca) 102 { 103 return rcu_dereference_check(ca->bucket_gens, 104 !ca->fs || 105 percpu_rwsem_is_held(&ca->fs->mark_lock) || 106 lockdep_is_held(&ca->fs->gc_lock) || 107 lockdep_is_held(&ca->bucket_lock)); 108 } 109 110 static inline u8 *bucket_gen(struct bch_dev *ca, size_t b) 111 { 112 struct bucket_gens *gens = bucket_gens(ca); 113 114 if (b - gens->first_bucket >= gens->nbuckets_minus_first) 115 return NULL; 116 return gens->b + b; 117 } 118 119 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca, 120 const struct bch_extent_ptr *ptr) 121 { 122 return sector_to_bucket(ca, ptr->offset); 123 } 124 125 static inline struct bpos PTR_BUCKET_POS(const struct bch_dev *ca, 126 const struct bch_extent_ptr *ptr) 127 { 128 return POS(ptr->dev, PTR_BUCKET_NR(ca, ptr)); 129 } 130 131 static inline struct bpos PTR_BUCKET_POS_OFFSET(const struct bch_dev *ca, 132 const struct bch_extent_ptr *ptr, 133 u32 *bucket_offset) 134 { 135 return POS(ptr->dev, sector_to_bucket_and_offset(ca, ptr->offset, bucket_offset)); 136 } 137 138 static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca, 139 const struct bch_extent_ptr *ptr) 140 { 141 return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr)); 142 } 143 144 static inline enum bch_data_type ptr_data_type(const struct bkey *k, 145 const struct bch_extent_ptr *ptr) 146 { 147 if (bkey_is_btree_ptr(k)) 148 return BCH_DATA_btree; 149 150 return ptr->cached ? BCH_DATA_cached : BCH_DATA_user; 151 } 152 153 static inline s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p) 154 { 155 EBUG_ON(sectors < 0); 156 157 return crc_is_compressed(p.crc) 158 ? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size, 159 p.crc.uncompressed_size) 160 : sectors; 161 } 162 163 static inline int gen_cmp(u8 a, u8 b) 164 { 165 return (s8) (a - b); 166 } 167 168 static inline int gen_after(u8 a, u8 b) 169 { 170 int r = gen_cmp(a, b); 171 172 return r > 0 ? r : 0; 173 } 174 175 static inline int dev_ptr_stale_rcu(struct bch_dev *ca, const struct bch_extent_ptr *ptr) 176 { 177 u8 *gen = bucket_gen(ca, PTR_BUCKET_NR(ca, ptr)); 178 if (!gen) 179 return -1; 180 return gen_after(*gen, ptr->gen); 181 } 182 183 /** 184 * dev_ptr_stale() - check if a pointer points into a bucket that has been 185 * invalidated. 186 */ 187 static inline int dev_ptr_stale(struct bch_dev *ca, const struct bch_extent_ptr *ptr) 188 { 189 rcu_read_lock(); 190 int ret = dev_ptr_stale_rcu(ca, ptr); 191 rcu_read_unlock(); 192 193 return ret; 194 } 195 196 /* Device usage: */ 197 198 void bch2_dev_usage_read_fast(struct bch_dev *, struct bch_dev_usage *); 199 static inline struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca) 200 { 201 struct bch_dev_usage ret; 202 203 bch2_dev_usage_read_fast(ca, &ret); 204 return ret; 205 } 206 207 void bch2_dev_usage_init(struct bch_dev *); 208 void bch2_dev_usage_to_text(struct printbuf *, struct bch_dev_usage *); 209 210 static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum bch_watermark watermark) 211 { 212 s64 reserved = 0; 213 214 switch (watermark) { 215 case BCH_WATERMARK_NR: 216 BUG(); 217 case BCH_WATERMARK_stripe: 218 reserved += ca->mi.nbuckets >> 6; 219 fallthrough; 220 case BCH_WATERMARK_normal: 221 reserved += ca->mi.nbuckets >> 6; 222 fallthrough; 223 case BCH_WATERMARK_copygc: 224 reserved += ca->nr_btree_reserve; 225 fallthrough; 226 case BCH_WATERMARK_btree: 227 reserved += ca->nr_btree_reserve; 228 fallthrough; 229 case BCH_WATERMARK_btree_copygc: 230 case BCH_WATERMARK_reclaim: 231 case BCH_WATERMARK_interior_updates: 232 break; 233 } 234 235 return reserved; 236 } 237 238 static inline u64 dev_buckets_free(struct bch_dev *ca, 239 struct bch_dev_usage usage, 240 enum bch_watermark watermark) 241 { 242 return max_t(s64, 0, 243 usage.d[BCH_DATA_free].buckets - 244 ca->nr_open_buckets - 245 bch2_dev_buckets_reserved(ca, watermark)); 246 } 247 248 static inline u64 __dev_buckets_available(struct bch_dev *ca, 249 struct bch_dev_usage usage, 250 enum bch_watermark watermark) 251 { 252 return max_t(s64, 0, 253 usage.d[BCH_DATA_free].buckets 254 + usage.d[BCH_DATA_cached].buckets 255 + usage.d[BCH_DATA_need_gc_gens].buckets 256 + usage.d[BCH_DATA_need_discard].buckets 257 - ca->nr_open_buckets 258 - bch2_dev_buckets_reserved(ca, watermark)); 259 } 260 261 static inline u64 dev_buckets_available(struct bch_dev *ca, 262 enum bch_watermark watermark) 263 { 264 return __dev_buckets_available(ca, bch2_dev_usage_read(ca), watermark); 265 } 266 267 /* Filesystem usage: */ 268 269 static inline unsigned __fs_usage_u64s(unsigned nr_replicas) 270 { 271 return sizeof(struct bch_fs_usage) / sizeof(u64) + nr_replicas; 272 } 273 274 static inline unsigned fs_usage_u64s(struct bch_fs *c) 275 { 276 return __fs_usage_u64s(READ_ONCE(c->replicas.nr)); 277 } 278 279 static inline unsigned __fs_usage_online_u64s(unsigned nr_replicas) 280 { 281 return sizeof(struct bch_fs_usage_online) / sizeof(u64) + nr_replicas; 282 } 283 284 static inline unsigned fs_usage_online_u64s(struct bch_fs *c) 285 { 286 return __fs_usage_online_u64s(READ_ONCE(c->replicas.nr)); 287 } 288 289 static inline unsigned dev_usage_u64s(void) 290 { 291 return sizeof(struct bch_dev_usage) / sizeof(u64); 292 } 293 294 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *); 295 296 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *); 297 298 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned); 299 300 void bch2_fs_usage_to_text(struct printbuf *, 301 struct bch_fs *, struct bch_fs_usage_online *); 302 303 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage_online *); 304 305 struct bch_fs_usage_short 306 bch2_fs_usage_read_short(struct bch_fs *); 307 308 void bch2_dev_usage_update(struct bch_fs *, struct bch_dev *, 309 const struct bch_alloc_v4 *, 310 const struct bch_alloc_v4 *, u64, bool); 311 312 /* key/bucket marking: */ 313 314 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c, 315 unsigned journal_seq, 316 bool gc) 317 { 318 percpu_rwsem_assert_held(&c->mark_lock); 319 BUG_ON(!gc && !journal_seq); 320 321 return this_cpu_ptr(gc 322 ? c->usage_gc 323 : c->usage[journal_seq & JOURNAL_BUF_MASK]); 324 } 325 326 int bch2_update_replicas(struct bch_fs *, struct bkey_s_c, 327 struct bch_replicas_entry_v1 *, s64, 328 unsigned, bool); 329 int bch2_update_replicas_list(struct btree_trans *, 330 struct bch_replicas_entry_v1 *, s64); 331 int bch2_update_cached_sectors_list(struct btree_trans *, unsigned, s64); 332 int bch2_replicas_deltas_realloc(struct btree_trans *, unsigned); 333 334 void bch2_fs_usage_initialize(struct bch_fs *); 335 336 int bch2_bucket_ref_update(struct btree_trans *, struct bch_dev *, 337 struct bkey_s_c, const struct bch_extent_ptr *, 338 s64, enum bch_data_type, u8, u8, u32 *); 339 340 int bch2_check_fix_ptrs(struct btree_trans *, 341 enum btree_id, unsigned, struct bkey_s_c, 342 enum btree_iter_update_trigger_flags); 343 344 int bch2_trigger_extent(struct btree_trans *, enum btree_id, unsigned, 345 struct bkey_s_c, struct bkey_s, 346 enum btree_iter_update_trigger_flags); 347 int bch2_trigger_reservation(struct btree_trans *, enum btree_id, unsigned, 348 struct bkey_s_c, struct bkey_s, 349 enum btree_iter_update_trigger_flags); 350 351 #define trigger_run_overwrite_then_insert(_fn, _trans, _btree_id, _level, _old, _new, _flags)\ 352 ({ \ 353 int ret = 0; \ 354 \ 355 if (_old.k->type) \ 356 ret = _fn(_trans, _btree_id, _level, _old, _flags & ~BTREE_TRIGGER_insert); \ 357 if (!ret && _new.k->type) \ 358 ret = _fn(_trans, _btree_id, _level, _new.s_c, _flags & ~BTREE_TRIGGER_overwrite);\ 359 ret; \ 360 }) 361 362 void bch2_trans_account_disk_usage_change(struct btree_trans *); 363 364 void bch2_trans_fs_usage_revert(struct btree_trans *, struct replicas_delta_list *); 365 int bch2_trans_fs_usage_apply(struct btree_trans *, struct replicas_delta_list *); 366 367 int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *, u64, 368 enum bch_data_type, unsigned, 369 enum btree_iter_update_trigger_flags); 370 int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *, 371 enum btree_iter_update_trigger_flags); 372 int bch2_trans_mark_dev_sbs_flags(struct bch_fs *, 373 enum btree_iter_update_trigger_flags); 374 int bch2_trans_mark_dev_sbs(struct bch_fs *); 375 376 static inline bool is_superblock_bucket(struct bch_dev *ca, u64 b) 377 { 378 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout; 379 u64 b_offset = bucket_to_sector(ca, b); 380 u64 b_end = bucket_to_sector(ca, b + 1); 381 unsigned i; 382 383 if (!b) 384 return true; 385 386 for (i = 0; i < layout->nr_superblocks; i++) { 387 u64 offset = le64_to_cpu(layout->sb_offset[i]); 388 u64 end = offset + (1 << layout->sb_max_size_bits); 389 390 if (!(offset >= b_end || end <= b_offset)) 391 return true; 392 } 393 394 return false; 395 } 396 397 static inline const char *bch2_data_type_str(enum bch_data_type type) 398 { 399 return type < BCH_DATA_NR 400 ? __bch2_data_types[type] 401 : "(invalid data type)"; 402 } 403 404 /* disk reservations: */ 405 406 static inline void bch2_disk_reservation_put(struct bch_fs *c, 407 struct disk_reservation *res) 408 { 409 if (res->sectors) { 410 this_cpu_sub(*c->online_reserved, res->sectors); 411 res->sectors = 0; 412 } 413 } 414 415 #define BCH_DISK_RESERVATION_NOFAIL (1 << 0) 416 417 int __bch2_disk_reservation_add(struct bch_fs *, 418 struct disk_reservation *, 419 u64, int); 420 421 static inline int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res, 422 u64 sectors, int flags) 423 { 424 #ifdef __KERNEL__ 425 u64 old, new; 426 427 do { 428 old = this_cpu_read(c->pcpu->sectors_available); 429 if (sectors > old) 430 return __bch2_disk_reservation_add(c, res, sectors, flags); 431 432 new = old - sectors; 433 } while (this_cpu_cmpxchg(c->pcpu->sectors_available, old, new) != old); 434 435 this_cpu_add(*c->online_reserved, sectors); 436 res->sectors += sectors; 437 return 0; 438 #else 439 return __bch2_disk_reservation_add(c, res, sectors, flags); 440 #endif 441 } 442 443 static inline struct disk_reservation 444 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas) 445 { 446 return (struct disk_reservation) { 447 .sectors = 0, 448 #if 0 449 /* not used yet: */ 450 .gen = c->capacity_gen, 451 #endif 452 .nr_replicas = nr_replicas, 453 }; 454 } 455 456 static inline int bch2_disk_reservation_get(struct bch_fs *c, 457 struct disk_reservation *res, 458 u64 sectors, unsigned nr_replicas, 459 int flags) 460 { 461 *res = bch2_disk_reservation_init(c, nr_replicas); 462 463 return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags); 464 } 465 466 #define RESERVE_FACTOR 6 467 468 static inline u64 avail_factor(u64 r) 469 { 470 return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1); 471 } 472 473 void bch2_buckets_nouse_free(struct bch_fs *); 474 int bch2_buckets_nouse_alloc(struct bch_fs *); 475 476 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64); 477 void bch2_dev_buckets_free(struct bch_dev *); 478 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *); 479 480 #endif /* _BUCKETS_H */ 481