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 "subvolume.h"
18 #include <trace/events/bcachefs.h>
20 static int insert_snapshot_whiteouts(struct btree_trans *trans,
25 struct bch_fs *c = trans->c;
26 struct btree_iter iter, iter2;
27 struct bkey_s_c k, k2;
29 struct bkey_i *update;
32 if (!btree_type_has_snapshots(id))
37 if (!bch2_snapshot_has_children(c, old_pos.snapshot))
40 bch2_trans_iter_init(trans, &iter, id, old_pos,
41 BTREE_ITER_NOT_EXTENTS|
42 BTREE_ITER_ALL_SNAPSHOTS);
44 k = bch2_btree_iter_prev(&iter);
52 if (!bkey_eq(old_pos, k.k->p))
55 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot) &&
56 !snapshot_list_has_ancestor(c, &s, k.k->p.snapshot)) {
57 struct bpos whiteout_pos = new_pos;
59 whiteout_pos.snapshot = k.k->p.snapshot;
61 bch2_trans_iter_init(trans, &iter2, id, whiteout_pos,
62 BTREE_ITER_NOT_EXTENTS|
64 k2 = bch2_btree_iter_peek_slot(&iter2);
67 if (!ret && k2.k->type == KEY_TYPE_deleted) {
68 update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
69 ret = PTR_ERR_OR_ZERO(update);
73 bkey_init(&update->k);
74 update->k.p = whiteout_pos;
75 update->k.type = KEY_TYPE_whiteout;
77 ret = bch2_trans_update(trans, &iter2, update,
78 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
80 bch2_trans_iter_exit(trans, &iter2);
85 ret = snapshot_list_add(c, &s, k.k->p.snapshot);
90 bch2_trans_iter_exit(trans, &iter);
96 static void trace_move_extent_fail2(struct data_update *m,
98 struct bkey_s_c wrote,
99 struct bkey_i *insert,
102 struct bch_fs *c = m->op.c;
103 struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
104 const union bch_extent_entry *entry;
105 struct bch_extent_ptr *ptr;
106 struct extent_ptr_decoded p;
107 struct printbuf buf = PRINTBUF;
108 unsigned i, rewrites_found = 0;
110 if (!trace_move_extent_fail_enabled())
117 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry) {
119 new_s.k = (void *) new.k;
120 new_s.v = (void *) new.v;
122 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
123 (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
125 rewrites_found |= 1U << i;
130 prt_printf(&buf, "\nrewrite ptrs: %u%u%u%u",
131 (m->data_opts.rewrite_ptrs & (1 << 0)) != 0,
132 (m->data_opts.rewrite_ptrs & (1 << 1)) != 0,
133 (m->data_opts.rewrite_ptrs & (1 << 2)) != 0,
134 (m->data_opts.rewrite_ptrs & (1 << 3)) != 0);
136 prt_printf(&buf, "\nrewrites found: %u%u%u%u",
137 (rewrites_found & (1 << 0)) != 0,
138 (rewrites_found & (1 << 1)) != 0,
139 (rewrites_found & (1 << 2)) != 0,
140 (rewrites_found & (1 << 3)) != 0);
142 prt_str(&buf, "\nold: ");
143 bch2_bkey_val_to_text(&buf, c, old);
145 prt_str(&buf, "\nnew: ");
146 bch2_bkey_val_to_text(&buf, c, new);
148 prt_str(&buf, "\nwrote: ");
149 bch2_bkey_val_to_text(&buf, c, wrote);
152 prt_str(&buf, "\ninsert: ");
153 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
156 trace_move_extent_fail(c, buf.buf);
160 static int __bch2_data_update_index_update(struct btree_trans *trans,
161 struct bch_write_op *op)
163 struct bch_fs *c = op->c;
164 struct btree_iter iter;
165 struct data_update *m =
166 container_of(op, struct data_update, op);
167 struct keylist *keys = &op->insert_keys;
168 struct bkey_buf _new, _insert;
171 bch2_bkey_buf_init(&_new);
172 bch2_bkey_buf_init(&_insert);
173 bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
175 bch2_trans_iter_init(trans, &iter, m->btree_id,
176 bkey_start_pos(&bch2_keylist_front(keys)->k),
177 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
181 struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
182 struct bkey_i *insert = NULL;
183 struct bkey_i_extent *new;
184 const union bch_extent_entry *entry_c;
185 union bch_extent_entry *entry;
186 struct extent_ptr_decoded p;
187 struct bch_extent_ptr *ptr;
188 const struct bch_extent_ptr *ptr_c;
189 struct bpos next_pos;
190 bool should_check_enospc;
191 s64 i_sectors_delta = 0, disk_sectors_delta = 0;
192 unsigned rewrites_found = 0, durability, i;
194 bch2_trans_begin(trans);
196 k = bch2_btree_iter_peek_slot(&iter);
201 new = bkey_i_to_extent(bch2_keylist_front(keys));
203 if (!bch2_extents_match(k, old)) {
204 trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i),
209 bkey_reassemble(_insert.k, k);
212 bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
213 new = bkey_i_to_extent(_new.k);
214 bch2_cut_front(iter.pos, &new->k_i);
216 bch2_cut_front(iter.pos, insert);
217 bch2_cut_back(new->k.p, insert);
218 bch2_cut_back(insert->k.p, &new->k_i);
221 * @old: extent that we read from
222 * @insert: key that we're going to update, initialized from
223 * extent currently in btree - same as @old unless we raced with
225 * @new: extent with new pointers that we'll be adding to @insert
227 * Fist, drop rewrite_ptrs from @new:
230 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry_c) {
231 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
232 (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
234 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
237 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), ptr);
239 rewrites_found |= 1U << i;
244 if (m->data_opts.rewrite_ptrs &&
246 bch2_bkey_durability(c, k) >= m->op.opts.data_replicas) {
247 trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i), insert, "no rewrites found:");
252 * A replica that we just wrote might conflict with a replica
253 * that we want to keep, due to racing with another move:
255 restart_drop_conflicting_replicas:
256 extent_for_each_ptr(extent_i_to_s(new), ptr)
257 if ((ptr_c = bch2_bkey_has_device_c(bkey_i_to_s_c(insert), ptr->dev)) &&
259 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(&new->k_i), ptr);
260 goto restart_drop_conflicting_replicas;
263 if (!bkey_val_u64s(&new->k)) {
264 trace_move_extent_fail2(m, k, bkey_i_to_s_c(&new->k_i), insert, "new replicas conflicted:");
268 /* Now, drop pointers that conflict with what we just wrote: */
269 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
270 if ((ptr = bch2_bkey_has_device(bkey_i_to_s(insert), p.ptr.dev)))
271 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
273 durability = bch2_bkey_durability(c, bkey_i_to_s_c(insert)) +
274 bch2_bkey_durability(c, bkey_i_to_s_c(&new->k_i));
276 /* Now, drop excess replicas: */
277 restart_drop_extra_replicas:
278 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs(bkey_i_to_s(insert)), p, entry) {
279 unsigned ptr_durability = bch2_extent_ptr_durability(c, &p);
282 durability - ptr_durability >= m->op.opts.data_replicas) {
283 durability -= ptr_durability;
284 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), &entry->ptr);
286 * Currently, we're dropping unneeded replicas
287 * instead of marking them as cached, since
288 * cached data in stripe buckets prevents them
290 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), &entry->ptr);
292 goto restart_drop_extra_replicas;
296 /* Finally, add the pointers we just wrote: */
297 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
298 bch2_extent_ptr_decoded_append(insert, &p);
300 bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
301 bch2_extent_normalize(c, bkey_i_to_s(insert));
303 ret = bch2_sum_sector_overwrites(trans, &iter, insert,
304 &should_check_enospc,
306 &disk_sectors_delta);
310 if (disk_sectors_delta > (s64) op->res.sectors) {
311 ret = bch2_disk_reservation_add(c, &op->res,
312 disk_sectors_delta - op->res.sectors,
314 ? BCH_DISK_RESERVATION_NOFAIL : 0);
319 next_pos = insert->k.p;
321 if (!bkey_eq(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) {
322 ret = insert_snapshot_whiteouts(trans, m->btree_id, k.k->p,
323 bkey_start_pos(&insert->k));
328 if (!bkey_eq(insert->k.p, k.k->p)) {
329 ret = insert_snapshot_whiteouts(trans, m->btree_id,
330 k.k->p, insert->k.p);
335 ret = bch2_trans_update(trans, &iter, insert,
336 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
337 bch2_trans_commit(trans, &op->res,
339 BTREE_INSERT_NOCHECK_RW|
341 m->data_opts.btree_insert_flags);
343 bch2_btree_iter_set_pos(&iter, next_pos);
345 this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
346 trace_move_extent_finish(&new->k);
349 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
354 while (bkey_ge(iter.pos, bch2_keylist_front(keys)->k.p)) {
355 bch2_keylist_pop_front(keys);
356 if (bch2_keylist_empty(keys))
361 if (m->ctxt && m->ctxt->stats) {
362 BUG_ON(k.k->p.offset <= iter.pos.offset);
363 atomic64_inc(&m->ctxt->stats->keys_raced);
364 atomic64_add(k.k->p.offset - iter.pos.offset,
365 &m->ctxt->stats->sectors_raced);
368 this_cpu_add(c->counters[BCH_COUNTER_move_extent_fail], new->k.size);
370 bch2_btree_iter_advance(&iter);
374 bch2_trans_iter_exit(trans, &iter);
375 bch2_bkey_buf_exit(&_insert, c);
376 bch2_bkey_buf_exit(&_new, c);
377 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
381 int bch2_data_update_index_update(struct bch_write_op *op)
383 return bch2_trans_run(op->c, __bch2_data_update_index_update(&trans, op));
386 void bch2_data_update_read_done(struct data_update *m,
387 struct bch_extent_crc_unpacked crc)
389 /* write bio must own pages: */
390 BUG_ON(!m->op.wbio.bio.bi_vcnt);
393 m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
395 closure_call(&m->op.cl, bch2_write, NULL, NULL);
398 void bch2_data_update_exit(struct data_update *update)
400 struct bch_fs *c = update->op.c;
401 struct bkey_ptrs_c ptrs =
402 bch2_bkey_ptrs_c(bkey_i_to_s_c(update->k.k));
403 const struct bch_extent_ptr *ptr;
405 bkey_for_each_ptr(ptrs, ptr) {
406 if (c->opts.nocow_enabled)
407 bch2_bucket_nocow_unlock(&c->nocow_locks,
408 PTR_BUCKET_POS(c, ptr), 0);
409 percpu_ref_put(&bch_dev_bkey_exists(c, ptr->dev)->ref);
412 bch2_bkey_buf_exit(&update->k, c);
413 bch2_disk_reservation_put(c, &update->op.res);
414 bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
417 void bch2_update_unwritten_extent(struct btree_trans *trans,
418 struct data_update *update)
420 struct bch_fs *c = update->op.c;
421 struct bio *bio = &update->op.wbio.bio;
422 struct bkey_i_extent *e;
423 struct write_point *wp;
424 struct bch_extent_ptr *ptr;
426 struct btree_iter iter;
430 closure_init_stack(&cl);
431 bch2_keylist_init(&update->op.insert_keys, update->op.inline_keys);
433 while (bio_sectors(bio)) {
434 unsigned sectors = bio_sectors(bio);
436 bch2_trans_iter_init(trans, &iter, update->btree_id, update->op.pos,
438 ret = lockrestart_do(trans, ({
439 k = bch2_btree_iter_peek_slot(&iter);
442 bch2_trans_iter_exit(trans, &iter);
444 if (ret || !bch2_extents_match(k, bkey_i_to_s_c(update->k.k)))
447 e = bkey_extent_init(update->op.insert_keys.top);
448 e->k.p = update->op.pos;
450 ret = bch2_alloc_sectors_start_trans(trans,
453 update->op.write_point,
454 &update->op.devs_have,
455 update->op.nr_replicas,
456 update->op.nr_replicas,
457 update->op.alloc_reserve,
459 if (bch2_err_matches(ret, BCH_ERR_operation_blocked)) {
460 bch2_trans_unlock(trans);
468 sectors = min(sectors, wp->sectors_free);
470 bch2_key_resize(&e->k, sectors);
472 bch2_open_bucket_get(c, wp, &update->op.open_buckets);
473 bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, sectors, false);
474 bch2_alloc_sectors_done(c, wp);
476 bio_advance(bio, sectors << 9);
477 update->op.pos.offset += sectors;
479 extent_for_each_ptr(extent_i_to_s(e), ptr)
480 ptr->unwritten = true;
481 bch2_keylist_push(&update->op.insert_keys);
483 ret = __bch2_data_update_index_update(trans, &update->op);
485 bch2_open_buckets_put(c, &update->op.open_buckets);
491 if ((atomic_read(&cl.remaining) & CLOSURE_REMAINING_MASK) != 1) {
492 bch2_trans_unlock(trans);
497 int bch2_data_update_init(struct btree_trans *trans,
498 struct moving_context *ctxt,
499 struct data_update *m,
500 struct write_point_specifier wp,
501 struct bch_io_opts io_opts,
502 struct data_update_opts data_opts,
503 enum btree_id btree_id,
506 struct bch_fs *c = trans->c;
507 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
508 const union bch_extent_entry *entry;
509 struct extent_ptr_decoded p;
510 const struct bch_extent_ptr *ptr;
511 unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
512 unsigned ptrs_locked = 0;
515 bch2_bkey_buf_init(&m->k);
516 bch2_bkey_buf_reassemble(&m->k, c, k);
517 m->btree_id = btree_id;
518 m->data_opts = data_opts;
520 bch2_write_op_init(&m->op, c, io_opts);
521 m->op.pos = bkey_start_pos(k.k);
522 m->op.version = k.k->version;
523 m->op.target = data_opts.target;
524 m->op.write_point = wp;
525 m->op.nr_replicas = 0;
526 m->op.flags |= BCH_WRITE_PAGES_STABLE|
527 BCH_WRITE_PAGES_OWNED|
528 BCH_WRITE_DATA_ENCODED|
530 m->data_opts.write_flags;
531 m->op.compression_type =
532 bch2_compression_opt_to_type[io_opts.background_compression ?:
533 io_opts.compression];
534 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
535 m->op.alloc_reserve = RESERVE_movinggc;
537 bkey_for_each_ptr(ptrs, ptr)
538 percpu_ref_get(&bch_dev_bkey_exists(c, ptr->dev)->ref);
541 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
544 if (((1U << i) & m->data_opts.rewrite_ptrs)) {
545 BUG_ON(p.ptr.cached);
547 if (crc_is_compressed(p.crc))
548 reserve_sectors += k.k->size;
550 m->op.nr_replicas += bch2_extent_ptr_durability(c, &p);
551 } else if (!p.ptr.cached) {
552 bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
556 * op->csum_type is normally initialized from the fs/file's
557 * current options - but if an extent is encrypted, we require
558 * that it stays encrypted:
560 if (bch2_csum_type_is_encryption(p.crc.csum_type)) {
561 m->op.nonce = p.crc.nonce + p.crc.offset;
562 m->op.csum_type = p.crc.csum_type;
565 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
566 m->op.incompressible = true;
568 if (c->opts.nocow_enabled) {
570 move_ctxt_wait_event(ctxt, trans,
571 (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
572 PTR_BUCKET_POS(c, &p.ptr), 0)) ||
573 !atomic_read(&ctxt->read_sectors));
576 bch2_bucket_nocow_lock(&c->nocow_locks,
577 PTR_BUCKET_POS(c, &p.ptr), 0);
579 if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
580 PTR_BUCKET_POS(c, &p.ptr), 0)) {
581 ret = -BCH_ERR_nocow_lock_blocked;
585 ptrs_locked |= (1U << i);
591 if (reserve_sectors) {
592 ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
593 m->data_opts.extra_replicas
595 : BCH_DISK_RESERVATION_NOFAIL);
600 m->op.nr_replicas += m->data_opts.extra_replicas;
601 m->op.nr_replicas_required = m->op.nr_replicas;
603 BUG_ON(!m->op.nr_replicas);
605 /* Special handling required: */
606 if (bkey_extent_is_unwritten(k))
607 return -BCH_ERR_unwritten_extent_update;
611 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
612 if ((1U << i) & ptrs_locked)
613 bch2_bucket_nocow_unlock(&c->nocow_locks,
614 PTR_BUCKET_POS(c, &p.ptr), 0);
615 percpu_ref_put(&bch_dev_bkey_exists(c, p.ptr.dev)->ref);
619 bch2_bkey_buf_exit(&m->k, c);
620 bch2_bio_free_pages_pool(c, &m->op.wbio.bio);
624 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
626 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
627 const struct bch_extent_ptr *ptr;
630 bkey_for_each_ptr(ptrs, ptr) {
631 if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
632 opts->kill_ptrs |= 1U << i;
633 opts->rewrite_ptrs ^= 1U << i;