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 fsck_err(c, "no replicas entry\n"
383 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
384 percpu_up_read(&c->mark_lock);
385 ret = bch2_mark_replicas(c, r);
386 percpu_down_read(&c->mark_lock);
390 idx = bch2_replicas_entry_idx(c, r);
398 fs_usage = fs_usage_ptr(c, journal_seq, gc);
399 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
400 fs_usage->replicas[idx] += sectors;
404 percpu_up_read(&c->mark_lock);
409 static inline int update_cached_sectors(struct bch_fs *c,
411 unsigned dev, s64 sectors,
412 unsigned journal_seq, bool gc)
414 struct bch_replicas_padded r;
416 bch2_replicas_entry_cached(&r.e, dev);
418 return update_replicas(c, k, &r.e, sectors, journal_seq, gc);
421 static struct replicas_delta_list *
422 replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
424 struct replicas_delta_list *d = trans->fs_usage_deltas;
425 unsigned new_size = d ? (d->size + more) * 2 : 128;
426 unsigned alloc_size = sizeof(*d) + new_size;
428 WARN_ON_ONCE(alloc_size > REPLICAS_DELTA_LIST_MAX);
430 if (!d || d->used + more > d->size) {
431 d = krealloc(d, alloc_size, GFP_NOIO|__GFP_ZERO);
433 BUG_ON(!d && alloc_size > REPLICAS_DELTA_LIST_MAX);
436 d = mempool_alloc(&trans->c->replicas_delta_pool, GFP_NOIO);
437 memset(d, 0, REPLICAS_DELTA_LIST_MAX);
439 if (trans->fs_usage_deltas)
440 memcpy(d, trans->fs_usage_deltas,
441 trans->fs_usage_deltas->size + sizeof(*d));
443 new_size = REPLICAS_DELTA_LIST_MAX - sizeof(*d);
444 kfree(trans->fs_usage_deltas);
448 trans->fs_usage_deltas = d;
453 static inline void update_replicas_list(struct btree_trans *trans,
454 struct bch_replicas_entry *r,
457 struct replicas_delta_list *d;
458 struct replicas_delta *n;
464 b = replicas_entry_bytes(r) + 8;
465 d = replicas_deltas_realloc(trans, b);
467 n = (void *) d->d + d->used;
469 memcpy(&n->r, r, replicas_entry_bytes(r));
470 bch2_replicas_entry_sort(&n->r);
474 static inline void update_cached_sectors_list(struct btree_trans *trans,
475 unsigned dev, s64 sectors)
477 struct bch_replicas_padded r;
479 bch2_replicas_entry_cached(&r.e, dev);
481 update_replicas_list(trans, &r.e, sectors);
484 int bch2_mark_alloc(struct btree_trans *trans,
485 struct bkey_s_c old, struct bkey_s_c new,
488 bool gc = flags & BTREE_TRIGGER_GC;
489 u64 journal_seq = trans->journal_res.seq;
490 struct bch_fs *c = trans->c;
491 struct bch_alloc_v4 old_a, new_a;
496 * alloc btree is read in by bch2_alloc_read, not gc:
498 if ((flags & BTREE_TRIGGER_GC) &&
499 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
502 if (bch2_trans_inconsistent_on(!bch2_dev_bucket_exists(c, new.k->p), trans,
503 "alloc key for invalid device or bucket"))
506 ca = bch_dev_bkey_exists(c, new.k->p.inode);
508 bch2_alloc_to_v4(old, &old_a);
509 bch2_alloc_to_v4(new, &new_a);
511 if ((flags & BTREE_TRIGGER_INSERT) &&
512 data_type_is_empty(old_a.data_type) !=
513 data_type_is_empty(new_a.data_type) &&
514 new.k->type == KEY_TYPE_alloc_v4) {
515 struct bch_alloc_v4 *v = (struct bch_alloc_v4 *) new.v;
517 BUG_ON(!journal_seq);
520 * If the btree updates referring to a bucket weren't flushed
521 * before the bucket became empty again, then the we don't have
522 * to wait on a journal flush before we can reuse the bucket:
524 new_a.journal_seq = data_type_is_empty(new_a.data_type) &&
525 (journal_seq == v->journal_seq ||
526 bch2_journal_noflush_seq(&c->journal, v->journal_seq))
528 v->journal_seq = new_a.journal_seq;
531 if (!data_type_is_empty(old_a.data_type) &&
532 data_type_is_empty(new_a.data_type) &&
534 ret = bch2_set_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
535 c->journal.flushed_seq_ondisk,
536 new.k->p.inode, new.k->p.offset,
539 bch2_fs_fatal_error(c,
540 "error setting bucket_needs_journal_commit: %i", ret);
545 if (new_a.data_type == BCH_DATA_free &&
546 (!new_a.journal_seq || new_a.journal_seq < c->journal.flushed_seq_ondisk))
547 closure_wake_up(&c->freelist_wait);
549 if (new_a.data_type == BCH_DATA_need_discard &&
550 (!new_a.journal_seq || new_a.journal_seq < c->journal.flushed_seq_ondisk))
553 if (old_a.data_type != BCH_DATA_cached &&
554 new_a.data_type == BCH_DATA_cached &&
555 should_invalidate_buckets(ca, bch2_dev_usage_read(ca)))
556 bch2_do_invalidates(c);
558 if (new_a.data_type == BCH_DATA_need_gc_gens)
561 percpu_down_read(&c->mark_lock);
562 if (!gc && new_a.gen != old_a.gen)
563 *bucket_gen(ca, new.k->p.offset) = new_a.gen;
565 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
568 struct bucket *g = gc_bucket(ca, new.k->p.offset);
574 g->data_type = new_a.data_type;
575 g->stripe = new_a.stripe;
576 g->stripe_redundancy = new_a.stripe_redundancy;
577 g->dirty_sectors = new_a.dirty_sectors;
578 g->cached_sectors = new_a.cached_sectors;
582 percpu_up_read(&c->mark_lock);
585 * need to know if we're getting called from the invalidate path or
589 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
590 old_a.cached_sectors) {
591 ret = update_cached_sectors(c, new, ca->dev_idx,
592 -old_a.cached_sectors,
595 bch2_fs_fatal_error(c, "bch2_mark_alloc(): no replicas entry while updating cached sectors");
603 int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
604 size_t b, enum bch_data_type data_type,
605 unsigned sectors, struct gc_pos pos,
608 struct bucket old, new, *g;
611 BUG_ON(!(flags & BTREE_TRIGGER_GC));
612 BUG_ON(data_type != BCH_DATA_sb &&
613 data_type != BCH_DATA_journal);
616 * Backup superblock might be past the end of our normal usable space:
618 if (b >= ca->mi.nbuckets)
621 percpu_down_read(&c->mark_lock);
622 g = gc_bucket(ca, b);
627 if (bch2_fs_inconsistent_on(g->data_type &&
628 g->data_type != data_type, c,
629 "different types of data in same bucket: %s, %s",
630 bch2_data_types[g->data_type],
631 bch2_data_types[data_type])) {
636 if (bch2_fs_inconsistent_on((u64) g->dirty_sectors + sectors > ca->mi.bucket_size, c,
637 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > bucket size",
638 ca->dev_idx, b, g->gen,
639 bch2_data_types[g->data_type ?: data_type],
640 g->dirty_sectors, sectors)) {
646 g->data_type = data_type;
647 g->dirty_sectors += sectors;
652 bch2_dev_usage_update_m(c, ca, old, new, 0, true);
653 percpu_up_read(&c->mark_lock);
657 static s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p)
659 EBUG_ON(sectors < 0);
661 return crc_is_compressed(p.crc)
662 ? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size,
663 p.crc.uncompressed_size)
667 static int check_bucket_ref(struct bch_fs *c,
669 const struct bch_extent_ptr *ptr,
670 s64 sectors, enum bch_data_type ptr_data_type,
671 u8 b_gen, u8 bucket_data_type,
672 u32 dirty_sectors, u32 cached_sectors)
674 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
675 size_t bucket_nr = PTR_BUCKET_NR(ca, ptr);
676 u16 bucket_sectors = !ptr->cached
679 struct printbuf buf = PRINTBUF;
682 if (bucket_data_type == BCH_DATA_cached)
683 bucket_data_type = BCH_DATA_user;
685 if (gen_after(ptr->gen, b_gen)) {
686 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
687 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
689 ptr->dev, bucket_nr, b_gen,
690 bch2_data_types[bucket_data_type ?: ptr_data_type],
692 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
697 if (gen_cmp(b_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
698 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
699 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
701 ptr->dev, bucket_nr, b_gen,
702 bch2_data_types[bucket_data_type ?: ptr_data_type],
704 (printbuf_reset(&buf),
705 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
710 if (b_gen != ptr->gen && !ptr->cached) {
711 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
712 "bucket %u:%zu gen %u (mem gen %u) data type %s: stale dirty ptr (gen %u)\n"
714 ptr->dev, bucket_nr, b_gen,
715 *bucket_gen(ca, bucket_nr),
716 bch2_data_types[bucket_data_type ?: ptr_data_type],
718 (printbuf_reset(&buf),
719 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
724 if (b_gen != ptr->gen) {
729 if (!data_type_is_empty(bucket_data_type) &&
731 bucket_data_type != ptr_data_type) {
732 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
733 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
735 ptr->dev, bucket_nr, b_gen,
736 bch2_data_types[bucket_data_type],
737 bch2_data_types[ptr_data_type],
738 (printbuf_reset(&buf),
739 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
744 if ((unsigned) (bucket_sectors + sectors) > U32_MAX) {
745 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
746 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
748 ptr->dev, bucket_nr, b_gen,
749 bch2_data_types[bucket_data_type ?: ptr_data_type],
750 bucket_sectors, sectors,
751 (printbuf_reset(&buf),
752 bch2_bkey_val_to_text(&buf, c, k), buf.buf));
761 static int mark_stripe_bucket(struct btree_trans *trans,
766 struct bch_fs *c = trans->c;
767 u64 journal_seq = trans->journal_res.seq;
768 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
769 unsigned nr_data = s->nr_blocks - s->nr_redundant;
770 bool parity = ptr_idx >= nr_data;
771 enum bch_data_type data_type = parity ? BCH_DATA_parity : 0;
772 s64 sectors = parity ? le16_to_cpu(s->sectors) : 0;
773 const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
774 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
775 struct bucket old, new, *g;
776 struct printbuf buf = PRINTBUF;
779 BUG_ON(!(flags & BTREE_TRIGGER_GC));
781 /* * XXX doesn't handle deletion */
783 percpu_down_read(&c->mark_lock);
785 g = PTR_GC_BUCKET(ca, ptr);
787 if (g->dirty_sectors ||
788 (g->stripe && g->stripe != k.k->p.offset)) {
789 bch2_fs_inconsistent(c,
790 "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
791 ptr->dev, PTR_BUCKET_NR(ca, ptr), g->gen,
792 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
800 ret = check_bucket_ref(c, k, ptr, sectors, data_type,
801 g->gen, g->data_type,
802 g->dirty_sectors, g->cached_sectors);
807 g->data_type = data_type;
808 g->dirty_sectors += sectors;
810 g->stripe = k.k->p.offset;
811 g->stripe_redundancy = s->nr_redundant;
816 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
817 percpu_up_read(&c->mark_lock);
822 static int __mark_pointer(struct btree_trans *trans,
824 const struct bch_extent_ptr *ptr,
825 s64 sectors, enum bch_data_type ptr_data_type,
826 u8 bucket_gen, u8 *bucket_data_type,
827 u32 *dirty_sectors, u32 *cached_sectors)
829 u32 *dst_sectors = !ptr->cached
832 int ret = check_bucket_ref(trans->c, k, ptr, sectors, ptr_data_type,
833 bucket_gen, *bucket_data_type,
834 *dirty_sectors, *cached_sectors);
839 *dst_sectors += sectors;
840 *bucket_data_type = *dirty_sectors || *cached_sectors
845 static int bch2_mark_pointer(struct btree_trans *trans,
847 struct extent_ptr_decoded p,
848 s64 sectors, enum bch_data_type data_type,
851 u64 journal_seq = trans->journal_res.seq;
852 struct bch_fs *c = trans->c;
853 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
854 struct bucket old, new, *g;
858 BUG_ON(!(flags & BTREE_TRIGGER_GC));
860 percpu_down_read(&c->mark_lock);
861 g = PTR_GC_BUCKET(ca, &p.ptr);
865 bucket_data_type = g->data_type;
866 ret = __mark_pointer(trans, k, &p.ptr, sectors,
872 g->data_type = bucket_data_type;
877 bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
878 percpu_up_read(&c->mark_lock);
883 static int bch2_mark_stripe_ptr(struct btree_trans *trans,
885 struct bch_extent_stripe_ptr p,
886 enum bch_data_type data_type,
890 struct bch_fs *c = trans->c;
891 struct bch_replicas_padded r;
894 BUG_ON(!(flags & BTREE_TRIGGER_GC));
896 m = genradix_ptr_alloc(&c->gc_stripes, p.idx, GFP_KERNEL);
898 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
903 spin_lock(&c->ec_stripes_heap_lock);
905 if (!m || !m->alive) {
906 spin_unlock(&c->ec_stripes_heap_lock);
907 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
909 bch2_inconsistent_error(c);
913 m->block_sectors[p.block] += sectors;
916 spin_unlock(&c->ec_stripes_heap_lock);
918 r.e.data_type = data_type;
919 update_replicas(c, k, &r.e, sectors, trans->journal_res.seq, true);
924 int bch2_mark_extent(struct btree_trans *trans,
925 struct bkey_s_c old, struct bkey_s_c new,
928 u64 journal_seq = trans->journal_res.seq;
929 struct bch_fs *c = trans->c;
930 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
931 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
932 const union bch_extent_entry *entry;
933 struct extent_ptr_decoded p;
934 struct bch_replicas_padded r;
935 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
938 s64 sectors = bkey_is_btree_ptr(k.k)
941 s64 dirty_sectors = 0;
945 BUG_ON(!(flags & BTREE_TRIGGER_GC));
947 r.e.data_type = data_type;
951 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
952 s64 disk_sectors = ptr_disk_sectors(sectors, p);
954 if (flags & BTREE_TRIGGER_OVERWRITE)
955 disk_sectors = -disk_sectors;
957 ret = bch2_mark_pointer(trans, k, p, disk_sectors,
966 ret = update_cached_sectors(c, k, p.ptr.dev,
967 disk_sectors, journal_seq, true);
969 bch2_fs_fatal_error(c, "bch2_mark_extent(): no replicas entry while updating cached sectors");
973 } else if (!p.has_ec) {
974 dirty_sectors += disk_sectors;
975 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
977 ret = bch2_mark_stripe_ptr(trans, k, p.ec, data_type,
978 disk_sectors, flags);
983 * There may be other dirty pointers in this extent, but
984 * if so they're not required for mounting if we have an
985 * erasure coded pointer in this extent:
992 ret = update_replicas(c, k, &r.e, dirty_sectors, journal_seq, true);
994 struct printbuf buf = PRINTBUF;
996 bch2_bkey_val_to_text(&buf, c, k);
997 bch2_fs_fatal_error(c, "no replicas entry for %s", buf.buf);
1006 int bch2_mark_stripe(struct btree_trans *trans,
1007 struct bkey_s_c old, struct bkey_s_c new,
1010 bool gc = flags & BTREE_TRIGGER_GC;
1011 u64 journal_seq = trans->journal_res.seq;
1012 struct bch_fs *c = trans->c;
1013 u64 idx = new.k->p.offset;
1014 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
1015 ? bkey_s_c_to_stripe(old).v : NULL;
1016 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
1017 ? bkey_s_c_to_stripe(new).v : NULL;
1021 BUG_ON(gc && old_s);
1024 struct stripe *m = genradix_ptr(&c->stripes, idx);
1026 if (!m || (old_s && !m->alive)) {
1027 struct printbuf buf1 = PRINTBUF;
1028 struct printbuf buf2 = PRINTBUF;
1030 bch2_bkey_val_to_text(&buf1, c, old);
1031 bch2_bkey_val_to_text(&buf2, c, new);
1032 bch_err_ratelimited(c, "error marking nonexistent stripe %llu while marking\n"
1034 "new %s", idx, buf1.buf, buf2.buf);
1035 printbuf_exit(&buf2);
1036 printbuf_exit(&buf1);
1037 bch2_inconsistent_error(c);
1042 spin_lock(&c->ec_stripes_heap_lock);
1043 bch2_stripes_heap_del(c, m, idx);
1044 spin_unlock(&c->ec_stripes_heap_lock);
1046 memset(m, 0, sizeof(*m));
1049 m->sectors = le16_to_cpu(new_s->sectors);
1050 m->algorithm = new_s->algorithm;
1051 m->nr_blocks = new_s->nr_blocks;
1052 m->nr_redundant = new_s->nr_redundant;
1053 m->blocks_nonempty = 0;
1055 for (i = 0; i < new_s->nr_blocks; i++)
1056 m->blocks_nonempty += !!stripe_blockcount_get(new_s, i);
1058 spin_lock(&c->ec_stripes_heap_lock);
1059 bch2_stripes_heap_update(c, m, idx);
1060 spin_unlock(&c->ec_stripes_heap_lock);
1063 struct gc_stripe *m =
1064 genradix_ptr_alloc(&c->gc_stripes, idx, GFP_KERNEL);
1067 bch_err(c, "error allocating memory for gc_stripes, idx %llu",
1072 * This will be wrong when we bring back runtime gc: we should
1073 * be unmarking the old key and then marking the new key
1076 m->sectors = le16_to_cpu(new_s->sectors);
1077 m->nr_blocks = new_s->nr_blocks;
1078 m->nr_redundant = new_s->nr_redundant;
1080 for (i = 0; i < new_s->nr_blocks; i++)
1081 m->ptrs[i] = new_s->ptrs[i];
1083 bch2_bkey_to_replicas(&m->r.e, new);
1086 * gc recalculates this field from stripe ptr
1089 memset(m->block_sectors, 0, sizeof(m->block_sectors));
1091 for (i = 0; i < new_s->nr_blocks; i++) {
1092 ret = mark_stripe_bucket(trans, new, i, flags);
1097 ret = update_replicas(c, new, &m->r.e,
1098 ((s64) m->sectors * m->nr_redundant),
1101 struct printbuf buf = PRINTBUF;
1103 bch2_bkey_val_to_text(&buf, c, new);
1104 bch2_fs_fatal_error(c, "no replicas entry for %s", buf.buf);
1105 printbuf_exit(&buf);
1113 int bch2_mark_inode(struct btree_trans *trans,
1114 struct bkey_s_c old, struct bkey_s_c new,
1117 struct bch_fs *c = trans->c;
1118 struct bch_fs_usage __percpu *fs_usage;
1119 u64 journal_seq = trans->journal_res.seq;
1121 if (flags & BTREE_TRIGGER_INSERT) {
1122 struct bch_inode_v2 *v = (struct bch_inode_v2 *) new.v;
1124 BUG_ON(!journal_seq);
1125 BUG_ON(new.k->type != KEY_TYPE_inode_v2);
1127 v->bi_journal_seq = cpu_to_le64(journal_seq);
1130 if (flags & BTREE_TRIGGER_GC) {
1131 percpu_down_read(&c->mark_lock);
1134 fs_usage = fs_usage_ptr(c, journal_seq, flags & BTREE_TRIGGER_GC);
1135 fs_usage->nr_inodes += bkey_is_inode(new.k);
1136 fs_usage->nr_inodes -= bkey_is_inode(old.k);
1139 percpu_up_read(&c->mark_lock);
1144 int bch2_mark_reservation(struct btree_trans *trans,
1145 struct bkey_s_c old, struct bkey_s_c new,
1148 struct bch_fs *c = trans->c;
1149 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
1150 struct bch_fs_usage __percpu *fs_usage;
1151 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1152 s64 sectors = (s64) k.k->size;
1154 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1156 if (flags & BTREE_TRIGGER_OVERWRITE)
1158 sectors *= replicas;
1160 percpu_down_read(&c->mark_lock);
1163 fs_usage = fs_usage_ptr(c, trans->journal_res.seq, flags & BTREE_TRIGGER_GC);
1164 replicas = clamp_t(unsigned, replicas, 1,
1165 ARRAY_SIZE(fs_usage->persistent_reserved));
1167 fs_usage->reserved += sectors;
1168 fs_usage->persistent_reserved[replicas - 1] += sectors;
1171 percpu_up_read(&c->mark_lock);
1176 static s64 __bch2_mark_reflink_p(struct btree_trans *trans,
1177 struct bkey_s_c_reflink_p p,
1179 u64 *idx, unsigned flags, size_t r_idx)
1181 struct bch_fs *c = trans->c;
1182 struct reflink_gc *r;
1183 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1186 struct printbuf buf = PRINTBUF;
1188 if (r_idx >= c->reflink_gc_nr)
1191 r = genradix_ptr(&c->reflink_gc_table, r_idx);
1192 next_idx = min(next_idx, r->offset - r->size);
1193 if (*idx < next_idx)
1196 BUG_ON((s64) r->refcount + add < 0);
1202 if (fsck_err(c, "pointer to missing indirect extent\n"
1204 " missing range %llu-%llu",
1205 (bch2_bkey_val_to_text(&buf, c, p.s_c), buf.buf),
1207 struct bkey_i_error new;
1210 new.k.type = KEY_TYPE_error;
1211 new.k.p = bkey_start_pos(p.k);
1212 new.k.p.offset += *idx - start;
1213 bch2_key_resize(&new.k, next_idx - *idx);
1214 ret = __bch2_btree_insert(trans, BTREE_ID_extents, &new.k_i);
1219 printbuf_exit(&buf);
1223 int bch2_mark_reflink_p(struct btree_trans *trans,
1224 struct bkey_s_c old, struct bkey_s_c new,
1227 struct bch_fs *c = trans->c;
1228 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;
1229 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1230 struct reflink_gc *ref;
1232 u64 idx = le64_to_cpu(p.v->idx), start = idx;
1233 u64 end = le64_to_cpu(p.v->idx) + p.k->size;
1236 BUG_ON(!(flags & BTREE_TRIGGER_GC));
1238 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix) {
1239 idx -= le32_to_cpu(p.v->front_pad);
1240 end += le32_to_cpu(p.v->back_pad);
1244 r = c->reflink_gc_nr;
1246 m = l + (r - l) / 2;
1248 ref = genradix_ptr(&c->reflink_gc_table, m);
1249 if (ref->offset <= idx)
1255 while (idx < end && !ret)
1256 ret = __bch2_mark_reflink_p(trans, p, start, end,
1262 static noinline __cold
1263 void fs_usage_apply_warn(struct btree_trans *trans,
1264 unsigned disk_res_sectors,
1265 s64 should_not_have_added)
1267 struct bch_fs *c = trans->c;
1268 struct btree_insert_entry *i;
1269 struct printbuf buf = PRINTBUF;
1271 bch_err(c, "disk usage increased %lli more than %u sectors reserved",
1272 should_not_have_added, disk_res_sectors);
1274 trans_for_each_update(trans, i) {
1275 struct bkey_s_c old = { &i->old_k, i->old_v };
1277 pr_err("while inserting");
1278 printbuf_reset(&buf);
1279 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(i->k));
1280 pr_err(" %s", buf.buf);
1281 pr_err("overlapping with");
1282 printbuf_reset(&buf);
1283 bch2_bkey_val_to_text(&buf, c, old);
1284 pr_err(" %s", buf.buf);
1288 printbuf_exit(&buf);
1291 int bch2_trans_fs_usage_apply(struct btree_trans *trans,
1292 struct replicas_delta_list *deltas)
1294 struct bch_fs *c = trans->c;
1295 static int warned_disk_usage = 0;
1297 unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1298 struct replicas_delta *d = deltas->d, *d2;
1299 struct replicas_delta *top = (void *) deltas->d + deltas->used;
1300 struct bch_fs_usage *dst;
1301 s64 added = 0, should_not_have_added;
1304 percpu_down_read(&c->mark_lock);
1306 dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1308 for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1309 switch (d->r.data_type) {
1310 case BCH_DATA_btree:
1312 case BCH_DATA_parity:
1316 if (__update_replicas(c, dst, &d->r, d->delta))
1320 dst->nr_inodes += deltas->nr_inodes;
1322 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1323 added += deltas->persistent_reserved[i];
1324 dst->reserved += deltas->persistent_reserved[i];
1325 dst->persistent_reserved[i] += deltas->persistent_reserved[i];
1329 * Not allowed to reduce sectors_available except by getting a
1332 should_not_have_added = added - (s64) disk_res_sectors;
1333 if (unlikely(should_not_have_added > 0)) {
1334 u64 old, new, v = atomic64_read(&c->sectors_available);
1338 new = max_t(s64, 0, old - should_not_have_added);
1339 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1342 added -= should_not_have_added;
1347 trans->disk_res->sectors -= added;
1348 this_cpu_sub(*c->online_reserved, added);
1352 percpu_up_read(&c->mark_lock);
1354 if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
1355 fs_usage_apply_warn(trans, disk_res_sectors, should_not_have_added);
1358 /* revert changes: */
1359 for (d2 = deltas->d; d2 != d; d2 = replicas_delta_next(d2))
1360 BUG_ON(__update_replicas(c, dst, &d2->r, -d2->delta));
1363 percpu_up_read(&c->mark_lock);
1369 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1370 struct bkey_s_c k, struct extent_ptr_decoded p,
1371 s64 sectors, enum bch_data_type data_type)
1373 struct btree_iter iter;
1374 struct bkey_i_alloc_v4 *a;
1377 a = bch2_trans_start_alloc_update(trans, &iter, PTR_BUCKET_POS(trans->c, &p.ptr));
1381 ret = __mark_pointer(trans, k, &p.ptr, sectors, data_type,
1382 a->v.gen, &a->v.data_type,
1383 &a->v.dirty_sectors, &a->v.cached_sectors) ?:
1384 bch2_trans_update(trans, &iter, &a->k_i, 0);
1385 bch2_trans_iter_exit(trans, &iter);
1389 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1390 struct extent_ptr_decoded p,
1391 s64 sectors, enum bch_data_type data_type)
1393 struct btree_iter iter;
1395 struct bkey_i_stripe *s;
1396 struct bch_replicas_padded r;
1399 bch2_trans_iter_init(trans, &iter, BTREE_ID_stripes, POS(0, p.ec.idx),
1401 BTREE_ITER_WITH_UPDATES);
1402 k = bch2_btree_iter_peek_slot(&iter);
1407 if (k.k->type != KEY_TYPE_stripe) {
1408 bch2_trans_inconsistent(trans,
1409 "pointer to nonexistent stripe %llu",
1415 if (!bch2_ptr_matches_stripe(bkey_s_c_to_stripe(k).v, p)) {
1416 bch2_trans_inconsistent(trans,
1417 "stripe pointer doesn't match stripe %llu",
1423 s = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1424 ret = PTR_ERR_OR_ZERO(s);
1428 bkey_reassemble(&s->k_i, k);
1429 stripe_blockcount_set(&s->v, p.ec.block,
1430 stripe_blockcount_get(&s->v, p.ec.block) +
1433 ret = bch2_trans_update(trans, &iter, &s->k_i, 0);
1437 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1438 r.e.data_type = data_type;
1439 update_replicas_list(trans, &r.e, sectors);
1441 bch2_trans_iter_exit(trans, &iter);
1445 int bch2_trans_mark_extent(struct btree_trans *trans,
1446 enum btree_id btree_id, unsigned level,
1447 struct bkey_s_c old, struct bkey_i *new,
1450 struct bch_fs *c = trans->c;
1451 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1453 : bkey_i_to_s_c(new);
1454 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1455 const union bch_extent_entry *entry;
1456 struct extent_ptr_decoded p;
1457 struct bch_replicas_padded r;
1458 enum bch_data_type data_type = bkey_is_btree_ptr(k.k)
1461 s64 sectors = bkey_is_btree_ptr(k.k)
1464 s64 dirty_sectors = 0;
1468 r.e.data_type = data_type;
1470 r.e.nr_required = 1;
1472 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1473 s64 disk_sectors = ptr_disk_sectors(sectors, p);
1475 if (flags & BTREE_TRIGGER_OVERWRITE)
1476 disk_sectors = -disk_sectors;
1478 ret = bch2_trans_mark_pointer(trans, k, p,
1479 disk_sectors, data_type);
1487 update_cached_sectors_list(trans, p.ptr.dev,
1489 } else if (!p.has_ec) {
1490 dirty_sectors += disk_sectors;
1491 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1493 ret = bch2_trans_mark_stripe_ptr(trans, p,
1494 disk_sectors, data_type);
1498 r.e.nr_required = 0;
1503 update_replicas_list(trans, &r.e, dirty_sectors);
1508 static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
1509 struct bkey_s_c_stripe s,
1510 unsigned idx, bool deleting)
1512 struct bch_fs *c = trans->c;
1513 const struct bch_extent_ptr *ptr = &s.v->ptrs[idx];
1514 struct btree_iter iter;
1515 struct bkey_i_alloc_v4 *a;
1516 enum bch_data_type data_type = idx >= s.v->nr_blocks - s.v->nr_redundant
1517 ? BCH_DATA_parity : 0;
1518 s64 sectors = data_type ? le16_to_cpu(s.v->sectors) : 0;
1524 a = bch2_trans_start_alloc_update(trans, &iter, PTR_BUCKET_POS(c, ptr));
1528 ret = check_bucket_ref(c, s.s_c, ptr, sectors, data_type,
1529 a->v.gen, a->v.data_type,
1530 a->v.dirty_sectors, a->v.cached_sectors);
1535 if (bch2_trans_inconsistent_on(a->v.stripe ||
1536 a->v.stripe_redundancy, trans,
1537 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)",
1538 iter.pos.inode, iter.pos.offset, a->v.gen,
1539 bch2_data_types[a->v.data_type],
1541 a->v.stripe, s.k->p.offset)) {
1546 if (bch2_trans_inconsistent_on(data_type && a->v.dirty_sectors, trans,
1547 "bucket %llu:%llu gen %u data type %s dirty_sectors %u: data already in stripe bucket %llu",
1548 iter.pos.inode, iter.pos.offset, a->v.gen,
1549 bch2_data_types[a->v.data_type],
1556 a->v.stripe = s.k->p.offset;
1557 a->v.stripe_redundancy = s.v->nr_redundant;
1559 if (bch2_trans_inconsistent_on(a->v.stripe != s.k->p.offset ||
1560 a->v.stripe_redundancy != s.v->nr_redundant, trans,
1561 "bucket %llu:%llu gen %u: not marked as stripe when deleting stripe %llu (got %u)",
1562 iter.pos.inode, iter.pos.offset, a->v.gen,
1563 s.k->p.offset, a->v.stripe)) {
1569 a->v.stripe_redundancy = 0;
1572 a->v.dirty_sectors += sectors;
1574 a->v.data_type = !deleting ? data_type : 0;
1576 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1580 bch2_trans_iter_exit(trans, &iter);
1584 int bch2_trans_mark_stripe(struct btree_trans *trans,
1585 enum btree_id btree_id, unsigned level,
1586 struct bkey_s_c old, struct bkey_i *new,
1589 const struct bch_stripe *old_s = NULL;
1590 struct bch_stripe *new_s = NULL;
1591 struct bch_replicas_padded r;
1592 unsigned i, nr_blocks;
1595 if (old.k->type == KEY_TYPE_stripe)
1596 old_s = bkey_s_c_to_stripe(old).v;
1597 if (new->k.type == KEY_TYPE_stripe)
1598 new_s = &bkey_i_to_stripe(new)->v;
1601 * If the pointers aren't changing, we don't need to do anything:
1603 if (new_s && old_s &&
1604 new_s->nr_blocks == old_s->nr_blocks &&
1605 new_s->nr_redundant == old_s->nr_redundant &&
1606 !memcmp(old_s->ptrs, new_s->ptrs,
1607 new_s->nr_blocks * sizeof(struct bch_extent_ptr)))
1610 BUG_ON(new_s && old_s &&
1611 (new_s->nr_blocks != old_s->nr_blocks ||
1612 new_s->nr_redundant != old_s->nr_redundant));
1614 nr_blocks = new_s ? new_s->nr_blocks : old_s->nr_blocks;
1617 s64 sectors = le16_to_cpu(new_s->sectors);
1619 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(new));
1620 update_replicas_list(trans, &r.e, sectors * new_s->nr_redundant);
1624 s64 sectors = -((s64) le16_to_cpu(old_s->sectors));
1626 bch2_bkey_to_replicas(&r.e, old);
1627 update_replicas_list(trans, &r.e, sectors * old_s->nr_redundant);
1630 for (i = 0; i < nr_blocks; i++) {
1631 if (new_s && old_s &&
1632 !memcmp(&new_s->ptrs[i],
1634 sizeof(new_s->ptrs[i])))
1638 ret = bch2_trans_mark_stripe_bucket(trans,
1639 bkey_i_to_s_c_stripe(new), i, false);
1645 ret = bch2_trans_mark_stripe_bucket(trans,
1646 bkey_s_c_to_stripe(old), i, true);
1655 int bch2_trans_mark_inode(struct btree_trans *trans,
1656 enum btree_id btree_id, unsigned level,
1657 struct bkey_s_c old,
1661 int nr = bkey_is_inode(&new->k) - bkey_is_inode(old.k);
1664 struct replicas_delta_list *d =
1665 replicas_deltas_realloc(trans, 0);
1672 int bch2_trans_mark_reservation(struct btree_trans *trans,
1673 enum btree_id btree_id, unsigned level,
1674 struct bkey_s_c old,
1678 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1680 : bkey_i_to_s_c(new);
1681 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1682 s64 sectors = (s64) k.k->size;
1683 struct replicas_delta_list *d;
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 enum btree_id btree_id, unsigned level,
1776 struct bkey_s_c old,
1780 struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
1782 : bkey_i_to_s_c(new);
1783 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
1787 if (flags & BTREE_TRIGGER_INSERT) {
1788 struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
1790 v->front_pad = v->back_pad = 0;
1793 idx = le64_to_cpu(p.v->idx) - le32_to_cpu(p.v->front_pad);
1794 end_idx = le64_to_cpu(p.v->idx) + p.k->size +
1795 le32_to_cpu(p.v->back_pad);
1797 while (idx < end_idx && !ret)
1798 ret = __bch2_trans_mark_reflink_p(trans, p, &idx, flags);
1803 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1804 struct bch_dev *ca, size_t b,
1805 enum bch_data_type type,
1808 struct bch_fs *c = trans->c;
1809 struct btree_iter iter;
1810 struct bkey_i_alloc_v4 *a;
1814 * Backup superblock might be past the end of our normal usable space:
1816 if (b >= ca->mi.nbuckets)
1819 a = bch2_trans_start_alloc_update(trans, &iter, POS(ca->dev_idx, b));
1823 if (a->v.data_type && a->v.data_type != type) {
1824 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
1825 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
1827 iter.pos.inode, iter.pos.offset, a->v.gen,
1828 bch2_data_types[a->v.data_type],
1829 bch2_data_types[type],
1830 bch2_data_types[type]);
1835 a->v.data_type = type;
1836 a->v.dirty_sectors = sectors;
1838 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1842 bch2_trans_iter_exit(trans, &iter);
1846 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1847 struct bch_dev *ca, size_t b,
1848 enum bch_data_type type,
1851 return __bch2_trans_do(trans, NULL, NULL, 0,
1852 __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors));
1855 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans,
1858 enum bch_data_type type,
1859 u64 *bucket, unsigned *bucket_sectors)
1862 u64 b = sector_to_bucket(ca, start);
1864 min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
1866 if (b != *bucket && *bucket_sectors) {
1867 int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket,
1868 type, *bucket_sectors);
1872 *bucket_sectors = 0;
1876 *bucket_sectors += sectors;
1878 } while (start < end);
1883 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
1886 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
1888 unsigned i, bucket_sectors = 0;
1891 for (i = 0; i < layout->nr_superblocks; i++) {
1892 u64 offset = le64_to_cpu(layout->sb_offset[i]);
1894 if (offset == BCH_SB_SECTOR) {
1895 ret = bch2_trans_mark_metadata_sectors(trans, ca,
1897 BCH_DATA_sb, &bucket, &bucket_sectors);
1902 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset,
1903 offset + (1 << layout->sb_max_size_bits),
1904 BCH_DATA_sb, &bucket, &bucket_sectors);
1909 if (bucket_sectors) {
1910 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1911 bucket, BCH_DATA_sb, bucket_sectors);
1916 for (i = 0; i < ca->journal.nr; i++) {
1917 ret = bch2_trans_mark_metadata_bucket(trans, ca,
1918 ca->journal.buckets[i],
1919 BCH_DATA_journal, ca->mi.bucket_size);
1927 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
1929 return bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
1930 __bch2_trans_mark_dev_sb(&trans, ca));
1933 /* Disk reservations: */
1935 #define SECTORS_CACHE 1024
1937 int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
1938 u64 sectors, int flags)
1940 struct bch_fs_pcpu *pcpu;
1942 s64 sectors_available;
1945 percpu_down_read(&c->mark_lock);
1947 pcpu = this_cpu_ptr(c->pcpu);
1949 if (sectors <= pcpu->sectors_available)
1952 v = atomic64_read(&c->sectors_available);
1955 get = min((u64) sectors + SECTORS_CACHE, old);
1957 if (get < sectors) {
1961 } while ((v = atomic64_cmpxchg(&c->sectors_available,
1962 old, old - get)) != old);
1964 pcpu->sectors_available += get;
1967 pcpu->sectors_available -= sectors;
1968 this_cpu_add(*c->online_reserved, sectors);
1969 res->sectors += sectors;
1972 percpu_up_read(&c->mark_lock);
1976 mutex_lock(&c->sectors_available_lock);
1978 percpu_u64_set(&c->pcpu->sectors_available, 0);
1979 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
1981 if (sectors <= sectors_available ||
1982 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
1983 atomic64_set(&c->sectors_available,
1984 max_t(s64, 0, sectors_available - sectors));
1985 this_cpu_add(*c->online_reserved, sectors);
1986 res->sectors += sectors;
1989 atomic64_set(&c->sectors_available, sectors_available);
1993 mutex_unlock(&c->sectors_available_lock);
1994 percpu_up_read(&c->mark_lock);
1999 /* Startup/shutdown: */
2001 static void bucket_gens_free_rcu(struct rcu_head *rcu)
2003 struct bucket_gens *buckets =
2004 container_of(rcu, struct bucket_gens, rcu);
2006 kvpfree(buckets, sizeof(*buckets) + buckets->nbuckets);
2009 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
2011 struct bucket_gens *bucket_gens = NULL, *old_bucket_gens = NULL;
2012 unsigned long *buckets_nouse = NULL;
2013 bool resize = ca->bucket_gens != NULL;
2016 if (!(bucket_gens = kvpmalloc(sizeof(struct bucket_gens) + nbuckets,
2017 GFP_KERNEL|__GFP_ZERO)) ||
2018 (c->opts.buckets_nouse &&
2019 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2020 sizeof(unsigned long),
2021 GFP_KERNEL|__GFP_ZERO))))
2024 bucket_gens->first_bucket = ca->mi.first_bucket;
2025 bucket_gens->nbuckets = nbuckets;
2027 bch2_copygc_stop(c);
2030 down_write(&c->gc_lock);
2031 down_write(&ca->bucket_lock);
2032 percpu_down_write(&c->mark_lock);
2035 old_bucket_gens = rcu_dereference_protected(ca->bucket_gens, 1);
2038 size_t n = min(bucket_gens->nbuckets, old_bucket_gens->nbuckets);
2040 memcpy(bucket_gens->b,
2044 memcpy(buckets_nouse,
2046 BITS_TO_LONGS(n) * sizeof(unsigned long));
2049 rcu_assign_pointer(ca->bucket_gens, bucket_gens);
2050 bucket_gens = old_bucket_gens;
2052 swap(ca->buckets_nouse, buckets_nouse);
2054 nbuckets = ca->mi.nbuckets;
2057 percpu_up_write(&c->mark_lock);
2058 up_write(&ca->bucket_lock);
2059 up_write(&c->gc_lock);
2064 kvpfree(buckets_nouse,
2065 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2067 call_rcu(&bucket_gens->rcu, bucket_gens_free_rcu);
2072 void bch2_dev_buckets_free(struct bch_dev *ca)
2076 kvpfree(ca->buckets_nouse,
2077 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2078 kvpfree(rcu_dereference_protected(ca->bucket_gens, 1),
2079 sizeof(struct bucket_gens) + ca->mi.nbuckets);
2081 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
2082 free_percpu(ca->usage[i]);
2083 kfree(ca->usage_base);
2086 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2090 ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL);
2091 if (!ca->usage_base)
2094 for (i = 0; i < ARRAY_SIZE(ca->usage); i++) {
2095 ca->usage[i] = alloc_percpu(struct bch_dev_usage);
2100 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);;