1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
6 #include "btree_update.h"
8 #include "data_update.h"
14 #include "nocow_locking.h"
15 #include "subvolume.h"
17 #include <trace/events/bcachefs.h>
19 static int insert_snapshot_whiteouts(struct btree_trans *trans,
24 struct bch_fs *c = trans->c;
25 struct btree_iter iter, iter2;
26 struct bkey_s_c k, k2;
28 struct bkey_i *update;
31 if (!btree_type_has_snapshots(id))
36 if (!bch2_snapshot_has_children(c, old_pos.snapshot))
39 bch2_trans_iter_init(trans, &iter, id, old_pos,
40 BTREE_ITER_NOT_EXTENTS|
41 BTREE_ITER_ALL_SNAPSHOTS);
43 k = bch2_btree_iter_prev(&iter);
51 if (!bkey_eq(old_pos, k.k->p))
54 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot) &&
55 !snapshot_list_has_ancestor(c, &s, k.k->p.snapshot)) {
56 struct bpos whiteout_pos = new_pos;
58 whiteout_pos.snapshot = k.k->p.snapshot;
60 bch2_trans_iter_init(trans, &iter2, id, whiteout_pos,
61 BTREE_ITER_NOT_EXTENTS|
63 k2 = bch2_btree_iter_peek_slot(&iter2);
66 if (!ret && k2.k->type == KEY_TYPE_deleted) {
67 update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
68 ret = PTR_ERR_OR_ZERO(update);
72 bkey_init(&update->k);
73 update->k.p = whiteout_pos;
74 update->k.type = KEY_TYPE_whiteout;
76 ret = bch2_trans_update(trans, &iter2, update,
77 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
79 bch2_trans_iter_exit(trans, &iter2);
84 ret = snapshot_list_add(c, &s, k.k->p.snapshot);
89 bch2_trans_iter_exit(trans, &iter);
95 static int __bch2_data_update_index_update(struct btree_trans *trans,
96 struct bch_write_op *op)
98 struct bch_fs *c = op->c;
99 struct btree_iter iter;
100 struct data_update *m =
101 container_of(op, struct data_update, op);
102 struct keylist *keys = &op->insert_keys;
103 struct bkey_buf _new, _insert;
106 bch2_bkey_buf_init(&_new);
107 bch2_bkey_buf_init(&_insert);
108 bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
110 bch2_trans_iter_init(trans, &iter, m->btree_id,
111 bkey_start_pos(&bch2_keylist_front(keys)->k),
112 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
116 struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
117 struct bkey_i *insert = NULL;
118 struct bkey_i_extent *new;
119 const union bch_extent_entry *entry_c;
120 union bch_extent_entry *entry;
121 struct extent_ptr_decoded p;
122 struct bch_extent_ptr *ptr;
123 const struct bch_extent_ptr *ptr_c;
124 struct bpos next_pos;
125 bool should_check_enospc;
126 s64 i_sectors_delta = 0, disk_sectors_delta = 0;
127 unsigned rewrites_found = 0, durability, i;
129 bch2_trans_begin(trans);
131 k = bch2_btree_iter_peek_slot(&iter);
136 new = bkey_i_to_extent(bch2_keylist_front(keys));
138 if (!bch2_extents_match(k, old))
141 bkey_reassemble(_insert.k, k);
144 bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
145 new = bkey_i_to_extent(_new.k);
146 bch2_cut_front(iter.pos, &new->k_i);
148 bch2_cut_front(iter.pos, insert);
149 bch2_cut_back(new->k.p, insert);
150 bch2_cut_back(insert->k.p, &new->k_i);
153 * @old: extent that we read from
154 * @insert: key that we're going to update, initialized from
155 * extent currently in btree - same as @old unless we raced with
157 * @new: extent with new pointers that we'll be adding to @insert
159 * Fist, drop rewrite_ptrs from @new:
162 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry_c) {
163 if (((1U << i) & m->data_opts.rewrite_ptrs) &&
164 (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
166 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
169 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), ptr);
171 rewrites_found |= 1U << i;
176 if (m->data_opts.rewrite_ptrs &&
178 bch2_bkey_durability(c, k) >= m->op.opts.data_replicas)
182 * A replica that we just wrote might conflict with a replica
183 * that we want to keep, due to racing with another move:
185 restart_drop_conflicting_replicas:
186 extent_for_each_ptr(extent_i_to_s(new), ptr)
187 if ((ptr_c = bch2_bkey_has_device_c(bkey_i_to_s_c(insert), ptr->dev)) &&
189 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(&new->k_i), ptr);
190 goto restart_drop_conflicting_replicas;
193 if (!bkey_val_u64s(&new->k))
196 /* Now, drop pointers that conflict with what we just wrote: */
197 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
198 if ((ptr = bch2_bkey_has_device(bkey_i_to_s(insert), p.ptr.dev)))
199 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), ptr);
201 durability = bch2_bkey_durability(c, bkey_i_to_s_c(insert)) +
202 bch2_bkey_durability(c, bkey_i_to_s_c(&new->k_i));
204 /* Now, drop excess replicas: */
205 restart_drop_extra_replicas:
206 bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs(bkey_i_to_s(insert)), p, entry) {
207 unsigned ptr_durability = bch2_extent_ptr_durability(c, &p);
210 durability - ptr_durability >= m->op.opts.data_replicas) {
211 durability -= ptr_durability;
212 bch2_bkey_drop_ptr_noerror(bkey_i_to_s(insert), &entry->ptr);
214 * Currently, we're dropping unneeded replicas
215 * instead of marking them as cached, since
216 * cached data in stripe buckets prevents them
218 bch2_extent_ptr_set_cached(bkey_i_to_s(insert), &entry->ptr);
220 goto restart_drop_extra_replicas;
224 /* Finally, add the pointers we just wrote: */
225 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry)
226 bch2_extent_ptr_decoded_append(insert, &p);
228 bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
229 bch2_extent_normalize(c, bkey_i_to_s(insert));
231 ret = bch2_sum_sector_overwrites(trans, &iter, insert,
232 &should_check_enospc,
234 &disk_sectors_delta);
238 if (disk_sectors_delta > (s64) op->res.sectors) {
239 ret = bch2_disk_reservation_add(c, &op->res,
240 disk_sectors_delta - op->res.sectors,
242 ? BCH_DISK_RESERVATION_NOFAIL : 0);
247 next_pos = insert->k.p;
249 if (!bkey_eq(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) {
250 ret = insert_snapshot_whiteouts(trans, m->btree_id, k.k->p,
251 bkey_start_pos(&insert->k));
256 if (!bkey_eq(insert->k.p, k.k->p)) {
257 ret = insert_snapshot_whiteouts(trans, m->btree_id,
258 k.k->p, insert->k.p);
263 ret = bch2_trans_update(trans, &iter, insert,
264 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
265 bch2_trans_commit(trans, &op->res,
267 BTREE_INSERT_NOCHECK_RW|
269 m->data_opts.btree_insert_flags);
271 bch2_btree_iter_set_pos(&iter, next_pos);
273 this_cpu_add(c->counters[BCH_COUNTER_move_extent_finish], new->k.size);
274 trace_move_extent_finish(&new->k);
277 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
282 while (bkey_ge(iter.pos, bch2_keylist_front(keys)->k.p)) {
283 bch2_keylist_pop_front(keys);
284 if (bch2_keylist_empty(keys))
289 if (m->ctxt && m->ctxt->stats) {
290 BUG_ON(k.k->p.offset <= iter.pos.offset);
291 atomic64_inc(&m->ctxt->stats->keys_raced);
292 atomic64_add(k.k->p.offset - iter.pos.offset,
293 &m->ctxt->stats->sectors_raced);
296 this_cpu_add(c->counters[BCH_COUNTER_move_extent_fail], new->k.size);
297 trace_move_extent_fail(&new->k);
299 bch2_btree_iter_advance(&iter);
303 bch2_trans_iter_exit(trans, &iter);
304 bch2_bkey_buf_exit(&_insert, c);
305 bch2_bkey_buf_exit(&_new, c);
306 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
310 int bch2_data_update_index_update(struct bch_write_op *op)
312 return bch2_trans_run(op->c, __bch2_data_update_index_update(&trans, op));
315 void bch2_data_update_read_done(struct data_update *m,
316 struct bch_extent_crc_unpacked crc)
318 /* write bio must own pages: */
319 BUG_ON(!m->op.wbio.bio.bi_vcnt);
322 m->op.wbio.bio.bi_iter.bi_size = crc.compressed_size << 9;
324 closure_call(&m->op.cl, bch2_write, NULL, NULL);
327 void bch2_data_update_exit(struct data_update *update)
329 struct bch_fs *c = update->op.c;
330 struct bkey_ptrs_c ptrs =
331 bch2_bkey_ptrs_c(bkey_i_to_s_c(update->k.k));
332 const struct bch_extent_ptr *ptr;
334 bkey_for_each_ptr(ptrs, ptr) {
335 if (c->opts.nocow_enabled)
336 bch2_bucket_nocow_unlock(&c->nocow_locks,
337 PTR_BUCKET_POS(c, ptr), 0);
338 percpu_ref_put(&bch_dev_bkey_exists(c, ptr->dev)->ref);
341 bch2_bkey_buf_exit(&update->k, c);
342 bch2_disk_reservation_put(c, &update->op.res);
343 bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
346 void bch2_update_unwritten_extent(struct btree_trans *trans,
347 struct data_update *update)
349 struct bch_fs *c = update->op.c;
350 struct bio *bio = &update->op.wbio.bio;
351 struct bkey_i_extent *e;
352 struct write_point *wp;
353 struct bch_extent_ptr *ptr;
355 struct btree_iter iter;
359 closure_init_stack(&cl);
360 bch2_keylist_init(&update->op.insert_keys, update->op.inline_keys);
362 while (bio_sectors(bio)) {
363 unsigned sectors = bio_sectors(bio);
365 bch2_trans_iter_init(trans, &iter, update->btree_id, update->op.pos,
367 ret = lockrestart_do(trans, ({
368 k = bch2_btree_iter_peek_slot(&iter);
371 bch2_trans_iter_exit(trans, &iter);
373 if (ret || !bch2_extents_match(k, bkey_i_to_s_c(update->k.k)))
376 e = bkey_extent_init(update->op.insert_keys.top);
377 e->k.p = update->op.pos;
379 ret = bch2_alloc_sectors_start_trans(trans,
382 update->op.write_point,
383 &update->op.devs_have,
384 update->op.nr_replicas,
385 update->op.nr_replicas,
386 update->op.alloc_reserve,
388 if (bch2_err_matches(ret, BCH_ERR_operation_blocked)) {
389 bch2_trans_unlock(trans);
397 sectors = min(sectors, wp->sectors_free);
399 bch2_key_resize(&e->k, sectors);
401 bch2_open_bucket_get(c, wp, &update->op.open_buckets);
402 bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, sectors, false);
403 bch2_alloc_sectors_done(c, wp);
405 bio_advance(bio, sectors << 9);
406 update->op.pos.offset += sectors;
408 extent_for_each_ptr(extent_i_to_s(e), ptr)
409 ptr->unwritten = true;
410 bch2_keylist_push(&update->op.insert_keys);
412 ret = __bch2_data_update_index_update(trans, &update->op);
414 bch2_open_buckets_put(c, &update->op.open_buckets);
420 if ((atomic_read(&cl.remaining) & CLOSURE_REMAINING_MASK) != 1) {
421 bch2_trans_unlock(trans);
426 int bch2_data_update_init(struct btree_trans *trans,
427 struct moving_context *ctxt,
428 struct data_update *m,
429 struct write_point_specifier wp,
430 struct bch_io_opts io_opts,
431 struct data_update_opts data_opts,
432 enum btree_id btree_id,
435 struct bch_fs *c = trans->c;
436 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
437 const union bch_extent_entry *entry;
438 struct extent_ptr_decoded p;
439 const struct bch_extent_ptr *ptr;
440 unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
441 unsigned ptrs_locked = 0;
444 bch2_bkey_buf_init(&m->k);
445 bch2_bkey_buf_reassemble(&m->k, c, k);
446 m->btree_id = btree_id;
447 m->data_opts = data_opts;
449 bch2_write_op_init(&m->op, c, io_opts);
450 m->op.pos = bkey_start_pos(k.k);
451 m->op.version = k.k->version;
452 m->op.target = data_opts.target;
453 m->op.write_point = wp;
454 m->op.nr_replicas = 0;
455 m->op.flags |= BCH_WRITE_PAGES_STABLE|
456 BCH_WRITE_PAGES_OWNED|
457 BCH_WRITE_DATA_ENCODED|
459 m->data_opts.write_flags;
460 m->op.compression_type =
461 bch2_compression_opt_to_type[io_opts.background_compression ?:
462 io_opts.compression];
463 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
464 m->op.alloc_reserve = RESERVE_movinggc;
466 bkey_for_each_ptr(ptrs, ptr)
467 percpu_ref_get(&bch_dev_bkey_exists(c, ptr->dev)->ref);
470 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
473 if (((1U << i) & m->data_opts.rewrite_ptrs)) {
474 BUG_ON(p.ptr.cached);
476 if (crc_is_compressed(p.crc))
477 reserve_sectors += k.k->size;
479 m->op.nr_replicas += bch2_extent_ptr_durability(c, &p);
480 } else if (!p.ptr.cached) {
481 bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
485 * op->csum_type is normally initialized from the fs/file's
486 * current options - but if an extent is encrypted, we require
487 * that it stays encrypted:
489 if (bch2_csum_type_is_encryption(p.crc.csum_type)) {
490 m->op.nonce = p.crc.nonce + p.crc.offset;
491 m->op.csum_type = p.crc.csum_type;
494 if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
495 m->op.incompressible = true;
497 if (c->opts.nocow_enabled) {
499 move_ctxt_wait_event(ctxt, trans,
500 (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
501 PTR_BUCKET_POS(c, &p.ptr), 0)) ||
502 !atomic_read(&ctxt->read_sectors));
505 bch2_bucket_nocow_lock(&c->nocow_locks,
506 PTR_BUCKET_POS(c, &p.ptr), 0);
508 if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
509 PTR_BUCKET_POS(c, &p.ptr), 0)) {
510 ret = -BCH_ERR_nocow_lock_blocked;
514 ptrs_locked |= (1U << i);
520 if (reserve_sectors) {
521 ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
522 m->data_opts.extra_replicas
524 : BCH_DISK_RESERVATION_NOFAIL);
529 m->op.nr_replicas += m->data_opts.extra_replicas;
530 m->op.nr_replicas_required = m->op.nr_replicas;
532 BUG_ON(!m->op.nr_replicas);
534 /* Special handling required: */
535 if (bkey_extent_is_unwritten(k))
536 return -BCH_ERR_unwritten_extent_update;
540 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
541 if ((1U << i) & ptrs_locked)
542 bch2_bucket_nocow_unlock(&c->nocow_locks,
543 PTR_BUCKET_POS(c, &p.ptr), 0);
544 percpu_ref_put(&bch_dev_bkey_exists(c, p.ptr.dev)->ref);
548 bch2_bkey_buf_exit(&m->k, c);
549 bch2_bio_free_pages_pool(c, &m->op.wbio.bio);
553 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
555 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
556 const struct bch_extent_ptr *ptr;
559 bkey_for_each_ptr(ptrs, ptr) {
560 if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
561 opts->kill_ptrs |= 1U << i;
562 opts->rewrite_ptrs ^= 1U << i;