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 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
94 struct bch_fs *c = ca->fs;
95 struct bch_dev_usage ret;
96 unsigned seq, i, u64s = dev_usage_u64s();
99 seq = read_seqcount_begin(&c->usage_lock);
100 memcpy(&ret, ca->usage_base, u64s * sizeof(u64));
101 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
102 acc_u64s_percpu((u64 *) &ret, (u64 __percpu *) ca->usage[i], u64s);
103 } while (read_seqcount_retry(&c->usage_lock, seq));
108 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
109 unsigned journal_seq,
112 percpu_rwsem_assert_held(&c->mark_lock);
113 BUG_ON(!gc && !journal_seq);
115 return this_cpu_ptr(gc
117 : c->usage[journal_seq & JOURNAL_BUF_MASK]);
120 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
122 ssize_t offset = v - (u64 *) c->usage_base;
126 BUG_ON(offset < 0 || offset >= fs_usage_u64s(c));
127 percpu_rwsem_assert_held(&c->mark_lock);
130 seq = read_seqcount_begin(&c->usage_lock);
133 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
134 ret += percpu_u64_get((u64 __percpu *) c->usage[i] + offset);
135 } while (read_seqcount_retry(&c->usage_lock, seq));
140 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *c)
142 struct bch_fs_usage_online *ret;
143 unsigned seq, i, u64s;
145 percpu_down_read(&c->mark_lock);
147 ret = kmalloc(sizeof(struct bch_fs_usage_online) +
148 sizeof(u64) * c->replicas.nr, GFP_NOFS);
149 if (unlikely(!ret)) {
150 percpu_up_read(&c->mark_lock);
154 ret->online_reserved = percpu_u64_get(c->online_reserved);
156 u64s = fs_usage_u64s(c);
158 seq = read_seqcount_begin(&c->usage_lock);
159 memcpy(&ret->u, c->usage_base, u64s * sizeof(u64));
160 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
161 acc_u64s_percpu((u64 *) &ret->u, (u64 __percpu *) c->usage[i], u64s);
162 } while (read_seqcount_retry(&c->usage_lock, seq));
167 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx)
170 unsigned i, u64s = fs_usage_u64s(c);
172 BUG_ON(idx >= ARRAY_SIZE(c->usage));
175 write_seqcount_begin(&c->usage_lock);
177 acc_u64s_percpu((u64 *) c->usage_base,
178 (u64 __percpu *) c->usage[idx], u64s);
179 percpu_memset(c->usage[idx], 0, u64s * sizeof(u64));
182 for_each_member_device_rcu(ca, c, i, NULL) {
183 u64s = dev_usage_u64s();
185 acc_u64s_percpu((u64 *) ca->usage_base,
186 (u64 __percpu *) ca->usage[idx], u64s);
187 percpu_memset(ca->usage[idx], 0, u64s * sizeof(u64));
191 write_seqcount_end(&c->usage_lock);
195 void bch2_fs_usage_to_text(struct printbuf *out,
197 struct bch_fs_usage_online *fs_usage)
201 prt_printf(out, "capacity:\t\t\t%llu\n", c->capacity);
203 prt_printf(out, "hidden:\t\t\t\t%llu\n",
205 prt_printf(out, "data:\t\t\t\t%llu\n",
207 prt_printf(out, "cached:\t\t\t\t%llu\n",
209 prt_printf(out, "reserved:\t\t\t%llu\n",
210 fs_usage->u.reserved);
211 prt_printf(out, "nr_inodes:\t\t\t%llu\n",
212 fs_usage->u.nr_inodes);
213 prt_printf(out, "online reserved:\t\t%llu\n",
214 fs_usage->online_reserved);
217 i < ARRAY_SIZE(fs_usage->u.persistent_reserved);
219 prt_printf(out, "%u replicas:\n", i + 1);
220 prt_printf(out, "\treserved:\t\t%llu\n",
221 fs_usage->u.persistent_reserved[i]);
224 for (i = 0; i < c->replicas.nr; i++) {
225 struct bch_replicas_entry *e =
226 cpu_replicas_entry(&c->replicas, i);
228 prt_printf(out, "\t");
229 bch2_replicas_entry_to_text(out, e);
230 prt_printf(out, ":\t%llu\n", fs_usage->u.replicas[i]);
234 static u64 reserve_factor(u64 r)
236 return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR);
239 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage_online *fs_usage)
241 return min(fs_usage->u.hidden +
244 reserve_factor(fs_usage->u.reserved +
245 fs_usage->online_reserved),
249 static struct bch_fs_usage_short
250 __bch2_fs_usage_read_short(struct bch_fs *c)
252 struct bch_fs_usage_short ret;
255 ret.capacity = c->capacity -
256 bch2_fs_usage_read_one(c, &c->usage_base->hidden);
258 data = bch2_fs_usage_read_one(c, &c->usage_base->data) +
259 bch2_fs_usage_read_one(c, &c->usage_base->btree);
260 reserved = bch2_fs_usage_read_one(c, &c->usage_base->reserved) +
261 percpu_u64_get(c->online_reserved);
263 ret.used = min(ret.capacity, data + reserve_factor(reserved));
264 ret.free = ret.capacity - ret.used;
266 ret.nr_inodes = bch2_fs_usage_read_one(c, &c->usage_base->nr_inodes);
271 struct bch_fs_usage_short
272 bch2_fs_usage_read_short(struct bch_fs *c)
274 struct bch_fs_usage_short ret;
276 percpu_down_read(&c->mark_lock);
277 ret = __bch2_fs_usage_read_short(c);
278 percpu_up_read(&c->mark_lock);
283 void bch2_dev_usage_init(struct bch_dev *ca)
285 ca->usage_base->d[BCH_DATA_free].buckets = ca->mi.nbuckets - ca->mi.first_bucket;
288 static inline int bucket_sectors_fragmented(struct bch_dev *ca,
289 struct bch_alloc_v4 a)
291 return a.dirty_sectors
292 ? max(0, (int) ca->mi.bucket_size - (int) a.dirty_sectors)
296 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
297 struct bch_alloc_v4 old,
298 struct bch_alloc_v4 new,
299 u64 journal_seq, bool gc)
301 struct bch_fs_usage *fs_usage;
302 struct bch_dev_usage *u;
305 fs_usage = fs_usage_ptr(c, journal_seq, gc);
307 if (data_type_is_hidden(old.data_type))
308 fs_usage->hidden -= ca->mi.bucket_size;
309 if (data_type_is_hidden(new.data_type))
310 fs_usage->hidden += ca->mi.bucket_size;
312 u = dev_usage_ptr(ca, journal_seq, gc);
314 u->d[old.data_type].buckets--;
315 u->d[new.data_type].buckets++;
317 u->buckets_ec -= (int) !!old.stripe;
318 u->buckets_ec += (int) !!new.stripe;
320 u->d[old.data_type].sectors -= old.dirty_sectors;
321 u->d[new.data_type].sectors += new.dirty_sectors;
323 u->d[BCH_DATA_cached].sectors += new.cached_sectors;
324 u->d[BCH_DATA_cached].sectors -= old.cached_sectors;
326 u->d[old.data_type].fragmented -= bucket_sectors_fragmented(ca, old);
327 u->d[new.data_type].fragmented += bucket_sectors_fragmented(ca, new);
332 static void bch2_dev_usage_update_m(struct bch_fs *c, struct bch_dev *ca,
333 struct bucket old, struct bucket new,
334 u64 journal_seq, bool gc)
336 struct bch_alloc_v4 old_a = {
338 .data_type = old.data_type,
339 .dirty_sectors = old.dirty_sectors,
340 .cached_sectors = old.cached_sectors,
341 .stripe = old.stripe,
343 struct bch_alloc_v4 new_a = {
345 .data_type = new.data_type,
346 .dirty_sectors = new.dirty_sectors,
347 .cached_sectors = new.cached_sectors,
348 .stripe = new.stripe,
351 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
354 static inline int __update_replicas(struct bch_fs *c,
355 struct bch_fs_usage *fs_usage,
356 struct bch_replicas_entry *r,
359 int idx = bch2_replicas_entry_idx(c, r);
364 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
365 fs_usage->replicas[idx] += sectors;
369 static inline int update_replicas(struct bch_fs *c, struct bkey_s_c k,
370 struct bch_replicas_entry *r, s64 sectors,
371 unsigned journal_seq, bool gc)
373 struct bch_fs_usage __percpu *fs_usage;
375 struct printbuf buf = PRINTBUF;
377 percpu_down_read(&c->mark_lock);
380 idx = bch2_replicas_entry_idx(c, r);
382 fsck_err(c, "no replicas entry\n"
384 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
385 percpu_up_read(&c->mark_lock);
386 ret = bch2_mark_replicas(c, r);
387 percpu_down_read(&c->mark_lock);
391 idx = bch2_replicas_entry_idx(c, r);
399 fs_usage = fs_usage_ptr(c, journal_seq, gc);
400 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
401 fs_usage->replicas[idx] += sectors;
405 percpu_up_read(&c->mark_lock);
410 static inline int update_cached_sectors(struct bch_fs *c,
412 unsigned dev, s64 sectors,
413 unsigned journal_seq, bool gc)
415 struct bch_replicas_padded r;
417 bch2_replicas_entry_cached(&r.e, dev);
419 return update_replicas(c, k, &r.e, sectors, journal_seq, gc);
422 static struct replicas_delta_list *
423 replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
425 struct replicas_delta_list *d = trans->fs_usage_deltas;
426 unsigned new_size = d ? (d->size + more) * 2 : 128;
427 unsigned alloc_size = sizeof(*d) + new_size;
429 WARN_ON_ONCE(alloc_size > REPLICAS_DELTA_LIST_MAX);
431 if (!d || d->used + more > d->size) {
432 d = krealloc(d, alloc_size, GFP_NOIO|__GFP_ZERO);
434 BUG_ON(!d && alloc_size > REPLICAS_DELTA_LIST_MAX);
437 d = mempool_alloc(&trans->c->replicas_delta_pool, GFP_NOIO);
438 memset(d, 0, REPLICAS_DELTA_LIST_MAX);
440 if (trans->fs_usage_deltas)
441 memcpy(d, trans->fs_usage_deltas,
442 trans->fs_usage_deltas->size + sizeof(*d));
444 new_size = REPLICAS_DELTA_LIST_MAX - sizeof(*d);
445 kfree(trans->fs_usage_deltas);
449 trans->fs_usage_deltas = d;
454 static inline void update_replicas_list(struct btree_trans *trans,
455 struct bch_replicas_entry *r,
458 struct replicas_delta_list *d;
459 struct replicas_delta *n;
465 b = replicas_entry_bytes(r) + 8;
466 d = replicas_deltas_realloc(trans, b);
468 n = (void *) d->d + d->used;
470 memcpy((void *) n + offsetof(struct replicas_delta, r),
471 r, replicas_entry_bytes(r));
472 bch2_replicas_entry_sort(&n->r);
476 static inline void update_cached_sectors_list(struct btree_trans *trans,
477 unsigned dev, s64 sectors)
479 struct bch_replicas_padded r;
481 bch2_replicas_entry_cached(&r.e, dev);
483 update_replicas_list(trans, &r.e, sectors);
486 int bch2_mark_alloc(struct btree_trans *trans,
487 struct bkey_s_c old, struct bkey_s_c new,
490 bool gc = flags & BTREE_TRIGGER_GC;
491 u64 journal_seq = trans->journal_res.seq;
492 struct bch_fs *c = trans->c;
493 struct bch_alloc_v4 old_a, new_a;
498 * alloc btree is read in by bch2_alloc_read, not gc:
500 if ((flags & BTREE_TRIGGER_GC) &&
501 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
504 if (bch2_trans_inconsistent_on(!bch2_dev_bucket_exists(c, new.k->p), trans,
505 "alloc key for invalid device or bucket"))
508 ca = bch_dev_bkey_exists(c, new.k->p.inode);
510 bch2_alloc_to_v4(old, &old_a);
511 bch2_alloc_to_v4(new, &new_a);
513 if ((flags & BTREE_TRIGGER_INSERT) &&
514 data_type_is_empty(old_a.data_type) !=
515 data_type_is_empty(new_a.data_type) &&
516 new.k->type == KEY_TYPE_alloc_v4) {
517 struct bch_alloc_v4 *v = (struct bch_alloc_v4 *) new.v;
519 BUG_ON(!journal_seq);
522 * If the btree updates referring to a bucket weren't flushed
523 * before the bucket became empty again, then the we don't have
524 * to wait on a journal flush before we can reuse the bucket:
526 new_a.journal_seq = data_type_is_empty(new_a.data_type) &&
527 (journal_seq == v->journal_seq ||
528 bch2_journal_noflush_seq(&c->journal, v->journal_seq))
530 v->journal_seq = new_a.journal_seq;
533 if (!data_type_is_empty(old_a.data_type) &&
534 data_type_is_empty(new_a.data_type) &&
536 ret = bch2_set_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
537 c->journal.flushed_seq_ondisk,
538 new.k->p.inode, new.k->p.offset,
541 bch2_fs_fatal_error(c,
542 "error setting bucket_needs_journal_commit: %i", ret);
547 if (new_a.data_type == BCH_DATA_free &&
548 (!new_a.journal_seq || new_a.journal_seq < c->journal.flushed_seq_ondisk))
549 closure_wake_up(&c->freelist_wait);
551 if (new_a.data_type == BCH_DATA_need_discard &&
552 (!new_a.journal_seq || new_a.journal_seq < c->journal.flushed_seq_ondisk))
555 if (old_a.data_type != BCH_DATA_cached &&
556 new_a.data_type == BCH_DATA_cached &&
557 should_invalidate_buckets(ca, bch2_dev_usage_read(ca)))
558 bch2_do_invalidates(c);
560 if (new_a.data_type == BCH_DATA_need_gc_gens)
563 percpu_down_read(&c->mark_lock);
564 if (!gc && new_a.gen != old_a.gen)
565 *bucket_gen(ca, new.k->p.offset) = new_a.gen;
567 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
570 struct bucket *g = gc_bucket(ca, new.k->p.offset);
576 g->data_type = new_a.data_type;
577 g->stripe = new_a.stripe;
578 g->stripe_redundancy = new_a.stripe_redundancy;
579 g->dirty_sectors = new_a.dirty_sectors;
580 g->cached_sectors = new_a.cached_sectors;
584 percpu_up_read(&c->mark_lock);
587 * need to know if we're getting called from the invalidate path or
591 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
592 old_a.cached_sectors) {
593 ret = update_cached_sectors(c, new, ca->dev_idx,
594 -old_a.cached_sectors,
597 bch2_fs_fatal_error(c, "bch2_mark_alloc(): no replicas entry while updating cached sectors");
605 int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
606 size_t b, enum bch_data_type data_type,
607 unsigned sectors, struct gc_pos pos,
610 struct bucket old, new, *g;
613 BUG_ON(!(flags & BTREE_TRIGGER_GC));
614 BUG_ON(data_type != BCH_DATA_sb &&
615 data_type != BCH_DATA_journal);
618 * Backup superblock might be past the end of our normal usable space:
620 if (b >= ca->mi.nbuckets)
623 percpu_down_read(&c->mark_lock);
624 g = gc_bucket(ca, b);
629 if (bch2_fs_inconsistent_on(g->data_type &&
630 g->data_type != data_type, c,
631 "different types of data in same bucket: %s, %s",
632 bch2_data_types[g->data_type],
633 bch2_data_types[data_type])) {
638 if (bch2_fs_inconsistent_on((u64) g->dirty_sectors + sectors > ca->mi.bucket_size, c,
639 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > bucket size",
640 ca->dev_idx, b, g->gen,
641 bch2_data_types[g->data_type ?: data_type],
642 g->dirty_sectors, sectors)) {
648 g->data_type = data_type;
649 g->dirty_sectors += sectors;
654 bch2_dev_usage_update_m(c, ca, old, new, 0, true);
655 percpu_up_read(&c->mark_lock);
659 static int check_bucket_ref(struct bch_fs *c,
661 const struct bch_extent_ptr *ptr,
662 s64 sectors, enum bch_data_type ptr_data_type,
663 u8 b_gen, u8 bucket_data_type,
664 u32 dirty_sectors, u32 cached_sectors)
666 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
667 size_t bucket_nr = PTR_BUCKET_NR(ca, ptr);
668 u16 bucket_sectors = !ptr->cached
671 struct printbuf buf = PRINTBUF;
674 if (bucket_data_type == BCH_DATA_cached)
675 bucket_data_type = BCH_DATA_user;
677 if (gen_after(ptr->gen, b_gen)) {
678 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
679 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
681 ptr->dev, bucket_nr, b_gen,
682 bch2_data_types[bucket_data_type ?: ptr_data_type],
684 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
689 if (gen_cmp(b_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
690 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
691 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
693 ptr->dev, bucket_nr, b_gen,
694 bch2_data_types[bucket_data_type ?: ptr_data_type],
696 (printbuf_reset(&buf),
697 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
702 if (b_gen != ptr->gen && !ptr->cached) {
703 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
704 "bucket %u:%zu gen %u (mem gen %u) data type %s: stale dirty ptr (gen %u)\n"
706 ptr->dev, bucket_nr, b_gen,
707 *bucket_gen(ca, bucket_nr),
708 bch2_data_types[bucket_data_type ?: ptr_data_type],
710 (printbuf_reset(&buf),
711 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
716 if (b_gen != ptr->gen) {
721 if (!data_type_is_empty(bucket_data_type) &&
723 bucket_data_type != ptr_data_type) {
724 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
725 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
727 ptr->dev, bucket_nr, b_gen,
728 bch2_data_types[bucket_data_type],
729 bch2_data_types[ptr_data_type],
730 (printbuf_reset(&buf),
731 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
736 if ((unsigned) (bucket_sectors + sectors) > U32_MAX) {
737 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
738 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
740 ptr->dev, bucket_nr, b_gen,
741 bch2_data_types[bucket_data_type ?: ptr_data_type],
742 bucket_sectors, sectors,
743 (printbuf_reset(&buf),
744 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
753 static int mark_stripe_bucket(struct btree_trans *trans,
758 struct bch_fs *c = trans->c;
759 u64 journal_seq = trans->journal_res.seq;
760 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
761 unsigned nr_data = s->nr_blocks - s->nr_redundant;
762 bool parity = ptr_idx >= nr_data;
763 enum bch_data_type data_type = parity ? BCH_DATA_parity : 0;
764 s64 sectors = parity ? le16_to_cpu(s->sectors) : 0;
765 const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
766 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
767 struct bucket old, new, *g;
768 struct printbuf buf = PRINTBUF;
771 BUG_ON(!(flags & BTREE_TRIGGER_GC));
773 /* * XXX doesn't handle deletion */
775 percpu_down_read(&c->mark_lock);
777 g = PTR_GC_BUCKET(ca, ptr);
779 if (g->dirty_sectors ||
780 (g->stripe && g->stripe != k.k->p.offset)) {
781 bch2_fs_inconsistent(c,
782 "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
783 ptr->dev, PTR_BUCKET_NR(ca, ptr), g->gen,
784 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
792 ret = check_bucket_ref(c, k, ptr, sectors, data_type,
793 g->gen, g->data_type,
794 g->dirty_sectors, g->cached_sectors);
799 g->data_type = data_type;
800 g->dirty_sectors += sectors;
802 g->stripe = k.k->p.offset;
803 g->stripe_redundancy = s->nr_redundant;
808 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
809 percpu_up_read(&c->mark_lock);
814 static int __mark_pointer(struct btree_trans *trans,
816 const struct bch_extent_ptr *ptr,
817 s64 sectors, enum bch_data_type ptr_data_type,
818 u8 bucket_gen, u8 *bucket_data_type,
819 u32 *dirty_sectors, u32 *cached_sectors)
821 u32 *dst_sectors = !ptr->cached
824 int ret = check_bucket_ref(trans->c, k, ptr, sectors, ptr_data_type,
825 bucket_gen, *bucket_data_type,
826 *dirty_sectors, *cached_sectors);
831 *dst_sectors += sectors;
832 *bucket_data_type = *dirty_sectors || *cached_sectors
837 static int bch2_mark_pointer(struct btree_trans *trans,
839 struct extent_ptr_decoded p,
840 s64 sectors, enum bch_data_type data_type,
843 u64 journal_seq = trans->journal_res.seq;
844 struct bch_fs *c = trans->c;
845 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
846 struct bucket old, new, *g;
850 BUG_ON(!(flags & BTREE_TRIGGER_GC));
852 percpu_down_read(&c->mark_lock);
853 g = PTR_GC_BUCKET(ca, &p.ptr);
857 bucket_data_type = g->data_type;
858 ret = __mark_pointer(trans, k, &p.ptr, sectors,
864 g->data_type = bucket_data_type;
869 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
870 percpu_up_read(&c->mark_lock);
875 static int bch2_mark_stripe_ptr(struct btree_trans *trans,
877 struct bch_extent_stripe_ptr p,
878 enum bch_data_type data_type,
882 struct bch_fs *c = trans->c;
883 struct bch_replicas_padded r;
886 BUG_ON(!(flags & BTREE_TRIGGER_GC));
888 m = genradix_ptr_alloc(&c->gc_stripes, p.idx, GFP_KERNEL);
890 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
895 spin_lock(&c->ec_stripes_heap_lock);
897 if (!m || !m->alive) {
898 spin_unlock(&c->ec_stripes_heap_lock);
899 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
901 bch2_inconsistent_error(c);
905 m->block_sectors[p.block] += sectors;
908 spin_unlock(&c->ec_stripes_heap_lock);
910 r.e.data_type = data_type;
911 update_replicas(c, k, &r.e, sectors, trans->journal_res.seq, true);
916 int bch2_mark_extent(struct btree_trans *trans,
917 struct bkey_s_c old, struct bkey_s_c new,
920 u64 journal_seq = trans->journal_res.seq;
921 struct bch_fs *c = trans->c;
922 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
923 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
924 const union bch_extent_entry *entry;
925 struct extent_ptr_decoded p;
926 struct bch_replicas_padded r;
927 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
930 s64 sectors = bkey_is_btree_ptr(k.k)
933 s64 dirty_sectors = 0;
937 BUG_ON(!(flags & BTREE_TRIGGER_GC));
939 r.e.data_type = data_type;
943 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
944 s64 disk_sectors = ptr_disk_sectors(sectors, p);
946 if (flags & BTREE_TRIGGER_OVERWRITE)
947 disk_sectors = -disk_sectors;
949 ret = bch2_mark_pointer(trans, k, p, disk_sectors,
958 ret = update_cached_sectors(c, k, p.ptr.dev,
959 disk_sectors, journal_seq, true);
961 bch2_fs_fatal_error(c, "bch2_mark_extent(): no replicas entry while updating cached sectors");
965 } else if (!p.has_ec) {
966 dirty_sectors += disk_sectors;
967 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
969 ret = bch2_mark_stripe_ptr(trans, k, p.ec, data_type,
970 disk_sectors, flags);
975 * There may be other dirty pointers in this extent, but
976 * if so they're not required for mounting if we have an
977 * erasure coded pointer in this extent:
984 ret = update_replicas(c, k, &r.e, dirty_sectors, journal_seq, true);
986 struct printbuf buf = PRINTBUF;
988 bch2_bkey_val_to_text(&buf, c, k);
989 bch2_fs_fatal_error(c, "no replicas entry for %s", buf.buf);
998 int bch2_mark_stripe(struct btree_trans *trans,
999 struct bkey_s_c old, struct bkey_s_c new,
1002 bool gc = flags & BTREE_TRIGGER_GC;
1003 u64 journal_seq = trans->journal_res.seq;
1004 struct bch_fs *c = trans->c;
1005 u64 idx = new.k->p.offset;
1006 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
1007 ? bkey_s_c_to_stripe(old).v : NULL;
1008 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
1009 ? bkey_s_c_to_stripe(new).v : NULL;
1013 BUG_ON(gc && old_s);
1016 struct stripe *m = genradix_ptr(&c->stripes, idx);
1018 if (!m || (old_s && !m->alive)) {
1019 struct printbuf buf1 = PRINTBUF;
1020 struct printbuf buf2 = PRINTBUF;
1022 bch2_bkey_val_to_text(&buf1, c, old);
1023 bch2_bkey_val_to_text(&buf2, c, new);
1024 bch_err_ratelimited(c, "error marking nonexistent stripe %llu while marking\n"
1026 "new %s", idx, buf1.buf, buf2.buf);
1027 printbuf_exit(&buf2);
1028 printbuf_exit(&buf1);
1029 bch2_inconsistent_error(c);
1034 spin_lock(&c->ec_stripes_heap_lock);
1035 bch2_stripes_heap_del(c, m, idx);
1036 spin_unlock(&c->ec_stripes_heap_lock);
1038 memset(m, 0, sizeof(*m));
1041 m->sectors = le16_to_cpu(new_s->sectors);
1042 m->algorithm = new_s->algorithm;
1043 m->nr_blocks = new_s->nr_blocks;
1044 m->nr_redundant = new_s->nr_redundant;
1045 m->blocks_nonempty = 0;
1047 for (i = 0; i < new_s->nr_blocks; i++)
1048 m->blocks_nonempty += !!stripe_blockcount_get(new_s, i);
1050 spin_lock(&c->ec_stripes_heap_lock);
1051 bch2_stripes_heap_update(c, m, idx);
1052 spin_unlock(&c->ec_stripes_heap_lock);
1055 struct gc_stripe *m =
1056 genradix_ptr_alloc(&c->gc_stripes, idx, GFP_KERNEL);
1059 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
1064 * This will be wrong when we bring back runtime gc: we should
1065 * be unmarking the old key and then marking the new key
1068 m->sectors = le16_to_cpu(new_s->sectors);
1069 m->nr_blocks = new_s->nr_blocks;
1070 m->nr_redundant = new_s->nr_redundant;
1072 for (i = 0; i < new_s->nr_blocks; i++)
1073 m->ptrs[i] = new_s->ptrs[i];
1075 bch2_bkey_to_replicas(&m->r.e, new);
1078 * gc recalculates this field from stripe ptr
1081 memset(m->block_sectors, 0, sizeof(m->block_sectors));
1083 for (i = 0; i < new_s->nr_blocks; i++) {
1084 ret = mark_stripe_bucket(trans, new, i, flags);
1089 ret = update_replicas(c, new, &m->r.e,
1090 ((s64) m->sectors * m->nr_redundant),
1093 struct printbuf buf = PRINTBUF;
1095 bch2_bkey_val_to_text(&buf, c, new);
1096 bch2_fs_fatal_error(c, "no replicas entry for %s", buf.buf);
1097 printbuf_exit(&buf);
1105 int bch2_mark_inode(struct btree_trans *trans,
1106 struct bkey_s_c old, struct bkey_s_c new,
1109 struct bch_fs *c = trans->c;
1110 struct bch_fs_usage __percpu *fs_usage;
1111 u64 journal_seq = trans->journal_res.seq;
1113 if (flags & BTREE_TRIGGER_INSERT) {
1114 struct bch_inode_v2 *v = (struct bch_inode_v2 *) new.v;
1116 BUG_ON(!journal_seq);
1117 BUG_ON(new.k->type != KEY_TYPE_inode_v2);
1119 v->bi_journal_seq = cpu_to_le64(journal_seq);
1122 if (flags & BTREE_TRIGGER_GC) {
1123 percpu_down_read(&c->mark_lock);
1126 fs_usage = fs_usage_ptr(c, journal_seq, flags & BTREE_TRIGGER_GC);
1127 fs_usage->nr_inodes += bkey_is_inode(new.k);
1128 fs_usage->nr_inodes -= bkey_is_inode(old.k);
1131 percpu_up_read(&c->mark_lock);
1136 int bch2_mark_reservation(struct btree_trans *trans,
1137 struct bkey_s_c old, struct bkey_s_c new,
1140 struct bch_fs *c = trans->c;
1141 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
1142 struct bch_fs_usage __percpu *fs_usage;
1143 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1144 s64 sectors = (s64) k.k->size;
1146 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1148 if (flags & BTREE_TRIGGER_OVERWRITE)
1150 sectors *= replicas;
1152 percpu_down_read(&c->mark_lock);
1155 fs_usage = fs_usage_ptr(c, trans->journal_res.seq, flags & BTREE_TRIGGER_GC);
1156 replicas = clamp_t(unsigned, replicas, 1,
1157 ARRAY_SIZE(fs_usage->persistent_reserved));
1159 fs_usage->reserved += sectors;
1160 fs_usage->persistent_reserved[replicas - 1] += sectors;
1163 percpu_up_read(&c->mark_lock);
1168 static s64 __bch2_mark_reflink_p(struct btree_trans *trans,
1169 struct bkey_s_c_reflink_p p,
1171 u64 *idx, unsigned flags, size_t r_idx)
1173 struct bch_fs *c = trans->c;
1174 struct reflink_gc *r;
1175 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1178 struct printbuf buf = PRINTBUF;
1180 if (r_idx >= c->reflink_gc_nr)
1183 r = genradix_ptr(&c->reflink_gc_table, r_idx);
1184 next_idx = min(next_idx, r->offset - r->size);
1185 if (*idx < next_idx)
1188 BUG_ON((s64) r->refcount + add < 0);
1194 if (fsck_err(c, "pointer to missing indirect extent\n"
1196 " missing range %llu-%llu",
1197 (bch2_bkey_val_to_text(&buf, c, p.s_c), buf.buf),
1199 struct bkey_i_error new;
1202 new.k.type = KEY_TYPE_error;
1203 new.k.p = bkey_start_pos(p.k);
1204 new.k.p.offset += *idx - start;
1205 bch2_key_resize(&new.k, next_idx - *idx);
1206 ret = __bch2_btree_insert(trans, BTREE_ID_extents, &new.k_i);
1211 printbuf_exit(&buf);
1215 int bch2_mark_reflink_p(struct btree_trans *trans,
1216 struct bkey_s_c old, struct bkey_s_c new,
1219 struct bch_fs *c = trans->c;
1220 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
1221 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1222 struct reflink_gc *ref;
1224 u64 idx = le64_to_cpu(p.v->idx), start = idx;
1225 u64 end = le64_to_cpu(p.v->idx) + p.k->size;
1228 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1230 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix) {
1231 idx -= le32_to_cpu(p.v->front_pad);
1232 end += le32_to_cpu(p.v->back_pad);
1236 r = c->reflink_gc_nr;
1238 m = l + (r - l) / 2;
1240 ref = genradix_ptr(&c->reflink_gc_table, m);
1241 if (ref->offset <= idx)
1247 while (idx < end && !ret)
1248 ret = __bch2_mark_reflink_p(trans, p, start, end,
1254 static noinline __cold
1255 void fs_usage_apply_warn(struct btree_trans *trans,
1256 unsigned disk_res_sectors,
1257 s64 should_not_have_added)
1259 struct bch_fs *c = trans->c;
1260 struct btree_insert_entry *i;
1261 struct printbuf buf = PRINTBUF;
1263 bch_err(c, "disk usage increased %lli more than %u sectors reserved",
1264 should_not_have_added, disk_res_sectors);
1266 trans_for_each_update(trans, i) {
1267 struct bkey_s_c old = { &i->old_k, i->old_v };
1269 pr_err("while inserting");
1270 printbuf_reset(&buf);
1271 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(i->k));
1272 pr_err(" %s", buf.buf);
1273 pr_err("overlapping with");
1274 printbuf_reset(&buf);
1275 bch2_bkey_val_to_text(&buf, c, old);
1276 pr_err(" %s", buf.buf);
1280 printbuf_exit(&buf);
1283 int bch2_trans_fs_usage_apply(struct btree_trans *trans,
1284 struct replicas_delta_list *deltas)
1286 struct bch_fs *c = trans->c;
1287 static int warned_disk_usage = 0;
1289 unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1290 struct replicas_delta *d = deltas->d, *d2;
1291 struct replicas_delta *top = (void *) deltas->d + deltas->used;
1292 struct bch_fs_usage *dst;
1293 s64 added = 0, should_not_have_added;
1296 percpu_down_read(&c->mark_lock);
1298 dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1300 for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1301 switch (d->r.data_type) {
1302 case BCH_DATA_btree:
1304 case BCH_DATA_parity:
1308 if (__update_replicas(c, dst, &d->r, d->delta))
1312 dst->nr_inodes += deltas->nr_inodes;
1314 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1315 added += deltas->persistent_reserved[i];
1316 dst->reserved += deltas->persistent_reserved[i];
1317 dst->persistent_reserved[i] += deltas->persistent_reserved[i];
1321 * Not allowed to reduce sectors_available except by getting a
1324 should_not_have_added = added - (s64) disk_res_sectors;
1325 if (unlikely(should_not_have_added > 0)) {
1326 u64 old, new, v = atomic64_read(&c->sectors_available);
1330 new = max_t(s64, 0, old - should_not_have_added);
1331 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1334 added -= should_not_have_added;
1339 trans->disk_res->sectors -= added;
1340 this_cpu_sub(*c->online_reserved, added);
1344 percpu_up_read(&c->mark_lock);
1346 if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
1347 fs_usage_apply_warn(trans, disk_res_sectors, should_not_have_added);
1350 /* revert changes: */
1351 for (d2 = deltas->d; d2 != d; d2 = replicas_delta_next(d2))
1352 BUG_ON(__update_replicas(c, dst, &d2->r, -d2->delta));
1355 percpu_up_read(&c->mark_lock);
1361 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1362 enum btree_id btree_id, unsigned level,
1363 struct bkey_s_c k, struct extent_ptr_decoded p,
1366 bool insert = !(flags & BTREE_TRIGGER_OVERWRITE);
1367 struct btree_iter iter;
1368 struct bkey_i_alloc_v4 *a;
1369 struct bpos bucket_pos;
1370 struct bch_backpointer bp;
1374 bch2_extent_ptr_to_bp(trans->c, btree_id, level, k, p, &bucket_pos, &bp);
1375 sectors = bp.bucket_len;
1379 a = bch2_trans_start_alloc_update(trans, &iter, bucket_pos);
1383 ret = __mark_pointer(trans, k, &p.ptr, sectors, bp.data_type,
1384 a->v.gen, &a->v.data_type,
1385 &a->v.dirty_sectors, &a->v.cached_sectors);
1389 if (!p.ptr.cached) {
1391 ? bch2_bucket_backpointer_add(trans, a, bp, k)
1392 : bch2_bucket_backpointer_del(trans, a, bp, k);
1397 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1399 bch2_trans_iter_exit(trans, &iter);
1403 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1404 struct extent_ptr_decoded p,
1405 s64 sectors, enum bch_data_type data_type)
1407 struct btree_iter iter;
1409 struct bkey_i_stripe *s;
1410 struct bch_replicas_padded r;
1413 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, POS(0, p.ec.idx),
1415 BTREE_ITER_WITH_UPDATES);
1416 k = bch2_btree_iter_peek_slot(&iter);
1421 if (k.k->type != KEY_TYPE_stripe) {
1422 bch2_trans_inconsistent(trans,
1423 "pointer to nonexistent stripe %llu",
1429 if (!bch2_ptr_matches_stripe(bkey_s_c_to_stripe(k).v, p)) {
1430 bch2_trans_inconsistent(trans,
1431 "stripe pointer doesn't match stripe %llu",
1437 s = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1438 ret = PTR_ERR_OR_ZERO(s);
1442 bkey_reassemble(&s->k_i, k);
1443 stripe_blockcount_set(&s->v, p.ec.block,
1444 stripe_blockcount_get(&s->v, p.ec.block) +
1447 ret = bch2_trans_update(trans, &iter, &s->k_i, 0);
1451 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1452 r.e.data_type = data_type;
1453 update_replicas_list(trans, &r.e, sectors);
1455 bch2_trans_iter_exit(trans, &iter);
1459 int bch2_trans_mark_extent(struct btree_trans *trans,
1460 enum btree_id btree_id, unsigned level,
1461 struct bkey_s_c old, struct bkey_i *new,
1464 struct bch_fs *c = trans->c;
1465 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1467 : bkey_i_to_s_c(new);
1468 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1469 const union bch_extent_entry *entry;
1470 struct extent_ptr_decoded p;
1471 struct bch_replicas_padded r;
1472 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
1475 s64 sectors = bkey_is_btree_ptr(k.k)
1478 s64 dirty_sectors = 0;
1482 r.e.data_type = data_type;
1484 r.e.nr_required = 1;
1486 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1487 s64 disk_sectors = ptr_disk_sectors(sectors, p);
1489 if (flags & BTREE_TRIGGER_OVERWRITE)
1490 disk_sectors = -disk_sectors;
1492 ret = bch2_trans_mark_pointer(trans, btree_id, level, k, p, flags);
1500 update_cached_sectors_list(trans, p.ptr.dev,
1502 } else if (!p.has_ec) {
1503 dirty_sectors += disk_sectors;
1504 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1506 ret = bch2_trans_mark_stripe_ptr(trans, p,
1507 disk_sectors, data_type);
1511 r.e.nr_required = 0;
1516 update_replicas_list(trans, &r.e, dirty_sectors);
1521 static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
1522 struct bkey_s_c_stripe s,
1523 unsigned idx, bool deleting)
1525 struct bch_fs *c = trans->c;
1526 const struct bch_extent_ptr *ptr = &s.v->ptrs[idx];
1527 struct btree_iter iter;
1528 struct bkey_i_alloc_v4 *a;
1529 enum bch_data_type data_type = idx >= s.v->nr_blocks - s.v->nr_redundant
1530 ? BCH_DATA_parity : 0;
1531 s64 sectors = data_type ? le16_to_cpu(s.v->sectors) : 0;
1537 a = bch2_trans_start_alloc_update(trans, &iter, PTR_BUCKET_POS(c, ptr));
1541 ret = check_bucket_ref(c, s.s_c, ptr, sectors, data_type,
1542 a->v.gen, a->v.data_type,
1543 a->v.dirty_sectors, a->v.cached_sectors);
1548 if (bch2_trans_inconsistent_on(a->v.stripe ||
1549 a->v.stripe_redundancy, trans,
1550 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)",
1551 iter.pos.inode, iter.pos.offset, a->v.gen,
1552 bch2_data_types[a->v.data_type],
1554 a->v.stripe, s.k->p.offset)) {
1559 if (bch2_trans_inconsistent_on(data_type && a->v.dirty_sectors, trans,
1560 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: data already in stripe bucket %llu",
1561 iter.pos.inode, iter.pos.offset, a->v.gen,
1562 bch2_data_types[a->v.data_type],
1569 a->v.stripe = s.k->p.offset;
1570 a->v.stripe_redundancy = s.v->nr_redundant;
1572 if (bch2_trans_inconsistent_on(a->v.stripe != s.k->p.offset ||
1573 a->v.stripe_redundancy != s.v->nr_redundant, trans,
1574 "bucket %llu:%llu gen %u: not marked as stripe when deleting stripe %llu (got %u)",
1575 iter.pos.inode, iter.pos.offset, a->v.gen,
1576 s.k->p.offset, a->v.stripe)) {
1582 a->v.stripe_redundancy = 0;
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 update_replicas_list(trans, &r.e, sectors * new_s->nr_redundant);
1637 s64 sectors = -((s64) le16_to_cpu(old_s->sectors));
1639 bch2_bkey_to_replicas(&r.e, old);
1640 update_replicas_list(trans, &r.e, sectors * old_s->nr_redundant);
1643 for (i = 0; i < nr_blocks; i++) {
1644 if (new_s && old_s &&
1645 !memcmp(&new_s->ptrs[i],
1647 sizeof(new_s->ptrs[i])))
1651 ret = bch2_trans_mark_stripe_bucket(trans,
1652 bkey_i_to_s_c_stripe(new), i, false);
1658 ret = bch2_trans_mark_stripe_bucket(trans,
1659 bkey_s_c_to_stripe(old), i, true);
1668 int bch2_trans_mark_inode(struct btree_trans *trans,
1669 enum btree_id btree_id, unsigned level,
1670 struct bkey_s_c old,
1674 int nr = bkey_is_inode(&new->k) - bkey_is_inode(old.k);
1677 struct replicas_delta_list *d =
1678 replicas_deltas_realloc(trans, 0);
1685 int bch2_trans_mark_reservation(struct btree_trans *trans,
1686 enum btree_id btree_id, unsigned level,
1687 struct bkey_s_c old,
1691 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1693 : bkey_i_to_s_c(new);
1694 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1695 s64 sectors = (s64) k.k->size;
1696 struct replicas_delta_list *d;
1698 if (flags & BTREE_TRIGGER_OVERWRITE)
1700 sectors *= replicas;
1702 d = replicas_deltas_realloc(trans, 0);
1704 replicas = clamp_t(unsigned, replicas, 1,
1705 ARRAY_SIZE(d->persistent_reserved));
1707 d->persistent_reserved[replicas - 1] += sectors;
1711 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1712 struct bkey_s_c_reflink_p p,
1713 u64 *idx, unsigned flags)
1715 struct bch_fs *c = trans->c;
1716 struct btree_iter iter;
1720 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1721 struct printbuf buf = PRINTBUF;
1724 bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink, POS(0, *idx),
1726 BTREE_ITER_WITH_UPDATES);
1727 k = bch2_btree_iter_peek_slot(&iter);
1732 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1733 ret = PTR_ERR_OR_ZERO(n);
1737 bkey_reassemble(n, k);
1739 refcount = bkey_refcount(n);
1741 bch2_bkey_val_to_text(&buf, c, p.s_c);
1742 bch2_trans_inconsistent(trans,
1743 "nonexistent indirect extent at %llu while marking\n %s",
1749 if (!*refcount && (flags & BTREE_TRIGGER_OVERWRITE)) {
1750 bch2_bkey_val_to_text(&buf, c, p.s_c);
1751 bch2_trans_inconsistent(trans,
1752 "indirect extent refcount underflow at %llu while marking\n %s",
1758 if (flags & BTREE_TRIGGER_INSERT) {
1759 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1762 pad = max_t(s64, le32_to_cpu(v->front_pad),
1763 le64_to_cpu(v->idx) - bkey_start_offset(k.k));
1764 BUG_ON(pad > U32_MAX);
1765 v->front_pad = cpu_to_le32(pad);
1767 pad = max_t(s64, le32_to_cpu(v->back_pad),
1768 k.k->p.offset - p.k->size - le64_to_cpu(v->idx));
1769 BUG_ON(pad > U32_MAX);
1770 v->back_pad = cpu_to_le32(pad);
1773 le64_add_cpu(refcount, add);
1775 bch2_btree_iter_set_pos_to_extent_start(&iter);
1776 ret = bch2_trans_update(trans, &iter, n, 0);
1780 *idx = k.k->p.offset;
1782 bch2_trans_iter_exit(trans, &iter);
1783 printbuf_exit(&buf);
1787 int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1788 enum btree_id btree_id, unsigned level,
1789 struct bkey_s_c old,
1793 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1795 : bkey_i_to_s_c(new);
1796 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1800 if (flags & BTREE_TRIGGER_INSERT) {
1801 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1803 v->front_pad = v->back_pad = 0;
1806 idx = le64_to_cpu(p.v->idx) - le32_to_cpu(p.v->front_pad);
1807 end_idx = le64_to_cpu(p.v->idx) + p.k->size +
1808 le32_to_cpu(p.v->back_pad);
1810 while (idx < end_idx && !ret)
1811 ret = __bch2_trans_mark_reflink_p(trans, p, &idx, flags);
1816 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1817 struct bch_dev *ca, size_t b,
1818 enum bch_data_type type,
1821 struct bch_fs *c = trans->c;
1822 struct btree_iter iter;
1823 struct bkey_i_alloc_v4 *a;
1827 * Backup superblock might be past the end of our normal usable space:
1829 if (b >= ca->mi.nbuckets)
1832 a = bch2_trans_start_alloc_update(trans, &iter, POS(ca->dev_idx, b));
1836 if (a->v.data_type && a->v.data_type != type) {
1837 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1838 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
1840 iter.pos.inode, iter.pos.offset, a->v.gen,
1841 bch2_data_types[a->v.data_type],
1842 bch2_data_types[type],
1843 bch2_data_types[type]);
1848 a->v.data_type = type;
1849 a->v.dirty_sectors = sectors;
1851 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1855 bch2_trans_iter_exit(trans, &iter);
1859 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1860 struct bch_dev *ca, size_t b,
1861 enum bch_data_type type,
1864 return commit_do(trans, NULL, NULL, 0,
1865 __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors));
1868 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans,
1871 enum bch_data_type type,
1872 u64 *bucket, unsigned *bucket_sectors)
1875 u64 b = sector_to_bucket(ca, start);
1877 min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
1879 if (b != *bucket && *bucket_sectors) {
1880 int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket,
1881 type, *bucket_sectors);
1885 *bucket_sectors = 0;
1889 *bucket_sectors += sectors;
1891 } while (start < end);
1896 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
1899 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
1901 unsigned i, bucket_sectors = 0;
1904 for (i = 0; i < layout->nr_superblocks; i++) {
1905 u64 offset = le64_to_cpu(layout->sb_offset[i]);
1907 if (offset == BCH_SB_SECTOR) {
1908 ret = bch2_trans_mark_metadata_sectors(trans, ca,
1910 BCH_DATA_sb, &bucket, &bucket_sectors);
1915 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset,
1916 offset + (1 << layout->sb_max_size_bits),
1917 BCH_DATA_sb, &bucket, &bucket_sectors);
1922 if (bucket_sectors) {
1923 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1924 bucket, BCH_DATA_sb, bucket_sectors);
1929 for (i = 0; i < ca->journal.nr; i++) {
1930 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1931 ca->journal.buckets[i],
1932 BCH_DATA_journal, ca->mi.bucket_size);
1940 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
1942 return bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
1943 __bch2_trans_mark_dev_sb(&trans, ca));
1946 /* Disk reservations: */
1948 #define SECTORS_CACHE 1024
1950 int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
1951 u64 sectors, int flags)
1953 struct bch_fs_pcpu *pcpu;
1955 s64 sectors_available;
1958 percpu_down_read(&c->mark_lock);
1960 pcpu = this_cpu_ptr(c->pcpu);
1962 if (sectors <= pcpu->sectors_available)
1965 v = atomic64_read(&c->sectors_available);
1968 get = min((u64) sectors + SECTORS_CACHE, old);
1970 if (get < sectors) {
1974 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1975 old, old - get)) != old);
1977 pcpu->sectors_available += get;
1980 pcpu->sectors_available -= sectors;
1981 this_cpu_add(*c->online_reserved, sectors);
1982 res->sectors += sectors;
1985 percpu_up_read(&c->mark_lock);
1989 mutex_lock(&c->sectors_available_lock);
1991 percpu_u64_set(&c->pcpu->sectors_available, 0);
1992 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
1994 if (sectors <= sectors_available ||
1995 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
1996 atomic64_set(&c->sectors_available,
1997 max_t(s64, 0, sectors_available - sectors));
1998 this_cpu_add(*c->online_reserved, sectors);
1999 res->sectors += sectors;
2002 atomic64_set(&c->sectors_available, sectors_available);
2006 mutex_unlock(&c->sectors_available_lock);
2007 percpu_up_read(&c->mark_lock);
2012 /* Startup/shutdown: */
2014 static void bucket_gens_free_rcu(struct rcu_head *rcu)
2016 struct bucket_gens *buckets =
2017 container_of(rcu, struct bucket_gens, rcu);
2019 kvpfree(buckets, sizeof(*buckets) + buckets->nbuckets);
2022 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
2024 struct bucket_gens *bucket_gens = NULL, *old_bucket_gens = NULL;
2025 unsigned long *buckets_nouse = NULL;
2026 bool resize = ca->bucket_gens != NULL;
2029 if (!(bucket_gens = kvpmalloc(sizeof(struct bucket_gens) + nbuckets,
2030 GFP_KERNEL|__GFP_ZERO)) ||
2031 (c->opts.buckets_nouse &&
2032 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2033 sizeof(unsigned long),
2034 GFP_KERNEL|__GFP_ZERO))))
2037 bucket_gens->first_bucket = ca->mi.first_bucket;
2038 bucket_gens->nbuckets = nbuckets;
2040 bch2_copygc_stop(c);
2043 down_write(&c->gc_lock);
2044 down_write(&ca->bucket_lock);
2045 percpu_down_write(&c->mark_lock);
2048 old_bucket_gens = rcu_dereference_protected(ca->bucket_gens, 1);
2051 size_t n = min(bucket_gens->nbuckets, old_bucket_gens->nbuckets);
2053 memcpy(bucket_gens->b,
2057 memcpy(buckets_nouse,
2059 BITS_TO_LONGS(n) * sizeof(unsigned long));
2062 rcu_assign_pointer(ca->bucket_gens, bucket_gens);
2063 bucket_gens = old_bucket_gens;
2065 swap(ca->buckets_nouse, buckets_nouse);
2067 nbuckets = ca->mi.nbuckets;
2070 percpu_up_write(&c->mark_lock);
2071 up_write(&ca->bucket_lock);
2072 up_write(&c->gc_lock);
2077 kvpfree(buckets_nouse,
2078 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2080 call_rcu(&bucket_gens->rcu, bucket_gens_free_rcu);
2085 void bch2_dev_buckets_free(struct bch_dev *ca)
2089 kvpfree(ca->buckets_nouse,
2090 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2091 kvpfree(rcu_dereference_protected(ca->bucket_gens, 1),
2092 sizeof(struct bucket_gens) + ca->mi.nbuckets);
2094 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
2095 free_percpu(ca->usage[i]);
2096 kfree(ca->usage_base);
2099 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2103 ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL);
2104 if (!ca->usage_base)
2107 for (i = 0; i < ARRAY_SIZE(ca->usage); i++) {
2108 ca->usage[i] = alloc_percpu(struct bch_dev_usage);
2113 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);;