summaryrefslogtreecommitdiff
path: root/libbcachefs/migrate.c
blob: 01c8896078f2c027849cc46e69b044bcd89f1281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Code for moving data off a device.
 */

#include "bcachefs.h"
#include "btree_update.h"
#include "buckets.h"
#include "extents.h"
#include "io.h"
#include "journal.h"
#include "keylist.h"
#include "migrate.h"
#include "move.h"
#include "super-io.h"

static bool migrate_pred(void *arg, struct bkey_s_c_extent e)
{
	struct bch_dev *ca = arg;

	return bch2_extent_has_device(e, ca->dev_idx);
}

#define MAX_DATA_OFF_ITER	10

static int bch2_dev_usrdata_migrate(struct bch_fs *c, struct bch_dev *ca,
				    int flags)
{
	struct btree_iter iter;
	struct bkey_s_c k;
	struct bch_move_stats stats;
	unsigned pass = 0;
	int ret = 0;

	if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_USER)))
		return 0;

	/*
	 * XXX: we should be able to do this in one pass, but bch2_move_data()
	 * can spuriously fail to move an extent due to racing with other move
	 * operations
	 */
	do {
		ret = bch2_move_data(c, NULL,
				     SECTORS_IN_FLIGHT_PER_DEVICE,
				     NULL,
				     writepoint_hashed((unsigned long) current),
				     0,
				     ca->dev_idx,
				     migrate_pred, ca,
				     &stats);
		if (ret) {
			bch_err(c, "error migrating data: %i", ret);
			return ret;
		}
	} while (atomic64_read(&stats.keys_moved) && pass++ < MAX_DATA_OFF_ITER);

	if (atomic64_read(&stats.keys_moved)) {
		bch_err(c, "unable to migrate all data in %d iterations",
			MAX_DATA_OFF_ITER);
		return -1;
	}

	mutex_lock(&c->replicas_gc_lock);
	bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);

	for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, BTREE_ITER_PREFETCH, k) {
		ret = bch2_check_mark_super(c, BCH_DATA_USER, bch2_bkey_devs(k));
		if (ret) {
			bch_err(c, "error migrating data %i from check_mark_super()", ret);
			break;
		}
	}

	bch2_replicas_gc_end(c, ret);
	mutex_unlock(&c->replicas_gc_lock);
	return ret;
}

static int bch2_dev_metadata_migrate(struct bch_fs *c, struct bch_dev *ca,
				     int flags)
{
	struct btree_iter iter;
	struct btree *b;
	int ret = 0;
	unsigned id;

	if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_BTREE)))
		return 0;

	mutex_lock(&c->replicas_gc_lock);
	bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);

	for (id = 0; id < BTREE_ID_NR; id++) {
		for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
			struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);

			if (!bch2_extent_has_device(e, ca->dev_idx))
				continue;

			ret = bch2_btree_node_rewrite(c, &iter, b->data->keys.seq, 0);
			if (ret) {
				bch2_btree_iter_unlock(&iter);
				goto err;
			}
		}
		ret = bch2_btree_iter_unlock(&iter);
		if (ret)
			goto err;
	}
err:
	bch2_replicas_gc_end(c, ret);
	mutex_unlock(&c->replicas_gc_lock);
	return ret;
}

int bch2_dev_data_migrate(struct bch_fs *c, struct bch_dev *ca, int flags)
{
	BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW &&
	       bch2_dev_is_online(ca));

	return bch2_dev_usrdata_migrate(c, ca, flags) ?:
		bch2_dev_metadata_migrate(c, ca, flags);
}

static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s_extent e,
			 unsigned dev_idx, int flags, bool metadata)
{
	unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
	unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
	unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
	unsigned nr_good;

	bch2_extent_drop_device(e, dev_idx);

	nr_good = bch2_extent_nr_good_ptrs(c, e.c);
	if ((!nr_good && !(flags & lost)) ||
	    (nr_good < replicas && !(flags & degraded)))
		return -EINVAL;

	return 0;
}

