2 * Code for manipulating bucket marks for garbage collection.
4 * Copyright 2014 Datera, Inc.
7 * - free bucket: mark == 0
8 * The bucket contains no data and will not be read
10 * - allocator bucket: owned_by_allocator == 1
11 * The bucket is on a free list, or it is an open bucket
13 * - cached bucket: owned_by_allocator == 0 &&
14 * dirty_sectors == 0 &&
16 * The bucket contains data but may be safely discarded as there are
17 * enough replicas of the data on other cache devices, or it has been
18 * written back to the backing device
20 * - dirty bucket: owned_by_allocator == 0 &&
22 * The bucket contains data that we must not discard (either only copy,
23 * or one of the 'main copies' for data requiring multiple replicas)
25 * - metadata bucket: owned_by_allocator == 0 && is_metadata == 1
26 * This is a btree node, journal or gen/prio bucket
30 * bucket invalidated => bucket on freelist => open bucket =>
31 * [dirty bucket =>] cached bucket => bucket invalidated => ...
33 * Note that cache promotion can skip the dirty bucket step, as data
34 * is copied from a deeper tier to a shallower tier, onto a cached
36 * Note also that a cached bucket can spontaneously become dirty --
39 * Only a traversal of the key space can determine whether a bucket is
40 * truly dirty or cached.
44 * - free => allocator: bucket was invalidated
45 * - cached => allocator: bucket was invalidated
47 * - allocator => dirty: open bucket was filled up
48 * - allocator => cached: open bucket was filled up
49 * - allocator => metadata: metadata was allocated
51 * - dirty => cached: dirty sectors were copied to a deeper tier
52 * - dirty => free: dirty sectors were overwritten or moved (copy gc)
53 * - cached => free: cached sectors were overwritten
55 * - metadata => free: metadata was freed
58 * - cached => dirty: a device was removed so formerly replicated data
59 * is no longer sufficiently replicated
60 * - free => cached: cannot happen
61 * - free => dirty: cannot happen
62 * - free => metadata: cannot happen
70 #include <linux/preempt.h>
71 #include <trace/events/bcache.h>
75 #define lg_local_lock lg_global_lock
76 #define lg_local_unlock lg_global_unlock
78 static void bch_fs_stats_verify(struct cache_set *c)
80 struct bucket_stats_cache_set stats =
81 __bch_bucket_stats_read_cache_set(c);
83 if ((s64) stats.sectors_dirty < 0)
84 panic("sectors_dirty underflow: %lli\n", stats.sectors_dirty);
86 if ((s64) stats.sectors_cached < 0)
87 panic("sectors_cached underflow: %lli\n", stats.sectors_cached);
89 if ((s64) stats.sectors_meta < 0)
90 panic("sectors_meta underflow: %lli\n", stats.sectors_meta);
92 if ((s64) stats.sectors_persistent_reserved < 0)
93 panic("sectors_persistent_reserved underflow: %lli\n", stats.sectors_persistent_reserved);
95 if ((s64) stats.sectors_online_reserved < 0)
96 panic("sectors_online_reserved underflow: %lli\n", stats.sectors_online_reserved);
101 static void bch_fs_stats_verify(struct cache_set *c) {}
105 void bch_bucket_seq_cleanup(struct cache_set *c)
107 u16 last_seq_ondisk = c->journal.last_seq_ondisk;
110 struct bucket_mark m;
113 for_each_cache(ca, c, i)
114 for_each_bucket(g, ca) {
115 bucket_cmpxchg(g, m, ({
116 if (!m.wait_on_journal ||
117 ((s16) last_seq_ondisk -
118 (s16) m.journal_seq < 0))
121 m.wait_on_journal = 0;
126 #define bucket_stats_add(_acc, _stats) \
128 typeof(_acc) _a = (_acc), _s = (_stats); \
131 for (i = 0; i < sizeof(*_a) / sizeof(u64); i++) \
132 ((u64 *) (_a))[i] += ((u64 *) (_s))[i]; \
135 #define bucket_stats_read_raw(_stats) \
137 typeof(*this_cpu_ptr(_stats)) _acc = { 0 }; \
140 for_each_possible_cpu(cpu) \
141 bucket_stats_add(&_acc, per_cpu_ptr((_stats), cpu)); \
146 #define bucket_stats_read_cached(_c, _cached, _uncached) \
148 typeof(_cached) _ret; \
152 _seq = read_seqcount_begin(&(_c)->gc_pos_lock); \
153 _ret = (_c)->gc_pos.phase == GC_PHASE_DONE \
154 ? bucket_stats_read_raw(_uncached) \
156 } while (read_seqcount_retry(&(_c)->gc_pos_lock, _seq)); \
161 struct bucket_stats_cache __bch_bucket_stats_read_cache(struct cache *ca)
163 return bucket_stats_read_raw(ca->bucket_stats_percpu);
166 struct bucket_stats_cache bch_bucket_stats_read_cache(struct cache *ca)
168 return bucket_stats_read_cached(ca->set,
169 ca->bucket_stats_cached,
170 ca->bucket_stats_percpu);
173 struct bucket_stats_cache_set
174 __bch_bucket_stats_read_cache_set(struct cache_set *c)
176 return bucket_stats_read_raw(c->bucket_stats_percpu);
179 struct bucket_stats_cache_set
180 bch_bucket_stats_read_cache_set(struct cache_set *c)
182 return bucket_stats_read_cached(c,
183 c->bucket_stats_cached,
184 c->bucket_stats_percpu);
187 static inline int is_meta_bucket(struct bucket_mark m)
189 return !m.owned_by_allocator && m.is_metadata;
192 static inline int is_dirty_bucket(struct bucket_mark m)
194 return !m.owned_by_allocator && !m.is_metadata && !!m.dirty_sectors;
197 static inline int is_cached_bucket(struct bucket_mark m)
199 return !m.owned_by_allocator && !m.dirty_sectors && !!m.cached_sectors;
202 void bch_fs_stats_apply(struct cache_set *c,
203 struct bucket_stats_cache_set *stats,
204 struct disk_reservation *disk_res,
205 struct gc_pos gc_pos)
208 stats->s[S_COMPRESSED][S_META] +
209 stats->s[S_COMPRESSED][S_DIRTY] +
210 stats->persistent_reserved +
211 stats->online_reserved;
214 * Not allowed to reduce sectors_available except by getting a
217 BUG_ON(added > (s64) (disk_res ? disk_res->sectors : 0));
220 disk_res->sectors -= added;
221 stats->online_reserved -= added;
224 lg_local_lock(&c->bucket_stats_lock);
225 /* online_reserved not subject to gc: */
226 this_cpu_ptr(c->bucket_stats_percpu)->online_reserved +=
227 stats->online_reserved;
228 stats->online_reserved = 0;
230 if (!gc_will_visit(c, gc_pos))
231 bucket_stats_add(this_cpu_ptr(c->bucket_stats_percpu), stats);
233 bch_fs_stats_verify(c);
234 lg_local_unlock(&c->bucket_stats_lock);
236 memset(stats, 0, sizeof(*stats));
239 static void bucket_stats_update(struct cache *ca,
240 struct bucket_mark old, struct bucket_mark new,
241 bool may_make_unavailable,
242 struct bucket_stats_cache_set *bch_alloc_stats)
244 struct cache_set *c = ca->set;
245 struct bucket_stats_cache *cache_stats;
247 BUG_ON(!may_make_unavailable &&
248 is_available_bucket(old) &&
249 !is_available_bucket(new) &&
250 c->gc_pos.phase == GC_PHASE_DONE);
252 if (bch_alloc_stats) {
253 bch_alloc_stats->s[S_COMPRESSED][S_CACHED] +=
254 (int) new.cached_sectors - (int) old.cached_sectors;
256 bch_alloc_stats->s[S_COMPRESSED]
257 [old.is_metadata ? S_META : S_DIRTY] -=
260 bch_alloc_stats->s[S_COMPRESSED]
261 [new.is_metadata ? S_META : S_DIRTY] +=
266 cache_stats = this_cpu_ptr(ca->bucket_stats_percpu);
268 cache_stats->sectors_cached +=
269 (int) new.cached_sectors - (int) old.cached_sectors;
272 cache_stats->sectors_meta -= old.dirty_sectors;
274 cache_stats->sectors_dirty -= old.dirty_sectors;
277 cache_stats->sectors_meta += new.dirty_sectors;
279 cache_stats->sectors_dirty += new.dirty_sectors;
281 cache_stats->buckets_alloc +=
282 (int) new.owned_by_allocator - (int) old.owned_by_allocator;
284 cache_stats->buckets_meta += is_meta_bucket(new) - is_meta_bucket(old);
285 cache_stats->buckets_cached += is_cached_bucket(new) - is_cached_bucket(old);
286 cache_stats->buckets_dirty += is_dirty_bucket(new) - is_dirty_bucket(old);
289 if (!is_available_bucket(old) && is_available_bucket(new))
290 bch_wake_allocator(ca);
293 void bch_invalidate_bucket(struct cache *ca, struct bucket *g)
295 struct bucket_stats_cache_set stats = { 0 };
296 struct bucket_mark old, new;
298 old = bucket_cmpxchg(g, new, ({
299 new.owned_by_allocator = 1;
301 new.cached_sectors = 0;
302 new.dirty_sectors = 0;
307 BUG_ON(old.dirty_sectors);
309 bucket_stats_update(ca, old, new, true, &stats);
314 * Only stats.sectors_cached should be nonzero: this is important
315 * because in this path we modify bch_alloc_stats based on how the
316 * bucket_mark was modified, and the sector counts in bucket_mark are
317 * subject to (saturating) overflow - and if they did overflow, the
318 * cache set stats will now be off. We can tolerate this for
319 * sectors_cached, but not anything else:
321 stats.s[S_COMPRESSED][S_CACHED] = 0;
322 stats.s[S_UNCOMPRESSED][S_CACHED] = 0;
323 BUG_ON(!bch_is_zero(&stats, sizeof(stats)));
325 if (!old.owned_by_allocator && old.cached_sectors)
326 trace_bcache_invalidate(ca, g - ca->buckets,
330 void bch_mark_free_bucket(struct cache *ca, struct bucket *g)
332 struct bucket_stats_cache_set stats = { 0 };
333 struct bucket_mark old, new;
335 old = bucket_cmpxchg(g, new, ({
336 new.owned_by_allocator = 0;
338 new.cached_sectors = 0;
339 new.dirty_sectors = 0;
342 bucket_stats_update(ca, old, new, false, &stats);
345 void bch_mark_alloc_bucket(struct cache *ca, struct bucket *g,
346 bool owned_by_allocator)
348 struct bucket_stats_cache_set stats = { 0 };
349 struct bucket_mark old, new;
351 old = bucket_cmpxchg(g, new, new.owned_by_allocator = owned_by_allocator);
353 bucket_stats_update(ca, old, new, true, &stats);
356 void bch_mark_metadata_bucket(struct cache *ca, struct bucket *g,
357 bool may_make_unavailable)
359 struct bucket_stats_cache_set stats = { 0 };
360 struct bucket_mark old, new;
362 old = bucket_cmpxchg(g, new, ({
364 new.had_metadata = 1;
367 BUG_ON(old.cached_sectors);
368 BUG_ON(old.dirty_sectors);
370 bucket_stats_update(ca, old, new, may_make_unavailable, &stats);
373 #define saturated_add(ca, dst, src, max) \
375 BUG_ON((int) (dst) + (src) < 0); \
376 if ((dst) == (max)) \
378 else if ((dst) + (src) <= (max)) \
382 trace_bcache_sectors_saturated(ca); \
387 /* Reverting this until the copygc + compression issue is fixed: */
389 static unsigned __disk_sectors(const union bch_extent_crc *crc, unsigned sectors)
391 return crc_compression_type(crc)
392 ? sectors * crc_compressed_size(crc) / crc_uncompressed_size(crc)
396 static unsigned __compressed_sectors(const union bch_extent_crc *crc, unsigned sectors)
398 return crc_compression_type(crc)
399 ? min_t(unsigned, crc_compressed_size(crc), sectors)
403 static unsigned __disk_sectors(const union bch_extent_crc *crc, unsigned sectors)
408 static unsigned __compressed_sectors(const union bch_extent_crc *crc, unsigned sectors)
415 * Checking against gc's position has to be done here, inside the cmpxchg()
416 * loop, to avoid racing with the start of gc clearing all the marks - GC does
417 * that with the gc pos seqlock held.
419 static void bch_mark_pointer(struct cache_set *c,
420 struct bkey_s_c_extent e,
422 const union bch_extent_crc *crc,
423 const struct bch_extent_ptr *ptr,
424 s64 sectors, enum s_alloc type,
425 bool may_make_unavailable,
426 struct bucket_stats_cache_set *stats,
427 bool gc_will_visit, u64 journal_seq)
429 struct bucket_mark old, new;
431 struct bucket *g = ca->buckets + PTR_BUCKET_NR(ca, ptr);
432 u64 v = READ_ONCE(g->_mark.counter);
433 unsigned old_sectors, new_sectors;
434 int disk_sectors, compressed_sectors;
438 new_sectors = sectors;
440 old_sectors = e.k->size;
441 new_sectors = e.k->size + sectors;
444 disk_sectors = -__disk_sectors(crc, old_sectors)
445 + __disk_sectors(crc, new_sectors);
446 compressed_sectors = -__compressed_sectors(crc, old_sectors)
447 + __compressed_sectors(crc, new_sectors);
451 bucket_cmpxchg(g, new, new.journal_seq = journal_seq);
457 new.counter = old.counter = v;
461 * Check this after reading bucket mark to guard against
462 * the allocator invalidating a bucket after we've already
465 if (gen_after(old.gen, ptr->gen)) {
466 EBUG_ON(type != S_CACHED &&
467 test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags));
471 EBUG_ON(type != S_CACHED &&
472 !may_make_unavailable &&
473 is_available_bucket(old) &&
474 test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags));
476 if (type != S_CACHED &&
477 new.dirty_sectors == GC_MAX_SECTORS_USED &&
479 saturated = -disk_sectors;
481 if (type == S_CACHED)
482 saturated_add(ca, new.cached_sectors, disk_sectors,
483 GC_MAX_SECTORS_USED);
485 saturated_add(ca, new.dirty_sectors, disk_sectors,
486 GC_MAX_SECTORS_USED);
488 if (!new.dirty_sectors &&
489 !new.cached_sectors) {
490 new.is_metadata = false;
493 new.wait_on_journal = true;
494 new.journal_seq = journal_seq;
497 new.is_metadata = (type == S_META);
500 new.had_metadata |= new.is_metadata;
501 } while ((v = cmpxchg(&g->_mark.counter,
503 new.counter)) != old.counter);
505 bucket_stats_update(ca, old, new, may_make_unavailable, NULL);
508 atomic_long_add_return(saturated,
509 &ca->saturated_count) >=
510 ca->free_inc.size << ca->bucket_bits) {
512 trace_bcache_gc_sectors_saturated(c);
513 wake_up_process(c->gc_thread);
517 stats->s[S_COMPRESSED][type] += compressed_sectors;
518 stats->s[S_UNCOMPRESSED][type] += sectors;
521 static void bch_mark_extent(struct cache_set *c, struct bkey_s_c_extent e,
522 s64 sectors, bool metadata,
523 bool may_make_unavailable,
524 struct bucket_stats_cache_set *stats,
525 bool gc_will_visit, u64 journal_seq)
527 const struct bch_extent_ptr *ptr;
528 const union bch_extent_crc *crc;
530 enum s_alloc type = metadata ? S_META : S_DIRTY;
532 BUG_ON(metadata && bkey_extent_is_cached(e.k));
536 extent_for_each_online_device_crc(c, e, crc, ptr, ca) {
537 trace_bcache_mark_bucket(ca, e.k, ptr, sectors, !ptr->cached);
539 bch_mark_pointer(c, e, ca, crc, ptr, sectors,
540 ptr->cached ? S_CACHED : type,
541 may_make_unavailable,
542 stats, gc_will_visit, journal_seq);
547 static void __bch_mark_key(struct cache_set *c, struct bkey_s_c k,
548 s64 sectors, bool metadata,
549 bool may_make_unavailable,
550 struct bucket_stats_cache_set *stats,
551 bool gc_will_visit, u64 journal_seq)
555 case BCH_EXTENT_CACHED:
556 bch_mark_extent(c, bkey_s_c_to_extent(k), sectors, metadata,
557 may_make_unavailable, stats,
558 gc_will_visit, journal_seq);
560 case BCH_RESERVATION: {
561 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
563 stats->persistent_reserved += r.v->nr_replicas * sectors;
569 void __bch_gc_mark_key(struct cache_set *c, struct bkey_s_c k,
570 s64 sectors, bool metadata,
571 struct bucket_stats_cache_set *stats)
573 __bch_mark_key(c, k, sectors, metadata, true, stats, false, 0);
576 void bch_gc_mark_key(struct cache_set *c, struct bkey_s_c k,
577 s64 sectors, bool metadata)
579 struct bucket_stats_cache_set stats = { 0 };
581 __bch_gc_mark_key(c, k, sectors, metadata, &stats);
584 bucket_stats_add(this_cpu_ptr(c->bucket_stats_percpu), &stats);
588 void bch_mark_key(struct cache_set *c, struct bkey_s_c k,
589 s64 sectors, bool metadata, struct gc_pos gc_pos,
590 struct bucket_stats_cache_set *stats, u64 journal_seq)
593 * synchronization w.r.t. GC:
595 * Normally, bucket sector counts/marks are updated on the fly, as
596 * references are added/removed from the btree, the lists of buckets the
597 * allocator owns, other metadata buckets, etc.
599 * When GC is in progress and going to mark this reference, we do _not_
600 * mark this reference here, to avoid double counting - GC will count it
601 * when it gets to it.
603 * To know whether we should mark a given reference (GC either isn't
604 * running, or has already marked references at this position) we
605 * construct a total order for everything GC walks. Then, we can simply
606 * compare the position of the reference we're marking - @gc_pos - with
607 * GC's current position. If GC is going to mark this reference, GC's
608 * current position will be less than @gc_pos; if GC's current position
609 * is greater than @gc_pos GC has either already walked this position,
612 * To avoid racing with GC's position changing, we have to deal with
613 * - GC's position being set to GC_POS_MIN when GC starts:
614 * bucket_stats_lock guards against this
615 * - GC's position overtaking @gc_pos: we guard against this with
616 * whatever lock protects the data structure the reference lives in
617 * (e.g. the btree node lock, or the relevant allocator lock).
619 lg_local_lock(&c->bucket_stats_lock);
620 __bch_mark_key(c, k, sectors, metadata, false, stats,
621 gc_will_visit(c, gc_pos), journal_seq);
623 bch_fs_stats_verify(c);
624 lg_local_unlock(&c->bucket_stats_lock);
627 static u64 __recalc_sectors_available(struct cache_set *c)
629 return c->capacity - bch_fs_sectors_used(c);
632 /* Used by gc when it's starting: */
633 void bch_recalc_sectors_available(struct cache_set *c)
637 lg_global_lock(&c->bucket_stats_lock);
639 for_each_possible_cpu(cpu)
640 per_cpu_ptr(c->bucket_stats_percpu, cpu)->available_cache = 0;
642 atomic64_set(&c->sectors_available,
643 __recalc_sectors_available(c));
645 lg_global_unlock(&c->bucket_stats_lock);
648 void bch_disk_reservation_put(struct cache_set *c,
649 struct disk_reservation *res)
652 lg_local_lock(&c->bucket_stats_lock);
653 this_cpu_sub(c->bucket_stats_percpu->online_reserved,
656 bch_fs_stats_verify(c);
657 lg_local_unlock(&c->bucket_stats_lock);
663 #define SECTORS_CACHE 1024
665 int bch_disk_reservation_add(struct cache_set *c,
666 struct disk_reservation *res,
667 unsigned sectors, int flags)
669 struct bucket_stats_cache_set *stats;
671 s64 sectors_available;
674 sectors *= res->nr_replicas;
676 lg_local_lock(&c->bucket_stats_lock);
677 stats = this_cpu_ptr(c->bucket_stats_percpu);
679 if (sectors >= stats->available_cache)
682 v = atomic64_read(&c->sectors_available);
686 lg_local_unlock(&c->bucket_stats_lock);
690 new = max_t(s64, 0, old - sectors - SECTORS_CACHE);
691 } while ((v = atomic64_cmpxchg(&c->sectors_available,
694 stats->available_cache += old - new;
696 stats->available_cache -= sectors;
697 stats->online_reserved += sectors;
698 res->sectors += sectors;
700 bch_fs_stats_verify(c);
701 lg_local_unlock(&c->bucket_stats_lock);
706 * GC recalculates sectors_available when it starts, so that hopefully
707 * we don't normally end up blocking here:
711 * Piss fuck, we can be called from extent_insert_fixup() with btree
715 if (!(flags & BCH_DISK_RESERVATION_GC_LOCK_HELD)) {
716 if (!(flags & BCH_DISK_RESERVATION_BTREE_LOCKS_HELD))
717 down_read(&c->gc_lock);
718 else if (!down_read_trylock(&c->gc_lock))
721 lg_global_lock(&c->bucket_stats_lock);
723 sectors_available = __recalc_sectors_available(c);
725 if (sectors <= sectors_available ||
726 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
727 atomic64_set(&c->sectors_available,
728 max_t(s64, 0, sectors_available - sectors));
729 stats->online_reserved += sectors;
730 res->sectors += sectors;
733 atomic64_set(&c->sectors_available, sectors_available);
737 bch_fs_stats_verify(c);
738 lg_global_unlock(&c->bucket_stats_lock);
739 if (!(flags & BCH_DISK_RESERVATION_GC_LOCK_HELD))
740 up_read(&c->gc_lock);
745 int bch_disk_reservation_get(struct cache_set *c,
746 struct disk_reservation *res,
747 unsigned sectors, int flags)
750 res->gen = c->capacity_gen;
751 res->nr_replicas = (flags & BCH_DISK_RESERVATION_METADATA)
752 ? c->opts.metadata_replicas
753 : c->opts.data_replicas;
755 return bch_disk_reservation_add(c, res, sectors, flags);