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