2 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
4 * Code for managing the extent btree and dynamically updating the writeback
9 #include "bkey_methods.h"
11 #include "btree_update.h"
12 #include "btree_update_interior.h"
17 #include "disk_groups.h"
28 #include <trace/events/bcachefs.h>
30 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
32 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
33 const struct bch_extent_ptr *ptr;
36 bkey_for_each_ptr(p, ptr)
42 unsigned bch2_bkey_nr_dirty_ptrs(struct bkey_s_c k)
47 case KEY_TYPE_btree_ptr:
48 case KEY_TYPE_extent: {
49 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
50 const struct bch_extent_ptr *ptr;
52 bkey_for_each_ptr(p, ptr)
53 nr_ptrs += !ptr->cached;
57 case KEY_TYPE_reservation:
58 nr_ptrs = bkey_s_c_to_reservation(k).v->nr_replicas;
65 static unsigned bch2_extent_ptr_durability(struct bch_fs *c,
66 struct extent_ptr_decoded p)
68 unsigned i, durability = 0;
74 ca = bch_dev_bkey_exists(c, p.ptr.dev);
76 if (ca->mi.state != BCH_MEMBER_STATE_FAILED)
77 durability = max_t(unsigned, durability, ca->mi.durability);
79 for (i = 0; i < p.ec_nr; i++) {
81 genradix_ptr(&c->stripes[0], p.idx);
86 durability = max_t(unsigned, durability, s->nr_redundant);
92 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
94 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
95 const union bch_extent_entry *entry;
96 struct extent_ptr_decoded p;
97 unsigned durability = 0;
99 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
100 durability += bch2_extent_ptr_durability(c, p);
105 static struct bch_dev_io_failures *dev_io_failures(struct bch_io_failures *f,
108 struct bch_dev_io_failures *i;
110 for (i = f->devs; i < f->devs + f->nr; i++)
117 void bch2_mark_io_failure(struct bch_io_failures *failed,
118 struct extent_ptr_decoded *p)
120 struct bch_dev_io_failures *f = dev_io_failures(failed, p->ptr.dev);
123 BUG_ON(failed->nr >= ARRAY_SIZE(failed->devs));
125 f = &failed->devs[failed->nr++];
130 } else if (p->idx != f->idx) {
140 * returns true if p1 is better than p2:
142 static inline bool ptr_better(struct bch_fs *c,
143 const struct extent_ptr_decoded p1,
144 const struct extent_ptr_decoded p2)
146 if (likely(!p1.idx && !p2.idx)) {
147 struct bch_dev *dev1 = bch_dev_bkey_exists(c, p1.ptr.dev);
148 struct bch_dev *dev2 = bch_dev_bkey_exists(c, p2.ptr.dev);
150 u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
151 u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
153 /* Pick at random, biased in favor of the faster device: */
155 return bch2_rand_range(l1 + l2) > l1;
158 if (force_reconstruct_read(c))
159 return p1.idx > p2.idx;
161 return p1.idx < p2.idx;
165 * This picks a non-stale pointer, preferably from a device other than @avoid.
166 * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to
167 * other devices, it will still pick a pointer from avoid.
169 int bch2_bkey_pick_read_device(struct bch_fs *c, struct bkey_s_c k,
170 struct bch_io_failures *failed,
171 struct extent_ptr_decoded *pick)
173 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
174 const union bch_extent_entry *entry;
175 struct extent_ptr_decoded p;
176 struct bch_dev_io_failures *f;
180 if (k.k->type == KEY_TYPE_error)
183 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
184 ca = bch_dev_bkey_exists(c, p.ptr.dev);
187 * If there are any dirty pointers it's an error if we can't
190 if (!ret && !p.ptr.cached)
193 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
196 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
198 p.idx = f->nr_failed < f->nr_retries
203 !bch2_dev_is_readable(ca))
206 if (force_reconstruct_read(c) &&
210 if (p.idx >= p.ec_nr + 1)
213 if (ret > 0 && !ptr_better(c, p, *pick))
223 void bch2_bkey_append_ptr(struct bkey_i *k,
224 struct bch_extent_ptr ptr)
226 EBUG_ON(bch2_bkey_has_device(bkey_i_to_s_c(k), ptr.dev));
229 case KEY_TYPE_btree_ptr:
230 case KEY_TYPE_extent:
231 EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
233 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
235 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
245 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
247 struct bch_extent_ptr *ptr;
249 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
252 /* extent specific utility code */
254 const struct bch_extent_ptr *
255 bch2_extent_has_device(struct bkey_s_c_extent e, unsigned dev)
257 const struct bch_extent_ptr *ptr;
259 extent_for_each_ptr(e, ptr)
266 const struct bch_extent_ptr *
267 bch2_extent_has_group(struct bch_fs *c, struct bkey_s_c_extent e, unsigned group)
269 const struct bch_extent_ptr *ptr;
271 extent_for_each_ptr(e, ptr) {
272 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
275 ca->mi.group - 1 == group)
282 const struct bch_extent_ptr *
283 bch2_extent_has_target(struct bch_fs *c, struct bkey_s_c_extent e, unsigned target)
285 const struct bch_extent_ptr *ptr;
287 extent_for_each_ptr(e, ptr)
288 if (bch2_dev_in_target(c, ptr->dev, target) &&
290 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
296 unsigned bch2_extent_is_compressed(struct bkey_s_c k)
301 case KEY_TYPE_extent: {
302 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
303 const union bch_extent_entry *entry;
304 struct extent_ptr_decoded p;
306 extent_for_each_ptr_decode(e, p, entry)
308 p.crc.compression_type != BCH_COMPRESSION_NONE &&
309 p.crc.compressed_size < p.crc.live_size)
310 ret += p.crc.compressed_size;
317 bool bch2_extent_matches_ptr(struct bch_fs *c, struct bkey_s_c_extent e,
318 struct bch_extent_ptr m, u64 offset)
320 const union bch_extent_entry *entry;
321 struct extent_ptr_decoded p;
323 extent_for_each_ptr_decode(e, p, entry)
324 if (p.ptr.dev == m.dev &&
325 p.ptr.gen == m.gen &&
326 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(e.k) ==
327 (s64) m.offset - offset)
333 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
334 union bch_extent_entry *entry)
336 union bch_extent_entry *i = ptrs.start;
341 while (extent_entry_next(i) != entry)
342 i = extent_entry_next(i);
346 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
347 struct bch_extent_ptr *ptr)
349 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
350 union bch_extent_entry *dst, *src, *prev;
351 bool drop_crc = true;
353 EBUG_ON(ptr < &ptrs.start->ptr ||
354 ptr >= &ptrs.end->ptr);
355 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
357 src = extent_entry_next(to_entry(ptr));
358 if (src != ptrs.end &&
359 !extent_entry_is_crc(src))
363 while ((prev = extent_entry_prev(ptrs, dst))) {
364 if (extent_entry_is_ptr(prev))
367 if (extent_entry_is_crc(prev)) {
376 memmove_u64s_down(dst, src,
377 (u64 *) ptrs.end - (u64 *) src);
378 k.k->u64s -= (u64 *) src - (u64 *) dst;
383 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
384 struct bch_extent_crc_unpacked n)
386 return !u.compression_type &&
388 u.uncompressed_size > u.live_size &&
389 bch2_csum_type_is_encryption(u.csum_type) ==
390 bch2_csum_type_is_encryption(n.csum_type);
393 bool bch2_can_narrow_extent_crcs(struct bkey_s_c_extent e,
394 struct bch_extent_crc_unpacked n)
396 struct bch_extent_crc_unpacked crc;
397 const union bch_extent_entry *i;
402 extent_for_each_crc(e, crc, i)
403 if (can_narrow_crc(crc, n))
410 * We're writing another replica for this extent, so while we've got the data in
411 * memory we'll be computing a new checksum for the currently live data.
413 * If there are other replicas we aren't moving, and they are checksummed but
414 * not compressed, we can modify them to point to only the data that is
415 * currently live (so that readers won't have to bounce) while we've got the
418 bool bch2_extent_narrow_crcs(struct bkey_i_extent *e,
419 struct bch_extent_crc_unpacked n)
421 struct bch_extent_crc_unpacked u;
422 struct extent_ptr_decoded p;
423 union bch_extent_entry *i;
426 /* Find a checksum entry that covers only live data: */
428 extent_for_each_crc(extent_i_to_s(e), u, i)
429 if (!u.compression_type &&
431 u.live_size == u.uncompressed_size) {
438 BUG_ON(n.compression_type);
440 BUG_ON(n.live_size != e->k.size);
442 restart_narrow_pointers:
443 extent_for_each_ptr_decode(extent_i_to_s(e), p, i)
444 if (can_narrow_crc(p.crc, n)) {
445 bch2_bkey_drop_ptr(extent_i_to_s(e).s, &i->ptr);
446 p.ptr.offset += p.crc.offset;
448 bch2_extent_ptr_decoded_append(e, &p);
450 goto restart_narrow_pointers;
456 /* returns true if not equal */
457 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
458 struct bch_extent_crc_unpacked r)
460 return (l.csum_type != r.csum_type ||
461 l.compression_type != r.compression_type ||
462 l.compressed_size != r.compressed_size ||
463 l.uncompressed_size != r.uncompressed_size ||
464 l.offset != r.offset ||
465 l.live_size != r.live_size ||
466 l.nonce != r.nonce ||
467 bch2_crc_cmp(l.csum, r.csum));
470 void bch2_ptr_swab(const struct bkey_format *f, struct bkey_packed *k)
472 union bch_extent_entry *entry;
473 u64 *d = (u64 *) bkeyp_val(f, k);
476 for (i = 0; i < bkeyp_val_u64s(f, k); i++)
479 for (entry = (union bch_extent_entry *) d;
480 entry < (union bch_extent_entry *) (d + bkeyp_val_u64s(f, k));
481 entry = extent_entry_next(entry)) {
482 switch (extent_entry_type(entry)) {
483 case BCH_EXTENT_ENTRY_ptr:
485 case BCH_EXTENT_ENTRY_crc32:
486 entry->crc32.csum = swab32(entry->crc32.csum);
488 case BCH_EXTENT_ENTRY_crc64:
489 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
490 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
492 case BCH_EXTENT_ENTRY_crc128:
493 entry->crc128.csum.hi = (__force __le64)
494 swab64((__force u64) entry->crc128.csum.hi);
495 entry->crc128.csum.lo = (__force __le64)
496 swab64((__force u64) entry->crc128.csum.lo);
498 case BCH_EXTENT_ENTRY_stripe_ptr:
504 static const char *extent_ptr_invalid(const struct bch_fs *c,
506 const struct bch_extent_ptr *ptr,
507 unsigned size_ondisk,
510 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
511 const struct bch_extent_ptr *ptr2;
514 if (ptr->dev >= c->sb.nr_devices ||
516 return "pointer to invalid device";
518 ca = bch_dev_bkey_exists(c, ptr->dev);
520 return "pointer to invalid device";
522 bkey_for_each_ptr(ptrs, ptr2)
523 if (ptr != ptr2 && ptr->dev == ptr2->dev)
524 return "multiple pointers to same device";
526 if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
527 return "offset past end of device";
529 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
530 return "offset before first bucket";
532 if (bucket_remainder(ca, ptr->offset) +
533 size_ondisk > ca->mi.bucket_size)
534 return "spans multiple buckets";
539 static void bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
542 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
543 const union bch_extent_entry *entry;
544 struct bch_extent_crc_unpacked crc;
545 const struct bch_extent_ptr *ptr;
546 const struct bch_extent_stripe_ptr *ec;
550 bkey_extent_entry_for_each(ptrs, entry) {
554 switch (__extent_entry_type(entry)) {
555 case BCH_EXTENT_ENTRY_ptr:
556 ptr = entry_to_ptr(entry);
557 ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
558 ? bch_dev_bkey_exists(c, ptr->dev)
561 pr_buf(out, "ptr: %u:%llu gen %u%s%s", ptr->dev,
562 (u64) ptr->offset, ptr->gen,
563 ptr->cached ? " cached" : "",
564 ca && ptr_stale(ca, ptr)
567 case BCH_EXTENT_ENTRY_crc32:
568 case BCH_EXTENT_ENTRY_crc64:
569 case BCH_EXTENT_ENTRY_crc128:
570 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
572 pr_buf(out, "crc: c_size %u size %u offset %u nonce %u csum %u compress %u",
574 crc.uncompressed_size,
575 crc.offset, crc.nonce,
577 crc.compression_type);
579 case BCH_EXTENT_ENTRY_stripe_ptr:
580 ec = &entry->stripe_ptr;
582 pr_buf(out, "ec: idx %llu block %u",
583 (u64) ec->idx, ec->block);
586 pr_buf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
596 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
598 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
599 const union bch_extent_entry *entry;
600 const struct bch_extent_ptr *ptr;
603 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
604 return "value too big";
606 bkey_extent_entry_for_each(ptrs, entry) {
607 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
608 return "invalid extent entry type";
610 if (!extent_entry_is_ptr(entry))
611 return "has non ptr field";
614 bkey_for_each_ptr(ptrs, ptr) {
615 reason = extent_ptr_invalid(c, k, ptr,
616 c->opts.btree_node_size,
625 void bch2_btree_ptr_debugcheck(struct bch_fs *c, struct btree *b,
628 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
629 const struct bch_extent_ptr *ptr;
633 struct bucket_mark mark;
635 unsigned replicas = 0;
638 bkey_for_each_ptr(ptrs, ptr) {
639 ca = bch_dev_bkey_exists(c, ptr->dev);
642 if (!test_bit(BCH_FS_ALLOC_READ_DONE, &c->flags))
646 if (ptr_stale(ca, ptr))
650 seq = read_seqcount_begin(&c->gc_pos_lock);
651 mark = ptr_bucket_mark(ca, ptr);
653 bad = gc_pos_cmp(c->gc_pos, gc_pos_btree_node(b)) > 0 &&
654 (mark.data_type != BCH_DATA_BTREE ||
655 mark.dirty_sectors < c->opts.btree_node_size);
656 } while (read_seqcount_retry(&c->gc_pos_lock, seq));
658 err = "inconsistent";
663 if (!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
664 !bch2_bkey_replicas_marked(c, k, false)) {
665 bch2_bkey_val_to_text(&PBUF(buf), c, k);
667 "btree key bad (replicas not marked in superblock):\n%s",
674 bch2_bkey_val_to_text(&PBUF(buf), c, k);
675 bch2_fs_bug(c, "%s btree pointer %s: bucket %zi gen %i mark %08x",
676 err, buf, PTR_BUCKET_NR(ca, ptr),
677 mark.gen, (unsigned) mark.v.counter);
680 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
685 bkey_ptrs_to_text(out, c, k);
687 invalid = bch2_btree_ptr_invalid(c, k);
689 pr_buf(out, " invalid: %s", invalid);
694 bool __bch2_cut_front(struct bpos where, struct bkey_s k)
698 if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
701 EBUG_ON(bkey_cmp(where, k.k->p) > 0);
703 len = k.k->p.offset - where.offset;
705 BUG_ON(len > k.k->size);
708 * Don't readjust offset if the key size is now 0, because that could
709 * cause offset to point to the next bucket:
712 k.k->type = KEY_TYPE_deleted;
713 else if (bkey_extent_is_data(k.k)) {
714 struct bkey_s_extent e = bkey_s_to_extent(k);
715 union bch_extent_entry *entry;
716 bool seen_crc = false;
718 extent_for_each_entry(e, entry) {
719 switch (extent_entry_type(entry)) {
720 case BCH_EXTENT_ENTRY_ptr:
722 entry->ptr.offset += e.k->size - len;
724 case BCH_EXTENT_ENTRY_crc32:
725 entry->crc32.offset += e.k->size - len;
727 case BCH_EXTENT_ENTRY_crc64:
728 entry->crc64.offset += e.k->size - len;
730 case BCH_EXTENT_ENTRY_crc128:
731 entry->crc128.offset += e.k->size - len;
733 case BCH_EXTENT_ENTRY_stripe_ptr:
737 if (extent_entry_is_crc(entry))
747 bool bch2_cut_back(struct bpos where, struct bkey *k)
751 if (bkey_cmp(where, k->p) >= 0)
754 EBUG_ON(bkey_cmp(where, bkey_start_pos(k)) < 0);
756 len = where.offset - bkey_start_offset(k);
758 BUG_ON(len > k->size);
764 k->type = KEY_TYPE_deleted;
770 * bch_key_resize - adjust size of @k
772 * bkey_start_offset(k) will be preserved, modifies where the extent ends
774 void bch2_key_resize(struct bkey *k,
777 k->p.offset -= k->size;
778 k->p.offset += new_size;
782 static bool extent_i_save(struct btree *b, struct bkey_packed *dst,
785 struct bkey_format *f = &b->format;
786 struct bkey_i *dst_unpacked;
787 struct bkey_packed tmp;
789 if ((dst_unpacked = packed_to_bkey(dst)))
790 dst_unpacked->k = src->k;
791 else if (bch2_bkey_pack_key(&tmp, &src->k, f))
792 memcpy_u64s(dst, &tmp, f->key_u64s);
796 memcpy_u64s(bkeyp_val(f, dst), &src->v, bkey_val_u64s(&src->k));
800 struct extent_insert_state {
801 struct btree_insert *trans;
802 struct btree_insert_entry *insert;
803 struct bpos committed;
806 struct bkey_i whiteout;
812 static bool bch2_extent_merge_inline(struct bch_fs *,
814 struct bkey_packed *,
815 struct bkey_packed *,
818 static void verify_extent_nonoverlapping(struct btree *b,
819 struct btree_node_iter *_iter,
820 struct bkey_i *insert)
822 #ifdef CONFIG_BCACHEFS_DEBUG
823 struct btree_node_iter iter;
824 struct bkey_packed *k;
828 k = bch2_btree_node_iter_prev_filter(&iter, b, KEY_TYPE_discard);
830 (uk = bkey_unpack_key(b, k),
831 bkey_cmp(uk.p, bkey_start_pos(&insert->k)) > 0));
834 k = bch2_btree_node_iter_peek_filter(&iter, b, KEY_TYPE_discard);
837 (uk = bkey_unpack_key(b, k),
838 bkey_cmp(insert->k.p, bkey_start_pos(&uk))) > 0);
841 (uk = bkey_unpack_key(b, k),
842 bkey_cmp(insert->k.p, bkey_start_pos(&uk))) > 0) {
846 bch2_bkey_to_text(&PBUF(buf1), &insert->k);
847 bch2_bkey_to_text(&PBUF(buf2), &uk);
849 bch2_dump_btree_node(b);
850 panic("insert > next :\n"
860 static void verify_modified_extent(struct btree_iter *iter,
861 struct bkey_packed *k)
863 bch2_btree_iter_verify(iter, iter->l[0].b);
864 bch2_verify_insert_pos(iter->l[0].b, k, k, k->u64s);
867 static void extent_bset_insert(struct bch_fs *c, struct btree_iter *iter,
868 struct bkey_i *insert)
870 struct btree_iter_level *l = &iter->l[0];
871 struct btree_node_iter node_iter;
872 struct bkey_packed *k;
874 BUG_ON(insert->k.u64s > bch_btree_keys_u64s_remaining(c, l->b));
876 EBUG_ON(bkey_deleted(&insert->k) || !insert->k.size);
877 verify_extent_nonoverlapping(l->b, &l->iter, insert);
880 k = bch2_btree_node_iter_prev_filter(&node_iter, l->b, KEY_TYPE_discard);
881 if (k && !bkey_written(l->b, k) &&
882 bch2_extent_merge_inline(c, iter, k, bkey_to_packed(insert), true))
886 k = bch2_btree_node_iter_peek_filter(&node_iter, l->b, KEY_TYPE_discard);
887 if (k && !bkey_written(l->b, k) &&
888 bch2_extent_merge_inline(c, iter, bkey_to_packed(insert), k, false))
891 k = bch2_btree_node_iter_bset_pos(&l->iter, l->b, bset_tree_last(l->b));
893 bch2_bset_insert(l->b, &l->iter, k, insert, 0);
894 bch2_btree_node_iter_fix(iter, l->b, &l->iter, k, 0, k->u64s);
895 bch2_btree_iter_verify(iter, l->b);
898 static void extent_insert_committed(struct extent_insert_state *s)
900 struct bch_fs *c = s->trans->c;
901 struct btree_iter *iter = s->insert->iter;
902 struct bkey_i *insert = s->insert->k;
903 BKEY_PADDED(k) split;
905 EBUG_ON(bkey_cmp(insert->k.p, s->committed) < 0);
906 EBUG_ON(bkey_cmp(s->committed, bkey_start_pos(&insert->k)) < 0);
908 bkey_copy(&split.k, insert);
910 split.k.k.type = KEY_TYPE_discard;
912 bch2_cut_back(s->committed, &split.k.k);
914 if (!bkey_cmp(s->committed, iter->pos))
917 bch2_btree_iter_set_pos_same_leaf(iter, s->committed);
919 if (s->update_btree) {
920 if (debug_check_bkeys(c))
921 bch2_bkey_debugcheck(c, iter->l[0].b,
922 bkey_i_to_s_c(&split.k));
924 EBUG_ON(bkey_deleted(&split.k.k) || !split.k.k.size);
926 extent_bset_insert(c, iter, &split.k);
929 if (s->update_journal) {
930 bkey_copy(&split.k, !s->deleting ? insert : &s->whiteout);
932 split.k.k.type = KEY_TYPE_discard;
934 bch2_cut_back(s->committed, &split.k.k);
936 EBUG_ON(bkey_deleted(&split.k.k) || !split.k.k.size);
938 bch2_btree_journal_key(s->trans, iter, &split.k);
941 bch2_cut_front(s->committed, insert);
943 insert->k.needs_whiteout = false;
946 void bch2_extent_trim_atomic(struct bkey_i *k, struct btree_iter *iter)
948 struct btree *b = iter->l[0].b;
950 BUG_ON(iter->uptodate > BTREE_ITER_NEED_PEEK);
952 bch2_cut_back(b->key.k.p, &k->k);
954 BUG_ON(bkey_cmp(bkey_start_pos(&k->k), b->data->min_key) < 0);
957 enum btree_insert_ret
958 bch2_extent_can_insert(struct btree_insert *trans,
959 struct btree_insert_entry *insert,
962 struct btree_iter_level *l = &insert->iter->l[0];
963 struct btree_node_iter node_iter = l->iter;
964 enum bch_extent_overlap overlap;
965 struct bkey_packed *_k;
966 struct bkey unpacked;
970 BUG_ON(trans->flags & BTREE_INSERT_ATOMIC &&
971 !bch2_extent_is_atomic(&insert->k->k, insert->iter));
974 * We avoid creating whiteouts whenever possible when deleting, but
975 * those optimizations mean we may potentially insert two whiteouts
976 * instead of one (when we overlap with the front of one extent and the
979 if (bkey_whiteout(&insert->k->k))
982 _k = bch2_btree_node_iter_peek_filter(&node_iter, l->b,
985 return BTREE_INSERT_OK;
987 k = bkey_disassemble(l->b, _k, &unpacked);
989 overlap = bch2_extent_overlap(&insert->k->k, k.k);
991 /* account for having to split existing extent: */
992 if (overlap == BCH_EXTENT_OVERLAP_MIDDLE)
995 if (overlap == BCH_EXTENT_OVERLAP_MIDDLE &&
996 (sectors = bch2_extent_is_compressed(k))) {
997 int flags = BCH_DISK_RESERVATION_BTREE_LOCKS_HELD;
999 if (trans->flags & BTREE_INSERT_NOFAIL)
1000 flags |= BCH_DISK_RESERVATION_NOFAIL;
1002 switch (bch2_disk_reservation_add(trans->c,
1008 return BTREE_INSERT_ENOSPC;
1010 return BTREE_INSERT_NEED_GC_LOCK;
1016 return BTREE_INSERT_OK;
1020 extent_squash(struct extent_insert_state *s, struct bkey_i *insert,
1021 struct bkey_packed *_k, struct bkey_s k,
1022 enum bch_extent_overlap overlap)
1024 struct bch_fs *c = s->trans->c;
1025 struct btree_iter *iter = s->insert->iter;
1026 struct btree_iter_level *l = &iter->l[0];
1029 case BCH_EXTENT_OVERLAP_FRONT:
1030 /* insert overlaps with start of k: */
1031 __bch2_cut_front(insert->k.p, k);
1032 BUG_ON(bkey_deleted(k.k));
1033 extent_save(l->b, _k, k.k);
1034 verify_modified_extent(iter, _k);
1037 case BCH_EXTENT_OVERLAP_BACK:
1038 /* insert overlaps with end of k: */
1039 bch2_cut_back(bkey_start_pos(&insert->k), k.k);
1040 BUG_ON(bkey_deleted(k.k));
1041 extent_save(l->b, _k, k.k);
1044 * As the auxiliary tree is indexed by the end of the
1045 * key and we've just changed the end, update the
1048 bch2_bset_fix_invalidated_key(l->b, _k);
1049 bch2_btree_node_iter_fix(iter, l->b, &l->iter,
1050 _k, _k->u64s, _k->u64s);
1051 verify_modified_extent(iter, _k);
1054 case BCH_EXTENT_OVERLAP_ALL: {
1055 /* The insert key completely covers k, invalidate k */
1056 if (!bkey_whiteout(k.k))
1057 btree_account_key_drop(l->b, _k);
1060 k.k->type = KEY_TYPE_deleted;
1062 if (_k >= btree_bset_last(l->b)->start) {
1063 unsigned u64s = _k->u64s;
1065 bch2_bset_delete(l->b, _k, _k->u64s);
1066 bch2_btree_node_iter_fix(iter, l->b, &l->iter,
1068 bch2_btree_iter_verify(iter, l->b);
1070 extent_save(l->b, _k, k.k);
1071 bch2_btree_node_iter_fix(iter, l->b, &l->iter,
1072 _k, _k->u64s, _k->u64s);
1073 verify_modified_extent(iter, _k);
1078 case BCH_EXTENT_OVERLAP_MIDDLE: {
1079 BKEY_PADDED(k) split;
1081 * The insert key falls 'in the middle' of k
1082 * The insert key splits k in 3:
1083 * - start only in k, preserve
1084 * - middle common section, invalidate in k
1085 * - end only in k, preserve
1087 * We update the old key to preserve the start,
1088 * insert will be the new common section,
1089 * we manually insert the end that we are preserving.
1091 * modify k _before_ doing the insert (which will move
1094 bkey_reassemble(&split.k, k.s_c);
1095 split.k.k.needs_whiteout |= bkey_written(l->b, _k);
1097 bch2_cut_back(bkey_start_pos(&insert->k), &split.k.k);
1098 BUG_ON(bkey_deleted(&split.k.k));
1100 __bch2_cut_front(insert->k.p, k);
1101 BUG_ON(bkey_deleted(k.k));
1102 extent_save(l->b, _k, k.k);
1103 verify_modified_extent(iter, _k);
1105 extent_bset_insert(c, iter, &split.k);
1111 static void __bch2_insert_fixup_extent(struct extent_insert_state *s)
1113 struct btree_iter *iter = s->insert->iter;
1114 struct btree_iter_level *l = &iter->l[0];
1115 struct bkey_packed *_k;
1116 struct bkey unpacked;
1117 struct bkey_i *insert = s->insert->k;
1119 while (bkey_cmp(s->committed, insert->k.p) < 0 &&
1120 (_k = bch2_btree_node_iter_peek_filter(&l->iter, l->b,
1121 KEY_TYPE_discard))) {
1122 struct bkey_s k = __bkey_disassemble(l->b, _k, &unpacked);
1123 enum bch_extent_overlap overlap = bch2_extent_overlap(&insert->k, k.k);
1125 EBUG_ON(bkey_cmp(iter->pos, k.k->p) >= 0);
1127 if (bkey_cmp(bkey_start_pos(k.k), insert->k.p) >= 0)
1130 s->committed = bpos_min(s->insert->k->k.p, k.k->p);
1132 if (!bkey_whiteout(k.k))
1133 s->update_journal = true;
1135 if (!s->update_journal) {
1136 bch2_cut_front(s->committed, insert);
1137 bch2_cut_front(s->committed, &s->whiteout);
1138 bch2_btree_iter_set_pos_same_leaf(iter, s->committed);
1143 * When deleting, if possible just do it by switching the type
1144 * of the key we're deleting, instead of creating and inserting
1149 !bkey_cmp(insert->k.p, k.k->p) &&
1150 !bkey_cmp(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) {
1151 if (!bkey_whiteout(k.k)) {
1152 btree_account_key_drop(l->b, _k);
1153 _k->type = KEY_TYPE_discard;
1154 reserve_whiteout(l->b, _k);
1159 if (k.k->needs_whiteout || bkey_written(l->b, _k)) {
1160 insert->k.needs_whiteout = true;
1161 s->update_btree = true;
1164 if (s->update_btree &&
1165 overlap == BCH_EXTENT_OVERLAP_ALL &&
1166 bkey_whiteout(k.k) &&
1167 k.k->needs_whiteout) {
1168 unreserve_whiteout(l->b, _k);
1169 _k->needs_whiteout = false;
1172 extent_squash(s, insert, _k, k, overlap);
1174 if (!s->update_btree)
1175 bch2_cut_front(s->committed, insert);
1177 if (overlap == BCH_EXTENT_OVERLAP_FRONT ||
1178 overlap == BCH_EXTENT_OVERLAP_MIDDLE)
1182 if (bkey_cmp(s->committed, insert->k.p) < 0)
1183 s->committed = bpos_min(s->insert->k->k.p, l->b->key.k.p);
1186 * may have skipped past some deleted extents greater than the insert
1187 * key, before we got to a non deleted extent and knew we could bail out
1188 * rewind the iterator a bit if necessary:
1191 struct btree_node_iter node_iter = l->iter;
1193 while ((_k = bch2_btree_node_iter_prev_all(&node_iter, l->b)) &&
1194 bkey_cmp_left_packed(l->b, _k, &s->committed) > 0)
1195 l->iter = node_iter;
1200 * bch_extent_insert_fixup - insert a new extent and deal with overlaps
1202 * this may result in not actually doing the insert, or inserting some subset
1203 * of the insert key. For cmpxchg operations this is where that logic lives.
1205 * All subsets of @insert that need to be inserted are inserted using
1206 * bch2_btree_insert_and_journal(). If @b or @res fills up, this function
1207 * returns false, setting @iter->pos for the prefix of @insert that actually got
1210 * BSET INVARIANTS: this function is responsible for maintaining all the
1211 * invariants for bsets of extents in memory. things get really hairy with 0
1216 * bkey_start_pos(bkey_next(k)) >= k
1217 * or bkey_start_offset(bkey_next(k)) >= k->offset
1219 * i.e. strict ordering, no overlapping extents.
1221 * multiple bsets (i.e. full btree node):
1224 * k.size != 0 ∧ j.size != 0 →
1225 * ¬ (k > bkey_start_pos(j) ∧ k < j)
1227 * i.e. no two overlapping keys _of nonzero size_
1229 * We can't realistically maintain this invariant for zero size keys because of
1230 * the key merging done in bch2_btree_insert_key() - for two mergeable keys k, j
1231 * there may be another 0 size key between them in another bset, and it will
1232 * thus overlap with the merged key.
1234 * In addition, the end of iter->pos indicates how much has been processed.
1235 * If the end of iter->pos is not the same as the end of insert, then
1236 * key insertion needs to continue/be retried.
1238 enum btree_insert_ret
1239 bch2_insert_fixup_extent(struct btree_insert *trans,
1240 struct btree_insert_entry *insert)
1242 struct btree_iter *iter = insert->iter;
1243 struct btree *b = iter->l[0].b;
1244 struct extent_insert_state s = {
1247 .committed = iter->pos,
1249 .whiteout = *insert->k,
1250 .update_journal = !bkey_whiteout(&insert->k->k),
1251 .update_btree = !bkey_whiteout(&insert->k->k),
1252 .deleting = bkey_whiteout(&insert->k->k),
1255 EBUG_ON(iter->level);
1256 EBUG_ON(!insert->k->k.size);
1259 * As we process overlapping extents, we advance @iter->pos both to
1260 * signal to our caller (btree_insert_key()) how much of @insert->k has
1261 * been inserted, and also to keep @iter->pos consistent with
1262 * @insert->k and the node iterator that we're advancing:
1264 EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k->k)));
1266 __bch2_insert_fixup_extent(&s);
1268 extent_insert_committed(&s);
1270 EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k->k)));
1271 EBUG_ON(bkey_cmp(iter->pos, s.committed));
1273 if (insert->k->k.size) {
1274 /* got to the end of this leaf node */
1275 BUG_ON(bkey_cmp(iter->pos, b->key.k.p));
1276 return BTREE_INSERT_NEED_TRAVERSE;
1279 return BTREE_INSERT_OK;
1282 const char *bch2_extent_invalid(const struct bch_fs *c, struct bkey_s_c k)
1284 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
1285 const union bch_extent_entry *entry;
1286 struct bch_extent_crc_unpacked crc;
1287 const struct bch_extent_ptr *ptr;
1288 unsigned size_ondisk = e.k->size;
1290 unsigned nonce = UINT_MAX;
1292 if (bkey_val_u64s(e.k) > BKEY_EXTENT_VAL_U64s_MAX)
1293 return "value too big";
1295 extent_for_each_entry(e, entry) {
1296 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
1297 return "invalid extent entry type";
1299 switch (extent_entry_type(entry)) {
1300 case BCH_EXTENT_ENTRY_ptr:
1301 ptr = entry_to_ptr(entry);
1303 reason = extent_ptr_invalid(c, e.s_c, &entry->ptr,
1304 size_ondisk, false);
1308 case BCH_EXTENT_ENTRY_crc32:
1309 case BCH_EXTENT_ENTRY_crc64:
1310 case BCH_EXTENT_ENTRY_crc128:
1311 crc = bch2_extent_crc_unpack(e.k, entry_to_crc(entry));
1313 if (crc.offset + e.k->size >
1314 crc.uncompressed_size)
1315 return "checksum offset + key size > uncompressed size";
1317 size_ondisk = crc.compressed_size;
1319 if (!bch2_checksum_type_valid(c, crc.csum_type))
1320 return "invalid checksum type";
1322 if (crc.compression_type >= BCH_COMPRESSION_NR)
1323 return "invalid compression type";
1325 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1326 if (nonce == UINT_MAX)
1327 nonce = crc.offset + crc.nonce;
1328 else if (nonce != crc.offset + crc.nonce)
1329 return "incorrect nonce";
1332 case BCH_EXTENT_ENTRY_stripe_ptr:
1340 void bch2_extent_debugcheck(struct bch_fs *c, struct btree *b,
1343 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
1344 const struct bch_extent_ptr *ptr;
1346 struct bucket_mark mark;
1347 unsigned seq, stale;
1350 unsigned replicas = 0;
1353 * XXX: we should be doing most/all of these checks at startup time,
1354 * where we check bch2_bkey_invalid() in btree_node_read_done()
1356 * But note that we can't check for stale pointers or incorrect gc marks
1357 * until after journal replay is done (it might be an extent that's
1358 * going to get overwritten during replay)
1361 extent_for_each_ptr(e, ptr) {
1362 ca = bch_dev_bkey_exists(c, ptr->dev);
1366 * If journal replay hasn't finished, we might be seeing keys
1367 * that will be overwritten by the time journal replay is done:
1369 if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
1375 seq = read_seqcount_begin(&c->gc_pos_lock);
1376 mark = ptr_bucket_mark(ca, ptr);
1378 /* between mark and bucket gen */
1381 stale = ptr_stale(ca, ptr);
1383 bch2_fs_bug_on(stale && !ptr->cached, c,
1384 "stale dirty pointer");
1386 bch2_fs_bug_on(stale > 96, c,
1387 "key too stale: %i",
1393 bad = gc_pos_cmp(c->gc_pos, gc_pos_btree_node(b)) > 0 &&
1394 (mark.data_type != BCH_DATA_USER ||
1396 ? mark.cached_sectors
1397 : mark.dirty_sectors));
1398 } while (read_seqcount_retry(&c->gc_pos_lock, seq));
1404 if (replicas > BCH_REPLICAS_MAX) {
1405 bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c);
1407 "extent key bad (too many replicas: %u): %s",
1412 if (!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
1413 !bch2_bkey_replicas_marked(c, e.s_c, false)) {
1414 bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c);
1416 "extent key bad (replicas not marked in superblock):\n%s",
1424 bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c);
1425 bch2_fs_bug(c, "extent pointer bad gc mark: %s:\nbucket %zu "
1426 "gen %i type %u", buf,
1427 PTR_BUCKET_NR(ca, ptr), mark.gen, mark.data_type);
1430 void bch2_extent_to_text(struct printbuf *out, struct bch_fs *c,
1433 const char *invalid;
1435 bkey_ptrs_to_text(out, c, k);
1437 invalid = bch2_extent_invalid(c, k);
1439 pr_buf(out, " invalid: %s", invalid);
1442 static void bch2_extent_crc_init(union bch_extent_crc *crc,
1443 struct bch_extent_crc_unpacked new)
1445 #define common_fields(_crc) \
1446 .csum_type = _crc.csum_type, \
1447 .compression_type = _crc.compression_type, \
1448 ._compressed_size = _crc.compressed_size - 1, \
1449 ._uncompressed_size = _crc.uncompressed_size - 1, \
1450 .offset = _crc.offset
1452 if (bch_crc_bytes[new.csum_type] <= 4 &&
1453 new.uncompressed_size <= CRC32_SIZE_MAX &&
1454 new.nonce <= CRC32_NONCE_MAX) {
1455 crc->crc32 = (struct bch_extent_crc32) {
1456 .type = 1 << BCH_EXTENT_ENTRY_crc32,
1458 .csum = *((__le32 *) &new.csum.lo),
1463 if (bch_crc_bytes[new.csum_type] <= 10 &&
1464 new.uncompressed_size <= CRC64_SIZE_MAX &&
1465 new.nonce <= CRC64_NONCE_MAX) {
1466 crc->crc64 = (struct bch_extent_crc64) {
1467 .type = 1 << BCH_EXTENT_ENTRY_crc64,
1470 .csum_lo = new.csum.lo,
1471 .csum_hi = *((__le16 *) &new.csum.hi),
1476 if (bch_crc_bytes[new.csum_type] <= 16 &&
1477 new.uncompressed_size <= CRC128_SIZE_MAX &&
1478 new.nonce <= CRC128_NONCE_MAX) {
1479 crc->crc128 = (struct bch_extent_crc128) {
1480 .type = 1 << BCH_EXTENT_ENTRY_crc128,
1487 #undef common_fields
1491 void bch2_extent_crc_append(struct bkey_i_extent *e,
1492 struct bch_extent_crc_unpacked new)
1494 bch2_extent_crc_init((void *) extent_entry_last(extent_i_to_s(e)), new);
1495 __extent_entry_push(e);
1498 void bch2_extent_ptr_decoded_append(struct bkey_i_extent *e,
1499 struct extent_ptr_decoded *p)
1501 struct bch_extent_crc_unpacked crc;
1502 union bch_extent_entry *pos;
1505 extent_for_each_crc(extent_i_to_s(e), crc, pos)
1506 if (!bch2_crc_unpacked_cmp(crc, p->crc))
1509 bch2_extent_crc_append(e, p->crc);
1510 pos = extent_entry_last(extent_i_to_s(e));
1512 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
1513 __extent_entry_insert(e, pos, to_entry(&p->ptr));
1515 for (i = 0; i < p->ec_nr; i++) {
1516 p->ec[i].type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
1517 __extent_entry_insert(e, pos, to_entry(&p->ec[i]));
1522 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
1524 * Returns true if @k should be dropped entirely
1526 * For existing keys, only called when btree nodes are being rewritten, not when
1527 * they're merely being compacted/resorted in memory.
1529 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
1531 struct bch_extent_ptr *ptr;
1533 bch2_bkey_drop_ptrs(k, ptr,
1535 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
1537 /* will only happen if all pointers were cached: */
1538 if (!bkey_val_u64s(k.k))
1539 k.k->type = KEY_TYPE_deleted;
1544 void bch2_extent_mark_replicas_cached(struct bch_fs *c,
1545 struct bkey_s_extent e,
1547 unsigned nr_desired_replicas)
1549 union bch_extent_entry *entry;
1550 struct extent_ptr_decoded p;
1551 int extra = bch2_bkey_durability(c, e.s_c) - nr_desired_replicas;
1553 if (target && extra > 0)
1554 extent_for_each_ptr_decode(e, p, entry) {
1555 int n = bch2_extent_ptr_durability(c, p);
1557 if (n && n <= extra &&
1558 !bch2_dev_in_target(c, p.ptr.dev, target)) {
1559 entry->ptr.cached = true;
1565 extent_for_each_ptr_decode(e, p, entry) {
1566 int n = bch2_extent_ptr_durability(c, p);
1568 if (n && n <= extra) {
1569 entry->ptr.cached = true;
1575 enum merge_result bch2_extent_merge(struct bch_fs *c,
1576 struct bkey_i *l, struct bkey_i *r)
1578 struct bkey_s_extent el = bkey_i_to_s_extent(l);
1579 struct bkey_s_extent er = bkey_i_to_s_extent(r);
1580 union bch_extent_entry *en_l, *en_r;
1582 if (bkey_val_u64s(&l->k) != bkey_val_u64s(&r->k))
1583 return BCH_MERGE_NOMERGE;
1585 extent_for_each_entry(el, en_l) {
1586 struct bch_extent_ptr *lp, *rp;
1589 en_r = vstruct_idx(er.v, (u64 *) en_l - el.v->_data);
1591 if ((extent_entry_type(en_l) !=
1592 extent_entry_type(en_r)) ||
1593 !extent_entry_is_ptr(en_l))
1594 return BCH_MERGE_NOMERGE;
1599 if (lp->offset + el.k->size != rp->offset ||
1600 lp->dev != rp->dev ||
1602 return BCH_MERGE_NOMERGE;
1604 /* We don't allow extents to straddle buckets: */
1605 ca = bch_dev_bkey_exists(c, lp->dev);
1607 if (PTR_BUCKET_NR(ca, lp) != PTR_BUCKET_NR(ca, rp))
1608 return BCH_MERGE_NOMERGE;
1611 l->k.needs_whiteout |= r->k.needs_whiteout;
1613 /* Keys with no pointers aren't restricted to one bucket and could
1616 if ((u64) l->k.size + r->k.size > KEY_SIZE_MAX) {
1617 bch2_key_resize(&l->k, KEY_SIZE_MAX);
1618 bch2_cut_front(l->k.p, r);
1619 return BCH_MERGE_PARTIAL;
1622 bch2_key_resize(&l->k, l->k.size + r->k.size);
1624 return BCH_MERGE_MERGE;
1628 * When merging an extent that we're inserting into a btree node, the new merged
1629 * extent could overlap with an existing 0 size extent - if we don't fix that,
1630 * it'll break the btree node iterator so this code finds those 0 size extents
1631 * and shifts them out of the way.
1633 * Also unpacks and repacks.
1635 static bool bch2_extent_merge_inline(struct bch_fs *c,
1636 struct btree_iter *iter,
1637 struct bkey_packed *l,
1638 struct bkey_packed *r,
1641 struct btree *b = iter->l[0].b;
1642 struct btree_node_iter *node_iter = &iter->l[0].iter;
1643 BKEY_PADDED(k) li, ri;
1644 struct bkey_packed *m = back_merge ? l : r;
1645 struct bkey_i *mi = back_merge ? &li.k : &ri.k;
1646 struct bset_tree *t = bch2_bkey_to_bset(b, m);
1647 enum merge_result ret;
1649 EBUG_ON(bkey_written(b, m));
1652 * We need to save copies of both l and r, because we might get a
1653 * partial merge (which modifies both) and then fails to repack
1655 bch2_bkey_unpack(b, &li.k, l);
1656 bch2_bkey_unpack(b, &ri.k, r);
1658 ret = bch2_bkey_merge(c, &li.k, &ri.k);
1659 if (ret == BCH_MERGE_NOMERGE)
1663 * check if we overlap with deleted extents - would break the sort
1667 struct bkey_packed *n = bkey_next(m);
1669 if (n != btree_bkey_last(b, t) &&
1670 bkey_cmp_left_packed(b, n, &li.k.k.p) <= 0 &&
1673 } else if (ret == BCH_MERGE_MERGE) {
1674 struct bkey_packed *prev = bch2_bkey_prev_all(b, t, m);
1677 bkey_cmp_left_packed_byval(b, prev,
1678 bkey_start_pos(&li.k.k)) > 0)
1682 if (ret == BCH_MERGE_PARTIAL) {
1683 if (!extent_i_save(b, m, mi))
1687 bkey_copy(packed_to_bkey(l), &li.k);
1689 bkey_copy(packed_to_bkey(r), &ri.k);
1691 if (!extent_i_save(b, m, &li.k))
1695 bch2_bset_fix_invalidated_key(b, m);
1696 bch2_btree_node_iter_fix(iter, b, node_iter,
1697 m, m->u64s, m->u64s);
1698 verify_modified_extent(iter, m);
1700 return ret == BCH_MERGE_MERGE;
1703 int bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size)
1705 struct btree_iter iter;
1706 struct bpos end = pos;
1712 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, pos,
1713 BTREE_ITER_SLOTS, k) {
1714 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
1717 if (!bch2_extent_is_fully_allocated(k)) {
1722 bch2_btree_iter_unlock(&iter);
1727 /* KEY_TYPE_reservation: */
1729 const char *bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k)
1731 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
1733 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation))
1734 return "incorrect value size";
1736 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX)
1737 return "invalid nr_replicas";
1742 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
1745 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
1747 pr_buf(out, "generation %u replicas %u",
1748 le32_to_cpu(r.v->generation),
1752 enum merge_result bch2_reservation_merge(struct bch_fs *c,
1753 struct bkey_i *l, struct bkey_i *r)
1755 struct bkey_i_reservation *li = bkey_i_to_reservation(l);
1756 struct bkey_i_reservation *ri = bkey_i_to_reservation(r);
1758 if (li->v.generation != ri->v.generation ||
1759 li->v.nr_replicas != ri->v.nr_replicas)
1760 return BCH_MERGE_NOMERGE;
1762 l->k.needs_whiteout |= r->k.needs_whiteout;
1764 /* Keys with no pointers aren't restricted to one bucket and could
1767 if ((u64) l->k.size + r->k.size > KEY_SIZE_MAX) {
1768 bch2_key_resize(&l->k, KEY_SIZE_MAX);
1769 bch2_cut_front(l->k.p, r);
1770 return BCH_MERGE_PARTIAL;
1773 bch2_key_resize(&l->k, l->k.size + r->k.size);
1775 return BCH_MERGE_MERGE;