ec.c 46.6 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0

/* erasure coding */

#include "bcachefs.h"
#include "alloc_foreground.h"
7
#include "backpointers.h"
8
#include "bkey_buf.h"
9 10 11
#include "bset.h"
#include "btree_gc.h"
#include "btree_update.h"
12
#include "btree_write_buffer.h"
13
#include "buckets.h"
Kent Overstreet's avatar
Kent Overstreet committed
14
#include "checksum.h"
15 16 17
#include "disk_groups.h"
#include "ec.h"
#include "error.h"
Kent Overstreet's avatar
Kent Overstreet committed
18
#include "io_read.h"
19
#include "keylist.h"
20
#include "recovery.h"
21
#include "replicas.h"
22 23 24
#include "super-io.h"
#include "util.h"

25 26 27 28
#include <linux/sort.h>

#ifdef __KERNEL__

29 30
#include <linux/raid/pq.h>
#include <linux/raid/xor.h>
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

static void raid5_recov(unsigned disks, unsigned failed_idx,
			size_t size, void **data)
{
	unsigned i = 2, nr;

	BUG_ON(failed_idx >= disks);

	swap(data[0], data[failed_idx]);
	memcpy(data[0], data[1], size);

	while (i < disks) {
		nr = min_t(unsigned, disks - i, MAX_XOR_BLOCKS);
		xor_blocks(nr, size, data[0], data + i);
		i += nr;
	}

	swap(data[0], data[failed_idx]);
}

static void raid_gen(int nd, int np, size_t size, void **v)
{
	if (np >= 1)
		raid5_recov(nd + np, nd, size, v);
	if (np >= 2)
		raid6_call.gen_syndrome(nd + np, size, v);
	BUG_ON(np > 2);
}

static void raid_rec(int nr, int *ir, int nd, int np, size_t size, void **v)
{
	switch (nr) {
	case 0:
		break;
	case 1:
		if (ir[0] < nd + 1)
			raid5_recov(nd + 1, ir[0], size, v);
		else
			raid6_call.gen_syndrome(nd + np, size, v);
		break;
	case 2:
		if (ir[1] < nd) {
			/* data+data failure. */
			raid6_2data_recov(nd + np, size, ir[0], ir[1], v);
		} else if (ir[0] < nd) {
			/* data + p/q failure */

			if (ir[1] == nd) /* data + p failure */
				raid6_datap_recov(nd + np, size, ir[0], v);
			else { /* data + q failure */
				raid5_recov(nd + 1, ir[0], size, v);
				raid6_call.gen_syndrome(nd + np, size, v);
			}
		} else {
			raid_gen(nd, np, size, v);
		}
		break;
	default:
		BUG();
	}
}

#else

#include <raid/raid.h>

#endif
98 99 100 101 102 103 104 105 106 107

struct ec_bio {
	struct bch_dev		*ca;
	struct ec_stripe_buf	*buf;
	size_t			idx;
	struct bio		bio;
};

/* Stripes btree keys: */

108
int bch2_stripe_invalid(struct bch_fs *c, struct bkey_s_c k,
109 110
			enum bkey_invalid_flags flags,
			struct printbuf *err)
111
{
112
	const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
113
	int ret = 0;
114

115 116 117 118
	bkey_fsck_err_on(bkey_eq(k.k->p, POS_MIN) ||
			 bpos_gt(k.k->p, POS(0, U32_MAX)), c, err,
			 stripe_pos_bad,
			 "stripe at bad pos");
119

120 121 122 123
	bkey_fsck_err_on(bkey_val_u64s(k.k) < stripe_val_u64s(s), c, err,
			 stripe_val_size_bad,
			 "incorrect value size (%zu < %u)",
			 bkey_val_u64s(k.k), stripe_val_u64s(s));
124

125 126 127
	ret = bch2_bkey_ptrs_invalid(c, k, flags, err);
fsck_err:
	return ret;
128 129
}

130
void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
131 132
			 struct bkey_s_c k)
{
133
	const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
134
	unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
135

136
	prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
137 138
	       s->algorithm,
	       le16_to_cpu(s->sectors),
139
	       nr_data,
140 141 142 143
	       s->nr_redundant,
	       s->csum_type,
	       1U << s->csum_granularity_bits);

144 145 146 147 148 149 150 151 152
	for (i = 0; i < s->nr_blocks; i++) {
		const struct bch_extent_ptr *ptr = s->ptrs + i;
		struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
		u32 offset;
		u64 b = sector_to_bucket_and_offset(ca, ptr->offset, &offset);

		prt_printf(out, " %u:%llu:%u", ptr->dev, b, offset);
		if (i < nr_data)
			prt_printf(out, "#%u", stripe_blockcount_get(s, i));
153
		prt_printf(out, " gen %u", ptr->gen);
154 155 156
		if (ptr_stale(ca, ptr))
			prt_printf(out, " stale");
	}
157 158
}

159
/* returns blocknr in stripe that we matched: */
160 161
static const struct bch_extent_ptr *bkey_matches_stripe(struct bch_stripe *s,
						struct bkey_s_c k, unsigned *block)
162
{
163 164 165
	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
	const struct bch_extent_ptr *ptr;
	unsigned i, nr_data = s->nr_blocks - s->nr_redundant;
166

167 168
	bkey_for_each_ptr(ptrs, ptr)
		for (i = 0; i < nr_data; i++)
169
			if (__bch2_ptr_matches_stripe(&s->ptrs[i], ptr,
170 171 172 173
						      le16_to_cpu(s->sectors))) {
				*block = i;
				return ptr;
			}
174

175
	return NULL;
176 177
}

178 179
static bool extent_has_stripe_ptr(struct bkey_s_c k, u64 idx)
{
180 181 182 183
	switch (k.k->type) {
	case KEY_TYPE_extent: {
		struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
		const union bch_extent_entry *entry;
184

185 186 187 188 189
		extent_for_each_entry(e, entry)
			if (extent_entry_type(entry) ==
			    BCH_EXTENT_ENTRY_stripe_ptr &&
			    entry->stripe_ptr.idx == idx)
				return true;
190

191 192 193
		break;
	}
	}
194 195 196 197

	return false;
}

198 199
/* Stripe bufs: */

200
static void ec_stripe_buf_exit(struct ec_stripe_buf *buf)
201
{
202 203 204
	if (buf->key.k.type == KEY_TYPE_stripe) {
		struct bkey_i_stripe *s = bkey_i_to_stripe(&buf->key);
		unsigned i;
205

206 207 208 209
		for (i = 0; i < s->v.nr_blocks; i++) {
			kvpfree(buf->data[i], buf->size << 9);
			buf->data[i] = NULL;
		}
210 211 212
	}
}

213
/* XXX: this is a non-mempoolified memory allocation: */
214
static int ec_stripe_buf_init(struct ec_stripe_buf *buf,
215
			      unsigned offset, unsigned size)
216
{
217
	struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
218 219
	unsigned csum_granularity = 1U << v->csum_granularity_bits;
	unsigned end = offset + size;
220 221
	unsigned i;

222 223 224 225 226
	BUG_ON(end > le16_to_cpu(v->sectors));

	offset	= round_down(offset, csum_granularity);
	end	= min_t(unsigned, le16_to_cpu(v->sectors),
			round_up(end, csum_granularity));
227

228 229 230 231 232
	buf->offset	= offset;
	buf->size	= end - offset;

	memset(buf->valid, 0xFF, sizeof(buf->valid));

233
	for (i = 0; i < v->nr_blocks; i++) {
234 235
		buf->data[i] = kvpmalloc(buf->size << 9, GFP_KERNEL);
		if (!buf->data[i])
236 237 238 239 240
			goto err;
	}

	return 0;
err:
241
	ec_stripe_buf_exit(buf);
242
	return -BCH_ERR_ENOMEM_stripe_buf;
243 244
}

245 246
/* Checksumming: */

247 248
static struct bch_csum ec_block_checksum(struct ec_stripe_buf *buf,
					 unsigned block, unsigned offset)
249
{
250
	struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
251
	unsigned csum_granularity = 1 << v->csum_granularity_bits;
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	unsigned end = buf->offset + buf->size;
	unsigned len = min(csum_granularity, end - offset);

	BUG_ON(offset >= end);
	BUG_ON(offset <  buf->offset);
	BUG_ON(offset & (csum_granularity - 1));
	BUG_ON(offset + len != le16_to_cpu(v->sectors) &&
	       (len & (csum_granularity - 1)));

	return bch2_checksum(NULL, v->csum_type,
			     null_nonce(),
			     buf->data[block] + ((offset - buf->offset) << 9),
			     len << 9);
}

static void ec_generate_checksums(struct ec_stripe_buf *buf)
{
269
	struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
270
	unsigned i, j, csums_per_device = stripe_csums_per_device(v);
271

272
	if (!v->csum_type)
273 274 275 276 277
		return;

	BUG_ON(buf->offset);
	BUG_ON(buf->size != le16_to_cpu(v->sectors));

278 279 280 281
	for (i = 0; i < v->nr_blocks; i++)
		for (j = 0; j < csums_per_device; j++)
			stripe_csum_set(v, i, j,
				ec_block_checksum(buf, i, j << v->csum_granularity_bits));
282 283 284 285
}

