xref: /linux/fs/bcachefs/rebalance.c (revision f96a974170b749e3a56844e25b31d46a7233b6f6)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "alloc_background.h"
5 #include "alloc_foreground.h"
6 #include "btree_iter.h"
7 #include "btree_update.h"
8 #include "btree_write_buffer.h"
9 #include "buckets.h"
10 #include "clock.h"
11 #include "compress.h"
12 #include "disk_groups.h"
13 #include "errcode.h"
14 #include "error.h"
15 #include "inode.h"
16 #include "io_write.h"
17 #include "move.h"
18 #include "rebalance.h"
19 #include "subvolume.h"
20 #include "super-io.h"
21 #include "trace.h"
22 
23 #include <linux/freezer.h>
24 #include <linux/kthread.h>
25 #include <linux/sched/cputime.h>
26 
27 /* bch_extent_rebalance: */
28 
29 static const struct bch_extent_rebalance *bch2_bkey_rebalance_opts(struct bkey_s_c k)
30 {
31 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
32 	const union bch_extent_entry *entry;
33 
34 	bkey_extent_entry_for_each(ptrs, entry)
35 		if (__extent_entry_type(entry) == BCH_EXTENT_ENTRY_rebalance)
36 			return &entry->rebalance;
37 
38 	return NULL;
39 }
40 
41 static inline unsigned bch2_bkey_ptrs_need_compress(struct bch_fs *c,
42 					   struct bch_io_opts *opts,
43 					   struct bkey_s_c k,
44 					   struct bkey_ptrs_c ptrs)
45 {
46 	if (!opts->background_compression)
47 		return 0;
48 
49 	unsigned compression_type = bch2_compression_opt_to_type(opts->background_compression);
50 	const union bch_extent_entry *entry;
51 	struct extent_ptr_decoded p;
52 	unsigned ptr_bit = 1;
53 	unsigned rewrite_ptrs = 0;
54 
55 	bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
56 		if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible ||
57 		    p.ptr.unwritten)
58 			return 0;
59 
60 		if (!p.ptr.cached && p.crc.compression_type != compression_type)
61 			rewrite_ptrs |= ptr_bit;
62 		ptr_bit <<= 1;
63 	}
64 
65 	return rewrite_ptrs;
66 }
67 
68 static inline unsigned bch2_bkey_ptrs_need_move(struct bch_fs *c,
69 				       struct bch_io_opts *opts,
70 				       struct bkey_ptrs_c ptrs)
71 {
72 	if (!opts->background_target ||
73 	    !bch2_target_accepts_data(c, BCH_DATA_user, opts->background_target))
74 		return 0;
75 
76 	unsigned ptr_bit = 1;
77 	unsigned rewrite_ptrs = 0;
78 
79 	bkey_for_each_ptr(ptrs, ptr) {
80 		if (!ptr->cached && !bch2_dev_in_target(c, ptr->dev, opts->background_target))
81 			rewrite_ptrs |= ptr_bit;
82 		ptr_bit <<= 1;
83 	}
84 
85 	return rewrite_ptrs;
86 }
87 
88 static unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c,
89 					      struct bch_io_opts *opts,
90 					      struct bkey_s_c k)
91 {
92 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
93 
94 	return bch2_bkey_ptrs_need_compress(c, opts, k, ptrs) |
95 		bch2_bkey_ptrs_need_move(c, opts, ptrs);
96 }
97 
98 u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k)
99 {
100 	const struct bch_extent_rebalance *opts = bch2_bkey_rebalance_opts(k);
101 	if (!opts)
102 		return 0;
103 
104 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
105 	const union bch_extent_entry *entry;
106 	struct extent_ptr_decoded p;
107 	u64 sectors = 0;
108 
109 	if (opts->background_compression) {
110 		unsigned compression_type = bch2_compression_opt_to_type(opts->background_compression);
111 
112 		bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
113 			if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible ||
114 			    p.ptr.unwritten) {
115 				sectors = 0;
116 				goto incompressible;
117 			}
118 
119 			if (!p.ptr.cached && p.crc.compression_type != compression_type)
120 				sectors += p.crc.compressed_size;
121 		}
122 	}
123 incompressible:
124 	if (opts->background_target &&
125 	    bch2_target_accepts_data(c, BCH_DATA_user, opts->background_target)) {
126 		bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
127 			if (!p.ptr.cached && !bch2_dev_in_target(c, p.ptr.dev, opts->background_target))
128 				sectors += p.crc.compressed_size;
129 	}
130 
131 	return sectors;
132 }
133 
134 static bool bch2_bkey_rebalance_needs_update(struct bch_fs *c, struct bch_io_opts *opts,
135 					     struct bkey_s_c k)
136 {
137 	if (!bkey_extent_is_direct_data(k.k))
138 		return 0;
139 
140 	const struct bch_extent_rebalance *old = bch2_bkey_rebalance_opts(k);
141 
142 	if (k.k->type == KEY_TYPE_reflink_v || bch2_bkey_ptrs_need_rebalance(c, opts, k)) {
143 		struct bch_extent_rebalance new = io_opts_to_rebalance_opts(opts);
144 		return old == NULL || memcmp(old, &new, sizeof(new));
145 	} else {
146 		return old != NULL;
147 	}
148 }
149 
150 int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bch_io_opts *opts,
151 				  struct bkey_i *_k)
152 {
153 	if (!bkey_extent_is_direct_data(&_k->k))
154 		return 0;
155 
156 	struct bkey_s k = bkey_i_to_s(_k);
157 	struct bch_extent_rebalance *old =
158 		(struct bch_extent_rebalance *) bch2_bkey_rebalance_opts(k.s_c);
159 
160 	if (k.k->type == KEY_TYPE_reflink_v || bch2_bkey_ptrs_need_rebalance(c, opts, k.s_c)) {
161 		if (!old) {
162 			old = bkey_val_end(k);
163 			k.k->u64s += sizeof(*old) / sizeof(u64);
164 		}
165 
166 		*old = io_opts_to_rebalance_opts(opts);
167 	} else {
168 		if (old)
169 			extent_entry_drop(k, (union bch_extent_entry *) old);
170 	}
171 
172 	return 0;
173 }
174 
175 int bch2_get_update_rebalance_opts(struct btree_trans *trans,
176 				   struct bch_io_opts *io_opts,
177 				   struct btree_iter *iter,
178 				   struct bkey_s_c k)
179 {
180 	BUG_ON(iter->flags & BTREE_ITER_is_extents);
181 	BUG_ON(iter->flags & BTREE_ITER_filter_snapshots);
182 
183 	const struct bch_extent_rebalance *r = k.k->type == KEY_TYPE_reflink_v
184 		? bch2_bkey_rebalance_opts(k) : NULL;
185 	if (r) {
186 #define x(_name)							\
187 		if (r->_name##_from_inode) {				\
188 			io_opts->_name = r->_name;			\
189 			io_opts->_name##_from_inode = true;		\
190 		}
191 		BCH_REBALANCE_OPTS()
192 #undef x
193 	}
194 
195 	if (!bch2_bkey_rebalance_needs_update(trans->c, io_opts, k))
196 		return 0;
197 
198 	struct bkey_i *n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + 8);
199 	int ret = PTR_ERR_OR_ZERO(n);
200 	if (ret)
201 		return ret;
202 
203 	bkey_reassemble(n, k);
204 
205 	/* On successfull transaction commit, @k was invalidated: */
206 
207 	return bch2_bkey_set_needs_rebalance(trans->c, io_opts, n) ?:
208 		bch2_trans_update(trans, iter, n, BTREE_UPDATE_internal_snapshot_node) ?:
209 		bch2_trans_commit(trans, NULL, NULL, 0) ?:
210 		-BCH_ERR_transaction_restart_nested;
211 }
212 
213 #define REBALANCE_WORK_SCAN_OFFSET	(U64_MAX - 1)
214 
215 static const char * const bch2_rebalance_state_strs[] = {
216 #define x(t) #t,
217 	BCH_REBALANCE_STATES()
218 	NULL
219 #undef x
220 };
221 
222 int bch2_set_rebalance_needs_scan_trans(struct btree_trans *trans, u64 inum)
223 {
224 	struct btree_iter iter;
225 	struct bkey_s_c k;
226 	struct bkey_i_cookie *cookie;
227 	u64 v;
228 	int ret;
229 
230 	bch2_trans_iter_init(trans, &iter, BTREE_ID_rebalance_work,
231 			     SPOS(inum, REBALANCE_WORK_SCAN_OFFSET, U32_MAX),
232 			     BTREE_ITER_intent);
233 	k = bch2_btree_iter_peek_slot(&iter);
234 	ret = bkey_err(k);
235 	if (ret)
236 		goto err;
237 
238 	v = k.k->type == KEY_TYPE_cookie
239 		? le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie)
240 		: 0;
241 
242 	cookie = bch2_trans_kmalloc(trans, sizeof(*cookie));
243 	ret = PTR_ERR_OR_ZERO(cookie);
244 	if (ret)
245 		goto err;
246 
247 	bkey_cookie_init(&cookie->k_i);
248 	cookie->k.p = iter.pos;
249 	cookie->v.cookie = cpu_to_le64(v + 1);
250 
251 	ret = bch2_trans_update(trans, &iter, &cookie->k_i, 0);
252 err:
253 	bch2_trans_iter_exit(trans, &iter);
254 	return ret;
255 }
256 
257 int bch2_set_rebalance_needs_scan(struct bch_fs *c, u64 inum)
258 {
259 	int ret = bch2_trans_commit_do(c, NULL, NULL,
260 				       BCH_TRANS_COMMIT_no_enospc,
261 			    bch2_set_rebalance_needs_scan_trans(trans, inum));
262 	rebalance_wakeup(c);
263 	return ret;
264 }
265 
266 int bch2_set_fs_needs_rebalance(struct bch_fs *c)
267 {
268 	return bch2_set_rebalance_needs_scan(c, 0);
269 }
270 
271 static int bch2_clear_rebalance_needs_scan(struct btree_trans *trans, u64 inum, u64 cookie)
272 {
273 	struct btree_iter iter;
274 	struct bkey_s_c k;
275 	u64 v;
276 	int ret;
277 
278 	bch2_trans_iter_init(trans, &iter, BTREE_ID_rebalance_work,
279 			     SPOS(inum, REBALANCE_WORK_SCAN_OFFSET, U32_MAX),
280 			     BTREE_ITER_intent);
281 	k = bch2_btree_iter_peek_slot(&iter);
282 	ret = bkey_err(k);
283 	if (ret)
284 		goto err;
285 
286 	v = k.k->type == KEY_TYPE_cookie
287 		? le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie)
288 		: 0;
289 
290 	if (v == cookie)
291 		ret = bch2_btree_delete_at(trans, &iter, 0);
292 err:
293 	bch2_trans_iter_exit(trans, &iter);
294 	return ret;
295 }
296 
297 static struct bkey_s_c next_rebalance_entry(struct btree_trans *trans,
298 					    struct btree_iter *work_iter)
299 {
300 	return !kthread_should_stop()
301 		? bch2_btree_iter_peek(work_iter)
302 		: bkey_s_c_null;
303 }
304 
305 static int bch2_bkey_clear_needs_rebalance(struct btree_trans *trans,
306 					   struct btree_iter *iter,
307 					   struct bkey_s_c k)
308 {
309 	if (!bch2_bkey_rebalance_opts(k))
310 		return 0;
311 
312 	struct bkey_i *n = bch2_bkey_make_mut(trans, iter, &k, 0);
313 	int ret = PTR_ERR_OR_ZERO(n);
314 	if (ret)
315 		return ret;
316 
317 	extent_entry_drop(bkey_i_to_s(n),
318 			  (void *) bch2_bkey_rebalance_opts(bkey_i_to_s_c(n)));
319 	return bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
320 }
321 
322 static struct bkey_s_c next_rebalance_extent(struct btree_trans *trans,
323 			struct bpos work_pos,
324 			struct btree_iter *extent_iter,
325 			struct bch_io_opts *io_opts,
326 			struct data_update_opts *data_opts)
327 {
328 	struct bch_fs *c = trans->c;
329 
330 	bch2_trans_iter_exit(trans, extent_iter);
331 	bch2_trans_iter_init(trans, extent_iter,
332 			     work_pos.inode ? BTREE_ID_extents : BTREE_ID_reflink,
333 			     work_pos,
334 			     BTREE_ITER_all_snapshots);
335 	struct bkey_s_c k = bch2_btree_iter_peek_slot(extent_iter);
336 	if (bkey_err(k))
337 		return k;
338 
339 	int ret = bch2_move_get_io_opts_one(trans, io_opts, extent_iter, k);
340 	if (ret)
341 		return bkey_s_c_err(ret);
342 
343 	memset(data_opts, 0, sizeof(*data_opts));
344 	data_opts->rewrite_ptrs		= bch2_bkey_ptrs_need_rebalance(c, io_opts, k);
345 	data_opts->target		= io_opts->background_target;
346 	data_opts->write_flags		|= BCH_WRITE_ONLY_SPECIFIED_DEVS;
347 
348 	if (!data_opts->rewrite_ptrs) {
349 		/*
350 		 * device we would want to write to offline? devices in target
351 		 * changed?
352 		 *
353 		 * We'll now need a full scan before this extent is picked up
354 		 * again:
355 		 */
356 		int ret = bch2_bkey_clear_needs_rebalance(trans, extent_iter, k);
357 		if (ret)
358 			return bkey_s_c_err(ret);
359 		return bkey_s_c_null;
360 	}
361 
362 	if (trace_rebalance_extent_enabled()) {
363 		struct printbuf buf = PRINTBUF;
364 
365 		bch2_bkey_val_to_text(&buf, c, k);
366 		prt_newline(&buf);
367 
368 		struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
369 
370 		unsigned p = bch2_bkey_ptrs_need_compress(c, io_opts, k, ptrs);
371 		if (p) {
372 			prt_str(&buf, "compression=");
373 			bch2_compression_opt_to_text(&buf, io_opts->background_compression);
374 			prt_str(&buf, " ");
375 			bch2_prt_u64_base2(&buf, p);
376 			prt_newline(&buf);
377 		}
378 
379 		p = bch2_bkey_ptrs_need_move(c, io_opts, ptrs);
380 		if (p) {
381 			prt_str(&buf, "move=");
382 			bch2_target_to_text(&buf, c, io_opts->background_target);
383 			prt_str(&buf, " ");
384 			bch2_prt_u64_base2(&buf, p);
385 			prt_newline(&buf);
386 		}
387 
388 		trace_rebalance_extent(c, buf.buf);
389 		printbuf_exit(&buf);
390 	}
391 
392 	return k;
393 }
394 
395 noinline_for_stack
396 static int do_rebalance_extent(struct moving_context *ctxt,
397 			       struct bpos work_pos,
398 			       struct btree_iter *extent_iter)
399 {
400 	struct btree_trans *trans = ctxt->trans;
401 	struct bch_fs *c = trans->c;
402 	struct bch_fs_rebalance *r = &trans->c->rebalance;
403 	struct data_update_opts data_opts;
404 	struct bch_io_opts io_opts;
405 	struct bkey_s_c k;
406 	struct bkey_buf sk;
407 	int ret;
408 
409 	ctxt->stats = &r->work_stats;
410 	r->state = BCH_REBALANCE_working;
411 
412 	bch2_bkey_buf_init(&sk);
413 
414 	ret = bkey_err(k = next_rebalance_extent(trans, work_pos,
415 				extent_iter, &io_opts, &data_opts));
416 	if (ret || !k.k)
417 		goto out;
418 
419 	atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
420 
421 	/*
422 	 * The iterator gets unlocked by __bch2_read_extent - need to
423 	 * save a copy of @k elsewhere:
424 	 */
425 	bch2_bkey_buf_reassemble(&sk, c, k);
426 	k = bkey_i_to_s_c(sk.k);
427 
428 	ret = bch2_move_extent(ctxt, NULL, extent_iter, k, io_opts, data_opts);
429 	if (ret) {
430 		if (bch2_err_matches(ret, ENOMEM)) {
431 			/* memory allocation failure, wait for some IO to finish */
432 			bch2_move_ctxt_wait_for_io(ctxt);
433 			ret = -BCH_ERR_transaction_restart_nested;
434 		}
435 
436 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
437 			goto out;
438 
439 		/* skip it and continue, XXX signal failure */
440 		ret = 0;
441 	}
442 out:
443 	bch2_bkey_buf_exit(&sk, c);
444 	return ret;
445 }
446 
447 static bool rebalance_pred(struct bch_fs *c, void *arg,
448 			   struct bkey_s_c k,
449 			   struct bch_io_opts *io_opts,
450 			   struct data_update_opts *data_opts)
451 {
452 	data_opts->rewrite_ptrs		= bch2_bkey_ptrs_need_rebalance(c, io_opts, k);
453 	data_opts->target		= io_opts->background_target;
454 	data_opts->write_flags		|= BCH_WRITE_ONLY_SPECIFIED_DEVS;
455 	return data_opts->rewrite_ptrs != 0;
456 }
457 
458 static int do_rebalance_scan(struct moving_context *ctxt, u64 inum, u64 cookie)
459 {
460 	struct btree_trans *trans = ctxt->trans;
461 	struct bch_fs_rebalance *r = &trans->c->rebalance;
462 	int ret;
463 
464 	bch2_move_stats_init(&r->scan_stats, "rebalance_scan");
465 	ctxt->stats = &r->scan_stats;
466 
467 	if (!inum) {
468 		r->scan_start	= BBPOS_MIN;
469 		r->scan_end	= BBPOS_MAX;
470 	} else {
471 		r->scan_start	= BBPOS(BTREE_ID_extents, POS(inum, 0));
472 		r->scan_end	= BBPOS(BTREE_ID_extents, POS(inum, U64_MAX));
473 	}
474 
475 	r->state = BCH_REBALANCE_scanning;
476 
477 	ret = __bch2_move_data(ctxt, r->scan_start, r->scan_end, rebalance_pred, NULL) ?:
478 		commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
479 			  bch2_clear_rebalance_needs_scan(trans, inum, cookie));
480 
481 	bch2_move_stats_exit(&r->scan_stats, trans->c);
482 	return ret;
483 }
484 
485 static void rebalance_wait(struct bch_fs *c)
486 {
487 	struct bch_fs_rebalance *r = &c->rebalance;
488 	struct io_clock *clock = &c->io_clock[WRITE];
489 	u64 now = atomic64_read(&clock->now);
490 	u64 min_member_capacity = bch2_min_rw_member_capacity(c);
491 
492 	if (min_member_capacity == U64_MAX)
493 		min_member_capacity = 128 * 2048;
494 
495 	r->wait_iotime_end		= now + (min_member_capacity >> 6);
496 
497 	if (r->state != BCH_REBALANCE_waiting) {
498 		r->wait_iotime_start	= now;
499 		r->wait_wallclock_start	= ktime_get_real_ns();
500 		r->state		= BCH_REBALANCE_waiting;
501 	}
502 
503 	bch2_kthread_io_clock_wait(clock, r->wait_iotime_end, MAX_SCHEDULE_TIMEOUT);
504 }
505 
506 static int do_rebalance(struct moving_context *ctxt)
507 {
508 	struct btree_trans *trans = ctxt->trans;
509 	struct bch_fs *c = trans->c;
510 	struct bch_fs_rebalance *r = &c->rebalance;
511 	struct btree_iter rebalance_work_iter, extent_iter = { NULL };
512 	struct bkey_s_c k;
513 	int ret = 0;
514 
515 	bch2_trans_begin(trans);
516 
517 	bch2_move_stats_init(&r->work_stats, "rebalance_work");
518 	bch2_move_stats_init(&r->scan_stats, "rebalance_scan");
519 
520 	bch2_trans_iter_init(trans, &rebalance_work_iter,
521 			     BTREE_ID_rebalance_work, POS_MIN,
522 			     BTREE_ITER_all_snapshots);
523 
524 	while (!bch2_move_ratelimit(ctxt)) {
525 		if (!c->opts.rebalance_enabled) {
526 			bch2_moving_ctxt_flush_all(ctxt);
527 			kthread_wait_freezable(c->opts.rebalance_enabled ||
528 					       kthread_should_stop());
529 		}
530 
531 		if (kthread_should_stop())
532 			break;
533 
534 		bch2_trans_begin(trans);
535 
536 		ret = bkey_err(k = next_rebalance_entry(trans, &rebalance_work_iter));
537 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
538 			continue;
539 		if (ret || !k.k)
540 			break;
541 
542 		ret = k.k->type == KEY_TYPE_cookie
543 			? do_rebalance_scan(ctxt, k.k->p.inode,
544 					    le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie))
545 			: do_rebalance_extent(ctxt, k.k->p, &extent_iter);
546 
547 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
548 			continue;
549 		if (ret)
550 			break;
551 
552 		bch2_btree_iter_advance(&rebalance_work_iter);
553 	}
554 
555 	bch2_trans_iter_exit(trans, &extent_iter);
556 	bch2_trans_iter_exit(trans, &rebalance_work_iter);
557 	bch2_move_stats_exit(&r->scan_stats, c);
558 
559 	if (!ret &&
560 	    !kthread_should_stop() &&
561 	    !atomic64_read(&r->work_stats.sectors_seen) &&
562 	    !atomic64_read(&r->scan_stats.sectors_seen)) {
563 		bch2_moving_ctxt_flush_all(ctxt);
564 		bch2_trans_unlock_long(trans);
565 		rebalance_wait(c);
566 	}
567 
568 	if (!bch2_err_matches(ret, EROFS))
569 		bch_err_fn(c, ret);
570 	return ret;
571 }
572 
573 static int bch2_rebalance_thread(void *arg)
574 {
575 	struct bch_fs *c = arg;
576 	struct bch_fs_rebalance *r = &c->rebalance;
577 	struct moving_context ctxt;
578 
579 	set_freezable();
580 
581 	bch2_moving_ctxt_init(&ctxt, c, NULL, &r->work_stats,
582 			      writepoint_ptr(&c->rebalance_write_point),
583 			      true);
584 
585 	while (!kthread_should_stop() && !do_rebalance(&ctxt))
586 		;
587 
588 	bch2_moving_ctxt_exit(&ctxt);
589 
590 	return 0;
591 }
592 
593 void bch2_rebalance_status_to_text(struct printbuf *out, struct bch_fs *c)
594 {
595 	struct bch_fs_rebalance *r = &c->rebalance;
596 
597 	prt_str(out, bch2_rebalance_state_strs[r->state]);
598 	prt_newline(out);
599 	printbuf_indent_add(out, 2);
600 
601 	switch (r->state) {
602 	case BCH_REBALANCE_waiting: {
603 		u64 now = atomic64_read(&c->io_clock[WRITE].now);
604 
605 		prt_str(out, "io wait duration:  ");
606 		bch2_prt_human_readable_s64(out, (r->wait_iotime_end - r->wait_iotime_start) << 9);
607 		prt_newline(out);
608 
609 		prt_str(out, "io wait remaining: ");
610 		bch2_prt_human_readable_s64(out, (r->wait_iotime_end - now) << 9);
611 		prt_newline(out);
612 
613 		prt_str(out, "duration waited:   ");
614 		bch2_pr_time_units(out, ktime_get_real_ns() - r->wait_wallclock_start);
615 		prt_newline(out);
616 		break;
617 	}
618 	case BCH_REBALANCE_working:
619 		bch2_move_stats_to_text(out, &r->work_stats);
620 		break;
621 	case BCH_REBALANCE_scanning:
622 		bch2_move_stats_to_text(out, &r->scan_stats);
623 		break;
624 	}
625 	prt_newline(out);
626 	printbuf_indent_sub(out, 2);
627 }
628 
629 void bch2_rebalance_stop(struct bch_fs *c)
630 {
631 	struct task_struct *p;
632 
633 	c->rebalance.pd.rate.rate = UINT_MAX;
634 	bch2_ratelimit_reset(&c->rebalance.pd.rate);
635 
636 	p = rcu_dereference_protected(c->rebalance.thread, 1);
637 	c->rebalance.thread = NULL;
638 
639 	if (p) {
640 		/* for sychronizing with rebalance_wakeup() */
641 		synchronize_rcu();
642 
643 		kthread_stop(p);
644 		put_task_struct(p);
645 	}
646 }
647 
648 int bch2_rebalance_start(struct bch_fs *c)
649 {
650 	struct task_struct *p;
651 	int ret;
652 
653 	if (c->rebalance.thread)
654 		return 0;
655 
656 	if (c->opts.nochanges)
657 		return 0;
658 
659 	p = kthread_create(bch2_rebalance_thread, c, "bch-rebalance/%s", c->name);
660 	ret = PTR_ERR_OR_ZERO(p);
661 	bch_err_msg(c, ret, "creating rebalance thread");
662 	if (ret)
663 		return ret;
664 
665 	get_task_struct(p);
666 	rcu_assign_pointer(c->rebalance.thread, p);
667 	wake_up_process(p);
668 	return 0;
669 }
670 
671 void bch2_fs_rebalance_init(struct bch_fs *c)
672 {
673 	bch2_pd_controller_init(&c->rebalance.pd);
674 }
675