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"
13 #include "btree_iter.h"
17 #include "disk_groups.h"
28 static unsigned bch2_crc_field_size_max[] = {
29 [BCH_EXTENT_ENTRY_crc32] = CRC32_SIZE_MAX,
30 [BCH_EXTENT_ENTRY_crc64] = CRC64_SIZE_MAX,
31 [BCH_EXTENT_ENTRY_crc128] = CRC128_SIZE_MAX,
34 static void bch2_extent_crc_pack(union bch_extent_crc *,
35 struct bch_extent_crc_unpacked,
36 enum bch_extent_entry_type);
38 static struct bch_dev_io_failures *dev_io_failures(struct bch_io_failures *f,
41 struct bch_dev_io_failures *i;
43 for (i = f->devs; i < f->devs + f->nr; i++)
50 void bch2_mark_io_failure(struct bch_io_failures *failed,
51 struct extent_ptr_decoded *p)
53 struct bch_dev_io_failures *f = dev_io_failures(failed, p->ptr.dev);
56 BUG_ON(failed->nr >= ARRAY_SIZE(failed->devs));
58 f = &failed->devs[failed->nr++];
63 } else if (p->idx != f->idx) {
73 * returns true if p1 is better than p2:
75 static inline bool ptr_better(struct bch_fs *c,
76 const struct extent_ptr_decoded p1,
77 const struct extent_ptr_decoded p2)
79 if (likely(!p1.idx && !p2.idx)) {
80 struct bch_dev *dev1 = bch_dev_bkey_exists(c, p1.ptr.dev);
81 struct bch_dev *dev2 = bch_dev_bkey_exists(c, p2.ptr.dev);
83 u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
84 u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
86 /* Pick at random, biased in favor of the faster device: */
88 return bch2_rand_range(l1 + l2) > l1;
91 if (bch2_force_reconstruct_read)
92 return p1.idx > p2.idx;
94 return p1.idx < p2.idx;
98 * This picks a non-stale pointer, preferably from a device other than @avoid.
99 * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to
100 * other devices, it will still pick a pointer from avoid.
102 int bch2_bkey_pick_read_device(struct bch_fs *c, struct bkey_s_c k,
103 struct bch_io_failures *failed,
104 struct extent_ptr_decoded *pick)
106 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
107 const union bch_extent_entry *entry;
108 struct extent_ptr_decoded p;
109 struct bch_dev_io_failures *f;
113 if (k.k->type == KEY_TYPE_error)
116 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
117 ca = bch_dev_bkey_exists(c, p.ptr.dev);
120 * If there are any dirty pointers it's an error if we can't
123 if (!ret && !p.ptr.cached)
126 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
129 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
131 p.idx = f->nr_failed < f->nr_retries
136 !bch2_dev_is_readable(ca))
139 if (bch2_force_reconstruct_read &&
143 if (p.idx >= (unsigned) p.has_ec + 1)
146 if (ret > 0 && !ptr_better(c, p, *pick))
156 /* KEY_TYPE_btree_ptr: */
158 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
160 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
161 return "value too big";
163 return bch2_bkey_ptrs_invalid(c, k);
166 void bch2_btree_ptr_debugcheck(struct bch_fs *c, struct bkey_s_c k)
168 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
169 const struct bch_extent_ptr *ptr;
172 struct bucket_mark mark;
175 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
178 if (!percpu_down_read_trylock(&c->mark_lock))
181 bkey_for_each_ptr(ptrs, ptr) {
182 ca = bch_dev_bkey_exists(c, ptr->dev);
184 mark = ptr_bucket_mark(ca, ptr);
187 if (gen_after(mark.gen, ptr->gen))
190 err = "inconsistent";
191 if (mark.data_type != BCH_DATA_btree ||
192 mark.dirty_sectors < c->opts.btree_node_size)
196 percpu_up_read(&c->mark_lock);
199 bch2_fs_inconsistent(c, "%s btree pointer %s: bucket %zi gen %i mark %08x",
200 err, (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf),
201 PTR_BUCKET_NR(ca, ptr),
202 mark.gen, (unsigned) mark.v.counter);
206 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
209 bch2_bkey_ptrs_to_text(out, c, k);
212 void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c,
215 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
217 pr_buf(out, "seq %llx sectors %u written %u min_key ",
218 le64_to_cpu(bp.v->seq),
219 le16_to_cpu(bp.v->sectors),
220 le16_to_cpu(bp.v->sectors_written));
222 bch2_bpos_to_text(out, bp.v->min_key);
224 bch2_bkey_ptrs_to_text(out, c, k);
227 void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version,
228 unsigned big_endian, int write,
231 struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k);
233 compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key);
235 if (version < bcachefs_metadata_version_inode_btree_change &&
236 btree_node_type_is_extents(btree_id) &&
237 bkey_cmp(bp.v->min_key, POS_MIN))
238 bp.v->min_key = write
239 ? bkey_predecessor(bp.v->min_key)
240 : bkey_successor(bp.v->min_key);
243 /* KEY_TYPE_extent: */
245 const char *bch2_extent_invalid(const struct bch_fs *c, struct bkey_s_c k)
247 return bch2_bkey_ptrs_invalid(c, k);
250 void bch2_extent_debugcheck(struct bch_fs *c, struct bkey_s_c k)
252 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
253 const union bch_extent_entry *entry;
254 struct extent_ptr_decoded p;
257 if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) ||
258 !test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
261 if (!percpu_down_read_trylock(&c->mark_lock))
264 extent_for_each_ptr_decode(e, p, entry) {
265 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
266 struct bucket_mark mark = ptr_bucket_mark(ca, &p.ptr);
267 unsigned stale = gen_after(mark.gen, p.ptr.gen);
268 unsigned disk_sectors = ptr_disk_sectors(p);
269 unsigned mark_sectors = p.ptr.cached
270 ? mark.cached_sectors
271 : mark.dirty_sectors;
273 bch2_fs_inconsistent_on(stale && !p.ptr.cached, c,
274 "stale dirty pointer (ptr gen %u bucket %u",
275 p.ptr.gen, mark.gen);
277 bch2_fs_inconsistent_on(stale > 96, c,
278 "key too stale: %i", stale);
280 bch2_fs_inconsistent_on(!stale &&
281 (mark.data_type != BCH_DATA_user ||
282 mark_sectors < disk_sectors), c,
283 "extent pointer not marked: %s:\n"
284 "type %u sectors %u < %u",
285 (bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c), buf),
287 mark_sectors, disk_sectors);
290 percpu_up_read(&c->mark_lock);
293 void bch2_extent_to_text(struct printbuf *out, struct bch_fs *c,
296 bch2_bkey_ptrs_to_text(out, c, k);
299 enum merge_result bch2_extent_merge(struct bch_fs *c,
300 struct bkey_s _l, struct bkey_s _r)
302 struct bkey_s_extent l = bkey_s_to_extent(_l);
303 struct bkey_s_extent r = bkey_s_to_extent(_r);
304 union bch_extent_entry *en_l = l.v->start;
305 union bch_extent_entry *en_r = r.v->start;
306 struct bch_extent_crc_unpacked crc_l, crc_r;
308 if (bkey_val_u64s(l.k) != bkey_val_u64s(r.k))
309 return BCH_MERGE_NOMERGE;
311 crc_l = bch2_extent_crc_unpack(l.k, NULL);
313 extent_for_each_entry(l, en_l) {
314 en_r = vstruct_idx(r.v, (u64 *) en_l - l.v->_data);
316 if (extent_entry_type(en_l) != extent_entry_type(en_r))
317 return BCH_MERGE_NOMERGE;
319 switch (extent_entry_type(en_l)) {
320 case BCH_EXTENT_ENTRY_ptr: {
321 const struct bch_extent_ptr *lp = &en_l->ptr;
322 const struct bch_extent_ptr *rp = &en_r->ptr;
325 if (lp->offset + crc_l.compressed_size != rp->offset ||
326 lp->dev != rp->dev ||
328 return BCH_MERGE_NOMERGE;
330 /* We don't allow extents to straddle buckets: */
331 ca = bch_dev_bkey_exists(c, lp->dev);
333 if (PTR_BUCKET_NR(ca, lp) != PTR_BUCKET_NR(ca, rp))
334 return BCH_MERGE_NOMERGE;
338 case BCH_EXTENT_ENTRY_stripe_ptr:
339 if (en_l->stripe_ptr.block != en_r->stripe_ptr.block ||
340 en_l->stripe_ptr.idx != en_r->stripe_ptr.idx)
341 return BCH_MERGE_NOMERGE;
343 case BCH_EXTENT_ENTRY_crc32:
344 case BCH_EXTENT_ENTRY_crc64:
345 case BCH_EXTENT_ENTRY_crc128:
346 crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
347 crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
349 if (crc_l.csum_type != crc_r.csum_type ||
350 crc_l.compression_type != crc_r.compression_type ||
351 crc_l.nonce != crc_r.nonce)
352 return BCH_MERGE_NOMERGE;
354 if (crc_l.offset + crc_l.live_size != crc_l.compressed_size ||
356 return BCH_MERGE_NOMERGE;
358 if (!bch2_checksum_mergeable(crc_l.csum_type))
359 return BCH_MERGE_NOMERGE;
361 if (crc_is_compressed(crc_l))
362 return BCH_MERGE_NOMERGE;
364 if (crc_l.csum_type &&
365 crc_l.uncompressed_size +
366 crc_r.uncompressed_size > c->sb.encoded_extent_max)
367 return BCH_MERGE_NOMERGE;
369 if (crc_l.uncompressed_size + crc_r.uncompressed_size >
370 bch2_crc_field_size_max[extent_entry_type(en_l)])
371 return BCH_MERGE_NOMERGE;
375 return BCH_MERGE_NOMERGE;
379 extent_for_each_entry(l, en_l) {
380 struct bch_extent_crc_unpacked crc_l, crc_r;
382 en_r = vstruct_idx(r.v, (u64 *) en_l - l.v->_data);
384 if (!extent_entry_is_crc(en_l))
387 crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
388 crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
390 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
393 crc_r.uncompressed_size << 9);
395 crc_l.uncompressed_size += crc_r.uncompressed_size;
396 crc_l.compressed_size += crc_r.compressed_size;
398 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l,
399 extent_entry_type(en_l));
402 bch2_key_resize(l.k, l.k->size + r.k->size);
404 return BCH_MERGE_MERGE;
407 /* KEY_TYPE_reservation: */
409 const char *bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k)
411 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
413 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation))
414 return "incorrect value size";
416 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX)
417 return "invalid nr_replicas";
422 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
425 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
427 pr_buf(out, "generation %u replicas %u",
428 le32_to_cpu(r.v->generation),
432 enum merge_result bch2_reservation_merge(struct bch_fs *c,
433 struct bkey_s _l, struct bkey_s _r)
435 struct bkey_s_reservation l = bkey_s_to_reservation(_l);
436 struct bkey_s_reservation r = bkey_s_to_reservation(_r);
438 if (l.v->generation != r.v->generation ||
439 l.v->nr_replicas != r.v->nr_replicas)
440 return BCH_MERGE_NOMERGE;
442 if ((u64) l.k->size + r.k->size > KEY_SIZE_MAX) {
443 bch2_key_resize(l.k, KEY_SIZE_MAX);
444 bch2_cut_front_s(l.k->p, r.s);
445 return BCH_MERGE_PARTIAL;
448 bch2_key_resize(l.k, l.k->size + r.k->size);
450 return BCH_MERGE_MERGE;
453 /* Extent checksum entries: */
455 /* returns true if not equal */
456 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
457 struct bch_extent_crc_unpacked r)
459 return (l.csum_type != r.csum_type ||
460 l.compression_type != r.compression_type ||
461 l.compressed_size != r.compressed_size ||
462 l.uncompressed_size != r.uncompressed_size ||
463 l.offset != r.offset ||
464 l.live_size != r.live_size ||
465 l.nonce != r.nonce ||
466 bch2_crc_cmp(l.csum, r.csum));
469 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
470 struct bch_extent_crc_unpacked n)
472 return !crc_is_compressed(u) &&
474 u.uncompressed_size > u.live_size &&
475 bch2_csum_type_is_encryption(u.csum_type) ==
476 bch2_csum_type_is_encryption(n.csum_type);
479 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
480 struct bch_extent_crc_unpacked n)
482 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
483 struct bch_extent_crc_unpacked crc;
484 const union bch_extent_entry *i;
489 bkey_for_each_crc(k.k, ptrs, crc, i)
490 if (can_narrow_crc(crc, n))
497 * We're writing another replica for this extent, so while we've got the data in
498 * memory we'll be computing a new checksum for the currently live data.
500 * If there are other replicas we aren't moving, and they are checksummed but
501 * not compressed, we can modify them to point to only the data that is
502 * currently live (so that readers won't have to bounce) while we've got the
505 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
507 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
508 struct bch_extent_crc_unpacked u;
509 struct extent_ptr_decoded p;
510 union bch_extent_entry *i;
513 /* Find a checksum entry that covers only live data: */
515 bkey_for_each_crc(&k->k, ptrs, u, i)
516 if (!crc_is_compressed(u) &&
518 u.live_size == u.uncompressed_size) {
525 BUG_ON(crc_is_compressed(n));
527 BUG_ON(n.live_size != k->k.size);
529 restart_narrow_pointers:
530 ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
532 bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
533 if (can_narrow_crc(p.crc, n)) {
534 bch2_bkey_drop_ptr(bkey_i_to_s(k), &i->ptr);
535 p.ptr.offset += p.crc.offset;
537 bch2_extent_ptr_decoded_append(k, &p);
539 goto restart_narrow_pointers;
545 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
546 struct bch_extent_crc_unpacked src,
547 enum bch_extent_entry_type type)
549 #define set_common_fields(_dst, _src) \
550 _dst.type = 1 << type; \
551 _dst.csum_type = _src.csum_type, \
552 _dst.compression_type = _src.compression_type, \
553 _dst._compressed_size = _src.compressed_size - 1, \
554 _dst._uncompressed_size = _src.uncompressed_size - 1, \
555 _dst.offset = _src.offset
558 case BCH_EXTENT_ENTRY_crc32:
559 set_common_fields(dst->crc32, src);
560 dst->crc32.csum = *((__le32 *) &src.csum.lo);
562 case BCH_EXTENT_ENTRY_crc64:
563 set_common_fields(dst->crc64, src);
564 dst->crc64.nonce = src.nonce;
565 dst->crc64.csum_lo = src.csum.lo;
566 dst->crc64.csum_hi = *((__le16 *) &src.csum.hi);
568 case BCH_EXTENT_ENTRY_crc128:
569 set_common_fields(dst->crc128, src);
570 dst->crc128.nonce = src.nonce;
571 dst->crc128.csum = src.csum;
576 #undef set_common_fields
579 void bch2_extent_crc_append(struct bkey_i *k,
580 struct bch_extent_crc_unpacked new)
582 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
583 union bch_extent_crc *crc = (void *) ptrs.end;
584 enum bch_extent_entry_type type;
586 if (bch_crc_bytes[new.csum_type] <= 4 &&
587 new.uncompressed_size <= CRC32_SIZE_MAX &&
588 new.nonce <= CRC32_NONCE_MAX)
589 type = BCH_EXTENT_ENTRY_crc32;
590 else if (bch_crc_bytes[new.csum_type] <= 10 &&
591 new.uncompressed_size <= CRC64_SIZE_MAX &&
592 new.nonce <= CRC64_NONCE_MAX)
593 type = BCH_EXTENT_ENTRY_crc64;
594 else if (bch_crc_bytes[new.csum_type] <= 16 &&
595 new.uncompressed_size <= CRC128_SIZE_MAX &&
596 new.nonce <= CRC128_NONCE_MAX)
597 type = BCH_EXTENT_ENTRY_crc128;
601 bch2_extent_crc_pack(crc, new, type);
603 k->k.u64s += extent_entry_u64s(ptrs.end);
605 EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
608 /* Generic code for keys with pointers: */
610 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
612 return bch2_bkey_devs(k).nr;
615 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
617 return k.k->type == KEY_TYPE_reservation
618 ? bkey_s_c_to_reservation(k).v->nr_replicas
619 : bch2_bkey_dirty_devs(k).nr;
622 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
626 if (k.k->type == KEY_TYPE_reservation) {
627 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
629 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
630 const union bch_extent_entry *entry;
631 struct extent_ptr_decoded p;
633 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
634 ret += !p.ptr.cached && !crc_is_compressed(p.crc);
640 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
642 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
643 const union bch_extent_entry *entry;
644 struct extent_ptr_decoded p;
647 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
648 if (!p.ptr.cached && crc_is_compressed(p.crc))
649 ret += p.crc.compressed_size;
654 bool bch2_bkey_is_incompressible(struct bkey_s_c k)
656 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
657 const union bch_extent_entry *entry;
658 struct bch_extent_crc_unpacked crc;
660 bkey_for_each_crc(k.k, ptrs, crc, entry)
661 if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
666 bool bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size,
667 unsigned nr_replicas, bool compressed)
669 struct btree_trans trans;
670 struct btree_iter *iter;
671 struct bpos end = pos;
678 bch2_trans_init(&trans, c, 0, 0);
680 for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, pos,
681 BTREE_ITER_SLOTS, k, err) {
682 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
685 if (nr_replicas > bch2_bkey_replicas(c, k) ||
686 (!compressed && bch2_bkey_sectors_compressed(k))) {
691 bch2_trans_exit(&trans);
696 unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
698 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
699 const union bch_extent_entry *entry;
700 struct extent_ptr_decoded p;
701 unsigned replicas = 0;
703 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
709 genradix_ptr(&c->stripes[0], p.ec.idx);
713 replicas += s->nr_redundant;
723 static unsigned bch2_extent_ptr_durability(struct bch_fs *c,
724 struct extent_ptr_decoded p)
726 unsigned durability = 0;
732 ca = bch_dev_bkey_exists(c, p.ptr.dev);
734 if (ca->mi.state != BCH_MEMBER_STATE_FAILED)
735 durability = max_t(unsigned, durability, ca->mi.durability);
739 genradix_ptr(&c->stripes[0], p.ec.idx);
744 durability += s->nr_redundant;
750 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
752 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
753 const union bch_extent_entry *entry;
754 struct extent_ptr_decoded p;
755 unsigned durability = 0;
757 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
758 durability += bch2_extent_ptr_durability(c, p);
763 void bch2_bkey_mark_replicas_cached(struct bch_fs *c, struct bkey_s k,
765 unsigned nr_desired_replicas)
767 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
768 union bch_extent_entry *entry;
769 struct extent_ptr_decoded p;
770 int extra = bch2_bkey_durability(c, k.s_c) - nr_desired_replicas;
772 if (target && extra > 0)
773 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
774 int n = bch2_extent_ptr_durability(c, p);
776 if (n && n <= extra &&
777 !bch2_dev_in_target(c, p.ptr.dev, target)) {
778 entry->ptr.cached = true;
784 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
785 int n = bch2_extent_ptr_durability(c, p);
787 if (n && n <= extra) {
788 entry->ptr.cached = true;
794 void bch2_bkey_append_ptr(struct bkey_i *k,
795 struct bch_extent_ptr ptr)
797 EBUG_ON(bch2_bkey_has_device(bkey_i_to_s_c(k), ptr.dev));
800 case KEY_TYPE_btree_ptr:
801 case KEY_TYPE_btree_ptr_v2:
802 case KEY_TYPE_extent:
803 EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
805 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
807 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
817 static inline void __extent_entry_insert(struct bkey_i *k,
818 union bch_extent_entry *dst,
819 union bch_extent_entry *new)
821 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
823 memmove_u64s_up_small((u64 *) dst + extent_entry_u64s(new),
824 dst, (u64 *) end - (u64 *) dst);
825 k->k.u64s += extent_entry_u64s(new);
826 memcpy_u64s_small(dst, new, extent_entry_u64s(new));
829 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
830 struct extent_ptr_decoded *p)
832 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
833 struct bch_extent_crc_unpacked crc =
834 bch2_extent_crc_unpack(&k->k, NULL);
835 union bch_extent_entry *pos;
837 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
842 bkey_for_each_crc(&k->k, ptrs, crc, pos)
843 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
844 pos = extent_entry_next(pos);
848 bch2_extent_crc_append(k, p->crc);
849 pos = bkey_val_end(bkey_i_to_s(k));
851 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
852 __extent_entry_insert(k, pos, to_entry(&p->ptr));
855 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
856 __extent_entry_insert(k, pos, to_entry(&p->ec));
860 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
861 union bch_extent_entry *entry)
863 union bch_extent_entry *i = ptrs.start;
868 while (extent_entry_next(i) != entry)
869 i = extent_entry_next(i);
873 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
874 struct bch_extent_ptr *ptr)
876 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
877 union bch_extent_entry *dst, *src, *prev;
878 bool drop_crc = true;
880 EBUG_ON(ptr < &ptrs.start->ptr ||
881 ptr >= &ptrs.end->ptr);
882 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
884 src = extent_entry_next(to_entry(ptr));
885 if (src != ptrs.end &&
886 !extent_entry_is_crc(src))
890 while ((prev = extent_entry_prev(ptrs, dst))) {
891 if (extent_entry_is_ptr(prev))
894 if (extent_entry_is_crc(prev)) {
903 memmove_u64s_down(dst, src,
904 (u64 *) ptrs.end - (u64 *) src);
905 k.k->u64s -= (u64 *) src - (u64 *) dst;
910 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
912 struct bch_extent_ptr *ptr;
914 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
917 const struct bch_extent_ptr *
918 bch2_bkey_has_device(struct bkey_s_c k, unsigned dev)
920 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
921 const struct bch_extent_ptr *ptr;
923 bkey_for_each_ptr(ptrs, ptr)
930 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
932 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
933 const struct bch_extent_ptr *ptr;
935 bkey_for_each_ptr(ptrs, ptr)
936 if (bch2_dev_in_target(c, ptr->dev, target) &&
938 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
944 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
945 struct bch_extent_ptr m, u64 offset)
947 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
948 const union bch_extent_entry *entry;
949 struct extent_ptr_decoded p;
951 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
952 if (p.ptr.dev == m.dev &&
953 p.ptr.gen == m.gen &&
954 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
955 (s64) m.offset - offset)
962 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
964 * Returns true if @k should be dropped entirely
966 * For existing keys, only called when btree nodes are being rewritten, not when
967 * they're merely being compacted/resorted in memory.
969 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
971 struct bch_extent_ptr *ptr;
973 bch2_bkey_drop_ptrs(k, ptr,
975 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
977 /* will only happen if all pointers were cached: */
978 if (!bch2_bkey_nr_ptrs(k.s_c))
979 k.k->type = KEY_TYPE_discard;
981 return bkey_whiteout(k.k);
984 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
987 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
988 const union bch_extent_entry *entry;
989 struct bch_extent_crc_unpacked crc;
990 const struct bch_extent_ptr *ptr;
991 const struct bch_extent_stripe_ptr *ec;
995 bkey_extent_entry_for_each(ptrs, entry) {
999 switch (__extent_entry_type(entry)) {
1000 case BCH_EXTENT_ENTRY_ptr:
1001 ptr = entry_to_ptr(entry);
1002 ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
1003 ? bch_dev_bkey_exists(c, ptr->dev)
1006 pr_buf(out, "ptr: %u:%llu gen %u%s%s", ptr->dev,
1007 (u64) ptr->offset, ptr->gen,
1008 ptr->cached ? " cached" : "",
1009 ca && ptr_stale(ca, ptr)
1012 case BCH_EXTENT_ENTRY_crc32:
1013 case BCH_EXTENT_ENTRY_crc64:
1014 case BCH_EXTENT_ENTRY_crc128:
1015 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1017 pr_buf(out, "crc: c_size %u size %u offset %u nonce %u csum %u compress %u",
1018 crc.compressed_size,
1019 crc.uncompressed_size,
1020 crc.offset, crc.nonce,
1022 crc.compression_type);
1024 case BCH_EXTENT_ENTRY_stripe_ptr:
1025 ec = &entry->stripe_ptr;
1027 pr_buf(out, "ec: idx %llu block %u",
1028 (u64) ec->idx, ec->block);
1031 pr_buf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
1039 static const char *extent_ptr_invalid(const struct bch_fs *c,
1041 const struct bch_extent_ptr *ptr,
1042 unsigned size_ondisk,
1045 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1046 const struct bch_extent_ptr *ptr2;
1049 if (!bch2_dev_exists2(c, ptr->dev))
1050 return "pointer to invalid device";
1052 ca = bch_dev_bkey_exists(c, ptr->dev);
1054 return "pointer to invalid device";
1056 bkey_for_each_ptr(ptrs, ptr2)
1057 if (ptr != ptr2 && ptr->dev == ptr2->dev)
1058 return "multiple pointers to same device";
1060 if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
1061 return "offset past end of device";
1063 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
1064 return "offset before first bucket";
1066 if (bucket_remainder(ca, ptr->offset) +
1067 size_ondisk > ca->mi.bucket_size)
1068 return "spans multiple buckets";
1073 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k)
1075 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1076 struct bch_devs_list devs;
1077 const union bch_extent_entry *entry;
1078 struct bch_extent_crc_unpacked crc;
1079 unsigned size_ondisk = k.k->size;
1081 unsigned nonce = UINT_MAX;
1084 if (k.k->type == KEY_TYPE_btree_ptr)
1085 size_ondisk = c->opts.btree_node_size;
1086 if (k.k->type == KEY_TYPE_btree_ptr_v2)
1087 size_ondisk = le16_to_cpu(bkey_s_c_to_btree_ptr_v2(k).v->sectors);
1089 bkey_extent_entry_for_each(ptrs, entry) {
1090 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
1091 return "invalid extent entry type";
1093 if (k.k->type == KEY_TYPE_btree_ptr &&
1094 !extent_entry_is_ptr(entry))
1095 return "has non ptr field";
1097 switch (extent_entry_type(entry)) {
1098 case BCH_EXTENT_ENTRY_ptr:
1099 reason = extent_ptr_invalid(c, k, &entry->ptr,
1100 size_ondisk, false);
1104 case BCH_EXTENT_ENTRY_crc32:
1105 case BCH_EXTENT_ENTRY_crc64:
1106 case BCH_EXTENT_ENTRY_crc128:
1107 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1109 if (crc.offset + crc.live_size >
1110 crc.uncompressed_size)
1111 return "checksum offset + key size > uncompressed size";
1113 size_ondisk = crc.compressed_size;
1115 if (!bch2_checksum_type_valid(c, crc.csum_type))
1116 return "invalid checksum type";
1118 if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR)
1119 return "invalid compression type";
1121 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1122 if (nonce == UINT_MAX)
1123 nonce = crc.offset + crc.nonce;
1124 else if (nonce != crc.offset + crc.nonce)
1125 return "incorrect nonce";
1128 case BCH_EXTENT_ENTRY_stripe_ptr:
1133 devs = bch2_bkey_devs(k);
1134 bubble_sort(devs.devs, devs.nr, u8_cmp);
1135 for (i = 0; i + 1 < devs.nr; i++)
1136 if (devs.devs[i] == devs.devs[i + 1])
1137 return "multiple ptrs to same device";
1142 void bch2_ptr_swab(struct bkey_s k)
1144 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1145 union bch_extent_entry *entry;
1148 for (d = (u64 *) ptrs.start;
1149 d != (u64 *) ptrs.end;
1153 for (entry = ptrs.start;
1155 entry = extent_entry_next(entry)) {
1156 switch (extent_entry_type(entry)) {
1157 case BCH_EXTENT_ENTRY_ptr:
1159 case BCH_EXTENT_ENTRY_crc32:
1160 entry->crc32.csum = swab32(entry->crc32.csum);
1162 case BCH_EXTENT_ENTRY_crc64:
1163 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1164 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1166 case BCH_EXTENT_ENTRY_crc128:
1167 entry->crc128.csum.hi = (__force __le64)
1168 swab64((__force u64) entry->crc128.csum.hi);
1169 entry->crc128.csum.lo = (__force __le64)
1170 swab64((__force u64) entry->crc128.csum.lo);
1172 case BCH_EXTENT_ENTRY_stripe_ptr:
1178 /* Generic extent code: */
1180 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1182 unsigned new_val_u64s = bkey_val_u64s(k.k);
1186 if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
1189 EBUG_ON(bkey_cmp(where, k.k->p) > 0);
1191 sub = where.offset - bkey_start_offset(k.k);
1196 k.k->type = KEY_TYPE_deleted;
1200 switch (k.k->type) {
1201 case KEY_TYPE_extent:
1202 case KEY_TYPE_reflink_v: {
1203 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1204 union bch_extent_entry *entry;
1205 bool seen_crc = false;
1207 bkey_extent_entry_for_each(ptrs, entry) {
1208 switch (extent_entry_type(entry)) {
1209 case BCH_EXTENT_ENTRY_ptr:
1211 entry->ptr.offset += sub;
1213 case BCH_EXTENT_ENTRY_crc32:
1214 entry->crc32.offset += sub;
1216 case BCH_EXTENT_ENTRY_crc64:
1217 entry->crc64.offset += sub;
1219 case BCH_EXTENT_ENTRY_crc128:
1220 entry->crc128.offset += sub;
1222 case BCH_EXTENT_ENTRY_stripe_ptr:
1226 if (extent_entry_is_crc(entry))
1232 case KEY_TYPE_reflink_p: {
1233 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1235 le64_add_cpu(&p.v->idx, sub);
1238 case KEY_TYPE_inline_data:
1239 case KEY_TYPE_indirect_inline_data: {
1240 void *p = bkey_inline_data_p(k);
1241 unsigned bytes = bkey_inline_data_bytes(k.k);
1243 sub = min_t(u64, sub << 9, bytes);
1245 memmove(p, p + sub, bytes - sub);
1247 new_val_u64s -= sub >> 3;
1252 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1253 BUG_ON(val_u64s_delta < 0);
1255 set_bkey_val_u64s(k.k, new_val_u64s);
1256 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1257 return -val_u64s_delta;
1260 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1262 unsigned new_val_u64s = bkey_val_u64s(k.k);
1266 if (bkey_cmp(where, k.k->p) >= 0)
1269 EBUG_ON(bkey_cmp(where, bkey_start_pos(k.k)) < 0);
1271 len = where.offset - bkey_start_offset(k.k);
1277 k.k->type = KEY_TYPE_deleted;
1281 switch (k.k->type) {
1282 case KEY_TYPE_inline_data:
1283 case KEY_TYPE_indirect_inline_data:
1284 new_val_u64s = (bkey_inline_data_offset(k.k) +
1285 min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
1289 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1290 BUG_ON(val_u64s_delta < 0);
1292 set_bkey_val_u64s(k.k, new_val_u64s);
1293 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1294 return -val_u64s_delta;