static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
{
286
	struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
287 288 289
	unsigned csum_granularity = 1 << v->csum_granularity_bits;
	unsigned i;

290
	if (!v->csum_type)
291 292 293 294 295 296 297 298 299 300 301 302
		return;

	for (i = 0; i < v->nr_blocks; i++) {
		unsigned offset = buf->offset;
		unsigned end = buf->offset + buf->size;

		if (!test_bit(i, buf->valid))
			continue;

		while (offset < end) {
			unsigned j = offset >> v->csum_granularity_bits;
			unsigned len = min(csum_granularity, end - offset);
303 304
			struct bch_csum want = stripe_csum_get(v, i, j);
			struct bch_csum got = ec_block_checksum(buf, i, offset);
305

306
			if (bch2_crc_cmp(want, got)) {
307 308 309 310 311 312 313 314 315 316 317
				struct printbuf err = PRINTBUF;
				struct bch_dev *ca = bch_dev_bkey_exists(c, v->ptrs[i].dev);

				prt_printf(&err, "stripe checksum error: expected %0llx:%0llx got %0llx:%0llx (type %s)\n",
					   want.hi, want.lo,
					   got.hi, got.lo,
					   bch2_csum_types[v->csum_type]);
				prt_printf(&err, "  for %ps at %u of\n  ", (void *) _RET_IP_, i);
				bch2_bkey_val_to_text(&err, c, bkey_i_to_s_c(&buf->key));
				bch_err_ratelimited(ca, "%s", err.buf);
				printbuf_exit(&err);
318

319
				clear_bit(i, buf->valid);
320 321

				bch2_io_error(ca, BCH_MEMBER_ERROR_checksum);
322 323 324 325 326 327 328 329 330 331 332 333
				break;
			}

			offset += len;
		}
	}
}

/* Erasure coding: */

static void ec_generate_ec(struct ec_stripe_buf *buf)
{
334
	struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
335 336 337
	unsigned nr_data = v->nr_blocks - v->nr_redundant;
	unsigned bytes = le16_to_cpu(v->sectors) << 9;

338
	raid_gen(nr_data, v->nr_redundant, bytes, buf->data);
339 340 341 342
}

static unsigned ec_nr_failed(struct ec_stripe_buf *buf)
{
343 344 345
	struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;

	return v->nr_blocks - bitmap_weight(buf->valid, v->nr_blocks);
346 347 348 349
}

static int ec_do_recov(struct bch_fs *c, struct ec_stripe_buf *buf)
{
350
	struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
351
	unsigned i, failed[BCH_BKEY_PTRS_MAX], nr_failed = 0;
352 353 354 355
	unsigned nr_data = v->nr_blocks - v->nr_redundant;
	unsigned bytes = buf->size << 9;

	if (ec_nr_failed(buf) > v->nr_redundant) {
356
		bch_err_ratelimited(c,
357 358 359 360 361 362 363 364
			"error doing reconstruct read: unable to read enough blocks");
		return -1;
	}

	for (i = 0; i < nr_data; i++)
		if (!test_bit(i, buf->valid))
			failed[nr_failed++] = i;

365
	raid_rec(nr_failed, failed, nr_data, v->nr_redundant, bytes, buf->data);
366 367 368 369 370 371 372 373
	return 0;
}

/* IO: */

static void ec_block_endio(struct bio *bio)
{
	struct ec_bio *ec_bio = container_of(bio, struct ec_bio, bio);
374
	struct bch_stripe *v = &bkey_i_to_stripe(&ec_bio->buf->key)->v;
375
	struct bch_extent_ptr *ptr = &v->ptrs[ec_bio->idx];
376 377 378
	struct bch_dev *ca = ec_bio->ca;
	struct closure *cl = bio->bi_private;

379 380 381 382 383
	if (bch2_dev_io_err_on(bio->bi_status, ca,
			       bio_data_dir(bio)
			       ? BCH_MEMBER_ERROR_write
			       : BCH_MEMBER_ERROR_read,
			       "erasure coding %s error: %s",
384
			       bio_data_dir(bio) ? "write" : "read",
385
			       bch2_blk_status_to_str(bio->bi_status)))
386 387
		clear_bit(ec_bio->idx, ec_bio->buf->valid);

388 389 390 391 392 393 394
	if (ptr_stale(ca, ptr)) {
		bch_err_ratelimited(ca->fs,
				    "error %s stripe: stale pointer after io",
				    bio_data_dir(bio) == READ ? "reading from" : "writing to");
		clear_bit(ec_bio->idx, ec_bio->buf->valid);
	}

395 396 397 398 399 400
	bio_put(&ec_bio->bio);
	percpu_ref_put(&ca->io_ref);
	closure_put(cl);
}

static void ec_block_io(struct bch_fs *c, struct ec_stripe_buf *buf,
401
			blk_opf_t opf, unsigned idx, struct closure *cl)
402
{
403
	struct bch_stripe *v = &bkey_i_to_stripe(&buf->key)->v;
404 405 406
	unsigned offset = 0, bytes = buf->size << 9;
	struct bch_extent_ptr *ptr = &v->ptrs[idx];
	struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
407
	enum bch_data_type data_type = idx < v->nr_blocks - v->nr_redundant
408 409
		? BCH_DATA_user
		: BCH_DATA_parity;
410
	int rw = op_is_write(opf);
411

412 413 414 415 416 417 418 419
	if (ptr_stale(ca, ptr)) {
		bch_err_ratelimited(c,
				    "error %s stripe: stale pointer",
				    rw == READ ? "reading from" : "writing to");
		clear_bit(idx, buf->valid);
		return;
	}

420 421 422 423 424
	if (!bch2_dev_get_ioref(ca, rw)) {
		clear_bit(idx, buf->valid);
		return;
	}

425 426
	this_cpu_add(ca->io_done->sectors[rw][data_type], buf->size);

427 428 429 430 431 432 433 434 435
	while (offset < bytes) {
		unsigned nr_iovecs = min_t(size_t, BIO_MAX_VECS,
					   DIV_ROUND_UP(bytes, PAGE_SIZE));
		unsigned b = min_t(size_t, bytes - offset,
				   nr_iovecs << PAGE_SHIFT);
		struct ec_bio *ec_bio;

		ec_bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev,
						       nr_iovecs,
436
						       opf,
437 438 439 440 441 442 443 444 445 446 447 448
						       GFP_KERNEL,
						       &c->ec_bioset),
				      struct ec_bio, bio);

		ec_bio->ca			= ca;
		ec_bio->buf			= buf;
		ec_bio->idx			= idx;

		ec_bio->bio.bi_iter.bi_sector	= ptr->offset + buf->offset + (offset >> 9);
		ec_bio->bio.bi_end_io		= ec_block_endio;
		ec_bio->bio.bi_private		= cl;

449
		bch2_bio_map(&ec_bio->bio, buf->data[idx] + offset, b);
450 451 452 453 454 455 456 457 458 459 460 461

		closure_get(cl);
		percpu_ref_get(&ca->io_ref);

		submit_bio(&ec_bio->bio);

		offset += b;
	}

	percpu_ref_put(&ca->io_ref);
}

462 463
static int get_stripe_key_trans(struct btree_trans *trans, u64 idx,
				struct ec_stripe_buf *stripe)
464
{
Kent Overstreet's avatar
Kent Overstreet committed
465
	struct btree_iter iter;
466 467 468
	struct bkey_s_c k;
	int ret;

469 470
	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
			       POS(0, idx), BTREE_ITER_SLOTS);
471 472 473 474 475 476 477
	ret = bkey_err(k);
	if (ret)
		goto err;
	if (k.k->type != KEY_TYPE_stripe) {
		ret = -ENOENT;
		goto err;
	}
478
	bkey_reassemble(&stripe->key, k);
479
err:
480
	bch2_trans_iter_exit(trans, &iter);
481 482 483 484
	return ret;
}

