]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/ec.c
Update bcachefs sources to 6a20aede29 bcachefs: Fix quotas + snapshots
[bcachefs-tools-debian] / libbcachefs / ec.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /* erasure coding */
4
5 #include "bcachefs.h"
6 #include "alloc_foreground.h"
7 #include "backpointers.h"
8 #include "bkey_buf.h"
9 #include "bset.h"
10 #include "btree_gc.h"
11 #include "btree_update.h"
12 #include "btree_write_buffer.h"
13 #include "buckets.h"
14 #include "disk_groups.h"
15 #include "ec.h"
16 #include "error.h"
17 #include "io.h"
18 #include "keylist.h"
19 #include "recovery.h"
20 #include "replicas.h"
21 #include "super-io.h"
22 #include "util.h"
23
24 #include <linux/sort.h>
25
26 #ifdef __KERNEL__
27
28 #include <linux/raid/pq.h>
29 #include <linux/raid/xor.h>
30
31 static void raid5_recov(unsigned disks, unsigned failed_idx,
32                         size_t size, void **data)
33 {
34         unsigned i = 2, nr;
35
36         BUG_ON(failed_idx >= disks);
37
38         swap(data[0], data[failed_idx]);
39         memcpy(data[0], data[1], size);
40
41         while (i < disks) {
42                 nr = min_t(unsigned, disks - i, MAX_XOR_BLOCKS);
43                 xor_blocks(nr, size, data[0], data + i);
44                 i += nr;
45         }
46
47         swap(data[0], data[failed_idx]);
48 }
49
50 static void raid_gen(int nd, int np, size_t size, void **v)
51 {
52         if (np >= 1)
53                 raid5_recov(nd + np, nd, size, v);
54         if (np >= 2)
55                 raid6_call.gen_syndrome(nd + np, size, v);
56         BUG_ON(np > 2);
57 }
58
59 static void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v)
60 {
61         switch (nr) {
62         case 0:
63                 break;
64         case 1:
65                 if (ir[0] < nd + 1)
66                         raid5_recov(nd + 1, ir[0], size, v);
67                 else
68                         raid6_call.gen_syndrome(nd + np, size, v);
69                 break;
70         case 2:
71                 if (ir[1] < nd) {
72                         /* data+data failure. */
73                         raid6_2data_recov(nd + np, size, ir[0], ir[1], v);
74                 } else if (ir[0] < nd) {
75                         /* data + p/q failure */
76
77                         if (ir[1] == nd) /* data + p failure */
78                                 raid6_datap_recov(nd + np, size, ir[0], v);
79                         else { /* data + q failure */
80                                 raid5_recov(nd + 1, ir[0], size, v);
81                                 raid6_call.gen_syndrome(nd + np, size, v);
82                         }
83                 } else {
84                         raid_gen(nd, np, size, v);
85                 }
86                 break;
87         default:
88                 BUG();
89         }
90 }
91
92 #else
93
94 #include <raid/raid.h>
95
96 #endif
97
98 struct ec_bio {
99         struct bch_dev          *ca;
100         struct ec_stripe_buf    *buf;
101         size_t                  idx;
102         struct bio              bio;
103 };
104
105 /* Stripes btree keys: */
106
107 int bch2_stripe_invalid(const struct bch_fs *c, struct bkey_s_c k,
108                         unsigned flags, struct printbuf *err)
109 {
110         const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
111
112         if (bkey_eq(k.k->p, POS_MIN)) {
113                 prt_printf(err, "stripe at POS_MIN");
114                 return -BCH_ERR_invalid_bkey;
115         }
116
117         if (k.k->p.inode) {
118                 prt_printf(err, "nonzero inode field");
119                 return -BCH_ERR_invalid_bkey;
120         }
121
122         if (bkey_val_u64s(k.k) < stripe_val_u64s(s)) {
123                 prt_printf(err, "incorrect value size (%zu < %u)",
124                        bkey_val_u64s(k.k), stripe_val_u64s(s));
125                 return -BCH_ERR_invalid_bkey;
126         }
127
128         return bch2_bkey_ptrs_invalid(c, k, flags, err);
129 }
130
131 void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
132                          struct bkey_s_c k)
133 {
134         const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
135         unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
136
137         prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
138                s->algorithm,
139                le16_to_cpu(s->sectors),
140                nr_data,
141                s->nr_redundant,
142                s->csum_type,
143                1U << s->csum_granularity_bits);
144
145         for (i = 0; i < s->nr_blocks; i++) {
146                 const struct bch_extent_ptr *ptr = s->ptrs + i;
147                 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
148                 u32 offset;
149                 u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);
150
151                 prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset);
152                 if (i < nr_data)
153                         prt_printf(out, "#%u", stripe_blockcount_get(s, i));
154                 if (ptr_stale(ca, ptr))
155                         prt_printf(out, " stale");
156         }
157 }
158
159 /* returns blocknr in stripe that we matched: */
160 static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
161                                                 struct bkey_s_c k, unsigned *block)
162 {
163         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
164         const struct bch_extent_ptr *ptr;
165         unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
166
167         bkey_for_each_ptr(ptrs, ptr)
168                 for (i = 0; i < nr_data; i++)
169                         if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
170                                                       le16_to_cpu(s->sectors))) {
171                                 *block = i;
172                                 return ptr;
173                         }
174
175         return NULL;
176 }
177
178 static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
179 {
180         switch (k.k->type) {
181         case KEY_TYPE_extent: {
182                 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
183                 const union bch_extent_entry *entry;
184
185                 extent_for_each_entry(e, entry)
186                         if (extent_entry_type(entry) ==
187                             BCH_EXTENT_ENTRY_stripe_ptr &&
188                             entry->stripe_ptr.idx == idx)
189                                 return true;
190
191                 break;
192         }
193         }
194
195         return false;
196 }
197
198 /* Stripe bufs: */
199
200 static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
201 {
202         unsigned i;
203
204         for (i = 0; i < buf->key.v.nr_blocks; i++) {
205                 kvpfree(buf->data[i], buf->size << 9);
206                 buf->data[i] = NULL;
207         }
208 }
209
210 /* XXX: this is a non-mempoolified memory allocation: */
211 static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
212                               unsigned offset, unsigned size)
213 {
214         struct bch_stripe *v = &buf->key.v;
215         unsigned csum_granularity = 1U << v->csum_granularity_bits;
216         unsigned end = offset + size;
217         unsigned i;
218
219         BUG_ON(end > le16_to_cpu(v->sectors));
220
221         offset  = round_down(offset, csum_granularity);
222         end     = min_t(unsigned, le16_to_cpu(v->sectors),
223                         round_up(end, csum_granularity));
224
225         buf->offset     = offset;
226         buf->size       = end - offset;
227
228         memset(buf->valid, 0xFF, sizeof(buf->valid));
229
230         for (i = 0; i < buf->key.v.nr_blocks; i++) {
231                 buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
232                 if (!buf->data[i])
233                         goto err;
234         }
235
236         return 0;
237 err:
238         ec_stripe_buf_exit(buf);
239         return -BCH_ERR_ENOMEM_stripe_buf;
240 }
241
242 /* Checksumming: */
243
244 static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
245                                          unsigned block, unsigned offset)
246 {
247         struct bch_stripe *v = &buf->key.v;
248         unsigned csum_granularity = 1 << v->csum_granularity_bits;
249         unsigned end = buf->offset + buf->size;
250         unsigned len = min(csum_granularity, end - offset);
251
252         BUG_ON(offset >= end);
253         BUG_ON(offset <  buf->offset);
254         BUG_ON(offset & (csum_granularity - 1));
255         BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
256                (len & (csum_granularity - 1)));
257
258         return bch2_checksum(NULL, v->csum_type,
259                              null_nonce(),
260                              buf->data[block] + ((offset - buf->offset) << 9),
261                              len << 9);
262 }
263
264 static void ec_generate_checksums(struct ec_stripe_buf *buf)
265 {
266         struct bch_stripe *v = &buf->key.v;
267         unsigned i, j, csums_per_device = stripe_csums_per_device(v);
268
269         if (!v->csum_type)
270                 return;
271
272         BUG_ON(buf->offset);
273         BUG_ON(buf->size != le16_to_cpu(v->sectors));
274
275         for (i = 0; i < v->nr_blocks; i++)
276                 for (j = 0; j < csums_per_device; j++)
277                         stripe_csum_set(v, i, j,
278                                 ec_block_checksum(buf, i, j << v->csum_granularity_bits));
279 }
280
281 static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
282 {
283         struct bch_stripe *v = &buf->key.v;
284         unsigned csum_granularity = 1 << v->csum_granularity_bits;
285         unsigned i;
286
287         if (!v->csum_type)
288                 return;
289
290         for (i = 0; i < v->nr_blocks; i++) {
291                 unsigned offset = buf->offset;
292                 unsigned end = buf->offset + buf->size;
293
294                 if (!test_bit(i, buf->valid))
295                         continue;
296
297                 while (offset < end) {
298                         unsigned j = offset >> v->csum_granularity_bits;
299                         unsigned len = min(csum_granularity, end - offset);
300                         struct bch_csum want = stripe_csum_get(v, i, j);
301                         struct bch_csum got = ec_block_checksum(buf, i, offset);
302
303                         if (bch2_crc_cmp(want, got)) {
304                                 struct printbuf buf2 = PRINTBUF;
305
306                                 bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(&buf->key.k_i));
307
308                                 bch_err_ratelimited(c,
309                                         "stripe checksum error for %ps at %u:%u: csum type %u, expected %llx got %llx\n%s",
310                                         (void *) _RET_IP_, i, j, v->csum_type,
311                                         want.lo, got.lo, buf2.buf);
312                                 printbuf_exit(&buf2);
313                                 clear_bit(i, buf->valid);
314                                 break;
315                         }
316
317                         offset += len;
318                 }
319         }
320 }
321
322 /* Erasure coding: */
323
324 static void ec_generate_ec(struct ec_stripe_buf *buf)
325 {
326         struct bch_stripe *v = &buf->key.v;
327         unsigned nr_data = v->nr_blocks - v->nr_redundant;
328         unsigned bytes = le16_to_cpu(v->sectors) << 9;
329
330         raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
331 }
332
333 static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
334 {
335         return buf->key.v.nr_blocks -
336                 bitmap_weight(buf->valid, buf->key.v.nr_blocks);
337 }
338
339 static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
340 {
341         struct bch_stripe *v = &buf->key.v;
342         unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
343         unsigned nr_data = v->nr_blocks - v->nr_redundant;
344         unsigned bytes = buf->size << 9;
345
346         if (ec_nr_failed(buf) > v->nr_redundant) {
347                 bch_err_ratelimited(c,
348                         "error doing reconstruct read: unable to read enough blocks");
349                 return -1;
350         }
351
352         for (i = 0; i < nr_data; i++)
353                 if (!test_bit(i, buf->valid))
354                         failed[nr_failed++] = i;
355
356         raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
357         return 0;
358 }
359
360 /* IO: */
361
362 static void ec_block_endio(struct bio *bio)
363 {
364         struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
365         struct bch_stripe *v = &ec_bio->buf->key.v;
366         struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
367         struct bch_dev *ca = ec_bio->ca;
368         struct closure *cl = bio->bi_private;
369
370         if (bch2_dev_io_err_on(bio->bi_status, ca, "erasure coding %s error: %s",
371                                bio_data_dir(bio) ? "write" : "read",
372                                bch2_blk_status_to_str(bio->bi_status)))
373                 clear_bit(ec_bio->idx, ec_bio->buf->valid);
374
375         if (ptr_stale(ca, ptr)) {
376                 bch_err_ratelimited(ca->fs,
377                                     "error %s stripe: stale pointer after io",
378                                     bio_data_dir(bio) == READ ? "reading from" : "writing to");
379                 clear_bit(ec_bio->idx, ec_bio->buf->valid);
380         }
381
382         bio_put(&ec_bio->bio);
383         percpu_ref_put(&ca->io_ref);
384         closure_put(cl);
385 }
386
387 static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
388                         unsigned rw, unsigned idx, struct closure *cl)
389 {
390         struct bch_stripe *v = &buf->key.v;
391         unsigned offset = 0, bytes = buf->size << 9;
392         struct bch_extent_ptr *ptr = &v->ptrs[idx];
393         struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
394         enum bch_data_type data_type = idx < buf->key.v.nr_blocks - buf->key.v.nr_redundant
395                 ? BCH_DATA_user
396                 : BCH_DATA_parity;
397
398         if (ptr_stale(ca, ptr)) {
399                 bch_err_ratelimited(c,
400                                     "error %s stripe: stale pointer",
401                                     rw == READ ? "reading from" : "writing to");
402                 clear_bit(idx, buf->valid);
403                 return;
404         }
405
406         if (!bch2_dev_get_ioref(ca, rw)) {
407                 clear_bit(idx, buf->valid);
408                 return;
409         }
410
411         this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);
412
413         while (offset < bytes) {
414                 unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
415                                            DIV_ROUND_UP(bytes, PAGE_SIZE));
416                 unsigned b = min_t(size_t, bytes - offset,
417                                    nr_iovecs << PAGE_SHIFT);
418                 struct ec_bio *ec_bio;
419
420                 ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
421                                                        nr_iovecs,
422                                                        rw,
423                                                        GFP_KERNEL,
424                                                        &c->ec_bioset),
425                                       struct ec_bio, bio);
426
427                 ec_bio->ca                      = ca;
428                 ec_bio->buf                     = buf;
429                 ec_bio->idx                     = idx;
430
431                 ec_bio->bio.bi_iter.bi_sector   = ptr->offset + buf->offset + (offset >> 9);
432                 ec_bio->bio.bi_end_io           = ec_block_endio;
433                 ec_bio->bio.bi_private          = cl;
434
435                 bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
436
437                 closure_get(cl);
438                 percpu_ref_get(&ca->io_ref);
439
440                 submit_bio(&ec_bio->bio);
441
442                 offset += b;
443         }
444
445         percpu_ref_put(&ca->io_ref);
446 }
447
448 static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
449                                 struct ec_stripe_buf *stripe)
450 {
451         struct btree_iter iter;
452         struct bkey_s_c k;
453         int ret;
454
455         k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
456                                POS(0, idx), BTREE_ITER_SLOTS);
457         ret = bkey_err(k);
458         if (ret)
459                 goto err;
460         if (k.k->type != KEY_TYPE_stripe) {
461                 ret = -ENOENT;
462                 goto err;
463         }
464         bkey_reassemble(&stripe->key.k_i, k);
465 err:
466         bch2_trans_iter_exit(trans, &iter);
467         return ret;
468 }
469
470 static int get_stripe_key(struct bch_fs *c, u64 idx, struct ec_stripe_buf *stripe)
471 {
472         return bch2_trans_run(c, get_stripe_key_trans(&trans, idx, stripe));
473 }
474
475 /* recovery read path: */
476 int bch2_ec_read_extent(struct bch_fs *c, struct bch_read_bio *rbio)
477 {
478         struct ec_stripe_buf *buf;
479         struct closure cl;
480         struct bch_stripe *v;
481         unsigned i, offset;
482         int ret = 0;
483
484         closure_init_stack(&cl);
485
486         BUG_ON(!rbio->pick.has_ec);
487
488         buf = kzalloc(sizeof(*buf), GFP_NOIO);
489         if (!buf)
490                 return -BCH_ERR_ENOMEM_ec_read_extent;
491
492         ret = get_stripe_key(c, rbio->pick.ec.idx, buf);
493         if (ret) {
494                 bch_err_ratelimited(c,
495                         "error doing reconstruct read: error %i looking up stripe", ret);
496                 kfree(buf);
497                 return -EIO;
498         }
499
500         v = &buf->key.v;
501
502         if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
503                 bch_err_ratelimited(c,
504                         "error doing reconstruct read: pointer doesn't match stripe");
505                 ret = -EIO;
506                 goto err;
507         }
508
509         offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
510         if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
511                 bch_err_ratelimited(c,
512                         "error doing reconstruct read: read is bigger than stripe");
513                 ret = -EIO;
514                 goto err;
515         }
516
517         ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
518         if (ret)
519                 goto err;
520
521         for (i = 0; i < v->nr_blocks; i++)
522                 ec_block_io(c, buf, REQ_OP_READ, i, &cl);
523
524         closure_sync(&cl);
525
526         if (ec_nr_failed(buf) > v->nr_redundant) {
527                 bch_err_ratelimited(c,
528                         "error doing reconstruct read: unable to read enough blocks");
529                 ret = -EIO;
530                 goto err;
531         }
532
533         ec_validate_checksums(c, buf);
534
535         ret = ec_do_recov(c, buf);
536         if (ret)
537                 goto err;
538
539         memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
540                       buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
541 err:
542         ec_stripe_buf_exit(buf);
543         kfree(buf);
544         return ret;
545 }
546
547 /* stripe bucket accounting: */
548
549 static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
550 {
551         ec_stripes_heap n, *h = &c->ec_stripes_heap;
552
553         if (idx >= h->size) {
554                 if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
555                         return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
556
557                 mutex_lock(&c->ec_stripes_heap_lock);
558                 if (n.size > h->size) {
559                         memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
560                         n.used = h->used;
561                         swap(*h, n);
562                 }
563                 mutex_unlock(&c->ec_stripes_heap_lock);
564
565                 free_heap(&n);
566         }
567
568         if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
569                 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
570
571         if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
572             !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
573                 return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
574
575         return 0;
576 }
577
578 static int ec_stripe_mem_alloc(struct btree_trans *trans,
579                                struct btree_iter *iter)
580 {
581         size_t idx = iter->pos.offset;
582
583         if (!__ec_stripe_mem_alloc(trans->c, idx, GFP_NOWAIT|__GFP_NOWARN))
584                 return 0;
585
586         bch2_trans_unlock(trans);
587
588         return   __ec_stripe_mem_alloc(trans->c, idx, GFP_KERNEL) ?:
589                 bch2_trans_relock(trans);
590 }
591
592 /*
593  * Hash table of open stripes:
594  * Stripes that are being created or modified are kept in a hash table, so that
595  * stripe deletion can skip them.
596  */
597
598 static bool __bch2_stripe_is_open(struct bch_fs *c, u64 idx)
599 {
600         unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
601         struct ec_stripe_new *s;
602
603         hlist_for_each_entry(s, &c->ec_stripes_new[hash], hash)
604                 if (s->idx == idx)
605                         return true;
606         return false;
607 }
608
609 static bool bch2_stripe_is_open(struct bch_fs *c, u64 idx)
610 {
611         bool ret = false;
612
613         spin_lock(&c->ec_stripes_new_lock);
614         ret = __bch2_stripe_is_open(c, idx);
615         spin_unlock(&c->ec_stripes_new_lock);
616
617         return ret;
618 }
619
620 static bool bch2_try_open_stripe(struct bch_fs *c,
621                                  struct ec_stripe_new *s,
622                                  u64 idx)
623 {
624         bool ret;
625
626         spin_lock(&c->ec_stripes_new_lock);
627         ret = !__bch2_stripe_is_open(c, idx);
628         if (ret) {
629                 unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
630
631                 s->idx = idx;
632                 hlist_add_head(&s->hash, &c->ec_stripes_new[hash]);
633         }
634         spin_unlock(&c->ec_stripes_new_lock);
635
636         return ret;
637 }
638
639 static void bch2_stripe_close(struct bch_fs *c, struct ec_stripe_new *s)
640 {
641         BUG_ON(!s->idx);
642
643         spin_lock(&c->ec_stripes_new_lock);
644         hlist_del_init(&s->hash);
645         spin_unlock(&c->ec_stripes_new_lock);
646
647         s->idx = 0;
648 }
649
650 /* Heap of all existing stripes, ordered by blocks_nonempty */
651
652 static u64 stripe_idx_to_delete(struct bch_fs *c)
653 {
654         ec_stripes_heap *h = &c->ec_stripes_heap;
655
656         lockdep_assert_held(&c->ec_stripes_heap_lock);
657
658         if (h->used &&
659             h->data[0].blocks_nonempty == 0 &&
660             !bch2_stripe_is_open(c, h->data[0].idx))
661                 return h->data[0].idx;
662
663         return 0;
664 }
665
666 static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
667                                       struct ec_stripe_heap_entry l,
668                                       struct ec_stripe_heap_entry r)
669 {
670         return ((l.blocks_nonempty > r.blocks_nonempty) -
671                 (l.blocks_nonempty < r.blocks_nonempty));
672 }
673
674 static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
675                                                    size_t i)
676 {
677         struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);
678
679         genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
680 }
681
682 static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
683 {
684         ec_stripes_heap *h = &c->ec_stripes_heap;
685         struct stripe *m = genradix_ptr(&c->stripes, idx);
686
687         BUG_ON(m->heap_idx >= h->used);
688         BUG_ON(h->data[m->heap_idx].idx != idx);
689 }
690
691 void bch2_stripes_heap_del(struct bch_fs *c,
692                            struct stripe *m, size_t idx)
693 {
694         mutex_lock(&c->ec_stripes_heap_lock);
695         heap_verify_backpointer(c, idx);
696
697         heap_del(&c->ec_stripes_heap, m->heap_idx,
698                  ec_stripes_heap_cmp,
699                  ec_stripes_heap_set_backpointer);
700         mutex_unlock(&c->ec_stripes_heap_lock);
701 }
702
703 void bch2_stripes_heap_insert(struct bch_fs *c,
704                               struct stripe *m, size_t idx)
705 {
706         mutex_lock(&c->ec_stripes_heap_lock);
707         BUG_ON(heap_full(&c->ec_stripes_heap));
708
709         heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
710                         .idx = idx,
711                         .blocks_nonempty = m->blocks_nonempty,
712                 }),
713                  ec_stripes_heap_cmp,
714                  ec_stripes_heap_set_backpointer);
715
716         heap_verify_backpointer(c, idx);
717         mutex_unlock(&c->ec_stripes_heap_lock);
718 }
719
720 void bch2_stripes_heap_update(struct bch_fs *c,
721                               struct stripe *m, size_t idx)
722 {
723         ec_stripes_heap *h = &c->ec_stripes_heap;
724         bool do_deletes;
725         size_t i;
726
727         mutex_lock(&c->ec_stripes_heap_lock);
728         heap_verify_backpointer(c, idx);
729
730         h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;
731
732         i = m->heap_idx;
733         heap_sift_up(h,   i, ec_stripes_heap_cmp,
734                      ec_stripes_heap_set_backpointer);
735         heap_sift_down(h, i, ec_stripes_heap_cmp,
736                        ec_stripes_heap_set_backpointer);
737
738         heap_verify_backpointer(c, idx);
739
740         do_deletes = stripe_idx_to_delete(c) != 0;
741         mutex_unlock(&c->ec_stripes_heap_lock);
742
743         if (do_deletes)
744                 bch2_do_stripe_deletes(c);
745 }
746
747 /* stripe deletion */
748
749 static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
750 {
751         struct bch_fs *c = trans->c;
752         struct btree_iter iter;
753         struct bkey_s_c k;
754         struct bkey_s_c_stripe s;
755         int ret;
756
757         k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes, POS(0, idx),
758                                BTREE_ITER_INTENT);
759         ret = bkey_err(k);
760         if (ret)
761                 goto err;
762
763         if (k.k->type != KEY_TYPE_stripe) {
764                 bch2_fs_inconsistent(c, "attempting to delete nonexistent stripe %llu", idx);
765                 ret = -EINVAL;
766                 goto err;
767         }
768
769         s = bkey_s_c_to_stripe(k);
770         for (unsigned i = 0; i < s.v->nr_blocks; i++)
771                 if (stripe_blockcount_get(s.v, i)) {
772                         struct printbuf buf = PRINTBUF;
773
774                         bch2_bkey_val_to_text(&buf, c, k);
775                         bch2_fs_inconsistent(c, "attempting to delete nonempty stripe %s", buf.buf);
776                         printbuf_exit(&buf);
777                         ret = -EINVAL;
778                         goto err;
779                 }
780
781         ret = bch2_btree_delete_at(trans, &iter, 0);
782 err:
783         bch2_trans_iter_exit(trans, &iter);
784         return ret;
785 }
786
787 static void ec_stripe_delete_work(struct work_struct *work)
788 {
789         struct bch_fs *c =
790                 container_of(work, struct bch_fs, ec_stripe_delete_work);
791         struct btree_trans trans;
792         int ret;
793         u64 idx;
794
795         bch2_trans_init(&trans, c, 0, 0);
796
797         while (1) {
798                 mutex_lock(&c->ec_stripes_heap_lock);
799                 idx = stripe_idx_to_delete(c);
800                 mutex_unlock(&c->ec_stripes_heap_lock);
801
802                 if (!idx)
803                         break;
804
805                 ret = commit_do(&trans, NULL, NULL, BTREE_INSERT_NOFAIL,
806                                 ec_stripe_delete(&trans, idx));
807                 if (ret) {
808                         bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
809                         break;
810                 }
811         }
812
813         bch2_trans_exit(&trans);
814
815         bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
816 }
817
818 void bch2_do_stripe_deletes(struct bch_fs *c)
819 {
820         if (bch2_write_ref_tryget(c, BCH_WRITE_REF_stripe_delete) &&
821             !queue_work(c->write_ref_wq, &c->ec_stripe_delete_work))
822                 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
823 }
824
825 /* stripe creation: */
826
827 static int ec_stripe_key_update(struct btree_trans *trans,
828                                 struct bkey_i_stripe *new,
829                                 bool create)
830 {
831         struct bch_fs *c = trans->c;
832         struct btree_iter iter;
833         struct bkey_s_c k;
834         int ret;
835
836         k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
837                                new->k.p, BTREE_ITER_INTENT);
838         ret = bkey_err(k);
839         if (ret)
840                 goto err;
841
842         if (k.k->type != (create ? KEY_TYPE_deleted : KEY_TYPE_stripe)) {
843                 bch2_fs_inconsistent(c, "error %s stripe: got existing key type %s",
844                                      create ? "creating" : "updating",
845                                      bch2_bkey_types[k.k->type]);
846                 ret = -EINVAL;
847                 goto err;
848         }
849
850         if (k.k->type == KEY_TYPE_stripe) {
851                 const struct bch_stripe *old = bkey_s_c_to_stripe(k).v;
852                 unsigned i;
853
854                 if (old->nr_blocks != new->v.nr_blocks) {
855                         bch_err(c, "error updating stripe: nr_blocks does not match");
856                         ret = -EINVAL;
857                         goto err;
858                 }
859
860                 for (i = 0; i < new->v.nr_blocks; i++) {
861                         unsigned v = stripe_blockcount_get(old, i);
862
863                         BUG_ON(v &&
864                                (old->ptrs[i].dev != new->v.ptrs[i].dev ||
865                                 old->ptrs[i].gen != new->v.ptrs[i].gen ||
866                                 old->ptrs[i].offset != new->v.ptrs[i].offset));
867
868                         stripe_blockcount_set(&new->v, i, v);
869                 }
870         }
871
872         ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
873 err:
874         bch2_trans_iter_exit(trans, &iter);
875         return ret;
876 }
877
878 static int ec_stripe_update_extent(struct btree_trans *trans,
879                                    struct bpos bucket, u8 gen,
880                                    struct ec_stripe_buf *s,
881                                    struct bpos *bp_pos)
882 {
883         struct bch_fs *c = trans->c;
884         struct bch_backpointer bp;
885         struct btree_iter iter;
886         struct bkey_s_c k;
887         const struct bch_extent_ptr *ptr_c;
888         struct bch_extent_ptr *ptr, *ec_ptr = NULL;
889         struct bch_extent_stripe_ptr stripe_ptr;
890         struct bkey_i *n;
891         int ret, dev, block;
892
893         ret = bch2_get_next_backpointer(trans, bucket, gen,
894                                 bp_pos, &bp, BTREE_ITER_CACHED);
895         if (ret)
896                 return ret;
897         if (bpos_eq(*bp_pos, SPOS_MAX))
898                 return 0;
899
900         if (bp.level) {
901                 struct printbuf buf = PRINTBUF;
902                 struct btree_iter node_iter;
903                 struct btree *b;
904
905                 b = bch2_backpointer_get_node(trans, &node_iter, *bp_pos, bp);
906                 bch2_trans_iter_exit(trans, &node_iter);
907
908                 if (!b)
909                         return 0;
910
911                 prt_printf(&buf, "found btree node in erasure coded bucket: b=%px\n", b);
912                 bch2_backpointer_to_text(&buf, &bp);
913
914                 bch2_fs_inconsistent(c, "%s", buf.buf);
915                 printbuf_exit(&buf);
916                 return -EIO;
917         }
918
919         k = bch2_backpointer_get_key(trans, &iter, *bp_pos, bp, BTREE_ITER_INTENT);
920         ret = bkey_err(k);
921         if (ret)
922                 return ret;
923         if (!k.k) {
924                 /*
925                  * extent no longer exists - we could flush the btree
926                  * write buffer and retry to verify, but no need:
927                  */
928                 return 0;
929         }
930
931         if (extent_has_stripe_ptr(k, s->key.k.p.offset))
932                 goto out;
933
934         ptr_c = bkey_matches_stripe(&s->key.v, k, &block);
935         /*
936          * It doesn't generally make sense to erasure code cached ptrs:
937          * XXX: should we be incrementing a counter?
938          */
939         if (!ptr_c || ptr_c->cached)
940                 goto out;
941
942         dev = s->key.v.ptrs[block].dev;
943
944         n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(stripe_ptr));
945         ret = PTR_ERR_OR_ZERO(n);
946         if (ret)
947                 goto out;
948
949         bkey_reassemble(n, k);
950
951         bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
952         ec_ptr = bch2_bkey_has_device(bkey_i_to_s(n), dev);
953         BUG_ON(!ec_ptr);
954
955         stripe_ptr = (struct bch_extent_stripe_ptr) {
956                 .type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
957                 .block          = block,
958                 .redundancy     = s->key.v.nr_redundant,
959                 .idx            = s->key.k.p.offset,
960         };
961
962         __extent_entry_insert(n,
963                         (union bch_extent_entry *) ec_ptr,
964                         (union bch_extent_entry *) &stripe_ptr);
965
966         ret = bch2_trans_update(trans, &iter, n, 0);
967 out:
968         bch2_trans_iter_exit(trans, &iter);
969         return ret;
970 }
971
972 static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
973                                    unsigned block)
974 {
975         struct bch_fs *c = trans->c;
976         struct bch_extent_ptr bucket = s->key.v.ptrs[block];
977         struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
978         struct bpos bp_pos = POS_MIN;
979         int ret = 0;
980
981         while (1) {
982                 ret = commit_do(trans, NULL, NULL,
983                                 BTREE_INSERT_NOCHECK_RW|
984                                 BTREE_INSERT_NOFAIL,
985                         ec_stripe_update_extent(trans, bucket_pos, bucket.gen,
986                                                 s, &bp_pos));
987                 if (ret)
988                         break;
989                 if (bkey_eq(bp_pos, POS_MAX))
990                         break;
991
992                 bp_pos = bpos_nosnap_successor(bp_pos);
993         }
994
995         return ret;
996 }
997
998 static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
999 {
1000         struct btree_trans trans;
1001         struct bch_stripe *v = &s->key.v;
1002         unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1003         int ret = 0;
1004
1005         bch2_trans_init(&trans, c, 0, 0);
1006
1007         ret = bch2_btree_write_buffer_flush(&trans);
1008         if (ret)
1009                 goto err;
1010
1011         for (i = 0; i < nr_data; i++) {
1012                 ret = ec_stripe_update_bucket(&trans, s, i);
1013                 if (ret)
1014                         break;
1015         }
1016 err:
1017         bch2_trans_exit(&trans);
1018
1019         return ret;
1020 }
1021
1022 static void zero_out_rest_of_ec_bucket(struct bch_fs *c,
1023                                        struct ec_stripe_new *s,
1024                                        unsigned block,
1025                                        struct open_bucket *ob)
1026 {
1027         struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1028         unsigned offset = ca->mi.bucket_size - ob->sectors_free;
1029         int ret;
1030
1031         if (!bch2_dev_get_ioref(ca, WRITE)) {
1032                 s->err = -EROFS;
1033                 return;
1034         }
1035
1036         memset(s->new_stripe.data[block] + (offset << 9),
1037                0,
1038                ob->sectors_free << 9);
1039
1040         ret = blkdev_issue_zeroout(ca->disk_sb.bdev,
1041                         ob->bucket * ca->mi.bucket_size + offset,
1042                         ob->sectors_free,
1043                         GFP_KERNEL, 0);
1044
1045         percpu_ref_put(&ca->io_ref);
1046
1047         if (ret)
1048                 s->err = ret;
1049 }
1050
1051 void bch2_ec_stripe_new_free(struct bch_fs *c, struct ec_stripe_new *s)
1052 {
1053         if (s->idx)
1054                 bch2_stripe_close(c, s);
1055         kfree(s);
1056 }
1057
1058 /*
1059  * data buckets of new stripe all written: create the stripe
1060  */
1061 static void ec_stripe_create(struct ec_stripe_new *s)
1062 {
1063         struct bch_fs *c = s->c;
1064         struct open_bucket *ob;
1065         struct bch_stripe *v = &s->new_stripe.key.v;
1066         unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
1067         int ret;
1068
1069         BUG_ON(s->h->s == s);
1070
1071         closure_sync(&s->iodone);
1072
1073         if (!s->err) {
1074                 for (i = 0; i < nr_data; i++)
1075                         if (s->blocks[i]) {
1076                                 ob = c->open_buckets + s->blocks[i];
1077
1078                                 if (ob->sectors_free)
1079                                         zero_out_rest_of_ec_bucket(c, s, i, ob);
1080                         }
1081         }
1082
1083         if (s->err) {
1084                 if (!bch2_err_matches(s->err, EROFS))
1085                         bch_err(c, "error creating stripe: error writing data buckets");
1086                 goto err;
1087         }
1088
1089         if (s->have_existing_stripe) {
1090                 ec_validate_checksums(c, &s->existing_stripe);
1091
1092                 if (ec_do_recov(c, &s->existing_stripe)) {
1093                         bch_err(c, "error creating stripe: error reading existing stripe");
1094                         goto err;
1095                 }
1096
1097                 for (i = 0; i < nr_data; i++)
1098                         if (stripe_blockcount_get(&s->existing_stripe.key.v, i))
1099                                 swap(s->new_stripe.data[i],
1100                                      s->existing_stripe.data[i]);
1101
1102                 ec_stripe_buf_exit(&s->existing_stripe);
1103         }
1104
1105         BUG_ON(!s->allocated);
1106         BUG_ON(!s->idx);
1107
1108         ec_generate_ec(&s->new_stripe);
1109
1110         ec_generate_checksums(&s->new_stripe);
1111
1112         /* write p/q: */
1113         for (i = nr_data; i < v->nr_blocks; i++)
1114                 ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
1115         closure_sync(&s->iodone);
1116
1117         if (ec_nr_failed(&s->new_stripe)) {
1118                 bch_err(c, "error creating stripe: error writing redundancy buckets");
1119                 goto err;
1120         }
1121
1122         ret = bch2_trans_do(c, &s->res, NULL,
1123                             BTREE_INSERT_NOCHECK_RW|
1124                             BTREE_INSERT_NOFAIL,
1125                             ec_stripe_key_update(&trans, &s->new_stripe.key,
1126                                                  !s->have_existing_stripe));
1127         if (ret) {
1128                 bch_err(c, "error creating stripe: error creating stripe key");
1129                 goto err;
1130         }
1131
1132         ret = ec_stripe_update_extents(c, &s->new_stripe);
1133         if (ret) {
1134                 bch_err(c, "error creating stripe: error updating pointers: %s",
1135                         bch2_err_str(ret));
1136                 goto err;
1137         }
1138 err:
1139         bch2_disk_reservation_put(c, &s->res);
1140
1141         for (i = 0; i < v->nr_blocks; i++)
1142                 if (s->blocks[i]) {
1143                         ob = c->open_buckets + s->blocks[i];
1144
1145                         if (i < nr_data) {
1146                                 ob->ec = NULL;
1147                                 __bch2_open_bucket_put(c, ob);
1148                         } else {
1149                                 bch2_open_bucket_put(c, ob);
1150                         }
1151                 }
1152
1153         mutex_lock(&c->ec_stripe_new_lock);
1154         list_del(&s->list);
1155         mutex_unlock(&c->ec_stripe_new_lock);
1156
1157         ec_stripe_buf_exit(&s->existing_stripe);
1158         ec_stripe_buf_exit(&s->new_stripe);
1159         closure_debug_destroy(&s->iodone);
1160
1161         ec_stripe_new_put(c, s, STRIPE_REF_stripe);
1162 }
1163
1164 static struct ec_stripe_new *get_pending_stripe(struct bch_fs *c)
1165 {
1166         struct ec_stripe_new *s;
1167
1168         mutex_lock(&c->ec_stripe_new_lock);
1169         list_for_each_entry(s, &c->ec_stripe_new_list, list)
1170                 if (!atomic_read(&s->ref[STRIPE_REF_io]))
1171                         goto out;
1172         s = NULL;
1173 out:
1174         mutex_unlock(&c->ec_stripe_new_lock);
1175
1176         return s;
1177 }
1178
1179 static void ec_stripe_create_work(struct work_struct *work)
1180 {
1181         struct bch_fs *c = container_of(work,
1182                 struct bch_fs, ec_stripe_create_work);
1183         struct ec_stripe_new *s;
1184
1185         while ((s = get_pending_stripe(c)))
1186                 ec_stripe_create(s);
1187
1188         bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1189 }
1190
1191 void bch2_ec_do_stripe_creates(struct bch_fs *c)
1192 {
1193         bch2_write_ref_get(c, BCH_WRITE_REF_stripe_create);
1194
1195         if (!queue_work(system_long_wq, &c->ec_stripe_create_work))
1196                 bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1197 }
1198
1199 static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1200 {
1201         struct ec_stripe_new *s = h->s;
1202
1203         BUG_ON(!s->allocated && !s->err);
1204
1205         h->s            = NULL;
1206         s->pending      = true;
1207
1208         mutex_lock(&c->ec_stripe_new_lock);
1209         list_add(&s->list, &c->ec_stripe_new_list);
1210         mutex_unlock(&c->ec_stripe_new_lock);
1211
1212         ec_stripe_new_put(c, s, STRIPE_REF_io);
1213 }
1214
1215 void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
1216 {
1217         struct ec_stripe_new *s = ob->ec;
1218
1219         s->err = -EIO;
1220 }
1221
1222 void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
1223 {
1224         struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
1225         struct bch_dev *ca;
1226         unsigned offset;
1227
1228         if (!ob)
1229                 return NULL;
1230
1231         BUG_ON(!ob->ec->new_stripe.data[ob->ec_idx]);
1232
1233         ca      = bch_dev_bkey_exists(c, ob->dev);
1234         offset  = ca->mi.bucket_size - ob->sectors_free;
1235
1236         return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1237 }
1238
1239 static int unsigned_cmp(const void *_l, const void *_r)
1240 {
1241         unsigned l = *((const unsigned *) _l);
1242         unsigned r = *((const unsigned *) _r);
1243
1244         return cmp_int(l, r);
1245 }
1246
1247 /* pick most common bucket size: */
1248 static unsigned pick_blocksize(struct bch_fs *c,
1249                                struct bch_devs_mask *devs)
1250 {
1251         struct bch_dev *ca;
1252         unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
1253         struct {
1254                 unsigned nr, size;
1255         } cur = { 0, 0 }, best = { 0, 0 };
1256
1257         for_each_member_device_rcu(ca, c, i, devs)
1258                 sizes[nr++] = ca->mi.bucket_size;
1259
1260         sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);
1261
1262         for (i = 0; i < nr; i++) {
1263                 if (sizes[i] != cur.size) {
1264                         if (cur.nr > best.nr)
1265                                 best = cur;
1266
1267                         cur.nr = 0;
1268                         cur.size = sizes[i];
1269                 }
1270
1271                 cur.nr++;
1272         }
1273
1274         if (cur.nr > best.nr)
1275                 best = cur;
1276
1277         return best.size;
1278 }
1279
1280 static bool may_create_new_stripe(struct bch_fs *c)
1281 {
1282         return false;
1283 }
1284
1285 static void ec_stripe_key_init(struct bch_fs *c,
1286                                struct bkey_i_stripe *s,
1287                                unsigned nr_data,
1288                                unsigned nr_parity,
1289                                unsigned stripe_size)
1290 {
1291         unsigned u64s;
1292
1293         bkey_stripe_init(&s->k_i);
1294         s->v.sectors                    = cpu_to_le16(stripe_size);
1295         s->v.algorithm                  = 0;
1296         s->v.nr_blocks                  = nr_data + nr_parity;
1297         s->v.nr_redundant               = nr_parity;
1298         s->v.csum_granularity_bits      = ilog2(c->opts.encoded_extent_max >> 9);
1299         s->v.csum_type                  = BCH_CSUM_crc32c;
1300         s->v.pad                        = 0;
1301
1302         while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
1303                 BUG_ON(1 << s->v.csum_granularity_bits >=
1304                        le16_to_cpu(s->v.sectors) ||
1305                        s->v.csum_granularity_bits == U8_MAX);
1306                 s->v.csum_granularity_bits++;
1307         }
1308
1309         set_bkey_val_u64s(&s->k, u64s);
1310 }
1311
1312 static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1313 {
1314         struct ec_stripe_new *s;
1315
1316         lockdep_assert_held(&h->lock);
1317
1318         s = kzalloc(sizeof(*s), GFP_KERNEL);
1319         if (!s)
1320                 return -BCH_ERR_ENOMEM_ec_new_stripe_alloc;
1321
1322         mutex_init(&s->lock);
1323         closure_init(&s->iodone, NULL);
1324         atomic_set(&s->ref[STRIPE_REF_stripe], 1);
1325         atomic_set(&s->ref[STRIPE_REF_io], 1);
1326         s->c            = c;
1327         s->h            = h;
1328         s->nr_data      = min_t(unsigned, h->nr_active_devs,
1329                                 BCH_BKEY_PTRS_MAX) - h->redundancy;
1330         s->nr_parity    = h->redundancy;
1331
1332         ec_stripe_key_init(c, &s->new_stripe.key, s->nr_data,
1333                            s->nr_parity, h->blocksize);
1334
1335         h->s = s;
1336         return 0;
1337 }
1338
1339 static struct ec_stripe_head *
1340 ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1341                          unsigned algo, unsigned redundancy,
1342                          enum alloc_reserve reserve)
1343 {
1344         struct ec_stripe_head *h;
1345         struct bch_dev *ca;
1346         unsigned i;
1347
1348         h = kzalloc(sizeof(*h), GFP_KERNEL);
1349         if (!h)
1350                 return NULL;
1351
1352         mutex_init(&h->lock);
1353         BUG_ON(!mutex_trylock(&h->lock));
1354
1355         h->target       = target;
1356         h->algo         = algo;
1357         h->redundancy   = redundancy;
1358         h->reserve      = reserve;
1359
1360         rcu_read_lock();
1361         h->devs = target_rw_devs(c, BCH_DATA_user, target);
1362
1363         for_each_member_device_rcu(ca, c, i, &h->devs)
1364                 if (!ca->mi.durability)
1365                         __clear_bit(i, h->devs.d);
1366
1367         h->blocksize = pick_blocksize(c, &h->devs);
1368
1369         for_each_member_device_rcu(ca, c, i, &h->devs)
1370                 if (ca->mi.bucket_size == h->blocksize)
1371                         h->nr_active_devs++;
1372
1373         rcu_read_unlock();
1374         list_add(&h->list, &c->ec_stripe_head_list);
1375         return h;
1376 }
1377
1378 void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1379 {
1380         if (h->s &&
1381             h->s->allocated &&
1382             bitmap_weight(h->s->blocks_allocated,
1383                           h->s->nr_data) == h->s->nr_data)
1384                 ec_stripe_set_pending(c, h);
1385
1386         mutex_unlock(&h->lock);
1387 }
1388
1389 struct ec_stripe_head *__bch2_ec_stripe_head_get(struct btree_trans *trans,
1390                                                  unsigned target,
1391                                                  unsigned algo,
1392                                                  unsigned redundancy,
1393                                                  enum alloc_reserve reserve)
1394 {
1395         struct bch_fs *c = trans->c;
1396         struct ec_stripe_head *h;
1397         int ret;
1398
1399         if (!redundancy)
1400                 return NULL;
1401
1402         ret = bch2_trans_mutex_lock(trans, &c->ec_stripe_head_lock);
1403         if (ret)
1404                 return ERR_PTR(ret);
1405
1406         if (test_bit(BCH_FS_GOING_RO, &c->flags)) {
1407                 h = ERR_PTR(-EROFS);
1408                 goto found;
1409         }
1410
1411         list_for_each_entry(h, &c->ec_stripe_head_list, list)
1412                 if (h->target           == target &&
1413                     h->algo             == algo &&
1414                     h->redundancy       == redundancy &&
1415                     h->reserve          == reserve) {
1416                         ret = bch2_trans_mutex_lock(trans, &h->lock);
1417                         if (ret)
1418                                 h = ERR_PTR(ret);
1419                         goto found;
1420                 }
1421
1422         h = ec_new_stripe_head_alloc(c, target, algo, redundancy, reserve);
1423 found:
1424         mutex_unlock(&c->ec_stripe_head_lock);
1425         return h;
1426 }
1427
1428 static int new_stripe_alloc_buckets(struct btree_trans *trans, struct ec_stripe_head *h,
1429                                     enum alloc_reserve reserve, struct closure *cl)
1430 {
1431         struct bch_fs *c = trans->c;
1432         struct bch_devs_mask devs = h->devs;
1433         struct open_bucket *ob;
1434         struct open_buckets buckets;
1435         unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1436         bool have_cache = true;
1437         int ret = 0;
1438
1439         BUG_ON(h->s->new_stripe.key.v.nr_blocks         != h->s->nr_data + h->s->nr_parity);
1440         BUG_ON(h->s->new_stripe.key.v.nr_redundant      != h->s->nr_parity);
1441
1442         for_each_set_bit(i, h->s->blocks_gotten, h->s->new_stripe.key.v.nr_blocks) {
1443                 __clear_bit(h->s->new_stripe.key.v.ptrs[i].dev, devs.d);
1444                 if (i < h->s->nr_data)
1445                         nr_have_data++;
1446                 else
1447                         nr_have_parity++;
1448         }
1449
1450         BUG_ON(nr_have_data     > h->s->nr_data);
1451         BUG_ON(nr_have_parity   > h->s->nr_parity);
1452
1453         buckets.nr = 0;
1454         if (nr_have_parity < h->s->nr_parity) {
1455                 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1456                                             &h->parity_stripe,
1457                                             &devs,
1458                                             h->s->nr_parity,
1459                                             &nr_have_parity,
1460                                             &have_cache, 0,
1461                                             BCH_DATA_parity,
1462                                             reserve,
1463                                             cl);
1464
1465                 open_bucket_for_each(c, &buckets, ob, i) {
1466                         j = find_next_zero_bit(h->s->blocks_gotten,
1467                                                h->s->nr_data + h->s->nr_parity,
1468                                                h->s->nr_data);
1469                         BUG_ON(j >= h->s->nr_data + h->s->nr_parity);
1470
1471                         h->s->blocks[j] = buckets.v[i];
1472                         h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1473                         __set_bit(j, h->s->blocks_gotten);
1474                 }
1475
1476                 if (ret)
1477                         return ret;
1478         }
1479
1480         buckets.nr = 0;
1481         if (nr_have_data < h->s->nr_data) {
1482                 ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1483                                             &h->block_stripe,
1484                                             &devs,
1485                                             h->s->nr_data,
1486                                             &nr_have_data,
1487                                             &have_cache, 0,
1488                                             BCH_DATA_user,
1489                                             reserve,
1490                                             cl);
1491
1492                 open_bucket_for_each(c, &buckets, ob, i) {
1493                         j = find_next_zero_bit(h->s->blocks_gotten,
1494                                                h->s->nr_data, 0);
1495                         BUG_ON(j >= h->s->nr_data);
1496
1497                         h->s->blocks[j] = buckets.v[i];
1498                         h->s->new_stripe.key.v.ptrs[j] = bch2_ob_ptr(c, ob);
1499                         __set_bit(j, h->s->blocks_gotten);
1500                 }
1501
1502                 if (ret)
1503                         return ret;
1504         }
1505
1506         return 0;
1507 }
1508
1509 /* XXX: doesn't obey target: */
1510 static s64 get_existing_stripe(struct bch_fs *c,
1511                                struct ec_stripe_head *head)
1512 {
1513         ec_stripes_heap *h = &c->ec_stripes_heap;
1514         struct stripe *m;
1515         size_t heap_idx;
1516         u64 stripe_idx;
1517         s64 ret = -1;
1518
1519         if (may_create_new_stripe(c))
1520                 return -1;
1521
1522         mutex_lock(&c->ec_stripes_heap_lock);
1523         for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1524                 /* No blocks worth reusing, stripe will just be deleted: */
1525                 if (!h->data[heap_idx].blocks_nonempty)
1526                         continue;
1527
1528                 stripe_idx = h->data[heap_idx].idx;
1529
1530                 m = genradix_ptr(&c->stripes, stripe_idx);
1531
1532                 if (m->algorithm        == head->algo &&
1533                     m->nr_redundant     == head->redundancy &&
1534                     m->sectors          == head->blocksize &&
1535                     m->blocks_nonempty  < m->nr_blocks - m->nr_redundant &&
1536                     bch2_try_open_stripe(c, head->s, stripe_idx)) {
1537                         ret = stripe_idx;
1538                         break;
1539                 }
1540         }
1541         mutex_unlock(&c->ec_stripes_heap_lock);
1542         return ret;
1543 }
1544
1545 static int __bch2_ec_stripe_head_reuse(struct btree_trans *trans, struct ec_stripe_head *h)
1546 {
1547         struct bch_fs *c = trans->c;
1548         unsigned i;
1549         s64 idx;
1550         int ret;
1551
1552         /*
1553          * If we can't allocate a new stripe, and there's no stripes with empty
1554          * blocks for us to reuse, that means we have to wait on copygc:
1555          */
1556         idx = get_existing_stripe(c, h);
1557         if (idx < 0)
1558                 return -BCH_ERR_stripe_alloc_blocked;
1559
1560         ret = get_stripe_key_trans(trans, idx, &h->s->existing_stripe);
1561         if (ret) {
1562                 bch2_stripe_close(c, h->s);
1563                 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
1564                         bch2_fs_fatal_error(c, "error reading stripe key: %s", bch2_err_str(ret));
1565                 return ret;
1566         }
1567
1568         BUG_ON(h->s->existing_stripe.key.v.nr_redundant != h->s->nr_parity);
1569         h->s->nr_data = h->s->existing_stripe.key.v.nr_blocks -
1570                 h->s->existing_stripe.key.v.nr_redundant;
1571
1572         ret = ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize);
1573         if (ret) {
1574                 bch2_stripe_close(c, h->s);
1575                 return ret;
1576         }
1577
1578         BUG_ON(h->s->existing_stripe.size != h->blocksize);
1579         BUG_ON(h->s->existing_stripe.size != h->s->existing_stripe.key.v.sectors);
1580
1581         /*
1582          * Free buckets we initially allocated - they might conflict with
1583          * blocks from the stripe we're reusing:
1584          */
1585         for_each_set_bit(i, h->s->blocks_gotten, h->s->new_stripe.key.v.nr_blocks) {
1586                 bch2_open_bucket_put(c, c->open_buckets + h->s->blocks[i]);
1587                 h->s->blocks[i] = 0;
1588         }
1589         memset(h->s->blocks_gotten, 0, sizeof(h->s->blocks_gotten));
1590         memset(h->s->blocks_allocated, 0, sizeof(h->s->blocks_allocated));
1591
1592         for (i = 0; i < h->s->existing_stripe.key.v.nr_blocks; i++) {
1593                 if (stripe_blockcount_get(&h->s->existing_stripe.key.v, i)) {
1594                         __set_bit(i, h->s->blocks_gotten);
1595                         __set_bit(i, h->s->blocks_allocated);
1596                 }
1597
1598                 ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
1599         }
1600
1601         bkey_copy(&h->s->new_stripe.key.k_i, &h->s->existing_stripe.key.k_i);
1602         h->s->have_existing_stripe = true;
1603
1604         return 0;
1605 }
1606
1607 static int __bch2_ec_stripe_head_reserve(struct btree_trans *trans, struct ec_stripe_head *h)
1608 {
1609         struct bch_fs *c = trans->c;
1610         struct btree_iter iter;
1611         struct bkey_s_c k;
1612         struct bpos min_pos = POS(0, 1);
1613         struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
1614         int ret;
1615
1616         if (!h->s->res.sectors) {
1617                 ret = bch2_disk_reservation_get(c, &h->s->res,
1618                                         h->blocksize,
1619                                         h->s->nr_parity,
1620                                         BCH_DISK_RESERVATION_NOFAIL);
1621                 if (ret)
1622                         return ret;
1623         }
1624
1625         for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
1626                            BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
1627                 if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
1628                         if (start_pos.offset) {
1629                                 start_pos = min_pos;
1630                                 bch2_btree_iter_set_pos(&iter, start_pos);
1631                                 continue;
1632                         }
1633
1634                         ret = -BCH_ERR_ENOSPC_stripe_create;
1635                         break;
1636                 }
1637
1638                 if (bkey_deleted(k.k) &&
1639                     bch2_try_open_stripe(c, h->s, k.k->p.offset))
1640                         break;
1641         }
1642
1643         c->ec_stripe_hint = iter.pos.offset;
1644
1645         if (ret)
1646                 goto err;
1647
1648         ret = ec_stripe_mem_alloc(trans, &iter);
1649         if (ret) {
1650                 bch2_stripe_close(c, h->s);
1651                 goto err;
1652         }
1653
1654         h->s->new_stripe.key.k.p = iter.pos;
1655 out:
1656         bch2_trans_iter_exit(trans, &iter);
1657         return ret;
1658 err:
1659         bch2_disk_reservation_put(c, &h->s->res);
1660         goto out;
1661 }
1662
1663 struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *trans,
1664                                                unsigned target,
1665                                                unsigned algo,
1666                                                unsigned redundancy,
1667                                                enum alloc_reserve reserve,
1668                                                struct closure *cl)
1669 {
1670         struct bch_fs *c = trans->c;
1671         struct ec_stripe_head *h;
1672         bool waiting = false;
1673         int ret;
1674
1675         h = __bch2_ec_stripe_head_get(trans, target, algo, redundancy, reserve);
1676         if (!h)
1677                 bch_err(c, "no stripe head");
1678         if (IS_ERR_OR_NULL(h))
1679                 return h;
1680
1681         if (!h->s) {
1682                 ret = ec_new_stripe_alloc(c, h);
1683                 if (ret) {
1684                         bch_err(c, "failed to allocate new stripe");
1685                         goto err;
1686                 }
1687         }
1688
1689         if (h->s->allocated)
1690                 goto allocated;
1691
1692         if (h->s->have_existing_stripe)
1693                 goto alloc_existing;
1694
1695         /* First, try to allocate a full stripe: */
1696         ret =   new_stripe_alloc_buckets(trans, h, RESERVE_stripe, NULL) ?:
1697                 __bch2_ec_stripe_head_reserve(trans, h);
1698         if (!ret)
1699                 goto allocate_buf;
1700         if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
1701             bch2_err_matches(ret, ENOMEM))
1702                 goto err;
1703
1704         /*
1705          * Not enough buckets available for a full stripe: we must reuse an
1706          * existing stripe:
1707          */
1708         while (1) {
1709                 ret = __bch2_ec_stripe_head_reuse(trans, h);
1710                 if (!ret)
1711                         break;
1712                 if (waiting || !cl || ret != -BCH_ERR_stripe_alloc_blocked)
1713                         goto err;
1714
1715                 if (reserve == RESERVE_movinggc) {
1716                         ret =   new_stripe_alloc_buckets(trans, h, reserve, NULL) ?:
1717                                 __bch2_ec_stripe_head_reserve(trans, h);
1718                         if (ret)
1719                                 goto err;
1720                         goto allocate_buf;
1721                 }
1722
1723                 /* XXX freelist_wait? */
1724                 closure_wait(&c->freelist_wait, cl);
1725                 waiting = true;
1726         }
1727
1728         if (waiting)
1729                 closure_wake_up(&c->freelist_wait);
1730 alloc_existing:
1731         /*
1732          * Retry allocating buckets, with the reserve watermark for this
1733          * particular write:
1734          */
1735         ret = new_stripe_alloc_buckets(trans, h, reserve, cl);
1736         if (ret)
1737                 goto err;
1738
1739 allocate_buf:
1740         ret = ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize);
1741         if (ret)
1742                 goto err;
1743
1744         h->s->allocated = true;
1745 allocated:
1746         BUG_ON(!h->s->idx);
1747         BUG_ON(!h->s->new_stripe.data[0]);
1748         BUG_ON(trans->restarted);
1749         return h;
1750 err:
1751         bch2_ec_stripe_head_put(c, h);
1752         return ERR_PTR(ret);
1753 }
1754
1755 static void __bch2_ec_stop(struct bch_fs *c, struct bch_dev *ca)
1756 {
1757         struct ec_stripe_head *h;
1758         struct open_bucket *ob;
1759         unsigned i;
1760
1761         mutex_lock(&c->ec_stripe_head_lock);
1762         list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1763                 mutex_lock(&h->lock);
1764                 if (!h->s)
1765                         goto unlock;
1766
1767                 if (!ca)
1768                         goto found;
1769
1770                 for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++) {
1771                         if (!h->s->blocks[i])
1772                                 continue;
1773
1774                         ob = c->open_buckets + h->s->blocks[i];
1775                         if (ob->dev == ca->dev_idx)
1776                                 goto found;
1777                 }
1778                 goto unlock;
1779 found:
1780                 h->s->err = -EROFS;
1781                 ec_stripe_set_pending(c, h);
1782 unlock:
1783                 mutex_unlock(&h->lock);
1784         }
1785         mutex_unlock(&c->ec_stripe_head_lock);
1786 }
1787
1788 void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
1789 {
1790         __bch2_ec_stop(c, ca);
1791 }
1792
1793 void bch2_fs_ec_stop(struct bch_fs *c)
1794 {
1795         __bch2_ec_stop(c, NULL);
1796 }
1797
1798 static bool bch2_fs_ec_flush_done(struct bch_fs *c)
1799 {
1800         bool ret;
1801
1802         mutex_lock(&c->ec_stripe_new_lock);
1803         ret = list_empty(&c->ec_stripe_new_list);
1804         mutex_unlock(&c->ec_stripe_new_lock);
1805
1806         return ret;
1807 }
1808
1809 void bch2_fs_ec_flush(struct bch_fs *c)
1810 {
1811         wait_event(c->ec_stripe_new_wait, bch2_fs_ec_flush_done(c));
1812 }
1813
1814 int bch2_stripes_read(struct bch_fs *c)
1815 {
1816         struct btree_trans trans;
1817         struct btree_iter iter;
1818         struct bkey_s_c k;
1819         const struct bch_stripe *s;
1820         struct stripe *m;
1821         unsigned i;
1822         int ret;
1823
1824         bch2_trans_init(&trans, c, 0, 0);
1825
1826         for_each_btree_key(&trans, iter, BTREE_ID_stripes, POS_MIN,
1827                            BTREE_ITER_PREFETCH, k, ret) {
1828                 if (k.k->type != KEY_TYPE_stripe)
1829                         continue;
1830
1831                 ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
1832                 if (ret)
1833                         break;
1834
1835                 s = bkey_s_c_to_stripe(k).v;
1836
1837                 m = genradix_ptr(&c->stripes, k.k->p.offset);
1838                 m->sectors      = le16_to_cpu(s->sectors);
1839                 m->algorithm    = s->algorithm;
1840                 m->nr_blocks    = s->nr_blocks;
1841                 m->nr_redundant = s->nr_redundant;
1842                 m->blocks_nonempty = 0;
1843
1844                 for (i = 0; i < s->nr_blocks; i++)
1845                         m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1846
1847                 bch2_stripes_heap_insert(c, m, k.k->p.offset);
1848         }
1849         bch2_trans_iter_exit(&trans, &iter);
1850
1851         bch2_trans_exit(&trans);
1852
1853         if (ret)
1854                 bch_err(c, "error reading stripes: %i", ret);
1855
1856         return ret;
1857 }
1858
1859 void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
1860 {
1861         ec_stripes_heap *h = &c->ec_stripes_heap;
1862         struct stripe *m;
1863         size_t i;
1864
1865         mutex_lock(&c->ec_stripes_heap_lock);
1866         for (i = 0; i < min_t(size_t, h->used, 50); i++) {
1867                 m = genradix_ptr(&c->stripes, h->data[i].idx);
1868
1869                 prt_printf(out, "%zu %u/%u+%u", h->data[i].idx,
1870                        h->data[i].blocks_nonempty,
1871                        m->nr_blocks - m->nr_redundant,
1872                        m->nr_redundant);
1873                 if (bch2_stripe_is_open(c, h->data[i].idx))
1874                         prt_str(out, " open");
1875                 prt_newline(out);
1876         }
1877         mutex_unlock(&c->ec_stripes_heap_lock);
1878 }
1879
1880 void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
1881 {
1882         struct ec_stripe_head *h;
1883         struct ec_stripe_new *s;
1884
1885         mutex_lock(&c->ec_stripe_head_lock);
1886         list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1887                 prt_printf(out, "target %u algo %u redundancy %u %s:\n",
1888                        h->target, h->algo, h->redundancy,
1889                        bch2_alloc_reserves[h->reserve]);
1890
1891                 if (h->s)
1892                         prt_printf(out, "\tidx %llu blocks %u+%u allocated %u\n",
1893                                h->s->idx, h->s->nr_data, h->s->nr_parity,
1894                                bitmap_weight(h->s->blocks_allocated,
1895                                              h->s->nr_data));
1896         }
1897         mutex_unlock(&c->ec_stripe_head_lock);
1898
1899         prt_printf(out, "in flight:\n");
1900
1901         mutex_lock(&c->ec_stripe_new_lock);
1902         list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1903                 prt_printf(out, "\tidx %llu blocks %u+%u ref %u %u %s\n",
1904                            s->idx, s->nr_data, s->nr_parity,
1905                            atomic_read(&s->ref[STRIPE_REF_io]),
1906                            atomic_read(&s->ref[STRIPE_REF_stripe]),
1907                            bch2_alloc_reserves[s->h->reserve]);
1908         }
1909         mutex_unlock(&c->ec_stripe_new_lock);
1910 }
1911
1912 void bch2_fs_ec_exit(struct bch_fs *c)
1913 {
1914         struct ec_stripe_head *h;
1915         unsigned i;
1916
1917         while (1) {
1918                 mutex_lock(&c->ec_stripe_head_lock);
1919                 h = list_first_entry_or_null(&c->ec_stripe_head_list,
1920                                              struct ec_stripe_head, list);
1921                 if (h)
1922                         list_del(&h->list);
1923                 mutex_unlock(&c->ec_stripe_head_lock);
1924                 if (!h)
1925                         break;
1926
1927                 if (h->s) {
1928                         for (i = 0; i < h->s->new_stripe.key.v.nr_blocks; i++)
1929                                 BUG_ON(h->s->blocks[i]);
1930
1931                         kfree(h->s);
1932                 }
1933                 kfree(h);
1934         }
1935
1936         BUG_ON(!list_empty(&c->ec_stripe_new_list));
1937
1938         free_heap(&c->ec_stripes_heap);
1939         genradix_free(&c->stripes);
1940         bioset_exit(&c->ec_bioset);
1941 }
1942
1943 void bch2_fs_ec_init_early(struct bch_fs *c)
1944 {
1945         spin_lock_init(&c->ec_stripes_new_lock);
1946         mutex_init(&c->ec_stripes_heap_lock);
1947
1948         INIT_LIST_HEAD(&c->ec_stripe_head_list);
1949         mutex_init(&c->ec_stripe_head_lock);
1950
1951         INIT_LIST_HEAD(&c->ec_stripe_new_list);
1952         mutex_init(&c->ec_stripe_new_lock);
1953         init_waitqueue_head(&c->ec_stripe_new_wait);
1954
1955         INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1956         INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1957 }
1958
1959 int bch2_fs_ec_init(struct bch_fs *c)
1960 {
1961         return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),
1962                            BIOSET_NEED_BVECS);
1963 }