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) {
119 * Unwritten extent: no need to actually read, treat it as a
120 * hole and return 0s:
125 ca = bch_dev_bkey_exists(c, p.ptr.dev);
128 * If there are any dirty pointers it's an error if we can't
131 if (!ret && !p.ptr.cached)
134 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
137 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
139 p.idx = f->nr_failed < f->nr_retries
144 !bch2_dev_is_readable(ca))
147 if (bch2_force_reconstruct_read &&
151 if (p.idx >= (unsigned) p.has_ec + 1)
154 if (ret > 0 && !ptr_better(c, p, *pick))
164 /* KEY_TYPE_btree_ptr: */
166 int bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k,
167 unsigned flags, struct printbuf *err)
169 if (bkey_val_u64s(k.k) > BCH_REPLICAS_MAX) {
170 prt_printf(err, "value too big (%zu > %u)",
171 bkey_val_u64s(k.k), BCH_REPLICAS_MAX);
172 return -BCH_ERR_invalid_bkey;
175 return bch2_bkey_ptrs_invalid(c, k, flags, err);
178 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
181 bch2_bkey_ptrs_to_text(out, c, k);
184 int bch2_btree_ptr_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
185 unsigned flags, struct printbuf *err)
187 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
189 if (bkey_val_bytes(k.k) <= sizeof(*bp.v)) {
190 prt_printf(err, "value too small (%zu <= %zu)",
191 bkey_val_bytes(k.k), sizeof(*bp.v));
192 return -BCH_ERR_invalid_bkey;
195 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX) {
196 prt_printf(err, "value too big (%zu > %zu)",
197 bkey_val_u64s(k.k), BKEY_BTREE_PTR_VAL_U64s_MAX);
198 return -BCH_ERR_invalid_bkey;
201 if (c->sb.version < bcachefs_metadata_version_snapshot &&
202 bp.v->min_key.snapshot) {
203 prt_printf(err, "invalid min_key.snapshot (%u != 0)",
204 bp.v->min_key.snapshot);
205 return -BCH_ERR_invalid_bkey;
208 return bch2_bkey_ptrs_invalid(c, k, flags, err);
211 void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c,
214 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
216 prt_printf(out, "seq %llx written %u min_key %s",
217 le64_to_cpu(bp.v->seq),
218 le16_to_cpu(bp.v->sectors_written),
219 BTREE_PTR_RANGE_UPDATED(bp.v) ? "R " : "");
221 bch2_bpos_to_text(out, bp.v->min_key);
222 prt_printf(out, " ");
223 bch2_bkey_ptrs_to_text(out, c, k);
226 void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version,
227 unsigned big_endian, int write,
230 struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k);
232 compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key);
234 if (version < bcachefs_metadata_version_inode_btree_change &&
235 btree_node_type_is_extents(btree_id) &&
236 !bkey_eq(bp.v->min_key, POS_MIN))
237 bp.v->min_key = write
238 ? bpos_nosnap_predecessor(bp.v->min_key)
239 : bpos_nosnap_successor(bp.v->min_key);
242 /* KEY_TYPE_extent: */
244 bool bch2_extent_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
246 struct bkey_ptrs l_ptrs = bch2_bkey_ptrs(l);
247 struct bkey_ptrs_c r_ptrs = bch2_bkey_ptrs_c(r);
248 union bch_extent_entry *en_l;
249 const union bch_extent_entry *en_r;
250 struct extent_ptr_decoded lp, rp;
256 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
257 if (extent_entry_type(en_l) != extent_entry_type(en_r))
260 en_l = extent_entry_next(en_l);
261 en_r = extent_entry_next(en_r);
264 if (en_l < l_ptrs.end || en_r < r_ptrs.end)
269 lp.crc = bch2_extent_crc_unpack(l.k, NULL);
270 rp.crc = bch2_extent_crc_unpack(r.k, NULL);
272 while (__bkey_ptr_next_decode(l.k, l_ptrs.end, lp, en_l) &&
273 __bkey_ptr_next_decode(r.k, r_ptrs.end, rp, en_r)) {
274 if (lp.ptr.offset + lp.crc.offset + lp.crc.live_size !=
275 rp.ptr.offset + rp.crc.offset ||
276 lp.ptr.dev != rp.ptr.dev ||
277 lp.ptr.gen != rp.ptr.gen ||
278 lp.ptr.unwritten != rp.ptr.unwritten ||
279 lp.has_ec != rp.has_ec)
282 /* Extents may not straddle buckets: */
283 ca = bch_dev_bkey_exists(c, lp.ptr.dev);
284 if (PTR_BUCKET_NR(ca, &lp.ptr) != PTR_BUCKET_NR(ca, &rp.ptr))
287 if (lp.has_ec != rp.has_ec ||
289 (lp.ec.block != rp.ec.block ||
290 lp.ec.redundancy != rp.ec.redundancy ||
291 lp.ec.idx != rp.ec.idx)))
294 if (lp.crc.compression_type != rp.crc.compression_type ||
295 lp.crc.nonce != rp.crc.nonce)
298 if (lp.crc.offset + lp.crc.live_size + rp.crc.live_size <=
299 lp.crc.uncompressed_size) {
300 /* can use left extent's crc entry */
301 } else if (lp.crc.live_size <= rp.crc.offset) {
302 /* can use right extent's crc entry */
304 /* check if checksums can be merged: */
305 if (lp.crc.csum_type != rp.crc.csum_type ||
306 lp.crc.nonce != rp.crc.nonce ||
307 crc_is_compressed(lp.crc) ||
308 !bch2_checksum_mergeable(lp.crc.csum_type))
311 if (lp.crc.offset + lp.crc.live_size != lp.crc.compressed_size ||
315 if (lp.crc.csum_type &&
316 lp.crc.uncompressed_size +
317 rp.crc.uncompressed_size > (c->opts.encoded_extent_max >> 9))
321 en_l = extent_entry_next(en_l);
322 en_r = extent_entry_next(en_r);
327 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
328 if (extent_entry_is_crc(en_l)) {
329 struct bch_extent_crc_unpacked crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
330 struct bch_extent_crc_unpacked crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
332 if (crc_l.uncompressed_size + crc_r.uncompressed_size >
333 bch2_crc_field_size_max[extent_entry_type(en_l)])
337 en_l = extent_entry_next(en_l);
338 en_r = extent_entry_next(en_r);
341 use_right_ptr = false;
344 while (en_l < l_ptrs.end) {
345 if (extent_entry_type(en_l) == BCH_EXTENT_ENTRY_ptr &&
347 en_l->ptr = en_r->ptr;
349 if (extent_entry_is_crc(en_l)) {
350 struct bch_extent_crc_unpacked crc_l =
351 bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
352 struct bch_extent_crc_unpacked crc_r =
353 bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
355 use_right_ptr = false;
357 if (crc_l.offset + crc_l.live_size + crc_r.live_size <=
358 crc_l.uncompressed_size) {
359 /* can use left extent's crc entry */
360 } else if (crc_l.live_size <= crc_r.offset) {
361 /* can use right extent's crc entry */
362 crc_r.offset -= crc_l.live_size;
363 bch2_extent_crc_pack(entry_to_crc(en_l), crc_r,
364 extent_entry_type(en_l));
365 use_right_ptr = true;
367 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
370 crc_r.uncompressed_size << 9);
372 crc_l.uncompressed_size += crc_r.uncompressed_size;
373 crc_l.compressed_size += crc_r.compressed_size;
374 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l,
375 extent_entry_type(en_l));
379 en_l = extent_entry_next(en_l);
380 en_r = extent_entry_next(en_r);
383 bch2_key_resize(l.k, l.k->size + r.k->size);
387 /* KEY_TYPE_reservation: */
389 int bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k,
390 unsigned flags, struct printbuf *err)
392 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
394 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation)) {
395 prt_printf(err, "incorrect value size (%zu != %zu)",
396 bkey_val_bytes(k.k), sizeof(*r.v));
397 return -BCH_ERR_invalid_bkey;
400 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX) {
401 prt_printf(err, "invalid nr_replicas (%u)",
403 return -BCH_ERR_invalid_bkey;
409 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
412 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
414 prt_printf(out, "generation %u replicas %u",
415 le32_to_cpu(r.v->generation),
419 bool bch2_reservation_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r)
421 struct bkey_s_reservation l = bkey_s_to_reservation(_l);
422 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(_r);
424 if (l.v->generation != r.v->generation ||
425 l.v->nr_replicas != r.v->nr_replicas)
428 bch2_key_resize(l.k, l.k->size + r.k->size);
432 /* Extent checksum entries: */
434 /* returns true if not equal */
435 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
436 struct bch_extent_crc_unpacked r)
438 return (l.csum_type != r.csum_type ||
439 l.compression_type != r.compression_type ||
440 l.compressed_size != r.compressed_size ||
441 l.uncompressed_size != r.uncompressed_size ||
442 l.offset != r.offset ||
443 l.live_size != r.live_size ||
444 l.nonce != r.nonce ||
445 bch2_crc_cmp(l.csum, r.csum));
448 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
449 struct bch_extent_crc_unpacked n)
451 return !crc_is_compressed(u) &&
453 u.uncompressed_size > u.live_size &&
454 bch2_csum_type_is_encryption(u.csum_type) ==
455 bch2_csum_type_is_encryption(n.csum_type);
458 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
459 struct bch_extent_crc_unpacked n)
461 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
462 struct bch_extent_crc_unpacked crc;
463 const union bch_extent_entry *i;
468 bkey_for_each_crc(k.k, ptrs, crc, i)
469 if (can_narrow_crc(crc, n))
476 * We're writing another replica for this extent, so while we've got the data in
477 * memory we'll be computing a new checksum for the currently live data.
479 * If there are other replicas we aren't moving, and they are checksummed but
480 * not compressed, we can modify them to point to only the data that is
481 * currently live (so that readers won't have to bounce) while we've got the
484 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
486 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
487 struct bch_extent_crc_unpacked u;
488 struct extent_ptr_decoded p;
489 union bch_extent_entry *i;
492 /* Find a checksum entry that covers only live data: */
494 bkey_for_each_crc(&k->k, ptrs, u, i)
495 if (!crc_is_compressed(u) &&
497 u.live_size == u.uncompressed_size) {
504 BUG_ON(crc_is_compressed(n));
506 BUG_ON(n.live_size != k->k.size);
508 restart_narrow_pointers:
509 ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
511 bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
512 if (can_narrow_crc(p.crc, n)) {
513 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(k), &i->ptr);
514 p.ptr.offset += p.crc.offset;
516 bch2_extent_ptr_decoded_append(k, &p);
518 goto restart_narrow_pointers;
524 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
525 struct bch_extent_crc_unpacked src,
526 enum bch_extent_entry_type type)
528 #define set_common_fields(_dst, _src) \
529 _dst.type = 1 << type; \
530 _dst.csum_type = _src.csum_type, \
531 _dst.compression_type = _src.compression_type, \
532 _dst._compressed_size = _src.compressed_size - 1, \
533 _dst._uncompressed_size = _src.uncompressed_size - 1, \
534 _dst.offset = _src.offset
537 case BCH_EXTENT_ENTRY_crc32:
538 set_common_fields(dst->crc32, src);
539 dst->crc32.csum = *((__le32 *) &src.csum.lo);
541 case BCH_EXTENT_ENTRY_crc64:
542 set_common_fields(dst->crc64, src);
543 dst->crc64.nonce = src.nonce;
544 dst->crc64.csum_lo = src.csum.lo;
545 dst->crc64.csum_hi = *((__le16 *) &src.csum.hi);
547 case BCH_EXTENT_ENTRY_crc128:
548 set_common_fields(dst->crc128, src);
549 dst->crc128.nonce = src.nonce;
550 dst->crc128.csum = src.csum;
555 #undef set_common_fields
558 void bch2_extent_crc_append(struct bkey_i *k,
559 struct bch_extent_crc_unpacked new)
561 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
562 union bch_extent_crc *crc = (void *) ptrs.end;
563 enum bch_extent_entry_type type;
565 if (bch_crc_bytes[new.csum_type] <= 4 &&
566 new.uncompressed_size <= CRC32_SIZE_MAX &&
567 new.nonce <= CRC32_NONCE_MAX)
568 type = BCH_EXTENT_ENTRY_crc32;
569 else if (bch_crc_bytes[new.csum_type] <= 10 &&
570 new.uncompressed_size <= CRC64_SIZE_MAX &&
571 new.nonce <= CRC64_NONCE_MAX)
572 type = BCH_EXTENT_ENTRY_crc64;
573 else if (bch_crc_bytes[new.csum_type] <= 16 &&
574 new.uncompressed_size <= CRC128_SIZE_MAX &&
575 new.nonce <= CRC128_NONCE_MAX)
576 type = BCH_EXTENT_ENTRY_crc128;
580 bch2_extent_crc_pack(crc, new, type);
582 k->k.u64s += extent_entry_u64s(ptrs.end);
584 EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
587 /* Generic code for keys with pointers: */
589 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
591 return bch2_bkey_devs(k).nr;
594 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
596 return k.k->type == KEY_TYPE_reservation
597 ? bkey_s_c_to_reservation(k).v->nr_replicas
598 : bch2_bkey_dirty_devs(k).nr;
601 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
605 if (k.k->type == KEY_TYPE_reservation) {
606 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
608 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
609 const union bch_extent_entry *entry;
610 struct extent_ptr_decoded p;
612 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
613 ret += !p.ptr.cached && !crc_is_compressed(p.crc);
619 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
621 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
622 const union bch_extent_entry *entry;
623 struct extent_ptr_decoded p;
626 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
627 if (!p.ptr.cached && crc_is_compressed(p.crc))
628 ret += p.crc.compressed_size;
633 bool bch2_bkey_is_incompressible(struct bkey_s_c k)
635 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
636 const union bch_extent_entry *entry;
637 struct bch_extent_crc_unpacked crc;
639 bkey_for_each_crc(k.k, ptrs, crc, entry)
640 if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
645 unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
647 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
648 const union bch_extent_entry *entry;
649 struct extent_ptr_decoded p = { 0 };
650 unsigned replicas = 0;
652 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
657 replicas += p.ec.redundancy;
666 unsigned bch2_extent_ptr_durability(struct bch_fs *c, struct extent_ptr_decoded *p)
668 unsigned durability = 0;
674 ca = bch_dev_bkey_exists(c, p->ptr.dev);
676 if (ca->mi.state != BCH_MEMBER_STATE_failed)
677 durability = max_t(unsigned, durability, ca->mi.durability);
680 durability += p->ec.redundancy;
685 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
687 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
688 const union bch_extent_entry *entry;
689 struct extent_ptr_decoded p;
690 unsigned durability = 0;
692 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
693 durability += bch2_extent_ptr_durability(c,& p);
698 void bch2_bkey_extent_entry_drop(struct bkey_i *k, union bch_extent_entry *entry)
700 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
701 union bch_extent_entry *next = extent_entry_next(entry);
703 memmove_u64s(entry, next, (u64 *) end - (u64 *) next);
704 k->k.u64s -= extent_entry_u64s(entry);
707 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
708 struct extent_ptr_decoded *p)
710 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
711 struct bch_extent_crc_unpacked crc =
712 bch2_extent_crc_unpack(&k->k, NULL);
713 union bch_extent_entry *pos;
715 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
720 bkey_for_each_crc(&k->k, ptrs, crc, pos)
721 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
722 pos = extent_entry_next(pos);
726 bch2_extent_crc_append(k, p->crc);
727 pos = bkey_val_end(bkey_i_to_s(k));
729 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
730 __extent_entry_insert(k, pos, to_entry(&p->ptr));
733 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
734 __extent_entry_insert(k, pos, to_entry(&p->ec));
738 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
739 union bch_extent_entry *entry)
741 union bch_extent_entry *i = ptrs.start;
746 while (extent_entry_next(i) != entry)
747 i = extent_entry_next(i);
751 static void extent_entry_drop(struct bkey_s k, union bch_extent_entry *entry)
753 union bch_extent_entry *next = extent_entry_next(entry);
755 /* stripes have ptrs, but their layout doesn't work with this code */
756 BUG_ON(k.k->type == KEY_TYPE_stripe);
758 memmove_u64s_down(entry, next,
759 (u64 *) bkey_val_end(k) - (u64 *) next);
760 k.k->u64s -= (u64 *) next - (u64 *) entry;
764 * Returns pointer to the next entry after the one being dropped:
766 union bch_extent_entry *bch2_bkey_drop_ptr_noerror(struct bkey_s k,
767 struct bch_extent_ptr *ptr)
769 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
770 union bch_extent_entry *entry = to_entry(ptr), *next;
771 union bch_extent_entry *ret = entry;
772 bool drop_crc = true;
774 EBUG_ON(ptr < &ptrs.start->ptr ||
775 ptr >= &ptrs.end->ptr);
776 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
778 for (next = extent_entry_next(entry);
780 next = extent_entry_next(next)) {
781 if (extent_entry_is_crc(next)) {
783 } else if (extent_entry_is_ptr(next)) {
789 extent_entry_drop(k, entry);
791 while ((entry = extent_entry_prev(ptrs, entry))) {
792 if (extent_entry_is_ptr(entry))
795 if ((extent_entry_is_crc(entry) && drop_crc) ||
796 extent_entry_is_stripe_ptr(entry)) {
797 ret = (void *) ret - extent_entry_bytes(entry);
798 extent_entry_drop(k, entry);
805 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
806 struct bch_extent_ptr *ptr)
808 bool have_dirty = bch2_bkey_dirty_devs(k.s_c).nr;
809 union bch_extent_entry *ret =
810 bch2_bkey_drop_ptr_noerror(k, ptr);
813 * If we deleted all the dirty pointers and there's still cached
814 * pointers, we could set the cached pointers to dirty if they're not
815 * stale - but to do that correctly we'd need to grab an open_bucket
816 * reference so that we don't race with bucket reuse:
819 !bch2_bkey_dirty_devs(k.s_c).nr) {
820 k.k->type = KEY_TYPE_error;
821 set_bkey_val_u64s(k.k, 0);
823 } else if (!bch2_bkey_nr_ptrs(k.s_c)) {
824 k.k->type = KEY_TYPE_deleted;
825 set_bkey_val_u64s(k.k, 0);
832 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
834 struct bch_extent_ptr *ptr;
836 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
839 void bch2_bkey_drop_device_noerror(struct bkey_s k, unsigned dev)
841 struct bch_extent_ptr *ptr = bch2_bkey_has_device(k, dev);
844 bch2_bkey_drop_ptr_noerror(k, ptr);
847 const struct bch_extent_ptr *bch2_bkey_has_device_c(struct bkey_s_c k, unsigned dev)
849 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
850 const struct bch_extent_ptr *ptr;
852 bkey_for_each_ptr(ptrs, ptr)
859 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
861 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
862 const struct bch_extent_ptr *ptr;
864 bkey_for_each_ptr(ptrs, ptr)
865 if (bch2_dev_in_target(c, ptr->dev, target) &&
867 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
873 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
874 struct bch_extent_ptr m, u64 offset)
876 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
877 const union bch_extent_entry *entry;
878 struct extent_ptr_decoded p;
880 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
881 if (p.ptr.dev == m.dev &&
882 p.ptr.gen == m.gen &&
883 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
884 (s64) m.offset - offset)
891 * Returns true if two extents refer to the same data:
893 bool bch2_extents_match(struct bkey_s_c k1, struct bkey_s_c k2)
895 if (k1.k->type != k2.k->type)
898 if (bkey_extent_is_direct_data(k1.k)) {
899 struct bkey_ptrs_c ptrs1 = bch2_bkey_ptrs_c(k1);
900 struct bkey_ptrs_c ptrs2 = bch2_bkey_ptrs_c(k2);
901 const union bch_extent_entry *entry1, *entry2;
902 struct extent_ptr_decoded p1, p2;
904 if (bkey_extent_is_unwritten(k1) != bkey_extent_is_unwritten(k2))
907 bkey_for_each_ptr_decode(k1.k, ptrs1, p1, entry1)
908 bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2)
909 if (p1.ptr.dev == p2.ptr.dev &&
910 p1.ptr.gen == p2.ptr.gen &&
911 (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) ==
912 (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k))
917 /* KEY_TYPE_deleted, etc. */
922 struct bch_extent_ptr *
923 bch2_extent_has_ptr(struct bkey_s_c k1, struct extent_ptr_decoded p1, struct bkey_s k2)
925 struct bkey_ptrs ptrs2 = bch2_bkey_ptrs(k2);
926 union bch_extent_entry *entry2;
927 struct extent_ptr_decoded p2;
929 bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2)
930 if (p1.ptr.dev == p2.ptr.dev &&
931 p1.ptr.gen == p2.ptr.gen &&
932 (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) ==
933 (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k))
939 void bch2_extent_ptr_set_cached(struct bkey_s k, struct bch_extent_ptr *ptr)
941 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
942 union bch_extent_entry *entry;
943 union bch_extent_entry *ec = NULL;
945 bkey_extent_entry_for_each(ptrs, entry) {
946 if (&entry->ptr == ptr) {
949 extent_entry_drop(k, ec);
953 if (extent_entry_is_stripe_ptr(entry))
955 else if (extent_entry_is_ptr(entry))
963 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
965 * Returns true if @k should be dropped entirely
967 * For existing keys, only called when btree nodes are being rewritten, not when
968 * they're merely being compacted/resorted in memory.
970 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
972 struct bch_extent_ptr *ptr;
974 bch2_bkey_drop_ptrs(k, ptr,
976 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
978 return bkey_deleted(k.k);
981 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
984 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
985 const union bch_extent_entry *entry;
986 struct bch_extent_crc_unpacked crc;
987 const struct bch_extent_ptr *ptr;
988 const struct bch_extent_stripe_ptr *ec;
993 prt_printf(out, "durability: %u ", bch2_bkey_durability(c, k));
995 bkey_extent_entry_for_each(ptrs, entry) {
997 prt_printf(out, " ");
999 switch (__extent_entry_type(entry)) {
1000 case BCH_EXTENT_ENTRY_ptr:
1001 ptr = entry_to_ptr(entry);
1002 ca = c && ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
1003 ? bch_dev_bkey_exists(c, ptr->dev)
1007 prt_printf(out, "ptr: %u:%llu gen %u%s", ptr->dev,
1008 (u64) ptr->offset, ptr->gen,
1009 ptr->cached ? " cached" : "");
1012 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
1014 prt_printf(out, "ptr: %u:%llu:%u gen %u",
1015 ptr->dev, b, offset, ptr->gen);
1017 prt_str(out, " cached");
1019 prt_str(out, " unwritten");
1020 if (ca && ptr_stale(ca, ptr))
1021 prt_printf(out, " stale");
1024 case BCH_EXTENT_ENTRY_crc32:
1025 case BCH_EXTENT_ENTRY_crc64:
1026 case BCH_EXTENT_ENTRY_crc128:
1027 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1029 prt_printf(out, "crc: c_size %u size %u offset %u nonce %u csum %s compress %s",
1030 crc.compressed_size,
1031 crc.uncompressed_size,
1032 crc.offset, crc.nonce,
1033 bch2_csum_types[crc.csum_type],
1034 bch2_compression_types[crc.compression_type]);
1036 case BCH_EXTENT_ENTRY_stripe_ptr:
1037 ec = &entry->stripe_ptr;
1039 prt_printf(out, "ec: idx %llu block %u",
1040 (u64) ec->idx, ec->block);
1043 prt_printf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
1051 static int extent_ptr_invalid(const struct bch_fs *c,
1053 const struct bch_extent_ptr *ptr,
1054 unsigned size_ondisk,
1056 struct printbuf *err)
1058 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1059 const struct bch_extent_ptr *ptr2;
1064 if (!bch2_dev_exists2(c, ptr->dev)) {
1065 prt_printf(err, "pointer to invalid device (%u)", ptr->dev);
1066 return -BCH_ERR_invalid_bkey;
1069 ca = bch_dev_bkey_exists(c, ptr->dev);
1070 bkey_for_each_ptr(ptrs, ptr2)
1071 if (ptr != ptr2 && ptr->dev == ptr2->dev) {
1072 prt_printf(err, "multiple pointers to same device (%u)", ptr->dev);
1073 return -BCH_ERR_invalid_bkey;
1076 bucket = sector_to_bucket_and_offset(ca, ptr->offset, &bucket_offset);
1078 if (bucket >= ca->mi.nbuckets) {
1079 prt_printf(err, "pointer past last bucket (%llu > %llu)",
1080 bucket, ca->mi.nbuckets);
1081 return -BCH_ERR_invalid_bkey;
1084 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket)) {
1085 prt_printf(err, "pointer before first bucket (%llu < %u)",
1086 bucket, ca->mi.first_bucket);
1087 return -BCH_ERR_invalid_bkey;
1090 if (bucket_offset + size_ondisk > ca->mi.bucket_size) {
1091 prt_printf(err, "pointer spans multiple buckets (%u + %u > %u)",
1092 bucket_offset, size_ondisk, ca->mi.bucket_size);
1093 return -BCH_ERR_invalid_bkey;
1099 int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
1100 unsigned flags, struct printbuf *err)
1102 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1103 const union bch_extent_entry *entry;
1104 struct bch_extent_crc_unpacked crc;
1105 unsigned size_ondisk = k.k->size;
1106 unsigned nonce = UINT_MAX;
1107 unsigned nr_ptrs = 0;
1108 bool unwritten = false, have_ec = false, crc_since_last_ptr = false;
1111 if (bkey_is_btree_ptr(k.k))
1112 size_ondisk = btree_sectors(c);
1114 bkey_extent_entry_for_each(ptrs, entry) {
1115 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX) {
1116 prt_printf(err, "invalid extent entry type (got %u, max %u)",
1117 __extent_entry_type(entry), BCH_EXTENT_ENTRY_MAX);
1118 return -BCH_ERR_invalid_bkey;
1121 if (bkey_is_btree_ptr(k.k) &&
1122 !extent_entry_is_ptr(entry)) {
1123 prt_printf(err, "has non ptr field");
1124 return -BCH_ERR_invalid_bkey;
1127 switch (extent_entry_type(entry)) {
1128 case BCH_EXTENT_ENTRY_ptr:
1129 ret = extent_ptr_invalid(c, k, &entry->ptr, size_ondisk,
1134 if (nr_ptrs && unwritten != entry->ptr.unwritten) {
1135 prt_printf(err, "extent with unwritten and written ptrs");
1136 return -BCH_ERR_invalid_bkey;
1139 if (k.k->type != KEY_TYPE_extent && entry->ptr.unwritten) {
1140 prt_printf(err, "has unwritten ptrs");
1141 return -BCH_ERR_invalid_bkey;
1144 if (entry->ptr.cached && have_ec) {
1145 prt_printf(err, "cached, erasure coded ptr");
1146 return -BCH_ERR_invalid_bkey;
1149 unwritten = entry->ptr.unwritten;
1151 crc_since_last_ptr = false;
1154 case BCH_EXTENT_ENTRY_crc32:
1155 case BCH_EXTENT_ENTRY_crc64:
1156 case BCH_EXTENT_ENTRY_crc128:
1157 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1159 if (crc.offset + crc.live_size >
1160 crc.uncompressed_size) {
1161 prt_printf(err, "checksum offset + key size > uncompressed size");
1162 return -BCH_ERR_invalid_bkey;
1165 size_ondisk = crc.compressed_size;
1167 if (!bch2_checksum_type_valid(c, crc.csum_type)) {
1168 prt_printf(err, "invalid checksum type");
1169 return -BCH_ERR_invalid_bkey;
1172 if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR) {
1173 prt_printf(err, "invalid compression type");
1174 return -BCH_ERR_invalid_bkey;
1177 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1178 if (nonce == UINT_MAX)
1179 nonce = crc.offset + crc.nonce;
1180 else if (nonce != crc.offset + crc.nonce) {
1181 prt_printf(err, "incorrect nonce");
1182 return -BCH_ERR_invalid_bkey;
1186 if (crc_since_last_ptr) {
1187 prt_printf(err, "redundant crc entry");
1188 return -BCH_ERR_invalid_bkey;
1190 crc_since_last_ptr = true;
1192 case BCH_EXTENT_ENTRY_stripe_ptr:
1194 prt_printf(err, "redundant stripe entry");
1195 return -BCH_ERR_invalid_bkey;
1203 prt_str(err, "no ptrs");
1204 return -BCH_ERR_invalid_bkey;
1207 if (nr_ptrs >= BCH_BKEY_PTRS_MAX) {
1208 prt_str(err, "too many ptrs");
1209 return -BCH_ERR_invalid_bkey;
1212 if (crc_since_last_ptr) {
1213 prt_printf(err, "redundant crc entry");
1214 return -BCH_ERR_invalid_bkey;
1218 prt_printf(err, "redundant stripe entry");
1219 return -BCH_ERR_invalid_bkey;
1225 void bch2_ptr_swab(struct bkey_s k)
1227 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1228 union bch_extent_entry *entry;
1231 for (d = (u64 *) ptrs.start;
1232 d != (u64 *) ptrs.end;
1236 for (entry = ptrs.start;
1238 entry = extent_entry_next(entry)) {
1239 switch (extent_entry_type(entry)) {
1240 case BCH_EXTENT_ENTRY_ptr:
1242 case BCH_EXTENT_ENTRY_crc32:
1243 entry->crc32.csum = swab32(entry->crc32.csum);
1245 case BCH_EXTENT_ENTRY_crc64:
1246 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1247 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1249 case BCH_EXTENT_ENTRY_crc128:
1250 entry->crc128.csum.hi = (__force __le64)
1251 swab64((__force u64) entry->crc128.csum.hi);
1252 entry->crc128.csum.lo = (__force __le64)
1253 swab64((__force u64) entry->crc128.csum.lo);
1255 case BCH_EXTENT_ENTRY_stripe_ptr:
1261 /* Generic extent code: */
1263 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1265 unsigned new_val_u64s = bkey_val_u64s(k.k);
1269 if (bkey_le(where, bkey_start_pos(k.k)))
1272 EBUG_ON(bkey_gt(where, k.k->p));
1274 sub = where.offset - bkey_start_offset(k.k);
1279 k.k->type = KEY_TYPE_deleted;
1283 switch (k.k->type) {
1284 case KEY_TYPE_extent:
1285 case KEY_TYPE_reflink_v: {
1286 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1287 union bch_extent_entry *entry;
1288 bool seen_crc = false;
1290 bkey_extent_entry_for_each(ptrs, entry) {
1291 switch (extent_entry_type(entry)) {
1292 case BCH_EXTENT_ENTRY_ptr:
1294 entry->ptr.offset += sub;
1296 case BCH_EXTENT_ENTRY_crc32:
1297 entry->crc32.offset += sub;
1299 case BCH_EXTENT_ENTRY_crc64:
1300 entry->crc64.offset += sub;
1302 case BCH_EXTENT_ENTRY_crc128:
1303 entry->crc128.offset += sub;
1305 case BCH_EXTENT_ENTRY_stripe_ptr:
1309 if (extent_entry_is_crc(entry))
1315 case KEY_TYPE_reflink_p: {
1316 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1318 le64_add_cpu(&p.v->idx, sub);
1321 case KEY_TYPE_inline_data:
1322 case KEY_TYPE_indirect_inline_data: {
1323 void *p = bkey_inline_data_p(k);
1324 unsigned bytes = bkey_inline_data_bytes(k.k);
1326 sub = min_t(u64, sub << 9, bytes);
1328 memmove(p, p + sub, bytes - sub);
1330 new_val_u64s -= sub >> 3;
1335 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1336 BUG_ON(val_u64s_delta < 0);
1338 set_bkey_val_u64s(k.k, new_val_u64s);
1339 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1340 return -val_u64s_delta;
1343 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1345 unsigned new_val_u64s = bkey_val_u64s(k.k);
1349 if (bkey_ge(where, k.k->p))
1352 EBUG_ON(bkey_lt(where, bkey_start_pos(k.k)));
1354 len = where.offset - bkey_start_offset(k.k);
1356 k.k->p.offset = where.offset;
1360 k.k->type = KEY_TYPE_deleted;
1364 switch (k.k->type) {
1365 case KEY_TYPE_inline_data:
1366 case KEY_TYPE_indirect_inline_data:
1367 new_val_u64s = (bkey_inline_data_offset(k.k) +
1368 min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
1372 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1373 BUG_ON(val_u64s_delta < 0);
1375 set_bkey_val_u64s(k.k, new_val_u64s);
1376 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1377 return -val_u64s_delta;