/* recovery read path: */
485
int bch2_ec_read_extent(struct btree_trans *trans, struct bch_read_bio *rbio)
486
{
487
	struct bch_fs *c = trans->c;
488 489 490
	struct ec_stripe_buf *buf;
	struct closure cl;
	struct bch_stripe *v;
491 492
	unsigned i, offset;
	int ret = 0;
493 494 495

	closure_init_stack(&cl);

496
	BUG_ON(!rbio->pick.has_ec);
497

498
	buf = kzalloc(sizeof(*buf), GFP_NOFS);
499
	if (!buf)
500
		return -BCH_ERR_ENOMEM_ec_read_extent;
501

502
	ret = lockrestart_do(trans, get_stripe_key_trans(trans, rbio->pick.ec.idx, buf));
503
	if (ret) {
504
		bch_err_ratelimited(c,
505
			"error doing reconstruct read: error %i looking up stripe", ret);
506
		kfree(buf);
507
		return -EIO;
508 509
	}

510
	v = &bkey_i_to_stripe(&buf->key)->v;
511

512 513 514 515 516
	if (!bch2_ptr_matches_stripe(v, rbio->pick)) {
		bch_err_ratelimited(c,
			"error doing reconstruct read: pointer doesn't match stripe");
		ret = -EIO;
		goto err;
517 518
	}

519 520 521 522 523 524 525
	offset = rbio->bio.bi_iter.bi_sector - v->ptrs[rbio->pick.ec.block].offset;
	if (offset + bio_sectors(&rbio->bio) > le16_to_cpu(v->sectors)) {
		bch_err_ratelimited(c,
			"error doing reconstruct read: read is bigger than stripe");
		ret = -EIO;
		goto err;
	}
526

527 528 529
	ret = ec_stripe_buf_init(buf, offset, bio_sectors(&rbio->bio));
	if (ret)
		goto err;
530

531
	for (i = 0; i < v->nr_blocks; i++)
532 533 534 535 536
		ec_block_io(c, buf, REQ_OP_READ, i, &cl);

	closure_sync(&cl);

	if (ec_nr_failed(buf) > v->nr_redundant) {
537
		bch_err_ratelimited(c,
538 539 540 541 542 543 544 545 546 547 548 549
			"error doing reconstruct read: unable to read enough blocks");
		ret = -EIO;
		goto err;
	}

	ec_validate_checksums(c, buf);

	ret = ec_do_recov(c, buf);
	if (ret)
		goto err;

	memcpy_to_bio(&rbio->bio, rbio->bio.bi_iter,
550
		      buf->data[rbio->pick.ec.block] + ((offset - buf->offset) << 9));
551
err:
552
	ec_stripe_buf_exit(buf);
553 554 555 556
	kfree(buf);
	return ret;
}

557
/* stripe bucket accounting: */
558 559 560 561 562 563 564

static int __ec_stripe_mem_alloc(struct bch_fs *c, size_t idx, gfp_t gfp)
{
	ec_stripes_heap n, *h = &c->ec_stripes_heap;

	if (idx >= h->size) {
		if (!init_heap(&n, max(1024UL, roundup_pow_of_two(idx + 1)), gfp))
565
			return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
566

567
		mutex_lock(&c->ec_stripes_heap_lock);
568 569 570 571 572
		if (n.size > h->size) {
			memcpy(n.data, h->data, h->used * sizeof(h->data[0]));
			n.used = h->used;
			swap(*h, n);
		}
573
		mutex_unlock(&c->ec_stripes_heap_lock);
574 575 576 577

		free_heap(&n);
	}

578
	if (!genradix_ptr_alloc(&c->stripes, idx, gfp))
579
		return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
580 581

	if (c->gc_pos.phase != GC_PHASE_NOT_RUNNING &&
582
	    !genradix_ptr_alloc(&c->gc_stripes, idx, gfp))
583
		return -BCH_ERR_ENOMEM_ec_stripe_mem_alloc;
584 585 586 587

	return 0;
}

588
static int ec_stripe_mem_alloc(struct btree_trans *trans,
589 590
			       struct btree_iter *iter)
{
591 592
	return allocate_dropping_locks_errcode(trans,
			__ec_stripe_mem_alloc(trans->c, iter->pos.offset, _gfp));
593 594
}

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
/*
 * Hash table of open stripes:
 * Stripes that are being created or modified are kept in a hash table, so that
 * stripe deletion can skip them.
 */

static bool __bch2_stripe_is_open(struct bch_fs *c, u64 idx)
{
	unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));
	struct ec_stripe_new *s;

	hlist_for_each_entry(s, &c->ec_stripes_new[hash], hash)
		if (s->idx == idx)
			return true;
	return false;
}

static bool bch2_stripe_is_open(struct bch_fs *c, u64 idx)
{
	bool ret = false;

	spin_lock(&c->ec_stripes_new_lock);
	ret = __bch2_stripe_is_open(c, idx);
	spin_unlock(&c->ec_stripes_new_lock);

	return ret;
}

static bool bch2_try_open_stripe(struct bch_fs *c,
				 struct ec_stripe_new *s,
				 u64 idx)
{
	bool ret;

	spin_lock(&c->ec_stripes_new_lock);
	ret = !__bch2_stripe_is_open(c, idx);
	if (ret) {
		unsigned hash = hash_64(idx, ilog2(ARRAY_SIZE(c->ec_stripes_new)));

		s->idx = idx;
		hlist_add_head(&s->hash, &c->ec_stripes_new[hash]);
	}
	spin_unlock(&c->ec_stripes_new_lock);

	return ret;
}

static void bch2_stripe_close(struct bch_fs *c, struct ec_stripe_new *s)
{
	BUG_ON(!s->idx);

	spin_lock(&c->ec_stripes_new_lock);
	hlist_del_init(&s->hash);
	spin_unlock(&c->ec_stripes_new_lock);

	s->idx = 0;
}

/* Heap of all existing stripes, ordered by blocks_nonempty */

655
static u64 stripe_idx_to_delete(struct bch_fs *c)
656 657
{
	ec_stripes_heap *h = &c->ec_stripes_heap;
658 659

	lockdep_assert_held(&c->ec_stripes_heap_lock);
660

661 662 663 664
	if (h->used &&
	    h->data[0].blocks_nonempty == 0 &&
	    !bch2_stripe_is_open(c, h->data[0].idx))
		return h->data[0].idx;
665 666

	return 0;
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
}

static inline int ec_stripes_heap_cmp(ec_stripes_heap *h,
				      struct ec_stripe_heap_entry l,
				      struct ec_stripe_heap_entry r)
{
	return ((l.blocks_nonempty > r.blocks_nonempty) -
		(l.blocks_nonempty < r.blocks_nonempty));
}

static inline void ec_stripes_heap_set_backpointer(ec_stripes_heap *h,
						   size_t i)
{
	struct bch_fs *c = container_of(h, struct bch_fs, ec_stripes_heap);

682
	genradix_ptr(&c->stripes, h->data[i].idx)->heap_idx = i;
683 684 685 686 687
}

static void heap_verify_backpointer(struct bch_fs *c, size_t idx)
{
	ec_stripes_heap *h = &c->ec_stripes_heap;
688
	struct stripe *m = genradix_ptr(&c->stripes, idx);
689 690 691 692 693 694

	BUG_ON(m->heap_idx >= h->used);
	BUG_ON(h->data[m->heap_idx].idx != idx);
}

void bch2_stripes_heap_del(struct bch_fs *c,
695
			   struct stripe *m, size_t idx)
696
{
697
	mutex_lock(&c->ec_stripes_heap_lock);
698 699 700 701 702
	heap_verify_backpointer(c, idx);

	heap_del(&c->ec_stripes_heap, m->heap_idx,
		 ec_stripes_heap_cmp,
		 ec_stripes_heap_set_backpointer);
703
	mutex_unlock(&c->ec_stripes_heap_lock);
704 705 706
}

void bch2_stripes_heap_insert(struct bch_fs *c,
707
			      struct stripe *m, size_t idx)
708
{
709
	mutex_lock(&c->ec_stripes_heap_lock);
710 711 712 713
	BUG_ON(heap_full(&c->ec_stripes_heap));

	heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) {
			.idx = idx,
714
			.blocks_nonempty = m->blocks_nonempty,
715 716 717 718 719
		}),
		 ec_stripes_heap_cmp,
		 ec_stripes_heap_set_backpointer);

	heap_verify_backpointer(c, idx);
720
	mutex_unlock(&c->ec_stripes_heap_lock);
721 722
}

723 724 725 726
void bch2_stripes_heap_update(struct bch_fs *c,
			      struct stripe *m, size_t idx)
{
	ec_stripes_heap *h = &c->ec_stripes_heap;
727
	bool do_deletes;
728 729
	size_t i;

730
	mutex_lock(&c->ec_stripes_heap_lock);
731 732 733 734 735 736 737 738 739 740 741 742
	heap_verify_backpointer(c, idx);

	h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty;

	i = m->heap_idx;
	heap_sift_up(h,	  i, ec_stripes_heap_cmp,
		     ec_stripes_heap_set_backpointer);
	heap_sift_down(h, i, ec_stripes_heap_cmp,
		       ec_stripes_heap_set_backpointer);

	heap_verify_backpointer(c, idx);

743 744 745 746
	do_deletes = stripe_idx_to_delete(c) != 0;
	mutex_unlock(&c->ec_stripes_heap_lock);

	if (do_deletes)
747
		bch2_do_stripe_deletes(c);
748 749
}

750 751
/* stripe deletion */

