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