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