752
static int ec_stripe_delete(struct btree_trans *trans, u64 idx)
753
{
754 755 756 757 758 759
	struct bch_fs *c = trans->c;
	struct btree_iter iter;
	struct bkey_s_c k;
	struct bkey_s_c_stripe s;
	int ret;

760 761
	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes, POS(0, idx),
			       BTREE_ITER_INTENT);
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	ret = bkey_err(k);
	if (ret)
		goto err;

	if (k.k->type != KEY_TYPE_stripe) {
		bch2_fs_inconsistent(c, "attempting to delete nonexistent stripe %llu", idx);
		ret = -EINVAL;
		goto err;
	}

	s = bkey_s_c_to_stripe(k);
	for (unsigned i = 0; i < s.v->nr_blocks; i++)
		if (stripe_blockcount_get(s.v, i)) {
			struct printbuf buf = PRINTBUF;

			bch2_bkey_val_to_text(&buf, c, k);
			bch2_fs_inconsistent(c, "attempting to delete nonempty stripe %s", buf.buf);
			printbuf_exit(&buf);
			ret = -EINVAL;
			goto err;
		}

	ret = bch2_btree_delete_at(trans, &iter, 0);
err:
	bch2_trans_iter_exit(trans, &iter);
	return ret;
788 789 790 791 792 793
}

static void ec_stripe_delete_work(struct work_struct *work)
{
	struct bch_fs *c =
		container_of(work, struct bch_fs, ec_stripe_delete_work);
794
	struct btree_trans *trans = bch2_trans_get(c);
795 796 797
	int ret;
	u64 idx;

798
	while (1) {
799
		mutex_lock(&c->ec_stripes_heap_lock);
800
		idx = stripe_idx_to_delete(c);
801
		mutex_unlock(&c->ec_stripes_heap_lock);
802

803 804 805
		if (!idx)
			break;

806
		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
807
				ec_stripe_delete(trans, idx));
808
		if (ret) {
809
			bch_err_fn(c, ret);
810
			break;
811
		}
812
	}
813

814
	bch2_trans_put(trans);
815

816
	bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
817 818 819 820
}

void bch2_do_stripe_deletes(struct bch_fs *c)
{
821
	if (bch2_write_ref_tryget(c, BCH_WRITE_REF_stripe_delete) &&
822
	    !queue_work(c->write_ref_wq, &c->ec_stripe_delete_work))
823
		bch2_write_ref_put(c, BCH_WRITE_REF_stripe_delete);
824 825
}

826 827
/* stripe creation: */

828 829 830
static int ec_stripe_key_update(struct btree_trans *trans,
				struct bkey_i_stripe *new,
				bool create)
831
{
832
	struct bch_fs *c = trans->c;
Kent Overstreet's avatar
Kent Overstreet committed
833
	struct btree_iter iter;
834
	struct bkey_s_c k;
835 836
	int ret;

837 838
	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_stripes,
			       new->k.p, BTREE_ITER_INTENT);
839 840 841 842
	ret = bkey_err(k);
	if (ret)
		goto err;

843 844 845 846 847
	if (k.k->type != (create ? KEY_TYPE_deleted : KEY_TYPE_stripe)) {
		bch2_fs_inconsistent(c, "error %s stripe: got existing key type %s",
				     create ? "creating" : "updating",
				     bch2_bkey_types[k.k->type]);
		ret = -EINVAL;
848 849 850
		goto err;
	}

851 852 853
	if (k.k->type == KEY_TYPE_stripe) {
		const struct bch_stripe *old = bkey_s_c_to_stripe(k).v;
		unsigned i;
854

855 856 857 858 859
		if (old->nr_blocks != new->v.nr_blocks) {
			bch_err(c, "error updating stripe: nr_blocks does not match");
			ret = -EINVAL;
			goto err;
		}
860

861 862 863 864 865 866 867 868 869 870
		for (i = 0; i < new->v.nr_blocks; i++) {
			unsigned v = stripe_blockcount_get(old, i);

			BUG_ON(v &&
			       (old->ptrs[i].dev != new->v.ptrs[i].dev ||
				old->ptrs[i].gen != new->v.ptrs[i].gen ||
				old->ptrs[i].offset != new->v.ptrs[i].offset));

			stripe_blockcount_set(&new->v, i, v);
		}
871
	}
872

Kent Overstreet's avatar
Kent Overstreet committed
873
	ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
874
err:
Kent Overstreet's avatar
Kent Overstreet committed
875
	bch2_trans_iter_exit(trans, &iter);
876 877 878
	return ret;
}

879
static int ec_stripe_update_extent(struct btree_trans *trans,
880
				   struct bpos bucket, u8 gen,
881
				   struct ec_stripe_buf *s,
882
				   struct bpos *bp_pos)
883
{
884
	struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
885 886 887 888
	struct bch_fs *c = trans->c;
	struct bch_backpointer bp;
	struct btree_iter iter;
	struct bkey_s_c k;
889 890
	const struct bch_extent_ptr *ptr_c;
	struct bch_extent_ptr *ptr, *ec_ptr = NULL;
891
	struct bch_extent_stripe_ptr stripe_ptr;
892 893 894
	struct bkey_i *n;
	int ret, dev, block;

895
	ret = bch2_get_next_backpointer(trans, bucket, gen,
896
				bp_pos, &bp, BTREE_ITER_CACHED);
897 898
	if (ret)
		return ret;
899
	if (bpos_eq(*bp_pos, SPOS_MAX))
900
		return 0;
901

902 903 904 905 906
	if (bp.level) {
		struct printbuf buf = PRINTBUF;
		struct btree_iter node_iter;
		struct btree *b;

907
		b = bch2_backpointer_get_node(trans, &node_iter, *bp_pos, bp);
908 909
		bch2_trans_iter_exit(trans, &node_iter);

910 911 912
		if (!b)
			return 0;

913 914 915 916 917
		prt_printf(&buf, "found btree node in erasure coded bucket: b=%px\n", b);
		bch2_backpointer_to_text(&buf, &bp);

		bch2_fs_inconsistent(c, "%s", buf.buf);
		printbuf_exit(&buf);
918
		return -EIO;
919
	}
920

921
	k = bch2_backpointer_get_key(trans, &iter, *bp_pos, bp, BTREE_ITER_INTENT);
922 923 924 925 926 927 928 929
	ret = bkey_err(k);
	if (ret)
		return ret;
	if (!k.k) {
		/*
		 * extent no longer exists - we could flush the btree
		 * write buffer and retry to verify, but no need:
		 */
930
		return 0;
931 932 933 934
	}

	if (extent_has_stripe_ptr(k, s->key.k.p.offset))
		goto out;
935

936
	ptr_c = bkey_matches_stripe(v, k, &block);
937 938 939 940 941
	/*
	 * It doesn't generally make sense to erasure code cached ptrs:
	 * XXX: should we be incrementing a counter?
	 */
	if (!ptr_c || ptr_c->cached)
942
		goto out;
943

944
	dev = v->ptrs[block].dev;
945

946
	n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(stripe_ptr));
947 948
	ret = PTR_ERR_OR_ZERO(n);
	if (ret)
949
		goto out;
950

951 952
	bkey_reassemble(n, k);

953
	bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, ptr->dev != dev);
954
	ec_ptr = bch2_bkey_has_device(bkey_i_to_s(n), dev);
955
	BUG_ON(!ec_ptr);
956

957 958 959
	stripe_ptr = (struct bch_extent_stripe_ptr) {
		.type = 1 << BCH_EXTENT_ENTRY_stripe_ptr,
		.block		= block,
960
		.redundancy	= v->nr_redundant,
961 962 963 964 965 966
		.idx		= s->key.k.p.offset,
	};

	__extent_entry_insert(n,
			(union bch_extent_entry *) ec_ptr,
			(union bch_extent_entry *) &stripe_ptr);
967

968 969 970 971
	ret = bch2_trans_update(trans, &iter, n, 0);
out:
	bch2_trans_iter_exit(trans, &iter);
	return ret;
972
}
973

974 975
static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_buf *s,
				   unsigned block)
976
{
977
	struct bch_fs *c = trans->c;
978 979
	struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
	struct bch_extent_ptr bucket = v->ptrs[block];
980
	struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
981
	struct bpos bp_pos = POS_MIN;
982 983 984 985
	int ret = 0;

	while (1) {
		ret = commit_do(trans, NULL, NULL,
986 987
				BCH_TRANS_COMMIT_no_check_rw|
				BCH_TRANS_COMMIT_no_enospc,
988
			ec_stripe_update_extent(trans, bucket_pos, bucket.gen,
989
						s, &bp_pos));
990 991
		if (ret)
			break;
992
		if (bkey_eq(bp_pos, POS_MAX))
993 994
			break;

995
		bp_pos = bpos_nosnap_successor(bp_pos);
996 997 998 999 1000 1001 1002
	}

	return ret;
}

static int ec_stripe_update_extents(struct bch_fs *c, struct ec_stripe_buf *s)
{
1003
	struct btree_trans *trans = bch2_trans_get(c);
1004
	struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
1005 1006 1007
	unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
	int ret = 0;

1008
	ret = bch2_btree_write_buffer_flush_sync(trans);
1009 1010 1011 1012
	if (ret)
		goto err;

	for (i = 0; i < nr_data; i++) {
1013
		ret = ec_stripe_update_bucket(trans, s, i);
1014 1015 1016 1017
		if (ret)
			break;
	}
err:
1018
	bch2_trans_put(trans);
1019 1020

	return ret;
1021 1022
}

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
static void zero_out_rest_of_ec_bucket(struct bch_fs *c,
				       struct ec_stripe_new *s,
				       unsigned block,
				       struct open_bucket *ob)
{
	struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
	unsigned offset = ca->mi.bucket_size - ob->sectors_free;
	int ret;

