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"
30 static void sort_key_next(struct btree_node_iter_large *iter,
32 struct btree_node_iter_set *i)
34 i->k += __btree_node_offset_to_key(b, i->k)->u64s;
37 *i = iter->data[--iter->used];
41 * Returns true if l > r - unless l == r, in which case returns true if l is
44 * Necessary for btree_sort_fixup() - if there are multiple keys that compare
45 * equal in different sets, we have to process them newest to oldest.
47 #define key_sort_cmp(h, l, r) \
50 __btree_node_offset_to_key(b, (l).k), \
51 __btree_node_offset_to_key(b, (r).k)) \
56 static inline bool should_drop_next_key(struct btree_node_iter_large *iter,
59 struct btree_node_iter_set *l = iter->data, *r = iter->data + 1;
60 struct bkey_packed *k = __btree_node_offset_to_key(b, l->k);
69 key_sort_cmp(iter, r[0], r[1]) >= 0)
73 * key_sort_cmp() ensures that when keys compare equal the older key
74 * comes first; so if l->k compares equal to r->k then l->k is older and
77 return !bkey_cmp_packed(b,
78 __btree_node_offset_to_key(b, l->k),
79 __btree_node_offset_to_key(b, r->k));
82 struct btree_nr_keys bch2_key_sort_fix_overlapping(struct bset *dst,
84 struct btree_node_iter_large *iter)
86 struct bkey_packed *out = dst->start;
87 struct btree_nr_keys nr;
89 memset(&nr, 0, sizeof(nr));
91 heap_resort(iter, key_sort_cmp);
93 while (!bch2_btree_node_iter_large_end(iter)) {
94 if (!should_drop_next_key(iter, b)) {
95 struct bkey_packed *k =
96 __btree_node_offset_to_key(b, iter->data->k);
99 btree_keys_account_key_add(&nr, 0, out);
100 out = bkey_next(out);
103 sort_key_next(iter, b, iter->data);
104 heap_sift_down(iter, 0, key_sort_cmp);
107 dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
111 /* Common among btree and extent ptrs */
113 const struct bch_extent_ptr *
114 bch2_extent_has_device(struct bkey_s_c_extent e, unsigned dev)
116 const struct bch_extent_ptr *ptr;
118 extent_for_each_ptr(e, ptr)
125 bool bch2_extent_drop_device(struct bkey_s_extent e, unsigned dev)
127 struct bch_extent_ptr *ptr;
128 bool dropped = false;
130 extent_for_each_ptr_backwards(e, ptr)
131 if (ptr->dev == dev) {
132 __bch2_extent_drop_ptr(e, ptr);
137 bch2_extent_drop_redundant_crcs(e);
141 const struct bch_extent_ptr *
142 bch2_extent_has_group(struct bch_fs *c, struct bkey_s_c_extent e, unsigned group)
144 const struct bch_extent_ptr *ptr;
146 extent_for_each_ptr(e, ptr) {
147 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
150 ca->mi.group - 1 == group)
157 const struct bch_extent_ptr *
158 bch2_extent_has_target(struct bch_fs *c, struct bkey_s_c_extent e, unsigned target)
160 const struct bch_extent_ptr *ptr;
162 extent_for_each_ptr(e, ptr)
163 if (bch2_dev_in_target(c, ptr->dev, target) &&
165 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
171 unsigned bch2_extent_nr_ptrs(struct bkey_s_c_extent e)
173 const struct bch_extent_ptr *ptr;
174 unsigned nr_ptrs = 0;
176 extent_for_each_ptr(e, ptr)
182 unsigned bch2_extent_nr_dirty_ptrs(struct bkey_s_c k)
184 struct bkey_s_c_extent e;
185 const struct bch_extent_ptr *ptr;
186 unsigned nr_ptrs = 0;
190 case BCH_EXTENT_CACHED:
191 e = bkey_s_c_to_extent(k);
193 extent_for_each_ptr(e, ptr)
194 nr_ptrs += !ptr->cached;
197 case BCH_RESERVATION:
198 nr_ptrs = bkey_s_c_to_reservation(k).v->nr_replicas;
205 unsigned bch2_extent_ptr_durability(struct bch_fs *c,
206 const struct bch_extent_ptr *ptr)
213 ca = bch_dev_bkey_exists(c, ptr->dev);
215 if (ca->mi.state == BCH_MEMBER_STATE_FAILED)
218 return ca->mi.durability;
221 unsigned bch2_extent_durability(struct bch_fs *c, struct bkey_s_c_extent e)
223 const struct bch_extent_ptr *ptr;
224 unsigned durability = 0;
226 extent_for_each_ptr(e, ptr)
227 durability += bch2_extent_ptr_durability(c, ptr);
232 unsigned bch2_extent_is_compressed(struct bkey_s_c k)
234 struct bkey_s_c_extent e;
235 const struct bch_extent_ptr *ptr;
236 struct bch_extent_crc_unpacked crc;
241 case BCH_EXTENT_CACHED:
242 e = bkey_s_c_to_extent(k);
244 extent_for_each_ptr_crc(e, ptr, crc)
246 crc.compression_type != BCH_COMPRESSION_NONE &&
247 crc.compressed_size < crc.live_size)
248 ret = max_t(unsigned, ret, crc.compressed_size);
254 bool bch2_extent_matches_ptr(struct bch_fs *c, struct bkey_s_c_extent e,
255 struct bch_extent_ptr m, u64 offset)
257 const struct bch_extent_ptr *ptr;
258 struct bch_extent_crc_unpacked crc;
260 extent_for_each_ptr_crc(e, ptr, crc)
261 if (ptr->dev == m.dev &&
263 (s64) ptr->offset + crc.offset - bkey_start_offset(e.k) ==
264 (s64) m.offset - offset)
270 /* Doesn't cleanup redundant crcs */
271 void __bch2_extent_drop_ptr(struct bkey_s_extent e, struct bch_extent_ptr *ptr)
273 EBUG_ON(ptr < &e.v->start->ptr ||
274 ptr >= &extent_entry_last(e)->ptr);
275 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
276 memmove_u64s_down(ptr, ptr + 1,
277 (u64 *) extent_entry_last(e) - (u64 *) (ptr + 1));
278 e.k->u64s -= sizeof(*ptr) / sizeof(u64);
281 void bch2_extent_drop_ptr(struct bkey_s_extent e, struct bch_extent_ptr *ptr)
283 __bch2_extent_drop_ptr(e, ptr);
284 bch2_extent_drop_redundant_crcs(e);
287 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
288 struct bch_extent_crc_unpacked n)
290 return !u.compression_type &&
292 u.uncompressed_size > u.live_size &&
293 bch2_csum_type_is_encryption(u.csum_type) ==
294 bch2_csum_type_is_encryption(n.csum_type);
297 bool bch2_can_narrow_extent_crcs(struct bkey_s_c_extent e,
298 struct bch_extent_crc_unpacked n)
300 struct bch_extent_crc_unpacked crc;
301 const union bch_extent_entry *i;
306 extent_for_each_crc(e, crc, i)
307 if (can_narrow_crc(crc, n))
314 * We're writing another replica for this extent, so while we've got the data in
315 * memory we'll be computing a new checksum for the currently live data.
317 * If there are other replicas we aren't moving, and they are checksummed but
318 * not compressed, we can modify them to point to only the data that is
319 * currently live (so that readers won't have to bounce) while we've got the
322 bool bch2_extent_narrow_crcs(struct bkey_i_extent *e,
323 struct bch_extent_crc_unpacked n)
325 struct bch_extent_crc_unpacked u;
326 struct bch_extent_ptr *ptr;
327 union bch_extent_entry *i;
329 /* Find a checksum entry that covers only live data: */
331 extent_for_each_crc(extent_i_to_s(e), u, i)
332 if (!u.compression_type &&
334 u.live_size == u.uncompressed_size) {
339 if (!bch2_can_narrow_extent_crcs(extent_i_to_s_c(e), n))
342 BUG_ON(n.compression_type);
344 BUG_ON(n.live_size != e->k.size);
346 bch2_extent_crc_append(e, n);
347 restart_narrow_pointers:
348 extent_for_each_ptr_crc(extent_i_to_s(e), ptr, u)
349 if (can_narrow_crc(u, n)) {
350 ptr->offset += u.offset;
351 extent_ptr_append(e, *ptr);
352 __bch2_extent_drop_ptr(extent_i_to_s(e), ptr);
353 goto restart_narrow_pointers;
356 bch2_extent_drop_redundant_crcs(extent_i_to_s(e));
360 /* returns true if not equal */
361 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
362 struct bch_extent_crc_unpacked r)
364 return (l.csum_type != r.csum_type ||
365 l.compression_type != r.compression_type ||
366 l.compressed_size != r.compressed_size ||
367 l.uncompressed_size != r.uncompressed_size ||
368 l.offset != r.offset ||
369 l.live_size != r.live_size ||
370 l.nonce != r.nonce ||
371 bch2_crc_cmp(l.csum, r.csum));
374 void bch2_extent_drop_redundant_crcs(struct bkey_s_extent e)
376 union bch_extent_entry *entry = e.v->start;
377 union bch_extent_crc *crc, *prev = NULL;
378 struct bch_extent_crc_unpacked u, prev_u = { 0 };
380 while (entry != extent_entry_last(e)) {
381 union bch_extent_entry *next = extent_entry_next(entry);
382 size_t crc_u64s = extent_entry_u64s(entry);
384 if (!extent_entry_is_crc(entry))
387 crc = entry_to_crc(entry);
388 u = bch2_extent_crc_unpack(e.k, crc);
390 if (next == extent_entry_last(e)) {
391 /* crc entry with no pointers after it: */
395 if (extent_entry_is_crc(next)) {
396 /* no pointers before next crc entry: */
400 if (prev && !bch2_crc_unpacked_cmp(u, prev_u)) {
401 /* identical to previous crc entry: */
407 !u.compression_type) {
408 /* null crc entry: */
409 union bch_extent_entry *e2;
411 extent_for_each_entry_from(e, e2, extent_entry_next(entry)) {
412 if (!extent_entry_is_ptr(e2))
415 e2->ptr.offset += u.offset;
426 memmove_u64s_down(crc, next,
427 (u64 *) extent_entry_last(e) - (u64 *) next);
428 e.k->u64s -= crc_u64s;
431 EBUG_ON(bkey_val_u64s(e.k) && !bch2_extent_nr_ptrs(e.c));
434 static bool should_drop_ptr(const struct bch_fs *c,
435 struct bkey_s_c_extent e,
436 const struct bch_extent_ptr *ptr)
438 return ptr->cached && ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr);
441 static void bch2_extent_drop_stale(struct bch_fs *c, struct bkey_s_extent e)
443 struct bch_extent_ptr *ptr = &e.v->start->ptr;
444 bool dropped = false;
446 while ((ptr = extent_ptr_next(e, ptr)))
447 if (should_drop_ptr(c, e.c, ptr)) {
448 __bch2_extent_drop_ptr(e, ptr);
454 bch2_extent_drop_redundant_crcs(e);
457 bool bch2_ptr_normalize(struct bch_fs *c, struct btree *b, struct bkey_s k)
459 return bch2_extent_normalize(c, k);
462 void bch2_ptr_swab(const struct bkey_format *f, struct bkey_packed *k)
466 case BCH_EXTENT_CACHED: {
467 union bch_extent_entry *entry;
468 u64 *d = (u64 *) bkeyp_val(f, k);
471 for (i = 0; i < bkeyp_val_u64s(f, k); i++)
474 for (entry = (union bch_extent_entry *) d;
475 entry < (union bch_extent_entry *) (d + bkeyp_val_u64s(f, k));
476 entry = extent_entry_next(entry)) {
477 switch (extent_entry_type(entry)) {
478 case BCH_EXTENT_ENTRY_crc32:
479 entry->crc32.csum = swab32(entry->crc32.csum);
481 case BCH_EXTENT_ENTRY_crc64:
482 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
483 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
485 case BCH_EXTENT_ENTRY_crc128:
486 entry->crc128.csum.hi = (__force __le64)
487 swab64((__force u64) entry->crc128.csum.hi);
488 entry->crc128.csum.lo = (__force __le64)
489 swab64((__force u64) entry->crc128.csum.lo);
491 case BCH_EXTENT_ENTRY_ptr:
500 static const char *extent_ptr_invalid(const struct bch_fs *c,
501 struct bkey_s_c_extent e,
502 const struct bch_extent_ptr *ptr,
503 unsigned size_ondisk,
506 const struct bch_extent_ptr *ptr2;
509 if (ptr->dev >= c->sb.nr_devices ||
511 return "pointer to invalid device";
513 ca = bch_dev_bkey_exists(c, ptr->dev);
515 return "pointer to invalid device";
517 extent_for_each_ptr(e, ptr2)
518 if (ptr != ptr2 && ptr->dev == ptr2->dev)
519 return "multiple pointers to same device";
521 if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
522 return "offset past end of device";
524 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
525 return "offset before first bucket";
527 if (bucket_remainder(ca, ptr->offset) +
528 size_ondisk > ca->mi.bucket_size)
529 return "spans multiple buckets";
534 static size_t extent_print_ptrs(struct bch_fs *c, char *buf,
535 size_t size, struct bkey_s_c_extent e)
537 char *out = buf, *end = buf + size;
538 const union bch_extent_entry *entry;
539 struct bch_extent_crc_unpacked crc;
540 const struct bch_extent_ptr *ptr;
544 #define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
546 extent_for_each_entry(e, entry) {
550 switch (__extent_entry_type(entry)) {
551 case BCH_EXTENT_ENTRY_crc32:
552 case BCH_EXTENT_ENTRY_crc64:
553 case BCH_EXTENT_ENTRY_crc128:
554 crc = bch2_extent_crc_unpack(e.k, entry_to_crc(entry));
556 p("crc: c_size %u size %u offset %u nonce %u csum %u compress %u",
558 crc.uncompressed_size,
559 crc.offset, crc.nonce,
561 crc.compression_type);
563 case BCH_EXTENT_ENTRY_ptr:
564 ptr = entry_to_ptr(entry);
565 ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
566 ? bch_dev_bkey_exists(c, ptr->dev)
569 p("ptr: %u:%llu gen %u%s%s", ptr->dev,
570 (u64) ptr->offset, ptr->gen,
571 ptr->cached ? " cached" : "",
572 ca && ptr_stale(ca, ptr)
576 p("(invalid extent entry %.16llx)", *((u64 *) entry));
583 if (bkey_extent_is_cached(e.k))
589 static inline bool dev_latency_better(struct bch_fs *c,
590 const struct bch_extent_ptr *ptr1,
591 const struct bch_extent_ptr *ptr2)
593 struct bch_dev *dev1 = bch_dev_bkey_exists(c, ptr1->dev);
594 struct bch_dev *dev2 = bch_dev_bkey_exists(c, ptr2->dev);
595 u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
596 u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
598 /* Pick at random, biased in favor of the faster device: */
600 return bch2_rand_range(l1 + l2) > l1;
603 static int extent_pick_read_device(struct bch_fs *c,
604 struct bkey_s_c_extent e,
605 struct bch_devs_mask *avoid,
606 struct extent_ptr_decoded *pick)
608 const struct bch_extent_ptr *ptr;
609 struct bch_extent_crc_unpacked crc;
613 extent_for_each_ptr_crc(e, ptr, crc) {
614 ca = bch_dev_bkey_exists(c, ptr->dev);
616 if (ptr->cached && ptr_stale(ca, ptr))
619 if (avoid && test_bit(ptr->dev, avoid->d))
622 if (ret && !dev_latency_better(c, ptr, &pick->ptr))
625 *pick = (struct extent_ptr_decoded) {
638 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
640 if (bkey_extent_is_cached(k.k))
644 return "nonzero key size";
646 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
647 return "value too big";
651 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
652 const union bch_extent_entry *entry;
653 const struct bch_extent_ptr *ptr;
656 extent_for_each_entry(e, entry) {
657 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
658 return "invalid extent entry type";
660 if (extent_entry_is_crc(entry))
661 return "has crc field";
664 extent_for_each_ptr(e, ptr) {
665 reason = extent_ptr_invalid(c, e, ptr,
666 c->opts.btree_node_size,
676 return "invalid value type";
680 void bch2_btree_ptr_debugcheck(struct bch_fs *c, struct btree *b,
683 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
684 const struct bch_extent_ptr *ptr;
688 struct bucket_mark mark;
690 unsigned replicas = 0;
693 extent_for_each_ptr(e, ptr) {
694 ca = bch_dev_bkey_exists(c, ptr->dev);
697 if (!test_bit(BCH_FS_ALLOC_READ_DONE, &c->flags))
701 if (ptr_stale(ca, ptr))
705 seq = read_seqcount_begin(&c->gc_pos_lock);
706 mark = ptr_bucket_mark(ca, ptr);
708 bad = gc_pos_cmp(c->gc_pos, gc_pos_btree_node(b)) > 0 &&
709 (mark.data_type != BCH_DATA_BTREE ||
710 mark.dirty_sectors < c->opts.btree_node_size);
711 } while (read_seqcount_retry(&c->gc_pos_lock, seq));
713 err = "inconsistent";
718 if (!bch2_bkey_replicas_marked(c, BCH_DATA_BTREE, e.s_c)) {
719 bch2_bkey_val_to_text(c, btree_node_type(b),
720 buf, sizeof(buf), k);
722 "btree key bad (replicas not marked in superblock):\n%s",
729 bch2_bkey_val_to_text(c, btree_node_type(b), buf, sizeof(buf), k);
730 bch2_fs_bug(c, "%s btree pointer %s: bucket %zi "
732 err, buf, PTR_BUCKET_NR(ca, ptr),
733 mark.gen, (unsigned) mark.v.counter);
736 int bch2_btree_ptr_to_text(struct bch_fs *c, char *buf,
737 size_t size, struct bkey_s_c k)
739 char *out = buf, *end = buf + size;
742 #define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
744 if (bkey_extent_is_data(k.k))
745 out += extent_print_ptrs(c, buf, size, bkey_s_c_to_extent(k));
747 invalid = bch2_btree_ptr_invalid(c, k);
749 p(" invalid: %s", invalid);
754 int bch2_btree_pick_ptr(struct bch_fs *c, const struct btree *b,
755 struct bch_devs_mask *avoid,
756 struct extent_ptr_decoded *pick)
758 return extent_pick_read_device(c, bkey_i_to_s_c_extent(&b->key),
764 static bool __bch2_cut_front(struct bpos where, struct bkey_s k)
768 if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
771 EBUG_ON(bkey_cmp(where, k.k->p) > 0);
773 len = k.k->p.offset - where.offset;
775 BUG_ON(len > k.k->size);
778 * Don't readjust offset if the key size is now 0, because that could
779 * cause offset to point to the next bucket:
782 k.k->type = KEY_TYPE_DELETED;
783 else if (bkey_extent_is_data(k.k)) {
784 struct bkey_s_extent e = bkey_s_to_extent(k);
785 union bch_extent_entry *entry;
786 bool seen_crc = false;
788 extent_for_each_entry(e, entry) {
789 switch (extent_entry_type(entry)) {
790 case BCH_EXTENT_ENTRY_ptr:
792 entry->ptr.offset += e.k->size - len;
794 case BCH_EXTENT_ENTRY_crc32:
795 entry->crc32.offset += e.k->size - len;
797 case BCH_EXTENT_ENTRY_crc64:
798 entry->crc64.offset += e.k->size - len;
800 case BCH_EXTENT_ENTRY_crc128:
801 entry->crc128.offset += e.k->size - len;
805 if (extent_entry_is_crc(entry))
815 bool bch2_cut_front(struct bpos where, struct bkey_i *k)
817 return __bch2_cut_front(where, bkey_i_to_s(k));
820 bool bch2_cut_back(struct bpos where, struct bkey *k)
824 if (bkey_cmp(where, k->p) >= 0)
827 EBUG_ON(bkey_cmp(where, bkey_start_pos(k)) < 0);
829 len = where.offset - bkey_start_offset(k);
831 BUG_ON(len > k->size);
837 k->type = KEY_TYPE_DELETED;
843 * bch_key_resize - adjust size of @k
845 * bkey_start_offset(k) will be preserved, modifies where the extent ends
847 void bch2_key_resize(struct bkey *k,
850 k->p.offset -= k->size;
851 k->p.offset += new_size;
856 * In extent_sort_fix_overlapping(), insert_fixup_extent(),
857 * extent_merge_inline() - we're modifying keys in place that are packed. To do
858 * that we have to unpack the key, modify the unpacked key - then this
859 * copies/repacks the unpacked to the original as necessary.
861 static void extent_save(struct btree *b, struct bkey_packed *dst,
864 struct bkey_format *f = &b->format;
865 struct bkey_i *dst_unpacked;
867 if ((dst_unpacked = packed_to_bkey(dst)))
868 dst_unpacked->k = *src;
870 BUG_ON(!bch2_bkey_pack_key(dst, src, f));
873 static bool extent_i_save(struct btree *b, struct bkey_packed *dst,
876 struct bkey_format *f = &b->format;
877 struct bkey_i *dst_unpacked;
878 struct bkey_packed tmp;
880 if ((dst_unpacked = packed_to_bkey(dst)))
881 dst_unpacked->k = src->k;
882 else if (bch2_bkey_pack_key(&tmp, &src->k, f))
883 memcpy_u64s(dst, &tmp, f->key_u64s);
887 memcpy_u64s(bkeyp_val(f, dst), &src->v, bkey_val_u64s(&src->k));
892 * If keys compare equal, compare by pointer order:
894 * Necessary for sort_fix_overlapping() - if there are multiple keys that
895 * compare equal in different sets, we have to process them newest to oldest.
897 #define extent_sort_cmp(h, l, r) \
899 struct bkey _ul = bkey_unpack_key(b, \
900 __btree_node_offset_to_key(b, (l).k)); \
901 struct bkey _ur = bkey_unpack_key(b, \
902 __btree_node_offset_to_key(b, (r).k)); \
904 bkey_cmp(bkey_start_pos(&_ul), \
905 bkey_start_pos(&_ur)) ?: (r).k - (l).k; \
908 static inline void extent_sort_sift(struct btree_node_iter_large *iter,
909 struct btree *b, size_t i)
911 heap_sift_down(iter, i, extent_sort_cmp);
914 static inline void extent_sort_next(struct btree_node_iter_large *iter,
916 struct btree_node_iter_set *i)
918 sort_key_next(iter, b, i);
919 heap_sift_down(iter, i - iter->data, extent_sort_cmp);
922 static void extent_sort_append(struct bch_fs *c,
924 struct btree_nr_keys *nr,
925 struct bkey_packed *start,
926 struct bkey_packed **prev,
927 struct bkey_packed *k)
929 struct bkey_format *f = &b->format;
932 if (bkey_whiteout(k))
935 bch2_bkey_unpack(b, &tmp.k, k);
938 bch2_extent_merge(c, b, (void *) *prev, &tmp.k))
942 bch2_bkey_pack(*prev, (void *) *prev, f);
944 btree_keys_account_key_add(nr, 0, *prev);
945 *prev = bkey_next(*prev);
950 bkey_copy(*prev, &tmp.k);
953 struct btree_nr_keys bch2_extent_sort_fix_overlapping(struct bch_fs *c,
956 struct btree_node_iter_large *iter)
958 struct bkey_format *f = &b->format;
959 struct btree_node_iter_set *_l = iter->data, *_r;
960 struct bkey_packed *prev = NULL, *out, *lk, *rk;
961 struct bkey l_unpacked, r_unpacked;
963 struct btree_nr_keys nr;
965 memset(&nr, 0, sizeof(nr));
967 heap_resort(iter, extent_sort_cmp);
969 while (!bch2_btree_node_iter_large_end(iter)) {
970 lk = __btree_node_offset_to_key(b, _l->k);
972 if (iter->used == 1) {
973 extent_sort_append(c, b, &nr, dst->start, &prev, lk);
974 extent_sort_next(iter, b, _l);
979 if (iter->used > 2 &&
980 extent_sort_cmp(iter, _r[0], _r[1]) >= 0)
983 rk = __btree_node_offset_to_key(b, _r->k);
985 l = __bkey_disassemble(b, lk, &l_unpacked);
986 r = __bkey_disassemble(b, rk, &r_unpacked);
988 /* If current key and next key don't overlap, just append */
989 if (bkey_cmp(l.k->p, bkey_start_pos(r.k)) <= 0) {
990 extent_sort_append(c, b, &nr, dst->start, &prev, lk);
991 extent_sort_next(iter, b, _l);
995 /* Skip 0 size keys */
997 extent_sort_next(iter, b, _r);
1002 * overlap: keep the newer key and trim the older key so they
1003 * don't overlap. comparing pointers tells us which one is
1004 * newer, since the bsets are appended one after the other.
1007 /* can't happen because of comparison func */
1008 BUG_ON(_l->k < _r->k &&
1009 !bkey_cmp(bkey_start_pos(l.k), bkey_start_pos(r.k)));
1011 if (_l->k > _r->k) {
1012 /* l wins, trim r */
1013 if (bkey_cmp(l.k->p, r.k->p) >= 0) {
1014 sort_key_next(iter, b, _r);
1016 __bch2_cut_front(l.k->p, r);
1017 extent_save(b, rk, r.k);
1020 extent_sort_sift(iter, b, _r - iter->data);
1021 } else if (bkey_cmp(l.k->p, r.k->p) > 0) {
1025 * r wins, but it overlaps in the middle of l - split l:
1027 bkey_reassemble(&tmp.k, l.s_c);
1028 bch2_cut_back(bkey_start_pos(r.k), &tmp.k.k);
1030 __bch2_cut_front(r.k->p, l);
1031 extent_save(b, lk, l.k);
1033 extent_sort_sift(iter, b, 0);
1035 extent_sort_append(c, b, &nr, dst->start, &prev,
1036 bkey_to_packed(&tmp.k));
1038 bch2_cut_back(bkey_start_pos(r.k), l.k);
1039 extent_save(b, lk, l.k);
1044 bch2_bkey_pack(prev, (void *) prev, f);
1045 btree_keys_account_key_add(&nr, 0, prev);
1046 out = bkey_next(prev);
1051 dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
1055 struct extent_insert_state {
1056 struct btree_insert *trans;
1057 struct btree_insert_entry *insert;
1058 struct bpos committed;
1059 struct bch_fs_usage stats;
1062 struct bkey_i whiteout;
1063 bool update_journal;
1068 static void bch2_add_sectors(struct extent_insert_state *s,
1069 struct bkey_s_c k, u64 offset, s64 sectors)
1071 struct bch_fs *c = s->trans->c;
1072 struct btree *b = s->insert->iter->l[0].b;
1074 EBUG_ON(bkey_cmp(bkey_start_pos(k.k), b->data->min_key) < 0);
1079 bch2_mark_key(c, k, sectors, BCH_DATA_USER, gc_pos_btree_node(b),
1080 &s->stats, s->trans->journal_res.seq, 0);
1083 static void bch2_subtract_sectors(struct extent_insert_state *s,
1084 struct bkey_s_c k, u64 offset, s64 sectors)
1086 bch2_add_sectors(s, k, offset, -sectors);
1089 /* These wrappers subtract exactly the sectors that we're removing from @k */
1090 static void bch2_cut_subtract_back(struct extent_insert_state *s,
1091 struct bpos where, struct bkey_s k)
1093 bch2_subtract_sectors(s, k.s_c, where.offset,
1094 k.k->p.offset - where.offset);
1095 bch2_cut_back(where, k.k);
1098 static void bch2_cut_subtract_front(struct extent_insert_state *s,
1099 struct bpos where, struct bkey_s k)
1101 bch2_subtract_sectors(s, k.s_c, bkey_start_offset(k.k),
1102 where.offset - bkey_start_offset(k.k));
1103 __bch2_cut_front(where, k);
1106 static void bch2_drop_subtract(struct extent_insert_state *s, struct bkey_s k)
1109 bch2_subtract_sectors(s, k.s_c,
1110 bkey_start_offset(k.k), k.k->size);
1112 k.k->type = KEY_TYPE_DELETED;
1115 static bool bch2_extent_merge_inline(struct bch_fs *,
1116 struct btree_iter *,
1117 struct bkey_packed *,
1118 struct bkey_packed *,
1121 static void verify_extent_nonoverlapping(struct btree *b,
1122 struct btree_node_iter *_iter,
1123 struct bkey_i *insert)
1125 #ifdef CONFIG_BCACHEFS_DEBUG
1126 struct btree_node_iter iter;
1127 struct bkey_packed *k;
1131 k = bch2_btree_node_iter_prev_filter(&iter, b, KEY_TYPE_DISCARD);
1133 (uk = bkey_unpack_key(b, k),
1134 bkey_cmp(uk.p, bkey_start_pos(&insert->k)) > 0));
1137 k = bch2_btree_node_iter_peek_filter(&iter, b, KEY_TYPE_DISCARD);
1140 (uk = bkey_unpack_key(b, k),
1141 bkey_cmp(insert->k.p, bkey_start_pos(&uk))) > 0);
1144 (uk = bkey_unpack_key(b, k),
1145 bkey_cmp(insert->k.p, bkey_start_pos(&uk))) > 0) {
1149 bch2_bkey_to_text(buf1, sizeof(buf1), &insert->k);
1150 bch2_bkey_to_text(buf2, sizeof(buf2), &uk);
1152 bch2_dump_btree_node(b);
1153 panic("insert > next :\n"
1163 static void verify_modified_extent(struct btree_iter *iter,
1164 struct bkey_packed *k)
1166 bch2_btree_iter_verify(iter, iter->l[0].b);
1167 bch2_verify_insert_pos(iter->l[0].b, k, k, k->u64s);
1170 static void extent_bset_insert(struct bch_fs *c, struct btree_iter *iter,
1171 struct bkey_i *insert)
1173 struct btree_iter_level *l = &iter->l[0];
1174 struct btree_node_iter node_iter;
1175 struct bkey_packed *k;
1177 BUG_ON(insert->k.u64s > bch_btree_keys_u64s_remaining(c, l->b));
1179 EBUG_ON(bkey_deleted(&insert->k) || !insert->k.size);
1180 verify_extent_nonoverlapping(l->b, &l->iter, insert);
1182 node_iter = l->iter;
1183 k = bch2_btree_node_iter_prev_filter(&node_iter, l->b, KEY_TYPE_DISCARD);
1184 if (k && !bkey_written(l->b, k) &&
1185 bch2_extent_merge_inline(c, iter, k, bkey_to_packed(insert), true))
1188 node_iter = l->iter;
1189 k = bch2_btree_node_iter_peek_filter(&node_iter, l->b, KEY_TYPE_DISCARD);
1190 if (k && !bkey_written(l->b, k) &&
1191 bch2_extent_merge_inline(c, iter, bkey_to_packed(insert), k, false))
1194 k = bch2_btree_node_iter_bset_pos(&l->iter, l->b, bset_tree_last(l->b));
1196 bch2_bset_insert(l->b, &l->iter, k, insert, 0);
1197 bch2_btree_node_iter_fix(iter, l->b, &l->iter, k, 0, k->u64s);
1198 bch2_btree_iter_verify(iter, l->b);
1201 static void extent_insert_committed(struct extent_insert_state *s)
1203 struct bch_fs *c = s->trans->c;
1204 struct btree_iter *iter = s->insert->iter;
1205 struct bkey_i *insert = s->insert->k;
1206 BKEY_PADDED(k) split;
1208 EBUG_ON(bkey_cmp(insert->k.p, s->committed) < 0);
1209 EBUG_ON(bkey_cmp(s->committed, bkey_start_pos(&insert->k)) < 0);
1211 bkey_copy(&split.k, insert);
1213 split.k.k.type = KEY_TYPE_DISCARD;
1215 if (!(s->trans->flags & BTREE_INSERT_JOURNAL_REPLAY))
1216 bch2_cut_subtract_back(s, s->committed,
1217 bkey_i_to_s(&split.k));
1219 bch2_cut_back(s->committed, &split.k.k);
1221 if (!bkey_cmp(s->committed, iter->pos))
1224 bch2_btree_iter_set_pos_same_leaf(iter, s->committed);
1226 if (s->update_btree) {
1227 if (debug_check_bkeys(c))
1228 bch2_bkey_debugcheck(c, iter->l[0].b,
1229 bkey_i_to_s_c(&split.k));
1231 EBUG_ON(bkey_deleted(&split.k.k) || !split.k.k.size);
1233 extent_bset_insert(c, iter, &split.k);
1236 if (s->update_journal) {
1237 bkey_copy(&split.k, !s->deleting ? insert : &s->whiteout);
1239 split.k.k.type = KEY_TYPE_DISCARD;
1241 bch2_cut_back(s->committed, &split.k.k);
1243 EBUG_ON(bkey_deleted(&split.k.k) || !split.k.k.size);
1245 bch2_btree_journal_key(s->trans, iter, &split.k);
1248 bch2_cut_front(s->committed, insert);
1250 insert->k.needs_whiteout = false;
1251 s->trans->did_work = true;
1254 void bch2_extent_trim_atomic(struct bkey_i *k, struct btree_iter *iter)
1256 struct btree *b = iter->l[0].b;
1258 BUG_ON(iter->uptodate > BTREE_ITER_NEED_PEEK);
1260 bch2_cut_back(b->key.k.p, &k->k);
1262 BUG_ON(bkey_cmp(bkey_start_pos(&k->k), b->data->min_key) < 0);
1265 enum btree_insert_ret
1266 bch2_extent_can_insert(struct btree_insert *trans,
1267 struct btree_insert_entry *insert,
1270 struct btree_iter_level *l = &insert->iter->l[0];
1271 struct btree_node_iter node_iter = l->iter;
1272 enum bch_extent_overlap overlap;
1273 struct bkey_packed *_k;
1274 struct bkey unpacked;
1278 BUG_ON(trans->flags & BTREE_INSERT_ATOMIC &&
1279 !bch2_extent_is_atomic(&insert->k->k, insert->iter));
1282 * We avoid creating whiteouts whenever possible when deleting, but
1283 * those optimizations mean we may potentially insert two whiteouts
1284 * instead of one (when we overlap with the front of one extent and the
1287 if (bkey_whiteout(&insert->k->k))
1290 _k = bch2_btree_node_iter_peek_filter(&node_iter, l->b,
1293 return BTREE_INSERT_OK;
1295 k = bkey_disassemble(l->b, _k, &unpacked);
1297 overlap = bch2_extent_overlap(&insert->k->k, k.k);
1299 /* account for having to split existing extent: */
1300 if (overlap == BCH_EXTENT_OVERLAP_MIDDLE)
1303 if (overlap == BCH_EXTENT_OVERLAP_MIDDLE &&
1304 (sectors = bch2_extent_is_compressed(k))) {
1305 int flags = BCH_DISK_RESERVATION_BTREE_LOCKS_HELD;
1307 if (trans->flags & BTREE_INSERT_NOFAIL)
1308 flags |= BCH_DISK_RESERVATION_NOFAIL;
1310 switch (bch2_disk_reservation_add(trans->c,
1312 sectors * bch2_extent_nr_dirty_ptrs(k),
1317 return BTREE_INSERT_ENOSPC;
1319 return BTREE_INSERT_NEED_GC_LOCK;
1325 return BTREE_INSERT_OK;
1329 extent_squash(struct extent_insert_state *s, struct bkey_i *insert,
1330 struct bkey_packed *_k, struct bkey_s k,
1331 enum bch_extent_overlap overlap)
1333 struct bch_fs *c = s->trans->c;
1334 struct btree_iter *iter = s->insert->iter;
1335 struct btree_iter_level *l = &iter->l[0];
1338 case BCH_EXTENT_OVERLAP_FRONT:
1339 /* insert overlaps with start of k: */
1340 bch2_cut_subtract_front(s, insert->k.p, k);
1341 BUG_ON(bkey_deleted(k.k));
1342 extent_save(l->b, _k, k.k);
1343 verify_modified_extent(iter, _k);
1346 case BCH_EXTENT_OVERLAP_BACK:
1347 /* insert overlaps with end of k: */
1348 bch2_cut_subtract_back(s, bkey_start_pos(&insert->k), k);
1349 BUG_ON(bkey_deleted(k.k));
1350 extent_save(l->b, _k, k.k);
1353 * As the auxiliary tree is indexed by the end of the
1354 * key and we've just changed the end, update the
1357 bch2_bset_fix_invalidated_key(l->b, _k);
1358 bch2_btree_node_iter_fix(iter, l->b, &l->iter,
1359 _k, _k->u64s, _k->u64s);
1360 verify_modified_extent(iter, _k);
1363 case BCH_EXTENT_OVERLAP_ALL: {
1364 /* The insert key completely covers k, invalidate k */
1365 if (!bkey_whiteout(k.k))
1366 btree_account_key_drop(l->b, _k);
1368 bch2_drop_subtract(s, k);
1370 if (_k >= btree_bset_last(l->b)->start) {
1371 unsigned u64s = _k->u64s;
1373 bch2_bset_delete(l->b, _k, _k->u64s);
1374 bch2_btree_node_iter_fix(iter, l->b, &l->iter,
1376 bch2_btree_iter_verify(iter, l->b);
1378 extent_save(l->b, _k, k.k);
1379 bch2_btree_node_iter_fix(iter, l->b, &l->iter,
1380 _k, _k->u64s, _k->u64s);
1381 verify_modified_extent(iter, _k);
1386 case BCH_EXTENT_OVERLAP_MIDDLE: {
1387 BKEY_PADDED(k) split;
1389 * The insert key falls 'in the middle' of k
1390 * The insert key splits k in 3:
1391 * - start only in k, preserve
1392 * - middle common section, invalidate in k
1393 * - end only in k, preserve
1395 * We update the old key to preserve the start,
1396 * insert will be the new common section,
1397 * we manually insert the end that we are preserving.
1399 * modify k _before_ doing the insert (which will move
1402 bkey_reassemble(&split.k, k.s_c);
1403 split.k.k.needs_whiteout |= bkey_written(l->b, _k);
1405 bch2_cut_back(bkey_start_pos(&insert->k), &split.k.k);
1406 BUG_ON(bkey_deleted(&split.k.k));
1408 bch2_cut_subtract_front(s, insert->k.p, k);
1409 BUG_ON(bkey_deleted(k.k));
1410 extent_save(l->b, _k, k.k);
1411 verify_modified_extent(iter, _k);
1413 bch2_add_sectors(s, bkey_i_to_s_c(&split.k),
1414 bkey_start_offset(&split.k.k),
1416 extent_bset_insert(c, iter, &split.k);
1422 static void __bch2_insert_fixup_extent(struct extent_insert_state *s)
1424 struct btree_iter *iter = s->insert->iter;
1425 struct btree_iter_level *l = &iter->l[0];
1426 struct bkey_packed *_k;
1427 struct bkey unpacked;
1428 struct bkey_i *insert = s->insert->k;
1430 while (bkey_cmp(s->committed, insert->k.p) < 0 &&
1431 (_k = bch2_btree_node_iter_peek_filter(&l->iter, l->b,
1432 KEY_TYPE_DISCARD))) {
1433 struct bkey_s k = __bkey_disassemble(l->b, _k, &unpacked);
1434 enum bch_extent_overlap overlap = bch2_extent_overlap(&insert->k, k.k);
1436 EBUG_ON(bkey_cmp(iter->pos, k.k->p) >= 0);
1438 if (bkey_cmp(bkey_start_pos(k.k), insert->k.p) >= 0)
1441 s->committed = bpos_min(s->insert->k->k.p, k.k->p);
1443 if (!bkey_whiteout(k.k))
1444 s->update_journal = true;
1446 if (!s->update_journal) {
1447 bch2_cut_front(s->committed, insert);
1448 bch2_cut_front(s->committed, &s->whiteout);
1449 bch2_btree_iter_set_pos_same_leaf(iter, s->committed);
1454 * When deleting, if possible just do it by switching the type
1455 * of the key we're deleting, instead of creating and inserting
1460 !bkey_cmp(insert->k.p, k.k->p) &&
1461 !bkey_cmp(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) {
1462 if (!bkey_whiteout(k.k)) {
1463 btree_account_key_drop(l->b, _k);
1464 bch2_subtract_sectors(s, k.s_c,
1465 bkey_start_offset(k.k), k.k->size);
1466 _k->type = KEY_TYPE_DISCARD;
1467 reserve_whiteout(l->b, _k);
1472 if (k.k->needs_whiteout || bkey_written(l->b, _k)) {
1473 insert->k.needs_whiteout = true;
1474 s->update_btree = true;
1477 if (s->update_btree &&
1478 overlap == BCH_EXTENT_OVERLAP_ALL &&
1479 bkey_whiteout(k.k) &&
1480 k.k->needs_whiteout) {
1481 unreserve_whiteout(l->b, _k);
1482 _k->needs_whiteout = false;
1485 extent_squash(s, insert, _k, k, overlap);
1487 if (!s->update_btree)
1488 bch2_cut_front(s->committed, insert);
1490 if (overlap == BCH_EXTENT_OVERLAP_FRONT ||
1491 overlap == BCH_EXTENT_OVERLAP_MIDDLE)
1495 if (bkey_cmp(s->committed, insert->k.p) < 0)
1496 s->committed = bpos_min(s->insert->k->k.p, l->b->key.k.p);
1499 * may have skipped past some deleted extents greater than the insert
1500 * key, before we got to a non deleted extent and knew we could bail out
1501 * rewind the iterator a bit if necessary:
1504 struct btree_node_iter node_iter = l->iter;
1506 while ((_k = bch2_btree_node_iter_prev_all(&node_iter, l->b)) &&
1507 bkey_cmp_left_packed(l->b, _k, &s->committed) > 0)
1508 l->iter = node_iter;
1513 * bch_extent_insert_fixup - insert a new extent and deal with overlaps
1515 * this may result in not actually doing the insert, or inserting some subset
1516 * of the insert key. For cmpxchg operations this is where that logic lives.
1518 * All subsets of @insert that need to be inserted are inserted using
1519 * bch2_btree_insert_and_journal(). If @b or @res fills up, this function
1520 * returns false, setting @iter->pos for the prefix of @insert that actually got
1523 * BSET INVARIANTS: this function is responsible for maintaining all the
1524 * invariants for bsets of extents in memory. things get really hairy with 0
1529 * bkey_start_pos(bkey_next(k)) >= k
1530 * or bkey_start_offset(bkey_next(k)) >= k->offset
1532 * i.e. strict ordering, no overlapping extents.
1534 * multiple bsets (i.e. full btree node):
1537 * k.size != 0 ∧ j.size != 0 →
1538 * ¬ (k > bkey_start_pos(j) ∧ k < j)
1540 * i.e. no two overlapping keys _of nonzero size_
1542 * We can't realistically maintain this invariant for zero size keys because of
1543 * the key merging done in bch2_btree_insert_key() - for two mergeable keys k, j
1544 * there may be another 0 size key between them in another bset, and it will
1545 * thus overlap with the merged key.
1547 * In addition, the end of iter->pos indicates how much has been processed.
1548 * If the end of iter->pos is not the same as the end of insert, then
1549 * key insertion needs to continue/be retried.
1551 enum btree_insert_ret
1552 bch2_insert_fixup_extent(struct btree_insert *trans,
1553 struct btree_insert_entry *insert)
1555 struct bch_fs *c = trans->c;
1556 struct btree_iter *iter = insert->iter;
1557 struct btree *b = iter->l[0].b;
1558 struct extent_insert_state s = {
1561 .committed = iter->pos,
1563 .whiteout = *insert->k,
1564 .update_journal = !bkey_whiteout(&insert->k->k),
1565 .update_btree = !bkey_whiteout(&insert->k->k),
1566 .deleting = bkey_whiteout(&insert->k->k),
1569 EBUG_ON(iter->level);
1570 EBUG_ON(!insert->k->k.size);
1573 * As we process overlapping extents, we advance @iter->pos both to
1574 * signal to our caller (btree_insert_key()) how much of @insert->k has
1575 * been inserted, and also to keep @iter->pos consistent with
1576 * @insert->k and the node iterator that we're advancing:
1578 EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k->k)));
1581 !(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))
1582 bch2_add_sectors(&s, bkey_i_to_s_c(insert->k),
1583 bkey_start_offset(&insert->k->k),
1586 __bch2_insert_fixup_extent(&s);
1588 extent_insert_committed(&s);
1590 bch2_fs_usage_apply(c, &s.stats, trans->disk_res,
1591 gc_pos_btree_node(b));
1593 EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k->k)));
1594 EBUG_ON(bkey_cmp(iter->pos, s.committed));
1596 if (insert->k->k.size) {
1597 /* got to the end of this leaf node */
1598 BUG_ON(bkey_cmp(iter->pos, b->key.k.p));
1599 return BTREE_INSERT_NEED_TRAVERSE;
1602 return BTREE_INSERT_OK;
1605 const char *bch2_extent_invalid(const struct bch_fs *c, struct bkey_s_c k)
1607 if (bkey_val_u64s(k.k) > BKEY_EXTENT_VAL_U64s_MAX)
1608 return "value too big";
1611 return "zero key size";
1613 switch (k.k->type) {
1615 case BCH_EXTENT_CACHED: {
1616 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
1617 const union bch_extent_entry *entry;
1618 struct bch_extent_crc_unpacked crc;
1619 const struct bch_extent_ptr *ptr;
1620 unsigned size_ondisk = e.k->size;
1622 unsigned nonce = UINT_MAX;
1624 extent_for_each_entry(e, entry) {
1625 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
1626 return "invalid extent entry type";
1628 if (extent_entry_is_crc(entry)) {
1629 crc = bch2_extent_crc_unpack(e.k, entry_to_crc(entry));
1631 if (crc.offset + e.k->size >
1632 crc.uncompressed_size)
1633 return "checksum offset + key size > uncompressed size";
1635 size_ondisk = crc.compressed_size;
1637 if (!bch2_checksum_type_valid(c, crc.csum_type))
1638 return "invalid checksum type";
1640 if (crc.compression_type >= BCH_COMPRESSION_NR)
1641 return "invalid compression type";
1643 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1644 if (nonce == UINT_MAX)
1645 nonce = crc.offset + crc.nonce;
1646 else if (nonce != crc.offset + crc.nonce)
1647 return "incorrect nonce";
1650 ptr = entry_to_ptr(entry);
1652 reason = extent_ptr_invalid(c, e, &entry->ptr,
1653 size_ondisk, false);
1662 case BCH_RESERVATION: {
1663 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
1665 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation))
1666 return "incorrect value size";
1668 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX)
1669 return "invalid nr_replicas";
1675 return "invalid value type";
1679 static void bch2_extent_debugcheck_extent(struct bch_fs *c, struct btree *b,
1680 struct bkey_s_c_extent e)
1682 const struct bch_extent_ptr *ptr;
1684 struct bucket_mark mark;
1685 unsigned seq, stale;
1688 unsigned replicas = 0;
1691 * XXX: we should be doing most/all of these checks at startup time,
1692 * where we check bch2_bkey_invalid() in btree_node_read_done()
1694 * But note that we can't check for stale pointers or incorrect gc marks
1695 * until after journal replay is done (it might be an extent that's
1696 * going to get overwritten during replay)
1699 extent_for_each_ptr(e, ptr) {
1700 ca = bch_dev_bkey_exists(c, ptr->dev);
1704 * If journal replay hasn't finished, we might be seeing keys
1705 * that will be overwritten by the time journal replay is done:
1707 if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
1713 seq = read_seqcount_begin(&c->gc_pos_lock);
1714 mark = ptr_bucket_mark(ca, ptr);
1716 /* between mark and bucket gen */
1719 stale = ptr_stale(ca, ptr);
1721 bch2_fs_bug_on(stale && !ptr->cached, c,
1722 "stale dirty pointer");
1724 bch2_fs_bug_on(stale > 96, c,
1725 "key too stale: %i",
1731 bad = gc_pos_cmp(c->gc_pos, gc_pos_btree_node(b)) > 0 &&
1732 (mark.data_type != BCH_DATA_USER ||
1734 ? mark.cached_sectors
1735 : mark.dirty_sectors));
1736 } while (read_seqcount_retry(&c->gc_pos_lock, seq));
1742 if (replicas > BCH_REPLICAS_MAX) {
1743 bch2_bkey_val_to_text(c, btree_node_type(b), buf,
1744 sizeof(buf), e.s_c);
1746 "extent key bad (too many replicas: %u): %s",
1751 if (!bkey_extent_is_cached(e.k) &&
1752 !bch2_bkey_replicas_marked(c, BCH_DATA_USER, e.s_c)) {
1753 bch2_bkey_val_to_text(c, btree_node_type(b),
1754 buf, sizeof(buf), e.s_c);
1756 "extent key bad (replicas not marked in superblock):\n%s",
1764 bch2_bkey_val_to_text(c, btree_node_type(b), buf,
1765 sizeof(buf), e.s_c);
1766 bch2_fs_bug(c, "extent pointer bad gc mark: %s:\nbucket %zu "
1767 "gen %i type %u", buf,
1768 PTR_BUCKET_NR(ca, ptr), mark.gen, mark.data_type);
1772 void bch2_extent_debugcheck(struct bch_fs *c, struct btree *b, struct bkey_s_c k)
1774 switch (k.k->type) {
1776 case BCH_EXTENT_CACHED:
1777 bch2_extent_debugcheck_extent(c, b, bkey_s_c_to_extent(k));
1779 case BCH_RESERVATION:
1786 int bch2_extent_to_text(struct bch_fs *c, char *buf,
1787 size_t size, struct bkey_s_c k)
1789 char *out = buf, *end = buf + size;
1790 const char *invalid;
1792 #define p(...) (out += scnprintf(out, end - out, __VA_ARGS__))
1794 if (bkey_extent_is_data(k.k))
1795 out += extent_print_ptrs(c, buf, size, bkey_s_c_to_extent(k));
1797 invalid = bch2_extent_invalid(c, k);
1799 p(" invalid: %s", invalid);
1804 static void bch2_extent_crc_init(union bch_extent_crc *crc,
1805 struct bch_extent_crc_unpacked new)
1807 #define common_fields(_crc) \
1808 .csum_type = _crc.csum_type, \
1809 .compression_type = _crc.compression_type, \
1810 ._compressed_size = _crc.compressed_size - 1, \
1811 ._uncompressed_size = _crc.uncompressed_size - 1, \
1812 .offset = _crc.offset
1814 if (bch_crc_bytes[new.csum_type] <= 4 &&
1815 new.uncompressed_size <= CRC32_SIZE_MAX &&
1816 new.nonce <= CRC32_NONCE_MAX) {
1817 crc->crc32 = (struct bch_extent_crc32) {
1818 .type = 1 << BCH_EXTENT_ENTRY_crc32,
1820 .csum = *((__le32 *) &new.csum.lo),
1825 if (bch_crc_bytes[new.csum_type] <= 10 &&
1826 new.uncompressed_size <= CRC64_SIZE_MAX &&
1827 new.nonce <= CRC64_NONCE_MAX) {
1828 crc->crc64 = (struct bch_extent_crc64) {
1829 .type = 1 << BCH_EXTENT_ENTRY_crc64,
1832 .csum_lo = new.csum.lo,
1833 .csum_hi = *((__le16 *) &new.csum.hi),
1838 if (bch_crc_bytes[new.csum_type] <= 16 &&
1839 new.uncompressed_size <= CRC128_SIZE_MAX &&
1840 new.nonce <= CRC128_NONCE_MAX) {
1841 crc->crc128 = (struct bch_extent_crc128) {
1842 .type = 1 << BCH_EXTENT_ENTRY_crc128,
1849 #undef common_fields
1853 void bch2_extent_crc_append(struct bkey_i_extent *e,
1854 struct bch_extent_crc_unpacked new)
1856 struct bch_extent_crc_unpacked crc;
1857 const union bch_extent_entry *i;
1859 BUG_ON(new.compressed_size > new.uncompressed_size);
1860 BUG_ON(new.live_size != e->k.size);
1861 BUG_ON(!new.compressed_size || !new.uncompressed_size);
1864 * Look up the last crc entry, so we can check if we need to add
1867 extent_for_each_crc(extent_i_to_s(e), crc, i)
1870 if (!bch2_crc_unpacked_cmp(crc, new))
1873 bch2_extent_crc_init((void *) extent_entry_last(extent_i_to_s(e)), new);
1874 __extent_entry_push(e);
1878 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
1880 * Returns true if @k should be dropped entirely
1882 * For existing keys, only called when btree nodes are being rewritten, not when
1883 * they're merely being compacted/resorted in memory.
1885 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
1887 struct bkey_s_extent e;
1889 switch (k.k->type) {
1890 case KEY_TYPE_ERROR:
1893 case KEY_TYPE_DELETED:
1895 case KEY_TYPE_DISCARD:
1896 return bversion_zero(k.k->version);
1897 case KEY_TYPE_COOKIE:
1901 case BCH_EXTENT_CACHED:
1902 e = bkey_s_to_extent(k);
1904 bch2_extent_drop_stale(c, e);
1906 if (!bkey_val_u64s(e.k)) {
1907 if (bkey_extent_is_cached(e.k)) {
1908 k.k->type = KEY_TYPE_DISCARD;
1909 if (bversion_zero(k.k->version))
1912 k.k->type = KEY_TYPE_ERROR;
1917 case BCH_RESERVATION:
1924 void bch2_extent_mark_replicas_cached(struct bch_fs *c,
1925 struct bkey_s_extent e,
1927 unsigned nr_desired_replicas)
1929 struct bch_extent_ptr *ptr;
1930 int extra = bch2_extent_durability(c, e.c) - nr_desired_replicas;
1932 if (target && extra > 0)
1933 extent_for_each_ptr(e, ptr) {
1934 int n = bch2_extent_ptr_durability(c, ptr);
1936 if (n && n <= extra &&
1937 !bch2_dev_in_target(c, ptr->dev, target)) {
1944 extent_for_each_ptr(e, ptr) {
1945 int n = bch2_extent_ptr_durability(c, ptr);
1947 if (n && n <= extra) {
1955 * This picks a non-stale pointer, preferably from a device other than @avoid.
1956 * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to
1957 * other devices, it will still pick a pointer from avoid.
1959 int bch2_extent_pick_ptr(struct bch_fs *c, struct bkey_s_c k,
1960 struct bch_devs_mask *avoid,
1961 struct extent_ptr_decoded *pick)
1965 switch (k.k->type) {
1966 case KEY_TYPE_ERROR:
1970 case BCH_EXTENT_CACHED:
1971 ret = extent_pick_read_device(c, bkey_s_c_to_extent(k),
1974 if (!ret && !bkey_extent_is_cached(k.k))
1984 enum merge_result bch2_extent_merge(struct bch_fs *c, struct btree *b,
1985 struct bkey_i *l, struct bkey_i *r)
1987 struct bkey_s_extent el, er;
1988 union bch_extent_entry *en_l, *en_r;
1990 if (key_merging_disabled(c))
1991 return BCH_MERGE_NOMERGE;
1994 * Generic header checks
1995 * Assumes left and right are in order
1996 * Left and right must be exactly aligned
1999 if (l->k.u64s != r->k.u64s ||
2000 l->k.type != r->k.type ||
2001 bversion_cmp(l->k.version, r->k.version) ||
2002 bkey_cmp(l->k.p, bkey_start_pos(&r->k)))
2003 return BCH_MERGE_NOMERGE;
2005 switch (l->k.type) {
2006 case KEY_TYPE_DISCARD:
2007 case KEY_TYPE_ERROR:
2008 /* These types are mergeable, and no val to check */
2012 case BCH_EXTENT_CACHED:
2013 el = bkey_i_to_s_extent(l);
2014 er = bkey_i_to_s_extent(r);
2016 extent_for_each_entry(el, en_l) {
2017 struct bch_extent_ptr *lp, *rp;
2020 en_r = vstruct_idx(er.v, (u64 *) en_l - el.v->_data);
2022 if ((extent_entry_type(en_l) !=
2023 extent_entry_type(en_r)) ||
2024 extent_entry_is_crc(en_l))
2025 return BCH_MERGE_NOMERGE;
2030 if (lp->offset + el.k->size != rp->offset ||
2031 lp->dev != rp->dev ||
2033 return BCH_MERGE_NOMERGE;
2035 /* We don't allow extents to straddle buckets: */
2036 ca = bch_dev_bkey_exists(c, lp->dev);
2038 if (PTR_BUCKET_NR(ca, lp) != PTR_BUCKET_NR(ca, rp))
2039 return BCH_MERGE_NOMERGE;
2043 case BCH_RESERVATION: {
2044 struct bkey_i_reservation *li = bkey_i_to_reservation(l);
2045 struct bkey_i_reservation *ri = bkey_i_to_reservation(r);
2047 if (li->v.generation != ri->v.generation ||
2048 li->v.nr_replicas != ri->v.nr_replicas)
2049 return BCH_MERGE_NOMERGE;
2053 return BCH_MERGE_NOMERGE;
2056 l->k.needs_whiteout |= r->k.needs_whiteout;
2058 /* Keys with no pointers aren't restricted to one bucket and could
2061 if ((u64) l->k.size + r->k.size > KEY_SIZE_MAX) {
2062 bch2_key_resize(&l->k, KEY_SIZE_MAX);
2063 bch2_cut_front(l->k.p, r);
2064 return BCH_MERGE_PARTIAL;
2067 bch2_key_resize(&l->k, l->k.size + r->k.size);
2069 return BCH_MERGE_MERGE;
2073 * When merging an extent that we're inserting into a btree node, the new merged
2074 * extent could overlap with an existing 0 size extent - if we don't fix that,
2075 * it'll break the btree node iterator so this code finds those 0 size extents
2076 * and shifts them out of the way.
2078 * Also unpacks and repacks.
2080 static bool bch2_extent_merge_inline(struct bch_fs *c,
2081 struct btree_iter *iter,
2082 struct bkey_packed *l,
2083 struct bkey_packed *r,
2086 struct btree *b = iter->l[0].b;
2087 struct btree_node_iter *node_iter = &iter->l[0].iter;
2088 BKEY_PADDED(k) li, ri;
2089 struct bkey_packed *m = back_merge ? l : r;
2090 struct bkey_i *mi = back_merge ? &li.k : &ri.k;
2091 struct bset_tree *t = bch2_bkey_to_bset(b, m);
2092 enum merge_result ret;
2094 EBUG_ON(bkey_written(b, m));
2097 * We need to save copies of both l and r, because we might get a
2098 * partial merge (which modifies both) and then fails to repack
2100 bch2_bkey_unpack(b, &li.k, l);
2101 bch2_bkey_unpack(b, &ri.k, r);
2103 ret = bch2_extent_merge(c, b, &li.k, &ri.k);
2104 if (ret == BCH_MERGE_NOMERGE)
2108 * check if we overlap with deleted extents - would break the sort
2112 struct bkey_packed *n = bkey_next(m);
2114 if (n != btree_bkey_last(b, t) &&
2115 bkey_cmp_left_packed(b, n, &li.k.k.p) <= 0 &&
2118 } else if (ret == BCH_MERGE_MERGE) {
2119 struct bkey_packed *prev = bch2_bkey_prev_all(b, t, m);
2122 bkey_cmp_left_packed_byval(b, prev,
2123 bkey_start_pos(&li.k.k)) > 0)
2127 if (ret == BCH_MERGE_PARTIAL) {
2128 if (!extent_i_save(b, m, mi))
2132 bkey_copy(packed_to_bkey(l), &li.k);
2134 bkey_copy(packed_to_bkey(r), &ri.k);
2136 if (!extent_i_save(b, m, &li.k))
2140 bch2_bset_fix_invalidated_key(b, m);
2141 bch2_btree_node_iter_fix(iter, b, node_iter,
2142 m, m->u64s, m->u64s);
2143 verify_modified_extent(iter, m);
2145 return ret == BCH_MERGE_MERGE;
2148 int bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size)
2150 struct btree_iter iter;
2151 struct bpos end = pos;
2157 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, pos,
2158 BTREE_ITER_SLOTS, k) {
2159 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
2162 if (!bch2_extent_is_fully_allocated(k)) {
2167 bch2_btree_iter_unlock(&iter);