]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/extents.c
Update bcachefs sources to 0906b1fb49 bcachefs: fixes for 32 bit/big endian machines
[bcachefs-tools-debian] / libbcachefs / extents.c
1 /*
2  * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
3  *
4  * Code for managing the extent btree and dynamically updating the writeback
5  * dirty sector count.
6  */
7
8 #include "bcachefs.h"
9 #include "bkey_methods.h"
10 #include "btree_gc.h"
11 #include "btree_update.h"
12 #include "btree_update_interior.h"
13 #include "buckets.h"
14 #include "checksum.h"
15 #include "debug.h"
16 #include "dirent.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 #include "xattr.h"
27
28 #include <trace/events/bcachefs.h>
29
30 static void sort_key_next(struct btree_node_iter_large *iter,
31                           struct btree *b,
32                           struct btree_node_iter_set *i)
33 {
34         i->k += __btree_node_offset_to_key(b, i->k)->u64s;
35
36         if (i->k == i->end)
37                 *i = iter->data[--iter->used];
38 }
39
40 /*
41  * Returns true if l > r - unless l == r, in which case returns true if l is
42  * older than r.
43  *
44  * Necessary for btree_sort_fixup() - if there are multiple keys that compare
45  * equal in different sets, we have to process them newest to oldest.
46  */
47 #define key_sort_cmp(h, l, r)                                           \
48 ({                                                                      \
49         bkey_cmp_packed(b,                                              \
50                         __btree_node_offset_to_key(b, (l).k),           \
51                         __btree_node_offset_to_key(b, (r).k))           \
52                                                                         \
53         ?: (l).k - (r).k;                                               \
54 })
55
56 static inline bool should_drop_next_key(struct btree_node_iter_large *iter,
57                                         struct btree *b)
58 {
59         struct btree_node_iter_set *l = iter->data, *r = iter->data + 1;
60         struct bkey_packed *k = __btree_node_offset_to_key(b, l->k);
61
62         if (bkey_whiteout(k))
63                 return true;
64
65         if (iter->used < 2)
66                 return false;
67
68         if (iter->used > 2 &&
69             key_sort_cmp(iter, r[0], r[1]) >= 0)
70                 r++;
71
72         /*
73          * key_sort_cmp() ensures that when keys compare equal the older key
74          * comes first; so if l->k compares equal to r->k then l->k is older and
75          * should be dropped.
76          */
77         return !bkey_cmp_packed(b,
78                                 __btree_node_offset_to_key(b, l->k),
79                                 __btree_node_offset_to_key(b, r->k));
80 }
81
82 struct btree_nr_keys bch2_key_sort_fix_overlapping(struct bset *dst,
83                                         struct btree *b,
84                                         struct btree_node_iter_large *iter)
85 {
86         struct bkey_packed *out = dst->start;
87         struct btree_nr_keys nr;
88
89         memset(&nr, 0, sizeof(nr));
90
91         heap_resort(iter, key_sort_cmp);
92
93         while (!bch2_btree_node_iter_large_end(iter)) {
94                 if (!should_drop_next_key(iter, b)) {
95                         struct bkey_packed *k =
96                                 __btree_node_offset_to_key(b, iter->data->k);
97
98                         bkey_copy(out, k);
99                         btree_keys_account_key_add(&nr, 0, out);
100                         out = bkey_next(out);
101                 }
102
103                 sort_key_next(iter, b, iter->data);
104                 heap_sift_down(iter, 0, key_sort_cmp);
105         }
106
107         dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
108         return nr;
109 }
110
111 /* Common among btree and extent ptrs */
112
113 const struct bch_extent_ptr *
114 bch2_extent_has_device(struct bkey_s_c_extent e, unsigned dev)
115 {
116         const struct bch_extent_ptr *ptr;
117
118         extent_for_each_ptr(e, ptr)
119                 if (ptr->dev == dev)
120                         return ptr;
121
122         return NULL;
123 }
124
125 bool bch2_extent_drop_device(struct bkey_s_extent e, unsigned dev)
126 {
127         struct bch_extent_ptr *ptr;
128         bool dropped = false;
129
130         extent_for_each_ptr_backwards(e, ptr)
131                 if (ptr->dev == dev) {
132                         __bch2_extent_drop_ptr(e, ptr);
133                         dropped = true;
134                 }
135
136         if (dropped)
137                 bch2_extent_drop_redundant_crcs(e);
138         return dropped;
139 }
140
141 const struct bch_extent_ptr *
142 bch2_extent_has_group(struct bch_fs *c, struct bkey_s_c_extent e, unsigned group)
143 {
144         const struct bch_extent_ptr *ptr;
145
146         extent_for_each_ptr(e, ptr) {
147                 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
148
149                 if (ca->mi.group &&
150                     ca->mi.group - 1 == group)
151                         return ptr;
152         }
153
154         return NULL;
155 }
156
157 const struct bch_extent_ptr *
158 bch2_extent_has_target(struct bch_fs *c, struct bkey_s_c_extent e, unsigned target)
159 {
160         const struct bch_extent_ptr *ptr;
161
162         extent_for_each_ptr(e, ptr)
163                 if (bch2_dev_in_target(c, ptr->dev, target) &&
164                     (!ptr->cached ||
165                      !ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr)))
166                         return ptr;
167
168         return NULL;
169 }
170
171 unsigned bch2_extent_nr_ptrs(struct bkey_s_c_extent e)
172 {
173         const struct bch_extent_ptr *ptr;
174         unsigned nr_ptrs = 0;
175
176         extent_for_each_ptr(e, ptr)
177                 nr_ptrs++;
178
179         return nr_ptrs;
180 }
181
182 unsigned bch2_extent_nr_dirty_ptrs(struct bkey_s_c k)
183 {
184         struct bkey_s_c_extent e;
185         const struct bch_extent_ptr *ptr;
186         unsigned nr_ptrs = 0;
187
188         switch (k.k->type) {
189         case BCH_EXTENT:
190         case BCH_EXTENT_CACHED:
191                 e = bkey_s_c_to_extent(k);
192
193                 extent_for_each_ptr(e, ptr)
194                         nr_ptrs += !ptr->cached;
195                 break;
196
197         case BCH_RESERVATION:
198                 nr_ptrs = bkey_s_c_to_reservation(k).v->nr_replicas;
199                 break;
200         }
201
202         return nr_ptrs;
203 }
204
205 unsigned bch2_extent_ptr_durability(struct bch_fs *c,
206                                     const struct bch_extent_ptr *ptr)
207 {
208         struct bch_dev *ca;
209
210         if (ptr->cached)
211                 return 0;
212
213         ca = bch_dev_bkey_exists(c, ptr->dev);
214
215         if (ca->mi.state == BCH_MEMBER_STATE_FAILED)
216                 return 0;
217
218         return ca->mi.durability;
219 }
220
221 unsigned bch2_extent_durability(struct bch_fs *c, struct bkey_s_c_extent e)
222 {
223         const struct bch_extent_ptr *ptr;
224         unsigned durability = 0;
225
226         extent_for_each_ptr(e, ptr)
227                 durability += bch2_extent_ptr_durability(c, ptr);
228
229         return durability;
230 }
231
232 unsigned bch2_extent_is_compressed(struct bkey_s_c k)
233 {
234         struct bkey_s_c_extent e;
235         const struct bch_extent_ptr *ptr;
236         struct bch_extent_crc_unpacked crc;
237         unsigned ret = 0;
238
239         switch (k.k->type) {
240         case BCH_EXTENT:
241         case BCH_EXTENT_CACHED:
242                 e = bkey_s_c_to_extent(k);
243
244                 extent_for_each_ptr_crc(e, ptr, crc)
245                         if (!ptr->cached &&
246                             crc.compression_type != BCH_COMPRESSION_NONE &&
247                             crc.compressed_size < crc.live_size)
248                                 ret = max_t(unsigned, ret, crc.compressed_size);
249         }
250
251         return ret;
252 }
253
254 bool bch2_extent_matches_ptr(struct bch_fs *c, struct bkey_s_c_extent e,
255                              struct bch_extent_ptr m, u64 offset)
256 {
257         const struct bch_extent_ptr *ptr;
258         struct bch_extent_crc_unpacked crc;
259
260         extent_for_each_ptr_crc(e, ptr, crc)
261                 if (ptr->dev    == m.dev &&
262                     ptr->gen    == m.gen &&
263                     (s64) ptr->offset + crc.offset - bkey_start_offset(e.k) ==
264                     (s64) m.offset  - offset)
265                         return ptr;
266
267         return NULL;
268 }
269
270 /* Doesn't cleanup redundant crcs */
271 void __bch2_extent_drop_ptr(struct bkey_s_extent e, struct bch_extent_ptr *ptr)
272 {
273         EBUG_ON(ptr < &e.v->start->ptr ||
274                 ptr >= &extent_entry_last(e)->ptr);
275         EBUG_ON(ptr->type != 1 << BCH_EXTENT_ENTRY_ptr);
276         memmove_u64s_down(ptr, ptr + 1,
277                           (u64 *) extent_entry_last(e) - (u64 *) (ptr + 1));
278         e.k->u64s -= sizeof(*ptr) / sizeof(u64);
279 }
280
281 void bch2_extent_drop_ptr(struct bkey_s_extent e, struct bch_extent_ptr *ptr)
282 {
283         __bch2_extent_drop_ptr(e, ptr);
284         bch2_extent_drop_redundant_crcs(e);
285 }
286
287 static inline bool can_narrow_crc(struct bch_extent_crc_unpacked u,
288                                   struct bch_extent_crc_unpacked n)
289 {
290         return !u.compression_type &&
291                 u.csum_type &&
292                 u.uncompressed_size > u.live_size &&
293                 bch2_csum_type_is_encryption(u.csum_type) ==
294                 bch2_csum_type_is_encryption(n.csum_type);
295 }
296
297 bool bch2_can_narrow_extent_crcs(struct bkey_s_c_extent e,
298                                  struct bch_extent_crc_unpacked n)
299 {
300         struct bch_extent_crc_unpacked crc;
301         const union bch_extent_entry *i;
302
303         if (!n.csum_type)
304                 return false;
305
306         extent_for_each_crc(e, crc, i)
307                 if (can_narrow_crc(crc, n))
308                         return true;
309
310         return false;
311 }
312
313 /*
314  * We're writing another replica for this extent, so while we've got the data in
315  * memory we'll be computing a new checksum for the currently live data.
316  *
317  * If there are other replicas we aren't moving, and they are checksummed but
318  * not compressed, we can modify them to point to only the data that is
319  * currently live (so that readers won't have to bounce) while we've got the
320  * checksum we need:
321  */
322 bool bch2_extent_narrow_crcs(struct bkey_i_extent *e,
323                              struct bch_extent_crc_unpacked n)
324 {
325         struct bch_extent_crc_unpacked u;
326         struct bch_extent_ptr *ptr;
327         union bch_extent_entry *i;
328
329         /* Find a checksum entry that covers only live data: */
330         if (!n.csum_type)
331                 extent_for_each_crc(extent_i_to_s(e), u, i)
332                         if (!u.compression_type &&
333                             u.csum_type &&
334                             u.live_size == u.uncompressed_size) {
335                                 n = u;
336                                 break;
337                         }
338
339         if (!bch2_can_narrow_extent_crcs(extent_i_to_s_c(e), n))
340                 return false;
341
342         BUG_ON(n.compression_type);
343         BUG_ON(n.offset);
344         BUG_ON(n.live_size != e->k.size);
345
346         bch2_extent_crc_append(e, n);
347 restart_narrow_pointers:
348         extent_for_each_ptr_crc(extent_i_to_s(e), ptr, u)
349                 if (can_narrow_crc(u, n)) {
350                         ptr->offset += u.offset;
351                         extent_ptr_append(e, *ptr);
352                         __bch2_extent_drop_ptr(extent_i_to_s(e), ptr);
353                         goto restart_narrow_pointers;
354                 }
355
356         bch2_extent_drop_redundant_crcs(extent_i_to_s(e));
357         return true;
358 }
359
360 /* returns true if not equal */
361 static inline bool bch2_crc_unpacked_cmp(struct bch_extent_crc_unpacked l,
362                                          struct bch_extent_crc_unpacked r)
363 {
364         return (l.csum_type             != r.csum_type ||
365                 l.compression_type      != r.compression_type ||
366                 l.compressed_size       != r.compressed_size ||
367                 l.uncompressed_size     != r.uncompressed_size ||
368                 l.offset                != r.offset ||
369                 l.live_size             != r.live_size ||
370                 l.nonce                 != r.nonce ||
371                 bch2_crc_cmp(l.csum, r.csum));
372 }
373
374 void bch2_extent_drop_redundant_crcs(struct bkey_s_extent e)
375 {
376         union bch_extent_entry *entry = e.v->start;
377         union bch_extent_crc *crc, *prev = NULL;
378         struct bch_extent_crc_unpacked u, prev_u = { 0 };
379
380         while (entry != extent_entry_last(e)) {
381                 union bch_extent_entry *next = extent_entry_next(entry);
382                 size_t crc_u64s = extent_entry_u64s(entry);
383
384                 if (!extent_entry_is_crc(entry))
385                         goto next;
386
387                 crc = entry_to_crc(entry);
388                 u = bch2_extent_crc_unpack(e.k, crc);
389
390                 if (next == extent_entry_last(e)) {
391                         /* crc entry with no pointers after it: */
392                         goto drop;
393                 }
394
395                 if (extent_entry_is_crc(next)) {
396                         /* no pointers before next crc entry: */
397                         goto drop;
398                 }
399
400                 if (prev && !bch2_crc_unpacked_cmp(u, prev_u)) {
401                         /* identical to previous crc entry: */
402                         goto drop;
403                 }
404
405                 if (!prev &&
406                     !u.csum_type &&
407                     !u.compression_type) {
408                         /* null crc entry: */
409                         union bch_extent_entry *e2;
410
411                         extent_for_each_entry_from(e, e2, extent_entry_next(entry)) {
412                                 if (!extent_entry_is_ptr(e2))
413                                         break;
414
415                                 e2->ptr.offset += u.offset;
416                         }
417                         goto drop;
418                 }
419
420                 prev = crc;
421                 prev_u = u;
422 next:
423                 entry = next;
424                 continue;
425 drop:
426                 memmove_u64s_down(crc, next,
427                                   (u64 *) extent_entry_last(e) - (u64 *) next);
428                 e.k->u64s -= crc_u64s;
429         }
430
431         EBUG_ON(bkey_val_u64s(e.k) && !bch2_extent_nr_ptrs(e.c));
432 }
433
434 static bool should_drop_ptr(const struct bch_fs *c,
435                             struct bkey_s_c_extent e,
436                             const struct bch_extent_ptr *ptr)
437 {
438         return ptr->cached && ptr_stale(bch_dev_bkey_exists(c, ptr->dev), ptr);
439 }
440
441 static void bch2_extent_drop_stale(struct bch_fs *c, struct bkey_s_extent e)
442 {
443         struct bch_extent_ptr *ptr = &e.v->start->ptr;
444         bool dropped = false;
445
446         while ((ptr = extent_ptr_next(e, ptr)))
447                 if (should_drop_ptr(c, e.c, ptr)) {
448                         __bch2_extent_drop_ptr(e, ptr);
449                         dropped = true;
450                 } else
451                         ptr++;
452
453         if (dropped)
454                 bch2_extent_drop_redundant_crcs(e);
455 }
456
457 bool bch2_ptr_normalize(struct bch_fs *c, struct btree *b, struct bkey_s k)
458 {
459         return bch2_extent_normalize(c, k);
460 }
461
462 void bch2_ptr_swab(const struct bkey_format *f, struct bkey_packed *k)
463 {
464         switch (k->type) {
465         case BCH_EXTENT:
466         case BCH_EXTENT_CACHED: {
467                 union bch_extent_entry *entry;
468                 u64 *d = (u64 *) bkeyp_val(f, k);
469                 unsigned i;
470
471                 for (i = 0; i < bkeyp_val_u64s(f, k); i++)
472                         d[i] = swab64(d[i]);
473
474                 for (entry = (union bch_extent_entry *) d;
475                      entry < (union bch_extent_entry *) (d + bkeyp_val_u64s(f, k));
476                      entry = extent_entry_next(entry)) {
477                         switch (extent_entry_type(entry)) {
478                         case BCH_EXTENT_ENTRY_crc32:
479                                 entry->crc32.csum = swab32(entry->crc32.csum);
480                                 break;
481                         case BCH_EXTENT_ENTRY_crc64:
482                                 entry->crc64.csum_hi = swab16(entry->crc64.csum_hi);
483                                 entry->crc64.csum_lo = swab64(entry->crc64.csum_lo);
484                                 break;
485                         case BCH_EXTENT_ENTRY_crc128:
486                                 entry->crc128.csum.hi = (__force __le64)
487                                         swab64((__force u64) entry->crc128.csum.hi);
488                                 entry->crc128.csum.lo = (__force __le64)
489                                         swab64((__force u64) entry->crc128.csum.lo);
490                                 break;
491                         case BCH_EXTENT_ENTRY_ptr:
492                                 break;
493                         }
494                 }
495                 break;
496         }
497         }
498 }
499
500 static const char *extent_ptr_invalid(const struct bch_fs *c,
501                                       struct bkey_s_c_extent e,
502                                       const struct bch_extent_ptr *ptr,
503                                       unsigned size_ondisk,
504                                       bool metadata)
505 {
506         const struct bch_extent_ptr *ptr2;
507         struct bch_dev *ca;
508
509         if (ptr->dev >= c->sb.nr_devices ||
510             !c->devs[ptr->dev])
511                 return "pointer to invalid device";
512
513         ca = bch_dev_bkey_exists(c, ptr->dev);
514         if (!ca)
515                 return "pointer to invalid device";
516
517         extent_for_each_ptr(e, ptr2)
518                 if (ptr != ptr2 && ptr->dev == ptr2->dev)
519                         return "multiple pointers to same device";
520
521         if (ptr->offset + size_ondisk > bucket_to_sector(ca, ca->mi.nbuckets))
522                 return "offset past end of device";
523
524         if (ptr->offset < bucket_to_sector(ca, ca->mi.first_bucket))
525                 return "offset before first bucket";
526
527         if (bucket_remainder(ca, ptr->offset) +
528             size_ondisk > ca->mi.bucket_size)
529                 return "spans multiple buckets";
530
531         return NULL;
532 }
533
534 static size_t extent_print_ptrs(struct bch_fs *c, char *buf,
535                                 size_t size, struct bkey_s_c_extent e)
536 {
537         char *out = buf, *end = buf + size;
538         const union bch_extent_entry *entry;
539         struct bch_extent_crc_unpacked crc;
540         const struct bch_extent_ptr *ptr;
541         struct bch_dev *ca;
542         bool first = true;
543
544 #define p(...)  (out += scnprintf(out, end - out, __VA_ARGS__))
545
546         extent_for_each_entry(e, entry) {
547                 if (!first)
548                         p(" ");
549
550                 switch (__extent_entry_type(entry)) {
551                 case BCH_EXTENT_ENTRY_crc32:
552                 case BCH_EXTENT_ENTRY_crc64:
553                 case BCH_EXTENT_ENTRY_crc128:
554                         crc = bch2_extent_crc_unpack(e.k, entry_to_crc(entry));
555
556                         p("crc: c_size %u size %u offset %u nonce %u csum %u compress %u",
557                           crc.compressed_size,
558                           crc.uncompressed_size,
559                           crc.offset, crc.nonce,
560                           crc.csum_type,
561                           crc.compression_type);
562                         break;
563                 case BCH_EXTENT_ENTRY_ptr:
564                         ptr = entry_to_ptr(entry);
565                         ca = ptr->dev < c->sb.nr_devices && c->devs[ptr->dev]
566                                 ? bch_dev_bkey_exists(c, ptr->dev)
567                                 : NULL;
568
569                         p("ptr: %u:%llu gen %u%s%s", ptr->dev,
570                           (u64) ptr->offset, ptr->gen,
571                           ptr->cached ? " cached" : "",
572                           ca && ptr_stale(ca, ptr)
573                           ? " stale" : "");
574                         break;
575                 default:
576                         p("(invalid extent entry %.16llx)", *((u64 *) entry));
577                         goto out;
578                 }
579
580                 first = false;
581         }
582 out:
583         if (bkey_extent_is_cached(e.k))
584                 p(" cached");
585 #undef p
586         return out - buf;
587 }
588
589 static inline bool dev_latency_better(struct bch_fs *c,
590                               const struct bch_extent_ptr *ptr1,
591                               const struct bch_extent_ptr *ptr2)
592 {
593         struct bch_dev *dev1 = bch_dev_bkey_exists(c, ptr1->dev);
594         struct bch_dev *dev2 = bch_dev_bkey_exists(c, ptr2->dev);
595         u64 l1 = atomic64_read(&dev1->cur_latency[READ]);
596         u64 l2 = atomic64_read(&dev2->cur_latency[READ]);
597
598         /* Pick at random, biased in favor of the faster device: */
599
600         return bch2_rand_range(l1 + l2) > l1;
601 }
602
603 static int extent_pick_read_device(struct bch_fs *c,
604                                    struct bkey_s_c_extent e,
605                                    struct bch_devs_mask *avoid,
606                                    struct extent_pick_ptr *pick)
607 {
608         const struct bch_extent_ptr *ptr;
609         struct bch_extent_crc_unpacked crc;
610         struct bch_dev *ca;
611         int ret = 0;
612
613         extent_for_each_ptr_crc(e, ptr, crc) {
614                 ca = bch_dev_bkey_exists(c, ptr->dev);
615
616                 if (ptr->cached && ptr_stale(ca, ptr))
617                         continue;
618
619                 if (avoid && test_bit(ptr->dev, avoid->d))
620                         continue;
621
622                 if (ret && !dev_latency_better(c, ptr, &pick->ptr))
623                         continue;
624
625                 *pick = (struct extent_pick_ptr) {
626                         .ptr    = *ptr,
627                         .crc    = crc,
628                 };
629
630                 ret = 1;
631         }
632
633         return ret;
634 }
635
636 /* Btree ptrs */
637
638 const char *bch2_btree_ptr_invalid(const struct bch_fs *c, struct bkey_s_c k)
639 {
640         if (bkey_extent_is_cached(k.k))
641                 return "cached";
642
643         if (k.k->size)
644                 return "nonzero key size";
645
646         if (bkey_val_u64s(k.k) > BKEY_BTREE_PTR_VAL_U64s_MAX)
647                 return "value too big";
648
649         switch (k.k->type) {
650         case BCH_EXTENT: {
651                 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
652                 const union bch_extent_entry *entry;
653                 const struct bch_extent_ptr *ptr;
654                 const char *reason;
655
656                 extent_for_each_entry(e, entry) {
657                         if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
658                                 return "invalid extent entry type";
659
660                         if (extent_entry_is_crc(entry))
661                                 return "has crc field";
662                 }
663
664                 extent_for_each_ptr(e, ptr) {
665                         reason = extent_ptr_invalid(c, e, ptr,
666                                                     c->opts.btree_node_size,
667                                                     true);
668                         if (reason)
669                                 return reason;
670                 }
671
672                 return NULL;
673         }
674
675         default:
676                 return "invalid value type";
677         }
678 }
679
680 void bch2_btree_ptr_debugcheck(struct bch_fs *c, struct btree *b,
681                                struct bkey_s_c k)
682 {
683         struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
684         const struct bch_extent_ptr *ptr;
685         unsigned seq;
686         const char *err;
687         char buf[160];
688         struct bucket_mark mark;
689         struct bch_dev *ca;
690         unsigned replicas = 0;
691         bool bad;
692
693         extent_for_each_ptr(e, ptr) {
694                 ca = bch_dev_bkey_exists(c, ptr->dev);
695                 replicas++;
696
697                 if (!test_bit(BCH_FS_ALLOC_READ_DONE, &c->flags))
698                         continue;
699
700                 err = "stale";
701                 if (ptr_stale(ca, ptr))
702                         goto err;
703
704                 do {
705                         seq = read_seqcount_begin(&c->gc_pos_lock);
706                         mark = ptr_bucket_mark(ca, ptr);
707
708                         bad = gc_pos_cmp(c->gc_pos, gc_pos_btree_node(b)) > 0 &&
709                                 (mark.data_type != BCH_DATA_BTREE ||
710                                  mark.dirty_sectors < c->opts.btree_node_size);
711                 } while (read_seqcount_retry(&c->gc_pos_lock, seq));
712
713                 err = "inconsistent";
714                 if (bad)
715                         goto err;
716         }
717
718         if (!bch2_bkey_replicas_marked(c, BCH_DATA_BTREE, e.s_c)) {
719                 bch2_bkey_val_to_text(c, btree_node_type(b),
720                                      buf, sizeof(buf), k);
721                 bch2_fs_bug(c,
722                         "btree key bad (replicas not marked in superblock):\n%s",
723                         buf);
724                 return;
725         }
726
727         return;
728 err:
729         bch2_bkey_val_to_text(c, btree_node_type(b), buf, sizeof(buf), k);
730         bch2_fs_bug(c, "%s btree pointer %s: bucket %zi "
731                       "gen %i mark %08x",
732                       err, buf, PTR_BUCKET_NR(ca, ptr),
733                       mark.gen, (unsigned) mark.v.counter);
734 }
735
736 void bch2_btree_ptr_to_text(struct bch_fs *c, char *buf,
737                             size_t size, struct bkey_s_c k)
738 {
739         char *out = buf, *end = buf + size;
740         const char *invalid;
741
742 #define p(...)  (out += scnprintf(out, end - out, __VA_ARGS__))
743
744         if (bkey_extent_is_data(k.k))
745                 out += extent_print_ptrs(c, buf, size, bkey_s_c_to_extent(k));
746
747         invalid = bch2_btree_ptr_invalid(c, k);
748         if (invalid)
749                 p(" invalid: %s", invalid);
750 #undef p
751 }
752
753 int bch2_btree_pick_ptr(struct bch_fs *c, const struct btree *b,
754                         struct bch_devs_mask *avoid,
755                         struct extent_pick_ptr *pick)
756 {
757         return extent_pick_read_device(c, bkey_i_to_s_c_extent(&b->key),
758                                        avoid, pick);
759 }
760
761 /* Extents */
762
763 static bool __bch2_cut_front(struct bpos where, struct bkey_s k)
764 {
765         u64 len = 0;
766
767         if (bkey_cmp(where, bkey_start_pos(k.k)) <= 0)
768                 return false;
769
770         EBUG_ON(bkey_cmp(where, k.k->p) > 0);
771
772         len = k.k->p.offset - where.offset;
773
774         BUG_ON(len > k.k->size);
775
776         /*
777          * Don't readjust offset if the key size is now 0, because that could
778          * cause offset to point to the next bucket:
779          */
780         if (!len)
781                 __set_bkey_deleted(k.k);
782         else if (bkey_extent_is_data(k.k)) {
783                 struct bkey_s_extent e = bkey_s_to_extent(k);
784                 union bch_extent_entry *entry;
785                 bool seen_crc = false;
786
787                 extent_for_each_entry(e, entry) {
788                         switch (extent_entry_type(entry)) {
789                         case BCH_EXTENT_ENTRY_ptr:
790                                 if (!seen_crc)
791                                         entry->ptr.offset += e.k->size - len;
792                                 break;
793                         case BCH_EXTENT_ENTRY_crc32:
794                                 entry->crc32.offset += e.k->size - len;
795                                 break;
796                         case BCH_EXTENT_ENTRY_crc64:
797                                 entry->crc64.offset += e.k->size - len;
798                                 break;
799                         case BCH_EXTENT_ENTRY_crc128:
800                                 entry->crc128.offset += e.k->size - len;
801                                 break;
802                         }
803
804                         if (extent_entry_is_crc(entry))
805                                 seen_crc = true;
806                 }
807         }
808
809         k.k->size = len;
810
811         return true;
812 }
813
814 bool bch2_cut_front(struct bpos where, struct bkey_i *k)
815 {
816         return __bch2_cut_front(where, bkey_i_to_s(k));
817 }
818
819 bool bch2_cut_back(struct bpos where, struct bkey *k)
820 {
821         u64 len = 0;
822
823         if (bkey_cmp(where, k->p) >= 0)
824                 return false;
825
826         EBUG_ON(bkey_cmp(where, bkey_start_pos(k)) < 0);
827
828         len = where.offset - bkey_start_offset(k);
829
830         BUG_ON(len > k->size);
831
832         k->p = where;
833         k->size = len;
834
835         if (!len)
836                 __set_bkey_deleted(k);
837
838         return true;
839 }
840
841 /**
842  * bch_key_resize - adjust size of @k
843  *
844  * bkey_start_offset(k) will be preserved, modifies where the extent ends
845  */
846 void bch2_key_resize(struct bkey *k,
847                     unsigned new_size)
848 {
849         k->p.offset -= k->size;
850         k->p.offset += new_size;
851         k->size = new_size;
852 }
853
854 /*
855  * In extent_sort_fix_overlapping(), insert_fixup_extent(),
856  * extent_merge_inline() - we're modifying keys in place that are packed. To do
857  * that we have to unpack the key, modify the unpacked key - then this
858  * copies/repacks the unpacked to the original as necessary.
859  */
860 static bool __extent_save(struct btree *b, struct btree_node_iter *iter,
861                           struct bkey_packed *dst, struct bkey *src)
862 {
863         struct bkey_format *f = &b->format;
864         struct bkey_i *dst_unpacked;
865         bool ret;
866
867         if ((dst_unpacked = packed_to_bkey(dst))) {
868                 dst_unpacked->k = *src;
869                 ret = true;
870         } else {
871                 ret = bch2_bkey_pack_key(dst, src, f);
872         }
873
874         if (ret && iter)
875                 bch2_verify_key_order(b, iter, dst);
876
877         return ret;
878 }
879
880 static void extent_save(struct btree *b, struct btree_node_iter *iter,
881                         struct bkey_packed *dst, struct bkey *src)
882 {
883         BUG_ON(!__extent_save(b, iter, dst, src));
884 }
885
886 /*
887  * If keys compare equal, compare by pointer order:
888  *
889  * Necessary for sort_fix_overlapping() - if there are multiple keys that
890  * compare equal in different sets, we have to process them newest to oldest.
891  */
892 #define extent_sort_cmp(h, l, r)                                        \
893 ({                                                                      \
894         struct bkey _ul = bkey_unpack_key(b,                            \
895                                 __btree_node_offset_to_key(b, (l).k));  \
896         struct bkey _ur = bkey_unpack_key(b,                            \
897                                 __btree_node_offset_to_key(b, (r).k));  \
898                                                                         \
899         bkey_cmp(bkey_start_pos(&_ul),                                  \
900                  bkey_start_pos(&_ur)) ?: (r).k - (l).k;                \
901 })
902
903 static inline void extent_sort_sift(struct btree_node_iter_large *iter,
904                                     struct btree *b, size_t i)
905 {
906         heap_sift_down(iter, i, extent_sort_cmp);
907 }
908
909 static inline void extent_sort_next(struct btree_node_iter_large *iter,
910                                     struct btree *b,
911                                     struct btree_node_iter_set *i)
912 {
913         sort_key_next(iter, b, i);
914         heap_sift_down(iter, i - iter->data, extent_sort_cmp);
915 }
916
917 static void extent_sort_append(struct bch_fs *c,
918                                struct btree *b,
919                                struct btree_nr_keys *nr,
920                                struct bkey_packed *start,
921                                struct bkey_packed **prev,
922                                struct bkey_packed *k)
923 {
924         struct bkey_format *f = &b->format;
925         BKEY_PADDED(k) tmp;
926
927         if (bkey_whiteout(k))
928                 return;
929
930         bch2_bkey_unpack(b, &tmp.k, k);
931
932         if (*prev &&
933             bch2_extent_merge(c, b, (void *) *prev, &tmp.k))
934                 return;
935
936         if (*prev) {
937                 bch2_bkey_pack(*prev, (void *) *prev, f);
938
939                 btree_keys_account_key_add(nr, 0, *prev);
940                 *prev = bkey_next(*prev);
941         } else {
942                 *prev = start;
943         }
944
945         bkey_copy(*prev, &tmp.k);
946 }
947
948 struct btree_nr_keys bch2_extent_sort_fix_overlapping(struct bch_fs *c,
949                                         struct bset *dst,
950                                         struct btree *b,
951                                         struct btree_node_iter_large *iter)
952 {
953         struct bkey_format *f = &b->format;
954         struct btree_node_iter_set *_l = iter->data, *_r;
955         struct bkey_packed *prev = NULL, *out, *lk, *rk;
956         struct bkey l_unpacked, r_unpacked;
957         struct bkey_s l, r;
958         struct btree_nr_keys nr;
959
960         memset(&nr, 0, sizeof(nr));
961
962         heap_resort(iter, extent_sort_cmp);
963
964         while (!bch2_btree_node_iter_large_end(iter)) {
965                 lk = __btree_node_offset_to_key(b, _l->k);
966
967                 if (iter->used == 1) {
968                         extent_sort_append(c, b, &nr, dst->start, &prev, lk);
969                         extent_sort_next(iter, b, _l);
970                         continue;
971                 }
972
973                 _r = iter->data + 1;
974                 if (iter->used > 2 &&
975                     extent_sort_cmp(iter, _r[0], _r[1]) >= 0)
976                         _r++;
977
978                 rk = __btree_node_offset_to_key(b, _r->k);
979
980                 l = __bkey_disassemble(b, lk, &l_unpacked);
981                 r = __bkey_disassemble(b, rk, &r_unpacked);
982
983                 /* If current key and next key don't overlap, just append */
984                 if (bkey_cmp(l.k->p, bkey_start_pos(r.k)) <= 0) {
985                         extent_sort_append(c, b, &nr, dst->start, &prev, lk);
986                         extent_sort_next(iter, b, _l);
987                         continue;
988                 }
989
990                 /* Skip 0 size keys */
991                 if (!r.k->size) {
992                         extent_sort_next(iter, b, _r);
993                         continue;
994                 }
995
996                 /*
997                  * overlap: keep the newer key and trim the older key so they
998                  * don't overlap. comparing pointers tells us which one is
999                  * newer, since the bsets are appended one after the other.
1000                  */
1001
1002                 /* can't happen because of comparison func */
1003                 BUG_ON(_l->k < _r->k &&
1004                        !bkey_cmp(bkey_start_pos(l.k), bkey_start_pos(r.k)));
1005
1006                 if (_l->k > _r->k) {
1007                         /* l wins, trim r */
1008                         if (bkey_cmp(l.k->p, r.k->p) >= 0) {
1009                                 sort_key_next(iter, b, _r);
1010                         } else {
1011                                 __bch2_cut_front(l.k->p, r);
1012                                 extent_save(b, NULL, rk, r.k);
1013                         }
1014
1015                         extent_sort_sift(iter, b, _r - iter->data);
1016                 } else if (bkey_cmp(l.k->p, r.k->p) > 0) {
1017                         BKEY_PADDED(k) tmp;
1018
1019                         /*
1020                          * r wins, but it overlaps in the middle of l - split l:
1021                          */
1022                         bkey_reassemble(&tmp.k, l.s_c);
1023                         bch2_cut_back(bkey_start_pos(r.k), &tmp.k.k);
1024
1025                         __bch2_cut_front(r.k->p, l);
1026                         extent_save(b, NULL, lk, l.k);
1027
1028                         extent_sort_sift(iter, b, 0);
1029
1030                         extent_sort_append(c, b, &nr, dst->start, &prev,
1031                                            bkey_to_packed(&tmp.k));
1032                 } else {
1033                         bch2_cut_back(bkey_start_pos(r.k), l.k);
1034                         extent_save(b, NULL, lk, l.k);
1035                 }
1036         }
1037
1038         if (prev) {
1039                 bch2_bkey_pack(prev, (void *) prev, f);
1040                 btree_keys_account_key_add(&nr, 0, prev);
1041                 out = bkey_next(prev);
1042         } else {
1043                 out = dst->start;
1044         }
1045
1046         dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
1047         return nr;
1048 }
1049
1050 struct extent_insert_state {
1051         struct btree_insert             *trans;
1052         struct btree_insert_entry       *insert;
1053         struct bpos                     committed;
1054         struct bch_fs_usage             stats;
1055
1056         /* for deleting: */
1057         struct bkey_i                   whiteout;
1058         bool                            do_journal;
1059         bool                            deleting;
1060 };
1061
1062 static void bch2_add_sectors(struct extent_insert_state *s,
1063                              struct bkey_s_c k, u64 offset, s64 sectors)
1064 {
1065         struct bch_fs *c = s->trans->c;
1066         struct btree *b = s->insert->iter->l[0].b;
1067
1068         EBUG_ON(bkey_cmp(bkey_start_pos(k.k), b->data->min_key) < 0);
1069
1070         if (!sectors)
1071                 return;
1072
1073         bch2_mark_key(c, k, sectors, false, gc_pos_btree_node(b),
1074                       &s->stats, s->trans->journal_res.seq, 0);
1075 }
1076
1077 static void bch2_subtract_sectors(struct extent_insert_state *s,
1078                                  struct bkey_s_c k, u64 offset, s64 sectors)
1079 {
1080         bch2_add_sectors(s, k, offset, -sectors);
1081 }
1082
1083 /* These wrappers subtract exactly the sectors that we're removing from @k */
1084 static void bch2_cut_subtract_back(struct extent_insert_state *s,
1085                                   struct bpos where, struct bkey_s k)
1086 {
1087         bch2_subtract_sectors(s, k.s_c, where.offset,
1088                              k.k->p.offset - where.offset);
1089         bch2_cut_back(where, k.k);
1090 }
1091
1092 static void bch2_cut_subtract_front(struct extent_insert_state *s,
1093                                    struct bpos where, struct bkey_s k)
1094 {
1095         bch2_subtract_sectors(s, k.s_c, bkey_start_offset(k.k),
1096                              where.offset - bkey_start_offset(k.k));
1097         __bch2_cut_front(where, k);
1098 }
1099
1100 static void bch2_drop_subtract(struct extent_insert_state *s, struct bkey_s k)
1101 {
1102         if (k.k->size)
1103                 bch2_subtract_sectors(s, k.s_c,
1104                                      bkey_start_offset(k.k), k.k->size);
1105         k.k->size = 0;
1106         __set_bkey_deleted(k.k);
1107 }
1108
1109 static bool bch2_extent_merge_inline(struct bch_fs *,
1110                                      struct btree_iter *,
1111                                      struct bkey_packed *,
1112                                      struct bkey_packed *,
1113                                      bool);
1114
1115 #define MAX_LOCK_HOLD_TIME      (5 * NSEC_PER_MSEC)
1116
1117 static enum btree_insert_ret
1118 extent_insert_should_stop(struct extent_insert_state *s)
1119 {
1120         struct btree *b = s->insert->iter->l[0].b;
1121
1122         /*
1123          * Check if we have sufficient space in both the btree node and the
1124          * journal reservation:
1125          *
1126          * Each insert checks for room in the journal entry, but we check for
1127          * room in the btree node up-front. In the worst case, bkey_cmpxchg()
1128          * will insert two keys, and one iteration of this room will insert one
1129          * key, so we need room for three keys.
1130          */
1131         if (!bch2_btree_node_insert_fits(s->trans->c, b, s->insert->k->k.u64s))
1132                 return BTREE_INSERT_BTREE_NODE_FULL;
1133         else if (!journal_res_insert_fits(s->trans, s->insert))
1134                 return BTREE_INSERT_JOURNAL_RES_FULL; /* XXX worth tracing */
1135         else
1136                 return BTREE_INSERT_OK;
1137 }
1138
1139 static void extent_bset_insert(struct bch_fs *c, struct btree_iter *iter,
1140                                struct bkey_i *insert)
1141 {
1142         struct btree_iter_level *l = &iter->l[0];
1143         struct bset_tree *t = bset_tree_last(l->b);
1144         struct bkey_packed *where =
1145                 bch2_btree_node_iter_bset_pos(&l->iter, l->b, t);
1146         struct bkey_packed *prev = bch2_bkey_prev(l->b, t, where);
1147         struct bkey_packed *next_live_key = where;
1148         unsigned clobber_u64s;
1149
1150         if (prev)
1151                 where = bkey_next(prev);
1152
1153         while (next_live_key != btree_bkey_last(l->b, t) &&
1154                bkey_deleted(next_live_key))
1155                 next_live_key = bkey_next(next_live_key);
1156
1157         /*
1158          * Everything between where and next_live_key is now deleted keys, and
1159          * is overwritten:
1160          */
1161         clobber_u64s = (u64 *) next_live_key - (u64 *) where;
1162
1163         if (prev &&
1164             bch2_extent_merge_inline(c, iter, prev, bkey_to_packed(insert), true))
1165                 goto drop_deleted_keys;
1166
1167         if (next_live_key != btree_bkey_last(l->b, t) &&
1168             bch2_extent_merge_inline(c, iter, bkey_to_packed(insert),
1169                                     next_live_key, false))
1170                 goto drop_deleted_keys;
1171
1172         bch2_bset_insert(l->b, &l->iter, where, insert, clobber_u64s);
1173         bch2_btree_node_iter_fix(iter, l->b, &l->iter, t, where,
1174                                 clobber_u64s, where->u64s);
1175         return;
1176 drop_deleted_keys:
1177         bch2_bset_delete(l->b, where, clobber_u64s);
1178         bch2_btree_node_iter_fix(iter, l->b, &l->iter, t,
1179                                  where, clobber_u64s, 0);
1180 }
1181
1182 static void extent_insert_committed(struct extent_insert_state *s)
1183 {
1184         struct bch_fs *c = s->trans->c;
1185         struct btree_iter *iter = s->insert->iter;
1186         struct bkey_i *insert = !s->deleting
1187                 ? s->insert->k
1188                 : &s->whiteout;
1189         BKEY_PADDED(k) split;
1190
1191         EBUG_ON(bkey_cmp(insert->k.p, s->committed) < 0);
1192         EBUG_ON(bkey_cmp(s->committed, bkey_start_pos(&insert->k)) < 0);
1193
1194         if (!bkey_cmp(s->committed, bkey_start_pos(&insert->k)))
1195                 return;
1196
1197         if (s->deleting && !s->do_journal) {
1198                 bch2_cut_front(s->committed, insert);
1199                 goto done;
1200         }
1201
1202         EBUG_ON(bkey_deleted(&insert->k) || !insert->k.size);
1203
1204         bkey_copy(&split.k, insert);
1205
1206         if (!(s->trans->flags & BTREE_INSERT_JOURNAL_REPLAY) &&
1207             bkey_cmp(s->committed, insert->k.p) &&
1208             bch2_extent_is_compressed(bkey_i_to_s_c(insert))) {
1209                 /* XXX: possibly need to increase our reservation? */
1210                 bch2_cut_subtract_back(s, s->committed,
1211                                       bkey_i_to_s(&split.k));
1212                 bch2_cut_front(s->committed, insert);
1213                 bch2_add_sectors(s, bkey_i_to_s_c(insert),
1214                                 bkey_start_offset(&insert->k),
1215                                 insert->k.size);
1216         } else {
1217                 bch2_cut_back(s->committed, &split.k.k);
1218                 bch2_cut_front(s->committed, insert);
1219         }
1220
1221         if (debug_check_bkeys(c))
1222                 bch2_bkey_debugcheck(c, iter->l[0].b, bkey_i_to_s_c(&split.k));
1223
1224         bch2_btree_journal_key(s->trans, iter, &split.k);
1225
1226         if (!s->deleting)
1227                 extent_bset_insert(c, iter, &split.k);
1228 done:
1229         bch2_btree_iter_set_pos_same_leaf(iter, s->committed);
1230
1231         insert->k.needs_whiteout        = false;
1232         s->do_journal                   = false;
1233         s->trans->did_work              = true;
1234 }
1235
1236 static enum btree_insert_ret
1237 __extent_insert_advance_pos(struct extent_insert_state *s,
1238                             struct bpos next_pos,
1239                             struct bkey_s_c k)
1240 {
1241         struct extent_insert_hook *hook = s->trans->hook;
1242         enum btree_insert_ret ret;
1243
1244         if (hook)
1245                 ret = hook->fn(hook, s->committed, next_pos, k, s->insert->k);
1246         else
1247                 ret = BTREE_INSERT_OK;
1248
1249         EBUG_ON(bkey_deleted(&s->insert->k->k) || !s->insert->k->k.size);
1250
1251         if (ret == BTREE_INSERT_OK)
1252                 s->committed = next_pos;
1253
1254         return ret;
1255 }
1256
1257 /*
1258  * Update iter->pos, marking how much of @insert we've processed, and call hook
1259  * fn:
1260  */
1261 static enum btree_insert_ret
1262 extent_insert_advance_pos(struct extent_insert_state *s, struct bkey_s_c k)
1263 {
1264         struct btree *b = s->insert->iter->l[0].b;
1265         struct bpos next_pos = bpos_min(s->insert->k->k.p,
1266                                         k.k ? k.k->p : b->key.k.p);
1267         enum btree_insert_ret ret;
1268
1269         if (race_fault())
1270                 return BTREE_INSERT_NEED_TRAVERSE;
1271
1272         /* hole? */
1273         if (k.k && bkey_cmp(s->committed, bkey_start_pos(k.k)) < 0) {
1274                 ret = __extent_insert_advance_pos(s, bkey_start_pos(k.k),
1275                                                     bkey_s_c_null);
1276                 if (ret != BTREE_INSERT_OK)
1277                         return ret;
1278         }
1279
1280         /* avoid redundant calls to hook fn: */
1281         if (!bkey_cmp(s->committed, next_pos))
1282                 return BTREE_INSERT_OK;
1283
1284         return __extent_insert_advance_pos(s, next_pos, k);
1285 }
1286
1287 static enum btree_insert_ret
1288 extent_insert_check_split_compressed(struct extent_insert_state *s,
1289                                      struct bkey_s_c k,
1290                                      enum bch_extent_overlap overlap)
1291 {
1292         struct bch_fs *c = s->trans->c;
1293         unsigned sectors;
1294
1295         if (overlap == BCH_EXTENT_OVERLAP_MIDDLE &&
1296             (sectors = bch2_extent_is_compressed(k))) {
1297                 int flags = BCH_DISK_RESERVATION_BTREE_LOCKS_HELD;
1298
1299                 if (s->trans->flags & BTREE_INSERT_NOFAIL)
1300                         flags |= BCH_DISK_RESERVATION_NOFAIL;
1301
1302                 switch (bch2_disk_reservation_add(c,
1303                                 s->trans->disk_res,
1304                                 sectors * bch2_extent_nr_dirty_ptrs(k),
1305                                 flags)) {
1306                 case 0:
1307                         break;
1308                 case -ENOSPC:
1309                         return BTREE_INSERT_ENOSPC;
1310                 case -EINTR:
1311                         return BTREE_INSERT_NEED_GC_LOCK;
1312                 default:
1313                         BUG();
1314                 }
1315         }
1316
1317         return BTREE_INSERT_OK;
1318 }
1319
1320 static enum btree_insert_ret
1321 extent_squash(struct extent_insert_state *s, struct bkey_i *insert,
1322               struct bset_tree *t, struct bkey_packed *_k, struct bkey_s k,
1323               enum bch_extent_overlap overlap)
1324 {
1325         struct bch_fs *c = s->trans->c;
1326         struct btree_iter *iter = s->insert->iter;
1327         struct btree_iter_level *l = &iter->l[0];
1328         struct btree *b = l->b;
1329         struct btree_node_iter *node_iter = &l->iter;
1330         enum btree_insert_ret ret;
1331
1332         switch (overlap) {
1333         case BCH_EXTENT_OVERLAP_FRONT:
1334                 /* insert overlaps with start of k: */
1335                 bch2_cut_subtract_front(s, insert->k.p, k);
1336                 BUG_ON(bkey_deleted(k.k));
1337                 extent_save(b, node_iter, _k, k.k);
1338                 break;
1339
1340         case BCH_EXTENT_OVERLAP_BACK:
1341                 /* insert overlaps with end of k: */
1342                 bch2_cut_subtract_back(s, bkey_start_pos(&insert->k), k);
1343                 BUG_ON(bkey_deleted(k.k));
1344                 extent_save(b, node_iter, _k, k.k);
1345
1346                 /*
1347                  * As the auxiliary tree is indexed by the end of the
1348                  * key and we've just changed the end, update the
1349                  * auxiliary tree.
1350                  */
1351                 bch2_bset_fix_invalidated_key(b, t, _k);
1352                 bch2_btree_node_iter_fix(iter, b, node_iter, t,
1353                                         _k, _k->u64s, _k->u64s);
1354                 break;
1355
1356         case BCH_EXTENT_OVERLAP_ALL: {
1357                 struct bpos orig_pos = k.k->p;
1358
1359                 /* The insert key completely covers k, invalidate k */
1360                 if (!bkey_whiteout(k.k))
1361                         btree_keys_account_key_drop(&b->nr,
1362                                                 t - b->set, _k);
1363
1364                 bch2_drop_subtract(s, k);
1365                 k.k->p = bkey_start_pos(&insert->k);
1366                 if (!__extent_save(b, node_iter, _k, k.k)) {
1367                         /*
1368                          * Couldn't repack: we aren't necessarily able
1369                          * to repack if the new key is outside the range
1370                          * of the old extent, so we have to split
1371                          * @insert:
1372                          */
1373                         k.k->p = orig_pos;
1374                         extent_save(b, node_iter, _k, k.k);
1375
1376                         ret = extent_insert_advance_pos(s, k.s_c);
1377                         if (ret != BTREE_INSERT_OK)
1378                                 return ret;
1379
1380                         extent_insert_committed(s);
1381                         /*
1382                          * We split and inserted upto at k.k->p - that
1383                          * has to coincide with iter->pos, so that we
1384                          * don't have anything more we have to insert
1385                          * until we recheck our journal reservation:
1386                          */
1387                         EBUG_ON(bkey_cmp(s->committed, k.k->p));
1388                 } else {
1389                         bch2_bset_fix_invalidated_key(b, t, _k);
1390                         bch2_btree_node_iter_fix(iter, b, node_iter, t,
1391                                                 _k, _k->u64s, _k->u64s);
1392                 }
1393
1394                 break;
1395         }
1396         case BCH_EXTENT_OVERLAP_MIDDLE: {
1397                 BKEY_PADDED(k) split;
1398                 /*
1399                  * The insert key falls 'in the middle' of k
1400                  * The insert key splits k in 3:
1401                  * - start only in k, preserve
1402                  * - middle common section, invalidate in k
1403                  * - end only in k, preserve
1404                  *
1405                  * We update the old key to preserve the start,
1406                  * insert will be the new common section,
1407                  * we manually insert the end that we are preserving.
1408                  *
1409                  * modify k _before_ doing the insert (which will move
1410                  * what k points to)
1411                  */
1412                 bkey_reassemble(&split.k, k.s_c);
1413                 split.k.k.needs_whiteout |= bset_written(b, bset(b, t));
1414
1415                 bch2_cut_back(bkey_start_pos(&insert->k), &split.k.k);
1416                 BUG_ON(bkey_deleted(&split.k.k));
1417
1418                 bch2_cut_subtract_front(s, insert->k.p, k);
1419                 BUG_ON(bkey_deleted(k.k));
1420                 extent_save(b, node_iter, _k, k.k);
1421
1422                 bch2_add_sectors(s, bkey_i_to_s_c(&split.k),
1423                                 bkey_start_offset(&split.k.k),
1424                                 split.k.k.size);
1425                 extent_bset_insert(c, iter, &split.k);
1426                 break;
1427         }
1428         }
1429
1430         return BTREE_INSERT_OK;
1431 }
1432
1433 static enum btree_insert_ret
1434 __bch2_delete_fixup_extent(struct extent_insert_state *s)
1435 {
1436         struct bch_fs *c = s->trans->c;
1437         struct btree_iter *iter = s->insert->iter;
1438         struct btree_iter_level *l = &iter->l[0];
1439         struct btree *b = l->b;
1440         struct btree_node_iter *node_iter = &l->iter;
1441         struct bkey_packed *_k;
1442         struct bkey unpacked;
1443         struct bkey_i *insert = s->insert->k;
1444         enum btree_insert_ret ret = BTREE_INSERT_OK;
1445
1446         EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k)));
1447
1448         s->whiteout = *insert;
1449
1450         while (bkey_cmp(s->committed, insert->k.p) < 0 &&
1451                (ret = extent_insert_should_stop(s)) == BTREE_INSERT_OK &&
1452                (_k = bch2_btree_node_iter_peek_all(node_iter, b))) {
1453                 struct bset_tree *t = bch2_bkey_to_bset(b, _k);
1454                 struct bkey_s k = __bkey_disassemble(b, _k, &unpacked);
1455                 enum bch_extent_overlap overlap;
1456
1457                 EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k)));
1458                 EBUG_ON(bkey_cmp(iter->pos, k.k->p) >= 0);
1459
1460                 if (bkey_cmp(bkey_start_pos(k.k), insert->k.p) >= 0)
1461                         break;
1462
1463                 if (bkey_whiteout(k.k)) {
1464                         s->committed = bpos_min(insert->k.p, k.k->p);
1465                         goto next;
1466                 }
1467
1468                 overlap = bch2_extent_overlap(&insert->k, k.k);
1469
1470                 ret = extent_insert_check_split_compressed(s, k.s_c, overlap);
1471                 if (ret)
1472                         break;
1473
1474                 ret = extent_insert_advance_pos(s, k.s_c);
1475                 if (ret)
1476                         break;
1477
1478                 s->do_journal = true;
1479
1480                 if (overlap == BCH_EXTENT_OVERLAP_ALL) {
1481                         btree_keys_account_key_drop(&b->nr,
1482                                                 t - b->set, _k);
1483                         bch2_subtract_sectors(s, k.s_c,
1484                                              bkey_start_offset(k.k), k.k->size);
1485                         _k->type = KEY_TYPE_DISCARD;
1486                         reserve_whiteout(b, t, _k);
1487                 } else if (k.k->needs_whiteout ||
1488                            bset_written(b, bset(b, t))) {
1489                         struct bkey_i discard = *insert;
1490
1491                         switch (overlap) {
1492                         case BCH_EXTENT_OVERLAP_FRONT:
1493                                 bch2_cut_front(bkey_start_pos(k.k), &discard);
1494                                 break;
1495                         case BCH_EXTENT_OVERLAP_BACK:
1496                                 bch2_cut_back(k.k->p, &discard.k);
1497                                 break;
1498                         default:
1499                                 break;
1500                         }
1501
1502                         discard.k.needs_whiteout = true;
1503
1504                         ret = extent_squash(s, insert, t, _k, k, overlap);
1505                         BUG_ON(ret != BTREE_INSERT_OK);
1506
1507                         extent_bset_insert(c, iter, &discard);
1508                 } else {
1509                         ret = extent_squash(s, insert, t, _k, k, overlap);
1510                         BUG_ON(ret != BTREE_INSERT_OK);
1511                 }
1512 next:
1513                 bch2_cut_front(s->committed, insert);
1514                 bch2_btree_iter_set_pos_same_leaf(iter, s->committed);
1515         }
1516
1517         return ret;
1518 }
1519
1520 static enum btree_insert_ret
1521 __bch2_insert_fixup_extent(struct extent_insert_state *s)
1522 {
1523         struct btree_iter *iter = s->insert->iter;
1524         struct btree_iter_level *l = &iter->l[0];
1525         struct btree *b = l->b;
1526         struct btree_node_iter *node_iter = &l->iter;
1527         struct bkey_packed *_k;
1528         struct bkey unpacked;
1529         struct bkey_i *insert = s->insert->k;
1530         enum btree_insert_ret ret = BTREE_INSERT_OK;
1531
1532         while (bkey_cmp(s->committed, insert->k.p) < 0 &&
1533                (ret = extent_insert_should_stop(s)) == BTREE_INSERT_OK &&
1534                (_k = bch2_btree_node_iter_peek_all(node_iter, b))) {
1535                 struct bset_tree *t = bch2_bkey_to_bset(b, _k);
1536                 struct bkey_s k = __bkey_disassemble(b, _k, &unpacked);
1537                 enum bch_extent_overlap overlap;
1538
1539                 EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k)));
1540                 EBUG_ON(bkey_cmp(iter->pos, k.k->p) >= 0);
1541
1542                 if (bkey_cmp(bkey_start_pos(k.k), insert->k.p) >= 0)
1543                         break;
1544
1545                 overlap = bch2_extent_overlap(&insert->k, k.k);
1546
1547                 ret = extent_insert_check_split_compressed(s, k.s_c, overlap);
1548                 if (ret)
1549                         break;
1550
1551                 if (!k.k->size)
1552                         goto squash;
1553
1554                 /*
1555                  * Only call advance pos & call hook for nonzero size extents:
1556                  */
1557                 ret = extent_insert_advance_pos(s, k.s_c);
1558                 if (ret)
1559                         break;
1560
1561                 if (k.k->size &&
1562                     (k.k->needs_whiteout || bset_written(b, bset(b, t))))
1563                         insert->k.needs_whiteout = true;
1564
1565                 if (overlap == BCH_EXTENT_OVERLAP_ALL &&
1566                     bkey_whiteout(k.k) &&
1567                     k.k->needs_whiteout) {
1568                         unreserve_whiteout(b, t, _k);
1569                         _k->needs_whiteout = false;
1570                 }
1571 squash:
1572                 ret = extent_squash(s, insert, t, _k, k, overlap);
1573                 if (ret != BTREE_INSERT_OK)
1574                         break;
1575         }
1576
1577         return ret;
1578 }
1579
1580 /**
1581  * bch_extent_insert_fixup - insert a new extent and deal with overlaps
1582  *
1583  * this may result in not actually doing the insert, or inserting some subset
1584  * of the insert key. For cmpxchg operations this is where that logic lives.
1585  *
1586  * All subsets of @insert that need to be inserted are inserted using
1587  * bch2_btree_insert_and_journal(). If @b or @res fills up, this function
1588  * returns false, setting @iter->pos for the prefix of @insert that actually got
1589  * inserted.
1590  *
1591  * BSET INVARIANTS: this function is responsible for maintaining all the
1592  * invariants for bsets of extents in memory. things get really hairy with 0
1593  * size extents
1594  *
1595  * within one bset:
1596  *
1597  * bkey_start_pos(bkey_next(k)) >= k
1598  * or bkey_start_offset(bkey_next(k)) >= k->offset
1599  *
1600  * i.e. strict ordering, no overlapping extents.
1601  *
1602  * multiple bsets (i.e. full btree node):
1603  *
1604  * âˆ€ k, j
1605  *   k.size != 0 âˆ§ j.size != 0 â†’
1606  *     Â¬ (k > bkey_start_pos(j) âˆ§ k < j)
1607  *
1608  * i.e. no two overlapping keys _of nonzero size_
1609  *
1610  * We can't realistically maintain this invariant for zero size keys because of
1611  * the key merging done in bch2_btree_insert_key() - for two mergeable keys k, j
1612  * there may be another 0 size key between them in another bset, and it will
1613  * thus overlap with the merged key.
1614  *
1615  * In addition, the end of iter->pos indicates how much has been processed.
1616  * If the end of iter->pos is not the same as the end of insert, then
1617  * key insertion needs to continue/be retried.
1618  */
1619 enum btree_insert_ret
1620 bch2_insert_fixup_extent(struct btree_insert *trans,
1621                          struct btree_insert_entry *insert)
1622 {
1623         struct bch_fs *c = trans->c;
1624         struct btree_iter *iter = insert->iter;
1625         struct btree_iter_level *l = &iter->l[0];
1626         struct btree *b = l->b;
1627         enum btree_insert_ret ret = BTREE_INSERT_OK;
1628
1629         struct extent_insert_state s = {
1630                 .trans          = trans,
1631                 .insert         = insert,
1632                 .committed      = insert->iter->pos,
1633                 .deleting       = bkey_whiteout(&insert->k->k),
1634         };
1635
1636         EBUG_ON(iter->level);
1637         EBUG_ON(bkey_deleted(&insert->k->k) || !insert->k->k.size);
1638
1639         /*
1640          * As we process overlapping extents, we advance @iter->pos both to
1641          * signal to our caller (btree_insert_key()) how much of @insert->k has
1642          * been inserted, and also to keep @iter->pos consistent with
1643          * @insert->k and the node iterator that we're advancing:
1644          */
1645         EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k->k)));
1646
1647         if (!s.deleting &&
1648             !(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))
1649                 bch2_add_sectors(&s, bkey_i_to_s_c(insert->k),
1650                                 bkey_start_offset(&insert->k->k),
1651                                 insert->k->k.size);
1652
1653         ret = !s.deleting
1654                 ? __bch2_insert_fixup_extent(&s)
1655                 : __bch2_delete_fixup_extent(&s);
1656
1657         if (ret == BTREE_INSERT_OK &&
1658             bkey_cmp(s.committed, insert->k->k.p) < 0)
1659                 ret = extent_insert_advance_pos(&s, bkey_s_c_null);
1660
1661         extent_insert_committed(&s);
1662
1663         if (s.deleting)
1664                 bch2_cut_front(iter->pos, insert->k);
1665
1666         /*
1667          * Subtract any remaining sectors from @insert, if we bailed out early
1668          * and didn't fully insert @insert:
1669          */
1670         if (!s.deleting &&
1671             !(trans->flags & BTREE_INSERT_JOURNAL_REPLAY) &&
1672             insert->k->k.size)
1673                 bch2_subtract_sectors(&s, bkey_i_to_s_c(insert->k),
1674                                      bkey_start_offset(&insert->k->k),
1675                                      insert->k->k.size);
1676
1677         bch2_fs_usage_apply(c, &s.stats, trans->disk_res,
1678                            gc_pos_btree_node(b));
1679
1680         EBUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&insert->k->k)));
1681         EBUG_ON(bkey_cmp(iter->pos, s.committed));
1682         EBUG_ON((bkey_cmp(iter->pos, b->key.k.p) == 0) !=
1683                 !!(iter->flags & BTREE_ITER_AT_END_OF_LEAF));
1684
1685         if (insert->k->k.size && (iter->flags & BTREE_ITER_AT_END_OF_LEAF))
1686                 ret = BTREE_INSERT_NEED_TRAVERSE;
1687
1688         WARN_ONCE((ret == BTREE_INSERT_OK) != (insert->k->k.size == 0),
1689                   "ret %u insert->k.size %u", ret, insert->k->k.size);
1690
1691         return ret;
1692 }
1693
1694 const char *bch2_extent_invalid(const struct bch_fs *c, struct bkey_s_c k)
1695 {
1696         if (bkey_val_u64s(k.k) > BKEY_EXTENT_VAL_U64s_MAX)
1697                 return "value too big";
1698
1699         if (!k.k->size)
1700                 return "zero key size";
1701
1702         switch (k.k->type) {
1703         case BCH_EXTENT:
1704         case BCH_EXTENT_CACHED: {
1705                 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
1706                 const union bch_extent_entry *entry;
1707                 struct bch_extent_crc_unpacked crc;
1708                 const struct bch_extent_ptr *ptr;
1709                 unsigned size_ondisk = e.k->size;
1710                 const char *reason;
1711                 unsigned nonce = UINT_MAX;
1712
1713                 extent_for_each_entry(e, entry) {
1714                         if (__extent_entry_type(entry) >= BCH_EXTENT_ENTRY_MAX)
1715                                 return "invalid extent entry type";
1716
1717                         if (extent_entry_is_crc(entry)) {
1718                                 crc = bch2_extent_crc_unpack(e.k, entry_to_crc(entry));
1719
1720                                 if (crc.offset + e.k->size >
1721                                     crc.uncompressed_size)
1722                                         return "checksum offset + key size > uncompressed size";
1723
1724                                 size_ondisk = crc.compressed_size;
1725
1726                                 if (!bch2_checksum_type_valid(c, crc.csum_type))
1727                                         return "invalid checksum type";
1728
1729                                 if (crc.compression_type >= BCH_COMPRESSION_NR)
1730                                         return "invalid compression type";
1731
1732                                 if (bch2_csum_type_is_encryption(crc.csum_type)) {
1733                                         if (nonce == UINT_MAX)
1734                                                 nonce = crc.offset + crc.nonce;
1735                                         else if (nonce != crc.offset + crc.nonce)
1736                                                 return "incorrect nonce";
1737                                 }
1738                         } else {
1739                                 ptr = entry_to_ptr(entry);
1740
1741                                 reason = extent_ptr_invalid(c, e, &entry->ptr,
1742                                                             size_ondisk, false);
1743                                 if (reason)
1744                                         return reason;
1745                         }
1746                 }
1747
1748                 return NULL;
1749         }
1750
1751         case BCH_RESERVATION: {
1752                 struct bkey_s_c_reservation r = bkey_s_c_to_reservation(k);
1753
1754                 if (bkey_val_bytes(k.k) != sizeof(struct bch_reservation))
1755                         return "incorrect value size";
1756
1757                 if (!r.v->nr_replicas || r.v->nr_replicas > BCH_REPLICAS_MAX)
1758                         return "invalid nr_replicas";
1759
1760                 return NULL;
1761         }
1762
1763         default:
1764                 return "invalid value type";
1765         }
1766 }
1767
1768 static void bch2_extent_debugcheck_extent(struct bch_fs *c, struct btree *b,
1769                                           struct bkey_s_c_extent e)
1770 {
1771         const struct bch_extent_ptr *ptr;
1772         struct bch_dev *ca;
1773         struct bucket_mark mark;
1774         unsigned seq, stale;
1775         char buf[160];
1776         bool bad;
1777         unsigned replicas = 0;
1778
1779         /*
1780          * XXX: we should be doing most/all of these checks at startup time,
1781          * where we check bch2_bkey_invalid() in btree_node_read_done()
1782          *
1783          * But note that we can't check for stale pointers or incorrect gc marks
1784          * until after journal replay is done (it might be an extent that's
1785          * going to get overwritten during replay)
1786          */
1787
1788         extent_for_each_ptr(e, ptr) {
1789                 ca = bch_dev_bkey_exists(c, ptr->dev);
1790                 replicas++;
1791
1792                 /*
1793                  * If journal replay hasn't finished, we might be seeing keys
1794                  * that will be overwritten by the time journal replay is done:
1795                  */
1796                 if (!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))
1797                         continue;
1798
1799                 stale = 0;
1800
1801                 do {
1802                         seq = read_seqcount_begin(&c->gc_pos_lock);
1803                         mark = ptr_bucket_mark(ca, ptr);
1804
1805                         /* between mark and bucket gen */
1806                         smp_rmb();
1807
1808                         stale = ptr_stale(ca, ptr);
1809
1810                         bch2_fs_bug_on(stale && !ptr->cached, c,
1811                                          "stale dirty pointer");
1812
1813                         bch2_fs_bug_on(stale > 96, c,
1814                                          "key too stale: %i",
1815                                          stale);
1816
1817                         if (stale)
1818                                 break;
1819
1820                         bad = gc_pos_cmp(c->gc_pos, gc_pos_btree_node(b)) > 0 &&
1821                                 (mark.data_type != BCH_DATA_USER ||
1822                                  !(ptr->cached
1823                                    ? mark.cached_sectors
1824                                    : mark.dirty_sectors));
1825                 } while (read_seqcount_retry(&c->gc_pos_lock, seq));
1826
1827                 if (bad)
1828                         goto bad_ptr;
1829         }
1830
1831         if (replicas > BCH_REPLICAS_MAX) {
1832                 bch2_bkey_val_to_text(c, btree_node_type(b), buf,
1833                                      sizeof(buf), e.s_c);
1834                 bch2_fs_bug(c,
1835                         "extent key bad (too many replicas: %u): %s",
1836                         replicas, buf);
1837                 return;
1838         }
1839
1840         if (!bkey_extent_is_cached(e.k) &&
1841             !bch2_bkey_replicas_marked(c, BCH_DATA_USER, e.s_c)) {
1842                 bch2_bkey_val_to_text(c, btree_node_type(b),
1843                                      buf, sizeof(buf), e.s_c);
1844                 bch2_fs_bug(c,
1845                         "extent key bad (replicas not marked in superblock):\n%s",
1846                         buf);
1847                 return;
1848         }
1849
1850         return;
1851
1852 bad_ptr:
1853         bch2_bkey_val_to_text(c, btree_node_type(b), buf,
1854                              sizeof(buf), e.s_c);
1855         bch2_fs_bug(c, "extent pointer bad gc mark: %s:\nbucket %zu "
1856                    "gen %i type %u", buf,
1857                    PTR_BUCKET_NR(ca, ptr), mark.gen, mark.data_type);
1858         return;
1859 }
1860
1861 void bch2_extent_debugcheck(struct bch_fs *c, struct btree *b, struct bkey_s_c k)
1862 {
1863         switch (k.k->type) {
1864         case BCH_EXTENT:
1865         case BCH_EXTENT_CACHED:
1866                 bch2_extent_debugcheck_extent(c, b, bkey_s_c_to_extent(k));
1867                 break;
1868         case BCH_RESERVATION:
1869                 break;
1870         default:
1871                 BUG();
1872         }
1873 }
1874
1875 void bch2_extent_to_text(struct bch_fs *c, char *buf,
1876                          size_t size, struct bkey_s_c k)
1877 {
1878         char *out = buf, *end = buf + size;
1879         const char *invalid;
1880
1881 #define p(...)  (out += scnprintf(out, end - out, __VA_ARGS__))
1882
1883         if (bkey_extent_is_data(k.k))
1884                 out += extent_print_ptrs(c, buf, size, bkey_s_c_to_extent(k));
1885
1886         invalid = bch2_extent_invalid(c, k);
1887         if (invalid)
1888                 p(" invalid: %s", invalid);
1889 #undef p
1890 }
1891
1892 static void bch2_extent_crc_init(union bch_extent_crc *crc,
1893                                  struct bch_extent_crc_unpacked new)
1894 {
1895 #define common_fields(_crc)                                             \
1896                 .csum_type              = _crc.csum_type,               \
1897                 .compression_type       = _crc.compression_type,        \
1898                 ._compressed_size       = _crc.compressed_size - 1,     \
1899                 ._uncompressed_size     = _crc.uncompressed_size - 1,   \
1900                 .offset                 = _crc.offset
1901
1902         if (bch_crc_bytes[new.csum_type]        <= 4 &&
1903             new.uncompressed_size               <= CRC32_SIZE_MAX &&
1904             new.nonce                           <= CRC32_NONCE_MAX) {
1905                 crc->crc32 = (struct bch_extent_crc32) {
1906                         .type = 1 << BCH_EXTENT_ENTRY_crc32,
1907                         common_fields(new),
1908                         .csum                   = *((__le32 *) &new.csum.lo),
1909                 };
1910                 return;
1911         }
1912
1913         if (bch_crc_bytes[new.csum_type]        <= 10 &&
1914             new.uncompressed_size               <= CRC64_SIZE_MAX &&
1915             new.nonce                           <= CRC64_NONCE_MAX) {
1916                 crc->crc64 = (struct bch_extent_crc64) {
1917                         .type = 1 << BCH_EXTENT_ENTRY_crc64,
1918                         common_fields(new),
1919                         .nonce                  = new.nonce,
1920                         .csum_lo                = new.csum.lo,
1921                         .csum_hi                = *((__le16 *) &new.csum.hi),
1922                 };
1923                 return;
1924         }
1925
1926         if (bch_crc_bytes[new.csum_type]        <= 16 &&
1927             new.uncompressed_size               <= CRC128_SIZE_MAX &&
1928             new.nonce                           <= CRC128_NONCE_MAX) {
1929                 crc->crc128 = (struct bch_extent_crc128) {
1930                         .type = 1 << BCH_EXTENT_ENTRY_crc128,
1931                         common_fields(new),
1932                         .nonce                  = new.nonce,
1933                         .csum                   = new.csum,
1934                 };
1935                 return;
1936         }
1937 #undef common_fields
1938         BUG();
1939 }
1940
1941 void bch2_extent_crc_append(struct bkey_i_extent *e,
1942                             struct bch_extent_crc_unpacked new)
1943 {
1944         struct bch_extent_crc_unpacked crc;
1945         const union bch_extent_entry *i;
1946
1947         BUG_ON(new.compressed_size > new.uncompressed_size);
1948         BUG_ON(new.live_size != e->k.size);
1949         BUG_ON(!new.compressed_size || !new.uncompressed_size);
1950
1951         /*
1952          * Look up the last crc entry, so we can check if we need to add
1953          * another:
1954          */
1955         extent_for_each_crc(extent_i_to_s(e), crc, i)
1956                 ;
1957
1958         if (!bch2_crc_unpacked_cmp(crc, new))
1959                 return;
1960
1961         bch2_extent_crc_init((void *) extent_entry_last(extent_i_to_s(e)), new);
1962         __extent_entry_push(e);
1963 }
1964
1965 /*
1966  * bch_extent_normalize - clean up an extent, dropping stale pointers etc.
1967  *
1968  * Returns true if @k should be dropped entirely
1969  *
1970  * For existing keys, only called when btree nodes are being rewritten, not when
1971  * they're merely being compacted/resorted in memory.
1972  */
1973 bool bch2_extent_normalize(struct bch_fs *c, struct bkey_s k)
1974 {
1975         struct bkey_s_extent e;
1976
1977         switch (k.k->type) {
1978         case KEY_TYPE_ERROR:
1979                 return false;
1980
1981         case KEY_TYPE_DELETED:
1982         case KEY_TYPE_COOKIE:
1983                 return true;
1984
1985         case KEY_TYPE_DISCARD:
1986                 return bversion_zero(k.k->version);
1987
1988         case BCH_EXTENT:
1989         case BCH_EXTENT_CACHED:
1990                 e = bkey_s_to_extent(k);
1991
1992                 bch2_extent_drop_stale(c, e);
1993
1994                 if (!bkey_val_u64s(e.k)) {
1995                         if (bkey_extent_is_cached(e.k)) {
1996                                 k.k->type = KEY_TYPE_DISCARD;
1997                                 if (bversion_zero(k.k->version))
1998                                         return true;
1999                         } else {
2000                                 k.k->type = KEY_TYPE_ERROR;
2001                         }
2002                 }
2003
2004                 return false;
2005         case BCH_RESERVATION:
2006                 return false;
2007         default:
2008                 BUG();
2009         }
2010 }
2011
2012 void bch2_extent_mark_replicas_cached(struct bch_fs *c,
2013                                       struct bkey_s_extent e,
2014                                       unsigned target,
2015                                       unsigned nr_desired_replicas)
2016 {
2017         struct bch_extent_ptr *ptr;
2018         int extra = bch2_extent_durability(c, e.c) - nr_desired_replicas;
2019
2020         if (target && extra > 0)
2021                 extent_for_each_ptr(e, ptr) {
2022                         int n = bch2_extent_ptr_durability(c, ptr);
2023
2024                         if (n && n <= extra &&
2025                             !bch2_dev_in_target(c, ptr->dev, target)) {
2026                                 ptr->cached = true;
2027                                 extra -= n;
2028                         }
2029                 }
2030
2031         if (extra > 0)
2032                 extent_for_each_ptr(e, ptr) {
2033                         int n = bch2_extent_ptr_durability(c, ptr);
2034
2035                         if (n && n <= extra) {
2036                                 ptr->cached = true;
2037                                 extra -= n;
2038                         }
2039                 }
2040 }
2041
2042 /*
2043  * This picks a non-stale pointer, preferably from a device other than @avoid.
2044  * Avoid can be NULL, meaning pick any. If there are no non-stale pointers to
2045  * other devices, it will still pick a pointer from avoid.
2046  */
2047 int bch2_extent_pick_ptr(struct bch_fs *c, struct bkey_s_c k,
2048                          struct bch_devs_mask *avoid,
2049                          struct extent_pick_ptr *pick)
2050 {
2051         int ret;
2052
2053         switch (k.k->type) {
2054         case KEY_TYPE_DELETED:
2055         case KEY_TYPE_DISCARD:
2056         case KEY_TYPE_COOKIE:
2057                 return 0;
2058
2059         case KEY_TYPE_ERROR:
2060                 return -EIO;
2061
2062         case BCH_EXTENT:
2063         case BCH_EXTENT_CACHED:
2064                 ret = extent_pick_read_device(c, bkey_s_c_to_extent(k),
2065                                               avoid, pick);
2066
2067                 if (!ret && !bkey_extent_is_cached(k.k))
2068                         ret = -EIO;
2069
2070                 return ret;
2071
2072         case BCH_RESERVATION:
2073                 return 0;
2074
2075         default:
2076                 BUG();
2077         }
2078 }
2079
2080 enum merge_result bch2_extent_merge(struct bch_fs *c, struct btree *b,
2081                                     struct bkey_i *l, struct bkey_i *r)
2082 {
2083         struct bkey_s_extent el, er;
2084         union bch_extent_entry *en_l, *en_r;
2085
2086         if (key_merging_disabled(c))
2087                 return BCH_MERGE_NOMERGE;
2088
2089         /*
2090          * Generic header checks
2091          * Assumes left and right are in order
2092          * Left and right must be exactly aligned
2093          */
2094
2095         if (l->k.u64s           != r->k.u64s ||
2096             l->k.type           != r->k.type ||
2097             bversion_cmp(l->k.version, r->k.version) ||
2098             bkey_cmp(l->k.p, bkey_start_pos(&r->k)))
2099                 return BCH_MERGE_NOMERGE;
2100
2101         switch (l->k.type) {
2102         case KEY_TYPE_DELETED:
2103         case KEY_TYPE_DISCARD:
2104         case KEY_TYPE_ERROR:
2105                 /* These types are mergeable, and no val to check */
2106                 break;
2107
2108         case BCH_EXTENT:
2109         case BCH_EXTENT_CACHED:
2110                 el = bkey_i_to_s_extent(l);
2111                 er = bkey_i_to_s_extent(r);
2112
2113                 extent_for_each_entry(el, en_l) {
2114                         struct bch_extent_ptr *lp, *rp;
2115                         struct bch_dev *ca;
2116
2117                         en_r = vstruct_idx(er.v, (u64 *) en_l - el.v->_data);
2118
2119                         if ((extent_entry_type(en_l) !=
2120                              extent_entry_type(en_r)) ||
2121                             extent_entry_is_crc(en_l))
2122                                 return BCH_MERGE_NOMERGE;
2123
2124                         lp = &en_l->ptr;
2125                         rp = &en_r->ptr;
2126
2127                         if (lp->offset + el.k->size     != rp->offset ||
2128                             lp->dev                     != rp->dev ||
2129                             lp->gen                     != rp->gen)
2130                                 return BCH_MERGE_NOMERGE;
2131
2132                         /* We don't allow extents to straddle buckets: */
2133                         ca = bch_dev_bkey_exists(c, lp->dev);
2134
2135                         if (PTR_BUCKET_NR(ca, lp) != PTR_BUCKET_NR(ca, rp))
2136                                 return BCH_MERGE_NOMERGE;
2137                 }
2138
2139                 break;
2140         case BCH_RESERVATION: {
2141                 struct bkey_i_reservation *li = bkey_i_to_reservation(l);
2142                 struct bkey_i_reservation *ri = bkey_i_to_reservation(r);
2143
2144                 if (li->v.generation != ri->v.generation ||
2145                     li->v.nr_replicas != ri->v.nr_replicas)
2146                         return BCH_MERGE_NOMERGE;
2147                 break;
2148         }
2149         default:
2150                 return BCH_MERGE_NOMERGE;
2151         }
2152
2153         l->k.needs_whiteout |= r->k.needs_whiteout;
2154
2155         /* Keys with no pointers aren't restricted to one bucket and could
2156          * overflow KEY_SIZE
2157          */
2158         if ((u64) l->k.size + r->k.size > KEY_SIZE_MAX) {
2159                 bch2_key_resize(&l->k, KEY_SIZE_MAX);
2160                 bch2_cut_front(l->k.p, r);
2161                 return BCH_MERGE_PARTIAL;
2162         }
2163
2164         bch2_key_resize(&l->k, l->k.size + r->k.size);
2165
2166         return BCH_MERGE_MERGE;
2167 }
2168
2169 static void extent_i_save(struct btree *b, struct bkey_packed *dst,
2170                           struct bkey_i *src)
2171 {
2172         struct bkey_format *f = &b->format;
2173         struct bkey_i *dst_unpacked;
2174
2175         BUG_ON(bkeyp_val_u64s(f, dst) != bkey_val_u64s(&src->k));
2176
2177         /*
2178          * We don't want the bch2_verify_key_order() call in extent_save(),
2179          * because we may be out of order with deleted keys that are about to be
2180          * removed by extent_bset_insert()
2181          */
2182
2183         if ((dst_unpacked = packed_to_bkey(dst)))
2184                 bkey_copy(dst_unpacked, src);
2185         else
2186                 BUG_ON(!bch2_bkey_pack(dst, src, f));
2187 }
2188
2189 static bool extent_merge_one_overlapping(struct btree_iter *iter,
2190                                          struct bpos new_pos,
2191                                          struct bset_tree *t,
2192                                          struct bkey_packed *k, struct bkey uk,
2193                                          bool check, bool could_pack)
2194 {
2195         struct btree_iter_level *l = &iter->l[0];
2196
2197         BUG_ON(!bkey_deleted(k));
2198
2199         if (check) {
2200                 return !bkey_packed(k) || could_pack;
2201         } else {
2202                 uk.p = new_pos;
2203                 extent_save(l->b, &l->iter, k, &uk);
2204                 bch2_bset_fix_invalidated_key(l->b, t, k);
2205                 bch2_btree_node_iter_fix(iter, l->b, &l->iter, t,
2206                                          k, k->u64s, k->u64s);
2207                 return true;
2208         }
2209 }
2210
2211 static bool extent_merge_do_overlapping(struct btree_iter *iter,
2212                                         struct bkey *m, bool back_merge)
2213 {
2214         struct btree_iter_level *l = &iter->l[0];
2215         struct btree *b = l->b;
2216         struct btree_node_iter *node_iter = &l->iter;
2217         struct bset_tree *t;
2218         struct bkey_packed *k;
2219         struct bkey uk;
2220         struct bpos new_pos = back_merge ? m->p : bkey_start_pos(m);
2221         bool could_pack = bkey_pack_pos((void *) &uk, new_pos, b);
2222         bool check = true;
2223
2224         /*
2225          * @m is the new merged extent:
2226          *
2227          * The merge took place in the last bset; we know there can't be any 0
2228          * size extents overlapping with m there because if so they would have
2229          * been between the two extents we merged.
2230          *
2231          * But in the other bsets, we have to check for and fix such extents:
2232          */
2233 do_fixup:
2234         for_each_bset(b, t) {
2235                 if (t == bset_tree_last(b))
2236                         break;
2237
2238                 /*
2239                  * if we don't find this bset in the iterator we already got to
2240                  * the end of that bset, so start searching from the end.
2241                  */
2242                 k = bch2_btree_node_iter_bset_pos(node_iter, b, t);
2243
2244                 if (k == btree_bkey_last(b, t))
2245                         k = bch2_bkey_prev_all(b, t, k);
2246                 if (!k)
2247                         continue;
2248
2249                 if (back_merge) {
2250                         /*
2251                          * Back merge: 0 size extents will be before the key
2252                          * that was just inserted (and thus the iterator
2253                          * position) - walk backwards to find them
2254                          */
2255                         for (;
2256                              k &&
2257                              (uk = bkey_unpack_key(b, k),
2258                               bkey_cmp(uk.p, bkey_start_pos(m)) > 0);
2259                              k = bch2_bkey_prev_all(b, t, k)) {
2260                                 if (bkey_cmp(uk.p, m->p) >= 0)
2261                                         continue;
2262
2263                                 if (!extent_merge_one_overlapping(iter, new_pos,
2264                                                 t, k, uk, check, could_pack))
2265                                         return false;
2266                         }
2267                 } else {
2268                         /* Front merge - walk forwards */
2269                         for (;
2270                              k != btree_bkey_last(b, t) &&
2271                              (uk = bkey_unpack_key(b, k),
2272                               bkey_cmp(uk.p, m->p) < 0);
2273                              k = bkey_next(k)) {
2274                                 if (bkey_cmp(uk.p,
2275                                              bkey_start_pos(m)) <= 0)
2276                                         continue;
2277
2278                                 if (!extent_merge_one_overlapping(iter, new_pos,
2279                                                 t, k, uk, check, could_pack))
2280                                         return false;
2281                         }
2282                 }
2283         }
2284
2285         if (check) {
2286                 check = false;
2287                 goto do_fixup;
2288         }
2289
2290         return true;
2291 }
2292
2293 /*
2294  * When merging an extent that we're inserting into a btree node, the new merged
2295  * extent could overlap with an existing 0 size extent - if we don't fix that,
2296  * it'll break the btree node iterator so this code finds those 0 size extents
2297  * and shifts them out of the way.
2298  *
2299  * Also unpacks and repacks.
2300  */
2301 static bool bch2_extent_merge_inline(struct bch_fs *c,
2302                                      struct btree_iter *iter,
2303                                      struct bkey_packed *l,
2304                                      struct bkey_packed *r,
2305                                      bool back_merge)
2306 {
2307         struct btree *b = iter->l[0].b;
2308         struct btree_node_iter *node_iter = &iter->l[0].iter;
2309         const struct bkey_format *f = &b->format;
2310         struct bset_tree *t = bset_tree_last(b);
2311         struct bkey_packed *m;
2312         BKEY_PADDED(k) li;
2313         BKEY_PADDED(k) ri;
2314         struct bkey_i *mi;
2315         struct bkey tmp;
2316
2317         /*
2318          * We need to save copies of both l and r, because we might get a
2319          * partial merge (which modifies both) and then fails to repack
2320          */
2321         bch2_bkey_unpack(b, &li.k, l);
2322         bch2_bkey_unpack(b, &ri.k, r);
2323
2324         m = back_merge ? l : r;
2325         mi = back_merge ? &li.k : &ri.k;
2326
2327         /* l & r should be in last bset: */
2328         EBUG_ON(bch2_bkey_to_bset(b, m) != t);
2329
2330         switch (bch2_extent_merge(c, b, &li.k, &ri.k)) {
2331         case BCH_MERGE_NOMERGE:
2332                 return false;
2333         case BCH_MERGE_PARTIAL:
2334                 if (bkey_packed(m) && !bch2_bkey_pack_key((void *) &tmp, &mi->k, f))
2335                         return false;
2336
2337                 if (!extent_merge_do_overlapping(iter, &li.k.k, back_merge))
2338                         return false;
2339
2340                 extent_i_save(b, m, mi);
2341                 bch2_bset_fix_invalidated_key(b, t, m);
2342
2343                 /*
2344                  * Update iterator to reflect what we just inserted - otherwise,
2345                  * the iter_fix() call is going to put us _before_ the key we
2346                  * just partially merged with:
2347                  */
2348                 if (back_merge)
2349                         bch2_btree_iter_set_pos_same_leaf(iter, li.k.k.p);
2350
2351                 bch2_btree_node_iter_fix(iter, b, node_iter,
2352                                          t, m, m->u64s, m->u64s);
2353
2354                 if (!back_merge)
2355                         bkey_copy(packed_to_bkey(l), &li.k);
2356                 else
2357                         bkey_copy(packed_to_bkey(r), &ri.k);
2358                 return false;
2359         case BCH_MERGE_MERGE:
2360                 if (bkey_packed(m) && !bch2_bkey_pack_key((void *) &tmp, &li.k.k, f))
2361                         return false;
2362
2363                 if (!extent_merge_do_overlapping(iter, &li.k.k, back_merge))
2364                         return false;
2365
2366                 extent_i_save(b, m, &li.k);
2367                 bch2_bset_fix_invalidated_key(b, t, m);
2368
2369                 bch2_btree_node_iter_fix(iter, b, node_iter,
2370                                          t, m, m->u64s, m->u64s);
2371                 return true;
2372         default:
2373                 BUG();
2374         }
2375 }
2376
2377 int bch2_check_range_allocated(struct bch_fs *c, struct bpos pos, u64 size)
2378 {
2379         struct btree_iter iter;
2380         struct bpos end = pos;
2381         struct bkey_s_c k;
2382         int ret = 0;
2383
2384         end.offset += size;
2385
2386         for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, pos,
2387                              BTREE_ITER_SLOTS, k) {
2388                 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
2389                         break;
2390
2391                 if (!bch2_extent_is_fully_allocated(k)) {
2392                         ret = -ENOSPC;
2393                         break;
2394                 }
2395         }
2396         bch2_btree_iter_unlock(&iter);
2397
2398         return ret;
2399 }