	if (!bch2_dev_get_ioref(ca, WRITE)) {
1033
		s->err = -BCH_ERR_erofs_no_writes;
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
		return;
	}

	memset(s->new_stripe.data[block] + (offset << 9),
	       0,
	       ob->sectors_free << 9);

	ret = blkdev_issue_zeroout(ca->disk_sb.bdev,
			ob->bucket * ca->mi.bucket_size + offset,
			ob->sectors_free,
			GFP_KERNEL, 0);

	percpu_ref_put(&ca->io_ref);

	if (ret)
		s->err = ret;
}

1052 1053 1054 1055 1056 1057 1058
void bch2_ec_stripe_new_free(struct bch_fs *c, struct ec_stripe_new *s)
{
	if (s->idx)
		bch2_stripe_close(c, s);
	kfree(s);
}

1059 1060 1061 1062 1063 1064 1065
/*
 * data buckets of new stripe all written: create the stripe
 */
static void ec_stripe_create(struct ec_stripe_new *s)
{
	struct bch_fs *c = s->c;
	struct open_bucket *ob;
1066
	struct bch_stripe *v = &bkey_i_to_stripe(&s->new_stripe.key)->v;
1067 1068 1069 1070 1071
	unsigned i, nr_data = v->nr_blocks - v->nr_redundant;
	int ret;

	BUG_ON(s->h->s == s);

1072
	closure_sync(&s->iodone);
1073

1074 1075 1076 1077
	if (!s->err) {
		for (i = 0; i < nr_data; i++)
			if (s->blocks[i]) {
				ob = c->open_buckets + s->blocks[i];
1078

1079 1080 1081 1082
				if (ob->sectors_free)
					zero_out_rest_of_ec_bucket(c, s, i, ob);
			}
	}
1083

1084
	if (s->err) {
1085
		if (!bch2_err_matches(s->err, EROFS))
1086
			bch_err(c, "error creating stripe: error writing data buckets");
1087 1088 1089
		goto err;
	}

1090 1091 1092 1093 1094 1095 1096 1097 1098
	if (s->have_existing_stripe) {
		ec_validate_checksums(c, &s->existing_stripe);

		if (ec_do_recov(c, &s->existing_stripe)) {
			bch_err(c, "error creating stripe: error reading existing stripe");
			goto err;
		}

		for (i = 0; i < nr_data; i++)
1099
			if (stripe_blockcount_get(&bkey_i_to_stripe(&s->existing_stripe.key)->v, i))
1100 1101 1102
				swap(s->new_stripe.data[i],
				     s->existing_stripe.data[i]);

1103
		ec_stripe_buf_exit(&s->existing_stripe);
1104 1105
	}

1106
	BUG_ON(!s->allocated);
1107
	BUG_ON(!s->idx);
1108

1109
	ec_generate_ec(&s->new_stripe);
1110

1111
	ec_generate_checksums(&s->new_stripe);
1112 1113 1114

	/* write p/q: */
	for (i = nr_data; i < v->nr_blocks; i++)
1115 1116
		ec_block_io(c, &s->new_stripe, REQ_OP_WRITE, i, &s->iodone);
	closure_sync(&s->iodone);
1117

1118 1119
	if (ec_nr_failed(&s->new_stripe)) {
		bch_err(c, "error creating stripe: error writing redundancy buckets");
1120
		goto err;
1121
	}
1122

1123
	ret = bch2_trans_do(c, &s->res, NULL,
1124 1125
			    BCH_TRANS_COMMIT_no_check_rw|
			    BCH_TRANS_COMMIT_no_enospc,
1126
			    ec_stripe_key_update(trans,
1127 1128
					bkey_i_to_stripe(&s->new_stripe.key),
					!s->have_existing_stripe));
1129 1130
	if (ret) {
		bch_err(c, "error creating stripe: error creating stripe key");
1131
		goto err;
1132 1133
	}

1134
	ret = ec_stripe_update_extents(c, &s->new_stripe);
1135
	if (ret) {
1136
		bch_err_msg(c, ret, "creating stripe: error updating pointers");
1137 1138
		goto err;
	}
1139
err:
1140 1141
	bch2_disk_reservation_put(c, &s->res);

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
	for (i = 0; i < v->nr_blocks; i++)
		if (s->blocks[i]) {
			ob = c->open_buckets + s->blocks[i];

			if (i < nr_data) {
				ob->ec = NULL;
				__bch2_open_bucket_put(c, ob);
			} else {
				bch2_open_bucket_put(c, ob);
			}
		}
1153

1154 1155 1156
	mutex_lock(&c->ec_stripe_new_lock);
	list_del(&s->list);
	mutex_unlock(&c->ec_stripe_new_lock);
1157
	wake_up(&c->ec_stripe_new_wait);
1158

1159 1160
	ec_stripe_buf_exit(&s->existing_stripe);
	ec_stripe_buf_exit(&s->new_stripe);
1161
	closure_debug_destroy(&s->iodone);
1162 1163

	ec_stripe_new_put(c, s, STRIPE_REF_stripe);
1164 1165
}

1166
static struct ec_stripe_new *get_pending_stripe(struct bch_fs *c)
1167
{
1168 1169
	struct ec_stripe_new *s;

1170
	mutex_lock(&c->ec_stripe_new_lock);
1171
	list_for_each_entry(s, &c->ec_stripe_new_list, list)
1172
		if (!atomic_read(&s->ref[STRIPE_REF_io]))
1173 1174 1175
			goto out;
	s = NULL;
out:
1176
	mutex_unlock(&c->ec_stripe_new_lock);
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

	return s;
}

static void ec_stripe_create_work(struct work_struct *work)
{
	struct bch_fs *c = container_of(work,
		struct bch_fs, ec_stripe_create_work);
	struct ec_stripe_new *s;

	while ((s = get_pending_stripe(c)))
		ec_stripe_create(s);

	bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
}

void bch2_ec_do_stripe_creates(struct bch_fs *c)
{
	bch2_write_ref_get(c, BCH_WRITE_REF_stripe_create);

	if (!queue_work(system_long_wq, &c->ec_stripe_create_work))
		bch2_write_ref_put(c, BCH_WRITE_REF_stripe_create);
1199
}
1200

1201
static void ec_stripe_set_pending(struct bch_fs *c, struct ec_stripe_head *h)
1202
{
1203 1204
	struct ec_stripe_new *s = h->s;

1205 1206
	BUG_ON(!s->allocated && !s->err);

1207 1208 1209 1210 1211 1212 1213
	h->s		= NULL;
	s->pending	= true;

	mutex_lock(&c->ec_stripe_new_lock);
	list_add(&s->list, &c->ec_stripe_new_list);
	mutex_unlock(&c->ec_stripe_new_lock);

1214
	ec_stripe_new_put(c, s, STRIPE_REF_io);
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
}

void bch2_ec_bucket_cancel(struct bch_fs *c, struct open_bucket *ob)
{
	struct ec_stripe_new *s = ob->ec;

	s->err = -EIO;
}

void *bch2_writepoint_ec_buf(struct bch_fs *c, struct write_point *wp)
{
	struct open_bucket *ob = ec_open_bucket(c, &wp->ptrs);
	struct bch_dev *ca;
	unsigned offset;

	if (!ob)
		return NULL;

1233 1234
	BUG_ON(!ob->ec->new_stripe.data[ob->ec_idx]);

1235
	ca	= bch_dev_bkey_exists(c, ob->dev);
1236 1237
	offset	= ca->mi.bucket_size - ob->sectors_free;

1238
	return ob->ec->new_stripe.data[ob->ec_idx] + (offset << 9);
1239 1240 1241 1242 1243 1244 1245
}

static int unsigned_cmp(const void *_l, const void *_r)
{
	unsigned l = *((const unsigned *) _l);
	unsigned r = *((const unsigned *) _r);

Kent Overstreet's avatar
Kent Overstreet committed
1246
	return cmp_int(l, r);
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
}

/* pick most common bucket size: */
static unsigned pick_blocksize(struct bch_fs *c,
			       struct bch_devs_mask *devs)
{
	struct bch_dev *ca;
	unsigned i, nr = 0, sizes[BCH_SB_MEMBERS_MAX];
	struct {
		unsigned nr, size;
	} cur = { 0, 0 }, best = { 0, 0 };

	for_each_member_device_rcu(ca, c, i, devs)
		sizes[nr++] = ca->mi.bucket_size;

	sort(sizes, nr, sizeof(unsigned), unsigned_cmp, NULL);

	for (i = 0; i < nr; i++) {
		if (sizes[i] != cur.size) {
			if (cur.nr > best.nr)
				best = cur;

			cur.nr = 0;
			cur.size = sizes[i];
		}

		cur.nr++;
	}

	if (cur.nr > best.nr)
		best = cur;

	return best.size;
}

1282 1283 1284 1285 1286
static bool may_create_new_stripe(struct bch_fs *c)
{
	return false;
}

