1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
5 * Code for managing the extent btree and dynamically updating the writeback
10 #include "bkey_methods.h"
13 #include "btree_iter.h"
17 #include "disk_groups.h"
28 static unsigned bch2_crc_field_size_max[] = {
29 [BCH_EXTENT_ENTRY_crc32] = CRC32_SIZE_MAX,
30 [BCH_EXTENT_ENTRY_crc64] = CRC64_SIZE_MAX,
31 [BCH_EXTENT_ENTRY_crc128] = CRC128_SIZE_MAX,
34 static void bch2_extent_crc_pack(union bch_extent_crc *,
35 struct bch_extent_crc_unpacked,
36 enum bch_extent_entry_type);
38 static struct bch_dev_io_failures *dev_io_failures(struct bch_io_failures *f,
41 struct bch_dev_io_failures *i;
43 for (i = f->devs; i < f->devs + f->nr; i++)
50 void bch2_mark_io_failure(struct bch_io_failures *failed,
51 struct extent_ptr_decoded *p)
53 struct bch_dev_io_failures *f = dev_io_failures(failed, p->ptr.dev);
56 BUG_ON(failed->nr >= ARRAY_SIZE(failed->devs));
58 f = &failed->devs[failed->nr++];
63 } else if (p->idx != f->idx) {
73 * returns true if p1 is better than p2:
75 static inline bool ptr_better(struct bch_fs *c,
76 const struct extent_ptr_decoded p1,
77 const struct extent_ptr_decoded p2)
79 if (likely(!p1.idx && !p2.idx)) {
80 struct bch_dev *dev1 = bch_dev_bkey_exists(c, p1.ptr.dev);
81 struct bch_dev *dev2 = bch_dev_bkey_exists(c, p2.ptr.dev);
83 u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
84 u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
86 /* Pick at random, biased in favor of the faster device: */
88 return bch2_rand_range(l1 + l2) > l1;
91 if (bch2_force_reconstruct_read)
92 return p1.idx > p2.idx;
94 return p1.idx < p2.idx;
98 * This picks a non-stale pointer, preferably from a device other than @avoid.
99 * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to
100 * other devices, it will still pick a pointer from avoid.
102 int bch2_bkey_pick_read_device(struct bch_fs *c, struct bkey_s_c k,
103 struct bch_io_failures *failed,
104 struct extent_ptr_decoded *pick)
106 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
107 const union bch_extent_entry *entry;
108 struct extent_ptr_decoded p;
109 struct bch_dev_io_failures *f;
113 if (k.k->type == KEY_TYPE_error)
116 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
118 * Unwritten extent: no need to actually read, treat it as a
119 * hole and return 0s:
124 ca = bch_dev_bkey_exists(c, p.ptr.dev);
127 * If there are any dirty pointers it's an error if we can't
130 if (!ret && !p.ptr.cached)
133 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
136 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
138 p.idx = f->nr_failed < f->nr_retries
143 !bch2_dev_is_readable(ca))
146 if (bch2_force_reconstruct_read &&
150 if (p.idx >= (unsigned) p.has_ec + 1)
153 if (ret > 0 && !ptr_better(c, p, *pick))
163 /* KEY_TYPE_btree_ptr: */
165 int bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k,
166 unsigned flags, struct printbuf *err)
168 if (bkey_val_u64s(k.k) > BCH_REPLICAS_MAX) {
169 prt_printf(err, "value too big (%zu > %u)",
170 bkey_val_u64s(k.k), BCH_REPLICAS_MAX);
171 return -BCH_ERR_invalid_bkey;
174 return bch2_bkey_ptrs_invalid(c, k, flags, err);
177 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
180 bch2_bkey_ptrs_to_text(out, c, k);
183 int bch2_btree_ptr_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
184 unsigned flags, struct printbuf *err)
186 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX) {
187 prt_printf(err, "value too big (%zu > %zu)",
188 bkey_val_u64s(k.k), BKEY_BTREE_PTR_VAL_U64s_MAX);
189 return -BCH_ERR_invalid_bkey;
192 return bch2_bkey_ptrs_invalid(c, k, flags, err);
195 void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c,
198 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
200 prt_printf(out, "seq %llx written %u min_key %s",
201 le64_to_cpu(bp.v->seq),
202 le16_to_cpu(bp.v->sectors_written),
203 BTREE_PTR_RANGE_UPDATED(bp.v) ? "R " : "");
205 bch2_bpos_to_text(out, bp.v->min_key);
206 prt_printf(out, " ");
207 bch2_bkey_ptrs_to_text(out, c, k);
210 void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version,
211 unsigned big_endian, int write,
214 struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k);
216 compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key);
218 if (version < bcachefs_metadata_version_inode_btree_change &&
219 btree_node_type_is_extents(btree_id) &&
220 !bkey_eq(bp.v->min_key, POS_MIN))
221 bp.v->min_key = write
222 ? bpos_nosnap_predecessor(bp.v->min_key)
223 : bpos_nosnap_successor(bp.v->min_key);
226 /* KEY_TYPE_extent: */
228 bool bch2_extent_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
230 struct bkey_ptrs l_ptrs = bch2_bkey_ptrs(l);
231 struct bkey_ptrs_c r_ptrs = bch2_bkey_ptrs_c(r);
232 union bch_extent_entry *en_l;
233 const union bch_extent_entry *en_r;
234 struct extent_ptr_decoded lp, rp;
240 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
241 if (extent_entry_type(en_l) != extent_entry_type(en_r))
244 en_l = extent_entry_next(en_l);
245 en_r = extent_entry_next(en_r);
248 if (en_l < l_ptrs.end || en_r < r_ptrs.end)
253 lp.crc = bch2_extent_crc_unpack(l.k, NULL);
254 rp.crc = bch2_extent_crc_unpack(r.k, NULL);
256 while (__bkey_ptr_next_decode(l.k, l_ptrs.end, lp, en_l) &&
257 __bkey_ptr_next_decode(r.k, r_ptrs.end, rp, en_r)) {
258 if (lp.ptr.offset + lp.crc.offset + lp.crc.live_size !=
259 rp.ptr.offset + rp.crc.offset ||
260 lp.ptr.dev != rp.ptr.dev ||
261 lp.ptr.gen != rp.ptr.gen ||
262 lp.ptr.unwritten != rp.ptr.unwritten ||
263 lp.has_ec != rp.has_ec)
266 /* Extents may not straddle buckets: */
267 ca = bch_dev_bkey_exists(c, lp.ptr.dev);
268 if (PTR_BUCKET_NR(ca, &lp.ptr) != PTR_BUCKET_NR(ca, &rp.ptr))
271 if (lp.has_ec != rp.has_ec ||
273 (lp.ec.block != rp.ec.block ||
274 lp.ec.redundancy != rp.ec.redundancy ||
275 lp.ec.idx != rp.ec.idx)))
278 if (lp.crc.compression_type != rp.crc.compression_type ||
279 lp.crc.nonce != rp.crc.nonce)
282 if (lp.crc.offset + lp.crc.live_size + rp.crc.live_size <=
283 lp.crc.uncompressed_size) {
284 /* can use left extent's crc entry */
285 } else if (lp.crc.live_size <= rp.crc.offset) {
286 /* can use right extent's crc entry */
288 /* check if checksums can be merged: */
289 if (lp.crc.csum_type != rp.crc.csum_type ||
290 lp.crc.nonce != rp.crc.nonce ||
291 crc_is_compressed(lp.crc) ||
292 !bch2_checksum_mergeable(lp.crc.csum_type))
295 if (lp.crc.offset + lp.crc.live_size != lp.crc.compressed_size ||
299 if (lp.crc.csum_type &&
300 lp.crc.uncompressed_size +
301 rp.crc.uncompressed_size > (c->opts.encoded_extent_max >> 9))
305 en_l = extent_entry_next(en_l);
306 en_r = extent_entry_next(en_r);
311 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
312 if (extent_entry_is_crc(en_l)) {
313 struct bch_extent_crc_unpacked crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
314 struct bch_extent_crc_unpacked crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
316 if (crc_l.uncompressed_size + crc_r.uncompressed_size >
317 bch2_crc_field_size_max[extent_entry_type(en_l)])
321 en_l = extent_entry_next(en_l);
322 en_r = extent_entry_next(en_r);
325 use_right_ptr = false;
328 while (en_l < l_ptrs.end) {
329 if (extent_entry_type(en_l) == BCH_EXTENT_ENTRY_ptr &&
331 en_l->ptr = en_r->ptr;
333 if (extent_entry_is_crc(en_l)) {
334 struct bch_extent_crc_unpacked crc_l =
335 bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
336 struct bch_extent_crc_unpacked crc_r =
337 bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
339 use_right_ptr = false;
341 if (crc_l.offset + crc_l.live_size + crc_r.live_size <=
342 crc_l.uncompressed_size) {
343 /* can use left extent's crc entry */
344 } else if (crc_l.live_size <= crc_r.offset) {
345 /* can use right extent's crc entry */
346 crc_r.offset -= crc_l.live_size;
347 bch2_extent_crc_pack(entry_to_crc(en_l), crc_r,
348 extent_entry_type(en_l));
349 use_right_ptr = true;
351 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
354 crc_r.uncompressed_size << 9);
356 crc_l.uncompressed_size += crc_r.uncompressed_size;
357 crc_l.compressed_size += crc_r.compressed_size;
358 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l,
359 extent_entry_type(en_l));
363 en_l = extent_entry_next(en_l);
364 en_r = extent_entry_next(en_r);
367 bch2_key_resize(l.k, l.k->size + r.k->size);
371 /* KEY_TYPE_reservation: */
373 int bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k,
374 unsigned flags, struct printbuf *err)
376 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
378 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX) {
379 prt_printf(err, "invalid nr_replicas (%u)",
381 return -BCH_ERR_invalid_bkey;
387 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
390 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
392 prt_printf(out, "generation %u replicas %u",
393 le32_to_cpu(r.v->generation),
397 bool bch2_reservation_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r)
399 struct bkey_s_reservation l = bkey_s_to_reservation(_l);
400 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(_r);
402 if (l.v->generation != r.v->generation ||
403 l.v->nr_replicas != r.v->nr_replicas)
406 bch2_key_resize(l.k, l.k->size + r.k->size);
410 /* Extent checksum entries: */
412 /* returns true if not equal */
413 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
414 struct bch_extent_crc_unpacked r)
416 return (l.csum_type != r.csum_type ||
417 l.compression_type != r.compression_type ||
418 l.compressed_size != r.compressed_size ||
419 l.uncompressed_size != r.uncompressed_size ||
420 l.offset != r.offset ||
421 l.live_size != r.live_size ||
422 l.nonce != r.nonce ||
423 bch2_crc_cmp(l.csum, r.csum));
426 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
427 struct bch_extent_crc_unpacked n)
429 return !crc_is_compressed(u) &&
431 u.uncompressed_size > u.live_size &&
432 bch2_csum_type_is_encryption(u.csum_type) ==
433 bch2_csum_type_is_encryption(n.csum_type);
436 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
437 struct bch_extent_crc_unpacked n)
439 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
440 struct bch_extent_crc_unpacked crc;
441 const union bch_extent_entry *i;
446 bkey_for_each_crc(k.k, ptrs, crc, i)
447 if (can_narrow_crc(crc, n))
454 * We're writing another replica for this extent, so while we've got the data in
455 * memory we'll be computing a new checksum for the currently live data.
457 * If there are other replicas we aren't moving, and they are checksummed but
458 * not compressed, we can modify them to point to only the data that is
459 * currently live (so that readers won't have to bounce) while we've got the
462 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
464 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
465 struct bch_extent_crc_unpacked u;
466 struct extent_ptr_decoded p;
467 union bch_extent_entry *i;
470 /* Find a checksum entry that covers only live data: */
472 bkey_for_each_crc(&k->k, ptrs, u, i)
473 if (!crc_is_compressed(u) &&
475 u.live_size == u.uncompressed_size) {
482 BUG_ON(crc_is_compressed(n));
484 BUG_ON(n.live_size != k->k.size);
486 restart_narrow_pointers:
487 ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
489 bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
490 if (can_narrow_crc(p.crc, n)) {
491 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(k), &i->ptr);
492 p.ptr.offset += p.crc.offset;
494 bch2_extent_ptr_decoded_append(k, &p);
496 goto restart_narrow_pointers;
502 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
503 struct bch_extent_crc_unpacked src,
504 enum bch_extent_entry_type type)
506 #define set_common_fields(_dst, _src) \
507 _dst.type = 1 << type; \
508 _dst.csum_type = _src.csum_type, \
509 _dst.compression_type = _src.compression_type, \
510 _dst._compressed_size = _src.compressed_size - 1, \
511 _dst._uncompressed_size = _src.uncompressed_size - 1, \
512 _dst.offset = _src.offset
515 case BCH_EXTENT_ENTRY_crc32:
516 set_common_fields(dst->crc32, src);
517 dst->crc32.csum = *((__le32 *) &src.csum.lo);
519 case BCH_EXTENT_ENTRY_crc64:
520 set_common_fields(dst->crc64, src);
521 dst->crc64.nonce = src.nonce;
522 dst->crc64.csum_lo = src.csum.lo;
523 dst->crc64.csum_hi = *((__le16 *) &src.csum.hi);
525 case BCH_EXTENT_ENTRY_crc128:
526 set_common_fields(dst->crc128, src);
527 dst->crc128.nonce = src.nonce;
528 dst->crc128.csum = src.csum;
533 #undef set_common_fields
536 void bch2_extent_crc_append(struct bkey_i *k,
537 struct bch_extent_crc_unpacked new)
539 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
540 union bch_extent_crc *crc = (void *) ptrs.end;
541 enum bch_extent_entry_type type;
543 if (bch_crc_bytes[new.csum_type] <= 4 &&
544 new.uncompressed_size <= CRC32_SIZE_MAX &&
545 new.nonce <= CRC32_NONCE_MAX)
546 type = BCH_EXTENT_ENTRY_crc32;
547 else if (bch_crc_bytes[new.csum_type] <= 10 &&
548 new.uncompressed_size <= CRC64_SIZE_MAX &&
549 new.nonce <= CRC64_NONCE_MAX)
550 type = BCH_EXTENT_ENTRY_crc64;
551 else if (bch_crc_bytes[new.csum_type] <= 16 &&
552 new.uncompressed_size <= CRC128_SIZE_MAX &&
553 new.nonce <= CRC128_NONCE_MAX)
554 type = BCH_EXTENT_ENTRY_crc128;
558 bch2_extent_crc_pack(crc, new, type);
560 k->k.u64s += extent_entry_u64s(ptrs.end);
562 EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
565 /* Generic code for keys with pointers: */
567 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
569 return bch2_bkey_devs(k).nr;
572 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
574 return k.k->type == KEY_TYPE_reservation
575 ? bkey_s_c_to_reservation(k).v->nr_replicas
576 : bch2_bkey_dirty_devs(k).nr;
579 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
583 if (k.k->type == KEY_TYPE_reservation) {
584 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
586 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
587 const union bch_extent_entry *entry;
588 struct extent_ptr_decoded p;
590 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
591 ret += !p.ptr.cached && !crc_is_compressed(p.crc);
597 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
599 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
600 const union bch_extent_entry *entry;
601 struct extent_ptr_decoded p;
604 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
605 if (!p.ptr.cached && crc_is_compressed(p.crc))
606 ret += p.crc.compressed_size;
611 bool bch2_bkey_is_incompressible(struct bkey_s_c k)
613 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
614 const union bch_extent_entry *entry;
615 struct bch_extent_crc_unpacked crc;
617 bkey_for_each_crc(k.k, ptrs, crc, entry)
618 if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
623 unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
625 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
626 const union bch_extent_entry *entry;
627 struct extent_ptr_decoded p = { 0 };
628 unsigned replicas = 0;
630 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
635 replicas += p.ec.redundancy;
644 unsigned bch2_extent_ptr_durability(struct bch_fs *c, struct extent_ptr_decoded *p)
646 unsigned durability = 0;
652 ca = bch_dev_bkey_exists(c, p->ptr.dev);
654 if (ca->mi.state != BCH_MEMBER_STATE_failed)
655 durability = max_t(unsigned, durability, ca->mi.durability);
658 durability += p->ec.redundancy;
663 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
665 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
666 const union bch_extent_entry *entry;
667 struct extent_ptr_decoded p;
668 unsigned durability = 0;
670 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
671 durability += bch2_extent_ptr_durability(c, &p);
676 static unsigned bch2_bkey_durability_safe(struct bch_fs *c, struct bkey_s_c k)
678 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
679 const union bch_extent_entry *entry;
680 struct extent_ptr_decoded p;
681 unsigned durability = 0;
683 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
684 if (p.ptr.dev < c->sb.nr_devices && c->devs[p.ptr.dev])
685 durability += bch2_extent_ptr_durability(c, &p);
690 void bch2_bkey_extent_entry_drop(struct bkey_i *k, union bch_extent_entry *entry)
692 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
693 union bch_extent_entry *next = extent_entry_next(entry);
695 memmove_u64s(entry, next, (u64 *) end - (u64 *) next);
696 k->k.u64s -= extent_entry_u64s(entry);
699 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
700 struct extent_ptr_decoded *p)
702 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
703 struct bch_extent_crc_unpacked crc =
704 bch2_extent_crc_unpack(&k->k, NULL);
705 union bch_extent_entry *pos;
707 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
712 bkey_for_each_crc(&k->k, ptrs, crc, pos)
713 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
714 pos = extent_entry_next(pos);
718 bch2_extent_crc_append(k, p->crc);
719 pos = bkey_val_end(bkey_i_to_s(k));
721 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
722 __extent_entry_insert(k, pos, to_entry(&p->ptr));
725 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
726 __extent_entry_insert(k, pos, to_entry(&p->ec));
730 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
731 union bch_extent_entry *entry)
733 union bch_extent_entry *i = ptrs.start;
738 while (extent_entry_next(i) != entry)
739 i = extent_entry_next(i);
743 static void extent_entry_drop(struct bkey_s k, union bch_extent_entry *entry)
745 union bch_extent_entry *next = extent_entry_next(entry);
747 /* stripes have ptrs, but their layout doesn't work with this code */
748 BUG_ON(k.k->type == KEY_TYPE_stripe);
750 memmove_u64s_down(entry, next,
751 (u64 *) bkey_val_end(k) - (u64 *) next);
752 k.k->u64s -= (u64 *) next - (u64 *) entry;
756 * Returns pointer to the next entry after the one being dropped:
758 union bch_extent_entry *bch2_bkey_drop_ptr_noerror(struct bkey_s k,
759 struct bch_extent_ptr *ptr)
761 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
762 union bch_extent_entry *entry = to_entry(ptr), *next;
763 union bch_extent_entry *ret = entry;
764 bool drop_crc = true;
766 EBUG_ON(ptr < &ptrs.start->ptr ||
767 ptr >= &ptrs.end->ptr);
768 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
770 for (next = extent_entry_next(entry);
772 next = extent_entry_next(next)) {
773 if (extent_entry_is_crc(next)) {
775 } else if (extent_entry_is_ptr(next)) {
781 extent_entry_drop(k, entry);
783 while ((entry = extent_entry_prev(ptrs, entry))) {
784 if (extent_entry_is_ptr(entry))
787 if ((extent_entry_is_crc(entry) && drop_crc) ||
788 extent_entry_is_stripe_ptr(entry)) {
789 ret = (void *) ret - extent_entry_bytes(entry);
790 extent_entry_drop(k, entry);
797 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
798 struct bch_extent_ptr *ptr)
800 bool have_dirty = bch2_bkey_dirty_devs(k.s_c).nr;
801 union bch_extent_entry *ret =
802 bch2_bkey_drop_ptr_noerror(k, ptr);
805 * If we deleted all the dirty pointers and there's still cached
806 * pointers, we could set the cached pointers to dirty if they're not
807 * stale - but to do that correctly we'd need to grab an open_bucket
808 * reference so that we don't race with bucket reuse:
811 !bch2_bkey_dirty_devs(k.s_c).nr) {
812 k.k->type = KEY_TYPE_error;
813 set_bkey_val_u64s(k.k, 0);
815 } else if (!bch2_bkey_nr_ptrs(k.s_c)) {
816 k.k->type = KEY_TYPE_deleted;
817 set_bkey_val_u64s(k.k, 0);
824 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
826 struct bch_extent_ptr *ptr;
828 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
831 void bch2_bkey_drop_device_noerror(struct bkey_s k, unsigned dev)
833 struct bch_extent_ptr *ptr = bch2_bkey_has_device(k, dev);
836 bch2_bkey_drop_ptr_noerror(k, ptr);
839 const struct bch_extent_ptr *bch2_bkey_has_device_c(struct bkey_s_c k, unsigned dev)
841 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
842 const struct bch_extent_ptr *ptr;
844 bkey_for_each_ptr(ptrs, ptr)
851 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
853 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
854 const struct bch_extent_ptr *ptr;
856 bkey_for_each_ptr(ptrs, ptr)
857 if (bch2_dev_in_target(c, ptr->dev, target) &&
859 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
865 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
866 struct bch_extent_ptr m, u64 offset)
868 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
869 const union bch_extent_entry *entry;
870 struct extent_ptr_decoded p;
872 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
873 if (p.ptr.dev == m.dev &&
874 p.ptr.gen == m.gen &&
875 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
876 (s64) m.offset - offset)
883 * Returns true if two extents refer to the same data:
885 bool bch2_extents_match(struct bkey_s_c k1, struct bkey_s_c k2)
887 if (k1.k->type != k2.k->type)
890 if (bkey_extent_is_direct_data(k1.k)) {
891 struct bkey_ptrs_c ptrs1 = bch2_bkey_ptrs_c(k1);
892 struct bkey_ptrs_c ptrs2 = bch2_bkey_ptrs_c(k2);
893 const union bch_extent_entry *entry1, *entry2;
894 struct extent_ptr_decoded p1, p2;
896 if (bkey_extent_is_unwritten(k1) != bkey_extent_is_unwritten(k2))
899 bkey_for_each_ptr_decode(k1.k, ptrs1, p1, entry1)
900 bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2)
901 if (p1.ptr.dev == p2.ptr.dev &&
902 p1.ptr.gen == p2.ptr.gen &&
903 (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) ==
904 (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k))
909 /* KEY_TYPE_deleted, etc. */
914 struct bch_extent_ptr *
915 bch2_extent_has_ptr(struct bkey_s_c k1, struct extent_ptr_decoded p1, struct bkey_s k2)
917 struct bkey_ptrs ptrs2 = bch2_bkey_ptrs(k2);
918 union bch_extent_entry *entry2;
919 struct extent_ptr_decoded p2;
921 bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2)
922 if (p1.ptr.dev == p2.ptr.dev &&
923 p1.ptr.gen == p2.ptr.gen &&
924 (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) ==
925 (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k))
931 void bch2_extent_ptr_set_cached(struct bkey_s k, struct bch_extent_ptr *ptr)
933 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
934 union bch_extent_entry *entry;
935 union bch_extent_entry *ec = NULL;
937 bkey_extent_entry_for_each(ptrs, entry) {
938 if (&entry->ptr == ptr) {
941 extent_entry_drop(k, ec);
945 if (extent_entry_is_stripe_ptr(entry))
947 else if (extent_entry_is_ptr(entry))
955 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
957 * Returns true if @k should be dropped entirely
959 * For existing keys, only called when btree nodes are being rewritten, not when
960 * they're merely being compacted/resorted in memory.
962 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
964 struct bch_extent_ptr *ptr;
966 bch2_bkey_drop_ptrs(k, ptr,
968 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
970 return bkey_deleted(k.k);
973 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
976 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
977 const union bch_extent_entry *entry;
978 struct bch_extent_crc_unpacked crc;
979 const struct bch_extent_ptr *ptr;
980 const struct bch_extent_stripe_ptr *ec;
985 prt_printf(out, "durability: %u ", bch2_bkey_durability_safe(c, k));
987 bkey_extent_entry_for_each(ptrs, entry) {
989 prt_printf(out, " ");
991 switch (__extent_entry_type(entry)) {
992 case BCH_EXTENT_ENTRY_ptr:
993 ptr = entry_to_ptr(entry);
994 ca = c && ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
995 ? bch_dev_bkey_exists(c, ptr->dev)
999 prt_printf(out, "ptr: %u:%llu gen %u%s", ptr->dev,
1000 (u64) ptr->offset, ptr->gen,
1001 ptr->cached ? " cached" : "");
1004 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
1006 prt_printf(out, "ptr: %u:%llu:%u gen %u",
1007 ptr->dev, b, offset, ptr->gen);
1009 prt_str(out, " cached");
1011 prt_str(out, " unwritten");
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);
1058 return -BCH_ERR_invalid_bkey;
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);
1065 return -BCH_ERR_invalid_bkey;
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);
1073 return -BCH_ERR_invalid_bkey;
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);
1079 return -BCH_ERR_invalid_bkey;
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);
1085 return -BCH_ERR_invalid_bkey;
1091 int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
1092 unsigned flags, 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;
1100 bool unwritten = false, have_ec = false, crc_since_last_ptr = false;
1103 if (bkey_is_btree_ptr(k.k))
1104 size_ondisk = btree_sectors(c);
1106 bkey_extent_entry_for_each(ptrs, entry) {
1107 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX) {
1108 prt_printf(err, "invalid extent entry type (got %u, max %u)",
1109 __extent_entry_type(entry), BCH_EXTENT_ENTRY_MAX);
1110 return -BCH_ERR_invalid_bkey;
1113 if (bkey_is_btree_ptr(k.k) &&
1114 !extent_entry_is_ptr(entry)) {
1115 prt_printf(err, "has non ptr field");
1116 return -BCH_ERR_invalid_bkey;
1119 switch (extent_entry_type(entry)) {
1120 case BCH_EXTENT_ENTRY_ptr:
1121 ret = extent_ptr_invalid(c, k, &entry->ptr, size_ondisk,
1126 if (nr_ptrs && unwritten != entry->ptr.unwritten) {
1127 prt_printf(err, "extent with unwritten and written ptrs");
1128 return -BCH_ERR_invalid_bkey;
1131 if (k.k->type != KEY_TYPE_extent && entry->ptr.unwritten) {
1132 prt_printf(err, "has unwritten ptrs");
1133 return -BCH_ERR_invalid_bkey;
1136 if (entry->ptr.cached && have_ec) {
1137 prt_printf(err, "cached, erasure coded ptr");
1138 return -BCH_ERR_invalid_bkey;
1141 unwritten = entry->ptr.unwritten;
1143 crc_since_last_ptr = false;
1146 case BCH_EXTENT_ENTRY_crc32:
1147 case BCH_EXTENT_ENTRY_crc64:
1148 case BCH_EXTENT_ENTRY_crc128:
1149 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1151 if (crc.offset + crc.live_size >
1152 crc.uncompressed_size) {
1153 prt_printf(err, "checksum offset + key size > uncompressed size");
1154 return -BCH_ERR_invalid_bkey;
1157 size_ondisk = crc.compressed_size;
1159 if (!bch2_checksum_type_valid(c, crc.csum_type)) {
1160 prt_printf(err, "invalid checksum type");
1161 return -BCH_ERR_invalid_bkey;
1164 if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR) {
1165 prt_printf(err, "invalid compression type");
1166 return -BCH_ERR_invalid_bkey;
1169 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1170 if (nonce == UINT_MAX)
1171 nonce = crc.offset + crc.nonce;
1172 else if (nonce != crc.offset + crc.nonce) {
1173 prt_printf(err, "incorrect nonce");
1174 return -BCH_ERR_invalid_bkey;
1178 if (crc_since_last_ptr) {
1179 prt_printf(err, "redundant crc entry");
1180 return -BCH_ERR_invalid_bkey;
1182 crc_since_last_ptr = true;
1184 case BCH_EXTENT_ENTRY_stripe_ptr:
1186 prt_printf(err, "redundant stripe entry");
1187 return -BCH_ERR_invalid_bkey;
1195 prt_str(err, "no ptrs");
1196 return -BCH_ERR_invalid_bkey;
1199 if (nr_ptrs >= BCH_BKEY_PTRS_MAX) {
1200 prt_str(err, "too many ptrs");
1201 return -BCH_ERR_invalid_bkey;
1204 if (crc_since_last_ptr) {
1205 prt_printf(err, "redundant crc entry");
1206 return -BCH_ERR_invalid_bkey;
1210 prt_printf(err, "redundant stripe entry");
1211 return -BCH_ERR_invalid_bkey;
1217 void bch2_ptr_swab(struct bkey_s k)
1219 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1220 union bch_extent_entry *entry;
1223 for (d = (u64 *) ptrs.start;
1224 d != (u64 *) ptrs.end;
1228 for (entry = ptrs.start;
1230 entry = extent_entry_next(entry)) {
1231 switch (extent_entry_type(entry)) {
1232 case BCH_EXTENT_ENTRY_ptr:
1234 case BCH_EXTENT_ENTRY_crc32:
1235 entry->crc32.csum = swab32(entry->crc32.csum);
1237 case BCH_EXTENT_ENTRY_crc64:
1238 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1239 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1241 case BCH_EXTENT_ENTRY_crc128:
1242 entry->crc128.csum.hi = (__force __le64)
1243 swab64((__force u64) entry->crc128.csum.hi);
1244 entry->crc128.csum.lo = (__force __le64)
1245 swab64((__force u64) entry->crc128.csum.lo);
1247 case BCH_EXTENT_ENTRY_stripe_ptr:
1253 /* Generic extent code: */
1255 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1257 unsigned new_val_u64s = bkey_val_u64s(k.k);
1261 if (bkey_le(where, bkey_start_pos(k.k)))
1264 EBUG_ON(bkey_gt(where, k.k->p));
1266 sub = where.offset - bkey_start_offset(k.k);
1271 k.k->type = KEY_TYPE_deleted;
1275 switch (k.k->type) {
1276 case KEY_TYPE_extent:
1277 case KEY_TYPE_reflink_v: {
1278 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1279 union bch_extent_entry *entry;
1280 bool seen_crc = false;
1282 bkey_extent_entry_for_each(ptrs, entry) {
1283 switch (extent_entry_type(entry)) {
1284 case BCH_EXTENT_ENTRY_ptr:
1286 entry->ptr.offset += sub;
1288 case BCH_EXTENT_ENTRY_crc32:
1289 entry->crc32.offset += sub;
1291 case BCH_EXTENT_ENTRY_crc64:
1292 entry->crc64.offset += sub;
1294 case BCH_EXTENT_ENTRY_crc128:
1295 entry->crc128.offset += sub;
1297 case BCH_EXTENT_ENTRY_stripe_ptr:
1301 if (extent_entry_is_crc(entry))
1307 case KEY_TYPE_reflink_p: {
1308 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1310 le64_add_cpu(&p.v->idx, sub);
1313 case KEY_TYPE_inline_data:
1314 case KEY_TYPE_indirect_inline_data: {
1315 void *p = bkey_inline_data_p(k);
1316 unsigned bytes = bkey_inline_data_bytes(k.k);
1318 sub = min_t(u64, sub << 9, bytes);
1320 memmove(p, p + sub, bytes - sub);
1322 new_val_u64s -= sub >> 3;
1327 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1328 BUG_ON(val_u64s_delta < 0);
1330 set_bkey_val_u64s(k.k, new_val_u64s);
1331 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1332 return -val_u64s_delta;
1335 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1337 unsigned new_val_u64s = bkey_val_u64s(k.k);
1341 if (bkey_ge(where, k.k->p))
1344 EBUG_ON(bkey_lt(where, bkey_start_pos(k.k)));
1346 len = where.offset - bkey_start_offset(k.k);
1348 k.k->p.offset = where.offset;
1352 k.k->type = KEY_TYPE_deleted;
1356 switch (k.k->type) {
1357 case KEY_TYPE_inline_data:
1358 case KEY_TYPE_indirect_inline_data:
1359 new_val_u64s = (bkey_inline_data_offset(k.k) +
1360 min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
1364 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1365 BUG_ON(val_u64s_delta < 0);
1367 set_bkey_val_u64s(k.k, new_val_u64s);
1368 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1369 return -val_u64s_delta;