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 (force_reconstruct_read(c))
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) {
117 ca = bch_dev_bkey_exists(c, p.ptr.dev);
120 * If there are any dirty pointers it's an error if we can't
123 if (!ret && !p.ptr.cached)
126 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
129 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
131 p.idx = f->nr_failed < f->nr_retries
136 !bch2_dev_is_readable(ca))
139 if (force_reconstruct_read(c) &&
143 if (p.idx >= (unsigned) p.has_ec + 1)
146 if (ret > 0 && !ptr_better(c, p, *pick))
156 /* KEY_TYPE_btree_ptr: */
158 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
160 if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
161 return "value too big";
163 return bch2_bkey_ptrs_invalid(c, k);
166 void bch2_btree_ptr_debugcheck(struct bch_fs *c, struct bkey_s_c k)
168 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
169 const struct bch_extent_ptr *ptr;
172 struct bucket_mark mark;
175 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
178 if (!percpu_down_read_trylock(&c->mark_lock))
181 bch2_fs_inconsistent_on(!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
182 !bch2_bkey_replicas_marked(c, k, false), c,
183 "btree key bad (replicas not marked in superblock):\n%s",
184 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf));
186 bkey_for_each_ptr(ptrs, ptr) {
187 ca = bch_dev_bkey_exists(c, ptr->dev);
189 mark = ptr_bucket_mark(ca, ptr);
192 if (gen_after(mark.gen, ptr->gen))
195 err = "inconsistent";
196 if (mark.data_type != BCH_DATA_BTREE ||
197 mark.dirty_sectors < c->opts.btree_node_size)
201 percpu_up_read(&c->mark_lock);
204 bch2_fs_inconsistent(c, "%s btree pointer %s: bucket %zi gen %i mark %08x",
205 err, (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf),
206 PTR_BUCKET_NR(ca, ptr),
207 mark.gen, (unsigned) mark.v.counter);
211 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
214 bch2_bkey_ptrs_to_text(out, c, k);
217 void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version,
218 unsigned big_endian, int write,
221 struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k);
223 compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key);
225 if (version < bcachefs_metadata_version_inode_btree_change &&
226 btree_node_type_is_extents(btree_id) &&
227 bkey_cmp(bp.v->min_key, POS_MIN))
228 bp.v->min_key = write
229 ? bkey_predecessor(bp.v->min_key)
230 : bkey_successor(bp.v->min_key);
233 /* KEY_TYPE_extent: */
235 const char *bch2_extent_invalid(const struct bch_fs *c, struct bkey_s_c k)
237 return bch2_bkey_ptrs_invalid(c, k);
240 void bch2_extent_debugcheck(struct bch_fs *c, struct bkey_s_c k)
242 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
243 const union bch_extent_entry *entry;
244 struct extent_ptr_decoded p;
247 if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) ||
248 !test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
251 if (!percpu_down_read_trylock(&c->mark_lock))
254 bch2_fs_inconsistent_on(!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
255 !bch2_bkey_replicas_marked_locked(c, e.s_c, false), c,
256 "extent key bad (replicas not marked in superblock):\n%s",
257 (bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c), buf));
259 extent_for_each_ptr_decode(e, p, entry) {
260 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
261 struct bucket_mark mark = ptr_bucket_mark(ca, &p.ptr);
262 unsigned stale = gen_after(mark.gen, p.ptr.gen);
263 unsigned disk_sectors = ptr_disk_sectors(p);
264 unsigned mark_sectors = p.ptr.cached
265 ? mark.cached_sectors
266 : mark.dirty_sectors;
268 bch2_fs_inconsistent_on(stale && !p.ptr.cached, c,
269 "stale dirty pointer (ptr gen %u bucket %u",
270 p.ptr.gen, mark.gen);
272 bch2_fs_inconsistent_on(stale > 96, c,
273 "key too stale: %i", stale);
275 bch2_fs_inconsistent_on(!stale &&
276 (mark.data_type != BCH_DATA_USER ||
277 mark_sectors < disk_sectors), c,
278 "extent pointer not marked: %s:\n"
279 "type %u sectors %u < %u",
280 (bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c), buf),
282 mark_sectors, disk_sectors);
285 percpu_up_read(&c->mark_lock);
288 void bch2_extent_to_text(struct printbuf *out, struct bch_fs *c,
291 bch2_bkey_ptrs_to_text(out, c, k);
294 enum merge_result bch2_extent_merge(struct bch_fs *c,
295 struct bkey_s _l, struct bkey_s _r)
297 struct bkey_s_extent l = bkey_s_to_extent(_l);
298 struct bkey_s_extent r = bkey_s_to_extent(_r);
299 union bch_extent_entry *en_l = l.v->start;
300 union bch_extent_entry *en_r = r.v->start;
301 struct bch_extent_crc_unpacked crc_l, crc_r;
303 if (bkey_val_u64s(l.k) != bkey_val_u64s(r.k))
304 return BCH_MERGE_NOMERGE;
306 crc_l = bch2_extent_crc_unpack(l.k, NULL);
308 extent_for_each_entry(l, en_l) {
309 en_r = vstruct_idx(r.v, (u64 *) en_l - l.v->_data);
311 if (extent_entry_type(en_l) != extent_entry_type(en_r))
312 return BCH_MERGE_NOMERGE;
314 switch (extent_entry_type(en_l)) {
315 case BCH_EXTENT_ENTRY_ptr: {
316 const struct bch_extent_ptr *lp = &en_l->ptr;
317 const struct bch_extent_ptr *rp = &en_r->ptr;
320 if (lp->offset + crc_l.compressed_size != rp->offset ||
321 lp->dev != rp->dev ||
323 return BCH_MERGE_NOMERGE;
325 /* We don't allow extents to straddle buckets: */
326 ca = bch_dev_bkey_exists(c, lp->dev);
328 if (PTR_BUCKET_NR(ca, lp) != PTR_BUCKET_NR(ca, rp))
329 return BCH_MERGE_NOMERGE;
333 case BCH_EXTENT_ENTRY_stripe_ptr:
334 if (en_l->stripe_ptr.block != en_r->stripe_ptr.block ||
335 en_l->stripe_ptr.idx != en_r->stripe_ptr.idx)
336 return BCH_MERGE_NOMERGE;
338 case BCH_EXTENT_ENTRY_crc32:
339 case BCH_EXTENT_ENTRY_crc64:
340 case BCH_EXTENT_ENTRY_crc128:
341 crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
342 crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
344 if (crc_l.csum_type != crc_r.csum_type ||
345 crc_l.compression_type != crc_r.compression_type ||
346 crc_l.nonce != crc_r.nonce)
347 return BCH_MERGE_NOMERGE;
349 if (crc_l.offset + crc_l.live_size != crc_l.compressed_size ||
351 return BCH_MERGE_NOMERGE;
353 if (!bch2_checksum_mergeable(crc_l.csum_type))
354 return BCH_MERGE_NOMERGE;
356 if (crc_is_compressed(crc_l))
357 return BCH_MERGE_NOMERGE;
359 if (crc_l.csum_type &&
360 crc_l.uncompressed_size +
361 crc_r.uncompressed_size > c->sb.encoded_extent_max)
362 return BCH_MERGE_NOMERGE;
364 if (crc_l.uncompressed_size + crc_r.uncompressed_size >
365 bch2_crc_field_size_max[extent_entry_type(en_l)])
366 return BCH_MERGE_NOMERGE;
370 return BCH_MERGE_NOMERGE;
374 extent_for_each_entry(l, en_l) {
375 struct bch_extent_crc_unpacked crc_l, crc_r;
377 en_r = vstruct_idx(r.v, (u64 *) en_l - l.v->_data);
379 if (!extent_entry_is_crc(en_l))
382 crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
383 crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
385 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
388 crc_r.uncompressed_size << 9);
390 crc_l.uncompressed_size += crc_r.uncompressed_size;
391 crc_l.compressed_size += crc_r.compressed_size;
393 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l,
394 extent_entry_type(en_l));
397 bch2_key_resize(l.k, l.k->size + r.k->size);
399 return BCH_MERGE_MERGE;
402 /* KEY_TYPE_reservation: */
404 const char *bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k)
406 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
408 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation))
409 return "incorrect value size";
411 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX)
412 return "invalid nr_replicas";
417 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
420 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
422 pr_buf(out, "generation %u replicas %u",
423 le32_to_cpu(r.v->generation),
427 enum merge_result bch2_reservation_merge(struct bch_fs *c,
428 struct bkey_s _l, struct bkey_s _r)
430 struct bkey_s_reservation l = bkey_s_to_reservation(_l);
431 struct bkey_s_reservation r = bkey_s_to_reservation(_r);
433 if (l.v->generation != r.v->generation ||
434 l.v->nr_replicas != r.v->nr_replicas)
435 return BCH_MERGE_NOMERGE;
437 if ((u64) l.k->size + r.k->size > KEY_SIZE_MAX) {
438 bch2_key_resize(l.k, KEY_SIZE_MAX);
439 bch2_cut_front_s(l.k->p, r.s);
440 return BCH_MERGE_PARTIAL;
443 bch2_key_resize(l.k, l.k->size + r.k->size);
445 return BCH_MERGE_MERGE;
448 /* Extent checksum entries: */
450 /* returns true if not equal */
451 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
452 struct bch_extent_crc_unpacked r)
454 return (l.csum_type != r.csum_type ||
455 l.compression_type != r.compression_type ||
456 l.compressed_size != r.compressed_size ||
457 l.uncompressed_size != r.uncompressed_size ||
458 l.offset != r.offset ||
459 l.live_size != r.live_size ||
460 l.nonce != r.nonce ||
461 bch2_crc_cmp(l.csum, r.csum));
464 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
465 struct bch_extent_crc_unpacked n)
467 return !crc_is_compressed(u) &&
469 u.uncompressed_size > u.live_size &&
470 bch2_csum_type_is_encryption(u.csum_type) ==
471 bch2_csum_type_is_encryption(n.csum_type);
474 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
475 struct bch_extent_crc_unpacked n)
477 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
478 struct bch_extent_crc_unpacked crc;
479 const union bch_extent_entry *i;
484 bkey_for_each_crc(k.k, ptrs, crc, i)
485 if (can_narrow_crc(crc, n))
492 * We're writing another replica for this extent, so while we've got the data in
493 * memory we'll be computing a new checksum for the currently live data.
495 * If there are other replicas we aren't moving, and they are checksummed but
496 * not compressed, we can modify them to point to only the data that is
497 * currently live (so that readers won't have to bounce) while we've got the
500 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
502 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
503 struct bch_extent_crc_unpacked u;
504 struct extent_ptr_decoded p;
505 union bch_extent_entry *i;
508 /* Find a checksum entry that covers only live data: */
510 bkey_for_each_crc(&k->k, ptrs, u, i)
511 if (!crc_is_compressed(u) &&
513 u.live_size == u.uncompressed_size) {
520 BUG_ON(crc_is_compressed(n));
522 BUG_ON(n.live_size != k->k.size);
524 restart_narrow_pointers:
525 ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
527 bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
528 if (can_narrow_crc(p.crc, n)) {
529 bch2_bkey_drop_ptr(bkey_i_to_s(k), &i->ptr);
530 p.ptr.offset += p.crc.offset;
532 bch2_extent_ptr_decoded_append(k, &p);
534 goto restart_narrow_pointers;
540 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
541 struct bch_extent_crc_unpacked src,
542 enum bch_extent_entry_type type)
544 #define set_common_fields(_dst, _src) \
545 _dst.type = 1 << type; \
546 _dst.csum_type = _src.csum_type, \
547 _dst.compression_type = _src.compression_type, \
548 _dst._compressed_size = _src.compressed_size - 1, \
549 _dst._uncompressed_size = _src.uncompressed_size - 1, \
550 _dst.offset = _src.offset
553 case BCH_EXTENT_ENTRY_crc32:
554 set_common_fields(dst->crc32, src);
555 dst->crc32.csum = *((__le32 *) &src.csum.lo);
557 case BCH_EXTENT_ENTRY_crc64:
558 set_common_fields(dst->crc64, src);
559 dst->crc64.nonce = src.nonce;
560 dst->crc64.csum_lo = src.csum.lo;
561 dst->crc64.csum_hi = *((__le16 *) &src.csum.hi);
563 case BCH_EXTENT_ENTRY_crc128:
564 set_common_fields(dst->crc128, src);
565 dst->crc128.nonce = src.nonce;
566 dst->crc128.csum = src.csum;
571 #undef set_common_fields
574 void bch2_extent_crc_append(struct bkey_i *k,
575 struct bch_extent_crc_unpacked new)
577 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
578 union bch_extent_crc *crc = (void *) ptrs.end;
579 enum bch_extent_entry_type type;
581 if (bch_crc_bytes[new.csum_type] <= 4 &&
582 new.uncompressed_size <= CRC32_SIZE_MAX &&
583 new.nonce <= CRC32_NONCE_MAX)
584 type = BCH_EXTENT_ENTRY_crc32;
585 else if (bch_crc_bytes[new.csum_type] <= 10 &&
586 new.uncompressed_size <= CRC64_SIZE_MAX &&
587 new.nonce <= CRC64_NONCE_MAX)
588 type = BCH_EXTENT_ENTRY_crc64;
589 else if (bch_crc_bytes[new.csum_type] <= 16 &&
590 new.uncompressed_size <= CRC128_SIZE_MAX &&
591 new.nonce <= CRC128_NONCE_MAX)
592 type = BCH_EXTENT_ENTRY_crc128;
596 bch2_extent_crc_pack(crc, new, type);
598 k->k.u64s += extent_entry_u64s(ptrs.end);
600 EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
603 /* Generic code for keys with pointers: */
605 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
607 return bch2_bkey_devs(k).nr;
610 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
612 return k.k->type == KEY_TYPE_reservation
613 ? bkey_s_c_to_reservation(k).v->nr_replicas
614 : bch2_bkey_dirty_devs(k).nr;
617 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
621 if (k.k->type == KEY_TYPE_reservation) {
622 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
624 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
625 const union bch_extent_entry *entry;
626 struct extent_ptr_decoded p;
628 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
629 ret += !p.ptr.cached && !crc_is_compressed(p.crc);
635 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
637 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
638 const union bch_extent_entry *entry;
639 struct extent_ptr_decoded p;
642 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
643 if (!p.ptr.cached && crc_is_compressed(p.crc))
644 ret += p.crc.compressed_size;
649 bool bch2_bkey_is_incompressible(struct bkey_s_c k)
651 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
652 const union bch_extent_entry *entry;
653 struct bch_extent_crc_unpacked crc;
655 bkey_for_each_crc(k.k, ptrs, crc, entry)
656 if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
661 bool bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size,
662 unsigned nr_replicas)
664 struct btree_trans trans;
665 struct btree_iter *iter;
666 struct bpos end = pos;
673 bch2_trans_init(&trans, c, 0, 0);
675 for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, pos,
676 BTREE_ITER_SLOTS, k, err) {
677 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
680 if (nr_replicas > bch2_bkey_nr_ptrs_fully_allocated(k)) {
685 bch2_trans_exit(&trans);
690 static unsigned bch2_extent_ptr_durability(struct bch_fs *c,
691 struct extent_ptr_decoded p)
693 unsigned durability = 0;
699 ca = bch_dev_bkey_exists(c, p.ptr.dev);
701 if (ca->mi.state != BCH_MEMBER_STATE_FAILED)
702 durability = max_t(unsigned, durability, ca->mi.durability);
706 genradix_ptr(&c->stripes[0], p.ec.idx);
711 durability = max_t(unsigned, durability, s->nr_redundant);
717 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
719 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
720 const union bch_extent_entry *entry;
721 struct extent_ptr_decoded p;
722 unsigned durability = 0;
724 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
725 durability += bch2_extent_ptr_durability(c, p);
730 void bch2_bkey_mark_replicas_cached(struct bch_fs *c, struct bkey_s k,
732 unsigned nr_desired_replicas)
734 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
735 union bch_extent_entry *entry;
736 struct extent_ptr_decoded p;
737 int extra = bch2_bkey_durability(c, k.s_c) - nr_desired_replicas;
739 if (target && extra > 0)
740 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
741 int n = bch2_extent_ptr_durability(c, p);
743 if (n && n <= extra &&
744 !bch2_dev_in_target(c, p.ptr.dev, target)) {
745 entry->ptr.cached = true;
751 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
752 int n = bch2_extent_ptr_durability(c, p);
754 if (n && n <= extra) {
755 entry->ptr.cached = true;
761 void bch2_bkey_append_ptr(struct bkey_i *k,
762 struct bch_extent_ptr ptr)
764 EBUG_ON(bch2_bkey_has_device(bkey_i_to_s_c(k), ptr.dev));
767 case KEY_TYPE_btree_ptr:
768 case KEY_TYPE_btree_ptr_v2:
769 case KEY_TYPE_extent:
770 EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
772 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
774 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
784 static inline void __extent_entry_insert(struct bkey_i *k,
785 union bch_extent_entry *dst,
786 union bch_extent_entry *new)
788 union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
790 memmove_u64s_up_small((u64 *) dst + extent_entry_u64s(new),
791 dst, (u64 *) end - (u64 *) dst);
792 k->k.u64s += extent_entry_u64s(new);
793 memcpy_u64s_small(dst, new, extent_entry_u64s(new));
796 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
797 struct extent_ptr_decoded *p)
799 struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
800 struct bch_extent_crc_unpacked crc =
801 bch2_extent_crc_unpack(&k->k, NULL);
802 union bch_extent_entry *pos;
804 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
809 bkey_for_each_crc(&k->k, ptrs, crc, pos)
810 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
811 pos = extent_entry_next(pos);
815 bch2_extent_crc_append(k, p->crc);
816 pos = bkey_val_end(bkey_i_to_s(k));
818 p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
819 __extent_entry_insert(k, pos, to_entry(&p->ptr));
822 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
823 __extent_entry_insert(k, pos, to_entry(&p->ec));
827 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
828 union bch_extent_entry *entry)
830 union bch_extent_entry *i = ptrs.start;
835 while (extent_entry_next(i) != entry)
836 i = extent_entry_next(i);
840 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
841 struct bch_extent_ptr *ptr)
843 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
844 union bch_extent_entry *dst, *src, *prev;
845 bool drop_crc = true;
847 EBUG_ON(ptr < &ptrs.start->ptr ||
848 ptr >= &ptrs.end->ptr);
849 EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
851 src = extent_entry_next(to_entry(ptr));
852 if (src != ptrs.end &&
853 !extent_entry_is_crc(src))
857 while ((prev = extent_entry_prev(ptrs, dst))) {
858 if (extent_entry_is_ptr(prev))
861 if (extent_entry_is_crc(prev)) {
870 memmove_u64s_down(dst, src,
871 (u64 *) ptrs.end - (u64 *) src);
872 k.k->u64s -= (u64 *) src - (u64 *) dst;
877 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
879 struct bch_extent_ptr *ptr;
881 bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
884 const struct bch_extent_ptr *
885 bch2_bkey_has_device(struct bkey_s_c k, unsigned dev)
887 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
888 const struct bch_extent_ptr *ptr;
890 bkey_for_each_ptr(ptrs, ptr)
897 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
899 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
900 const struct bch_extent_ptr *ptr;
902 bkey_for_each_ptr(ptrs, ptr)
903 if (bch2_dev_in_target(c, ptr->dev, target) &&
905 !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
911 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
912 struct bch_extent_ptr m, u64 offset)
914 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
915 const union bch_extent_entry *entry;
916 struct extent_ptr_decoded p;
918 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
919 if (p.ptr.dev == m.dev &&
920 p.ptr.gen == m.gen &&
921 (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
922 (s64) m.offset - offset)
929 * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
931 * Returns true if @k should be dropped entirely
933 * For existing keys, only called when btree nodes are being rewritten, not when
934 * they're merely being compacted/resorted in memory.
936 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
938 struct bch_extent_ptr *ptr;
940 bch2_bkey_drop_ptrs(k, ptr,
942 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
944 /* will only happen if all pointers were cached: */
945 if (!bch2_bkey_nr_ptrs(k.s_c))
946 k.k->type = KEY_TYPE_discard;
948 return bkey_whiteout(k.k);
951 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
954 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
955 const union bch_extent_entry *entry;
956 struct bch_extent_crc_unpacked crc;
957 const struct bch_extent_ptr *ptr;
958 const struct bch_extent_stripe_ptr *ec;
962 bkey_extent_entry_for_each(ptrs, entry) {
966 switch (__extent_entry_type(entry)) {
967 case BCH_EXTENT_ENTRY_ptr:
968 ptr = entry_to_ptr(entry);
969 ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
970 ? bch_dev_bkey_exists(c, ptr->dev)
973 pr_buf(out, "ptr: %u:%llu gen %u%s%s", ptr->dev,
974 (u64) ptr->offset, ptr->gen,
975 ptr->cached ? " cached" : "",
976 ca && ptr_stale(ca, ptr)
979 case BCH_EXTENT_ENTRY_crc32:
980 case BCH_EXTENT_ENTRY_crc64:
981 case BCH_EXTENT_ENTRY_crc128:
982 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
984 pr_buf(out, "crc: c_size %u size %u offset %u nonce %u csum %u compress %u",
986 crc.uncompressed_size,
987 crc.offset, crc.nonce,
989 crc.compression_type);
991 case BCH_EXTENT_ENTRY_stripe_ptr:
992 ec = &entry->stripe_ptr;
994 pr_buf(out, "ec: idx %llu block %u",
995 (u64) ec->idx, ec->block);
998 pr_buf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
1006 static const char *extent_ptr_invalid(const struct bch_fs *c,
1008 const struct bch_extent_ptr *ptr,
1009 unsigned size_ondisk,
1012 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1013 const struct bch_extent_ptr *ptr2;
1016 if (!bch2_dev_exists2(c, ptr->dev))
1017 return "pointer to invalid device";
1019 ca = bch_dev_bkey_exists(c, ptr->dev);
1021 return "pointer to invalid device";
1023 bkey_for_each_ptr(ptrs, ptr2)
1024 if (ptr != ptr2 && ptr->dev == ptr2->dev)
1025 return "multiple pointers to same device";
1027 if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
1028 return "offset past end of device";
1030 if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
1031 return "offset before first bucket";
1033 if (bucket_remainder(ca, ptr->offset) +
1034 size_ondisk > ca->mi.bucket_size)
1035 return "spans multiple buckets";
1040 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k)
1042 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1043 const union bch_extent_entry *entry;
1044 struct bch_extent_crc_unpacked crc;
1045 unsigned size_ondisk = k.k->size;
1047 unsigned nonce = UINT_MAX;
1049 if (k.k->type == KEY_TYPE_btree_ptr)
1050 size_ondisk = c->opts.btree_node_size;
1051 if (k.k->type == KEY_TYPE_btree_ptr_v2)
1052 size_ondisk = le16_to_cpu(bkey_s_c_to_btree_ptr_v2(k).v->sectors);
1054 bkey_extent_entry_for_each(ptrs, entry) {
1055 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
1056 return "invalid extent entry type";
1058 if (k.k->type == KEY_TYPE_btree_ptr &&
1059 !extent_entry_is_ptr(entry))
1060 return "has non ptr field";
1062 switch (extent_entry_type(entry)) {
1063 case BCH_EXTENT_ENTRY_ptr:
1064 reason = extent_ptr_invalid(c, k, &entry->ptr,
1065 size_ondisk, false);
1069 case BCH_EXTENT_ENTRY_crc32:
1070 case BCH_EXTENT_ENTRY_crc64:
1071 case BCH_EXTENT_ENTRY_crc128:
1072 crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1074 if (crc.offset + crc.live_size >
1075 crc.uncompressed_size)
1076 return "checksum offset + key size > uncompressed size";
1078 size_ondisk = crc.compressed_size;
1080 if (!bch2_checksum_type_valid(c, crc.csum_type))
1081 return "invalid checksum type";
1083 if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR)
1084 return "invalid compression type";
1086 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1087 if (nonce == UINT_MAX)
1088 nonce = crc.offset + crc.nonce;
1089 else if (nonce != crc.offset + crc.nonce)
1090 return "incorrect nonce";
1093 case BCH_EXTENT_ENTRY_stripe_ptr:
1101 void bch2_ptr_swab(struct bkey_s k)
1103 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1104 union bch_extent_entry *entry;
1107 for (d = (u64 *) ptrs.start;
1108 d != (u64 *) ptrs.end;
1112 for (entry = ptrs.start;
1114 entry = extent_entry_next(entry)) {
1115 switch (extent_entry_type(entry)) {
1116 case BCH_EXTENT_ENTRY_ptr:
1118 case BCH_EXTENT_ENTRY_crc32:
1119 entry->crc32.csum = swab32(entry->crc32.csum);
1121 case BCH_EXTENT_ENTRY_crc64:
1122 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1123 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1125 case BCH_EXTENT_ENTRY_crc128:
1126 entry->crc128.csum.hi = (__force __le64)
1127 swab64((__force u64) entry->crc128.csum.hi);
1128 entry->crc128.csum.lo = (__force __le64)
1129 swab64((__force u64) entry->crc128.csum.lo);
1131 case BCH_EXTENT_ENTRY_stripe_ptr:
1137 /* Generic extent code: */
1139 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1141 unsigned new_val_u64s = bkey_val_u64s(k.k);
1145 if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
1148 EBUG_ON(bkey_cmp(where, k.k->p) > 0);
1150 sub = where.offset - bkey_start_offset(k.k);
1155 k.k->type = KEY_TYPE_deleted;
1159 switch (k.k->type) {
1160 case KEY_TYPE_extent:
1161 case KEY_TYPE_reflink_v: {
1162 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1163 union bch_extent_entry *entry;
1164 bool seen_crc = false;
1166 bkey_extent_entry_for_each(ptrs, entry) {
1167 switch (extent_entry_type(entry)) {
1168 case BCH_EXTENT_ENTRY_ptr:
1170 entry->ptr.offset += sub;
1172 case BCH_EXTENT_ENTRY_crc32:
1173 entry->crc32.offset += sub;
1175 case BCH_EXTENT_ENTRY_crc64:
1176 entry->crc64.offset += sub;
1178 case BCH_EXTENT_ENTRY_crc128:
1179 entry->crc128.offset += sub;
1181 case BCH_EXTENT_ENTRY_stripe_ptr:
1185 if (extent_entry_is_crc(entry))
1191 case KEY_TYPE_reflink_p: {
1192 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1194 le64_add_cpu(&p.v->idx, sub);
1197 case KEY_TYPE_inline_data: {
1198 struct bkey_s_inline_data d = bkey_s_to_inline_data(k);
1200 sub = min_t(u64, sub << 9, bkey_val_bytes(d.k));
1204 bkey_val_bytes(d.k) - sub);
1206 new_val_u64s -= sub >> 3;
1211 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1212 BUG_ON(val_u64s_delta < 0);
1214 set_bkey_val_u64s(k.k, new_val_u64s);
1215 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1216 return -val_u64s_delta;
1219 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1221 unsigned new_val_u64s = bkey_val_u64s(k.k);
1225 if (bkey_cmp(where, k.k->p) >= 0)
1228 EBUG_ON(bkey_cmp(where, bkey_start_pos(k.k)) < 0);
1230 len = where.offset - bkey_start_offset(k.k);
1236 k.k->type = KEY_TYPE_deleted;
1240 switch (k.k->type) {
1241 case KEY_TYPE_inline_data:
1242 new_val_u64s = min(new_val_u64s, k.k->size << 6);
1246 val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1247 BUG_ON(val_u64s_delta < 0);
1249 set_bkey_val_u64s(k.k, new_val_u64s);
1250 memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1251 return -val_u64s_delta;