1287
static void ec_stripe_key_init(struct bch_fs *c,
1288
			       struct bkey_i *k,
1289 1290 1291 1292
			       unsigned nr_data,
			       unsigned nr_parity,
			       unsigned stripe_size)
{
1293
	struct bkey_i_stripe *s = bkey_stripe_init(k);
1294 1295 1296 1297 1298 1299
	unsigned u64s;

	s->v.sectors			= cpu_to_le16(stripe_size);
	s->v.algorithm			= 0;
	s->v.nr_blocks			= nr_data + nr_parity;
	s->v.nr_redundant		= nr_parity;
1300
	s->v.csum_granularity_bits	= ilog2(c->opts.encoded_extent_max >> 9);
1301
	s->v.csum_type			= BCH_CSUM_crc32c;
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	s->v.pad			= 0;

	while ((u64s = stripe_val_u64s(&s->v)) > BKEY_VAL_U64s_MAX) {
		BUG_ON(1 << s->v.csum_granularity_bits >=
		       le16_to_cpu(s->v.sectors) ||
		       s->v.csum_granularity_bits == U8_MAX);
		s->v.csum_granularity_bits++;
	}

	set_bkey_val_u64s(&s->k, u64s);
}

static int ec_new_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
1315 1316 1317 1318 1319 1320 1321
{
	struct ec_stripe_new *s;

	lockdep_assert_held(&h->lock);

	s = kzalloc(sizeof(*s), GFP_KERNEL);
	if (!s)
1322
		return -BCH_ERR_ENOMEM_ec_new_stripe_alloc;
1323 1324

	mutex_init(&s->lock);
1325
	closure_init(&s->iodone, NULL);
1326 1327
	atomic_set(&s->ref[STRIPE_REF_stripe], 1);
	atomic_set(&s->ref[STRIPE_REF_io], 1);
1328 1329
	s->c		= c;
	s->h		= h;
1330
	s->nr_data	= min_t(unsigned, h->nr_active_devs,
1331
				BCH_BKEY_PTRS_MAX) - h->redundancy;
1332
	s->nr_parity	= h->redundancy;
1333

1334 1335
	ec_stripe_key_init(c, &s->new_stripe.key,
			   s->nr_data, s->nr_parity, h->blocksize);
1336 1337 1338 1339 1340 1341 1342

	h->s = s;
	return 0;
}

static struct ec_stripe_head *
ec_new_stripe_head_alloc(struct bch_fs *c, unsigned target,
1343
			 unsigned algo, unsigned redundancy,
1344
			 enum bch_watermark watermark)
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
{
	struct ec_stripe_head *h;
	struct bch_dev *ca;
	unsigned i;

	h = kzalloc(sizeof(*h), GFP_KERNEL);
	if (!h)
		return NULL;

	mutex_init(&h->lock);
1355
	BUG_ON(!mutex_trylock(&h->lock));
1356 1357 1358 1359

	h->target	= target;
	h->algo		= algo;
	h->redundancy	= redundancy;
1360
	h->watermark	= watermark;
1361 1362

	rcu_read_lock();
1363
	h->devs = target_rw_devs(c, BCH_DATA_user, target);
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375

	for_each_member_device_rcu(ca, c, i, &h->devs)
		if (!ca->mi.durability)
			__clear_bit(i, h->devs.d);

	h->blocksize = pick_blocksize(c, &h->devs);

	for_each_member_device_rcu(ca, c, i, &h->devs)
		if (ca->mi.bucket_size == h->blocksize)
			h->nr_active_devs++;

	rcu_read_unlock();
1376 1377 1378 1379 1380 1381 1382 1383 1384

	/*
	 * If we only have redundancy + 1 devices, we're better off with just
	 * replication:
	 */
	if (h->nr_active_devs < h->redundancy + 2)
		bch_err(c, "insufficient devices available to create stripe (have %u, need %u) - mismatched bucket sizes?",
			h->nr_active_devs, h->redundancy + 2);

1385
	list_add(&h->list, &c->ec_stripe_head_list);
1386 1387 1388
	return h;
}

1389
void bch2_ec_stripe_head_put(struct bch_fs *c, struct ec_stripe_head *h)
1390 1391
{
	if (h->s &&
1392
	    h->s->allocated &&
1393
	    bitmap_weight(h->s->blocks_allocated,
1394
			  h->s->nr_data) == h->s->nr_data)
1395
		ec_stripe_set_pending(c, h);
1396 1397 1398 1399

	mutex_unlock(&h->lock);
}

1400 1401 1402 1403 1404 1405
static struct ec_stripe_head *
__bch2_ec_stripe_head_get(struct btree_trans *trans,
			  unsigned target,
			  unsigned algo,
			  unsigned redundancy,
			  enum bch_watermark watermark)
1406
{
1407
	struct bch_fs *c = trans->c;
1408
	struct ec_stripe_head *h;
1409
	int ret;
1410 1411 1412 1413

	if (!redundancy)
		return NULL;

1414 1415 1416
	ret = bch2_trans_mutex_lock(trans, &c->ec_stripe_head_lock);
	if (ret)
		return ERR_PTR(ret);
1417

1418
	if (test_bit(BCH_FS_going_ro, &c->flags)) {
1419
		h = ERR_PTR(-BCH_ERR_erofs_no_writes);
1420 1421 1422
		goto found;
	}

1423
	list_for_each_entry(h, &c->ec_stripe_head_list, list)
1424 1425
		if (h->target		== target &&
		    h->algo		== algo &&
1426
		    h->redundancy	== redundancy &&
1427
		    h->watermark	== watermark) {
1428 1429 1430
			ret = bch2_trans_mutex_lock(trans, &h->lock);
			if (ret)
				h = ERR_PTR(ret);
1431 1432 1433
			goto found;
		}

1434
	h = ec_new_stripe_head_alloc(c, target, algo, redundancy, watermark);
1435
found:
1436 1437 1438 1439 1440
	if (!IS_ERR_OR_NULL(h) &&
	    h->nr_active_devs < h->redundancy + 2) {
		mutex_unlock(&h->lock);
		h = NULL;
	}
1441
	mutex_unlock(&c->ec_stripe_head_lock);
1442 1443 1444
	return h;
}

1445
static int new_stripe_alloc_buckets(struct btree_trans *trans, struct ec_stripe_head *h,
1446
				    enum bch_watermark watermark, struct closure *cl)
1447
{
1448
	struct bch_fs *c = trans->c;
1449
	struct bch_devs_mask devs = h->devs;
1450
	struct open_bucket *ob;
1451
	struct open_buckets buckets;
1452
	struct bch_stripe *v = &bkey_i_to_stripe(&h->s->new_stripe.key)->v;
1453
	unsigned i, j, nr_have_parity = 0, nr_have_data = 0;
1454
	bool have_cache = true;
1455
	int ret = 0;
1456

1457 1458
	BUG_ON(v->nr_blocks	!= h->s->nr_data + h->s->nr_parity);
	BUG_ON(v->nr_redundant	!= h->s->nr_parity);
1459

1460 1461
	for_each_set_bit(i, h->s->blocks_gotten, v->nr_blocks) {
		__clear_bit(v->ptrs[i].dev, devs.d);
1462 1463 1464 1465
		if (i < h->s->nr_data)
			nr_have_data++;
		else
			nr_have_parity++;
1466 1467
	}

1468 1469
	BUG_ON(nr_have_data	> h->s->nr_data);
	BUG_ON(nr_have_parity	> h->s->nr_parity);
1470

1471 1472
	buckets.nr = 0;
	if (nr_have_parity < h->s->nr_parity) {
1473
		ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1474 1475
					    &h->parity_stripe,
					    &devs,
1476 1477
					    h->s->nr_parity,
					    &nr_have_parity,
1478 1479
					    &have_cache, 0,
					    BCH_DATA_parity,
1480
					    watermark,
1481
					    cl);
1482 1483 1484 1485 1486 1487 1488 1489

		open_bucket_for_each(c, &buckets, ob, i) {
			j = find_next_zero_bit(h->s->blocks_gotten,
					       h->s->nr_data + h->s->nr_parity,
					       h->s->nr_data);
			BUG_ON(j >= h->s->nr_data + h->s->nr_parity);

			h->s->blocks[j] = buckets.v[i];
1490
			v->ptrs[j] = bch2_ob_ptr(c, ob);
1491 1492 1493
			__set_bit(j, h->s->blocks_gotten);
		}

1494
		if (ret)
1495
			return ret;
1496 1497
	}

1498 1499
	buckets.nr = 0;
	if (nr_have_data < h->s->nr_data) {
1500
		ret = bch2_bucket_alloc_set_trans(trans, &buckets,
1501 1502
					    &h->block_stripe,
					    &devs,
1503 1504
					    h->s->nr_data,
					    &nr_have_data,
1505 1506
					    &have_cache, 0,
					    BCH_DATA_user,
1507
					    watermark,
1508
					    cl);
1509 1510 1511 1512 1513 1514 1515

		open_bucket_for_each(c, &buckets, ob, i) {
			j = find_next_zero_bit(h->s->blocks_gotten,
					       h->s->nr_data, 0);
			BUG_ON(j >= h->s->nr_data);

			h->s->blocks[j] = buckets.v[i];
1516
			v->ptrs[j] = bch2_ob_ptr(c, ob);
1517 1518 1519
			__set_bit(j, h->s->blocks_gotten);
		}

1520
		if (ret)
1521
			return ret;
1522
	}
