bcachefs: Erasure coding fixes & refactoring
[linux-block.git] / fs / bcachefs / extents.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
4  *
5  * Code for managing the extent btree and dynamically updating the writeback
6  * dirty sector count.
7  */
8
9 #include "bcachefs.h"
10 #include "bkey_methods.h"
11 #include "btree_gc.h"
12 #include "btree_io.h"
13 #include "btree_iter.h"
14 #include "buckets.h"
15 #include "checksum.h"
16 #include "debug.h"
17 #include "disk_groups.h"
18 #include "error.h"
19 #include "extents.h"
20 #include "inode.h"
21 #include "journal.h"
22 #include "replicas.h"
23 #include "super.h"
24 #include "super-io.h"
25 #include "trace.h"
26 #include "util.h"
27
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,
32 };
33
34 static void bch2_extent_crc_pack(union bch_extent_crc *,
35                                  struct bch_extent_crc_unpacked,
36                                  enum bch_extent_entry_type);
37
38 static struct bch_dev_io_failures *dev_io_failures(struct bch_io_failures *f,
39                                                    unsigned dev)
40 {
41         struct bch_dev_io_failures *i;
42
43         for (i = f->devs; i < f->devs + f->nr; i++)
44                 if (i->dev == dev)
45                         return i;
46
47         return NULL;
48 }
49
50 void bch2_mark_io_failure(struct bch_io_failures *failed,
51                           struct extent_ptr_decoded *p)
52 {
53         struct bch_dev_io_failures *f = dev_io_failures(failed, p->ptr.dev);
54
55         if (!f) {
56                 BUG_ON(failed->nr >= ARRAY_SIZE(failed->devs));
57
58                 f = &failed->devs[failed->nr++];
59                 f->dev          = p->ptr.dev;
60                 f->idx          = p->idx;
61                 f->nr_failed    = 1;
62                 f->nr_retries   = 0;
63         } else if (p->idx != f->idx) {
64                 f->idx          = p->idx;
65                 f->nr_failed    = 1;
66                 f->nr_retries   = 0;
67         } else {
68                 f->nr_failed++;
69         }
70 }
71
72 /*
73  * returns true if p1 is better than p2:
74  */
75 static inline bool ptr_better(struct bch_fs *c,
76                               const struct extent_ptr_decoded p1,
77                               const struct extent_ptr_decoded p2)
78 {
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);
82
83                 u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
84                 u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
85
86                 /* Pick at random, biased in favor of the faster device: */
87
88                 return bch2_rand_range(l1 + l2) > l1;
89         }
90
91         if (bch2_force_reconstruct_read)
92                 return p1.idx > p2.idx;
93
94         return p1.idx < p2.idx;
95 }
96
97 /*
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.
101  */
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)
105 {
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;
110         struct bch_dev *ca;
111         int ret = 0;
112
113         if (k.k->type == KEY_TYPE_error)
114                 return -EIO;
115
116         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
117                 ca = bch_dev_bkey_exists(c, p.ptr.dev);
118
119                 /*
120                  * If there are any dirty pointers it's an error if we can't
121                  * read:
122                  */
123                 if (!ret && !p.ptr.cached)
124                         ret = -EIO;
125
126                 if (p.ptr.cached && ptr_stale(ca, &p.ptr))
127                         continue;
128
129                 f = failed ? dev_io_failures(failed, p.ptr.dev) : NULL;
130                 if (f)
131                         p.idx = f->nr_failed < f->nr_retries
132                                 ? f->idx
133                                 : f->idx + 1;
134
135                 if (!p.idx &&
136                     !bch2_dev_is_readable(ca))
137                         p.idx++;
138
139                 if (bch2_force_reconstruct_read &&
140                     !p.idx && p.has_ec)
141                         p.idx++;
142
143                 if (p.idx >= (unsigned) p.has_ec + 1)
144                         continue;
145
146                 if (ret > 0 && !ptr_better(c, p, *pick))
147                         continue;
148
149                 *pick = p;
150                 ret = 1;
151         }
152
153         return ret;
154 }
155
156 /* KEY_TYPE_btree_ptr: */
157
158 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
159 {
160         if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
161                 return "value too big";
162
163         return bch2_bkey_ptrs_invalid(c, k);
164 }
165
166 void bch2_btree_ptr_debugcheck(struct bch_fs *c, struct bkey_s_c k)
167 {
168         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
169         const struct bch_extent_ptr *ptr;
170         const char *err;
171         char buf[160];
172         struct bucket_mark mark;
173         struct bch_dev *ca;
174
175         if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
176                 return;
177
178         if (!percpu_down_read_trylock(&c->mark_lock))
179                 return;
180
181         bkey_for_each_ptr(ptrs, ptr) {
182                 ca = bch_dev_bkey_exists(c, ptr->dev);
183
184                 mark = ptr_bucket_mark(ca, ptr);
185
186                 err = "stale";
187                 if (gen_after(mark.gen, ptr->gen))
188                         goto err;
189
190                 err = "inconsistent";
191                 if (mark.data_type != BCH_DATA_btree ||
192                     mark.dirty_sectors < c->opts.btree_node_size)
193                         goto err;
194         }
195 out:
196         percpu_up_read(&c->mark_lock);
197         return;
198 err:
199         bch2_fs_inconsistent(c, "%s btree pointer %s: bucket %zi gen %i mark %08x",
200                 err, (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf),
201                 PTR_BUCKET_NR(ca, ptr),
202                 mark.gen, (unsigned) mark.v.counter);
203         goto out;
204 }
205
206 void bch2_btree_ptr_to_text(struct printbuf *out, struct bch_fs *c,
207                             struct bkey_s_c k)
208 {
209         bch2_bkey_ptrs_to_text(out, c, k);
210 }
211
212 void bch2_btree_ptr_v2_to_text(struct printbuf *out, struct bch_fs *c,
213                             struct bkey_s_c k)
214 {
215         struct bkey_s_c_btree_ptr_v2 bp = bkey_s_c_to_btree_ptr_v2(k);
216
217         pr_buf(out, "seq %llx sectors %u written %u min_key ",
218                le64_to_cpu(bp.v->seq),
219                le16_to_cpu(bp.v->sectors),
220                le16_to_cpu(bp.v->sectors_written));
221
222         bch2_bpos_to_text(out, bp.v->min_key);
223         pr_buf(out, " ");
224         bch2_bkey_ptrs_to_text(out, c, k);
225 }
226
227 void bch2_btree_ptr_v2_compat(enum btree_id btree_id, unsigned version,
228                               unsigned big_endian, int write,
229                               struct bkey_s k)
230 {
231         struct bkey_s_btree_ptr_v2 bp = bkey_s_to_btree_ptr_v2(k);
232
233         compat_bpos(0, btree_id, version, big_endian, write, &bp.v->min_key);
234
235         if (version < bcachefs_metadata_version_inode_btree_change &&
236             btree_node_type_is_extents(btree_id) &&
237             bkey_cmp(bp.v->min_key, POS_MIN))
238                 bp.v->min_key = write
239                         ? bkey_predecessor(bp.v->min_key)
240                         : bkey_successor(bp.v->min_key);
241 }
242
243 /* KEY_TYPE_extent: */
244
245 const char *bch2_extent_invalid(const struct bch_fs *c, struct bkey_s_c k)
246 {
247         return bch2_bkey_ptrs_invalid(c, k);
248 }
249
250 void bch2_extent_debugcheck(struct bch_fs *c, struct bkey_s_c k)
251 {
252         struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
253         const union bch_extent_entry *entry;
254         struct extent_ptr_decoded p;
255         char buf[160];
256
257         if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) ||
258             !test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
259                 return;
260
261         if (!percpu_down_read_trylock(&c->mark_lock))
262                 return;
263
264         extent_for_each_ptr_decode(e, p, entry) {
265                 struct bch_dev *ca      = bch_dev_bkey_exists(c, p.ptr.dev);
266                 struct bucket_mark mark = ptr_bucket_mark(ca, &p.ptr);
267                 unsigned stale          = gen_after(mark.gen, p.ptr.gen);
268                 unsigned disk_sectors   = ptr_disk_sectors(p);
269                 unsigned mark_sectors   = p.ptr.cached
270                         ? mark.cached_sectors
271                         : mark.dirty_sectors;
272
273                 bch2_fs_inconsistent_on(stale && !p.ptr.cached, c,
274                         "stale dirty pointer (ptr gen %u bucket %u",
275                         p.ptr.gen, mark.gen);
276
277                 bch2_fs_inconsistent_on(stale > 96, c,
278                         "key too stale: %i", stale);
279
280                 bch2_fs_inconsistent_on(!stale &&
281                         (mark.data_type != BCH_DATA_user ||
282                          mark_sectors < disk_sectors), c,
283                         "extent pointer not marked: %s:\n"
284                         "type %u sectors %u < %u",
285                         (bch2_bkey_val_to_text(&PBUF(buf), c, e.s_c), buf),
286                         mark.data_type,
287                         mark_sectors, disk_sectors);
288         }
289
290         percpu_up_read(&c->mark_lock);
291 }
292
293 void bch2_extent_to_text(struct printbuf *out, struct bch_fs *c,
294                          struct bkey_s_c k)
295 {
296         bch2_bkey_ptrs_to_text(out, c, k);
297 }
298
299 enum merge_result bch2_extent_merge(struct bch_fs *c,
300                                     struct bkey_s _l, struct bkey_s _r)
301 {
302         struct bkey_s_extent l = bkey_s_to_extent(_l);
303         struct bkey_s_extent r = bkey_s_to_extent(_r);
304         union bch_extent_entry *en_l = l.v->start;
305         union bch_extent_entry *en_r = r.v->start;
306         struct bch_extent_crc_unpacked crc_l, crc_r;
307
308         if (bkey_val_u64s(l.k) != bkey_val_u64s(r.k))
309                 return BCH_MERGE_NOMERGE;
310
311         crc_l = bch2_extent_crc_unpack(l.k, NULL);
312
313         extent_for_each_entry(l, en_l) {
314                 en_r = vstruct_idx(r.v, (u64 *) en_l - l.v->_data);
315
316                 if (extent_entry_type(en_l) != extent_entry_type(en_r))
317                         return BCH_MERGE_NOMERGE;
318
319                 switch (extent_entry_type(en_l)) {
320                 case BCH_EXTENT_ENTRY_ptr: {
321                         const struct bch_extent_ptr *lp = &en_l->ptr;
322                         const struct bch_extent_ptr *rp = &en_r->ptr;
323                         struct bch_dev *ca;
324
325                         if (lp->offset + crc_l.compressed_size != rp->offset ||
326                             lp->dev                     != rp->dev ||
327                             lp->gen                     != rp->gen)
328                                 return BCH_MERGE_NOMERGE;
329
330                         /* We don't allow extents to straddle buckets: */
331                         ca = bch_dev_bkey_exists(c, lp->dev);
332
333                         if (PTR_BUCKET_NR(ca, lp) != PTR_BUCKET_NR(ca, rp))
334                                 return BCH_MERGE_NOMERGE;
335
336                         break;
337                 }
338                 case BCH_EXTENT_ENTRY_stripe_ptr:
339                         if (en_l->stripe_ptr.block      != en_r->stripe_ptr.block ||
340                             en_l->stripe_ptr.idx        != en_r->stripe_ptr.idx)
341                                 return BCH_MERGE_NOMERGE;
342                         break;
343                 case BCH_EXTENT_ENTRY_crc32:
344                 case BCH_EXTENT_ENTRY_crc64:
345                 case BCH_EXTENT_ENTRY_crc128:
346                         crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
347                         crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
348
349                         if (crc_l.csum_type             != crc_r.csum_type ||
350                             crc_l.compression_type      != crc_r.compression_type ||
351                             crc_l.nonce                 != crc_r.nonce)
352                                 return BCH_MERGE_NOMERGE;
353
354                         if (crc_l.offset + crc_l.live_size != crc_l.compressed_size ||
355                             crc_r.offset)
356                                 return BCH_MERGE_NOMERGE;
357
358                         if (!bch2_checksum_mergeable(crc_l.csum_type))
359                                 return BCH_MERGE_NOMERGE;
360
361                         if (crc_is_compressed(crc_l))
362                                 return BCH_MERGE_NOMERGE;
363
364                         if (crc_l.csum_type &&
365                             crc_l.uncompressed_size +
366                             crc_r.uncompressed_size > c->sb.encoded_extent_max)
367                                 return BCH_MERGE_NOMERGE;
368
369                         if (crc_l.uncompressed_size + crc_r.uncompressed_size >
370                             bch2_crc_field_size_max[extent_entry_type(en_l)])
371                                 return BCH_MERGE_NOMERGE;
372
373                         break;
374                 default:
375                         return BCH_MERGE_NOMERGE;
376                 }
377         }
378
379         extent_for_each_entry(l, en_l) {
380                 struct bch_extent_crc_unpacked crc_l, crc_r;
381
382                 en_r = vstruct_idx(r.v, (u64 *) en_l - l.v->_data);
383
384                 if (!extent_entry_is_crc(en_l))
385                         continue;
386
387                 crc_l = bch2_extent_crc_unpack(l.k, entry_to_crc(en_l));
388                 crc_r = bch2_extent_crc_unpack(r.k, entry_to_crc(en_r));
389
390                 crc_l.csum = bch2_checksum_merge(crc_l.csum_type,
391                                                  crc_l.csum,
392                                                  crc_r.csum,
393                                                  crc_r.uncompressed_size << 9);
394
395                 crc_l.uncompressed_size += crc_r.uncompressed_size;
396                 crc_l.compressed_size   += crc_r.compressed_size;
397
398                 bch2_extent_crc_pack(entry_to_crc(en_l), crc_l,
399                                      extent_entry_type(en_l));
400         }
401
402         bch2_key_resize(l.k, l.k->size + r.k->size);
403
404         return BCH_MERGE_MERGE;
405 }
406
407 /* KEY_TYPE_reservation: */
408
409 const char *bch2_reservation_invalid(const struct bch_fs *c, struct bkey_s_c k)
410 {
411         struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
412
413         if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation))
414                 return "incorrect value size";
415
416         if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX)
417                 return "invalid nr_replicas";
418
419         return NULL;
420 }
421
422 void bch2_reservation_to_text(struct printbuf *out, struct bch_fs *c,
423                               struct bkey_s_c k)
424 {
425         struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
426
427         pr_buf(out, "generation %u replicas %u",
428                le32_to_cpu(r.v->generation),
429                r.v->nr_replicas);
430 }
431
432 enum merge_result bch2_reservation_merge(struct bch_fs *c,
433                                          struct bkey_s _l, struct bkey_s _r)
434 {
435         struct bkey_s_reservation l = bkey_s_to_reservation(_l);
436         struct bkey_s_reservation r = bkey_s_to_reservation(_r);
437
438         if (l.v->generation != r.v->generation ||
439             l.v->nr_replicas != r.v->nr_replicas)
440                 return BCH_MERGE_NOMERGE;
441
442         if ((u64) l.k->size + r.k->size > KEY_SIZE_MAX) {
443                 bch2_key_resize(l.k, KEY_SIZE_MAX);
444                 bch2_cut_front_s(l.k->p, r.s);
445                 return BCH_MERGE_PARTIAL;
446         }
447
448         bch2_key_resize(l.k, l.k->size + r.k->size);
449
450         return BCH_MERGE_MERGE;
451 }
452
453 /* Extent checksum entries: */
454
455 /* returns true if not equal */
456 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
457                                          struct bch_extent_crc_unpacked r)
458 {
459         return (l.csum_type             != r.csum_type ||
460                 l.compression_type      != r.compression_type ||
461                 l.compressed_size       != r.compressed_size ||
462                 l.uncompressed_size     != r.uncompressed_size ||
463                 l.offset                != r.offset ||
464                 l.live_size             != r.live_size ||
465                 l.nonce                 != r.nonce ||
466                 bch2_crc_cmp(l.csum, r.csum));
467 }
468
469 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
470                                   struct bch_extent_crc_unpacked n)
471 {
472         return !crc_is_compressed(u) &&
473                 u.csum_type &&
474                 u.uncompressed_size > u.live_size &&
475                 bch2_csum_type_is_encryption(u.csum_type) ==
476                 bch2_csum_type_is_encryption(n.csum_type);
477 }
478
479 bool bch2_can_narrow_extent_crcs(struct bkey_s_c k,
480                                  struct bch_extent_crc_unpacked n)
481 {
482         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
483         struct bch_extent_crc_unpacked crc;
484         const union bch_extent_entry *i;
485
486         if (!n.csum_type)
487                 return false;
488
489         bkey_for_each_crc(k.k, ptrs, crc, i)
490                 if (can_narrow_crc(crc, n))
491                         return true;
492
493         return false;
494 }
495
496 /*
497  * We're writing another replica for this extent, so while we've got the data in
498  * memory we'll be computing a new checksum for the currently live data.
499  *
500  * If there are other replicas we aren't moving, and they are checksummed but
501  * not compressed, we can modify them to point to only the data that is
502  * currently live (so that readers won't have to bounce) while we've got the
503  * checksum we need:
504  */
505 bool bch2_bkey_narrow_crcs(struct bkey_i *k, struct bch_extent_crc_unpacked n)
506 {
507         struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
508         struct bch_extent_crc_unpacked u;
509         struct extent_ptr_decoded p;
510         union bch_extent_entry *i;
511         bool ret = false;
512
513         /* Find a checksum entry that covers only live data: */
514         if (!n.csum_type) {
515                 bkey_for_each_crc(&k->k, ptrs, u, i)
516                         if (!crc_is_compressed(u) &&
517                             u.csum_type &&
518                             u.live_size == u.uncompressed_size) {
519                                 n = u;
520                                 goto found;
521                         }
522                 return false;
523         }
524 found:
525         BUG_ON(crc_is_compressed(n));
526         BUG_ON(n.offset);
527         BUG_ON(n.live_size != k->k.size);
528
529 restart_narrow_pointers:
530         ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
531
532         bkey_for_each_ptr_decode(&k->k, ptrs, p, i)
533                 if (can_narrow_crc(p.crc, n)) {
534                         bch2_bkey_drop_ptr(bkey_i_to_s(k), &i->ptr);
535                         p.ptr.offset += p.crc.offset;
536                         p.crc = n;
537                         bch2_extent_ptr_decoded_append(k, &p);
538                         ret = true;
539                         goto restart_narrow_pointers;
540                 }
541
542         return ret;
543 }
544
545 static void bch2_extent_crc_pack(union bch_extent_crc *dst,
546                                  struct bch_extent_crc_unpacked src,
547                                  enum bch_extent_entry_type type)
548 {
549 #define set_common_fields(_dst, _src)                                   \
550                 _dst.type               = 1 << type;                    \
551                 _dst.csum_type          = _src.csum_type,               \
552                 _dst.compression_type   = _src.compression_type,        \
553                 _dst._compressed_size   = _src.compressed_size - 1,     \
554                 _dst._uncompressed_size = _src.uncompressed_size - 1,   \
555                 _dst.offset             = _src.offset
556
557         switch (type) {
558         case BCH_EXTENT_ENTRY_crc32:
559                 set_common_fields(dst->crc32, src);
560                 dst->crc32.csum  = *((__le32 *) &src.csum.lo);
561                 break;
562         case BCH_EXTENT_ENTRY_crc64:
563                 set_common_fields(dst->crc64, src);
564                 dst->crc64.nonce        = src.nonce;
565                 dst->crc64.csum_lo      = src.csum.lo;
566                 dst->crc64.csum_hi      = *((__le16 *) &src.csum.hi);
567                 break;
568         case BCH_EXTENT_ENTRY_crc128:
569                 set_common_fields(dst->crc128, src);
570                 dst->crc128.nonce       = src.nonce;
571                 dst->crc128.csum        = src.csum;
572                 break;
573         default:
574                 BUG();
575         }
576 #undef set_common_fields
577 }
578
579 void bch2_extent_crc_append(struct bkey_i *k,
580                             struct bch_extent_crc_unpacked new)
581 {
582         struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
583         union bch_extent_crc *crc = (void *) ptrs.end;
584         enum bch_extent_entry_type type;
585
586         if (bch_crc_bytes[new.csum_type]        <= 4 &&
587             new.uncompressed_size               <= CRC32_SIZE_MAX &&
588             new.nonce                           <= CRC32_NONCE_MAX)
589                 type = BCH_EXTENT_ENTRY_crc32;
590         else if (bch_crc_bytes[new.csum_type]   <= 10 &&
591                    new.uncompressed_size        <= CRC64_SIZE_MAX &&
592                    new.nonce                    <= CRC64_NONCE_MAX)
593                 type = BCH_EXTENT_ENTRY_crc64;
594         else if (bch_crc_bytes[new.csum_type]   <= 16 &&
595                    new.uncompressed_size        <= CRC128_SIZE_MAX &&
596                    new.nonce                    <= CRC128_NONCE_MAX)
597                 type = BCH_EXTENT_ENTRY_crc128;
598         else
599                 BUG();
600
601         bch2_extent_crc_pack(crc, new, type);
602
603         k->k.u64s += extent_entry_u64s(ptrs.end);
604
605         EBUG_ON(bkey_val_u64s(&k->k) > BKEY_EXTENT_VAL_U64s_MAX);
606 }
607
608 /* Generic code for keys with pointers: */
609
610 unsigned bch2_bkey_nr_ptrs(struct bkey_s_c k)
611 {
612         return bch2_bkey_devs(k).nr;
613 }
614
615 unsigned bch2_bkey_nr_ptrs_allocated(struct bkey_s_c k)
616 {
617         return k.k->type == KEY_TYPE_reservation
618                 ? bkey_s_c_to_reservation(k).v->nr_replicas
619                 : bch2_bkey_dirty_devs(k).nr;
620 }
621
622 unsigned bch2_bkey_nr_ptrs_fully_allocated(struct bkey_s_c k)
623 {
624         unsigned ret = 0;
625
626         if (k.k->type == KEY_TYPE_reservation) {
627                 ret = bkey_s_c_to_reservation(k).v->nr_replicas;
628         } else {
629                 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
630                 const union bch_extent_entry *entry;
631                 struct extent_ptr_decoded p;
632
633                 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
634                         ret += !p.ptr.cached && !crc_is_compressed(p.crc);
635         }
636
637         return ret;
638 }
639
640 unsigned bch2_bkey_sectors_compressed(struct bkey_s_c k)
641 {
642         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
643         const union bch_extent_entry *entry;
644         struct extent_ptr_decoded p;
645         unsigned ret = 0;
646
647         bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
648                 if (!p.ptr.cached && crc_is_compressed(p.crc))
649                         ret += p.crc.compressed_size;
650
651         return ret;
652 }
653
654 bool bch2_bkey_is_incompressible(struct bkey_s_c k)
655 {
656         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
657         const union bch_extent_entry *entry;
658         struct bch_extent_crc_unpacked crc;
659
660         bkey_for_each_crc(k.k, ptrs, crc, entry)
661                 if (crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
662                         return true;
663         return false;
664 }
665
666 bool bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size,
667                                 unsigned nr_replicas, bool compressed)
668 {
669         struct btree_trans trans;
670         struct btree_iter *iter;
671         struct bpos end = pos;
672         struct bkey_s_c k;
673         bool ret = true;
674         int err;
675
676         end.offset += size;
677
678         bch2_trans_init(&trans, c, 0, 0);
679
680         for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, pos,
681                            BTREE_ITER_SLOTS, k, err) {
682                 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
683                         break;
684
685                 if (nr_replicas > bch2_bkey_replicas(c, k) ||
686                     (!compressed && bch2_bkey_sectors_compressed(k))) {
687                         ret = false;
688                         break;
689                 }
690         }
691         bch2_trans_exit(&trans);
692
693         return ret;
694 }
695
696 unsigned bch2_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
697 {
698         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
699         const union bch_extent_entry *entry;
700         struct extent_ptr_decoded p;
701         unsigned replicas = 0;
702
703         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
704                 if (p.ptr.cached)
705                         continue;
706
707                 if (p.has_ec) {
708                         struct stripe *s =
709                                 genradix_ptr(&c->stripes[0], p.ec.idx);
710
711                         WARN_ON(!s);
712                         if (s)
713                                 replicas += s->nr_redundant;
714                 }
715
716                 replicas++;
717
718         }
719
720         return replicas;
721 }
722
723 static unsigned bch2_extent_ptr_durability(struct bch_fs *c,
724                                            struct extent_ptr_decoded p)
725 {
726         unsigned durability = 0;
727         struct bch_dev *ca;
728
729         if (p.ptr.cached)
730                 return 0;
731
732         ca = bch_dev_bkey_exists(c, p.ptr.dev);
733
734         if (ca->mi.state != BCH_MEMBER_STATE_FAILED)
735                 durability = max_t(unsigned, durability, ca->mi.durability);
736
737         if (p.has_ec) {
738                 struct stripe *s =
739                         genradix_ptr(&c->stripes[0], p.ec.idx);
740
741                 if (WARN_ON(!s))
742                         goto out;
743
744                 durability += s->nr_redundant;
745         }
746 out:
747         return durability;
748 }
749
750 unsigned bch2_bkey_durability(struct bch_fs *c, struct bkey_s_c k)
751 {
752         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
753         const union bch_extent_entry *entry;
754         struct extent_ptr_decoded p;
755         unsigned durability = 0;
756
757         bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
758                 durability += bch2_extent_ptr_durability(c, p);
759
760         return durability;
761 }
762
763 void bch2_bkey_mark_replicas_cached(struct bch_fs *c, struct bkey_s k,
764                                     unsigned target,
765                                     unsigned nr_desired_replicas)
766 {
767         struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
768         union bch_extent_entry *entry;
769         struct extent_ptr_decoded p;
770         int extra = bch2_bkey_durability(c, k.s_c) - nr_desired_replicas;
771
772         if (target && extra > 0)
773                 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
774                         int n = bch2_extent_ptr_durability(c, p);
775
776                         if (n && n <= extra &&
777                             !bch2_dev_in_target(c, p.ptr.dev, target)) {
778                                 entry->ptr.cached = true;
779                                 extra -= n;
780                         }
781                 }
782
783         if (extra > 0)
784                 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
785                         int n = bch2_extent_ptr_durability(c, p);
786
787                         if (n && n <= extra) {
788                                 entry->ptr.cached = true;
789                                 extra -= n;
790                         }
791                 }
792 }
793
794 void bch2_bkey_append_ptr(struct bkey_i *k,
795                           struct bch_extent_ptr ptr)
796 {
797         EBUG_ON(bch2_bkey_has_device(bkey_i_to_s_c(k), ptr.dev));
798
799         switch (k->k.type) {
800         case KEY_TYPE_btree_ptr:
801         case KEY_TYPE_btree_ptr_v2:
802         case KEY_TYPE_extent:
803                 EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
804
805                 ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
806
807                 memcpy((void *) &k->v + bkey_val_bytes(&k->k),
808                        &ptr,
809                        sizeof(ptr));
810                 k->u64s++;
811                 break;
812         default:
813                 BUG();
814         }
815 }
816
817 static inline void __extent_entry_insert(struct bkey_i *k,
818                                          union bch_extent_entry *dst,
819                                          union bch_extent_entry *new)
820 {
821         union bch_extent_entry *end = bkey_val_end(bkey_i_to_s(k));
822
823         memmove_u64s_up_small((u64 *) dst + extent_entry_u64s(new),
824                               dst, (u64 *) end - (u64 *) dst);
825         k->k.u64s += extent_entry_u64s(new);
826         memcpy_u64s_small(dst, new, extent_entry_u64s(new));
827 }
828
829 void bch2_extent_ptr_decoded_append(struct bkey_i *k,
830                                     struct extent_ptr_decoded *p)
831 {
832         struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(k));
833         struct bch_extent_crc_unpacked crc =
834                 bch2_extent_crc_unpack(&k->k, NULL);
835         union bch_extent_entry *pos;
836
837         if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
838                 pos = ptrs.start;
839                 goto found;
840         }
841
842         bkey_for_each_crc(&k->k, ptrs, crc, pos)
843                 if (!bch2_crc_unpacked_cmp(crc, p->crc)) {
844                         pos = extent_entry_next(pos);
845                         goto found;
846                 }
847
848         bch2_extent_crc_append(k, p->crc);
849         pos = bkey_val_end(bkey_i_to_s(k));
850 found:
851         p->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
852         __extent_entry_insert(k, pos, to_entry(&p->ptr));
853
854         if (p->has_ec) {
855                 p->ec.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr;
856                 __extent_entry_insert(k, pos, to_entry(&p->ec));
857         }
858 }
859
860 static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
861                                           union bch_extent_entry *entry)
862 {
863         union bch_extent_entry *i = ptrs.start;
864
865         if (i == entry)
866                 return NULL;
867
868         while (extent_entry_next(i) != entry)
869                 i = extent_entry_next(i);
870         return i;
871 }
872
873 union bch_extent_entry *bch2_bkey_drop_ptr(struct bkey_s k,
874                                            struct bch_extent_ptr *ptr)
875 {
876         struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
877         union bch_extent_entry *dst, *src, *prev;
878         bool drop_crc = true;
879
880         EBUG_ON(ptr < &ptrs.start->ptr ||
881                 ptr >= &ptrs.end->ptr);
882         EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
883
884         src = extent_entry_next(to_entry(ptr));
885         if (src != ptrs.end &&
886             !extent_entry_is_crc(src))
887                 drop_crc = false;
888
889         dst = to_entry(ptr);
890         while ((prev = extent_entry_prev(ptrs, dst))) {
891                 if (extent_entry_is_ptr(prev))
892                         break;
893
894                 if (extent_entry_is_crc(prev)) {
895                         if (drop_crc)
896                                 dst = prev;
897                         break;
898                 }
899
900                 dst = prev;
901         }
902
903         memmove_u64s_down(dst, src,
904                           (u64 *) ptrs.end - (u64 *) src);
905         k.k->u64s -= (u64 *) src - (u64 *) dst;
906
907         return dst;
908 }
909
910 void bch2_bkey_drop_device(struct bkey_s k, unsigned dev)
911 {
912         struct bch_extent_ptr *ptr;
913
914         bch2_bkey_drop_ptrs(k, ptr, ptr->dev == dev);
915 }
916
917 const struct bch_extent_ptr *
918 bch2_bkey_has_device(struct bkey_s_c k, unsigned dev)
919 {
920         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
921         const struct bch_extent_ptr *ptr;
922
923         bkey_for_each_ptr(ptrs, ptr)
924                 if (ptr->dev == dev)
925                         return ptr;
926
927         return NULL;
928 }
929
930 bool bch2_bkey_has_target(struct bch_fs *c, struct bkey_s_c k, unsigned target)
931 {
932         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
933         const struct bch_extent_ptr *ptr;
934
935         bkey_for_each_ptr(ptrs, ptr)
936                 if (bch2_dev_in_target(c, ptr->dev, target) &&
937                     (!ptr->cached ||
938                      !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
939                         return true;
940
941         return false;
942 }
943
944 bool bch2_bkey_matches_ptr(struct bch_fs *c, struct bkey_s_c k,
945                            struct bch_extent_ptr m, u64 offset)
946 {
947         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
948         const union bch_extent_entry *entry;
949         struct extent_ptr_decoded p;
950
951         bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
952                 if (p.ptr.dev   == m.dev &&
953                     p.ptr.gen   == m.gen &&
954                     (s64) p.ptr.offset + p.crc.offset - bkey_start_offset(k.k) ==
955                     (s64) m.offset  - offset)
956                         return true;
957
958         return false;
959 }
960
961 /*
962  * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
963  *
964  * Returns true if @k should be dropped entirely
965  *
966  * For existing keys, only called when btree nodes are being rewritten, not when
967  * they're merely being compacted/resorted in memory.
968  */
969 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
970 {
971         struct bch_extent_ptr *ptr;
972
973         bch2_bkey_drop_ptrs(k, ptr,
974                 ptr->cached &&
975                 ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr));
976
977         /* will only happen if all pointers were cached: */
978         if (!bch2_bkey_nr_ptrs(k.s_c))
979                 k.k->type = KEY_TYPE_discard;
980
981         return bkey_whiteout(k.k);
982 }
983
984 void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
985                             struct bkey_s_c k)
986 {
987         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
988         const union bch_extent_entry *entry;
989         struct bch_extent_crc_unpacked crc;
990         const struct bch_extent_ptr *ptr;
991         const struct bch_extent_stripe_ptr *ec;
992         struct bch_dev *ca;
993         bool first = true;
994
995         bkey_extent_entry_for_each(ptrs, entry) {
996                 if (!first)
997                         pr_buf(out, " ");
998
999                 switch (__extent_entry_type(entry)) {
1000                 case BCH_EXTENT_ENTRY_ptr:
1001                         ptr = entry_to_ptr(entry);
1002                         ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
1003                                 ? bch_dev_bkey_exists(c, ptr->dev)
1004                                 : NULL;
1005
1006                         pr_buf(out, "ptr: %u:%llu gen %u%s%s", ptr->dev,
1007                                (u64) ptr->offset, ptr->gen,
1008                                ptr->cached ? " cached" : "",
1009                                ca && ptr_stale(ca, ptr)
1010                                ? " stale" : "");
1011                         break;
1012                 case BCH_EXTENT_ENTRY_crc32:
1013                 case BCH_EXTENT_ENTRY_crc64:
1014                 case BCH_EXTENT_ENTRY_crc128:
1015                         crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1016
1017                         pr_buf(out, "crc: c_size %u size %u offset %u nonce %u csum %u compress %u",
1018                                crc.compressed_size,
1019                                crc.uncompressed_size,
1020                                crc.offset, crc.nonce,
1021                                crc.csum_type,
1022                                crc.compression_type);
1023                         break;
1024                 case BCH_EXTENT_ENTRY_stripe_ptr:
1025                         ec = &entry->stripe_ptr;
1026
1027                         pr_buf(out, "ec: idx %llu block %u",
1028                                (u64) ec->idx, ec->block);
1029                         break;
1030                 default:
1031                         pr_buf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
1032                         return;
1033                 }
1034
1035                 first = false;
1036         }
1037 }
1038
1039 static const char *extent_ptr_invalid(const struct bch_fs *c,
1040                                       struct bkey_s_c k,
1041                                       const struct bch_extent_ptr *ptr,
1042                                       unsigned size_ondisk,
1043                                       bool metadata)
1044 {
1045         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1046         const struct bch_extent_ptr *ptr2;
1047         struct bch_dev *ca;
1048
1049         if (!bch2_dev_exists2(c, ptr->dev))
1050                 return "pointer to invalid device";
1051
1052         ca = bch_dev_bkey_exists(c, ptr->dev);
1053         if (!ca)
1054                 return "pointer to invalid device";
1055
1056         bkey_for_each_ptr(ptrs, ptr2)
1057                 if (ptr != ptr2 && ptr->dev == ptr2->dev)
1058                         return "multiple pointers to same device";
1059
1060         if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
1061                 return "offset past end of device";
1062
1063         if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
1064                 return "offset before first bucket";
1065
1066         if (bucket_remainder(ca, ptr->offset) +
1067             size_ondisk > ca->mi.bucket_size)
1068                 return "spans multiple buckets";
1069
1070         return NULL;
1071 }
1072
1073 const char *bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k)
1074 {
1075         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1076         struct bch_devs_list devs;
1077         const union bch_extent_entry *entry;
1078         struct bch_extent_crc_unpacked crc;
1079         unsigned size_ondisk = k.k->size;
1080         const char *reason;
1081         unsigned nonce = UINT_MAX;
1082         unsigned i;
1083
1084         if (k.k->type == KEY_TYPE_btree_ptr)
1085                 size_ondisk = c->opts.btree_node_size;
1086         if (k.k->type == KEY_TYPE_btree_ptr_v2)
1087                 size_ondisk = le16_to_cpu(bkey_s_c_to_btree_ptr_v2(k).v->sectors);
1088
1089         bkey_extent_entry_for_each(ptrs, entry) {
1090                 if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
1091                         return "invalid extent entry type";
1092
1093                 if (k.k->type == KEY_TYPE_btree_ptr &&
1094                     !extent_entry_is_ptr(entry))
1095                         return "has non ptr field";
1096
1097                 switch (extent_entry_type(entry)) {
1098                 case BCH_EXTENT_ENTRY_ptr:
1099                         reason = extent_ptr_invalid(c, k, &entry->ptr,
1100                                                     size_ondisk, false);
1101                         if (reason)
1102                                 return reason;
1103                         break;
1104                 case BCH_EXTENT_ENTRY_crc32:
1105                 case BCH_EXTENT_ENTRY_crc64:
1106                 case BCH_EXTENT_ENTRY_crc128:
1107                         crc = bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
1108
1109                         if (crc.offset + crc.live_size >
1110                             crc.uncompressed_size)
1111                                 return "checksum offset + key size > uncompressed size";
1112
1113                         size_ondisk = crc.compressed_size;
1114
1115                         if (!bch2_checksum_type_valid(c, crc.csum_type))
1116                                 return "invalid checksum type";
1117
1118                         if (crc.compression_type >= BCH_COMPRESSION_TYPE_NR)
1119                                 return "invalid compression type";
1120
1121                         if (bch2_csum_type_is_encryption(crc.csum_type)) {
1122                                 if (nonce == UINT_MAX)
1123                                         nonce = crc.offset + crc.nonce;
1124                                 else if (nonce != crc.offset + crc.nonce)
1125                                         return "incorrect nonce";
1126                         }
1127                         break;
1128                 case BCH_EXTENT_ENTRY_stripe_ptr:
1129                         break;
1130                 }
1131         }
1132
1133         devs = bch2_bkey_devs(k);
1134         bubble_sort(devs.devs, devs.nr, u8_cmp);
1135         for (i = 0; i + 1 < devs.nr; i++)
1136                 if (devs.devs[i] == devs.devs[i + 1])
1137                         return "multiple ptrs to same device";
1138
1139         return NULL;
1140 }
1141
1142 void bch2_ptr_swab(struct bkey_s k)
1143 {
1144         struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1145         union bch_extent_entry *entry;
1146         u64 *d;
1147
1148         for (d =  (u64 *) ptrs.start;
1149              d != (u64 *) ptrs.end;
1150              d++)
1151                 *d = swab64(*d);
1152
1153         for (entry = ptrs.start;
1154              entry < ptrs.end;
1155              entry = extent_entry_next(entry)) {
1156                 switch (extent_entry_type(entry)) {
1157                 case BCH_EXTENT_ENTRY_ptr:
1158                         break;
1159                 case BCH_EXTENT_ENTRY_crc32:
1160                         entry->crc32.csum = swab32(entry->crc32.csum);
1161                         break;
1162                 case BCH_EXTENT_ENTRY_crc64:
1163                         entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
1164                         entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
1165                         break;
1166                 case BCH_EXTENT_ENTRY_crc128:
1167                         entry->crc128.csum.hi = (__force __le64)
1168                                 swab64((__force u64) entry->crc128.csum.hi);
1169                         entry->crc128.csum.lo = (__force __le64)
1170                                 swab64((__force u64) entry->crc128.csum.lo);
1171                         break;
1172                 case BCH_EXTENT_ENTRY_stripe_ptr:
1173                         break;
1174                 }
1175         }
1176 }
1177
1178 /* Generic extent code: */
1179
1180 int bch2_cut_front_s(struct bpos where, struct bkey_s k)
1181 {
1182         unsigned new_val_u64s = bkey_val_u64s(k.k);
1183         int val_u64s_delta;
1184         u64 sub;
1185
1186         if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
1187                 return 0;
1188
1189         EBUG_ON(bkey_cmp(where, k.k->p) > 0);
1190
1191         sub = where.offset - bkey_start_offset(k.k);
1192
1193         k.k->size -= sub;
1194
1195         if (!k.k->size) {
1196                 k.k->type = KEY_TYPE_deleted;
1197                 new_val_u64s = 0;
1198         }
1199
1200         switch (k.k->type) {
1201         case KEY_TYPE_extent:
1202         case KEY_TYPE_reflink_v: {
1203                 struct bkey_ptrs ptrs = bch2_bkey_ptrs(k);
1204                 union bch_extent_entry *entry;
1205                 bool seen_crc = false;
1206
1207                 bkey_extent_entry_for_each(ptrs, entry) {
1208                         switch (extent_entry_type(entry)) {
1209                         case BCH_EXTENT_ENTRY_ptr:
1210                                 if (!seen_crc)
1211                                         entry->ptr.offset += sub;
1212                                 break;
1213                         case BCH_EXTENT_ENTRY_crc32:
1214                                 entry->crc32.offset += sub;
1215                                 break;
1216                         case BCH_EXTENT_ENTRY_crc64:
1217                                 entry->crc64.offset += sub;
1218                                 break;
1219                         case BCH_EXTENT_ENTRY_crc128:
1220                                 entry->crc128.offset += sub;
1221                                 break;
1222                         case BCH_EXTENT_ENTRY_stripe_ptr:
1223                                 break;
1224                         }
1225
1226                         if (extent_entry_is_crc(entry))
1227                                 seen_crc = true;
1228                 }
1229
1230                 break;
1231         }
1232         case KEY_TYPE_reflink_p: {
1233                 struct bkey_s_reflink_p p = bkey_s_to_reflink_p(k);
1234
1235                 le64_add_cpu(&p.v->idx, sub);
1236                 break;
1237         }
1238         case KEY_TYPE_inline_data:
1239         case KEY_TYPE_indirect_inline_data: {
1240                 void *p = bkey_inline_data_p(k);
1241                 unsigned bytes = bkey_inline_data_bytes(k.k);
1242
1243                 sub = min_t(u64, sub << 9, bytes);
1244
1245                 memmove(p, p + sub, bytes - sub);
1246
1247                 new_val_u64s -= sub >> 3;
1248                 break;
1249         }
1250         }
1251
1252         val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1253         BUG_ON(val_u64s_delta < 0);
1254
1255         set_bkey_val_u64s(k.k, new_val_u64s);
1256         memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1257         return -val_u64s_delta;
1258 }
1259
1260 int bch2_cut_back_s(struct bpos where, struct bkey_s k)
1261 {
1262         unsigned new_val_u64s = bkey_val_u64s(k.k);
1263         int val_u64s_delta;
1264         u64 len = 0;
1265
1266         if (bkey_cmp(where, k.k->p) >= 0)
1267                 return 0;
1268
1269         EBUG_ON(bkey_cmp(where, bkey_start_pos(k.k)) < 0);
1270
1271         len = where.offset - bkey_start_offset(k.k);
1272
1273         k.k->p = where;
1274         k.k->size = len;
1275
1276         if (!len) {
1277                 k.k->type = KEY_TYPE_deleted;
1278                 new_val_u64s = 0;
1279         }
1280
1281         switch (k.k->type) {
1282         case KEY_TYPE_inline_data:
1283         case KEY_TYPE_indirect_inline_data:
1284                 new_val_u64s = (bkey_inline_data_offset(k.k) +
1285                                 min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
1286                 break;
1287         }
1288
1289         val_u64s_delta = bkey_val_u64s(k.k) - new_val_u64s;
1290         BUG_ON(val_u64s_delta < 0);
1291
1292         set_bkey_val_u64s(k.k, new_val_u64s);
1293         memset(bkey_val_end(k), 0, val_u64s_delta * sizeof(u64));
1294         return -val_u64s_delta;
1295 }