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 union bch_extent_entry *__bch2_bkey_drop_ptr(struct bkey_s, struct bch_extent_ptr *);
31 static unsigned bch2_crc_field_size_max[] = {
32 [BCH_EXTENT_ENTRY_crc32] = CRC32_SIZE_MAX,
33 [BCH_EXTENT_ENTRY_crc64] = CRC64_SIZE_MAX,
34 [BCH_EXTENT_ENTRY_crc128] = CRC128_SIZE_MAX,
37 static void bch2_extent_crc_pack(union bch_extent_crc *,
38 struct bch_extent_crc_unpacked,
39 enum bch_extent_entry_type);
41 static struct bch_dev_io_failures *dev_io_failures(struct bch_io_failures *f,
44 struct bch_dev_io_failures *i;
46 for (i = f->devs; i < f->devs + f->nr; i++)
53 void bch2_mark_io_failure(struct bch_io_failures *failed,
54 struct extent_ptr_decoded *p)
56 struct bch_dev_io_failures *f = dev_io_failures(failed, p->ptr.dev);
59 BUG_ON(failed->nr >= ARRAY_SIZE(failed->devs));
61 f = &failed->devs[failed->nr++];
66 } else if (p->idx != f->idx) {
76 * returns true if p1 is better than p2:
78 static inline bool ptr_better(struct bch_fs *c,
79 const struct extent_ptr_decoded p1,
80 const struct extent_ptr_decoded p2)
82 if (likely(!p1.idx && !p2.idx)) {
83 struct bch_dev *dev1 = bch_dev_bkey_exists(c, p1.ptr.dev);
84 struct bch_dev *dev2 = bch_dev_bkey_exists(c, p2.ptr.dev);
86 u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
87 u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
89 /* Pick at random, biased in favor of the faster device: */
91 return bch2_rand_range(l1 + l2) > l1;
94 if (bch2_force_reconstruct_read)
95 return p1.idx > p2.idx;
97 return p1.idx < p2.idx;
101 * This picks a non-stale pointer, preferably from a device other than @avoid.
102 * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to
103 * other devices, it will still pick a pointer from avoid.
105 int bch2_bkey_pick_read_device(struct bch_fs *c, struct bkey_s_c k,
106 struct bch_io_failures *failed,
107 struct extent_ptr_decoded *pick)
109 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
110 const union bch_extent_entry *entry;
111 struct extent_ptr_decoded p;
112 struct bch_dev_io_failures *f;
116 if (k.k->type == KEY_TYPE_error)
119 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
120 ca = bch_dev_bkey_exists(c, p.ptr.dev);
123 * If there are any dirty pointers it's an error if we can't
126 if (!ret && !p.ptr.cached)
129 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
132 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
134 p.idx = f->nr_failed < f->nr_retries
139 !bch2_dev_is_readable(ca))
142 if (bch2_force_reconstruct_read &&
146 if (p.idx >= (unsigned) p.has_ec + 1)
149 if (ret > 0 && !ptr_better(c, p, *pick))
159 /* KEY_TYPE_btree_ptr: */
161 int bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k,
162 int rw, struct printbuf *err)
164 if (bkey_val_u64s(k.k) > BCH_REPLICAS_MAX) {
165 prt_printf(err, "value too big (%zu > %u)",
166 bkey_val_u64s(k.k), BCH_REPLICAS_MAX);
170 return bch2_bkey_ptrs_invalid(c, k, rw, err);
173 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
176 bch2_bkey_ptrs_to_text(out, c, k);
179 int bch2_btree_ptr_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
180 int rw, struct printbuf *err)
182 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
184 if (bkey_val_bytes(k.k) <= sizeof(*bp.v)) {
185 prt_printf(err, "value too small (%zu <= %zu)",
186 bkey_val_bytes(k.k), sizeof(*bp.v));
190 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX) {
191 prt_printf(err, "value too big (%zu > %zu)",
192 bkey_val_u64s(k.k), BKEY_BTREE_PTR_VAL_U64s_MAX);
196 if (c->sb.version < bcachefs_metadata_version_snapshot &&
197 bp.v->min_key.snapshot) {
198 prt_printf(err, "invalid min_key.snapshot (%u != 0)",
199 bp.v->min_key.snapshot);
203 return bch2_bkey_ptrs_invalid(c, k, rw, err);
206 void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c,
209 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
211 prt_printf(out, "seq %llx written %u min_key %s",
212 le64_to_cpu(bp.v->seq),
213 le16_to_cpu(bp.v->sectors_written),
214 BTREE_PTR_RANGE_UPDATED(bp.v) ? "R " : "");
216 bch2_bpos_to_text(out, bp.v->min_key);
217 prt_printf(out, " ");
218 bch2_bkey_ptrs_to_text(out, c, k);
221 void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version,
222 unsigned big_endian, int write,
225 struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k);
227 compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key);
229 if (version < bcachefs_metadata_version_inode_btree_change &&
230 btree_node_type_is_extents(btree_id) &&
231 bkey_cmp(bp.v->min_key, POS_MIN))
232 bp.v->min_key = write
233 ? bpos_nosnap_predecessor(bp.v->min_key)
234 : bpos_nosnap_successor(bp.v->min_key);
237 /* KEY_TYPE_extent: */
239 bool bch2_extent_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
241 struct bkey_ptrs l_ptrs = bch2_bkey_ptrs(l);
242 struct bkey_ptrs_c r_ptrs = bch2_bkey_ptrs_c(r);
243 union bch_extent_entry *en_l;
244 const union bch_extent_entry *en_r;
245 struct extent_ptr_decoded lp, rp;
251 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
252 if (extent_entry_type(en_l) != extent_entry_type(en_r))
255 en_l = extent_entry_next(en_l);
256 en_r = extent_entry_next(en_r);
259 if (en_l < l_ptrs.end || en_r < r_ptrs.end)
264 lp.crc = bch2_extent_crc_unpack(l.k, NULL);
265 rp.crc = bch2_extent_crc_unpack(r.k, NULL);
267 while (__bkey_ptr_next_decode(l.k, l_ptrs.end, lp, en_l) &&
268 __bkey_ptr_next_decode(r.k, r_ptrs.end, rp, en_r)) {
269 if (lp.ptr.offset + lp.crc.offset + lp.crc.live_size !=
270 rp.ptr.offset + rp.crc.offset ||
271 lp.ptr.dev != rp.ptr.dev ||
272 lp.ptr.gen != rp.ptr.gen ||
273 lp.has_ec != rp.has_ec)
276 /* Extents may not straddle buckets: */
277 ca = bch_dev_bkey_exists(c, lp.ptr.dev);
278 if (PTR_BUCKET_NR(ca, &lp.ptr) != PTR_BUCKET_NR(ca, &rp.ptr))
281 if (lp.has_ec != rp.has_ec ||
283 (lp.ec.block != rp.ec.block ||
284 lp.ec.redundancy != rp.ec.redundancy ||
285 lp.ec.idx != rp.ec.idx)))
288 if (lp.crc.compression_type != rp.crc.compression_type ||
289 lp.crc.nonce != rp.crc.nonce)
292 if (lp.crc.offset + lp.crc.live_size + rp.crc.live_size <=
293 lp.crc.uncompressed_size) {
294 /* can use left extent's crc entry */
295 } else if (lp.crc.live_size <= rp.crc.offset) {
296 /* can use right extent's crc entry */
298 /* check if checksums can be merged: */
299 if (lp.crc.csum_type != rp.crc.csum_type ||
300 lp.crc.nonce != rp.crc.nonce ||
301 crc_is_compressed(lp.crc) ||
302 !bch2_checksum_mergeable(lp.crc.csum_type))
305 if (lp.crc.offset + lp.crc.live_size != lp.crc.compressed_size ||
309 if (lp.crc.csum_type &&
310 lp.crc.uncompressed_size +
311 rp.crc.uncompressed_size > (c->opts.encoded_extent_max >> 9))
315 en_l = extent_entry_next(en_l);
316 en_r = extent_entry_next(en_r);
321 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
322 if (extent_entry_is_crc(en_l)) {
323 struct bch_extent_crc_unpacked crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
324 struct bch_extent_crc_unpacked crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
326 if (crc_l.uncompressed_size + crc_r.uncompressed_size >
327 bch2_crc_field_size_max[extent_entry_type(en_l)])
331 en_l = extent_entry_next(en_l);
332 en_r = extent_entry_next(en_r);
335 use_right_ptr = false;
338 while (en_l < l_ptrs.end) {
339 if (extent_entry_type(en_l) == BCH_EXTENT_ENTRY_ptr &&
341 en_l->ptr = en_r->ptr;
343 if (extent_entry_is_crc(en_l)) {
344 struct bch_extent_crc_unpacked crc_l =
345 bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
346 struct bch_extent_crc_unpacked crc_r =
347 bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
349 use_right_ptr = false;
351 if (crc_l.offset + crc_l.live_size + crc_r.live_size <=
352 crc_l.uncompressed_size) {
353 /* can use left extent's crc entry */
354 } else if (crc_l.live_size <= crc_r.offset) {
355 /* can use right extent's crc entry */
356 crc_r.offset -= crc_l.live_size;
357 bch2_extent_crc_pack(entry_to_crc(en_l), crc_r,
358 extent_entry_type(en_l));
359 use_right_ptr = true;
361 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
364 crc_r.uncompressed_size << 9);
366 crc_l.uncompressed_size += crc_r.uncompressed_size;
367 crc_l.compressed_size += crc_r.compressed_size;
368 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l,
369 extent_entry_type(en_l));
373 en_l = extent_entry_next(en_l);
374 en_r = extent_entry_next(en_r);
377 bch2_key_resize(l.k, l.k->size + r.k->size);
381 /* KEY_TYPE_reservation: */
383 int bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k,
384 int rw, struct printbuf *err)
386 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
388 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation)) {
389 prt_printf(err, "incorrect value size (%zu != %zu)",
390 bkey_val_bytes(k.k), sizeof(*r.v));
394 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX) {
395 prt_printf(err, "invalid nr_replicas (%u)",
403 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
406 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
408 prt_printf(out, "generation %u replicas %u",
409 le32_to_cpu(r.v->generation),
413 bool bch2_reservation_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r)
415 struct bkey_s_reservation l = bkey_s_to_reservation(_l);
416 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(_r);
418 if (l.v->generation != r.v->generation ||
419 l.v->nr_replicas != r.v->nr_replicas)
422 bch2_key_resize(l.k, l.k->size + r.k->size);
426 /* Extent checksum entries: */
428 /* returns true if not equal */
429 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
430 struct bch_extent_crc_unpacked r)
432 return (l.csum_type != r.csum_type ||
433 l.compression_type != r.compression_type ||
434 l.compressed_size != r.compressed_size ||
435 l.uncompressed_size != r.uncompressed_size ||
436 l.offset != r.offset ||
437 l.live_size != r.live_size ||
438 l.nonce != r.nonce ||
439 bch2_crc_cmp(l.csum, r.csum));
442 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
443 struct bch_extent_crc_unpacked n)
445 return !crc_is_compressed(u) &&
447 u.uncompressed_size > u.live_size &&
448 bch2_csum_type_is_encryption(u.csum_type) ==
449 bch2_csum_type_is_encryption(n.csum_type);
452 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
453 struct bch_extent_crc_unpacked n)
455 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
456 struct bch_extent_crc_unpacked crc;
457 const union bch_extent_entry *i;
462 bkey_for_each_crc(k.k, ptrs, crc, i)
463 if (can_narrow_crc(crc, n))
470 * We're writing another replica for this extent, so while we've got the data in
471 * memory we'll be computing a new checksum for the currently live data.
473 * If there are other replicas we aren't moving, and they are checksummed but
474 * not compressed, we can modify them to point to only the data that is
475 * currently live (so that readers won't have to bounce) while we've got the
478 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
480 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
481 struct bch_extent_crc_unpacked u;
482 struct extent_ptr_decoded p;
483 union bch_extent_entry *i;
486 /* Find a checksum entry that covers only live data: */
488 bkey_for_each_crc(&k->k, ptrs, u, i)
489 if (!crc_is_compressed(u) &&
491 u.live_size == u.uncompressed_size) {
498 BUG_ON(crc_is_compressed(n));
500 BUG_ON(n.live_size != k->k.size);
502 restart_narrow_pointers:
503 ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
505 bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
506 if (can_narrow_crc(p.crc, n)) {
507 __bch2_bkey_drop_ptr(bkey_i_to_s(k), &i->ptr);
508 p.ptr.offset += p.crc.offset;
510 bch2_extent_ptr_decoded_append(k, &p);
512 goto restart_narrow_pointers;
518 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
519 struct bch_extent_crc_unpacked src,
520 enum bch_extent_entry_type type)
522 #define set_common_fields(_dst, _src) \
523 _dst.type = 1 << type; \
524 _dst.csum_type = _src.csum_type, \
525 _dst.compression_type = _src.compression_type, \
526 _dst._compressed_size = _src.compressed_size - 1, \
527 _dst._uncompressed_size = _src.uncompressed_size - 1, \
528 _dst.offset = _src.offset
531 case BCH_EXTENT_ENTRY_crc32:
532 set_common_fields(dst->crc32, src);
533 dst->crc32.csum = *((__le32 *) &src.csum.lo);
535 case BCH_EXTENT_ENTRY_crc64:
536 set_common_fields(dst->crc64, src);
537 dst->crc64.nonce = src.nonce;
538 dst->crc64.csum_lo = src.csum.lo;
539 dst->crc64.csum_hi = *((__le16 *) &src.csum.hi);
541 case BCH_EXTENT_ENTRY_crc128:
542 set_common_fields(dst->crc128, src);
543 dst->crc128.nonce = src.nonce;
544 dst->crc128.csum = src.csum;
549 #undef set_common_fields
552 void bch2_extent_crc_append(struct bkey_i *k,
553 struct bch_extent_crc_unpacked new)
555 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
556 union bch_extent_crc *crc = (void *) ptrs.end;
557 enum bch_extent_entry_type type;
559 if (bch_crc_bytes[new.csum_type] <= 4 &&
560 new.uncompressed_size <= CRC32_SIZE_MAX &&
561 new.nonce <= CRC32_NONCE_MAX)
562 type = BCH_EXTENT_ENTRY_crc32;
563 else if (bch_crc_bytes[new.csum_type] <= 10 &&
564 new.uncompressed_size <= CRC64_SIZE_MAX &&
565 new.nonce <= CRC64_NONCE_MAX)
566 type = BCH_EXTENT_ENTRY_crc64;
567 else if (bch_crc_bytes[new.csum_type] <= 16 &&
568 new.uncompressed_size <= CRC128_SIZE_MAX &&
569 new.nonce <= CRC128_NONCE_MAX)
570 type = BCH_EXTENT_ENTRY_crc128;
574 bch2_extent_crc_pack(crc, new, type);
576 k->k.u64s += extent_entry_u64s(ptrs.end);
578 EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
581 /* Generic code for keys with pointers: */
583 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
585 return bch2_bkey_devs(k).nr;
588 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
590 return k.k->type == KEY_TYPE_reservation
591 ? bkey_s_c_to_reservation(k).v->nr_replicas
592 : bch2_bkey_dirty_devs(k).nr;
595 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
599 if (k.k->type == KEY_TYPE_reservation) {
600 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
602 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
603 const union bch_extent_entry *entry;
604 struct extent_ptr_decoded p;
606 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
607 ret += !p.ptr.cached && !crc_is_compressed(p.crc);
613 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
615 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
616 const union bch_extent_entry *entry;
617 struct extent_ptr_decoded p;
620 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
621 if (!p.ptr.cached && crc_is_compressed(p.crc))
622 ret += p.crc.compressed_size;
627 bool bch2_bkey_is_incompressible(struct bkey_s_c k)
629 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
630 const union bch_extent_entry *entry;
631 struct bch_extent_crc_unpacked crc;
633 bkey_for_each_crc(k.k, ptrs, crc, entry)
634 if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
639 unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
641 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
642 const union bch_extent_entry *entry;
643 struct extent_ptr_decoded p = { 0 };
644 unsigned replicas = 0;
646 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
651 replicas += p.ec.redundancy;
660 static unsigned bch2_extent_ptr_durability(struct bch_fs *c,
661 struct extent_ptr_decoded p)
663 unsigned durability = 0;
669 ca = bch_dev_bkey_exists(c, p.ptr.dev);
671 if (ca->mi.state != BCH_MEMBER_STATE_failed)
672 durability = max_t(unsigned, durability, ca->mi.durability);
675 durability += p.ec.redundancy;
680 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
682 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
683 const union bch_extent_entry *entry;
684 struct extent_ptr_decoded p;
685 unsigned durability = 0;
687 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
688 durability += bch2_extent_ptr_durability(c, p);
693 void bch2_bkey_extent_entry_drop(struct bkey_i *k, union bch_extent_entry *entry)
695 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
696 union bch_extent_entry *next = extent_entry_next(entry);
698 memmove_u64s(entry, next, (u64 *) end - (u64 *) next);
699 k->k.u64s -= extent_entry_u64s(entry);
702 void bch2_bkey_append_ptr(struct bkey_i *k,
703 struct bch_extent_ptr ptr)
705 EBUG_ON(bch2_bkey_has_device(bkey_i_to_s_c(k), ptr.dev));
708 case KEY_TYPE_btree_ptr:
709 case KEY_TYPE_btree_ptr_v2:
710 case KEY_TYPE_extent:
711 EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
713 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
715 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
725 static inline void __extent_entry_insert(struct bkey_i *k,
726 union bch_extent_entry *dst,
727 union bch_extent_entry *new)
729 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
731 memmove_u64s_up_small((u64 *) dst + extent_entry_u64s(new),
732 dst, (u64 *) end - (u64 *) dst);
733 k->k.u64s += extent_entry_u64s(new);
734 memcpy(dst, new, extent_entry_bytes(new));
737 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
738 struct extent_ptr_decoded *p)
740 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
741 struct bch_extent_crc_unpacked crc =
742 bch2_extent_crc_unpack(&k->k, NULL);
743 union bch_extent_entry *pos;
745 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
750 bkey_for_each_crc(&k->k, ptrs, crc, pos)
751 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
752 pos = extent_entry_next(pos);
756 bch2_extent_crc_append(k, p->crc);
757 pos = bkey_val_end(bkey_i_to_s(k));
759 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
760 __extent_entry_insert(k, pos, to_entry(&p->ptr));
763 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
764 __extent_entry_insert(k, pos, to_entry(&p->ec));
768 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
769 union bch_extent_entry *entry)
771 union bch_extent_entry *i = ptrs.start;
776 while (extent_entry_next(i) != entry)
777 i = extent_entry_next(i);
781 static void extent_entry_drop(struct bkey_s k, union bch_extent_entry *entry)
783 union bch_extent_entry *next = extent_entry_next(entry);
785 /* stripes have ptrs, but their layout doesn't work with this code */
786 BUG_ON(k.k->type == KEY_TYPE_stripe);
788 memmove_u64s_down(entry, next,
789 (u64 *) bkey_val_end(k) - (u64 *) next);
790 k.k->u64s -= (u64 *) next - (u64 *) entry;
794 * Returns pointer to the next entry after the one being dropped:
796 static union bch_extent_entry *__bch2_bkey_drop_ptr(struct bkey_s k,
797 struct bch_extent_ptr *ptr)
799 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
800 union bch_extent_entry *entry = to_entry(ptr), *next;
801 union bch_extent_entry *ret = entry;
802 bool drop_crc = true;
804 EBUG_ON(ptr < &ptrs.start->ptr ||
805 ptr >= &ptrs.end->ptr);
806 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
808 for (next = extent_entry_next(entry);
810 next = extent_entry_next(next)) {
811 if (extent_entry_is_crc(next)) {
813 } else if (extent_entry_is_ptr(next)) {
819 extent_entry_drop(k, entry);
821 while ((entry = extent_entry_prev(ptrs, entry))) {
822 if (extent_entry_is_ptr(entry))
825 if ((extent_entry_is_crc(entry) && drop_crc) ||
826 extent_entry_is_stripe_ptr(entry)) {
827 ret = (void *) ret - extent_entry_bytes(entry);
828 extent_entry_drop(k, entry);
835 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
836 struct bch_extent_ptr *ptr)
838 bool have_dirty = bch2_bkey_dirty_devs(k.s_c).nr;
839 union bch_extent_entry *ret =
840 __bch2_bkey_drop_ptr(k, ptr);
843 * If we deleted all the dirty pointers and there's still cached
844 * pointers, we could set the cached pointers to dirty if they're not
845 * stale - but to do that correctly we'd need to grab an open_bucket
846 * reference so that we don't race with bucket reuse:
849 !bch2_bkey_dirty_devs(k.s_c).nr) {
850 k.k->type = KEY_TYPE_error;
851 set_bkey_val_u64s(k.k, 0);
853 } else if (!bch2_bkey_nr_ptrs(k.s_c)) {
854 k.k->type = KEY_TYPE_deleted;
855 set_bkey_val_u64s(k.k, 0);
862 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
864 struct bch_extent_ptr *ptr;
866 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
869 void bch2_bkey_drop_device_noerror(struct bkey_s k, unsigned dev)
871 struct bch_extent_ptr *ptr = (void *) bch2_bkey_has_device(k.s_c, dev);
874 __bch2_bkey_drop_ptr(k, ptr);
877 const struct bch_extent_ptr *
878 bch2_bkey_has_device(struct bkey_s_c k, unsigned dev)
880 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
881 const struct bch_extent_ptr *ptr;
883 bkey_for_each_ptr(ptrs, ptr)
890 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
892 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
893 const struct bch_extent_ptr *ptr;
895 bkey_for_each_ptr(ptrs, ptr)
896 if (bch2_dev_in_target(c, ptr->dev, target) &&
898 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
904 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
905 struct bch_extent_ptr m, u64 offset)
907 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
908 const union bch_extent_entry *entry;
909 struct extent_ptr_decoded p;
911 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
912 if (p.ptr.dev == m.dev &&
913 p.ptr.gen == m.gen &&
914 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
915 (s64) m.offset - offset)
922 * Returns true if two extents refer to the same data:
924 bool bch2_extents_match(struct bkey_s_c k1, struct bkey_s_c k2)
926 struct bkey_ptrs_c ptrs1 = bch2_bkey_ptrs_c(k1);
927 struct bkey_ptrs_c ptrs2 = bch2_bkey_ptrs_c(k2);
928 const union bch_extent_entry *entry1, *entry2;
929 struct extent_ptr_decoded p1, p2;
931 bkey_for_each_ptr_decode(k1.k, ptrs1, p1, entry1)
932 bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2)
933 if (p1.ptr.dev == p2.ptr.dev &&
934 p1.ptr.gen == p2.ptr.gen &&
935 (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) ==
936 (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k))
942 bool bch2_extent_has_ptr(struct bkey_s_c k1, struct extent_ptr_decoded p1,
945 struct bkey_ptrs_c ptrs2 = bch2_bkey_ptrs_c(k2);
946 const union bch_extent_entry *entry2;
947 struct extent_ptr_decoded p2;
949 bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2)
950 if (p1.ptr.dev == p2.ptr.dev &&
951 p1.ptr.gen == p2.ptr.gen &&
952 (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) ==
953 (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k))
960 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
962 * Returns true if @k should be dropped entirely
964 * For existing keys, only called when btree nodes are being rewritten, not when
965 * they're merely being compacted/resorted in memory.
967 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
969 struct bch_extent_ptr *ptr;
971 bch2_bkey_drop_ptrs(k, ptr,
973 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
975 return bkey_deleted(k.k);
978 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
981 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
982 const union bch_extent_entry *entry;
983 struct bch_extent_crc_unpacked crc;
984 const struct bch_extent_ptr *ptr;
985 const struct bch_extent_stripe_ptr *ec;
989 bkey_extent_entry_for_each(ptrs, entry) {
991 prt_printf(out, " ");
993 switch (__extent_entry_type(entry)) {
994 case BCH_EXTENT_ENTRY_ptr:
995 ptr = entry_to_ptr(entry);
996 ca = c && ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
997 ? bch_dev_bkey_exists(c, ptr->dev)
1001 prt_printf(out, "ptr: %u:%llu gen %u%s", ptr->dev,
1002 (u64) ptr->offset, ptr->gen,
1003 ptr->cached ? " cached" : "");
1006 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
1008 prt_printf(out, "ptr: %u:%llu:%u gen %u%s", ptr->dev,
1009 b, offset, ptr->gen,
1010 ptr->cached ? " cached" : "");
1012 if (ca && ptr_stale(ca, ptr))
1013 prt_printf(out, " stale");
1016 case BCH_EXTENT_ENTRY_crc32:
1017 case BCH_EXTENT_ENTRY_crc64:
1018 case BCH_EXTENT_ENTRY_crc128:
1019 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1021 prt_printf(out, "crc: c_size %u size %u offset %u nonce %u csum %s compress %s",
1022 crc.compressed_size,
1023 crc.uncompressed_size,
1024 crc.offset, crc.nonce,
1025 bch2_csum_types[crc.csum_type],
1026 bch2_compression_types[crc.compression_type]);
1028 case BCH_EXTENT_ENTRY_stripe_ptr:
1029 ec = &entry->stripe_ptr;
1031 prt_printf(out, "ec: idx %llu block %u",
1032 (u64) ec->idx, ec->block);
1035 prt_printf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
1043 static int extent_ptr_invalid(const struct bch_fs *c,
1045 const struct bch_extent_ptr *ptr,
1046 unsigned size_ondisk,
1048 struct printbuf *err)
1050 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1051 const struct bch_extent_ptr *ptr2;
1056 if (!bch2_dev_exists2(c, ptr->dev)) {
1057 prt_printf(err, "pointer to invalid device (%u)", ptr->dev);
1061 ca = bch_dev_bkey_exists(c, ptr->dev);
1062 bkey_for_each_ptr(ptrs, ptr2)
1063 if (ptr != ptr2 && ptr->dev == ptr2->dev) {
1064 prt_printf(err, "multiple pointers to same device (%u)", ptr->dev);
1068 bucket = sector_to_bucket_and_offset(ca, ptr->offset, &bucket_offset);
1070 if (bucket >= ca->mi.nbuckets) {
1071 prt_printf(err, "pointer past last bucket (%llu > %llu)",
1072 bucket, ca->mi.nbuckets);
1076 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket)) {
1077 prt_printf(err, "pointer before first bucket (%llu < %u)",
1078 bucket, ca->mi.first_bucket);
1082 if (bucket_offset + size_ondisk > ca->mi.bucket_size) {
1083 prt_printf(err, "pointer spans multiple buckets (%u + %u > %u)",
1084 bucket_offset, size_ondisk, ca->mi.bucket_size);
1091 int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
1092 int rw, struct printbuf *err)
1094 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1095 const union bch_extent_entry *entry;
1096 struct bch_extent_crc_unpacked crc;
1097 unsigned size_ondisk = k.k->size;
1098 unsigned nonce = UINT_MAX;
1099 unsigned nr_ptrs = 0;
1102 if (bkey_is_btree_ptr(k.k))
1103 size_ondisk = btree_sectors(c);
1105 bkey_extent_entry_for_each(ptrs, entry) {
1106 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX) {
1107 prt_printf(err, "invalid extent entry type (got %u, max %u)",
1108 __extent_entry_type(entry), BCH_EXTENT_ENTRY_MAX);
1112 if (bkey_is_btree_ptr(k.k) &&
1113 !extent_entry_is_ptr(entry)) {
1114 prt_printf(err, "has non ptr field");
1118 switch (extent_entry_type(entry)) {
1119 case BCH_EXTENT_ENTRY_ptr:
1120 ret = extent_ptr_invalid(c, k, &entry->ptr, size_ondisk,
1126 case BCH_EXTENT_ENTRY_crc32:
1127 case BCH_EXTENT_ENTRY_crc64:
1128 case BCH_EXTENT_ENTRY_crc128:
1129 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1131 if (crc.offset + crc.live_size >
1132 crc.uncompressed_size) {
1133 prt_printf(err, "checksum offset + key size > uncompressed size");
1137 size_ondisk = crc.compressed_size;
1139 if (!bch2_checksum_type_valid(c, crc.csum_type)) {
1140 prt_printf(err, "invalid checksum type");
1144 if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR) {
1145 prt_printf(err, "invalid compression type");
1149 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1150 if (nonce == UINT_MAX)
1151 nonce = crc.offset + crc.nonce;
1152 else if (nonce != crc.offset + crc.nonce) {
1153 prt_printf(err, "incorrect nonce");
1158 case BCH_EXTENT_ENTRY_stripe_ptr:
1163 if (nr_ptrs >= BCH_BKEY_PTRS_MAX) {
1164 prt_str(err, "too many ptrs");
1171 void bch2_ptr_swab(struct bkey_s k)
1173 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1174 union bch_extent_entry *entry;
1177 for (d = (u64 *) ptrs.start;
1178 d != (u64 *) ptrs.end;
1182 for (entry = ptrs.start;
1184 entry = extent_entry_next(entry)) {
1185 switch (extent_entry_type(entry)) {
1186 case BCH_EXTENT_ENTRY_ptr:
1188 case BCH_EXTENT_ENTRY_crc32:
1189 entry->crc32.csum = swab32(entry->crc32.csum);
1191 case BCH_EXTENT_ENTRY_crc64:
1192 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1193 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1195 case BCH_EXTENT_ENTRY_crc128:
1196 entry->crc128.csum.hi = (__force __le64)
1197 swab64((__force u64) entry->crc128.csum.hi);
1198 entry->crc128.csum.lo = (__force __le64)
1199 swab64((__force u64) entry->crc128.csum.lo);
1201 case BCH_EXTENT_ENTRY_stripe_ptr:
1207 /* Generic extent code: */
1209 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1211 unsigned new_val_u64s = bkey_val_u64s(k.k);
1215 if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
1218 EBUG_ON(bkey_cmp(where, k.k->p) > 0);
1220 sub = where.offset - bkey_start_offset(k.k);
1225 k.k->type = KEY_TYPE_deleted;
1229 switch (k.k->type) {
1230 case KEY_TYPE_extent:
1231 case KEY_TYPE_reflink_v: {
1232 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1233 union bch_extent_entry *entry;
1234 bool seen_crc = false;
1236 bkey_extent_entry_for_each(ptrs, entry) {
1237 switch (extent_entry_type(entry)) {
1238 case BCH_EXTENT_ENTRY_ptr:
1240 entry->ptr.offset += sub;
1242 case BCH_EXTENT_ENTRY_crc32:
1243 entry->crc32.offset += sub;
1245 case BCH_EXTENT_ENTRY_crc64:
1246 entry->crc64.offset += sub;
1248 case BCH_EXTENT_ENTRY_crc128:
1249 entry->crc128.offset += sub;
1251 case BCH_EXTENT_ENTRY_stripe_ptr:
1255 if (extent_entry_is_crc(entry))
1261 case KEY_TYPE_reflink_p: {
1262 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1264 le64_add_cpu(&p.v->idx, sub);
1267 case KEY_TYPE_inline_data:
1268 case KEY_TYPE_indirect_inline_data: {
1269 void *p = bkey_inline_data_p(k);
1270 unsigned bytes = bkey_inline_data_bytes(k.k);
1272 sub = min_t(u64, sub << 9, bytes);
1274 memmove(p, p + sub, bytes - sub);
1276 new_val_u64s -= sub >> 3;
1281 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1282 BUG_ON(val_u64s_delta < 0);
1284 set_bkey_val_u64s(k.k, new_val_u64s);
1285 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1286 return -val_u64s_delta;
1289 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1291 unsigned new_val_u64s = bkey_val_u64s(k.k);
1295 if (bkey_cmp(where, k.k->p) >= 0)
1298 EBUG_ON(bkey_cmp(where, bkey_start_pos(k.k)) < 0);
1300 len = where.offset - bkey_start_offset(k.k);
1302 k.k->p.offset = where.offset;
1306 k.k->type = KEY_TYPE_deleted;
1310 switch (k.k->type) {
1311 case KEY_TYPE_inline_data:
1312 case KEY_TYPE_indirect_inline_data:
1313 new_val_u64s = (bkey_inline_data_offset(k.k) +
1314 min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
1318 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1319 BUG_ON(val_u64s_delta < 0);
1321 set_bkey_val_u64s(k.k, new_val_u64s);
1322 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1323 return -val_u64s_delta;