1523 1524

	return 0;
1525 1526
}

1527 1528
/* XXX: doesn't obey target: */
static s64 get_existing_stripe(struct bch_fs *c,
1529
			       struct ec_stripe_head *head)
1530 1531 1532 1533 1534
{
	ec_stripes_heap *h = &c->ec_stripes_heap;
	struct stripe *m;
	size_t heap_idx;
	u64 stripe_idx;
1535
	s64 ret = -1;
1536 1537 1538 1539

	if (may_create_new_stripe(c))
		return -1;

1540
	mutex_lock(&c->ec_stripes_heap_lock);
1541
	for (heap_idx = 0; heap_idx < h->used; heap_idx++) {
1542
		/* No blocks worth reusing, stripe will just be deleted: */
1543 1544 1545 1546
		if (!h->data[heap_idx].blocks_nonempty)
			continue;

		stripe_idx = h->data[heap_idx].idx;
1547

1548
		m = genradix_ptr(&c->stripes, stripe_idx);
1549

1550 1551 1552
		if (m->algorithm	== head->algo &&
		    m->nr_redundant	== head->redundancy &&
		    m->sectors		== head->blocksize &&
1553 1554
		    m->blocks_nonempty	< m->nr_blocks - m->nr_redundant &&
		    bch2_try_open_stripe(c, head->s, stripe_idx)) {
1555 1556
			ret = stripe_idx;
			break;
1557 1558
		}
	}
1559
	mutex_unlock(&c->ec_stripes_heap_lock);
1560
	return ret;
1561 1562
}

1563
static int __bch2_ec_stripe_head_reuse(struct btree_trans *trans, struct ec_stripe_head *h)
1564
{
1565
	struct bch_fs *c = trans->c;
1566 1567
	struct bch_stripe *new_v = &bkey_i_to_stripe(&h->s->new_stripe.key)->v;
	struct bch_stripe *existing_v;
1568 1569 1570 1571
	unsigned i;
	s64 idx;
	int ret;

1572 1573 1574 1575
	/*
	 * If we can't allocate a new stripe, and there's no stripes with empty
	 * blocks for us to reuse, that means we have to wait on copygc:
	 */
1576
	idx = get_existing_stripe(c, h);
1577
	if (idx < 0)
1578
		return -BCH_ERR_stripe_alloc_blocked;
1579

1580
	ret = get_stripe_key_trans(trans, idx, &h->s->existing_stripe);
1581
	if (ret) {
1582 1583 1584
		bch2_stripe_close(c, h->s);
		if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
			bch2_fs_fatal_error(c, "error reading stripe key: %s", bch2_err_str(ret));
1585 1586 1587
		return ret;
	}

1588 1589 1590 1591 1592
	existing_v = &bkey_i_to_stripe(&h->s->existing_stripe.key)->v;

	BUG_ON(existing_v->nr_redundant != h->s->nr_parity);
	h->s->nr_data = existing_v->nr_blocks -
		existing_v->nr_redundant;
1593 1594 1595 1596 1597

	ret = ec_stripe_buf_init(&h->s->existing_stripe, 0, h->blocksize);
	if (ret) {
		bch2_stripe_close(c, h->s);
		return ret;
1598 1599 1600
	}

	BUG_ON(h->s->existing_stripe.size != h->blocksize);
1601
	BUG_ON(h->s->existing_stripe.size != le16_to_cpu(existing_v->sectors));
1602

1603 1604 1605 1606
	/*
	 * Free buckets we initially allocated - they might conflict with
	 * blocks from the stripe we're reusing:
	 */
1607
	for_each_set_bit(i, h->s->blocks_gotten, new_v->nr_blocks) {
1608 1609 1610 1611 1612 1613
		bch2_open_bucket_put(c, c->open_buckets + h->s->blocks[i]);
		h->s->blocks[i] = 0;
	}
	memset(h->s->blocks_gotten, 0, sizeof(h->s->blocks_gotten));
	memset(h->s->blocks_allocated, 0, sizeof(h->s->blocks_allocated));

1614 1615
	for (i = 0; i < existing_v->nr_blocks; i++) {
		if (stripe_blockcount_get(existing_v, i)) {
1616 1617 1618 1619 1620 1621 1622
			__set_bit(i, h->s->blocks_gotten);
			__set_bit(i, h->s->blocks_allocated);
		}

		ec_block_io(c, &h->s->existing_stripe, READ, i, &h->s->iodone);
	}

1623
	bkey_copy(&h->s->new_stripe.key, &h->s->existing_stripe.key);
1624
	h->s->have_existing_stripe = true;
1625 1626 1627 1628

	return 0;
}

1629
static int __bch2_ec_stripe_head_reserve(struct btree_trans *trans, struct ec_stripe_head *h)
1630
{
1631 1632 1633 1634 1635 1636 1637
	struct bch_fs *c = trans->c;
	struct btree_iter iter;
	struct bkey_s_c k;
	struct bpos min_pos = POS(0, 1);
	struct bpos start_pos = bpos_max(min_pos, POS(0, c->ec_stripe_hint));
	int ret;

1638 1639
	if (!h->s->res.sectors) {
		ret = bch2_disk_reservation_get(c, &h->s->res,
1640
					h->blocksize,
1641 1642 1643 1644 1645
					h->s->nr_parity,
					BCH_DISK_RESERVATION_NOFAIL);
		if (ret)
			return ret;
	}
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682

	for_each_btree_key_norestart(trans, iter, BTREE_ID_stripes, start_pos,
			   BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
		if (bkey_gt(k.k->p, POS(0, U32_MAX))) {
			if (start_pos.offset) {
				start_pos = min_pos;
				bch2_btree_iter_set_pos(&iter, start_pos);
				continue;
			}

			ret = -BCH_ERR_ENOSPC_stripe_create;
			break;
		}

		if (bkey_deleted(k.k) &&
		    bch2_try_open_stripe(c, h->s, k.k->p.offset))
			break;
	}

	c->ec_stripe_hint = iter.pos.offset;

	if (ret)
		goto err;

	ret = ec_stripe_mem_alloc(trans, &iter);
	if (ret) {
		bch2_stripe_close(c, h->s);
		goto err;
	}

	h->s->new_stripe.key.k.p = iter.pos;
out:
	bch2_trans_iter_exit(trans, &iter);
	return ret;
err:
	bch2_disk_reservation_put(c, &h->s->res);
	goto out;
1683 1684
}

1685
struct ec_stripe_head *bch2_ec_stripe_head_get(struct btree_trans *trans,
1686 1687
					       unsigned target,
					       unsigned algo,
1688
					       unsigned redundancy,
1689
					       enum bch_watermark watermark,
1690
					       struct closure *cl)
1691
{
1692
	struct bch_fs *c = trans->c;
1693
	struct ec_stripe_head *h;
1694
	bool waiting = false;
1695
	int ret;
1696

1697
	h = __bch2_ec_stripe_head_get(trans, target, algo, redundancy, watermark);
1698 1699
	if (IS_ERR_OR_NULL(h))
		return h;
1700

1701
	if (!h->s) {
1702 1703
		ret = ec_new_stripe_alloc(c, h);
		if (ret) {
1704
			bch_err(c, "failed to allocate new stripe");
1705
			goto err;
1706
		}
1707
	}
1708

1709 1710
	if (h->s->allocated)
		goto allocated;
1711

1712 1713 1714 1715
	if (h->s->have_existing_stripe)
		goto alloc_existing;

	/* First, try to allocate a full stripe: */
1716
	ret =   new_stripe_alloc_buckets(trans, h, BCH_WATERMARK_stripe, NULL) ?:
1717 1718
		__bch2_ec_stripe_head_reserve(trans, h);
	if (!ret)
1719
		goto allocate_buf;
1720 1721
	if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
	    bch2_err_matches(ret, ENOMEM))
1722
		goto err;
1723

1724 1725 1726 1727 1728 1729 1730 1731 1732
	/*
	 * Not enough buckets available for a full stripe: we must reuse an
	 * existing stripe:
	 */
	while (1) {
		ret = __bch2_ec_stripe_head_reuse(trans, h);
		if (!ret)
			break;
		if (waiting || !cl || ret != -BCH_ERR_stripe_alloc_blocked)
1733
			goto err;
1734

1735 1736
		if (watermark == BCH_WATERMARK_copygc) {
			ret =   new_stripe_alloc_buckets(trans, h, watermark, NULL) ?:
1737 1738 1739 1740 1741 1742
				__bch2_ec_stripe_head_reserve(trans, h);
			if (ret)
				goto err;
			goto allocate_buf;
		}

1743 1744 1745
		/* XXX freelist_wait? */
		closure_wait(&c->freelist_wait, cl);
		waiting = true;
1746
	}
1747

