1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
5 * Code for managing the extent btree and dynamically updating the writeback
10 #include "bkey_methods.h"
12 #include "btree_update.h"
13 #include "btree_update_interior.h"
18 #include "disk_groups.h"
29 #include <trace/events/bcachefs.h>
31 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
33 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
34 const struct bch_extent_ptr *ptr;
37 bkey_for_each_ptr(p, ptr)
43 unsigned bch2_bkey_nr_dirty_ptrs(struct bkey_s_c k)
48 case KEY_TYPE_btree_ptr:
50 case KEY_TYPE_reflink_v: {
51 struct bkey_ptrs_c p = bch2_bkey_ptrs_c(k);
52 const struct bch_extent_ptr *ptr;
54 bkey_for_each_ptr(p, ptr)
55 nr_ptrs += !ptr->cached;
59 case KEY_TYPE_reservation:
60 nr_ptrs = bkey_s_c_to_reservation(k).v->nr_replicas;
67 static unsigned bch2_extent_ptr_durability(struct bch_fs *c,
68 struct extent_ptr_decoded p)
70 unsigned durability = 0;
76 ca = bch_dev_bkey_exists(c, p.ptr.dev);
78 if (ca->mi.state != BCH_MEMBER_STATE_FAILED)
79 durability = max_t(unsigned, durability, ca->mi.durability);
83 genradix_ptr(&c->stripes[0], p.ec.idx);
88 durability = max_t(unsigned, durability, s->nr_redundant);
94 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
96 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
97 const union bch_extent_entry *entry;
98 struct extent_ptr_decoded p;
99 unsigned durability = 0;
101 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
102 durability += bch2_extent_ptr_durability(c, p);
107 static struct bch_dev_io_failures *dev_io_failures(struct bch_io_failures *f,
110 struct bch_dev_io_failures *i;
112 for (i = f->devs; i < f->devs + f->nr; i++)
119 void bch2_mark_io_failure(struct bch_io_failures *failed,
120 struct extent_ptr_decoded *p)
122 struct bch_dev_io_failures *f = dev_io_failures(failed, p->ptr.dev);
125 BUG_ON(failed->nr >= ARRAY_SIZE(failed->devs));
127 f = &failed->devs[failed->nr++];
132 } else if (p->idx != f->idx) {
142 * returns true if p1 is better than p2:
144 static inline bool ptr_better(struct bch_fs *c,
145 const struct extent_ptr_decoded p1,
146 const struct extent_ptr_decoded p2)
148 if (likely(!p1.idx && !p2.idx)) {
149 struct bch_dev *dev1 = bch_dev_bkey_exists(c, p1.ptr.dev);
150 struct bch_dev *dev2 = bch_dev_bkey_exists(c, p2.ptr.dev);
152 u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
153 u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
155 /* Pick at random, biased in favor of the faster device: */
157 return bch2_rand_range(l1 + l2) > l1;
160 if (force_reconstruct_read(c))
161 return p1.idx > p2.idx;
163 return p1.idx < p2.idx;
167 * This picks a non-stale pointer, preferably from a device other than @avoid.
168 * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to
169 * other devices, it will still pick a pointer from avoid.
171 int bch2_bkey_pick_read_device(struct bch_fs *c, struct bkey_s_c k,
172 struct bch_io_failures *failed,
173 struct extent_ptr_decoded *pick)
175 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
176 const union bch_extent_entry *entry;
177 struct extent_ptr_decoded p;
178 struct bch_dev_io_failures *f;
182 if (k.k->type == KEY_TYPE_error)
185 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
186 ca = bch_dev_bkey_exists(c, p.ptr.dev);
189 * If there are any dirty pointers it's an error if we can't
192 if (!ret && !p.ptr.cached)
195 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
198 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
200 p.idx = f->nr_failed < f->nr_retries
205 !bch2_dev_is_readable(ca))
208 if (force_reconstruct_read(c) &&
212 if (p.idx >= (unsigned) p.has_ec + 1)
215 if (ret > 0 && !ptr_better(c, p, *pick))
225 void bch2_bkey_append_ptr(struct bkey_i *k,
226 struct bch_extent_ptr ptr)
228 EBUG_ON(bch2_bkey_has_device(bkey_i_to_s_c(k), ptr.dev));
231 case KEY_TYPE_btree_ptr:
232 case KEY_TYPE_extent:
233 EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
235 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
237 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
247 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
249 struct bch_extent_ptr *ptr;
251 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
254 const struct bch_extent_ptr *
255 bch2_bkey_has_device(struct bkey_s_c k, unsigned dev)
257 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
258 const struct bch_extent_ptr *ptr;
260 bkey_for_each_ptr(ptrs, ptr)
267 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
269 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
270 const struct bch_extent_ptr *ptr;
272 bkey_for_each_ptr(ptrs, ptr)
273 if (bch2_dev_in_target(c, ptr->dev, target) &&
275 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
281 /* extent specific utility code */
283 const struct bch_extent_ptr *
284 bch2_extent_has_device(struct bkey_s_c_extent e, unsigned dev)
286 const struct bch_extent_ptr *ptr;
288 extent_for_each_ptr(e, ptr)
295 const struct bch_extent_ptr *
296 bch2_extent_has_group(struct bch_fs *c, struct bkey_s_c_extent e, unsigned group)
298 const struct bch_extent_ptr *ptr;
300 extent_for_each_ptr(e, ptr) {
301 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
304 ca->mi.group - 1 == group)
311 unsigned bch2_extent_is_compressed(struct bkey_s_c k)
313 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
314 const union bch_extent_entry *entry;
315 struct extent_ptr_decoded p;
318 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
320 p.crc.compression_type != BCH_COMPRESSION_NONE)
321 ret += p.crc.compressed_size;
326 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
327 struct bch_extent_ptr m, u64 offset)
329 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
330 const union bch_extent_entry *entry;
331 struct extent_ptr_decoded p;
333 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
334 if (p.ptr.dev == m.dev &&
335 p.ptr.gen == m.gen &&
336 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
337 (s64) m.offset - offset)
343 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
344 union bch_extent_entry *entry)
346 union bch_extent_entry *i = ptrs.start;
351 while (extent_entry_next(i) != entry)
352 i = extent_entry_next(i);
356 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
357 struct bch_extent_ptr *ptr)
359 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
360 union bch_extent_entry *dst, *src, *prev;
361 bool drop_crc = true;
363 EBUG_ON(ptr < &ptrs.start->ptr ||
364 ptr >= &ptrs.end->ptr);
365 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
367 src = extent_entry_next(to_entry(ptr));
368 if (src != ptrs.end &&
369 !extent_entry_is_crc(src))
373 while ((prev = extent_entry_prev(ptrs, dst))) {
374 if (extent_entry_is_ptr(prev))
377 if (extent_entry_is_crc(prev)) {
386 memmove_u64s_down(dst, src,
387 (u64 *) ptrs.end - (u64 *) src);
388 k.k->u64s -= (u64 *) src - (u64 *) dst;
393 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
394 struct bch_extent_crc_unpacked n)
396 return !u.compression_type &&
398 u.uncompressed_size > u.live_size &&
399 bch2_csum_type_is_encryption(u.csum_type) ==
400 bch2_csum_type_is_encryption(n.csum_type);
403 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
404 struct bch_extent_crc_unpacked n)
406 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
407 struct bch_extent_crc_unpacked crc;
408 const union bch_extent_entry *i;
413 bkey_for_each_crc(k.k, ptrs, crc, i)
414 if (can_narrow_crc(crc, n))
421 * We're writing another replica for this extent, so while we've got the data in
422 * memory we'll be computing a new checksum for the currently live data.
424 * If there are other replicas we aren't moving, and they are checksummed but
425 * not compressed, we can modify them to point to only the data that is
426 * currently live (so that readers won't have to bounce) while we've got the
429 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
431 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
432 struct bch_extent_crc_unpacked u;
433 struct extent_ptr_decoded p;
434 union bch_extent_entry *i;
437 /* Find a checksum entry that covers only live data: */
439 bkey_for_each_crc(&k->k, ptrs, u, i)
440 if (!u.compression_type &&
442 u.live_size == u.uncompressed_size) {
449 BUG_ON(n.compression_type);
451 BUG_ON(n.live_size != k->k.size);
453 restart_narrow_pointers:
454 ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
456 bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
457 if (can_narrow_crc(p.crc, n)) {
458 bch2_bkey_drop_ptr(bkey_i_to_s(k), &i->ptr);
459 p.ptr.offset += p.crc.offset;
461 bch2_extent_ptr_decoded_append(k, &p);
463 goto restart_narrow_pointers;
469 /* returns true if not equal */
470 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
471 struct bch_extent_crc_unpacked r)
473 return (l.csum_type != r.csum_type ||
474 l.compression_type != r.compression_type ||
475 l.compressed_size != r.compressed_size ||
476 l.uncompressed_size != r.uncompressed_size ||
477 l.offset != r.offset ||
478 l.live_size != r.live_size ||
479 l.nonce != r.nonce ||
480 bch2_crc_cmp(l.csum, r.csum));
483 void bch2_ptr_swab(const struct bkey_format *f, struct bkey_packed *k)
485 union bch_extent_entry *entry;
486 u64 *d = (u64 *) bkeyp_val(f, k);
489 for (i = 0; i < bkeyp_val_u64s(f, k); i++)
492 for (entry = (union bch_extent_entry *) d;
493 entry < (union bch_extent_entry *) (d + bkeyp_val_u64s(f, k));
494 entry = extent_entry_next(entry)) {
495 switch (extent_entry_type(entry)) {
496 case BCH_EXTENT_ENTRY_ptr:
498 case BCH_EXTENT_ENTRY_crc32:
499 entry->crc32.csum = swab32(entry->crc32.csum);
501 case BCH_EXTENT_ENTRY_crc64:
502 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
503 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
505 case BCH_EXTENT_ENTRY_crc128:
506 entry->crc128.csum.hi = (__force __le64)
507 swab64((__force u64) entry->crc128.csum.hi);
508 entry->crc128.csum.lo = (__force __le64)
509 swab64((__force u64) entry->crc128.csum.lo);
511 case BCH_EXTENT_ENTRY_stripe_ptr:
517 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
520 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
521 const union bch_extent_entry *entry;
522 struct bch_extent_crc_unpacked crc;
523 const struct bch_extent_ptr *ptr;
524 const struct bch_extent_stripe_ptr *ec;
528 bkey_extent_entry_for_each(ptrs, entry) {
532 switch (__extent_entry_type(entry)) {
533 case BCH_EXTENT_ENTRY_ptr:
534 ptr = entry_to_ptr(entry);
535 ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
536 ? bch_dev_bkey_exists(c, ptr->dev)
539 pr_buf(out, "ptr: %u:%llu gen %u%s%s", ptr->dev,
540 (u64) ptr->offset, ptr->gen,
541 ptr->cached ? " cached" : "",
542 ca && ptr_stale(ca, ptr)
545 case BCH_EXTENT_ENTRY_crc32:
546 case BCH_EXTENT_ENTRY_crc64:
547 case BCH_EXTENT_ENTRY_crc128:
548 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
550 pr_buf(out, "crc: c_size %u size %u offset %u nonce %u csum %u compress %u",
552 crc.uncompressed_size,
553 crc.offset, crc.nonce,
555 crc.compression_type);
557 case BCH_EXTENT_ENTRY_stripe_ptr:
558 ec = &entry->stripe_ptr;
560 pr_buf(out, "ec: idx %llu block %u",
561 (u64) ec->idx, ec->block);
564 pr_buf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
572 static const char *extent_ptr_invalid(const struct bch_fs *c,
574 const struct bch_extent_ptr *ptr,
575 unsigned size_ondisk,
578 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
579 const struct bch_extent_ptr *ptr2;
582 if (!bch2_dev_exists2(c, ptr->dev))
583 return "pointer to invalid device";
585 ca = bch_dev_bkey_exists(c, ptr->dev);
587 return "pointer to invalid device";
589 bkey_for_each_ptr(ptrs, ptr2)
590 if (ptr != ptr2 && ptr->dev == ptr2->dev)
591 return "multiple pointers to same device";
593 if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
594 return "offset past end of device";
596 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
597 return "offset before first bucket";
599 if (bucket_remainder(ca, ptr->offset) +
600 size_ondisk > ca->mi.bucket_size)
601 return "spans multiple buckets";
606 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k)
608 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
609 const union bch_extent_entry *entry;
610 struct bch_extent_crc_unpacked crc;
611 unsigned size_ondisk = k.k->size;
613 unsigned nonce = UINT_MAX;
615 if (k.k->type == KEY_TYPE_btree_ptr)
616 size_ondisk = c->opts.btree_node_size;
618 bkey_extent_entry_for_each(ptrs, entry) {
619 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
620 return "invalid extent entry type";
622 if (k.k->type == KEY_TYPE_btree_ptr &&
623 !extent_entry_is_ptr(entry))
624 return "has non ptr field";
626 switch (extent_entry_type(entry)) {
627 case BCH_EXTENT_ENTRY_ptr:
628 reason = extent_ptr_invalid(c, k, &entry->ptr,
633 case BCH_EXTENT_ENTRY_crc32:
634 case BCH_EXTENT_ENTRY_crc64:
635 case BCH_EXTENT_ENTRY_crc128:
636 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
638 if (crc.offset + crc.live_size >
639 crc.uncompressed_size)
640 return "checksum offset + key size > uncompressed size";
642 size_ondisk = crc.compressed_size;
644 if (!bch2_checksum_type_valid(c, crc.csum_type))
645 return "invalid checksum type";
647 if (crc.compression_type >= BCH_COMPRESSION_NR)
648 return "invalid compression type";
650 if (bch2_csum_type_is_encryption(crc.csum_type)) {
651 if (nonce == UINT_MAX)
652 nonce = crc.offset + crc.nonce;
653 else if (nonce != crc.offset + crc.nonce)
654 return "incorrect nonce";
657 case BCH_EXTENT_ENTRY_stripe_ptr:
667 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
669 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
670 return "value too big";
672 return bch2_bkey_ptrs_invalid(c, k);
675 void bch2_btree_ptr_debugcheck(struct bch_fs *c, struct bkey_s_c k)
677 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
678 const struct bch_extent_ptr *ptr;
681 struct bucket_mark mark;
684 bch2_fs_bug_on(!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
685 !bch2_bkey_replicas_marked(c, k, false), c,
686 "btree key bad (replicas not marked in superblock):\n%s",
687 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
689 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
692 bkey_for_each_ptr(ptrs, ptr) {
693 ca = bch_dev_bkey_exists(c, ptr->dev);
695 mark = ptr_bucket_mark(ca, ptr);
698 if (gen_after(mark.gen, ptr->gen))
701 err = "inconsistent";
702 if (mark.data_type != BCH_DATA_BTREE ||
703 mark.dirty_sectors < c->opts.btree_node_size)
709 bch2_bkey_val_to_text(&PBUF(buf), c, k);
710 bch2_fs_bug(c, "%s btree pointer %s: bucket %zi gen %i mark %08x",
711 err, buf, PTR_BUCKET_NR(ca, ptr),
712 mark.gen, (unsigned) mark.v.counter);
715 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
718 bch2_bkey_ptrs_to_text(out, c, k);
723 void __bch2_cut_front(struct bpos where, struct bkey_s k)
727 if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
730 EBUG_ON(bkey_cmp(where, k.k->p) > 0);
732 sub = where.offset - bkey_start_offset(k.k);
737 k.k->type = KEY_TYPE_deleted;
740 case KEY_TYPE_deleted:
741 case KEY_TYPE_discard:
743 case KEY_TYPE_cookie:
745 case KEY_TYPE_extent:
746 case KEY_TYPE_reflink_v: {
747 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
748 union bch_extent_entry *entry;
749 bool seen_crc = false;
751 bkey_extent_entry_for_each(ptrs, entry) {
752 switch (extent_entry_type(entry)) {
753 case BCH_EXTENT_ENTRY_ptr:
755 entry->ptr.offset += sub;
757 case BCH_EXTENT_ENTRY_crc32:
758 entry->crc32.offset += sub;
760 case BCH_EXTENT_ENTRY_crc64:
761 entry->crc64.offset += sub;
763 case BCH_EXTENT_ENTRY_crc128:
764 entry->crc128.offset += sub;
766 case BCH_EXTENT_ENTRY_stripe_ptr:
770 if (extent_entry_is_crc(entry))
776 case KEY_TYPE_reflink_p: {
777 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
779 le64_add_cpu(&p.v->idx, sub);
782 case KEY_TYPE_reservation:
789 bool bch2_cut_back(struct bpos where, struct bkey *k)
793 if (bkey_cmp(where, k->p) >= 0)
796 EBUG_ON(bkey_cmp(where, bkey_start_pos(k)) < 0);
798 len = where.offset - bkey_start_offset(k);
804 k->type = KEY_TYPE_deleted;
809 static unsigned bch2_bkey_nr_alloc_ptrs(struct bkey_s_c k)
811 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
812 const union bch_extent_entry *entry;
815 bkey_extent_entry_for_each(ptrs, entry) {
816 switch (__extent_entry_type(entry)) {
817 case BCH_EXTENT_ENTRY_ptr:
818 case BCH_EXTENT_ENTRY_stripe_ptr:
826 static int count_iters_for_insert(struct btree_trans *trans,
837 case KEY_TYPE_extent:
838 case KEY_TYPE_reflink_v:
839 *nr_iters += bch2_bkey_nr_alloc_ptrs(k);
841 if (*nr_iters >= max_iters) {
842 *end = bpos_min(*end, k.k->p);
847 case KEY_TYPE_reflink_p: {
848 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
849 u64 idx = le64_to_cpu(p.v->idx);
850 unsigned sectors = bpos_min(*end, p.k->p).offset -
851 bkey_start_offset(p.k);
852 struct btree_iter *iter;
855 for_each_btree_key(trans, iter,
856 BTREE_ID_REFLINK, POS(0, idx + offset),
857 BTREE_ITER_SLOTS, r_k, ret) {
858 if (bkey_cmp(bkey_start_pos(r_k.k),
859 POS(0, idx + sectors)) >= 0)
862 *nr_iters += 1 + bch2_bkey_nr_alloc_ptrs(r_k);
864 if (*nr_iters >= max_iters) {
865 struct bpos pos = bkey_start_pos(k.k);
866 pos.offset += r_k.k->p.offset - idx;
868 *end = bpos_min(*end, pos);
874 bch2_trans_iter_put(trans, iter);
882 #define EXTENT_ITERS_MAX (BTREE_ITER_MAX / 3)
884 int bch2_extent_atomic_end(struct btree_iter *iter,
885 struct bkey_i *insert,
888 struct btree_trans *trans = iter->trans;
890 struct btree_node_iter node_iter;
891 struct bkey_packed *_k;
892 unsigned nr_iters = 0;
895 ret = bch2_btree_iter_traverse(iter);
900 node_iter = iter->l[0].iter;
902 BUG_ON(bkey_cmp(bkey_start_pos(&insert->k), b->data->min_key) < 0);
904 *end = bpos_min(insert->k.p, b->key.k.p);
906 ret = count_iters_for_insert(trans, bkey_i_to_s_c(insert), 0, end,
907 &nr_iters, EXTENT_ITERS_MAX / 2, false);
911 while ((_k = bch2_btree_node_iter_peek_filter(&node_iter, b,
912 KEY_TYPE_discard))) {
913 struct bkey unpacked;
914 struct bkey_s_c k = bkey_disassemble(b, _k, &unpacked);
917 if (bkey_cmp(bkey_start_pos(k.k), *end) >= 0)
920 if (bkey_cmp(bkey_start_pos(&insert->k),
921 bkey_start_pos(k.k)) > 0)
922 offset = bkey_start_offset(&insert->k) -
923 bkey_start_offset(k.k);
925 ret = count_iters_for_insert(trans, k, offset, end,
926 &nr_iters, EXTENT_ITERS_MAX, true);
930 bch2_btree_node_iter_advance(&node_iter, b);
933 return ret < 0 ? ret : 0;
936 int bch2_extent_trim_atomic(struct bkey_i *k, struct btree_iter *iter)
941 ret = bch2_extent_atomic_end(iter, k, &end);
945 bch2_cut_back(end, &k->k);
949 int bch2_extent_is_atomic(struct bkey_i *k, struct btree_iter *iter)
954 ret = bch2_extent_atomic_end(iter, k, &end);
958 return !bkey_cmp(end, k->k.p);
961 enum btree_insert_ret
962 bch2_extent_can_insert(struct btree_trans *trans,
963 struct btree_insert_entry *insert,
966 struct btree_iter_level *l = &insert->iter->l[0];
967 struct btree_node_iter node_iter = l->iter;
968 enum bch_extent_overlap overlap;
969 struct bkey_packed *_k;
970 struct bkey unpacked;
975 * We avoid creating whiteouts whenever possible when deleting, but
976 * those optimizations mean we may potentially insert two whiteouts
977 * instead of one (when we overlap with the front of one extent and the
980 if (bkey_whiteout(&insert->k->k))
983 _k = bch2_btree_node_iter_peek_filter(&node_iter, l->b,
986 return BTREE_INSERT_OK;
988 k = bkey_disassemble(l->b, _k, &unpacked);
990 overlap = bch2_extent_overlap(&insert->k->k, k.k);
992 /* account for having to split existing extent: */
993 if (overlap == BCH_EXTENT_OVERLAP_MIDDLE)
996 if (overlap == BCH_EXTENT_OVERLAP_MIDDLE &&
997 (sectors = bch2_extent_is_compressed(k))) {
998 int flags = trans->flags & BTREE_INSERT_NOFAIL
999 ? BCH_DISK_RESERVATION_NOFAIL : 0;
1001 switch (bch2_disk_reservation_add(trans->c,
1007 return BTREE_INSERT_ENOSPC;
1013 return BTREE_INSERT_OK;
1016 static void verify_extent_nonoverlapping(struct bch_fs *c,
1018 struct btree_node_iter *_iter,
1019 struct bkey_i *insert)
1021 #ifdef CONFIG_BCACHEFS_DEBUG
1022 struct btree_node_iter iter;
1023 struct bkey_packed *k;
1026 if (!expensive_debug_checks(c))
1030 k = bch2_btree_node_iter_prev_filter(&iter, b, KEY_TYPE_discard);
1032 (uk = bkey_unpack_key(b, k),
1033 bkey_cmp(uk.p, bkey_start_pos(&insert->k)) > 0));
1036 k = bch2_btree_node_iter_peek_filter(&iter, b, KEY_TYPE_discard);
1039 (uk = bkey_unpack_key(b, k),
1040 bkey_cmp(insert->k.p, bkey_start_pos(&uk))) > 0);
1043 (uk = bkey_unpack_key(b, k),
1044 bkey_cmp(insert->k.p, bkey_start_pos(&uk))) > 0) {
1048 bch2_bkey_to_text(&PBUF(buf1), &insert->k);
1049 bch2_bkey_to_text(&PBUF(buf2), &uk);
1051 bch2_dump_btree_node(b);
1052 panic("insert > next :\n"
1062 static void extent_bset_insert(struct bch_fs *c, struct btree_iter *iter,
1063 struct bkey_i *insert)
1065 struct btree_iter_level *l = &iter->l[0];
1066 struct bkey_packed *k =
1067 bch2_btree_node_iter_bset_pos(&l->iter, l->b, bset_tree_last(l->b));
1069 BUG_ON(insert->k.u64s > bch_btree_keys_u64s_remaining(c, l->b));
1071 EBUG_ON(bkey_deleted(&insert->k) || !insert->k.size);
1072 verify_extent_nonoverlapping(c, l->b, &l->iter, insert);
1074 if (debug_check_bkeys(c))
1075 bch2_bkey_debugcheck(c, l->b, bkey_i_to_s_c(insert));
1077 bch2_bset_insert(l->b, &l->iter, k, insert, 0);
1078 bch2_btree_node_iter_fix(iter, l->b, &l->iter, k, 0, k->u64s);
1082 extent_squash(struct bch_fs *c, struct btree_iter *iter,
1083 struct bkey_i *insert,
1084 struct bkey_packed *_k, struct bkey_s k,
1085 enum bch_extent_overlap overlap)
1087 struct btree_iter_level *l = &iter->l[0];
1090 case BCH_EXTENT_OVERLAP_FRONT:
1091 /* insert overlaps with start of k: */
1092 __bch2_cut_front(insert->k.p, k);
1093 EBUG_ON(bkey_deleted(k.k));
1094 extent_save(l->b, _k, k.k);
1095 bch2_btree_iter_fix_key_modified(iter, l->b, _k);
1098 case BCH_EXTENT_OVERLAP_BACK:
1099 /* insert overlaps with end of k: */
1100 bch2_cut_back(bkey_start_pos(&insert->k), k.k);
1101 EBUG_ON(bkey_deleted(k.k));
1102 extent_save(l->b, _k, k.k);
1105 * As the auxiliary tree is indexed by the end of the
1106 * key and we've just changed the end, update the
1109 bch2_bset_fix_invalidated_key(l->b, _k);
1110 bch2_btree_node_iter_fix(iter, l->b, &l->iter,
1111 _k, _k->u64s, _k->u64s);
1114 case BCH_EXTENT_OVERLAP_ALL: {
1115 /* The insert key completely covers k, invalidate k */
1116 if (!bkey_whiteout(k.k))
1117 btree_account_key_drop(l->b, _k);
1120 k.k->type = KEY_TYPE_deleted;
1122 if (_k >= btree_bset_last(l->b)->start) {
1123 unsigned u64s = _k->u64s;
1125 bch2_bset_delete(l->b, _k, _k->u64s);
1126 bch2_btree_node_iter_fix(iter, l->b, &l->iter,
1129 extent_save(l->b, _k, k.k);
1130 bch2_btree_iter_fix_key_modified(iter, l->b, _k);
1135 case BCH_EXTENT_OVERLAP_MIDDLE: {
1136 BKEY_PADDED(k) split;
1138 * The insert key falls 'in the middle' of k
1139 * The insert key splits k in 3:
1140 * - start only in k, preserve
1141 * - middle common section, invalidate in k
1142 * - end only in k, preserve
1144 * We update the old key to preserve the start,
1145 * insert will be the new common section,
1146 * we manually insert the end that we are preserving.
1148 * modify k _before_ doing the insert (which will move
1151 bkey_reassemble(&split.k, k.s_c);
1152 split.k.k.needs_whiteout |= bkey_written(l->b, _k);
1154 bch2_cut_back(bkey_start_pos(&insert->k), &split.k.k);
1155 BUG_ON(bkey_deleted(&split.k.k));
1157 __bch2_cut_front(insert->k.p, k);
1158 BUG_ON(bkey_deleted(k.k));
1159 extent_save(l->b, _k, k.k);
1160 bch2_btree_iter_fix_key_modified(iter, l->b, _k);
1162 extent_bset_insert(c, iter, &split.k);
1169 * bch_extent_insert_fixup - insert a new extent and deal with overlaps
1171 * this may result in not actually doing the insert, or inserting some subset
1172 * of the insert key. For cmpxchg operations this is where that logic lives.
1174 * All subsets of @insert that need to be inserted are inserted using
1175 * bch2_btree_insert_and_journal(). If @b or @res fills up, this function
1176 * returns false, setting @iter->pos for the prefix of @insert that actually got
1179 * BSET INVARIANTS: this function is responsible for maintaining all the
1180 * invariants for bsets of extents in memory. things get really hairy with 0
1185 * bkey_start_pos(bkey_next(k)) >= k
1186 * or bkey_start_offset(bkey_next(k)) >= k->offset
1188 * i.e. strict ordering, no overlapping extents.
1190 * multiple bsets (i.e. full btree node):
1193 * k.size != 0 ∧ j.size != 0 →
1194 * ¬ (k > bkey_start_pos(j) ∧ k < j)
1196 * i.e. no two overlapping keys _of nonzero size_
1198 * We can't realistically maintain this invariant for zero size keys because of
1199 * the key merging done in bch2_btree_insert_key() - for two mergeable keys k, j
1200 * there may be another 0 size key between them in another bset, and it will
1201 * thus overlap with the merged key.
1203 * In addition, the end of iter->pos indicates how much has been processed.
1204 * If the end of iter->pos is not the same as the end of insert, then
1205 * key insertion needs to continue/be retried.
1207 void bch2_insert_fixup_extent(struct btree_trans *trans,
1208 struct btree_insert_entry *insert_entry)
1210 struct bch_fs *c = trans->c;
1211 struct btree_iter *iter = insert_entry->iter;
1212 struct bkey_i *insert = insert_entry->k;
1213 struct btree_iter_level *l = &iter->l[0];
1214 struct btree_node_iter node_iter = l->iter;
1215 bool deleting = bkey_whiteout(&insert->k);
1216 bool update_journal = !deleting;
1217 bool update_btree = !deleting;
1218 struct bkey_i whiteout = *insert;
1219 struct bkey_packed *_k;
1220 struct bkey unpacked;
1223 EBUG_ON(iter->level);
1224 EBUG_ON(!insert->k.size);
1225 EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k)));
1227 while ((_k = bch2_btree_node_iter_peek_filter(&l->iter, l->b,
1228 KEY_TYPE_discard))) {
1229 struct bkey_s k = __bkey_disassemble(l->b, _k, &unpacked);
1230 struct bpos cur_end = bpos_min(insert->k.p, k.k->p);
1231 enum bch_extent_overlap overlap =
1232 bch2_extent_overlap(&insert->k, k.k);
1234 if (bkey_cmp(bkey_start_pos(k.k), insert->k.p) >= 0)
1237 if (!bkey_whiteout(k.k))
1238 update_journal = true;
1240 if (!update_journal) {
1241 bch2_cut_front(cur_end, insert);
1242 bch2_cut_front(cur_end, &whiteout);
1243 bch2_btree_iter_set_pos_same_leaf(iter, cur_end);
1248 * When deleting, if possible just do it by switching the type
1249 * of the key we're deleting, instead of creating and inserting
1254 !bkey_cmp(insert->k.p, k.k->p) &&
1255 !bkey_cmp(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) {
1256 if (!bkey_whiteout(k.k)) {
1257 btree_account_key_drop(l->b, _k);
1258 _k->type = KEY_TYPE_discard;
1259 reserve_whiteout(l->b, _k);
1260 bch2_btree_iter_fix_key_modified(iter,
1266 if (k.k->needs_whiteout || bkey_written(l->b, _k)) {
1267 insert->k.needs_whiteout = true;
1268 update_btree = true;
1272 overlap == BCH_EXTENT_OVERLAP_ALL &&
1273 bkey_whiteout(k.k) &&
1274 k.k->needs_whiteout) {
1275 unreserve_whiteout(l->b, _k);
1276 _k->needs_whiteout = false;
1279 extent_squash(c, iter, insert, _k, k, overlap);
1282 bch2_cut_front(cur_end, insert);
1284 node_iter = l->iter;
1286 if (overlap == BCH_EXTENT_OVERLAP_FRONT ||
1287 overlap == BCH_EXTENT_OVERLAP_MIDDLE)
1291 l->iter = node_iter;
1292 bch2_btree_iter_set_pos_same_leaf(iter, insert->k.p);
1295 bkey_copy(&tmp.k, insert);
1298 tmp.k.k.type = KEY_TYPE_discard;
1300 EBUG_ON(bkey_deleted(&tmp.k.k) || !tmp.k.k.size);
1302 extent_bset_insert(c, iter, &tmp.k);
1305 if (update_journal) {
1306 bkey_copy(&tmp.k, !deleting ? insert : &whiteout);
1309 tmp.k.k.type = KEY_TYPE_discard;
1311 EBUG_ON(bkey_deleted(&tmp.k.k) || !tmp.k.k.size);
1313 bch2_btree_journal_key(trans, iter, &tmp.k);
1316 bch2_cut_front(insert->k.p, insert);
1319 const char *bch2_extent_invalid(const struct bch_fs *c, struct bkey_s_c k)
1321 return bch2_bkey_ptrs_invalid(c, k);
1324 void bch2_extent_debugcheck(struct bch_fs *c, struct bkey_s_c k)
1326 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
1327 const union bch_extent_entry *entry;
1328 struct extent_ptr_decoded p;
1332 * XXX: we should be doing most/all of these checks at startup time,
1333 * where we check bch2_bkey_invalid() in btree_node_read_done()
1335 * But note that we can't check for stale pointers or incorrect gc marks
1336 * until after journal replay is done (it might be an extent that's
1337 * going to get overwritten during replay)
1340 if (percpu_down_read_trylock(&c->mark_lock)) {
1341 bch2_fs_bug_on(!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
1342 !bch2_bkey_replicas_marked_locked(c, e.s_c, false), c,
1343 "extent key bad (replicas not marked in superblock):\n%s",
1344 (bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c), buf));
1345 percpu_up_read(&c->mark_lock);
1348 * If journal replay hasn't finished, we might be seeing keys
1349 * that will be overwritten by the time journal replay is done:
1351 if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
1354 extent_for_each_ptr_decode(e, p, entry) {
1355 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
1356 struct bucket_mark mark = ptr_bucket_mark(ca, &p.ptr);
1357 unsigned stale = gen_after(mark.gen, p.ptr.gen);
1358 unsigned disk_sectors = ptr_disk_sectors(p);
1359 unsigned mark_sectors = p.ptr.cached
1360 ? mark.cached_sectors
1361 : mark.dirty_sectors;
1363 bch2_fs_bug_on(stale && !p.ptr.cached, c,
1364 "stale dirty pointer (ptr gen %u bucket %u",
1365 p.ptr.gen, mark.gen);
1367 bch2_fs_bug_on(stale > 96, c, "key too stale: %i", stale);
1369 bch2_fs_bug_on(!stale &&
1370 (mark.data_type != BCH_DATA_USER ||
1371 mark_sectors < disk_sectors), c,
1372 "extent pointer not marked: %s:\n"
1373 "type %u sectors %u < %u",
1374 (bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c), buf),
1376 mark_sectors, disk_sectors);
1380 void bch2_extent_to_text(struct printbuf *out, struct bch_fs *c,
1383 bch2_bkey_ptrs_to_text(out, c, k);
1386 static unsigned bch2_crc_field_size_max[] = {
1387 [BCH_EXTENT_ENTRY_crc32] = CRC32_SIZE_MAX,
1388 [BCH_EXTENT_ENTRY_crc64] = CRC64_SIZE_MAX,
1389 [BCH_EXTENT_ENTRY_crc128] = CRC128_SIZE_MAX,
1392 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
1393 struct bch_extent_crc_unpacked src)
1395 #define set_common_fields(_dst, _src) \
1396 _dst.csum_type = _src.csum_type, \
1397 _dst.compression_type = _src.compression_type, \
1398 _dst._compressed_size = _src.compressed_size - 1, \
1399 _dst._uncompressed_size = _src.uncompressed_size - 1, \
1400 _dst.offset = _src.offset
1402 switch (extent_entry_type(to_entry(dst))) {
1403 case BCH_EXTENT_ENTRY_crc32:
1404 set_common_fields(dst->crc32, src);
1405 dst->crc32.csum = *((__le32 *) &src.csum.lo);
1407 case BCH_EXTENT_ENTRY_crc64:
1408 set_common_fields(dst->crc64, src);
1409 dst->crc64.nonce = src.nonce;
1410 dst->crc64.csum_lo = src.csum.lo;
1411 dst->crc64.csum_hi = *((__le16 *) &src.csum.hi);
1413 case BCH_EXTENT_ENTRY_crc128:
1414 set_common_fields(dst->crc128, src);
1415 dst->crc128.nonce = src.nonce;
1416 dst->crc128.csum = src.csum;
1421 #undef set_common_fields
1424 void bch2_extent_crc_append(struct bkey_i *k,
1425 struct bch_extent_crc_unpacked new)
1427 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
1428 union bch_extent_crc *crc = (void *) ptrs.end;
1430 if (bch_crc_bytes[new.csum_type] <= 4 &&
1431 new.uncompressed_size - 1 <= CRC32_SIZE_MAX &&
1432 new.nonce <= CRC32_NONCE_MAX)
1433 crc->type = 1 << BCH_EXTENT_ENTRY_crc32;
1434 else if (bch_crc_bytes[new.csum_type] <= 10 &&
1435 new.uncompressed_size - 1 <= CRC64_SIZE_MAX &&
1436 new.nonce <= CRC64_NONCE_MAX)
1437 crc->type = 1 << BCH_EXTENT_ENTRY_crc64;
1438 else if (bch_crc_bytes[new.csum_type] <= 16 &&
1439 new.uncompressed_size - 1 <= CRC128_SIZE_MAX &&
1440 new.nonce <= CRC128_NONCE_MAX)
1441 crc->type = 1 << BCH_EXTENT_ENTRY_crc128;
1445 bch2_extent_crc_pack(crc, new);
1447 k->k.u64s += extent_entry_u64s(ptrs.end);
1449 EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
1452 static inline void __extent_entry_insert(struct bkey_i *k,
1453 union bch_extent_entry *dst,
1454 union bch_extent_entry *new)
1456 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
1458 memmove_u64s_up_small((u64 *) dst + extent_entry_u64s(new),
1459 dst, (u64 *) end - (u64 *) dst);
1460 k->k.u64s += extent_entry_u64s(new);
1461 memcpy(dst, new, extent_entry_bytes(new));
1464 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
1465 struct extent_ptr_decoded *p)
1467 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
1468 struct bch_extent_crc_unpacked crc =
1469 bch2_extent_crc_unpack(&k->k, NULL);
1470 union bch_extent_entry *pos;
1472 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
1477 bkey_for_each_crc(&k->k, ptrs, crc, pos)
1478 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
1479 pos = extent_entry_next(pos);
1483 bch2_extent_crc_append(k, p->crc);
1484 pos = bkey_val_end(bkey_i_to_s(k));
1486 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
1487 __extent_entry_insert(k, pos, to_entry(&p->ptr));
1490 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
1491 __extent_entry_insert(k, pos, to_entry(&p->ec));
1496 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
1498 * Returns true if @k should be dropped entirely
1500 * For existing keys, only called when btree nodes are being rewritten, not when
1501 * they're merely being compacted/resorted in memory.
1503 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
1505 struct bch_extent_ptr *ptr;
1507 bch2_bkey_drop_ptrs(k, ptr,
1509 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
1511 /* will only happen if all pointers were cached: */
1512 if (!bkey_val_u64s(k.k))
1513 k.k->type = KEY_TYPE_discard;
1515 return bkey_whiteout(k.k);
1518 void bch2_bkey_mark_replicas_cached(struct bch_fs *c, struct bkey_s k,
1520 unsigned nr_desired_replicas)
1522 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1523 union bch_extent_entry *entry;
1524 struct extent_ptr_decoded p;
1525 int extra = bch2_bkey_durability(c, k.s_c) - nr_desired_replicas;
1527 if (target && extra > 0)
1528 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1529 int n = bch2_extent_ptr_durability(c, p);
1531 if (n && n <= extra &&
1532 !bch2_dev_in_target(c, p.ptr.dev, target)) {
1533 entry->ptr.cached = true;
1539 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1540 int n = bch2_extent_ptr_durability(c, p);
1542 if (n && n <= extra) {
1543 entry->ptr.cached = true;
1549 enum merge_result bch2_extent_merge(struct bch_fs *c,
1550 struct bkey_s _l, struct bkey_s _r)
1552 struct bkey_s_extent l = bkey_s_to_extent(_l);
1553 struct bkey_s_extent r = bkey_s_to_extent(_r);
1554 union bch_extent_entry *en_l = l.v->start;
1555 union bch_extent_entry *en_r = r.v->start;
1556 struct bch_extent_crc_unpacked crc_l, crc_r;
1558 if (bkey_val_u64s(l.k) != bkey_val_u64s(r.k))
1559 return BCH_MERGE_NOMERGE;
1561 crc_l = bch2_extent_crc_unpack(l.k, NULL);
1563 extent_for_each_entry(l, en_l) {
1564 en_r = vstruct_idx(r.v, (u64 *) en_l - l.v->_data);
1566 if (extent_entry_type(en_l) != extent_entry_type(en_r))
1567 return BCH_MERGE_NOMERGE;
1569 switch (extent_entry_type(en_l)) {
1570 case BCH_EXTENT_ENTRY_ptr: {
1571 const struct bch_extent_ptr *lp = &en_l->ptr;
1572 const struct bch_extent_ptr *rp = &en_r->ptr;
1575 if (lp->offset + crc_l.compressed_size != rp->offset ||
1576 lp->dev != rp->dev ||
1578 return BCH_MERGE_NOMERGE;
1580 /* We don't allow extents to straddle buckets: */
1581 ca = bch_dev_bkey_exists(c, lp->dev);
1583 if (PTR_BUCKET_NR(ca, lp) != PTR_BUCKET_NR(ca, rp))
1584 return BCH_MERGE_NOMERGE;
1588 case BCH_EXTENT_ENTRY_stripe_ptr:
1589 if (en_l->stripe_ptr.block != en_r->stripe_ptr.block ||
1590 en_l->stripe_ptr.idx != en_r->stripe_ptr.idx)
1591 return BCH_MERGE_NOMERGE;
1593 case BCH_EXTENT_ENTRY_crc32:
1594 case BCH_EXTENT_ENTRY_crc64:
1595 case BCH_EXTENT_ENTRY_crc128:
1596 crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
1597 crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
1599 if (crc_l.csum_type != crc_r.csum_type ||
1600 crc_l.compression_type != crc_r.compression_type ||
1601 crc_l.nonce != crc_r.nonce)
1602 return BCH_MERGE_NOMERGE;
1604 if (crc_l.offset + crc_l.live_size != crc_l.compressed_size ||
1606 return BCH_MERGE_NOMERGE;
1608 if (!bch2_checksum_mergeable(crc_l.csum_type))
1609 return BCH_MERGE_NOMERGE;
1611 if (crc_l.compression_type)
1612 return BCH_MERGE_NOMERGE;
1614 if (crc_l.csum_type &&
1615 crc_l.uncompressed_size +
1616 crc_r.uncompressed_size > c->sb.encoded_extent_max)
1617 return BCH_MERGE_NOMERGE;
1619 if (crc_l.uncompressed_size + crc_r.uncompressed_size - 1 >
1620 bch2_crc_field_size_max[extent_entry_type(en_l)])
1621 return BCH_MERGE_NOMERGE;
1625 return BCH_MERGE_NOMERGE;
1629 extent_for_each_entry(l, en_l) {
1630 struct bch_extent_crc_unpacked crc_l, crc_r;
1632 en_r = vstruct_idx(r.v, (u64 *) en_l - l.v->_data);
1634 if (!extent_entry_is_crc(en_l))
1637 crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
1638 crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
1640 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
1643 crc_r.uncompressed_size << 9);
1645 crc_l.uncompressed_size += crc_r.uncompressed_size;
1646 crc_l.compressed_size += crc_r.compressed_size;
1648 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l);
1651 bch2_key_resize(l.k, l.k->size + r.k->size);
1653 return BCH_MERGE_MERGE;
1656 bool bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size,
1657 unsigned nr_replicas)
1659 struct btree_trans trans;
1660 struct btree_iter *iter;
1661 struct bpos end = pos;
1668 bch2_trans_init(&trans, c, 0, 0);
1670 for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, pos,
1671 BTREE_ITER_SLOTS, k, err) {
1672 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
1675 if (nr_replicas > bch2_bkey_nr_ptrs_allocated(k)) {
1680 bch2_trans_exit(&trans);
1685 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
1689 switch (k.k->type) {
1690 case KEY_TYPE_extent: {
1691 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
1692 const union bch_extent_entry *entry;
1693 struct extent_ptr_decoded p;
1695 extent_for_each_ptr_decode(e, p, entry)
1696 ret += !p.ptr.cached &&
1697 p.crc.compression_type == BCH_COMPRESSION_NONE;
1700 case KEY_TYPE_reservation:
1701 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
1708 /* KEY_TYPE_reservation: */
1710 const char *bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k)
1712 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
1714 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation))
1715 return "incorrect value size";
1717 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX)
1718 return "invalid nr_replicas";
1723 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
1726 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
1728 pr_buf(out, "generation %u replicas %u",
1729 le32_to_cpu(r.v->generation),
1733 enum merge_result bch2_reservation_merge(struct bch_fs *c,
1734 struct bkey_s _l, struct bkey_s _r)
1736 struct bkey_s_reservation l = bkey_s_to_reservation(_l);
1737 struct bkey_s_reservation r = bkey_s_to_reservation(_r);
1739 if (l.v->generation != r.v->generation ||
1740 l.v->nr_replicas != r.v->nr_replicas)
1741 return BCH_MERGE_NOMERGE;
1743 if ((u64) l.k->size + r.k->size > KEY_SIZE_MAX) {
1744 bch2_key_resize(l.k, KEY_SIZE_MAX);
1745 __bch2_cut_front(l.k->p, r.s);
1746 return BCH_MERGE_PARTIAL;
1749 bch2_key_resize(l.k, l.k->size + r.k->size);
1751 return BCH_MERGE_MERGE;