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"
18 #include "disk_groups.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 enum bkey_invalid_flags flags,
168 struct printbuf *err)
170 if (bkey_val_u64s(k.k) > BCH_REPLICAS_MAX) {
171 prt_printf(err, "value too big (%zu > %u)",
172 bkey_val_u64s(k.k), BCH_REPLICAS_MAX);
173 return -BCH_ERR_invalid_bkey;
176 return bch2_bkey_ptrs_invalid(c, k, flags, err);
179 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
182 bch2_bkey_ptrs_to_text(out, c, k);
185 int bch2_btree_ptr_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
186 enum bkey_invalid_flags flags,
187 struct printbuf *err)
189 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX) {
190 prt_printf(err, "value too big (%zu > %zu)",
191 bkey_val_u64s(k.k), BKEY_BTREE_PTR_VAL_U64s_MAX);
192 return -BCH_ERR_invalid_bkey;
195 return bch2_bkey_ptrs_invalid(c, k, flags, err);
198 void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c,
201 struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
203 prt_printf(out, "seq %llx written %u min_key %s",
204 le64_to_cpu(bp.v->seq),
205 le16_to_cpu(bp.v->sectors_written),
206 BTREE_PTR_RANGE_UPDATED(bp.v) ? "R " : "");
208 bch2_bpos_to_text(out, bp.v->min_key);
209 prt_printf(out, " ");
210 bch2_bkey_ptrs_to_text(out, c, k);
213 void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version,
214 unsigned big_endian, int write,
217 struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k);
219 compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key);
221 if (version < bcachefs_metadata_version_inode_btree_change &&
222 btree_id_is_extents(btree_id) &&
223 !bkey_eq(bp.v->min_key, POS_MIN))
224 bp.v->min_key = write
225 ? bpos_nosnap_predecessor(bp.v->min_key)
226 : bpos_nosnap_successor(bp.v->min_key);
229 /* KEY_TYPE_extent: */
231 bool bch2_extent_merge(struct bch_fs *c, struct bkey_s l, struct bkey_s_c r)
233 struct bkey_ptrs l_ptrs = bch2_bkey_ptrs(l);
234 struct bkey_ptrs_c r_ptrs = bch2_bkey_ptrs_c(r);
235 union bch_extent_entry *en_l;
236 const union bch_extent_entry *en_r;
237 struct extent_ptr_decoded lp, rp;
243 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
244 if (extent_entry_type(en_l) != extent_entry_type(en_r))
247 en_l = extent_entry_next(en_l);
248 en_r = extent_entry_next(en_r);
251 if (en_l < l_ptrs.end || en_r < r_ptrs.end)
256 lp.crc = bch2_extent_crc_unpack(l.k, NULL);
257 rp.crc = bch2_extent_crc_unpack(r.k, NULL);
259 while (__bkey_ptr_next_decode(l.k, l_ptrs.end, lp, en_l) &&
260 __bkey_ptr_next_decode(r.k, r_ptrs.end, rp, en_r)) {
261 if (lp.ptr.offset + lp.crc.offset + lp.crc.live_size !=
262 rp.ptr.offset + rp.crc.offset ||
263 lp.ptr.dev != rp.ptr.dev ||
264 lp.ptr.gen != rp.ptr.gen ||
265 lp.ptr.unwritten != rp.ptr.unwritten ||
266 lp.has_ec != rp.has_ec)
269 /* Extents may not straddle buckets: */
270 ca = bch_dev_bkey_exists(c, lp.ptr.dev);
271 if (PTR_BUCKET_NR(ca, &lp.ptr) != PTR_BUCKET_NR(ca, &rp.ptr))
274 if (lp.has_ec != rp.has_ec ||
276 (lp.ec.block != rp.ec.block ||
277 lp.ec.redundancy != rp.ec.redundancy ||
278 lp.ec.idx != rp.ec.idx)))
281 if (lp.crc.compression_type != rp.crc.compression_type ||
282 lp.crc.nonce != rp.crc.nonce)
285 if (lp.crc.offset + lp.crc.live_size + rp.crc.live_size <=
286 lp.crc.uncompressed_size) {
287 /* can use left extent's crc entry */
288 } else if (lp.crc.live_size <= rp.crc.offset) {
289 /* can use right extent's crc entry */
291 /* check if checksums can be merged: */
292 if (lp.crc.csum_type != rp.crc.csum_type ||
293 lp.crc.nonce != rp.crc.nonce ||
294 crc_is_compressed(lp.crc) ||
295 !bch2_checksum_mergeable(lp.crc.csum_type))
298 if (lp.crc.offset + lp.crc.live_size != lp.crc.compressed_size ||
302 if (lp.crc.csum_type &&
303 lp.crc.uncompressed_size +
304 rp.crc.uncompressed_size > (c->opts.encoded_extent_max >> 9))
308 en_l = extent_entry_next(en_l);
309 en_r = extent_entry_next(en_r);
314 while (en_l < l_ptrs.end && en_r < r_ptrs.end) {
315 if (extent_entry_is_crc(en_l)) {
316 struct bch_extent_crc_unpacked crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
317 struct bch_extent_crc_unpacked crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
319 if (crc_l.uncompressed_size + crc_r.uncompressed_size >
320 bch2_crc_field_size_max[extent_entry_type(en_l)])
324 en_l = extent_entry_next(en_l);
325 en_r = extent_entry_next(en_r);
328 use_right_ptr = false;
331 while (en_l < l_ptrs.end) {
332 if (extent_entry_type(en_l) == BCH_EXTENT_ENTRY_ptr &&
334 en_l->ptr = en_r->ptr;
336 if (extent_entry_is_crc(en_l)) {
337 struct bch_extent_crc_unpacked crc_l =
338 bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
339 struct bch_extent_crc_unpacked crc_r =
340 bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
342 use_right_ptr = false;
344 if (crc_l.offset + crc_l.live_size + crc_r.live_size <=
345 crc_l.uncompressed_size) {
346 /* can use left extent's crc entry */
347 } else if (crc_l.live_size <= crc_r.offset) {
348 /* can use right extent's crc entry */
349 crc_r.offset -= crc_l.live_size;
350 bch2_extent_crc_pack(entry_to_crc(en_l), crc_r,
351 extent_entry_type(en_l));
352 use_right_ptr = true;
354 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
357 crc_r.uncompressed_size << 9);
359 crc_l.uncompressed_size += crc_r.uncompressed_size;
360 crc_l.compressed_size += crc_r.compressed_size;
361 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l,
362 extent_entry_type(en_l));
366 en_l = extent_entry_next(en_l);
367 en_r = extent_entry_next(en_r);
370 bch2_key_resize(l.k, l.k->size + r.k->size);
374 /* KEY_TYPE_reservation: */
376 int bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k,
377 enum bkey_invalid_flags flags,
378 struct printbuf *err)
380 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
382 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX) {
383 prt_printf(err, "invalid nr_replicas (%u)",
385 return -BCH_ERR_invalid_bkey;
391 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
394 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
396 prt_printf(out, "generation %u replicas %u",
397 le32_to_cpu(r.v->generation),
401 bool bch2_reservation_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r)
403 struct bkey_s_reservation l = bkey_s_to_reservation(_l);
404 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(_r);
406 if (l.v->generation != r.v->generation ||
407 l.v->nr_replicas != r.v->nr_replicas)
410 bch2_key_resize(l.k, l.k->size + r.k->size);
414 /* Extent checksum entries: */
416 /* returns true if not equal */
417 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
418 struct bch_extent_crc_unpacked r)
420 return (l.csum_type != r.csum_type ||
421 l.compression_type != r.compression_type ||
422 l.compressed_size != r.compressed_size ||
423 l.uncompressed_size != r.uncompressed_size ||
424 l.offset != r.offset ||
425 l.live_size != r.live_size ||
426 l.nonce != r.nonce ||
427 bch2_crc_cmp(l.csum, r.csum));
430 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
431 struct bch_extent_crc_unpacked n)
433 return !crc_is_compressed(u) &&
435 u.uncompressed_size > u.live_size &&
436 bch2_csum_type_is_encryption(u.csum_type) ==
437 bch2_csum_type_is_encryption(n.csum_type);
440 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
441 struct bch_extent_crc_unpacked n)
443 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
444 struct bch_extent_crc_unpacked crc;
445 const union bch_extent_entry *i;
450 bkey_for_each_crc(k.k, ptrs, crc, i)
451 if (can_narrow_crc(crc, n))
458 * We're writing another replica for this extent, so while we've got the data in
459 * memory we'll be computing a new checksum for the currently live data.
461 * If there are other replicas we aren't moving, and they are checksummed but
462 * not compressed, we can modify them to point to only the data that is
463 * currently live (so that readers won't have to bounce) while we've got the
466 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
468 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
469 struct bch_extent_crc_unpacked u;
470 struct extent_ptr_decoded p;
471 union bch_extent_entry *i;
474 /* Find a checksum entry that covers only live data: */
476 bkey_for_each_crc(&k->k, ptrs, u, i)
477 if (!crc_is_compressed(u) &&
479 u.live_size == u.uncompressed_size) {
486 BUG_ON(crc_is_compressed(n));
488 BUG_ON(n.live_size != k->k.size);
490 restart_narrow_pointers:
491 ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
493 bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
494 if (can_narrow_crc(p.crc, n)) {
495 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(k), &i->ptr);
496 p.ptr.offset += p.crc.offset;
498 bch2_extent_ptr_decoded_append(k, &p);
500 goto restart_narrow_pointers;
506 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
507 struct bch_extent_crc_unpacked src,
508 enum bch_extent_entry_type type)
510 #define set_common_fields(_dst, _src) \
511 _dst.type = 1 << type; \
512 _dst.csum_type = _src.csum_type, \
513 _dst.compression_type = _src.compression_type, \
514 _dst._compressed_size = _src.compressed_size - 1, \
515 _dst._uncompressed_size = _src.uncompressed_size - 1, \
516 _dst.offset = _src.offset
519 case BCH_EXTENT_ENTRY_crc32:
520 set_common_fields(dst->crc32, src);
521 dst->crc32.csum = (u32 __force) *((__le32 *) &src.csum.lo);
523 case BCH_EXTENT_ENTRY_crc64:
524 set_common_fields(dst->crc64, src);
525 dst->crc64.nonce = src.nonce;
526 dst->crc64.csum_lo = (u64 __force) src.csum.lo;
527 dst->crc64.csum_hi = (u64 __force) *((__le16 *) &src.csum.hi);
529 case BCH_EXTENT_ENTRY_crc128:
530 set_common_fields(dst->crc128, src);
531 dst->crc128.nonce = src.nonce;
532 dst->crc128.csum = src.csum;
537 #undef set_common_fields
540 void bch2_extent_crc_append(struct bkey_i *k,
541 struct bch_extent_crc_unpacked new)
543 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
544 union bch_extent_crc *crc = (void *) ptrs.end;
545 enum bch_extent_entry_type type;
547 if (bch_crc_bytes[new.csum_type] <= 4 &&
548 new.uncompressed_size <= CRC32_SIZE_MAX &&
549 new.nonce <= CRC32_NONCE_MAX)
550 type = BCH_EXTENT_ENTRY_crc32;
551 else if (bch_crc_bytes[new.csum_type] <= 10 &&
552 new.uncompressed_size <= CRC64_SIZE_MAX &&
553 new.nonce <= CRC64_NONCE_MAX)
554 type = BCH_EXTENT_ENTRY_crc64;
555 else if (bch_crc_bytes[new.csum_type] <= 16 &&
556 new.uncompressed_size <= CRC128_SIZE_MAX &&
557 new.nonce <= CRC128_NONCE_MAX)
558 type = BCH_EXTENT_ENTRY_crc128;
562 bch2_extent_crc_pack(crc, new, type);
564 k->k.u64s += extent_entry_u64s(ptrs.end);
566 EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
569 /* Generic code for keys with pointers: */
571 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
573 return bch2_bkey_devs(k).nr;
576 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
578 return k.k->type == KEY_TYPE_reservation
579 ? bkey_s_c_to_reservation(k).v->nr_replicas
580 : bch2_bkey_dirty_devs(k).nr;
583 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
587 if (k.k->type == KEY_TYPE_reservation) {
588 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
590 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
591 const union bch_extent_entry *entry;
592 struct extent_ptr_decoded p;
594 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
595 ret += !p.ptr.cached && !crc_is_compressed(p.crc);
601 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
603 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
604 const union bch_extent_entry *entry;
605 struct extent_ptr_decoded p;
608 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
609 if (!p.ptr.cached && crc_is_compressed(p.crc))
610 ret += p.crc.compressed_size;
615 bool bch2_bkey_is_incompressible(struct bkey_s_c k)
617 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
618 const union bch_extent_entry *entry;
619 struct bch_extent_crc_unpacked crc;
621 bkey_for_each_crc(k.k, ptrs, crc, entry)
622 if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
627 unsigned bch2_bkey_replicas(struct bch_fs *c, 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 extent_ptr_decoded p = { 0 };
632 unsigned replicas = 0;
634 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
639 replicas += p.ec.redundancy;
648 unsigned bch2_extent_ptr_desired_durability(struct bch_fs *c, struct extent_ptr_decoded *p)
655 ca = bch_dev_bkey_exists(c, p->ptr.dev);
657 return ca->mi.durability +
663 unsigned bch2_extent_ptr_durability(struct bch_fs *c, struct extent_ptr_decoded *p)
670 ca = bch_dev_bkey_exists(c, p->ptr.dev);
672 if (ca->mi.state == BCH_MEMBER_STATE_failed)
675 return ca->mi.durability +
681 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
683 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
684 const union bch_extent_entry *entry;
685 struct extent_ptr_decoded p;
686 unsigned durability = 0;
688 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
689 durability += bch2_extent_ptr_durability(c, &p);
694 static unsigned bch2_bkey_durability_safe(struct bch_fs *c, struct bkey_s_c k)
696 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
697 const union bch_extent_entry *entry;
698 struct extent_ptr_decoded p;
699 unsigned durability = 0;
701 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
702 if (p.ptr.dev < c->sb.nr_devices && c->devs[p.ptr.dev])
703 durability += bch2_extent_ptr_durability(c, &p);
708 void bch2_bkey_extent_entry_drop(struct bkey_i *k, union bch_extent_entry *entry)
710 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
711 union bch_extent_entry *next = extent_entry_next(entry);
713 memmove_u64s(entry, next, (u64 *) end - (u64 *) next);
714 k->k.u64s -= extent_entry_u64s(entry);
717 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
718 struct extent_ptr_decoded *p)
720 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
721 struct bch_extent_crc_unpacked crc =
722 bch2_extent_crc_unpack(&k->k, NULL);
723 union bch_extent_entry *pos;
725 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
730 bkey_for_each_crc(&k->k, ptrs, crc, pos)
731 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
732 pos = extent_entry_next(pos);
736 bch2_extent_crc_append(k, p->crc);
737 pos = bkey_val_end(bkey_i_to_s(k));
739 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
740 __extent_entry_insert(k, pos, to_entry(&p->ptr));
743 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
744 __extent_entry_insert(k, pos, to_entry(&p->ec));
748 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
749 union bch_extent_entry *entry)
751 union bch_extent_entry *i = ptrs.start;
756 while (extent_entry_next(i) != entry)
757 i = extent_entry_next(i);
762 * Returns pointer to the next entry after the one being dropped:
764 union bch_extent_entry *bch2_bkey_drop_ptr_noerror(struct bkey_s k,
765 struct bch_extent_ptr *ptr)
767 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
768 union bch_extent_entry *entry = to_entry(ptr), *next;
769 union bch_extent_entry *ret = entry;
770 bool drop_crc = true;
772 EBUG_ON(ptr < &ptrs.start->ptr ||
773 ptr >= &ptrs.end->ptr);
774 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
776 for (next = extent_entry_next(entry);
778 next = extent_entry_next(next)) {
779 if (extent_entry_is_crc(next)) {
781 } else if (extent_entry_is_ptr(next)) {
787 extent_entry_drop(k, entry);
789 while ((entry = extent_entry_prev(ptrs, entry))) {
790 if (extent_entry_is_ptr(entry))
793 if ((extent_entry_is_crc(entry) && drop_crc) ||
794 extent_entry_is_stripe_ptr(entry)) {
795 ret = (void *) ret - extent_entry_bytes(entry);
796 extent_entry_drop(k, entry);
803 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
804 struct bch_extent_ptr *ptr)
806 bool have_dirty = bch2_bkey_dirty_devs(k.s_c).nr;
807 union bch_extent_entry *ret =
808 bch2_bkey_drop_ptr_noerror(k, ptr);
811 * If we deleted all the dirty pointers and there's still cached
812 * pointers, we could set the cached pointers to dirty if they're not
813 * stale - but to do that correctly we'd need to grab an open_bucket
814 * reference so that we don't race with bucket reuse:
817 !bch2_bkey_dirty_devs(k.s_c).nr) {
818 k.k->type = KEY_TYPE_error;
819 set_bkey_val_u64s(k.k, 0);
821 } else if (!bch2_bkey_nr_ptrs(k.s_c)) {
822 k.k->type = KEY_TYPE_deleted;
823 set_bkey_val_u64s(k.k, 0);
830 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
832 struct bch_extent_ptr *ptr;
834 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
837 void bch2_bkey_drop_device_noerror(struct bkey_s k, unsigned dev)
839 struct bch_extent_ptr *ptr = bch2_bkey_has_device(k, dev);
842 bch2_bkey_drop_ptr_noerror(k, ptr);
845 const struct bch_extent_ptr *bch2_bkey_has_device_c(struct bkey_s_c k, unsigned dev)
847 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
848 const struct bch_extent_ptr *ptr;
850 bkey_for_each_ptr(ptrs, ptr)
857 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
859 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
860 const struct bch_extent_ptr *ptr;
862 bkey_for_each_ptr(ptrs, ptr)
863 if (bch2_dev_in_target(c, ptr->dev, target) &&
865 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
871 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
872 struct bch_extent_ptr m, u64 offset)
874 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
875 const union bch_extent_entry *entry;
876 struct extent_ptr_decoded p;
878 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
879 if (p.ptr.dev == m.dev &&
880 p.ptr.gen == m.gen &&
881 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
882 (s64) m.offset - offset)
889 * Returns true if two extents refer to the same data:
891 bool bch2_extents_match(struct bkey_s_c k1, struct bkey_s_c k2)
893 if (k1.k->type != k2.k->type)
896 if (bkey_extent_is_direct_data(k1.k)) {
897 struct bkey_ptrs_c ptrs1 = bch2_bkey_ptrs_c(k1);
898 struct bkey_ptrs_c ptrs2 = bch2_bkey_ptrs_c(k2);
899 const union bch_extent_entry *entry1, *entry2;
900 struct extent_ptr_decoded p1, p2;
902 if (bkey_extent_is_unwritten(k1) != bkey_extent_is_unwritten(k2))
905 bkey_for_each_ptr_decode(k1.k, ptrs1, p1, entry1)
906 bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2)
907 if (p1.ptr.dev == p2.ptr.dev &&
908 p1.ptr.gen == p2.ptr.gen &&
909 (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) ==
910 (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k))
915 /* KEY_TYPE_deleted, etc. */
920 struct bch_extent_ptr *
921 bch2_extent_has_ptr(struct bkey_s_c k1, struct extent_ptr_decoded p1, struct bkey_s k2)
923 struct bkey_ptrs ptrs2 = bch2_bkey_ptrs(k2);
924 union bch_extent_entry *entry2;
925 struct extent_ptr_decoded p2;
927 bkey_for_each_ptr_decode(k2.k, ptrs2, p2, entry2)
928 if (p1.ptr.dev == p2.ptr.dev &&
929 p1.ptr.gen == p2.ptr.gen &&
930 (s64) p1.ptr.offset + p1.crc.offset - bkey_start_offset(k1.k) ==
931 (s64) p2.ptr.offset + p2.crc.offset - bkey_start_offset(k2.k))
937 void bch2_extent_ptr_set_cached(struct bkey_s k, struct bch_extent_ptr *ptr)
939 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
940 union bch_extent_entry *entry;
941 union bch_extent_entry *ec = NULL;
943 bkey_extent_entry_for_each(ptrs, entry) {
944 if (&entry->ptr == ptr) {
947 extent_entry_drop(k, ec);
951 if (extent_entry_is_stripe_ptr(entry))
953 else if (extent_entry_is_ptr(entry))
961 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
963 * Returns true if @k should be dropped entirely
965 * For existing keys, only called when btree nodes are being rewritten, not when
966 * they're merely being compacted/resorted in memory.
968 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
970 struct bch_extent_ptr *ptr;
972 bch2_bkey_drop_ptrs(k, ptr,
974 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
976 return bkey_deleted(k.k);
979 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
982 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
983 const union bch_extent_entry *entry;
987 prt_printf(out, "durability: %u ", bch2_bkey_durability_safe(c, k));
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 const struct bch_extent_ptr *ptr = entry_to_ptr(entry);
996 struct bch_dev *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",
1009 ptr->dev, b, offset, ptr->gen);
1011 prt_str(out, " cached");
1013 prt_str(out, " unwritten");
1014 if (ca && ptr_stale(ca, ptr))
1015 prt_printf(out, " stale");
1019 case BCH_EXTENT_ENTRY_crc32:
1020 case BCH_EXTENT_ENTRY_crc64:
1021 case BCH_EXTENT_ENTRY_crc128: {
1022 struct bch_extent_crc_unpacked crc =
1023 bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1025 prt_printf(out, "crc: c_size %u size %u offset %u nonce %u csum %s compress %s",
1026 crc.compressed_size,
1027 crc.uncompressed_size,
1028 crc.offset, crc.nonce,
1029 bch2_csum_types[crc.csum_type],
1030 bch2_compression_types[crc.compression_type]);
1033 case BCH_EXTENT_ENTRY_stripe_ptr: {
1034 const struct bch_extent_stripe_ptr *ec = &entry->stripe_ptr;
1036 prt_printf(out, "ec: idx %llu block %u",
1037 (u64) ec->idx, ec->block);
1040 case BCH_EXTENT_ENTRY_rebalance: {
1041 const struct bch_extent_rebalance *r = &entry->rebalance;
1043 prt_str(out, "rebalance: target ");
1045 bch2_target_to_text(out, c, r->target);
1047 prt_printf(out, "%u", r->target);
1048 prt_str(out, " compression ");
1049 bch2_compression_opt_to_text(out, r->compression);
1053 prt_printf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
1061 static int extent_ptr_invalid(const struct bch_fs *c,
1063 enum bkey_invalid_flags flags,
1064 const struct bch_extent_ptr *ptr,
1065 unsigned size_ondisk,
1067 struct printbuf *err)
1069 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1070 const struct bch_extent_ptr *ptr2;
1075 if (!bch2_dev_exists2(c, ptr->dev)) {
1077 * If we're in the write path this key might have already been
1078 * overwritten, and we could be seeing a device that doesn't
1079 * exist anymore due to racing with device removal:
1081 if (flags & BKEY_INVALID_WRITE)
1084 prt_printf(err, "pointer to invalid device (%u)", ptr->dev);
1085 return -BCH_ERR_invalid_bkey;
1088 ca = bch_dev_bkey_exists(c, ptr->dev);
1089 bkey_for_each_ptr(ptrs, ptr2)
1090 if (ptr != ptr2 && ptr->dev == ptr2->dev) {
1091 prt_printf(err, "multiple pointers to same device (%u)", ptr->dev);
1092 return -BCH_ERR_invalid_bkey;
1095 bucket = sector_to_bucket_and_offset(ca, ptr->offset, &bucket_offset);
1097 if (bucket >= ca->mi.nbuckets) {
1098 prt_printf(err, "pointer past last bucket (%llu > %llu)",
1099 bucket, ca->mi.nbuckets);
1100 return -BCH_ERR_invalid_bkey;
1103 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket)) {
1104 prt_printf(err, "pointer before first bucket (%llu < %u)",
1105 bucket, ca->mi.first_bucket);
1106 return -BCH_ERR_invalid_bkey;
1109 if (bucket_offset + size_ondisk > ca->mi.bucket_size) {
1110 prt_printf(err, "pointer spans multiple buckets (%u + %u > %u)",
1111 bucket_offset, size_ondisk, ca->mi.bucket_size);
1112 return -BCH_ERR_invalid_bkey;
1118 int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
1119 enum bkey_invalid_flags flags,
1120 struct printbuf *err)
1122 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1123 const union bch_extent_entry *entry;
1124 struct bch_extent_crc_unpacked crc;
1125 unsigned size_ondisk = k.k->size;
1126 unsigned nonce = UINT_MAX;
1127 unsigned nr_ptrs = 0;
1128 bool unwritten = false, have_ec = false, crc_since_last_ptr = false;
1131 if (bkey_is_btree_ptr(k.k))
1132 size_ondisk = btree_sectors(c);
1134 bkey_extent_entry_for_each(ptrs, entry) {
1135 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX) {
1136 prt_printf(err, "invalid extent entry type (got %u, max %u)",
1137 __extent_entry_type(entry), BCH_EXTENT_ENTRY_MAX);
1138 return -BCH_ERR_invalid_bkey;
1141 if (bkey_is_btree_ptr(k.k) &&
1142 !extent_entry_is_ptr(entry)) {
1143 prt_printf(err, "has non ptr field");
1144 return -BCH_ERR_invalid_bkey;
1147 switch (extent_entry_type(entry)) {
1148 case BCH_EXTENT_ENTRY_ptr:
1149 ret = extent_ptr_invalid(c, k, flags, &entry->ptr,
1150 size_ondisk, false, err);
1154 if (nr_ptrs && unwritten != entry->ptr.unwritten) {
1155 prt_printf(err, "extent with unwritten and written ptrs");
1156 return -BCH_ERR_invalid_bkey;
1159 if (k.k->type != KEY_TYPE_extent && entry->ptr.unwritten) {
1160 prt_printf(err, "has unwritten ptrs");
1161 return -BCH_ERR_invalid_bkey;
1164 if (entry->ptr.cached && have_ec) {
1165 prt_printf(err, "cached, erasure coded ptr");
1166 return -BCH_ERR_invalid_bkey;
1169 unwritten = entry->ptr.unwritten;
1171 crc_since_last_ptr = false;
1174 case BCH_EXTENT_ENTRY_crc32:
1175 case BCH_EXTENT_ENTRY_crc64:
1176 case BCH_EXTENT_ENTRY_crc128:
1177 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1179 if (crc.offset + crc.live_size >
1180 crc.uncompressed_size) {
1181 prt_printf(err, "checksum offset + key size > uncompressed size");
1182 return -BCH_ERR_invalid_bkey;
1185 size_ondisk = crc.compressed_size;
1187 if (!bch2_checksum_type_valid(c, crc.csum_type)) {
1188 prt_printf(err, "invalid checksum type");
1189 return -BCH_ERR_invalid_bkey;
1192 if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR) {
1193 prt_printf(err, "invalid compression type");
1194 return -BCH_ERR_invalid_bkey;
1197 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1198 if (nonce == UINT_MAX)
1199 nonce = crc.offset + crc.nonce;
1200 else if (nonce != crc.offset + crc.nonce) {
1201 prt_printf(err, "incorrect nonce");
1202 return -BCH_ERR_invalid_bkey;
1206 if (crc_since_last_ptr) {
1207 prt_printf(err, "redundant crc entry");
1208 return -BCH_ERR_invalid_bkey;
1210 crc_since_last_ptr = true;
1212 if (crc_is_encoded(crc) &&
1213 (crc.uncompressed_size > c->opts.encoded_extent_max >> 9) &&
1214 (flags & (BKEY_INVALID_WRITE|BKEY_INVALID_COMMIT))) {
1215 prt_printf(err, "too large encoded extent");
1216 return -BCH_ERR_invalid_bkey;
1220 case BCH_EXTENT_ENTRY_stripe_ptr:
1222 prt_printf(err, "redundant stripe entry");
1223 return -BCH_ERR_invalid_bkey;
1227 case BCH_EXTENT_ENTRY_rebalance: {
1228 const struct bch_extent_rebalance *r = &entry->rebalance;
1230 if (!bch2_compression_opt_valid(r->compression)) {
1231 struct bch_compression_opt opt = __bch2_compression_decode(r->compression);
1232 prt_printf(err, "invalid compression opt %u:%u",
1233 opt.type, opt.level);
1234 return -BCH_ERR_invalid_bkey;
1242 prt_str(err, "no ptrs");
1243 return -BCH_ERR_invalid_bkey;
1246 if (nr_ptrs >= BCH_BKEY_PTRS_MAX) {
1247 prt_str(err, "too many ptrs");
1248 return -BCH_ERR_invalid_bkey;
1251 if (crc_since_last_ptr) {
1252 prt_printf(err, "redundant crc entry");
1253 return -BCH_ERR_invalid_bkey;
1257 prt_printf(err, "redundant stripe entry");
1258 return -BCH_ERR_invalid_bkey;
1264 void bch2_ptr_swab(struct bkey_s k)
1266 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1267 union bch_extent_entry *entry;
1270 for (d = (u64 *) ptrs.start;
1271 d != (u64 *) ptrs.end;
1275 for (entry = ptrs.start;
1277 entry = extent_entry_next(entry)) {
1278 switch (extent_entry_type(entry)) {
1279 case BCH_EXTENT_ENTRY_ptr:
1281 case BCH_EXTENT_ENTRY_crc32:
1282 entry->crc32.csum = swab32(entry->crc32.csum);
1284 case BCH_EXTENT_ENTRY_crc64:
1285 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1286 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1288 case BCH_EXTENT_ENTRY_crc128:
1289 entry->crc128.csum.hi = (__force __le64)
1290 swab64((__force u64) entry->crc128.csum.hi);
1291 entry->crc128.csum.lo = (__force __le64)
1292 swab64((__force u64) entry->crc128.csum.lo);
1294 case BCH_EXTENT_ENTRY_stripe_ptr:
1296 case BCH_EXTENT_ENTRY_rebalance:
1302 const struct bch_extent_rebalance *bch2_bkey_rebalance_opts(struct bkey_s_c k)
1304 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1305 const union bch_extent_entry *entry;
1307 bkey_extent_entry_for_each(ptrs, entry)
1308 if (__extent_entry_type(entry) == BCH_EXTENT_ENTRY_rebalance)
1309 return &entry->rebalance;
1314 unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
1315 unsigned target, unsigned compression)
1317 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1318 unsigned rewrite_ptrs = 0;
1321 unsigned compression_type = bch2_compression_opt_to_type(compression);
1322 const union bch_extent_entry *entry;
1323 struct extent_ptr_decoded p;
1326 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
1327 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible) {
1329 goto incompressible;
1332 if (!p.ptr.cached && p.crc.compression_type != compression_type)
1333 rewrite_ptrs |= 1U << i;
1338 if (target && bch2_target_accepts_data(c, BCH_DATA_user, target)) {
1339 const struct bch_extent_ptr *ptr;
1342 bkey_for_each_ptr(ptrs, ptr) {
1343 if (!ptr->cached && !bch2_dev_in_target(c, ptr->dev, target))
1344 rewrite_ptrs |= 1U << i;
1349 return rewrite_ptrs;
1352 bool bch2_bkey_needs_rebalance(struct bch_fs *c, struct bkey_s_c k)
1354 const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k);
1357 * If it's an indirect extent, we don't delete the rebalance entry when
1358 * done so that we know what options were applied - check if it still
1362 k.k->type == KEY_TYPE_reflink_v &&
1363 !bch2_bkey_ptrs_need_rebalance(c, k, r->target, r->compression))
1369 int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bkey_i *_k,
1370 unsigned target, unsigned compression)
1372 struct bkey_s k = bkey_i_to_s(_k);
1373 struct bch_extent_rebalance *r;
1374 bool needs_rebalance;
1376 if (!bkey_extent_is_direct_data(k.k))
1379 /* get existing rebalance entry: */
1380 r = (struct bch_extent_rebalance *) bch2_bkey_rebalance_opts(k.s_c);
1382 if (k.k->type == KEY_TYPE_reflink_v) {
1384 * indirect extents: existing options take precedence,
1385 * so that we don't move extents back and forth if
1386 * they're referenced by different inodes with different
1392 compression = r->compression;
1396 r->compression = compression;
1399 needs_rebalance = bch2_bkey_ptrs_need_rebalance(c, k.s_c, target, compression);
1401 if (needs_rebalance && !r) {
1402 union bch_extent_entry *new = bkey_val_end(k);
1404 new->rebalance.type = 1U << BCH_EXTENT_ENTRY_rebalance;
1405 new->rebalance.compression = compression;
1406 new->rebalance.target = target;
1407 new->rebalance.unused = 0;
1408 k.k->u64s += extent_entry_u64s(new);
1409 } else if (!needs_rebalance && r && k.k->type != KEY_TYPE_reflink_v) {
1411 * For indirect extents, don't delete the rebalance entry when
1412 * we're finished so that we know we specifically moved it or
1413 * compressed it to its current location/compression type
1415 extent_entry_drop(k, (union bch_extent_entry *) r);
1421 /* Generic extent code: */
1423 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1425 unsigned new_val_u64s = bkey_val_u64s(k.k);
1429 if (bkey_le(where, bkey_start_pos(k.k)))
1432 EBUG_ON(bkey_gt(where, k.k->p));
1434 sub = where.offset - bkey_start_offset(k.k);
1439 k.k->type = KEY_TYPE_deleted;
1443 switch (k.k->type) {
1444 case KEY_TYPE_extent:
1445 case KEY_TYPE_reflink_v: {
1446 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1447 union bch_extent_entry *entry;
1448 bool seen_crc = false;
1450 bkey_extent_entry_for_each(ptrs, entry) {
1451 switch (extent_entry_type(entry)) {
1452 case BCH_EXTENT_ENTRY_ptr:
1454 entry->ptr.offset += sub;
1456 case BCH_EXTENT_ENTRY_crc32:
1457 entry->crc32.offset += sub;
1459 case BCH_EXTENT_ENTRY_crc64:
1460 entry->crc64.offset += sub;
1462 case BCH_EXTENT_ENTRY_crc128:
1463 entry->crc128.offset += sub;
1465 case BCH_EXTENT_ENTRY_stripe_ptr:
1467 case BCH_EXTENT_ENTRY_rebalance:
1471 if (extent_entry_is_crc(entry))
1477 case KEY_TYPE_reflink_p: {
1478 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1480 le64_add_cpu(&p.v->idx, sub);
1483 case KEY_TYPE_inline_data:
1484 case KEY_TYPE_indirect_inline_data: {
1485 void *p = bkey_inline_data_p(k);
1486 unsigned bytes = bkey_inline_data_bytes(k.k);
1488 sub = min_t(u64, sub << 9, bytes);
1490 memmove(p, p + sub, bytes - sub);
1492 new_val_u64s -= sub >> 3;
1497 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1498 BUG_ON(val_u64s_delta < 0);
1500 set_bkey_val_u64s(k.k, new_val_u64s);
1501 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1502 return -val_u64s_delta;
1505 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1507 unsigned new_val_u64s = bkey_val_u64s(k.k);
1511 if (bkey_ge(where, k.k->p))
1514 EBUG_ON(bkey_lt(where, bkey_start_pos(k.k)));
1516 len = where.offset - bkey_start_offset(k.k);
1518 k.k->p.offset = where.offset;
1522 k.k->type = KEY_TYPE_deleted;
1526 switch (k.k->type) {
1527 case KEY_TYPE_inline_data:
1528 case KEY_TYPE_indirect_inline_data:
1529 new_val_u64s = (bkey_inline_data_offset(k.k) +
1530 min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
1534 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1535 BUG_ON(val_u64s_delta < 0);
1537 set_bkey_val_u64s(k.k, new_val_u64s);
1538 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1539 return -val_u64s_delta;