1 // SPDX-License-Identifier: GPL-2.0
3 * Code for manipulating bucket marks for garbage collection.
5 * Copyright 2014 Datera, Inc.
9 #include "alloc_background.h"
10 #include "backpointers.h"
13 #include "btree_update.h"
15 #include "buckets_waiting_for_journal.h"
23 #include "subvolume.h"
26 #include <linux/preempt.h>
28 static inline void fs_usage_data_type_to_base(struct bch_fs_usage *fs_usage,
29 enum bch_data_type data_type,
34 fs_usage->btree += sectors;
38 fs_usage->data += sectors;
41 fs_usage->cached += sectors;
48 void bch2_fs_usage_initialize(struct bch_fs *c)
50 struct bch_fs_usage *usage;
54 percpu_down_write(&c->mark_lock);
55 usage = c->usage_base;
57 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
58 bch2_fs_usage_acc_to_base(c, i);
60 for (i = 0; i < BCH_REPLICAS_MAX; i++)
61 usage->reserved += usage->persistent_reserved[i];
63 for (i = 0; i < c->replicas.nr; i++) {
64 struct bch_replicas_entry *e =
65 cpu_replicas_entry(&c->replicas, i);
67 fs_usage_data_type_to_base(usage, e->data_type, usage->replicas[i]);
70 for_each_member_device(ca, c, i) {
71 struct bch_dev_usage dev = bch2_dev_usage_read(ca);
73 usage->hidden += (dev.d[BCH_DATA_sb].buckets +
74 dev.d[BCH_DATA_journal].buckets) *
78 percpu_up_write(&c->mark_lock);
81 static inline struct bch_dev_usage *dev_usage_ptr(struct bch_dev *ca,
85 BUG_ON(!gc && !journal_seq);
87 return this_cpu_ptr(gc
89 : ca->usage[journal_seq & JOURNAL_BUF_MASK]);
92 void bch2_dev_usage_read_fast(struct bch_dev *ca, struct bch_dev_usage *usage)
94 struct bch_fs *c = ca->fs;
95 unsigned seq, i, u64s = dev_usage_u64s();
98 seq = read_seqcount_begin(&c->usage_lock);
99 memcpy(usage, ca->usage_base, u64s * sizeof(u64));
100 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
101 acc_u64s_percpu((u64 *) usage, (u64 __percpu *) ca->usage[i], u64s);
102 } while (read_seqcount_retry(&c->usage_lock, seq));
105 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
107 ssize_t offset = v - (u64 *) c->usage_base;
111 BUG_ON(offset < 0 || offset >= fs_usage_u64s(c));
112 percpu_rwsem_assert_held(&c->mark_lock);
115 seq = read_seqcount_begin(&c->usage_lock);
118 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
119 ret += percpu_u64_get((u64 __percpu *) c->usage[i] + offset);
120 } while (read_seqcount_retry(&c->usage_lock, seq));
125 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *c)
127 struct bch_fs_usage_online *ret;
128 unsigned nr_replicas = READ_ONCE(c->replicas.nr);
131 ret = kmalloc(__fs_usage_online_u64s(nr_replicas) * sizeof(u64), GFP_KERNEL);
135 percpu_down_read(&c->mark_lock);
137 if (nr_replicas != c->replicas.nr) {
138 nr_replicas = c->replicas.nr;
139 percpu_up_read(&c->mark_lock);
144 ret->online_reserved = percpu_u64_get(c->online_reserved);
147 seq = read_seqcount_begin(&c->usage_lock);
148 unsafe_memcpy(&ret->u, c->usage_base,
149 __fs_usage_u64s(nr_replicas) * sizeof(u64),
150 "embedded variable length struct");
151 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
152 acc_u64s_percpu((u64 *) &ret->u, (u64 __percpu *) c->usage[i],
153 __fs_usage_u64s(nr_replicas));
154 } while (read_seqcount_retry(&c->usage_lock, seq));
159 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx)
162 unsigned i, u64s = fs_usage_u64s(c);
164 BUG_ON(idx >= ARRAY_SIZE(c->usage));
167 write_seqcount_begin(&c->usage_lock);
169 acc_u64s_percpu((u64 *) c->usage_base,
170 (u64 __percpu *) c->usage[idx], u64s);
171 percpu_memset(c->usage[idx], 0, u64s * sizeof(u64));
174 for_each_member_device_rcu(ca, c, i, NULL) {
175 u64s = dev_usage_u64s();
177 acc_u64s_percpu((u64 *) ca->usage_base,
178 (u64 __percpu *) ca->usage[idx], u64s);
179 percpu_memset(ca->usage[idx], 0, u64s * sizeof(u64));
183 write_seqcount_end(&c->usage_lock);
187 void bch2_fs_usage_to_text(struct printbuf *out,
189 struct bch_fs_usage_online *fs_usage)
193 prt_printf(out, "capacity:\t\t\t%llu\n", c->capacity);
195 prt_printf(out, "hidden:\t\t\t\t%llu\n",
197 prt_printf(out, "data:\t\t\t\t%llu\n",
199 prt_printf(out, "cached:\t\t\t\t%llu\n",
201 prt_printf(out, "reserved:\t\t\t%llu\n",
202 fs_usage->u.reserved);
203 prt_printf(out, "nr_inodes:\t\t\t%llu\n",
204 fs_usage->u.nr_inodes);
205 prt_printf(out, "online reserved:\t\t%llu\n",
206 fs_usage->online_reserved);
209 i < ARRAY_SIZE(fs_usage->u.persistent_reserved);
211 prt_printf(out, "%u replicas:\n", i + 1);
212 prt_printf(out, "\treserved:\t\t%llu\n",
213 fs_usage->u.persistent_reserved[i]);
216 for (i = 0; i < c->replicas.nr; i++) {
217 struct bch_replicas_entry *e =
218 cpu_replicas_entry(&c->replicas, i);
220 prt_printf(out, "\t");
221 bch2_replicas_entry_to_text(out, e);
222 prt_printf(out, ":\t%llu\n", fs_usage->u.replicas[i]);
226 static u64 reserve_factor(u64 r)
228 return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR);
231 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage_online *fs_usage)
233 return min(fs_usage->u.hidden +
236 reserve_factor(fs_usage->u.reserved +
237 fs_usage->online_reserved),
241 static struct bch_fs_usage_short
242 __bch2_fs_usage_read_short(struct bch_fs *c)
244 struct bch_fs_usage_short ret;
247 ret.capacity = c->capacity -
248 bch2_fs_usage_read_one(c, &c->usage_base->hidden);
250 data = bch2_fs_usage_read_one(c, &c->usage_base->data) +
251 bch2_fs_usage_read_one(c, &c->usage_base->btree);
252 reserved = bch2_fs_usage_read_one(c, &c->usage_base->reserved) +
253 percpu_u64_get(c->online_reserved);
255 ret.used = min(ret.capacity, data + reserve_factor(reserved));
256 ret.free = ret.capacity - ret.used;
258 ret.nr_inodes = bch2_fs_usage_read_one(c, &c->usage_base->nr_inodes);
263 struct bch_fs_usage_short
264 bch2_fs_usage_read_short(struct bch_fs *c)
266 struct bch_fs_usage_short ret;
268 percpu_down_read(&c->mark_lock);
269 ret = __bch2_fs_usage_read_short(c);
270 percpu_up_read(&c->mark_lock);
275 void bch2_dev_usage_init(struct bch_dev *ca)
277 ca->usage_base->d[BCH_DATA_free].buckets = ca->mi.nbuckets - ca->mi.first_bucket;
280 static inline int bucket_sectors_fragmented(struct bch_dev *ca,
281 struct bch_alloc_v4 a)
283 return a.dirty_sectors
284 ? max(0, (int) ca->mi.bucket_size - (int) a.dirty_sectors)
288 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
289 struct bch_alloc_v4 old,
290 struct bch_alloc_v4 new,
291 u64 journal_seq, bool gc)
293 struct bch_fs_usage *fs_usage;
294 struct bch_dev_usage *u;
297 fs_usage = fs_usage_ptr(c, journal_seq, gc);
299 if (data_type_is_hidden(old.data_type))
300 fs_usage->hidden -= ca->mi.bucket_size;
301 if (data_type_is_hidden(new.data_type))
302 fs_usage->hidden += ca->mi.bucket_size;
304 u = dev_usage_ptr(ca, journal_seq, gc);
306 u->d[old.data_type].buckets--;
307 u->d[new.data_type].buckets++;
309 u->buckets_ec -= (int) !!old.stripe;
310 u->buckets_ec += (int) !!new.stripe;
312 u->d[old.data_type].sectors -= old.dirty_sectors;
313 u->d[new.data_type].sectors += new.dirty_sectors;
315 u->d[BCH_DATA_cached].sectors += new.cached_sectors;
316 u->d[BCH_DATA_cached].sectors -= old.cached_sectors;
318 u->d[old.data_type].fragmented -= bucket_sectors_fragmented(ca, old);
319 u->d[new.data_type].fragmented += bucket_sectors_fragmented(ca, new);
324 static void bch2_dev_usage_update_m(struct bch_fs *c, struct bch_dev *ca,
325 struct bucket old, struct bucket new,
326 u64 journal_seq, bool gc)
328 struct bch_alloc_v4 old_a = {
330 .data_type = old.data_type,
331 .dirty_sectors = old.dirty_sectors,
332 .cached_sectors = old.cached_sectors,
333 .stripe = old.stripe,
335 struct bch_alloc_v4 new_a = {
337 .data_type = new.data_type,
338 .dirty_sectors = new.dirty_sectors,
339 .cached_sectors = new.cached_sectors,
340 .stripe = new.stripe,
343 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
346 static inline int __update_replicas(struct bch_fs *c,
347 struct bch_fs_usage *fs_usage,
348 struct bch_replicas_entry *r,
351 int idx = bch2_replicas_entry_idx(c, r);
356 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
357 fs_usage->replicas[idx] += sectors;
361 static inline int update_replicas(struct bch_fs *c, struct bkey_s_c k,
362 struct bch_replicas_entry *r, s64 sectors,
363 unsigned journal_seq, bool gc)
365 struct bch_fs_usage *fs_usage;
367 struct printbuf buf = PRINTBUF;
369 percpu_down_read(&c->mark_lock);
371 idx = bch2_replicas_entry_idx(c, r);
373 fsck_err(c, "no replicas entry\n"
375 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
376 percpu_up_read(&c->mark_lock);
377 ret = bch2_mark_replicas(c, r);
378 percpu_down_read(&c->mark_lock);
382 idx = bch2_replicas_entry_idx(c, r);
390 fs_usage = fs_usage_ptr(c, journal_seq, gc);
391 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
392 fs_usage->replicas[idx] += sectors;
396 percpu_up_read(&c->mark_lock);
401 static inline int update_cached_sectors(struct bch_fs *c,
403 unsigned dev, s64 sectors,
404 unsigned journal_seq, bool gc)
406 struct bch_replicas_padded r;
408 bch2_replicas_entry_cached(&r.e, dev);
410 return update_replicas(c, k, &r.e, sectors, journal_seq, gc);
413 static int __replicas_deltas_realloc(struct btree_trans *trans, unsigned more,
416 struct replicas_delta_list *d = trans->fs_usage_deltas;
417 unsigned new_size = d ? (d->size + more) * 2 : 128;
418 unsigned alloc_size = sizeof(*d) + new_size;
420 WARN_ON_ONCE(alloc_size > REPLICAS_DELTA_LIST_MAX);
422 if (!d || d->used + more > d->size) {
423 d = krealloc(d, alloc_size, gfp|__GFP_ZERO);
426 if (alloc_size > REPLICAS_DELTA_LIST_MAX)
429 d = mempool_alloc(&trans->c->replicas_delta_pool, gfp);
433 memset(d, 0, REPLICAS_DELTA_LIST_MAX);
435 if (trans->fs_usage_deltas)
436 memcpy(d, trans->fs_usage_deltas,
437 trans->fs_usage_deltas->size + sizeof(*d));
439 new_size = REPLICAS_DELTA_LIST_MAX - sizeof(*d);
440 kfree(trans->fs_usage_deltas);
444 trans->fs_usage_deltas = d;
450 int bch2_replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
452 return allocate_dropping_locks_errcode(trans,
453 __replicas_deltas_realloc(trans, more, _gfp));
456 static inline int update_replicas_list(struct btree_trans *trans,
457 struct bch_replicas_entry *r,
460 struct replicas_delta_list *d;
461 struct replicas_delta *n;
468 b = replicas_entry_bytes(r) + 8;
469 ret = bch2_replicas_deltas_realloc(trans, b);
473 d = trans->fs_usage_deltas;
474 n = (void *) d->d + d->used;
476 memcpy((void *) n + offsetof(struct replicas_delta, r),
477 r, replicas_entry_bytes(r));
478 bch2_replicas_entry_sort(&n->r);
483 static inline int update_cached_sectors_list(struct btree_trans *trans,
484 unsigned dev, s64 sectors)
486 struct bch_replicas_padded r;
488 bch2_replicas_entry_cached(&r.e, dev);
490 return update_replicas_list(trans, &r.e, sectors);
493 int bch2_mark_alloc(struct btree_trans *trans,
494 enum btree_id btree, unsigned level,
495 struct bkey_s_c old, struct bkey_s_c new,
498 bool gc = flags & BTREE_TRIGGER_GC;
499 u64 journal_seq = trans->journal_res.seq;
500 u64 bucket_journal_seq;
501 struct bch_fs *c = trans->c;
502 struct bch_alloc_v4 old_a_convert, new_a_convert;
503 const struct bch_alloc_v4 *old_a, *new_a;
508 * alloc btree is read in by bch2_alloc_read, not gc:
510 if ((flags & BTREE_TRIGGER_GC) &&
511 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
514 if (bch2_trans_inconsistent_on(!bch2_dev_bucket_exists(c, new.k->p), trans,
515 "alloc key for invalid device or bucket"))
518 ca = bch_dev_bkey_exists(c, new.k->p.inode);
520 old_a = bch2_alloc_to_v4(old, &old_a_convert);
521 new_a = bch2_alloc_to_v4(new, &new_a_convert);
523 bucket_journal_seq = new_a->journal_seq;
525 if ((flags & BTREE_TRIGGER_INSERT) &&
526 data_type_is_empty(old_a->data_type) !=
527 data_type_is_empty(new_a->data_type) &&
528 new.k->type == KEY_TYPE_alloc_v4) {
529 struct bch_alloc_v4 *v = (struct bch_alloc_v4 *) new.v;
531 EBUG_ON(!journal_seq);
534 * If the btree updates referring to a bucket weren't flushed
535 * before the bucket became empty again, then the we don't have
536 * to wait on a journal flush before we can reuse the bucket:
538 v->journal_seq = bucket_journal_seq =
539 data_type_is_empty(new_a->data_type) &&
540 (journal_seq == v->journal_seq ||
541 bch2_journal_noflush_seq(&c->journal, v->journal_seq))
545 if (!data_type_is_empty(old_a->data_type) &&
546 data_type_is_empty(new_a->data_type) &&
547 bucket_journal_seq) {
548 ret = bch2_set_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
549 c->journal.flushed_seq_ondisk,
550 new.k->p.inode, new.k->p.offset,
553 bch2_fs_fatal_error(c,
554 "error setting bucket_needs_journal_commit: %i", ret);
559 percpu_down_read(&c->mark_lock);
560 if (!gc && new_a->gen != old_a->gen)
561 *bucket_gen(ca, new.k->p.offset) = new_a->gen;
563 bch2_dev_usage_update(c, ca, *old_a, *new_a, journal_seq, gc);
566 struct bucket *g = gc_bucket(ca, new.k->p.offset);
572 g->data_type = new_a->data_type;
573 g->stripe = new_a->stripe;
574 g->stripe_redundancy = new_a->stripe_redundancy;
575 g->dirty_sectors = new_a->dirty_sectors;
576 g->cached_sectors = new_a->cached_sectors;
580 percpu_up_read(&c->mark_lock);
583 * need to know if we're getting called from the invalidate path or
587 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
588 old_a->cached_sectors) {
589 ret = update_cached_sectors(c, new, ca->dev_idx,
590 -((s64) old_a->cached_sectors),
593 bch2_fs_fatal_error(c, "%s(): no replicas entry while updating cached sectors",
599 if (new_a->data_type == BCH_DATA_free &&
600 (!new_a->journal_seq || new_a->journal_seq < c->journal.flushed_seq_ondisk))
601 closure_wake_up(&c->freelist_wait);
603 if (new_a->data_type == BCH_DATA_need_discard &&
604 (!bucket_journal_seq || bucket_journal_seq < c->journal.flushed_seq_ondisk))
607 if (old_a->data_type != BCH_DATA_cached &&
608 new_a->data_type == BCH_DATA_cached &&
609 should_invalidate_buckets(ca, bch2_dev_usage_read(ca)))
610 bch2_do_invalidates(c);
612 if (new_a->data_type == BCH_DATA_need_gc_gens)
618 int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
619 size_t b, enum bch_data_type data_type,
620 unsigned sectors, struct gc_pos pos,
623 struct bucket old, new, *g;
626 BUG_ON(!(flags & BTREE_TRIGGER_GC));
627 BUG_ON(data_type != BCH_DATA_sb &&
628 data_type != BCH_DATA_journal);
631 * Backup superblock might be past the end of our normal usable space:
633 if (b >= ca->mi.nbuckets)
636 percpu_down_read(&c->mark_lock);
637 g = gc_bucket(ca, b);
642 if (bch2_fs_inconsistent_on(g->data_type &&
643 g->data_type != data_type, c,
644 "different types of data in same bucket: %s, %s",
645 bch2_data_types[g->data_type],
646 bch2_data_types[data_type])) {
651 if (bch2_fs_inconsistent_on((u64) g->dirty_sectors + sectors > ca->mi.bucket_size, c,
652 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > bucket size",
653 ca->dev_idx, b, g->gen,
654 bch2_data_types[g->data_type ?: data_type],
655 g->dirty_sectors, sectors)) {
661 g->data_type = data_type;
662 g->dirty_sectors += sectors;
667 bch2_dev_usage_update_m(c, ca, old, new, 0, true);
668 percpu_up_read(&c->mark_lock);
672 static int check_bucket_ref(struct btree_trans *trans,
674 const struct bch_extent_ptr *ptr,
675 s64 sectors, enum bch_data_type ptr_data_type,
676 u8 b_gen, u8 bucket_data_type,
677 u32 dirty_sectors, u32 cached_sectors)
679 struct bch_fs *c = trans->c;
680 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
681 size_t bucket_nr = PTR_BUCKET_NR(ca, ptr);
682 u32 bucket_sectors = !ptr->cached
685 struct printbuf buf = PRINTBUF;
688 if (bucket_data_type == BCH_DATA_cached)
689 bucket_data_type = BCH_DATA_user;
691 if ((bucket_data_type == BCH_DATA_stripe && ptr_data_type == BCH_DATA_user) ||
692 (bucket_data_type == BCH_DATA_user && ptr_data_type == BCH_DATA_stripe))
693 bucket_data_type = ptr_data_type = BCH_DATA_stripe;
695 if (gen_after(ptr->gen, b_gen)) {
696 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
697 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
699 ptr->dev, bucket_nr, b_gen,
700 bch2_data_types[bucket_data_type ?: ptr_data_type],
702 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
707 if (gen_cmp(b_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
708 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
709 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
711 ptr->dev, bucket_nr, b_gen,
712 bch2_data_types[bucket_data_type ?: ptr_data_type],
714 (printbuf_reset(&buf),
715 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
720 if (b_gen != ptr->gen && !ptr->cached) {
721 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
722 "bucket %u:%zu gen %u (mem gen %u) data type %s: stale dirty ptr (gen %u)\n"
724 ptr->dev, bucket_nr, b_gen,
725 *bucket_gen(ca, bucket_nr),
726 bch2_data_types[bucket_data_type ?: ptr_data_type],
728 (printbuf_reset(&buf),
729 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
734 if (b_gen != ptr->gen) {
739 if (!data_type_is_empty(bucket_data_type) &&
741 bucket_data_type != ptr_data_type) {
742 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
743 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
745 ptr->dev, bucket_nr, b_gen,
746 bch2_data_types[bucket_data_type],
747 bch2_data_types[ptr_data_type],
748 (printbuf_reset(&buf),
749 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
754 if ((u64) bucket_sectors + sectors > U32_MAX) {
755 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
756 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U32_MAX\n"
758 ptr->dev, bucket_nr, b_gen,
759 bch2_data_types[bucket_data_type ?: ptr_data_type],
760 bucket_sectors, sectors,
761 (printbuf_reset(&buf),
762 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
770 bch2_dump_trans_updates(trans);
774 static int mark_stripe_bucket(struct btree_trans *trans,
779 struct bch_fs *c = trans->c;
780 u64 journal_seq = trans->journal_res.seq;
781 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
782 unsigned nr_data = s->nr_blocks - s->nr_redundant;
783 bool parity = ptr_idx >= nr_data;
784 enum bch_data_type data_type = parity ? BCH_DATA_parity : BCH_DATA_stripe;
785 s64 sectors = parity ? le16_to_cpu(s->sectors) : 0;
786 const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
787 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
788 struct bucket old, new, *g;
789 struct printbuf buf = PRINTBUF;
792 BUG_ON(!(flags & BTREE_TRIGGER_GC));
794 /* * XXX doesn't handle deletion */
796 percpu_down_read(&c->mark_lock);
797 g = PTR_GC_BUCKET(ca, ptr);
799 if (g->dirty_sectors ||
800 (g->stripe && g->stripe != k.k->p.offset)) {
801 bch2_fs_inconsistent(c,
802 "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
803 ptr->dev, PTR_BUCKET_NR(ca, ptr), g->gen,
804 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
812 ret = check_bucket_ref(trans, k, ptr, sectors, data_type,
813 g->gen, g->data_type,
814 g->dirty_sectors, g->cached_sectors);
818 g->data_type = data_type;
819 g->dirty_sectors += sectors;
821 g->stripe = k.k->p.offset;
822 g->stripe_redundancy = s->nr_redundant;
827 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
828 percpu_up_read(&c->mark_lock);
833 static int __mark_pointer(struct btree_trans *trans,
835 const struct bch_extent_ptr *ptr,
836 s64 sectors, enum bch_data_type ptr_data_type,
837 u8 bucket_gen, u8 *bucket_data_type,
838 u32 *dirty_sectors, u32 *cached_sectors)
840 u32 *dst_sectors = !ptr->cached
843 int ret = check_bucket_ref(trans, k, ptr, sectors, ptr_data_type,
844 bucket_gen, *bucket_data_type,
845 *dirty_sectors, *cached_sectors);
850 *dst_sectors += sectors;
851 *bucket_data_type = *dirty_sectors || *cached_sectors
856 static int bch2_mark_pointer(struct btree_trans *trans,
857 enum btree_id btree_id, unsigned level,
859 struct extent_ptr_decoded p,
863 u64 journal_seq = trans->journal_res.seq;
864 struct bch_fs *c = trans->c;
865 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
866 struct bucket old, new, *g;
867 enum bch_data_type data_type = bkey_ptr_data_type(btree_id, level, k, p);
871 BUG_ON(!(flags & BTREE_TRIGGER_GC));
873 percpu_down_read(&c->mark_lock);
874 g = PTR_GC_BUCKET(ca, &p.ptr);
878 bucket_data_type = g->data_type;
879 ret = __mark_pointer(trans, k, &p.ptr, sectors,
885 g->data_type = bucket_data_type;
890 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
891 percpu_up_read(&c->mark_lock);
896 static int bch2_mark_stripe_ptr(struct btree_trans *trans,
898 struct bch_extent_stripe_ptr p,
899 enum bch_data_type data_type,
903 struct bch_fs *c = trans->c;
904 struct bch_replicas_padded r;
907 BUG_ON(!(flags & BTREE_TRIGGER_GC));
909 m = genradix_ptr_alloc(&c->gc_stripes, p.idx, GFP_KERNEL);
911 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
913 return -BCH_ERR_ENOMEM_mark_stripe_ptr;
916 mutex_lock(&c->ec_stripes_heap_lock);
918 if (!m || !m->alive) {
919 mutex_unlock(&c->ec_stripes_heap_lock);
920 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
922 bch2_inconsistent_error(c);
926 m->block_sectors[p.block] += sectors;
929 mutex_unlock(&c->ec_stripes_heap_lock);
931 r.e.data_type = data_type;
932 update_replicas(c, k, &r.e, sectors, trans->journal_res.seq, true);
937 int bch2_mark_extent(struct btree_trans *trans,
938 enum btree_id btree_id, unsigned level,
939 struct bkey_s_c old, struct bkey_s_c new,
942 u64 journal_seq = trans->journal_res.seq;
943 struct bch_fs *c = trans->c;
944 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
945 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
946 const union bch_extent_entry *entry;
947 struct extent_ptr_decoded p;
948 struct bch_replicas_padded r;
949 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
952 s64 sectors = bkey_is_btree_ptr(k.k)
955 s64 dirty_sectors = 0;
959 BUG_ON(!(flags & BTREE_TRIGGER_GC));
961 r.e.data_type = data_type;
965 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
966 s64 disk_sectors = ptr_disk_sectors(sectors, p);
968 if (flags & BTREE_TRIGGER_OVERWRITE)
969 disk_sectors = -disk_sectors;
971 ret = bch2_mark_pointer(trans, btree_id, level, k, p, disk_sectors, flags);
979 ret = update_cached_sectors(c, k, p.ptr.dev,
980 disk_sectors, journal_seq, true);
982 bch2_fs_fatal_error(c, "%s(): no replicas entry while updating cached sectors",
987 } else if (!p.has_ec) {
988 dirty_sectors += disk_sectors;
989 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
991 ret = bch2_mark_stripe_ptr(trans, k, p.ec, data_type,
992 disk_sectors, flags);
997 * There may be other dirty pointers in this extent, but
998 * if so they're not required for mounting if we have an
999 * erasure coded pointer in this extent:
1001 r.e.nr_required = 0;
1006 ret = update_replicas(c, k, &r.e, dirty_sectors, journal_seq, true);
1008 struct printbuf buf = PRINTBUF;
1010 bch2_bkey_val_to_text(&buf, c, k);
1011 bch2_fs_fatal_error(c, "%s(): no replicas entry for %s", __func__, buf.buf);
1012 printbuf_exit(&buf);
1020 int bch2_mark_stripe(struct btree_trans *trans,
1021 enum btree_id btree_id, unsigned level,
1022 struct bkey_s_c old, struct bkey_s_c new,
1025 bool gc = flags & BTREE_TRIGGER_GC;
1026 u64 journal_seq = trans->journal_res.seq;
1027 struct bch_fs *c = trans->c;
1028 u64 idx = new.k->p.offset;
1029 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
1030 ? bkey_s_c_to_stripe(old).v : NULL;
1031 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
1032 ? bkey_s_c_to_stripe(new).v : NULL;
1036 BUG_ON(gc && old_s);
1039 struct stripe *m = genradix_ptr(&c->stripes, idx);
1042 struct printbuf buf1 = PRINTBUF;
1043 struct printbuf buf2 = PRINTBUF;
1045 bch2_bkey_val_to_text(&buf1, c, old);
1046 bch2_bkey_val_to_text(&buf2, c, new);
1047 bch_err_ratelimited(c, "error marking nonexistent stripe %llu while marking\n"
1049 "new %s", idx, buf1.buf, buf2.buf);
1050 printbuf_exit(&buf2);
1051 printbuf_exit(&buf1);
1052 bch2_inconsistent_error(c);
1057 bch2_stripes_heap_del(c, m, idx);
1059 memset(m, 0, sizeof(*m));
1061 m->sectors = le16_to_cpu(new_s->sectors);
1062 m->algorithm = new_s->algorithm;
1063 m->nr_blocks = new_s->nr_blocks;
1064 m->nr_redundant = new_s->nr_redundant;
1065 m->blocks_nonempty = 0;
1067 for (i = 0; i < new_s->nr_blocks; i++)
1068 m->blocks_nonempty += !!stripe_blockcount_get(new_s, i);
1071 bch2_stripes_heap_insert(c, m, idx);
1073 bch2_stripes_heap_update(c, m, idx);
1076 struct gc_stripe *m =
1077 genradix_ptr_alloc(&c->gc_stripes, idx, GFP_KERNEL);
1080 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
1082 return -BCH_ERR_ENOMEM_mark_stripe;
1085 * This will be wrong when we bring back runtime gc: we should
1086 * be unmarking the old key and then marking the new key
1089 m->sectors = le16_to_cpu(new_s->sectors);
1090 m->nr_blocks = new_s->nr_blocks;
1091 m->nr_redundant = new_s->nr_redundant;
1093 for (i = 0; i < new_s->nr_blocks; i++)
1094 m->ptrs[i] = new_s->ptrs[i];
1096 bch2_bkey_to_replicas(&m->r.e, new);
1099 * gc recalculates this field from stripe ptr
1102 memset(m->block_sectors, 0, sizeof(m->block_sectors));
1104 for (i = 0; i < new_s->nr_blocks; i++) {
1105 ret = mark_stripe_bucket(trans, new, i, flags);
1110 ret = update_replicas(c, new, &m->r.e,
1111 ((s64) m->sectors * m->nr_redundant),
1114 struct printbuf buf = PRINTBUF;
1116 bch2_bkey_val_to_text(&buf, c, new);
1117 bch2_fs_fatal_error(c, "no replicas entry for %s", buf.buf);
1118 printbuf_exit(&buf);
1126 int bch2_mark_reservation(struct btree_trans *trans,
1127 enum btree_id btree_id, unsigned level,
1128 struct bkey_s_c old, struct bkey_s_c new,
1131 struct bch_fs *c = trans->c;
1132 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
1133 struct bch_fs_usage *fs_usage;
1134 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1135 s64 sectors = (s64) k.k->size;
1137 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1139 if (flags & BTREE_TRIGGER_OVERWRITE)
1141 sectors *= replicas;
1143 percpu_down_read(&c->mark_lock);
1146 fs_usage = fs_usage_ptr(c, trans->journal_res.seq, flags & BTREE_TRIGGER_GC);
1147 replicas = clamp_t(unsigned, replicas, 1,
1148 ARRAY_SIZE(fs_usage->persistent_reserved));
1150 fs_usage->reserved += sectors;
1151 fs_usage->persistent_reserved[replicas - 1] += sectors;
1154 percpu_up_read(&c->mark_lock);
1159 static s64 __bch2_mark_reflink_p(struct btree_trans *trans,
1160 struct bkey_s_c_reflink_p p,
1162 u64 *idx, unsigned flags, size_t r_idx)
1164 struct bch_fs *c = trans->c;
1165 struct reflink_gc *r;
1166 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1169 struct printbuf buf = PRINTBUF;
1171 if (r_idx >= c->reflink_gc_nr)
1174 r = genradix_ptr(&c->reflink_gc_table, r_idx);
1175 next_idx = min(next_idx, r->offset - r->size);
1176 if (*idx < next_idx)
1179 BUG_ON((s64) r->refcount + add < 0);
1185 if (fsck_err(c, "pointer to missing indirect extent\n"
1187 " missing range %llu-%llu",
1188 (bch2_bkey_val_to_text(&buf, c, p.s_c), buf.buf),
1190 struct bkey_i_error *new;
1192 new = bch2_trans_kmalloc(trans, sizeof(*new));
1193 ret = PTR_ERR_OR_ZERO(new);
1198 new->k.type = KEY_TYPE_error;
1199 new->k.p = bkey_start_pos(p.k);
1200 new->k.p.offset += *idx - start;
1201 bch2_key_resize(&new->k, next_idx - *idx);
1202 ret = bch2_btree_insert_trans(trans, BTREE_ID_extents, &new->k_i,
1203 BTREE_TRIGGER_NORUN);
1209 printbuf_exit(&buf);
1213 int bch2_mark_reflink_p(struct btree_trans *trans,
1214 enum btree_id btree_id, unsigned level,
1215 struct bkey_s_c old, struct bkey_s_c new,
1218 struct bch_fs *c = trans->c;
1219 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
1220 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1221 struct reflink_gc *ref;
1223 u64 idx = le64_to_cpu(p.v->idx), start = idx;
1224 u64 end = le64_to_cpu(p.v->idx) + p.k->size;
1227 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1229 if (c->sb.version_upgrade_complete >= bcachefs_metadata_version_reflink_p_fix) {
1230 idx -= le32_to_cpu(p.v->front_pad);
1231 end += le32_to_cpu(p.v->back_pad);
1235 r = c->reflink_gc_nr;
1237 m = l + (r - l) / 2;
1239 ref = genradix_ptr(&c->reflink_gc_table, m);
1240 if (ref->offset <= idx)
1246 while (idx < end && !ret)
1247 ret = __bch2_mark_reflink_p(trans, p, start, end,
1253 void bch2_trans_fs_usage_revert(struct btree_trans *trans,
1254 struct replicas_delta_list *deltas)
1256 struct bch_fs *c = trans->c;
1257 struct bch_fs_usage *dst;
1258 struct replicas_delta *d, *top = (void *) deltas->d + deltas->used;
1262 percpu_down_read(&c->mark_lock);
1264 dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1266 /* revert changes: */
1267 for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1268 switch (d->r.data_type) {
1269 case BCH_DATA_btree:
1271 case BCH_DATA_parity:
1274 BUG_ON(__update_replicas(c, dst, &d->r, -d->delta));
1277 dst->nr_inodes -= deltas->nr_inodes;
1279 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1280 added -= deltas->persistent_reserved[i];
1281 dst->reserved -= deltas->persistent_reserved[i];
1282 dst->persistent_reserved[i] -= deltas->persistent_reserved[i];
1286 trans->disk_res->sectors += added;
1287 this_cpu_add(*c->online_reserved, added);
1291 percpu_up_read(&c->mark_lock);
1294 int bch2_trans_fs_usage_apply(struct btree_trans *trans,
1295 struct replicas_delta_list *deltas)
1297 struct bch_fs *c = trans->c;
1298 static int warned_disk_usage = 0;
1300 unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1301 struct replicas_delta *d, *d2;
1302 struct replicas_delta *top = (void *) deltas->d + deltas->used;
1303 struct bch_fs_usage *dst;
1304 s64 added = 0, should_not_have_added;
1307 percpu_down_read(&c->mark_lock);
1309 dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1311 for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1312 switch (d->r.data_type) {
1313 case BCH_DATA_btree:
1315 case BCH_DATA_parity:
1319 if (__update_replicas(c, dst, &d->r, d->delta))
1323 dst->nr_inodes += deltas->nr_inodes;
1325 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1326 added += deltas->persistent_reserved[i];
1327 dst->reserved += deltas->persistent_reserved[i];
1328 dst->persistent_reserved[i] += deltas->persistent_reserved[i];
1332 * Not allowed to reduce sectors_available except by getting a
1335 should_not_have_added = added - (s64) disk_res_sectors;
1336 if (unlikely(should_not_have_added > 0)) {
1337 u64 old, new, v = atomic64_read(&c->sectors_available);
1341 new = max_t(s64, 0, old - should_not_have_added);
1342 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1345 added -= should_not_have_added;
1350 trans->disk_res->sectors -= added;
1351 this_cpu_sub(*c->online_reserved, added);
1355 percpu_up_read(&c->mark_lock);
1357 if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
1358 bch2_trans_inconsistent(trans,
1359 "disk usage increased %lli more than %u sectors reserved)",
1360 should_not_have_added, disk_res_sectors);
1363 /* revert changes: */
1364 for (d2 = deltas->d; d2 != d; d2 = replicas_delta_next(d2))
1365 BUG_ON(__update_replicas(c, dst, &d2->r, -d2->delta));
1368 percpu_up_read(&c->mark_lock);
1374 static inline int bch2_trans_mark_pointer(struct btree_trans *trans,
1375 enum btree_id btree_id, unsigned level,
1376 struct bkey_s_c k, struct extent_ptr_decoded p,
1379 bool insert = !(flags & BTREE_TRIGGER_OVERWRITE);
1380 struct btree_iter iter;
1381 struct bkey_i_alloc_v4 *a;
1383 struct bch_backpointer bp;
1387 bch2_extent_ptr_to_bp(trans->c, btree_id, level, k, p, &bucket, &bp);
1388 sectors = bp.bucket_len;
1392 a = bch2_trans_start_alloc_update(trans, &iter, bucket);
1396 ret = __mark_pointer(trans, k, &p.ptr, sectors, bp.data_type,
1397 a->v.gen, &a->v.data_type,
1398 &a->v.dirty_sectors, &a->v.cached_sectors) ?:
1399 bch2_trans_update(trans, &iter, &a->k_i, 0);
1400 bch2_trans_iter_exit(trans, &iter);
1405 if (!p.ptr.cached) {
1406 ret = bch2_bucket_backpointer_mod(trans, bucket, bp, k, insert);
1414 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1415 struct extent_ptr_decoded p,
1416 s64 sectors, enum bch_data_type data_type)
1418 struct btree_iter iter;
1419 struct bkey_i_stripe *s;
1420 struct bch_replicas_padded r;
1423 s = bch2_bkey_get_mut_typed(trans, &iter,
1424 BTREE_ID_stripes, POS(0, p.ec.idx),
1425 BTREE_ITER_WITH_UPDATES, stripe);
1426 ret = PTR_ERR_OR_ZERO(s);
1427 if (unlikely(ret)) {
1428 bch2_trans_inconsistent_on(bch2_err_matches(ret, ENOENT), trans,
1429 "pointer to nonexistent stripe %llu",
1434 if (!bch2_ptr_matches_stripe(&s->v, p)) {
1435 bch2_trans_inconsistent(trans,
1436 "stripe pointer doesn't match stripe %llu",
1442 stripe_blockcount_set(&s->v, p.ec.block,
1443 stripe_blockcount_get(&s->v, p.ec.block) +
1446 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1447 r.e.data_type = data_type;
1448 ret = update_replicas_list(trans, &r.e, sectors);
1450 bch2_trans_iter_exit(trans, &iter);
1454 int bch2_trans_mark_extent(struct btree_trans *trans,
1455 enum btree_id btree_id, unsigned level,
1456 struct bkey_s_c old, struct bkey_i *new,
1459 struct bch_fs *c = trans->c;
1460 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1462 : bkey_i_to_s_c(new);
1463 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1464 const union bch_extent_entry *entry;
1465 struct extent_ptr_decoded p;
1466 struct bch_replicas_padded r;
1467 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
1470 s64 sectors = bkey_is_btree_ptr(k.k)
1473 s64 dirty_sectors = 0;
1477 r.e.data_type = data_type;
1479 r.e.nr_required = 1;
1481 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1482 s64 disk_sectors = ptr_disk_sectors(sectors, p);
1484 if (flags & BTREE_TRIGGER_OVERWRITE)
1485 disk_sectors = -disk_sectors;
1487 ret = bch2_trans_mark_pointer(trans, btree_id, level, k, p, flags);
1495 ret = update_cached_sectors_list(trans, p.ptr.dev,
1500 } else if (!p.has_ec) {
1501 dirty_sectors += disk_sectors;
1502 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1504 ret = bch2_trans_mark_stripe_ptr(trans, p,
1505 disk_sectors, data_type);
1509 r.e.nr_required = 0;
1514 ret = update_replicas_list(trans, &r.e, dirty_sectors);
1519 static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
1520 struct bkey_s_c_stripe s,
1521 unsigned idx, bool deleting)
1523 struct bch_fs *c = trans->c;
1524 const struct bch_extent_ptr *ptr = &s.v->ptrs[idx];
1525 struct btree_iter iter;
1526 struct bkey_i_alloc_v4 *a;
1527 enum bch_data_type data_type = idx >= s.v->nr_blocks - s.v->nr_redundant
1528 ? BCH_DATA_parity : 0;
1529 s64 sectors = data_type ? le16_to_cpu(s.v->sectors) : 0;
1535 a = bch2_trans_start_alloc_update(trans, &iter, PTR_BUCKET_POS(c, ptr));
1539 ret = check_bucket_ref(trans, s.s_c, ptr, sectors, data_type,
1540 a->v.gen, a->v.data_type,
1541 a->v.dirty_sectors, a->v.cached_sectors);
1546 if (bch2_trans_inconsistent_on(a->v.stripe ||
1547 a->v.stripe_redundancy, trans,
1548 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)",
1549 iter.pos.inode, iter.pos.offset, a->v.gen,
1550 bch2_data_types[a->v.data_type],
1552 a->v.stripe, s.k->p.offset)) {
1557 if (bch2_trans_inconsistent_on(data_type && a->v.dirty_sectors, trans,
1558 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: data already in stripe bucket %llu",
1559 iter.pos.inode, iter.pos.offset, a->v.gen,
1560 bch2_data_types[a->v.data_type],
1567 a->v.stripe = s.k->p.offset;
1568 a->v.stripe_redundancy = s.v->nr_redundant;
1569 a->v.data_type = BCH_DATA_stripe;
1571 if (bch2_trans_inconsistent_on(a->v.stripe != s.k->p.offset ||
1572 a->v.stripe_redundancy != s.v->nr_redundant, trans,
1573 "bucket %llu:%llu gen %u: not marked as stripe when deleting stripe %llu (got %u)",
1574 iter.pos.inode, iter.pos.offset, a->v.gen,
1575 s.k->p.offset, a->v.stripe)) {
1581 a->v.stripe_redundancy = 0;
1582 a->v.data_type = alloc_data_type(a->v, BCH_DATA_user);
1585 a->v.dirty_sectors += sectors;
1587 a->v.data_type = !deleting ? data_type : 0;
1589 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1593 bch2_trans_iter_exit(trans, &iter);
1597 int bch2_trans_mark_stripe(struct btree_trans *trans,
1598 enum btree_id btree_id, unsigned level,
1599 struct bkey_s_c old, struct bkey_i *new,
1602 const struct bch_stripe *old_s = NULL;
1603 struct bch_stripe *new_s = NULL;
1604 struct bch_replicas_padded r;
1605 unsigned i, nr_blocks;
1608 if (old.k->type == KEY_TYPE_stripe)
1609 old_s = bkey_s_c_to_stripe(old).v;
1610 if (new->k.type == KEY_TYPE_stripe)
1611 new_s = &bkey_i_to_stripe(new)->v;
1614 * If the pointers aren't changing, we don't need to do anything:
1616 if (new_s && old_s &&
1617 new_s->nr_blocks == old_s->nr_blocks &&
1618 new_s->nr_redundant == old_s->nr_redundant &&
1619 !memcmp(old_s->ptrs, new_s->ptrs,
1620 new_s->nr_blocks * sizeof(struct bch_extent_ptr)))
1623 BUG_ON(new_s && old_s &&
1624 (new_s->nr_blocks != old_s->nr_blocks ||
1625 new_s->nr_redundant != old_s->nr_redundant));
1627 nr_blocks = new_s ? new_s->nr_blocks : old_s->nr_blocks;
1630 s64 sectors = le16_to_cpu(new_s->sectors);
1632 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(new));
1633 ret = update_replicas_list(trans, &r.e, sectors * new_s->nr_redundant);
1639 s64 sectors = -((s64) le16_to_cpu(old_s->sectors));
1641 bch2_bkey_to_replicas(&r.e, old);
1642 ret = update_replicas_list(trans, &r.e, sectors * old_s->nr_redundant);
1647 for (i = 0; i < nr_blocks; i++) {
1648 if (new_s && old_s &&
1649 !memcmp(&new_s->ptrs[i],
1651 sizeof(new_s->ptrs[i])))
1655 ret = bch2_trans_mark_stripe_bucket(trans,
1656 bkey_i_to_s_c_stripe(new), i, false);
1662 ret = bch2_trans_mark_stripe_bucket(trans,
1663 bkey_s_c_to_stripe(old), i, true);
1672 int bch2_trans_mark_reservation(struct btree_trans *trans,
1673 enum btree_id btree_id, unsigned level,
1674 struct bkey_s_c old,
1678 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1680 : bkey_i_to_s_c(new);
1681 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1682 s64 sectors = (s64) k.k->size;
1683 struct replicas_delta_list *d;
1686 if (flags & BTREE_TRIGGER_OVERWRITE)
1688 sectors *= replicas;
1690 ret = bch2_replicas_deltas_realloc(trans, 0);
1694 d = trans->fs_usage_deltas;
1695 replicas = clamp_t(unsigned, replicas, 1,
1696 ARRAY_SIZE(d->persistent_reserved));
1698 d->persistent_reserved[replicas - 1] += sectors;
1702 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1703 struct bkey_s_c_reflink_p p,
1704 u64 *idx, unsigned flags)
1706 struct bch_fs *c = trans->c;
1707 struct btree_iter iter;
1710 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1711 struct printbuf buf = PRINTBUF;
1714 k = bch2_bkey_get_mut_noupdate(trans, &iter,
1715 BTREE_ID_reflink, POS(0, *idx),
1716 BTREE_ITER_WITH_UPDATES);
1717 ret = PTR_ERR_OR_ZERO(k);
1721 refcount = bkey_refcount(k);
1723 bch2_bkey_val_to_text(&buf, c, p.s_c);
1724 bch2_trans_inconsistent(trans,
1725 "nonexistent indirect extent at %llu while marking\n %s",
1731 if (!*refcount && (flags & BTREE_TRIGGER_OVERWRITE)) {
1732 bch2_bkey_val_to_text(&buf, c, p.s_c);
1733 bch2_trans_inconsistent(trans,
1734 "indirect extent refcount underflow at %llu while marking\n %s",
1740 if (flags & BTREE_TRIGGER_INSERT) {
1741 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1744 pad = max_t(s64, le32_to_cpu(v->front_pad),
1745 le64_to_cpu(v->idx) - bkey_start_offset(&k->k));
1746 BUG_ON(pad > U32_MAX);
1747 v->front_pad = cpu_to_le32(pad);
1749 pad = max_t(s64, le32_to_cpu(v->back_pad),
1750 k->k.p.offset - p.k->size - le64_to_cpu(v->idx));
1751 BUG_ON(pad > U32_MAX);
1752 v->back_pad = cpu_to_le32(pad);
1755 le64_add_cpu(refcount, add);
1757 bch2_btree_iter_set_pos_to_extent_start(&iter);
1758 ret = bch2_trans_update(trans, &iter, k, 0);
1762 *idx = k->k.p.offset;
1764 bch2_trans_iter_exit(trans, &iter);
1765 printbuf_exit(&buf);
1769 int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1770 enum btree_id btree_id, unsigned level,
1771 struct bkey_s_c old,
1775 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1777 : bkey_i_to_s_c(new);
1778 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1782 if (flags & BTREE_TRIGGER_INSERT) {
1783 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1785 v->front_pad = v->back_pad = 0;
1788 idx = le64_to_cpu(p.v->idx) - le32_to_cpu(p.v->front_pad);
1789 end_idx = le64_to_cpu(p.v->idx) + p.k->size +
1790 le32_to_cpu(p.v->back_pad);
1792 while (idx < end_idx && !ret)
1793 ret = __bch2_trans_mark_reflink_p(trans, p, &idx, flags);
1798 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1799 struct bch_dev *ca, size_t b,
1800 enum bch_data_type type,
1803 struct bch_fs *c = trans->c;
1804 struct btree_iter iter;
1805 struct bkey_i_alloc_v4 *a;
1809 * Backup superblock might be past the end of our normal usable space:
1811 if (b >= ca->mi.nbuckets)
1814 a = bch2_trans_start_alloc_update(trans, &iter, POS(ca->dev_idx, b));
1818 if (a->v.data_type && type && a->v.data_type != type) {
1819 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1820 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
1822 iter.pos.inode, iter.pos.offset, a->v.gen,
1823 bch2_data_types[a->v.data_type],
1824 bch2_data_types[type],
1825 bch2_data_types[type]);
1830 a->v.data_type = type;
1831 a->v.dirty_sectors = sectors;
1833 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1837 bch2_trans_iter_exit(trans, &iter);
1841 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1842 struct bch_dev *ca, size_t b,
1843 enum bch_data_type type,
1846 return commit_do(trans, NULL, NULL, 0,
1847 __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors));
1850 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans,
1853 enum bch_data_type type,
1854 u64 *bucket, unsigned *bucket_sectors)
1857 u64 b = sector_to_bucket(ca, start);
1859 min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
1861 if (b != *bucket && *bucket_sectors) {
1862 int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket,
1863 type, *bucket_sectors);
1867 *bucket_sectors = 0;
1871 *bucket_sectors += sectors;
1873 } while (start < end);
1878 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
1881 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
1883 unsigned i, bucket_sectors = 0;
1886 for (i = 0; i < layout->nr_superblocks; i++) {
1887 u64 offset = le64_to_cpu(layout->sb_offset[i]);
1889 if (offset == BCH_SB_SECTOR) {
1890 ret = bch2_trans_mark_metadata_sectors(trans, ca,
1892 BCH_DATA_sb, &bucket, &bucket_sectors);
1897 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset,
1898 offset + (1 << layout->sb_max_size_bits),
1899 BCH_DATA_sb, &bucket, &bucket_sectors);
1904 if (bucket_sectors) {
1905 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1906 bucket, BCH_DATA_sb, bucket_sectors);
1911 for (i = 0; i < ca->journal.nr; i++) {
1912 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1913 ca->journal.buckets[i],
1914 BCH_DATA_journal, ca->mi.bucket_size);
1922 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
1924 int ret = bch2_trans_run(c, __bch2_trans_mark_dev_sb(trans, ca));
1931 /* Disk reservations: */
1933 #define SECTORS_CACHE 1024
1935 int __bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
1936 u64 sectors, int flags)
1938 struct bch_fs_pcpu *pcpu;
1940 s64 sectors_available;
1943 percpu_down_read(&c->mark_lock);
1945 pcpu = this_cpu_ptr(c->pcpu);
1947 if (sectors <= pcpu->sectors_available)
1950 v = atomic64_read(&c->sectors_available);
1953 get = min((u64) sectors + SECTORS_CACHE, old);
1955 if (get < sectors) {
1959 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1960 old, old - get)) != old);
1962 pcpu->sectors_available += get;
1965 pcpu->sectors_available -= sectors;
1966 this_cpu_add(*c->online_reserved, sectors);
1967 res->sectors += sectors;
1970 percpu_up_read(&c->mark_lock);
1974 mutex_lock(&c->sectors_available_lock);
1976 percpu_u64_set(&c->pcpu->sectors_available, 0);
1977 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
1979 if (sectors <= sectors_available ||
1980 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
1981 atomic64_set(&c->sectors_available,
1982 max_t(s64, 0, sectors_available - sectors));
1983 this_cpu_add(*c->online_reserved, sectors);
1984 res->sectors += sectors;
1987 atomic64_set(&c->sectors_available, sectors_available);
1988 ret = -BCH_ERR_ENOSPC_disk_reservation;
1991 mutex_unlock(&c->sectors_available_lock);
1992 percpu_up_read(&c->mark_lock);
1997 /* Startup/shutdown: */
1999 static void bucket_gens_free_rcu(struct rcu_head *rcu)
2001 struct bucket_gens *buckets =
2002 container_of(rcu, struct bucket_gens, rcu);
2004 kvpfree(buckets, sizeof(*buckets) + buckets->nbuckets);
2007 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
2009 struct bucket_gens *bucket_gens = NULL, *old_bucket_gens = NULL;
2010 unsigned long *buckets_nouse = NULL;
2011 bool resize = ca->bucket_gens != NULL;
2014 if (!(bucket_gens = kvpmalloc(sizeof(struct bucket_gens) + nbuckets,
2015 GFP_KERNEL|__GFP_ZERO))) {
2016 ret = -BCH_ERR_ENOMEM_bucket_gens;
2020 if ((c->opts.buckets_nouse &&
2021 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2022 sizeof(unsigned long),
2023 GFP_KERNEL|__GFP_ZERO)))) {
2024 ret = -BCH_ERR_ENOMEM_buckets_nouse;
2028 bucket_gens->first_bucket = ca->mi.first_bucket;
2029 bucket_gens->nbuckets = nbuckets;
2031 bch2_copygc_stop(c);
2034 down_write(&c->gc_lock);
2035 down_write(&ca->bucket_lock);
2036 percpu_down_write(&c->mark_lock);
2039 old_bucket_gens = rcu_dereference_protected(ca->bucket_gens, 1);
2042 size_t n = min(bucket_gens->nbuckets, old_bucket_gens->nbuckets);
2044 memcpy(bucket_gens->b,
2048 memcpy(buckets_nouse,
2050 BITS_TO_LONGS(n) * sizeof(unsigned long));
2053 rcu_assign_pointer(ca->bucket_gens, bucket_gens);
2054 bucket_gens = old_bucket_gens;
2056 swap(ca->buckets_nouse, buckets_nouse);
2058 nbuckets = ca->mi.nbuckets;
2061 percpu_up_write(&c->mark_lock);
2062 up_write(&ca->bucket_lock);
2063 up_write(&c->gc_lock);
2068 kvpfree(buckets_nouse,
2069 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2071 call_rcu(&bucket_gens->rcu, bucket_gens_free_rcu);
2076 void bch2_dev_buckets_free(struct bch_dev *ca)
2080 kvpfree(ca->buckets_nouse,
2081 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2082 kvpfree(rcu_dereference_protected(ca->bucket_gens, 1),
2083 sizeof(struct bucket_gens) + ca->mi.nbuckets);
2085 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
2086 free_percpu(ca->usage[i]);
2087 kfree(ca->usage_base);
2090 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2094 ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL);
2095 if (!ca->usage_base)
2096 return -BCH_ERR_ENOMEM_usage_init;
2098 for (i = 0; i < ARRAY_SIZE(ca->usage); i++) {
2099 ca->usage[i] = alloc_percpu(struct bch_dev_usage);
2101 return -BCH_ERR_ENOMEM_usage_init;
2104 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);