1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
6 #include "btree_update.h"
8 #include "data_update.h"
15 #include "nocow_locking.h"
16 #include "rebalance.h"
17 #include "subvolume.h"
20 static void trace_move_extent_finish2(struct bch_fs *c, struct bkey_s_c k)
22 if (trace_move_extent_finish_enabled()) {
23 struct printbuf buf = PRINTBUF;
25 bch2_bkey_val_to_text(&buf, c, k);
26 trace_move_extent_finish(c, buf.buf);
31 static void trace_move_extent_fail2(struct data_update *m,
33 struct bkey_s_c wrote,
34 struct bkey_i *insert,
37 struct bch_fs *c = m->op.c;
38 struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
39 const union bch_extent_entry *entry;
40 struct bch_extent_ptr *ptr;
41 struct extent_ptr_decoded p;
42 struct printbuf buf = PRINTBUF;
43 unsigned i, rewrites_found = 0;
45 if (!trace_move_extent_fail_enabled())
52 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry) {
53 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
54 (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
56 rewrites_found |= 1U << i;
61 prt_printf(&buf, "\nrewrite ptrs: %u%u%u%u",
62 (m->data_opts.rewrite_ptrs & (1 << 0)) != 0,
63 (m->data_opts.rewrite_ptrs & (1 << 1)) != 0,
64 (m->data_opts.rewrite_ptrs & (1 << 2)) != 0,
65 (m->data_opts.rewrite_ptrs & (1 << 3)) != 0);
67 prt_printf(&buf, "\nrewrites found: %u%u%u%u",
68 (rewrites_found & (1 << 0)) != 0,
69 (rewrites_found & (1 << 1)) != 0,
70 (rewrites_found & (1 << 2)) != 0,
71 (rewrites_found & (1 << 3)) != 0);
73 prt_str(&buf, "\nold: ");
74 bch2_bkey_val_to_text(&buf, c, old);
76 prt_str(&buf, "\nnew: ");
77 bch2_bkey_val_to_text(&buf, c, new);
79 prt_str(&buf, "\nwrote: ");
80 bch2_bkey_val_to_text(&buf, c, wrote);
83 prt_str(&buf, "\ninsert: ");
84 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
87 trace_move_extent_fail(c, buf.buf);
91 static int __bch2_data_update_index_update(struct btree_trans *trans,
92 struct bch_write_op *op)
94 struct bch_fs *c = op->c;
95 struct btree_iter iter;
96 struct data_update *m =
97 container_of(op, struct data_update, op);
98 struct keylist *keys = &op->insert_keys;
99 struct bkey_buf _new, _insert;
102 bch2_bkey_buf_init(&_new);
103 bch2_bkey_buf_init(&_insert);
104 bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
106 bch2_trans_iter_init(trans, &iter, m->btree_id,
107 bkey_start_pos(&bch2_keylist_front(keys)->k),
108 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
112 struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
113 struct bkey_i *insert = NULL;
114 struct bkey_i_extent *new;
115 const union bch_extent_entry *entry_c;
116 union bch_extent_entry *entry;
117 struct extent_ptr_decoded p;
118 struct bch_extent_ptr *ptr;
119 const struct bch_extent_ptr *ptr_c;
120 struct bpos next_pos;
121 bool should_check_enospc;
122 s64 i_sectors_delta = 0, disk_sectors_delta = 0;
123 unsigned rewrites_found = 0, durability, i;
125 bch2_trans_begin(trans);
127 k = bch2_btree_iter_peek_slot(&iter);
132 new = bkey_i_to_extent(bch2_keylist_front(keys));
134 if (!bch2_extents_match(k, old)) {
135 trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i),
140 bkey_reassemble(_insert.k, k);
143 bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
144 new = bkey_i_to_extent(_new.k);
145 bch2_cut_front(iter.pos, &new->k_i);
147 bch2_cut_front(iter.pos, insert);
148 bch2_cut_back(new->k.p, insert);
149 bch2_cut_back(insert->k.p, &new->k_i);
152 * @old: extent that we read from
153 * @insert: key that we're going to update, initialized from
154 * extent currently in btree - same as @old unless we raced with
156 * @new: extent with new pointers that we'll be adding to @insert
158 * Fist, drop rewrite_ptrs from @new:
161 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry_c) {
162 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
163 (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
165 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), ptr);
166 rewrites_found |= 1U << i;
171 if (m->data_opts.rewrite_ptrs &&
173 bch2_bkey_durability(c, k) >= m->op.opts.data_replicas) {
174 trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i), insert, "no rewrites found:");
179 * A replica that we just wrote might conflict with a replica
180 * that we want to keep, due to racing with another move:
182 restart_drop_conflicting_replicas:
183 extent_for_each_ptr(extent_i_to_s(new), ptr)
184 if ((ptr_c = bch2_bkey_has_device_c(bkey_i_to_s_c(insert), ptr->dev)) &&
186 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(&new->k_i), ptr);
187 goto restart_drop_conflicting_replicas;
190 if (!bkey_val_u64s(&new->k)) {
191 trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i), insert, "new replicas conflicted:");
195 /* Now, drop pointers that conflict with what we just wrote: */
196 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
197 if ((ptr = bch2_bkey_has_device(bkey_i_to_s(insert), p.ptr.dev)))
198 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
200 durability = bch2_bkey_durability(c, bkey_i_to_s_c(insert)) +
201 bch2_bkey_durability(c, bkey_i_to_s_c(&new->k_i));
203 /* Now, drop excess replicas: */
204 restart_drop_extra_replicas:
205 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs(bkey_i_to_s(insert)), p, entry) {
206 unsigned ptr_durability = bch2_extent_ptr_durability(c, &p);
209 durability - ptr_durability >= m->op.opts.data_replicas) {
210 durability -= ptr_durability;
212 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), &entry->ptr);
213 goto restart_drop_extra_replicas;
217 /* Finally, add the pointers we just wrote: */
218 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
219 bch2_extent_ptr_decoded_append(insert, &p);
221 bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
222 bch2_extent_normalize(c, bkey_i_to_s(insert));
224 ret = bch2_sum_sector_overwrites(trans, &iter, insert,
225 &should_check_enospc,
227 &disk_sectors_delta);
231 if (disk_sectors_delta > (s64) op->res.sectors) {
232 ret = bch2_disk_reservation_add(c, &op->res,
233 disk_sectors_delta - op->res.sectors,
235 ? BCH_DISK_RESERVATION_NOFAIL : 0);
240 next_pos = insert->k.p;
243 * Check for nonce offset inconsistency:
244 * This is debug code - we've been seeing this bug rarely, and
245 * it's been hard to reproduce, so this should give us some more
246 * information when it does occur:
248 struct printbuf err = PRINTBUF;
249 int invalid = bch2_bkey_invalid(c, bkey_i_to_s_c(insert), __btree_node_type(0, m->btree_id), 0, &err);
253 struct printbuf buf = PRINTBUF;
255 prt_str(&buf, "about to insert invalid key in data update path");
256 prt_str(&buf, "\nold: ");
257 bch2_bkey_val_to_text(&buf, c, old);
258 prt_str(&buf, "\nk: ");
259 bch2_bkey_val_to_text(&buf, c, k);
260 prt_str(&buf, "\nnew: ");
261 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
263 bch2_print_string_as_lines(KERN_ERR, buf.buf);
270 ret = bch2_insert_snapshot_whiteouts(trans, m->btree_id,
271 k.k->p, bkey_start_pos(&insert->k)) ?:
272 bch2_insert_snapshot_whiteouts(trans, m->btree_id,
273 k.k->p, insert->k.p) ?:
274 bch2_bkey_set_needs_rebalance(c, insert,
275 op->opts.background_target,
276 op->opts.background_compression) ?:
277 bch2_trans_update(trans, &iter, insert,
278 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
279 bch2_trans_commit(trans, &op->res,
281 BCH_TRANS_COMMIT_no_check_rw|
282 BCH_TRANS_COMMIT_no_enospc|
283 m->data_opts.btree_insert_flags);
285 bch2_btree_iter_set_pos(&iter, next_pos);
287 this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
288 trace_move_extent_finish2(c, bkey_i_to_s_c(&new->k_i));
291 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
296 while (bkey_ge(iter.pos, bch2_keylist_front(keys)->k.p)) {
297 bch2_keylist_pop_front(keys);
298 if (bch2_keylist_empty(keys))
303 if (m->stats && m->stats) {
304 BUG_ON(k.k->p.offset <= iter.pos.offset);
305 atomic64_inc(&m->stats->keys_raced);
306 atomic64_add(k.k->p.offset - iter.pos.offset,
307 &m->stats->sectors_raced);
310 this_cpu_inc(c->counters[BCH_COUNTER_move_extent_fail]);
312 bch2_btree_iter_advance(&iter);
316 bch2_trans_iter_exit(trans, &iter);
317 bch2_bkey_buf_exit(&_insert, c);
318 bch2_bkey_buf_exit(&_new, c);
319 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
323 int bch2_data_update_index_update(struct bch_write_op *op)
325 return bch2_trans_run(op->c, __bch2_data_update_index_update(trans, op));
328 void bch2_data_update_read_done(struct data_update *m,
329 struct bch_extent_crc_unpacked crc)
331 /* write bio must own pages: */
332 BUG_ON(!m->op.wbio.bio.bi_vcnt);
335 m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
337 closure_call(&m->op.cl, bch2_write, NULL, NULL);
340 void bch2_data_update_exit(struct data_update *update)
342 struct bch_fs *c = update->op.c;
343 struct bkey_ptrs_c ptrs =
344 bch2_bkey_ptrs_c(bkey_i_to_s_c(update->k.k));
345 const struct bch_extent_ptr *ptr;
347 bkey_for_each_ptr(ptrs, ptr) {
348 if (c->opts.nocow_enabled)
349 bch2_bucket_nocow_unlock(&c->nocow_locks,
350 PTR_BUCKET_POS(c, ptr), 0);
351 percpu_ref_put(&bch_dev_bkey_exists(c, ptr->dev)->ref);
354 bch2_bkey_buf_exit(&update->k, c);
355 bch2_disk_reservation_put(c, &update->op.res);
356 bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
359 void bch2_update_unwritten_extent(struct btree_trans *trans,
360 struct data_update *update)
362 struct bch_fs *c = update->op.c;
363 struct bio *bio = &update->op.wbio.bio;
364 struct bkey_i_extent *e;
365 struct write_point *wp;
366 struct bch_extent_ptr *ptr;
368 struct btree_iter iter;
372 closure_init_stack(&cl);
373 bch2_keylist_init(&update->op.insert_keys, update->op.inline_keys);
375 while (bio_sectors(bio)) {
376 unsigned sectors = bio_sectors(bio);
378 bch2_trans_iter_init(trans, &iter, update->btree_id, update->op.pos,
380 ret = lockrestart_do(trans, ({
381 k = bch2_btree_iter_peek_slot(&iter);
384 bch2_trans_iter_exit(trans, &iter);
386 if (ret || !bch2_extents_match(k, bkey_i_to_s_c(update->k.k)))
389 e = bkey_extent_init(update->op.insert_keys.top);
390 e->k.p = update->op.pos;
392 ret = bch2_alloc_sectors_start_trans(trans,
395 update->op.write_point,
396 &update->op.devs_have,
397 update->op.nr_replicas,
398 update->op.nr_replicas,
399 update->op.watermark,
401 if (bch2_err_matches(ret, BCH_ERR_operation_blocked)) {
402 bch2_trans_unlock(trans);
410 sectors = min(sectors, wp->sectors_free);
412 bch2_key_resize(&e->k, sectors);
414 bch2_open_bucket_get(c, wp, &update->op.open_buckets);
415 bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, sectors, false);
416 bch2_alloc_sectors_done(c, wp);
418 bio_advance(bio, sectors << 9);
419 update->op.pos.offset += sectors;
421 extent_for_each_ptr(extent_i_to_s(e), ptr)
422 ptr->unwritten = true;
423 bch2_keylist_push(&update->op.insert_keys);
425 ret = __bch2_data_update_index_update(trans, &update->op);
427 bch2_open_buckets_put(c, &update->op.open_buckets);
433 if (closure_nr_remaining(&cl) != 1) {
434 bch2_trans_unlock(trans);
439 int bch2_data_update_init(struct btree_trans *trans,
440 struct moving_context *ctxt,
441 struct data_update *m,
442 struct write_point_specifier wp,
443 struct bch_io_opts io_opts,
444 struct data_update_opts data_opts,
445 enum btree_id btree_id,
448 struct bch_fs *c = trans->c;
449 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
450 const union bch_extent_entry *entry;
451 struct extent_ptr_decoded p;
452 const struct bch_extent_ptr *ptr;
453 unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
454 unsigned ptrs_locked = 0;
457 bch2_bkey_buf_init(&m->k);
458 bch2_bkey_buf_reassemble(&m->k, c, k);
459 m->btree_id = btree_id;
460 m->data_opts = data_opts;
462 m->stats = ctxt ? ctxt->stats : NULL;
464 bch2_write_op_init(&m->op, c, io_opts);
465 m->op.pos = bkey_start_pos(k.k);
466 m->op.version = k.k->version;
467 m->op.target = data_opts.target;
468 m->op.write_point = wp;
469 m->op.nr_replicas = 0;
470 m->op.flags |= BCH_WRITE_PAGES_STABLE|
471 BCH_WRITE_PAGES_OWNED|
472 BCH_WRITE_DATA_ENCODED|
474 m->data_opts.write_flags;
475 m->op.compression_opt = io_opts.background_compression ?: io_opts.compression;
476 m->op.watermark = m->data_opts.btree_insert_flags & BCH_WATERMARK_MASK;
478 bkey_for_each_ptr(ptrs, ptr)
479 percpu_ref_get(&bch_dev_bkey_exists(c, ptr->dev)->ref);
482 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
485 if (((1U << i) & m->data_opts.rewrite_ptrs)) {
486 BUG_ON(p.ptr.cached);
488 if (crc_is_compressed(p.crc))
489 reserve_sectors += k.k->size;
491 m->op.nr_replicas += bch2_extent_ptr_desired_durability(c, &p);
492 } else if (!p.ptr.cached) {
493 bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
497 * op->csum_type is normally initialized from the fs/file's
498 * current options - but if an extent is encrypted, we require
499 * that it stays encrypted:
501 if (bch2_csum_type_is_encryption(p.crc.csum_type)) {
502 m->op.nonce = p.crc.nonce + p.crc.offset;
503 m->op.csum_type = p.crc.csum_type;
506 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
507 m->op.incompressible = true;
509 if (c->opts.nocow_enabled) {
511 move_ctxt_wait_event(ctxt,
512 (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
513 PTR_BUCKET_POS(c, &p.ptr), 0)) ||
514 !atomic_read(&ctxt->read_sectors));
517 bch2_bucket_nocow_lock(&c->nocow_locks,
518 PTR_BUCKET_POS(c, &p.ptr), 0);
520 if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
521 PTR_BUCKET_POS(c, &p.ptr), 0)) {
522 ret = -BCH_ERR_nocow_lock_blocked;
526 ptrs_locked |= (1U << i);
532 if (reserve_sectors) {
533 ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
534 m->data_opts.extra_replicas
536 : BCH_DISK_RESERVATION_NOFAIL);
541 m->op.nr_replicas += m->data_opts.extra_replicas;
542 m->op.nr_replicas_required = m->op.nr_replicas;
544 BUG_ON(!m->op.nr_replicas);
546 /* Special handling required: */
547 if (bkey_extent_is_unwritten(k))
548 return -BCH_ERR_unwritten_extent_update;
552 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
553 if ((1U << i) & ptrs_locked)
554 bch2_bucket_nocow_unlock(&c->nocow_locks,
555 PTR_BUCKET_POS(c, &p.ptr), 0);
556 percpu_ref_put(&bch_dev_bkey_exists(c, p.ptr.dev)->ref);
560 bch2_bkey_buf_exit(&m->k, c);
561 bch2_bio_free_pages_pool(c, &m->op.wbio.bio);
565 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
567 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
568 const struct bch_extent_ptr *ptr;
571 bkey_for_each_ptr(ptrs, ptr) {
572 if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
573 opts->kill_ptrs |= 1U << i;
574 opts->rewrite_ptrs ^= 1U << i;