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"
27 #include <trace/events/bcachefs.h>
29 static unsigned bch2_crc_field_size_max[] = {
30 [BCH_EXTENT_ENTRY_crc32] = CRC32_SIZE_MAX,
31 [BCH_EXTENT_ENTRY_crc64] = CRC64_SIZE_MAX,
32 [BCH_EXTENT_ENTRY_crc128] = CRC128_SIZE_MAX,
35 static void bch2_extent_crc_pack(union bch_extent_crc *,
36 struct bch_extent_crc_unpacked,
37 enum bch_extent_entry_type);
39 static struct bch_dev_io_failures *dev_io_failures(struct bch_io_failures *f,
42 struct bch_dev_io_failures *i;
44 for (i = f->devs; i < f->devs + f->nr; i++)
51 void bch2_mark_io_failure(struct bch_io_failures *failed,
52 struct extent_ptr_decoded *p)
54 struct bch_dev_io_failures *f = dev_io_failures(failed, p->ptr.dev);
57 BUG_ON(failed->nr >= ARRAY_SIZE(failed->devs));
59 f = &failed->devs[failed->nr++];
64 } else if (p->idx != f->idx) {
74 * returns true if p1 is better than p2:
76 static inline bool ptr_better(struct bch_fs *c,
77 const struct extent_ptr_decoded p1,
78 const struct extent_ptr_decoded p2)
80 if (likely(!p1.idx && !p2.idx)) {
81 struct bch_dev *dev1 = bch_dev_bkey_exists(c, p1.ptr.dev);
82 struct bch_dev *dev2 = bch_dev_bkey_exists(c, p2.ptr.dev);
84 u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
85 u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
87 /* Pick at random, biased in favor of the faster device: */
89 return bch2_rand_range(l1 + l2) > l1;
92 if (bch2_force_reconstruct_read)
93 return p1.idx > p2.idx;
95 return p1.idx < p2.idx;
99 * This picks a non-stale pointer, preferably from a device other than @avoid.
100 * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to
101 * other devices, it will still pick a pointer from avoid.
103 int bch2_bkey_pick_read_device(struct bch_fs *c, struct bkey_s_c k,
104 struct bch_io_failures *failed,
105 struct extent_ptr_decoded *pick)
107 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
108 const union bch_extent_entry *entry;
109 struct extent_ptr_decoded p;
110 struct bch_dev_io_failures *f;
114 if (k.k->type == KEY_TYPE_error)
117 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
118 ca = bch_dev_bkey_exists(c, p.ptr.dev);
121 * If there are any dirty pointers it's an error if we can't
124 if (!ret && !p.ptr.cached)
127 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
130 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
132 p.idx = f->nr_failed < f->nr_retries
137 !bch2_dev_is_readable(ca))
140 if (bch2_force_reconstruct_read &&
144 if (p.idx >= (unsigned) p.has_ec + 1)
147 if (ret > 0 && !ptr_better(c, p, *pick))
157 /* KEY_TYPE_btree_ptr: */
159 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
161 if (bkey_val_u64s(k.k) > BCH_REPLICAS_MAX)
162 return "value too big";
164 return bch2_bkey_ptrs_invalid(c, k);
167 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
170 bch2_bkey_ptrs_to_text(out, c, k);
173 const char *bch2_btree_ptr_v2_invalid(const struct bch_fs *c, struct bkey_s_c k)
175 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
177 if (bkey_val_bytes(k.k) <= sizeof(*bp.v))
178 return "value too small";
180 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
181 return "value too big";
183 if (c->sb.version < bcachefs_metadata_version_snapshot &&
184 bp.v->min_key.snapshot)
185 return "invalid min_key.snapshot";
187 return bch2_bkey_ptrs_invalid(c, k);
190 void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c,
193 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
195 pr_buf(out, "seq %llx written %u min_key %s",
196 le64_to_cpu(bp.v->seq),
197 le16_to_cpu(bp.v->sectors_written),
198 BTREE_PTR_RANGE_UPDATED(bp.v) ? "R " : "");
200 bch2_bpos_to_text(out, bp.v->min_key);
202 bch2_bkey_ptrs_to_text(out, c, k);
205 void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version,
206 unsigned big_endian, int write,
209 struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k);
211 compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key);
213 if (version < bcachefs_metadata_version_inode_btree_change &&
214 btree_node_type_is_extents(btree_id) &&
215 bkey_cmp(bp.v->min_key, POS_MIN))
216 bp.v->min_key = write
217 ? bpos_nosnap_predecessor(bp.v->min_key)
218 : bpos_nosnap_successor(bp.v->min_key);
221 /* KEY_TYPE_extent: */
223 const char *bch2_extent_invalid(const struct bch_fs *c, struct bkey_s_c k)
225 return bch2_bkey_ptrs_invalid(c, k);
228 void bch2_extent_to_text(struct printbuf *out, struct bch_fs *c,
231 bch2_bkey_ptrs_to_text(out, c, k);
234 bool bch2_extent_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
236 struct bkey_ptrs l_ptrs = bch2_bkey_ptrs(l);
237 struct bkey_ptrs_c r_ptrs = bch2_bkey_ptrs_c(r);
238 union bch_extent_entry *en_l;
239 const union bch_extent_entry *en_r;
240 struct extent_ptr_decoded lp, rp;
246 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
247 if (extent_entry_type(en_l) != extent_entry_type(en_r))
250 en_l = extent_entry_next(en_l);
251 en_r = extent_entry_next(en_r);
254 if (en_l < l_ptrs.end || en_r < r_ptrs.end)
259 lp.crc = bch2_extent_crc_unpack(l.k, NULL);
260 rp.crc = bch2_extent_crc_unpack(r.k, NULL);
262 while (__bkey_ptr_next_decode(l.k, l_ptrs.end, lp, en_l) &&
263 __bkey_ptr_next_decode(r.k, r_ptrs.end, rp, en_r)) {
264 if (lp.ptr.offset + lp.crc.offset + lp.crc.live_size !=
265 rp.ptr.offset + rp.crc.offset ||
266 lp.ptr.dev != rp.ptr.dev ||
267 lp.ptr.gen != rp.ptr.gen ||
268 lp.has_ec != rp.has_ec)
271 /* Extents may not straddle buckets: */
272 ca = bch_dev_bkey_exists(c, lp.ptr.dev);
273 if (PTR_BUCKET_NR(ca, &lp.ptr) != PTR_BUCKET_NR(ca, &rp.ptr))
276 if (lp.has_ec != rp.has_ec ||
278 (lp.ec.block != rp.ec.block ||
279 lp.ec.redundancy != rp.ec.redundancy ||
280 lp.ec.idx != rp.ec.idx)))
283 if (lp.crc.compression_type != rp.crc.compression_type ||
284 lp.crc.nonce != rp.crc.nonce)
287 if (lp.crc.offset + lp.crc.live_size + rp.crc.live_size <=
288 lp.crc.uncompressed_size) {
289 /* can use left extent's crc entry */
290 } else if (lp.crc.live_size <= rp.crc.offset ) {
291 /* can use right extent's crc entry */
293 /* check if checksums can be merged: */
294 if (lp.crc.csum_type != rp.crc.csum_type ||
295 lp.crc.nonce != rp.crc.nonce ||
296 crc_is_compressed(lp.crc) ||
297 !bch2_checksum_mergeable(lp.crc.csum_type))
300 if (lp.crc.offset + lp.crc.live_size != lp.crc.compressed_size ||
304 if (lp.crc.csum_type &&
305 lp.crc.uncompressed_size +
306 rp.crc.uncompressed_size > c->sb.encoded_extent_max)
309 if (lp.crc.uncompressed_size + rp.crc.uncompressed_size >
310 bch2_crc_field_size_max[extent_entry_type(en_l)])
314 en_l = extent_entry_next(en_l);
315 en_r = extent_entry_next(en_r);
318 use_right_ptr = false;
321 while (en_l < l_ptrs.end) {
322 if (extent_entry_type(en_l) == BCH_EXTENT_ENTRY_ptr &&
324 en_l->ptr = en_r->ptr;
326 if (extent_entry_is_crc(en_l)) {
327 struct bch_extent_crc_unpacked crc_l =
328 bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
329 struct bch_extent_crc_unpacked crc_r =
330 bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
332 use_right_ptr = false;
334 if (crc_l.offset + crc_l.live_size + crc_r.live_size <=
335 crc_l.uncompressed_size) {
336 /* can use left extent's crc entry */
337 } else if (crc_l.live_size <= crc_r.offset ) {
338 /* can use right extent's crc entry */
339 crc_r.offset -= crc_l.live_size;
340 bch2_extent_crc_pack(entry_to_crc(en_l), crc_r,
341 extent_entry_type(en_l));
342 use_right_ptr = true;
344 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
347 crc_r.uncompressed_size << 9);
349 crc_l.uncompressed_size += crc_r.uncompressed_size;
350 crc_l.compressed_size += crc_r.compressed_size;
351 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l,
352 extent_entry_type(en_l));
356 en_l = extent_entry_next(en_l);
357 en_r = extent_entry_next(en_r);
360 bch2_key_resize(l.k, l.k->size + r.k->size);
364 /* KEY_TYPE_reservation: */
366 const char *bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k)
368 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
370 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation))
371 return "incorrect value size";
373 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX)
374 return "invalid nr_replicas";
379 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
382 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
384 pr_buf(out, "generation %u replicas %u",
385 le32_to_cpu(r.v->generation),
389 bool bch2_reservation_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r)
391 struct bkey_s_reservation l = bkey_s_to_reservation(_l);
392 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(_r);
394 if (l.v->generation != r.v->generation ||
395 l.v->nr_replicas != r.v->nr_replicas)
398 bch2_key_resize(l.k, l.k->size + r.k->size);
402 /* Extent checksum entries: */
404 /* returns true if not equal */
405 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
406 struct bch_extent_crc_unpacked r)
408 return (l.csum_type != r.csum_type ||
409 l.compression_type != r.compression_type ||
410 l.compressed_size != r.compressed_size ||
411 l.uncompressed_size != r.uncompressed_size ||
412 l.offset != r.offset ||
413 l.live_size != r.live_size ||
414 l.nonce != r.nonce ||
415 bch2_crc_cmp(l.csum, r.csum));
418 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
419 struct bch_extent_crc_unpacked n)
421 return !crc_is_compressed(u) &&
423 u.uncompressed_size > u.live_size &&
424 bch2_csum_type_is_encryption(u.csum_type) ==
425 bch2_csum_type_is_encryption(n.csum_type);
428 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
429 struct bch_extent_crc_unpacked n)
431 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
432 struct bch_extent_crc_unpacked crc;
433 const union bch_extent_entry *i;
438 bkey_for_each_crc(k.k, ptrs, crc, i)
439 if (can_narrow_crc(crc, n))
446 * We're writing another replica for this extent, so while we've got the data in
447 * memory we'll be computing a new checksum for the currently live data.
449 * If there are other replicas we aren't moving, and they are checksummed but
450 * not compressed, we can modify them to point to only the data that is
451 * currently live (so that readers won't have to bounce) while we've got the
454 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
456 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
457 struct bch_extent_crc_unpacked u;
458 struct extent_ptr_decoded p;
459 union bch_extent_entry *i;
462 /* Find a checksum entry that covers only live data: */
464 bkey_for_each_crc(&k->k, ptrs, u, i)
465 if (!crc_is_compressed(u) &&
467 u.live_size == u.uncompressed_size) {
474 BUG_ON(crc_is_compressed(n));
476 BUG_ON(n.live_size != k->k.size);
478 restart_narrow_pointers:
479 ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
481 bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
482 if (can_narrow_crc(p.crc, n)) {
483 bch2_bkey_drop_ptr(bkey_i_to_s(k), &i->ptr);
484 p.ptr.offset += p.crc.offset;
486 bch2_extent_ptr_decoded_append(k, &p);
488 goto restart_narrow_pointers;
494 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
495 struct bch_extent_crc_unpacked src,
496 enum bch_extent_entry_type type)
498 #define set_common_fields(_dst, _src) \
499 _dst.type = 1 << type; \
500 _dst.csum_type = _src.csum_type, \
501 _dst.compression_type = _src.compression_type, \
502 _dst._compressed_size = _src.compressed_size - 1, \
503 _dst._uncompressed_size = _src.uncompressed_size - 1, \
504 _dst.offset = _src.offset
507 case BCH_EXTENT_ENTRY_crc32:
508 set_common_fields(dst->crc32, src);
509 dst->crc32.csum = *((__le32 *) &src.csum.lo);
511 case BCH_EXTENT_ENTRY_crc64:
512 set_common_fields(dst->crc64, src);
513 dst->crc64.nonce = src.nonce;
514 dst->crc64.csum_lo = src.csum.lo;
515 dst->crc64.csum_hi = *((__le16 *) &src.csum.hi);
517 case BCH_EXTENT_ENTRY_crc128:
518 set_common_fields(dst->crc128, src);
519 dst->crc128.nonce = src.nonce;
520 dst->crc128.csum = src.csum;
525 #undef set_common_fields
528 void bch2_extent_crc_append(struct bkey_i *k,
529 struct bch_extent_crc_unpacked new)
531 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
532 union bch_extent_crc *crc = (void *) ptrs.end;
533 enum bch_extent_entry_type type;
535 if (bch_crc_bytes[new.csum_type] <= 4 &&
536 new.uncompressed_size <= CRC32_SIZE_MAX &&
537 new.nonce <= CRC32_NONCE_MAX)
538 type = BCH_EXTENT_ENTRY_crc32;
539 else if (bch_crc_bytes[new.csum_type] <= 10 &&
540 new.uncompressed_size <= CRC64_SIZE_MAX &&
541 new.nonce <= CRC64_NONCE_MAX)
542 type = BCH_EXTENT_ENTRY_crc64;
543 else if (bch_crc_bytes[new.csum_type] <= 16 &&
544 new.uncompressed_size <= CRC128_SIZE_MAX &&
545 new.nonce <= CRC128_NONCE_MAX)
546 type = BCH_EXTENT_ENTRY_crc128;
550 bch2_extent_crc_pack(crc, new, type);
552 k->k.u64s += extent_entry_u64s(ptrs.end);
554 EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
557 /* Generic code for keys with pointers: */
559 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
561 return bch2_bkey_devs(k).nr;
564 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
566 return k.k->type == KEY_TYPE_reservation
567 ? bkey_s_c_to_reservation(k).v->nr_replicas
568 : bch2_bkey_dirty_devs(k).nr;
571 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
575 if (k.k->type == KEY_TYPE_reservation) {
576 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
578 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
579 const union bch_extent_entry *entry;
580 struct extent_ptr_decoded p;
582 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
583 ret += !p.ptr.cached && !crc_is_compressed(p.crc);
589 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
591 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
592 const union bch_extent_entry *entry;
593 struct extent_ptr_decoded p;
596 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
597 if (!p.ptr.cached && crc_is_compressed(p.crc))
598 ret += p.crc.compressed_size;
603 bool bch2_bkey_is_incompressible(struct bkey_s_c k)
605 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
606 const union bch_extent_entry *entry;
607 struct bch_extent_crc_unpacked crc;
609 bkey_for_each_crc(k.k, ptrs, crc, entry)
610 if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
615 bool bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size,
616 unsigned nr_replicas, bool compressed)
618 struct btree_trans trans;
619 struct btree_iter *iter;
620 struct bpos end = pos;
627 bch2_trans_init(&trans, c, 0, 0);
629 for_each_btree_key(&trans, iter, BTREE_ID_extents, pos,
630 BTREE_ITER_SLOTS, k, err) {
631 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
634 if (nr_replicas > bch2_bkey_replicas(c, k) ||
635 (!compressed && bch2_bkey_sectors_compressed(k))) {
640 bch2_trans_iter_put(&trans, iter);
642 bch2_trans_exit(&trans);
647 unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
649 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
650 const union bch_extent_entry *entry;
651 struct extent_ptr_decoded p = { 0 };
652 unsigned replicas = 0;
654 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
659 replicas += p.ec.redundancy;
668 static unsigned bch2_extent_ptr_durability(struct bch_fs *c,
669 struct extent_ptr_decoded p)
671 unsigned durability = 0;
677 ca = bch_dev_bkey_exists(c, p.ptr.dev);
679 if (ca->mi.state != BCH_MEMBER_STATE_failed)
680 durability = max_t(unsigned, durability, ca->mi.durability);
683 durability += p.ec.redundancy;
688 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
690 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
691 const union bch_extent_entry *entry;
692 struct extent_ptr_decoded p;
693 unsigned durability = 0;
695 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
696 durability += bch2_extent_ptr_durability(c, p);
701 void bch2_bkey_mark_replicas_cached(struct bch_fs *c, struct bkey_s k,
703 unsigned nr_desired_replicas)
705 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
706 union bch_extent_entry *entry;
707 struct extent_ptr_decoded p;
708 int extra = bch2_bkey_durability(c, k.s_c) - nr_desired_replicas;
710 if (target && extra > 0)
711 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
712 int n = bch2_extent_ptr_durability(c, p);
714 if (n && n <= extra &&
715 !bch2_dev_in_target(c, p.ptr.dev, target)) {
716 entry->ptr.cached = true;
722 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
723 int n = bch2_extent_ptr_durability(c, p);
725 if (n && n <= extra) {
726 entry->ptr.cached = true;
732 void bch2_bkey_extent_entry_drop(struct bkey_i *k, union bch_extent_entry *entry)
734 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
735 union bch_extent_entry *next = extent_entry_next(entry);
737 memmove_u64s(entry, next, (u64 *) end - (u64 *) next);
738 k->k.u64s -= extent_entry_u64s(entry);
741 void bch2_bkey_append_ptr(struct bkey_i *k,
742 struct bch_extent_ptr ptr)
744 EBUG_ON(bch2_bkey_has_device(bkey_i_to_s_c(k), ptr.dev));
747 case KEY_TYPE_btree_ptr:
748 case KEY_TYPE_btree_ptr_v2:
749 case KEY_TYPE_extent:
750 EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
752 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
754 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
764 static inline void __extent_entry_insert(struct bkey_i *k,
765 union bch_extent_entry *dst,
766 union bch_extent_entry *new)
768 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
770 memmove_u64s_up_small((u64 *) dst + extent_entry_u64s(new),
771 dst, (u64 *) end - (u64 *) dst);
772 k->k.u64s += extent_entry_u64s(new);
773 memcpy(dst, new, extent_entry_bytes(new));
776 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
777 struct extent_ptr_decoded *p)
779 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
780 struct bch_extent_crc_unpacked crc =
781 bch2_extent_crc_unpack(&k->k, NULL);
782 union bch_extent_entry *pos;
784 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
789 bkey_for_each_crc(&k->k, ptrs, crc, pos)
790 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
791 pos = extent_entry_next(pos);
795 bch2_extent_crc_append(k, p->crc);
796 pos = bkey_val_end(bkey_i_to_s(k));
798 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
799 __extent_entry_insert(k, pos, to_entry(&p->ptr));
802 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
803 __extent_entry_insert(k, pos, to_entry(&p->ec));
807 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
808 union bch_extent_entry *entry)
810 union bch_extent_entry *i = ptrs.start;
815 while (extent_entry_next(i) != entry)
816 i = extent_entry_next(i);
820 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
821 struct bch_extent_ptr *ptr)
823 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
824 union bch_extent_entry *dst, *src, *prev;
825 bool drop_crc = true;
827 EBUG_ON(ptr < &ptrs.start->ptr ||
828 ptr >= &ptrs.end->ptr);
829 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
831 src = extent_entry_next(to_entry(ptr));
832 if (src != ptrs.end &&
833 !extent_entry_is_crc(src))
837 while ((prev = extent_entry_prev(ptrs, dst))) {
838 if (extent_entry_is_ptr(prev))
841 if (extent_entry_is_crc(prev)) {
850 memmove_u64s_down(dst, src,
851 (u64 *) ptrs.end - (u64 *) src);
852 k.k->u64s -= (u64 *) src - (u64 *) dst;
857 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
859 struct bch_extent_ptr *ptr;
861 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
864 const struct bch_extent_ptr *
865 bch2_bkey_has_device(struct bkey_s_c k, unsigned dev)
867 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
868 const struct bch_extent_ptr *ptr;
870 bkey_for_each_ptr(ptrs, ptr)
877 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
879 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
880 const struct bch_extent_ptr *ptr;
882 bkey_for_each_ptr(ptrs, ptr)
883 if (bch2_dev_in_target(c, ptr->dev, target) &&
885 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
891 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
892 struct bch_extent_ptr m, u64 offset)
894 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
895 const union bch_extent_entry *entry;
896 struct extent_ptr_decoded p;
898 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
899 if (p.ptr.dev == m.dev &&
900 p.ptr.gen == m.gen &&
901 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
902 (s64) m.offset - offset)
909 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
911 * Returns true if @k should be dropped entirely
913 * For existing keys, only called when btree nodes are being rewritten, not when
914 * they're merely being compacted/resorted in memory.
916 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
918 struct bch_extent_ptr *ptr;
920 bch2_bkey_drop_ptrs(k, ptr,
922 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
924 /* will only happen if all pointers were cached: */
925 if (!bch2_bkey_nr_ptrs(k.s_c))
926 k.k->type = KEY_TYPE_deleted;
928 return bkey_deleted(k.k);
931 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
934 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
935 const union bch_extent_entry *entry;
936 struct bch_extent_crc_unpacked crc;
937 const struct bch_extent_ptr *ptr;
938 const struct bch_extent_stripe_ptr *ec;
942 bkey_extent_entry_for_each(ptrs, entry) {
946 switch (__extent_entry_type(entry)) {
947 case BCH_EXTENT_ENTRY_ptr:
948 ptr = entry_to_ptr(entry);
949 ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
950 ? bch_dev_bkey_exists(c, ptr->dev)
953 pr_buf(out, "ptr: %u:%llu gen %u%s%s", ptr->dev,
954 (u64) ptr->offset, ptr->gen,
955 ptr->cached ? " cached" : "",
956 ca && ptr_stale(ca, ptr)
959 case BCH_EXTENT_ENTRY_crc32:
960 case BCH_EXTENT_ENTRY_crc64:
961 case BCH_EXTENT_ENTRY_crc128:
962 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
964 pr_buf(out, "crc: c_size %u size %u offset %u nonce %u csum %u compress %u",
966 crc.uncompressed_size,
967 crc.offset, crc.nonce,
969 crc.compression_type);
971 case BCH_EXTENT_ENTRY_stripe_ptr:
972 ec = &entry->stripe_ptr;
974 pr_buf(out, "ec: idx %llu block %u",
975 (u64) ec->idx, ec->block);
978 pr_buf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
986 static const char *extent_ptr_invalid(const struct bch_fs *c,
988 const struct bch_extent_ptr *ptr,
989 unsigned size_ondisk,
992 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
993 const struct bch_extent_ptr *ptr2;
996 if (!bch2_dev_exists2(c, ptr->dev))
997 return "pointer to invalid device";
999 ca = bch_dev_bkey_exists(c, ptr->dev);
1001 return "pointer to invalid device";
1003 bkey_for_each_ptr(ptrs, ptr2)
1004 if (ptr != ptr2 && ptr->dev == ptr2->dev)
1005 return "multiple pointers to same device";
1007 if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
1008 return "offset past end of device";
1010 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
1011 return "offset before first bucket";
1013 if (bucket_remainder(ca, ptr->offset) +
1014 size_ondisk > ca->mi.bucket_size)
1015 return "spans multiple buckets";
1020 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k)
1022 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1023 struct bch_devs_list devs;
1024 const union bch_extent_entry *entry;
1025 struct bch_extent_crc_unpacked crc;
1026 unsigned size_ondisk = k.k->size;
1028 unsigned nonce = UINT_MAX;
1031 if (k.k->type == KEY_TYPE_btree_ptr ||
1032 k.k->type == KEY_TYPE_btree_ptr_v2)
1033 size_ondisk = c->opts.btree_node_size;
1035 bkey_extent_entry_for_each(ptrs, entry) {
1036 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
1037 return "invalid extent entry type";
1039 if (k.k->type == KEY_TYPE_btree_ptr &&
1040 !extent_entry_is_ptr(entry))
1041 return "has non ptr field";
1043 switch (extent_entry_type(entry)) {
1044 case BCH_EXTENT_ENTRY_ptr:
1045 reason = extent_ptr_invalid(c, k, &entry->ptr,
1046 size_ondisk, false);
1050 case BCH_EXTENT_ENTRY_crc32:
1051 case BCH_EXTENT_ENTRY_crc64:
1052 case BCH_EXTENT_ENTRY_crc128:
1053 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1055 if (crc.offset + crc.live_size >
1056 crc.uncompressed_size)
1057 return "checksum offset + key size > uncompressed size";
1059 size_ondisk = crc.compressed_size;
1061 if (!bch2_checksum_type_valid(c, crc.csum_type))
1062 return "invalid checksum type";
1064 if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR)
1065 return "invalid compression type";
1067 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1068 if (nonce == UINT_MAX)
1069 nonce = crc.offset + crc.nonce;
1070 else if (nonce != crc.offset + crc.nonce)
1071 return "incorrect nonce";
1074 case BCH_EXTENT_ENTRY_stripe_ptr:
1079 devs = bch2_bkey_devs(k);
1080 bubble_sort(devs.devs, devs.nr, u8_cmp);
1081 for (i = 0; i + 1 < devs.nr; i++)
1082 if (devs.devs[i] == devs.devs[i + 1])
1083 return "multiple ptrs to same device";
1088 void bch2_ptr_swab(struct bkey_s k)
1090 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1091 union bch_extent_entry *entry;
1094 for (d = (u64 *) ptrs.start;
1095 d != (u64 *) ptrs.end;
1099 for (entry = ptrs.start;
1101 entry = extent_entry_next(entry)) {
1102 switch (extent_entry_type(entry)) {
1103 case BCH_EXTENT_ENTRY_ptr:
1105 case BCH_EXTENT_ENTRY_crc32:
1106 entry->crc32.csum = swab32(entry->crc32.csum);
1108 case BCH_EXTENT_ENTRY_crc64:
1109 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1110 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1112 case BCH_EXTENT_ENTRY_crc128:
1113 entry->crc128.csum.hi = (__force __le64)
1114 swab64((__force u64) entry->crc128.csum.hi);
1115 entry->crc128.csum.lo = (__force __le64)
1116 swab64((__force u64) entry->crc128.csum.lo);
1118 case BCH_EXTENT_ENTRY_stripe_ptr:
1124 /* Generic extent code: */
1126 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1128 unsigned new_val_u64s = bkey_val_u64s(k.k);
1132 if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
1135 EBUG_ON(bkey_cmp(where, k.k->p) > 0);
1137 sub = where.offset - bkey_start_offset(k.k);
1142 k.k->type = KEY_TYPE_deleted;
1146 switch (k.k->type) {
1147 case KEY_TYPE_extent:
1148 case KEY_TYPE_reflink_v: {
1149 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1150 union bch_extent_entry *entry;
1151 bool seen_crc = false;
1153 bkey_extent_entry_for_each(ptrs, entry) {
1154 switch (extent_entry_type(entry)) {
1155 case BCH_EXTENT_ENTRY_ptr:
1157 entry->ptr.offset += sub;
1159 case BCH_EXTENT_ENTRY_crc32:
1160 entry->crc32.offset += sub;
1162 case BCH_EXTENT_ENTRY_crc64:
1163 entry->crc64.offset += sub;
1165 case BCH_EXTENT_ENTRY_crc128:
1166 entry->crc128.offset += sub;
1168 case BCH_EXTENT_ENTRY_stripe_ptr:
1172 if (extent_entry_is_crc(entry))
1178 case KEY_TYPE_reflink_p: {
1179 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1181 le64_add_cpu(&p.v->idx, sub);
1184 case KEY_TYPE_inline_data:
1185 case KEY_TYPE_indirect_inline_data: {
1186 void *p = bkey_inline_data_p(k);
1187 unsigned bytes = bkey_inline_data_bytes(k.k);
1189 sub = min_t(u64, sub << 9, bytes);
1191 memmove(p, p + sub, bytes - sub);
1193 new_val_u64s -= sub >> 3;
1198 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1199 BUG_ON(val_u64s_delta < 0);
1201 set_bkey_val_u64s(k.k, new_val_u64s);
1202 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1203 return -val_u64s_delta;
1206 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1208 unsigned new_val_u64s = bkey_val_u64s(k.k);
1212 if (bkey_cmp(where, k.k->p) >= 0)
1215 EBUG_ON(bkey_cmp(where, bkey_start_pos(k.k)) < 0);
1217 len = where.offset - bkey_start_offset(k.k);
1219 k.k->p.offset = where.offset;
1223 k.k->type = KEY_TYPE_deleted;
1227 switch (k.k->type) {
1228 case KEY_TYPE_inline_data:
1229 case KEY_TYPE_indirect_inline_data:
1230 new_val_u64s = (bkey_inline_data_offset(k.k) +
1231 min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
1235 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1236 BUG_ON(val_u64s_delta < 0);
1238 set_bkey_val_u64s(k.k, new_val_u64s);
1239 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1240 return -val_u64s_delta;