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