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"
19 #include <linux/preempt.h>
20 #include <trace/events/bcachefs.h>
22 static inline void fs_usage_data_type_to_base(struct bch_fs_usage *fs_usage,
23 enum bch_data_type data_type,
28 fs_usage->btree += sectors;
32 fs_usage->data += sectors;
35 fs_usage->cached += sectors;
43 * Clear journal_seq_valid for buckets for which it's not needed, to prevent
46 void bch2_bucket_seq_cleanup(struct bch_fs *c)
48 u64 journal_seq = atomic64_read(&c->journal.seq);
49 u16 last_seq_ondisk = c->journal.last_seq_ondisk;
51 struct bucket_array *buckets;
56 if (journal_seq - c->last_bucket_seq_cleanup <
57 (1U << (BUCKET_JOURNAL_SEQ_BITS - 2)))
60 c->last_bucket_seq_cleanup = journal_seq;
62 for_each_member_device(ca, c, i) {
63 down_read(&ca->bucket_lock);
64 buckets = bucket_array(ca);
66 for_each_bucket(g, buckets) {
67 bucket_cmpxchg(g, m, ({
68 if (!m.journal_seq_valid ||
69 bucket_needs_journal_commit(m, last_seq_ondisk))
72 m.journal_seq_valid = 0;
75 up_read(&ca->bucket_lock);
79 void bch2_fs_usage_initialize(struct bch_fs *c)
81 struct bch_fs_usage *usage;
85 percpu_down_write(&c->mark_lock);
86 usage = c->usage_base;
88 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
89 bch2_fs_usage_acc_to_base(c, i);
91 for (i = 0; i < BCH_REPLICAS_MAX; i++)
92 usage->reserved += usage->persistent_reserved[i];
94 for (i = 0; i < c->replicas.nr; i++) {
95 struct bch_replicas_entry *e =
96 cpu_replicas_entry(&c->replicas, i);
98 fs_usage_data_type_to_base(usage, e->data_type, usage->replicas[i]);
101 for_each_member_device(ca, c, i) {
102 struct bch_dev_usage dev = bch2_dev_usage_read(ca);
104 usage->hidden += (dev.d[BCH_DATA_sb].buckets +
105 dev.d[BCH_DATA_journal].buckets) *
109 percpu_up_write(&c->mark_lock);
112 static inline struct bch_dev_usage *dev_usage_ptr(struct bch_dev *ca,
113 unsigned journal_seq,
116 return this_cpu_ptr(gc
118 : ca->usage[journal_seq & JOURNAL_BUF_MASK]);
121 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
123 struct bch_fs *c = ca->fs;
124 struct bch_dev_usage ret;
125 unsigned seq, i, u64s = dev_usage_u64s();
128 seq = read_seqcount_begin(&c->usage_lock);
129 memcpy(&ret, ca->usage_base, u64s * sizeof(u64));
130 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
131 acc_u64s_percpu((u64 *) &ret, (u64 __percpu *) ca->usage[i], u64s);
132 } while (read_seqcount_retry(&c->usage_lock, seq));
137 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
138 unsigned journal_seq,
141 return this_cpu_ptr(gc
143 : c->usage[journal_seq & JOURNAL_BUF_MASK]);
146 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
148 ssize_t offset = v - (u64 *) c->usage_base;
152 BUG_ON(offset < 0 || offset >= fs_usage_u64s(c));
153 percpu_rwsem_assert_held(&c->mark_lock);
156 seq = read_seqcount_begin(&c->usage_lock);
159 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
160 ret += percpu_u64_get((u64 __percpu *) c->usage[i] + offset);
161 } while (read_seqcount_retry(&c->usage_lock, seq));
166 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *c)
168 struct bch_fs_usage_online *ret;
169 unsigned seq, i, u64s;
171 percpu_down_read(&c->mark_lock);
173 ret = kmalloc(sizeof(struct bch_fs_usage_online) +
174 sizeof(u64) * c->replicas.nr, GFP_NOFS);
175 if (unlikely(!ret)) {
176 percpu_up_read(&c->mark_lock);
180 ret->online_reserved = percpu_u64_get(c->online_reserved);
182 u64s = fs_usage_u64s(c);
184 seq = read_seqcount_begin(&c->usage_lock);
185 memcpy(&ret->u, c->usage_base, u64s * sizeof(u64));
186 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
187 acc_u64s_percpu((u64 *) &ret->u, (u64 __percpu *) c->usage[i], u64s);
188 } while (read_seqcount_retry(&c->usage_lock, seq));
193 void bch2_fs_usage_acc_to_base(struct bch_fs *c, unsigned idx)
196 unsigned i, u64s = fs_usage_u64s(c);
198 BUG_ON(idx >= ARRAY_SIZE(c->usage));
201 write_seqcount_begin(&c->usage_lock);
203 acc_u64s_percpu((u64 *) c->usage_base,
204 (u64 __percpu *) c->usage[idx], u64s);
205 percpu_memset(c->usage[idx], 0, u64s * sizeof(u64));
208 for_each_member_device_rcu(ca, c, i, NULL) {
209 u64s = dev_usage_u64s();
211 acc_u64s_percpu((u64 *) ca->usage_base,
212 (u64 __percpu *) ca->usage[idx], u64s);
213 percpu_memset(ca->usage[idx], 0, u64s * sizeof(u64));
217 write_seqcount_end(&c->usage_lock);
221 void bch2_fs_usage_to_text(struct printbuf *out,
223 struct bch_fs_usage_online *fs_usage)
227 pr_buf(out, "capacity:\t\t\t%llu\n", c->capacity);
229 pr_buf(out, "hidden:\t\t\t\t%llu\n",
231 pr_buf(out, "data:\t\t\t\t%llu\n",
233 pr_buf(out, "cached:\t\t\t\t%llu\n",
235 pr_buf(out, "reserved:\t\t\t%llu\n",
236 fs_usage->u.reserved);
237 pr_buf(out, "nr_inodes:\t\t\t%llu\n",
238 fs_usage->u.nr_inodes);
239 pr_buf(out, "online reserved:\t\t%llu\n",
240 fs_usage->online_reserved);
243 i < ARRAY_SIZE(fs_usage->u.persistent_reserved);
245 pr_buf(out, "%u replicas:\n", i + 1);
246 pr_buf(out, "\treserved:\t\t%llu\n",
247 fs_usage->u.persistent_reserved[i]);
250 for (i = 0; i < c->replicas.nr; i++) {
251 struct bch_replicas_entry *e =
252 cpu_replicas_entry(&c->replicas, i);
255 bch2_replicas_entry_to_text(out, e);
256 pr_buf(out, ":\t%llu\n", fs_usage->u.replicas[i]);
260 #define RESERVE_FACTOR 6
262 static u64 reserve_factor(u64 r)
264 return r + (round_up(r, (1 << RESERVE_FACTOR)) >> RESERVE_FACTOR);
267 static u64 avail_factor(u64 r)
269 return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
272 u64 bch2_fs_sectors_used(struct bch_fs *c, struct bch_fs_usage_online *fs_usage)
274 return min(fs_usage->u.hidden +
277 reserve_factor(fs_usage->u.reserved +
278 fs_usage->online_reserved),
282 static struct bch_fs_usage_short
283 __bch2_fs_usage_read_short(struct bch_fs *c)
285 struct bch_fs_usage_short ret;
288 ret.capacity = c->capacity -
289 bch2_fs_usage_read_one(c, &c->usage_base->hidden);
291 data = bch2_fs_usage_read_one(c, &c->usage_base->data) +
292 bch2_fs_usage_read_one(c, &c->usage_base->btree);
293 reserved = bch2_fs_usage_read_one(c, &c->usage_base->reserved) +
294 percpu_u64_get(c->online_reserved);
296 ret.used = min(ret.capacity, data + reserve_factor(reserved));
297 ret.free = ret.capacity - ret.used;
299 ret.nr_inodes = bch2_fs_usage_read_one(c, &c->usage_base->nr_inodes);
304 struct bch_fs_usage_short
305 bch2_fs_usage_read_short(struct bch_fs *c)
307 struct bch_fs_usage_short ret;
309 percpu_down_read(&c->mark_lock);
310 ret = __bch2_fs_usage_read_short(c);
311 percpu_up_read(&c->mark_lock);
316 static inline int is_unavailable_bucket(struct bucket_mark m)
318 return !is_available_bucket(m);
321 static inline int bucket_sectors_fragmented(struct bch_dev *ca,
322 struct bucket_mark m)
324 return bucket_sectors_used(m)
325 ? max(0, (int) ca->mi.bucket_size - (int) bucket_sectors_used(m))
329 static inline int is_stripe_data_bucket(struct bucket_mark m)
331 return m.stripe && m.data_type != BCH_DATA_parity;
334 static inline enum bch_data_type bucket_type(struct bucket_mark m)
336 return m.cached_sectors && !m.dirty_sectors
341 static bool bucket_became_unavailable(struct bucket_mark old,
342 struct bucket_mark new)
344 return is_available_bucket(old) &&
345 !is_available_bucket(new);
348 static inline void account_bucket(struct bch_fs_usage *fs_usage,
349 struct bch_dev_usage *dev_usage,
350 enum bch_data_type type,
353 if (type == BCH_DATA_sb || type == BCH_DATA_journal)
354 fs_usage->hidden += size;
356 dev_usage->d[type].buckets += nr;
359 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
360 struct bch_fs_usage *fs_usage,
361 struct bucket_mark old, struct bucket_mark new,
362 u64 journal_seq, bool gc)
364 struct bch_dev_usage *u;
366 percpu_rwsem_assert_held(&c->mark_lock);
370 fs_usage = fs_usage_ptr(c, journal_seq, gc);
371 u = dev_usage_ptr(ca, journal_seq, gc);
373 if (bucket_type(old))
374 account_bucket(fs_usage, u, bucket_type(old),
375 -1, -ca->mi.bucket_size);
377 if (bucket_type(new))
378 account_bucket(fs_usage, u, bucket_type(new),
379 1, ca->mi.bucket_size);
381 u->buckets_ec += (int) new.stripe - (int) old.stripe;
382 u->buckets_unavailable +=
383 is_unavailable_bucket(new) - is_unavailable_bucket(old);
385 u->d[old.data_type].sectors -= old.dirty_sectors;
386 u->d[new.data_type].sectors += new.dirty_sectors;
387 u->d[BCH_DATA_cached].sectors +=
388 (int) new.cached_sectors - (int) old.cached_sectors;
390 u->d[old.data_type].fragmented -= bucket_sectors_fragmented(ca, old);
391 u->d[new.data_type].fragmented += bucket_sectors_fragmented(ca, new);
395 if (!is_available_bucket(old) && is_available_bucket(new))
396 bch2_wake_allocator(ca);
399 static inline int update_replicas(struct bch_fs *c,
400 struct bch_fs_usage *fs_usage,
401 struct bch_replicas_entry *r,
404 int idx = bch2_replicas_entry_idx(c, r);
409 fs_usage_data_type_to_base(fs_usage, r->data_type, sectors);
410 fs_usage->replicas[idx] += sectors;
414 static inline int update_cached_sectors(struct bch_fs *c,
415 struct bch_fs_usage *fs_usage,
416 unsigned dev, s64 sectors)
418 struct bch_replicas_padded r;
420 bch2_replicas_entry_cached(&r.e, dev);
422 return update_replicas(c, fs_usage, &r.e, sectors);
425 static struct replicas_delta_list *
426 replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
428 struct replicas_delta_list *d = trans->fs_usage_deltas;
429 unsigned new_size = d ? (d->size + more) * 2 : 128;
430 unsigned alloc_size = sizeof(*d) + new_size;
432 WARN_ON_ONCE(alloc_size > REPLICAS_DELTA_LIST_MAX);
434 if (!d || d->used + more > d->size) {
435 d = krealloc(d, alloc_size, GFP_NOIO|__GFP_ZERO);
437 BUG_ON(!d && alloc_size > REPLICAS_DELTA_LIST_MAX);
440 d = mempool_alloc(&trans->c->replicas_delta_pool, GFP_NOIO);
441 memset(d, 0, REPLICAS_DELTA_LIST_MAX);
443 if (trans->fs_usage_deltas)
444 memcpy(d, trans->fs_usage_deltas,
445 trans->fs_usage_deltas->size + sizeof(*d));
447 new_size = REPLICAS_DELTA_LIST_MAX - sizeof(*d);
448 kfree(trans->fs_usage_deltas);
452 trans->fs_usage_deltas = d;
457 static inline void update_replicas_list(struct btree_trans *trans,
458 struct bch_replicas_entry *r,
461 struct replicas_delta_list *d;
462 struct replicas_delta *n;
468 b = replicas_entry_bytes(r) + 8;
469 d = replicas_deltas_realloc(trans, b);
471 n = (void *) d->d + d->used;
473 memcpy(&n->r, r, replicas_entry_bytes(r));
474 bch2_replicas_entry_sort(&n->r);
478 static inline void update_cached_sectors_list(struct btree_trans *trans,
479 unsigned dev, s64 sectors)
481 struct bch_replicas_padded r;
483 bch2_replicas_entry_cached(&r.e, dev);
485 update_replicas_list(trans, &r.e, sectors);
488 #define do_mark_fn(fn, c, pos, flags, ...) \
492 percpu_rwsem_assert_held(&c->mark_lock); \
494 for (gc = 0; gc < 2 && !ret; gc++) \
495 if (!gc == !(flags & BTREE_TRIGGER_GC) || \
496 (gc && gc_visited(c, pos))) \
497 ret = fn(c, __VA_ARGS__, gc); \
501 void bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
502 size_t b, bool owned_by_allocator)
504 struct bucket *g = bucket(ca, b);
505 struct bucket_mark old, new;
507 old = bucket_cmpxchg(g, new, ({
508 new.owned_by_allocator = owned_by_allocator;
511 BUG_ON(owned_by_allocator == old.owned_by_allocator);
514 static int bch2_mark_alloc(struct bch_fs *c,
515 struct bkey_s_c old, struct bkey_s_c new,
516 struct bch_fs_usage *fs_usage,
517 u64 journal_seq, unsigned flags)
519 bool gc = flags & BTREE_TRIGGER_GC;
520 struct bkey_alloc_unpacked u;
523 struct bucket_mark old_m, m;
525 /* We don't do anything for deletions - do we?: */
526 if (new.k->type != KEY_TYPE_alloc &&
527 new.k->type != KEY_TYPE_alloc_v2)
531 * alloc btree is read in by bch2_alloc_read, not gc:
533 if ((flags & BTREE_TRIGGER_GC) &&
534 !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
537 ca = bch_dev_bkey_exists(c, new.k->p.inode);
539 if (new.k->p.offset >= ca->mi.nbuckets)
542 g = __bucket(ca, new.k->p.offset, gc);
543 u = bch2_alloc_unpack(new);
545 old_m = bucket_cmpxchg(g, m, ({
547 m.data_type = u.data_type;
548 m.dirty_sectors = u.dirty_sectors;
549 m.cached_sectors = u.cached_sectors;
550 m.stripe = u.stripe != 0;
553 m.journal_seq_valid = 1;
554 m.journal_seq = journal_seq;
558 bch2_dev_usage_update(c, ca, fs_usage, old_m, m, journal_seq, gc);
560 g->io_time[READ] = u.read_time;
561 g->io_time[WRITE] = u.write_time;
562 g->oldest_gen = u.oldest_gen;
564 g->stripe = u.stripe;
565 g->stripe_redundancy = u.stripe_redundancy;
568 * need to know if we're getting called from the invalidate path or
572 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
573 old_m.cached_sectors) {
574 if (update_cached_sectors(c, fs_usage, ca->dev_idx,
575 -old_m.cached_sectors)) {
576 bch2_fs_fatal_error(c, "bch2_mark_alloc(): no replicas entry while updating cached sectors");
580 trace_invalidate(ca, bucket_to_sector(ca, new.k->p.offset),
581 old_m.cached_sectors);
587 #define checked_add(a, b) \
589 unsigned _res = (unsigned) (a) + (b); \
590 bool overflow = _res > U16_MAX; \
597 static int __bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
598 size_t b, enum bch_data_type data_type,
599 unsigned sectors, bool gc)
601 struct bucket *g = __bucket(ca, b, gc);
602 struct bucket_mark old, new;
605 BUG_ON(data_type != BCH_DATA_sb &&
606 data_type != BCH_DATA_journal);
608 old = bucket_cmpxchg(g, new, ({
609 new.data_type = data_type;
610 overflow = checked_add(new.dirty_sectors, sectors);
613 bch2_fs_inconsistent_on(old.data_type &&
614 old.data_type != data_type, c,
615 "different types of data in same bucket: %s, %s",
616 bch2_data_types[old.data_type],
617 bch2_data_types[data_type]);
619 bch2_fs_inconsistent_on(overflow, c,
620 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > U16_MAX",
621 ca->dev_idx, b, new.gen,
622 bch2_data_types[old.data_type ?: data_type],
623 old.dirty_sectors, sectors);
626 bch2_dev_usage_update(c, ca, fs_usage_ptr(c, 0, gc),
632 void bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
633 size_t b, enum bch_data_type type,
634 unsigned sectors, struct gc_pos pos,
637 BUG_ON(type != BCH_DATA_sb &&
638 type != BCH_DATA_journal);
643 do_mark_fn(__bch2_mark_metadata_bucket, c, pos, flags,
644 ca, b, type, sectors);
646 __bch2_mark_metadata_bucket(c, ca, b, type, sectors, 0);
652 static s64 disk_sectors_scaled(unsigned n, unsigned d, unsigned sectors)
654 return DIV_ROUND_UP(sectors * n, d);
657 static s64 __ptr_disk_sectors_delta(unsigned old_size,
658 unsigned offset, s64 delta,
660 unsigned n, unsigned d)
664 if (flags & BTREE_TRIGGER_OVERWRITE_SPLIT) {
665 BUG_ON(offset + -delta > old_size);
667 return -disk_sectors_scaled(n, d, old_size) +
668 disk_sectors_scaled(n, d, offset) +
669 disk_sectors_scaled(n, d, old_size - offset + delta);
670 } else if (flags & BTREE_TRIGGER_OVERWRITE) {
671 BUG_ON(offset + -delta > old_size);
673 return -disk_sectors_scaled(n, d, old_size) +
674 disk_sectors_scaled(n, d, old_size + delta);
676 return disk_sectors_scaled(n, d, delta);
680 static s64 ptr_disk_sectors_delta(struct extent_ptr_decoded p,
681 unsigned offset, s64 delta,
684 return __ptr_disk_sectors_delta(p.crc.live_size,
685 offset, delta, flags,
686 p.crc.compressed_size,
687 p.crc.uncompressed_size);
690 static int check_bucket_ref(struct bch_fs *c, struct bkey_s_c k,
691 const struct bch_extent_ptr *ptr,
692 s64 sectors, enum bch_data_type ptr_data_type,
693 u8 bucket_gen, u8 bucket_data_type,
694 u16 dirty_sectors, u16 cached_sectors)
696 size_t bucket_nr = PTR_BUCKET_NR(bch_dev_bkey_exists(c, ptr->dev), ptr);
697 u16 bucket_sectors = !ptr->cached
702 if (gen_after(ptr->gen, bucket_gen)) {
703 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
704 "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
706 ptr->dev, bucket_nr, bucket_gen,
707 bch2_data_types[bucket_data_type ?: ptr_data_type],
709 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
713 if (gen_cmp(bucket_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
714 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
715 "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
717 ptr->dev, bucket_nr, bucket_gen,
718 bch2_data_types[bucket_data_type ?: ptr_data_type],
720 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
724 if (bucket_gen != ptr->gen && !ptr->cached) {
725 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
726 "bucket %u:%zu gen %u data type %s: stale dirty ptr (gen %u)\n"
728 ptr->dev, bucket_nr, bucket_gen,
729 bch2_data_types[bucket_data_type ?: ptr_data_type],
731 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
735 if (bucket_gen != ptr->gen)
738 if (bucket_data_type && ptr_data_type &&
739 bucket_data_type != ptr_data_type) {
740 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
741 "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
743 ptr->dev, bucket_nr, bucket_gen,
744 bch2_data_types[bucket_data_type],
745 bch2_data_types[ptr_data_type],
746 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
750 if ((unsigned) (bucket_sectors + sectors) > U16_MAX) {
751 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
752 "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
754 ptr->dev, bucket_nr, bucket_gen,
755 bch2_data_types[bucket_data_type ?: ptr_data_type],
756 bucket_sectors, sectors,
757 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
764 static int mark_stripe_bucket(struct bch_fs *c, struct bkey_s_c k,
766 struct bch_fs_usage *fs_usage,
767 u64 journal_seq, unsigned flags)
769 const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
770 unsigned nr_data = s->nr_blocks - s->nr_redundant;
771 bool parity = ptr_idx >= nr_data;
772 const struct bch_extent_ptr *ptr = s->ptrs + ptr_idx;
773 bool gc = flags & BTREE_TRIGGER_GC;
774 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
775 struct bucket *g = PTR_BUCKET(ca, ptr, gc);
776 struct bucket_mark new, old;
780 if (g->stripe && g->stripe != k.k->p.offset) {
781 bch2_fs_inconsistent(c,
782 "bucket %u:%zu gen %u: multiple stripes using same bucket\n%s",
783 ptr->dev, PTR_BUCKET_NR(ca, ptr), g->mark.gen,
784 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
788 old = bucket_cmpxchg(g, new, ({
789 ret = check_bucket_ref(c, k, ptr, 0, 0, new.gen, new.data_type,
790 new.dirty_sectors, new.cached_sectors);
795 new.data_type = BCH_DATA_parity;
796 new.dirty_sectors = le16_to_cpu(s->sectors);
800 new.journal_seq_valid = 1;
801 new.journal_seq = journal_seq;
805 g->stripe = k.k->p.offset;
806 g->stripe_redundancy = s->nr_redundant;
808 bch2_dev_usage_update(c, ca, fs_usage, old, new, journal_seq, gc);
812 static int __mark_pointer(struct bch_fs *c, struct bkey_s_c k,
813 const struct bch_extent_ptr *ptr,
814 s64 sectors, enum bch_data_type ptr_data_type,
815 u8 bucket_gen, u8 *bucket_data_type,
816 u16 *dirty_sectors, u16 *cached_sectors)
818 u16 *dst_sectors = !ptr->cached
821 int ret = check_bucket_ref(c, k, ptr, sectors, ptr_data_type,
822 bucket_gen, *bucket_data_type,
823 *dirty_sectors, *cached_sectors);
828 *dst_sectors += sectors;
829 *bucket_data_type = *dirty_sectors || *cached_sectors
834 static int bch2_mark_pointer(struct bch_fs *c, struct bkey_s_c k,
835 struct extent_ptr_decoded p,
836 s64 sectors, enum bch_data_type data_type,
837 struct bch_fs_usage *fs_usage,
838 u64 journal_seq, unsigned flags)
840 bool gc = flags & BTREE_TRIGGER_GC;
841 struct bucket_mark old, new;
842 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
843 struct bucket *g = PTR_BUCKET(ca, &p.ptr, gc);
848 v = atomic64_read(&g->_mark.v);
850 new.v.counter = old.v.counter = v;
851 bucket_data_type = new.data_type;
853 ret = __mark_pointer(c, k, &p.ptr, sectors, data_type, new.gen,
856 &new.cached_sectors);
860 new.data_type = bucket_data_type;
863 new.journal_seq_valid = 1;
864 new.journal_seq = journal_seq;
867 if (flags & BTREE_TRIGGER_NOATOMIC) {
871 } while ((v = atomic64_cmpxchg(&g->_mark.v,
873 new.v.counter)) != old.v.counter);
875 bch2_dev_usage_update(c, ca, fs_usage, old, new, journal_seq, gc);
877 BUG_ON(!gc && bucket_became_unavailable(old, new));
882 static int bch2_mark_stripe_ptr(struct bch_fs *c,
883 struct bch_extent_stripe_ptr p,
884 enum bch_data_type data_type,
885 struct bch_fs_usage *fs_usage,
886 s64 sectors, unsigned flags)
888 bool gc = flags & BTREE_TRIGGER_GC;
889 struct bch_replicas_padded r;
891 unsigned i, blocks_nonempty = 0;
893 m = genradix_ptr(&c->stripes[gc], p.idx);
895 spin_lock(&c->ec_stripes_heap_lock);
897 if (!m || !m->alive) {
898 spin_unlock(&c->ec_stripes_heap_lock);
899 bch_err_ratelimited(c, "pointer to nonexistent stripe %llu",
901 bch2_inconsistent_error(c);
905 m->block_sectors[p.block] += sectors;
909 for (i = 0; i < m->nr_blocks; i++)
910 blocks_nonempty += m->block_sectors[i] != 0;
912 if (m->blocks_nonempty != blocks_nonempty) {
913 m->blocks_nonempty = blocks_nonempty;
915 bch2_stripes_heap_update(c, m, p.idx);
918 spin_unlock(&c->ec_stripes_heap_lock);
920 r.e.data_type = data_type;
921 update_replicas(c, fs_usage, &r.e, sectors);
926 static int bch2_mark_extent(struct bch_fs *c,
927 struct bkey_s_c old, struct bkey_s_c new,
928 unsigned offset, s64 sectors,
929 enum bch_data_type data_type,
930 struct bch_fs_usage *fs_usage,
931 unsigned journal_seq, unsigned flags)
933 struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
934 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
935 const union bch_extent_entry *entry;
936 struct extent_ptr_decoded p;
937 struct bch_replicas_padded r;
938 s64 dirty_sectors = 0;
942 r.e.data_type = data_type;
948 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
949 s64 disk_sectors = data_type == BCH_DATA_btree
951 : ptr_disk_sectors_delta(p, offset, sectors, flags);
953 ret = bch2_mark_pointer(c, k, p, disk_sectors, data_type,
954 fs_usage, journal_seq, flags);
962 if (update_cached_sectors(c, fs_usage, p.ptr.dev,
964 bch2_fs_fatal_error(c, "bch2_mark_extent(): no replicas entry while updating cached sectors");
968 } else if (!p.has_ec) {
969 dirty_sectors += disk_sectors;
970 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
972 ret = bch2_mark_stripe_ptr(c, p.ec, data_type,
973 fs_usage, disk_sectors, flags);
978 * There may be other dirty pointers in this extent, but
979 * if so they're not required for mounting if we have an
980 * erasure coded pointer in this extent:
987 if (update_replicas(c, fs_usage, &r.e, dirty_sectors)) {
990 bch2_bkey_val_to_text(&PBUF(buf), c, k);
991 bch2_fs_fatal_error(c, "no replicas entry for %s", buf);
999 static int bch2_mark_stripe(struct bch_fs *c,
1000 struct bkey_s_c old, struct bkey_s_c new,
1001 struct bch_fs_usage *fs_usage,
1002 u64 journal_seq, unsigned flags)
1004 bool gc = flags & BTREE_TRIGGER_GC;
1005 size_t idx = new.k->p.offset;
1006 const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
1007 ? bkey_s_c_to_stripe(old).v : NULL;
1008 const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
1009 ? bkey_s_c_to_stripe(new).v : NULL;
1010 struct stripe *m = genradix_ptr(&c->stripes[gc], idx);
1014 BUG_ON(gc && old_s);
1016 if (!m || (old_s && !m->alive)) {
1017 bch_err_ratelimited(c, "error marking nonexistent stripe %zu",
1019 bch2_inconsistent_error(c);
1024 spin_lock(&c->ec_stripes_heap_lock);
1025 bch2_stripes_heap_del(c, m, idx);
1026 spin_unlock(&c->ec_stripes_heap_lock);
1028 memset(m, 0, sizeof(*m));
1031 m->sectors = le16_to_cpu(new_s->sectors);
1032 m->algorithm = new_s->algorithm;
1033 m->nr_blocks = new_s->nr_blocks;
1034 m->nr_redundant = new_s->nr_redundant;
1035 m->blocks_nonempty = 0;
1037 for (i = 0; i < new_s->nr_blocks; i++) {
1038 m->block_sectors[i] =
1039 stripe_blockcount_get(new_s, i);
1040 m->blocks_nonempty += !!m->block_sectors[i];
1042 m->ptrs[i] = new_s->ptrs[i];
1045 bch2_bkey_to_replicas(&m->r.e, new);
1048 spin_lock(&c->ec_stripes_heap_lock);
1049 bch2_stripes_heap_update(c, m, idx);
1050 spin_unlock(&c->ec_stripes_heap_lock);
1056 * gc recalculates this field from stripe ptr
1059 memset(m->block_sectors, 0, sizeof(m->block_sectors));
1060 m->blocks_nonempty = 0;
1062 for (i = 0; i < new_s->nr_blocks; i++) {
1063 ret = mark_stripe_bucket(c, new, i, fs_usage,
1064 journal_seq, flags);
1069 if (update_replicas(c, fs_usage, &m->r.e,
1070 ((s64) m->sectors * m->nr_redundant))) {
1073 bch2_bkey_val_to_text(&PBUF(buf), c, new);
1074 bch2_fs_fatal_error(c, "no replicas entry for %s", buf);
1082 static int bch2_mark_key_locked(struct bch_fs *c,
1083 struct bkey_s_c old,
1084 struct bkey_s_c new,
1085 unsigned offset, s64 sectors,
1086 struct bch_fs_usage *fs_usage,
1087 u64 journal_seq, unsigned flags)
1089 struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
1092 BUG_ON(!(flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)));
1096 if (!fs_usage || (flags & BTREE_TRIGGER_GC))
1097 fs_usage = fs_usage_ptr(c, journal_seq,
1098 flags & BTREE_TRIGGER_GC);
1100 switch (k.k->type) {
1101 case KEY_TYPE_alloc:
1102 case KEY_TYPE_alloc_v2:
1103 ret = bch2_mark_alloc(c, old, new, fs_usage, journal_seq, flags);
1105 case KEY_TYPE_btree_ptr:
1106 case KEY_TYPE_btree_ptr_v2:
1107 sectors = !(flags & BTREE_TRIGGER_OVERWRITE)
1108 ? c->opts.btree_node_size
1109 : -c->opts.btree_node_size;
1111 ret = bch2_mark_extent(c, old, new, offset, sectors,
1112 BCH_DATA_btree, fs_usage, journal_seq, flags);
1114 case KEY_TYPE_extent:
1115 case KEY_TYPE_reflink_v:
1116 ret = bch2_mark_extent(c, old, new, offset, sectors,
1117 BCH_DATA_user, fs_usage, journal_seq, flags);
1119 case KEY_TYPE_stripe:
1120 ret = bch2_mark_stripe(c, old, new, fs_usage, journal_seq, flags);
1122 case KEY_TYPE_inode:
1123 fs_usage->nr_inodes += new.k->type == KEY_TYPE_inode;
1124 fs_usage->nr_inodes -= old.k->type == KEY_TYPE_inode;
1126 case KEY_TYPE_reservation: {
1127 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1129 sectors *= replicas;
1130 replicas = clamp_t(unsigned, replicas, 1,
1131 ARRAY_SIZE(fs_usage->persistent_reserved));
1133 fs_usage->reserved += sectors;
1134 fs_usage->persistent_reserved[replicas - 1] += sectors;
1144 int bch2_mark_key(struct bch_fs *c, struct bkey_s_c new,
1145 unsigned offset, s64 sectors,
1146 struct bch_fs_usage *fs_usage,
1147 u64 journal_seq, unsigned flags)
1149 struct bkey deleted;
1150 struct bkey_s_c old = (struct bkey_s_c) { &deleted, NULL };
1153 bkey_init(&deleted);
1155 percpu_down_read(&c->mark_lock);
1156 ret = bch2_mark_key_locked(c, old, new, offset, sectors,
1157 fs_usage, journal_seq,
1158 BTREE_TRIGGER_INSERT|flags);
1159 percpu_up_read(&c->mark_lock);
1164 int bch2_mark_update(struct btree_trans *trans,
1165 struct btree_iter *iter,
1167 struct bch_fs_usage *fs_usage,
1170 struct bch_fs *c = trans->c;
1171 struct bkey_s_c old;
1172 struct bkey unpacked;
1175 if (unlikely(flags & BTREE_TRIGGER_NORUN))
1178 if (!btree_node_type_needs_gc(iter->btree_id))
1181 bkey_init(&unpacked);
1182 old = (struct bkey_s_c) { &unpacked, NULL };
1184 if (!btree_node_type_is_extents(iter->btree_id)) {
1185 /* iterators should be uptodate, shouldn't get errors here: */
1186 if (btree_iter_type(iter) != BTREE_ITER_CACHED) {
1187 old = bch2_btree_iter_peek_slot(iter);
1188 BUG_ON(bkey_err(old));
1190 struct bkey_cached *ck = (void *) iter->l[0].b;
1193 old = bkey_i_to_s_c(ck->k);
1196 if (old.k->type == new->k.type) {
1197 bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
1198 fs_usage, trans->journal_res.seq,
1199 BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE|flags);
1202 bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
1203 fs_usage, trans->journal_res.seq,
1204 BTREE_TRIGGER_INSERT|flags);
1205 bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
1206 fs_usage, trans->journal_res.seq,
1207 BTREE_TRIGGER_OVERWRITE|flags);
1210 struct btree_iter *copy;
1212 BUG_ON(btree_iter_type(iter) == BTREE_ITER_CACHED);
1213 bch2_mark_key_locked(c, old, bkey_i_to_s_c(new),
1215 fs_usage, trans->journal_res.seq,
1216 BTREE_TRIGGER_INSERT|flags);
1218 copy = bch2_trans_copy_iter(trans, iter);
1220 for_each_btree_key_continue(copy, 0, old, ret) {
1221 unsigned offset = 0;
1222 s64 sectors = -((s64) old.k->size);
1224 flags |= BTREE_TRIGGER_OVERWRITE;
1226 if (bkey_cmp(new->k.p, bkey_start_pos(old.k)) <= 0)
1229 switch (bch2_extent_overlap(&new->k, old.k)) {
1230 case BCH_EXTENT_OVERLAP_ALL:
1232 sectors = -((s64) old.k->size);
1234 case BCH_EXTENT_OVERLAP_BACK:
1235 offset = bkey_start_offset(&new->k) -
1236 bkey_start_offset(old.k);
1237 sectors = bkey_start_offset(&new->k) -
1240 case BCH_EXTENT_OVERLAP_FRONT:
1242 sectors = bkey_start_offset(old.k) -
1245 case BCH_EXTENT_OVERLAP_MIDDLE:
1246 offset = bkey_start_offset(&new->k) -
1247 bkey_start_offset(old.k);
1248 sectors = -((s64) new->k.size);
1249 flags |= BTREE_TRIGGER_OVERWRITE_SPLIT;
1253 BUG_ON(sectors >= 0);
1255 ret = bch2_mark_key_locked(c, old, bkey_i_to_s_c(new),
1256 offset, sectors, fs_usage,
1257 trans->journal_res.seq, flags) ?: 1;
1261 bch2_trans_iter_put(trans, copy);
1267 static noinline __cold
1268 void fs_usage_apply_warn(struct btree_trans *trans,
1269 unsigned disk_res_sectors,
1270 s64 should_not_have_added)
1272 struct bch_fs *c = trans->c;
1273 struct btree_insert_entry *i;
1276 bch_err(c, "disk usage increased %lli more than %u sectors reserved",
1277 should_not_have_added, disk_res_sectors);
1279 trans_for_each_update(trans, i) {
1280 pr_err("while inserting");
1281 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(i->k));
1283 pr_err("overlapping with");
1285 if (btree_iter_type(i->iter) != BTREE_ITER_CACHED) {
1286 struct btree_iter *copy = bch2_trans_copy_iter(trans, i->iter);
1290 for_each_btree_key_continue(copy, 0, k, ret) {
1291 if (btree_node_type_is_extents(i->iter->btree_id)
1292 ? bkey_cmp(i->k->k.p, bkey_start_pos(k.k)) <= 0
1293 : bkey_cmp(i->k->k.p, k.k->p))
1296 bch2_bkey_val_to_text(&PBUF(buf), c, k);
1299 bch2_trans_iter_put(trans, copy);
1301 struct bkey_cached *ck = (void *) i->iter->l[0].b;
1304 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(ck->k));
1312 void bch2_trans_fs_usage_apply(struct btree_trans *trans,
1313 struct replicas_delta_list *deltas)
1315 struct bch_fs *c = trans->c;
1316 static int warned_disk_usage = 0;
1318 unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
1319 struct replicas_delta *d = deltas->d;
1320 struct replicas_delta *top = (void *) deltas->d + deltas->used;
1321 struct bch_fs_usage *dst;
1322 s64 added = 0, should_not_have_added;
1325 percpu_rwsem_assert_held(&c->mark_lock);
1328 dst = fs_usage_ptr(c, trans->journal_res.seq, false);
1330 for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
1331 switch (d->r.data_type) {
1332 case BCH_DATA_btree:
1334 case BCH_DATA_parity:
1338 BUG_ON(update_replicas(c, dst, &d->r, d->delta));
1341 dst->nr_inodes += deltas->nr_inodes;
1343 for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1344 added += deltas->persistent_reserved[i];
1345 dst->reserved += deltas->persistent_reserved[i];
1346 dst->persistent_reserved[i] += deltas->persistent_reserved[i];
1350 * Not allowed to reduce sectors_available except by getting a
1353 should_not_have_added = added - (s64) disk_res_sectors;
1354 if (unlikely(should_not_have_added > 0)) {
1355 atomic64_sub(should_not_have_added, &c->sectors_available);
1356 added -= should_not_have_added;
1361 trans->disk_res->sectors -= added;
1362 this_cpu_sub(*c->online_reserved, added);
1367 if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
1368 fs_usage_apply_warn(trans, disk_res_sectors, should_not_have_added);
1373 static struct btree_iter *trans_get_update(struct btree_trans *trans,
1374 enum btree_id btree_id, struct bpos pos,
1377 struct btree_insert_entry *i;
1379 trans_for_each_update(trans, i)
1380 if (i->iter->btree_id == btree_id &&
1381 (btree_node_type_is_extents(btree_id)
1382 ? bkey_cmp(pos, bkey_start_pos(&i->k->k)) >= 0 &&
1383 bkey_cmp(pos, i->k->k.p) < 0
1384 : !bkey_cmp(pos, i->iter->pos))) {
1385 *k = bkey_i_to_s_c(i->k);
1388 BUG_ON(btree_iter_live(trans, i->iter));
1389 trans->iters_live |= 1ULL << i->iter->idx;
1396 static int trans_get_key(struct btree_trans *trans,
1397 enum btree_id btree_id, struct bpos pos,
1398 struct btree_iter **iter,
1401 unsigned flags = btree_id != BTREE_ID_alloc
1403 : BTREE_ITER_CACHED;
1406 *iter = trans_get_update(trans, btree_id, pos, k);
1410 *iter = bch2_trans_get_iter(trans, btree_id, pos,
1411 flags|BTREE_ITER_INTENT);
1412 *k = __bch2_btree_iter_peek(*iter, flags);
1415 bch2_trans_iter_put(trans, *iter);
1419 static struct bkey_alloc_buf *
1420 bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter **_iter,
1421 const struct bch_extent_ptr *ptr,
1422 struct bkey_alloc_unpacked *u)
1424 struct bch_fs *c = trans->c;
1425 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
1426 struct bpos pos = POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
1428 struct btree_iter *iter;
1430 struct bkey_alloc_buf *a;
1433 a = bch2_trans_kmalloc(trans, sizeof(struct bkey_alloc_buf));
1437 iter = trans_get_update(trans, BTREE_ID_alloc, pos, &k);
1439 *u = bch2_alloc_unpack(k);
1441 iter = bch2_trans_get_iter(trans, BTREE_ID_alloc, pos,
1443 BTREE_ITER_CACHED_NOFILL|
1445 ret = bch2_btree_iter_traverse(iter);
1447 bch2_trans_iter_put(trans, iter);
1448 return ERR_PTR(ret);
1451 percpu_down_read(&c->mark_lock);
1452 g = bucket(ca, pos.offset);
1453 *u = alloc_mem_to_key(iter, g, READ_ONCE(g->mark));
1454 percpu_up_read(&c->mark_lock);
1461 static int bch2_trans_mark_pointer(struct btree_trans *trans,
1462 struct bkey_s_c k, struct extent_ptr_decoded p,
1463 s64 sectors, enum bch_data_type data_type)
1465 struct bch_fs *c = trans->c;
1466 struct btree_iter *iter;
1467 struct bkey_alloc_unpacked u;
1468 struct bkey_alloc_buf *a;
1471 a = bch2_trans_start_alloc_update(trans, &iter, &p.ptr, &u);
1475 ret = __mark_pointer(c, k, &p.ptr, sectors, data_type, u.gen, &u.data_type,
1476 &u.dirty_sectors, &u.cached_sectors);
1480 bch2_alloc_pack(c, a, u);
1481 bch2_trans_update(trans, iter, &a->k, 0);
1483 bch2_trans_iter_put(trans, iter);
1487 static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
1488 struct extent_ptr_decoded p,
1489 s64 sectors, enum bch_data_type data_type)
1491 struct bch_fs *c = trans->c;
1492 struct btree_iter *iter;
1494 struct bkey_i_stripe *s;
1495 struct bch_replicas_padded r;
1498 ret = trans_get_key(trans, BTREE_ID_stripes, POS(0, p.ec.idx), &iter, &k);
1502 if (k.k->type != KEY_TYPE_stripe) {
1503 bch2_fs_inconsistent(c,
1504 "pointer to nonexistent stripe %llu",
1506 bch2_inconsistent_error(c);
1511 if (!bch2_ptr_matches_stripe(bkey_s_c_to_stripe(k).v, p)) {
1512 bch2_fs_inconsistent(c,
1513 "stripe pointer doesn't match stripe %llu",
1519 s = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1520 ret = PTR_ERR_OR_ZERO(s);
1524 bkey_reassemble(&s->k_i, k);
1525 stripe_blockcount_set(&s->v, p.ec.block,
1526 stripe_blockcount_get(&s->v, p.ec.block) +
1528 bch2_trans_update(trans, iter, &s->k_i, 0);
1530 bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
1531 r.e.data_type = data_type;
1532 update_replicas_list(trans, &r.e, sectors);
1534 bch2_trans_iter_put(trans, iter);
1538 static int bch2_trans_mark_extent(struct btree_trans *trans,
1539 struct bkey_s_c k, unsigned offset,
1540 s64 sectors, unsigned flags,
1541 enum bch_data_type data_type)
1543 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1544 const union bch_extent_entry *entry;
1545 struct extent_ptr_decoded p;
1546 struct bch_replicas_padded r;
1547 s64 dirty_sectors = 0;
1551 r.e.data_type = data_type;
1553 r.e.nr_required = 1;
1557 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1558 s64 disk_sectors = data_type == BCH_DATA_btree
1560 : ptr_disk_sectors_delta(p, offset, sectors, flags);
1562 ret = bch2_trans_mark_pointer(trans, k, p, disk_sectors,
1571 update_cached_sectors_list(trans, p.ptr.dev,
1573 } else if (!p.has_ec) {
1574 dirty_sectors += disk_sectors;
1575 r.e.devs[r.e.nr_devs++] = p.ptr.dev;
1577 ret = bch2_trans_mark_stripe_ptr(trans, p,
1578 disk_sectors, data_type);
1582 r.e.nr_required = 0;
1587 update_replicas_list(trans, &r.e, dirty_sectors);
1592 static int bch2_trans_mark_stripe_alloc_ref(struct btree_trans *trans,
1593 struct bkey_s_c_stripe s,
1594 unsigned idx, bool deleting)
1596 struct bch_fs *c = trans->c;
1597 const struct bch_extent_ptr *ptr = &s.v->ptrs[idx];
1598 struct bkey_alloc_buf *a;
1599 struct btree_iter *iter;
1600 struct bkey_alloc_unpacked u;
1601 bool parity = idx >= s.v->nr_blocks - s.v->nr_redundant;
1604 a = bch2_trans_start_alloc_update(trans, &iter, ptr, &u);
1609 s64 sectors = le16_to_cpu(s.v->sectors);
1614 u.dirty_sectors += sectors;
1615 u.data_type = u.dirty_sectors
1621 if (bch2_fs_inconsistent_on(u.stripe && u.stripe != s.k->p.offset, c,
1622 "bucket %llu:%llu gen %u: multiple stripes using same bucket (%u, %llu)",
1623 iter->pos.inode, iter->pos.offset, u.gen,
1624 u.stripe, s.k->p.offset)) {
1629 u.stripe = s.k->p.offset;
1630 u.stripe_redundancy = s.v->nr_redundant;
1633 u.stripe_redundancy = 0;
1636 bch2_alloc_pack(c, a, u);
1637 bch2_trans_update(trans, iter, &a->k, 0);
1639 bch2_trans_iter_put(trans, iter);
1643 static int bch2_trans_mark_stripe(struct btree_trans *trans,
1644 struct bkey_s_c old, struct bkey_s_c new,
1647 struct bkey_s_c_stripe old_s = { .k = NULL };
1648 struct bkey_s_c_stripe new_s = { .k = NULL };
1649 struct bch_replicas_padded r;
1653 if (old.k->type == KEY_TYPE_stripe)
1654 old_s = bkey_s_c_to_stripe(old);
1655 if (new.k->type == KEY_TYPE_stripe)
1656 new_s = bkey_s_c_to_stripe(new);
1659 * If the pointers aren't changing, we don't need to do anything:
1661 if (new_s.k && old_s.k &&
1662 new_s.v->nr_blocks == old_s.v->nr_blocks &&
1663 new_s.v->nr_redundant == old_s.v->nr_redundant &&
1664 !memcmp(old_s.v->ptrs, new_s.v->ptrs,
1665 new_s.v->nr_blocks * sizeof(struct bch_extent_ptr)))
1669 s64 sectors = le16_to_cpu(new_s.v->sectors);
1671 bch2_bkey_to_replicas(&r.e, new);
1672 update_replicas_list(trans, &r.e, sectors * new_s.v->nr_redundant);
1674 for (i = 0; i < new_s.v->nr_blocks; i++) {
1675 ret = bch2_trans_mark_stripe_alloc_ref(trans, new_s,
1683 s64 sectors = -((s64) le16_to_cpu(old_s.v->sectors));
1685 bch2_bkey_to_replicas(&r.e, old);
1686 update_replicas_list(trans, &r.e, sectors * old_s.v->nr_redundant);
1688 for (i = 0; i < old_s.v->nr_blocks; i++) {
1689 ret = bch2_trans_mark_stripe_alloc_ref(trans, old_s,
1699 static __le64 *bkey_refcount(struct bkey_i *k)
1701 switch (k->k.type) {
1702 case KEY_TYPE_reflink_v:
1703 return &bkey_i_to_reflink_v(k)->v.refcount;
1704 case KEY_TYPE_indirect_inline_data:
1705 return &bkey_i_to_indirect_inline_data(k)->v.refcount;
1711 static bool reflink_p_frag_references(struct bkey_s_c_reflink_p p,
1718 start += le64_to_cpu(p.v->idx);
1719 end += le64_to_cpu(p.v->idx);
1721 if (end <= bkey_start_offset(k.k))
1723 if (start >= k.k->p.offset)
1728 static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
1729 struct bkey_s_c_reflink_p p,
1730 u64 idx, unsigned sectors,
1731 unsigned front_frag,
1735 struct bch_fs *c = trans->c;
1736 struct btree_iter *iter;
1740 int add = !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1;
1743 ret = trans_get_key(trans, BTREE_ID_reflink,
1744 POS(0, idx), &iter, &k);
1748 if (reflink_p_frag_references(p, 0, front_frag, k) &&
1749 reflink_p_frag_references(p, back_frag, p.k->size, k)) {
1750 BUG_ON(!(flags & BTREE_TRIGGER_OVERWRITE_SPLIT));
1752 } else if (reflink_p_frag_references(p, 0, front_frag, k) ||
1753 reflink_p_frag_references(p, back_frag, p.k->size, k)) {
1754 BUG_ON(!(flags & BTREE_TRIGGER_OVERWRITE));
1758 sectors = min_t(u64, sectors, k.k->p.offset - idx);
1760 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1761 ret = PTR_ERR_OR_ZERO(n);
1765 bkey_reassemble(n, k);
1767 refcount = bkey_refcount(n);
1769 bch2_fs_inconsistent(c,
1770 "%llu:%llu len %u points to nonexistent indirect extent %llu",
1771 p.k->p.inode, p.k->p.offset, p.k->size, idx);
1772 bch2_inconsistent_error(c);
1777 BUG_ON(!*refcount && (flags & BTREE_TRIGGER_OVERWRITE));
1778 le64_add_cpu(refcount, add);
1781 n->k.type = KEY_TYPE_deleted;
1782 set_bkey_val_u64s(&n->k, 0);
1785 bch2_btree_iter_set_pos(iter, bkey_start_pos(k.k));
1786 bch2_trans_update(trans, iter, n, 0);
1790 bch2_trans_iter_put(trans, iter);
1794 static int bch2_trans_mark_reflink_p(struct btree_trans *trans,
1795 struct bkey_s_c_reflink_p p, unsigned offset,
1796 s64 sectors, unsigned flags)
1798 u64 idx = le64_to_cpu(p.v->idx) + offset;
1799 unsigned front_frag, back_frag;
1805 BUG_ON(offset + sectors > p.k->size);
1807 front_frag = offset;
1808 back_frag = offset + sectors;
1811 ret = __bch2_trans_mark_reflink_p(trans, p, idx, sectors,
1812 front_frag, back_frag, flags);
1817 sectors = max_t(s64, 0LL, sectors - ret);
1824 int bch2_trans_mark_key(struct btree_trans *trans,
1825 struct bkey_s_c old,
1826 struct bkey_s_c new,
1827 unsigned offset, s64 sectors, unsigned flags)
1829 struct bch_fs *c = trans->c;
1830 struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
1831 struct replicas_delta_list *d;
1833 BUG_ON(!(flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)));
1835 switch (k.k->type) {
1836 case KEY_TYPE_btree_ptr:
1837 case KEY_TYPE_btree_ptr_v2:
1838 sectors = !(flags & BTREE_TRIGGER_OVERWRITE)
1839 ? c->opts.btree_node_size
1840 : -c->opts.btree_node_size;
1842 return bch2_trans_mark_extent(trans, k, offset, sectors,
1843 flags, BCH_DATA_btree);
1844 case KEY_TYPE_extent:
1845 case KEY_TYPE_reflink_v:
1846 return bch2_trans_mark_extent(trans, k, offset, sectors,
1847 flags, BCH_DATA_user);
1848 case KEY_TYPE_stripe:
1849 return bch2_trans_mark_stripe(trans, old, new, flags);
1850 case KEY_TYPE_inode: {
1851 int nr = (new.k->type == KEY_TYPE_inode) -
1852 (old.k->type == KEY_TYPE_inode);
1855 d = replicas_deltas_realloc(trans, 0);
1861 case KEY_TYPE_reservation: {
1862 unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
1864 d = replicas_deltas_realloc(trans, 0);
1866 sectors *= replicas;
1867 replicas = clamp_t(unsigned, replicas, 1,
1868 ARRAY_SIZE(d->persistent_reserved));
1870 d->persistent_reserved[replicas - 1] += sectors;
1873 case KEY_TYPE_reflink_p:
1874 return bch2_trans_mark_reflink_p(trans,
1875 bkey_s_c_to_reflink_p(k),
1876 offset, sectors, flags);
1882 int bch2_trans_mark_update(struct btree_trans *trans,
1883 struct btree_iter *iter,
1887 struct bkey_s_c old;
1890 if (unlikely(flags & BTREE_TRIGGER_NORUN))
1893 if (!btree_node_type_needs_gc(iter->btree_id))
1896 if (!btree_node_type_is_extents(iter->btree_id)) {
1897 if (btree_iter_type(iter) != BTREE_ITER_CACHED) {
1898 old = bch2_btree_iter_peek_slot(iter);
1899 ret = bkey_err(old);
1903 struct bkey_cached *ck = (void *) iter->l[0].b;
1906 old = bkey_i_to_s_c(ck->k);
1909 if (old.k->type == new->k.type) {
1910 ret = bch2_trans_mark_key(trans, old, bkey_i_to_s_c(new), 0, 0,
1911 BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE|flags);
1913 ret = bch2_trans_mark_key(trans, old, bkey_i_to_s_c(new), 0, 0,
1914 BTREE_TRIGGER_INSERT|flags) ?:
1915 bch2_trans_mark_key(trans, old, bkey_i_to_s_c(new), 0, 0,
1916 BTREE_TRIGGER_OVERWRITE|flags);
1919 struct btree_iter *copy;
1922 EBUG_ON(btree_iter_type(iter) == BTREE_ITER_CACHED);
1925 old = (struct bkey_s_c) { &_old, NULL };
1927 ret = bch2_trans_mark_key(trans, old, bkey_i_to_s_c(new),
1929 BTREE_TRIGGER_INSERT);
1933 copy = bch2_trans_copy_iter(trans, iter);
1935 for_each_btree_key_continue(copy, 0, old, ret) {
1936 unsigned offset = 0;
1937 s64 sectors = -((s64) old.k->size);
1939 flags |= BTREE_TRIGGER_OVERWRITE;
1941 if (bkey_cmp(new->k.p, bkey_start_pos(old.k)) <= 0)
1944 switch (bch2_extent_overlap(&new->k, old.k)) {
1945 case BCH_EXTENT_OVERLAP_ALL:
1947 sectors = -((s64) old.k->size);
1949 case BCH_EXTENT_OVERLAP_BACK:
1950 offset = bkey_start_offset(&new->k) -
1951 bkey_start_offset(old.k);
1952 sectors = bkey_start_offset(&new->k) -
1955 case BCH_EXTENT_OVERLAP_FRONT:
1957 sectors = bkey_start_offset(old.k) -
1960 case BCH_EXTENT_OVERLAP_MIDDLE:
1961 offset = bkey_start_offset(&new->k) -
1962 bkey_start_offset(old.k);
1963 sectors = -((s64) new->k.size);
1964 flags |= BTREE_TRIGGER_OVERWRITE_SPLIT;
1968 BUG_ON(sectors >= 0);
1970 ret = bch2_trans_mark_key(trans, old, bkey_i_to_s_c(new),
1971 offset, sectors, flags);
1975 bch2_trans_iter_put(trans, copy);
1981 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
1982 struct bch_dev *ca, size_t b,
1983 enum bch_data_type type,
1986 struct bch_fs *c = trans->c;
1987 struct btree_iter *iter;
1988 struct bkey_alloc_unpacked u;
1989 struct bkey_alloc_buf *a;
1990 struct bch_extent_ptr ptr = {
1992 .offset = bucket_to_sector(ca, b),
1996 a = bch2_trans_start_alloc_update(trans, &iter, &ptr, &u);
2000 if (u.data_type && u.data_type != type) {
2001 bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
2002 "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
2004 iter->pos.inode, iter->pos.offset, u.gen,
2005 bch2_data_types[u.data_type],
2006 bch2_data_types[type],
2007 bch2_data_types[type]);
2013 u.dirty_sectors = sectors;
2015 bch2_alloc_pack(c, a, u);
2016 bch2_trans_update(trans, iter, &a->k, 0);
2018 bch2_trans_iter_put(trans, iter);
2022 int bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
2023 struct bch_dev *ca, size_t b,
2024 enum bch_data_type type,
2027 return __bch2_trans_do(trans, NULL, NULL, 0,
2028 __bch2_trans_mark_metadata_bucket(trans, ca, b, type, sectors));
2031 static int bch2_trans_mark_metadata_sectors(struct btree_trans *trans,
2034 enum bch_data_type type,
2035 u64 *bucket, unsigned *bucket_sectors)
2038 u64 b = sector_to_bucket(ca, start);
2040 min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
2042 if (b != *bucket && *bucket_sectors) {
2043 int ret = bch2_trans_mark_metadata_bucket(trans, ca, *bucket,
2044 type, *bucket_sectors);
2048 *bucket_sectors = 0;
2052 *bucket_sectors += sectors;
2054 } while (start < end);
2059 static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
2062 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
2064 unsigned i, bucket_sectors = 0;
2067 for (i = 0; i < layout->nr_superblocks; i++) {
2068 u64 offset = le64_to_cpu(layout->sb_offset[i]);
2070 if (offset == BCH_SB_SECTOR) {
2071 ret = bch2_trans_mark_metadata_sectors(trans, ca,
2073 BCH_DATA_sb, &bucket, &bucket_sectors);
2078 ret = bch2_trans_mark_metadata_sectors(trans, ca, offset,
2079 offset + (1 << layout->sb_max_size_bits),
2080 BCH_DATA_sb, &bucket, &bucket_sectors);
2085 if (bucket_sectors) {
2086 ret = bch2_trans_mark_metadata_bucket(trans, ca,
2087 bucket, BCH_DATA_sb, bucket_sectors);
2092 for (i = 0; i < ca->journal.nr; i++) {
2093 ret = bch2_trans_mark_metadata_bucket(trans, ca,
2094 ca->journal.buckets[i],
2095 BCH_DATA_journal, ca->mi.bucket_size);
2103 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
2105 return bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
2106 __bch2_trans_mark_dev_sb(&trans, ca));
2109 /* Disk reservations: */
2111 #define SECTORS_CACHE 1024
2113 int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
2114 u64 sectors, int flags)
2116 struct bch_fs_pcpu *pcpu;
2118 s64 sectors_available;
2121 percpu_down_read(&c->mark_lock);
2123 pcpu = this_cpu_ptr(c->pcpu);
2125 if (sectors <= pcpu->sectors_available)
2128 v = atomic64_read(&c->sectors_available);
2131 get = min((u64) sectors + SECTORS_CACHE, old);
2133 if (get < sectors) {
2137 } while ((v = atomic64_cmpxchg(&c->sectors_available,
2138 old, old - get)) != old);
2140 pcpu->sectors_available += get;
2143 pcpu->sectors_available -= sectors;
2144 this_cpu_add(*c->online_reserved, sectors);
2145 res->sectors += sectors;
2148 percpu_up_read(&c->mark_lock);
2152 mutex_lock(&c->sectors_available_lock);
2154 percpu_u64_set(&c->pcpu->sectors_available, 0);
2155 sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
2157 if (sectors <= sectors_available ||
2158 (flags & BCH_DISK_RESERVATION_NOFAIL)) {
2159 atomic64_set(&c->sectors_available,
2160 max_t(s64, 0, sectors_available - sectors));
2161 this_cpu_add(*c->online_reserved, sectors);
2162 res->sectors += sectors;
2165 atomic64_set(&c->sectors_available, sectors_available);
2169 mutex_unlock(&c->sectors_available_lock);
2170 percpu_up_read(&c->mark_lock);
2175 /* Startup/shutdown: */
2177 static void buckets_free_rcu(struct rcu_head *rcu)
2179 struct bucket_array *buckets =
2180 container_of(rcu, struct bucket_array, rcu);
2183 sizeof(struct bucket_array) +
2184 buckets->nbuckets * sizeof(struct bucket));
2187 int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
2189 struct bucket_array *buckets = NULL, *old_buckets = NULL;
2190 unsigned long *buckets_nouse = NULL;
2191 alloc_fifo free[RESERVE_NR];
2192 alloc_fifo free_inc;
2193 alloc_heap alloc_heap;
2195 size_t btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE,
2196 ca->mi.bucket_size / c->opts.btree_node_size);
2197 /* XXX: these should be tunable */
2198 size_t reserve_none = max_t(size_t, 1, nbuckets >> 9);
2199 size_t copygc_reserve = max_t(size_t, 2, nbuckets >> 6);
2200 size_t free_inc_nr = max(max_t(size_t, 1, nbuckets >> 12),
2202 bool resize = ca->buckets[0] != NULL;
2206 memset(&free, 0, sizeof(free));
2207 memset(&free_inc, 0, sizeof(free_inc));
2208 memset(&alloc_heap, 0, sizeof(alloc_heap));
2210 if (!(buckets = kvpmalloc(sizeof(struct bucket_array) +
2211 nbuckets * sizeof(struct bucket),
2212 GFP_KERNEL|__GFP_ZERO)) ||
2213 !(buckets_nouse = kvpmalloc(BITS_TO_LONGS(nbuckets) *
2214 sizeof(unsigned long),
2215 GFP_KERNEL|__GFP_ZERO)) ||
2216 !init_fifo(&free[RESERVE_MOVINGGC],
2217 copygc_reserve, GFP_KERNEL) ||
2218 !init_fifo(&free[RESERVE_NONE], reserve_none, GFP_KERNEL) ||
2219 !init_fifo(&free_inc, free_inc_nr, GFP_KERNEL) ||
2220 !init_heap(&alloc_heap, ALLOC_SCAN_BATCH(ca) << 1, GFP_KERNEL))
2223 buckets->first_bucket = ca->mi.first_bucket;
2224 buckets->nbuckets = nbuckets;
2226 bch2_copygc_stop(c);
2229 down_write(&c->gc_lock);
2230 down_write(&ca->bucket_lock);
2231 percpu_down_write(&c->mark_lock);
2234 old_buckets = bucket_array(ca);
2237 size_t n = min(buckets->nbuckets, old_buckets->nbuckets);
2241 n * sizeof(struct bucket));
2242 memcpy(buckets_nouse,
2244 BITS_TO_LONGS(n) * sizeof(unsigned long));
2247 rcu_assign_pointer(ca->buckets[0], buckets);
2248 buckets = old_buckets;
2250 swap(ca->buckets_nouse, buckets_nouse);
2253 percpu_up_write(&c->mark_lock);
2254 up_write(&c->gc_lock);
2257 spin_lock(&c->freelist_lock);
2258 for (i = 0; i < RESERVE_NR; i++) {
2259 fifo_move(&free[i], &ca->free[i]);
2260 swap(ca->free[i], free[i]);
2262 fifo_move(&free_inc, &ca->free_inc);
2263 swap(ca->free_inc, free_inc);
2264 spin_unlock(&c->freelist_lock);
2266 /* with gc lock held, alloc_heap can't be in use: */
2267 swap(ca->alloc_heap, alloc_heap);
2269 nbuckets = ca->mi.nbuckets;
2272 up_write(&ca->bucket_lock);
2276 free_heap(&alloc_heap);
2277 free_fifo(&free_inc);
2278 for (i = 0; i < RESERVE_NR; i++)
2279 free_fifo(&free[i]);
2280 kvpfree(buckets_nouse,
2281 BITS_TO_LONGS(nbuckets) * sizeof(unsigned long));
2283 call_rcu(&old_buckets->rcu, buckets_free_rcu);
2288 void bch2_dev_buckets_free(struct bch_dev *ca)
2292 free_heap(&ca->alloc_heap);
2293 free_fifo(&ca->free_inc);
2294 for (i = 0; i < RESERVE_NR; i++)
2295 free_fifo(&ca->free[i]);
2296 kvpfree(ca->buckets_nouse,
2297 BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
2298 kvpfree(rcu_dereference_protected(ca->buckets[0], 1),
2299 sizeof(struct bucket_array) +
2300 ca->mi.nbuckets * sizeof(struct bucket));
2302 for (i = 0; i < ARRAY_SIZE(ca->usage); i++)
2303 free_percpu(ca->usage[i]);
2304 kfree(ca->usage_base);
2307 int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
2311 ca->usage_base = kzalloc(sizeof(struct bch_dev_usage), GFP_KERNEL);
2312 if (!ca->usage_base)
2315 for (i = 0; i < ARRAY_SIZE(ca->usage); i++) {
2316 ca->usage[i] = alloc_percpu(struct bch_dev_usage);
2321 return bch2_dev_buckets_resize(c, ca, ca->mi.nbuckets);;