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 unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
617 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
618 const union bch_extent_entry *entry;
619 struct extent_ptr_decoded p = { 0 };
620 unsigned replicas = 0;
622 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
627 replicas += p.ec.redundancy;
636 static unsigned bch2_extent_ptr_durability(struct bch_fs *c,
637 struct extent_ptr_decoded p)
639 unsigned durability = 0;
645 ca = bch_dev_bkey_exists(c, p.ptr.dev);
647 if (ca->mi.state != BCH_MEMBER_STATE_failed)
648 durability = max_t(unsigned, durability, ca->mi.durability);
651 durability += p.ec.redundancy;
656 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
658 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
659 const union bch_extent_entry *entry;
660 struct extent_ptr_decoded p;
661 unsigned durability = 0;
663 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
664 durability += bch2_extent_ptr_durability(c, p);
669 void bch2_bkey_mark_replicas_cached(struct bch_fs *c, struct bkey_s k,
671 unsigned nr_desired_replicas)
673 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
674 union bch_extent_entry *entry;
675 struct extent_ptr_decoded p;
676 int extra = bch2_bkey_durability(c, k.s_c) - nr_desired_replicas;
678 if (target && extra > 0)
679 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
680 int n = bch2_extent_ptr_durability(c, p);
682 if (n && n <= extra &&
683 !bch2_dev_in_target(c, p.ptr.dev, target)) {
684 entry->ptr.cached = true;
690 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
691 int n = bch2_extent_ptr_durability(c, p);
693 if (n && n <= extra) {
694 entry->ptr.cached = true;
700 void bch2_bkey_extent_entry_drop(struct bkey_i *k, union bch_extent_entry *entry)
702 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
703 union bch_extent_entry *next = extent_entry_next(entry);
705 memmove_u64s(entry, next, (u64 *) end - (u64 *) next);
706 k->k.u64s -= extent_entry_u64s(entry);
709 void bch2_bkey_append_ptr(struct bkey_i *k,
710 struct bch_extent_ptr ptr)
712 EBUG_ON(bch2_bkey_has_device(bkey_i_to_s_c(k), ptr.dev));
715 case KEY_TYPE_btree_ptr:
716 case KEY_TYPE_btree_ptr_v2:
717 case KEY_TYPE_extent:
718 EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
720 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
722 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
732 static inline void __extent_entry_insert(struct bkey_i *k,
733 union bch_extent_entry *dst,
734 union bch_extent_entry *new)
736 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
738 memmove_u64s_up_small((u64 *) dst + extent_entry_u64s(new),
739 dst, (u64 *) end - (u64 *) dst);
740 k->k.u64s += extent_entry_u64s(new);
741 memcpy(dst, new, extent_entry_bytes(new));
744 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
745 struct extent_ptr_decoded *p)
747 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
748 struct bch_extent_crc_unpacked crc =
749 bch2_extent_crc_unpack(&k->k, NULL);
750 union bch_extent_entry *pos;
752 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
757 bkey_for_each_crc(&k->k, ptrs, crc, pos)
758 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
759 pos = extent_entry_next(pos);
763 bch2_extent_crc_append(k, p->crc);
764 pos = bkey_val_end(bkey_i_to_s(k));
766 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
767 __extent_entry_insert(k, pos, to_entry(&p->ptr));
770 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
771 __extent_entry_insert(k, pos, to_entry(&p->ec));
775 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
776 union bch_extent_entry *entry)
778 union bch_extent_entry *i = ptrs.start;
783 while (extent_entry_next(i) != entry)
784 i = extent_entry_next(i);
788 static void extent_entry_drop(struct bkey_s k, union bch_extent_entry *entry)
790 union bch_extent_entry *next = extent_entry_next(entry);
792 /* stripes have ptrs, but their layout doesn't work with this code */
793 BUG_ON(k.k->type == KEY_TYPE_stripe);
795 memmove_u64s_down(entry, next,
796 (u64 *) bkey_val_end(k) - (u64 *) next);
797 k.k->u64s -= (u64 *) next - (u64 *) entry;
801 * Returns pointer to the next entry after the one being dropped:
803 union bch_extent_entry *__bch2_bkey_drop_ptr(struct bkey_s k,
804 struct bch_extent_ptr *ptr)
806 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
807 union bch_extent_entry *entry = to_entry(ptr), *next;
808 union bch_extent_entry *ret = entry;
809 bool drop_crc = true;
811 EBUG_ON(ptr < &ptrs.start->ptr ||
812 ptr >= &ptrs.end->ptr);
813 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
815 for (next = extent_entry_next(entry);
817 next = extent_entry_next(next)) {
818 if (extent_entry_is_crc(next)) {
820 } else if (extent_entry_is_ptr(next)) {
826 extent_entry_drop(k, entry);
828 while ((entry = extent_entry_prev(ptrs, entry))) {
829 if (extent_entry_is_ptr(entry))
832 if ((extent_entry_is_crc(entry) && drop_crc) ||
833 extent_entry_is_stripe_ptr(entry)) {
834 ret = (void *) ret - extent_entry_bytes(entry);
835 extent_entry_drop(k, entry);
842 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
843 struct bch_extent_ptr *ptr)
845 bool have_dirty = bch2_bkey_dirty_devs(k.s_c).nr;
846 union bch_extent_entry *ret =
847 __bch2_bkey_drop_ptr(k, ptr);
850 * If we deleted all the dirty pointers and there's still cached
851 * pointers, we could set the cached pointers to dirty if they're not
852 * stale - but to do that correctly we'd need to grab an open_bucket
853 * reference so that we don't race with bucket reuse:
856 !bch2_bkey_dirty_devs(k.s_c).nr) {
857 k.k->type = KEY_TYPE_error;
858 set_bkey_val_u64s(k.k, 0);
860 } else if (!bch2_bkey_nr_ptrs(k.s_c)) {
861 k.k->type = KEY_TYPE_deleted;
862 set_bkey_val_u64s(k.k, 0);
869 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
871 struct bch_extent_ptr *ptr;
873 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
876 const struct bch_extent_ptr *
877 bch2_bkey_has_device(struct bkey_s_c k, unsigned dev)
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)
889 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
891 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
892 const struct bch_extent_ptr *ptr;
894 bkey_for_each_ptr(ptrs, ptr)
895 if (bch2_dev_in_target(c, ptr->dev, target) &&
897 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
903 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
904 struct bch_extent_ptr m, u64 offset)
906 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
907 const union bch_extent_entry *entry;
908 struct extent_ptr_decoded p;
910 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
911 if (p.ptr.dev == m.dev &&
912 p.ptr.gen == m.gen &&
913 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
914 (s64) m.offset - offset)
921 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
923 * Returns true if @k should be dropped entirely
925 * For existing keys, only called when btree nodes are being rewritten, not when
926 * they're merely being compacted/resorted in memory.
928 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
930 struct bch_extent_ptr *ptr;
932 bch2_bkey_drop_ptrs(k, ptr,
934 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
936 return bkey_deleted(k.k);
939 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
942 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
943 const union bch_extent_entry *entry;
944 struct bch_extent_crc_unpacked crc;
945 const struct bch_extent_ptr *ptr;
946 const struct bch_extent_stripe_ptr *ec;
950 bkey_extent_entry_for_each(ptrs, entry) {
954 switch (__extent_entry_type(entry)) {
955 case BCH_EXTENT_ENTRY_ptr:
956 ptr = entry_to_ptr(entry);
957 ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
958 ? bch_dev_bkey_exists(c, ptr->dev)
961 pr_buf(out, "ptr: %u:%llu gen %u%s%s", ptr->dev,
962 (u64) ptr->offset, ptr->gen,
963 ptr->cached ? " cached" : "",
964 ca && ptr_stale(ca, ptr)
967 case BCH_EXTENT_ENTRY_crc32:
968 case BCH_EXTENT_ENTRY_crc64:
969 case BCH_EXTENT_ENTRY_crc128:
970 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
972 pr_buf(out, "crc: c_size %u size %u offset %u nonce %u csum %s compress %s",
974 crc.uncompressed_size,
975 crc.offset, crc.nonce,
976 bch2_csum_types[crc.csum_type],
977 bch2_compression_types[crc.compression_type]);
979 case BCH_EXTENT_ENTRY_stripe_ptr:
980 ec = &entry->stripe_ptr;
982 pr_buf(out, "ec: idx %llu block %u",
983 (u64) ec->idx, ec->block);
986 pr_buf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
994 static const char *extent_ptr_invalid(const struct bch_fs *c,
996 const struct bch_extent_ptr *ptr,
997 unsigned size_ondisk,
1000 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1001 const struct bch_extent_ptr *ptr2;
1004 if (!bch2_dev_exists2(c, ptr->dev))
1005 return "pointer to invalid device";
1007 ca = bch_dev_bkey_exists(c, ptr->dev);
1009 return "pointer to invalid device";
1011 bkey_for_each_ptr(ptrs, ptr2)
1012 if (ptr != ptr2 && ptr->dev == ptr2->dev)
1013 return "multiple pointers to same device";
1015 if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
1016 return "offset past end of device";
1018 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
1019 return "offset before first bucket";
1021 if (bucket_remainder(ca, ptr->offset) +
1022 size_ondisk > ca->mi.bucket_size)
1023 return "spans multiple buckets";
1028 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k)
1030 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1031 struct bch_devs_list devs;
1032 const union bch_extent_entry *entry;
1033 struct bch_extent_crc_unpacked crc;
1034 unsigned size_ondisk = k.k->size;
1036 unsigned nonce = UINT_MAX;
1039 if (k.k->type == KEY_TYPE_btree_ptr ||
1040 k.k->type == KEY_TYPE_btree_ptr_v2)
1041 size_ondisk = c->opts.btree_node_size;
1043 bkey_extent_entry_for_each(ptrs, entry) {
1044 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
1045 return "invalid extent entry type";
1047 if (k.k->type == KEY_TYPE_btree_ptr &&
1048 !extent_entry_is_ptr(entry))
1049 return "has non ptr field";
1051 switch (extent_entry_type(entry)) {
1052 case BCH_EXTENT_ENTRY_ptr:
1053 reason = extent_ptr_invalid(c, k, &entry->ptr,
1054 size_ondisk, false);
1058 case BCH_EXTENT_ENTRY_crc32:
1059 case BCH_EXTENT_ENTRY_crc64:
1060 case BCH_EXTENT_ENTRY_crc128:
1061 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1063 if (crc.offset + crc.live_size >
1064 crc.uncompressed_size)
1065 return "checksum offset + key size > uncompressed size";
1067 size_ondisk = crc.compressed_size;
1069 if (!bch2_checksum_type_valid(c, crc.csum_type))
1070 return "invalid checksum type";
1072 if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR)
1073 return "invalid compression type";
1075 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1076 if (nonce == UINT_MAX)
1077 nonce = crc.offset + crc.nonce;
1078 else if (nonce != crc.offset + crc.nonce)
1079 return "incorrect nonce";
1082 case BCH_EXTENT_ENTRY_stripe_ptr:
1087 devs = bch2_bkey_devs(k);
1088 bubble_sort(devs.devs, devs.nr, u8_cmp);
1089 for (i = 0; i + 1 < devs.nr; i++)
1090 if (devs.devs[i] == devs.devs[i + 1])
1091 return "multiple ptrs to same device";
1096 void bch2_ptr_swab(struct bkey_s k)
1098 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1099 union bch_extent_entry *entry;
1102 for (d = (u64 *) ptrs.start;
1103 d != (u64 *) ptrs.end;
1107 for (entry = ptrs.start;
1109 entry = extent_entry_next(entry)) {
1110 switch (extent_entry_type(entry)) {
1111 case BCH_EXTENT_ENTRY_ptr:
1113 case BCH_EXTENT_ENTRY_crc32:
1114 entry->crc32.csum = swab32(entry->crc32.csum);
1116 case BCH_EXTENT_ENTRY_crc64:
1117 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1118 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1120 case BCH_EXTENT_ENTRY_crc128:
1121 entry->crc128.csum.hi = (__force __le64)
1122 swab64((__force u64) entry->crc128.csum.hi);
1123 entry->crc128.csum.lo = (__force __le64)
1124 swab64((__force u64) entry->crc128.csum.lo);
1126 case BCH_EXTENT_ENTRY_stripe_ptr:
1132 /* Generic extent code: */
1134 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1136 unsigned new_val_u64s = bkey_val_u64s(k.k);
1140 if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
1143 EBUG_ON(bkey_cmp(where, k.k->p) > 0);
1145 sub = where.offset - bkey_start_offset(k.k);
1150 k.k->type = KEY_TYPE_deleted;
1154 switch (k.k->type) {
1155 case KEY_TYPE_extent:
1156 case KEY_TYPE_reflink_v: {
1157 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1158 union bch_extent_entry *entry;
1159 bool seen_crc = false;
1161 bkey_extent_entry_for_each(ptrs, entry) {
1162 switch (extent_entry_type(entry)) {
1163 case BCH_EXTENT_ENTRY_ptr:
1165 entry->ptr.offset += sub;
1167 case BCH_EXTENT_ENTRY_crc32:
1168 entry->crc32.offset += sub;
1170 case BCH_EXTENT_ENTRY_crc64:
1171 entry->crc64.offset += sub;
1173 case BCH_EXTENT_ENTRY_crc128:
1174 entry->crc128.offset += sub;
1176 case BCH_EXTENT_ENTRY_stripe_ptr:
1180 if (extent_entry_is_crc(entry))
1186 case KEY_TYPE_reflink_p: {
1187 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1189 le64_add_cpu(&p.v->idx, sub);
1192 case KEY_TYPE_inline_data:
1193 case KEY_TYPE_indirect_inline_data: {
1194 void *p = bkey_inline_data_p(k);
1195 unsigned bytes = bkey_inline_data_bytes(k.k);
1197 sub = min_t(u64, sub << 9, bytes);
1199 memmove(p, p + sub, bytes - sub);
1201 new_val_u64s -= sub >> 3;
1206 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1207 BUG_ON(val_u64s_delta < 0);
1209 set_bkey_val_u64s(k.k, new_val_u64s);
1210 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1211 return -val_u64s_delta;
1214 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1216 unsigned new_val_u64s = bkey_val_u64s(k.k);
1220 if (bkey_cmp(where, k.k->p) >= 0)
1223 EBUG_ON(bkey_cmp(where, bkey_start_pos(k.k)) < 0);
1225 len = where.offset - bkey_start_offset(k.k);
1227 k.k->p.offset = where.offset;
1231 k.k->type = KEY_TYPE_deleted;
1235 switch (k.k->type) {
1236 case KEY_TYPE_inline_data:
1237 case KEY_TYPE_indirect_inline_data:
1238 new_val_u64s = (bkey_inline_data_offset(k.k) +
1239 min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
1243 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1244 BUG_ON(val_u64s_delta < 0);
1246 set_bkey_val_u64s(k.k, new_val_u64s);
1247 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1248 return -val_u64s_delta;