static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
{
	struct bkey_s_c k;
	struct bkey_s_extent e;
	BKEY_PADDED(key) tmp;
	struct btree_iter iter;
	int ret = 0;

	mutex_lock(&c->replicas_gc_lock);
	bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);

	bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
			     POS_MIN, BTREE_ITER_PREFETCH);

	while ((k = bch2_btree_iter_peek(&iter)).k &&
	       !(ret = btree_iter_err(k))) {
		if (!bkey_extent_is_data(k.k) ||
		    !bch2_extent_has_device(bkey_s_c_to_extent(k), dev_idx)) {
			ret = bch2_check_mark_super(c, BCH_DATA_USER,
						    bch2_bkey_devs(k));
			if (ret)
				break;
			bch2_btree_iter_next(&iter);
			continue;
		}

		bkey_reassemble(&tmp.key, k);
		e = bkey_i_to_s_extent(&tmp.key);

		ret = drop_dev_ptrs(c, e, dev_idx, flags, false);
		if (ret)
			break;

		/*
		 * If the new extent no longer has any pointers, bch2_extent_normalize()
		 * will do the appropriate thing with it (turning it into a
		 * KEY_TYPE_ERROR key, or just a discard if it was a cached extent)
		 */
		bch2_extent_normalize(c, e.s);

		ret = bch2_check_mark_super(c, BCH_DATA_USER,
				bch2_bkey_devs(bkey_i_to_s_c(&tmp.key)));
		if (ret)
			break;

		iter.pos = bkey_start_pos(&tmp.key.k);

		ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
					   BTREE_INSERT_ATOMIC|
					   BTREE_INSERT_NOFAIL,
					   BTREE_INSERT_ENTRY(&iter, &tmp.key));

		/*
		 * don't want to leave ret == -EINTR, since if we raced and
		 * something else overwrote the key we could spuriously return
		 * -EINTR below:
		 */
		if (ret == -EINTR)
			ret = 0;
		if (ret)
			break;
	}

	bch2_btree_iter_unlock(&iter);

	bch2_replicas_gc_end(c, ret);
	mutex_unlock(&c->replicas_gc_lock);

	return ret;
}

static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
{
	struct btree_iter iter;
	struct closure cl;
	struct btree *b;
	unsigned id;
	int ret;

	/* don't handle this yet: */
	if (flags & BCH_FORCE_IF_METADATA_LOST)
		return -EINVAL;

	closure_init_stack(&cl);

	mutex_lock(&c->replicas_gc_lock);
	bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);

	for (id = 0; id < BTREE_ID_NR; id++) {
		for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
			__BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX) tmp;
			struct bkey_i_extent *new_key;
retry:
			if (!bch2_extent_has_device(bkey_i_to_s_c_extent(&b->key),
						    dev_idx)) {
				bch2_btree_iter_set_locks_want(&iter, 0);

				ret = bch2_check_mark_super(c, BCH_DATA_BTREE,
						bch2_bkey_devs(bkey_i_to_s_c(&b->key)));
				if (ret)
					goto err;
			} else {
				bkey_copy(&tmp.k, &b->key);
				new_key = bkey_i_to_extent(&tmp.k);

				ret = drop_dev_ptrs(c, extent_i_to_s(new_key),
						    dev_idx, flags, true);
				if (ret)
					goto err;

				if (!bch2_btree_iter_set_locks_want(&iter, U8_MAX)) {
					b = bch2_btree_iter_peek_node(&iter);
					goto retry;
				}

				ret = bch2_btree_node_update_key(c, &iter, b, new_key);
				if (ret == -EINTR) {
					b = bch2_btree_iter_peek_node(&iter);
					goto retry;
				}
				if (ret)
					goto err;
			}
		}
		bch2_btree_iter_unlock(&iter);
	}

	ret = 0;
out:
	bch2_replicas_gc_end(c, ret);
	mutex_unlock(&c->replicas_gc_lock);

	return ret;
err:
	bch2_btree_iter_unlock(&iter);
	goto out;
}

int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
{
	return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
		bch2_dev_metadata_drop(c, dev_idx, flags);
}