1 // SPDX-License-Identifier: GPL-2.0
3 * Code for manipulating bucket marks for garbage collection.
5 * Copyright 2014 Datera, Inc.
8 * - free bucket: mark == 0
9 * The bucket contains no data and will not be read
11 * - allocator bucket: owned_by_allocator == 1
12 * The bucket is on a free list, or it is an open bucket
14 * - cached bucket: owned_by_allocator == 0 &&
15 * dirty_sectors == 0 &&
17 * The bucket contains data but may be safely discarded as there are
18 * enough replicas of the data on other cache devices, or it has been
19 * written back to the backing device
21 * - dirty bucket: owned_by_allocator == 0 &&
23 * The bucket contains data that we must not discard (either only copy,
24 * or one of the 'main copies' for data requiring multiple replicas)
26 * - metadata bucket: owned_by_allocator == 0 && is_metadata == 1
27 * This is a btree node, journal or gen/prio bucket
31 * bucket invalidated => bucket on freelist => open bucket =>
32 * [dirty bucket =>] cached bucket => bucket invalidated => ...
34 * Note that cache promotion can skip the dirty bucket step, as data
35 * is copied from a deeper tier to a shallower tier, onto a cached
37 * Note also that a cached bucket can spontaneously become dirty --
40 * Only a traversal of the key space can determine whether a bucket is
41 * truly dirty or cached.
45 * - free => allocator: bucket was invalidated
46 * - cached => allocator: bucket was invalidated
48 * - allocator => dirty: open bucket was filled up
49 * - allocator => cached: open bucket was filled up
50 * - allocator => metadata: metadata was allocated
52 * - dirty => cached: dirty sectors were copied to a deeper tier
53 * - dirty => free: dirty sectors were overwritten or moved (copy gc)
54 * - cached => free: cached sectors were overwritten
56 * - metadata => free: metadata was freed
59 * - cached => dirty: a device was removed so formerly replicated data
60 * is no longer sufficiently replicated
61 * - free => cached: cannot happen
62 * - free => dirty: cannot happen
63 * - free => metadata: cannot happen
67 #include "alloc_background.h"
70 #include "btree_update.h"
77 #include <linux/preempt.h>
78 #include <trace/events/bcachefs.h>
81 * Clear journal_seq_valid for buckets for which it's not needed, to prevent
84 void bch2_bucket_seq_cleanup(struct bch_fs *c)
86 u64 journal_seq = atomic64_read(&c->journal.seq);
87 u16 last_seq_ondisk = c->journal.last_seq_ondisk;
89 struct bucket_array *buckets;
94 if (journal_seq - c->last_bucket_seq_cleanup <
95 (1U << (BUCKET_JOURNAL_SEQ_BITS - 2)))
98 c->last_bucket_seq_cleanup = journal_seq;
100 for_each_member_device(ca, c, i) {
101 down_read(&ca->bucket_lock);
102 buckets = bucket_array(ca);
104 for_each_bucket(g, buckets) {
105 bucket_cmpxchg(g, m, ({
106 if (!m.journal_seq_valid ||
107 bucket_needs_journal_commit(m, last_seq_ondisk))
110 m.journal_seq_valid = 0;
113 up_read(&ca->bucket_lock);
117 void bch2_fs_usage_initialize(struct bch_fs *c)
119 struct bch_fs_usage *usage;
122 percpu_down_write(&c->mark_lock);
123 usage = c->usage_base;
125 bch2_fs_usage_acc_to_base(c, 0);
126 bch2_fs_usage_acc_to_base(c, 1);
128 for (i = 0; i < BCH_REPLICAS_MAX; i++)
129 usage->reserved += usage->persistent_reserved[i];
131 for (i = 0; i < c->replicas.nr; i++) {
132 struct bch_replicas_entry *e =
133 cpu_replicas_entry(&c->replicas, i);
135 switch (e->data_type) {
137 usage->btree += usage->replicas[i];
140 usage->data += usage->replicas[i];
142 case BCH_DATA_CACHED:
143 usage->cached += usage->replicas[i];
148 percpu_up_write(&c->mark_lock);
151 void bch2_fs_usage_scratch_put(struct bch_fs *c, struct bch_fs_usage *fs_usage)
153 if (fs_usage == c->usage_scratch)
154 mutex_unlock(&c->usage_scratch_lock);
159 struct bch_fs_usage *bch2_fs_usage_scratch_get(struct bch_fs *c)
161 struct bch_fs_usage *ret;
162 unsigned bytes = fs_usage_u64s(c) * sizeof(u64);
164 ret = kzalloc(bytes, GFP_NOWAIT|__GFP_NOWARN);
168 if (mutex_trylock(&c->usage_scratch_lock))
171 ret = kzalloc(bytes, GFP_NOFS);
175 mutex_lock(&c->usage_scratch_lock);
177 ret = c->usage_scratch;
178 memset(ret, 0, bytes);
182 struct bch_dev_usage bch2_dev_usage_read(struct bch_fs *c, struct bch_dev *ca)
184 struct bch_dev_usage ret;
186 memset(&ret, 0, sizeof(ret));
187 acc_u64s_percpu((u64 *) &ret,
188 (u64 __percpu *) ca->usage[0],
189 sizeof(ret) / sizeof(u64));
194 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
195 unsigned journal_seq,
198 return this_cpu_ptr(gc
200 : c->usage[journal_seq & 1]);
203 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
205 ssize_t offset = v - (u64 *) c->usage_base;
209 BUG_ON(offset < 0 || offset >= fs_usage_u64s(c));
210 percpu_rwsem_assert_held(&c->mark_lock);
213 seq = read_seqcount_begin(&c->usage_lock);
215 percpu_u64_get((u64 __percpu *) c->usage[0] + offset) +
216 percpu_u64_get((u64 __percpu *) c->usage[1] + offset);
217 } while (read_seqcount_retry(&c->usage_lock, seq));
222 struct bch_fs_usage *bch2_fs_usage_read(struct bch_fs *c)
224 struct bch_fs_usage *ret;
225 unsigned seq, v, u64s = fs_usage_u64s(c);
227 ret = kmalloc(u64s * sizeof(u64), GFP_NOFS);
231 percpu_down_read(&c->mark_lock);
233 v = fs_usage_u64s(c);
234 if (unlikely(u64s != v)) {
236 percpu_up_read(&c->mark_lock);
242 seq = read_seqcount_begin(&c->usage_lock);
243 memcpy(ret, c->usage_base, u64s * sizeof(u64));
244 acc_u64s_percpu((u64 *) ret, (u64 __percpu *) c->usage[0], u64s);
245 acc_u64s_percpu((u64 *) ret, (u64 __percpu *) c->usage[1], u64s);
246 } while (read_seqcount_retry(&c->usage_lock, seq));
251 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx)
253 unsigned u64s = fs_usage_u64s(c);
257 write_seqcount_begin(&c->usage_lock);
259 acc_u64s_percpu((u64 *) c->usage_base,
260 (u64 __percpu *) c->usage[idx], u64s);
261 percpu_memset(c->usage[idx], 0, u64s * sizeof(u64));
263 write_seqcount_end(&c->usage_lock);
266 void bch2_fs_usage_to_text(struct printbuf *out,
268 struct bch_fs_usage *fs_usage)
272 pr_buf(out, "capacity:\t\t\t%llu\n", c->capacity);
274 pr_buf(out, "hidden:\t\t\t\t%llu\n",
276 pr_buf(out, "data:\t\t\t\t%llu\n",
278 pr_buf(out, "cached:\t\t\t\t%llu\n",
280 pr_buf(out, "reserved:\t\t\t%llu\n",
282 pr_buf(out, "nr_inodes:\t\t\t%llu\n",
283 fs_usage->nr_inodes);
284 pr_buf(out, "online reserved:\t\t%llu\n",
285 fs_usage->online_reserved);
288 i < ARRAY_SIZE(fs_usage->persistent_reserved);
290 pr_buf(out, "%u replicas:\n", i + 1);
291 pr_buf(out, "\treserved:\t\t%llu\n",
292 fs_usage->persistent_reserved[i]);
295 for (i = 0; i < c->replicas.nr; i++) {
296 struct bch_replicas_entry *e =
297 cpu_replicas_entry(&c->replicas, i);
300 bch2_replicas_entry_to_text(out, e);
301 pr_buf(out, ":\t%llu\n", fs_usage->replicas[i]);
305 #define RESERVE_FACTOR 6
307 static u64 reserve_factor(u64 r)
309 return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR);
312 static u64 avail_factor(u64 r)
314 return (r << RESERVE_FACTOR) / ((1 << RESERVE_FACTOR) + 1);
317 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage *fs_usage)
319 return min(fs_usage->hidden +
322 reserve_factor(fs_usage->reserved +
323 fs_usage->online_reserved),
327 static struct bch_fs_usage_short
328 __bch2_fs_usage_read_short(struct bch_fs *c)
330 struct bch_fs_usage_short ret;
333 ret.capacity = c->capacity -
334 bch2_fs_usage_read_one(c, &c->usage_base->hidden);
336 data = bch2_fs_usage_read_one(c, &c->usage_base->data) +
337 bch2_fs_usage_read_one(c, &c->usage_base->btree);
338 reserved = bch2_fs_usage_read_one(c, &c->usage_base->reserved) +
339 bch2_fs_usage_read_one(c, &c->usage_base->online_reserved);
341 ret.used = min(ret.capacity, data + reserve_factor(reserved));
342 ret.free = ret.capacity - ret.used;
344 ret.nr_inodes = bch2_fs_usage_read_one(c, &c->usage_base->nr_inodes);
349 struct bch_fs_usage_short
350 bch2_fs_usage_read_short(struct bch_fs *c)
352 struct bch_fs_usage_short ret;
354 percpu_down_read(&c->mark_lock);
355 ret = __bch2_fs_usage_read_short(c);
356 percpu_up_read(&c->mark_lock);
361 static inline int is_unavailable_bucket(struct bucket_mark m)
363 return !is_available_bucket(m);
366 static inline int is_fragmented_bucket(struct bucket_mark m,
369 if (!m.owned_by_allocator &&
370 m.data_type == BCH_DATA_USER &&
371 bucket_sectors_used(m))
372 return max_t(int, 0, (int) ca->mi.bucket_size -
373 bucket_sectors_used(m));
377 static inline enum bch_data_type bucket_type(struct bucket_mark m)
379 return m.cached_sectors && !m.dirty_sectors
384 static bool bucket_became_unavailable(struct bucket_mark old,
385 struct bucket_mark new)
387 return is_available_bucket(old) &&
388 !is_available_bucket(new);
391 int bch2_fs_usage_apply(struct bch_fs *c,
392 struct bch_fs_usage *fs_usage,
393 struct disk_reservation *disk_res,
394 unsigned journal_seq)
396 s64 added = fs_usage->data + fs_usage->reserved;
397 s64 should_not_have_added;
400 percpu_rwsem_assert_held(&c->mark_lock);
403 * Not allowed to reduce sectors_available except by getting a
406 should_not_have_added = added - (s64) (disk_res ? disk_res->sectors : 0);
407 if (WARN_ONCE(should_not_have_added > 0,
408 "disk usage increased by %lli without a reservation",
409 should_not_have_added)) {
410 atomic64_sub(should_not_have_added, &c->sectors_available);
411 added -= should_not_have_added;
416 disk_res->sectors -= added;
417 fs_usage->online_reserved -= added;
421 acc_u64s((u64 *) fs_usage_ptr(c, journal_seq, false),
422 (u64 *) fs_usage, fs_usage_u64s(c));
428 static inline void account_bucket(struct bch_fs_usage *fs_usage,
429 struct bch_dev_usage *dev_usage,
430 enum bch_data_type type,
433 if (type == BCH_DATA_SB || type == BCH_DATA_JOURNAL)
434 fs_usage->hidden += size;
436 dev_usage->buckets[type] += nr;
439 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
440 struct bch_fs_usage *fs_usage,
441 struct bucket_mark old, struct bucket_mark new,
444 struct bch_dev_usage *dev_usage;
446 percpu_rwsem_assert_held(&c->mark_lock);
449 dev_usage = this_cpu_ptr(ca->usage[gc]);
451 if (bucket_type(old))
452 account_bucket(fs_usage, dev_usage, bucket_type(old),
453 -1, -ca->mi.bucket_size);
455 if (bucket_type(new))
456 account_bucket(fs_usage, dev_usage, bucket_type(new),
457 1, ca->mi.bucket_size);
459 dev_usage->buckets_alloc +=
460 (int) new.owned_by_allocator - (int) old.owned_by_allocator;
461 dev_usage->buckets_ec +=
462 (int) new.stripe - (int) old.stripe;
463 dev_usage->buckets_unavailable +=
464 is_unavailable_bucket(new) - is_unavailable_bucket(old);
466 dev_usage->sectors[old.data_type] -= old.dirty_sectors;
467 dev_usage->sectors[new.data_type] += new.dirty_sectors;
468 dev_usage->sectors[BCH_DATA_CACHED] +=
469 (int) new.cached_sectors - (int) old.cached_sectors;
470 dev_usage->sectors_fragmented +=
471 is_fragmented_bucket(new, ca) - is_fragmented_bucket(old, ca);
474 if (!is_available_bucket(old) && is_available_bucket(new))
475 bch2_wake_allocator(ca);
478 void bch2_dev_usage_from_buckets(struct bch_fs *c)
481 struct bucket_mark old = { .v.counter = 0 };
482 struct bucket_array *buckets;
487 c->usage_base->hidden = 0;
489 for_each_member_device(ca, c, i) {
490 for_each_possible_cpu(cpu)
491 memset(per_cpu_ptr(ca->usage[0], cpu), 0,
492 sizeof(*ca->usage[0]));
494 buckets = bucket_array(ca);
496 for_each_bucket(g, buckets)
497 bch2_dev_usage_update(c, ca, c->usage_base,
498 old, g->mark, false);
502 static inline int update_replicas(struct bch_fs *c,
503 struct bch_fs_usage *fs_usage,
504 struct bch_replicas_entry *r,
507 int idx = bch2_replicas_entry_idx(c, r);
515 switch (r->data_type) {
517 fs_usage->btree += sectors;
520 fs_usage->data += sectors;
522 case BCH_DATA_CACHED:
523 fs_usage->cached += sectors;
526 fs_usage->replicas[idx] += sectors;
530 static inline void update_cached_sectors(struct bch_fs *c,
531 struct bch_fs_usage *fs_usage,
532 unsigned dev, s64 sectors)
534 struct bch_replicas_padded r;
536 bch2_replicas_entry_cached(&r.e, dev);
538 update_replicas(c, fs_usage, &r.e, sectors);
541 static struct replicas_delta_list *
542 replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
544 struct replicas_delta_list *d = trans->fs_usage_deltas;
545 unsigned new_size = d ? (d->size + more) * 2 : 128;
547 if (!d || d->used + more > d->size) {
548 d = krealloc(d, sizeof(*d) + new_size, GFP_NOIO|__GFP_ZERO);
552 trans->fs_usage_deltas = d;
557 static inline void update_replicas_list(struct btree_trans *trans,
558 struct bch_replicas_entry *r,
561 struct replicas_delta_list *d;
562 struct replicas_delta *n;
568 b = replicas_entry_bytes(r) + 8;
569 d = replicas_deltas_realloc(trans, b);
571 n = (void *) d->d + d->used;
573 memcpy(&n->r, r, replicas_entry_bytes(r));
577 static inline void update_cached_sectors_list(struct btree_trans *trans,
578 unsigned dev, s64 sectors)
580 struct bch_replicas_padded r;
582 bch2_replicas_entry_cached(&r.e, dev);
584 update_replicas_list(trans, &r.e, sectors);
587 static inline struct replicas_delta *
588 replicas_delta_next(struct replicas_delta *d)
590 return (void *) d + replicas_entry_bytes(&d->r) + 8;
593 int bch2_replicas_delta_list_apply(struct bch_fs *c,
594 struct bch_fs_usage *fs_usage,
595 struct replicas_delta_list *r)
597 struct replicas_delta *d = r->d;
598 struct replicas_delta *top = (void *) r->d + r->used;
601 for (d = r->d; d != top; d = replicas_delta_next(d))
602 if (update_replicas(c, fs_usage, &d->r, d->delta)) {
610 fs_usage->nr_inodes += r->nr_inodes;
612 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
613 fs_usage->reserved += r->persistent_reserved[i];
614 fs_usage->persistent_reserved[i] += r->persistent_reserved[i];
619 for (d = r->d; d != top; d = replicas_delta_next(d))
620 update_replicas(c, fs_usage, &d->r, -d->delta);
624 #define do_mark_fn(fn, c, pos, flags, ...) \
628 percpu_rwsem_assert_held(&c->mark_lock); \
630 for (gc = 0; gc < 2 && !ret; gc++) \
631 if (!gc == !(flags & BTREE_TRIGGER_GC) || \
632 (gc && gc_visited(c, pos))) \
633 ret = fn(c, __VA_ARGS__, gc); \
637 static int __bch2_invalidate_bucket(struct bch_fs *c, struct bch_dev *ca,
638 size_t b, struct bucket_mark *ret,
641 struct bch_fs_usage *fs_usage = fs_usage_ptr(c, 0, gc);
642 struct bucket *g = __bucket(ca, b, gc);
643 struct bucket_mark old, new;
645 old = bucket_cmpxchg(g, new, ({
646 BUG_ON(!is_available_bucket(new));
648 new.owned_by_allocator = true;
650 new.cached_sectors = 0;
651 new.dirty_sectors = 0;
655 bch2_dev_usage_update(c, ca, fs_usage, old, new, gc);
657 if (old.cached_sectors)
658 update_cached_sectors(c, fs_usage, ca->dev_idx,
659 -((s64) old.cached_sectors));
666 void bch2_invalidate_bucket(struct bch_fs *c, struct bch_dev *ca,
667 size_t b, struct bucket_mark *old)
669 do_mark_fn(__bch2_invalidate_bucket, c, gc_phase(GC_PHASE_START), 0,
672 if (!old->owned_by_allocator && old->cached_sectors)
673 trace_invalidate(ca, bucket_to_sector(ca, b),
674 old->cached_sectors);
677 static int __bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
678 size_t b, bool owned_by_allocator,
681 struct bch_fs_usage *fs_usage = fs_usage_ptr(c, 0, gc);
682 struct bucket *g = __bucket(ca, b, gc);
683 struct bucket_mark old, new;
685 old = bucket_cmpxchg(g, new, ({
686 new.owned_by_allocator = owned_by_allocator;
689 bch2_dev_usage_update(c, ca, fs_usage, old, new, gc);
692 !owned_by_allocator && !old.owned_by_allocator);
697 void bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
698 size_t b, bool owned_by_allocator,
699 struct gc_pos pos, unsigned flags)
703 do_mark_fn(__bch2_mark_alloc_bucket, c, pos, flags,
704 ca, b, owned_by_allocator);
709 static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k,
710 struct bch_fs_usage *fs_usage,
711 u64 journal_seq, unsigned flags)
713 bool gc = flags & BTREE_TRIGGER_GC;
714 struct bkey_alloc_unpacked u;
717 struct bucket_mark old, m;
720 * alloc btree is read in by bch2_alloc_read, not gc:
722 if ((flags & BTREE_TRIGGER_GC) &&
723 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
726 ca = bch_dev_bkey_exists(c, k.k->p.inode);
728 if (k.k->p.offset >= ca->mi.nbuckets)
731 g = __bucket(ca, k.k->p.offset, gc);
732 u = bch2_alloc_unpack(k);
734 old = bucket_cmpxchg(g, m, ({
736 m.data_type = u.data_type;
737 m.dirty_sectors = u.dirty_sectors;
738 m.cached_sectors = u.cached_sectors;
741 m.journal_seq_valid = 1;
742 m.journal_seq = journal_seq;
746 if (!(flags & BTREE_TRIGGER_ALLOC_READ))
747 bch2_dev_usage_update(c, ca, fs_usage, old, m, gc);
749 g->io_time[READ] = u.read_time;
750 g->io_time[WRITE] = u.write_time;
751 g->oldest_gen = u.oldest_gen;
755 * need to know if we're getting called from the invalidate path or
759 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
760 old.cached_sectors) {
761 update_cached_sectors(c, fs_usage, ca->dev_idx,
762 -old.cached_sectors);
763 trace_invalidate(ca, bucket_to_sector(ca, k.k->p.offset),
770 #define checked_add(a, b) \
772 unsigned _res = (unsigned) (a) + (b); \
773 bool overflow = _res > U16_MAX; \
780 static int __bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
781 size_t b, enum bch_data_type type,
782 unsigned sectors, bool gc)
784 struct bucket *g = __bucket(ca, b, gc);
785 struct bucket_mark old, new;
788 BUG_ON(type != BCH_DATA_SB &&
789 type != BCH_DATA_JOURNAL);
791 old = bucket_cmpxchg(g, new, ({
792 new.data_type = type;
793 overflow = checked_add(new.dirty_sectors, sectors);
796 bch2_fs_inconsistent_on(old.data_type &&
797 old.data_type != type, c,
798 "different types of data in same bucket: %s, %s",
799 bch2_data_types[old.data_type],
800 bch2_data_types[type]);
802 bch2_fs_inconsistent_on(overflow, c,
803 "bucket sector count overflow: %u + %u > U16_MAX",
804 old.dirty_sectors, sectors);
807 bch2_dev_usage_update(c, ca, fs_usage_ptr(c, 0, gc),
813 void bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
814 size_t b, enum bch_data_type type,
815 unsigned sectors, struct gc_pos pos,
818 BUG_ON(type != BCH_DATA_SB &&
819 type != BCH_DATA_JOURNAL);
824 do_mark_fn(__bch2_mark_metadata_bucket, c, pos, flags,
825 ca, b, type, sectors);
827 __bch2_mark_metadata_bucket(c, ca, b, type, sectors, 0);
833 static s64 disk_sectors_scaled(unsigned n, unsigned d, unsigned sectors)
835 return DIV_ROUND_UP(sectors * n, d);
838 static s64 __ptr_disk_sectors_delta(unsigned old_size,
839 unsigned offset, s64 delta,
841 unsigned n, unsigned d)
845 if (flags & BTREE_TRIGGER_OVERWRITE_SPLIT) {
846 BUG_ON(offset + -delta > old_size);
848 return -disk_sectors_scaled(n, d, old_size) +
849 disk_sectors_scaled(n, d, offset) +
850 disk_sectors_scaled(n, d, old_size - offset + delta);
851 } else if (flags & BTREE_TRIGGER_OVERWRITE) {
852 BUG_ON(offset + -delta > old_size);
854 return -disk_sectors_scaled(n, d, old_size) +
855 disk_sectors_scaled(n, d, old_size + delta);
857 return disk_sectors_scaled(n, d, delta);
861 static s64 ptr_disk_sectors_delta(struct extent_ptr_decoded p,
862 unsigned offset, s64 delta,
865 return __ptr_disk_sectors_delta(p.crc.live_size,
866 offset, delta, flags,
867 p.crc.compressed_size,
868 p.crc.uncompressed_size);
871 static void bucket_set_stripe(struct bch_fs *c,
872 const struct bch_stripe *v,
873 struct bch_fs_usage *fs_usage,
877 bool enabled = !(flags & BTREE_TRIGGER_OVERWRITE);
878 bool gc = flags & BTREE_TRIGGER_GC;
881 for (i = 0; i < v->nr_blocks; i++) {
882 const struct bch_extent_ptr *ptr = v->ptrs + i;
883 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
884 struct bucket *g = PTR_BUCKET(ca, ptr, gc);
885 struct bucket_mark new, old;
887 old = bucket_cmpxchg(g, new, ({
888 new.stripe = enabled;
890 new.journal_seq_valid = 1;
891 new.journal_seq = journal_seq;
895 bch2_dev_usage_update(c, ca, fs_usage, old, new, gc);
898 * XXX write repair code for these, flag stripe as possibly bad
900 if (old.gen != ptr->gen)
901 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
902 "stripe with stale pointer");
905 * We'd like to check for these, but these checks don't work
908 if (old.stripe && enabled)
909 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
910 "multiple stripes using same bucket");
912 if (!old.stripe && !enabled)
913 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
914 "deleting stripe but bucket not marked as stripe bucket");
919 static bool bch2_mark_pointer(struct bch_fs *c,
920 struct extent_ptr_decoded p,
921 s64 sectors, enum bch_data_type data_type,
922 struct bch_fs_usage *fs_usage,
923 u64 journal_seq, unsigned flags)
925 bool gc = flags & BTREE_TRIGGER_GC;
926 struct bucket_mark old, new;
927 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
928 struct bucket *g = PTR_BUCKET(ca, &p.ptr, gc);
932 v = atomic64_read(&g->_mark.v);
934 new.v.counter = old.v.counter = v;
937 * Check this after reading bucket mark to guard against
938 * the allocator invalidating a bucket after we've already
941 if (gen_after(p.ptr.gen, new.gen)) {
942 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
943 "pointer gen in the future");
947 if (new.gen != p.ptr.gen) {
948 /* XXX write repair code for this */
950 test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
951 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
952 "stale dirty pointer");
957 overflow = checked_add(new.dirty_sectors, sectors);
959 overflow = checked_add(new.cached_sectors, sectors);
961 if (!new.dirty_sectors &&
962 !new.cached_sectors) {
966 new.journal_seq_valid = 1;
967 new.journal_seq = journal_seq;
970 new.data_type = data_type;
973 if (flags & BTREE_TRIGGER_NOATOMIC) {
977 } while ((v = atomic64_cmpxchg(&g->_mark.v,
979 new.v.counter)) != old.v.counter);
981 if (old.data_type && old.data_type != data_type)
982 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
983 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s",
984 p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
986 bch2_data_types[old.data_type],
987 bch2_data_types[data_type]);
989 bch2_fs_inconsistent_on(overflow, c,
990 "bucket sector count overflow: %u + %lli > U16_MAX",
993 : old.cached_sectors, sectors);
995 bch2_dev_usage_update(c, ca, fs_usage, old, new, gc);
997 BUG_ON(!gc && bucket_became_unavailable(old, new));
1002 static int bch2_mark_stripe_ptr(struct bch_fs *c,
1003 struct bch_extent_stripe_ptr p,
1004 enum bch_data_type data_type,
1005 struct bch_fs_usage *fs_usage,
1006 s64 sectors, unsigned flags,
1007 struct bch_replicas_padded *r,
1009 unsigned *nr_parity)
1011 bool gc = flags & BTREE_TRIGGER_GC;
1014 int blocks_nonempty_delta;
1016 m = genradix_ptr(&c->stripes[gc], p.idx);
1018 spin_lock(&c->ec_stripes_heap_lock);
1020 if (!m || !m->alive) {
1021 spin_unlock(&c->ec_stripes_heap_lock);
1022 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
1027 BUG_ON(m->r.e.data_type != data_type);
1029 *nr_data = m->nr_blocks - m->nr_redundant;
1030 *nr_parity = m->nr_redundant;
1033 old = m->block_sectors[p.block];
1034 m->block_sectors[p.block] += sectors;
1035 new = m->block_sectors[p.block];
1037 blocks_nonempty_delta = (int) !!new - (int) !!old;
1038 if (blocks_nonempty_delta) {
1039 m->blocks_nonempty += blocks_nonempty_delta;
1042 bch2_stripes_heap_update(c, m, p.idx);
1047 spin_unlock(&c->ec_stripes_heap_lock);
1052 static int bch2_mark_extent(struct bch_fs *c, struct bkey_s_c k,
1053 unsigned offset, s64 sectors,
1054 enum bch_data_type data_type,
1055 struct bch_fs_usage *fs_usage,
1056 unsigned journal_seq, unsigned flags)
1058 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1059 const union bch_extent_entry *entry;
1060 struct extent_ptr_decoded p;
1061 struct bch_replicas_padded r;
1062 s64 dirty_sectors = 0;
1065 r.e.data_type = data_type;
1067 r.e.nr_required = 1;
1071 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1072 s64 disk_sectors = data_type == BCH_DATA_BTREE
1074 : ptr_disk_sectors_delta(p, offset, sectors, flags);
1075 bool stale = bch2_mark_pointer(c, p, disk_sectors, data_type,
1076 fs_usage, journal_seq, flags);
1080 update_cached_sectors(c, fs_usage, p.ptr.dev,
1082 } else if (!p.has_ec) {
1083 dirty_sectors += disk_sectors;
1084 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1086 struct bch_replicas_padded ec_r;
1087 unsigned nr_data, nr_parity;
1090 ret = bch2_mark_stripe_ptr(c, p.ec, data_type,
1091 fs_usage, disk_sectors, flags,
1092 &ec_r, &nr_data, &nr_parity);
1097 __ptr_disk_sectors_delta(p.crc.live_size,
1098 offset, sectors, flags,
1099 p.crc.compressed_size * nr_parity,
1100 p.crc.uncompressed_size * nr_data);
1102 update_replicas(c, fs_usage, &ec_r.e,
1103 disk_sectors + parity_sectors);
1106 * There may be other dirty pointers in this extent, but
1107 * if so they're not required for mounting if we have an
1108 * erasure coded pointer in this extent:
1110 r.e.nr_required = 0;
1115 update_replicas(c, fs_usage, &r.e, dirty_sectors);
1120 static int bch2_mark_stripe(struct bch_fs *c, struct bkey_s_c k,
1121 struct bch_fs_usage *fs_usage,
1122 u64 journal_seq, unsigned flags)
1124 bool gc = flags & BTREE_TRIGGER_GC;
1125 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
1126 size_t idx = s.k->p.offset;
1127 struct stripe *m = genradix_ptr(&c->stripes[gc], idx);
1130 spin_lock(&c->ec_stripes_heap_lock);
1132 if (!m || ((flags & BTREE_TRIGGER_OVERWRITE) && !m->alive)) {
1133 spin_unlock(&c->ec_stripes_heap_lock);
1134 bch_err_ratelimited(c, "error marking nonexistent stripe %zu",
1139 if (!(flags & BTREE_TRIGGER_OVERWRITE)) {
1140 m->sectors = le16_to_cpu(s.v->sectors);
1141 m->algorithm = s.v->algorithm;
1142 m->nr_blocks = s.v->nr_blocks;
1143 m->nr_redundant = s.v->nr_redundant;
1145 bch2_bkey_to_replicas(&m->r.e, k);
1148 * XXX: account for stripes somehow here
1151 update_replicas(c, fs_usage, &m->r.e, stripe_sectors);
1154 /* gc recalculates these fields: */
1155 if (!(flags & BTREE_TRIGGER_GC)) {
1156 for (i = 0; i < s.v->nr_blocks; i++) {
1157 m->block_sectors[i] =
1158 stripe_blockcount_get(s.v, i);
1159 m->blocks_nonempty += !!m->block_sectors[i];
1164 bch2_stripes_heap_update(c, m, idx);
1168 bch2_stripes_heap_del(c, m, idx);
1169 memset(m, 0, sizeof(*m));
1172 spin_unlock(&c->ec_stripes_heap_lock);
1174 bucket_set_stripe(c, s.v, fs_usage, 0, flags);
1178 int bch2_mark_key_locked(struct bch_fs *c,
1180 unsigned offset, s64 sectors,
1181 struct bch_fs_usage *fs_usage,
1182 u64 journal_seq, unsigned flags)
1188 if (!fs_usage || (flags & BTREE_TRIGGER_GC))
1189 fs_usage = fs_usage_ptr(c, journal_seq,
1190 flags & BTREE_TRIGGER_GC);
1192 switch (k.k->type) {
1193 case KEY_TYPE_alloc:
1194 ret = bch2_mark_alloc(c, k, fs_usage, journal_seq, flags);
1196 case KEY_TYPE_btree_ptr:
1197 case KEY_TYPE_btree_ptr_v2:
1198 sectors = !(flags & BTREE_TRIGGER_OVERWRITE)
1199 ? c->opts.btree_node_size
1200 : -c->opts.btree_node_size;
1202 ret = bch2_mark_extent(c, k, offset, sectors, BCH_DATA_BTREE,
1203 fs_usage, journal_seq, flags);
1205 case KEY_TYPE_extent:
1206 case KEY_TYPE_reflink_v:
1207 ret = bch2_mark_extent(c, k, offset, sectors, BCH_DATA_USER,
1208 fs_usage, journal_seq, flags);
1210 case KEY_TYPE_stripe:
1211 ret = bch2_mark_stripe(c, k, fs_usage, journal_seq, flags);
1213 case KEY_TYPE_inode:
1214 if (!(flags & BTREE_TRIGGER_OVERWRITE))
1215 fs_usage->nr_inodes++;
1217 fs_usage->nr_inodes--;
1219 case KEY_TYPE_reservation: {
1220 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1222 sectors *= replicas;
1223 replicas = clamp_t(unsigned, replicas, 1,
1224 ARRAY_SIZE(fs_usage->persistent_reserved));
1226 fs_usage->reserved += sectors;
1227 fs_usage->persistent_reserved[replicas - 1] += sectors;
1237 int bch2_mark_key(struct bch_fs *c, struct bkey_s_c k,
1238 unsigned offset, s64 sectors,
1239 struct bch_fs_usage *fs_usage,
1240 u64 journal_seq, unsigned flags)
1244 percpu_down_read(&c->mark_lock);
1245 ret = bch2_mark_key_locked(c, k, offset, sectors,
1246 fs_usage, journal_seq, flags);
1247 percpu_up_read(&c->mark_lock);
1252 inline int bch2_mark_overwrite(struct btree_trans *trans,
1253 struct btree_iter *iter,
1254 struct bkey_s_c old,
1256 struct bch_fs_usage *fs_usage,
1260 struct bch_fs *c = trans->c;
1261 unsigned offset = 0;
1262 s64 sectors = -((s64) old.k->size);
1264 flags |= BTREE_TRIGGER_OVERWRITE;
1267 ? bkey_cmp(new->k.p, bkey_start_pos(old.k)) <= 0
1268 : bkey_cmp(new->k.p, old.k->p))
1272 switch (bch2_extent_overlap(&new->k, old.k)) {
1273 case BCH_EXTENT_OVERLAP_ALL:
1275 sectors = -((s64) old.k->size);
1277 case BCH_EXTENT_OVERLAP_BACK:
1278 offset = bkey_start_offset(&new->k) -
1279 bkey_start_offset(old.k);
1280 sectors = bkey_start_offset(&new->k) -
1283 case BCH_EXTENT_OVERLAP_FRONT:
1285 sectors = bkey_start_offset(old.k) -
1288 case BCH_EXTENT_OVERLAP_MIDDLE:
1289 offset = bkey_start_offset(&new->k) -
1290 bkey_start_offset(old.k);
1291 sectors = -((s64) new->k.size);
1292 flags |= BTREE_TRIGGER_OVERWRITE_SPLIT;
1296 BUG_ON(sectors >= 0);
1299 return bch2_mark_key_locked(c, old, offset, sectors, fs_usage,
1300 trans->journal_res.seq, flags) ?: 1;
1303 int bch2_mark_update(struct btree_trans *trans,
1304 struct btree_iter *iter,
1305 struct bkey_i *insert,
1306 struct bch_fs_usage *fs_usage,
1309 struct bch_fs *c = trans->c;
1310 struct btree *b = iter->l[0].b;
1311 struct btree_node_iter node_iter = iter->l[0].iter;
1312 struct bkey_packed *_k;
1315 if (unlikely(flags & BTREE_TRIGGER_NORUN))
1318 if (!btree_node_type_needs_gc(iter->btree_id))
1321 bch2_mark_key_locked(c, bkey_i_to_s_c(insert),
1323 fs_usage, trans->journal_res.seq,
1324 BTREE_TRIGGER_INSERT|flags);
1326 if (unlikely(flags & BTREE_TRIGGER_NOOVERWRITES))
1330 * For non extents, we only mark the new key, not the key being
1331 * overwritten - unless we're actually deleting:
1333 if ((iter->btree_id == BTREE_ID_ALLOC ||
1334 iter->btree_id == BTREE_ID_EC) &&
1335 !bkey_deleted(&insert->k))
1338 while ((_k = bch2_btree_node_iter_peek(&node_iter, b))) {
1339 struct bkey unpacked;
1340 struct bkey_s_c k = bkey_disassemble(b, _k, &unpacked);
1342 ret = bch2_mark_overwrite(trans, iter, k, insert,
1344 btree_node_type_is_extents(iter->btree_id));
1348 bch2_btree_node_iter_advance(&node_iter, b);
1354 void bch2_trans_fs_usage_apply(struct btree_trans *trans,
1355 struct bch_fs_usage *fs_usage)
1357 struct bch_fs *c = trans->c;
1358 struct btree_insert_entry *i;
1359 static int warned_disk_usage = 0;
1360 u64 disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1363 if (!bch2_fs_usage_apply(c, fs_usage, trans->disk_res,
1364 trans->journal_res.seq) ||
1365 warned_disk_usage ||
1366 xchg(&warned_disk_usage, 1))
1369 bch_err(c, "disk usage increased more than %llu sectors reserved",
1372 trans_for_each_update(trans, i) {
1373 struct btree_iter *iter = i->iter;
1374 struct btree *b = iter->l[0].b;
1375 struct btree_node_iter node_iter = iter->l[0].iter;
1376 struct bkey_packed *_k;
1378 pr_err("while inserting");
1379 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(i->k));
1381 pr_err("overlapping with");
1383 node_iter = iter->l[0].iter;
1384 while ((_k = bch2_btree_node_iter_peek(&node_iter, b))) {
1385 struct bkey unpacked;
1388 k = bkey_disassemble(b, _k, &unpacked);
1390 if (btree_node_is_extents(b)
1391 ? bkey_cmp(i->k->k.p, bkey_start_pos(k.k)) <= 0
1392 : bkey_cmp(i->k->k.p, k.k->p))
1395 bch2_bkey_val_to_text(&PBUF(buf), c, k);
1398 bch2_btree_node_iter_advance(&node_iter, b);
1405 static int trans_get_key(struct btree_trans *trans,
1406 enum btree_id btree_id, struct bpos pos,
1407 struct btree_iter **iter,
1410 struct btree_insert_entry *i;
1413 trans_for_each_update(trans, i)
1414 if (i->iter->btree_id == btree_id &&
1415 (btree_node_type_is_extents(btree_id)
1416 ? bkey_cmp(pos, bkey_start_pos(&i->k->k)) >= 0 &&
1417 bkey_cmp(pos, i->k->k.p) < 0
1418 : !bkey_cmp(pos, i->iter->pos))) {
1420 *k = bkey_i_to_s_c(i->k);
1424 *iter = bch2_trans_get_iter(trans, btree_id, pos,
1425 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
1427 return PTR_ERR(*iter);
1429 *k = bch2_btree_iter_peek_slot(*iter);
1432 bch2_trans_iter_put(trans, *iter);
1436 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1437 struct extent_ptr_decoded p,
1438 s64 sectors, enum bch_data_type data_type)
1440 struct bch_fs *c = trans->c;
1441 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
1442 struct btree_iter *iter;
1444 struct bkey_alloc_unpacked u;
1445 struct bkey_i_alloc *a;
1446 u16 *dst_sectors, orig_sectors;
1449 ret = trans_get_key(trans, BTREE_ID_ALLOC,
1450 POS(p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr)),
1455 if (!ret && unlikely(!test_bit(BCH_FS_ALLOC_WRITTEN, &c->flags))) {
1457 * During journal replay, and if gc repairs alloc info at
1458 * runtime, the alloc info in the btree might not be up to date
1459 * yet - so, trust the in memory mark:
1462 struct bucket_mark m;
1464 percpu_down_read(&c->mark_lock);
1465 g = bucket(ca, iter->pos.offset);
1466 m = READ_ONCE(g->mark);
1467 u = alloc_mem_to_key(g, m);
1468 percpu_up_read(&c->mark_lock);
1471 * Unless we're already updating that key:
1473 if (k.k->type != KEY_TYPE_alloc) {
1474 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1475 "pointer to nonexistent bucket %llu:%llu",
1476 iter->pos.inode, iter->pos.offset);
1481 u = bch2_alloc_unpack(k);
1484 if (gen_after(u.gen, p.ptr.gen)) {
1489 if (u.data_type && u.data_type != data_type) {
1490 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1491 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s",
1492 iter->pos.inode, iter->pos.offset,
1494 bch2_data_types[u.data_type],
1495 bch2_data_types[data_type]);
1500 dst_sectors = !p.ptr.cached
1502 : &u.cached_sectors;
1503 orig_sectors = *dst_sectors;
1505 if (checked_add(*dst_sectors, sectors)) {
1506 bch2_fs_inconsistent(c,
1507 "bucket sector count overflow: %u + %lli > U16_MAX",
1508 orig_sectors, sectors);
1509 /* return an error indicating that we need full fsck */
1514 u.data_type = u.dirty_sectors || u.cached_sectors
1517 a = bch2_trans_kmalloc(trans, BKEY_ALLOC_U64s_MAX * 8);
1518 ret = PTR_ERR_OR_ZERO(a);
1522 bkey_alloc_init(&a->k_i);
1524 bch2_alloc_pack(a, u);
1525 bch2_trans_update(trans, iter, &a->k_i, 0);
1527 bch2_trans_iter_put(trans, iter);
1531 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1532 struct bch_extent_stripe_ptr p,
1533 s64 sectors, enum bch_data_type data_type,
1534 struct bch_replicas_padded *r,
1536 unsigned *nr_parity)
1538 struct bch_fs *c = trans->c;
1539 struct btree_iter *iter;
1541 struct bkey_i_stripe *s;
1544 ret = trans_get_key(trans, BTREE_ID_EC, POS(0, p.idx), &iter, &k);
1548 if (k.k->type != KEY_TYPE_stripe) {
1549 bch2_fs_inconsistent(c,
1550 "pointer to nonexistent stripe %llu",
1556 s = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1557 ret = PTR_ERR_OR_ZERO(s);
1561 bkey_reassemble(&s->k_i, k);
1563 stripe_blockcount_set(&s->v, p.block,
1564 stripe_blockcount_get(&s->v, p.block) +
1567 *nr_data = s->v.nr_blocks - s->v.nr_redundant;
1568 *nr_parity = s->v.nr_redundant;
1569 bch2_bkey_to_replicas(&r->e, bkey_i_to_s_c(&s->k_i));
1570 bch2_trans_update(trans, iter, &s->k_i, 0);
1572 bch2_trans_iter_put(trans, iter);
1576 static int bch2_trans_mark_extent(struct btree_trans *trans,
1577 struct bkey_s_c k, unsigned offset,
1578 s64 sectors, unsigned flags,
1579 enum bch_data_type data_type)
1581 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1582 const union bch_extent_entry *entry;
1583 struct extent_ptr_decoded p;
1584 struct bch_replicas_padded r;
1585 s64 dirty_sectors = 0;
1589 r.e.data_type = data_type;
1591 r.e.nr_required = 1;
1595 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1596 s64 disk_sectors = data_type == BCH_DATA_BTREE
1598 : ptr_disk_sectors_delta(p, offset, sectors, flags);
1600 ret = bch2_trans_mark_pointer(trans, p, disk_sectors,
1609 update_cached_sectors_list(trans, p.ptr.dev,
1611 } else if (!p.has_ec) {
1612 dirty_sectors += disk_sectors;
1613 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1615 struct bch_replicas_padded ec_r;
1616 unsigned nr_data, nr_parity;
1619 ret = bch2_trans_mark_stripe_ptr(trans, p.ec,
1620 disk_sectors, data_type,
1621 &ec_r, &nr_data, &nr_parity);
1626 __ptr_disk_sectors_delta(p.crc.live_size,
1627 offset, sectors, flags,
1628 p.crc.compressed_size * nr_parity,
1629 p.crc.uncompressed_size * nr_data);
1631 update_replicas_list(trans, &ec_r.e,
1632 disk_sectors + parity_sectors);
1634 r.e.nr_required = 0;
1639 update_replicas_list(trans, &r.e, dirty_sectors);
1644 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1645 struct bkey_s_c_reflink_p p,
1646 u64 idx, unsigned sectors,
1649 struct bch_fs *c = trans->c;
1650 struct btree_iter *iter;
1652 struct bkey_i_reflink_v *r_v;
1655 ret = trans_get_key(trans, BTREE_ID_REFLINK,
1656 POS(0, idx), &iter, &k);
1660 if (k.k->type != KEY_TYPE_reflink_v) {
1661 bch2_fs_inconsistent(c,
1662 "%llu:%llu len %u points to nonexistent indirect extent %llu",
1663 p.k->p.inode, p.k->p.offset, p.k->size, idx);
1668 if ((flags & BTREE_TRIGGER_OVERWRITE) &&
1669 (bkey_start_offset(k.k) < idx ||
1670 k.k->p.offset > idx + sectors))
1673 sectors = k.k->p.offset - idx;
1675 r_v = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1676 ret = PTR_ERR_OR_ZERO(r_v);
1680 bkey_reassemble(&r_v->k_i, k);
1682 le64_add_cpu(&r_v->v.refcount,
1683 !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1);
1685 if (!r_v->v.refcount) {
1686 r_v->k.type = KEY_TYPE_deleted;
1687 set_bkey_val_u64s(&r_v->k, 0);
1690 bch2_btree_iter_set_pos(iter, bkey_start_pos(k.k));
1691 BUG_ON(iter->uptodate > BTREE_ITER_NEED_PEEK);
1693 bch2_trans_update(trans, iter, &r_v->k_i, 0);
1697 bch2_trans_iter_put(trans, iter);
1701 static int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1702 struct bkey_s_c_reflink_p p, unsigned offset,
1703 s64 sectors, unsigned flags)
1705 u64 idx = le64_to_cpu(p.v->idx) + offset;
1708 sectors = abs(sectors);
1709 BUG_ON(offset + sectors > p.k->size);
1712 ret = __bch2_trans_mark_reflink_p(trans, p, idx, sectors, flags);
1717 sectors = max_t(s64, 0LL, sectors - ret);
1724 int bch2_trans_mark_key(struct btree_trans *trans, struct bkey_s_c k,
1725 unsigned offset, s64 sectors, unsigned flags)
1727 struct replicas_delta_list *d;
1728 struct bch_fs *c = trans->c;
1730 switch (k.k->type) {
1731 case KEY_TYPE_btree_ptr:
1732 case KEY_TYPE_btree_ptr_v2:
1733 sectors = !(flags & BTREE_TRIGGER_OVERWRITE)
1734 ? c->opts.btree_node_size
1735 : -c->opts.btree_node_size;
1737 return bch2_trans_mark_extent(trans, k, offset, sectors,
1738 flags, BCH_DATA_BTREE);
1739 case KEY_TYPE_extent:
1740 case KEY_TYPE_reflink_v:
1741 return bch2_trans_mark_extent(trans, k, offset, sectors,
1742 flags, BCH_DATA_USER);
1743 case KEY_TYPE_inode:
1744 d = replicas_deltas_realloc(trans, 0);
1746 if (!(flags & BTREE_TRIGGER_OVERWRITE))
1751 case KEY_TYPE_reservation: {
1752 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1754 d = replicas_deltas_realloc(trans, 0);
1756 sectors *= replicas;
1757 replicas = clamp_t(unsigned, replicas, 1,
1758 ARRAY_SIZE(d->persistent_reserved));
1760 d->persistent_reserved[replicas - 1] += sectors;
1763 case KEY_TYPE_reflink_p:
1764 return bch2_trans_mark_reflink_p(trans,
1765 bkey_s_c_to_reflink_p(k),
1766 offset, sectors, flags);
1772 int bch2_trans_mark_update(struct btree_trans *trans,
1773 struct btree_iter *iter,
1774 struct bkey_i *insert,
1777 struct btree *b = iter->l[0].b;
1778 struct btree_node_iter node_iter = iter->l[0].iter;
1779 struct bkey_packed *_k;
1782 if (unlikely(flags & BTREE_TRIGGER_NORUN))
1785 if (!btree_node_type_needs_gc(iter->btree_id))
1788 ret = bch2_trans_mark_key(trans, bkey_i_to_s_c(insert),
1789 0, insert->k.size, BTREE_TRIGGER_INSERT);
1793 if (unlikely(flags & BTREE_TRIGGER_NOOVERWRITES))
1796 while ((_k = bch2_btree_node_iter_peek(&node_iter, b))) {
1797 struct bkey unpacked;
1799 unsigned offset = 0;
1801 unsigned flags = BTREE_TRIGGER_OVERWRITE;
1803 k = bkey_disassemble(b, _k, &unpacked);
1805 if (btree_node_is_extents(b)
1806 ? bkey_cmp(insert->k.p, bkey_start_pos(k.k)) <= 0
1807 : bkey_cmp(insert->k.p, k.k->p))
1810 if (btree_node_is_extents(b)) {
1811 switch (bch2_extent_overlap(&insert->k, k.k)) {
1812 case BCH_EXTENT_OVERLAP_ALL:
1814 sectors = -((s64) k.k->size);
1816 case BCH_EXTENT_OVERLAP_BACK:
1817 offset = bkey_start_offset(&insert->k) -
1818 bkey_start_offset(k.k);
1819 sectors = bkey_start_offset(&insert->k) -
1822 case BCH_EXTENT_OVERLAP_FRONT:
1824 sectors = bkey_start_offset(k.k) -
1827 case BCH_EXTENT_OVERLAP_MIDDLE:
1828 offset = bkey_start_offset(&insert->k) -
1829 bkey_start_offset(k.k);
1830 sectors = -((s64) insert->k.size);
1831 flags |= BTREE_TRIGGER_OVERWRITE_SPLIT;
1835 BUG_ON(sectors >= 0);
1838 ret = bch2_trans_mark_key(trans, k, offset, sectors, flags);
1842 bch2_btree_node_iter_advance(&node_iter, b);
1848 /* Disk reservations: */
1850 static u64 bch2_recalc_sectors_available(struct bch_fs *c)
1852 percpu_u64_set(&c->pcpu->sectors_available, 0);
1854 return avail_factor(__bch2_fs_usage_read_short(c).free);
1857 void __bch2_disk_reservation_put(struct bch_fs *c, struct disk_reservation *res)
1859 percpu_down_read(&c->mark_lock);
1860 this_cpu_sub(c->usage[0]->online_reserved,
1862 percpu_up_read(&c->mark_lock);
1867 #define SECTORS_CACHE 1024
1869 int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
1870 unsigned sectors, int flags)
1872 struct bch_fs_pcpu *pcpu;
1874 s64 sectors_available;
1877 percpu_down_read(&c->mark_lock);
1879 pcpu = this_cpu_ptr(c->pcpu);
1881 if (sectors <= pcpu->sectors_available)
1884 v = atomic64_read(&c->sectors_available);
1887 get = min((u64) sectors + SECTORS_CACHE, old);
1889 if (get < sectors) {
1891 percpu_up_read(&c->mark_lock);
1894 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1895 old, old - get)) != old);
1897 pcpu->sectors_available += get;
1900 pcpu->sectors_available -= sectors;
1901 this_cpu_add(c->usage[0]->online_reserved, sectors);
1902 res->sectors += sectors;
1905 percpu_up_read(&c->mark_lock);
1909 percpu_down_write(&c->mark_lock);
1911 sectors_available = bch2_recalc_sectors_available(c);
1913 if (sectors <= sectors_available ||
1914 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
1915 atomic64_set(&c->sectors_available,
1916 max_t(s64, 0, sectors_available - sectors));
1917 this_cpu_add(c->usage[0]->online_reserved, sectors);
1918 res->sectors += sectors;
1921 atomic64_set(&c->sectors_available, sectors_available);
1925 percpu_up_write(&c->mark_lock);
1930 /* Startup/shutdown: */
1932 static void buckets_free_rcu(struct rcu_head *rcu)
1934 struct bucket_array *buckets =
1935 container_of(rcu, struct bucket_array, rcu);
1938 sizeof(struct bucket_array) +
1939 buckets->nbuckets * sizeof(struct bucket));
1942 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1944 struct bucket_array *buckets = NULL, *old_buckets = NULL;
1945 unsigned long *buckets_nouse = NULL;
1946 alloc_fifo free[RESERVE_NR];
1947 alloc_fifo free_inc;
1948 alloc_heap alloc_heap;
1949 copygc_heap copygc_heap;
1951 size_t btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE,
1952 ca->mi.bucket_size / c->opts.btree_node_size);
1953 /* XXX: these should be tunable */
1954 size_t reserve_none = max_t(size_t, 1, nbuckets >> 9);
1955 size_t copygc_reserve = max_t(size_t, 2, nbuckets >> 7);
1956 size_t free_inc_nr = max(max_t(size_t, 1, nbuckets >> 12),
1958 bool resize = ca->buckets[0] != NULL,
1959 start_copygc = ca->copygc_thread != NULL;
1963 memset(&free, 0, sizeof(free));
1964 memset(&free_inc, 0, sizeof(free_inc));
1965 memset(&alloc_heap, 0, sizeof(alloc_heap));
1966 memset(©gc_heap, 0, sizeof(copygc_heap));
1968 if (!(buckets = kvpmalloc(sizeof(struct bucket_array) +
1969 nbuckets * sizeof(struct bucket),
1970 GFP_KERNEL|__GFP_ZERO)) ||
1971 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
1972 sizeof(unsigned long),
1973 GFP_KERNEL|__GFP_ZERO)) ||
1974 !init_fifo(&free[RESERVE_BTREE], btree_reserve, GFP_KERNEL) ||
1975 !init_fifo(&free[RESERVE_MOVINGGC],
1976 copygc_reserve, GFP_KERNEL) ||
1977 !init_fifo(&free[RESERVE_NONE], reserve_none, GFP_KERNEL) ||
1978 !init_fifo(&free_inc, free_inc_nr, GFP_KERNEL) ||
1979 !init_heap(&alloc_heap, ALLOC_SCAN_BATCH(ca) << 1, GFP_KERNEL) ||
1980 !init_heap(©gc_heap, copygc_reserve, GFP_KERNEL))
1983 buckets->first_bucket = ca->mi.first_bucket;
1984 buckets->nbuckets = nbuckets;
1986 bch2_copygc_stop(ca);
1989 down_write(&c->gc_lock);
1990 down_write(&ca->bucket_lock);
1991 percpu_down_write(&c->mark_lock);
1994 old_buckets = bucket_array(ca);
1997 size_t n = min(buckets->nbuckets, old_buckets->nbuckets);
2001 n * sizeof(struct bucket));
2002 memcpy(buckets_nouse,
2004 BITS_TO_LONGS(n) * sizeof(unsigned long));
2007 rcu_assign_pointer(ca->buckets[0], buckets);
2008 buckets = old_buckets;
2010 swap(ca->buckets_nouse, buckets_nouse);
2013 percpu_up_write(&c->mark_lock);
2015 spin_lock(&c->freelist_lock);
2016 for (i = 0; i < RESERVE_NR; i++) {
2017 fifo_move(&free[i], &ca->free[i]);
2018 swap(ca->free[i], free[i]);
2020 fifo_move(&free_inc, &ca->free_inc);
2021 swap(ca->free_inc, free_inc);
2022 spin_unlock(&c->freelist_lock);
2024 /* with gc lock held, alloc_heap can't be in use: */
2025 swap(ca->alloc_heap, alloc_heap);
2027 /* and we shut down copygc: */
2028 swap(ca->copygc_heap, copygc_heap);
2030 nbuckets = ca->mi.nbuckets;
2033 up_write(&ca->bucket_lock);
2034 up_write(&c->gc_lock);
2038 bch2_copygc_start(c, ca))
2039 bch_err(ca, "error restarting copygc thread");
2043 free_heap(©gc_heap);
2044 free_heap(&alloc_heap);
2045 free_fifo(&free_inc);
2046 for (i = 0; i < RESERVE_NR; i++)
2047 free_fifo(&free[i]);
2048 kvpfree(buckets_nouse,
2049 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2051 call_rcu(&old_buckets->rcu, buckets_free_rcu);
2056 void bch2_dev_buckets_free(struct bch_dev *ca)
2060 free_heap(&ca->copygc_heap);
2061 free_heap(&ca->alloc_heap);
2062 free_fifo(&ca->free_inc);
2063 for (i = 0; i < RESERVE_NR; i++)
2064 free_fifo(&ca->free[i]);
2065 kvpfree(ca->buckets_nouse,
2066 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2067 kvpfree(rcu_dereference_protected(ca->buckets[0], 1),
2068 sizeof(struct bucket_array) +
2069 ca->mi.nbuckets * sizeof(struct bucket));
2071 free_percpu(ca->usage[0]);
2074 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2076 if (!(ca->usage[0] = alloc_percpu(struct bch_dev_usage)))
2079 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);;