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"
25 #include <linux/preempt.h>
26 #include <trace/events/bcachefs.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 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
106 unsigned journal_seq,
109 percpu_rwsem_assert_held(&c->mark_lock);
110 BUG_ON(!gc && !journal_seq);
112 return this_cpu_ptr(gc
114 : c->usage[journal_seq & JOURNAL_BUF_MASK]);
117 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
119 ssize_t offset = v - (u64 *) c->usage_base;
123 BUG_ON(offset < 0 || offset >= fs_usage_u64s(c));
124 percpu_rwsem_assert_held(&c->mark_lock);
127 seq = read_seqcount_begin(&c->usage_lock);
130 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
131 ret += percpu_u64_get((u64 __percpu *) c->usage[i] + offset);
132 } while (read_seqcount_retry(&c->usage_lock, seq));
137 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *c)
139 struct bch_fs_usage_online *ret;
140 unsigned seq, i, v, u64s = fs_usage_u64s(c) + 1;
142 ret = kmalloc(u64s * sizeof(u64), GFP_NOFS);
146 percpu_down_read(&c->mark_lock);
148 v = fs_usage_u64s(c) + 1;
149 if (unlikely(u64s != v)) {
151 percpu_up_read(&c->mark_lock);
156 ret->online_reserved = percpu_u64_get(c->online_reserved);
159 seq = read_seqcount_begin(&c->usage_lock);
160 unsafe_memcpy(&ret->u, c->usage_base, u64s * sizeof(u64),
161 "embedded variable length struct");
162 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
163 acc_u64s_percpu((u64 *) &ret->u, (u64 __percpu *) c->usage[i], u64s);
164 } while (read_seqcount_retry(&c->usage_lock, seq));
169 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx)
172 unsigned i, u64s = fs_usage_u64s(c);
174 BUG_ON(idx >= ARRAY_SIZE(c->usage));
177 write_seqcount_begin(&c->usage_lock);
179 acc_u64s_percpu((u64 *) c->usage_base,
180 (u64 __percpu *) c->usage[idx], u64s);
181 percpu_memset(c->usage[idx], 0, u64s * sizeof(u64));
184 for_each_member_device_rcu(ca, c, i, NULL) {
185 u64s = dev_usage_u64s();
187 acc_u64s_percpu((u64 *) ca->usage_base,
188 (u64 __percpu *) ca->usage[idx], u64s);
189 percpu_memset(ca->usage[idx], 0, u64s * sizeof(u64));
193 write_seqcount_end(&c->usage_lock);
197 void bch2_fs_usage_to_text(struct printbuf *out,
199 struct bch_fs_usage_online *fs_usage)
203 prt_printf(out, "capacity:\t\t\t%llu\n", c->capacity);
205 prt_printf(out, "hidden:\t\t\t\t%llu\n",
207 prt_printf(out, "data:\t\t\t\t%llu\n",
209 prt_printf(out, "cached:\t\t\t\t%llu\n",
211 prt_printf(out, "reserved:\t\t\t%llu\n",
212 fs_usage->u.reserved);
213 prt_printf(out, "nr_inodes:\t\t\t%llu\n",
214 fs_usage->u.nr_inodes);
215 prt_printf(out, "online reserved:\t\t%llu\n",
216 fs_usage->online_reserved);
219 i < ARRAY_SIZE(fs_usage->u.persistent_reserved);
221 prt_printf(out, "%u replicas:\n", i + 1);
222 prt_printf(out, "\treserved:\t\t%llu\n",
223 fs_usage->u.persistent_reserved[i]);
226 for (i = 0; i < c->replicas.nr; i++) {
227 struct bch_replicas_entry *e =
228 cpu_replicas_entry(&c->replicas, i);
230 prt_printf(out, "\t");
231 bch2_replicas_entry_to_text(out, e);
232 prt_printf(out, ":\t%llu\n", fs_usage->u.replicas[i]);
236 static u64 reserve_factor(u64 r)
238 return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR);
241 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage_online *fs_usage)
243 return min(fs_usage->u.hidden +
246 reserve_factor(fs_usage->u.reserved +
247 fs_usage->online_reserved),
251 static struct bch_fs_usage_short
252 __bch2_fs_usage_read_short(struct bch_fs *c)
254 struct bch_fs_usage_short ret;
257 ret.capacity = c->capacity -
258 bch2_fs_usage_read_one(c, &c->usage_base->hidden);
260 data = bch2_fs_usage_read_one(c, &c->usage_base->data) +
261 bch2_fs_usage_read_one(c, &c->usage_base->btree);
262 reserved = bch2_fs_usage_read_one(c, &c->usage_base->reserved) +
263 percpu_u64_get(c->online_reserved);
265 ret.used = min(ret.capacity, data + reserve_factor(reserved));
266 ret.free = ret.capacity - ret.used;
268 ret.nr_inodes = bch2_fs_usage_read_one(c, &c->usage_base->nr_inodes);
273 struct bch_fs_usage_short
274 bch2_fs_usage_read_short(struct bch_fs *c)
276 struct bch_fs_usage_short ret;
278 percpu_down_read(&c->mark_lock);
279 ret = __bch2_fs_usage_read_short(c);
280 percpu_up_read(&c->mark_lock);
285 void bch2_dev_usage_init(struct bch_dev *ca)
287 ca->usage_base->d[BCH_DATA_free].buckets = ca->mi.nbuckets - ca->mi.first_bucket;
290 static inline int bucket_sectors_fragmented(struct bch_dev *ca,
291 struct bch_alloc_v4 a)
293 return a.dirty_sectors
294 ? max(0, (int) ca->mi.bucket_size - (int) a.dirty_sectors)
298 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
299 struct bch_alloc_v4 old,
300 struct bch_alloc_v4 new,
301 u64 journal_seq, bool gc)
303 struct bch_fs_usage *fs_usage;
304 struct bch_dev_usage *u;
307 fs_usage = fs_usage_ptr(c, journal_seq, gc);
309 if (data_type_is_hidden(old.data_type))
310 fs_usage->hidden -= ca->mi.bucket_size;
311 if (data_type_is_hidden(new.data_type))
312 fs_usage->hidden += ca->mi.bucket_size;
314 u = dev_usage_ptr(ca, journal_seq, gc);
316 u->d[old.data_type].buckets--;
317 u->d[new.data_type].buckets++;
319 u->buckets_ec -= (int) !!old.stripe;
320 u->buckets_ec += (int) !!new.stripe;
322 u->d[old.data_type].sectors -= old.dirty_sectors;
323 u->d[new.data_type].sectors += new.dirty_sectors;
325 u->d[BCH_DATA_cached].sectors += new.cached_sectors;
326 u->d[BCH_DATA_cached].sectors -= old.cached_sectors;
328 u->d[old.data_type].fragmented -= bucket_sectors_fragmented(ca, old);
329 u->d[new.data_type].fragmented += bucket_sectors_fragmented(ca, new);
334 static void bch2_dev_usage_update_m(struct bch_fs *c, struct bch_dev *ca,
335 struct bucket old, struct bucket new,
336 u64 journal_seq, bool gc)
338 struct bch_alloc_v4 old_a = {
340 .data_type = old.data_type,
341 .dirty_sectors = old.dirty_sectors,
342 .cached_sectors = old.cached_sectors,
343 .stripe = old.stripe,
345 struct bch_alloc_v4 new_a = {
347 .data_type = new.data_type,
348 .dirty_sectors = new.dirty_sectors,
349 .cached_sectors = new.cached_sectors,
350 .stripe = new.stripe,
353 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
356 static inline int __update_replicas(struct bch_fs *c,
357 struct bch_fs_usage *fs_usage,
358 struct bch_replicas_entry *r,
361 int idx = bch2_replicas_entry_idx(c, r);
366 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
367 fs_usage->replicas[idx] += sectors;
371 static inline int update_replicas(struct bch_fs *c, struct bkey_s_c k,
372 struct bch_replicas_entry *r, s64 sectors,
373 unsigned journal_seq, bool gc)
375 struct bch_fs_usage __percpu *fs_usage;
377 struct printbuf buf = PRINTBUF;
379 percpu_down_read(&c->mark_lock);
382 idx = bch2_replicas_entry_idx(c, r);
384 fsck_err(c, "no replicas entry\n"
386 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
387 percpu_up_read(&c->mark_lock);
388 ret = bch2_mark_replicas(c, r);
389 percpu_down_read(&c->mark_lock);
393 idx = bch2_replicas_entry_idx(c, r);
401 fs_usage = fs_usage_ptr(c, journal_seq, gc);
402 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
403 fs_usage->replicas[idx] += sectors;
407 percpu_up_read(&c->mark_lock);
412 static inline int update_cached_sectors(struct bch_fs *c,
414 unsigned dev, s64 sectors,
415 unsigned journal_seq, bool gc)
417 struct bch_replicas_padded r;
419 bch2_replicas_entry_cached(&r.e, dev);
421 return update_replicas(c, k, &r.e, sectors, journal_seq, gc);
424 static struct replicas_delta_list *
425 replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
427 struct replicas_delta_list *d = trans->fs_usage_deltas;
428 unsigned new_size = d ? (d->size + more) * 2 : 128;
429 unsigned alloc_size = sizeof(*d) + new_size;
431 WARN_ON_ONCE(alloc_size > REPLICAS_DELTA_LIST_MAX);
433 if (!d || d->used + more > d->size) {
434 d = krealloc(d, alloc_size, GFP_NOIO|__GFP_ZERO);
436 BUG_ON(!d && alloc_size > REPLICAS_DELTA_LIST_MAX);
439 d = mempool_alloc(&trans->c->replicas_delta_pool, GFP_NOIO);
440 memset(d, 0, REPLICAS_DELTA_LIST_MAX);
442 if (trans->fs_usage_deltas)
443 memcpy(d, trans->fs_usage_deltas,
444 trans->fs_usage_deltas->size + sizeof(*d));
446 new_size = REPLICAS_DELTA_LIST_MAX - sizeof(*d);
447 kfree(trans->fs_usage_deltas);
451 trans->fs_usage_deltas = d;
456 static inline void update_replicas_list(struct btree_trans *trans,
457 struct bch_replicas_entry *r,
460 struct replicas_delta_list *d;
461 struct replicas_delta *n;
467 b = replicas_entry_bytes(r) + 8;
468 d = replicas_deltas_realloc(trans, b);
470 n = (void *) d->d + d->used;
472 memcpy((void *) n + offsetof(struct replicas_delta, r),
473 r, replicas_entry_bytes(r));
474 bch2_replicas_entry_sort(&n->r);
478 static inline void update_cached_sectors_list(struct btree_trans *trans,
479 unsigned dev, s64 sectors)
481 struct bch_replicas_padded r;
483 bch2_replicas_entry_cached(&r.e, dev);
485 update_replicas_list(trans, &r.e, sectors);
488 int bch2_mark_alloc(struct btree_trans *trans,
489 struct bkey_s_c old, struct bkey_s_c new,
492 bool gc = flags & BTREE_TRIGGER_GC;
493 u64 journal_seq = trans->journal_res.seq;
494 u64 bucket_journal_seq;
495 struct bch_fs *c = trans->c;
496 struct bch_alloc_v4 old_a_convert, new_a_convert;
497 const struct bch_alloc_v4 *old_a, *new_a;
502 * alloc btree is read in by bch2_alloc_read, not gc:
504 if ((flags & BTREE_TRIGGER_GC) &&
505 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
508 if (bch2_trans_inconsistent_on(!bch2_dev_bucket_exists(c, new.k->p), trans,
509 "alloc key for invalid device or bucket"))
512 ca = bch_dev_bkey_exists(c, new.k->p.inode);
514 old_a = bch2_alloc_to_v4(old, &old_a_convert);
515 new_a = bch2_alloc_to_v4(new, &new_a_convert);
517 bucket_journal_seq = new_a->journal_seq;
519 if ((flags & BTREE_TRIGGER_INSERT) &&
520 data_type_is_empty(old_a->data_type) !=
521 data_type_is_empty(new_a->data_type) &&
522 new.k->type == KEY_TYPE_alloc_v4) {
523 struct bch_alloc_v4 *v = (struct bch_alloc_v4 *) new.v;
525 EBUG_ON(!journal_seq);
528 * If the btree updates referring to a bucket weren't flushed
529 * before the bucket became empty again, then the we don't have
530 * to wait on a journal flush before we can reuse the bucket:
532 v->journal_seq = bucket_journal_seq =
533 data_type_is_empty(new_a->data_type) &&
534 (journal_seq == v->journal_seq ||
535 bch2_journal_noflush_seq(&c->journal, v->journal_seq))
539 if (!data_type_is_empty(old_a->data_type) &&
540 data_type_is_empty(new_a->data_type) &&
541 bucket_journal_seq) {
542 ret = bch2_set_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
543 c->journal.flushed_seq_ondisk,
544 new.k->p.inode, new.k->p.offset,
547 bch2_fs_fatal_error(c,
548 "error setting bucket_needs_journal_commit: %i", ret);
553 percpu_down_read(&c->mark_lock);
554 if (!gc && new_a->gen != old_a->gen)
555 *bucket_gen(ca, new.k->p.offset) = new_a->gen;
557 bch2_dev_usage_update(c, ca, *old_a, *new_a, journal_seq, gc);
560 struct bucket *g = gc_bucket(ca, new.k->p.offset);
566 g->data_type = new_a->data_type;
567 g->stripe = new_a->stripe;
568 g->stripe_redundancy = new_a->stripe_redundancy;
569 g->dirty_sectors = new_a->dirty_sectors;
570 g->cached_sectors = new_a->cached_sectors;
574 percpu_up_read(&c->mark_lock);
577 * need to know if we're getting called from the invalidate path or
581 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
582 old_a->cached_sectors) {
583 ret = update_cached_sectors(c, new, ca->dev_idx,
584 -((s64) old_a->cached_sectors),
587 bch2_fs_fatal_error(c, "%s(): no replicas entry while updating cached sectors",
593 if (new_a->data_type == BCH_DATA_free &&
594 (!new_a->journal_seq || new_a->journal_seq < c->journal.flushed_seq_ondisk))
595 closure_wake_up(&c->freelist_wait);
597 if (new_a->data_type == BCH_DATA_need_discard &&
598 (!bucket_journal_seq || bucket_journal_seq < c->journal.flushed_seq_ondisk))
601 if (old_a->data_type != BCH_DATA_cached &&
602 new_a->data_type == BCH_DATA_cached &&
603 should_invalidate_buckets(ca, bch2_dev_usage_read(ca)))
604 bch2_do_invalidates(c);
606 if (new_a->data_type == BCH_DATA_need_gc_gens)
612 int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
613 size_t b, enum bch_data_type data_type,
614 unsigned sectors, struct gc_pos pos,
617 struct bucket old, new, *g;
620 BUG_ON(!(flags & BTREE_TRIGGER_GC));
621 BUG_ON(data_type != BCH_DATA_sb &&
622 data_type != BCH_DATA_journal);
625 * Backup superblock might be past the end of our normal usable space:
627 if (b >= ca->mi.nbuckets)
630 percpu_down_read(&c->mark_lock);
631 g = gc_bucket(ca, b);
636 if (bch2_fs_inconsistent_on(g->data_type &&
637 g->data_type != data_type, c,
638 "different types of data in same bucket: %s, %s",
639 bch2_data_types[g->data_type],
640 bch2_data_types[data_type])) {
645 if (bch2_fs_inconsistent_on((u64) g->dirty_sectors + sectors > ca->mi.bucket_size, c,
646 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > bucket size",
647 ca->dev_idx, b, g->gen,
648 bch2_data_types[g->data_type ?: data_type],
649 g->dirty_sectors, sectors)) {
655 g->data_type = data_type;
656 g->dirty_sectors += sectors;
661 bch2_dev_usage_update_m(c, ca, old, new, 0, true);
662 percpu_up_read(&c->mark_lock);
666 static int check_bucket_ref(struct bch_fs *c,
668 const struct bch_extent_ptr *ptr,
669 s64 sectors, enum bch_data_type ptr_data_type,
670 u8 b_gen, u8 bucket_data_type,
671 u32 dirty_sectors, u32 cached_sectors)
673 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
674 size_t bucket_nr = PTR_BUCKET_NR(ca, ptr);
675 u16 bucket_sectors = !ptr->cached
678 struct printbuf buf = PRINTBUF;
681 if (bucket_data_type == BCH_DATA_cached)
682 bucket_data_type = BCH_DATA_user;
684 if ((bucket_data_type == BCH_DATA_stripe && ptr_data_type == BCH_DATA_user) ||
685 (bucket_data_type == BCH_DATA_user && ptr_data_type == BCH_DATA_stripe))
686 bucket_data_type = ptr_data_type = BCH_DATA_stripe;
688 if (gen_after(ptr->gen, b_gen)) {
689 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
690 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
692 ptr->dev, bucket_nr, b_gen,
693 bch2_data_types[bucket_data_type ?: ptr_data_type],
695 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
700 if (gen_cmp(b_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
701 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
702 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
704 ptr->dev, bucket_nr, b_gen,
705 bch2_data_types[bucket_data_type ?: ptr_data_type],
707 (printbuf_reset(&buf),
708 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
713 if (b_gen != ptr->gen && !ptr->cached) {
714 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
715 "bucket %u:%zu gen %u (mem gen %u) data type %s: stale dirty ptr (gen %u)\n"
717 ptr->dev, bucket_nr, b_gen,
718 *bucket_gen(ca, bucket_nr),
719 bch2_data_types[bucket_data_type ?: ptr_data_type],
721 (printbuf_reset(&buf),
722 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
727 if (b_gen != ptr->gen) {
732 if (!data_type_is_empty(bucket_data_type) &&
734 bucket_data_type != ptr_data_type) {
735 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
736 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
738 ptr->dev, bucket_nr, b_gen,
739 bch2_data_types[bucket_data_type],
740 bch2_data_types[ptr_data_type],
741 (printbuf_reset(&buf),
742 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
747 if ((unsigned) (bucket_sectors + sectors) > U32_MAX) {
748 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
749 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
751 ptr->dev, bucket_nr, b_gen,
752 bch2_data_types[bucket_data_type ?: ptr_data_type],
753 bucket_sectors, sectors,
754 (printbuf_reset(&buf),
755 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
764 static int mark_stripe_bucket(struct btree_trans *trans,
769 struct bch_fs *c = trans->c;
770 u64 journal_seq = trans->journal_res.seq;
771 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
772 unsigned nr_data = s->nr_blocks - s->nr_redundant;
773 bool parity = ptr_idx >= nr_data;
774 enum bch_data_type data_type = parity ? BCH_DATA_parity : 0;
775 s64 sectors = parity ? le16_to_cpu(s->sectors) : 0;
776 const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
777 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
778 struct bucket old, new, *g;
779 struct printbuf buf = PRINTBUF;
782 BUG_ON(!(flags & BTREE_TRIGGER_GC));
784 /* * XXX doesn't handle deletion */
786 percpu_down_read(&c->mark_lock);
788 g = PTR_GC_BUCKET(ca, ptr);
790 if (g->dirty_sectors ||
791 (g->stripe && g->stripe != k.k->p.offset)) {
792 bch2_fs_inconsistent(c,
793 "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
794 ptr->dev, PTR_BUCKET_NR(ca, ptr), g->gen,
795 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
803 ret = check_bucket_ref(c, k, ptr, sectors, data_type,
804 g->gen, g->data_type,
805 g->dirty_sectors, g->cached_sectors);
810 g->data_type = data_type;
811 g->dirty_sectors += sectors;
813 g->stripe = k.k->p.offset;
814 g->stripe_redundancy = s->nr_redundant;
819 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
820 percpu_up_read(&c->mark_lock);
825 static int __mark_pointer(struct btree_trans *trans,
827 const struct bch_extent_ptr *ptr,
828 s64 sectors, enum bch_data_type ptr_data_type,
829 u8 bucket_gen, u8 *bucket_data_type,
830 u32 *dirty_sectors, u32 *cached_sectors)
832 u32 *dst_sectors = !ptr->cached
835 int ret = check_bucket_ref(trans->c, k, ptr, sectors, ptr_data_type,
836 bucket_gen, *bucket_data_type,
837 *dirty_sectors, *cached_sectors);
842 *dst_sectors += sectors;
843 *bucket_data_type = *dirty_sectors || *cached_sectors
848 static int bch2_mark_pointer(struct btree_trans *trans,
850 struct extent_ptr_decoded p,
851 s64 sectors, enum bch_data_type data_type,
854 u64 journal_seq = trans->journal_res.seq;
855 struct bch_fs *c = trans->c;
856 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
857 struct bucket old, new, *g;
861 BUG_ON(!(flags & BTREE_TRIGGER_GC));
863 percpu_down_read(&c->mark_lock);
864 g = PTR_GC_BUCKET(ca, &p.ptr);
868 bucket_data_type = g->data_type;
869 ret = __mark_pointer(trans, k, &p.ptr, sectors,
875 g->data_type = bucket_data_type;
880 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
881 percpu_up_read(&c->mark_lock);
886 static int bch2_mark_stripe_ptr(struct btree_trans *trans,
888 struct bch_extent_stripe_ptr p,
889 enum bch_data_type data_type,
893 struct bch_fs *c = trans->c;
894 struct bch_replicas_padded r;
897 BUG_ON(!(flags & BTREE_TRIGGER_GC));
899 m = genradix_ptr_alloc(&c->gc_stripes, p.idx, GFP_KERNEL);
901 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
906 spin_lock(&c->ec_stripes_heap_lock);
908 if (!m || !m->alive) {
909 spin_unlock(&c->ec_stripes_heap_lock);
910 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
912 bch2_inconsistent_error(c);
916 m->block_sectors[p.block] += sectors;
919 spin_unlock(&c->ec_stripes_heap_lock);
921 r.e.data_type = data_type;
922 update_replicas(c, k, &r.e, sectors, trans->journal_res.seq, true);
927 int bch2_mark_extent(struct btree_trans *trans,
928 struct bkey_s_c old, struct bkey_s_c new,
931 u64 journal_seq = trans->journal_res.seq;
932 struct bch_fs *c = trans->c;
933 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
934 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
935 const union bch_extent_entry *entry;
936 struct extent_ptr_decoded p;
937 struct bch_replicas_padded r;
938 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
941 s64 sectors = bkey_is_btree_ptr(k.k)
944 s64 dirty_sectors = 0;
948 BUG_ON(!(flags & BTREE_TRIGGER_GC));
950 r.e.data_type = data_type;
954 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
955 s64 disk_sectors = ptr_disk_sectors(sectors, p);
957 if (flags & BTREE_TRIGGER_OVERWRITE)
958 disk_sectors = -disk_sectors;
960 ret = bch2_mark_pointer(trans, k, p, disk_sectors,
969 ret = update_cached_sectors(c, k, p.ptr.dev,
970 disk_sectors, journal_seq, true);
972 bch2_fs_fatal_error(c, "%s(): no replicas entry while updating cached sectors",
977 } else if (!p.has_ec) {
978 dirty_sectors += disk_sectors;
979 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
981 ret = bch2_mark_stripe_ptr(trans, k, p.ec, data_type,
982 disk_sectors, flags);
987 * There may be other dirty pointers in this extent, but
988 * if so they're not required for mounting if we have an
989 * erasure coded pointer in this extent:
996 ret = update_replicas(c, k, &r.e, dirty_sectors, journal_seq, true);
998 struct printbuf buf = PRINTBUF;
1000 bch2_bkey_val_to_text(&buf, c, k);
1001 bch2_fs_fatal_error(c, "%s(): no replicas entry for %s", __func__, buf.buf);
1002 printbuf_exit(&buf);
1010 int bch2_mark_stripe(struct btree_trans *trans,
1011 struct bkey_s_c old, struct bkey_s_c new,
1014 bool gc = flags & BTREE_TRIGGER_GC;
1015 u64 journal_seq = trans->journal_res.seq;
1016 struct bch_fs *c = trans->c;
1017 u64 idx = new.k->p.offset;
1018 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
1019 ? bkey_s_c_to_stripe(old).v : NULL;
1020 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
1021 ? bkey_s_c_to_stripe(new).v : NULL;
1025 BUG_ON(gc && old_s);
1028 struct stripe *m = genradix_ptr(&c->stripes, idx);
1030 if (!m || (old_s && !m->alive)) {
1031 struct printbuf buf1 = PRINTBUF;
1032 struct printbuf buf2 = PRINTBUF;
1034 bch2_bkey_val_to_text(&buf1, c, old);
1035 bch2_bkey_val_to_text(&buf2, c, new);
1036 bch_err_ratelimited(c, "error marking nonexistent stripe %llu while marking\n"
1038 "new %s", idx, buf1.buf, buf2.buf);
1039 printbuf_exit(&buf2);
1040 printbuf_exit(&buf1);
1041 bch2_inconsistent_error(c);
1046 spin_lock(&c->ec_stripes_heap_lock);
1047 bch2_stripes_heap_del(c, m, idx);
1048 spin_unlock(&c->ec_stripes_heap_lock);
1050 memset(m, 0, sizeof(*m));
1053 m->sectors = le16_to_cpu(new_s->sectors);
1054 m->algorithm = new_s->algorithm;
1055 m->nr_blocks = new_s->nr_blocks;
1056 m->nr_redundant = new_s->nr_redundant;
1057 m->blocks_nonempty = 0;
1059 for (i = 0; i < new_s->nr_blocks; i++)
1060 m->blocks_nonempty += !!stripe_blockcount_get(new_s, i);
1062 spin_lock(&c->ec_stripes_heap_lock);
1063 bch2_stripes_heap_update(c, m, idx);
1064 spin_unlock(&c->ec_stripes_heap_lock);
1067 struct gc_stripe *m =
1068 genradix_ptr_alloc(&c->gc_stripes, idx, GFP_KERNEL);
1071 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
1076 * This will be wrong when we bring back runtime gc: we should
1077 * be unmarking the old key and then marking the new key
1080 m->sectors = le16_to_cpu(new_s->sectors);
1081 m->nr_blocks = new_s->nr_blocks;
1082 m->nr_redundant = new_s->nr_redundant;
1084 for (i = 0; i < new_s->nr_blocks; i++)
1085 m->ptrs[i] = new_s->ptrs[i];
1087 bch2_bkey_to_replicas(&m->r.e, new);
1090 * gc recalculates this field from stripe ptr
1093 memset(m->block_sectors, 0, sizeof(m->block_sectors));
1095 for (i = 0; i < new_s->nr_blocks; i++) {
1096 ret = mark_stripe_bucket(trans, new, i, flags);
1101 ret = update_replicas(c, new, &m->r.e,
1102 ((s64) m->sectors * m->nr_redundant),
1105 struct printbuf buf = PRINTBUF;
1107 bch2_bkey_val_to_text(&buf, c, new);
1108 bch2_fs_fatal_error(c, "no replicas entry for %s", buf.buf);
1109 printbuf_exit(&buf);
1117 int bch2_mark_inode(struct btree_trans *trans,
1118 struct bkey_s_c old, struct bkey_s_c new,
1121 struct bch_fs *c = trans->c;
1122 struct bch_fs_usage __percpu *fs_usage;
1123 u64 journal_seq = trans->journal_res.seq;
1125 if (flags & BTREE_TRIGGER_INSERT) {
1126 struct bch_inode_v3 *v = (struct bch_inode_v3 *) new.v;
1128 BUG_ON(!journal_seq);
1129 BUG_ON(new.k->type != KEY_TYPE_inode_v3);
1131 v->bi_journal_seq = cpu_to_le64(journal_seq);
1134 if (flags & BTREE_TRIGGER_GC) {
1135 percpu_down_read(&c->mark_lock);
1138 fs_usage = fs_usage_ptr(c, journal_seq, flags & BTREE_TRIGGER_GC);
1139 fs_usage->nr_inodes += bkey_is_inode(new.k);
1140 fs_usage->nr_inodes -= bkey_is_inode(old.k);
1143 percpu_up_read(&c->mark_lock);
1148 int bch2_mark_reservation(struct btree_trans *trans,
1149 struct bkey_s_c old, struct bkey_s_c new,
1152 struct bch_fs *c = trans->c;
1153 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
1154 struct bch_fs_usage __percpu *fs_usage;
1155 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1156 s64 sectors = (s64) k.k->size;
1158 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1160 if (flags & BTREE_TRIGGER_OVERWRITE)
1162 sectors *= replicas;
1164 percpu_down_read(&c->mark_lock);
1167 fs_usage = fs_usage_ptr(c, trans->journal_res.seq, flags & BTREE_TRIGGER_GC);
1168 replicas = clamp_t(unsigned, replicas, 1,
1169 ARRAY_SIZE(fs_usage->persistent_reserved));
1171 fs_usage->reserved += sectors;
1172 fs_usage->persistent_reserved[replicas - 1] += sectors;
1175 percpu_up_read(&c->mark_lock);
1180 static s64 __bch2_mark_reflink_p(struct btree_trans *trans,
1181 struct bkey_s_c_reflink_p p,
1183 u64 *idx, unsigned flags, size_t r_idx)
1185 struct bch_fs *c = trans->c;
1186 struct reflink_gc *r;
1187 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1190 struct printbuf buf = PRINTBUF;
1192 if (r_idx >= c->reflink_gc_nr)
1195 r = genradix_ptr(&c->reflink_gc_table, r_idx);
1196 next_idx = min(next_idx, r->offset - r->size);
1197 if (*idx < next_idx)
1200 BUG_ON((s64) r->refcount + add < 0);
1206 if (fsck_err(c, "pointer to missing indirect extent\n"
1208 " missing range %llu-%llu",
1209 (bch2_bkey_val_to_text(&buf, c, p.s_c), buf.buf),
1211 struct bkey_i_error *new;
1213 new = bch2_trans_kmalloc(trans, sizeof(*new));
1214 ret = PTR_ERR_OR_ZERO(new);
1219 new->k.type = KEY_TYPE_error;
1220 new->k.p = bkey_start_pos(p.k);
1221 new->k.p.offset += *idx - start;
1222 bch2_key_resize(&new->k, next_idx - *idx);
1223 ret = __bch2_btree_insert(trans, BTREE_ID_extents, &new->k_i);
1229 printbuf_exit(&buf);
1233 int bch2_mark_reflink_p(struct btree_trans *trans,
1234 struct bkey_s_c old, struct bkey_s_c new,
1237 struct bch_fs *c = trans->c;
1238 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
1239 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1240 struct reflink_gc *ref;
1242 u64 idx = le64_to_cpu(p.v->idx), start = idx;
1243 u64 end = le64_to_cpu(p.v->idx) + p.k->size;
1246 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1248 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix) {
1249 idx -= le32_to_cpu(p.v->front_pad);
1250 end += le32_to_cpu(p.v->back_pad);
1254 r = c->reflink_gc_nr;
1256 m = l + (r - l) / 2;
1258 ref = genradix_ptr(&c->reflink_gc_table, m);
1259 if (ref->offset <= idx)
1265 while (idx < end && !ret)
1266 ret = __bch2_mark_reflink_p(trans, p, start, end,
1272 int bch2_trans_fs_usage_apply(struct btree_trans *trans,
1273 struct replicas_delta_list *deltas)
1275 struct bch_fs *c = trans->c;
1276 static int warned_disk_usage = 0;
1278 unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1279 struct replicas_delta *d = deltas->d, *d2;
1280 struct replicas_delta *top = (void *) deltas->d + deltas->used;
1281 struct bch_fs_usage *dst;
1282 s64 added = 0, should_not_have_added;
1285 percpu_down_read(&c->mark_lock);
1287 dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1289 for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1290 switch (d->r.data_type) {
1291 case BCH_DATA_btree:
1293 case BCH_DATA_parity:
1297 if (__update_replicas(c, dst, &d->r, d->delta))
1301 dst->nr_inodes += deltas->nr_inodes;
1303 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1304 added += deltas->persistent_reserved[i];
1305 dst->reserved += deltas->persistent_reserved[i];
1306 dst->persistent_reserved[i] += deltas->persistent_reserved[i];
1310 * Not allowed to reduce sectors_available except by getting a
1313 should_not_have_added = added - (s64) disk_res_sectors;
1314 if (unlikely(should_not_have_added > 0)) {
1315 u64 old, new, v = atomic64_read(&c->sectors_available);
1319 new = max_t(s64, 0, old - should_not_have_added);
1320 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1323 added -= should_not_have_added;
1328 trans->disk_res->sectors -= added;
1329 this_cpu_sub(*c->online_reserved, added);
1333 percpu_up_read(&c->mark_lock);
1335 if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
1336 bch2_trans_inconsistent(trans,
1337 "disk usage increased %lli more than %u sectors reserved)",
1338 should_not_have_added, disk_res_sectors);
1341 /* revert changes: */
1342 for (d2 = deltas->d; d2 != d; d2 = replicas_delta_next(d2))
1343 BUG_ON(__update_replicas(c, dst, &d2->r, -d2->delta));
1346 percpu_up_read(&c->mark_lock);
1352 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1353 enum btree_id btree_id, unsigned level,
1354 struct bkey_s_c k, struct extent_ptr_decoded p,
1357 bool insert = !(flags & BTREE_TRIGGER_OVERWRITE);
1358 struct btree_iter iter;
1359 struct bkey_i_alloc_v4 *a;
1360 struct bpos bucket_pos;
1361 struct bch_backpointer bp;
1365 bch2_extent_ptr_to_bp(trans->c, btree_id, level, k, p, &bucket_pos, &bp);
1366 sectors = bp.bucket_len;
1370 a = bch2_trans_start_alloc_update(trans, &iter, bucket_pos);
1374 ret = __mark_pointer(trans, k, &p.ptr, sectors, bp.data_type,
1375 a->v.gen, &a->v.data_type,
1376 &a->v.dirty_sectors, &a->v.cached_sectors);
1380 if (!p.ptr.cached) {
1382 ? bch2_bucket_backpointer_add(trans, a, bp, k)
1383 : bch2_bucket_backpointer_del(trans, a, bp, k);
1388 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1390 bch2_trans_iter_exit(trans, &iter);
1394 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1395 struct extent_ptr_decoded p,
1396 s64 sectors, enum bch_data_type data_type)
1398 struct btree_iter iter;
1399 struct bkey_i_stripe *s;
1400 struct bch_replicas_padded r;
1403 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, POS(0, p.ec.idx),
1405 BTREE_ITER_WITH_UPDATES);
1406 s = bch2_bkey_get_mut_typed(trans, &iter, stripe);
1407 ret = PTR_ERR_OR_ZERO(s);
1408 if (unlikely(ret)) {
1409 bch2_trans_inconsistent_on(ret == -ENOENT, trans,
1410 "pointer to nonexistent stripe %llu",
1415 if (!bch2_ptr_matches_stripe(&s->v, p)) {
1416 bch2_trans_inconsistent(trans,
1417 "stripe pointer doesn't match stripe %llu",
1423 stripe_blockcount_set(&s->v, p.ec.block,
1424 stripe_blockcount_get(&s->v, p.ec.block) +
1427 ret = bch2_trans_update(trans, &iter, &s->k_i, 0);
1431 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1432 r.e.data_type = data_type;
1433 update_replicas_list(trans, &r.e, sectors);
1435 bch2_trans_iter_exit(trans, &iter);
1439 int bch2_trans_mark_extent(struct btree_trans *trans,
1440 enum btree_id btree_id, unsigned level,
1441 struct bkey_s_c old, struct bkey_i *new,
1444 struct bch_fs *c = trans->c;
1445 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1447 : bkey_i_to_s_c(new);
1448 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1449 const union bch_extent_entry *entry;
1450 struct extent_ptr_decoded p;
1451 struct bch_replicas_padded r;
1452 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
1455 s64 sectors = bkey_is_btree_ptr(k.k)
1458 s64 dirty_sectors = 0;
1462 r.e.data_type = data_type;
1464 r.e.nr_required = 1;
1466 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1467 s64 disk_sectors = ptr_disk_sectors(sectors, p);
1469 if (flags & BTREE_TRIGGER_OVERWRITE)
1470 disk_sectors = -disk_sectors;
1472 ret = bch2_trans_mark_pointer(trans, btree_id, level, k, p, flags);
1480 update_cached_sectors_list(trans, p.ptr.dev,
1482 } else if (!p.has_ec) {
1483 dirty_sectors += disk_sectors;
1484 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1486 ret = bch2_trans_mark_stripe_ptr(trans, p,
1487 disk_sectors, data_type);
1491 r.e.nr_required = 0;
1496 update_replicas_list(trans, &r.e, dirty_sectors);
1501 static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
1502 struct bkey_s_c_stripe s,
1503 unsigned idx, bool deleting)
1505 struct bch_fs *c = trans->c;
1506 const struct bch_extent_ptr *ptr = &s.v->ptrs[idx];
1507 struct btree_iter iter;
1508 struct bkey_i_alloc_v4 *a;
1509 enum bch_data_type data_type = idx >= s.v->nr_blocks - s.v->nr_redundant
1510 ? BCH_DATA_parity : 0;
1511 s64 sectors = data_type ? le16_to_cpu(s.v->sectors) : 0;
1517 a = bch2_trans_start_alloc_update(trans, &iter, PTR_BUCKET_POS(c, ptr));
1521 ret = check_bucket_ref(c, s.s_c, ptr, sectors, data_type,
1522 a->v.gen, a->v.data_type,
1523 a->v.dirty_sectors, a->v.cached_sectors);
1528 if (bch2_trans_inconsistent_on(a->v.stripe ||
1529 a->v.stripe_redundancy, trans,
1530 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)",
1531 iter.pos.inode, iter.pos.offset, a->v.gen,
1532 bch2_data_types[a->v.data_type],
1534 a->v.stripe, s.k->p.offset)) {
1539 if (bch2_trans_inconsistent_on(data_type && a->v.dirty_sectors, trans,
1540 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: data already in stripe bucket %llu",
1541 iter.pos.inode, iter.pos.offset, a->v.gen,
1542 bch2_data_types[a->v.data_type],
1549 a->v.stripe = s.k->p.offset;
1550 a->v.stripe_redundancy = s.v->nr_redundant;
1552 if (bch2_trans_inconsistent_on(a->v.stripe != s.k->p.offset ||
1553 a->v.stripe_redundancy != s.v->nr_redundant, trans,
1554 "bucket %llu:%llu gen %u: not marked as stripe when deleting stripe %llu (got %u)",
1555 iter.pos.inode, iter.pos.offset, a->v.gen,
1556 s.k->p.offset, a->v.stripe)) {
1562 a->v.stripe_redundancy = 0;
1565 a->v.dirty_sectors += sectors;
1567 a->v.data_type = !deleting ? data_type : 0;
1569 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1573 bch2_trans_iter_exit(trans, &iter);
1577 int bch2_trans_mark_stripe(struct btree_trans *trans,
1578 enum btree_id btree_id, unsigned level,
1579 struct bkey_s_c old, struct bkey_i *new,
1582 const struct bch_stripe *old_s = NULL;
1583 struct bch_stripe *new_s = NULL;
1584 struct bch_replicas_padded r;
1585 unsigned i, nr_blocks;
1588 if (old.k->type == KEY_TYPE_stripe)
1589 old_s = bkey_s_c_to_stripe(old).v;
1590 if (new->k.type == KEY_TYPE_stripe)
1591 new_s = &bkey_i_to_stripe(new)->v;
1594 * If the pointers aren't changing, we don't need to do anything:
1596 if (new_s && old_s &&
1597 new_s->nr_blocks == old_s->nr_blocks &&
1598 new_s->nr_redundant == old_s->nr_redundant &&
1599 !memcmp(old_s->ptrs, new_s->ptrs,
1600 new_s->nr_blocks * sizeof(struct bch_extent_ptr)))
1603 BUG_ON(new_s && old_s &&
1604 (new_s->nr_blocks != old_s->nr_blocks ||
1605 new_s->nr_redundant != old_s->nr_redundant));
1607 nr_blocks = new_s ? new_s->nr_blocks : old_s->nr_blocks;
1610 s64 sectors = le16_to_cpu(new_s->sectors);
1612 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(new));
1613 update_replicas_list(trans, &r.e, sectors * new_s->nr_redundant);
1617 s64 sectors = -((s64) le16_to_cpu(old_s->sectors));
1619 bch2_bkey_to_replicas(&r.e, old);
1620 update_replicas_list(trans, &r.e, sectors * old_s->nr_redundant);
1623 for (i = 0; i < nr_blocks; i++) {
1624 if (new_s && old_s &&
1625 !memcmp(&new_s->ptrs[i],
1627 sizeof(new_s->ptrs[i])))
1631 ret = bch2_trans_mark_stripe_bucket(trans,
1632 bkey_i_to_s_c_stripe(new), i, false);
1638 ret = bch2_trans_mark_stripe_bucket(trans,
1639 bkey_s_c_to_stripe(old), i, true);
1648 int bch2_trans_mark_inode(struct btree_trans *trans,
1649 enum btree_id btree_id, unsigned level,
1650 struct bkey_s_c old,
1654 int nr = bkey_is_inode(&new->k) - bkey_is_inode(old.k);
1657 struct replicas_delta_list *d =
1658 replicas_deltas_realloc(trans, 0);
1665 int bch2_trans_mark_reservation(struct btree_trans *trans,
1666 enum btree_id btree_id, unsigned level,
1667 struct bkey_s_c old,
1671 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1673 : bkey_i_to_s_c(new);
1674 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1675 s64 sectors = (s64) k.k->size;
1676 struct replicas_delta_list *d;
1678 if (flags & BTREE_TRIGGER_OVERWRITE)
1680 sectors *= replicas;
1682 d = replicas_deltas_realloc(trans, 0);
1684 replicas = clamp_t(unsigned, replicas, 1,
1685 ARRAY_SIZE(d->persistent_reserved));
1687 d->persistent_reserved[replicas - 1] += sectors;
1691 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1692 struct bkey_s_c_reflink_p p,
1693 u64 *idx, unsigned flags)
1695 struct bch_fs *c = trans->c;
1696 struct btree_iter iter;
1699 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1700 struct printbuf buf = PRINTBUF;
1703 bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink, POS(0, *idx),
1705 BTREE_ITER_WITH_UPDATES);
1706 k = bch2_bkey_get_mut(trans, &iter);
1707 ret = PTR_ERR_OR_ZERO(k);
1711 refcount = bkey_refcount(k);
1713 bch2_bkey_val_to_text(&buf, c, p.s_c);
1714 bch2_trans_inconsistent(trans,
1715 "nonexistent indirect extent at %llu while marking\n %s",
1721 if (!*refcount && (flags & BTREE_TRIGGER_OVERWRITE)) {
1722 bch2_bkey_val_to_text(&buf, c, p.s_c);
1723 bch2_trans_inconsistent(trans,
1724 "indirect extent refcount underflow at %llu while marking\n %s",
1730 if (flags & BTREE_TRIGGER_INSERT) {
1731 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1734 pad = max_t(s64, le32_to_cpu(v->front_pad),
1735 le64_to_cpu(v->idx) - bkey_start_offset(&k->k));
1736 BUG_ON(pad > U32_MAX);
1737 v->front_pad = cpu_to_le32(pad);
1739 pad = max_t(s64, le32_to_cpu(v->back_pad),
1740 k->k.p.offset - p.k->size - le64_to_cpu(v->idx));
1741 BUG_ON(pad > U32_MAX);
1742 v->back_pad = cpu_to_le32(pad);
1745 le64_add_cpu(refcount, add);
1747 bch2_btree_iter_set_pos_to_extent_start(&iter);
1748 ret = bch2_trans_update(trans, &iter, k, 0);
1752 *idx = k->k.p.offset;
1754 bch2_trans_iter_exit(trans, &iter);
1755 printbuf_exit(&buf);
1759 int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1760 enum btree_id btree_id, unsigned level,
1761 struct bkey_s_c old,
1765 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1767 : bkey_i_to_s_c(new);
1768 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1772 if (flags & BTREE_TRIGGER_INSERT) {
1773 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1775 v->front_pad = v->back_pad = 0;
1778 idx = le64_to_cpu(p.v->idx) - le32_to_cpu(p.v->front_pad);
1779 end_idx = le64_to_cpu(p.v->idx) + p.k->size +
1780 le32_to_cpu(p.v->back_pad);
1782 while (idx < end_idx && !ret)
1783 ret = __bch2_trans_mark_reflink_p(trans, p, &idx, flags);
1788 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1789 struct bch_dev *ca, size_t b,
1790 enum bch_data_type type,
1793 struct bch_fs *c = trans->c;
1794 struct btree_iter iter;
1795 struct bkey_i_alloc_v4 *a;
1799 * Backup superblock might be past the end of our normal usable space:
1801 if (b >= ca->mi.nbuckets)
1804 a = bch2_trans_start_alloc_update(trans, &iter, POS(ca->dev_idx, b));
1808 if (a->v.data_type && a->v.data_type != type) {
1809 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1810 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
1812 iter.pos.inode, iter.pos.offset, a->v.gen,
1813 bch2_data_types[a->v.data_type],
1814 bch2_data_types[type],
1815 bch2_data_types[type]);
1820 a->v.data_type = type;
1821 a->v.dirty_sectors = sectors;
1823 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1827 bch2_trans_iter_exit(trans, &iter);
1831 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1832 struct bch_dev *ca, size_t b,
1833 enum bch_data_type type,
1836 return commit_do(trans, NULL, NULL, 0,
1837 __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors));
1840 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans,
1843 enum bch_data_type type,
1844 u64 *bucket, unsigned *bucket_sectors)
1847 u64 b = sector_to_bucket(ca, start);
1849 min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
1851 if (b != *bucket && *bucket_sectors) {
1852 int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket,
1853 type, *bucket_sectors);
1857 *bucket_sectors = 0;
1861 *bucket_sectors += sectors;
1863 } while (start < end);
1868 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
1871 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
1873 unsigned i, bucket_sectors = 0;
1876 for (i = 0; i < layout->nr_superblocks; i++) {
1877 u64 offset = le64_to_cpu(layout->sb_offset[i]);
1879 if (offset == BCH_SB_SECTOR) {
1880 ret = bch2_trans_mark_metadata_sectors(trans, ca,
1882 BCH_DATA_sb, &bucket, &bucket_sectors);
1887 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset,
1888 offset + (1 << layout->sb_max_size_bits),
1889 BCH_DATA_sb, &bucket, &bucket_sectors);
1894 if (bucket_sectors) {
1895 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1896 bucket, BCH_DATA_sb, bucket_sectors);
1901 for (i = 0; i < ca->journal.nr; i++) {
1902 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1903 ca->journal.buckets[i],
1904 BCH_DATA_journal, ca->mi.bucket_size);
1912 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
1914 return bch2_trans_run(c, __bch2_trans_mark_dev_sb(&trans, ca));
1917 /* Disk reservations: */
1919 #define SECTORS_CACHE 1024
1921 int __bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
1922 u64 sectors, int flags)
1924 struct bch_fs_pcpu *pcpu;
1926 s64 sectors_available;
1929 percpu_down_read(&c->mark_lock);
1931 pcpu = this_cpu_ptr(c->pcpu);
1933 if (sectors <= pcpu->sectors_available)
1936 v = atomic64_read(&c->sectors_available);
1939 get = min((u64) sectors + SECTORS_CACHE, old);
1941 if (get < sectors) {
1945 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1946 old, old - get)) != old);
1948 pcpu->sectors_available += get;
1951 pcpu->sectors_available -= sectors;
1952 this_cpu_add(*c->online_reserved, sectors);
1953 res->sectors += sectors;
1956 percpu_up_read(&c->mark_lock);
1960 mutex_lock(&c->sectors_available_lock);
1962 percpu_u64_set(&c->pcpu->sectors_available, 0);
1963 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
1965 if (sectors <= sectors_available ||
1966 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
1967 atomic64_set(&c->sectors_available,
1968 max_t(s64, 0, sectors_available - sectors));
1969 this_cpu_add(*c->online_reserved, sectors);
1970 res->sectors += sectors;
1973 atomic64_set(&c->sectors_available, sectors_available);
1974 ret = -BCH_ERR_ENOSPC_disk_reservation;
1977 mutex_unlock(&c->sectors_available_lock);
1978 percpu_up_read(&c->mark_lock);
1983 /* Startup/shutdown: */
1985 static void bucket_gens_free_rcu(struct rcu_head *rcu)
1987 struct bucket_gens *buckets =
1988 container_of(rcu, struct bucket_gens, rcu);
1990 kvpfree(buckets, sizeof(*buckets) + buckets->nbuckets);
1993 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1995 struct bucket_gens *bucket_gens = NULL, *old_bucket_gens = NULL;
1996 unsigned long *buckets_nouse = NULL;
1997 bool resize = ca->bucket_gens != NULL;
2000 if (!(bucket_gens = kvpmalloc(sizeof(struct bucket_gens) + nbuckets,
2001 GFP_KERNEL|__GFP_ZERO)) ||
2002 (c->opts.buckets_nouse &&
2003 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2004 sizeof(unsigned long),
2005 GFP_KERNEL|__GFP_ZERO))))
2008 bucket_gens->first_bucket = ca->mi.first_bucket;
2009 bucket_gens->nbuckets = nbuckets;
2011 bch2_copygc_stop(c);
2014 down_write(&c->gc_lock);
2015 down_write(&ca->bucket_lock);
2016 percpu_down_write(&c->mark_lock);
2019 old_bucket_gens = rcu_dereference_protected(ca->bucket_gens, 1);
2022 size_t n = min(bucket_gens->nbuckets, old_bucket_gens->nbuckets);
2024 memcpy(bucket_gens->b,
2028 memcpy(buckets_nouse,
2030 BITS_TO_LONGS(n) * sizeof(unsigned long));
2033 rcu_assign_pointer(ca->bucket_gens, bucket_gens);
2034 bucket_gens = old_bucket_gens;
2036 swap(ca->buckets_nouse, buckets_nouse);
2038 nbuckets = ca->mi.nbuckets;
2041 percpu_up_write(&c->mark_lock);
2042 up_write(&ca->bucket_lock);
2043 up_write(&c->gc_lock);
2048 kvpfree(buckets_nouse,
2049 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2051 call_rcu(&bucket_gens->rcu, bucket_gens_free_rcu);
2056 void bch2_dev_buckets_free(struct bch_dev *ca)
2060 kvpfree(ca->buckets_nouse,
2061 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2062 kvpfree(rcu_dereference_protected(ca->bucket_gens, 1),
2063 sizeof(struct bucket_gens) + ca->mi.nbuckets);
2065 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
2066 free_percpu(ca->usage[i]);
2067 kfree(ca->usage_base);
2070 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2074 ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL);
2075 if (!ca->usage_base)
2078 for (i = 0; i < ARRAY_SIZE(ca->usage); i++) {
2079 ca->usage[i] = alloc_percpu(struct bch_dev_usage);
2084 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);