1748 1749 1750 1751
	if (waiting)
		closure_wake_up(&c->freelist_wait);
alloc_existing:
	/*
1752
	 * Retry allocating buckets, with the watermark for this
1753 1754
	 * particular write:
	 */
1755
	ret = new_stripe_alloc_buckets(trans, h, watermark, cl);
1756 1757
	if (ret)
		goto err;
1758 1759 1760 1761 1762 1763

allocate_buf:
	ret = ec_stripe_buf_init(&h->s->new_stripe, 0, h->blocksize);
	if (ret)
		goto err;

1764
	h->s->allocated = true;
1765
allocated:
1766
	BUG_ON(!h->s->idx);
1767
	BUG_ON(!h->s->new_stripe.data[0]);
1768
	BUG_ON(trans->restarted);
1769
	return h;
1770 1771
err:
	bch2_ec_stripe_head_put(c, h);
1772
	return ERR_PTR(ret);
1773 1774
}

1775
static void __bch2_ec_stop(struct bch_fs *c, struct bch_dev *ca)
1776 1777 1778 1779 1780
{
	struct ec_stripe_head *h;
	struct open_bucket *ob;
	unsigned i;

1781 1782
	mutex_lock(&c->ec_stripe_head_lock);
	list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1783 1784 1785 1786
		mutex_lock(&h->lock);
		if (!h->s)
			goto unlock;

1787 1788 1789
		if (!ca)
			goto found;

1790
		for (i = 0; i < bkey_i_to_stripe(&h->s->new_stripe.key)->v.nr_blocks; i++) {
1791 1792 1793 1794
			if (!h->s->blocks[i])
				continue;

			ob = c->open_buckets + h->s->blocks[i];
1795
			if (ob->dev == ca->dev_idx)
1796
				goto found;
1797
		}
1798 1799
		goto unlock;
found:
1800
		h->s->err = -BCH_ERR_erofs_no_writes;
1801
		ec_stripe_set_pending(c, h);
1802 1803 1804
unlock:
		mutex_unlock(&h->lock);
	}
1805
	mutex_unlock(&c->ec_stripe_head_lock);
1806 1807
}

1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
void bch2_ec_stop_dev(struct bch_fs *c, struct bch_dev *ca)
{
	__bch2_ec_stop(c, ca);
}

void bch2_fs_ec_stop(struct bch_fs *c)
{
	__bch2_ec_stop(c, NULL);
}

static bool bch2_fs_ec_flush_done(struct bch_fs *c)
{
	bool ret;

	mutex_lock(&c->ec_stripe_new_lock);
	ret = list_empty(&c->ec_stripe_new_list);
	mutex_unlock(&c->ec_stripe_new_lock);

	return ret;
}

void bch2_fs_ec_flush(struct bch_fs *c)
{
	wait_event(c->ec_stripe_new_wait, bch2_fs_ec_flush_done(c));
}

1834
int bch2_stripes_read(struct bch_fs *c)
1835
{
1836 1837
	struct btree_iter iter;
	struct bkey_s_c k;
1838 1839 1840
	const struct bch_stripe *s;
	struct stripe *m;
	unsigned i;
1841
	int ret;
1842

1843
	ret = bch2_trans_run(c,
1844 1845
		for_each_btree_key(trans, iter, BTREE_ID_stripes, POS_MIN,
				   BTREE_ITER_PREFETCH, k, ({
1846 1847
			if (k.k->type != KEY_TYPE_stripe)
				continue;
1848

1849 1850 1851
			ret = __ec_stripe_mem_alloc(c, k.k->p.offset, GFP_KERNEL);
			if (ret)
				break;
1852

1853
			s = bkey_s_c_to_stripe(k).v;
1854

1855 1856 1857 1858 1859 1860
			m = genradix_ptr(&c->stripes, k.k->p.offset);
			m->sectors	= le16_to_cpu(s->sectors);
			m->algorithm	= s->algorithm;
			m->nr_blocks	= s->nr_blocks;
			m->nr_redundant	= s->nr_redundant;
			m->blocks_nonempty = 0;
1861

1862 1863
			for (i = 0; i < s->nr_blocks; i++)
				m->blocks_nonempty += !!stripe_blockcount_get(s, i);
1864

1865 1866 1867
			bch2_stripes_heap_insert(c, m, k.k->p.offset);
			0;
		})));
1868

1869
	bch_err_fn(c, ret);
1870
	return ret;
1871 1872
}

1873 1874 1875 1876 1877 1878
void bch2_stripes_heap_to_text(struct printbuf *out, struct bch_fs *c)
{
	ec_stripes_heap *h = &c->ec_stripes_heap;
	struct stripe *m;
	size_t i;

1879
	mutex_lock(&c->ec_stripes_heap_lock);
1880
	for (i = 0; i < min_t(size_t, h->used, 50); i++) {
1881
		m = genradix_ptr(&c->stripes, h->data[i].idx);
1882

1883
		prt_printf(out, "%zu %u/%u+%u", h->data[i].idx,
1884 1885 1886
		       h->data[i].blocks_nonempty,
		       m->nr_blocks - m->nr_redundant,
		       m->nr_redundant);
1887 1888 1889
		if (bch2_stripe_is_open(c, h->data[i].idx))
			prt_str(out, " open");
		prt_newline(out);
1890
	}
1891
	mutex_unlock(&c->ec_stripes_heap_lock);
1892 1893
}

1894 1895 1896 1897 1898 1899 1900
void bch2_new_stripes_to_text(struct printbuf *out, struct bch_fs *c)
{
	struct ec_stripe_head *h;
	struct ec_stripe_new *s;

	mutex_lock(&c->ec_stripe_head_lock);
	list_for_each_entry(h, &c->ec_stripe_head_list, list) {
1901 1902
		prt_printf(out, "target %u algo %u redundancy %u %s:\n",
		       h->target, h->algo, h->redundancy,
1903
		       bch2_watermarks[h->watermark]);
1904 1905

		if (h->s)
1906
			prt_printf(out, "\tidx %llu blocks %u+%u allocated %u\n",
1907
			       h->s->idx, h->s->nr_data, h->s->nr_parity,
1908
			       bitmap_weight(h->s->blocks_allocated,
1909
					     h->s->nr_data));
1910 1911 1912
	}
	mutex_unlock(&c->ec_stripe_head_lock);

1913 1914
	prt_printf(out, "in flight:\n");

1915
	mutex_lock(&c->ec_stripe_new_lock);
1916
	list_for_each_entry(s, &c->ec_stripe_new_list, list) {
1917
		prt_printf(out, "\tidx %llu blocks %u+%u ref %u %u %s\n",
1918
			   s->idx, s->nr_data, s->nr_parity,
1919
			   atomic_read(&s->ref[STRIPE_REF_io]),
1920
			   atomic_read(&s->ref[STRIPE_REF_stripe]),
1921
			   bch2_watermarks[s->h->watermark]);
1922 1923 1924 1925
	}
	mutex_unlock(&c->ec_stripe_new_lock);
}

1926 1927 1928
void bch2_fs_ec_exit(struct bch_fs *c)
{
	struct ec_stripe_head *h;
1929
	unsigned i;
1930 1931

	while (1) {
1932 1933
		mutex_lock(&c->ec_stripe_head_lock);
		h = list_first_entry_or_null(&c->ec_stripe_head_list,
1934 1935 1936
					     struct ec_stripe_head, list);
		if (h)
			list_del(&h->list);
1937
		mutex_unlock(&c->ec_stripe_head_lock);
1938 1939 1940
		if (!h)
			break;

1941
		if (h->s) {
1942
			for (i = 0; i < bkey_i_to_stripe(&h->s->new_stripe.key)->v.nr_blocks; i++)
1943 1944 1945 1946
				BUG_ON(h->s->blocks[i]);

			kfree(h->s);
		}
1947 1948 1949
		kfree(h);
	}

1950 1951
	BUG_ON(!list_empty(&c->ec_stripe_new_list));

1952
	free_heap(&c->ec_stripes_heap);
1953
	genradix_free(&c->stripes);
1954 1955 1956
	bioset_exit(&c->ec_bioset);
}

1957
void bch2_fs_ec_init_early(struct bch_fs *c)
1958
{
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
	spin_lock_init(&c->ec_stripes_new_lock);
	mutex_init(&c->ec_stripes_heap_lock);

	INIT_LIST_HEAD(&c->ec_stripe_head_list);
	mutex_init(&c->ec_stripe_head_lock);

	INIT_LIST_HEAD(&c->ec_stripe_new_list);
	mutex_init(&c->ec_stripe_new_lock);
	init_waitqueue_head(&c->ec_stripe_new_wait);

1969
	INIT_WORK(&c->ec_stripe_create_work, ec_stripe_create_work);
1970
	INIT_WORK(&c->ec_stripe_delete_work, ec_stripe_delete_work);
1971
}
1972

1973 1974
int bch2_fs_ec_init(struct bch_fs *c)
{
1975 1976 1977
	return bioset_init(&c->ec_bioset, 1, offsetof(struct ec_bio, bio),
			   BIOSET_NEED_BVECS);
}