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"
12 #include "btree_update.h"
14 #include "buckets_waiting_for_journal.h"
22 #include "subvolume.h"
24 #include <linux/preempt.h>
25 #include <trace/events/bcachefs.h>
27 static inline void fs_usage_data_type_to_base(struct bch_fs_usage *fs_usage,
28 enum bch_data_type data_type,
33 fs_usage->btree += sectors;
37 fs_usage->data += sectors;
40 fs_usage->cached += sectors;
47 void bch2_fs_usage_initialize(struct bch_fs *c)
49 struct bch_fs_usage *usage;
53 percpu_down_write(&c->mark_lock);
54 usage = c->usage_base;
56 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
57 bch2_fs_usage_acc_to_base(c, i);
59 for (i = 0; i < BCH_REPLICAS_MAX; i++)
60 usage->reserved += usage->persistent_reserved[i];
62 for (i = 0; i < c->replicas.nr; i++) {
63 struct bch_replicas_entry *e =
64 cpu_replicas_entry(&c->replicas, i);
66 fs_usage_data_type_to_base(usage, e->data_type, usage->replicas[i]);
69 for_each_member_device(ca, c, i) {
70 struct bch_dev_usage dev = bch2_dev_usage_read(ca);
72 usage->hidden += (dev.d[BCH_DATA_sb].buckets +
73 dev.d[BCH_DATA_journal].buckets) *
77 percpu_up_write(&c->mark_lock);
80 static inline struct bch_dev_usage *dev_usage_ptr(struct bch_dev *ca,
84 BUG_ON(!gc && !journal_seq);
86 return this_cpu_ptr(gc
88 : ca->usage[journal_seq & JOURNAL_BUF_MASK]);
91 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
93 struct bch_fs *c = ca->fs;
94 struct bch_dev_usage ret;
95 unsigned seq, i, u64s = dev_usage_u64s();
98 seq = read_seqcount_begin(&c->usage_lock);
99 memcpy(&ret, ca->usage_base, u64s * sizeof(u64));
100 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
101 acc_u64s_percpu((u64 *) &ret, (u64 __percpu *) ca->usage[i], u64s);
102 } while (read_seqcount_retry(&c->usage_lock, seq));
107 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
108 unsigned journal_seq,
111 percpu_rwsem_assert_held(&c->mark_lock);
112 BUG_ON(!gc && !journal_seq);
114 return this_cpu_ptr(gc
116 : c->usage[journal_seq & JOURNAL_BUF_MASK]);
119 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
121 ssize_t offset = v - (u64 *) c->usage_base;
125 BUG_ON(offset < 0 || offset >= fs_usage_u64s(c));
126 percpu_rwsem_assert_held(&c->mark_lock);
129 seq = read_seqcount_begin(&c->usage_lock);
132 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
133 ret += percpu_u64_get((u64 __percpu *) c->usage[i] + offset);
134 } while (read_seqcount_retry(&c->usage_lock, seq));
139 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *c)
141 struct bch_fs_usage_online *ret;
142 unsigned seq, i, u64s;
144 percpu_down_read(&c->mark_lock);
146 ret = kmalloc(sizeof(struct bch_fs_usage_online) +
147 sizeof(u64) * c->replicas.nr, GFP_NOFS);
148 if (unlikely(!ret)) {
149 percpu_up_read(&c->mark_lock);
153 ret->online_reserved = percpu_u64_get(c->online_reserved);
155 u64s = fs_usage_u64s(c);
157 seq = read_seqcount_begin(&c->usage_lock);
158 memcpy(&ret->u, c->usage_base, u64s * sizeof(u64));
159 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
160 acc_u64s_percpu((u64 *) &ret->u, (u64 __percpu *) c->usage[i], u64s);
161 } while (read_seqcount_retry(&c->usage_lock, seq));
166 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx)
169 unsigned i, u64s = fs_usage_u64s(c);
171 BUG_ON(idx >= ARRAY_SIZE(c->usage));
174 write_seqcount_begin(&c->usage_lock);
176 acc_u64s_percpu((u64 *) c->usage_base,
177 (u64 __percpu *) c->usage[idx], u64s);
178 percpu_memset(c->usage[idx], 0, u64s * sizeof(u64));
181 for_each_member_device_rcu(ca, c, i, NULL) {
182 u64s = dev_usage_u64s();
184 acc_u64s_percpu((u64 *) ca->usage_base,
185 (u64 __percpu *) ca->usage[idx], u64s);
186 percpu_memset(ca->usage[idx], 0, u64s * sizeof(u64));
190 write_seqcount_end(&c->usage_lock);
194 void bch2_fs_usage_to_text(struct printbuf *out,
196 struct bch_fs_usage_online *fs_usage)
200 pr_buf(out, "capacity:\t\t\t%llu\n", c->capacity);
202 pr_buf(out, "hidden:\t\t\t\t%llu\n",
204 pr_buf(out, "data:\t\t\t\t%llu\n",
206 pr_buf(out, "cached:\t\t\t\t%llu\n",
208 pr_buf(out, "reserved:\t\t\t%llu\n",
209 fs_usage->u.reserved);
210 pr_buf(out, "nr_inodes:\t\t\t%llu\n",
211 fs_usage->u.nr_inodes);
212 pr_buf(out, "online reserved:\t\t%llu\n",
213 fs_usage->online_reserved);
216 i < ARRAY_SIZE(fs_usage->u.persistent_reserved);
218 pr_buf(out, "%u replicas:\n", i + 1);
219 pr_buf(out, "\treserved:\t\t%llu\n",
220 fs_usage->u.persistent_reserved[i]);
223 for (i = 0; i < c->replicas.nr; i++) {
224 struct bch_replicas_entry *e =
225 cpu_replicas_entry(&c->replicas, i);
228 bch2_replicas_entry_to_text(out, e);
229 pr_buf(out, ":\t%llu\n", fs_usage->u.replicas[i]);
233 static u64 reserve_factor(u64 r)
235 return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR);
238 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage_online *fs_usage)
240 return min(fs_usage->u.hidden +
243 reserve_factor(fs_usage->u.reserved +
244 fs_usage->online_reserved),
248 static struct bch_fs_usage_short
249 __bch2_fs_usage_read_short(struct bch_fs *c)
251 struct bch_fs_usage_short ret;
254 ret.capacity = c->capacity -
255 bch2_fs_usage_read_one(c, &c->usage_base->hidden);
257 data = bch2_fs_usage_read_one(c, &c->usage_base->data) +
258 bch2_fs_usage_read_one(c, &c->usage_base->btree);
259 reserved = bch2_fs_usage_read_one(c, &c->usage_base->reserved) +
260 percpu_u64_get(c->online_reserved);
262 ret.used = min(ret.capacity, data + reserve_factor(reserved));
263 ret.free = ret.capacity - ret.used;
265 ret.nr_inodes = bch2_fs_usage_read_one(c, &c->usage_base->nr_inodes);
270 struct bch_fs_usage_short
271 bch2_fs_usage_read_short(struct bch_fs *c)
273 struct bch_fs_usage_short ret;
275 percpu_down_read(&c->mark_lock);
276 ret = __bch2_fs_usage_read_short(c);
277 percpu_up_read(&c->mark_lock);
282 void bch2_dev_usage_init(struct bch_dev *ca)
284 ca->usage_base->d[BCH_DATA_free].buckets = ca->mi.nbuckets - ca->mi.first_bucket;
287 static inline int bucket_sectors_fragmented(struct bch_dev *ca,
288 struct bch_alloc_v4 a)
290 return a.dirty_sectors
291 ? max(0, (int) ca->mi.bucket_size - (int) a.dirty_sectors)
295 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
296 struct bch_alloc_v4 old,
297 struct bch_alloc_v4 new,
298 u64 journal_seq, bool gc)
300 struct bch_fs_usage *fs_usage;
301 struct bch_dev_usage *u;
304 fs_usage = fs_usage_ptr(c, journal_seq, gc);
306 if (data_type_is_hidden(old.data_type))
307 fs_usage->hidden -= ca->mi.bucket_size;
308 if (data_type_is_hidden(new.data_type))
309 fs_usage->hidden += ca->mi.bucket_size;
311 u = dev_usage_ptr(ca, journal_seq, gc);
313 u->d[old.data_type].buckets--;
314 u->d[new.data_type].buckets++;
316 u->buckets_ec -= (int) !!old.stripe;
317 u->buckets_ec += (int) !!new.stripe;
319 u->d[old.data_type].sectors -= old.dirty_sectors;
320 u->d[new.data_type].sectors += new.dirty_sectors;
322 u->d[BCH_DATA_cached].sectors += new.cached_sectors;
323 u->d[BCH_DATA_cached].sectors -= old.cached_sectors;
325 u->d[old.data_type].fragmented -= bucket_sectors_fragmented(ca, old);
326 u->d[new.data_type].fragmented += bucket_sectors_fragmented(ca, new);
331 static void bch2_dev_usage_update_m(struct bch_fs *c, struct bch_dev *ca,
332 struct bucket old, struct bucket new,
333 u64 journal_seq, bool gc)
335 struct bch_alloc_v4 old_a = {
337 .data_type = old.data_type,
338 .dirty_sectors = old.dirty_sectors,
339 .cached_sectors = old.cached_sectors,
340 .stripe = old.stripe,
342 struct bch_alloc_v4 new_a = {
344 .data_type = new.data_type,
345 .dirty_sectors = new.dirty_sectors,
346 .cached_sectors = new.cached_sectors,
347 .stripe = new.stripe,
350 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
353 static inline int __update_replicas(struct bch_fs *c,
354 struct bch_fs_usage *fs_usage,
355 struct bch_replicas_entry *r,
358 int idx = bch2_replicas_entry_idx(c, r);
363 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
364 fs_usage->replicas[idx] += sectors;
368 static inline int update_replicas(struct bch_fs *c, struct bkey_s_c k,
369 struct bch_replicas_entry *r, s64 sectors,
370 unsigned journal_seq, bool gc)
372 struct bch_fs_usage __percpu *fs_usage;
374 struct printbuf buf = PRINTBUF;
376 percpu_down_read(&c->mark_lock);
379 idx = bch2_replicas_entry_idx(c, r);
381 (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) ||
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(&n->r, r, replicas_entry_bytes(r));
471 bch2_replicas_entry_sort(&n->r);
475 static inline void update_cached_sectors_list(struct btree_trans *trans,
476 unsigned dev, s64 sectors)
478 struct bch_replicas_padded r;
480 bch2_replicas_entry_cached(&r.e, dev);
482 update_replicas_list(trans, &r.e, sectors);
485 int bch2_mark_alloc(struct btree_trans *trans,
486 struct bkey_s_c old, struct bkey_s_c new,
489 bool gc = flags & BTREE_TRIGGER_GC;
490 u64 journal_seq = trans->journal_res.seq;
491 struct bch_fs *c = trans->c;
492 struct bch_alloc_v4 old_a, new_a;
497 * alloc btree is read in by bch2_alloc_read, not gc:
499 if ((flags & BTREE_TRIGGER_GC) &&
500 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
503 if (bch2_trans_inconsistent_on(!bch2_dev_bucket_exists(c, new.k->p), trans,
504 "alloc key for invalid device or bucket"))
507 ca = bch_dev_bkey_exists(c, new.k->p.inode);
509 bch2_alloc_to_v4(old, &old_a);
510 bch2_alloc_to_v4(new, &new_a);
512 if ((flags & BTREE_TRIGGER_INSERT) &&
513 data_type_is_empty(old_a.data_type) !=
514 data_type_is_empty(new_a.data_type) &&
515 new.k->type == KEY_TYPE_alloc_v4) {
516 struct bch_alloc_v4 *v = (struct bch_alloc_v4 *) new.v;
518 BUG_ON(!journal_seq);
521 * If the btree updates referring to a bucket weren't flushed
522 * before the bucket became empty again, then the we don't have
523 * to wait on a journal flush before we can reuse the bucket:
525 new_a.journal_seq = data_type_is_empty(new_a.data_type) &&
526 (journal_seq == v->journal_seq ||
527 bch2_journal_noflush_seq(&c->journal, v->journal_seq))
529 v->journal_seq = new_a.journal_seq;
532 if (!data_type_is_empty(old_a.data_type) &&
533 data_type_is_empty(new_a.data_type) &&
535 ret = bch2_set_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
536 c->journal.flushed_seq_ondisk,
537 new.k->p.inode, new.k->p.offset,
540 bch2_fs_fatal_error(c,
541 "error setting bucket_needs_journal_commit: %i", ret);
546 if (new_a.data_type == BCH_DATA_free &&
547 (!new_a.journal_seq || new_a.journal_seq < c->journal.flushed_seq_ondisk))
548 closure_wake_up(&c->freelist_wait);
550 if (new_a.data_type == BCH_DATA_need_discard &&
551 (!new_a.journal_seq || new_a.journal_seq < c->journal.flushed_seq_ondisk))
554 if (old_a.data_type != BCH_DATA_cached &&
555 new_a.data_type == BCH_DATA_cached &&
556 should_invalidate_buckets(ca, bch2_dev_usage_read(ca)))
557 bch2_do_invalidates(c);
559 if (new_a.data_type == BCH_DATA_need_gc_gens)
562 percpu_down_read(&c->mark_lock);
563 if (!gc && new_a.gen != old_a.gen)
564 *bucket_gen(ca, new.k->p.offset) = new_a.gen;
566 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
569 struct bucket *g = gc_bucket(ca, new.k->p.offset);
575 g->data_type = new_a.data_type;
576 g->stripe = new_a.stripe;
577 g->stripe_redundancy = new_a.stripe_redundancy;
578 g->dirty_sectors = new_a.dirty_sectors;
579 g->cached_sectors = new_a.cached_sectors;
583 percpu_up_read(&c->mark_lock);
586 * need to know if we're getting called from the invalidate path or
590 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
591 old_a.cached_sectors) {
592 ret = update_cached_sectors(c, new, ca->dev_idx,
593 -old_a.cached_sectors,
596 bch2_fs_fatal_error(c, "bch2_mark_alloc(): no replicas entry while updating cached sectors");
600 trace_invalidate(ca, bucket_to_sector(ca, new.k->p.offset),
601 old_a.cached_sectors);
607 int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
608 size_t b, enum bch_data_type data_type,
609 unsigned sectors, struct gc_pos pos,
612 struct bucket old, new, *g;
615 BUG_ON(!(flags & BTREE_TRIGGER_GC));
616 BUG_ON(data_type != BCH_DATA_sb &&
617 data_type != BCH_DATA_journal);
620 * Backup superblock might be past the end of our normal usable space:
622 if (b >= ca->mi.nbuckets)
625 percpu_down_read(&c->mark_lock);
626 g = gc_bucket(ca, b);
631 if (bch2_fs_inconsistent_on(g->data_type &&
632 g->data_type != data_type, c,
633 "different types of data in same bucket: %s, %s",
634 bch2_data_types[g->data_type],
635 bch2_data_types[data_type])) {
640 if (bch2_fs_inconsistent_on((u64) g->dirty_sectors + sectors > ca->mi.bucket_size, c,
641 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > bucket size",
642 ca->dev_idx, b, g->gen,
643 bch2_data_types[g->data_type ?: data_type],
644 g->dirty_sectors, sectors)) {
650 g->data_type = data_type;
651 g->dirty_sectors += sectors;
656 bch2_dev_usage_update_m(c, ca, old, new, 0, true);
657 percpu_up_read(&c->mark_lock);
661 static s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p)
663 EBUG_ON(sectors < 0);
665 return crc_is_compressed(p.crc)
666 ? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size,
667 p.crc.uncompressed_size)
671 static int check_bucket_ref(struct bch_fs *c,
673 const struct bch_extent_ptr *ptr,
674 s64 sectors, enum bch_data_type ptr_data_type,
675 u8 b_gen, u8 bucket_data_type,
676 u32 dirty_sectors, u32 cached_sectors)
678 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
679 size_t bucket_nr = PTR_BUCKET_NR(ca, ptr);
680 u16 bucket_sectors = !ptr->cached
683 struct printbuf buf = PRINTBUF;
686 if (bucket_data_type == BCH_DATA_cached)
687 bucket_data_type = BCH_DATA_user;
689 if (gen_after(ptr->gen, b_gen)) {
690 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
691 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
693 ptr->dev, bucket_nr, b_gen,
694 bch2_data_types[bucket_data_type ?: ptr_data_type],
696 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
701 if (gen_cmp(b_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
702 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
703 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
705 ptr->dev, bucket_nr, b_gen,
706 bch2_data_types[bucket_data_type ?: ptr_data_type],
708 (printbuf_reset(&buf),
709 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
714 if (b_gen != ptr->gen && !ptr->cached) {
715 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
716 "bucket %u:%zu gen %u (mem gen %u) data type %s: stale dirty ptr (gen %u)\n"
718 ptr->dev, bucket_nr, b_gen,
719 *bucket_gen(ca, bucket_nr),
720 bch2_data_types[bucket_data_type ?: ptr_data_type],
722 (printbuf_reset(&buf),
723 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
728 if (b_gen != ptr->gen) {
733 if (!data_type_is_empty(bucket_data_type) &&
735 bucket_data_type != ptr_data_type) {
736 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
737 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
739 ptr->dev, bucket_nr, b_gen,
740 bch2_data_types[bucket_data_type],
741 bch2_data_types[ptr_data_type],
742 (printbuf_reset(&buf),
743 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
748 if ((unsigned) (bucket_sectors + sectors) > U32_MAX) {
749 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
750 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
752 ptr->dev, bucket_nr, b_gen,
753 bch2_data_types[bucket_data_type ?: ptr_data_type],
754 bucket_sectors, sectors,
755 (printbuf_reset(&buf),
756 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
765 static int mark_stripe_bucket(struct btree_trans *trans,
770 struct bch_fs *c = trans->c;
771 u64 journal_seq = trans->journal_res.seq;
772 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
773 unsigned nr_data = s->nr_blocks - s->nr_redundant;
774 bool parity = ptr_idx >= nr_data;
775 enum bch_data_type data_type = parity ? BCH_DATA_parity : 0;
776 s64 sectors = parity ? le16_to_cpu(s->sectors) : 0;
777 const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
778 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
779 struct bucket old, new, *g;
780 struct printbuf buf = PRINTBUF;
783 BUG_ON(!(flags & BTREE_TRIGGER_GC));
785 /* * XXX doesn't handle deletion */
787 percpu_down_read(&c->mark_lock);
789 g = PTR_GC_BUCKET(ca, ptr);
791 if (g->dirty_sectors ||
792 (g->stripe && g->stripe != k.k->p.offset)) {
793 bch2_fs_inconsistent(c,
794 "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
795 ptr->dev, PTR_BUCKET_NR(ca, ptr), g->gen,
796 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
804 ret = check_bucket_ref(c, k, ptr, sectors, data_type,
805 g->gen, g->data_type,
806 g->dirty_sectors, g->cached_sectors);
811 g->data_type = data_type;
812 g->dirty_sectors += sectors;
814 g->stripe = k.k->p.offset;
815 g->stripe_redundancy = s->nr_redundant;
820 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
821 percpu_up_read(&c->mark_lock);
826 static int __mark_pointer(struct btree_trans *trans,
828 const struct bch_extent_ptr *ptr,
829 s64 sectors, enum bch_data_type ptr_data_type,
830 u8 bucket_gen, u8 *bucket_data_type,
831 u32 *dirty_sectors, u32 *cached_sectors)
833 u32 *dst_sectors = !ptr->cached
836 int ret = check_bucket_ref(trans->c, k, ptr, sectors, ptr_data_type,
837 bucket_gen, *bucket_data_type,
838 *dirty_sectors, *cached_sectors);
843 *dst_sectors += sectors;
844 *bucket_data_type = *dirty_sectors || *cached_sectors
849 static int bch2_mark_pointer(struct btree_trans *trans,
851 struct extent_ptr_decoded p,
852 s64 sectors, enum bch_data_type data_type,
855 u64 journal_seq = trans->journal_res.seq;
856 struct bch_fs *c = trans->c;
857 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
858 struct bucket old, new, *g;
862 BUG_ON(!(flags & BTREE_TRIGGER_GC));
864 percpu_down_read(&c->mark_lock);
865 g = PTR_GC_BUCKET(ca, &p.ptr);
869 bucket_data_type = g->data_type;
870 ret = __mark_pointer(trans, k, &p.ptr, sectors,
876 g->data_type = bucket_data_type;
881 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
882 percpu_up_read(&c->mark_lock);
887 static int bch2_mark_stripe_ptr(struct btree_trans *trans,
889 struct bch_extent_stripe_ptr p,
890 enum bch_data_type data_type,
894 struct bch_fs *c = trans->c;
895 struct bch_replicas_padded r;
898 BUG_ON(!(flags & BTREE_TRIGGER_GC));
900 m = genradix_ptr_alloc(&c->gc_stripes, p.idx, GFP_KERNEL);
902 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
907 spin_lock(&c->ec_stripes_heap_lock);
909 if (!m || !m->alive) {
910 spin_unlock(&c->ec_stripes_heap_lock);
911 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
913 bch2_inconsistent_error(c);
917 m->block_sectors[p.block] += sectors;
920 spin_unlock(&c->ec_stripes_heap_lock);
922 r.e.data_type = data_type;
923 update_replicas(c, k, &r.e, sectors, trans->journal_res.seq, true);
928 int bch2_mark_extent(struct btree_trans *trans,
929 struct bkey_s_c old, struct bkey_s_c new,
932 u64 journal_seq = trans->journal_res.seq;
933 struct bch_fs *c = trans->c;
934 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
935 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
936 const union bch_extent_entry *entry;
937 struct extent_ptr_decoded p;
938 struct bch_replicas_padded r;
939 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
942 s64 sectors = bkey_is_btree_ptr(k.k)
945 s64 dirty_sectors = 0;
949 BUG_ON(!(flags & BTREE_TRIGGER_GC));
951 r.e.data_type = data_type;
955 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
956 s64 disk_sectors = ptr_disk_sectors(sectors, p);
958 if (flags & BTREE_TRIGGER_OVERWRITE)
959 disk_sectors = -disk_sectors;
961 ret = bch2_mark_pointer(trans, k, p, disk_sectors,
970 ret = update_cached_sectors(c, k, p.ptr.dev,
971 disk_sectors, journal_seq, true);
973 bch2_fs_fatal_error(c, "bch2_mark_extent(): 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, "no replicas entry for %s", 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_v2 *v = (struct bch_inode_v2 *) new.v;
1128 BUG_ON(!journal_seq);
1129 BUG_ON(new.k->type != KEY_TYPE_inode_v2);
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;
1214 new.k.type = KEY_TYPE_error;
1215 new.k.p = bkey_start_pos(p.k);
1216 new.k.p.offset += *idx - start;
1217 bch2_key_resize(&new.k, next_idx - *idx);
1218 ret = __bch2_btree_insert(trans, BTREE_ID_extents, &new.k_i);
1223 printbuf_exit(&buf);
1227 int bch2_mark_reflink_p(struct btree_trans *trans,
1228 struct bkey_s_c old, struct bkey_s_c new,
1231 struct bch_fs *c = trans->c;
1232 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
1233 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1234 struct reflink_gc *ref;
1236 u64 idx = le64_to_cpu(p.v->idx), start = idx;
1237 u64 end = le64_to_cpu(p.v->idx) + p.k->size;
1240 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1242 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix) {
1243 idx -= le32_to_cpu(p.v->front_pad);
1244 end += le32_to_cpu(p.v->back_pad);
1248 r = c->reflink_gc_nr;
1250 m = l + (r - l) / 2;
1252 ref = genradix_ptr(&c->reflink_gc_table, m);
1253 if (ref->offset <= idx)
1259 while (idx < end && !ret)
1260 ret = __bch2_mark_reflink_p(trans, p, start, end,
1266 static noinline __cold
1267 void fs_usage_apply_warn(struct btree_trans *trans,
1268 unsigned disk_res_sectors,
1269 s64 should_not_have_added)
1271 struct bch_fs *c = trans->c;
1272 struct btree_insert_entry *i;
1273 struct printbuf buf = PRINTBUF;
1275 bch_err(c, "disk usage increased %lli more than %u sectors reserved",
1276 should_not_have_added, disk_res_sectors);
1278 trans_for_each_update(trans, i) {
1279 struct bkey_s_c old = { &i->old_k, i->old_v };
1281 pr_err("while inserting");
1282 printbuf_reset(&buf);
1283 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(i->k));
1284 pr_err(" %s", buf.buf);
1285 pr_err("overlapping with");
1286 printbuf_reset(&buf);
1287 bch2_bkey_val_to_text(&buf, c, old);
1288 pr_err(" %s", buf.buf);
1292 printbuf_exit(&buf);
1295 int bch2_trans_fs_usage_apply(struct btree_trans *trans,
1296 struct replicas_delta_list *deltas)
1298 struct bch_fs *c = trans->c;
1299 static int warned_disk_usage = 0;
1301 unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1302 struct replicas_delta *d = deltas->d, *d2;
1303 struct replicas_delta *top = (void *) deltas->d + deltas->used;
1304 struct bch_fs_usage *dst;
1305 s64 added = 0, should_not_have_added;
1308 percpu_down_read(&c->mark_lock);
1310 dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1312 for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1313 switch (d->r.data_type) {
1314 case BCH_DATA_btree:
1316 case BCH_DATA_parity:
1320 if (__update_replicas(c, dst, &d->r, d->delta))
1324 dst->nr_inodes += deltas->nr_inodes;
1326 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1327 added += deltas->persistent_reserved[i];
1328 dst->reserved += deltas->persistent_reserved[i];
1329 dst->persistent_reserved[i] += deltas->persistent_reserved[i];
1333 * Not allowed to reduce sectors_available except by getting a
1336 should_not_have_added = added - (s64) disk_res_sectors;
1337 if (unlikely(should_not_have_added > 0)) {
1338 u64 old, new, v = atomic64_read(&c->sectors_available);
1342 new = max_t(s64, 0, old - should_not_have_added);
1343 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1346 added -= should_not_have_added;
1351 trans->disk_res->sectors -= added;
1352 this_cpu_sub(*c->online_reserved, added);
1356 percpu_up_read(&c->mark_lock);
1358 if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
1359 fs_usage_apply_warn(trans, disk_res_sectors, should_not_have_added);
1362 /* revert changes: */
1363 for (d2 = deltas->d; d2 != d; d2 = replicas_delta_next(d2))
1364 BUG_ON(__update_replicas(c, dst, &d2->r, -d2->delta));
1367 percpu_up_read(&c->mark_lock);
1373 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1374 struct bkey_s_c k, struct extent_ptr_decoded p,
1375 s64 sectors, enum bch_data_type data_type)
1377 struct btree_iter iter;
1378 struct bkey_i_alloc_v4 *a;
1381 a = bch2_trans_start_alloc_update(trans, &iter, PTR_BUCKET_POS(trans->c, &p.ptr));
1385 ret = __mark_pointer(trans, k, &p.ptr, sectors, data_type,
1386 a->v.gen, &a->v.data_type,
1387 &a->v.dirty_sectors, &a->v.cached_sectors) ?:
1388 bch2_trans_update(trans, &iter, &a->k_i, 0);
1389 bch2_trans_iter_exit(trans, &iter);
1393 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1394 struct extent_ptr_decoded p,
1395 s64 sectors, enum bch_data_type data_type)
1397 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 k = bch2_btree_iter_peek_slot(&iter);
1411 if (k.k->type != KEY_TYPE_stripe) {
1412 bch2_trans_inconsistent(trans,
1413 "pointer to nonexistent stripe %llu",
1419 if (!bch2_ptr_matches_stripe(bkey_s_c_to_stripe(k).v, p)) {
1420 bch2_trans_inconsistent(trans,
1421 "stripe pointer doesn't match stripe %llu",
1427 s = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1428 ret = PTR_ERR_OR_ZERO(s);
1432 bkey_reassemble(&s->k_i, k);
1433 stripe_blockcount_set(&s->v, p.ec.block,
1434 stripe_blockcount_get(&s->v, p.ec.block) +
1437 ret = bch2_trans_update(trans, &iter, &s->k_i, 0);
1441 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1442 r.e.data_type = data_type;
1443 update_replicas_list(trans, &r.e, sectors);
1445 bch2_trans_iter_exit(trans, &iter);
1449 int bch2_trans_mark_extent(struct btree_trans *trans,
1450 struct bkey_s_c old, struct bkey_i *new,
1453 struct bch_fs *c = trans->c;
1454 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1456 : bkey_i_to_s_c(new);
1457 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1458 const union bch_extent_entry *entry;
1459 struct extent_ptr_decoded p;
1460 struct bch_replicas_padded r;
1461 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
1464 s64 sectors = bkey_is_btree_ptr(k.k)
1467 s64 dirty_sectors = 0;
1471 r.e.data_type = data_type;
1473 r.e.nr_required = 1;
1475 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1476 s64 disk_sectors = ptr_disk_sectors(sectors, p);
1478 if (flags & BTREE_TRIGGER_OVERWRITE)
1479 disk_sectors = -disk_sectors;
1481 ret = bch2_trans_mark_pointer(trans, k, p,
1482 disk_sectors, data_type);
1490 update_cached_sectors_list(trans, p.ptr.dev,
1492 } else if (!p.has_ec) {
1493 dirty_sectors += disk_sectors;
1494 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1496 ret = bch2_trans_mark_stripe_ptr(trans, p,
1497 disk_sectors, data_type);
1501 r.e.nr_required = 0;
1506 update_replicas_list(trans, &r.e, dirty_sectors);
1511 static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
1512 struct bkey_s_c_stripe s,
1513 unsigned idx, bool deleting)
1515 struct bch_fs *c = trans->c;
1516 const struct bch_extent_ptr *ptr = &s.v->ptrs[idx];
1517 struct btree_iter iter;
1518 struct bkey_i_alloc_v4 *a;
1519 enum bch_data_type data_type = idx >= s.v->nr_blocks - s.v->nr_redundant
1520 ? BCH_DATA_parity : 0;
1521 s64 sectors = data_type ? le16_to_cpu(s.v->sectors) : 0;
1527 a = bch2_trans_start_alloc_update(trans, &iter, PTR_BUCKET_POS(c, ptr));
1531 ret = check_bucket_ref(c, s.s_c, ptr, sectors, data_type,
1532 a->v.gen, a->v.data_type,
1533 a->v.dirty_sectors, a->v.cached_sectors);
1538 if (bch2_trans_inconsistent_on(a->v.stripe ||
1539 a->v.stripe_redundancy, trans,
1540 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)",
1541 iter.pos.inode, iter.pos.offset, a->v.gen,
1542 bch2_data_types[a->v.data_type],
1544 a->v.stripe, s.k->p.offset)) {
1549 if (bch2_trans_inconsistent_on(data_type && a->v.dirty_sectors, trans,
1550 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: data already in stripe bucket %llu",
1551 iter.pos.inode, iter.pos.offset, a->v.gen,
1552 bch2_data_types[a->v.data_type],
1559 a->v.stripe = s.k->p.offset;
1560 a->v.stripe_redundancy = s.v->nr_redundant;
1562 if (bch2_trans_inconsistent_on(a->v.stripe != s.k->p.offset ||
1563 a->v.stripe_redundancy != s.v->nr_redundant, trans,
1564 "bucket %llu:%llu gen %u: not marked as stripe when deleting stripe %llu (got %u)",
1565 iter.pos.inode, iter.pos.offset, a->v.gen,
1566 s.k->p.offset, a->v.stripe)) {
1572 a->v.stripe_redundancy = 0;
1575 a->v.dirty_sectors += sectors;
1577 a->v.data_type = !deleting ? data_type : 0;
1579 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1583 bch2_trans_iter_exit(trans, &iter);
1587 int bch2_trans_mark_stripe(struct btree_trans *trans,
1588 struct bkey_s_c old, struct bkey_i *new,
1591 const struct bch_stripe *old_s = NULL;
1592 struct bch_stripe *new_s = NULL;
1593 struct bch_replicas_padded r;
1594 unsigned i, nr_blocks;
1597 if (old.k->type == KEY_TYPE_stripe)
1598 old_s = bkey_s_c_to_stripe(old).v;
1599 if (new->k.type == KEY_TYPE_stripe)
1600 new_s = &bkey_i_to_stripe(new)->v;
1603 * If the pointers aren't changing, we don't need to do anything:
1605 if (new_s && old_s &&
1606 new_s->nr_blocks == old_s->nr_blocks &&
1607 new_s->nr_redundant == old_s->nr_redundant &&
1608 !memcmp(old_s->ptrs, new_s->ptrs,
1609 new_s->nr_blocks * sizeof(struct bch_extent_ptr)))
1612 BUG_ON(new_s && old_s &&
1613 (new_s->nr_blocks != old_s->nr_blocks ||
1614 new_s->nr_redundant != old_s->nr_redundant));
1616 nr_blocks = new_s ? new_s->nr_blocks : old_s->nr_blocks;
1619 s64 sectors = le16_to_cpu(new_s->sectors);
1621 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(new));
1622 update_replicas_list(trans, &r.e, sectors * new_s->nr_redundant);
1626 s64 sectors = -((s64) le16_to_cpu(old_s->sectors));
1628 bch2_bkey_to_replicas(&r.e, old);
1629 update_replicas_list(trans, &r.e, sectors * old_s->nr_redundant);
1632 for (i = 0; i < nr_blocks; i++) {
1633 if (new_s && old_s &&
1634 !memcmp(&new_s->ptrs[i],
1636 sizeof(new_s->ptrs[i])))
1640 ret = bch2_trans_mark_stripe_bucket(trans,
1641 bkey_i_to_s_c_stripe(new), i, false);
1647 ret = bch2_trans_mark_stripe_bucket(trans,
1648 bkey_s_c_to_stripe(old), i, true);
1657 int bch2_trans_mark_inode(struct btree_trans *trans,
1658 struct bkey_s_c old,
1662 int nr = bkey_is_inode(&new->k) - bkey_is_inode(old.k);
1665 struct replicas_delta_list *d =
1666 replicas_deltas_realloc(trans, 0);
1673 int bch2_trans_mark_reservation(struct btree_trans *trans,
1674 struct bkey_s_c old,
1678 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1680 : bkey_i_to_s_c(new);
1681 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1682 s64 sectors = (s64) k.k->size;
1683 struct replicas_delta_list *d;
1685 if (flags & BTREE_TRIGGER_OVERWRITE)
1687 sectors *= replicas;
1689 d = replicas_deltas_realloc(trans, 0);
1691 replicas = clamp_t(unsigned, replicas, 1,
1692 ARRAY_SIZE(d->persistent_reserved));
1694 d->persistent_reserved[replicas - 1] += sectors;
1698 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1699 struct bkey_s_c_reflink_p p,
1700 u64 *idx, unsigned flags)
1702 struct bch_fs *c = trans->c;
1703 struct btree_iter iter;
1707 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1708 struct printbuf buf = PRINTBUF;
1711 bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink, POS(0, *idx),
1713 BTREE_ITER_WITH_UPDATES);
1714 k = bch2_btree_iter_peek_slot(&iter);
1719 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1720 ret = PTR_ERR_OR_ZERO(n);
1724 bkey_reassemble(n, k);
1726 refcount = bkey_refcount(n);
1728 bch2_bkey_val_to_text(&buf, c, p.s_c);
1729 bch2_trans_inconsistent(trans,
1730 "nonexistent indirect extent at %llu while marking\n %s",
1736 if (!*refcount && (flags & BTREE_TRIGGER_OVERWRITE)) {
1737 bch2_bkey_val_to_text(&buf, c, p.s_c);
1738 bch2_trans_inconsistent(trans,
1739 "indirect extent refcount underflow at %llu while marking\n %s",
1745 if (flags & BTREE_TRIGGER_INSERT) {
1746 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1749 pad = max_t(s64, le32_to_cpu(v->front_pad),
1750 le64_to_cpu(v->idx) - bkey_start_offset(k.k));
1751 BUG_ON(pad > U32_MAX);
1752 v->front_pad = cpu_to_le32(pad);
1754 pad = max_t(s64, le32_to_cpu(v->back_pad),
1755 k.k->p.offset - p.k->size - le64_to_cpu(v->idx));
1756 BUG_ON(pad > U32_MAX);
1757 v->back_pad = cpu_to_le32(pad);
1760 le64_add_cpu(refcount, add);
1762 bch2_btree_iter_set_pos_to_extent_start(&iter);
1763 ret = bch2_trans_update(trans, &iter, n, 0);
1767 *idx = k.k->p.offset;
1769 bch2_trans_iter_exit(trans, &iter);
1770 printbuf_exit(&buf);
1774 int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1775 struct bkey_s_c old,
1779 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1781 : bkey_i_to_s_c(new);
1782 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1786 if (flags & BTREE_TRIGGER_INSERT) {
1787 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1789 v->front_pad = v->back_pad = 0;
1792 idx = le64_to_cpu(p.v->idx) - le32_to_cpu(p.v->front_pad);
1793 end_idx = le64_to_cpu(p.v->idx) + p.k->size +
1794 le32_to_cpu(p.v->back_pad);
1796 while (idx < end_idx && !ret)
1797 ret = __bch2_trans_mark_reflink_p(trans, p, &idx, flags);
1802 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1803 struct bch_dev *ca, size_t b,
1804 enum bch_data_type type,
1807 struct bch_fs *c = trans->c;
1808 struct btree_iter iter;
1809 struct bkey_i_alloc_v4 *a;
1813 * Backup superblock might be past the end of our normal usable space:
1815 if (b >= ca->mi.nbuckets)
1818 a = bch2_trans_start_alloc_update(trans, &iter, POS(ca->dev_idx, b));
1822 if (a->v.data_type && a->v.data_type != type) {
1823 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1824 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
1826 iter.pos.inode, iter.pos.offset, a->v.gen,
1827 bch2_data_types[a->v.data_type],
1828 bch2_data_types[type],
1829 bch2_data_types[type]);
1834 a->v.data_type = type;
1835 a->v.dirty_sectors = sectors;
1837 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1841 bch2_trans_iter_exit(trans, &iter);
1845 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1846 struct bch_dev *ca, size_t b,
1847 enum bch_data_type type,
1850 return __bch2_trans_do(trans, NULL, NULL, 0,
1851 __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors));
1854 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans,
1857 enum bch_data_type type,
1858 u64 *bucket, unsigned *bucket_sectors)
1861 u64 b = sector_to_bucket(ca, start);
1863 min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
1865 if (b != *bucket && *bucket_sectors) {
1866 int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket,
1867 type, *bucket_sectors);
1871 *bucket_sectors = 0;
1875 *bucket_sectors += sectors;
1877 } while (start < end);
1882 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
1885 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
1887 unsigned i, bucket_sectors = 0;
1890 for (i = 0; i < layout->nr_superblocks; i++) {
1891 u64 offset = le64_to_cpu(layout->sb_offset[i]);
1893 if (offset == BCH_SB_SECTOR) {
1894 ret = bch2_trans_mark_metadata_sectors(trans, ca,
1896 BCH_DATA_sb, &bucket, &bucket_sectors);
1901 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset,
1902 offset + (1 << layout->sb_max_size_bits),
1903 BCH_DATA_sb, &bucket, &bucket_sectors);
1908 if (bucket_sectors) {
1909 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1910 bucket, BCH_DATA_sb, bucket_sectors);
1915 for (i = 0; i < ca->journal.nr; i++) {
1916 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1917 ca->journal.buckets[i],
1918 BCH_DATA_journal, ca->mi.bucket_size);
1926 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
1928 return bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
1929 __bch2_trans_mark_dev_sb(&trans, ca));
1932 /* Disk reservations: */
1934 #define SECTORS_CACHE 1024
1936 int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
1937 u64 sectors, int flags)
1939 struct bch_fs_pcpu *pcpu;
1941 s64 sectors_available;
1944 percpu_down_read(&c->mark_lock);
1946 pcpu = this_cpu_ptr(c->pcpu);
1948 if (sectors <= pcpu->sectors_available)
1951 v = atomic64_read(&c->sectors_available);
1954 get = min((u64) sectors + SECTORS_CACHE, old);
1956 if (get < sectors) {
1960 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1961 old, old - get)) != old);
1963 pcpu->sectors_available += get;
1966 pcpu->sectors_available -= sectors;
1967 this_cpu_add(*c->online_reserved, sectors);
1968 res->sectors += sectors;
1971 percpu_up_read(&c->mark_lock);
1975 mutex_lock(&c->sectors_available_lock);
1977 percpu_u64_set(&c->pcpu->sectors_available, 0);
1978 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
1980 if (sectors <= sectors_available ||
1981 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
1982 atomic64_set(&c->sectors_available,
1983 max_t(s64, 0, sectors_available - sectors));
1984 this_cpu_add(*c->online_reserved, sectors);
1985 res->sectors += sectors;
1988 atomic64_set(&c->sectors_available, sectors_available);
1992 mutex_unlock(&c->sectors_available_lock);
1993 percpu_up_read(&c->mark_lock);
1998 /* Startup/shutdown: */
2000 static void bucket_gens_free_rcu(struct rcu_head *rcu)
2002 struct bucket_gens *buckets =
2003 container_of(rcu, struct bucket_gens, rcu);
2005 kvpfree(buckets, sizeof(*buckets) + buckets->nbuckets);
2008 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
2010 struct bucket_gens *bucket_gens = NULL, *old_bucket_gens = NULL;
2011 unsigned long *buckets_nouse = NULL;
2012 bool resize = ca->bucket_gens != NULL;
2015 if (!(bucket_gens = kvpmalloc(sizeof(struct bucket_gens) + nbuckets,
2016 GFP_KERNEL|__GFP_ZERO)) ||
2017 (c->opts.buckets_nouse &&
2018 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2019 sizeof(unsigned long),
2020 GFP_KERNEL|__GFP_ZERO))))
2023 bucket_gens->first_bucket = ca->mi.first_bucket;
2024 bucket_gens->nbuckets = nbuckets;
2026 bch2_copygc_stop(c);
2029 down_write(&c->gc_lock);
2030 down_write(&ca->bucket_lock);
2031 percpu_down_write(&c->mark_lock);
2034 old_bucket_gens = rcu_dereference_protected(ca->bucket_gens, 1);
2037 size_t n = min(bucket_gens->nbuckets, old_bucket_gens->nbuckets);
2039 memcpy(bucket_gens->b,
2043 memcpy(buckets_nouse,
2045 BITS_TO_LONGS(n) * sizeof(unsigned long));
2048 rcu_assign_pointer(ca->bucket_gens, bucket_gens);
2049 bucket_gens = old_bucket_gens;
2051 swap(ca->buckets_nouse, buckets_nouse);
2053 nbuckets = ca->mi.nbuckets;
2056 percpu_up_write(&c->mark_lock);
2057 up_write(&ca->bucket_lock);
2058 up_write(&c->gc_lock);
2063 kvpfree(buckets_nouse,
2064 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2066 call_rcu(&bucket_gens->rcu, bucket_gens_free_rcu);
2071 void bch2_dev_buckets_free(struct bch_dev *ca)
2075 kvpfree(ca->buckets_nouse,
2076 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2077 kvpfree(rcu_dereference_protected(ca->bucket_gens, 1),
2078 sizeof(struct bucket_gens) + ca->mi.nbuckets);
2080 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
2081 free_percpu(ca->usage[i]);
2082 kfree(ca->usage_base);
2085 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2089 ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL);
2090 if (!ca->usage_base)
2093 for (i = 0; i < ARRAY_SIZE(ca->usage); i++) {
2094 ca->usage[i] = alloc_percpu(struct bch_dev_usage);
2099 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);;