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>
80 static inline void fs_usage_data_type_to_base(struct bch_fs_usage *fs_usage,
81 enum bch_data_type data_type,
86 fs_usage->btree += sectors;
90 fs_usage->data += sectors;
93 fs_usage->cached += sectors;
101 * Clear journal_seq_valid for buckets for which it's not needed, to prevent
104 void bch2_bucket_seq_cleanup(struct bch_fs *c)
106 u64 journal_seq = atomic64_read(&c->journal.seq);
107 u16 last_seq_ondisk = c->journal.last_seq_ondisk;
109 struct bucket_array *buckets;
111 struct bucket_mark m;
114 if (journal_seq - c->last_bucket_seq_cleanup <
115 (1U << (BUCKET_JOURNAL_SEQ_BITS - 2)))
118 c->last_bucket_seq_cleanup = journal_seq;
120 for_each_member_device(ca, c, i) {
121 down_read(&ca->bucket_lock);
122 buckets = bucket_array(ca);
124 for_each_bucket(g, buckets) {
125 bucket_cmpxchg(g, m, ({
126 if (!m.journal_seq_valid ||
127 bucket_needs_journal_commit(m, last_seq_ondisk))
130 m.journal_seq_valid = 0;
133 up_read(&ca->bucket_lock);
137 void bch2_fs_usage_initialize(struct bch_fs *c)
139 struct bch_fs_usage *usage;
142 percpu_down_write(&c->mark_lock);
143 usage = c->usage_base;
145 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
146 bch2_fs_usage_acc_to_base(c, i);
148 for (i = 0; i < BCH_REPLICAS_MAX; i++)
149 usage->reserved += usage->persistent_reserved[i];
151 for (i = 0; i < c->replicas.nr; i++) {
152 struct bch_replicas_entry *e =
153 cpu_replicas_entry(&c->replicas, i);
155 fs_usage_data_type_to_base(usage, e->data_type, usage->replicas[i]);
158 percpu_up_write(&c->mark_lock);
161 void bch2_fs_usage_scratch_put(struct bch_fs *c, struct bch_fs_usage *fs_usage)
163 if (fs_usage == c->usage_scratch)
164 mutex_unlock(&c->usage_scratch_lock);
169 struct bch_fs_usage *bch2_fs_usage_scratch_get(struct bch_fs *c)
171 struct bch_fs_usage *ret;
172 unsigned bytes = fs_usage_u64s(c) * sizeof(u64);
174 ret = kzalloc(bytes, GFP_NOWAIT|__GFP_NOWARN);
178 if (mutex_trylock(&c->usage_scratch_lock))
181 ret = kzalloc(bytes, GFP_NOFS);
185 mutex_lock(&c->usage_scratch_lock);
187 ret = c->usage_scratch;
188 memset(ret, 0, bytes);
192 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
194 struct bch_dev_usage ret;
196 memset(&ret, 0, sizeof(ret));
197 acc_u64s_percpu((u64 *) &ret,
198 (u64 __percpu *) ca->usage[0],
199 sizeof(ret) / sizeof(u64));
204 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
205 unsigned journal_seq,
208 return this_cpu_ptr(gc
210 : c->usage[journal_seq & JOURNAL_BUF_MASK]);
213 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
215 ssize_t offset = v - (u64 *) c->usage_base;
219 BUG_ON(offset < 0 || offset >= fs_usage_u64s(c));
220 percpu_rwsem_assert_held(&c->mark_lock);
223 seq = read_seqcount_begin(&c->usage_lock);
226 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
227 ret += percpu_u64_get((u64 __percpu *) c->usage[i] + offset);
228 } while (read_seqcount_retry(&c->usage_lock, seq));
233 struct bch_fs_usage *bch2_fs_usage_read(struct bch_fs *c)
235 struct bch_fs_usage *ret;
236 unsigned seq, i, v, u64s = fs_usage_u64s(c);
238 ret = kmalloc(u64s * sizeof(u64), GFP_NOFS);
242 percpu_down_read(&c->mark_lock);
244 v = fs_usage_u64s(c);
245 if (unlikely(u64s != v)) {
247 percpu_up_read(&c->mark_lock);
253 seq = read_seqcount_begin(&c->usage_lock);
254 memcpy(ret, c->usage_base, u64s * sizeof(u64));
255 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
256 acc_u64s_percpu((u64 *) ret, (u64 __percpu *) c->usage[i], u64s);
257 } while (read_seqcount_retry(&c->usage_lock, seq));
262 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx)
264 unsigned u64s = fs_usage_u64s(c);
266 BUG_ON(idx >= ARRAY_SIZE(c->usage));
269 write_seqcount_begin(&c->usage_lock);
271 acc_u64s_percpu((u64 *) c->usage_base,
272 (u64 __percpu *) c->usage[idx], u64s);
273 percpu_memset(c->usage[idx], 0, u64s * sizeof(u64));
275 write_seqcount_end(&c->usage_lock);
279 void bch2_fs_usage_to_text(struct printbuf *out,
281 struct bch_fs_usage *fs_usage)
285 pr_buf(out, "capacity:\t\t\t%llu\n", c->capacity);
287 pr_buf(out, "hidden:\t\t\t\t%llu\n",
289 pr_buf(out, "data:\t\t\t\t%llu\n",
291 pr_buf(out, "cached:\t\t\t\t%llu\n",
293 pr_buf(out, "reserved:\t\t\t%llu\n",
295 pr_buf(out, "nr_inodes:\t\t\t%llu\n",
296 fs_usage->nr_inodes);
297 pr_buf(out, "online reserved:\t\t%llu\n",
298 fs_usage->online_reserved);
301 i < ARRAY_SIZE(fs_usage->persistent_reserved);
303 pr_buf(out, "%u replicas:\n", i + 1);
304 pr_buf(out, "\treserved:\t\t%llu\n",
305 fs_usage->persistent_reserved[i]);
308 for (i = 0; i < c->replicas.nr; i++) {
309 struct bch_replicas_entry *e =
310 cpu_replicas_entry(&c->replicas, i);
313 bch2_replicas_entry_to_text(out, e);
314 pr_buf(out, ":\t%llu\n", fs_usage->replicas[i]);
318 #define RESERVE_FACTOR 6
320 static u64 reserve_factor(u64 r)
322 return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR);
325 static u64 avail_factor(u64 r)
327 return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
330 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage *fs_usage)
332 return min(fs_usage->hidden +
335 reserve_factor(fs_usage->reserved +
336 fs_usage->online_reserved),
340 static struct bch_fs_usage_short
341 __bch2_fs_usage_read_short(struct bch_fs *c)
343 struct bch_fs_usage_short ret;
346 ret.capacity = c->capacity -
347 bch2_fs_usage_read_one(c, &c->usage_base->hidden);
349 data = bch2_fs_usage_read_one(c, &c->usage_base->data) +
350 bch2_fs_usage_read_one(c, &c->usage_base->btree);
351 reserved = bch2_fs_usage_read_one(c, &c->usage_base->reserved) +
352 bch2_fs_usage_read_one(c, &c->usage_base->online_reserved);
354 ret.used = min(ret.capacity, data + reserve_factor(reserved));
355 ret.free = ret.capacity - ret.used;
357 ret.nr_inodes = bch2_fs_usage_read_one(c, &c->usage_base->nr_inodes);
362 struct bch_fs_usage_short
363 bch2_fs_usage_read_short(struct bch_fs *c)
365 struct bch_fs_usage_short ret;
367 percpu_down_read(&c->mark_lock);
368 ret = __bch2_fs_usage_read_short(c);
369 percpu_up_read(&c->mark_lock);
374 static inline int is_unavailable_bucket(struct bucket_mark m)
376 return !is_available_bucket(m);
379 static inline int is_fragmented_bucket(struct bucket_mark m,
382 if (!m.owned_by_allocator &&
383 m.data_type == BCH_DATA_user &&
384 bucket_sectors_used(m))
385 return max_t(int, 0, (int) ca->mi.bucket_size -
386 bucket_sectors_used(m));
390 static inline int is_stripe_data_bucket(struct bucket_mark m)
392 return m.stripe && m.data_type != BCH_DATA_parity;
395 static inline int bucket_stripe_sectors(struct bucket_mark m)
397 return is_stripe_data_bucket(m) ? m.dirty_sectors : 0;
400 static inline enum bch_data_type bucket_type(struct bucket_mark m)
402 return m.cached_sectors && !m.dirty_sectors
407 static bool bucket_became_unavailable(struct bucket_mark old,
408 struct bucket_mark new)
410 return is_available_bucket(old) &&
411 !is_available_bucket(new);
414 int bch2_fs_usage_apply(struct bch_fs *c,
415 struct bch_fs_usage *fs_usage,
416 struct disk_reservation *disk_res,
417 unsigned journal_seq)
419 s64 added = fs_usage->data + fs_usage->reserved;
420 s64 should_not_have_added;
423 percpu_rwsem_assert_held(&c->mark_lock);
426 * Not allowed to reduce sectors_available except by getting a
429 should_not_have_added = added - (s64) (disk_res ? disk_res->sectors : 0);
430 if (WARN_ONCE(should_not_have_added > 0,
431 "disk usage increased by %lli more than reservation of %llu",
432 added, disk_res ? disk_res->sectors : 0)) {
433 atomic64_sub(should_not_have_added, &c->sectors_available);
434 added -= should_not_have_added;
439 disk_res->sectors -= added;
440 fs_usage->online_reserved -= added;
444 acc_u64s((u64 *) fs_usage_ptr(c, journal_seq, false),
445 (u64 *) fs_usage, fs_usage_u64s(c));
451 static inline void account_bucket(struct bch_fs_usage *fs_usage,
452 struct bch_dev_usage *dev_usage,
453 enum bch_data_type type,
456 if (type == BCH_DATA_sb || type == BCH_DATA_journal)
457 fs_usage->hidden += size;
459 dev_usage->buckets[type] += nr;
462 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
463 struct bch_fs_usage *fs_usage,
464 struct bucket_mark old, struct bucket_mark new,
467 struct bch_dev_usage *u;
469 percpu_rwsem_assert_held(&c->mark_lock);
472 u = this_cpu_ptr(ca->usage[gc]);
474 if (bucket_type(old))
475 account_bucket(fs_usage, u, bucket_type(old),
476 -1, -ca->mi.bucket_size);
478 if (bucket_type(new))
479 account_bucket(fs_usage, u, bucket_type(new),
480 1, ca->mi.bucket_size);
483 (int) new.owned_by_allocator - (int) old.owned_by_allocator;
484 u->buckets_unavailable +=
485 is_unavailable_bucket(new) - is_unavailable_bucket(old);
487 u->buckets_ec += (int) new.stripe - (int) old.stripe;
488 u->sectors_ec += bucket_stripe_sectors(new) -
489 bucket_stripe_sectors(old);
491 u->sectors[old.data_type] -= old.dirty_sectors;
492 u->sectors[new.data_type] += new.dirty_sectors;
493 u->sectors[BCH_DATA_cached] +=
494 (int) new.cached_sectors - (int) old.cached_sectors;
495 u->sectors_fragmented +=
496 is_fragmented_bucket(new, ca) - is_fragmented_bucket(old, ca);
499 if (!is_available_bucket(old) && is_available_bucket(new))
500 bch2_wake_allocator(ca);
504 void bch2_dev_usage_from_buckets(struct bch_fs *c)
507 struct bucket_mark old = { .v.counter = 0 };
508 struct bucket_array *buckets;
513 c->usage_base->hidden = 0;
515 for_each_member_device(ca, c, i) {
516 for_each_possible_cpu(cpu)
517 memset(per_cpu_ptr(ca->usage[0], cpu), 0,
518 sizeof(*ca->usage[0]));
520 buckets = bucket_array(ca);
522 for_each_bucket(g, buckets)
523 bch2_dev_usage_update(c, ca, c->usage_base,
524 old, g->mark, false);
528 static inline int update_replicas(struct bch_fs *c,
529 struct bch_fs_usage *fs_usage,
530 struct bch_replicas_entry *r,
533 int idx = bch2_replicas_entry_idx(c, r);
541 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
542 fs_usage->replicas[idx] += sectors;
546 static inline void update_cached_sectors(struct bch_fs *c,
547 struct bch_fs_usage *fs_usage,
548 unsigned dev, s64 sectors)
550 struct bch_replicas_padded r;
552 bch2_replicas_entry_cached(&r.e, dev);
554 update_replicas(c, fs_usage, &r.e, sectors);
557 static struct replicas_delta_list *
558 replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
560 struct replicas_delta_list *d = trans->fs_usage_deltas;
561 unsigned new_size = d ? (d->size + more) * 2 : 128;
563 if (!d || d->used + more > d->size) {
564 d = krealloc(d, sizeof(*d) + new_size, GFP_NOIO|__GFP_ZERO);
568 trans->fs_usage_deltas = d;
573 static inline void update_replicas_list(struct btree_trans *trans,
574 struct bch_replicas_entry *r,
577 struct replicas_delta_list *d;
578 struct replicas_delta *n;
584 b = replicas_entry_bytes(r) + 8;
585 d = replicas_deltas_realloc(trans, b);
587 n = (void *) d->d + d->used;
589 memcpy(&n->r, r, replicas_entry_bytes(r));
593 static inline void update_cached_sectors_list(struct btree_trans *trans,
594 unsigned dev, s64 sectors)
596 struct bch_replicas_padded r;
598 bch2_replicas_entry_cached(&r.e, dev);
600 update_replicas_list(trans, &r.e, sectors);
603 static inline struct replicas_delta *
604 replicas_delta_next(struct replicas_delta *d)
606 return (void *) d + replicas_entry_bytes(&d->r) + 8;
609 int bch2_replicas_delta_list_apply(struct bch_fs *c,
610 struct bch_fs_usage *fs_usage,
611 struct replicas_delta_list *r)
613 struct replicas_delta *d = r->d;
614 struct replicas_delta *top = (void *) r->d + r->used;
617 for (d = r->d; d != top; d = replicas_delta_next(d))
618 if (update_replicas(c, fs_usage, &d->r, d->delta)) {
626 fs_usage->nr_inodes += r->nr_inodes;
628 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
629 fs_usage->reserved += r->persistent_reserved[i];
630 fs_usage->persistent_reserved[i] += r->persistent_reserved[i];
635 for (d = r->d; d != top; d = replicas_delta_next(d))
636 update_replicas(c, fs_usage, &d->r, -d->delta);
640 #define do_mark_fn(fn, c, pos, flags, ...) \
644 percpu_rwsem_assert_held(&c->mark_lock); \
646 for (gc = 0; gc < 2 && !ret; gc++) \
647 if (!gc == !(flags & BTREE_TRIGGER_GC) || \
648 (gc && gc_visited(c, pos))) \
649 ret = fn(c, __VA_ARGS__, gc); \
653 static int __bch2_invalidate_bucket(struct bch_fs *c, struct bch_dev *ca,
654 size_t b, struct bucket_mark *ret,
657 struct bch_fs_usage *fs_usage = fs_usage_ptr(c, 0, gc);
658 struct bucket *g = __bucket(ca, b, gc);
659 struct bucket_mark old, new;
661 old = bucket_cmpxchg(g, new, ({
662 BUG_ON(!is_available_bucket(new));
664 new.owned_by_allocator = true;
666 new.cached_sectors = 0;
667 new.dirty_sectors = 0;
671 bch2_dev_usage_update(c, ca, fs_usage, old, new, gc);
673 if (old.cached_sectors)
674 update_cached_sectors(c, fs_usage, ca->dev_idx,
675 -((s64) old.cached_sectors));
682 void bch2_invalidate_bucket(struct bch_fs *c, struct bch_dev *ca,
683 size_t b, struct bucket_mark *old)
685 do_mark_fn(__bch2_invalidate_bucket, c, gc_phase(GC_PHASE_START), 0,
688 if (!old->owned_by_allocator && old->cached_sectors)
689 trace_invalidate(ca, bucket_to_sector(ca, b),
690 old->cached_sectors);
693 static int __bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
694 size_t b, bool owned_by_allocator,
697 struct bch_fs_usage *fs_usage = fs_usage_ptr(c, 0, gc);
698 struct bucket *g = __bucket(ca, b, gc);
699 struct bucket_mark old, new;
701 old = bucket_cmpxchg(g, new, ({
702 new.owned_by_allocator = owned_by_allocator;
705 bch2_dev_usage_update(c, ca, fs_usage, old, new, gc);
708 !owned_by_allocator && !old.owned_by_allocator);
713 void bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
714 size_t b, bool owned_by_allocator,
715 struct gc_pos pos, unsigned flags)
719 do_mark_fn(__bch2_mark_alloc_bucket, c, pos, flags,
720 ca, b, owned_by_allocator);
725 static int bch2_mark_alloc(struct bch_fs *c,
726 struct bkey_s_c old, struct bkey_s_c new,
727 struct bch_fs_usage *fs_usage,
728 u64 journal_seq, unsigned flags)
730 bool gc = flags & BTREE_TRIGGER_GC;
731 struct bkey_alloc_unpacked u;
734 struct bucket_mark old_m, m;
736 /* We don't do anything for deletions - do we?: */
737 if (new.k->type != KEY_TYPE_alloc)
741 * alloc btree is read in by bch2_alloc_read, not gc:
743 if ((flags & BTREE_TRIGGER_GC) &&
744 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
747 ca = bch_dev_bkey_exists(c, new.k->p.inode);
749 if (new.k->p.offset >= ca->mi.nbuckets)
752 g = __bucket(ca, new.k->p.offset, gc);
753 u = bch2_alloc_unpack(new);
755 old_m = bucket_cmpxchg(g, m, ({
757 m.data_type = u.data_type;
758 m.dirty_sectors = u.dirty_sectors;
759 m.cached_sectors = u.cached_sectors;
762 m.journal_seq_valid = 1;
763 m.journal_seq = journal_seq;
767 bch2_dev_usage_update(c, ca, fs_usage, old_m, m, gc);
769 g->io_time[READ] = u.read_time;
770 g->io_time[WRITE] = u.write_time;
771 g->oldest_gen = u.oldest_gen;
775 * need to know if we're getting called from the invalidate path or
779 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
780 old_m.cached_sectors) {
781 update_cached_sectors(c, fs_usage, ca->dev_idx,
782 -old_m.cached_sectors);
783 trace_invalidate(ca, bucket_to_sector(ca, new.k->p.offset),
784 old_m.cached_sectors);
790 #define checked_add(a, b) \
792 unsigned _res = (unsigned) (a) + (b); \
793 bool overflow = _res > U16_MAX; \
800 static int __bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
801 size_t b, enum bch_data_type data_type,
802 unsigned sectors, bool gc)
804 struct bucket *g = __bucket(ca, b, gc);
805 struct bucket_mark old, new;
808 BUG_ON(data_type != BCH_DATA_sb &&
809 data_type != BCH_DATA_journal);
811 old = bucket_cmpxchg(g, new, ({
812 new.data_type = data_type;
813 overflow = checked_add(new.dirty_sectors, sectors);
816 bch2_fs_inconsistent_on(old.data_type &&
817 old.data_type != data_type, c,
818 "different types of data in same bucket: %s, %s",
819 bch2_data_types[old.data_type],
820 bch2_data_types[data_type]);
822 bch2_fs_inconsistent_on(overflow, c,
823 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > U16_MAX",
824 ca->dev_idx, b, new.gen,
825 bch2_data_types[old.data_type ?: data_type],
826 old.dirty_sectors, sectors);
829 bch2_dev_usage_update(c, ca, fs_usage_ptr(c, 0, gc),
835 void bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
836 size_t b, enum bch_data_type type,
837 unsigned sectors, struct gc_pos pos,
840 BUG_ON(type != BCH_DATA_sb &&
841 type != BCH_DATA_journal);
846 do_mark_fn(__bch2_mark_metadata_bucket, c, pos, flags,
847 ca, b, type, sectors);
849 __bch2_mark_metadata_bucket(c, ca, b, type, sectors, 0);
855 static s64 disk_sectors_scaled(unsigned n, unsigned d, unsigned sectors)
857 return DIV_ROUND_UP(sectors * n, d);
860 static s64 __ptr_disk_sectors_delta(unsigned old_size,
861 unsigned offset, s64 delta,
863 unsigned n, unsigned d)
867 if (flags & BTREE_TRIGGER_OVERWRITE_SPLIT) {
868 BUG_ON(offset + -delta > old_size);
870 return -disk_sectors_scaled(n, d, old_size) +
871 disk_sectors_scaled(n, d, offset) +
872 disk_sectors_scaled(n, d, old_size - offset + delta);
873 } else if (flags & BTREE_TRIGGER_OVERWRITE) {
874 BUG_ON(offset + -delta > old_size);
876 return -disk_sectors_scaled(n, d, old_size) +
877 disk_sectors_scaled(n, d, old_size + delta);
879 return disk_sectors_scaled(n, d, delta);
883 static s64 ptr_disk_sectors_delta(struct extent_ptr_decoded p,
884 unsigned offset, s64 delta,
887 return __ptr_disk_sectors_delta(p.crc.live_size,
888 offset, delta, flags,
889 p.crc.compressed_size,
890 p.crc.uncompressed_size);
893 static int check_bucket_ref(struct bch_fs *c, struct bkey_s_c k,
894 const struct bch_extent_ptr *ptr,
895 s64 sectors, enum bch_data_type ptr_data_type,
896 u8 bucket_gen, u8 bucket_data_type,
897 u16 dirty_sectors, u16 cached_sectors)
899 size_t bucket_nr = PTR_BUCKET_NR(bch_dev_bkey_exists(c, ptr->dev), ptr);
900 u16 bucket_sectors = !ptr->cached
905 if (gen_after(ptr->gen, bucket_gen)) {
906 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
907 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
909 ptr->dev, bucket_nr, bucket_gen,
910 bch2_data_types[bucket_data_type ?: ptr_data_type],
912 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
916 if (gen_cmp(bucket_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
917 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
918 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
920 ptr->dev, bucket_nr, bucket_gen,
921 bch2_data_types[bucket_data_type ?: ptr_data_type],
923 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
927 if (bucket_gen != ptr->gen && !ptr->cached) {
928 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
929 "bucket %u:%zu gen %u data type %s: stale dirty ptr (gen %u)\n"
931 ptr->dev, bucket_nr, bucket_gen,
932 bch2_data_types[bucket_data_type ?: ptr_data_type],
934 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
938 if (bucket_gen != ptr->gen)
941 if (bucket_data_type && ptr_data_type &&
942 bucket_data_type != ptr_data_type) {
943 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
944 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
946 ptr->dev, bucket_nr, bucket_gen,
947 bch2_data_types[bucket_data_type],
948 bch2_data_types[ptr_data_type],
949 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
953 if ((unsigned) (bucket_sectors + sectors) > U16_MAX) {
954 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
955 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
957 ptr->dev, bucket_nr, bucket_gen,
958 bch2_data_types[bucket_data_type ?: ptr_data_type],
959 bucket_sectors, sectors,
960 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
967 static int bucket_set_stripe(struct bch_fs *c, struct bkey_s_c k,
969 struct bch_fs_usage *fs_usage,
970 u64 journal_seq, unsigned flags,
973 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
974 unsigned nr_data = s->nr_blocks - s->nr_redundant;
975 bool parity = ptr_idx >= nr_data;
976 const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
977 bool gc = flags & BTREE_TRIGGER_GC;
978 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
979 struct bucket *g = PTR_BUCKET(ca, ptr, gc);
980 struct bucket_mark new, old;
985 g->ec_redundancy = s->nr_redundant;
987 old = bucket_cmpxchg(g, new, ({
988 ret = check_bucket_ref(c, k, ptr, 0, 0, new.gen, new.data_type,
989 new.dirty_sectors, new.cached_sectors);
993 if (new.stripe && enabled)
994 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
995 "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
996 ptr->dev, PTR_BUCKET_NR(ca, ptr), new.gen,
997 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
999 if (!new.stripe && !enabled)
1000 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1001 "bucket %u:%zu gen %u: deleting stripe but not marked\n%s",
1002 ptr->dev, PTR_BUCKET_NR(ca, ptr), new.gen,
1003 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
1005 new.stripe = enabled;
1007 if ((flags & BTREE_TRIGGER_GC) && parity) {
1008 new.data_type = enabled ? BCH_DATA_parity : 0;
1009 new.dirty_sectors = enabled ? le16_to_cpu(s->sectors): 0;
1013 new.journal_seq_valid = 1;
1014 new.journal_seq = journal_seq;
1019 g->ec_redundancy = 0;
1021 bch2_dev_usage_update(c, ca, fs_usage, old, new, gc);
1025 static int __mark_pointer(struct bch_fs *c, struct bkey_s_c k,
1026 const struct bch_extent_ptr *ptr,
1027 s64 sectors, enum bch_data_type ptr_data_type,
1028 u8 bucket_gen, u8 *bucket_data_type,
1029 u16 *dirty_sectors, u16 *cached_sectors)
1031 u16 *dst_sectors = !ptr->cached
1034 int ret = check_bucket_ref(c, k, ptr, sectors, ptr_data_type,
1035 bucket_gen, *bucket_data_type,
1036 *dirty_sectors, *cached_sectors);
1041 *dst_sectors += sectors;
1042 *bucket_data_type = *dirty_sectors || *cached_sectors
1043 ? ptr_data_type : 0;
1047 static int bch2_mark_pointer(struct bch_fs *c, struct bkey_s_c k,
1048 struct extent_ptr_decoded p,
1049 s64 sectors, enum bch_data_type data_type,
1050 struct bch_fs_usage *fs_usage,
1051 u64 journal_seq, unsigned flags)
1053 bool gc = flags & BTREE_TRIGGER_GC;
1054 struct bucket_mark old, new;
1055 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
1056 struct bucket *g = PTR_BUCKET(ca, &p.ptr, gc);
1057 u8 bucket_data_type;
1061 v = atomic64_read(&g->_mark.v);
1063 new.v.counter = old.v.counter = v;
1064 bucket_data_type = new.data_type;
1066 ret = __mark_pointer(c, k, &p.ptr, sectors, data_type, new.gen,
1069 &new.cached_sectors);
1073 new.data_type = bucket_data_type;
1076 new.journal_seq_valid = 1;
1077 new.journal_seq = journal_seq;
1080 if (flags & BTREE_TRIGGER_NOATOMIC) {
1084 } while ((v = atomic64_cmpxchg(&g->_mark.v,
1086 new.v.counter)) != old.v.counter);
1088 bch2_dev_usage_update(c, ca, fs_usage, old, new, gc);
1090 BUG_ON(!gc && bucket_became_unavailable(old, new));
1095 static int bch2_mark_stripe_ptr(struct bch_fs *c,
1096 struct bch_extent_stripe_ptr p,
1097 enum bch_data_type data_type,
1098 struct bch_fs_usage *fs_usage,
1099 s64 sectors, unsigned flags)
1101 bool gc = flags & BTREE_TRIGGER_GC;
1102 struct bch_replicas_padded r;
1104 unsigned i, blocks_nonempty = 0;
1106 m = genradix_ptr(&c->stripes[gc], p.idx);
1108 spin_lock(&c->ec_stripes_heap_lock);
1110 if (!m || !m->alive) {
1111 spin_unlock(&c->ec_stripes_heap_lock);
1112 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
1117 m->block_sectors[p.block] += sectors;
1121 for (i = 0; i < m->nr_blocks; i++)
1122 blocks_nonempty += m->block_sectors[i] != 0;
1124 if (m->blocks_nonempty != blocks_nonempty) {
1125 m->blocks_nonempty = blocks_nonempty;
1127 bch2_stripes_heap_update(c, m, p.idx);
1130 spin_unlock(&c->ec_stripes_heap_lock);
1132 r.e.data_type = data_type;
1133 update_replicas(c, fs_usage, &r.e, sectors);
1138 static int bch2_mark_extent(struct bch_fs *c,
1139 struct bkey_s_c old, struct bkey_s_c new,
1140 unsigned offset, s64 sectors,
1141 enum bch_data_type data_type,
1142 struct bch_fs_usage *fs_usage,
1143 unsigned journal_seq, unsigned flags)
1145 struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
1146 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1147 const union bch_extent_entry *entry;
1148 struct extent_ptr_decoded p;
1149 struct bch_replicas_padded r;
1150 s64 dirty_sectors = 0;
1154 r.e.data_type = data_type;
1156 r.e.nr_required = 1;
1160 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1161 s64 disk_sectors = data_type == BCH_DATA_btree
1163 : ptr_disk_sectors_delta(p, offset, sectors, flags);
1165 ret = bch2_mark_pointer(c, k, p, disk_sectors, data_type,
1166 fs_usage, journal_seq, flags);
1174 update_cached_sectors(c, fs_usage, p.ptr.dev,
1176 } else if (!p.has_ec) {
1177 dirty_sectors += disk_sectors;
1178 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1180 ret = bch2_mark_stripe_ptr(c, p.ec, data_type,
1181 fs_usage, disk_sectors, flags);
1186 * There may be other dirty pointers in this extent, but
1187 * if so they're not required for mounting if we have an
1188 * erasure coded pointer in this extent:
1190 r.e.nr_required = 0;
1195 update_replicas(c, fs_usage, &r.e, dirty_sectors);
1200 static int bch2_mark_stripe(struct bch_fs *c,
1201 struct bkey_s_c old, struct bkey_s_c new,
1202 struct bch_fs_usage *fs_usage,
1203 u64 journal_seq, unsigned flags)
1205 bool gc = flags & BTREE_TRIGGER_GC;
1206 size_t idx = new.k->p.offset;
1207 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
1208 ? bkey_s_c_to_stripe(old).v : NULL;
1209 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
1210 ? bkey_s_c_to_stripe(new).v : NULL;
1211 struct stripe *m = genradix_ptr(&c->stripes[gc], idx);
1215 if (!m || (old_s && !m->alive)) {
1216 bch_err_ratelimited(c, "error marking nonexistent stripe %zu",
1223 for (i = 0; i < old_s->nr_blocks; i++) {
1224 ret = bucket_set_stripe(c, old, i, fs_usage,
1225 journal_seq, flags, false);
1230 if (!gc && m->on_heap) {
1231 spin_lock(&c->ec_stripes_heap_lock);
1232 bch2_stripes_heap_del(c, m, idx);
1233 spin_unlock(&c->ec_stripes_heap_lock);
1237 update_replicas(c, fs_usage, &m->r.e,
1238 -((s64) m->sectors * m->nr_redundant));
1240 memset(m, 0, sizeof(*m));
1242 BUG_ON(old_s && new_s->nr_blocks != old_s->nr_blocks);
1243 BUG_ON(old_s && new_s->nr_redundant != old_s->nr_redundant);
1245 for (i = 0; i < new_s->nr_blocks; i++) {
1247 memcmp(new_s->ptrs + i,
1249 sizeof(struct bch_extent_ptr))) {
1252 bucket_set_stripe(c, old, i, fs_usage,
1253 journal_seq, flags, false);
1257 ret = bucket_set_stripe(c, new, i, fs_usage,
1258 journal_seq, flags, true);
1265 m->sectors = le16_to_cpu(new_s->sectors);
1266 m->algorithm = new_s->algorithm;
1267 m->nr_blocks = new_s->nr_blocks;
1268 m->nr_redundant = new_s->nr_redundant;
1269 m->blocks_nonempty = 0;
1271 for (i = 0; i < new_s->nr_blocks; i++) {
1272 m->block_sectors[i] =
1273 stripe_blockcount_get(new_s, i);
1274 m->blocks_nonempty += !!m->block_sectors[i];
1278 update_replicas(c, fs_usage, &m->r.e,
1279 -((s64) m->sectors * m->nr_redundant));
1281 bch2_bkey_to_replicas(&m->r.e, new);
1284 update_replicas(c, fs_usage, &m->r.e,
1285 ((s64) m->sectors * m->nr_redundant));
1288 spin_lock(&c->ec_stripes_heap_lock);
1289 bch2_stripes_heap_update(c, m, idx);
1290 spin_unlock(&c->ec_stripes_heap_lock);
1297 static int bch2_mark_key_locked(struct bch_fs *c,
1298 struct bkey_s_c old,
1299 struct bkey_s_c new,
1300 unsigned offset, s64 sectors,
1301 struct bch_fs_usage *fs_usage,
1302 u64 journal_seq, unsigned flags)
1304 struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
1307 BUG_ON(!(flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)));
1311 if (!fs_usage || (flags & BTREE_TRIGGER_GC))
1312 fs_usage = fs_usage_ptr(c, journal_seq,
1313 flags & BTREE_TRIGGER_GC);
1315 switch (k.k->type) {
1316 case KEY_TYPE_alloc:
1317 ret = bch2_mark_alloc(c, old, new, fs_usage, journal_seq, flags);
1319 case KEY_TYPE_btree_ptr:
1320 case KEY_TYPE_btree_ptr_v2:
1321 sectors = !(flags & BTREE_TRIGGER_OVERWRITE)
1322 ? c->opts.btree_node_size
1323 : -c->opts.btree_node_size;
1325 ret = bch2_mark_extent(c, old, new, offset, sectors,
1326 BCH_DATA_btree, fs_usage, journal_seq, flags);
1328 case KEY_TYPE_extent:
1329 case KEY_TYPE_reflink_v:
1330 ret = bch2_mark_extent(c, old, new, offset, sectors,
1331 BCH_DATA_user, fs_usage, journal_seq, flags);
1333 case KEY_TYPE_stripe:
1334 ret = bch2_mark_stripe(c, old, new, fs_usage, journal_seq, flags);
1336 case KEY_TYPE_inode:
1337 if (!(flags & BTREE_TRIGGER_OVERWRITE))
1338 fs_usage->nr_inodes++;
1340 fs_usage->nr_inodes--;
1342 case KEY_TYPE_reservation: {
1343 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1345 sectors *= replicas;
1346 replicas = clamp_t(unsigned, replicas, 1,
1347 ARRAY_SIZE(fs_usage->persistent_reserved));
1349 fs_usage->reserved += sectors;
1350 fs_usage->persistent_reserved[replicas - 1] += sectors;
1360 int bch2_mark_key(struct bch_fs *c, struct bkey_s_c new,
1361 unsigned offset, s64 sectors,
1362 struct bch_fs_usage *fs_usage,
1363 u64 journal_seq, unsigned flags)
1365 struct bkey deleted;
1366 struct bkey_s_c old = (struct bkey_s_c) { &deleted, NULL };
1369 bkey_init(&deleted);
1371 percpu_down_read(&c->mark_lock);
1372 ret = bch2_mark_key_locked(c, old, new, offset, sectors,
1373 fs_usage, journal_seq,
1374 BTREE_TRIGGER_INSERT|flags);
1375 percpu_up_read(&c->mark_lock);
1380 int bch2_mark_update(struct btree_trans *trans,
1381 struct btree_iter *iter,
1383 struct bch_fs_usage *fs_usage,
1386 struct bch_fs *c = trans->c;
1387 struct btree *b = iter_l(iter)->b;
1388 struct btree_node_iter node_iter = iter_l(iter)->iter;
1389 struct bkey_packed *_old;
1390 struct bkey_s_c old;
1391 struct bkey unpacked;
1394 if (unlikely(flags & BTREE_TRIGGER_NORUN))
1397 if (!btree_node_type_needs_gc(iter->btree_id))
1400 bkey_init(&unpacked);
1401 old = (struct bkey_s_c) { &unpacked, NULL };
1403 if (!btree_node_type_is_extents(iter->btree_id)) {
1404 if (btree_iter_type(iter) != BTREE_ITER_CACHED) {
1405 _old = bch2_btree_node_iter_peek(&node_iter, b);
1407 old = bkey_disassemble(b, _old, &unpacked);
1409 struct bkey_cached *ck = (void *) iter->l[0].b;
1412 old = bkey_i_to_s_c(ck->k);
1415 if (old.k->type == new->k.type) {
1416 bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
1417 fs_usage, trans->journal_res.seq,
1418 BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE|flags);
1421 bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
1422 fs_usage, trans->journal_res.seq,
1423 BTREE_TRIGGER_INSERT|flags);
1424 bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
1425 fs_usage, trans->journal_res.seq,
1426 BTREE_TRIGGER_OVERWRITE|flags);
1429 BUG_ON(btree_iter_type(iter) == BTREE_ITER_CACHED);
1430 bch2_mark_key_locked(c, old, bkey_i_to_s_c(new),
1432 fs_usage, trans->journal_res.seq,
1433 BTREE_TRIGGER_INSERT|flags);
1435 while ((_old = bch2_btree_node_iter_peek(&node_iter, b))) {
1436 unsigned offset = 0;
1439 old = bkey_disassemble(b, _old, &unpacked);
1440 sectors = -((s64) old.k->size);
1442 flags |= BTREE_TRIGGER_OVERWRITE;
1444 if (bkey_cmp(new->k.p, bkey_start_pos(old.k)) <= 0)
1447 switch (bch2_extent_overlap(&new->k, old.k)) {
1448 case BCH_EXTENT_OVERLAP_ALL:
1450 sectors = -((s64) old.k->size);
1452 case BCH_EXTENT_OVERLAP_BACK:
1453 offset = bkey_start_offset(&new->k) -
1454 bkey_start_offset(old.k);
1455 sectors = bkey_start_offset(&new->k) -
1458 case BCH_EXTENT_OVERLAP_FRONT:
1460 sectors = bkey_start_offset(old.k) -
1463 case BCH_EXTENT_OVERLAP_MIDDLE:
1464 offset = bkey_start_offset(&new->k) -
1465 bkey_start_offset(old.k);
1466 sectors = -((s64) new->k.size);
1467 flags |= BTREE_TRIGGER_OVERWRITE_SPLIT;
1471 BUG_ON(sectors >= 0);
1473 ret = bch2_mark_key_locked(c, old, bkey_i_to_s_c(new),
1474 offset, sectors, fs_usage,
1475 trans->journal_res.seq, flags) ?: 1;
1479 bch2_btree_node_iter_advance(&node_iter, b);
1486 void bch2_trans_fs_usage_apply(struct btree_trans *trans,
1487 struct bch_fs_usage *fs_usage)
1489 struct bch_fs *c = trans->c;
1490 struct btree_insert_entry *i;
1491 static int warned_disk_usage = 0;
1492 u64 disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1495 if (!bch2_fs_usage_apply(c, fs_usage, trans->disk_res,
1496 trans->journal_res.seq) ||
1497 warned_disk_usage ||
1498 xchg(&warned_disk_usage, 1))
1501 bch_err(c, "disk usage increased more than %llu sectors reserved",
1504 trans_for_each_update(trans, i) {
1505 pr_err("while inserting");
1506 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(i->k));
1508 pr_err("overlapping with");
1510 if (btree_iter_type(i->iter) != BTREE_ITER_CACHED) {
1511 struct btree *b = iter_l(i->iter)->b;
1512 struct btree_node_iter node_iter = iter_l(i->iter)->iter;
1513 struct bkey_packed *_k;
1515 while ((_k = bch2_btree_node_iter_peek(&node_iter, b))) {
1516 struct bkey unpacked;
1519 pr_info("_k %px format %u", _k, _k->format);
1520 k = bkey_disassemble(b, _k, &unpacked);
1522 if (btree_node_is_extents(b)
1523 ? bkey_cmp(i->k->k.p, bkey_start_pos(k.k)) <= 0
1524 : bkey_cmp(i->k->k.p, k.k->p))
1527 bch2_bkey_val_to_text(&PBUF(buf), c, k);
1530 bch2_btree_node_iter_advance(&node_iter, b);
1533 struct bkey_cached *ck = (void *) i->iter->l[0].b;
1536 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(ck->k));
1545 static struct btree_iter *trans_get_update(struct btree_trans *trans,
1546 enum btree_id btree_id, struct bpos pos,
1549 struct btree_insert_entry *i;
1551 trans_for_each_update(trans, i)
1552 if (i->iter->btree_id == btree_id &&
1553 (btree_node_type_is_extents(btree_id)
1554 ? bkey_cmp(pos, bkey_start_pos(&i->k->k)) >= 0 &&
1555 bkey_cmp(pos, i->k->k.p) < 0
1556 : !bkey_cmp(pos, i->iter->pos))) {
1557 *k = bkey_i_to_s_c(i->k);
1564 static int trans_get_key(struct btree_trans *trans,
1565 enum btree_id btree_id, struct bpos pos,
1566 struct btree_iter **iter,
1569 unsigned flags = btree_id != BTREE_ID_ALLOC
1571 : BTREE_ITER_CACHED;
1574 *iter = trans_get_update(trans, btree_id, pos, k);
1578 *iter = bch2_trans_get_iter(trans, btree_id, pos,
1579 flags|BTREE_ITER_INTENT);
1580 *k = __bch2_btree_iter_peek(*iter, flags);
1583 bch2_trans_iter_put(trans, *iter);
1587 static int bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter **_iter,
1588 const struct bch_extent_ptr *ptr,
1589 struct bkey_alloc_unpacked *u)
1591 struct bch_fs *c = trans->c;
1592 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
1593 struct bpos pos = POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
1595 struct btree_iter *iter;
1599 iter = trans_get_update(trans, BTREE_ID_ALLOC, pos, &k);
1601 *u = bch2_alloc_unpack(k);
1603 iter = bch2_trans_get_iter(trans, BTREE_ID_ALLOC, pos,
1605 BTREE_ITER_CACHED_NOFILL|
1607 ret = bch2_btree_iter_traverse(iter);
1609 bch2_trans_iter_put(trans, iter);
1613 percpu_down_read(&c->mark_lock);
1614 g = bucket(ca, pos.offset);
1615 *u = alloc_mem_to_key(g, READ_ONCE(g->mark));
1616 percpu_up_read(&c->mark_lock);
1623 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1624 struct bkey_s_c k, struct extent_ptr_decoded p,
1625 s64 sectors, enum bch_data_type data_type)
1627 struct bch_fs *c = trans->c;
1628 struct btree_iter *iter;
1629 struct bkey_alloc_unpacked u;
1630 struct bkey_i_alloc *a;
1633 ret = bch2_trans_start_alloc_update(trans, &iter, &p.ptr, &u);
1637 ret = __mark_pointer(c, k, &p.ptr, sectors, data_type, u.gen, &u.data_type,
1638 &u.dirty_sectors, &u.cached_sectors);
1642 a = bch2_trans_kmalloc(trans, BKEY_ALLOC_U64s_MAX * 8);
1643 ret = PTR_ERR_OR_ZERO(a);
1647 bkey_alloc_init(&a->k_i);
1649 bch2_alloc_pack(a, u);
1650 bch2_trans_update(trans, iter, &a->k_i, 0);
1652 bch2_trans_iter_put(trans, iter);
1656 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1657 struct bch_extent_stripe_ptr p,
1658 s64 sectors, enum bch_data_type data_type)
1660 struct bch_fs *c = trans->c;
1661 struct btree_iter *iter;
1663 struct bkey_i_stripe *s;
1664 struct bch_replicas_padded r;
1667 ret = trans_get_key(trans, BTREE_ID_EC, POS(0, p.idx), &iter, &k);
1671 if (k.k->type != KEY_TYPE_stripe) {
1672 bch2_fs_inconsistent(c,
1673 "pointer to nonexistent stripe %llu",
1679 s = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1680 ret = PTR_ERR_OR_ZERO(s);
1684 bkey_reassemble(&s->k_i, k);
1685 stripe_blockcount_set(&s->v, p.block,
1686 stripe_blockcount_get(&s->v, p.block) +
1688 bch2_trans_update(trans, iter, &s->k_i, 0);
1690 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1691 r.e.data_type = data_type;
1692 update_replicas_list(trans, &r.e, sectors);
1694 bch2_trans_iter_put(trans, iter);
1698 static int bch2_trans_mark_extent(struct btree_trans *trans,
1699 struct bkey_s_c k, unsigned offset,
1700 s64 sectors, unsigned flags,
1701 enum bch_data_type data_type)
1703 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1704 const union bch_extent_entry *entry;
1705 struct extent_ptr_decoded p;
1706 struct bch_replicas_padded r;
1707 s64 dirty_sectors = 0;
1711 r.e.data_type = data_type;
1713 r.e.nr_required = 1;
1717 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1718 s64 disk_sectors = data_type == BCH_DATA_btree
1720 : ptr_disk_sectors_delta(p, offset, sectors, flags);
1722 ret = bch2_trans_mark_pointer(trans, k, p, disk_sectors,
1731 update_cached_sectors_list(trans, p.ptr.dev,
1733 } else if (!p.has_ec) {
1734 dirty_sectors += disk_sectors;
1735 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1737 ret = bch2_trans_mark_stripe_ptr(trans, p.ec,
1738 disk_sectors, data_type);
1742 r.e.nr_required = 0;
1747 update_replicas_list(trans, &r.e, dirty_sectors);
1752 static int bch2_trans_mark_stripe(struct btree_trans *trans,
1756 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
1757 unsigned nr_data = s->nr_blocks - s->nr_redundant;
1758 struct bch_replicas_padded r;
1759 struct bkey_alloc_unpacked u;
1760 struct bkey_i_alloc *a;
1761 struct btree_iter *iter;
1762 bool deleting = flags & BTREE_TRIGGER_OVERWRITE;
1763 s64 sectors = le16_to_cpu(s->sectors);
1770 bch2_bkey_to_replicas(&r.e, k);
1771 update_replicas_list(trans, &r.e, sectors * s->nr_redundant);
1774 * The allocator code doesn't necessarily update bucket gens in the
1775 * btree when incrementing them, right before handing out new buckets -
1776 * we just need to persist those updates here along with the new stripe:
1779 for (i = 0; i < s->nr_blocks && !ret; i++) {
1780 bool parity = i >= nr_data;
1782 ret = bch2_trans_start_alloc_update(trans, &iter,
1788 u.dirty_sectors += sectors;
1789 u.data_type = u.dirty_sectors
1794 a = bch2_trans_kmalloc(trans, BKEY_ALLOC_U64s_MAX * 8);
1795 ret = PTR_ERR_OR_ZERO(a);
1799 bkey_alloc_init(&a->k_i);
1801 bch2_alloc_pack(a, u);
1802 bch2_trans_update(trans, iter, &a->k_i, 0);
1804 bch2_trans_iter_put(trans, iter);
1810 static __le64 *bkey_refcount(struct bkey_i *k)
1812 switch (k->k.type) {
1813 case KEY_TYPE_reflink_v:
1814 return &bkey_i_to_reflink_v(k)->v.refcount;
1815 case KEY_TYPE_indirect_inline_data:
1816 return &bkey_i_to_indirect_inline_data(k)->v.refcount;
1822 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1823 struct bkey_s_c_reflink_p p,
1824 u64 idx, unsigned sectors,
1827 struct bch_fs *c = trans->c;
1828 struct btree_iter *iter;
1834 ret = trans_get_key(trans, BTREE_ID_REFLINK,
1835 POS(0, idx), &iter, &k);
1839 if ((flags & BTREE_TRIGGER_OVERWRITE) &&
1840 (bkey_start_offset(k.k) < idx ||
1841 k.k->p.offset > idx + sectors))
1844 sectors = k.k->p.offset - idx;
1846 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1847 ret = PTR_ERR_OR_ZERO(n);
1851 bkey_reassemble(n, k);
1853 refcount = bkey_refcount(n);
1855 bch2_fs_inconsistent(c,
1856 "%llu:%llu len %u points to nonexistent indirect extent %llu",
1857 p.k->p.inode, p.k->p.offset, p.k->size, idx);
1862 le64_add_cpu(refcount, !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1);
1865 n->k.type = KEY_TYPE_deleted;
1866 set_bkey_val_u64s(&n->k, 0);
1869 bch2_btree_iter_set_pos(iter, bkey_start_pos(k.k));
1870 BUG_ON(iter->uptodate > BTREE_ITER_NEED_PEEK);
1872 bch2_trans_update(trans, iter, n, 0);
1876 bch2_trans_iter_put(trans, iter);
1880 static int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1881 struct bkey_s_c_reflink_p p, unsigned offset,
1882 s64 sectors, unsigned flags)
1884 u64 idx = le64_to_cpu(p.v->idx) + offset;
1887 sectors = abs(sectors);
1888 BUG_ON(offset + sectors > p.k->size);
1891 ret = __bch2_trans_mark_reflink_p(trans, p, idx, sectors, flags);
1896 sectors = max_t(s64, 0LL, sectors - ret);
1903 int bch2_trans_mark_key(struct btree_trans *trans, struct bkey_s_c k,
1904 unsigned offset, s64 sectors, unsigned flags)
1906 struct replicas_delta_list *d;
1907 struct bch_fs *c = trans->c;
1909 switch (k.k->type) {
1910 case KEY_TYPE_btree_ptr:
1911 case KEY_TYPE_btree_ptr_v2:
1912 sectors = !(flags & BTREE_TRIGGER_OVERWRITE)
1913 ? c->opts.btree_node_size
1914 : -c->opts.btree_node_size;
1916 return bch2_trans_mark_extent(trans, k, offset, sectors,
1917 flags, BCH_DATA_btree);
1918 case KEY_TYPE_extent:
1919 case KEY_TYPE_reflink_v:
1920 return bch2_trans_mark_extent(trans, k, offset, sectors,
1921 flags, BCH_DATA_user);
1922 case KEY_TYPE_stripe:
1923 return bch2_trans_mark_stripe(trans, k, flags);
1924 case KEY_TYPE_inode:
1925 d = replicas_deltas_realloc(trans, 0);
1927 if (!(flags & BTREE_TRIGGER_OVERWRITE))
1932 case KEY_TYPE_reservation: {
1933 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1935 d = replicas_deltas_realloc(trans, 0);
1937 sectors *= replicas;
1938 replicas = clamp_t(unsigned, replicas, 1,
1939 ARRAY_SIZE(d->persistent_reserved));
1941 d->persistent_reserved[replicas - 1] += sectors;
1944 case KEY_TYPE_reflink_p:
1945 return bch2_trans_mark_reflink_p(trans,
1946 bkey_s_c_to_reflink_p(k),
1947 offset, sectors, flags);
1953 int bch2_trans_mark_update(struct btree_trans *trans,
1954 struct btree_iter *iter,
1955 struct bkey_i *insert,
1958 struct btree *b = iter_l(iter)->b;
1959 struct btree_node_iter node_iter = iter_l(iter)->iter;
1960 struct bkey_packed *_k;
1963 if (unlikely(flags & BTREE_TRIGGER_NORUN))
1966 if (!btree_node_type_needs_gc(iter->btree_id))
1969 ret = bch2_trans_mark_key(trans, bkey_i_to_s_c(insert),
1970 0, insert->k.size, BTREE_TRIGGER_INSERT);
1974 if (btree_iter_type(iter) == BTREE_ITER_CACHED) {
1975 struct bkey_cached *ck = (void *) iter->l[0].b;
1977 return bch2_trans_mark_key(trans, bkey_i_to_s_c(ck->k),
1978 0, 0, BTREE_TRIGGER_OVERWRITE);
1981 while ((_k = bch2_btree_node_iter_peek(&node_iter, b))) {
1982 struct bkey unpacked;
1984 unsigned offset = 0;
1986 unsigned flags = BTREE_TRIGGER_OVERWRITE;
1988 k = bkey_disassemble(b, _k, &unpacked);
1990 if (btree_node_is_extents(b)
1991 ? bkey_cmp(insert->k.p, bkey_start_pos(k.k)) <= 0
1992 : bkey_cmp(insert->k.p, k.k->p))
1995 if (btree_node_is_extents(b)) {
1996 switch (bch2_extent_overlap(&insert->k, k.k)) {
1997 case BCH_EXTENT_OVERLAP_ALL:
1999 sectors = -((s64) k.k->size);
2001 case BCH_EXTENT_OVERLAP_BACK:
2002 offset = bkey_start_offset(&insert->k) -
2003 bkey_start_offset(k.k);
2004 sectors = bkey_start_offset(&insert->k) -
2007 case BCH_EXTENT_OVERLAP_FRONT:
2009 sectors = bkey_start_offset(k.k) -
2012 case BCH_EXTENT_OVERLAP_MIDDLE:
2013 offset = bkey_start_offset(&insert->k) -
2014 bkey_start_offset(k.k);
2015 sectors = -((s64) insert->k.size);
2016 flags |= BTREE_TRIGGER_OVERWRITE_SPLIT;
2020 BUG_ON(sectors >= 0);
2023 ret = bch2_trans_mark_key(trans, k, offset, sectors, flags);
2027 bch2_btree_node_iter_advance(&node_iter, b);
2033 /* Disk reservations: */
2035 void __bch2_disk_reservation_put(struct bch_fs *c, struct disk_reservation *res)
2037 percpu_down_read(&c->mark_lock);
2038 this_cpu_sub(c->usage[0]->online_reserved,
2040 percpu_up_read(&c->mark_lock);
2045 #define SECTORS_CACHE 1024
2047 int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
2048 unsigned sectors, int flags)
2050 struct bch_fs_pcpu *pcpu;
2052 s64 sectors_available;
2055 percpu_down_read(&c->mark_lock);
2057 pcpu = this_cpu_ptr(c->pcpu);
2059 if (sectors <= pcpu->sectors_available)
2062 v = atomic64_read(&c->sectors_available);
2065 get = min((u64) sectors + SECTORS_CACHE, old);
2067 if (get < sectors) {
2071 } while ((v = atomic64_cmpxchg(&c->sectors_available,
2072 old, old - get)) != old);
2074 pcpu->sectors_available += get;
2077 pcpu->sectors_available -= sectors;
2078 this_cpu_add(c->usage[0]->online_reserved, sectors);
2079 res->sectors += sectors;
2082 percpu_up_read(&c->mark_lock);
2086 mutex_lock(&c->sectors_available_lock);
2088 percpu_u64_set(&c->pcpu->sectors_available, 0);
2089 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
2091 if (sectors <= sectors_available ||
2092 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
2093 atomic64_set(&c->sectors_available,
2094 max_t(s64, 0, sectors_available - sectors));
2095 this_cpu_add(c->usage[0]->online_reserved, sectors);
2096 res->sectors += sectors;
2099 atomic64_set(&c->sectors_available, sectors_available);
2103 mutex_unlock(&c->sectors_available_lock);
2104 percpu_up_read(&c->mark_lock);
2109 /* Startup/shutdown: */
2111 static void buckets_free_rcu(struct rcu_head *rcu)
2113 struct bucket_array *buckets =
2114 container_of(rcu, struct bucket_array, rcu);
2117 sizeof(struct bucket_array) +
2118 buckets->nbuckets * sizeof(struct bucket));
2121 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
2123 struct bucket_array *buckets = NULL, *old_buckets = NULL;
2124 unsigned long *buckets_nouse = NULL;
2125 alloc_fifo free[RESERVE_NR];
2126 alloc_fifo free_inc;
2127 alloc_heap alloc_heap;
2129 size_t btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE,
2130 ca->mi.bucket_size / c->opts.btree_node_size);
2131 /* XXX: these should be tunable */
2132 size_t reserve_none = max_t(size_t, 1, nbuckets >> 9);
2133 size_t copygc_reserve = max_t(size_t, 2, nbuckets >> 7);
2134 size_t free_inc_nr = max(max_t(size_t, 1, nbuckets >> 12),
2136 bool resize = ca->buckets[0] != NULL;
2140 memset(&free, 0, sizeof(free));
2141 memset(&free_inc, 0, sizeof(free_inc));
2142 memset(&alloc_heap, 0, sizeof(alloc_heap));
2144 if (!(buckets = kvpmalloc(sizeof(struct bucket_array) +
2145 nbuckets * sizeof(struct bucket),
2146 GFP_KERNEL|__GFP_ZERO)) ||
2147 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2148 sizeof(unsigned long),
2149 GFP_KERNEL|__GFP_ZERO)) ||
2150 !init_fifo(&free[RESERVE_BTREE], btree_reserve, GFP_KERNEL) ||
2151 !init_fifo(&free[RESERVE_MOVINGGC],
2152 copygc_reserve, GFP_KERNEL) ||
2153 !init_fifo(&free[RESERVE_NONE], reserve_none, GFP_KERNEL) ||
2154 !init_fifo(&free_inc, free_inc_nr, GFP_KERNEL) ||
2155 !init_heap(&alloc_heap, ALLOC_SCAN_BATCH(ca) << 1, GFP_KERNEL))
2158 buckets->first_bucket = ca->mi.first_bucket;
2159 buckets->nbuckets = nbuckets;
2161 bch2_copygc_stop(c);
2164 down_write(&c->gc_lock);
2165 down_write(&ca->bucket_lock);
2166 percpu_down_write(&c->mark_lock);
2169 old_buckets = bucket_array(ca);
2172 size_t n = min(buckets->nbuckets, old_buckets->nbuckets);
2176 n * sizeof(struct bucket));
2177 memcpy(buckets_nouse,
2179 BITS_TO_LONGS(n) * sizeof(unsigned long));
2182 rcu_assign_pointer(ca->buckets[0], buckets);
2183 buckets = old_buckets;
2185 swap(ca->buckets_nouse, buckets_nouse);
2188 percpu_up_write(&c->mark_lock);
2189 up_write(&c->gc_lock);
2192 spin_lock(&c->freelist_lock);
2193 for (i = 0; i < RESERVE_NR; i++) {
2194 fifo_move(&free[i], &ca->free[i]);
2195 swap(ca->free[i], free[i]);
2197 fifo_move(&free_inc, &ca->free_inc);
2198 swap(ca->free_inc, free_inc);
2199 spin_unlock(&c->freelist_lock);
2201 /* with gc lock held, alloc_heap can't be in use: */
2202 swap(ca->alloc_heap, alloc_heap);
2204 nbuckets = ca->mi.nbuckets;
2207 up_write(&ca->bucket_lock);
2211 free_heap(&alloc_heap);
2212 free_fifo(&free_inc);
2213 for (i = 0; i < RESERVE_NR; i++)
2214 free_fifo(&free[i]);
2215 kvpfree(buckets_nouse,
2216 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2218 call_rcu(&old_buckets->rcu, buckets_free_rcu);
2223 void bch2_dev_buckets_free(struct bch_dev *ca)
2227 free_heap(&ca->alloc_heap);
2228 free_fifo(&ca->free_inc);
2229 for (i = 0; i < RESERVE_NR; i++)
2230 free_fifo(&ca->free[i]);
2231 kvpfree(ca->buckets_nouse,
2232 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2233 kvpfree(rcu_dereference_protected(ca->buckets[0], 1),
2234 sizeof(struct bucket_array) +
2235 ca->mi.nbuckets * sizeof(struct bucket));
2237 free_percpu(ca->usage[0]);
2240 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2242 if (!(ca->usage[0] = alloc_percpu(struct bch_dev_usage)))
2245 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);;