xref: /linux/fs/bcachefs/rebalance.c (revision d642ef71)
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 "move.h"
17 #include "rebalance.h"
18 #include "subvolume.h"
19 #include "super-io.h"
20 #include "trace.h"
21 
22 #include <linux/freezer.h>
23 #include <linux/kthread.h>
24 #include <linux/sched/cputime.h>
25 
26 #define REBALANCE_WORK_SCAN_OFFSET	(U64_MAX - 1)
27 
28 static const char * const bch2_rebalance_state_strs[] = {
29 #define x(t) #t,
30 	BCH_REBALANCE_STATES()
31 	NULL
32 #undef x
33 };
34 
35 static int __bch2_set_rebalance_needs_scan(struct btree_trans *trans, u64 inum)
36 {
37 	struct btree_iter iter;
38 	struct bkey_s_c k;
39 	struct bkey_i_cookie *cookie;
40 	u64 v;
41 	int ret;
42 
43 	bch2_trans_iter_init(trans, &iter, BTREE_ID_rebalance_work,
44 			     SPOS(inum, REBALANCE_WORK_SCAN_OFFSET, U32_MAX),
45 			     BTREE_ITER_INTENT);
46 	k = bch2_btree_iter_peek_slot(&iter);
47 	ret = bkey_err(k);
48 	if (ret)
49 		goto err;
50 
51 	v = k.k->type == KEY_TYPE_cookie
52 		? le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie)
53 		: 0;
54 
55 	cookie = bch2_trans_kmalloc(trans, sizeof(*cookie));
56 	ret = PTR_ERR_OR_ZERO(cookie);
57 	if (ret)
58 		goto err;
59 
60 	bkey_cookie_init(&cookie->k_i);
61 	cookie->k.p = iter.pos;
62 	cookie->v.cookie = cpu_to_le64(v + 1);
63 
64 	ret = bch2_trans_update(trans, &iter, &cookie->k_i, 0);
65 err:
66 	bch2_trans_iter_exit(trans, &iter);
67 	return ret;
68 }
69 
70 int bch2_set_rebalance_needs_scan(struct bch_fs *c, u64 inum)
71 {
72 	int ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
73 			    __bch2_set_rebalance_needs_scan(trans, inum));
74 	rebalance_wakeup(c);
75 	return ret;
76 }
77 
78 int bch2_set_fs_needs_rebalance(struct bch_fs *c)
79 {
80 	return bch2_set_rebalance_needs_scan(c, 0);
81 }
82 
83 static int bch2_clear_rebalance_needs_scan(struct btree_trans *trans, u64 inum, u64 cookie)
84 {
85 	struct btree_iter iter;
86 	struct bkey_s_c k;
87 	u64 v;
88 	int ret;
89 
90 	bch2_trans_iter_init(trans, &iter, BTREE_ID_rebalance_work,
91 			     SPOS(inum, REBALANCE_WORK_SCAN_OFFSET, U32_MAX),
92 			     BTREE_ITER_INTENT);
93 	k = bch2_btree_iter_peek_slot(&iter);
94 	ret = bkey_err(k);
95 	if (ret)
96 		goto err;
97 
98 	v = k.k->type == KEY_TYPE_cookie
99 		? le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie)
100 		: 0;
101 
102 	if (v == cookie)
103 		ret = bch2_btree_delete_at(trans, &iter, 0);
104 err:
105 	bch2_trans_iter_exit(trans, &iter);
106 	return ret;
107 }
108 
109 static struct bkey_s_c next_rebalance_entry(struct btree_trans *trans,
110 					    struct btree_iter *work_iter)
111 {
112 	return !kthread_should_stop()
113 		? bch2_btree_iter_peek(work_iter)
114 		: bkey_s_c_null;
115 }
116 
117 static int bch2_bkey_clear_needs_rebalance(struct btree_trans *trans,
118 					   struct btree_iter *iter,
119 					   struct bkey_s_c k)
120 {
121 	struct bkey_i *n = bch2_bkey_make_mut(trans, iter, &k, 0);
122 	int ret = PTR_ERR_OR_ZERO(n);
123 	if (ret)
124 		return ret;
125 
126 	extent_entry_drop(bkey_i_to_s(n),
127 			  (void *) bch2_bkey_rebalance_opts(bkey_i_to_s_c(n)));
128 	return bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
129 }
130 
131 static struct bkey_s_c next_rebalance_extent(struct btree_trans *trans,
132 			struct bpos work_pos,
133 			struct btree_iter *extent_iter,
134 			struct data_update_opts *data_opts)
135 {
136 	struct bch_fs *c = trans->c;
137 	struct bkey_s_c k;
138 
139 	bch2_trans_iter_exit(trans, extent_iter);
140 	bch2_trans_iter_init(trans, extent_iter,
141 			     work_pos.inode ? BTREE_ID_extents : BTREE_ID_reflink,
142 			     work_pos,
143 			     BTREE_ITER_ALL_SNAPSHOTS);
144 	k = bch2_btree_iter_peek_slot(extent_iter);
145 	if (bkey_err(k))
146 		return k;
147 
148 	const struct bch_extent_rebalance *r = k.k ? bch2_bkey_rebalance_opts(k) : NULL;
149 	if (!r) {
150 		/* raced due to btree write buffer, nothing to do */
151 		return bkey_s_c_null;
152 	}
153 
154 	memset(data_opts, 0, sizeof(*data_opts));
155 
156 	data_opts->rewrite_ptrs		=
157 		bch2_bkey_ptrs_need_rebalance(c, k, r->target, r->compression);
158 	data_opts->target		= r->target;
159 
160 	if (!data_opts->rewrite_ptrs) {
161 		/*
162 		 * device we would want to write to offline? devices in target
163 		 * changed?
164 		 *
165 		 * We'll now need a full scan before this extent is picked up
166 		 * again:
167 		 */
168 		int ret = bch2_bkey_clear_needs_rebalance(trans, extent_iter, k);
169 		if (ret)
170 			return bkey_s_c_err(ret);
171 		return bkey_s_c_null;
172 	}
173 
174 	return k;
175 }
176 
177 noinline_for_stack
178 static int do_rebalance_extent(struct moving_context *ctxt,
179 			       struct bpos work_pos,
180 			       struct btree_iter *extent_iter)
181 {
182 	struct btree_trans *trans = ctxt->trans;
183 	struct bch_fs *c = trans->c;
184 	struct bch_fs_rebalance *r = &trans->c->rebalance;
185 	struct data_update_opts data_opts;
186 	struct bch_io_opts io_opts;
187 	struct bkey_s_c k;
188 	struct bkey_buf sk;
189 	int ret;
190 
191 	ctxt->stats = &r->work_stats;
192 	r->state = BCH_REBALANCE_working;
193 
194 	bch2_bkey_buf_init(&sk);
195 
196 	ret = bkey_err(k = next_rebalance_extent(trans, work_pos,
197 						 extent_iter, &data_opts));
198 	if (ret || !k.k)
199 		goto out;
200 
201 	ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
202 	if (ret)
203 		goto out;
204 
205 	atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
206 
207 	/*
208 	 * The iterator gets unlocked by __bch2_read_extent - need to
209 	 * save a copy of @k elsewhere:
210 	 */
211 	bch2_bkey_buf_reassemble(&sk, c, k);
212 	k = bkey_i_to_s_c(sk.k);
213 
214 	ret = bch2_move_extent(ctxt, NULL, extent_iter, k, io_opts, data_opts);
215 	if (ret) {
216 		if (bch2_err_matches(ret, ENOMEM)) {
217 			/* memory allocation failure, wait for some IO to finish */
218 			bch2_move_ctxt_wait_for_io(ctxt);
219 			ret = -BCH_ERR_transaction_restart_nested;
220 		}
221 
222 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
223 			goto out;
224 
225 		/* skip it and continue, XXX signal failure */
226 		ret = 0;
227 	}
228 out:
229 	bch2_bkey_buf_exit(&sk, c);
230 	return ret;
231 }
232 
233 static bool rebalance_pred(struct bch_fs *c, void *arg,
234 			   struct bkey_s_c k,
235 			   struct bch_io_opts *io_opts,
236 			   struct data_update_opts *data_opts)
237 {
238 	unsigned target, compression;
239 
240 	if (k.k->p.inode) {
241 		target		= io_opts->background_target;
242 		compression	= io_opts->background_compression ?: io_opts->compression;
243 	} else {
244 		const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k);
245 
246 		target		= r ? r->target : io_opts->background_target;
247 		compression	= r ? r->compression :
248 			(io_opts->background_compression ?: io_opts->compression);
249 	}
250 
251 	data_opts->rewrite_ptrs		= bch2_bkey_ptrs_need_rebalance(c, k, target, compression);
252 	data_opts->target		= target;
253 	return data_opts->rewrite_ptrs != 0;
254 }
255 
256 static int do_rebalance_scan(struct moving_context *ctxt, u64 inum, u64 cookie)
257 {
258 	struct btree_trans *trans = ctxt->trans;
259 	struct bch_fs_rebalance *r = &trans->c->rebalance;
260 	int ret;
261 
262 	bch2_move_stats_init(&r->scan_stats, "rebalance_scan");
263 	ctxt->stats = &r->scan_stats;
264 
265 	if (!inum) {
266 		r->scan_start	= BBPOS_MIN;
267 		r->scan_end	= BBPOS_MAX;
268 	} else {
269 		r->scan_start	= BBPOS(BTREE_ID_extents, POS(inum, 0));
270 		r->scan_end	= BBPOS(BTREE_ID_extents, POS(inum, U64_MAX));
271 	}
272 
273 	r->state = BCH_REBALANCE_scanning;
274 
275 	ret = __bch2_move_data(ctxt, r->scan_start, r->scan_end, rebalance_pred, NULL) ?:
276 		commit_do(trans, NULL, NULL, BTREE_INSERT_NOFAIL,
277 			  bch2_clear_rebalance_needs_scan(trans, inum, cookie));
278 
279 	bch2_move_stats_exit(&r->scan_stats, trans->c);
280 	return ret;
281 }
282 
283 static void rebalance_wait(struct bch_fs *c)
284 {
285 	struct bch_fs_rebalance *r = &c->rebalance;
286 	struct io_clock *clock = &c->io_clock[WRITE];
287 	u64 now = atomic64_read(&clock->now);
288 	u64 min_member_capacity = bch2_min_rw_member_capacity(c);
289 
290 	if (min_member_capacity == U64_MAX)
291 		min_member_capacity = 128 * 2048;
292 
293 	r->wait_iotime_end		= now + (min_member_capacity >> 6);
294 
295 	if (r->state != BCH_REBALANCE_waiting) {
296 		r->wait_iotime_start	= now;
297 		r->wait_wallclock_start	= ktime_get_real_ns();
298 		r->state		= BCH_REBALANCE_waiting;
299 	}
300 
301 	bch2_kthread_io_clock_wait(clock, r->wait_iotime_end, MAX_SCHEDULE_TIMEOUT);
302 }
303 
304 static int do_rebalance(struct moving_context *ctxt)
305 {
306 	struct btree_trans *trans = ctxt->trans;
307 	struct bch_fs *c = trans->c;
308 	struct bch_fs_rebalance *r = &c->rebalance;
309 	struct btree_iter rebalance_work_iter, extent_iter = { NULL };
310 	struct bkey_s_c k;
311 	int ret = 0;
312 
313 	bch2_move_stats_init(&r->work_stats, "rebalance_work");
314 	bch2_move_stats_init(&r->scan_stats, "rebalance_scan");
315 
316 	bch2_trans_iter_init(trans, &rebalance_work_iter,
317 			     BTREE_ID_rebalance_work, POS_MIN,
318 			     BTREE_ITER_ALL_SNAPSHOTS);
319 
320 	while (!bch2_move_ratelimit(ctxt) &&
321 	       !kthread_wait_freezable(r->enabled)) {
322 		bch2_trans_begin(trans);
323 
324 		ret = bkey_err(k = next_rebalance_entry(trans, &rebalance_work_iter));
325 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
326 			continue;
327 		if (ret || !k.k)
328 			break;
329 
330 		ret = k.k->type == KEY_TYPE_cookie
331 			? do_rebalance_scan(ctxt, k.k->p.inode,
332 					    le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie))
333 			: do_rebalance_extent(ctxt, k.k->p, &extent_iter);
334 
335 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
336 			continue;
337 		if (ret)
338 			break;
339 
340 		bch2_btree_iter_advance(&rebalance_work_iter);
341 	}
342 
343 	bch2_trans_iter_exit(trans, &extent_iter);
344 	bch2_trans_iter_exit(trans, &rebalance_work_iter);
345 	bch2_move_stats_exit(&r->scan_stats, c);
346 
347 	if (!ret &&
348 	    !kthread_should_stop() &&
349 	    !atomic64_read(&r->work_stats.sectors_seen) &&
350 	    !atomic64_read(&r->scan_stats.sectors_seen)) {
351 		bch2_trans_unlock_long(trans);
352 		rebalance_wait(c);
353 	}
354 
355 	if (!bch2_err_matches(ret, EROFS))
356 		bch_err_fn(c, ret);
357 	return ret;
358 }
359 
360 static int bch2_rebalance_thread(void *arg)
361 {
362 	struct bch_fs *c = arg;
363 	struct bch_fs_rebalance *r = &c->rebalance;
364 	struct moving_context ctxt;
365 	int ret;
366 
367 	set_freezable();
368 
369 	bch2_moving_ctxt_init(&ctxt, c, NULL, &r->work_stats,
370 			      writepoint_ptr(&c->rebalance_write_point),
371 			      true);
372 
373 	while (!kthread_should_stop() &&
374 	       !(ret = do_rebalance(&ctxt)))
375 		;
376 
377 	bch2_moving_ctxt_exit(&ctxt);
378 
379 	return 0;
380 }
381 
382 void bch2_rebalance_status_to_text(struct printbuf *out, struct bch_fs *c)
383 {
384 	struct bch_fs_rebalance *r = &c->rebalance;
385 
386 	prt_str(out, bch2_rebalance_state_strs[r->state]);
387 	prt_newline(out);
388 	printbuf_indent_add(out, 2);
389 
390 	switch (r->state) {
391 	case BCH_REBALANCE_waiting: {
392 		u64 now = atomic64_read(&c->io_clock[WRITE].now);
393 
394 		prt_str(out, "io wait duration:  ");
395 		bch2_prt_human_readable_s64(out, r->wait_iotime_end - r->wait_iotime_start);
396 		prt_newline(out);
397 
398 		prt_str(out, "io wait remaining: ");
399 		bch2_prt_human_readable_s64(out, r->wait_iotime_end - now);
400 		prt_newline(out);
401 
402 		prt_str(out, "duration waited:   ");
403 		bch2_pr_time_units(out, ktime_get_real_ns() - r->wait_wallclock_start);
404 		prt_newline(out);
405 		break;
406 	}
407 	case BCH_REBALANCE_working:
408 		bch2_move_stats_to_text(out, &r->work_stats);
409 		break;
410 	case BCH_REBALANCE_scanning:
411 		bch2_move_stats_to_text(out, &r->scan_stats);
412 		break;
413 	}
414 	prt_newline(out);
415 	printbuf_indent_sub(out, 2);
416 }
417 
418 void bch2_rebalance_stop(struct bch_fs *c)
419 {
420 	struct task_struct *p;
421 
422 	c->rebalance.pd.rate.rate = UINT_MAX;
423 	bch2_ratelimit_reset(&c->rebalance.pd.rate);
424 
425 	p = rcu_dereference_protected(c->rebalance.thread, 1);
426 	c->rebalance.thread = NULL;
427 
428 	if (p) {
429 		/* for sychronizing with rebalance_wakeup() */
430 		synchronize_rcu();
431 
432 		kthread_stop(p);
433 		put_task_struct(p);
434 	}
435 }
436 
437 int bch2_rebalance_start(struct bch_fs *c)
438 {
439 	struct task_struct *p;
440 	int ret;
441 
442 	if (c->rebalance.thread)
443 		return 0;
444 
445 	if (c->opts.nochanges)
446 		return 0;
447 
448 	p = kthread_create(bch2_rebalance_thread, c, "bch-rebalance/%s", c->name);
449 	ret = PTR_ERR_OR_ZERO(p);
450 	if (ret) {
451 		bch_err_msg(c, ret, "creating rebalance thread");
452 		return ret;
453 	}
454 
455 	get_task_struct(p);
456 	rcu_assign_pointer(c->rebalance.thread, p);
457 	wake_up_process(p);
458 	return 0;
459 }
460 
461 void bch2_fs_rebalance_init(struct bch_fs *c)
462 {
463 	bch2_pd_controller_init(&c->rebalance.pd);
464 }
465