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 static inline int is_unavailable_bucket(struct bkey_alloc_unpacked a)
284 return a.dirty_sectors || a.stripe;
287 static inline int bucket_sectors_fragmented(struct bch_dev *ca,
288 struct bkey_alloc_unpacked a)
290 return a.dirty_sectors
291 ? max(0, (int) ca->mi.bucket_size - (int) a.dirty_sectors)
295 static inline enum bch_data_type bucket_type(struct bkey_alloc_unpacked a)
297 return a.cached_sectors && !a.dirty_sectors
302 static inline void account_bucket(struct bch_fs_usage *fs_usage,
303 struct bch_dev_usage *dev_usage,
304 enum bch_data_type type,
307 if (type == BCH_DATA_sb || type == BCH_DATA_journal)
308 fs_usage->hidden += size;
310 dev_usage->d[type].buckets += nr;
313 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
314 struct bkey_alloc_unpacked old,
315 struct bkey_alloc_unpacked new,
316 u64 journal_seq, bool gc)
318 struct bch_fs_usage *fs_usage;
319 struct bch_dev_usage *u;
322 fs_usage = fs_usage_ptr(c, journal_seq, gc);
323 u = dev_usage_ptr(ca, journal_seq, gc);
325 if (bucket_type(old))
326 account_bucket(fs_usage, u, bucket_type(old),
327 -1, -ca->mi.bucket_size);
329 if (bucket_type(new))
330 account_bucket(fs_usage, u, bucket_type(new),
331 1, ca->mi.bucket_size);
333 u->buckets_ec += (int) new.stripe - (int) old.stripe;
334 u->buckets_unavailable +=
335 is_unavailable_bucket(new) - is_unavailable_bucket(old);
337 u->d[old.data_type].sectors -= old.dirty_sectors;
338 u->d[new.data_type].sectors += new.dirty_sectors;
339 u->d[BCH_DATA_cached].sectors +=
340 (int) new.cached_sectors - (int) old.cached_sectors;
342 u->d[old.data_type].fragmented -= bucket_sectors_fragmented(ca, old);
343 u->d[new.data_type].fragmented += bucket_sectors_fragmented(ca, new);
348 static void bch2_dev_usage_update_m(struct bch_fs *c, struct bch_dev *ca,
349 struct bucket old, struct bucket new,
350 u64 journal_seq, bool gc)
352 struct bkey_alloc_unpacked old_a = {
354 .data_type = old.data_type,
355 .dirty_sectors = old.dirty_sectors,
356 .cached_sectors = old.cached_sectors,
357 .stripe = old.stripe,
359 struct bkey_alloc_unpacked new_a = {
361 .data_type = new.data_type,
362 .dirty_sectors = new.dirty_sectors,
363 .cached_sectors = new.cached_sectors,
364 .stripe = new.stripe,
367 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
370 static inline int __update_replicas(struct bch_fs *c,
371 struct bch_fs_usage *fs_usage,
372 struct bch_replicas_entry *r,
375 int idx = bch2_replicas_entry_idx(c, r);
380 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
381 fs_usage->replicas[idx] += sectors;
385 static inline int update_replicas(struct bch_fs *c, struct bkey_s_c k,
386 struct bch_replicas_entry *r, s64 sectors,
387 unsigned journal_seq, bool gc)
389 struct bch_fs_usage __percpu *fs_usage;
391 struct printbuf buf = PRINTBUF;
393 percpu_down_read(&c->mark_lock);
396 idx = bch2_replicas_entry_idx(c, r);
398 (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) ||
399 fsck_err(c, "no replicas entry\n"
401 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))) {
402 percpu_up_read(&c->mark_lock);
403 ret = bch2_mark_replicas(c, r);
404 percpu_down_read(&c->mark_lock);
408 idx = bch2_replicas_entry_idx(c, r);
416 fs_usage = fs_usage_ptr(c, journal_seq, gc);
417 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
418 fs_usage->replicas[idx] += sectors;
422 percpu_up_read(&c->mark_lock);
427 static inline int update_cached_sectors(struct bch_fs *c,
429 unsigned dev, s64 sectors,
430 unsigned journal_seq, bool gc)
432 struct bch_replicas_padded r;
434 bch2_replicas_entry_cached(&r.e, dev);
436 return update_replicas(c, k, &r.e, sectors, journal_seq, gc);
439 static struct replicas_delta_list *
440 replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
442 struct replicas_delta_list *d = trans->fs_usage_deltas;
443 unsigned new_size = d ? (d->size + more) * 2 : 128;
444 unsigned alloc_size = sizeof(*d) + new_size;
446 WARN_ON_ONCE(alloc_size > REPLICAS_DELTA_LIST_MAX);
448 if (!d || d->used + more > d->size) {
449 d = krealloc(d, alloc_size, GFP_NOIO|__GFP_ZERO);
451 BUG_ON(!d && alloc_size > REPLICAS_DELTA_LIST_MAX);
454 d = mempool_alloc(&trans->c->replicas_delta_pool, GFP_NOIO);
455 memset(d, 0, REPLICAS_DELTA_LIST_MAX);
457 if (trans->fs_usage_deltas)
458 memcpy(d, trans->fs_usage_deltas,
459 trans->fs_usage_deltas->size + sizeof(*d));
461 new_size = REPLICAS_DELTA_LIST_MAX - sizeof(*d);
462 kfree(trans->fs_usage_deltas);
466 trans->fs_usage_deltas = d;
471 static inline void update_replicas_list(struct btree_trans *trans,
472 struct bch_replicas_entry *r,
475 struct replicas_delta_list *d;
476 struct replicas_delta *n;
482 b = replicas_entry_bytes(r) + 8;
483 d = replicas_deltas_realloc(trans, b);
485 n = (void *) d->d + d->used;
487 memcpy(&n->r, r, replicas_entry_bytes(r));
488 bch2_replicas_entry_sort(&n->r);
492 static inline void update_cached_sectors_list(struct btree_trans *trans,
493 unsigned dev, s64 sectors)
495 struct bch_replicas_padded r;
497 bch2_replicas_entry_cached(&r.e, dev);
499 update_replicas_list(trans, &r.e, sectors);
502 int bch2_mark_alloc(struct btree_trans *trans,
503 struct bkey_s_c old, struct bkey_s_c new,
506 bool gc = flags & BTREE_TRIGGER_GC;
507 u64 journal_seq = trans->journal_res.seq;
508 struct bch_fs *c = trans->c;
509 struct bkey_alloc_unpacked old_u = bch2_alloc_unpack(old);
510 struct bkey_alloc_unpacked new_u = bch2_alloc_unpack(new);
511 struct bch_dev *ca = bch_dev_bkey_exists(c, new_u.dev);
514 if (bch2_trans_inconsistent_on(new_u.bucket < ca->mi.first_bucket ||
515 new_u.bucket >= ca->mi.nbuckets, trans,
516 "alloc key outside range of device's buckets"))
520 * alloc btree is read in by bch2_alloc_read, not gc:
522 if ((flags & BTREE_TRIGGER_GC) &&
523 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
526 if ((flags & BTREE_TRIGGER_INSERT) &&
527 !old_u.data_type != !new_u.data_type &&
528 new.k->type == KEY_TYPE_alloc_v3) {
529 struct bch_alloc_v3 *v = (struct bch_alloc_v3 *) new.v;
530 u64 old_journal_seq = le64_to_cpu(v->journal_seq);
532 BUG_ON(!journal_seq);
535 * If the btree updates referring to a bucket weren't flushed
536 * before the bucket became empty again, then the we don't have
537 * to wait on a journal flush before we can reuse the bucket:
539 new_u.journal_seq = !new_u.data_type &&
540 (journal_seq == old_journal_seq ||
541 bch2_journal_noflush_seq(&c->journal, old_journal_seq))
543 v->journal_seq = cpu_to_le64(new_u.journal_seq);
546 if (old_u.data_type && !new_u.data_type && new_u.journal_seq) {
547 ret = bch2_set_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
548 c->journal.flushed_seq_ondisk,
549 new_u.dev, new_u.bucket,
552 bch2_fs_fatal_error(c,
553 "error setting bucket_needs_journal_commit: %i", ret);
558 if (!new_u.data_type &&
559 (!new_u.journal_seq || new_u.journal_seq < c->journal.flushed_seq_ondisk))
560 closure_wake_up(&c->freelist_wait);
562 if ((flags & BTREE_TRIGGER_INSERT) &&
563 new_u.need_discard &&
567 if (!old_u.data_type &&
569 should_invalidate_buckets(ca))
570 bch2_do_invalidates(c);
572 if (bucket_state(new_u) == BUCKET_need_gc_gens) {
573 atomic_inc(&c->kick_gc);
574 wake_up_process(c->gc_thread);
577 percpu_down_read(&c->mark_lock);
578 if (!gc && new_u.gen != old_u.gen)
579 *bucket_gen(ca, new_u.bucket) = new_u.gen;
581 bch2_dev_usage_update(c, ca, old_u, new_u, journal_seq, gc);
584 struct bucket *g = gc_bucket(ca, new_u.bucket);
590 g->data_type = new_u.data_type;
591 g->stripe = new_u.stripe;
592 g->stripe_redundancy = new_u.stripe_redundancy;
593 g->dirty_sectors = new_u.dirty_sectors;
594 g->cached_sectors = new_u.cached_sectors;
598 percpu_up_read(&c->mark_lock);
601 * need to know if we're getting called from the invalidate path or
605 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
606 old_u.cached_sectors) {
607 ret = update_cached_sectors(c, new, ca->dev_idx,
608 -old_u.cached_sectors,
611 bch2_fs_fatal_error(c, "bch2_mark_alloc(): no replicas entry while updating cached sectors");
615 trace_invalidate(ca, bucket_to_sector(ca, new_u.bucket),
616 old_u.cached_sectors);
622 void bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
623 size_t b, enum bch_data_type data_type,
624 unsigned sectors, struct gc_pos pos,
627 struct bucket old, new, *g;
630 BUG_ON(!(flags & BTREE_TRIGGER_GC));
631 BUG_ON(data_type != BCH_DATA_sb &&
632 data_type != BCH_DATA_journal);
635 * Backup superblock might be past the end of our normal usable space:
637 if (b >= ca->mi.nbuckets)
640 percpu_down_read(&c->mark_lock);
641 g = gc_bucket(ca, b);
646 g->data_type = data_type;
647 g->dirty_sectors += sectors;
648 overflow = g->dirty_sectors < sectors;
653 bch2_fs_inconsistent_on(old.data_type &&
654 old.data_type != data_type, c,
655 "different types of data in same bucket: %s, %s",
656 bch2_data_types[old.data_type],
657 bch2_data_types[data_type]);
659 bch2_fs_inconsistent_on(overflow, c,
660 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > U16_MAX",
661 ca->dev_idx, b, new.gen,
662 bch2_data_types[old.data_type ?: data_type],
663 old.dirty_sectors, sectors);
665 bch2_dev_usage_update_m(c, ca, old, new, 0, true);
666 percpu_up_read(&c->mark_lock);
669 static s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p)
671 EBUG_ON(sectors < 0);
673 return p.crc.compression_type &&
674 p.crc.compression_type != BCH_COMPRESSION_TYPE_incompressible
675 ? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size,
676 p.crc.uncompressed_size)
680 static int check_bucket_ref(struct bch_fs *c,
682 const struct bch_extent_ptr *ptr,
683 s64 sectors, enum bch_data_type ptr_data_type,
684 u8 b_gen, u8 bucket_data_type,
685 u32 dirty_sectors, u32 cached_sectors)
687 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
688 size_t bucket_nr = PTR_BUCKET_NR(ca, ptr);
689 u16 bucket_sectors = !ptr->cached
692 struct printbuf buf = PRINTBUF;
695 if (gen_after(ptr->gen, b_gen)) {
696 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
697 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
699 ptr->dev, bucket_nr, b_gen,
700 bch2_data_types[bucket_data_type ?: ptr_data_type],
702 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
707 if (gen_cmp(b_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
708 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
709 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
711 ptr->dev, bucket_nr, b_gen,
712 bch2_data_types[bucket_data_type ?: ptr_data_type],
714 (printbuf_reset(&buf),
715 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
720 if (b_gen != ptr->gen && !ptr->cached) {
721 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
722 "bucket %u:%zu gen %u (mem gen %u) data type %s: stale dirty ptr (gen %u)\n"
724 ptr->dev, bucket_nr, b_gen,
725 *bucket_gen(ca, bucket_nr),
726 bch2_data_types[bucket_data_type ?: ptr_data_type],
728 (printbuf_reset(&buf),
729 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
734 if (b_gen != ptr->gen) {
739 if (bucket_data_type && ptr_data_type &&
740 bucket_data_type != ptr_data_type) {
741 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
742 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
744 ptr->dev, bucket_nr, b_gen,
745 bch2_data_types[bucket_data_type],
746 bch2_data_types[ptr_data_type],
747 (printbuf_reset(&buf),
748 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
753 if ((unsigned) (bucket_sectors + sectors) > U32_MAX) {
754 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
755 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
757 ptr->dev, bucket_nr, b_gen,
758 bch2_data_types[bucket_data_type ?: ptr_data_type],
759 bucket_sectors, sectors,
760 (printbuf_reset(&buf),
761 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
770 static int mark_stripe_bucket(struct btree_trans *trans,
775 struct bch_fs *c = trans->c;
776 u64 journal_seq = trans->journal_res.seq;
777 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
778 unsigned nr_data = s->nr_blocks - s->nr_redundant;
779 bool parity = ptr_idx >= nr_data;
780 enum bch_data_type data_type = parity ? BCH_DATA_parity : 0;
781 s64 sectors = parity ? le16_to_cpu(s->sectors) : 0;
782 const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
783 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
784 struct bucket old, new, *g;
785 struct printbuf buf = PRINTBUF;
788 BUG_ON(!(flags & BTREE_TRIGGER_GC));
790 /* * XXX doesn't handle deletion */
792 percpu_down_read(&c->mark_lock);
794 g = PTR_GC_BUCKET(ca, ptr);
796 if (g->dirty_sectors ||
797 (g->stripe && g->stripe != k.k->p.offset)) {
798 bch2_fs_inconsistent(c,
799 "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
800 ptr->dev, PTR_BUCKET_NR(ca, ptr), g->gen,
801 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
809 ret = check_bucket_ref(c, k, ptr, sectors, data_type,
810 new.gen, new.data_type,
811 new.dirty_sectors, new.cached_sectors);
817 new.dirty_sectors += sectors;
819 new.data_type = data_type;
821 g->stripe = k.k->p.offset;
822 g->stripe_redundancy = s->nr_redundant;
827 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
829 percpu_up_read(&c->mark_lock);
834 static int __mark_pointer(struct btree_trans *trans,
836 const struct bch_extent_ptr *ptr,
837 s64 sectors, enum bch_data_type ptr_data_type,
838 u8 bucket_gen, u8 *bucket_data_type,
839 u32 *dirty_sectors, u32 *cached_sectors)
841 u32 *dst_sectors = !ptr->cached
844 int ret = check_bucket_ref(trans->c, k, ptr, sectors, ptr_data_type,
845 bucket_gen, *bucket_data_type,
846 *dirty_sectors, *cached_sectors);
851 *dst_sectors += sectors;
852 *bucket_data_type = *dirty_sectors || *cached_sectors
857 static int bch2_mark_pointer(struct btree_trans *trans,
859 struct extent_ptr_decoded p,
860 s64 sectors, enum bch_data_type data_type,
863 u64 journal_seq = trans->journal_res.seq;
864 struct bch_fs *c = trans->c;
865 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
866 struct bucket old, new, *g;
870 BUG_ON(!(flags & BTREE_TRIGGER_GC));
872 percpu_down_read(&c->mark_lock);
873 g = PTR_GC_BUCKET(ca, &p.ptr);
878 bucket_data_type = g->data_type;
880 ret = __mark_pointer(trans, k, &p.ptr, sectors,
890 g->data_type = bucket_data_type;
895 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
897 percpu_up_read(&c->mark_lock);
902 static int bch2_mark_stripe_ptr(struct btree_trans *trans,
904 struct bch_extent_stripe_ptr p,
905 enum bch_data_type data_type,
909 struct bch_fs *c = trans->c;
910 struct bch_replicas_padded r;
913 BUG_ON(!(flags & BTREE_TRIGGER_GC));
915 m = genradix_ptr_alloc(&c->gc_stripes, p.idx, GFP_KERNEL);
917 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
922 spin_lock(&c->ec_stripes_heap_lock);
924 if (!m || !m->alive) {
925 spin_unlock(&c->ec_stripes_heap_lock);
926 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
928 bch2_inconsistent_error(c);
932 m->block_sectors[p.block] += sectors;
935 spin_unlock(&c->ec_stripes_heap_lock);
937 r.e.data_type = data_type;
938 update_replicas(c, k, &r.e, sectors, trans->journal_res.seq, true);
943 int bch2_mark_extent(struct btree_trans *trans,
944 struct bkey_s_c old, struct bkey_s_c new,
947 u64 journal_seq = trans->journal_res.seq;
948 struct bch_fs *c = trans->c;
949 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
950 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
951 const union bch_extent_entry *entry;
952 struct extent_ptr_decoded p;
953 struct bch_replicas_padded r;
954 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
957 s64 sectors = bkey_is_btree_ptr(k.k)
960 s64 dirty_sectors = 0;
964 BUG_ON(!(flags & BTREE_TRIGGER_GC));
966 r.e.data_type = data_type;
970 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
971 s64 disk_sectors = ptr_disk_sectors(sectors, p);
973 if (flags & BTREE_TRIGGER_OVERWRITE)
974 disk_sectors = -disk_sectors;
976 ret = bch2_mark_pointer(trans, k, p, disk_sectors,
985 ret = update_cached_sectors(c, k, p.ptr.dev,
986 disk_sectors, journal_seq, true);
988 bch2_fs_fatal_error(c, "bch2_mark_extent(): no replicas entry while updating cached sectors");
992 } else if (!p.has_ec) {
993 dirty_sectors += disk_sectors;
994 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
996 ret = bch2_mark_stripe_ptr(trans, k, p.ec, data_type,
997 disk_sectors, flags);
1002 * There may be other dirty pointers in this extent, but
1003 * if so they're not required for mounting if we have an
1004 * erasure coded pointer in this extent:
1006 r.e.nr_required = 0;
1011 ret = update_replicas(c, k, &r.e, dirty_sectors, journal_seq, true);
1013 struct printbuf buf = PRINTBUF;
1015 bch2_bkey_val_to_text(&buf, c, k);
1016 bch2_fs_fatal_error(c, "no replicas entry for %s", buf.buf);
1017 printbuf_exit(&buf);
1025 int bch2_mark_stripe(struct btree_trans *trans,
1026 struct bkey_s_c old, struct bkey_s_c new,
1029 bool gc = flags & BTREE_TRIGGER_GC;
1030 u64 journal_seq = trans->journal_res.seq;
1031 struct bch_fs *c = trans->c;
1032 u64 idx = new.k->p.offset;
1033 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
1034 ? bkey_s_c_to_stripe(old).v : NULL;
1035 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
1036 ? bkey_s_c_to_stripe(new).v : NULL;
1040 BUG_ON(gc && old_s);
1043 struct stripe *m = genradix_ptr(&c->stripes, idx);
1045 if (!m || (old_s && !m->alive)) {
1046 struct printbuf buf1 = PRINTBUF;
1047 struct printbuf buf2 = PRINTBUF;
1049 bch2_bkey_val_to_text(&buf1, c, old);
1050 bch2_bkey_val_to_text(&buf2, c, new);
1051 bch_err_ratelimited(c, "error marking nonexistent stripe %llu while marking\n"
1053 "new %s", idx, buf1.buf, buf2.buf);
1054 printbuf_exit(&buf2);
1055 printbuf_exit(&buf1);
1056 bch2_inconsistent_error(c);
1061 spin_lock(&c->ec_stripes_heap_lock);
1062 bch2_stripes_heap_del(c, m, idx);
1063 spin_unlock(&c->ec_stripes_heap_lock);
1065 memset(m, 0, sizeof(*m));
1068 m->sectors = le16_to_cpu(new_s->sectors);
1069 m->algorithm = new_s->algorithm;
1070 m->nr_blocks = new_s->nr_blocks;
1071 m->nr_redundant = new_s->nr_redundant;
1072 m->blocks_nonempty = 0;
1074 for (i = 0; i < new_s->nr_blocks; i++)
1075 m->blocks_nonempty += !!stripe_blockcount_get(new_s, i);
1077 spin_lock(&c->ec_stripes_heap_lock);
1078 bch2_stripes_heap_update(c, m, idx);
1079 spin_unlock(&c->ec_stripes_heap_lock);
1082 struct gc_stripe *m =
1083 genradix_ptr_alloc(&c->gc_stripes, idx, GFP_KERNEL);
1086 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
1091 * This will be wrong when we bring back runtime gc: we should
1092 * be unmarking the old key and then marking the new key
1095 m->sectors = le16_to_cpu(new_s->sectors);
1096 m->nr_blocks = new_s->nr_blocks;
1097 m->nr_redundant = new_s->nr_redundant;
1099 for (i = 0; i < new_s->nr_blocks; i++)
1100 m->ptrs[i] = new_s->ptrs[i];
1102 bch2_bkey_to_replicas(&m->r.e, new);
1105 * gc recalculates this field from stripe ptr
1108 memset(m->block_sectors, 0, sizeof(m->block_sectors));
1110 for (i = 0; i < new_s->nr_blocks; i++) {
1111 ret = mark_stripe_bucket(trans, new, i, flags);
1116 ret = update_replicas(c, new, &m->r.e,
1117 ((s64) m->sectors * m->nr_redundant),
1120 struct printbuf buf = PRINTBUF;
1122 bch2_bkey_val_to_text(&buf, c, new);
1123 bch2_fs_fatal_error(c, "no replicas entry for %s", buf.buf);
1124 printbuf_exit(&buf);
1132 int bch2_mark_inode(struct btree_trans *trans,
1133 struct bkey_s_c old, struct bkey_s_c new,
1136 struct bch_fs *c = trans->c;
1137 struct bch_fs_usage __percpu *fs_usage;
1138 u64 journal_seq = trans->journal_res.seq;
1140 if (flags & BTREE_TRIGGER_INSERT) {
1141 struct bch_inode_v2 *v = (struct bch_inode_v2 *) new.v;
1143 BUG_ON(!journal_seq);
1144 BUG_ON(new.k->type != KEY_TYPE_inode_v2);
1146 v->bi_journal_seq = cpu_to_le64(journal_seq);
1149 if (flags & BTREE_TRIGGER_GC) {
1150 percpu_down_read(&c->mark_lock);
1153 fs_usage = fs_usage_ptr(c, journal_seq, flags & BTREE_TRIGGER_GC);
1154 fs_usage->nr_inodes += bkey_is_inode(new.k);
1155 fs_usage->nr_inodes -= bkey_is_inode(old.k);
1158 percpu_up_read(&c->mark_lock);
1163 int bch2_mark_reservation(struct btree_trans *trans,
1164 struct bkey_s_c old, struct bkey_s_c new,
1167 struct bch_fs *c = trans->c;
1168 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
1169 struct bch_fs_usage __percpu *fs_usage;
1170 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1171 s64 sectors = (s64) k.k->size;
1173 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1175 if (flags & BTREE_TRIGGER_OVERWRITE)
1177 sectors *= replicas;
1179 percpu_down_read(&c->mark_lock);
1182 fs_usage = fs_usage_ptr(c, trans->journal_res.seq, flags & BTREE_TRIGGER_GC);
1183 replicas = clamp_t(unsigned, replicas, 1,
1184 ARRAY_SIZE(fs_usage->persistent_reserved));
1186 fs_usage->reserved += sectors;
1187 fs_usage->persistent_reserved[replicas - 1] += sectors;
1190 percpu_up_read(&c->mark_lock);
1195 static s64 __bch2_mark_reflink_p(struct btree_trans *trans,
1196 struct bkey_s_c_reflink_p p,
1198 u64 *idx, unsigned flags, size_t r_idx)
1200 struct bch_fs *c = trans->c;
1201 struct reflink_gc *r;
1202 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1205 struct printbuf buf = PRINTBUF;
1207 if (r_idx >= c->reflink_gc_nr)
1210 r = genradix_ptr(&c->reflink_gc_table, r_idx);
1211 next_idx = min(next_idx, r->offset - r->size);
1212 if (*idx < next_idx)
1215 BUG_ON((s64) r->refcount + add < 0);
1221 if (fsck_err(c, "pointer to missing indirect extent\n"
1223 " missing range %llu-%llu",
1224 (bch2_bkey_val_to_text(&buf, c, p.s_c), buf.buf),
1226 struct bkey_i_error new;
1229 new.k.type = KEY_TYPE_error;
1230 new.k.p = bkey_start_pos(p.k);
1231 new.k.p.offset += *idx - start;
1232 bch2_key_resize(&new.k, next_idx - *idx);
1233 ret = __bch2_btree_insert(trans, BTREE_ID_extents, &new.k_i);
1238 printbuf_exit(&buf);
1242 int bch2_mark_reflink_p(struct btree_trans *trans,
1243 struct bkey_s_c old, struct bkey_s_c new,
1246 struct bch_fs *c = trans->c;
1247 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
1248 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1249 struct reflink_gc *ref;
1251 u64 idx = le64_to_cpu(p.v->idx), start = idx;
1252 u64 end = le64_to_cpu(p.v->idx) + p.k->size;
1255 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1257 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix) {
1258 idx -= le32_to_cpu(p.v->front_pad);
1259 end += le32_to_cpu(p.v->back_pad);
1263 r = c->reflink_gc_nr;
1265 m = l + (r - l) / 2;
1267 ref = genradix_ptr(&c->reflink_gc_table, m);
1268 if (ref->offset <= idx)
1274 while (idx < end && !ret)
1275 ret = __bch2_mark_reflink_p(trans, p, start, end,
1281 static noinline __cold
1282 void fs_usage_apply_warn(struct btree_trans *trans,
1283 unsigned disk_res_sectors,
1284 s64 should_not_have_added)
1286 struct bch_fs *c = trans->c;
1287 struct btree_insert_entry *i;
1288 struct printbuf buf = PRINTBUF;
1290 bch_err(c, "disk usage increased %lli more than %u sectors reserved",
1291 should_not_have_added, disk_res_sectors);
1293 trans_for_each_update(trans, i) {
1294 struct bkey_s_c old = { &i->old_k, i->old_v };
1296 pr_err("while inserting");
1297 printbuf_reset(&buf);
1298 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(i->k));
1299 pr_err(" %s", buf.buf);
1300 pr_err("overlapping with");
1301 printbuf_reset(&buf);
1302 bch2_bkey_val_to_text(&buf, c, old);
1303 pr_err(" %s", buf.buf);
1307 printbuf_exit(&buf);
1310 int bch2_trans_fs_usage_apply(struct btree_trans *trans,
1311 struct replicas_delta_list *deltas)
1313 struct bch_fs *c = trans->c;
1314 static int warned_disk_usage = 0;
1316 unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1317 struct replicas_delta *d = deltas->d, *d2;
1318 struct replicas_delta *top = (void *) deltas->d + deltas->used;
1319 struct bch_fs_usage *dst;
1320 s64 added = 0, should_not_have_added;
1323 percpu_down_read(&c->mark_lock);
1325 dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1327 for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1328 switch (d->r.data_type) {
1329 case BCH_DATA_btree:
1331 case BCH_DATA_parity:
1335 if (__update_replicas(c, dst, &d->r, d->delta))
1339 dst->nr_inodes += deltas->nr_inodes;
1341 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1342 added += deltas->persistent_reserved[i];
1343 dst->reserved += deltas->persistent_reserved[i];
1344 dst->persistent_reserved[i] += deltas->persistent_reserved[i];
1348 * Not allowed to reduce sectors_available except by getting a
1351 should_not_have_added = added - (s64) disk_res_sectors;
1352 if (unlikely(should_not_have_added > 0)) {
1353 u64 old, new, v = atomic64_read(&c->sectors_available);
1357 new = max_t(s64, 0, old - should_not_have_added);
1358 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1361 added -= should_not_have_added;
1366 trans->disk_res->sectors -= added;
1367 this_cpu_sub(*c->online_reserved, added);
1371 percpu_up_read(&c->mark_lock);
1373 if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
1374 fs_usage_apply_warn(trans, disk_res_sectors, should_not_have_added);
1377 /* revert changes: */
1378 for (d2 = deltas->d; d2 != d; d2 = replicas_delta_next(d2))
1379 BUG_ON(__update_replicas(c, dst, &d2->r, -d2->delta));
1382 percpu_up_read(&c->mark_lock);
1388 static int bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
1389 const struct bch_extent_ptr *ptr,
1390 struct bkey_alloc_unpacked *u)
1392 struct bch_fs *c = trans->c;
1393 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
1397 bch2_trans_iter_init(trans, iter, BTREE_ID_alloc,
1398 POS(ptr->dev, PTR_BUCKET_NR(ca, ptr)),
1399 BTREE_ITER_WITH_UPDATES|
1402 k = bch2_btree_iter_peek_slot(iter);
1405 bch2_trans_iter_exit(trans, iter);
1409 *u = bch2_alloc_unpack(k);
1413 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1414 struct bkey_s_c k, struct extent_ptr_decoded p,
1415 s64 sectors, enum bch_data_type data_type)
1417 struct btree_iter iter;
1418 struct bkey_alloc_unpacked u;
1421 ret = bch2_trans_start_alloc_update(trans, &iter, &p.ptr, &u);
1425 ret = __mark_pointer(trans, k, &p.ptr, sectors, data_type,
1426 u.gen, &u.data_type,
1427 &u.dirty_sectors, &u.cached_sectors);
1431 ret = bch2_alloc_write(trans, &iter, &u, 0);
1435 bch2_trans_iter_exit(trans, &iter);
1439 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1440 struct extent_ptr_decoded p,
1441 s64 sectors, enum bch_data_type data_type)
1443 struct btree_iter iter;
1445 struct bkey_i_stripe *s;
1446 struct bch_replicas_padded r;
1449 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, POS(0, p.ec.idx),
1451 BTREE_ITER_WITH_UPDATES);
1452 k = bch2_btree_iter_peek_slot(&iter);
1457 if (k.k->type != KEY_TYPE_stripe) {
1458 bch2_trans_inconsistent(trans,
1459 "pointer to nonexistent stripe %llu",
1465 if (!bch2_ptr_matches_stripe(bkey_s_c_to_stripe(k).v, p)) {
1466 bch2_trans_inconsistent(trans,
1467 "stripe pointer doesn't match stripe %llu",
1473 s = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1474 ret = PTR_ERR_OR_ZERO(s);
1478 bkey_reassemble(&s->k_i, k);
1479 stripe_blockcount_set(&s->v, p.ec.block,
1480 stripe_blockcount_get(&s->v, p.ec.block) +
1483 ret = bch2_trans_update(trans, &iter, &s->k_i, 0);
1487 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1488 r.e.data_type = data_type;
1489 update_replicas_list(trans, &r.e, sectors);
1491 bch2_trans_iter_exit(trans, &iter);
1495 int bch2_trans_mark_extent(struct btree_trans *trans,
1496 struct bkey_s_c old, struct bkey_i *new,
1499 struct bch_fs *c = trans->c;
1500 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1502 : bkey_i_to_s_c(new);
1503 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1504 const union bch_extent_entry *entry;
1505 struct extent_ptr_decoded p;
1506 struct bch_replicas_padded r;
1507 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
1510 s64 sectors = bkey_is_btree_ptr(k.k)
1513 s64 dirty_sectors = 0;
1517 r.e.data_type = data_type;
1519 r.e.nr_required = 1;
1521 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1522 s64 disk_sectors = ptr_disk_sectors(sectors, p);
1524 if (flags & BTREE_TRIGGER_OVERWRITE)
1525 disk_sectors = -disk_sectors;
1527 ret = bch2_trans_mark_pointer(trans, k, p,
1528 disk_sectors, data_type);
1536 update_cached_sectors_list(trans, p.ptr.dev,
1538 } else if (!p.has_ec) {
1539 dirty_sectors += disk_sectors;
1540 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1542 ret = bch2_trans_mark_stripe_ptr(trans, p,
1543 disk_sectors, data_type);
1547 r.e.nr_required = 0;
1552 update_replicas_list(trans, &r.e, dirty_sectors);
1557 static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
1558 struct bkey_s_c_stripe s,
1559 unsigned idx, bool deleting)
1561 struct bch_fs *c = trans->c;
1562 const struct bch_extent_ptr *ptr = &s.v->ptrs[idx];
1563 struct btree_iter iter;
1564 struct bkey_alloc_unpacked u;
1565 enum bch_data_type data_type = idx >= s.v->nr_blocks - s.v->nr_redundant
1566 ? BCH_DATA_parity : 0;
1567 s64 sectors = data_type ? le16_to_cpu(s.v->sectors) : 0;
1573 ret = bch2_trans_start_alloc_update(trans, &iter, ptr, &u);
1577 ret = check_bucket_ref(c, s.s_c, ptr, sectors, data_type,
1579 u.dirty_sectors, u.cached_sectors);
1584 if (bch2_trans_inconsistent_on(u.stripe ||
1585 u.stripe_redundancy, trans,
1586 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)",
1587 iter.pos.inode, iter.pos.offset, u.gen,
1588 bch2_data_types[u.data_type],
1590 u.stripe, s.k->p.offset)) {
1595 if (bch2_trans_inconsistent_on(data_type && u.dirty_sectors, trans,
1596 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: data already in stripe bucket %llu",
1597 iter.pos.inode, iter.pos.offset, u.gen,
1598 bch2_data_types[u.data_type],
1605 u.stripe = s.k->p.offset;
1606 u.stripe_redundancy = s.v->nr_redundant;
1608 if (bch2_trans_inconsistent_on(u.stripe != s.k->p.offset ||
1609 u.stripe_redundancy != s.v->nr_redundant, trans,
1610 "bucket %llu:%llu gen %u: not marked as stripe when deleting stripe %llu (got %u)",
1611 iter.pos.inode, iter.pos.offset, u.gen,
1612 s.k->p.offset, u.stripe)) {
1618 u.stripe_redundancy = 0;
1621 u.dirty_sectors += sectors;
1623 u.data_type = !deleting ? data_type : 0;
1625 ret = bch2_alloc_write(trans, &iter, &u, 0);
1629 bch2_trans_iter_exit(trans, &iter);
1633 int bch2_trans_mark_stripe(struct btree_trans *trans,
1634 struct bkey_s_c old, struct bkey_i *new,
1637 const struct bch_stripe *old_s = NULL;
1638 struct bch_stripe *new_s = NULL;
1639 struct bch_replicas_padded r;
1640 unsigned i, nr_blocks;
1643 if (old.k->type == KEY_TYPE_stripe)
1644 old_s = bkey_s_c_to_stripe(old).v;
1645 if (new->k.type == KEY_TYPE_stripe)
1646 new_s = &bkey_i_to_stripe(new)->v;
1649 * If the pointers aren't changing, we don't need to do anything:
1651 if (new_s && old_s &&
1652 new_s->nr_blocks == old_s->nr_blocks &&
1653 new_s->nr_redundant == old_s->nr_redundant &&
1654 !memcmp(old_s->ptrs, new_s->ptrs,
1655 new_s->nr_blocks * sizeof(struct bch_extent_ptr)))
1658 BUG_ON(new_s && old_s &&
1659 (new_s->nr_blocks != old_s->nr_blocks ||
1660 new_s->nr_redundant != old_s->nr_redundant));
1662 nr_blocks = new_s ? new_s->nr_blocks : old_s->nr_blocks;
1665 s64 sectors = le16_to_cpu(new_s->sectors);
1667 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(new));
1668 update_replicas_list(trans, &r.e, sectors * new_s->nr_redundant);
1672 s64 sectors = -((s64) le16_to_cpu(old_s->sectors));
1674 bch2_bkey_to_replicas(&r.e, old);
1675 update_replicas_list(trans, &r.e, sectors * old_s->nr_redundant);
1678 for (i = 0; i < nr_blocks; i++) {
1679 if (new_s && old_s &&
1680 !memcmp(&new_s->ptrs[i],
1682 sizeof(new_s->ptrs[i])))
1686 ret = bch2_trans_mark_stripe_bucket(trans,
1687 bkey_i_to_s_c_stripe(new), i, false);
1693 ret = bch2_trans_mark_stripe_bucket(trans,
1694 bkey_s_c_to_stripe(old), i, true);
1703 int bch2_trans_mark_inode(struct btree_trans *trans,
1704 struct bkey_s_c old,
1708 int nr = bkey_is_inode(&new->k) - bkey_is_inode(old.k);
1711 struct replicas_delta_list *d =
1712 replicas_deltas_realloc(trans, 0);
1719 int bch2_trans_mark_reservation(struct btree_trans *trans,
1720 struct bkey_s_c old,
1724 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1726 : bkey_i_to_s_c(new);
1727 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1728 s64 sectors = (s64) k.k->size;
1729 struct replicas_delta_list *d;
1731 if (flags & BTREE_TRIGGER_OVERWRITE)
1733 sectors *= replicas;
1735 d = replicas_deltas_realloc(trans, 0);
1737 replicas = clamp_t(unsigned, replicas, 1,
1738 ARRAY_SIZE(d->persistent_reserved));
1740 d->persistent_reserved[replicas - 1] += sectors;
1744 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1745 struct bkey_s_c_reflink_p p,
1746 u64 *idx, unsigned flags)
1748 struct bch_fs *c = trans->c;
1749 struct btree_iter iter;
1753 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1754 struct printbuf buf = PRINTBUF;
1757 bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink, POS(0, *idx),
1759 BTREE_ITER_WITH_UPDATES);
1760 k = bch2_btree_iter_peek_slot(&iter);
1765 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1766 ret = PTR_ERR_OR_ZERO(n);
1770 bkey_reassemble(n, k);
1772 refcount = bkey_refcount(n);
1774 bch2_bkey_val_to_text(&buf, c, p.s_c);
1775 bch2_trans_inconsistent(trans,
1776 "nonexistent indirect extent at %llu while marking\n %s",
1782 if (!*refcount && (flags & BTREE_TRIGGER_OVERWRITE)) {
1783 bch2_bkey_val_to_text(&buf, c, p.s_c);
1784 bch2_trans_inconsistent(trans,
1785 "indirect extent refcount underflow at %llu while marking\n %s",
1791 if (flags & BTREE_TRIGGER_INSERT) {
1792 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1795 pad = max_t(s64, le32_to_cpu(v->front_pad),
1796 le64_to_cpu(v->idx) - bkey_start_offset(k.k));
1797 BUG_ON(pad > U32_MAX);
1798 v->front_pad = cpu_to_le32(pad);
1800 pad = max_t(s64, le32_to_cpu(v->back_pad),
1801 k.k->p.offset - p.k->size - le64_to_cpu(v->idx));
1802 BUG_ON(pad > U32_MAX);
1803 v->back_pad = cpu_to_le32(pad);
1806 le64_add_cpu(refcount, add);
1809 n->k.type = KEY_TYPE_deleted;
1810 set_bkey_val_u64s(&n->k, 0);
1813 bch2_btree_iter_set_pos_to_extent_start(&iter);
1814 ret = bch2_trans_update(trans, &iter, n, 0);
1818 *idx = k.k->p.offset;
1820 bch2_trans_iter_exit(trans, &iter);
1821 printbuf_exit(&buf);
1825 int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1826 struct bkey_s_c old,
1830 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1832 : bkey_i_to_s_c(new);
1833 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1837 if (flags & BTREE_TRIGGER_INSERT) {
1838 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1840 v->front_pad = v->back_pad = 0;
1843 idx = le64_to_cpu(p.v->idx) - le32_to_cpu(p.v->front_pad);
1844 end_idx = le64_to_cpu(p.v->idx) + p.k->size +
1845 le32_to_cpu(p.v->back_pad);
1847 while (idx < end_idx && !ret)
1848 ret = __bch2_trans_mark_reflink_p(trans, p, &idx, flags);
1853 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1854 struct bch_dev *ca, size_t b,
1855 enum bch_data_type type,
1858 struct bch_fs *c = trans->c;
1859 struct btree_iter iter;
1860 struct bkey_alloc_unpacked u;
1861 struct bch_extent_ptr ptr = {
1863 .offset = bucket_to_sector(ca, b),
1868 * Backup superblock might be past the end of our normal usable space:
1870 if (b >= ca->mi.nbuckets)
1873 ret = bch2_trans_start_alloc_update(trans, &iter, &ptr, &u);
1877 if (u.data_type && u.data_type != type) {
1878 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1879 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
1881 iter.pos.inode, iter.pos.offset, u.gen,
1882 bch2_data_types[u.data_type],
1883 bch2_data_types[type],
1884 bch2_data_types[type]);
1890 u.dirty_sectors = sectors;
1892 ret = bch2_alloc_write(trans, &iter, &u, 0);
1896 bch2_trans_iter_exit(trans, &iter);
1900 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1901 struct bch_dev *ca, size_t b,
1902 enum bch_data_type type,
1905 return __bch2_trans_do(trans, NULL, NULL, 0,
1906 __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors));
1909 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans,
1912 enum bch_data_type type,
1913 u64 *bucket, unsigned *bucket_sectors)
1916 u64 b = sector_to_bucket(ca, start);
1918 min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
1920 if (b != *bucket && *bucket_sectors) {
1921 int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket,
1922 type, *bucket_sectors);
1926 *bucket_sectors = 0;
1930 *bucket_sectors += sectors;
1932 } while (start < end);
1937 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
1940 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
1942 unsigned i, bucket_sectors = 0;
1945 for (i = 0; i < layout->nr_superblocks; i++) {
1946 u64 offset = le64_to_cpu(layout->sb_offset[i]);
1948 if (offset == BCH_SB_SECTOR) {
1949 ret = bch2_trans_mark_metadata_sectors(trans, ca,
1951 BCH_DATA_sb, &bucket, &bucket_sectors);
1956 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset,
1957 offset + (1 << layout->sb_max_size_bits),
1958 BCH_DATA_sb, &bucket, &bucket_sectors);
1963 if (bucket_sectors) {
1964 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1965 bucket, BCH_DATA_sb, bucket_sectors);
1970 for (i = 0; i < ca->journal.nr; i++) {
1971 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1972 ca->journal.buckets[i],
1973 BCH_DATA_journal, ca->mi.bucket_size);
1981 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
1983 return bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
1984 __bch2_trans_mark_dev_sb(&trans, ca));
1987 /* Disk reservations: */
1989 #define SECTORS_CACHE 1024
1991 int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
1992 u64 sectors, int flags)
1994 struct bch_fs_pcpu *pcpu;
1996 s64 sectors_available;
1999 percpu_down_read(&c->mark_lock);
2001 pcpu = this_cpu_ptr(c->pcpu);
2003 if (sectors <= pcpu->sectors_available)
2006 v = atomic64_read(&c->sectors_available);
2009 get = min((u64) sectors + SECTORS_CACHE, old);
2011 if (get < sectors) {
2015 } while ((v = atomic64_cmpxchg(&c->sectors_available,
2016 old, old - get)) != old);
2018 pcpu->sectors_available += get;
2021 pcpu->sectors_available -= sectors;
2022 this_cpu_add(*c->online_reserved, sectors);
2023 res->sectors += sectors;
2026 percpu_up_read(&c->mark_lock);
2030 mutex_lock(&c->sectors_available_lock);
2032 percpu_u64_set(&c->pcpu->sectors_available, 0);
2033 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
2035 if (sectors <= sectors_available ||
2036 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
2037 atomic64_set(&c->sectors_available,
2038 max_t(s64, 0, sectors_available - sectors));
2039 this_cpu_add(*c->online_reserved, sectors);
2040 res->sectors += sectors;
2043 atomic64_set(&c->sectors_available, sectors_available);
2047 mutex_unlock(&c->sectors_available_lock);
2048 percpu_up_read(&c->mark_lock);
2053 /* Startup/shutdown: */
2055 static void bucket_gens_free_rcu(struct rcu_head *rcu)
2057 struct bucket_gens *buckets =
2058 container_of(rcu, struct bucket_gens, rcu);
2060 kvpfree(buckets, sizeof(*buckets) + buckets->nbuckets);
2063 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
2065 struct bucket_gens *bucket_gens = NULL, *old_bucket_gens = NULL;
2066 unsigned long *buckets_nouse = NULL;
2067 bool resize = ca->bucket_gens != NULL;
2070 if (!(bucket_gens = kvpmalloc(sizeof(struct bucket_gens) + nbuckets,
2071 GFP_KERNEL|__GFP_ZERO)) ||
2072 (c->opts.buckets_nouse &&
2073 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2074 sizeof(unsigned long),
2075 GFP_KERNEL|__GFP_ZERO))))
2078 bucket_gens->first_bucket = ca->mi.first_bucket;
2079 bucket_gens->nbuckets = nbuckets;
2081 bch2_copygc_stop(c);
2084 down_write(&c->gc_lock);
2085 down_write(&ca->bucket_lock);
2086 percpu_down_write(&c->mark_lock);
2089 old_bucket_gens = rcu_dereference_protected(ca->bucket_gens, 1);
2092 size_t n = min(bucket_gens->nbuckets, old_bucket_gens->nbuckets);
2094 memcpy(bucket_gens->b,
2098 memcpy(buckets_nouse,
2100 BITS_TO_LONGS(n) * sizeof(unsigned long));
2103 rcu_assign_pointer(ca->bucket_gens, bucket_gens);
2104 bucket_gens = old_bucket_gens;
2106 swap(ca->buckets_nouse, buckets_nouse);
2108 nbuckets = ca->mi.nbuckets;
2111 percpu_up_write(&c->mark_lock);
2112 up_write(&ca->bucket_lock);
2113 up_write(&c->gc_lock);
2118 kvpfree(buckets_nouse,
2119 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2121 call_rcu(&bucket_gens->rcu, bucket_gens_free_rcu);
2126 void bch2_dev_buckets_free(struct bch_dev *ca)
2130 kvpfree(ca->buckets_nouse,
2131 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2132 kvpfree(rcu_dereference_protected(ca->bucket_gens, 1),
2133 sizeof(struct bucket_gens) + ca->mi.nbuckets);
2135 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
2136 free_percpu(ca->usage[i]);
2137 kfree(ca->usage_base);
2140 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2144 ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL);
2145 if (!ca->usage_base)
2148 for (i = 0; i < ARRAY_SIZE(ca->usage); i++) {
2149 ca->usage[i] = alloc_percpu(struct bch_dev_usage);
2154 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);;